2 Unix SMB/CIFS implementation.
3 read/write to a files_struct
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
25 /****************************************************************************
26 seek a file. Try to avoid the seek if possible
27 ****************************************************************************/
29 SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
34 if (fsp->print_file && lp_postscript(fsp->conn->service))
37 seek_ret = fsp->conn->vfs_ops.lseek(fsp,fsp->fd,pos+offset,SEEK_SET);
40 DEBUG(0,("seek_file: sys_lseek failed. Error was %s\n", strerror(errno) ));
45 fsp->pos = seek_ret - offset;
47 DEBUG(10,("seek_file: requested pos = %.0f, new pos = %.0f\n",
48 (double)(pos+offset), (double)fsp->pos ));
53 /****************************************************************************
54 Read from write cache if we can.
55 ****************************************************************************/
58 BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
60 write_cache *wcp = fsp->wcp;
65 if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
68 memcpy(data, wcp->data + (pos - wcp->offset), n);
70 DO_PROFILE_INC(writecache_read_hits);
75 /****************************************************************************
77 ****************************************************************************/
79 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
81 ssize_t ret=0,readret;
83 /* you can't read from print files */
88 * Serve from write cache if we can.
91 if(read_from_write_cache(fsp, data, pos, n))
94 flush_write_cache(fsp, READ_FLUSH);
96 if (seek_file(fsp,pos) == -1) {
97 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
105 readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
107 if ((errno == EAGAIN) && numretries) {
108 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
115 #else /* NO DMF fix. */
116 readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
127 /* how many write cache buffers have been allocated */
128 static unsigned int allocated_write_caches;
130 /****************************************************************************
131 *Really* write to a file.
132 ****************************************************************************/
134 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
136 if ((pos != -1) && (seek_file(fsp,pos) == -1))
139 return vfs_write_data(fsp,data,n);
142 /****************************************************************************
144 ****************************************************************************/
146 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
148 write_cache *wcp = fsp->wcp;
149 ssize_t total_written = 0;
152 if (fsp->print_file) {
153 return print_job_write(fsp->print_jobid, data, n);
156 if (!fsp->can_write) {
161 if (!fsp->modified) {
163 fsp->modified = True;
165 if (fsp->conn->vfs_ops.fstat(fsp,fsp->fd,&st) == 0) {
166 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
167 fsp->size = st.st_size;
168 if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) {
169 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
173 * If this is the first write and we have an exclusive oplock then setup
177 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
178 setup_write_cache(fsp, st.st_size);
185 DO_PROFILE_INC(writecache_total_writes);
186 if (!fsp->oplock_type) {
187 DO_PROFILE_INC(writecache_non_oplock_writes);
192 * If this file is level II oplocked then we need
193 * to grab the shared memory lock and inform all
194 * other files with a level II lock that they need
195 * to flush their read caches. We keep the lock over
196 * the shared memory area whilst doing this.
199 release_level_2_oplocks_on_change(fsp);
202 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
203 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
204 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
205 profile_p->writecache_init_writes,
206 profile_p->writecache_abutted_writes,
207 profile_p->writecache_total_writes,
208 profile_p->writecache_non_oplock_writes,
209 profile_p->writecache_allocated_write_caches,
210 profile_p->writecache_num_write_caches,
211 profile_p->writecache_direct_writes,
212 profile_p->writecache_num_perfect_writes,
213 profile_p->writecache_read_hits ));
215 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
216 profile_p->writecache_flushed_writes[SEEK_FLUSH],
217 profile_p->writecache_flushed_writes[READ_FLUSH],
218 profile_p->writecache_flushed_writes[WRITE_FLUSH],
219 profile_p->writecache_flushed_writes[READRAW_FLUSH],
220 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
221 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
222 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
227 DO_PROFILE_INC(writecache_direct_writes);
228 total_written = real_write_file(fsp, data, pos, n);
229 if ((total_written != -1) && (pos + total_written > fsp->size))
230 fsp->size = pos + total_written;
231 return total_written;
234 DEBUG(9,("write_file(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
235 fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
238 * If we have active cache and it isn't contiguous then we flush.
239 * NOTE: There is a small problem with running out of disk ....
242 if (wcp->data_size) {
244 BOOL cache_flush_needed = False;
246 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
249 * Start of write overlaps or abutts the existing data.
252 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
254 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
257 * Update the current buffer size with the new data.
260 if(pos + data_used > wcp->offset + wcp->data_size)
261 wcp->data_size = pos + data_used - wcp->offset;
264 * Update the file size if changed.
267 if (wcp->offset + wcp->data_size > wcp->file_size)
268 fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
271 * If we used all the data then
278 cache_flush_needed = True;
281 * Move the start of data forward by the amount used,
282 * cut down the amount left by the same amount.
289 DO_PROFILE_INC(writecache_abutted_writes);
290 total_written = data_used;
294 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
295 (pos + n <= wcp->offset + wcp->alloc_size)) {
298 * End of write overlaps the existing data.
301 size_t data_used = pos + n - wcp->offset;
303 memcpy(wcp->data, data + n - data_used, data_used);
306 * Update the current buffer size with the new data.
309 if(pos + n > wcp->offset + wcp->data_size)
310 wcp->data_size = pos + n - wcp->offset;
313 * Update the file size if changed.
316 if (wcp->offset + wcp->data_size > wcp->file_size)
317 fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
320 * We don't need to move the start of data, but we
321 * cut down the amount left by the amount used.
327 * We cannot have used all the data here.
330 cache_flush_needed = True;
332 DO_PROFILE_INC(writecache_abutted_writes);
333 total_written = data_used;
337 } else if ( (pos >= wcp->file_size) &&
338 (wcp->offset + wcp->data_size == wcp->file_size) &&
339 (pos > wcp->offset + wcp->data_size) &&
340 (pos < wcp->offset + wcp->alloc_size) ) {
343 * Non-contiguous write part of which fits within
344 * the cache buffer and is extending the file
345 * and the cache contents reflect the current
346 * data up to the current end of the file.
351 if(pos + n <= wcp->offset + wcp->alloc_size)
354 data_used = wcp->offset + wcp->alloc_size - pos;
357 * Fill in the non-continuous area with zeros.
360 memset(wcp->data + wcp->data_size, '\0',
361 pos - (wcp->offset + wcp->data_size) );
363 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
366 * Update the current buffer size with the new data.
369 if(pos + data_used > wcp->offset + wcp->data_size)
370 wcp->data_size = pos + data_used - wcp->offset;
373 * Update the file size if changed.
376 if (wcp->offset + wcp->data_size > wcp->file_size)
377 fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
380 * If we used all the data then
387 cache_flush_needed = True;
390 * Move the start of data forward by the amount used,
391 * cut down the amount left by the same amount.
398 DO_PROFILE_INC(writecache_abutted_writes);
399 total_written = data_used;
406 * Write is bigger than buffer, or there is no overlap on the
410 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
411 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
414 * Update the file size if needed.
417 if(pos + n > wcp->file_size)
418 fsp->size = wcp->file_size = pos + n;
421 * If write would fit in the cache, and is larger than
422 * the data already in the cache, flush the cache and
423 * preferentially copy the data new data into it. Otherwise
424 * just write the data directly.
427 if ( n <= wcp->alloc_size && n > wcp->data_size) {
428 cache_flush_needed = True;
430 ssize_t ret = real_write_file(fsp, data, pos, n);
432 DO_PROFILE_INC(writecache_direct_writes);
436 if (pos + ret > wcp->file_size)
437 fsp->size = wcp->file_size = pos + ret;
446 if(wcp->data_size > wcp->file_size)
447 fsp->size = wcp->file_size = wcp->data_size;
449 if (cache_flush_needed) {
450 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
451 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
452 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
453 (double)wcp->offset, (unsigned int)wcp->data_size ));
455 flush_write_cache(fsp, WRITE_FLUSH);
460 * If the write request is bigger than the cache
461 * size, write it all out.
464 if (n > wcp->alloc_size ) {
465 ssize_t ret = real_write_file(fsp, data, pos, n);
469 if (pos + ret > wcp->file_size)
470 fsp->size = wcp->file_size = pos + n;
472 DO_PROFILE_INC(writecache_direct_writes);
473 return total_written + n;
477 * If there's any data left, cache it.
482 if (wcp->data_size) {
483 DO_PROFILE_INC(writecache_abutted_writes);
485 DO_PROFILE_INC(writecache_init_writes);
488 memcpy(wcp->data+wcp->data_size, data, n);
489 if (wcp->data_size == 0) {
491 DO_PROFILE_INC(writecache_num_write_caches);
496 * Update the file size if changed.
499 if (wcp->offset + wcp->data_size > wcp->file_size)
500 fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
501 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
502 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
505 return total_written; /* .... that's a write :) */
508 return total_written;
511 /****************************************************************************
512 Delete the write cache structure.
513 ****************************************************************************/
515 void delete_write_cache(files_struct *fsp)
522 if(!(wcp = fsp->wcp))
525 DO_PROFILE_DEC(writecache_allocated_write_caches);
526 allocated_write_caches--;
528 SMB_ASSERT(wcp->data_size == 0);
530 SAFE_FREE(wcp->data);
533 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
537 /****************************************************************************
538 Setup the write cache structure.
539 ****************************************************************************/
541 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
543 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
546 if (allocated_write_caches >= MAX_WRITE_CACHES)
549 if(alloc_size == 0 || fsp->wcp)
552 if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
553 DEBUG(0,("setup_write_cache: malloc fail.\n"));
557 wcp->file_size = file_size;
559 wcp->alloc_size = alloc_size;
561 if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
562 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
563 (unsigned int)wcp->alloc_size ));
568 memset(wcp->data, '\0', wcp->alloc_size );
571 DO_PROFILE_INC(writecache_allocated_write_caches);
572 allocated_write_caches++;
574 DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
575 fsp->fsp_name, wcp->alloc_size ));
580 /****************************************************************************
581 Cope with a size change.
582 ****************************************************************************/
584 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
586 fsp->size = file_size;
588 /* The cache *must* have been flushed before we do this. */
589 if (fsp->wcp->data_size != 0) {
591 slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
592 on file %s with write cache size = %u\n", fsp->fsp_name, fsp->wcp->data_size );
595 fsp->wcp->file_size = file_size;
599 /*******************************************************************
600 Flush a write cache struct to disk.
601 ********************************************************************/
603 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
605 write_cache *wcp = fsp->wcp;
609 if(!wcp || !wcp->data_size)
612 data_size = wcp->data_size;
615 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
617 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
618 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
621 if(data_size == wcp->alloc_size)
622 DO_PROFILE_INC(writecache_num_perfect_writes);
625 ret = real_write_file(fsp, wcp->data, wcp->offset, data_size);
628 * Ensure file size if kept up to date if write extends file.
631 if ((ret != -1) && (wcp->offset + ret > wcp->file_size))
632 wcp->file_size = wcp->offset + ret;
637 /*******************************************************************
639 ********************************************************************/
641 void sync_file(connection_struct *conn, files_struct *fsp)
643 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
644 flush_write_cache(fsp, SYNC_FLUSH);
645 conn->vfs_ops.fsync(fsp,fsp->fd);