2 Unix SMB/Netbios implementation.
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/>.
23 #include "smbd/globals.h"
25 static bool setup_write_cache(files_struct *, SMB_OFF_T);
27 /****************************************************************************
28 Read from write cache if we can.
29 ****************************************************************************/
31 static bool read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
33 write_cache *wcp = fsp->wcp;
39 if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
43 memcpy(data, wcp->data + (pos - wcp->offset), n);
45 DO_PROFILE_INC(writecache_read_hits);
50 /****************************************************************************
52 ****************************************************************************/
54 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
56 ssize_t ret=0,readret;
58 /* you can't read from print files */
59 if (fsp->print_file) {
65 * Serve from write cache if we can.
68 if(read_from_write_cache(fsp, data, pos, n)) {
69 fsp->fh->pos = pos + n;
70 fsp->fh->position_information = fsp->fh->pos;
74 flush_write_cache(fsp, READ_FLUSH);
82 readret = SMB_VFS_PREAD(fsp,data,n,pos);
85 if ((errno == EAGAIN) && numretries) {
86 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
93 #else /* NO DMF fix. */
94 readret = SMB_VFS_PREAD(fsp,data,n,pos);
105 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
106 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
109 fsp->fh->position_information = fsp->fh->pos;
114 /****************************************************************************
115 *Really* write to a file.
116 ****************************************************************************/
118 static ssize_t real_write_file(struct smb_request *req,
127 ret = vfs_write_data(req, fsp, data, n);
130 if (pos && lp_strict_allocate(SNUM(fsp->conn))) {
131 if (vfs_fill_sparse(fsp, pos) == -1) {
135 ret = vfs_pwrite_data(req, fsp, data, n, pos);
138 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
139 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
144 /* Yes - this is correct - writes don't update this. JRA. */
145 /* Found by Samba4 tests. */
147 fsp->position_information = fsp->pos;
154 /****************************************************************************
155 File size cache change.
156 Updates size on disk but doesn't flush the cache.
157 ****************************************************************************/
159 static int wcp_file_size_change(files_struct *fsp)
162 write_cache *wcp = fsp->wcp;
164 wcp->file_size = wcp->offset + wcp->data_size;
165 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
167 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f error %s\n",
168 fsp->fsp_name, (double)wcp->file_size, strerror(errno) ));
173 static void update_write_time_handler(struct event_context *ctx,
174 struct timed_event *te,
178 files_struct *fsp = (files_struct *)private_data;
180 /* Remove the timed event handler. */
181 TALLOC_FREE(fsp->update_write_time_event);
182 DEBUG(5, ("Update write time on %s\n", fsp->fsp_name));
184 /* change the write time if not already changed by someone else */
185 update_write_time(fsp);
188 /*********************************************************
189 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
191 *********************************************************/
193 void trigger_write_time_update(struct files_struct *fsp)
197 if (fsp->posix_open) {
198 /* Don't use delayed writes on POSIX files. */
202 if (fsp->write_time_forced) {
203 /* No point - "sticky" write times
209 if (fsp->update_write_time_triggered) {
211 * We only update the write time
212 * on the first write. After that
213 * no other writes affect this.
217 fsp->update_write_time_triggered = true;
219 delay = lp_parm_int(SNUM(fsp->conn),
220 "smbd", "writetimeupdatedelay",
221 WRITE_TIME_UPDATE_USEC_DELAY);
223 /* trigger the update 2 seconds later */
224 fsp->update_write_time_on_close = true;
225 fsp->update_write_time_event =
226 event_add_timed(smbd_event_context(), NULL,
227 timeval_current_ofs(0, delay),
228 update_write_time_handler, fsp);
231 void trigger_write_time_update_immediate(struct files_struct *fsp)
233 if (fsp->posix_open) {
234 /* Don't use delayed writes on POSIX files. */
238 if (fsp->write_time_forced) {
240 * No point - "sticky" write times
246 TALLOC_FREE(fsp->update_write_time_event);
247 DEBUG(5, ("Update write time immediate on %s\n", fsp->fsp_name));
249 fsp->update_write_time_triggered = true;
251 fsp->update_write_time_on_close = false;
252 update_write_time(fsp);
255 /****************************************************************************
257 ****************************************************************************/
259 ssize_t write_file(struct smb_request *req,
265 write_cache *wcp = fsp->wcp;
266 ssize_t total_written = 0;
269 if (fsp->print_file) {
272 if (!rap_to_pjobid(fsp->rap_print_jobid, NULL, &jobid)) {
273 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
274 (unsigned int)fsp->rap_print_jobid ));
279 return print_job_write(SNUM(fsp->conn), jobid, data, pos, n);
282 if (!fsp->can_write) {
287 if (!fsp->modified) {
288 struct smb_filename *smb_fname = NULL;
291 fsp->modified = True;
293 status = create_synthetic_smb_fname_split(talloc_tos(),
296 if (!NT_STATUS_IS_OK(status)) {
297 errno = map_errno_from_nt_status(status);
301 if (SMB_VFS_FSTAT(fsp, &smb_fname->st) == 0) {
303 trigger_write_time_update(fsp);
304 dosmode = dos_mode(fsp->conn, smb_fname);
305 if ((lp_store_dos_attributes(SNUM(fsp->conn)) ||
306 MAP_ARCHIVE(fsp->conn)) &&
307 !IS_DOS_ARCHIVE(dosmode)) {
308 file_set_dosmode(fsp->conn, smb_fname,
309 dosmode | aARCH, NULL, false);
313 * If this is the first write and we have an exclusive oplock then setup
317 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
318 setup_write_cache(fsp,
319 smb_fname->st.st_ex_size);
323 TALLOC_FREE(smb_fname);
327 DO_PROFILE_INC(writecache_total_writes);
328 if (!fsp->oplock_type) {
329 DO_PROFILE_INC(writecache_non_oplock_writes);
334 * If this file is level II oplocked then we need
335 * to grab the shared memory lock and inform all
336 * other files with a level II lock that they need
337 * to flush their read caches. We keep the lock over
338 * the shared memory area whilst doing this.
341 /* This should actually be improved to span the write. */
342 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
343 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
346 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
347 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
348 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
349 profile_p->writecache_init_writes,
350 profile_p->writecache_abutted_writes,
351 profile_p->writecache_total_writes,
352 profile_p->writecache_non_oplock_writes,
353 profile_p->writecache_allocated_write_caches,
354 profile_p->writecache_num_write_caches,
355 profile_p->writecache_direct_writes,
356 profile_p->writecache_num_perfect_writes,
357 profile_p->writecache_read_hits ));
359 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
360 profile_p->writecache_flushed_writes[SEEK_FLUSH],
361 profile_p->writecache_flushed_writes[READ_FLUSH],
362 profile_p->writecache_flushed_writes[WRITE_FLUSH],
363 profile_p->writecache_flushed_writes[READRAW_FLUSH],
364 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
365 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
366 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
370 if (wcp && req->unread_bytes) {
371 /* If we're using receivefile don't
372 * deal with a write cache.
374 flush_write_cache(fsp, WRITE_FLUSH);
375 delete_write_cache(fsp);
380 DO_PROFILE_INC(writecache_direct_writes);
381 total_written = real_write_file(req, fsp, data, pos, n);
382 return total_written;
385 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
386 fsp->fsp_name, fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
388 fsp->fh->pos = pos + n;
391 * If we have active cache and it isn't contiguous then we flush.
392 * NOTE: There is a small problem with running out of disk ....
395 if (wcp->data_size) {
396 bool cache_flush_needed = False;
398 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
400 /* ASCII art.... JRA.
402 +--------------+-----
403 | Cached data | Rest of allocated cache buffer....
404 +--------------+-----
406 +-------------------+
408 +-------------------+
413 * Start of write overlaps or abutts the existing data.
416 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
418 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
421 * Update the current buffer size with the new data.
424 if(pos + data_used > wcp->offset + wcp->data_size) {
425 wcp->data_size = pos + data_used - wcp->offset;
429 * Update the file size if changed.
432 if (wcp->offset + wcp->data_size > wcp->file_size) {
433 if (wcp_file_size_change(fsp) == -1) {
439 * If we used all the data then
446 cache_flush_needed = True;
449 * Move the start of data forward by the amount used,
450 * cut down the amount left by the same amount.
457 DO_PROFILE_INC(writecache_abutted_writes);
458 total_written = data_used;
462 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
463 (pos + n <= wcp->offset + wcp->alloc_size)) {
465 /* ASCII art.... JRA.
471 +-------------------+
473 +-------------------+
478 * End of write overlaps the existing data.
481 size_t data_used = pos + n - wcp->offset;
483 memcpy(wcp->data, data + n - data_used, data_used);
486 * Update the current buffer size with the new data.
489 if(pos + n > wcp->offset + wcp->data_size) {
490 wcp->data_size = pos + n - wcp->offset;
494 * Update the file size if changed.
497 if (wcp->offset + wcp->data_size > wcp->file_size) {
498 if (wcp_file_size_change(fsp) == -1) {
504 * We don't need to move the start of data, but we
505 * cut down the amount left by the amount used.
511 * We cannot have used all the data here.
514 cache_flush_needed = True;
516 DO_PROFILE_INC(writecache_abutted_writes);
517 total_written = data_used;
521 } else if ( (pos >= wcp->file_size) &&
522 (wcp->offset + wcp->data_size == wcp->file_size) &&
523 (pos > wcp->offset + wcp->data_size) &&
524 (pos < wcp->offset + wcp->alloc_size) ) {
526 /* ASCII art.... JRA.
530 +---------------+---------------+
531 | Cached data | Cache buffer |
532 +---------------+---------------+
534 +-------------------+
536 +-------------------+
541 * Non-contiguous write part of which fits within
542 * the cache buffer and is extending the file
543 * and the cache contents reflect the current
544 * data up to the current end of the file.
549 if(pos + n <= wcp->offset + wcp->alloc_size) {
552 data_used = wcp->offset + wcp->alloc_size - pos;
556 * Fill in the non-continuous area with zeros.
559 memset(wcp->data + wcp->data_size, '\0',
560 pos - (wcp->offset + wcp->data_size) );
562 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
565 * Update the current buffer size with the new data.
568 if(pos + data_used > wcp->offset + wcp->data_size) {
569 wcp->data_size = pos + data_used - wcp->offset;
573 * Update the file size if changed.
576 if (wcp->offset + wcp->data_size > wcp->file_size) {
577 if (wcp_file_size_change(fsp) == -1) {
583 * If we used all the data then
590 cache_flush_needed = True;
594 * Move the start of data forward by the amount used,
595 * cut down the amount left by the same amount.
602 DO_PROFILE_INC(writecache_abutted_writes);
603 total_written = data_used;
607 } else if ( (pos >= wcp->file_size) &&
609 (wcp->file_size == wcp->offset + wcp->data_size) &&
610 (pos < wcp->file_size + wcp->alloc_size)) {
616 +---------------+---------------+
617 | Cached data | Cache buffer |
618 +---------------+---------------+
620 |<------- allocated size ---------------->|
626 MS-Office seems to do this a lot to determine if there's enough
627 space on the filesystem to write a new file.
632 +-----------------------+--------+
633 | Zeroed Cached data | 1 Byte |
634 +-----------------------+--------+
637 flush_write_cache(fsp, WRITE_FLUSH);
638 wcp->offset = wcp->file_size;
639 wcp->data_size = pos - wcp->file_size + 1;
640 memset(wcp->data, '\0', wcp->data_size);
641 memcpy(wcp->data + wcp->data_size-1, data, 1);
644 * Update the file size if changed.
647 if (wcp->offset + wcp->data_size > wcp->file_size) {
648 if (wcp_file_size_change(fsp) == -1) {
657 /* ASCII art..... JRA.
661 +---------------+---------------+
662 | Cached data | Cache buffer |
663 +---------------+---------------+
665 +-------------------+
667 +-------------------+
671 +---------------+---------------+
672 | Cached data | Cache buffer |
673 +---------------+---------------+
675 +-------------------+
677 +-------------------+
681 +---------------+---------------+
682 | Cached data | Cache buffer |
683 +---------------+---------------+
685 +-----------------------------------------------------+
687 +-----------------------------------------------------+
692 * Write is bigger than buffer, or there is no overlap on the
696 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
697 len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
700 * If write would fit in the cache, and is larger than
701 * the data already in the cache, flush the cache and
702 * preferentially copy the data new data into it. Otherwise
703 * just write the data directly.
706 if ( n <= wcp->alloc_size && n > wcp->data_size) {
707 cache_flush_needed = True;
709 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
712 * If the write overlaps the entire cache, then
713 * discard the current contents of the cache.
714 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
717 if ((pos <= wcp->offset) &&
718 (pos + n >= wcp->offset + wcp->data_size) ) {
719 DEBUG(9,("write_file: discarding overwritten write \
720 cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
724 DO_PROFILE_INC(writecache_direct_writes);
729 if (pos + ret > wcp->file_size) {
730 wcp->file_size = pos + ret;
740 if (cache_flush_needed) {
741 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
742 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
743 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
744 (double)wcp->offset, (unsigned int)wcp->data_size ));
746 flush_write_cache(fsp, WRITE_FLUSH);
751 * If the write request is bigger than the cache
752 * size, write it all out.
755 if (n > wcp->alloc_size ) {
756 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
761 if (pos + ret > wcp->file_size) {
762 wcp->file_size = pos + n;
765 DO_PROFILE_INC(writecache_direct_writes);
766 return total_written + n;
770 * If there's any data left, cache it.
775 if (wcp->data_size) {
776 DO_PROFILE_INC(writecache_abutted_writes);
778 DO_PROFILE_INC(writecache_init_writes);
781 memcpy(wcp->data+wcp->data_size, data, n);
782 if (wcp->data_size == 0) {
784 DO_PROFILE_INC(writecache_num_write_caches);
789 * Update the file size if changed.
792 if (wcp->offset + wcp->data_size > wcp->file_size) {
793 if (wcp_file_size_change(fsp) == -1) {
797 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
798 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
801 return total_written; /* .... that's a write :) */
804 return total_written;
807 /****************************************************************************
808 Delete the write cache structure.
809 ****************************************************************************/
811 void delete_write_cache(files_struct *fsp)
819 if(!(wcp = fsp->wcp)) {
823 DO_PROFILE_DEC(writecache_allocated_write_caches);
824 allocated_write_caches--;
826 SMB_ASSERT(wcp->data_size == 0);
828 SAFE_FREE(wcp->data);
831 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
834 /****************************************************************************
835 Setup the write cache structure.
836 ****************************************************************************/
838 static bool setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
840 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
843 if (allocated_write_caches >= MAX_WRITE_CACHES) {
847 if(alloc_size == 0 || fsp->wcp) {
851 if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
852 DEBUG(0,("setup_write_cache: malloc fail.\n"));
856 wcp->file_size = file_size;
858 wcp->alloc_size = alloc_size;
860 if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
861 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
862 (unsigned int)wcp->alloc_size ));
867 memset(wcp->data, '\0', wcp->alloc_size );
870 DO_PROFILE_INC(writecache_allocated_write_caches);
871 allocated_write_caches++;
873 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
874 fsp->fsp_name, (unsigned long)wcp->alloc_size ));
879 /****************************************************************************
880 Cope with a size change.
881 ****************************************************************************/
883 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
886 /* The cache *must* have been flushed before we do this. */
887 if (fsp->wcp->data_size != 0) {
889 if (asprintf(&msg, "set_filelen_write_cache: size change "
890 "on file %s with write cache size = %lu\n",
892 (unsigned long)fsp->wcp->data_size) != -1) {
895 smb_panic("set_filelen_write_cache");
898 fsp->wcp->file_size = file_size;
902 /*******************************************************************
903 Flush a write cache struct to disk.
904 ********************************************************************/
906 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
908 write_cache *wcp = fsp->wcp;
912 if(!wcp || !wcp->data_size) {
916 data_size = wcp->data_size;
919 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
921 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
922 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
925 if(data_size == wcp->alloc_size) {
926 DO_PROFILE_INC(writecache_num_perfect_writes);
930 ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
933 * Ensure file size if kept up to date if write extends file.
936 if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
937 wcp->file_size = wcp->offset + ret;
943 /*******************************************************************
945 ********************************************************************/
947 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
949 if (fsp->fh->fd == -1)
950 return NT_STATUS_INVALID_HANDLE;
952 if (lp_strict_sync(SNUM(conn)) &&
953 (lp_syncalways(SNUM(conn)) || write_through)) {
954 int ret = flush_write_cache(fsp, SYNC_FLUSH);
956 return map_nt_error_from_unix(errno);
958 ret = SMB_VFS_FSYNC(fsp);
960 return map_nt_error_from_unix(errno);
966 /************************************************************
967 Perform a stat whether a valid fd or not.
968 ************************************************************/
970 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
972 if (fsp->fh->fd == -1) {
973 return vfs_stat_smb_fname(fsp->conn, fsp->fsp_name, pst);
975 return SMB_VFS_FSTAT(fsp, pst);