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
);
109 rw_init(&zp
->z_xattr_lock
, NULL
, RW_DEFAULT
, NULL
);
111 mutex_init(&zp
->z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
112 avl_create(&zp
->z_range_avl
, zfs_range_compare
,
113 sizeof (rl_t
), offsetof(rl_t
, r_node
));
115 zp
->z_dirlocks
= NULL
;
116 zp
->z_acl_cached
= NULL
;
117 zp
->z_xattr_cached
= NULL
;
124 zfs_znode_cache_destructor(void *buf
, void *arg
)
128 ASSERT(!list_link_active(&zp
->z_link_node
));
129 mutex_destroy(&zp
->z_lock
);
130 rw_destroy(&zp
->z_parent_lock
);
131 rw_destroy(&zp
->z_name_lock
);
132 mutex_destroy(&zp
->z_acl_lock
);
133 rw_destroy(&zp
->z_xattr_lock
);
134 avl_destroy(&zp
->z_range_avl
);
135 mutex_destroy(&zp
->z_range_lock
);
137 ASSERT(zp
->z_dirlocks
== NULL
);
138 ASSERT(zp
->z_acl_cached
== NULL
);
139 ASSERT(zp
->z_xattr_cached
== NULL
);
148 ASSERT(znode_cache
== NULL
);
149 znode_cache
= kmem_cache_create("zfs_znode_cache",
150 sizeof (znode_t
), 0, zfs_znode_cache_constructor
,
151 zfs_znode_cache_destructor
, NULL
, NULL
, NULL
, KMC_KMEM
);
161 kmem_cache_destroy(znode_cache
);
166 zfs_create_share_dir(zfs_sb_t
*zsb
, dmu_tx_t
*tx
)
168 #ifdef HAVE_SMB_SHARE
169 zfs_acl_ids_t acl_ids
;
176 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
177 vattr
.va_mode
= S_IFDIR
| 0555;
178 vattr
.va_uid
= crgetuid(kcred
);
179 vattr
.va_gid
= crgetgid(kcred
);
181 sharezp
= kmem_cache_alloc(znode_cache
, KM_PUSHPAGE
);
182 sharezp
->z_moved
= 0;
183 sharezp
->z_unlinked
= 0;
184 sharezp
->z_atime_dirty
= 0;
185 sharezp
->z_zfsvfs
= zfsvfs
;
186 sharezp
->z_is_sa
= zfsvfs
->z_use_sa
;
192 VERIFY(0 == zfs_acl_ids_create(sharezp
, IS_ROOT_NODE
, &vattr
,
193 kcred
, NULL
, &acl_ids
));
194 zfs_mknode(sharezp
, &vattr
, tx
, kcred
, IS_ROOT_NODE
, &zp
, &acl_ids
);
195 ASSERT3P(zp
, ==, sharezp
);
196 ASSERT(!vn_in_dnlc(ZTOV(sharezp
))); /* not valid to move */
197 POINTER_INVALIDATE(&sharezp
->z_zfsvfs
);
198 error
= zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
199 ZFS_SHARES_DIR
, 8, 1, &sharezp
->z_id
, tx
);
200 zfsvfs
->z_shares_dir
= sharezp
->z_id
;
202 zfs_acl_ids_free(&acl_ids
);
203 // ZTOV(sharezp)->v_count = 0;
204 sa_handle_destroy(sharezp
->z_sa_hdl
);
205 kmem_cache_free(znode_cache
, sharezp
);
210 #endif /* HAVE_SMB_SHARE */
214 zfs_znode_sa_init(zfs_sb_t
*zsb
, znode_t
*zp
,
215 dmu_buf_t
*db
, dmu_object_type_t obj_type
, sa_handle_t
*sa_hdl
)
217 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb
, zp
->z_id
)));
219 mutex_enter(&zp
->z_lock
);
221 ASSERT(zp
->z_sa_hdl
== NULL
);
222 ASSERT(zp
->z_acl_cached
== NULL
);
223 if (sa_hdl
== NULL
) {
224 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, zp
,
225 SA_HDL_SHARED
, &zp
->z_sa_hdl
));
227 zp
->z_sa_hdl
= sa_hdl
;
228 sa_set_userp(sa_hdl
, zp
);
231 zp
->z_is_sa
= (obj_type
== DMU_OT_SA
) ? B_TRUE
: B_FALSE
;
233 mutex_exit(&zp
->z_lock
);
237 zfs_znode_dmu_fini(znode_t
*zp
)
239 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp
), zp
->z_id
)) ||
241 RW_WRITE_HELD(&ZTOZSB(zp
)->z_teardown_inactive_lock
));
243 sa_handle_destroy(zp
->z_sa_hdl
);
248 * Called by new_inode() to allocate a new inode.
251 zfs_inode_alloc(struct super_block
*sb
, struct inode
**ip
)
255 zp
= kmem_cache_alloc(znode_cache
, KM_PUSHPAGE
);
262 * Called in multiple places when an inode should be destroyed.
265 zfs_inode_destroy(struct inode
*ip
)
267 znode_t
*zp
= ITOZ(ip
);
268 zfs_sb_t
*zsb
= ZTOZSB(zp
);
270 mutex_enter(&zsb
->z_znodes_lock
);
271 list_remove(&zsb
->z_all_znodes
, zp
);
272 mutex_exit(&zsb
->z_znodes_lock
);
274 if (zp
->z_acl_cached
) {
275 zfs_acl_free(zp
->z_acl_cached
);
276 zp
->z_acl_cached
= NULL
;
279 if (zp
->z_xattr_cached
) {
280 nvlist_free(zp
->z_xattr_cached
);
281 zp
->z_xattr_cached
= NULL
;
284 kmem_cache_free(znode_cache
, zp
);
288 zfs_inode_set_ops(zfs_sb_t
*zsb
, struct inode
*ip
)
292 switch (ip
->i_mode
& S_IFMT
) {
294 ip
->i_op
= &zpl_inode_operations
;
295 ip
->i_fop
= &zpl_file_operations
;
296 ip
->i_mapping
->a_ops
= &zpl_address_space_operations
;
300 ip
->i_op
= &zpl_dir_inode_operations
;
301 ip
->i_fop
= &zpl_dir_file_operations
;
302 ITOZ(ip
)->z_zn_prefetch
= B_TRUE
;
306 ip
->i_op
= &zpl_symlink_inode_operations
;
310 * rdev is only stored in a SA only for device files.
314 VERIFY(sa_lookup(ITOZ(ip
)->z_sa_hdl
, SA_ZPL_RDEV(zsb
),
315 &rdev
, sizeof (rdev
)) == 0);
319 init_special_inode(ip
, ip
->i_mode
, rdev
);
320 ip
->i_op
= &zpl_special_inode_operations
;
324 printk("ZFS: Invalid mode: 0x%x\n", ip
->i_mode
);
330 * Construct a znode+inode and initialize.
332 * This does not do a call to dmu_set_user() that is
333 * up to the caller to do, in case you don't want to
337 zfs_znode_alloc(zfs_sb_t
*zsb
, dmu_buf_t
*db
, int blksz
,
338 dmu_object_type_t obj_type
, uint64_t obj
, sa_handle_t
*hdl
,
339 struct dentry
*dentry
, struct inode
*dip
)
344 sa_bulk_attr_t bulk
[9];
349 ip
= new_inode(zsb
->z_sb
);
354 ASSERT(zp
->z_dirlocks
== NULL
);
358 zp
->z_atime_dirty
= 0;
360 zp
->z_id
= db
->db_object
;
362 zp
->z_seq
= 0x7A4653;
366 zfs_znode_sa_init(zsb
, zp
, db
, obj_type
, hdl
);
368 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
, &zp
->z_mode
, 8);
369 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
, &zp
->z_gen
, 8);
370 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
371 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
, &zp
->z_links
, 8);
372 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
374 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zsb
), NULL
,
376 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
378 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
, &zp
->z_uid
, 8);
379 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
, &zp
->z_gid
, 8);
381 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
) != 0 || zp
->z_gen
== 0) {
383 sa_handle_destroy(zp
->z_sa_hdl
);
389 zfs_inode_update(zp
);
390 zfs_inode_set_ops(zsb
, ip
);
392 if (insert_inode_locked(ip
))
396 if (zpl_xattr_security_init(ip
, dip
, &dentry
->d_name
))
399 d_instantiate(dentry
, ip
);
402 mutex_enter(&zsb
->z_znodes_lock
);
403 list_insert_tail(&zsb
->z_all_znodes
, zp
);
405 mutex_exit(&zsb
->z_znodes_lock
);
407 unlock_new_inode(ip
);
411 unlock_new_inode(ip
);
417 * Update the embedded inode given the znode. We should work toward
418 * eliminating this function as soon as possible by removing values
419 * which are duplicated between the znode and inode. If the generic
420 * inode has the correct field it should be used, and the ZFS code
421 * updated to access the inode. This can be done incrementally.
424 zfs_inode_update(znode_t
*zp
)
429 uint64_t atime
[2], mtime
[2], ctime
[2];
435 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
), &atime
, 16);
436 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MTIME(zsb
), &mtime
, 16);
437 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CTIME(zsb
), &ctime
, 16);
439 spin_lock(&ip
->i_lock
);
440 ip
->i_generation
= zp
->z_gen
;
441 ip
->i_uid
= zp
->z_uid
;
442 ip
->i_gid
= zp
->z_gid
;
443 set_nlink(ip
, zp
->z_links
);
444 ip
->i_mode
= zp
->z_mode
;
445 ip
->i_blkbits
= SPA_MINBLOCKSHIFT
;
446 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &blksize
,
447 (u_longlong_t
*)&ip
->i_blocks
);
449 ZFS_TIME_DECODE(&ip
->i_atime
, atime
);
450 ZFS_TIME_DECODE(&ip
->i_mtime
, mtime
);
451 ZFS_TIME_DECODE(&ip
->i_ctime
, ctime
);
453 i_size_write(ip
, zp
->z_size
);
454 spin_unlock(&ip
->i_lock
);
457 static uint64_t empty_xattr
;
458 static uint64_t pad
[4];
459 static zfs_acl_phys_t acl_phys
;
461 * Create a new DMU object to hold a zfs znode.
463 * IN: dzp - parent directory for new znode
464 * vap - file attributes for new znode
465 * tx - dmu transaction id for zap operations
466 * cr - credentials of caller
468 * IS_ROOT_NODE - new object will be root
469 * IS_XATTR - new object is an attribute
470 * bonuslen - length of bonus buffer
471 * setaclp - File/Dir initial ACL
472 * fuidp - Tracks fuid allocation.
474 * OUT: zpp - allocated znode
478 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
479 uint_t flag
, znode_t
**zpp
, zfs_acl_ids_t
*acl_ids
)
481 uint64_t crtime
[2], atime
[2], mtime
[2], ctime
[2];
482 uint64_t mode
, size
, links
, parent
, pflags
;
483 uint64_t dzp_pflags
= 0;
485 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
492 dmu_object_type_t obj_type
;
493 sa_bulk_attr_t
*sa_attrs
;
495 zfs_acl_locator_cb_t locate
= { 0 };
498 obj
= vap
->va_nodeid
;
499 now
= vap
->va_ctime
; /* see zfs_replay_create() */
500 gen
= vap
->va_nblocks
; /* ditto */
504 gen
= dmu_tx_get_txg(tx
);
507 obj_type
= zsb
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
508 bonuslen
= (obj_type
== DMU_OT_SA
) ?
509 DN_MAX_BONUSLEN
: ZFS_OLD_ZNODE_PHYS_SIZE
;
512 * Create a new DMU object.
515 * There's currently no mechanism for pre-reading the blocks that will
516 * be needed to allocate a new object, so we accept the small chance
517 * that there will be an i/o error and we will fail one of the
520 if (S_ISDIR(vap
->va_mode
)) {
522 err
= zap_create_claim_norm(zsb
->z_os
, obj
,
523 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
524 obj_type
, bonuslen
, tx
);
525 ASSERT3U(err
, ==, 0);
527 obj
= zap_create_norm(zsb
->z_os
,
528 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
529 obj_type
, bonuslen
, tx
);
533 err
= dmu_object_claim(zsb
->z_os
, obj
,
534 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
535 obj_type
, bonuslen
, tx
);
536 ASSERT3U(err
, ==, 0);
538 obj
= dmu_object_alloc(zsb
->z_os
,
539 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
540 obj_type
, bonuslen
, tx
);
544 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
545 VERIFY(0 == sa_buf_hold(zsb
->z_os
, obj
, NULL
, &db
));
548 * If this is the root, fix up the half-initialized parent pointer
549 * to reference the just-allocated physical data area.
551 if (flag
& IS_ROOT_NODE
) {
554 dzp_pflags
= dzp
->z_pflags
;
558 * If parent is an xattr, so am I.
560 if (dzp_pflags
& ZFS_XATTR
) {
564 if (zsb
->z_use_fuids
)
565 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
569 if (S_ISDIR(vap
->va_mode
)) {
570 size
= 2; /* contents ("." and "..") */
571 links
= (flag
& (IS_ROOT_NODE
| IS_XATTR
)) ? 2 : 1;
576 if (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))
580 mode
= acl_ids
->z_mode
;
585 * No execs denied will be deterimed when zfs_mode_compute() is called.
587 pflags
|= acl_ids
->z_aclp
->z_hints
&
588 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
589 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
591 ZFS_TIME_ENCODE(&now
, crtime
);
592 ZFS_TIME_ENCODE(&now
, ctime
);
594 if (vap
->va_mask
& ATTR_ATIME
) {
595 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
597 ZFS_TIME_ENCODE(&now
, atime
);
600 if (vap
->va_mask
& ATTR_MTIME
) {
601 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
603 ZFS_TIME_ENCODE(&now
, mtime
);
606 /* Now add in all of the "SA" attributes */
607 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, NULL
, SA_HDL_SHARED
,
611 * Setup the array of attributes to be replaced/set on the new file
613 * order for DMU_OT_ZNODE is critical since it needs to be constructed
614 * in the old znode_phys_t format. Don't change this ordering
616 sa_attrs
= kmem_alloc(sizeof(sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
618 if (obj_type
== DMU_OT_ZNODE
) {
619 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
621 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
623 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
625 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
627 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
629 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
631 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
633 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
636 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
638 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
640 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
642 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
),
643 NULL
, &acl_ids
->z_fuid
, 8);
644 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
),
645 NULL
, &acl_ids
->z_fgid
, 8);
646 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
648 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
650 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
652 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
654 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
656 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
660 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zsb
), NULL
, &links
, 8);
662 if (obj_type
== DMU_OT_ZNODE
) {
663 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zsb
), NULL
,
666 if (obj_type
== DMU_OT_ZNODE
||
667 (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))) {
668 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zsb
),
671 if (obj_type
== DMU_OT_ZNODE
) {
672 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
674 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
), NULL
,
675 &acl_ids
->z_fuid
, 8);
676 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
), NULL
,
677 &acl_ids
->z_fgid
, 8);
678 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zsb
), NULL
, pad
,
679 sizeof (uint64_t) * 4);
680 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zsb
), NULL
,
681 &acl_phys
, sizeof (zfs_acl_phys_t
));
682 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
683 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zsb
), NULL
,
684 &acl_ids
->z_aclp
->z_acl_count
, 8);
685 locate
.cb_aclp
= acl_ids
->z_aclp
;
686 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zsb
),
687 zfs_acl_data_locator
, &locate
,
688 acl_ids
->z_aclp
->z_acl_bytes
);
689 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
690 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
693 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
695 if (!(flag
& IS_ROOT_NODE
)) {
696 *zpp
= zfs_znode_alloc(zsb
, db
, 0, obj_type
, obj
, sa_hdl
,
697 vap
->va_dentry
, ZTOI(dzp
));
698 ASSERT(*zpp
!= NULL
);
702 * If we are creating the root node, the "parent" we
703 * passed in is the znode for the root.
707 (*zpp
)->z_sa_hdl
= sa_hdl
;
710 (*zpp
)->z_pflags
= pflags
;
711 (*zpp
)->z_mode
= mode
;
713 if (obj_type
== DMU_OT_ZNODE
||
714 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
715 err
= zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
);
716 ASSERT3S(err
, ==, 0);
718 kmem_free(sa_attrs
, sizeof(sa_bulk_attr_t
) * ZPL_END
);
719 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
723 * zfs_xvattr_set only updates the in-core attributes
724 * it is assumed the caller will be doing an sa_bulk_update
725 * to push the changes out
728 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
732 xoap
= xva_getxoptattr(xvap
);
735 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
737 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, times
);
738 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_CRTIME(ZTOZSB(zp
)),
739 ×
, sizeof (times
), tx
);
740 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
742 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
743 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
,
745 XVA_SET_RTN(xvap
, XAT_READONLY
);
747 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
748 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
,
750 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
752 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
753 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
,
755 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
757 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
758 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
,
760 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
762 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
763 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
,
765 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
767 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
768 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
,
770 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
772 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
773 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
,
775 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
777 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
778 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
,
780 XVA_SET_RTN(xvap
, XAT_NODUMP
);
782 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
783 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
,
785 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
787 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
788 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
789 xoap
->xoa_av_quarantined
, zp
->z_pflags
, tx
);
790 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
792 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
793 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
,
795 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
797 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
798 zfs_sa_set_scanstamp(zp
, xvap
, tx
);
799 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
801 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
802 ZFS_ATTR_SET(zp
, ZFS_REPARSE
, xoap
->xoa_reparse
,
804 XVA_SET_RTN(xvap
, XAT_REPARSE
);
806 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
807 ZFS_ATTR_SET(zp
, ZFS_OFFLINE
, xoap
->xoa_offline
,
809 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
811 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
812 ZFS_ATTR_SET(zp
, ZFS_SPARSE
, xoap
->xoa_sparse
,
814 XVA_SET_RTN(xvap
, XAT_SPARSE
);
819 zfs_zget(zfs_sb_t
*zsb
, uint64_t obj_num
, znode_t
**zpp
)
821 dmu_object_info_t doi
;
831 ip
= ilookup(zsb
->z_sb
, obj_num
);
833 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
835 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
837 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
842 dmu_object_info_from_db(db
, &doi
);
843 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
844 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
845 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
846 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
847 sa_buf_rele(db
, NULL
);
848 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
853 hdl
= dmu_buf_get_user(db
);
857 * ilookup returned NULL, which means
858 * the znode is dying - but the SA handle isn't
859 * quite dead yet, we need to drop any locks
860 * we're holding, re-schedule the task and try again.
862 sa_buf_rele(db
, NULL
);
863 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
869 zp
= sa_get_userdata(hdl
);
872 * Since "SA" does immediate eviction we
873 * should never find a sa handle that doesn't
874 * know about the znode.
877 ASSERT3P(zp
, !=, NULL
);
879 mutex_enter(&zp
->z_lock
);
880 ASSERT3U(zp
->z_id
, ==, obj_num
);
881 if (zp
->z_unlinked
) {
888 sa_buf_rele(db
, NULL
);
889 mutex_exit(&zp
->z_lock
);
890 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
895 ASSERT3P(ip
, ==, NULL
);
898 * Not found create new znode/vnode but only if file exists.
900 * There is a small window where zfs_vget() could
901 * find this object while a file create is still in
902 * progress. This is checked for in zfs_znode_alloc()
904 * if zfs_znode_alloc() fails it will drop the hold on the
907 zp
= zfs_znode_alloc(zsb
, db
, doi
.doi_data_block_size
,
908 doi
.doi_bonus_type
, obj_num
, NULL
, NULL
, NULL
);
914 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
919 zfs_rezget(znode_t
*zp
)
921 zfs_sb_t
*zsb
= ZTOZSB(zp
);
922 dmu_object_info_t doi
;
924 uint64_t obj_num
= zp
->z_id
;
926 sa_bulk_attr_t bulk
[8];
931 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
933 mutex_enter(&zp
->z_acl_lock
);
934 if (zp
->z_acl_cached
) {
935 zfs_acl_free(zp
->z_acl_cached
);
936 zp
->z_acl_cached
= NULL
;
939 mutex_exit(&zp
->z_acl_lock
);
940 ASSERT(zp
->z_sa_hdl
== NULL
);
941 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
943 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
947 dmu_object_info_from_db(db
, &doi
);
948 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
949 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
950 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
951 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
952 sa_buf_rele(db
, NULL
);
953 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
957 zfs_znode_sa_init(zsb
, zp
, db
, doi
.doi_bonus_type
, NULL
);
959 /* reload cached values */
960 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
,
962 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
,
963 &zp
->z_size
, sizeof (zp
->z_size
));
964 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
,
965 &zp
->z_links
, sizeof (zp
->z_links
));
966 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
967 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
968 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
969 &zp
->z_atime
, sizeof (zp
->z_atime
));
970 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
971 &zp
->z_uid
, sizeof (zp
->z_uid
));
972 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
,
973 &zp
->z_gid
, sizeof (zp
->z_gid
));
974 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
975 &mode
, sizeof (mode
));
977 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
978 zfs_znode_dmu_fini(zp
);
979 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
985 if (gen
!= zp
->z_gen
) {
986 zfs_znode_dmu_fini(zp
);
987 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
991 zp
->z_unlinked
= (zp
->z_links
== 0);
992 zp
->z_blksz
= doi
.doi_data_block_size
;
994 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
1000 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
1002 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1003 objset_t
*os
= zsb
->z_os
;
1004 uint64_t obj
= zp
->z_id
;
1005 uint64_t acl_obj
= zfs_external_acl(zp
);
1007 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
1009 VERIFY(!zp
->z_is_sa
);
1010 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1012 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1013 zfs_znode_dmu_fini(zp
);
1014 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
1018 zfs_zinactive(znode_t
*zp
)
1020 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1021 uint64_t z_id
= zp
->z_id
;
1022 boolean_t drop_mutex
= 0;
1024 ASSERT(zp
->z_sa_hdl
);
1027 * Don't allow a zfs_zget() while were trying to release this znode.
1029 * Linux allows direct memory reclaim which means that any KM_SLEEP
1030 * allocation may trigger inode eviction. This can lead to a deadlock
1031 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
1032 * zfs_zinactive() call path. To avoid this deadlock the process
1033 * must not reacquire the mutex when it is already holding it.
1035 if (!ZFS_OBJ_HOLD_OWNED(zsb
, z_id
)) {
1036 ZFS_OBJ_HOLD_ENTER(zsb
, z_id
);
1040 mutex_enter(&zp
->z_lock
);
1043 * If this was the last reference to a file with no links,
1044 * remove the file from the file system.
1046 if (zp
->z_unlinked
) {
1047 mutex_exit(&zp
->z_lock
);
1050 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
1056 mutex_exit(&zp
->z_lock
);
1057 zfs_znode_dmu_fini(zp
);
1060 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
1064 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1065 uint64_t ctime
[2], boolean_t have_tx
)
1071 if (have_tx
) { /* will sa_bulk_update happen really soon? */
1072 zp
->z_atime_dirty
= 0;
1075 zp
->z_atime_dirty
= 1;
1078 if (flag
& ATTR_ATIME
) {
1079 ZFS_TIME_ENCODE(&now
, zp
->z_atime
);
1082 if (flag
& ATTR_MTIME
) {
1083 ZFS_TIME_ENCODE(&now
, mtime
);
1084 if (ZTOZSB(zp
)->z_use_fuids
) {
1085 zp
->z_pflags
|= (ZFS_ARCHIVE
|
1090 if (flag
& ATTR_CTIME
) {
1091 ZFS_TIME_ENCODE(&now
, ctime
);
1092 if (ZTOZSB(zp
)->z_use_fuids
)
1093 zp
->z_pflags
|= ZFS_ARCHIVE
;
1098 * Grow the block size for a file.
1100 * IN: zp - znode of file to free data in.
1101 * size - requested block size
1102 * tx - open transaction.
1104 * NOTE: this function assumes that the znode is write locked.
1107 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1112 if (size
<= zp
->z_blksz
)
1115 * If the file size is already greater than the current blocksize,
1116 * we will not grow. If there is more than one block in a file,
1117 * the blocksize cannot change.
1119 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1122 error
= dmu_object_set_blocksize(ZTOZSB(zp
)->z_os
, zp
->z_id
,
1125 if (error
== ENOTSUP
)
1127 ASSERT3U(error
, ==, 0);
1129 /* What blocksize did we actually get? */
1130 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1134 * Increase the file length
1136 * IN: zp - znode of file to free data in.
1137 * end - new end-of-file
1139 * RETURN: 0 if success
1140 * error code if failure
1143 zfs_extend(znode_t
*zp
, uint64_t end
)
1145 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1152 * We will change zp_size, lock the whole file.
1154 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1157 * Nothing to do if file already at desired length.
1159 if (end
<= zp
->z_size
) {
1160 zfs_range_unlock(rl
);
1164 tx
= dmu_tx_create(zsb
->z_os
);
1165 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1166 zfs_sa_upgrade_txholds(tx
, zp
);
1167 if (end
> zp
->z_blksz
&&
1168 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zsb
->z_max_blksz
)) {
1170 * We are growing the file past the current block size.
1172 if (zp
->z_blksz
> ZTOZSB(zp
)->z_max_blksz
) {
1173 ASSERT(!ISP2(zp
->z_blksz
));
1174 newblksz
= MIN(end
, SPA_MAXBLOCKSIZE
);
1176 newblksz
= MIN(end
, ZTOZSB(zp
)->z_max_blksz
);
1178 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1183 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1185 if (error
== ERESTART
) {
1191 zfs_range_unlock(rl
);
1196 zfs_grow_blocksize(zp
, newblksz
, tx
);
1200 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(ZTOZSB(zp
)),
1201 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1203 zfs_range_unlock(rl
);
1211 * Free space in a file.
1213 * IN: zp - znode of file to free data in.
1214 * off - start of section to free.
1215 * len - length of section to free.
1217 * RETURN: 0 if success
1218 * error code if failure
1221 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1223 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1228 * Lock the range being freed.
1230 rl
= zfs_range_lock(zp
, off
, len
, RL_WRITER
);
1233 * Nothing to do if file already at desired length.
1235 if (off
>= zp
->z_size
) {
1236 zfs_range_unlock(rl
);
1240 if (off
+ len
> zp
->z_size
)
1241 len
= zp
->z_size
- off
;
1243 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, off
, len
);
1245 zfs_range_unlock(rl
);
1253 * IN: zp - znode of file to free data in.
1254 * end - new end-of-file.
1256 * RETURN: 0 if success
1257 * error code if failure
1260 zfs_trunc(znode_t
*zp
, uint64_t end
)
1262 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1266 sa_bulk_attr_t bulk
[2];
1270 * We will change zp_size, lock the whole file.
1272 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1275 * Nothing to do if file already at desired length.
1277 if (end
>= zp
->z_size
) {
1278 zfs_range_unlock(rl
);
1282 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, end
, -1);
1284 zfs_range_unlock(rl
);
1288 tx
= dmu_tx_create(zsb
->z_os
);
1289 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1290 zfs_sa_upgrade_txholds(tx
, zp
);
1291 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1293 if (error
== ERESTART
) {
1299 zfs_range_unlock(rl
);
1304 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
),
1305 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1308 zp
->z_pflags
&= ~ZFS_SPARSE
;
1309 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1310 NULL
, &zp
->z_pflags
, 8);
1312 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1316 zfs_range_unlock(rl
);
1322 * Free space in a file
1324 * IN: zp - znode of file to free data in.
1325 * off - start of range
1326 * len - end of range (0 => EOF)
1327 * flag - current file open mode flags.
1328 * log - TRUE if this action should be logged
1330 * RETURN: 0 if success
1331 * error code if failure
1334 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1336 struct inode
*ip
= ZTOI(zp
);
1338 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1339 zilog_t
*zilog
= zsb
->z_log
;
1341 uint64_t mtime
[2], ctime
[2];
1342 sa_bulk_attr_t bulk
[3];
1346 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
), &mode
,
1347 sizeof (mode
))) != 0)
1350 if (off
> zp
->z_size
) {
1351 error
= zfs_extend(zp
, off
+len
);
1352 if (error
== 0 && log
)
1359 * Check for any locks in the region to be freed.
1361 if (ip
->i_flock
&& mandatory_lock(ip
)) {
1362 uint64_t length
= (len
? len
: zp
->z_size
- off
);
1363 if (!lock_may_write(ip
, off
, length
))
1368 error
= zfs_trunc(zp
, off
);
1370 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1371 off
+ len
> zp
->z_size
)
1372 error
= zfs_extend(zp
, off
+len
);
1377 tx
= dmu_tx_create(zsb
->z_os
);
1378 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1379 zfs_sa_upgrade_txholds(tx
, zp
);
1380 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1382 if (error
== ERESTART
) {
1391 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, mtime
, 16);
1392 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, ctime
, 16);
1393 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1394 NULL
, &zp
->z_pflags
, 8);
1395 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
, B_TRUE
);
1396 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1399 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1402 zfs_inode_update(zp
);
1407 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1409 struct super_block
*sb
;
1411 uint64_t moid
, obj
, sa_obj
, version
;
1412 uint64_t sense
= ZFS_CASE_SENSITIVE
;
1417 znode_t
*rootzp
= NULL
;
1420 zfs_acl_ids_t acl_ids
;
1423 * First attempt to create master node.
1426 * In an empty objset, there are no blocks to read and thus
1427 * there can be no i/o errors (which we assert below).
1429 moid
= MASTER_NODE_OBJ
;
1430 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1431 DMU_OT_NONE
, 0, tx
);
1435 * Set starting attributes.
1437 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1439 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1440 /* For the moment we expect all zpl props to be uint64_ts */
1444 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1445 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1446 name
= nvpair_name(elem
);
1447 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1451 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1454 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1456 else if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_CASE
)) == 0)
1459 ASSERT(version
!= 0);
1460 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1463 * Create zap object used for SA attribute registration
1466 if (version
>= ZPL_VERSION_SA
) {
1467 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1468 DMU_OT_NONE
, 0, tx
);
1469 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1475 * Create a delete queue.
1477 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1479 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1483 * Create root znode. Create minimal znode/inode/zsb/sb
1484 * to allow zfs_mknode to work.
1486 vattr
.va_mask
= ATTR_MODE
|ATTR_UID
|ATTR_GID
;
1487 vattr
.va_mode
= S_IFDIR
|0755;
1488 vattr
.va_uid
= crgetuid(cr
);
1489 vattr
.va_gid
= crgetgid(cr
);
1491 rootzp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
1492 rootzp
->z_moved
= 0;
1493 rootzp
->z_unlinked
= 0;
1494 rootzp
->z_atime_dirty
= 0;
1495 rootzp
->z_is_sa
= USE_SA(version
, os
);
1497 zsb
= kmem_zalloc(sizeof (zfs_sb_t
), KM_SLEEP
);
1499 zsb
->z_parent
= zsb
;
1500 zsb
->z_version
= version
;
1501 zsb
->z_use_fuids
= USE_FUIDS(version
, os
);
1502 zsb
->z_use_sa
= USE_SA(version
, os
);
1505 sb
= kmem_zalloc(sizeof (struct super_block
), KM_SLEEP
);
1506 sb
->s_fs_info
= zsb
;
1508 ZTOI(rootzp
)->i_sb
= sb
;
1510 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
1511 &zsb
->z_attr_table
);
1516 * Fold case on file systems that are always or sometimes case
1519 if (sense
== ZFS_CASE_INSENSITIVE
|| sense
== ZFS_CASE_MIXED
)
1520 zsb
->z_norm
|= U8_TEXTPREP_TOUPPER
;
1522 mutex_init(&zsb
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1523 list_create(&zsb
->z_all_znodes
, sizeof (znode_t
),
1524 offsetof(znode_t
, z_link_node
));
1526 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1527 mutex_init(&zsb
->z_hold_mtx
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
1529 VERIFY(0 == zfs_acl_ids_create(rootzp
, IS_ROOT_NODE
, &vattr
,
1530 cr
, NULL
, &acl_ids
));
1531 zfs_mknode(rootzp
, &vattr
, tx
, cr
, IS_ROOT_NODE
, &zp
, &acl_ids
);
1532 ASSERT3P(zp
, ==, rootzp
);
1533 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &rootzp
->z_id
, tx
);
1535 zfs_acl_ids_free(&acl_ids
);
1537 atomic_set(&ZTOI(rootzp
)->i_count
, 0);
1538 sa_handle_destroy(rootzp
->z_sa_hdl
);
1539 kmem_cache_free(znode_cache
, rootzp
);
1542 * Create shares directory
1544 error
= zfs_create_share_dir(zsb
, tx
);
1547 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1548 mutex_destroy(&zsb
->z_hold_mtx
[i
]);
1550 kmem_free(sb
, sizeof (struct super_block
));
1551 kmem_free(zsb
, sizeof (zfs_sb_t
));
1553 #endif /* _KERNEL */
1556 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1558 uint64_t sa_obj
= 0;
1561 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1562 if (error
!= 0 && error
!= ENOENT
)
1565 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1570 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1571 dmu_buf_t
**db
, void *tag
)
1573 dmu_object_info_t doi
;
1576 if ((error
= sa_buf_hold(osp
, obj
, tag
, db
)) != 0)
1579 dmu_object_info_from_db(*db
, &doi
);
1580 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
1581 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
1582 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1583 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
1584 sa_buf_rele(*db
, tag
);
1588 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
1590 sa_buf_rele(*db
, tag
);
1598 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
, void *tag
)
1600 sa_handle_destroy(hdl
);
1601 sa_buf_rele(db
, tag
);
1605 * Given an object number, return its parent object number and whether
1606 * or not the object is an extended attribute directory.
1609 zfs_obj_to_pobj(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
, uint64_t *pobjp
,
1615 sa_bulk_attr_t bulk
[3];
1619 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
1620 &parent
, sizeof (parent
));
1621 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
1622 &pflags
, sizeof (pflags
));
1623 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1624 &mode
, sizeof (mode
));
1626 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
1630 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
1636 * Given an object number, return some zpl level statistics
1639 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
1642 sa_bulk_attr_t bulk
[4];
1645 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1646 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
1647 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
1648 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
1649 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
1650 &sb
->zs_links
, sizeof (sb
->zs_links
));
1651 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
1652 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
1654 return (sa_bulk_lookup(hdl
, bulk
, count
));
1658 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
1659 sa_attr_type_t
*sa_table
, char *buf
, int len
)
1661 sa_handle_t
*sa_hdl
;
1662 sa_handle_t
*prevhdl
= NULL
;
1663 dmu_buf_t
*prevdb
= NULL
;
1664 dmu_buf_t
*sa_db
= NULL
;
1665 char *path
= buf
+ len
- 1;
1673 char component
[MAXNAMELEN
+ 2];
1678 zfs_release_sa_handle(prevhdl
, prevdb
, FTAG
);
1680 if ((error
= zfs_obj_to_pobj(sa_hdl
, sa_table
, &pobj
,
1681 &is_xattrdir
)) != 0)
1692 (void) sprintf(component
+ 1, "<xattrdir>");
1694 error
= zap_value_search(osp
, pobj
, obj
,
1695 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
1700 complen
= strlen(component
);
1702 ASSERT(path
>= buf
);
1703 bcopy(component
, path
, complen
);
1706 if (sa_hdl
!= hdl
) {
1710 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
, FTAG
);
1718 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
1719 ASSERT(sa_db
!= NULL
);
1720 zfs_release_sa_handle(sa_hdl
, sa_db
, FTAG
);
1724 (void) memmove(buf
, path
, buf
+ len
- path
);
1730 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
1732 sa_attr_type_t
*sa_table
;
1737 error
= zfs_sa_setup(osp
, &sa_table
);
1741 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
, FTAG
);
1745 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1747 zfs_release_sa_handle(hdl
, db
, FTAG
);
1752 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
1755 char *path
= buf
+ len
- 1;
1756 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_stats_impl(hdl
, sa_table
, sb
);
1773 zfs_release_sa_handle(hdl
, db
, FTAG
);
1777 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1779 zfs_release_sa_handle(hdl
, db
, FTAG
);
1783 #if defined(_KERNEL) && defined(HAVE_SPL)
1784 EXPORT_SYMBOL(zfs_create_fs
);
1785 EXPORT_SYMBOL(zfs_obj_to_path
);