4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc.
27 * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek
30 /* Portions Copyright 2007 Jeremy Teo */
31 /* Portions Copyright 2010 Robert Milkowski */
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/sysmacros.h>
38 #include <sys/uio_impl.h>
42 #include <sys/cmn_err.h>
43 #include <sys/errno.h>
44 #include <sys/zfs_dir.h>
45 #include <sys/zfs_acl.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/fs/zfs.h>
49 #include <sys/dmu_objset.h>
53 #include <sys/policy.h>
54 #include <sys/zfeature.h>
55 #include <sys/zfs_vnops.h>
56 #include <sys/zfs_quota.h>
57 #include <sys/zfs_vfsops.h>
58 #include <sys/zfs_znode.h>
62 zfs_fsync(znode_t
*zp
, int syncflag
, cred_t
*cr
)
65 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
67 if (zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
68 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
70 atomic_inc_32(&zp
->z_sync_writes_cnt
);
71 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
72 atomic_dec_32(&zp
->z_sync_writes_cnt
);
73 zfs_exit(zfsvfs
, FTAG
);
79 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
81 * Lseek support for finding holes (cmd == SEEK_HOLE) and
82 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
85 zfs_holey_common(znode_t
*zp
, ulong_t cmd
, loff_t
*off
)
87 zfs_locked_range_t
*lr
;
88 uint64_t noff
= (uint64_t)*off
; /* new offset */
94 if (noff
>= file_sz
) {
95 return (SET_ERROR(ENXIO
));
98 if (cmd
== F_SEEK_HOLE
)
103 /* Flush any mmap()'d data to disk */
104 if (zn_has_cached_data(zp
, 0, file_sz
- 1))
105 zn_flush_cached_data(zp
, B_FALSE
);
107 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, 0, UINT64_MAX
, RL_READER
);
108 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
109 zfs_rangelock_exit(lr
);
112 return (SET_ERROR(ENXIO
));
114 /* File was dirty, so fall back to using generic logic */
115 if (error
== EBUSY
) {
123 * We could find a hole that begins after the logical end-of-file,
124 * because dmu_offset_next() only works on whole blocks. If the
125 * EOF falls mid-block, then indicate that the "virtual hole"
126 * at the end of the file begins at the logical EOF, rather than
127 * at the end of the last block.
129 if (noff
> file_sz
) {
141 zfs_holey(znode_t
*zp
, ulong_t cmd
, loff_t
*off
)
143 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
146 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
149 error
= zfs_holey_common(zp
, cmd
, off
);
151 zfs_exit(zfsvfs
, FTAG
);
154 #endif /* SEEK_HOLE && SEEK_DATA */
157 zfs_access(znode_t
*zp
, int mode
, int flag
, cred_t
*cr
)
159 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
162 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
165 if (flag
& V_ACE_MASK
)
166 #if defined(__linux__)
167 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
,
170 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
,
174 #if defined(__linux__)
175 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
, zfs_init_idmap
);
177 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
, NULL
);
180 zfs_exit(zfsvfs
, FTAG
);
184 static uint64_t zfs_vnops_read_chunk_size
= 1024 * 1024; /* Tunable */
187 * Read bytes from specified file into supplied buffer.
189 * IN: zp - inode of file to be read from.
190 * uio - structure supplying read location, range info,
192 * ioflag - O_SYNC flags; used to provide FRSYNC semantics.
193 * O_DIRECT flag; used to bypass page cache.
194 * cr - credentials of caller.
196 * OUT: uio - updated offset and range, buffer filled.
198 * RETURN: 0 on success, error code on failure.
201 * inode - atime updated if byte count > 0
204 zfs_read(struct znode
*zp
, zfs_uio_t
*uio
, int ioflag
, cred_t
*cr
)
208 boolean_t frsync
= B_FALSE
;
210 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
211 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
214 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
215 zfs_exit(zfsvfs
, FTAG
);
216 return (SET_ERROR(EACCES
));
219 /* We don't copy out anything useful for directories. */
220 if (Z_ISDIR(ZTOTYPE(zp
))) {
221 zfs_exit(zfsvfs
, FTAG
);
222 return (SET_ERROR(EISDIR
));
226 * Validate file offset
228 if (zfs_uio_offset(uio
) < (offset_t
)0) {
229 zfs_exit(zfsvfs
, FTAG
);
230 return (SET_ERROR(EINVAL
));
234 * Fasttrack empty reads
236 if (zfs_uio_resid(uio
) == 0) {
237 zfs_exit(zfsvfs
, FTAG
);
243 * If we're in FRSYNC mode, sync out this znode before reading it.
244 * Only do this for non-snapshots.
246 * Some platforms do not support FRSYNC and instead map it
247 * to O_SYNC, which results in unnecessary calls to zil_commit. We
248 * only honor FRSYNC requests on platforms which support it.
250 frsync
= !!(ioflag
& FRSYNC
);
253 (frsync
|| zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
))
254 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
257 * Lock the range against changes.
259 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
260 zfs_uio_offset(uio
), zfs_uio_resid(uio
), RL_READER
);
263 * If we are reading past end-of-file we can skip
264 * to the end; but we might still need to set atime.
266 if (zfs_uio_offset(uio
) >= zp
->z_size
) {
271 ASSERT(zfs_uio_offset(uio
) < zp
->z_size
);
272 #if defined(__linux__)
273 ssize_t start_offset
= zfs_uio_offset(uio
);
275 ssize_t n
= MIN(zfs_uio_resid(uio
), zp
->z_size
- zfs_uio_offset(uio
));
276 ssize_t start_resid
= n
;
279 ssize_t nbytes
= MIN(n
, zfs_vnops_read_chunk_size
-
280 P2PHASE(zfs_uio_offset(uio
), zfs_vnops_read_chunk_size
));
282 if (zfs_uio_segflg(uio
) == UIO_NOCOPY
)
283 error
= mappedread_sf(zp
, nbytes
, uio
);
286 if (zn_has_cached_data(zp
, zfs_uio_offset(uio
),
287 zfs_uio_offset(uio
) + nbytes
- 1) && !(ioflag
& O_DIRECT
)) {
288 error
= mappedread(zp
, nbytes
, uio
);
290 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
295 /* convert checksum errors into IO errors */
297 error
= SET_ERROR(EIO
);
299 #if defined(__linux__)
301 * if we actually read some bytes, bubbling EFAULT
302 * up to become EAGAIN isn't what we want here...
304 * ...on Linux, at least. On FBSD, doing this breaks.
306 if (error
== EFAULT
&&
307 (zfs_uio_offset(uio
) - start_offset
) != 0)
316 int64_t nread
= start_resid
- n
;
317 dataset_kstats_update_read_kstats(&zfsvfs
->z_kstat
, nread
);
318 task_io_account_read(nread
);
320 zfs_rangelock_exit(lr
);
322 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
323 zfs_exit(zfsvfs
, FTAG
);
328 zfs_clear_setid_bits_if_necessary(zfsvfs_t
*zfsvfs
, znode_t
*zp
, cred_t
*cr
,
329 uint64_t *clear_setid_bits_txgp
, dmu_tx_t
*tx
)
331 zilog_t
*zilog
= zfsvfs
->z_log
;
332 const uint64_t uid
= KUID_TO_SUID(ZTOUID(zp
));
334 ASSERT(clear_setid_bits_txgp
!= NULL
);
338 * Clear Set-UID/Set-GID bits on successful write if not
339 * privileged and at least one of the execute bits is set.
341 * It would be nice to do this after all writes have
342 * been done, but that would still expose the ISUID/ISGID
343 * to another app after the partial write is committed.
345 * Note: we don't call zfs_fuid_map_id() here because
346 * user 0 is not an ephemeral uid.
348 mutex_enter(&zp
->z_acl_lock
);
349 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) | (S_IXUSR
>> 6))) != 0 &&
350 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
351 secpolicy_vnode_setid_retain(zp
, cr
,
352 ((zp
->z_mode
& S_ISUID
) != 0 && uid
== 0)) != 0) {
355 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
356 newmode
= zp
->z_mode
;
357 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
),
358 (void *)&newmode
, sizeof (uint64_t), tx
);
360 mutex_exit(&zp
->z_acl_lock
);
363 * Make sure SUID/SGID bits will be removed when we replay the
364 * log. If the setid bits are keep coming back, don't log more
365 * than one TX_SETATTR per transaction group.
367 if (*clear_setid_bits_txgp
!= dmu_tx_get_txg(tx
)) {
370 va
.va_mask
= ATTR_MODE
;
371 va
.va_nodeid
= zp
->z_id
;
372 va
.va_mode
= newmode
;
373 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, &va
,
375 *clear_setid_bits_txgp
= dmu_tx_get_txg(tx
);
378 mutex_exit(&zp
->z_acl_lock
);
383 * Write the bytes to a file.
385 * IN: zp - znode of file to be written to.
386 * uio - structure supplying write location, range info,
388 * ioflag - O_APPEND flag set if in append mode.
389 * O_DIRECT flag; used to bypass page cache.
390 * cr - credentials of caller.
392 * OUT: uio - updated offset and range.
394 * RETURN: 0 if success
395 * error code if failure
398 * ip - ctime|mtime updated if byte count > 0
401 zfs_write(znode_t
*zp
, zfs_uio_t
*uio
, int ioflag
, cred_t
*cr
)
403 int error
= 0, error1
;
404 ssize_t start_resid
= zfs_uio_resid(uio
);
405 uint64_t clear_setid_bits_txg
= 0;
408 * Fasttrack empty write
410 ssize_t n
= start_resid
;
414 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
415 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
418 sa_bulk_attr_t bulk
[4];
420 uint64_t mtime
[2], ctime
[2];
421 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
422 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
423 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
425 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
429 * Callers might not be able to detect properly that we are read-only,
430 * so check it explicitly here.
432 if (zfs_is_readonly(zfsvfs
)) {
433 zfs_exit(zfsvfs
, FTAG
);
434 return (SET_ERROR(EROFS
));
438 * If immutable or not appending then return EPERM.
439 * Intentionally allow ZFS_READONLY through here.
440 * See zfs_zaccess_common()
442 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) ||
443 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& O_APPEND
) &&
444 (zfs_uio_offset(uio
) < zp
->z_size
))) {
445 zfs_exit(zfsvfs
, FTAG
);
446 return (SET_ERROR(EPERM
));
450 * Validate file offset
452 offset_t woff
= ioflag
& O_APPEND
? zp
->z_size
: zfs_uio_offset(uio
);
454 zfs_exit(zfsvfs
, FTAG
);
455 return (SET_ERROR(EINVAL
));
459 * Pre-fault the pages to ensure slow (eg NFS) pages
462 ssize_t pfbytes
= MIN(n
, DMU_MAX_ACCESS
>> 1);
463 if (zfs_uio_prefaultpages(pfbytes
, uio
)) {
464 zfs_exit(zfsvfs
, FTAG
);
465 return (SET_ERROR(EFAULT
));
469 * If in append mode, set the io offset pointer to eof.
471 zfs_locked_range_t
*lr
;
472 if (ioflag
& O_APPEND
) {
474 * Obtain an appending range lock to guarantee file append
475 * semantics. We reset the write offset once we have the lock.
477 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, 0, n
, RL_APPEND
);
478 woff
= lr
->lr_offset
;
479 if (lr
->lr_length
== UINT64_MAX
) {
481 * We overlocked the file because this write will cause
482 * the file block size to increase.
483 * Note that zp_size cannot change with this lock held.
487 zfs_uio_setoffset(uio
, woff
);
490 * Note that if the file block size will change as a result of
491 * this write, then this range lock will lock the entire file
492 * so that we can re-write the block safely.
494 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, woff
, n
, RL_WRITER
);
497 if (zn_rlimit_fsize_uio(zp
, uio
)) {
498 zfs_rangelock_exit(lr
);
499 zfs_exit(zfsvfs
, FTAG
);
500 return (SET_ERROR(EFBIG
));
503 const rlim64_t limit
= MAXOFFSET_T
;
506 zfs_rangelock_exit(lr
);
507 zfs_exit(zfsvfs
, FTAG
);
508 return (SET_ERROR(EFBIG
));
511 if (n
> limit
- woff
)
514 uint64_t end_size
= MAX(zp
->z_size
, woff
+ n
);
515 zilog_t
*zilog
= zfsvfs
->z_log
;
516 boolean_t commit
= (ioflag
& (O_SYNC
| O_DSYNC
)) ||
517 (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
);
519 const uint64_t uid
= KUID_TO_SUID(ZTOUID(zp
));
520 const uint64_t gid
= KGID_TO_SGID(ZTOGID(zp
));
521 const uint64_t projid
= zp
->z_projid
;
524 * Write the file in reasonable size chunks. Each chunk is written
525 * in a separate transaction; this keeps the intent log records small
526 * and allows us to do more fine-grained space accounting.
529 woff
= zfs_uio_offset(uio
);
531 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
, uid
) ||
532 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
, gid
) ||
533 (projid
!= ZFS_DEFAULT_PROJID
&&
534 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
536 error
= SET_ERROR(EDQUOT
);
541 if (lr
->lr_length
== UINT64_MAX
&& zp
->z_size
<= zp
->z_blksz
) {
542 if (zp
->z_blksz
> zfsvfs
->z_max_blksz
&&
543 !ISP2(zp
->z_blksz
)) {
545 * File's blocksize is already larger than the
546 * "recordsize" property. Only let it grow to
547 * the next power of 2.
549 blksz
= 1 << highbit64(zp
->z_blksz
);
551 blksz
= zfsvfs
->z_max_blksz
;
553 blksz
= MIN(blksz
, P2ROUNDUP(end_size
,
555 blksz
= MAX(blksz
, zp
->z_blksz
);
560 arc_buf_t
*abuf
= NULL
;
562 if (n
>= blksz
&& woff
>= zp
->z_size
&&
563 P2PHASE(woff
, blksz
) == 0 &&
564 (blksz
>= SPA_OLD_MAXBLOCKSIZE
|| n
< 4 * blksz
)) {
566 * This write covers a full block. "Borrow" a buffer
567 * from the dmu so that we can fill it before we enter
568 * a transaction. This avoids the possibility of
569 * holding up the transaction if the data copy hangs
570 * up on a pagefault (e.g., from an NFS server mapping).
572 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
574 ASSERT(abuf
!= NULL
);
575 ASSERT(arc_buf_size(abuf
) == blksz
);
576 if ((error
= zfs_uiocopy(abuf
->b_data
, blksz
,
577 UIO_WRITE
, uio
, &nbytes
))) {
578 dmu_return_arcbuf(abuf
);
581 ASSERT3S(nbytes
, ==, blksz
);
583 nbytes
= MIN(n
, (DMU_MAX_ACCESS
>> 1) -
584 P2PHASE(woff
, blksz
));
585 if (pfbytes
< nbytes
) {
586 if (zfs_uio_prefaultpages(nbytes
, uio
)) {
587 error
= SET_ERROR(EFAULT
);
595 * Start a transaction.
597 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
598 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
599 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)sa_get_db(zp
->z_sa_hdl
);
601 dmu_tx_hold_write_by_dnode(tx
, DB_DNODE(db
), woff
, nbytes
);
603 zfs_sa_upgrade_txholds(tx
, zp
);
604 error
= dmu_tx_assign(tx
, TXG_WAIT
);
608 dmu_return_arcbuf(abuf
);
613 * NB: We must call zfs_clear_setid_bits_if_necessary before
614 * committing the transaction!
618 * If rangelock_enter() over-locked we grow the blocksize
619 * and then reduce the lock range. This will only happen
620 * on the first iteration since rangelock_reduce() will
621 * shrink down lr_length to the appropriate size.
623 if (lr
->lr_length
== UINT64_MAX
) {
624 zfs_grow_blocksize(zp
, blksz
, tx
);
625 zfs_rangelock_reduce(lr
, woff
, n
);
630 tx_bytes
= zfs_uio_resid(uio
);
631 zfs_uio_fault_disable(uio
, B_TRUE
);
632 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
634 zfs_uio_fault_disable(uio
, B_FALSE
);
636 if (error
== EFAULT
) {
637 zfs_clear_setid_bits_if_necessary(zfsvfs
, zp
,
638 cr
, &clear_setid_bits_txg
, tx
);
641 * Account for partial writes before
642 * continuing the loop.
643 * Update needs to occur before the next
644 * zfs_uio_prefaultpages, or prefaultpages may
645 * error, and we may break the loop early.
647 n
-= tx_bytes
- zfs_uio_resid(uio
);
648 pfbytes
-= tx_bytes
- zfs_uio_resid(uio
);
653 * On FreeBSD, EFAULT should be propagated back to the
654 * VFS, which will handle faulting and will retry.
656 if (error
!= 0 && error
!= EFAULT
) {
657 zfs_clear_setid_bits_if_necessary(zfsvfs
, zp
,
658 cr
, &clear_setid_bits_txg
, tx
);
662 tx_bytes
-= zfs_uio_resid(uio
);
665 * Thus, we're writing a full block at a block-aligned
666 * offset and extending the file past EOF.
668 * dmu_assign_arcbuf_by_dbuf() will directly assign the
669 * arc buffer to a dbuf.
671 error
= dmu_assign_arcbuf_by_dbuf(
672 sa_get_db(zp
->z_sa_hdl
), woff
, abuf
, tx
);
675 * XXX This might not be necessary if
676 * dmu_assign_arcbuf_by_dbuf is guaranteed
679 zfs_clear_setid_bits_if_necessary(zfsvfs
, zp
,
680 cr
, &clear_setid_bits_txg
, tx
);
681 dmu_return_arcbuf(abuf
);
685 ASSERT3S(nbytes
, <=, zfs_uio_resid(uio
));
686 zfs_uioskip(uio
, nbytes
);
690 zn_has_cached_data(zp
, woff
, woff
+ tx_bytes
- 1) &&
691 !(ioflag
& O_DIRECT
)) {
692 update_pages(zp
, woff
, tx_bytes
, zfsvfs
->z_os
);
696 * If we made no progress, we're done. If we made even
697 * partial progress, update the znode and ZIL accordingly.
700 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
701 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
707 zfs_clear_setid_bits_if_necessary(zfsvfs
, zp
, cr
,
708 &clear_setid_bits_txg
, tx
);
710 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
713 * Update the file size (zp_size) if it has changed;
714 * account for possible concurrent updates.
716 while ((end_size
= zp
->z_size
) < zfs_uio_offset(uio
)) {
717 (void) atomic_cas_64(&zp
->z_size
, end_size
,
718 zfs_uio_offset(uio
));
719 ASSERT(error
== 0 || error
== EFAULT
);
722 * If we are replaying and eof is non zero then force
723 * the file size to the specified eof. Note, there's no
724 * concurrency during replay.
726 if (zfsvfs
->z_replay
&& zfsvfs
->z_replay_eof
!= 0)
727 zp
->z_size
= zfsvfs
->z_replay_eof
;
729 error1
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
731 /* Avoid clobbering EFAULT. */
735 * NB: During replay, the TX_SETATTR record logged by
736 * zfs_clear_setid_bits_if_necessary must precede any of
737 * the TX_WRITE records logged here.
739 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, commit
,
746 ASSERT3S(tx_bytes
, ==, nbytes
);
751 zfs_znode_update_vfs(zp
);
752 zfs_rangelock_exit(lr
);
755 * If we're in replay mode, or we made no progress, or the
756 * uio data is inaccessible return an error. Otherwise, it's
757 * at least a partial write, so it's successful.
759 if (zfsvfs
->z_replay
|| zfs_uio_resid(uio
) == start_resid
||
761 zfs_exit(zfsvfs
, FTAG
);
766 zil_commit(zilog
, zp
->z_id
);
768 const int64_t nwritten
= start_resid
- zfs_uio_resid(uio
);
769 dataset_kstats_update_write_kstats(&zfsvfs
->z_kstat
, nwritten
);
770 task_io_account_write(nwritten
);
772 zfs_exit(zfsvfs
, FTAG
);
777 zfs_getsecattr(znode_t
*zp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
779 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
781 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
783 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
785 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
786 zfs_exit(zfsvfs
, FTAG
);
792 zfs_setsecattr(znode_t
*zp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
794 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
796 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
797 zilog_t
*zilog
= zfsvfs
->z_log
;
799 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
802 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
804 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
805 zil_commit(zilog
, 0);
807 zfs_exit(zfsvfs
, FTAG
);
812 static int zil_fault_io
= 0;
815 static void zfs_get_done(zgd_t
*zgd
, int error
);
818 * Get data to generate a TX_WRITE intent log record.
821 zfs_get_data(void *arg
, uint64_t gen
, lr_write_t
*lr
, char *buf
,
822 struct lwb
*lwb
, zio_t
*zio
)
824 zfsvfs_t
*zfsvfs
= arg
;
825 objset_t
*os
= zfsvfs
->z_os
;
827 uint64_t object
= lr
->lr_foid
;
828 uint64_t offset
= lr
->lr_offset
;
829 uint64_t size
= lr
->lr_length
;
835 ASSERT3P(lwb
, !=, NULL
);
836 ASSERT3U(size
, !=, 0);
839 * Nothing to do if the file has been removed
841 if (zfs_zget(zfsvfs
, object
, &zp
) != 0)
842 return (SET_ERROR(ENOENT
));
843 if (zp
->z_unlinked
) {
845 * Release the vnode asynchronously as we currently have the
846 * txg stopped from syncing.
849 return (SET_ERROR(ENOENT
));
851 /* check if generation number matches */
852 if (sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
), &zp_gen
,
853 sizeof (zp_gen
)) != 0) {
855 return (SET_ERROR(EIO
));
859 return (SET_ERROR(ENOENT
));
862 zgd
= kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
864 zgd
->zgd_private
= zp
;
867 * Write records come in two flavors: immediate and indirect.
868 * For small writes it's cheaper to store the data with the
869 * log record (immediate); for large writes it's cheaper to
870 * sync the data and get a pointer to it (indirect) so that
871 * we don't have to write the data twice.
873 if (buf
!= NULL
) { /* immediate write */
874 zgd
->zgd_lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
875 offset
, size
, RL_READER
);
876 /* test for truncation needs to be done while range locked */
877 if (offset
>= zp
->z_size
) {
878 error
= SET_ERROR(ENOENT
);
880 error
= dmu_read(os
, object
, offset
, size
, buf
,
881 DMU_READ_NO_PREFETCH
);
883 ASSERT(error
== 0 || error
== ENOENT
);
884 } else { /* indirect write */
885 ASSERT3P(zio
, !=, NULL
);
887 * Have to lock the whole block to ensure when it's
888 * written out and its checksum is being calculated
889 * that no one can change the data. We need to re-check
890 * blocksize after we get the lock in case it's changed!
895 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
897 zgd
->zgd_lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
898 offset
, size
, RL_READER
);
899 if (zp
->z_blksz
== size
)
902 zfs_rangelock_exit(zgd
->zgd_lr
);
904 /* test for truncation needs to be done while range locked */
905 if (lr
->lr_offset
>= zp
->z_size
)
906 error
= SET_ERROR(ENOENT
);
909 error
= SET_ERROR(EIO
);
914 error
= dmu_buf_hold_noread(os
, object
, offset
, zgd
,
918 blkptr_t
*bp
= &lr
->lr_blkptr
;
923 ASSERT(db
->db_offset
== offset
);
924 ASSERT(db
->db_size
== size
);
926 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
928 ASSERT(error
|| lr
->lr_length
<= size
);
931 * On success, we need to wait for the write I/O
932 * initiated by dmu_sync() to complete before we can
933 * release this dbuf. We will finish everything up
934 * in the zfs_get_done() callback.
939 if (error
== EALREADY
) {
940 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
942 * TX_WRITE2 relies on the data previously
943 * written by the TX_WRITE that caused
944 * EALREADY. We zero out the BP because
945 * it is the old, currently-on-disk BP.
954 zfs_get_done(zgd
, error
);
961 zfs_get_done(zgd_t
*zgd
, int error
)
964 znode_t
*zp
= zgd
->zgd_private
;
967 dmu_buf_rele(zgd
->zgd_db
, zgd
);
969 zfs_rangelock_exit(zgd
->zgd_lr
);
972 * Release the vnode asynchronously as we currently have the
973 * txg stopped from syncing.
977 kmem_free(zgd
, sizeof (zgd_t
));
981 zfs_enter_two(zfsvfs_t
*zfsvfs1
, zfsvfs_t
*zfsvfs2
, const char *tag
)
985 /* Swap. Not sure if the order of zfs_enter()s is important. */
986 if (zfsvfs1
> zfsvfs2
) {
994 error
= zfs_enter(zfsvfs1
, tag
);
997 if (zfsvfs1
!= zfsvfs2
) {
998 error
= zfs_enter(zfsvfs2
, tag
);
1000 zfs_exit(zfsvfs1
, tag
);
1009 zfs_exit_two(zfsvfs_t
*zfsvfs1
, zfsvfs_t
*zfsvfs2
, const char *tag
)
1012 zfs_exit(zfsvfs1
, tag
);
1013 if (zfsvfs1
!= zfsvfs2
)
1014 zfs_exit(zfsvfs2
, tag
);
1018 * We split each clone request in chunks that can fit into a single ZIL
1019 * log entry. Each ZIL log entry can fit 130816 bytes for a block cloning
1020 * operation (see zil_max_log_data() and zfs_log_clone_range()). This gives
1021 * us room for storing 1022 block pointers.
1023 * On success, the function return the number of bytes copied in *lenp.
1024 * Note, it doesn't return how much bytes are left to be copied.
1025 * On errors which are caused by any file system limitations or
1026 * brt limitations `EINVAL` is returned. In the most cases a user
1027 * requested bad parameters, it could be possible to clone the file but
1028 * some parameters don't match the requirements.
1031 zfs_clone_range(znode_t
*inzp
, uint64_t *inoffp
, znode_t
*outzp
,
1032 uint64_t *outoffp
, uint64_t *lenp
, cred_t
*cr
)
1034 zfsvfs_t
*inzfsvfs
, *outzfsvfs
;
1035 objset_t
*inos
, *outos
;
1036 zfs_locked_range_t
*inlr
, *outlr
;
1040 uint64_t inoff
, outoff
, len
, done
;
1041 uint64_t outsize
, size
;
1044 sa_bulk_attr_t bulk
[3];
1045 uint64_t mtime
[2], ctime
[2];
1046 uint64_t uid
, gid
, projid
;
1048 size_t maxblocks
, nbps
;
1050 uint64_t clear_setid_bits_txg
= 0;
1057 inzfsvfs
= ZTOZSB(inzp
);
1058 outzfsvfs
= ZTOZSB(outzp
);
1061 * We need to call zfs_enter() potentially on two different datasets,
1062 * so we need a dedicated function for that.
1064 error
= zfs_enter_two(inzfsvfs
, outzfsvfs
, FTAG
);
1068 inos
= inzfsvfs
->z_os
;
1069 outos
= outzfsvfs
->z_os
;
1072 * Both source and destination have to belong to the same storage pool.
1074 if (dmu_objset_spa(inos
) != dmu_objset_spa(outos
)) {
1075 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1076 return (SET_ERROR(EXDEV
));
1080 * outos and inos belongs to the same storage pool.
1081 * see a few lines above, only one check.
1083 if (!spa_feature_is_enabled(dmu_objset_spa(outos
),
1084 SPA_FEATURE_BLOCK_CLONING
)) {
1085 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1086 return (SET_ERROR(EOPNOTSUPP
));
1089 ASSERT(!outzfsvfs
->z_replay
);
1092 * Block cloning from an unencrypted dataset into an encrypted
1093 * dataset and vice versa is not supported.
1095 if (inos
->os_encrypted
!= outos
->os_encrypted
) {
1096 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1097 return (SET_ERROR(EXDEV
));
1100 error
= zfs_verify_zp(inzp
);
1102 error
= zfs_verify_zp(outzp
);
1104 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1109 * We don't copy source file's flags that's why we don't allow to clone
1110 * files that are in quarantine.
1112 if (inzp
->z_pflags
& ZFS_AV_QUARANTINED
) {
1113 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1114 return (SET_ERROR(EACCES
));
1117 if (inoff
>= inzp
->z_size
) {
1119 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1122 if (len
> inzp
->z_size
- inoff
) {
1123 len
= inzp
->z_size
- inoff
;
1127 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1132 * Callers might not be able to detect properly that we are read-only,
1133 * so check it explicitly here.
1135 if (zfs_is_readonly(outzfsvfs
)) {
1136 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1137 return (SET_ERROR(EROFS
));
1141 * If immutable or not appending then return EPERM.
1142 * Intentionally allow ZFS_READONLY through here.
1143 * See zfs_zaccess_common()
1145 if ((outzp
->z_pflags
& ZFS_IMMUTABLE
) != 0) {
1146 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1147 return (SET_ERROR(EPERM
));
1151 * No overlapping if we are cloning within the same file.
1153 if (inzp
== outzp
) {
1154 if (inoff
< outoff
+ len
&& outoff
< inoff
+ len
) {
1155 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1156 return (SET_ERROR(EINVAL
));
1161 * Maintain predictable lock order.
1163 if (inzp
< outzp
|| (inzp
== outzp
&& inoff
< outoff
)) {
1164 inlr
= zfs_rangelock_enter(&inzp
->z_rangelock
, inoff
, len
,
1166 outlr
= zfs_rangelock_enter(&outzp
->z_rangelock
, outoff
, len
,
1169 outlr
= zfs_rangelock_enter(&outzp
->z_rangelock
, outoff
, len
,
1171 inlr
= zfs_rangelock_enter(&inzp
->z_rangelock
, inoff
, len
,
1175 inblksz
= inzp
->z_blksz
;
1178 * We cannot clone into files with different block size if we can't
1179 * grow it (block size is already bigger or more than one block).
1181 if (inblksz
!= outzp
->z_blksz
&& (outzp
->z_size
> outzp
->z_blksz
||
1182 outzp
->z_size
> inblksz
)) {
1183 error
= SET_ERROR(EINVAL
);
1188 * Block size must be power-of-2 if destination offset != 0.
1189 * There can be no multiple blocks of non-power-of-2 size.
1191 if (outoff
!= 0 && !ISP2(inblksz
)) {
1192 error
= SET_ERROR(EINVAL
);
1197 * Offsets and len must be at block boundries.
1199 if ((inoff
% inblksz
) != 0 || (outoff
% inblksz
) != 0) {
1200 error
= SET_ERROR(EINVAL
);
1204 * Length must be multipe of blksz, except for the end of the file.
1206 if ((len
% inblksz
) != 0 &&
1207 (len
< inzp
->z_size
- inoff
|| len
< outzp
->z_size
- outoff
)) {
1208 error
= SET_ERROR(EINVAL
);
1213 * If we are copying only one block and it is smaller than recordsize
1214 * property, do not allow destination to grow beyond one block if it
1215 * is not there yet. Otherwise the destination will get stuck with
1216 * that block size forever, that can be as small as 512 bytes, no
1217 * matter how big the destination grow later.
1219 if (len
<= inblksz
&& inblksz
< outzfsvfs
->z_max_blksz
&&
1220 outzp
->z_size
<= inblksz
&& outoff
+ len
> inblksz
) {
1221 error
= SET_ERROR(EINVAL
);
1225 error
= zn_rlimit_fsize(outoff
+ len
);
1230 if (inoff
>= MAXOFFSET_T
|| outoff
>= MAXOFFSET_T
) {
1231 error
= SET_ERROR(EFBIG
);
1235 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(outzfsvfs
), NULL
,
1237 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(outzfsvfs
), NULL
,
1239 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(outzfsvfs
), NULL
,
1242 zilog
= outzfsvfs
->z_log
;
1243 maxblocks
= zil_max_log_data(zilog
, sizeof (lr_clone_range_t
)) /
1246 uid
= KUID_TO_SUID(ZTOUID(outzp
));
1247 gid
= KGID_TO_SGID(ZTOGID(outzp
));
1248 projid
= outzp
->z_projid
;
1250 bps
= vmem_alloc(sizeof (bps
[0]) * maxblocks
, KM_SLEEP
);
1253 * Clone the file in reasonable size chunks. Each chunk is cloned
1254 * in a separate transaction; this keeps the intent log records small
1255 * and allows us to do more fine-grained space accounting.
1258 size
= MIN(inblksz
* maxblocks
, len
);
1260 if (zfs_id_overblockquota(outzfsvfs
, DMU_USERUSED_OBJECT
,
1262 zfs_id_overblockquota(outzfsvfs
, DMU_GROUPUSED_OBJECT
,
1264 (projid
!= ZFS_DEFAULT_PROJID
&&
1265 zfs_id_overblockquota(outzfsvfs
, DMU_PROJECTUSED_OBJECT
,
1267 error
= SET_ERROR(EDQUOT
);
1272 error
= dmu_read_l0_bps(inos
, inzp
->z_id
, inoff
, size
, bps
,
1276 * If we are trying to clone a block that was created
1277 * in the current transaction group, error will be
1278 * EAGAIN here, which we can just return to the caller
1279 * so it can fallback if it likes.
1284 * Encrypted data is fine as long as it comes from the same
1286 * TODO: We want to extend it in the future to allow cloning to
1287 * datasets with the same keys, like clones or to be able to
1288 * clone a file from a snapshot of an encrypted dataset into the
1291 if (BP_IS_PROTECTED(&bps
[0])) {
1292 if (inzfsvfs
!= outzfsvfs
) {
1293 error
= SET_ERROR(EXDEV
);
1299 * Start a transaction.
1301 tx
= dmu_tx_create(outos
);
1302 dmu_tx_hold_sa(tx
, outzp
->z_sa_hdl
, B_FALSE
);
1303 db
= (dmu_buf_impl_t
*)sa_get_db(outzp
->z_sa_hdl
);
1305 dmu_tx_hold_clone_by_dnode(tx
, DB_DNODE(db
), outoff
, size
);
1307 zfs_sa_upgrade_txholds(tx
, outzp
);
1308 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1315 * Copy source znode's block size. This only happens on the
1316 * first iteration since zfs_rangelock_reduce() will shrink down
1317 * lr_len to the appropriate size.
1319 if (outlr
->lr_length
== UINT64_MAX
) {
1320 zfs_grow_blocksize(outzp
, inblksz
, tx
);
1322 * Round range lock up to the block boundary, so we
1323 * prevent appends until we are done.
1325 zfs_rangelock_reduce(outlr
, outoff
,
1326 ((len
- 1) / inblksz
+ 1) * inblksz
);
1329 error
= dmu_brt_clone(outos
, outzp
->z_id
, outoff
, size
, tx
,
1336 zfs_clear_setid_bits_if_necessary(outzfsvfs
, outzp
, cr
,
1337 &clear_setid_bits_txg
, tx
);
1339 zfs_tstamp_update_setup(outzp
, CONTENT_MODIFIED
, mtime
, ctime
);
1342 * Update the file size (zp_size) if it has changed;
1343 * account for possible concurrent updates.
1345 while ((outsize
= outzp
->z_size
) < outoff
+ size
) {
1346 (void) atomic_cas_64(&outzp
->z_size
, outsize
,
1350 error
= sa_bulk_update(outzp
->z_sa_hdl
, bulk
, count
, tx
);
1352 zfs_log_clone_range(zilog
, tx
, TX_CLONE_RANGE
, outzp
, outoff
,
1353 size
, inblksz
, bps
, nbps
);
1366 vmem_free(bps
, sizeof (bps
[0]) * maxblocks
);
1367 zfs_znode_update_vfs(outzp
);
1370 zfs_rangelock_exit(outlr
);
1371 zfs_rangelock_exit(inlr
);
1375 * If we have made at least partial progress, reset the error.
1379 ZFS_ACCESSTIME_STAMP(inzfsvfs
, inzp
);
1381 if (outos
->os_sync
== ZFS_SYNC_ALWAYS
) {
1382 zil_commit(zilog
, outzp
->z_id
);
1390 * If we made no progress, there must be a good reason.
1391 * EOF is handled explicitly above, before the loop.
1393 ASSERT3S(error
, !=, 0);
1396 zfs_exit_two(inzfsvfs
, outzfsvfs
, FTAG
);
1402 * Usual pattern would be to call zfs_clone_range() from zfs_replay_clone(),
1403 * but we cannot do that, because when replaying we don't have source znode
1404 * available. This is why we need a dedicated replay function.
1407 zfs_clone_range_replay(znode_t
*zp
, uint64_t off
, uint64_t len
, uint64_t blksz
,
1408 const blkptr_t
*bps
, size_t nbps
)
1415 sa_bulk_attr_t bulk
[3];
1416 uint64_t mtime
[2], ctime
[2];
1418 ASSERT3U(off
, <, MAXOFFSET_T
);
1419 ASSERT3U(len
, >, 0);
1420 ASSERT3U(nbps
, >, 0);
1422 zfsvfs
= ZTOZSB(zp
);
1424 ASSERT(spa_feature_is_enabled(dmu_objset_spa(zfsvfs
->z_os
),
1425 SPA_FEATURE_BLOCK_CLONING
));
1427 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1430 ASSERT(zfsvfs
->z_replay
);
1431 ASSERT(!zfs_is_readonly(zfsvfs
));
1433 if ((off
% blksz
) != 0) {
1434 zfs_exit(zfsvfs
, FTAG
);
1435 return (SET_ERROR(EINVAL
));
1438 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
1439 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
1440 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
1444 * Start a transaction.
1446 tx
= dmu_tx_create(zfsvfs
->z_os
);
1448 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1449 db
= (dmu_buf_impl_t
*)sa_get_db(zp
->z_sa_hdl
);
1451 dmu_tx_hold_clone_by_dnode(tx
, DB_DNODE(db
), off
, len
);
1453 zfs_sa_upgrade_txholds(tx
, zp
);
1454 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1457 zfs_exit(zfsvfs
, FTAG
);
1461 if (zp
->z_blksz
< blksz
)
1462 zfs_grow_blocksize(zp
, blksz
, tx
);
1464 dmu_brt_clone(zfsvfs
->z_os
, zp
->z_id
, off
, len
, tx
, bps
, nbps
);
1466 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
1468 if (zp
->z_size
< off
+ len
)
1469 zp
->z_size
= off
+ len
;
1471 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1474 * zil_replaying() not only check if we are replaying ZIL, but also
1475 * updates the ZIL header to record replay progress.
1477 VERIFY(zil_replaying(zfsvfs
->z_log
, tx
));
1481 zfs_znode_update_vfs(zp
);
1483 zfs_exit(zfsvfs
, FTAG
);
1488 EXPORT_SYMBOL(zfs_access
);
1489 EXPORT_SYMBOL(zfs_fsync
);
1490 EXPORT_SYMBOL(zfs_holey
);
1491 EXPORT_SYMBOL(zfs_read
);
1492 EXPORT_SYMBOL(zfs_write
);
1493 EXPORT_SYMBOL(zfs_getsecattr
);
1494 EXPORT_SYMBOL(zfs_setsecattr
);
1495 EXPORT_SYMBOL(zfs_clone_range
);
1496 EXPORT_SYMBOL(zfs_clone_range_replay
);
1498 ZFS_MODULE_PARAM(zfs_vnops
, zfs_vnops_
, read_chunk_size
, U64
, ZMOD_RW
,
1499 "Bytes to read per chunk");