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