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 3 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, see <http://www.gnu.org/licenses/>.
24 static bool setup_write_cache(files_struct
*, SMB_OFF_T
);
26 /****************************************************************************
27 Read from write cache if we can.
28 ****************************************************************************/
30 static bool read_from_write_cache(files_struct
*fsp
,char *data
,SMB_OFF_T pos
,size_t n
)
32 write_cache
*wcp
= fsp
->wcp
;
38 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 */
58 if (fsp
->print_file
) {
63 * Serve from write cache if we can.
66 if(read_from_write_cache(fsp
, data
, pos
, n
)) {
67 fsp
->fh
->pos
= pos
+ n
;
68 fsp
->fh
->position_information
= fsp
->fh
->pos
;
72 flush_write_cache(fsp
, READ_FLUSH
);
80 readret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
83 if ((errno
== EAGAIN
) && numretries
) {
84 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
91 #else /* NO DMF fix. */
92 readret
= SMB_VFS_PREAD(fsp
,data
,n
,pos
);
103 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
104 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
107 fsp
->fh
->position_information
= fsp
->fh
->pos
;
112 /* how many write cache buffers have been allocated */
113 static unsigned int allocated_write_caches
;
115 /****************************************************************************
116 *Really* write to a file.
117 ****************************************************************************/
119 static ssize_t
real_write_file(struct smb_request
*req
,
128 ret
= vfs_write_data(req
, fsp
, data
, n
);
131 if (pos
&& lp_strict_allocate(SNUM(fsp
->conn
))) {
132 if (vfs_fill_sparse(fsp
, pos
) == -1) {
136 ret
= vfs_pwrite_data(req
, fsp
, data
, n
, pos
);
139 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
140 fsp
->fsp_name
, (double)pos
, (unsigned long)n
, (long)ret
));
145 /* Yes - this is correct - writes don't update this. JRA. */
146 /* Found by Samba4 tests. */
148 fsp
->position_information
= fsp
->pos
;
155 /****************************************************************************
156 File size cache change.
157 Updates size on disk but doesn't flush the cache.
158 ****************************************************************************/
160 static int wcp_file_size_change(files_struct
*fsp
)
163 write_cache
*wcp
= fsp
->wcp
;
165 wcp
->file_size
= wcp
->offset
+ wcp
->data_size
;
166 ret
= SMB_VFS_FTRUNCATE(fsp
, wcp
->file_size
);
168 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f error %s\n",
169 fsp
->fsp_name
, (double)wcp
->file_size
, strerror(errno
) ));
174 static void update_write_time_handler(struct event_context
*ctx
,
175 struct timed_event
*te
,
176 const struct timeval
*now
,
179 files_struct
*fsp
= (files_struct
*)private_data
;
181 /* Remove the timed event handler. */
182 TALLOC_FREE(fsp
->update_write_time_event
);
183 DEBUG(5, ("Update write time on %s\n", fsp
->fsp_name
));
185 /* change the write time if not already changed by someoneelse */
186 set_write_time_fsp(fsp
, timespec_current(), false);
189 void trigger_write_time_update(struct files_struct
*fsp
)
193 if (fsp
->write_time_forced
) {
197 if (fsp
->update_write_time_triggered
) {
200 fsp
->update_write_time_triggered
= true;
202 delay
= lp_parm_int(SNUM(fsp
->conn
),
203 "smbd", "writetimeupdatedelay",
204 WRITE_TIME_UPDATE_USEC_DELAY
);
206 /* trigger the update 2 seconds later */
207 fsp
->update_write_time_on_close
= true;
208 fsp
->update_write_time_event
=
209 event_add_timed(smbd_event_context(), NULL
,
210 timeval_current_ofs(0, delay
),
211 "update_write_time_handler",
212 update_write_time_handler
, fsp
);
215 /****************************************************************************
217 ****************************************************************************/
219 ssize_t
write_file(struct smb_request
*req
,
225 write_cache
*wcp
= fsp
->wcp
;
226 ssize_t total_written
= 0;
229 if (fsp
->print_file
) {
233 if (!rap_to_pjobid(fsp
->rap_print_jobid
, sharename
, &jobid
)) {
234 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
235 (unsigned int)fsp
->rap_print_jobid
));
240 return print_job_write(SNUM(fsp
->conn
), jobid
, data
, pos
, n
);
243 if (!fsp
->can_write
) {
248 if (!fsp
->modified
) {
250 fsp
->modified
= True
;
252 if (SMB_VFS_FSTAT(fsp
, &st
) == 0) {
254 trigger_write_time_update(fsp
);
255 dosmode
= dos_mode(fsp
->conn
,fsp
->fsp_name
,&st
);
256 if ((lp_store_dos_attributes(SNUM(fsp
->conn
)) ||
257 MAP_ARCHIVE(fsp
->conn
)) &&
258 !IS_DOS_ARCHIVE(dosmode
)) {
259 file_set_dosmode(fsp
->conn
,fsp
->fsp_name
,
266 * If this is the first write and we have an exclusive oplock then setup
270 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && !wcp
) {
271 setup_write_cache(fsp
, st
.st_size
);
278 DO_PROFILE_INC(writecache_total_writes
);
279 if (!fsp
->oplock_type
) {
280 DO_PROFILE_INC(writecache_non_oplock_writes
);
285 * If this file is level II oplocked then we need
286 * to grab the shared memory lock and inform all
287 * other files with a level II lock that they need
288 * to flush their read caches. We keep the lock over
289 * the shared memory area whilst doing this.
292 release_level_2_oplocks_on_change(fsp
);
295 if (profile_p
&& profile_p
->writecache_total_writes
% 500 == 0) {
296 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
297 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
298 profile_p
->writecache_init_writes
,
299 profile_p
->writecache_abutted_writes
,
300 profile_p
->writecache_total_writes
,
301 profile_p
->writecache_non_oplock_writes
,
302 profile_p
->writecache_allocated_write_caches
,
303 profile_p
->writecache_num_write_caches
,
304 profile_p
->writecache_direct_writes
,
305 profile_p
->writecache_num_perfect_writes
,
306 profile_p
->writecache_read_hits
));
308 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
309 profile_p
->writecache_flushed_writes
[SEEK_FLUSH
],
310 profile_p
->writecache_flushed_writes
[READ_FLUSH
],
311 profile_p
->writecache_flushed_writes
[WRITE_FLUSH
],
312 profile_p
->writecache_flushed_writes
[READRAW_FLUSH
],
313 profile_p
->writecache_flushed_writes
[OPLOCK_RELEASE_FLUSH
],
314 profile_p
->writecache_flushed_writes
[CLOSE_FLUSH
],
315 profile_p
->writecache_flushed_writes
[SYNC_FLUSH
] ));
319 if (wcp
&& req
->unread_bytes
) {
320 /* If we're using receivefile don't
321 * deal with a write cache.
323 flush_write_cache(fsp
, WRITE_FLUSH
);
324 delete_write_cache(fsp
);
329 DO_PROFILE_INC(writecache_direct_writes
);
330 total_written
= real_write_file(req
, fsp
, data
, pos
, n
);
331 return total_written
;
334 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
335 fsp
->fsp_name
, fsp
->fh
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
337 fsp
->fh
->pos
= pos
+ n
;
340 * If we have active cache and it isn't contiguous then we flush.
341 * NOTE: There is a small problem with running out of disk ....
344 if (wcp
->data_size
) {
345 bool cache_flush_needed
= False
;
347 if ((pos
>= wcp
->offset
) && (pos
<= wcp
->offset
+ wcp
->data_size
)) {
349 /* ASCII art.... JRA.
351 +--------------+-----
352 | Cached data | Rest of allocated cache buffer....
353 +--------------+-----
355 +-------------------+
357 +-------------------+
362 * Start of write overlaps or abutts the existing data.
365 size_t data_used
= MIN((wcp
->alloc_size
- (pos
- wcp
->offset
)), n
);
367 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
370 * Update the current buffer size with the new data.
373 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
374 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
378 * Update the file size if changed.
381 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
382 if (wcp_file_size_change(fsp
) == -1) {
388 * If we used all the data then
395 cache_flush_needed
= True
;
398 * Move the start of data forward by the amount used,
399 * cut down the amount left by the same amount.
406 DO_PROFILE_INC(writecache_abutted_writes
);
407 total_written
= data_used
;
411 } else if ((pos
< wcp
->offset
) && (pos
+ n
> wcp
->offset
) &&
412 (pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
)) {
414 /* ASCII art.... JRA.
420 +-------------------+
422 +-------------------+
427 * End of write overlaps the existing data.
430 size_t data_used
= pos
+ n
- wcp
->offset
;
432 memcpy(wcp
->data
, data
+ n
- data_used
, data_used
);
435 * Update the current buffer size with the new data.
438 if(pos
+ n
> wcp
->offset
+ wcp
->data_size
) {
439 wcp
->data_size
= pos
+ n
- wcp
->offset
;
443 * Update the file size if changed.
446 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
447 if (wcp_file_size_change(fsp
) == -1) {
453 * We don't need to move the start of data, but we
454 * cut down the amount left by the amount used.
460 * We cannot have used all the data here.
463 cache_flush_needed
= True
;
465 DO_PROFILE_INC(writecache_abutted_writes
);
466 total_written
= data_used
;
470 } else if ( (pos
>= wcp
->file_size
) &&
471 (wcp
->offset
+ wcp
->data_size
== wcp
->file_size
) &&
472 (pos
> wcp
->offset
+ wcp
->data_size
) &&
473 (pos
< wcp
->offset
+ wcp
->alloc_size
) ) {
475 /* ASCII art.... JRA.
479 +---------------+---------------+
480 | Cached data | Cache buffer |
481 +---------------+---------------+
483 +-------------------+
485 +-------------------+
490 * Non-contiguous write part of which fits within
491 * the cache buffer and is extending the file
492 * and the cache contents reflect the current
493 * data up to the current end of the file.
498 if(pos
+ n
<= wcp
->offset
+ wcp
->alloc_size
) {
501 data_used
= wcp
->offset
+ wcp
->alloc_size
- pos
;
505 * Fill in the non-continuous area with zeros.
508 memset(wcp
->data
+ wcp
->data_size
, '\0',
509 pos
- (wcp
->offset
+ wcp
->data_size
) );
511 memcpy(wcp
->data
+ (pos
- wcp
->offset
), data
, data_used
);
514 * Update the current buffer size with the new data.
517 if(pos
+ data_used
> wcp
->offset
+ wcp
->data_size
) {
518 wcp
->data_size
= pos
+ data_used
- wcp
->offset
;
522 * Update the file size if changed.
525 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
526 if (wcp_file_size_change(fsp
) == -1) {
532 * If we used all the data then
539 cache_flush_needed
= True
;
543 * Move the start of data forward by the amount used,
544 * cut down the amount left by the same amount.
551 DO_PROFILE_INC(writecache_abutted_writes
);
552 total_written
= data_used
;
556 } else if ( (pos
>= wcp
->file_size
) &&
558 (wcp
->file_size
== wcp
->offset
+ wcp
->data_size
) &&
559 (pos
< wcp
->file_size
+ wcp
->alloc_size
)) {
565 +---------------+---------------+
566 | Cached data | Cache buffer |
567 +---------------+---------------+
569 |<------- allocated size ---------------->|
575 MS-Office seems to do this a lot to determine if there's enough
576 space on the filesystem to write a new file.
581 +-----------------------+--------+
582 | Zeroed Cached data | 1 Byte |
583 +-----------------------+--------+
586 flush_write_cache(fsp
, WRITE_FLUSH
);
587 wcp
->offset
= wcp
->file_size
;
588 wcp
->data_size
= pos
- wcp
->file_size
+ 1;
589 memset(wcp
->data
, '\0', wcp
->data_size
);
590 memcpy(wcp
->data
+ wcp
->data_size
-1, data
, 1);
593 * Update the file size if changed.
596 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
597 if (wcp_file_size_change(fsp
) == -1) {
606 /* ASCII art..... JRA.
610 +---------------+---------------+
611 | Cached data | Cache buffer |
612 +---------------+---------------+
614 +-------------------+
616 +-------------------+
620 +---------------+---------------+
621 | Cached data | Cache buffer |
622 +---------------+---------------+
624 +-------------------+
626 +-------------------+
630 +---------------+---------------+
631 | Cached data | Cache buffer |
632 +---------------+---------------+
634 +-----------------------------------------------------+
636 +-----------------------------------------------------+
641 * Write is bigger than buffer, or there is no overlap on the
645 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
646 len = %u\n",fsp
->fh
->fd
, (double)pos
, (unsigned int)n
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
649 * If write would fit in the cache, and is larger than
650 * the data already in the cache, flush the cache and
651 * preferentially copy the data new data into it. Otherwise
652 * just write the data directly.
655 if ( n
<= wcp
->alloc_size
&& n
> wcp
->data_size
) {
656 cache_flush_needed
= True
;
658 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
661 * If the write overlaps the entire cache, then
662 * discard the current contents of the cache.
663 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
666 if ((pos
<= wcp
->offset
) &&
667 (pos
+ n
>= wcp
->offset
+ wcp
->data_size
) ) {
668 DEBUG(9,("write_file: discarding overwritten write \
669 cache: fd = %d, off=%.0f, size=%u\n", fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
673 DO_PROFILE_INC(writecache_direct_writes
);
678 if (pos
+ ret
> wcp
->file_size
) {
679 wcp
->file_size
= pos
+ ret
;
689 if (cache_flush_needed
) {
690 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
691 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
692 write_path
, fsp
->fh
->fd
, (double)wcp
->file_size
, (double)pos
, (unsigned int)n
,
693 (double)wcp
->offset
, (unsigned int)wcp
->data_size
));
695 flush_write_cache(fsp
, WRITE_FLUSH
);
700 * If the write request is bigger than the cache
701 * size, write it all out.
704 if (n
> wcp
->alloc_size
) {
705 ssize_t ret
= real_write_file(NULL
,fsp
, data
, pos
, n
);
710 if (pos
+ ret
> wcp
->file_size
) {
711 wcp
->file_size
= pos
+ n
;
714 DO_PROFILE_INC(writecache_direct_writes
);
715 return total_written
+ n
;
719 * If there's any data left, cache it.
724 if (wcp
->data_size
) {
725 DO_PROFILE_INC(writecache_abutted_writes
);
727 DO_PROFILE_INC(writecache_init_writes
);
730 memcpy(wcp
->data
+wcp
->data_size
, data
, n
);
731 if (wcp
->data_size
== 0) {
733 DO_PROFILE_INC(writecache_num_write_caches
);
738 * Update the file size if changed.
741 if (wcp
->offset
+ wcp
->data_size
> wcp
->file_size
) {
742 if (wcp_file_size_change(fsp
) == -1) {
746 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
747 (double)wcp
->offset
, (unsigned int)wcp
->data_size
, (unsigned int)n
));
750 return total_written
; /* .... that's a write :) */
753 return total_written
;
756 /****************************************************************************
757 Delete the write cache structure.
758 ****************************************************************************/
760 void delete_write_cache(files_struct
*fsp
)
768 if(!(wcp
= fsp
->wcp
)) {
772 DO_PROFILE_DEC(writecache_allocated_write_caches
);
773 allocated_write_caches
--;
775 SMB_ASSERT(wcp
->data_size
== 0);
777 SAFE_FREE(wcp
->data
);
780 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp
->fsp_name
));
783 /****************************************************************************
784 Setup the write cache structure.
785 ****************************************************************************/
787 static bool setup_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
789 ssize_t alloc_size
= lp_write_cache_size(SNUM(fsp
->conn
));
792 if (allocated_write_caches
>= MAX_WRITE_CACHES
) {
796 if(alloc_size
== 0 || fsp
->wcp
) {
800 if((wcp
= SMB_MALLOC_P(write_cache
)) == NULL
) {
801 DEBUG(0,("setup_write_cache: malloc fail.\n"));
805 wcp
->file_size
= file_size
;
807 wcp
->alloc_size
= alloc_size
;
809 if((wcp
->data
= (char *)SMB_MALLOC(wcp
->alloc_size
)) == NULL
) {
810 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
811 (unsigned int)wcp
->alloc_size
));
816 memset(wcp
->data
, '\0', wcp
->alloc_size
);
819 DO_PROFILE_INC(writecache_allocated_write_caches
);
820 allocated_write_caches
++;
822 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
823 fsp
->fsp_name
, (unsigned long)wcp
->alloc_size
));
828 /****************************************************************************
829 Cope with a size change.
830 ****************************************************************************/
832 void set_filelen_write_cache(files_struct
*fsp
, SMB_OFF_T file_size
)
835 /* The cache *must* have been flushed before we do this. */
836 if (fsp
->wcp
->data_size
!= 0) {
838 asprintf(&msg
, "set_filelen_write_cache: size change "
839 "on file %s with write cache size = %lu\n",
841 (unsigned long)fsp
->wcp
->data_size
);
844 fsp
->wcp
->file_size
= file_size
;
848 /*******************************************************************
849 Flush a write cache struct to disk.
850 ********************************************************************/
852 ssize_t
flush_write_cache(files_struct
*fsp
, enum flush_reason_enum reason
)
854 write_cache
*wcp
= fsp
->wcp
;
858 if(!wcp
|| !wcp
->data_size
) {
862 data_size
= wcp
->data_size
;
865 DO_PROFILE_DEC_INC(writecache_num_write_caches
,writecache_flushed_writes
[reason
]);
867 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
868 fsp
->fh
->fd
, (double)wcp
->offset
, (unsigned int)data_size
));
871 if(data_size
== wcp
->alloc_size
) {
872 DO_PROFILE_INC(writecache_num_perfect_writes
);
876 ret
= real_write_file(NULL
, fsp
, wcp
->data
, wcp
->offset
, data_size
);
879 * Ensure file size if kept up to date if write extends file.
882 if ((ret
!= -1) && (wcp
->offset
+ ret
> wcp
->file_size
)) {
883 wcp
->file_size
= wcp
->offset
+ ret
;
889 /*******************************************************************
891 ********************************************************************/
893 NTSTATUS
sync_file(connection_struct
*conn
, files_struct
*fsp
, bool write_through
)
895 if (fsp
->fh
->fd
== -1)
896 return NT_STATUS_INVALID_HANDLE
;
898 if (lp_strict_sync(SNUM(conn
)) &&
899 (lp_syncalways(SNUM(conn
)) || write_through
)) {
900 int ret
= flush_write_cache(fsp
, SYNC_FLUSH
);
902 return map_nt_error_from_unix(errno
);
904 ret
= SMB_VFS_FSYNC(fsp
);
906 return map_nt_error_from_unix(errno
);
912 /************************************************************
913 Perform a stat whether a valid fd or not.
914 ************************************************************/
916 int fsp_stat(files_struct
*fsp
, SMB_STRUCT_STAT
*pst
)
918 if (fsp
->fh
->fd
== -1) {
919 return SMB_VFS_STAT(fsp
->conn
, fsp
->fsp_name
, pst
);
921 return SMB_VFS_FSTAT(fsp
, pst
);