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/zfs_ctldir.h>
56 #include <sys/dnode.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/kidmap.h>
63 #include <sys/refcount.h>
66 #include <sys/zfs_znode.h>
68 #include <sys/zfs_sa.h>
69 #include <sys/zfs_stat.h>
72 #include "zfs_comutil.h"
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.
95 static kmem_cache_t *znode_cache = NULL;
99 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
103 inode_init_once(ZTOI(zp));
104 list_link_init(&zp->z_link_node);
106 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
107 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
108 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
109 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
110 rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL);
112 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
113 avl_create(&zp->z_range_avl, zfs_range_compare,
114 sizeof (rl_t), offsetof(rl_t, r_node));
116 zp->z_dirlocks = NULL;
117 zp->z_acl_cached = NULL;
118 zp->z_xattr_cached = NULL;
119 zp->z_xattr_parent = NULL;
126 zfs_znode_cache_destructor(void *buf, void *arg)
130 ASSERT(!list_link_active(&zp->z_link_node));
131 mutex_destroy(&zp->z_lock);
132 rw_destroy(&zp->z_parent_lock);
133 rw_destroy(&zp->z_name_lock);
134 mutex_destroy(&zp->z_acl_lock);
135 rw_destroy(&zp->z_xattr_lock);
136 avl_destroy(&zp->z_range_avl);
137 mutex_destroy(&zp->z_range_lock);
139 ASSERT(zp->z_dirlocks == NULL);
140 ASSERT(zp->z_acl_cached == NULL);
141 ASSERT(zp->z_xattr_cached == NULL);
142 ASSERT(zp->z_xattr_parent == NULL);
151 ASSERT(znode_cache == NULL);
152 znode_cache = kmem_cache_create("zfs_znode_cache",
153 sizeof (znode_t), 0, zfs_znode_cache_constructor,
154 zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_KMEM);
164 kmem_cache_destroy(znode_cache);
169 zfs_create_share_dir(zfs_sb_t *zsb, dmu_tx_t *tx)
171 #ifdef HAVE_SMB_SHARE
172 zfs_acl_ids_t acl_ids;
179 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
180 vattr.va_mode = S_IFDIR | 0555;
181 vattr.va_uid = crgetuid(kcred);
182 vattr.va_gid = crgetgid(kcred);
184 sharezp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
185 sharezp->z_moved = 0;
186 sharezp->z_unlinked = 0;
187 sharezp->z_atime_dirty = 0;
188 sharezp->z_zfsvfs = zfsvfs;
189 sharezp->z_is_sa = zfsvfs->z_use_sa;
195 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
196 kcred, NULL, &acl_ids));
197 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
198 ASSERT3P(zp, ==, sharezp);
199 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
200 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
201 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
202 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
203 zfsvfs->z_shares_dir = sharezp->z_id;
205 zfs_acl_ids_free(&acl_ids);
206 // ZTOV(sharezp)->v_count = 0;
207 sa_handle_destroy(sharezp->z_sa_hdl);
208 kmem_cache_free(znode_cache, sharezp);
213 #endif /* HAVE_SMB_SHARE */
217 zfs_znode_sa_init(zfs_sb_t *zsb, znode_t *zp,
218 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
220 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb, zp->z_id)));
222 mutex_enter(&zp->z_lock);
224 ASSERT(zp->z_sa_hdl == NULL);
225 ASSERT(zp->z_acl_cached == NULL);
226 if (sa_hdl == NULL) {
227 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, zp,
228 SA_HDL_SHARED, &zp->z_sa_hdl));
230 zp->z_sa_hdl = sa_hdl;
231 sa_set_userp(sa_hdl, zp);
234 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
236 mutex_exit(&zp->z_lock);
240 zfs_znode_dmu_fini(znode_t *zp)
242 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp), zp->z_id)) ||
244 RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock));
246 sa_handle_destroy(zp->z_sa_hdl);
251 * Called by new_inode() to allocate a new inode.
254 zfs_inode_alloc(struct super_block *sb, struct inode **ip)
258 zp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
265 * Called in multiple places when an inode should be destroyed.
268 zfs_inode_destroy(struct inode *ip)
270 znode_t *zp = ITOZ(ip);
271 zfs_sb_t *zsb = ZTOZSB(zp);
273 if (zfsctl_is_node(ip))
274 zfsctl_inode_destroy(ip);
276 mutex_enter(&zsb->z_znodes_lock);
277 list_remove(&zsb->z_all_znodes, zp);
279 mutex_exit(&zsb->z_znodes_lock);
281 if (zp->z_acl_cached) {
282 zfs_acl_free(zp->z_acl_cached);
283 zp->z_acl_cached = NULL;
286 if (zp->z_xattr_cached) {
287 nvlist_free(zp->z_xattr_cached);
288 zp->z_xattr_cached = NULL;
291 if (zp->z_xattr_parent) {
292 iput(ZTOI(zp->z_xattr_parent));
293 zp->z_xattr_parent = NULL;
296 kmem_cache_free(znode_cache, zp);
300 zfs_inode_set_ops(zfs_sb_t *zsb, struct inode *ip)
304 switch (ip->i_mode & S_IFMT) {
306 ip->i_op = &zpl_inode_operations;
307 ip->i_fop = &zpl_file_operations;
308 ip->i_mapping->a_ops = &zpl_address_space_operations;
312 ip->i_op = &zpl_dir_inode_operations;
313 ip->i_fop = &zpl_dir_file_operations;
314 ITOZ(ip)->z_zn_prefetch = B_TRUE;
318 ip->i_op = &zpl_symlink_inode_operations;
322 * rdev is only stored in a SA only for device files.
326 VERIFY(sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zsb),
327 &rdev, sizeof (rdev)) == 0);
331 init_special_inode(ip, ip->i_mode, rdev);
332 ip->i_op = &zpl_special_inode_operations;
336 printk("ZFS: Invalid mode: 0x%x\n", ip->i_mode);
342 * Construct a znode+inode and initialize.
344 * This does not do a call to dmu_set_user() that is
345 * up to the caller to do, in case you don't want to
349 zfs_znode_alloc(zfs_sb_t *zsb, dmu_buf_t *db, int blksz,
350 dmu_object_type_t obj_type, uint64_t obj, sa_handle_t *hdl,
351 struct dentry *dentry, struct inode *dip)
356 sa_bulk_attr_t bulk[9];
361 ip = new_inode(zsb->z_sb);
366 ASSERT(zp->z_dirlocks == NULL);
367 ASSERT3P(zp->z_acl_cached, ==, NULL);
368 ASSERT3P(zp->z_xattr_cached, ==, NULL);
369 ASSERT3P(zp->z_xattr_parent, ==, NULL);
373 zp->z_atime_dirty = 0;
375 zp->z_id = db->db_object;
377 zp->z_seq = 0x7A4653;
379 zp->z_is_zvol = B_FALSE;
380 zp->z_is_mapped = B_FALSE;
381 zp->z_is_ctldir = B_FALSE;
383 zfs_znode_sa_init(zsb, zp, db, obj_type, hdl);
385 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &zp->z_mode, 8);
386 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, &zp->z_gen, 8);
387 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
388 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, &zp->z_links, 8);
389 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
391 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL,
393 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
395 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &zp->z_uid, 8);
396 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &zp->z_gid, 8);
398 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
400 sa_handle_destroy(zp->z_sa_hdl);
406 * xattr znodes hold a reference on their unique parent
408 if (dip && zp->z_pflags & ZFS_XATTR) {
410 zp->z_xattr_parent = ITOZ(dip);
414 zfs_inode_update(zp);
415 zfs_inode_set_ops(zsb, ip);
417 if (insert_inode_locked(ip))
421 d_instantiate(dentry, ip);
423 mutex_enter(&zsb->z_znodes_lock);
424 list_insert_tail(&zsb->z_all_znodes, zp);
427 mutex_exit(&zsb->z_znodes_lock);
429 unlock_new_inode(ip);
433 unlock_new_inode(ip);
439 * Update the embedded inode given the znode. We should work toward
440 * eliminating this function as soon as possible by removing values
441 * which are duplicated between the znode and inode. If the generic
442 * inode has the correct field it should be used, and the ZFS code
443 * updated to access the inode. This can be done incrementally.
446 zfs_inode_update(znode_t *zp)
451 uint64_t atime[2], mtime[2], ctime[2];
457 /* Skip .zfs control nodes which do not exist on disk. */
458 if (zfsctl_is_node(ip))
461 sa_lookup(zp->z_sa_hdl, SA_ZPL_ATIME(zsb), &atime, 16);
462 sa_lookup(zp->z_sa_hdl, SA_ZPL_MTIME(zsb), &mtime, 16);
463 sa_lookup(zp->z_sa_hdl, SA_ZPL_CTIME(zsb), &ctime, 16);
465 spin_lock(&ip->i_lock);
466 ip->i_generation = zp->z_gen;
467 ip->i_uid = zp->z_uid;
468 ip->i_gid = zp->z_gid;
469 set_nlink(ip, zp->z_links);
470 ip->i_mode = zp->z_mode;
471 ip->i_blkbits = SPA_MINBLOCKSHIFT;
472 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize,
473 (u_longlong_t *)&ip->i_blocks);
475 ZFS_TIME_DECODE(&ip->i_atime, atime);
476 ZFS_TIME_DECODE(&ip->i_mtime, mtime);
477 ZFS_TIME_DECODE(&ip->i_ctime, ctime);
479 i_size_write(ip, zp->z_size);
480 spin_unlock(&ip->i_lock);
483 static uint64_t empty_xattr;
484 static uint64_t pad[4];
485 static zfs_acl_phys_t acl_phys;
487 * Create a new DMU object to hold a zfs znode.
489 * IN: dzp - parent directory for new znode
490 * vap - file attributes for new znode
491 * tx - dmu transaction id for zap operations
492 * cr - credentials of caller
494 * IS_ROOT_NODE - new object will be root
495 * IS_XATTR - new object is an attribute
496 * bonuslen - length of bonus buffer
497 * setaclp - File/Dir initial ACL
498 * fuidp - Tracks fuid allocation.
500 * OUT: zpp - allocated znode
504 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
505 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
507 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
508 uint64_t mode, size, links, parent, pflags;
509 uint64_t dzp_pflags = 0;
511 zfs_sb_t *zsb = ZTOZSB(dzp);
518 dmu_object_type_t obj_type;
519 sa_bulk_attr_t *sa_attrs;
521 zfs_acl_locator_cb_t locate = { 0 };
524 obj = vap->va_nodeid;
525 now = vap->va_ctime; /* see zfs_replay_create() */
526 gen = vap->va_nblocks; /* ditto */
530 gen = dmu_tx_get_txg(tx);
533 obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
534 bonuslen = (obj_type == DMU_OT_SA) ?
535 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
538 * Create a new DMU object.
541 * There's currently no mechanism for pre-reading the blocks that will
542 * be needed to allocate a new object, so we accept the small chance
543 * that there will be an i/o error and we will fail one of the
546 if (S_ISDIR(vap->va_mode)) {
548 err = zap_create_claim_norm(zsb->z_os, obj,
549 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
550 obj_type, bonuslen, tx);
551 ASSERT3U(err, ==, 0);
553 obj = zap_create_norm(zsb->z_os,
554 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
555 obj_type, bonuslen, tx);
559 err = dmu_object_claim(zsb->z_os, obj,
560 DMU_OT_PLAIN_FILE_CONTENTS, 0,
561 obj_type, bonuslen, tx);
562 ASSERT3U(err, ==, 0);
564 obj = dmu_object_alloc(zsb->z_os,
565 DMU_OT_PLAIN_FILE_CONTENTS, 0,
566 obj_type, bonuslen, tx);
570 ZFS_OBJ_HOLD_ENTER(zsb, obj);
571 VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db));
574 * If this is the root, fix up the half-initialized parent pointer
575 * to reference the just-allocated physical data area.
577 if (flag & IS_ROOT_NODE) {
580 dzp_pflags = dzp->z_pflags;
584 * If parent is an xattr, so am I.
586 if (dzp_pflags & ZFS_XATTR) {
590 if (zsb->z_use_fuids)
591 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
595 if (S_ISDIR(vap->va_mode)) {
596 size = 2; /* contents ("." and "..") */
597 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
602 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
606 mode = acl_ids->z_mode;
611 * No execs denied will be deterimed when zfs_mode_compute() is called.
613 pflags |= acl_ids->z_aclp->z_hints &
614 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
615 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
617 ZFS_TIME_ENCODE(&now, crtime);
618 ZFS_TIME_ENCODE(&now, ctime);
620 if (vap->va_mask & ATTR_ATIME) {
621 ZFS_TIME_ENCODE(&vap->va_atime, atime);
623 ZFS_TIME_ENCODE(&now, atime);
626 if (vap->va_mask & ATTR_MTIME) {
627 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
629 ZFS_TIME_ENCODE(&now, mtime);
632 /* Now add in all of the "SA" attributes */
633 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED,
637 * Setup the array of attributes to be replaced/set on the new file
639 * order for DMU_OT_ZNODE is critical since it needs to be constructed
640 * in the old znode_phys_t format. Don't change this ordering
642 sa_attrs = kmem_alloc(sizeof(sa_bulk_attr_t) * ZPL_END, KM_PUSHPAGE);
644 if (obj_type == DMU_OT_ZNODE) {
645 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
647 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
649 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
651 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
653 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
655 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
657 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
659 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
662 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
664 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
666 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
668 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb),
669 NULL, &acl_ids->z_fuid, 8);
670 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb),
671 NULL, &acl_ids->z_fgid, 8);
672 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
674 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
676 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
678 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
680 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
682 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
686 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8);
688 if (obj_type == DMU_OT_ZNODE) {
689 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL,
692 if (obj_type == DMU_OT_ZNODE ||
693 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
694 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb),
697 if (obj_type == DMU_OT_ZNODE) {
698 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
700 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL,
701 &acl_ids->z_fuid, 8);
702 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL,
703 &acl_ids->z_fgid, 8);
704 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad,
705 sizeof (uint64_t) * 4);
706 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL,
707 &acl_phys, sizeof (zfs_acl_phys_t));
708 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
709 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL,
710 &acl_ids->z_aclp->z_acl_count, 8);
711 locate.cb_aclp = acl_ids->z_aclp;
712 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb),
713 zfs_acl_data_locator, &locate,
714 acl_ids->z_aclp->z_acl_bytes);
715 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
716 acl_ids->z_fuid, acl_ids->z_fgid);
719 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
721 if (!(flag & IS_ROOT_NODE)) {
722 *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl,
723 vap->va_dentry, ZTOI(dzp));
724 ASSERT(*zpp != NULL);
728 * If we are creating the root node, the "parent" we
729 * passed in is the znode for the root.
733 (*zpp)->z_sa_hdl = sa_hdl;
736 (*zpp)->z_pflags = pflags;
737 (*zpp)->z_mode = mode;
739 if (obj_type == DMU_OT_ZNODE ||
740 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
741 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
742 ASSERT3S(err, ==, 0);
744 kmem_free(sa_attrs, sizeof(sa_bulk_attr_t) * ZPL_END);
745 ZFS_OBJ_HOLD_EXIT(zsb, obj);
749 * zfs_xvattr_set only updates the in-core attributes
750 * it is assumed the caller will be doing an sa_bulk_update
751 * to push the changes out
754 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
758 xoap = xva_getxoptattr(xvap);
761 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
763 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
764 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
765 ×, sizeof (times), tx);
766 XVA_SET_RTN(xvap, XAT_CREATETIME);
768 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
769 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
771 XVA_SET_RTN(xvap, XAT_READONLY);
773 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
774 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
776 XVA_SET_RTN(xvap, XAT_HIDDEN);
778 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
779 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
781 XVA_SET_RTN(xvap, XAT_SYSTEM);
783 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
784 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
786 XVA_SET_RTN(xvap, XAT_ARCHIVE);
788 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
789 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
791 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
793 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
794 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
796 XVA_SET_RTN(xvap, XAT_NOUNLINK);
798 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
799 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
801 XVA_SET_RTN(xvap, XAT_APPENDONLY);
803 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
804 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
806 XVA_SET_RTN(xvap, XAT_NODUMP);
808 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
809 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
811 XVA_SET_RTN(xvap, XAT_OPAQUE);
813 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
814 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
815 xoap->xoa_av_quarantined, zp->z_pflags, tx);
816 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
818 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
819 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
821 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
823 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
824 zfs_sa_set_scanstamp(zp, xvap, tx);
825 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
827 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
828 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
830 XVA_SET_RTN(xvap, XAT_REPARSE);
832 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
833 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
835 XVA_SET_RTN(xvap, XAT_OFFLINE);
837 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
838 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
840 XVA_SET_RTN(xvap, XAT_SPARSE);
845 zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp)
847 dmu_object_info_t doi;
857 ip = ilookup(zsb->z_sb, obj_num);
859 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
861 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
863 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
868 dmu_object_info_from_db(db, &doi);
869 if (doi.doi_bonus_type != DMU_OT_SA &&
870 (doi.doi_bonus_type != DMU_OT_ZNODE ||
871 (doi.doi_bonus_type == DMU_OT_ZNODE &&
872 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
873 sa_buf_rele(db, NULL);
874 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
879 hdl = dmu_buf_get_user(db);
883 * ilookup returned NULL, which means
884 * the znode is dying - but the SA handle isn't
885 * quite dead yet, we need to drop any locks
886 * we're holding, re-schedule the task and try again.
888 sa_buf_rele(db, NULL);
889 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
895 zp = sa_get_userdata(hdl);
898 * Since "SA" does immediate eviction we
899 * should never find a sa handle that doesn't
900 * know about the znode.
903 ASSERT3P(zp, !=, NULL);
905 mutex_enter(&zp->z_lock);
906 ASSERT3U(zp->z_id, ==, obj_num);
907 if (zp->z_unlinked) {
914 sa_buf_rele(db, NULL);
915 mutex_exit(&zp->z_lock);
916 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
921 ASSERT3P(ip, ==, NULL);
924 * Not found create new znode/vnode but only if file exists.
926 * There is a small window where zfs_vget() could
927 * find this object while a file create is still in
928 * progress. This is checked for in zfs_znode_alloc()
930 * if zfs_znode_alloc() fails it will drop the hold on the
933 zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size,
934 doi.doi_bonus_type, obj_num, NULL, NULL, NULL);
940 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
945 zfs_rezget(znode_t *zp)
947 zfs_sb_t *zsb = ZTOZSB(zp);
948 dmu_object_info_t doi;
950 uint64_t obj_num = zp->z_id;
952 sa_bulk_attr_t bulk[8];
957 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
959 mutex_enter(&zp->z_acl_lock);
960 if (zp->z_acl_cached) {
961 zfs_acl_free(zp->z_acl_cached);
962 zp->z_acl_cached = NULL;
965 mutex_exit(&zp->z_acl_lock);
966 ASSERT(zp->z_sa_hdl == NULL);
967 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
969 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
973 dmu_object_info_from_db(db, &doi);
974 if (doi.doi_bonus_type != DMU_OT_SA &&
975 (doi.doi_bonus_type != DMU_OT_ZNODE ||
976 (doi.doi_bonus_type == DMU_OT_ZNODE &&
977 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
978 sa_buf_rele(db, NULL);
979 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
983 zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL);
985 /* reload cached values */
986 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL,
988 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL,
989 &zp->z_size, sizeof (zp->z_size));
990 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL,
991 &zp->z_links, sizeof (zp->z_links));
992 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
993 &zp->z_pflags, sizeof (zp->z_pflags));
994 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
995 &zp->z_atime, sizeof (zp->z_atime));
996 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
997 &zp->z_uid, sizeof (zp->z_uid));
998 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL,
999 &zp->z_gid, sizeof (zp->z_gid));
1000 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
1001 &mode, sizeof (mode));
1003 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1004 zfs_znode_dmu_fini(zp);
1005 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1011 if (gen != zp->z_gen) {
1012 zfs_znode_dmu_fini(zp);
1013 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1017 zp->z_unlinked = (zp->z_links == 0);
1018 zp->z_blksz = doi.doi_data_block_size;
1020 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1026 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1028 zfs_sb_t *zsb = ZTOZSB(zp);
1029 objset_t *os = zsb->z_os;
1030 uint64_t obj = zp->z_id;
1031 uint64_t acl_obj = zfs_external_acl(zp);
1033 ZFS_OBJ_HOLD_ENTER(zsb, obj);
1035 VERIFY(!zp->z_is_sa);
1036 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1038 VERIFY(0 == dmu_object_free(os, obj, tx));
1039 zfs_znode_dmu_fini(zp);
1040 ZFS_OBJ_HOLD_EXIT(zsb, obj);
1044 zfs_zinactive(znode_t *zp)
1046 zfs_sb_t *zsb = ZTOZSB(zp);
1047 uint64_t z_id = zp->z_id;
1048 boolean_t drop_mutex = 0;
1050 ASSERT(zp->z_sa_hdl);
1053 * Don't allow a zfs_zget() while were trying to release this znode.
1055 * Linux allows direct memory reclaim which means that any KM_SLEEP
1056 * allocation may trigger inode eviction. This can lead to a deadlock
1057 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
1058 * zfs_zinactive() call path. To avoid this deadlock the process
1059 * must not reacquire the mutex when it is already holding it.
1061 if (!ZFS_OBJ_HOLD_OWNED(zsb, z_id)) {
1062 ZFS_OBJ_HOLD_ENTER(zsb, z_id);
1066 mutex_enter(&zp->z_lock);
1069 * If this was the last reference to a file with no links,
1070 * remove the file from the file system.
1072 if (zp->z_unlinked) {
1073 mutex_exit(&zp->z_lock);
1076 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1082 mutex_exit(&zp->z_lock);
1083 zfs_znode_dmu_fini(zp);
1086 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1090 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1091 uint64_t ctime[2], boolean_t have_tx)
1097 if (have_tx) { /* will sa_bulk_update happen really soon? */
1098 zp->z_atime_dirty = 0;
1101 zp->z_atime_dirty = 1;
1104 if (flag & ATTR_ATIME) {
1105 ZFS_TIME_ENCODE(&now, zp->z_atime);
1108 if (flag & ATTR_MTIME) {
1109 ZFS_TIME_ENCODE(&now, mtime);
1110 if (ZTOZSB(zp)->z_use_fuids) {
1111 zp->z_pflags |= (ZFS_ARCHIVE |
1116 if (flag & ATTR_CTIME) {
1117 ZFS_TIME_ENCODE(&now, ctime);
1118 if (ZTOZSB(zp)->z_use_fuids)
1119 zp->z_pflags |= ZFS_ARCHIVE;
1124 * Grow the block size for a file.
1126 * IN: zp - znode of file to free data in.
1127 * size - requested block size
1128 * tx - open transaction.
1130 * NOTE: this function assumes that the znode is write locked.
1133 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1138 if (size <= zp->z_blksz)
1141 * If the file size is already greater than the current blocksize,
1142 * we will not grow. If there is more than one block in a file,
1143 * the blocksize cannot change.
1145 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1148 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
1151 if (error == ENOTSUP)
1153 ASSERT3U(error, ==, 0);
1155 /* What blocksize did we actually get? */
1156 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1160 * Increase the file length
1162 * IN: zp - znode of file to free data in.
1163 * end - new end-of-file
1165 * RETURN: 0 if success
1166 * error code if failure
1169 zfs_extend(znode_t *zp, uint64_t end)
1171 zfs_sb_t *zsb = ZTOZSB(zp);
1178 * We will change zp_size, lock the whole file.
1180 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1183 * Nothing to do if file already at desired length.
1185 if (end <= zp->z_size) {
1186 zfs_range_unlock(rl);
1190 tx = dmu_tx_create(zsb->z_os);
1191 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1192 zfs_sa_upgrade_txholds(tx, zp);
1193 if (end > zp->z_blksz &&
1194 (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) {
1196 * We are growing the file past the current block size.
1198 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
1199 ASSERT(!ISP2(zp->z_blksz));
1200 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1202 newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
1204 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1209 error = dmu_tx_assign(tx, TXG_NOWAIT);
1211 if (error == ERESTART) {
1217 zfs_range_unlock(rl);
1222 zfs_grow_blocksize(zp, newblksz, tx);
1226 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
1227 &zp->z_size, sizeof (zp->z_size), tx));
1229 zfs_range_unlock(rl);
1237 * Free space in a file.
1239 * IN: zp - znode of file to free data in.
1240 * off - start of section to free.
1241 * len - length of section to free.
1243 * RETURN: 0 if success
1244 * error code if failure
1247 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1249 zfs_sb_t *zsb = ZTOZSB(zp);
1254 * Lock the range being freed.
1256 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1259 * Nothing to do if file already at desired length.
1261 if (off >= zp->z_size) {
1262 zfs_range_unlock(rl);
1266 if (off + len > zp->z_size)
1267 len = zp->z_size - off;
1269 error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len);
1271 zfs_range_unlock(rl);
1279 * IN: zp - znode of file to free data in.
1280 * end - new end-of-file.
1282 * RETURN: 0 if success
1283 * error code if failure
1286 zfs_trunc(znode_t *zp, uint64_t end)
1288 zfs_sb_t *zsb = ZTOZSB(zp);
1292 sa_bulk_attr_t bulk[2];
1296 * We will change zp_size, lock the whole file.
1298 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1301 * Nothing to do if file already at desired length.
1303 if (end >= zp->z_size) {
1304 zfs_range_unlock(rl);
1308 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
1310 zfs_range_unlock(rl);
1314 tx = dmu_tx_create(zsb->z_os);
1315 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1316 zfs_sa_upgrade_txholds(tx, zp);
1317 error = dmu_tx_assign(tx, TXG_NOWAIT);
1319 if (error == ERESTART) {
1325 zfs_range_unlock(rl);
1330 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
1331 NULL, &zp->z_size, sizeof (zp->z_size));
1334 zp->z_pflags &= ~ZFS_SPARSE;
1335 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1336 NULL, &zp->z_pflags, 8);
1338 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1342 zfs_range_unlock(rl);
1348 * Free space in a file
1350 * IN: zp - znode of file to free data in.
1351 * off - start of range
1352 * len - end of range (0 => EOF)
1353 * flag - current file open mode flags.
1354 * log - TRUE if this action should be logged
1356 * RETURN: 0 if success
1357 * error code if failure
1360 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1362 struct inode *ip = ZTOI(zp);
1364 zfs_sb_t *zsb = ZTOZSB(zp);
1365 zilog_t *zilog = zsb->z_log;
1367 uint64_t mtime[2], ctime[2];
1368 sa_bulk_attr_t bulk[3];
1372 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
1373 sizeof (mode))) != 0)
1376 if (off > zp->z_size) {
1377 error = zfs_extend(zp, off+len);
1378 if (error == 0 && log)
1385 * Check for any locks in the region to be freed.
1387 if (ip->i_flock && mandatory_lock(ip)) {
1388 uint64_t length = (len ? len : zp->z_size - off);
1389 if (!lock_may_write(ip, off, length))
1394 error = zfs_trunc(zp, off);
1396 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1397 off + len > zp->z_size)
1398 error = zfs_extend(zp, off+len);
1403 tx = dmu_tx_create(zsb->z_os);
1404 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1405 zfs_sa_upgrade_txholds(tx, zp);
1406 error = dmu_tx_assign(tx, TXG_NOWAIT);
1408 if (error == ERESTART) {
1417 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1418 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1419 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1420 NULL, &zp->z_pflags, 8);
1421 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1422 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1425 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1428 zfs_inode_update(zp);
1433 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1435 struct super_block *sb;
1437 uint64_t moid, obj, sa_obj, version;
1438 uint64_t sense = ZFS_CASE_SENSITIVE;
1443 znode_t *rootzp = NULL;
1446 zfs_acl_ids_t acl_ids;
1449 * First attempt to create master node.
1452 * In an empty objset, there are no blocks to read and thus
1453 * there can be no i/o errors (which we assert below).
1455 moid = MASTER_NODE_OBJ;
1456 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1457 DMU_OT_NONE, 0, tx);
1461 * Set starting attributes.
1463 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1465 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1466 /* For the moment we expect all zpl props to be uint64_ts */
1470 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1471 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1472 name = nvpair_name(elem);
1473 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1477 error = zap_update(os, moid, name, 8, 1, &val, tx);
1480 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1482 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1485 ASSERT(version != 0);
1486 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1489 * Create zap object used for SA attribute registration
1492 if (version >= ZPL_VERSION_SA) {
1493 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1494 DMU_OT_NONE, 0, tx);
1495 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1501 * Create a delete queue.
1503 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1505 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1509 * Create root znode. Create minimal znode/inode/zsb/sb
1510 * to allow zfs_mknode to work.
1512 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1513 vattr.va_mode = S_IFDIR|0755;
1514 vattr.va_uid = crgetuid(cr);
1515 vattr.va_gid = crgetgid(cr);
1517 rootzp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
1518 rootzp->z_moved = 0;
1519 rootzp->z_unlinked = 0;
1520 rootzp->z_atime_dirty = 0;
1521 rootzp->z_is_sa = USE_SA(version, os);
1523 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_PUSHPAGE | KM_NODEBUG);
1525 zsb->z_parent = zsb;
1526 zsb->z_version = version;
1527 zsb->z_use_fuids = USE_FUIDS(version, os);
1528 zsb->z_use_sa = USE_SA(version, os);
1531 sb = kmem_zalloc(sizeof (struct super_block), KM_PUSHPAGE);
1532 sb->s_fs_info = zsb;
1534 ZTOI(rootzp)->i_sb = sb;
1536 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1537 &zsb->z_attr_table);
1542 * Fold case on file systems that are always or sometimes case
1545 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1546 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
1548 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1549 list_create(&zsb->z_all_znodes, sizeof (znode_t),
1550 offsetof(znode_t, z_link_node));
1552 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1553 mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1555 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1556 cr, NULL, &acl_ids));
1557 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1558 ASSERT3P(zp, ==, rootzp);
1559 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1561 zfs_acl_ids_free(&acl_ids);
1563 atomic_set(&ZTOI(rootzp)->i_count, 0);
1564 sa_handle_destroy(rootzp->z_sa_hdl);
1565 kmem_cache_free(znode_cache, rootzp);
1568 * Create shares directory
1570 error = zfs_create_share_dir(zsb, tx);
1573 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1574 mutex_destroy(&zsb->z_hold_mtx[i]);
1576 kmem_free(sb, sizeof (struct super_block));
1577 kmem_free(zsb, sizeof (zfs_sb_t));
1579 #endif /* _KERNEL */
1582 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1584 uint64_t sa_obj = 0;
1587 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1588 if (error != 0 && error != ENOENT)
1591 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1596 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1597 dmu_buf_t **db, void *tag)
1599 dmu_object_info_t doi;
1602 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1605 dmu_object_info_from_db(*db, &doi);
1606 if ((doi.doi_bonus_type != DMU_OT_SA &&
1607 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1608 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1609 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1610 sa_buf_rele(*db, tag);
1614 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1616 sa_buf_rele(*db, tag);
1624 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1626 sa_handle_destroy(hdl);
1627 sa_buf_rele(db, tag);
1631 * Given an object number, return its parent object number and whether
1632 * or not the object is an extended attribute directory.
1635 zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
1641 sa_bulk_attr_t bulk[3];
1645 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1646 &parent, sizeof (parent));
1647 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1648 &pflags, sizeof (pflags));
1649 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1650 &mode, sizeof (mode));
1652 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1656 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1662 * Given an object number, return some zpl level statistics
1665 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1668 sa_bulk_attr_t bulk[4];
1671 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1672 &sb->zs_mode, sizeof (sb->zs_mode));
1673 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1674 &sb->zs_gen, sizeof (sb->zs_gen));
1675 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1676 &sb->zs_links, sizeof (sb->zs_links));
1677 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1678 &sb->zs_ctime, sizeof (sb->zs_ctime));
1680 return (sa_bulk_lookup(hdl, bulk, count));
1684 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1685 sa_attr_type_t *sa_table, char *buf, int len)
1687 sa_handle_t *sa_hdl;
1688 sa_handle_t *prevhdl = NULL;
1689 dmu_buf_t *prevdb = NULL;
1690 dmu_buf_t *sa_db = NULL;
1691 char *path = buf + len - 1;
1699 char component[MAXNAMELEN + 2];
1704 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
1706 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
1707 &is_xattrdir)) != 0)
1718 (void) sprintf(component + 1, "<xattrdir>");
1720 error = zap_value_search(osp, pobj, obj,
1721 ZFS_DIRENT_OBJ(-1ULL), component + 1);
1726 complen = strlen(component);
1728 ASSERT(path >= buf);
1729 bcopy(component, path, complen);
1732 if (sa_hdl != hdl) {
1736 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
1744 if (sa_hdl != NULL && sa_hdl != hdl) {
1745 ASSERT(sa_db != NULL);
1746 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1750 (void) memmove(buf, path, buf + len - path);
1756 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1758 sa_attr_type_t *sa_table;
1763 error = zfs_sa_setup(osp, &sa_table);
1767 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1771 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1773 zfs_release_sa_handle(hdl, db, FTAG);
1778 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
1781 char *path = buf + len - 1;
1782 sa_attr_type_t *sa_table;
1789 error = zfs_sa_setup(osp, &sa_table);
1793 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1797 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
1799 zfs_release_sa_handle(hdl, db, FTAG);
1803 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1805 zfs_release_sa_handle(hdl, db, FTAG);
1809 #if defined(_KERNEL) && defined(HAVE_SPL)
1810 EXPORT_SYMBOL(zfs_create_fs);
1811 EXPORT_SYMBOL(zfs_obj_to_path);