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