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