r4348: syncing up for 3.0.11pre1
[Samba.git] / source / smbd / fileio.c
bloba21bd69a36c634c232e9477c91e967f03618aa30
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
23 #include "includes.h"
25 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
27 /****************************************************************************
28 Read from write cache if we can.
29 ****************************************************************************/
32 static BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
34 write_cache *wcp = fsp->wcp;
36 if(!wcp)
37 return False;
39 if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
40 return False;
42 memcpy(data, wcp->data + (pos - wcp->offset), n);
44 DO_PROFILE_INC(writecache_read_hits);
46 return True;
49 /****************************************************************************
50 Read from a file.
51 ****************************************************************************/
53 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
55 ssize_t ret=0,readret;
57 /* you can't read from print files */
58 if (fsp->print_file)
59 return -1;
62 * Serve from write cache if we can.
65 if(read_from_write_cache(fsp, data, pos, n)) {
66 fsp->pos = pos + n;
67 fsp->position_information = fsp->pos;
68 return n;
71 flush_write_cache(fsp, READ_FLUSH);
73 fsp->pos = pos;
75 if (n > 0) {
76 #ifdef DMF_FIX
77 int numretries = 3;
78 tryagain:
79 readret = SMB_VFS_PREAD(fsp,fsp->fd,data,n,pos);
81 if (readret == -1) {
82 if ((errno == EAGAIN) && numretries) {
83 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
84 (void)sleep(10);
85 --numretries;
86 goto tryagain;
88 return -1;
90 #else /* NO DMF fix. */
91 readret = SMB_VFS_PREAD(fsp,fsp->fd,data,n,pos);
93 if (readret == -1)
94 return -1;
95 #endif
96 if (readret > 0)
97 ret += readret;
100 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
101 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
103 fsp->pos += ret;
104 fsp->position_information = fsp->pos;
106 return(ret);
109 /* how many write cache buffers have been allocated */
110 static unsigned int allocated_write_caches;
112 /****************************************************************************
113 *Really* write to a file.
114 ****************************************************************************/
116 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
118 ssize_t ret;
120 if (pos == -1)
121 ret = vfs_write_data(fsp, data, n);
122 else {
123 fsp->pos = pos;
124 ret = vfs_pwrite_data(fsp, data, n, pos);
127 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
128 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
130 if (ret != -1) {
131 fsp->pos += ret;
134 * It turns out that setting the last write time from a Windows
135 * client stops any subsequent writes from updating the write time.
136 * Doing this after the write gives a race condition here where
137 * a stat may see the changed write time before we reset it here,
138 * but it's cheaper than having to store the write time in shared
139 * memory and look it up using dev/inode across all running smbd's.
140 * The 99% solution will hopefully be good enough in this case. JRA.
143 if (fsp->pending_modtime) {
144 set_filetime(fsp->conn, fsp->fsp_name, fsp->pending_modtime);
147 /* Yes - this is correct - writes don't update this. JRA. */
148 /* Found by Samba4 tests. */
149 #if 0
150 fsp->position_information = fsp->pos;
151 #endif
154 return ret;
157 /****************************************************************************
158 write to a file
159 ****************************************************************************/
161 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
163 write_cache *wcp = fsp->wcp;
164 ssize_t total_written = 0;
165 int write_path = -1;
167 if (fsp->print_file) {
168 fstring sharename;
169 uint32 jobid;
171 if (!rap_to_pjobid(fsp->rap_print_jobid, sharename, &jobid)) {
172 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
173 (unsigned int)fsp->rap_print_jobid ));
174 errno = EBADF;
175 return -1;
178 return print_job_write(SNUM(fsp->conn), jobid, data, n);
181 if (!fsp->can_write) {
182 errno = EPERM;
183 return(0);
186 if (!fsp->modified) {
187 SMB_STRUCT_STAT st;
188 fsp->modified = True;
190 if (SMB_VFS_FSTAT(fsp,fsp->fd,&st) == 0) {
191 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
192 fsp->size = (SMB_BIG_UINT)st.st_size;
193 if ((lp_store_dos_attributes(SNUM(fsp->conn)) || MAP_ARCHIVE(fsp->conn)) && !IS_DOS_ARCHIVE(dosmode)) {
194 file_set_dosmode(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st, False);
198 * If this is the first write and we have an exclusive oplock then setup
199 * the write cache.
202 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
203 setup_write_cache(fsp, st.st_size);
204 wcp = fsp->wcp;
209 #ifdef WITH_PROFILE
210 DO_PROFILE_INC(writecache_total_writes);
211 if (!fsp->oplock_type) {
212 DO_PROFILE_INC(writecache_non_oplock_writes);
214 #endif
217 * If this file is level II oplocked then we need
218 * to grab the shared memory lock and inform all
219 * other files with a level II lock that they need
220 * to flush their read caches. We keep the lock over
221 * the shared memory area whilst doing this.
224 release_level_2_oplocks_on_change(fsp);
226 #ifdef WITH_PROFILE
227 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
228 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
229 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
230 profile_p->writecache_init_writes,
231 profile_p->writecache_abutted_writes,
232 profile_p->writecache_total_writes,
233 profile_p->writecache_non_oplock_writes,
234 profile_p->writecache_allocated_write_caches,
235 profile_p->writecache_num_write_caches,
236 profile_p->writecache_direct_writes,
237 profile_p->writecache_num_perfect_writes,
238 profile_p->writecache_read_hits ));
240 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
241 profile_p->writecache_flushed_writes[SEEK_FLUSH],
242 profile_p->writecache_flushed_writes[READ_FLUSH],
243 profile_p->writecache_flushed_writes[WRITE_FLUSH],
244 profile_p->writecache_flushed_writes[READRAW_FLUSH],
245 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
246 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
247 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
249 #endif
251 if(!wcp) {
252 DO_PROFILE_INC(writecache_direct_writes);
253 total_written = real_write_file(fsp, data, pos, n);
254 if ((total_written != -1) && (pos + total_written > (SMB_OFF_T)fsp->size))
255 fsp->size = (SMB_BIG_UINT)(pos + total_written);
256 return total_written;
259 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
260 fsp->fsp_name, fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
262 fsp->pos = pos + n;
265 * If we have active cache and it isn't contiguous then we flush.
266 * NOTE: There is a small problem with running out of disk ....
269 if (wcp->data_size) {
271 BOOL cache_flush_needed = False;
273 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
275 /* ASCII art.... JRA.
277 +--------------+-----
278 | Cached data | Rest of allocated cache buffer....
279 +--------------+-----
281 +-------------------+
282 | Data to write |
283 +-------------------+
288 * Start of write overlaps or abutts the existing data.
291 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
293 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
296 * Update the current buffer size with the new data.
299 if(pos + data_used > wcp->offset + wcp->data_size)
300 wcp->data_size = pos + data_used - wcp->offset;
303 * Update the file size if changed.
306 if (wcp->offset + wcp->data_size > wcp->file_size) {
307 wcp->file_size = wcp->offset + wcp->data_size;
308 fsp->size = (SMB_BIG_UINT)wcp->file_size;
312 * If we used all the data then
313 * return here.
316 if(n == data_used)
317 return n;
318 else
319 cache_flush_needed = True;
322 * Move the start of data forward by the amount used,
323 * cut down the amount left by the same amount.
326 data += data_used;
327 pos += data_used;
328 n -= data_used;
330 DO_PROFILE_INC(writecache_abutted_writes);
331 total_written = data_used;
333 write_path = 1;
335 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
336 (pos + n <= wcp->offset + wcp->alloc_size)) {
338 /* ASCII art.... JRA.
340 +---------------+
341 | Cache buffer |
342 +---------------+
344 +-------------------+
345 | Data to write |
346 +-------------------+
351 * End of write overlaps the existing data.
354 size_t data_used = pos + n - wcp->offset;
356 memcpy(wcp->data, data + n - data_used, data_used);
359 * Update the current buffer size with the new data.
362 if(pos + n > wcp->offset + wcp->data_size)
363 wcp->data_size = pos + n - wcp->offset;
366 * Update the file size if changed.
369 if (wcp->offset + wcp->data_size > wcp->file_size) {
370 wcp->file_size = wcp->offset + wcp->data_size;
371 fsp->size = (SMB_BIG_UINT)wcp->file_size;
375 * We don't need to move the start of data, but we
376 * cut down the amount left by the amount used.
379 n -= data_used;
382 * We cannot have used all the data here.
385 cache_flush_needed = True;
387 DO_PROFILE_INC(writecache_abutted_writes);
388 total_written = data_used;
390 write_path = 2;
392 } else if ( (pos >= wcp->file_size) &&
393 (wcp->offset + wcp->data_size == wcp->file_size) &&
394 (pos > wcp->offset + wcp->data_size) &&
395 (pos < wcp->offset + wcp->alloc_size) ) {
397 /* ASCII art.... JRA.
399 End of file ---->|
401 +---------------+---------------+
402 | Cached data | Cache buffer |
403 +---------------+---------------+
405 +-------------------+
406 | Data to write |
407 +-------------------+
412 * Non-contiguous write part of which fits within
413 * the cache buffer and is extending the file
414 * and the cache contents reflect the current
415 * data up to the current end of the file.
418 size_t data_used;
420 if(pos + n <= wcp->offset + wcp->alloc_size)
421 data_used = n;
422 else
423 data_used = wcp->offset + wcp->alloc_size - pos;
426 * Fill in the non-continuous area with zeros.
429 memset(wcp->data + wcp->data_size, '\0',
430 pos - (wcp->offset + wcp->data_size) );
432 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
435 * Update the current buffer size with the new data.
438 if(pos + data_used > wcp->offset + wcp->data_size)
439 wcp->data_size = pos + data_used - wcp->offset;
442 * Update the file size if changed.
445 if (wcp->offset + wcp->data_size > wcp->file_size) {
446 wcp->file_size = wcp->offset + wcp->data_size;
447 fsp->size = (SMB_BIG_UINT)wcp->file_size;
451 * If we used all the data then
452 * return here.
455 if(n == data_used)
456 return n;
457 else
458 cache_flush_needed = True;
461 * Move the start of data forward by the amount used,
462 * cut down the amount left by the same amount.
465 data += data_used;
466 pos += data_used;
467 n -= data_used;
469 DO_PROFILE_INC(writecache_abutted_writes);
470 total_written = data_used;
472 write_path = 3;
474 } else {
476 /* ASCII art..... JRA.
478 Case 1).
480 +---------------+---------------+
481 | Cached data | Cache buffer |
482 +---------------+---------------+
484 +-------------------+
485 | Data to write |
486 +-------------------+
488 Case 2).
490 +---------------+---------------+
491 | Cached data | Cache buffer |
492 +---------------+---------------+
494 +-------------------+
495 | Data to write |
496 +-------------------+
498 Case 3).
500 +---------------+---------------+
501 | Cached data | Cache buffer |
502 +---------------+---------------+
504 +-----------------------------------------------------+
505 | Data to write |
506 +-----------------------------------------------------+
511 * Write is bigger than buffer, or there is no overlap on the
512 * low or high ends.
515 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
516 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
519 * Update the file size if needed.
522 if(pos + n > wcp->file_size) {
523 wcp->file_size = pos + n;
524 fsp->size = (SMB_BIG_UINT)wcp->file_size;
528 * If write would fit in the cache, and is larger than
529 * the data already in the cache, flush the cache and
530 * preferentially copy the data new data into it. Otherwise
531 * just write the data directly.
534 if ( n <= wcp->alloc_size && n > wcp->data_size) {
535 cache_flush_needed = True;
536 } else {
537 ssize_t ret = real_write_file(fsp, data, pos, n);
540 * If the write overlaps the entire cache, then
541 * discard the current contents of the cache.
542 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
545 if ((pos <= wcp->offset) &&
546 (pos + n >= wcp->offset + wcp->data_size) ) {
547 DEBUG(9,("write_file: discarding overwritten write \
548 cache: fd = %d, off=%.0f, size=%u\n", fsp->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
549 wcp->data_size = 0;
552 DO_PROFILE_INC(writecache_direct_writes);
553 if (ret == -1)
554 return ret;
556 if (pos + ret > wcp->file_size) {
557 wcp->file_size = pos + ret;
558 fsp->size = (SMB_BIG_UINT)wcp->file_size;
561 return ret;
564 write_path = 4;
568 if(wcp->data_size > wcp->file_size) {
569 wcp->file_size = wcp->data_size;
570 fsp->size = (SMB_BIG_UINT)wcp->file_size;
573 if (cache_flush_needed) {
574 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
575 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
576 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
577 (double)wcp->offset, (unsigned int)wcp->data_size ));
579 flush_write_cache(fsp, WRITE_FLUSH);
584 * If the write request is bigger than the cache
585 * size, write it all out.
588 if (n > wcp->alloc_size ) {
589 ssize_t ret = real_write_file(fsp, data, pos, n);
590 if (ret == -1)
591 return -1;
593 if (pos + ret > wcp->file_size) {
594 wcp->file_size = pos + n;
595 fsp->size = (SMB_BIG_UINT)wcp->file_size;
598 DO_PROFILE_INC(writecache_direct_writes);
599 return total_written + n;
603 * If there's any data left, cache it.
606 if (n) {
607 #ifdef WITH_PROFILE
608 if (wcp->data_size) {
609 DO_PROFILE_INC(writecache_abutted_writes);
610 } else {
611 DO_PROFILE_INC(writecache_init_writes);
613 #endif
614 memcpy(wcp->data+wcp->data_size, data, n);
615 if (wcp->data_size == 0) {
616 wcp->offset = pos;
617 DO_PROFILE_INC(writecache_num_write_caches);
619 wcp->data_size += n;
622 * Update the file size if changed.
625 if (wcp->offset + wcp->data_size > wcp->file_size) {
626 wcp->file_size = wcp->offset + wcp->data_size;
627 fsp->size = (SMB_BIG_UINT)wcp->file_size;
629 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
630 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
632 total_written += n;
633 return total_written; /* .... that's a write :) */
636 return total_written;
639 /****************************************************************************
640 Delete the write cache structure.
641 ****************************************************************************/
643 void delete_write_cache(files_struct *fsp)
645 write_cache *wcp;
647 if(!fsp)
648 return;
650 if(!(wcp = fsp->wcp))
651 return;
653 DO_PROFILE_DEC(writecache_allocated_write_caches);
654 allocated_write_caches--;
656 SMB_ASSERT(wcp->data_size == 0);
658 SAFE_FREE(wcp->data);
659 SAFE_FREE(fsp->wcp);
661 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
664 /****************************************************************************
665 Setup the write cache structure.
666 ****************************************************************************/
668 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
670 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
671 write_cache *wcp;
673 if (allocated_write_caches >= MAX_WRITE_CACHES)
674 return False;
676 if(alloc_size == 0 || fsp->wcp)
677 return False;
679 if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
680 DEBUG(0,("setup_write_cache: malloc fail.\n"));
681 return False;
684 wcp->file_size = file_size;
685 wcp->offset = 0;
686 wcp->alloc_size = alloc_size;
687 wcp->data_size = 0;
688 if((wcp->data = SMB_MALLOC(wcp->alloc_size)) == NULL) {
689 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
690 (unsigned int)wcp->alloc_size ));
691 SAFE_FREE(wcp);
692 return False;
695 memset(wcp->data, '\0', wcp->alloc_size );
697 fsp->wcp = wcp;
698 DO_PROFILE_INC(writecache_allocated_write_caches);
699 allocated_write_caches++;
701 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
702 fsp->fsp_name, (unsigned long)wcp->alloc_size ));
704 return True;
707 /****************************************************************************
708 Cope with a size change.
709 ****************************************************************************/
711 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
713 fsp->size = (SMB_BIG_UINT)file_size;
714 if(fsp->wcp) {
715 /* The cache *must* have been flushed before we do this. */
716 if (fsp->wcp->data_size != 0) {
717 pstring msg;
718 slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
719 on file %s with write cache size = %lu\n", fsp->fsp_name, (unsigned long)fsp->wcp->data_size );
720 smb_panic(msg);
722 fsp->wcp->file_size = file_size;
726 /*******************************************************************
727 Flush a write cache struct to disk.
728 ********************************************************************/
730 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
732 write_cache *wcp = fsp->wcp;
733 size_t data_size;
734 ssize_t ret;
736 if(!wcp || !wcp->data_size)
737 return 0;
739 data_size = wcp->data_size;
740 wcp->data_size = 0;
742 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
744 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
745 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
747 #ifdef WITH_PROFILE
748 if(data_size == wcp->alloc_size)
749 DO_PROFILE_INC(writecache_num_perfect_writes);
750 #endif
752 ret = real_write_file(fsp, wcp->data, wcp->offset, data_size);
755 * Ensure file size if kept up to date if write extends file.
758 if ((ret != -1) && (wcp->offset + ret > wcp->file_size))
759 wcp->file_size = wcp->offset + ret;
761 return ret;
764 /*******************************************************************
765 sync a file
766 ********************************************************************/
768 void sync_file(connection_struct *conn, files_struct *fsp)
770 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
771 flush_write_cache(fsp, SYNC_FLUSH);
772 SMB_VFS_FSYNC(fsp,fsp->fd);
777 /************************************************************
778 Perform a stat whether a valid fd or not.
779 ************************************************************/
781 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
783 if (fsp->fd == -1)
784 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name, pst);
785 else
786 return SMB_VFS_FSTAT(fsp,fsp->fd, pst);