2 Unix SMB/Netbios implementation.
4 read/write to a files_struct
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
26 /****************************************************************************
27 seek a file. Try to avoid the seek if possible
28 ****************************************************************************/
30 SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
35 if (fsp->print_file && lp_postscript(fsp->conn->service))
38 seek_ret = fsp->conn->vfs_ops.lseek(fsp,fsp->fd,pos+offset,SEEK_SET);
41 * We want to maintain the fiction that we can seek
42 * on a fifo for file system purposes. This allows
43 * people to set up UNIX fifo's that feed data to Windows
47 if((seek_ret == -1) && (errno == ESPIPE)) {
48 seek_ret = pos+offset;
52 if((seek_ret == -1) || (seek_ret != pos+offset)) {
53 DEBUG(0,("seek_file: sys_lseek failed. Error was %s\n", strerror(errno) ));
58 fsp->pos = seek_ret - offset;
60 DEBUG(10,("seek_file: requested pos = %.0f, new pos = %.0f\n",
61 (double)(pos+offset), (double)fsp->pos ));
66 /****************************************************************************
67 Read from write cache if we can.
68 ****************************************************************************/
71 BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
73 write_cache *wcp = fsp->wcp;
78 if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
81 memcpy(data, wcp->data + (pos - wcp->offset), n);
83 DO_PROFILE_INC(writecache_read_hits);
88 /****************************************************************************
90 ****************************************************************************/
92 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
94 ssize_t ret=0,readret;
96 /* you can't read from print files */
97 if (fsp->print_file) {
102 * Serve from write cache if we can.
104 if(read_from_write_cache(fsp, data, pos, n))
107 flush_write_cache(fsp, READ_FLUSH);
109 if (seek_file(fsp,pos) == -1) {
110 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
115 readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
118 if (readret > 0) ret += readret;
124 /* how many write cache buffers have been allocated */
125 static unsigned int allocated_write_caches;
127 /****************************************************************************
128 *Really* write to a file.
129 ****************************************************************************/
131 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
133 if ((pos != -1) && (seek_file(fsp,pos) == -1))
136 return vfs_write_data(fsp,data,n);
139 /****************************************************************************
141 ****************************************************************************/
143 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
145 write_cache *wcp = fsp->wcp;
146 ssize_t total_written = 0;
149 if (fsp->print_file) {
150 return print_job_write(fsp->print_jobid, data, n);
153 if (!fsp->can_write) {
158 if (!fsp->modified) {
160 fsp->modified = True;
162 if (fsp->conn->vfs_ops.fstat(fsp,fsp->fd,&st) == 0) {
163 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
164 if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) {
165 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
169 * If this is the first write and we have an exclusive oplock then setup
173 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
174 setup_write_cache(fsp, st.st_size);
181 DO_PROFILE_INC(writecache_total_writes);
182 if (!fsp->oplock_type) {
183 DO_PROFILE_INC(writecache_non_oplock_writes);
188 * If this file is level II oplocked then we need
189 * to grab the shared memory lock and inform all
190 * other files with a level II lock that they need
191 * to flush their read caches. We keep the lock over
192 * the shared memory area whilst doing this.
195 release_level_2_oplocks_on_change(fsp);
198 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
199 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
200 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
201 profile_p->writecache_init_writes,
202 profile_p->writecache_abutted_writes,
203 profile_p->writecache_total_writes,
204 profile_p->writecache_non_oplock_writes,
205 profile_p->writecache_allocated_write_caches,
206 profile_p->writecache_num_write_caches,
207 profile_p->writecache_direct_writes,
208 profile_p->writecache_num_perfect_writes,
209 profile_p->writecache_read_hits ));
211 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
212 profile_p->writecache_flushed_writes[SEEK_FLUSH],
213 profile_p->writecache_flushed_writes[READ_FLUSH],
214 profile_p->writecache_flushed_writes[WRITE_FLUSH],
215 profile_p->writecache_flushed_writes[READRAW_FLUSH],
216 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
217 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
218 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
223 DO_PROFILE_INC(writecache_direct_writes);
224 return real_write_file(fsp, data, pos, n);
227 DEBUG(9,("write_file(fd=%d pos=%d size=%d) wofs=%d wsize=%d\n",
228 fsp->fd, (int)pos, (int)n, (int)wcp->offset, (int)wcp->data_size));
231 * If we have active cache and it isn't contiguous then we flush.
232 * NOTE: There is a small problem with running out of disk ....
235 if (wcp->data_size) {
237 BOOL cache_flush_needed = False;
239 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
242 * Start of write overlaps or abutts the existing data.
245 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
247 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
250 * Update the current buffer size with the new data.
253 if(pos + data_used > wcp->offset + wcp->data_size)
254 wcp->data_size = pos + data_used - wcp->offset;
257 * If we used all the data then
264 cache_flush_needed = True;
267 * Move the start of data forward by the amount used,
268 * cut down the amount left by the same amount.
275 DO_PROFILE_INC(writecache_abutted_writes);
276 total_written = data_used;
280 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
281 (pos + n <= wcp->offset + wcp->alloc_size)) {
284 * End of write overlaps the existing data.
287 size_t data_used = pos + n - wcp->offset;
289 memcpy(wcp->data, data + n - data_used, data_used);
292 * Update the current buffer size with the new data.
295 if(pos + n > wcp->offset + wcp->data_size)
296 wcp->data_size = pos + n - wcp->offset;
299 * We don't need to move the start of data, but we
300 * cut down the amount left by the amount used.
306 * We cannot have used all the data here.
309 cache_flush_needed = True;
311 DO_PROFILE_INC(writecache_abutted_writes);
312 total_written = data_used;
316 } else if ( (pos >= wcp->file_size) &&
317 (pos > wcp->offset + wcp->data_size) &&
318 (pos < wcp->offset + wcp->alloc_size) ) {
321 * Non-contiguous write part of which fits within
322 * the cache buffer and is extending the file.
327 if(pos + n <= wcp->offset + wcp->alloc_size)
330 data_used = wcp->offset + wcp->alloc_size - pos;
333 * Fill in the non-continuous area with zeros.
336 memset(wcp->data + wcp->data_size, '\0',
337 pos - (wcp->offset + wcp->data_size) );
339 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
342 * Update the current buffer size with the new data.
345 if(pos + data_used > wcp->offset + wcp->data_size)
346 wcp->data_size = pos + data_used - wcp->offset;
349 * Update the known file length.
352 wcp->file_size = wcp->offset + wcp->data_size;
355 * If we used all the data then
362 cache_flush_needed = True;
365 * Move the start of data forward by the amount used,
366 * cut down the amount left by the same amount.
373 DO_PROFILE_INC(writecache_abutted_writes);
374 total_written = data_used;
381 * Write is bigger than buffer, or there is no overlap on the
385 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
386 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
389 * Update the file size if needed.
392 if(pos + n > wcp->file_size)
393 wcp->file_size = pos + n;
396 * If write would fit in the cache, and is larger than
397 * the data already in the cache, flush the cache and
398 * preferentially copy the data new data into it. Otherwise
399 * just write the data directly.
402 if ( n <= wcp->alloc_size && n > wcp->data_size) {
403 cache_flush_needed = True;
405 DO_PROFILE_INC(writecache_direct_writes);
406 return real_write_file(fsp, data, pos, n);
413 if(wcp->data_size > wcp->file_size)
414 wcp->file_size = wcp->data_size;
416 if (cache_flush_needed) {
417 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
418 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
419 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
420 (double)wcp->offset, (unsigned int)wcp->data_size ));
422 flush_write_cache(fsp, WRITE_FLUSH);
427 * If the write request is bigger than the cache
428 * size, write it all out.
431 if (n > wcp->alloc_size ) {
432 if(real_write_file(fsp, data, pos, n) == -1)
434 DO_PROFILE_INC(writecache_direct_writes);
435 return total_written + n;
439 * If there's any data left, cache it.
444 if (wcp->data_size) {
445 DO_PROFILE_INC(writecache_abutted_writes);
447 DO_PROFILE_INC(writecache_init_writes);
450 memcpy(wcp->data+wcp->data_size, data, n);
451 if (wcp->data_size == 0) {
453 DO_PROFILE_INC(writecache_num_write_caches);
456 DEBUG(9,("cache return %u\n", (unsigned int)n));
458 return total_written; /* .... that's a write :) */
461 return total_written;
464 /****************************************************************************
465 Delete the write cache structure.
466 ****************************************************************************/
468 void delete_write_cache(files_struct *fsp)
475 if(!(wcp = fsp->wcp))
478 DO_PROFILE_DEC(writecache_allocated_write_caches);
479 allocated_write_caches--;
481 SMB_ASSERT(wcp->data_size == 0);
483 SAFE_FREE(wcp->data);
486 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
490 /****************************************************************************
491 Setup the write cache structure.
492 ****************************************************************************/
494 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
496 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
499 if (allocated_write_caches >= MAX_WRITE_CACHES)
502 if(alloc_size == 0 || fsp->wcp)
505 if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
506 DEBUG(0,("setup_write_cache: malloc fail.\n"));
510 wcp->file_size = file_size;
512 wcp->alloc_size = alloc_size;
514 if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
515 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
516 (unsigned int)wcp->alloc_size ));
522 DO_PROFILE_INC(writecache_allocated_write_caches);
523 allocated_write_caches++;
525 DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
526 fsp->fsp_name, wcp->alloc_size ));
531 /****************************************************************************
532 Cope with a size change.
533 ****************************************************************************/
535 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
538 flush_write_cache(fsp, SIZECHANGE_FLUSH);
539 fsp->wcp->file_size = file_size;
543 /*******************************************************************
544 Flush a write cache struct to disk.
545 ********************************************************************/
547 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
549 write_cache *wcp = fsp->wcp;
552 if(!wcp || !wcp->data_size)
555 data_size = wcp->data_size;
558 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
560 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
561 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
564 if(data_size == wcp->alloc_size)
565 DO_PROFILE_INC(writecache_num_perfect_writes);
568 return real_write_file(fsp, wcp->data, wcp->offset, data_size);
571 /*******************************************************************
573 ********************************************************************/
575 void sync_file(connection_struct *conn, files_struct *fsp)
577 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
578 flush_write_cache(fsp, SYNC_FLUSH);
579 conn->vfs_ops.fsync(fsp,fsp->fd);