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