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/globals.h"
26 static bool setup_write_cache(files_struct
*, SMB_OFF_T
);
28 /****************************************************************************
29 Read from write cache if we can.
30 ****************************************************************************/
32 static bool read_from_write_cache(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
34 write_cache
*wcp
= fsp
->wcp
;
40 if( n
> wcp
->data_size
|| pos
< wcp
->offset
|| pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
44 memcpy(data
, wcp
->data
+ (pos
- wcp
->offset
), n
);
46 DO_PROFILE_INC(writecache_read_hits
);
51 /****************************************************************************
53 ****************************************************************************/
55 ssize_t
read_file(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
57 ssize_t ret
=0,readret
;
59 /* you can't read from print files */
60 if (fsp
->print_file
) {
66 * Serve from write cache if we can.
69 if(read_from_write_cache(fsp
, data
, pos
, n
)) {
70 fsp
->fh
->pos
= pos
+ n
;
71 fsp
->fh
->position_information
= fsp
->fh
->pos
;
75 flush_write_cache(fsp
, READ_FLUSH
);
83 readret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
86 if ((errno
== EAGAIN
) && numretries
) {
87 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
94 #else /* NO DMF fix. */
95 readret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
106 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
107 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
110 fsp
->fh
->position_information
= fsp
->fh
->pos
;
115 /****************************************************************************
116 *Really* write to a file.
117 ****************************************************************************/
119 static ssize_t
real_write_file(struct smb_request
*req
,
128 ret
= vfs_write_data(req
, fsp
, data
, n
);
131 if (pos
&& lp_strict_allocate(SNUM(fsp
->conn
) &&
133 if (vfs_fill_sparse(fsp
, pos
) == -1) {
137 ret
= vfs_pwrite_data(req
, fsp
, data
, n
, pos
);
140 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
141 fsp_str_dbg(fsp
), (double)pos
, (unsigned long)n
, (long)ret
));
146 /* Yes - this is correct - writes don't update this. JRA. */
147 /* Found by Samba4 tests. */
149 fsp
->position_information
= fsp
->pos
;
156 /****************************************************************************
157 File size cache change.
158 Updates size on disk but doesn't flush the cache.
159 ****************************************************************************/
161 static int wcp_file_size_change(files_struct
*fsp
)
164 write_cache
*wcp
= fsp
->wcp
;
166 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
167 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
169 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
170 "error %s\n", fsp_str_dbg(fsp
),
171 (double)wcp
->file_size
, strerror(errno
)));
176 void update_write_time_handler(struct event_context
*ctx
,
177 struct timed_event
*te
,
181 files_struct
*fsp
= (files_struct
*)private_data
;
183 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp
)));
185 /* change the write time in the open file db. */
186 (void)set_write_time(fsp
->file_id
, timespec_current());
189 notify_fname(fsp
->conn
, NOTIFY_ACTION_MODIFIED
,
190 FILE_NOTIFY_CHANGE_LAST_WRITE
, fsp
->fsp_name
->base_name
);
192 /* Remove the timed event handler. */
193 TALLOC_FREE(fsp
->update_write_time_event
);
196 /*********************************************************
197 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
199 *********************************************************/
201 void trigger_write_time_update(struct files_struct
*fsp
)
205 if (fsp
->posix_open
) {
206 /* Don't use delayed writes on POSIX files. */
210 if (fsp
->write_time_forced
) {
211 /* No point - "sticky" write times
217 /* We need to remember someone did a write
218 * and update to current time on close. */
220 fsp
->update_write_time_on_close
= true;
222 if (fsp
->update_write_time_triggered
) {
224 * We only update the write time after 2 seconds
225 * on the first normal write. After that
226 * no other writes affect this until close.
230 fsp
->update_write_time_triggered
= true;
232 delay
= lp_parm_int(SNUM(fsp
->conn
),
233 "smbd", "writetimeupdatedelay",
234 WRITE_TIME_UPDATE_USEC_DELAY
);
236 DEBUG(5, ("Update write time %d usec later on %s\n",
237 delay
, fsp_str_dbg(fsp
)));
239 /* trigger the update 2 seconds later */
240 fsp
->update_write_time_event
=
241 event_add_timed(smbd_event_context(), NULL
,
242 timeval_current_ofs(0, delay
),
243 update_write_time_handler
, fsp
);
246 void trigger_write_time_update_immediate(struct files_struct
*fsp
)
248 struct smb_file_time ft
;
250 if (fsp
->posix_open
) {
251 /* Don't use delayed writes on POSIX files. */
255 if (fsp
->write_time_forced
) {
257 * No point - "sticky" write times
263 TALLOC_FREE(fsp
->update_write_time_event
);
264 DEBUG(5, ("Update write time immediate on %s\n",
267 /* After an immediate update, reset the trigger. */
268 fsp
->update_write_time_triggered
= true;
269 fsp
->update_write_time_on_close
= false;
272 ft
.mtime
= timespec_current();
274 /* Update the time in the open file db. */
275 (void)set_write_time(fsp
->file_id
, ft
.mtime
);
277 /* Now set on disk - takes care of notify. */
278 (void)smb_set_file_time(fsp
->conn
, fsp
, fsp
->fsp_name
, &ft
, false);
281 /****************************************************************************
283 ****************************************************************************/
285 ssize_t
write_file(struct smb_request
*req
,
291 write_cache
*wcp
= fsp
->wcp
;
292 ssize_t total_written
= 0;
295 if (fsp
->print_file
) {
299 ret
= print_spool_write(fsp
, data
, n
, pos
, &t
);
307 if (!fsp
->can_write
) {
312 if (!fsp
->modified
) {
313 fsp
->modified
= True
;
315 if (SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
) == 0) {
316 trigger_write_time_update(fsp
);
317 if (!fsp
->posix_open
&&
318 (lp_store_dos_attributes(SNUM(fsp
->conn
)) ||
319 MAP_ARCHIVE(fsp
->conn
))) {
320 int dosmode
= dos_mode(fsp
->conn
, fsp
->fsp_name
);
321 if (!IS_DOS_ARCHIVE(dosmode
)) {
322 file_set_dosmode(fsp
->conn
, fsp
->fsp_name
,
323 dosmode
| aARCH
, NULL
, false);
328 * If this is the first write and we have an exclusive oplock then setup
332 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
333 setup_write_cache(fsp
,
334 fsp
->fsp_name
->st
.st_ex_size
);
341 DO_PROFILE_INC(writecache_total_writes
);
342 if (!fsp
->oplock_type
) {
343 DO_PROFILE_INC(writecache_non_oplock_writes
);
348 * If this file is level II oplocked then we need
349 * to grab the shared memory lock and inform all
350 * other files with a level II lock that they need
351 * to flush their read caches. We keep the lock over
352 * the shared memory area whilst doing this.
355 /* This should actually be improved to span the write. */
356 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
357 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
360 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
361 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
362 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
363 profile_p
->writecache_init_writes
,
364 profile_p
->writecache_abutted_writes
,
365 profile_p
->writecache_total_writes
,
366 profile_p
->writecache_non_oplock_writes
,
367 profile_p
->writecache_allocated_write_caches
,
368 profile_p
->writecache_num_write_caches
,
369 profile_p
->writecache_direct_writes
,
370 profile_p
->writecache_num_perfect_writes
,
371 profile_p
->writecache_read_hits
));
373 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
374 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
375 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
376 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
377 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
378 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
379 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
380 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
384 if (wcp
&& req
->unread_bytes
) {
385 /* If we're using receivefile don't
386 * deal with a write cache.
388 flush_write_cache(fsp
, WRITE_FLUSH
);
389 delete_write_cache(fsp
);
394 DO_PROFILE_INC(writecache_direct_writes
);
395 total_written
= real_write_file(req
, fsp
, data
, pos
, n
);
396 return total_written
;
399 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
400 "wcp->data_size=%u\n", fsp_str_dbg(fsp
), fsp
->fh
->fd
,
401 (double)pos
, (unsigned int)n
, (double)wcp
->offset
,
402 (unsigned int)wcp
->data_size
));
404 fsp
->fh
->pos
= pos
+ n
;
406 if ((n
== 1) && (data
[0] == '\0') && (pos
> wcp
->file_size
)) {
410 * This is a 1-byte write of a 0 beyond the EOF and
411 * thus implicitly also beyond the current active
412 * write cache, the typical file-extending (and
413 * allocating, but we're using the write cache here)
414 * write done by Windows. We just have to ftruncate
415 * the file and rely on posix semantics to return
416 * zeros for non-written file data that is within the
419 * We can not use wcp_file_size_change here because we
420 * might have an existing write cache, and
421 * wcp_file_size_change assumes a change to just the
422 * end of the current write cache.
425 wcp
->file_size
= pos
+ 1;
426 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
428 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f"
429 "error %s\n", fsp_str_dbg(fsp
),
430 (double)wcp
->file_size
, strerror(errno
)));
438 * If we have active cache and it isn't contiguous then we flush.
439 * NOTE: There is a small problem with running out of disk ....
442 if (wcp
->data_size
) {
443 bool cache_flush_needed
= False
;
445 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
447 /* ASCII art.... JRA.
449 +--------------+-----
450 | Cached data | Rest of allocated cache buffer....
451 +--------------+-----
453 +-------------------+
455 +-------------------+
460 * Start of write overlaps or abutts the existing data.
463 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
465 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
468 * Update the current buffer size with the new data.
471 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
472 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
476 * Update the file size if changed.
479 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
480 if (wcp_file_size_change(fsp
) == -1) {
486 * If we used all the data then
493 cache_flush_needed
= True
;
496 * Move the start of data forward by the amount used,
497 * cut down the amount left by the same amount.
504 DO_PROFILE_INC(writecache_abutted_writes
);
505 total_written
= data_used
;
509 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
510 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
512 /* ASCII art.... JRA.
518 +-------------------+
520 +-------------------+
525 * End of write overlaps the existing data.
528 size_t data_used
= pos
+ n
- wcp
->offset
;
530 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
533 * Update the current buffer size with the new data.
536 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
537 wcp
->data_size
= pos
+ n
- wcp
->offset
;
541 * Update the file size if changed.
544 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
545 if (wcp_file_size_change(fsp
) == -1) {
551 * We don't need to move the start of data, but we
552 * cut down the amount left by the amount used.
558 * We cannot have used all the data here.
561 cache_flush_needed
= True
;
563 DO_PROFILE_INC(writecache_abutted_writes
);
564 total_written
= data_used
;
568 } else if ( (pos
>= wcp
->file_size
) &&
569 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
570 (pos
> wcp
->offset
+ wcp
->data_size
) &&
571 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
573 /* ASCII art.... JRA.
577 +---------------+---------------+
578 | Cached data | Cache buffer |
579 +---------------+---------------+
581 +-------------------+
583 +-------------------+
588 * Non-contiguous write part of which fits within
589 * the cache buffer and is extending the file
590 * and the cache contents reflect the current
591 * data up to the current end of the file.
596 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
) {
599 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
603 * Fill in the non-continuous area with zeros.
606 memset(wcp
->data
+ wcp
->data_size
, '\0',
607 pos
- (wcp
->offset
+ wcp
->data_size
) );
609 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
612 * Update the current buffer size with the new data.
615 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
616 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
620 * Update the file size if changed.
623 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
624 if (wcp_file_size_change(fsp
) == -1) {
630 * If we used all the data then
637 cache_flush_needed
= True
;
641 * Move the start of data forward by the amount used,
642 * cut down the amount left by the same amount.
649 DO_PROFILE_INC(writecache_abutted_writes
);
650 total_written
= data_used
;
654 } else if ( (pos
>= wcp
->file_size
) &&
656 (wcp
->file_size
== wcp
->offset
+ wcp
->data_size
) &&
657 (pos
< wcp
->file_size
+ wcp
->alloc_size
)) {
663 +---------------+---------------+
664 | Cached data | Cache buffer |
665 +---------------+---------------+
667 |<------- allocated size ---------------->|
673 MS-Office seems to do this a lot to determine if there's enough
674 space on the filesystem to write a new file.
679 +-----------------------+--------+
680 | Zeroed Cached data | 1 Byte |
681 +-----------------------+--------+
684 flush_write_cache(fsp
, WRITE_FLUSH
);
685 wcp
->offset
= wcp
->file_size
;
686 wcp
->data_size
= pos
- wcp
->file_size
+ 1;
687 memset(wcp
->data
, '\0', wcp
->data_size
);
688 memcpy(wcp
->data
+ wcp
->data_size
-1, data
, 1);
691 * Update the file size if changed.
694 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
695 if (wcp_file_size_change(fsp
) == -1) {
704 /* ASCII art..... JRA.
708 +---------------+---------------+
709 | Cached data | Cache buffer |
710 +---------------+---------------+
712 +-------------------+
714 +-------------------+
718 +---------------+---------------+
719 | Cached data | Cache buffer |
720 +---------------+---------------+
722 +-------------------+
724 +-------------------+
728 +---------------+---------------+
729 | Cached data | Cache buffer |
730 +---------------+---------------+
732 +-----------------------------------------------------+
734 +-----------------------------------------------------+
739 * Write is bigger than buffer, or there is no overlap on the
743 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
744 len = %u\n",fsp
->fh
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
747 * If write would fit in the cache, and is larger than
748 * the data already in the cache, flush the cache and
749 * preferentially copy the data new data into it. Otherwise
750 * just write the data directly.
753 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
754 cache_flush_needed
= True
;
756 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
759 * If the write overlaps the entire cache, then
760 * discard the current contents of the cache.
761 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
764 if ((pos
<= wcp
->offset
) &&
765 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
766 DEBUG(9,("write_file: discarding overwritten write \
767 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
771 DO_PROFILE_INC(writecache_direct_writes
);
776 if (pos
+ ret
> wcp
->file_size
) {
777 wcp
->file_size
= pos
+ ret
;
787 if (cache_flush_needed
) {
788 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
789 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
790 write_path
, fsp
->fh
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
791 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
793 flush_write_cache(fsp
, WRITE_FLUSH
);
798 * If the write request is bigger than the cache
799 * size, write it all out.
802 if (n
> wcp
->alloc_size
) {
803 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
808 if (pos
+ ret
> wcp
->file_size
) {
809 wcp
->file_size
= pos
+ n
;
812 DO_PROFILE_INC(writecache_direct_writes
);
813 return total_written
+ n
;
817 * If there's any data left, cache it.
822 if (wcp
->data_size
) {
823 DO_PROFILE_INC(writecache_abutted_writes
);
825 DO_PROFILE_INC(writecache_init_writes
);
829 if ((wcp
->data_size
== 0)
830 && (pos
> wcp
->file_size
)
831 && (pos
+ n
<= wcp
->file_size
+ wcp
->alloc_size
)) {
833 * This is a write completely beyond the
834 * current EOF, but within reach of the write
835 * cache. We expect fill-up writes pretty
836 * soon, so it does not make sense to start
837 * the write cache at the current
838 * offset. These fill-up writes would trigger
839 * separate pwrites or even unnecessary cache
840 * flushes because they overlap if this is a
841 * one-byte allocating write.
843 wcp
->offset
= wcp
->file_size
;
844 wcp
->data_size
= pos
- wcp
->file_size
;
845 memset(wcp
->data
, 0, wcp
->data_size
);
848 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
849 if (wcp
->data_size
== 0) {
851 DO_PROFILE_INC(writecache_num_write_caches
);
856 * Update the file size if changed.
859 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
860 if (wcp_file_size_change(fsp
) == -1) {
864 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
865 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
868 return total_written
; /* .... that's a write :) */
871 return total_written
;
874 /****************************************************************************
875 Delete the write cache structure.
876 ****************************************************************************/
878 void delete_write_cache(files_struct
*fsp
)
886 if(!(wcp
= fsp
->wcp
)) {
890 DO_PROFILE_DEC(writecache_allocated_write_caches
);
891 allocated_write_caches
--;
893 SMB_ASSERT(wcp
->data_size
== 0);
895 SAFE_FREE(wcp
->data
);
898 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
902 /****************************************************************************
903 Setup the write cache structure.
904 ****************************************************************************/
906 static bool setup_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
908 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
911 if (allocated_write_caches
>= MAX_WRITE_CACHES
) {
915 if(alloc_size
== 0 || fsp
->wcp
) {
919 if((wcp
= SMB_MALLOC_P(write_cache
)) == NULL
) {
920 DEBUG(0,("setup_write_cache: malloc fail.\n"));
924 wcp
->file_size
= file_size
;
926 wcp
->alloc_size
= alloc_size
;
928 if((wcp
->data
= (char *)SMB_MALLOC(wcp
->alloc_size
)) == NULL
) {
929 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
930 (unsigned int)wcp
->alloc_size
));
935 memset(wcp
->data
, '\0', wcp
->alloc_size
);
938 DO_PROFILE_INC(writecache_allocated_write_caches
);
939 allocated_write_caches
++;
941 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
942 fsp_str_dbg(fsp
), (unsigned long)wcp
->alloc_size
));
947 /****************************************************************************
948 Cope with a size change.
949 ****************************************************************************/
951 void set_filelen_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
954 /* The cache *must* have been flushed before we do this. */
955 if (fsp
->wcp
->data_size
!= 0) {
957 if (asprintf(&msg
, "set_filelen_write_cache: size change "
958 "on file %s with write cache size = %lu\n",
959 fsp
->fsp_name
->base_name
,
960 (unsigned long)fsp
->wcp
->data_size
) != -1) {
963 smb_panic("set_filelen_write_cache");
966 fsp
->wcp
->file_size
= file_size
;
970 /*******************************************************************
971 Flush a write cache struct to disk.
972 ********************************************************************/
974 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
976 write_cache
*wcp
= fsp
->wcp
;
980 if(!wcp
|| !wcp
->data_size
) {
984 data_size
= wcp
->data_size
;
987 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
989 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
990 fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
993 if(data_size
== wcp
->alloc_size
) {
994 DO_PROFILE_INC(writecache_num_perfect_writes
);
998 ret
= real_write_file(NULL
, fsp
, wcp
->data
, wcp
->offset
, data_size
);
1001 * Ensure file size if kept up to date if write extends file.
1004 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
)) {
1005 wcp
->file_size
= wcp
->offset
+ ret
;
1011 /*******************************************************************
1013 ********************************************************************/
1015 NTSTATUS
sync_file(connection_struct
*conn
, files_struct
*fsp
, bool write_through
)
1017 if (fsp
->fh
->fd
== -1)
1018 return NT_STATUS_INVALID_HANDLE
;
1020 if (lp_strict_sync(SNUM(conn
)) &&
1021 (lp_syncalways(SNUM(conn
)) || write_through
)) {
1022 int ret
= flush_write_cache(fsp
, SYNC_FLUSH
);
1024 return map_nt_error_from_unix(errno
);
1026 ret
= SMB_VFS_FSYNC(fsp
);
1028 return map_nt_error_from_unix(errno
);
1031 return NT_STATUS_OK
;
1034 /************************************************************
1035 Perform a stat whether a valid fd or not.
1036 ************************************************************/
1038 int fsp_stat(files_struct
*fsp
)
1040 if (fsp
->fh
->fd
== -1) {
1041 if (fsp
->posix_open
) {
1042 return SMB_VFS_LSTAT(fsp
->conn
, fsp
->fsp_name
);
1044 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
);
1047 return SMB_VFS_FSTAT(fsp
, &fsp
->fsp_name
->st
);