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