]> git.samba.org - samba.git/blob - source3/smbd/fileio.c
Add new parameter, "min receivefile size" (by default set
[samba.git] / source3 / 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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 static bool setup_write_cache(files_struct *, SMB_OFF_T);
25
26 /****************************************************************************
27  Read from write cache if we can.
28 ****************************************************************************/
29
30 static bool read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
31 {
32         write_cache *wcp = fsp->wcp;
33
34         if(!wcp) {
35                 return False;
36         }
37
38         if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
39                 return False;
40         }
41
42         memcpy(data, wcp->data + (pos - wcp->offset), n);
43
44         DO_PROFILE_INC(writecache_read_hits);
45
46         return True;
47 }
48
49 /****************************************************************************
50  Read from a file.
51 ****************************************************************************/
52
53 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
54 {
55         ssize_t ret=0,readret;
56
57         /* you can't read from print files */
58         if (fsp->print_file) {
59                 return -1;
60         }
61
62         /*
63          * Serve from write cache if we can.
64          */
65
66         if(read_from_write_cache(fsp, data, pos, n)) {
67                 fsp->fh->pos = pos + n;
68                 fsp->fh->position_information = fsp->fh->pos;
69                 return n;
70         }
71
72         flush_write_cache(fsp, READ_FLUSH);
73
74         fsp->fh->pos = pos;
75
76         if (n > 0) {
77 #ifdef DMF_FIX
78                 int numretries = 3;
79 tryagain:
80                 readret = SMB_VFS_PREAD(fsp,fsp->fh->fd,data,n,pos);
81
82                 if (readret == -1) {
83                         if ((errno == EAGAIN) && numretries) {
84                                 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
85                                 (void)sleep(10);
86                                 --numretries;
87                                 goto tryagain;
88                         }
89                         return -1;
90                 }
91 #else /* NO DMF fix. */
92                 readret = SMB_VFS_PREAD(fsp,fsp->fh->fd,data,n,pos);
93
94                 if (readret == -1) {
95                         return -1;
96                 }
97 #endif
98                 if (readret > 0) {
99                         ret += readret;
100                 }
101         }
102
103         DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
104                 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
105
106         fsp->fh->pos += ret;
107         fsp->fh->position_information = fsp->fh->pos;
108
109         return(ret);
110 }
111
112 /* how many write cache buffers have been allocated */
113 static unsigned int allocated_write_caches;
114
115 /****************************************************************************
116  *Really* write to a file.
117 ****************************************************************************/
118
119 static ssize_t real_write_file(struct smb_request *req,
120                                 files_struct *fsp,
121                                 const char *data,
122                                 SMB_OFF_T pos,
123                                 size_t n)
124 {
125         ssize_t ret;
126
127         if (pos == -1) {
128                 ret = vfs_write_data(req, fsp, data, n);
129         } else {
130                 fsp->fh->pos = pos;
131                 if (pos && lp_strict_allocate(SNUM(fsp->conn))) {
132                         if (vfs_fill_sparse(fsp, pos) == -1) {
133                                 return -1;
134                         }
135                 }
136                 ret = vfs_pwrite_data(req, fsp, data, n, pos);
137         }
138
139         DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
140                 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
141
142         if (ret != -1) {
143                 fsp->fh->pos += ret;
144
145                 /*
146                  * It turns out that setting the last write time from a Windows
147                  * client stops any subsequent writes from updating the write time.
148                  * Doing this after the write gives a race condition here where
149                  * a stat may see the changed write time before we reset it here,
150                  * but it's cheaper than having to store the write time in shared
151                  * memory and look it up using dev/inode across all running smbd's.
152                  * The 99% solution will hopefully be good enough in this case. JRA.
153                  */
154
155                 if (!null_timespec(fsp->pending_modtime)) {
156                         set_filetime(fsp->conn, fsp->fsp_name, fsp->pending_modtime);
157
158                         /* If we didn't get the "set modtime" call ourselves, we must
159                            store the last write time to restore on close. JRA. */
160                         if (!fsp->pending_modtime_owner) {
161                                 fsp->last_write_time = timespec_current();
162                         }
163                 }
164
165 /* Yes - this is correct - writes don't update this. JRA. */
166 /* Found by Samba4 tests. */
167 #if 0
168                 fsp->position_information = fsp->pos;
169 #endif
170         }
171
172         return ret;
173 }
174
175 /****************************************************************************
176  File size cache change.
177  Updates size on disk but doesn't flush the cache.
178 ****************************************************************************/
179
180 static int wcp_file_size_change(files_struct *fsp)
181 {
182         int ret;
183         write_cache *wcp = fsp->wcp;
184
185         wcp->file_size = wcp->offset + wcp->data_size;
186         ret = SMB_VFS_FTRUNCATE(fsp, fsp->fh->fd, wcp->file_size);
187         if (ret == -1) {
188                 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f error %s\n",
189                         fsp->fsp_name, (double)wcp->file_size, strerror(errno) ));
190         }
191         return ret;
192 }
193
194 /****************************************************************************
195  Write to a file.
196 ****************************************************************************/
197
198 ssize_t write_file(struct smb_request *req,
199                         files_struct *fsp,
200                         const char *data,
201                         SMB_OFF_T pos,
202                         size_t n)
203 {
204         write_cache *wcp = fsp->wcp;
205         ssize_t total_written = 0;
206         int write_path = -1;
207
208         if (fsp->print_file) {
209                 fstring sharename;
210                 uint32 jobid;
211
212                 if (!rap_to_pjobid(fsp->rap_print_jobid, sharename, &jobid)) {
213                         DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
214                                                 (unsigned int)fsp->rap_print_jobid ));
215                         errno = EBADF;
216                         return -1;
217                 }
218
219                 return print_job_write(SNUM(fsp->conn), jobid, data, pos, n);
220         }
221
222         if (!fsp->can_write) {
223                 errno = EPERM;
224                 return -1;
225         }
226
227         if (!fsp->modified) {
228                 SMB_STRUCT_STAT st;
229                 fsp->modified = True;
230
231                 if (SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st) == 0) {
232                         int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
233                         if ((lp_store_dos_attributes(SNUM(fsp->conn)) || MAP_ARCHIVE(fsp->conn)) && !IS_DOS_ARCHIVE(dosmode)) {
234                                 file_set_dosmode(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st, False);
235                         }
236
237                         /*
238                          * If this is the first write and we have an exclusive oplock then setup
239                          * the write cache.
240                          */
241
242                         if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
243                                 setup_write_cache(fsp, st.st_size);
244                                 wcp = fsp->wcp;
245                         }
246                 }
247         }
248
249 #ifdef WITH_PROFILE
250         DO_PROFILE_INC(writecache_total_writes);
251         if (!fsp->oplock_type) {
252                 DO_PROFILE_INC(writecache_non_oplock_writes);
253         }
254 #endif
255
256         /*
257          * If this file is level II oplocked then we need
258          * to grab the shared memory lock and inform all
259          * other files with a level II lock that they need
260          * to flush their read caches. We keep the lock over
261          * the shared memory area whilst doing this.
262          */
263
264         release_level_2_oplocks_on_change(fsp);
265
266 #ifdef WITH_PROFILE
267         if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
268                 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
269 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
270                         profile_p->writecache_init_writes,
271                         profile_p->writecache_abutted_writes,
272                         profile_p->writecache_total_writes,
273                         profile_p->writecache_non_oplock_writes,
274                         profile_p->writecache_allocated_write_caches,
275                         profile_p->writecache_num_write_caches,
276                         profile_p->writecache_direct_writes,
277                         profile_p->writecache_num_perfect_writes,
278                         profile_p->writecache_read_hits ));
279
280                 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
281                         profile_p->writecache_flushed_writes[SEEK_FLUSH],
282                         profile_p->writecache_flushed_writes[READ_FLUSH],
283                         profile_p->writecache_flushed_writes[WRITE_FLUSH],
284                         profile_p->writecache_flushed_writes[READRAW_FLUSH],
285                         profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
286                         profile_p->writecache_flushed_writes[CLOSE_FLUSH],
287                         profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
288         }
289 #endif
290
291         if (wcp && req->unread_bytes) {
292                 /* If we're using receivefile don't
293                  * deal with a write cache.
294                  */
295                 flush_write_cache(fsp, WRITE_FLUSH);
296                 delete_write_cache(fsp);
297                 wcp = NULL;
298         }
299
300         if(!wcp) {
301                 DO_PROFILE_INC(writecache_direct_writes);
302                 total_written = real_write_file(req, fsp, data, pos, n);
303                 return total_written;
304         }
305
306         DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
307                 fsp->fsp_name, fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
308
309         fsp->fh->pos = pos + n;
310
311         /*
312          * If we have active cache and it isn't contiguous then we flush.
313          * NOTE: There is a small problem with running out of disk ....
314          */
315
316         if (wcp->data_size) {
317                 bool cache_flush_needed = False;
318
319                 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
320       
321                         /* ASCII art.... JRA.
322
323       +--------------+-----
324       | Cached data  | Rest of allocated cache buffer....
325       +--------------+-----
326
327             +-------------------+
328             | Data to write     |
329             +-------------------+
330
331                         */
332
333                         /*
334                          * Start of write overlaps or abutts the existing data.
335                          */
336
337                         size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
338
339                         memcpy(wcp->data + (pos - wcp->offset), data, data_used);
340
341                         /*
342                          * Update the current buffer size with the new data.
343                          */
344
345                         if(pos + data_used > wcp->offset + wcp->data_size) {
346                                 wcp->data_size = pos + data_used - wcp->offset;
347                         }
348
349                         /*
350                          * Update the file size if changed.
351                          */
352
353                         if (wcp->offset + wcp->data_size > wcp->file_size) {
354                                 if (wcp_file_size_change(fsp) == -1) {
355                                         return -1;
356                                 }
357                         }
358
359                         /*
360                          * If we used all the data then
361                          * return here.
362                          */
363
364                         if(n == data_used) {
365                                 return n;
366                         } else {
367                                 cache_flush_needed = True;
368                         }
369                         /*
370                          * Move the start of data forward by the amount used,
371                          * cut down the amount left by the same amount.
372                          */
373
374                         data += data_used;
375                         pos += data_used;
376                         n -= data_used;
377
378                         DO_PROFILE_INC(writecache_abutted_writes);
379                         total_written = data_used;
380
381                         write_path = 1;
382
383                 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) && 
384                                         (pos + n <= wcp->offset + wcp->alloc_size)) {
385
386                         /* ASCII art.... JRA.
387
388                         +---------------+
389                         | Cache buffer  |
390                         +---------------+
391
392             +-------------------+
393             | Data to write     |
394             +-------------------+
395
396                         */
397
398                         /*
399                          * End of write overlaps the existing data.
400                          */
401
402                         size_t data_used = pos + n - wcp->offset;
403
404                         memcpy(wcp->data, data + n - data_used, data_used);
405
406                         /*
407                          * Update the current buffer size with the new data.
408                          */
409
410                         if(pos + n > wcp->offset + wcp->data_size) {
411                                 wcp->data_size = pos + n - wcp->offset;
412                         }
413
414                         /*
415                          * Update the file size if changed.
416                          */
417
418                         if (wcp->offset + wcp->data_size > wcp->file_size) {
419                                 if (wcp_file_size_change(fsp) == -1) {
420                                         return -1;
421                                 }
422                         }
423
424                         /*
425                          * We don't need to move the start of data, but we
426                          * cut down the amount left by the amount used.
427                          */
428
429                         n -= data_used;
430
431                         /*
432                          * We cannot have used all the data here.
433                          */
434
435                         cache_flush_needed = True;
436
437                         DO_PROFILE_INC(writecache_abutted_writes);
438                         total_written = data_used;
439
440                         write_path = 2;
441
442                 } else if ( (pos >= wcp->file_size) && 
443                                         (wcp->offset + wcp->data_size == wcp->file_size) &&
444                                         (pos > wcp->offset + wcp->data_size) && 
445                                         (pos < wcp->offset + wcp->alloc_size) ) {
446
447                         /* ASCII art.... JRA.
448
449                        End of file ---->|
450
451                         +---------------+---------------+
452                         | Cached data   | Cache buffer  |
453                         +---------------+---------------+
454
455                                               +-------------------+
456                                               | Data to write     |
457                                               +-------------------+
458
459                         */
460
461                         /*
462                          * Non-contiguous write part of which fits within
463                          * the cache buffer and is extending the file
464                          * and the cache contents reflect the current
465                          * data up to the current end of the file.
466                          */
467
468                         size_t data_used;
469
470                         if(pos + n <= wcp->offset + wcp->alloc_size) {
471                                 data_used = n;
472                         } else {
473                                 data_used = wcp->offset + wcp->alloc_size - pos;
474                         }
475
476                         /*
477                          * Fill in the non-continuous area with zeros.
478                          */
479
480                         memset(wcp->data + wcp->data_size, '\0',
481                                 pos - (wcp->offset + wcp->data_size) );
482
483                         memcpy(wcp->data + (pos - wcp->offset), data, data_used);
484
485                         /*
486                          * Update the current buffer size with the new data.
487                          */
488
489                         if(pos + data_used > wcp->offset + wcp->data_size) {
490                                 wcp->data_size = pos + data_used - wcp->offset;
491                         }
492
493                         /*
494                          * Update the file size if changed.
495                          */
496
497                         if (wcp->offset + wcp->data_size > wcp->file_size) {
498                                 if (wcp_file_size_change(fsp) == -1) {
499                                         return -1;
500                                 }
501                         }
502
503                         /*
504                          * If we used all the data then
505                          * return here.
506                          */
507
508                         if(n == data_used) {
509                                 return n;
510                         } else {
511                                 cache_flush_needed = True;
512                         }
513
514                         /*
515                          * Move the start of data forward by the amount used,
516                          * cut down the amount left by the same amount.
517                          */
518
519                         data += data_used;
520                         pos += data_used;
521                         n -= data_used;
522
523                         DO_PROFILE_INC(writecache_abutted_writes);
524                         total_written = data_used;
525
526                         write_path = 3;
527
528                 } else if ( (pos >= wcp->file_size) &&
529                             (n == 1) &&
530                             (wcp->file_size == wcp->offset + wcp->data_size) &&
531                             (pos < wcp->file_size + wcp->alloc_size)) {
532
533                         /*
534
535                 End of file ---->|
536
537                  +---------------+---------------+
538                  | Cached data   | Cache buffer  |
539                  +---------------+---------------+
540
541                                  |<------- allocated size ---------------->|
542
543                                                          +--------+
544                                                          | 1 Byte |
545                                                          +--------+
546
547                         MS-Office seems to do this a lot to determine if there's enough
548                         space on the filesystem to write a new file.
549
550                         Change to :
551
552                 End of file ---->|
553                                  +-----------------------+--------+
554                                  | Zeroed Cached data    | 1 Byte |
555                                  +-----------------------+--------+
556                         */
557
558                         flush_write_cache(fsp, WRITE_FLUSH);
559                         wcp->offset = wcp->file_size;
560                         wcp->data_size = pos - wcp->file_size + 1;
561                         memset(wcp->data, '\0', wcp->data_size);
562                         memcpy(wcp->data + wcp->data_size-1, data, 1);
563
564                         /*
565                          * Update the file size if changed.
566                          */
567
568                         if (wcp->offset + wcp->data_size > wcp->file_size) {
569                                 if (wcp_file_size_change(fsp) == -1) {
570                                         return -1;
571                                 }
572                         }
573
574                         return n;
575
576                 } else {
577
578                         /* ASCII art..... JRA.
579
580    Case 1).
581
582                         +---------------+---------------+
583                         | Cached data   | Cache buffer  |
584                         +---------------+---------------+
585
586                                                               +-------------------+
587                                                               | Data to write     |
588                                                               +-------------------+
589
590    Case 2).
591
592                            +---------------+---------------+
593                            | Cached data   | Cache buffer  |
594                            +---------------+---------------+
595
596    +-------------------+
597    | Data to write     |
598    +-------------------+
599
600     Case 3).
601
602                            +---------------+---------------+
603                            | Cached data   | Cache buffer  |
604                            +---------------+---------------+
605
606                   +-----------------------------------------------------+
607                   | Data to write                                       |
608                   +-----------------------------------------------------+
609
610                   */
611
612                         /*
613                          * Write is bigger than buffer, or there is no overlap on the
614                          * low or high ends.
615                          */
616
617                         DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
618 len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
619
620                         /*
621                          * If write would fit in the cache, and is larger than
622                          * the data already in the cache, flush the cache and
623                          * preferentially copy the data new data into it. Otherwise
624                          * just write the data directly.
625                          */
626
627                         if ( n <= wcp->alloc_size && n > wcp->data_size) {
628                                 cache_flush_needed = True;
629                         } else {
630                                 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
631
632                                 /*
633                                  * If the write overlaps the entire cache, then
634                                  * discard the current contents of the cache.
635                                  * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
636                                  */
637
638                                 if ((pos <= wcp->offset) &&
639                                                 (pos + n >= wcp->offset + wcp->data_size) ) {
640                                         DEBUG(9,("write_file: discarding overwritten write \
641 cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
642                                         wcp->data_size = 0;
643                                 }
644
645                                 DO_PROFILE_INC(writecache_direct_writes);
646                                 if (ret == -1) {
647                                         return ret;
648                                 }
649
650                                 if (pos + ret > wcp->file_size) {
651                                         wcp->file_size = pos + ret;
652                                 }
653
654                                 return ret;
655                         }
656
657                         write_path = 4;
658
659                 }
660
661                 if (cache_flush_needed) {
662                         DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
663 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
664                                 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
665                                 (double)wcp->offset, (unsigned int)wcp->data_size ));
666
667                         flush_write_cache(fsp, WRITE_FLUSH);
668                 }
669         }
670
671         /*
672          * If the write request is bigger than the cache
673          * size, write it all out.
674          */
675
676         if (n > wcp->alloc_size ) {
677                 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
678                 if (ret == -1) {
679                         return -1;
680                 }
681
682                 if (pos + ret > wcp->file_size) {
683                         wcp->file_size = pos + n;
684                 }
685
686                 DO_PROFILE_INC(writecache_direct_writes);
687                 return total_written + n;
688         }
689
690         /*
691          * If there's any data left, cache it.
692          */
693
694         if (n) {
695 #ifdef WITH_PROFILE
696                 if (wcp->data_size) {
697                         DO_PROFILE_INC(writecache_abutted_writes);
698                 } else {
699                         DO_PROFILE_INC(writecache_init_writes);
700                 }
701 #endif
702                 memcpy(wcp->data+wcp->data_size, data, n);
703                 if (wcp->data_size == 0) {
704                         wcp->offset = pos;
705                         DO_PROFILE_INC(writecache_num_write_caches);
706                 }
707                 wcp->data_size += n;
708
709                 /*
710                  * Update the file size if changed.
711                  */
712
713                 if (wcp->offset + wcp->data_size > wcp->file_size) {
714                         if (wcp_file_size_change(fsp) == -1) {
715                                 return -1;
716                         }
717                 }
718                 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
719                         (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
720
721                 total_written += n;
722                 return total_written; /* .... that's a write :) */
723         }
724   
725         return total_written;
726 }
727
728 /****************************************************************************
729  Delete the write cache structure.
730 ****************************************************************************/
731
732 void delete_write_cache(files_struct *fsp)
733 {
734         write_cache *wcp;
735
736         if(!fsp) {
737                 return;
738         }
739
740         if(!(wcp = fsp->wcp)) {
741                 return;
742         }
743
744         DO_PROFILE_DEC(writecache_allocated_write_caches);
745         allocated_write_caches--;
746
747         SMB_ASSERT(wcp->data_size == 0);
748
749         SAFE_FREE(wcp->data);
750         SAFE_FREE(fsp->wcp);
751
752         DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
753 }
754
755 /****************************************************************************
756  Setup the write cache structure.
757 ****************************************************************************/
758
759 static bool setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
760 {
761         ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
762         write_cache *wcp;
763
764         if (allocated_write_caches >= MAX_WRITE_CACHES) {
765                 return False;
766         }
767
768         if(alloc_size == 0 || fsp->wcp) {
769                 return False;
770         }
771
772         if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
773                 DEBUG(0,("setup_write_cache: malloc fail.\n"));
774                 return False;
775         }
776
777         wcp->file_size = file_size;
778         wcp->offset = 0;
779         wcp->alloc_size = alloc_size;
780         wcp->data_size = 0;
781         if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
782                 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
783                         (unsigned int)wcp->alloc_size ));
784                 SAFE_FREE(wcp);
785                 return False;
786         }
787
788         memset(wcp->data, '\0', wcp->alloc_size );
789
790         fsp->wcp = wcp;
791         DO_PROFILE_INC(writecache_allocated_write_caches);
792         allocated_write_caches++;
793
794         DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
795                 fsp->fsp_name, (unsigned long)wcp->alloc_size ));
796
797         return True;
798 }
799
800 /****************************************************************************
801  Cope with a size change.
802 ****************************************************************************/
803
804 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
805 {
806         if(fsp->wcp) {
807                 /* The cache *must* have been flushed before we do this. */
808                 if (fsp->wcp->data_size != 0) {
809                         char *msg;
810                         asprintf(&msg, "set_filelen_write_cache: size change "
811                                  "on file %s with write cache size = %lu\n",
812                                  fsp->fsp_name,
813                                  (unsigned long)fsp->wcp->data_size);
814                         smb_panic(msg);
815                 }
816                 fsp->wcp->file_size = file_size;
817         }
818 }
819
820 /*******************************************************************
821  Flush a write cache struct to disk.
822 ********************************************************************/
823
824 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
825 {
826         write_cache *wcp = fsp->wcp;
827         size_t data_size;
828         ssize_t ret;
829
830         if(!wcp || !wcp->data_size) {
831                 return 0;
832         }
833
834         data_size = wcp->data_size;
835         wcp->data_size = 0;
836
837         DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
838
839         DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
840                 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
841
842 #ifdef WITH_PROFILE
843         if(data_size == wcp->alloc_size) {
844                 DO_PROFILE_INC(writecache_num_perfect_writes);
845         }
846 #endif
847
848         ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
849
850         /*
851          * Ensure file size if kept up to date if write extends file.
852          */
853
854         if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
855                 wcp->file_size = wcp->offset + ret;
856         }
857
858         return ret;
859 }
860
861 /*******************************************************************
862 sync a file
863 ********************************************************************/
864
865 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
866 {
867         if (fsp->fh->fd == -1)
868                 return NT_STATUS_INVALID_HANDLE;
869
870         if (lp_strict_sync(SNUM(conn)) &&
871             (lp_syncalways(SNUM(conn)) || write_through)) {
872                 int ret = flush_write_cache(fsp, SYNC_FLUSH);
873                 if (ret == -1) {
874                         return map_nt_error_from_unix(errno);
875                 }
876                 ret = SMB_VFS_FSYNC(fsp,fsp->fh->fd);
877                 if (ret == -1) {
878                         return map_nt_error_from_unix(errno);
879                 }
880         }
881         return NT_STATUS_OK;
882 }
883
884 /************************************************************
885  Perform a stat whether a valid fd or not.
886 ************************************************************/
887
888 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
889 {
890         if (fsp->fh->fd == -1) {
891                 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name, pst);
892         } else {
893                 return SMB_VFS_FSTAT(fsp,fsp->fh->fd, pst);
894         }
895 }