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 update_write_time_handler(struct tevent_context
*ctx
,
164 struct tevent_timer
*te
,
168 files_struct
*fsp
= (files_struct
*)private_data
;
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 /*********************************************************
184 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
186 *********************************************************/
188 void trigger_write_time_update(struct files_struct
*fsp
)
192 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
193 /* Don't use delayed writes on POSIX files. */
197 if (fsp
->write_time_forced
) {
198 /* No point - "sticky" write times
204 /* We need to remember someone did a write
205 * and update to current time on close. */
207 fsp
->update_write_time_on_close
= true;
209 if (fsp
->update_write_time_triggered
) {
211 * We only update the write time after 2 seconds
212 * on the first normal write. After that
213 * no other writes affect this until close.
217 fsp
->update_write_time_triggered
= true;
219 delay
= lp_parm_int(SNUM(fsp
->conn
),
220 "smbd", "writetimeupdatedelay",
221 WRITE_TIME_UPDATE_USEC_DELAY
);
223 DEBUG(5, ("Update write time %d usec later on %s\n",
224 delay
, fsp_str_dbg(fsp
)));
226 /* trigger the update 2 seconds later */
227 fsp
->update_write_time_event
=
228 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, NULL
,
229 timeval_current_ofs_usec(delay
),
230 update_write_time_handler
, fsp
);
233 void trigger_write_time_update_immediate(struct files_struct
*fsp
)
235 struct smb_file_time ft
;
237 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
238 /* Don't use delayed writes on POSIX files. */
242 if (fsp
->write_time_forced
) {
244 * No point - "sticky" write times
250 TALLOC_FREE(fsp
->update_write_time_event
);
251 DEBUG(5, ("Update write time immediate on %s\n",
254 /* After an immediate update, reset the trigger. */
255 fsp
->update_write_time_triggered
= true;
256 fsp
->update_write_time_on_close
= false;
259 ft
.mtime
= timespec_current();
261 /* Update the time in the open file db. */
262 (void)set_write_time(fsp
->file_id
, ft
.mtime
);
264 /* Now set on disk - takes care of notify. */
265 (void)smb_set_file_time(fsp
->conn
, fsp
, fsp
->fsp_name
, &ft
, false);
268 void mark_file_modified(files_struct
*fsp
)
276 fsp
->modified
= true;
278 if (SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
) != 0) {
281 trigger_write_time_update(fsp
);
283 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
286 if (!(lp_store_dos_attributes(SNUM(fsp
->conn
)) ||
287 MAP_ARCHIVE(fsp
->conn
))) {
291 dosmode
= dos_mode(fsp
->conn
, fsp
->fsp_name
);
292 if (IS_DOS_ARCHIVE(dosmode
)) {
295 file_set_dosmode(fsp
->conn
, fsp
->fsp_name
,
296 dosmode
| FILE_ATTRIBUTE_ARCHIVE
, NULL
, false);
299 /****************************************************************************
301 ****************************************************************************/
303 ssize_t
write_file(struct smb_request
*req
,
309 struct write_cache
*wcp
= fsp
->wcp
;
310 ssize_t total_written
= 0;
313 if (fsp
->print_file
) {
317 ret
= print_spool_write(fsp
, data
, n
, pos
, &t
);
325 if (!fsp
->can_write
) {
331 * If this is the first write and we have an exclusive oplock
332 * then setup the write cache.
335 if (!fsp
->modified
&&
336 EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) &&
339 * Note: no write cache with leases!
340 * as the handles would have to share the write cache
341 * that's possible but an improvement for another day...
343 setup_write_cache(fsp
, fsp
->fsp_name
->st
.st_ex_size
);
347 mark_file_modified(fsp
);
349 DO_PROFILE_INC(writecache_total_writes
);
350 if (!fsp
->oplock_type
) {
351 DO_PROFILE_INC(writecache_non_oplock_writes
);
355 * If this file is level II oplocked then we need
356 * to grab the shared memory lock and inform all
357 * other files with a level II lock that they need
358 * to flush their read caches. We keep the lock over
359 * the shared memory area whilst doing this.
362 /* This should actually be improved to span the write. */
363 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
364 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
366 if (wcp
&& req
->unread_bytes
) {
367 /* If we're using receivefile don't
368 * deal with a write cache.
370 flush_write_cache(fsp
, SAMBA_WRITE_FLUSH
);
371 delete_write_cache(fsp
);
376 DO_PROFILE_INC(writecache_direct_writes
);
377 total_written
= real_write_file(req
, fsp
, data
, pos
, n
);
378 return total_written
;
381 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
382 "wcp->data_size=%u\n", fsp_str_dbg(fsp
), fsp
->fh
->fd
,
383 (double)pos
, (unsigned int)n
, (double)wcp
->offset
,
384 (unsigned int)wcp
->data_size
));
386 fsp
->fh
->pos
= pos
+ n
;
388 if ((n
== 1) && (data
[0] == '\0') && (pos
> wcp
->file_size
)) {
392 * This is a 1-byte write of a 0 beyond the EOF and
393 * thus implicitly also beyond the current active
394 * write cache, the typical file-extending (and
395 * allocating, but we're using the write cache here)
396 * write done by Windows. We just have to ftruncate
397 * the file and rely on posix semantics to return
398 * zeros for non-written file data that is within the
401 * We can not use wcp_file_size_change here because we
402 * might have an existing write cache, and
403 * wcp_file_size_change assumes a change to just the
404 * end of the current write cache.
407 wcp
->file_size
= pos
+ 1;
408 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
410 DEBUG(0, ("wcp_file_size_change (%s): ftruncate of "
411 "size %.0f error %s\n", fsp_str_dbg(fsp
),
412 (double)wcp
->file_size
, strerror(errno
)));
420 * If we have active cache and it isn't contiguous then we flush.
421 * NOTE: There is a small problem with running out of disk ....
424 if (wcp
->data_size
) {
425 bool cache_flush_needed
= False
;
427 if ((pos
>= wcp
->offset
) &&
428 (pos
<= wcp
->offset
+ wcp
->data_size
)) {
430 /* ASCII art.... JRA.
432 +--------------+-----
433 | Cached data | Rest of allocated cache buffer....
434 +--------------+-----
436 +-------------------+
438 +-------------------+
443 * Start of write overlaps or abutts the existing data.
448 data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)),
451 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
,
455 * Update the current buffer size with the new data.
458 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
459 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
463 * Update the file size if changed.
466 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
467 if (wcp_file_size_change(fsp
) == -1) {
473 * If we used all the data then
480 cache_flush_needed
= True
;
483 * Move the start of data forward by the amount used,
484 * cut down the amount left by the same amount.
491 DO_PROFILE_INC(writecache_abutted_writes
);
492 total_written
= data_used
;
496 } else if ((pos
< wcp
->offset
) &&
497 (pos
+ n
> wcp
->offset
) &&
498 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
500 /* ASCII art.... JRA.
506 +-------------------+
508 +-------------------+
513 * End of write overlaps the existing data.
516 size_t data_used
= pos
+ n
- wcp
->offset
;
518 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
521 * Update the current buffer size with the new data.
524 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
525 wcp
->data_size
= pos
+ n
- wcp
->offset
;
529 * Update the file size if changed.
532 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
533 if (wcp_file_size_change(fsp
) == -1) {
539 * We don't need to move the start of data, but we
540 * cut down the amount left by the amount used.
546 * We cannot have used all the data here.
549 cache_flush_needed
= True
;
551 DO_PROFILE_INC(writecache_abutted_writes
);
552 total_written
= data_used
;
556 } else if ((pos
>= wcp
->file_size
) &&
557 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
558 (pos
> wcp
->offset
+ wcp
->data_size
) &&
559 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
561 /* ASCII art.... JRA.
565 +---------------+---------------+
566 | Cached data | Cache buffer |
567 +---------------+---------------+
569 +-------------------+
571 +-------------------+
576 * Non-contiguous write part of which fits within
577 * the cache buffer and is extending the file
578 * and the cache contents reflect the current
579 * data up to the current end of the file.
584 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
) {
587 data_used
= wcp
->offset
+wcp
->alloc_size
-pos
;
591 * Fill in the non-continuous area with zeros.
594 memset(wcp
->data
+ wcp
->data_size
, '\0',
595 pos
- (wcp
->offset
+ wcp
->data_size
) );
597 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
,
601 * Update the current buffer size with the new data.
604 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
605 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
609 * Update the file size if changed.
612 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
613 if (wcp_file_size_change(fsp
) == -1) {
619 * If we used all the data then
626 cache_flush_needed
= True
;
630 * Move the start of data forward by the amount used,
631 * cut down the amount left by the same amount.
638 DO_PROFILE_INC(writecache_abutted_writes
);
639 total_written
= data_used
;
643 } else if ( (pos
>= wcp
->file_size
) &&
645 (wcp
->file_size
== wcp
->offset
+ wcp
->data_size
) &&
646 (pos
< wcp
->file_size
+ wcp
->alloc_size
)) {
652 +---------------+---------------+
653 | Cached data | Cache buffer |
654 +---------------+---------------+
656 |<------- allocated size ---------------->|
662 MS-Office seems to do this a lot to determine if
663 there's enough space on the filesystem to write a new
669 +-----------------------+--------+
670 | Zeroed Cached data | 1 Byte |
671 +-----------------------+--------+
674 flush_write_cache(fsp
, SAMBA_WRITE_FLUSH
);
675 wcp
->offset
= wcp
->file_size
;
676 wcp
->data_size
= pos
- wcp
->file_size
+ 1;
677 memset(wcp
->data
, '\0', wcp
->data_size
);
678 memcpy(wcp
->data
+ wcp
->data_size
-1, data
, 1);
681 * Update the file size if changed.
684 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
685 if (wcp_file_size_change(fsp
) == -1) {
694 /* ASCII art..... JRA.
698 +---------------+---------------+
699 | Cached data | Cache buffer |
700 +---------------+---------------+
708 +---------------+---------------+
709 | Cached data | Cache buffer |
710 +---------------+---------------+
712 +-------------------+
714 +-------------------+
718 +---------------+---------------+
719 | Cached data | Cache buffer |
720 +---------------+---------------+
722 +-----------------------------------------------------+
724 +-----------------------------------------------------+
729 * Write is bigger than buffer, or there is no
730 * overlap on the low or high ends.
733 DEBUG(9,("write_file: non cacheable write : fd = %d, "
734 "pos = %.0f, len = %u, "
735 "current cache pos = %.0f len = %u\n",
736 fsp
->fh
->fd
, (double)pos
, (unsigned int)n
,
738 (unsigned int)wcp
->data_size
));
741 * If write would fit in the cache, and is
742 * larger than the data already in the cache,
743 * flush the cache and preferentially copy the
744 * data new data into it. Otherwise just write
748 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
749 cache_flush_needed
= True
;
751 ssize_t ret
= real_write_file(NULL
, fsp
, data
,
755 * If the write overlaps the entire
756 * cache, then discard the current
757 * contents of the cache. Fix from
758 * Rasmus Borup Hansen rbh@math.ku.dk.
761 if ((pos
<= wcp
->offset
) &&
762 (pos
+ n
>= wcp
->offset
+wcp
->data_size
)) {
763 DEBUG(9,("write_file: discarding "
764 "overwritten write cache: "
765 "fd = %d, off=%.0f, "
766 "size=%u\n", fsp
->fh
->fd
,
768 (unsigned)wcp
->data_size
));
772 DO_PROFILE_INC(writecache_direct_writes
);
777 if (pos
+ ret
> wcp
->file_size
) {
778 wcp
->file_size
= pos
+ ret
;
788 if (cache_flush_needed
) {
789 DEBUG(3, ("SAMBA_WRITE_FLUSH:%d: due to noncontinuous "
790 "write: fd = %d, size = %.0f, pos = %.0f, "
791 "n = %u, wcp->offset=%.0f, "
792 "wcp->data_size=%u\n",
793 write_path
, fsp
->fh
->fd
,
794 (double)wcp
->file_size
, (double)pos
,
795 (unsigned int)n
, (double)wcp
->offset
,
796 (unsigned int)wcp
->data_size
));
798 flush_write_cache(fsp
, SAMBA_WRITE_FLUSH
);
803 * If the write request is bigger than the cache
804 * size, write it all out.
807 if (n
> wcp
->alloc_size
) {
808 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
813 if (pos
+ ret
> wcp
->file_size
) {
814 wcp
->file_size
= pos
+ n
;
817 DO_PROFILE_INC(writecache_direct_writes
);
818 return total_written
+ n
;
822 * If there's any data left, cache it.
826 DO_PROFILE_INC(writecache_cached_writes
);
827 if (wcp
->data_size
) {
828 DO_PROFILE_INC(writecache_abutted_writes
);
830 DO_PROFILE_INC(writecache_init_writes
);
833 if ((wcp
->data_size
== 0)
834 && (pos
> wcp
->file_size
)
835 && (pos
+ n
<= wcp
->file_size
+ wcp
->alloc_size
)) {
837 * This is a write completely beyond the
838 * current EOF, but within reach of the write
839 * cache. We expect fill-up writes pretty
840 * soon, so it does not make sense to start
841 * the write cache at the current
842 * offset. These fill-up writes would trigger
843 * separate pwrites or even unnecessary cache
844 * flushes because they overlap if this is a
845 * one-byte allocating write.
847 wcp
->offset
= wcp
->file_size
;
848 wcp
->data_size
= pos
- wcp
->file_size
;
849 memset(wcp
->data
, 0, wcp
->data_size
);
852 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
853 if (wcp
->data_size
== 0) {
859 * Update the file size if changed.
862 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
863 if (wcp_file_size_change(fsp
) == -1) {
867 DEBUG(9, ("wcp->offset = %.0f wcp->data_size = %u cache "
869 (double)wcp
->offset
, (unsigned int)wcp
->data_size
,
873 return total_written
; /* .... that's a write :) */
876 return total_written
;
879 /****************************************************************************
880 Delete the write cache structure.
881 ****************************************************************************/
883 void delete_write_cache(files_struct
*fsp
)
885 struct write_cache
*wcp
;
891 if(!(wcp
= fsp
->wcp
)) {
895 DO_PROFILE_INC(writecache_deallocations
);
896 allocated_write_caches
--;
898 SMB_ASSERT(wcp
->data_size
== 0);
900 SAFE_FREE(wcp
->data
);
903 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
907 /****************************************************************************
908 Setup the write cache structure.
909 ****************************************************************************/
911 static bool setup_write_cache(files_struct
*fsp
, off_t file_size
)
913 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
914 struct write_cache
*wcp
;
916 if (allocated_write_caches
>= MAX_WRITE_CACHES
) {
920 if(alloc_size
== 0 || fsp
->wcp
) {
924 if((wcp
= SMB_MALLOC_P(struct write_cache
)) == NULL
) {
925 DEBUG(0,("setup_write_cache: malloc fail.\n"));
929 wcp
->file_size
= file_size
;
931 wcp
->alloc_size
= alloc_size
;
933 if((wcp
->data
= (char *)SMB_MALLOC(wcp
->alloc_size
)) == NULL
) {
934 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
935 (unsigned int)wcp
->alloc_size
));
940 memset(wcp
->data
, '\0', wcp
->alloc_size
);
943 DO_PROFILE_INC(writecache_allocations
);
944 allocated_write_caches
++;
946 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
947 fsp_str_dbg(fsp
), (unsigned long)wcp
->alloc_size
));
952 /****************************************************************************
953 Cope with a size change.
954 ****************************************************************************/
956 void set_filelen_write_cache(files_struct
*fsp
, off_t file_size
)
959 /* The cache *must* have been flushed before we do this. */
960 if (fsp
->wcp
->data_size
!= 0) {
962 if (asprintf(&msg
, "set_filelen_write_cache: size change "
963 "on file %s with write cache size = %lu\n",
964 fsp
->fsp_name
->base_name
,
965 (unsigned long)fsp
->wcp
->data_size
) != -1) {
968 smb_panic("set_filelen_write_cache");
971 fsp
->wcp
->file_size
= file_size
;
975 /*******************************************************************
976 Flush a write cache struct to disk.
977 ********************************************************************/
979 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
981 struct write_cache
*wcp
= fsp
->wcp
;
985 if(!wcp
|| !wcp
->data_size
) {
989 data_size
= wcp
->data_size
;
993 case SAMBA_SEEK_FLUSH
:
994 DO_PROFILE_INC(writecache_flush_reason_seek
);
996 case SAMBA_READ_FLUSH
:
997 DO_PROFILE_INC(writecache_flush_reason_read
);
999 case SAMBA_WRITE_FLUSH
:
1000 DO_PROFILE_INC(writecache_flush_reason_write
);;
1002 case SAMBA_READRAW_FLUSH
:
1003 DO_PROFILE_INC(writecache_flush_reason_readraw
);
1005 case SAMBA_OPLOCK_RELEASE_FLUSH
:
1006 DO_PROFILE_INC(writecache_flush_reason_oplock
);
1008 case SAMBA_CLOSE_FLUSH
:
1009 DO_PROFILE_INC(writecache_flush_reason_close
);
1011 case SAMBA_SYNC_FLUSH
:
1012 DO_PROFILE_INC(writecache_flush_reason_sync
);
1014 case SAMBA_SIZECHANGE_FLUSH
:
1015 DO_PROFILE_INC(writecache_flush_reason_sizechange
);
1021 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
1022 fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
1024 if(data_size
== wcp
->alloc_size
) {
1025 DO_PROFILE_INC(writecache_perfect_writes
);
1028 ret
= real_write_file(NULL
, fsp
, wcp
->data
, wcp
->offset
, data_size
);
1031 * Ensure file size if kept up to date if write extends file.
1034 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
)) {
1035 wcp
->file_size
= wcp
->offset
+ ret
;
1041 /*******************************************************************
1043 ********************************************************************/
1045 NTSTATUS
sync_file(connection_struct
*conn
, files_struct
*fsp
, bool write_through
)
1047 if (fsp
->fh
->fd
== -1)
1048 return NT_STATUS_INVALID_HANDLE
;
1050 if (lp_strict_sync(SNUM(conn
)) &&
1051 (lp_sync_always(SNUM(conn
)) || write_through
)) {
1052 int ret
= flush_write_cache(fsp
, SAMBA_SYNC_FLUSH
);
1054 return map_nt_error_from_unix(errno
);
1056 ret
= smb_vfs_fsync_sync(fsp
);
1058 return map_nt_error_from_unix(errno
);
1061 return NT_STATUS_OK
;
1064 /************************************************************
1065 Perform a stat whether a valid fd or not.
1066 ************************************************************/
1068 int fsp_stat(files_struct
*fsp
)
1070 if (fsp
->fh
->fd
== -1) {
1071 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
1072 return SMB_VFS_LSTAT(fsp
->conn
, fsp
->fsp_name
);
1074 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
);
1077 return SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
);