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.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/sysmacros.h>
41 #include <sys/taskq.h>
43 #include <sys/vmsystm.h>
44 #include <sys/atomic.h>
45 #include <sys/pathname.h>
46 #include <sys/cmn_err.h>
47 #include <sys/errno.h>
48 #include <sys/zfs_dir.h>
49 #include <sys/zfs_acl.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/fs/zfs.h>
53 #include <sys/dmu_objset.h>
59 #include <sys/policy.h>
60 #include <sys/sunddi.h>
62 #include <sys/zfs_ctldir.h>
63 #include <sys/zfs_fuid.h>
64 #include <sys/zfs_quota.h>
65 #include <sys/zfs_sa.h>
66 #include <sys/zfs_vnops.h>
67 #include <sys/zfs_rlock.h>
71 #include <sys/sa_impl.h>
76 * Each vnode op performs some logical unit of work. To do this, the ZPL must
77 * properly lock its in-core state, create a DMU transaction, do the work,
78 * record this work in the intent log (ZIL), commit the DMU transaction,
79 * and wait for the intent log to commit if it is a synchronous operation.
80 * Moreover, the vnode ops must work in both normal and log replay context.
81 * The ordering of events is important to avoid deadlocks and references
82 * to freed memory. The example below illustrates the following Big Rules:
84 * (1) A check must be made in each zfs thread for a mounted file system.
85 * This is done avoiding races using zfs_enter(zfsvfs).
86 * A zfs_exit(zfsvfs) is needed before all returns. Any znodes
87 * must be checked with zfs_verify_zp(zp). Both of these macros
88 * can return EIO from the calling function.
90 * (2) zrele() should always be the last thing except for zil_commit() (if
91 * necessary) and zfs_exit(). This is for 3 reasons: First, if it's the
92 * last reference, the vnode/znode can be freed, so the zp may point to
93 * freed memory. Second, the last reference will call zfs_zinactive(),
94 * which may induce a lot of work -- pushing cached pages (which acquires
95 * range locks) and syncing out cached atime changes. Third,
96 * zfs_zinactive() may require a new tx, which could deadlock the system
97 * if you were already holding one. This deadlock occurs because the tx
98 * currently being operated on prevents a txg from syncing, which
99 * prevents the new tx from progressing, resulting in a deadlock. If you
100 * must call zrele() within a tx, use zfs_zrele_async(). Note that iput()
101 * is a synonym for zrele().
103 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
104 * as they can span dmu_tx_assign() calls.
106 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
107 * dmu_tx_assign(). This is critical because we don't want to block
108 * while holding locks.
110 * If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT. This
111 * reduces lock contention and CPU usage when we must wait (note that if
112 * throughput is constrained by the storage, nearly every transaction
115 * Note, in particular, that if a lock is sometimes acquired before
116 * the tx assigns, and sometimes after (e.g. z_lock), then failing
117 * to use a non-blocking assign can deadlock the system. The scenario:
119 * Thread A has grabbed a lock before calling dmu_tx_assign().
120 * Thread B is in an already-assigned tx, and blocks for this lock.
121 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
122 * forever, because the previous txg can't quiesce until B's tx commits.
124 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
125 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
126 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
127 * to indicate that this operation has already called dmu_tx_wait().
128 * This will ensure that we don't retry forever, waiting a short bit
131 * (5) If the operation succeeded, generate the intent log entry for it
132 * before dropping locks. This ensures that the ordering of events
133 * in the intent log matches the order in which they actually occurred.
134 * During ZIL replay the zfs_log_* functions will update the sequence
135 * number to indicate the zil transaction has replayed.
137 * (6) At the end of each vnode op, the DMU tx must always commit,
138 * regardless of whether there were any errors.
140 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
141 * to ensure that synchronous semantics are provided when necessary.
143 * In general, this is how things should be ordered in each vnode op:
145 * zfs_enter(zfsvfs); // exit if unmounted
147 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
148 * rw_enter(...); // grab any other locks you need
149 * tx = dmu_tx_create(...); // get DMU tx
150 * dmu_tx_hold_*(); // hold each object you might modify
151 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
153 * rw_exit(...); // drop locks
154 * zfs_dirent_unlock(dl); // unlock directory entry
155 * zrele(...); // release held znodes
156 * if (error == ERESTART) {
162 * dmu_tx_abort(tx); // abort DMU tx
163 * zfs_exit(zfsvfs); // finished in zfs
164 * return (error); // really out of space
166 * error = do_real_work(); // do whatever this VOP does
168 * zfs_log_*(...); // on success, make ZIL entry
169 * dmu_tx_commit(tx); // commit DMU tx -- error or not
170 * rw_exit(...); // drop locks
171 * zfs_dirent_unlock(dl); // unlock directory entry
172 * zrele(...); // release held znodes
173 * zil_commit(zilog, foid); // synchronous when necessary
174 * zfs_exit(zfsvfs); // finished in zfs
175 * return (error); // done, report error
178 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
181 znode_t
*zp
= ITOZ(ip
);
182 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
185 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
188 /* Honor ZFS_APPENDONLY file attribute */
189 if (blk_mode_is_open_write(mode
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
190 ((flag
& O_APPEND
) == 0)) {
191 zfs_exit(zfsvfs
, FTAG
);
192 return (SET_ERROR(EPERM
));
195 /* Keep a count of the synchronous opens in the znode */
197 atomic_inc_32(&zp
->z_sync_cnt
);
199 zfs_exit(zfsvfs
, FTAG
);
204 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
207 znode_t
*zp
= ITOZ(ip
);
208 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
211 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
214 /* Decrement the synchronous opens in the znode */
216 atomic_dec_32(&zp
->z_sync_cnt
);
218 zfs_exit(zfsvfs
, FTAG
);
224 static int zfs_fillpage(struct inode
*ip
, struct page
*pp
);
227 * When a file is memory mapped, we must keep the IO data synchronized
228 * between the DMU cache and the memory mapped pages. Update all mapped
229 * pages with the contents of the coresponding dmu buffer.
232 update_pages(znode_t
*zp
, int64_t start
, int len
, objset_t
*os
)
234 struct address_space
*mp
= ZTOI(zp
)->i_mapping
;
235 int64_t off
= start
& (PAGE_SIZE
- 1);
237 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
238 uint64_t nbytes
= MIN(PAGE_SIZE
- off
, len
);
240 struct page
*pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
242 if (mapping_writably_mapped(mp
))
243 flush_dcache_page(pp
);
246 int error
= dmu_read(os
, zp
->z_id
, start
+ off
,
247 nbytes
, pb
+ off
, DMU_READ_PREFETCH
);
252 ClearPageUptodate(pp
);
257 if (mapping_writably_mapped(mp
))
258 flush_dcache_page(pp
);
260 mark_page_accessed(pp
);
273 * When a file is memory mapped, we must keep the I/O data synchronized
274 * between the DMU cache and the memory mapped pages. Preferentially read
275 * from memory mapped pages, otherwise fallback to reading through the dmu.
278 mappedread(znode_t
*zp
, int nbytes
, zfs_uio_t
*uio
)
280 struct inode
*ip
= ZTOI(zp
);
281 struct address_space
*mp
= ip
->i_mapping
;
282 int64_t start
= uio
->uio_loffset
;
283 int64_t off
= start
& (PAGE_SIZE
- 1);
287 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
288 uint64_t bytes
= MIN(PAGE_SIZE
- off
, len
);
290 struct page
*pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
293 * If filemap_fault() retries there exists a window
294 * where the page will be unlocked and not up to date.
295 * In this case we must try and fill the page.
297 if (unlikely(!PageUptodate(pp
))) {
298 error
= zfs_fillpage(ip
, pp
);
306 ASSERT(PageUptodate(pp
) || PageDirty(pp
));
311 error
= zfs_uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
314 if (mapping_writably_mapped(mp
))
315 flush_dcache_page(pp
);
317 mark_page_accessed(pp
);
320 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
335 static unsigned long zfs_delete_blocks
= DMU_MAX_DELETEBLKCNT
;
338 * Write the bytes to a file.
340 * IN: zp - znode of file to be written to
341 * data - bytes to write
342 * len - number of bytes to write
343 * pos - offset to start writing at
345 * OUT: resid - remaining bytes to write
347 * RETURN: 0 if success
348 * positive error code if failure. EIO is returned
349 * for a short write when residp isn't provided.
352 * zp - ctime|mtime updated if byte count > 0
355 zfs_write_simple(znode_t
*zp
, const void *data
, size_t len
,
356 loff_t pos
, size_t *residp
)
358 fstrans_cookie_t cookie
;
362 iov
.iov_base
= (void *)data
;
366 zfs_uio_iovec_init(&uio
, &iov
, 1, pos
, UIO_SYSSPACE
, len
, 0);
368 cookie
= spl_fstrans_mark();
369 error
= zfs_write(zp
, &uio
, 0, kcred
);
370 spl_fstrans_unmark(cookie
);
374 *residp
= zfs_uio_resid(&uio
);
375 else if (zfs_uio_resid(&uio
) != 0)
376 error
= SET_ERROR(EIO
);
383 zfs_rele_async_task(void *arg
)
389 zfs_zrele_async(znode_t
*zp
)
391 struct inode
*ip
= ZTOI(zp
);
392 objset_t
*os
= ITOZSB(ip
)->z_os
;
394 ASSERT(atomic_read(&ip
->i_count
) > 0);
398 * If decrementing the count would put us at 0, we can't do it inline
399 * here, because that would be synchronous. Instead, dispatch an iput
402 * For more information on the dangers of a synchronous iput, see the
403 * header comment of this file.
405 if (!atomic_add_unless(&ip
->i_count
, -1, 1)) {
406 VERIFY(taskq_dispatch(dsl_pool_zrele_taskq(dmu_objset_pool(os
)),
407 zfs_rele_async_task
, ip
, TQ_SLEEP
) != TASKQID_INVALID
);
413 * Lookup an entry in a directory, or an extended attribute directory.
414 * If it exists, return a held inode reference for it.
416 * IN: zdp - znode of directory to search.
417 * nm - name of entry to lookup.
418 * flags - LOOKUP_XATTR set if looking for an attribute.
419 * cr - credentials of caller.
420 * direntflags - directory lookup flags
421 * realpnp - returned pathname.
423 * OUT: zpp - znode of located entry, NULL if not found.
425 * RETURN: 0 on success, error code on failure.
431 zfs_lookup(znode_t
*zdp
, char *nm
, znode_t
**zpp
, int flags
, cred_t
*cr
,
432 int *direntflags
, pathname_t
*realpnp
)
434 zfsvfs_t
*zfsvfs
= ZTOZSB(zdp
);
438 * Fast path lookup, however we must skip DNLC lookup
439 * for case folding or normalizing lookups because the
440 * DNLC code only stores the passed in name. This means
441 * creating 'a' and removing 'A' on a case insensitive
442 * file system would work, but DNLC still thinks 'a'
443 * exists and won't let you create it again on the next
444 * pass through fast path.
446 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
448 if (!S_ISDIR(ZTOI(zdp
)->i_mode
)) {
449 return (SET_ERROR(ENOTDIR
));
450 } else if (zdp
->z_sa_hdl
== NULL
) {
451 return (SET_ERROR(EIO
));
454 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
455 error
= zfs_fastaccesschk_execute(zdp
, cr
);
465 if ((error
= zfs_enter_verify_zp(zfsvfs
, zdp
, FTAG
)) != 0)
470 if (flags
& LOOKUP_XATTR
) {
472 * We don't allow recursive attributes..
473 * Maybe someday we will.
475 if (zdp
->z_pflags
& ZFS_XATTR
) {
476 zfs_exit(zfsvfs
, FTAG
);
477 return (SET_ERROR(EINVAL
));
480 if ((error
= zfs_get_xattrdir(zdp
, zpp
, cr
, flags
))) {
481 zfs_exit(zfsvfs
, FTAG
);
486 * Do we have permission to get into attribute directory?
489 if ((error
= zfs_zaccess(*zpp
, ACE_EXECUTE
, 0,
490 B_TRUE
, cr
, zfs_init_idmap
))) {
495 zfs_exit(zfsvfs
, FTAG
);
499 if (!S_ISDIR(ZTOI(zdp
)->i_mode
)) {
500 zfs_exit(zfsvfs
, FTAG
);
501 return (SET_ERROR(ENOTDIR
));
505 * Check accessibility of directory.
508 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
,
510 zfs_exit(zfsvfs
, FTAG
);
514 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
515 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
516 zfs_exit(zfsvfs
, FTAG
);
517 return (SET_ERROR(EILSEQ
));
520 error
= zfs_dirlook(zdp
, nm
, zpp
, flags
, direntflags
, realpnp
);
521 if ((error
== 0) && (*zpp
))
522 zfs_znode_update_vfs(*zpp
);
524 zfs_exit(zfsvfs
, FTAG
);
529 * Attempt to create a new entry in a directory. If the entry
530 * already exists, truncate the file if permissible, else return
531 * an error. Return the ip of the created or trunc'd file.
533 * IN: dzp - znode of directory to put new file entry in.
534 * name - name of new file entry.
535 * vap - attributes of new file.
536 * excl - flag indicating exclusive or non-exclusive mode.
537 * mode - mode to open file with.
538 * cr - credentials of caller.
540 * vsecp - ACL to be set
541 * mnt_ns - user namespace of the mount
543 * OUT: zpp - znode of created or trunc'd entry.
545 * RETURN: 0 on success, error code on failure.
548 * dzp - ctime|mtime updated if new entry created
549 * zp - ctime|mtime always, atime if new
552 zfs_create(znode_t
*dzp
, char *name
, vattr_t
*vap
, int excl
,
553 int mode
, znode_t
**zpp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
,
557 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
565 zfs_acl_ids_t acl_ids
;
566 boolean_t fuid_dirtied
;
567 boolean_t have_acl
= B_FALSE
;
568 boolean_t waited
= B_FALSE
;
569 boolean_t skip_acl
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
572 * If we have an ephemeral id, ACL, or XVATTR then
573 * make sure file system is at proper version
579 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
580 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
581 return (SET_ERROR(EINVAL
));
584 return (SET_ERROR(EINVAL
));
586 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
589 zilog
= zfsvfs
->z_log
;
591 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
592 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
593 zfs_exit(zfsvfs
, FTAG
);
594 return (SET_ERROR(EILSEQ
));
597 if (vap
->va_mask
& ATTR_XVATTR
) {
598 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
599 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
600 zfs_exit(zfsvfs
, FTAG
);
609 * Null component name refers to the directory itself.
616 /* possible igrab(zp) */
619 if (flag
& FIGNORECASE
)
622 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
626 zfs_acl_ids_free(&acl_ids
);
627 if (strcmp(name
, "..") == 0)
628 error
= SET_ERROR(EISDIR
);
629 zfs_exit(zfsvfs
, FTAG
);
636 uint64_t projid
= ZFS_DEFAULT_PROJID
;
639 * Create a new file object and update the directory
642 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, skip_acl
, cr
,
645 zfs_acl_ids_free(&acl_ids
);
650 * We only support the creation of regular files in
651 * extended attribute directories.
654 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
656 zfs_acl_ids_free(&acl_ids
);
657 error
= SET_ERROR(EINVAL
);
661 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
662 cr
, vsecp
, &acl_ids
, mnt_ns
)) != 0)
666 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
667 projid
= zfs_inherit_projid(dzp
);
668 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
669 zfs_acl_ids_free(&acl_ids
);
670 error
= SET_ERROR(EDQUOT
);
674 tx
= dmu_tx_create(os
);
676 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
677 ZFS_SA_BASE_ATTR_SIZE
);
679 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
681 zfs_fuid_txhold(zfsvfs
, tx
);
682 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
683 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
684 if (!zfsvfs
->z_use_sa
&&
685 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
686 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
687 0, acl_ids
.z_aclp
->z_acl_bytes
);
690 error
= dmu_tx_assign(tx
,
691 (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
693 zfs_dirent_unlock(dl
);
694 if (error
== ERESTART
) {
700 zfs_acl_ids_free(&acl_ids
);
702 zfs_exit(zfsvfs
, FTAG
);
705 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
707 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
710 * Since, we failed to add the directory entry for it,
711 * delete the newly created dnode.
713 zfs_znode_delete(zp
, tx
);
714 remove_inode_hash(ZTOI(zp
));
715 zfs_acl_ids_free(&acl_ids
);
721 zfs_fuid_sync(zfsvfs
, tx
);
723 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
724 if (flag
& FIGNORECASE
)
726 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
727 vsecp
, acl_ids
.z_fuidp
, vap
);
728 zfs_acl_ids_free(&acl_ids
);
731 int aflags
= (flag
& O_APPEND
) ? V_APPEND
: 0;
734 zfs_acl_ids_free(&acl_ids
);
737 * A directory entry already exists for this name.
740 * Can't truncate an existing file if in exclusive mode.
743 error
= SET_ERROR(EEXIST
);
747 * Can't open a directory for writing.
749 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
750 error
= SET_ERROR(EISDIR
);
754 * Verify requested access to file.
756 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
,
761 mutex_enter(&dzp
->z_lock
);
763 mutex_exit(&dzp
->z_lock
);
766 * Truncate regular files if requested.
768 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
769 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
770 /* we can't hold any locks when calling zfs_freesp() */
772 zfs_dirent_unlock(dl
);
775 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
781 zfs_dirent_unlock(dl
);
787 zfs_znode_update_vfs(dzp
);
788 zfs_znode_update_vfs(zp
);
792 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
793 zil_commit(zilog
, 0);
795 zfs_exit(zfsvfs
, FTAG
);
800 zfs_tmpfile(struct inode
*dip
, vattr_t
*vap
, int excl
,
801 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
,
804 (void) excl
, (void) mode
, (void) flag
;
805 znode_t
*zp
= NULL
, *dzp
= ITOZ(dip
);
806 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
812 zfs_acl_ids_t acl_ids
;
813 uint64_t projid
= ZFS_DEFAULT_PROJID
;
814 boolean_t fuid_dirtied
;
815 boolean_t have_acl
= B_FALSE
;
816 boolean_t waited
= B_FALSE
;
819 * If we have an ephemeral id, ACL, or XVATTR then
820 * make sure file system is at proper version
826 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
827 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
828 return (SET_ERROR(EINVAL
));
830 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
834 if (vap
->va_mask
& ATTR_XVATTR
) {
835 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
836 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
837 zfs_exit(zfsvfs
, FTAG
);
846 * Create a new file object and update the directory
849 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, mnt_ns
))) {
851 zfs_acl_ids_free(&acl_ids
);
855 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
856 cr
, vsecp
, &acl_ids
, mnt_ns
)) != 0)
860 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
861 projid
= zfs_inherit_projid(dzp
);
862 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
863 zfs_acl_ids_free(&acl_ids
);
864 error
= SET_ERROR(EDQUOT
);
868 tx
= dmu_tx_create(os
);
870 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
871 ZFS_SA_BASE_ATTR_SIZE
);
872 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
874 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
876 zfs_fuid_txhold(zfsvfs
, tx
);
877 if (!zfsvfs
->z_use_sa
&&
878 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
879 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
880 0, acl_ids
.z_aclp
->z_acl_bytes
);
882 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
884 if (error
== ERESTART
) {
890 zfs_acl_ids_free(&acl_ids
);
892 zfs_exit(zfsvfs
, FTAG
);
895 zfs_mknode(dzp
, vap
, tx
, cr
, IS_TMPFILE
, &zp
, &acl_ids
);
898 zfs_fuid_sync(zfsvfs
, tx
);
900 /* Add to unlinked set */
901 zp
->z_unlinked
= B_TRUE
;
902 zfs_unlinked_add(zp
, tx
);
903 zfs_acl_ids_free(&acl_ids
);
911 zfs_znode_update_vfs(dzp
);
912 zfs_znode_update_vfs(zp
);
916 zfs_exit(zfsvfs
, FTAG
);
921 * Remove an entry from a directory.
923 * IN: dzp - znode of directory to remove entry from.
924 * name - name of entry to remove.
925 * cr - credentials of caller.
926 * flags - case flags.
928 * RETURN: 0 if success
929 * error code if failure
933 * ip - ctime (if nlink > 0)
936 static uint64_t null_xattr
= 0;
939 zfs_remove(znode_t
*dzp
, char *name
, cred_t
*cr
, int flags
)
943 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
945 uint64_t acl_obj
, xattr_obj
;
946 uint64_t xattr_obj_unlinked
= 0;
951 boolean_t may_delete_now
, delete_now
= FALSE
;
952 boolean_t unlinked
, toobig
= FALSE
;
954 pathname_t
*realnmp
= NULL
;
958 boolean_t waited
= B_FALSE
;
961 return (SET_ERROR(EINVAL
));
963 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
965 zilog
= zfsvfs
->z_log
;
967 if (flags
& FIGNORECASE
) {
977 * Attempt to lock directory; fail if entry doesn't exist.
979 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
983 zfs_exit(zfsvfs
, FTAG
);
987 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
, zfs_init_idmap
))) {
992 * Need to use rmdir for removing directories.
994 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
995 error
= SET_ERROR(EPERM
);
999 mutex_enter(&zp
->z_lock
);
1000 may_delete_now
= atomic_read(&ZTOI(zp
)->i_count
) == 1 &&
1001 !zn_has_cached_data(zp
, 0, LLONG_MAX
);
1002 mutex_exit(&zp
->z_lock
);
1005 * We may delete the znode now, or we may put it in the unlinked set;
1006 * it depends on whether we're the last link, and on whether there are
1007 * other holds on the inode. So we dmu_tx_hold() the right things to
1008 * allow for either case.
1011 tx
= dmu_tx_create(zfsvfs
->z_os
);
1012 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1013 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1014 zfs_sa_upgrade_txholds(tx
, zp
);
1015 zfs_sa_upgrade_txholds(tx
, dzp
);
1016 if (may_delete_now
) {
1017 toobig
= zp
->z_size
> zp
->z_blksz
* zfs_delete_blocks
;
1018 /* if the file is too big, only hold_free a token amount */
1019 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1020 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1023 /* are there any extended attributes? */
1024 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1025 &xattr_obj
, sizeof (xattr_obj
));
1026 if (error
== 0 && xattr_obj
) {
1027 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1029 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1030 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1033 mutex_enter(&zp
->z_lock
);
1034 if ((acl_obj
= zfs_external_acl(zp
)) != 0 && may_delete_now
)
1035 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1036 mutex_exit(&zp
->z_lock
);
1038 /* charge as an update -- would be nice not to charge at all */
1039 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1042 * Mark this transaction as typically resulting in a net free of space
1044 dmu_tx_mark_netfree(tx
);
1046 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1048 zfs_dirent_unlock(dl
);
1049 if (error
== ERESTART
) {
1064 zfs_exit(zfsvfs
, FTAG
);
1069 * Remove the directory entry.
1071 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1080 * Hold z_lock so that we can make sure that the ACL obj
1081 * hasn't changed. Could have been deleted due to
1084 mutex_enter(&zp
->z_lock
);
1085 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1086 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1087 delete_now
= may_delete_now
&& !toobig
&&
1088 atomic_read(&ZTOI(zp
)->i_count
) == 1 &&
1089 !zn_has_cached_data(zp
, 0, LLONG_MAX
) &&
1090 xattr_obj
== xattr_obj_unlinked
&&
1091 zfs_external_acl(zp
) == acl_obj
;
1092 VERIFY_IMPLY(xattr_obj_unlinked
, xzp
);
1096 if (xattr_obj_unlinked
) {
1097 ASSERT3U(ZTOI(xzp
)->i_nlink
, ==, 2);
1098 mutex_enter(&xzp
->z_lock
);
1099 xzp
->z_unlinked
= B_TRUE
;
1100 clear_nlink(ZTOI(xzp
));
1102 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
1103 &links
, sizeof (links
), tx
);
1104 ASSERT3U(error
, ==, 0);
1105 mutex_exit(&xzp
->z_lock
);
1106 zfs_unlinked_add(xzp
, tx
);
1109 error
= sa_remove(zp
->z_sa_hdl
,
1110 SA_ZPL_XATTR(zfsvfs
), tx
);
1112 error
= sa_update(zp
->z_sa_hdl
,
1113 SA_ZPL_XATTR(zfsvfs
), &null_xattr
,
1114 sizeof (uint64_t), tx
);
1118 * Add to the unlinked set because a new reference could be
1119 * taken concurrently resulting in a deferred destruction.
1121 zfs_unlinked_add(zp
, tx
);
1122 mutex_exit(&zp
->z_lock
);
1123 } else if (unlinked
) {
1124 mutex_exit(&zp
->z_lock
);
1125 zfs_unlinked_add(zp
, tx
);
1129 if (flags
& FIGNORECASE
)
1131 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
, unlinked
);
1138 zfs_dirent_unlock(dl
);
1139 zfs_znode_update_vfs(dzp
);
1140 zfs_znode_update_vfs(zp
);
1145 zfs_zrele_async(zp
);
1148 zfs_znode_update_vfs(xzp
);
1149 zfs_zrele_async(xzp
);
1152 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1153 zil_commit(zilog
, 0);
1155 zfs_exit(zfsvfs
, FTAG
);
1160 * Create a new directory and insert it into dzp using the name
1161 * provided. Return a pointer to the inserted directory.
1163 * IN: dzp - znode of directory to add subdir to.
1164 * dirname - name of new directory.
1165 * vap - attributes of new directory.
1166 * cr - credentials of caller.
1167 * flags - case flags.
1168 * vsecp - ACL to be set
1169 * mnt_ns - user namespace of the mount
1171 * OUT: zpp - znode of created directory.
1173 * RETURN: 0 if success
1174 * error code if failure
1177 * dzp - ctime|mtime updated
1178 * zpp - ctime|mtime|atime updated
1181 zfs_mkdir(znode_t
*dzp
, char *dirname
, vattr_t
*vap
, znode_t
**zpp
,
1182 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
, zidmap_t
*mnt_ns
)
1185 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1193 gid_t gid
= crgetgid(cr
);
1194 zfs_acl_ids_t acl_ids
;
1195 boolean_t fuid_dirtied
;
1196 boolean_t waited
= B_FALSE
;
1198 ASSERT(S_ISDIR(vap
->va_mode
));
1201 * If we have an ephemeral id, ACL, or XVATTR then
1202 * make sure file system is at proper version
1206 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1207 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1208 return (SET_ERROR(EINVAL
));
1210 if (dirname
== NULL
)
1211 return (SET_ERROR(EINVAL
));
1213 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1215 zilog
= zfsvfs
->z_log
;
1217 if (dzp
->z_pflags
& ZFS_XATTR
) {
1218 zfs_exit(zfsvfs
, FTAG
);
1219 return (SET_ERROR(EINVAL
));
1222 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1223 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1224 zfs_exit(zfsvfs
, FTAG
);
1225 return (SET_ERROR(EILSEQ
));
1227 if (flags
& FIGNORECASE
)
1230 if (vap
->va_mask
& ATTR_XVATTR
) {
1231 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1232 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1233 zfs_exit(zfsvfs
, FTAG
);
1238 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1239 vsecp
, &acl_ids
, mnt_ns
)) != 0) {
1240 zfs_exit(zfsvfs
, FTAG
);
1244 * First make sure the new directory doesn't exist.
1246 * Existence is checked first to make sure we don't return
1247 * EACCES instead of EEXIST which can cause some applications
1253 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1255 zfs_acl_ids_free(&acl_ids
);
1256 zfs_exit(zfsvfs
, FTAG
);
1260 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
,
1262 zfs_acl_ids_free(&acl_ids
);
1263 zfs_dirent_unlock(dl
);
1264 zfs_exit(zfsvfs
, FTAG
);
1268 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, zfs_inherit_projid(dzp
))) {
1269 zfs_acl_ids_free(&acl_ids
);
1270 zfs_dirent_unlock(dl
);
1271 zfs_exit(zfsvfs
, FTAG
);
1272 return (SET_ERROR(EDQUOT
));
1276 * Add a new entry to the directory.
1278 tx
= dmu_tx_create(zfsvfs
->z_os
);
1279 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1280 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1281 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1283 zfs_fuid_txhold(zfsvfs
, tx
);
1284 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1285 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1286 acl_ids
.z_aclp
->z_acl_bytes
);
1289 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1290 ZFS_SA_BASE_ATTR_SIZE
);
1292 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1294 zfs_dirent_unlock(dl
);
1295 if (error
== ERESTART
) {
1301 zfs_acl_ids_free(&acl_ids
);
1303 zfs_exit(zfsvfs
, FTAG
);
1310 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1313 * Now put new name in parent dir.
1315 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
1317 zfs_znode_delete(zp
, tx
);
1318 remove_inode_hash(ZTOI(zp
));
1323 zfs_fuid_sync(zfsvfs
, tx
);
1327 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1328 if (flags
& FIGNORECASE
)
1330 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1331 acl_ids
.z_fuidp
, vap
);
1334 zfs_acl_ids_free(&acl_ids
);
1338 zfs_dirent_unlock(dl
);
1340 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1341 zil_commit(zilog
, 0);
1346 zfs_znode_update_vfs(dzp
);
1347 zfs_znode_update_vfs(zp
);
1349 zfs_exit(zfsvfs
, FTAG
);
1354 * Remove a directory subdir entry. If the current working
1355 * directory is the same as the subdir to be removed, the
1358 * IN: dzp - znode of directory to remove from.
1359 * name - name of directory to be removed.
1360 * cwd - inode of current working directory.
1361 * cr - credentials of caller.
1362 * flags - case flags
1364 * RETURN: 0 on success, error code on failure.
1367 * dzp - ctime|mtime updated
1370 zfs_rmdir(znode_t
*dzp
, char *name
, znode_t
*cwd
, cred_t
*cr
,
1374 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1380 boolean_t waited
= B_FALSE
;
1383 return (SET_ERROR(EINVAL
));
1385 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1387 zilog
= zfsvfs
->z_log
;
1389 if (flags
& FIGNORECASE
)
1395 * Attempt to lock directory; fail if entry doesn't exist.
1397 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1399 zfs_exit(zfsvfs
, FTAG
);
1403 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
, zfs_init_idmap
))) {
1407 if (!S_ISDIR(ZTOI(zp
)->i_mode
)) {
1408 error
= SET_ERROR(ENOTDIR
);
1413 error
= SET_ERROR(EINVAL
);
1418 * Grab a lock on the directory to make sure that no one is
1419 * trying to add (or lookup) entries while we are removing it.
1421 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1424 * Grab a lock on the parent pointer to make sure we play well
1425 * with the treewalk and directory rename code.
1427 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1429 tx
= dmu_tx_create(zfsvfs
->z_os
);
1430 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1431 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1432 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1433 zfs_sa_upgrade_txholds(tx
, zp
);
1434 zfs_sa_upgrade_txholds(tx
, dzp
);
1435 dmu_tx_mark_netfree(tx
);
1436 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1438 rw_exit(&zp
->z_parent_lock
);
1439 rw_exit(&zp
->z_name_lock
);
1440 zfs_dirent_unlock(dl
);
1441 if (error
== ERESTART
) {
1450 zfs_exit(zfsvfs
, FTAG
);
1454 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1457 uint64_t txtype
= TX_RMDIR
;
1458 if (flags
& FIGNORECASE
)
1460 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
,
1466 rw_exit(&zp
->z_parent_lock
);
1467 rw_exit(&zp
->z_name_lock
);
1469 zfs_dirent_unlock(dl
);
1471 zfs_znode_update_vfs(dzp
);
1472 zfs_znode_update_vfs(zp
);
1475 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1476 zil_commit(zilog
, 0);
1478 zfs_exit(zfsvfs
, FTAG
);
1483 * Read directory entries from the given directory cursor position and emit
1484 * name and position for each entry.
1486 * IN: ip - inode of directory to read.
1487 * ctx - directory entry context.
1488 * cr - credentials of caller.
1490 * RETURN: 0 if success
1491 * error code if failure
1494 * ip - atime updated
1496 * Note that the low 4 bits of the cookie returned by zap is always zero.
1497 * This allows us to use the low range for "special" directory entries:
1498 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1499 * we use the offset 2 for the '.zfs' directory.
1502 zfs_readdir(struct inode
*ip
, zpl_dir_context_t
*ctx
, cred_t
*cr
)
1505 znode_t
*zp
= ITOZ(ip
);
1506 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1509 zap_attribute_t zap
;
1515 uint64_t offset
; /* must be unsigned; checks for < 1 */
1517 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1520 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
1521 &parent
, sizeof (parent
))) != 0)
1525 * Quit if directory has been removed (posix)
1533 prefetch
= zp
->z_zn_prefetch
;
1536 * Initialize the iterator cursor.
1540 * Start iteration from the beginning of the directory.
1542 zap_cursor_init(&zc
, os
, zp
->z_id
);
1545 * The offset is a serialized cursor.
1547 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
1551 * Transform to file-system independent format
1556 * Special case `.', `..', and `.zfs'.
1559 (void) strcpy(zap
.za_name
, ".");
1560 zap
.za_normalization_conflict
= 0;
1563 } else if (offset
== 1) {
1564 (void) strcpy(zap
.za_name
, "..");
1565 zap
.za_normalization_conflict
= 0;
1568 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
1569 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
1570 zap
.za_normalization_conflict
= 0;
1571 objnum
= ZFSCTL_INO_ROOT
;
1577 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
1578 if (error
== ENOENT
)
1585 * Allow multiple entries provided the first entry is
1586 * the object id. Non-zpl consumers may safely make
1587 * use of the additional space.
1589 * XXX: This should be a feature flag for compatibility
1591 if (zap
.za_integer_length
!= 8 ||
1592 zap
.za_num_integers
== 0) {
1593 cmn_err(CE_WARN
, "zap_readdir: bad directory "
1594 "entry, obj = %lld, offset = %lld, "
1595 "length = %d, num = %lld\n",
1596 (u_longlong_t
)zp
->z_id
,
1597 (u_longlong_t
)offset
,
1598 zap
.za_integer_length
,
1599 (u_longlong_t
)zap
.za_num_integers
);
1600 error
= SET_ERROR(ENXIO
);
1604 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
1605 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
1608 done
= !zpl_dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
1613 /* Prefetch znode */
1615 dmu_prefetch(os
, objnum
, 0, 0, 0,
1616 ZIO_PRIORITY_SYNC_READ
);
1620 * Move to the next entry, fill in the previous offset.
1622 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
1623 zap_cursor_advance(&zc
);
1624 offset
= zap_cursor_serialize(&zc
);
1630 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
1633 zap_cursor_fini(&zc
);
1634 if (error
== ENOENT
)
1637 zfs_exit(zfsvfs
, FTAG
);
1643 * Get the basic file attributes and place them in the provided kstat
1644 * structure. The inode is assumed to be the authoritative source
1645 * for most of the attributes. However, the znode currently has the
1646 * authoritative atime, blksize, and block count.
1648 * IN: ip - inode of file.
1650 * OUT: sp - kstat values.
1652 * RETURN: 0 (always succeeds)
1655 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
1656 zfs_getattr_fast(zidmap_t
*user_ns
, u32 request_mask
, struct inode
*ip
,
1659 zfs_getattr_fast(zidmap_t
*user_ns
, struct inode
*ip
, struct kstat
*sp
)
1662 znode_t
*zp
= ITOZ(ip
);
1663 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1665 u_longlong_t nblocks
;
1668 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1671 mutex_enter(&zp
->z_lock
);
1673 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
1674 zpl_generic_fillattr(user_ns
, request_mask
, ip
, sp
);
1676 zpl_generic_fillattr(user_ns
, ip
, sp
);
1679 * +1 link count for root inode with visible '.zfs' directory.
1681 if ((zp
->z_id
== zfsvfs
->z_root
) && zfs_show_ctldir(zp
))
1682 if (sp
->nlink
< ZFS_LINK_MAX
)
1685 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
1686 sp
->blksize
= blksize
;
1687 sp
->blocks
= nblocks
;
1689 if (unlikely(zp
->z_blksz
== 0)) {
1691 * Block size hasn't been set; suggest maximal I/O transfers.
1693 sp
->blksize
= zfsvfs
->z_max_blksz
;
1696 mutex_exit(&zp
->z_lock
);
1699 * Required to prevent NFS client from detecting different inode
1700 * numbers of snapshot root dentry before and after snapshot mount.
1702 if (zfsvfs
->z_issnap
) {
1703 if (ip
->i_sb
->s_root
->d_inode
== ip
)
1704 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
1705 dmu_objset_id(zfsvfs
->z_os
);
1708 zfs_exit(zfsvfs
, FTAG
);
1714 * For the operation of changing file's user/group/project, we need to
1715 * handle not only the main object that is assigned to the file directly,
1716 * but also the ones that are used by the file via hidden xattr directory.
1718 * Because the xattr directory may contains many EA entries, as to it may
1719 * be impossible to change all of them via the transaction of changing the
1720 * main object's user/group/project attributes. Then we have to change them
1721 * via other multiple independent transactions one by one. It may be not good
1722 * solution, but we have no better idea yet.
1725 zfs_setattr_dir(znode_t
*dzp
)
1727 struct inode
*dxip
= ZTOI(dzp
);
1728 struct inode
*xip
= NULL
;
1729 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1730 objset_t
*os
= zfsvfs
->z_os
;
1732 zap_attribute_t zap
;
1735 dmu_tx_t
*tx
= NULL
;
1737 sa_bulk_attr_t bulk
[4];
1741 zap_cursor_init(&zc
, os
, dzp
->z_id
);
1742 while ((err
= zap_cursor_retrieve(&zc
, &zap
)) == 0) {
1744 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
1749 err
= zfs_dirent_lock(&dl
, dzp
, (char *)zap
.za_name
, &zp
,
1750 ZEXISTS
, NULL
, NULL
);
1757 if (KUID_TO_SUID(xip
->i_uid
) == KUID_TO_SUID(dxip
->i_uid
) &&
1758 KGID_TO_SGID(xip
->i_gid
) == KGID_TO_SGID(dxip
->i_gid
) &&
1759 zp
->z_projid
== dzp
->z_projid
)
1762 tx
= dmu_tx_create(os
);
1763 if (!(zp
->z_pflags
& ZFS_PROJID
))
1764 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1766 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1768 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1772 mutex_enter(&dzp
->z_lock
);
1774 if (KUID_TO_SUID(xip
->i_uid
) != KUID_TO_SUID(dxip
->i_uid
)) {
1775 xip
->i_uid
= dxip
->i_uid
;
1776 uid
= zfs_uid_read(dxip
);
1777 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
1778 &uid
, sizeof (uid
));
1781 if (KGID_TO_SGID(xip
->i_gid
) != KGID_TO_SGID(dxip
->i_gid
)) {
1782 xip
->i_gid
= dxip
->i_gid
;
1783 gid
= zfs_gid_read(dxip
);
1784 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
1785 &gid
, sizeof (gid
));
1788 if (zp
->z_projid
!= dzp
->z_projid
) {
1789 if (!(zp
->z_pflags
& ZFS_PROJID
)) {
1790 zp
->z_pflags
|= ZFS_PROJID
;
1791 SA_ADD_BULK_ATTR(bulk
, count
,
1792 SA_ZPL_FLAGS(zfsvfs
), NULL
, &zp
->z_pflags
,
1793 sizeof (zp
->z_pflags
));
1796 zp
->z_projid
= dzp
->z_projid
;
1797 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PROJID(zfsvfs
),
1798 NULL
, &zp
->z_projid
, sizeof (zp
->z_projid
));
1801 mutex_exit(&dzp
->z_lock
);
1803 if (likely(count
> 0)) {
1804 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1810 if (err
!= 0 && err
!= ENOENT
)
1817 zfs_dirent_unlock(dl
);
1819 zap_cursor_advance(&zc
);
1826 zfs_dirent_unlock(dl
);
1828 zap_cursor_fini(&zc
);
1830 return (err
== ENOENT
? 0 : err
);
1834 * Set the file attributes to the values contained in the
1837 * IN: zp - znode of file to be modified.
1838 * vap - new attribute values.
1839 * If ATTR_XVATTR set, then optional attrs are being set
1840 * flags - ATTR_UTIME set if non-default time values provided.
1841 * - ATTR_NOACLCHECK (CIFS context only).
1842 * cr - credentials of caller.
1843 * mnt_ns - user namespace of the mount
1845 * RETURN: 0 if success
1846 * error code if failure
1849 * ip - ctime updated, mtime updated if size changed.
1852 zfs_setattr(znode_t
*zp
, vattr_t
*vap
, int flags
, cred_t
*cr
, zidmap_t
*mnt_ns
)
1855 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1856 objset_t
*os
= zfsvfs
->z_os
;
1860 xvattr_t
*tmpxvattr
;
1861 uint_t mask
= vap
->va_mask
;
1862 uint_t saved_mask
= 0;
1865 uint64_t new_kuid
= 0, new_kgid
= 0, new_uid
, new_gid
;
1867 uint64_t mtime
[2], ctime
[2], atime
[2];
1868 uint64_t projid
= ZFS_INVALID_PROJID
;
1870 int need_policy
= FALSE
;
1872 zfs_fuid_info_t
*fuidp
= NULL
;
1873 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
1876 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
1877 boolean_t fuid_dirtied
= B_FALSE
;
1878 boolean_t handle_eadir
= B_FALSE
;
1879 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
1880 int count
= 0, xattr_count
= 0, bulks
= 8;
1885 if ((err
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1890 * If this is a xvattr_t, then get a pointer to the structure of
1891 * optional attributes. If this is NULL, then we have a vattr_t.
1893 xoap
= xva_getxoptattr(xvap
);
1894 if (xoap
!= NULL
&& (mask
& ATTR_XVATTR
)) {
1895 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
1896 if (!dmu_objset_projectquota_enabled(os
) ||
1897 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
))) {
1898 zfs_exit(zfsvfs
, FTAG
);
1899 return (SET_ERROR(ENOTSUP
));
1902 projid
= xoap
->xoa_projid
;
1903 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
1904 zfs_exit(zfsvfs
, FTAG
);
1905 return (SET_ERROR(EINVAL
));
1908 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
1909 projid
= ZFS_INVALID_PROJID
;
1914 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
1915 (xoap
->xoa_projinherit
!=
1916 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) &&
1917 (!dmu_objset_projectquota_enabled(os
) ||
1918 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
)))) {
1919 zfs_exit(zfsvfs
, FTAG
);
1920 return (SET_ERROR(ENOTSUP
));
1924 zilog
= zfsvfs
->z_log
;
1927 * Make sure that if we have ephemeral uid/gid or xvattr specified
1928 * that file system is at proper version level
1931 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1932 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
1933 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
1934 (mask
& ATTR_XVATTR
))) {
1935 zfs_exit(zfsvfs
, FTAG
);
1936 return (SET_ERROR(EINVAL
));
1939 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
1940 zfs_exit(zfsvfs
, FTAG
);
1941 return (SET_ERROR(EISDIR
));
1944 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
1945 zfs_exit(zfsvfs
, FTAG
);
1946 return (SET_ERROR(EINVAL
));
1949 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
1950 xva_init(tmpxvattr
);
1952 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
1953 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
1956 * Immutable files can only alter immutable bit and atime
1958 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
1959 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
1960 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
1961 err
= SET_ERROR(EPERM
);
1965 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
1966 err
= SET_ERROR(EPERM
);
1971 * Verify timestamps doesn't overflow 32 bits.
1972 * ZFS can handle large timestamps, but 32bit syscalls can't
1973 * handle times greater than 2039. This check should be removed
1974 * once large timestamps are fully supported.
1976 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
1977 if (((mask
& ATTR_ATIME
) &&
1978 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
1979 ((mask
& ATTR_MTIME
) &&
1980 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
1981 err
= SET_ERROR(EOVERFLOW
);
1990 /* Can this be moved to before the top label? */
1991 if (zfs_is_readonly(zfsvfs
)) {
1992 err
= SET_ERROR(EROFS
);
1997 * First validate permissions
2000 if (mask
& ATTR_SIZE
) {
2001 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
,
2007 * XXX - Note, we are not providing any open
2008 * mode flags here (like FNDELAY), so we may
2009 * block if there are locks present... this
2010 * should be addressed in openat().
2012 /* XXX - would it be OK to generate a log record here? */
2013 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2018 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2019 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2020 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2021 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2022 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2023 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2024 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2025 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2026 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2027 skipaclchk
, cr
, mnt_ns
);
2030 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2031 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2038 * NOTE: even if a new mode is being set,
2039 * we may clear S_ISUID/S_ISGID bits.
2042 if (!(mask
& ATTR_MODE
))
2043 vap
->va_mode
= zp
->z_mode
;
2046 * Take ownership or chgrp to group we are a member of
2049 uid
= zfs_uid_to_vfsuid(mnt_ns
, zfs_i_user_ns(ip
),
2051 gid
= zfs_gid_to_vfsgid(mnt_ns
, zfs_i_user_ns(ip
),
2053 take_owner
= (mask
& ATTR_UID
) && (uid
== crgetuid(cr
));
2054 take_group
= (mask
& ATTR_GID
) &&
2055 zfs_groupmember(zfsvfs
, gid
, cr
);
2058 * If both ATTR_UID and ATTR_GID are set then take_owner and
2059 * take_group must both be set in order to allow taking
2062 * Otherwise, send the check through secpolicy_vnode_setattr()
2066 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2067 take_owner
&& take_group
) ||
2068 ((idmask
== ATTR_UID
) && take_owner
) ||
2069 ((idmask
== ATTR_GID
) && take_group
)) {
2070 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2071 skipaclchk
, cr
, mnt_ns
) == 0) {
2073 * Remove setuid/setgid for non-privileged users
2075 (void) secpolicy_setid_clear(vap
, cr
);
2076 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2085 mutex_enter(&zp
->z_lock
);
2086 oldva
.va_mode
= zp
->z_mode
;
2087 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2088 if (mask
& ATTR_XVATTR
) {
2090 * Update xvattr mask to include only those attributes
2091 * that are actually changing.
2093 * the bits will be restored prior to actually setting
2094 * the attributes so the caller thinks they were set.
2096 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2097 if (xoap
->xoa_appendonly
!=
2098 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2101 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2102 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2106 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2107 if (xoap
->xoa_projinherit
!=
2108 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
2111 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
2112 XVA_SET_REQ(tmpxvattr
, XAT_PROJINHERIT
);
2116 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2117 if (xoap
->xoa_nounlink
!=
2118 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2121 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2122 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2126 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2127 if (xoap
->xoa_immutable
!=
2128 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2131 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2132 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2136 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2137 if (xoap
->xoa_nodump
!=
2138 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2141 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2142 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2146 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2147 if (xoap
->xoa_av_modified
!=
2148 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2151 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2152 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2156 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2157 if ((!S_ISREG(ip
->i_mode
) &&
2158 xoap
->xoa_av_quarantined
) ||
2159 xoap
->xoa_av_quarantined
!=
2160 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2163 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2164 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2168 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2169 mutex_exit(&zp
->z_lock
);
2170 err
= SET_ERROR(EPERM
);
2174 if (need_policy
== FALSE
&&
2175 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2176 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2181 mutex_exit(&zp
->z_lock
);
2183 if (mask
& ATTR_MODE
) {
2184 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
,
2186 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2187 &oldva
, cr
, mnt_ns
, zfs_i_user_ns(ip
));
2190 trim_mask
|= ATTR_MODE
;
2198 * If trim_mask is set then take ownership
2199 * has been granted or write_acl is present and user
2200 * has the ability to modify mode. In that case remove
2201 * UID|GID and or MODE from mask so that
2202 * secpolicy_vnode_setattr() doesn't revoke it.
2206 saved_mask
= vap
->va_mask
;
2207 vap
->va_mask
&= ~trim_mask
;
2209 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2210 zfs_zaccess_unix
, zp
);
2215 vap
->va_mask
|= saved_mask
;
2219 * secpolicy_vnode_setattr, or take ownership may have
2222 mask
= vap
->va_mask
;
2224 if ((mask
& (ATTR_UID
| ATTR_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
2225 handle_eadir
= B_TRUE
;
2226 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
2227 &xattr_obj
, sizeof (xattr_obj
));
2229 if (err
== 0 && xattr_obj
) {
2230 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2234 if (mask
& ATTR_UID
) {
2235 new_kuid
= zfs_fuid_create(zfsvfs
,
2236 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2237 if (new_kuid
!= KUID_TO_SUID(ZTOI(zp
)->i_uid
) &&
2238 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
2242 err
= SET_ERROR(EDQUOT
);
2247 if (mask
& ATTR_GID
) {
2248 new_kgid
= zfs_fuid_create(zfsvfs
,
2249 (uint64_t)vap
->va_gid
, cr
, ZFS_GROUP
, &fuidp
);
2250 if (new_kgid
!= KGID_TO_SGID(ZTOI(zp
)->i_gid
) &&
2251 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
2255 err
= SET_ERROR(EDQUOT
);
2260 if (projid
!= ZFS_INVALID_PROJID
&&
2261 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
2268 tx
= dmu_tx_create(os
);
2270 if (mask
& ATTR_MODE
) {
2271 uint64_t pmode
= zp
->z_mode
;
2273 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2275 if (ZTOZSB(zp
)->z_acl_mode
== ZFS_ACL_RESTRICTED
&&
2276 !(zp
->z_pflags
& ZFS_ACL_TRIVIAL
)) {
2281 if ((err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
)))
2284 mutex_enter(&zp
->z_lock
);
2285 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2287 * Are we upgrading ACL from old V0 format
2290 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
2291 zfs_znode_acl_version(zp
) ==
2292 ZFS_ACL_VERSION_INITIAL
) {
2293 dmu_tx_hold_free(tx
, acl_obj
, 0,
2295 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2296 0, aclp
->z_acl_bytes
);
2298 dmu_tx_hold_write(tx
, acl_obj
, 0,
2301 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2302 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2303 0, aclp
->z_acl_bytes
);
2305 mutex_exit(&zp
->z_lock
);
2306 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2308 if (((mask
& ATTR_XVATTR
) &&
2309 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
2310 (projid
!= ZFS_INVALID_PROJID
&&
2311 !(zp
->z_pflags
& ZFS_PROJID
)))
2312 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2314 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2318 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2321 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2323 zfs_fuid_txhold(zfsvfs
, tx
);
2325 zfs_sa_upgrade_txholds(tx
, zp
);
2327 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2333 * Set each attribute requested.
2334 * We group settings according to the locks they need to acquire.
2336 * Note: you cannot set ctime directly, although it will be
2337 * updated as a side-effect of calling this function.
2340 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
2342 * For the existed object that is upgraded from old system,
2343 * its on-disk layout has no slot for the project ID attribute.
2344 * But quota accounting logic needs to access related slots by
2345 * offset directly. So we need to adjust old objects' layout
2346 * to make the project ID to some unified and fixed offset.
2349 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
2351 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
2353 if (unlikely(err
== EEXIST
))
2358 projid
= ZFS_INVALID_PROJID
;
2361 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2362 mutex_enter(&zp
->z_acl_lock
);
2363 mutex_enter(&zp
->z_lock
);
2365 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
2366 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2369 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2370 mutex_enter(&attrzp
->z_acl_lock
);
2371 mutex_enter(&attrzp
->z_lock
);
2372 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2373 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
2374 sizeof (attrzp
->z_pflags
));
2375 if (projid
!= ZFS_INVALID_PROJID
) {
2376 attrzp
->z_projid
= projid
;
2377 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2378 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
2379 sizeof (attrzp
->z_projid
));
2383 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2385 if (mask
& ATTR_UID
) {
2386 ZTOI(zp
)->i_uid
= SUID_TO_KUID(new_kuid
);
2387 new_uid
= zfs_uid_read(ZTOI(zp
));
2388 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2389 &new_uid
, sizeof (new_uid
));
2391 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2392 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
2394 ZTOI(attrzp
)->i_uid
= SUID_TO_KUID(new_uid
);
2398 if (mask
& ATTR_GID
) {
2399 ZTOI(zp
)->i_gid
= SGID_TO_KGID(new_kgid
);
2400 new_gid
= zfs_gid_read(ZTOI(zp
));
2401 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
2402 NULL
, &new_gid
, sizeof (new_gid
));
2404 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2405 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
2407 ZTOI(attrzp
)->i_gid
= SGID_TO_KGID(new_kgid
);
2410 if (!(mask
& ATTR_MODE
)) {
2411 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
2412 NULL
, &new_mode
, sizeof (new_mode
));
2413 new_mode
= zp
->z_mode
;
2415 err
= zfs_acl_chown_setattr(zp
);
2418 err
= zfs_acl_chown_setattr(attrzp
);
2423 if (mask
& ATTR_MODE
) {
2424 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
2425 &new_mode
, sizeof (new_mode
));
2426 zp
->z_mode
= ZTOI(zp
)->i_mode
= new_mode
;
2427 ASSERT3P(aclp
, !=, NULL
);
2428 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2430 if (zp
->z_acl_cached
)
2431 zfs_acl_free(zp
->z_acl_cached
);
2432 zp
->z_acl_cached
= aclp
;
2436 if ((mask
& ATTR_ATIME
) || zp
->z_atime_dirty
) {
2437 zp
->z_atime_dirty
= B_FALSE
;
2438 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
2439 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
2440 &atime
, sizeof (atime
));
2443 if (mask
& (ATTR_MTIME
| ATTR_SIZE
)) {
2444 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2445 ZTOI(zp
)->i_mtime
= zpl_inode_timestamp_truncate(
2446 vap
->va_mtime
, ZTOI(zp
));
2448 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
2449 mtime
, sizeof (mtime
));
2452 if (mask
& (ATTR_CTIME
| ATTR_SIZE
)) {
2453 ZFS_TIME_ENCODE(&vap
->va_ctime
, ctime
);
2454 zpl_inode_set_ctime_to_ts(ZTOI(zp
),
2455 zpl_inode_timestamp_truncate(vap
->va_ctime
, ZTOI(zp
)));
2456 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2457 ctime
, sizeof (ctime
));
2460 if (projid
!= ZFS_INVALID_PROJID
) {
2461 zp
->z_projid
= projid
;
2462 SA_ADD_BULK_ATTR(bulk
, count
,
2463 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
2464 sizeof (zp
->z_projid
));
2467 if (attrzp
&& mask
) {
2468 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2469 SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
,
2474 * Do this after setting timestamps to prevent timestamp
2475 * update from toggling bit
2478 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2481 * restore trimmed off masks
2482 * so that return masks can be set for caller.
2485 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2486 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2488 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2489 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2491 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2492 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2494 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2495 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2497 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2498 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2500 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
2501 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2503 if (XVA_ISSET_REQ(tmpxvattr
, XAT_PROJINHERIT
)) {
2504 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
2507 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2508 ASSERT(S_ISREG(ip
->i_mode
));
2510 zfs_xvattr_set(zp
, xvap
, tx
);
2514 zfs_fuid_sync(zfsvfs
, tx
);
2517 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2519 mutex_exit(&zp
->z_lock
);
2520 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2521 mutex_exit(&zp
->z_acl_lock
);
2524 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2525 mutex_exit(&attrzp
->z_acl_lock
);
2526 mutex_exit(&attrzp
->z_lock
);
2529 if (err
== 0 && xattr_count
> 0) {
2530 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
2539 zfs_fuid_info_free(fuidp
);
2547 if (err
== ERESTART
)
2551 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2554 if (err2
== 0 && handle_eadir
)
2555 err
= zfs_setattr_dir(attrzp
);
2558 zfs_znode_update_vfs(zp
);
2562 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
2563 zil_commit(zilog
, 0);
2566 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
2567 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
2568 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
2569 zfs_exit(zfsvfs
, FTAG
);
2573 typedef struct zfs_zlock
{
2574 krwlock_t
*zl_rwlock
; /* lock we acquired */
2575 znode_t
*zl_znode
; /* znode we held */
2576 struct zfs_zlock
*zl_next
; /* next in list */
2580 * Drop locks and release vnodes that were held by zfs_rename_lock().
2583 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
2587 while ((zl
= *zlpp
) != NULL
) {
2588 if (zl
->zl_znode
!= NULL
)
2589 zfs_zrele_async(zl
->zl_znode
);
2590 rw_exit(zl
->zl_rwlock
);
2591 *zlpp
= zl
->zl_next
;
2592 kmem_free(zl
, sizeof (*zl
));
2597 * Search back through the directory tree, using the ".." entries.
2598 * Lock each directory in the chain to prevent concurrent renames.
2599 * Fail any attempt to move a directory into one of its own descendants.
2600 * XXX - z_parent_lock can overlap with map or grow locks
2603 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
2607 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
2608 uint64_t oidp
= zp
->z_id
;
2609 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
2610 krw_t rw
= RW_WRITER
;
2613 * First pass write-locks szp and compares to zp->z_id.
2614 * Later passes read-lock zp and compare to zp->z_parent.
2617 if (!rw_tryenter(rwlp
, rw
)) {
2619 * Another thread is renaming in this path.
2620 * Note that if we are a WRITER, we don't have any
2621 * parent_locks held yet.
2623 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
2625 * Drop our locks and restart
2627 zfs_rename_unlock(&zl
);
2631 rwlp
= &szp
->z_parent_lock
;
2636 * Wait for other thread to drop its locks
2642 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
2643 zl
->zl_rwlock
= rwlp
;
2644 zl
->zl_znode
= NULL
;
2645 zl
->zl_next
= *zlpp
;
2648 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
2649 return (SET_ERROR(EINVAL
));
2651 if (oidp
== rootid
) /* We've hit the top */
2654 if (rw
== RW_READER
) { /* i.e. not the first pass */
2655 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
2660 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
2661 &oidp
, sizeof (oidp
));
2662 rwlp
= &zp
->z_parent_lock
;
2665 } while (zp
->z_id
!= sdzp
->z_id
);
2671 * Move an entry from the provided source directory to the target
2672 * directory. Change the entry name as indicated.
2674 * IN: sdzp - Source directory containing the "old entry".
2675 * snm - Old entry name.
2676 * tdzp - Target directory to contain the "new entry".
2677 * tnm - New entry name.
2678 * cr - credentials of caller.
2679 * flags - case flags
2680 * rflags - RENAME_* flags
2681 * wa_vap - attributes for RENAME_WHITEOUT (must be a char 0:0).
2682 * mnt_ns - user namespace of the mount
2684 * RETURN: 0 on success, error code on failure.
2687 * sdzp,tdzp - ctime|mtime updated
2690 zfs_rename(znode_t
*sdzp
, char *snm
, znode_t
*tdzp
, char *tnm
,
2691 cred_t
*cr
, int flags
, uint64_t rflags
, vattr_t
*wo_vap
, zidmap_t
*mnt_ns
)
2694 zfsvfs_t
*zfsvfs
= ZTOZSB(sdzp
);
2696 zfs_dirlock_t
*sdl
, *tdl
;
2699 int cmp
, serr
, terr
;
2702 boolean_t waited
= B_FALSE
;
2703 /* Needed for whiteout inode creation. */
2704 boolean_t fuid_dirtied
;
2705 zfs_acl_ids_t acl_ids
;
2706 boolean_t have_acl
= B_FALSE
;
2707 znode_t
*wzp
= NULL
;
2710 if (snm
== NULL
|| tnm
== NULL
)
2711 return (SET_ERROR(EINVAL
));
2713 if (rflags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
2714 return (SET_ERROR(EINVAL
));
2716 /* Already checked by Linux VFS, but just to make sure. */
2717 if (rflags
& RENAME_EXCHANGE
&&
2718 (rflags
& (RENAME_NOREPLACE
| RENAME_WHITEOUT
)))
2719 return (SET_ERROR(EINVAL
));
2722 * Make sure we only get wo_vap iff. RENAME_WHITEOUT and that it's the
2723 * right kind of vattr_t for the whiteout file. These are set
2724 * internally by ZFS so should never be incorrect.
2726 VERIFY_EQUIV(rflags
& RENAME_WHITEOUT
, wo_vap
!= NULL
);
2727 VERIFY_IMPLY(wo_vap
, wo_vap
->va_mode
== S_IFCHR
);
2728 VERIFY_IMPLY(wo_vap
, wo_vap
->va_rdev
== makedevice(0, 0));
2730 if ((error
= zfs_enter_verify_zp(zfsvfs
, sdzp
, FTAG
)) != 0)
2732 zilog
= zfsvfs
->z_log
;
2734 if ((error
= zfs_verify_zp(tdzp
)) != 0) {
2735 zfs_exit(zfsvfs
, FTAG
);
2740 * We check i_sb because snapshots and the ctldir must have different
2743 if (ZTOI(tdzp
)->i_sb
!= ZTOI(sdzp
)->i_sb
||
2744 zfsctl_is_node(ZTOI(tdzp
))) {
2745 zfs_exit(zfsvfs
, FTAG
);
2746 return (SET_ERROR(EXDEV
));
2749 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
2750 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
2751 zfs_exit(zfsvfs
, FTAG
);
2752 return (SET_ERROR(EILSEQ
));
2755 if (flags
& FIGNORECASE
)
2764 * This is to prevent the creation of links into attribute space
2765 * by renaming a linked file into/outof an attribute directory.
2766 * See the comment in zfs_link() for why this is considered bad.
2768 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
2769 zfs_exit(zfsvfs
, FTAG
);
2770 return (SET_ERROR(EINVAL
));
2774 * Lock source and target directory entries. To prevent deadlock,
2775 * a lock ordering must be defined. We lock the directory with
2776 * the smallest object id first, or if it's a tie, the one with
2777 * the lexically first name.
2779 if (sdzp
->z_id
< tdzp
->z_id
) {
2781 } else if (sdzp
->z_id
> tdzp
->z_id
) {
2785 * First compare the two name arguments without
2786 * considering any case folding.
2788 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
2790 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
2791 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
2794 * POSIX: "If the old argument and the new argument
2795 * both refer to links to the same existing file,
2796 * the rename() function shall return successfully
2797 * and perform no other action."
2799 zfs_exit(zfsvfs
, FTAG
);
2803 * If the file system is case-folding, then we may
2804 * have some more checking to do. A case-folding file
2805 * system is either supporting mixed case sensitivity
2806 * access or is completely case-insensitive. Note
2807 * that the file system is always case preserving.
2809 * In mixed sensitivity mode case sensitive behavior
2810 * is the default. FIGNORECASE must be used to
2811 * explicitly request case insensitive behavior.
2813 * If the source and target names provided differ only
2814 * by case (e.g., a request to rename 'tim' to 'Tim'),
2815 * we will treat this as a special case in the
2816 * case-insensitive mode: as long as the source name
2817 * is an exact match, we will allow this to proceed as
2818 * a name-change request.
2820 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
2821 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
2822 flags
& FIGNORECASE
)) &&
2823 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
2826 * case preserving rename request, require exact
2835 * If the source and destination directories are the same, we should
2836 * grab the z_name_lock of that directory only once.
2840 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
2844 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
2845 ZEXISTS
| zflg
, NULL
, NULL
);
2846 terr
= zfs_dirent_lock(&tdl
,
2847 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
2849 terr
= zfs_dirent_lock(&tdl
,
2850 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
2851 serr
= zfs_dirent_lock(&sdl
,
2852 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
2858 * Source entry invalid or not there.
2861 zfs_dirent_unlock(tdl
);
2867 rw_exit(&sdzp
->z_name_lock
);
2869 if (strcmp(snm
, "..") == 0)
2871 zfs_exit(zfsvfs
, FTAG
);
2875 zfs_dirent_unlock(sdl
);
2879 rw_exit(&sdzp
->z_name_lock
);
2881 if (strcmp(tnm
, "..") == 0)
2883 zfs_exit(zfsvfs
, FTAG
);
2888 * If we are using project inheritance, means if the directory has
2889 * ZFS_PROJINHERIT set, then its descendant directories will inherit
2890 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
2891 * such case, we only allow renames into our tree when the project
2894 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
2895 tdzp
->z_projid
!= szp
->z_projid
) {
2896 error
= SET_ERROR(EXDEV
);
2901 * Must have write access at the source to remove the old entry
2902 * and write access at the target to create the new entry.
2903 * Note that if target and source are the same, this can be
2904 * done in a single check.
2906 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
, mnt_ns
)))
2909 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
2911 * Check to make sure rename is valid.
2912 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2914 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
2919 * Does target exist?
2922 if (rflags
& RENAME_NOREPLACE
) {
2923 error
= SET_ERROR(EEXIST
);
2927 * Source and target must be the same type (unless exchanging).
2929 if (!(rflags
& RENAME_EXCHANGE
)) {
2930 boolean_t s_is_dir
= S_ISDIR(ZTOI(szp
)->i_mode
) != 0;
2931 boolean_t t_is_dir
= S_ISDIR(ZTOI(tzp
)->i_mode
) != 0;
2933 if (s_is_dir
!= t_is_dir
) {
2934 error
= SET_ERROR(s_is_dir
? ENOTDIR
: EISDIR
);
2939 * POSIX dictates that when the source and target
2940 * entries refer to the same file object, rename
2941 * must do nothing and exit without error.
2943 if (szp
->z_id
== tzp
->z_id
) {
2947 } else if (rflags
& RENAME_EXCHANGE
) {
2948 /* Target must exist for RENAME_EXCHANGE. */
2949 error
= SET_ERROR(ENOENT
);
2953 /* Set up inode creation for RENAME_WHITEOUT. */
2954 if (rflags
& RENAME_WHITEOUT
) {
2956 * Whiteout files are not regular files or directories, so to
2957 * match zfs_create() we do not inherit the project id.
2959 uint64_t wo_projid
= ZFS_DEFAULT_PROJID
;
2961 error
= zfs_zaccess(sdzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, mnt_ns
);
2966 error
= zfs_acl_ids_create(sdzp
, 0, wo_vap
, cr
, NULL
,
2973 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, wo_projid
)) {
2974 error
= SET_ERROR(EDQUOT
);
2979 tx
= dmu_tx_create(zfsvfs
->z_os
);
2980 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
2981 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
2982 dmu_tx_hold_zap(tx
, sdzp
->z_id
,
2983 (rflags
& RENAME_EXCHANGE
) ? TRUE
: FALSE
, snm
);
2984 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
2986 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
2987 zfs_sa_upgrade_txholds(tx
, tdzp
);
2990 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
2991 zfs_sa_upgrade_txholds(tx
, tzp
);
2993 if (rflags
& RENAME_WHITEOUT
) {
2994 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
2995 ZFS_SA_BASE_ATTR_SIZE
);
2997 dmu_tx_hold_zap(tx
, sdzp
->z_id
, TRUE
, snm
);
2998 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
2999 if (!zfsvfs
->z_use_sa
&&
3000 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3001 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
3002 0, acl_ids
.z_aclp
->z_acl_bytes
);
3005 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3007 zfs_fuid_txhold(zfsvfs
, tx
);
3008 zfs_sa_upgrade_txholds(tx
, szp
);
3009 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3010 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3013 zfs_rename_unlock(&zl
);
3014 zfs_dirent_unlock(sdl
);
3015 zfs_dirent_unlock(tdl
);
3018 rw_exit(&sdzp
->z_name_lock
);
3020 if (error
== ERESTART
) {
3033 zfs_exit(zfsvfs
, FTAG
);
3038 * Unlink the source.
3040 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3041 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
)
3042 szp
->z_pflags
|= ZFS_PROJINHERIT
;
3044 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3045 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3048 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3053 * Unlink the target.
3058 if (rflags
& RENAME_EXCHANGE
) {
3059 /* This inode will be re-linked soon. */
3062 tzp
->z_pflags
|= ZFS_AV_MODIFIED
;
3063 if (sdzp
->z_pflags
& ZFS_PROJINHERIT
)
3064 tzp
->z_pflags
|= ZFS_PROJINHERIT
;
3066 error
= sa_update(tzp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3067 (void *)&tzp
->z_pflags
, sizeof (uint64_t), tx
);
3070 error
= zfs_link_destroy(tdl
, tzp
, tx
, tzflg
, NULL
);
3072 goto commit_link_szp
;
3076 * Create the new target links:
3077 * * We always link the target.
3078 * * RENAME_EXCHANGE: Link the old target to the source.
3079 * * RENAME_WHITEOUT: Create a whiteout inode in-place of the source.
3081 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3084 * If we have removed the existing target, a subsequent call to
3085 * zfs_link_create() to add back the same entry, but with a new
3086 * dnode (szp), should not fail.
3088 ASSERT3P(tzp
, ==, NULL
);
3089 goto commit_link_tzp
;
3092 switch (rflags
& (RENAME_EXCHANGE
| RENAME_WHITEOUT
)) {
3093 case RENAME_EXCHANGE
:
3094 error
= zfs_link_create(sdl
, tzp
, tx
, ZRENAMING
);
3096 * The same argument as zfs_link_create() failing for
3097 * szp applies here, since the source directory must
3098 * have had an entry we are replacing.
3102 goto commit_unlink_td_szp
;
3104 case RENAME_WHITEOUT
:
3105 zfs_mknode(sdzp
, wo_vap
, tx
, cr
, 0, &wzp
, &acl_ids
);
3106 error
= zfs_link_create(sdl
, wzp
, tx
, ZNEW
);
3108 zfs_znode_delete(wzp
, tx
);
3109 remove_inode_hash(ZTOI(wzp
));
3110 goto commit_unlink_td_szp
;
3116 zfs_fuid_sync(zfsvfs
, tx
);
3118 switch (rflags
& (RENAME_EXCHANGE
| RENAME_WHITEOUT
)) {
3119 case RENAME_EXCHANGE
:
3120 zfs_log_rename_exchange(zilog
, tx
,
3121 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
, sdl
->dl_name
,
3122 tdzp
, tdl
->dl_name
, szp
);
3124 case RENAME_WHITEOUT
:
3125 zfs_log_rename_whiteout(zilog
, tx
,
3126 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
, sdl
->dl_name
,
3127 tdzp
, tdl
->dl_name
, szp
, wzp
);
3130 ASSERT0(rflags
& ~RENAME_NOREPLACE
);
3131 zfs_log_rename(zilog
, tx
, (flags
& FIGNORECASE
? TX_CI
: 0),
3132 sdzp
, sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3140 zfs_acl_ids_free(&acl_ids
);
3142 zfs_znode_update_vfs(sdzp
);
3144 rw_exit(&sdzp
->z_name_lock
);
3147 zfs_znode_update_vfs(tdzp
);
3149 zfs_znode_update_vfs(szp
);
3152 zfs_znode_update_vfs(wzp
);
3156 zfs_znode_update_vfs(tzp
);
3161 zfs_rename_unlock(&zl
);
3163 zfs_dirent_unlock(sdl
);
3164 zfs_dirent_unlock(tdl
);
3166 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3167 zil_commit(zilog
, 0);
3169 zfs_exit(zfsvfs
, FTAG
);
3173 * Clean-up path for broken link state.
3175 * At this point we are in a (very) bad state, so we need to do our
3176 * best to correct the state. In particular, all of the nlinks are
3177 * wrong because we were destroying and creating links with ZRENAMING.
3179 * In some form, all of these operations have to resolve the state:
3181 * * link_destroy() *must* succeed. Fortunately, this is very likely
3182 * since we only just created it.
3184 * * link_create()s are allowed to fail (though they shouldn't because
3185 * we only just unlinked them and are putting the entries back
3186 * during clean-up). But if they fail, we can just forcefully drop
3187 * the nlink value to (at the very least) avoid broken nlink values
3188 * -- though in the case of non-empty directories we will have to
3189 * panic (otherwise we'd have a leaked directory with a broken ..).
3191 commit_unlink_td_szp
:
3192 VERIFY0(zfs_link_destroy(tdl
, szp
, tx
, ZRENAMING
, NULL
));
3195 if (zfs_link_create(tdl
, tzp
, tx
, ZRENAMING
))
3196 VERIFY0(zfs_drop_nlink(tzp
, tx
, NULL
));
3199 if (zfs_link_create(sdl
, szp
, tx
, ZRENAMING
))
3200 VERIFY0(zfs_drop_nlink(szp
, tx
, NULL
));
3205 * Insert the indicated symbolic reference entry into the directory.
3207 * IN: dzp - Directory to contain new symbolic link.
3208 * name - Name of directory entry in dip.
3209 * vap - Attributes of new entry.
3210 * link - Name for new symlink entry.
3211 * cr - credentials of caller.
3212 * flags - case flags
3213 * mnt_ns - user namespace of the mount
3215 * OUT: zpp - Znode for new symbolic link.
3217 * RETURN: 0 on success, error code on failure.
3220 * dip - ctime|mtime updated
3223 zfs_symlink(znode_t
*dzp
, char *name
, vattr_t
*vap
, char *link
,
3224 znode_t
**zpp
, cred_t
*cr
, int flags
, zidmap_t
*mnt_ns
)
3229 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
3231 uint64_t len
= strlen(link
);
3234 zfs_acl_ids_t acl_ids
;
3235 boolean_t fuid_dirtied
;
3236 uint64_t txtype
= TX_SYMLINK
;
3237 boolean_t waited
= B_FALSE
;
3239 ASSERT(S_ISLNK(vap
->va_mode
));
3242 return (SET_ERROR(EINVAL
));
3244 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
3246 zilog
= zfsvfs
->z_log
;
3248 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3249 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3250 zfs_exit(zfsvfs
, FTAG
);
3251 return (SET_ERROR(EILSEQ
));
3253 if (flags
& FIGNORECASE
)
3256 if (len
> MAXPATHLEN
) {
3257 zfs_exit(zfsvfs
, FTAG
);
3258 return (SET_ERROR(ENAMETOOLONG
));
3261 if ((error
= zfs_acl_ids_create(dzp
, 0,
3262 vap
, cr
, NULL
, &acl_ids
, mnt_ns
)) != 0) {
3263 zfs_exit(zfsvfs
, FTAG
);
3270 * Attempt to lock directory; fail if entry already exists.
3272 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3274 zfs_acl_ids_free(&acl_ids
);
3275 zfs_exit(zfsvfs
, FTAG
);
3279 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, mnt_ns
))) {
3280 zfs_acl_ids_free(&acl_ids
);
3281 zfs_dirent_unlock(dl
);
3282 zfs_exit(zfsvfs
, FTAG
);
3286 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, ZFS_DEFAULT_PROJID
)) {
3287 zfs_acl_ids_free(&acl_ids
);
3288 zfs_dirent_unlock(dl
);
3289 zfs_exit(zfsvfs
, FTAG
);
3290 return (SET_ERROR(EDQUOT
));
3292 tx
= dmu_tx_create(zfsvfs
->z_os
);
3293 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3294 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3295 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3296 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3297 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3298 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3299 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3300 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3301 acl_ids
.z_aclp
->z_acl_bytes
);
3304 zfs_fuid_txhold(zfsvfs
, tx
);
3305 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3307 zfs_dirent_unlock(dl
);
3308 if (error
== ERESTART
) {
3314 zfs_acl_ids_free(&acl_ids
);
3316 zfs_exit(zfsvfs
, FTAG
);
3321 * Create a new object for the symlink.
3322 * for version 4 ZPL datasets the symlink will be an SA attribute
3324 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3327 zfs_fuid_sync(zfsvfs
, tx
);
3329 mutex_enter(&zp
->z_lock
);
3331 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
3334 zfs_sa_symlink(zp
, link
, len
, tx
);
3335 mutex_exit(&zp
->z_lock
);
3338 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
3339 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3341 * Insert the new object into the directory.
3343 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
3345 zfs_znode_delete(zp
, tx
);
3346 remove_inode_hash(ZTOI(zp
));
3348 if (flags
& FIGNORECASE
)
3350 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3352 zfs_znode_update_vfs(dzp
);
3353 zfs_znode_update_vfs(zp
);
3356 zfs_acl_ids_free(&acl_ids
);
3360 zfs_dirent_unlock(dl
);
3365 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3366 zil_commit(zilog
, 0);
3371 zfs_exit(zfsvfs
, FTAG
);
3376 * Return, in the buffer contained in the provided uio structure,
3377 * the symbolic path referred to by ip.
3379 * IN: ip - inode of symbolic link
3380 * uio - structure to contain the link path.
3381 * cr - credentials of caller.
3383 * RETURN: 0 if success
3384 * error code if failure
3387 * ip - atime updated
3390 zfs_readlink(struct inode
*ip
, zfs_uio_t
*uio
, cred_t
*cr
)
3393 znode_t
*zp
= ITOZ(ip
);
3394 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3397 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3400 mutex_enter(&zp
->z_lock
);
3402 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3403 SA_ZPL_SYMLINK(zfsvfs
), uio
);
3405 error
= zfs_sa_readlink(zp
, uio
);
3406 mutex_exit(&zp
->z_lock
);
3408 zfs_exit(zfsvfs
, FTAG
);
3413 * Insert a new entry into directory tdzp referencing szp.
3415 * IN: tdzp - Directory to contain new entry.
3416 * szp - znode of new entry.
3417 * name - name of new entry.
3418 * cr - credentials of caller.
3419 * flags - case flags.
3421 * RETURN: 0 if success
3422 * error code if failure
3425 * tdzp - ctime|mtime updated
3426 * szp - ctime updated
3429 zfs_link(znode_t
*tdzp
, znode_t
*szp
, char *name
, cred_t
*cr
,
3432 struct inode
*sip
= ZTOI(szp
);
3434 zfsvfs_t
*zfsvfs
= ZTOZSB(tdzp
);
3442 boolean_t waited
= B_FALSE
;
3443 boolean_t is_tmpfile
= 0;
3446 is_tmpfile
= (sip
->i_nlink
== 0 && (sip
->i_state
& I_LINKABLE
));
3448 ASSERT(S_ISDIR(ZTOI(tdzp
)->i_mode
));
3451 return (SET_ERROR(EINVAL
));
3453 if ((error
= zfs_enter_verify_zp(zfsvfs
, tdzp
, FTAG
)) != 0)
3455 zilog
= zfsvfs
->z_log
;
3458 * POSIX dictates that we return EPERM here.
3459 * Better choices include ENOTSUP or EISDIR.
3461 if (S_ISDIR(sip
->i_mode
)) {
3462 zfs_exit(zfsvfs
, FTAG
);
3463 return (SET_ERROR(EPERM
));
3466 if ((error
= zfs_verify_zp(szp
)) != 0) {
3467 zfs_exit(zfsvfs
, FTAG
);
3472 * If we are using project inheritance, means if the directory has
3473 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3474 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3475 * such case, we only allow hard link creation in our tree when the
3476 * project IDs are the same.
3478 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3479 tdzp
->z_projid
!= szp
->z_projid
) {
3480 zfs_exit(zfsvfs
, FTAG
);
3481 return (SET_ERROR(EXDEV
));
3485 * We check i_sb because snapshots and the ctldir must have different
3488 if (sip
->i_sb
!= ZTOI(tdzp
)->i_sb
|| zfsctl_is_node(sip
)) {
3489 zfs_exit(zfsvfs
, FTAG
);
3490 return (SET_ERROR(EXDEV
));
3493 /* Prevent links to .zfs/shares files */
3495 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
3496 &parent
, sizeof (uint64_t))) != 0) {
3497 zfs_exit(zfsvfs
, FTAG
);
3500 if (parent
== zfsvfs
->z_shares_dir
) {
3501 zfs_exit(zfsvfs
, FTAG
);
3502 return (SET_ERROR(EPERM
));
3505 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3506 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3507 zfs_exit(zfsvfs
, FTAG
);
3508 return (SET_ERROR(EILSEQ
));
3510 if (flags
& FIGNORECASE
)
3514 * We do not support links between attributes and non-attributes
3515 * because of the potential security risk of creating links
3516 * into "normal" file space in order to circumvent restrictions
3517 * imposed in attribute space.
3519 if ((szp
->z_pflags
& ZFS_XATTR
) != (tdzp
->z_pflags
& ZFS_XATTR
)) {
3520 zfs_exit(zfsvfs
, FTAG
);
3521 return (SET_ERROR(EINVAL
));
3524 owner
= zfs_fuid_map_id(zfsvfs
, KUID_TO_SUID(sip
->i_uid
),
3526 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3527 zfs_exit(zfsvfs
, FTAG
);
3528 return (SET_ERROR(EPERM
));
3531 if ((error
= zfs_zaccess(tdzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
,
3533 zfs_exit(zfsvfs
, FTAG
);
3539 * Attempt to lock directory; fail if entry already exists.
3541 error
= zfs_dirent_lock(&dl
, tdzp
, name
, &tzp
, zf
, NULL
, NULL
);
3543 zfs_exit(zfsvfs
, FTAG
);
3547 tx
= dmu_tx_create(zfsvfs
->z_os
);
3548 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3549 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, name
);
3551 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3553 zfs_sa_upgrade_txholds(tx
, szp
);
3554 zfs_sa_upgrade_txholds(tx
, tdzp
);
3555 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3557 zfs_dirent_unlock(dl
);
3558 if (error
== ERESTART
) {
3565 zfs_exit(zfsvfs
, FTAG
);
3568 /* unmark z_unlinked so zfs_link_create will not reject */
3570 szp
->z_unlinked
= B_FALSE
;
3571 error
= zfs_link_create(dl
, szp
, tx
, 0);
3574 uint64_t txtype
= TX_LINK
;
3576 * tmpfile is created to be in z_unlinkedobj, so remove it.
3577 * Also, we don't log in ZIL, because all previous file
3578 * operation on the tmpfile are ignored by ZIL. Instead we
3579 * always wait for txg to sync to make sure all previous
3580 * operation are sync safe.
3583 VERIFY(zap_remove_int(zfsvfs
->z_os
,
3584 zfsvfs
->z_unlinkedobj
, szp
->z_id
, tx
) == 0);
3586 if (flags
& FIGNORECASE
)
3588 zfs_log_link(zilog
, tx
, txtype
, tdzp
, szp
, name
);
3590 } else if (is_tmpfile
) {
3591 /* restore z_unlinked since when linking failed */
3592 szp
->z_unlinked
= B_TRUE
;
3594 txg
= dmu_tx_get_txg(tx
);
3597 zfs_dirent_unlock(dl
);
3599 if (!is_tmpfile
&& zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3600 zil_commit(zilog
, 0);
3602 if (is_tmpfile
&& zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
)
3603 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), txg
);
3605 zfs_znode_update_vfs(tdzp
);
3606 zfs_znode_update_vfs(szp
);
3607 zfs_exit(zfsvfs
, FTAG
);
3612 zfs_putpage_sync_commit_cb(void *arg
)
3614 struct page
*pp
= arg
;
3617 end_page_writeback(pp
);
3621 zfs_putpage_async_commit_cb(void *arg
)
3623 struct page
*pp
= arg
;
3624 znode_t
*zp
= ITOZ(pp
->mapping
->host
);
3627 end_page_writeback(pp
);
3628 atomic_dec_32(&zp
->z_async_writes_cnt
);
3632 * Push a page out to disk, once the page is on stable storage the
3633 * registered commit callback will be run as notification of completion.
3635 * IN: ip - page mapped for inode.
3636 * pp - page to push (page is locked)
3637 * wbc - writeback control data
3638 * for_sync - does the caller intend to wait synchronously for the
3639 * page writeback to complete?
3641 * RETURN: 0 if success
3642 * error code if failure
3645 * ip - ctime|mtime updated
3648 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
,
3651 znode_t
*zp
= ITOZ(ip
);
3652 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3659 uint64_t mtime
[2], ctime
[2];
3660 inode_timespec_t tmp_ctime
;
3661 sa_bulk_attr_t bulk
[3];
3663 struct address_space
*mapping
;
3665 if ((err
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3668 ASSERT(PageLocked(pp
));
3670 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3671 offset
= i_size_read(ip
); /* File length in bytes */
3672 pglen
= MIN(PAGE_SIZE
, /* Page length in bytes */
3673 P2ROUNDUP(offset
, PAGE_SIZE
)-pgoff
);
3675 /* Page is beyond end of file */
3676 if (pgoff
>= offset
) {
3678 zfs_exit(zfsvfs
, FTAG
);
3682 /* Truncate page length to end of file */
3683 if (pgoff
+ pglen
> offset
)
3684 pglen
= offset
- pgoff
;
3688 * FIXME: Allow mmap writes past its quota. The correct fix
3689 * is to register a page_mkwrite() handler to count the page
3690 * against its quota when it is about to be dirtied.
3692 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
3693 KUID_TO_SUID(ip
->i_uid
)) ||
3694 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
3695 KGID_TO_SGID(ip
->i_gid
)) ||
3696 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
3697 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
3704 * The ordering here is critical and must adhere to the following
3705 * rules in order to avoid deadlocking in either zfs_read() or
3706 * zfs_free_range() due to a lock inversion.
3708 * 1) The page must be unlocked prior to acquiring the range lock.
3709 * This is critical because zfs_read() calls find_lock_page()
3710 * which may block on the page lock while holding the range lock.
3712 * 2) Before setting or clearing write back on a page the range lock
3713 * must be held in order to prevent a lock inversion with the
3714 * zfs_free_range() function.
3716 * This presents a problem because upon entering this function the
3717 * page lock is already held. To safely acquire the range lock the
3718 * page lock must be dropped. This creates a window where another
3719 * process could truncate, invalidate, dirty, or write out the page.
3721 * Therefore, after successfully reacquiring the range and page locks
3722 * the current page state is checked. In the common case everything
3723 * will be as is expected and it can be written out. However, if
3724 * the page state has changed it must be handled accordingly.
3726 mapping
= pp
->mapping
;
3727 redirty_page_for_writepage(wbc
, pp
);
3730 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
3731 pgoff
, pglen
, RL_WRITER
);
3734 /* Page mapping changed or it was no longer dirty, we're done */
3735 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
3737 zfs_rangelock_exit(lr
);
3738 zfs_exit(zfsvfs
, FTAG
);
3742 /* Another process started write block if required */
3743 if (PageWriteback(pp
)) {
3745 zfs_rangelock_exit(lr
);
3747 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3749 * Speed up any non-sync page writebacks since
3750 * they may take several seconds to complete.
3751 * Refer to the comment in zpl_fsync() (when
3752 * HAVE_FSYNC_RANGE is defined) for details.
3754 if (atomic_load_32(&zp
->z_async_writes_cnt
) > 0) {
3755 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
3758 if (PageWriteback(pp
))
3759 #ifdef HAVE_PAGEMAP_FOLIO_WAIT_BIT
3760 folio_wait_bit(page_folio(pp
), PG_writeback
);
3762 wait_on_page_bit(pp
, PG_writeback
);
3766 zfs_exit(zfsvfs
, FTAG
);
3770 /* Clear the dirty flag the required locks are held */
3771 if (!clear_page_dirty_for_io(pp
)) {
3773 zfs_rangelock_exit(lr
);
3774 zfs_exit(zfsvfs
, FTAG
);
3779 * Counterpart for redirty_page_for_writepage() above. This page
3780 * was in fact not skipped and should not be counted as if it were.
3782 wbc
->pages_skipped
--;
3784 atomic_inc_32(&zp
->z_async_writes_cnt
);
3785 set_page_writeback(pp
);
3788 tx
= dmu_tx_create(zfsvfs
->z_os
);
3789 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3790 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3791 zfs_sa_upgrade_txholds(tx
, zp
);
3793 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3795 if (err
== ERESTART
)
3799 #ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
3800 filemap_dirty_folio(page_mapping(pp
), page_folio(pp
));
3802 __set_page_dirty_nobuffers(pp
);
3805 end_page_writeback(pp
);
3807 atomic_dec_32(&zp
->z_async_writes_cnt
);
3808 zfs_rangelock_exit(lr
);
3809 zfs_exit(zfsvfs
, FTAG
);
3814 ASSERT3U(pglen
, <=, PAGE_SIZE
);
3815 dmu_write(zfsvfs
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
3818 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
3819 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
3820 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
3823 /* Preserve the mtime and ctime provided by the inode */
3824 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3825 tmp_ctime
= zpl_inode_get_ctime(ip
);
3826 ZFS_TIME_ENCODE(&tmp_ctime
, ctime
);
3827 zp
->z_atime_dirty
= B_FALSE
;
3830 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3832 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
3833 for_sync
? zfs_putpage_sync_commit_cb
:
3834 zfs_putpage_async_commit_cb
, pp
);
3838 zfs_rangelock_exit(lr
);
3840 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3842 * Note that this is rarely called under writepages(), because
3843 * writepages() normally handles the entire commit for
3844 * performance reasons.
3846 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
3847 } else if (!for_sync
&& atomic_load_32(&zp
->z_sync_writes_cnt
) > 0) {
3849 * If the caller does not intend to wait synchronously
3850 * for this page writeback to complete and there are active
3851 * synchronous calls on this file, do a commit so that
3852 * the latter don't accidentally end up waiting for
3853 * our writeback to complete. Refer to the comment in
3854 * zpl_fsync() (when HAVE_FSYNC_RANGE is defined) for details.
3856 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
3859 dataset_kstats_update_write_kstats(&zfsvfs
->z_kstat
, pglen
);
3861 zfs_exit(zfsvfs
, FTAG
);
3866 * Update the system attributes when the inode has been dirtied. For the
3867 * moment we only update the mode, atime, mtime, and ctime.
3870 zfs_dirty_inode(struct inode
*ip
, int flags
)
3872 znode_t
*zp
= ITOZ(ip
);
3873 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3875 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
3876 inode_timespec_t tmp_ctime
;
3877 sa_bulk_attr_t bulk
[4];
3881 if (zfs_is_readonly(zfsvfs
) || dmu_objset_is_snapshot(zfsvfs
->z_os
))
3884 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3889 * This is the lazytime semantic introduced in Linux 4.0
3890 * This flag will only be called from update_time when lazytime is set.
3891 * (Note, I_DIRTY_SYNC will also set if not lazytime)
3892 * Fortunately mtime and ctime are managed within ZFS itself, so we
3893 * only need to dirty atime.
3895 if (flags
== I_DIRTY_TIME
) {
3896 zp
->z_atime_dirty
= B_TRUE
;
3901 tx
= dmu_tx_create(zfsvfs
->z_os
);
3903 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3904 zfs_sa_upgrade_txholds(tx
, zp
);
3906 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3912 mutex_enter(&zp
->z_lock
);
3913 zp
->z_atime_dirty
= B_FALSE
;
3915 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
3916 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
3917 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
3918 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
3920 /* Preserve the mode, mtime and ctime provided by the inode */
3921 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3922 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3923 tmp_ctime
= zpl_inode_get_ctime(ip
);
3924 ZFS_TIME_ENCODE(&tmp_ctime
, ctime
);
3929 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3930 mutex_exit(&zp
->z_lock
);
3934 zfs_exit(zfsvfs
, FTAG
);
3939 zfs_inactive(struct inode
*ip
)
3941 znode_t
*zp
= ITOZ(ip
);
3942 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3945 int need_unlock
= 0;
3947 /* Only read lock if we haven't already write locked, e.g. rollback */
3948 if (!RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
)) {
3950 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
3952 if (zp
->z_sa_hdl
== NULL
) {
3954 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3958 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== B_FALSE
) {
3959 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
3961 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3962 zfs_sa_upgrade_txholds(tx
, zp
);
3963 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3967 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3968 mutex_enter(&zp
->z_lock
);
3969 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
3970 (void *)&atime
, sizeof (atime
), tx
);
3971 zp
->z_atime_dirty
= B_FALSE
;
3972 mutex_exit(&zp
->z_lock
);
3979 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3983 * Fill pages with data from the disk.
3986 zfs_fillpage(struct inode
*ip
, struct page
*pp
)
3988 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3989 loff_t i_size
= i_size_read(ip
);
3990 u_offset_t io_off
= page_offset(pp
);
3991 size_t io_len
= PAGE_SIZE
;
3993 ASSERT3U(io_off
, <, i_size
);
3995 if (io_off
+ io_len
> i_size
)
3996 io_len
= i_size
- io_off
;
3998 void *va
= kmap(pp
);
3999 int error
= dmu_read(zfsvfs
->z_os
, ITOZ(ip
)->z_id
, io_off
,
4000 io_len
, va
, DMU_READ_PREFETCH
);
4001 if (io_len
!= PAGE_SIZE
)
4002 memset((char *)va
+ io_len
, 0, PAGE_SIZE
- io_len
);
4006 /* convert checksum errors into IO errors */
4007 if (error
== ECKSUM
)
4008 error
= SET_ERROR(EIO
);
4011 ClearPageUptodate(pp
);
4014 SetPageUptodate(pp
);
4021 * Uses zfs_fillpage to read data from the file and fill the page.
4023 * IN: ip - inode of file to get data from.
4026 * RETURN: 0 on success, error code on failure.
4029 * vp - atime updated
4032 zfs_getpage(struct inode
*ip
, struct page
*pp
)
4034 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4035 znode_t
*zp
= ITOZ(ip
);
4038 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
4041 error
= zfs_fillpage(ip
, pp
);
4043 dataset_kstats_update_read_kstats(&zfsvfs
->z_kstat
, PAGE_SIZE
);
4045 zfs_exit(zfsvfs
, FTAG
);
4051 * Check ZFS specific permissions to memory map a section of a file.
4053 * IN: ip - inode of the file to mmap
4055 * addrp - start address in memory region
4056 * len - length of memory region
4057 * vm_flags- address flags
4059 * RETURN: 0 if success
4060 * error code if failure
4063 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4064 unsigned long vm_flags
)
4067 znode_t
*zp
= ITOZ(ip
);
4068 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4071 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
4074 if ((vm_flags
& VM_WRITE
) && (vm_flags
& VM_SHARED
) &&
4075 (zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4076 zfs_exit(zfsvfs
, FTAG
);
4077 return (SET_ERROR(EPERM
));
4080 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4081 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4082 zfs_exit(zfsvfs
, FTAG
);
4083 return (SET_ERROR(EACCES
));
4086 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4087 zfs_exit(zfsvfs
, FTAG
);
4088 return (SET_ERROR(ENXIO
));
4091 zfs_exit(zfsvfs
, FTAG
);
4096 * Free or allocate space in a file. Currently, this function only
4097 * supports the `F_FREESP' command. However, this command is somewhat
4098 * misnamed, as its functionality includes the ability to allocate as
4099 * well as free space.
4101 * IN: zp - znode of file to free data in.
4102 * cmd - action to take (only F_FREESP supported).
4103 * bfp - section of file to free/alloc.
4104 * flag - current file open mode flags.
4105 * offset - current file offset.
4106 * cr - credentials of caller.
4108 * RETURN: 0 on success, error code on failure.
4111 * zp - ctime|mtime updated
4114 zfs_space(znode_t
*zp
, int cmd
, flock64_t
*bfp
, int flag
,
4115 offset_t offset
, cred_t
*cr
)
4118 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
4122 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
4125 if (cmd
!= F_FREESP
) {
4126 zfs_exit(zfsvfs
, FTAG
);
4127 return (SET_ERROR(EINVAL
));
4131 * Callers might not be able to detect properly that we are read-only,
4132 * so check it explicitly here.
4134 if (zfs_is_readonly(zfsvfs
)) {
4135 zfs_exit(zfsvfs
, FTAG
);
4136 return (SET_ERROR(EROFS
));
4139 if (bfp
->l_len
< 0) {
4140 zfs_exit(zfsvfs
, FTAG
);
4141 return (SET_ERROR(EINVAL
));
4145 * Permissions aren't checked on Solaris because on this OS
4146 * zfs_space() can only be called with an opened file handle.
4147 * On Linux we can get here through truncate_range() which
4148 * operates directly on inodes, so we need to check access rights.
4150 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
,
4152 zfs_exit(zfsvfs
, FTAG
);
4157 len
= bfp
->l_len
; /* 0 means from off to end of file */
4159 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4161 zfs_exit(zfsvfs
, FTAG
);
4166 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4168 znode_t
*zp
= ITOZ(ip
);
4169 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
4172 uint64_t object
= zp
->z_id
;
4176 if ((error
= zfs_enter(zfsvfs
, FTAG
)) != 0)
4179 if (fidp
->fid_len
< SHORT_FID_LEN
) {
4180 fidp
->fid_len
= SHORT_FID_LEN
;
4181 zfs_exit(zfsvfs
, FTAG
);
4182 return (SET_ERROR(ENOSPC
));
4185 if ((error
= zfs_verify_zp(zp
)) != 0) {
4186 zfs_exit(zfsvfs
, FTAG
);
4190 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
4191 &gen64
, sizeof (uint64_t))) != 0) {
4192 zfs_exit(zfsvfs
, FTAG
);
4196 gen
= (uint32_t)gen64
;
4198 size
= SHORT_FID_LEN
;
4200 zfid
= (zfid_short_t
*)fidp
;
4202 zfid
->zf_len
= size
;
4204 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4205 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4207 /* Must have a non-zero generation number to distinguish from .zfs */
4210 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4211 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4213 zfs_exit(zfsvfs
, FTAG
);
4217 #if defined(_KERNEL)
4218 EXPORT_SYMBOL(zfs_open
);
4219 EXPORT_SYMBOL(zfs_close
);
4220 EXPORT_SYMBOL(zfs_lookup
);
4221 EXPORT_SYMBOL(zfs_create
);
4222 EXPORT_SYMBOL(zfs_tmpfile
);
4223 EXPORT_SYMBOL(zfs_remove
);
4224 EXPORT_SYMBOL(zfs_mkdir
);
4225 EXPORT_SYMBOL(zfs_rmdir
);
4226 EXPORT_SYMBOL(zfs_readdir
);
4227 EXPORT_SYMBOL(zfs_getattr_fast
);
4228 EXPORT_SYMBOL(zfs_setattr
);
4229 EXPORT_SYMBOL(zfs_rename
);
4230 EXPORT_SYMBOL(zfs_symlink
);
4231 EXPORT_SYMBOL(zfs_readlink
);
4232 EXPORT_SYMBOL(zfs_link
);
4233 EXPORT_SYMBOL(zfs_inactive
);
4234 EXPORT_SYMBOL(zfs_space
);
4235 EXPORT_SYMBOL(zfs_fid
);
4236 EXPORT_SYMBOL(zfs_getpage
);
4237 EXPORT_SYMBOL(zfs_putpage
);
4238 EXPORT_SYMBOL(zfs_dirty_inode
);
4239 EXPORT_SYMBOL(zfs_map
);
4242 module_param(zfs_delete_blocks
, ulong
, 0644);
4243 MODULE_PARM_DESC(zfs_delete_blocks
, "Delete files larger than N blocks async");
4246 module_param(zfs_bclone_enabled
, uint
, 0644);
4247 MODULE_PARM_DESC(zfs_bclone_enabled
, "Enable block cloning");