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