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
));
133 /* Yes - this is correct - writes don't update this. JRA. */
134 /* Found by Samba4 tests. */
136 fsp
->position_information
= fsp
->pos
;
143 /****************************************************************************
145 ****************************************************************************/
147 ssize_t
write_file(files_struct
*fsp
, char *data
, SMB_OFF_T pos
, size_t n
)
149 write_cache
*wcp
= fsp
->wcp
;
150 ssize_t total_written
= 0;
153 if (fsp
->print_file
) {
157 if (!rap_to_pjobid(fsp
->rap_print_jobid
, &snum
, &jobid
)) {
158 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
159 (unsigned int)fsp
->rap_print_jobid
));
164 return print_job_write(SNUM(fsp
->conn
), jobid
, data
, n
);
167 if (!fsp
->can_write
) {
172 if (!fsp
->modified
) {
174 fsp
->modified
= True
;
176 if (SMB_VFS_FSTAT(fsp
,fsp
->fd
,&st
) == 0) {
177 int dosmode
= dos_mode(fsp
->conn
,fsp
->fsp_name
,&st
);
178 fsp
->size
= (SMB_BIG_UINT
)st
.st_size
;
179 if (MAP_ARCHIVE(fsp
->conn
) && !IS_DOS_ARCHIVE(dosmode
))
180 file_chmod(fsp
->conn
,fsp
->fsp_name
,dosmode
| aARCH
,&st
);
183 * If this is the first write and we have an exclusive oplock then setup
187 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
188 setup_write_cache(fsp
, st
.st_size
);
195 DO_PROFILE_INC(writecache_total_writes
);
196 if (!fsp
->oplock_type
) {
197 DO_PROFILE_INC(writecache_non_oplock_writes
);
202 * If this file is level II oplocked then we need
203 * to grab the shared memory lock and inform all
204 * other files with a level II lock that they need
205 * to flush their read caches. We keep the lock over
206 * the shared memory area whilst doing this.
209 release_level_2_oplocks_on_change(fsp
);
212 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
213 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
214 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
215 profile_p
->writecache_init_writes
,
216 profile_p
->writecache_abutted_writes
,
217 profile_p
->writecache_total_writes
,
218 profile_p
->writecache_non_oplock_writes
,
219 profile_p
->writecache_allocated_write_caches
,
220 profile_p
->writecache_num_write_caches
,
221 profile_p
->writecache_direct_writes
,
222 profile_p
->writecache_num_perfect_writes
,
223 profile_p
->writecache_read_hits
));
225 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
226 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
227 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
228 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
229 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
230 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
231 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
232 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
237 DO_PROFILE_INC(writecache_direct_writes
);
238 total_written
= real_write_file(fsp
, data
, pos
, n
);
239 if ((total_written
!= -1) && (pos
+ total_written
> (SMB_OFF_T
)fsp
->size
))
240 fsp
->size
= (SMB_BIG_UINT
)(pos
+ total_written
);
241 return total_written
;
244 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
245 fsp
->fsp_name
, fsp
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
250 * If we have active cache and it isn't contiguous then we flush.
251 * NOTE: There is a small problem with running out of disk ....
254 if (wcp
->data_size
) {
256 BOOL cache_flush_needed
= False
;
258 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
260 /* ASCII art.... JRA.
262 +--------------+-----
263 | Cached data | Rest of allocated cache buffer....
264 +--------------+-----
266 +-------------------+
268 +-------------------+
273 * Start of write overlaps or abutts the existing data.
276 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
278 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
281 * Update the current buffer size with the new data.
284 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
)
285 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
288 * Update the file size if changed.
291 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
292 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
293 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
297 * If we used all the data then
304 cache_flush_needed
= True
;
307 * Move the start of data forward by the amount used,
308 * cut down the amount left by the same amount.
315 DO_PROFILE_INC(writecache_abutted_writes
);
316 total_written
= data_used
;
320 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
321 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
323 /* ASCII art.... JRA.
329 +-------------------+
331 +-------------------+
336 * End of write overlaps the existing data.
339 size_t data_used
= pos
+ n
- wcp
->offset
;
341 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
344 * Update the current buffer size with the new data.
347 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
)
348 wcp
->data_size
= pos
+ n
- wcp
->offset
;
351 * Update the file size if changed.
354 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
355 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
356 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
360 * We don't need to move the start of data, but we
361 * cut down the amount left by the amount used.
367 * We cannot have used all the data here.
370 cache_flush_needed
= True
;
372 DO_PROFILE_INC(writecache_abutted_writes
);
373 total_written
= data_used
;
377 } else if ( (pos
>= wcp
->file_size
) &&
378 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
379 (pos
> wcp
->offset
+ wcp
->data_size
) &&
380 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
382 /* ASCII art.... JRA.
386 +---------------+---------------+
387 | Cached data | Cache buffer |
388 +---------------+---------------+
390 +-------------------+
392 +-------------------+
397 * Non-contiguous write part of which fits within
398 * the cache buffer and is extending the file
399 * and the cache contents reflect the current
400 * data up to the current end of the file.
405 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)
408 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
411 * Fill in the non-continuous area with zeros.
414 memset(wcp
->data
+ wcp
->data_size
, '\0',
415 pos
- (wcp
->offset
+ wcp
->data_size
) );
417 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
420 * Update the current buffer size with the new data.
423 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
)
424 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
427 * Update the file size if changed.
430 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
431 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
432 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
436 * If we used all the data then
443 cache_flush_needed
= True
;
446 * Move the start of data forward by the amount used,
447 * cut down the amount left by the same amount.
454 DO_PROFILE_INC(writecache_abutted_writes
);
455 total_written
= data_used
;
461 /* ASCII art..... JRA.
465 +---------------+---------------+
466 | Cached data | Cache buffer |
467 +---------------+---------------+
469 +-------------------+
471 +-------------------+
475 +---------------+---------------+
476 | Cached data | Cache buffer |
477 +---------------+---------------+
479 +-------------------+
481 +-------------------+
485 +---------------+---------------+
486 | Cached data | Cache buffer |
487 +---------------+---------------+
489 +-----------------------------------------------------+
491 +-----------------------------------------------------+
496 * Write is bigger than buffer, or there is no overlap on the
500 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
501 len = %u\n",fsp
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
504 * Update the file size if needed.
507 if(pos
+ n
> wcp
->file_size
) {
508 wcp
->file_size
= pos
+ n
;
509 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
513 * If write would fit in the cache, and is larger than
514 * the data already in the cache, flush the cache and
515 * preferentially copy the data new data into it. Otherwise
516 * just write the data directly.
519 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
520 cache_flush_needed
= True
;
522 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
525 * If the write overlaps the entire cache, then
526 * discard the current contents of the cache.
527 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
530 if ((pos
<= wcp
->offset
) &&
531 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
532 DEBUG(9,("write_file: discarding overwritten write \
533 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
537 DO_PROFILE_INC(writecache_direct_writes
);
541 if (pos
+ ret
> wcp
->file_size
) {
542 wcp
->file_size
= pos
+ ret
;
543 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
553 if(wcp
->data_size
> wcp
->file_size
) {
554 wcp
->file_size
= wcp
->data_size
;
555 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
558 if (cache_flush_needed
) {
559 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
560 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
561 write_path
, fsp
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
562 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
564 flush_write_cache(fsp
, WRITE_FLUSH
);
569 * If the write request is bigger than the cache
570 * size, write it all out.
573 if (n
> wcp
->alloc_size
) {
574 ssize_t ret
= real_write_file(fsp
, data
, pos
, n
);
578 if (pos
+ ret
> wcp
->file_size
) {
579 wcp
->file_size
= pos
+ n
;
580 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
583 DO_PROFILE_INC(writecache_direct_writes
);
584 return total_written
+ n
;
588 * If there's any data left, cache it.
593 if (wcp
->data_size
) {
594 DO_PROFILE_INC(writecache_abutted_writes
);
596 DO_PROFILE_INC(writecache_init_writes
);
599 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
600 if (wcp
->data_size
== 0) {
602 DO_PROFILE_INC(writecache_num_write_caches
);
607 * Update the file size if changed.
610 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
611 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
612 fsp
->size
= (SMB_BIG_UINT
)wcp
->file_size
;
614 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
615 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
618 return total_written
; /* .... that's a write :) */
621 return total_written
;
624 /****************************************************************************
625 Delete the write cache structure.
626 ****************************************************************************/
628 void delete_write_cache(files_struct
*fsp
)
635 if(!(wcp
= fsp
->wcp
))
638 DO_PROFILE_DEC(writecache_allocated_write_caches
);
639 allocated_write_caches
--;
641 SMB_ASSERT(wcp
->data_size
== 0);
643 SAFE_FREE(wcp
->data
);
646 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp
->fsp_name
));
649 /****************************************************************************
650 Setup the write cache structure.
651 ****************************************************************************/
653 static BOOL
setup_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
655 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
658 if (allocated_write_caches
>= MAX_WRITE_CACHES
)
661 if(alloc_size
== 0 || fsp
->wcp
)
664 if((wcp
= (write_cache
*)malloc(sizeof(write_cache
))) == NULL
) {
665 DEBUG(0,("setup_write_cache: malloc fail.\n"));
669 wcp
->file_size
= file_size
;
671 wcp
->alloc_size
= alloc_size
;
673 if((wcp
->data
= malloc(wcp
->alloc_size
)) == NULL
) {
674 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
675 (unsigned int)wcp
->alloc_size
));
680 memset(wcp
->data
, '\0', wcp
->alloc_size
);
683 DO_PROFILE_INC(writecache_allocated_write_caches
);
684 allocated_write_caches
++;
686 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
687 fsp
->fsp_name
, (unsigned long)wcp
->alloc_size
));
692 /****************************************************************************
693 Cope with a size change.
694 ****************************************************************************/
696 void set_filelen_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
698 fsp
->size
= (SMB_BIG_UINT
)file_size
;
700 /* The cache *must* have been flushed before we do this. */
701 if (fsp
->wcp
->data_size
!= 0) {
703 slprintf(msg
, sizeof(msg
)-1, "set_filelen_write_cache: size change \
704 on file %s with write cache size = %lu\n", fsp
->fsp_name
, (unsigned long)fsp
->wcp
->data_size
);
707 fsp
->wcp
->file_size
= file_size
;
711 /*******************************************************************
712 Flush a write cache struct to disk.
713 ********************************************************************/
715 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
717 write_cache
*wcp
= fsp
->wcp
;
721 if(!wcp
|| !wcp
->data_size
)
724 data_size
= wcp
->data_size
;
727 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
729 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
730 fsp
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
733 if(data_size
== wcp
->alloc_size
)
734 DO_PROFILE_INC(writecache_num_perfect_writes
);
737 ret
= real_write_file(fsp
, wcp
->data
, wcp
->offset
, data_size
);
740 * Ensure file size if kept up to date if write extends file.
743 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
))
744 wcp
->file_size
= wcp
->offset
+ ret
;
749 /*******************************************************************
751 ********************************************************************/
753 void sync_file(connection_struct
*conn
, files_struct
*fsp
)
755 if(lp_strict_sync(SNUM(conn
)) && fsp
->fd
!= -1) {
756 flush_write_cache(fsp
, SYNC_FLUSH
);
757 SMB_VFS_FSYNC(fsp
,fsp
->fd
);
762 /************************************************************
763 Perform a stat whether a valid fd or not.
764 ************************************************************/
766 int fsp_stat(files_struct
*fsp
, SMB_STRUCT_STAT
*pst
)
769 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
, pst
);
771 return SMB_VFS_FSTAT(fsp
,fsp
->fd
, pst
);