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 static SMB_OFF_T
seek_file(files_struct
*fsp
,SMB_OFF_T pos
)
35 seek_ret
= SMB_VFS_LSEEK(fsp
,fsp
->fd
,pos
,SEEK_SET
);
38 DEBUG(0,("seek_file: (%s) sys_lseek failed. Error was %s\n",
39 fsp
->fsp_name
, strerror(errno
) ));
46 DEBUG(10,("seek_file (%s): requested pos = %.0f, new pos = %.0f\n",
47 fsp
->fsp_name
, (double)pos
, (double)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
;
64 if(n
> wcp
->data_size
|| pos
< wcp
->offset
|| pos
+ n
> wcp
->offset
+ wcp
->data_size
)
67 memcpy(data
, wcp
->data
+ (pos
- wcp
->offset
), n
);
69 DO_PROFILE_INC(writecache_read_hits
);
74 /****************************************************************************
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 */
87 * Serve from write cache if we can.
90 if(read_from_write_cache(fsp
, data
, pos
, n
))
93 flush_write_cache(fsp
, READ_FLUSH
);
95 if (seek_file(fsp
,pos
) == -1) {
96 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos
));
104 readret
= SMB_VFS_READ(fsp
,fsp
->fd
,data
,n
);
106 if ((errno
== EAGAIN
) && numretries
) {
107 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
114 #else /* NO DMF fix. */
115 readret
= SMB_VFS_READ(fsp
,fsp
->fd
,data
,n
);
123 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
124 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
129 /* how many write cache buffers have been allocated */
130 static unsigned int allocated_write_caches
;
132 /****************************************************************************
133 *Really* write to a file.
134 ****************************************************************************/
136 static ssize_t
real_write_file(files_struct
*fsp
,char *data
,SMB_OFF_T pos
, size_t n
)
140 if ((pos
!= -1) && (seek_file(fsp
,pos
) == -1))
143 ret
= vfs_write_data(fsp
,data
,n
);
145 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
146 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
151 /****************************************************************************
153 ****************************************************************************/
155 ssize_t
write_file(files_struct
*fsp
, char *data
, SMB_OFF_T pos
, size_t n
)
157 write_cache
*wcp
= fsp
->wcp
;
158 ssize_t total_written
= 0;
161 if (fsp
->print_file
) {
165 if (!rap_to_pjobid(fsp
->rap_print_jobid
, &snum
, &jobid
)) {
166 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
167 (unsigned int)fsp
->rap_print_jobid
));
172 return print_job_write(SNUM(fsp
->conn
), jobid
, data
, n
);
175 if (!fsp
->can_write
) {
180 if (!fsp
->modified
) {
182 fsp
->modified
= True
;
184 if (SMB_VFS_FSTAT(fsp
,fsp
->fd
,&st
) == 0) {
185 int dosmode
= dos_mode(fsp
->conn
,fsp
->fsp_name
,&st
);
186 fsp
->size
= (SMB_BIG_UINT
)st
.st_size
;
187 if (MAP_ARCHIVE(fsp
->conn
) && !IS_DOS_ARCHIVE(dosmode
))
188 file_chmod(fsp
->conn
,fsp
->fsp_name
,dosmode
| aARCH
,&st
);
191 * If this is the first write and we have an exclusive oplock then setup
195 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
196 setup_write_cache(fsp
, st
.st_size
);
203 DO_PROFILE_INC(writecache_total_writes
);
204 if (!fsp
->oplock_type
) {
205 DO_PROFILE_INC(writecache_non_oplock_writes
);
210 * If this file is level II oplocked then we need
211 * to grab the shared memory lock and inform all
212 * other files with a level II lock that they need
213 * to flush their read caches. We keep the lock over
214 * the shared memory area whilst doing this.
217 release_level_2_oplocks_on_change(fsp
);
220 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
221 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
222 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
223 profile_p
->writecache_init_writes
,
224 profile_p
->writecache_abutted_writes
,
225 profile_p
->writecache_total_writes
,
226 profile_p
->writecache_non_oplock_writes
,
227 profile_p
->writecache_allocated_write_caches
,
228 profile_p
->writecache_num_write_caches
,
229 profile_p
->writecache_direct_writes
,
230 profile_p
->writecache_num_perfect_writes
,
231 profile_p
->writecache_read_hits
));
233 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
234 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
235 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
236 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
237 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
238 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
239 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
240 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
245 DO_PROFILE_INC(writecache_direct_writes
);
246 total_written
= real_write_file(fsp
, data
, pos
, n
);
247 if ((total_written
!= -1) && (pos
+ total_written
> (SMB_OFF_T
)fsp
->size
))
248 fsp
->size
= (SMB_BIG_UINT
)(pos
+ total_written
);
249 return total_written
;
252 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
253 fsp
->fsp_name
, fsp
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
256 * If we have active cache and it isn't contiguous then we flush.
257 * NOTE: There is a small problem with running out of disk ....
260 if (wcp
->data_size
) {
262 BOOL cache_flush_needed
= False
;
264 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
266 /* ASCII art.... JRA.
268 +--------------+-----
269 | Cached data | Rest of allocated cache buffer....
270 +--------------+-----
272 +-------------------+
274 +-------------------+
279 * Start of write overlaps or abutts the existing data.
282 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
284 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
287 * Update the current buffer size with the new data.
290 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
)
291 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
294 * Update the file size if changed.
297 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
298 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
299 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
303 * If we used all the data then
310 cache_flush_needed
= True
;
313 * Move the start of data forward by the amount used,
314 * cut down the amount left by the same amount.
321 DO_PROFILE_INC(writecache_abutted_writes
);
322 total_written
= data_used
;
326 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
327 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
329 /* ASCII art.... JRA.
335 +-------------------+
337 +-------------------+
342 * End of write overlaps the existing data.
345 size_t data_used
= pos
+ n
- wcp
->offset
;
347 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
350 * Update the current buffer size with the new data.
353 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
)
354 wcp
->data_size
= pos
+ n
- wcp
->offset
;
357 * Update the file size if changed.
360 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
361 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
362 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
366 * We don't need to move the start of data, but we
367 * cut down the amount left by the amount used.
373 * We cannot have used all the data here.
376 cache_flush_needed
= True
;
378 DO_PROFILE_INC(writecache_abutted_writes
);
379 total_written
= data_used
;
383 } else if ( (pos
>= wcp
->file_size
) &&
384 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
385 (pos
> wcp
->offset
+ wcp
->data_size
) &&
386 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
388 /* ASCII art.... JRA.
392 +---------------+---------------+
393 | Cached data | Cache buffer |
394 +---------------+---------------+
396 +-------------------+
398 +-------------------+
403 * Non-contiguous write part of which fits within
404 * the cache buffer and is extending the file
405 * and the cache contents reflect the current
406 * data up to the current end of the file.
411 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)
414 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
417 * Fill in the non-continuous area with zeros.
420 memset(wcp
->data
+ wcp
->data_size
, '\0',
421 pos
- (wcp
->offset
+ wcp
->data_size
) );
423 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
426 * Update the current buffer size with the new data.
429 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
)
430 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
433 * Update the file size if changed.
436 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
437 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
438 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
442 * If we used all the data then
449 cache_flush_needed
= True
;
452 * Move the start of data forward by the amount used,
453 * cut down the amount left by the same amount.
460 DO_PROFILE_INC(writecache_abutted_writes
);
461 total_written
= data_used
;
467 /* ASCII art..... JRA.
471 +---------------+---------------+
472 | Cached data | Cache buffer |
473 +---------------+---------------+
475 +-------------------+
477 +-------------------+
481 +---------------+---------------+
482 | Cached data | Cache buffer |
483 +---------------+---------------+
485 +-------------------+
487 +-------------------+
491 +---------------+---------------+
492 | Cached data | Cache buffer |
493 +---------------+---------------+
495 +-----------------------------------------------------+
497 +-----------------------------------------------------+
502 * Write is bigger than buffer, or there is no overlap on the
506 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
507 len = %u\n",fsp
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
510 * Update the file size if needed.
513 if(pos
+ n
> wcp
->file_size
) {
514 wcp
->file_size
= pos
+ n
;
515 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
519 * If write would fit in the cache, and is larger than
520 * the data already in the cache, flush the cache and
521 * preferentially copy the data new data into it. Otherwise
522 * just write the data directly.
525 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
526 cache_flush_needed
= True
;
528 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
531 * If the write overlaps the entire cache, then
532 * discard the current contents of the cache.
533 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
536 if ((pos
<= wcp
->offset
) &&
537 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
538 DEBUG(9,("write_file: discarding overwritten write \
539 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
543 DO_PROFILE_INC(writecache_direct_writes
);
547 if (pos
+ ret
> wcp
->file_size
) {
548 wcp
->file_size
= pos
+ ret
;
549 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
559 if(wcp
->data_size
> wcp
->file_size
) {
560 wcp
->file_size
= wcp
->data_size
;
561 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
564 if (cache_flush_needed
) {
565 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
566 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
567 write_path
, fsp
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
568 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
570 flush_write_cache(fsp
, WRITE_FLUSH
);
575 * If the write request is bigger than the cache
576 * size, write it all out.
579 if (n
> wcp
->alloc_size
) {
580 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
584 if (pos
+ ret
> wcp
->file_size
) {
585 wcp
->file_size
= pos
+ n
;
586 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
589 DO_PROFILE_INC(writecache_direct_writes
);
590 return total_written
+ n
;
594 * If there's any data left, cache it.
599 if (wcp
->data_size
) {
600 DO_PROFILE_INC(writecache_abutted_writes
);
602 DO_PROFILE_INC(writecache_init_writes
);
605 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
606 if (wcp
->data_size
== 0) {
608 DO_PROFILE_INC(writecache_num_write_caches
);
613 * Update the file size if changed.
616 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
617 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
618 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
620 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
621 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
624 return total_written
; /* .... that's a write :) */
627 return total_written
;
630 /****************************************************************************
631 Delete the write cache structure.
632 ****************************************************************************/
634 void delete_write_cache(files_struct
*fsp
)
641 if(!(wcp
= fsp
->wcp
))
644 DO_PROFILE_DEC(writecache_allocated_write_caches
);
645 allocated_write_caches
--;
647 SMB_ASSERT(wcp
->data_size
== 0);
649 SAFE_FREE(wcp
->data
);
652 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp
->fsp_name
));
655 /****************************************************************************
656 Setup the write cache structure.
657 ****************************************************************************/
659 static BOOL
setup_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
661 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
664 if (allocated_write_caches
>= MAX_WRITE_CACHES
)
667 if(alloc_size
== 0 || fsp
->wcp
)
670 if((wcp
= (write_cache
*)malloc(sizeof(write_cache
))) == NULL
) {
671 DEBUG(0,("setup_write_cache: malloc fail.\n"));
675 wcp
->file_size
= file_size
;
677 wcp
->alloc_size
= alloc_size
;
679 if((wcp
->data
= malloc(wcp
->alloc_size
)) == NULL
) {
680 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
681 (unsigned int)wcp
->alloc_size
));
686 memset(wcp
->data
, '\0', wcp
->alloc_size
);
689 DO_PROFILE_INC(writecache_allocated_write_caches
);
690 allocated_write_caches
++;
692 DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
693 fsp
->fsp_name
, wcp
->alloc_size
));
698 /****************************************************************************
699 Cope with a size change.
700 ****************************************************************************/
702 void set_filelen_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
704 fsp
->size
= (SMB_BIG_UINT
)file_size
;
706 /* The cache *must* have been flushed before we do this. */
707 if (fsp
->wcp
->data_size
!= 0) {
709 slprintf(msg
, sizeof(msg
)-1, "set_filelen_write_cache: size change \
710 on file %s with write cache size = %u\n", fsp
->fsp_name
, fsp
->wcp
->data_size
);
713 fsp
->wcp
->file_size
= file_size
;
717 /*******************************************************************
718 Flush a write cache struct to disk.
719 ********************************************************************/
721 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
723 write_cache
*wcp
= fsp
->wcp
;
727 if(!wcp
|| !wcp
->data_size
)
730 data_size
= wcp
->data_size
;
733 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
735 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
736 fsp
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
739 if(data_size
== wcp
->alloc_size
)
740 DO_PROFILE_INC(writecache_num_perfect_writes
);
743 ret
= real_write_file(fsp
, wcp
->data
, wcp
->offset
, data_size
);
746 * Ensure file size if kept up to date if write extends file.
749 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
))
750 wcp
->file_size
= wcp
->offset
+ ret
;
755 /*******************************************************************
757 ********************************************************************/
759 void sync_file(connection_struct
*conn
, files_struct
*fsp
)
761 if(lp_strict_sync(SNUM(conn
)) && fsp
->fd
!= -1) {
762 flush_write_cache(fsp
, SYNC_FLUSH
);
763 SMB_VFS_FSYNC(fsp
,fsp
->fd
);
768 /************************************************************
769 Perform a stat whether a valid fd or not.
770 ************************************************************/
772 int fsp_stat(files_struct
*fsp
, SMB_STRUCT_STAT
*pst
)
775 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
, pst
);
777 return SMB_VFS_FSTAT(fsp
,fsp
->fd
, pst
);