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