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 Seek a file. Try to avoid the seek if possible.
29 ****************************************************************************/
31 SMB_OFF_T
seek_file(files_struct
*fsp
,SMB_OFF_T pos
)
36 if (fsp
->print_file
&& lp_postscript(fsp
->conn
->service
))
39 seek_ret
= fsp
->conn
->vfs_ops
.lseek(fsp
,fsp
->fd
,pos
+offset
,SEEK_SET
);
42 DEBUG(0,("seek_file: (%s) sys_lseek failed. Error was %s\n",
43 fsp
->fsp_name
, strerror(errno
) ));
48 fsp
->pos
= seek_ret
- offset
;
50 DEBUG(10,("seek_file (%s): requested pos = %.0f, new pos = %.0f\n",
51 fsp
->fsp_name
, (double)(pos
+offset
), (double)fsp
->pos
));
56 /****************************************************************************
57 Read from write cache if we can.
58 ****************************************************************************/
61 BOOL
read_from_write_cache(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
63 write_cache
*wcp
= fsp
->wcp
;
68 if(n
> wcp
->data_size
|| pos
< wcp
->offset
|| pos
+ n
> wcp
->offset
+ wcp
->data_size
)
71 memcpy(data
, wcp
->data
+ (pos
- wcp
->offset
), n
);
73 DO_PROFILE_INC(writecache_read_hits
);
78 /****************************************************************************
80 ****************************************************************************/
82 ssize_t
read_file(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
84 ssize_t ret
=0,readret
;
86 /* you can't read from print files */
91 * Serve from write cache if we can.
94 if(read_from_write_cache(fsp
, data
, pos
, n
))
97 flush_write_cache(fsp
, READ_FLUSH
);
99 if (seek_file(fsp
,pos
) == -1) {
100 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos
));
108 readret
= fsp
->conn
->vfs_ops
.read(fsp
,fsp
->fd
,data
,n
);
110 if ((errno
== EAGAIN
) && numretries
) {
111 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
118 #else /* NO DMF fix. */
119 readret
= fsp
->conn
->vfs_ops
.read(fsp
,fsp
->fd
,data
,n
);
127 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
128 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
133 /* how many write cache buffers have been allocated */
134 static unsigned int allocated_write_caches
;
136 /****************************************************************************
137 *Really* write to a file.
138 ****************************************************************************/
140 static ssize_t
real_write_file(files_struct
*fsp
,char *data
,SMB_OFF_T pos
, size_t n
)
144 if ((pos
!= -1) && (seek_file(fsp
,pos
) == -1))
147 ret
= vfs_write_data(fsp
,data
,n
);
149 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
150 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
155 /****************************************************************************
157 ****************************************************************************/
159 ssize_t
write_file(files_struct
*fsp
, char *data
, SMB_OFF_T pos
, size_t n
)
161 write_cache
*wcp
= fsp
->wcp
;
162 ssize_t total_written
= 0;
166 return print_job_write(fsp
->print_jobid
, data
, n
);
168 if (!fsp
->can_write
) {
173 if (!fsp
->modified
) {
175 fsp
->modified
= True
;
177 if (fsp
->conn
->vfs_ops
.fstat(fsp
,fsp
->fd
,&st
) == 0) {
178 int dosmode
= dos_mode(fsp
->conn
,fsp
->fsp_name
,&st
);
179 fsp
->size
= (SMB_BIG_UINT
)st
.st_size
;
180 if (MAP_ARCHIVE(fsp
->conn
) && !IS_DOS_ARCHIVE(dosmode
))
181 file_chmod(fsp
->conn
,fsp
->fsp_name
,dosmode
| aARCH
,&st
);
184 * If this is the first write and we have an exclusive oplock then setup
188 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
189 setup_write_cache(fsp
, st
.st_size
);
196 DO_PROFILE_INC(writecache_total_writes
);
197 if (!fsp
->oplock_type
) {
198 DO_PROFILE_INC(writecache_non_oplock_writes
);
203 * If this file is level II oplocked then we need
204 * to grab the shared memory lock and inform all
205 * other files with a level II lock that they need
206 * to flush their read caches. We keep the lock over
207 * the shared memory area whilst doing this.
210 release_level_2_oplocks_on_change(fsp
);
213 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
214 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
215 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
216 profile_p
->writecache_init_writes
,
217 profile_p
->writecache_abutted_writes
,
218 profile_p
->writecache_total_writes
,
219 profile_p
->writecache_non_oplock_writes
,
220 profile_p
->writecache_allocated_write_caches
,
221 profile_p
->writecache_num_write_caches
,
222 profile_p
->writecache_direct_writes
,
223 profile_p
->writecache_num_perfect_writes
,
224 profile_p
->writecache_read_hits
));
226 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
227 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
228 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
229 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
230 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
231 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
232 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
233 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
238 DO_PROFILE_INC(writecache_direct_writes
);
239 total_written
= real_write_file(fsp
, data
, pos
, n
);
240 if ((total_written
!= -1) && (pos
+ total_written
> (SMB_OFF_T
)fsp
->size
))
241 fsp
->size
= (SMB_BIG_UINT
)(pos
+ total_written
);
242 return total_written
;
245 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
246 fsp
->fsp_name
, fsp
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
249 * If we have active cache and it isn't contiguous then we flush.
250 * NOTE: There is a small problem with running out of disk ....
253 if (wcp
->data_size
) {
255 BOOL cache_flush_needed
= False
;
257 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
259 /* ASCII art.... JRA.
261 +--------------+-----
262 | Cached data | Rest of allocated cache buffer....
263 +--------------+-----
265 +-------------------+
267 +-------------------+
272 * Start of write overlaps or abutts the existing data.
275 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
277 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
280 * Update the current buffer size with the new data.
283 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
)
284 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
287 * Update the file size if changed.
290 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
291 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
292 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
296 * If we used all the data then
303 cache_flush_needed
= True
;
306 * Move the start of data forward by the amount used,
307 * cut down the amount left by the same amount.
314 DO_PROFILE_INC(writecache_abutted_writes
);
315 total_written
= data_used
;
319 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
320 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
322 /* ASCII art.... JRA.
328 +-------------------+
330 +-------------------+
335 * End of write overlaps the existing data.
338 size_t data_used
= pos
+ n
- wcp
->offset
;
340 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
343 * Update the current buffer size with the new data.
346 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
)
347 wcp
->data_size
= pos
+ n
- wcp
->offset
;
350 * Update the file size if changed.
353 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
354 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
355 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
359 * We don't need to move the start of data, but we
360 * cut down the amount left by the amount used.
366 * We cannot have used all the data here.
369 cache_flush_needed
= True
;
371 DO_PROFILE_INC(writecache_abutted_writes
);
372 total_written
= data_used
;
376 } else if ( (pos
>= wcp
->file_size
) &&
377 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
378 (pos
> wcp
->offset
+ wcp
->data_size
) &&
379 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
381 /* ASCII art.... JRA.
385 +---------------+---------------+
386 | Cached data | Cache buffer |
387 +---------------+---------------+
389 +-------------------+
391 +-------------------+
396 * Non-contiguous write part of which fits within
397 * the cache buffer and is extending the file
398 * and the cache contents reflect the current
399 * data up to the current end of the file.
404 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)
407 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
410 * Fill in the non-continuous area with zeros.
413 memset(wcp
->data
+ wcp
->data_size
, '\0',
414 pos
- (wcp
->offset
+ wcp
->data_size
) );
416 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
419 * Update the current buffer size with the new data.
422 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
)
423 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
426 * Update the file size if changed.
429 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
430 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
431 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
435 * If we used all the data then
442 cache_flush_needed
= True
;
445 * Move the start of data forward by the amount used,
446 * cut down the amount left by the same amount.
453 DO_PROFILE_INC(writecache_abutted_writes
);
454 total_written
= data_used
;
460 /* ASCII art..... JRA.
464 +---------------+---------------+
465 | Cached data | Cache buffer |
466 +---------------+---------------+
468 +-------------------+
470 +-------------------+
474 +---------------+---------------+
475 | Cached data | Cache buffer |
476 +---------------+---------------+
478 +-------------------+
480 +-------------------+
484 +---------------+---------------+
485 | Cached data | Cache buffer |
486 +---------------+---------------+
488 +-----------------------------------------------------+
490 +-----------------------------------------------------+
495 * Write is bigger than buffer, or there is no overlap on the
499 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
500 len = %u\n",fsp
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
503 * Update the file size if needed.
506 if(pos
+ n
> wcp
->file_size
) {
507 wcp
->file_size
= pos
+ n
;
508 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
512 * If write would fit in the cache, and is larger than
513 * the data already in the cache, flush the cache and
514 * preferentially copy the data new data into it. Otherwise
515 * just write the data directly.
518 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
519 cache_flush_needed
= True
;
521 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
524 * If the write overlaps the entire cache, then
525 * discard the current contents of the cache.
526 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
529 if ((pos
<= wcp
->offset
) &&
530 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
531 DEBUG(9,("write_file: discarding overwritten write \
532 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
536 DO_PROFILE_INC(writecache_direct_writes
);
540 if (pos
+ ret
> wcp
->file_size
) {
541 wcp
->file_size
= pos
+ ret
;
542 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
552 if(wcp
->data_size
> wcp
->file_size
) {
553 wcp
->file_size
= wcp
->data_size
;
554 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
557 if (cache_flush_needed
) {
558 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
559 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
560 write_path
, fsp
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
561 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
563 flush_write_cache(fsp
, WRITE_FLUSH
);
568 * If the write request is bigger than the cache
569 * size, write it all out.
572 if (n
> wcp
->alloc_size
) {
573 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
577 if (pos
+ ret
> wcp
->file_size
) {
578 wcp
->file_size
= pos
+ n
;
579 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
582 DO_PROFILE_INC(writecache_direct_writes
);
583 return total_written
+ n
;
587 * If there's any data left, cache it.
592 if (wcp
->data_size
) {
593 DO_PROFILE_INC(writecache_abutted_writes
);
595 DO_PROFILE_INC(writecache_init_writes
);
598 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
599 if (wcp
->data_size
== 0) {
601 DO_PROFILE_INC(writecache_num_write_caches
);
606 * Update the file size if changed.
609 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
610 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
611 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
613 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
614 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
617 return total_written
; /* .... that's a write :) */
620 return total_written
;
623 /****************************************************************************
624 Delete the write cache structure.
625 ****************************************************************************/
627 void delete_write_cache(files_struct
*fsp
)
634 if(!(wcp
= fsp
->wcp
))
637 DO_PROFILE_DEC(writecache_allocated_write_caches
);
638 allocated_write_caches
--;
640 SMB_ASSERT(wcp
->data_size
== 0);
642 SAFE_FREE(wcp
->data
);
645 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp
->fsp_name
));
648 /****************************************************************************
649 Setup the write cache structure.
650 ****************************************************************************/
652 static BOOL
setup_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
654 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
657 if (allocated_write_caches
>= MAX_WRITE_CACHES
)
660 if(alloc_size
== 0 || fsp
->wcp
)
663 if((wcp
= (write_cache
*)malloc(sizeof(write_cache
))) == NULL
) {
664 DEBUG(0,("setup_write_cache: malloc fail.\n"));
668 wcp
->file_size
= file_size
;
670 wcp
->alloc_size
= alloc_size
;
672 if((wcp
->data
= malloc(wcp
->alloc_size
)) == NULL
) {
673 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
674 (unsigned int)wcp
->alloc_size
));
679 memset(wcp
->data
, '\0', wcp
->alloc_size
);
682 DO_PROFILE_INC(writecache_allocated_write_caches
);
683 allocated_write_caches
++;
685 DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
686 fsp
->fsp_name
, wcp
->alloc_size
));
691 /****************************************************************************
692 Cope with a size change.
693 ****************************************************************************/
695 void set_filelen_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
697 fsp
->size
= (SMB_BIG_UINT
)file_size
;
699 /* The cache *must* have been flushed before we do this. */
700 if (fsp
->wcp
->data_size
!= 0) {
702 slprintf(msg
, sizeof(msg
)-1, "set_filelen_write_cache: size change \
703 on file %s with write cache size = %u\n", fsp
->fsp_name
, fsp
->wcp
->data_size
);
706 fsp
->wcp
->file_size
= file_size
;
710 /*******************************************************************
711 Flush a write cache struct to disk.
712 ********************************************************************/
714 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
716 write_cache
*wcp
= fsp
->wcp
;
720 if(!wcp
|| !wcp
->data_size
)
723 data_size
= wcp
->data_size
;
726 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
728 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
729 fsp
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
732 if(data_size
== wcp
->alloc_size
)
733 DO_PROFILE_INC(writecache_num_perfect_writes
);
736 ret
= real_write_file(fsp
, wcp
->data
, wcp
->offset
, data_size
);
739 * Ensure file size if kept up to date if write extends file.
742 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
))
743 wcp
->file_size
= wcp
->offset
+ ret
;
748 /*******************************************************************
750 ********************************************************************/
752 void sync_file(connection_struct
*conn
, files_struct
*fsp
)
754 if(lp_strict_sync(SNUM(conn
)) && fsp
->fd
!= -1) {
755 flush_write_cache(fsp
, SYNC_FLUSH
);
756 conn
->vfs_ops
.fsync(fsp
,fsp
->fd
);
761 /************************************************************
762 Perform a stat whether a valid fd or not.
763 ************************************************************/
765 int fsp_stat(files_struct
*fsp
, SMB_STRUCT_STAT
*pst
)
768 return vfs_stat(fsp
->conn
, fsp
->fsp_name
, pst
);
770 return vfs_fstat(fsp
,fsp
->fd
, pst
);