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 http://www.opensolaris.org/os/licensing.
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
);
187 /* Honor ZFS_APPENDONLY file attribute */
188 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
189 ((flag
& O_APPEND
) == 0)) {
191 return (SET_ERROR(EPERM
));
194 /* Keep a count of the synchronous opens in the znode */
196 atomic_inc_32(&zp
->z_sync_cnt
);
203 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
206 znode_t
*zp
= ITOZ(ip
);
207 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
212 /* Decrement the synchronous opens in the znode */
214 atomic_dec_32(&zp
->z_sync_cnt
);
222 * When a file is memory mapped, we must keep the IO data synchronized
223 * between the DMU cache and the memory mapped pages. What this means:
225 * On Write: If we find a memory mapped page, we write to *both*
226 * the page and the dmu buffer.
229 update_pages(znode_t
*zp
, int64_t start
, int len
, objset_t
*os
)
231 struct inode
*ip
= ZTOI(zp
);
232 struct address_space
*mp
= ip
->i_mapping
;
238 off
= start
& (PAGE_SIZE
-1);
239 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
240 nbytes
= MIN(PAGE_SIZE
- off
, len
);
242 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
244 if (mapping_writably_mapped(mp
))
245 flush_dcache_page(pp
);
248 (void) dmu_read(os
, zp
->z_id
, start
+ off
, nbytes
,
249 pb
+ off
, DMU_READ_PREFETCH
);
252 if (mapping_writably_mapped(mp
))
253 flush_dcache_page(pp
);
255 mark_page_accessed(pp
);
268 * When a file is memory mapped, we must keep the IO data synchronized
269 * between the DMU cache and the memory mapped pages. What this means:
271 * On Read: We "read" preferentially from memory mapped pages,
272 * else we default from the dmu buffer.
274 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
275 * the file is memory mapped.
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
;
289 start
= uio
->uio_loffset
;
290 off
= start
& (PAGE_SIZE
-1);
291 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
292 bytes
= MIN(PAGE_SIZE
- off
, len
);
294 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
296 ASSERT(PageUptodate(pp
));
300 error
= zfs_uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
303 if (mapping_writably_mapped(mp
))
304 flush_dcache_page(pp
);
306 mark_page_accessed(pp
);
309 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
322 static unsigned long zfs_delete_blocks
= DMU_MAX_DELETEBLKCNT
;
325 * Write the bytes to a file.
327 * IN: zp - znode of file to be written to
328 * data - bytes to write
329 * len - number of bytes to write
330 * pos - offset to start writing at
332 * OUT: resid - remaining bytes to write
334 * RETURN: 0 if success
335 * positive error code if failure. EIO is returned
336 * for a short write when residp isn't provided.
339 * zp - ctime|mtime updated if byte count > 0
342 zfs_write_simple(znode_t
*zp
, const void *data
, size_t len
,
343 loff_t pos
, size_t *residp
)
345 fstrans_cookie_t cookie
;
349 iov
.iov_base
= (void *)data
;
353 zfs_uio_iovec_init(&uio
, &iov
, 1, pos
, UIO_SYSSPACE
, len
, 0);
355 cookie
= spl_fstrans_mark();
356 error
= zfs_write(zp
, &uio
, 0, kcred
);
357 spl_fstrans_unmark(cookie
);
361 *residp
= zfs_uio_resid(&uio
);
362 else if (zfs_uio_resid(&uio
) != 0)
363 error
= SET_ERROR(EIO
);
370 zfs_rele_async_task(void *arg
)
376 zfs_zrele_async(znode_t
*zp
)
378 struct inode
*ip
= ZTOI(zp
);
379 objset_t
*os
= ITOZSB(ip
)->z_os
;
381 ASSERT(atomic_read(&ip
->i_count
) > 0);
385 * If decrementing the count would put us at 0, we can't do it inline
386 * here, because that would be synchronous. Instead, dispatch an iput
389 * For more information on the dangers of a synchronous iput, see the
390 * header comment of this file.
392 if (!atomic_add_unless(&ip
->i_count
, -1, 1)) {
393 VERIFY(taskq_dispatch(dsl_pool_zrele_taskq(dmu_objset_pool(os
)),
394 zfs_rele_async_task
, ip
, TQ_SLEEP
) != TASKQID_INVALID
);
400 * Lookup an entry in a directory, or an extended attribute directory.
401 * If it exists, return a held inode reference for it.
403 * IN: zdp - znode of directory to search.
404 * nm - name of entry to lookup.
405 * flags - LOOKUP_XATTR set if looking for an attribute.
406 * cr - credentials of caller.
407 * direntflags - directory lookup flags
408 * realpnp - returned pathname.
410 * OUT: zpp - znode of located entry, NULL if not found.
412 * RETURN: 0 on success, error code on failure.
418 zfs_lookup(znode_t
*zdp
, char *nm
, znode_t
**zpp
, int flags
, cred_t
*cr
,
419 int *direntflags
, pathname_t
*realpnp
)
421 zfsvfs_t
*zfsvfs
= ZTOZSB(zdp
);
425 * Fast path lookup, however we must skip DNLC lookup
426 * for case folding or normalizing lookups because the
427 * DNLC code only stores the passed in name. This means
428 * creating 'a' and removing 'A' on a case insensitive
429 * file system would work, but DNLC still thinks 'a'
430 * exists and won't let you create it again on the next
431 * pass through fast path.
433 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
435 if (!S_ISDIR(ZTOI(zdp
)->i_mode
)) {
436 return (SET_ERROR(ENOTDIR
));
437 } else if (zdp
->z_sa_hdl
== NULL
) {
438 return (SET_ERROR(EIO
));
441 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
442 error
= zfs_fastaccesschk_execute(zdp
, cr
);
457 if (flags
& LOOKUP_XATTR
) {
459 * We don't allow recursive attributes..
460 * Maybe someday we will.
462 if (zdp
->z_pflags
& ZFS_XATTR
) {
464 return (SET_ERROR(EINVAL
));
467 if ((error
= zfs_get_xattrdir(zdp
, zpp
, cr
, flags
))) {
473 * Do we have permission to get into attribute directory?
476 if ((error
= zfs_zaccess(*zpp
, ACE_EXECUTE
, 0,
486 if (!S_ISDIR(ZTOI(zdp
)->i_mode
)) {
488 return (SET_ERROR(ENOTDIR
));
492 * Check accessibility of directory.
495 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
500 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
501 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
503 return (SET_ERROR(EILSEQ
));
506 error
= zfs_dirlook(zdp
, nm
, zpp
, flags
, direntflags
, realpnp
);
507 if ((error
== 0) && (*zpp
))
508 zfs_znode_update_vfs(*zpp
);
515 * Attempt to create a new entry in a directory. If the entry
516 * already exists, truncate the file if permissible, else return
517 * an error. Return the ip of the created or trunc'd file.
519 * IN: dzp - znode of directory to put new file entry in.
520 * name - name of new file entry.
521 * vap - attributes of new file.
522 * excl - flag indicating exclusive or non-exclusive mode.
523 * mode - mode to open file with.
524 * cr - credentials of caller.
526 * vsecp - ACL to be set
528 * OUT: zpp - znode of created or trunc'd entry.
530 * RETURN: 0 on success, error code on failure.
533 * dzp - ctime|mtime updated if new entry created
534 * zp - ctime|mtime always, atime if new
537 zfs_create(znode_t
*dzp
, char *name
, vattr_t
*vap
, int excl
,
538 int mode
, znode_t
**zpp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
541 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
549 zfs_acl_ids_t acl_ids
;
550 boolean_t fuid_dirtied
;
551 boolean_t have_acl
= B_FALSE
;
552 boolean_t waited
= B_FALSE
;
555 * If we have an ephemeral id, ACL, or XVATTR then
556 * make sure file system is at proper version
562 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
563 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
564 return (SET_ERROR(EINVAL
));
567 return (SET_ERROR(EINVAL
));
572 zilog
= zfsvfs
->z_log
;
574 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
575 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
577 return (SET_ERROR(EILSEQ
));
580 if (vap
->va_mask
& ATTR_XVATTR
) {
581 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
582 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
592 * Null component name refers to the directory itself.
599 /* possible igrab(zp) */
602 if (flag
& FIGNORECASE
)
605 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
609 zfs_acl_ids_free(&acl_ids
);
610 if (strcmp(name
, "..") == 0)
611 error
= SET_ERROR(EISDIR
);
619 uint64_t projid
= ZFS_DEFAULT_PROJID
;
622 * Create a new file object and update the directory
625 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
627 zfs_acl_ids_free(&acl_ids
);
632 * We only support the creation of regular files in
633 * extended attribute directories.
636 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
638 zfs_acl_ids_free(&acl_ids
);
639 error
= SET_ERROR(EINVAL
);
643 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
644 cr
, vsecp
, &acl_ids
)) != 0)
648 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
649 projid
= zfs_inherit_projid(dzp
);
650 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
651 zfs_acl_ids_free(&acl_ids
);
652 error
= SET_ERROR(EDQUOT
);
656 tx
= dmu_tx_create(os
);
658 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
659 ZFS_SA_BASE_ATTR_SIZE
);
661 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
663 zfs_fuid_txhold(zfsvfs
, tx
);
664 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
665 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
666 if (!zfsvfs
->z_use_sa
&&
667 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
668 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
669 0, acl_ids
.z_aclp
->z_acl_bytes
);
672 error
= dmu_tx_assign(tx
,
673 (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
675 zfs_dirent_unlock(dl
);
676 if (error
== ERESTART
) {
682 zfs_acl_ids_free(&acl_ids
);
687 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
689 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
692 * Since, we failed to add the directory entry for it,
693 * delete the newly created dnode.
695 zfs_znode_delete(zp
, tx
);
696 remove_inode_hash(ZTOI(zp
));
697 zfs_acl_ids_free(&acl_ids
);
703 zfs_fuid_sync(zfsvfs
, tx
);
705 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
706 if (flag
& FIGNORECASE
)
708 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
709 vsecp
, acl_ids
.z_fuidp
, vap
);
710 zfs_acl_ids_free(&acl_ids
);
713 int aflags
= (flag
& O_APPEND
) ? V_APPEND
: 0;
716 zfs_acl_ids_free(&acl_ids
);
720 * A directory entry already exists for this name.
723 * Can't truncate an existing file if in exclusive mode.
726 error
= SET_ERROR(EEXIST
);
730 * Can't open a directory for writing.
732 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
733 error
= SET_ERROR(EISDIR
);
737 * Verify requested access to file.
739 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
743 mutex_enter(&dzp
->z_lock
);
745 mutex_exit(&dzp
->z_lock
);
748 * Truncate regular files if requested.
750 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
751 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
752 /* we can't hold any locks when calling zfs_freesp() */
754 zfs_dirent_unlock(dl
);
757 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
763 zfs_dirent_unlock(dl
);
769 zfs_znode_update_vfs(dzp
);
770 zfs_znode_update_vfs(zp
);
774 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
775 zil_commit(zilog
, 0);
782 zfs_tmpfile(struct inode
*dip
, vattr_t
*vap
, int excl
,
783 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
785 (void) excl
, (void) mode
, (void) flag
;
786 znode_t
*zp
= NULL
, *dzp
= ITOZ(dip
);
787 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
793 zfs_acl_ids_t acl_ids
;
794 uint64_t projid
= ZFS_DEFAULT_PROJID
;
795 boolean_t fuid_dirtied
;
796 boolean_t have_acl
= B_FALSE
;
797 boolean_t waited
= B_FALSE
;
800 * If we have an ephemeral id, ACL, or XVATTR then
801 * make sure file system is at proper version
807 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
808 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
809 return (SET_ERROR(EINVAL
));
815 if (vap
->va_mask
& ATTR_XVATTR
) {
816 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
817 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
827 * Create a new file object and update the directory
830 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
832 zfs_acl_ids_free(&acl_ids
);
836 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
837 cr
, vsecp
, &acl_ids
)) != 0)
841 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
842 projid
= zfs_inherit_projid(dzp
);
843 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
844 zfs_acl_ids_free(&acl_ids
);
845 error
= SET_ERROR(EDQUOT
);
849 tx
= dmu_tx_create(os
);
851 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
852 ZFS_SA_BASE_ATTR_SIZE
);
853 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
855 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
857 zfs_fuid_txhold(zfsvfs
, tx
);
858 if (!zfsvfs
->z_use_sa
&&
859 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
860 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
861 0, acl_ids
.z_aclp
->z_acl_bytes
);
863 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
865 if (error
== ERESTART
) {
871 zfs_acl_ids_free(&acl_ids
);
876 zfs_mknode(dzp
, vap
, tx
, cr
, IS_TMPFILE
, &zp
, &acl_ids
);
879 zfs_fuid_sync(zfsvfs
, tx
);
881 /* Add to unlinked set */
882 zp
->z_unlinked
= B_TRUE
;
883 zfs_unlinked_add(zp
, tx
);
884 zfs_acl_ids_free(&acl_ids
);
892 zfs_znode_update_vfs(dzp
);
893 zfs_znode_update_vfs(zp
);
902 * Remove an entry from a directory.
904 * IN: dzp - znode of directory to remove entry from.
905 * name - name of entry to remove.
906 * cr - credentials of caller.
907 * flags - case flags.
909 * RETURN: 0 if success
910 * error code if failure
914 * ip - ctime (if nlink > 0)
917 static uint64_t null_xattr
= 0;
920 zfs_remove(znode_t
*dzp
, char *name
, cred_t
*cr
, int flags
)
924 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
926 uint64_t acl_obj
, xattr_obj
;
927 uint64_t xattr_obj_unlinked
= 0;
932 boolean_t may_delete_now
, delete_now
= FALSE
;
933 boolean_t unlinked
, toobig
= FALSE
;
935 pathname_t
*realnmp
= NULL
;
939 boolean_t waited
= B_FALSE
;
942 return (SET_ERROR(EINVAL
));
946 zilog
= zfsvfs
->z_log
;
948 if (flags
& FIGNORECASE
) {
958 * Attempt to lock directory; fail if entry doesn't exist.
960 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
968 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
973 * Need to use rmdir for removing directories.
975 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
976 error
= SET_ERROR(EPERM
);
980 mutex_enter(&zp
->z_lock
);
981 may_delete_now
= atomic_read(&ZTOI(zp
)->i_count
) == 1 &&
983 mutex_exit(&zp
->z_lock
);
986 * We may delete the znode now, or we may put it in the unlinked set;
987 * it depends on whether we're the last link, and on whether there are
988 * other holds on the inode. So we dmu_tx_hold() the right things to
989 * allow for either case.
992 tx
= dmu_tx_create(zfsvfs
->z_os
);
993 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
994 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
995 zfs_sa_upgrade_txholds(tx
, zp
);
996 zfs_sa_upgrade_txholds(tx
, dzp
);
997 if (may_delete_now
) {
998 toobig
= zp
->z_size
> zp
->z_blksz
* zfs_delete_blocks
;
999 /* if the file is too big, only hold_free a token amount */
1000 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1001 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1004 /* are there any extended attributes? */
1005 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1006 &xattr_obj
, sizeof (xattr_obj
));
1007 if (error
== 0 && xattr_obj
) {
1008 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1010 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1011 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1014 mutex_enter(&zp
->z_lock
);
1015 if ((acl_obj
= zfs_external_acl(zp
)) != 0 && may_delete_now
)
1016 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1017 mutex_exit(&zp
->z_lock
);
1019 /* charge as an update -- would be nice not to charge at all */
1020 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1023 * Mark this transaction as typically resulting in a net free of space
1025 dmu_tx_mark_netfree(tx
);
1027 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1029 zfs_dirent_unlock(dl
);
1030 if (error
== ERESTART
) {
1050 * Remove the directory entry.
1052 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1061 * Hold z_lock so that we can make sure that the ACL obj
1062 * hasn't changed. Could have been deleted due to
1065 mutex_enter(&zp
->z_lock
);
1066 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1067 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1068 delete_now
= may_delete_now
&& !toobig
&&
1069 atomic_read(&ZTOI(zp
)->i_count
) == 1 &&
1070 !(zp
->z_is_mapped
) && xattr_obj
== xattr_obj_unlinked
&&
1071 zfs_external_acl(zp
) == acl_obj
;
1075 if (xattr_obj_unlinked
) {
1076 ASSERT3U(ZTOI(xzp
)->i_nlink
, ==, 2);
1077 mutex_enter(&xzp
->z_lock
);
1078 xzp
->z_unlinked
= B_TRUE
;
1079 clear_nlink(ZTOI(xzp
));
1081 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
1082 &links
, sizeof (links
), tx
);
1083 ASSERT3U(error
, ==, 0);
1084 mutex_exit(&xzp
->z_lock
);
1085 zfs_unlinked_add(xzp
, tx
);
1088 error
= sa_remove(zp
->z_sa_hdl
,
1089 SA_ZPL_XATTR(zfsvfs
), tx
);
1091 error
= sa_update(zp
->z_sa_hdl
,
1092 SA_ZPL_XATTR(zfsvfs
), &null_xattr
,
1093 sizeof (uint64_t), tx
);
1097 * Add to the unlinked set because a new reference could be
1098 * taken concurrently resulting in a deferred destruction.
1100 zfs_unlinked_add(zp
, tx
);
1101 mutex_exit(&zp
->z_lock
);
1102 } else if (unlinked
) {
1103 mutex_exit(&zp
->z_lock
);
1104 zfs_unlinked_add(zp
, tx
);
1108 if (flags
& FIGNORECASE
)
1110 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
, unlinked
);
1117 zfs_dirent_unlock(dl
);
1118 zfs_znode_update_vfs(dzp
);
1119 zfs_znode_update_vfs(zp
);
1124 zfs_zrele_async(zp
);
1127 zfs_znode_update_vfs(xzp
);
1128 zfs_zrele_async(xzp
);
1131 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1132 zil_commit(zilog
, 0);
1139 * Create a new directory and insert it into dzp using the name
1140 * provided. Return a pointer to the inserted directory.
1142 * IN: dzp - znode of directory to add subdir to.
1143 * dirname - name of new directory.
1144 * vap - attributes of new directory.
1145 * cr - credentials of caller.
1146 * flags - case flags.
1147 * vsecp - ACL to be set
1149 * OUT: zpp - znode of created directory.
1151 * RETURN: 0 if success
1152 * error code if failure
1155 * dzp - ctime|mtime updated
1156 * zpp - ctime|mtime|atime updated
1159 zfs_mkdir(znode_t
*dzp
, char *dirname
, vattr_t
*vap
, znode_t
**zpp
,
1160 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1163 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1171 gid_t gid
= crgetgid(cr
);
1172 zfs_acl_ids_t acl_ids
;
1173 boolean_t fuid_dirtied
;
1174 boolean_t waited
= B_FALSE
;
1176 ASSERT(S_ISDIR(vap
->va_mode
));
1179 * If we have an ephemeral id, ACL, or XVATTR then
1180 * make sure file system is at proper version
1184 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1185 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1186 return (SET_ERROR(EINVAL
));
1188 if (dirname
== NULL
)
1189 return (SET_ERROR(EINVAL
));
1193 zilog
= zfsvfs
->z_log
;
1195 if (dzp
->z_pflags
& ZFS_XATTR
) {
1197 return (SET_ERROR(EINVAL
));
1200 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1201 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1203 return (SET_ERROR(EILSEQ
));
1205 if (flags
& FIGNORECASE
)
1208 if (vap
->va_mask
& ATTR_XVATTR
) {
1209 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1210 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1216 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1217 vsecp
, &acl_ids
)) != 0) {
1222 * First make sure the new directory doesn't exist.
1224 * Existence is checked first to make sure we don't return
1225 * EACCES instead of EEXIST which can cause some applications
1231 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1233 zfs_acl_ids_free(&acl_ids
);
1238 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1239 zfs_acl_ids_free(&acl_ids
);
1240 zfs_dirent_unlock(dl
);
1245 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, zfs_inherit_projid(dzp
))) {
1246 zfs_acl_ids_free(&acl_ids
);
1247 zfs_dirent_unlock(dl
);
1249 return (SET_ERROR(EDQUOT
));
1253 * Add a new entry to the directory.
1255 tx
= dmu_tx_create(zfsvfs
->z_os
);
1256 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1257 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1258 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1260 zfs_fuid_txhold(zfsvfs
, tx
);
1261 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1262 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1263 acl_ids
.z_aclp
->z_acl_bytes
);
1266 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1267 ZFS_SA_BASE_ATTR_SIZE
);
1269 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1271 zfs_dirent_unlock(dl
);
1272 if (error
== ERESTART
) {
1278 zfs_acl_ids_free(&acl_ids
);
1287 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1290 * Now put new name in parent dir.
1292 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
1294 zfs_znode_delete(zp
, tx
);
1295 remove_inode_hash(ZTOI(zp
));
1300 zfs_fuid_sync(zfsvfs
, tx
);
1304 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1305 if (flags
& FIGNORECASE
)
1307 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1308 acl_ids
.z_fuidp
, vap
);
1311 zfs_acl_ids_free(&acl_ids
);
1315 zfs_dirent_unlock(dl
);
1317 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1318 zil_commit(zilog
, 0);
1323 zfs_znode_update_vfs(dzp
);
1324 zfs_znode_update_vfs(zp
);
1331 * Remove a directory subdir entry. If the current working
1332 * directory is the same as the subdir to be removed, the
1335 * IN: dzp - znode of directory to remove from.
1336 * name - name of directory to be removed.
1337 * cwd - inode of current working directory.
1338 * cr - credentials of caller.
1339 * flags - case flags
1341 * RETURN: 0 on success, error code on failure.
1344 * dzp - ctime|mtime updated
1347 zfs_rmdir(znode_t
*dzp
, char *name
, znode_t
*cwd
, cred_t
*cr
,
1351 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1357 boolean_t waited
= B_FALSE
;
1360 return (SET_ERROR(EINVAL
));
1364 zilog
= zfsvfs
->z_log
;
1366 if (flags
& FIGNORECASE
)
1372 * Attempt to lock directory; fail if entry doesn't exist.
1374 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1380 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1384 if (!S_ISDIR(ZTOI(zp
)->i_mode
)) {
1385 error
= SET_ERROR(ENOTDIR
);
1390 error
= SET_ERROR(EINVAL
);
1395 * Grab a lock on the directory to make sure that no one is
1396 * trying to add (or lookup) entries while we are removing it.
1398 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1401 * Grab a lock on the parent pointer to make sure we play well
1402 * with the treewalk and directory rename code.
1404 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1406 tx
= dmu_tx_create(zfsvfs
->z_os
);
1407 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1408 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1409 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1410 zfs_sa_upgrade_txholds(tx
, zp
);
1411 zfs_sa_upgrade_txholds(tx
, dzp
);
1412 dmu_tx_mark_netfree(tx
);
1413 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1415 rw_exit(&zp
->z_parent_lock
);
1416 rw_exit(&zp
->z_name_lock
);
1417 zfs_dirent_unlock(dl
);
1418 if (error
== ERESTART
) {
1431 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1434 uint64_t txtype
= TX_RMDIR
;
1435 if (flags
& FIGNORECASE
)
1437 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
,
1443 rw_exit(&zp
->z_parent_lock
);
1444 rw_exit(&zp
->z_name_lock
);
1446 zfs_dirent_unlock(dl
);
1448 zfs_znode_update_vfs(dzp
);
1449 zfs_znode_update_vfs(zp
);
1452 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1453 zil_commit(zilog
, 0);
1460 * Read directory entries from the given directory cursor position and emit
1461 * name and position for each entry.
1463 * IN: ip - inode of directory to read.
1464 * ctx - directory entry context.
1465 * cr - credentials of caller.
1467 * RETURN: 0 if success
1468 * error code if failure
1471 * ip - atime updated
1473 * Note that the low 4 bits of the cookie returned by zap is always zero.
1474 * This allows us to use the low range for "special" directory entries:
1475 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1476 * we use the offset 2 for the '.zfs' directory.
1479 zfs_readdir(struct inode
*ip
, zpl_dir_context_t
*ctx
, cred_t
*cr
)
1482 znode_t
*zp
= ITOZ(ip
);
1483 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1486 zap_attribute_t zap
;
1492 uint64_t offset
; /* must be unsigned; checks for < 1 */
1497 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
1498 &parent
, sizeof (parent
))) != 0)
1502 * Quit if directory has been removed (posix)
1510 prefetch
= zp
->z_zn_prefetch
;
1513 * Initialize the iterator cursor.
1517 * Start iteration from the beginning of the directory.
1519 zap_cursor_init(&zc
, os
, zp
->z_id
);
1522 * The offset is a serialized cursor.
1524 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
1528 * Transform to file-system independent format
1533 * Special case `.', `..', and `.zfs'.
1536 (void) strcpy(zap
.za_name
, ".");
1537 zap
.za_normalization_conflict
= 0;
1540 } else if (offset
== 1) {
1541 (void) strcpy(zap
.za_name
, "..");
1542 zap
.za_normalization_conflict
= 0;
1545 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
1546 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
1547 zap
.za_normalization_conflict
= 0;
1548 objnum
= ZFSCTL_INO_ROOT
;
1554 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
1555 if (error
== ENOENT
)
1562 * Allow multiple entries provided the first entry is
1563 * the object id. Non-zpl consumers may safely make
1564 * use of the additional space.
1566 * XXX: This should be a feature flag for compatibility
1568 if (zap
.za_integer_length
!= 8 ||
1569 zap
.za_num_integers
== 0) {
1570 cmn_err(CE_WARN
, "zap_readdir: bad directory "
1571 "entry, obj = %lld, offset = %lld, "
1572 "length = %d, num = %lld\n",
1573 (u_longlong_t
)zp
->z_id
,
1574 (u_longlong_t
)offset
,
1575 zap
.za_integer_length
,
1576 (u_longlong_t
)zap
.za_num_integers
);
1577 error
= SET_ERROR(ENXIO
);
1581 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
1582 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
1585 done
= !zpl_dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
1590 /* Prefetch znode */
1592 dmu_prefetch(os
, objnum
, 0, 0, 0,
1593 ZIO_PRIORITY_SYNC_READ
);
1597 * Move to the next entry, fill in the previous offset.
1599 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
1600 zap_cursor_advance(&zc
);
1601 offset
= zap_cursor_serialize(&zc
);
1607 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
1610 zap_cursor_fini(&zc
);
1611 if (error
== ENOENT
)
1620 * Get the basic file attributes and place them in the provided kstat
1621 * structure. The inode is assumed to be the authoritative source
1622 * for most of the attributes. However, the znode currently has the
1623 * authoritative atime, blksize, and block count.
1625 * IN: ip - inode of file.
1627 * OUT: sp - kstat values.
1629 * RETURN: 0 (always succeeds)
1632 zfs_getattr_fast(struct user_namespace
*user_ns
, struct inode
*ip
,
1635 znode_t
*zp
= ITOZ(ip
);
1636 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1638 u_longlong_t nblocks
;
1643 mutex_enter(&zp
->z_lock
);
1645 zpl_generic_fillattr(user_ns
, ip
, sp
);
1647 * +1 link count for root inode with visible '.zfs' directory.
1649 if ((zp
->z_id
== zfsvfs
->z_root
) && zfs_show_ctldir(zp
))
1650 if (sp
->nlink
< ZFS_LINK_MAX
)
1653 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
1654 sp
->blksize
= blksize
;
1655 sp
->blocks
= nblocks
;
1657 if (unlikely(zp
->z_blksz
== 0)) {
1659 * Block size hasn't been set; suggest maximal I/O transfers.
1661 sp
->blksize
= zfsvfs
->z_max_blksz
;
1664 mutex_exit(&zp
->z_lock
);
1667 * Required to prevent NFS client from detecting different inode
1668 * numbers of snapshot root dentry before and after snapshot mount.
1670 if (zfsvfs
->z_issnap
) {
1671 if (ip
->i_sb
->s_root
->d_inode
== ip
)
1672 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
1673 dmu_objset_id(zfsvfs
->z_os
);
1682 * For the operation of changing file's user/group/project, we need to
1683 * handle not only the main object that is assigned to the file directly,
1684 * but also the ones that are used by the file via hidden xattr directory.
1686 * Because the xattr directory may contains many EA entries, as to it may
1687 * be impossible to change all of them via the transaction of changing the
1688 * main object's user/group/project attributes. Then we have to change them
1689 * via other multiple independent transactions one by one. It may be not good
1690 * solution, but we have no better idea yet.
1693 zfs_setattr_dir(znode_t
*dzp
)
1695 struct inode
*dxip
= ZTOI(dzp
);
1696 struct inode
*xip
= NULL
;
1697 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1698 objset_t
*os
= zfsvfs
->z_os
;
1700 zap_attribute_t zap
;
1703 dmu_tx_t
*tx
= NULL
;
1705 sa_bulk_attr_t bulk
[4];
1709 zap_cursor_init(&zc
, os
, dzp
->z_id
);
1710 while ((err
= zap_cursor_retrieve(&zc
, &zap
)) == 0) {
1712 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
1717 err
= zfs_dirent_lock(&dl
, dzp
, (char *)zap
.za_name
, &zp
,
1718 ZEXISTS
, NULL
, NULL
);
1725 if (KUID_TO_SUID(xip
->i_uid
) == KUID_TO_SUID(dxip
->i_uid
) &&
1726 KGID_TO_SGID(xip
->i_gid
) == KGID_TO_SGID(dxip
->i_gid
) &&
1727 zp
->z_projid
== dzp
->z_projid
)
1730 tx
= dmu_tx_create(os
);
1731 if (!(zp
->z_pflags
& ZFS_PROJID
))
1732 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1734 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1736 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1740 mutex_enter(&dzp
->z_lock
);
1742 if (KUID_TO_SUID(xip
->i_uid
) != KUID_TO_SUID(dxip
->i_uid
)) {
1743 xip
->i_uid
= dxip
->i_uid
;
1744 uid
= zfs_uid_read(dxip
);
1745 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
1746 &uid
, sizeof (uid
));
1749 if (KGID_TO_SGID(xip
->i_gid
) != KGID_TO_SGID(dxip
->i_gid
)) {
1750 xip
->i_gid
= dxip
->i_gid
;
1751 gid
= zfs_gid_read(dxip
);
1752 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
1753 &gid
, sizeof (gid
));
1756 if (zp
->z_projid
!= dzp
->z_projid
) {
1757 if (!(zp
->z_pflags
& ZFS_PROJID
)) {
1758 zp
->z_pflags
|= ZFS_PROJID
;
1759 SA_ADD_BULK_ATTR(bulk
, count
,
1760 SA_ZPL_FLAGS(zfsvfs
), NULL
, &zp
->z_pflags
,
1761 sizeof (zp
->z_pflags
));
1764 zp
->z_projid
= dzp
->z_projid
;
1765 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PROJID(zfsvfs
),
1766 NULL
, &zp
->z_projid
, sizeof (zp
->z_projid
));
1769 mutex_exit(&dzp
->z_lock
);
1771 if (likely(count
> 0)) {
1772 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1778 if (err
!= 0 && err
!= ENOENT
)
1785 zfs_dirent_unlock(dl
);
1787 zap_cursor_advance(&zc
);
1794 zfs_dirent_unlock(dl
);
1796 zap_cursor_fini(&zc
);
1798 return (err
== ENOENT
? 0 : err
);
1802 * Set the file attributes to the values contained in the
1805 * IN: zp - znode of file to be modified.
1806 * vap - new attribute values.
1807 * If ATTR_XVATTR set, then optional attrs are being set
1808 * flags - ATTR_UTIME set if non-default time values provided.
1809 * - ATTR_NOACLCHECK (CIFS context only).
1810 * cr - credentials of caller.
1812 * RETURN: 0 if success
1813 * error code if failure
1816 * ip - ctime updated, mtime updated if size changed.
1819 zfs_setattr(znode_t
*zp
, vattr_t
*vap
, int flags
, cred_t
*cr
)
1822 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1823 objset_t
*os
= zfsvfs
->z_os
;
1827 xvattr_t
*tmpxvattr
;
1828 uint_t mask
= vap
->va_mask
;
1829 uint_t saved_mask
= 0;
1832 uint64_t new_kuid
= 0, new_kgid
= 0, new_uid
, new_gid
;
1834 uint64_t mtime
[2], ctime
[2], atime
[2];
1835 uint64_t projid
= ZFS_INVALID_PROJID
;
1837 int need_policy
= FALSE
;
1839 zfs_fuid_info_t
*fuidp
= NULL
;
1840 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
1843 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
1844 boolean_t fuid_dirtied
= B_FALSE
;
1845 boolean_t handle_eadir
= B_FALSE
;
1846 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
1847 int count
= 0, xattr_count
= 0, bulks
= 8;
1857 * If this is a xvattr_t, then get a pointer to the structure of
1858 * optional attributes. If this is NULL, then we have a vattr_t.
1860 xoap
= xva_getxoptattr(xvap
);
1861 if (xoap
!= NULL
&& (mask
& ATTR_XVATTR
)) {
1862 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
1863 if (!dmu_objset_projectquota_enabled(os
) ||
1864 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
))) {
1866 return (SET_ERROR(ENOTSUP
));
1869 projid
= xoap
->xoa_projid
;
1870 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
1872 return (SET_ERROR(EINVAL
));
1875 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
1876 projid
= ZFS_INVALID_PROJID
;
1881 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
1882 (xoap
->xoa_projinherit
!=
1883 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) &&
1884 (!dmu_objset_projectquota_enabled(os
) ||
1885 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
)))) {
1887 return (SET_ERROR(ENOTSUP
));
1891 zilog
= zfsvfs
->z_log
;
1894 * Make sure that if we have ephemeral uid/gid or xvattr specified
1895 * that file system is at proper version level
1898 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1899 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
1900 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
1901 (mask
& ATTR_XVATTR
))) {
1903 return (SET_ERROR(EINVAL
));
1906 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
1908 return (SET_ERROR(EISDIR
));
1911 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
1913 return (SET_ERROR(EINVAL
));
1916 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
1917 xva_init(tmpxvattr
);
1919 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
1920 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
1923 * Immutable files can only alter immutable bit and atime
1925 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
1926 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
1927 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
1928 err
= SET_ERROR(EPERM
);
1932 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
1933 err
= SET_ERROR(EPERM
);
1938 * Verify timestamps doesn't overflow 32 bits.
1939 * ZFS can handle large timestamps, but 32bit syscalls can't
1940 * handle times greater than 2039. This check should be removed
1941 * once large timestamps are fully supported.
1943 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
1944 if (((mask
& ATTR_ATIME
) &&
1945 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
1946 ((mask
& ATTR_MTIME
) &&
1947 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
1948 err
= SET_ERROR(EOVERFLOW
);
1957 /* Can this be moved to before the top label? */
1958 if (zfs_is_readonly(zfsvfs
)) {
1959 err
= SET_ERROR(EROFS
);
1964 * First validate permissions
1967 if (mask
& ATTR_SIZE
) {
1968 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
1973 * XXX - Note, we are not providing any open
1974 * mode flags here (like FNDELAY), so we may
1975 * block if there are locks present... this
1976 * should be addressed in openat().
1978 /* XXX - would it be OK to generate a log record here? */
1979 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
1984 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
1985 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
1986 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
1987 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
1988 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
1989 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
1990 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
1991 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
1992 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
1996 if (mask
& (ATTR_UID
|ATTR_GID
)) {
1997 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2002 * NOTE: even if a new mode is being set,
2003 * we may clear S_ISUID/S_ISGID bits.
2006 if (!(mask
& ATTR_MODE
))
2007 vap
->va_mode
= zp
->z_mode
;
2010 * Take ownership or chgrp to group we are a member of
2013 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2014 take_group
= (mask
& ATTR_GID
) &&
2015 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2018 * If both ATTR_UID and ATTR_GID are set then take_owner and
2019 * take_group must both be set in order to allow taking
2022 * Otherwise, send the check through secpolicy_vnode_setattr()
2026 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2027 take_owner
&& take_group
) ||
2028 ((idmask
== ATTR_UID
) && take_owner
) ||
2029 ((idmask
== ATTR_GID
) && take_group
)) {
2030 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2031 skipaclchk
, cr
) == 0) {
2033 * Remove setuid/setgid for non-privileged users
2035 (void) secpolicy_setid_clear(vap
, cr
);
2036 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2045 mutex_enter(&zp
->z_lock
);
2046 oldva
.va_mode
= zp
->z_mode
;
2047 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2048 if (mask
& ATTR_XVATTR
) {
2050 * Update xvattr mask to include only those attributes
2051 * that are actually changing.
2053 * the bits will be restored prior to actually setting
2054 * the attributes so the caller thinks they were set.
2056 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2057 if (xoap
->xoa_appendonly
!=
2058 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2061 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2062 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2066 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2067 if (xoap
->xoa_projinherit
!=
2068 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
2071 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
2072 XVA_SET_REQ(tmpxvattr
, XAT_PROJINHERIT
);
2076 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2077 if (xoap
->xoa_nounlink
!=
2078 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2081 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2082 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2086 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2087 if (xoap
->xoa_immutable
!=
2088 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2091 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2092 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2096 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2097 if (xoap
->xoa_nodump
!=
2098 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2101 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2102 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2106 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2107 if (xoap
->xoa_av_modified
!=
2108 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2111 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2112 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2116 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2117 if ((!S_ISREG(ip
->i_mode
) &&
2118 xoap
->xoa_av_quarantined
) ||
2119 xoap
->xoa_av_quarantined
!=
2120 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2123 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2124 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2128 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2129 mutex_exit(&zp
->z_lock
);
2130 err
= SET_ERROR(EPERM
);
2134 if (need_policy
== FALSE
&&
2135 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2136 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2141 mutex_exit(&zp
->z_lock
);
2143 if (mask
& ATTR_MODE
) {
2144 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2145 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2150 trim_mask
|= ATTR_MODE
;
2158 * If trim_mask is set then take ownership
2159 * has been granted or write_acl is present and user
2160 * has the ability to modify mode. In that case remove
2161 * UID|GID and or MODE from mask so that
2162 * secpolicy_vnode_setattr() doesn't revoke it.
2166 saved_mask
= vap
->va_mask
;
2167 vap
->va_mask
&= ~trim_mask
;
2169 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2170 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2175 vap
->va_mask
|= saved_mask
;
2179 * secpolicy_vnode_setattr, or take ownership may have
2182 mask
= vap
->va_mask
;
2184 if ((mask
& (ATTR_UID
| ATTR_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
2185 handle_eadir
= B_TRUE
;
2186 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
2187 &xattr_obj
, sizeof (xattr_obj
));
2189 if (err
== 0 && xattr_obj
) {
2190 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2194 if (mask
& ATTR_UID
) {
2195 new_kuid
= zfs_fuid_create(zfsvfs
,
2196 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2197 if (new_kuid
!= KUID_TO_SUID(ZTOI(zp
)->i_uid
) &&
2198 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
2202 err
= SET_ERROR(EDQUOT
);
2207 if (mask
& ATTR_GID
) {
2208 new_kgid
= zfs_fuid_create(zfsvfs
,
2209 (uint64_t)vap
->va_gid
, cr
, ZFS_GROUP
, &fuidp
);
2210 if (new_kgid
!= KGID_TO_SGID(ZTOI(zp
)->i_gid
) &&
2211 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
2215 err
= SET_ERROR(EDQUOT
);
2220 if (projid
!= ZFS_INVALID_PROJID
&&
2221 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
2228 tx
= dmu_tx_create(os
);
2230 if (mask
& ATTR_MODE
) {
2231 uint64_t pmode
= zp
->z_mode
;
2233 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2235 if (ZTOZSB(zp
)->z_acl_mode
== ZFS_ACL_RESTRICTED
&&
2236 !(zp
->z_pflags
& ZFS_ACL_TRIVIAL
)) {
2241 if ((err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
)))
2244 mutex_enter(&zp
->z_lock
);
2245 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2247 * Are we upgrading ACL from old V0 format
2250 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
2251 zfs_znode_acl_version(zp
) ==
2252 ZFS_ACL_VERSION_INITIAL
) {
2253 dmu_tx_hold_free(tx
, acl_obj
, 0,
2255 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2256 0, aclp
->z_acl_bytes
);
2258 dmu_tx_hold_write(tx
, acl_obj
, 0,
2261 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2262 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2263 0, aclp
->z_acl_bytes
);
2265 mutex_exit(&zp
->z_lock
);
2266 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2268 if (((mask
& ATTR_XVATTR
) &&
2269 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
2270 (projid
!= ZFS_INVALID_PROJID
&&
2271 !(zp
->z_pflags
& ZFS_PROJID
)))
2272 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2274 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2278 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2281 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2283 zfs_fuid_txhold(zfsvfs
, tx
);
2285 zfs_sa_upgrade_txholds(tx
, zp
);
2287 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2293 * Set each attribute requested.
2294 * We group settings according to the locks they need to acquire.
2296 * Note: you cannot set ctime directly, although it will be
2297 * updated as a side-effect of calling this function.
2300 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
2302 * For the existed object that is upgraded from old system,
2303 * its on-disk layout has no slot for the project ID attribute.
2304 * But quota accounting logic needs to access related slots by
2305 * offset directly. So we need to adjust old objects' layout
2306 * to make the project ID to some unified and fixed offset.
2309 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
2311 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
2313 if (unlikely(err
== EEXIST
))
2318 projid
= ZFS_INVALID_PROJID
;
2321 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2322 mutex_enter(&zp
->z_acl_lock
);
2323 mutex_enter(&zp
->z_lock
);
2325 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
2326 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2329 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2330 mutex_enter(&attrzp
->z_acl_lock
);
2331 mutex_enter(&attrzp
->z_lock
);
2332 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2333 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
2334 sizeof (attrzp
->z_pflags
));
2335 if (projid
!= ZFS_INVALID_PROJID
) {
2336 attrzp
->z_projid
= projid
;
2337 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2338 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
2339 sizeof (attrzp
->z_projid
));
2343 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2345 if (mask
& ATTR_UID
) {
2346 ZTOI(zp
)->i_uid
= SUID_TO_KUID(new_kuid
);
2347 new_uid
= zfs_uid_read(ZTOI(zp
));
2348 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2349 &new_uid
, sizeof (new_uid
));
2351 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2352 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
2354 ZTOI(attrzp
)->i_uid
= SUID_TO_KUID(new_uid
);
2358 if (mask
& ATTR_GID
) {
2359 ZTOI(zp
)->i_gid
= SGID_TO_KGID(new_kgid
);
2360 new_gid
= zfs_gid_read(ZTOI(zp
));
2361 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
2362 NULL
, &new_gid
, sizeof (new_gid
));
2364 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2365 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
2367 ZTOI(attrzp
)->i_gid
= SGID_TO_KGID(new_kgid
);
2370 if (!(mask
& ATTR_MODE
)) {
2371 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
2372 NULL
, &new_mode
, sizeof (new_mode
));
2373 new_mode
= zp
->z_mode
;
2375 err
= zfs_acl_chown_setattr(zp
);
2378 err
= zfs_acl_chown_setattr(attrzp
);
2383 if (mask
& ATTR_MODE
) {
2384 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
2385 &new_mode
, sizeof (new_mode
));
2386 zp
->z_mode
= ZTOI(zp
)->i_mode
= new_mode
;
2387 ASSERT3P(aclp
, !=, NULL
);
2388 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2390 if (zp
->z_acl_cached
)
2391 zfs_acl_free(zp
->z_acl_cached
);
2392 zp
->z_acl_cached
= aclp
;
2396 if ((mask
& ATTR_ATIME
) || zp
->z_atime_dirty
) {
2397 zp
->z_atime_dirty
= B_FALSE
;
2398 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
2399 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
2400 &atime
, sizeof (atime
));
2403 if (mask
& (ATTR_MTIME
| ATTR_SIZE
)) {
2404 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2405 ZTOI(zp
)->i_mtime
= zpl_inode_timestamp_truncate(
2406 vap
->va_mtime
, ZTOI(zp
));
2408 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
2409 mtime
, sizeof (mtime
));
2412 if (mask
& (ATTR_CTIME
| ATTR_SIZE
)) {
2413 ZFS_TIME_ENCODE(&vap
->va_ctime
, ctime
);
2414 ZTOI(zp
)->i_ctime
= zpl_inode_timestamp_truncate(vap
->va_ctime
,
2416 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2417 ctime
, sizeof (ctime
));
2420 if (projid
!= ZFS_INVALID_PROJID
) {
2421 zp
->z_projid
= projid
;
2422 SA_ADD_BULK_ATTR(bulk
, count
,
2423 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
2424 sizeof (zp
->z_projid
));
2427 if (attrzp
&& mask
) {
2428 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2429 SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
,
2434 * Do this after setting timestamps to prevent timestamp
2435 * update from toggling bit
2438 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2441 * restore trimmed off masks
2442 * so that return masks can be set for caller.
2445 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2446 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2448 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2449 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2451 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2452 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2454 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2455 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2457 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2458 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2460 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
2461 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2463 if (XVA_ISSET_REQ(tmpxvattr
, XAT_PROJINHERIT
)) {
2464 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
2467 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2468 ASSERT(S_ISREG(ip
->i_mode
));
2470 zfs_xvattr_set(zp
, xvap
, tx
);
2474 zfs_fuid_sync(zfsvfs
, tx
);
2477 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2479 mutex_exit(&zp
->z_lock
);
2480 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2481 mutex_exit(&zp
->z_acl_lock
);
2484 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2485 mutex_exit(&attrzp
->z_acl_lock
);
2486 mutex_exit(&attrzp
->z_lock
);
2489 if (err
== 0 && xattr_count
> 0) {
2490 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
2499 zfs_fuid_info_free(fuidp
);
2507 if (err
== ERESTART
)
2511 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2514 if (err2
== 0 && handle_eadir
)
2515 err2
= zfs_setattr_dir(attrzp
);
2518 zfs_znode_update_vfs(zp
);
2522 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
2523 zil_commit(zilog
, 0);
2526 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
2527 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
2528 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
2533 typedef struct zfs_zlock
{
2534 krwlock_t
*zl_rwlock
; /* lock we acquired */
2535 znode_t
*zl_znode
; /* znode we held */
2536 struct zfs_zlock
*zl_next
; /* next in list */
2540 * Drop locks and release vnodes that were held by zfs_rename_lock().
2543 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
2547 while ((zl
= *zlpp
) != NULL
) {
2548 if (zl
->zl_znode
!= NULL
)
2549 zfs_zrele_async(zl
->zl_znode
);
2550 rw_exit(zl
->zl_rwlock
);
2551 *zlpp
= zl
->zl_next
;
2552 kmem_free(zl
, sizeof (*zl
));
2557 * Search back through the directory tree, using the ".." entries.
2558 * Lock each directory in the chain to prevent concurrent renames.
2559 * Fail any attempt to move a directory into one of its own descendants.
2560 * XXX - z_parent_lock can overlap with map or grow locks
2563 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
2567 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
2568 uint64_t oidp
= zp
->z_id
;
2569 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
2570 krw_t rw
= RW_WRITER
;
2573 * First pass write-locks szp and compares to zp->z_id.
2574 * Later passes read-lock zp and compare to zp->z_parent.
2577 if (!rw_tryenter(rwlp
, rw
)) {
2579 * Another thread is renaming in this path.
2580 * Note that if we are a WRITER, we don't have any
2581 * parent_locks held yet.
2583 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
2585 * Drop our locks and restart
2587 zfs_rename_unlock(&zl
);
2591 rwlp
= &szp
->z_parent_lock
;
2596 * Wait for other thread to drop its locks
2602 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
2603 zl
->zl_rwlock
= rwlp
;
2604 zl
->zl_znode
= NULL
;
2605 zl
->zl_next
= *zlpp
;
2608 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
2609 return (SET_ERROR(EINVAL
));
2611 if (oidp
== rootid
) /* We've hit the top */
2614 if (rw
== RW_READER
) { /* i.e. not the first pass */
2615 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
2620 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
2621 &oidp
, sizeof (oidp
));
2622 rwlp
= &zp
->z_parent_lock
;
2625 } while (zp
->z_id
!= sdzp
->z_id
);
2631 * Move an entry from the provided source directory to the target
2632 * directory. Change the entry name as indicated.
2634 * IN: sdzp - Source directory containing the "old entry".
2635 * snm - Old entry name.
2636 * tdzp - Target directory to contain the "new entry".
2637 * tnm - New entry name.
2638 * cr - credentials of caller.
2639 * flags - case flags
2641 * RETURN: 0 on success, error code on failure.
2644 * sdzp,tdzp - ctime|mtime updated
2647 zfs_rename(znode_t
*sdzp
, char *snm
, znode_t
*tdzp
, char *tnm
,
2648 cred_t
*cr
, int flags
)
2651 zfsvfs_t
*zfsvfs
= ZTOZSB(sdzp
);
2653 zfs_dirlock_t
*sdl
, *tdl
;
2656 int cmp
, serr
, terr
;
2659 boolean_t waited
= B_FALSE
;
2661 if (snm
== NULL
|| tnm
== NULL
)
2662 return (SET_ERROR(EINVAL
));
2665 ZFS_VERIFY_ZP(sdzp
);
2666 zilog
= zfsvfs
->z_log
;
2668 ZFS_VERIFY_ZP(tdzp
);
2671 * We check i_sb because snapshots and the ctldir must have different
2674 if (ZTOI(tdzp
)->i_sb
!= ZTOI(sdzp
)->i_sb
||
2675 zfsctl_is_node(ZTOI(tdzp
))) {
2677 return (SET_ERROR(EXDEV
));
2680 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
2681 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
2683 return (SET_ERROR(EILSEQ
));
2686 if (flags
& FIGNORECASE
)
2695 * This is to prevent the creation of links into attribute space
2696 * by renaming a linked file into/outof an attribute directory.
2697 * See the comment in zfs_link() for why this is considered bad.
2699 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
2701 return (SET_ERROR(EINVAL
));
2705 * Lock source and target directory entries. To prevent deadlock,
2706 * a lock ordering must be defined. We lock the directory with
2707 * the smallest object id first, or if it's a tie, the one with
2708 * the lexically first name.
2710 if (sdzp
->z_id
< tdzp
->z_id
) {
2712 } else if (sdzp
->z_id
> tdzp
->z_id
) {
2716 * First compare the two name arguments without
2717 * considering any case folding.
2719 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
2721 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
2722 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
2725 * POSIX: "If the old argument and the new argument
2726 * both refer to links to the same existing file,
2727 * the rename() function shall return successfully
2728 * and perform no other action."
2734 * If the file system is case-folding, then we may
2735 * have some more checking to do. A case-folding file
2736 * system is either supporting mixed case sensitivity
2737 * access or is completely case-insensitive. Note
2738 * that the file system is always case preserving.
2740 * In mixed sensitivity mode case sensitive behavior
2741 * is the default. FIGNORECASE must be used to
2742 * explicitly request case insensitive behavior.
2744 * If the source and target names provided differ only
2745 * by case (e.g., a request to rename 'tim' to 'Tim'),
2746 * we will treat this as a special case in the
2747 * case-insensitive mode: as long as the source name
2748 * is an exact match, we will allow this to proceed as
2749 * a name-change request.
2751 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
2752 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
2753 flags
& FIGNORECASE
)) &&
2754 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
2757 * case preserving rename request, require exact
2766 * If the source and destination directories are the same, we should
2767 * grab the z_name_lock of that directory only once.
2771 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
2775 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
2776 ZEXISTS
| zflg
, NULL
, NULL
);
2777 terr
= zfs_dirent_lock(&tdl
,
2778 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
2780 terr
= zfs_dirent_lock(&tdl
,
2781 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
2782 serr
= zfs_dirent_lock(&sdl
,
2783 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
2789 * Source entry invalid or not there.
2792 zfs_dirent_unlock(tdl
);
2798 rw_exit(&sdzp
->z_name_lock
);
2800 if (strcmp(snm
, "..") == 0)
2806 zfs_dirent_unlock(sdl
);
2810 rw_exit(&sdzp
->z_name_lock
);
2812 if (strcmp(tnm
, "..") == 0)
2819 * If we are using project inheritance, means if the directory has
2820 * ZFS_PROJINHERIT set, then its descendant directories will inherit
2821 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
2822 * such case, we only allow renames into our tree when the project
2825 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
2826 tdzp
->z_projid
!= szp
->z_projid
) {
2827 error
= SET_ERROR(EXDEV
);
2832 * Must have write access at the source to remove the old entry
2833 * and write access at the target to create the new entry.
2834 * Note that if target and source are the same, this can be
2835 * done in a single check.
2838 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
2841 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
2843 * Check to make sure rename is valid.
2844 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2846 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
2851 * Does target exist?
2855 * Source and target must be the same type.
2857 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
2858 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
2859 error
= SET_ERROR(ENOTDIR
);
2863 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
2864 error
= SET_ERROR(EISDIR
);
2869 * POSIX dictates that when the source and target
2870 * entries refer to the same file object, rename
2871 * must do nothing and exit without error.
2873 if (szp
->z_id
== tzp
->z_id
) {
2879 tx
= dmu_tx_create(zfsvfs
->z_os
);
2880 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
2881 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
2882 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
2883 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
2885 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
2886 zfs_sa_upgrade_txholds(tx
, tdzp
);
2889 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
2890 zfs_sa_upgrade_txholds(tx
, tzp
);
2893 zfs_sa_upgrade_txholds(tx
, szp
);
2894 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
2895 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
2898 zfs_rename_unlock(&zl
);
2899 zfs_dirent_unlock(sdl
);
2900 zfs_dirent_unlock(tdl
);
2903 rw_exit(&sdzp
->z_name_lock
);
2905 if (error
== ERESTART
) {
2922 if (tzp
) /* Attempt to remove the existing target */
2923 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
2926 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
2928 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
2929 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
)
2930 szp
->z_pflags
|= ZFS_PROJINHERIT
;
2932 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
2933 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
2936 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
2938 zfs_log_rename(zilog
, tx
, TX_RENAME
|
2939 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
2940 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
2943 * At this point, we have successfully created
2944 * the target name, but have failed to remove
2945 * the source name. Since the create was done
2946 * with the ZRENAMING flag, there are
2947 * complications; for one, the link count is
2948 * wrong. The easiest way to deal with this
2949 * is to remove the newly created target, and
2950 * return the original error. This must
2951 * succeed; fortunately, it is very unlikely to
2952 * fail, since we just created it.
2954 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
2955 ZRENAMING
, NULL
), ==, 0);
2959 * If we had removed the existing target, subsequent
2960 * call to zfs_link_create() to add back the same entry
2961 * but, the new dnode (szp) should not fail.
2963 ASSERT(tzp
== NULL
);
2970 zfs_rename_unlock(&zl
);
2972 zfs_dirent_unlock(sdl
);
2973 zfs_dirent_unlock(tdl
);
2975 zfs_znode_update_vfs(sdzp
);
2977 rw_exit(&sdzp
->z_name_lock
);
2980 zfs_znode_update_vfs(tdzp
);
2982 zfs_znode_update_vfs(szp
);
2985 zfs_znode_update_vfs(tzp
);
2989 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2990 zil_commit(zilog
, 0);
2997 * Insert the indicated symbolic reference entry into the directory.
2999 * IN: dzp - Directory to contain new symbolic link.
3000 * name - Name of directory entry in dip.
3001 * vap - Attributes of new entry.
3002 * link - Name for new symlink entry.
3003 * cr - credentials of caller.
3004 * flags - case flags
3006 * OUT: zpp - Znode for new symbolic link.
3008 * RETURN: 0 on success, error code on failure.
3011 * dip - ctime|mtime updated
3014 zfs_symlink(znode_t
*dzp
, char *name
, vattr_t
*vap
, char *link
,
3015 znode_t
**zpp
, cred_t
*cr
, int flags
)
3020 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
3022 uint64_t len
= strlen(link
);
3025 zfs_acl_ids_t acl_ids
;
3026 boolean_t fuid_dirtied
;
3027 uint64_t txtype
= TX_SYMLINK
;
3028 boolean_t waited
= B_FALSE
;
3030 ASSERT(S_ISLNK(vap
->va_mode
));
3033 return (SET_ERROR(EINVAL
));
3037 zilog
= zfsvfs
->z_log
;
3039 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3040 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3042 return (SET_ERROR(EILSEQ
));
3044 if (flags
& FIGNORECASE
)
3047 if (len
> MAXPATHLEN
) {
3049 return (SET_ERROR(ENAMETOOLONG
));
3052 if ((error
= zfs_acl_ids_create(dzp
, 0,
3053 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3061 * Attempt to lock directory; fail if entry already exists.
3063 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3065 zfs_acl_ids_free(&acl_ids
);
3070 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3071 zfs_acl_ids_free(&acl_ids
);
3072 zfs_dirent_unlock(dl
);
3077 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, ZFS_DEFAULT_PROJID
)) {
3078 zfs_acl_ids_free(&acl_ids
);
3079 zfs_dirent_unlock(dl
);
3081 return (SET_ERROR(EDQUOT
));
3083 tx
= dmu_tx_create(zfsvfs
->z_os
);
3084 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3085 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3086 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3087 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3088 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3089 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3090 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3091 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3092 acl_ids
.z_aclp
->z_acl_bytes
);
3095 zfs_fuid_txhold(zfsvfs
, tx
);
3096 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3098 zfs_dirent_unlock(dl
);
3099 if (error
== ERESTART
) {
3105 zfs_acl_ids_free(&acl_ids
);
3112 * Create a new object for the symlink.
3113 * for version 4 ZPL datasets the symlink will be an SA attribute
3115 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3118 zfs_fuid_sync(zfsvfs
, tx
);
3120 mutex_enter(&zp
->z_lock
);
3122 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
3125 zfs_sa_symlink(zp
, link
, len
, tx
);
3126 mutex_exit(&zp
->z_lock
);
3129 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
3130 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3132 * Insert the new object into the directory.
3134 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
3136 zfs_znode_delete(zp
, tx
);
3137 remove_inode_hash(ZTOI(zp
));
3139 if (flags
& FIGNORECASE
)
3141 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3143 zfs_znode_update_vfs(dzp
);
3144 zfs_znode_update_vfs(zp
);
3147 zfs_acl_ids_free(&acl_ids
);
3151 zfs_dirent_unlock(dl
);
3156 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3157 zil_commit(zilog
, 0);
3167 * Return, in the buffer contained in the provided uio structure,
3168 * the symbolic path referred to by ip.
3170 * IN: ip - inode of symbolic link
3171 * uio - structure to contain the link path.
3172 * cr - credentials of caller.
3174 * RETURN: 0 if success
3175 * error code if failure
3178 * ip - atime updated
3181 zfs_readlink(struct inode
*ip
, zfs_uio_t
*uio
, cred_t
*cr
)
3184 znode_t
*zp
= ITOZ(ip
);
3185 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3191 mutex_enter(&zp
->z_lock
);
3193 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3194 SA_ZPL_SYMLINK(zfsvfs
), uio
);
3196 error
= zfs_sa_readlink(zp
, uio
);
3197 mutex_exit(&zp
->z_lock
);
3204 * Insert a new entry into directory tdzp referencing szp.
3206 * IN: tdzp - Directory to contain new entry.
3207 * szp - znode of new entry.
3208 * name - name of new entry.
3209 * cr - credentials of caller.
3210 * flags - case flags.
3212 * RETURN: 0 if success
3213 * error code if failure
3216 * tdzp - ctime|mtime updated
3217 * szp - ctime updated
3220 zfs_link(znode_t
*tdzp
, znode_t
*szp
, char *name
, cred_t
*cr
,
3223 struct inode
*sip
= ZTOI(szp
);
3225 zfsvfs_t
*zfsvfs
= ZTOZSB(tdzp
);
3233 boolean_t waited
= B_FALSE
;
3234 boolean_t is_tmpfile
= 0;
3237 is_tmpfile
= (sip
->i_nlink
== 0 && (sip
->i_state
& I_LINKABLE
));
3239 ASSERT(S_ISDIR(ZTOI(tdzp
)->i_mode
));
3242 return (SET_ERROR(EINVAL
));
3245 ZFS_VERIFY_ZP(tdzp
);
3246 zilog
= zfsvfs
->z_log
;
3249 * POSIX dictates that we return EPERM here.
3250 * Better choices include ENOTSUP or EISDIR.
3252 if (S_ISDIR(sip
->i_mode
)) {
3254 return (SET_ERROR(EPERM
));
3260 * If we are using project inheritance, means if the directory has
3261 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3262 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3263 * such case, we only allow hard link creation in our tree when the
3264 * project IDs are the same.
3266 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3267 tdzp
->z_projid
!= szp
->z_projid
) {
3269 return (SET_ERROR(EXDEV
));
3273 * We check i_sb because snapshots and the ctldir must have different
3276 if (sip
->i_sb
!= ZTOI(tdzp
)->i_sb
|| zfsctl_is_node(sip
)) {
3278 return (SET_ERROR(EXDEV
));
3281 /* Prevent links to .zfs/shares files */
3283 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
3284 &parent
, sizeof (uint64_t))) != 0) {
3288 if (parent
== zfsvfs
->z_shares_dir
) {
3290 return (SET_ERROR(EPERM
));
3293 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3294 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3296 return (SET_ERROR(EILSEQ
));
3298 if (flags
& FIGNORECASE
)
3302 * We do not support links between attributes and non-attributes
3303 * because of the potential security risk of creating links
3304 * into "normal" file space in order to circumvent restrictions
3305 * imposed in attribute space.
3307 if ((szp
->z_pflags
& ZFS_XATTR
) != (tdzp
->z_pflags
& ZFS_XATTR
)) {
3309 return (SET_ERROR(EINVAL
));
3312 owner
= zfs_fuid_map_id(zfsvfs
, KUID_TO_SUID(sip
->i_uid
),
3314 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3316 return (SET_ERROR(EPERM
));
3319 if ((error
= zfs_zaccess(tdzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3326 * Attempt to lock directory; fail if entry already exists.
3328 error
= zfs_dirent_lock(&dl
, tdzp
, name
, &tzp
, zf
, NULL
, NULL
);
3334 tx
= dmu_tx_create(zfsvfs
->z_os
);
3335 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3336 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, name
);
3338 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3340 zfs_sa_upgrade_txholds(tx
, szp
);
3341 zfs_sa_upgrade_txholds(tx
, tdzp
);
3342 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3344 zfs_dirent_unlock(dl
);
3345 if (error
== ERESTART
) {
3355 /* unmark z_unlinked so zfs_link_create will not reject */
3357 szp
->z_unlinked
= B_FALSE
;
3358 error
= zfs_link_create(dl
, szp
, tx
, 0);
3361 uint64_t txtype
= TX_LINK
;
3363 * tmpfile is created to be in z_unlinkedobj, so remove it.
3364 * Also, we don't log in ZIL, because all previous file
3365 * operation on the tmpfile are ignored by ZIL. Instead we
3366 * always wait for txg to sync to make sure all previous
3367 * operation are sync safe.
3370 VERIFY(zap_remove_int(zfsvfs
->z_os
,
3371 zfsvfs
->z_unlinkedobj
, szp
->z_id
, tx
) == 0);
3373 if (flags
& FIGNORECASE
)
3375 zfs_log_link(zilog
, tx
, txtype
, tdzp
, szp
, name
);
3377 } else if (is_tmpfile
) {
3378 /* restore z_unlinked since when linking failed */
3379 szp
->z_unlinked
= B_TRUE
;
3381 txg
= dmu_tx_get_txg(tx
);
3384 zfs_dirent_unlock(dl
);
3386 if (!is_tmpfile
&& zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3387 zil_commit(zilog
, 0);
3389 if (is_tmpfile
&& zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
)
3390 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), txg
);
3392 zfs_znode_update_vfs(tdzp
);
3393 zfs_znode_update_vfs(szp
);
3399 zfs_putpage_commit_cb(void *arg
)
3401 struct page
*pp
= arg
;
3404 end_page_writeback(pp
);
3408 * Push a page out to disk, once the page is on stable storage the
3409 * registered commit callback will be run as notification of completion.
3411 * IN: ip - page mapped for inode.
3412 * pp - page to push (page is locked)
3413 * wbc - writeback control data
3415 * RETURN: 0 if success
3416 * error code if failure
3419 * ip - ctime|mtime updated
3422 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3424 znode_t
*zp
= ITOZ(ip
);
3425 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3432 uint64_t mtime
[2], ctime
[2];
3433 sa_bulk_attr_t bulk
[3];
3435 struct address_space
*mapping
;
3440 ASSERT(PageLocked(pp
));
3442 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3443 offset
= i_size_read(ip
); /* File length in bytes */
3444 pglen
= MIN(PAGE_SIZE
, /* Page length in bytes */
3445 P2ROUNDUP(offset
, PAGE_SIZE
)-pgoff
);
3447 /* Page is beyond end of file */
3448 if (pgoff
>= offset
) {
3454 /* Truncate page length to end of file */
3455 if (pgoff
+ pglen
> offset
)
3456 pglen
= offset
- pgoff
;
3460 * FIXME: Allow mmap writes past its quota. The correct fix
3461 * is to register a page_mkwrite() handler to count the page
3462 * against its quota when it is about to be dirtied.
3464 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
3465 KUID_TO_SUID(ip
->i_uid
)) ||
3466 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
3467 KGID_TO_SGID(ip
->i_gid
)) ||
3468 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
3469 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
3476 * The ordering here is critical and must adhere to the following
3477 * rules in order to avoid deadlocking in either zfs_read() or
3478 * zfs_free_range() due to a lock inversion.
3480 * 1) The page must be unlocked prior to acquiring the range lock.
3481 * This is critical because zfs_read() calls find_lock_page()
3482 * which may block on the page lock while holding the range lock.
3484 * 2) Before setting or clearing write back on a page the range lock
3485 * must be held in order to prevent a lock inversion with the
3486 * zfs_free_range() function.
3488 * This presents a problem because upon entering this function the
3489 * page lock is already held. To safely acquire the range lock the
3490 * page lock must be dropped. This creates a window where another
3491 * process could truncate, invalidate, dirty, or write out the page.
3493 * Therefore, after successfully reacquiring the range and page locks
3494 * the current page state is checked. In the common case everything
3495 * will be as is expected and it can be written out. However, if
3496 * the page state has changed it must be handled accordingly.
3498 mapping
= pp
->mapping
;
3499 redirty_page_for_writepage(wbc
, pp
);
3502 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
3503 pgoff
, pglen
, RL_WRITER
);
3506 /* Page mapping changed or it was no longer dirty, we're done */
3507 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
3509 zfs_rangelock_exit(lr
);
3514 /* Another process started write block if required */
3515 if (PageWriteback(pp
)) {
3517 zfs_rangelock_exit(lr
);
3519 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3520 if (PageWriteback(pp
))
3521 #ifdef HAVE_PAGEMAP_FOLIO_WAIT_BIT
3522 folio_wait_bit(page_folio(pp
), PG_writeback
);
3524 wait_on_page_bit(pp
, PG_writeback
);
3532 /* Clear the dirty flag the required locks are held */
3533 if (!clear_page_dirty_for_io(pp
)) {
3535 zfs_rangelock_exit(lr
);
3541 * Counterpart for redirty_page_for_writepage() above. This page
3542 * was in fact not skipped and should not be counted as if it were.
3544 wbc
->pages_skipped
--;
3545 set_page_writeback(pp
);
3548 tx
= dmu_tx_create(zfsvfs
->z_os
);
3549 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3550 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3551 zfs_sa_upgrade_txholds(tx
, zp
);
3553 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3555 if (err
== ERESTART
)
3559 #ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
3560 filemap_dirty_folio(page_mapping(pp
), page_folio(pp
));
3562 __set_page_dirty_nobuffers(pp
);
3565 end_page_writeback(pp
);
3566 zfs_rangelock_exit(lr
);
3572 ASSERT3U(pglen
, <=, PAGE_SIZE
);
3573 dmu_write(zfsvfs
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
3576 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
3577 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
3578 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
3581 /* Preserve the mtime and ctime provided by the inode */
3582 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3583 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
3584 zp
->z_atime_dirty
= B_FALSE
;
3587 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3589 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
3590 zfs_putpage_commit_cb
, pp
);
3593 zfs_rangelock_exit(lr
);
3595 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3597 * Note that this is rarely called under writepages(), because
3598 * writepages() normally handles the entire commit for
3599 * performance reasons.
3601 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
3604 dataset_kstats_update_write_kstats(&zfsvfs
->z_kstat
, pglen
);
3611 * Update the system attributes when the inode has been dirtied. For the
3612 * moment we only update the mode, atime, mtime, and ctime.
3615 zfs_dirty_inode(struct inode
*ip
, int flags
)
3617 znode_t
*zp
= ITOZ(ip
);
3618 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3620 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
3621 sa_bulk_attr_t bulk
[4];
3625 if (zfs_is_readonly(zfsvfs
) || dmu_objset_is_snapshot(zfsvfs
->z_os
))
3633 * This is the lazytime semantic introduced in Linux 4.0
3634 * This flag will only be called from update_time when lazytime is set.
3635 * (Note, I_DIRTY_SYNC will also set if not lazytime)
3636 * Fortunately mtime and ctime are managed within ZFS itself, so we
3637 * only need to dirty atime.
3639 if (flags
== I_DIRTY_TIME
) {
3640 zp
->z_atime_dirty
= B_TRUE
;
3645 tx
= dmu_tx_create(zfsvfs
->z_os
);
3647 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3648 zfs_sa_upgrade_txholds(tx
, zp
);
3650 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3656 mutex_enter(&zp
->z_lock
);
3657 zp
->z_atime_dirty
= B_FALSE
;
3659 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
3660 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
3661 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
3662 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
3664 /* Preserve the mode, mtime and ctime provided by the inode */
3665 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3666 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3667 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
3672 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3673 mutex_exit(&zp
->z_lock
);
3682 zfs_inactive(struct inode
*ip
)
3684 znode_t
*zp
= ITOZ(ip
);
3685 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3688 int need_unlock
= 0;
3690 /* Only read lock if we haven't already write locked, e.g. rollback */
3691 if (!RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
)) {
3693 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
3695 if (zp
->z_sa_hdl
== NULL
) {
3697 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3701 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== B_FALSE
) {
3702 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
3704 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3705 zfs_sa_upgrade_txholds(tx
, zp
);
3706 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3710 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3711 mutex_enter(&zp
->z_lock
);
3712 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
3713 (void *)&atime
, sizeof (atime
), tx
);
3714 zp
->z_atime_dirty
= B_FALSE
;
3715 mutex_exit(&zp
->z_lock
);
3722 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3726 * Fill pages with data from the disk.
3729 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
3731 znode_t
*zp
= ITOZ(ip
);
3732 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3734 struct page
*cur_pp
;
3735 u_offset_t io_off
, total
;
3742 io_len
= nr_pages
<< PAGE_SHIFT
;
3743 i_size
= i_size_read(ip
);
3744 io_off
= page_offset(pl
[0]);
3746 if (io_off
+ io_len
> i_size
)
3747 io_len
= i_size
- io_off
;
3750 * Iterate over list of pages and read each page individually.
3753 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
3756 cur_pp
= pl
[page_idx
++];
3758 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
3762 /* convert checksum errors into IO errors */
3764 err
= SET_ERROR(EIO
);
3773 * Uses zfs_fillpage to read data from the file and fill the pages.
3775 * IN: ip - inode of file to get data from.
3776 * pl - list of pages to read
3777 * nr_pages - number of pages to read
3779 * RETURN: 0 on success, error code on failure.
3782 * vp - atime updated
3785 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
3787 znode_t
*zp
= ITOZ(ip
);
3788 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3797 err
= zfs_fillpage(ip
, pl
, nr_pages
);
3799 dataset_kstats_update_read_kstats(&zfsvfs
->z_kstat
, nr_pages
*PAGESIZE
);
3806 * Check ZFS specific permissions to memory map a section of a file.
3808 * IN: ip - inode of the file to mmap
3810 * addrp - start address in memory region
3811 * len - length of memory region
3812 * vm_flags- address flags
3814 * RETURN: 0 if success
3815 * error code if failure
3818 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
3819 unsigned long vm_flags
)
3822 znode_t
*zp
= ITOZ(ip
);
3823 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3828 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
3829 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
3831 return (SET_ERROR(EPERM
));
3834 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
3835 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
3837 return (SET_ERROR(EACCES
));
3840 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
3842 return (SET_ERROR(ENXIO
));
3850 * Free or allocate space in a file. Currently, this function only
3851 * supports the `F_FREESP' command. However, this command is somewhat
3852 * misnamed, as its functionality includes the ability to allocate as
3853 * well as free space.
3855 * IN: zp - znode of file to free data in.
3856 * cmd - action to take (only F_FREESP supported).
3857 * bfp - section of file to free/alloc.
3858 * flag - current file open mode flags.
3859 * offset - current file offset.
3860 * cr - credentials of caller.
3862 * RETURN: 0 on success, error code on failure.
3865 * zp - ctime|mtime updated
3868 zfs_space(znode_t
*zp
, int cmd
, flock64_t
*bfp
, int flag
,
3869 offset_t offset
, cred_t
*cr
)
3872 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
3879 if (cmd
!= F_FREESP
) {
3881 return (SET_ERROR(EINVAL
));
3885 * Callers might not be able to detect properly that we are read-only,
3886 * so check it explicitly here.
3888 if (zfs_is_readonly(zfsvfs
)) {
3890 return (SET_ERROR(EROFS
));
3893 if (bfp
->l_len
< 0) {
3895 return (SET_ERROR(EINVAL
));
3899 * Permissions aren't checked on Solaris because on this OS
3900 * zfs_space() can only be called with an opened file handle.
3901 * On Linux we can get here through truncate_range() which
3902 * operates directly on inodes, so we need to check access rights.
3904 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
3910 len
= bfp
->l_len
; /* 0 means from off to end of file */
3912 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
3919 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
3921 znode_t
*zp
= ITOZ(ip
);
3922 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3925 uint64_t object
= zp
->z_id
;
3931 if (fidp
->fid_len
< SHORT_FID_LEN
) {
3932 fidp
->fid_len
= SHORT_FID_LEN
;
3934 return (SET_ERROR(ENOSPC
));
3939 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
3940 &gen64
, sizeof (uint64_t))) != 0) {
3945 gen
= (uint32_t)gen64
;
3947 size
= SHORT_FID_LEN
;
3949 zfid
= (zfid_short_t
*)fidp
;
3951 zfid
->zf_len
= size
;
3953 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
3954 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
3956 /* Must have a non-zero generation number to distinguish from .zfs */
3959 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
3960 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
3966 #if defined(_KERNEL)
3967 EXPORT_SYMBOL(zfs_open
);
3968 EXPORT_SYMBOL(zfs_close
);
3969 EXPORT_SYMBOL(zfs_lookup
);
3970 EXPORT_SYMBOL(zfs_create
);
3971 EXPORT_SYMBOL(zfs_tmpfile
);
3972 EXPORT_SYMBOL(zfs_remove
);
3973 EXPORT_SYMBOL(zfs_mkdir
);
3974 EXPORT_SYMBOL(zfs_rmdir
);
3975 EXPORT_SYMBOL(zfs_readdir
);
3976 EXPORT_SYMBOL(zfs_getattr_fast
);
3977 EXPORT_SYMBOL(zfs_setattr
);
3978 EXPORT_SYMBOL(zfs_rename
);
3979 EXPORT_SYMBOL(zfs_symlink
);
3980 EXPORT_SYMBOL(zfs_readlink
);
3981 EXPORT_SYMBOL(zfs_link
);
3982 EXPORT_SYMBOL(zfs_inactive
);
3983 EXPORT_SYMBOL(zfs_space
);
3984 EXPORT_SYMBOL(zfs_fid
);
3985 EXPORT_SYMBOL(zfs_getpage
);
3986 EXPORT_SYMBOL(zfs_putpage
);
3987 EXPORT_SYMBOL(zfs_dirty_inode
);
3988 EXPORT_SYMBOL(zfs_map
);
3991 module_param(zfs_delete_blocks
, ulong
, 0644);
3992 MODULE_PARM_DESC(zfs_delete_blocks
, "Delete files larger than N blocks async");