Merge of 64-bit printf warning fixes.
[Samba/gebeck_regimport.git] / source3 / smbd / fileio.c
blob84339c3a6fc48cf9f3f933438df50b9f0a2450e8
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 Seek a file. Try to avoid the seek if possible.
29 ****************************************************************************/
31 static SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
33 SMB_OFF_T seek_ret;
35 seek_ret = SMB_VFS_LSEEK(fsp,fsp->fd,pos,SEEK_SET);
37 if(seek_ret == -1) {
38 DEBUG(0,("seek_file: (%s) sys_lseek failed. Error was %s\n",
39 fsp->fsp_name, strerror(errno) ));
40 fsp->pos = -1;
41 return -1;
44 fsp->pos = seek_ret;
46 DEBUG(10,("seek_file (%s): requested pos = %.0f, new pos = %.0f\n",
47 fsp->fsp_name, (double)pos, (double)fsp->pos ));
49 return(fsp->pos);
52 /****************************************************************************
53 Read from write cache if we can.
54 ****************************************************************************/
57 static BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
59 write_cache *wcp = fsp->wcp;
61 if(!wcp)
62 return False;
64 if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
65 return False;
67 memcpy(data, wcp->data + (pos - wcp->offset), n);
69 DO_PROFILE_INC(writecache_read_hits);
71 return True;
74 /****************************************************************************
75 Read from a file.
76 ****************************************************************************/
78 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
80 ssize_t ret=0,readret;
82 /* you can't read from print files */
83 if (fsp->print_file)
84 return -1;
87 * Serve from write cache if we can.
90 if(read_from_write_cache(fsp, data, pos, n)) {
91 fsp->pos = pos + n;
92 fsp->position_information = fsp->pos;
93 return n;
96 flush_write_cache(fsp, READ_FLUSH);
98 if (seek_file(fsp,pos) == -1) {
99 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
100 return(ret);
103 if (n > 0) {
104 #ifdef DMF_FIX
105 int numretries = 3;
106 tryagain:
107 readret = SMB_VFS_READ(fsp,fsp->fd,data,n);
108 if (readret == -1) {
109 if ((errno == EAGAIN) && numretries) {
110 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
111 (void)sleep(10);
112 --numretries;
113 goto tryagain;
115 return -1;
117 #else /* NO DMF fix. */
118 readret = SMB_VFS_READ(fsp,fsp->fd,data,n);
119 if (readret == -1)
120 return -1;
121 #endif
122 if (readret > 0)
123 ret += readret;
126 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
127 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
129 fsp->pos += ret;
130 fsp->position_information = fsp->pos;
132 return(ret);
135 /* how many write cache buffers have been allocated */
136 static unsigned int allocated_write_caches;
138 /****************************************************************************
139 *Really* write to a file.
140 ****************************************************************************/
142 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
144 ssize_t ret;
146 if ((pos != -1) && (seek_file(fsp,pos) == -1))
147 return -1;
149 ret = vfs_write_data(fsp,data,n);
151 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
152 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
154 if (ret != -1) {
155 fsp->pos += ret;
157 /* Yes - this is correct - writes don't update this. JRA. */
158 /* Found by Samba4 tests. */
159 #if 0
160 fsp->position_information = fsp->pos;
161 #endif
164 return ret;
167 /****************************************************************************
168 write to a file
169 ****************************************************************************/
171 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
173 write_cache *wcp = fsp->wcp;
174 ssize_t total_written = 0;
175 int write_path = -1;
177 if (fsp->print_file) {
178 int snum;
179 uint32 jobid;
181 if (!rap_to_pjobid(fsp->rap_print_jobid, &snum, &jobid)) {
182 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
183 (unsigned int)fsp->rap_print_jobid ));
184 errno = EBADF;
185 return -1;
188 return print_job_write(SNUM(fsp->conn), jobid, data, n);
191 if (!fsp->can_write) {
192 errno = EPERM;
193 return(0);
196 if (!fsp->modified) {
197 SMB_STRUCT_STAT st;
198 fsp->modified = True;
200 if (SMB_VFS_FSTAT(fsp,fsp->fd,&st) == 0) {
201 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
202 fsp->size = (SMB_BIG_UINT)st.st_size;
203 if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode))
204 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
207 * If this is the first write and we have an exclusive oplock then setup
208 * the write cache.
211 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
212 setup_write_cache(fsp, st.st_size);
213 wcp = fsp->wcp;
218 #ifdef WITH_PROFILE
219 DO_PROFILE_INC(writecache_total_writes);
220 if (!fsp->oplock_type) {
221 DO_PROFILE_INC(writecache_non_oplock_writes);
223 #endif
226 * If this file is level II oplocked then we need
227 * to grab the shared memory lock and inform all
228 * other files with a level II lock that they need
229 * to flush their read caches. We keep the lock over
230 * the shared memory area whilst doing this.
233 release_level_2_oplocks_on_change(fsp);
235 #ifdef WITH_PROFILE
236 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
237 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
238 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
239 profile_p->writecache_init_writes,
240 profile_p->writecache_abutted_writes,
241 profile_p->writecache_total_writes,
242 profile_p->writecache_non_oplock_writes,
243 profile_p->writecache_allocated_write_caches,
244 profile_p->writecache_num_write_caches,
245 profile_p->writecache_direct_writes,
246 profile_p->writecache_num_perfect_writes,
247 profile_p->writecache_read_hits ));
249 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
250 profile_p->writecache_flushed_writes[SEEK_FLUSH],
251 profile_p->writecache_flushed_writes[READ_FLUSH],
252 profile_p->writecache_flushed_writes[WRITE_FLUSH],
253 profile_p->writecache_flushed_writes[READRAW_FLUSH],
254 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
255 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
256 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
258 #endif
260 if(!wcp) {
261 DO_PROFILE_INC(writecache_direct_writes);
262 total_written = real_write_file(fsp, data, pos, n);
263 if ((total_written != -1) && (pos + total_written > (SMB_OFF_T)fsp->size))
264 fsp->size = (SMB_BIG_UINT)(pos + total_written);
265 return total_written;
268 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
269 fsp->fsp_name, fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
271 fsp->pos = pos + n;
274 * If we have active cache and it isn't contiguous then we flush.
275 * NOTE: There is a small problem with running out of disk ....
278 if (wcp->data_size) {
280 BOOL cache_flush_needed = False;
282 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
284 /* ASCII art.... JRA.
286 +--------------+-----
287 | Cached data | Rest of allocated cache buffer....
288 +--------------+-----
290 +-------------------+
291 | Data to write |
292 +-------------------+
297 * Start of write overlaps or abutts the existing data.
300 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
302 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
305 * Update the current buffer size with the new data.
308 if(pos + data_used > wcp->offset + wcp->data_size)
309 wcp->data_size = pos + data_used - wcp->offset;
312 * Update the file size if changed.
315 if (wcp->offset + wcp->data_size > wcp->file_size) {
316 wcp->file_size = wcp->offset + wcp->data_size;
317 fsp->size = (SMB_BIG_UINT)wcp->file_size;
321 * If we used all the data then
322 * return here.
325 if(n == data_used)
326 return n;
327 else
328 cache_flush_needed = True;
331 * Move the start of data forward by the amount used,
332 * cut down the amount left by the same amount.
335 data += data_used;
336 pos += data_used;
337 n -= data_used;
339 DO_PROFILE_INC(writecache_abutted_writes);
340 total_written = data_used;
342 write_path = 1;
344 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
345 (pos + n <= wcp->offset + wcp->alloc_size)) {
347 /* ASCII art.... JRA.
349 +---------------+
350 | Cache buffer |
351 +---------------+
353 +-------------------+
354 | Data to write |
355 +-------------------+
360 * End of write overlaps the existing data.
363 size_t data_used = pos + n - wcp->offset;
365 memcpy(wcp->data, data + n - data_used, data_used);
368 * Update the current buffer size with the new data.
371 if(pos + n > wcp->offset + wcp->data_size)
372 wcp->data_size = pos + n - wcp->offset;
375 * Update the file size if changed.
378 if (wcp->offset + wcp->data_size > wcp->file_size) {
379 wcp->file_size = wcp->offset + wcp->data_size;
380 fsp->size = (SMB_BIG_UINT)wcp->file_size;
384 * We don't need to move the start of data, but we
385 * cut down the amount left by the amount used.
388 n -= data_used;
391 * We cannot have used all the data here.
394 cache_flush_needed = True;
396 DO_PROFILE_INC(writecache_abutted_writes);
397 total_written = data_used;
399 write_path = 2;
401 } else if ( (pos >= wcp->file_size) &&
402 (wcp->offset + wcp->data_size == wcp->file_size) &&
403 (pos > wcp->offset + wcp->data_size) &&
404 (pos < wcp->offset + wcp->alloc_size) ) {
406 /* ASCII art.... JRA.
408 End of file ---->|
410 +---------------+---------------+
411 | Cached data | Cache buffer |
412 +---------------+---------------+
414 +-------------------+
415 | Data to write |
416 +-------------------+
421 * Non-contiguous write part of which fits within
422 * the cache buffer and is extending the file
423 * and the cache contents reflect the current
424 * data up to the current end of the file.
427 size_t data_used;
429 if(pos + n <= wcp->offset + wcp->alloc_size)
430 data_used = n;
431 else
432 data_used = wcp->offset + wcp->alloc_size - pos;
435 * Fill in the non-continuous area with zeros.
438 memset(wcp->data + wcp->data_size, '\0',
439 pos - (wcp->offset + wcp->data_size) );
441 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
444 * Update the current buffer size with the new data.
447 if(pos + data_used > wcp->offset + wcp->data_size)
448 wcp->data_size = pos + data_used - wcp->offset;
451 * Update the file size if changed.
454 if (wcp->offset + wcp->data_size > wcp->file_size) {
455 wcp->file_size = wcp->offset + wcp->data_size;
456 fsp->size = (SMB_BIG_UINT)wcp->file_size;
460 * If we used all the data then
461 * return here.
464 if(n == data_used)
465 return n;
466 else
467 cache_flush_needed = True;
470 * Move the start of data forward by the amount used,
471 * cut down the amount left by the same amount.
474 data += data_used;
475 pos += data_used;
476 n -= data_used;
478 DO_PROFILE_INC(writecache_abutted_writes);
479 total_written = data_used;
481 write_path = 3;
483 } else {
485 /* ASCII art..... JRA.
487 Case 1).
489 +---------------+---------------+
490 | Cached data | Cache buffer |
491 +---------------+---------------+
493 +-------------------+
494 | Data to write |
495 +-------------------+
497 Case 2).
499 +---------------+---------------+
500 | Cached data | Cache buffer |
501 +---------------+---------------+
503 +-------------------+
504 | Data to write |
505 +-------------------+
507 Case 3).
509 +---------------+---------------+
510 | Cached data | Cache buffer |
511 +---------------+---------------+
513 +-----------------------------------------------------+
514 | Data to write |
515 +-----------------------------------------------------+
520 * Write is bigger than buffer, or there is no overlap on the
521 * low or high ends.
524 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
525 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
528 * Update the file size if needed.
531 if(pos + n > wcp->file_size) {
532 wcp->file_size = pos + n;
533 fsp->size = (SMB_BIG_UINT)wcp->file_size;
537 * If write would fit in the cache, and is larger than
538 * the data already in the cache, flush the cache and
539 * preferentially copy the data new data into it. Otherwise
540 * just write the data directly.
543 if ( n <= wcp->alloc_size && n > wcp->data_size) {
544 cache_flush_needed = True;
545 } else {
546 ssize_t ret = real_write_file(fsp, data, pos, n);
549 * If the write overlaps the entire cache, then
550 * discard the current contents of the cache.
551 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
554 if ((pos <= wcp->offset) &&
555 (pos + n >= wcp->offset + wcp->data_size) ) {
556 DEBUG(9,("write_file: discarding overwritten write \
557 cache: fd = %d, off=%.0f, size=%u\n", fsp->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
558 wcp->data_size = 0;
561 DO_PROFILE_INC(writecache_direct_writes);
562 if (ret == -1)
563 return ret;
565 if (pos + ret > wcp->file_size) {
566 wcp->file_size = pos + ret;
567 fsp->size = (SMB_BIG_UINT)wcp->file_size;
570 return ret;
573 write_path = 4;
577 if(wcp->data_size > wcp->file_size) {
578 wcp->file_size = wcp->data_size;
579 fsp->size = (SMB_BIG_UINT)wcp->file_size;
582 if (cache_flush_needed) {
583 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
584 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
585 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
586 (double)wcp->offset, (unsigned int)wcp->data_size ));
588 flush_write_cache(fsp, WRITE_FLUSH);
593 * If the write request is bigger than the cache
594 * size, write it all out.
597 if (n > wcp->alloc_size ) {
598 ssize_t ret = real_write_file(fsp, data, pos, n);
599 if (ret == -1)
600 return -1;
602 if (pos + ret > wcp->file_size) {
603 wcp->file_size = pos + n;
604 fsp->size = (SMB_BIG_UINT)wcp->file_size;
607 DO_PROFILE_INC(writecache_direct_writes);
608 return total_written + n;
612 * If there's any data left, cache it.
615 if (n) {
616 #ifdef WITH_PROFILE
617 if (wcp->data_size) {
618 DO_PROFILE_INC(writecache_abutted_writes);
619 } else {
620 DO_PROFILE_INC(writecache_init_writes);
622 #endif
623 memcpy(wcp->data+wcp->data_size, data, n);
624 if (wcp->data_size == 0) {
625 wcp->offset = pos;
626 DO_PROFILE_INC(writecache_num_write_caches);
628 wcp->data_size += n;
631 * Update the file size if changed.
634 if (wcp->offset + wcp->data_size > wcp->file_size) {
635 wcp->file_size = wcp->offset + wcp->data_size;
636 fsp->size = (SMB_BIG_UINT)wcp->file_size;
638 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
639 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
641 total_written += n;
642 return total_written; /* .... that's a write :) */
645 return total_written;
648 /****************************************************************************
649 Delete the write cache structure.
650 ****************************************************************************/
652 void delete_write_cache(files_struct *fsp)
654 write_cache *wcp;
656 if(!fsp)
657 return;
659 if(!(wcp = fsp->wcp))
660 return;
662 DO_PROFILE_DEC(writecache_allocated_write_caches);
663 allocated_write_caches--;
665 SMB_ASSERT(wcp->data_size == 0);
667 SAFE_FREE(wcp->data);
668 SAFE_FREE(fsp->wcp);
670 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
673 /****************************************************************************
674 Setup the write cache structure.
675 ****************************************************************************/
677 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
679 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
680 write_cache *wcp;
682 if (allocated_write_caches >= MAX_WRITE_CACHES)
683 return False;
685 if(alloc_size == 0 || fsp->wcp)
686 return False;
688 if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
689 DEBUG(0,("setup_write_cache: malloc fail.\n"));
690 return False;
693 wcp->file_size = file_size;
694 wcp->offset = 0;
695 wcp->alloc_size = alloc_size;
696 wcp->data_size = 0;
697 if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
698 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
699 (unsigned int)wcp->alloc_size ));
700 SAFE_FREE(wcp);
701 return False;
704 memset(wcp->data, '\0', wcp->alloc_size );
706 fsp->wcp = wcp;
707 DO_PROFILE_INC(writecache_allocated_write_caches);
708 allocated_write_caches++;
710 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
711 fsp->fsp_name, (unsigned long)wcp->alloc_size ));
713 return True;
716 /****************************************************************************
717 Cope with a size change.
718 ****************************************************************************/
720 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
722 fsp->size = (SMB_BIG_UINT)file_size;
723 if(fsp->wcp) {
724 /* The cache *must* have been flushed before we do this. */
725 if (fsp->wcp->data_size != 0) {
726 pstring msg;
727 slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
728 on file %s with write cache size = %lu\n", fsp->fsp_name, (unsigned long)fsp->wcp->data_size );
729 smb_panic(msg);
731 fsp->wcp->file_size = file_size;
735 /*******************************************************************
736 Flush a write cache struct to disk.
737 ********************************************************************/
739 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
741 write_cache *wcp = fsp->wcp;
742 size_t data_size;
743 ssize_t ret;
745 if(!wcp || !wcp->data_size)
746 return 0;
748 data_size = wcp->data_size;
749 wcp->data_size = 0;
751 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
753 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
754 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
756 #ifdef WITH_PROFILE
757 if(data_size == wcp->alloc_size)
758 DO_PROFILE_INC(writecache_num_perfect_writes);
759 #endif
761 ret = real_write_file(fsp, wcp->data, wcp->offset, data_size);
764 * Ensure file size if kept up to date if write extends file.
767 if ((ret != -1) && (wcp->offset + ret > wcp->file_size))
768 wcp->file_size = wcp->offset + ret;
770 return ret;
773 /*******************************************************************
774 sync a file
775 ********************************************************************/
777 void sync_file(connection_struct *conn, files_struct *fsp)
779 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
780 flush_write_cache(fsp, SYNC_FLUSH);
781 SMB_VFS_FSYNC(fsp,fsp->fd);
786 /************************************************************
787 Perform a stat whether a valid fd or not.
788 ************************************************************/
790 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
792 if (fsp->fd == -1)
793 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name, pst);
794 else
795 return SMB_VFS_FSTAT(fsp,fsp->fd, pst);