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