loadparm: fix spacing in parm_table (training spaces and empty lines).
[Samba.git] / source / smbd / fileio.c
blob8cea4989f5b4dd16f5d3ad26208e951094a1e908
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 static bool setup_write_cache(files_struct *, SMB_OFF_T);
26 /****************************************************************************
27 Read from write cache if we can.
28 ****************************************************************************/
30 static bool read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
32 write_cache *wcp = fsp->wcp;
34 if(!wcp) {
35 return False;
38 if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
39 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;
63 * Serve from write cache if we can.
66 if(read_from_write_cache(fsp, data, pos, n)) {
67 fsp->fh->pos = pos + n;
68 fsp->fh->position_information = fsp->fh->pos;
69 return n;
72 flush_write_cache(fsp, READ_FLUSH);
74 fsp->fh->pos = pos;
76 if (n > 0) {
77 #ifdef DMF_FIX
78 int numretries = 3;
79 tryagain:
80 readret = SMB_VFS_PREAD(fsp,data,n,pos);
82 if (readret == -1) {
83 if ((errno == EAGAIN) && numretries) {
84 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
85 (void)sleep(10);
86 --numretries;
87 goto tryagain;
89 return -1;
91 #else /* NO DMF fix. */
92 readret = SMB_VFS_PREAD(fsp,data,n,pos);
94 if (readret == -1) {
95 return -1;
97 #endif
98 if (readret > 0) {
99 ret += readret;
103 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
104 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
106 fsp->fh->pos += ret;
107 fsp->fh->position_information = fsp->fh->pos;
109 return(ret);
112 /* how many write cache buffers have been allocated */
113 static unsigned int allocated_write_caches;
115 /****************************************************************************
116 *Really* write to a file.
117 ****************************************************************************/
119 static ssize_t real_write_file(struct smb_request *req,
120 files_struct *fsp,
121 const char *data,
122 SMB_OFF_T pos,
123 size_t n)
125 ssize_t ret;
127 if (pos == -1) {
128 ret = vfs_write_data(req, fsp, data, n);
129 } else {
130 fsp->fh->pos = pos;
131 if (pos && lp_strict_allocate(SNUM(fsp->conn))) {
132 if (vfs_fill_sparse(fsp, pos) == -1) {
133 return -1;
136 ret = vfs_pwrite_data(req, fsp, data, n, pos);
139 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
140 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
142 if (ret != -1) {
143 fsp->fh->pos += ret;
146 * It turns out that setting the last write time from a Windows
147 * client stops any subsequent writes from updating the write time.
148 * Doing this after the write gives a race condition here where
149 * a stat may see the changed write time before we reset it here,
150 * but it's cheaper than having to store the write time in shared
151 * memory and look it up using dev/inode across all running smbd's.
152 * The 99% solution will hopefully be good enough in this case. JRA.
155 if (!null_timespec(fsp->pending_modtime)) {
156 set_filetime(fsp->conn, fsp->fsp_name,
157 fsp->pending_modtime);
159 /* If we didn't get the "set modtime" call ourselves, we must
160 store the last write time to restore on close. JRA. */
161 if (!fsp->pending_modtime_owner) {
162 fsp->last_write_time = timespec_current();
166 /* Yes - this is correct - writes don't update this. JRA. */
167 /* Found by Samba4 tests. */
168 #if 0
169 fsp->position_information = fsp->pos;
170 #endif
173 return ret;
176 /****************************************************************************
177 File size cache change.
178 Updates size on disk but doesn't flush the cache.
179 ****************************************************************************/
181 static int wcp_file_size_change(files_struct *fsp)
183 int ret;
184 write_cache *wcp = fsp->wcp;
186 wcp->file_size = wcp->offset + wcp->data_size;
187 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
188 if (ret == -1) {
189 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f error %s\n",
190 fsp->fsp_name, (double)wcp->file_size, strerror(errno) ));
192 return ret;
195 /****************************************************************************
196 Write to a file.
197 ****************************************************************************/
199 ssize_t write_file(struct smb_request *req,
200 files_struct *fsp,
201 const char *data,
202 SMB_OFF_T pos,
203 size_t n)
205 write_cache *wcp = fsp->wcp;
206 ssize_t total_written = 0;
207 int write_path = -1;
209 if (fsp->print_file) {
210 fstring sharename;
211 uint32 jobid;
213 if (!rap_to_pjobid(fsp->rap_print_jobid, sharename, &jobid)) {
214 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
215 (unsigned int)fsp->rap_print_jobid ));
216 errno = EBADF;
217 return -1;
220 return print_job_write(SNUM(fsp->conn), jobid, data, pos, n);
223 if (!fsp->can_write) {
224 errno = EPERM;
225 return -1;
228 if (!fsp->modified) {
229 SMB_STRUCT_STAT st;
230 fsp->modified = True;
232 if (SMB_VFS_FSTAT(fsp, &st) == 0) {
233 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
234 if ((lp_store_dos_attributes(SNUM(fsp->conn)) ||
235 MAP_ARCHIVE(fsp->conn)) &&
236 !IS_DOS_ARCHIVE(dosmode)) {
237 file_set_dosmode(fsp->conn,fsp->fsp_name,
238 dosmode | aARCH,&st,
239 NULL,
240 false);
244 * If this is the first write and we have an exclusive oplock then setup
245 * the write cache.
248 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
249 setup_write_cache(fsp, st.st_size);
250 wcp = fsp->wcp;
255 #ifdef WITH_PROFILE
256 DO_PROFILE_INC(writecache_total_writes);
257 if (!fsp->oplock_type) {
258 DO_PROFILE_INC(writecache_non_oplock_writes);
260 #endif
263 * If this file is level II oplocked then we need
264 * to grab the shared memory lock and inform all
265 * other files with a level II lock that they need
266 * to flush their read caches. We keep the lock over
267 * the shared memory area whilst doing this.
270 release_level_2_oplocks_on_change(fsp);
272 #ifdef WITH_PROFILE
273 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
274 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
275 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
276 profile_p->writecache_init_writes,
277 profile_p->writecache_abutted_writes,
278 profile_p->writecache_total_writes,
279 profile_p->writecache_non_oplock_writes,
280 profile_p->writecache_allocated_write_caches,
281 profile_p->writecache_num_write_caches,
282 profile_p->writecache_direct_writes,
283 profile_p->writecache_num_perfect_writes,
284 profile_p->writecache_read_hits ));
286 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
287 profile_p->writecache_flushed_writes[SEEK_FLUSH],
288 profile_p->writecache_flushed_writes[READ_FLUSH],
289 profile_p->writecache_flushed_writes[WRITE_FLUSH],
290 profile_p->writecache_flushed_writes[READRAW_FLUSH],
291 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
292 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
293 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
295 #endif
297 if (wcp && req->unread_bytes) {
298 /* If we're using receivefile don't
299 * deal with a write cache.
301 flush_write_cache(fsp, WRITE_FLUSH);
302 delete_write_cache(fsp);
303 wcp = NULL;
306 if(!wcp) {
307 DO_PROFILE_INC(writecache_direct_writes);
308 total_written = real_write_file(req, fsp, data, pos, n);
309 return total_written;
312 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
313 fsp->fsp_name, fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
315 fsp->fh->pos = pos + n;
318 * If we have active cache and it isn't contiguous then we flush.
319 * NOTE: There is a small problem with running out of disk ....
322 if (wcp->data_size) {
323 bool cache_flush_needed = False;
325 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
327 /* ASCII art.... JRA.
329 +--------------+-----
330 | Cached data | Rest of allocated cache buffer....
331 +--------------+-----
333 +-------------------+
334 | Data to write |
335 +-------------------+
340 * Start of write overlaps or abutts the existing data.
343 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
345 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
348 * Update the current buffer size with the new data.
351 if(pos + data_used > wcp->offset + wcp->data_size) {
352 wcp->data_size = pos + data_used - wcp->offset;
356 * Update the file size if changed.
359 if (wcp->offset + wcp->data_size > wcp->file_size) {
360 if (wcp_file_size_change(fsp) == -1) {
361 return -1;
366 * If we used all the data then
367 * return here.
370 if(n == data_used) {
371 return n;
372 } else {
373 cache_flush_needed = True;
376 * Move the start of data forward by the amount used,
377 * cut down the amount left by the same amount.
380 data += data_used;
381 pos += data_used;
382 n -= data_used;
384 DO_PROFILE_INC(writecache_abutted_writes);
385 total_written = data_used;
387 write_path = 1;
389 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
390 (pos + n <= wcp->offset + wcp->alloc_size)) {
392 /* ASCII art.... JRA.
394 +---------------+
395 | Cache buffer |
396 +---------------+
398 +-------------------+
399 | Data to write |
400 +-------------------+
405 * End of write overlaps the existing data.
408 size_t data_used = pos + n - wcp->offset;
410 memcpy(wcp->data, data + n - data_used, data_used);
413 * Update the current buffer size with the new data.
416 if(pos + n > wcp->offset + wcp->data_size) {
417 wcp->data_size = pos + n - wcp->offset;
421 * Update the file size if changed.
424 if (wcp->offset + wcp->data_size > wcp->file_size) {
425 if (wcp_file_size_change(fsp) == -1) {
426 return -1;
431 * We don't need to move the start of data, but we
432 * cut down the amount left by the amount used.
435 n -= data_used;
438 * We cannot have used all the data here.
441 cache_flush_needed = True;
443 DO_PROFILE_INC(writecache_abutted_writes);
444 total_written = data_used;
446 write_path = 2;
448 } else if ( (pos >= wcp->file_size) &&
449 (wcp->offset + wcp->data_size == wcp->file_size) &&
450 (pos > wcp->offset + wcp->data_size) &&
451 (pos < wcp->offset + wcp->alloc_size) ) {
453 /* ASCII art.... JRA.
455 End of file ---->|
457 +---------------+---------------+
458 | Cached data | Cache buffer |
459 +---------------+---------------+
461 +-------------------+
462 | Data to write |
463 +-------------------+
468 * Non-contiguous write part of which fits within
469 * the cache buffer and is extending the file
470 * and the cache contents reflect the current
471 * data up to the current end of the file.
474 size_t data_used;
476 if(pos + n <= wcp->offset + wcp->alloc_size) {
477 data_used = n;
478 } else {
479 data_used = wcp->offset + wcp->alloc_size - pos;
483 * Fill in the non-continuous area with zeros.
486 memset(wcp->data + wcp->data_size, '\0',
487 pos - (wcp->offset + wcp->data_size) );
489 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
492 * Update the current buffer size with the new data.
495 if(pos + data_used > wcp->offset + wcp->data_size) {
496 wcp->data_size = pos + data_used - wcp->offset;
500 * Update the file size if changed.
503 if (wcp->offset + wcp->data_size > wcp->file_size) {
504 if (wcp_file_size_change(fsp) == -1) {
505 return -1;
510 * If we used all the data then
511 * return here.
514 if(n == data_used) {
515 return n;
516 } else {
517 cache_flush_needed = True;
521 * Move the start of data forward by the amount used,
522 * cut down the amount left by the same amount.
525 data += data_used;
526 pos += data_used;
527 n -= data_used;
529 DO_PROFILE_INC(writecache_abutted_writes);
530 total_written = data_used;
532 write_path = 3;
534 } else if ( (pos >= wcp->file_size) &&
535 (n == 1) &&
536 (wcp->file_size == wcp->offset + wcp->data_size) &&
537 (pos < wcp->file_size + wcp->alloc_size)) {
541 End of file ---->|
543 +---------------+---------------+
544 | Cached data | Cache buffer |
545 +---------------+---------------+
547 |<------- allocated size ---------------->|
549 +--------+
550 | 1 Byte |
551 +--------+
553 MS-Office seems to do this a lot to determine if there's enough
554 space on the filesystem to write a new file.
556 Change to :
558 End of file ---->|
559 +-----------------------+--------+
560 | Zeroed Cached data | 1 Byte |
561 +-----------------------+--------+
564 flush_write_cache(fsp, WRITE_FLUSH);
565 wcp->offset = wcp->file_size;
566 wcp->data_size = pos - wcp->file_size + 1;
567 memset(wcp->data, '\0', wcp->data_size);
568 memcpy(wcp->data + wcp->data_size-1, data, 1);
571 * Update the file size if changed.
574 if (wcp->offset + wcp->data_size > wcp->file_size) {
575 if (wcp_file_size_change(fsp) == -1) {
576 return -1;
580 return n;
582 } else {
584 /* ASCII art..... JRA.
586 Case 1).
588 +---------------+---------------+
589 | Cached data | Cache buffer |
590 +---------------+---------------+
592 +-------------------+
593 | Data to write |
594 +-------------------+
596 Case 2).
598 +---------------+---------------+
599 | Cached data | Cache buffer |
600 +---------------+---------------+
602 +-------------------+
603 | Data to write |
604 +-------------------+
606 Case 3).
608 +---------------+---------------+
609 | Cached data | Cache buffer |
610 +---------------+---------------+
612 +-----------------------------------------------------+
613 | Data to write |
614 +-----------------------------------------------------+
619 * Write is bigger than buffer, or there is no overlap on the
620 * low or high ends.
623 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
624 len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
627 * If write would fit in the cache, and is larger than
628 * the data already in the cache, flush the cache and
629 * preferentially copy the data new data into it. Otherwise
630 * just write the data directly.
633 if ( n <= wcp->alloc_size && n > wcp->data_size) {
634 cache_flush_needed = True;
635 } else {
636 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
639 * If the write overlaps the entire cache, then
640 * discard the current contents of the cache.
641 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
644 if ((pos <= wcp->offset) &&
645 (pos + n >= wcp->offset + wcp->data_size) ) {
646 DEBUG(9,("write_file: discarding overwritten write \
647 cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
648 wcp->data_size = 0;
651 DO_PROFILE_INC(writecache_direct_writes);
652 if (ret == -1) {
653 return ret;
656 if (pos + ret > wcp->file_size) {
657 wcp->file_size = pos + ret;
660 return ret;
663 write_path = 4;
667 if (cache_flush_needed) {
668 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
669 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
670 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
671 (double)wcp->offset, (unsigned int)wcp->data_size ));
673 flush_write_cache(fsp, WRITE_FLUSH);
678 * If the write request is bigger than the cache
679 * size, write it all out.
682 if (n > wcp->alloc_size ) {
683 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
684 if (ret == -1) {
685 return -1;
688 if (pos + ret > wcp->file_size) {
689 wcp->file_size = pos + n;
692 DO_PROFILE_INC(writecache_direct_writes);
693 return total_written + n;
697 * If there's any data left, cache it.
700 if (n) {
701 #ifdef WITH_PROFILE
702 if (wcp->data_size) {
703 DO_PROFILE_INC(writecache_abutted_writes);
704 } else {
705 DO_PROFILE_INC(writecache_init_writes);
707 #endif
708 memcpy(wcp->data+wcp->data_size, data, n);
709 if (wcp->data_size == 0) {
710 wcp->offset = pos;
711 DO_PROFILE_INC(writecache_num_write_caches);
713 wcp->data_size += n;
716 * Update the file size if changed.
719 if (wcp->offset + wcp->data_size > wcp->file_size) {
720 if (wcp_file_size_change(fsp) == -1) {
721 return -1;
724 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
725 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
727 total_written += n;
728 return total_written; /* .... that's a write :) */
731 return total_written;
734 /****************************************************************************
735 Delete the write cache structure.
736 ****************************************************************************/
738 void delete_write_cache(files_struct *fsp)
740 write_cache *wcp;
742 if(!fsp) {
743 return;
746 if(!(wcp = fsp->wcp)) {
747 return;
750 DO_PROFILE_DEC(writecache_allocated_write_caches);
751 allocated_write_caches--;
753 SMB_ASSERT(wcp->data_size == 0);
755 SAFE_FREE(wcp->data);
756 SAFE_FREE(fsp->wcp);
758 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
761 /****************************************************************************
762 Setup the write cache structure.
763 ****************************************************************************/
765 static bool setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
767 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
768 write_cache *wcp;
770 if (allocated_write_caches >= MAX_WRITE_CACHES) {
771 return False;
774 if(alloc_size == 0 || fsp->wcp) {
775 return False;
778 if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
779 DEBUG(0,("setup_write_cache: malloc fail.\n"));
780 return False;
783 wcp->file_size = file_size;
784 wcp->offset = 0;
785 wcp->alloc_size = alloc_size;
786 wcp->data_size = 0;
787 if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
788 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
789 (unsigned int)wcp->alloc_size ));
790 SAFE_FREE(wcp);
791 return False;
794 memset(wcp->data, '\0', wcp->alloc_size );
796 fsp->wcp = wcp;
797 DO_PROFILE_INC(writecache_allocated_write_caches);
798 allocated_write_caches++;
800 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
801 fsp->fsp_name, (unsigned long)wcp->alloc_size ));
803 return True;
806 /****************************************************************************
807 Cope with a size change.
808 ****************************************************************************/
810 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
812 if(fsp->wcp) {
813 /* The cache *must* have been flushed before we do this. */
814 if (fsp->wcp->data_size != 0) {
815 char *msg;
816 asprintf(&msg, "set_filelen_write_cache: size change "
817 "on file %s with write cache size = %lu\n",
818 fsp->fsp_name,
819 (unsigned long)fsp->wcp->data_size);
820 smb_panic(msg);
822 fsp->wcp->file_size = file_size;
826 /*******************************************************************
827 Flush a write cache struct to disk.
828 ********************************************************************/
830 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
832 write_cache *wcp = fsp->wcp;
833 size_t data_size;
834 ssize_t ret;
836 if(!wcp || !wcp->data_size) {
837 return 0;
840 data_size = wcp->data_size;
841 wcp->data_size = 0;
843 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
845 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
846 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
848 #ifdef WITH_PROFILE
849 if(data_size == wcp->alloc_size) {
850 DO_PROFILE_INC(writecache_num_perfect_writes);
852 #endif
854 ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
857 * Ensure file size if kept up to date if write extends file.
860 if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
861 wcp->file_size = wcp->offset + ret;
864 return ret;
867 /*******************************************************************
868 sync a file
869 ********************************************************************/
871 NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
873 if (fsp->fh->fd == -1)
874 return NT_STATUS_INVALID_HANDLE;
876 if (lp_strict_sync(SNUM(conn)) &&
877 (lp_syncalways(SNUM(conn)) || write_through)) {
878 int ret = flush_write_cache(fsp, SYNC_FLUSH);
879 if (ret == -1) {
880 return map_nt_error_from_unix(errno);
882 ret = SMB_VFS_FSYNC(fsp);
883 if (ret == -1) {
884 return map_nt_error_from_unix(errno);
887 return NT_STATUS_OK;
890 /************************************************************
891 Perform a stat whether a valid fd or not.
892 ************************************************************/
894 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
896 if (fsp->fh->fd == -1) {
897 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name, pst);
898 } else {
899 return SMB_VFS_FSTAT(fsp, pst);