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"
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 struct 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_cached_reads
);
61 /****************************************************************************
63 ****************************************************************************/
65 ssize_t
read_file(files_struct
*fsp
,char *data
,off_t pos
,size_t n
)
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
, SAMBA_READ_FLUSH
);
90 ret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
97 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
98 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
101 fsp
->fh
->position_information
= fsp
->fh
->pos
;
106 /****************************************************************************
107 *Really* write to a file.
108 ****************************************************************************/
110 static ssize_t
real_write_file(struct smb_request
*req
,
119 if (pos
&& lp_strict_allocate(SNUM(fsp
->conn
) &&
121 if (vfs_fill_sparse(fsp
, pos
) == -1) {
125 ret
= vfs_pwrite_data(req
, fsp
, data
, n
, pos
);
127 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
128 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
133 /* Yes - this is correct - writes don't update this. JRA. */
134 /* Found by Samba4 tests. */
136 fsp
->position_information
= fsp
->pos
;
143 /****************************************************************************
144 File size cache change.
145 Updates size on disk but doesn't flush the cache.
146 ****************************************************************************/
148 static int wcp_file_size_change(files_struct
*fsp
)
151 struct write_cache
*wcp
= fsp
->wcp
;
153 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
154 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
156 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
157 "error %s\n", fsp_str_dbg(fsp
),
158 (double)wcp
->file_size
, strerror(errno
)));
163 void fsp_flush_write_time_update(struct files_struct
*fsp
)
166 * Note this won't expect any impersonation!
167 * So don't call any SMB_VFS operations here!
170 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp
)));
172 /* change the write time in the open file db. */
173 (void)set_write_time(fsp
->file_id
, timespec_current());
176 notify_fname(fsp
->conn
, NOTIFY_ACTION_MODIFIED
,
177 FILE_NOTIFY_CHANGE_LAST_WRITE
, fsp
->fsp_name
->base_name
);
179 /* Remove the timed event handler. */
180 TALLOC_FREE(fsp
->update_write_time_event
);
183 static void update_write_time_handler(struct tevent_context
*ctx
,
184 struct tevent_timer
*te
,
188 files_struct
*fsp
= (files_struct
*)private_data
;
189 fsp_flush_write_time_update(fsp
);
192 /*********************************************************
193 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
195 *********************************************************/
197 void trigger_write_time_update(struct files_struct
*fsp
)
201 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
202 /* Don't use delayed writes on POSIX files. */
206 if (fsp
->write_time_forced
) {
207 /* No point - "sticky" write times
213 /* We need to remember someone did a write
214 * and update to current time on close. */
216 fsp
->update_write_time_on_close
= true;
218 if (fsp
->update_write_time_triggered
) {
220 * We only update the write time after 2 seconds
221 * on the first normal write. After that
222 * no other writes affect this until close.
226 fsp
->update_write_time_triggered
= true;
228 delay
= lp_parm_int(SNUM(fsp
->conn
),
229 "smbd", "writetimeupdatedelay",
230 WRITE_TIME_UPDATE_USEC_DELAY
);
232 DEBUG(5, ("Update write time %d usec later on %s\n",
233 delay
, fsp_str_dbg(fsp
)));
235 /* trigger the update 2 seconds later */
236 fsp
->update_write_time_event
=
237 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, NULL
,
238 timeval_current_ofs_usec(delay
),
239 update_write_time_handler
, fsp
);
242 void trigger_write_time_update_immediate(struct files_struct
*fsp
)
244 struct smb_file_time ft
;
246 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
247 /* Don't use delayed writes on POSIX files. */
251 if (fsp
->write_time_forced
) {
253 * No point - "sticky" write times
259 TALLOC_FREE(fsp
->update_write_time_event
);
260 DEBUG(5, ("Update write time immediate on %s\n",
263 /* After an immediate update, reset the trigger. */
264 fsp
->update_write_time_triggered
= true;
265 fsp
->update_write_time_on_close
= false;
268 ft
.mtime
= timespec_current();
270 /* Update the time in the open file db. */
271 (void)set_write_time(fsp
->file_id
, ft
.mtime
);
273 /* Now set on disk - takes care of notify. */
274 (void)smb_set_file_time(fsp
->conn
, fsp
, fsp
->fsp_name
, &ft
, false);
277 void mark_file_modified(files_struct
*fsp
)
285 fsp
->modified
= true;
287 if (SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
) != 0) {
290 trigger_write_time_update(fsp
);
292 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
295 if (!(lp_store_dos_attributes(SNUM(fsp
->conn
)) ||
296 MAP_ARCHIVE(fsp
->conn
))) {
300 dosmode
= dos_mode(fsp
->conn
, fsp
->fsp_name
);
301 if (IS_DOS_ARCHIVE(dosmode
)) {
304 file_set_dosmode(fsp
->conn
, fsp
->fsp_name
,
305 dosmode
| FILE_ATTRIBUTE_ARCHIVE
, NULL
, false);
308 /****************************************************************************
310 ****************************************************************************/
312 ssize_t
write_file(struct smb_request
*req
,
318 struct write_cache
*wcp
= fsp
->wcp
;
319 ssize_t total_written
= 0;
322 if (fsp
->print_file
) {
326 ret
= print_spool_write(fsp
, data
, n
, pos
, &t
);
334 if (!fsp
->can_write
) {
340 * If this is the first write and we have an exclusive oplock
341 * then setup the write cache.
344 if (!fsp
->modified
&&
345 EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) &&
348 * Note: no write cache with leases!
349 * as the handles would have to share the write cache
350 * that's possible but an improvement for another day...
352 setup_write_cache(fsp
, fsp
->fsp_name
->st
.st_ex_size
);
356 mark_file_modified(fsp
);
358 DO_PROFILE_INC(writecache_total_writes
);
359 if (!fsp
->oplock_type
) {
360 DO_PROFILE_INC(writecache_non_oplock_writes
);
364 * If this file is level II oplocked then we need
365 * to grab the shared memory lock and inform all
366 * other files with a level II lock that they need
367 * to flush their read caches. We keep the lock over
368 * the shared memory area whilst doing this.
371 /* This should actually be improved to span the write. */
372 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
373 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
375 if (wcp
&& req
->unread_bytes
) {
376 /* If we're using receivefile don't
377 * deal with a write cache.
379 flush_write_cache(fsp
, SAMBA_WRITE_FLUSH
);
380 delete_write_cache(fsp
);
385 DO_PROFILE_INC(writecache_direct_writes
);
386 total_written
= real_write_file(req
, fsp
, data
, pos
, n
);
387 return total_written
;
390 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
391 "wcp->data_size=%u\n", fsp_str_dbg(fsp
), fsp
->fh
->fd
,
392 (double)pos
, (unsigned int)n
, (double)wcp
->offset
,
393 (unsigned int)wcp
->data_size
));
395 fsp
->fh
->pos
= pos
+ n
;
397 if ((n
== 1) && (data
[0] == '\0') && (pos
> wcp
->file_size
)) {
401 * This is a 1-byte write of a 0 beyond the EOF and
402 * thus implicitly also beyond the current active
403 * write cache, the typical file-extending (and
404 * allocating, but we're using the write cache here)
405 * write done by Windows. We just have to ftruncate
406 * the file and rely on posix semantics to return
407 * zeros for non-written file data that is within the
410 * We can not use wcp_file_size_change here because we
411 * might have an existing write cache, and
412 * wcp_file_size_change assumes a change to just the
413 * end of the current write cache.
416 wcp
->file_size
= pos
+ 1;
417 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
419 DEBUG(0, ("wcp_file_size_change (%s): ftruncate of "
420 "size %.0f error %s\n", fsp_str_dbg(fsp
),
421 (double)wcp
->file_size
, strerror(errno
)));
429 * If we have active cache and it isn't contiguous then we flush.
430 * NOTE: There is a small problem with running out of disk ....
433 if (wcp
->data_size
) {
434 bool cache_flush_needed
= False
;
436 if ((pos
>= wcp
->offset
) &&
437 (pos
<= wcp
->offset
+ wcp
->data_size
)) {
439 /* ASCII art.... JRA.
441 +--------------+-----
442 | Cached data | Rest of allocated cache buffer....
443 +--------------+-----
445 +-------------------+
447 +-------------------+
452 * Start of write overlaps or abutts the existing data.
457 data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)),
460 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
,
464 * Update the current buffer size with the new data.
467 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
468 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
472 * Update the file size if changed.
475 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
476 if (wcp_file_size_change(fsp
) == -1) {
482 * If we used all the data then
489 cache_flush_needed
= True
;
492 * Move the start of data forward by the amount used,
493 * cut down the amount left by the same amount.
500 DO_PROFILE_INC(writecache_abutted_writes
);
501 total_written
= data_used
;
505 } else if ((pos
< wcp
->offset
) &&
506 (pos
+ n
> wcp
->offset
) &&
507 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
509 /* ASCII art.... JRA.
515 +-------------------+
517 +-------------------+
522 * End of write overlaps the existing data.
525 size_t data_used
= pos
+ n
- wcp
->offset
;
527 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
530 * Update the current buffer size with the new data.
533 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
534 wcp
->data_size
= pos
+ n
- wcp
->offset
;
538 * Update the file size if changed.
541 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
542 if (wcp_file_size_change(fsp
) == -1) {
548 * We don't need to move the start of data, but we
549 * cut down the amount left by the amount used.
555 * We cannot have used all the data here.
558 cache_flush_needed
= True
;
560 DO_PROFILE_INC(writecache_abutted_writes
);
561 total_written
= data_used
;
565 } else if ((pos
>= wcp
->file_size
) &&
566 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
567 (pos
> wcp
->offset
+ wcp
->data_size
) &&
568 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
570 /* ASCII art.... JRA.
574 +---------------+---------------+
575 | Cached data | Cache buffer |
576 +---------------+---------------+
578 +-------------------+
580 +-------------------+
585 * Non-contiguous write part of which fits within
586 * the cache buffer and is extending the file
587 * and the cache contents reflect the current
588 * data up to the current end of the file.
593 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
) {
596 data_used
= wcp
->offset
+wcp
->alloc_size
-pos
;
600 * Fill in the non-continuous area with zeros.
603 memset(wcp
->data
+ wcp
->data_size
, '\0',
604 pos
- (wcp
->offset
+ wcp
->data_size
) );
606 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
,
610 * Update the current buffer size with the new data.
613 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
614 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
618 * Update the file size if changed.
621 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
622 if (wcp_file_size_change(fsp
) == -1) {
628 * If we used all the data then
635 cache_flush_needed
= True
;
639 * Move the start of data forward by the amount used,
640 * cut down the amount left by the same amount.
647 DO_PROFILE_INC(writecache_abutted_writes
);
648 total_written
= data_used
;
652 } else if ( (pos
>= wcp
->file_size
) &&
654 (wcp
->file_size
== wcp
->offset
+ wcp
->data_size
) &&
655 (pos
< wcp
->file_size
+ wcp
->alloc_size
)) {
661 +---------------+---------------+
662 | Cached data | Cache buffer |
663 +---------------+---------------+
665 |<------- allocated size ---------------->|
671 MS-Office seems to do this a lot to determine if
672 there's enough space on the filesystem to write a new
678 +-----------------------+--------+
679 | Zeroed Cached data | 1 Byte |
680 +-----------------------+--------+
683 flush_write_cache(fsp
, SAMBA_WRITE_FLUSH
);
684 wcp
->offset
= wcp
->file_size
;
685 wcp
->data_size
= pos
- wcp
->file_size
+ 1;
686 memset(wcp
->data
, '\0', wcp
->data_size
);
687 memcpy(wcp
->data
+ wcp
->data_size
-1, data
, 1);
690 * Update the file size if changed.
693 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
694 if (wcp_file_size_change(fsp
) == -1) {
703 /* ASCII art..... JRA.
707 +---------------+---------------+
708 | Cached data | Cache buffer |
709 +---------------+---------------+
717 +---------------+---------------+
718 | Cached data | Cache buffer |
719 +---------------+---------------+
721 +-------------------+
723 +-------------------+
727 +---------------+---------------+
728 | Cached data | Cache buffer |
729 +---------------+---------------+
731 +-----------------------------------------------------+
733 +-----------------------------------------------------+
738 * Write is bigger than buffer, or there is no
739 * overlap on the low or high ends.
742 DEBUG(9,("write_file: non cacheable write : fd = %d, "
743 "pos = %.0f, len = %u, "
744 "current cache pos = %.0f len = %u\n",
745 fsp
->fh
->fd
, (double)pos
, (unsigned int)n
,
747 (unsigned int)wcp
->data_size
));
750 * If write would fit in the cache, and is
751 * larger than the data already in the cache,
752 * flush the cache and preferentially copy the
753 * data new data into it. Otherwise just write
757 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
758 cache_flush_needed
= True
;
760 ssize_t ret
= real_write_file(NULL
, fsp
, data
,
764 * If the write overlaps the entire
765 * cache, then discard the current
766 * contents of the cache. Fix from
767 * Rasmus Borup Hansen rbh@math.ku.dk.
770 if ((pos
<= wcp
->offset
) &&
771 (pos
+ n
>= wcp
->offset
+wcp
->data_size
)) {
772 DEBUG(9,("write_file: discarding "
773 "overwritten write cache: "
774 "fd = %d, off=%.0f, "
775 "size=%u\n", fsp
->fh
->fd
,
777 (unsigned)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, ("SAMBA_WRITE_FLUSH:%d: due to noncontinuous "
799 "write: fd = %d, size = %.0f, pos = %.0f, "
800 "n = %u, wcp->offset=%.0f, "
801 "wcp->data_size=%u\n",
802 write_path
, fsp
->fh
->fd
,
803 (double)wcp
->file_size
, (double)pos
,
804 (unsigned int)n
, (double)wcp
->offset
,
805 (unsigned int)wcp
->data_size
));
807 flush_write_cache(fsp
, SAMBA_WRITE_FLUSH
);
812 * If the write request is bigger than the cache
813 * size, write it all out.
816 if (n
> wcp
->alloc_size
) {
817 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
822 if (pos
+ ret
> wcp
->file_size
) {
823 wcp
->file_size
= pos
+ n
;
826 DO_PROFILE_INC(writecache_direct_writes
);
827 return total_written
+ n
;
831 * If there's any data left, cache it.
835 DO_PROFILE_INC(writecache_cached_writes
);
836 if (wcp
->data_size
) {
837 DO_PROFILE_INC(writecache_abutted_writes
);
839 DO_PROFILE_INC(writecache_init_writes
);
842 if ((wcp
->data_size
== 0)
843 && (pos
> wcp
->file_size
)
844 && (pos
+ n
<= wcp
->file_size
+ wcp
->alloc_size
)) {
846 * This is a write completely beyond the
847 * current EOF, but within reach of the write
848 * cache. We expect fill-up writes pretty
849 * soon, so it does not make sense to start
850 * the write cache at the current
851 * offset. These fill-up writes would trigger
852 * separate pwrites or even unnecessary cache
853 * flushes because they overlap if this is a
854 * one-byte allocating write.
856 wcp
->offset
= wcp
->file_size
;
857 wcp
->data_size
= pos
- wcp
->file_size
;
858 memset(wcp
->data
, 0, wcp
->data_size
);
861 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
862 if (wcp
->data_size
== 0) {
868 * Update the file size if changed.
871 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
872 if (wcp_file_size_change(fsp
) == -1) {
876 DEBUG(9, ("wcp->offset = %.0f wcp->data_size = %u cache "
878 (double)wcp
->offset
, (unsigned int)wcp
->data_size
,
882 return total_written
; /* .... that's a write :) */
885 return total_written
;
888 /****************************************************************************
889 Delete the write cache structure.
890 ****************************************************************************/
892 void delete_write_cache(files_struct
*fsp
)
894 struct write_cache
*wcp
;
900 if(!(wcp
= fsp
->wcp
)) {
904 DO_PROFILE_INC(writecache_deallocations
);
905 allocated_write_caches
--;
907 SMB_ASSERT(wcp
->data_size
== 0);
909 SAFE_FREE(wcp
->data
);
912 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
916 /****************************************************************************
917 Setup the write cache structure.
918 ****************************************************************************/
920 static bool setup_write_cache(files_struct
*fsp
, off_t file_size
)
922 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
923 struct write_cache
*wcp
;
925 if (allocated_write_caches
>= MAX_WRITE_CACHES
) {
929 if(alloc_size
== 0 || fsp
->wcp
) {
933 if((wcp
= SMB_MALLOC_P(struct write_cache
)) == NULL
) {
934 DEBUG(0,("setup_write_cache: malloc fail.\n"));
938 wcp
->file_size
= file_size
;
940 wcp
->alloc_size
= alloc_size
;
942 if((wcp
->data
= (char *)SMB_MALLOC(wcp
->alloc_size
)) == NULL
) {
943 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
944 (unsigned int)wcp
->alloc_size
));
949 memset(wcp
->data
, '\0', wcp
->alloc_size
);
952 DO_PROFILE_INC(writecache_allocations
);
953 allocated_write_caches
++;
955 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
956 fsp_str_dbg(fsp
), (unsigned long)wcp
->alloc_size
));
961 /****************************************************************************
962 Cope with a size change.
963 ****************************************************************************/
965 void set_filelen_write_cache(files_struct
*fsp
, off_t file_size
)
968 /* The cache *must* have been flushed before we do this. */
969 if (fsp
->wcp
->data_size
!= 0) {
971 if (asprintf(&msg
, "set_filelen_write_cache: size change "
972 "on file %s with write cache size = %lu\n",
973 fsp
->fsp_name
->base_name
,
974 (unsigned long)fsp
->wcp
->data_size
) != -1) {
977 smb_panic("set_filelen_write_cache");
980 fsp
->wcp
->file_size
= file_size
;
984 /*******************************************************************
985 Flush a write cache struct to disk.
986 ********************************************************************/
988 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
990 struct write_cache
*wcp
= fsp
->wcp
;
994 if(!wcp
|| !wcp
->data_size
) {
998 data_size
= wcp
->data_size
;
1002 case SAMBA_SEEK_FLUSH
:
1003 DO_PROFILE_INC(writecache_flush_reason_seek
);
1005 case SAMBA_READ_FLUSH
:
1006 DO_PROFILE_INC(writecache_flush_reason_read
);
1008 case SAMBA_WRITE_FLUSH
:
1009 DO_PROFILE_INC(writecache_flush_reason_write
);;
1011 case SAMBA_READRAW_FLUSH
:
1012 DO_PROFILE_INC(writecache_flush_reason_readraw
);
1014 case SAMBA_OPLOCK_RELEASE_FLUSH
:
1015 DO_PROFILE_INC(writecache_flush_reason_oplock
);
1017 case SAMBA_CLOSE_FLUSH
:
1018 DO_PROFILE_INC(writecache_flush_reason_close
);
1020 case SAMBA_SYNC_FLUSH
:
1021 DO_PROFILE_INC(writecache_flush_reason_sync
);
1023 case SAMBA_SIZECHANGE_FLUSH
:
1024 DO_PROFILE_INC(writecache_flush_reason_sizechange
);
1030 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
1031 fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
1033 if(data_size
== wcp
->alloc_size
) {
1034 DO_PROFILE_INC(writecache_perfect_writes
);
1037 ret
= real_write_file(NULL
, fsp
, wcp
->data
, wcp
->offset
, data_size
);
1040 * Ensure file size if kept up to date if write extends file.
1043 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
)) {
1044 wcp
->file_size
= wcp
->offset
+ ret
;
1050 /*******************************************************************
1052 ********************************************************************/
1054 NTSTATUS
sync_file(connection_struct
*conn
, files_struct
*fsp
, bool write_through
)
1056 if (fsp
->fh
->fd
== -1)
1057 return NT_STATUS_INVALID_HANDLE
;
1059 if (lp_strict_sync(SNUM(conn
)) &&
1060 (lp_sync_always(SNUM(conn
)) || write_through
)) {
1061 int ret
= flush_write_cache(fsp
, SAMBA_SYNC_FLUSH
);
1063 return map_nt_error_from_unix(errno
);
1065 ret
= smb_vfs_fsync_sync(fsp
);
1067 return map_nt_error_from_unix(errno
);
1070 return NT_STATUS_OK
;
1073 /************************************************************
1074 Perform a stat whether a valid fd or not.
1075 ************************************************************/
1077 int fsp_stat(files_struct
*fsp
)
1079 if (fsp
->fh
->fd
== -1) {
1080 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
1081 return SMB_VFS_LSTAT(fsp
->conn
, fsp
->fsp_name
);
1083 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
);
1086 return SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
);