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 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/mntent.h>
36 #include <sys/mkdev.h>
37 #include <sys/u8_textprep.h>
38 #include <sys/dsl_dataset.h>
40 #include <sys/vfs_opreg.h>
41 #include <sys/vnode.h>
44 #include <sys/errno.h>
45 #include <sys/unistd.h>
47 #include <sys/atomic.h>
49 #include "fs/fs_subr.h"
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_rlock.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/zfs_vnops.h>
56 #include <sys/zfs_ctldir.h>
57 #include <sys/dnode.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/kidmap.h>
64 #include <sys/refcount.h>
67 #include <sys/zfs_znode.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_stat.h>
73 #include "zfs_comutil.h"
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 static kmem_cache_t *znode_cache = NULL;
100 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
104 inode_init_once(ZTOI(zp));
105 list_link_init(&zp->z_link_node);
107 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
108 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
109 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
110 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
111 rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL);
113 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
114 avl_create(&zp->z_range_avl, zfs_range_compare,
115 sizeof (rl_t), offsetof(rl_t, r_node));
117 zp->z_dirlocks = NULL;
118 zp->z_acl_cached = NULL;
119 zp->z_xattr_cached = NULL;
120 zp->z_xattr_parent = NULL;
127 zfs_znode_cache_destructor(void *buf, void *arg)
131 ASSERT(!list_link_active(&zp->z_link_node));
132 mutex_destroy(&zp->z_lock);
133 rw_destroy(&zp->z_parent_lock);
134 rw_destroy(&zp->z_name_lock);
135 mutex_destroy(&zp->z_acl_lock);
136 rw_destroy(&zp->z_xattr_lock);
137 avl_destroy(&zp->z_range_avl);
138 mutex_destroy(&zp->z_range_lock);
140 ASSERT(zp->z_dirlocks == NULL);
141 ASSERT(zp->z_acl_cached == NULL);
142 ASSERT(zp->z_xattr_cached == NULL);
143 ASSERT(zp->z_xattr_parent == NULL);
152 ASSERT(znode_cache == NULL);
153 znode_cache = kmem_cache_create("zfs_znode_cache",
154 sizeof (znode_t), 0, zfs_znode_cache_constructor,
155 zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_KMEM);
165 kmem_cache_destroy(znode_cache);
170 zfs_create_share_dir(zfs_sb_t *zsb, dmu_tx_t *tx)
172 #ifdef HAVE_SMB_SHARE
173 zfs_acl_ids_t acl_ids;
180 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
181 vattr.va_mode = S_IFDIR | 0555;
182 vattr.va_uid = crgetuid(kcred);
183 vattr.va_gid = crgetgid(kcred);
185 sharezp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
186 sharezp->z_moved = 0;
187 sharezp->z_unlinked = 0;
188 sharezp->z_atime_dirty = 0;
189 sharezp->z_zfsvfs = zfsvfs;
190 sharezp->z_is_sa = zfsvfs->z_use_sa;
196 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
197 kcred, NULL, &acl_ids));
198 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
199 ASSERT3P(zp, ==, sharezp);
200 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
201 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
202 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
203 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
204 zfsvfs->z_shares_dir = sharezp->z_id;
206 zfs_acl_ids_free(&acl_ids);
207 // ZTOV(sharezp)->v_count = 0;
208 sa_handle_destroy(sharezp->z_sa_hdl);
209 kmem_cache_free(znode_cache, sharezp);
214 #endif /* HAVE_SMB_SHARE */
218 zfs_znode_sa_init(zfs_sb_t *zsb, znode_t *zp,
219 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
221 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb, zp->z_id)));
223 mutex_enter(&zp->z_lock);
225 ASSERT(zp->z_sa_hdl == NULL);
226 ASSERT(zp->z_acl_cached == NULL);
227 if (sa_hdl == NULL) {
228 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, zp,
229 SA_HDL_SHARED, &zp->z_sa_hdl));
231 zp->z_sa_hdl = sa_hdl;
232 sa_set_userp(sa_hdl, zp);
235 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
237 mutex_exit(&zp->z_lock);
241 zfs_znode_dmu_fini(znode_t *zp)
243 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp), zp->z_id)) ||
245 RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock));
247 sa_handle_destroy(zp->z_sa_hdl);
252 * Called by new_inode() to allocate a new inode.
255 zfs_inode_alloc(struct super_block *sb, struct inode **ip)
259 zp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
266 * Called in multiple places when an inode should be destroyed.
269 zfs_inode_destroy(struct inode *ip)
271 znode_t *zp = ITOZ(ip);
272 zfs_sb_t *zsb = ZTOZSB(zp);
274 if (zfsctl_is_node(ip))
275 zfsctl_inode_destroy(ip);
277 mutex_enter(&zsb->z_znodes_lock);
278 if (list_link_active(&zp->z_link_node)) {
279 list_remove(&zsb->z_all_znodes, zp);
282 mutex_exit(&zsb->z_znodes_lock);
284 if (zp->z_acl_cached) {
285 zfs_acl_free(zp->z_acl_cached);
286 zp->z_acl_cached = NULL;
289 if (zp->z_xattr_cached) {
290 nvlist_free(zp->z_xattr_cached);
291 zp->z_xattr_cached = NULL;
294 if (zp->z_xattr_parent) {
295 iput(ZTOI(zp->z_xattr_parent));
296 zp->z_xattr_parent = NULL;
299 kmem_cache_free(znode_cache, zp);
303 zfs_inode_set_ops(zfs_sb_t *zsb, struct inode *ip)
307 switch (ip->i_mode & S_IFMT) {
309 ip->i_op = &zpl_inode_operations;
310 ip->i_fop = &zpl_file_operations;
311 ip->i_mapping->a_ops = &zpl_address_space_operations;
315 ip->i_op = &zpl_dir_inode_operations;
316 ip->i_fop = &zpl_dir_file_operations;
317 ITOZ(ip)->z_zn_prefetch = B_TRUE;
321 ip->i_op = &zpl_symlink_inode_operations;
325 * rdev is only stored in a SA only for device files.
329 VERIFY(sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zsb),
330 &rdev, sizeof (rdev)) == 0);
334 init_special_inode(ip, ip->i_mode, rdev);
335 ip->i_op = &zpl_special_inode_operations;
339 printk("ZFS: Invalid mode: 0x%x\n", ip->i_mode);
345 * Construct a znode+inode and initialize.
347 * This does not do a call to dmu_set_user() that is
348 * up to the caller to do, in case you don't want to
352 zfs_znode_alloc(zfs_sb_t *zsb, dmu_buf_t *db, int blksz,
353 dmu_object_type_t obj_type, uint64_t obj, sa_handle_t *hdl,
359 sa_bulk_attr_t bulk[9];
364 ip = new_inode(zsb->z_sb);
369 ASSERT(zp->z_dirlocks == NULL);
370 ASSERT3P(zp->z_acl_cached, ==, NULL);
371 ASSERT3P(zp->z_xattr_cached, ==, NULL);
372 ASSERT3P(zp->z_xattr_parent, ==, NULL);
376 zp->z_atime_dirty = 0;
378 zp->z_id = db->db_object;
380 zp->z_seq = 0x7A4653;
382 zp->z_is_zvol = B_FALSE;
383 zp->z_is_mapped = B_FALSE;
384 zp->z_is_ctldir = B_FALSE;
385 zp->z_is_stale = B_FALSE;
387 zfs_znode_sa_init(zsb, zp, db, obj_type, hdl);
389 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &zp->z_mode, 8);
390 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, &zp->z_gen, 8);
391 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
392 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, &zp->z_links, 8);
393 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
395 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL,
397 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
399 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &zp->z_uid, 8);
400 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &zp->z_gid, 8);
402 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
404 sa_handle_destroy(zp->z_sa_hdl);
410 * xattr znodes hold a reference on their unique parent
412 if (dip && zp->z_pflags & ZFS_XATTR) {
414 zp->z_xattr_parent = ITOZ(dip);
418 zfs_inode_update(zp);
419 zfs_inode_set_ops(zsb, ip);
422 * The only way insert_inode_locked() can fail is if the ip->i_ino
423 * number is already hashed for this super block. This can never
424 * happen because the inode numbers map 1:1 with the object numbers.
426 * The one exception is rolling back a mounted file system, but in
427 * this case all the active inode are unhashed during the rollback.
429 VERIFY3S(insert_inode_locked(ip), ==, 0);
431 mutex_enter(&zsb->z_znodes_lock);
432 list_insert_tail(&zsb->z_all_znodes, zp);
435 mutex_exit(&zsb->z_znodes_lock);
437 unlock_new_inode(ip);
441 unlock_new_inode(ip);
447 * Update the embedded inode given the znode. We should work toward
448 * eliminating this function as soon as possible by removing values
449 * which are duplicated between the znode and inode. If the generic
450 * inode has the correct field it should be used, and the ZFS code
451 * updated to access the inode. This can be done incrementally.
454 zfs_inode_update(znode_t *zp)
459 uint64_t atime[2], mtime[2], ctime[2];
465 /* Skip .zfs control nodes which do not exist on disk. */
466 if (zfsctl_is_node(ip))
469 sa_lookup(zp->z_sa_hdl, SA_ZPL_ATIME(zsb), &atime, 16);
470 sa_lookup(zp->z_sa_hdl, SA_ZPL_MTIME(zsb), &mtime, 16);
471 sa_lookup(zp->z_sa_hdl, SA_ZPL_CTIME(zsb), &ctime, 16);
473 spin_lock(&ip->i_lock);
474 ip->i_generation = zp->z_gen;
475 ip->i_uid = zp->z_uid;
476 ip->i_gid = zp->z_gid;
477 set_nlink(ip, zp->z_links);
478 ip->i_mode = zp->z_mode;
479 ip->i_blkbits = SPA_MINBLOCKSHIFT;
480 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize,
481 (u_longlong_t *)&ip->i_blocks);
483 ZFS_TIME_DECODE(&ip->i_atime, atime);
484 ZFS_TIME_DECODE(&ip->i_mtime, mtime);
485 ZFS_TIME_DECODE(&ip->i_ctime, ctime);
487 i_size_write(ip, zp->z_size);
488 spin_unlock(&ip->i_lock);
491 static uint64_t empty_xattr;
492 static uint64_t pad[4];
493 static zfs_acl_phys_t acl_phys;
495 * Create a new DMU object to hold a zfs znode.
497 * IN: dzp - parent directory for new znode
498 * vap - file attributes for new znode
499 * tx - dmu transaction id for zap operations
500 * cr - credentials of caller
502 * IS_ROOT_NODE - new object will be root
503 * IS_XATTR - new object is an attribute
504 * bonuslen - length of bonus buffer
505 * setaclp - File/Dir initial ACL
506 * fuidp - Tracks fuid allocation.
508 * OUT: zpp - allocated znode
512 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
513 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
515 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
516 uint64_t mode, size, links, parent, pflags;
517 uint64_t dzp_pflags = 0;
519 zfs_sb_t *zsb = ZTOZSB(dzp);
526 dmu_object_type_t obj_type;
527 sa_bulk_attr_t *sa_attrs;
529 zfs_acl_locator_cb_t locate = { 0 };
532 obj = vap->va_nodeid;
533 now = vap->va_ctime; /* see zfs_replay_create() */
534 gen = vap->va_nblocks; /* ditto */
538 gen = dmu_tx_get_txg(tx);
541 obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
542 bonuslen = (obj_type == DMU_OT_SA) ?
543 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
546 * Create a new DMU object.
549 * There's currently no mechanism for pre-reading the blocks that will
550 * be needed to allocate a new object, so we accept the small chance
551 * that there will be an i/o error and we will fail one of the
554 if (S_ISDIR(vap->va_mode)) {
556 err = zap_create_claim_norm(zsb->z_os, obj,
557 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
558 obj_type, bonuslen, tx);
561 obj = zap_create_norm(zsb->z_os,
562 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
563 obj_type, bonuslen, tx);
567 err = dmu_object_claim(zsb->z_os, obj,
568 DMU_OT_PLAIN_FILE_CONTENTS, 0,
569 obj_type, bonuslen, tx);
572 obj = dmu_object_alloc(zsb->z_os,
573 DMU_OT_PLAIN_FILE_CONTENTS, 0,
574 obj_type, bonuslen, tx);
578 ZFS_OBJ_HOLD_ENTER(zsb, obj);
579 VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db));
582 * If this is the root, fix up the half-initialized parent pointer
583 * to reference the just-allocated physical data area.
585 if (flag & IS_ROOT_NODE) {
588 dzp_pflags = dzp->z_pflags;
592 * If parent is an xattr, so am I.
594 if (dzp_pflags & ZFS_XATTR) {
598 if (zsb->z_use_fuids)
599 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
603 if (S_ISDIR(vap->va_mode)) {
604 size = 2; /* contents ("." and "..") */
605 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
610 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
614 mode = acl_ids->z_mode;
619 * No execs denied will be deterimed when zfs_mode_compute() is called.
621 pflags |= acl_ids->z_aclp->z_hints &
622 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
623 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
625 ZFS_TIME_ENCODE(&now, crtime);
626 ZFS_TIME_ENCODE(&now, ctime);
628 if (vap->va_mask & ATTR_ATIME) {
629 ZFS_TIME_ENCODE(&vap->va_atime, atime);
631 ZFS_TIME_ENCODE(&now, atime);
634 if (vap->va_mask & ATTR_MTIME) {
635 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
637 ZFS_TIME_ENCODE(&now, mtime);
640 /* Now add in all of the "SA" attributes */
641 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED,
645 * Setup the array of attributes to be replaced/set on the new file
647 * order for DMU_OT_ZNODE is critical since it needs to be constructed
648 * in the old znode_phys_t format. Don't change this ordering
650 sa_attrs = kmem_alloc(sizeof(sa_bulk_attr_t) * ZPL_END, KM_PUSHPAGE);
652 if (obj_type == DMU_OT_ZNODE) {
653 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
655 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
657 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
659 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
661 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
663 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
665 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
667 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
670 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
672 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
674 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
676 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb),
677 NULL, &acl_ids->z_fuid, 8);
678 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb),
679 NULL, &acl_ids->z_fgid, 8);
680 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
682 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
684 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
686 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
688 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
690 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
694 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8);
696 if (obj_type == DMU_OT_ZNODE) {
697 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL,
700 if (obj_type == DMU_OT_ZNODE ||
701 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
702 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb),
705 if (obj_type == DMU_OT_ZNODE) {
706 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
708 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL,
709 &acl_ids->z_fuid, 8);
710 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL,
711 &acl_ids->z_fgid, 8);
712 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad,
713 sizeof (uint64_t) * 4);
714 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL,
715 &acl_phys, sizeof (zfs_acl_phys_t));
716 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
717 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL,
718 &acl_ids->z_aclp->z_acl_count, 8);
719 locate.cb_aclp = acl_ids->z_aclp;
720 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb),
721 zfs_acl_data_locator, &locate,
722 acl_ids->z_aclp->z_acl_bytes);
723 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
724 acl_ids->z_fuid, acl_ids->z_fgid);
727 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
729 if (!(flag & IS_ROOT_NODE)) {
730 *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl,
732 VERIFY(*zpp != NULL);
736 * If we are creating the root node, the "parent" we
737 * passed in is the znode for the root.
741 (*zpp)->z_sa_hdl = sa_hdl;
744 (*zpp)->z_pflags = pflags;
745 (*zpp)->z_mode = mode;
747 if (obj_type == DMU_OT_ZNODE ||
748 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
749 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
752 kmem_free(sa_attrs, sizeof(sa_bulk_attr_t) * ZPL_END);
753 ZFS_OBJ_HOLD_EXIT(zsb, obj);
757 * zfs_xvattr_set only updates the in-core attributes
758 * it is assumed the caller will be doing an sa_bulk_update
759 * to push the changes out
762 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
766 xoap = xva_getxoptattr(xvap);
769 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
771 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
772 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
773 ×, sizeof (times), tx);
774 XVA_SET_RTN(xvap, XAT_CREATETIME);
776 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
777 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
779 XVA_SET_RTN(xvap, XAT_READONLY);
781 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
782 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
784 XVA_SET_RTN(xvap, XAT_HIDDEN);
786 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
787 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
789 XVA_SET_RTN(xvap, XAT_SYSTEM);
791 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
792 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
794 XVA_SET_RTN(xvap, XAT_ARCHIVE);
796 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
797 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
799 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
801 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
802 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
804 XVA_SET_RTN(xvap, XAT_NOUNLINK);
806 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
807 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
809 XVA_SET_RTN(xvap, XAT_APPENDONLY);
811 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
812 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
814 XVA_SET_RTN(xvap, XAT_NODUMP);
816 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
817 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
819 XVA_SET_RTN(xvap, XAT_OPAQUE);
821 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
822 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
823 xoap->xoa_av_quarantined, zp->z_pflags, tx);
824 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
826 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
827 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
829 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
831 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
832 zfs_sa_set_scanstamp(zp, xvap, tx);
833 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
835 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
836 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
838 XVA_SET_RTN(xvap, XAT_REPARSE);
840 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
841 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
843 XVA_SET_RTN(xvap, XAT_OFFLINE);
845 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
846 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
848 XVA_SET_RTN(xvap, XAT_SPARSE);
853 zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp)
855 dmu_object_info_t doi;
865 ip = ilookup(zsb->z_sb, obj_num);
867 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
869 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
871 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
876 dmu_object_info_from_db(db, &doi);
877 if (doi.doi_bonus_type != DMU_OT_SA &&
878 (doi.doi_bonus_type != DMU_OT_ZNODE ||
879 (doi.doi_bonus_type == DMU_OT_ZNODE &&
880 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
881 sa_buf_rele(db, NULL);
882 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
887 hdl = dmu_buf_get_user(db);
891 * ilookup returned NULL, which means
892 * the znode is dying - but the SA handle isn't
893 * quite dead yet, we need to drop any locks
894 * we're holding, re-schedule the task and try again.
896 sa_buf_rele(db, NULL);
897 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
903 zp = sa_get_userdata(hdl);
906 * Since "SA" does immediate eviction we
907 * should never find a sa handle that doesn't
908 * know about the znode.
911 ASSERT3P(zp, !=, NULL);
913 mutex_enter(&zp->z_lock);
914 ASSERT3U(zp->z_id, ==, obj_num);
915 if (zp->z_unlinked) {
922 sa_buf_rele(db, NULL);
923 mutex_exit(&zp->z_lock);
924 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
929 ASSERT3P(ip, ==, NULL);
932 * Not found create new znode/vnode but only if file exists.
934 * There is a small window where zfs_vget() could
935 * find this object while a file create is still in
936 * progress. This is checked for in zfs_znode_alloc()
938 * if zfs_znode_alloc() fails it will drop the hold on the
941 zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size,
942 doi.doi_bonus_type, obj_num, NULL, NULL);
948 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
953 zfs_rezget(znode_t *zp)
955 zfs_sb_t *zsb = ZTOZSB(zp);
956 dmu_object_info_t doi;
958 uint64_t obj_num = zp->z_id;
960 sa_bulk_attr_t bulk[8];
965 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
967 mutex_enter(&zp->z_acl_lock);
968 if (zp->z_acl_cached) {
969 zfs_acl_free(zp->z_acl_cached);
970 zp->z_acl_cached = NULL;
972 mutex_exit(&zp->z_acl_lock);
974 rw_enter(&zp->z_xattr_lock, RW_WRITER);
975 if (zp->z_xattr_cached) {
976 nvlist_free(zp->z_xattr_cached);
977 zp->z_xattr_cached = NULL;
980 if (zp->z_xattr_parent) {
981 iput(ZTOI(zp->z_xattr_parent));
982 zp->z_xattr_parent = NULL;
984 rw_exit(&zp->z_xattr_lock);
986 ASSERT(zp->z_sa_hdl == NULL);
987 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
989 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
993 dmu_object_info_from_db(db, &doi);
994 if (doi.doi_bonus_type != DMU_OT_SA &&
995 (doi.doi_bonus_type != DMU_OT_ZNODE ||
996 (doi.doi_bonus_type == DMU_OT_ZNODE &&
997 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
998 sa_buf_rele(db, NULL);
999 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1003 zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL);
1005 /* reload cached values */
1006 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL,
1007 &gen, sizeof (gen));
1008 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL,
1009 &zp->z_size, sizeof (zp->z_size));
1010 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL,
1011 &zp->z_links, sizeof (zp->z_links));
1012 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
1013 &zp->z_pflags, sizeof (zp->z_pflags));
1014 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
1015 &zp->z_atime, sizeof (zp->z_atime));
1016 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
1017 &zp->z_uid, sizeof (zp->z_uid));
1018 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL,
1019 &zp->z_gid, sizeof (zp->z_gid));
1020 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
1021 &mode, sizeof (mode));
1023 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1024 zfs_znode_dmu_fini(zp);
1025 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1031 if (gen != zp->z_gen) {
1032 zfs_znode_dmu_fini(zp);
1033 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1037 zp->z_unlinked = (zp->z_links == 0);
1038 zp->z_blksz = doi.doi_data_block_size;
1039 zfs_inode_update(zp);
1041 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1047 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1049 zfs_sb_t *zsb = ZTOZSB(zp);
1050 objset_t *os = zsb->z_os;
1051 uint64_t obj = zp->z_id;
1052 uint64_t acl_obj = zfs_external_acl(zp);
1054 ZFS_OBJ_HOLD_ENTER(zsb, obj);
1056 VERIFY(!zp->z_is_sa);
1057 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1059 VERIFY(0 == dmu_object_free(os, obj, tx));
1060 zfs_znode_dmu_fini(zp);
1061 ZFS_OBJ_HOLD_EXIT(zsb, obj);
1065 zfs_zinactive(znode_t *zp)
1067 zfs_sb_t *zsb = ZTOZSB(zp);
1068 uint64_t z_id = zp->z_id;
1069 boolean_t drop_mutex = 0;
1071 ASSERT(zp->z_sa_hdl);
1074 * Don't allow a zfs_zget() while were trying to release this znode.
1076 * Linux allows direct memory reclaim which means that any KM_SLEEP
1077 * allocation may trigger inode eviction. This can lead to a deadlock
1078 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
1079 * zfs_zinactive() call path. To avoid this deadlock the process
1080 * must not reacquire the mutex when it is already holding it.
1082 if (!ZFS_OBJ_HOLD_OWNED(zsb, z_id)) {
1083 ZFS_OBJ_HOLD_ENTER(zsb, z_id);
1087 mutex_enter(&zp->z_lock);
1090 * If this was the last reference to a file with no links,
1091 * remove the file from the file system.
1093 if (zp->z_unlinked) {
1094 mutex_exit(&zp->z_lock);
1097 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1103 mutex_exit(&zp->z_lock);
1104 zfs_znode_dmu_fini(zp);
1107 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1111 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1112 uint64_t ctime[2], boolean_t have_tx)
1118 if (have_tx) { /* will sa_bulk_update happen really soon? */
1119 zp->z_atime_dirty = 0;
1122 zp->z_atime_dirty = 1;
1125 if (flag & ATTR_ATIME) {
1126 ZFS_TIME_ENCODE(&now, zp->z_atime);
1129 if (flag & ATTR_MTIME) {
1130 ZFS_TIME_ENCODE(&now, mtime);
1131 if (ZTOZSB(zp)->z_use_fuids) {
1132 zp->z_pflags |= (ZFS_ARCHIVE |
1137 if (flag & ATTR_CTIME) {
1138 ZFS_TIME_ENCODE(&now, ctime);
1139 if (ZTOZSB(zp)->z_use_fuids)
1140 zp->z_pflags |= ZFS_ARCHIVE;
1145 * Grow the block size for a file.
1147 * IN: zp - znode of file to free data in.
1148 * size - requested block size
1149 * tx - open transaction.
1151 * NOTE: this function assumes that the znode is write locked.
1154 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1159 if (size <= zp->z_blksz)
1162 * If the file size is already greater than the current blocksize,
1163 * we will not grow. If there is more than one block in a file,
1164 * the blocksize cannot change.
1166 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1169 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
1172 if (error == ENOTSUP)
1176 /* What blocksize did we actually get? */
1177 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1181 * Increase the file length
1183 * IN: zp - znode of file to free data in.
1184 * end - new end-of-file
1186 * RETURN: 0 if success
1187 * error code if failure
1190 zfs_extend(znode_t *zp, uint64_t end)
1192 zfs_sb_t *zsb = ZTOZSB(zp);
1199 * We will change zp_size, lock the whole file.
1201 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1204 * Nothing to do if file already at desired length.
1206 if (end <= zp->z_size) {
1207 zfs_range_unlock(rl);
1211 tx = dmu_tx_create(zsb->z_os);
1212 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1213 zfs_sa_upgrade_txholds(tx, zp);
1214 if (end > zp->z_blksz &&
1215 (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) {
1217 * We are growing the file past the current block size.
1219 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
1220 ASSERT(!ISP2(zp->z_blksz));
1221 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1223 newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
1225 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1230 error = dmu_tx_assign(tx, TXG_NOWAIT);
1232 if (error == ERESTART) {
1238 zfs_range_unlock(rl);
1243 zfs_grow_blocksize(zp, newblksz, tx);
1247 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
1248 &zp->z_size, sizeof (zp->z_size), tx));
1250 zfs_range_unlock(rl);
1258 * Free space in a file.
1260 * IN: zp - znode of file to free data in.
1261 * off - start of section to free.
1262 * len - length of section to free.
1264 * RETURN: 0 if success
1265 * error code if failure
1268 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1270 zfs_sb_t *zsb = ZTOZSB(zp);
1275 * Lock the range being freed.
1277 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1280 * Nothing to do if file already at desired length.
1282 if (off >= zp->z_size) {
1283 zfs_range_unlock(rl);
1287 if (off + len > zp->z_size)
1288 len = zp->z_size - off;
1290 error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len);
1292 zfs_range_unlock(rl);
1300 * IN: zp - znode of file to free data in.
1301 * end - new end-of-file.
1303 * RETURN: 0 if success
1304 * error code if failure
1307 zfs_trunc(znode_t *zp, uint64_t end)
1309 zfs_sb_t *zsb = ZTOZSB(zp);
1313 sa_bulk_attr_t bulk[2];
1317 * We will change zp_size, lock the whole file.
1319 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1322 * Nothing to do if file already at desired length.
1324 if (end >= zp->z_size) {
1325 zfs_range_unlock(rl);
1329 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
1331 zfs_range_unlock(rl);
1335 tx = dmu_tx_create(zsb->z_os);
1336 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1337 zfs_sa_upgrade_txholds(tx, zp);
1338 error = dmu_tx_assign(tx, TXG_NOWAIT);
1340 if (error == ERESTART) {
1346 zfs_range_unlock(rl);
1351 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
1352 NULL, &zp->z_size, sizeof (zp->z_size));
1355 zp->z_pflags &= ~ZFS_SPARSE;
1356 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1357 NULL, &zp->z_pflags, 8);
1359 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1363 zfs_range_unlock(rl);
1369 * Free space in a file
1371 * IN: zp - znode of file to free data in.
1372 * off - start of range
1373 * len - end of range (0 => EOF)
1374 * flag - current file open mode flags.
1375 * log - TRUE if this action should be logged
1377 * RETURN: 0 if success
1378 * error code if failure
1381 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1383 struct inode *ip = ZTOI(zp);
1385 zfs_sb_t *zsb = ZTOZSB(zp);
1386 zilog_t *zilog = zsb->z_log;
1388 uint64_t mtime[2], ctime[2];
1389 sa_bulk_attr_t bulk[3];
1393 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
1394 sizeof (mode))) != 0)
1397 if (off > zp->z_size) {
1398 error = zfs_extend(zp, off+len);
1399 if (error == 0 && log)
1406 * Check for any locks in the region to be freed.
1408 if (ip->i_flock && mandatory_lock(ip)) {
1409 uint64_t length = (len ? len : zp->z_size - off);
1410 if (!lock_may_write(ip, off, length))
1415 error = zfs_trunc(zp, off);
1417 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1418 off + len > zp->z_size)
1419 error = zfs_extend(zp, off+len);
1424 tx = dmu_tx_create(zsb->z_os);
1425 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1426 zfs_sa_upgrade_txholds(tx, zp);
1427 error = dmu_tx_assign(tx, TXG_NOWAIT);
1429 if (error == ERESTART) {
1438 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1439 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1440 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1441 NULL, &zp->z_pflags, 8);
1442 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1443 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1446 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1449 zfs_inode_update(zp);
1454 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1456 struct super_block *sb;
1458 uint64_t moid, obj, sa_obj, version;
1459 uint64_t sense = ZFS_CASE_SENSITIVE;
1464 znode_t *rootzp = NULL;
1467 zfs_acl_ids_t acl_ids;
1470 * First attempt to create master node.
1473 * In an empty objset, there are no blocks to read and thus
1474 * there can be no i/o errors (which we assert below).
1476 moid = MASTER_NODE_OBJ;
1477 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1478 DMU_OT_NONE, 0, tx);
1482 * Set starting attributes.
1484 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1486 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1487 /* For the moment we expect all zpl props to be uint64_ts */
1491 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1492 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1493 name = nvpair_name(elem);
1494 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1498 error = zap_update(os, moid, name, 8, 1, &val, tx);
1501 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1503 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1506 ASSERT(version != 0);
1507 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1510 * Create zap object used for SA attribute registration
1513 if (version >= ZPL_VERSION_SA) {
1514 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1515 DMU_OT_NONE, 0, tx);
1516 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1522 * Create a delete queue.
1524 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1526 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1530 * Create root znode. Create minimal znode/inode/zsb/sb
1531 * to allow zfs_mknode to work.
1533 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1534 vattr.va_mode = S_IFDIR|0755;
1535 vattr.va_uid = crgetuid(cr);
1536 vattr.va_gid = crgetgid(cr);
1538 rootzp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
1539 rootzp->z_moved = 0;
1540 rootzp->z_unlinked = 0;
1541 rootzp->z_atime_dirty = 0;
1542 rootzp->z_is_sa = USE_SA(version, os);
1544 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_PUSHPAGE | KM_NODEBUG);
1546 zsb->z_parent = zsb;
1547 zsb->z_version = version;
1548 zsb->z_use_fuids = USE_FUIDS(version, os);
1549 zsb->z_use_sa = USE_SA(version, os);
1552 sb = kmem_zalloc(sizeof (struct super_block), KM_PUSHPAGE);
1553 sb->s_fs_info = zsb;
1555 ZTOI(rootzp)->i_sb = sb;
1557 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1558 &zsb->z_attr_table);
1563 * Fold case on file systems that are always or sometimes case
1566 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1567 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
1569 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1570 list_create(&zsb->z_all_znodes, sizeof (znode_t),
1571 offsetof(znode_t, z_link_node));
1573 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1574 mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1576 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1577 cr, NULL, &acl_ids));
1578 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1579 ASSERT3P(zp, ==, rootzp);
1580 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1582 zfs_acl_ids_free(&acl_ids);
1584 atomic_set(&ZTOI(rootzp)->i_count, 0);
1585 sa_handle_destroy(rootzp->z_sa_hdl);
1586 kmem_cache_free(znode_cache, rootzp);
1589 * Create shares directory
1591 error = zfs_create_share_dir(zsb, tx);
1594 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1595 mutex_destroy(&zsb->z_hold_mtx[i]);
1597 kmem_free(sb, sizeof (struct super_block));
1598 kmem_free(zsb, sizeof (zfs_sb_t));
1600 #endif /* _KERNEL */
1603 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1605 uint64_t sa_obj = 0;
1608 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1609 if (error != 0 && error != ENOENT)
1612 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1617 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1618 dmu_buf_t **db, void *tag)
1620 dmu_object_info_t doi;
1623 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1626 dmu_object_info_from_db(*db, &doi);
1627 if ((doi.doi_bonus_type != DMU_OT_SA &&
1628 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1629 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1630 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1631 sa_buf_rele(*db, tag);
1635 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1637 sa_buf_rele(*db, tag);
1645 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1647 sa_handle_destroy(hdl);
1648 sa_buf_rele(db, tag);
1652 * Given an object number, return its parent object number and whether
1653 * or not the object is an extended attribute directory.
1656 zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
1662 sa_bulk_attr_t bulk[3];
1666 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1667 &parent, sizeof (parent));
1668 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1669 &pflags, sizeof (pflags));
1670 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1671 &mode, sizeof (mode));
1673 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1677 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1683 * Given an object number, return some zpl level statistics
1686 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1689 sa_bulk_attr_t bulk[4];
1692 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1693 &sb->zs_mode, sizeof (sb->zs_mode));
1694 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1695 &sb->zs_gen, sizeof (sb->zs_gen));
1696 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1697 &sb->zs_links, sizeof (sb->zs_links));
1698 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1699 &sb->zs_ctime, sizeof (sb->zs_ctime));
1701 return (sa_bulk_lookup(hdl, bulk, count));
1705 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1706 sa_attr_type_t *sa_table, char *buf, int len)
1708 sa_handle_t *sa_hdl;
1709 sa_handle_t *prevhdl = NULL;
1710 dmu_buf_t *prevdb = NULL;
1711 dmu_buf_t *sa_db = NULL;
1712 char *path = buf + len - 1;
1720 char component[MAXNAMELEN + 2];
1725 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
1727 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
1728 &is_xattrdir)) != 0)
1739 (void) sprintf(component + 1, "<xattrdir>");
1741 error = zap_value_search(osp, pobj, obj,
1742 ZFS_DIRENT_OBJ(-1ULL), component + 1);
1747 complen = strlen(component);
1749 ASSERT(path >= buf);
1750 bcopy(component, path, complen);
1753 if (sa_hdl != hdl) {
1757 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
1765 if (sa_hdl != NULL && sa_hdl != hdl) {
1766 ASSERT(sa_db != NULL);
1767 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1771 (void) memmove(buf, path, buf + len - path);
1777 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1779 sa_attr_type_t *sa_table;
1784 error = zfs_sa_setup(osp, &sa_table);
1788 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1792 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1794 zfs_release_sa_handle(hdl, db, FTAG);
1799 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
1802 char *path = buf + len - 1;
1803 sa_attr_type_t *sa_table;
1810 error = zfs_sa_setup(osp, &sa_table);
1814 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1818 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
1820 zfs_release_sa_handle(hdl, db, FTAG);
1824 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1826 zfs_release_sa_handle(hdl, db, FTAG);
1830 #if defined(_KERNEL) && defined(HAVE_SPL)
1831 EXPORT_SYMBOL(zfs_create_fs);
1832 EXPORT_SYMBOL(zfs_obj_to_path);