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
)) {
92 fsp
->position_information
= fsp
->pos
;
96 flush_write_cache(fsp
, READ_FLUSH
);
98 if (seek_file(fsp
,pos
) == -1) {
99 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos
));
107 readret
= SMB_VFS_READ(fsp
,fsp
->fd
,data
,n
);
109 if ((errno
== EAGAIN
) && numretries
) {
110 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
117 #else /* NO DMF fix. */
118 readret
= SMB_VFS_READ(fsp
,fsp
->fd
,data
,n
);
126 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
127 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
130 fsp
->position_information
= fsp
->pos
;
135 /* how many write cache buffers have been allocated */
136 static unsigned int allocated_write_caches
;
138 /****************************************************************************
139 *Really* write to a file.
140 ****************************************************************************/
142 static ssize_t
real_write_file(files_struct
*fsp
,char *data
,SMB_OFF_T pos
, size_t n
)
146 if ((pos
!= -1) && (seek_file(fsp
,pos
) == -1))
149 ret
= vfs_write_data(fsp
,data
,n
);
151 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
152 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
157 /* Yes - this is correct - writes don't update this. JRA. */
158 /* Found by Samba4 tests. */
160 fsp
->position_information
= fsp
->pos
;
167 /****************************************************************************
169 ****************************************************************************/
171 ssize_t
write_file(files_struct
*fsp
, char *data
, SMB_OFF_T pos
, size_t n
)
173 write_cache
*wcp
= fsp
->wcp
;
174 ssize_t total_written
= 0;
177 if (fsp
->print_file
) {
181 if (!rap_to_pjobid(fsp
->rap_print_jobid
, &snum
, &jobid
)) {
182 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
183 (unsigned int)fsp
->rap_print_jobid
));
188 return print_job_write(SNUM(fsp
->conn
), jobid
, data
, n
);
191 if (!fsp
->can_write
) {
196 if (!fsp
->modified
) {
198 fsp
->modified
= True
;
200 if (SMB_VFS_FSTAT(fsp
,fsp
->fd
,&st
) == 0) {
201 int dosmode
= dos_mode(fsp
->conn
,fsp
->fsp_name
,&st
);
202 fsp
->size
= (SMB_BIG_UINT
)st
.st_size
;
203 if (MAP_ARCHIVE(fsp
->conn
) && !IS_DOS_ARCHIVE(dosmode
))
204 file_chmod(fsp
->conn
,fsp
->fsp_name
,dosmode
| aARCH
,&st
);
207 * If this is the first write and we have an exclusive oplock then setup
211 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
212 setup_write_cache(fsp
, st
.st_size
);
219 DO_PROFILE_INC(writecache_total_writes
);
220 if (!fsp
->oplock_type
) {
221 DO_PROFILE_INC(writecache_non_oplock_writes
);
226 * If this file is level II oplocked then we need
227 * to grab the shared memory lock and inform all
228 * other files with a level II lock that they need
229 * to flush their read caches. We keep the lock over
230 * the shared memory area whilst doing this.
233 release_level_2_oplocks_on_change(fsp
);
236 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
237 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
238 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
239 profile_p
->writecache_init_writes
,
240 profile_p
->writecache_abutted_writes
,
241 profile_p
->writecache_total_writes
,
242 profile_p
->writecache_non_oplock_writes
,
243 profile_p
->writecache_allocated_write_caches
,
244 profile_p
->writecache_num_write_caches
,
245 profile_p
->writecache_direct_writes
,
246 profile_p
->writecache_num_perfect_writes
,
247 profile_p
->writecache_read_hits
));
249 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
250 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
251 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
252 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
253 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
254 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
255 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
256 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
261 DO_PROFILE_INC(writecache_direct_writes
);
262 total_written
= real_write_file(fsp
, data
, pos
, n
);
263 if ((total_written
!= -1) && (pos
+ total_written
> (SMB_OFF_T
)fsp
->size
))
264 fsp
->size
= (SMB_BIG_UINT
)(pos
+ total_written
);
265 return total_written
;
268 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
269 fsp
->fsp_name
, fsp
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
274 * If we have active cache and it isn't contiguous then we flush.
275 * NOTE: There is a small problem with running out of disk ....
278 if (wcp
->data_size
) {
280 BOOL cache_flush_needed
= False
;
282 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
284 /* ASCII art.... JRA.
286 +--------------+-----
287 | Cached data | Rest of allocated cache buffer....
288 +--------------+-----
290 +-------------------+
292 +-------------------+
297 * Start of write overlaps or abutts the existing data.
300 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
302 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
305 * Update the current buffer size with the new data.
308 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
)
309 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
312 * Update the file size if changed.
315 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
316 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
317 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
321 * If we used all the data then
328 cache_flush_needed
= True
;
331 * Move the start of data forward by the amount used,
332 * cut down the amount left by the same amount.
339 DO_PROFILE_INC(writecache_abutted_writes
);
340 total_written
= data_used
;
344 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
345 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
347 /* ASCII art.... JRA.
353 +-------------------+
355 +-------------------+
360 * End of write overlaps the existing data.
363 size_t data_used
= pos
+ n
- wcp
->offset
;
365 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
368 * Update the current buffer size with the new data.
371 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
)
372 wcp
->data_size
= pos
+ n
- wcp
->offset
;
375 * Update the file size if changed.
378 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
379 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
380 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
384 * We don't need to move the start of data, but we
385 * cut down the amount left by the amount used.
391 * We cannot have used all the data here.
394 cache_flush_needed
= True
;
396 DO_PROFILE_INC(writecache_abutted_writes
);
397 total_written
= data_used
;
401 } else if ( (pos
>= wcp
->file_size
) &&
402 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
403 (pos
> wcp
->offset
+ wcp
->data_size
) &&
404 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
406 /* ASCII art.... JRA.
410 +---------------+---------------+
411 | Cached data | Cache buffer |
412 +---------------+---------------+
414 +-------------------+
416 +-------------------+
421 * Non-contiguous write part of which fits within
422 * the cache buffer and is extending the file
423 * and the cache contents reflect the current
424 * data up to the current end of the file.
429 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)
432 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
435 * Fill in the non-continuous area with zeros.
438 memset(wcp
->data
+ wcp
->data_size
, '\0',
439 pos
- (wcp
->offset
+ wcp
->data_size
) );
441 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
444 * Update the current buffer size with the new data.
447 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
)
448 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
451 * Update the file size if changed.
454 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
455 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
456 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
460 * If we used all the data then
467 cache_flush_needed
= True
;
470 * Move the start of data forward by the amount used,
471 * cut down the amount left by the same amount.
478 DO_PROFILE_INC(writecache_abutted_writes
);
479 total_written
= data_used
;
485 /* ASCII art..... JRA.
489 +---------------+---------------+
490 | Cached data | Cache buffer |
491 +---------------+---------------+
493 +-------------------+
495 +-------------------+
499 +---------------+---------------+
500 | Cached data | Cache buffer |
501 +---------------+---------------+
503 +-------------------+
505 +-------------------+
509 +---------------+---------------+
510 | Cached data | Cache buffer |
511 +---------------+---------------+
513 +-----------------------------------------------------+
515 +-----------------------------------------------------+
520 * Write is bigger than buffer, or there is no overlap on the
524 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
525 len = %u\n",fsp
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
528 * Update the file size if needed.
531 if(pos
+ n
> wcp
->file_size
) {
532 wcp
->file_size
= pos
+ n
;
533 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
537 * If write would fit in the cache, and is larger than
538 * the data already in the cache, flush the cache and
539 * preferentially copy the data new data into it. Otherwise
540 * just write the data directly.
543 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
544 cache_flush_needed
= True
;
546 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
549 * If the write overlaps the entire cache, then
550 * discard the current contents of the cache.
551 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
554 if ((pos
<= wcp
->offset
) &&
555 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
556 DEBUG(9,("write_file: discarding overwritten write \
557 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
561 DO_PROFILE_INC(writecache_direct_writes
);
565 if (pos
+ ret
> wcp
->file_size
) {
566 wcp
->file_size
= pos
+ ret
;
567 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
577 if(wcp
->data_size
> wcp
->file_size
) {
578 wcp
->file_size
= wcp
->data_size
;
579 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
582 if (cache_flush_needed
) {
583 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
584 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
585 write_path
, fsp
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
586 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
588 flush_write_cache(fsp
, WRITE_FLUSH
);
593 * If the write request is bigger than the cache
594 * size, write it all out.
597 if (n
> wcp
->alloc_size
) {
598 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
602 if (pos
+ ret
> wcp
->file_size
) {
603 wcp
->file_size
= pos
+ n
;
604 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
607 DO_PROFILE_INC(writecache_direct_writes
);
608 return total_written
+ n
;
612 * If there's any data left, cache it.
617 if (wcp
->data_size
) {
618 DO_PROFILE_INC(writecache_abutted_writes
);
620 DO_PROFILE_INC(writecache_init_writes
);
623 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
624 if (wcp
->data_size
== 0) {
626 DO_PROFILE_INC(writecache_num_write_caches
);
631 * Update the file size if changed.
634 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
635 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
636 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
638 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
639 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
642 return total_written
; /* .... that's a write :) */
645 return total_written
;
648 /****************************************************************************
649 Delete the write cache structure.
650 ****************************************************************************/
652 void delete_write_cache(files_struct
*fsp
)
659 if(!(wcp
= fsp
->wcp
))
662 DO_PROFILE_DEC(writecache_allocated_write_caches
);
663 allocated_write_caches
--;
665 SMB_ASSERT(wcp
->data_size
== 0);
667 SAFE_FREE(wcp
->data
);
670 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp
->fsp_name
));
673 /****************************************************************************
674 Setup the write cache structure.
675 ****************************************************************************/
677 static BOOL
setup_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
679 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
682 if (allocated_write_caches
>= MAX_WRITE_CACHES
)
685 if(alloc_size
== 0 || fsp
->wcp
)
688 if((wcp
= (write_cache
*)malloc(sizeof(write_cache
))) == NULL
) {
689 DEBUG(0,("setup_write_cache: malloc fail.\n"));
693 wcp
->file_size
= file_size
;
695 wcp
->alloc_size
= alloc_size
;
697 if((wcp
->data
= malloc(wcp
->alloc_size
)) == NULL
) {
698 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
699 (unsigned int)wcp
->alloc_size
));
704 memset(wcp
->data
, '\0', wcp
->alloc_size
);
707 DO_PROFILE_INC(writecache_allocated_write_caches
);
708 allocated_write_caches
++;
710 DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
711 fsp
->fsp_name
, wcp
->alloc_size
));
716 /****************************************************************************
717 Cope with a size change.
718 ****************************************************************************/
720 void set_filelen_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
722 fsp
->size
= (SMB_BIG_UINT
)file_size
;
724 /* The cache *must* have been flushed before we do this. */
725 if (fsp
->wcp
->data_size
!= 0) {
727 slprintf(msg
, sizeof(msg
)-1, "set_filelen_write_cache: size change \
728 on file %s with write cache size = %u\n", fsp
->fsp_name
, fsp
->wcp
->data_size
);
731 fsp
->wcp
->file_size
= file_size
;
735 /*******************************************************************
736 Flush a write cache struct to disk.
737 ********************************************************************/
739 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
741 write_cache
*wcp
= fsp
->wcp
;
745 if(!wcp
|| !wcp
->data_size
)
748 data_size
= wcp
->data_size
;
751 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
753 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
754 fsp
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
757 if(data_size
== wcp
->alloc_size
)
758 DO_PROFILE_INC(writecache_num_perfect_writes
);
761 ret
= real_write_file(fsp
, wcp
->data
, wcp
->offset
, data_size
);
764 * Ensure file size if kept up to date if write extends file.
767 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
))
768 wcp
->file_size
= wcp
->offset
+ ret
;
773 /*******************************************************************
775 ********************************************************************/
777 void sync_file(connection_struct
*conn
, files_struct
*fsp
)
779 if(lp_strict_sync(SNUM(conn
)) && fsp
->fd
!= -1) {
780 flush_write_cache(fsp
, SYNC_FLUSH
);
781 SMB_VFS_FSYNC(fsp
,fsp
->fd
);
786 /************************************************************
787 Perform a stat whether a valid fd or not.
788 ************************************************************/
790 int fsp_stat(files_struct
*fsp
, SMB_STRUCT_STAT
*pst
)
793 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
, pst
);
795 return SMB_VFS_FSTAT(fsp
,fsp
->fd
, pst
);