s3:smbd: remove typedef for struct write_cache
[Samba/gebeck_regimport.git] / source3 / smbd / fileio.c
blobab505f4f8690c9736dabf73f494f02cbd532ac85
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,readret;
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 #ifdef DMF_FIX
91 int numretries = 3;
92 tryagain:
93 readret = SMB_VFS_PREAD(fsp,data,n,pos);
95 if (readret == -1) {
96 if ((errno == EAGAIN) && numretries) {
97 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
98 (void)sleep(10);
99 --numretries;
100 goto tryagain;
102 return -1;
104 #else /* NO DMF fix. */
105 readret = SMB_VFS_PREAD(fsp,data,n,pos);
107 if (readret == -1) {
108 return -1;
110 #endif
111 if (readret > 0) {
112 ret += readret;
116 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
117 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
119 fsp->fh->pos += ret;
120 fsp->fh->position_information = fsp->fh->pos;
122 return(ret);
125 /****************************************************************************
126 *Really* write to a file.
127 ****************************************************************************/
129 static ssize_t real_write_file(struct smb_request *req,
130 files_struct *fsp,
131 const char *data,
132 off_t pos,
133 size_t n)
135 ssize_t ret;
137 if (pos == -1) {
138 ret = vfs_write_data(req, fsp, data, n);
139 } else {
140 fsp->fh->pos = pos;
141 if (pos && lp_strict_allocate(SNUM(fsp->conn) &&
142 !fsp->is_sparse)) {
143 if (vfs_fill_sparse(fsp, pos) == -1) {
144 return -1;
147 ret = vfs_pwrite_data(req, fsp, data, n, pos);
150 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
151 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
153 if (ret != -1) {
154 fsp->fh->pos += ret;
156 /* Yes - this is correct - writes don't update this. JRA. */
157 /* Found by Samba4 tests. */
158 #if 0
159 fsp->position_information = fsp->pos;
160 #endif
163 return ret;
166 /****************************************************************************
167 File size cache change.
168 Updates size on disk but doesn't flush the cache.
169 ****************************************************************************/
171 static int wcp_file_size_change(files_struct *fsp)
173 int ret;
174 struct write_cache *wcp = fsp->wcp;
176 wcp->file_size = wcp->offset + wcp->data_size;
177 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
178 if (ret == -1) {
179 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
180 "error %s\n", fsp_str_dbg(fsp),
181 (double)wcp->file_size, strerror(errno)));
183 return ret;
186 void update_write_time_handler(struct event_context *ctx,
187 struct timed_event *te,
188 struct timeval now,
189 void *private_data)
191 files_struct *fsp = (files_struct *)private_data;
193 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp)));
195 /* change the write time in the open file db. */
196 (void)set_write_time(fsp->file_id, timespec_current());
198 /* And notify. */
199 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
200 FILE_NOTIFY_CHANGE_LAST_WRITE, fsp->fsp_name->base_name);
202 /* Remove the timed event handler. */
203 TALLOC_FREE(fsp->update_write_time_event);
206 /*********************************************************
207 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
208 in the future.
209 *********************************************************/
211 void trigger_write_time_update(struct files_struct *fsp)
213 int delay;
215 if (fsp->posix_open) {
216 /* Don't use delayed writes on POSIX files. */
217 return;
220 if (fsp->write_time_forced) {
221 /* No point - "sticky" write times
222 * in effect.
224 return;
227 /* We need to remember someone did a write
228 * and update to current time on close. */
230 fsp->update_write_time_on_close = true;
232 if (fsp->update_write_time_triggered) {
234 * We only update the write time after 2 seconds
235 * on the first normal write. After that
236 * no other writes affect this until close.
238 return;
240 fsp->update_write_time_triggered = true;
242 delay = lp_parm_int(SNUM(fsp->conn),
243 "smbd", "writetimeupdatedelay",
244 WRITE_TIME_UPDATE_USEC_DELAY);
246 DEBUG(5, ("Update write time %d usec later on %s\n",
247 delay, fsp_str_dbg(fsp)));
249 /* trigger the update 2 seconds later */
250 fsp->update_write_time_event =
251 tevent_add_timer(fsp->conn->sconn->ev_ctx, NULL,
252 timeval_current_ofs_usec(delay),
253 update_write_time_handler, fsp);
256 void trigger_write_time_update_immediate(struct files_struct *fsp)
258 struct smb_file_time ft;
260 if (fsp->posix_open) {
261 /* Don't use delayed writes on POSIX files. */
262 return;
265 if (fsp->write_time_forced) {
267 * No point - "sticky" write times
268 * in effect.
270 return;
273 TALLOC_FREE(fsp->update_write_time_event);
274 DEBUG(5, ("Update write time immediate on %s\n",
275 fsp_str_dbg(fsp)));
277 /* After an immediate update, reset the trigger. */
278 fsp->update_write_time_triggered = true;
279 fsp->update_write_time_on_close = false;
281 ZERO_STRUCT(ft);
282 ft.mtime = timespec_current();
284 /* Update the time in the open file db. */
285 (void)set_write_time(fsp->file_id, ft.mtime);
287 /* Now set on disk - takes care of notify. */
288 (void)smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
291 /****************************************************************************
292 Write to a file.
293 ****************************************************************************/
295 ssize_t write_file(struct smb_request *req,
296 files_struct *fsp,
297 const char *data,
298 off_t pos,
299 size_t n)
301 struct write_cache *wcp = fsp->wcp;
302 ssize_t total_written = 0;
303 int write_path = -1;
305 if (fsp->print_file) {
306 uint32_t t;
307 int ret;
309 ret = print_spool_write(fsp, data, n, pos, &t);
310 if (ret) {
311 errno = ret;
312 return -1;
314 return t;
317 if (!fsp->can_write) {
318 errno = EPERM;
319 return -1;
322 if (!fsp->modified) {
323 fsp->modified = True;
325 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) == 0) {
326 trigger_write_time_update(fsp);
327 if (!fsp->posix_open &&
328 (lp_store_dos_attributes(SNUM(fsp->conn)) ||
329 MAP_ARCHIVE(fsp->conn))) {
330 int dosmode = dos_mode(fsp->conn, fsp->fsp_name);
331 if (!IS_DOS_ARCHIVE(dosmode)) {
332 file_set_dosmode(fsp->conn, fsp->fsp_name,
333 dosmode | FILE_ATTRIBUTE_ARCHIVE, NULL, false);
338 * If this is the first write and we have an exclusive oplock then setup
339 * the write cache.
342 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
343 setup_write_cache(fsp,
344 fsp->fsp_name->st.st_ex_size);
345 wcp = fsp->wcp;
350 #ifdef WITH_PROFILE
351 DO_PROFILE_INC(writecache_total_writes);
352 if (!fsp->oplock_type) {
353 DO_PROFILE_INC(writecache_non_oplock_writes);
355 #endif
358 * If this file is level II oplocked then we need
359 * to grab the shared memory lock and inform all
360 * other files with a level II lock that they need
361 * to flush their read caches. We keep the lock over
362 * the shared memory area whilst doing this.
365 /* This should actually be improved to span the write. */
366 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
367 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
369 #ifdef WITH_PROFILE
370 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
371 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
372 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
373 profile_p->writecache_init_writes,
374 profile_p->writecache_abutted_writes,
375 profile_p->writecache_total_writes,
376 profile_p->writecache_non_oplock_writes,
377 profile_p->writecache_allocated_write_caches,
378 profile_p->writecache_num_write_caches,
379 profile_p->writecache_direct_writes,
380 profile_p->writecache_num_perfect_writes,
381 profile_p->writecache_read_hits ));
383 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
384 profile_p->writecache_flushed_writes[SEEK_FLUSH],
385 profile_p->writecache_flushed_writes[READ_FLUSH],
386 profile_p->writecache_flushed_writes[WRITE_FLUSH],
387 profile_p->writecache_flushed_writes[READRAW_FLUSH],
388 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
389 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
390 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
392 #endif
394 if (wcp && req->unread_bytes) {
395 /* If we're using receivefile don't
396 * deal with a write cache.
398 flush_write_cache(fsp, WRITE_FLUSH);
399 delete_write_cache(fsp);
400 wcp = NULL;
403 if(!wcp) {
404 DO_PROFILE_INC(writecache_direct_writes);
405 total_written = real_write_file(req, fsp, data, pos, n);
406 return total_written;
409 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
410 "wcp->data_size=%u\n", fsp_str_dbg(fsp), fsp->fh->fd,
411 (double)pos, (unsigned int)n, (double)wcp->offset,
412 (unsigned int)wcp->data_size));
414 fsp->fh->pos = pos + n;
416 if ((n == 1) && (data[0] == '\0') && (pos > wcp->file_size)) {
417 int ret;
420 * This is a 1-byte write of a 0 beyond the EOF and
421 * thus implicitly also beyond the current active
422 * write cache, the typical file-extending (and
423 * allocating, but we're using the write cache here)
424 * write done by Windows. We just have to ftruncate
425 * the file and rely on posix semantics to return
426 * zeros for non-written file data that is within the
427 * file length.
429 * We can not use wcp_file_size_change here because we
430 * might have an existing write cache, and
431 * wcp_file_size_change assumes a change to just the
432 * end of the current write cache.
435 wcp->file_size = pos + 1;
436 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
437 if (ret == -1) {
438 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f"
439 "error %s\n", fsp_str_dbg(fsp),
440 (double)wcp->file_size, strerror(errno)));
441 return -1;
443 return 1;
448 * If we have active cache and it isn't contiguous then we flush.
449 * NOTE: There is a small problem with running out of disk ....
452 if (wcp->data_size) {
453 bool cache_flush_needed = False;
455 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
457 /* ASCII art.... JRA.
459 +--------------+-----
460 | Cached data | Rest of allocated cache buffer....
461 +--------------+-----
463 +-------------------+
464 | Data to write |
465 +-------------------+
470 * Start of write overlaps or abutts the existing data.
473 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
475 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
478 * Update the current buffer size with the new data.
481 if(pos + data_used > wcp->offset + wcp->data_size) {
482 wcp->data_size = pos + data_used - wcp->offset;
486 * Update the file size if changed.
489 if (wcp->offset + wcp->data_size > wcp->file_size) {
490 if (wcp_file_size_change(fsp) == -1) {
491 return -1;
496 * If we used all the data then
497 * return here.
500 if(n == data_used) {
501 return n;
502 } else {
503 cache_flush_needed = True;
506 * Move the start of data forward by the amount used,
507 * cut down the amount left by the same amount.
510 data += data_used;
511 pos += data_used;
512 n -= data_used;
514 DO_PROFILE_INC(writecache_abutted_writes);
515 total_written = data_used;
517 write_path = 1;
519 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
520 (pos + n <= wcp->offset + wcp->alloc_size)) {
522 /* ASCII art.... JRA.
524 +---------------+
525 | Cache buffer |
526 +---------------+
528 +-------------------+
529 | Data to write |
530 +-------------------+
535 * End of write overlaps the existing data.
538 size_t data_used = pos + n - wcp->offset;
540 memcpy(wcp->data, data + n - data_used, data_used);
543 * Update the current buffer size with the new data.
546 if(pos + n > wcp->offset + wcp->data_size) {
547 wcp->data_size = pos + n - wcp->offset;
551 * Update the file size if changed.
554 if (wcp->offset + wcp->data_size > wcp->file_size) {
555 if (wcp_file_size_change(fsp) == -1) {
556 return -1;
561 * We don't need to move the start of data, but we
562 * cut down the amount left by the amount used.
565 n -= data_used;
568 * We cannot have used all the data here.
571 cache_flush_needed = True;
573 DO_PROFILE_INC(writecache_abutted_writes);
574 total_written = data_used;
576 write_path = 2;
578 } else if ( (pos >= wcp->file_size) &&
579 (wcp->offset + wcp->data_size == wcp->file_size) &&
580 (pos > wcp->offset + wcp->data_size) &&
581 (pos < wcp->offset + wcp->alloc_size) ) {
583 /* ASCII art.... JRA.
585 End of file ---->|
587 +---------------+---------------+
588 | Cached data | Cache buffer |
589 +---------------+---------------+
591 +-------------------+
592 | Data to write |
593 +-------------------+
598 * Non-contiguous write part of which fits within
599 * the cache buffer and is extending the file
600 * and the cache contents reflect the current
601 * data up to the current end of the file.
604 size_t data_used;
606 if(pos + n <= wcp->offset + wcp->alloc_size) {
607 data_used = n;
608 } else {
609 data_used = wcp->offset + wcp->alloc_size - pos;
613 * Fill in the non-continuous area with zeros.
616 memset(wcp->data + wcp->data_size, '\0',
617 pos - (wcp->offset + wcp->data_size) );
619 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
622 * Update the current buffer size with the new data.
625 if(pos + data_used > wcp->offset + wcp->data_size) {
626 wcp->data_size = pos + data_used - wcp->offset;
630 * Update the file size if changed.
633 if (wcp->offset + wcp->data_size > wcp->file_size) {
634 if (wcp_file_size_change(fsp) == -1) {
635 return -1;
640 * If we used all the data then
641 * return here.
644 if(n == data_used) {
645 return n;
646 } else {
647 cache_flush_needed = True;
651 * Move the start of data forward by the amount used,
652 * cut down the amount left by the same amount.
655 data += data_used;
656 pos += data_used;
657 n -= data_used;
659 DO_PROFILE_INC(writecache_abutted_writes);
660 total_written = data_used;
662 write_path = 3;
664 } else if ( (pos >= wcp->file_size) &&
665 (n == 1) &&
666 (wcp->file_size == wcp->offset + wcp->data_size) &&
667 (pos < wcp->file_size + wcp->alloc_size)) {
671 End of file ---->|
673 +---------------+---------------+
674 | Cached data | Cache buffer |
675 +---------------+---------------+
677 |<------- allocated size ---------------->|
679 +--------+
680 | 1 Byte |
681 +--------+
683 MS-Office seems to do this a lot to determine if there's enough
684 space on the filesystem to write a new file.
686 Change to :
688 End of file ---->|
689 +-----------------------+--------+
690 | Zeroed Cached data | 1 Byte |
691 +-----------------------+--------+
694 flush_write_cache(fsp, WRITE_FLUSH);
695 wcp->offset = wcp->file_size;
696 wcp->data_size = pos - wcp->file_size + 1;
697 memset(wcp->data, '\0', wcp->data_size);
698 memcpy(wcp->data + wcp->data_size-1, data, 1);
701 * Update the file size if changed.
704 if (wcp->offset + wcp->data_size > wcp->file_size) {
705 if (wcp_file_size_change(fsp) == -1) {
706 return -1;
710 return n;
712 } else {
714 /* ASCII art..... JRA.
716 Case 1).
718 +---------------+---------------+
719 | Cached data | Cache buffer |
720 +---------------+---------------+
722 +-------------------+
723 | Data to write |
724 +-------------------+
726 Case 2).
728 +---------------+---------------+
729 | Cached data | Cache buffer |
730 +---------------+---------------+
732 +-------------------+
733 | Data to write |
734 +-------------------+
736 Case 3).
738 +---------------+---------------+
739 | Cached data | Cache buffer |
740 +---------------+---------------+
742 +-----------------------------------------------------+
743 | Data to write |
744 +-----------------------------------------------------+
749 * Write is bigger than buffer, or there is no overlap on the
750 * low or high ends.
753 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
754 len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
757 * If write would fit in the cache, and is larger than
758 * the data already in the cache, flush the cache and
759 * preferentially copy the data new data into it. Otherwise
760 * just write the data directly.
763 if ( n <= wcp->alloc_size && n > wcp->data_size) {
764 cache_flush_needed = True;
765 } else {
766 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
769 * If the write overlaps the entire cache, then
770 * discard the current contents of the cache.
771 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
774 if ((pos <= wcp->offset) &&
775 (pos + n >= wcp->offset + wcp->data_size) ) {
776 DEBUG(9,("write_file: discarding overwritten write \
777 cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
778 wcp->data_size = 0;
781 DO_PROFILE_INC(writecache_direct_writes);
782 if (ret == -1) {
783 return ret;
786 if (pos + ret > wcp->file_size) {
787 wcp->file_size = pos + ret;
790 return ret;
793 write_path = 4;
797 if (cache_flush_needed) {
798 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
799 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
800 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
801 (double)wcp->offset, (unsigned int)wcp->data_size ));
803 flush_write_cache(fsp, WRITE_FLUSH);
808 * If the write request is bigger than the cache
809 * size, write it all out.
812 if (n > wcp->alloc_size ) {
813 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
814 if (ret == -1) {
815 return -1;
818 if (pos + ret > wcp->file_size) {
819 wcp->file_size = pos + n;
822 DO_PROFILE_INC(writecache_direct_writes);
823 return total_written + n;
827 * If there's any data left, cache it.
830 if (n) {
831 #ifdef WITH_PROFILE
832 if (wcp->data_size) {
833 DO_PROFILE_INC(writecache_abutted_writes);
834 } else {
835 DO_PROFILE_INC(writecache_init_writes);
837 #endif
839 if ((wcp->data_size == 0)
840 && (pos > wcp->file_size)
841 && (pos + n <= wcp->file_size + wcp->alloc_size)) {
843 * This is a write completely beyond the
844 * current EOF, but within reach of the write
845 * cache. We expect fill-up writes pretty
846 * soon, so it does not make sense to start
847 * the write cache at the current
848 * offset. These fill-up writes would trigger
849 * separate pwrites or even unnecessary cache
850 * flushes because they overlap if this is a
851 * one-byte allocating write.
853 wcp->offset = wcp->file_size;
854 wcp->data_size = pos - wcp->file_size;
855 memset(wcp->data, 0, wcp->data_size);
858 memcpy(wcp->data+wcp->data_size, data, n);
859 if (wcp->data_size == 0) {
860 wcp->offset = pos;
861 DO_PROFILE_INC(writecache_num_write_caches);
863 wcp->data_size += n;
866 * Update the file size if changed.
869 if (wcp->offset + wcp->data_size > wcp->file_size) {
870 if (wcp_file_size_change(fsp) == -1) {
871 return -1;
874 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
875 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
877 total_written += n;
878 return total_written; /* .... that's a write :) */
881 return total_written;
884 /****************************************************************************
885 Delete the write cache structure.
886 ****************************************************************************/
888 void delete_write_cache(files_struct *fsp)
890 struct write_cache *wcp;
892 if(!fsp) {
893 return;
896 if(!(wcp = fsp->wcp)) {
897 return;
900 DO_PROFILE_DEC(writecache_allocated_write_caches);
901 allocated_write_caches--;
903 SMB_ASSERT(wcp->data_size == 0);
905 SAFE_FREE(wcp->data);
906 SAFE_FREE(fsp->wcp);
908 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
909 fsp_str_dbg(fsp)));
912 /****************************************************************************
913 Setup the write cache structure.
914 ****************************************************************************/
916 static bool setup_write_cache(files_struct *fsp, off_t file_size)
918 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
919 struct write_cache *wcp;
921 if (allocated_write_caches >= MAX_WRITE_CACHES) {
922 return False;
925 if(alloc_size == 0 || fsp->wcp) {
926 return False;
929 if((wcp = SMB_MALLOC_P(struct write_cache)) == NULL) {
930 DEBUG(0,("setup_write_cache: malloc fail.\n"));
931 return False;
934 wcp->file_size = file_size;
935 wcp->offset = 0;
936 wcp->alloc_size = alloc_size;
937 wcp->data_size = 0;
938 if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
939 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
940 (unsigned int)wcp->alloc_size ));
941 SAFE_FREE(wcp);
942 return False;
945 memset(wcp->data, '\0', wcp->alloc_size );
947 fsp->wcp = wcp;
948 DO_PROFILE_INC(writecache_allocated_write_caches);
949 allocated_write_caches++;
951 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
952 fsp_str_dbg(fsp), (unsigned long)wcp->alloc_size));
954 return True;
957 /****************************************************************************
958 Cope with a size change.
959 ****************************************************************************/
961 void set_filelen_write_cache(files_struct *fsp, off_t file_size)
963 if(fsp->wcp) {
964 /* The cache *must* have been flushed before we do this. */
965 if (fsp->wcp->data_size != 0) {
966 char *msg;
967 if (asprintf(&msg, "set_filelen_write_cache: size change "
968 "on file %s with write cache size = %lu\n",
969 fsp->fsp_name->base_name,
970 (unsigned long)fsp->wcp->data_size) != -1) {
971 smb_panic(msg);
972 } else {
973 smb_panic("set_filelen_write_cache");
976 fsp->wcp->file_size = file_size;
980 /*******************************************************************
981 Flush a write cache struct to disk.
982 ********************************************************************/
984 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
986 struct write_cache *wcp = fsp->wcp;
987 size_t data_size;
988 ssize_t ret;
990 if(!wcp || !wcp->data_size) {
991 return 0;
994 data_size = wcp->data_size;
995 wcp->data_size = 0;
997 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
999 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
1000 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
1002 #ifdef WITH_PROFILE
1003 if(data_size == wcp->alloc_size) {
1004 DO_PROFILE_INC(writecache_num_perfect_writes);
1006 #endif
1008 ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
1011 * Ensure file size if kept up to date if write extends file.
1014 if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
1015 wcp->file_size = wcp->offset + ret;
1018 return ret;
1021 /*******************************************************************
1022 sync a file
1023 ********************************************************************/
1025 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
1027 if (fsp->fh->fd == -1)
1028 return NT_STATUS_INVALID_HANDLE;
1030 if (lp_strict_sync(SNUM(conn)) &&
1031 (lp_syncalways(SNUM(conn)) || write_through)) {
1032 int ret = flush_write_cache(fsp, SYNC_FLUSH);
1033 if (ret == -1) {
1034 return map_nt_error_from_unix(errno);
1036 ret = SMB_VFS_FSYNC(fsp);
1037 if (ret == -1) {
1038 return map_nt_error_from_unix(errno);
1041 return NT_STATUS_OK;
1044 /************************************************************
1045 Perform a stat whether a valid fd or not.
1046 ************************************************************/
1048 int fsp_stat(files_struct *fsp)
1050 if (fsp->fh->fd == -1) {
1051 if (fsp->posix_open) {
1052 return SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1053 } else {
1054 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1056 } else {
1057 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);