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