s3: fine tune and clean up statvfs tests
[Samba/wip.git] / source3 / smbd / fileio.c
bloba14be7806a04ea9cc2c96ac3968fefa2d4b84265
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_read_hits);
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, 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 if (pos == -1) {
119 ret = vfs_write_data(req, fsp, data, n);
120 } else {
121 fsp->fh->pos = pos;
122 if (pos && lp_strict_allocate(SNUM(fsp->conn) &&
123 !fsp->is_sparse)) {
124 if (vfs_fill_sparse(fsp, pos) == -1) {
125 return -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));
134 if (ret != -1) {
135 fsp->fh->pos += ret;
137 /* Yes - this is correct - writes don't update this. JRA. */
138 /* Found by Samba4 tests. */
139 #if 0
140 fsp->position_information = fsp->pos;
141 #endif
144 return ret;
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)
154 int ret;
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);
159 if (ret == -1) {
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)));
164 return ret;
167 void update_write_time_handler(struct event_context *ctx,
168 struct timed_event *te,
169 struct timeval now,
170 void *private_data)
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());
179 /* And notify. */
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
189 in the future.
190 *********************************************************/
192 void trigger_write_time_update(struct files_struct *fsp)
194 int delay;
196 if (fsp->posix_open) {
197 /* Don't use delayed writes on POSIX files. */
198 return;
201 if (fsp->write_time_forced) {
202 /* No point - "sticky" write times
203 * in effect.
205 return;
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.
219 return;
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. */
243 return;
246 if (fsp->write_time_forced) {
248 * No point - "sticky" write times
249 * in effect.
251 return;
254 TALLOC_FREE(fsp->update_write_time_event);
255 DEBUG(5, ("Update write time immediate on %s\n",
256 fsp_str_dbg(fsp)));
258 /* After an immediate update, reset the trigger. */
259 fsp->update_write_time_triggered = true;
260 fsp->update_write_time_on_close = false;
262 ZERO_STRUCT(ft);
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 /****************************************************************************
273 Write to a file.
274 ****************************************************************************/
276 ssize_t write_file(struct smb_request *req,
277 files_struct *fsp,
278 const char *data,
279 off_t pos,
280 size_t n)
282 struct write_cache *wcp = fsp->wcp;
283 ssize_t total_written = 0;
284 int write_path = -1;
286 if (fsp->print_file) {
287 uint32_t t;
288 int ret;
290 ret = print_spool_write(fsp, data, n, pos, &t);
291 if (ret) {
292 errno = ret;
293 return -1;
295 return t;
298 if (!fsp->can_write) {
299 errno = EPERM;
300 return -1;
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
320 * the write cache.
323 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
324 setup_write_cache(fsp,
325 fsp->fsp_name->st.st_ex_size);
326 wcp = fsp->wcp;
331 #ifdef WITH_PROFILE
332 DO_PROFILE_INC(writecache_total_writes);
333 if (!fsp->oplock_type) {
334 DO_PROFILE_INC(writecache_non_oplock_writes);
336 #endif
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);
350 #ifdef WITH_PROFILE
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] ));
373 #endif
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);
381 wcp = NULL;
384 if(!wcp) {
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)) {
398 int ret;
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
408 * file length.
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);
418 if (ret == -1) {
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)));
422 return -1;
424 return 1;
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 +-------------------+
445 | Data to write |
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) {
472 return -1;
477 * If we used all the data then
478 * return here.
481 if(n == data_used) {
482 return n;
483 } else {
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.
491 data += data_used;
492 pos += data_used;
493 n -= data_used;
495 DO_PROFILE_INC(writecache_abutted_writes);
496 total_written = data_used;
498 write_path = 1;
500 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
501 (pos + n <= wcp->offset + wcp->alloc_size)) {
503 /* ASCII art.... JRA.
505 +---------------+
506 | Cache buffer |
507 +---------------+
509 +-------------------+
510 | Data to write |
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) {
537 return -1;
542 * We don't need to move the start of data, but we
543 * cut down the amount left by the amount used.
546 n -= data_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;
557 write_path = 2;
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.
566 End of file ---->|
568 +---------------+---------------+
569 | Cached data | Cache buffer |
570 +---------------+---------------+
572 +-------------------+
573 | Data to write |
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.
585 size_t data_used;
587 if(pos + n <= wcp->offset + wcp->alloc_size) {
588 data_used = n;
589 } else {
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) {
616 return -1;
621 * If we used all the data then
622 * return here.
625 if(n == data_used) {
626 return n;
627 } else {
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.
636 data += data_used;
637 pos += data_used;
638 n -= data_used;
640 DO_PROFILE_INC(writecache_abutted_writes);
641 total_written = data_used;
643 write_path = 3;
645 } else if ( (pos >= wcp->file_size) &&
646 (n == 1) &&
647 (wcp->file_size == wcp->offset + wcp->data_size) &&
648 (pos < wcp->file_size + wcp->alloc_size)) {
652 End of file ---->|
654 +---------------+---------------+
655 | Cached data | Cache buffer |
656 +---------------+---------------+
658 |<------- allocated size ---------------->|
660 +--------+
661 | 1 Byte |
662 +--------+
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.
667 Change to :
669 End of 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) {
687 return -1;
691 return n;
693 } else {
695 /* ASCII art..... JRA.
697 Case 1).
699 +---------------+---------------+
700 | Cached data | Cache buffer |
701 +---------------+---------------+
703 +-------------------+
704 | Data to write |
705 +-------------------+
707 Case 2).
709 +---------------+---------------+
710 | Cached data | Cache buffer |
711 +---------------+---------------+
713 +-------------------+
714 | Data to write |
715 +-------------------+
717 Case 3).
719 +---------------+---------------+
720 | Cached data | Cache buffer |
721 +---------------+---------------+
723 +-----------------------------------------------------+
724 | Data to write |
725 +-----------------------------------------------------+
730 * Write is bigger than buffer, or there is no overlap on the
731 * low or high ends.
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;
746 } else {
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 ));
759 wcp->data_size = 0;
762 DO_PROFILE_INC(writecache_direct_writes);
763 if (ret == -1) {
764 return ret;
767 if (pos + ret > wcp->file_size) {
768 wcp->file_size = pos + ret;
771 return ret;
774 write_path = 4;
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);
795 if (ret == -1) {
796 return -1;
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.
811 if (n) {
812 #ifdef WITH_PROFILE
813 if (wcp->data_size) {
814 DO_PROFILE_INC(writecache_abutted_writes);
815 } else {
816 DO_PROFILE_INC(writecache_init_writes);
818 #endif
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) {
841 wcp->offset = pos;
842 DO_PROFILE_INC(writecache_num_write_caches);
844 wcp->data_size += n;
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) {
852 return -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));
858 total_written += 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;
873 if(!fsp) {
874 return;
877 if(!(wcp = fsp->wcp)) {
878 return;
881 DO_PROFILE_DEC(writecache_allocated_write_caches);
882 allocated_write_caches--;
884 SMB_ASSERT(wcp->data_size == 0);
886 SAFE_FREE(wcp->data);
887 SAFE_FREE(fsp->wcp);
889 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
890 fsp_str_dbg(fsp)));
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) {
903 return False;
906 if(alloc_size == 0 || fsp->wcp) {
907 return False;
910 if((wcp = SMB_MALLOC_P(struct write_cache)) == NULL) {
911 DEBUG(0,("setup_write_cache: malloc fail.\n"));
912 return False;
915 wcp->file_size = file_size;
916 wcp->offset = 0;
917 wcp->alloc_size = alloc_size;
918 wcp->data_size = 0;
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 ));
922 SAFE_FREE(wcp);
923 return False;
926 memset(wcp->data, '\0', wcp->alloc_size );
928 fsp->wcp = wcp;
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));
935 return True;
938 /****************************************************************************
939 Cope with a size change.
940 ****************************************************************************/
942 void set_filelen_write_cache(files_struct *fsp, off_t file_size)
944 if(fsp->wcp) {
945 /* The cache *must* have been flushed before we do this. */
946 if (fsp->wcp->data_size != 0) {
947 char *msg;
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) {
952 smb_panic(msg);
953 } else {
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;
968 size_t data_size;
969 ssize_t ret;
971 if(!wcp || !wcp->data_size) {
972 return 0;
975 data_size = wcp->data_size;
976 wcp->data_size = 0;
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));
983 #ifdef WITH_PROFILE
984 if(data_size == wcp->alloc_size) {
985 DO_PROFILE_INC(writecache_num_perfect_writes);
987 #endif
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;
999 return ret;
1002 /*******************************************************************
1003 sync a file
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);
1014 if (ret == -1) {
1015 return map_nt_error_from_unix(errno);
1017 ret = SMB_VFS_FSYNC(fsp);
1018 if (ret == -1) {
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);
1034 } else {
1035 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1037 } else {
1038 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);