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