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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Integros [integros.com]
27 /* Portions Copyright 2007 Jeremy Teo */
28 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
31 #include <sys/types.h>
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <sys/mntent.h>
38 #include <sys/u8_textprep.h>
39 #include <sys/dsl_dataset.h>
41 #include <sys/vnode.h>
44 #include <sys/errno.h>
45 #include <sys/unistd.h>
46 #include <sys/atomic.h>
47 #include <sys/zfs_dir.h>
48 #include <sys/zfs_acl.h>
49 #include <sys/zfs_ioctl.h>
50 #include <sys/zfs_rlock.h>
51 #include <sys/zfs_fuid.h>
52 #include <sys/dnode.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/kidmap.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/refcount.h>
62 #include <sys/zfs_znode.h>
64 #include <sys/zfs_sa.h>
65 #include <sys/zfs_stat.h>
66 #include <sys/refcount.h>
69 #include "zfs_comutil.h"
71 /* Used by fstat(1). */
72 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD,
73 SYSCTL_NULL_INT_PTR, sizeof(znode_t), "sizeof(znode_t)");
76 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
77 * turned on when DEBUG is also defined.
84 #define ZNODE_STAT_ADD(stat) ((stat)++)
86 #define ZNODE_STAT_ADD(stat) /* nothing */
87 #endif /* ZNODE_STATS */
90 * Functions needed for userland (ie: libzpool) are not put under
91 * #ifdef_KERNEL; the rest of the functions have dependencies
92 * (such as VFS logic) that will not compile easily in userland.
96 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
97 * be freed before it can be safely accessed.
99 krwlock_t zfsvfs_lock;
101 static kmem_cache_t *znode_cache = NULL;
105 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
108 * We should never drop all dbuf refs without first clearing
109 * the eviction callback.
111 panic("evicting znode %p\n", user_ptr);
114 extern struct vop_vector zfs_vnodeops;
115 extern struct vop_vector zfs_fifoops;
116 extern struct vop_vector zfs_shareops;
119 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
123 POINTER_INVALIDATE(&zp->z_zfsvfs);
125 list_link_init(&zp->z_link_node);
127 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
129 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
130 avl_create(&zp->z_range_avl, zfs_range_compare,
131 sizeof (rl_t), offsetof(rl_t, r_node));
133 zp->z_acl_cached = NULL;
141 zfs_znode_cache_destructor(void *buf, void *arg)
145 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
146 ASSERT3P(zp->z_vnode, ==, NULL);
147 ASSERT(!list_link_active(&zp->z_link_node));
148 mutex_destroy(&zp->z_acl_lock);
149 avl_destroy(&zp->z_range_avl);
150 mutex_destroy(&zp->z_range_lock);
152 ASSERT(zp->z_acl_cached == NULL);
157 uint64_t zms_zfsvfs_invalid;
158 uint64_t zms_zfsvfs_recheck1;
159 uint64_t zms_zfsvfs_unmounted;
160 uint64_t zms_zfsvfs_recheck2;
161 uint64_t zms_obj_held;
162 uint64_t zms_vnode_locked;
163 uint64_t zms_not_only_dnlc;
165 #endif /* ZNODE_STATS */
169 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
174 nzp->z_zfsvfs = ozp->z_zfsvfs;
178 nzp->z_vnode = ozp->z_vnode;
179 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
180 ZTOV(ozp)->v_data = ozp;
181 ZTOV(nzp)->v_data = nzp;
183 nzp->z_id = ozp->z_id;
184 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
185 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
186 nzp->z_unlinked = ozp->z_unlinked;
187 nzp->z_atime_dirty = ozp->z_atime_dirty;
188 nzp->z_zn_prefetch = ozp->z_zn_prefetch;
189 nzp->z_blksz = ozp->z_blksz;
190 nzp->z_seq = ozp->z_seq;
191 nzp->z_mapcnt = ozp->z_mapcnt;
192 nzp->z_gen = ozp->z_gen;
193 nzp->z_sync_cnt = ozp->z_sync_cnt;
194 nzp->z_is_sa = ozp->z_is_sa;
195 nzp->z_sa_hdl = ozp->z_sa_hdl;
196 bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
197 nzp->z_links = ozp->z_links;
198 nzp->z_size = ozp->z_size;
199 nzp->z_pflags = ozp->z_pflags;
200 nzp->z_uid = ozp->z_uid;
201 nzp->z_gid = ozp->z_gid;
202 nzp->z_mode = ozp->z_mode;
205 * Since this is just an idle znode and kmem is already dealing with
206 * memory pressure, release any cached ACL.
208 if (ozp->z_acl_cached) {
209 zfs_acl_free(ozp->z_acl_cached);
210 ozp->z_acl_cached = NULL;
213 sa_set_userp(nzp->z_sa_hdl, nzp);
216 * Invalidate the original znode by clearing fields that provide a
217 * pointer back to the znode. Set the low bit of the vfs pointer to
218 * ensure that zfs_znode_move() recognizes the znode as invalid in any
219 * subsequent callback.
221 ozp->z_sa_hdl = NULL;
222 POINTER_INVALIDATE(&ozp->z_zfsvfs);
228 ozp->z_moved = (uint8_t)-1;
233 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
235 znode_t *ozp = buf, *nzp = newbuf;
240 * The znode is on the file system's list of known znodes if the vfs
241 * pointer is valid. We set the low bit of the vfs pointer when freeing
242 * the znode to invalidate it, and the memory patterns written by kmem
243 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
244 * created znode sets the vfs pointer last of all to indicate that the
245 * znode is known and in a valid state to be moved by this function.
247 zfsvfs = ozp->z_zfsvfs;
248 if (!POINTER_IS_VALID(zfsvfs)) {
249 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
250 return (KMEM_CBRC_DONT_KNOW);
254 * Close a small window in which it's possible that the filesystem could
255 * be unmounted and freed, and zfsvfs, though valid in the previous
256 * statement, could point to unrelated memory by the time we try to
257 * prevent the filesystem from being unmounted.
259 rw_enter(&zfsvfs_lock, RW_WRITER);
260 if (zfsvfs != ozp->z_zfsvfs) {
261 rw_exit(&zfsvfs_lock);
262 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
263 return (KMEM_CBRC_DONT_KNOW);
267 * If the znode is still valid, then so is the file system. We know that
268 * no valid file system can be freed while we hold zfsvfs_lock, so we
269 * can safely ensure that the filesystem is not and will not be
270 * unmounted. The next statement is equivalent to ZFS_ENTER().
272 rrm_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
273 if (zfsvfs->z_unmounted) {
275 rw_exit(&zfsvfs_lock);
276 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
277 return (KMEM_CBRC_DONT_KNOW);
279 rw_exit(&zfsvfs_lock);
281 mutex_enter(&zfsvfs->z_znodes_lock);
283 * Recheck the vfs pointer in case the znode was removed just before
284 * acquiring the lock.
286 if (zfsvfs != ozp->z_zfsvfs) {
287 mutex_exit(&zfsvfs->z_znodes_lock);
289 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
290 return (KMEM_CBRC_DONT_KNOW);
294 * At this point we know that as long as we hold z_znodes_lock, the
295 * znode cannot be freed and fields within the znode can be safely
296 * accessed. Now, prevent a race with zfs_zget().
298 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
299 mutex_exit(&zfsvfs->z_znodes_lock);
301 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
302 return (KMEM_CBRC_LATER);
306 if (mutex_tryenter(&vp->v_lock) == 0) {
307 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
308 mutex_exit(&zfsvfs->z_znodes_lock);
310 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
311 return (KMEM_CBRC_LATER);
314 /* Only move znodes that are referenced _only_ by the DNLC. */
315 if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
316 mutex_exit(&vp->v_lock);
317 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
318 mutex_exit(&zfsvfs->z_znodes_lock);
320 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
321 return (KMEM_CBRC_LATER);
325 * The znode is known and in a valid state to move. We're holding the
326 * locks needed to execute the critical section.
328 zfs_znode_move_impl(ozp, nzp);
329 mutex_exit(&vp->v_lock);
330 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
332 list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
333 mutex_exit(&zfsvfs->z_znodes_lock);
336 return (KMEM_CBRC_YES);
346 rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
347 ASSERT(znode_cache == NULL);
348 znode_cache = kmem_cache_create("zfs_znode_cache",
349 sizeof (znode_t), 0, zfs_znode_cache_constructor,
350 zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
351 kmem_cache_set_move(znode_cache, zfs_znode_move);
359 * Cleanup vfs & vnode ops
361 zfs_remove_op_tables();
368 kmem_cache_destroy(znode_cache);
370 rw_destroy(&zfsvfs_lock);
374 struct vnodeops *zfs_dvnodeops;
375 struct vnodeops *zfs_fvnodeops;
376 struct vnodeops *zfs_symvnodeops;
377 struct vnodeops *zfs_xdvnodeops;
378 struct vnodeops *zfs_evnodeops;
379 struct vnodeops *zfs_sharevnodeops;
382 zfs_remove_op_tables()
388 (void) vfs_freevfsops_by_type(zfsfstype);
395 vn_freevnodeops(zfs_dvnodeops);
397 vn_freevnodeops(zfs_fvnodeops);
399 vn_freevnodeops(zfs_symvnodeops);
401 vn_freevnodeops(zfs_xdvnodeops);
403 vn_freevnodeops(zfs_evnodeops);
404 if (zfs_sharevnodeops)
405 vn_freevnodeops(zfs_sharevnodeops);
407 zfs_dvnodeops = NULL;
408 zfs_fvnodeops = NULL;
409 zfs_symvnodeops = NULL;
410 zfs_xdvnodeops = NULL;
411 zfs_evnodeops = NULL;
412 zfs_sharevnodeops = NULL;
415 extern const fs_operation_def_t zfs_dvnodeops_template[];
416 extern const fs_operation_def_t zfs_fvnodeops_template[];
417 extern const fs_operation_def_t zfs_xdvnodeops_template[];
418 extern const fs_operation_def_t zfs_symvnodeops_template[];
419 extern const fs_operation_def_t zfs_evnodeops_template[];
420 extern const fs_operation_def_t zfs_sharevnodeops_template[];
423 zfs_create_op_tables()
428 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
429 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
430 * In this case we just return as the ops vectors are already set up.
435 error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
440 error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
445 error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
450 error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
455 error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
460 error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
468 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
470 zfs_acl_ids_t acl_ids;
476 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
477 vattr.va_type = VDIR;
478 vattr.va_mode = S_IFDIR|0555;
479 vattr.va_uid = crgetuid(kcred);
480 vattr.va_gid = crgetgid(kcred);
482 sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
483 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
484 sharezp->z_moved = 0;
485 sharezp->z_unlinked = 0;
486 sharezp->z_atime_dirty = 0;
487 sharezp->z_zfsvfs = zfsvfs;
488 sharezp->z_is_sa = zfsvfs->z_use_sa;
490 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
491 kcred, NULL, &acl_ids));
492 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
493 ASSERT3P(zp, ==, sharezp);
494 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
495 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
496 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
497 zfsvfs->z_shares_dir = sharezp->z_id;
499 zfs_acl_ids_free(&acl_ids);
500 sa_handle_destroy(sharezp->z_sa_hdl);
501 kmem_cache_free(znode_cache, sharezp);
507 * define a couple of values we need available
508 * for both 64 and 32 bit environments.
511 #define NBITSMINOR64 32
514 #define MAXMAJ64 0xffffffffUL
517 #define MAXMIN64 0xffffffffUL
521 * Create special expldev for ZFS private use.
522 * Can't use standard expldev since it doesn't do
523 * what we want. The standard expldev() takes a
524 * dev32_t in LP64 and expands it to a long dev_t.
525 * We need an interface that takes a dev32_t in ILP32
526 * and expands it to a long dev_t.
529 zfs_expldev(dev_t dev)
531 return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
534 * Special cmpldev for ZFS private use.
535 * Can't use standard cmpldev since it takes
536 * a long dev_t and compresses it to dev32_t in
537 * LP64. We need to do a compaction of a long dev_t
538 * to a dev32_t in ILP32.
541 zfs_cmpldev(uint64_t dev)
543 return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
547 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
548 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
550 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
551 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
553 ASSERT(zp->z_sa_hdl == NULL);
554 ASSERT(zp->z_acl_cached == NULL);
555 if (sa_hdl == NULL) {
556 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
557 SA_HDL_SHARED, &zp->z_sa_hdl));
559 zp->z_sa_hdl = sa_hdl;
560 sa_set_userp(sa_hdl, zp);
563 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
566 * Slap on VROOT if we are the root znode unless we are the root
567 * node of a snapshot mounted under .zfs.
569 if (zp->z_id == zfsvfs->z_root && zfsvfs->z_parent == zfsvfs)
570 ZTOV(zp)->v_flag |= VROOT;
576 zfs_znode_dmu_fini(znode_t *zp)
578 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
580 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
582 sa_handle_destroy(zp->z_sa_hdl);
587 zfs_vnode_forget(vnode_t *vp)
590 /* copied from insmntque_stddtr */
592 vp->v_op = &dead_vnodeops;
598 * Construct a new znode/vnode and intialize.
600 * This does not do a call to dmu_set_user() that is
601 * up to the caller to do, in case you don't want to
605 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
606 dmu_object_type_t obj_type, sa_handle_t *hdl)
612 sa_bulk_attr_t bulk[9];
616 zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
618 KASSERT(curthread->td_vp_reserv > 0,
619 ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
620 error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp);
622 kmem_cache_free(znode_cache, zp);
628 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
632 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
633 * the zfs_znode_move() callback.
637 zp->z_atime_dirty = 0;
639 zp->z_id = db->db_object;
641 zp->z_seq = 0x7A4653;
646 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
648 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
649 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
650 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
652 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
654 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
656 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
657 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
659 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
661 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
664 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
666 sa_handle_destroy(zp->z_sa_hdl);
667 zfs_vnode_forget(vp);
669 kmem_cache_free(znode_cache, zp);
675 vp->v_type = IFTOVT((mode_t)mode);
677 switch (vp->v_type) {
679 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
686 VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
687 &rdev, sizeof (rdev)) == 0);
689 vp->v_rdev = zfs_cmpldev(rdev);
698 vp->v_op = &zfs_fifoops;
701 if (parent == zfsvfs->z_shares_dir) {
702 ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
703 vp->v_op = &zfs_shareops;
708 vn_setops(vp, zfs_symvnodeops);
711 vn_setops(vp, zfs_evnodeops);
716 mutex_enter(&zfsvfs->z_znodes_lock);
717 list_insert_tail(&zfsvfs->z_all_znodes, zp);
720 * Everything else must be valid before assigning z_zfsvfs makes the
721 * znode eligible for zfs_znode_move().
723 zp->z_zfsvfs = zfsvfs;
724 mutex_exit(&zfsvfs->z_znodes_lock);
727 * Acquire vnode lock before making it available to the world.
729 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
731 if (vp->v_type != VFIFO)
735 VFS_HOLD(zfsvfs->z_vfs);
740 static uint64_t empty_xattr;
741 static uint64_t pad[4];
742 static zfs_acl_phys_t acl_phys;
744 * Create a new DMU object to hold a zfs znode.
746 * IN: dzp - parent directory for new znode
747 * vap - file attributes for new znode
748 * tx - dmu transaction id for zap operations
749 * cr - credentials of caller
751 * IS_ROOT_NODE - new object will be root
752 * IS_XATTR - new object is an attribute
753 * bonuslen - length of bonus buffer
754 * setaclp - File/Dir initial ACL
755 * fuidp - Tracks fuid allocation.
757 * OUT: zpp - allocated znode
761 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
762 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
764 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
765 uint64_t mode, size, links, parent, pflags;
766 uint64_t dzp_pflags = 0;
768 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
775 dmu_object_type_t obj_type;
776 sa_bulk_attr_t sa_attrs[ZPL_END];
778 zfs_acl_locator_cb_t locate = { 0 };
780 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
782 if (zfsvfs->z_replay) {
783 obj = vap->va_nodeid;
784 now = vap->va_ctime; /* see zfs_replay_create() */
785 gen = vap->va_nblocks; /* ditto */
789 gen = dmu_tx_get_txg(tx);
792 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
793 bonuslen = (obj_type == DMU_OT_SA) ?
794 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
797 * Create a new DMU object.
800 * There's currently no mechanism for pre-reading the blocks that will
801 * be needed to allocate a new object, so we accept the small chance
802 * that there will be an i/o error and we will fail one of the
805 if (vap->va_type == VDIR) {
806 if (zfsvfs->z_replay) {
807 VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj,
808 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
809 obj_type, bonuslen, tx));
811 obj = zap_create_norm(zfsvfs->z_os,
812 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
813 obj_type, bonuslen, tx);
816 if (zfsvfs->z_replay) {
817 VERIFY0(dmu_object_claim(zfsvfs->z_os, obj,
818 DMU_OT_PLAIN_FILE_CONTENTS, 0,
819 obj_type, bonuslen, tx));
821 obj = dmu_object_alloc(zfsvfs->z_os,
822 DMU_OT_PLAIN_FILE_CONTENTS, 0,
823 obj_type, bonuslen, tx);
827 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
828 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
831 * If this is the root, fix up the half-initialized parent pointer
832 * to reference the just-allocated physical data area.
834 if (flag & IS_ROOT_NODE) {
837 dzp_pflags = dzp->z_pflags;
841 * If parent is an xattr, so am I.
843 if (dzp_pflags & ZFS_XATTR) {
847 if (zfsvfs->z_use_fuids)
848 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
852 if (vap->va_type == VDIR) {
853 size = 2; /* contents ("." and "..") */
854 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
859 if (vap->va_type == VBLK || vap->va_type == VCHR) {
860 rdev = zfs_expldev(vap->va_rdev);
864 mode = acl_ids->z_mode;
869 * No execs denied will be deterimed when zfs_mode_compute() is called.
871 pflags |= acl_ids->z_aclp->z_hints &
872 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
873 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
875 ZFS_TIME_ENCODE(&now, crtime);
876 ZFS_TIME_ENCODE(&now, ctime);
878 if (vap->va_mask & AT_ATIME) {
879 ZFS_TIME_ENCODE(&vap->va_atime, atime);
881 ZFS_TIME_ENCODE(&now, atime);
884 if (vap->va_mask & AT_MTIME) {
885 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
887 ZFS_TIME_ENCODE(&now, mtime);
890 /* Now add in all of the "SA" attributes */
891 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
895 * Setup the array of attributes to be replaced/set on the new file
897 * order for DMU_OT_ZNODE is critical since it needs to be constructed
898 * in the old znode_phys_t format. Don't change this ordering
901 if (obj_type == DMU_OT_ZNODE) {
902 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
904 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
906 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
908 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
910 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
912 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
914 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
916 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
919 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
921 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
923 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
925 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
926 &acl_ids->z_fuid, 8);
927 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
928 &acl_ids->z_fgid, 8);
929 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
931 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
933 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
935 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
937 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
939 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
943 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
945 if (obj_type == DMU_OT_ZNODE) {
946 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
949 if (obj_type == DMU_OT_ZNODE ||
950 (vap->va_type == VBLK || vap->va_type == VCHR)) {
951 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
955 if (obj_type == DMU_OT_ZNODE) {
956 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
958 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
959 &acl_ids->z_fuid, 8);
960 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
961 &acl_ids->z_fgid, 8);
962 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
963 sizeof (uint64_t) * 4);
964 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
965 &acl_phys, sizeof (zfs_acl_phys_t));
966 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
967 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
968 &acl_ids->z_aclp->z_acl_count, 8);
969 locate.cb_aclp = acl_ids->z_aclp;
970 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
971 zfs_acl_data_locator, &locate,
972 acl_ids->z_aclp->z_acl_bytes);
973 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
974 acl_ids->z_fuid, acl_ids->z_fgid);
977 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
979 if (!(flag & IS_ROOT_NODE)) {
980 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
981 ASSERT(*zpp != NULL);
984 * If we are creating the root node, the "parent" we
985 * passed in is the znode for the root.
989 (*zpp)->z_sa_hdl = sa_hdl;
992 (*zpp)->z_pflags = pflags;
993 (*zpp)->z_mode = mode;
995 if (vap->va_mask & AT_XVATTR)
996 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
998 if (obj_type == DMU_OT_ZNODE ||
999 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1000 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
1002 if (!(flag & IS_ROOT_NODE)) {
1006 vp->v_vflag |= VV_FORCEINSMQ;
1007 err = insmntque(vp, zfsvfs->z_vfs);
1008 vp->v_vflag &= ~VV_FORCEINSMQ;
1009 KASSERT(err == 0, ("insmntque() failed: error %d", err));
1011 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1015 * Update in-core attributes. It is assumed the caller will be doing an
1016 * sa_bulk_update to push the changes out.
1019 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1023 xoap = xva_getxoptattr(xvap);
1026 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1028 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1029 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1030 ×, sizeof (times), tx);
1031 XVA_SET_RTN(xvap, XAT_CREATETIME);
1033 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1034 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1036 XVA_SET_RTN(xvap, XAT_READONLY);
1038 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1039 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1041 XVA_SET_RTN(xvap, XAT_HIDDEN);
1043 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1044 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1046 XVA_SET_RTN(xvap, XAT_SYSTEM);
1048 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1049 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1051 XVA_SET_RTN(xvap, XAT_ARCHIVE);
1053 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1054 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1056 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1058 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1059 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1061 XVA_SET_RTN(xvap, XAT_NOUNLINK);
1063 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1064 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1066 XVA_SET_RTN(xvap, XAT_APPENDONLY);
1068 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1069 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1071 XVA_SET_RTN(xvap, XAT_NODUMP);
1073 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1074 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1076 XVA_SET_RTN(xvap, XAT_OPAQUE);
1078 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1079 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1080 xoap->xoa_av_quarantined, zp->z_pflags, tx);
1081 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1083 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1084 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1086 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1088 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1089 zfs_sa_set_scanstamp(zp, xvap, tx);
1090 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1092 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1093 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1095 XVA_SET_RTN(xvap, XAT_REPARSE);
1097 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1098 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1100 XVA_SET_RTN(xvap, XAT_OFFLINE);
1102 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1103 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1105 XVA_SET_RTN(xvap, XAT_SPARSE);
1110 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1112 dmu_object_info_t doi;
1122 getnewvnode_reserve(1);
1125 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1127 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1129 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1130 getnewvnode_drop_reserve();
1134 dmu_object_info_from_db(db, &doi);
1135 if (doi.doi_bonus_type != DMU_OT_SA &&
1136 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1137 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1138 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1139 sa_buf_rele(db, NULL);
1140 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1142 getnewvnode_drop_reserve();
1144 return (SET_ERROR(EINVAL));
1147 hdl = dmu_buf_get_user(db);
1149 zp = sa_get_userdata(hdl);
1152 * Since "SA" does immediate eviction we
1153 * should never find a sa handle that doesn't
1154 * know about the znode.
1156 ASSERT3P(zp, !=, NULL);
1157 ASSERT3U(zp->z_id, ==, obj_num);
1158 if (zp->z_unlinked) {
1159 err = SET_ERROR(ENOENT);
1163 * Don't let the vnode disappear after
1164 * ZFS_OBJ_HOLD_EXIT.
1171 sa_buf_rele(db, NULL);
1172 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1175 getnewvnode_drop_reserve();
1179 locked = VOP_ISLOCKED(vp);
1181 if ((vp->v_iflag & VI_DOOMED) != 0 &&
1182 locked != LK_EXCLUSIVE) {
1184 * The vnode is doomed and this thread doesn't
1185 * hold the exclusive lock on it, so the vnode
1186 * must be being reclaimed by another thread.
1187 * Otherwise the doomed vnode is being reclaimed
1188 * by this thread and zfs_zget is called from
1194 * XXX vrele() locks the vnode when the last reference
1195 * is dropped. Although in this case the vnode is
1196 * doomed / dead and so no inactivation is required,
1197 * the vnode lock is still acquired. That could result
1198 * in a LOR with z_teardown_lock if another thread holds
1199 * the vnode's lock and tries to take z_teardown_lock.
1200 * But that is only possible if the other thread peforms
1201 * a ZFS vnode operation on the vnode. That either
1202 * should not happen if the vnode is dead or the thread
1203 * should also have a refrence to the vnode and thus
1204 * our reference is not last.
1210 getnewvnode_drop_reserve();
1215 * Not found create new znode/vnode
1216 * but only if file exists.
1218 * There is a small window where zfs_vget() could
1219 * find this object while a file create is still in
1220 * progress. This is checked for in zfs_znode_alloc()
1222 * if zfs_znode_alloc() fails it will drop the hold on the
1225 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1226 doi.doi_bonus_type, NULL);
1228 err = SET_ERROR(ENOENT);
1233 vnode_t *vp = ZTOV(zp);
1235 err = insmntque(vp, zfsvfs->z_vfs);
1237 vp->v_hash = obj_num;
1241 zfs_znode_dmu_fini(zp);
1246 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1247 getnewvnode_drop_reserve();
1252 zfs_rezget(znode_t *zp)
1254 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1255 dmu_object_info_t doi;
1258 uint64_t obj_num = zp->z_id;
1259 uint64_t mode, size;
1260 sa_bulk_attr_t bulk[8];
1266 * Remove cached pages before reloading the znode, so that they are not
1267 * lingering after we run into any error. Ideally, we should vgone()
1268 * the vnode in case of error, but currently we cannot do that
1269 * because of the LOR between the vnode lock and z_teardown_lock.
1270 * So, instead, we have to "doom" the znode in the illumos style.
1273 vn_pages_remove(vp, 0, 0);
1275 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1277 mutex_enter(&zp->z_acl_lock);
1278 if (zp->z_acl_cached) {
1279 zfs_acl_free(zp->z_acl_cached);
1280 zp->z_acl_cached = NULL;
1283 mutex_exit(&zp->z_acl_lock);
1284 ASSERT(zp->z_sa_hdl == NULL);
1285 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1287 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1291 dmu_object_info_from_db(db, &doi);
1292 if (doi.doi_bonus_type != DMU_OT_SA &&
1293 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1294 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1295 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1296 sa_buf_rele(db, NULL);
1297 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1298 return (SET_ERROR(EINVAL));
1301 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1304 /* reload cached values */
1305 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1306 &gen, sizeof (gen));
1307 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1308 &zp->z_size, sizeof (zp->z_size));
1309 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1310 &zp->z_links, sizeof (zp->z_links));
1311 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1312 &zp->z_pflags, sizeof (zp->z_pflags));
1313 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1314 &zp->z_atime, sizeof (zp->z_atime));
1315 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1316 &zp->z_uid, sizeof (zp->z_uid));
1317 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1318 &zp->z_gid, sizeof (zp->z_gid));
1319 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1320 &mode, sizeof (mode));
1322 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1323 zfs_znode_dmu_fini(zp);
1324 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1325 return (SET_ERROR(EIO));
1330 if (gen != zp->z_gen) {
1331 zfs_znode_dmu_fini(zp);
1332 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1333 return (SET_ERROR(EIO));
1337 * It is highly improbable but still quite possible that two
1338 * objects in different datasets are created with the same
1339 * object numbers and in transaction groups with the same
1340 * numbers. znodes corresponding to those objects would
1341 * have the same z_id and z_gen, but their other attributes
1343 * zfs recv -F may replace one of such objects with the other.
1344 * As a result file properties recorded in the replaced
1345 * object's vnode may no longer match the received object's
1346 * properties. At present the only cached property is the
1347 * files type recorded in v_type.
1348 * So, handle this case by leaving the old vnode and znode
1349 * disassociated from the actual object. A new vnode and a
1350 * znode will be created if the object is accessed
1351 * (e.g. via a look-up). The old vnode and znode will be
1352 * recycled when the last vnode reference is dropped.
1354 if (vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1355 zfs_znode_dmu_fini(zp);
1356 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1357 return (SET_ERROR(EIO));
1361 * If the file has zero links, then it has been unlinked on the send
1362 * side and it must be in the received unlinked set.
1363 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1364 * stale data and to prevent automatical removal of the file in
1365 * zfs_zinactive(). The file will be removed either when it is removed
1366 * on the send side and the next incremental stream is received or
1367 * when the unlinked set gets processed.
1369 zp->z_unlinked = (zp->z_links == 0);
1370 if (zp->z_unlinked) {
1371 zfs_znode_dmu_fini(zp);
1372 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1376 zp->z_blksz = doi.doi_data_block_size;
1377 if (zp->z_size != size)
1378 vnode_pager_setsize(vp, zp->z_size);
1380 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1386 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1388 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1389 objset_t *os = zfsvfs->z_os;
1390 uint64_t obj = zp->z_id;
1391 uint64_t acl_obj = zfs_external_acl(zp);
1393 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1395 VERIFY(!zp->z_is_sa);
1396 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1398 VERIFY(0 == dmu_object_free(os, obj, tx));
1399 zfs_znode_dmu_fini(zp);
1400 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1405 zfs_zinactive(znode_t *zp)
1407 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1408 uint64_t z_id = zp->z_id;
1410 ASSERT(zp->z_sa_hdl);
1413 * Don't allow a zfs_zget() while were trying to release this znode
1415 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1418 * If this was the last reference to a file with no links, remove
1419 * the file from the file system unless the file system is mounted
1420 * read-only. That can happen, for example, if the file system was
1421 * originally read-write, the file was opened, then unlinked and
1422 * the file system was made read-only before the file was finally
1423 * closed. The file will remain in the unlinked set.
1425 if (zp->z_unlinked) {
1426 ASSERT(!zfsvfs->z_issnap);
1427 if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) {
1428 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1434 zfs_znode_dmu_fini(zp);
1435 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1440 zfs_znode_free(znode_t *zp)
1442 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1444 ASSERT(zp->z_sa_hdl == NULL);
1446 mutex_enter(&zfsvfs->z_znodes_lock);
1447 POINTER_INVALIDATE(&zp->z_zfsvfs);
1448 list_remove(&zfsvfs->z_all_znodes, zp);
1449 mutex_exit(&zfsvfs->z_znodes_lock);
1451 if (zp->z_acl_cached) {
1452 zfs_acl_free(zp->z_acl_cached);
1453 zp->z_acl_cached = NULL;
1456 kmem_cache_free(znode_cache, zp);
1459 VFS_RELE(zfsvfs->z_vfs);
1464 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1465 uint64_t ctime[2], boolean_t have_tx)
1469 vfs_timestamp(&now);
1471 if (have_tx) { /* will sa_bulk_update happen really soon? */
1472 zp->z_atime_dirty = 0;
1475 zp->z_atime_dirty = 1;
1478 if (flag & AT_ATIME) {
1479 ZFS_TIME_ENCODE(&now, zp->z_atime);
1482 if (flag & AT_MTIME) {
1483 ZFS_TIME_ENCODE(&now, mtime);
1484 if (zp->z_zfsvfs->z_use_fuids) {
1485 zp->z_pflags |= (ZFS_ARCHIVE |
1490 if (flag & AT_CTIME) {
1491 ZFS_TIME_ENCODE(&now, ctime);
1492 if (zp->z_zfsvfs->z_use_fuids)
1493 zp->z_pflags |= ZFS_ARCHIVE;
1498 * Grow the block size for a file.
1500 * IN: zp - znode of file to free data in.
1501 * size - requested block size
1502 * tx - open transaction.
1504 * NOTE: this function assumes that the znode is write locked.
1507 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1512 if (size <= zp->z_blksz)
1515 * If the file size is already greater than the current blocksize,
1516 * we will not grow. If there is more than one block in a file,
1517 * the blocksize cannot change.
1519 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1522 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1525 if (error == ENOTSUP)
1529 /* What blocksize did we actually get? */
1530 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1535 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1536 * be calling back into the fs for a putpage(). E.g.: when truncating
1537 * a file, the pages being "thrown away* don't need to be written out.
1541 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1542 int flags, cred_t *cr)
1550 * Increase the file length
1552 * IN: zp - znode of file to free data in.
1553 * end - new end-of-file
1555 * RETURN: 0 on success, error code on failure
1558 zfs_extend(znode_t *zp, uint64_t end)
1560 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1567 * We will change zp_size, lock the whole file.
1569 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1572 * Nothing to do if file already at desired length.
1574 if (end <= zp->z_size) {
1575 zfs_range_unlock(rl);
1578 tx = dmu_tx_create(zfsvfs->z_os);
1579 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1580 zfs_sa_upgrade_txholds(tx, zp);
1581 if (end > zp->z_blksz &&
1582 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1584 * We are growing the file past the current block size.
1586 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1588 * File's blocksize is already larger than the
1589 * "recordsize" property. Only let it grow to
1590 * the next power of 2.
1592 ASSERT(!ISP2(zp->z_blksz));
1593 newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1595 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1597 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1602 error = dmu_tx_assign(tx, TXG_WAIT);
1605 zfs_range_unlock(rl);
1610 zfs_grow_blocksize(zp, newblksz, tx);
1614 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1615 &zp->z_size, sizeof (zp->z_size), tx));
1617 vnode_pager_setsize(ZTOV(zp), end);
1619 zfs_range_unlock(rl);
1627 * Free space in a file.
1629 * IN: zp - znode of file to free data in.
1630 * off - start of section to free.
1631 * len - length of section to free.
1633 * RETURN: 0 on success, error code on failure
1636 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1638 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1643 * Lock the range being freed.
1645 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1648 * Nothing to do if file already at desired length.
1650 if (off >= zp->z_size) {
1651 zfs_range_unlock(rl);
1655 if (off + len > zp->z_size)
1656 len = zp->z_size - off;
1658 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1662 * In FreeBSD we cannot free block in the middle of a file,
1663 * but only at the end of a file, so this code path should
1666 vnode_pager_setsize(ZTOV(zp), off);
1669 zfs_range_unlock(rl);
1677 * IN: zp - znode of file to free data in.
1678 * end - new end-of-file.
1680 * RETURN: 0 on success, error code on failure
1683 zfs_trunc(znode_t *zp, uint64_t end)
1685 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1686 vnode_t *vp = ZTOV(zp);
1690 sa_bulk_attr_t bulk[2];
1694 * We will change zp_size, lock the whole file.
1696 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1699 * Nothing to do if file already at desired length.
1701 if (end >= zp->z_size) {
1702 zfs_range_unlock(rl);
1706 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1);
1708 zfs_range_unlock(rl);
1711 tx = dmu_tx_create(zfsvfs->z_os);
1712 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1713 zfs_sa_upgrade_txholds(tx, zp);
1714 dmu_tx_mark_netfree(tx);
1715 error = dmu_tx_assign(tx, TXG_WAIT);
1718 zfs_range_unlock(rl);
1723 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1724 NULL, &zp->z_size, sizeof (zp->z_size));
1727 zp->z_pflags &= ~ZFS_SPARSE;
1728 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1729 NULL, &zp->z_pflags, 8);
1731 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1736 * Clear any mapped pages in the truncated region. This has to
1737 * happen outside of the transaction to avoid the possibility of
1738 * a deadlock with someone trying to push a page that we are
1739 * about to invalidate.
1741 vnode_pager_setsize(vp, end);
1743 zfs_range_unlock(rl);
1749 * Free space in a file
1751 * IN: zp - znode of file to free data in.
1752 * off - start of range
1753 * len - end of range (0 => EOF)
1754 * flag - current file open mode flags.
1755 * log - TRUE if this action should be logged
1757 * RETURN: 0 on success, error code on failure
1760 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1762 vnode_t *vp = ZTOV(zp);
1764 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1765 zilog_t *zilog = zfsvfs->z_log;
1767 uint64_t mtime[2], ctime[2];
1768 sa_bulk_attr_t bulk[3];
1772 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1773 sizeof (mode))) != 0)
1776 if (off > zp->z_size) {
1777 error = zfs_extend(zp, off+len);
1778 if (error == 0 && log)
1785 * Check for any locks in the region to be freed.
1788 if (MANDLOCK(vp, (mode_t)mode)) {
1789 uint64_t length = (len ? len : zp->z_size - off);
1790 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1795 error = zfs_trunc(zp, off);
1797 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1798 off + len > zp->z_size)
1799 error = zfs_extend(zp, off+len);
1804 tx = dmu_tx_create(zfsvfs->z_os);
1805 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1806 zfs_sa_upgrade_txholds(tx, zp);
1807 error = dmu_tx_assign(tx, TXG_WAIT);
1813 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1814 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1815 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1816 NULL, &zp->z_pflags, 8);
1817 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1818 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1821 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1828 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1830 uint64_t moid, obj, sa_obj, version;
1831 uint64_t sense = ZFS_CASE_SENSITIVE;
1836 znode_t *rootzp = NULL;
1840 zfs_acl_ids_t acl_ids;
1843 * First attempt to create master node.
1846 * In an empty objset, there are no blocks to read and thus
1847 * there can be no i/o errors (which we assert below).
1849 moid = MASTER_NODE_OBJ;
1850 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1851 DMU_OT_NONE, 0, tx);
1855 * Set starting attributes.
1857 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1859 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1860 /* For the moment we expect all zpl props to be uint64_ts */
1864 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1865 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1866 name = nvpair_name(elem);
1867 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1871 error = zap_update(os, moid, name, 8, 1, &val, tx);
1874 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1876 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1879 ASSERT(version != 0);
1880 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1883 * Create zap object used for SA attribute registration
1886 if (version >= ZPL_VERSION_SA) {
1887 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1888 DMU_OT_NONE, 0, tx);
1889 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1895 * Create a delete queue.
1897 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1899 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1903 * Create root znode. Create minimal znode/vnode/zfsvfs
1904 * to allow zfs_mknode to work.
1907 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1908 vattr.va_type = VDIR;
1909 vattr.va_mode = S_IFDIR|0755;
1910 vattr.va_uid = crgetuid(cr);
1911 vattr.va_gid = crgetgid(cr);
1913 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1915 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1916 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1917 rootzp->z_moved = 0;
1918 rootzp->z_unlinked = 0;
1919 rootzp->z_atime_dirty = 0;
1920 rootzp->z_is_sa = USE_SA(version, os);
1923 zfsvfs->z_parent = zfsvfs;
1924 zfsvfs->z_version = version;
1925 zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1926 zfsvfs->z_use_sa = USE_SA(version, os);
1927 zfsvfs->z_norm = norm;
1929 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1930 &zfsvfs->z_attr_table);
1935 * Fold case on file systems that are always or sometimes case
1938 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1939 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1941 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1942 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1943 offsetof(znode_t, z_link_node));
1945 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1946 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1948 rootzp->z_zfsvfs = zfsvfs;
1949 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1950 cr, NULL, &acl_ids));
1951 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1952 ASSERT3P(zp, ==, rootzp);
1953 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1955 zfs_acl_ids_free(&acl_ids);
1956 POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1958 sa_handle_destroy(rootzp->z_sa_hdl);
1959 kmem_cache_free(znode_cache, rootzp);
1962 * Create shares directory
1965 error = zfs_create_share_dir(zfsvfs, tx);
1969 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1970 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1971 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1973 #endif /* _KERNEL */
1976 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1978 uint64_t sa_obj = 0;
1981 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1982 if (error != 0 && error != ENOENT)
1985 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1990 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1991 dmu_buf_t **db, void *tag)
1993 dmu_object_info_t doi;
1996 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1999 dmu_object_info_from_db(*db, &doi);
2000 if ((doi.doi_bonus_type != DMU_OT_SA &&
2001 doi.doi_bonus_type != DMU_OT_ZNODE) ||
2002 doi.doi_bonus_type == DMU_OT_ZNODE &&
2003 doi.doi_bonus_size < sizeof (znode_phys_t)) {
2004 sa_buf_rele(*db, tag);
2005 return (SET_ERROR(ENOTSUP));
2008 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
2010 sa_buf_rele(*db, tag);
2018 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
2020 sa_handle_destroy(hdl);
2021 sa_buf_rele(db, tag);
2025 * Given an object number, return its parent object number and whether
2026 * or not the object is an extended attribute directory.
2029 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2030 uint64_t *pobjp, int *is_xattrdir)
2035 uint64_t parent_mode;
2036 sa_bulk_attr_t bulk[3];
2037 sa_handle_t *sa_hdl;
2042 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2043 &parent, sizeof (parent));
2044 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2045 &pflags, sizeof (pflags));
2046 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2047 &mode, sizeof (mode));
2049 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2053 * When a link is removed its parent pointer is not changed and will
2054 * be invalid. There are two cases where a link is removed but the
2055 * file stays around, when it goes to the delete queue and when there
2056 * are additional links.
2058 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2062 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2063 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2067 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2070 * Extended attributes can be applied to files, directories, etc.
2071 * Otherwise the parent must be a directory.
2073 if (!*is_xattrdir && !S_ISDIR(parent_mode))
2074 return (SET_ERROR(EINVAL));
2082 * Given an object number, return some zpl level statistics
2085 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2088 sa_bulk_attr_t bulk[4];
2091 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2092 &sb->zs_mode, sizeof (sb->zs_mode));
2093 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2094 &sb->zs_gen, sizeof (sb->zs_gen));
2095 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2096 &sb->zs_links, sizeof (sb->zs_links));
2097 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2098 &sb->zs_ctime, sizeof (sb->zs_ctime));
2100 return (sa_bulk_lookup(hdl, bulk, count));
2104 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2105 sa_attr_type_t *sa_table, char *buf, int len)
2107 sa_handle_t *sa_hdl;
2108 sa_handle_t *prevhdl = NULL;
2109 dmu_buf_t *prevdb = NULL;
2110 dmu_buf_t *sa_db = NULL;
2111 char *path = buf + len - 1;
2119 char component[MAXNAMELEN + 2];
2124 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2126 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2127 &is_xattrdir)) != 0)
2138 (void) sprintf(component + 1, "<xattrdir>");
2140 error = zap_value_search(osp, pobj, obj,
2141 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2146 complen = strlen(component);
2148 ASSERT(path >= buf);
2149 bcopy(component, path, complen);
2152 if (sa_hdl != hdl) {
2156 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2164 if (sa_hdl != NULL && sa_hdl != hdl) {
2165 ASSERT(sa_db != NULL);
2166 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2170 (void) memmove(buf, path, buf + len - path);
2176 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2178 sa_attr_type_t *sa_table;
2183 error = zfs_sa_setup(osp, &sa_table);
2187 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2191 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2193 zfs_release_sa_handle(hdl, db, FTAG);
2198 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2201 char *path = buf + len - 1;
2202 sa_attr_type_t *sa_table;
2209 error = zfs_sa_setup(osp, &sa_table);
2213 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2217 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2219 zfs_release_sa_handle(hdl, db, FTAG);
2223 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2225 zfs_release_sa_handle(hdl, db, FTAG);
2231 zfs_znode_parent_and_name(znode_t *zp, znode_t **dzpp, char *buf)
2233 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2238 /* Extended attributes should not be visible as regular files. */
2239 if ((zp->z_pflags & ZFS_XATTR) != 0)
2240 return (SET_ERROR(EINVAL));
2242 err = zfs_obj_to_pobj(zfsvfs->z_os, zp->z_sa_hdl, zfsvfs->z_attr_table,
2243 &parent, &is_xattrdir);
2246 ASSERT0(is_xattrdir);
2248 /* No name as this is a root object. */
2249 if (parent == zp->z_id)
2250 return (SET_ERROR(EINVAL));
2252 err = zap_value_search(zfsvfs->z_os, parent, zp->z_id,
2253 ZFS_DIRENT_OBJ(-1ULL), buf);
2256 err = zfs_zget(zfsvfs, parent, dzpp);
2259 #endif /* _KERNEL */