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_SLEEP
);
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 * define a couple of values we need available
211 * for both 64 and 32 bit environments.
214 #define NBITSMINOR64 32
217 #define MAXMAJ64 0xffffffffUL
220 #define MAXMIN64 0xffffffffUL
224 * Create special expldev for ZFS private use.
225 * Can't use standard expldev since it doesn't do
226 * what we want. The standard expldev() takes a
227 * dev32_t in LP64 and expands it to a long dev_t.
228 * We need an interface that takes a dev32_t in ILP32
229 * and expands it to a long dev_t.
232 zfs_expldev(dev_t dev
)
235 major_t major
= (major_t
)dev
>> NBITSMINOR32
& MAXMAJ32
;
236 return (((uint64_t)major
<< NBITSMINOR64
) |
237 ((minor_t
)dev
& MAXMIN32
));
244 zfs_znode_sa_init(zfs_sb_t
*zsb
, znode_t
*zp
,
245 dmu_buf_t
*db
, dmu_object_type_t obj_type
, sa_handle_t
*sa_hdl
)
247 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb
, zp
->z_id
)));
249 mutex_enter(&zp
->z_lock
);
251 ASSERT(zp
->z_sa_hdl
== NULL
);
252 ASSERT(zp
->z_acl_cached
== NULL
);
253 if (sa_hdl
== NULL
) {
254 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, zp
,
255 SA_HDL_SHARED
, &zp
->z_sa_hdl
));
257 zp
->z_sa_hdl
= sa_hdl
;
258 sa_set_userp(sa_hdl
, zp
);
261 zp
->z_is_sa
= (obj_type
== DMU_OT_SA
) ? B_TRUE
: B_FALSE
;
263 mutex_exit(&zp
->z_lock
);
267 zfs_znode_dmu_fini(znode_t
*zp
)
269 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp
), zp
->z_id
)) ||
271 RW_WRITE_HELD(&ZTOZSB(zp
)->z_teardown_inactive_lock
));
273 sa_handle_destroy(zp
->z_sa_hdl
);
278 * Called by new_inode() to allocate a new inode.
281 zfs_inode_alloc(struct super_block
*sb
, struct inode
**ip
)
285 zp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
292 * Called in multiple places when an inode should be destroyed.
295 zfs_inode_destroy(struct inode
*ip
)
297 znode_t
*zp
= ITOZ(ip
);
298 zfs_sb_t
*zsb
= ZTOZSB(zp
);
300 mutex_enter(&zsb
->z_znodes_lock
);
301 list_remove(&zsb
->z_all_znodes
, zp
);
302 mutex_exit(&zsb
->z_znodes_lock
);
304 if (zp
->z_acl_cached
) {
305 zfs_acl_free(zp
->z_acl_cached
);
306 zp
->z_acl_cached
= NULL
;
309 kmem_cache_free(znode_cache
, zp
);
313 zfs_inode_set_ops(zfs_sb_t
*zsb
, struct inode
*ip
)
317 switch (ip
->i_mode
& S_IFMT
) {
319 ip
->i_op
= &zpl_inode_operations
;
320 ip
->i_fop
= &zpl_file_operations
;
321 ip
->i_mapping
->a_ops
= &zpl_address_space_operations
;
325 ip
->i_op
= &zpl_dir_inode_operations
;
326 ip
->i_fop
= &zpl_dir_file_operations
;
327 ITOZ(ip
)->z_zn_prefetch
= B_TRUE
;
331 ip
->i_op
= &zpl_symlink_inode_operations
;
338 VERIFY(sa_lookup(ITOZ(ip
)->z_sa_hdl
, SA_ZPL_RDEV(zsb
),
339 &rdev
, sizeof (rdev
)) == 0);
340 init_special_inode(ip
, ip
->i_mode
, rdev
);
341 ip
->i_op
= &zpl_special_inode_operations
;
345 printk("ZFS: Invalid mode: 0x%x\n", ip
->i_mode
);
351 * Construct a znode+inode and initialize.
353 * This does not do a call to dmu_set_user() that is
354 * up to the caller to do, in case you don't want to
358 zfs_znode_alloc(zfs_sb_t
*zsb
, dmu_buf_t
*db
, int blksz
,
359 dmu_object_type_t obj_type
, uint64_t obj
, sa_handle_t
*hdl
)
364 sa_bulk_attr_t bulk
[9];
369 ip
= new_inode(zsb
->z_sb
);
374 ASSERT(zp
->z_dirlocks
== NULL
);
378 zp
->z_atime_dirty
= 0;
380 zp
->z_id
= db
->db_object
;
382 zp
->z_seq
= 0x7A4653;
385 zfs_znode_sa_init(zsb
, zp
, db
, obj_type
, hdl
);
387 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
, &zp
->z_mode
, 8);
388 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
, &zp
->z_gen
, 8);
389 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
390 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
, &zp
->z_links
, 8);
391 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
393 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zsb
), NULL
,
395 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
397 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
, &zp
->z_uid
, 8);
398 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
, &zp
->z_gid
, 8);
400 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
) != 0 || zp
->z_gen
== 0) {
402 sa_handle_destroy(zp
->z_sa_hdl
);
408 ip
->i_mode
= zp
->z_mode
;
409 ip
->i_mtime
= ip
->i_atime
= ip
->i_ctime
= CURRENT_TIME_SEC
;
410 zfs_inode_set_ops(zsb
, ip
);
412 if (insert_inode_locked(ip
))
415 mutex_enter(&zsb
->z_znodes_lock
);
416 list_insert_tail(&zsb
->z_all_znodes
, zp
);
418 mutex_exit(&zsb
->z_znodes_lock
);
420 unlock_new_inode(ip
);
424 unlock_new_inode(ip
);
430 * Update the embedded inode given the znode. We should work toward
431 * eliminating this function as soon as possible by removing values
432 * which are duplicated between the znode and inode. If the generic
433 * inode has the correct field it should be used, and the ZFS code
434 * updated to access the inode. This can be done incrementally.
437 zfs_inode_update(znode_t
*zp
)
442 uint64_t atime
[2], mtime
[2], ctime
[2];
448 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
), &atime
, 16);
449 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MTIME(zsb
), &mtime
, 16);
450 sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CTIME(zsb
), &ctime
, 16);
452 spin_lock(&ip
->i_lock
);
453 ip
->i_generation
= zp
->z_gen
;
454 ip
->i_uid
= zp
->z_uid
;
455 ip
->i_gid
= zp
->z_gid
;
456 ip
->i_nlink
= zp
->z_links
;
457 ip
->i_mode
= zp
->z_mode
;
458 ip
->i_blkbits
= SPA_MINBLOCKSHIFT
;
459 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &blksize
,
460 (u_longlong_t
*)&ip
->i_blocks
);
462 ZFS_TIME_DECODE(&ip
->i_atime
, atime
);
463 ZFS_TIME_DECODE(&ip
->i_mtime
, mtime
);
464 ZFS_TIME_DECODE(&ip
->i_ctime
, ctime
);
466 i_size_write(ip
, zp
->z_size
);
467 spin_unlock(&ip
->i_lock
);
470 static uint64_t empty_xattr
;
471 static uint64_t pad
[4];
472 static zfs_acl_phys_t acl_phys
;
474 * Create a new DMU object to hold a zfs znode.
476 * IN: dzp - parent directory for new znode
477 * vap - file attributes for new znode
478 * tx - dmu transaction id for zap operations
479 * cr - credentials of caller
481 * IS_ROOT_NODE - new object will be root
482 * IS_XATTR - new object is an attribute
483 * bonuslen - length of bonus buffer
484 * setaclp - File/Dir initial ACL
485 * fuidp - Tracks fuid allocation.
487 * OUT: zpp - allocated znode
491 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
492 uint_t flag
, znode_t
**zpp
, zfs_acl_ids_t
*acl_ids
)
494 uint64_t crtime
[2], atime
[2], mtime
[2], ctime
[2];
495 uint64_t mode
, size
, links
, parent
, pflags
;
496 uint64_t dzp_pflags
= 0;
498 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
505 dmu_object_type_t obj_type
;
506 sa_bulk_attr_t
*sa_attrs
;
508 zfs_acl_locator_cb_t locate
= { 0 };
511 obj
= vap
->va_nodeid
;
512 now
= vap
->va_ctime
; /* see zfs_replay_create() */
513 gen
= vap
->va_nblocks
; /* ditto */
517 gen
= dmu_tx_get_txg(tx
);
520 obj_type
= zsb
->z_use_sa
? DMU_OT_SA
: DMU_OT_ZNODE
;
521 bonuslen
= (obj_type
== DMU_OT_SA
) ?
522 DN_MAX_BONUSLEN
: ZFS_OLD_ZNODE_PHYS_SIZE
;
525 * Create a new DMU object.
528 * There's currently no mechanism for pre-reading the blocks that will
529 * be needed to allocate a new object, so we accept the small chance
530 * that there will be an i/o error and we will fail one of the
533 if (S_ISDIR(vap
->va_mode
)) {
535 err
= zap_create_claim_norm(zsb
->z_os
, obj
,
536 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
537 obj_type
, bonuslen
, tx
);
538 ASSERT3U(err
, ==, 0);
540 obj
= zap_create_norm(zsb
->z_os
,
541 zsb
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
542 obj_type
, bonuslen
, tx
);
546 err
= dmu_object_claim(zsb
->z_os
, obj
,
547 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
548 obj_type
, bonuslen
, tx
);
549 ASSERT3U(err
, ==, 0);
551 obj
= dmu_object_alloc(zsb
->z_os
,
552 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
553 obj_type
, bonuslen
, tx
);
557 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
558 VERIFY(0 == sa_buf_hold(zsb
->z_os
, obj
, NULL
, &db
));
561 * If this is the root, fix up the half-initialized parent pointer
562 * to reference the just-allocated physical data area.
564 if (flag
& IS_ROOT_NODE
) {
567 dzp_pflags
= dzp
->z_pflags
;
571 * If parent is an xattr, so am I.
573 if (dzp_pflags
& ZFS_XATTR
) {
577 if (zsb
->z_use_fuids
)
578 pflags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
582 if (S_ISDIR(vap
->va_mode
)) {
583 size
= 2; /* contents ("." and "..") */
584 links
= (flag
& (IS_ROOT_NODE
| IS_XATTR
)) ? 2 : 1;
589 if (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
) ||
590 S_ISFIFO(vap
->va_mode
) || S_ISSOCK(vap
->va_mode
))
591 rdev
= zfs_expldev(vap
->va_rdev
);
594 mode
= acl_ids
->z_mode
;
599 * No execs denied will be deterimed when zfs_mode_compute() is called.
601 pflags
|= acl_ids
->z_aclp
->z_hints
&
602 (ZFS_ACL_TRIVIAL
|ZFS_INHERIT_ACE
|ZFS_ACL_AUTO_INHERIT
|
603 ZFS_ACL_DEFAULTED
|ZFS_ACL_PROTECTED
);
605 ZFS_TIME_ENCODE(&now
, crtime
);
606 ZFS_TIME_ENCODE(&now
, ctime
);
608 if (vap
->va_mask
& ATTR_ATIME
) {
609 ZFS_TIME_ENCODE(&vap
->va_atime
, atime
);
611 ZFS_TIME_ENCODE(&now
, atime
);
614 if (vap
->va_mask
& ATTR_MTIME
) {
615 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
617 ZFS_TIME_ENCODE(&now
, mtime
);
620 /* Now add in all of the "SA" attributes */
621 VERIFY(0 == sa_handle_get_from_db(zsb
->z_os
, db
, NULL
, SA_HDL_SHARED
,
625 * Setup the array of attributes to be replaced/set on the new file
627 * order for DMU_OT_ZNODE is critical since it needs to be constructed
628 * in the old znode_phys_t format. Don't change this ordering
630 sa_attrs
= kmem_alloc(sizeof(sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
632 if (obj_type
== DMU_OT_ZNODE
) {
633 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
635 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
637 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
639 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
641 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
643 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
645 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
647 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
650 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MODE(zsb
),
652 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_SIZE(zsb
),
654 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GEN(zsb
),
656 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
),
657 NULL
, &acl_ids
->z_fuid
, 8);
658 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
),
659 NULL
, &acl_ids
->z_fgid
, 8);
660 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PARENT(zsb
),
662 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
664 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ATIME(zsb
),
666 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_MTIME(zsb
),
668 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CTIME(zsb
),
670 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_CRTIME(zsb
),
674 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_LINKS(zsb
), NULL
, &links
, 8);
676 if (obj_type
== DMU_OT_ZNODE
) {
677 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_XATTR(zsb
), NULL
,
680 if (obj_type
== DMU_OT_ZNODE
||
681 (S_ISBLK(vap
->va_mode
) || S_ISCHR(vap
->va_mode
) ||
682 S_ISFIFO(vap
->va_mode
) || S_ISSOCK(vap
->va_mode
))) {
683 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_RDEV(zsb
),
686 if (obj_type
== DMU_OT_ZNODE
) {
687 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_FLAGS(zsb
),
689 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_UID(zsb
), NULL
,
690 &acl_ids
->z_fuid
, 8);
691 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_GID(zsb
), NULL
,
692 &acl_ids
->z_fgid
, 8);
693 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_PAD(zsb
), NULL
, pad
,
694 sizeof (uint64_t) * 4);
695 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_ZNODE_ACL(zsb
), NULL
,
696 &acl_phys
, sizeof (zfs_acl_phys_t
));
697 } else if (acl_ids
->z_aclp
->z_version
>= ZFS_ACL_VERSION_FUID
) {
698 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_COUNT(zsb
), NULL
,
699 &acl_ids
->z_aclp
->z_acl_count
, 8);
700 locate
.cb_aclp
= acl_ids
->z_aclp
;
701 SA_ADD_BULK_ATTR(sa_attrs
, cnt
, SA_ZPL_DACL_ACES(zsb
),
702 zfs_acl_data_locator
, &locate
,
703 acl_ids
->z_aclp
->z_acl_bytes
);
704 mode
= zfs_mode_compute(mode
, acl_ids
->z_aclp
, &pflags
,
705 acl_ids
->z_fuid
, acl_ids
->z_fgid
);
708 VERIFY(sa_replace_all_by_template(sa_hdl
, sa_attrs
, cnt
, tx
) == 0);
710 if (!(flag
& IS_ROOT_NODE
)) {
711 *zpp
= zfs_znode_alloc(zsb
, db
, 0, obj_type
, obj
, sa_hdl
);
712 ASSERT(*zpp
!= NULL
);
714 err
= zpl_xattr_security_init(ZTOI(*zpp
), ZTOI(dzp
));
715 ASSERT3S(err
, ==, 0);
718 * If we are creating the root node, the "parent" we
719 * passed in is the znode for the root.
723 (*zpp
)->z_sa_hdl
= sa_hdl
;
726 (*zpp
)->z_pflags
= pflags
;
727 (*zpp
)->z_mode
= mode
;
729 if (obj_type
== DMU_OT_ZNODE
||
730 acl_ids
->z_aclp
->z_version
< ZFS_ACL_VERSION_FUID
) {
731 err
= zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
);
732 ASSERT3S(err
, ==, 0);
734 kmem_free(sa_attrs
, sizeof(sa_bulk_attr_t
) * ZPL_END
);
735 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
739 zfs_zget(zfs_sb_t
*zsb
, uint64_t obj_num
, znode_t
**zpp
)
741 dmu_object_info_t doi
;
749 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
751 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
753 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
757 dmu_object_info_from_db(db
, &doi
);
758 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
759 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
760 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
761 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
762 sa_buf_rele(db
, NULL
);
763 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
767 hdl
= dmu_buf_get_user(db
);
769 zp
= sa_get_userdata(hdl
);
773 * Since "SA" does immediate eviction we
774 * should never find a sa handle that doesn't
775 * know about the znode.
778 ASSERT3P(zp
, !=, NULL
);
780 mutex_enter(&zp
->z_lock
);
781 ASSERT3U(zp
->z_id
, ==, obj_num
);
782 if (zp
->z_unlinked
) {
789 sa_buf_rele(db
, NULL
);
790 mutex_exit(&zp
->z_lock
);
791 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
796 * Not found create new znode/vnode but only if file exists.
798 * There is a small window where zfs_vget() could
799 * find this object while a file create is still in
800 * progress. This is checked for in zfs_znode_alloc()
802 * if zfs_znode_alloc() fails it will drop the hold on the
805 zp
= zfs_znode_alloc(zsb
, db
, doi
.doi_data_block_size
,
806 doi
.doi_bonus_type
, obj_num
, NULL
);
812 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
817 zfs_rezget(znode_t
*zp
)
819 zfs_sb_t
*zsb
= ZTOZSB(zp
);
820 dmu_object_info_t doi
;
822 uint64_t obj_num
= zp
->z_id
;
824 sa_bulk_attr_t bulk
[8];
829 ZFS_OBJ_HOLD_ENTER(zsb
, obj_num
);
831 mutex_enter(&zp
->z_acl_lock
);
832 if (zp
->z_acl_cached
) {
833 zfs_acl_free(zp
->z_acl_cached
);
834 zp
->z_acl_cached
= NULL
;
837 mutex_exit(&zp
->z_acl_lock
);
838 ASSERT(zp
->z_sa_hdl
== NULL
);
839 err
= sa_buf_hold(zsb
->z_os
, obj_num
, NULL
, &db
);
841 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
845 dmu_object_info_from_db(db
, &doi
);
846 if (doi
.doi_bonus_type
!= DMU_OT_SA
&&
847 (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
848 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
849 doi
.doi_bonus_size
< sizeof (znode_phys_t
)))) {
850 sa_buf_rele(db
, NULL
);
851 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
855 zfs_znode_sa_init(zsb
, zp
, db
, doi
.doi_bonus_type
, NULL
);
857 /* reload cached values */
858 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zsb
), NULL
,
860 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
,
861 &zp
->z_size
, sizeof (zp
->z_size
));
862 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zsb
), NULL
,
863 &zp
->z_links
, sizeof (zp
->z_links
));
864 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
865 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
866 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
867 &zp
->z_atime
, sizeof (zp
->z_atime
));
868 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
869 &zp
->z_uid
, sizeof (zp
->z_uid
));
870 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
,
871 &zp
->z_gid
, sizeof (zp
->z_gid
));
872 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
873 &mode
, sizeof (mode
));
875 if (sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) {
876 zfs_znode_dmu_fini(zp
);
877 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
883 if (gen
!= zp
->z_gen
) {
884 zfs_znode_dmu_fini(zp
);
885 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
889 zp
->z_unlinked
= (zp
->z_links
== 0);
890 zp
->z_blksz
= doi
.doi_data_block_size
;
892 ZFS_OBJ_HOLD_EXIT(zsb
, obj_num
);
898 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
900 zfs_sb_t
*zsb
= ZTOZSB(zp
);
901 objset_t
*os
= zsb
->z_os
;
902 uint64_t obj
= zp
->z_id
;
903 uint64_t acl_obj
= zfs_external_acl(zp
);
905 ZFS_OBJ_HOLD_ENTER(zsb
, obj
);
907 VERIFY(!zp
->z_is_sa
);
908 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
910 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
911 zfs_znode_dmu_fini(zp
);
912 ZFS_OBJ_HOLD_EXIT(zsb
, obj
);
916 zfs_zinactive(znode_t
*zp
)
918 zfs_sb_t
*zsb
= ZTOZSB(zp
);
919 uint64_t z_id
= zp
->z_id
;
921 ASSERT(zp
->z_sa_hdl
);
924 * Don't allow a zfs_zget() while were trying to release this znode
926 ZFS_OBJ_HOLD_ENTER(zsb
, z_id
);
927 mutex_enter(&zp
->z_lock
);
930 * If this was the last reference to a file with no links,
931 * remove the file from the file system.
933 if (zp
->z_unlinked
) {
934 mutex_exit(&zp
->z_lock
);
935 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
940 mutex_exit(&zp
->z_lock
);
941 zfs_znode_dmu_fini(zp
);
942 ZFS_OBJ_HOLD_EXIT(zsb
, z_id
);
946 zfs_tstamp_update_setup(znode_t
*zp
, uint_t flag
, uint64_t mtime
[2],
947 uint64_t ctime
[2], boolean_t have_tx
)
953 if (have_tx
) { /* will sa_bulk_update happen really soon? */
954 zp
->z_atime_dirty
= 0;
957 zp
->z_atime_dirty
= 1;
960 if (flag
& ATTR_ATIME
) {
961 ZFS_TIME_ENCODE(&now
, zp
->z_atime
);
964 if (flag
& ATTR_MTIME
) {
965 ZFS_TIME_ENCODE(&now
, mtime
);
966 if (ZTOZSB(zp
)->z_use_fuids
) {
967 zp
->z_pflags
|= (ZFS_ARCHIVE
|
972 if (flag
& ATTR_CTIME
) {
973 ZFS_TIME_ENCODE(&now
, ctime
);
974 if (ZTOZSB(zp
)->z_use_fuids
)
975 zp
->z_pflags
|= ZFS_ARCHIVE
;
980 * Grow the block size for a file.
982 * IN: zp - znode of file to free data in.
983 * size - requested block size
984 * tx - open transaction.
986 * NOTE: this function assumes that the znode is write locked.
989 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
994 if (size
<= zp
->z_blksz
)
997 * If the file size is already greater than the current blocksize,
998 * we will not grow. If there is more than one block in a file,
999 * the blocksize cannot change.
1001 if (zp
->z_blksz
&& zp
->z_size
> zp
->z_blksz
)
1004 error
= dmu_object_set_blocksize(ZTOZSB(zp
)->z_os
, zp
->z_id
,
1007 if (error
== ENOTSUP
)
1009 ASSERT3U(error
, ==, 0);
1011 /* What blocksize did we actually get? */
1012 dmu_object_size_from_db(sa_get_db(zp
->z_sa_hdl
), &zp
->z_blksz
, &dummy
);
1017 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1018 * be calling back into the fs for a putpage(). E.g.: when truncating
1019 * a file, the pages being "thrown away* don't need to be written out.
1023 zfs_no_putpage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
, size_t *lenp
,
1024 int flags
, cred_t
*cr
)
1029 #endif /* HAVE_MMAP */
1032 * Increase the file length
1034 * IN: zp - znode of file to free data in.
1035 * end - new end-of-file
1037 * RETURN: 0 if success
1038 * error code if failure
1041 zfs_extend(znode_t
*zp
, uint64_t end
)
1043 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1050 * We will change zp_size, lock the whole file.
1052 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1055 * Nothing to do if file already at desired length.
1057 if (end
<= zp
->z_size
) {
1058 zfs_range_unlock(rl
);
1062 tx
= dmu_tx_create(zsb
->z_os
);
1063 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1064 zfs_sa_upgrade_txholds(tx
, zp
);
1065 if (end
> zp
->z_blksz
&&
1066 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zsb
->z_max_blksz
)) {
1068 * We are growing the file past the current block size.
1070 if (zp
->z_blksz
> ZTOZSB(zp
)->z_max_blksz
) {
1071 ASSERT(!ISP2(zp
->z_blksz
));
1072 newblksz
= MIN(end
, SPA_MAXBLOCKSIZE
);
1074 newblksz
= MIN(end
, ZTOZSB(zp
)->z_max_blksz
);
1076 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1081 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1083 if (error
== ERESTART
) {
1089 zfs_range_unlock(rl
);
1094 zfs_grow_blocksize(zp
, newblksz
, tx
);
1098 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(ZTOZSB(zp
)),
1099 &zp
->z_size
, sizeof (zp
->z_size
), tx
));
1101 zfs_range_unlock(rl
);
1109 * Free space in a file.
1111 * IN: zp - znode of file to free data in.
1112 * off - start of section to free.
1113 * len - length of section to free.
1115 * RETURN: 0 if success
1116 * error code if failure
1119 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1121 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1126 * Lock the range being freed.
1128 rl
= zfs_range_lock(zp
, off
, len
, RL_WRITER
);
1131 * Nothing to do if file already at desired length.
1133 if (off
>= zp
->z_size
) {
1134 zfs_range_unlock(rl
);
1138 if (off
+ len
> zp
->z_size
)
1139 len
= zp
->z_size
- off
;
1141 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, off
, len
);
1143 zfs_range_unlock(rl
);
1151 * IN: zp - znode of file to free data in.
1152 * end - new end-of-file.
1154 * RETURN: 0 if success
1155 * error code if failure
1158 zfs_trunc(znode_t
*zp
, uint64_t end
)
1160 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1164 sa_bulk_attr_t bulk
[2];
1168 * We will change zp_size, lock the whole file.
1170 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1173 * Nothing to do if file already at desired length.
1175 if (end
>= zp
->z_size
) {
1176 zfs_range_unlock(rl
);
1180 error
= dmu_free_long_range(zsb
->z_os
, zp
->z_id
, end
, -1);
1182 zfs_range_unlock(rl
);
1186 tx
= dmu_tx_create(zsb
->z_os
);
1187 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1188 zfs_sa_upgrade_txholds(tx
, zp
);
1189 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1191 if (error
== ERESTART
) {
1197 zfs_range_unlock(rl
);
1202 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
),
1203 NULL
, &zp
->z_size
, sizeof (zp
->z_size
));
1206 zp
->z_pflags
&= ~ZFS_SPARSE
;
1207 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1208 NULL
, &zp
->z_pflags
, 8);
1210 VERIFY(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
) == 0);
1214 zfs_range_unlock(rl
);
1220 * Free space in a file
1222 * IN: zp - znode of file to free data in.
1223 * off - start of range
1224 * len - end of range (0 => EOF)
1225 * flag - current file open mode flags.
1226 * log - TRUE if this action should be logged
1228 * RETURN: 0 if success
1229 * error code if failure
1232 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1234 #ifdef HAVE_MANDLOCKS
1235 struct inode
*ip
= ZTOI(zp
);
1236 #endif /* HAVE_MANDLOCKS */
1238 zfs_sb_t
*zsb
= ZTOZSB(zp
);
1239 zilog_t
*zilog
= zsb
->z_log
;
1241 uint64_t mtime
[2], ctime
[2];
1242 sa_bulk_attr_t bulk
[3];
1246 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
), &mode
,
1247 sizeof (mode
))) != 0)
1250 if (off
> zp
->z_size
) {
1251 error
= zfs_extend(zp
, off
+len
);
1252 if (error
== 0 && log
)
1258 #ifdef HAVE_MANDLOCKS
1260 * Check for any locks in the region to be freed.
1263 if (MANDLOCK(ip
, (mode_t
)mode
)) {
1264 uint64_t length
= (len
? len
: zp
->z_size
- off
);
1265 if (error
= chklock(ip
, FWRITE
, off
, length
, flag
, NULL
))
1268 #endif /* HAVE_MANDLOCKS */
1271 error
= zfs_trunc(zp
, off
);
1273 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1274 off
+ len
> zp
->z_size
)
1275 error
= zfs_extend(zp
, off
+len
);
1280 tx
= dmu_tx_create(zsb
->z_os
);
1281 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1282 zfs_sa_upgrade_txholds(tx
, zp
);
1283 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1285 if (error
== ERESTART
) {
1294 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, mtime
, 16);
1295 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, ctime
, 16);
1296 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
),
1297 NULL
, &zp
->z_pflags
, 8);
1298 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
, B_TRUE
);
1299 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
1302 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1305 zfs_inode_update(zp
);
1310 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1312 uint64_t moid
, obj
, sa_obj
, version
;
1321 * First attempt to create master node.
1324 * In an empty objset, there are no blocks to read and thus
1325 * there can be no i/o errors (which we assert below).
1327 moid
= MASTER_NODE_OBJ
;
1328 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1329 DMU_OT_NONE
, 0, tx
);
1333 * Set starting attributes.
1335 version
= zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)));
1337 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1338 /* For the moment we expect all zpl props to be uint64_ts */
1342 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1343 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1344 name
= nvpair_name(elem
);
1345 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1349 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1352 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1355 ASSERT(version
!= 0);
1356 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1359 * Create zap object used for SA attribute registration
1362 if (version
>= ZPL_VERSION_SA
) {
1363 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1364 DMU_OT_NONE
, 0, tx
);
1365 error
= zap_add(os
, moid
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1371 * Create a delete queue.
1373 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1375 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1379 * Create root znode with code free of VFS dependencies. This
1380 * is important because without a registered filesystem and super
1381 * block all the required VFS hooks will be missing. The critical
1382 * thing is to just crete the required root znode.
1384 obj
= zap_create_norm(os
, norm
, DMU_OT_DIRECTORY_CONTENTS
,
1385 DMU_OT_ZNODE
, sizeof (znode_phys_t
), tx
);
1387 VERIFY(0 == dmu_bonus_hold(os
, obj
, FTAG
, &db
));
1388 dmu_buf_will_dirty(db
, tx
);
1391 * Initialize the znode physical data to zero.
1393 ASSERT(db
->db_size
>= sizeof (znode_phys_t
));
1394 bzero(db
->db_data
, db
->db_size
);
1397 if (USE_FUIDS(version
, os
))
1398 pzp
->zp_flags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
1400 pzp
->zp_size
= 2; /* "." and ".." */
1402 pzp
->zp_parent
= obj
;
1403 pzp
->zp_gen
= dmu_tx_get_txg(tx
);
1404 pzp
->zp_mode
= S_IFDIR
| 0755;
1405 pzp
->zp_flags
= ZFS_ACL_TRIVIAL
;
1409 ZFS_TIME_ENCODE(&now
, pzp
->zp_crtime
);
1410 ZFS_TIME_ENCODE(&now
, pzp
->zp_ctime
);
1411 ZFS_TIME_ENCODE(&now
, pzp
->zp_atime
);
1412 ZFS_TIME_ENCODE(&now
, pzp
->zp_mtime
);
1414 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &obj
, tx
);
1417 dmu_buf_rele(db
, FTAG
);
1420 #endif /* _KERNEL */
1423 zfs_sa_setup(objset_t
*osp
, sa_attr_type_t
**sa_table
)
1425 uint64_t sa_obj
= 0;
1428 error
= zap_lookup(osp
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1429 if (error
!= 0 && error
!= ENOENT
)
1432 error
= sa_setup(osp
, sa_obj
, zfs_attr_table
, ZPL_END
, sa_table
);
1437 zfs_grab_sa_handle(objset_t
*osp
, uint64_t obj
, sa_handle_t
**hdlp
,
1440 dmu_object_info_t doi
;
1443 if ((error
= sa_buf_hold(osp
, obj
, FTAG
, db
)) != 0)
1446 dmu_object_info_from_db(*db
, &doi
);
1447 if ((doi
.doi_bonus_type
!= DMU_OT_SA
&&
1448 doi
.doi_bonus_type
!= DMU_OT_ZNODE
) ||
1449 (doi
.doi_bonus_type
== DMU_OT_ZNODE
&&
1450 doi
.doi_bonus_size
< sizeof (znode_phys_t
))) {
1451 sa_buf_rele(*db
, FTAG
);
1455 error
= sa_handle_get(osp
, obj
, NULL
, SA_HDL_PRIVATE
, hdlp
);
1457 sa_buf_rele(*db
, FTAG
);
1465 zfs_release_sa_handle(sa_handle_t
*hdl
, dmu_buf_t
*db
)
1467 sa_handle_destroy(hdl
);
1468 sa_buf_rele(db
, FTAG
);
1472 * Given an object number, return its parent object number and whether
1473 * or not the object is an extended attribute directory.
1476 zfs_obj_to_pobj(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
, uint64_t *pobjp
,
1482 sa_bulk_attr_t bulk
[3];
1486 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_PARENT
], NULL
,
1487 &parent
, sizeof (parent
));
1488 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_FLAGS
], NULL
,
1489 &pflags
, sizeof (pflags
));
1490 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1491 &mode
, sizeof (mode
));
1493 if ((error
= sa_bulk_lookup(hdl
, bulk
, count
)) != 0)
1497 *is_xattrdir
= ((pflags
& ZFS_XATTR
) != 0) && S_ISDIR(mode
);
1503 * Given an object number, return some zpl level statistics
1506 zfs_obj_to_stats_impl(sa_handle_t
*hdl
, sa_attr_type_t
*sa_table
,
1509 sa_bulk_attr_t bulk
[4];
1512 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_MODE
], NULL
,
1513 &sb
->zs_mode
, sizeof (sb
->zs_mode
));
1514 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_GEN
], NULL
,
1515 &sb
->zs_gen
, sizeof (sb
->zs_gen
));
1516 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_LINKS
], NULL
,
1517 &sb
->zs_links
, sizeof (sb
->zs_links
));
1518 SA_ADD_BULK_ATTR(bulk
, count
, sa_table
[ZPL_CTIME
], NULL
,
1519 &sb
->zs_ctime
, sizeof (sb
->zs_ctime
));
1521 return (sa_bulk_lookup(hdl
, bulk
, count
));
1525 zfs_obj_to_path_impl(objset_t
*osp
, uint64_t obj
, sa_handle_t
*hdl
,
1526 sa_attr_type_t
*sa_table
, char *buf
, int len
)
1528 sa_handle_t
*sa_hdl
;
1529 sa_handle_t
*prevhdl
= NULL
;
1530 dmu_buf_t
*prevdb
= NULL
;
1531 dmu_buf_t
*sa_db
= NULL
;
1532 char *path
= buf
+ len
- 1;
1540 char component
[MAXNAMELEN
+ 2];
1545 zfs_release_sa_handle(prevhdl
, prevdb
);
1547 if ((error
= zfs_obj_to_pobj(sa_hdl
, sa_table
, &pobj
,
1548 &is_xattrdir
)) != 0)
1559 (void) sprintf(component
+ 1, "<xattrdir>");
1561 error
= zap_value_search(osp
, pobj
, obj
,
1562 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
1567 complen
= strlen(component
);
1569 ASSERT(path
>= buf
);
1570 bcopy(component
, path
, complen
);
1573 if (sa_hdl
!= hdl
) {
1577 error
= zfs_grab_sa_handle(osp
, obj
, &sa_hdl
, &sa_db
);
1585 if (sa_hdl
!= NULL
&& sa_hdl
!= hdl
) {
1586 ASSERT(sa_db
!= NULL
);
1587 zfs_release_sa_handle(sa_hdl
, sa_db
);
1591 (void) memmove(buf
, path
, buf
+ len
- path
);
1597 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
1599 sa_attr_type_t
*sa_table
;
1604 error
= zfs_sa_setup(osp
, &sa_table
);
1608 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
);
1612 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1614 zfs_release_sa_handle(hdl
, db
);
1619 zfs_obj_to_stats(objset_t
*osp
, uint64_t obj
, zfs_stat_t
*sb
,
1622 char *path
= buf
+ len
- 1;
1623 sa_attr_type_t
*sa_table
;
1630 error
= zfs_sa_setup(osp
, &sa_table
);
1634 error
= zfs_grab_sa_handle(osp
, obj
, &hdl
, &db
);
1638 error
= zfs_obj_to_stats_impl(hdl
, sa_table
, sb
);
1640 zfs_release_sa_handle(hdl
, db
);
1644 error
= zfs_obj_to_path_impl(osp
, obj
, hdl
, sa_table
, buf
, len
);
1646 zfs_release_sa_handle(hdl
, db
);
1650 #if defined(_KERNEL) && defined(HAVE_SPL)
1651 EXPORT_SYMBOL(zfs_create_fs
);
1652 EXPORT_SYMBOL(zfs_obj_to_path
);