Merge of new 2.2 code into HEAD (Gerald I hate you :-) :-). Allows new SAMR
[tprouty/samba.git] / source / smbd / fileio.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    read/write to a files_struct
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25
26 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
27
28 /****************************************************************************
29 seek a file. Try to avoid the seek if possible
30 ****************************************************************************/
31
32 SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
33 {
34   SMB_OFF_T offset = 0;
35   SMB_OFF_T seek_ret;
36
37   if (fsp->print_file && lp_postscript(fsp->conn->service))
38     offset = 3;
39
40   seek_ret = fsp->conn->vfs_ops.lseek(fsp,fsp->fd,pos+offset,SEEK_SET);
41
42   /*
43    * We want to maintain the fiction that we can seek
44    * on a fifo for file system purposes. This allows 
45    * people to set up UNIX fifo's that feed data to Windows
46    * applications. JRA.
47    */
48
49   if((seek_ret == -1) && (errno == ESPIPE)) {
50     seek_ret = pos+offset;
51     errno = 0;
52   }
53
54   if((seek_ret == -1) || (seek_ret != pos+offset)) {
55     DEBUG(0,("seek_file: sys_lseek failed. Error was %s\n", strerror(errno) ));
56     fsp->pos = -1;
57     return -1;
58   }
59
60   fsp->pos = seek_ret - offset;
61
62   DEBUG(10,("seek_file: requested pos = %.0f, new pos = %.0f\n",
63         (double)(pos+offset), (double)fsp->pos ));
64
65   return(fsp->pos);
66 }
67
68 /****************************************************************************
69  Read from write cache if we can.
70 ****************************************************************************/
71
72
73 BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
74 {
75   write_cache *wcp = fsp->wcp;
76
77   if(!wcp)
78     return False;
79
80   if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
81     return False;
82
83   memcpy(data, wcp->data + (pos - wcp->offset), n);
84
85   DO_PROFILE_INC(writecache_read_hits);
86
87   return True;
88 }
89
90 /****************************************************************************
91 read from a file
92 ****************************************************************************/
93
94 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
95 {
96   ssize_t ret=0,readret;
97
98   /* you can't read from print files */
99   if (fsp->print_file) {
100           return -1;
101   }
102
103   /*
104    * Serve from write cache if we can.
105    */
106   if(read_from_write_cache(fsp, data, pos, n))
107     return n;
108
109   flush_write_cache(fsp, READ_FLUSH);
110
111   if (seek_file(fsp,pos) == -1) {
112     DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
113     return(ret);
114   }
115   
116   if (n > 0) {
117     readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
118     if (readret == -1)
119       return -1;
120     if (readret > 0) ret += readret;
121   }
122
123   return(ret);
124 }
125
126 /* how many write cache buffers have been allocated */
127 static unsigned int allocated_write_caches;
128
129 /****************************************************************************
130  *Really* write to a file.
131 ****************************************************************************/
132
133 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
134 {
135   if ((pos != -1) && (seek_file(fsp,pos) == -1))
136     return -1;
137
138   return vfs_write_data(fsp,data,n);
139 }
140
141 /****************************************************************************
142 write to a file
143 ****************************************************************************/
144
145 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
146 {
147   write_cache *wcp = fsp->wcp;
148   ssize_t total_written = 0;
149   int write_path = -1; 
150
151   if (fsp->print_file) {
152           return print_job_write(fsp->print_jobid, data, n);
153   }
154
155   if (!fsp->can_write) {
156     errno = EPERM;
157     return(0);
158   }
159
160   if (!fsp->modified) {
161     SMB_STRUCT_STAT st;
162     fsp->modified = True;
163
164     if (fsp->conn->vfs_ops.fstat(fsp,fsp->fd,&st) == 0) {
165       int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
166       if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) { 
167         file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
168       }
169
170       /*
171        * If this is the first write and we have an exclusive oplock then setup
172        * the write cache.
173        */
174
175       if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
176         setup_write_cache(fsp, st.st_size);
177         wcp = fsp->wcp;
178       } 
179     }  
180   }
181
182 #ifdef WITH_PROFILE
183   DO_PROFILE_INC(writecache_total_writes);
184   if (!fsp->oplock_type) {
185     DO_PROFILE_INC(writecache_non_oplock_writes);
186   }
187 #endif
188
189   /*
190    * If this file is level II oplocked then we need
191    * to grab the shared memory lock and inform all
192    * other files with a level II lock that they need
193    * to flush their read caches. We keep the lock over
194    * the shared memory area whilst doing this.
195    */
196
197   release_level_2_oplocks_on_change(fsp);
198
199 #ifdef WITH_PROFILE
200   if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
201     DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
202 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
203         profile_p->writecache_init_writes,
204         profile_p->writecache_abutted_writes,
205         profile_p->writecache_total_writes,
206         profile_p->writecache_non_oplock_writes,
207         profile_p->writecache_allocated_write_caches,
208         profile_p->writecache_num_write_caches,
209         profile_p->writecache_direct_writes,
210         profile_p->writecache_num_perfect_writes,
211         profile_p->writecache_read_hits ));
212
213     DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
214         profile_p->writecache_flushed_writes[SEEK_FLUSH],
215         profile_p->writecache_flushed_writes[READ_FLUSH],
216         profile_p->writecache_flushed_writes[WRITE_FLUSH],
217         profile_p->writecache_flushed_writes[READRAW_FLUSH],
218         profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
219         profile_p->writecache_flushed_writes[CLOSE_FLUSH],
220         profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
221   }
222 #endif
223
224   if(!wcp) {
225     DO_PROFILE_INC(writecache_direct_writes);
226     return real_write_file(fsp, data, pos, n);
227   }
228
229   DEBUG(9,("write_file(fd=%d pos=%d size=%d) wofs=%d wsize=%d\n",
230            fsp->fd, (int)pos, (int)n, (int)wcp->offset, (int)wcp->data_size));
231
232   /* 
233    * If we have active cache and it isn't contiguous then we flush.
234    * NOTE: There is a small problem with running out of disk ....
235    */
236
237   if (wcp->data_size) {
238
239     BOOL cache_flush_needed = False;
240
241     if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
242       
243       /*
244        * Start of write overlaps or abutts the existing data.
245        */
246
247       size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
248
249       memcpy(wcp->data + (pos - wcp->offset), data, data_used);
250
251       /*
252        * Update the current buffer size with the new data.
253        */
254
255       if(pos + data_used > wcp->offset + wcp->data_size)
256         wcp->data_size = pos + data_used - wcp->offset;
257
258       /*
259        * If we used all the data then
260        * return here.
261        */
262
263       if(n == data_used)
264         return n;
265       else
266         cache_flush_needed = True;
267
268       /*
269        * Move the start of data forward by the amount used,
270        * cut down the amount left by the same amount.
271        */
272
273       data += data_used;
274       pos += data_used;
275       n -= data_used;
276
277       DO_PROFILE_INC(writecache_abutted_writes);
278       total_written = data_used;
279
280       write_path = 1;
281
282     } else if ((pos < wcp->offset) && (pos + n > wcp->offset) && 
283                (pos + n <= wcp->offset + wcp->alloc_size)) {
284
285       /*
286        * End of write overlaps the existing data.
287        */
288
289       size_t data_used = pos + n - wcp->offset;
290
291       memcpy(wcp->data, data + n - data_used, data_used);
292
293       /*
294        * Update the current buffer size with the new data.
295        */
296
297       if(pos + n > wcp->offset + wcp->data_size)
298         wcp->data_size = pos + n - wcp->offset;
299
300       /*
301        * We don't need to move the start of data, but we
302        * cut down the amount left by the amount used.
303        */
304
305       n -= data_used;
306
307       /*
308        * We cannot have used all the data here.
309        */
310
311       cache_flush_needed = True;
312
313       DO_PROFILE_INC(writecache_abutted_writes);
314       total_written = data_used;
315
316       write_path = 2;
317
318     } else if ( (pos >= wcp->file_size) && 
319                 (pos > wcp->offset + wcp->data_size) && 
320                 (pos < wcp->offset + wcp->alloc_size) ) {
321
322       /*
323        * Non-contiguous write part of which fits within
324        * the cache buffer and is extending the file.
325        */
326
327       size_t data_used;
328
329       if(pos + n <= wcp->offset + wcp->alloc_size)
330         data_used = n;
331       else
332         data_used = wcp->offset + wcp->alloc_size - pos;
333
334       /*
335        * Fill in the non-continuous area with zeros.
336        */
337
338       memset(wcp->data + wcp->data_size, '\0',
339              pos - (wcp->offset + wcp->data_size) );
340
341       memcpy(wcp->data + (pos - wcp->offset), data, data_used);
342
343       /*
344        * Update the current buffer size with the new data.
345        */
346
347       if(pos + data_used > wcp->offset + wcp->data_size)
348         wcp->data_size = pos + data_used - wcp->offset;
349
350       /*
351        * Update the known file length.
352        */
353
354       wcp->file_size = wcp->offset + wcp->data_size;
355
356       /*
357        * If we used all the data then
358        * return here.
359        */
360
361       if(n == data_used)
362         return n;
363       else
364         cache_flush_needed = True;
365
366       /*
367        * Move the start of data forward by the amount used,
368        * cut down the amount left by the same amount.
369        */
370
371       data += data_used;
372       pos += data_used;
373       n -= data_used;
374
375       DO_PROFILE_INC(writecache_abutted_writes);
376       total_written = data_used;
377
378       write_path = 3;
379
380     } else {
381
382       /*
383        * Write is bigger than buffer, or there is no overlap on the
384        * low or high ends.
385        */
386
387       DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
388 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
389
390       /*
391        * Update the file size if needed.
392        */
393
394       if(pos + n > wcp->file_size)
395         wcp->file_size = pos + n;
396
397       /*
398        * If write would fit in the cache, and is larger than
399        * the data already in the cache, flush the cache and
400        * preferentially copy the data new data into it. Otherwise
401        * just write the data directly.
402        */
403
404       if ( n <= wcp->alloc_size && n > wcp->data_size) {
405         cache_flush_needed = True;
406       } else {
407         DO_PROFILE_INC(writecache_direct_writes);
408         return real_write_file(fsp, data, pos, n);
409       }
410
411       write_path = 4;
412
413     }
414
415     if(wcp->data_size > wcp->file_size)
416       wcp->file_size = wcp->data_size;
417
418     if (cache_flush_needed) {
419       DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
420 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
421              write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
422              (double)wcp->offset, (unsigned int)wcp->data_size ));
423
424       flush_write_cache(fsp, WRITE_FLUSH);
425     }
426   }
427
428   /*
429    * If the write request is bigger than the cache
430    * size, write it all out.
431    */
432
433   if (n > wcp->alloc_size ) {
434     if(real_write_file(fsp, data, pos, n) == -1)
435       return -1;
436     DO_PROFILE_INC(writecache_direct_writes);
437     return total_written + n;
438   }
439
440   /*
441    * If there's any data left, cache it.
442    */
443
444   if (n) {
445 #ifdef WITH_PROFILE
446     if (wcp->data_size) {
447       DO_PROFILE_INC(writecache_abutted_writes);
448     } else {
449       DO_PROFILE_INC(writecache_init_writes);
450     }
451 #endif
452     memcpy(wcp->data+wcp->data_size, data, n);
453     if (wcp->data_size == 0) {
454       wcp->offset = pos;
455       DO_PROFILE_INC(writecache_num_write_caches);
456     }
457     wcp->data_size += n;
458     DEBUG(9,("cache return %u\n", (unsigned int)n));
459     total_written += n;
460     return total_written; /* .... that's a write :) */
461   }
462   
463   return total_written;
464 }
465
466 /****************************************************************************
467  Delete the write cache structure.
468 ****************************************************************************/
469
470 void delete_write_cache(files_struct *fsp)
471 {
472   write_cache *wcp;
473
474   if(!fsp)
475     return;
476
477   if(!(wcp = fsp->wcp))
478     return;
479
480   DO_PROFILE_DEC(writecache_allocated_write_caches);
481   allocated_write_caches--;
482
483   SMB_ASSERT(wcp->data_size == 0);
484
485   free(wcp->data);
486   free(wcp);
487
488   fsp->wcp = NULL;
489
490   DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
491
492 }
493
494 /****************************************************************************
495  Setup the write cache structure.
496 ****************************************************************************/
497
498 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
499 {
500   ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
501   write_cache *wcp;
502
503   if (allocated_write_caches >= MAX_WRITE_CACHES) 
504         return False;
505
506   if(alloc_size == 0 || fsp->wcp)
507     return False;
508
509   if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
510     DEBUG(0,("setup_write_cache: malloc fail.\n"));
511     return False;
512   }
513
514   wcp->file_size = file_size;
515   wcp->offset = 0;
516   wcp->alloc_size = alloc_size;
517   wcp->data_size = 0;
518   if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
519     DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
520           (unsigned int)wcp->alloc_size ));
521     free(wcp);
522     return False;
523   }
524
525   fsp->wcp = wcp;
526   DO_PROFILE_INC(writecache_allocated_write_caches);
527   allocated_write_caches++;
528
529   DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
530                 fsp->fsp_name, wcp->alloc_size ));
531
532   return True;
533 }
534
535 /****************************************************************************
536  Cope with a size change.
537 ****************************************************************************/
538
539 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
540 {
541   if(fsp->wcp) {
542     flush_write_cache(fsp, SIZECHANGE_FLUSH);
543     fsp->wcp->file_size = file_size;
544   }
545 }
546
547 /*******************************************************************
548  Flush a write cache struct to disk.
549 ********************************************************************/
550
551 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
552 {
553   write_cache *wcp = fsp->wcp;
554   size_t data_size;
555
556   if(!wcp || !wcp->data_size)
557     return 0;
558
559   data_size = wcp->data_size;
560   wcp->data_size = 0;
561
562   DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
563
564   DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
565            fsp->fd, (double)wcp->offset, (unsigned int)data_size));
566
567 #ifdef WITH_PROFILE
568   if(data_size == wcp->alloc_size)
569     DO_PROFILE_INC(writecache_num_perfect_writes);
570 #endif
571
572   return real_write_file(fsp, wcp->data, wcp->offset, data_size);
573 }
574
575 /*******************************************************************
576 sync a file
577 ********************************************************************/
578
579 void sync_file(connection_struct *conn, files_struct *fsp)
580 {
581     if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
582       flush_write_cache(fsp, SYNC_FLUSH);
583       conn->vfs_ops.fsync(fsp,fsp->fd);
584     }
585 }