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