missed one on BUG 1195; make sure to set the private * to NULL
[Samba/gebeck_regimport.git] / source3 / smbd / fileio.c
blob3462a3b9fa57ff03324b838019e0104a50aea479
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;
133 /* Yes - this is correct - writes don't update this. JRA. */
134 /* Found by Samba4 tests. */
135 #if 0
136 fsp->position_information = fsp->pos;
137 #endif
140 return ret;
143 /****************************************************************************
144 write to a file
145 ****************************************************************************/
147 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
149 write_cache *wcp = fsp->wcp;
150 ssize_t total_written = 0;
151 int write_path = -1;
153 if (fsp->print_file) {
154 int snum;
155 uint32 jobid;
157 if (!rap_to_pjobid(fsp->rap_print_jobid, &snum, &jobid)) {
158 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
159 (unsigned int)fsp->rap_print_jobid ));
160 errno = EBADF;
161 return -1;
164 return print_job_write(SNUM(fsp->conn), jobid, data, n);
167 if (!fsp->can_write) {
168 errno = EPERM;
169 return(0);
172 if (!fsp->modified) {
173 SMB_STRUCT_STAT st;
174 fsp->modified = True;
176 if (SMB_VFS_FSTAT(fsp,fsp->fd,&st) == 0) {
177 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
178 fsp->size = (SMB_BIG_UINT)st.st_size;
179 if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode))
180 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
183 * If this is the first write and we have an exclusive oplock then setup
184 * the write cache.
187 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
188 setup_write_cache(fsp, st.st_size);
189 wcp = fsp->wcp;
194 #ifdef WITH_PROFILE
195 DO_PROFILE_INC(writecache_total_writes);
196 if (!fsp->oplock_type) {
197 DO_PROFILE_INC(writecache_non_oplock_writes);
199 #endif
202 * If this file is level II oplocked then we need
203 * to grab the shared memory lock and inform all
204 * other files with a level II lock that they need
205 * to flush their read caches. We keep the lock over
206 * the shared memory area whilst doing this.
209 release_level_2_oplocks_on_change(fsp);
211 #ifdef WITH_PROFILE
212 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
213 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
214 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
215 profile_p->writecache_init_writes,
216 profile_p->writecache_abutted_writes,
217 profile_p->writecache_total_writes,
218 profile_p->writecache_non_oplock_writes,
219 profile_p->writecache_allocated_write_caches,
220 profile_p->writecache_num_write_caches,
221 profile_p->writecache_direct_writes,
222 profile_p->writecache_num_perfect_writes,
223 profile_p->writecache_read_hits ));
225 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
226 profile_p->writecache_flushed_writes[SEEK_FLUSH],
227 profile_p->writecache_flushed_writes[READ_FLUSH],
228 profile_p->writecache_flushed_writes[WRITE_FLUSH],
229 profile_p->writecache_flushed_writes[READRAW_FLUSH],
230 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
231 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
232 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
234 #endif
236 if(!wcp) {
237 DO_PROFILE_INC(writecache_direct_writes);
238 total_written = real_write_file(fsp, data, pos, n);
239 if ((total_written != -1) && (pos + total_written > (SMB_OFF_T)fsp->size))
240 fsp->size = (SMB_BIG_UINT)(pos + total_written);
241 return total_written;
244 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
245 fsp->fsp_name, fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
247 fsp->pos = pos + n;
250 * If we have active cache and it isn't contiguous then we flush.
251 * NOTE: There is a small problem with running out of disk ....
254 if (wcp->data_size) {
256 BOOL cache_flush_needed = False;
258 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
260 /* ASCII art.... JRA.
262 +--------------+-----
263 | Cached data | Rest of allocated cache buffer....
264 +--------------+-----
266 +-------------------+
267 | Data to write |
268 +-------------------+
273 * Start of write overlaps or abutts the existing data.
276 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
278 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
281 * Update the current buffer size with the new data.
284 if(pos + data_used > wcp->offset + wcp->data_size)
285 wcp->data_size = pos + data_used - wcp->offset;
288 * Update the file size if changed.
291 if (wcp->offset + wcp->data_size > wcp->file_size) {
292 wcp->file_size = wcp->offset + wcp->data_size;
293 fsp->size = (SMB_BIG_UINT)wcp->file_size;
297 * If we used all the data then
298 * return here.
301 if(n == data_used)
302 return n;
303 else
304 cache_flush_needed = True;
307 * Move the start of data forward by the amount used,
308 * cut down the amount left by the same amount.
311 data += data_used;
312 pos += data_used;
313 n -= data_used;
315 DO_PROFILE_INC(writecache_abutted_writes);
316 total_written = data_used;
318 write_path = 1;
320 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
321 (pos + n <= wcp->offset + wcp->alloc_size)) {
323 /* ASCII art.... JRA.
325 +---------------+
326 | Cache buffer |
327 +---------------+
329 +-------------------+
330 | Data to write |
331 +-------------------+
336 * End of write overlaps the existing data.
339 size_t data_used = pos + n - wcp->offset;
341 memcpy(wcp->data, data + n - data_used, data_used);
344 * Update the current buffer size with the new data.
347 if(pos + n > wcp->offset + wcp->data_size)
348 wcp->data_size = pos + n - wcp->offset;
351 * Update the file size if changed.
354 if (wcp->offset + wcp->data_size > wcp->file_size) {
355 wcp->file_size = wcp->offset + wcp->data_size;
356 fsp->size = (SMB_BIG_UINT)wcp->file_size;
360 * We don't need to move the start of data, but we
361 * cut down the amount left by the amount used.
364 n -= data_used;
367 * We cannot have used all the data here.
370 cache_flush_needed = True;
372 DO_PROFILE_INC(writecache_abutted_writes);
373 total_written = data_used;
375 write_path = 2;
377 } else if ( (pos >= wcp->file_size) &&
378 (wcp->offset + wcp->data_size == wcp->file_size) &&
379 (pos > wcp->offset + wcp->data_size) &&
380 (pos < wcp->offset + wcp->alloc_size) ) {
382 /* ASCII art.... JRA.
384 End of file ---->|
386 +---------------+---------------+
387 | Cached data | Cache buffer |
388 +---------------+---------------+
390 +-------------------+
391 | Data to write |
392 +-------------------+
397 * Non-contiguous write part of which fits within
398 * the cache buffer and is extending the file
399 * and the cache contents reflect the current
400 * data up to the current end of the file.
403 size_t data_used;
405 if(pos + n <= wcp->offset + wcp->alloc_size)
406 data_used = n;
407 else
408 data_used = wcp->offset + wcp->alloc_size - pos;
411 * Fill in the non-continuous area with zeros.
414 memset(wcp->data + wcp->data_size, '\0',
415 pos - (wcp->offset + wcp->data_size) );
417 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
420 * Update the current buffer size with the new data.
423 if(pos + data_used > wcp->offset + wcp->data_size)
424 wcp->data_size = pos + data_used - wcp->offset;
427 * Update the file size if changed.
430 if (wcp->offset + wcp->data_size > wcp->file_size) {
431 wcp->file_size = wcp->offset + wcp->data_size;
432 fsp->size = (SMB_BIG_UINT)wcp->file_size;
436 * If we used all the data then
437 * return here.
440 if(n == data_used)
441 return n;
442 else
443 cache_flush_needed = True;
446 * Move the start of data forward by the amount used,
447 * cut down the amount left by the same amount.
450 data += data_used;
451 pos += data_used;
452 n -= data_used;
454 DO_PROFILE_INC(writecache_abutted_writes);
455 total_written = data_used;
457 write_path = 3;
459 } else {
461 /* ASCII art..... JRA.
463 Case 1).
465 +---------------+---------------+
466 | Cached data | Cache buffer |
467 +---------------+---------------+
469 +-------------------+
470 | Data to write |
471 +-------------------+
473 Case 2).
475 +---------------+---------------+
476 | Cached data | Cache buffer |
477 +---------------+---------------+
479 +-------------------+
480 | Data to write |
481 +-------------------+
483 Case 3).
485 +---------------+---------------+
486 | Cached data | Cache buffer |
487 +---------------+---------------+
489 +-----------------------------------------------------+
490 | Data to write |
491 +-----------------------------------------------------+
496 * Write is bigger than buffer, or there is no overlap on the
497 * low or high ends.
500 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
501 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
504 * Update the file size if needed.
507 if(pos + n > wcp->file_size) {
508 wcp->file_size = pos + n;
509 fsp->size = (SMB_BIG_UINT)wcp->file_size;
513 * If write would fit in the cache, and is larger than
514 * the data already in the cache, flush the cache and
515 * preferentially copy the data new data into it. Otherwise
516 * just write the data directly.
519 if ( n <= wcp->alloc_size && n > wcp->data_size) {
520 cache_flush_needed = True;
521 } else {
522 ssize_t ret = real_write_file(fsp, data, pos, n);
525 * If the write overlaps the entire cache, then
526 * discard the current contents of the cache.
527 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
530 if ((pos <= wcp->offset) &&
531 (pos + n >= wcp->offset + wcp->data_size) ) {
532 DEBUG(9,("write_file: discarding overwritten write \
533 cache: fd = %d, off=%.0f, size=%u\n", fsp->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
534 wcp->data_size = 0;
537 DO_PROFILE_INC(writecache_direct_writes);
538 if (ret == -1)
539 return ret;
541 if (pos + ret > wcp->file_size) {
542 wcp->file_size = pos + ret;
543 fsp->size = (SMB_BIG_UINT)wcp->file_size;
546 return ret;
549 write_path = 4;
553 if(wcp->data_size > wcp->file_size) {
554 wcp->file_size = wcp->data_size;
555 fsp->size = (SMB_BIG_UINT)wcp->file_size;
558 if (cache_flush_needed) {
559 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
560 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
561 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
562 (double)wcp->offset, (unsigned int)wcp->data_size ));
564 flush_write_cache(fsp, WRITE_FLUSH);
569 * If the write request is bigger than the cache
570 * size, write it all out.
573 if (n > wcp->alloc_size ) {
574 ssize_t ret = real_write_file(fsp, data, pos, n);
575 if (ret == -1)
576 return -1;
578 if (pos + ret > wcp->file_size) {
579 wcp->file_size = pos + n;
580 fsp->size = (SMB_BIG_UINT)wcp->file_size;
583 DO_PROFILE_INC(writecache_direct_writes);
584 return total_written + n;
588 * If there's any data left, cache it.
591 if (n) {
592 #ifdef WITH_PROFILE
593 if (wcp->data_size) {
594 DO_PROFILE_INC(writecache_abutted_writes);
595 } else {
596 DO_PROFILE_INC(writecache_init_writes);
598 #endif
599 memcpy(wcp->data+wcp->data_size, data, n);
600 if (wcp->data_size == 0) {
601 wcp->offset = pos;
602 DO_PROFILE_INC(writecache_num_write_caches);
604 wcp->data_size += n;
607 * Update the file size if changed.
610 if (wcp->offset + wcp->data_size > wcp->file_size) {
611 wcp->file_size = wcp->offset + wcp->data_size;
612 fsp->size = (SMB_BIG_UINT)wcp->file_size;
614 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
615 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
617 total_written += n;
618 return total_written; /* .... that's a write :) */
621 return total_written;
624 /****************************************************************************
625 Delete the write cache structure.
626 ****************************************************************************/
628 void delete_write_cache(files_struct *fsp)
630 write_cache *wcp;
632 if(!fsp)
633 return;
635 if(!(wcp = fsp->wcp))
636 return;
638 DO_PROFILE_DEC(writecache_allocated_write_caches);
639 allocated_write_caches--;
641 SMB_ASSERT(wcp->data_size == 0);
643 SAFE_FREE(wcp->data);
644 SAFE_FREE(fsp->wcp);
646 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
649 /****************************************************************************
650 Setup the write cache structure.
651 ****************************************************************************/
653 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
655 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
656 write_cache *wcp;
658 if (allocated_write_caches >= MAX_WRITE_CACHES)
659 return False;
661 if(alloc_size == 0 || fsp->wcp)
662 return False;
664 if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
665 DEBUG(0,("setup_write_cache: malloc fail.\n"));
666 return False;
669 wcp->file_size = file_size;
670 wcp->offset = 0;
671 wcp->alloc_size = alloc_size;
672 wcp->data_size = 0;
673 if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
674 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
675 (unsigned int)wcp->alloc_size ));
676 SAFE_FREE(wcp);
677 return False;
680 memset(wcp->data, '\0', wcp->alloc_size );
682 fsp->wcp = wcp;
683 DO_PROFILE_INC(writecache_allocated_write_caches);
684 allocated_write_caches++;
686 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
687 fsp->fsp_name, (unsigned long)wcp->alloc_size ));
689 return True;
692 /****************************************************************************
693 Cope with a size change.
694 ****************************************************************************/
696 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
698 fsp->size = (SMB_BIG_UINT)file_size;
699 if(fsp->wcp) {
700 /* The cache *must* have been flushed before we do this. */
701 if (fsp->wcp->data_size != 0) {
702 pstring msg;
703 slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
704 on file %s with write cache size = %lu\n", fsp->fsp_name, (unsigned long)fsp->wcp->data_size );
705 smb_panic(msg);
707 fsp->wcp->file_size = file_size;
711 /*******************************************************************
712 Flush a write cache struct to disk.
713 ********************************************************************/
715 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
717 write_cache *wcp = fsp->wcp;
718 size_t data_size;
719 ssize_t ret;
721 if(!wcp || !wcp->data_size)
722 return 0;
724 data_size = wcp->data_size;
725 wcp->data_size = 0;
727 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
729 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
730 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
732 #ifdef WITH_PROFILE
733 if(data_size == wcp->alloc_size)
734 DO_PROFILE_INC(writecache_num_perfect_writes);
735 #endif
737 ret = real_write_file(fsp, wcp->data, wcp->offset, data_size);
740 * Ensure file size if kept up to date if write extends file.
743 if ((ret != -1) && (wcp->offset + ret > wcp->file_size))
744 wcp->file_size = wcp->offset + ret;
746 return ret;
749 /*******************************************************************
750 sync a file
751 ********************************************************************/
753 void sync_file(connection_struct *conn, files_struct *fsp)
755 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
756 flush_write_cache(fsp, SYNC_FLUSH);
757 SMB_VFS_FSYNC(fsp,fsp->fd);
762 /************************************************************
763 Perform a stat whether a valid fd or not.
764 ************************************************************/
766 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
768 if (fsp->fd == -1)
769 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name, pst);
770 else
771 return SMB_VFS_FSTAT(fsp,fsp->fd, pst);