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