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 typedef struct write_cache
{
36 static bool setup_write_cache(files_struct
*, off_t
);
38 /****************************************************************************
39 Read from write cache if we can.
40 ****************************************************************************/
42 static bool read_from_write_cache(files_struct
*fsp
,char *data
,off_t pos
,size_t n
)
44 write_cache
*wcp
= fsp
->wcp
;
50 if( n
> wcp
->data_size
|| pos
< wcp
->offset
|| pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
54 memcpy(data
, wcp
->data
+ (pos
- wcp
->offset
), n
);
56 DO_PROFILE_INC(writecache_read_hits
);
61 /****************************************************************************
63 ****************************************************************************/
65 ssize_t
read_file(files_struct
*fsp
,char *data
,off_t pos
,size_t n
)
67 ssize_t ret
=0,readret
;
69 /* you can't read from print files */
70 if (fsp
->print_file
) {
76 * Serve from write cache if we can.
79 if(read_from_write_cache(fsp
, data
, pos
, n
)) {
80 fsp
->fh
->pos
= pos
+ n
;
81 fsp
->fh
->position_information
= fsp
->fh
->pos
;
85 flush_write_cache(fsp
, READ_FLUSH
);
93 readret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
96 if ((errno
== EAGAIN
) && numretries
) {
97 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
104 #else /* NO DMF fix. */
105 readret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
116 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
117 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
120 fsp
->fh
->position_information
= fsp
->fh
->pos
;
125 /****************************************************************************
126 *Really* write to a file.
127 ****************************************************************************/
129 static ssize_t
real_write_file(struct smb_request
*req
,
138 ret
= vfs_write_data(req
, fsp
, data
, n
);
141 if (pos
&& lp_strict_allocate(SNUM(fsp
->conn
) &&
143 if (vfs_fill_sparse(fsp
, pos
) == -1) {
147 ret
= vfs_pwrite_data(req
, fsp
, data
, n
, pos
);
150 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
151 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
156 /* Yes - this is correct - writes don't update this. JRA. */
157 /* Found by Samba4 tests. */
159 fsp
->position_information
= fsp
->pos
;
166 /****************************************************************************
167 File size cache change.
168 Updates size on disk but doesn't flush the cache.
169 ****************************************************************************/
171 static int wcp_file_size_change(files_struct
*fsp
)
174 write_cache
*wcp
= fsp
->wcp
;
176 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
177 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
179 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
180 "error %s\n", fsp_str_dbg(fsp
),
181 (double)wcp
->file_size
, strerror(errno
)));
186 void update_write_time_handler(struct event_context
*ctx
,
187 struct timed_event
*te
,
191 files_struct
*fsp
= (files_struct
*)private_data
;
193 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp
)));
195 /* change the write time in the open file db. */
196 (void)set_write_time(fsp
->file_id
, timespec_current());
199 notify_fname(fsp
->conn
, NOTIFY_ACTION_MODIFIED
,
200 FILE_NOTIFY_CHANGE_LAST_WRITE
, fsp
->fsp_name
->base_name
);
202 /* Remove the timed event handler. */
203 TALLOC_FREE(fsp
->update_write_time_event
);
206 /*********************************************************
207 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
209 *********************************************************/
211 void trigger_write_time_update(struct files_struct
*fsp
)
215 if (fsp
->posix_open
) {
216 /* Don't use delayed writes on POSIX files. */
220 if (fsp
->write_time_forced
) {
221 /* No point - "sticky" write times
227 /* We need to remember someone did a write
228 * and update to current time on close. */
230 fsp
->update_write_time_on_close
= true;
232 if (fsp
->update_write_time_triggered
) {
234 * We only update the write time after 2 seconds
235 * on the first normal write. After that
236 * no other writes affect this until close.
240 fsp
->update_write_time_triggered
= true;
242 delay
= lp_parm_int(SNUM(fsp
->conn
),
243 "smbd", "writetimeupdatedelay",
244 WRITE_TIME_UPDATE_USEC_DELAY
);
246 DEBUG(5, ("Update write time %d usec later on %s\n",
247 delay
, fsp_str_dbg(fsp
)));
249 /* trigger the update 2 seconds later */
250 fsp
->update_write_time_event
=
251 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, NULL
,
252 timeval_current_ofs_usec(delay
),
253 update_write_time_handler
, fsp
);
256 void trigger_write_time_update_immediate(struct files_struct
*fsp
)
258 struct smb_file_time ft
;
260 if (fsp
->posix_open
) {
261 /* Don't use delayed writes on POSIX files. */
265 if (fsp
->write_time_forced
) {
267 * No point - "sticky" write times
273 TALLOC_FREE(fsp
->update_write_time_event
);
274 DEBUG(5, ("Update write time immediate on %s\n",
277 /* After an immediate update, reset the trigger. */
278 fsp
->update_write_time_triggered
= true;
279 fsp
->update_write_time_on_close
= false;
282 ft
.mtime
= timespec_current();
284 /* Update the time in the open file db. */
285 (void)set_write_time(fsp
->file_id
, ft
.mtime
);
287 /* Now set on disk - takes care of notify. */
288 (void)smb_set_file_time(fsp
->conn
, fsp
, fsp
->fsp_name
, &ft
, false);
291 /****************************************************************************
293 ****************************************************************************/
295 ssize_t
write_file(struct smb_request
*req
,
301 write_cache
*wcp
= fsp
->wcp
;
302 ssize_t total_written
= 0;
305 if (fsp
->print_file
) {
309 ret
= print_spool_write(fsp
, data
, n
, pos
, &t
);
317 if (!fsp
->can_write
) {
322 if (!fsp
->modified
) {
323 fsp
->modified
= True
;
325 if (SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
) == 0) {
326 trigger_write_time_update(fsp
);
327 if (!fsp
->posix_open
&&
328 (lp_store_dos_attributes(SNUM(fsp
->conn
)) ||
329 MAP_ARCHIVE(fsp
->conn
))) {
330 int dosmode
= dos_mode(fsp
->conn
, fsp
->fsp_name
);
331 if (!IS_DOS_ARCHIVE(dosmode
)) {
332 file_set_dosmode(fsp
->conn
, fsp
->fsp_name
,
333 dosmode
| FILE_ATTRIBUTE_ARCHIVE
, NULL
, false);
338 * If this is the first write and we have an exclusive oplock then setup
342 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
343 setup_write_cache(fsp
,
344 fsp
->fsp_name
->st
.st_ex_size
);
351 DO_PROFILE_INC(writecache_total_writes
);
352 if (!fsp
->oplock_type
) {
353 DO_PROFILE_INC(writecache_non_oplock_writes
);
358 * If this file is level II oplocked then we need
359 * to grab the shared memory lock and inform all
360 * other files with a level II lock that they need
361 * to flush their read caches. We keep the lock over
362 * the shared memory area whilst doing this.
365 /* This should actually be improved to span the write. */
366 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
367 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
370 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
371 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
372 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
373 profile_p
->writecache_init_writes
,
374 profile_p
->writecache_abutted_writes
,
375 profile_p
->writecache_total_writes
,
376 profile_p
->writecache_non_oplock_writes
,
377 profile_p
->writecache_allocated_write_caches
,
378 profile_p
->writecache_num_write_caches
,
379 profile_p
->writecache_direct_writes
,
380 profile_p
->writecache_num_perfect_writes
,
381 profile_p
->writecache_read_hits
));
383 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
384 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
385 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
386 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
387 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
388 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
389 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
390 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
394 if (wcp
&& req
->unread_bytes
) {
395 /* If we're using receivefile don't
396 * deal with a write cache.
398 flush_write_cache(fsp
, WRITE_FLUSH
);
399 delete_write_cache(fsp
);
404 DO_PROFILE_INC(writecache_direct_writes
);
405 total_written
= real_write_file(req
, fsp
, data
, pos
, n
);
406 return total_written
;
409 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
410 "wcp->data_size=%u\n", fsp_str_dbg(fsp
), fsp
->fh
->fd
,
411 (double)pos
, (unsigned int)n
, (double)wcp
->offset
,
412 (unsigned int)wcp
->data_size
));
414 fsp
->fh
->pos
= pos
+ n
;
416 if ((n
== 1) && (data
[0] == '\0') && (pos
> wcp
->file_size
)) {
420 * This is a 1-byte write of a 0 beyond the EOF and
421 * thus implicitly also beyond the current active
422 * write cache, the typical file-extending (and
423 * allocating, but we're using the write cache here)
424 * write done by Windows. We just have to ftruncate
425 * the file and rely on posix semantics to return
426 * zeros for non-written file data that is within the
429 * We can not use wcp_file_size_change here because we
430 * might have an existing write cache, and
431 * wcp_file_size_change assumes a change to just the
432 * end of the current write cache.
435 wcp
->file_size
= pos
+ 1;
436 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
438 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f"
439 "error %s\n", fsp_str_dbg(fsp
),
440 (double)wcp
->file_size
, strerror(errno
)));
448 * If we have active cache and it isn't contiguous then we flush.
449 * NOTE: There is a small problem with running out of disk ....
452 if (wcp
->data_size
) {
453 bool cache_flush_needed
= False
;
455 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
457 /* ASCII art.... JRA.
459 +--------------+-----
460 | Cached data | Rest of allocated cache buffer....
461 +--------------+-----
463 +-------------------+
465 +-------------------+
470 * Start of write overlaps or abutts the existing data.
473 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
475 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
478 * Update the current buffer size with the new data.
481 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
482 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
486 * Update the file size if changed.
489 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
490 if (wcp_file_size_change(fsp
) == -1) {
496 * If we used all the data then
503 cache_flush_needed
= True
;
506 * Move the start of data forward by the amount used,
507 * cut down the amount left by the same amount.
514 DO_PROFILE_INC(writecache_abutted_writes
);
515 total_written
= data_used
;
519 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
520 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
522 /* ASCII art.... JRA.
528 +-------------------+
530 +-------------------+
535 * End of write overlaps the existing data.
538 size_t data_used
= pos
+ n
- wcp
->offset
;
540 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
543 * Update the current buffer size with the new data.
546 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
547 wcp
->data_size
= pos
+ n
- wcp
->offset
;
551 * Update the file size if changed.
554 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
555 if (wcp_file_size_change(fsp
) == -1) {
561 * We don't need to move the start of data, but we
562 * cut down the amount left by the amount used.
568 * We cannot have used all the data here.
571 cache_flush_needed
= True
;
573 DO_PROFILE_INC(writecache_abutted_writes
);
574 total_written
= data_used
;
578 } else if ( (pos
>= wcp
->file_size
) &&
579 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
580 (pos
> wcp
->offset
+ wcp
->data_size
) &&
581 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
583 /* ASCII art.... JRA.
587 +---------------+---------------+
588 | Cached data | Cache buffer |
589 +---------------+---------------+
591 +-------------------+
593 +-------------------+
598 * Non-contiguous write part of which fits within
599 * the cache buffer and is extending the file
600 * and the cache contents reflect the current
601 * data up to the current end of the file.
606 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
) {
609 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
613 * Fill in the non-continuous area with zeros.
616 memset(wcp
->data
+ wcp
->data_size
, '\0',
617 pos
- (wcp
->offset
+ wcp
->data_size
) );
619 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
622 * Update the current buffer size with the new data.
625 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
626 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
630 * Update the file size if changed.
633 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
634 if (wcp_file_size_change(fsp
) == -1) {
640 * If we used all the data then
647 cache_flush_needed
= True
;
651 * Move the start of data forward by the amount used,
652 * cut down the amount left by the same amount.
659 DO_PROFILE_INC(writecache_abutted_writes
);
660 total_written
= data_used
;
664 } else if ( (pos
>= wcp
->file_size
) &&
666 (wcp
->file_size
== wcp
->offset
+ wcp
->data_size
) &&
667 (pos
< wcp
->file_size
+ wcp
->alloc_size
)) {
673 +---------------+---------------+
674 | Cached data | Cache buffer |
675 +---------------+---------------+
677 |<------- allocated size ---------------->|
683 MS-Office seems to do this a lot to determine if there's enough
684 space on the filesystem to write a new file.
689 +-----------------------+--------+
690 | Zeroed Cached data | 1 Byte |
691 +-----------------------+--------+
694 flush_write_cache(fsp
, WRITE_FLUSH
);
695 wcp
->offset
= wcp
->file_size
;
696 wcp
->data_size
= pos
- wcp
->file_size
+ 1;
697 memset(wcp
->data
, '\0', wcp
->data_size
);
698 memcpy(wcp
->data
+ wcp
->data_size
-1, data
, 1);
701 * Update the file size if changed.
704 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
705 if (wcp_file_size_change(fsp
) == -1) {
714 /* ASCII art..... JRA.
718 +---------------+---------------+
719 | Cached data | Cache buffer |
720 +---------------+---------------+
722 +-------------------+
724 +-------------------+
728 +---------------+---------------+
729 | Cached data | Cache buffer |
730 +---------------+---------------+
732 +-------------------+
734 +-------------------+
738 +---------------+---------------+
739 | Cached data | Cache buffer |
740 +---------------+---------------+
742 +-----------------------------------------------------+
744 +-----------------------------------------------------+
749 * Write is bigger than buffer, or there is no overlap on the
753 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
754 len = %u\n",fsp
->fh
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
757 * If write would fit in the cache, and is larger than
758 * the data already in the cache, flush the cache and
759 * preferentially copy the data new data into it. Otherwise
760 * just write the data directly.
763 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
764 cache_flush_needed
= True
;
766 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
769 * If the write overlaps the entire cache, then
770 * discard the current contents of the cache.
771 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
774 if ((pos
<= wcp
->offset
) &&
775 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
776 DEBUG(9,("write_file: discarding overwritten write \
777 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
781 DO_PROFILE_INC(writecache_direct_writes
);
786 if (pos
+ ret
> wcp
->file_size
) {
787 wcp
->file_size
= pos
+ ret
;
797 if (cache_flush_needed
) {
798 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
799 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
800 write_path
, fsp
->fh
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
801 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
803 flush_write_cache(fsp
, WRITE_FLUSH
);
808 * If the write request is bigger than the cache
809 * size, write it all out.
812 if (n
> wcp
->alloc_size
) {
813 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
818 if (pos
+ ret
> wcp
->file_size
) {
819 wcp
->file_size
= pos
+ n
;
822 DO_PROFILE_INC(writecache_direct_writes
);
823 return total_written
+ n
;
827 * If there's any data left, cache it.
832 if (wcp
->data_size
) {
833 DO_PROFILE_INC(writecache_abutted_writes
);
835 DO_PROFILE_INC(writecache_init_writes
);
839 if ((wcp
->data_size
== 0)
840 && (pos
> wcp
->file_size
)
841 && (pos
+ n
<= wcp
->file_size
+ wcp
->alloc_size
)) {
843 * This is a write completely beyond the
844 * current EOF, but within reach of the write
845 * cache. We expect fill-up writes pretty
846 * soon, so it does not make sense to start
847 * the write cache at the current
848 * offset. These fill-up writes would trigger
849 * separate pwrites or even unnecessary cache
850 * flushes because they overlap if this is a
851 * one-byte allocating write.
853 wcp
->offset
= wcp
->file_size
;
854 wcp
->data_size
= pos
- wcp
->file_size
;
855 memset(wcp
->data
, 0, wcp
->data_size
);
858 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
859 if (wcp
->data_size
== 0) {
861 DO_PROFILE_INC(writecache_num_write_caches
);
866 * Update the file size if changed.
869 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
870 if (wcp_file_size_change(fsp
) == -1) {
874 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
875 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
878 return total_written
; /* .... that's a write :) */
881 return total_written
;
884 /****************************************************************************
885 Delete the write cache structure.
886 ****************************************************************************/
888 void delete_write_cache(files_struct
*fsp
)
896 if(!(wcp
= fsp
->wcp
)) {
900 DO_PROFILE_DEC(writecache_allocated_write_caches
);
901 allocated_write_caches
--;
903 SMB_ASSERT(wcp
->data_size
== 0);
905 SAFE_FREE(wcp
->data
);
908 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
912 /****************************************************************************
913 Setup the write cache structure.
914 ****************************************************************************/
916 static bool setup_write_cache(files_struct
*fsp
, off_t file_size
)
918 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
921 if (allocated_write_caches
>= MAX_WRITE_CACHES
) {
925 if(alloc_size
== 0 || fsp
->wcp
) {
929 if((wcp
= SMB_MALLOC_P(write_cache
)) == NULL
) {
930 DEBUG(0,("setup_write_cache: malloc fail.\n"));
934 wcp
->file_size
= file_size
;
936 wcp
->alloc_size
= alloc_size
;
938 if((wcp
->data
= (char *)SMB_MALLOC(wcp
->alloc_size
)) == NULL
) {
939 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
940 (unsigned int)wcp
->alloc_size
));
945 memset(wcp
->data
, '\0', wcp
->alloc_size
);
948 DO_PROFILE_INC(writecache_allocated_write_caches
);
949 allocated_write_caches
++;
951 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
952 fsp_str_dbg(fsp
), (unsigned long)wcp
->alloc_size
));
957 /****************************************************************************
958 Cope with a size change.
959 ****************************************************************************/
961 void set_filelen_write_cache(files_struct
*fsp
, off_t file_size
)
964 /* The cache *must* have been flushed before we do this. */
965 if (fsp
->wcp
->data_size
!= 0) {
967 if (asprintf(&msg
, "set_filelen_write_cache: size change "
968 "on file %s with write cache size = %lu\n",
969 fsp
->fsp_name
->base_name
,
970 (unsigned long)fsp
->wcp
->data_size
) != -1) {
973 smb_panic("set_filelen_write_cache");
976 fsp
->wcp
->file_size
= file_size
;
980 /*******************************************************************
981 Flush a write cache struct to disk.
982 ********************************************************************/
984 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
986 write_cache
*wcp
= fsp
->wcp
;
990 if(!wcp
|| !wcp
->data_size
) {
994 data_size
= wcp
->data_size
;
997 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
999 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
1000 fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
1003 if(data_size
== wcp
->alloc_size
) {
1004 DO_PROFILE_INC(writecache_num_perfect_writes
);
1008 ret
= real_write_file(NULL
, fsp
, wcp
->data
, wcp
->offset
, data_size
);
1011 * Ensure file size if kept up to date if write extends file.
1014 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
)) {
1015 wcp
->file_size
= wcp
->offset
+ ret
;
1021 /*******************************************************************
1023 ********************************************************************/
1025 NTSTATUS
sync_file(connection_struct
*conn
, files_struct
*fsp
, bool write_through
)
1027 if (fsp
->fh
->fd
== -1)
1028 return NT_STATUS_INVALID_HANDLE
;
1030 if (lp_strict_sync(SNUM(conn
)) &&
1031 (lp_syncalways(SNUM(conn
)) || write_through
)) {
1032 int ret
= flush_write_cache(fsp
, SYNC_FLUSH
);
1034 return map_nt_error_from_unix(errno
);
1036 ret
= SMB_VFS_FSYNC(fsp
);
1038 return map_nt_error_from_unix(errno
);
1041 return NT_STATUS_OK
;
1044 /************************************************************
1045 Perform a stat whether a valid fd or not.
1046 ************************************************************/
1048 int fsp_stat(files_struct
*fsp
)
1050 if (fsp
->fh
->fd
== -1) {
1051 if (fsp
->posix_open
) {
1052 return SMB_VFS_LSTAT(fsp
->conn
, fsp
->fsp_name
);
1054 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
);
1057 return SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
);