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
180 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
182 znode_t
*zp
= ITOZ(ip
);
183 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
188 /* Honor ZFS_APPENDONLY file attribute */
189 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
190 ((flag
& O_APPEND
) == 0)) {
192 return (SET_ERROR(EPERM
));
195 /* Keep a count of the synchronous opens in the znode */
197 atomic_inc_32(&zp
->z_sync_cnt
);
205 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
207 znode_t
*zp
= ITOZ(ip
);
208 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
213 /* Decrement the synchronous opens in the znode */
215 atomic_dec_32(&zp
->z_sync_cnt
);
223 * When a file is memory mapped, we must keep the IO data synchronized
224 * between the DMU cache and the memory mapped pages. What this means:
226 * On Write: If we find a memory mapped page, we write to *both*
227 * the page and the dmu buffer.
230 update_pages(znode_t
*zp
, int64_t start
, int len
, objset_t
*os
)
232 struct inode
*ip
= ZTOI(zp
);
233 struct address_space
*mp
= ip
->i_mapping
;
239 off
= start
& (PAGE_SIZE
-1);
240 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
241 nbytes
= MIN(PAGE_SIZE
- off
, len
);
243 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
245 if (mapping_writably_mapped(mp
))
246 flush_dcache_page(pp
);
249 (void) dmu_read(os
, zp
->z_id
, start
+ off
, nbytes
,
250 pb
+ off
, DMU_READ_PREFETCH
);
253 if (mapping_writably_mapped(mp
))
254 flush_dcache_page(pp
);
256 mark_page_accessed(pp
);
269 * When a file is memory mapped, we must keep the IO data synchronized
270 * between the DMU cache and the memory mapped pages. What this means:
272 * On Read: We "read" preferentially from memory mapped pages,
273 * else we default from the dmu buffer.
275 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
276 * the file is memory mapped.
279 mappedread(znode_t
*zp
, int nbytes
, zfs_uio_t
*uio
)
281 struct inode
*ip
= ZTOI(zp
);
282 struct address_space
*mp
= ip
->i_mapping
;
290 start
= uio
->uio_loffset
;
291 off
= start
& (PAGE_SIZE
-1);
292 for (start
&= PAGE_MASK
; len
> 0; start
+= PAGE_SIZE
) {
293 bytes
= MIN(PAGE_SIZE
- off
, len
);
295 pp
= find_lock_page(mp
, start
>> PAGE_SHIFT
);
297 ASSERT(PageUptodate(pp
));
301 error
= zfs_uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
304 if (mapping_writably_mapped(mp
))
305 flush_dcache_page(pp
);
307 mark_page_accessed(pp
);
310 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
323 static unsigned long zfs_delete_blocks
= DMU_MAX_DELETEBLKCNT
;
326 * Write the bytes to a file.
328 * IN: zp - znode of file to be written to
329 * data - bytes to write
330 * len - number of bytes to write
331 * pos - offset to start writing at
333 * OUT: resid - remaining bytes to write
335 * RETURN: 0 if success
336 * positive error code if failure. EIO is returned
337 * for a short write when residp isn't provided.
340 * zp - ctime|mtime updated if byte count > 0
343 zfs_write_simple(znode_t
*zp
, const void *data
, size_t len
,
344 loff_t pos
, size_t *residp
)
346 fstrans_cookie_t cookie
;
350 iov
.iov_base
= (void *)data
;
354 zfs_uio_iovec_init(&uio
, &iov
, 1, pos
, UIO_SYSSPACE
, len
, 0);
356 cookie
= spl_fstrans_mark();
357 error
= zfs_write(zp
, &uio
, 0, kcred
);
358 spl_fstrans_unmark(cookie
);
362 *residp
= zfs_uio_resid(&uio
);
363 else if (zfs_uio_resid(&uio
) != 0)
364 error
= SET_ERROR(EIO
);
371 zfs_rele_async_task(void *arg
)
377 zfs_zrele_async(znode_t
*zp
)
379 struct inode
*ip
= ZTOI(zp
);
380 objset_t
*os
= ITOZSB(ip
)->z_os
;
382 ASSERT(atomic_read(&ip
->i_count
) > 0);
386 * If decrementing the count would put us at 0, we can't do it inline
387 * here, because that would be synchronous. Instead, dispatch an iput
390 * For more information on the dangers of a synchronous iput, see the
391 * header comment of this file.
393 if (!atomic_add_unless(&ip
->i_count
, -1, 1)) {
394 VERIFY(taskq_dispatch(dsl_pool_zrele_taskq(dmu_objset_pool(os
)),
395 zfs_rele_async_task
, ip
, TQ_SLEEP
) != TASKQID_INVALID
);
401 * Lookup an entry in a directory, or an extended attribute directory.
402 * If it exists, return a held inode reference for it.
404 * IN: zdp - znode of directory to search.
405 * nm - name of entry to lookup.
406 * flags - LOOKUP_XATTR set if looking for an attribute.
407 * cr - credentials of caller.
408 * direntflags - directory lookup flags
409 * realpnp - returned pathname.
411 * OUT: zpp - znode of located entry, NULL if not found.
413 * RETURN: 0 on success, error code on failure.
420 zfs_lookup(znode_t
*zdp
, char *nm
, znode_t
**zpp
, int flags
, cred_t
*cr
,
421 int *direntflags
, pathname_t
*realpnp
)
423 zfsvfs_t
*zfsvfs
= ZTOZSB(zdp
);
427 * Fast path lookup, however we must skip DNLC lookup
428 * for case folding or normalizing lookups because the
429 * DNLC code only stores the passed in name. This means
430 * creating 'a' and removing 'A' on a case insensitive
431 * file system would work, but DNLC still thinks 'a'
432 * exists and won't let you create it again on the next
433 * pass through fast path.
435 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
437 if (!S_ISDIR(ZTOI(zdp
)->i_mode
)) {
438 return (SET_ERROR(ENOTDIR
));
439 } else if (zdp
->z_sa_hdl
== NULL
) {
440 return (SET_ERROR(EIO
));
443 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
444 error
= zfs_fastaccesschk_execute(zdp
, cr
);
459 if (flags
& LOOKUP_XATTR
) {
461 * We don't allow recursive attributes..
462 * Maybe someday we will.
464 if (zdp
->z_pflags
& ZFS_XATTR
) {
466 return (SET_ERROR(EINVAL
));
469 if ((error
= zfs_get_xattrdir(zdp
, zpp
, cr
, flags
))) {
475 * Do we have permission to get into attribute directory?
478 if ((error
= zfs_zaccess(*zpp
, ACE_EXECUTE
, 0,
488 if (!S_ISDIR(ZTOI(zdp
)->i_mode
)) {
490 return (SET_ERROR(ENOTDIR
));
494 * Check accessibility of directory.
497 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
502 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
503 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
505 return (SET_ERROR(EILSEQ
));
508 error
= zfs_dirlook(zdp
, nm
, zpp
, flags
, direntflags
, realpnp
);
509 if ((error
== 0) && (*zpp
))
510 zfs_znode_update_vfs(*zpp
);
517 * Attempt to create a new entry in a directory. If the entry
518 * already exists, truncate the file if permissible, else return
519 * an error. Return the ip of the created or trunc'd file.
521 * IN: dzp - znode of directory to put new file entry in.
522 * name - name of new file entry.
523 * vap - attributes of new file.
524 * excl - flag indicating exclusive or non-exclusive mode.
525 * mode - mode to open file with.
526 * cr - credentials of caller.
528 * vsecp - ACL to be set
530 * OUT: zpp - znode of created or trunc'd entry.
532 * RETURN: 0 on success, error code on failure.
535 * dzp - ctime|mtime updated if new entry created
536 * zp - ctime|mtime always, atime if new
541 zfs_create(znode_t
*dzp
, char *name
, vattr_t
*vap
, int excl
,
542 int mode
, znode_t
**zpp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
545 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
553 zfs_acl_ids_t acl_ids
;
554 boolean_t fuid_dirtied
;
555 boolean_t have_acl
= B_FALSE
;
556 boolean_t waited
= B_FALSE
;
559 * If we have an ephemeral id, ACL, or XVATTR then
560 * make sure file system is at proper version
566 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
567 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
568 return (SET_ERROR(EINVAL
));
571 return (SET_ERROR(EINVAL
));
576 zilog
= zfsvfs
->z_log
;
578 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
579 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
581 return (SET_ERROR(EILSEQ
));
584 if (vap
->va_mask
& ATTR_XVATTR
) {
585 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
586 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
596 * Null component name refers to the directory itself.
603 /* possible igrab(zp) */
606 if (flag
& FIGNORECASE
)
609 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
613 zfs_acl_ids_free(&acl_ids
);
614 if (strcmp(name
, "..") == 0)
615 error
= SET_ERROR(EISDIR
);
623 uint64_t projid
= ZFS_DEFAULT_PROJID
;
626 * Create a new file object and update the directory
629 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
631 zfs_acl_ids_free(&acl_ids
);
636 * We only support the creation of regular files in
637 * extended attribute directories.
640 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
642 zfs_acl_ids_free(&acl_ids
);
643 error
= SET_ERROR(EINVAL
);
647 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
648 cr
, vsecp
, &acl_ids
)) != 0)
652 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
653 projid
= zfs_inherit_projid(dzp
);
654 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
655 zfs_acl_ids_free(&acl_ids
);
656 error
= SET_ERROR(EDQUOT
);
660 tx
= dmu_tx_create(os
);
662 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
663 ZFS_SA_BASE_ATTR_SIZE
);
665 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
667 zfs_fuid_txhold(zfsvfs
, tx
);
668 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
669 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
670 if (!zfsvfs
->z_use_sa
&&
671 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
672 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
673 0, acl_ids
.z_aclp
->z_acl_bytes
);
676 error
= dmu_tx_assign(tx
,
677 (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
679 zfs_dirent_unlock(dl
);
680 if (error
== ERESTART
) {
686 zfs_acl_ids_free(&acl_ids
);
691 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
693 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
696 * Since, we failed to add the directory entry for it,
697 * delete the newly created dnode.
699 zfs_znode_delete(zp
, tx
);
700 remove_inode_hash(ZTOI(zp
));
701 zfs_acl_ids_free(&acl_ids
);
707 zfs_fuid_sync(zfsvfs
, tx
);
709 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
710 if (flag
& FIGNORECASE
)
712 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
713 vsecp
, acl_ids
.z_fuidp
, vap
);
714 zfs_acl_ids_free(&acl_ids
);
717 int aflags
= (flag
& O_APPEND
) ? V_APPEND
: 0;
720 zfs_acl_ids_free(&acl_ids
);
724 * A directory entry already exists for this name.
727 * Can't truncate an existing file if in exclusive mode.
730 error
= SET_ERROR(EEXIST
);
734 * Can't open a directory for writing.
736 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
737 error
= SET_ERROR(EISDIR
);
741 * Verify requested access to file.
743 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
747 mutex_enter(&dzp
->z_lock
);
749 mutex_exit(&dzp
->z_lock
);
752 * Truncate regular files if requested.
754 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
755 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
756 /* we can't hold any locks when calling zfs_freesp() */
758 zfs_dirent_unlock(dl
);
761 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
767 zfs_dirent_unlock(dl
);
773 zfs_znode_update_vfs(dzp
);
774 zfs_znode_update_vfs(zp
);
778 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
779 zil_commit(zilog
, 0);
787 zfs_tmpfile(struct inode
*dip
, vattr_t
*vap
, int excl
,
788 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
790 znode_t
*zp
= NULL
, *dzp
= ITOZ(dip
);
791 zfsvfs_t
*zfsvfs
= ITOZSB(dip
);
797 zfs_acl_ids_t acl_ids
;
798 uint64_t projid
= ZFS_DEFAULT_PROJID
;
799 boolean_t fuid_dirtied
;
800 boolean_t have_acl
= B_FALSE
;
801 boolean_t waited
= B_FALSE
;
804 * If we have an ephemeral id, ACL, or XVATTR then
805 * make sure file system is at proper version
811 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
812 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
813 return (SET_ERROR(EINVAL
));
819 if (vap
->va_mask
& ATTR_XVATTR
) {
820 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
821 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
831 * Create a new file object and update the directory
834 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
836 zfs_acl_ids_free(&acl_ids
);
840 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
841 cr
, vsecp
, &acl_ids
)) != 0)
845 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
846 projid
= zfs_inherit_projid(dzp
);
847 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
848 zfs_acl_ids_free(&acl_ids
);
849 error
= SET_ERROR(EDQUOT
);
853 tx
= dmu_tx_create(os
);
855 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
856 ZFS_SA_BASE_ATTR_SIZE
);
857 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
859 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
861 zfs_fuid_txhold(zfsvfs
, tx
);
862 if (!zfsvfs
->z_use_sa
&&
863 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
864 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
865 0, acl_ids
.z_aclp
->z_acl_bytes
);
867 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
869 if (error
== ERESTART
) {
875 zfs_acl_ids_free(&acl_ids
);
880 zfs_mknode(dzp
, vap
, tx
, cr
, IS_TMPFILE
, &zp
, &acl_ids
);
883 zfs_fuid_sync(zfsvfs
, tx
);
885 /* Add to unlinked set */
886 zp
->z_unlinked
= B_TRUE
;
887 zfs_unlinked_add(zp
, tx
);
888 zfs_acl_ids_free(&acl_ids
);
896 zfs_znode_update_vfs(dzp
);
897 zfs_znode_update_vfs(zp
);
906 * Remove an entry from a directory.
908 * IN: dzp - znode of directory to remove entry from.
909 * name - name of entry to remove.
910 * cr - credentials of caller.
911 * flags - case flags.
913 * RETURN: 0 if success
914 * error code if failure
918 * ip - ctime (if nlink > 0)
921 uint64_t null_xattr
= 0;
925 zfs_remove(znode_t
*dzp
, char *name
, cred_t
*cr
, int flags
)
929 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
931 uint64_t acl_obj
, xattr_obj
;
932 uint64_t xattr_obj_unlinked
= 0;
937 boolean_t may_delete_now
, delete_now
= FALSE
;
938 boolean_t unlinked
, toobig
= FALSE
;
940 pathname_t
*realnmp
= NULL
;
944 boolean_t waited
= B_FALSE
;
947 return (SET_ERROR(EINVAL
));
951 zilog
= zfsvfs
->z_log
;
953 if (flags
& FIGNORECASE
) {
963 * Attempt to lock directory; fail if entry doesn't exist.
965 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
973 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
978 * Need to use rmdir for removing directories.
980 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
981 error
= SET_ERROR(EPERM
);
985 mutex_enter(&zp
->z_lock
);
986 may_delete_now
= atomic_read(&ZTOI(zp
)->i_count
) == 1 &&
988 mutex_exit(&zp
->z_lock
);
991 * We may delete the znode now, or we may put it in the unlinked set;
992 * it depends on whether we're the last link, and on whether there are
993 * other holds on the inode. So we dmu_tx_hold() the right things to
994 * allow for either case.
997 tx
= dmu_tx_create(zfsvfs
->z_os
);
998 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
999 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1000 zfs_sa_upgrade_txholds(tx
, zp
);
1001 zfs_sa_upgrade_txholds(tx
, dzp
);
1002 if (may_delete_now
) {
1003 toobig
= zp
->z_size
> zp
->z_blksz
* zfs_delete_blocks
;
1004 /* if the file is too big, only hold_free a token amount */
1005 dmu_tx_hold_free(tx
, zp
->z_id
, 0,
1006 (toobig
? DMU_MAX_ACCESS
: DMU_OBJECT_END
));
1009 /* are there any extended attributes? */
1010 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1011 &xattr_obj
, sizeof (xattr_obj
));
1012 if (error
== 0 && xattr_obj
) {
1013 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1015 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1016 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1019 mutex_enter(&zp
->z_lock
);
1020 if ((acl_obj
= zfs_external_acl(zp
)) != 0 && may_delete_now
)
1021 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
1022 mutex_exit(&zp
->z_lock
);
1024 /* charge as an update -- would be nice not to charge at all */
1025 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1028 * Mark this transaction as typically resulting in a net free of space
1030 dmu_tx_mark_netfree(tx
);
1032 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1034 zfs_dirent_unlock(dl
);
1035 if (error
== ERESTART
) {
1055 * Remove the directory entry.
1057 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1066 * Hold z_lock so that we can make sure that the ACL obj
1067 * hasn't changed. Could have been deleted due to
1070 mutex_enter(&zp
->z_lock
);
1071 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1072 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1073 delete_now
= may_delete_now
&& !toobig
&&
1074 atomic_read(&ZTOI(zp
)->i_count
) == 1 &&
1075 !(zp
->z_is_mapped
) && xattr_obj
== xattr_obj_unlinked
&&
1076 zfs_external_acl(zp
) == acl_obj
;
1080 if (xattr_obj_unlinked
) {
1081 ASSERT3U(ZTOI(xzp
)->i_nlink
, ==, 2);
1082 mutex_enter(&xzp
->z_lock
);
1083 xzp
->z_unlinked
= B_TRUE
;
1084 clear_nlink(ZTOI(xzp
));
1086 error
= sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
1087 &links
, sizeof (links
), tx
);
1088 ASSERT3U(error
, ==, 0);
1089 mutex_exit(&xzp
->z_lock
);
1090 zfs_unlinked_add(xzp
, tx
);
1093 error
= sa_remove(zp
->z_sa_hdl
,
1094 SA_ZPL_XATTR(zfsvfs
), tx
);
1096 error
= sa_update(zp
->z_sa_hdl
,
1097 SA_ZPL_XATTR(zfsvfs
), &null_xattr
,
1098 sizeof (uint64_t), tx
);
1102 * Add to the unlinked set because a new reference could be
1103 * taken concurrently resulting in a deferred destruction.
1105 zfs_unlinked_add(zp
, tx
);
1106 mutex_exit(&zp
->z_lock
);
1107 } else if (unlinked
) {
1108 mutex_exit(&zp
->z_lock
);
1109 zfs_unlinked_add(zp
, tx
);
1113 if (flags
& FIGNORECASE
)
1115 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
, unlinked
);
1122 zfs_dirent_unlock(dl
);
1123 zfs_znode_update_vfs(dzp
);
1124 zfs_znode_update_vfs(zp
);
1129 zfs_zrele_async(zp
);
1132 zfs_znode_update_vfs(xzp
);
1133 zfs_zrele_async(xzp
);
1136 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1137 zil_commit(zilog
, 0);
1144 * Create a new directory and insert it into dzp using the name
1145 * provided. Return a pointer to the inserted directory.
1147 * IN: dzp - znode of directory to add subdir to.
1148 * dirname - name of new directory.
1149 * vap - attributes of new directory.
1150 * cr - credentials of caller.
1151 * flags - case flags.
1152 * vsecp - ACL to be set
1154 * OUT: zpp - znode of created directory.
1156 * RETURN: 0 if success
1157 * error code if failure
1160 * dzp - ctime|mtime updated
1161 * zpp - ctime|mtime|atime updated
1165 zfs_mkdir(znode_t
*dzp
, char *dirname
, vattr_t
*vap
, znode_t
**zpp
,
1166 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1169 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1177 gid_t gid
= crgetgid(cr
);
1178 zfs_acl_ids_t acl_ids
;
1179 boolean_t fuid_dirtied
;
1180 boolean_t waited
= B_FALSE
;
1182 ASSERT(S_ISDIR(vap
->va_mode
));
1185 * If we have an ephemeral id, ACL, or XVATTR then
1186 * make sure file system is at proper version
1190 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1191 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1192 return (SET_ERROR(EINVAL
));
1194 if (dirname
== NULL
)
1195 return (SET_ERROR(EINVAL
));
1199 zilog
= zfsvfs
->z_log
;
1201 if (dzp
->z_pflags
& ZFS_XATTR
) {
1203 return (SET_ERROR(EINVAL
));
1206 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1207 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1209 return (SET_ERROR(EILSEQ
));
1211 if (flags
& FIGNORECASE
)
1214 if (vap
->va_mask
& ATTR_XVATTR
) {
1215 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1216 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1222 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1223 vsecp
, &acl_ids
)) != 0) {
1228 * First make sure the new directory doesn't exist.
1230 * Existence is checked first to make sure we don't return
1231 * EACCES instead of EEXIST which can cause some applications
1237 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1239 zfs_acl_ids_free(&acl_ids
);
1244 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1245 zfs_acl_ids_free(&acl_ids
);
1246 zfs_dirent_unlock(dl
);
1251 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, zfs_inherit_projid(dzp
))) {
1252 zfs_acl_ids_free(&acl_ids
);
1253 zfs_dirent_unlock(dl
);
1255 return (SET_ERROR(EDQUOT
));
1259 * Add a new entry to the directory.
1261 tx
= dmu_tx_create(zfsvfs
->z_os
);
1262 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1263 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1264 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1266 zfs_fuid_txhold(zfsvfs
, tx
);
1267 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1268 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1269 acl_ids
.z_aclp
->z_acl_bytes
);
1272 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1273 ZFS_SA_BASE_ATTR_SIZE
);
1275 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1277 zfs_dirent_unlock(dl
);
1278 if (error
== ERESTART
) {
1284 zfs_acl_ids_free(&acl_ids
);
1293 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1296 * Now put new name in parent dir.
1298 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
1300 zfs_znode_delete(zp
, tx
);
1301 remove_inode_hash(ZTOI(zp
));
1306 zfs_fuid_sync(zfsvfs
, tx
);
1310 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1311 if (flags
& FIGNORECASE
)
1313 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1314 acl_ids
.z_fuidp
, vap
);
1317 zfs_acl_ids_free(&acl_ids
);
1321 zfs_dirent_unlock(dl
);
1323 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1324 zil_commit(zilog
, 0);
1329 zfs_znode_update_vfs(dzp
);
1330 zfs_znode_update_vfs(zp
);
1337 * Remove a directory subdir entry. If the current working
1338 * directory is the same as the subdir to be removed, the
1341 * IN: dzp - znode of directory to remove from.
1342 * name - name of directory to be removed.
1343 * cwd - inode of current working directory.
1344 * cr - credentials of caller.
1345 * flags - case flags
1347 * RETURN: 0 on success, error code on failure.
1350 * dzp - ctime|mtime updated
1354 zfs_rmdir(znode_t
*dzp
, char *name
, znode_t
*cwd
, cred_t
*cr
,
1358 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1364 boolean_t waited
= B_FALSE
;
1367 return (SET_ERROR(EINVAL
));
1371 zilog
= zfsvfs
->z_log
;
1373 if (flags
& FIGNORECASE
)
1379 * Attempt to lock directory; fail if entry doesn't exist.
1381 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1387 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1391 if (!S_ISDIR(ZTOI(zp
)->i_mode
)) {
1392 error
= SET_ERROR(ENOTDIR
);
1397 error
= SET_ERROR(EINVAL
);
1402 * Grab a lock on the directory to make sure that no one is
1403 * trying to add (or lookup) entries while we are removing it.
1405 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1408 * Grab a lock on the parent pointer to make sure we play well
1409 * with the treewalk and directory rename code.
1411 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1413 tx
= dmu_tx_create(zfsvfs
->z_os
);
1414 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1415 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1416 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1417 zfs_sa_upgrade_txholds(tx
, zp
);
1418 zfs_sa_upgrade_txholds(tx
, dzp
);
1419 dmu_tx_mark_netfree(tx
);
1420 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
1422 rw_exit(&zp
->z_parent_lock
);
1423 rw_exit(&zp
->z_name_lock
);
1424 zfs_dirent_unlock(dl
);
1425 if (error
== ERESTART
) {
1438 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1441 uint64_t txtype
= TX_RMDIR
;
1442 if (flags
& FIGNORECASE
)
1444 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
,
1450 rw_exit(&zp
->z_parent_lock
);
1451 rw_exit(&zp
->z_name_lock
);
1453 zfs_dirent_unlock(dl
);
1455 zfs_znode_update_vfs(dzp
);
1456 zfs_znode_update_vfs(zp
);
1459 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1460 zil_commit(zilog
, 0);
1467 * Read directory entries from the given directory cursor position and emit
1468 * name and position for each entry.
1470 * IN: ip - inode of directory to read.
1471 * ctx - directory entry context.
1472 * cr - credentials of caller.
1474 * RETURN: 0 if success
1475 * error code if failure
1478 * ip - atime updated
1480 * Note that the low 4 bits of the cookie returned by zap is always zero.
1481 * This allows us to use the low range for "special" directory entries:
1482 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1483 * we use the offset 2 for the '.zfs' directory.
1487 zfs_readdir(struct inode
*ip
, zpl_dir_context_t
*ctx
, cred_t
*cr
)
1489 znode_t
*zp
= ITOZ(ip
);
1490 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1493 zap_attribute_t zap
;
1499 uint64_t offset
; /* must be unsigned; checks for < 1 */
1504 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
1505 &parent
, sizeof (parent
))) != 0)
1509 * Quit if directory has been removed (posix)
1517 prefetch
= zp
->z_zn_prefetch
;
1520 * Initialize the iterator cursor.
1524 * Start iteration from the beginning of the directory.
1526 zap_cursor_init(&zc
, os
, zp
->z_id
);
1529 * The offset is a serialized cursor.
1531 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
1535 * Transform to file-system independent format
1540 * Special case `.', `..', and `.zfs'.
1543 (void) strcpy(zap
.za_name
, ".");
1544 zap
.za_normalization_conflict
= 0;
1547 } else if (offset
== 1) {
1548 (void) strcpy(zap
.za_name
, "..");
1549 zap
.za_normalization_conflict
= 0;
1552 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
1553 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
1554 zap
.za_normalization_conflict
= 0;
1555 objnum
= ZFSCTL_INO_ROOT
;
1561 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
1562 if (error
== ENOENT
)
1569 * Allow multiple entries provided the first entry is
1570 * the object id. Non-zpl consumers may safely make
1571 * use of the additional space.
1573 * XXX: This should be a feature flag for compatibility
1575 if (zap
.za_integer_length
!= 8 ||
1576 zap
.za_num_integers
== 0) {
1577 cmn_err(CE_WARN
, "zap_readdir: bad directory "
1578 "entry, obj = %lld, offset = %lld, "
1579 "length = %d, num = %lld\n",
1580 (u_longlong_t
)zp
->z_id
,
1581 (u_longlong_t
)offset
,
1582 zap
.za_integer_length
,
1583 (u_longlong_t
)zap
.za_num_integers
);
1584 error
= SET_ERROR(ENXIO
);
1588 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
1589 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
1592 done
= !zpl_dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
1597 /* Prefetch znode */
1599 dmu_prefetch(os
, objnum
, 0, 0, 0,
1600 ZIO_PRIORITY_SYNC_READ
);
1604 * Move to the next entry, fill in the previous offset.
1606 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
1607 zap_cursor_advance(&zc
);
1608 offset
= zap_cursor_serialize(&zc
);
1614 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
1617 zap_cursor_fini(&zc
);
1618 if (error
== ENOENT
)
1627 * Get the basic file attributes and place them in the provided kstat
1628 * structure. The inode is assumed to be the authoritative source
1629 * for most of the attributes. However, the znode currently has the
1630 * authoritative atime, blksize, and block count.
1632 * IN: ip - inode of file.
1634 * OUT: sp - kstat values.
1636 * RETURN: 0 (always succeeds)
1640 zfs_getattr_fast(struct user_namespace
*user_ns
, struct inode
*ip
,
1643 znode_t
*zp
= ITOZ(ip
);
1644 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1646 u_longlong_t nblocks
;
1651 mutex_enter(&zp
->z_lock
);
1653 zpl_generic_fillattr(user_ns
, ip
, sp
);
1655 * +1 link count for root inode with visible '.zfs' directory.
1657 if ((zp
->z_id
== zfsvfs
->z_root
) && zfs_show_ctldir(zp
))
1658 if (sp
->nlink
< ZFS_LINK_MAX
)
1661 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
1662 sp
->blksize
= blksize
;
1663 sp
->blocks
= nblocks
;
1665 if (unlikely(zp
->z_blksz
== 0)) {
1667 * Block size hasn't been set; suggest maximal I/O transfers.
1669 sp
->blksize
= zfsvfs
->z_max_blksz
;
1672 mutex_exit(&zp
->z_lock
);
1675 * Required to prevent NFS client from detecting different inode
1676 * numbers of snapshot root dentry before and after snapshot mount.
1678 if (zfsvfs
->z_issnap
) {
1679 if (ip
->i_sb
->s_root
->d_inode
== ip
)
1680 sp
->ino
= ZFSCTL_INO_SNAPDIRS
-
1681 dmu_objset_id(zfsvfs
->z_os
);
1690 * For the operation of changing file's user/group/project, we need to
1691 * handle not only the main object that is assigned to the file directly,
1692 * but also the ones that are used by the file via hidden xattr directory.
1694 * Because the xattr directory may contains many EA entries, as to it may
1695 * be impossible to change all of them via the transaction of changing the
1696 * main object's user/group/project attributes. Then we have to change them
1697 * via other multiple independent transactions one by one. It may be not good
1698 * solution, but we have no better idea yet.
1701 zfs_setattr_dir(znode_t
*dzp
)
1703 struct inode
*dxip
= ZTOI(dzp
);
1704 struct inode
*xip
= NULL
;
1705 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
1706 objset_t
*os
= zfsvfs
->z_os
;
1708 zap_attribute_t zap
;
1711 dmu_tx_t
*tx
= NULL
;
1713 sa_bulk_attr_t bulk
[4];
1717 zap_cursor_init(&zc
, os
, dzp
->z_id
);
1718 while ((err
= zap_cursor_retrieve(&zc
, &zap
)) == 0) {
1720 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
1725 err
= zfs_dirent_lock(&dl
, dzp
, (char *)zap
.za_name
, &zp
,
1726 ZEXISTS
, NULL
, NULL
);
1733 if (KUID_TO_SUID(xip
->i_uid
) == KUID_TO_SUID(dxip
->i_uid
) &&
1734 KGID_TO_SGID(xip
->i_gid
) == KGID_TO_SGID(dxip
->i_gid
) &&
1735 zp
->z_projid
== dzp
->z_projid
)
1738 tx
= dmu_tx_create(os
);
1739 if (!(zp
->z_pflags
& ZFS_PROJID
))
1740 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1742 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1744 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1748 mutex_enter(&dzp
->z_lock
);
1750 if (KUID_TO_SUID(xip
->i_uid
) != KUID_TO_SUID(dxip
->i_uid
)) {
1751 xip
->i_uid
= dxip
->i_uid
;
1752 uid
= zfs_uid_read(dxip
);
1753 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
1754 &uid
, sizeof (uid
));
1757 if (KGID_TO_SGID(xip
->i_gid
) != KGID_TO_SGID(dxip
->i_gid
)) {
1758 xip
->i_gid
= dxip
->i_gid
;
1759 gid
= zfs_gid_read(dxip
);
1760 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
,
1761 &gid
, sizeof (gid
));
1764 if (zp
->z_projid
!= dzp
->z_projid
) {
1765 if (!(zp
->z_pflags
& ZFS_PROJID
)) {
1766 zp
->z_pflags
|= ZFS_PROJID
;
1767 SA_ADD_BULK_ATTR(bulk
, count
,
1768 SA_ZPL_FLAGS(zfsvfs
), NULL
, &zp
->z_pflags
,
1769 sizeof (zp
->z_pflags
));
1772 zp
->z_projid
= dzp
->z_projid
;
1773 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PROJID(zfsvfs
),
1774 NULL
, &zp
->z_projid
, sizeof (zp
->z_projid
));
1777 mutex_exit(&dzp
->z_lock
);
1779 if (likely(count
> 0)) {
1780 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1786 if (err
!= 0 && err
!= ENOENT
)
1793 zfs_dirent_unlock(dl
);
1795 zap_cursor_advance(&zc
);
1802 zfs_dirent_unlock(dl
);
1804 zap_cursor_fini(&zc
);
1806 return (err
== ENOENT
? 0 : err
);
1810 * Set the file attributes to the values contained in the
1813 * IN: zp - znode of file to be modified.
1814 * vap - new attribute values.
1815 * If ATTR_XVATTR set, then optional attrs are being set
1816 * flags - ATTR_UTIME set if non-default time values provided.
1817 * - ATTR_NOACLCHECK (CIFS context only).
1818 * cr - credentials of caller.
1820 * RETURN: 0 if success
1821 * error code if failure
1824 * ip - ctime updated, mtime updated if size changed.
1828 zfs_setattr(znode_t
*zp
, vattr_t
*vap
, int flags
, cred_t
*cr
)
1831 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
1832 objset_t
*os
= zfsvfs
->z_os
;
1836 xvattr_t
*tmpxvattr
;
1837 uint_t mask
= vap
->va_mask
;
1838 uint_t saved_mask
= 0;
1841 uint64_t new_kuid
= 0, new_kgid
= 0, new_uid
, new_gid
;
1843 uint64_t mtime
[2], ctime
[2], atime
[2];
1844 uint64_t projid
= ZFS_INVALID_PROJID
;
1846 int need_policy
= FALSE
;
1848 zfs_fuid_info_t
*fuidp
= NULL
;
1849 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
1852 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
1853 boolean_t fuid_dirtied
= B_FALSE
;
1854 boolean_t handle_eadir
= B_FALSE
;
1855 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
1856 int count
= 0, xattr_count
= 0, bulks
= 8;
1866 * If this is a xvattr_t, then get a pointer to the structure of
1867 * optional attributes. If this is NULL, then we have a vattr_t.
1869 xoap
= xva_getxoptattr(xvap
);
1870 if (xoap
!= NULL
&& (mask
& ATTR_XVATTR
)) {
1871 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
1872 if (!dmu_objset_projectquota_enabled(os
) ||
1873 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
))) {
1875 return (SET_ERROR(ENOTSUP
));
1878 projid
= xoap
->xoa_projid
;
1879 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
1881 return (SET_ERROR(EINVAL
));
1884 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
1885 projid
= ZFS_INVALID_PROJID
;
1890 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
1891 (xoap
->xoa_projinherit
!=
1892 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) &&
1893 (!dmu_objset_projectquota_enabled(os
) ||
1894 (!S_ISREG(ip
->i_mode
) && !S_ISDIR(ip
->i_mode
)))) {
1896 return (SET_ERROR(ENOTSUP
));
1900 zilog
= zfsvfs
->z_log
;
1903 * Make sure that if we have ephemeral uid/gid or xvattr specified
1904 * that file system is at proper version level
1907 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1908 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
1909 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
1910 (mask
& ATTR_XVATTR
))) {
1912 return (SET_ERROR(EINVAL
));
1915 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
1917 return (SET_ERROR(EISDIR
));
1920 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
1922 return (SET_ERROR(EINVAL
));
1925 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
1926 xva_init(tmpxvattr
);
1928 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
1929 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * bulks
, KM_SLEEP
);
1932 * Immutable files can only alter immutable bit and atime
1934 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
1935 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
1936 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
1937 err
= SET_ERROR(EPERM
);
1941 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
1942 err
= SET_ERROR(EPERM
);
1947 * Verify timestamps doesn't overflow 32 bits.
1948 * ZFS can handle large timestamps, but 32bit syscalls can't
1949 * handle times greater than 2039. This check should be removed
1950 * once large timestamps are fully supported.
1952 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
1953 if (((mask
& ATTR_ATIME
) &&
1954 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
1955 ((mask
& ATTR_MTIME
) &&
1956 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
1957 err
= SET_ERROR(EOVERFLOW
);
1966 /* Can this be moved to before the top label? */
1967 if (zfs_is_readonly(zfsvfs
)) {
1968 err
= SET_ERROR(EROFS
);
1973 * First validate permissions
1976 if (mask
& ATTR_SIZE
) {
1977 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
1982 * XXX - Note, we are not providing any open
1983 * mode flags here (like FNDELAY), so we may
1984 * block if there are locks present... this
1985 * should be addressed in openat().
1987 /* XXX - would it be OK to generate a log record here? */
1988 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
1993 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
1994 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
1995 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
1996 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
1997 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
1998 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
1999 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2000 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2001 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2005 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2006 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2011 * NOTE: even if a new mode is being set,
2012 * we may clear S_ISUID/S_ISGID bits.
2015 if (!(mask
& ATTR_MODE
))
2016 vap
->va_mode
= zp
->z_mode
;
2019 * Take ownership or chgrp to group we are a member of
2022 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2023 take_group
= (mask
& ATTR_GID
) &&
2024 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2027 * If both ATTR_UID and ATTR_GID are set then take_owner and
2028 * take_group must both be set in order to allow taking
2031 * Otherwise, send the check through secpolicy_vnode_setattr()
2035 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2036 take_owner
&& take_group
) ||
2037 ((idmask
== ATTR_UID
) && take_owner
) ||
2038 ((idmask
== ATTR_GID
) && take_group
)) {
2039 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2040 skipaclchk
, cr
) == 0) {
2042 * Remove setuid/setgid for non-privileged users
2044 (void) secpolicy_setid_clear(vap
, cr
);
2045 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2054 mutex_enter(&zp
->z_lock
);
2055 oldva
.va_mode
= zp
->z_mode
;
2056 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2057 if (mask
& ATTR_XVATTR
) {
2059 * Update xvattr mask to include only those attributes
2060 * that are actually changing.
2062 * the bits will be restored prior to actually setting
2063 * the attributes so the caller thinks they were set.
2065 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2066 if (xoap
->xoa_appendonly
!=
2067 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2070 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2071 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2075 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2076 if (xoap
->xoa_projinherit
!=
2077 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
2080 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
2081 XVA_SET_REQ(tmpxvattr
, XAT_PROJINHERIT
);
2085 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2086 if (xoap
->xoa_nounlink
!=
2087 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2090 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2091 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2095 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2096 if (xoap
->xoa_immutable
!=
2097 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2100 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2101 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2105 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2106 if (xoap
->xoa_nodump
!=
2107 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2110 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2111 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2115 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2116 if (xoap
->xoa_av_modified
!=
2117 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2120 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2121 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2125 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2126 if ((!S_ISREG(ip
->i_mode
) &&
2127 xoap
->xoa_av_quarantined
) ||
2128 xoap
->xoa_av_quarantined
!=
2129 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2132 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2133 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2137 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2138 mutex_exit(&zp
->z_lock
);
2139 err
= SET_ERROR(EPERM
);
2143 if (need_policy
== FALSE
&&
2144 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2145 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2150 mutex_exit(&zp
->z_lock
);
2152 if (mask
& ATTR_MODE
) {
2153 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2154 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2159 trim_mask
|= ATTR_MODE
;
2167 * If trim_mask is set then take ownership
2168 * has been granted or write_acl is present and user
2169 * has the ability to modify mode. In that case remove
2170 * UID|GID and or MODE from mask so that
2171 * secpolicy_vnode_setattr() doesn't revoke it.
2175 saved_mask
= vap
->va_mask
;
2176 vap
->va_mask
&= ~trim_mask
;
2178 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2179 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2184 vap
->va_mask
|= saved_mask
;
2188 * secpolicy_vnode_setattr, or take ownership may have
2191 mask
= vap
->va_mask
;
2193 if ((mask
& (ATTR_UID
| ATTR_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
2194 handle_eadir
= B_TRUE
;
2195 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
2196 &xattr_obj
, sizeof (xattr_obj
));
2198 if (err
== 0 && xattr_obj
) {
2199 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2203 if (mask
& ATTR_UID
) {
2204 new_kuid
= zfs_fuid_create(zfsvfs
,
2205 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2206 if (new_kuid
!= KUID_TO_SUID(ZTOI(zp
)->i_uid
) &&
2207 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
2211 err
= SET_ERROR(EDQUOT
);
2216 if (mask
& ATTR_GID
) {
2217 new_kgid
= zfs_fuid_create(zfsvfs
,
2218 (uint64_t)vap
->va_gid
, cr
, ZFS_GROUP
, &fuidp
);
2219 if (new_kgid
!= KGID_TO_SGID(ZTOI(zp
)->i_gid
) &&
2220 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
2224 err
= SET_ERROR(EDQUOT
);
2229 if (projid
!= ZFS_INVALID_PROJID
&&
2230 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
2237 tx
= dmu_tx_create(os
);
2239 if (mask
& ATTR_MODE
) {
2240 uint64_t pmode
= zp
->z_mode
;
2242 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2244 if (ZTOZSB(zp
)->z_acl_mode
== ZFS_ACL_RESTRICTED
&&
2245 !(zp
->z_pflags
& ZFS_ACL_TRIVIAL
)) {
2250 if ((err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
)))
2253 mutex_enter(&zp
->z_lock
);
2254 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2256 * Are we upgrading ACL from old V0 format
2259 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
2260 zfs_znode_acl_version(zp
) ==
2261 ZFS_ACL_VERSION_INITIAL
) {
2262 dmu_tx_hold_free(tx
, acl_obj
, 0,
2264 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2265 0, aclp
->z_acl_bytes
);
2267 dmu_tx_hold_write(tx
, acl_obj
, 0,
2270 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2271 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2272 0, aclp
->z_acl_bytes
);
2274 mutex_exit(&zp
->z_lock
);
2275 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2277 if (((mask
& ATTR_XVATTR
) &&
2278 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
2279 (projid
!= ZFS_INVALID_PROJID
&&
2280 !(zp
->z_pflags
& ZFS_PROJID
)))
2281 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2283 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2287 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2290 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2292 zfs_fuid_txhold(zfsvfs
, tx
);
2294 zfs_sa_upgrade_txholds(tx
, zp
);
2296 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2302 * Set each attribute requested.
2303 * We group settings according to the locks they need to acquire.
2305 * Note: you cannot set ctime directly, although it will be
2306 * updated as a side-effect of calling this function.
2309 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
2311 * For the existed object that is upgraded from old system,
2312 * its on-disk layout has no slot for the project ID attribute.
2313 * But quota accounting logic needs to access related slots by
2314 * offset directly. So we need to adjust old objects' layout
2315 * to make the project ID to some unified and fixed offset.
2318 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
2320 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
2322 if (unlikely(err
== EEXIST
))
2327 projid
= ZFS_INVALID_PROJID
;
2330 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2331 mutex_enter(&zp
->z_acl_lock
);
2332 mutex_enter(&zp
->z_lock
);
2334 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
2335 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2338 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2339 mutex_enter(&attrzp
->z_acl_lock
);
2340 mutex_enter(&attrzp
->z_lock
);
2341 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2342 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
2343 sizeof (attrzp
->z_pflags
));
2344 if (projid
!= ZFS_INVALID_PROJID
) {
2345 attrzp
->z_projid
= projid
;
2346 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2347 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
2348 sizeof (attrzp
->z_projid
));
2352 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2354 if (mask
& ATTR_UID
) {
2355 ZTOI(zp
)->i_uid
= SUID_TO_KUID(new_kuid
);
2356 new_uid
= zfs_uid_read(ZTOI(zp
));
2357 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2358 &new_uid
, sizeof (new_uid
));
2360 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2361 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
2363 ZTOI(attrzp
)->i_uid
= SUID_TO_KUID(new_uid
);
2367 if (mask
& ATTR_GID
) {
2368 ZTOI(zp
)->i_gid
= SGID_TO_KGID(new_kgid
);
2369 new_gid
= zfs_gid_read(ZTOI(zp
));
2370 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
2371 NULL
, &new_gid
, sizeof (new_gid
));
2373 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2374 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
2376 ZTOI(attrzp
)->i_gid
= SGID_TO_KGID(new_kgid
);
2379 if (!(mask
& ATTR_MODE
)) {
2380 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
2381 NULL
, &new_mode
, sizeof (new_mode
));
2382 new_mode
= zp
->z_mode
;
2384 err
= zfs_acl_chown_setattr(zp
);
2387 err
= zfs_acl_chown_setattr(attrzp
);
2392 if (mask
& ATTR_MODE
) {
2393 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
2394 &new_mode
, sizeof (new_mode
));
2395 zp
->z_mode
= ZTOI(zp
)->i_mode
= new_mode
;
2396 ASSERT3P(aclp
, !=, NULL
);
2397 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2399 if (zp
->z_acl_cached
)
2400 zfs_acl_free(zp
->z_acl_cached
);
2401 zp
->z_acl_cached
= aclp
;
2405 if ((mask
& ATTR_ATIME
) || zp
->z_atime_dirty
) {
2406 zp
->z_atime_dirty
= B_FALSE
;
2407 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
2408 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
2409 &atime
, sizeof (atime
));
2412 if (mask
& (ATTR_MTIME
| ATTR_SIZE
)) {
2413 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2414 ZTOI(zp
)->i_mtime
= zpl_inode_timestamp_truncate(
2415 vap
->va_mtime
, ZTOI(zp
));
2417 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
2418 mtime
, sizeof (mtime
));
2421 if (mask
& (ATTR_CTIME
| ATTR_SIZE
)) {
2422 ZFS_TIME_ENCODE(&vap
->va_ctime
, ctime
);
2423 ZTOI(zp
)->i_ctime
= zpl_inode_timestamp_truncate(vap
->va_ctime
,
2425 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2426 ctime
, sizeof (ctime
));
2429 if (projid
!= ZFS_INVALID_PROJID
) {
2430 zp
->z_projid
= projid
;
2431 SA_ADD_BULK_ATTR(bulk
, count
,
2432 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
2433 sizeof (zp
->z_projid
));
2436 if (attrzp
&& mask
) {
2437 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2438 SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
,
2443 * Do this after setting timestamps to prevent timestamp
2444 * update from toggling bit
2447 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2450 * restore trimmed off masks
2451 * so that return masks can be set for caller.
2454 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2455 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2457 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2458 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2460 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2461 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2463 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2464 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2466 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2467 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2469 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
2470 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2472 if (XVA_ISSET_REQ(tmpxvattr
, XAT_PROJINHERIT
)) {
2473 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
2476 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2477 ASSERT(S_ISREG(ip
->i_mode
));
2479 zfs_xvattr_set(zp
, xvap
, tx
);
2483 zfs_fuid_sync(zfsvfs
, tx
);
2486 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2488 mutex_exit(&zp
->z_lock
);
2489 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2490 mutex_exit(&zp
->z_acl_lock
);
2493 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2494 mutex_exit(&attrzp
->z_acl_lock
);
2495 mutex_exit(&attrzp
->z_lock
);
2498 if (err
== 0 && xattr_count
> 0) {
2499 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
2508 zfs_fuid_info_free(fuidp
);
2516 if (err
== ERESTART
)
2520 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2523 if (err2
== 0 && handle_eadir
)
2524 err2
= zfs_setattr_dir(attrzp
);
2527 zfs_znode_update_vfs(zp
);
2531 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
2532 zil_commit(zilog
, 0);
2535 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
2536 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * bulks
);
2537 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
2542 typedef struct zfs_zlock
{
2543 krwlock_t
*zl_rwlock
; /* lock we acquired */
2544 znode_t
*zl_znode
; /* znode we held */
2545 struct zfs_zlock
*zl_next
; /* next in list */
2549 * Drop locks and release vnodes that were held by zfs_rename_lock().
2552 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
2556 while ((zl
= *zlpp
) != NULL
) {
2557 if (zl
->zl_znode
!= NULL
)
2558 zfs_zrele_async(zl
->zl_znode
);
2559 rw_exit(zl
->zl_rwlock
);
2560 *zlpp
= zl
->zl_next
;
2561 kmem_free(zl
, sizeof (*zl
));
2566 * Search back through the directory tree, using the ".." entries.
2567 * Lock each directory in the chain to prevent concurrent renames.
2568 * Fail any attempt to move a directory into one of its own descendants.
2569 * XXX - z_parent_lock can overlap with map or grow locks
2572 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
2576 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
2577 uint64_t oidp
= zp
->z_id
;
2578 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
2579 krw_t rw
= RW_WRITER
;
2582 * First pass write-locks szp and compares to zp->z_id.
2583 * Later passes read-lock zp and compare to zp->z_parent.
2586 if (!rw_tryenter(rwlp
, rw
)) {
2588 * Another thread is renaming in this path.
2589 * Note that if we are a WRITER, we don't have any
2590 * parent_locks held yet.
2592 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
2594 * Drop our locks and restart
2596 zfs_rename_unlock(&zl
);
2600 rwlp
= &szp
->z_parent_lock
;
2605 * Wait for other thread to drop its locks
2611 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
2612 zl
->zl_rwlock
= rwlp
;
2613 zl
->zl_znode
= NULL
;
2614 zl
->zl_next
= *zlpp
;
2617 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
2618 return (SET_ERROR(EINVAL
));
2620 if (oidp
== rootid
) /* We've hit the top */
2623 if (rw
== RW_READER
) { /* i.e. not the first pass */
2624 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
2629 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
2630 &oidp
, sizeof (oidp
));
2631 rwlp
= &zp
->z_parent_lock
;
2634 } while (zp
->z_id
!= sdzp
->z_id
);
2640 * Move an entry from the provided source directory to the target
2641 * directory. Change the entry name as indicated.
2643 * IN: sdzp - Source directory containing the "old entry".
2644 * snm - Old entry name.
2645 * tdzp - Target directory to contain the "new entry".
2646 * tnm - New entry name.
2647 * cr - credentials of caller.
2648 * flags - case flags
2650 * RETURN: 0 on success, error code on failure.
2653 * sdzp,tdzp - ctime|mtime updated
2657 zfs_rename(znode_t
*sdzp
, char *snm
, znode_t
*tdzp
, char *tnm
,
2658 cred_t
*cr
, int flags
)
2661 zfsvfs_t
*zfsvfs
= ZTOZSB(sdzp
);
2663 zfs_dirlock_t
*sdl
, *tdl
;
2666 int cmp
, serr
, terr
;
2669 boolean_t waited
= B_FALSE
;
2671 if (snm
== NULL
|| tnm
== NULL
)
2672 return (SET_ERROR(EINVAL
));
2675 ZFS_VERIFY_ZP(sdzp
);
2676 zilog
= zfsvfs
->z_log
;
2678 ZFS_VERIFY_ZP(tdzp
);
2681 * We check i_sb because snapshots and the ctldir must have different
2684 if (ZTOI(tdzp
)->i_sb
!= ZTOI(sdzp
)->i_sb
||
2685 zfsctl_is_node(ZTOI(tdzp
))) {
2687 return (SET_ERROR(EXDEV
));
2690 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
2691 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
2693 return (SET_ERROR(EILSEQ
));
2696 if (flags
& FIGNORECASE
)
2705 * This is to prevent the creation of links into attribute space
2706 * by renaming a linked file into/outof an attribute directory.
2707 * See the comment in zfs_link() for why this is considered bad.
2709 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
2711 return (SET_ERROR(EINVAL
));
2715 * Lock source and target directory entries. To prevent deadlock,
2716 * a lock ordering must be defined. We lock the directory with
2717 * the smallest object id first, or if it's a tie, the one with
2718 * the lexically first name.
2720 if (sdzp
->z_id
< tdzp
->z_id
) {
2722 } else if (sdzp
->z_id
> tdzp
->z_id
) {
2726 * First compare the two name arguments without
2727 * considering any case folding.
2729 int nofold
= (zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
2731 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
2732 ASSERT(error
== 0 || !zfsvfs
->z_utf8
);
2735 * POSIX: "If the old argument and the new argument
2736 * both refer to links to the same existing file,
2737 * the rename() function shall return successfully
2738 * and perform no other action."
2744 * If the file system is case-folding, then we may
2745 * have some more checking to do. A case-folding file
2746 * system is either supporting mixed case sensitivity
2747 * access or is completely case-insensitive. Note
2748 * that the file system is always case preserving.
2750 * In mixed sensitivity mode case sensitive behavior
2751 * is the default. FIGNORECASE must be used to
2752 * explicitly request case insensitive behavior.
2754 * If the source and target names provided differ only
2755 * by case (e.g., a request to rename 'tim' to 'Tim'),
2756 * we will treat this as a special case in the
2757 * case-insensitive mode: as long as the source name
2758 * is an exact match, we will allow this to proceed as
2759 * a name-change request.
2761 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
2762 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
2763 flags
& FIGNORECASE
)) &&
2764 u8_strcmp(snm
, tnm
, 0, zfsvfs
->z_norm
, U8_UNICODE_LATEST
,
2767 * case preserving rename request, require exact
2776 * If the source and destination directories are the same, we should
2777 * grab the z_name_lock of that directory only once.
2781 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
2785 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
2786 ZEXISTS
| zflg
, NULL
, NULL
);
2787 terr
= zfs_dirent_lock(&tdl
,
2788 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
2790 terr
= zfs_dirent_lock(&tdl
,
2791 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
2792 serr
= zfs_dirent_lock(&sdl
,
2793 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
2799 * Source entry invalid or not there.
2802 zfs_dirent_unlock(tdl
);
2808 rw_exit(&sdzp
->z_name_lock
);
2810 if (strcmp(snm
, "..") == 0)
2816 zfs_dirent_unlock(sdl
);
2820 rw_exit(&sdzp
->z_name_lock
);
2822 if (strcmp(tnm
, "..") == 0)
2829 * If we are using project inheritance, means if the directory has
2830 * ZFS_PROJINHERIT set, then its descendant directories will inherit
2831 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
2832 * such case, we only allow renames into our tree when the project
2835 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
2836 tdzp
->z_projid
!= szp
->z_projid
) {
2837 error
= SET_ERROR(EXDEV
);
2842 * Must have write access at the source to remove the old entry
2843 * and write access at the target to create the new entry.
2844 * Note that if target and source are the same, this can be
2845 * done in a single check.
2848 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
2851 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
2853 * Check to make sure rename is valid.
2854 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2856 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
2861 * Does target exist?
2865 * Source and target must be the same type.
2867 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
2868 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
2869 error
= SET_ERROR(ENOTDIR
);
2873 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
2874 error
= SET_ERROR(EISDIR
);
2879 * POSIX dictates that when the source and target
2880 * entries refer to the same file object, rename
2881 * must do nothing and exit without error.
2883 if (szp
->z_id
== tzp
->z_id
) {
2889 tx
= dmu_tx_create(zfsvfs
->z_os
);
2890 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
2891 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
2892 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
2893 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
2895 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
2896 zfs_sa_upgrade_txholds(tx
, tdzp
);
2899 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
2900 zfs_sa_upgrade_txholds(tx
, tzp
);
2903 zfs_sa_upgrade_txholds(tx
, szp
);
2904 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
2905 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
2908 zfs_rename_unlock(&zl
);
2909 zfs_dirent_unlock(sdl
);
2910 zfs_dirent_unlock(tdl
);
2913 rw_exit(&sdzp
->z_name_lock
);
2915 if (error
== ERESTART
) {
2932 if (tzp
) /* Attempt to remove the existing target */
2933 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
2936 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
2938 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
2939 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
)
2940 szp
->z_pflags
|= ZFS_PROJINHERIT
;
2942 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
2943 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
2946 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
2948 zfs_log_rename(zilog
, tx
, TX_RENAME
|
2949 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
2950 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
2953 * At this point, we have successfully created
2954 * the target name, but have failed to remove
2955 * the source name. Since the create was done
2956 * with the ZRENAMING flag, there are
2957 * complications; for one, the link count is
2958 * wrong. The easiest way to deal with this
2959 * is to remove the newly created target, and
2960 * return the original error. This must
2961 * succeed; fortunately, it is very unlikely to
2962 * fail, since we just created it.
2964 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
2965 ZRENAMING
, NULL
), ==, 0);
2969 * If we had removed the existing target, subsequent
2970 * call to zfs_link_create() to add back the same entry
2971 * but, the new dnode (szp) should not fail.
2973 ASSERT(tzp
== NULL
);
2980 zfs_rename_unlock(&zl
);
2982 zfs_dirent_unlock(sdl
);
2983 zfs_dirent_unlock(tdl
);
2985 zfs_znode_update_vfs(sdzp
);
2987 rw_exit(&sdzp
->z_name_lock
);
2990 zfs_znode_update_vfs(tdzp
);
2992 zfs_znode_update_vfs(szp
);
2995 zfs_znode_update_vfs(tzp
);
2999 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3000 zil_commit(zilog
, 0);
3007 * Insert the indicated symbolic reference entry into the directory.
3009 * IN: dzp - Directory to contain new symbolic link.
3010 * name - Name of directory entry in dip.
3011 * vap - Attributes of new entry.
3012 * link - Name for new symlink entry.
3013 * cr - credentials of caller.
3014 * flags - case flags
3016 * OUT: zpp - Znode for new symbolic link.
3018 * RETURN: 0 on success, error code on failure.
3021 * dip - ctime|mtime updated
3025 zfs_symlink(znode_t
*dzp
, char *name
, vattr_t
*vap
, char *link
,
3026 znode_t
**zpp
, cred_t
*cr
, int flags
)
3031 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
3033 uint64_t len
= strlen(link
);
3036 zfs_acl_ids_t acl_ids
;
3037 boolean_t fuid_dirtied
;
3038 uint64_t txtype
= TX_SYMLINK
;
3039 boolean_t waited
= B_FALSE
;
3041 ASSERT(S_ISLNK(vap
->va_mode
));
3044 return (SET_ERROR(EINVAL
));
3048 zilog
= zfsvfs
->z_log
;
3050 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3051 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3053 return (SET_ERROR(EILSEQ
));
3055 if (flags
& FIGNORECASE
)
3058 if (len
> MAXPATHLEN
) {
3060 return (SET_ERROR(ENAMETOOLONG
));
3063 if ((error
= zfs_acl_ids_create(dzp
, 0,
3064 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3072 * Attempt to lock directory; fail if entry already exists.
3074 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3076 zfs_acl_ids_free(&acl_ids
);
3081 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3082 zfs_acl_ids_free(&acl_ids
);
3083 zfs_dirent_unlock(dl
);
3088 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, ZFS_DEFAULT_PROJID
)) {
3089 zfs_acl_ids_free(&acl_ids
);
3090 zfs_dirent_unlock(dl
);
3092 return (SET_ERROR(EDQUOT
));
3094 tx
= dmu_tx_create(zfsvfs
->z_os
);
3095 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3096 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3097 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3098 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3099 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3100 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3101 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3102 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3103 acl_ids
.z_aclp
->z_acl_bytes
);
3106 zfs_fuid_txhold(zfsvfs
, tx
);
3107 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3109 zfs_dirent_unlock(dl
);
3110 if (error
== ERESTART
) {
3116 zfs_acl_ids_free(&acl_ids
);
3123 * Create a new object for the symlink.
3124 * for version 4 ZPL datasets the symlink will be an SA attribute
3126 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3129 zfs_fuid_sync(zfsvfs
, tx
);
3131 mutex_enter(&zp
->z_lock
);
3133 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
3136 zfs_sa_symlink(zp
, link
, len
, tx
);
3137 mutex_exit(&zp
->z_lock
);
3140 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
3141 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3143 * Insert the new object into the directory.
3145 error
= zfs_link_create(dl
, zp
, tx
, ZNEW
);
3147 zfs_znode_delete(zp
, tx
);
3148 remove_inode_hash(ZTOI(zp
));
3150 if (flags
& FIGNORECASE
)
3152 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3154 zfs_znode_update_vfs(dzp
);
3155 zfs_znode_update_vfs(zp
);
3158 zfs_acl_ids_free(&acl_ids
);
3162 zfs_dirent_unlock(dl
);
3167 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3168 zil_commit(zilog
, 0);
3178 * Return, in the buffer contained in the provided uio structure,
3179 * the symbolic path referred to by ip.
3181 * IN: ip - inode of symbolic link
3182 * uio - structure to contain the link path.
3183 * cr - credentials of caller.
3185 * RETURN: 0 if success
3186 * error code if failure
3189 * ip - atime updated
3193 zfs_readlink(struct inode
*ip
, zfs_uio_t
*uio
, cred_t
*cr
)
3195 znode_t
*zp
= ITOZ(ip
);
3196 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3202 mutex_enter(&zp
->z_lock
);
3204 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3205 SA_ZPL_SYMLINK(zfsvfs
), uio
);
3207 error
= zfs_sa_readlink(zp
, uio
);
3208 mutex_exit(&zp
->z_lock
);
3215 * Insert a new entry into directory tdzp referencing szp.
3217 * IN: tdzp - Directory to contain new entry.
3218 * szp - znode of new entry.
3219 * name - name of new entry.
3220 * cr - credentials of caller.
3221 * flags - case flags.
3223 * RETURN: 0 if success
3224 * error code if failure
3227 * tdzp - ctime|mtime updated
3228 * szp - ctime updated
3232 zfs_link(znode_t
*tdzp
, znode_t
*szp
, char *name
, cred_t
*cr
,
3235 struct inode
*sip
= ZTOI(szp
);
3237 zfsvfs_t
*zfsvfs
= ZTOZSB(tdzp
);
3245 boolean_t waited
= B_FALSE
;
3246 boolean_t is_tmpfile
= 0;
3249 is_tmpfile
= (sip
->i_nlink
== 0 && (sip
->i_state
& I_LINKABLE
));
3251 ASSERT(S_ISDIR(ZTOI(tdzp
)->i_mode
));
3254 return (SET_ERROR(EINVAL
));
3257 ZFS_VERIFY_ZP(tdzp
);
3258 zilog
= zfsvfs
->z_log
;
3261 * POSIX dictates that we return EPERM here.
3262 * Better choices include ENOTSUP or EISDIR.
3264 if (S_ISDIR(sip
->i_mode
)) {
3266 return (SET_ERROR(EPERM
));
3272 * If we are using project inheritance, means if the directory has
3273 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3274 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3275 * such case, we only allow hard link creation in our tree when the
3276 * project IDs are the same.
3278 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3279 tdzp
->z_projid
!= szp
->z_projid
) {
3281 return (SET_ERROR(EXDEV
));
3285 * We check i_sb because snapshots and the ctldir must have different
3288 if (sip
->i_sb
!= ZTOI(tdzp
)->i_sb
|| zfsctl_is_node(sip
)) {
3290 return (SET_ERROR(EXDEV
));
3293 /* Prevent links to .zfs/shares files */
3295 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
3296 &parent
, sizeof (uint64_t))) != 0) {
3300 if (parent
== zfsvfs
->z_shares_dir
) {
3302 return (SET_ERROR(EPERM
));
3305 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3306 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3308 return (SET_ERROR(EILSEQ
));
3310 if (flags
& FIGNORECASE
)
3314 * We do not support links between attributes and non-attributes
3315 * because of the potential security risk of creating links
3316 * into "normal" file space in order to circumvent restrictions
3317 * imposed in attribute space.
3319 if ((szp
->z_pflags
& ZFS_XATTR
) != (tdzp
->z_pflags
& ZFS_XATTR
)) {
3321 return (SET_ERROR(EINVAL
));
3324 owner
= zfs_fuid_map_id(zfsvfs
, KUID_TO_SUID(sip
->i_uid
),
3326 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3328 return (SET_ERROR(EPERM
));
3331 if ((error
= zfs_zaccess(tdzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3338 * Attempt to lock directory; fail if entry already exists.
3340 error
= zfs_dirent_lock(&dl
, tdzp
, name
, &tzp
, zf
, NULL
, NULL
);
3346 tx
= dmu_tx_create(zfsvfs
->z_os
);
3347 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3348 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, name
);
3350 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3352 zfs_sa_upgrade_txholds(tx
, szp
);
3353 zfs_sa_upgrade_txholds(tx
, tdzp
);
3354 error
= dmu_tx_assign(tx
, (waited
? TXG_NOTHROTTLE
: 0) | TXG_NOWAIT
);
3356 zfs_dirent_unlock(dl
);
3357 if (error
== ERESTART
) {
3367 /* unmark z_unlinked so zfs_link_create will not reject */
3369 szp
->z_unlinked
= B_FALSE
;
3370 error
= zfs_link_create(dl
, szp
, tx
, 0);
3373 uint64_t txtype
= TX_LINK
;
3375 * tmpfile is created to be in z_unlinkedobj, so remove it.
3376 * Also, we don't log in ZIL, because all previous file
3377 * operation on the tmpfile are ignored by ZIL. Instead we
3378 * always wait for txg to sync to make sure all previous
3379 * operation are sync safe.
3382 VERIFY(zap_remove_int(zfsvfs
->z_os
,
3383 zfsvfs
->z_unlinkedobj
, szp
->z_id
, tx
) == 0);
3385 if (flags
& FIGNORECASE
)
3387 zfs_log_link(zilog
, tx
, txtype
, tdzp
, szp
, name
);
3389 } else if (is_tmpfile
) {
3390 /* restore z_unlinked since when linking failed */
3391 szp
->z_unlinked
= B_TRUE
;
3393 txg
= dmu_tx_get_txg(tx
);
3396 zfs_dirent_unlock(dl
);
3398 if (!is_tmpfile
&& zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3399 zil_commit(zilog
, 0);
3401 if (is_tmpfile
&& zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
)
3402 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), txg
);
3404 zfs_znode_update_vfs(tdzp
);
3405 zfs_znode_update_vfs(szp
);
3411 zfs_putpage_commit_cb(void *arg
)
3413 struct page
*pp
= arg
;
3416 end_page_writeback(pp
);
3420 * Push a page out to disk, once the page is on stable storage the
3421 * registered commit callback will be run as notification of completion.
3423 * IN: ip - page mapped for inode.
3424 * pp - page to push (page is locked)
3425 * wbc - writeback control data
3427 * RETURN: 0 if success
3428 * error code if failure
3431 * ip - ctime|mtime updated
3435 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3437 znode_t
*zp
= ITOZ(ip
);
3438 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3445 uint64_t mtime
[2], ctime
[2];
3446 sa_bulk_attr_t bulk
[3];
3448 struct address_space
*mapping
;
3453 ASSERT(PageLocked(pp
));
3455 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3456 offset
= i_size_read(ip
); /* File length in bytes */
3457 pglen
= MIN(PAGE_SIZE
, /* Page length in bytes */
3458 P2ROUNDUP(offset
, PAGE_SIZE
)-pgoff
);
3460 /* Page is beyond end of file */
3461 if (pgoff
>= offset
) {
3467 /* Truncate page length to end of file */
3468 if (pgoff
+ pglen
> offset
)
3469 pglen
= offset
- pgoff
;
3473 * FIXME: Allow mmap writes past its quota. The correct fix
3474 * is to register a page_mkwrite() handler to count the page
3475 * against its quota when it is about to be dirtied.
3477 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
,
3478 KUID_TO_SUID(ip
->i_uid
)) ||
3479 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
3480 KGID_TO_SGID(ip
->i_gid
)) ||
3481 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
3482 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
3489 * The ordering here is critical and must adhere to the following
3490 * rules in order to avoid deadlocking in either zfs_read() or
3491 * zfs_free_range() due to a lock inversion.
3493 * 1) The page must be unlocked prior to acquiring the range lock.
3494 * This is critical because zfs_read() calls find_lock_page()
3495 * which may block on the page lock while holding the range lock.
3497 * 2) Before setting or clearing write back on a page the range lock
3498 * must be held in order to prevent a lock inversion with the
3499 * zfs_free_range() function.
3501 * This presents a problem because upon entering this function the
3502 * page lock is already held. To safely acquire the range lock the
3503 * page lock must be dropped. This creates a window where another
3504 * process could truncate, invalidate, dirty, or write out the page.
3506 * Therefore, after successfully reacquiring the range and page locks
3507 * the current page state is checked. In the common case everything
3508 * will be as is expected and it can be written out. However, if
3509 * the page state has changed it must be handled accordingly.
3511 mapping
= pp
->mapping
;
3512 redirty_page_for_writepage(wbc
, pp
);
3515 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
3516 pgoff
, pglen
, RL_WRITER
);
3519 /* Page mapping changed or it was no longer dirty, we're done */
3520 if (unlikely((mapping
!= pp
->mapping
) || !PageDirty(pp
))) {
3522 zfs_rangelock_exit(lr
);
3527 /* Another process started write block if required */
3528 if (PageWriteback(pp
)) {
3530 zfs_rangelock_exit(lr
);
3532 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3533 if (PageWriteback(pp
))
3534 #ifdef HAVE_PAGEMAP_FOLIO_WAIT_BIT
3535 folio_wait_bit(page_folio(pp
), PG_writeback
);
3537 wait_on_page_bit(pp
, PG_writeback
);
3545 /* Clear the dirty flag the required locks are held */
3546 if (!clear_page_dirty_for_io(pp
)) {
3548 zfs_rangelock_exit(lr
);
3554 * Counterpart for redirty_page_for_writepage() above. This page
3555 * was in fact not skipped and should not be counted as if it were.
3557 wbc
->pages_skipped
--;
3558 set_page_writeback(pp
);
3561 tx
= dmu_tx_create(zfsvfs
->z_os
);
3562 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3563 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3564 zfs_sa_upgrade_txholds(tx
, zp
);
3566 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3568 if (err
== ERESTART
)
3572 __set_page_dirty_nobuffers(pp
);
3574 end_page_writeback(pp
);
3575 zfs_rangelock_exit(lr
);
3581 ASSERT3U(pglen
, <=, PAGE_SIZE
);
3582 dmu_write(zfsvfs
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
3585 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
3586 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
3587 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
3590 /* Preserve the mtime and ctime provided by the inode */
3591 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3592 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
3593 zp
->z_atime_dirty
= B_FALSE
;
3596 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3598 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
3599 zfs_putpage_commit_cb
, pp
);
3602 zfs_rangelock_exit(lr
);
3604 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3606 * Note that this is rarely called under writepages(), because
3607 * writepages() normally handles the entire commit for
3608 * performance reasons.
3610 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
3618 * Update the system attributes when the inode has been dirtied. For the
3619 * moment we only update the mode, atime, mtime, and ctime.
3622 zfs_dirty_inode(struct inode
*ip
, int flags
)
3624 znode_t
*zp
= ITOZ(ip
);
3625 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3627 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
3628 sa_bulk_attr_t bulk
[4];
3632 if (zfs_is_readonly(zfsvfs
) || dmu_objset_is_snapshot(zfsvfs
->z_os
))
3640 * This is the lazytime semantic introduced in Linux 4.0
3641 * This flag will only be called from update_time when lazytime is set.
3642 * (Note, I_DIRTY_SYNC will also set if not lazytime)
3643 * Fortunately mtime and ctime are managed within ZFS itself, so we
3644 * only need to dirty atime.
3646 if (flags
== I_DIRTY_TIME
) {
3647 zp
->z_atime_dirty
= B_TRUE
;
3652 tx
= dmu_tx_create(zfsvfs
->z_os
);
3654 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3655 zfs_sa_upgrade_txholds(tx
, zp
);
3657 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3663 mutex_enter(&zp
->z_lock
);
3664 zp
->z_atime_dirty
= B_FALSE
;
3666 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
3667 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
3668 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
3669 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
3671 /* Preserve the mode, mtime and ctime provided by the inode */
3672 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3673 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3674 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
3679 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3680 mutex_exit(&zp
->z_lock
);
3690 zfs_inactive(struct inode
*ip
)
3692 znode_t
*zp
= ITOZ(ip
);
3693 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3696 int need_unlock
= 0;
3698 /* Only read lock if we haven't already write locked, e.g. rollback */
3699 if (!RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
)) {
3701 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_READER
);
3703 if (zp
->z_sa_hdl
== NULL
) {
3705 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3709 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== B_FALSE
) {
3710 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
3712 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3713 zfs_sa_upgrade_txholds(tx
, zp
);
3714 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3718 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
3719 mutex_enter(&zp
->z_lock
);
3720 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
3721 (void *)&atime
, sizeof (atime
), tx
);
3722 zp
->z_atime_dirty
= B_FALSE
;
3723 mutex_exit(&zp
->z_lock
);
3730 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
3734 * Fill pages with data from the disk.
3737 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
3739 znode_t
*zp
= ITOZ(ip
);
3740 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3742 struct page
*cur_pp
;
3743 u_offset_t io_off
, total
;
3750 io_len
= nr_pages
<< PAGE_SHIFT
;
3751 i_size
= i_size_read(ip
);
3752 io_off
= page_offset(pl
[0]);
3754 if (io_off
+ io_len
> i_size
)
3755 io_len
= i_size
- io_off
;
3758 * Iterate over list of pages and read each page individually.
3761 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
3764 cur_pp
= pl
[page_idx
++];
3766 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
3770 /* convert checksum errors into IO errors */
3772 err
= SET_ERROR(EIO
);
3781 * Uses zfs_fillpage to read data from the file and fill the pages.
3783 * IN: ip - inode of file to get data from.
3784 * pl - list of pages to read
3785 * nr_pages - number of pages to read
3787 * RETURN: 0 on success, error code on failure.
3790 * vp - atime updated
3794 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
3796 znode_t
*zp
= ITOZ(ip
);
3797 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3806 err
= zfs_fillpage(ip
, pl
, nr_pages
);
3813 * Check ZFS specific permissions to memory map a section of a file.
3815 * IN: ip - inode of the file to mmap
3817 * addrp - start address in memory region
3818 * len - length of memory region
3819 * vm_flags- address flags
3821 * RETURN: 0 if success
3822 * error code if failure
3826 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
3827 unsigned long vm_flags
)
3829 znode_t
*zp
= ITOZ(ip
);
3830 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3835 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
3836 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
3838 return (SET_ERROR(EPERM
));
3841 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
3842 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
3844 return (SET_ERROR(EACCES
));
3847 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
3849 return (SET_ERROR(ENXIO
));
3857 * Free or allocate space in a file. Currently, this function only
3858 * supports the `F_FREESP' command. However, this command is somewhat
3859 * misnamed, as its functionality includes the ability to allocate as
3860 * well as free space.
3862 * IN: zp - znode of file to free data in.
3863 * cmd - action to take (only F_FREESP supported).
3864 * bfp - section of file to free/alloc.
3865 * flag - current file open mode flags.
3866 * offset - current file offset.
3867 * cr - credentials of caller.
3869 * RETURN: 0 on success, error code on failure.
3872 * zp - ctime|mtime updated
3876 zfs_space(znode_t
*zp
, int cmd
, flock64_t
*bfp
, int flag
,
3877 offset_t offset
, cred_t
*cr
)
3879 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
3886 if (cmd
!= F_FREESP
) {
3888 return (SET_ERROR(EINVAL
));
3892 * Callers might not be able to detect properly that we are read-only,
3893 * so check it explicitly here.
3895 if (zfs_is_readonly(zfsvfs
)) {
3897 return (SET_ERROR(EROFS
));
3900 if (bfp
->l_len
< 0) {
3902 return (SET_ERROR(EINVAL
));
3906 * Permissions aren't checked on Solaris because on this OS
3907 * zfs_space() can only be called with an opened file handle.
3908 * On Linux we can get here through truncate_range() which
3909 * operates directly on inodes, so we need to check access rights.
3911 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
3917 len
= bfp
->l_len
; /* 0 means from off to end of file */
3919 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
3927 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
3929 znode_t
*zp
= ITOZ(ip
);
3930 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
3933 uint64_t object
= zp
->z_id
;
3939 if (fidp
->fid_len
< SHORT_FID_LEN
) {
3940 fidp
->fid_len
= SHORT_FID_LEN
;
3942 return (SET_ERROR(ENOSPC
));
3947 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
3948 &gen64
, sizeof (uint64_t))) != 0) {
3953 gen
= (uint32_t)gen64
;
3955 size
= SHORT_FID_LEN
;
3957 zfid
= (zfid_short_t
*)fidp
;
3959 zfid
->zf_len
= size
;
3961 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
3962 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
3964 /* Must have a non-zero generation number to distinguish from .zfs */
3967 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
3968 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
3974 #if defined(_KERNEL)
3975 EXPORT_SYMBOL(zfs_open
);
3976 EXPORT_SYMBOL(zfs_close
);
3977 EXPORT_SYMBOL(zfs_lookup
);
3978 EXPORT_SYMBOL(zfs_create
);
3979 EXPORT_SYMBOL(zfs_tmpfile
);
3980 EXPORT_SYMBOL(zfs_remove
);
3981 EXPORT_SYMBOL(zfs_mkdir
);
3982 EXPORT_SYMBOL(zfs_rmdir
);
3983 EXPORT_SYMBOL(zfs_readdir
);
3984 EXPORT_SYMBOL(zfs_getattr_fast
);
3985 EXPORT_SYMBOL(zfs_setattr
);
3986 EXPORT_SYMBOL(zfs_rename
);
3987 EXPORT_SYMBOL(zfs_symlink
);
3988 EXPORT_SYMBOL(zfs_readlink
);
3989 EXPORT_SYMBOL(zfs_link
);
3990 EXPORT_SYMBOL(zfs_inactive
);
3991 EXPORT_SYMBOL(zfs_space
);
3992 EXPORT_SYMBOL(zfs_fid
);
3993 EXPORT_SYMBOL(zfs_getpage
);
3994 EXPORT_SYMBOL(zfs_putpage
);
3995 EXPORT_SYMBOL(zfs_dirty_inode
);
3996 EXPORT_SYMBOL(zfs_map
);
3999 module_param(zfs_delete_blocks
, ulong
, 0644);
4000 MODULE_PARM_DESC(zfs_delete_blocks
, "Delete files larger than N blocks async");