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