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 ****************************************************************************/
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
;
39 if(n
> wcp
->data_size
|| pos
< wcp
->offset
|| pos
+ n
> wcp
->offset
+ wcp
->data_size
)
42 memcpy(data
, wcp
->data
+ (pos
- wcp
->offset
), n
);
44 DO_PROFILE_INC(writecache_read_hits
);
49 /****************************************************************************
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 */
62 * Serve from write cache if we can.
65 if(read_from_write_cache(fsp
, data
, pos
, n
)) {
67 fsp
->position_information
= fsp
->pos
;
71 flush_write_cache(fsp
, READ_FLUSH
);
79 readret
= SMB_VFS_PREAD(fsp
,fsp
->fd
,data
,n
,pos
);
82 if ((errno
== EAGAIN
) && numretries
) {
83 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
90 #else /* NO DMF fix. */
91 readret
= SMB_VFS_PREAD(fsp
,fsp
->fd
,data
,n
,pos
);
100 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
101 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
104 fsp
->position_information
= fsp
->pos
;
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
)
121 ret
= vfs_write_data(fsp
, data
, n
);
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
));
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. */
150 fsp
->position_information
= fsp
->pos
;
157 /****************************************************************************
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;
167 if (fsp
->print_file
) {
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
));
178 return print_job_write(SNUM(fsp
->conn
), jobid
, data
, n
);
181 if (!fsp
->can_write
) {
186 if (!fsp
->modified
) {
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
202 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
203 setup_write_cache(fsp
, st
.st_size
);
210 DO_PROFILE_INC(writecache_total_writes
);
211 if (!fsp
->oplock_type
) {
212 DO_PROFILE_INC(writecache_non_oplock_writes
);
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
);
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
] ));
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
));
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 +-------------------+
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
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.
330 DO_PROFILE_INC(writecache_abutted_writes
);
331 total_written
= data_used
;
335 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
336 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
338 /* ASCII art.... JRA.
344 +-------------------+
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.
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
;
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.
401 +---------------+---------------+
402 | Cached data | Cache buffer |
403 +---------------+---------------+
405 +-------------------+
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.
420 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)
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
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.
469 DO_PROFILE_INC(writecache_abutted_writes
);
470 total_written
= data_used
;
476 /* ASCII art..... JRA.
480 +---------------+---------------+
481 | Cached data | Cache buffer |
482 +---------------+---------------+
484 +-------------------+
486 +-------------------+
490 +---------------+---------------+
491 | Cached data | Cache buffer |
492 +---------------+---------------+
494 +-------------------+
496 +-------------------+
500 +---------------+---------------+
501 | Cached data | Cache buffer |
502 +---------------+---------------+
504 +-----------------------------------------------------+
506 +-----------------------------------------------------+
511 * Write is bigger than buffer, or there is no overlap on the
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
;
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
));
552 DO_PROFILE_INC(writecache_direct_writes
);
556 if (pos
+ ret
> wcp
->file_size
) {
557 wcp
->file_size
= pos
+ ret
;
558 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
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
);
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.
608 if (wcp
->data_size
) {
609 DO_PROFILE_INC(writecache_abutted_writes
);
611 DO_PROFILE_INC(writecache_init_writes
);
614 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
615 if (wcp
->data_size
== 0) {
617 DO_PROFILE_INC(writecache_num_write_caches
);
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
));
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
)
650 if(!(wcp
= fsp
->wcp
))
653 DO_PROFILE_DEC(writecache_allocated_write_caches
);
654 allocated_write_caches
--;
656 SMB_ASSERT(wcp
->data_size
== 0);
658 SAFE_FREE(wcp
->data
);
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
));
673 if (allocated_write_caches
>= MAX_WRITE_CACHES
)
676 if(alloc_size
== 0 || fsp
->wcp
)
679 if((wcp
= SMB_MALLOC_P(write_cache
)) == NULL
) {
680 DEBUG(0,("setup_write_cache: malloc fail.\n"));
684 wcp
->file_size
= file_size
;
686 wcp
->alloc_size
= alloc_size
;
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
));
695 memset(wcp
->data
, '\0', wcp
->alloc_size
);
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
));
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
;
715 /* The cache *must* have been flushed before we do this. */
716 if (fsp
->wcp
->data_size
!= 0) {
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
);
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
;
736 if(!wcp
|| !wcp
->data_size
)
739 data_size
= wcp
->data_size
;
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
));
748 if(data_size
== wcp
->alloc_size
)
749 DO_PROFILE_INC(writecache_num_perfect_writes
);
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
;
764 /*******************************************************************
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
)
784 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
, pst
);
786 return SMB_VFS_FSTAT(fsp
,fsp
->fd
, pst
);