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 = fsp->conn->vfs_ops.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))
93 flush_write_cache(fsp, READ_FLUSH);
95 if (seek_file(fsp,pos) == -1) {
96 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
104 readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
106 if ((errno == EAGAIN) && numretries) {
107 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
114 #else /* NO DMF fix. */
115 readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
123 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
124 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
129 /* how many write cache buffers have been allocated */
130 static unsigned int allocated_write_caches;
132 /****************************************************************************
133 *Really* write to a file.
134 ****************************************************************************/
136 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
140 if ((pos != -1) && (seek_file(fsp,pos) == -1))
143 ret = vfs_write_data(fsp,data,n);
145 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
146 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
151 /****************************************************************************
153 ****************************************************************************/
155 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
157 write_cache *wcp = fsp->wcp;
158 ssize_t total_written = 0;
162 return print_job_write(SNUM(fsp->conn), fsp->print_jobid, data, n);
164 if (!fsp->can_write) {
169 if (!fsp->modified) {
171 fsp->modified = True;
173 if (fsp->conn->vfs_ops.fstat(fsp,fsp->fd,&st) == 0) {
174 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
175 fsp->size = st.st_size;
176 if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode))
177 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
180 * If this is the first write and we have an exclusive oplock then setup
184 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
185 setup_write_cache(fsp, st.st_size);
192 DO_PROFILE_INC(writecache_total_writes);
193 if (!fsp->oplock_type) {
194 DO_PROFILE_INC(writecache_non_oplock_writes);
199 * If this file is level II oplocked then we need
200 * to grab the shared memory lock and inform all
201 * other files with a level II lock that they need
202 * to flush their read caches. We keep the lock over
203 * the shared memory area whilst doing this.
206 release_level_2_oplocks_on_change(fsp);
209 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
210 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
211 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
212 profile_p->writecache_init_writes,
213 profile_p->writecache_abutted_writes,
214 profile_p->writecache_total_writes,
215 profile_p->writecache_non_oplock_writes,
216 profile_p->writecache_allocated_write_caches,
217 profile_p->writecache_num_write_caches,
218 profile_p->writecache_direct_writes,
219 profile_p->writecache_num_perfect_writes,
220 profile_p->writecache_read_hits ));
222 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
223 profile_p->writecache_flushed_writes[SEEK_FLUSH],
224 profile_p->writecache_flushed_writes[READ_FLUSH],
225 profile_p->writecache_flushed_writes[WRITE_FLUSH],
226 profile_p->writecache_flushed_writes[READRAW_FLUSH],
227 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
228 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
229 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
234 DO_PROFILE_INC(writecache_direct_writes);
235 total_written = real_write_file(fsp, data, pos, n);
236 if ((total_written != -1) && (pos + total_written > fsp->size))
237 fsp->size = pos + total_written;
238 return total_written;
241 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
242 fsp->fsp_name, fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
245 * If we have active cache and it isn't contiguous then we flush.
246 * NOTE: There is a small problem with running out of disk ....
249 if (wcp->data_size) {
251 BOOL cache_flush_needed = False;
253 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
255 /* ASCII art.... JRA.
257 +--------------+-----
258 | Cached data | Rest of allocated cache buffer....
259 +--------------+-----
261 +-------------------+
263 +-------------------+
268 * Start of write overlaps or abutts the existing data.
271 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
273 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
276 * Update the current buffer size with the new data.
279 if(pos + data_used > wcp->offset + wcp->data_size)
280 wcp->data_size = pos + data_used - wcp->offset;
283 * Update the file size if changed.
286 if (wcp->offset + wcp->data_size > wcp->file_size)
287 fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
290 * If we used all the data then
297 cache_flush_needed = True;
300 * Move the start of data forward by the amount used,
301 * cut down the amount left by the same amount.
308 DO_PROFILE_INC(writecache_abutted_writes);
309 total_written = data_used;
313 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
314 (pos + n <= wcp->offset + wcp->alloc_size)) {
316 /* ASCII art.... JRA.
322 +-------------------+
324 +-------------------+
329 * End of write overlaps the existing data.
332 size_t data_used = pos + n - wcp->offset;
334 memcpy(wcp->data, data + n - data_used, data_used);
337 * Update the current buffer size with the new data.
340 if(pos + n > wcp->offset + wcp->data_size)
341 wcp->data_size = pos + n - wcp->offset;
344 * Update the file size if changed.
347 if (wcp->offset + wcp->data_size > wcp->file_size)
348 fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
351 * We don't need to move the start of data, but we
352 * cut down the amount left by the amount used.
358 * We cannot have used all the data here.
361 cache_flush_needed = True;
363 DO_PROFILE_INC(writecache_abutted_writes);
364 total_written = data_used;
368 } else if ( (pos >= wcp->file_size) &&
369 (wcp->offset + wcp->data_size == wcp->file_size) &&
370 (pos > wcp->offset + wcp->data_size) &&
371 (pos < wcp->offset + wcp->alloc_size) ) {
373 /* ASCII art.... JRA.
377 +---------------+---------------+
378 | Cached data | Cache buffer |
379 +---------------+---------------+
381 +-------------------+
383 +-------------------+
388 * Non-contiguous write part of which fits within
389 * the cache buffer and is extending the file
390 * and the cache contents reflect the current
391 * data up to the current end of the file.
396 if(pos + n <= wcp->offset + wcp->alloc_size)
399 data_used = wcp->offset + wcp->alloc_size - pos;
402 * Fill in the non-continuous area with zeros.
405 memset(wcp->data + wcp->data_size, '\0',
406 pos - (wcp->offset + wcp->data_size) );
408 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
411 * Update the current buffer size with the new data.
414 if(pos + data_used > wcp->offset + wcp->data_size)
415 wcp->data_size = pos + data_used - wcp->offset;
418 * Update the file size if changed.
421 if (wcp->offset + wcp->data_size > wcp->file_size)
422 fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
425 * If we used all the data then
432 cache_flush_needed = True;
435 * Move the start of data forward by the amount used,
436 * cut down the amount left by the same amount.
443 DO_PROFILE_INC(writecache_abutted_writes);
444 total_written = data_used;
450 /* ASCII art..... JRA.
454 +---------------+---------------+
455 | Cached data | Cache buffer |
456 +---------------+---------------+
458 +-------------------+
460 +-------------------+
464 +---------------+---------------+
465 | Cached data | Cache buffer |
466 +---------------+---------------+
468 +-------------------+
470 +-------------------+
474 +---------------+---------------+
475 | Cached data | Cache buffer |
476 +---------------+---------------+
478 +-----------------------------------------------------+
480 +-----------------------------------------------------+
485 * Write is bigger than buffer, or there is no overlap on the
489 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
490 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
493 * Update the file size if needed.
496 if(pos + n > wcp->file_size)
497 fsp->size = wcp->file_size = pos + n;
500 * If write would fit in the cache, and is larger than
501 * the data already in the cache, flush the cache and
502 * preferentially copy the data new data into it. Otherwise
503 * just write the data directly.
506 if ( n <= wcp->alloc_size && n > wcp->data_size) {
507 cache_flush_needed = True;
509 ssize_t ret = real_write_file(fsp, data, pos, n);
512 * If the write overlaps the entire cache, then
513 * discard the current contents of the cache.
514 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
517 if ((pos <= wcp->offset) &&
518 (pos + n >= wcp->offset + wcp->data_size) ) {
519 DEBUG(9,("write_file: discarding overwritten write \
520 cache: fd = %d, off=%.0f, size=%u\n", fsp->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
524 DO_PROFILE_INC(writecache_direct_writes);
528 if (pos + ret > wcp->file_size)
529 fsp->size = wcp->file_size = pos + ret;
538 if(wcp->data_size > wcp->file_size)
539 fsp->size = wcp->file_size = wcp->data_size;
541 if (cache_flush_needed) {
542 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
543 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
544 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
545 (double)wcp->offset, (unsigned int)wcp->data_size ));
547 flush_write_cache(fsp, WRITE_FLUSH);
552 * If the write request is bigger than the cache
553 * size, write it all out.
556 if (n > wcp->alloc_size ) {
557 ssize_t ret = real_write_file(fsp, data, pos, n);
561 if (pos + ret > wcp->file_size)
562 fsp->size = wcp->file_size = pos + n;
564 DO_PROFILE_INC(writecache_direct_writes);
565 return total_written + n;
569 * If there's any data left, cache it.
574 if (wcp->data_size) {
575 DO_PROFILE_INC(writecache_abutted_writes);
577 DO_PROFILE_INC(writecache_init_writes);
580 memcpy(wcp->data+wcp->data_size, data, n);
581 if (wcp->data_size == 0) {
583 DO_PROFILE_INC(writecache_num_write_caches);
588 * Update the file size if changed.
591 if (wcp->offset + wcp->data_size > wcp->file_size)
592 fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
593 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
594 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
597 return total_written; /* .... that's a write :) */
600 return total_written;
603 /****************************************************************************
604 Delete the write cache structure.
605 ****************************************************************************/
607 void delete_write_cache(files_struct *fsp)
614 if(!(wcp = fsp->wcp))
617 DO_PROFILE_DEC(writecache_allocated_write_caches);
618 allocated_write_caches--;
620 SMB_ASSERT(wcp->data_size == 0);
622 SAFE_FREE(wcp->data);
625 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
628 /****************************************************************************
629 Setup the write cache structure.
630 ****************************************************************************/
632 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
634 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
637 if (allocated_write_caches >= MAX_WRITE_CACHES)
640 if(alloc_size == 0 || fsp->wcp)
643 if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
644 DEBUG(0,("setup_write_cache: malloc fail.\n"));
648 wcp->file_size = file_size;
650 wcp->alloc_size = alloc_size;
652 if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
653 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
654 (unsigned int)wcp->alloc_size ));
659 memset(wcp->data, '\0', wcp->alloc_size );
662 DO_PROFILE_INC(writecache_allocated_write_caches);
663 allocated_write_caches++;
665 DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
666 fsp->fsp_name, wcp->alloc_size ));
671 /****************************************************************************
672 Cope with a size change.
673 ****************************************************************************/
675 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
677 fsp->size = file_size;
679 /* The cache *must* have been flushed before we do this. */
680 if (fsp->wcp->data_size != 0) {
682 slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
683 on file %s with write cache size = %u\n", fsp->fsp_name, fsp->wcp->data_size );
686 fsp->wcp->file_size = file_size;
690 /*******************************************************************
691 Flush a write cache struct to disk.
692 ********************************************************************/
694 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
696 write_cache *wcp = fsp->wcp;
700 if(!wcp || !wcp->data_size)
703 data_size = wcp->data_size;
706 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
708 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
709 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
712 if(data_size == wcp->alloc_size)
713 DO_PROFILE_INC(writecache_num_perfect_writes);
716 ret = real_write_file(fsp, wcp->data, wcp->offset, data_size);
719 * Ensure file size if kept up to date if write extends file.
722 if ((ret != -1) && (wcp->offset + ret > wcp->file_size))
723 wcp->file_size = wcp->offset + ret;
728 /*******************************************************************
730 ********************************************************************/
732 void sync_file(connection_struct *conn, files_struct *fsp)
734 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
735 flush_write_cache(fsp, SYNC_FLUSH);
736 conn->vfs_ops.fsync(fsp,fsp->fd);
741 /************************************************************
742 Perform a stat whether a valid fd or not.
743 ************************************************************/
745 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
748 return vfs_stat(fsp->conn, fsp->fsp_name, pst);
750 return vfs_fstat(fsp,fsp->fd, pst);