s3-torture/denytest.c: replace cli_read_old() with cli_read()
[Samba/gebeck_regimport.git] / source3 / smbd / fileio.c
blob3b317f9a8630459b06d4cb7fba94e2a6c7dd8b20
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 static bool setup_write_cache(files_struct *, SMB_OFF_T);
30 /****************************************************************************
31 Read from write cache if we can.
32 ****************************************************************************/
34 static bool read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
36 write_cache *wcp = fsp->wcp;
38 if(!wcp) {
39 return False;
42 if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
43 return False;
46 memcpy(data, wcp->data + (pos - wcp->offset), n);
48 DO_PROFILE_INC(writecache_read_hits);
50 return True;
53 /****************************************************************************
54 Read from a file.
55 ****************************************************************************/
57 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
59 ssize_t ret=0,readret;
61 /* you can't read from print files */
62 if (fsp->print_file) {
63 errno = EBADF;
64 return -1;
68 * Serve from write cache if we can.
71 if(read_from_write_cache(fsp, data, pos, n)) {
72 fsp->fh->pos = pos + n;
73 fsp->fh->position_information = fsp->fh->pos;
74 return n;
77 flush_write_cache(fsp, READ_FLUSH);
79 fsp->fh->pos = pos;
81 if (n > 0) {
82 #ifdef DMF_FIX
83 int numretries = 3;
84 tryagain:
85 readret = SMB_VFS_PREAD(fsp,data,n,pos);
87 if (readret == -1) {
88 if ((errno == EAGAIN) && numretries) {
89 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
90 (void)sleep(10);
91 --numretries;
92 goto tryagain;
94 return -1;
96 #else /* NO DMF fix. */
97 readret = SMB_VFS_PREAD(fsp,data,n,pos);
99 if (readret == -1) {
100 return -1;
102 #endif
103 if (readret > 0) {
104 ret += readret;
108 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
109 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
111 fsp->fh->pos += ret;
112 fsp->fh->position_information = fsp->fh->pos;
114 return(ret);
117 /****************************************************************************
118 *Really* write to a file.
119 ****************************************************************************/
121 static ssize_t real_write_file(struct smb_request *req,
122 files_struct *fsp,
123 const char *data,
124 SMB_OFF_T pos,
125 size_t n)
127 ssize_t ret;
129 if (pos == -1) {
130 ret = vfs_write_data(req, fsp, data, n);
131 } else {
132 fsp->fh->pos = pos;
133 if (pos && lp_strict_allocate(SNUM(fsp->conn) &&
134 !fsp->is_sparse)) {
135 if (vfs_fill_sparse(fsp, pos) == -1) {
136 return -1;
139 ret = vfs_pwrite_data(req, fsp, data, n, pos);
142 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
143 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
145 if (ret != -1) {
146 fsp->fh->pos += ret;
148 /* Yes - this is correct - writes don't update this. JRA. */
149 /* Found by Samba4 tests. */
150 #if 0
151 fsp->position_information = fsp->pos;
152 #endif
155 return ret;
158 /****************************************************************************
159 File size cache change.
160 Updates size on disk but doesn't flush the cache.
161 ****************************************************************************/
163 static int wcp_file_size_change(files_struct *fsp)
165 int ret;
166 write_cache *wcp = fsp->wcp;
168 wcp->file_size = wcp->offset + wcp->data_size;
169 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
170 if (ret == -1) {
171 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
172 "error %s\n", fsp_str_dbg(fsp),
173 (double)wcp->file_size, strerror(errno)));
175 return ret;
178 void update_write_time_handler(struct event_context *ctx,
179 struct timed_event *te,
180 struct timeval now,
181 void *private_data)
183 files_struct *fsp = (files_struct *)private_data;
185 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp)));
187 /* change the write time in the open file db. */
188 (void)set_write_time(fsp->file_id, timespec_current());
190 /* And notify. */
191 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
192 FILE_NOTIFY_CHANGE_LAST_WRITE, fsp->fsp_name->base_name);
194 /* Remove the timed event handler. */
195 TALLOC_FREE(fsp->update_write_time_event);
198 /*********************************************************
199 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
200 in the future.
201 *********************************************************/
203 void trigger_write_time_update(struct files_struct *fsp)
205 int delay;
207 if (fsp->posix_open) {
208 /* Don't use delayed writes on POSIX files. */
209 return;
212 if (fsp->write_time_forced) {
213 /* No point - "sticky" write times
214 * in effect.
216 return;
219 /* We need to remember someone did a write
220 * and update to current time on close. */
222 fsp->update_write_time_on_close = true;
224 if (fsp->update_write_time_triggered) {
226 * We only update the write time after 2 seconds
227 * on the first normal write. After that
228 * no other writes affect this until close.
230 return;
232 fsp->update_write_time_triggered = true;
234 delay = lp_parm_int(SNUM(fsp->conn),
235 "smbd", "writetimeupdatedelay",
236 WRITE_TIME_UPDATE_USEC_DELAY);
238 DEBUG(5, ("Update write time %d usec later on %s\n",
239 delay, fsp_str_dbg(fsp)));
241 /* trigger the update 2 seconds later */
242 fsp->update_write_time_event =
243 event_add_timed(server_event_context(), NULL,
244 timeval_current_ofs_usec(delay),
245 update_write_time_handler, fsp);
248 void trigger_write_time_update_immediate(struct files_struct *fsp)
250 struct smb_file_time ft;
252 if (fsp->posix_open) {
253 /* Don't use delayed writes on POSIX files. */
254 return;
257 if (fsp->write_time_forced) {
259 * No point - "sticky" write times
260 * in effect.
262 return;
265 TALLOC_FREE(fsp->update_write_time_event);
266 DEBUG(5, ("Update write time immediate on %s\n",
267 fsp_str_dbg(fsp)));
269 /* After an immediate update, reset the trigger. */
270 fsp->update_write_time_triggered = true;
271 fsp->update_write_time_on_close = false;
273 ZERO_STRUCT(ft);
274 ft.mtime = timespec_current();
276 /* Update the time in the open file db. */
277 (void)set_write_time(fsp->file_id, ft.mtime);
279 /* Now set on disk - takes care of notify. */
280 (void)smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
283 /****************************************************************************
284 Write to a file.
285 ****************************************************************************/
287 ssize_t write_file(struct smb_request *req,
288 files_struct *fsp,
289 const char *data,
290 SMB_OFF_T pos,
291 size_t n)
293 write_cache *wcp = fsp->wcp;
294 ssize_t total_written = 0;
295 int write_path = -1;
297 if (fsp->print_file) {
298 uint32_t t;
299 int ret;
301 ret = print_spool_write(fsp, data, n, pos, &t);
302 if (ret) {
303 errno = ret;
304 return -1;
306 return t;
309 if (!fsp->can_write) {
310 errno = EPERM;
311 return -1;
314 if (!fsp->modified) {
315 fsp->modified = True;
317 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) == 0) {
318 trigger_write_time_update(fsp);
319 if (!fsp->posix_open &&
320 (lp_store_dos_attributes(SNUM(fsp->conn)) ||
321 MAP_ARCHIVE(fsp->conn))) {
322 int dosmode = dos_mode(fsp->conn, fsp->fsp_name);
323 if (!IS_DOS_ARCHIVE(dosmode)) {
324 file_set_dosmode(fsp->conn, fsp->fsp_name,
325 dosmode | FILE_ATTRIBUTE_ARCHIVE, NULL, false);
330 * If this is the first write and we have an exclusive oplock then setup
331 * the write cache.
334 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
335 setup_write_cache(fsp,
336 fsp->fsp_name->st.st_ex_size);
337 wcp = fsp->wcp;
342 #ifdef WITH_PROFILE
343 DO_PROFILE_INC(writecache_total_writes);
344 if (!fsp->oplock_type) {
345 DO_PROFILE_INC(writecache_non_oplock_writes);
347 #endif
350 * If this file is level II oplocked then we need
351 * to grab the shared memory lock and inform all
352 * other files with a level II lock that they need
353 * to flush their read caches. We keep the lock over
354 * the shared memory area whilst doing this.
357 /* This should actually be improved to span the write. */
358 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
359 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
361 #ifdef WITH_PROFILE
362 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
363 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
364 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
365 profile_p->writecache_init_writes,
366 profile_p->writecache_abutted_writes,
367 profile_p->writecache_total_writes,
368 profile_p->writecache_non_oplock_writes,
369 profile_p->writecache_allocated_write_caches,
370 profile_p->writecache_num_write_caches,
371 profile_p->writecache_direct_writes,
372 profile_p->writecache_num_perfect_writes,
373 profile_p->writecache_read_hits ));
375 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
376 profile_p->writecache_flushed_writes[SEEK_FLUSH],
377 profile_p->writecache_flushed_writes[READ_FLUSH],
378 profile_p->writecache_flushed_writes[WRITE_FLUSH],
379 profile_p->writecache_flushed_writes[READRAW_FLUSH],
380 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
381 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
382 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
384 #endif
386 if (wcp && req->unread_bytes) {
387 /* If we're using receivefile don't
388 * deal with a write cache.
390 flush_write_cache(fsp, WRITE_FLUSH);
391 delete_write_cache(fsp);
392 wcp = NULL;
395 if(!wcp) {
396 DO_PROFILE_INC(writecache_direct_writes);
397 total_written = real_write_file(req, fsp, data, pos, n);
398 return total_written;
401 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
402 "wcp->data_size=%u\n", fsp_str_dbg(fsp), fsp->fh->fd,
403 (double)pos, (unsigned int)n, (double)wcp->offset,
404 (unsigned int)wcp->data_size));
406 fsp->fh->pos = pos + n;
408 if ((n == 1) && (data[0] == '\0') && (pos > wcp->file_size)) {
409 int ret;
412 * This is a 1-byte write of a 0 beyond the EOF and
413 * thus implicitly also beyond the current active
414 * write cache, the typical file-extending (and
415 * allocating, but we're using the write cache here)
416 * write done by Windows. We just have to ftruncate
417 * the file and rely on posix semantics to return
418 * zeros for non-written file data that is within the
419 * file length.
421 * We can not use wcp_file_size_change here because we
422 * might have an existing write cache, and
423 * wcp_file_size_change assumes a change to just the
424 * end of the current write cache.
427 wcp->file_size = pos + 1;
428 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
429 if (ret == -1) {
430 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f"
431 "error %s\n", fsp_str_dbg(fsp),
432 (double)wcp->file_size, strerror(errno)));
433 return -1;
435 return 1;
440 * If we have active cache and it isn't contiguous then we flush.
441 * NOTE: There is a small problem with running out of disk ....
444 if (wcp->data_size) {
445 bool cache_flush_needed = False;
447 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
449 /* ASCII art.... JRA.
451 +--------------+-----
452 | Cached data | Rest of allocated cache buffer....
453 +--------------+-----
455 +-------------------+
456 | Data to write |
457 +-------------------+
462 * Start of write overlaps or abutts the existing data.
465 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
467 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
470 * Update the current buffer size with the new data.
473 if(pos + data_used > wcp->offset + wcp->data_size) {
474 wcp->data_size = pos + data_used - wcp->offset;
478 * Update the file size if changed.
481 if (wcp->offset + wcp->data_size > wcp->file_size) {
482 if (wcp_file_size_change(fsp) == -1) {
483 return -1;
488 * If we used all the data then
489 * return here.
492 if(n == data_used) {
493 return n;
494 } else {
495 cache_flush_needed = True;
498 * Move the start of data forward by the amount used,
499 * cut down the amount left by the same amount.
502 data += data_used;
503 pos += data_used;
504 n -= data_used;
506 DO_PROFILE_INC(writecache_abutted_writes);
507 total_written = data_used;
509 write_path = 1;
511 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
512 (pos + n <= wcp->offset + wcp->alloc_size)) {
514 /* ASCII art.... JRA.
516 +---------------+
517 | Cache buffer |
518 +---------------+
520 +-------------------+
521 | Data to write |
522 +-------------------+
527 * End of write overlaps the existing data.
530 size_t data_used = pos + n - wcp->offset;
532 memcpy(wcp->data, data + n - data_used, data_used);
535 * Update the current buffer size with the new data.
538 if(pos + n > wcp->offset + wcp->data_size) {
539 wcp->data_size = pos + n - wcp->offset;
543 * Update the file size if changed.
546 if (wcp->offset + wcp->data_size > wcp->file_size) {
547 if (wcp_file_size_change(fsp) == -1) {
548 return -1;
553 * We don't need to move the start of data, but we
554 * cut down the amount left by the amount used.
557 n -= data_used;
560 * We cannot have used all the data here.
563 cache_flush_needed = True;
565 DO_PROFILE_INC(writecache_abutted_writes);
566 total_written = data_used;
568 write_path = 2;
570 } else if ( (pos >= wcp->file_size) &&
571 (wcp->offset + wcp->data_size == wcp->file_size) &&
572 (pos > wcp->offset + wcp->data_size) &&
573 (pos < wcp->offset + wcp->alloc_size) ) {
575 /* ASCII art.... JRA.
577 End of file ---->|
579 +---------------+---------------+
580 | Cached data | Cache buffer |
581 +---------------+---------------+
583 +-------------------+
584 | Data to write |
585 +-------------------+
590 * Non-contiguous write part of which fits within
591 * the cache buffer and is extending the file
592 * and the cache contents reflect the current
593 * data up to the current end of the file.
596 size_t data_used;
598 if(pos + n <= wcp->offset + wcp->alloc_size) {
599 data_used = n;
600 } else {
601 data_used = wcp->offset + wcp->alloc_size - pos;
605 * Fill in the non-continuous area with zeros.
608 memset(wcp->data + wcp->data_size, '\0',
609 pos - (wcp->offset + wcp->data_size) );
611 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
614 * Update the current buffer size with the new data.
617 if(pos + data_used > wcp->offset + wcp->data_size) {
618 wcp->data_size = pos + data_used - wcp->offset;
622 * Update the file size if changed.
625 if (wcp->offset + wcp->data_size > wcp->file_size) {
626 if (wcp_file_size_change(fsp) == -1) {
627 return -1;
632 * If we used all the data then
633 * return here.
636 if(n == data_used) {
637 return n;
638 } else {
639 cache_flush_needed = True;
643 * Move the start of data forward by the amount used,
644 * cut down the amount left by the same amount.
647 data += data_used;
648 pos += data_used;
649 n -= data_used;
651 DO_PROFILE_INC(writecache_abutted_writes);
652 total_written = data_used;
654 write_path = 3;
656 } else if ( (pos >= wcp->file_size) &&
657 (n == 1) &&
658 (wcp->file_size == wcp->offset + wcp->data_size) &&
659 (pos < wcp->file_size + wcp->alloc_size)) {
663 End of file ---->|
665 +---------------+---------------+
666 | Cached data | Cache buffer |
667 +---------------+---------------+
669 |<------- allocated size ---------------->|
671 +--------+
672 | 1 Byte |
673 +--------+
675 MS-Office seems to do this a lot to determine if there's enough
676 space on the filesystem to write a new file.
678 Change to :
680 End of file ---->|
681 +-----------------------+--------+
682 | Zeroed Cached data | 1 Byte |
683 +-----------------------+--------+
686 flush_write_cache(fsp, WRITE_FLUSH);
687 wcp->offset = wcp->file_size;
688 wcp->data_size = pos - wcp->file_size + 1;
689 memset(wcp->data, '\0', wcp->data_size);
690 memcpy(wcp->data + wcp->data_size-1, data, 1);
693 * Update the file size if changed.
696 if (wcp->offset + wcp->data_size > wcp->file_size) {
697 if (wcp_file_size_change(fsp) == -1) {
698 return -1;
702 return n;
704 } else {
706 /* ASCII art..... JRA.
708 Case 1).
710 +---------------+---------------+
711 | Cached data | Cache buffer |
712 +---------------+---------------+
714 +-------------------+
715 | Data to write |
716 +-------------------+
718 Case 2).
720 +---------------+---------------+
721 | Cached data | Cache buffer |
722 +---------------+---------------+
724 +-------------------+
725 | Data to write |
726 +-------------------+
728 Case 3).
730 +---------------+---------------+
731 | Cached data | Cache buffer |
732 +---------------+---------------+
734 +-----------------------------------------------------+
735 | Data to write |
736 +-----------------------------------------------------+
741 * Write is bigger than buffer, or there is no overlap on the
742 * low or high ends.
745 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
746 len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
749 * If write would fit in the cache, and is larger than
750 * the data already in the cache, flush the cache and
751 * preferentially copy the data new data into it. Otherwise
752 * just write the data directly.
755 if ( n <= wcp->alloc_size && n > wcp->data_size) {
756 cache_flush_needed = True;
757 } else {
758 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
761 * If the write overlaps the entire cache, then
762 * discard the current contents of the cache.
763 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
766 if ((pos <= wcp->offset) &&
767 (pos + n >= wcp->offset + wcp->data_size) ) {
768 DEBUG(9,("write_file: discarding overwritten write \
769 cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
770 wcp->data_size = 0;
773 DO_PROFILE_INC(writecache_direct_writes);
774 if (ret == -1) {
775 return ret;
778 if (pos + ret > wcp->file_size) {
779 wcp->file_size = pos + ret;
782 return ret;
785 write_path = 4;
789 if (cache_flush_needed) {
790 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
791 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
792 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
793 (double)wcp->offset, (unsigned int)wcp->data_size ));
795 flush_write_cache(fsp, WRITE_FLUSH);
800 * If the write request is bigger than the cache
801 * size, write it all out.
804 if (n > wcp->alloc_size ) {
805 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
806 if (ret == -1) {
807 return -1;
810 if (pos + ret > wcp->file_size) {
811 wcp->file_size = pos + n;
814 DO_PROFILE_INC(writecache_direct_writes);
815 return total_written + n;
819 * If there's any data left, cache it.
822 if (n) {
823 #ifdef WITH_PROFILE
824 if (wcp->data_size) {
825 DO_PROFILE_INC(writecache_abutted_writes);
826 } else {
827 DO_PROFILE_INC(writecache_init_writes);
829 #endif
831 if ((wcp->data_size == 0)
832 && (pos > wcp->file_size)
833 && (pos + n <= wcp->file_size + wcp->alloc_size)) {
835 * This is a write completely beyond the
836 * current EOF, but within reach of the write
837 * cache. We expect fill-up writes pretty
838 * soon, so it does not make sense to start
839 * the write cache at the current
840 * offset. These fill-up writes would trigger
841 * separate pwrites or even unnecessary cache
842 * flushes because they overlap if this is a
843 * one-byte allocating write.
845 wcp->offset = wcp->file_size;
846 wcp->data_size = pos - wcp->file_size;
847 memset(wcp->data, 0, wcp->data_size);
850 memcpy(wcp->data+wcp->data_size, data, n);
851 if (wcp->data_size == 0) {
852 wcp->offset = pos;
853 DO_PROFILE_INC(writecache_num_write_caches);
855 wcp->data_size += n;
858 * Update the file size if changed.
861 if (wcp->offset + wcp->data_size > wcp->file_size) {
862 if (wcp_file_size_change(fsp) == -1) {
863 return -1;
866 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
867 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
869 total_written += n;
870 return total_written; /* .... that's a write :) */
873 return total_written;
876 /****************************************************************************
877 Delete the write cache structure.
878 ****************************************************************************/
880 void delete_write_cache(files_struct *fsp)
882 write_cache *wcp;
884 if(!fsp) {
885 return;
888 if(!(wcp = fsp->wcp)) {
889 return;
892 DO_PROFILE_DEC(writecache_allocated_write_caches);
893 allocated_write_caches--;
895 SMB_ASSERT(wcp->data_size == 0);
897 SAFE_FREE(wcp->data);
898 SAFE_FREE(fsp->wcp);
900 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
901 fsp_str_dbg(fsp)));
904 /****************************************************************************
905 Setup the write cache structure.
906 ****************************************************************************/
908 static bool setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
910 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
911 write_cache *wcp;
913 if (allocated_write_caches >= MAX_WRITE_CACHES) {
914 return False;
917 if(alloc_size == 0 || fsp->wcp) {
918 return False;
921 if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
922 DEBUG(0,("setup_write_cache: malloc fail.\n"));
923 return False;
926 wcp->file_size = file_size;
927 wcp->offset = 0;
928 wcp->alloc_size = alloc_size;
929 wcp->data_size = 0;
930 if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
931 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
932 (unsigned int)wcp->alloc_size ));
933 SAFE_FREE(wcp);
934 return False;
937 memset(wcp->data, '\0', wcp->alloc_size );
939 fsp->wcp = wcp;
940 DO_PROFILE_INC(writecache_allocated_write_caches);
941 allocated_write_caches++;
943 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
944 fsp_str_dbg(fsp), (unsigned long)wcp->alloc_size));
946 return True;
949 /****************************************************************************
950 Cope with a size change.
951 ****************************************************************************/
953 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
955 if(fsp->wcp) {
956 /* The cache *must* have been flushed before we do this. */
957 if (fsp->wcp->data_size != 0) {
958 char *msg;
959 if (asprintf(&msg, "set_filelen_write_cache: size change "
960 "on file %s with write cache size = %lu\n",
961 fsp->fsp_name->base_name,
962 (unsigned long)fsp->wcp->data_size) != -1) {
963 smb_panic(msg);
964 } else {
965 smb_panic("set_filelen_write_cache");
968 fsp->wcp->file_size = file_size;
972 /*******************************************************************
973 Flush a write cache struct to disk.
974 ********************************************************************/
976 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
978 write_cache *wcp = fsp->wcp;
979 size_t data_size;
980 ssize_t ret;
982 if(!wcp || !wcp->data_size) {
983 return 0;
986 data_size = wcp->data_size;
987 wcp->data_size = 0;
989 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
991 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
992 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
994 #ifdef WITH_PROFILE
995 if(data_size == wcp->alloc_size) {
996 DO_PROFILE_INC(writecache_num_perfect_writes);
998 #endif
1000 ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
1003 * Ensure file size if kept up to date if write extends file.
1006 if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
1007 wcp->file_size = wcp->offset + ret;
1010 return ret;
1013 /*******************************************************************
1014 sync a file
1015 ********************************************************************/
1017 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
1019 if (fsp->fh->fd == -1)
1020 return NT_STATUS_INVALID_HANDLE;
1022 if (lp_strict_sync(SNUM(conn)) &&
1023 (lp_syncalways(SNUM(conn)) || write_through)) {
1024 int ret = flush_write_cache(fsp, SYNC_FLUSH);
1025 if (ret == -1) {
1026 return map_nt_error_from_unix(errno);
1028 ret = SMB_VFS_FSYNC(fsp);
1029 if (ret == -1) {
1030 return map_nt_error_from_unix(errno);
1033 return NT_STATUS_OK;
1036 /************************************************************
1037 Perform a stat whether a valid fd or not.
1038 ************************************************************/
1040 int fsp_stat(files_struct *fsp)
1042 if (fsp->fh->fd == -1) {
1043 if (fsp->posix_open) {
1044 return SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1045 } else {
1046 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1048 } else {
1049 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);