2 Unix SMB/Netbios implementation.
4 read/write to a files_struct
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Jeremy Allison 2000-2002. - write cache.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "smbprofile.h"
28 static bool setup_write_cache(files_struct
*, SMB_OFF_T
);
30 /****************************************************************************
31 Read from write cache if we can.
32 ****************************************************************************/
34 static bool read_from_write_cache(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
36 write_cache
*wcp
= fsp
->wcp
;
42 if( n
> wcp
->data_size
|| pos
< wcp
->offset
|| pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
46 memcpy(data
, wcp
->data
+ (pos
- wcp
->offset
), n
);
48 DO_PROFILE_INC(writecache_read_hits
);
53 /****************************************************************************
55 ****************************************************************************/
57 ssize_t
read_file(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
59 ssize_t ret
=0,readret
;
61 /* you can't read from print files */
62 if (fsp
->print_file
) {
68 * Serve from write cache if we can.
71 if(read_from_write_cache(fsp
, data
, pos
, n
)) {
72 fsp
->fh
->pos
= pos
+ n
;
73 fsp
->fh
->position_information
= fsp
->fh
->pos
;
77 flush_write_cache(fsp
, READ_FLUSH
);
85 readret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
88 if ((errno
== EAGAIN
) && numretries
) {
89 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
96 #else /* NO DMF fix. */
97 readret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
108 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
109 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
112 fsp
->fh
->position_information
= fsp
->fh
->pos
;
117 /****************************************************************************
118 *Really* write to a file.
119 ****************************************************************************/
121 static ssize_t
real_write_file(struct smb_request
*req
,
130 ret
= vfs_write_data(req
, fsp
, data
, n
);
133 if (pos
&& lp_strict_allocate(SNUM(fsp
->conn
) &&
135 if (vfs_fill_sparse(fsp
, pos
) == -1) {
139 ret
= vfs_pwrite_data(req
, fsp
, data
, n
, pos
);
142 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
143 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
148 /* Yes - this is correct - writes don't update this. JRA. */
149 /* Found by Samba4 tests. */
151 fsp
->position_information
= fsp
->pos
;
158 /****************************************************************************
159 File size cache change.
160 Updates size on disk but doesn't flush the cache.
161 ****************************************************************************/
163 static int wcp_file_size_change(files_struct
*fsp
)
166 write_cache
*wcp
= fsp
->wcp
;
168 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
169 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
171 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
172 "error %s\n", fsp_str_dbg(fsp
),
173 (double)wcp
->file_size
, strerror(errno
)));
178 void update_write_time_handler(struct event_context
*ctx
,
179 struct timed_event
*te
,
183 files_struct
*fsp
= (files_struct
*)private_data
;
185 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp
)));
187 /* change the write time in the open file db. */
188 (void)set_write_time(fsp
->file_id
, timespec_current());
191 notify_fname(fsp
->conn
, NOTIFY_ACTION_MODIFIED
,
192 FILE_NOTIFY_CHANGE_LAST_WRITE
, fsp
->fsp_name
->base_name
);
194 /* Remove the timed event handler. */
195 TALLOC_FREE(fsp
->update_write_time_event
);
198 /*********************************************************
199 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
201 *********************************************************/
203 void trigger_write_time_update(struct files_struct
*fsp
)
207 if (fsp
->posix_open
) {
208 /* Don't use delayed writes on POSIX files. */
212 if (fsp
->write_time_forced
) {
213 /* No point - "sticky" write times
219 /* We need to remember someone did a write
220 * and update to current time on close. */
222 fsp
->update_write_time_on_close
= true;
224 if (fsp
->update_write_time_triggered
) {
226 * We only update the write time after 2 seconds
227 * on the first normal write. After that
228 * no other writes affect this until close.
232 fsp
->update_write_time_triggered
= true;
234 delay
= lp_parm_int(SNUM(fsp
->conn
),
235 "smbd", "writetimeupdatedelay",
236 WRITE_TIME_UPDATE_USEC_DELAY
);
238 DEBUG(5, ("Update write time %d usec later on %s\n",
239 delay
, fsp_str_dbg(fsp
)));
241 /* trigger the update 2 seconds later */
242 fsp
->update_write_time_event
=
243 event_add_timed(server_event_context(), NULL
,
244 timeval_current_ofs_usec(delay
),
245 update_write_time_handler
, fsp
);
248 void trigger_write_time_update_immediate(struct files_struct
*fsp
)
250 struct smb_file_time ft
;
252 if (fsp
->posix_open
) {
253 /* Don't use delayed writes on POSIX files. */
257 if (fsp
->write_time_forced
) {
259 * No point - "sticky" write times
265 TALLOC_FREE(fsp
->update_write_time_event
);
266 DEBUG(5, ("Update write time immediate on %s\n",
269 /* After an immediate update, reset the trigger. */
270 fsp
->update_write_time_triggered
= true;
271 fsp
->update_write_time_on_close
= false;
274 ft
.mtime
= timespec_current();
276 /* Update the time in the open file db. */
277 (void)set_write_time(fsp
->file_id
, ft
.mtime
);
279 /* Now set on disk - takes care of notify. */
280 (void)smb_set_file_time(fsp
->conn
, fsp
, fsp
->fsp_name
, &ft
, false);
283 /****************************************************************************
285 ****************************************************************************/
287 ssize_t
write_file(struct smb_request
*req
,
293 write_cache
*wcp
= fsp
->wcp
;
294 ssize_t total_written
= 0;
297 if (fsp
->print_file
) {
301 ret
= print_spool_write(fsp
, data
, n
, pos
, &t
);
309 if (!fsp
->can_write
) {
314 if (!fsp
->modified
) {
315 fsp
->modified
= True
;
317 if (SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
) == 0) {
318 trigger_write_time_update(fsp
);
319 if (!fsp
->posix_open
&&
320 (lp_store_dos_attributes(SNUM(fsp
->conn
)) ||
321 MAP_ARCHIVE(fsp
->conn
))) {
322 int dosmode
= dos_mode(fsp
->conn
, fsp
->fsp_name
);
323 if (!IS_DOS_ARCHIVE(dosmode
)) {
324 file_set_dosmode(fsp
->conn
, fsp
->fsp_name
,
325 dosmode
| FILE_ATTRIBUTE_ARCHIVE
, NULL
, false);
330 * If this is the first write and we have an exclusive oplock then setup
334 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
335 setup_write_cache(fsp
,
336 fsp
->fsp_name
->st
.st_ex_size
);
343 DO_PROFILE_INC(writecache_total_writes
);
344 if (!fsp
->oplock_type
) {
345 DO_PROFILE_INC(writecache_non_oplock_writes
);
350 * If this file is level II oplocked then we need
351 * to grab the shared memory lock and inform all
352 * other files with a level II lock that they need
353 * to flush their read caches. We keep the lock over
354 * the shared memory area whilst doing this.
357 /* This should actually be improved to span the write. */
358 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
359 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
362 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
363 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
364 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
365 profile_p
->writecache_init_writes
,
366 profile_p
->writecache_abutted_writes
,
367 profile_p
->writecache_total_writes
,
368 profile_p
->writecache_non_oplock_writes
,
369 profile_p
->writecache_allocated_write_caches
,
370 profile_p
->writecache_num_write_caches
,
371 profile_p
->writecache_direct_writes
,
372 profile_p
->writecache_num_perfect_writes
,
373 profile_p
->writecache_read_hits
));
375 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
376 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
377 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
378 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
379 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
380 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
381 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
382 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
386 if (wcp
&& req
->unread_bytes
) {
387 /* If we're using receivefile don't
388 * deal with a write cache.
390 flush_write_cache(fsp
, WRITE_FLUSH
);
391 delete_write_cache(fsp
);
396 DO_PROFILE_INC(writecache_direct_writes
);
397 total_written
= real_write_file(req
, fsp
, data
, pos
, n
);
398 return total_written
;
401 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
402 "wcp->data_size=%u\n", fsp_str_dbg(fsp
), fsp
->fh
->fd
,
403 (double)pos
, (unsigned int)n
, (double)wcp
->offset
,
404 (unsigned int)wcp
->data_size
));
406 fsp
->fh
->pos
= pos
+ n
;
408 if ((n
== 1) && (data
[0] == '\0') && (pos
> wcp
->file_size
)) {
412 * This is a 1-byte write of a 0 beyond the EOF and
413 * thus implicitly also beyond the current active
414 * write cache, the typical file-extending (and
415 * allocating, but we're using the write cache here)
416 * write done by Windows. We just have to ftruncate
417 * the file and rely on posix semantics to return
418 * zeros for non-written file data that is within the
421 * We can not use wcp_file_size_change here because we
422 * might have an existing write cache, and
423 * wcp_file_size_change assumes a change to just the
424 * end of the current write cache.
427 wcp
->file_size
= pos
+ 1;
428 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
430 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f"
431 "error %s\n", fsp_str_dbg(fsp
),
432 (double)wcp
->file_size
, strerror(errno
)));
440 * If we have active cache and it isn't contiguous then we flush.
441 * NOTE: There is a small problem with running out of disk ....
444 if (wcp
->data_size
) {
445 bool cache_flush_needed
= False
;
447 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
449 /* ASCII art.... JRA.
451 +--------------+-----
452 | Cached data | Rest of allocated cache buffer....
453 +--------------+-----
455 +-------------------+
457 +-------------------+
462 * Start of write overlaps or abutts the existing data.
465 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
467 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
470 * Update the current buffer size with the new data.
473 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
474 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
478 * Update the file size if changed.
481 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
482 if (wcp_file_size_change(fsp
) == -1) {
488 * If we used all the data then
495 cache_flush_needed
= True
;
498 * Move the start of data forward by the amount used,
499 * cut down the amount left by the same amount.
506 DO_PROFILE_INC(writecache_abutted_writes
);
507 total_written
= data_used
;
511 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
512 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
514 /* ASCII art.... JRA.
520 +-------------------+
522 +-------------------+
527 * End of write overlaps the existing data.
530 size_t data_used
= pos
+ n
- wcp
->offset
;
532 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
535 * Update the current buffer size with the new data.
538 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
539 wcp
->data_size
= pos
+ n
- wcp
->offset
;
543 * Update the file size if changed.
546 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
547 if (wcp_file_size_change(fsp
) == -1) {
553 * We don't need to move the start of data, but we
554 * cut down the amount left by the amount used.
560 * We cannot have used all the data here.
563 cache_flush_needed
= True
;
565 DO_PROFILE_INC(writecache_abutted_writes
);
566 total_written
= data_used
;
570 } else if ( (pos
>= wcp
->file_size
) &&
571 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
572 (pos
> wcp
->offset
+ wcp
->data_size
) &&
573 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
575 /* ASCII art.... JRA.
579 +---------------+---------------+
580 | Cached data | Cache buffer |
581 +---------------+---------------+
583 +-------------------+
585 +-------------------+
590 * Non-contiguous write part of which fits within
591 * the cache buffer and is extending the file
592 * and the cache contents reflect the current
593 * data up to the current end of the file.
598 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
) {
601 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
605 * Fill in the non-continuous area with zeros.
608 memset(wcp
->data
+ wcp
->data_size
, '\0',
609 pos
- (wcp
->offset
+ wcp
->data_size
) );
611 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
614 * Update the current buffer size with the new data.
617 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
618 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
622 * Update the file size if changed.
625 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
626 if (wcp_file_size_change(fsp
) == -1) {
632 * If we used all the data then
639 cache_flush_needed
= True
;
643 * Move the start of data forward by the amount used,
644 * cut down the amount left by the same amount.
651 DO_PROFILE_INC(writecache_abutted_writes
);
652 total_written
= data_used
;
656 } else if ( (pos
>= wcp
->file_size
) &&
658 (wcp
->file_size
== wcp
->offset
+ wcp
->data_size
) &&
659 (pos
< wcp
->file_size
+ wcp
->alloc_size
)) {
665 +---------------+---------------+
666 | Cached data | Cache buffer |
667 +---------------+---------------+
669 |<------- allocated size ---------------->|
675 MS-Office seems to do this a lot to determine if there's enough
676 space on the filesystem to write a new file.
681 +-----------------------+--------+
682 | Zeroed Cached data | 1 Byte |
683 +-----------------------+--------+
686 flush_write_cache(fsp
, WRITE_FLUSH
);
687 wcp
->offset
= wcp
->file_size
;
688 wcp
->data_size
= pos
- wcp
->file_size
+ 1;
689 memset(wcp
->data
, '\0', wcp
->data_size
);
690 memcpy(wcp
->data
+ wcp
->data_size
-1, data
, 1);
693 * Update the file size if changed.
696 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
697 if (wcp_file_size_change(fsp
) == -1) {
706 /* ASCII art..... JRA.
710 +---------------+---------------+
711 | Cached data | Cache buffer |
712 +---------------+---------------+
714 +-------------------+
716 +-------------------+
720 +---------------+---------------+
721 | Cached data | Cache buffer |
722 +---------------+---------------+
724 +-------------------+
726 +-------------------+
730 +---------------+---------------+
731 | Cached data | Cache buffer |
732 +---------------+---------------+
734 +-----------------------------------------------------+
736 +-----------------------------------------------------+
741 * Write is bigger than buffer, or there is no overlap on the
745 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
746 len = %u\n",fsp
->fh
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
749 * If write would fit in the cache, and is larger than
750 * the data already in the cache, flush the cache and
751 * preferentially copy the data new data into it. Otherwise
752 * just write the data directly.
755 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
756 cache_flush_needed
= True
;
758 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
761 * If the write overlaps the entire cache, then
762 * discard the current contents of the cache.
763 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
766 if ((pos
<= wcp
->offset
) &&
767 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
768 DEBUG(9,("write_file: discarding overwritten write \
769 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
773 DO_PROFILE_INC(writecache_direct_writes
);
778 if (pos
+ ret
> wcp
->file_size
) {
779 wcp
->file_size
= pos
+ ret
;
789 if (cache_flush_needed
) {
790 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
791 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
792 write_path
, fsp
->fh
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
793 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
795 flush_write_cache(fsp
, WRITE_FLUSH
);
800 * If the write request is bigger than the cache
801 * size, write it all out.
804 if (n
> wcp
->alloc_size
) {
805 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
810 if (pos
+ ret
> wcp
->file_size
) {
811 wcp
->file_size
= pos
+ n
;
814 DO_PROFILE_INC(writecache_direct_writes
);
815 return total_written
+ n
;
819 * If there's any data left, cache it.
824 if (wcp
->data_size
) {
825 DO_PROFILE_INC(writecache_abutted_writes
);
827 DO_PROFILE_INC(writecache_init_writes
);
831 if ((wcp
->data_size
== 0)
832 && (pos
> wcp
->file_size
)
833 && (pos
+ n
<= wcp
->file_size
+ wcp
->alloc_size
)) {
835 * This is a write completely beyond the
836 * current EOF, but within reach of the write
837 * cache. We expect fill-up writes pretty
838 * soon, so it does not make sense to start
839 * the write cache at the current
840 * offset. These fill-up writes would trigger
841 * separate pwrites or even unnecessary cache
842 * flushes because they overlap if this is a
843 * one-byte allocating write.
845 wcp
->offset
= wcp
->file_size
;
846 wcp
->data_size
= pos
- wcp
->file_size
;
847 memset(wcp
->data
, 0, wcp
->data_size
);
850 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
851 if (wcp
->data_size
== 0) {
853 DO_PROFILE_INC(writecache_num_write_caches
);
858 * Update the file size if changed.
861 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
862 if (wcp_file_size_change(fsp
) == -1) {
866 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
867 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
870 return total_written
; /* .... that's a write :) */
873 return total_written
;
876 /****************************************************************************
877 Delete the write cache structure.
878 ****************************************************************************/
880 void delete_write_cache(files_struct
*fsp
)
888 if(!(wcp
= fsp
->wcp
)) {
892 DO_PROFILE_DEC(writecache_allocated_write_caches
);
893 allocated_write_caches
--;
895 SMB_ASSERT(wcp
->data_size
== 0);
897 SAFE_FREE(wcp
->data
);
900 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
904 /****************************************************************************
905 Setup the write cache structure.
906 ****************************************************************************/
908 static bool setup_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
910 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
913 if (allocated_write_caches
>= MAX_WRITE_CACHES
) {
917 if(alloc_size
== 0 || fsp
->wcp
) {
921 if((wcp
= SMB_MALLOC_P(write_cache
)) == NULL
) {
922 DEBUG(0,("setup_write_cache: malloc fail.\n"));
926 wcp
->file_size
= file_size
;
928 wcp
->alloc_size
= alloc_size
;
930 if((wcp
->data
= (char *)SMB_MALLOC(wcp
->alloc_size
)) == NULL
) {
931 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
932 (unsigned int)wcp
->alloc_size
));
937 memset(wcp
->data
, '\0', wcp
->alloc_size
);
940 DO_PROFILE_INC(writecache_allocated_write_caches
);
941 allocated_write_caches
++;
943 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
944 fsp_str_dbg(fsp
), (unsigned long)wcp
->alloc_size
));
949 /****************************************************************************
950 Cope with a size change.
951 ****************************************************************************/
953 void set_filelen_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
956 /* The cache *must* have been flushed before we do this. */
957 if (fsp
->wcp
->data_size
!= 0) {
959 if (asprintf(&msg
, "set_filelen_write_cache: size change "
960 "on file %s with write cache size = %lu\n",
961 fsp
->fsp_name
->base_name
,
962 (unsigned long)fsp
->wcp
->data_size
) != -1) {
965 smb_panic("set_filelen_write_cache");
968 fsp
->wcp
->file_size
= file_size
;
972 /*******************************************************************
973 Flush a write cache struct to disk.
974 ********************************************************************/
976 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
978 write_cache
*wcp
= fsp
->wcp
;
982 if(!wcp
|| !wcp
->data_size
) {
986 data_size
= wcp
->data_size
;
989 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
991 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
992 fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
995 if(data_size
== wcp
->alloc_size
) {
996 DO_PROFILE_INC(writecache_num_perfect_writes
);
1000 ret
= real_write_file(NULL
, fsp
, wcp
->data
, wcp
->offset
, data_size
);
1003 * Ensure file size if kept up to date if write extends file.
1006 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
)) {
1007 wcp
->file_size
= wcp
->offset
+ ret
;
1013 /*******************************************************************
1015 ********************************************************************/
1017 NTSTATUS
sync_file(connection_struct
*conn
, files_struct
*fsp
, bool write_through
)
1019 if (fsp
->fh
->fd
== -1)
1020 return NT_STATUS_INVALID_HANDLE
;
1022 if (lp_strict_sync(SNUM(conn
)) &&
1023 (lp_syncalways(SNUM(conn
)) || write_through
)) {
1024 int ret
= flush_write_cache(fsp
, SYNC_FLUSH
);
1026 return map_nt_error_from_unix(errno
);
1028 ret
= SMB_VFS_FSYNC(fsp
);
1030 return map_nt_error_from_unix(errno
);
1033 return NT_STATUS_OK
;
1036 /************************************************************
1037 Perform a stat whether a valid fd or not.
1038 ************************************************************/
1040 int fsp_stat(files_struct
*fsp
)
1042 if (fsp
->fh
->fd
== -1) {
1043 if (fsp
->posix_open
) {
1044 return SMB_VFS_LSTAT(fsp
->conn
, fsp
->fsp_name
);
1046 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
);
1049 return SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
);