Add some debug in s4's winbind server
[Samba/gebeck_regimport.git] / source3 / smbd / fileio.c
blobda40013bc5b0242623bddda56b7bb56b48f85b9d
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 !fsp->is_sparse)) {
133 if (vfs_fill_sparse(fsp, pos) == -1) {
134 return -1;
137 ret = vfs_pwrite_data(req, fsp, data, n, pos);
140 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
141 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
143 if (ret != -1) {
144 fsp->fh->pos += ret;
146 /* Yes - this is correct - writes don't update this. JRA. */
147 /* Found by Samba4 tests. */
148 #if 0
149 fsp->position_information = fsp->pos;
150 #endif
153 return ret;
156 /****************************************************************************
157 File size cache change.
158 Updates size on disk but doesn't flush the cache.
159 ****************************************************************************/
161 static int wcp_file_size_change(files_struct *fsp)
163 int ret;
164 write_cache *wcp = fsp->wcp;
166 wcp->file_size = wcp->offset + wcp->data_size;
167 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
168 if (ret == -1) {
169 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
170 "error %s\n", fsp_str_dbg(fsp),
171 (double)wcp->file_size, strerror(errno)));
173 return ret;
176 void update_write_time_handler(struct event_context *ctx,
177 struct timed_event *te,
178 struct timeval now,
179 void *private_data)
181 files_struct *fsp = (files_struct *)private_data;
183 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp)));
185 /* change the write time in the open file db. */
186 (void)set_write_time(fsp->file_id, timespec_current());
188 /* And notify. */
189 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
190 FILE_NOTIFY_CHANGE_LAST_WRITE, fsp->fsp_name->base_name);
192 /* Remove the timed event handler. */
193 TALLOC_FREE(fsp->update_write_time_event);
196 /*********************************************************
197 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
198 in the future.
199 *********************************************************/
201 void trigger_write_time_update(struct files_struct *fsp)
203 int delay;
205 if (fsp->posix_open) {
206 /* Don't use delayed writes on POSIX files. */
207 return;
210 if (fsp->write_time_forced) {
211 /* No point - "sticky" write times
212 * in effect.
214 return;
217 /* We need to remember someone did a write
218 * and update to current time on close. */
220 fsp->update_write_time_on_close = true;
222 if (fsp->update_write_time_triggered) {
224 * We only update the write time after 2 seconds
225 * on the first normal write. After that
226 * no other writes affect this until close.
228 return;
230 fsp->update_write_time_triggered = true;
232 delay = lp_parm_int(SNUM(fsp->conn),
233 "smbd", "writetimeupdatedelay",
234 WRITE_TIME_UPDATE_USEC_DELAY);
236 DEBUG(5, ("Update write time %d usec later on %s\n",
237 delay, fsp_str_dbg(fsp)));
239 /* trigger the update 2 seconds later */
240 fsp->update_write_time_event =
241 event_add_timed(smbd_event_context(), NULL,
242 timeval_current_ofs(0, delay),
243 update_write_time_handler, fsp);
246 void trigger_write_time_update_immediate(struct files_struct *fsp)
248 struct smb_file_time ft;
250 if (fsp->posix_open) {
251 /* Don't use delayed writes on POSIX files. */
252 return;
255 if (fsp->write_time_forced) {
257 * No point - "sticky" write times
258 * in effect.
260 return;
263 TALLOC_FREE(fsp->update_write_time_event);
264 DEBUG(5, ("Update write time immediate on %s\n",
265 fsp_str_dbg(fsp)));
267 /* After an immediate update, reset the trigger. */
268 fsp->update_write_time_triggered = true;
269 fsp->update_write_time_on_close = false;
271 ZERO_STRUCT(ft);
272 ft.mtime = timespec_current();
274 /* Update the time in the open file db. */
275 (void)set_write_time(fsp->file_id, ft.mtime);
277 /* Now set on disk - takes care of notify. */
278 (void)smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
281 /****************************************************************************
282 Write to a file.
283 ****************************************************************************/
285 ssize_t write_file(struct smb_request *req,
286 files_struct *fsp,
287 const char *data,
288 SMB_OFF_T pos,
289 size_t n)
291 write_cache *wcp = fsp->wcp;
292 ssize_t total_written = 0;
293 int write_path = -1;
295 if (fsp->print_file) {
296 uint32_t t;
297 int ret;
299 ret = print_spool_write(fsp, data, n, pos, &t);
300 if (ret) {
301 errno = ret;
302 return -1;
304 return t;
307 if (!fsp->can_write) {
308 errno = EPERM;
309 return -1;
312 if (!fsp->modified) {
313 fsp->modified = True;
315 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) == 0) {
316 trigger_write_time_update(fsp);
317 if (!fsp->posix_open &&
318 (lp_store_dos_attributes(SNUM(fsp->conn)) ||
319 MAP_ARCHIVE(fsp->conn))) {
320 int dosmode = dos_mode(fsp->conn, fsp->fsp_name);
321 if (!IS_DOS_ARCHIVE(dosmode)) {
322 file_set_dosmode(fsp->conn, fsp->fsp_name,
323 dosmode | aARCH, NULL, false);
328 * If this is the first write and we have an exclusive oplock then setup
329 * the write cache.
332 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
333 setup_write_cache(fsp,
334 fsp->fsp_name->st.st_ex_size);
335 wcp = fsp->wcp;
340 #ifdef WITH_PROFILE
341 DO_PROFILE_INC(writecache_total_writes);
342 if (!fsp->oplock_type) {
343 DO_PROFILE_INC(writecache_non_oplock_writes);
345 #endif
348 * If this file is level II oplocked then we need
349 * to grab the shared memory lock and inform all
350 * other files with a level II lock that they need
351 * to flush their read caches. We keep the lock over
352 * the shared memory area whilst doing this.
355 /* This should actually be improved to span the write. */
356 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
357 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
359 #ifdef WITH_PROFILE
360 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
361 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
362 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
363 profile_p->writecache_init_writes,
364 profile_p->writecache_abutted_writes,
365 profile_p->writecache_total_writes,
366 profile_p->writecache_non_oplock_writes,
367 profile_p->writecache_allocated_write_caches,
368 profile_p->writecache_num_write_caches,
369 profile_p->writecache_direct_writes,
370 profile_p->writecache_num_perfect_writes,
371 profile_p->writecache_read_hits ));
373 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
374 profile_p->writecache_flushed_writes[SEEK_FLUSH],
375 profile_p->writecache_flushed_writes[READ_FLUSH],
376 profile_p->writecache_flushed_writes[WRITE_FLUSH],
377 profile_p->writecache_flushed_writes[READRAW_FLUSH],
378 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
379 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
380 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
382 #endif
384 if (wcp && req->unread_bytes) {
385 /* If we're using receivefile don't
386 * deal with a write cache.
388 flush_write_cache(fsp, WRITE_FLUSH);
389 delete_write_cache(fsp);
390 wcp = NULL;
393 if(!wcp) {
394 DO_PROFILE_INC(writecache_direct_writes);
395 total_written = real_write_file(req, fsp, data, pos, n);
396 return total_written;
399 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
400 "wcp->data_size=%u\n", fsp_str_dbg(fsp), fsp->fh->fd,
401 (double)pos, (unsigned int)n, (double)wcp->offset,
402 (unsigned int)wcp->data_size));
404 fsp->fh->pos = pos + n;
406 if ((n == 1) && (data[0] == '\0') && (pos > wcp->file_size)) {
407 int ret;
410 * This is a 1-byte write of a 0 beyond the EOF and
411 * thus implicitly also beyond the current active
412 * write cache, the typical file-extending (and
413 * allocating, but we're using the write cache here)
414 * write done by Windows. We just have to ftruncate
415 * the file and rely on posix semantics to return
416 * zeros for non-written file data that is within the
417 * file length.
419 * We can not use wcp_file_size_change here because we
420 * might have an existing write cache, and
421 * wcp_file_size_change assumes a change to just the
422 * end of the current write cache.
425 wcp->file_size = pos + 1;
426 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
427 if (ret == -1) {
428 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f"
429 "error %s\n", fsp_str_dbg(fsp),
430 (double)wcp->file_size, strerror(errno)));
431 return -1;
433 return 1;
438 * If we have active cache and it isn't contiguous then we flush.
439 * NOTE: There is a small problem with running out of disk ....
442 if (wcp->data_size) {
443 bool cache_flush_needed = False;
445 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
447 /* ASCII art.... JRA.
449 +--------------+-----
450 | Cached data | Rest of allocated cache buffer....
451 +--------------+-----
453 +-------------------+
454 | Data to write |
455 +-------------------+
460 * Start of write overlaps or abutts the existing data.
463 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
465 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
468 * Update the current buffer size with the new data.
471 if(pos + data_used > wcp->offset + wcp->data_size) {
472 wcp->data_size = pos + data_used - wcp->offset;
476 * Update the file size if changed.
479 if (wcp->offset + wcp->data_size > wcp->file_size) {
480 if (wcp_file_size_change(fsp) == -1) {
481 return -1;
486 * If we used all the data then
487 * return here.
490 if(n == data_used) {
491 return n;
492 } else {
493 cache_flush_needed = True;
496 * Move the start of data forward by the amount used,
497 * cut down the amount left by the same amount.
500 data += data_used;
501 pos += data_used;
502 n -= data_used;
504 DO_PROFILE_INC(writecache_abutted_writes);
505 total_written = data_used;
507 write_path = 1;
509 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
510 (pos + n <= wcp->offset + wcp->alloc_size)) {
512 /* ASCII art.... JRA.
514 +---------------+
515 | Cache buffer |
516 +---------------+
518 +-------------------+
519 | Data to write |
520 +-------------------+
525 * End of write overlaps the existing data.
528 size_t data_used = pos + n - wcp->offset;
530 memcpy(wcp->data, data + n - data_used, data_used);
533 * Update the current buffer size with the new data.
536 if(pos + n > wcp->offset + wcp->data_size) {
537 wcp->data_size = pos + n - wcp->offset;
541 * Update the file size if changed.
544 if (wcp->offset + wcp->data_size > wcp->file_size) {
545 if (wcp_file_size_change(fsp) == -1) {
546 return -1;
551 * We don't need to move the start of data, but we
552 * cut down the amount left by the amount used.
555 n -= data_used;
558 * We cannot have used all the data here.
561 cache_flush_needed = True;
563 DO_PROFILE_INC(writecache_abutted_writes);
564 total_written = data_used;
566 write_path = 2;
568 } else if ( (pos >= wcp->file_size) &&
569 (wcp->offset + wcp->data_size == wcp->file_size) &&
570 (pos > wcp->offset + wcp->data_size) &&
571 (pos < wcp->offset + wcp->alloc_size) ) {
573 /* ASCII art.... JRA.
575 End of file ---->|
577 +---------------+---------------+
578 | Cached data | Cache buffer |
579 +---------------+---------------+
581 +-------------------+
582 | Data to write |
583 +-------------------+
588 * Non-contiguous write part of which fits within
589 * the cache buffer and is extending the file
590 * and the cache contents reflect the current
591 * data up to the current end of the file.
594 size_t data_used;
596 if(pos + n <= wcp->offset + wcp->alloc_size) {
597 data_used = n;
598 } else {
599 data_used = wcp->offset + wcp->alloc_size - pos;
603 * Fill in the non-continuous area with zeros.
606 memset(wcp->data + wcp->data_size, '\0',
607 pos - (wcp->offset + wcp->data_size) );
609 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
612 * Update the current buffer size with the new data.
615 if(pos + data_used > wcp->offset + wcp->data_size) {
616 wcp->data_size = pos + data_used - wcp->offset;
620 * Update the file size if changed.
623 if (wcp->offset + wcp->data_size > wcp->file_size) {
624 if (wcp_file_size_change(fsp) == -1) {
625 return -1;
630 * If we used all the data then
631 * return here.
634 if(n == data_used) {
635 return n;
636 } else {
637 cache_flush_needed = True;
641 * Move the start of data forward by the amount used,
642 * cut down the amount left by the same amount.
645 data += data_used;
646 pos += data_used;
647 n -= data_used;
649 DO_PROFILE_INC(writecache_abutted_writes);
650 total_written = data_used;
652 write_path = 3;
654 } else if ( (pos >= wcp->file_size) &&
655 (n == 1) &&
656 (wcp->file_size == wcp->offset + wcp->data_size) &&
657 (pos < wcp->file_size + wcp->alloc_size)) {
661 End of file ---->|
663 +---------------+---------------+
664 | Cached data | Cache buffer |
665 +---------------+---------------+
667 |<------- allocated size ---------------->|
669 +--------+
670 | 1 Byte |
671 +--------+
673 MS-Office seems to do this a lot to determine if there's enough
674 space on the filesystem to write a new file.
676 Change to :
678 End of file ---->|
679 +-----------------------+--------+
680 | Zeroed Cached data | 1 Byte |
681 +-----------------------+--------+
684 flush_write_cache(fsp, WRITE_FLUSH);
685 wcp->offset = wcp->file_size;
686 wcp->data_size = pos - wcp->file_size + 1;
687 memset(wcp->data, '\0', wcp->data_size);
688 memcpy(wcp->data + wcp->data_size-1, data, 1);
691 * Update the file size if changed.
694 if (wcp->offset + wcp->data_size > wcp->file_size) {
695 if (wcp_file_size_change(fsp) == -1) {
696 return -1;
700 return n;
702 } else {
704 /* ASCII art..... JRA.
706 Case 1).
708 +---------------+---------------+
709 | Cached data | Cache buffer |
710 +---------------+---------------+
712 +-------------------+
713 | Data to write |
714 +-------------------+
716 Case 2).
718 +---------------+---------------+
719 | Cached data | Cache buffer |
720 +---------------+---------------+
722 +-------------------+
723 | Data to write |
724 +-------------------+
726 Case 3).
728 +---------------+---------------+
729 | Cached data | Cache buffer |
730 +---------------+---------------+
732 +-----------------------------------------------------+
733 | Data to write |
734 +-----------------------------------------------------+
739 * Write is bigger than buffer, or there is no overlap on the
740 * low or high ends.
743 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
744 len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
747 * If write would fit in the cache, and is larger than
748 * the data already in the cache, flush the cache and
749 * preferentially copy the data new data into it. Otherwise
750 * just write the data directly.
753 if ( n <= wcp->alloc_size && n > wcp->data_size) {
754 cache_flush_needed = True;
755 } else {
756 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
759 * If the write overlaps the entire cache, then
760 * discard the current contents of the cache.
761 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
764 if ((pos <= wcp->offset) &&
765 (pos + n >= wcp->offset + wcp->data_size) ) {
766 DEBUG(9,("write_file: discarding overwritten write \
767 cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
768 wcp->data_size = 0;
771 DO_PROFILE_INC(writecache_direct_writes);
772 if (ret == -1) {
773 return ret;
776 if (pos + ret > wcp->file_size) {
777 wcp->file_size = pos + ret;
780 return ret;
783 write_path = 4;
787 if (cache_flush_needed) {
788 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
789 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
790 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
791 (double)wcp->offset, (unsigned int)wcp->data_size ));
793 flush_write_cache(fsp, WRITE_FLUSH);
798 * If the write request is bigger than the cache
799 * size, write it all out.
802 if (n > wcp->alloc_size ) {
803 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
804 if (ret == -1) {
805 return -1;
808 if (pos + ret > wcp->file_size) {
809 wcp->file_size = pos + n;
812 DO_PROFILE_INC(writecache_direct_writes);
813 return total_written + n;
817 * If there's any data left, cache it.
820 if (n) {
821 #ifdef WITH_PROFILE
822 if (wcp->data_size) {
823 DO_PROFILE_INC(writecache_abutted_writes);
824 } else {
825 DO_PROFILE_INC(writecache_init_writes);
827 #endif
829 if ((wcp->data_size == 0)
830 && (pos > wcp->file_size)
831 && (pos + n <= wcp->file_size + wcp->alloc_size)) {
833 * This is a write completely beyond the
834 * current EOF, but within reach of the write
835 * cache. We expect fill-up writes pretty
836 * soon, so it does not make sense to start
837 * the write cache at the current
838 * offset. These fill-up writes would trigger
839 * separate pwrites or even unnecessary cache
840 * flushes because they overlap if this is a
841 * one-byte allocating write.
843 wcp->offset = wcp->file_size;
844 wcp->data_size = pos - wcp->file_size;
845 memset(wcp->data, 0, wcp->data_size);
848 memcpy(wcp->data+wcp->data_size, data, n);
849 if (wcp->data_size == 0) {
850 wcp->offset = pos;
851 DO_PROFILE_INC(writecache_num_write_caches);
853 wcp->data_size += n;
856 * Update the file size if changed.
859 if (wcp->offset + wcp->data_size > wcp->file_size) {
860 if (wcp_file_size_change(fsp) == -1) {
861 return -1;
864 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
865 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
867 total_written += n;
868 return total_written; /* .... that's a write :) */
871 return total_written;
874 /****************************************************************************
875 Delete the write cache structure.
876 ****************************************************************************/
878 void delete_write_cache(files_struct *fsp)
880 write_cache *wcp;
882 if(!fsp) {
883 return;
886 if(!(wcp = fsp->wcp)) {
887 return;
890 DO_PROFILE_DEC(writecache_allocated_write_caches);
891 allocated_write_caches--;
893 SMB_ASSERT(wcp->data_size == 0);
895 SAFE_FREE(wcp->data);
896 SAFE_FREE(fsp->wcp);
898 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
899 fsp_str_dbg(fsp)));
902 /****************************************************************************
903 Setup the write cache structure.
904 ****************************************************************************/
906 static bool setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
908 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
909 write_cache *wcp;
911 if (allocated_write_caches >= MAX_WRITE_CACHES) {
912 return False;
915 if(alloc_size == 0 || fsp->wcp) {
916 return False;
919 if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
920 DEBUG(0,("setup_write_cache: malloc fail.\n"));
921 return False;
924 wcp->file_size = file_size;
925 wcp->offset = 0;
926 wcp->alloc_size = alloc_size;
927 wcp->data_size = 0;
928 if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
929 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
930 (unsigned int)wcp->alloc_size ));
931 SAFE_FREE(wcp);
932 return False;
935 memset(wcp->data, '\0', wcp->alloc_size );
937 fsp->wcp = wcp;
938 DO_PROFILE_INC(writecache_allocated_write_caches);
939 allocated_write_caches++;
941 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
942 fsp_str_dbg(fsp), (unsigned long)wcp->alloc_size));
944 return True;
947 /****************************************************************************
948 Cope with a size change.
949 ****************************************************************************/
951 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
953 if(fsp->wcp) {
954 /* The cache *must* have been flushed before we do this. */
955 if (fsp->wcp->data_size != 0) {
956 char *msg;
957 if (asprintf(&msg, "set_filelen_write_cache: size change "
958 "on file %s with write cache size = %lu\n",
959 fsp->fsp_name->base_name,
960 (unsigned long)fsp->wcp->data_size) != -1) {
961 smb_panic(msg);
962 } else {
963 smb_panic("set_filelen_write_cache");
966 fsp->wcp->file_size = file_size;
970 /*******************************************************************
971 Flush a write cache struct to disk.
972 ********************************************************************/
974 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
976 write_cache *wcp = fsp->wcp;
977 size_t data_size;
978 ssize_t ret;
980 if(!wcp || !wcp->data_size) {
981 return 0;
984 data_size = wcp->data_size;
985 wcp->data_size = 0;
987 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
989 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
990 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
992 #ifdef WITH_PROFILE
993 if(data_size == wcp->alloc_size) {
994 DO_PROFILE_INC(writecache_num_perfect_writes);
996 #endif
998 ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
1001 * Ensure file size if kept up to date if write extends file.
1004 if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
1005 wcp->file_size = wcp->offset + ret;
1008 return ret;
1011 /*******************************************************************
1012 sync a file
1013 ********************************************************************/
1015 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
1017 if (fsp->fh->fd == -1)
1018 return NT_STATUS_INVALID_HANDLE;
1020 if (lp_strict_sync(SNUM(conn)) &&
1021 (lp_syncalways(SNUM(conn)) || write_through)) {
1022 int ret = flush_write_cache(fsp, SYNC_FLUSH);
1023 if (ret == -1) {
1024 return map_nt_error_from_unix(errno);
1026 ret = SMB_VFS_FSYNC(fsp);
1027 if (ret == -1) {
1028 return map_nt_error_from_unix(errno);
1031 return NT_STATUS_OK;
1034 /************************************************************
1035 Perform a stat whether a valid fd or not.
1036 ************************************************************/
1038 int fsp_stat(files_struct *fsp)
1040 if (fsp->fh->fd == -1) {
1041 if (fsp->posix_open) {
1042 return SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1043 } else {
1044 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1046 } else {
1047 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);