xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_znode.c (revision 8a3c961b6b8e22607c570d092514b791eb1519e9)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /* Portions Copyright 2007 Jeremy Teo */
27 
28 #ifdef _KERNEL
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/mntent.h>
36 #include <sys/mkdev.h>
37 #include <sys/u8_textprep.h>
38 #include <sys/dsl_dataset.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/errno.h>
45 #include <sys/unistd.h>
46 #include <sys/mode.h>
47 #include <sys/atomic.h>
48 #include <vm/pvn.h>
49 #include "fs/fs_subr.h"
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_rlock.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/kidmap.h>
57 #endif /* _KERNEL */
58 
59 #include <sys/dmu.h>
60 #include <sys/refcount.h>
61 #include <sys/stat.h>
62 #include <sys/zap.h>
63 #include <sys/zfs_znode.h>
64 
65 #include "zfs_prop.h"
66 
67 /*
68  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
69  * turned on when DEBUG is also defined.
70  */
71 #ifdef	DEBUG
72 #define	ZNODE_STATS
73 #endif	/* DEBUG */
74 
75 #ifdef	ZNODE_STATS
76 #define	ZNODE_STAT_ADD(stat)			((stat)++)
77 #else
78 #define	ZNODE_STAT_ADD(stat)			/* nothing */
79 #endif	/* ZNODE_STATS */
80 
81 #define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
82 #define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
83 
84 /*
85  * Functions needed for userland (ie: libzpool) are not put under
86  * #ifdef_KERNEL; the rest of the functions have dependencies
87  * (such as VFS logic) that will not compile easily in userland.
88  */
89 #ifdef _KERNEL
90 static kmem_cache_t *znode_cache = NULL;
91 
92 /*ARGSUSED*/
93 static void
94 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
95 {
96 	/*
97 	 * We should never drop all dbuf refs without first clearing
98 	 * the eviction callback.
99 	 */
100 	panic("evicting znode %p\n", user_ptr);
101 }
102 
103 /*ARGSUSED*/
104 static int
105 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
106 {
107 	znode_t *zp = buf;
108 
109 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
110 
111 	zp->z_vnode = vn_alloc(kmflags);
112 	if (zp->z_vnode == NULL) {
113 		return (-1);
114 	}
115 	ZTOV(zp)->v_data = zp;
116 
117 	list_link_init(&zp->z_link_node);
118 
119 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
120 	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
121 	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
122 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
123 
124 	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
125 	avl_create(&zp->z_range_avl, zfs_range_compare,
126 	    sizeof (rl_t), offsetof(rl_t, r_node));
127 
128 	zp->z_dbuf = NULL;
129 	zp->z_dirlocks = NULL;
130 	return (0);
131 }
132 
133 /*ARGSUSED*/
134 static void
135 zfs_znode_cache_destructor(void *buf, void *arg)
136 {
137 	znode_t *zp = buf;
138 
139 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
140 	ASSERT(ZTOV(zp)->v_data == zp);
141 	vn_free(ZTOV(zp));
142 	ASSERT(!list_link_active(&zp->z_link_node));
143 	mutex_destroy(&zp->z_lock);
144 	rw_destroy(&zp->z_parent_lock);
145 	rw_destroy(&zp->z_name_lock);
146 	mutex_destroy(&zp->z_acl_lock);
147 	avl_destroy(&zp->z_range_avl);
148 	mutex_destroy(&zp->z_range_lock);
149 
150 	ASSERT(zp->z_dbuf == NULL);
151 	ASSERT(zp->z_dirlocks == NULL);
152 }
153 
154 #ifdef	ZNODE_STATS
155 static struct {
156 	uint64_t zms_zfsvfs_invalid;
157 	uint64_t zms_zfsvfs_unmounted;
158 	uint64_t zms_zfsvfs_recheck_invalid;
159 	uint64_t zms_obj_held;
160 	uint64_t zms_vnode_locked;
161 	uint64_t zms_not_only_dnlc;
162 } znode_move_stats;
163 #endif	/* ZNODE_STATS */
164 
165 static void
166 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
167 {
168 	vnode_t *vp;
169 
170 	/* Copy fields. */
171 	nzp->z_zfsvfs = ozp->z_zfsvfs;
172 
173 	/* Swap vnodes. */
174 	vp = nzp->z_vnode;
175 	nzp->z_vnode = ozp->z_vnode;
176 	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
177 	ZTOV(ozp)->v_data = ozp;
178 	ZTOV(nzp)->v_data = nzp;
179 
180 	nzp->z_id = ozp->z_id;
181 	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
182 	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
183 	nzp->z_unlinked = ozp->z_unlinked;
184 	nzp->z_atime_dirty = ozp->z_atime_dirty;
185 	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
186 	nzp->z_blksz = ozp->z_blksz;
187 	nzp->z_seq = ozp->z_seq;
188 	nzp->z_mapcnt = ozp->z_mapcnt;
189 	nzp->z_last_itx = ozp->z_last_itx;
190 	nzp->z_gen = ozp->z_gen;
191 	nzp->z_sync_cnt = ozp->z_sync_cnt;
192 	nzp->z_phys = ozp->z_phys;
193 	nzp->z_dbuf = ozp->z_dbuf;
194 
195 	/* Update back pointers. */
196 	(void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys,
197 	    znode_evict_error);
198 
199 	/*
200 	 * Invalidate the original znode by clearing fields that provide a
201 	 * pointer back to the znode. Set the low bit of the vfs pointer to
202 	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
203 	 * subsequent callback.
204 	 */
205 	ozp->z_dbuf = NULL;
206 	POINTER_INVALIDATE(&ozp->z_zfsvfs);
207 }
208 
209 /*
210  * Wrapper function for ZFS_ENTER that returns 0 if successful and otherwise
211  * returns a non-zero error code.
212  */
213 static int
214 zfs_enter(zfsvfs_t *zfsvfs)
215 {
216 	ZFS_ENTER(zfsvfs);
217 	return (0);
218 }
219 
220 /*ARGSUSED*/
221 static kmem_cbrc_t
222 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
223 {
224 	znode_t *ozp = buf, *nzp = newbuf;
225 	zfsvfs_t *zfsvfs;
226 	vnode_t *vp;
227 
228 	/*
229 	 * The znode is on the file system's list of known znodes if the vfs
230 	 * pointer is valid. We set the low bit of the vfs pointer when freeing
231 	 * the znode to invalidate it, and the memory patterns written by kmem
232 	 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
233 	 * created znode sets the vfs pointer last of all to indicate that the
234 	 * znode is known and in a valid state to be moved by this function.
235 	 */
236 	zfsvfs = ozp->z_zfsvfs;
237 	if (!POINTER_IS_VALID(zfsvfs)) {
238 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
239 		return (KMEM_CBRC_DONT_KNOW);
240 	}
241 
242 	/*
243 	 * Ensure that the filesystem is not unmounted during the move.
244 	 */
245 	if (zfs_enter(zfsvfs) != 0) {		/* ZFS_ENTER */
246 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
247 		return (KMEM_CBRC_DONT_KNOW);
248 	}
249 
250 	mutex_enter(&zfsvfs->z_znodes_lock);
251 	/*
252 	 * Recheck the vfs pointer in case the znode was removed just before
253 	 * acquiring the lock.
254 	 */
255 	if (zfsvfs != ozp->z_zfsvfs) {
256 		mutex_exit(&zfsvfs->z_znodes_lock);
257 		ZFS_EXIT(zfsvfs);
258 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck_invalid);
259 		return (KMEM_CBRC_DONT_KNOW);
260 	}
261 
262 	/*
263 	 * At this point we know that as long as we hold z_znodes_lock, the
264 	 * znode cannot be freed and fields within the znode can be safely
265 	 * accessed. Now, prevent a race with zfs_zget().
266 	 */
267 	if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
268 		mutex_exit(&zfsvfs->z_znodes_lock);
269 		ZFS_EXIT(zfsvfs);
270 		ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
271 		return (KMEM_CBRC_LATER);
272 	}
273 
274 	vp = ZTOV(ozp);
275 	if (mutex_tryenter(&vp->v_lock) == 0) {
276 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
277 		mutex_exit(&zfsvfs->z_znodes_lock);
278 		ZFS_EXIT(zfsvfs);
279 		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
280 		return (KMEM_CBRC_LATER);
281 	}
282 
283 	/* Only move znodes that are referenced _only_ by the DNLC. */
284 	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
285 		mutex_exit(&vp->v_lock);
286 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
287 		mutex_exit(&zfsvfs->z_znodes_lock);
288 		ZFS_EXIT(zfsvfs);
289 		ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
290 		return (KMEM_CBRC_LATER);
291 	}
292 
293 	/*
294 	 * The znode is known and in a valid state to move. We're holding the
295 	 * locks needed to execute the critical section.
296 	 */
297 	zfs_znode_move_impl(ozp, nzp);
298 	mutex_exit(&vp->v_lock);
299 	ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
300 
301 	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
302 	mutex_exit(&zfsvfs->z_znodes_lock);
303 	ZFS_EXIT(zfsvfs);
304 
305 	return (KMEM_CBRC_YES);
306 }
307 
308 void
309 zfs_znode_init(void)
310 {
311 	/*
312 	 * Initialize zcache
313 	 */
314 	ASSERT(znode_cache == NULL);
315 	znode_cache = kmem_cache_create("zfs_znode_cache",
316 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
317 	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
318 	kmem_cache_set_move(znode_cache, zfs_znode_move);
319 }
320 
321 void
322 zfs_znode_fini(void)
323 {
324 	/*
325 	 * Cleanup vfs & vnode ops
326 	 */
327 	zfs_remove_op_tables();
328 
329 	/*
330 	 * Cleanup zcache
331 	 */
332 	if (znode_cache)
333 		kmem_cache_destroy(znode_cache);
334 	znode_cache = NULL;
335 }
336 
337 struct vnodeops *zfs_dvnodeops;
338 struct vnodeops *zfs_fvnodeops;
339 struct vnodeops *zfs_symvnodeops;
340 struct vnodeops *zfs_xdvnodeops;
341 struct vnodeops *zfs_evnodeops;
342 struct vnodeops *zfs_sharevnodeops;
343 
344 void
345 zfs_remove_op_tables()
346 {
347 	/*
348 	 * Remove vfs ops
349 	 */
350 	ASSERT(zfsfstype);
351 	(void) vfs_freevfsops_by_type(zfsfstype);
352 	zfsfstype = 0;
353 
354 	/*
355 	 * Remove vnode ops
356 	 */
357 	if (zfs_dvnodeops)
358 		vn_freevnodeops(zfs_dvnodeops);
359 	if (zfs_fvnodeops)
360 		vn_freevnodeops(zfs_fvnodeops);
361 	if (zfs_symvnodeops)
362 		vn_freevnodeops(zfs_symvnodeops);
363 	if (zfs_xdvnodeops)
364 		vn_freevnodeops(zfs_xdvnodeops);
365 	if (zfs_evnodeops)
366 		vn_freevnodeops(zfs_evnodeops);
367 	if (zfs_sharevnodeops)
368 		vn_freevnodeops(zfs_sharevnodeops);
369 
370 	zfs_dvnodeops = NULL;
371 	zfs_fvnodeops = NULL;
372 	zfs_symvnodeops = NULL;
373 	zfs_xdvnodeops = NULL;
374 	zfs_evnodeops = NULL;
375 	zfs_sharevnodeops = NULL;
376 }
377 
378 extern const fs_operation_def_t zfs_dvnodeops_template[];
379 extern const fs_operation_def_t zfs_fvnodeops_template[];
380 extern const fs_operation_def_t zfs_xdvnodeops_template[];
381 extern const fs_operation_def_t zfs_symvnodeops_template[];
382 extern const fs_operation_def_t zfs_evnodeops_template[];
383 extern const fs_operation_def_t zfs_sharevnodeops_template[];
384 
385 int
386 zfs_create_op_tables()
387 {
388 	int error;
389 
390 	/*
391 	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
392 	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
393 	 * In this case we just return as the ops vectors are already set up.
394 	 */
395 	if (zfs_dvnodeops)
396 		return (0);
397 
398 	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
399 	    &zfs_dvnodeops);
400 	if (error)
401 		return (error);
402 
403 	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
404 	    &zfs_fvnodeops);
405 	if (error)
406 		return (error);
407 
408 	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
409 	    &zfs_symvnodeops);
410 	if (error)
411 		return (error);
412 
413 	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
414 	    &zfs_xdvnodeops);
415 	if (error)
416 		return (error);
417 
418 	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
419 	    &zfs_evnodeops);
420 	if (error)
421 		return (error);
422 
423 	error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
424 	    &zfs_sharevnodeops);
425 
426 	return (error);
427 }
428 
429 int
430 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
431 {
432 	vattr_t vattr;
433 	znode_t *sharezp;
434 	vnode_t *vp;
435 	znode_t *zp;
436 	int error;
437 
438 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
439 	vattr.va_type = VDIR;
440 	vattr.va_mode = S_IFDIR|0555;
441 	vattr.va_uid = crgetuid(kcred);
442 	vattr.va_gid = crgetgid(kcred);
443 
444 	sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
445 	sharezp->z_unlinked = 0;
446 	sharezp->z_atime_dirty = 0;
447 	sharezp->z_zfsvfs = zfsvfs;
448 
449 	vp = ZTOV(sharezp);
450 	vn_reinit(vp);
451 	vp->v_type = VDIR;
452 
453 	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE,
454 	    &zp, 0, NULL, NULL);
455 	ASSERT3P(zp, ==, sharezp);
456 	ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
457 	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
458 	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
459 	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
460 	zfsvfs->z_shares_dir = sharezp->z_id;
461 
462 	ZTOV(sharezp)->v_count = 0;
463 	dmu_buf_rele(sharezp->z_dbuf, NULL);
464 	sharezp->z_dbuf = NULL;
465 	kmem_cache_free(znode_cache, sharezp);
466 
467 	return (error);
468 }
469 
470 /*
471  * zfs_init_fs - Initialize the zfsvfs struct and the file system
472  *	incore "master" object.  Verify version compatibility.
473  */
474 int
475 zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp)
476 {
477 	extern int zfsfstype;
478 
479 	objset_t	*os = zfsvfs->z_os;
480 	int		i, error;
481 	uint64_t fsid_guid;
482 	uint64_t zval;
483 
484 	*zpp = NULL;
485 
486 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
487 	if (error) {
488 		return (error);
489 	} else if (zfsvfs->z_version > ZPL_VERSION) {
490 		(void) printf("Mismatched versions:  File system "
491 		    "is version %llu on-disk format, which is "
492 		    "incompatible with this software version %lld!",
493 		    (u_longlong_t)zfsvfs->z_version, ZPL_VERSION);
494 		return (ENOTSUP);
495 	}
496 
497 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
498 		return (error);
499 	zfsvfs->z_norm = (int)zval;
500 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
501 		return (error);
502 	zfsvfs->z_utf8 = (zval != 0);
503 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
504 		return (error);
505 	zfsvfs->z_case = (uint_t)zval;
506 	/*
507 	 * Fold case on file systems that are always or sometimes case
508 	 * insensitive.
509 	 */
510 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
511 	    zfsvfs->z_case == ZFS_CASE_MIXED)
512 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
513 
514 	/*
515 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
516 	 * separates our fsid from any other filesystem types, and a
517 	 * 56-bit objset unique ID.  The objset unique ID is unique to
518 	 * all objsets open on this system, provided by unique_create().
519 	 * The 8-bit fs type must be put in the low bits of fsid[1]
520 	 * because that's where other Solaris filesystems put it.
521 	 */
522 	fsid_guid = dmu_objset_fsid_guid(os);
523 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
524 	zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid;
525 	zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
526 	    zfsfstype & 0xFF;
527 
528 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
529 	    &zfsvfs->z_root);
530 	if (error)
531 		return (error);
532 	ASSERT(zfsvfs->z_root != 0);
533 
534 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
535 	    &zfsvfs->z_unlinkedobj);
536 	if (error)
537 		return (error);
538 
539 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
540 	    &zfsvfs->z_fuid_obj);
541 	if (error == ENOENT)
542 		error = 0;
543 
544 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
545 	    &zfsvfs->z_shares_dir);
546 	if (error && error != ENOENT)
547 		return (error);
548 
549 	/*
550 	 * Initialize zget mutex's
551 	 */
552 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
553 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
554 
555 	error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp);
556 	if (error) {
557 		/*
558 		 * On error, we destroy the mutexes here since it's not
559 		 * possible for the caller to determine if the mutexes were
560 		 * initialized properly.
561 		 */
562 		for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
563 			mutex_destroy(&zfsvfs->z_hold_mtx[i]);
564 		return (error);
565 	}
566 	ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root);
567 
568 	return (error);
569 }
570 
571 /*
572  * define a couple of values we need available
573  * for both 64 and 32 bit environments.
574  */
575 #ifndef NBITSMINOR64
576 #define	NBITSMINOR64	32
577 #endif
578 #ifndef MAXMAJ64
579 #define	MAXMAJ64	0xffffffffUL
580 #endif
581 #ifndef	MAXMIN64
582 #define	MAXMIN64	0xffffffffUL
583 #endif
584 
585 /*
586  * Create special expldev for ZFS private use.
587  * Can't use standard expldev since it doesn't do
588  * what we want.  The standard expldev() takes a
589  * dev32_t in LP64 and expands it to a long dev_t.
590  * We need an interface that takes a dev32_t in ILP32
591  * and expands it to a long dev_t.
592  */
593 static uint64_t
594 zfs_expldev(dev_t dev)
595 {
596 #ifndef _LP64
597 	major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
598 	return (((uint64_t)major << NBITSMINOR64) |
599 	    ((minor_t)dev & MAXMIN32));
600 #else
601 	return (dev);
602 #endif
603 }
604 
605 /*
606  * Special cmpldev for ZFS private use.
607  * Can't use standard cmpldev since it takes
608  * a long dev_t and compresses it to dev32_t in
609  * LP64.  We need to do a compaction of a long dev_t
610  * to a dev32_t in ILP32.
611  */
612 dev_t
613 zfs_cmpldev(uint64_t dev)
614 {
615 #ifndef _LP64
616 	minor_t minor = (minor_t)dev & MAXMIN64;
617 	major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
618 
619 	if (major > MAXMAJ32 || minor > MAXMIN32)
620 		return (NODEV32);
621 
622 	return (((dev32_t)major << NBITSMINOR32) | minor);
623 #else
624 	return (dev);
625 #endif
626 }
627 
628 static void
629 zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
630 {
631 	znode_t		*nzp;
632 
633 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
634 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
635 
636 	mutex_enter(&zp->z_lock);
637 
638 	ASSERT(zp->z_dbuf == NULL);
639 	zp->z_dbuf = db;
640 	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error);
641 
642 	/*
643 	 * there should be no
644 	 * concurrent zgets on this object.
645 	 */
646 	if (nzp != NULL)
647 		panic("existing znode %p for dbuf %p", (void *)nzp, (void *)db);
648 
649 	/*
650 	 * Slap on VROOT if we are the root znode
651 	 */
652 	if (zp->z_id == zfsvfs->z_root)
653 		ZTOV(zp)->v_flag |= VROOT;
654 
655 	mutex_exit(&zp->z_lock);
656 	vn_exists(ZTOV(zp));
657 }
658 
659 void
660 zfs_znode_dmu_fini(znode_t *zp)
661 {
662 	dmu_buf_t *db = zp->z_dbuf;
663 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
664 	    zp->z_unlinked ||
665 	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
666 	ASSERT(zp->z_dbuf != NULL);
667 	zp->z_dbuf = NULL;
668 	VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL));
669 	dmu_buf_rele(db, NULL);
670 }
671 
672 /*
673  * Construct a new znode/vnode and intialize.
674  *
675  * This does not do a call to dmu_set_user() that is
676  * up to the caller to do, in case you don't want to
677  * return the znode
678  */
679 static znode_t *
680 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
681 {
682 	znode_t	*zp;
683 	vnode_t *vp;
684 
685 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
686 
687 	ASSERT(zp->z_dirlocks == NULL);
688 	ASSERT(zp->z_dbuf == NULL);
689 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
690 
691 	/*
692 	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
693 	 * the zfs_znode_move() callback.
694 	 */
695 	zp->z_phys = NULL;
696 	zp->z_unlinked = 0;
697 	zp->z_atime_dirty = 0;
698 	zp->z_mapcnt = 0;
699 	zp->z_last_itx = 0;
700 	zp->z_id = db->db_object;
701 	zp->z_blksz = blksz;
702 	zp->z_seq = 0x7A4653;
703 	zp->z_sync_cnt = 0;
704 
705 	vp = ZTOV(zp);
706 	vn_reinit(vp);
707 
708 	zfs_znode_dmu_init(zfsvfs, zp, db);
709 
710 	zp->z_gen = zp->z_phys->zp_gen;
711 
712 	vp->v_vfsp = zfsvfs->z_parent->z_vfs;
713 	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
714 
715 	switch (vp->v_type) {
716 	case VDIR:
717 		if (zp->z_phys->zp_flags & ZFS_XATTR) {
718 			vn_setops(vp, zfs_xdvnodeops);
719 			vp->v_flag |= V_XATTRDIR;
720 		} else {
721 			vn_setops(vp, zfs_dvnodeops);
722 		}
723 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
724 		break;
725 	case VBLK:
726 	case VCHR:
727 		vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
728 		/*FALLTHROUGH*/
729 	case VFIFO:
730 	case VSOCK:
731 	case VDOOR:
732 		vn_setops(vp, zfs_fvnodeops);
733 		break;
734 	case VREG:
735 		vp->v_flag |= VMODSORT;
736 		if (zp->z_phys->zp_parent == zfsvfs->z_shares_dir)
737 			vn_setops(vp, zfs_sharevnodeops);
738 		else
739 			vn_setops(vp, zfs_fvnodeops);
740 		break;
741 	case VLNK:
742 		vn_setops(vp, zfs_symvnodeops);
743 		break;
744 	default:
745 		vn_setops(vp, zfs_evnodeops);
746 		break;
747 	}
748 
749 	mutex_enter(&zfsvfs->z_znodes_lock);
750 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
751 	membar_producer();
752 	/*
753 	 * Everything else must be valid before assigning z_zfsvfs makes the
754 	 * znode eligible for zfs_znode_move().
755 	 */
756 	zp->z_zfsvfs = zfsvfs;
757 	mutex_exit(&zfsvfs->z_znodes_lock);
758 
759 	VFS_HOLD(zfsvfs->z_vfs);
760 	return (zp);
761 }
762 
763 /*
764  * Create a new DMU object to hold a zfs znode.
765  *
766  *	IN:	dzp	- parent directory for new znode
767  *		vap	- file attributes for new znode
768  *		tx	- dmu transaction id for zap operations
769  *		cr	- credentials of caller
770  *		flag	- flags:
771  *			  IS_ROOT_NODE	- new object will be root
772  *			  IS_XATTR	- new object is an attribute
773  *			  IS_REPLAY	- intent log replay
774  *		bonuslen - length of bonus buffer
775  *		setaclp  - File/Dir initial ACL
776  *		fuidp	 - Tracks fuid allocation.
777  *
778  *	OUT:	zpp	- allocated znode
779  *
780  */
781 void
782 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
783     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_t *setaclp,
784     zfs_fuid_info_t **fuidp)
785 {
786 	dmu_buf_t	*db;
787 	znode_phys_t	*pzp;
788 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
789 	timestruc_t	now;
790 	uint64_t	gen, obj;
791 	int		err;
792 
793 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
794 
795 	if (zfsvfs->z_replay) {
796 		obj = vap->va_nodeid;
797 		flag |= IS_REPLAY;
798 		now = vap->va_ctime;		/* see zfs_replay_create() */
799 		gen = vap->va_nblocks;		/* ditto */
800 	} else {
801 		obj = 0;
802 		gethrestime(&now);
803 		gen = dmu_tx_get_txg(tx);
804 	}
805 
806 	/*
807 	 * Create a new DMU object.
808 	 */
809 	/*
810 	 * There's currently no mechanism for pre-reading the blocks that will
811 	 * be to needed allocate a new object, so we accept the small chance
812 	 * that there will be an i/o error and we will fail one of the
813 	 * assertions below.
814 	 */
815 	if (vap->va_type == VDIR) {
816 		if (flag & IS_REPLAY) {
817 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
818 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
819 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
820 			ASSERT3U(err, ==, 0);
821 		} else {
822 			obj = zap_create_norm(zfsvfs->z_os,
823 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
824 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
825 		}
826 	} else {
827 		if (flag & IS_REPLAY) {
828 			err = dmu_object_claim(zfsvfs->z_os, obj,
829 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
830 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
831 			ASSERT3U(err, ==, 0);
832 		} else {
833 			obj = dmu_object_alloc(zfsvfs->z_os,
834 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
835 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
836 		}
837 	}
838 	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
839 	dmu_buf_will_dirty(db, tx);
840 
841 	/*
842 	 * Initialize the znode physical data to zero.
843 	 */
844 	ASSERT(db->db_size >= sizeof (znode_phys_t));
845 	bzero(db->db_data, db->db_size);
846 	pzp = db->db_data;
847 
848 	/*
849 	 * If this is the root, fix up the half-initialized parent pointer
850 	 * to reference the just-allocated physical data area.
851 	 */
852 	if (flag & IS_ROOT_NODE) {
853 		dzp->z_dbuf = db;
854 		dzp->z_phys = pzp;
855 		dzp->z_id = obj;
856 	}
857 
858 	/*
859 	 * If parent is an xattr, so am I.
860 	 */
861 	if (dzp->z_phys->zp_flags & ZFS_XATTR)
862 		flag |= IS_XATTR;
863 
864 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
865 		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
866 	}
867 
868 	if (zfsvfs->z_use_fuids)
869 		pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
870 
871 	if (vap->va_type == VDIR) {
872 		pzp->zp_size = 2;		/* contents ("." and "..") */
873 		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
874 	}
875 
876 	pzp->zp_parent = dzp->z_id;
877 	if (flag & IS_XATTR)
878 		pzp->zp_flags |= ZFS_XATTR;
879 
880 	pzp->zp_gen = gen;
881 
882 	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
883 	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
884 
885 	if (vap->va_mask & AT_ATIME) {
886 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
887 	} else {
888 		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
889 	}
890 
891 	if (vap->va_mask & AT_MTIME) {
892 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
893 	} else {
894 		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
895 	}
896 
897 	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
898 	if (!(flag & IS_ROOT_NODE)) {
899 		ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
900 		*zpp = zfs_znode_alloc(zfsvfs, db, 0);
901 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
902 	} else {
903 		/*
904 		 * If we are creating the root node, the "parent" we
905 		 * passed in is the znode for the root.
906 		 */
907 		*zpp = dzp;
908 	}
909 	zfs_perm_init(*zpp, dzp, flag, vap, tx, cr, setaclp, fuidp);
910 }
911 
912 void
913 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
914 {
915 	xoptattr_t *xoap;
916 
917 	xoap = xva_getxoptattr(xvap);
918 	ASSERT(xoap);
919 
920 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
921 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
922 		XVA_SET_RTN(xvap, XAT_CREATETIME);
923 	}
924 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
925 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
926 		XVA_SET_RTN(xvap, XAT_READONLY);
927 	}
928 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
929 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
930 		XVA_SET_RTN(xvap, XAT_HIDDEN);
931 	}
932 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
933 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
934 		XVA_SET_RTN(xvap, XAT_SYSTEM);
935 	}
936 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
937 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
938 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
939 	}
940 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
941 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
942 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
943 	}
944 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
945 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
946 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
947 	}
948 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
949 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
950 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
951 	}
952 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
953 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
954 		XVA_SET_RTN(xvap, XAT_NODUMP);
955 	}
956 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
957 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
958 		XVA_SET_RTN(xvap, XAT_OPAQUE);
959 	}
960 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
961 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
962 		    xoap->xoa_av_quarantined);
963 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
964 	}
965 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
966 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
967 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
968 	}
969 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
970 		(void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
971 		    sizeof (xoap->xoa_av_scanstamp));
972 		zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
973 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
974 	}
975 }
976 
977 int
978 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
979 {
980 	dmu_object_info_t doi;
981 	dmu_buf_t	*db;
982 	znode_t		*zp;
983 	int err;
984 
985 	*zpp = NULL;
986 
987 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
988 
989 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
990 	if (err) {
991 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
992 		return (err);
993 	}
994 
995 	dmu_object_info_from_db(db, &doi);
996 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
997 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
998 		dmu_buf_rele(db, NULL);
999 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1000 		return (EINVAL);
1001 	}
1002 
1003 	zp = dmu_buf_get_user(db);
1004 	if (zp != NULL) {
1005 		mutex_enter(&zp->z_lock);
1006 
1007 		/*
1008 		 * Since we do immediate eviction of the z_dbuf, we
1009 		 * should never find a dbuf with a znode that doesn't
1010 		 * know about the dbuf.
1011 		 */
1012 		ASSERT3P(zp->z_dbuf, ==, db);
1013 		ASSERT3U(zp->z_id, ==, obj_num);
1014 		if (zp->z_unlinked) {
1015 			err = ENOENT;
1016 		} else {
1017 			VN_HOLD(ZTOV(zp));
1018 			*zpp = zp;
1019 			err = 0;
1020 		}
1021 		dmu_buf_rele(db, NULL);
1022 		mutex_exit(&zp->z_lock);
1023 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1024 		return (err);
1025 	}
1026 
1027 	/*
1028 	 * Not found create new znode/vnode
1029 	 */
1030 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
1031 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1032 	*zpp = zp;
1033 	return (0);
1034 }
1035 
1036 int
1037 zfs_rezget(znode_t *zp)
1038 {
1039 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1040 	dmu_object_info_t doi;
1041 	dmu_buf_t *db;
1042 	uint64_t obj_num = zp->z_id;
1043 	int err;
1044 
1045 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1046 
1047 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
1048 	if (err) {
1049 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1050 		return (err);
1051 	}
1052 
1053 	dmu_object_info_from_db(db, &doi);
1054 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1055 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1056 		dmu_buf_rele(db, NULL);
1057 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1058 		return (EINVAL);
1059 	}
1060 
1061 	if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
1062 		dmu_buf_rele(db, NULL);
1063 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1064 		return (EIO);
1065 	}
1066 
1067 	zfs_znode_dmu_init(zfsvfs, zp, db);
1068 	zp->z_unlinked = (zp->z_phys->zp_links == 0);
1069 	zp->z_blksz = doi.doi_data_block_size;
1070 
1071 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1072 
1073 	return (0);
1074 }
1075 
1076 void
1077 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1078 {
1079 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1080 	objset_t *os = zfsvfs->z_os;
1081 	uint64_t obj = zp->z_id;
1082 	uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
1083 
1084 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1085 	if (acl_obj)
1086 		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1087 	VERIFY(0 == dmu_object_free(os, obj, tx));
1088 	zfs_znode_dmu_fini(zp);
1089 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1090 	zfs_znode_free(zp);
1091 }
1092 
1093 void
1094 zfs_zinactive(znode_t *zp)
1095 {
1096 	vnode_t	*vp = ZTOV(zp);
1097 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1098 	uint64_t z_id = zp->z_id;
1099 
1100 	ASSERT(zp->z_dbuf && zp->z_phys);
1101 
1102 	/*
1103 	 * Don't allow a zfs_zget() while were trying to release this znode
1104 	 */
1105 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1106 
1107 	mutex_enter(&zp->z_lock);
1108 	mutex_enter(&vp->v_lock);
1109 	vp->v_count--;
1110 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1111 		/*
1112 		 * If the hold count is greater than zero, somebody has
1113 		 * obtained a new reference on this znode while we were
1114 		 * processing it here, so we are done.  If we still have
1115 		 * mapped pages then we are also done, since we don't
1116 		 * want to inactivate the znode until the pages get pushed.
1117 		 *
1118 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1119 		 * this seems like it would leave the znode hanging with
1120 		 * no chance to go inactive...
1121 		 */
1122 		mutex_exit(&vp->v_lock);
1123 		mutex_exit(&zp->z_lock);
1124 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1125 		return;
1126 	}
1127 	mutex_exit(&vp->v_lock);
1128 
1129 	/*
1130 	 * If this was the last reference to a file with no links,
1131 	 * remove the file from the file system.
1132 	 */
1133 	if (zp->z_unlinked) {
1134 		mutex_exit(&zp->z_lock);
1135 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1136 		zfs_rmnode(zp);
1137 		return;
1138 	}
1139 	mutex_exit(&zp->z_lock);
1140 	zfs_znode_dmu_fini(zp);
1141 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1142 	zfs_znode_free(zp);
1143 }
1144 
1145 void
1146 zfs_znode_free(znode_t *zp)
1147 {
1148 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1149 
1150 	vn_invalid(ZTOV(zp));
1151 
1152 	ASSERT(ZTOV(zp)->v_count == 0);
1153 
1154 	mutex_enter(&zfsvfs->z_znodes_lock);
1155 	POINTER_INVALIDATE(&zp->z_zfsvfs);
1156 	list_remove(&zfsvfs->z_all_znodes, zp);
1157 	mutex_exit(&zfsvfs->z_znodes_lock);
1158 
1159 	kmem_cache_free(znode_cache, zp);
1160 
1161 	VFS_RELE(zfsvfs->z_vfs);
1162 }
1163 
1164 void
1165 zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1166 {
1167 	timestruc_t	now;
1168 
1169 	ASSERT(MUTEX_HELD(&zp->z_lock));
1170 
1171 	gethrestime(&now);
1172 
1173 	if (tx) {
1174 		dmu_buf_will_dirty(zp->z_dbuf, tx);
1175 		zp->z_atime_dirty = 0;
1176 		zp->z_seq++;
1177 	} else {
1178 		zp->z_atime_dirty = 1;
1179 	}
1180 
1181 	if (flag & AT_ATIME)
1182 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
1183 
1184 	if (flag & AT_MTIME) {
1185 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
1186 		if (zp->z_zfsvfs->z_use_fuids)
1187 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
1188 	}
1189 
1190 	if (flag & AT_CTIME) {
1191 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
1192 		if (zp->z_zfsvfs->z_use_fuids)
1193 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
1194 	}
1195 }
1196 
1197 /*
1198  * Update the requested znode timestamps with the current time.
1199  * If we are in a transaction, then go ahead and mark the znode
1200  * dirty in the transaction so the timestamps will go to disk.
1201  * Otherwise, we will get pushed next time the znode is updated
1202  * in a transaction, or when this znode eventually goes inactive.
1203  *
1204  * Why is this OK?
1205  *  1 - Only the ACCESS time is ever updated outside of a transaction.
1206  *  2 - Multiple consecutive updates will be collapsed into a single
1207  *	znode update by the transaction grouping semantics of the DMU.
1208  */
1209 void
1210 zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1211 {
1212 	mutex_enter(&zp->z_lock);
1213 	zfs_time_stamper_locked(zp, flag, tx);
1214 	mutex_exit(&zp->z_lock);
1215 }
1216 
1217 /*
1218  * Grow the block size for a file.
1219  *
1220  *	IN:	zp	- znode of file to free data in.
1221  *		size	- requested block size
1222  *		tx	- open transaction.
1223  *
1224  * NOTE: this function assumes that the znode is write locked.
1225  */
1226 void
1227 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1228 {
1229 	int		error;
1230 	u_longlong_t	dummy;
1231 
1232 	if (size <= zp->z_blksz)
1233 		return;
1234 	/*
1235 	 * If the file size is already greater than the current blocksize,
1236 	 * we will not grow.  If there is more than one block in a file,
1237 	 * the blocksize cannot change.
1238 	 */
1239 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1240 		return;
1241 
1242 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1243 	    size, 0, tx);
1244 	if (error == ENOTSUP)
1245 		return;
1246 	ASSERT3U(error, ==, 0);
1247 
1248 	/* What blocksize did we actually get? */
1249 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1250 }
1251 
1252 /*
1253  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1254  * be calling back into the fs for a putpage().  E.g.: when truncating
1255  * a file, the pages being "thrown away* don't need to be written out.
1256  */
1257 /* ARGSUSED */
1258 static int
1259 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1260     int flags, cred_t *cr)
1261 {
1262 	ASSERT(0);
1263 	return (0);
1264 }
1265 
1266 /*
1267  * Increase the file length
1268  *
1269  *	IN:	zp	- znode of file to free data in.
1270  *		end	- new end-of-file
1271  *
1272  * 	RETURN:	0 if success
1273  *		error code if failure
1274  */
1275 static int
1276 zfs_extend(znode_t *zp, uint64_t end)
1277 {
1278 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1279 	dmu_tx_t *tx;
1280 	rl_t *rl;
1281 	uint64_t newblksz;
1282 	int error;
1283 
1284 	/*
1285 	 * We will change zp_size, lock the whole file.
1286 	 */
1287 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1288 
1289 	/*
1290 	 * Nothing to do if file already at desired length.
1291 	 */
1292 	if (end <= zp->z_phys->zp_size) {
1293 		zfs_range_unlock(rl);
1294 		return (0);
1295 	}
1296 top:
1297 	tx = dmu_tx_create(zfsvfs->z_os);
1298 	dmu_tx_hold_bonus(tx, zp->z_id);
1299 	if (end > zp->z_blksz &&
1300 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1301 		/*
1302 		 * We are growing the file past the current block size.
1303 		 */
1304 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1305 			ASSERT(!ISP2(zp->z_blksz));
1306 			newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1307 		} else {
1308 			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1309 		}
1310 		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1311 	} else {
1312 		newblksz = 0;
1313 	}
1314 
1315 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1316 	if (error) {
1317 		if (error == ERESTART) {
1318 			dmu_tx_wait(tx);
1319 			dmu_tx_abort(tx);
1320 			goto top;
1321 		}
1322 		dmu_tx_abort(tx);
1323 		zfs_range_unlock(rl);
1324 		return (error);
1325 	}
1326 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1327 
1328 	if (newblksz)
1329 		zfs_grow_blocksize(zp, newblksz, tx);
1330 
1331 	zp->z_phys->zp_size = end;
1332 
1333 	zfs_range_unlock(rl);
1334 
1335 	dmu_tx_commit(tx);
1336 
1337 	return (0);
1338 }
1339 
1340 /*
1341  * Free space in a file.
1342  *
1343  *	IN:	zp	- znode of file to free data in.
1344  *		off	- start of section to free.
1345  *		len	- length of section to free.
1346  *
1347  * 	RETURN:	0 if success
1348  *		error code if failure
1349  */
1350 static int
1351 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1352 {
1353 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1354 	rl_t *rl;
1355 	int error;
1356 
1357 	/*
1358 	 * Lock the range being freed.
1359 	 */
1360 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1361 
1362 	/*
1363 	 * Nothing to do if file already at desired length.
1364 	 */
1365 	if (off >= zp->z_phys->zp_size) {
1366 		zfs_range_unlock(rl);
1367 		return (0);
1368 	}
1369 
1370 	if (off + len > zp->z_phys->zp_size)
1371 		len = zp->z_phys->zp_size - off;
1372 
1373 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1374 
1375 	zfs_range_unlock(rl);
1376 
1377 	return (error);
1378 }
1379 
1380 /*
1381  * Truncate a file
1382  *
1383  *	IN:	zp	- znode of file to free data in.
1384  *		end	- new end-of-file.
1385  *
1386  * 	RETURN:	0 if success
1387  *		error code if failure
1388  */
1389 static int
1390 zfs_trunc(znode_t *zp, uint64_t end)
1391 {
1392 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1393 	vnode_t *vp = ZTOV(zp);
1394 	dmu_tx_t *tx;
1395 	rl_t *rl;
1396 	int error;
1397 
1398 	/*
1399 	 * We will change zp_size, lock the whole file.
1400 	 */
1401 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1402 
1403 	/*
1404 	 * Nothing to do if file already at desired length.
1405 	 */
1406 	if (end >= zp->z_phys->zp_size) {
1407 		zfs_range_unlock(rl);
1408 		return (0);
1409 	}
1410 
1411 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1412 	if (error) {
1413 		zfs_range_unlock(rl);
1414 		return (error);
1415 	}
1416 top:
1417 	tx = dmu_tx_create(zfsvfs->z_os);
1418 	dmu_tx_hold_bonus(tx, zp->z_id);
1419 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1420 	if (error) {
1421 		if (error == ERESTART) {
1422 			dmu_tx_wait(tx);
1423 			dmu_tx_abort(tx);
1424 			goto top;
1425 		}
1426 		dmu_tx_abort(tx);
1427 		zfs_range_unlock(rl);
1428 		return (error);
1429 	}
1430 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1431 
1432 	zp->z_phys->zp_size = end;
1433 
1434 	dmu_tx_commit(tx);
1435 
1436 	/*
1437 	 * Clear any mapped pages in the truncated region.  This has to
1438 	 * happen outside of the transaction to avoid the possibility of
1439 	 * a deadlock with someone trying to push a page that we are
1440 	 * about to invalidate.
1441 	 */
1442 	if (vn_has_cached_data(vp)) {
1443 		page_t *pp;
1444 		uint64_t start = end & PAGEMASK;
1445 		int poff = end & PAGEOFFSET;
1446 
1447 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1448 			/*
1449 			 * We need to zero a partial page.
1450 			 */
1451 			pagezero(pp, poff, PAGESIZE - poff);
1452 			start += PAGESIZE;
1453 			page_unlock(pp);
1454 		}
1455 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1456 		    B_INVAL | B_TRUNC, NULL);
1457 		ASSERT(error == 0);
1458 	}
1459 
1460 	zfs_range_unlock(rl);
1461 
1462 	return (0);
1463 }
1464 
1465 /*
1466  * Free space in a file
1467  *
1468  *	IN:	zp	- znode of file to free data in.
1469  *		off	- start of range
1470  *		len	- end of range (0 => EOF)
1471  *		flag	- current file open mode flags.
1472  *		log	- TRUE if this action should be logged
1473  *
1474  * 	RETURN:	0 if success
1475  *		error code if failure
1476  */
1477 int
1478 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1479 {
1480 	vnode_t *vp = ZTOV(zp);
1481 	dmu_tx_t *tx;
1482 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1483 	zilog_t *zilog = zfsvfs->z_log;
1484 	int error;
1485 
1486 	if (off > zp->z_phys->zp_size) {
1487 		error =  zfs_extend(zp, off+len);
1488 		if (error == 0 && log)
1489 			goto log;
1490 		else
1491 			return (error);
1492 	}
1493 
1494 	/*
1495 	 * Check for any locks in the region to be freed.
1496 	 */
1497 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
1498 		uint64_t length = (len ? len : zp->z_phys->zp_size - off);
1499 		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1500 			return (error);
1501 	}
1502 
1503 	if (len == 0) {
1504 		error = zfs_trunc(zp, off);
1505 	} else {
1506 		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1507 		    off + len > zp->z_phys->zp_size)
1508 			error = zfs_extend(zp, off+len);
1509 	}
1510 	if (error || !log)
1511 		return (error);
1512 log:
1513 	tx = dmu_tx_create(zfsvfs->z_os);
1514 	dmu_tx_hold_bonus(tx, zp->z_id);
1515 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1516 	if (error) {
1517 		if (error == ERESTART) {
1518 			dmu_tx_wait(tx);
1519 			dmu_tx_abort(tx);
1520 			goto log;
1521 		}
1522 		dmu_tx_abort(tx);
1523 		return (error);
1524 	}
1525 
1526 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1527 	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1528 
1529 	dmu_tx_commit(tx);
1530 	return (0);
1531 }
1532 
1533 void
1534 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1535 {
1536 	zfsvfs_t	zfsvfs;
1537 	uint64_t	moid, doid, version;
1538 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1539 	uint64_t	norm = 0;
1540 	nvpair_t	*elem;
1541 	int		error;
1542 	znode_t		*rootzp = NULL;
1543 	vnode_t		*vp;
1544 	vattr_t		vattr;
1545 	znode_t		*zp;
1546 
1547 	/*
1548 	 * First attempt to create master node.
1549 	 */
1550 	/*
1551 	 * In an empty objset, there are no blocks to read and thus
1552 	 * there can be no i/o errors (which we assert below).
1553 	 */
1554 	moid = MASTER_NODE_OBJ;
1555 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1556 	    DMU_OT_NONE, 0, tx);
1557 	ASSERT(error == 0);
1558 
1559 	/*
1560 	 * Set starting attributes.
1561 	 */
1562 	if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
1563 		version = ZPL_VERSION;
1564 	else
1565 		version = ZPL_VERSION_FUID - 1;
1566 	error = zap_update(os, moid, ZPL_VERSION_STR,
1567 	    8, 1, &version, tx);
1568 	elem = NULL;
1569 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1570 		/* For the moment we expect all zpl props to be uint64_ts */
1571 		uint64_t val;
1572 		char *name;
1573 
1574 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1575 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1576 		name = nvpair_name(elem);
1577 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1578 			version = val;
1579 			error = zap_update(os, moid, ZPL_VERSION_STR,
1580 			    8, 1, &version, tx);
1581 		} else {
1582 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1583 		}
1584 		ASSERT(error == 0);
1585 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1586 			norm = val;
1587 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1588 			sense = val;
1589 	}
1590 	ASSERT(version != 0);
1591 
1592 	/*
1593 	 * Create a delete queue.
1594 	 */
1595 	doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1596 
1597 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx);
1598 	ASSERT(error == 0);
1599 
1600 	/*
1601 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1602 	 * to allow zfs_mknode to work.
1603 	 */
1604 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1605 	vattr.va_type = VDIR;
1606 	vattr.va_mode = S_IFDIR|0755;
1607 	vattr.va_uid = crgetuid(cr);
1608 	vattr.va_gid = crgetgid(cr);
1609 
1610 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1611 	rootzp->z_unlinked = 0;
1612 	rootzp->z_atime_dirty = 0;
1613 
1614 	vp = ZTOV(rootzp);
1615 	vn_reinit(vp);
1616 	vp->v_type = VDIR;
1617 
1618 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1619 
1620 	zfsvfs.z_os = os;
1621 	zfsvfs.z_parent = &zfsvfs;
1622 	zfsvfs.z_version = version;
1623 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1624 	zfsvfs.z_norm = norm;
1625 	/*
1626 	 * Fold case on file systems that are always or sometimes case
1627 	 * insensitive.
1628 	 */
1629 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1630 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1631 
1632 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1633 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1634 	    offsetof(znode_t, z_link_node));
1635 
1636 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1637 	rootzp->z_zfsvfs = &zfsvfs;
1638 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, NULL, NULL);
1639 	ASSERT3P(zp, ==, rootzp);
1640 	ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
1641 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1642 	ASSERT(error == 0);
1643 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1644 
1645 	ZTOV(rootzp)->v_count = 0;
1646 	dmu_buf_rele(rootzp->z_dbuf, NULL);
1647 	rootzp->z_dbuf = NULL;
1648 	kmem_cache_free(znode_cache, rootzp);
1649 
1650 	/*
1651 	 * Create shares directory
1652 	 */
1653 
1654 	error = zfs_create_share_dir(&zfsvfs, tx);
1655 	ASSERT(error == 0);
1656 }
1657 
1658 #endif /* _KERNEL */
1659 /*
1660  * Given an object number, return its parent object number and whether
1661  * or not the object is an extended attribute directory.
1662  */
1663 static int
1664 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1665 {
1666 	dmu_buf_t *db;
1667 	dmu_object_info_t doi;
1668 	znode_phys_t *zp;
1669 	int error;
1670 
1671 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1672 		return (error);
1673 
1674 	dmu_object_info_from_db(db, &doi);
1675 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1676 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1677 		dmu_buf_rele(db, FTAG);
1678 		return (EINVAL);
1679 	}
1680 
1681 	zp = db->db_data;
1682 	*pobjp = zp->zp_parent;
1683 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1684 	    S_ISDIR(zp->zp_mode);
1685 	dmu_buf_rele(db, FTAG);
1686 
1687 	return (0);
1688 }
1689 
1690 int
1691 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1692 {
1693 	char *path = buf + len - 1;
1694 	int error;
1695 
1696 	*path = '\0';
1697 
1698 	for (;;) {
1699 		uint64_t pobj;
1700 		char component[MAXNAMELEN + 2];
1701 		size_t complen;
1702 		int is_xattrdir;
1703 
1704 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1705 		    &is_xattrdir)) != 0)
1706 			break;
1707 
1708 		if (pobj == obj) {
1709 			if (path[0] != '/')
1710 				*--path = '/';
1711 			break;
1712 		}
1713 
1714 		component[0] = '/';
1715 		if (is_xattrdir) {
1716 			(void) sprintf(component + 1, "<xattrdir>");
1717 		} else {
1718 			error = zap_value_search(osp, pobj, obj,
1719 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
1720 			if (error != 0)
1721 				break;
1722 		}
1723 
1724 		complen = strlen(component);
1725 		path -= complen;
1726 		ASSERT(path >= buf);
1727 		bcopy(component, path, complen);
1728 		obj = pobj;
1729 	}
1730 
1731 	if (error == 0)
1732 		(void) memmove(buf, path, buf + len - path);
1733 	return (error);
1734 }
1735