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