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 extern int DEBUGLEVEL;
26 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
28 /****************************************************************************
29 seek a file. Try to avoid the seek if possible
30 ****************************************************************************/
32 SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
37 if (fsp->print_file && lp_postscript(fsp->conn->service))
40 seek_ret = fsp->conn->vfs_ops.lseek(fsp,fsp->fd,pos+offset,SEEK_SET);
43 * We want to maintain the fiction that we can seek
44 * on a fifo for file system purposes. This allows
45 * people to set up UNIX fifo's that feed data to Windows
49 if((seek_ret == -1) && (errno == ESPIPE)) {
50 seek_ret = pos+offset;
54 if((seek_ret == -1) || (seek_ret != pos+offset)) {
55 DEBUG(0,("seek_file: sys_lseek failed. Error was %s\n", strerror(errno) ));
60 fsp->pos = seek_ret - offset;
62 DEBUG(10,("seek_file: requested pos = %.0f, new pos = %.0f\n",
63 (double)(pos+offset), (double)fsp->pos ));
68 /****************************************************************************
69 Read from write cache if we can.
70 ****************************************************************************/
73 BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
75 write_cache *wcp = fsp->wcp;
80 if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
83 memcpy(data, wcp->data + (pos - wcp->offset), n);
85 DO_PROFILE_INC(writecache_read_hits);
90 /****************************************************************************
92 ****************************************************************************/
94 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
96 ssize_t ret=0,readret;
98 /* you can't read from print files */
99 if (fsp->print_file) {
104 * Serve from write cache if we can.
106 if(read_from_write_cache(fsp, data, pos, n))
109 flush_write_cache(fsp, READ_FLUSH);
111 if (seek_file(fsp,pos) == -1) {
112 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
117 readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
120 if (readret > 0) ret += readret;
126 /* how many write cache buffers have been allocated */
127 static unsigned int allocated_write_caches;
129 /****************************************************************************
130 *Really* write to a file.
131 ****************************************************************************/
133 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
135 if ((pos != -1) && (seek_file(fsp,pos) == -1))
138 return vfs_write_data(fsp,data,n);
141 /****************************************************************************
143 ****************************************************************************/
145 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
147 write_cache *wcp = fsp->wcp;
148 ssize_t total_written = 0;
151 if (fsp->print_file) {
152 return print_job_write(fsp->print_jobid, data, n);
155 if (!fsp->can_write) {
160 if (!fsp->modified) {
162 fsp->modified = True;
164 if (fsp->conn->vfs_ops.fstat(fsp,fsp->fd,&st) == 0) {
165 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
166 if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) {
167 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
171 * If this is the first write and we have an exclusive oplock then setup
175 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
176 setup_write_cache(fsp, st.st_size);
183 DO_PROFILE_INC(writecache_total_writes);
184 if (!fsp->oplock_type) {
185 DO_PROFILE_INC(writecache_non_oplock_writes);
190 * If this file is level II oplocked then we need
191 * to grab the shared memory lock and inform all
192 * other files with a level II lock that they need
193 * to flush their read caches. We keep the lock over
194 * the shared memory area whilst doing this.
197 release_level_2_oplocks_on_change(fsp);
200 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
201 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
202 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
203 profile_p->writecache_init_writes,
204 profile_p->writecache_abutted_writes,
205 profile_p->writecache_total_writes,
206 profile_p->writecache_non_oplock_writes,
207 profile_p->writecache_allocated_write_caches,
208 profile_p->writecache_num_write_caches,
209 profile_p->writecache_direct_writes,
210 profile_p->writecache_num_perfect_writes,
211 profile_p->writecache_read_hits ));
213 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
214 profile_p->writecache_flushed_writes[SEEK_FLUSH],
215 profile_p->writecache_flushed_writes[READ_FLUSH],
216 profile_p->writecache_flushed_writes[WRITE_FLUSH],
217 profile_p->writecache_flushed_writes[READRAW_FLUSH],
218 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
219 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
220 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
225 DO_PROFILE_INC(writecache_direct_writes);
226 return real_write_file(fsp, data, pos, n);
229 DEBUG(9,("write_file(fd=%d pos=%d size=%d) wofs=%d wsize=%d\n",
230 fsp->fd, (int)pos, (int)n, (int)wcp->offset, (int)wcp->data_size));
233 * If we have active cache and it isn't contiguous then we flush.
234 * NOTE: There is a small problem with running out of disk ....
237 if (wcp->data_size) {
239 BOOL cache_flush_needed = False;
241 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
244 * Start of write overlaps or abutts the existing data.
247 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
249 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
252 * Update the current buffer size with the new data.
255 if(pos + data_used > wcp->offset + wcp->data_size)
256 wcp->data_size = pos + data_used - wcp->offset;
259 * If we used all the data then
266 cache_flush_needed = True;
269 * Move the start of data forward by the amount used,
270 * cut down the amount left by the same amount.
277 DO_PROFILE_INC(writecache_abutted_writes);
278 total_written = data_used;
282 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
283 (pos + n <= wcp->offset + wcp->alloc_size)) {
286 * End of write overlaps the existing data.
289 size_t data_used = pos + n - wcp->offset;
291 memcpy(wcp->data, data + n - data_used, data_used);
294 * Update the current buffer size with the new data.
297 if(pos + n > wcp->offset + wcp->data_size)
298 wcp->data_size = pos + n - wcp->offset;
301 * We don't need to move the start of data, but we
302 * cut down the amount left by the amount used.
308 * We cannot have used all the data here.
311 cache_flush_needed = True;
313 DO_PROFILE_INC(writecache_abutted_writes);
314 total_written = data_used;
318 } else if ( (pos >= wcp->file_size) &&
319 (pos > wcp->offset + wcp->data_size) &&
320 (pos < wcp->offset + wcp->alloc_size) ) {
323 * Non-contiguous write part of which fits within
324 * the cache buffer and is extending the file.
329 if(pos + n <= wcp->offset + wcp->alloc_size)
332 data_used = wcp->offset + wcp->alloc_size - pos;
335 * Fill in the non-continuous area with zeros.
338 memset(wcp->data + wcp->data_size, '\0',
339 pos - (wcp->offset + wcp->data_size) );
341 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
344 * Update the current buffer size with the new data.
347 if(pos + data_used > wcp->offset + wcp->data_size)
348 wcp->data_size = pos + data_used - wcp->offset;
351 * Update the known file length.
354 wcp->file_size = wcp->offset + wcp->data_size;
357 * If we used all the data then
364 cache_flush_needed = True;
367 * Move the start of data forward by the amount used,
368 * cut down the amount left by the same amount.
375 DO_PROFILE_INC(writecache_abutted_writes);
376 total_written = data_used;
383 * Write is bigger than buffer, or there is no overlap on the
387 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
388 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
391 * Update the file size if needed.
394 if(pos + n > wcp->file_size)
395 wcp->file_size = pos + n;
398 * If write would fit in the cache, and is larger than
399 * the data already in the cache, flush the cache and
400 * preferentially copy the data new data into it. Otherwise
401 * just write the data directly.
404 if ( n <= wcp->alloc_size && n > wcp->data_size) {
405 cache_flush_needed = True;
407 DO_PROFILE_INC(writecache_direct_writes);
408 return real_write_file(fsp, data, pos, n);
415 if(wcp->data_size > wcp->file_size)
416 wcp->file_size = wcp->data_size;
418 if (cache_flush_needed) {
419 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
420 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
421 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
422 (double)wcp->offset, (unsigned int)wcp->data_size ));
424 flush_write_cache(fsp, WRITE_FLUSH);
429 * If the write request is bigger than the cache
430 * size, write it all out.
433 if (n > wcp->alloc_size ) {
434 if(real_write_file(fsp, data, pos, n) == -1)
436 DO_PROFILE_INC(writecache_direct_writes);
437 return total_written + n;
441 * If there's any data left, cache it.
446 if (wcp->data_size) {
447 DO_PROFILE_INC(writecache_abutted_writes);
449 DO_PROFILE_INC(writecache_init_writes);
452 memcpy(wcp->data+wcp->data_size, data, n);
453 if (wcp->data_size == 0) {
455 DO_PROFILE_INC(writecache_num_write_caches);
458 DEBUG(9,("cache return %u\n", (unsigned int)n));
460 return total_written; /* .... that's a write :) */
463 return total_written;
466 /****************************************************************************
467 Delete the write cache structure.
468 ****************************************************************************/
470 void delete_write_cache(files_struct *fsp)
477 if(!(wcp = fsp->wcp))
480 DO_PROFILE_DEC(writecache_allocated_write_caches);
481 allocated_write_caches--;
483 SMB_ASSERT(wcp->data_size == 0);
490 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
494 /****************************************************************************
495 Setup the write cache structure.
496 ****************************************************************************/
498 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
500 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
503 if (allocated_write_caches >= MAX_WRITE_CACHES)
506 if(alloc_size == 0 || fsp->wcp)
509 if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
510 DEBUG(0,("setup_write_cache: malloc fail.\n"));
514 wcp->file_size = file_size;
516 wcp->alloc_size = alloc_size;
518 if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
519 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
520 (unsigned int)wcp->alloc_size ));
526 DO_PROFILE_INC(writecache_allocated_write_caches);
527 allocated_write_caches++;
529 DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
530 fsp->fsp_name, wcp->alloc_size ));
535 /****************************************************************************
536 Cope with a size change.
537 ****************************************************************************/
539 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
542 flush_write_cache(fsp, SIZECHANGE_FLUSH);
543 fsp->wcp->file_size = file_size;
547 /*******************************************************************
548 Flush a write cache struct to disk.
549 ********************************************************************/
551 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
553 write_cache *wcp = fsp->wcp;
556 if(!wcp || !wcp->data_size)
559 data_size = wcp->data_size;
562 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
564 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
565 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
568 if(data_size == wcp->alloc_size)
569 DO_PROFILE_INC(writecache_num_perfect_writes);
572 return real_write_file(fsp, wcp->data, wcp->offset, data_size);
575 /*******************************************************************
577 ********************************************************************/
579 void sync_file(connection_struct *conn, files_struct *fsp)
581 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
582 flush_write_cache(fsp, SYNC_FLUSH);
583 conn->vfs_ops.fsync(fsp,fsp->fd);