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