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