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