4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Portions Copyright 2007 Jeremy Teo */
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/mntent.h>
35 #include <sys/mkdev.h>
36 #include <sys/u8_textprep.h>
37 #include <sys/dsl_dataset.h>
39 #include <sys/vfs_opreg.h>
40 #include <sys/vnode.h>
43 #include <sys/errno.h>
44 #include <sys/unistd.h>
46 #include <sys/atomic.h>
48 #include "fs/fs_subr.h"
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/zfs_rlock.h>
53 #include <sys/zfs_fuid.h>
54 #include <sys/zfs_vnops.h>
55 #include <sys/dnode.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/kidmap.h>
62 #include <sys/refcount.h>
65 #include <sys/zfs_znode.h>
67 #include <sys/zfs_sa.h>
68 #include <sys/zfs_stat.h>
71 #include "zfs_comutil.h"
74 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
75 * turned on when DEBUG is also defined.
82 #define ZNODE_STAT_ADD(stat) ((stat)++)
84 #define ZNODE_STAT_ADD(stat) /* nothing */
85 #endif /* ZNODE_STATS */
88 * Functions needed for userland (ie: libzpool) are not put under
89 * #ifdef_KERNEL; the rest of the functions have dependencies
90 * (such as VFS logic) that will not compile easily in userland.
94 static kmem_cache_t
*znode_cache
= NULL
;
98 zfs_znode_cache_constructor(void *buf
, void *arg
, int kmflags
)
102 inode_init_once(ZTOI(zp
));
103 list_link_init(&zp
->z_link_node
);
105 mutex_init(&zp
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
106 rw_init(&zp
->z_parent_lock
, NULL
, RW_DEFAULT
, NULL
);
107 rw_init(&zp
->z_name_lock
, NULL
, RW_DEFAULT
, NULL
);
108 mutex_init(&zp
->z_acl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
110 mutex_init(&zp
->z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
111 avl_create(&zp
->z_range_avl
, zfs_range_compare
,
112 sizeof (rl_t
), offsetof(rl_t
, r_node
));
114 zp
->z_dirlocks
= NULL
;
115 zp
->z_acl_cached
= NULL
;
122 zfs_znode_cache_destructor(void *buf
, void *arg
)
126 ASSERT(!list_link_active(&zp
->z_link_node
));
127 mutex_destroy(&zp
->z_lock
);
128 rw_destroy(&zp
->z_parent_lock
);
129 rw_destroy(&zp
->z_name_lock
);
130 mutex_destroy(&zp
->z_acl_lock
);
131 avl_destroy(&zp
->z_range_avl
);
132 mutex_destroy(&zp
->z_range_lock
);
134 ASSERT(zp
->z_dirlocks
== NULL
);
135 ASSERT(zp
->z_acl_cached
== NULL
);
144 ASSERT(znode_cache
== NULL
);
145 znode_cache
= kmem_cache_create("zfs_znode_cache",
146 sizeof (znode_t
), 0, zfs_znode_cache_constructor
,
147 zfs_znode_cache_destructor
, NULL
, NULL
, NULL
, KMC_KMEM
);
157 kmem_cache_destroy(znode_cache
);
162 zfs_create_share_dir(zfs_sb_t
*zsb
, dmu_tx_t
*tx
)
165 zfs_acl_ids_t acl_ids
;
172 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
173 vattr
.va_mode
= S_IFDIR
| 0555;
174 vattr
.va_uid
= crgetuid(kcred
);
175 vattr
.va_gid
= crgetgid(kcred
);
177 sharezp
= kmem_cache_alloc(znode_cache
, KM_PUSHPAGE
);
178 sharezp
->z_moved
= 0;
179 sharezp
->z_unlinked
= 0;
180 sharezp
->z_atime_dirty
= 0;
181 sharezp
->z_zfsvfs
= zfsvfs
;
182 sharezp
->z_is_sa
= zfsvfs
->z_use_sa
;
188 VERIFY(0 == zfs_acl_ids_create(sharezp
, IS_ROOT_NODE
, &vattr
,
189 kcred
, NULL
, &acl_ids
));
190 zfs_mknode(sharezp
, &vattr
, tx
, kcred
, IS_ROOT_NODE
, &zp
, &acl_ids
);
191 ASSERT3P(zp
, ==, sharezp
);
192 ASSERT(!vn_in_dnlc(ZTOV(sharezp
))); /* not valid to move */
193 POINTER_INVALIDATE(&sharezp
->z_zfsvfs
);
194 error
= zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
195 ZFS_SHARES_DIR
, 8, 1, &sharezp
->z_id
, tx
);
196 zfsvfs
->z_shares_dir
= sharezp
->z_id
;
198 zfs_acl_ids_free(&acl_ids
);
199 // ZTOV(sharezp)->v_count = 0;
200 sa_handle_destroy(sharezp
->z_sa_hdl
);
201 kmem_cache_free(znode_cache
, sharezp
);
206 #endif /* HAVE_SHARE */
210 zfs_znode_sa_init(zfs_sb_t
*zsb
, znode_t
*zp
,
211 dmu_buf_t
*db
, dmu_object_type_t obj_type
, sa_handle_t
*sa_hdl
)
213 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb
, zp
->z_id
)));
215 mutex_enter(&zp
->z_lock
);
217 ASSERT(zp
->z_sa_hdl
== NULL
);
218 ASSERT(zp
->z_acl_cached
== NULL
);
219 if (sa_hdl
== NULL
) {
220 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, zp
,
221 SA_HDL_SHARED
, &zp
->z_sa_hdl
));
223 zp
->z_sa_hdl
= sa_hdl
;
224 sa_set_userp(sa_hdl
, zp
);
227 zp
->z_is_sa
= (obj_type
== DMU_OT_SA
) ? B_TRUE
: B_FALSE
;
229 mutex_exit(&zp
->z_lock
);
233 zfs_znode_dmu_fini(znode_t
*zp
)
235 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp
), zp
->z_id
)) ||
237 RW_WRITE_HELD(&ZTOZSB(zp
)->z_teardown_inactive_lock
));
239 sa_handle_destroy(zp
->z_sa_hdl
);
244 * Called by new_inode() to allocate a new inode.
247 zfs_inode_alloc(struct super_block
*sb
, struct inode
**ip
)
251 zp
= kmem_cache_alloc(znode_cache
, KM_PUSHPAGE
);
258 * Called in multiple places when an inode should be destroyed.
261 zfs_inode_destroy(struct inode
*ip
)
263 znode_t
*zp
= ITOZ(ip
);
264 zfs_sb_t
*zsb
= ZTOZSB(zp
);
266 mutex_enter(&zsb
->z_znodes_lock
);
267 list_remove(&zsb
->z_all_znodes
, zp
);
268 mutex_exit(&zsb
->z_znodes_lock
);
270 if (zp
->z_acl_cached
) {
271 zfs_acl_free(zp
->z_acl_cached
);
272 zp
->z_acl_cached
= NULL
;
275 kmem_cache_free(znode_cache
, zp
);
279 zfs_inode_set_ops(zfs_sb_t
*zsb
, struct inode
*ip
)
283 switch (ip
->i_mode
& S_IFMT
) {
285 ip
->i_op
= &zpl_inode_operations
;
286 ip
->i_fop
= &zpl_file_operations
;
287 ip
->i_mapping
->a_ops
= &zpl_address_space_operations
;
291 ip
->i_op
= &zpl_dir_inode_operations
;
292 ip
->i_fop
= &zpl_dir_file_operations
;
293 ITOZ(ip
)->z_zn_prefetch
= B_TRUE
;
297 ip
->i_op
= &zpl_symlink_inode_operations
;
301 * rdev is only stored in a SA only for device files.
305 VERIFY(sa_lookup(ITOZ(ip
)->z_sa_hdl
, SA_ZPL_RDEV(zsb
),
306 &rdev
, sizeof (rdev
)) == 0);
310 init_special_inode(ip
, ip
->i_mode
, rdev
);
311 ip
->i_op
= &zpl_special_inode_operations
;
315 printk("ZFS: Invalid mode: 0x%x\n", ip
->i_mode
);
321 * Construct a znode+inode and initialize.
323 * This does not do a call to dmu_set_user() that is
324 * up to the caller to do, in case you don't want to
328 zfs_znode_alloc(zfs_sb_t
*zsb
, dmu_buf_t
*db
, int blksz
,
329 dmu_object_type_t obj_type
, uint64_t obj
, sa_handle_t
*hdl
,
330 struct dentry
*dentry
, struct inode
*dip
)
335 sa_bulk_attr_t bulk
[9];
340 ip
= new_inode(zsb
->z_sb
);
345 ASSERT(zp
->z_dirlocks
== NULL
);
349 zp
->z_atime_dirty
= 0;
351 zp
->z_id
= db
->db_object
;
353 zp
->z_seq
= 0x7A4653;
357 zfs_znode_sa_init(zsb
, zp
, db
, obj_type
, hdl
);
359 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
, &zp
->z_mode
, 8);
360 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
, &zp
->z_gen
, 8);
361 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
362 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
, &zp
->z_links
, 8);
363 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
365 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zsb
), NULL
,
367 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
369 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
, &zp
->z_uid
, 8);
370 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
, &zp
->z_gid
, 8);
372 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
) != 0 || zp
->z_gen
== 0) {
374 sa_handle_destroy(zp
->z_sa_hdl
);
380 zfs_inode_update(zp
);
381 zfs_inode_set_ops(zsb
, ip
);
383 if (insert_inode_locked(ip
))
387 if (zpl_xattr_security_init(ip
, dip
, &dentry
->d_name
))
390 d_instantiate(dentry
, ip
);
393 mutex_enter(&zsb
->z_znodes_lock
);
394 list_insert_tail(&zsb
->z_all_znodes
, zp
);
396 mutex_exit(&zsb
->z_znodes_lock
);
398 unlock_new_inode(ip
);
402 unlock_new_inode(ip
);
408 * Update the embedded inode given the znode. We should work toward
409 * eliminating this function as soon as possible by removing values
410 * which are duplicated between the znode and inode. If the generic
411 * inode has the correct field it should be used, and the ZFS code
412 * updated to access the inode. This can be done incrementally.
415 zfs_inode_update(znode_t
*zp
)
420 uint64_t atime
[2], mtime
[2], ctime
[2];
426 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
), &atime
, 16);
427 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MTIME(zsb
), &mtime
, 16);
428 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CTIME(zsb
), &ctime
, 16);
430 spin_lock(&ip
->i_lock
);
431 ip
->i_generation
= zp
->z_gen
;
432 ip
->i_uid
= zp
->z_uid
;
433 ip
->i_gid
= zp
->z_gid
;
434 ip
->i_nlink
= zp
->z_links
;
435 ip
->i_mode
= zp
->z_mode
;
436 ip
->i_blkbits
= SPA_MINBLOCKSHIFT
;
437 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &blksize
,
438 (u_longlong_t
*)&ip
->i_blocks
);
440 ZFS_TIME_DECODE(&ip
->i_atime
, atime
);
441 ZFS_TIME_DECODE(&ip
->i_mtime
, mtime
);
442 ZFS_TIME_DECODE(&ip
->i_ctime
, ctime
);
444 i_size_write(ip
, zp
->z_size
);
445 spin_unlock(&ip
->i_lock
);
448 static uint64_t empty_xattr
;
449 static uint64_t pad
[4];
450 static zfs_acl_phys_t acl_phys
;
452 * Create a new DMU object to hold a zfs znode.
454 * IN: dzp - parent directory for new znode
455 * vap - file attributes for new znode
456 * tx - dmu transaction id for zap operations
457 * cr - credentials of caller
459 * IS_ROOT_NODE - new object will be root
460 * IS_XATTR - new object is an attribute
461 * bonuslen - length of bonus buffer
462 * setaclp - File/Dir initial ACL
463 * fuidp - Tracks fuid allocation.
465 * OUT: zpp - allocated znode
469 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
470 uint_t flag
, znode_t
**zpp
, zfs_acl_ids_t
*acl_ids
)
472 uint64_t crtime
[2], atime
[2], mtime
[2], ctime
[2];
473 uint64_t mode
, size
, links
, parent
, pflags
;
474 uint64_t dzp_pflags
= 0;
476 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
483 dmu_object_type_t obj_type
;
484 sa_bulk_attr_t
*sa_attrs
;
486 zfs_acl_locator_cb_t locate
= { 0 };
489 obj
= vap
->va_nodeid
;
490 now
= vap
->va_ctime
; /* see zfs_replay_create() */
491 gen
= vap
->va_nblocks
; /* ditto */
495 gen
= dmu_tx_get_txg(tx
);
498 obj_type
= zsb
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
499 bonuslen
= (obj_type
== DMU_OT_SA
) ?
500 DN_MAX_BONUSLEN
: ZFS_OLD_ZNODE_PHYS_SIZE
;
503 * Create a new DMU object.
506 * There's currently no mechanism for pre-reading the blocks that will
507 * be needed to allocate a new object, so we accept the small chance
508 * that there will be an i/o error and we will fail one of the
511 if (S_ISDIR(vap
->va_mode
)) {
513 err
= zap_create_claim_norm(zsb
->z_os
, obj
,
514 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
515 obj_type
, bonuslen
, tx
);
516 ASSERT3U(err
, ==, 0);
518 obj
= zap_create_norm(zsb
->z_os
,
519 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
520 obj_type
, bonuslen
, tx
);
524 err
= dmu_object_claim(zsb
->z_os
, obj
,
525 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
526 obj_type
, bonuslen
, tx
);
527 ASSERT3U(err
, ==, 0);
529 obj
= dmu_object_alloc(zsb
->z_os
,
530 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
531 obj_type
, bonuslen
, tx
);
535 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
536 VERIFY(0 == sa_buf_hold(zsb
->z_os
, obj
, NULL
, &db
));
539 * If this is the root, fix up the half-initialized parent pointer
540 * to reference the just-allocated physical data area.
542 if (flag
& IS_ROOT_NODE
) {
545 dzp_pflags
= dzp
->z_pflags
;
549 * If parent is an xattr, so am I.
551 if (dzp_pflags
& ZFS_XATTR
) {
555 if (zsb
->z_use_fuids
)
556 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
560 if (S_ISDIR(vap
->va_mode
)) {
561 size
= 2; /* contents ("." and "..") */
562 links
= (flag
& (IS_ROOT_NODE
| IS_XATTR
)) ? 2 : 1;
567 if (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))
571 mode
= acl_ids
->z_mode
;
576 * No execs denied will be deterimed when zfs_mode_compute() is called.
578 pflags
|= acl_ids
->z_aclp
->z_hints
&
579 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
580 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
582 ZFS_TIME_ENCODE(&now
, crtime
);
583 ZFS_TIME_ENCODE(&now
, ctime
);
585 if (vap
->va_mask
& ATTR_ATIME
) {
586 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
588 ZFS_TIME_ENCODE(&now
, atime
);
591 if (vap
->va_mask
& ATTR_MTIME
) {
592 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
594 ZFS_TIME_ENCODE(&now
, mtime
);
597 /* Now add in all of the "SA" attributes */
598 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, NULL
, SA_HDL_SHARED
,
602 * Setup the array of attributes to be replaced/set on the new file
604 * order for DMU_OT_ZNODE is critical since it needs to be constructed
605 * in the old znode_phys_t format. Don't change this ordering
607 sa_attrs
= kmem_alloc(sizeof(sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
609 if (obj_type
== DMU_OT_ZNODE
) {
610 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
612 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
614 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
616 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
618 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
620 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
622 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
624 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
627 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
629 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
631 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
633 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
),
634 NULL
, &acl_ids
->z_fuid
, 8);
635 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
),
636 NULL
, &acl_ids
->z_fgid
, 8);
637 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
639 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
641 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
643 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
645 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
647 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
651 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zsb
), NULL
, &links
, 8);
653 if (obj_type
== DMU_OT_ZNODE
) {
654 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zsb
), NULL
,
657 if (obj_type
== DMU_OT_ZNODE
||
658 (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
))) {
659 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zsb
),
662 if (obj_type
== DMU_OT_ZNODE
) {
663 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
665 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
), NULL
,
666 &acl_ids
->z_fuid
, 8);
667 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
), NULL
,
668 &acl_ids
->z_fgid
, 8);
669 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zsb
), NULL
, pad
,
670 sizeof (uint64_t) * 4);
671 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zsb
), NULL
,
672 &acl_phys
, sizeof (zfs_acl_phys_t
));
673 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
674 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zsb
), NULL
,
675 &acl_ids
->z_aclp
->z_acl_count
, 8);
676 locate
.cb_aclp
= acl_ids
->z_aclp
;
677 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zsb
),
678 zfs_acl_data_locator
, &locate
,
679 acl_ids
->z_aclp
->z_acl_bytes
);
680 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
681 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
684 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
686 if (!(flag
& IS_ROOT_NODE
)) {
687 *zpp
= zfs_znode_alloc(zsb
, db
, 0, obj_type
, obj
, sa_hdl
,
688 vap
->va_dentry
, ZTOI(dzp
));
689 ASSERT(*zpp
!= NULL
);
693 * If we are creating the root node, the "parent" we
694 * passed in is the znode for the root.
698 (*zpp
)->z_sa_hdl
= sa_hdl
;
701 (*zpp
)->z_pflags
= pflags
;
702 (*zpp
)->z_mode
= mode
;
704 if (obj_type
== DMU_OT_ZNODE
||
705 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
706 err
= zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
);
707 ASSERT3S(err
, ==, 0);
709 kmem_free(sa_attrs
, sizeof(sa_bulk_attr_t
) * ZPL_END
);
710 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
714 * zfs_xvattr_set only updates the in-core attributes
715 * it is assumed the caller will be doing an sa_bulk_update
716 * to push the changes out
719 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
723 xoap
= xva_getxoptattr(xvap
);
726 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
728 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, times
);
729 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_CRTIME(ZTOZSB(zp
)),
730 ×
, sizeof (times
), tx
);
731 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
733 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
734 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
,
736 XVA_SET_RTN(xvap
, XAT_READONLY
);
738 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
739 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
,
741 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
743 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
744 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
,
746 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
748 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
749 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
,
751 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
753 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
754 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
,
756 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
758 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
759 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
,
761 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
763 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
764 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
,
766 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
768 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
769 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
,
771 XVA_SET_RTN(xvap
, XAT_NODUMP
);
773 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
774 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
,
776 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
778 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
779 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
780 xoap
->xoa_av_quarantined
, zp
->z_pflags
, tx
);
781 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
783 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
784 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
,
786 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
788 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
789 zfs_sa_set_scanstamp(zp
, xvap
, tx
);
790 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
792 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
793 ZFS_ATTR_SET(zp
, ZFS_REPARSE
, xoap
->xoa_reparse
,
795 XVA_SET_RTN(xvap
, XAT_REPARSE
);
797 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
798 ZFS_ATTR_SET(zp
, ZFS_OFFLINE
, xoap
->xoa_offline
,
800 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
802 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
803 ZFS_ATTR_SET(zp
, ZFS_SPARSE
, xoap
->xoa_sparse
,
805 XVA_SET_RTN(xvap
, XAT_SPARSE
);
810 zfs_zget(zfs_sb_t
*zsb
, uint64_t obj_num
, znode_t
**zpp
)
812 dmu_object_info_t doi
;
822 ip
= ilookup(zsb
->z_sb
, obj_num
);
824 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
826 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
828 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
833 dmu_object_info_from_db(db
, &doi
);
834 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
835 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
836 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
837 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
838 sa_buf_rele(db
, NULL
);
839 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
844 hdl
= dmu_buf_get_user(db
);
848 * ilookup returned NULL, which means
849 * the znode is dying - but the SA handle isn't
850 * quite dead yet, we need to drop any locks
851 * we're holding, re-schedule the task and try again.
853 sa_buf_rele(db
, NULL
);
854 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
860 zp
= sa_get_userdata(hdl
);
863 * Since "SA" does immediate eviction we
864 * should never find a sa handle that doesn't
865 * know about the znode.
868 ASSERT3P(zp
, !=, NULL
);
870 mutex_enter(&zp
->z_lock
);
871 ASSERT3U(zp
->z_id
, ==, obj_num
);
872 if (zp
->z_unlinked
) {
879 sa_buf_rele(db
, NULL
);
880 mutex_exit(&zp
->z_lock
);
881 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
886 ASSERT3P(ip
, ==, NULL
);
889 * Not found create new znode/vnode but only if file exists.
891 * There is a small window where zfs_vget() could
892 * find this object while a file create is still in
893 * progress. This is checked for in zfs_znode_alloc()
895 * if zfs_znode_alloc() fails it will drop the hold on the
898 zp
= zfs_znode_alloc(zsb
, db
, doi
.doi_data_block_size
,
899 doi
.doi_bonus_type
, obj_num
, NULL
, NULL
, NULL
);
905 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
910 zfs_rezget(znode_t
*zp
)
912 zfs_sb_t
*zsb
= ZTOZSB(zp
);
913 dmu_object_info_t doi
;
915 uint64_t obj_num
= zp
->z_id
;
917 sa_bulk_attr_t bulk
[8];
922 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
924 mutex_enter(&zp
->z_acl_lock
);
925 if (zp
->z_acl_cached
) {
926 zfs_acl_free(zp
->z_acl_cached
);
927 zp
->z_acl_cached
= NULL
;
930 mutex_exit(&zp
->z_acl_lock
);
931 ASSERT(zp
->z_sa_hdl
== NULL
);
932 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
934 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
938 dmu_object_info_from_db(db
, &doi
);
939 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
940 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
941 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
942 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
943 sa_buf_rele(db
, NULL
);
944 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
948 zfs_znode_sa_init(zsb
, zp
, db
, doi
.doi_bonus_type
, NULL
);
950 /* reload cached values */
951 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
,
953 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
,
954 &zp
->z_size
, sizeof (zp
->z_size
));
955 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
,
956 &zp
->z_links
, sizeof (zp
->z_links
));
957 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
958 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
959 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
960 &zp
->z_atime
, sizeof (zp
->z_atime
));
961 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
962 &zp
->z_uid
, sizeof (zp
->z_uid
));
963 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
,
964 &zp
->z_gid
, sizeof (zp
->z_gid
));
965 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
966 &mode
, sizeof (mode
));
968 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
969 zfs_znode_dmu_fini(zp
);
970 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
976 if (gen
!= zp
->z_gen
) {
977 zfs_znode_dmu_fini(zp
);
978 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
982 zp
->z_unlinked
= (zp
->z_links
== 0);
983 zp
->z_blksz
= doi
.doi_data_block_size
;
985 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
991 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
993 zfs_sb_t
*zsb
= ZTOZSB(zp
);
994 objset_t
*os
= zsb
->z_os
;
995 uint64_t obj
= zp
->z_id
;
996 uint64_t acl_obj
= zfs_external_acl(zp
);
998 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
1000 VERIFY(!zp
->z_is_sa
);
1001 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1003 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1004 zfs_znode_dmu_fini(zp
);
1005 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
1009 zfs_zinactive(znode_t
*zp
)
1011 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1012 uint64_t z_id
= zp
->z_id
;
1013 boolean_t drop_mutex
= 0;
1015 ASSERT(zp
->z_sa_hdl
);
1018 * Don't allow a zfs_zget() while were trying to release this znode.
1020 * Linux allows direct memory reclaim which means that any KM_SLEEP
1021 * allocation may trigger inode eviction. This can lead to a deadlock
1022 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
1023 * zfs_zinactive() call path. To avoid this deadlock the process
1024 * must not reacquire the mutex when it is already holding it.
1026 if (!ZFS_OBJ_HOLD_OWNED(zsb
, z_id
)) {
1027 ZFS_OBJ_HOLD_ENTER(zsb
, z_id
);
1031 mutex_enter(&zp
->z_lock
);
1034 * If this was the last reference to a file with no links,
1035 * remove the file from the file system.
1037 if (zp
->z_unlinked
) {
1038 mutex_exit(&zp
->z_lock
);
1041 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
1047 mutex_exit(&zp
->z_lock
);
1048 zfs_znode_dmu_fini(zp
);
1051 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
1055 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
1056 uint64_t ctime
[2], boolean_t have_tx
)
1062 if (have_tx
) { /* will sa_bulk_update happen really soon? */
1063 zp
->z_atime_dirty
= 0;
1066 zp
->z_atime_dirty
= 1;
1069 if (flag
& ATTR_ATIME
) {
1070 ZFS_TIME_ENCODE(&now
, zp
->z_atime
);
1073 if (flag
& ATTR_MTIME
) {
1074 ZFS_TIME_ENCODE(&now
, mtime
);
1075 if (ZTOZSB(zp
)->z_use_fuids
) {
1076 zp
->z_pflags
|= (ZFS_ARCHIVE
|
1081 if (flag
& ATTR_CTIME
) {
1082 ZFS_TIME_ENCODE(&now
, ctime
);
1083 if (ZTOZSB(zp
)->z_use_fuids
)
1084 zp
->z_pflags
|= ZFS_ARCHIVE
;
1089 * Grow the block size for a file.
1091 * IN: zp - znode of file to free data in.
1092 * size - requested block size
1093 * tx - open transaction.
1095 * NOTE: this function assumes that the znode is write locked.
1098 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1103 if (size
<= zp
->z_blksz
)
1106 * If the file size is already greater than the current blocksize,
1107 * we will not grow. If there is more than one block in a file,
1108 * the blocksize cannot change.
1110 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1113 error
= dmu_object_set_blocksize(ZTOZSB(zp
)->z_os
, zp
->z_id
,
1116 if (error
== ENOTSUP
)
1118 ASSERT3U(error
, ==, 0);
1120 /* What blocksize did we actually get? */
1121 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1125 * Increase the file length
1127 * IN: zp - znode of file to free data in.
1128 * end - new end-of-file
1130 * RETURN: 0 if success
1131 * error code if failure
1134 zfs_extend(znode_t
*zp
, uint64_t end
)
1136 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1143 * We will change zp_size, lock the whole file.
1145 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1148 * Nothing to do if file already at desired length.
1150 if (end
<= zp
->z_size
) {
1151 zfs_range_unlock(rl
);
1155 tx
= dmu_tx_create(zsb
->z_os
);
1156 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1157 zfs_sa_upgrade_txholds(tx
, zp
);
1158 if (end
> zp
->z_blksz
&&
1159 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zsb
->z_max_blksz
)) {
1161 * We are growing the file past the current block size.
1163 if (zp
->z_blksz
> ZTOZSB(zp
)->z_max_blksz
) {
1164 ASSERT(!ISP2(zp
->z_blksz
));
1165 newblksz
= MIN(end
, SPA_MAXBLOCKSIZE
);
1167 newblksz
= MIN(end
, ZTOZSB(zp
)->z_max_blksz
);
1169 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1174 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1176 if (error
== ERESTART
) {
1182 zfs_range_unlock(rl
);
1187 zfs_grow_blocksize(zp
, newblksz
, tx
);
1191 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(ZTOZSB(zp
)),
1192 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1194 zfs_range_unlock(rl
);
1202 * Free space in a file.
1204 * IN: zp - znode of file to free data in.
1205 * off - start of section to free.
1206 * len - length of section to free.
1208 * RETURN: 0 if success
1209 * error code if failure
1212 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1214 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1219 * Lock the range being freed.
1221 rl
= zfs_range_lock(zp
, off
, len
, RL_WRITER
);
1224 * Nothing to do if file already at desired length.
1226 if (off
>= zp
->z_size
) {
1227 zfs_range_unlock(rl
);
1231 if (off
+ len
> zp
->z_size
)
1232 len
= zp
->z_size
- off
;
1234 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, off
, len
);
1236 zfs_range_unlock(rl
);
1244 * IN: zp - znode of file to free data in.
1245 * end - new end-of-file.
1247 * RETURN: 0 if success
1248 * error code if failure
1251 zfs_trunc(znode_t
*zp
, uint64_t end
)
1253 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1257 sa_bulk_attr_t bulk
[2];
1261 * We will change zp_size, lock the whole file.
1263 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1266 * Nothing to do if file already at desired length.
1268 if (end
>= zp
->z_size
) {
1269 zfs_range_unlock(rl
);
1273 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, end
, -1);
1275 zfs_range_unlock(rl
);
1279 tx
= dmu_tx_create(zsb
->z_os
);
1280 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1281 zfs_sa_upgrade_txholds(tx
, zp
);
1282 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1284 if (error
== ERESTART
) {
1290 zfs_range_unlock(rl
);
1295 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
),
1296 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1299 zp
->z_pflags
&= ~ZFS_SPARSE
;
1300 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1301 NULL
, &zp
->z_pflags
, 8);
1303 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1307 zfs_range_unlock(rl
);
1313 * Free space in a file
1315 * IN: zp - znode of file to free data in.
1316 * off - start of range
1317 * len - end of range (0 => EOF)
1318 * flag - current file open mode flags.
1319 * log - TRUE if this action should be logged
1321 * RETURN: 0 if success
1322 * error code if failure
1325 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1327 struct inode
*ip
= ZTOI(zp
);
1329 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1330 zilog_t
*zilog
= zsb
->z_log
;
1332 uint64_t mtime
[2], ctime
[2];
1333 sa_bulk_attr_t bulk
[3];
1337 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
), &mode
,
1338 sizeof (mode
))) != 0)
1341 if (off
> zp
->z_size
) {
1342 error
= zfs_extend(zp
, off
+len
);
1343 if (error
== 0 && log
)
1350 * Check for any locks in the region to be freed.
1352 if (ip
->i_flock
&& mandatory_lock(ip
)) {
1353 uint64_t length
= (len
? len
: zp
->z_size
- off
);
1354 if (!lock_may_write(ip
, off
, length
))
1359 error
= zfs_trunc(zp
, off
);
1361 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1362 off
+ len
> zp
->z_size
)
1363 error
= zfs_extend(zp
, off
+len
);
1368 tx
= dmu_tx_create(zsb
->z_os
);
1369 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1370 zfs_sa_upgrade_txholds(tx
, zp
);
1371 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1373 if (error
== ERESTART
) {
1382 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, mtime
, 16);
1383 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, ctime
, 16);
1384 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1385 NULL
, &zp
->z_pflags
, 8);
1386 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
, B_TRUE
);
1387 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1390 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1393 zfs_inode_update(zp
);
1398 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1400 uint64_t moid
, obj
, sa_obj
, version
;
1409 * First attempt to create master node.
1412 * In an empty objset, there are no blocks to read and thus
1413 * there can be no i/o errors (which we assert below).
1415 moid
= MASTER_NODE_OBJ
;
1416 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1417 DMU_OT_NONE
, 0, tx
);
1421 * Set starting attributes.
1423 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1425 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1426 /* For the moment we expect all zpl props to be uint64_ts */
1430 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1431 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1432 name
= nvpair_name(elem
);
1433 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1437 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1440 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1443 ASSERT(version
!= 0);
1444 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1447 * Create zap object used for SA attribute registration
1450 if (version
>= ZPL_VERSION_SA
) {
1451 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1452 DMU_OT_NONE
, 0, tx
);
1453 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1459 * Create a delete queue.
1461 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1463 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1467 * Create root znode with code free of VFS dependencies. This
1468 * is important because without a registered filesystem and super
1469 * block all the required VFS hooks will be missing. The critical
1470 * thing is to just crete the required root znode.
1472 obj
= zap_create_norm(os
, norm
, DMU_OT_DIRECTORY_CONTENTS
,
1473 DMU_OT_ZNODE
, sizeof (znode_phys_t
), tx
);
1475 VERIFY(0 == dmu_bonus_hold(os
, obj
, FTAG
, &db
));
1476 dmu_buf_will_dirty(db
, tx
);
1479 * Initialize the znode physical data to zero.
1481 ASSERT(db
->db_size
>= sizeof (znode_phys_t
));
1482 bzero(db
->db_data
, db
->db_size
);
1485 if (USE_FUIDS(version
, os
))
1486 pzp
->zp_flags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
1488 pzp
->zp_size
= 2; /* "." and ".." */
1490 pzp
->zp_parent
= obj
;
1491 pzp
->zp_gen
= dmu_tx_get_txg(tx
);
1492 pzp
->zp_mode
= S_IFDIR
| 0755;
1493 pzp
->zp_flags
= ZFS_ACL_TRIVIAL
;
1497 ZFS_TIME_ENCODE(&now
, pzp
->zp_crtime
);
1498 ZFS_TIME_ENCODE(&now
, pzp
->zp_ctime
);
1499 ZFS_TIME_ENCODE(&now
, pzp
->zp_atime
);
1500 ZFS_TIME_ENCODE(&now
, pzp
->zp_mtime
);
1502 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &obj
, tx
);
1505 dmu_buf_rele(db
, FTAG
);
1508 #endif /* _KERNEL */
1511 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1513 uint64_t sa_obj
= 0;
1516 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1517 if (error
!= 0 && error
!= ENOENT
)
1520 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1525 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1528 dmu_object_info_t doi
;
1531 if ((error
= sa_buf_hold(osp
, obj
, FTAG
, db
)) != 0)
1534 dmu_object_info_from_db(*db
, &doi
);
1535 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
1536 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
1537 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1538 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
1539 sa_buf_rele(*db
, FTAG
);
1543 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
1545 sa_buf_rele(*db
, FTAG
);
1553 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
)
1555 sa_handle_destroy(hdl
);
1556 sa_buf_rele(db
, FTAG
);
1560 * Given an object number, return its parent object number and whether
1561 * or not the object is an extended attribute directory.
1564 zfs_obj_to_pobj(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
, uint64_t *pobjp
,
1570 sa_bulk_attr_t bulk
[3];
1574 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
1575 &parent
, sizeof (parent
));
1576 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
1577 &pflags
, sizeof (pflags
));
1578 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1579 &mode
, sizeof (mode
));
1581 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
1585 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
1591 * Given an object number, return some zpl level statistics
1594 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
1597 sa_bulk_attr_t bulk
[4];
1600 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1601 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
1602 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
1603 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
1604 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
1605 &sb
->zs_links
, sizeof (sb
->zs_links
));
1606 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
1607 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
1609 return (sa_bulk_lookup(hdl
, bulk
, count
));
1613 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
1614 sa_attr_type_t
*sa_table
, char *buf
, int len
)
1616 sa_handle_t
*sa_hdl
;
1617 sa_handle_t
*prevhdl
= NULL
;
1618 dmu_buf_t
*prevdb
= NULL
;
1619 dmu_buf_t
*sa_db
= NULL
;
1620 char *path
= buf
+ len
- 1;
1628 char component
[MAXNAMELEN
+ 2];
1633 zfs_release_sa_handle(prevhdl
, prevdb
);
1635 if ((error
= zfs_obj_to_pobj(sa_hdl
, sa_table
, &pobj
,
1636 &is_xattrdir
)) != 0)
1647 (void) sprintf(component
+ 1, "<xattrdir>");
1649 error
= zap_value_search(osp
, pobj
, obj
,
1650 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
1655 complen
= strlen(component
);
1657 ASSERT(path
>= buf
);
1658 bcopy(component
, path
, complen
);
1661 if (sa_hdl
!= hdl
) {
1665 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
);
1673 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
1674 ASSERT(sa_db
!= NULL
);
1675 zfs_release_sa_handle(sa_hdl
, sa_db
);
1679 (void) memmove(buf
, path
, buf
+ len
- path
);
1685 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
1687 sa_attr_type_t
*sa_table
;
1692 error
= zfs_sa_setup(osp
, &sa_table
);
1696 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
);
1700 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1702 zfs_release_sa_handle(hdl
, db
);
1707 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
1710 char *path
= buf
+ len
- 1;
1711 sa_attr_type_t
*sa_table
;
1718 error
= zfs_sa_setup(osp
, &sa_table
);
1722 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
);
1726 error
= zfs_obj_to_stats_impl(hdl
, sa_table
, sb
);
1728 zfs_release_sa_handle(hdl
, db
);
1732 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1734 zfs_release_sa_handle(hdl
, db
);
1738 #if defined(_KERNEL) && defined(HAVE_SPL)
1739 EXPORT_SYMBOL(zfs_create_fs
);
1740 EXPORT_SYMBOL(zfs_obj_to_path
);