s3: vfs: Use the new smb_vfs_fsync_sync() call in place of SMB_VFS_FSYNC().
[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 #include "printing.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "smbprofile.h"
27
28 struct write_cache {
29         off_t file_size;
30         off_t offset;
31         size_t alloc_size;
32         size_t data_size;
33         char *data;
34 };
35
36 static bool setup_write_cache(files_struct *, off_t);
37
38 /****************************************************************************
39  Read from write cache if we can.
40 ****************************************************************************/
41
42 static bool read_from_write_cache(files_struct *fsp,char *data,off_t pos,size_t n)
43 {
44         struct write_cache *wcp = fsp->wcp;
45
46         if(!wcp) {
47                 return False;
48         }
49
50         if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
51                 return False;
52         }
53
54         memcpy(data, wcp->data + (pos - wcp->offset), n);
55
56         DO_PROFILE_INC(writecache_cached_reads);
57
58         return True;
59 }
60
61 /****************************************************************************
62  Read from a file.
63 ****************************************************************************/
64
65 ssize_t read_file(files_struct *fsp,char *data,off_t pos,size_t n)
66 {
67         ssize_t ret = 0;
68
69         /* you can't read from print files */
70         if (fsp->print_file) {
71                 errno = EBADF;
72                 return -1;
73         }
74
75         /*
76          * Serve from write cache if we can.
77          */
78
79         if(read_from_write_cache(fsp, data, pos, n)) {
80                 fsp->fh->pos = pos + n;
81                 fsp->fh->position_information = fsp->fh->pos;
82                 return n;
83         }
84
85         flush_write_cache(fsp, SAMBA_READ_FLUSH);
86
87         fsp->fh->pos = pos;
88
89         if (n > 0) {
90                 ret = SMB_VFS_PREAD(fsp,data,n,pos);
91
92                 if (ret == -1) {
93                         return -1;
94                 }
95         }
96
97         DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
98                   fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
99
100         fsp->fh->pos += ret;
101         fsp->fh->position_information = fsp->fh->pos;
102
103         return(ret);
104 }
105
106 /****************************************************************************
107  *Really* write to a file.
108 ****************************************************************************/
109
110 static ssize_t real_write_file(struct smb_request *req,
111                                 files_struct *fsp,
112                                 const char *data,
113                                 off_t pos,
114                                 size_t n)
115 {
116         ssize_t ret;
117
118         if (pos == -1) {
119                 ret = vfs_write_data(req, fsp, data, n);
120         } else {
121                 fsp->fh->pos = pos;
122                 if (pos && lp_strict_allocate(SNUM(fsp->conn) &&
123                                 !fsp->is_sparse)) {
124                         if (vfs_fill_sparse(fsp, pos) == -1) {
125                                 return -1;
126                         }
127                 }
128                 ret = vfs_pwrite_data(req, fsp, data, n, pos);
129         }
130
131         DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
132                   fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
133
134         if (ret != -1) {
135                 fsp->fh->pos += ret;
136
137 /* Yes - this is correct - writes don't update this. JRA. */
138 /* Found by Samba4 tests. */
139 #if 0
140                 fsp->position_information = fsp->pos;
141 #endif
142         }
143
144         return ret;
145 }
146
147 /****************************************************************************
148  File size cache change.
149  Updates size on disk but doesn't flush the cache.
150 ****************************************************************************/
151
152 static int wcp_file_size_change(files_struct *fsp)
153 {
154         int ret;
155         struct write_cache *wcp = fsp->wcp;
156
157         wcp->file_size = wcp->offset + wcp->data_size;
158         ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
159         if (ret == -1) {
160                 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
161                          "error %s\n", fsp_str_dbg(fsp),
162                          (double)wcp->file_size, strerror(errno)));
163         }
164         return ret;
165 }
166
167 void update_write_time_handler(struct tevent_context *ctx,
168                                       struct tevent_timer *te,
169                                       struct timeval now,
170                                       void *private_data)
171 {
172         files_struct *fsp = (files_struct *)private_data;
173
174         DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp)));
175
176         /* change the write time in the open file db. */
177         (void)set_write_time(fsp->file_id, timespec_current());
178
179         /* And notify. */
180         notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
181                      FILE_NOTIFY_CHANGE_LAST_WRITE, fsp->fsp_name->base_name);
182
183         /* Remove the timed event handler. */
184         TALLOC_FREE(fsp->update_write_time_event);
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_flags & FSP_POSIX_FLAGS_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         /* We need to remember someone did a write
209          * and update to current time on close. */
210
211         fsp->update_write_time_on_close = true;
212
213         if (fsp->update_write_time_triggered) {
214                 /*
215                  * We only update the write time after 2 seconds
216                  * on the first normal write. After that
217                  * no other writes affect this until close.
218                  */
219                 return;
220         }
221         fsp->update_write_time_triggered = true;
222
223         delay = lp_parm_int(SNUM(fsp->conn),
224                             "smbd", "writetimeupdatedelay",
225                             WRITE_TIME_UPDATE_USEC_DELAY);
226
227         DEBUG(5, ("Update write time %d usec later on %s\n",
228                   delay, fsp_str_dbg(fsp)));
229
230         /* trigger the update 2 seconds later */
231         fsp->update_write_time_event =
232                 tevent_add_timer(fsp->conn->sconn->ev_ctx, NULL,
233                                  timeval_current_ofs_usec(delay),
234                                  update_write_time_handler, fsp);
235 }
236
237 void trigger_write_time_update_immediate(struct files_struct *fsp)
238 {
239         struct smb_file_time ft;
240
241         if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
242                 /* Don't use delayed writes on POSIX files. */
243                 return;
244         }
245
246         if (fsp->write_time_forced) {
247                 /*
248                  * No point - "sticky" write times
249                  * in effect.
250                  */
251                 return;
252         }
253
254         TALLOC_FREE(fsp->update_write_time_event);
255         DEBUG(5, ("Update write time immediate on %s\n",
256                   fsp_str_dbg(fsp)));
257
258         /* After an immediate update, reset the trigger. */
259         fsp->update_write_time_triggered = true;
260         fsp->update_write_time_on_close = false;
261
262         ZERO_STRUCT(ft);
263         ft.mtime = timespec_current();
264
265         /* Update the time in the open file db. */
266         (void)set_write_time(fsp->file_id, ft.mtime);
267
268         /* Now set on disk - takes care of notify. */
269         (void)smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
270 }
271
272 void mark_file_modified(files_struct *fsp)
273 {
274         int dosmode;
275
276         if (fsp->modified) {
277                 return;
278         }
279
280         fsp->modified = true;
281
282         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
283                 return;
284         }
285         trigger_write_time_update(fsp);
286
287         if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
288                 return;
289         }
290         if (!(lp_store_dos_attributes(SNUM(fsp->conn)) ||
291               MAP_ARCHIVE(fsp->conn))) {
292                 return;
293         }
294
295         dosmode = dos_mode(fsp->conn, fsp->fsp_name);
296         if (IS_DOS_ARCHIVE(dosmode)) {
297                 return;
298         }
299         file_set_dosmode(fsp->conn, fsp->fsp_name,
300                          dosmode | FILE_ATTRIBUTE_ARCHIVE, NULL, false);
301 }
302
303 /****************************************************************************
304  Write to a file.
305 ****************************************************************************/
306
307 ssize_t write_file(struct smb_request *req,
308                         files_struct *fsp,
309                         const char *data,
310                         off_t pos,
311                         size_t n)
312 {
313         struct write_cache *wcp = fsp->wcp;
314         ssize_t total_written = 0;
315         int write_path = -1;
316
317         if (fsp->print_file) {
318                 uint32_t t;
319                 int ret;
320
321                 ret = print_spool_write(fsp, data, n, pos, &t);
322                 if (ret) {
323                         errno = ret;
324                         return -1;
325                 }
326                 return t;
327         }
328
329         if (!fsp->can_write) {
330                 errno = EPERM;
331                 return -1;
332         }
333
334         /*
335          * If this is the first write and we have an exclusive oplock
336          * then setup the write cache.
337          */
338
339         if (!fsp->modified &&
340             EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) &&
341             (wcp == NULL)) {
342                 /*
343                  * Note: no write cache with leases!
344                  * as the handles would have to share the write cache
345                  * that's possible but an improvement for another day...
346                  */
347                 setup_write_cache(fsp, fsp->fsp_name->st.st_ex_size);
348                 wcp = fsp->wcp;
349         }
350
351         mark_file_modified(fsp);
352
353         DO_PROFILE_INC(writecache_total_writes);
354         if (!fsp->oplock_type) {
355                 DO_PROFILE_INC(writecache_non_oplock_writes);
356         }
357
358         /*
359          * If this file is level II oplocked then we need
360          * to grab the shared memory lock and inform all
361          * other files with a level II lock that they need
362          * to flush their read caches. We keep the lock over
363          * the shared memory area whilst doing this.
364          */
365
366         /* This should actually be improved to span the write. */
367         contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
368         contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
369
370         if (wcp && req->unread_bytes) {
371                 /* If we're using receivefile don't
372                  * deal with a write cache.
373                  */
374                 flush_write_cache(fsp, SAMBA_WRITE_FLUSH);
375                 delete_write_cache(fsp);
376                 wcp = NULL;
377         }
378
379         if(!wcp) {
380                 DO_PROFILE_INC(writecache_direct_writes);
381                 total_written = real_write_file(req, fsp, data, pos, n);
382                 return total_written;
383         }
384
385         DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
386                  "wcp->data_size=%u\n", fsp_str_dbg(fsp), fsp->fh->fd,
387                  (double)pos, (unsigned int)n, (double)wcp->offset,
388                  (unsigned int)wcp->data_size));
389
390         fsp->fh->pos = pos + n;
391
392         if ((n == 1) && (data[0] == '\0') && (pos > wcp->file_size)) {
393                 int ret;
394
395                 /*
396                  * This is a 1-byte write of a 0 beyond the EOF and
397                  * thus implicitly also beyond the current active
398                  * write cache, the typical file-extending (and
399                  * allocating, but we're using the write cache here)
400                  * write done by Windows. We just have to ftruncate
401                  * the file and rely on posix semantics to return
402                  * zeros for non-written file data that is within the
403                  * file length.
404                  *
405                  * We can not use wcp_file_size_change here because we
406                  * might have an existing write cache, and
407                  * wcp_file_size_change assumes a change to just the
408                  * end of the current write cache.
409                  */
410
411                 wcp->file_size = pos + 1;
412                 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
413                 if (ret == -1) {
414                         DEBUG(0, ("wcp_file_size_change (%s): ftruncate of "
415                                   "size %.0f error %s\n", fsp_str_dbg(fsp),
416                                   (double)wcp->file_size, strerror(errno)));
417                         return -1;
418                 }
419                 return 1;
420         }
421
422
423         /*
424          * If we have active cache and it isn't contiguous then we flush.
425          * NOTE: There is a small problem with running out of disk ....
426          */
427
428         if (wcp->data_size) {
429                 bool cache_flush_needed = False;
430
431                 if ((pos >= wcp->offset) &&
432                     (pos <= wcp->offset + wcp->data_size)) {
433
434                         /* ASCII art.... JRA.
435
436       +--------------+-----
437       | Cached data  | Rest of allocated cache buffer....
438       +--------------+-----
439
440             +-------------------+
441             | Data to write     |
442             +-------------------+
443
444                         */
445
446                         /*
447                          * Start of write overlaps or abutts the existing data.
448                          */
449
450                         size_t data_used;
451
452                         data_used = MIN((wcp->alloc_size - (pos - wcp->offset)),
453                                         n);
454
455                         memcpy(wcp->data + (pos - wcp->offset), data,
456                                data_used);
457
458                         /*
459                          * Update the current buffer size with the new data.
460                          */
461
462                         if(pos + data_used > wcp->offset + wcp->data_size) {
463                                 wcp->data_size = pos + data_used - wcp->offset;
464                         }
465
466                         /*
467                          * Update the file size if changed.
468                          */
469
470                         if (wcp->offset + wcp->data_size > wcp->file_size) {
471                                 if (wcp_file_size_change(fsp) == -1) {
472                                         return -1;
473                                 }
474                         }
475
476                         /*
477                          * If we used all the data then
478                          * return here.
479                          */
480
481                         if(n == data_used) {
482                                 return n;
483                         } else {
484                                 cache_flush_needed = True;
485                         }
486                         /*
487                          * Move the start of data forward by the amount used,
488                          * cut down the amount left by the same amount.
489                          */
490
491                         data += data_used;
492                         pos += data_used;
493                         n -= data_used;
494
495                         DO_PROFILE_INC(writecache_abutted_writes);
496                         total_written = data_used;
497
498                         write_path = 1;
499
500                 } else if ((pos < wcp->offset) &&
501                            (pos + n > wcp->offset) &&
502                            (pos + n <= wcp->offset + wcp->alloc_size)) {
503
504                         /* ASCII art.... JRA.
505
506                         +---------------+
507                         | Cache buffer  |
508                         +---------------+
509
510             +-------------------+
511             | Data to write     |
512             +-------------------+
513
514                         */
515
516                         /*
517                          * End of write overlaps the existing data.
518                          */
519
520                         size_t data_used = pos + n - wcp->offset;
521
522                         memcpy(wcp->data, data + n - data_used, data_used);
523
524                         /*
525                          * Update the current buffer size with the new data.
526                          */
527
528                         if(pos + n > wcp->offset + wcp->data_size) {
529                                 wcp->data_size = pos + n - wcp->offset;
530                         }
531
532                         /*
533                          * Update the file size if changed.
534                          */
535
536                         if (wcp->offset + wcp->data_size > wcp->file_size) {
537                                 if (wcp_file_size_change(fsp) == -1) {
538                                         return -1;
539                                 }
540                         }
541
542                         /*
543                          * We don't need to move the start of data, but we
544                          * cut down the amount left by the amount used.
545                          */
546
547                         n -= data_used;
548
549                         /*
550                          * We cannot have used all the data here.
551                          */
552
553                         cache_flush_needed = True;
554
555                         DO_PROFILE_INC(writecache_abutted_writes);
556                         total_written = data_used;
557
558                         write_path = 2;
559
560                 } else if ((pos >= wcp->file_size) &&
561                            (wcp->offset + wcp->data_size == wcp->file_size) &&
562                            (pos > wcp->offset + wcp->data_size) &&
563                            (pos < wcp->offset + wcp->alloc_size) ) {
564
565                         /* ASCII art.... JRA.
566
567                        End of file ---->|
568
569                         +---------------+---------------+
570                         | Cached data   | Cache buffer  |
571                         +---------------+---------------+
572
573                                               +-------------------+
574                                               | Data to write     |
575                                               +-------------------+
576
577                         */
578
579                         /*
580                          * Non-contiguous write part of which fits within
581                          * the cache buffer and is extending the file
582                          * and the cache contents reflect the current
583                          * data up to the current end of the file.
584                          */
585
586                         size_t data_used;
587
588                         if(pos + n <= wcp->offset + wcp->alloc_size) {
589                                 data_used = n;
590                         } else {
591                                 data_used = wcp->offset+wcp->alloc_size-pos;
592                         }
593
594                         /*
595                          * Fill in the non-continuous area with zeros.
596                          */
597
598                         memset(wcp->data + wcp->data_size, '\0',
599                                 pos - (wcp->offset + wcp->data_size) );
600
601                         memcpy(wcp->data + (pos - wcp->offset), data,
602                                data_used);
603
604                         /*
605                          * Update the current buffer size with the new data.
606                          */
607
608                         if(pos + data_used > wcp->offset + wcp->data_size) {
609                                 wcp->data_size = pos + data_used - wcp->offset;
610                         }
611
612                         /*
613                          * Update the file size if changed.
614                          */
615
616                         if (wcp->offset + wcp->data_size > wcp->file_size) {
617                                 if (wcp_file_size_change(fsp) == -1) {
618                                         return -1;
619                                 }
620                         }
621
622                         /*
623                          * If we used all the data then
624                          * return here.
625                          */
626
627                         if(n == data_used) {
628                                 return n;
629                         } else {
630                                 cache_flush_needed = True;
631                         }
632
633                         /*
634                          * Move the start of data forward by the amount used,
635                          * cut down the amount left by the same amount.
636                          */
637
638                         data += data_used;
639                         pos += data_used;
640                         n -= data_used;
641
642                         DO_PROFILE_INC(writecache_abutted_writes);
643                         total_written = data_used;
644
645                         write_path = 3;
646
647                 } else if ( (pos >= wcp->file_size) &&
648                             (n == 1) &&
649                             (wcp->file_size == wcp->offset + wcp->data_size) &&
650                             (pos < wcp->file_size + wcp->alloc_size)) {
651
652                         /*
653
654                 End of file ---->|
655
656                  +---------------+---------------+
657                  | Cached data   | Cache buffer  |
658                  +---------------+---------------+
659
660                                  |<------- allocated size ---------------->|
661
662                                                          +--------+
663                                                          | 1 Byte |
664                                                          +--------+
665
666                         MS-Office seems to do this a lot to determine if
667                         there's enough space on the filesystem to write a new
668                         file.
669
670                         Change to :
671
672                 End of file ---->|
673                                  +-----------------------+--------+
674                                  | Zeroed Cached data    | 1 Byte |
675                                  +-----------------------+--------+
676                         */
677
678                         flush_write_cache(fsp, SAMBA_WRITE_FLUSH);
679                         wcp->offset = wcp->file_size;
680                         wcp->data_size = pos - wcp->file_size + 1;
681                         memset(wcp->data, '\0', wcp->data_size);
682                         memcpy(wcp->data + wcp->data_size-1, data, 1);
683
684                         /*
685                          * Update the file size if changed.
686                          */
687
688                         if (wcp->offset + wcp->data_size > wcp->file_size) {
689                                 if (wcp_file_size_change(fsp) == -1) {
690                                         return -1;
691                                 }
692                         }
693
694                         return n;
695
696                 } else {
697
698                         /* ASCII art..... JRA.
699
700    Case 1).
701
702                         +---------------+---------------+
703                         | Cached data   | Cache buffer  |
704                         +---------------+---------------+
705
706                                                               +---------------+
707                                                               | Data to write |
708                                                               +---------------+
709
710    Case 2).
711
712                            +---------------+---------------+
713                            | Cached data   | Cache buffer  |
714                            +---------------+---------------+
715
716    +-------------------+
717    | Data to write     |
718    +-------------------+
719
720     Case 3).
721
722                            +---------------+---------------+
723                            | Cached data   | Cache buffer  |
724                            +---------------+---------------+
725
726                   +-----------------------------------------------------+
727                   | Data to write                                       |
728                   +-----------------------------------------------------+
729
730                   */
731
732                         /*
733                          * Write is bigger than buffer, or there is no
734                          * overlap on the low or high ends.
735                          */
736
737                         DEBUG(9,("write_file: non cacheable write : fd = %d, "
738                                  "pos = %.0f, len = %u, "
739                                  "current cache pos = %.0f len = %u\n",
740                                  fsp->fh->fd, (double)pos, (unsigned int)n,
741                                  (double)wcp->offset,
742                                  (unsigned int)wcp->data_size ));
743
744                         /*
745                          * If write would fit in the cache, and is
746                          * larger than the data already in the cache,
747                          * flush the cache and preferentially copy the
748                          * data new data into it. Otherwise just write
749                          * the data directly.
750                          */
751
752                         if ( n <= wcp->alloc_size && n > wcp->data_size) {
753                                 cache_flush_needed = True;
754                         } else {
755                                 ssize_t ret = real_write_file(NULL, fsp, data,
756                                                               pos, n);
757
758                                 /*
759                                  * If the write overlaps the entire
760                                  * cache, then discard the current
761                                  * contents of the cache.  Fix from
762                                  * Rasmus Borup Hansen rbh@math.ku.dk.
763                                  */
764
765                                 if ((pos <= wcp->offset) &&
766                                     (pos + n >= wcp->offset+wcp->data_size)) {
767                                         DEBUG(9,("write_file: discarding "
768                                                  "overwritten write cache: "
769                                                  "fd = %d, off=%.0f, "
770                                                  "size=%u\n", fsp->fh->fd,
771                                                  (double)wcp->offset,
772                                                  (unsigned)wcp->data_size));
773                                         wcp->data_size = 0;
774                                 }
775
776                                 DO_PROFILE_INC(writecache_direct_writes);
777                                 if (ret == -1) {
778                                         return ret;
779                                 }
780
781                                 if (pos + ret > wcp->file_size) {
782                                         wcp->file_size = pos + ret;
783                                 }
784
785                                 return ret;
786                         }
787
788                         write_path = 4;
789
790                 }
791
792                 if (cache_flush_needed) {
793                         DEBUG(3, ("SAMBA_WRITE_FLUSH:%d: due to noncontinuous "
794                                   "write: fd = %d, size = %.0f, pos = %.0f, "
795                                   "n = %u, wcp->offset=%.0f, "
796                                   "wcp->data_size=%u\n",
797                                   write_path, fsp->fh->fd,
798                                   (double)wcp->file_size, (double)pos,
799                                   (unsigned int)n, (double)wcp->offset,
800                                   (unsigned int)wcp->data_size ));
801
802                         flush_write_cache(fsp, SAMBA_WRITE_FLUSH);
803                 }
804         }
805
806         /*
807          * If the write request is bigger than the cache
808          * size, write it all out.
809          */
810
811         if (n > wcp->alloc_size ) {
812                 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
813                 if (ret == -1) {
814                         return -1;
815                 }
816
817                 if (pos + ret > wcp->file_size) {
818                         wcp->file_size = pos + n;
819                 }
820
821                 DO_PROFILE_INC(writecache_direct_writes);
822                 return total_written + n;
823         }
824
825         /*
826          * If there's any data left, cache it.
827          */
828
829         if (n) {
830                 DO_PROFILE_INC(writecache_cached_writes);
831                 if (wcp->data_size) {
832                         DO_PROFILE_INC(writecache_abutted_writes);
833                 } else {
834                         DO_PROFILE_INC(writecache_init_writes);
835                 }
836
837                 if ((wcp->data_size == 0)
838                     && (pos > wcp->file_size)
839                     && (pos + n <= wcp->file_size + wcp->alloc_size)) {
840                         /*
841                          * This is a write completely beyond the
842                          * current EOF, but within reach of the write
843                          * cache. We expect fill-up writes pretty
844                          * soon, so it does not make sense to start
845                          * the write cache at the current
846                          * offset. These fill-up writes would trigger
847                          * separate pwrites or even unnecessary cache
848                          * flushes because they overlap if this is a
849                          * one-byte allocating write.
850                          */
851                         wcp->offset = wcp->file_size;
852                         wcp->data_size = pos - wcp->file_size;
853                         memset(wcp->data, 0, wcp->data_size);
854                 }
855
856                 memcpy(wcp->data+wcp->data_size, data, n);
857                 if (wcp->data_size == 0) {
858                         wcp->offset = pos;
859                 }
860                 wcp->data_size += n;
861
862                 /*
863                  * Update the file size if changed.
864                  */
865
866                 if (wcp->offset + wcp->data_size > wcp->file_size) {
867                         if (wcp_file_size_change(fsp) == -1) {
868                                 return -1;
869                         }
870                 }
871                 DEBUG(9, ("wcp->offset = %.0f wcp->data_size = %u cache "
872                           "return %u\n",
873                           (double)wcp->offset, (unsigned int)wcp->data_size,
874                           (unsigned int)n));
875
876                 total_written += n;
877                 return total_written; /* .... that's a write :) */
878         }
879
880         return total_written;
881 }
882
883 /****************************************************************************
884  Delete the write cache structure.
885 ****************************************************************************/
886
887 void delete_write_cache(files_struct *fsp)
888 {
889         struct write_cache *wcp;
890
891         if(!fsp) {
892                 return;
893         }
894
895         if(!(wcp = fsp->wcp)) {
896                 return;
897         }
898
899         DO_PROFILE_INC(writecache_deallocations);
900         allocated_write_caches--;
901
902         SMB_ASSERT(wcp->data_size == 0);
903
904         SAFE_FREE(wcp->data);
905         SAFE_FREE(fsp->wcp);
906
907         DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
908                   fsp_str_dbg(fsp)));
909 }
910
911 /****************************************************************************
912  Setup the write cache structure.
913 ****************************************************************************/
914
915 static bool setup_write_cache(files_struct *fsp, off_t file_size)
916 {
917         ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
918         struct write_cache *wcp;
919
920         if (allocated_write_caches >= MAX_WRITE_CACHES) {
921                 return False;
922         }
923
924         if(alloc_size == 0 || fsp->wcp) {
925                 return False;
926         }
927
928         if((wcp = SMB_MALLOC_P(struct write_cache)) == NULL) {
929                 DEBUG(0,("setup_write_cache: malloc fail.\n"));
930                 return False;
931         }
932
933         wcp->file_size = file_size;
934         wcp->offset = 0;
935         wcp->alloc_size = alloc_size;
936         wcp->data_size = 0;
937         if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
938                 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
939                         (unsigned int)wcp->alloc_size ));
940                 SAFE_FREE(wcp);
941                 return False;
942         }
943
944         memset(wcp->data, '\0', wcp->alloc_size );
945
946         fsp->wcp = wcp;
947         DO_PROFILE_INC(writecache_allocations);
948         allocated_write_caches++;
949
950         DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
951                   fsp_str_dbg(fsp), (unsigned long)wcp->alloc_size));
952
953         return True;
954 }
955
956 /****************************************************************************
957  Cope with a size change.
958 ****************************************************************************/
959
960 void set_filelen_write_cache(files_struct *fsp, off_t file_size)
961 {
962         if(fsp->wcp) {
963                 /* The cache *must* have been flushed before we do this. */
964                 if (fsp->wcp->data_size != 0) {
965                         char *msg;
966                         if (asprintf(&msg, "set_filelen_write_cache: size change "
967                                  "on file %s with write cache size = %lu\n",
968                                  fsp->fsp_name->base_name,
969                                  (unsigned long)fsp->wcp->data_size) != -1) {
970                                 smb_panic(msg);
971                         } else {
972                                 smb_panic("set_filelen_write_cache");
973                         }
974                 }
975                 fsp->wcp->file_size = file_size;
976         }
977 }
978
979 /*******************************************************************
980  Flush a write cache struct to disk.
981 ********************************************************************/
982
983 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
984 {
985         struct write_cache *wcp = fsp->wcp;
986         size_t data_size;
987         ssize_t ret;
988
989         if(!wcp || !wcp->data_size) {
990                 return 0;
991         }
992
993         data_size = wcp->data_size;
994         wcp->data_size = 0;
995
996         switch (reason) {
997         case SAMBA_SEEK_FLUSH:
998                 DO_PROFILE_INC(writecache_flush_reason_seek);
999                 break;
1000         case SAMBA_READ_FLUSH:
1001                 DO_PROFILE_INC(writecache_flush_reason_read);
1002                 break;
1003         case SAMBA_WRITE_FLUSH:
1004                 DO_PROFILE_INC(writecache_flush_reason_write);;
1005                 break;
1006         case SAMBA_READRAW_FLUSH:
1007                 DO_PROFILE_INC(writecache_flush_reason_readraw);
1008                 break;
1009         case SAMBA_OPLOCK_RELEASE_FLUSH:
1010                 DO_PROFILE_INC(writecache_flush_reason_oplock);
1011                 break;
1012         case SAMBA_CLOSE_FLUSH:
1013                 DO_PROFILE_INC(writecache_flush_reason_close);
1014                 break;
1015         case SAMBA_SYNC_FLUSH:
1016                 DO_PROFILE_INC(writecache_flush_reason_sync);
1017                 break;
1018         case SAMBA_SIZECHANGE_FLUSH:
1019                 DO_PROFILE_INC(writecache_flush_reason_sizechange);
1020                 break;
1021         default:
1022                 break;
1023         }
1024
1025         DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
1026                 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
1027
1028         if(data_size == wcp->alloc_size) {
1029                 DO_PROFILE_INC(writecache_perfect_writes);
1030         }
1031
1032         ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
1033
1034         /*
1035          * Ensure file size if kept up to date if write extends file.
1036          */
1037
1038         if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
1039                 wcp->file_size = wcp->offset + ret;
1040         }
1041
1042         return ret;
1043 }
1044
1045 /*******************************************************************
1046 sync a file
1047 ********************************************************************/
1048
1049 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
1050 {
1051         if (fsp->fh->fd == -1)
1052                 return NT_STATUS_INVALID_HANDLE;
1053
1054         if (lp_strict_sync(SNUM(conn)) &&
1055             (lp_sync_always(SNUM(conn)) || write_through)) {
1056                 int ret = flush_write_cache(fsp, SAMBA_SYNC_FLUSH);
1057                 if (ret == -1) {
1058                         return map_nt_error_from_unix(errno);
1059                 }
1060                 ret = smb_vfs_fsync_sync(fsp);
1061                 if (ret == -1) {
1062                         return map_nt_error_from_unix(errno);
1063                 }
1064         }
1065         return NT_STATUS_OK;
1066 }
1067
1068 /************************************************************
1069  Perform a stat whether a valid fd or not.
1070 ************************************************************/
1071
1072 int fsp_stat(files_struct *fsp)
1073 {
1074         if (fsp->fh->fd == -1) {
1075                 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1076                         return SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1077                 } else {
1078                         return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1079                 }
1080         } else {
1081                 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
1082         }
1083 }