s3-rpc_server: Use struct pipes_struct.
[Samba/gbeck.git] / source3 / smbd / fileio.c
blobe5a288822ecd52021a924b7d0b4ee13ab8329cb2
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/globals.h"
26 static bool setup_write_cache(files_struct *, SMB_OFF_T);
28 /****************************************************************************
29 Read from write cache if we can.
30 ****************************************************************************/
32 static bool read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
34 write_cache *wcp = fsp->wcp;
36 if(!wcp) {
37 return False;
40 if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
41 return False;
44 memcpy(data, wcp->data + (pos - wcp->offset), n);
46 DO_PROFILE_INC(writecache_read_hits);
48 return True;
51 /****************************************************************************
52 Read from a file.
53 ****************************************************************************/
55 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
57 ssize_t ret=0,readret;
59 /* you can't read from print files */
60 if (fsp->print_file) {
61 errno = EBADF;
62 return -1;
66 * Serve from write cache if we can.
69 if(read_from_write_cache(fsp, data, pos, n)) {
70 fsp->fh->pos = pos + n;
71 fsp->fh->position_information = fsp->fh->pos;
72 return n;
75 flush_write_cache(fsp, READ_FLUSH);
77 fsp->fh->pos = pos;
79 if (n > 0) {
80 #ifdef DMF_FIX
81 int numretries = 3;
82 tryagain:
83 readret = SMB_VFS_PREAD(fsp,data,n,pos);
85 if (readret == -1) {
86 if ((errno == EAGAIN) && numretries) {
87 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
88 (void)sleep(10);
89 --numretries;
90 goto tryagain;
92 return -1;
94 #else /* NO DMF fix. */
95 readret = SMB_VFS_PREAD(fsp,data,n,pos);
97 if (readret == -1) {
98 return -1;
100 #endif
101 if (readret > 0) {
102 ret += readret;
106 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
107 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
109 fsp->fh->pos += ret;
110 fsp->fh->position_information = fsp->fh->pos;
112 return(ret);
115 /****************************************************************************
116 *Really* write to a file.
117 ****************************************************************************/
119 static ssize_t real_write_file(struct smb_request *req,
120 files_struct *fsp,
121 const char *data,
122 SMB_OFF_T pos,
123 size_t n)
125 ssize_t ret;
127 if (pos == -1) {
128 ret = vfs_write_data(req, fsp, data, n);
129 } else {
130 fsp->fh->pos = pos;
131 if (pos && lp_strict_allocate(SNUM(fsp->conn))) {
132 if (vfs_fill_sparse(fsp, pos) == -1) {
133 return -1;
136 ret = vfs_pwrite_data(req, fsp, data, n, pos);
139 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
140 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
142 if (ret != -1) {
143 fsp->fh->pos += ret;
145 /* Yes - this is correct - writes don't update this. JRA. */
146 /* Found by Samba4 tests. */
147 #if 0
148 fsp->position_information = fsp->pos;
149 #endif
152 return ret;
155 /****************************************************************************
156 File size cache change.
157 Updates size on disk but doesn't flush the cache.
158 ****************************************************************************/
160 static int wcp_file_size_change(files_struct *fsp)
162 int ret;
163 write_cache *wcp = fsp->wcp;
165 wcp->file_size = wcp->offset + wcp->data_size;
166 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
167 if (ret == -1) {
168 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
169 "error %s\n", fsp_str_dbg(fsp),
170 (double)wcp->file_size, strerror(errno)));
172 return ret;
175 void update_write_time_handler(struct event_context *ctx,
176 struct timed_event *te,
177 struct timeval now,
178 void *private_data)
180 files_struct *fsp = (files_struct *)private_data;
182 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp)));
184 /* change the write time in the open file db. */
185 (void)set_write_time(fsp->file_id, timespec_current());
187 /* And notify. */
188 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
189 FILE_NOTIFY_CHANGE_LAST_WRITE, fsp->fsp_name->base_name);
191 /* Remove the timed event handler. */
192 TALLOC_FREE(fsp->update_write_time_event);
195 /*********************************************************
196 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
197 in the future.
198 *********************************************************/
200 void trigger_write_time_update(struct files_struct *fsp)
202 int delay;
204 if (fsp->posix_open) {
205 /* Don't use delayed writes on POSIX files. */
206 return;
209 if (fsp->write_time_forced) {
210 /* No point - "sticky" write times
211 * in effect.
213 return;
216 /* We need to remember someone did a write
217 * and update to current time on close. */
219 fsp->update_write_time_on_close = true;
221 if (fsp->update_write_time_triggered) {
223 * We only update the write time after 2 seconds
224 * on the first normal write. After that
225 * no other writes affect this until close.
227 return;
229 fsp->update_write_time_triggered = true;
231 delay = lp_parm_int(SNUM(fsp->conn),
232 "smbd", "writetimeupdatedelay",
233 WRITE_TIME_UPDATE_USEC_DELAY);
235 DEBUG(5, ("Update write time %d usec later on %s\n",
236 delay, fsp_str_dbg(fsp)));
238 /* trigger the update 2 seconds later */
239 fsp->update_write_time_event =
240 event_add_timed(smbd_event_context(), NULL,
241 timeval_current_ofs(0, delay),
242 update_write_time_handler, fsp);
245 void trigger_write_time_update_immediate(struct files_struct *fsp)
247 struct smb_file_time ft;
249 if (fsp->posix_open) {
250 /* Don't use delayed writes on POSIX files. */
251 return;
254 if (fsp->write_time_forced) {
256 * No point - "sticky" write times
257 * in effect.
259 return;
262 TALLOC_FREE(fsp->update_write_time_event);
263 DEBUG(5, ("Update write time immediate on %s\n",
264 fsp_str_dbg(fsp)));
266 /* After an immediate update, reset the trigger. */
267 fsp->update_write_time_triggered = true;
268 fsp->update_write_time_on_close = false;
270 ZERO_STRUCT(ft);
271 ft.mtime = timespec_current();
273 /* Update the time in the open file db. */
274 (void)set_write_time(fsp->file_id, ft.mtime);
276 /* Now set on disk - takes care of notify. */
277 (void)smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
280 /****************************************************************************
281 Write to a file.
282 ****************************************************************************/
284 ssize_t write_file(struct smb_request *req,
285 files_struct *fsp,
286 const char *data,
287 SMB_OFF_T pos,
288 size_t n)
290 write_cache *wcp = fsp->wcp;
291 ssize_t total_written = 0;
292 int write_path = -1;
294 if (fsp->print_file) {
295 uint32_t t;
296 int ret;
298 ret = print_spool_write(fsp, data, n, pos, &t);
299 if (ret) {
300 errno = ret;
301 return -1;
303 return t;
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 int dosmode;
316 trigger_write_time_update(fsp);
317 dosmode = dos_mode(fsp->conn, fsp->fsp_name);
318 if ((lp_store_dos_attributes(SNUM(fsp->conn)) ||
319 MAP_ARCHIVE(fsp->conn)) &&
320 !IS_DOS_ARCHIVE(dosmode)) {
321 file_set_dosmode(fsp->conn, fsp->fsp_name,
322 dosmode | aARCH, NULL, false);
326 * If this is the first write and we have an exclusive oplock then setup
327 * the write cache.
330 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
331 setup_write_cache(fsp,
332 fsp->fsp_name->st.st_ex_size);
333 wcp = fsp->wcp;
338 #ifdef WITH_PROFILE
339 DO_PROFILE_INC(writecache_total_writes);
340 if (!fsp->oplock_type) {
341 DO_PROFILE_INC(writecache_non_oplock_writes);
343 #endif
346 * If this file is level II oplocked then we need
347 * to grab the shared memory lock and inform all
348 * other files with a level II lock that they need
349 * to flush their read caches. We keep the lock over
350 * the shared memory area whilst doing this.
353 /* This should actually be improved to span the write. */
354 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
355 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
357 #ifdef WITH_PROFILE
358 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
359 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
360 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
361 profile_p->writecache_init_writes,
362 profile_p->writecache_abutted_writes,
363 profile_p->writecache_total_writes,
364 profile_p->writecache_non_oplock_writes,
365 profile_p->writecache_allocated_write_caches,
366 profile_p->writecache_num_write_caches,
367 profile_p->writecache_direct_writes,
368 profile_p->writecache_num_perfect_writes,
369 profile_p->writecache_read_hits ));
371 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
372 profile_p->writecache_flushed_writes[SEEK_FLUSH],
373 profile_p->writecache_flushed_writes[READ_FLUSH],
374 profile_p->writecache_flushed_writes[WRITE_FLUSH],
375 profile_p->writecache_flushed_writes[READRAW_FLUSH],
376 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
377 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
378 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
380 #endif
382 if (wcp && req->unread_bytes) {
383 /* If we're using receivefile don't
384 * deal with a write cache.
386 flush_write_cache(fsp, WRITE_FLUSH);
387 delete_write_cache(fsp);
388 wcp = NULL;
391 if(!wcp) {
392 DO_PROFILE_INC(writecache_direct_writes);
393 total_written = real_write_file(req, fsp, data, pos, n);
394 return total_written;
397 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
398 "wcp->data_size=%u\n", fsp_str_dbg(fsp), fsp->fh->fd,
399 (double)pos, (unsigned int)n, (double)wcp->offset,
400 (unsigned int)wcp->data_size));
402 fsp->fh->pos = pos + n;
405 * If we have active cache and it isn't contiguous then we flush.
406 * NOTE: There is a small problem with running out of disk ....
409 if (wcp->data_size) {
410 bool cache_flush_needed = False;
412 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
414 /* ASCII art.... JRA.
416 +--------------+-----
417 | Cached data | Rest of allocated cache buffer....
418 +--------------+-----
420 +-------------------+
421 | Data to write |
422 +-------------------+
427 * Start of write overlaps or abutts the existing data.
430 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
432 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
435 * Update the current buffer size with the new data.
438 if(pos + data_used > wcp->offset + wcp->data_size) {
439 wcp->data_size = pos + data_used - wcp->offset;
443 * Update the file size if changed.
446 if (wcp->offset + wcp->data_size > wcp->file_size) {
447 if (wcp_file_size_change(fsp) == -1) {
448 return -1;
453 * If we used all the data then
454 * return here.
457 if(n == data_used) {
458 return n;
459 } else {
460 cache_flush_needed = True;
463 * Move the start of data forward by the amount used,
464 * cut down the amount left by the same amount.
467 data += data_used;
468 pos += data_used;
469 n -= data_used;
471 DO_PROFILE_INC(writecache_abutted_writes);
472 total_written = data_used;
474 write_path = 1;
476 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
477 (pos + n <= wcp->offset + wcp->alloc_size)) {
479 /* ASCII art.... JRA.
481 +---------------+
482 | Cache buffer |
483 +---------------+
485 +-------------------+
486 | Data to write |
487 +-------------------+
492 * End of write overlaps the existing data.
495 size_t data_used = pos + n - wcp->offset;
497 memcpy(wcp->data, data + n - data_used, data_used);
500 * Update the current buffer size with the new data.
503 if(pos + n > wcp->offset + wcp->data_size) {
504 wcp->data_size = pos + n - wcp->offset;
508 * Update the file size if changed.
511 if (wcp->offset + wcp->data_size > wcp->file_size) {
512 if (wcp_file_size_change(fsp) == -1) {
513 return -1;
518 * We don't need to move the start of data, but we
519 * cut down the amount left by the amount used.
522 n -= data_used;
525 * We cannot have used all the data here.
528 cache_flush_needed = True;
530 DO_PROFILE_INC(writecache_abutted_writes);
531 total_written = data_used;
533 write_path = 2;
535 } else if ( (pos >= wcp->file_size) &&
536 (wcp->offset + wcp->data_size == wcp->file_size) &&
537 (pos > wcp->offset + wcp->data_size) &&
538 (pos < wcp->offset + wcp->alloc_size) ) {
540 /* ASCII art.... JRA.
542 End of file ---->|
544 +---------------+---------------+
545 | Cached data | Cache buffer |
546 +---------------+---------------+
548 +-------------------+
549 | Data to write |
550 +-------------------+
555 * Non-contiguous write part of which fits within
556 * the cache buffer and is extending the file
557 * and the cache contents reflect the current
558 * data up to the current end of the file.
561 size_t data_used;
563 if(pos + n <= wcp->offset + wcp->alloc_size) {
564 data_used = n;
565 } else {
566 data_used = wcp->offset + wcp->alloc_size - pos;
570 * Fill in the non-continuous area with zeros.
573 memset(wcp->data + wcp->data_size, '\0',
574 pos - (wcp->offset + wcp->data_size) );
576 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
579 * Update the current buffer size with the new data.
582 if(pos + data_used > wcp->offset + wcp->data_size) {
583 wcp->data_size = pos + data_used - wcp->offset;
587 * Update the file size if changed.
590 if (wcp->offset + wcp->data_size > wcp->file_size) {
591 if (wcp_file_size_change(fsp) == -1) {
592 return -1;
597 * If we used all the data then
598 * return here.
601 if(n == data_used) {
602 return n;
603 } else {
604 cache_flush_needed = True;
608 * Move the start of data forward by the amount used,
609 * cut down the amount left by the same amount.
612 data += data_used;
613 pos += data_used;
614 n -= data_used;
616 DO_PROFILE_INC(writecache_abutted_writes);
617 total_written = data_used;
619 write_path = 3;
621 } else if ( (pos >= wcp->file_size) &&
622 (n == 1) &&
623 (wcp->file_size == wcp->offset + wcp->data_size) &&
624 (pos < wcp->file_size + wcp->alloc_size)) {
628 End of file ---->|
630 +---------------+---------------+
631 | Cached data | Cache buffer |
632 +---------------+---------------+
634 |<------- allocated size ---------------->|
636 +--------+
637 | 1 Byte |
638 +--------+
640 MS-Office seems to do this a lot to determine if there's enough
641 space on the filesystem to write a new file.
643 Change to :
645 End of file ---->|
646 +-----------------------+--------+
647 | Zeroed Cached data | 1 Byte |
648 +-----------------------+--------+
651 flush_write_cache(fsp, WRITE_FLUSH);
653 if (data[0] == '\0') {
655 * This is a 1-byte write of a 0
656 * beyond the EOF, the typical
657 * file-extending (and allocating, but
658 * we're using the write cache here)
659 * write done by Windows. We just have
660 * to ftruncate the file and rely on
661 * posix semantics to return zeros for
662 * non-written file data that is
663 * within the file length.
665 * We have to cheat the offset to make
666 * wcp_file_size_change do the right
667 * thing with the ftruncate call.
669 wcp->offset = pos + 1;
670 wcp->data_size = 0;
671 } else {
672 wcp->offset = wcp->file_size;
673 wcp->data_size = pos - wcp->file_size + 1;
674 memset(wcp->data, '\0', wcp->data_size);
675 memcpy(wcp->data + wcp->data_size-1, data, 1);
679 * Update the file size if changed.
682 if (wcp->offset + wcp->data_size > wcp->file_size) {
683 if (wcp_file_size_change(fsp) == -1) {
684 return -1;
688 return n;
690 } else {
692 /* ASCII art..... JRA.
694 Case 1).
696 +---------------+---------------+
697 | Cached data | Cache buffer |
698 +---------------+---------------+
700 +-------------------+
701 | Data to write |
702 +-------------------+
704 Case 2).
706 +---------------+---------------+
707 | Cached data | Cache buffer |
708 +---------------+---------------+
710 +-------------------+
711 | Data to write |
712 +-------------------+
714 Case 3).
716 +---------------+---------------+
717 | Cached data | Cache buffer |
718 +---------------+---------------+
720 +-----------------------------------------------------+
721 | Data to write |
722 +-----------------------------------------------------+
727 * Write is bigger than buffer, or there is no overlap on the
728 * low or high ends.
731 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
732 len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
735 * If write would fit in the cache, and is larger than
736 * the data already in the cache, flush the cache and
737 * preferentially copy the data new data into it. Otherwise
738 * just write the data directly.
741 if ( n <= wcp->alloc_size && n > wcp->data_size) {
742 cache_flush_needed = True;
743 } else {
744 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
747 * If the write overlaps the entire cache, then
748 * discard the current contents of the cache.
749 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
752 if ((pos <= wcp->offset) &&
753 (pos + n >= wcp->offset + wcp->data_size) ) {
754 DEBUG(9,("write_file: discarding overwritten write \
755 cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
756 wcp->data_size = 0;
759 DO_PROFILE_INC(writecache_direct_writes);
760 if (ret == -1) {
761 return ret;
764 if (pos + ret > wcp->file_size) {
765 wcp->file_size = pos + ret;
768 return ret;
771 write_path = 4;
775 if (cache_flush_needed) {
776 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
777 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
778 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
779 (double)wcp->offset, (unsigned int)wcp->data_size ));
781 flush_write_cache(fsp, WRITE_FLUSH);
786 * If the write request is bigger than the cache
787 * size, write it all out.
790 if (n > wcp->alloc_size ) {
791 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
792 if (ret == -1) {
793 return -1;
796 if (pos + ret > wcp->file_size) {
797 wcp->file_size = pos + n;
800 DO_PROFILE_INC(writecache_direct_writes);
801 return total_written + n;
805 * If there's any data left, cache it.
808 if (n) {
809 #ifdef WITH_PROFILE
810 if (wcp->data_size) {
811 DO_PROFILE_INC(writecache_abutted_writes);
812 } else {
813 DO_PROFILE_INC(writecache_init_writes);
815 #endif
817 if ((wcp->data_size == 0)
818 && (pos > wcp->file_size)
819 && (n == 1)
820 && (data[0] == '\0')) {
822 * This is a 1-byte write of a 0 beyond the
823 * EOF, the typical file-extending (and
824 * allocating, but we're using the write cache
825 * here) write done by Windows. We just have
826 * to ftruncate the file and rely on posix
827 * semantics to return zeros for non-written
828 * file data that is within the file length.
830 * We have to cheat the offset to make
831 * wcp_file_size_change do the right thing
832 * with the ftruncate call.
834 wcp->offset = pos+1;
835 wcp->data_size = 0;
836 if (wcp_file_size_change(fsp) == -1) {
837 return -1;
839 return 1;
842 if ((wcp->data_size == 0)
843 && (pos > wcp->file_size)
844 && (pos + n <= wcp->file_size + wcp->alloc_size)) {
846 * This is a write completely beyond the
847 * current EOF, but within reach of the write
848 * cache. We expect fill-up writes pretty
849 * soon, so it does not make sense to start
850 * the write cache at the current
851 * offset. These fill-up writes would trigger
852 * separate pwrites or even unnecessary cache
853 * flushes because they overlap if this is a
854 * one-byte allocating write.
856 wcp->offset = wcp->file_size;
857 wcp->data_size = pos - wcp->file_size;
858 memset(wcp->data, 0, wcp->data_size);
861 memcpy(wcp->data+wcp->data_size, data, n);
862 if (wcp->data_size == 0) {
863 wcp->offset = pos;
864 DO_PROFILE_INC(writecache_num_write_caches);
866 wcp->data_size += n;
869 * Update the file size if changed.
872 if (wcp->offset + wcp->data_size > wcp->file_size) {
873 if (wcp_file_size_change(fsp) == -1) {
874 return -1;
877 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
878 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
880 total_written += n;
881 return total_written; /* .... that's a write :) */
884 return total_written;
887 /****************************************************************************
888 Delete the write cache structure.
889 ****************************************************************************/
891 void delete_write_cache(files_struct *fsp)
893 write_cache *wcp;
895 if(!fsp) {
896 return;
899 if(!(wcp = fsp->wcp)) {
900 return;
903 DO_PROFILE_DEC(writecache_allocated_write_caches);
904 allocated_write_caches--;
906 SMB_ASSERT(wcp->data_size == 0);
908 SAFE_FREE(wcp->data);
909 SAFE_FREE(fsp->wcp);
911 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
912 fsp_str_dbg(fsp)));
915 /****************************************************************************
916 Setup the write cache structure.
917 ****************************************************************************/
919 static bool setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
921 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
922 write_cache *wcp;
924 if (allocated_write_caches >= MAX_WRITE_CACHES) {
925 return False;
928 if(alloc_size == 0 || fsp->wcp) {
929 return False;
932 if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
933 DEBUG(0,("setup_write_cache: malloc fail.\n"));
934 return False;
937 wcp->file_size = file_size;
938 wcp->offset = 0;
939 wcp->alloc_size = alloc_size;
940 wcp->data_size = 0;
941 if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
942 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
943 (unsigned int)wcp->alloc_size ));
944 SAFE_FREE(wcp);
945 return False;
948 memset(wcp->data, '\0', wcp->alloc_size );
950 fsp->wcp = wcp;
951 DO_PROFILE_INC(writecache_allocated_write_caches);
952 allocated_write_caches++;
954 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
955 fsp_str_dbg(fsp), (unsigned long)wcp->alloc_size));
957 return True;
960 /****************************************************************************
961 Cope with a size change.
962 ****************************************************************************/
964 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
966 if(fsp->wcp) {
967 /* The cache *must* have been flushed before we do this. */
968 if (fsp->wcp->data_size != 0) {
969 char *msg;
970 if (asprintf(&msg, "set_filelen_write_cache: size change "
971 "on file %s with write cache size = %lu\n",
972 fsp->fsp_name->base_name,
973 (unsigned long)fsp->wcp->data_size) != -1) {
974 smb_panic(msg);
975 } else {
976 smb_panic("set_filelen_write_cache");
979 fsp->wcp->file_size = file_size;
983 /*******************************************************************
984 Flush a write cache struct to disk.
985 ********************************************************************/
987 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
989 write_cache *wcp = fsp->wcp;
990 size_t data_size;
991 ssize_t ret;
993 if(!wcp || !wcp->data_size) {
994 return 0;
997 data_size = wcp->data_size;
998 wcp->data_size = 0;
1000 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
1002 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
1003 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
1005 #ifdef WITH_PROFILE
1006 if(data_size == wcp->alloc_size) {
1007 DO_PROFILE_INC(writecache_num_perfect_writes);
1009 #endif
1011 ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
1014 * Ensure file size if kept up to date if write extends file.
1017 if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
1018 wcp->file_size = wcp->offset + ret;
1021 return ret;
1024 /*******************************************************************
1025 sync a file
1026 ********************************************************************/
1028 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
1030 if (fsp->fh->fd == -1)
1031 return NT_STATUS_INVALID_HANDLE;
1033 if (lp_strict_sync(SNUM(conn)) &&
1034 (lp_syncalways(SNUM(conn)) || write_through)) {
1035 int ret = flush_write_cache(fsp, SYNC_FLUSH);
1036 if (ret == -1) {
1037 return map_nt_error_from_unix(errno);
1039 ret = SMB_VFS_FSYNC(fsp);
1040 if (ret == -1) {
1041 return map_nt_error_from_unix(errno);
1044 return NT_STATUS_OK;
1047 /************************************************************
1048 Perform a stat whether a valid fd or not.
1049 ************************************************************/
1051 int fsp_stat(files_struct *fsp)
1053 if (fsp->fh->fd == -1) {
1054 if (fsp->posix_open) {
1055 return SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1056 } else {
1057 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1059 } else {
1060 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);