Second part of fix for bug #8673 - NT ACL issue.
[Samba.git] / source3 / smbd / fileio.c
blob9754b71d5de3cfc219b8111eec5b1e826c43517a
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 "smbd/globals.h"
25 static bool setup_write_cache(files_struct *, SMB_OFF_T);
27 /****************************************************************************
28 Read from write cache if we can.
29 ****************************************************************************/
31 static bool read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
33 write_cache *wcp = fsp->wcp;
35 if(!wcp) {
36 return False;
39 if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
40 return False;
43 memcpy(data, wcp->data + (pos - wcp->offset), n);
45 DO_PROFILE_INC(writecache_read_hits);
47 return True;
50 /****************************************************************************
51 Read from a file.
52 ****************************************************************************/
54 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
56 ssize_t ret=0,readret;
58 /* you can't read from print files */
59 if (fsp->print_file) {
60 errno = EBADF;
61 return -1;
65 * Serve from write cache if we can.
68 if(read_from_write_cache(fsp, data, pos, n)) {
69 fsp->fh->pos = pos + n;
70 fsp->fh->position_information = fsp->fh->pos;
71 return n;
74 flush_write_cache(fsp, READ_FLUSH);
76 fsp->fh->pos = pos;
78 if (n > 0) {
79 #ifdef DMF_FIX
80 int numretries = 3;
81 tryagain:
82 readret = SMB_VFS_PREAD(fsp,data,n,pos);
84 if (readret == -1) {
85 if ((errno == EAGAIN) && numretries) {
86 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
87 (void)sleep(10);
88 --numretries;
89 goto tryagain;
91 return -1;
93 #else /* NO DMF fix. */
94 readret = SMB_VFS_PREAD(fsp,data,n,pos);
96 if (readret == -1) {
97 return -1;
99 #endif
100 if (readret > 0) {
101 ret += readret;
105 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
106 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
108 fsp->fh->pos += ret;
109 fsp->fh->position_information = fsp->fh->pos;
111 return(ret);
114 /****************************************************************************
115 *Really* write to a file.
116 ****************************************************************************/
118 static ssize_t real_write_file(struct smb_request *req,
119 files_struct *fsp,
120 const char *data,
121 SMB_OFF_T pos,
122 size_t n)
124 ssize_t ret;
126 if (pos == -1) {
127 ret = vfs_write_data(req, fsp, data, n);
128 } else {
129 fsp->fh->pos = pos;
130 if (pos && lp_strict_allocate(SNUM(fsp->conn))) {
131 if (vfs_fill_sparse(fsp, pos) == -1) {
132 return -1;
135 ret = vfs_pwrite_data(req, fsp, data, n, pos);
138 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
139 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
141 if (ret != -1) {
142 fsp->fh->pos += ret;
144 /* Yes - this is correct - writes don't update this. JRA. */
145 /* Found by Samba4 tests. */
146 #if 0
147 fsp->position_information = fsp->pos;
148 #endif
151 return ret;
154 /****************************************************************************
155 File size cache change.
156 Updates size on disk but doesn't flush the cache.
157 ****************************************************************************/
159 static int wcp_file_size_change(files_struct *fsp)
161 int ret;
162 write_cache *wcp = fsp->wcp;
164 wcp->file_size = wcp->offset + wcp->data_size;
165 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
166 if (ret == -1) {
167 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
168 "error %s\n", fsp_str_dbg(fsp),
169 (double)wcp->file_size, strerror(errno)));
171 return ret;
174 void update_write_time_handler(struct event_context *ctx,
175 struct timed_event *te,
176 struct timeval now,
177 void *private_data)
179 files_struct *fsp = (files_struct *)private_data;
181 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp)));
183 /* change the write time in the open file db. */
184 (void)set_write_time(fsp->file_id, timespec_current());
186 /* And notify. */
187 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
188 FILE_NOTIFY_CHANGE_LAST_WRITE, fsp->fsp_name->base_name);
190 /* Remove the timed event handler. */
191 TALLOC_FREE(fsp->update_write_time_event);
194 /*********************************************************
195 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
196 in the future.
197 *********************************************************/
199 void trigger_write_time_update(struct files_struct *fsp)
201 int delay;
203 if (fsp->posix_open) {
204 /* Don't use delayed writes on POSIX files. */
205 return;
208 if (fsp->write_time_forced) {
209 /* No point - "sticky" write times
210 * in effect.
212 return;
215 /* We need to remember someone did a write
216 * and update to current time on close. */
218 fsp->update_write_time_on_close = true;
220 if (fsp->update_write_time_triggered) {
222 * We only update the write time after 2 seconds
223 * on the first normal write. After that
224 * no other writes affect this until close.
226 return;
228 fsp->update_write_time_triggered = true;
230 delay = lp_parm_int(SNUM(fsp->conn),
231 "smbd", "writetimeupdatedelay",
232 WRITE_TIME_UPDATE_USEC_DELAY);
234 DEBUG(5, ("Update write time %d usec later on %s\n",
235 delay, fsp_str_dbg(fsp)));
237 /* trigger the update 2 seconds later */
238 fsp->update_write_time_event =
239 event_add_timed(smbd_event_context(), NULL,
240 timeval_current_ofs(0, delay),
241 update_write_time_handler, fsp);
244 void trigger_write_time_update_immediate(struct files_struct *fsp)
246 struct smb_file_time ft;
248 if (fsp->posix_open) {
249 /* Don't use delayed writes on POSIX files. */
250 return;
253 if (fsp->write_time_forced) {
255 * No point - "sticky" write times
256 * in effect.
258 return;
261 TALLOC_FREE(fsp->update_write_time_event);
262 DEBUG(5, ("Update write time immediate on %s\n",
263 fsp_str_dbg(fsp)));
265 /* After an immediate update, reset the trigger. */
266 fsp->update_write_time_triggered = true;
267 fsp->update_write_time_on_close = false;
269 ZERO_STRUCT(ft);
270 ft.mtime = timespec_current();
272 /* Update the time in the open file db. */
273 (void)set_write_time(fsp->file_id, ft.mtime);
275 /* Now set on disk - takes care of notify. */
276 (void)smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
279 /****************************************************************************
280 Write to a file.
281 ****************************************************************************/
283 ssize_t write_file(struct smb_request *req,
284 files_struct *fsp,
285 const char *data,
286 SMB_OFF_T pos,
287 size_t n)
289 write_cache *wcp = fsp->wcp;
290 ssize_t total_written = 0;
291 int write_path = -1;
293 if (fsp->print_file) {
294 uint32 jobid;
296 if (!rap_to_pjobid(fsp->rap_print_jobid, NULL, &jobid)) {
297 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
298 (unsigned int)fsp->rap_print_jobid ));
299 errno = EBADF;
300 return -1;
303 return print_job_write(SNUM(fsp->conn), jobid, data, pos, n);
306 if (!fsp->can_write) {
307 errno = EPERM;
308 return -1;
311 if (!fsp->modified) {
312 fsp->modified = True;
314 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) == 0) {
315 trigger_write_time_update(fsp);
316 if (!fsp->posix_open &&
317 (lp_store_dos_attributes(SNUM(fsp->conn)) ||
318 MAP_ARCHIVE(fsp->conn))) {
319 int dosmode = dos_mode(fsp->conn, fsp->fsp_name);
320 if (!IS_DOS_ARCHIVE(dosmode)) {
321 file_set_dosmode(fsp->conn, fsp->fsp_name,
322 dosmode | aARCH, NULL, false);
327 * If this is the first write and we have an exclusive oplock then setup
328 * the write cache.
331 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
332 setup_write_cache(fsp,
333 fsp->fsp_name->st.st_ex_size);
334 wcp = fsp->wcp;
339 #ifdef WITH_PROFILE
340 DO_PROFILE_INC(writecache_total_writes);
341 if (!fsp->oplock_type) {
342 DO_PROFILE_INC(writecache_non_oplock_writes);
344 #endif
347 * If this file is level II oplocked then we need
348 * to grab the shared memory lock and inform all
349 * other files with a level II lock that they need
350 * to flush their read caches. We keep the lock over
351 * the shared memory area whilst doing this.
354 /* This should actually be improved to span the write. */
355 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
356 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
358 #ifdef WITH_PROFILE
359 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
360 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
361 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
362 profile_p->writecache_init_writes,
363 profile_p->writecache_abutted_writes,
364 profile_p->writecache_total_writes,
365 profile_p->writecache_non_oplock_writes,
366 profile_p->writecache_allocated_write_caches,
367 profile_p->writecache_num_write_caches,
368 profile_p->writecache_direct_writes,
369 profile_p->writecache_num_perfect_writes,
370 profile_p->writecache_read_hits ));
372 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
373 profile_p->writecache_flushed_writes[SEEK_FLUSH],
374 profile_p->writecache_flushed_writes[READ_FLUSH],
375 profile_p->writecache_flushed_writes[WRITE_FLUSH],
376 profile_p->writecache_flushed_writes[READRAW_FLUSH],
377 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
378 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
379 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
381 #endif
383 if (wcp && req->unread_bytes) {
384 /* If we're using receivefile don't
385 * deal with a write cache.
387 flush_write_cache(fsp, WRITE_FLUSH);
388 delete_write_cache(fsp);
389 wcp = NULL;
392 if(!wcp) {
393 DO_PROFILE_INC(writecache_direct_writes);
394 total_written = real_write_file(req, fsp, data, pos, n);
395 return total_written;
398 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
399 "wcp->data_size=%u\n", fsp_str_dbg(fsp), fsp->fh->fd,
400 (double)pos, (unsigned int)n, (double)wcp->offset,
401 (unsigned int)wcp->data_size));
403 fsp->fh->pos = pos + n;
406 * If we have active cache and it isn't contiguous then we flush.
407 * NOTE: There is a small problem with running out of disk ....
410 if (wcp->data_size) {
411 bool cache_flush_needed = False;
413 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
415 /* ASCII art.... JRA.
417 +--------------+-----
418 | Cached data | Rest of allocated cache buffer....
419 +--------------+-----
421 +-------------------+
422 | Data to write |
423 +-------------------+
428 * Start of write overlaps or abutts the existing data.
431 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
433 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
436 * Update the current buffer size with the new data.
439 if(pos + data_used > wcp->offset + wcp->data_size) {
440 wcp->data_size = pos + data_used - wcp->offset;
444 * Update the file size if changed.
447 if (wcp->offset + wcp->data_size > wcp->file_size) {
448 if (wcp_file_size_change(fsp) == -1) {
449 return -1;
454 * If we used all the data then
455 * return here.
458 if(n == data_used) {
459 return n;
460 } else {
461 cache_flush_needed = True;
464 * Move the start of data forward by the amount used,
465 * cut down the amount left by the same amount.
468 data += data_used;
469 pos += data_used;
470 n -= data_used;
472 DO_PROFILE_INC(writecache_abutted_writes);
473 total_written = data_used;
475 write_path = 1;
477 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
478 (pos + n <= wcp->offset + wcp->alloc_size)) {
480 /* ASCII art.... JRA.
482 +---------------+
483 | Cache buffer |
484 +---------------+
486 +-------------------+
487 | Data to write |
488 +-------------------+
493 * End of write overlaps the existing data.
496 size_t data_used = pos + n - wcp->offset;
498 memcpy(wcp->data, data + n - data_used, data_used);
501 * Update the current buffer size with the new data.
504 if(pos + n > wcp->offset + wcp->data_size) {
505 wcp->data_size = pos + n - wcp->offset;
509 * Update the file size if changed.
512 if (wcp->offset + wcp->data_size > wcp->file_size) {
513 if (wcp_file_size_change(fsp) == -1) {
514 return -1;
519 * We don't need to move the start of data, but we
520 * cut down the amount left by the amount used.
523 n -= data_used;
526 * We cannot have used all the data here.
529 cache_flush_needed = True;
531 DO_PROFILE_INC(writecache_abutted_writes);
532 total_written = data_used;
534 write_path = 2;
536 } else if ( (pos >= wcp->file_size) &&
537 (wcp->offset + wcp->data_size == wcp->file_size) &&
538 (pos > wcp->offset + wcp->data_size) &&
539 (pos < wcp->offset + wcp->alloc_size) ) {
541 /* ASCII art.... JRA.
543 End of file ---->|
545 +---------------+---------------+
546 | Cached data | Cache buffer |
547 +---------------+---------------+
549 +-------------------+
550 | Data to write |
551 +-------------------+
556 * Non-contiguous write part of which fits within
557 * the cache buffer and is extending the file
558 * and the cache contents reflect the current
559 * data up to the current end of the file.
562 size_t data_used;
564 if(pos + n <= wcp->offset + wcp->alloc_size) {
565 data_used = n;
566 } else {
567 data_used = wcp->offset + wcp->alloc_size - pos;
571 * Fill in the non-continuous area with zeros.
574 memset(wcp->data + wcp->data_size, '\0',
575 pos - (wcp->offset + wcp->data_size) );
577 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
580 * Update the current buffer size with the new data.
583 if(pos + data_used > wcp->offset + wcp->data_size) {
584 wcp->data_size = pos + data_used - wcp->offset;
588 * Update the file size if changed.
591 if (wcp->offset + wcp->data_size > wcp->file_size) {
592 if (wcp_file_size_change(fsp) == -1) {
593 return -1;
598 * If we used all the data then
599 * return here.
602 if(n == data_used) {
603 return n;
604 } else {
605 cache_flush_needed = True;
609 * Move the start of data forward by the amount used,
610 * cut down the amount left by the same amount.
613 data += data_used;
614 pos += data_used;
615 n -= data_used;
617 DO_PROFILE_INC(writecache_abutted_writes);
618 total_written = data_used;
620 write_path = 3;
622 } else if ( (pos >= wcp->file_size) &&
623 (n == 1) &&
624 (wcp->file_size == wcp->offset + wcp->data_size) &&
625 (pos < wcp->file_size + wcp->alloc_size)) {
629 End of file ---->|
631 +---------------+---------------+
632 | Cached data | Cache buffer |
633 +---------------+---------------+
635 |<------- allocated size ---------------->|
637 +--------+
638 | 1 Byte |
639 +--------+
641 MS-Office seems to do this a lot to determine if there's enough
642 space on the filesystem to write a new file.
644 Change to :
646 End of file ---->|
647 +-----------------------+--------+
648 | Zeroed Cached data | 1 Byte |
649 +-----------------------+--------+
652 flush_write_cache(fsp, WRITE_FLUSH);
653 wcp->offset = wcp->file_size;
654 wcp->data_size = pos - wcp->file_size + 1;
655 memset(wcp->data, '\0', wcp->data_size);
656 memcpy(wcp->data + wcp->data_size-1, data, 1);
659 * Update the file size if changed.
662 if (wcp->offset + wcp->data_size > wcp->file_size) {
663 if (wcp_file_size_change(fsp) == -1) {
664 return -1;
668 return n;
670 } else {
672 /* ASCII art..... JRA.
674 Case 1).
676 +---------------+---------------+
677 | Cached data | Cache buffer |
678 +---------------+---------------+
680 +-------------------+
681 | Data to write |
682 +-------------------+
684 Case 2).
686 +---------------+---------------+
687 | Cached data | Cache buffer |
688 +---------------+---------------+
690 +-------------------+
691 | Data to write |
692 +-------------------+
694 Case 3).
696 +---------------+---------------+
697 | Cached data | Cache buffer |
698 +---------------+---------------+
700 +-----------------------------------------------------+
701 | Data to write |
702 +-----------------------------------------------------+
707 * Write is bigger than buffer, or there is no overlap on the
708 * low or high ends.
711 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
712 len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
715 * If write would fit in the cache, and is larger than
716 * the data already in the cache, flush the cache and
717 * preferentially copy the data new data into it. Otherwise
718 * just write the data directly.
721 if ( n <= wcp->alloc_size && n > wcp->data_size) {
722 cache_flush_needed = True;
723 } else {
724 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
727 * If the write overlaps the entire cache, then
728 * discard the current contents of the cache.
729 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
732 if ((pos <= wcp->offset) &&
733 (pos + n >= wcp->offset + wcp->data_size) ) {
734 DEBUG(9,("write_file: discarding overwritten write \
735 cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
736 wcp->data_size = 0;
739 DO_PROFILE_INC(writecache_direct_writes);
740 if (ret == -1) {
741 return ret;
744 if (pos + ret > wcp->file_size) {
745 wcp->file_size = pos + ret;
748 return ret;
751 write_path = 4;
755 if (cache_flush_needed) {
756 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
757 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
758 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
759 (double)wcp->offset, (unsigned int)wcp->data_size ));
761 flush_write_cache(fsp, WRITE_FLUSH);
766 * If the write request is bigger than the cache
767 * size, write it all out.
770 if (n > wcp->alloc_size ) {
771 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
772 if (ret == -1) {
773 return -1;
776 if (pos + ret > wcp->file_size) {
777 wcp->file_size = pos + n;
780 DO_PROFILE_INC(writecache_direct_writes);
781 return total_written + n;
785 * If there's any data left, cache it.
788 if (n) {
789 #ifdef WITH_PROFILE
790 if (wcp->data_size) {
791 DO_PROFILE_INC(writecache_abutted_writes);
792 } else {
793 DO_PROFILE_INC(writecache_init_writes);
795 #endif
796 memcpy(wcp->data+wcp->data_size, data, n);
797 if (wcp->data_size == 0) {
798 wcp->offset = pos;
799 DO_PROFILE_INC(writecache_num_write_caches);
801 wcp->data_size += n;
804 * Update the file size if changed.
807 if (wcp->offset + wcp->data_size > wcp->file_size) {
808 if (wcp_file_size_change(fsp) == -1) {
809 return -1;
812 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
813 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
815 total_written += n;
816 return total_written; /* .... that's a write :) */
819 return total_written;
822 /****************************************************************************
823 Delete the write cache structure.
824 ****************************************************************************/
826 void delete_write_cache(files_struct *fsp)
828 write_cache *wcp;
830 if(!fsp) {
831 return;
834 if(!(wcp = fsp->wcp)) {
835 return;
838 DO_PROFILE_DEC(writecache_allocated_write_caches);
839 allocated_write_caches--;
841 SMB_ASSERT(wcp->data_size == 0);
843 SAFE_FREE(wcp->data);
844 SAFE_FREE(fsp->wcp);
846 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
847 fsp_str_dbg(fsp)));
850 /****************************************************************************
851 Setup the write cache structure.
852 ****************************************************************************/
854 static bool setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
856 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
857 write_cache *wcp;
859 if (allocated_write_caches >= MAX_WRITE_CACHES) {
860 return False;
863 if(alloc_size == 0 || fsp->wcp) {
864 return False;
867 if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
868 DEBUG(0,("setup_write_cache: malloc fail.\n"));
869 return False;
872 wcp->file_size = file_size;
873 wcp->offset = 0;
874 wcp->alloc_size = alloc_size;
875 wcp->data_size = 0;
876 if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
877 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
878 (unsigned int)wcp->alloc_size ));
879 SAFE_FREE(wcp);
880 return False;
883 memset(wcp->data, '\0', wcp->alloc_size );
885 fsp->wcp = wcp;
886 DO_PROFILE_INC(writecache_allocated_write_caches);
887 allocated_write_caches++;
889 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
890 fsp_str_dbg(fsp), (unsigned long)wcp->alloc_size));
892 return True;
895 /****************************************************************************
896 Cope with a size change.
897 ****************************************************************************/
899 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
901 if(fsp->wcp) {
902 /* The cache *must* have been flushed before we do this. */
903 if (fsp->wcp->data_size != 0) {
904 char *msg;
905 if (asprintf(&msg, "set_filelen_write_cache: size change "
906 "on file %s with write cache size = %lu\n",
907 fsp->fsp_name->base_name,
908 (unsigned long)fsp->wcp->data_size) != -1) {
909 smb_panic(msg);
910 } else {
911 smb_panic("set_filelen_write_cache");
914 fsp->wcp->file_size = file_size;
918 /*******************************************************************
919 Flush a write cache struct to disk.
920 ********************************************************************/
922 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
924 write_cache *wcp = fsp->wcp;
925 size_t data_size;
926 ssize_t ret;
928 if(!wcp || !wcp->data_size) {
929 return 0;
932 data_size = wcp->data_size;
933 wcp->data_size = 0;
935 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
937 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
938 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
940 #ifdef WITH_PROFILE
941 if(data_size == wcp->alloc_size) {
942 DO_PROFILE_INC(writecache_num_perfect_writes);
944 #endif
946 ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
949 * Ensure file size if kept up to date if write extends file.
952 if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
953 wcp->file_size = wcp->offset + ret;
956 return ret;
959 /*******************************************************************
960 sync a file
961 ********************************************************************/
963 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
965 if (fsp->fh->fd == -1)
966 return NT_STATUS_INVALID_HANDLE;
968 if (lp_strict_sync(SNUM(conn)) &&
969 (lp_syncalways(SNUM(conn)) || write_through)) {
970 int ret = flush_write_cache(fsp, SYNC_FLUSH);
971 if (ret == -1) {
972 return map_nt_error_from_unix(errno);
974 ret = SMB_VFS_FSYNC(fsp);
975 if (ret == -1) {
976 return map_nt_error_from_unix(errno);
979 return NT_STATUS_OK;
982 /************************************************************
983 Perform a stat whether a valid fd or not.
984 ************************************************************/
986 int fsp_stat(files_struct *fsp)
988 if (fsp->fh->fd == -1) {
989 if (fsp->posix_open) {
990 return SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
991 } else {
992 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
994 } else {
995 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);