traffic_packets: provision request data for packet_drsuapi_13
[Samba.git] / source3 / smbd / fileio.c
blob1c8bb41620c869b196587ba7815941563209b2d8
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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/>.
22 #include "includes.h"
23 #include "printing.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "smbprofile.h"
28 struct write_cache {
29 off_t file_size;
30 off_t offset;
31 size_t alloc_size;
32 size_t data_size;
33 char *data;
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;
46 if(!wcp) {
47 return False;
50 if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
51 return False;
54 memcpy(data, wcp->data + (pos - wcp->offset), n);
56 DO_PROFILE_INC(writecache_cached_reads);
58 return True;
61 /****************************************************************************
62 Read from a file.
63 ****************************************************************************/
65 ssize_t read_file(files_struct *fsp,char *data,off_t pos,size_t n)
67 ssize_t ret = 0;
69 /* you can't read from print files */
70 if (fsp->print_file) {
71 errno = EBADF;
72 return -1;
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;
82 return n;
85 flush_write_cache(fsp, SAMBA_READ_FLUSH);
87 fsp->fh->pos = pos;
89 if (n > 0) {
90 ret = SMB_VFS_PREAD(fsp,data,n,pos);
92 if (ret == -1) {
93 return -1;
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));
100 fsp->fh->pos += ret;
101 fsp->fh->position_information = fsp->fh->pos;
103 return(ret);
106 /****************************************************************************
107 *Really* write to a file.
108 ****************************************************************************/
110 static ssize_t real_write_file(struct smb_request *req,
111 files_struct *fsp,
112 const char *data,
113 off_t pos,
114 size_t n)
116 ssize_t ret;
118 fsp->fh->pos = pos;
119 if (pos && lp_strict_allocate(SNUM(fsp->conn) &&
120 !fsp->is_sparse)) {
121 if (vfs_fill_sparse(fsp, pos) == -1) {
122 return -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));
130 if (ret != -1) {
131 fsp->fh->pos += ret;
133 /* Yes - this is correct - writes don't update this. JRA. */
134 /* Found by Samba4 tests. */
135 #if 0
136 fsp->position_information = fsp->pos;
137 #endif
140 return ret;
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)
150 int ret;
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);
155 if (ret == -1) {
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)));
160 return ret;
163 void update_write_time_handler(struct tevent_context *ctx,
164 struct tevent_timer *te,
165 struct timeval now,
166 void *private_data)
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());
175 /* And notify. */
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
185 in the future.
186 *********************************************************/
188 void trigger_write_time_update(struct files_struct *fsp)
190 int delay;
192 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
193 /* Don't use delayed writes on POSIX files. */
194 return;
197 if (fsp->write_time_forced) {
198 /* No point - "sticky" write times
199 * in effect.
201 return;
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.
215 return;
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. */
239 return;
242 if (fsp->write_time_forced) {
244 * No point - "sticky" write times
245 * in effect.
247 return;
250 TALLOC_FREE(fsp->update_write_time_event);
251 DEBUG(5, ("Update write time immediate on %s\n",
252 fsp_str_dbg(fsp)));
254 /* After an immediate update, reset the trigger. */
255 fsp->update_write_time_triggered = true;
256 fsp->update_write_time_on_close = false;
258 ZERO_STRUCT(ft);
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)
270 int dosmode;
272 if (fsp->modified) {
273 return;
276 fsp->modified = true;
278 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
279 return;
281 trigger_write_time_update(fsp);
283 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
284 return;
286 if (!(lp_store_dos_attributes(SNUM(fsp->conn)) ||
287 MAP_ARCHIVE(fsp->conn))) {
288 return;
291 dosmode = dos_mode(fsp->conn, fsp->fsp_name);
292 if (IS_DOS_ARCHIVE(dosmode)) {
293 return;
295 file_set_dosmode(fsp->conn, fsp->fsp_name,
296 dosmode | FILE_ATTRIBUTE_ARCHIVE, NULL, false);
299 /****************************************************************************
300 Write to a file.
301 ****************************************************************************/
303 ssize_t write_file(struct smb_request *req,
304 files_struct *fsp,
305 const char *data,
306 off_t pos,
307 size_t n)
309 struct write_cache *wcp = fsp->wcp;
310 ssize_t total_written = 0;
311 int write_path = -1;
313 if (fsp->print_file) {
314 uint32_t t;
315 int ret;
317 ret = print_spool_write(fsp, data, n, pos, &t);
318 if (ret) {
319 errno = ret;
320 return -1;
322 return t;
325 if (!fsp->can_write) {
326 errno = EPERM;
327 return -1;
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) &&
337 (wcp == NULL)) {
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);
344 wcp = fsp->wcp;
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);
372 wcp = NULL;
375 if(!wcp) {
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)) {
389 int ret;
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
399 * file length.
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);
409 if (ret == -1) {
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)));
413 return -1;
415 return 1;
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 +-------------------+
437 | Data to write |
438 +-------------------+
443 * Start of write overlaps or abutts the existing data.
446 size_t data_used;
448 data_used = MIN((wcp->alloc_size - (pos - wcp->offset)),
451 memcpy(wcp->data + (pos - wcp->offset), data,
452 data_used);
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) {
468 return -1;
473 * If we used all the data then
474 * return here.
477 if(n == data_used) {
478 return n;
479 } else {
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.
487 data += data_used;
488 pos += data_used;
489 n -= data_used;
491 DO_PROFILE_INC(writecache_abutted_writes);
492 total_written = data_used;
494 write_path = 1;
496 } else if ((pos < wcp->offset) &&
497 (pos + n > wcp->offset) &&
498 (pos + n <= wcp->offset + wcp->alloc_size)) {
500 /* ASCII art.... JRA.
502 +---------------+
503 | Cache buffer |
504 +---------------+
506 +-------------------+
507 | Data to write |
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) {
534 return -1;
539 * We don't need to move the start of data, but we
540 * cut down the amount left by the amount used.
543 n -= data_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;
554 write_path = 2;
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.
563 End of file ---->|
565 +---------------+---------------+
566 | Cached data | Cache buffer |
567 +---------------+---------------+
569 +-------------------+
570 | Data to write |
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.
582 size_t data_used;
584 if(pos + n <= wcp->offset + wcp->alloc_size) {
585 data_used = n;
586 } else {
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,
598 data_used);
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) {
614 return -1;
619 * If we used all the data then
620 * return here.
623 if(n == data_used) {
624 return n;
625 } else {
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.
634 data += data_used;
635 pos += data_used;
636 n -= data_used;
638 DO_PROFILE_INC(writecache_abutted_writes);
639 total_written = data_used;
641 write_path = 3;
643 } else if ( (pos >= wcp->file_size) &&
644 (n == 1) &&
645 (wcp->file_size == wcp->offset + wcp->data_size) &&
646 (pos < wcp->file_size + wcp->alloc_size)) {
650 End of file ---->|
652 +---------------+---------------+
653 | Cached data | Cache buffer |
654 +---------------+---------------+
656 |<------- allocated size ---------------->|
658 +--------+
659 | 1 Byte |
660 +--------+
662 MS-Office seems to do this a lot to determine if
663 there's enough space on the filesystem to write a new
664 file.
666 Change to :
668 End of file ---->|
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) {
686 return -1;
690 return n;
692 } else {
694 /* ASCII art..... JRA.
696 Case 1).
698 +---------------+---------------+
699 | Cached data | Cache buffer |
700 +---------------+---------------+
702 +---------------+
703 | Data to write |
704 +---------------+
706 Case 2).
708 +---------------+---------------+
709 | Cached data | Cache buffer |
710 +---------------+---------------+
712 +-------------------+
713 | Data to write |
714 +-------------------+
716 Case 3).
718 +---------------+---------------+
719 | Cached data | Cache buffer |
720 +---------------+---------------+
722 +-----------------------------------------------------+
723 | Data to write |
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,
737 (double)wcp->offset,
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
745 * the data directly.
748 if ( n <= wcp->alloc_size && n > wcp->data_size) {
749 cache_flush_needed = True;
750 } else {
751 ssize_t ret = real_write_file(NULL, fsp, data,
752 pos, n);
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,
767 (double)wcp->offset,
768 (unsigned)wcp->data_size));
769 wcp->data_size = 0;
772 DO_PROFILE_INC(writecache_direct_writes);
773 if (ret == -1) {
774 return ret;
777 if (pos + ret > wcp->file_size) {
778 wcp->file_size = pos + ret;
781 return ret;
784 write_path = 4;
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);
809 if (ret == -1) {
810 return -1;
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.
825 if (n) {
826 DO_PROFILE_INC(writecache_cached_writes);
827 if (wcp->data_size) {
828 DO_PROFILE_INC(writecache_abutted_writes);
829 } else {
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) {
854 wcp->offset = pos;
856 wcp->data_size += n;
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) {
864 return -1;
867 DEBUG(9, ("wcp->offset = %.0f wcp->data_size = %u cache "
868 "return %u\n",
869 (double)wcp->offset, (unsigned int)wcp->data_size,
870 (unsigned int)n));
872 total_written += n;
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;
887 if(!fsp) {
888 return;
891 if(!(wcp = fsp->wcp)) {
892 return;
895 DO_PROFILE_INC(writecache_deallocations);
896 allocated_write_caches--;
898 SMB_ASSERT(wcp->data_size == 0);
900 SAFE_FREE(wcp->data);
901 SAFE_FREE(fsp->wcp);
903 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
904 fsp_str_dbg(fsp)));
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) {
917 return False;
920 if(alloc_size == 0 || fsp->wcp) {
921 return False;
924 if((wcp = SMB_MALLOC_P(struct write_cache)) == NULL) {
925 DEBUG(0,("setup_write_cache: malloc fail.\n"));
926 return False;
929 wcp->file_size = file_size;
930 wcp->offset = 0;
931 wcp->alloc_size = alloc_size;
932 wcp->data_size = 0;
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 ));
936 SAFE_FREE(wcp);
937 return False;
940 memset(wcp->data, '\0', wcp->alloc_size );
942 fsp->wcp = wcp;
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));
949 return True;
952 /****************************************************************************
953 Cope with a size change.
954 ****************************************************************************/
956 void set_filelen_write_cache(files_struct *fsp, off_t file_size)
958 if(fsp->wcp) {
959 /* The cache *must* have been flushed before we do this. */
960 if (fsp->wcp->data_size != 0) {
961 char *msg;
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) {
966 smb_panic(msg);
967 } else {
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;
982 size_t data_size;
983 ssize_t ret;
985 if(!wcp || !wcp->data_size) {
986 return 0;
989 data_size = wcp->data_size;
990 wcp->data_size = 0;
992 switch (reason) {
993 case SAMBA_SEEK_FLUSH:
994 DO_PROFILE_INC(writecache_flush_reason_seek);
995 break;
996 case SAMBA_READ_FLUSH:
997 DO_PROFILE_INC(writecache_flush_reason_read);
998 break;
999 case SAMBA_WRITE_FLUSH:
1000 DO_PROFILE_INC(writecache_flush_reason_write);;
1001 break;
1002 case SAMBA_READRAW_FLUSH:
1003 DO_PROFILE_INC(writecache_flush_reason_readraw);
1004 break;
1005 case SAMBA_OPLOCK_RELEASE_FLUSH:
1006 DO_PROFILE_INC(writecache_flush_reason_oplock);
1007 break;
1008 case SAMBA_CLOSE_FLUSH:
1009 DO_PROFILE_INC(writecache_flush_reason_close);
1010 break;
1011 case SAMBA_SYNC_FLUSH:
1012 DO_PROFILE_INC(writecache_flush_reason_sync);
1013 break;
1014 case SAMBA_SIZECHANGE_FLUSH:
1015 DO_PROFILE_INC(writecache_flush_reason_sizechange);
1016 break;
1017 default:
1018 break;
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;
1038 return ret;
1041 /*******************************************************************
1042 sync a file
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);
1053 if (ret == -1) {
1054 return map_nt_error_from_unix(errno);
1056 ret = smb_vfs_fsync_sync(fsp);
1057 if (ret == -1) {
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);
1073 } else {
1074 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1076 } else {
1077 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);