xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_znode.c (revision d0538f66491267879b7418b21ad78e3dcc2dcc83)
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_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 
848 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
849 
850 	return (0);
851 }
852 
853 void
854 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
855 {
856 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
857 	uint64_t obj = zp->z_id;
858 
859 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
860 	if (zp->z_phys->zp_acl.z_acl_extern_obj) {
861 		VERIFY(0 == dmu_object_free(zfsvfs->z_os,
862 		    zp->z_phys->zp_acl.z_acl_extern_obj, tx));
863 	}
864 	VERIFY(0 == dmu_object_free(zfsvfs->z_os, obj, tx));
865 	zfs_znode_dmu_fini(zp);
866 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
867 	zfs_znode_free(zp);
868 }
869 
870 void
871 zfs_zinactive(znode_t *zp)
872 {
873 	vnode_t	*vp = ZTOV(zp);
874 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
875 	uint64_t z_id = zp->z_id;
876 
877 	ASSERT(zp->z_dbuf && zp->z_phys);
878 
879 	/*
880 	 * Don't allow a zfs_zget() while were trying to release this znode
881 	 */
882 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
883 
884 	mutex_enter(&zp->z_lock);
885 	mutex_enter(&vp->v_lock);
886 	vp->v_count--;
887 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
888 		/*
889 		 * If the hold count is greater than zero, somebody has
890 		 * obtained a new reference on this znode while we were
891 		 * processing it here, so we are done.  If we still have
892 		 * mapped pages then we are also done, since we don't
893 		 * want to inactivate the znode until the pages get pushed.
894 		 *
895 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
896 		 * this seems like it would leave the znode hanging with
897 		 * no chance to go inactive...
898 		 */
899 		mutex_exit(&vp->v_lock);
900 		mutex_exit(&zp->z_lock);
901 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
902 		return;
903 	}
904 	mutex_exit(&vp->v_lock);
905 
906 	/*
907 	 * If this was the last reference to a file with no links,
908 	 * remove the file from the file system.
909 	 */
910 	if (zp->z_unlinked) {
911 		mutex_exit(&zp->z_lock);
912 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
913 		zfs_rmnode(zp);
914 		return;
915 	}
916 	mutex_exit(&zp->z_lock);
917 	zfs_znode_dmu_fini(zp);
918 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
919 	zfs_znode_free(zp);
920 }
921 
922 void
923 zfs_znode_free(znode_t *zp)
924 {
925 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
926 
927 	vn_invalid(ZTOV(zp));
928 
929 	mutex_enter(&zfsvfs->z_znodes_lock);
930 	list_remove(&zfsvfs->z_all_znodes, zp);
931 	mutex_exit(&zfsvfs->z_znodes_lock);
932 
933 	kmem_cache_free(znode_cache, zp);
934 
935 	VFS_RELE(zfsvfs->z_vfs);
936 }
937 
938 void
939 zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
940 {
941 	timestruc_t	now;
942 
943 	ASSERT(MUTEX_HELD(&zp->z_lock));
944 
945 	gethrestime(&now);
946 
947 	if (tx) {
948 		dmu_buf_will_dirty(zp->z_dbuf, tx);
949 		zp->z_atime_dirty = 0;
950 		zp->z_seq++;
951 	} else {
952 		zp->z_atime_dirty = 1;
953 	}
954 
955 	if (flag & AT_ATIME)
956 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
957 
958 	if (flag & AT_MTIME) {
959 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
960 		if (zp->z_zfsvfs->z_use_fuids)
961 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
962 	}
963 
964 	if (flag & AT_CTIME) {
965 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
966 		if (zp->z_zfsvfs->z_use_fuids)
967 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
968 	}
969 }
970 
971 /*
972  * Update the requested znode timestamps with the current time.
973  * If we are in a transaction, then go ahead and mark the znode
974  * dirty in the transaction so the timestamps will go to disk.
975  * Otherwise, we will get pushed next time the znode is updated
976  * in a transaction, or when this znode eventually goes inactive.
977  *
978  * Why is this OK?
979  *  1 - Only the ACCESS time is ever updated outside of a transaction.
980  *  2 - Multiple consecutive updates will be collapsed into a single
981  *	znode update by the transaction grouping semantics of the DMU.
982  */
983 void
984 zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
985 {
986 	mutex_enter(&zp->z_lock);
987 	zfs_time_stamper_locked(zp, flag, tx);
988 	mutex_exit(&zp->z_lock);
989 }
990 
991 /*
992  * Grow the block size for a file.
993  *
994  *	IN:	zp	- znode of file to free data in.
995  *		size	- requested block size
996  *		tx	- open transaction.
997  *
998  * NOTE: this function assumes that the znode is write locked.
999  */
1000 void
1001 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1002 {
1003 	int		error;
1004 	u_longlong_t	dummy;
1005 
1006 	if (size <= zp->z_blksz)
1007 		return;
1008 	/*
1009 	 * If the file size is already greater than the current blocksize,
1010 	 * we will not grow.  If there is more than one block in a file,
1011 	 * the blocksize cannot change.
1012 	 */
1013 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1014 		return;
1015 
1016 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1017 	    size, 0, tx);
1018 	if (error == ENOTSUP)
1019 		return;
1020 	ASSERT3U(error, ==, 0);
1021 
1022 	/* What blocksize did we actually get? */
1023 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1024 }
1025 
1026 /*
1027  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1028  * be calling back into the fs for a putpage().  E.g.: when truncating
1029  * a file, the pages being "thrown away* don't need to be written out.
1030  */
1031 /* ARGSUSED */
1032 static int
1033 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1034     int flags, cred_t *cr)
1035 {
1036 	ASSERT(0);
1037 	return (0);
1038 }
1039 
1040 /*
1041  * Free space in a file.
1042  *
1043  *	IN:	zp	- znode of file to free data in.
1044  *		off	- start of section to free.
1045  *		len	- length of section to free (0 => to EOF).
1046  *		flag	- current file open mode flags.
1047  *
1048  * 	RETURN:	0 if success
1049  *		error code if failure
1050  */
1051 int
1052 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1053 {
1054 	vnode_t *vp = ZTOV(zp);
1055 	dmu_tx_t *tx;
1056 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1057 	zilog_t *zilog = zfsvfs->z_log;
1058 	rl_t *rl;
1059 	uint64_t end = off + len;
1060 	uint64_t size, new_blksz;
1061 	uint64_t pflags = zp->z_phys->zp_flags;
1062 	int error;
1063 
1064 	if ((pflags & (ZFS_IMMUTABLE|ZFS_READONLY)) ||
1065 	    off < zp->z_phys->zp_size && (pflags & ZFS_APPENDONLY))
1066 		return (EPERM);
1067 
1068 	if (ZTOV(zp)->v_type == VFIFO)
1069 		return (0);
1070 
1071 	/*
1072 	 * If we will change zp_size then lock the whole file,
1073 	 * otherwise just lock the range being freed.
1074 	 */
1075 	if (len == 0 || off + len > zp->z_phys->zp_size) {
1076 		rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1077 	} else {
1078 		rl = zfs_range_lock(zp, off, len, RL_WRITER);
1079 		/* recheck, in case zp_size changed */
1080 		if (off + len > zp->z_phys->zp_size) {
1081 			/* lost race: file size changed, lock whole file */
1082 			zfs_range_unlock(rl);
1083 			rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1084 		}
1085 	}
1086 
1087 	/*
1088 	 * Nothing to do if file already at desired length.
1089 	 */
1090 	size = zp->z_phys->zp_size;
1091 	if (len == 0 && size == off && off != 0) {
1092 		zfs_range_unlock(rl);
1093 		return (0);
1094 	}
1095 
1096 	/*
1097 	 * Check for any locks in the region to be freed.
1098 	 */
1099 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
1100 		uint64_t start = off;
1101 		uint64_t extent = len;
1102 
1103 		if (off > size) {
1104 			start = size;
1105 			extent += off - size;
1106 		} else if (len == 0) {
1107 			extent = size - off;
1108 		}
1109 		if (error = chklock(vp, FWRITE, start, extent, flag, NULL)) {
1110 			zfs_range_unlock(rl);
1111 			return (error);
1112 		}
1113 	}
1114 
1115 	tx = dmu_tx_create(zfsvfs->z_os);
1116 	dmu_tx_hold_bonus(tx, zp->z_id);
1117 	new_blksz = 0;
1118 	if (end > size &&
1119 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1120 		/*
1121 		 * We are growing the file past the current block size.
1122 		 */
1123 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1124 			ASSERT(!ISP2(zp->z_blksz));
1125 			new_blksz = MIN(end, SPA_MAXBLOCKSIZE);
1126 		} else {
1127 			new_blksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1128 		}
1129 		dmu_tx_hold_write(tx, zp->z_id, 0, MIN(end, new_blksz));
1130 	} else if (off < size) {
1131 		/*
1132 		 * If len == 0, we are truncating the file.
1133 		 */
1134 		dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END);
1135 	}
1136 
1137 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1138 	if (error) {
1139 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
1140 			dmu_tx_wait(tx);
1141 		dmu_tx_abort(tx);
1142 		zfs_range_unlock(rl);
1143 		return (error);
1144 	}
1145 
1146 	if (new_blksz)
1147 		zfs_grow_blocksize(zp, new_blksz, tx);
1148 
1149 	if (end > size || len == 0)
1150 		zp->z_phys->zp_size = end;
1151 
1152 	if (off < size) {
1153 		objset_t *os = zfsvfs->z_os;
1154 		uint64_t rlen = len;
1155 
1156 		if (len == 0)
1157 			rlen = -1;
1158 		else if (end > size)
1159 			rlen = size - off;
1160 		VERIFY(0 == dmu_free_range(os, zp->z_id, off, rlen, tx));
1161 	}
1162 
1163 	if (log) {
1164 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1165 		zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1166 	}
1167 
1168 	zfs_range_unlock(rl);
1169 
1170 	dmu_tx_commit(tx);
1171 
1172 	/*
1173 	 * Clear any mapped pages in the truncated region.  This has to
1174 	 * happen outside of the transaction to avoid the possibility of
1175 	 * a deadlock with someone trying to push a page that we are
1176 	 * about to invalidate.
1177 	 */
1178 	rw_enter(&zp->z_map_lock, RW_WRITER);
1179 	if (off < size && vn_has_cached_data(vp)) {
1180 		page_t *pp;
1181 		uint64_t start = off & PAGEMASK;
1182 		int poff = off & PAGEOFFSET;
1183 
1184 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1185 			/*
1186 			 * We need to zero a partial page.
1187 			 */
1188 			pagezero(pp, poff, PAGESIZE - poff);
1189 			start += PAGESIZE;
1190 			page_unlock(pp);
1191 		}
1192 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1193 		    B_INVAL | B_TRUNC, NULL);
1194 		ASSERT(error == 0);
1195 	}
1196 	rw_exit(&zp->z_map_lock);
1197 
1198 	return (0);
1199 }
1200 
1201 void
1202 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1203 {
1204 	zfsvfs_t	zfsvfs;
1205 	uint64_t	moid, doid;
1206 	uint64_t	version = 0;
1207 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1208 	uint64_t	norm = 0;
1209 	nvpair_t	*elem;
1210 	int		error;
1211 	znode_t		*rootzp = NULL;
1212 	vnode_t		*vp;
1213 	vattr_t		vattr;
1214 	znode_t		*zp;
1215 
1216 	/*
1217 	 * First attempt to create master node.
1218 	 */
1219 	/*
1220 	 * In an empty objset, there are no blocks to read and thus
1221 	 * there can be no i/o errors (which we assert below).
1222 	 */
1223 	moid = MASTER_NODE_OBJ;
1224 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1225 	    DMU_OT_NONE, 0, tx);
1226 	ASSERT(error == 0);
1227 
1228 	/*
1229 	 * Set starting attributes.
1230 	 */
1231 	elem = NULL;
1232 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1233 		/* For the moment we expect all zpl props to be uint64_ts */
1234 		uint64_t val;
1235 		char *name;
1236 
1237 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1238 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1239 		name = nvpair_name(elem);
1240 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1241 			version = val;
1242 			error = zap_update(os, moid, ZPL_VERSION_STR,
1243 			    8, 1, &version, tx);
1244 		} else {
1245 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1246 		}
1247 		ASSERT(error == 0);
1248 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1249 			norm = val;
1250 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1251 			sense = val;
1252 	}
1253 	ASSERT(version != 0);
1254 
1255 	/*
1256 	 * Create a delete queue.
1257 	 */
1258 	doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1259 
1260 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx);
1261 	ASSERT(error == 0);
1262 
1263 	/*
1264 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1265 	 * to allow zfs_mknode to work.
1266 	 */
1267 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1268 	vattr.va_type = VDIR;
1269 	vattr.va_mode = S_IFDIR|0755;
1270 	vattr.va_uid = crgetuid(cr);
1271 	vattr.va_gid = crgetgid(cr);
1272 
1273 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1274 	rootzp->z_zfsvfs = &zfsvfs;
1275 	rootzp->z_unlinked = 0;
1276 	rootzp->z_atime_dirty = 0;
1277 
1278 	vp = ZTOV(rootzp);
1279 	vn_reinit(vp);
1280 	vp->v_type = VDIR;
1281 
1282 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1283 
1284 	zfsvfs.z_os = os;
1285 	zfsvfs.z_assign = TXG_NOWAIT;
1286 	zfsvfs.z_parent = &zfsvfs;
1287 	zfsvfs.z_version = version;
1288 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1289 	zfsvfs.z_norm = norm;
1290 	/*
1291 	 * Fold case on file systems that are always or sometimes case
1292 	 * insensitive.
1293 	 */
1294 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1295 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1296 
1297 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1298 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1299 	    offsetof(znode_t, z_link_node));
1300 
1301 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, NULL, NULL);
1302 	ASSERT3P(zp, ==, rootzp);
1303 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1304 	ASSERT(error == 0);
1305 
1306 	ZTOV(rootzp)->v_count = 0;
1307 	dmu_buf_rele(rootzp->z_dbuf, NULL);
1308 	rootzp->z_dbuf = NULL;
1309 	kmem_cache_free(znode_cache, rootzp);
1310 }
1311 
1312 #endif /* _KERNEL */
1313 /*
1314  * Given an object number, return its parent object number and whether
1315  * or not the object is an extended attribute directory.
1316  */
1317 static int
1318 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1319 {
1320 	dmu_buf_t *db;
1321 	dmu_object_info_t doi;
1322 	znode_phys_t *zp;
1323 	int error;
1324 
1325 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1326 		return (error);
1327 
1328 	dmu_object_info_from_db(db, &doi);
1329 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1330 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1331 		dmu_buf_rele(db, FTAG);
1332 		return (EINVAL);
1333 	}
1334 
1335 	zp = db->db_data;
1336 	*pobjp = zp->zp_parent;
1337 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1338 	    S_ISDIR(zp->zp_mode);
1339 	dmu_buf_rele(db, FTAG);
1340 
1341 	return (0);
1342 }
1343 
1344 int
1345 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1346 {
1347 	char *path = buf + len - 1;
1348 	int error;
1349 
1350 	*path = '\0';
1351 
1352 	for (;;) {
1353 		uint64_t pobj;
1354 		char component[MAXNAMELEN + 2];
1355 		size_t complen;
1356 		int is_xattrdir;
1357 
1358 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1359 		    &is_xattrdir)) != 0)
1360 			break;
1361 
1362 		if (pobj == obj) {
1363 			if (path[0] != '/')
1364 				*--path = '/';
1365 			break;
1366 		}
1367 
1368 		component[0] = '/';
1369 		if (is_xattrdir) {
1370 			(void) sprintf(component + 1, "<xattrdir>");
1371 		} else {
1372 			error = zap_value_search(osp, pobj, obj,
1373 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
1374 			if (error != 0)
1375 				break;
1376 		}
1377 
1378 		complen = strlen(component);
1379 		path -= complen;
1380 		ASSERT(path >= buf);
1381 		bcopy(component, path, complen);
1382 		obj = pobj;
1383 	}
1384 
1385 	if (error == 0)
1386 		(void) memmove(buf, path, buf + len - path);
1387 	return (error);
1388 }
1389