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_read_hits
);
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
, 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 ret
= vfs_write_data(req
, fsp
, data
, n
);
122 if (pos
&& lp_strict_allocate(SNUM(fsp
->conn
) &&
124 if (vfs_fill_sparse(fsp
, pos
) == -1) {
128 ret
= vfs_pwrite_data(req
, fsp
, data
, n
, pos
);
131 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
132 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
137 /* Yes - this is correct - writes don't update this. JRA. */
138 /* Found by Samba4 tests. */
140 fsp
->position_information
= fsp
->pos
;
147 /****************************************************************************
148 File size cache change.
149 Updates size on disk but doesn't flush the cache.
150 ****************************************************************************/
152 static int wcp_file_size_change(files_struct
*fsp
)
155 struct write_cache
*wcp
= fsp
->wcp
;
157 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
158 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
160 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
161 "error %s\n", fsp_str_dbg(fsp
),
162 (double)wcp
->file_size
, strerror(errno
)));
167 void update_write_time_handler(struct event_context
*ctx
,
168 struct timed_event
*te
,
172 files_struct
*fsp
= (files_struct
*)private_data
;
174 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp
)));
176 /* change the write time in the open file db. */
177 (void)set_write_time(fsp
->file_id
, timespec_current());
180 notify_fname(fsp
->conn
, NOTIFY_ACTION_MODIFIED
,
181 FILE_NOTIFY_CHANGE_LAST_WRITE
, fsp
->fsp_name
->base_name
);
183 /* Remove the timed event handler. */
184 TALLOC_FREE(fsp
->update_write_time_event
);
187 /*********************************************************
188 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
190 *********************************************************/
192 void trigger_write_time_update(struct files_struct
*fsp
)
196 if (fsp
->posix_open
) {
197 /* Don't use delayed writes on POSIX files. */
201 if (fsp
->write_time_forced
) {
202 /* No point - "sticky" write times
208 /* We need to remember someone did a write
209 * and update to current time on close. */
211 fsp
->update_write_time_on_close
= true;
213 if (fsp
->update_write_time_triggered
) {
215 * We only update the write time after 2 seconds
216 * on the first normal write. After that
217 * no other writes affect this until close.
221 fsp
->update_write_time_triggered
= true;
223 delay
= lp_parm_int(SNUM(fsp
->conn
),
224 "smbd", "writetimeupdatedelay",
225 WRITE_TIME_UPDATE_USEC_DELAY
);
227 DEBUG(5, ("Update write time %d usec later on %s\n",
228 delay
, fsp_str_dbg(fsp
)));
230 /* trigger the update 2 seconds later */
231 fsp
->update_write_time_event
=
232 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, NULL
,
233 timeval_current_ofs_usec(delay
),
234 update_write_time_handler
, fsp
);
237 void trigger_write_time_update_immediate(struct files_struct
*fsp
)
239 struct smb_file_time ft
;
241 if (fsp
->posix_open
) {
242 /* Don't use delayed writes on POSIX files. */
246 if (fsp
->write_time_forced
) {
248 * No point - "sticky" write times
254 TALLOC_FREE(fsp
->update_write_time_event
);
255 DEBUG(5, ("Update write time immediate on %s\n",
258 /* After an immediate update, reset the trigger. */
259 fsp
->update_write_time_triggered
= true;
260 fsp
->update_write_time_on_close
= false;
263 ft
.mtime
= timespec_current();
265 /* Update the time in the open file db. */
266 (void)set_write_time(fsp
->file_id
, ft
.mtime
);
268 /* Now set on disk - takes care of notify. */
269 (void)smb_set_file_time(fsp
->conn
, fsp
, fsp
->fsp_name
, &ft
, false);
272 /****************************************************************************
274 ****************************************************************************/
276 ssize_t
write_file(struct smb_request
*req
,
282 struct write_cache
*wcp
= fsp
->wcp
;
283 ssize_t total_written
= 0;
286 if (fsp
->print_file
) {
290 ret
= print_spool_write(fsp
, data
, n
, pos
, &t
);
298 if (!fsp
->can_write
) {
303 if (!fsp
->modified
) {
304 fsp
->modified
= True
;
306 if (SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
) == 0) {
307 trigger_write_time_update(fsp
);
308 if (!fsp
->posix_open
&&
309 (lp_store_dos_attributes(SNUM(fsp
->conn
)) ||
310 MAP_ARCHIVE(fsp
->conn
))) {
311 int dosmode
= dos_mode(fsp
->conn
, fsp
->fsp_name
);
312 if (!IS_DOS_ARCHIVE(dosmode
)) {
313 file_set_dosmode(fsp
->conn
, fsp
->fsp_name
,
314 dosmode
| FILE_ATTRIBUTE_ARCHIVE
, NULL
, false);
319 * If this is the first write and we have an exclusive oplock then setup
323 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
324 setup_write_cache(fsp
,
325 fsp
->fsp_name
->st
.st_ex_size
);
332 DO_PROFILE_INC(writecache_total_writes
);
333 if (!fsp
->oplock_type
) {
334 DO_PROFILE_INC(writecache_non_oplock_writes
);
339 * If this file is level II oplocked then we need
340 * to grab the shared memory lock and inform all
341 * other files with a level II lock that they need
342 * to flush their read caches. We keep the lock over
343 * the shared memory area whilst doing this.
346 /* This should actually be improved to span the write. */
347 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
348 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
351 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
352 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
353 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
354 profile_p
->writecache_init_writes
,
355 profile_p
->writecache_abutted_writes
,
356 profile_p
->writecache_total_writes
,
357 profile_p
->writecache_non_oplock_writes
,
358 profile_p
->writecache_allocated_write_caches
,
359 profile_p
->writecache_num_write_caches
,
360 profile_p
->writecache_direct_writes
,
361 profile_p
->writecache_num_perfect_writes
,
362 profile_p
->writecache_read_hits
));
364 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
365 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
366 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
367 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
368 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
369 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
370 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
371 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
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
, 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 size %.0f"
420 "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
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
438 /* ASCII art.... JRA.
440 +--------------+-----
441 | Cached data | Rest of allocated cache buffer....
442 +--------------+-----
444 +-------------------+
446 +-------------------+
451 * Start of write overlaps or abutts the existing data.
454 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
456 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
459 * Update the current buffer size with the new data.
462 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
463 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
467 * Update the file size if changed.
470 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
471 if (wcp_file_size_change(fsp
) == -1) {
477 * If we used all the data then
484 cache_flush_needed
= True
;
487 * Move the start of data forward by the amount used,
488 * cut down the amount left by the same amount.
495 DO_PROFILE_INC(writecache_abutted_writes
);
496 total_written
= data_used
;
500 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
501 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
503 /* ASCII art.... JRA.
509 +-------------------+
511 +-------------------+
516 * End of write overlaps the existing data.
519 size_t data_used
= pos
+ n
- wcp
->offset
;
521 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
524 * Update the current buffer size with the new data.
527 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
528 wcp
->data_size
= pos
+ n
- wcp
->offset
;
532 * Update the file size if changed.
535 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
536 if (wcp_file_size_change(fsp
) == -1) {
542 * We don't need to move the start of data, but we
543 * cut down the amount left by the amount used.
549 * We cannot have used all the data here.
552 cache_flush_needed
= True
;
554 DO_PROFILE_INC(writecache_abutted_writes
);
555 total_written
= data_used
;
559 } else if ( (pos
>= wcp
->file_size
) &&
560 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
561 (pos
> wcp
->offset
+ wcp
->data_size
) &&
562 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
564 /* ASCII art.... JRA.
568 +---------------+---------------+
569 | Cached data | Cache buffer |
570 +---------------+---------------+
572 +-------------------+
574 +-------------------+
579 * Non-contiguous write part of which fits within
580 * the cache buffer and is extending the file
581 * and the cache contents reflect the current
582 * data up to the current end of the file.
587 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
) {
590 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
594 * Fill in the non-continuous area with zeros.
597 memset(wcp
->data
+ wcp
->data_size
, '\0',
598 pos
- (wcp
->offset
+ wcp
->data_size
) );
600 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
603 * Update the current buffer size with the new data.
606 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
607 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
611 * Update the file size if changed.
614 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
615 if (wcp_file_size_change(fsp
) == -1) {
621 * If we used all the data then
628 cache_flush_needed
= True
;
632 * Move the start of data forward by the amount used,
633 * cut down the amount left by the same amount.
640 DO_PROFILE_INC(writecache_abutted_writes
);
641 total_written
= data_used
;
645 } else if ( (pos
>= wcp
->file_size
) &&
647 (wcp
->file_size
== wcp
->offset
+ wcp
->data_size
) &&
648 (pos
< wcp
->file_size
+ wcp
->alloc_size
)) {
654 +---------------+---------------+
655 | Cached data | Cache buffer |
656 +---------------+---------------+
658 |<------- allocated size ---------------->|
664 MS-Office seems to do this a lot to determine if there's enough
665 space on the filesystem to write a new file.
670 +-----------------------+--------+
671 | Zeroed Cached data | 1 Byte |
672 +-----------------------+--------+
675 flush_write_cache(fsp
, WRITE_FLUSH
);
676 wcp
->offset
= wcp
->file_size
;
677 wcp
->data_size
= pos
- wcp
->file_size
+ 1;
678 memset(wcp
->data
, '\0', wcp
->data_size
);
679 memcpy(wcp
->data
+ wcp
->data_size
-1, data
, 1);
682 * Update the file size if changed.
685 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
686 if (wcp_file_size_change(fsp
) == -1) {
695 /* ASCII art..... JRA.
699 +---------------+---------------+
700 | Cached data | Cache buffer |
701 +---------------+---------------+
703 +-------------------+
705 +-------------------+
709 +---------------+---------------+
710 | Cached data | Cache buffer |
711 +---------------+---------------+
713 +-------------------+
715 +-------------------+
719 +---------------+---------------+
720 | Cached data | Cache buffer |
721 +---------------+---------------+
723 +-----------------------------------------------------+
725 +-----------------------------------------------------+
730 * Write is bigger than buffer, or there is no overlap on the
734 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
735 len = %u\n",fsp
->fh
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
738 * If write would fit in the cache, and is larger than
739 * the data already in the cache, flush the cache and
740 * preferentially copy the data new data into it. Otherwise
741 * just write the data directly.
744 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
745 cache_flush_needed
= True
;
747 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
750 * If the write overlaps the entire cache, then
751 * discard the current contents of the cache.
752 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
755 if ((pos
<= wcp
->offset
) &&
756 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
757 DEBUG(9,("write_file: discarding overwritten write \
758 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
762 DO_PROFILE_INC(writecache_direct_writes
);
767 if (pos
+ ret
> wcp
->file_size
) {
768 wcp
->file_size
= pos
+ ret
;
778 if (cache_flush_needed
) {
779 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
780 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
781 write_path
, fsp
->fh
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
782 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
784 flush_write_cache(fsp
, WRITE_FLUSH
);
789 * If the write request is bigger than the cache
790 * size, write it all out.
793 if (n
> wcp
->alloc_size
) {
794 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
799 if (pos
+ ret
> wcp
->file_size
) {
800 wcp
->file_size
= pos
+ n
;
803 DO_PROFILE_INC(writecache_direct_writes
);
804 return total_written
+ n
;
808 * If there's any data left, cache it.
813 if (wcp
->data_size
) {
814 DO_PROFILE_INC(writecache_abutted_writes
);
816 DO_PROFILE_INC(writecache_init_writes
);
820 if ((wcp
->data_size
== 0)
821 && (pos
> wcp
->file_size
)
822 && (pos
+ n
<= wcp
->file_size
+ wcp
->alloc_size
)) {
824 * This is a write completely beyond the
825 * current EOF, but within reach of the write
826 * cache. We expect fill-up writes pretty
827 * soon, so it does not make sense to start
828 * the write cache at the current
829 * offset. These fill-up writes would trigger
830 * separate pwrites or even unnecessary cache
831 * flushes because they overlap if this is a
832 * one-byte allocating write.
834 wcp
->offset
= wcp
->file_size
;
835 wcp
->data_size
= pos
- wcp
->file_size
;
836 memset(wcp
->data
, 0, wcp
->data_size
);
839 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
840 if (wcp
->data_size
== 0) {
842 DO_PROFILE_INC(writecache_num_write_caches
);
847 * Update the file size if changed.
850 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
851 if (wcp_file_size_change(fsp
) == -1) {
855 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
856 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
859 return total_written
; /* .... that's a write :) */
862 return total_written
;
865 /****************************************************************************
866 Delete the write cache structure.
867 ****************************************************************************/
869 void delete_write_cache(files_struct
*fsp
)
871 struct write_cache
*wcp
;
877 if(!(wcp
= fsp
->wcp
)) {
881 DO_PROFILE_DEC(writecache_allocated_write_caches
);
882 allocated_write_caches
--;
884 SMB_ASSERT(wcp
->data_size
== 0);
886 SAFE_FREE(wcp
->data
);
889 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
893 /****************************************************************************
894 Setup the write cache structure.
895 ****************************************************************************/
897 static bool setup_write_cache(files_struct
*fsp
, off_t file_size
)
899 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
900 struct write_cache
*wcp
;
902 if (allocated_write_caches
>= MAX_WRITE_CACHES
) {
906 if(alloc_size
== 0 || fsp
->wcp
) {
910 if((wcp
= SMB_MALLOC_P(struct write_cache
)) == NULL
) {
911 DEBUG(0,("setup_write_cache: malloc fail.\n"));
915 wcp
->file_size
= file_size
;
917 wcp
->alloc_size
= alloc_size
;
919 if((wcp
->data
= (char *)SMB_MALLOC(wcp
->alloc_size
)) == NULL
) {
920 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
921 (unsigned int)wcp
->alloc_size
));
926 memset(wcp
->data
, '\0', wcp
->alloc_size
);
929 DO_PROFILE_INC(writecache_allocated_write_caches
);
930 allocated_write_caches
++;
932 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
933 fsp_str_dbg(fsp
), (unsigned long)wcp
->alloc_size
));
938 /****************************************************************************
939 Cope with a size change.
940 ****************************************************************************/
942 void set_filelen_write_cache(files_struct
*fsp
, off_t file_size
)
945 /* The cache *must* have been flushed before we do this. */
946 if (fsp
->wcp
->data_size
!= 0) {
948 if (asprintf(&msg
, "set_filelen_write_cache: size change "
949 "on file %s with write cache size = %lu\n",
950 fsp
->fsp_name
->base_name
,
951 (unsigned long)fsp
->wcp
->data_size
) != -1) {
954 smb_panic("set_filelen_write_cache");
957 fsp
->wcp
->file_size
= file_size
;
961 /*******************************************************************
962 Flush a write cache struct to disk.
963 ********************************************************************/
965 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
967 struct write_cache
*wcp
= fsp
->wcp
;
971 if(!wcp
|| !wcp
->data_size
) {
975 data_size
= wcp
->data_size
;
978 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
980 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
981 fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
984 if(data_size
== wcp
->alloc_size
) {
985 DO_PROFILE_INC(writecache_num_perfect_writes
);
989 ret
= real_write_file(NULL
, fsp
, wcp
->data
, wcp
->offset
, data_size
);
992 * Ensure file size if kept up to date if write extends file.
995 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
)) {
996 wcp
->file_size
= wcp
->offset
+ ret
;
1002 /*******************************************************************
1004 ********************************************************************/
1006 NTSTATUS
sync_file(connection_struct
*conn
, files_struct
*fsp
, bool write_through
)
1008 if (fsp
->fh
->fd
== -1)
1009 return NT_STATUS_INVALID_HANDLE
;
1011 if (lp_strict_sync(SNUM(conn
)) &&
1012 (lp_syncalways(SNUM(conn
)) || write_through
)) {
1013 int ret
= flush_write_cache(fsp
, SYNC_FLUSH
);
1015 return map_nt_error_from_unix(errno
);
1017 ret
= SMB_VFS_FSYNC(fsp
);
1019 return map_nt_error_from_unix(errno
);
1022 return NT_STATUS_OK
;
1025 /************************************************************
1026 Perform a stat whether a valid fd or not.
1027 ************************************************************/
1029 int fsp_stat(files_struct
*fsp
)
1031 if (fsp
->fh
->fd
== -1) {
1032 if (fsp
->posix_open
) {
1033 return SMB_VFS_LSTAT(fsp
->conn
, fsp
->fsp_name
);
1035 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
);
1038 return SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
);