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