xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_znode.c (revision b5fca8f855054d167d04d3b4de5210c83ed2083c)
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 /*
226166ad1cSek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
2675c76197Speteh /* Portions Copyright 2007 Jeremy Teo */
2775c76197Speteh 
28fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
29fa9e4066Sahrens 
3055434c77Sek #ifdef _KERNEL
31fa9e4066Sahrens #include <sys/types.h>
32fa9e4066Sahrens #include <sys/param.h>
33fa9e4066Sahrens #include <sys/time.h>
34fa9e4066Sahrens #include <sys/systm.h>
35fa9e4066Sahrens #include <sys/sysmacros.h>
36fa9e4066Sahrens #include <sys/resource.h>
37fa9e4066Sahrens #include <sys/mntent.h>
3872fc53bcSmarks #include <sys/mkdev.h>
39de8267e0Stimh #include <sys/u8_textprep.h>
40ab04eb8eStimh #include <sys/dsl_dataset.h>
41fa9e4066Sahrens #include <sys/vfs.h>
42aa59c4cbSrsb #include <sys/vfs_opreg.h>
43fa9e4066Sahrens #include <sys/vnode.h>
44fa9e4066Sahrens #include <sys/file.h>
45fa9e4066Sahrens #include <sys/kmem.h>
46fa9e4066Sahrens #include <sys/errno.h>
47fa9e4066Sahrens #include <sys/unistd.h>
48fa9e4066Sahrens #include <sys/mode.h>
49fa9e4066Sahrens #include <sys/atomic.h>
50fa9e4066Sahrens #include <vm/pvn.h>
51fa9e4066Sahrens #include "fs/fs_subr.h"
52fa9e4066Sahrens #include <sys/zfs_dir.h>
53fa9e4066Sahrens #include <sys/zfs_acl.h>
54fa9e4066Sahrens #include <sys/zfs_ioctl.h>
55104e2ed7Sperrin #include <sys/zfs_rlock.h>
56da6c28aaSamw #include <sys/zfs_fuid.h>
57fa9e4066Sahrens #include <sys/fs/zfs.h>
58da6c28aaSamw #include <sys/kidmap.h>
5955434c77Sek #endif /* _KERNEL */
6055434c77Sek 
6155434c77Sek #include <sys/dmu.h>
6255434c77Sek #include <sys/refcount.h>
6355434c77Sek #include <sys/stat.h>
6455434c77Sek #include <sys/zap.h>
6555434c77Sek #include <sys/zfs_znode.h>
66fa9e4066Sahrens 
67de8267e0Stimh #include "zfs_prop.h"
68de8267e0Stimh 
69*b5fca8f8Stomee /*
70*b5fca8f8Stomee  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
71*b5fca8f8Stomee  * turned on when DEBUG is also defined.
72*b5fca8f8Stomee  */
73*b5fca8f8Stomee #ifdef	DEBUG
74*b5fca8f8Stomee #define	ZNODE_STATS
75*b5fca8f8Stomee #endif	/* DEBUG */
76*b5fca8f8Stomee 
77*b5fca8f8Stomee #ifdef	ZNODE_STATS
78*b5fca8f8Stomee #define	ZNODE_STAT_ADD(stat)			((stat)++)
79*b5fca8f8Stomee #else
80*b5fca8f8Stomee #define	ZNODE_STAT_ADD(stat)			/* nothing */
81*b5fca8f8Stomee #endif	/* ZNODE_STATS */
82*b5fca8f8Stomee 
83*b5fca8f8Stomee #define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
84*b5fca8f8Stomee #define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
85*b5fca8f8Stomee 
8655434c77Sek /*
8755434c77Sek  * Functions needed for userland (ie: libzpool) are not put under
8855434c77Sek  * #ifdef_KERNEL; the rest of the functions have dependencies
8955434c77Sek  * (such as VFS logic) that will not compile easily in userland.
9055434c77Sek  */
9155434c77Sek #ifdef _KERNEL
92*b5fca8f8Stomee static kmem_cache_t *znode_cache = NULL;
93fa9e4066Sahrens 
94fa9e4066Sahrens /*ARGSUSED*/
95fa9e4066Sahrens static void
96874395d5Smaybee znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
97fa9e4066Sahrens {
98874395d5Smaybee 	/*
99874395d5Smaybee 	 * We should never drop all dbuf refs without first clearing
100874395d5Smaybee 	 * the eviction callback.
101874395d5Smaybee 	 */
102874395d5Smaybee 	panic("evicting znode %p\n", user_ptr);
103fa9e4066Sahrens }
104fa9e4066Sahrens 
105fa9e4066Sahrens /*ARGSUSED*/
106fa9e4066Sahrens static int
107*b5fca8f8Stomee zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
108fa9e4066Sahrens {
109fa9e4066Sahrens 	znode_t *zp = buf;
110fa9e4066Sahrens 
111*b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
112*b5fca8f8Stomee 
113*b5fca8f8Stomee 	zp->z_vnode = vn_alloc(kmflags);
114*b5fca8f8Stomee 	if (zp->z_vnode == NULL) {
115*b5fca8f8Stomee 		return (-1);
116*b5fca8f8Stomee 	}
117*b5fca8f8Stomee 	ZTOV(zp)->v_data = zp;
118*b5fca8f8Stomee 
119*b5fca8f8Stomee 	list_link_init(&zp->z_link_node);
120*b5fca8f8Stomee 
121fa9e4066Sahrens 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
122fa9e4066Sahrens 	rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL);
123104e2ed7Sperrin 	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
124af2c4821Smaybee 	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
125fa9e4066Sahrens 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
126104e2ed7Sperrin 
127104e2ed7Sperrin 	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
128104e2ed7Sperrin 	avl_create(&zp->z_range_avl, zfs_range_compare,
129104e2ed7Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
130104e2ed7Sperrin 
1314ccbb6e7Sahrens 	zp->z_dbuf = NULL;
132*b5fca8f8Stomee 	zp->z_dirlocks = NULL;
133fa9e4066Sahrens 	return (0);
134fa9e4066Sahrens }
135fa9e4066Sahrens 
136fa9e4066Sahrens /*ARGSUSED*/
137fa9e4066Sahrens static void
138*b5fca8f8Stomee zfs_znode_cache_destructor(void *buf, void *arg)
139fa9e4066Sahrens {
140fa9e4066Sahrens 	znode_t *zp = buf;
141fa9e4066Sahrens 
142*b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
143*b5fca8f8Stomee 	ASSERT(ZTOV(zp)->v_data == zp);
144*b5fca8f8Stomee 	vn_free(ZTOV(zp));
145*b5fca8f8Stomee 	ASSERT(!list_link_active(&zp->z_link_node));
146fa9e4066Sahrens 	mutex_destroy(&zp->z_lock);
147fa9e4066Sahrens 	rw_destroy(&zp->z_map_lock);
148104e2ed7Sperrin 	rw_destroy(&zp->z_parent_lock);
149af2c4821Smaybee 	rw_destroy(&zp->z_name_lock);
150fa9e4066Sahrens 	mutex_destroy(&zp->z_acl_lock);
151104e2ed7Sperrin 	avl_destroy(&zp->z_range_avl);
152c25056deSgw 	mutex_destroy(&zp->z_range_lock);
153fa9e4066Sahrens 
1544ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
155*b5fca8f8Stomee 	ASSERT(zp->z_dirlocks == NULL);
156*b5fca8f8Stomee }
157*b5fca8f8Stomee 
158*b5fca8f8Stomee #ifdef	ZNODE_STATS
159*b5fca8f8Stomee static struct {
160*b5fca8f8Stomee 	uint64_t zms_zfsvfs_invalid;
161*b5fca8f8Stomee 	uint64_t zms_zfsvfs_unmounted;
162*b5fca8f8Stomee 	uint64_t zms_zfsvfs_recheck_invalid;
163*b5fca8f8Stomee 	uint64_t zms_vnode_locked;
164*b5fca8f8Stomee 	uint64_t zms_znode_in_use;
165*b5fca8f8Stomee 	uint64_t zms_yes;
166*b5fca8f8Stomee 	uint64_t zms_later;
167*b5fca8f8Stomee 	uint64_t zms_dont_know;
168*b5fca8f8Stomee } znode_move_stats;
169*b5fca8f8Stomee #endif	/* ZNODE_STATS */
170*b5fca8f8Stomee 
171*b5fca8f8Stomee static void
172*b5fca8f8Stomee zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
173*b5fca8f8Stomee {
174*b5fca8f8Stomee 	vnode_t *vp;
175*b5fca8f8Stomee 
176*b5fca8f8Stomee 	/* Copy fields. */
177*b5fca8f8Stomee 	nzp->z_zfsvfs = ozp->z_zfsvfs;
178*b5fca8f8Stomee 
179*b5fca8f8Stomee 	/* Swap vnodes. */
180*b5fca8f8Stomee 	vp = nzp->z_vnode;
181*b5fca8f8Stomee 	nzp->z_vnode = ozp->z_vnode;
182*b5fca8f8Stomee 	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
183*b5fca8f8Stomee 	ZTOV(ozp)->v_data = ozp;
184*b5fca8f8Stomee 	ZTOV(nzp)->v_data = nzp;
185*b5fca8f8Stomee 
186*b5fca8f8Stomee 	nzp->z_id = ozp->z_id;
187*b5fca8f8Stomee 	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
188*b5fca8f8Stomee 	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
189*b5fca8f8Stomee 	nzp->z_unlinked = ozp->z_unlinked;
190*b5fca8f8Stomee 	nzp->z_atime_dirty = ozp->z_atime_dirty;
191*b5fca8f8Stomee 	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
192*b5fca8f8Stomee 	nzp->z_blksz = ozp->z_blksz;
193*b5fca8f8Stomee 	nzp->z_seq = ozp->z_seq;
194*b5fca8f8Stomee 	nzp->z_mapcnt = ozp->z_mapcnt;
195*b5fca8f8Stomee 	nzp->z_last_itx = ozp->z_last_itx;
196*b5fca8f8Stomee 	nzp->z_gen = ozp->z_gen;
197*b5fca8f8Stomee 	nzp->z_sync_cnt = ozp->z_sync_cnt;
198*b5fca8f8Stomee 	nzp->z_phys = ozp->z_phys;
199*b5fca8f8Stomee 	nzp->z_dbuf = ozp->z_dbuf;
200*b5fca8f8Stomee 
201*b5fca8f8Stomee 	/* Update back pointers. */
202*b5fca8f8Stomee 	(void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys,
203*b5fca8f8Stomee 	    znode_evict_error);
204*b5fca8f8Stomee 
205*b5fca8f8Stomee 	/*
206*b5fca8f8Stomee 	 * Invalidate the original znode by clearing fields that provide a
207*b5fca8f8Stomee 	 * pointer back to the znode. Set the low bit of the vfs pointer to
208*b5fca8f8Stomee 	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
209*b5fca8f8Stomee 	 * subsequent callback.
210*b5fca8f8Stomee 	 */
211*b5fca8f8Stomee 	ozp->z_dbuf = NULL;
212*b5fca8f8Stomee 	POINTER_INVALIDATE(&ozp->z_zfsvfs);
213*b5fca8f8Stomee }
214*b5fca8f8Stomee 
215*b5fca8f8Stomee /*
216*b5fca8f8Stomee  * Wrapper function for ZFS_ENTER that returns 0 if successful and otherwise
217*b5fca8f8Stomee  * returns a non-zero error code.
218*b5fca8f8Stomee  */
219*b5fca8f8Stomee static int
220*b5fca8f8Stomee zfs_enter(zfsvfs_t *zfsvfs)
221*b5fca8f8Stomee {
222*b5fca8f8Stomee 	ZFS_ENTER(zfsvfs);
223*b5fca8f8Stomee 	return (0);
224*b5fca8f8Stomee }
225*b5fca8f8Stomee 
226*b5fca8f8Stomee /*ARGSUSED*/
227*b5fca8f8Stomee static kmem_cbrc_t
228*b5fca8f8Stomee zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
229*b5fca8f8Stomee {
230*b5fca8f8Stomee 	znode_t *ozp = buf, *nzp = newbuf;
231*b5fca8f8Stomee 	zfsvfs_t *zfsvfs;
232*b5fca8f8Stomee 	vnode_t *vp;
233*b5fca8f8Stomee 
234*b5fca8f8Stomee 	/*
235*b5fca8f8Stomee 	 * The znode is on the file system's list of known znodes if the vfs
236*b5fca8f8Stomee 	 * pointer is valid. We set the low bit of the vfs pointer when freeing
237*b5fca8f8Stomee 	 * the znode to invalidate it, and the memory patterns written by kmem
238*b5fca8f8Stomee 	 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
239*b5fca8f8Stomee 	 * created znode sets the vfs pointer last of all to indicate that the
240*b5fca8f8Stomee 	 * znode is known and in a valid state to be moved by this function.
241*b5fca8f8Stomee 	 */
242*b5fca8f8Stomee 	zfsvfs = ozp->z_zfsvfs;
243*b5fca8f8Stomee 	if (!POINTER_IS_VALID(zfsvfs)) {
244*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
245*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_dont_know);
246*b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
247*b5fca8f8Stomee 	}
248*b5fca8f8Stomee 
249*b5fca8f8Stomee 	/*
250*b5fca8f8Stomee 	 * Ensure that the filesystem is not unmounted during the move.
251*b5fca8f8Stomee 	 */
252*b5fca8f8Stomee 	if (zfs_enter(zfsvfs) != 0) {		/* ZFS_ENTER */
253*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
254*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_dont_know);
255*b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
256*b5fca8f8Stomee 	}
257*b5fca8f8Stomee 
258*b5fca8f8Stomee 	mutex_enter(&zfsvfs->z_znodes_lock);
259*b5fca8f8Stomee 	/*
260*b5fca8f8Stomee 	 * Recheck the vfs pointer in case the znode was removed just before
261*b5fca8f8Stomee 	 * acquiring the lock.
262*b5fca8f8Stomee 	 */
263*b5fca8f8Stomee 	if (zfsvfs != ozp->z_zfsvfs) {
264*b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
265*b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
266*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck_invalid);
267*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_dont_know);
268*b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
269*b5fca8f8Stomee 	}
270*b5fca8f8Stomee 
271*b5fca8f8Stomee 	/*
272*b5fca8f8Stomee 	 * At this point we know that as long as we hold z_znodes_lock, the
273*b5fca8f8Stomee 	 * znode cannot be freed and fields within the znode can be safely
274*b5fca8f8Stomee 	 * accessed.
275*b5fca8f8Stomee 	 */
276*b5fca8f8Stomee 	vp = ZTOV(ozp);
277*b5fca8f8Stomee 	if (mutex_tryenter(&vp->v_lock) == 0) {
278*b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
279*b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
280*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
281*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_later);
282*b5fca8f8Stomee 		return (KMEM_CBRC_LATER);
283*b5fca8f8Stomee 	}
284*b5fca8f8Stomee 	/* Only move znodes that are referenced _only_ by the DNLC. */
285*b5fca8f8Stomee 	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
286*b5fca8f8Stomee 		mutex_exit(&vp->v_lock);
287*b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
288*b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
289*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_znode_in_use);
290*b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_later);
291*b5fca8f8Stomee 		return (KMEM_CBRC_LATER);
292*b5fca8f8Stomee 	}
293*b5fca8f8Stomee 
294*b5fca8f8Stomee 	/*
295*b5fca8f8Stomee 	 * The znode is known and in a valid state to move. We're holding the
296*b5fca8f8Stomee 	 * locks needed to execute the critical section.
297*b5fca8f8Stomee 	 */
298*b5fca8f8Stomee 	zfs_znode_move_impl(ozp, nzp);
299*b5fca8f8Stomee 	mutex_exit(&vp->v_lock);
300*b5fca8f8Stomee 
301*b5fca8f8Stomee 	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
302*b5fca8f8Stomee 	mutex_exit(&zfsvfs->z_znodes_lock);
303*b5fca8f8Stomee 	ZFS_EXIT(zfsvfs);
304*b5fca8f8Stomee 
305*b5fca8f8Stomee 	ZNODE_STAT_ADD(znode_move_stats.zms_yes);
306*b5fca8f8Stomee 	return (KMEM_CBRC_YES);
307fa9e4066Sahrens }
308fa9e4066Sahrens 
309fa9e4066Sahrens void
310fa9e4066Sahrens zfs_znode_init(void)
311fa9e4066Sahrens {
312fa9e4066Sahrens 	/*
313fa9e4066Sahrens 	 * Initialize zcache
314fa9e4066Sahrens 	 */
315fa9e4066Sahrens 	ASSERT(znode_cache == NULL);
316fa9e4066Sahrens 	znode_cache = kmem_cache_create("zfs_znode_cache",
317fa9e4066Sahrens 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
318fa9e4066Sahrens 	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
319*b5fca8f8Stomee 	kmem_cache_set_move(znode_cache, zfs_znode_move);
320fa9e4066Sahrens }
321fa9e4066Sahrens 
322fa9e4066Sahrens void
323fa9e4066Sahrens zfs_znode_fini(void)
324fa9e4066Sahrens {
325fa9e4066Sahrens 	/*
326fa9e4066Sahrens 	 * Cleanup vfs & vnode ops
327fa9e4066Sahrens 	 */
328fa9e4066Sahrens 	zfs_remove_op_tables();
329fa9e4066Sahrens 
330fa9e4066Sahrens 	/*
331fa9e4066Sahrens 	 * Cleanup zcache
332fa9e4066Sahrens 	 */
333fa9e4066Sahrens 	if (znode_cache)
334fa9e4066Sahrens 		kmem_cache_destroy(znode_cache);
335fa9e4066Sahrens 	znode_cache = NULL;
336fa9e4066Sahrens }
337fa9e4066Sahrens 
338fa9e4066Sahrens struct vnodeops *zfs_dvnodeops;
339fa9e4066Sahrens struct vnodeops *zfs_fvnodeops;
340fa9e4066Sahrens struct vnodeops *zfs_symvnodeops;
341fa9e4066Sahrens struct vnodeops *zfs_xdvnodeops;
342fa9e4066Sahrens struct vnodeops *zfs_evnodeops;
343fa9e4066Sahrens 
344fa9e4066Sahrens void
345fa9e4066Sahrens zfs_remove_op_tables()
346fa9e4066Sahrens {
347fa9e4066Sahrens 	/*
348fa9e4066Sahrens 	 * Remove vfs ops
349fa9e4066Sahrens 	 */
350fa9e4066Sahrens 	ASSERT(zfsfstype);
351fa9e4066Sahrens 	(void) vfs_freevfsops_by_type(zfsfstype);
352fa9e4066Sahrens 	zfsfstype = 0;
353fa9e4066Sahrens 
354fa9e4066Sahrens 	/*
355fa9e4066Sahrens 	 * Remove vnode ops
356fa9e4066Sahrens 	 */
357fa9e4066Sahrens 	if (zfs_dvnodeops)
358fa9e4066Sahrens 		vn_freevnodeops(zfs_dvnodeops);
359fa9e4066Sahrens 	if (zfs_fvnodeops)
360fa9e4066Sahrens 		vn_freevnodeops(zfs_fvnodeops);
361fa9e4066Sahrens 	if (zfs_symvnodeops)
362fa9e4066Sahrens 		vn_freevnodeops(zfs_symvnodeops);
363fa9e4066Sahrens 	if (zfs_xdvnodeops)
364fa9e4066Sahrens 		vn_freevnodeops(zfs_xdvnodeops);
365fa9e4066Sahrens 	if (zfs_evnodeops)
366fa9e4066Sahrens 		vn_freevnodeops(zfs_evnodeops);
367fa9e4066Sahrens 
368fa9e4066Sahrens 	zfs_dvnodeops = NULL;
369fa9e4066Sahrens 	zfs_fvnodeops = NULL;
370fa9e4066Sahrens 	zfs_symvnodeops = NULL;
371fa9e4066Sahrens 	zfs_xdvnodeops = NULL;
372fa9e4066Sahrens 	zfs_evnodeops = NULL;
373fa9e4066Sahrens }
374fa9e4066Sahrens 
375fa9e4066Sahrens extern const fs_operation_def_t zfs_dvnodeops_template[];
376fa9e4066Sahrens extern const fs_operation_def_t zfs_fvnodeops_template[];
377fa9e4066Sahrens extern const fs_operation_def_t zfs_xdvnodeops_template[];
378fa9e4066Sahrens extern const fs_operation_def_t zfs_symvnodeops_template[];
379fa9e4066Sahrens extern const fs_operation_def_t zfs_evnodeops_template[];
380fa9e4066Sahrens 
381fa9e4066Sahrens int
382fa9e4066Sahrens zfs_create_op_tables()
383fa9e4066Sahrens {
384fa9e4066Sahrens 	int error;
385fa9e4066Sahrens 
386fa9e4066Sahrens 	/*
387fa9e4066Sahrens 	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
388fa9e4066Sahrens 	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
389fa9e4066Sahrens 	 * In this case we just return as the ops vectors are already set up.
390fa9e4066Sahrens 	 */
391fa9e4066Sahrens 	if (zfs_dvnodeops)
392fa9e4066Sahrens 		return (0);
393fa9e4066Sahrens 
394fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
395fa9e4066Sahrens 	    &zfs_dvnodeops);
396fa9e4066Sahrens 	if (error)
397fa9e4066Sahrens 		return (error);
398fa9e4066Sahrens 
399fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
400fa9e4066Sahrens 	    &zfs_fvnodeops);
401fa9e4066Sahrens 	if (error)
402fa9e4066Sahrens 		return (error);
403fa9e4066Sahrens 
404fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
405fa9e4066Sahrens 	    &zfs_symvnodeops);
406fa9e4066Sahrens 	if (error)
407fa9e4066Sahrens 		return (error);
408fa9e4066Sahrens 
409fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
410fa9e4066Sahrens 	    &zfs_xdvnodeops);
411fa9e4066Sahrens 	if (error)
412fa9e4066Sahrens 		return (error);
413fa9e4066Sahrens 
414fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
415fa9e4066Sahrens 	    &zfs_evnodeops);
416fa9e4066Sahrens 
417fa9e4066Sahrens 	return (error);
418fa9e4066Sahrens }
419fa9e4066Sahrens 
420fa9e4066Sahrens /*
421fa9e4066Sahrens  * zfs_init_fs - Initialize the zfsvfs struct and the file system
422fa9e4066Sahrens  *	incore "master" object.  Verify version compatibility.
423fa9e4066Sahrens  */
424fa9e4066Sahrens int
425fa9e4066Sahrens zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr)
426fa9e4066Sahrens {
427fa9e4066Sahrens 	extern int zfsfstype;
428fa9e4066Sahrens 
429fa9e4066Sahrens 	objset_t	*os = zfsvfs->z_os;
430fa9e4066Sahrens 	int		i, error;
431fa9e4066Sahrens 	dmu_object_info_t doi;
432a2eea2e1Sahrens 	uint64_t fsid_guid;
433de8267e0Stimh 	uint64_t zval;
434fa9e4066Sahrens 
435fa9e4066Sahrens 	*zpp = NULL;
436fa9e4066Sahrens 
437fa9e4066Sahrens 	/*
438fa9e4066Sahrens 	 * XXX - hack to auto-create the pool root filesystem at
439fa9e4066Sahrens 	 * the first attempted mount.
440fa9e4066Sahrens 	 */
441fa9e4066Sahrens 	if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) {
442fa9e4066Sahrens 		dmu_tx_t *tx = dmu_tx_create(os);
443da6c28aaSamw 		uint64_t zpl_version;
444de8267e0Stimh 		nvlist_t *zprops;
445fa9e4066Sahrens 
446ea8dc4b6Seschrock 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */
447ea8dc4b6Seschrock 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */
448fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */
449fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
450fa9e4066Sahrens 		ASSERT3U(error, ==, 0);
451da6c28aaSamw 		if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
452da6c28aaSamw 			zpl_version = ZPL_VERSION;
453da6c28aaSamw 		else
454da6c28aaSamw 			zpl_version = ZPL_VERSION_FUID - 1;
455de8267e0Stimh 
456de8267e0Stimh 		VERIFY(nvlist_alloc(&zprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
457de8267e0Stimh 		VERIFY(nvlist_add_uint64(zprops,
458de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), zpl_version) == 0);
459de8267e0Stimh 		zfs_create_fs(os, cr, zprops, tx);
460de8267e0Stimh 		nvlist_free(zprops);
461fa9e4066Sahrens 		dmu_tx_commit(tx);
462fa9e4066Sahrens 	}
463fa9e4066Sahrens 
464de8267e0Stimh 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
465ea8dc4b6Seschrock 	if (error) {
466ea8dc4b6Seschrock 		return (error);
467e7437265Sahrens 	} else if (zfsvfs->z_version > ZPL_VERSION) {
468fa9e4066Sahrens 		(void) printf("Mismatched versions:  File system "
469b3d911cbStimh 		    "is version %llu on-disk format, which is "
470fa9e4066Sahrens 		    "incompatible with this software version %lld!",
471e7437265Sahrens 		    (u_longlong_t)zfsvfs->z_version, ZPL_VERSION);
472fa9e4066Sahrens 		return (ENOTSUP);
473fa9e4066Sahrens 	}
474fa9e4066Sahrens 
475de8267e0Stimh 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
476de8267e0Stimh 		return (error);
477de8267e0Stimh 	zfsvfs->z_norm = (int)zval;
478de8267e0Stimh 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
479de8267e0Stimh 		return (error);
480de8267e0Stimh 	zfsvfs->z_utf8 = (zval != 0);
481de8267e0Stimh 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
482de8267e0Stimh 		return (error);
483de8267e0Stimh 	zfsvfs->z_case = (uint_t)zval;
484de8267e0Stimh 	/*
485de8267e0Stimh 	 * Fold case on file systems that are always or sometimes case
486de8267e0Stimh 	 * insensitive.
487de8267e0Stimh 	 */
488de8267e0Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
489de8267e0Stimh 	    zfsvfs->z_case == ZFS_CASE_MIXED)
490de8267e0Stimh 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
491de8267e0Stimh 
492fa9e4066Sahrens 	/*
493fa9e4066Sahrens 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
494fa9e4066Sahrens 	 * separates our fsid from any other filesystem types, and a
495fa9e4066Sahrens 	 * 56-bit objset unique ID.  The objset unique ID is unique to
496fa9e4066Sahrens 	 * all objsets open on this system, provided by unique_create().
497fa9e4066Sahrens 	 * The 8-bit fs type must be put in the low bits of fsid[1]
498fa9e4066Sahrens 	 * because that's where other Solaris filesystems put it.
499fa9e4066Sahrens 	 */
500a2eea2e1Sahrens 	fsid_guid = dmu_objset_fsid_guid(os);
501a2eea2e1Sahrens 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
502a2eea2e1Sahrens 	zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid;
503a2eea2e1Sahrens 	zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
504fa9e4066Sahrens 	    zfsfstype & 0xFF;
505fa9e4066Sahrens 
506893a6d32Sahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
507893a6d32Sahrens 	    &zfsvfs->z_root);
508ea8dc4b6Seschrock 	if (error)
509ea8dc4b6Seschrock 		return (error);
510893a6d32Sahrens 	ASSERT(zfsvfs->z_root != 0);
511fa9e4066Sahrens 
512c25056deSgw 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
513c25056deSgw 	    &zfsvfs->z_unlinkedobj);
514c25056deSgw 	if (error)
515c25056deSgw 		return (error);
516c25056deSgw 
517fa9e4066Sahrens 	/*
518fa9e4066Sahrens 	 * Initialize zget mutex's
519fa9e4066Sahrens 	 */
520fa9e4066Sahrens 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
521fa9e4066Sahrens 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
522fa9e4066Sahrens 
523893a6d32Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp);
524c25056deSgw 	if (error) {
525c25056deSgw 		/*
526c25056deSgw 		 * On error, we destroy the mutexes here since it's not
527c25056deSgw 		 * possible for the caller to determine if the mutexes were
528c25056deSgw 		 * initialized properly.
529c25056deSgw 		 */
530c25056deSgw 		for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
531c25056deSgw 			mutex_destroy(&zfsvfs->z_hold_mtx[i]);
532fa9e4066Sahrens 		return (error);
533c25056deSgw 	}
534893a6d32Sahrens 	ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root);
535da6c28aaSamw 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
536da6c28aaSamw 	    &zfsvfs->z_fuid_obj);
537da6c28aaSamw 	if (error == ENOENT)
538da6c28aaSamw 		error = 0;
539da6c28aaSamw 
540fa9e4066Sahrens 	return (0);
541fa9e4066Sahrens }
542fa9e4066Sahrens 
54372fc53bcSmarks /*
54472fc53bcSmarks  * define a couple of values we need available
54572fc53bcSmarks  * for both 64 and 32 bit environments.
54672fc53bcSmarks  */
54772fc53bcSmarks #ifndef NBITSMINOR64
54872fc53bcSmarks #define	NBITSMINOR64	32
54972fc53bcSmarks #endif
55072fc53bcSmarks #ifndef MAXMAJ64
55172fc53bcSmarks #define	MAXMAJ64	0xffffffffUL
55272fc53bcSmarks #endif
55372fc53bcSmarks #ifndef	MAXMIN64
55472fc53bcSmarks #define	MAXMIN64	0xffffffffUL
55572fc53bcSmarks #endif
55672fc53bcSmarks 
55772fc53bcSmarks /*
55872fc53bcSmarks  * Create special expldev for ZFS private use.
55972fc53bcSmarks  * Can't use standard expldev since it doesn't do
56072fc53bcSmarks  * what we want.  The standard expldev() takes a
56172fc53bcSmarks  * dev32_t in LP64 and expands it to a long dev_t.
56272fc53bcSmarks  * We need an interface that takes a dev32_t in ILP32
56372fc53bcSmarks  * and expands it to a long dev_t.
56472fc53bcSmarks  */
56572fc53bcSmarks static uint64_t
56672fc53bcSmarks zfs_expldev(dev_t dev)
56772fc53bcSmarks {
56872fc53bcSmarks #ifndef _LP64
56972fc53bcSmarks 	major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
57072fc53bcSmarks 	return (((uint64_t)major << NBITSMINOR64) |
57172fc53bcSmarks 	    ((minor_t)dev & MAXMIN32));
57272fc53bcSmarks #else
57372fc53bcSmarks 	return (dev);
57472fc53bcSmarks #endif
57572fc53bcSmarks }
57672fc53bcSmarks 
57772fc53bcSmarks /*
57872fc53bcSmarks  * Special cmpldev for ZFS private use.
57972fc53bcSmarks  * Can't use standard cmpldev since it takes
58072fc53bcSmarks  * a long dev_t and compresses it to dev32_t in
58172fc53bcSmarks  * LP64.  We need to do a compaction of a long dev_t
58272fc53bcSmarks  * to a dev32_t in ILP32.
58372fc53bcSmarks  */
58472fc53bcSmarks dev_t
58572fc53bcSmarks zfs_cmpldev(uint64_t dev)
58672fc53bcSmarks {
58772fc53bcSmarks #ifndef _LP64
58872fc53bcSmarks 	minor_t minor = (minor_t)dev & MAXMIN64;
58972fc53bcSmarks 	major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
59072fc53bcSmarks 
59172fc53bcSmarks 	if (major > MAXMAJ32 || minor > MAXMIN32)
59272fc53bcSmarks 		return (NODEV32);
59372fc53bcSmarks 
59472fc53bcSmarks 	return (((dev32_t)major << NBITSMINOR32) | minor);
59572fc53bcSmarks #else
59672fc53bcSmarks 	return (dev);
59772fc53bcSmarks #endif
59872fc53bcSmarks }
59972fc53bcSmarks 
6004ccbb6e7Sahrens static void
601*b5fca8f8Stomee zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
6024ccbb6e7Sahrens {
6034ccbb6e7Sahrens 	znode_t		*nzp;
6044ccbb6e7Sahrens 
605*b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
606*b5fca8f8Stomee 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
6074ccbb6e7Sahrens 
6084ccbb6e7Sahrens 	mutex_enter(&zp->z_lock);
6094ccbb6e7Sahrens 
6104ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
6114ccbb6e7Sahrens 	zp->z_dbuf = db;
612874395d5Smaybee 	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error);
6134ccbb6e7Sahrens 
6144ccbb6e7Sahrens 	/*
6154ccbb6e7Sahrens 	 * there should be no
6164ccbb6e7Sahrens 	 * concurrent zgets on this object.
6174ccbb6e7Sahrens 	 */
6184ccbb6e7Sahrens 	if (nzp != NULL)
6194ccbb6e7Sahrens 		panic("existing znode %p for dbuf %p", nzp, db);
6204ccbb6e7Sahrens 
6214ccbb6e7Sahrens 	/*
6224ccbb6e7Sahrens 	 * Slap on VROOT if we are the root znode
6234ccbb6e7Sahrens 	 */
6244ccbb6e7Sahrens 	if (zp->z_id == zfsvfs->z_root)
6254ccbb6e7Sahrens 		ZTOV(zp)->v_flag |= VROOT;
6264ccbb6e7Sahrens 
6274ccbb6e7Sahrens 	mutex_exit(&zp->z_lock);
6284ccbb6e7Sahrens 	vn_exists(ZTOV(zp));
6294ccbb6e7Sahrens }
6304ccbb6e7Sahrens 
631874395d5Smaybee void
6324ccbb6e7Sahrens zfs_znode_dmu_fini(znode_t *zp)
6334ccbb6e7Sahrens {
6344ccbb6e7Sahrens 	dmu_buf_t *db = zp->z_dbuf;
635*b5fca8f8Stomee 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
636*b5fca8f8Stomee 	    zp->z_unlinked ||
637874395d5Smaybee 	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
6384ccbb6e7Sahrens 	ASSERT(zp->z_dbuf != NULL);
6394ccbb6e7Sahrens 	zp->z_dbuf = NULL;
640874395d5Smaybee 	VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL));
6414ccbb6e7Sahrens 	dmu_buf_rele(db, NULL);
6424ccbb6e7Sahrens }
6434ccbb6e7Sahrens 
644fa9e4066Sahrens /*
645fa9e4066Sahrens  * Construct a new znode/vnode and intialize.
646fa9e4066Sahrens  *
647fa9e4066Sahrens  * This does not do a call to dmu_set_user() that is
648fa9e4066Sahrens  * up to the caller to do, in case you don't want to
649fa9e4066Sahrens  * return the znode
650fa9e4066Sahrens  */
651ea8dc4b6Seschrock static znode_t *
6524ccbb6e7Sahrens zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
653fa9e4066Sahrens {
654fa9e4066Sahrens 	znode_t	*zp;
655fa9e4066Sahrens 	vnode_t *vp;
656fa9e4066Sahrens 
657fa9e4066Sahrens 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
658fa9e4066Sahrens 
659fa9e4066Sahrens 	ASSERT(zp->z_dirlocks == NULL);
6604ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
661*b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
662fa9e4066Sahrens 
663*b5fca8f8Stomee 	/*
664*b5fca8f8Stomee 	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
665*b5fca8f8Stomee 	 * the zfs_znode_move() callback.
666*b5fca8f8Stomee 	 */
6674ccbb6e7Sahrens 	zp->z_phys = NULL;
668893a6d32Sahrens 	zp->z_unlinked = 0;
669fa9e4066Sahrens 	zp->z_atime_dirty = 0;
670fa9e4066Sahrens 	zp->z_mapcnt = 0;
671fa9e4066Sahrens 	zp->z_last_itx = 0;
6724ccbb6e7Sahrens 	zp->z_id = db->db_object;
673fa9e4066Sahrens 	zp->z_blksz = blksz;
674fa9e4066Sahrens 	zp->z_seq = 0x7A4653;
67567bd71c6Sperrin 	zp->z_sync_cnt = 0;
6764ccbb6e7Sahrens 
6774ccbb6e7Sahrens 	vp = ZTOV(zp);
6784ccbb6e7Sahrens 	vn_reinit(vp);
6794ccbb6e7Sahrens 
680*b5fca8f8Stomee 	zfs_znode_dmu_init(zfsvfs, zp, db);
6814ccbb6e7Sahrens 
682f18faf3fSek 	zp->z_gen = zp->z_phys->zp_gen;
683fa9e4066Sahrens 
684fa9e4066Sahrens 	vp->v_vfsp = zfsvfs->z_parent->z_vfs;
685fa9e4066Sahrens 	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
686fa9e4066Sahrens 
687fa9e4066Sahrens 	switch (vp->v_type) {
688fa9e4066Sahrens 	case VDIR:
689fa9e4066Sahrens 		if (zp->z_phys->zp_flags & ZFS_XATTR) {
690fa9e4066Sahrens 			vn_setops(vp, zfs_xdvnodeops);
691fa9e4066Sahrens 			vp->v_flag |= V_XATTRDIR;
6924ccbb6e7Sahrens 		} else {
693fa9e4066Sahrens 			vn_setops(vp, zfs_dvnodeops);
6944ccbb6e7Sahrens 		}
6957f6e3e7dSperrin 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
696fa9e4066Sahrens 		break;
697fa9e4066Sahrens 	case VBLK:
698fa9e4066Sahrens 	case VCHR:
69972fc53bcSmarks 		vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
700fa9e4066Sahrens 		/*FALLTHROUGH*/
701fa9e4066Sahrens 	case VFIFO:
702fa9e4066Sahrens 	case VSOCK:
703fa9e4066Sahrens 	case VDOOR:
704fa9e4066Sahrens 		vn_setops(vp, zfs_fvnodeops);
705fa9e4066Sahrens 		break;
706fa9e4066Sahrens 	case VREG:
707fa9e4066Sahrens 		vp->v_flag |= VMODSORT;
708fa9e4066Sahrens 		vn_setops(vp, zfs_fvnodeops);
709fa9e4066Sahrens 		break;
710fa9e4066Sahrens 	case VLNK:
711fa9e4066Sahrens 		vn_setops(vp, zfs_symvnodeops);
712fa9e4066Sahrens 		break;
713fa9e4066Sahrens 	default:
714fa9e4066Sahrens 		vn_setops(vp, zfs_evnodeops);
715fa9e4066Sahrens 		break;
716fa9e4066Sahrens 	}
717fa9e4066Sahrens 
718*b5fca8f8Stomee 	mutex_enter(&zfsvfs->z_znodes_lock);
719*b5fca8f8Stomee 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
720*b5fca8f8Stomee 	membar_producer();
721*b5fca8f8Stomee 	/*
722*b5fca8f8Stomee 	 * Everything else must be valid before assigning z_zfsvfs makes the
723*b5fca8f8Stomee 	 * znode eligible for zfs_znode_move().
724*b5fca8f8Stomee 	 */
725*b5fca8f8Stomee 	zp->z_zfsvfs = zfsvfs;
726*b5fca8f8Stomee 	mutex_exit(&zfsvfs->z_znodes_lock);
727*b5fca8f8Stomee 
728874395d5Smaybee 	VFS_HOLD(zfsvfs->z_vfs);
729fa9e4066Sahrens 	return (zp);
730fa9e4066Sahrens }
731fa9e4066Sahrens 
732fa9e4066Sahrens /*
733fa9e4066Sahrens  * Create a new DMU object to hold a zfs znode.
734fa9e4066Sahrens  *
735fa9e4066Sahrens  *	IN:	dzp	- parent directory for new znode
736fa9e4066Sahrens  *		vap	- file attributes for new znode
737fa9e4066Sahrens  *		tx	- dmu transaction id for zap operations
738fa9e4066Sahrens  *		cr	- credentials of caller
739fa9e4066Sahrens  *		flag	- flags:
740fa9e4066Sahrens  *			  IS_ROOT_NODE	- new object will be root
741fa9e4066Sahrens  *			  IS_XATTR	- new object is an attribute
742fa9e4066Sahrens  *			  IS_REPLAY	- intent log replay
743da6c28aaSamw  *		bonuslen - length of bonus buffer
744da6c28aaSamw  *		setaclp  - File/Dir initial ACL
745da6c28aaSamw  *		fuidp	 - Tracks fuid allocation.
746fa9e4066Sahrens  *
7474ccbb6e7Sahrens  *	OUT:	zpp	- allocated znode
748fa9e4066Sahrens  *
749fa9e4066Sahrens  */
750fa9e4066Sahrens void
7514ccbb6e7Sahrens zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
752da6c28aaSamw     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_t *setaclp,
753da6c28aaSamw     zfs_fuid_info_t **fuidp)
754fa9e4066Sahrens {
7554ccbb6e7Sahrens 	dmu_buf_t	*db;
756fa9e4066Sahrens 	znode_phys_t	*pzp;
757fa9e4066Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
758fa9e4066Sahrens 	timestruc_t	now;
7594ccbb6e7Sahrens 	uint64_t	gen, obj;
760fa9e4066Sahrens 	int		err;
761fa9e4066Sahrens 
762fa9e4066Sahrens 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
763fa9e4066Sahrens 
764fa9e4066Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL) {		/* ZIL replay */
7654ccbb6e7Sahrens 		obj = vap->va_nodeid;
766fa9e4066Sahrens 		flag |= IS_REPLAY;
767fa9e4066Sahrens 		now = vap->va_ctime;		/* see zfs_replay_create() */
768fa9e4066Sahrens 		gen = vap->va_nblocks;		/* ditto */
769fa9e4066Sahrens 	} else {
7704ccbb6e7Sahrens 		obj = 0;
771fa9e4066Sahrens 		gethrestime(&now);
772fa9e4066Sahrens 		gen = dmu_tx_get_txg(tx);
773fa9e4066Sahrens 	}
774fa9e4066Sahrens 
775fa9e4066Sahrens 	/*
776fa9e4066Sahrens 	 * Create a new DMU object.
777fa9e4066Sahrens 	 */
778ea8dc4b6Seschrock 	/*
779ea8dc4b6Seschrock 	 * There's currently no mechanism for pre-reading the blocks that will
780ea8dc4b6Seschrock 	 * be to needed allocate a new object, so we accept the small chance
781ea8dc4b6Seschrock 	 * that there will be an i/o error and we will fail one of the
782ea8dc4b6Seschrock 	 * assertions below.
783ea8dc4b6Seschrock 	 */
784fa9e4066Sahrens 	if (vap->va_type == VDIR) {
785fa9e4066Sahrens 		if (flag & IS_REPLAY) {
7864ccbb6e7Sahrens 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
787da6c28aaSamw 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
788fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
789fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
790fa9e4066Sahrens 		} else {
7914ccbb6e7Sahrens 			obj = zap_create_norm(zfsvfs->z_os,
792da6c28aaSamw 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
793fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
794fa9e4066Sahrens 		}
795fa9e4066Sahrens 	} else {
796fa9e4066Sahrens 		if (flag & IS_REPLAY) {
7974ccbb6e7Sahrens 			err = dmu_object_claim(zfsvfs->z_os, obj,
798fa9e4066Sahrens 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
799fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
800fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
801fa9e4066Sahrens 		} else {
8024ccbb6e7Sahrens 			obj = dmu_object_alloc(zfsvfs->z_os,
803fa9e4066Sahrens 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
804fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
805fa9e4066Sahrens 		}
806fa9e4066Sahrens 	}
8074ccbb6e7Sahrens 	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
8084ccbb6e7Sahrens 	dmu_buf_will_dirty(db, tx);
809fa9e4066Sahrens 
810fa9e4066Sahrens 	/*
811fa9e4066Sahrens 	 * Initialize the znode physical data to zero.
812fa9e4066Sahrens 	 */
8134ccbb6e7Sahrens 	ASSERT(db->db_size >= sizeof (znode_phys_t));
8144ccbb6e7Sahrens 	bzero(db->db_data, db->db_size);
8154ccbb6e7Sahrens 	pzp = db->db_data;
816fa9e4066Sahrens 
817fa9e4066Sahrens 	/*
818fa9e4066Sahrens 	 * If this is the root, fix up the half-initialized parent pointer
819fa9e4066Sahrens 	 * to reference the just-allocated physical data area.
820fa9e4066Sahrens 	 */
821fa9e4066Sahrens 	if (flag & IS_ROOT_NODE) {
822874395d5Smaybee 		dzp->z_dbuf = db;
823fa9e4066Sahrens 		dzp->z_phys = pzp;
8244ccbb6e7Sahrens 		dzp->z_id = obj;
825fa9e4066Sahrens 	}
826fa9e4066Sahrens 
827fa9e4066Sahrens 	/*
828fa9e4066Sahrens 	 * If parent is an xattr, so am I.
829fa9e4066Sahrens 	 */
830fa9e4066Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR)
831fa9e4066Sahrens 		flag |= IS_XATTR;
832fa9e4066Sahrens 
833fa9e4066Sahrens 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
83472fc53bcSmarks 		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
835fa9e4066Sahrens 	}
836fa9e4066Sahrens 
837da6c28aaSamw 	if (zfsvfs->z_use_fuids)
838da6c28aaSamw 		pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
839da6c28aaSamw 
840fa9e4066Sahrens 	if (vap->va_type == VDIR) {
841fa9e4066Sahrens 		pzp->zp_size = 2;		/* contents ("." and "..") */
842fa9e4066Sahrens 		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
843fa9e4066Sahrens 	}
844fa9e4066Sahrens 
845fa9e4066Sahrens 	pzp->zp_parent = dzp->z_id;
846fa9e4066Sahrens 	if (flag & IS_XATTR)
847fa9e4066Sahrens 		pzp->zp_flags |= ZFS_XATTR;
848fa9e4066Sahrens 
849fa9e4066Sahrens 	pzp->zp_gen = gen;
850fa9e4066Sahrens 
851fa9e4066Sahrens 	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
852fa9e4066Sahrens 	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
853fa9e4066Sahrens 
854fa9e4066Sahrens 	if (vap->va_mask & AT_ATIME) {
855fa9e4066Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
856fa9e4066Sahrens 	} else {
857fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
858fa9e4066Sahrens 	}
859fa9e4066Sahrens 
860fa9e4066Sahrens 	if (vap->va_mask & AT_MTIME) {
861fa9e4066Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
862fa9e4066Sahrens 	} else {
863fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
864fa9e4066Sahrens 	}
865fa9e4066Sahrens 
866fa9e4066Sahrens 	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
867874395d5Smaybee 	if (!(flag & IS_ROOT_NODE)) {
868*b5fca8f8Stomee 		ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
869874395d5Smaybee 		*zpp = zfs_znode_alloc(zfsvfs, db, 0);
870874395d5Smaybee 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
871874395d5Smaybee 	} else {
872874395d5Smaybee 		/*
873874395d5Smaybee 		 * If we are creating the root node, the "parent" we
874874395d5Smaybee 		 * passed in is the znode for the root.
875874395d5Smaybee 		 */
876874395d5Smaybee 		*zpp = dzp;
877874395d5Smaybee 	}
8784ccbb6e7Sahrens 	zfs_perm_init(*zpp, dzp, flag, vap, tx, cr, setaclp, fuidp);
879fa9e4066Sahrens }
880fa9e4066Sahrens 
881da6c28aaSamw void
882da6c28aaSamw zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
883da6c28aaSamw {
884da6c28aaSamw 	xoptattr_t *xoap;
885da6c28aaSamw 
886da6c28aaSamw 	xoap = xva_getxoptattr(xvap);
887da6c28aaSamw 	ASSERT(xoap);
888da6c28aaSamw 
889da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
890da6c28aaSamw 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
891da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_CREATETIME);
892da6c28aaSamw 	}
893da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
894da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
895da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_READONLY);
896da6c28aaSamw 	}
897da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
898da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
899da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_HIDDEN);
900da6c28aaSamw 	}
901da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
902da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
903da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_SYSTEM);
904da6c28aaSamw 	}
905da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
906da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
907da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
908da6c28aaSamw 	}
909da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
910da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
911da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
912da6c28aaSamw 	}
913da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
914da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
915da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
916da6c28aaSamw 	}
917da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
918da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
919da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
920da6c28aaSamw 	}
921da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
922da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
923da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_NODUMP);
924da6c28aaSamw 	}
925da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
926da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
927da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_OPAQUE);
928da6c28aaSamw 	}
929da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
930da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
931da6c28aaSamw 		    xoap->xoa_av_quarantined);
932da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
933da6c28aaSamw 	}
934da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
935da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
936da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
937da6c28aaSamw 	}
938da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
939da6c28aaSamw 		(void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
940da6c28aaSamw 		    sizeof (xoap->xoa_av_scanstamp));
941da6c28aaSamw 		zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
942da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
943da6c28aaSamw 	}
944da6c28aaSamw }
945da6c28aaSamw 
946fa9e4066Sahrens int
947fa9e4066Sahrens zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
948fa9e4066Sahrens {
949fa9e4066Sahrens 	dmu_object_info_t doi;
950fa9e4066Sahrens 	dmu_buf_t	*db;
951fa9e4066Sahrens 	znode_t		*zp;
952ea8dc4b6Seschrock 	int err;
953fa9e4066Sahrens 
954fa9e4066Sahrens 	*zpp = NULL;
955fa9e4066Sahrens 
956fa9e4066Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
957fa9e4066Sahrens 
958ea8dc4b6Seschrock 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
959ea8dc4b6Seschrock 	if (err) {
960fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
961ea8dc4b6Seschrock 		return (err);
962fa9e4066Sahrens 	}
963fa9e4066Sahrens 
964fa9e4066Sahrens 	dmu_object_info_from_db(db, &doi);
965fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
966fa9e4066Sahrens 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
967ea8dc4b6Seschrock 		dmu_buf_rele(db, NULL);
968fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
969fa9e4066Sahrens 		return (EINVAL);
970fa9e4066Sahrens 	}
971fa9e4066Sahrens 
972fa9e4066Sahrens 	zp = dmu_buf_get_user(db);
973fa9e4066Sahrens 	if (zp != NULL) {
974fa9e4066Sahrens 		mutex_enter(&zp->z_lock);
975fa9e4066Sahrens 
9764ccbb6e7Sahrens 		/*
9774ccbb6e7Sahrens 		 * Since we do immediate eviction of the z_dbuf, we
9784ccbb6e7Sahrens 		 * should never find a dbuf with a znode that doesn't
9794ccbb6e7Sahrens 		 * know about the dbuf.
9804ccbb6e7Sahrens 		 */
9814ccbb6e7Sahrens 		ASSERT3P(zp->z_dbuf, ==, db);
982fa9e4066Sahrens 		ASSERT3U(zp->z_id, ==, obj_num);
983893a6d32Sahrens 		if (zp->z_unlinked) {
9844ccbb6e7Sahrens 			err = ENOENT;
985fa9e4066Sahrens 		} else {
9864ccbb6e7Sahrens 			VN_HOLD(ZTOV(zp));
9874ccbb6e7Sahrens 			*zpp = zp;
9884ccbb6e7Sahrens 			err = 0;
989fa9e4066Sahrens 		}
9904ccbb6e7Sahrens 		dmu_buf_rele(db, NULL);
991fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
992ea8dc4b6Seschrock 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
9934ccbb6e7Sahrens 		return (err);
994fa9e4066Sahrens 	}
995fa9e4066Sahrens 
996fa9e4066Sahrens 	/*
997fa9e4066Sahrens 	 * Not found create new znode/vnode
998fa9e4066Sahrens 	 */
9994ccbb6e7Sahrens 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
1000ea8dc4b6Seschrock 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1001fa9e4066Sahrens 	*zpp = zp;
1002fa9e4066Sahrens 	return (0);
1003fa9e4066Sahrens }
1004fa9e4066Sahrens 
1005f18faf3fSek int
1006f18faf3fSek zfs_rezget(znode_t *zp)
1007f18faf3fSek {
1008f18faf3fSek 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1009f18faf3fSek 	dmu_object_info_t doi;
1010f18faf3fSek 	dmu_buf_t *db;
1011f18faf3fSek 	uint64_t obj_num = zp->z_id;
1012f18faf3fSek 	int err;
1013f18faf3fSek 
1014f18faf3fSek 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1015f18faf3fSek 
1016f18faf3fSek 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
1017f18faf3fSek 	if (err) {
1018f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1019f18faf3fSek 		return (err);
1020f18faf3fSek 	}
1021f18faf3fSek 
1022f18faf3fSek 	dmu_object_info_from_db(db, &doi);
1023f18faf3fSek 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1024f18faf3fSek 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1025f18faf3fSek 		dmu_buf_rele(db, NULL);
1026f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1027f18faf3fSek 		return (EINVAL);
1028f18faf3fSek 	}
1029f18faf3fSek 
1030f18faf3fSek 	if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
1031f18faf3fSek 		dmu_buf_rele(db, NULL);
1032f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1033f18faf3fSek 		return (EIO);
1034f18faf3fSek 	}
1035f18faf3fSek 
1036*b5fca8f8Stomee 	zfs_znode_dmu_init(zfsvfs, zp, db);
1037f18faf3fSek 	zp->z_unlinked = (zp->z_phys->zp_links == 0);
10386166ad1cSek 	zp->z_blksz = doi.doi_data_block_size;
1039f18faf3fSek 
1040f18faf3fSek 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1041f18faf3fSek 
1042f18faf3fSek 	return (0);
1043f18faf3fSek }
1044f18faf3fSek 
1045fa9e4066Sahrens void
1046fa9e4066Sahrens zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1047fa9e4066Sahrens {
1048fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
10494ccbb6e7Sahrens 	uint64_t obj = zp->z_id;
1050fa9e4066Sahrens 
10514ccbb6e7Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1052fa9e4066Sahrens 	if (zp->z_phys->zp_acl.z_acl_extern_obj) {
10534ccbb6e7Sahrens 		VERIFY(0 == dmu_object_free(zfsvfs->z_os,
10544ccbb6e7Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj, tx));
1055fa9e4066Sahrens 	}
10564ccbb6e7Sahrens 	VERIFY(0 == dmu_object_free(zfsvfs->z_os, obj, tx));
10574ccbb6e7Sahrens 	zfs_znode_dmu_fini(zp);
10584ccbb6e7Sahrens 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1059874395d5Smaybee 	zfs_znode_free(zp);
1060fa9e4066Sahrens }
1061fa9e4066Sahrens 
1062fa9e4066Sahrens void
1063fa9e4066Sahrens zfs_zinactive(znode_t *zp)
1064fa9e4066Sahrens {
1065fa9e4066Sahrens 	vnode_t	*vp = ZTOV(zp);
1066fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1067fa9e4066Sahrens 	uint64_t z_id = zp->z_id;
1068fa9e4066Sahrens 
10694ccbb6e7Sahrens 	ASSERT(zp->z_dbuf && zp->z_phys);
1070fa9e4066Sahrens 
1071fa9e4066Sahrens 	/*
1072fa9e4066Sahrens 	 * Don't allow a zfs_zget() while were trying to release this znode
1073fa9e4066Sahrens 	 */
1074fa9e4066Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1075fa9e4066Sahrens 
1076fa9e4066Sahrens 	mutex_enter(&zp->z_lock);
1077fa9e4066Sahrens 	mutex_enter(&vp->v_lock);
1078fa9e4066Sahrens 	vp->v_count--;
1079fa9e4066Sahrens 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1080fa9e4066Sahrens 		/*
1081fa9e4066Sahrens 		 * If the hold count is greater than zero, somebody has
1082fa9e4066Sahrens 		 * obtained a new reference on this znode while we were
1083fa9e4066Sahrens 		 * processing it here, so we are done.  If we still have
1084fa9e4066Sahrens 		 * mapped pages then we are also done, since we don't
1085fa9e4066Sahrens 		 * want to inactivate the znode until the pages get pushed.
1086fa9e4066Sahrens 		 *
1087fa9e4066Sahrens 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1088fa9e4066Sahrens 		 * this seems like it would leave the znode hanging with
1089fa9e4066Sahrens 		 * no chance to go inactive...
1090fa9e4066Sahrens 		 */
1091fa9e4066Sahrens 		mutex_exit(&vp->v_lock);
1092fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1093fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1094fa9e4066Sahrens 		return;
1095fa9e4066Sahrens 	}
1096fa9e4066Sahrens 	mutex_exit(&vp->v_lock);
1097fa9e4066Sahrens 
1098fa9e4066Sahrens 	/*
1099fa9e4066Sahrens 	 * If this was the last reference to a file with no links,
1100fa9e4066Sahrens 	 * remove the file from the file system.
1101fa9e4066Sahrens 	 */
1102893a6d32Sahrens 	if (zp->z_unlinked) {
1103fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1104fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1105893a6d32Sahrens 		zfs_rmnode(zp);
1106fa9e4066Sahrens 		return;
1107fa9e4066Sahrens 	}
1108fa9e4066Sahrens 	mutex_exit(&zp->z_lock);
11094ccbb6e7Sahrens 	zfs_znode_dmu_fini(zp);
1110fa9e4066Sahrens 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1111874395d5Smaybee 	zfs_znode_free(zp);
1112fa9e4066Sahrens }
1113fa9e4066Sahrens 
1114fa9e4066Sahrens void
1115fa9e4066Sahrens zfs_znode_free(znode_t *zp)
1116fa9e4066Sahrens {
1117fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1118fa9e4066Sahrens 
1119874395d5Smaybee 	vn_invalid(ZTOV(zp));
1120874395d5Smaybee 
1121*b5fca8f8Stomee 	ASSERT(ZTOV(zp)->v_count == 0);
1122*b5fca8f8Stomee 
1123fa9e4066Sahrens 	mutex_enter(&zfsvfs->z_znodes_lock);
1124*b5fca8f8Stomee 	POINTER_INVALIDATE(&zp->z_zfsvfs);
1125fa9e4066Sahrens 	list_remove(&zfsvfs->z_all_znodes, zp);
1126fa9e4066Sahrens 	mutex_exit(&zfsvfs->z_znodes_lock);
1127fa9e4066Sahrens 
1128fa9e4066Sahrens 	kmem_cache_free(znode_cache, zp);
1129874395d5Smaybee 
1130874395d5Smaybee 	VFS_RELE(zfsvfs->z_vfs);
1131fa9e4066Sahrens }
1132fa9e4066Sahrens 
1133fa9e4066Sahrens void
1134fa9e4066Sahrens zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1135fa9e4066Sahrens {
1136fa9e4066Sahrens 	timestruc_t	now;
1137fa9e4066Sahrens 
1138fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zp->z_lock));
1139fa9e4066Sahrens 
1140fa9e4066Sahrens 	gethrestime(&now);
1141fa9e4066Sahrens 
1142fa9e4066Sahrens 	if (tx) {
1143fa9e4066Sahrens 		dmu_buf_will_dirty(zp->z_dbuf, tx);
1144fa9e4066Sahrens 		zp->z_atime_dirty = 0;
1145fa9e4066Sahrens 		zp->z_seq++;
1146fa9e4066Sahrens 	} else {
1147fa9e4066Sahrens 		zp->z_atime_dirty = 1;
1148fa9e4066Sahrens 	}
1149fa9e4066Sahrens 
1150fa9e4066Sahrens 	if (flag & AT_ATIME)
1151fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
1152fa9e4066Sahrens 
1153da6c28aaSamw 	if (flag & AT_MTIME) {
1154fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
1155da6c28aaSamw 		if (zp->z_zfsvfs->z_use_fuids)
1156da6c28aaSamw 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
1157da6c28aaSamw 	}
1158fa9e4066Sahrens 
1159da6c28aaSamw 	if (flag & AT_CTIME) {
1160fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
1161da6c28aaSamw 		if (zp->z_zfsvfs->z_use_fuids)
1162da6c28aaSamw 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
1163da6c28aaSamw 	}
1164fa9e4066Sahrens }
1165fa9e4066Sahrens 
1166fa9e4066Sahrens /*
1167fa9e4066Sahrens  * Update the requested znode timestamps with the current time.
1168fa9e4066Sahrens  * If we are in a transaction, then go ahead and mark the znode
1169fa9e4066Sahrens  * dirty in the transaction so the timestamps will go to disk.
1170fa9e4066Sahrens  * Otherwise, we will get pushed next time the znode is updated
1171fa9e4066Sahrens  * in a transaction, or when this znode eventually goes inactive.
1172fa9e4066Sahrens  *
1173fa9e4066Sahrens  * Why is this OK?
1174fa9e4066Sahrens  *  1 - Only the ACCESS time is ever updated outside of a transaction.
1175fa9e4066Sahrens  *  2 - Multiple consecutive updates will be collapsed into a single
1176fa9e4066Sahrens  *	znode update by the transaction grouping semantics of the DMU.
1177fa9e4066Sahrens  */
1178fa9e4066Sahrens void
1179fa9e4066Sahrens zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1180fa9e4066Sahrens {
1181fa9e4066Sahrens 	mutex_enter(&zp->z_lock);
1182fa9e4066Sahrens 	zfs_time_stamper_locked(zp, flag, tx);
1183fa9e4066Sahrens 	mutex_exit(&zp->z_lock);
1184fa9e4066Sahrens }
1185fa9e4066Sahrens 
1186fa9e4066Sahrens /*
1187104e2ed7Sperrin  * Grow the block size for a file.
1188fa9e4066Sahrens  *
1189fa9e4066Sahrens  *	IN:	zp	- znode of file to free data in.
1190fa9e4066Sahrens  *		size	- requested block size
1191fa9e4066Sahrens  *		tx	- open transaction.
1192fa9e4066Sahrens  *
1193fa9e4066Sahrens  * NOTE: this function assumes that the znode is write locked.
1194fa9e4066Sahrens  */
1195104e2ed7Sperrin void
1196fa9e4066Sahrens zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1197fa9e4066Sahrens {
1198fa9e4066Sahrens 	int		error;
1199fa9e4066Sahrens 	u_longlong_t	dummy;
1200fa9e4066Sahrens 
1201fa9e4066Sahrens 	if (size <= zp->z_blksz)
1202104e2ed7Sperrin 		return;
1203fa9e4066Sahrens 	/*
1204fa9e4066Sahrens 	 * If the file size is already greater than the current blocksize,
1205fa9e4066Sahrens 	 * we will not grow.  If there is more than one block in a file,
1206fa9e4066Sahrens 	 * the blocksize cannot change.
1207fa9e4066Sahrens 	 */
1208fa9e4066Sahrens 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1209104e2ed7Sperrin 		return;
1210fa9e4066Sahrens 
1211fa9e4066Sahrens 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1212fa9e4066Sahrens 	    size, 0, tx);
1213fa9e4066Sahrens 	if (error == ENOTSUP)
1214104e2ed7Sperrin 		return;
1215fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
1216fa9e4066Sahrens 
1217fa9e4066Sahrens 	/* What blocksize did we actually get? */
1218fa9e4066Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1219fa9e4066Sahrens }
1220fa9e4066Sahrens 
1221fa9e4066Sahrens /*
1222fa9e4066Sahrens  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1223fa9e4066Sahrens  * be calling back into the fs for a putpage().  E.g.: when truncating
1224fa9e4066Sahrens  * a file, the pages being "thrown away* don't need to be written out.
1225fa9e4066Sahrens  */
1226fa9e4066Sahrens /* ARGSUSED */
1227fa9e4066Sahrens static int
1228fa9e4066Sahrens zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1229fa9e4066Sahrens     int flags, cred_t *cr)
1230fa9e4066Sahrens {
1231fa9e4066Sahrens 	ASSERT(0);
1232fa9e4066Sahrens 	return (0);
1233fa9e4066Sahrens }
1234fa9e4066Sahrens 
1235fa9e4066Sahrens /*
1236104e2ed7Sperrin  * Free space in a file.
1237fa9e4066Sahrens  *
1238fa9e4066Sahrens  *	IN:	zp	- znode of file to free data in.
12395730cc9aSmaybee  *		off	- start of section to free.
1240fa9e4066Sahrens  *		len	- length of section to free (0 => to EOF).
1241fa9e4066Sahrens  *		flag	- current file open mode flags.
1242fa9e4066Sahrens  *
1243fa9e4066Sahrens  * 	RETURN:	0 if success
1244fa9e4066Sahrens  *		error code if failure
1245fa9e4066Sahrens  */
1246fa9e4066Sahrens int
12475730cc9aSmaybee zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1248fa9e4066Sahrens {
1249fa9e4066Sahrens 	vnode_t *vp = ZTOV(zp);
12505730cc9aSmaybee 	dmu_tx_t *tx;
12515730cc9aSmaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
12525730cc9aSmaybee 	zilog_t *zilog = zfsvfs->z_log;
12535730cc9aSmaybee 	rl_t *rl;
12545730cc9aSmaybee 	uint64_t end = off + len;
12555730cc9aSmaybee 	uint64_t size, new_blksz;
1256da6c28aaSamw 	uint64_t pflags = zp->z_phys->zp_flags;
1257104e2ed7Sperrin 	int error;
1258fa9e4066Sahrens 
1259da6c28aaSamw 	if ((pflags & (ZFS_IMMUTABLE|ZFS_READONLY)) ||
1260da6c28aaSamw 	    off < zp->z_phys->zp_size && (pflags & ZFS_APPENDONLY))
1261da6c28aaSamw 		return (EPERM);
1262da6c28aaSamw 
12638ef9dde0Smarks 	if (ZTOV(zp)->v_type == VFIFO)
12648ef9dde0Smarks 		return (0);
12658ef9dde0Smarks 
12665730cc9aSmaybee 	/*
12675730cc9aSmaybee 	 * If we will change zp_size then lock the whole file,
12685730cc9aSmaybee 	 * otherwise just lock the range being freed.
12695730cc9aSmaybee 	 */
12705730cc9aSmaybee 	if (len == 0 || off + len > zp->z_phys->zp_size) {
12715730cc9aSmaybee 		rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
12725730cc9aSmaybee 	} else {
12735730cc9aSmaybee 		rl = zfs_range_lock(zp, off, len, RL_WRITER);
12745730cc9aSmaybee 		/* recheck, in case zp_size changed */
12755730cc9aSmaybee 		if (off + len > zp->z_phys->zp_size) {
12765730cc9aSmaybee 			/* lost race: file size changed, lock whole file */
1277c5c6ffa0Smaybee 			zfs_range_unlock(rl);
12785730cc9aSmaybee 			rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
12795730cc9aSmaybee 		}
12805730cc9aSmaybee 	}
12815730cc9aSmaybee 
1282fa9e4066Sahrens 	/*
1283fa9e4066Sahrens 	 * Nothing to do if file already at desired length.
1284fa9e4066Sahrens 	 */
12855730cc9aSmaybee 	size = zp->z_phys->zp_size;
128675c76197Speteh 	if (len == 0 && size == off && off != 0) {
1287c5c6ffa0Smaybee 		zfs_range_unlock(rl);
1288fa9e4066Sahrens 		return (0);
1289fa9e4066Sahrens 	}
1290fa9e4066Sahrens 
1291fa9e4066Sahrens 	/*
1292fa9e4066Sahrens 	 * Check for any locks in the region to be freed.
1293fa9e4066Sahrens 	 */
1294fa9e4066Sahrens 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
12955730cc9aSmaybee 		uint64_t start = off;
12965730cc9aSmaybee 		uint64_t extent = len;
1297fa9e4066Sahrens 
12985730cc9aSmaybee 		if (off > size) {
1299fa9e4066Sahrens 			start = size;
13005730cc9aSmaybee 			extent += off - size;
13015730cc9aSmaybee 		} else if (len == 0) {
13025730cc9aSmaybee 			extent = size - off;
13035730cc9aSmaybee 		}
13045730cc9aSmaybee 		if (error = chklock(vp, FWRITE, start, extent, flag, NULL)) {
1305c5c6ffa0Smaybee 			zfs_range_unlock(rl);
1306fa9e4066Sahrens 			return (error);
13075730cc9aSmaybee 		}
1308fa9e4066Sahrens 	}
1309fa9e4066Sahrens 
13105730cc9aSmaybee 	tx = dmu_tx_create(zfsvfs->z_os);
13115730cc9aSmaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
13125730cc9aSmaybee 	new_blksz = 0;
13135730cc9aSmaybee 	if (end > size &&
13145730cc9aSmaybee 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1315fa9e4066Sahrens 		/*
1316fa9e4066Sahrens 		 * We are growing the file past the current block size.
1317fa9e4066Sahrens 		 */
1318fa9e4066Sahrens 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1319fa9e4066Sahrens 			ASSERT(!ISP2(zp->z_blksz));
1320fa9e4066Sahrens 			new_blksz = MIN(end, SPA_MAXBLOCKSIZE);
1321fa9e4066Sahrens 		} else {
1322fa9e4066Sahrens 			new_blksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1323fa9e4066Sahrens 		}
13245730cc9aSmaybee 		dmu_tx_hold_write(tx, zp->z_id, 0, MIN(end, new_blksz));
13255730cc9aSmaybee 	} else if (off < size) {
13265730cc9aSmaybee 		/*
13275730cc9aSmaybee 		 * If len == 0, we are truncating the file.
13285730cc9aSmaybee 		 */
13295730cc9aSmaybee 		dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END);
13305730cc9aSmaybee 	}
13315730cc9aSmaybee 
13325730cc9aSmaybee 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
13335730cc9aSmaybee 	if (error) {
13348a2f1b91Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
13358a2f1b91Sahrens 			dmu_tx_wait(tx);
13365730cc9aSmaybee 		dmu_tx_abort(tx);
1337c5c6ffa0Smaybee 		zfs_range_unlock(rl);
13385730cc9aSmaybee 		return (error);
1339fa9e4066Sahrens 	}
13405730cc9aSmaybee 
13415730cc9aSmaybee 	if (new_blksz)
13425730cc9aSmaybee 		zfs_grow_blocksize(zp, new_blksz, tx);
13435730cc9aSmaybee 
1344fa9e4066Sahrens 	if (end > size || len == 0)
1345fa9e4066Sahrens 		zp->z_phys->zp_size = end;
13465730cc9aSmaybee 
13475730cc9aSmaybee 	if (off < size) {
13485730cc9aSmaybee 		objset_t *os = zfsvfs->z_os;
13495dc8af33Smaybee 		uint64_t rlen = len;
13505730cc9aSmaybee 
13515730cc9aSmaybee 		if (len == 0)
13525dc8af33Smaybee 			rlen = -1;
13535730cc9aSmaybee 		else if (end > size)
13545dc8af33Smaybee 			rlen = size - off;
13555dc8af33Smaybee 		VERIFY(0 == dmu_free_range(os, zp->z_id, off, rlen, tx));
13565730cc9aSmaybee 	}
13575730cc9aSmaybee 
13585730cc9aSmaybee 	if (log) {
13595730cc9aSmaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1360b19a79ecSperrin 		zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
13615730cc9aSmaybee 	}
13625730cc9aSmaybee 
1363c5c6ffa0Smaybee 	zfs_range_unlock(rl);
13645730cc9aSmaybee 
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 	 */
1373fa9e4066Sahrens 	rw_enter(&zp->z_map_lock, RW_WRITER);
13745730cc9aSmaybee 	if (off < size && vn_has_cached_data(vp)) {
1375fa9e4066Sahrens 		page_t *pp;
13765730cc9aSmaybee 		uint64_t start = off & PAGEMASK;
13775730cc9aSmaybee 		int poff = off & PAGEOFFSET;
1378fa9e4066Sahrens 
13795730cc9aSmaybee 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1380fa9e4066Sahrens 			/*
1381fa9e4066Sahrens 			 * We need to zero a partial page.
1382fa9e4066Sahrens 			 */
13835730cc9aSmaybee 			pagezero(pp, poff, PAGESIZE - poff);
1384fa9e4066Sahrens 			start += PAGESIZE;
1385fa9e4066Sahrens 			page_unlock(pp);
1386fa9e4066Sahrens 		}
1387fa9e4066Sahrens 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
13885730cc9aSmaybee 		    B_INVAL | B_TRUNC, NULL);
1389fa9e4066Sahrens 		ASSERT(error == 0);
1390fa9e4066Sahrens 	}
1391fa9e4066Sahrens 	rw_exit(&zp->z_map_lock);
1392fa9e4066Sahrens 
1393fa9e4066Sahrens 	return (0);
1394fa9e4066Sahrens }
1395fa9e4066Sahrens 
1396fa9e4066Sahrens void
1397de8267e0Stimh zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1398fa9e4066Sahrens {
1399fa9e4066Sahrens 	zfsvfs_t	zfsvfs;
14004ccbb6e7Sahrens 	uint64_t	moid, doid;
1401de8267e0Stimh 	uint64_t	version = 0;
1402de8267e0Stimh 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1403de8267e0Stimh 	uint64_t	norm = 0;
1404de8267e0Stimh 	nvpair_t	*elem;
1405fa9e4066Sahrens 	int		error;
1406fa9e4066Sahrens 	znode_t		*rootzp = NULL;
1407fa9e4066Sahrens 	vnode_t		*vp;
1408fa9e4066Sahrens 	vattr_t		vattr;
14094ccbb6e7Sahrens 	znode_t		*zp;
1410fa9e4066Sahrens 
1411fa9e4066Sahrens 	/*
1412fa9e4066Sahrens 	 * First attempt to create master node.
1413fa9e4066Sahrens 	 */
1414ea8dc4b6Seschrock 	/*
1415ea8dc4b6Seschrock 	 * In an empty objset, there are no blocks to read and thus
1416ea8dc4b6Seschrock 	 * there can be no i/o errors (which we assert below).
1417ea8dc4b6Seschrock 	 */
1418fa9e4066Sahrens 	moid = MASTER_NODE_OBJ;
1419fa9e4066Sahrens 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1420fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
1421fa9e4066Sahrens 	ASSERT(error == 0);
1422fa9e4066Sahrens 
1423fa9e4066Sahrens 	/*
1424fa9e4066Sahrens 	 * Set starting attributes.
1425fa9e4066Sahrens 	 */
1426de8267e0Stimh 	elem = NULL;
1427de8267e0Stimh 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1428de8267e0Stimh 		/* For the moment we expect all zpl props to be uint64_ts */
1429de8267e0Stimh 		uint64_t val;
1430de8267e0Stimh 		char *name;
1431de8267e0Stimh 
1432de8267e0Stimh 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1433b3d911cbStimh 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1434de8267e0Stimh 		name = nvpair_name(elem);
1435de8267e0Stimh 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1436de8267e0Stimh 			version = val;
1437de8267e0Stimh 			error = zap_update(os, moid, ZPL_VERSION_STR,
1438de8267e0Stimh 			    8, 1, &version, tx);
1439de8267e0Stimh 		} else {
1440de8267e0Stimh 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1441de8267e0Stimh 		}
1442de8267e0Stimh 		ASSERT(error == 0);
1443de8267e0Stimh 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1444de8267e0Stimh 			norm = val;
1445de8267e0Stimh 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1446de8267e0Stimh 			sense = val;
1447de8267e0Stimh 	}
1448de8267e0Stimh 	ASSERT(version != 0);
1449fa9e4066Sahrens 
1450fa9e4066Sahrens 	/*
1451fa9e4066Sahrens 	 * Create a delete queue.
1452fa9e4066Sahrens 	 */
1453893a6d32Sahrens 	doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1454fa9e4066Sahrens 
1455893a6d32Sahrens 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx);
1456fa9e4066Sahrens 	ASSERT(error == 0);
1457fa9e4066Sahrens 
1458fa9e4066Sahrens 	/*
1459fa9e4066Sahrens 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1460fa9e4066Sahrens 	 * to allow zfs_mknode to work.
1461fa9e4066Sahrens 	 */
1462fa9e4066Sahrens 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1463fa9e4066Sahrens 	vattr.va_type = VDIR;
1464fa9e4066Sahrens 	vattr.va_mode = S_IFDIR|0755;
1465ecd6cf80Smarks 	vattr.va_uid = crgetuid(cr);
1466ecd6cf80Smarks 	vattr.va_gid = crgetgid(cr);
1467fa9e4066Sahrens 
1468fa9e4066Sahrens 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1469893a6d32Sahrens 	rootzp->z_unlinked = 0;
1470fa9e4066Sahrens 	rootzp->z_atime_dirty = 0;
1471fa9e4066Sahrens 
1472fa9e4066Sahrens 	vp = ZTOV(rootzp);
1473fa9e4066Sahrens 	vn_reinit(vp);
1474fa9e4066Sahrens 	vp->v_type = VDIR;
1475fa9e4066Sahrens 
1476fa9e4066Sahrens 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1477fa9e4066Sahrens 
1478fa9e4066Sahrens 	zfsvfs.z_os = os;
1479fa9e4066Sahrens 	zfsvfs.z_assign = TXG_NOWAIT;
1480fa9e4066Sahrens 	zfsvfs.z_parent = &zfsvfs;
1481da6c28aaSamw 	zfsvfs.z_version = version;
1482da6c28aaSamw 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1483da6c28aaSamw 	zfsvfs.z_norm = norm;
1484de8267e0Stimh 	/*
1485de8267e0Stimh 	 * Fold case on file systems that are always or sometimes case
1486de8267e0Stimh 	 * insensitive.
1487de8267e0Stimh 	 */
1488de8267e0Stimh 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1489de8267e0Stimh 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1490fa9e4066Sahrens 
1491fa9e4066Sahrens 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1492fa9e4066Sahrens 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1493fa9e4066Sahrens 	    offsetof(znode_t, z_link_node));
1494fa9e4066Sahrens 
1495*b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1496*b5fca8f8Stomee 	rootzp->z_zfsvfs = &zfsvfs;
1497874395d5Smaybee 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, NULL, NULL);
1498874395d5Smaybee 	ASSERT3P(zp, ==, rootzp);
1499*b5fca8f8Stomee 	ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
15004ccbb6e7Sahrens 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1501fa9e4066Sahrens 	ASSERT(error == 0);
1502*b5fca8f8Stomee 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1503fa9e4066Sahrens 
1504fa9e4066Sahrens 	ZTOV(rootzp)->v_count = 0;
1505874395d5Smaybee 	dmu_buf_rele(rootzp->z_dbuf, NULL);
1506874395d5Smaybee 	rootzp->z_dbuf = NULL;
1507fa9e4066Sahrens 	kmem_cache_free(znode_cache, rootzp);
1508fa9e4066Sahrens }
150955434c77Sek 
1510da6c28aaSamw #endif /* _KERNEL */
151155434c77Sek /*
151255434c77Sek  * Given an object number, return its parent object number and whether
151355434c77Sek  * or not the object is an extended attribute directory.
151455434c77Sek  */
151555434c77Sek static int
151655434c77Sek zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
151755434c77Sek {
151855434c77Sek 	dmu_buf_t *db;
151955434c77Sek 	dmu_object_info_t doi;
152055434c77Sek 	znode_phys_t *zp;
152155434c77Sek 	int error;
152255434c77Sek 
152355434c77Sek 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
152455434c77Sek 		return (error);
152555434c77Sek 
152655434c77Sek 	dmu_object_info_from_db(db, &doi);
152755434c77Sek 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
152855434c77Sek 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
152955434c77Sek 		dmu_buf_rele(db, FTAG);
153055434c77Sek 		return (EINVAL);
153155434c77Sek 	}
153255434c77Sek 
153355434c77Sek 	zp = db->db_data;
153455434c77Sek 	*pobjp = zp->zp_parent;
153555434c77Sek 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
153655434c77Sek 	    S_ISDIR(zp->zp_mode);
153755434c77Sek 	dmu_buf_rele(db, FTAG);
153855434c77Sek 
153955434c77Sek 	return (0);
154055434c77Sek }
154155434c77Sek 
154255434c77Sek int
154355434c77Sek zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
154455434c77Sek {
154555434c77Sek 	char *path = buf + len - 1;
154655434c77Sek 	int error;
154755434c77Sek 
154855434c77Sek 	*path = '\0';
154955434c77Sek 
155055434c77Sek 	for (;;) {
155155434c77Sek 		uint64_t pobj;
155255434c77Sek 		char component[MAXNAMELEN + 2];
155355434c77Sek 		size_t complen;
155455434c77Sek 		int is_xattrdir;
155555434c77Sek 
155655434c77Sek 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
155755434c77Sek 		    &is_xattrdir)) != 0)
155855434c77Sek 			break;
155955434c77Sek 
156055434c77Sek 		if (pobj == obj) {
156155434c77Sek 			if (path[0] != '/')
156255434c77Sek 				*--path = '/';
156355434c77Sek 			break;
156455434c77Sek 		}
156555434c77Sek 
156655434c77Sek 		component[0] = '/';
156755434c77Sek 		if (is_xattrdir) {
156855434c77Sek 			(void) sprintf(component + 1, "<xattrdir>");
156955434c77Sek 		} else {
1570e7437265Sahrens 			error = zap_value_search(osp, pobj, obj,
1571e7437265Sahrens 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
157255434c77Sek 			if (error != 0)
157355434c77Sek 				break;
157455434c77Sek 		}
157555434c77Sek 
157655434c77Sek 		complen = strlen(component);
157755434c77Sek 		path -= complen;
157855434c77Sek 		ASSERT(path >= buf);
157955434c77Sek 		bcopy(component, path, complen);
158055434c77Sek 		obj = pobj;
158155434c77Sek 	}
158255434c77Sek 
158355434c77Sek 	if (error == 0)
158455434c77Sek 		(void) memmove(buf, path, buf + len - path);
158555434c77Sek 	return (error);
158655434c77Sek }
1587