2 Unix SMB/Netbios implementation.
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.
25 static BOOL
setup_write_cache(files_struct
*, SMB_OFF_T
);
27 /****************************************************************************
28 Read from write cache if we can.
29 ****************************************************************************/
31 static BOOL
read_from_write_cache(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
33 write_cache
*wcp
= fsp
->wcp
;
39 if( n
> wcp
->data_size
|| pos
< wcp
->offset
|| pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
43 memcpy(data
, wcp
->data
+ (pos
- wcp
->offset
), n
);
45 DO_PROFILE_INC(writecache_read_hits
);
50 /****************************************************************************
52 ****************************************************************************/
54 ssize_t
read_file(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
56 ssize_t ret
=0,readret
;
58 /* you can't read from print files */
59 if (fsp
->print_file
) {
64 * Serve from write cache if we can.
67 if(read_from_write_cache(fsp
, data
, pos
, n
)) {
68 fsp
->fh
->pos
= pos
+ n
;
69 fsp
->fh
->position_information
= fsp
->fh
->pos
;
73 flush_write_cache(fsp
, READ_FLUSH
);
81 readret
= SMB_VFS_PREAD(fsp
,fsp
->fh
->fd
,data
,n
,pos
);
84 if ((errno
== EAGAIN
) && numretries
) {
85 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
92 #else /* NO DMF fix. */
93 readret
= SMB_VFS_PREAD(fsp
,fsp
->fh
->fd
,data
,n
,pos
);
104 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
105 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
108 fsp
->fh
->position_information
= fsp
->fh
->pos
;
113 /* how many write cache buffers have been allocated */
114 static unsigned int allocated_write_caches
;
116 /****************************************************************************
117 *Really* write to a file.
118 ****************************************************************************/
120 static ssize_t
real_write_file(files_struct
*fsp
,const char *data
, SMB_OFF_T pos
, size_t n
)
125 ret
= vfs_write_data(fsp
, data
, n
);
128 if (pos
&& lp_strict_allocate(SNUM(fsp
->conn
))) {
129 if (vfs_fill_sparse(fsp
, pos
) == -1) {
133 ret
= vfs_pwrite_data(fsp
, data
, n
, pos
);
136 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
137 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
143 * It turns out that setting the last write time from a Windows
144 * client stops any subsequent writes from updating the write time.
145 * Doing this after the write gives a race condition here where
146 * a stat may see the changed write time before we reset it here,
147 * but it's cheaper than having to store the write time in shared
148 * memory and look it up using dev/inode across all running smbd's.
149 * The 99% solution will hopefully be good enough in this case. JRA.
152 if (!null_timespec(fsp
->pending_modtime
)) {
153 set_filetime(fsp
->conn
, fsp
->fsp_name
, fsp
->pending_modtime
);
155 /* If we didn't get the "set modtime" call ourselves, we must
156 store the last write time to restore on close. JRA. */
157 if (!fsp
->pending_modtime_owner
) {
158 fsp
->last_write_time
= timespec_current();
162 /* Yes - this is correct - writes don't update this. JRA. */
163 /* Found by Samba4 tests. */
165 fsp
->position_information
= fsp
->pos
;
172 /****************************************************************************
173 File size cache change.
174 Updates size on disk but doesn't flush the cache.
175 ****************************************************************************/
177 static int wcp_file_size_change(files_struct
*fsp
)
180 write_cache
*wcp
= fsp
->wcp
;
182 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
183 ret
= SMB_VFS_FTRUNCATE(fsp
, fsp
->fh
->fd
, wcp
->file_size
);
185 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f error %s\n",
186 fsp
->fsp_name
, (double)wcp
->file_size
, strerror(errno
) ));
191 /****************************************************************************
193 ****************************************************************************/
195 ssize_t
write_file(files_struct
*fsp
, const char *data
, SMB_OFF_T pos
, size_t n
)
197 write_cache
*wcp
= fsp
->wcp
;
198 ssize_t total_written
= 0;
201 if (fsp
->print_file
) {
205 if (!rap_to_pjobid(fsp
->rap_print_jobid
, sharename
, &jobid
)) {
206 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
207 (unsigned int)fsp
->rap_print_jobid
));
212 return print_job_write(SNUM(fsp
->conn
), jobid
, data
, pos
, n
);
215 if (!fsp
->can_write
) {
220 if (!fsp
->modified
) {
222 fsp
->modified
= True
;
224 if (SMB_VFS_FSTAT(fsp
,fsp
->fh
->fd
,&st
) == 0) {
225 int dosmode
= dos_mode(fsp
->conn
,fsp
->fsp_name
,&st
);
226 if ((lp_store_dos_attributes(SNUM(fsp
->conn
)) || MAP_ARCHIVE(fsp
->conn
)) && !IS_DOS_ARCHIVE(dosmode
)) {
227 file_set_dosmode(fsp
->conn
,fsp
->fsp_name
,dosmode
| aARCH
,&st
, False
);
231 * If this is the first write and we have an exclusive oplock then setup
235 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
236 setup_write_cache(fsp
, st
.st_size
);
243 DO_PROFILE_INC(writecache_total_writes
);
244 if (!fsp
->oplock_type
) {
245 DO_PROFILE_INC(writecache_non_oplock_writes
);
250 * If this file is level II oplocked then we need
251 * to grab the shared memory lock and inform all
252 * other files with a level II lock that they need
253 * to flush their read caches. We keep the lock over
254 * the shared memory area whilst doing this.
257 release_level_2_oplocks_on_change(fsp
);
260 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
261 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
262 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
263 profile_p
->writecache_init_writes
,
264 profile_p
->writecache_abutted_writes
,
265 profile_p
->writecache_total_writes
,
266 profile_p
->writecache_non_oplock_writes
,
267 profile_p
->writecache_allocated_write_caches
,
268 profile_p
->writecache_num_write_caches
,
269 profile_p
->writecache_direct_writes
,
270 profile_p
->writecache_num_perfect_writes
,
271 profile_p
->writecache_read_hits
));
273 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
274 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
275 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
276 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
277 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
278 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
279 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
280 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
285 DO_PROFILE_INC(writecache_direct_writes
);
286 total_written
= real_write_file(fsp
, data
, pos
, n
);
287 return total_written
;
290 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
291 fsp
->fsp_name
, fsp
->fh
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
293 fsp
->fh
->pos
= pos
+ n
;
296 * If we have active cache and it isn't contiguous then we flush.
297 * NOTE: There is a small problem with running out of disk ....
300 if (wcp
->data_size
) {
301 BOOL cache_flush_needed
= False
;
303 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
305 /* ASCII art.... JRA.
307 +--------------+-----
308 | Cached data | Rest of allocated cache buffer....
309 +--------------+-----
311 +-------------------+
313 +-------------------+
318 * Start of write overlaps or abutts the existing data.
321 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
323 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
326 * Update the current buffer size with the new data.
329 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
330 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
334 * Update the file size if changed.
337 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
338 if (wcp_file_size_change(fsp
) == -1) {
344 * If we used all the data then
351 cache_flush_needed
= True
;
354 * Move the start of data forward by the amount used,
355 * cut down the amount left by the same amount.
362 DO_PROFILE_INC(writecache_abutted_writes
);
363 total_written
= data_used
;
367 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
368 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
370 /* ASCII art.... JRA.
376 +-------------------+
378 +-------------------+
383 * End of write overlaps the existing data.
386 size_t data_used
= pos
+ n
- wcp
->offset
;
388 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
391 * Update the current buffer size with the new data.
394 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
395 wcp
->data_size
= pos
+ n
- wcp
->offset
;
399 * Update the file size if changed.
402 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
403 if (wcp_file_size_change(fsp
) == -1) {
409 * We don't need to move the start of data, but we
410 * cut down the amount left by the amount used.
416 * We cannot have used all the data here.
419 cache_flush_needed
= True
;
421 DO_PROFILE_INC(writecache_abutted_writes
);
422 total_written
= data_used
;
426 } else if ( (pos
>= wcp
->file_size
) &&
427 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
428 (pos
> wcp
->offset
+ wcp
->data_size
) &&
429 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
431 /* ASCII art.... JRA.
435 +---------------+---------------+
436 | Cached data | Cache buffer |
437 +---------------+---------------+
439 +-------------------+
441 +-------------------+
446 * Non-contiguous write part of which fits within
447 * the cache buffer and is extending the file
448 * and the cache contents reflect the current
449 * data up to the current end of the file.
454 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
) {
457 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
461 * Fill in the non-continuous area with zeros.
464 memset(wcp
->data
+ wcp
->data_size
, '\0',
465 pos
- (wcp
->offset
+ wcp
->data_size
) );
467 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
470 * Update the current buffer size with the new data.
473 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
474 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
478 * Update the file size if changed.
481 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
482 if (wcp_file_size_change(fsp
) == -1) {
488 * If we used all the data then
495 cache_flush_needed
= True
;
499 * Move the start of data forward by the amount used,
500 * cut down the amount left by the same amount.
507 DO_PROFILE_INC(writecache_abutted_writes
);
508 total_written
= data_used
;
512 } else if ( (pos
>= wcp
->file_size
) &&
514 (pos
< wcp
->offset
+ 2*wcp
->alloc_size
) &&
515 (wcp
->file_size
== wcp
->offset
+ wcp
->data_size
)) {
526 MS-Office seems to do this a lot to determine if there's enough
527 space on the filesystem to write a new file.
530 SMB_BIG_UINT new_start
= wcp
->offset
+ wcp
->data_size
;
532 flush_write_cache(fsp
, WRITE_FLUSH
);
533 wcp
->offset
= new_start
;
534 wcp
->data_size
= pos
- new_start
+ 1;
535 memset(wcp
->data
, '\0', wcp
->data_size
);
536 memcpy(wcp
->data
+ wcp
->data_size
-1, data
, 1);
539 * Update the file size if changed.
542 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
543 if (wcp_file_size_change(fsp
) == -1) {
552 /* ASCII art..... JRA.
556 +---------------+---------------+
557 | Cached data | Cache buffer |
558 +---------------+---------------+
560 +-------------------+
562 +-------------------+
566 +---------------+---------------+
567 | Cached data | Cache buffer |
568 +---------------+---------------+
570 +-------------------+
572 +-------------------+
576 +---------------+---------------+
577 | Cached data | Cache buffer |
578 +---------------+---------------+
580 +-----------------------------------------------------+
582 +-----------------------------------------------------+
587 * Write is bigger than buffer, or there is no overlap on the
591 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
592 len = %u\n",fsp
->fh
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
595 * If write would fit in the cache, and is larger than
596 * the data already in the cache, flush the cache and
597 * preferentially copy the data new data into it. Otherwise
598 * just write the data directly.
601 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
602 cache_flush_needed
= True
;
604 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
607 * If the write overlaps the entire cache, then
608 * discard the current contents of the cache.
609 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
612 if ((pos
<= wcp
->offset
) &&
613 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
614 DEBUG(9,("write_file: discarding overwritten write \
615 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
619 DO_PROFILE_INC(writecache_direct_writes
);
624 if (pos
+ ret
> wcp
->file_size
) {
625 wcp
->file_size
= pos
+ ret
;
635 if (cache_flush_needed
) {
636 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
637 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
638 write_path
, fsp
->fh
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
639 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
641 flush_write_cache(fsp
, WRITE_FLUSH
);
646 * If the write request is bigger than the cache
647 * size, write it all out.
650 if (n
> wcp
->alloc_size
) {
651 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
656 if (pos
+ ret
> wcp
->file_size
) {
657 wcp
->file_size
= pos
+ n
;
660 DO_PROFILE_INC(writecache_direct_writes
);
661 return total_written
+ n
;
665 * If there's any data left, cache it.
670 if (wcp
->data_size
) {
671 DO_PROFILE_INC(writecache_abutted_writes
);
673 DO_PROFILE_INC(writecache_init_writes
);
676 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
677 if (wcp
->data_size
== 0) {
679 DO_PROFILE_INC(writecache_num_write_caches
);
684 * Update the file size if changed.
687 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
688 if (wcp_file_size_change(fsp
) == -1) {
692 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
693 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
696 return total_written
; /* .... that's a write :) */
699 return total_written
;
702 /****************************************************************************
703 Delete the write cache structure.
704 ****************************************************************************/
706 void delete_write_cache(files_struct
*fsp
)
714 if(!(wcp
= fsp
->wcp
)) {
718 DO_PROFILE_DEC(writecache_allocated_write_caches
);
719 allocated_write_caches
--;
721 SMB_ASSERT(wcp
->data_size
== 0);
723 SAFE_FREE(wcp
->data
);
726 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp
->fsp_name
));
729 /****************************************************************************
730 Setup the write cache structure.
731 ****************************************************************************/
733 static BOOL
setup_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
735 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
738 if (allocated_write_caches
>= MAX_WRITE_CACHES
) {
742 if(alloc_size
== 0 || fsp
->wcp
) {
746 if((wcp
= SMB_MALLOC_P(write_cache
)) == NULL
) {
747 DEBUG(0,("setup_write_cache: malloc fail.\n"));
751 wcp
->file_size
= file_size
;
753 wcp
->alloc_size
= alloc_size
;
755 if((wcp
->data
= (char *)SMB_MALLOC(wcp
->alloc_size
)) == NULL
) {
756 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
757 (unsigned int)wcp
->alloc_size
));
762 memset(wcp
->data
, '\0', wcp
->alloc_size
);
765 DO_PROFILE_INC(writecache_allocated_write_caches
);
766 allocated_write_caches
++;
768 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
769 fsp
->fsp_name
, (unsigned long)wcp
->alloc_size
));
774 /****************************************************************************
775 Cope with a size change.
776 ****************************************************************************/
778 void set_filelen_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
781 /* The cache *must* have been flushed before we do this. */
782 if (fsp
->wcp
->data_size
!= 0) {
784 asprintf(&msg
, "set_filelen_write_cache: size change "
785 "on file %s with write cache size = %lu\n",
787 (unsigned long)fsp
->wcp
->data_size
);
790 fsp
->wcp
->file_size
= file_size
;
794 /*******************************************************************
795 Flush a write cache struct to disk.
796 ********************************************************************/
798 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
800 write_cache
*wcp
= fsp
->wcp
;
804 if(!wcp
|| !wcp
->data_size
) {
808 data_size
= wcp
->data_size
;
811 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
813 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
814 fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
817 if(data_size
== wcp
->alloc_size
) {
818 DO_PROFILE_INC(writecache_num_perfect_writes
);
822 ret
= real_write_file(fsp
, wcp
->data
, wcp
->offset
, data_size
);
825 * Ensure file size if kept up to date if write extends file.
828 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
)) {
829 wcp
->file_size
= wcp
->offset
+ ret
;
835 /*******************************************************************
837 ********************************************************************/
839 NTSTATUS
sync_file(connection_struct
*conn
, files_struct
*fsp
, BOOL write_through
)
841 if (fsp
->fh
->fd
== -1)
842 return NT_STATUS_INVALID_HANDLE
;
844 if (lp_strict_sync(SNUM(conn
)) &&
845 (lp_syncalways(SNUM(conn
)) || write_through
)) {
846 int ret
= flush_write_cache(fsp
, SYNC_FLUSH
);
848 return map_nt_error_from_unix(errno
);
850 ret
= SMB_VFS_FSYNC(fsp
,fsp
->fh
->fd
);
852 return map_nt_error_from_unix(errno
);
858 /************************************************************
859 Perform a stat whether a valid fd or not.
860 ************************************************************/
862 int fsp_stat(files_struct
*fsp
, SMB_STRUCT_STAT
*pst
)
864 if (fsp
->fh
->fd
== -1) {
865 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
, pst
);
867 return SMB_VFS_FSTAT(fsp
,fsp
->fh
->fd
, pst
);