xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_znode.c (revision 4e9583b23260dab68308b306795694143381ab0f)
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
90*4e9583b2STom Erickson /*
91*4e9583b2STom Erickson  * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
92*4e9583b2STom Erickson  * be freed before it can be safely accessed.
93*4e9583b2STom Erickson  */
94*4e9583b2STom Erickson krwlock_t zfsvfs_lock;
95*4e9583b2STom 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;
136fa9e4066Sahrens 	return (0);
137fa9e4066Sahrens }
138fa9e4066Sahrens 
139fa9e4066Sahrens /*ARGSUSED*/
140fa9e4066Sahrens static void
141b5fca8f8Stomee zfs_znode_cache_destructor(void *buf, void *arg)
142fa9e4066Sahrens {
143fa9e4066Sahrens 	znode_t *zp = buf;
144fa9e4066Sahrens 
145b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
146b5fca8f8Stomee 	ASSERT(ZTOV(zp)->v_data == zp);
147b5fca8f8Stomee 	vn_free(ZTOV(zp));
148b5fca8f8Stomee 	ASSERT(!list_link_active(&zp->z_link_node));
149fa9e4066Sahrens 	mutex_destroy(&zp->z_lock);
150104e2ed7Sperrin 	rw_destroy(&zp->z_parent_lock);
151af2c4821Smaybee 	rw_destroy(&zp->z_name_lock);
152fa9e4066Sahrens 	mutex_destroy(&zp->z_acl_lock);
153104e2ed7Sperrin 	avl_destroy(&zp->z_range_avl);
154c25056deSgw 	mutex_destroy(&zp->z_range_lock);
155fa9e4066Sahrens 
1564ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
157b5fca8f8Stomee 	ASSERT(zp->z_dirlocks == NULL);
158b5fca8f8Stomee }
159b5fca8f8Stomee 
160b5fca8f8Stomee #ifdef	ZNODE_STATS
161b5fca8f8Stomee static struct {
162b5fca8f8Stomee 	uint64_t zms_zfsvfs_invalid;
163*4e9583b2STom Erickson 	uint64_t zms_zfsvfs_recheck1;
164b5fca8f8Stomee 	uint64_t zms_zfsvfs_unmounted;
165*4e9583b2STom Erickson 	uint64_t zms_zfsvfs_recheck2;
166a66b2b35STom Erickson 	uint64_t zms_obj_held;
167b5fca8f8Stomee 	uint64_t zms_vnode_locked;
168a66b2b35STom Erickson 	uint64_t zms_not_only_dnlc;
169b5fca8f8Stomee } znode_move_stats;
170b5fca8f8Stomee #endif	/* ZNODE_STATS */
171b5fca8f8Stomee 
172b5fca8f8Stomee static void
173b5fca8f8Stomee zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
174b5fca8f8Stomee {
175b5fca8f8Stomee 	vnode_t *vp;
176b5fca8f8Stomee 
177b5fca8f8Stomee 	/* Copy fields. */
178b5fca8f8Stomee 	nzp->z_zfsvfs = ozp->z_zfsvfs;
179b5fca8f8Stomee 
180b5fca8f8Stomee 	/* Swap vnodes. */
181b5fca8f8Stomee 	vp = nzp->z_vnode;
182b5fca8f8Stomee 	nzp->z_vnode = ozp->z_vnode;
183b5fca8f8Stomee 	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
184b5fca8f8Stomee 	ZTOV(ozp)->v_data = ozp;
185b5fca8f8Stomee 	ZTOV(nzp)->v_data = nzp;
186b5fca8f8Stomee 
187b5fca8f8Stomee 	nzp->z_id = ozp->z_id;
188b5fca8f8Stomee 	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
189b5fca8f8Stomee 	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
190b5fca8f8Stomee 	nzp->z_unlinked = ozp->z_unlinked;
191b5fca8f8Stomee 	nzp->z_atime_dirty = ozp->z_atime_dirty;
192b5fca8f8Stomee 	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
193b5fca8f8Stomee 	nzp->z_blksz = ozp->z_blksz;
194b5fca8f8Stomee 	nzp->z_seq = ozp->z_seq;
195b5fca8f8Stomee 	nzp->z_mapcnt = ozp->z_mapcnt;
196b5fca8f8Stomee 	nzp->z_last_itx = ozp->z_last_itx;
197b5fca8f8Stomee 	nzp->z_gen = ozp->z_gen;
198b5fca8f8Stomee 	nzp->z_sync_cnt = ozp->z_sync_cnt;
199b5fca8f8Stomee 	nzp->z_phys = ozp->z_phys;
200b5fca8f8Stomee 	nzp->z_dbuf = ozp->z_dbuf;
201b5fca8f8Stomee 
202b5fca8f8Stomee 	/* Update back pointers. */
203b5fca8f8Stomee 	(void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys,
204b5fca8f8Stomee 	    znode_evict_error);
205b5fca8f8Stomee 
206b5fca8f8Stomee 	/*
207b5fca8f8Stomee 	 * Invalidate the original znode by clearing fields that provide a
208b5fca8f8Stomee 	 * pointer back to the znode. Set the low bit of the vfs pointer to
209b5fca8f8Stomee 	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
210b5fca8f8Stomee 	 * subsequent callback.
211b5fca8f8Stomee 	 */
212b5fca8f8Stomee 	ozp->z_dbuf = NULL;
213b5fca8f8Stomee 	POINTER_INVALIDATE(&ozp->z_zfsvfs);
214b5fca8f8Stomee }
215b5fca8f8Stomee 
216b5fca8f8Stomee /*ARGSUSED*/
217b5fca8f8Stomee static kmem_cbrc_t
218b5fca8f8Stomee zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
219b5fca8f8Stomee {
220b5fca8f8Stomee 	znode_t *ozp = buf, *nzp = newbuf;
221b5fca8f8Stomee 	zfsvfs_t *zfsvfs;
222b5fca8f8Stomee 	vnode_t *vp;
223b5fca8f8Stomee 
224b5fca8f8Stomee 	/*
225b5fca8f8Stomee 	 * The znode is on the file system's list of known znodes if the vfs
226b5fca8f8Stomee 	 * pointer is valid. We set the low bit of the vfs pointer when freeing
227b5fca8f8Stomee 	 * the znode to invalidate it, and the memory patterns written by kmem
228b5fca8f8Stomee 	 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
229b5fca8f8Stomee 	 * created znode sets the vfs pointer last of all to indicate that the
230b5fca8f8Stomee 	 * znode is known and in a valid state to be moved by this function.
231b5fca8f8Stomee 	 */
232b5fca8f8Stomee 	zfsvfs = ozp->z_zfsvfs;
233b5fca8f8Stomee 	if (!POINTER_IS_VALID(zfsvfs)) {
234b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
235b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
236b5fca8f8Stomee 	}
237b5fca8f8Stomee 
238b5fca8f8Stomee 	/*
239*4e9583b2STom Erickson 	 * Close a small window in which it's possible that the filesystem could
240*4e9583b2STom Erickson 	 * be unmounted and freed, and zfsvfs, though valid in the previous
241*4e9583b2STom Erickson 	 * statement, could point to unrelated memory by the time we try to
242*4e9583b2STom Erickson 	 * prevent the filesystem from being unmounted.
243*4e9583b2STom Erickson 	 */
244*4e9583b2STom Erickson 	rw_enter(&zfsvfs_lock, RW_WRITER);
245*4e9583b2STom Erickson 	if (zfsvfs != ozp->z_zfsvfs) {
246*4e9583b2STom Erickson 		rw_exit(&zfsvfs_lock);
247*4e9583b2STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
248*4e9583b2STom Erickson 		return (KMEM_CBRC_DONT_KNOW);
249*4e9583b2STom Erickson 	}
250*4e9583b2STom Erickson 
251*4e9583b2STom Erickson 	/*
252*4e9583b2STom Erickson 	 * If the znode is still valid, then so is the file system. We know that
253*4e9583b2STom Erickson 	 * no valid file system can be freed while we hold zfsvfs_lock, so we
254*4e9583b2STom Erickson 	 * can safely ensure that the filesystem is not and will not be
255*4e9583b2STom Erickson 	 * unmounted. The next statement is equivalent to ZFS_ENTER().
256b5fca8f8Stomee 	 */
25714843421SMatthew Ahrens 	rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
25814843421SMatthew Ahrens 	if (zfsvfs->z_unmounted) {
25914843421SMatthew Ahrens 		ZFS_EXIT(zfsvfs);
260*4e9583b2STom Erickson 		rw_exit(&zfsvfs_lock);
261b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
262b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
263b5fca8f8Stomee 	}
264*4e9583b2STom Erickson 	rw_exit(&zfsvfs_lock);
265b5fca8f8Stomee 
266b5fca8f8Stomee 	mutex_enter(&zfsvfs->z_znodes_lock);
267b5fca8f8Stomee 	/*
268b5fca8f8Stomee 	 * Recheck the vfs pointer in case the znode was removed just before
269b5fca8f8Stomee 	 * acquiring the lock.
270b5fca8f8Stomee 	 */
271b5fca8f8Stomee 	if (zfsvfs != ozp->z_zfsvfs) {
272b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
273b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
274*4e9583b2STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
275b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
276b5fca8f8Stomee 	}
277b5fca8f8Stomee 
278b5fca8f8Stomee 	/*
279b5fca8f8Stomee 	 * At this point we know that as long as we hold z_znodes_lock, the
280b5fca8f8Stomee 	 * znode cannot be freed and fields within the znode can be safely
281a66b2b35STom Erickson 	 * accessed. Now, prevent a race with zfs_zget().
282b5fca8f8Stomee 	 */
283a66b2b35STom Erickson 	if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
284a66b2b35STom Erickson 		mutex_exit(&zfsvfs->z_znodes_lock);
285a66b2b35STom Erickson 		ZFS_EXIT(zfsvfs);
286a66b2b35STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
287a66b2b35STom Erickson 		return (KMEM_CBRC_LATER);
288a66b2b35STom Erickson 	}
289a66b2b35STom Erickson 
290b5fca8f8Stomee 	vp = ZTOV(ozp);
291b5fca8f8Stomee 	if (mutex_tryenter(&vp->v_lock) == 0) {
292a66b2b35STom Erickson 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
293b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
294b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
295b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
296b5fca8f8Stomee 		return (KMEM_CBRC_LATER);
297b5fca8f8Stomee 	}
298a66b2b35STom Erickson 
299b5fca8f8Stomee 	/* Only move znodes that are referenced _only_ by the DNLC. */
300b5fca8f8Stomee 	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
301b5fca8f8Stomee 		mutex_exit(&vp->v_lock);
302a66b2b35STom Erickson 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
303b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
304b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
305a66b2b35STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
306b5fca8f8Stomee 		return (KMEM_CBRC_LATER);
307b5fca8f8Stomee 	}
308b5fca8f8Stomee 
309b5fca8f8Stomee 	/*
310b5fca8f8Stomee 	 * The znode is known and in a valid state to move. We're holding the
311b5fca8f8Stomee 	 * locks needed to execute the critical section.
312b5fca8f8Stomee 	 */
313b5fca8f8Stomee 	zfs_znode_move_impl(ozp, nzp);
314b5fca8f8Stomee 	mutex_exit(&vp->v_lock);
315a66b2b35STom Erickson 	ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
316b5fca8f8Stomee 
317b5fca8f8Stomee 	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
318b5fca8f8Stomee 	mutex_exit(&zfsvfs->z_znodes_lock);
319b5fca8f8Stomee 	ZFS_EXIT(zfsvfs);
320b5fca8f8Stomee 
321b5fca8f8Stomee 	return (KMEM_CBRC_YES);
322fa9e4066Sahrens }
323fa9e4066Sahrens 
324fa9e4066Sahrens void
325fa9e4066Sahrens zfs_znode_init(void)
326fa9e4066Sahrens {
327fa9e4066Sahrens 	/*
328fa9e4066Sahrens 	 * Initialize zcache
329fa9e4066Sahrens 	 */
330*4e9583b2STom Erickson 	rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
331fa9e4066Sahrens 	ASSERT(znode_cache == NULL);
332fa9e4066Sahrens 	znode_cache = kmem_cache_create("zfs_znode_cache",
333fa9e4066Sahrens 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
334fa9e4066Sahrens 	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
335b5fca8f8Stomee 	kmem_cache_set_move(znode_cache, zfs_znode_move);
336fa9e4066Sahrens }
337fa9e4066Sahrens 
338fa9e4066Sahrens void
339fa9e4066Sahrens zfs_znode_fini(void)
340fa9e4066Sahrens {
341fa9e4066Sahrens 	/*
342fa9e4066Sahrens 	 * Cleanup vfs & vnode ops
343fa9e4066Sahrens 	 */
344fa9e4066Sahrens 	zfs_remove_op_tables();
345fa9e4066Sahrens 
346fa9e4066Sahrens 	/*
347fa9e4066Sahrens 	 * Cleanup zcache
348fa9e4066Sahrens 	 */
349fa9e4066Sahrens 	if (znode_cache)
350fa9e4066Sahrens 		kmem_cache_destroy(znode_cache);
351fa9e4066Sahrens 	znode_cache = NULL;
352*4e9583b2STom Erickson 	rw_destroy(&zfsvfs_lock);
353fa9e4066Sahrens }
354fa9e4066Sahrens 
355fa9e4066Sahrens struct vnodeops *zfs_dvnodeops;
356fa9e4066Sahrens struct vnodeops *zfs_fvnodeops;
357fa9e4066Sahrens struct vnodeops *zfs_symvnodeops;
358fa9e4066Sahrens struct vnodeops *zfs_xdvnodeops;
359fa9e4066Sahrens struct vnodeops *zfs_evnodeops;
360743a77edSAlan Wright struct vnodeops *zfs_sharevnodeops;
361fa9e4066Sahrens 
362fa9e4066Sahrens void
363fa9e4066Sahrens zfs_remove_op_tables()
364fa9e4066Sahrens {
365fa9e4066Sahrens 	/*
366fa9e4066Sahrens 	 * Remove vfs ops
367fa9e4066Sahrens 	 */
368fa9e4066Sahrens 	ASSERT(zfsfstype);
369fa9e4066Sahrens 	(void) vfs_freevfsops_by_type(zfsfstype);
370fa9e4066Sahrens 	zfsfstype = 0;
371fa9e4066Sahrens 
372fa9e4066Sahrens 	/*
373fa9e4066Sahrens 	 * Remove vnode ops
374fa9e4066Sahrens 	 */
375fa9e4066Sahrens 	if (zfs_dvnodeops)
376fa9e4066Sahrens 		vn_freevnodeops(zfs_dvnodeops);
377fa9e4066Sahrens 	if (zfs_fvnodeops)
378fa9e4066Sahrens 		vn_freevnodeops(zfs_fvnodeops);
379fa9e4066Sahrens 	if (zfs_symvnodeops)
380fa9e4066Sahrens 		vn_freevnodeops(zfs_symvnodeops);
381fa9e4066Sahrens 	if (zfs_xdvnodeops)
382fa9e4066Sahrens 		vn_freevnodeops(zfs_xdvnodeops);
383fa9e4066Sahrens 	if (zfs_evnodeops)
384fa9e4066Sahrens 		vn_freevnodeops(zfs_evnodeops);
385743a77edSAlan Wright 	if (zfs_sharevnodeops)
386743a77edSAlan Wright 		vn_freevnodeops(zfs_sharevnodeops);
387fa9e4066Sahrens 
388fa9e4066Sahrens 	zfs_dvnodeops = NULL;
389fa9e4066Sahrens 	zfs_fvnodeops = NULL;
390fa9e4066Sahrens 	zfs_symvnodeops = NULL;
391fa9e4066Sahrens 	zfs_xdvnodeops = NULL;
392fa9e4066Sahrens 	zfs_evnodeops = NULL;
393743a77edSAlan Wright 	zfs_sharevnodeops = NULL;
394fa9e4066Sahrens }
395fa9e4066Sahrens 
396fa9e4066Sahrens extern const fs_operation_def_t zfs_dvnodeops_template[];
397fa9e4066Sahrens extern const fs_operation_def_t zfs_fvnodeops_template[];
398fa9e4066Sahrens extern const fs_operation_def_t zfs_xdvnodeops_template[];
399fa9e4066Sahrens extern const fs_operation_def_t zfs_symvnodeops_template[];
400fa9e4066Sahrens extern const fs_operation_def_t zfs_evnodeops_template[];
401743a77edSAlan Wright extern const fs_operation_def_t zfs_sharevnodeops_template[];
402fa9e4066Sahrens 
403fa9e4066Sahrens int
404fa9e4066Sahrens zfs_create_op_tables()
405fa9e4066Sahrens {
406fa9e4066Sahrens 	int error;
407fa9e4066Sahrens 
408fa9e4066Sahrens 	/*
409fa9e4066Sahrens 	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
410fa9e4066Sahrens 	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
411fa9e4066Sahrens 	 * In this case we just return as the ops vectors are already set up.
412fa9e4066Sahrens 	 */
413fa9e4066Sahrens 	if (zfs_dvnodeops)
414fa9e4066Sahrens 		return (0);
415fa9e4066Sahrens 
416fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
417fa9e4066Sahrens 	    &zfs_dvnodeops);
418fa9e4066Sahrens 	if (error)
419fa9e4066Sahrens 		return (error);
420fa9e4066Sahrens 
421fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
422fa9e4066Sahrens 	    &zfs_fvnodeops);
423fa9e4066Sahrens 	if (error)
424fa9e4066Sahrens 		return (error);
425fa9e4066Sahrens 
426fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
427fa9e4066Sahrens 	    &zfs_symvnodeops);
428fa9e4066Sahrens 	if (error)
429fa9e4066Sahrens 		return (error);
430fa9e4066Sahrens 
431fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
432fa9e4066Sahrens 	    &zfs_xdvnodeops);
433fa9e4066Sahrens 	if (error)
434fa9e4066Sahrens 		return (error);
435fa9e4066Sahrens 
436fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
437fa9e4066Sahrens 	    &zfs_evnodeops);
438743a77edSAlan Wright 	if (error)
439743a77edSAlan Wright 		return (error);
440743a77edSAlan Wright 
441743a77edSAlan Wright 	error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
442743a77edSAlan Wright 	    &zfs_sharevnodeops);
443743a77edSAlan Wright 
444743a77edSAlan Wright 	return (error);
445743a77edSAlan Wright }
446743a77edSAlan Wright 
4479e1320c0SMark Shellenbaum int
448743a77edSAlan Wright zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
449743a77edSAlan Wright {
45089459e17SMark Shellenbaum 	zfs_acl_ids_t acl_ids;
451743a77edSAlan Wright 	vattr_t vattr;
452743a77edSAlan Wright 	znode_t *sharezp;
453743a77edSAlan Wright 	vnode_t *vp;
454743a77edSAlan Wright 	znode_t *zp;
455743a77edSAlan Wright 	int error;
456743a77edSAlan Wright 
457743a77edSAlan Wright 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
458743a77edSAlan Wright 	vattr.va_type = VDIR;
459743a77edSAlan Wright 	vattr.va_mode = S_IFDIR|0555;
460743a77edSAlan Wright 	vattr.va_uid = crgetuid(kcred);
461743a77edSAlan Wright 	vattr.va_gid = crgetgid(kcred);
462743a77edSAlan Wright 
463743a77edSAlan Wright 	sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
464743a77edSAlan Wright 	sharezp->z_unlinked = 0;
465743a77edSAlan Wright 	sharezp->z_atime_dirty = 0;
466743a77edSAlan Wright 	sharezp->z_zfsvfs = zfsvfs;
467743a77edSAlan Wright 
468743a77edSAlan Wright 	vp = ZTOV(sharezp);
469743a77edSAlan Wright 	vn_reinit(vp);
470743a77edSAlan Wright 	vp->v_type = VDIR;
471743a77edSAlan Wright 
47289459e17SMark Shellenbaum 	VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
47389459e17SMark Shellenbaum 	    kcred, NULL, &acl_ids));
474743a77edSAlan Wright 	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE,
47589459e17SMark Shellenbaum 	    &zp, 0, &acl_ids);
476743a77edSAlan Wright 	ASSERT3P(zp, ==, sharezp);
477743a77edSAlan Wright 	ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
478743a77edSAlan Wright 	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
479743a77edSAlan Wright 	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
480743a77edSAlan Wright 	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
481743a77edSAlan Wright 	zfsvfs->z_shares_dir = sharezp->z_id;
482743a77edSAlan Wright 
48389459e17SMark Shellenbaum 	zfs_acl_ids_free(&acl_ids);
484743a77edSAlan Wright 	ZTOV(sharezp)->v_count = 0;
485743a77edSAlan Wright 	dmu_buf_rele(sharezp->z_dbuf, NULL);
486743a77edSAlan Wright 	sharezp->z_dbuf = NULL;
487743a77edSAlan Wright 	kmem_cache_free(znode_cache, sharezp);
488fa9e4066Sahrens 
489fa9e4066Sahrens 	return (error);
490fa9e4066Sahrens }
491fa9e4066Sahrens 
49272fc53bcSmarks /*
49372fc53bcSmarks  * define a couple of values we need available
49472fc53bcSmarks  * for both 64 and 32 bit environments.
49572fc53bcSmarks  */
49672fc53bcSmarks #ifndef NBITSMINOR64
49772fc53bcSmarks #define	NBITSMINOR64	32
49872fc53bcSmarks #endif
49972fc53bcSmarks #ifndef MAXMAJ64
50072fc53bcSmarks #define	MAXMAJ64	0xffffffffUL
50172fc53bcSmarks #endif
50272fc53bcSmarks #ifndef	MAXMIN64
50372fc53bcSmarks #define	MAXMIN64	0xffffffffUL
50472fc53bcSmarks #endif
50572fc53bcSmarks 
50672fc53bcSmarks /*
50772fc53bcSmarks  * Create special expldev for ZFS private use.
50872fc53bcSmarks  * Can't use standard expldev since it doesn't do
50972fc53bcSmarks  * what we want.  The standard expldev() takes a
51072fc53bcSmarks  * dev32_t in LP64 and expands it to a long dev_t.
51172fc53bcSmarks  * We need an interface that takes a dev32_t in ILP32
51272fc53bcSmarks  * and expands it to a long dev_t.
51372fc53bcSmarks  */
51472fc53bcSmarks static uint64_t
51572fc53bcSmarks zfs_expldev(dev_t dev)
51672fc53bcSmarks {
51772fc53bcSmarks #ifndef _LP64
51872fc53bcSmarks 	major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
51972fc53bcSmarks 	return (((uint64_t)major << NBITSMINOR64) |
52072fc53bcSmarks 	    ((minor_t)dev & MAXMIN32));
52172fc53bcSmarks #else
52272fc53bcSmarks 	return (dev);
52372fc53bcSmarks #endif
52472fc53bcSmarks }
52572fc53bcSmarks 
52672fc53bcSmarks /*
52772fc53bcSmarks  * Special cmpldev for ZFS private use.
52872fc53bcSmarks  * Can't use standard cmpldev since it takes
52972fc53bcSmarks  * a long dev_t and compresses it to dev32_t in
53072fc53bcSmarks  * LP64.  We need to do a compaction of a long dev_t
53172fc53bcSmarks  * to a dev32_t in ILP32.
53272fc53bcSmarks  */
53372fc53bcSmarks dev_t
53472fc53bcSmarks zfs_cmpldev(uint64_t dev)
53572fc53bcSmarks {
53672fc53bcSmarks #ifndef _LP64
53772fc53bcSmarks 	minor_t minor = (minor_t)dev & MAXMIN64;
53872fc53bcSmarks 	major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
53972fc53bcSmarks 
54072fc53bcSmarks 	if (major > MAXMAJ32 || minor > MAXMIN32)
54172fc53bcSmarks 		return (NODEV32);
54272fc53bcSmarks 
54372fc53bcSmarks 	return (((dev32_t)major << NBITSMINOR32) | minor);
54472fc53bcSmarks #else
54572fc53bcSmarks 	return (dev);
54672fc53bcSmarks #endif
54772fc53bcSmarks }
54872fc53bcSmarks 
5494ccbb6e7Sahrens static void
550b5fca8f8Stomee zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
5514ccbb6e7Sahrens {
5524ccbb6e7Sahrens 	znode_t		*nzp;
5534ccbb6e7Sahrens 
554b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
555b5fca8f8Stomee 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
5564ccbb6e7Sahrens 
5574ccbb6e7Sahrens 	mutex_enter(&zp->z_lock);
5584ccbb6e7Sahrens 
5594ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
5604ccbb6e7Sahrens 	zp->z_dbuf = db;
561874395d5Smaybee 	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error);
5624ccbb6e7Sahrens 
5634ccbb6e7Sahrens 	/*
5644ccbb6e7Sahrens 	 * there should be no
5654ccbb6e7Sahrens 	 * concurrent zgets on this object.
5664ccbb6e7Sahrens 	 */
5674ccbb6e7Sahrens 	if (nzp != NULL)
568903a11ebSrh 		panic("existing znode %p for dbuf %p", (void *)nzp, (void *)db);
5694ccbb6e7Sahrens 
5704ccbb6e7Sahrens 	/*
5714ccbb6e7Sahrens 	 * Slap on VROOT if we are the root znode
5724ccbb6e7Sahrens 	 */
5734ccbb6e7Sahrens 	if (zp->z_id == zfsvfs->z_root)
5744ccbb6e7Sahrens 		ZTOV(zp)->v_flag |= VROOT;
5754ccbb6e7Sahrens 
5764ccbb6e7Sahrens 	mutex_exit(&zp->z_lock);
5774ccbb6e7Sahrens 	vn_exists(ZTOV(zp));
5784ccbb6e7Sahrens }
5794ccbb6e7Sahrens 
580874395d5Smaybee void
5814ccbb6e7Sahrens zfs_znode_dmu_fini(znode_t *zp)
5824ccbb6e7Sahrens {
5834ccbb6e7Sahrens 	dmu_buf_t *db = zp->z_dbuf;
584b5fca8f8Stomee 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
585b5fca8f8Stomee 	    zp->z_unlinked ||
586874395d5Smaybee 	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
5874ccbb6e7Sahrens 	ASSERT(zp->z_dbuf != NULL);
5884ccbb6e7Sahrens 	zp->z_dbuf = NULL;
589874395d5Smaybee 	VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL));
5904ccbb6e7Sahrens 	dmu_buf_rele(db, NULL);
5914ccbb6e7Sahrens }
5924ccbb6e7Sahrens 
593fa9e4066Sahrens /*
594fa9e4066Sahrens  * Construct a new znode/vnode and intialize.
595fa9e4066Sahrens  *
596fa9e4066Sahrens  * This does not do a call to dmu_set_user() that is
597fa9e4066Sahrens  * up to the caller to do, in case you don't want to
598fa9e4066Sahrens  * return the znode
599fa9e4066Sahrens  */
600ea8dc4b6Seschrock static znode_t *
6014ccbb6e7Sahrens zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
602fa9e4066Sahrens {
603fa9e4066Sahrens 	znode_t	*zp;
604fa9e4066Sahrens 	vnode_t *vp;
605fa9e4066Sahrens 
606fa9e4066Sahrens 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
607fa9e4066Sahrens 
608fa9e4066Sahrens 	ASSERT(zp->z_dirlocks == NULL);
6094ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
610b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
611fa9e4066Sahrens 
612b5fca8f8Stomee 	/*
613b5fca8f8Stomee 	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
614b5fca8f8Stomee 	 * the zfs_znode_move() callback.
615b5fca8f8Stomee 	 */
6164ccbb6e7Sahrens 	zp->z_phys = NULL;
617893a6d32Sahrens 	zp->z_unlinked = 0;
618fa9e4066Sahrens 	zp->z_atime_dirty = 0;
619fa9e4066Sahrens 	zp->z_mapcnt = 0;
620fa9e4066Sahrens 	zp->z_last_itx = 0;
6214ccbb6e7Sahrens 	zp->z_id = db->db_object;
622fa9e4066Sahrens 	zp->z_blksz = blksz;
623fa9e4066Sahrens 	zp->z_seq = 0x7A4653;
62467bd71c6Sperrin 	zp->z_sync_cnt = 0;
6254ccbb6e7Sahrens 
6264ccbb6e7Sahrens 	vp = ZTOV(zp);
6274ccbb6e7Sahrens 	vn_reinit(vp);
6284ccbb6e7Sahrens 
629b5fca8f8Stomee 	zfs_znode_dmu_init(zfsvfs, zp, db);
6304ccbb6e7Sahrens 
631f18faf3fSek 	zp->z_gen = zp->z_phys->zp_gen;
632fa9e4066Sahrens 
633fa9e4066Sahrens 	vp->v_vfsp = zfsvfs->z_parent->z_vfs;
634fa9e4066Sahrens 	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
635fa9e4066Sahrens 
636fa9e4066Sahrens 	switch (vp->v_type) {
637fa9e4066Sahrens 	case VDIR:
638fa9e4066Sahrens 		if (zp->z_phys->zp_flags & ZFS_XATTR) {
639fa9e4066Sahrens 			vn_setops(vp, zfs_xdvnodeops);
640fa9e4066Sahrens 			vp->v_flag |= V_XATTRDIR;
6414ccbb6e7Sahrens 		} else {
642fa9e4066Sahrens 			vn_setops(vp, zfs_dvnodeops);
6434ccbb6e7Sahrens 		}
6447f6e3e7dSperrin 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
645fa9e4066Sahrens 		break;
646fa9e4066Sahrens 	case VBLK:
647fa9e4066Sahrens 	case VCHR:
64872fc53bcSmarks 		vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
649fa9e4066Sahrens 		/*FALLTHROUGH*/
650fa9e4066Sahrens 	case VFIFO:
651fa9e4066Sahrens 	case VSOCK:
652fa9e4066Sahrens 	case VDOOR:
653fa9e4066Sahrens 		vn_setops(vp, zfs_fvnodeops);
654fa9e4066Sahrens 		break;
655fa9e4066Sahrens 	case VREG:
656fa9e4066Sahrens 		vp->v_flag |= VMODSORT;
657743a77edSAlan Wright 		if (zp->z_phys->zp_parent == zfsvfs->z_shares_dir)
658743a77edSAlan Wright 			vn_setops(vp, zfs_sharevnodeops);
659743a77edSAlan Wright 		else
660743a77edSAlan Wright 			vn_setops(vp, zfs_fvnodeops);
661fa9e4066Sahrens 		break;
662fa9e4066Sahrens 	case VLNK:
663fa9e4066Sahrens 		vn_setops(vp, zfs_symvnodeops);
664fa9e4066Sahrens 		break;
665fa9e4066Sahrens 	default:
666fa9e4066Sahrens 		vn_setops(vp, zfs_evnodeops);
667fa9e4066Sahrens 		break;
668fa9e4066Sahrens 	}
669fa9e4066Sahrens 
670b5fca8f8Stomee 	mutex_enter(&zfsvfs->z_znodes_lock);
671b5fca8f8Stomee 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
672b5fca8f8Stomee 	membar_producer();
673b5fca8f8Stomee 	/*
674b5fca8f8Stomee 	 * Everything else must be valid before assigning z_zfsvfs makes the
675b5fca8f8Stomee 	 * znode eligible for zfs_znode_move().
676b5fca8f8Stomee 	 */
677b5fca8f8Stomee 	zp->z_zfsvfs = zfsvfs;
678b5fca8f8Stomee 	mutex_exit(&zfsvfs->z_znodes_lock);
679b5fca8f8Stomee 
680874395d5Smaybee 	VFS_HOLD(zfsvfs->z_vfs);
681fa9e4066Sahrens 	return (zp);
682fa9e4066Sahrens }
683fa9e4066Sahrens 
684fa9e4066Sahrens /*
685fa9e4066Sahrens  * Create a new DMU object to hold a zfs znode.
686fa9e4066Sahrens  *
687fa9e4066Sahrens  *	IN:	dzp	- parent directory for new znode
688fa9e4066Sahrens  *		vap	- file attributes for new znode
689fa9e4066Sahrens  *		tx	- dmu transaction id for zap operations
690fa9e4066Sahrens  *		cr	- credentials of caller
691fa9e4066Sahrens  *		flag	- flags:
692fa9e4066Sahrens  *			  IS_ROOT_NODE	- new object will be root
693fa9e4066Sahrens  *			  IS_XATTR	- new object is an attribute
694fa9e4066Sahrens  *			  IS_REPLAY	- intent log replay
695da6c28aaSamw  *		bonuslen - length of bonus buffer
696da6c28aaSamw  *		setaclp  - File/Dir initial ACL
697da6c28aaSamw  *		fuidp	 - Tracks fuid allocation.
698fa9e4066Sahrens  *
6994ccbb6e7Sahrens  *	OUT:	zpp	- allocated znode
700fa9e4066Sahrens  *
701fa9e4066Sahrens  */
702fa9e4066Sahrens void
7034ccbb6e7Sahrens zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
70489459e17SMark Shellenbaum     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_ids_t *acl_ids)
705fa9e4066Sahrens {
7064ccbb6e7Sahrens 	dmu_buf_t	*db;
707fa9e4066Sahrens 	znode_phys_t	*pzp;
708fa9e4066Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
709fa9e4066Sahrens 	timestruc_t	now;
7104ccbb6e7Sahrens 	uint64_t	gen, obj;
711fa9e4066Sahrens 	int		err;
712fa9e4066Sahrens 
713fa9e4066Sahrens 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
714fa9e4066Sahrens 
7151209a471SNeil Perrin 	if (zfsvfs->z_replay) {
7164ccbb6e7Sahrens 		obj = vap->va_nodeid;
717fa9e4066Sahrens 		flag |= IS_REPLAY;
718fa9e4066Sahrens 		now = vap->va_ctime;		/* see zfs_replay_create() */
719fa9e4066Sahrens 		gen = vap->va_nblocks;		/* ditto */
720fa9e4066Sahrens 	} else {
7214ccbb6e7Sahrens 		obj = 0;
722fa9e4066Sahrens 		gethrestime(&now);
723fa9e4066Sahrens 		gen = dmu_tx_get_txg(tx);
724fa9e4066Sahrens 	}
725fa9e4066Sahrens 
726fa9e4066Sahrens 	/*
727fa9e4066Sahrens 	 * Create a new DMU object.
728fa9e4066Sahrens 	 */
729ea8dc4b6Seschrock 	/*
730ea8dc4b6Seschrock 	 * There's currently no mechanism for pre-reading the blocks that will
731ea8dc4b6Seschrock 	 * be to needed allocate a new object, so we accept the small chance
732ea8dc4b6Seschrock 	 * that there will be an i/o error and we will fail one of the
733ea8dc4b6Seschrock 	 * assertions below.
734ea8dc4b6Seschrock 	 */
735fa9e4066Sahrens 	if (vap->va_type == VDIR) {
736fa9e4066Sahrens 		if (flag & IS_REPLAY) {
7374ccbb6e7Sahrens 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
738da6c28aaSamw 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
739fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
740fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
741fa9e4066Sahrens 		} else {
7424ccbb6e7Sahrens 			obj = zap_create_norm(zfsvfs->z_os,
743da6c28aaSamw 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
744fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
745fa9e4066Sahrens 		}
746fa9e4066Sahrens 	} else {
747fa9e4066Sahrens 		if (flag & IS_REPLAY) {
7484ccbb6e7Sahrens 			err = dmu_object_claim(zfsvfs->z_os, obj,
749fa9e4066Sahrens 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
750fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
751fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
752fa9e4066Sahrens 		} else {
7534ccbb6e7Sahrens 			obj = dmu_object_alloc(zfsvfs->z_os,
754fa9e4066Sahrens 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
755fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
756fa9e4066Sahrens 		}
757fa9e4066Sahrens 	}
7584ccbb6e7Sahrens 	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
7594ccbb6e7Sahrens 	dmu_buf_will_dirty(db, tx);
760fa9e4066Sahrens 
761fa9e4066Sahrens 	/*
762fa9e4066Sahrens 	 * Initialize the znode physical data to zero.
763fa9e4066Sahrens 	 */
7644ccbb6e7Sahrens 	ASSERT(db->db_size >= sizeof (znode_phys_t));
7654ccbb6e7Sahrens 	bzero(db->db_data, db->db_size);
7664ccbb6e7Sahrens 	pzp = db->db_data;
767fa9e4066Sahrens 
768fa9e4066Sahrens 	/*
769fa9e4066Sahrens 	 * If this is the root, fix up the half-initialized parent pointer
770fa9e4066Sahrens 	 * to reference the just-allocated physical data area.
771fa9e4066Sahrens 	 */
772fa9e4066Sahrens 	if (flag & IS_ROOT_NODE) {
773874395d5Smaybee 		dzp->z_dbuf = db;
774fa9e4066Sahrens 		dzp->z_phys = pzp;
7754ccbb6e7Sahrens 		dzp->z_id = obj;
776fa9e4066Sahrens 	}
777fa9e4066Sahrens 
778fa9e4066Sahrens 	/*
779fa9e4066Sahrens 	 * If parent is an xattr, so am I.
780fa9e4066Sahrens 	 */
781fa9e4066Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR)
782fa9e4066Sahrens 		flag |= IS_XATTR;
783fa9e4066Sahrens 
784fa9e4066Sahrens 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
78572fc53bcSmarks 		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
786fa9e4066Sahrens 	}
787fa9e4066Sahrens 
788da6c28aaSamw 	if (zfsvfs->z_use_fuids)
789da6c28aaSamw 		pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
790da6c28aaSamw 
791fa9e4066Sahrens 	if (vap->va_type == VDIR) {
792fa9e4066Sahrens 		pzp->zp_size = 2;		/* contents ("." and "..") */
793fa9e4066Sahrens 		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
794fa9e4066Sahrens 	}
795fa9e4066Sahrens 
796fa9e4066Sahrens 	pzp->zp_parent = dzp->z_id;
797fa9e4066Sahrens 	if (flag & IS_XATTR)
798fa9e4066Sahrens 		pzp->zp_flags |= ZFS_XATTR;
799fa9e4066Sahrens 
800fa9e4066Sahrens 	pzp->zp_gen = gen;
801fa9e4066Sahrens 
802fa9e4066Sahrens 	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
803fa9e4066Sahrens 	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
804fa9e4066Sahrens 
805fa9e4066Sahrens 	if (vap->va_mask & AT_ATIME) {
806fa9e4066Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
807fa9e4066Sahrens 	} else {
808fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
809fa9e4066Sahrens 	}
810fa9e4066Sahrens 
811fa9e4066Sahrens 	if (vap->va_mask & AT_MTIME) {
812fa9e4066Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
813fa9e4066Sahrens 	} else {
814fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
815fa9e4066Sahrens 	}
816fa9e4066Sahrens 
817fa9e4066Sahrens 	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
818874395d5Smaybee 	if (!(flag & IS_ROOT_NODE)) {
819b5fca8f8Stomee 		ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
820874395d5Smaybee 		*zpp = zfs_znode_alloc(zfsvfs, db, 0);
821874395d5Smaybee 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
822874395d5Smaybee 	} else {
823874395d5Smaybee 		/*
824874395d5Smaybee 		 * If we are creating the root node, the "parent" we
825874395d5Smaybee 		 * passed in is the znode for the root.
826874395d5Smaybee 		 */
827874395d5Smaybee 		*zpp = dzp;
828874395d5Smaybee 	}
82989459e17SMark Shellenbaum 	pzp->zp_uid = acl_ids->z_fuid;
83089459e17SMark Shellenbaum 	pzp->zp_gid = acl_ids->z_fgid;
83189459e17SMark Shellenbaum 	pzp->zp_mode = acl_ids->z_mode;
83289459e17SMark Shellenbaum 	VERIFY(0 == zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
83389459e17SMark Shellenbaum 	if (vap->va_mask & AT_XVATTR)
83489459e17SMark Shellenbaum 		zfs_xvattr_set(*zpp, (xvattr_t *)vap);
835fa9e4066Sahrens }
836fa9e4066Sahrens 
837da6c28aaSamw void
838da6c28aaSamw zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
839da6c28aaSamw {
840da6c28aaSamw 	xoptattr_t *xoap;
841da6c28aaSamw 
842da6c28aaSamw 	xoap = xva_getxoptattr(xvap);
843da6c28aaSamw 	ASSERT(xoap);
844da6c28aaSamw 
845da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
846da6c28aaSamw 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
847da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_CREATETIME);
848da6c28aaSamw 	}
849da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
850da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
851da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_READONLY);
852da6c28aaSamw 	}
853da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
854da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
855da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_HIDDEN);
856da6c28aaSamw 	}
857da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
858da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
859da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_SYSTEM);
860da6c28aaSamw 	}
861da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
862da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
863da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
864da6c28aaSamw 	}
865da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
866da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
867da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
868da6c28aaSamw 	}
869da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
870da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
871da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
872da6c28aaSamw 	}
873da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
874da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
875da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
876da6c28aaSamw 	}
877da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
878da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
879da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_NODUMP);
880da6c28aaSamw 	}
881da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
882da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
883da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_OPAQUE);
884da6c28aaSamw 	}
885da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
886da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
887da6c28aaSamw 		    xoap->xoa_av_quarantined);
888da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
889da6c28aaSamw 	}
890da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
891da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
892da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
893da6c28aaSamw 	}
894da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
895da6c28aaSamw 		(void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
896da6c28aaSamw 		    sizeof (xoap->xoa_av_scanstamp));
897da6c28aaSamw 		zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
898da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
899da6c28aaSamw 	}
900da6c28aaSamw }
901da6c28aaSamw 
902fa9e4066Sahrens int
903fa9e4066Sahrens zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
904fa9e4066Sahrens {
905fa9e4066Sahrens 	dmu_object_info_t doi;
906fa9e4066Sahrens 	dmu_buf_t	*db;
907fa9e4066Sahrens 	znode_t		*zp;
908ea8dc4b6Seschrock 	int err;
909fa9e4066Sahrens 
910fa9e4066Sahrens 	*zpp = NULL;
911fa9e4066Sahrens 
912fa9e4066Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
913fa9e4066Sahrens 
914ea8dc4b6Seschrock 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
915ea8dc4b6Seschrock 	if (err) {
916fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
917ea8dc4b6Seschrock 		return (err);
918fa9e4066Sahrens 	}
919fa9e4066Sahrens 
920fa9e4066Sahrens 	dmu_object_info_from_db(db, &doi);
921fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
922fa9e4066Sahrens 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
923ea8dc4b6Seschrock 		dmu_buf_rele(db, NULL);
924fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
925fa9e4066Sahrens 		return (EINVAL);
926fa9e4066Sahrens 	}
927fa9e4066Sahrens 
928fa9e4066Sahrens 	zp = dmu_buf_get_user(db);
929fa9e4066Sahrens 	if (zp != NULL) {
930fa9e4066Sahrens 		mutex_enter(&zp->z_lock);
931fa9e4066Sahrens 
9324ccbb6e7Sahrens 		/*
9334ccbb6e7Sahrens 		 * Since we do immediate eviction of the z_dbuf, we
9344ccbb6e7Sahrens 		 * should never find a dbuf with a znode that doesn't
9354ccbb6e7Sahrens 		 * know about the dbuf.
9364ccbb6e7Sahrens 		 */
9374ccbb6e7Sahrens 		ASSERT3P(zp->z_dbuf, ==, db);
938fa9e4066Sahrens 		ASSERT3U(zp->z_id, ==, obj_num);
939893a6d32Sahrens 		if (zp->z_unlinked) {
9404ccbb6e7Sahrens 			err = ENOENT;
941fa9e4066Sahrens 		} else {
9424ccbb6e7Sahrens 			VN_HOLD(ZTOV(zp));
9434ccbb6e7Sahrens 			*zpp = zp;
9444ccbb6e7Sahrens 			err = 0;
945fa9e4066Sahrens 		}
9464ccbb6e7Sahrens 		dmu_buf_rele(db, NULL);
947fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
948ea8dc4b6Seschrock 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
9494ccbb6e7Sahrens 		return (err);
950fa9e4066Sahrens 	}
951fa9e4066Sahrens 
952fa9e4066Sahrens 	/*
953fa9e4066Sahrens 	 * Not found create new znode/vnode
954fa9e4066Sahrens 	 */
9554ccbb6e7Sahrens 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
956ea8dc4b6Seschrock 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
957fa9e4066Sahrens 	*zpp = zp;
958fa9e4066Sahrens 	return (0);
959fa9e4066Sahrens }
960fa9e4066Sahrens 
961f18faf3fSek int
962f18faf3fSek zfs_rezget(znode_t *zp)
963f18faf3fSek {
964f18faf3fSek 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
965f18faf3fSek 	dmu_object_info_t doi;
966f18faf3fSek 	dmu_buf_t *db;
967f18faf3fSek 	uint64_t obj_num = zp->z_id;
968f18faf3fSek 	int err;
969f18faf3fSek 
970f18faf3fSek 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
971f18faf3fSek 
972f18faf3fSek 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
973f18faf3fSek 	if (err) {
974f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
975f18faf3fSek 		return (err);
976f18faf3fSek 	}
977f18faf3fSek 
978f18faf3fSek 	dmu_object_info_from_db(db, &doi);
979f18faf3fSek 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
980f18faf3fSek 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
981f18faf3fSek 		dmu_buf_rele(db, NULL);
982f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
983f18faf3fSek 		return (EINVAL);
984f18faf3fSek 	}
985f18faf3fSek 
986f18faf3fSek 	if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
987f18faf3fSek 		dmu_buf_rele(db, NULL);
988f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
989f18faf3fSek 		return (EIO);
990f18faf3fSek 	}
991f18faf3fSek 
992b5fca8f8Stomee 	zfs_znode_dmu_init(zfsvfs, zp, db);
993f18faf3fSek 	zp->z_unlinked = (zp->z_phys->zp_links == 0);
9946166ad1cSek 	zp->z_blksz = doi.doi_data_block_size;
995f18faf3fSek 
996f18faf3fSek 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
997f18faf3fSek 
998f18faf3fSek 	return (0);
999f18faf3fSek }
1000f18faf3fSek 
1001fa9e4066Sahrens void
1002fa9e4066Sahrens zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1003fa9e4066Sahrens {
1004fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1005cdb0ab79Smaybee 	objset_t *os = zfsvfs->z_os;
10064ccbb6e7Sahrens 	uint64_t obj = zp->z_id;
1007cdb0ab79Smaybee 	uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
1008fa9e4066Sahrens 
10094ccbb6e7Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1010cdb0ab79Smaybee 	if (acl_obj)
1011cdb0ab79Smaybee 		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1012cdb0ab79Smaybee 	VERIFY(0 == dmu_object_free(os, obj, tx));
10134ccbb6e7Sahrens 	zfs_znode_dmu_fini(zp);
10144ccbb6e7Sahrens 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1015874395d5Smaybee 	zfs_znode_free(zp);
1016fa9e4066Sahrens }
1017fa9e4066Sahrens 
1018fa9e4066Sahrens void
1019fa9e4066Sahrens zfs_zinactive(znode_t *zp)
1020fa9e4066Sahrens {
1021fa9e4066Sahrens 	vnode_t	*vp = ZTOV(zp);
1022fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1023fa9e4066Sahrens 	uint64_t z_id = zp->z_id;
1024fa9e4066Sahrens 
10254ccbb6e7Sahrens 	ASSERT(zp->z_dbuf && zp->z_phys);
1026fa9e4066Sahrens 
1027fa9e4066Sahrens 	/*
1028fa9e4066Sahrens 	 * Don't allow a zfs_zget() while were trying to release this znode
1029fa9e4066Sahrens 	 */
1030fa9e4066Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1031fa9e4066Sahrens 
1032fa9e4066Sahrens 	mutex_enter(&zp->z_lock);
1033fa9e4066Sahrens 	mutex_enter(&vp->v_lock);
1034fa9e4066Sahrens 	vp->v_count--;
1035fa9e4066Sahrens 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1036fa9e4066Sahrens 		/*
1037fa9e4066Sahrens 		 * If the hold count is greater than zero, somebody has
1038fa9e4066Sahrens 		 * obtained a new reference on this znode while we were
1039fa9e4066Sahrens 		 * processing it here, so we are done.  If we still have
1040fa9e4066Sahrens 		 * mapped pages then we are also done, since we don't
1041fa9e4066Sahrens 		 * want to inactivate the znode until the pages get pushed.
1042fa9e4066Sahrens 		 *
1043fa9e4066Sahrens 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1044fa9e4066Sahrens 		 * this seems like it would leave the znode hanging with
1045fa9e4066Sahrens 		 * no chance to go inactive...
1046fa9e4066Sahrens 		 */
1047fa9e4066Sahrens 		mutex_exit(&vp->v_lock);
1048fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1049fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1050fa9e4066Sahrens 		return;
1051fa9e4066Sahrens 	}
1052fa9e4066Sahrens 	mutex_exit(&vp->v_lock);
1053fa9e4066Sahrens 
1054fa9e4066Sahrens 	/*
1055fa9e4066Sahrens 	 * If this was the last reference to a file with no links,
1056fa9e4066Sahrens 	 * remove the file from the file system.
1057fa9e4066Sahrens 	 */
1058893a6d32Sahrens 	if (zp->z_unlinked) {
1059fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1060fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1061893a6d32Sahrens 		zfs_rmnode(zp);
1062fa9e4066Sahrens 		return;
1063fa9e4066Sahrens 	}
1064fa9e4066Sahrens 	mutex_exit(&zp->z_lock);
10654ccbb6e7Sahrens 	zfs_znode_dmu_fini(zp);
1066fa9e4066Sahrens 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1067874395d5Smaybee 	zfs_znode_free(zp);
1068fa9e4066Sahrens }
1069fa9e4066Sahrens 
1070fa9e4066Sahrens void
1071fa9e4066Sahrens zfs_znode_free(znode_t *zp)
1072fa9e4066Sahrens {
1073fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1074fa9e4066Sahrens 
1075874395d5Smaybee 	vn_invalid(ZTOV(zp));
1076874395d5Smaybee 
1077b5fca8f8Stomee 	ASSERT(ZTOV(zp)->v_count == 0);
1078b5fca8f8Stomee 
1079fa9e4066Sahrens 	mutex_enter(&zfsvfs->z_znodes_lock);
1080b5fca8f8Stomee 	POINTER_INVALIDATE(&zp->z_zfsvfs);
1081fa9e4066Sahrens 	list_remove(&zfsvfs->z_all_znodes, zp);
1082fa9e4066Sahrens 	mutex_exit(&zfsvfs->z_znodes_lock);
1083fa9e4066Sahrens 
1084fa9e4066Sahrens 	kmem_cache_free(znode_cache, zp);
1085874395d5Smaybee 
1086874395d5Smaybee 	VFS_RELE(zfsvfs->z_vfs);
1087fa9e4066Sahrens }
1088fa9e4066Sahrens 
1089fa9e4066Sahrens void
1090fa9e4066Sahrens zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1091fa9e4066Sahrens {
1092fa9e4066Sahrens 	timestruc_t	now;
1093fa9e4066Sahrens 
1094fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zp->z_lock));
1095fa9e4066Sahrens 
1096fa9e4066Sahrens 	gethrestime(&now);
1097fa9e4066Sahrens 
1098fa9e4066Sahrens 	if (tx) {
1099fa9e4066Sahrens 		dmu_buf_will_dirty(zp->z_dbuf, tx);
1100fa9e4066Sahrens 		zp->z_atime_dirty = 0;
1101fa9e4066Sahrens 		zp->z_seq++;
1102fa9e4066Sahrens 	} else {
1103fa9e4066Sahrens 		zp->z_atime_dirty = 1;
1104fa9e4066Sahrens 	}
1105fa9e4066Sahrens 
1106fa9e4066Sahrens 	if (flag & AT_ATIME)
1107fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
1108fa9e4066Sahrens 
1109da6c28aaSamw 	if (flag & AT_MTIME) {
1110fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
1111da6c28aaSamw 		if (zp->z_zfsvfs->z_use_fuids)
1112da6c28aaSamw 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
1113da6c28aaSamw 	}
1114fa9e4066Sahrens 
1115da6c28aaSamw 	if (flag & AT_CTIME) {
1116fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
1117da6c28aaSamw 		if (zp->z_zfsvfs->z_use_fuids)
1118da6c28aaSamw 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
1119da6c28aaSamw 	}
1120fa9e4066Sahrens }
1121fa9e4066Sahrens 
1122fa9e4066Sahrens /*
1123fa9e4066Sahrens  * Update the requested znode timestamps with the current time.
1124fa9e4066Sahrens  * If we are in a transaction, then go ahead and mark the znode
1125fa9e4066Sahrens  * dirty in the transaction so the timestamps will go to disk.
1126fa9e4066Sahrens  * Otherwise, we will get pushed next time the znode is updated
1127fa9e4066Sahrens  * in a transaction, or when this znode eventually goes inactive.
1128fa9e4066Sahrens  *
1129fa9e4066Sahrens  * Why is this OK?
1130fa9e4066Sahrens  *  1 - Only the ACCESS time is ever updated outside of a transaction.
1131fa9e4066Sahrens  *  2 - Multiple consecutive updates will be collapsed into a single
1132fa9e4066Sahrens  *	znode update by the transaction grouping semantics of the DMU.
1133fa9e4066Sahrens  */
1134fa9e4066Sahrens void
1135fa9e4066Sahrens zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1136fa9e4066Sahrens {
1137fa9e4066Sahrens 	mutex_enter(&zp->z_lock);
1138fa9e4066Sahrens 	zfs_time_stamper_locked(zp, flag, tx);
1139fa9e4066Sahrens 	mutex_exit(&zp->z_lock);
1140fa9e4066Sahrens }
1141fa9e4066Sahrens 
1142fa9e4066Sahrens /*
1143104e2ed7Sperrin  * Grow the block size for a file.
1144fa9e4066Sahrens  *
1145fa9e4066Sahrens  *	IN:	zp	- znode of file to free data in.
1146fa9e4066Sahrens  *		size	- requested block size
1147fa9e4066Sahrens  *		tx	- open transaction.
1148fa9e4066Sahrens  *
1149fa9e4066Sahrens  * NOTE: this function assumes that the znode is write locked.
1150fa9e4066Sahrens  */
1151104e2ed7Sperrin void
1152fa9e4066Sahrens zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1153fa9e4066Sahrens {
1154fa9e4066Sahrens 	int		error;
1155fa9e4066Sahrens 	u_longlong_t	dummy;
1156fa9e4066Sahrens 
1157fa9e4066Sahrens 	if (size <= zp->z_blksz)
1158104e2ed7Sperrin 		return;
1159fa9e4066Sahrens 	/*
1160fa9e4066Sahrens 	 * If the file size is already greater than the current blocksize,
1161fa9e4066Sahrens 	 * we will not grow.  If there is more than one block in a file,
1162fa9e4066Sahrens 	 * the blocksize cannot change.
1163fa9e4066Sahrens 	 */
1164fa9e4066Sahrens 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1165104e2ed7Sperrin 		return;
1166fa9e4066Sahrens 
1167fa9e4066Sahrens 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1168fa9e4066Sahrens 	    size, 0, tx);
1169fa9e4066Sahrens 	if (error == ENOTSUP)
1170104e2ed7Sperrin 		return;
1171fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
1172fa9e4066Sahrens 
1173fa9e4066Sahrens 	/* What blocksize did we actually get? */
1174fa9e4066Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1175fa9e4066Sahrens }
1176fa9e4066Sahrens 
1177fa9e4066Sahrens /*
1178fa9e4066Sahrens  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1179fa9e4066Sahrens  * be calling back into the fs for a putpage().  E.g.: when truncating
1180fa9e4066Sahrens  * a file, the pages being "thrown away* don't need to be written out.
1181fa9e4066Sahrens  */
1182fa9e4066Sahrens /* ARGSUSED */
1183fa9e4066Sahrens static int
1184fa9e4066Sahrens zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1185fa9e4066Sahrens     int flags, cred_t *cr)
1186fa9e4066Sahrens {
1187fa9e4066Sahrens 	ASSERT(0);
1188fa9e4066Sahrens 	return (0);
1189fa9e4066Sahrens }
1190fa9e4066Sahrens 
1191fa9e4066Sahrens /*
1192cdb0ab79Smaybee  * Increase the file length
1193fa9e4066Sahrens  *
1194fa9e4066Sahrens  *	IN:	zp	- znode of file to free data in.
1195cdb0ab79Smaybee  *		end	- new end-of-file
1196fa9e4066Sahrens  *
1197fa9e4066Sahrens  * 	RETURN:	0 if success
1198fa9e4066Sahrens  *		error code if failure
1199fa9e4066Sahrens  */
1200cdb0ab79Smaybee static int
1201cdb0ab79Smaybee zfs_extend(znode_t *zp, uint64_t end)
1202fa9e4066Sahrens {
12035730cc9aSmaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1204cdb0ab79Smaybee 	dmu_tx_t *tx;
12055730cc9aSmaybee 	rl_t *rl;
1206cdb0ab79Smaybee 	uint64_t newblksz;
1207104e2ed7Sperrin 	int error;
1208fa9e4066Sahrens 
12095730cc9aSmaybee 	/*
1210cdb0ab79Smaybee 	 * We will change zp_size, lock the whole file.
12115730cc9aSmaybee 	 */
1212cdb0ab79Smaybee 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
12135730cc9aSmaybee 
1214fa9e4066Sahrens 	/*
1215fa9e4066Sahrens 	 * Nothing to do if file already at desired length.
1216fa9e4066Sahrens 	 */
1217cdb0ab79Smaybee 	if (end <= zp->z_phys->zp_size) {
1218c5c6ffa0Smaybee 		zfs_range_unlock(rl);
1219fa9e4066Sahrens 		return (0);
1220fa9e4066Sahrens 	}
1221cdb0ab79Smaybee top:
12225730cc9aSmaybee 	tx = dmu_tx_create(zfsvfs->z_os);
12235730cc9aSmaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
1224cdb0ab79Smaybee 	if (end > zp->z_blksz &&
12255730cc9aSmaybee 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1226fa9e4066Sahrens 		/*
1227fa9e4066Sahrens 		 * We are growing the file past the current block size.
1228fa9e4066Sahrens 		 */
1229fa9e4066Sahrens 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1230fa9e4066Sahrens 			ASSERT(!ISP2(zp->z_blksz));
1231cdb0ab79Smaybee 			newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1232fa9e4066Sahrens 		} else {
1233cdb0ab79Smaybee 			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1234fa9e4066Sahrens 		}
1235cdb0ab79Smaybee 		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1236cdb0ab79Smaybee 	} else {
1237cdb0ab79Smaybee 		newblksz = 0;
12385730cc9aSmaybee 	}
12395730cc9aSmaybee 
12401209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
12415730cc9aSmaybee 	if (error) {
12421209a471SNeil Perrin 		if (error == ERESTART) {
12438a2f1b91Sahrens 			dmu_tx_wait(tx);
1244cdb0ab79Smaybee 			dmu_tx_abort(tx);
1245cdb0ab79Smaybee 			goto top;
1246cdb0ab79Smaybee 		}
12475730cc9aSmaybee 		dmu_tx_abort(tx);
1248c5c6ffa0Smaybee 		zfs_range_unlock(rl);
12495730cc9aSmaybee 		return (error);
1250fa9e4066Sahrens 	}
1251cdb0ab79Smaybee 	dmu_buf_will_dirty(zp->z_dbuf, tx);
12525730cc9aSmaybee 
1253cdb0ab79Smaybee 	if (newblksz)
1254cdb0ab79Smaybee 		zfs_grow_blocksize(zp, newblksz, tx);
12555730cc9aSmaybee 
1256cdb0ab79Smaybee 	zp->z_phys->zp_size = end;
12575730cc9aSmaybee 
1258cdb0ab79Smaybee 	zfs_range_unlock(rl);
12595730cc9aSmaybee 
1260cdb0ab79Smaybee 	dmu_tx_commit(tx);
12615730cc9aSmaybee 
1262cdb0ab79Smaybee 	return (0);
1263cdb0ab79Smaybee }
1264cdb0ab79Smaybee 
1265cdb0ab79Smaybee /*
1266cdb0ab79Smaybee  * Free space in a file.
1267cdb0ab79Smaybee  *
1268cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1269cdb0ab79Smaybee  *		off	- start of section to free.
1270cdb0ab79Smaybee  *		len	- length of section to free.
1271cdb0ab79Smaybee  *
1272cdb0ab79Smaybee  * 	RETURN:	0 if success
1273cdb0ab79Smaybee  *		error code if failure
1274cdb0ab79Smaybee  */
1275cdb0ab79Smaybee static int
1276cdb0ab79Smaybee zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1277cdb0ab79Smaybee {
1278cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1279cdb0ab79Smaybee 	rl_t *rl;
1280cdb0ab79Smaybee 	int error;
1281cdb0ab79Smaybee 
1282cdb0ab79Smaybee 	/*
1283cdb0ab79Smaybee 	 * Lock the range being freed.
1284cdb0ab79Smaybee 	 */
1285cdb0ab79Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1286cdb0ab79Smaybee 
1287cdb0ab79Smaybee 	/*
1288cdb0ab79Smaybee 	 * Nothing to do if file already at desired length.
1289cdb0ab79Smaybee 	 */
1290cdb0ab79Smaybee 	if (off >= zp->z_phys->zp_size) {
1291cdb0ab79Smaybee 		zfs_range_unlock(rl);
1292cdb0ab79Smaybee 		return (0);
12935730cc9aSmaybee 	}
12945730cc9aSmaybee 
1295cdb0ab79Smaybee 	if (off + len > zp->z_phys->zp_size)
1296cdb0ab79Smaybee 		len = zp->z_phys->zp_size - off;
1297cdb0ab79Smaybee 
1298cdb0ab79Smaybee 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1299cdb0ab79Smaybee 
1300c5c6ffa0Smaybee 	zfs_range_unlock(rl);
13015730cc9aSmaybee 
1302cdb0ab79Smaybee 	return (error);
1303cdb0ab79Smaybee }
1304cdb0ab79Smaybee 
1305cdb0ab79Smaybee /*
1306cdb0ab79Smaybee  * Truncate a file
1307cdb0ab79Smaybee  *
1308cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1309cdb0ab79Smaybee  *		end	- new end-of-file.
1310cdb0ab79Smaybee  *
1311cdb0ab79Smaybee  * 	RETURN:	0 if success
1312cdb0ab79Smaybee  *		error code if failure
1313cdb0ab79Smaybee  */
1314cdb0ab79Smaybee static int
1315cdb0ab79Smaybee zfs_trunc(znode_t *zp, uint64_t end)
1316cdb0ab79Smaybee {
1317cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1318cdb0ab79Smaybee 	vnode_t *vp = ZTOV(zp);
1319cdb0ab79Smaybee 	dmu_tx_t *tx;
1320cdb0ab79Smaybee 	rl_t *rl;
1321cdb0ab79Smaybee 	int error;
1322cdb0ab79Smaybee 
1323cdb0ab79Smaybee 	/*
1324cdb0ab79Smaybee 	 * We will change zp_size, lock the whole file.
1325cdb0ab79Smaybee 	 */
1326cdb0ab79Smaybee 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1327cdb0ab79Smaybee 
1328cdb0ab79Smaybee 	/*
1329cdb0ab79Smaybee 	 * Nothing to do if file already at desired length.
1330cdb0ab79Smaybee 	 */
1331cdb0ab79Smaybee 	if (end >= zp->z_phys->zp_size) {
1332cdb0ab79Smaybee 		zfs_range_unlock(rl);
1333cdb0ab79Smaybee 		return (0);
1334cdb0ab79Smaybee 	}
1335cdb0ab79Smaybee 
1336cdb0ab79Smaybee 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1337cdb0ab79Smaybee 	if (error) {
1338cdb0ab79Smaybee 		zfs_range_unlock(rl);
1339cdb0ab79Smaybee 		return (error);
1340cdb0ab79Smaybee 	}
1341cdb0ab79Smaybee top:
1342cdb0ab79Smaybee 	tx = dmu_tx_create(zfsvfs->z_os);
1343cdb0ab79Smaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
13441209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1345cdb0ab79Smaybee 	if (error) {
13461209a471SNeil Perrin 		if (error == ERESTART) {
1347cdb0ab79Smaybee 			dmu_tx_wait(tx);
1348cdb0ab79Smaybee 			dmu_tx_abort(tx);
1349cdb0ab79Smaybee 			goto top;
1350cdb0ab79Smaybee 		}
1351cdb0ab79Smaybee 		dmu_tx_abort(tx);
1352cdb0ab79Smaybee 		zfs_range_unlock(rl);
1353cdb0ab79Smaybee 		return (error);
1354cdb0ab79Smaybee 	}
1355cdb0ab79Smaybee 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1356cdb0ab79Smaybee 
1357cdb0ab79Smaybee 	zp->z_phys->zp_size = end;
1358cdb0ab79Smaybee 
13595730cc9aSmaybee 	dmu_tx_commit(tx);
13605730cc9aSmaybee 
1361fa9e4066Sahrens 	/*
13625730cc9aSmaybee 	 * Clear any mapped pages in the truncated region.  This has to
13635730cc9aSmaybee 	 * happen outside of the transaction to avoid the possibility of
13645730cc9aSmaybee 	 * a deadlock with someone trying to push a page that we are
13655730cc9aSmaybee 	 * about to invalidate.
1366fa9e4066Sahrens 	 */
1367cdb0ab79Smaybee 	if (vn_has_cached_data(vp)) {
1368fa9e4066Sahrens 		page_t *pp;
1369cdb0ab79Smaybee 		uint64_t start = end & PAGEMASK;
1370cdb0ab79Smaybee 		int poff = end & PAGEOFFSET;
1371fa9e4066Sahrens 
13725730cc9aSmaybee 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1373fa9e4066Sahrens 			/*
1374fa9e4066Sahrens 			 * We need to zero a partial page.
1375fa9e4066Sahrens 			 */
13765730cc9aSmaybee 			pagezero(pp, poff, PAGESIZE - poff);
1377fa9e4066Sahrens 			start += PAGESIZE;
1378fa9e4066Sahrens 			page_unlock(pp);
1379fa9e4066Sahrens 		}
1380fa9e4066Sahrens 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
13815730cc9aSmaybee 		    B_INVAL | B_TRUNC, NULL);
1382fa9e4066Sahrens 		ASSERT(error == 0);
1383fa9e4066Sahrens 	}
1384ac05c741SMark Maybee 
1385ac05c741SMark Maybee 	zfs_range_unlock(rl);
1386fa9e4066Sahrens 
1387fa9e4066Sahrens 	return (0);
1388fa9e4066Sahrens }
1389fa9e4066Sahrens 
1390cdb0ab79Smaybee /*
1391cdb0ab79Smaybee  * Free space in a file
1392cdb0ab79Smaybee  *
1393cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1394cdb0ab79Smaybee  *		off	- start of range
1395cdb0ab79Smaybee  *		len	- end of range (0 => EOF)
1396cdb0ab79Smaybee  *		flag	- current file open mode flags.
1397cdb0ab79Smaybee  *		log	- TRUE if this action should be logged
1398cdb0ab79Smaybee  *
1399cdb0ab79Smaybee  * 	RETURN:	0 if success
1400cdb0ab79Smaybee  *		error code if failure
1401cdb0ab79Smaybee  */
1402cdb0ab79Smaybee int
1403cdb0ab79Smaybee zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1404cdb0ab79Smaybee {
1405cdb0ab79Smaybee 	vnode_t *vp = ZTOV(zp);
1406cdb0ab79Smaybee 	dmu_tx_t *tx;
1407cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1408cdb0ab79Smaybee 	zilog_t *zilog = zfsvfs->z_log;
1409cdb0ab79Smaybee 	int error;
1410cdb0ab79Smaybee 
1411cdb0ab79Smaybee 	if (off > zp->z_phys->zp_size) {
1412cdb0ab79Smaybee 		error =  zfs_extend(zp, off+len);
1413cdb0ab79Smaybee 		if (error == 0 && log)
1414cdb0ab79Smaybee 			goto log;
1415cdb0ab79Smaybee 		else
1416cdb0ab79Smaybee 			return (error);
1417cdb0ab79Smaybee 	}
1418cdb0ab79Smaybee 
1419cdb0ab79Smaybee 	/*
1420cdb0ab79Smaybee 	 * Check for any locks in the region to be freed.
1421cdb0ab79Smaybee 	 */
1422cdb0ab79Smaybee 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
1423cdb0ab79Smaybee 		uint64_t length = (len ? len : zp->z_phys->zp_size - off);
1424cdb0ab79Smaybee 		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1425cdb0ab79Smaybee 			return (error);
1426cdb0ab79Smaybee 	}
1427cdb0ab79Smaybee 
1428cdb0ab79Smaybee 	if (len == 0) {
1429cdb0ab79Smaybee 		error = zfs_trunc(zp, off);
1430cdb0ab79Smaybee 	} else {
1431cdb0ab79Smaybee 		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1432cdb0ab79Smaybee 		    off + len > zp->z_phys->zp_size)
1433cdb0ab79Smaybee 			error = zfs_extend(zp, off+len);
1434cdb0ab79Smaybee 	}
1435cdb0ab79Smaybee 	if (error || !log)
1436cdb0ab79Smaybee 		return (error);
1437cdb0ab79Smaybee log:
1438cdb0ab79Smaybee 	tx = dmu_tx_create(zfsvfs->z_os);
1439cdb0ab79Smaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
14401209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1441cdb0ab79Smaybee 	if (error) {
14421209a471SNeil Perrin 		if (error == ERESTART) {
1443cdb0ab79Smaybee 			dmu_tx_wait(tx);
1444cdb0ab79Smaybee 			dmu_tx_abort(tx);
1445cdb0ab79Smaybee 			goto log;
1446cdb0ab79Smaybee 		}
1447cdb0ab79Smaybee 		dmu_tx_abort(tx);
1448cdb0ab79Smaybee 		return (error);
1449cdb0ab79Smaybee 	}
1450cdb0ab79Smaybee 
1451cdb0ab79Smaybee 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1452cdb0ab79Smaybee 	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1453cdb0ab79Smaybee 
1454cdb0ab79Smaybee 	dmu_tx_commit(tx);
1455cdb0ab79Smaybee 	return (0);
1456cdb0ab79Smaybee }
1457cdb0ab79Smaybee 
1458fa9e4066Sahrens void
1459de8267e0Stimh zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1460fa9e4066Sahrens {
1461fa9e4066Sahrens 	zfsvfs_t	zfsvfs;
146214843421SMatthew Ahrens 	uint64_t	moid, obj, version;
1463de8267e0Stimh 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1464de8267e0Stimh 	uint64_t	norm = 0;
1465de8267e0Stimh 	nvpair_t	*elem;
1466fa9e4066Sahrens 	int		error;
1467fa9e4066Sahrens 	znode_t		*rootzp = NULL;
1468fa9e4066Sahrens 	vnode_t		*vp;
1469fa9e4066Sahrens 	vattr_t		vattr;
14704ccbb6e7Sahrens 	znode_t		*zp;
147189459e17SMark Shellenbaum 	zfs_acl_ids_t	acl_ids;
1472fa9e4066Sahrens 
1473fa9e4066Sahrens 	/*
1474fa9e4066Sahrens 	 * First attempt to create master node.
1475fa9e4066Sahrens 	 */
1476ea8dc4b6Seschrock 	/*
1477ea8dc4b6Seschrock 	 * In an empty objset, there are no blocks to read and thus
1478ea8dc4b6Seschrock 	 * there can be no i/o errors (which we assert below).
1479ea8dc4b6Seschrock 	 */
1480fa9e4066Sahrens 	moid = MASTER_NODE_OBJ;
1481fa9e4066Sahrens 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1482fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
1483fa9e4066Sahrens 	ASSERT(error == 0);
1484fa9e4066Sahrens 
1485fa9e4066Sahrens 	/*
1486fa9e4066Sahrens 	 * Set starting attributes.
1487fa9e4066Sahrens 	 */
148814843421SMatthew Ahrens 	if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_USERSPACE)
1489088f3894Sahrens 		version = ZPL_VERSION;
149014843421SMatthew Ahrens 	else if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
149114843421SMatthew Ahrens 		version = ZPL_VERSION_USERSPACE - 1;
1492088f3894Sahrens 	else
1493088f3894Sahrens 		version = ZPL_VERSION_FUID - 1;
1494de8267e0Stimh 	elem = NULL;
1495de8267e0Stimh 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1496de8267e0Stimh 		/* For the moment we expect all zpl props to be uint64_ts */
1497de8267e0Stimh 		uint64_t val;
1498de8267e0Stimh 		char *name;
1499de8267e0Stimh 
1500de8267e0Stimh 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1501b3d911cbStimh 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1502de8267e0Stimh 		name = nvpair_name(elem);
1503de8267e0Stimh 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
150414843421SMatthew Ahrens 			if (val < version)
150514843421SMatthew Ahrens 				version = val;
1506de8267e0Stimh 		} else {
1507de8267e0Stimh 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1508de8267e0Stimh 		}
1509de8267e0Stimh 		ASSERT(error == 0);
1510de8267e0Stimh 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1511de8267e0Stimh 			norm = val;
1512de8267e0Stimh 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1513de8267e0Stimh 			sense = val;
1514de8267e0Stimh 	}
1515de8267e0Stimh 	ASSERT(version != 0);
151614843421SMatthew Ahrens 	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1517fa9e4066Sahrens 
1518fa9e4066Sahrens 	/*
1519fa9e4066Sahrens 	 * Create a delete queue.
1520fa9e4066Sahrens 	 */
152114843421SMatthew Ahrens 	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1522fa9e4066Sahrens 
152314843421SMatthew Ahrens 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1524fa9e4066Sahrens 	ASSERT(error == 0);
1525fa9e4066Sahrens 
1526fa9e4066Sahrens 	/*
1527fa9e4066Sahrens 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1528fa9e4066Sahrens 	 * to allow zfs_mknode to work.
1529fa9e4066Sahrens 	 */
1530fa9e4066Sahrens 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1531fa9e4066Sahrens 	vattr.va_type = VDIR;
1532fa9e4066Sahrens 	vattr.va_mode = S_IFDIR|0755;
1533ecd6cf80Smarks 	vattr.va_uid = crgetuid(cr);
1534ecd6cf80Smarks 	vattr.va_gid = crgetgid(cr);
1535fa9e4066Sahrens 
1536fa9e4066Sahrens 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1537893a6d32Sahrens 	rootzp->z_unlinked = 0;
1538fa9e4066Sahrens 	rootzp->z_atime_dirty = 0;
1539fa9e4066Sahrens 
1540fa9e4066Sahrens 	vp = ZTOV(rootzp);
1541fa9e4066Sahrens 	vn_reinit(vp);
1542fa9e4066Sahrens 	vp->v_type = VDIR;
1543fa9e4066Sahrens 
1544fa9e4066Sahrens 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1545fa9e4066Sahrens 
1546fa9e4066Sahrens 	zfsvfs.z_os = os;
1547fa9e4066Sahrens 	zfsvfs.z_parent = &zfsvfs;
1548da6c28aaSamw 	zfsvfs.z_version = version;
1549da6c28aaSamw 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1550da6c28aaSamw 	zfsvfs.z_norm = norm;
1551de8267e0Stimh 	/*
1552de8267e0Stimh 	 * Fold case on file systems that are always or sometimes case
1553de8267e0Stimh 	 * insensitive.
1554de8267e0Stimh 	 */
1555de8267e0Stimh 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1556de8267e0Stimh 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1557fa9e4066Sahrens 
1558fa9e4066Sahrens 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1559fa9e4066Sahrens 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1560fa9e4066Sahrens 	    offsetof(znode_t, z_link_node));
1561fa9e4066Sahrens 
1562b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1563b5fca8f8Stomee 	rootzp->z_zfsvfs = &zfsvfs;
156489459e17SMark Shellenbaum 	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
156589459e17SMark Shellenbaum 	    cr, NULL, &acl_ids));
156689459e17SMark Shellenbaum 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, &acl_ids);
1567874395d5Smaybee 	ASSERT3P(zp, ==, rootzp);
1568b5fca8f8Stomee 	ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
15694ccbb6e7Sahrens 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1570fa9e4066Sahrens 	ASSERT(error == 0);
157189459e17SMark Shellenbaum 	zfs_acl_ids_free(&acl_ids);
1572b5fca8f8Stomee 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1573fa9e4066Sahrens 
1574fa9e4066Sahrens 	ZTOV(rootzp)->v_count = 0;
1575874395d5Smaybee 	dmu_buf_rele(rootzp->z_dbuf, NULL);
1576874395d5Smaybee 	rootzp->z_dbuf = NULL;
1577fa9e4066Sahrens 	kmem_cache_free(znode_cache, rootzp);
1578743a77edSAlan Wright 
1579743a77edSAlan Wright 	/*
1580743a77edSAlan Wright 	 * Create shares directory
1581743a77edSAlan Wright 	 */
1582743a77edSAlan Wright 
1583743a77edSAlan Wright 	error = zfs_create_share_dir(&zfsvfs, tx);
158489459e17SMark Shellenbaum 
1585743a77edSAlan Wright 	ASSERT(error == 0);
1586fa9e4066Sahrens }
158755434c77Sek 
1588da6c28aaSamw #endif /* _KERNEL */
158955434c77Sek /*
159055434c77Sek  * Given an object number, return its parent object number and whether
159155434c77Sek  * or not the object is an extended attribute directory.
159255434c77Sek  */
159355434c77Sek static int
159455434c77Sek zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
159555434c77Sek {
159655434c77Sek 	dmu_buf_t *db;
159755434c77Sek 	dmu_object_info_t doi;
159855434c77Sek 	znode_phys_t *zp;
159955434c77Sek 	int error;
160055434c77Sek 
160155434c77Sek 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
160255434c77Sek 		return (error);
160355434c77Sek 
160455434c77Sek 	dmu_object_info_from_db(db, &doi);
160555434c77Sek 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
160655434c77Sek 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
160755434c77Sek 		dmu_buf_rele(db, FTAG);
160855434c77Sek 		return (EINVAL);
160955434c77Sek 	}
161055434c77Sek 
161155434c77Sek 	zp = db->db_data;
161255434c77Sek 	*pobjp = zp->zp_parent;
161355434c77Sek 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
161455434c77Sek 	    S_ISDIR(zp->zp_mode);
161555434c77Sek 	dmu_buf_rele(db, FTAG);
161655434c77Sek 
161755434c77Sek 	return (0);
161855434c77Sek }
161955434c77Sek 
162055434c77Sek int
162155434c77Sek zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
162255434c77Sek {
162355434c77Sek 	char *path = buf + len - 1;
162455434c77Sek 	int error;
162555434c77Sek 
162655434c77Sek 	*path = '\0';
162755434c77Sek 
162855434c77Sek 	for (;;) {
162955434c77Sek 		uint64_t pobj;
163055434c77Sek 		char component[MAXNAMELEN + 2];
163155434c77Sek 		size_t complen;
163255434c77Sek 		int is_xattrdir;
163355434c77Sek 
163455434c77Sek 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
163555434c77Sek 		    &is_xattrdir)) != 0)
163655434c77Sek 			break;
163755434c77Sek 
163855434c77Sek 		if (pobj == obj) {
163955434c77Sek 			if (path[0] != '/')
164055434c77Sek 				*--path = '/';
164155434c77Sek 			break;
164255434c77Sek 		}
164355434c77Sek 
164455434c77Sek 		component[0] = '/';
164555434c77Sek 		if (is_xattrdir) {
164655434c77Sek 			(void) sprintf(component + 1, "<xattrdir>");
164755434c77Sek 		} else {
1648e7437265Sahrens 			error = zap_value_search(osp, pobj, obj,
1649e7437265Sahrens 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
165055434c77Sek 			if (error != 0)
165155434c77Sek 				break;
165255434c77Sek 		}
165355434c77Sek 
165455434c77Sek 		complen = strlen(component);
165555434c77Sek 		path -= complen;
165655434c77Sek 		ASSERT(path >= buf);
165755434c77Sek 		bcopy(component, path, complen);
165855434c77Sek 		obj = pobj;
165955434c77Sek 	}
166055434c77Sek 
166155434c77Sek 	if (error == 0)
166255434c77Sek 		(void) memmove(buf, path, buf + len - path);
166355434c77Sek 	return (error);
166455434c77Sek }
1665