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