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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
27 /****************************************************************************
28 Seek a file. Try to avoid the seek if possible.
29 ****************************************************************************/
31 static SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
35 seek_ret = SMB_VFS_LSEEK(fsp,fsp->fd,pos,SEEK_SET);
38 DEBUG(0,("seek_file: (%s) sys_lseek failed. Error was %s\n",
39 fsp->fsp_name, strerror(errno) ));
46 DEBUG(10,("seek_file (%s): requested pos = %.0f, new pos = %.0f\n",
47 fsp->fsp_name, (double)pos, (double)fsp->pos ));
52 /****************************************************************************
53 Read from write cache if we can.
54 ****************************************************************************/
57 static BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
59 write_cache *wcp = fsp->wcp;
64 if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
67 memcpy(data, wcp->data + (pos - wcp->offset), n);
69 DO_PROFILE_INC(writecache_read_hits);
74 /****************************************************************************
76 ****************************************************************************/
78 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
80 ssize_t ret=0,readret;
82 /* you can't read from print files */
87 * Serve from write cache if we can.
90 if(read_from_write_cache(fsp, data, pos, n)) {
92 fsp->position_information = fsp->pos;
96 flush_write_cache(fsp, READ_FLUSH);
104 readret = SMB_VFS_PREAD(fsp,fsp->fd,data,n,pos);
107 if ((errno == EAGAIN) && numretries) {
108 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
115 #else /* NO DMF fix. */
116 readret = SMB_VFS_PREAD(fsp,fsp->fd,data,n,pos);
125 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
126 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
129 fsp->position_information = fsp->pos;
134 /* how many write cache buffers have been allocated */
135 static unsigned int allocated_write_caches;
137 /****************************************************************************
138 *Really* write to a file.
139 ****************************************************************************/
141 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
146 ret = vfs_write_data(fsp, data, n);
149 ret = vfs_pwrite_data(fsp, data, n, pos);
152 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
153 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
158 /* Yes - this is correct - writes don't update this. JRA. */
159 /* Found by Samba4 tests. */
161 fsp->position_information = fsp->pos;
168 /****************************************************************************
170 ****************************************************************************/
172 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
174 write_cache *wcp = fsp->wcp;
175 ssize_t total_written = 0;
178 if (fsp->print_file) {
182 if (!rap_to_pjobid(fsp->rap_print_jobid, &snum, &jobid)) {
183 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
184 (unsigned int)fsp->rap_print_jobid ));
189 return print_job_write(SNUM(fsp->conn), jobid, data, n);
192 if (!fsp->can_write) {
197 if (!fsp->modified) {
199 fsp->modified = True;
201 if (SMB_VFS_FSTAT(fsp,fsp->fd,&st) == 0) {
202 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
203 fsp->size = (SMB_BIG_UINT)st.st_size;
204 if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode))
205 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
208 * If this is the first write and we have an exclusive oplock then setup
212 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
213 setup_write_cache(fsp, st.st_size);
220 DO_PROFILE_INC(writecache_total_writes);
221 if (!fsp->oplock_type) {
222 DO_PROFILE_INC(writecache_non_oplock_writes);
227 * If this file is level II oplocked then we need
228 * to grab the shared memory lock and inform all
229 * other files with a level II lock that they need
230 * to flush their read caches. We keep the lock over
231 * the shared memory area whilst doing this.
234 release_level_2_oplocks_on_change(fsp);
237 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
238 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
239 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
240 profile_p->writecache_init_writes,
241 profile_p->writecache_abutted_writes,
242 profile_p->writecache_total_writes,
243 profile_p->writecache_non_oplock_writes,
244 profile_p->writecache_allocated_write_caches,
245 profile_p->writecache_num_write_caches,
246 profile_p->writecache_direct_writes,
247 profile_p->writecache_num_perfect_writes,
248 profile_p->writecache_read_hits ));
250 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
251 profile_p->writecache_flushed_writes[SEEK_FLUSH],
252 profile_p->writecache_flushed_writes[READ_FLUSH],
253 profile_p->writecache_flushed_writes[WRITE_FLUSH],
254 profile_p->writecache_flushed_writes[READRAW_FLUSH],
255 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
256 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
257 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
262 DO_PROFILE_INC(writecache_direct_writes);
263 total_written = real_write_file(fsp, data, pos, n);
264 if ((total_written != -1) && (pos + total_written > (SMB_OFF_T)fsp->size))
265 fsp->size = (SMB_BIG_UINT)(pos + total_written);
266 return total_written;
269 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
270 fsp->fsp_name, fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
275 * If we have active cache and it isn't contiguous then we flush.
276 * NOTE: There is a small problem with running out of disk ....
279 if (wcp->data_size) {
281 BOOL cache_flush_needed = False;
283 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
285 /* ASCII art.... JRA.
287 +--------------+-----
288 | Cached data | Rest of allocated cache buffer....
289 +--------------+-----
291 +-------------------+
293 +-------------------+
298 * Start of write overlaps or abutts the existing data.
301 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
303 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
306 * Update the current buffer size with the new data.
309 if(pos + data_used > wcp->offset + wcp->data_size)
310 wcp->data_size = pos + data_used - wcp->offset;
313 * Update the file size if changed.
316 if (wcp->offset + wcp->data_size > wcp->file_size) {
317 wcp->file_size = wcp->offset + wcp->data_size;
318 fsp->size = (SMB_BIG_UINT)wcp->file_size;
322 * If we used all the data then
329 cache_flush_needed = True;
332 * Move the start of data forward by the amount used,
333 * cut down the amount left by the same amount.
340 DO_PROFILE_INC(writecache_abutted_writes);
341 total_written = data_used;
345 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
346 (pos + n <= wcp->offset + wcp->alloc_size)) {
348 /* ASCII art.... JRA.
354 +-------------------+
356 +-------------------+
361 * End of write overlaps the existing data.
364 size_t data_used = pos + n - wcp->offset;
366 memcpy(wcp->data, data + n - data_used, data_used);
369 * Update the current buffer size with the new data.
372 if(pos + n > wcp->offset + wcp->data_size)
373 wcp->data_size = pos + n - wcp->offset;
376 * Update the file size if changed.
379 if (wcp->offset + wcp->data_size > wcp->file_size) {
380 wcp->file_size = wcp->offset + wcp->data_size;
381 fsp->size = (SMB_BIG_UINT)wcp->file_size;
385 * We don't need to move the start of data, but we
386 * cut down the amount left by the amount used.
392 * We cannot have used all the data here.
395 cache_flush_needed = True;
397 DO_PROFILE_INC(writecache_abutted_writes);
398 total_written = data_used;
402 } else if ( (pos >= wcp->file_size) &&
403 (wcp->offset + wcp->data_size == wcp->file_size) &&
404 (pos > wcp->offset + wcp->data_size) &&
405 (pos < wcp->offset + wcp->alloc_size) ) {
407 /* ASCII art.... JRA.
411 +---------------+---------------+
412 | Cached data | Cache buffer |
413 +---------------+---------------+
415 +-------------------+
417 +-------------------+
422 * Non-contiguous write part of which fits within
423 * the cache buffer and is extending the file
424 * and the cache contents reflect the current
425 * data up to the current end of the file.
430 if(pos + n <= wcp->offset + wcp->alloc_size)
433 data_used = wcp->offset + wcp->alloc_size - pos;
436 * Fill in the non-continuous area with zeros.
439 memset(wcp->data + wcp->data_size, '\0',
440 pos - (wcp->offset + wcp->data_size) );
442 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
445 * Update the current buffer size with the new data.
448 if(pos + data_used > wcp->offset + wcp->data_size)
449 wcp->data_size = pos + data_used - wcp->offset;
452 * Update the file size if changed.
455 if (wcp->offset + wcp->data_size > wcp->file_size) {
456 wcp->file_size = wcp->offset + wcp->data_size;
457 fsp->size = (SMB_BIG_UINT)wcp->file_size;
461 * If we used all the data then
468 cache_flush_needed = True;
471 * Move the start of data forward by the amount used,
472 * cut down the amount left by the same amount.
479 DO_PROFILE_INC(writecache_abutted_writes);
480 total_written = data_used;
486 /* ASCII art..... JRA.
490 +---------------+---------------+
491 | Cached data | Cache buffer |
492 +---------------+---------------+
494 +-------------------+
496 +-------------------+
500 +---------------+---------------+
501 | Cached data | Cache buffer |
502 +---------------+---------------+
504 +-------------------+
506 +-------------------+
510 +---------------+---------------+
511 | Cached data | Cache buffer |
512 +---------------+---------------+
514 +-----------------------------------------------------+
516 +-----------------------------------------------------+
521 * Write is bigger than buffer, or there is no overlap on the
525 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
526 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
529 * Update the file size if needed.
532 if(pos + n > wcp->file_size) {
533 wcp->file_size = pos + n;
534 fsp->size = (SMB_BIG_UINT)wcp->file_size;
538 * If write would fit in the cache, and is larger than
539 * the data already in the cache, flush the cache and
540 * preferentially copy the data new data into it. Otherwise
541 * just write the data directly.
544 if ( n <= wcp->alloc_size && n > wcp->data_size) {
545 cache_flush_needed = True;
547 ssize_t ret = real_write_file(fsp, data, pos, n);
550 * If the write overlaps the entire cache, then
551 * discard the current contents of the cache.
552 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
555 if ((pos <= wcp->offset) &&
556 (pos + n >= wcp->offset + wcp->data_size) ) {
557 DEBUG(9,("write_file: discarding overwritten write \
558 cache: fd = %d, off=%.0f, size=%u\n", fsp->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
562 DO_PROFILE_INC(writecache_direct_writes);
566 if (pos + ret > wcp->file_size) {
567 wcp->file_size = pos + ret;
568 fsp->size = (SMB_BIG_UINT)wcp->file_size;
578 if(wcp->data_size > wcp->file_size) {
579 wcp->file_size = wcp->data_size;
580 fsp->size = (SMB_BIG_UINT)wcp->file_size;
583 if (cache_flush_needed) {
584 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
585 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
586 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
587 (double)wcp->offset, (unsigned int)wcp->data_size ));
589 flush_write_cache(fsp, WRITE_FLUSH);
594 * If the write request is bigger than the cache
595 * size, write it all out.
598 if (n > wcp->alloc_size ) {
599 ssize_t ret = real_write_file(fsp, data, pos, n);
603 if (pos + ret > wcp->file_size) {
604 wcp->file_size = pos + n;
605 fsp->size = (SMB_BIG_UINT)wcp->file_size;
608 DO_PROFILE_INC(writecache_direct_writes);
609 return total_written + n;
613 * If there's any data left, cache it.
618 if (wcp->data_size) {
619 DO_PROFILE_INC(writecache_abutted_writes);
621 DO_PROFILE_INC(writecache_init_writes);
624 memcpy(wcp->data+wcp->data_size, data, n);
625 if (wcp->data_size == 0) {
627 DO_PROFILE_INC(writecache_num_write_caches);
632 * Update the file size if changed.
635 if (wcp->offset + wcp->data_size > wcp->file_size) {
636 wcp->file_size = wcp->offset + wcp->data_size;
637 fsp->size = (SMB_BIG_UINT)wcp->file_size;
639 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
640 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
643 return total_written; /* .... that's a write :) */
646 return total_written;
649 /****************************************************************************
650 Delete the write cache structure.
651 ****************************************************************************/
653 void delete_write_cache(files_struct *fsp)
660 if(!(wcp = fsp->wcp))
663 DO_PROFILE_DEC(writecache_allocated_write_caches);
664 allocated_write_caches--;
666 SMB_ASSERT(wcp->data_size == 0);
668 SAFE_FREE(wcp->data);
671 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
674 /****************************************************************************
675 Setup the write cache structure.
676 ****************************************************************************/
678 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
680 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
683 if (allocated_write_caches >= MAX_WRITE_CACHES)
686 if(alloc_size == 0 || fsp->wcp)
689 if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
690 DEBUG(0,("setup_write_cache: malloc fail.\n"));
694 wcp->file_size = file_size;
696 wcp->alloc_size = alloc_size;
698 if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
699 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
700 (unsigned int)wcp->alloc_size ));
705 memset(wcp->data, '\0', wcp->alloc_size );
708 DO_PROFILE_INC(writecache_allocated_write_caches);
709 allocated_write_caches++;
711 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
712 fsp->fsp_name, (unsigned long)wcp->alloc_size ));
717 /****************************************************************************
718 Cope with a size change.
719 ****************************************************************************/
721 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
723 fsp->size = (SMB_BIG_UINT)file_size;
725 /* The cache *must* have been flushed before we do this. */
726 if (fsp->wcp->data_size != 0) {
728 slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
729 on file %s with write cache size = %lu\n", fsp->fsp_name, (unsigned long)fsp->wcp->data_size );
732 fsp->wcp->file_size = file_size;
736 /*******************************************************************
737 Flush a write cache struct to disk.
738 ********************************************************************/
740 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
742 write_cache *wcp = fsp->wcp;
746 if(!wcp || !wcp->data_size)
749 data_size = wcp->data_size;
752 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
754 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
755 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
758 if(data_size == wcp->alloc_size)
759 DO_PROFILE_INC(writecache_num_perfect_writes);
762 ret = real_write_file(fsp, wcp->data, wcp->offset, data_size);
765 * Ensure file size if kept up to date if write extends file.
768 if ((ret != -1) && (wcp->offset + ret > wcp->file_size))
769 wcp->file_size = wcp->offset + ret;
774 /*******************************************************************
776 ********************************************************************/
778 void sync_file(connection_struct *conn, files_struct *fsp)
780 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
781 flush_write_cache(fsp, SYNC_FLUSH);
782 SMB_VFS_FSYNC(fsp,fsp->fd);
787 /************************************************************
788 Perform a stat whether a valid fd or not.
789 ************************************************************/
791 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
794 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name, pst);
796 return SMB_VFS_FSTAT(fsp,fsp->fd, pst);