xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_znode.c (revision d47621a49c68c359358f6630aa45cc320762f51f)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22ac05c741SMark Maybee  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
2675c76197Speteh /* Portions Copyright 2007 Jeremy Teo */
2775c76197Speteh 
2855434c77Sek #ifdef _KERNEL
29fa9e4066Sahrens #include <sys/types.h>
30fa9e4066Sahrens #include <sys/param.h>
31fa9e4066Sahrens #include <sys/time.h>
32fa9e4066Sahrens #include <sys/systm.h>
33fa9e4066Sahrens #include <sys/sysmacros.h>
34fa9e4066Sahrens #include <sys/resource.h>
35fa9e4066Sahrens #include <sys/mntent.h>
3672fc53bcSmarks #include <sys/mkdev.h>
37de8267e0Stimh #include <sys/u8_textprep.h>
38ab04eb8eStimh #include <sys/dsl_dataset.h>
39fa9e4066Sahrens #include <sys/vfs.h>
40aa59c4cbSrsb #include <sys/vfs_opreg.h>
41fa9e4066Sahrens #include <sys/vnode.h>
42fa9e4066Sahrens #include <sys/file.h>
43fa9e4066Sahrens #include <sys/kmem.h>
44fa9e4066Sahrens #include <sys/errno.h>
45fa9e4066Sahrens #include <sys/unistd.h>
46fa9e4066Sahrens #include <sys/mode.h>
47fa9e4066Sahrens #include <sys/atomic.h>
48fa9e4066Sahrens #include <vm/pvn.h>
49fa9e4066Sahrens #include "fs/fs_subr.h"
50fa9e4066Sahrens #include <sys/zfs_dir.h>
51fa9e4066Sahrens #include <sys/zfs_acl.h>
52fa9e4066Sahrens #include <sys/zfs_ioctl.h>
53104e2ed7Sperrin #include <sys/zfs_rlock.h>
54da6c28aaSamw #include <sys/zfs_fuid.h>
55fa9e4066Sahrens #include <sys/fs/zfs.h>
56da6c28aaSamw #include <sys/kidmap.h>
5755434c77Sek #endif /* _KERNEL */
5855434c77Sek 
5955434c77Sek #include <sys/dmu.h>
6055434c77Sek #include <sys/refcount.h>
6155434c77Sek #include <sys/stat.h>
6255434c77Sek #include <sys/zap.h>
6355434c77Sek #include <sys/zfs_znode.h>
64fa9e4066Sahrens 
65de8267e0Stimh #include "zfs_prop.h"
66de8267e0Stimh 
67b5fca8f8Stomee /*
68b5fca8f8Stomee  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
69b5fca8f8Stomee  * turned on when DEBUG is also defined.
70b5fca8f8Stomee  */
71b5fca8f8Stomee #ifdef	DEBUG
72b5fca8f8Stomee #define	ZNODE_STATS
73b5fca8f8Stomee #endif	/* DEBUG */
74b5fca8f8Stomee 
75b5fca8f8Stomee #ifdef	ZNODE_STATS
76b5fca8f8Stomee #define	ZNODE_STAT_ADD(stat)			((stat)++)
77b5fca8f8Stomee #else
78b5fca8f8Stomee #define	ZNODE_STAT_ADD(stat)			/* nothing */
79b5fca8f8Stomee #endif	/* ZNODE_STATS */
80b5fca8f8Stomee 
81b5fca8f8Stomee #define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
82b5fca8f8Stomee #define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
83b5fca8f8Stomee 
8455434c77Sek /*
8555434c77Sek  * Functions needed for userland (ie: libzpool) are not put under
8655434c77Sek  * #ifdef_KERNEL; the rest of the functions have dependencies
8755434c77Sek  * (such as VFS logic) that will not compile easily in userland.
8855434c77Sek  */
8955434c77Sek #ifdef _KERNEL
904e9583b2STom Erickson /*
914e9583b2STom Erickson  * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
924e9583b2STom Erickson  * be freed before it can be safely accessed.
934e9583b2STom Erickson  */
944e9583b2STom Erickson krwlock_t zfsvfs_lock;
954e9583b2STom Erickson 
96b5fca8f8Stomee static kmem_cache_t *znode_cache = NULL;
97fa9e4066Sahrens 
98fa9e4066Sahrens /*ARGSUSED*/
99fa9e4066Sahrens static void
100874395d5Smaybee znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
101fa9e4066Sahrens {
102874395d5Smaybee 	/*
103874395d5Smaybee 	 * We should never drop all dbuf refs without first clearing
104874395d5Smaybee 	 * the eviction callback.
105874395d5Smaybee 	 */
106874395d5Smaybee 	panic("evicting znode %p\n", user_ptr);
107fa9e4066Sahrens }
108fa9e4066Sahrens 
109fa9e4066Sahrens /*ARGSUSED*/
110fa9e4066Sahrens static int
111b5fca8f8Stomee zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	znode_t *zp = buf;
114fa9e4066Sahrens 
115b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
116b5fca8f8Stomee 
117b5fca8f8Stomee 	zp->z_vnode = vn_alloc(kmflags);
118b5fca8f8Stomee 	if (zp->z_vnode == NULL) {
119b5fca8f8Stomee 		return (-1);
120b5fca8f8Stomee 	}
121b5fca8f8Stomee 	ZTOV(zp)->v_data = zp;
122b5fca8f8Stomee 
123b5fca8f8Stomee 	list_link_init(&zp->z_link_node);
124b5fca8f8Stomee 
125fa9e4066Sahrens 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
126104e2ed7Sperrin 	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
127af2c4821Smaybee 	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
128fa9e4066Sahrens 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
129104e2ed7Sperrin 
130104e2ed7Sperrin 	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
131104e2ed7Sperrin 	avl_create(&zp->z_range_avl, zfs_range_compare,
132104e2ed7Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
133104e2ed7Sperrin 
1344ccbb6e7Sahrens 	zp->z_dbuf = NULL;
135b5fca8f8Stomee 	zp->z_dirlocks = NULL;
136*d47621a4STim Haley 	zp->z_acl_cached = NULL;
137fa9e4066Sahrens 	return (0);
138fa9e4066Sahrens }
139fa9e4066Sahrens 
140fa9e4066Sahrens /*ARGSUSED*/
141fa9e4066Sahrens static void
142b5fca8f8Stomee zfs_znode_cache_destructor(void *buf, void *arg)
143fa9e4066Sahrens {
144fa9e4066Sahrens 	znode_t *zp = buf;
145fa9e4066Sahrens 
146b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
147b5fca8f8Stomee 	ASSERT(ZTOV(zp)->v_data == zp);
148b5fca8f8Stomee 	vn_free(ZTOV(zp));
149b5fca8f8Stomee 	ASSERT(!list_link_active(&zp->z_link_node));
150fa9e4066Sahrens 	mutex_destroy(&zp->z_lock);
151104e2ed7Sperrin 	rw_destroy(&zp->z_parent_lock);
152af2c4821Smaybee 	rw_destroy(&zp->z_name_lock);
153fa9e4066Sahrens 	mutex_destroy(&zp->z_acl_lock);
154104e2ed7Sperrin 	avl_destroy(&zp->z_range_avl);
155c25056deSgw 	mutex_destroy(&zp->z_range_lock);
156fa9e4066Sahrens 
1574ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
158b5fca8f8Stomee 	ASSERT(zp->z_dirlocks == NULL);
159b5fca8f8Stomee }
160b5fca8f8Stomee 
161b5fca8f8Stomee #ifdef	ZNODE_STATS
162b5fca8f8Stomee static struct {
163b5fca8f8Stomee 	uint64_t zms_zfsvfs_invalid;
1644e9583b2STom Erickson 	uint64_t zms_zfsvfs_recheck1;
165b5fca8f8Stomee 	uint64_t zms_zfsvfs_unmounted;
1664e9583b2STom Erickson 	uint64_t zms_zfsvfs_recheck2;
167a66b2b35STom Erickson 	uint64_t zms_obj_held;
168b5fca8f8Stomee 	uint64_t zms_vnode_locked;
169a66b2b35STom Erickson 	uint64_t zms_not_only_dnlc;
170b5fca8f8Stomee } znode_move_stats;
171b5fca8f8Stomee #endif	/* ZNODE_STATS */
172b5fca8f8Stomee 
173b5fca8f8Stomee static void
174b5fca8f8Stomee zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
175b5fca8f8Stomee {
176b5fca8f8Stomee 	vnode_t *vp;
177b5fca8f8Stomee 
178b5fca8f8Stomee 	/* Copy fields. */
179b5fca8f8Stomee 	nzp->z_zfsvfs = ozp->z_zfsvfs;
180b5fca8f8Stomee 
181b5fca8f8Stomee 	/* Swap vnodes. */
182b5fca8f8Stomee 	vp = nzp->z_vnode;
183b5fca8f8Stomee 	nzp->z_vnode = ozp->z_vnode;
184b5fca8f8Stomee 	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
185b5fca8f8Stomee 	ZTOV(ozp)->v_data = ozp;
186b5fca8f8Stomee 	ZTOV(nzp)->v_data = nzp;
187b5fca8f8Stomee 
188b5fca8f8Stomee 	nzp->z_id = ozp->z_id;
189b5fca8f8Stomee 	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
190b5fca8f8Stomee 	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
191b5fca8f8Stomee 	nzp->z_unlinked = ozp->z_unlinked;
192b5fca8f8Stomee 	nzp->z_atime_dirty = ozp->z_atime_dirty;
193b5fca8f8Stomee 	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
194b5fca8f8Stomee 	nzp->z_blksz = ozp->z_blksz;
195b5fca8f8Stomee 	nzp->z_seq = ozp->z_seq;
196b5fca8f8Stomee 	nzp->z_mapcnt = ozp->z_mapcnt;
197b5fca8f8Stomee 	nzp->z_last_itx = ozp->z_last_itx;
198b5fca8f8Stomee 	nzp->z_gen = ozp->z_gen;
199b5fca8f8Stomee 	nzp->z_sync_cnt = ozp->z_sync_cnt;
200b5fca8f8Stomee 	nzp->z_phys = ozp->z_phys;
201b5fca8f8Stomee 	nzp->z_dbuf = ozp->z_dbuf;
202b5fca8f8Stomee 
203b5fca8f8Stomee 	/* Update back pointers. */
204b5fca8f8Stomee 	(void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys,
205b5fca8f8Stomee 	    znode_evict_error);
206b5fca8f8Stomee 
207b5fca8f8Stomee 	/*
208b5fca8f8Stomee 	 * Invalidate the original znode by clearing fields that provide a
209b5fca8f8Stomee 	 * pointer back to the znode. Set the low bit of the vfs pointer to
210b5fca8f8Stomee 	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
211b5fca8f8Stomee 	 * subsequent callback.
212b5fca8f8Stomee 	 */
213b5fca8f8Stomee 	ozp->z_dbuf = NULL;
214b5fca8f8Stomee 	POINTER_INVALIDATE(&ozp->z_zfsvfs);
215b5fca8f8Stomee }
216b5fca8f8Stomee 
217b5fca8f8Stomee /*ARGSUSED*/
218b5fca8f8Stomee static kmem_cbrc_t
219b5fca8f8Stomee zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
220b5fca8f8Stomee {
221b5fca8f8Stomee 	znode_t *ozp = buf, *nzp = newbuf;
222b5fca8f8Stomee 	zfsvfs_t *zfsvfs;
223b5fca8f8Stomee 	vnode_t *vp;
224b5fca8f8Stomee 
225b5fca8f8Stomee 	/*
226b5fca8f8Stomee 	 * The znode is on the file system's list of known znodes if the vfs
227b5fca8f8Stomee 	 * pointer is valid. We set the low bit of the vfs pointer when freeing
228b5fca8f8Stomee 	 * the znode to invalidate it, and the memory patterns written by kmem
229b5fca8f8Stomee 	 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
230b5fca8f8Stomee 	 * created znode sets the vfs pointer last of all to indicate that the
231b5fca8f8Stomee 	 * znode is known and in a valid state to be moved by this function.
232b5fca8f8Stomee 	 */
233b5fca8f8Stomee 	zfsvfs = ozp->z_zfsvfs;
234b5fca8f8Stomee 	if (!POINTER_IS_VALID(zfsvfs)) {
235b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
236b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
237b5fca8f8Stomee 	}
238b5fca8f8Stomee 
239b5fca8f8Stomee 	/*
2404e9583b2STom Erickson 	 * Close a small window in which it's possible that the filesystem could
2414e9583b2STom Erickson 	 * be unmounted and freed, and zfsvfs, though valid in the previous
2424e9583b2STom Erickson 	 * statement, could point to unrelated memory by the time we try to
2434e9583b2STom Erickson 	 * prevent the filesystem from being unmounted.
2444e9583b2STom Erickson 	 */
2454e9583b2STom Erickson 	rw_enter(&zfsvfs_lock, RW_WRITER);
2464e9583b2STom Erickson 	if (zfsvfs != ozp->z_zfsvfs) {
2474e9583b2STom Erickson 		rw_exit(&zfsvfs_lock);
2484e9583b2STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
2494e9583b2STom Erickson 		return (KMEM_CBRC_DONT_KNOW);
2504e9583b2STom Erickson 	}
2514e9583b2STom Erickson 
2524e9583b2STom Erickson 	/*
2534e9583b2STom Erickson 	 * If the znode is still valid, then so is the file system. We know that
2544e9583b2STom Erickson 	 * no valid file system can be freed while we hold zfsvfs_lock, so we
2554e9583b2STom Erickson 	 * can safely ensure that the filesystem is not and will not be
2564e9583b2STom Erickson 	 * unmounted. The next statement is equivalent to ZFS_ENTER().
257b5fca8f8Stomee 	 */
25814843421SMatthew Ahrens 	rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
25914843421SMatthew Ahrens 	if (zfsvfs->z_unmounted) {
26014843421SMatthew Ahrens 		ZFS_EXIT(zfsvfs);
2614e9583b2STom Erickson 		rw_exit(&zfsvfs_lock);
262b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
263b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
264b5fca8f8Stomee 	}
2654e9583b2STom Erickson 	rw_exit(&zfsvfs_lock);
266b5fca8f8Stomee 
267b5fca8f8Stomee 	mutex_enter(&zfsvfs->z_znodes_lock);
268b5fca8f8Stomee 	/*
269b5fca8f8Stomee 	 * Recheck the vfs pointer in case the znode was removed just before
270b5fca8f8Stomee 	 * acquiring the lock.
271b5fca8f8Stomee 	 */
272b5fca8f8Stomee 	if (zfsvfs != ozp->z_zfsvfs) {
273b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
274b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
2754e9583b2STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
276b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
277b5fca8f8Stomee 	}
278b5fca8f8Stomee 
279b5fca8f8Stomee 	/*
280b5fca8f8Stomee 	 * At this point we know that as long as we hold z_znodes_lock, the
281b5fca8f8Stomee 	 * znode cannot be freed and fields within the znode can be safely
282a66b2b35STom Erickson 	 * accessed. Now, prevent a race with zfs_zget().
283b5fca8f8Stomee 	 */
284a66b2b35STom Erickson 	if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
285a66b2b35STom Erickson 		mutex_exit(&zfsvfs->z_znodes_lock);
286a66b2b35STom Erickson 		ZFS_EXIT(zfsvfs);
287a66b2b35STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
288a66b2b35STom Erickson 		return (KMEM_CBRC_LATER);
289a66b2b35STom Erickson 	}
290a66b2b35STom Erickson 
291b5fca8f8Stomee 	vp = ZTOV(ozp);
292b5fca8f8Stomee 	if (mutex_tryenter(&vp->v_lock) == 0) {
293a66b2b35STom Erickson 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
294b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
295b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
296b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
297b5fca8f8Stomee 		return (KMEM_CBRC_LATER);
298b5fca8f8Stomee 	}
299a66b2b35STom Erickson 
300b5fca8f8Stomee 	/* Only move znodes that are referenced _only_ by the DNLC. */
301b5fca8f8Stomee 	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
302b5fca8f8Stomee 		mutex_exit(&vp->v_lock);
303a66b2b35STom Erickson 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
304b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
305b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
306a66b2b35STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
307b5fca8f8Stomee 		return (KMEM_CBRC_LATER);
308b5fca8f8Stomee 	}
309b5fca8f8Stomee 
310b5fca8f8Stomee 	/*
311b5fca8f8Stomee 	 * The znode is known and in a valid state to move. We're holding the
312b5fca8f8Stomee 	 * locks needed to execute the critical section.
313b5fca8f8Stomee 	 */
314b5fca8f8Stomee 	zfs_znode_move_impl(ozp, nzp);
315b5fca8f8Stomee 	mutex_exit(&vp->v_lock);
316a66b2b35STom Erickson 	ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
317b5fca8f8Stomee 
318b5fca8f8Stomee 	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
319b5fca8f8Stomee 	mutex_exit(&zfsvfs->z_znodes_lock);
320b5fca8f8Stomee 	ZFS_EXIT(zfsvfs);
321b5fca8f8Stomee 
322b5fca8f8Stomee 	return (KMEM_CBRC_YES);
323fa9e4066Sahrens }
324fa9e4066Sahrens 
325fa9e4066Sahrens void
326fa9e4066Sahrens zfs_znode_init(void)
327fa9e4066Sahrens {
328fa9e4066Sahrens 	/*
329fa9e4066Sahrens 	 * Initialize zcache
330fa9e4066Sahrens 	 */
3314e9583b2STom Erickson 	rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
332fa9e4066Sahrens 	ASSERT(znode_cache == NULL);
333fa9e4066Sahrens 	znode_cache = kmem_cache_create("zfs_znode_cache",
334fa9e4066Sahrens 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
335fa9e4066Sahrens 	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
336b5fca8f8Stomee 	kmem_cache_set_move(znode_cache, zfs_znode_move);
337fa9e4066Sahrens }
338fa9e4066Sahrens 
339fa9e4066Sahrens void
340fa9e4066Sahrens zfs_znode_fini(void)
341fa9e4066Sahrens {
342fa9e4066Sahrens 	/*
343fa9e4066Sahrens 	 * Cleanup vfs & vnode ops
344fa9e4066Sahrens 	 */
345fa9e4066Sahrens 	zfs_remove_op_tables();
346fa9e4066Sahrens 
347fa9e4066Sahrens 	/*
348fa9e4066Sahrens 	 * Cleanup zcache
349fa9e4066Sahrens 	 */
350fa9e4066Sahrens 	if (znode_cache)
351fa9e4066Sahrens 		kmem_cache_destroy(znode_cache);
352fa9e4066Sahrens 	znode_cache = NULL;
3534e9583b2STom Erickson 	rw_destroy(&zfsvfs_lock);
354fa9e4066Sahrens }
355fa9e4066Sahrens 
356fa9e4066Sahrens struct vnodeops *zfs_dvnodeops;
357fa9e4066Sahrens struct vnodeops *zfs_fvnodeops;
358fa9e4066Sahrens struct vnodeops *zfs_symvnodeops;
359fa9e4066Sahrens struct vnodeops *zfs_xdvnodeops;
360fa9e4066Sahrens struct vnodeops *zfs_evnodeops;
361743a77edSAlan Wright struct vnodeops *zfs_sharevnodeops;
362fa9e4066Sahrens 
363fa9e4066Sahrens void
364fa9e4066Sahrens zfs_remove_op_tables()
365fa9e4066Sahrens {
366fa9e4066Sahrens 	/*
367fa9e4066Sahrens 	 * Remove vfs ops
368fa9e4066Sahrens 	 */
369fa9e4066Sahrens 	ASSERT(zfsfstype);
370fa9e4066Sahrens 	(void) vfs_freevfsops_by_type(zfsfstype);
371fa9e4066Sahrens 	zfsfstype = 0;
372fa9e4066Sahrens 
373fa9e4066Sahrens 	/*
374fa9e4066Sahrens 	 * Remove vnode ops
375fa9e4066Sahrens 	 */
376fa9e4066Sahrens 	if (zfs_dvnodeops)
377fa9e4066Sahrens 		vn_freevnodeops(zfs_dvnodeops);
378fa9e4066Sahrens 	if (zfs_fvnodeops)
379fa9e4066Sahrens 		vn_freevnodeops(zfs_fvnodeops);
380fa9e4066Sahrens 	if (zfs_symvnodeops)
381fa9e4066Sahrens 		vn_freevnodeops(zfs_symvnodeops);
382fa9e4066Sahrens 	if (zfs_xdvnodeops)
383fa9e4066Sahrens 		vn_freevnodeops(zfs_xdvnodeops);
384fa9e4066Sahrens 	if (zfs_evnodeops)
385fa9e4066Sahrens 		vn_freevnodeops(zfs_evnodeops);
386743a77edSAlan Wright 	if (zfs_sharevnodeops)
387743a77edSAlan Wright 		vn_freevnodeops(zfs_sharevnodeops);
388fa9e4066Sahrens 
389fa9e4066Sahrens 	zfs_dvnodeops = NULL;
390fa9e4066Sahrens 	zfs_fvnodeops = NULL;
391fa9e4066Sahrens 	zfs_symvnodeops = NULL;
392fa9e4066Sahrens 	zfs_xdvnodeops = NULL;
393fa9e4066Sahrens 	zfs_evnodeops = NULL;
394743a77edSAlan Wright 	zfs_sharevnodeops = NULL;
395fa9e4066Sahrens }
396fa9e4066Sahrens 
397fa9e4066Sahrens extern const fs_operation_def_t zfs_dvnodeops_template[];
398fa9e4066Sahrens extern const fs_operation_def_t zfs_fvnodeops_template[];
399fa9e4066Sahrens extern const fs_operation_def_t zfs_xdvnodeops_template[];
400fa9e4066Sahrens extern const fs_operation_def_t zfs_symvnodeops_template[];
401fa9e4066Sahrens extern const fs_operation_def_t zfs_evnodeops_template[];
402743a77edSAlan Wright extern const fs_operation_def_t zfs_sharevnodeops_template[];
403fa9e4066Sahrens 
404fa9e4066Sahrens int
405fa9e4066Sahrens zfs_create_op_tables()
406fa9e4066Sahrens {
407fa9e4066Sahrens 	int error;
408fa9e4066Sahrens 
409fa9e4066Sahrens 	/*
410fa9e4066Sahrens 	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
411fa9e4066Sahrens 	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
412fa9e4066Sahrens 	 * In this case we just return as the ops vectors are already set up.
413fa9e4066Sahrens 	 */
414fa9e4066Sahrens 	if (zfs_dvnodeops)
415fa9e4066Sahrens 		return (0);
416fa9e4066Sahrens 
417fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
418fa9e4066Sahrens 	    &zfs_dvnodeops);
419fa9e4066Sahrens 	if (error)
420fa9e4066Sahrens 		return (error);
421fa9e4066Sahrens 
422fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
423fa9e4066Sahrens 	    &zfs_fvnodeops);
424fa9e4066Sahrens 	if (error)
425fa9e4066Sahrens 		return (error);
426fa9e4066Sahrens 
427fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
428fa9e4066Sahrens 	    &zfs_symvnodeops);
429fa9e4066Sahrens 	if (error)
430fa9e4066Sahrens 		return (error);
431fa9e4066Sahrens 
432fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
433fa9e4066Sahrens 	    &zfs_xdvnodeops);
434fa9e4066Sahrens 	if (error)
435fa9e4066Sahrens 		return (error);
436fa9e4066Sahrens 
437fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
438fa9e4066Sahrens 	    &zfs_evnodeops);
439743a77edSAlan Wright 	if (error)
440743a77edSAlan Wright 		return (error);
441743a77edSAlan Wright 
442743a77edSAlan Wright 	error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
443743a77edSAlan Wright 	    &zfs_sharevnodeops);
444743a77edSAlan Wright 
445743a77edSAlan Wright 	return (error);
446743a77edSAlan Wright }
447743a77edSAlan Wright 
4489e1320c0SMark Shellenbaum int
449743a77edSAlan Wright zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
450743a77edSAlan Wright {
45189459e17SMark Shellenbaum 	zfs_acl_ids_t acl_ids;
452743a77edSAlan Wright 	vattr_t vattr;
453743a77edSAlan Wright 	znode_t *sharezp;
454743a77edSAlan Wright 	vnode_t *vp;
455743a77edSAlan Wright 	znode_t *zp;
456743a77edSAlan Wright 	int error;
457743a77edSAlan Wright 
458743a77edSAlan Wright 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
459743a77edSAlan Wright 	vattr.va_type = VDIR;
460743a77edSAlan Wright 	vattr.va_mode = S_IFDIR|0555;
461743a77edSAlan Wright 	vattr.va_uid = crgetuid(kcred);
462743a77edSAlan Wright 	vattr.va_gid = crgetgid(kcred);
463743a77edSAlan Wright 
464743a77edSAlan Wright 	sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
465743a77edSAlan Wright 	sharezp->z_unlinked = 0;
466743a77edSAlan Wright 	sharezp->z_atime_dirty = 0;
467743a77edSAlan Wright 	sharezp->z_zfsvfs = zfsvfs;
468743a77edSAlan Wright 
469743a77edSAlan Wright 	vp = ZTOV(sharezp);
470743a77edSAlan Wright 	vn_reinit(vp);
471743a77edSAlan Wright 	vp->v_type = VDIR;
472743a77edSAlan Wright 
47389459e17SMark Shellenbaum 	VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
47489459e17SMark Shellenbaum 	    kcred, NULL, &acl_ids));
475743a77edSAlan Wright 	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE,
47689459e17SMark Shellenbaum 	    &zp, 0, &acl_ids);
477743a77edSAlan Wright 	ASSERT3P(zp, ==, sharezp);
478743a77edSAlan Wright 	ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
479743a77edSAlan Wright 	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
480743a77edSAlan Wright 	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
481743a77edSAlan Wright 	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
482743a77edSAlan Wright 	zfsvfs->z_shares_dir = sharezp->z_id;
483743a77edSAlan Wright 
48489459e17SMark Shellenbaum 	zfs_acl_ids_free(&acl_ids);
485743a77edSAlan Wright 	ZTOV(sharezp)->v_count = 0;
486743a77edSAlan Wright 	dmu_buf_rele(sharezp->z_dbuf, NULL);
487743a77edSAlan Wright 	sharezp->z_dbuf = NULL;
488743a77edSAlan Wright 	kmem_cache_free(znode_cache, sharezp);
489fa9e4066Sahrens 
490fa9e4066Sahrens 	return (error);
491fa9e4066Sahrens }
492fa9e4066Sahrens 
49372fc53bcSmarks /*
49472fc53bcSmarks  * define a couple of values we need available
49572fc53bcSmarks  * for both 64 and 32 bit environments.
49672fc53bcSmarks  */
49772fc53bcSmarks #ifndef NBITSMINOR64
49872fc53bcSmarks #define	NBITSMINOR64	32
49972fc53bcSmarks #endif
50072fc53bcSmarks #ifndef MAXMAJ64
50172fc53bcSmarks #define	MAXMAJ64	0xffffffffUL
50272fc53bcSmarks #endif
50372fc53bcSmarks #ifndef	MAXMIN64
50472fc53bcSmarks #define	MAXMIN64	0xffffffffUL
50572fc53bcSmarks #endif
50672fc53bcSmarks 
50772fc53bcSmarks /*
50872fc53bcSmarks  * Create special expldev for ZFS private use.
50972fc53bcSmarks  * Can't use standard expldev since it doesn't do
51072fc53bcSmarks  * what we want.  The standard expldev() takes a
51172fc53bcSmarks  * dev32_t in LP64 and expands it to a long dev_t.
51272fc53bcSmarks  * We need an interface that takes a dev32_t in ILP32
51372fc53bcSmarks  * and expands it to a long dev_t.
51472fc53bcSmarks  */
51572fc53bcSmarks static uint64_t
51672fc53bcSmarks zfs_expldev(dev_t dev)
51772fc53bcSmarks {
51872fc53bcSmarks #ifndef _LP64
51972fc53bcSmarks 	major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
52072fc53bcSmarks 	return (((uint64_t)major << NBITSMINOR64) |
52172fc53bcSmarks 	    ((minor_t)dev & MAXMIN32));
52272fc53bcSmarks #else
52372fc53bcSmarks 	return (dev);
52472fc53bcSmarks #endif
52572fc53bcSmarks }
52672fc53bcSmarks 
52772fc53bcSmarks /*
52872fc53bcSmarks  * Special cmpldev for ZFS private use.
52972fc53bcSmarks  * Can't use standard cmpldev since it takes
53072fc53bcSmarks  * a long dev_t and compresses it to dev32_t in
53172fc53bcSmarks  * LP64.  We need to do a compaction of a long dev_t
53272fc53bcSmarks  * to a dev32_t in ILP32.
53372fc53bcSmarks  */
53472fc53bcSmarks dev_t
53572fc53bcSmarks zfs_cmpldev(uint64_t dev)
53672fc53bcSmarks {
53772fc53bcSmarks #ifndef _LP64
53872fc53bcSmarks 	minor_t minor = (minor_t)dev & MAXMIN64;
53972fc53bcSmarks 	major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
54072fc53bcSmarks 
54172fc53bcSmarks 	if (major > MAXMAJ32 || minor > MAXMIN32)
54272fc53bcSmarks 		return (NODEV32);
54372fc53bcSmarks 
54472fc53bcSmarks 	return (((dev32_t)major << NBITSMINOR32) | minor);
54572fc53bcSmarks #else
54672fc53bcSmarks 	return (dev);
54772fc53bcSmarks #endif
54872fc53bcSmarks }
54972fc53bcSmarks 
5504ccbb6e7Sahrens static void
551b5fca8f8Stomee zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
5524ccbb6e7Sahrens {
5534ccbb6e7Sahrens 	znode_t		*nzp;
5544ccbb6e7Sahrens 
555b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
556b5fca8f8Stomee 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
5574ccbb6e7Sahrens 
5584ccbb6e7Sahrens 	mutex_enter(&zp->z_lock);
5594ccbb6e7Sahrens 
5604ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
5614ccbb6e7Sahrens 	zp->z_dbuf = db;
562874395d5Smaybee 	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error);
5634ccbb6e7Sahrens 
5644ccbb6e7Sahrens 	/*
5654ccbb6e7Sahrens 	 * there should be no
5664ccbb6e7Sahrens 	 * concurrent zgets on this object.
5674ccbb6e7Sahrens 	 */
5684ccbb6e7Sahrens 	if (nzp != NULL)
569903a11ebSrh 		panic("existing znode %p for dbuf %p", (void *)nzp, (void *)db);
5704ccbb6e7Sahrens 
5714ccbb6e7Sahrens 	/*
5724ccbb6e7Sahrens 	 * Slap on VROOT if we are the root znode
5734ccbb6e7Sahrens 	 */
5744ccbb6e7Sahrens 	if (zp->z_id == zfsvfs->z_root)
5754ccbb6e7Sahrens 		ZTOV(zp)->v_flag |= VROOT;
5764ccbb6e7Sahrens 
5774ccbb6e7Sahrens 	mutex_exit(&zp->z_lock);
5784ccbb6e7Sahrens 	vn_exists(ZTOV(zp));
5794ccbb6e7Sahrens }
5804ccbb6e7Sahrens 
581874395d5Smaybee void
5824ccbb6e7Sahrens zfs_znode_dmu_fini(znode_t *zp)
5834ccbb6e7Sahrens {
5844ccbb6e7Sahrens 	dmu_buf_t *db = zp->z_dbuf;
585b5fca8f8Stomee 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
586b5fca8f8Stomee 	    zp->z_unlinked ||
587874395d5Smaybee 	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
5884ccbb6e7Sahrens 	ASSERT(zp->z_dbuf != NULL);
5894ccbb6e7Sahrens 	zp->z_dbuf = NULL;
590874395d5Smaybee 	VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL));
5914ccbb6e7Sahrens 	dmu_buf_rele(db, NULL);
5924ccbb6e7Sahrens }
5934ccbb6e7Sahrens 
594fa9e4066Sahrens /*
595fa9e4066Sahrens  * Construct a new znode/vnode and intialize.
596fa9e4066Sahrens  *
597fa9e4066Sahrens  * This does not do a call to dmu_set_user() that is
598fa9e4066Sahrens  * up to the caller to do, in case you don't want to
599fa9e4066Sahrens  * return the znode
600fa9e4066Sahrens  */
601ea8dc4b6Seschrock static znode_t *
6024ccbb6e7Sahrens zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
603fa9e4066Sahrens {
604fa9e4066Sahrens 	znode_t	*zp;
605fa9e4066Sahrens 	vnode_t *vp;
606fa9e4066Sahrens 
607fa9e4066Sahrens 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
608fa9e4066Sahrens 
609fa9e4066Sahrens 	ASSERT(zp->z_dirlocks == NULL);
6104ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
611b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
612fa9e4066Sahrens 
613b5fca8f8Stomee 	/*
614b5fca8f8Stomee 	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
615b5fca8f8Stomee 	 * the zfs_znode_move() callback.
616b5fca8f8Stomee 	 */
6174ccbb6e7Sahrens 	zp->z_phys = NULL;
618893a6d32Sahrens 	zp->z_unlinked = 0;
619fa9e4066Sahrens 	zp->z_atime_dirty = 0;
620fa9e4066Sahrens 	zp->z_mapcnt = 0;
621fa9e4066Sahrens 	zp->z_last_itx = 0;
6224ccbb6e7Sahrens 	zp->z_id = db->db_object;
623fa9e4066Sahrens 	zp->z_blksz = blksz;
624fa9e4066Sahrens 	zp->z_seq = 0x7A4653;
62567bd71c6Sperrin 	zp->z_sync_cnt = 0;
6264ccbb6e7Sahrens 
6274ccbb6e7Sahrens 	vp = ZTOV(zp);
6284ccbb6e7Sahrens 	vn_reinit(vp);
6294ccbb6e7Sahrens 
630b5fca8f8Stomee 	zfs_znode_dmu_init(zfsvfs, zp, db);
6314ccbb6e7Sahrens 
632f18faf3fSek 	zp->z_gen = zp->z_phys->zp_gen;
633fa9e4066Sahrens 
634fa9e4066Sahrens 	vp->v_vfsp = zfsvfs->z_parent->z_vfs;
635fa9e4066Sahrens 	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
636fa9e4066Sahrens 
637fa9e4066Sahrens 	switch (vp->v_type) {
638fa9e4066Sahrens 	case VDIR:
639fa9e4066Sahrens 		if (zp->z_phys->zp_flags & ZFS_XATTR) {
640fa9e4066Sahrens 			vn_setops(vp, zfs_xdvnodeops);
641fa9e4066Sahrens 			vp->v_flag |= V_XATTRDIR;
6424ccbb6e7Sahrens 		} else {
643fa9e4066Sahrens 			vn_setops(vp, zfs_dvnodeops);
6444ccbb6e7Sahrens 		}
6457f6e3e7dSperrin 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
646fa9e4066Sahrens 		break;
647fa9e4066Sahrens 	case VBLK:
648fa9e4066Sahrens 	case VCHR:
64972fc53bcSmarks 		vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
650fa9e4066Sahrens 		/*FALLTHROUGH*/
651fa9e4066Sahrens 	case VFIFO:
652fa9e4066Sahrens 	case VSOCK:
653fa9e4066Sahrens 	case VDOOR:
654fa9e4066Sahrens 		vn_setops(vp, zfs_fvnodeops);
655fa9e4066Sahrens 		break;
656fa9e4066Sahrens 	case VREG:
657fa9e4066Sahrens 		vp->v_flag |= VMODSORT;
658743a77edSAlan Wright 		if (zp->z_phys->zp_parent == zfsvfs->z_shares_dir)
659743a77edSAlan Wright 			vn_setops(vp, zfs_sharevnodeops);
660743a77edSAlan Wright 		else
661743a77edSAlan Wright 			vn_setops(vp, zfs_fvnodeops);
662fa9e4066Sahrens 		break;
663fa9e4066Sahrens 	case VLNK:
664fa9e4066Sahrens 		vn_setops(vp, zfs_symvnodeops);
665fa9e4066Sahrens 		break;
666fa9e4066Sahrens 	default:
667fa9e4066Sahrens 		vn_setops(vp, zfs_evnodeops);
668fa9e4066Sahrens 		break;
669fa9e4066Sahrens 	}
670fa9e4066Sahrens 
671b5fca8f8Stomee 	mutex_enter(&zfsvfs->z_znodes_lock);
672b5fca8f8Stomee 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
673b5fca8f8Stomee 	membar_producer();
674b5fca8f8Stomee 	/*
675b5fca8f8Stomee 	 * Everything else must be valid before assigning z_zfsvfs makes the
676b5fca8f8Stomee 	 * znode eligible for zfs_znode_move().
677b5fca8f8Stomee 	 */
678b5fca8f8Stomee 	zp->z_zfsvfs = zfsvfs;
679b5fca8f8Stomee 	mutex_exit(&zfsvfs->z_znodes_lock);
680b5fca8f8Stomee 
681874395d5Smaybee 	VFS_HOLD(zfsvfs->z_vfs);
682fa9e4066Sahrens 	return (zp);
683fa9e4066Sahrens }
684fa9e4066Sahrens 
685fa9e4066Sahrens /*
686fa9e4066Sahrens  * Create a new DMU object to hold a zfs znode.
687fa9e4066Sahrens  *
688fa9e4066Sahrens  *	IN:	dzp	- parent directory for new znode
689fa9e4066Sahrens  *		vap	- file attributes for new znode
690fa9e4066Sahrens  *		tx	- dmu transaction id for zap operations
691fa9e4066Sahrens  *		cr	- credentials of caller
692fa9e4066Sahrens  *		flag	- flags:
693fa9e4066Sahrens  *			  IS_ROOT_NODE	- new object will be root
694fa9e4066Sahrens  *			  IS_XATTR	- new object is an attribute
695fa9e4066Sahrens  *			  IS_REPLAY	- intent log replay
696da6c28aaSamw  *		bonuslen - length of bonus buffer
697da6c28aaSamw  *		setaclp  - File/Dir initial ACL
698da6c28aaSamw  *		fuidp	 - Tracks fuid allocation.
699fa9e4066Sahrens  *
7004ccbb6e7Sahrens  *	OUT:	zpp	- allocated znode
701fa9e4066Sahrens  *
702fa9e4066Sahrens  */
703fa9e4066Sahrens void
7044ccbb6e7Sahrens zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
70589459e17SMark Shellenbaum     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_ids_t *acl_ids)
706fa9e4066Sahrens {
7074ccbb6e7Sahrens 	dmu_buf_t	*db;
708fa9e4066Sahrens 	znode_phys_t	*pzp;
709fa9e4066Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
710fa9e4066Sahrens 	timestruc_t	now;
7114ccbb6e7Sahrens 	uint64_t	gen, obj;
712fa9e4066Sahrens 	int		err;
713fa9e4066Sahrens 
714fa9e4066Sahrens 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
715fa9e4066Sahrens 
7161209a471SNeil Perrin 	if (zfsvfs->z_replay) {
7174ccbb6e7Sahrens 		obj = vap->va_nodeid;
718fa9e4066Sahrens 		flag |= IS_REPLAY;
719fa9e4066Sahrens 		now = vap->va_ctime;		/* see zfs_replay_create() */
720fa9e4066Sahrens 		gen = vap->va_nblocks;		/* ditto */
721fa9e4066Sahrens 	} else {
7224ccbb6e7Sahrens 		obj = 0;
723fa9e4066Sahrens 		gethrestime(&now);
724fa9e4066Sahrens 		gen = dmu_tx_get_txg(tx);
725fa9e4066Sahrens 	}
726fa9e4066Sahrens 
727fa9e4066Sahrens 	/*
728fa9e4066Sahrens 	 * Create a new DMU object.
729fa9e4066Sahrens 	 */
730ea8dc4b6Seschrock 	/*
731ea8dc4b6Seschrock 	 * There's currently no mechanism for pre-reading the blocks that will
732ea8dc4b6Seschrock 	 * be to needed allocate a new object, so we accept the small chance
733ea8dc4b6Seschrock 	 * that there will be an i/o error and we will fail one of the
734ea8dc4b6Seschrock 	 * assertions below.
735ea8dc4b6Seschrock 	 */
736fa9e4066Sahrens 	if (vap->va_type == VDIR) {
737fa9e4066Sahrens 		if (flag & IS_REPLAY) {
7384ccbb6e7Sahrens 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
739da6c28aaSamw 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
740fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
741fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
742fa9e4066Sahrens 		} else {
7434ccbb6e7Sahrens 			obj = zap_create_norm(zfsvfs->z_os,
744da6c28aaSamw 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
745fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
746fa9e4066Sahrens 		}
747fa9e4066Sahrens 	} else {
748fa9e4066Sahrens 		if (flag & IS_REPLAY) {
7494ccbb6e7Sahrens 			err = dmu_object_claim(zfsvfs->z_os, obj,
750fa9e4066Sahrens 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
751fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
752fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
753fa9e4066Sahrens 		} else {
7544ccbb6e7Sahrens 			obj = dmu_object_alloc(zfsvfs->z_os,
755fa9e4066Sahrens 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
756fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
757fa9e4066Sahrens 		}
758fa9e4066Sahrens 	}
7594ccbb6e7Sahrens 	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
7604ccbb6e7Sahrens 	dmu_buf_will_dirty(db, tx);
761fa9e4066Sahrens 
762fa9e4066Sahrens 	/*
763fa9e4066Sahrens 	 * Initialize the znode physical data to zero.
764fa9e4066Sahrens 	 */
7654ccbb6e7Sahrens 	ASSERT(db->db_size >= sizeof (znode_phys_t));
7664ccbb6e7Sahrens 	bzero(db->db_data, db->db_size);
7674ccbb6e7Sahrens 	pzp = db->db_data;
768fa9e4066Sahrens 
769fa9e4066Sahrens 	/*
770fa9e4066Sahrens 	 * If this is the root, fix up the half-initialized parent pointer
771fa9e4066Sahrens 	 * to reference the just-allocated physical data area.
772fa9e4066Sahrens 	 */
773fa9e4066Sahrens 	if (flag & IS_ROOT_NODE) {
774874395d5Smaybee 		dzp->z_dbuf = db;
775fa9e4066Sahrens 		dzp->z_phys = pzp;
7764ccbb6e7Sahrens 		dzp->z_id = obj;
777fa9e4066Sahrens 	}
778fa9e4066Sahrens 
779fa9e4066Sahrens 	/*
780fa9e4066Sahrens 	 * If parent is an xattr, so am I.
781fa9e4066Sahrens 	 */
782fa9e4066Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR)
783fa9e4066Sahrens 		flag |= IS_XATTR;
784fa9e4066Sahrens 
785fa9e4066Sahrens 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
78672fc53bcSmarks 		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
787fa9e4066Sahrens 	}
788fa9e4066Sahrens 
789da6c28aaSamw 	if (zfsvfs->z_use_fuids)
790da6c28aaSamw 		pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
791da6c28aaSamw 
792fa9e4066Sahrens 	if (vap->va_type == VDIR) {
793fa9e4066Sahrens 		pzp->zp_size = 2;		/* contents ("." and "..") */
794fa9e4066Sahrens 		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
795fa9e4066Sahrens 	}
796fa9e4066Sahrens 
797fa9e4066Sahrens 	pzp->zp_parent = dzp->z_id;
798fa9e4066Sahrens 	if (flag & IS_XATTR)
799fa9e4066Sahrens 		pzp->zp_flags |= ZFS_XATTR;
800fa9e4066Sahrens 
801fa9e4066Sahrens 	pzp->zp_gen = gen;
802fa9e4066Sahrens 
803fa9e4066Sahrens 	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
804fa9e4066Sahrens 	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
805fa9e4066Sahrens 
806fa9e4066Sahrens 	if (vap->va_mask & AT_ATIME) {
807fa9e4066Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
808fa9e4066Sahrens 	} else {
809fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
810fa9e4066Sahrens 	}
811fa9e4066Sahrens 
812fa9e4066Sahrens 	if (vap->va_mask & AT_MTIME) {
813fa9e4066Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
814fa9e4066Sahrens 	} else {
815fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
816fa9e4066Sahrens 	}
817fa9e4066Sahrens 
818fa9e4066Sahrens 	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
819874395d5Smaybee 	if (!(flag & IS_ROOT_NODE)) {
820b5fca8f8Stomee 		ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
821874395d5Smaybee 		*zpp = zfs_znode_alloc(zfsvfs, db, 0);
822874395d5Smaybee 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
823874395d5Smaybee 	} else {
824874395d5Smaybee 		/*
825874395d5Smaybee 		 * If we are creating the root node, the "parent" we
826874395d5Smaybee 		 * passed in is the znode for the root.
827874395d5Smaybee 		 */
828874395d5Smaybee 		*zpp = dzp;
829874395d5Smaybee 	}
83089459e17SMark Shellenbaum 	pzp->zp_uid = acl_ids->z_fuid;
83189459e17SMark Shellenbaum 	pzp->zp_gid = acl_ids->z_fgid;
83289459e17SMark Shellenbaum 	pzp->zp_mode = acl_ids->z_mode;
83389459e17SMark Shellenbaum 	VERIFY(0 == zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
83489459e17SMark Shellenbaum 	if (vap->va_mask & AT_XVATTR)
83589459e17SMark Shellenbaum 		zfs_xvattr_set(*zpp, (xvattr_t *)vap);
836fa9e4066Sahrens }
837fa9e4066Sahrens 
838da6c28aaSamw void
839da6c28aaSamw zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
840da6c28aaSamw {
841da6c28aaSamw 	xoptattr_t *xoap;
842da6c28aaSamw 
843da6c28aaSamw 	xoap = xva_getxoptattr(xvap);
844da6c28aaSamw 	ASSERT(xoap);
845da6c28aaSamw 
846da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
847da6c28aaSamw 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
848da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_CREATETIME);
849da6c28aaSamw 	}
850da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
851da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
852da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_READONLY);
853da6c28aaSamw 	}
854da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
855da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
856da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_HIDDEN);
857da6c28aaSamw 	}
858da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
859da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
860da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_SYSTEM);
861da6c28aaSamw 	}
862da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
863da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
864da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
865da6c28aaSamw 	}
866da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
867da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
868da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
869da6c28aaSamw 	}
870da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
871da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
872da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
873da6c28aaSamw 	}
874da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
875da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
876da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
877da6c28aaSamw 	}
878da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
879da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
880da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_NODUMP);
881da6c28aaSamw 	}
882da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
883da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
884da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_OPAQUE);
885da6c28aaSamw 	}
886da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
887da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
888da6c28aaSamw 		    xoap->xoa_av_quarantined);
889da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
890da6c28aaSamw 	}
891da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
892da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
893da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
894da6c28aaSamw 	}
895da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
896da6c28aaSamw 		(void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
897da6c28aaSamw 		    sizeof (xoap->xoa_av_scanstamp));
898da6c28aaSamw 		zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
899da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
900da6c28aaSamw 	}
901da6c28aaSamw }
902da6c28aaSamw 
903fa9e4066Sahrens int
904fa9e4066Sahrens zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
905fa9e4066Sahrens {
906fa9e4066Sahrens 	dmu_object_info_t doi;
907fa9e4066Sahrens 	dmu_buf_t	*db;
908fa9e4066Sahrens 	znode_t		*zp;
909ea8dc4b6Seschrock 	int err;
910fa9e4066Sahrens 
911fa9e4066Sahrens 	*zpp = NULL;
912fa9e4066Sahrens 
913fa9e4066Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
914fa9e4066Sahrens 
915ea8dc4b6Seschrock 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
916ea8dc4b6Seschrock 	if (err) {
917fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
918ea8dc4b6Seschrock 		return (err);
919fa9e4066Sahrens 	}
920fa9e4066Sahrens 
921fa9e4066Sahrens 	dmu_object_info_from_db(db, &doi);
922fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
923fa9e4066Sahrens 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
924ea8dc4b6Seschrock 		dmu_buf_rele(db, NULL);
925fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
926fa9e4066Sahrens 		return (EINVAL);
927fa9e4066Sahrens 	}
928fa9e4066Sahrens 
929fa9e4066Sahrens 	zp = dmu_buf_get_user(db);
930fa9e4066Sahrens 	if (zp != NULL) {
931fa9e4066Sahrens 		mutex_enter(&zp->z_lock);
932fa9e4066Sahrens 
9334ccbb6e7Sahrens 		/*
9344ccbb6e7Sahrens 		 * Since we do immediate eviction of the z_dbuf, we
9354ccbb6e7Sahrens 		 * should never find a dbuf with a znode that doesn't
9364ccbb6e7Sahrens 		 * know about the dbuf.
9374ccbb6e7Sahrens 		 */
9384ccbb6e7Sahrens 		ASSERT3P(zp->z_dbuf, ==, db);
939fa9e4066Sahrens 		ASSERT3U(zp->z_id, ==, obj_num);
940893a6d32Sahrens 		if (zp->z_unlinked) {
9414ccbb6e7Sahrens 			err = ENOENT;
942fa9e4066Sahrens 		} else {
9434ccbb6e7Sahrens 			VN_HOLD(ZTOV(zp));
9444ccbb6e7Sahrens 			*zpp = zp;
9454ccbb6e7Sahrens 			err = 0;
946fa9e4066Sahrens 		}
9474ccbb6e7Sahrens 		dmu_buf_rele(db, NULL);
948fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
949ea8dc4b6Seschrock 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
9504ccbb6e7Sahrens 		return (err);
951fa9e4066Sahrens 	}
952fa9e4066Sahrens 
953fa9e4066Sahrens 	/*
954fa9e4066Sahrens 	 * Not found create new znode/vnode
955fa9e4066Sahrens 	 */
9564ccbb6e7Sahrens 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
957ea8dc4b6Seschrock 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
958fa9e4066Sahrens 	*zpp = zp;
959fa9e4066Sahrens 	return (0);
960fa9e4066Sahrens }
961fa9e4066Sahrens 
962f18faf3fSek int
963f18faf3fSek zfs_rezget(znode_t *zp)
964f18faf3fSek {
965f18faf3fSek 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
966f18faf3fSek 	dmu_object_info_t doi;
967f18faf3fSek 	dmu_buf_t *db;
968f18faf3fSek 	uint64_t obj_num = zp->z_id;
969f18faf3fSek 	int err;
970f18faf3fSek 
971f18faf3fSek 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
972f18faf3fSek 
973f18faf3fSek 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
974f18faf3fSek 	if (err) {
975f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
976f18faf3fSek 		return (err);
977f18faf3fSek 	}
978f18faf3fSek 
979f18faf3fSek 	dmu_object_info_from_db(db, &doi);
980f18faf3fSek 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
981f18faf3fSek 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
982f18faf3fSek 		dmu_buf_rele(db, NULL);
983f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
984f18faf3fSek 		return (EINVAL);
985f18faf3fSek 	}
986f18faf3fSek 
987f18faf3fSek 	if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
988f18faf3fSek 		dmu_buf_rele(db, NULL);
989f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
990f18faf3fSek 		return (EIO);
991f18faf3fSek 	}
992f18faf3fSek 
993b5fca8f8Stomee 	zfs_znode_dmu_init(zfsvfs, zp, db);
994f18faf3fSek 	zp->z_unlinked = (zp->z_phys->zp_links == 0);
9956166ad1cSek 	zp->z_blksz = doi.doi_data_block_size;
996f18faf3fSek 
997f18faf3fSek 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
998f18faf3fSek 
999f18faf3fSek 	return (0);
1000f18faf3fSek }
1001f18faf3fSek 
1002fa9e4066Sahrens void
1003fa9e4066Sahrens zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1004fa9e4066Sahrens {
1005fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1006cdb0ab79Smaybee 	objset_t *os = zfsvfs->z_os;
10074ccbb6e7Sahrens 	uint64_t obj = zp->z_id;
1008cdb0ab79Smaybee 	uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
1009fa9e4066Sahrens 
10104ccbb6e7Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1011cdb0ab79Smaybee 	if (acl_obj)
1012cdb0ab79Smaybee 		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1013cdb0ab79Smaybee 	VERIFY(0 == dmu_object_free(os, obj, tx));
10144ccbb6e7Sahrens 	zfs_znode_dmu_fini(zp);
10154ccbb6e7Sahrens 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1016874395d5Smaybee 	zfs_znode_free(zp);
1017fa9e4066Sahrens }
1018fa9e4066Sahrens 
1019fa9e4066Sahrens void
1020fa9e4066Sahrens zfs_zinactive(znode_t *zp)
1021fa9e4066Sahrens {
1022fa9e4066Sahrens 	vnode_t	*vp = ZTOV(zp);
1023fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1024fa9e4066Sahrens 	uint64_t z_id = zp->z_id;
1025fa9e4066Sahrens 
10264ccbb6e7Sahrens 	ASSERT(zp->z_dbuf && zp->z_phys);
1027fa9e4066Sahrens 
1028fa9e4066Sahrens 	/*
1029fa9e4066Sahrens 	 * Don't allow a zfs_zget() while were trying to release this znode
1030fa9e4066Sahrens 	 */
1031fa9e4066Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1032fa9e4066Sahrens 
1033fa9e4066Sahrens 	mutex_enter(&zp->z_lock);
1034fa9e4066Sahrens 	mutex_enter(&vp->v_lock);
1035fa9e4066Sahrens 	vp->v_count--;
1036fa9e4066Sahrens 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1037fa9e4066Sahrens 		/*
1038fa9e4066Sahrens 		 * If the hold count is greater than zero, somebody has
1039fa9e4066Sahrens 		 * obtained a new reference on this znode while we were
1040fa9e4066Sahrens 		 * processing it here, so we are done.  If we still have
1041fa9e4066Sahrens 		 * mapped pages then we are also done, since we don't
1042fa9e4066Sahrens 		 * want to inactivate the znode until the pages get pushed.
1043fa9e4066Sahrens 		 *
1044fa9e4066Sahrens 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1045fa9e4066Sahrens 		 * this seems like it would leave the znode hanging with
1046fa9e4066Sahrens 		 * no chance to go inactive...
1047fa9e4066Sahrens 		 */
1048fa9e4066Sahrens 		mutex_exit(&vp->v_lock);
1049fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1050fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1051fa9e4066Sahrens 		return;
1052fa9e4066Sahrens 	}
1053fa9e4066Sahrens 	mutex_exit(&vp->v_lock);
1054fa9e4066Sahrens 
1055fa9e4066Sahrens 	/*
1056fa9e4066Sahrens 	 * If this was the last reference to a file with no links,
1057fa9e4066Sahrens 	 * remove the file from the file system.
1058fa9e4066Sahrens 	 */
1059893a6d32Sahrens 	if (zp->z_unlinked) {
1060fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1061fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1062893a6d32Sahrens 		zfs_rmnode(zp);
1063fa9e4066Sahrens 		return;
1064fa9e4066Sahrens 	}
1065fa9e4066Sahrens 	mutex_exit(&zp->z_lock);
10664ccbb6e7Sahrens 	zfs_znode_dmu_fini(zp);
1067fa9e4066Sahrens 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1068874395d5Smaybee 	zfs_znode_free(zp);
1069fa9e4066Sahrens }
1070fa9e4066Sahrens 
1071fa9e4066Sahrens void
1072fa9e4066Sahrens zfs_znode_free(znode_t *zp)
1073fa9e4066Sahrens {
1074fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1075fa9e4066Sahrens 
1076874395d5Smaybee 	vn_invalid(ZTOV(zp));
1077874395d5Smaybee 
1078b5fca8f8Stomee 	ASSERT(ZTOV(zp)->v_count == 0);
1079b5fca8f8Stomee 
1080fa9e4066Sahrens 	mutex_enter(&zfsvfs->z_znodes_lock);
1081b5fca8f8Stomee 	POINTER_INVALIDATE(&zp->z_zfsvfs);
1082fa9e4066Sahrens 	list_remove(&zfsvfs->z_all_znodes, zp);
1083fa9e4066Sahrens 	mutex_exit(&zfsvfs->z_znodes_lock);
1084fa9e4066Sahrens 
1085*d47621a4STim Haley 	if (zp->z_acl_cached) {
1086*d47621a4STim Haley 		zfs_acl_free(zp->z_acl_cached);
1087*d47621a4STim Haley 		zp->z_acl_cached = NULL;
1088*d47621a4STim Haley 	}
1089*d47621a4STim Haley 
1090fa9e4066Sahrens 	kmem_cache_free(znode_cache, zp);
1091874395d5Smaybee 
1092874395d5Smaybee 	VFS_RELE(zfsvfs->z_vfs);
1093fa9e4066Sahrens }
1094fa9e4066Sahrens 
1095fa9e4066Sahrens void
1096fa9e4066Sahrens zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1097fa9e4066Sahrens {
1098fa9e4066Sahrens 	timestruc_t	now;
1099fa9e4066Sahrens 
1100fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zp->z_lock));
1101fa9e4066Sahrens 
1102fa9e4066Sahrens 	gethrestime(&now);
1103fa9e4066Sahrens 
1104fa9e4066Sahrens 	if (tx) {
1105fa9e4066Sahrens 		dmu_buf_will_dirty(zp->z_dbuf, tx);
1106fa9e4066Sahrens 		zp->z_atime_dirty = 0;
1107fa9e4066Sahrens 		zp->z_seq++;
1108fa9e4066Sahrens 	} else {
1109fa9e4066Sahrens 		zp->z_atime_dirty = 1;
1110fa9e4066Sahrens 	}
1111fa9e4066Sahrens 
1112fa9e4066Sahrens 	if (flag & AT_ATIME)
1113fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
1114fa9e4066Sahrens 
1115da6c28aaSamw 	if (flag & AT_MTIME) {
1116fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
1117da6c28aaSamw 		if (zp->z_zfsvfs->z_use_fuids)
1118da6c28aaSamw 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
1119da6c28aaSamw 	}
1120fa9e4066Sahrens 
1121da6c28aaSamw 	if (flag & AT_CTIME) {
1122fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
1123da6c28aaSamw 		if (zp->z_zfsvfs->z_use_fuids)
1124da6c28aaSamw 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
1125da6c28aaSamw 	}
1126fa9e4066Sahrens }
1127fa9e4066Sahrens 
1128fa9e4066Sahrens /*
1129fa9e4066Sahrens  * Update the requested znode timestamps with the current time.
1130fa9e4066Sahrens  * If we are in a transaction, then go ahead and mark the znode
1131fa9e4066Sahrens  * dirty in the transaction so the timestamps will go to disk.
1132fa9e4066Sahrens  * Otherwise, we will get pushed next time the znode is updated
1133fa9e4066Sahrens  * in a transaction, or when this znode eventually goes inactive.
1134fa9e4066Sahrens  *
1135fa9e4066Sahrens  * Why is this OK?
1136fa9e4066Sahrens  *  1 - Only the ACCESS time is ever updated outside of a transaction.
1137fa9e4066Sahrens  *  2 - Multiple consecutive updates will be collapsed into a single
1138fa9e4066Sahrens  *	znode update by the transaction grouping semantics of the DMU.
1139fa9e4066Sahrens  */
1140fa9e4066Sahrens void
1141fa9e4066Sahrens zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1142fa9e4066Sahrens {
1143fa9e4066Sahrens 	mutex_enter(&zp->z_lock);
1144fa9e4066Sahrens 	zfs_time_stamper_locked(zp, flag, tx);
1145fa9e4066Sahrens 	mutex_exit(&zp->z_lock);
1146fa9e4066Sahrens }
1147fa9e4066Sahrens 
1148fa9e4066Sahrens /*
1149104e2ed7Sperrin  * Grow the block size for a file.
1150fa9e4066Sahrens  *
1151fa9e4066Sahrens  *	IN:	zp	- znode of file to free data in.
1152fa9e4066Sahrens  *		size	- requested block size
1153fa9e4066Sahrens  *		tx	- open transaction.
1154fa9e4066Sahrens  *
1155fa9e4066Sahrens  * NOTE: this function assumes that the znode is write locked.
1156fa9e4066Sahrens  */
1157104e2ed7Sperrin void
1158fa9e4066Sahrens zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1159fa9e4066Sahrens {
1160fa9e4066Sahrens 	int		error;
1161fa9e4066Sahrens 	u_longlong_t	dummy;
1162fa9e4066Sahrens 
1163fa9e4066Sahrens 	if (size <= zp->z_blksz)
1164104e2ed7Sperrin 		return;
1165fa9e4066Sahrens 	/*
1166fa9e4066Sahrens 	 * If the file size is already greater than the current blocksize,
1167fa9e4066Sahrens 	 * we will not grow.  If there is more than one block in a file,
1168fa9e4066Sahrens 	 * the blocksize cannot change.
1169fa9e4066Sahrens 	 */
1170fa9e4066Sahrens 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1171104e2ed7Sperrin 		return;
1172fa9e4066Sahrens 
1173fa9e4066Sahrens 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1174fa9e4066Sahrens 	    size, 0, tx);
1175fa9e4066Sahrens 	if (error == ENOTSUP)
1176104e2ed7Sperrin 		return;
1177fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
1178fa9e4066Sahrens 
1179fa9e4066Sahrens 	/* What blocksize did we actually get? */
1180fa9e4066Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1181fa9e4066Sahrens }
1182fa9e4066Sahrens 
1183fa9e4066Sahrens /*
1184fa9e4066Sahrens  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1185fa9e4066Sahrens  * be calling back into the fs for a putpage().  E.g.: when truncating
1186fa9e4066Sahrens  * a file, the pages being "thrown away* don't need to be written out.
1187fa9e4066Sahrens  */
1188fa9e4066Sahrens /* ARGSUSED */
1189fa9e4066Sahrens static int
1190fa9e4066Sahrens zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1191fa9e4066Sahrens     int flags, cred_t *cr)
1192fa9e4066Sahrens {
1193fa9e4066Sahrens 	ASSERT(0);
1194fa9e4066Sahrens 	return (0);
1195fa9e4066Sahrens }
1196fa9e4066Sahrens 
1197fa9e4066Sahrens /*
1198cdb0ab79Smaybee  * Increase the file length
1199fa9e4066Sahrens  *
1200fa9e4066Sahrens  *	IN:	zp	- znode of file to free data in.
1201cdb0ab79Smaybee  *		end	- new end-of-file
1202fa9e4066Sahrens  *
1203fa9e4066Sahrens  * 	RETURN:	0 if success
1204fa9e4066Sahrens  *		error code if failure
1205fa9e4066Sahrens  */
1206cdb0ab79Smaybee static int
1207cdb0ab79Smaybee zfs_extend(znode_t *zp, uint64_t end)
1208fa9e4066Sahrens {
12095730cc9aSmaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1210cdb0ab79Smaybee 	dmu_tx_t *tx;
12115730cc9aSmaybee 	rl_t *rl;
1212cdb0ab79Smaybee 	uint64_t newblksz;
1213104e2ed7Sperrin 	int error;
1214fa9e4066Sahrens 
12155730cc9aSmaybee 	/*
1216cdb0ab79Smaybee 	 * We will change zp_size, lock the whole file.
12175730cc9aSmaybee 	 */
1218cdb0ab79Smaybee 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
12195730cc9aSmaybee 
1220fa9e4066Sahrens 	/*
1221fa9e4066Sahrens 	 * Nothing to do if file already at desired length.
1222fa9e4066Sahrens 	 */
1223cdb0ab79Smaybee 	if (end <= zp->z_phys->zp_size) {
1224c5c6ffa0Smaybee 		zfs_range_unlock(rl);
1225fa9e4066Sahrens 		return (0);
1226fa9e4066Sahrens 	}
1227cdb0ab79Smaybee top:
12285730cc9aSmaybee 	tx = dmu_tx_create(zfsvfs->z_os);
12295730cc9aSmaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
1230cdb0ab79Smaybee 	if (end > zp->z_blksz &&
12315730cc9aSmaybee 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1232fa9e4066Sahrens 		/*
1233fa9e4066Sahrens 		 * We are growing the file past the current block size.
1234fa9e4066Sahrens 		 */
1235fa9e4066Sahrens 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1236fa9e4066Sahrens 			ASSERT(!ISP2(zp->z_blksz));
1237cdb0ab79Smaybee 			newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1238fa9e4066Sahrens 		} else {
1239cdb0ab79Smaybee 			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1240fa9e4066Sahrens 		}
1241cdb0ab79Smaybee 		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1242cdb0ab79Smaybee 	} else {
1243cdb0ab79Smaybee 		newblksz = 0;
12445730cc9aSmaybee 	}
12455730cc9aSmaybee 
12461209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
12475730cc9aSmaybee 	if (error) {
12481209a471SNeil Perrin 		if (error == ERESTART) {
12498a2f1b91Sahrens 			dmu_tx_wait(tx);
1250cdb0ab79Smaybee 			dmu_tx_abort(tx);
1251cdb0ab79Smaybee 			goto top;
1252cdb0ab79Smaybee 		}
12535730cc9aSmaybee 		dmu_tx_abort(tx);
1254c5c6ffa0Smaybee 		zfs_range_unlock(rl);
12555730cc9aSmaybee 		return (error);
1256fa9e4066Sahrens 	}
1257cdb0ab79Smaybee 	dmu_buf_will_dirty(zp->z_dbuf, tx);
12585730cc9aSmaybee 
1259cdb0ab79Smaybee 	if (newblksz)
1260cdb0ab79Smaybee 		zfs_grow_blocksize(zp, newblksz, tx);
12615730cc9aSmaybee 
1262cdb0ab79Smaybee 	zp->z_phys->zp_size = end;
12635730cc9aSmaybee 
1264cdb0ab79Smaybee 	zfs_range_unlock(rl);
12655730cc9aSmaybee 
1266cdb0ab79Smaybee 	dmu_tx_commit(tx);
12675730cc9aSmaybee 
1268cdb0ab79Smaybee 	return (0);
1269cdb0ab79Smaybee }
1270cdb0ab79Smaybee 
1271cdb0ab79Smaybee /*
1272cdb0ab79Smaybee  * Free space in a file.
1273cdb0ab79Smaybee  *
1274cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1275cdb0ab79Smaybee  *		off	- start of section to free.
1276cdb0ab79Smaybee  *		len	- length of section to free.
1277cdb0ab79Smaybee  *
1278cdb0ab79Smaybee  * 	RETURN:	0 if success
1279cdb0ab79Smaybee  *		error code if failure
1280cdb0ab79Smaybee  */
1281cdb0ab79Smaybee static int
1282cdb0ab79Smaybee zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1283cdb0ab79Smaybee {
1284cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1285cdb0ab79Smaybee 	rl_t *rl;
1286cdb0ab79Smaybee 	int error;
1287cdb0ab79Smaybee 
1288cdb0ab79Smaybee 	/*
1289cdb0ab79Smaybee 	 * Lock the range being freed.
1290cdb0ab79Smaybee 	 */
1291cdb0ab79Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1292cdb0ab79Smaybee 
1293cdb0ab79Smaybee 	/*
1294cdb0ab79Smaybee 	 * Nothing to do if file already at desired length.
1295cdb0ab79Smaybee 	 */
1296cdb0ab79Smaybee 	if (off >= zp->z_phys->zp_size) {
1297cdb0ab79Smaybee 		zfs_range_unlock(rl);
1298cdb0ab79Smaybee 		return (0);
12995730cc9aSmaybee 	}
13005730cc9aSmaybee 
1301cdb0ab79Smaybee 	if (off + len > zp->z_phys->zp_size)
1302cdb0ab79Smaybee 		len = zp->z_phys->zp_size - off;
1303cdb0ab79Smaybee 
1304cdb0ab79Smaybee 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1305cdb0ab79Smaybee 
1306c5c6ffa0Smaybee 	zfs_range_unlock(rl);
13075730cc9aSmaybee 
1308cdb0ab79Smaybee 	return (error);
1309cdb0ab79Smaybee }
1310cdb0ab79Smaybee 
1311cdb0ab79Smaybee /*
1312cdb0ab79Smaybee  * Truncate a file
1313cdb0ab79Smaybee  *
1314cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1315cdb0ab79Smaybee  *		end	- new end-of-file.
1316cdb0ab79Smaybee  *
1317cdb0ab79Smaybee  * 	RETURN:	0 if success
1318cdb0ab79Smaybee  *		error code if failure
1319cdb0ab79Smaybee  */
1320cdb0ab79Smaybee static int
1321cdb0ab79Smaybee zfs_trunc(znode_t *zp, uint64_t end)
1322cdb0ab79Smaybee {
1323cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1324cdb0ab79Smaybee 	vnode_t *vp = ZTOV(zp);
1325cdb0ab79Smaybee 	dmu_tx_t *tx;
1326cdb0ab79Smaybee 	rl_t *rl;
1327cdb0ab79Smaybee 	int error;
1328cdb0ab79Smaybee 
1329cdb0ab79Smaybee 	/*
1330cdb0ab79Smaybee 	 * We will change zp_size, lock the whole file.
1331cdb0ab79Smaybee 	 */
1332cdb0ab79Smaybee 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1333cdb0ab79Smaybee 
1334cdb0ab79Smaybee 	/*
1335cdb0ab79Smaybee 	 * Nothing to do if file already at desired length.
1336cdb0ab79Smaybee 	 */
1337cdb0ab79Smaybee 	if (end >= zp->z_phys->zp_size) {
1338cdb0ab79Smaybee 		zfs_range_unlock(rl);
1339cdb0ab79Smaybee 		return (0);
1340cdb0ab79Smaybee 	}
1341cdb0ab79Smaybee 
1342cdb0ab79Smaybee 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1343cdb0ab79Smaybee 	if (error) {
1344cdb0ab79Smaybee 		zfs_range_unlock(rl);
1345cdb0ab79Smaybee 		return (error);
1346cdb0ab79Smaybee 	}
1347cdb0ab79Smaybee top:
1348cdb0ab79Smaybee 	tx = dmu_tx_create(zfsvfs->z_os);
1349cdb0ab79Smaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
13501209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1351cdb0ab79Smaybee 	if (error) {
13521209a471SNeil Perrin 		if (error == ERESTART) {
1353cdb0ab79Smaybee 			dmu_tx_wait(tx);
1354cdb0ab79Smaybee 			dmu_tx_abort(tx);
1355cdb0ab79Smaybee 			goto top;
1356cdb0ab79Smaybee 		}
1357cdb0ab79Smaybee 		dmu_tx_abort(tx);
1358cdb0ab79Smaybee 		zfs_range_unlock(rl);
1359cdb0ab79Smaybee 		return (error);
1360cdb0ab79Smaybee 	}
1361cdb0ab79Smaybee 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1362cdb0ab79Smaybee 
1363cdb0ab79Smaybee 	zp->z_phys->zp_size = end;
1364cdb0ab79Smaybee 
13655730cc9aSmaybee 	dmu_tx_commit(tx);
13665730cc9aSmaybee 
1367fa9e4066Sahrens 	/*
13685730cc9aSmaybee 	 * Clear any mapped pages in the truncated region.  This has to
13695730cc9aSmaybee 	 * happen outside of the transaction to avoid the possibility of
13705730cc9aSmaybee 	 * a deadlock with someone trying to push a page that we are
13715730cc9aSmaybee 	 * about to invalidate.
1372fa9e4066Sahrens 	 */
1373cdb0ab79Smaybee 	if (vn_has_cached_data(vp)) {
1374fa9e4066Sahrens 		page_t *pp;
1375cdb0ab79Smaybee 		uint64_t start = end & PAGEMASK;
1376cdb0ab79Smaybee 		int poff = end & PAGEOFFSET;
1377fa9e4066Sahrens 
13785730cc9aSmaybee 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1379fa9e4066Sahrens 			/*
1380fa9e4066Sahrens 			 * We need to zero a partial page.
1381fa9e4066Sahrens 			 */
13825730cc9aSmaybee 			pagezero(pp, poff, PAGESIZE - poff);
1383fa9e4066Sahrens 			start += PAGESIZE;
1384fa9e4066Sahrens 			page_unlock(pp);
1385fa9e4066Sahrens 		}
1386fa9e4066Sahrens 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
13875730cc9aSmaybee 		    B_INVAL | B_TRUNC, NULL);
1388fa9e4066Sahrens 		ASSERT(error == 0);
1389fa9e4066Sahrens 	}
1390ac05c741SMark Maybee 
1391ac05c741SMark Maybee 	zfs_range_unlock(rl);
1392fa9e4066Sahrens 
1393fa9e4066Sahrens 	return (0);
1394fa9e4066Sahrens }
1395fa9e4066Sahrens 
1396cdb0ab79Smaybee /*
1397cdb0ab79Smaybee  * Free space in a file
1398cdb0ab79Smaybee  *
1399cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1400cdb0ab79Smaybee  *		off	- start of range
1401cdb0ab79Smaybee  *		len	- end of range (0 => EOF)
1402cdb0ab79Smaybee  *		flag	- current file open mode flags.
1403cdb0ab79Smaybee  *		log	- TRUE if this action should be logged
1404cdb0ab79Smaybee  *
1405cdb0ab79Smaybee  * 	RETURN:	0 if success
1406cdb0ab79Smaybee  *		error code if failure
1407cdb0ab79Smaybee  */
1408cdb0ab79Smaybee int
1409cdb0ab79Smaybee zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1410cdb0ab79Smaybee {
1411cdb0ab79Smaybee 	vnode_t *vp = ZTOV(zp);
1412cdb0ab79Smaybee 	dmu_tx_t *tx;
1413cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1414cdb0ab79Smaybee 	zilog_t *zilog = zfsvfs->z_log;
1415cdb0ab79Smaybee 	int error;
1416cdb0ab79Smaybee 
1417cdb0ab79Smaybee 	if (off > zp->z_phys->zp_size) {
1418cdb0ab79Smaybee 		error =  zfs_extend(zp, off+len);
1419cdb0ab79Smaybee 		if (error == 0 && log)
1420cdb0ab79Smaybee 			goto log;
1421cdb0ab79Smaybee 		else
1422cdb0ab79Smaybee 			return (error);
1423cdb0ab79Smaybee 	}
1424cdb0ab79Smaybee 
1425cdb0ab79Smaybee 	/*
1426cdb0ab79Smaybee 	 * Check for any locks in the region to be freed.
1427cdb0ab79Smaybee 	 */
1428cdb0ab79Smaybee 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
1429cdb0ab79Smaybee 		uint64_t length = (len ? len : zp->z_phys->zp_size - off);
1430cdb0ab79Smaybee 		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1431cdb0ab79Smaybee 			return (error);
1432cdb0ab79Smaybee 	}
1433cdb0ab79Smaybee 
1434cdb0ab79Smaybee 	if (len == 0) {
1435cdb0ab79Smaybee 		error = zfs_trunc(zp, off);
1436cdb0ab79Smaybee 	} else {
1437cdb0ab79Smaybee 		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1438cdb0ab79Smaybee 		    off + len > zp->z_phys->zp_size)
1439cdb0ab79Smaybee 			error = zfs_extend(zp, off+len);
1440cdb0ab79Smaybee 	}
1441cdb0ab79Smaybee 	if (error || !log)
1442cdb0ab79Smaybee 		return (error);
1443cdb0ab79Smaybee log:
1444cdb0ab79Smaybee 	tx = dmu_tx_create(zfsvfs->z_os);
1445cdb0ab79Smaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
14461209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1447cdb0ab79Smaybee 	if (error) {
14481209a471SNeil Perrin 		if (error == ERESTART) {
1449cdb0ab79Smaybee 			dmu_tx_wait(tx);
1450cdb0ab79Smaybee 			dmu_tx_abort(tx);
1451cdb0ab79Smaybee 			goto log;
1452cdb0ab79Smaybee 		}
1453cdb0ab79Smaybee 		dmu_tx_abort(tx);
1454cdb0ab79Smaybee 		return (error);
1455cdb0ab79Smaybee 	}
1456cdb0ab79Smaybee 
1457cdb0ab79Smaybee 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1458cdb0ab79Smaybee 	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1459cdb0ab79Smaybee 
1460cdb0ab79Smaybee 	dmu_tx_commit(tx);
1461cdb0ab79Smaybee 	return (0);
1462cdb0ab79Smaybee }
1463cdb0ab79Smaybee 
1464fa9e4066Sahrens void
1465de8267e0Stimh zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1466fa9e4066Sahrens {
1467fa9e4066Sahrens 	zfsvfs_t	zfsvfs;
146814843421SMatthew Ahrens 	uint64_t	moid, obj, version;
1469de8267e0Stimh 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1470de8267e0Stimh 	uint64_t	norm = 0;
1471de8267e0Stimh 	nvpair_t	*elem;
1472fa9e4066Sahrens 	int		error;
1473fa9e4066Sahrens 	znode_t		*rootzp = NULL;
1474fa9e4066Sahrens 	vnode_t		*vp;
1475fa9e4066Sahrens 	vattr_t		vattr;
14764ccbb6e7Sahrens 	znode_t		*zp;
147789459e17SMark Shellenbaum 	zfs_acl_ids_t	acl_ids;
1478fa9e4066Sahrens 
1479fa9e4066Sahrens 	/*
1480fa9e4066Sahrens 	 * First attempt to create master node.
1481fa9e4066Sahrens 	 */
1482ea8dc4b6Seschrock 	/*
1483ea8dc4b6Seschrock 	 * In an empty objset, there are no blocks to read and thus
1484ea8dc4b6Seschrock 	 * there can be no i/o errors (which we assert below).
1485ea8dc4b6Seschrock 	 */
1486fa9e4066Sahrens 	moid = MASTER_NODE_OBJ;
1487fa9e4066Sahrens 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1488fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
1489fa9e4066Sahrens 	ASSERT(error == 0);
1490fa9e4066Sahrens 
1491fa9e4066Sahrens 	/*
1492fa9e4066Sahrens 	 * Set starting attributes.
1493fa9e4066Sahrens 	 */
149414843421SMatthew Ahrens 	if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_USERSPACE)
1495088f3894Sahrens 		version = ZPL_VERSION;
149614843421SMatthew Ahrens 	else if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
149714843421SMatthew Ahrens 		version = ZPL_VERSION_USERSPACE - 1;
1498088f3894Sahrens 	else
1499088f3894Sahrens 		version = ZPL_VERSION_FUID - 1;
1500de8267e0Stimh 	elem = NULL;
1501de8267e0Stimh 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1502de8267e0Stimh 		/* For the moment we expect all zpl props to be uint64_ts */
1503de8267e0Stimh 		uint64_t val;
1504de8267e0Stimh 		char *name;
1505de8267e0Stimh 
1506de8267e0Stimh 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1507b3d911cbStimh 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1508de8267e0Stimh 		name = nvpair_name(elem);
1509de8267e0Stimh 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
151014843421SMatthew Ahrens 			if (val < version)
151114843421SMatthew Ahrens 				version = val;
1512de8267e0Stimh 		} else {
1513de8267e0Stimh 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1514de8267e0Stimh 		}
1515de8267e0Stimh 		ASSERT(error == 0);
1516de8267e0Stimh 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1517de8267e0Stimh 			norm = val;
1518de8267e0Stimh 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1519de8267e0Stimh 			sense = val;
1520de8267e0Stimh 	}
1521de8267e0Stimh 	ASSERT(version != 0);
152214843421SMatthew Ahrens 	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1523fa9e4066Sahrens 
1524fa9e4066Sahrens 	/*
1525fa9e4066Sahrens 	 * Create a delete queue.
1526fa9e4066Sahrens 	 */
152714843421SMatthew Ahrens 	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1528fa9e4066Sahrens 
152914843421SMatthew Ahrens 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1530fa9e4066Sahrens 	ASSERT(error == 0);
1531fa9e4066Sahrens 
1532fa9e4066Sahrens 	/*
1533fa9e4066Sahrens 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1534fa9e4066Sahrens 	 * to allow zfs_mknode to work.
1535fa9e4066Sahrens 	 */
1536fa9e4066Sahrens 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1537fa9e4066Sahrens 	vattr.va_type = VDIR;
1538fa9e4066Sahrens 	vattr.va_mode = S_IFDIR|0755;
1539ecd6cf80Smarks 	vattr.va_uid = crgetuid(cr);
1540ecd6cf80Smarks 	vattr.va_gid = crgetgid(cr);
1541fa9e4066Sahrens 
1542fa9e4066Sahrens 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1543893a6d32Sahrens 	rootzp->z_unlinked = 0;
1544fa9e4066Sahrens 	rootzp->z_atime_dirty = 0;
1545fa9e4066Sahrens 
1546fa9e4066Sahrens 	vp = ZTOV(rootzp);
1547fa9e4066Sahrens 	vn_reinit(vp);
1548fa9e4066Sahrens 	vp->v_type = VDIR;
1549fa9e4066Sahrens 
1550fa9e4066Sahrens 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1551fa9e4066Sahrens 
1552fa9e4066Sahrens 	zfsvfs.z_os = os;
1553fa9e4066Sahrens 	zfsvfs.z_parent = &zfsvfs;
1554da6c28aaSamw 	zfsvfs.z_version = version;
1555da6c28aaSamw 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1556da6c28aaSamw 	zfsvfs.z_norm = norm;
1557de8267e0Stimh 	/*
1558de8267e0Stimh 	 * Fold case on file systems that are always or sometimes case
1559de8267e0Stimh 	 * insensitive.
1560de8267e0Stimh 	 */
1561de8267e0Stimh 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1562de8267e0Stimh 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1563fa9e4066Sahrens 
1564fa9e4066Sahrens 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1565fa9e4066Sahrens 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1566fa9e4066Sahrens 	    offsetof(znode_t, z_link_node));
1567fa9e4066Sahrens 
1568b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1569b5fca8f8Stomee 	rootzp->z_zfsvfs = &zfsvfs;
157089459e17SMark Shellenbaum 	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
157189459e17SMark Shellenbaum 	    cr, NULL, &acl_ids));
157289459e17SMark Shellenbaum 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, &acl_ids);
1573874395d5Smaybee 	ASSERT3P(zp, ==, rootzp);
1574b5fca8f8Stomee 	ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
15754ccbb6e7Sahrens 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1576fa9e4066Sahrens 	ASSERT(error == 0);
157789459e17SMark Shellenbaum 	zfs_acl_ids_free(&acl_ids);
1578b5fca8f8Stomee 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1579fa9e4066Sahrens 
1580fa9e4066Sahrens 	ZTOV(rootzp)->v_count = 0;
1581874395d5Smaybee 	dmu_buf_rele(rootzp->z_dbuf, NULL);
1582874395d5Smaybee 	rootzp->z_dbuf = NULL;
1583fa9e4066Sahrens 	kmem_cache_free(znode_cache, rootzp);
1584743a77edSAlan Wright 
1585743a77edSAlan Wright 	/*
1586743a77edSAlan Wright 	 * Create shares directory
1587743a77edSAlan Wright 	 */
1588743a77edSAlan Wright 
1589743a77edSAlan Wright 	error = zfs_create_share_dir(&zfsvfs, tx);
159089459e17SMark Shellenbaum 
1591743a77edSAlan Wright 	ASSERT(error == 0);
1592fa9e4066Sahrens }
159355434c77Sek 
1594da6c28aaSamw #endif /* _KERNEL */
159555434c77Sek /*
159655434c77Sek  * Given an object number, return its parent object number and whether
159755434c77Sek  * or not the object is an extended attribute directory.
159855434c77Sek  */
159955434c77Sek static int
160055434c77Sek zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
160155434c77Sek {
160255434c77Sek 	dmu_buf_t *db;
160355434c77Sek 	dmu_object_info_t doi;
160455434c77Sek 	znode_phys_t *zp;
160555434c77Sek 	int error;
160655434c77Sek 
160755434c77Sek 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
160855434c77Sek 		return (error);
160955434c77Sek 
161055434c77Sek 	dmu_object_info_from_db(db, &doi);
161155434c77Sek 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
161255434c77Sek 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
161355434c77Sek 		dmu_buf_rele(db, FTAG);
161455434c77Sek 		return (EINVAL);
161555434c77Sek 	}
161655434c77Sek 
161755434c77Sek 	zp = db->db_data;
161855434c77Sek 	*pobjp = zp->zp_parent;
161955434c77Sek 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
162055434c77Sek 	    S_ISDIR(zp->zp_mode);
162155434c77Sek 	dmu_buf_rele(db, FTAG);
162255434c77Sek 
162355434c77Sek 	return (0);
162455434c77Sek }
162555434c77Sek 
162655434c77Sek int
162755434c77Sek zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
162855434c77Sek {
162955434c77Sek 	char *path = buf + len - 1;
163055434c77Sek 	int error;
163155434c77Sek 
163255434c77Sek 	*path = '\0';
163355434c77Sek 
163455434c77Sek 	for (;;) {
163555434c77Sek 		uint64_t pobj;
163655434c77Sek 		char component[MAXNAMELEN + 2];
163755434c77Sek 		size_t complen;
163855434c77Sek 		int is_xattrdir;
163955434c77Sek 
164055434c77Sek 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
164155434c77Sek 		    &is_xattrdir)) != 0)
164255434c77Sek 			break;
164355434c77Sek 
164455434c77Sek 		if (pobj == obj) {
164555434c77Sek 			if (path[0] != '/')
164655434c77Sek 				*--path = '/';
164755434c77Sek 			break;
164855434c77Sek 		}
164955434c77Sek 
165055434c77Sek 		component[0] = '/';
165155434c77Sek 		if (is_xattrdir) {
165255434c77Sek 			(void) sprintf(component + 1, "<xattrdir>");
165355434c77Sek 		} else {
1654e7437265Sahrens 			error = zap_value_search(osp, pobj, obj,
1655e7437265Sahrens 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
165655434c77Sek 			if (error != 0)
165755434c77Sek 				break;
165855434c77Sek 		}
165955434c77Sek 
166055434c77Sek 		complen = strlen(component);
166155434c77Sek 		path -= complen;
166255434c77Sek 		ASSERT(path >= buf);
166355434c77Sek 		bcopy(component, path, complen);
166455434c77Sek 		obj = pobj;
166555434c77Sek 	}
166655434c77Sek 
166755434c77Sek 	if (error == 0)
166855434c77Sek 		(void) memmove(buf, path, buf + len - path);
166955434c77Sek 	return (error);
167055434c77Sek }
1671