xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_znode.c (revision 747cfc73193da6c99aeb06f23e70552319393cc4)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /* Portions Copyright 2007 Jeremy Teo */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #ifdef _KERNEL
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <sys/mntent.h>
38 #include <sys/mkdev.h>
39 #include <sys/u8_textprep.h>
40 #include <sys/vfs.h>
41 #include <sys/vfs_opreg.h>
42 #include <sys/vnode.h>
43 #include <sys/file.h>
44 #include <sys/kmem.h>
45 #include <sys/errno.h>
46 #include <sys/unistd.h>
47 #include <sys/mode.h>
48 #include <sys/atomic.h>
49 #include <vm/pvn.h>
50 #include "fs/fs_subr.h"
51 #include <sys/zfs_dir.h>
52 #include <sys/zfs_acl.h>
53 #include <sys/zfs_ioctl.h>
54 #include <sys/zfs_rlock.h>
55 #include <sys/zfs_fuid.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/kidmap.h>
58 #endif /* _KERNEL */
59 
60 #include <sys/dmu.h>
61 #include <sys/refcount.h>
62 #include <sys/stat.h>
63 #include <sys/zap.h>
64 #include <sys/zfs_znode.h>
65 
66 #include "zfs_prop.h"
67 
68 /*
69  * Functions needed for userland (ie: libzpool) are not put under
70  * #ifdef_KERNEL; the rest of the functions have dependencies
71  * (such as VFS logic) that will not compile easily in userland.
72  */
73 #ifdef _KERNEL
74 struct kmem_cache *znode_cache = NULL;
75 
76 /*ARGSUSED*/
77 static void
78 znode_pageout_func(dmu_buf_t *dbuf, void *user_ptr)
79 {
80 	znode_t *zp = user_ptr;
81 	vnode_t *vp = ZTOV(zp);
82 
83 	mutex_enter(&zp->z_lock);
84 	zp->z_dbuf = NULL;
85 	if (vp->v_count == 0) {
86 		mutex_exit(&zp->z_lock);
87 		vn_invalid(vp);
88 		zfs_znode_free(zp);
89 	} else {
90 		mutex_exit(&zp->z_lock);
91 	}
92 }
93 
94 /*ARGSUSED*/
95 static int
96 zfs_znode_cache_constructor(void *buf, void *cdrarg, int kmflags)
97 {
98 	znode_t *zp = buf;
99 
100 	zp->z_vnode = vn_alloc(KM_SLEEP);
101 	zp->z_vnode->v_data = (caddr_t)zp;
102 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
103 	rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL);
104 	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
105 	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
106 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
107 
108 	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
109 	avl_create(&zp->z_range_avl, zfs_range_compare,
110 	    sizeof (rl_t), offsetof(rl_t, r_node));
111 
112 	zp->z_dbuf = NULL;
113 	zp->z_dirlocks = 0;
114 	return (0);
115 }
116 
117 /*ARGSUSED*/
118 static void
119 zfs_znode_cache_destructor(void *buf, void *cdarg)
120 {
121 	znode_t *zp = buf;
122 
123 	ASSERT(zp->z_dirlocks == 0);
124 	mutex_destroy(&zp->z_lock);
125 	rw_destroy(&zp->z_map_lock);
126 	rw_destroy(&zp->z_parent_lock);
127 	rw_destroy(&zp->z_name_lock);
128 	mutex_destroy(&zp->z_acl_lock);
129 	avl_destroy(&zp->z_range_avl);
130 	mutex_destroy(&zp->z_range_lock);
131 
132 	ASSERT(zp->z_dbuf == NULL);
133 	ASSERT(ZTOV(zp)->v_count == 0);
134 	vn_free(ZTOV(zp));
135 }
136 
137 void
138 zfs_znode_init(void)
139 {
140 	/*
141 	 * Initialize zcache
142 	 */
143 	ASSERT(znode_cache == NULL);
144 	znode_cache = kmem_cache_create("zfs_znode_cache",
145 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
146 	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
147 }
148 
149 void
150 zfs_znode_fini(void)
151 {
152 	/*
153 	 * Cleanup vfs & vnode ops
154 	 */
155 	zfs_remove_op_tables();
156 
157 	/*
158 	 * Cleanup zcache
159 	 */
160 	if (znode_cache)
161 		kmem_cache_destroy(znode_cache);
162 	znode_cache = NULL;
163 }
164 
165 struct vnodeops *zfs_dvnodeops;
166 struct vnodeops *zfs_fvnodeops;
167 struct vnodeops *zfs_symvnodeops;
168 struct vnodeops *zfs_xdvnodeops;
169 struct vnodeops *zfs_evnodeops;
170 
171 void
172 zfs_remove_op_tables()
173 {
174 	/*
175 	 * Remove vfs ops
176 	 */
177 	ASSERT(zfsfstype);
178 	(void) vfs_freevfsops_by_type(zfsfstype);
179 	zfsfstype = 0;
180 
181 	/*
182 	 * Remove vnode ops
183 	 */
184 	if (zfs_dvnodeops)
185 		vn_freevnodeops(zfs_dvnodeops);
186 	if (zfs_fvnodeops)
187 		vn_freevnodeops(zfs_fvnodeops);
188 	if (zfs_symvnodeops)
189 		vn_freevnodeops(zfs_symvnodeops);
190 	if (zfs_xdvnodeops)
191 		vn_freevnodeops(zfs_xdvnodeops);
192 	if (zfs_evnodeops)
193 		vn_freevnodeops(zfs_evnodeops);
194 
195 	zfs_dvnodeops = NULL;
196 	zfs_fvnodeops = NULL;
197 	zfs_symvnodeops = NULL;
198 	zfs_xdvnodeops = NULL;
199 	zfs_evnodeops = NULL;
200 }
201 
202 extern const fs_operation_def_t zfs_dvnodeops_template[];
203 extern const fs_operation_def_t zfs_fvnodeops_template[];
204 extern const fs_operation_def_t zfs_xdvnodeops_template[];
205 extern const fs_operation_def_t zfs_symvnodeops_template[];
206 extern const fs_operation_def_t zfs_evnodeops_template[];
207 
208 int
209 zfs_create_op_tables()
210 {
211 	int error;
212 
213 	/*
214 	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
215 	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
216 	 * In this case we just return as the ops vectors are already set up.
217 	 */
218 	if (zfs_dvnodeops)
219 		return (0);
220 
221 	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
222 	    &zfs_dvnodeops);
223 	if (error)
224 		return (error);
225 
226 	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
227 	    &zfs_fvnodeops);
228 	if (error)
229 		return (error);
230 
231 	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
232 	    &zfs_symvnodeops);
233 	if (error)
234 		return (error);
235 
236 	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
237 	    &zfs_xdvnodeops);
238 	if (error)
239 		return (error);
240 
241 	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
242 	    &zfs_evnodeops);
243 
244 	return (error);
245 }
246 
247 /*
248  * zfs_init_fs - Initialize the zfsvfs struct and the file system
249  *	incore "master" object.  Verify version compatibility.
250  */
251 int
252 zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr)
253 {
254 	extern int zfsfstype;
255 
256 	objset_t	*os = zfsvfs->z_os;
257 	int		i, error;
258 	dmu_object_info_t doi;
259 	uint64_t fsid_guid;
260 	uint64_t zval;
261 
262 	*zpp = NULL;
263 
264 	/*
265 	 * XXX - hack to auto-create the pool root filesystem at
266 	 * the first attempted mount.
267 	 */
268 	if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) {
269 		dmu_tx_t *tx = dmu_tx_create(os);
270 		uint64_t zpl_version;
271 		nvlist_t *zprops;
272 
273 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */
274 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */
275 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */
276 		error = dmu_tx_assign(tx, TXG_WAIT);
277 		ASSERT3U(error, ==, 0);
278 		if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
279 			zpl_version = ZPL_VERSION;
280 		else
281 			zpl_version = ZPL_VERSION_FUID - 1;
282 
283 		VERIFY(nvlist_alloc(&zprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
284 		VERIFY(nvlist_add_uint64(zprops,
285 		    zfs_prop_to_name(ZFS_PROP_VERSION), zpl_version) == 0);
286 		zfs_create_fs(os, cr, zprops, tx);
287 		nvlist_free(zprops);
288 		dmu_tx_commit(tx);
289 	}
290 
291 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
292 	if (error) {
293 		return (error);
294 	} else if (zfsvfs->z_version > ZPL_VERSION) {
295 		(void) printf("Mismatched versions:  File system "
296 		    "is version %llu on-disk format, which is "
297 		    "incompatible with this software version %lld!",
298 		    (u_longlong_t)zfsvfs->z_version, ZPL_VERSION);
299 		return (ENOTSUP);
300 	}
301 
302 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
303 		return (error);
304 	zfsvfs->z_norm = (int)zval;
305 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
306 		return (error);
307 	zfsvfs->z_utf8 = (zval != 0);
308 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
309 		return (error);
310 	zfsvfs->z_case = (uint_t)zval;
311 	/*
312 	 * Fold case on file systems that are always or sometimes case
313 	 * insensitive.
314 	 */
315 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
316 	    zfsvfs->z_case == ZFS_CASE_MIXED)
317 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
318 
319 	/*
320 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
321 	 * separates our fsid from any other filesystem types, and a
322 	 * 56-bit objset unique ID.  The objset unique ID is unique to
323 	 * all objsets open on this system, provided by unique_create().
324 	 * The 8-bit fs type must be put in the low bits of fsid[1]
325 	 * because that's where other Solaris filesystems put it.
326 	 */
327 	fsid_guid = dmu_objset_fsid_guid(os);
328 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
329 	zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid;
330 	zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
331 	    zfsfstype & 0xFF;
332 
333 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
334 	    &zfsvfs->z_root);
335 	if (error)
336 		return (error);
337 	ASSERT(zfsvfs->z_root != 0);
338 
339 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
340 	    &zfsvfs->z_unlinkedobj);
341 	if (error)
342 		return (error);
343 
344 	/*
345 	 * Initialize zget mutex's
346 	 */
347 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
348 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
349 
350 	error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp);
351 	if (error) {
352 		/*
353 		 * On error, we destroy the mutexes here since it's not
354 		 * possible for the caller to determine if the mutexes were
355 		 * initialized properly.
356 		 */
357 		for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
358 			mutex_destroy(&zfsvfs->z_hold_mtx[i]);
359 		return (error);
360 	}
361 	ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root);
362 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
363 	    &zfsvfs->z_fuid_obj);
364 	if (error == ENOENT)
365 		error = 0;
366 
367 	return (0);
368 }
369 
370 /*
371  * define a couple of values we need available
372  * for both 64 and 32 bit environments.
373  */
374 #ifndef NBITSMINOR64
375 #define	NBITSMINOR64	32
376 #endif
377 #ifndef MAXMAJ64
378 #define	MAXMAJ64	0xffffffffUL
379 #endif
380 #ifndef	MAXMIN64
381 #define	MAXMIN64	0xffffffffUL
382 #endif
383 
384 /*
385  * Create special expldev for ZFS private use.
386  * Can't use standard expldev since it doesn't do
387  * what we want.  The standard expldev() takes a
388  * dev32_t in LP64 and expands it to a long dev_t.
389  * We need an interface that takes a dev32_t in ILP32
390  * and expands it to a long dev_t.
391  */
392 static uint64_t
393 zfs_expldev(dev_t dev)
394 {
395 #ifndef _LP64
396 	major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
397 	return (((uint64_t)major << NBITSMINOR64) |
398 	    ((minor_t)dev & MAXMIN32));
399 #else
400 	return (dev);
401 #endif
402 }
403 
404 /*
405  * Special cmpldev for ZFS private use.
406  * Can't use standard cmpldev since it takes
407  * a long dev_t and compresses it to dev32_t in
408  * LP64.  We need to do a compaction of a long dev_t
409  * to a dev32_t in ILP32.
410  */
411 dev_t
412 zfs_cmpldev(uint64_t dev)
413 {
414 #ifndef _LP64
415 	minor_t minor = (minor_t)dev & MAXMIN64;
416 	major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
417 
418 	if (major > MAXMAJ32 || minor > MAXMIN32)
419 		return (NODEV32);
420 
421 	return (((dev32_t)major << NBITSMINOR32) | minor);
422 #else
423 	return (dev);
424 #endif
425 }
426 
427 static void
428 zfs_znode_dmu_init(znode_t *zp, dmu_buf_t *db)
429 {
430 	znode_t		*nzp;
431 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
432 
433 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp)));
434 
435 	mutex_enter(&zp->z_lock);
436 
437 	ASSERT(zp->z_dbuf == NULL);
438 	zp->z_dbuf = db;
439 	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_pageout_func);
440 
441 	/*
442 	 * there should be no
443 	 * concurrent zgets on this object.
444 	 */
445 	if (nzp != NULL)
446 		panic("existing znode %p for dbuf %p", nzp, db);
447 
448 	/*
449 	 * Slap on VROOT if we are the root znode
450 	 */
451 	if (zp->z_id == zfsvfs->z_root)
452 		ZTOV(zp)->v_flag |= VROOT;
453 
454 	mutex_exit(&zp->z_lock);
455 	vn_exists(ZTOV(zp));
456 }
457 
458 static void
459 zfs_znode_dmu_fini(znode_t *zp)
460 {
461 	dmu_buf_t *db = zp->z_dbuf;
462 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp)));
463 	ASSERT(zp->z_dbuf != NULL);
464 	zp->z_dbuf = NULL;
465 	dmu_buf_rele(db, NULL);
466 }
467 
468 /*
469  * Construct a new znode/vnode and intialize.
470  *
471  * This does not do a call to dmu_set_user() that is
472  * up to the caller to do, in case you don't want to
473  * return the znode
474  */
475 static znode_t *
476 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
477 {
478 	znode_t	*zp;
479 	vnode_t *vp;
480 
481 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
482 
483 	ASSERT(zp->z_dirlocks == NULL);
484 	ASSERT(zp->z_dbuf == NULL);
485 
486 	zp->z_phys = NULL;
487 	zp->z_zfsvfs = zfsvfs;
488 	zp->z_unlinked = 0;
489 	zp->z_atime_dirty = 0;
490 	zp->z_mapcnt = 0;
491 	zp->z_last_itx = 0;
492 	zp->z_id = db->db_object;
493 	zp->z_blksz = blksz;
494 	zp->z_seq = 0x7A4653;
495 	zp->z_sync_cnt = 0;
496 
497 	vp = ZTOV(zp);
498 	vn_reinit(vp);
499 
500 	zfs_znode_dmu_init(zp, db);
501 
502 	zp->z_gen = zp->z_phys->zp_gen;
503 
504 	mutex_enter(&zfsvfs->z_znodes_lock);
505 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
506 	mutex_exit(&zfsvfs->z_znodes_lock);
507 
508 	vp->v_vfsp = zfsvfs->z_parent->z_vfs;
509 	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
510 
511 	switch (vp->v_type) {
512 	case VDIR:
513 		if (zp->z_phys->zp_flags & ZFS_XATTR) {
514 			vn_setops(vp, zfs_xdvnodeops);
515 			vp->v_flag |= V_XATTRDIR;
516 		} else {
517 			vn_setops(vp, zfs_dvnodeops);
518 		}
519 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
520 		break;
521 	case VBLK:
522 	case VCHR:
523 		vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
524 		/*FALLTHROUGH*/
525 	case VFIFO:
526 	case VSOCK:
527 	case VDOOR:
528 		vn_setops(vp, zfs_fvnodeops);
529 		break;
530 	case VREG:
531 		vp->v_flag |= VMODSORT;
532 		vn_setops(vp, zfs_fvnodeops);
533 		break;
534 	case VLNK:
535 		vn_setops(vp, zfs_symvnodeops);
536 		break;
537 	default:
538 		vn_setops(vp, zfs_evnodeops);
539 		break;
540 	}
541 
542 	/* it can be NULL from zfs_create_fs */
543 	if (zfsvfs->z_vfs)
544 		VFS_HOLD(zfsvfs->z_vfs);
545 	return (zp);
546 }
547 
548 /*
549  * Create a new DMU object to hold a zfs znode.
550  *
551  *	IN:	dzp	- parent directory for new znode
552  *		vap	- file attributes for new znode
553  *		tx	- dmu transaction id for zap operations
554  *		cr	- credentials of caller
555  *		flag	- flags:
556  *			  IS_ROOT_NODE	- new object will be root
557  *			  IS_XATTR	- new object is an attribute
558  *			  IS_REPLAY	- intent log replay
559  *		bonuslen - length of bonus buffer
560  *		setaclp  - File/Dir initial ACL
561  *		fuidp	 - Tracks fuid allocation.
562  *
563  *	OUT:	zpp	- allocated znode
564  *
565  */
566 void
567 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
568     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_t *setaclp,
569     zfs_fuid_info_t **fuidp)
570 {
571 	dmu_buf_t	*db;
572 	znode_phys_t	*pzp;
573 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
574 	timestruc_t	now;
575 	uint64_t	gen, obj;
576 	int		err;
577 
578 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
579 
580 	if (zfsvfs->z_assign >= TXG_INITIAL) {		/* ZIL replay */
581 		obj = vap->va_nodeid;
582 		flag |= IS_REPLAY;
583 		now = vap->va_ctime;		/* see zfs_replay_create() */
584 		gen = vap->va_nblocks;		/* ditto */
585 	} else {
586 		obj = 0;
587 		gethrestime(&now);
588 		gen = dmu_tx_get_txg(tx);
589 	}
590 
591 	/*
592 	 * Create a new DMU object.
593 	 */
594 	/*
595 	 * There's currently no mechanism for pre-reading the blocks that will
596 	 * be to needed allocate a new object, so we accept the small chance
597 	 * that there will be an i/o error and we will fail one of the
598 	 * assertions below.
599 	 */
600 	if (vap->va_type == VDIR) {
601 		if (flag & IS_REPLAY) {
602 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
603 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
604 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
605 			ASSERT3U(err, ==, 0);
606 		} else {
607 			obj = zap_create_norm(zfsvfs->z_os,
608 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
609 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
610 		}
611 	} else {
612 		if (flag & IS_REPLAY) {
613 			err = dmu_object_claim(zfsvfs->z_os, obj,
614 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
615 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
616 			ASSERT3U(err, ==, 0);
617 		} else {
618 			obj = dmu_object_alloc(zfsvfs->z_os,
619 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
620 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
621 		}
622 	}
623 	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
624 	dmu_buf_will_dirty(db, tx);
625 
626 	/*
627 	 * Initialize the znode physical data to zero.
628 	 */
629 	ASSERT(db->db_size >= sizeof (znode_phys_t));
630 	bzero(db->db_data, db->db_size);
631 	pzp = db->db_data;
632 
633 	/*
634 	 * If this is the root, fix up the half-initialized parent pointer
635 	 * to reference the just-allocated physical data area.
636 	 */
637 	if (flag & IS_ROOT_NODE) {
638 		dzp->z_phys = pzp;
639 		dzp->z_id = obj;
640 	}
641 
642 	/*
643 	 * If parent is an xattr, so am I.
644 	 */
645 	if (dzp->z_phys->zp_flags & ZFS_XATTR)
646 		flag |= IS_XATTR;
647 
648 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
649 		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
650 	}
651 
652 	if (zfsvfs->z_use_fuids)
653 		pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
654 
655 	if (vap->va_type == VDIR) {
656 		pzp->zp_size = 2;		/* contents ("." and "..") */
657 		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
658 	}
659 
660 	pzp->zp_parent = dzp->z_id;
661 	if (flag & IS_XATTR)
662 		pzp->zp_flags |= ZFS_XATTR;
663 
664 	pzp->zp_gen = gen;
665 
666 	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
667 	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
668 
669 	if (vap->va_mask & AT_ATIME) {
670 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
671 	} else {
672 		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
673 	}
674 
675 	if (vap->va_mask & AT_MTIME) {
676 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
677 	} else {
678 		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
679 	}
680 
681 	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
682 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj)
683 	*zpp = zfs_znode_alloc(zfsvfs, db, 0);
684 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
685 	zfs_perm_init(*zpp, dzp, flag, vap, tx, cr, setaclp, fuidp);
686 }
687 
688 void
689 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
690 {
691 	xoptattr_t *xoap;
692 
693 	xoap = xva_getxoptattr(xvap);
694 	ASSERT(xoap);
695 
696 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
697 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
698 		XVA_SET_RTN(xvap, XAT_CREATETIME);
699 	}
700 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
701 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
702 		XVA_SET_RTN(xvap, XAT_READONLY);
703 	}
704 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
705 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
706 		XVA_SET_RTN(xvap, XAT_HIDDEN);
707 	}
708 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
709 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
710 		XVA_SET_RTN(xvap, XAT_SYSTEM);
711 	}
712 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
713 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
714 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
715 	}
716 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
717 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
718 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
719 	}
720 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
721 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
722 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
723 	}
724 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
725 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
726 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
727 	}
728 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
729 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
730 		XVA_SET_RTN(xvap, XAT_NODUMP);
731 	}
732 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
733 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
734 		XVA_SET_RTN(xvap, XAT_OPAQUE);
735 	}
736 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
737 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
738 		    xoap->xoa_av_quarantined);
739 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
740 	}
741 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
742 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
743 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
744 	}
745 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
746 		(void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
747 		    sizeof (xoap->xoa_av_scanstamp));
748 		zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
749 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
750 	}
751 }
752 
753 int
754 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
755 {
756 	dmu_object_info_t doi;
757 	dmu_buf_t	*db;
758 	znode_t		*zp;
759 	int err;
760 
761 	*zpp = NULL;
762 
763 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
764 
765 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
766 	if (err) {
767 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
768 		return (err);
769 	}
770 
771 	dmu_object_info_from_db(db, &doi);
772 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
773 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
774 		dmu_buf_rele(db, NULL);
775 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
776 		return (EINVAL);
777 	}
778 
779 	zp = dmu_buf_get_user(db);
780 	if (zp != NULL) {
781 		mutex_enter(&zp->z_lock);
782 
783 		/*
784 		 * Since we do immediate eviction of the z_dbuf, we
785 		 * should never find a dbuf with a znode that doesn't
786 		 * know about the dbuf.
787 		 */
788 		ASSERT3P(zp->z_dbuf, ==, db);
789 		ASSERT3U(zp->z_id, ==, obj_num);
790 		if (zp->z_unlinked) {
791 			err = ENOENT;
792 		} else {
793 			VN_HOLD(ZTOV(zp));
794 			*zpp = zp;
795 			err = 0;
796 		}
797 		dmu_buf_rele(db, NULL);
798 		mutex_exit(&zp->z_lock);
799 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
800 		return (err);
801 	}
802 
803 	/*
804 	 * Not found create new znode/vnode
805 	 */
806 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
807 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
808 	*zpp = zp;
809 	return (0);
810 }
811 
812 int
813 zfs_rezget(znode_t *zp)
814 {
815 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
816 	dmu_object_info_t doi;
817 	dmu_buf_t *db;
818 	uint64_t obj_num = zp->z_id;
819 	int err;
820 
821 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
822 
823 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
824 	if (err) {
825 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
826 		return (err);
827 	}
828 
829 	dmu_object_info_from_db(db, &doi);
830 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
831 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
832 		dmu_buf_rele(db, NULL);
833 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
834 		return (EINVAL);
835 	}
836 
837 	if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
838 		dmu_buf_rele(db, NULL);
839 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
840 		return (EIO);
841 	}
842 
843 	zfs_znode_dmu_init(zp, db);
844 	zp->z_unlinked = (zp->z_phys->zp_links == 0);
845 
846 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
847 
848 	return (0);
849 }
850 
851 void
852 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
853 {
854 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
855 	uint64_t obj = zp->z_id;
856 
857 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
858 	if (zp->z_phys->zp_acl.z_acl_extern_obj) {
859 		VERIFY(0 == dmu_object_free(zfsvfs->z_os,
860 		    zp->z_phys->zp_acl.z_acl_extern_obj, tx));
861 	}
862 	VERIFY(0 == dmu_object_free(zfsvfs->z_os, obj, tx));
863 	zfs_znode_dmu_fini(zp);
864 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
865 }
866 
867 void
868 zfs_zinactive(znode_t *zp)
869 {
870 	vnode_t	*vp = ZTOV(zp);
871 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
872 	uint64_t z_id = zp->z_id;
873 
874 	ASSERT(zp->z_dbuf && zp->z_phys);
875 
876 	/*
877 	 * Don't allow a zfs_zget() while were trying to release this znode
878 	 */
879 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
880 
881 	mutex_enter(&zp->z_lock);
882 	mutex_enter(&vp->v_lock);
883 	vp->v_count--;
884 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
885 		/*
886 		 * If the hold count is greater than zero, somebody has
887 		 * obtained a new reference on this znode while we were
888 		 * processing it here, so we are done.  If we still have
889 		 * mapped pages then we are also done, since we don't
890 		 * want to inactivate the znode until the pages get pushed.
891 		 *
892 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
893 		 * this seems like it would leave the znode hanging with
894 		 * no chance to go inactive...
895 		 */
896 		mutex_exit(&vp->v_lock);
897 		mutex_exit(&zp->z_lock);
898 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
899 		return;
900 	}
901 	mutex_exit(&vp->v_lock);
902 
903 	/*
904 	 * If this was the last reference to a file with no links,
905 	 * remove the file from the file system.
906 	 */
907 	if (zp->z_unlinked) {
908 		mutex_exit(&zp->z_lock);
909 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
910 		zfs_rmnode(zp);
911 		VFS_RELE(zfsvfs->z_vfs);
912 		return;
913 	}
914 	mutex_exit(&zp->z_lock);
915 	zfs_znode_dmu_fini(zp);
916 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
917 	/* it can be NULL from zfs_create_fs */
918 	if (zfsvfs->z_vfs)
919 		VFS_RELE(zfsvfs->z_vfs);
920 }
921 
922 void
923 zfs_znode_free(znode_t *zp)
924 {
925 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
926 
927 	mutex_enter(&zfsvfs->z_znodes_lock);
928 	list_remove(&zfsvfs->z_all_znodes, zp);
929 	mutex_exit(&zfsvfs->z_znodes_lock);
930 
931 	kmem_cache_free(znode_cache, zp);
932 }
933 
934 void
935 zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
936 {
937 	timestruc_t	now;
938 
939 	ASSERT(MUTEX_HELD(&zp->z_lock));
940 
941 	gethrestime(&now);
942 
943 	if (tx) {
944 		dmu_buf_will_dirty(zp->z_dbuf, tx);
945 		zp->z_atime_dirty = 0;
946 		zp->z_seq++;
947 	} else {
948 		zp->z_atime_dirty = 1;
949 	}
950 
951 	if (flag & AT_ATIME)
952 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
953 
954 	if (flag & AT_MTIME) {
955 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
956 		if (zp->z_zfsvfs->z_use_fuids)
957 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
958 	}
959 
960 	if (flag & AT_CTIME) {
961 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
962 		if (zp->z_zfsvfs->z_use_fuids)
963 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
964 	}
965 }
966 
967 /*
968  * Update the requested znode timestamps with the current time.
969  * If we are in a transaction, then go ahead and mark the znode
970  * dirty in the transaction so the timestamps will go to disk.
971  * Otherwise, we will get pushed next time the znode is updated
972  * in a transaction, or when this znode eventually goes inactive.
973  *
974  * Why is this OK?
975  *  1 - Only the ACCESS time is ever updated outside of a transaction.
976  *  2 - Multiple consecutive updates will be collapsed into a single
977  *	znode update by the transaction grouping semantics of the DMU.
978  */
979 void
980 zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
981 {
982 	mutex_enter(&zp->z_lock);
983 	zfs_time_stamper_locked(zp, flag, tx);
984 	mutex_exit(&zp->z_lock);
985 }
986 
987 /*
988  * Grow the block size for a file.
989  *
990  *	IN:	zp	- znode of file to free data in.
991  *		size	- requested block size
992  *		tx	- open transaction.
993  *
994  * NOTE: this function assumes that the znode is write locked.
995  */
996 void
997 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
998 {
999 	int		error;
1000 	u_longlong_t	dummy;
1001 
1002 	if (size <= zp->z_blksz)
1003 		return;
1004 	/*
1005 	 * If the file size is already greater than the current blocksize,
1006 	 * we will not grow.  If there is more than one block in a file,
1007 	 * the blocksize cannot change.
1008 	 */
1009 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1010 		return;
1011 
1012 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1013 	    size, 0, tx);
1014 	if (error == ENOTSUP)
1015 		return;
1016 	ASSERT3U(error, ==, 0);
1017 
1018 	/* What blocksize did we actually get? */
1019 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1020 }
1021 
1022 /*
1023  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1024  * be calling back into the fs for a putpage().  E.g.: when truncating
1025  * a file, the pages being "thrown away* don't need to be written out.
1026  */
1027 /* ARGSUSED */
1028 static int
1029 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1030     int flags, cred_t *cr)
1031 {
1032 	ASSERT(0);
1033 	return (0);
1034 }
1035 
1036 /*
1037  * Free space in a file.
1038  *
1039  *	IN:	zp	- znode of file to free data in.
1040  *		off	- start of section to free.
1041  *		len	- length of section to free (0 => to EOF).
1042  *		flag	- current file open mode flags.
1043  *
1044  * 	RETURN:	0 if success
1045  *		error code if failure
1046  */
1047 int
1048 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1049 {
1050 	vnode_t *vp = ZTOV(zp);
1051 	dmu_tx_t *tx;
1052 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1053 	zilog_t *zilog = zfsvfs->z_log;
1054 	rl_t *rl;
1055 	uint64_t end = off + len;
1056 	uint64_t size, new_blksz;
1057 	uint64_t pflags = zp->z_phys->zp_flags;
1058 	int error;
1059 
1060 	if ((pflags & (ZFS_IMMUTABLE|ZFS_READONLY)) ||
1061 	    off < zp->z_phys->zp_size && (pflags & ZFS_APPENDONLY))
1062 		return (EPERM);
1063 
1064 	if (ZTOV(zp)->v_type == VFIFO)
1065 		return (0);
1066 
1067 	/*
1068 	 * If we will change zp_size then lock the whole file,
1069 	 * otherwise just lock the range being freed.
1070 	 */
1071 	if (len == 0 || off + len > zp->z_phys->zp_size) {
1072 		rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1073 	} else {
1074 		rl = zfs_range_lock(zp, off, len, RL_WRITER);
1075 		/* recheck, in case zp_size changed */
1076 		if (off + len > zp->z_phys->zp_size) {
1077 			/* lost race: file size changed, lock whole file */
1078 			zfs_range_unlock(rl);
1079 			rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1080 		}
1081 	}
1082 
1083 	/*
1084 	 * Nothing to do if file already at desired length.
1085 	 */
1086 	size = zp->z_phys->zp_size;
1087 	if (len == 0 && size == off && off != 0) {
1088 		zfs_range_unlock(rl);
1089 		return (0);
1090 	}
1091 
1092 	/*
1093 	 * Check for any locks in the region to be freed.
1094 	 */
1095 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
1096 		uint64_t start = off;
1097 		uint64_t extent = len;
1098 
1099 		if (off > size) {
1100 			start = size;
1101 			extent += off - size;
1102 		} else if (len == 0) {
1103 			extent = size - off;
1104 		}
1105 		if (error = chklock(vp, FWRITE, start, extent, flag, NULL)) {
1106 			zfs_range_unlock(rl);
1107 			return (error);
1108 		}
1109 	}
1110 
1111 	tx = dmu_tx_create(zfsvfs->z_os);
1112 	dmu_tx_hold_bonus(tx, zp->z_id);
1113 	new_blksz = 0;
1114 	if (end > size &&
1115 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1116 		/*
1117 		 * We are growing the file past the current block size.
1118 		 */
1119 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1120 			ASSERT(!ISP2(zp->z_blksz));
1121 			new_blksz = MIN(end, SPA_MAXBLOCKSIZE);
1122 		} else {
1123 			new_blksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1124 		}
1125 		dmu_tx_hold_write(tx, zp->z_id, 0, MIN(end, new_blksz));
1126 	} else if (off < size) {
1127 		/*
1128 		 * If len == 0, we are truncating the file.
1129 		 */
1130 		dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END);
1131 	}
1132 
1133 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1134 	if (error) {
1135 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
1136 			dmu_tx_wait(tx);
1137 		dmu_tx_abort(tx);
1138 		zfs_range_unlock(rl);
1139 		return (error);
1140 	}
1141 
1142 	if (new_blksz)
1143 		zfs_grow_blocksize(zp, new_blksz, tx);
1144 
1145 	if (end > size || len == 0)
1146 		zp->z_phys->zp_size = end;
1147 
1148 	if (off < size) {
1149 		objset_t *os = zfsvfs->z_os;
1150 		uint64_t rlen = len;
1151 
1152 		if (len == 0)
1153 			rlen = -1;
1154 		else if (end > size)
1155 			rlen = size - off;
1156 		VERIFY(0 == dmu_free_range(os, zp->z_id, off, rlen, tx));
1157 	}
1158 
1159 	if (log) {
1160 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1161 		zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1162 	}
1163 
1164 	zfs_range_unlock(rl);
1165 
1166 	dmu_tx_commit(tx);
1167 
1168 	/*
1169 	 * Clear any mapped pages in the truncated region.  This has to
1170 	 * happen outside of the transaction to avoid the possibility of
1171 	 * a deadlock with someone trying to push a page that we are
1172 	 * about to invalidate.
1173 	 */
1174 	rw_enter(&zp->z_map_lock, RW_WRITER);
1175 	if (off < size && vn_has_cached_data(vp)) {
1176 		page_t *pp;
1177 		uint64_t start = off & PAGEMASK;
1178 		int poff = off & PAGEOFFSET;
1179 
1180 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1181 			/*
1182 			 * We need to zero a partial page.
1183 			 */
1184 			pagezero(pp, poff, PAGESIZE - poff);
1185 			start += PAGESIZE;
1186 			page_unlock(pp);
1187 		}
1188 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1189 		    B_INVAL | B_TRUNC, NULL);
1190 		ASSERT(error == 0);
1191 	}
1192 	rw_exit(&zp->z_map_lock);
1193 
1194 	return (0);
1195 }
1196 
1197 void
1198 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1199 {
1200 	zfsvfs_t	zfsvfs;
1201 	uint64_t	moid, doid;
1202 	uint64_t	version = 0;
1203 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1204 	uint64_t	norm = 0;
1205 	nvpair_t	*elem;
1206 	int		error;
1207 	znode_t		*rootzp = NULL;
1208 	vnode_t		*vp;
1209 	vattr_t		vattr;
1210 	znode_t		*zp;
1211 
1212 	/*
1213 	 * First attempt to create master node.
1214 	 */
1215 	/*
1216 	 * In an empty objset, there are no blocks to read and thus
1217 	 * there can be no i/o errors (which we assert below).
1218 	 */
1219 	moid = MASTER_NODE_OBJ;
1220 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1221 	    DMU_OT_NONE, 0, tx);
1222 	ASSERT(error == 0);
1223 
1224 	/*
1225 	 * Set starting attributes.
1226 	 */
1227 	elem = NULL;
1228 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1229 		/* For the moment we expect all zpl props to be uint64_ts */
1230 		uint64_t val;
1231 		char *name;
1232 
1233 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1234 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1235 		name = nvpair_name(elem);
1236 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1237 			version = val;
1238 			error = zap_update(os, moid, ZPL_VERSION_STR,
1239 			    8, 1, &version, tx);
1240 		} else {
1241 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1242 		}
1243 		ASSERT(error == 0);
1244 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1245 			norm = val;
1246 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1247 			sense = val;
1248 	}
1249 	ASSERT(version != 0);
1250 
1251 	/*
1252 	 * Create a delete queue.
1253 	 */
1254 	doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1255 
1256 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx);
1257 	ASSERT(error == 0);
1258 
1259 	/*
1260 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1261 	 * to allow zfs_mknode to work.
1262 	 */
1263 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1264 	vattr.va_type = VDIR;
1265 	vattr.va_mode = S_IFDIR|0755;
1266 	vattr.va_uid = crgetuid(cr);
1267 	vattr.va_gid = crgetgid(cr);
1268 
1269 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1270 	rootzp->z_zfsvfs = &zfsvfs;
1271 	rootzp->z_unlinked = 0;
1272 	rootzp->z_atime_dirty = 0;
1273 
1274 	vp = ZTOV(rootzp);
1275 	vn_reinit(vp);
1276 	vp->v_type = VDIR;
1277 
1278 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1279 
1280 	zfsvfs.z_os = os;
1281 	zfsvfs.z_assign = TXG_NOWAIT;
1282 	zfsvfs.z_parent = &zfsvfs;
1283 	zfsvfs.z_version = version;
1284 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1285 	zfsvfs.z_norm = norm;
1286 	/*
1287 	 * Fold case on file systems that are always or sometimes case
1288 	 * insensitive.
1289 	 */
1290 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1291 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1292 
1293 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1294 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1295 	    offsetof(znode_t, z_link_node));
1296 
1297 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE,
1298 	    &zp, 0, NULL, NULL);
1299 	VN_RELE(ZTOV(zp));
1300 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1301 	ASSERT(error == 0);
1302 
1303 	ZTOV(rootzp)->v_count = 0;
1304 	kmem_cache_free(znode_cache, rootzp);
1305 }
1306 
1307 #endif /* _KERNEL */
1308 /*
1309  * Given an object number, return its parent object number and whether
1310  * or not the object is an extended attribute directory.
1311  */
1312 static int
1313 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1314 {
1315 	dmu_buf_t *db;
1316 	dmu_object_info_t doi;
1317 	znode_phys_t *zp;
1318 	int error;
1319 
1320 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1321 		return (error);
1322 
1323 	dmu_object_info_from_db(db, &doi);
1324 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1325 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1326 		dmu_buf_rele(db, FTAG);
1327 		return (EINVAL);
1328 	}
1329 
1330 	zp = db->db_data;
1331 	*pobjp = zp->zp_parent;
1332 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1333 	    S_ISDIR(zp->zp_mode);
1334 	dmu_buf_rele(db, FTAG);
1335 
1336 	return (0);
1337 }
1338 
1339 int
1340 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1341 {
1342 	char *path = buf + len - 1;
1343 	int error;
1344 
1345 	*path = '\0';
1346 
1347 	for (;;) {
1348 		uint64_t pobj;
1349 		char component[MAXNAMELEN + 2];
1350 		size_t complen;
1351 		int is_xattrdir;
1352 
1353 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1354 		    &is_xattrdir)) != 0)
1355 			break;
1356 
1357 		if (pobj == obj) {
1358 			if (path[0] != '/')
1359 				*--path = '/';
1360 			break;
1361 		}
1362 
1363 		component[0] = '/';
1364 		if (is_xattrdir) {
1365 			(void) sprintf(component + 1, "<xattrdir>");
1366 		} else {
1367 			error = zap_value_search(osp, pobj, obj,
1368 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
1369 			if (error != 0)
1370 				break;
1371 		}
1372 
1373 		complen = strlen(component);
1374 		path -= complen;
1375 		ASSERT(path >= buf);
1376 		bcopy(component, path, complen);
1377 		obj = pobj;
1378 	}
1379 
1380 	if (error == 0)
1381 		(void) memmove(buf, path, buf + len - path);
1382 	return (error);
1383 }
1384