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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Portions Copyright 2007 Jeremy Teo */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/mntent.h>
36 #include <sys/mkdev.h>
37 #include <sys/u8_textprep.h>
38 #include <sys/dsl_dataset.h>
40 #include <sys/vfs_opreg.h>
41 #include <sys/vnode.h>
44 #include <sys/errno.h>
45 #include <sys/unistd.h>
47 #include <sys/atomic.h>
49 #include "fs/fs_subr.h"
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_rlock.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/kidmap.h>
60 #include <sys/refcount.h>
63 #include <sys/zfs_znode.h>
68 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
69 * turned on when DEBUG is also defined.
76 #define ZNODE_STAT_ADD(stat) ((stat)++)
78 #define ZNODE_STAT_ADD(stat) /* nothing */
79 #endif /* ZNODE_STATS */
81 #define POINTER_IS_VALID(p) (!((uintptr_t)(p) & 0x3))
82 #define POINTER_INVALIDATE(pp) (*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
85 * Functions needed for userland (ie: libzpool) are not put under
86 * #ifdef_KERNEL; the rest of the functions have dependencies
87 * (such as VFS logic) that will not compile easily in userland.
91 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
92 * be freed before it can be safely accessed.
94 krwlock_t zfsvfs_lock
;
96 static kmem_cache_t
*znode_cache
= NULL
;
100 znode_evict_error(dmu_buf_t
*dbuf
, void *user_ptr
)
103 * We should never drop all dbuf refs without first clearing
104 * the eviction callback.
106 panic("evicting znode %p\n", user_ptr
);
111 zfs_znode_cache_constructor(void *buf
, void *arg
, int kmflags
)
115 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
));
117 zp
->z_vnode
= vn_alloc(kmflags
);
118 if (zp
->z_vnode
== NULL
) {
121 ZTOV(zp
)->v_data
= zp
;
123 list_link_init(&zp
->z_link_node
);
125 mutex_init(&zp
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
126 rw_init(&zp
->z_parent_lock
, NULL
, RW_DEFAULT
, NULL
);
127 rw_init(&zp
->z_name_lock
, NULL
, RW_DEFAULT
, NULL
);
128 mutex_init(&zp
->z_acl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
130 mutex_init(&zp
->z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
131 avl_create(&zp
->z_range_avl
, zfs_range_compare
,
132 sizeof (rl_t
), offsetof(rl_t
, r_node
));
135 zp
->z_dirlocks
= NULL
;
136 zp
->z_acl_cached
= NULL
;
142 zfs_znode_cache_destructor(void *buf
, void *arg
)
146 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
));
147 ASSERT(ZTOV(zp
)->v_data
== zp
);
149 ASSERT(!list_link_active(&zp
->z_link_node
));
150 mutex_destroy(&zp
->z_lock
);
151 rw_destroy(&zp
->z_parent_lock
);
152 rw_destroy(&zp
->z_name_lock
);
153 mutex_destroy(&zp
->z_acl_lock
);
154 avl_destroy(&zp
->z_range_avl
);
155 mutex_destroy(&zp
->z_range_lock
);
157 ASSERT(zp
->z_dbuf
== NULL
);
158 ASSERT(zp
->z_dirlocks
== NULL
);
159 ASSERT(zp
->z_acl_cached
== NULL
);
164 uint64_t zms_zfsvfs_invalid
;
165 uint64_t zms_zfsvfs_recheck1
;
166 uint64_t zms_zfsvfs_unmounted
;
167 uint64_t zms_zfsvfs_recheck2
;
168 uint64_t zms_obj_held
;
169 uint64_t zms_vnode_locked
;
170 uint64_t zms_not_only_dnlc
;
172 #endif /* ZNODE_STATS */
175 zfs_znode_move_impl(znode_t
*ozp
, znode_t
*nzp
)
180 nzp
->z_zfsvfs
= ozp
->z_zfsvfs
;
184 nzp
->z_vnode
= ozp
->z_vnode
;
185 ozp
->z_vnode
= vp
; /* let destructor free the overwritten vnode */
186 ZTOV(ozp
)->v_data
= ozp
;
187 ZTOV(nzp
)->v_data
= nzp
;
189 nzp
->z_id
= ozp
->z_id
;
190 ASSERT(ozp
->z_dirlocks
== NULL
); /* znode not in use */
191 ASSERT(avl_numnodes(&ozp
->z_range_avl
) == 0);
192 nzp
->z_unlinked
= ozp
->z_unlinked
;
193 nzp
->z_atime_dirty
= ozp
->z_atime_dirty
;
194 nzp
->z_zn_prefetch
= ozp
->z_zn_prefetch
;
195 nzp
->z_blksz
= ozp
->z_blksz
;
196 nzp
->z_seq
= ozp
->z_seq
;
197 nzp
->z_mapcnt
= ozp
->z_mapcnt
;
198 nzp
->z_last_itx
= ozp
->z_last_itx
;
199 nzp
->z_gen
= ozp
->z_gen
;
200 nzp
->z_sync_cnt
= ozp
->z_sync_cnt
;
201 nzp
->z_phys
= ozp
->z_phys
;
202 nzp
->z_dbuf
= ozp
->z_dbuf
;
205 * Release any cached ACL, since it *may* have
206 * zfs_acl_node_t's that directly references an
207 * embedded ACL in the zp_acl of the old znode_phys_t
209 * It will be recached the next time the ACL is needed.
211 if (ozp
->z_acl_cached
) {
212 zfs_acl_free(ozp
->z_acl_cached
);
213 ozp
->z_acl_cached
= NULL
;
216 /* Update back pointers. */
217 (void) dmu_buf_update_user(nzp
->z_dbuf
, ozp
, nzp
, &nzp
->z_phys
,
221 * Invalidate the original znode by clearing fields that provide a
222 * pointer back to the znode. Set the low bit of the vfs pointer to
223 * ensure that zfs_znode_move() recognizes the znode as invalid in any
224 * subsequent callback.
227 POINTER_INVALIDATE(&ozp
->z_zfsvfs
);
232 zfs_znode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
234 znode_t
*ozp
= buf
, *nzp
= newbuf
;
239 * The znode is on the file system's list of known znodes if the vfs
240 * pointer is valid. We set the low bit of the vfs pointer when freeing
241 * the znode to invalidate it, and the memory patterns written by kmem
242 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
243 * created znode sets the vfs pointer last of all to indicate that the
244 * znode is known and in a valid state to be moved by this function.
246 zfsvfs
= ozp
->z_zfsvfs
;
247 if (!POINTER_IS_VALID(zfsvfs
)) {
248 ZNODE_STAT_ADD(znode_move_stats
.zms_zfsvfs_invalid
);
249 return (KMEM_CBRC_DONT_KNOW
);
253 * Close a small window in which it's possible that the filesystem could
254 * be unmounted and freed, and zfsvfs, though valid in the previous
255 * statement, could point to unrelated memory by the time we try to
256 * prevent the filesystem from being unmounted.
258 rw_enter(&zfsvfs_lock
, RW_WRITER
);
259 if (zfsvfs
!= ozp
->z_zfsvfs
) {
260 rw_exit(&zfsvfs_lock
);
261 ZNODE_STAT_ADD(znode_move_stats
.zms_zfsvfs_recheck1
);
262 return (KMEM_CBRC_DONT_KNOW
);
266 * If the znode is still valid, then so is the file system. We know that
267 * no valid file system can be freed while we hold zfsvfs_lock, so we
268 * can safely ensure that the filesystem is not and will not be
269 * unmounted. The next statement is equivalent to ZFS_ENTER().
271 rrw_enter(&zfsvfs
->z_teardown_lock
, RW_READER
, FTAG
);
272 if (zfsvfs
->z_unmounted
) {
274 rw_exit(&zfsvfs_lock
);
275 ZNODE_STAT_ADD(znode_move_stats
.zms_zfsvfs_unmounted
);
276 return (KMEM_CBRC_DONT_KNOW
);
278 rw_exit(&zfsvfs_lock
);
280 mutex_enter(&zfsvfs
->z_znodes_lock
);
282 * Recheck the vfs pointer in case the znode was removed just before
283 * acquiring the lock.
285 if (zfsvfs
!= ozp
->z_zfsvfs
) {
286 mutex_exit(&zfsvfs
->z_znodes_lock
);
288 ZNODE_STAT_ADD(znode_move_stats
.zms_zfsvfs_recheck2
);
289 return (KMEM_CBRC_DONT_KNOW
);
293 * At this point we know that as long as we hold z_znodes_lock, the
294 * znode cannot be freed and fields within the znode can be safely
295 * accessed. Now, prevent a race with zfs_zget().
297 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs
, ozp
->z_id
) == 0) {
298 mutex_exit(&zfsvfs
->z_znodes_lock
);
300 ZNODE_STAT_ADD(znode_move_stats
.zms_obj_held
);
301 return (KMEM_CBRC_LATER
);
305 if (mutex_tryenter(&vp
->v_lock
) == 0) {
306 ZFS_OBJ_HOLD_EXIT(zfsvfs
, ozp
->z_id
);
307 mutex_exit(&zfsvfs
->z_znodes_lock
);
309 ZNODE_STAT_ADD(znode_move_stats
.zms_vnode_locked
);
310 return (KMEM_CBRC_LATER
);
313 /* Only move znodes that are referenced _only_ by the DNLC. */
314 if (vp
->v_count
!= 1 || !vn_in_dnlc(vp
)) {
315 mutex_exit(&vp
->v_lock
);
316 ZFS_OBJ_HOLD_EXIT(zfsvfs
, ozp
->z_id
);
317 mutex_exit(&zfsvfs
->z_znodes_lock
);
319 ZNODE_STAT_ADD(znode_move_stats
.zms_not_only_dnlc
);
320 return (KMEM_CBRC_LATER
);
324 * The znode is known and in a valid state to move. We're holding the
325 * locks needed to execute the critical section.
327 zfs_znode_move_impl(ozp
, nzp
);
328 mutex_exit(&vp
->v_lock
);
329 ZFS_OBJ_HOLD_EXIT(zfsvfs
, ozp
->z_id
);
331 list_link_replace(&ozp
->z_link_node
, &nzp
->z_link_node
);
332 mutex_exit(&zfsvfs
->z_znodes_lock
);
335 return (KMEM_CBRC_YES
);
344 rw_init(&zfsvfs_lock
, NULL
, RW_DEFAULT
, NULL
);
345 ASSERT(znode_cache
== NULL
);
346 znode_cache
= kmem_cache_create("zfs_znode_cache",
347 sizeof (znode_t
), 0, zfs_znode_cache_constructor
,
348 zfs_znode_cache_destructor
, NULL
, NULL
, NULL
, 0);
349 kmem_cache_set_move(znode_cache
, zfs_znode_move
);
356 * Cleanup vfs & vnode ops
358 zfs_remove_op_tables();
364 kmem_cache_destroy(znode_cache
);
366 rw_destroy(&zfsvfs_lock
);
369 struct vnodeops
*zfs_dvnodeops
;
370 struct vnodeops
*zfs_fvnodeops
;
371 struct vnodeops
*zfs_symvnodeops
;
372 struct vnodeops
*zfs_xdvnodeops
;
373 struct vnodeops
*zfs_evnodeops
;
374 struct vnodeops
*zfs_sharevnodeops
;
377 zfs_remove_op_tables()
383 (void) vfs_freevfsops_by_type(zfsfstype
);
390 vn_freevnodeops(zfs_dvnodeops
);
392 vn_freevnodeops(zfs_fvnodeops
);
394 vn_freevnodeops(zfs_symvnodeops
);
396 vn_freevnodeops(zfs_xdvnodeops
);
398 vn_freevnodeops(zfs_evnodeops
);
399 if (zfs_sharevnodeops
)
400 vn_freevnodeops(zfs_sharevnodeops
);
402 zfs_dvnodeops
= NULL
;
403 zfs_fvnodeops
= NULL
;
404 zfs_symvnodeops
= NULL
;
405 zfs_xdvnodeops
= NULL
;
406 zfs_evnodeops
= NULL
;
407 zfs_sharevnodeops
= NULL
;
410 extern const fs_operation_def_t zfs_dvnodeops_template
[];
411 extern const fs_operation_def_t zfs_fvnodeops_template
[];
412 extern const fs_operation_def_t zfs_xdvnodeops_template
[];
413 extern const fs_operation_def_t zfs_symvnodeops_template
[];
414 extern const fs_operation_def_t zfs_evnodeops_template
[];
415 extern const fs_operation_def_t zfs_sharevnodeops_template
[];
418 zfs_create_op_tables()
423 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
424 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
425 * In this case we just return as the ops vectors are already set up.
430 error
= vn_make_ops(MNTTYPE_ZFS
, zfs_dvnodeops_template
,
435 error
= vn_make_ops(MNTTYPE_ZFS
, zfs_fvnodeops_template
,
440 error
= vn_make_ops(MNTTYPE_ZFS
, zfs_symvnodeops_template
,
445 error
= vn_make_ops(MNTTYPE_ZFS
, zfs_xdvnodeops_template
,
450 error
= vn_make_ops(MNTTYPE_ZFS
, zfs_evnodeops_template
,
455 error
= vn_make_ops(MNTTYPE_ZFS
, zfs_sharevnodeops_template
,
462 zfs_create_share_dir(zfsvfs_t
*zfsvfs
, dmu_tx_t
*tx
)
464 zfs_acl_ids_t acl_ids
;
471 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
472 vattr
.va_type
= VDIR
;
473 vattr
.va_mode
= S_IFDIR
|0555;
474 vattr
.va_uid
= crgetuid(kcred
);
475 vattr
.va_gid
= crgetgid(kcred
);
477 sharezp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
478 sharezp
->z_unlinked
= 0;
479 sharezp
->z_atime_dirty
= 0;
480 sharezp
->z_zfsvfs
= zfsvfs
;
486 VERIFY(0 == zfs_acl_ids_create(sharezp
, IS_ROOT_NODE
, &vattr
,
487 kcred
, NULL
, &acl_ids
));
488 zfs_mknode(sharezp
, &vattr
, tx
, kcred
, IS_ROOT_NODE
,
490 ASSERT3P(zp
, ==, sharezp
);
491 ASSERT(!vn_in_dnlc(ZTOV(sharezp
))); /* not valid to move */
492 POINTER_INVALIDATE(&sharezp
->z_zfsvfs
);
493 error
= zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
494 ZFS_SHARES_DIR
, 8, 1, &sharezp
->z_id
, tx
);
495 zfsvfs
->z_shares_dir
= sharezp
->z_id
;
497 zfs_acl_ids_free(&acl_ids
);
498 ZTOV(sharezp
)->v_count
= 0;
499 dmu_buf_rele(sharezp
->z_dbuf
, NULL
);
500 sharezp
->z_dbuf
= NULL
;
501 kmem_cache_free(znode_cache
, sharezp
);
507 * define a couple of values we need available
508 * for both 64 and 32 bit environments.
511 #define NBITSMINOR64 32
514 #define MAXMAJ64 0xffffffffUL
517 #define MAXMIN64 0xffffffffUL
521 * Create special expldev for ZFS private use.
522 * Can't use standard expldev since it doesn't do
523 * what we want. The standard expldev() takes a
524 * dev32_t in LP64 and expands it to a long dev_t.
525 * We need an interface that takes a dev32_t in ILP32
526 * and expands it to a long dev_t.
529 zfs_expldev(dev_t dev
)
532 major_t major
= (major_t
)dev
>> NBITSMINOR32
& MAXMAJ32
;
533 return (((uint64_t)major
<< NBITSMINOR64
) |
534 ((minor_t
)dev
& MAXMIN32
));
541 * Special cmpldev for ZFS private use.
542 * Can't use standard cmpldev since it takes
543 * a long dev_t and compresses it to dev32_t in
544 * LP64. We need to do a compaction of a long dev_t
545 * to a dev32_t in ILP32.
548 zfs_cmpldev(uint64_t dev
)
551 minor_t minor
= (minor_t
)dev
& MAXMIN64
;
552 major_t major
= (major_t
)(dev
>> NBITSMINOR64
) & MAXMAJ64
;
554 if (major
> MAXMAJ32
|| minor
> MAXMIN32
)
557 return (((dev32_t
)major
<< NBITSMINOR32
) | minor
);
564 zfs_znode_dmu_init(zfsvfs_t
*zfsvfs
, znode_t
*zp
, dmu_buf_t
*db
)
568 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
) || (zfsvfs
== zp
->z_zfsvfs
));
569 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs
, zp
->z_id
)));
571 mutex_enter(&zp
->z_lock
);
573 ASSERT(zp
->z_dbuf
== NULL
);
575 nzp
= dmu_buf_set_user_ie(db
, zp
, &zp
->z_phys
, znode_evict_error
);
579 * concurrent zgets on this object.
582 panic("existing znode %p for dbuf %p", (void *)nzp
, (void *)db
);
585 * Slap on VROOT if we are the root znode
587 if (zp
->z_id
== zfsvfs
->z_root
)
588 ZTOV(zp
)->v_flag
|= VROOT
;
590 mutex_exit(&zp
->z_lock
);
595 zfs_znode_dmu_fini(znode_t
*zp
)
597 dmu_buf_t
*db
= zp
->z_dbuf
;
598 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp
->z_zfsvfs
, zp
->z_id
)) ||
600 RW_WRITE_HELD(&zp
->z_zfsvfs
->z_teardown_inactive_lock
));
601 ASSERT(zp
->z_dbuf
!= NULL
);
603 VERIFY(zp
== dmu_buf_update_user(db
, zp
, NULL
, NULL
, NULL
));
604 dmu_buf_rele(db
, NULL
);
608 * Construct a new znode/vnode and intialize.
610 * This does not do a call to dmu_set_user() that is
611 * up to the caller to do, in case you don't want to
615 zfs_znode_alloc(zfsvfs_t
*zfsvfs
, dmu_buf_t
*db
, int blksz
)
620 zp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
622 ASSERT(zp
->z_dirlocks
== NULL
);
623 ASSERT(zp
->z_dbuf
== NULL
);
624 ASSERT(!POINTER_IS_VALID(zp
->z_zfsvfs
));
627 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
628 * the zfs_znode_move() callback.
632 zp
->z_atime_dirty
= 0;
635 zp
->z_id
= db
->db_object
;
637 zp
->z_seq
= 0x7A4653;
643 zfs_znode_dmu_init(zfsvfs
, zp
, db
);
645 zp
->z_gen
= zp
->z_phys
->zp_gen
;
647 vp
->v_vfsp
= zfsvfs
->z_parent
->z_vfs
;
648 vp
->v_type
= IFTOVT((mode_t
)zp
->z_phys
->zp_mode
);
650 switch (vp
->v_type
) {
652 if (zp
->z_phys
->zp_flags
& ZFS_XATTR
) {
653 vn_setops(vp
, zfs_xdvnodeops
);
654 vp
->v_flag
|= V_XATTRDIR
;
656 vn_setops(vp
, zfs_dvnodeops
);
658 zp
->z_zn_prefetch
= B_TRUE
; /* z_prefetch default is enabled */
662 vp
->v_rdev
= zfs_cmpldev(zp
->z_phys
->zp_rdev
);
667 vn_setops(vp
, zfs_fvnodeops
);
670 vp
->v_flag
|= VMODSORT
;
671 if (zp
->z_phys
->zp_parent
== zfsvfs
->z_shares_dir
)
672 vn_setops(vp
, zfs_sharevnodeops
);
674 vn_setops(vp
, zfs_fvnodeops
);
677 vn_setops(vp
, zfs_symvnodeops
);
680 vn_setops(vp
, zfs_evnodeops
);
684 mutex_enter(&zfsvfs
->z_znodes_lock
);
685 list_insert_tail(&zfsvfs
->z_all_znodes
, zp
);
688 * Everything else must be valid before assigning z_zfsvfs makes the
689 * znode eligible for zfs_znode_move().
691 zp
->z_zfsvfs
= zfsvfs
;
692 mutex_exit(&zfsvfs
->z_znodes_lock
);
694 VFS_HOLD(zfsvfs
->z_vfs
);
699 * Create a new DMU object to hold a zfs znode.
701 * IN: dzp - parent directory for new znode
702 * vap - file attributes for new znode
703 * tx - dmu transaction id for zap operations
704 * cr - credentials of caller
706 * IS_ROOT_NODE - new object will be root
707 * IS_XATTR - new object is an attribute
708 * IS_REPLAY - intent log replay
709 * bonuslen - length of bonus buffer
710 * setaclp - File/Dir initial ACL
711 * fuidp - Tracks fuid allocation.
713 * OUT: zpp - allocated znode
717 zfs_mknode(znode_t
*dzp
, vattr_t
*vap
, dmu_tx_t
*tx
, cred_t
*cr
,
718 uint_t flag
, znode_t
**zpp
, int bonuslen
, zfs_acl_ids_t
*acl_ids
)
722 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
727 ASSERT(vap
&& (vap
->va_mask
& (AT_TYPE
|AT_MODE
)) == (AT_TYPE
|AT_MODE
));
729 if (zfsvfs
->z_replay
) {
730 obj
= vap
->va_nodeid
;
732 now
= vap
->va_ctime
; /* see zfs_replay_create() */
733 gen
= vap
->va_nblocks
; /* ditto */
737 gen
= dmu_tx_get_txg(tx
);
741 * Create a new DMU object.
744 * There's currently no mechanism for pre-reading the blocks that will
745 * be to needed allocate a new object, so we accept the small chance
746 * that there will be an i/o error and we will fail one of the
749 if (vap
->va_type
== VDIR
) {
750 if (flag
& IS_REPLAY
) {
751 err
= zap_create_claim_norm(zfsvfs
->z_os
, obj
,
752 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
753 DMU_OT_ZNODE
, sizeof (znode_phys_t
) + bonuslen
, tx
);
754 ASSERT3U(err
, ==, 0);
756 obj
= zap_create_norm(zfsvfs
->z_os
,
757 zfsvfs
->z_norm
, DMU_OT_DIRECTORY_CONTENTS
,
758 DMU_OT_ZNODE
, sizeof (znode_phys_t
) + bonuslen
, tx
);
761 if (flag
& IS_REPLAY
) {
762 err
= dmu_object_claim(zfsvfs
->z_os
, obj
,
763 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
764 DMU_OT_ZNODE
, sizeof (znode_phys_t
) + bonuslen
, tx
);
765 ASSERT3U(err
, ==, 0);
767 obj
= dmu_object_alloc(zfsvfs
->z_os
,
768 DMU_OT_PLAIN_FILE_CONTENTS
, 0,
769 DMU_OT_ZNODE
, sizeof (znode_phys_t
) + bonuslen
, tx
);
772 VERIFY(0 == dmu_bonus_hold(zfsvfs
->z_os
, obj
, NULL
, &db
));
773 dmu_buf_will_dirty(db
, tx
);
776 * Initialize the znode physical data to zero.
778 ASSERT(db
->db_size
>= sizeof (znode_phys_t
));
779 bzero(db
->db_data
, db
->db_size
);
783 * If this is the root, fix up the half-initialized parent pointer
784 * to reference the just-allocated physical data area.
786 if (flag
& IS_ROOT_NODE
) {
793 * If parent is an xattr, so am I.
795 if (dzp
->z_phys
->zp_flags
& ZFS_XATTR
)
798 if (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
) {
799 pzp
->zp_rdev
= zfs_expldev(vap
->va_rdev
);
802 if (zfsvfs
->z_use_fuids
)
803 pzp
->zp_flags
= ZFS_ARCHIVE
| ZFS_AV_MODIFIED
;
805 if (vap
->va_type
== VDIR
) {
806 pzp
->zp_size
= 2; /* contents ("." and "..") */
807 pzp
->zp_links
= (flag
& (IS_ROOT_NODE
| IS_XATTR
)) ? 2 : 1;
810 pzp
->zp_parent
= dzp
->z_id
;
812 pzp
->zp_flags
|= ZFS_XATTR
;
816 ZFS_TIME_ENCODE(&now
, pzp
->zp_crtime
);
817 ZFS_TIME_ENCODE(&now
, pzp
->zp_ctime
);
819 if (vap
->va_mask
& AT_ATIME
) {
820 ZFS_TIME_ENCODE(&vap
->va_atime
, pzp
->zp_atime
);
822 ZFS_TIME_ENCODE(&now
, pzp
->zp_atime
);
825 if (vap
->va_mask
& AT_MTIME
) {
826 ZFS_TIME_ENCODE(&vap
->va_mtime
, pzp
->zp_mtime
);
828 ZFS_TIME_ENCODE(&now
, pzp
->zp_mtime
);
831 pzp
->zp_mode
= MAKEIMODE(vap
->va_type
, vap
->va_mode
);
832 if (!(flag
& IS_ROOT_NODE
)) {
833 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj
);
834 *zpp
= zfs_znode_alloc(zfsvfs
, db
, 0);
835 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj
);
838 * If we are creating the root node, the "parent" we
839 * passed in is the znode for the root.
843 pzp
->zp_uid
= acl_ids
->z_fuid
;
844 pzp
->zp_gid
= acl_ids
->z_fgid
;
845 pzp
->zp_mode
= acl_ids
->z_mode
;
846 VERIFY(0 == zfs_aclset_common(*zpp
, acl_ids
->z_aclp
, cr
, tx
));
847 if (vap
->va_mask
& AT_XVATTR
)
848 zfs_xvattr_set(*zpp
, (xvattr_t
*)vap
);
852 zfs_xvattr_set(znode_t
*zp
, xvattr_t
*xvap
)
856 xoap
= xva_getxoptattr(xvap
);
859 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
860 ZFS_TIME_ENCODE(&xoap
->xoa_createtime
, zp
->z_phys
->zp_crtime
);
861 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
863 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
864 ZFS_ATTR_SET(zp
, ZFS_READONLY
, xoap
->xoa_readonly
);
865 XVA_SET_RTN(xvap
, XAT_READONLY
);
867 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
868 ZFS_ATTR_SET(zp
, ZFS_HIDDEN
, xoap
->xoa_hidden
);
869 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
871 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
872 ZFS_ATTR_SET(zp
, ZFS_SYSTEM
, xoap
->xoa_system
);
873 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
875 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
876 ZFS_ATTR_SET(zp
, ZFS_ARCHIVE
, xoap
->xoa_archive
);
877 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
879 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
880 ZFS_ATTR_SET(zp
, ZFS_IMMUTABLE
, xoap
->xoa_immutable
);
881 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
883 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
884 ZFS_ATTR_SET(zp
, ZFS_NOUNLINK
, xoap
->xoa_nounlink
);
885 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
887 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
888 ZFS_ATTR_SET(zp
, ZFS_APPENDONLY
, xoap
->xoa_appendonly
);
889 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
891 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
892 ZFS_ATTR_SET(zp
, ZFS_NODUMP
, xoap
->xoa_nodump
);
893 XVA_SET_RTN(xvap
, XAT_NODUMP
);
895 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
896 ZFS_ATTR_SET(zp
, ZFS_OPAQUE
, xoap
->xoa_opaque
);
897 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
899 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
900 ZFS_ATTR_SET(zp
, ZFS_AV_QUARANTINED
,
901 xoap
->xoa_av_quarantined
);
902 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
904 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
905 ZFS_ATTR_SET(zp
, ZFS_AV_MODIFIED
, xoap
->xoa_av_modified
);
906 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
908 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
909 (void) memcpy(zp
->z_phys
+ 1, xoap
->xoa_av_scanstamp
,
910 sizeof (xoap
->xoa_av_scanstamp
));
911 zp
->z_phys
->zp_flags
|= ZFS_BONUS_SCANSTAMP
;
912 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
917 zfs_zget(zfsvfs_t
*zfsvfs
, uint64_t obj_num
, znode_t
**zpp
)
919 dmu_object_info_t doi
;
926 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj_num
);
928 err
= dmu_bonus_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
930 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
934 dmu_object_info_from_db(db
, &doi
);
935 if (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
936 doi
.doi_bonus_size
< sizeof (znode_phys_t
)) {
937 dmu_buf_rele(db
, NULL
);
938 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
942 zp
= dmu_buf_get_user(db
);
944 mutex_enter(&zp
->z_lock
);
947 * Since we do immediate eviction of the z_dbuf, we
948 * should never find a dbuf with a znode that doesn't
949 * know about the dbuf.
951 ASSERT3P(zp
->z_dbuf
, ==, db
);
952 ASSERT3U(zp
->z_id
, ==, obj_num
);
953 if (zp
->z_unlinked
) {
960 dmu_buf_rele(db
, NULL
);
961 mutex_exit(&zp
->z_lock
);
962 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
967 * Not found create new znode/vnode
969 zp
= zfs_znode_alloc(zfsvfs
, db
, doi
.doi_data_block_size
);
970 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
976 zfs_rezget(znode_t
*zp
)
978 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
979 dmu_object_info_t doi
;
981 uint64_t obj_num
= zp
->z_id
;
984 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj_num
);
986 err
= dmu_bonus_hold(zfsvfs
->z_os
, obj_num
, NULL
, &db
);
988 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
992 dmu_object_info_from_db(db
, &doi
);
993 if (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
994 doi
.doi_bonus_size
< sizeof (znode_phys_t
)) {
995 dmu_buf_rele(db
, NULL
);
996 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1000 if (((znode_phys_t
*)db
->db_data
)->zp_gen
!= zp
->z_gen
) {
1001 dmu_buf_rele(db
, NULL
);
1002 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1006 zfs_znode_dmu_init(zfsvfs
, zp
, db
);
1007 zp
->z_unlinked
= (zp
->z_phys
->zp_links
== 0);
1008 zp
->z_blksz
= doi
.doi_data_block_size
;
1010 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj_num
);
1016 zfs_znode_delete(znode_t
*zp
, dmu_tx_t
*tx
)
1018 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1019 objset_t
*os
= zfsvfs
->z_os
;
1020 uint64_t obj
= zp
->z_id
;
1021 uint64_t acl_obj
= zp
->z_phys
->zp_acl
.z_acl_extern_obj
;
1023 ZFS_OBJ_HOLD_ENTER(zfsvfs
, obj
);
1025 VERIFY(0 == dmu_object_free(os
, acl_obj
, tx
));
1026 VERIFY(0 == dmu_object_free(os
, obj
, tx
));
1027 zfs_znode_dmu_fini(zp
);
1028 ZFS_OBJ_HOLD_EXIT(zfsvfs
, obj
);
1033 zfs_zinactive(znode_t
*zp
)
1035 vnode_t
*vp
= ZTOV(zp
);
1036 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1037 uint64_t z_id
= zp
->z_id
;
1039 ASSERT(zp
->z_dbuf
&& zp
->z_phys
);
1042 * Don't allow a zfs_zget() while were trying to release this znode
1044 ZFS_OBJ_HOLD_ENTER(zfsvfs
, z_id
);
1046 mutex_enter(&zp
->z_lock
);
1047 mutex_enter(&vp
->v_lock
);
1049 if (vp
->v_count
> 0 || vn_has_cached_data(vp
)) {
1051 * If the hold count is greater than zero, somebody has
1052 * obtained a new reference on this znode while we were
1053 * processing it here, so we are done. If we still have
1054 * mapped pages then we are also done, since we don't
1055 * want to inactivate the znode until the pages get pushed.
1057 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1058 * this seems like it would leave the znode hanging with
1059 * no chance to go inactive...
1061 mutex_exit(&vp
->v_lock
);
1062 mutex_exit(&zp
->z_lock
);
1063 ZFS_OBJ_HOLD_EXIT(zfsvfs
, z_id
);
1066 mutex_exit(&vp
->v_lock
);
1069 * If this was the last reference to a file with no links,
1070 * remove the file from the file system.
1072 if (zp
->z_unlinked
) {
1073 mutex_exit(&zp
->z_lock
);
1074 ZFS_OBJ_HOLD_EXIT(zfsvfs
, z_id
);
1078 mutex_exit(&zp
->z_lock
);
1079 zfs_znode_dmu_fini(zp
);
1080 ZFS_OBJ_HOLD_EXIT(zfsvfs
, z_id
);
1085 zfs_znode_free(znode_t
*zp
)
1087 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1089 vn_invalid(ZTOV(zp
));
1091 ASSERT(ZTOV(zp
)->v_count
== 0);
1093 mutex_enter(&zfsvfs
->z_znodes_lock
);
1094 POINTER_INVALIDATE(&zp
->z_zfsvfs
);
1095 list_remove(&zfsvfs
->z_all_znodes
, zp
);
1096 mutex_exit(&zfsvfs
->z_znodes_lock
);
1098 if (zp
->z_acl_cached
) {
1099 zfs_acl_free(zp
->z_acl_cached
);
1100 zp
->z_acl_cached
= NULL
;
1103 kmem_cache_free(znode_cache
, zp
);
1105 VFS_RELE(zfsvfs
->z_vfs
);
1109 zfs_time_stamper_locked(znode_t
*zp
, uint_t flag
, dmu_tx_t
*tx
)
1113 ASSERT(MUTEX_HELD(&zp
->z_lock
));
1118 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
1119 zp
->z_atime_dirty
= 0;
1122 zp
->z_atime_dirty
= 1;
1125 if (flag
& AT_ATIME
)
1126 ZFS_TIME_ENCODE(&now
, zp
->z_phys
->zp_atime
);
1128 if (flag
& AT_MTIME
) {
1129 ZFS_TIME_ENCODE(&now
, zp
->z_phys
->zp_mtime
);
1130 if (zp
->z_zfsvfs
->z_use_fuids
)
1131 zp
->z_phys
->zp_flags
|= (ZFS_ARCHIVE
| ZFS_AV_MODIFIED
);
1134 if (flag
& AT_CTIME
) {
1135 ZFS_TIME_ENCODE(&now
, zp
->z_phys
->zp_ctime
);
1136 if (zp
->z_zfsvfs
->z_use_fuids
)
1137 zp
->z_phys
->zp_flags
|= ZFS_ARCHIVE
;
1142 * Update the requested znode timestamps with the current time.
1143 * If we are in a transaction, then go ahead and mark the znode
1144 * dirty in the transaction so the timestamps will go to disk.
1145 * Otherwise, we will get pushed next time the znode is updated
1146 * in a transaction, or when this znode eventually goes inactive.
1149 * 1 - Only the ACCESS time is ever updated outside of a transaction.
1150 * 2 - Multiple consecutive updates will be collapsed into a single
1151 * znode update by the transaction grouping semantics of the DMU.
1154 zfs_time_stamper(znode_t
*zp
, uint_t flag
, dmu_tx_t
*tx
)
1156 mutex_enter(&zp
->z_lock
);
1157 zfs_time_stamper_locked(zp
, flag
, tx
);
1158 mutex_exit(&zp
->z_lock
);
1162 * Grow the block size for a file.
1164 * IN: zp - znode of file to free data in.
1165 * size - requested block size
1166 * tx - open transaction.
1168 * NOTE: this function assumes that the znode is write locked.
1171 zfs_grow_blocksize(znode_t
*zp
, uint64_t size
, dmu_tx_t
*tx
)
1176 if (size
<= zp
->z_blksz
)
1179 * If the file size is already greater than the current blocksize,
1180 * we will not grow. If there is more than one block in a file,
1181 * the blocksize cannot change.
1183 if (zp
->z_blksz
&& zp
->z_phys
->zp_size
> zp
->z_blksz
)
1186 error
= dmu_object_set_blocksize(zp
->z_zfsvfs
->z_os
, zp
->z_id
,
1188 if (error
== ENOTSUP
)
1190 ASSERT3U(error
, ==, 0);
1192 /* What blocksize did we actually get? */
1193 dmu_object_size_from_db(zp
->z_dbuf
, &zp
->z_blksz
, &dummy
);
1197 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1198 * be calling back into the fs for a putpage(). E.g.: when truncating
1199 * a file, the pages being "thrown away* don't need to be written out.
1203 zfs_no_putpage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
, size_t *lenp
,
1204 int flags
, cred_t
*cr
)
1211 * Increase the file length
1213 * IN: zp - znode of file to free data in.
1214 * end - new end-of-file
1216 * RETURN: 0 if success
1217 * error code if failure
1220 zfs_extend(znode_t
*zp
, uint64_t end
)
1222 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1229 * We will change zp_size, lock the whole file.
1231 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1234 * Nothing to do if file already at desired length.
1236 if (end
<= zp
->z_phys
->zp_size
) {
1237 zfs_range_unlock(rl
);
1241 tx
= dmu_tx_create(zfsvfs
->z_os
);
1242 dmu_tx_hold_bonus(tx
, zp
->z_id
);
1243 if (end
> zp
->z_blksz
&&
1244 (!ISP2(zp
->z_blksz
) || zp
->z_blksz
< zfsvfs
->z_max_blksz
)) {
1246 * We are growing the file past the current block size.
1248 if (zp
->z_blksz
> zp
->z_zfsvfs
->z_max_blksz
) {
1249 ASSERT(!ISP2(zp
->z_blksz
));
1250 newblksz
= MIN(end
, SPA_MAXBLOCKSIZE
);
1252 newblksz
= MIN(end
, zp
->z_zfsvfs
->z_max_blksz
);
1254 dmu_tx_hold_write(tx
, zp
->z_id
, 0, newblksz
);
1259 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1261 if (error
== ERESTART
) {
1267 zfs_range_unlock(rl
);
1270 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
1273 zfs_grow_blocksize(zp
, newblksz
, tx
);
1275 zp
->z_phys
->zp_size
= end
;
1277 zfs_range_unlock(rl
);
1285 * Free space in a file.
1287 * IN: zp - znode of file to free data in.
1288 * off - start of section to free.
1289 * len - length of section to free.
1291 * RETURN: 0 if success
1292 * error code if failure
1295 zfs_free_range(znode_t
*zp
, uint64_t off
, uint64_t len
)
1297 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1302 * Lock the range being freed.
1304 rl
= zfs_range_lock(zp
, off
, len
, RL_WRITER
);
1307 * Nothing to do if file already at desired length.
1309 if (off
>= zp
->z_phys
->zp_size
) {
1310 zfs_range_unlock(rl
);
1314 if (off
+ len
> zp
->z_phys
->zp_size
)
1315 len
= zp
->z_phys
->zp_size
- off
;
1317 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, off
, len
);
1319 zfs_range_unlock(rl
);
1327 * IN: zp - znode of file to free data in.
1328 * end - new end-of-file.
1330 * RETURN: 0 if success
1331 * error code if failure
1334 zfs_trunc(znode_t
*zp
, uint64_t end
)
1336 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1337 vnode_t
*vp
= ZTOV(zp
);
1343 * We will change zp_size, lock the whole file.
1345 rl
= zfs_range_lock(zp
, 0, UINT64_MAX
, RL_WRITER
);
1348 * Nothing to do if file already at desired length.
1350 if (end
>= zp
->z_phys
->zp_size
) {
1351 zfs_range_unlock(rl
);
1355 error
= dmu_free_long_range(zfsvfs
->z_os
, zp
->z_id
, end
, -1);
1357 zfs_range_unlock(rl
);
1361 tx
= dmu_tx_create(zfsvfs
->z_os
);
1362 dmu_tx_hold_bonus(tx
, zp
->z_id
);
1363 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1365 if (error
== ERESTART
) {
1371 zfs_range_unlock(rl
);
1374 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
1376 zp
->z_phys
->zp_size
= end
;
1381 * Clear any mapped pages in the truncated region. This has to
1382 * happen outside of the transaction to avoid the possibility of
1383 * a deadlock with someone trying to push a page that we are
1384 * about to invalidate.
1386 if (vn_has_cached_data(vp
)) {
1388 uint64_t start
= end
& PAGEMASK
;
1389 int poff
= end
& PAGEOFFSET
;
1391 if (poff
!= 0 && (pp
= page_lookup(vp
, start
, SE_SHARED
))) {
1393 * We need to zero a partial page.
1395 pagezero(pp
, poff
, PAGESIZE
- poff
);
1399 error
= pvn_vplist_dirty(vp
, start
, zfs_no_putpage
,
1400 B_INVAL
| B_TRUNC
, NULL
);
1404 zfs_range_unlock(rl
);
1410 * Free space in a file
1412 * IN: zp - znode of file to free data in.
1413 * off - start of range
1414 * len - end of range (0 => EOF)
1415 * flag - current file open mode flags.
1416 * log - TRUE if this action should be logged
1418 * RETURN: 0 if success
1419 * error code if failure
1422 zfs_freesp(znode_t
*zp
, uint64_t off
, uint64_t len
, int flag
, boolean_t log
)
1424 vnode_t
*vp
= ZTOV(zp
);
1426 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1427 zilog_t
*zilog
= zfsvfs
->z_log
;
1430 if (off
> zp
->z_phys
->zp_size
) {
1431 error
= zfs_extend(zp
, off
+len
);
1432 if (error
== 0 && log
)
1439 * Check for any locks in the region to be freed.
1441 if (MANDLOCK(vp
, (mode_t
)zp
->z_phys
->zp_mode
)) {
1442 uint64_t length
= (len
? len
: zp
->z_phys
->zp_size
- off
);
1443 if (error
= chklock(vp
, FWRITE
, off
, length
, flag
, NULL
))
1448 error
= zfs_trunc(zp
, off
);
1450 if ((error
= zfs_free_range(zp
, off
, len
)) == 0 &&
1451 off
+ len
> zp
->z_phys
->zp_size
)
1452 error
= zfs_extend(zp
, off
+len
);
1457 tx
= dmu_tx_create(zfsvfs
->z_os
);
1458 dmu_tx_hold_bonus(tx
, zp
->z_id
);
1459 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1461 if (error
== ERESTART
) {
1470 zfs_time_stamper(zp
, CONTENT_MODIFIED
, tx
);
1471 zfs_log_truncate(zilog
, tx
, TX_TRUNCATE
, zp
, off
, len
);
1478 zfs_create_fs(objset_t
*os
, cred_t
*cr
, nvlist_t
*zplprops
, dmu_tx_t
*tx
)
1481 uint64_t moid
, obj
, version
;
1482 uint64_t sense
= ZFS_CASE_SENSITIVE
;
1486 znode_t
*rootzp
= NULL
;
1490 zfs_acl_ids_t acl_ids
;
1493 * First attempt to create master node.
1496 * In an empty objset, there are no blocks to read and thus
1497 * there can be no i/o errors (which we assert below).
1499 moid
= MASTER_NODE_OBJ
;
1500 error
= zap_create_claim(os
, moid
, DMU_OT_MASTER_NODE
,
1501 DMU_OT_NONE
, 0, tx
);
1505 * Set starting attributes.
1507 if (spa_version(dmu_objset_spa(os
)) >= SPA_VERSION_USERSPACE
)
1508 version
= ZPL_VERSION
;
1509 else if (spa_version(dmu_objset_spa(os
)) >= SPA_VERSION_FUID
)
1510 version
= ZPL_VERSION_USERSPACE
- 1;
1512 version
= ZPL_VERSION_FUID
- 1;
1514 while ((elem
= nvlist_next_nvpair(zplprops
, elem
)) != NULL
) {
1515 /* For the moment we expect all zpl props to be uint64_ts */
1519 ASSERT(nvpair_type(elem
) == DATA_TYPE_UINT64
);
1520 VERIFY(nvpair_value_uint64(elem
, &val
) == 0);
1521 name
= nvpair_name(elem
);
1522 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_VERSION
)) == 0) {
1526 error
= zap_update(os
, moid
, name
, 8, 1, &val
, tx
);
1529 if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_NORMALIZE
)) == 0)
1531 else if (strcmp(name
, zfs_prop_to_name(ZFS_PROP_CASE
)) == 0)
1534 ASSERT(version
!= 0);
1535 error
= zap_update(os
, moid
, ZPL_VERSION_STR
, 8, 1, &version
, tx
);
1538 * Create a delete queue.
1540 obj
= zap_create(os
, DMU_OT_UNLINKED_SET
, DMU_OT_NONE
, 0, tx
);
1542 error
= zap_add(os
, moid
, ZFS_UNLINKED_SET
, 8, 1, &obj
, tx
);
1546 * Create root znode. Create minimal znode/vnode/zfsvfs
1547 * to allow zfs_mknode to work.
1549 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
1550 vattr
.va_type
= VDIR
;
1551 vattr
.va_mode
= S_IFDIR
|0755;
1552 vattr
.va_uid
= crgetuid(cr
);
1553 vattr
.va_gid
= crgetgid(cr
);
1555 rootzp
= kmem_cache_alloc(znode_cache
, KM_SLEEP
);
1556 rootzp
->z_unlinked
= 0;
1557 rootzp
->z_atime_dirty
= 0;
1563 bzero(&zfsvfs
, sizeof (zfsvfs_t
));
1566 zfsvfs
.z_parent
= &zfsvfs
;
1567 zfsvfs
.z_version
= version
;
1568 zfsvfs
.z_use_fuids
= USE_FUIDS(version
, os
);
1569 zfsvfs
.z_norm
= norm
;
1571 * Fold case on file systems that are always or sometimes case
1574 if (sense
== ZFS_CASE_INSENSITIVE
|| sense
== ZFS_CASE_MIXED
)
1575 zfsvfs
.z_norm
|= U8_TEXTPREP_TOUPPER
;
1577 mutex_init(&zfsvfs
.z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1578 list_create(&zfsvfs
.z_all_znodes
, sizeof (znode_t
),
1579 offsetof(znode_t
, z_link_node
));
1581 ASSERT(!POINTER_IS_VALID(rootzp
->z_zfsvfs
));
1582 rootzp
->z_zfsvfs
= &zfsvfs
;
1583 VERIFY(0 == zfs_acl_ids_create(rootzp
, IS_ROOT_NODE
, &vattr
,
1584 cr
, NULL
, &acl_ids
));
1585 zfs_mknode(rootzp
, &vattr
, tx
, cr
, IS_ROOT_NODE
, &zp
, 0, &acl_ids
);
1586 ASSERT3P(zp
, ==, rootzp
);
1587 ASSERT(!vn_in_dnlc(ZTOV(rootzp
))); /* not valid to move */
1588 error
= zap_add(os
, moid
, ZFS_ROOT_OBJ
, 8, 1, &rootzp
->z_id
, tx
);
1590 zfs_acl_ids_free(&acl_ids
);
1591 POINTER_INVALIDATE(&rootzp
->z_zfsvfs
);
1593 ZTOV(rootzp
)->v_count
= 0;
1594 dmu_buf_rele(rootzp
->z_dbuf
, NULL
);
1595 rootzp
->z_dbuf
= NULL
;
1596 kmem_cache_free(znode_cache
, rootzp
);
1599 * Create shares directory
1602 error
= zfs_create_share_dir(&zfsvfs
, tx
);
1607 #endif /* _KERNEL */
1609 * Given an object number, return its parent object number and whether
1610 * or not the object is an extended attribute directory.
1613 zfs_obj_to_pobj(objset_t
*osp
, uint64_t obj
, uint64_t *pobjp
, int *is_xattrdir
)
1616 dmu_object_info_t doi
;
1620 if ((error
= dmu_bonus_hold(osp
, obj
, FTAG
, &db
)) != 0)
1623 dmu_object_info_from_db(db
, &doi
);
1624 if (doi
.doi_bonus_type
!= DMU_OT_ZNODE
||
1625 doi
.doi_bonus_size
< sizeof (znode_phys_t
)) {
1626 dmu_buf_rele(db
, FTAG
);
1631 *pobjp
= zp
->zp_parent
;
1632 *is_xattrdir
= ((zp
->zp_flags
& ZFS_XATTR
) != 0) &&
1633 S_ISDIR(zp
->zp_mode
);
1634 dmu_buf_rele(db
, FTAG
);
1640 zfs_obj_to_path(objset_t
*osp
, uint64_t obj
, char *buf
, int len
)
1642 char *path
= buf
+ len
- 1;
1649 char component
[MAXNAMELEN
+ 2];
1653 if ((error
= zfs_obj_to_pobj(osp
, obj
, &pobj
,
1654 &is_xattrdir
)) != 0)
1665 (void) sprintf(component
+ 1, "<xattrdir>");
1667 error
= zap_value_search(osp
, pobj
, obj
,
1668 ZFS_DIRENT_OBJ(-1ULL), component
+ 1);
1673 complen
= strlen(component
);
1675 ASSERT(path
>= buf
);
1676 bcopy(component
, path
, complen
);
1681 (void) memmove(buf
, path
, buf
+ len
- path
);