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.
25 /* Portions Copyright 2007 Jeremy Teo */
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/mntent.h>
35 #include <sys/mkdev.h>
36 #include <sys/u8_textprep.h>
37 #include <sys/dsl_dataset.h>
39 #include <sys/vfs_opreg.h>
40 #include <sys/vnode.h>
43 #include <sys/errno.h>
44 #include <sys/unistd.h>
46 #include <sys/atomic.h>
48 #include "fs/fs_subr.h"
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/zfs_rlock.h>
53 #include <sys/zfs_fuid.h>
54 #include <sys/zfs_vnops.h>
55 #include <sys/dnode.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/kidmap.h>
62 #include <sys/refcount.h>
65 #include <sys/zfs_znode.h>
67 #include <sys/zfs_sa.h>
68 #include <sys/zfs_stat.h>
71 #include "zfs_comutil.h"
74 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
75 * turned on when DEBUG is also defined.
82 #define ZNODE_STAT_ADD(stat) ((stat)++)
84 #define ZNODE_STAT_ADD(stat) /* nothing */
85 #endif /* ZNODE_STATS */
88 * Functions needed for userland (ie: libzpool) are not put under
89 * #ifdef_KERNEL; the rest of the functions have dependencies
90 * (such as VFS logic) that will not compile easily in userland.
94 static kmem_cache_t *znode_cache = NULL;
98 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
102 inode_init_once(ZTOI(zp));
103 list_link_init(&zp->z_link_node);
105 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
106 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
107 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
108 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
110 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
111 avl_create(&zp->z_range_avl, zfs_range_compare,
112 sizeof (rl_t), offsetof(rl_t, r_node));
114 zp->z_dirlocks = NULL;
115 zp->z_acl_cached = NULL;
122 zfs_znode_cache_destructor(void *buf, void *arg)
126 ASSERT(!list_link_active(&zp->z_link_node));
127 mutex_destroy(&zp->z_lock);
128 rw_destroy(&zp->z_parent_lock);
129 rw_destroy(&zp->z_name_lock);
130 mutex_destroy(&zp->z_acl_lock);
131 avl_destroy(&zp->z_range_avl);
132 mutex_destroy(&zp->z_range_lock);
134 ASSERT(zp->z_dirlocks == NULL);
135 ASSERT(zp->z_acl_cached == NULL);
144 ASSERT(znode_cache == NULL);
145 znode_cache = kmem_cache_create("zfs_znode_cache",
146 sizeof (znode_t), 0, zfs_znode_cache_constructor,
147 zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_KMEM);
157 kmem_cache_destroy(znode_cache);
162 zfs_create_share_dir(zfs_sb_t *zsb, dmu_tx_t *tx)
165 zfs_acl_ids_t acl_ids;
172 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
173 vattr.va_mode = S_IFDIR | 0555;
174 vattr.va_uid = crgetuid(kcred);
175 vattr.va_gid = crgetgid(kcred);
177 sharezp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
178 sharezp->z_moved = 0;
179 sharezp->z_unlinked = 0;
180 sharezp->z_atime_dirty = 0;
181 sharezp->z_zfsvfs = zfsvfs;
182 sharezp->z_is_sa = zfsvfs->z_use_sa;
188 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
189 kcred, NULL, &acl_ids));
190 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
191 ASSERT3P(zp, ==, sharezp);
192 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
193 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
194 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
195 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
196 zfsvfs->z_shares_dir = sharezp->z_id;
198 zfs_acl_ids_free(&acl_ids);
199 // ZTOV(sharezp)->v_count = 0;
200 sa_handle_destroy(sharezp->z_sa_hdl);
201 kmem_cache_free(znode_cache, sharezp);
206 #endif /* HAVE_SHARE */
210 zfs_znode_sa_init(zfs_sb_t *zsb, znode_t *zp,
211 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
213 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb, zp->z_id)));
215 mutex_enter(&zp->z_lock);
217 ASSERT(zp->z_sa_hdl == NULL);
218 ASSERT(zp->z_acl_cached == NULL);
219 if (sa_hdl == NULL) {
220 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, zp,
221 SA_HDL_SHARED, &zp->z_sa_hdl));
223 zp->z_sa_hdl = sa_hdl;
224 sa_set_userp(sa_hdl, zp);
227 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
229 mutex_exit(&zp->z_lock);
233 zfs_znode_dmu_fini(znode_t *zp)
235 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp), zp->z_id)) ||
237 RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock));
239 sa_handle_destroy(zp->z_sa_hdl);
244 * Called by new_inode() to allocate a new inode.
247 zfs_inode_alloc(struct super_block *sb, struct inode **ip)
251 zp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
258 * Called in multiple places when an inode should be destroyed.
261 zfs_inode_destroy(struct inode *ip)
263 znode_t *zp = ITOZ(ip);
264 zfs_sb_t *zsb = ZTOZSB(zp);
266 mutex_enter(&zsb->z_znodes_lock);
267 list_remove(&zsb->z_all_znodes, zp);
268 mutex_exit(&zsb->z_znodes_lock);
270 if (zp->z_acl_cached) {
271 zfs_acl_free(zp->z_acl_cached);
272 zp->z_acl_cached = NULL;
275 kmem_cache_free(znode_cache, zp);
279 zfs_inode_set_ops(zfs_sb_t *zsb, struct inode *ip)
283 switch (ip->i_mode & S_IFMT) {
285 ip->i_op = &zpl_inode_operations;
286 ip->i_fop = &zpl_file_operations;
287 ip->i_mapping->a_ops = &zpl_address_space_operations;
291 ip->i_op = &zpl_dir_inode_operations;
292 ip->i_fop = &zpl_dir_file_operations;
293 ITOZ(ip)->z_zn_prefetch = B_TRUE;
297 ip->i_op = &zpl_symlink_inode_operations;
304 VERIFY(sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zsb),
305 &rdev, sizeof (rdev)) == 0);
306 init_special_inode(ip, ip->i_mode, rdev);
307 ip->i_op = &zpl_special_inode_operations;
311 printk("ZFS: Invalid mode: 0x%x\n", ip->i_mode);
317 * Construct a znode+inode and initialize.
319 * This does not do a call to dmu_set_user() that is
320 * up to the caller to do, in case you don't want to
324 zfs_znode_alloc(zfs_sb_t *zsb, dmu_buf_t *db, int blksz,
325 dmu_object_type_t obj_type, uint64_t obj, sa_handle_t *hdl,
326 struct dentry *dentry)
331 sa_bulk_attr_t bulk[9];
336 ip = new_inode(zsb->z_sb);
341 ASSERT(zp->z_dirlocks == NULL);
345 zp->z_atime_dirty = 0;
347 zp->z_id = db->db_object;
349 zp->z_seq = 0x7A4653;
353 zfs_znode_sa_init(zsb, zp, db, obj_type, hdl);
355 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &zp->z_mode, 8);
356 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, &zp->z_gen, 8);
357 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
358 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, &zp->z_links, 8);
359 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
361 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL,
363 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
365 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &zp->z_uid, 8);
366 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &zp->z_gid, 8);
368 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
370 sa_handle_destroy(zp->z_sa_hdl);
376 ip->i_mode = zp->z_mode;
377 ip->i_mtime = ip->i_atime = ip->i_ctime = CURRENT_TIME_SEC;
378 zfs_inode_set_ops(zsb, ip);
380 if (insert_inode_locked(ip))
384 d_instantiate(dentry, ip);
386 mutex_enter(&zsb->z_znodes_lock);
387 list_insert_tail(&zsb->z_all_znodes, zp);
389 mutex_exit(&zsb->z_znodes_lock);
391 unlock_new_inode(ip);
395 unlock_new_inode(ip);
401 * Update the embedded inode given the znode. We should work toward
402 * eliminating this function as soon as possible by removing values
403 * which are duplicated between the znode and inode. If the generic
404 * inode has the correct field it should be used, and the ZFS code
405 * updated to access the inode. This can be done incrementally.
408 zfs_inode_update(znode_t *zp)
413 uint64_t atime[2], mtime[2], ctime[2];
419 sa_lookup(zp->z_sa_hdl, SA_ZPL_ATIME(zsb), &atime, 16);
420 sa_lookup(zp->z_sa_hdl, SA_ZPL_MTIME(zsb), &mtime, 16);
421 sa_lookup(zp->z_sa_hdl, SA_ZPL_CTIME(zsb), &ctime, 16);
423 spin_lock(&ip->i_lock);
424 ip->i_generation = zp->z_gen;
425 ip->i_uid = zp->z_uid;
426 ip->i_gid = zp->z_gid;
427 ip->i_nlink = zp->z_links;
428 ip->i_mode = zp->z_mode;
429 ip->i_blkbits = SPA_MINBLOCKSHIFT;
430 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize,
431 (u_longlong_t *)&ip->i_blocks);
433 ZFS_TIME_DECODE(&ip->i_atime, atime);
434 ZFS_TIME_DECODE(&ip->i_mtime, mtime);
435 ZFS_TIME_DECODE(&ip->i_ctime, ctime);
437 i_size_write(ip, zp->z_size);
438 spin_unlock(&ip->i_lock);
441 static uint64_t empty_xattr;
442 static uint64_t pad[4];
443 static zfs_acl_phys_t acl_phys;
445 * Create a new DMU object to hold a zfs znode.
447 * IN: dzp - parent directory for new znode
448 * vap - file attributes for new znode
449 * tx - dmu transaction id for zap operations
450 * cr - credentials of caller
452 * IS_ROOT_NODE - new object will be root
453 * IS_XATTR - new object is an attribute
454 * bonuslen - length of bonus buffer
455 * setaclp - File/Dir initial ACL
456 * fuidp - Tracks fuid allocation.
458 * OUT: zpp - allocated znode
462 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
463 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
465 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
466 uint64_t mode, size, links, parent, pflags;
467 uint64_t dzp_pflags = 0;
469 zfs_sb_t *zsb = ZTOZSB(dzp);
476 dmu_object_type_t obj_type;
477 sa_bulk_attr_t *sa_attrs;
479 zfs_acl_locator_cb_t locate = { 0 };
482 obj = vap->va_nodeid;
483 now = vap->va_ctime; /* see zfs_replay_create() */
484 gen = vap->va_nblocks; /* ditto */
488 gen = dmu_tx_get_txg(tx);
491 obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
492 bonuslen = (obj_type == DMU_OT_SA) ?
493 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
496 * Create a new DMU object.
499 * There's currently no mechanism for pre-reading the blocks that will
500 * be needed to allocate a new object, so we accept the small chance
501 * that there will be an i/o error and we will fail one of the
504 if (S_ISDIR(vap->va_mode)) {
506 err = zap_create_claim_norm(zsb->z_os, obj,
507 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
508 obj_type, bonuslen, tx);
509 ASSERT3U(err, ==, 0);
511 obj = zap_create_norm(zsb->z_os,
512 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
513 obj_type, bonuslen, tx);
517 err = dmu_object_claim(zsb->z_os, obj,
518 DMU_OT_PLAIN_FILE_CONTENTS, 0,
519 obj_type, bonuslen, tx);
520 ASSERT3U(err, ==, 0);
522 obj = dmu_object_alloc(zsb->z_os,
523 DMU_OT_PLAIN_FILE_CONTENTS, 0,
524 obj_type, bonuslen, tx);
528 ZFS_OBJ_HOLD_ENTER(zsb, obj);
529 VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db));
532 * If this is the root, fix up the half-initialized parent pointer
533 * to reference the just-allocated physical data area.
535 if (flag & IS_ROOT_NODE) {
538 dzp_pflags = dzp->z_pflags;
542 * If parent is an xattr, so am I.
544 if (dzp_pflags & ZFS_XATTR) {
548 if (zsb->z_use_fuids)
549 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
553 if (S_ISDIR(vap->va_mode)) {
554 size = 2; /* contents ("." and "..") */
555 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
560 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode) ||
561 S_ISFIFO(vap->va_mode) || S_ISSOCK(vap->va_mode))
565 mode = acl_ids->z_mode;
570 * No execs denied will be deterimed when zfs_mode_compute() is called.
572 pflags |= acl_ids->z_aclp->z_hints &
573 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
574 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
576 ZFS_TIME_ENCODE(&now, crtime);
577 ZFS_TIME_ENCODE(&now, ctime);
579 if (vap->va_mask & ATTR_ATIME) {
580 ZFS_TIME_ENCODE(&vap->va_atime, atime);
582 ZFS_TIME_ENCODE(&now, atime);
585 if (vap->va_mask & ATTR_MTIME) {
586 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
588 ZFS_TIME_ENCODE(&now, mtime);
591 /* Now add in all of the "SA" attributes */
592 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED,
596 * Setup the array of attributes to be replaced/set on the new file
598 * order for DMU_OT_ZNODE is critical since it needs to be constructed
599 * in the old znode_phys_t format. Don't change this ordering
601 sa_attrs = kmem_alloc(sizeof(sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
603 if (obj_type == DMU_OT_ZNODE) {
604 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
606 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
608 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
610 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
612 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
614 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
616 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
618 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
621 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
623 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
625 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
627 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb),
628 NULL, &acl_ids->z_fuid, 8);
629 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb),
630 NULL, &acl_ids->z_fgid, 8);
631 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
633 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
635 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
637 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
639 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
641 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
645 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8);
647 if (obj_type == DMU_OT_ZNODE) {
648 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL,
651 if (obj_type == DMU_OT_ZNODE ||
652 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode) ||
653 S_ISFIFO(vap->va_mode) || S_ISSOCK(vap->va_mode))) {
654 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb),
657 if (obj_type == DMU_OT_ZNODE) {
658 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
660 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL,
661 &acl_ids->z_fuid, 8);
662 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL,
663 &acl_ids->z_fgid, 8);
664 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad,
665 sizeof (uint64_t) * 4);
666 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL,
667 &acl_phys, sizeof (zfs_acl_phys_t));
668 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
669 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL,
670 &acl_ids->z_aclp->z_acl_count, 8);
671 locate.cb_aclp = acl_ids->z_aclp;
672 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb),
673 zfs_acl_data_locator, &locate,
674 acl_ids->z_aclp->z_acl_bytes);
675 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
676 acl_ids->z_fuid, acl_ids->z_fgid);
679 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
681 if (!(flag & IS_ROOT_NODE)) {
682 *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl,
684 ASSERT(*zpp != NULL);
686 err = zpl_xattr_security_init(ZTOI(*zpp), ZTOI(dzp));
687 ASSERT3S(err, ==, 0);
690 * If we are creating the root node, the "parent" we
691 * passed in is the znode for the root.
695 (*zpp)->z_sa_hdl = sa_hdl;
698 (*zpp)->z_pflags = pflags;
699 (*zpp)->z_mode = mode;
701 if (obj_type == DMU_OT_ZNODE ||
702 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
703 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
704 ASSERT3S(err, ==, 0);
706 kmem_free(sa_attrs, sizeof(sa_bulk_attr_t) * ZPL_END);
707 ZFS_OBJ_HOLD_EXIT(zsb, obj);
711 * zfs_xvattr_set only updates the in-core attributes
712 * it is assumed the caller will be doing an sa_bulk_update
713 * to push the changes out
716 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
720 xoap = xva_getxoptattr(xvap);
723 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
725 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
726 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
727 ×, sizeof (times), tx);
728 XVA_SET_RTN(xvap, XAT_CREATETIME);
730 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
731 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
733 XVA_SET_RTN(xvap, XAT_READONLY);
735 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
736 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
738 XVA_SET_RTN(xvap, XAT_HIDDEN);
740 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
741 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
743 XVA_SET_RTN(xvap, XAT_SYSTEM);
745 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
746 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
748 XVA_SET_RTN(xvap, XAT_ARCHIVE);
750 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
751 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
753 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
755 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
756 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
758 XVA_SET_RTN(xvap, XAT_NOUNLINK);
760 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
761 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
763 XVA_SET_RTN(xvap, XAT_APPENDONLY);
765 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
766 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
768 XVA_SET_RTN(xvap, XAT_NODUMP);
770 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
771 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
773 XVA_SET_RTN(xvap, XAT_OPAQUE);
775 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
776 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
777 xoap->xoa_av_quarantined, zp->z_pflags, tx);
778 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
780 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
781 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
783 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
785 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
786 zfs_sa_set_scanstamp(zp, xvap, tx);
787 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
789 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
790 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
792 XVA_SET_RTN(xvap, XAT_REPARSE);
794 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
795 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
797 XVA_SET_RTN(xvap, XAT_OFFLINE);
799 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
800 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
802 XVA_SET_RTN(xvap, XAT_SPARSE);
807 zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp)
809 dmu_object_info_t doi;
817 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
819 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
821 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
825 dmu_object_info_from_db(db, &doi);
826 if (doi.doi_bonus_type != DMU_OT_SA &&
827 (doi.doi_bonus_type != DMU_OT_ZNODE ||
828 (doi.doi_bonus_type == DMU_OT_ZNODE &&
829 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
830 sa_buf_rele(db, NULL);
831 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
835 hdl = dmu_buf_get_user(db);
837 zp = sa_get_userdata(hdl);
841 * Since "SA" does immediate eviction we
842 * should never find a sa handle that doesn't
843 * know about the znode.
846 ASSERT3P(zp, !=, NULL);
848 mutex_enter(&zp->z_lock);
849 ASSERT3U(zp->z_id, ==, obj_num);
850 if (zp->z_unlinked) {
857 sa_buf_rele(db, NULL);
858 mutex_exit(&zp->z_lock);
859 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
864 * Not found create new znode/vnode but only if file exists.
866 * There is a small window where zfs_vget() could
867 * find this object while a file create is still in
868 * progress. This is checked for in zfs_znode_alloc()
870 * if zfs_znode_alloc() fails it will drop the hold on the
873 zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size,
874 doi.doi_bonus_type, obj_num, NULL, NULL);
880 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
885 zfs_rezget(znode_t *zp)
887 zfs_sb_t *zsb = ZTOZSB(zp);
888 dmu_object_info_t doi;
890 uint64_t obj_num = zp->z_id;
892 sa_bulk_attr_t bulk[8];
897 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
899 mutex_enter(&zp->z_acl_lock);
900 if (zp->z_acl_cached) {
901 zfs_acl_free(zp->z_acl_cached);
902 zp->z_acl_cached = NULL;
905 mutex_exit(&zp->z_acl_lock);
906 ASSERT(zp->z_sa_hdl == NULL);
907 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
909 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
913 dmu_object_info_from_db(db, &doi);
914 if (doi.doi_bonus_type != DMU_OT_SA &&
915 (doi.doi_bonus_type != DMU_OT_ZNODE ||
916 (doi.doi_bonus_type == DMU_OT_ZNODE &&
917 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
918 sa_buf_rele(db, NULL);
919 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
923 zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL);
925 /* reload cached values */
926 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL,
928 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL,
929 &zp->z_size, sizeof (zp->z_size));
930 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL,
931 &zp->z_links, sizeof (zp->z_links));
932 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
933 &zp->z_pflags, sizeof (zp->z_pflags));
934 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
935 &zp->z_atime, sizeof (zp->z_atime));
936 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
937 &zp->z_uid, sizeof (zp->z_uid));
938 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL,
939 &zp->z_gid, sizeof (zp->z_gid));
940 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
941 &mode, sizeof (mode));
943 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
944 zfs_znode_dmu_fini(zp);
945 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
951 if (gen != zp->z_gen) {
952 zfs_znode_dmu_fini(zp);
953 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
957 zp->z_unlinked = (zp->z_links == 0);
958 zp->z_blksz = doi.doi_data_block_size;
960 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
966 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
968 zfs_sb_t *zsb = ZTOZSB(zp);
969 objset_t *os = zsb->z_os;
970 uint64_t obj = zp->z_id;
971 uint64_t acl_obj = zfs_external_acl(zp);
973 ZFS_OBJ_HOLD_ENTER(zsb, obj);
975 VERIFY(!zp->z_is_sa);
976 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
978 VERIFY(0 == dmu_object_free(os, obj, tx));
979 zfs_znode_dmu_fini(zp);
980 ZFS_OBJ_HOLD_EXIT(zsb, obj);
984 zfs_zinactive(znode_t *zp)
986 zfs_sb_t *zsb = ZTOZSB(zp);
987 uint64_t z_id = zp->z_id;
988 boolean_t drop_mutex = 0;
990 ASSERT(zp->z_sa_hdl);
993 * Don't allow a zfs_zget() while were trying to release this znode.
995 * Linux allows direct memory reclaim which means that any KM_SLEEP
996 * allocation may trigger inode eviction. This can lead to a deadlock
997 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
998 * zfs_zinactive() call path. To avoid this deadlock the process
999 * must not reacquire the mutex when it is already holding it.
1001 if (!ZFS_OBJ_HOLD_OWNED(zsb, z_id)) {
1002 ZFS_OBJ_HOLD_ENTER(zsb, z_id);
1006 mutex_enter(&zp->z_lock);
1009 * If this was the last reference to a file with no links,
1010 * remove the file from the file system.
1012 if (zp->z_unlinked) {
1013 mutex_exit(&zp->z_lock);
1016 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1022 mutex_exit(&zp->z_lock);
1023 zfs_znode_dmu_fini(zp);
1026 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1030 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1031 uint64_t ctime[2], boolean_t have_tx)
1037 if (have_tx) { /* will sa_bulk_update happen really soon? */
1038 zp->z_atime_dirty = 0;
1041 zp->z_atime_dirty = 1;
1044 if (flag & ATTR_ATIME) {
1045 ZFS_TIME_ENCODE(&now, zp->z_atime);
1048 if (flag & ATTR_MTIME) {
1049 ZFS_TIME_ENCODE(&now, mtime);
1050 if (ZTOZSB(zp)->z_use_fuids) {
1051 zp->z_pflags |= (ZFS_ARCHIVE |
1056 if (flag & ATTR_CTIME) {
1057 ZFS_TIME_ENCODE(&now, ctime);
1058 if (ZTOZSB(zp)->z_use_fuids)
1059 zp->z_pflags |= ZFS_ARCHIVE;
1064 * Grow the block size for a file.
1066 * IN: zp - znode of file to free data in.
1067 * size - requested block size
1068 * tx - open transaction.
1070 * NOTE: this function assumes that the znode is write locked.
1073 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1078 if (size <= zp->z_blksz)
1081 * If the file size is already greater than the current blocksize,
1082 * we will not grow. If there is more than one block in a file,
1083 * the blocksize cannot change.
1085 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1088 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
1091 if (error == ENOTSUP)
1093 ASSERT3U(error, ==, 0);
1095 /* What blocksize did we actually get? */
1096 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1101 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1102 * be calling back into the fs for a putpage(). E.g.: when truncating
1103 * a file, the pages being "thrown away* don't need to be written out.
1107 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1108 int flags, cred_t *cr)
1113 #endif /* HAVE_MMAP */
1116 * Increase the file length
1118 * IN: zp - znode of file to free data in.
1119 * end - new end-of-file
1121 * RETURN: 0 if success
1122 * error code if failure
1125 zfs_extend(znode_t *zp, uint64_t end)
1127 zfs_sb_t *zsb = ZTOZSB(zp);
1134 * We will change zp_size, lock the whole file.
1136 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1139 * Nothing to do if file already at desired length.
1141 if (end <= zp->z_size) {
1142 zfs_range_unlock(rl);
1146 tx = dmu_tx_create(zsb->z_os);
1147 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1148 zfs_sa_upgrade_txholds(tx, zp);
1149 if (end > zp->z_blksz &&
1150 (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) {
1152 * We are growing the file past the current block size.
1154 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
1155 ASSERT(!ISP2(zp->z_blksz));
1156 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1158 newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
1160 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1165 error = dmu_tx_assign(tx, TXG_NOWAIT);
1167 if (error == ERESTART) {
1173 zfs_range_unlock(rl);
1178 zfs_grow_blocksize(zp, newblksz, tx);
1182 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
1183 &zp->z_size, sizeof (zp->z_size), tx));
1185 zfs_range_unlock(rl);
1193 * Free space in a file.
1195 * IN: zp - znode of file to free data in.
1196 * off - start of section to free.
1197 * len - length of section to free.
1199 * RETURN: 0 if success
1200 * error code if failure
1203 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1205 zfs_sb_t *zsb = ZTOZSB(zp);
1210 * Lock the range being freed.
1212 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1215 * Nothing to do if file already at desired length.
1217 if (off >= zp->z_size) {
1218 zfs_range_unlock(rl);
1222 if (off + len > zp->z_size)
1223 len = zp->z_size - off;
1225 error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len);
1227 zfs_range_unlock(rl);
1235 * IN: zp - znode of file to free data in.
1236 * end - new end-of-file.
1238 * RETURN: 0 if success
1239 * error code if failure
1242 zfs_trunc(znode_t *zp, uint64_t end)
1244 zfs_sb_t *zsb = ZTOZSB(zp);
1248 sa_bulk_attr_t bulk[2];
1252 * We will change zp_size, lock the whole file.
1254 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1257 * Nothing to do if file already at desired length.
1259 if (end >= zp->z_size) {
1260 zfs_range_unlock(rl);
1264 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
1266 zfs_range_unlock(rl);
1270 tx = dmu_tx_create(zsb->z_os);
1271 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1272 zfs_sa_upgrade_txholds(tx, zp);
1273 error = dmu_tx_assign(tx, TXG_NOWAIT);
1275 if (error == ERESTART) {
1281 zfs_range_unlock(rl);
1286 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
1287 NULL, &zp->z_size, sizeof (zp->z_size));
1290 zp->z_pflags &= ~ZFS_SPARSE;
1291 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1292 NULL, &zp->z_pflags, 8);
1294 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1298 zfs_range_unlock(rl);
1304 * Free space in a file
1306 * IN: zp - znode of file to free data in.
1307 * off - start of range
1308 * len - end of range (0 => EOF)
1309 * flag - current file open mode flags.
1310 * log - TRUE if this action should be logged
1312 * RETURN: 0 if success
1313 * error code if failure
1316 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1318 #ifdef HAVE_MANDLOCKS
1319 struct inode *ip = ZTOI(zp);
1320 #endif /* HAVE_MANDLOCKS */
1322 zfs_sb_t *zsb = ZTOZSB(zp);
1323 zilog_t *zilog = zsb->z_log;
1325 uint64_t mtime[2], ctime[2];
1326 sa_bulk_attr_t bulk[3];
1330 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
1331 sizeof (mode))) != 0)
1334 if (off > zp->z_size) {
1335 error = zfs_extend(zp, off+len);
1336 if (error == 0 && log)
1342 #ifdef HAVE_MANDLOCKS
1344 * Check for any locks in the region to be freed.
1347 if (MANDLOCK(ip, (mode_t)mode)) {
1348 uint64_t length = (len ? len : zp->z_size - off);
1349 if (error = chklock(ip, FWRITE, off, length, flag, NULL))
1352 #endif /* HAVE_MANDLOCKS */
1355 error = zfs_trunc(zp, off);
1357 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1358 off + len > zp->z_size)
1359 error = zfs_extend(zp, off+len);
1364 tx = dmu_tx_create(zsb->z_os);
1365 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1366 zfs_sa_upgrade_txholds(tx, zp);
1367 error = dmu_tx_assign(tx, TXG_NOWAIT);
1369 if (error == ERESTART) {
1378 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1379 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1380 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1381 NULL, &zp->z_pflags, 8);
1382 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1383 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1386 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1389 zfs_inode_update(zp);
1394 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1396 uint64_t moid, obj, sa_obj, version;
1405 * First attempt to create master node.
1408 * In an empty objset, there are no blocks to read and thus
1409 * there can be no i/o errors (which we assert below).
1411 moid = MASTER_NODE_OBJ;
1412 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1413 DMU_OT_NONE, 0, tx);
1417 * Set starting attributes.
1419 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1421 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1422 /* For the moment we expect all zpl props to be uint64_ts */
1426 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1427 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1428 name = nvpair_name(elem);
1429 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1433 error = zap_update(os, moid, name, 8, 1, &val, tx);
1436 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1439 ASSERT(version != 0);
1440 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1443 * Create zap object used for SA attribute registration
1446 if (version >= ZPL_VERSION_SA) {
1447 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1448 DMU_OT_NONE, 0, tx);
1449 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1455 * Create a delete queue.
1457 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1459 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1463 * Create root znode with code free of VFS dependencies. This
1464 * is important because without a registered filesystem and super
1465 * block all the required VFS hooks will be missing. The critical
1466 * thing is to just crete the required root znode.
1468 obj = zap_create_norm(os, norm, DMU_OT_DIRECTORY_CONTENTS,
1469 DMU_OT_ZNODE, sizeof (znode_phys_t), tx);
1471 VERIFY(0 == dmu_bonus_hold(os, obj, FTAG, &db));
1472 dmu_buf_will_dirty(db, tx);
1475 * Initialize the znode physical data to zero.
1477 ASSERT(db->db_size >= sizeof (znode_phys_t));
1478 bzero(db->db_data, db->db_size);
1481 if (USE_FUIDS(version, os))
1482 pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
1484 pzp->zp_size = 2; /* "." and ".." */
1486 pzp->zp_parent = obj;
1487 pzp->zp_gen = dmu_tx_get_txg(tx);
1488 pzp->zp_mode = S_IFDIR | 0755;
1489 pzp->zp_flags = ZFS_ACL_TRIVIAL;
1493 ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
1494 ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
1495 ZFS_TIME_ENCODE(&now, pzp->zp_atime);
1496 ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
1498 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &obj, tx);
1501 dmu_buf_rele(db, FTAG);
1504 #endif /* _KERNEL */
1507 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1509 uint64_t sa_obj = 0;
1512 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1513 if (error != 0 && error != ENOENT)
1516 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1521 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1524 dmu_object_info_t doi;
1527 if ((error = sa_buf_hold(osp, obj, FTAG, db)) != 0)
1530 dmu_object_info_from_db(*db, &doi);
1531 if ((doi.doi_bonus_type != DMU_OT_SA &&
1532 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1533 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1534 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1535 sa_buf_rele(*db, FTAG);
1539 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1541 sa_buf_rele(*db, FTAG);
1549 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db)
1551 sa_handle_destroy(hdl);
1552 sa_buf_rele(db, FTAG);
1556 * Given an object number, return its parent object number and whether
1557 * or not the object is an extended attribute directory.
1560 zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
1566 sa_bulk_attr_t bulk[3];
1570 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1571 &parent, sizeof (parent));
1572 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1573 &pflags, sizeof (pflags));
1574 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1575 &mode, sizeof (mode));
1577 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1581 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1587 * Given an object number, return some zpl level statistics
1590 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1593 sa_bulk_attr_t bulk[4];
1596 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1597 &sb->zs_mode, sizeof (sb->zs_mode));
1598 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1599 &sb->zs_gen, sizeof (sb->zs_gen));
1600 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1601 &sb->zs_links, sizeof (sb->zs_links));
1602 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1603 &sb->zs_ctime, sizeof (sb->zs_ctime));
1605 return (sa_bulk_lookup(hdl, bulk, count));
1609 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1610 sa_attr_type_t *sa_table, char *buf, int len)
1612 sa_handle_t *sa_hdl;
1613 sa_handle_t *prevhdl = NULL;
1614 dmu_buf_t *prevdb = NULL;
1615 dmu_buf_t *sa_db = NULL;
1616 char *path = buf + len - 1;
1624 char component[MAXNAMELEN + 2];
1629 zfs_release_sa_handle(prevhdl, prevdb);
1631 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
1632 &is_xattrdir)) != 0)
1643 (void) sprintf(component + 1, "<xattrdir>");
1645 error = zap_value_search(osp, pobj, obj,
1646 ZFS_DIRENT_OBJ(-1ULL), component + 1);
1651 complen = strlen(component);
1653 ASSERT(path >= buf);
1654 bcopy(component, path, complen);
1657 if (sa_hdl != hdl) {
1661 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db);
1669 if (sa_hdl != NULL && sa_hdl != hdl) {
1670 ASSERT(sa_db != NULL);
1671 zfs_release_sa_handle(sa_hdl, sa_db);
1675 (void) memmove(buf, path, buf + len - path);
1681 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1683 sa_attr_type_t *sa_table;
1688 error = zfs_sa_setup(osp, &sa_table);
1692 error = zfs_grab_sa_handle(osp, obj, &hdl, &db);
1696 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1698 zfs_release_sa_handle(hdl, db);
1703 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
1706 char *path = buf + len - 1;
1707 sa_attr_type_t *sa_table;
1714 error = zfs_sa_setup(osp, &sa_table);
1718 error = zfs_grab_sa_handle(osp, obj, &hdl, &db);
1722 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
1724 zfs_release_sa_handle(hdl, db);
1728 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1730 zfs_release_sa_handle(hdl, db);
1734 #if defined(_KERNEL) && defined(HAVE_SPL)
1735 EXPORT_SYMBOL(zfs_create_fs);
1736 EXPORT_SYMBOL(zfs_obj_to_path);