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>
57 #include <sys/dmu_objset.h>
58 #include <sys/dmu_tx.h>
59 #include <sys/zfs_refcount.h>
62 #include <sys/zfs_znode.h>
64 #include <sys/zfs_sa.h>
65 #include <sys/zfs_stat.h>
68 #include "zfs_comutil.h"
70 /* Used by fstat(1). */
71 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD,
72 SYSCTL_NULL_INT_PTR, sizeof (znode_t), "sizeof(znode_t)");
75 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
76 * turned on when DEBUG is also defined.
83 #define ZNODE_STAT_ADD(stat) ((stat)++)
85 #define ZNODE_STAT_ADD(stat) /* nothing */
86 #endif /* ZNODE_STATS */
89 * Functions needed for userland (ie: libzpool) are not put under
90 * #ifdef_KERNEL; the rest of the functions have dependencies
91 * (such as VFS logic) that will not compile easily in userland.
94 #if !defined(KMEM_DEBUG) && __FreeBSD_version >= 1300102
96 static uma_zone_t znode_uma_zone;
98 static kmem_cache_t *znode_cache = NULL;
101 extern struct vop_vector zfs_vnodeops;
102 extern struct vop_vector zfs_fifoops;
103 extern struct vop_vector zfs_shareops;
107 * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on
108 * z_rangelock. It will modify the offset and length of the lock to reflect
109 * znode-specific information, and convert RL_APPEND to RL_WRITER. This is
110 * called with the rangelock_t's rl_lock held, which avoids races.
113 zfs_rangelock_cb(zfs_locked_range_t *new, void *arg)
118 * If in append mode, convert to writer and lock starting at the
119 * current end of file.
121 if (new->lr_type == RL_APPEND) {
122 new->lr_offset = zp->z_size;
123 new->lr_type = RL_WRITER;
127 * If we need to grow the block size then lock the whole file range.
129 uint64_t end_size = MAX(zp->z_size, new->lr_offset + new->lr_length);
130 if (end_size > zp->z_blksz && (!ISP2(zp->z_blksz) ||
131 zp->z_blksz < ZTOZSB(zp)->z_max_blksz)) {
133 new->lr_length = UINT64_MAX;
138 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
142 POINTER_INVALIDATE(&zp->z_zfsvfs);
144 list_link_init(&zp->z_link_node);
146 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
148 zfs_rangelock_init(&zp->z_rangelock, zfs_rangelock_cb, zp);
150 zp->z_acl_cached = NULL;
157 zfs_znode_cache_destructor(void *buf, void *arg)
161 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
162 ASSERT3P(zp->z_vnode, ==, NULL);
163 ASSERT(!list_link_active(&zp->z_link_node));
164 mutex_destroy(&zp->z_acl_lock);
165 zfs_rangelock_fini(&zp->z_rangelock);
167 ASSERT(zp->z_acl_cached == NULL);
175 zfs_znode_cache_constructor_smr(void *mem, int size __unused, void *private,
179 return (zfs_znode_cache_constructor(mem, private, flags));
183 zfs_znode_cache_destructor_smr(void *mem, int size __unused, void *private)
186 zfs_znode_cache_destructor(mem, private);
195 ASSERT(znode_uma_zone == NULL);
196 znode_uma_zone = uma_zcreate("zfs_znode_cache",
197 sizeof (znode_t), zfs_znode_cache_constructor_smr,
198 zfs_znode_cache_destructor_smr, NULL, NULL, 0, 0);
199 VFS_SMR_ZONE_SET(znode_uma_zone);
203 zfs_znode_alloc_kmem(int flags)
206 return (uma_zalloc_smr(znode_uma_zone, flags));
210 zfs_znode_free_kmem(znode_t *zp)
213 uma_zfree_smr(znode_uma_zone, zp);
222 ASSERT(znode_cache == NULL);
223 znode_cache = kmem_cache_create("zfs_znode_cache",
224 sizeof (znode_t), 0, zfs_znode_cache_constructor,
225 zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
229 zfs_znode_alloc_kmem(int flags)
232 return (kmem_cache_alloc(znode_cache, flags));
236 zfs_znode_free_kmem(znode_t *zp)
239 kmem_cache_free(znode_cache, zp);
250 if (znode_uma_zone) {
251 uma_zdestroy(znode_uma_zone);
252 znode_uma_zone = NULL;
256 kmem_cache_destroy(znode_cache);
264 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
266 zfs_acl_ids_t acl_ids;
272 vattr.va_mask = AT_MODE|AT_UID|AT_GID;
273 vattr.va_type = VDIR;
274 vattr.va_mode = S_IFDIR|0555;
275 vattr.va_uid = crgetuid(kcred);
276 vattr.va_gid = crgetgid(kcred);
278 sharezp = zfs_znode_alloc_kmem(KM_SLEEP);
279 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
280 sharezp->z_unlinked = 0;
281 sharezp->z_atime_dirty = 0;
282 sharezp->z_zfsvfs = zfsvfs;
283 sharezp->z_is_sa = zfsvfs->z_use_sa;
285 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
286 kcred, NULL, &acl_ids));
287 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
288 ASSERT3P(zp, ==, sharezp);
289 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
290 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
291 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
292 zfsvfs->z_shares_dir = sharezp->z_id;
294 zfs_acl_ids_free(&acl_ids);
295 sa_handle_destroy(sharezp->z_sa_hdl);
296 zfs_znode_free_kmem(sharezp);
302 * define a couple of values we need available
303 * for both 64 and 32 bit environments.
306 #define NBITSMINOR64 32
309 #define MAXMAJ64 0xffffffffUL
312 #define MAXMIN64 0xffffffffUL
316 * Create special expldev for ZFS private use.
317 * Can't use standard expldev since it doesn't do
318 * what we want. The standard expldev() takes a
319 * dev32_t in LP64 and expands it to a long dev_t.
320 * We need an interface that takes a dev32_t in ILP32
321 * and expands it to a long dev_t.
324 zfs_expldev(dev_t dev)
326 return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
329 * Special cmpldev for ZFS private use.
330 * Can't use standard cmpldev since it takes
331 * a long dev_t and compresses it to dev32_t in
332 * LP64. We need to do a compaction of a long dev_t
333 * to a dev32_t in ILP32.
336 zfs_cmpldev(uint64_t dev)
338 return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
342 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
343 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
345 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
346 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
348 ASSERT(zp->z_sa_hdl == NULL);
349 ASSERT(zp->z_acl_cached == NULL);
350 if (sa_hdl == NULL) {
351 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
352 SA_HDL_SHARED, &zp->z_sa_hdl));
354 zp->z_sa_hdl = sa_hdl;
355 sa_set_userp(sa_hdl, zp);
358 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
361 * Slap on VROOT if we are the root znode unless we are the root
362 * node of a snapshot mounted under .zfs.
364 if (zp->z_id == zfsvfs->z_root && zfsvfs->z_parent == zfsvfs)
365 ZTOV(zp)->v_flag |= VROOT;
371 zfs_znode_dmu_fini(znode_t *zp)
373 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
375 ZFS_TEARDOWN_INACTIVE_WRITE_HELD(zp->z_zfsvfs));
377 sa_handle_destroy(zp->z_sa_hdl);
382 zfs_vnode_forget(vnode_t *vp)
385 /* copied from insmntque_stddtr */
387 vp->v_op = &dead_vnodeops;
393 * Construct a new znode/vnode and initialize.
395 * This does not do a call to dmu_set_user() that is
396 * up to the caller to do, in case you don't want to
400 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
401 dmu_object_type_t obj_type, sa_handle_t *hdl)
408 uint64_t mtime[2], ctime[2];
410 uint64_t projid = ZFS_DEFAULT_PROJID;
411 sa_bulk_attr_t bulk[9];
415 zp = zfs_znode_alloc_kmem(KM_SLEEP);
418 KASSERT((zfsvfs->z_parent->z_vfs->mnt_kern_flag & MNTK_FPLOOKUP) == 0,
419 ("%s: fast path lookup enabled without smr", __func__));
422 #if __FreeBSD_version >= 1300076
423 KASSERT(curthread->td_vp_reserved != NULL,
424 ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
426 KASSERT(curthread->td_vp_reserv > 0,
427 ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
429 error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp);
431 zfs_znode_free_kmem(zp);
437 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
441 zp->z_atime_dirty = 0;
443 zp->z_id = db->db_object;
445 zp->z_seq = 0x7A4653;
447 atomic_store_ptr((uintptr_t *)&zp->z_cached_symlink, (uintptr_t)NULL);
451 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
453 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
454 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
455 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
457 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
459 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
461 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
462 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
465 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
467 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
470 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
472 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
475 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0 ||
476 (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
477 (zp->z_pflags & ZFS_PROJID) &&
478 sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs), &projid, 8) != 0)) {
480 sa_handle_destroy(zp->z_sa_hdl);
481 zfs_vnode_forget(vp);
483 zfs_znode_free_kmem(zp);
487 zp->z_projid = projid;
490 /* Cache the xattr parent id */
491 if (zp->z_pflags & ZFS_XATTR)
492 zp->z_xattr_parent = parent;
494 vp->v_type = IFTOVT((mode_t)mode);
496 switch (vp->v_type) {
498 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
501 vp->v_op = &zfs_fifoops;
504 if (parent == zfsvfs->z_shares_dir) {
505 ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
506 vp->v_op = &zfs_shareops;
513 mutex_enter(&zfsvfs->z_znodes_lock);
514 list_insert_tail(&zfsvfs->z_all_znodes, zp);
515 zfsvfs->z_nr_znodes++;
516 zp->z_zfsvfs = zfsvfs;
517 mutex_exit(&zfsvfs->z_znodes_lock);
520 * Acquire vnode lock before making it available to the world.
522 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
524 if (vp->v_type != VFIFO)
530 static uint64_t empty_xattr;
531 static uint64_t pad[4];
532 static zfs_acl_phys_t acl_phys;
534 * Create a new DMU object to hold a zfs znode.
536 * IN: dzp - parent directory for new znode
537 * vap - file attributes for new znode
538 * tx - dmu transaction id for zap operations
539 * cr - credentials of caller
541 * IS_ROOT_NODE - new object will be root
542 * IS_XATTR - new object is an attribute
543 * bonuslen - length of bonus buffer
544 * setaclp - File/Dir initial ACL
545 * fuidp - Tracks fuid allocation.
547 * OUT: zpp - allocated znode
551 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
552 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
554 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
555 uint64_t mode, size, links, parent, pflags;
556 uint64_t dzp_pflags = 0;
558 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
566 dmu_object_type_t obj_type;
567 sa_bulk_attr_t *sa_attrs;
569 zfs_acl_locator_cb_t locate = { 0 };
571 ASSERT(vap && ((vap->va_mask & AT_MODE) == AT_MODE));
573 if (zfsvfs->z_replay) {
574 obj = vap->va_nodeid;
575 now = vap->va_ctime; /* see zfs_replay_create() */
576 gen = vap->va_nblocks; /* ditto */
577 dnodesize = vap->va_fsid; /* ditto */
581 gen = dmu_tx_get_txg(tx);
582 dnodesize = dmu_objset_dnodesize(zfsvfs->z_os);
586 dnodesize = DNODE_MIN_SIZE;
588 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
589 bonuslen = (obj_type == DMU_OT_SA) ?
590 DN_BONUS_SIZE(dnodesize) : ZFS_OLD_ZNODE_PHYS_SIZE;
593 * Create a new DMU object.
596 * There's currently no mechanism for pre-reading the blocks that will
597 * be needed to allocate a new object, so we accept the small chance
598 * that there will be an i/o error and we will fail one of the
601 if (vap->va_type == VDIR) {
602 if (zfsvfs->z_replay) {
603 VERIFY0(zap_create_claim_norm_dnsize(zfsvfs->z_os, obj,
604 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
605 obj_type, bonuslen, dnodesize, tx));
607 obj = zap_create_norm_dnsize(zfsvfs->z_os,
608 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
609 obj_type, bonuslen, dnodesize, tx);
612 if (zfsvfs->z_replay) {
613 VERIFY0(dmu_object_claim_dnsize(zfsvfs->z_os, obj,
614 DMU_OT_PLAIN_FILE_CONTENTS, 0,
615 obj_type, bonuslen, dnodesize, tx));
617 obj = dmu_object_alloc_dnsize(zfsvfs->z_os,
618 DMU_OT_PLAIN_FILE_CONTENTS, 0,
619 obj_type, bonuslen, dnodesize, tx);
623 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
624 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
627 * If this is the root, fix up the half-initialized parent pointer
628 * to reference the just-allocated physical data area.
630 if (flag & IS_ROOT_NODE) {
633 dzp_pflags = dzp->z_pflags;
637 * If parent is an xattr, so am I.
639 if (dzp_pflags & ZFS_XATTR) {
643 if (zfsvfs->z_use_fuids)
644 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
648 if (vap->va_type == VDIR) {
649 size = 2; /* contents ("." and "..") */
650 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
655 if (vap->va_type == VBLK || vap->va_type == VCHR) {
656 rdev = zfs_expldev(vap->va_rdev);
660 mode = acl_ids->z_mode;
665 * No execs denied will be determined when zfs_mode_compute() is called.
667 pflags |= acl_ids->z_aclp->z_hints &
668 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
669 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
671 ZFS_TIME_ENCODE(&now, crtime);
672 ZFS_TIME_ENCODE(&now, ctime);
674 if (vap->va_mask & AT_ATIME) {
675 ZFS_TIME_ENCODE(&vap->va_atime, atime);
677 ZFS_TIME_ENCODE(&now, atime);
680 if (vap->va_mask & AT_MTIME) {
681 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
683 ZFS_TIME_ENCODE(&now, mtime);
686 /* Now add in all of the "SA" attributes */
687 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
691 * Setup the array of attributes to be replaced/set on the new file
693 * order for DMU_OT_ZNODE is critical since it needs to be constructed
694 * in the old znode_phys_t format. Don't change this ordering
696 sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
698 if (obj_type == DMU_OT_ZNODE) {
699 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
701 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
703 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
705 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
707 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
709 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
711 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
713 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
716 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
718 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
720 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
722 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs),
723 NULL, &acl_ids->z_fuid, 8);
724 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs),
725 NULL, &acl_ids->z_fgid, 8);
726 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
728 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
730 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
732 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
734 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
736 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
740 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
742 if (obj_type == DMU_OT_ZNODE) {
743 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
746 if (obj_type == DMU_OT_ZNODE ||
747 (vap->va_type == VBLK || vap->va_type == VCHR)) {
748 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
752 if (obj_type == DMU_OT_ZNODE) {
753 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
755 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
756 &acl_ids->z_fuid, 8);
757 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
758 &acl_ids->z_fgid, 8);
759 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
760 sizeof (uint64_t) * 4);
761 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
762 &acl_phys, sizeof (zfs_acl_phys_t));
763 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
764 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
765 &acl_ids->z_aclp->z_acl_count, 8);
766 locate.cb_aclp = acl_ids->z_aclp;
767 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
768 zfs_acl_data_locator, &locate,
769 acl_ids->z_aclp->z_acl_bytes);
770 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
771 acl_ids->z_fuid, acl_ids->z_fgid);
774 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
776 if (!(flag & IS_ROOT_NODE)) {
777 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
778 ASSERT(*zpp != NULL);
781 * If we are creating the root node, the "parent" we
782 * passed in is the znode for the root.
786 (*zpp)->z_sa_hdl = sa_hdl;
789 (*zpp)->z_pflags = pflags;
790 (*zpp)->z_mode = mode;
791 (*zpp)->z_dnodesize = dnodesize;
793 if (vap->va_mask & AT_XVATTR)
794 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
796 if (obj_type == DMU_OT_ZNODE ||
797 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
798 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
800 if (!(flag & IS_ROOT_NODE)) {
804 vp->v_vflag |= VV_FORCEINSMQ;
805 err = insmntque(vp, zfsvfs->z_vfs);
806 vp->v_vflag &= ~VV_FORCEINSMQ;
807 KASSERT(err == 0, ("insmntque() failed: error %d", err));
809 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
810 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
814 * Update in-core attributes. It is assumed the caller will be doing an
815 * sa_bulk_update to push the changes out.
818 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
822 xoap = xva_getxoptattr(xvap);
825 ASSERT_VOP_IN_SEQC(ZTOV(zp));
827 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
829 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
830 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
831 ×, sizeof (times), tx);
832 XVA_SET_RTN(xvap, XAT_CREATETIME);
834 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
835 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
837 XVA_SET_RTN(xvap, XAT_READONLY);
839 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
840 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
842 XVA_SET_RTN(xvap, XAT_HIDDEN);
844 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
845 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
847 XVA_SET_RTN(xvap, XAT_SYSTEM);
849 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
850 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
852 XVA_SET_RTN(xvap, XAT_ARCHIVE);
854 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
855 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
857 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
859 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
860 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
862 XVA_SET_RTN(xvap, XAT_NOUNLINK);
864 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
865 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
867 XVA_SET_RTN(xvap, XAT_APPENDONLY);
869 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
870 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
872 XVA_SET_RTN(xvap, XAT_NODUMP);
874 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
875 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
877 XVA_SET_RTN(xvap, XAT_OPAQUE);
879 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
880 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
881 xoap->xoa_av_quarantined, zp->z_pflags, tx);
882 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
884 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
885 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
887 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
889 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
890 zfs_sa_set_scanstamp(zp, xvap, tx);
891 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
893 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
894 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
896 XVA_SET_RTN(xvap, XAT_REPARSE);
898 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
899 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
901 XVA_SET_RTN(xvap, XAT_OFFLINE);
903 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
904 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
906 XVA_SET_RTN(xvap, XAT_SPARSE);
911 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
913 dmu_object_info_t doi;
923 getnewvnode_reserve_();
926 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
928 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
930 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
931 getnewvnode_drop_reserve();
935 dmu_object_info_from_db(db, &doi);
936 if (doi.doi_bonus_type != DMU_OT_SA &&
937 (doi.doi_bonus_type != DMU_OT_ZNODE ||
938 (doi.doi_bonus_type == DMU_OT_ZNODE &&
939 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
940 sa_buf_rele(db, NULL);
941 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
942 getnewvnode_drop_reserve();
943 return (SET_ERROR(EINVAL));
946 hdl = dmu_buf_get_user(db);
948 zp = sa_get_userdata(hdl);
951 * Since "SA" does immediate eviction we
952 * should never find a sa handle that doesn't
953 * know about the znode.
955 ASSERT3P(zp, !=, NULL);
956 ASSERT3U(zp->z_id, ==, obj_num);
957 if (zp->z_unlinked) {
958 err = SET_ERROR(ENOENT);
962 * Don't let the vnode disappear after
970 sa_buf_rele(db, NULL);
971 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
974 getnewvnode_drop_reserve();
978 locked = VOP_ISLOCKED(vp);
980 if (VN_IS_DOOMED(vp) && locked != LK_EXCLUSIVE) {
982 * The vnode is doomed and this thread doesn't
983 * hold the exclusive lock on it, so the vnode
984 * must be being reclaimed by another thread.
985 * Otherwise the doomed vnode is being reclaimed
986 * by this thread and zfs_zget is called from
992 * XXX vrele() locks the vnode when the last reference
993 * is dropped. Although in this case the vnode is
994 * doomed / dead and so no inactivation is required,
995 * the vnode lock is still acquired. That could result
996 * in a LOR with z_teardown_lock if another thread holds
997 * the vnode's lock and tries to take z_teardown_lock.
998 * But that is only possible if the other thread peforms
999 * a ZFS vnode operation on the vnode. That either
1000 * should not happen if the vnode is dead or the thread
1001 * should also have a reference to the vnode and thus
1002 * our reference is not last.
1008 getnewvnode_drop_reserve();
1013 * Not found create new znode/vnode
1014 * but only if file exists.
1016 * There is a small window where zfs_vget() could
1017 * find this object while a file create is still in
1018 * progress. This is checked for in zfs_znode_alloc()
1020 * if zfs_znode_alloc() fails it will drop the hold on the
1023 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1024 doi.doi_bonus_type, NULL);
1026 err = SET_ERROR(ENOENT);
1031 vnode_t *vp = ZTOV(zp);
1033 err = insmntque(vp, zfsvfs->z_vfs);
1035 vp->v_hash = obj_num;
1039 zfs_znode_dmu_fini(zp);
1044 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1045 getnewvnode_drop_reserve();
1050 zfs_rezget(znode_t *zp)
1052 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1053 dmu_object_info_t doi;
1056 uint64_t obj_num = zp->z_id;
1057 uint64_t mode, size;
1058 sa_bulk_attr_t bulk[8];
1064 * Remove cached pages before reloading the znode, so that they are not
1065 * lingering after we run into any error. Ideally, we should vgone()
1066 * the vnode in case of error, but currently we cannot do that
1067 * because of the LOR between the vnode lock and z_teardown_lock.
1068 * So, instead, we have to "doom" the znode in the illumos style.
1071 vn_pages_remove(vp, 0, 0);
1073 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1075 mutex_enter(&zp->z_acl_lock);
1076 if (zp->z_acl_cached) {
1077 zfs_acl_free(zp->z_acl_cached);
1078 zp->z_acl_cached = NULL;
1081 mutex_exit(&zp->z_acl_lock);
1082 ASSERT(zp->z_sa_hdl == NULL);
1083 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1085 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1089 dmu_object_info_from_db(db, &doi);
1090 if (doi.doi_bonus_type != DMU_OT_SA &&
1091 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1092 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1093 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1094 sa_buf_rele(db, NULL);
1095 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1096 return (SET_ERROR(EINVAL));
1099 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1102 /* reload cached values */
1103 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1104 &gen, sizeof (gen));
1105 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1106 &zp->z_size, sizeof (zp->z_size));
1107 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1108 &zp->z_links, sizeof (zp->z_links));
1109 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1110 &zp->z_pflags, sizeof (zp->z_pflags));
1111 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1112 &zp->z_atime, sizeof (zp->z_atime));
1113 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1114 &zp->z_uid, sizeof (zp->z_uid));
1115 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1116 &zp->z_gid, sizeof (zp->z_gid));
1117 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1118 &mode, sizeof (mode));
1120 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1121 zfs_znode_dmu_fini(zp);
1122 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1123 return (SET_ERROR(EIO));
1128 if (gen != zp->z_gen) {
1129 zfs_znode_dmu_fini(zp);
1130 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1131 return (SET_ERROR(EIO));
1135 * It is highly improbable but still quite possible that two
1136 * objects in different datasets are created with the same
1137 * object numbers and in transaction groups with the same
1138 * numbers. znodes corresponding to those objects would
1139 * have the same z_id and z_gen, but their other attributes
1141 * zfs recv -F may replace one of such objects with the other.
1142 * As a result file properties recorded in the replaced
1143 * object's vnode may no longer match the received object's
1144 * properties. At present the only cached property is the
1145 * files type recorded in v_type.
1146 * So, handle this case by leaving the old vnode and znode
1147 * disassociated from the actual object. A new vnode and a
1148 * znode will be created if the object is accessed
1149 * (e.g. via a look-up). The old vnode and znode will be
1150 * recycled when the last vnode reference is dropped.
1152 if (vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1153 zfs_znode_dmu_fini(zp);
1154 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1155 return (SET_ERROR(EIO));
1159 * If the file has zero links, then it has been unlinked on the send
1160 * side and it must be in the received unlinked set.
1161 * We call zfs_znode_dmu_fini() now to prevent any accesses to the
1162 * stale data and to prevent automatically removal of the file in
1163 * zfs_zinactive(). The file will be removed either when it is removed
1164 * on the send side and the next incremental stream is received or
1165 * when the unlinked set gets processed.
1167 zp->z_unlinked = (zp->z_links == 0);
1168 if (zp->z_unlinked) {
1169 zfs_znode_dmu_fini(zp);
1170 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1174 zp->z_blksz = doi.doi_data_block_size;
1175 if (zp->z_size != size)
1176 vnode_pager_setsize(vp, zp->z_size);
1178 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1184 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1186 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1187 objset_t *os = zfsvfs->z_os;
1188 uint64_t obj = zp->z_id;
1189 uint64_t acl_obj = zfs_external_acl(zp);
1191 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1193 VERIFY(!zp->z_is_sa);
1194 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1196 VERIFY(0 == dmu_object_free(os, obj, tx));
1197 zfs_znode_dmu_fini(zp);
1198 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1203 zfs_zinactive(znode_t *zp)
1205 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1206 uint64_t z_id = zp->z_id;
1208 ASSERT(zp->z_sa_hdl);
1211 * Don't allow a zfs_zget() while were trying to release this znode
1213 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1216 * If this was the last reference to a file with no links, remove
1217 * the file from the file system unless the file system is mounted
1218 * read-only. That can happen, for example, if the file system was
1219 * originally read-write, the file was opened, then unlinked and
1220 * the file system was made read-only before the file was finally
1221 * closed. The file will remain in the unlinked set.
1223 if (zp->z_unlinked) {
1224 ASSERT(!zfsvfs->z_issnap);
1225 if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) {
1226 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1232 zfs_znode_dmu_fini(zp);
1233 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1238 zfs_znode_free(znode_t *zp)
1240 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1243 ASSERT(zp->z_sa_hdl == NULL);
1245 mutex_enter(&zfsvfs->z_znodes_lock);
1246 POINTER_INVALIDATE(&zp->z_zfsvfs);
1247 list_remove(&zfsvfs->z_all_znodes, zp);
1248 zfsvfs->z_nr_znodes--;
1249 mutex_exit(&zfsvfs->z_znodes_lock);
1250 symlink = atomic_load_ptr(&zp->z_cached_symlink);
1251 if (symlink != NULL) {
1252 atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink, (uintptr_t)NULL);
1253 cache_symlink_free(symlink, strlen(symlink) + 1);
1256 if (zp->z_acl_cached) {
1257 zfs_acl_free(zp->z_acl_cached);
1258 zp->z_acl_cached = NULL;
1261 zfs_znode_free_kmem(zp);
1265 zfs_tstamp_update_setup_ext(znode_t *zp, uint_t flag, uint64_t mtime[2],
1266 uint64_t ctime[2], boolean_t have_tx)
1270 vfs_timestamp(&now);
1272 if (have_tx) { /* will sa_bulk_update happen really soon? */
1273 zp->z_atime_dirty = 0;
1276 zp->z_atime_dirty = 1;
1279 if (flag & AT_ATIME) {
1280 ZFS_TIME_ENCODE(&now, zp->z_atime);
1283 if (flag & AT_MTIME) {
1284 ZFS_TIME_ENCODE(&now, mtime);
1285 if (zp->z_zfsvfs->z_use_fuids) {
1286 zp->z_pflags |= (ZFS_ARCHIVE |
1291 if (flag & AT_CTIME) {
1292 ZFS_TIME_ENCODE(&now, ctime);
1293 if (zp->z_zfsvfs->z_use_fuids)
1294 zp->z_pflags |= ZFS_ARCHIVE;
1300 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1303 zfs_tstamp_update_setup_ext(zp, flag, mtime, ctime, B_TRUE);
1306 * Grow the block size for a file.
1308 * IN: zp - znode of file to free data in.
1309 * size - requested block size
1310 * tx - open transaction.
1312 * NOTE: this function assumes that the znode is write locked.
1315 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1320 if (size <= zp->z_blksz)
1323 * If the file size is already greater than the current blocksize,
1324 * we will not grow. If there is more than one block in a file,
1325 * the blocksize cannot change.
1327 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1330 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1333 if (error == ENOTSUP)
1337 /* What blocksize did we actually get? */
1338 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1342 * Increase the file length
1344 * IN: zp - znode of file to free data in.
1345 * end - new end-of-file
1347 * RETURN: 0 on success, error code on failure
1350 zfs_extend(znode_t *zp, uint64_t end)
1352 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1354 zfs_locked_range_t *lr;
1359 * We will change zp_size, lock the whole file.
1361 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1364 * Nothing to do if file already at desired length.
1366 if (end <= zp->z_size) {
1367 zfs_rangelock_exit(lr);
1370 tx = dmu_tx_create(zfsvfs->z_os);
1371 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1372 zfs_sa_upgrade_txholds(tx, zp);
1373 if (end > zp->z_blksz &&
1374 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1376 * We are growing the file past the current block size.
1378 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1380 * File's blocksize is already larger than the
1381 * "recordsize" property. Only let it grow to
1382 * the next power of 2.
1384 ASSERT(!ISP2(zp->z_blksz));
1385 newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1387 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1389 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1394 error = dmu_tx_assign(tx, TXG_WAIT);
1397 zfs_rangelock_exit(lr);
1402 zfs_grow_blocksize(zp, newblksz, tx);
1406 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1407 &zp->z_size, sizeof (zp->z_size), tx));
1409 vnode_pager_setsize(ZTOV(zp), end);
1411 zfs_rangelock_exit(lr);
1419 * Free space in a file.
1421 * IN: zp - znode of file to free data in.
1422 * off - start of section to free.
1423 * len - length of section to free.
1425 * RETURN: 0 on success, error code on failure
1428 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1430 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1431 zfs_locked_range_t *lr;
1435 * Lock the range being freed.
1437 lr = zfs_rangelock_enter(&zp->z_rangelock, off, len, RL_WRITER);
1440 * Nothing to do if file already at desired length.
1442 if (off >= zp->z_size) {
1443 zfs_rangelock_exit(lr);
1447 if (off + len > zp->z_size)
1448 len = zp->z_size - off;
1450 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1454 * In FreeBSD we cannot free block in the middle of a file,
1455 * but only at the end of a file, so this code path should
1458 vnode_pager_setsize(ZTOV(zp), off);
1461 zfs_rangelock_exit(lr);
1469 * IN: zp - znode of file to free data in.
1470 * end - new end-of-file.
1472 * RETURN: 0 on success, error code on failure
1475 zfs_trunc(znode_t *zp, uint64_t end)
1477 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1478 vnode_t *vp = ZTOV(zp);
1480 zfs_locked_range_t *lr;
1482 sa_bulk_attr_t bulk[2];
1486 * We will change zp_size, lock the whole file.
1488 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER);
1491 * Nothing to do if file already at desired length.
1493 if (end >= zp->z_size) {
1494 zfs_rangelock_exit(lr);
1498 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,
1501 zfs_rangelock_exit(lr);
1504 tx = dmu_tx_create(zfsvfs->z_os);
1505 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1506 zfs_sa_upgrade_txholds(tx, zp);
1507 dmu_tx_mark_netfree(tx);
1508 error = dmu_tx_assign(tx, TXG_WAIT);
1511 zfs_rangelock_exit(lr);
1516 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1517 NULL, &zp->z_size, sizeof (zp->z_size));
1520 zp->z_pflags &= ~ZFS_SPARSE;
1521 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1522 NULL, &zp->z_pflags, 8);
1524 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1529 * Clear any mapped pages in the truncated region. This has to
1530 * happen outside of the transaction to avoid the possibility of
1531 * a deadlock with someone trying to push a page that we are
1532 * about to invalidate.
1534 vnode_pager_setsize(vp, end);
1536 zfs_rangelock_exit(lr);
1542 * Free space in a file
1544 * IN: zp - znode of file to free data in.
1545 * off - start of range
1546 * len - end of range (0 => EOF)
1547 * flag - current file open mode flags.
1548 * log - TRUE if this action should be logged
1550 * RETURN: 0 on success, error code on failure
1553 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1556 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1557 zilog_t *zilog = zfsvfs->z_log;
1559 uint64_t mtime[2], ctime[2];
1560 sa_bulk_attr_t bulk[3];
1564 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1565 sizeof (mode))) != 0)
1568 if (off > zp->z_size) {
1569 error = zfs_extend(zp, off+len);
1570 if (error == 0 && log)
1577 error = zfs_trunc(zp, off);
1579 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1580 off + len > zp->z_size)
1581 error = zfs_extend(zp, off+len);
1586 tx = dmu_tx_create(zfsvfs->z_os);
1587 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1588 zfs_sa_upgrade_txholds(tx, zp);
1589 error = dmu_tx_assign(tx, TXG_WAIT);
1595 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1596 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1597 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1598 NULL, &zp->z_pflags, 8);
1599 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
1600 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1603 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1610 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1612 uint64_t moid, obj, sa_obj, version;
1613 uint64_t sense = ZFS_CASE_SENSITIVE;
1618 znode_t *rootzp = NULL;
1622 zfs_acl_ids_t acl_ids;
1625 * First attempt to create master node.
1628 * In an empty objset, there are no blocks to read and thus
1629 * there can be no i/o errors (which we assert below).
1631 moid = MASTER_NODE_OBJ;
1632 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1633 DMU_OT_NONE, 0, tx);
1637 * Set starting attributes.
1639 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1641 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1642 /* For the moment we expect all zpl props to be uint64_ts */
1646 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1647 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1648 name = nvpair_name(elem);
1649 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1653 error = zap_update(os, moid, name, 8, 1, &val, tx);
1656 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1658 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1661 ASSERT(version != 0);
1662 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1665 * Create zap object used for SA attribute registration
1668 if (version >= ZPL_VERSION_SA) {
1669 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1670 DMU_OT_NONE, 0, tx);
1671 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1677 * Create a delete queue.
1679 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1681 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1685 * Create root znode. Create minimal znode/vnode/zfsvfs
1686 * to allow zfs_mknode to work.
1689 vattr.va_mask = AT_MODE|AT_UID|AT_GID;
1690 vattr.va_type = VDIR;
1691 vattr.va_mode = S_IFDIR|0755;
1692 vattr.va_uid = crgetuid(cr);
1693 vattr.va_gid = crgetgid(cr);
1695 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1697 rootzp = zfs_znode_alloc_kmem(KM_SLEEP);
1698 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1699 rootzp->z_unlinked = 0;
1700 rootzp->z_atime_dirty = 0;
1701 rootzp->z_is_sa = USE_SA(version, os);
1704 zfsvfs->z_parent = zfsvfs;
1705 zfsvfs->z_version = version;
1706 zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1707 zfsvfs->z_use_sa = USE_SA(version, os);
1708 zfsvfs->z_norm = norm;
1710 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1711 &zfsvfs->z_attr_table);
1716 * Fold case on file systems that are always or sometimes case
1719 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1720 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1722 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1723 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1724 offsetof(znode_t, z_link_node));
1726 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1727 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1729 rootzp->z_zfsvfs = zfsvfs;
1730 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1731 cr, NULL, &acl_ids));
1732 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1733 ASSERT3P(zp, ==, rootzp);
1734 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1736 zfs_acl_ids_free(&acl_ids);
1737 POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1739 sa_handle_destroy(rootzp->z_sa_hdl);
1740 zfs_znode_free_kmem(rootzp);
1743 * Create shares directory
1746 error = zfs_create_share_dir(zfsvfs, tx);
1750 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1751 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1752 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1754 #endif /* _KERNEL */
1757 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1759 uint64_t sa_obj = 0;
1762 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1763 if (error != 0 && error != ENOENT)
1766 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1771 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1772 dmu_buf_t **db, void *tag)
1774 dmu_object_info_t doi;
1777 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1780 dmu_object_info_from_db(*db, &doi);
1781 if ((doi.doi_bonus_type != DMU_OT_SA &&
1782 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1783 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1784 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1785 sa_buf_rele(*db, tag);
1786 return (SET_ERROR(ENOTSUP));
1789 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1791 sa_buf_rele(*db, tag);
1799 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1801 sa_handle_destroy(hdl);
1802 sa_buf_rele(db, tag);
1806 * Given an object number, return its parent object number and whether
1807 * or not the object is an extended attribute directory.
1810 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
1811 uint64_t *pobjp, int *is_xattrdir)
1816 uint64_t parent_mode;
1817 sa_bulk_attr_t bulk[3];
1818 sa_handle_t *sa_hdl;
1823 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1824 &parent, sizeof (parent));
1825 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1826 &pflags, sizeof (pflags));
1827 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1828 &mode, sizeof (mode));
1830 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1834 * When a link is removed its parent pointer is not changed and will
1835 * be invalid. There are two cases where a link is removed but the
1836 * file stays around, when it goes to the delete queue and when there
1837 * are additional links.
1839 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
1843 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
1844 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1848 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1851 * Extended attributes can be applied to files, directories, etc.
1852 * Otherwise the parent must be a directory.
1854 if (!*is_xattrdir && !S_ISDIR(parent_mode))
1855 return (SET_ERROR(EINVAL));
1863 * Given an object number, return some zpl level statistics
1866 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1869 sa_bulk_attr_t bulk[4];
1872 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1873 &sb->zs_mode, sizeof (sb->zs_mode));
1874 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1875 &sb->zs_gen, sizeof (sb->zs_gen));
1876 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1877 &sb->zs_links, sizeof (sb->zs_links));
1878 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1879 &sb->zs_ctime, sizeof (sb->zs_ctime));
1881 return (sa_bulk_lookup(hdl, bulk, count));
1885 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1886 sa_attr_type_t *sa_table, char *buf, int len)
1888 sa_handle_t *sa_hdl;
1889 sa_handle_t *prevhdl = NULL;
1890 dmu_buf_t *prevdb = NULL;
1891 dmu_buf_t *sa_db = NULL;
1892 char *path = buf + len - 1;
1898 uint64_t deleteq_obj;
1899 VERIFY0(zap_lookup(osp, MASTER_NODE_OBJ,
1900 ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj));
1901 error = zap_lookup_int(osp, deleteq_obj, obj);
1904 } else if (error != ENOENT) {
1911 char component[MAXNAMELEN + 2];
1916 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
1918 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
1919 &is_xattrdir)) != 0)
1930 (void) sprintf(component + 1, "<xattrdir>");
1932 error = zap_value_search(osp, pobj, obj,
1933 ZFS_DIRENT_OBJ(-1ULL), component + 1);
1938 complen = strlen(component);
1940 ASSERT(path >= buf);
1941 bcopy(component, path, complen);
1944 if (sa_hdl != hdl) {
1948 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
1956 if (sa_hdl != NULL && sa_hdl != hdl) {
1957 ASSERT(sa_db != NULL);
1958 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1962 (void) memmove(buf, path, buf + len - path);
1968 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1970 sa_attr_type_t *sa_table;
1975 error = zfs_sa_setup(osp, &sa_table);
1979 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1983 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1985 zfs_release_sa_handle(hdl, db, FTAG);
1990 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
1993 char *path = buf + len - 1;
1994 sa_attr_type_t *sa_table;
2001 error = zfs_sa_setup(osp, &sa_table);
2005 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2009 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2011 zfs_release_sa_handle(hdl, db, FTAG);
2015 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2017 zfs_release_sa_handle(hdl, db, FTAG);
2023 zfs_inode_update(znode_t *zp)
2027 if ((object = ZTOV(zp)->v_object) == NULL ||
2028 zp->z_size == object->un_pager.vnp.vnp_size)
2031 vnode_pager_setsize(ZTOV(zp), zp->z_size);
2037 zfs_znode_parent_and_name(znode_t *zp, znode_t **dzpp, char *buf)
2039 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2044 /* Extended attributes should not be visible as regular files. */
2045 if ((zp->z_pflags & ZFS_XATTR) != 0)
2046 return (SET_ERROR(EINVAL));
2048 err = zfs_obj_to_pobj(zfsvfs->z_os, zp->z_sa_hdl, zfsvfs->z_attr_table,
2049 &parent, &is_xattrdir);
2052 ASSERT0(is_xattrdir);
2054 /* No name as this is a root object. */
2055 if (parent == zp->z_id)
2056 return (SET_ERROR(EINVAL));
2058 err = zap_value_search(zfsvfs->z_os, parent, zp->z_id,
2059 ZFS_DIRENT_OBJ(-1ULL), buf);
2062 err = zfs_zget(zfsvfs, parent, dzpp);
2065 #endif /* _KERNEL */