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