xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_znode.c (revision 9e1320c015cea6985d2122bc1d654b79fa479f7a)
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
90b5fca8f8Stomee static kmem_cache_t *znode_cache = NULL;
91fa9e4066Sahrens 
92fa9e4066Sahrens /*ARGSUSED*/
93fa9e4066Sahrens static void
94874395d5Smaybee znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
95fa9e4066Sahrens {
96874395d5Smaybee 	/*
97874395d5Smaybee 	 * We should never drop all dbuf refs without first clearing
98874395d5Smaybee 	 * the eviction callback.
99874395d5Smaybee 	 */
100874395d5Smaybee 	panic("evicting znode %p\n", user_ptr);
101fa9e4066Sahrens }
102fa9e4066Sahrens 
103fa9e4066Sahrens /*ARGSUSED*/
104fa9e4066Sahrens static int
105b5fca8f8Stomee zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
106fa9e4066Sahrens {
107fa9e4066Sahrens 	znode_t *zp = buf;
108fa9e4066Sahrens 
109b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
110b5fca8f8Stomee 
111b5fca8f8Stomee 	zp->z_vnode = vn_alloc(kmflags);
112b5fca8f8Stomee 	if (zp->z_vnode == NULL) {
113b5fca8f8Stomee 		return (-1);
114b5fca8f8Stomee 	}
115b5fca8f8Stomee 	ZTOV(zp)->v_data = zp;
116b5fca8f8Stomee 
117b5fca8f8Stomee 	list_link_init(&zp->z_link_node);
118b5fca8f8Stomee 
119fa9e4066Sahrens 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
120104e2ed7Sperrin 	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
121af2c4821Smaybee 	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
122fa9e4066Sahrens 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
123104e2ed7Sperrin 
124104e2ed7Sperrin 	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
125104e2ed7Sperrin 	avl_create(&zp->z_range_avl, zfs_range_compare,
126104e2ed7Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
127104e2ed7Sperrin 
1284ccbb6e7Sahrens 	zp->z_dbuf = NULL;
129b5fca8f8Stomee 	zp->z_dirlocks = NULL;
130fa9e4066Sahrens 	return (0);
131fa9e4066Sahrens }
132fa9e4066Sahrens 
133fa9e4066Sahrens /*ARGSUSED*/
134fa9e4066Sahrens static void
135b5fca8f8Stomee zfs_znode_cache_destructor(void *buf, void *arg)
136fa9e4066Sahrens {
137fa9e4066Sahrens 	znode_t *zp = buf;
138fa9e4066Sahrens 
139b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
140b5fca8f8Stomee 	ASSERT(ZTOV(zp)->v_data == zp);
141b5fca8f8Stomee 	vn_free(ZTOV(zp));
142b5fca8f8Stomee 	ASSERT(!list_link_active(&zp->z_link_node));
143fa9e4066Sahrens 	mutex_destroy(&zp->z_lock);
144104e2ed7Sperrin 	rw_destroy(&zp->z_parent_lock);
145af2c4821Smaybee 	rw_destroy(&zp->z_name_lock);
146fa9e4066Sahrens 	mutex_destroy(&zp->z_acl_lock);
147104e2ed7Sperrin 	avl_destroy(&zp->z_range_avl);
148c25056deSgw 	mutex_destroy(&zp->z_range_lock);
149fa9e4066Sahrens 
1504ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
151b5fca8f8Stomee 	ASSERT(zp->z_dirlocks == NULL);
152b5fca8f8Stomee }
153b5fca8f8Stomee 
154b5fca8f8Stomee #ifdef	ZNODE_STATS
155b5fca8f8Stomee static struct {
156b5fca8f8Stomee 	uint64_t zms_zfsvfs_invalid;
157b5fca8f8Stomee 	uint64_t zms_zfsvfs_unmounted;
158b5fca8f8Stomee 	uint64_t zms_zfsvfs_recheck_invalid;
159a66b2b35STom Erickson 	uint64_t zms_obj_held;
160b5fca8f8Stomee 	uint64_t zms_vnode_locked;
161a66b2b35STom Erickson 	uint64_t zms_not_only_dnlc;
162b5fca8f8Stomee } znode_move_stats;
163b5fca8f8Stomee #endif	/* ZNODE_STATS */
164b5fca8f8Stomee 
165b5fca8f8Stomee static void
166b5fca8f8Stomee zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
167b5fca8f8Stomee {
168b5fca8f8Stomee 	vnode_t *vp;
169b5fca8f8Stomee 
170b5fca8f8Stomee 	/* Copy fields. */
171b5fca8f8Stomee 	nzp->z_zfsvfs = ozp->z_zfsvfs;
172b5fca8f8Stomee 
173b5fca8f8Stomee 	/* Swap vnodes. */
174b5fca8f8Stomee 	vp = nzp->z_vnode;
175b5fca8f8Stomee 	nzp->z_vnode = ozp->z_vnode;
176b5fca8f8Stomee 	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
177b5fca8f8Stomee 	ZTOV(ozp)->v_data = ozp;
178b5fca8f8Stomee 	ZTOV(nzp)->v_data = nzp;
179b5fca8f8Stomee 
180b5fca8f8Stomee 	nzp->z_id = ozp->z_id;
181b5fca8f8Stomee 	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
182b5fca8f8Stomee 	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
183b5fca8f8Stomee 	nzp->z_unlinked = ozp->z_unlinked;
184b5fca8f8Stomee 	nzp->z_atime_dirty = ozp->z_atime_dirty;
185b5fca8f8Stomee 	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
186b5fca8f8Stomee 	nzp->z_blksz = ozp->z_blksz;
187b5fca8f8Stomee 	nzp->z_seq = ozp->z_seq;
188b5fca8f8Stomee 	nzp->z_mapcnt = ozp->z_mapcnt;
189b5fca8f8Stomee 	nzp->z_last_itx = ozp->z_last_itx;
190b5fca8f8Stomee 	nzp->z_gen = ozp->z_gen;
191b5fca8f8Stomee 	nzp->z_sync_cnt = ozp->z_sync_cnt;
192b5fca8f8Stomee 	nzp->z_phys = ozp->z_phys;
193b5fca8f8Stomee 	nzp->z_dbuf = ozp->z_dbuf;
194b5fca8f8Stomee 
195b5fca8f8Stomee 	/* Update back pointers. */
196b5fca8f8Stomee 	(void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys,
197b5fca8f8Stomee 	    znode_evict_error);
198b5fca8f8Stomee 
199b5fca8f8Stomee 	/*
200b5fca8f8Stomee 	 * Invalidate the original znode by clearing fields that provide a
201b5fca8f8Stomee 	 * pointer back to the znode. Set the low bit of the vfs pointer to
202b5fca8f8Stomee 	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
203b5fca8f8Stomee 	 * subsequent callback.
204b5fca8f8Stomee 	 */
205b5fca8f8Stomee 	ozp->z_dbuf = NULL;
206b5fca8f8Stomee 	POINTER_INVALIDATE(&ozp->z_zfsvfs);
207b5fca8f8Stomee }
208b5fca8f8Stomee 
209b5fca8f8Stomee /*
210b5fca8f8Stomee  * Wrapper function for ZFS_ENTER that returns 0 if successful and otherwise
211b5fca8f8Stomee  * returns a non-zero error code.
212b5fca8f8Stomee  */
213b5fca8f8Stomee static int
214b5fca8f8Stomee zfs_enter(zfsvfs_t *zfsvfs)
215b5fca8f8Stomee {
216b5fca8f8Stomee 	ZFS_ENTER(zfsvfs);
217b5fca8f8Stomee 	return (0);
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 	/*
243b5fca8f8Stomee 	 * Ensure that the filesystem is not unmounted during the move.
244b5fca8f8Stomee 	 */
245b5fca8f8Stomee 	if (zfs_enter(zfsvfs) != 0) {		/* ZFS_ENTER */
246b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
247b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
248b5fca8f8Stomee 	}
249b5fca8f8Stomee 
250b5fca8f8Stomee 	mutex_enter(&zfsvfs->z_znodes_lock);
251b5fca8f8Stomee 	/*
252b5fca8f8Stomee 	 * Recheck the vfs pointer in case the znode was removed just before
253b5fca8f8Stomee 	 * acquiring the lock.
254b5fca8f8Stomee 	 */
255b5fca8f8Stomee 	if (zfsvfs != ozp->z_zfsvfs) {
256b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
257b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
258b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck_invalid);
259b5fca8f8Stomee 		return (KMEM_CBRC_DONT_KNOW);
260b5fca8f8Stomee 	}
261b5fca8f8Stomee 
262b5fca8f8Stomee 	/*
263b5fca8f8Stomee 	 * At this point we know that as long as we hold z_znodes_lock, the
264b5fca8f8Stomee 	 * znode cannot be freed and fields within the znode can be safely
265a66b2b35STom Erickson 	 * accessed. Now, prevent a race with zfs_zget().
266b5fca8f8Stomee 	 */
267a66b2b35STom Erickson 	if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
268a66b2b35STom Erickson 		mutex_exit(&zfsvfs->z_znodes_lock);
269a66b2b35STom Erickson 		ZFS_EXIT(zfsvfs);
270a66b2b35STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
271a66b2b35STom Erickson 		return (KMEM_CBRC_LATER);
272a66b2b35STom Erickson 	}
273a66b2b35STom Erickson 
274b5fca8f8Stomee 	vp = ZTOV(ozp);
275b5fca8f8Stomee 	if (mutex_tryenter(&vp->v_lock) == 0) {
276a66b2b35STom Erickson 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
277b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
278b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
279b5fca8f8Stomee 		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
280b5fca8f8Stomee 		return (KMEM_CBRC_LATER);
281b5fca8f8Stomee 	}
282a66b2b35STom Erickson 
283b5fca8f8Stomee 	/* Only move znodes that are referenced _only_ by the DNLC. */
284b5fca8f8Stomee 	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
285b5fca8f8Stomee 		mutex_exit(&vp->v_lock);
286a66b2b35STom Erickson 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
287b5fca8f8Stomee 		mutex_exit(&zfsvfs->z_znodes_lock);
288b5fca8f8Stomee 		ZFS_EXIT(zfsvfs);
289a66b2b35STom Erickson 		ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
290b5fca8f8Stomee 		return (KMEM_CBRC_LATER);
291b5fca8f8Stomee 	}
292b5fca8f8Stomee 
293b5fca8f8Stomee 	/*
294b5fca8f8Stomee 	 * The znode is known and in a valid state to move. We're holding the
295b5fca8f8Stomee 	 * locks needed to execute the critical section.
296b5fca8f8Stomee 	 */
297b5fca8f8Stomee 	zfs_znode_move_impl(ozp, nzp);
298b5fca8f8Stomee 	mutex_exit(&vp->v_lock);
299a66b2b35STom Erickson 	ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
300b5fca8f8Stomee 
301b5fca8f8Stomee 	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
302b5fca8f8Stomee 	mutex_exit(&zfsvfs->z_znodes_lock);
303b5fca8f8Stomee 	ZFS_EXIT(zfsvfs);
304b5fca8f8Stomee 
305b5fca8f8Stomee 	return (KMEM_CBRC_YES);
306fa9e4066Sahrens }
307fa9e4066Sahrens 
308fa9e4066Sahrens void
309fa9e4066Sahrens zfs_znode_init(void)
310fa9e4066Sahrens {
311fa9e4066Sahrens 	/*
312fa9e4066Sahrens 	 * Initialize zcache
313fa9e4066Sahrens 	 */
314fa9e4066Sahrens 	ASSERT(znode_cache == NULL);
315fa9e4066Sahrens 	znode_cache = kmem_cache_create("zfs_znode_cache",
316fa9e4066Sahrens 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
317fa9e4066Sahrens 	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
318b5fca8f8Stomee 	kmem_cache_set_move(znode_cache, zfs_znode_move);
319fa9e4066Sahrens }
320fa9e4066Sahrens 
321fa9e4066Sahrens void
322fa9e4066Sahrens zfs_znode_fini(void)
323fa9e4066Sahrens {
324fa9e4066Sahrens 	/*
325fa9e4066Sahrens 	 * Cleanup vfs & vnode ops
326fa9e4066Sahrens 	 */
327fa9e4066Sahrens 	zfs_remove_op_tables();
328fa9e4066Sahrens 
329fa9e4066Sahrens 	/*
330fa9e4066Sahrens 	 * Cleanup zcache
331fa9e4066Sahrens 	 */
332fa9e4066Sahrens 	if (znode_cache)
333fa9e4066Sahrens 		kmem_cache_destroy(znode_cache);
334fa9e4066Sahrens 	znode_cache = NULL;
335fa9e4066Sahrens }
336fa9e4066Sahrens 
337fa9e4066Sahrens struct vnodeops *zfs_dvnodeops;
338fa9e4066Sahrens struct vnodeops *zfs_fvnodeops;
339fa9e4066Sahrens struct vnodeops *zfs_symvnodeops;
340fa9e4066Sahrens struct vnodeops *zfs_xdvnodeops;
341fa9e4066Sahrens struct vnodeops *zfs_evnodeops;
342743a77edSAlan Wright struct vnodeops *zfs_sharevnodeops;
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);
367743a77edSAlan Wright 	if (zfs_sharevnodeops)
368743a77edSAlan Wright 		vn_freevnodeops(zfs_sharevnodeops);
369fa9e4066Sahrens 
370fa9e4066Sahrens 	zfs_dvnodeops = NULL;
371fa9e4066Sahrens 	zfs_fvnodeops = NULL;
372fa9e4066Sahrens 	zfs_symvnodeops = NULL;
373fa9e4066Sahrens 	zfs_xdvnodeops = NULL;
374fa9e4066Sahrens 	zfs_evnodeops = NULL;
375743a77edSAlan Wright 	zfs_sharevnodeops = NULL;
376fa9e4066Sahrens }
377fa9e4066Sahrens 
378fa9e4066Sahrens extern const fs_operation_def_t zfs_dvnodeops_template[];
379fa9e4066Sahrens extern const fs_operation_def_t zfs_fvnodeops_template[];
380fa9e4066Sahrens extern const fs_operation_def_t zfs_xdvnodeops_template[];
381fa9e4066Sahrens extern const fs_operation_def_t zfs_symvnodeops_template[];
382fa9e4066Sahrens extern const fs_operation_def_t zfs_evnodeops_template[];
383743a77edSAlan Wright extern const fs_operation_def_t zfs_sharevnodeops_template[];
384fa9e4066Sahrens 
385fa9e4066Sahrens int
386fa9e4066Sahrens zfs_create_op_tables()
387fa9e4066Sahrens {
388fa9e4066Sahrens 	int error;
389fa9e4066Sahrens 
390fa9e4066Sahrens 	/*
391fa9e4066Sahrens 	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
392fa9e4066Sahrens 	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
393fa9e4066Sahrens 	 * In this case we just return as the ops vectors are already set up.
394fa9e4066Sahrens 	 */
395fa9e4066Sahrens 	if (zfs_dvnodeops)
396fa9e4066Sahrens 		return (0);
397fa9e4066Sahrens 
398fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
399fa9e4066Sahrens 	    &zfs_dvnodeops);
400fa9e4066Sahrens 	if (error)
401fa9e4066Sahrens 		return (error);
402fa9e4066Sahrens 
403fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
404fa9e4066Sahrens 	    &zfs_fvnodeops);
405fa9e4066Sahrens 	if (error)
406fa9e4066Sahrens 		return (error);
407fa9e4066Sahrens 
408fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
409fa9e4066Sahrens 	    &zfs_symvnodeops);
410fa9e4066Sahrens 	if (error)
411fa9e4066Sahrens 		return (error);
412fa9e4066Sahrens 
413fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
414fa9e4066Sahrens 	    &zfs_xdvnodeops);
415fa9e4066Sahrens 	if (error)
416fa9e4066Sahrens 		return (error);
417fa9e4066Sahrens 
418fa9e4066Sahrens 	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
419fa9e4066Sahrens 	    &zfs_evnodeops);
420743a77edSAlan Wright 	if (error)
421743a77edSAlan Wright 		return (error);
422743a77edSAlan Wright 
423743a77edSAlan Wright 	error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
424743a77edSAlan Wright 	    &zfs_sharevnodeops);
425743a77edSAlan Wright 
426743a77edSAlan Wright 	return (error);
427743a77edSAlan Wright }
428743a77edSAlan Wright 
429*9e1320c0SMark Shellenbaum int
430743a77edSAlan Wright zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
431743a77edSAlan Wright {
432743a77edSAlan Wright 	vattr_t vattr;
433743a77edSAlan Wright 	znode_t *sharezp;
434743a77edSAlan Wright 	vnode_t *vp;
435743a77edSAlan Wright 	znode_t *zp;
436743a77edSAlan Wright 	int error;
437743a77edSAlan Wright 
438743a77edSAlan Wright 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
439743a77edSAlan Wright 	vattr.va_type = VDIR;
440743a77edSAlan Wright 	vattr.va_mode = S_IFDIR|0555;
441743a77edSAlan Wright 	vattr.va_uid = crgetuid(kcred);
442743a77edSAlan Wright 	vattr.va_gid = crgetgid(kcred);
443743a77edSAlan Wright 
444743a77edSAlan Wright 	sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
445743a77edSAlan Wright 	sharezp->z_unlinked = 0;
446743a77edSAlan Wright 	sharezp->z_atime_dirty = 0;
447743a77edSAlan Wright 	sharezp->z_zfsvfs = zfsvfs;
448743a77edSAlan Wright 
449743a77edSAlan Wright 	vp = ZTOV(sharezp);
450743a77edSAlan Wright 	vn_reinit(vp);
451743a77edSAlan Wright 	vp->v_type = VDIR;
452743a77edSAlan Wright 
453743a77edSAlan Wright 	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE,
454743a77edSAlan Wright 	    &zp, 0, NULL, NULL);
455743a77edSAlan Wright 	ASSERT3P(zp, ==, sharezp);
456743a77edSAlan Wright 	ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
457743a77edSAlan Wright 	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
458743a77edSAlan Wright 	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
459743a77edSAlan Wright 	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
460743a77edSAlan Wright 	zfsvfs->z_shares_dir = sharezp->z_id;
461743a77edSAlan Wright 
462743a77edSAlan Wright 	ZTOV(sharezp)->v_count = 0;
463743a77edSAlan Wright 	dmu_buf_rele(sharezp->z_dbuf, NULL);
464743a77edSAlan Wright 	sharezp->z_dbuf = NULL;
465743a77edSAlan Wright 	kmem_cache_free(znode_cache, sharezp);
466fa9e4066Sahrens 
467fa9e4066Sahrens 	return (error);
468fa9e4066Sahrens }
469fa9e4066Sahrens 
470fa9e4066Sahrens /*
471fa9e4066Sahrens  * zfs_init_fs - Initialize the zfsvfs struct and the file system
472fa9e4066Sahrens  *	incore "master" object.  Verify version compatibility.
473fa9e4066Sahrens  */
474fa9e4066Sahrens int
475088f3894Sahrens zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp)
476fa9e4066Sahrens {
477fa9e4066Sahrens 	extern int zfsfstype;
478fa9e4066Sahrens 
479fa9e4066Sahrens 	objset_t	*os = zfsvfs->z_os;
480fa9e4066Sahrens 	int		i, error;
481a2eea2e1Sahrens 	uint64_t fsid_guid;
482de8267e0Stimh 	uint64_t zval;
483fa9e4066Sahrens 
484fa9e4066Sahrens 	*zpp = NULL;
485fa9e4066Sahrens 
486de8267e0Stimh 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
487ea8dc4b6Seschrock 	if (error) {
488ea8dc4b6Seschrock 		return (error);
489e7437265Sahrens 	} else if (zfsvfs->z_version > ZPL_VERSION) {
490fa9e4066Sahrens 		(void) printf("Mismatched versions:  File system "
491b3d911cbStimh 		    "is version %llu on-disk format, which is "
492fa9e4066Sahrens 		    "incompatible with this software version %lld!",
493e7437265Sahrens 		    (u_longlong_t)zfsvfs->z_version, ZPL_VERSION);
494fa9e4066Sahrens 		return (ENOTSUP);
495fa9e4066Sahrens 	}
496fa9e4066Sahrens 
497de8267e0Stimh 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
498de8267e0Stimh 		return (error);
499de8267e0Stimh 	zfsvfs->z_norm = (int)zval;
500de8267e0Stimh 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
501de8267e0Stimh 		return (error);
502de8267e0Stimh 	zfsvfs->z_utf8 = (zval != 0);
503de8267e0Stimh 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
504de8267e0Stimh 		return (error);
505de8267e0Stimh 	zfsvfs->z_case = (uint_t)zval;
506de8267e0Stimh 	/*
507de8267e0Stimh 	 * Fold case on file systems that are always or sometimes case
508de8267e0Stimh 	 * insensitive.
509de8267e0Stimh 	 */
510de8267e0Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
511de8267e0Stimh 	    zfsvfs->z_case == ZFS_CASE_MIXED)
512de8267e0Stimh 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
513de8267e0Stimh 
514fa9e4066Sahrens 	/*
515fa9e4066Sahrens 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
516fa9e4066Sahrens 	 * separates our fsid from any other filesystem types, and a
517fa9e4066Sahrens 	 * 56-bit objset unique ID.  The objset unique ID is unique to
518fa9e4066Sahrens 	 * all objsets open on this system, provided by unique_create().
519fa9e4066Sahrens 	 * The 8-bit fs type must be put in the low bits of fsid[1]
520fa9e4066Sahrens 	 * because that's where other Solaris filesystems put it.
521fa9e4066Sahrens 	 */
522a2eea2e1Sahrens 	fsid_guid = dmu_objset_fsid_guid(os);
523a2eea2e1Sahrens 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
524a2eea2e1Sahrens 	zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid;
525a2eea2e1Sahrens 	zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
526fa9e4066Sahrens 	    zfsfstype & 0xFF;
527fa9e4066Sahrens 
528893a6d32Sahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
529893a6d32Sahrens 	    &zfsvfs->z_root);
530ea8dc4b6Seschrock 	if (error)
531ea8dc4b6Seschrock 		return (error);
532893a6d32Sahrens 	ASSERT(zfsvfs->z_root != 0);
533fa9e4066Sahrens 
534c25056deSgw 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
535c25056deSgw 	    &zfsvfs->z_unlinkedobj);
536c25056deSgw 	if (error)
537c25056deSgw 		return (error);
538c25056deSgw 
539*9e1320c0SMark Shellenbaum 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
540*9e1320c0SMark Shellenbaum 	    &zfsvfs->z_fuid_obj);
541*9e1320c0SMark Shellenbaum 	if (error == ENOENT)
542*9e1320c0SMark Shellenbaum 		error = 0;
543*9e1320c0SMark Shellenbaum 
544*9e1320c0SMark Shellenbaum 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
545*9e1320c0SMark Shellenbaum 	    &zfsvfs->z_shares_dir);
546*9e1320c0SMark Shellenbaum 	if (error && error != ENOENT)
547*9e1320c0SMark Shellenbaum 		return (error);
548*9e1320c0SMark Shellenbaum 
549fa9e4066Sahrens 	/*
550fa9e4066Sahrens 	 * Initialize zget mutex's
551fa9e4066Sahrens 	 */
552fa9e4066Sahrens 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
553fa9e4066Sahrens 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
554fa9e4066Sahrens 
555893a6d32Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp);
556c25056deSgw 	if (error) {
557c25056deSgw 		/*
558c25056deSgw 		 * On error, we destroy the mutexes here since it's not
559c25056deSgw 		 * possible for the caller to determine if the mutexes were
560c25056deSgw 		 * initialized properly.
561c25056deSgw 		 */
562c25056deSgw 		for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
563c25056deSgw 			mutex_destroy(&zfsvfs->z_hold_mtx[i]);
564fa9e4066Sahrens 		return (error);
565c25056deSgw 	}
566893a6d32Sahrens 	ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root);
567da6c28aaSamw 
568743a77edSAlan Wright 	return (error);
569fa9e4066Sahrens }
570fa9e4066Sahrens 
57172fc53bcSmarks /*
57272fc53bcSmarks  * define a couple of values we need available
57372fc53bcSmarks  * for both 64 and 32 bit environments.
57472fc53bcSmarks  */
57572fc53bcSmarks #ifndef NBITSMINOR64
57672fc53bcSmarks #define	NBITSMINOR64	32
57772fc53bcSmarks #endif
57872fc53bcSmarks #ifndef MAXMAJ64
57972fc53bcSmarks #define	MAXMAJ64	0xffffffffUL
58072fc53bcSmarks #endif
58172fc53bcSmarks #ifndef	MAXMIN64
58272fc53bcSmarks #define	MAXMIN64	0xffffffffUL
58372fc53bcSmarks #endif
58472fc53bcSmarks 
58572fc53bcSmarks /*
58672fc53bcSmarks  * Create special expldev for ZFS private use.
58772fc53bcSmarks  * Can't use standard expldev since it doesn't do
58872fc53bcSmarks  * what we want.  The standard expldev() takes a
58972fc53bcSmarks  * dev32_t in LP64 and expands it to a long dev_t.
59072fc53bcSmarks  * We need an interface that takes a dev32_t in ILP32
59172fc53bcSmarks  * and expands it to a long dev_t.
59272fc53bcSmarks  */
59372fc53bcSmarks static uint64_t
59472fc53bcSmarks zfs_expldev(dev_t dev)
59572fc53bcSmarks {
59672fc53bcSmarks #ifndef _LP64
59772fc53bcSmarks 	major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
59872fc53bcSmarks 	return (((uint64_t)major << NBITSMINOR64) |
59972fc53bcSmarks 	    ((minor_t)dev & MAXMIN32));
60072fc53bcSmarks #else
60172fc53bcSmarks 	return (dev);
60272fc53bcSmarks #endif
60372fc53bcSmarks }
60472fc53bcSmarks 
60572fc53bcSmarks /*
60672fc53bcSmarks  * Special cmpldev for ZFS private use.
60772fc53bcSmarks  * Can't use standard cmpldev since it takes
60872fc53bcSmarks  * a long dev_t and compresses it to dev32_t in
60972fc53bcSmarks  * LP64.  We need to do a compaction of a long dev_t
61072fc53bcSmarks  * to a dev32_t in ILP32.
61172fc53bcSmarks  */
61272fc53bcSmarks dev_t
61372fc53bcSmarks zfs_cmpldev(uint64_t dev)
61472fc53bcSmarks {
61572fc53bcSmarks #ifndef _LP64
61672fc53bcSmarks 	minor_t minor = (minor_t)dev & MAXMIN64;
61772fc53bcSmarks 	major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
61872fc53bcSmarks 
61972fc53bcSmarks 	if (major > MAXMAJ32 || minor > MAXMIN32)
62072fc53bcSmarks 		return (NODEV32);
62172fc53bcSmarks 
62272fc53bcSmarks 	return (((dev32_t)major << NBITSMINOR32) | minor);
62372fc53bcSmarks #else
62472fc53bcSmarks 	return (dev);
62572fc53bcSmarks #endif
62672fc53bcSmarks }
62772fc53bcSmarks 
6284ccbb6e7Sahrens static void
629b5fca8f8Stomee zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
6304ccbb6e7Sahrens {
6314ccbb6e7Sahrens 	znode_t		*nzp;
6324ccbb6e7Sahrens 
633b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
634b5fca8f8Stomee 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
6354ccbb6e7Sahrens 
6364ccbb6e7Sahrens 	mutex_enter(&zp->z_lock);
6374ccbb6e7Sahrens 
6384ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
6394ccbb6e7Sahrens 	zp->z_dbuf = db;
640874395d5Smaybee 	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error);
6414ccbb6e7Sahrens 
6424ccbb6e7Sahrens 	/*
6434ccbb6e7Sahrens 	 * there should be no
6444ccbb6e7Sahrens 	 * concurrent zgets on this object.
6454ccbb6e7Sahrens 	 */
6464ccbb6e7Sahrens 	if (nzp != NULL)
647903a11ebSrh 		panic("existing znode %p for dbuf %p", (void *)nzp, (void *)db);
6484ccbb6e7Sahrens 
6494ccbb6e7Sahrens 	/*
6504ccbb6e7Sahrens 	 * Slap on VROOT if we are the root znode
6514ccbb6e7Sahrens 	 */
6524ccbb6e7Sahrens 	if (zp->z_id == zfsvfs->z_root)
6534ccbb6e7Sahrens 		ZTOV(zp)->v_flag |= VROOT;
6544ccbb6e7Sahrens 
6554ccbb6e7Sahrens 	mutex_exit(&zp->z_lock);
6564ccbb6e7Sahrens 	vn_exists(ZTOV(zp));
6574ccbb6e7Sahrens }
6584ccbb6e7Sahrens 
659874395d5Smaybee void
6604ccbb6e7Sahrens zfs_znode_dmu_fini(znode_t *zp)
6614ccbb6e7Sahrens {
6624ccbb6e7Sahrens 	dmu_buf_t *db = zp->z_dbuf;
663b5fca8f8Stomee 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
664b5fca8f8Stomee 	    zp->z_unlinked ||
665874395d5Smaybee 	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
6664ccbb6e7Sahrens 	ASSERT(zp->z_dbuf != NULL);
6674ccbb6e7Sahrens 	zp->z_dbuf = NULL;
668874395d5Smaybee 	VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL));
6694ccbb6e7Sahrens 	dmu_buf_rele(db, NULL);
6704ccbb6e7Sahrens }
6714ccbb6e7Sahrens 
672fa9e4066Sahrens /*
673fa9e4066Sahrens  * Construct a new znode/vnode and intialize.
674fa9e4066Sahrens  *
675fa9e4066Sahrens  * This does not do a call to dmu_set_user() that is
676fa9e4066Sahrens  * up to the caller to do, in case you don't want to
677fa9e4066Sahrens  * return the znode
678fa9e4066Sahrens  */
679ea8dc4b6Seschrock static znode_t *
6804ccbb6e7Sahrens zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
681fa9e4066Sahrens {
682fa9e4066Sahrens 	znode_t	*zp;
683fa9e4066Sahrens 	vnode_t *vp;
684fa9e4066Sahrens 
685fa9e4066Sahrens 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
686fa9e4066Sahrens 
687fa9e4066Sahrens 	ASSERT(zp->z_dirlocks == NULL);
6884ccbb6e7Sahrens 	ASSERT(zp->z_dbuf == NULL);
689b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
690fa9e4066Sahrens 
691b5fca8f8Stomee 	/*
692b5fca8f8Stomee 	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
693b5fca8f8Stomee 	 * the zfs_znode_move() callback.
694b5fca8f8Stomee 	 */
6954ccbb6e7Sahrens 	zp->z_phys = NULL;
696893a6d32Sahrens 	zp->z_unlinked = 0;
697fa9e4066Sahrens 	zp->z_atime_dirty = 0;
698fa9e4066Sahrens 	zp->z_mapcnt = 0;
699fa9e4066Sahrens 	zp->z_last_itx = 0;
7004ccbb6e7Sahrens 	zp->z_id = db->db_object;
701fa9e4066Sahrens 	zp->z_blksz = blksz;
702fa9e4066Sahrens 	zp->z_seq = 0x7A4653;
70367bd71c6Sperrin 	zp->z_sync_cnt = 0;
7044ccbb6e7Sahrens 
7054ccbb6e7Sahrens 	vp = ZTOV(zp);
7064ccbb6e7Sahrens 	vn_reinit(vp);
7074ccbb6e7Sahrens 
708b5fca8f8Stomee 	zfs_znode_dmu_init(zfsvfs, zp, db);
7094ccbb6e7Sahrens 
710f18faf3fSek 	zp->z_gen = zp->z_phys->zp_gen;
711fa9e4066Sahrens 
712fa9e4066Sahrens 	vp->v_vfsp = zfsvfs->z_parent->z_vfs;
713fa9e4066Sahrens 	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
714fa9e4066Sahrens 
715fa9e4066Sahrens 	switch (vp->v_type) {
716fa9e4066Sahrens 	case VDIR:
717fa9e4066Sahrens 		if (zp->z_phys->zp_flags & ZFS_XATTR) {
718fa9e4066Sahrens 			vn_setops(vp, zfs_xdvnodeops);
719fa9e4066Sahrens 			vp->v_flag |= V_XATTRDIR;
7204ccbb6e7Sahrens 		} else {
721fa9e4066Sahrens 			vn_setops(vp, zfs_dvnodeops);
7224ccbb6e7Sahrens 		}
7237f6e3e7dSperrin 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
724fa9e4066Sahrens 		break;
725fa9e4066Sahrens 	case VBLK:
726fa9e4066Sahrens 	case VCHR:
72772fc53bcSmarks 		vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
728fa9e4066Sahrens 		/*FALLTHROUGH*/
729fa9e4066Sahrens 	case VFIFO:
730fa9e4066Sahrens 	case VSOCK:
731fa9e4066Sahrens 	case VDOOR:
732fa9e4066Sahrens 		vn_setops(vp, zfs_fvnodeops);
733fa9e4066Sahrens 		break;
734fa9e4066Sahrens 	case VREG:
735fa9e4066Sahrens 		vp->v_flag |= VMODSORT;
736743a77edSAlan Wright 		if (zp->z_phys->zp_parent == zfsvfs->z_shares_dir)
737743a77edSAlan Wright 			vn_setops(vp, zfs_sharevnodeops);
738743a77edSAlan Wright 		else
739743a77edSAlan Wright 			vn_setops(vp, zfs_fvnodeops);
740fa9e4066Sahrens 		break;
741fa9e4066Sahrens 	case VLNK:
742fa9e4066Sahrens 		vn_setops(vp, zfs_symvnodeops);
743fa9e4066Sahrens 		break;
744fa9e4066Sahrens 	default:
745fa9e4066Sahrens 		vn_setops(vp, zfs_evnodeops);
746fa9e4066Sahrens 		break;
747fa9e4066Sahrens 	}
748fa9e4066Sahrens 
749b5fca8f8Stomee 	mutex_enter(&zfsvfs->z_znodes_lock);
750b5fca8f8Stomee 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
751b5fca8f8Stomee 	membar_producer();
752b5fca8f8Stomee 	/*
753b5fca8f8Stomee 	 * Everything else must be valid before assigning z_zfsvfs makes the
754b5fca8f8Stomee 	 * znode eligible for zfs_znode_move().
755b5fca8f8Stomee 	 */
756b5fca8f8Stomee 	zp->z_zfsvfs = zfsvfs;
757b5fca8f8Stomee 	mutex_exit(&zfsvfs->z_znodes_lock);
758b5fca8f8Stomee 
759874395d5Smaybee 	VFS_HOLD(zfsvfs->z_vfs);
760fa9e4066Sahrens 	return (zp);
761fa9e4066Sahrens }
762fa9e4066Sahrens 
763fa9e4066Sahrens /*
764fa9e4066Sahrens  * Create a new DMU object to hold a zfs znode.
765fa9e4066Sahrens  *
766fa9e4066Sahrens  *	IN:	dzp	- parent directory for new znode
767fa9e4066Sahrens  *		vap	- file attributes for new znode
768fa9e4066Sahrens  *		tx	- dmu transaction id for zap operations
769fa9e4066Sahrens  *		cr	- credentials of caller
770fa9e4066Sahrens  *		flag	- flags:
771fa9e4066Sahrens  *			  IS_ROOT_NODE	- new object will be root
772fa9e4066Sahrens  *			  IS_XATTR	- new object is an attribute
773fa9e4066Sahrens  *			  IS_REPLAY	- intent log replay
774da6c28aaSamw  *		bonuslen - length of bonus buffer
775da6c28aaSamw  *		setaclp  - File/Dir initial ACL
776da6c28aaSamw  *		fuidp	 - Tracks fuid allocation.
777fa9e4066Sahrens  *
7784ccbb6e7Sahrens  *	OUT:	zpp	- allocated znode
779fa9e4066Sahrens  *
780fa9e4066Sahrens  */
781fa9e4066Sahrens void
7824ccbb6e7Sahrens zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
783da6c28aaSamw     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_t *setaclp,
784da6c28aaSamw     zfs_fuid_info_t **fuidp)
785fa9e4066Sahrens {
7864ccbb6e7Sahrens 	dmu_buf_t	*db;
787fa9e4066Sahrens 	znode_phys_t	*pzp;
788fa9e4066Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
789fa9e4066Sahrens 	timestruc_t	now;
7904ccbb6e7Sahrens 	uint64_t	gen, obj;
791fa9e4066Sahrens 	int		err;
792fa9e4066Sahrens 
793fa9e4066Sahrens 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
794fa9e4066Sahrens 
7951209a471SNeil Perrin 	if (zfsvfs->z_replay) {
7964ccbb6e7Sahrens 		obj = vap->va_nodeid;
797fa9e4066Sahrens 		flag |= IS_REPLAY;
798fa9e4066Sahrens 		now = vap->va_ctime;		/* see zfs_replay_create() */
799fa9e4066Sahrens 		gen = vap->va_nblocks;		/* ditto */
800fa9e4066Sahrens 	} else {
8014ccbb6e7Sahrens 		obj = 0;
802fa9e4066Sahrens 		gethrestime(&now);
803fa9e4066Sahrens 		gen = dmu_tx_get_txg(tx);
804fa9e4066Sahrens 	}
805fa9e4066Sahrens 
806fa9e4066Sahrens 	/*
807fa9e4066Sahrens 	 * Create a new DMU object.
808fa9e4066Sahrens 	 */
809ea8dc4b6Seschrock 	/*
810ea8dc4b6Seschrock 	 * There's currently no mechanism for pre-reading the blocks that will
811ea8dc4b6Seschrock 	 * be to needed allocate a new object, so we accept the small chance
812ea8dc4b6Seschrock 	 * that there will be an i/o error and we will fail one of the
813ea8dc4b6Seschrock 	 * assertions below.
814ea8dc4b6Seschrock 	 */
815fa9e4066Sahrens 	if (vap->va_type == VDIR) {
816fa9e4066Sahrens 		if (flag & IS_REPLAY) {
8174ccbb6e7Sahrens 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
818da6c28aaSamw 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
819fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
820fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
821fa9e4066Sahrens 		} else {
8224ccbb6e7Sahrens 			obj = zap_create_norm(zfsvfs->z_os,
823da6c28aaSamw 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
824fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
825fa9e4066Sahrens 		}
826fa9e4066Sahrens 	} else {
827fa9e4066Sahrens 		if (flag & IS_REPLAY) {
8284ccbb6e7Sahrens 			err = dmu_object_claim(zfsvfs->z_os, obj,
829fa9e4066Sahrens 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
830fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
831fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
832fa9e4066Sahrens 		} else {
8334ccbb6e7Sahrens 			obj = dmu_object_alloc(zfsvfs->z_os,
834fa9e4066Sahrens 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
835fa9e4066Sahrens 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
836fa9e4066Sahrens 		}
837fa9e4066Sahrens 	}
8384ccbb6e7Sahrens 	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
8394ccbb6e7Sahrens 	dmu_buf_will_dirty(db, tx);
840fa9e4066Sahrens 
841fa9e4066Sahrens 	/*
842fa9e4066Sahrens 	 * Initialize the znode physical data to zero.
843fa9e4066Sahrens 	 */
8444ccbb6e7Sahrens 	ASSERT(db->db_size >= sizeof (znode_phys_t));
8454ccbb6e7Sahrens 	bzero(db->db_data, db->db_size);
8464ccbb6e7Sahrens 	pzp = db->db_data;
847fa9e4066Sahrens 
848fa9e4066Sahrens 	/*
849fa9e4066Sahrens 	 * If this is the root, fix up the half-initialized parent pointer
850fa9e4066Sahrens 	 * to reference the just-allocated physical data area.
851fa9e4066Sahrens 	 */
852fa9e4066Sahrens 	if (flag & IS_ROOT_NODE) {
853874395d5Smaybee 		dzp->z_dbuf = db;
854fa9e4066Sahrens 		dzp->z_phys = pzp;
8554ccbb6e7Sahrens 		dzp->z_id = obj;
856fa9e4066Sahrens 	}
857fa9e4066Sahrens 
858fa9e4066Sahrens 	/*
859fa9e4066Sahrens 	 * If parent is an xattr, so am I.
860fa9e4066Sahrens 	 */
861fa9e4066Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR)
862fa9e4066Sahrens 		flag |= IS_XATTR;
863fa9e4066Sahrens 
864fa9e4066Sahrens 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
86572fc53bcSmarks 		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
866fa9e4066Sahrens 	}
867fa9e4066Sahrens 
868da6c28aaSamw 	if (zfsvfs->z_use_fuids)
869da6c28aaSamw 		pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
870da6c28aaSamw 
871fa9e4066Sahrens 	if (vap->va_type == VDIR) {
872fa9e4066Sahrens 		pzp->zp_size = 2;		/* contents ("." and "..") */
873fa9e4066Sahrens 		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
874fa9e4066Sahrens 	}
875fa9e4066Sahrens 
876fa9e4066Sahrens 	pzp->zp_parent = dzp->z_id;
877fa9e4066Sahrens 	if (flag & IS_XATTR)
878fa9e4066Sahrens 		pzp->zp_flags |= ZFS_XATTR;
879fa9e4066Sahrens 
880fa9e4066Sahrens 	pzp->zp_gen = gen;
881fa9e4066Sahrens 
882fa9e4066Sahrens 	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
883fa9e4066Sahrens 	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
884fa9e4066Sahrens 
885fa9e4066Sahrens 	if (vap->va_mask & AT_ATIME) {
886fa9e4066Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
887fa9e4066Sahrens 	} else {
888fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
889fa9e4066Sahrens 	}
890fa9e4066Sahrens 
891fa9e4066Sahrens 	if (vap->va_mask & AT_MTIME) {
892fa9e4066Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
893fa9e4066Sahrens 	} else {
894fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
895fa9e4066Sahrens 	}
896fa9e4066Sahrens 
897fa9e4066Sahrens 	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
898874395d5Smaybee 	if (!(flag & IS_ROOT_NODE)) {
899b5fca8f8Stomee 		ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
900874395d5Smaybee 		*zpp = zfs_znode_alloc(zfsvfs, db, 0);
901874395d5Smaybee 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
902874395d5Smaybee 	} else {
903874395d5Smaybee 		/*
904874395d5Smaybee 		 * If we are creating the root node, the "parent" we
905874395d5Smaybee 		 * passed in is the znode for the root.
906874395d5Smaybee 		 */
907874395d5Smaybee 		*zpp = dzp;
908874395d5Smaybee 	}
9094ccbb6e7Sahrens 	zfs_perm_init(*zpp, dzp, flag, vap, tx, cr, setaclp, fuidp);
910fa9e4066Sahrens }
911fa9e4066Sahrens 
912da6c28aaSamw void
913da6c28aaSamw zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
914da6c28aaSamw {
915da6c28aaSamw 	xoptattr_t *xoap;
916da6c28aaSamw 
917da6c28aaSamw 	xoap = xva_getxoptattr(xvap);
918da6c28aaSamw 	ASSERT(xoap);
919da6c28aaSamw 
920da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
921da6c28aaSamw 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
922da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_CREATETIME);
923da6c28aaSamw 	}
924da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
925da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
926da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_READONLY);
927da6c28aaSamw 	}
928da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
929da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
930da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_HIDDEN);
931da6c28aaSamw 	}
932da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
933da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
934da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_SYSTEM);
935da6c28aaSamw 	}
936da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
937da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
938da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
939da6c28aaSamw 	}
940da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
941da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
942da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
943da6c28aaSamw 	}
944da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
945da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
946da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
947da6c28aaSamw 	}
948da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
949da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
950da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
951da6c28aaSamw 	}
952da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
953da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
954da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_NODUMP);
955da6c28aaSamw 	}
956da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
957da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
958da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_OPAQUE);
959da6c28aaSamw 	}
960da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
961da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
962da6c28aaSamw 		    xoap->xoa_av_quarantined);
963da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
964da6c28aaSamw 	}
965da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
966da6c28aaSamw 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
967da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
968da6c28aaSamw 	}
969da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
970da6c28aaSamw 		(void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
971da6c28aaSamw 		    sizeof (xoap->xoa_av_scanstamp));
972da6c28aaSamw 		zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
973da6c28aaSamw 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
974da6c28aaSamw 	}
975da6c28aaSamw }
976da6c28aaSamw 
977fa9e4066Sahrens int
978fa9e4066Sahrens zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
979fa9e4066Sahrens {
980fa9e4066Sahrens 	dmu_object_info_t doi;
981fa9e4066Sahrens 	dmu_buf_t	*db;
982fa9e4066Sahrens 	znode_t		*zp;
983ea8dc4b6Seschrock 	int err;
984fa9e4066Sahrens 
985fa9e4066Sahrens 	*zpp = NULL;
986fa9e4066Sahrens 
987fa9e4066Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
988fa9e4066Sahrens 
989ea8dc4b6Seschrock 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
990ea8dc4b6Seschrock 	if (err) {
991fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
992ea8dc4b6Seschrock 		return (err);
993fa9e4066Sahrens 	}
994fa9e4066Sahrens 
995fa9e4066Sahrens 	dmu_object_info_from_db(db, &doi);
996fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
997fa9e4066Sahrens 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
998ea8dc4b6Seschrock 		dmu_buf_rele(db, NULL);
999fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1000fa9e4066Sahrens 		return (EINVAL);
1001fa9e4066Sahrens 	}
1002fa9e4066Sahrens 
1003fa9e4066Sahrens 	zp = dmu_buf_get_user(db);
1004fa9e4066Sahrens 	if (zp != NULL) {
1005fa9e4066Sahrens 		mutex_enter(&zp->z_lock);
1006fa9e4066Sahrens 
10074ccbb6e7Sahrens 		/*
10084ccbb6e7Sahrens 		 * Since we do immediate eviction of the z_dbuf, we
10094ccbb6e7Sahrens 		 * should never find a dbuf with a znode that doesn't
10104ccbb6e7Sahrens 		 * know about the dbuf.
10114ccbb6e7Sahrens 		 */
10124ccbb6e7Sahrens 		ASSERT3P(zp->z_dbuf, ==, db);
1013fa9e4066Sahrens 		ASSERT3U(zp->z_id, ==, obj_num);
1014893a6d32Sahrens 		if (zp->z_unlinked) {
10154ccbb6e7Sahrens 			err = ENOENT;
1016fa9e4066Sahrens 		} else {
10174ccbb6e7Sahrens 			VN_HOLD(ZTOV(zp));
10184ccbb6e7Sahrens 			*zpp = zp;
10194ccbb6e7Sahrens 			err = 0;
1020fa9e4066Sahrens 		}
10214ccbb6e7Sahrens 		dmu_buf_rele(db, NULL);
1022fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1023ea8dc4b6Seschrock 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
10244ccbb6e7Sahrens 		return (err);
1025fa9e4066Sahrens 	}
1026fa9e4066Sahrens 
1027fa9e4066Sahrens 	/*
1028fa9e4066Sahrens 	 * Not found create new znode/vnode
1029fa9e4066Sahrens 	 */
10304ccbb6e7Sahrens 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
1031ea8dc4b6Seschrock 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1032fa9e4066Sahrens 	*zpp = zp;
1033fa9e4066Sahrens 	return (0);
1034fa9e4066Sahrens }
1035fa9e4066Sahrens 
1036f18faf3fSek int
1037f18faf3fSek zfs_rezget(znode_t *zp)
1038f18faf3fSek {
1039f18faf3fSek 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1040f18faf3fSek 	dmu_object_info_t doi;
1041f18faf3fSek 	dmu_buf_t *db;
1042f18faf3fSek 	uint64_t obj_num = zp->z_id;
1043f18faf3fSek 	int err;
1044f18faf3fSek 
1045f18faf3fSek 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1046f18faf3fSek 
1047f18faf3fSek 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
1048f18faf3fSek 	if (err) {
1049f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1050f18faf3fSek 		return (err);
1051f18faf3fSek 	}
1052f18faf3fSek 
1053f18faf3fSek 	dmu_object_info_from_db(db, &doi);
1054f18faf3fSek 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1055f18faf3fSek 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1056f18faf3fSek 		dmu_buf_rele(db, NULL);
1057f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1058f18faf3fSek 		return (EINVAL);
1059f18faf3fSek 	}
1060f18faf3fSek 
1061f18faf3fSek 	if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
1062f18faf3fSek 		dmu_buf_rele(db, NULL);
1063f18faf3fSek 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1064f18faf3fSek 		return (EIO);
1065f18faf3fSek 	}
1066f18faf3fSek 
1067b5fca8f8Stomee 	zfs_znode_dmu_init(zfsvfs, zp, db);
1068f18faf3fSek 	zp->z_unlinked = (zp->z_phys->zp_links == 0);
10696166ad1cSek 	zp->z_blksz = doi.doi_data_block_size;
1070f18faf3fSek 
1071f18faf3fSek 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1072f18faf3fSek 
1073f18faf3fSek 	return (0);
1074f18faf3fSek }
1075f18faf3fSek 
1076fa9e4066Sahrens void
1077fa9e4066Sahrens zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1078fa9e4066Sahrens {
1079fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1080cdb0ab79Smaybee 	objset_t *os = zfsvfs->z_os;
10814ccbb6e7Sahrens 	uint64_t obj = zp->z_id;
1082cdb0ab79Smaybee 	uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
1083fa9e4066Sahrens 
10844ccbb6e7Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1085cdb0ab79Smaybee 	if (acl_obj)
1086cdb0ab79Smaybee 		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1087cdb0ab79Smaybee 	VERIFY(0 == dmu_object_free(os, obj, tx));
10884ccbb6e7Sahrens 	zfs_znode_dmu_fini(zp);
10894ccbb6e7Sahrens 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1090874395d5Smaybee 	zfs_znode_free(zp);
1091fa9e4066Sahrens }
1092fa9e4066Sahrens 
1093fa9e4066Sahrens void
1094fa9e4066Sahrens zfs_zinactive(znode_t *zp)
1095fa9e4066Sahrens {
1096fa9e4066Sahrens 	vnode_t	*vp = ZTOV(zp);
1097fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1098fa9e4066Sahrens 	uint64_t z_id = zp->z_id;
1099fa9e4066Sahrens 
11004ccbb6e7Sahrens 	ASSERT(zp->z_dbuf && zp->z_phys);
1101fa9e4066Sahrens 
1102fa9e4066Sahrens 	/*
1103fa9e4066Sahrens 	 * Don't allow a zfs_zget() while were trying to release this znode
1104fa9e4066Sahrens 	 */
1105fa9e4066Sahrens 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1106fa9e4066Sahrens 
1107fa9e4066Sahrens 	mutex_enter(&zp->z_lock);
1108fa9e4066Sahrens 	mutex_enter(&vp->v_lock);
1109fa9e4066Sahrens 	vp->v_count--;
1110fa9e4066Sahrens 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1111fa9e4066Sahrens 		/*
1112fa9e4066Sahrens 		 * If the hold count is greater than zero, somebody has
1113fa9e4066Sahrens 		 * obtained a new reference on this znode while we were
1114fa9e4066Sahrens 		 * processing it here, so we are done.  If we still have
1115fa9e4066Sahrens 		 * mapped pages then we are also done, since we don't
1116fa9e4066Sahrens 		 * want to inactivate the znode until the pages get pushed.
1117fa9e4066Sahrens 		 *
1118fa9e4066Sahrens 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1119fa9e4066Sahrens 		 * this seems like it would leave the znode hanging with
1120fa9e4066Sahrens 		 * no chance to go inactive...
1121fa9e4066Sahrens 		 */
1122fa9e4066Sahrens 		mutex_exit(&vp->v_lock);
1123fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1124fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1125fa9e4066Sahrens 		return;
1126fa9e4066Sahrens 	}
1127fa9e4066Sahrens 	mutex_exit(&vp->v_lock);
1128fa9e4066Sahrens 
1129fa9e4066Sahrens 	/*
1130fa9e4066Sahrens 	 * If this was the last reference to a file with no links,
1131fa9e4066Sahrens 	 * remove the file from the file system.
1132fa9e4066Sahrens 	 */
1133893a6d32Sahrens 	if (zp->z_unlinked) {
1134fa9e4066Sahrens 		mutex_exit(&zp->z_lock);
1135fa9e4066Sahrens 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1136893a6d32Sahrens 		zfs_rmnode(zp);
1137fa9e4066Sahrens 		return;
1138fa9e4066Sahrens 	}
1139fa9e4066Sahrens 	mutex_exit(&zp->z_lock);
11404ccbb6e7Sahrens 	zfs_znode_dmu_fini(zp);
1141fa9e4066Sahrens 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1142874395d5Smaybee 	zfs_znode_free(zp);
1143fa9e4066Sahrens }
1144fa9e4066Sahrens 
1145fa9e4066Sahrens void
1146fa9e4066Sahrens zfs_znode_free(znode_t *zp)
1147fa9e4066Sahrens {
1148fa9e4066Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1149fa9e4066Sahrens 
1150874395d5Smaybee 	vn_invalid(ZTOV(zp));
1151874395d5Smaybee 
1152b5fca8f8Stomee 	ASSERT(ZTOV(zp)->v_count == 0);
1153b5fca8f8Stomee 
1154fa9e4066Sahrens 	mutex_enter(&zfsvfs->z_znodes_lock);
1155b5fca8f8Stomee 	POINTER_INVALIDATE(&zp->z_zfsvfs);
1156fa9e4066Sahrens 	list_remove(&zfsvfs->z_all_znodes, zp);
1157fa9e4066Sahrens 	mutex_exit(&zfsvfs->z_znodes_lock);
1158fa9e4066Sahrens 
1159fa9e4066Sahrens 	kmem_cache_free(znode_cache, zp);
1160874395d5Smaybee 
1161874395d5Smaybee 	VFS_RELE(zfsvfs->z_vfs);
1162fa9e4066Sahrens }
1163fa9e4066Sahrens 
1164fa9e4066Sahrens void
1165fa9e4066Sahrens zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1166fa9e4066Sahrens {
1167fa9e4066Sahrens 	timestruc_t	now;
1168fa9e4066Sahrens 
1169fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zp->z_lock));
1170fa9e4066Sahrens 
1171fa9e4066Sahrens 	gethrestime(&now);
1172fa9e4066Sahrens 
1173fa9e4066Sahrens 	if (tx) {
1174fa9e4066Sahrens 		dmu_buf_will_dirty(zp->z_dbuf, tx);
1175fa9e4066Sahrens 		zp->z_atime_dirty = 0;
1176fa9e4066Sahrens 		zp->z_seq++;
1177fa9e4066Sahrens 	} else {
1178fa9e4066Sahrens 		zp->z_atime_dirty = 1;
1179fa9e4066Sahrens 	}
1180fa9e4066Sahrens 
1181fa9e4066Sahrens 	if (flag & AT_ATIME)
1182fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
1183fa9e4066Sahrens 
1184da6c28aaSamw 	if (flag & AT_MTIME) {
1185fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
1186da6c28aaSamw 		if (zp->z_zfsvfs->z_use_fuids)
1187da6c28aaSamw 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
1188da6c28aaSamw 	}
1189fa9e4066Sahrens 
1190da6c28aaSamw 	if (flag & AT_CTIME) {
1191fa9e4066Sahrens 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
1192da6c28aaSamw 		if (zp->z_zfsvfs->z_use_fuids)
1193da6c28aaSamw 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
1194da6c28aaSamw 	}
1195fa9e4066Sahrens }
1196fa9e4066Sahrens 
1197fa9e4066Sahrens /*
1198fa9e4066Sahrens  * Update the requested znode timestamps with the current time.
1199fa9e4066Sahrens  * If we are in a transaction, then go ahead and mark the znode
1200fa9e4066Sahrens  * dirty in the transaction so the timestamps will go to disk.
1201fa9e4066Sahrens  * Otherwise, we will get pushed next time the znode is updated
1202fa9e4066Sahrens  * in a transaction, or when this znode eventually goes inactive.
1203fa9e4066Sahrens  *
1204fa9e4066Sahrens  * Why is this OK?
1205fa9e4066Sahrens  *  1 - Only the ACCESS time is ever updated outside of a transaction.
1206fa9e4066Sahrens  *  2 - Multiple consecutive updates will be collapsed into a single
1207fa9e4066Sahrens  *	znode update by the transaction grouping semantics of the DMU.
1208fa9e4066Sahrens  */
1209fa9e4066Sahrens void
1210fa9e4066Sahrens zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1211fa9e4066Sahrens {
1212fa9e4066Sahrens 	mutex_enter(&zp->z_lock);
1213fa9e4066Sahrens 	zfs_time_stamper_locked(zp, flag, tx);
1214fa9e4066Sahrens 	mutex_exit(&zp->z_lock);
1215fa9e4066Sahrens }
1216fa9e4066Sahrens 
1217fa9e4066Sahrens /*
1218104e2ed7Sperrin  * Grow the block size for a file.
1219fa9e4066Sahrens  *
1220fa9e4066Sahrens  *	IN:	zp	- znode of file to free data in.
1221fa9e4066Sahrens  *		size	- requested block size
1222fa9e4066Sahrens  *		tx	- open transaction.
1223fa9e4066Sahrens  *
1224fa9e4066Sahrens  * NOTE: this function assumes that the znode is write locked.
1225fa9e4066Sahrens  */
1226104e2ed7Sperrin void
1227fa9e4066Sahrens zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1228fa9e4066Sahrens {
1229fa9e4066Sahrens 	int		error;
1230fa9e4066Sahrens 	u_longlong_t	dummy;
1231fa9e4066Sahrens 
1232fa9e4066Sahrens 	if (size <= zp->z_blksz)
1233104e2ed7Sperrin 		return;
1234fa9e4066Sahrens 	/*
1235fa9e4066Sahrens 	 * If the file size is already greater than the current blocksize,
1236fa9e4066Sahrens 	 * we will not grow.  If there is more than one block in a file,
1237fa9e4066Sahrens 	 * the blocksize cannot change.
1238fa9e4066Sahrens 	 */
1239fa9e4066Sahrens 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1240104e2ed7Sperrin 		return;
1241fa9e4066Sahrens 
1242fa9e4066Sahrens 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1243fa9e4066Sahrens 	    size, 0, tx);
1244fa9e4066Sahrens 	if (error == ENOTSUP)
1245104e2ed7Sperrin 		return;
1246fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
1247fa9e4066Sahrens 
1248fa9e4066Sahrens 	/* What blocksize did we actually get? */
1249fa9e4066Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1250fa9e4066Sahrens }
1251fa9e4066Sahrens 
1252fa9e4066Sahrens /*
1253fa9e4066Sahrens  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1254fa9e4066Sahrens  * be calling back into the fs for a putpage().  E.g.: when truncating
1255fa9e4066Sahrens  * a file, the pages being "thrown away* don't need to be written out.
1256fa9e4066Sahrens  */
1257fa9e4066Sahrens /* ARGSUSED */
1258fa9e4066Sahrens static int
1259fa9e4066Sahrens zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1260fa9e4066Sahrens     int flags, cred_t *cr)
1261fa9e4066Sahrens {
1262fa9e4066Sahrens 	ASSERT(0);
1263fa9e4066Sahrens 	return (0);
1264fa9e4066Sahrens }
1265fa9e4066Sahrens 
1266fa9e4066Sahrens /*
1267cdb0ab79Smaybee  * Increase the file length
1268fa9e4066Sahrens  *
1269fa9e4066Sahrens  *	IN:	zp	- znode of file to free data in.
1270cdb0ab79Smaybee  *		end	- new end-of-file
1271fa9e4066Sahrens  *
1272fa9e4066Sahrens  * 	RETURN:	0 if success
1273fa9e4066Sahrens  *		error code if failure
1274fa9e4066Sahrens  */
1275cdb0ab79Smaybee static int
1276cdb0ab79Smaybee zfs_extend(znode_t *zp, uint64_t end)
1277fa9e4066Sahrens {
12785730cc9aSmaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1279cdb0ab79Smaybee 	dmu_tx_t *tx;
12805730cc9aSmaybee 	rl_t *rl;
1281cdb0ab79Smaybee 	uint64_t newblksz;
1282104e2ed7Sperrin 	int error;
1283fa9e4066Sahrens 
12845730cc9aSmaybee 	/*
1285cdb0ab79Smaybee 	 * We will change zp_size, lock the whole file.
12865730cc9aSmaybee 	 */
1287cdb0ab79Smaybee 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
12885730cc9aSmaybee 
1289fa9e4066Sahrens 	/*
1290fa9e4066Sahrens 	 * Nothing to do if file already at desired length.
1291fa9e4066Sahrens 	 */
1292cdb0ab79Smaybee 	if (end <= zp->z_phys->zp_size) {
1293c5c6ffa0Smaybee 		zfs_range_unlock(rl);
1294fa9e4066Sahrens 		return (0);
1295fa9e4066Sahrens 	}
1296cdb0ab79Smaybee top:
12975730cc9aSmaybee 	tx = dmu_tx_create(zfsvfs->z_os);
12985730cc9aSmaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
1299cdb0ab79Smaybee 	if (end > zp->z_blksz &&
13005730cc9aSmaybee 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1301fa9e4066Sahrens 		/*
1302fa9e4066Sahrens 		 * We are growing the file past the current block size.
1303fa9e4066Sahrens 		 */
1304fa9e4066Sahrens 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1305fa9e4066Sahrens 			ASSERT(!ISP2(zp->z_blksz));
1306cdb0ab79Smaybee 			newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1307fa9e4066Sahrens 		} else {
1308cdb0ab79Smaybee 			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1309fa9e4066Sahrens 		}
1310cdb0ab79Smaybee 		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1311cdb0ab79Smaybee 	} else {
1312cdb0ab79Smaybee 		newblksz = 0;
13135730cc9aSmaybee 	}
13145730cc9aSmaybee 
13151209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
13165730cc9aSmaybee 	if (error) {
13171209a471SNeil Perrin 		if (error == ERESTART) {
13188a2f1b91Sahrens 			dmu_tx_wait(tx);
1319cdb0ab79Smaybee 			dmu_tx_abort(tx);
1320cdb0ab79Smaybee 			goto top;
1321cdb0ab79Smaybee 		}
13225730cc9aSmaybee 		dmu_tx_abort(tx);
1323c5c6ffa0Smaybee 		zfs_range_unlock(rl);
13245730cc9aSmaybee 		return (error);
1325fa9e4066Sahrens 	}
1326cdb0ab79Smaybee 	dmu_buf_will_dirty(zp->z_dbuf, tx);
13275730cc9aSmaybee 
1328cdb0ab79Smaybee 	if (newblksz)
1329cdb0ab79Smaybee 		zfs_grow_blocksize(zp, newblksz, tx);
13305730cc9aSmaybee 
1331cdb0ab79Smaybee 	zp->z_phys->zp_size = end;
13325730cc9aSmaybee 
1333cdb0ab79Smaybee 	zfs_range_unlock(rl);
13345730cc9aSmaybee 
1335cdb0ab79Smaybee 	dmu_tx_commit(tx);
13365730cc9aSmaybee 
1337cdb0ab79Smaybee 	return (0);
1338cdb0ab79Smaybee }
1339cdb0ab79Smaybee 
1340cdb0ab79Smaybee /*
1341cdb0ab79Smaybee  * Free space in a file.
1342cdb0ab79Smaybee  *
1343cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1344cdb0ab79Smaybee  *		off	- start of section to free.
1345cdb0ab79Smaybee  *		len	- length of section to free.
1346cdb0ab79Smaybee  *
1347cdb0ab79Smaybee  * 	RETURN:	0 if success
1348cdb0ab79Smaybee  *		error code if failure
1349cdb0ab79Smaybee  */
1350cdb0ab79Smaybee static int
1351cdb0ab79Smaybee zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1352cdb0ab79Smaybee {
1353cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1354cdb0ab79Smaybee 	rl_t *rl;
1355cdb0ab79Smaybee 	int error;
1356cdb0ab79Smaybee 
1357cdb0ab79Smaybee 	/*
1358cdb0ab79Smaybee 	 * Lock the range being freed.
1359cdb0ab79Smaybee 	 */
1360cdb0ab79Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1361cdb0ab79Smaybee 
1362cdb0ab79Smaybee 	/*
1363cdb0ab79Smaybee 	 * Nothing to do if file already at desired length.
1364cdb0ab79Smaybee 	 */
1365cdb0ab79Smaybee 	if (off >= zp->z_phys->zp_size) {
1366cdb0ab79Smaybee 		zfs_range_unlock(rl);
1367cdb0ab79Smaybee 		return (0);
13685730cc9aSmaybee 	}
13695730cc9aSmaybee 
1370cdb0ab79Smaybee 	if (off + len > zp->z_phys->zp_size)
1371cdb0ab79Smaybee 		len = zp->z_phys->zp_size - off;
1372cdb0ab79Smaybee 
1373cdb0ab79Smaybee 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1374cdb0ab79Smaybee 
1375c5c6ffa0Smaybee 	zfs_range_unlock(rl);
13765730cc9aSmaybee 
1377cdb0ab79Smaybee 	return (error);
1378cdb0ab79Smaybee }
1379cdb0ab79Smaybee 
1380cdb0ab79Smaybee /*
1381cdb0ab79Smaybee  * Truncate a file
1382cdb0ab79Smaybee  *
1383cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1384cdb0ab79Smaybee  *		end	- new end-of-file.
1385cdb0ab79Smaybee  *
1386cdb0ab79Smaybee  * 	RETURN:	0 if success
1387cdb0ab79Smaybee  *		error code if failure
1388cdb0ab79Smaybee  */
1389cdb0ab79Smaybee static int
1390cdb0ab79Smaybee zfs_trunc(znode_t *zp, uint64_t end)
1391cdb0ab79Smaybee {
1392cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1393cdb0ab79Smaybee 	vnode_t *vp = ZTOV(zp);
1394cdb0ab79Smaybee 	dmu_tx_t *tx;
1395cdb0ab79Smaybee 	rl_t *rl;
1396cdb0ab79Smaybee 	int error;
1397cdb0ab79Smaybee 
1398cdb0ab79Smaybee 	/*
1399cdb0ab79Smaybee 	 * We will change zp_size, lock the whole file.
1400cdb0ab79Smaybee 	 */
1401cdb0ab79Smaybee 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1402cdb0ab79Smaybee 
1403cdb0ab79Smaybee 	/*
1404cdb0ab79Smaybee 	 * Nothing to do if file already at desired length.
1405cdb0ab79Smaybee 	 */
1406cdb0ab79Smaybee 	if (end >= zp->z_phys->zp_size) {
1407cdb0ab79Smaybee 		zfs_range_unlock(rl);
1408cdb0ab79Smaybee 		return (0);
1409cdb0ab79Smaybee 	}
1410cdb0ab79Smaybee 
1411cdb0ab79Smaybee 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1412cdb0ab79Smaybee 	if (error) {
1413cdb0ab79Smaybee 		zfs_range_unlock(rl);
1414cdb0ab79Smaybee 		return (error);
1415cdb0ab79Smaybee 	}
1416cdb0ab79Smaybee top:
1417cdb0ab79Smaybee 	tx = dmu_tx_create(zfsvfs->z_os);
1418cdb0ab79Smaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
14191209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1420cdb0ab79Smaybee 	if (error) {
14211209a471SNeil Perrin 		if (error == ERESTART) {
1422cdb0ab79Smaybee 			dmu_tx_wait(tx);
1423cdb0ab79Smaybee 			dmu_tx_abort(tx);
1424cdb0ab79Smaybee 			goto top;
1425cdb0ab79Smaybee 		}
1426cdb0ab79Smaybee 		dmu_tx_abort(tx);
1427cdb0ab79Smaybee 		zfs_range_unlock(rl);
1428cdb0ab79Smaybee 		return (error);
1429cdb0ab79Smaybee 	}
1430cdb0ab79Smaybee 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1431cdb0ab79Smaybee 
1432cdb0ab79Smaybee 	zp->z_phys->zp_size = end;
1433cdb0ab79Smaybee 
14345730cc9aSmaybee 	dmu_tx_commit(tx);
14355730cc9aSmaybee 
1436fa9e4066Sahrens 	/*
14375730cc9aSmaybee 	 * Clear any mapped pages in the truncated region.  This has to
14385730cc9aSmaybee 	 * happen outside of the transaction to avoid the possibility of
14395730cc9aSmaybee 	 * a deadlock with someone trying to push a page that we are
14405730cc9aSmaybee 	 * about to invalidate.
1441fa9e4066Sahrens 	 */
1442cdb0ab79Smaybee 	if (vn_has_cached_data(vp)) {
1443fa9e4066Sahrens 		page_t *pp;
1444cdb0ab79Smaybee 		uint64_t start = end & PAGEMASK;
1445cdb0ab79Smaybee 		int poff = end & PAGEOFFSET;
1446fa9e4066Sahrens 
14475730cc9aSmaybee 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1448fa9e4066Sahrens 			/*
1449fa9e4066Sahrens 			 * We need to zero a partial page.
1450fa9e4066Sahrens 			 */
14515730cc9aSmaybee 			pagezero(pp, poff, PAGESIZE - poff);
1452fa9e4066Sahrens 			start += PAGESIZE;
1453fa9e4066Sahrens 			page_unlock(pp);
1454fa9e4066Sahrens 		}
1455fa9e4066Sahrens 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
14565730cc9aSmaybee 		    B_INVAL | B_TRUNC, NULL);
1457fa9e4066Sahrens 		ASSERT(error == 0);
1458fa9e4066Sahrens 	}
1459ac05c741SMark Maybee 
1460ac05c741SMark Maybee 	zfs_range_unlock(rl);
1461fa9e4066Sahrens 
1462fa9e4066Sahrens 	return (0);
1463fa9e4066Sahrens }
1464fa9e4066Sahrens 
1465cdb0ab79Smaybee /*
1466cdb0ab79Smaybee  * Free space in a file
1467cdb0ab79Smaybee  *
1468cdb0ab79Smaybee  *	IN:	zp	- znode of file to free data in.
1469cdb0ab79Smaybee  *		off	- start of range
1470cdb0ab79Smaybee  *		len	- end of range (0 => EOF)
1471cdb0ab79Smaybee  *		flag	- current file open mode flags.
1472cdb0ab79Smaybee  *		log	- TRUE if this action should be logged
1473cdb0ab79Smaybee  *
1474cdb0ab79Smaybee  * 	RETURN:	0 if success
1475cdb0ab79Smaybee  *		error code if failure
1476cdb0ab79Smaybee  */
1477cdb0ab79Smaybee int
1478cdb0ab79Smaybee zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1479cdb0ab79Smaybee {
1480cdb0ab79Smaybee 	vnode_t *vp = ZTOV(zp);
1481cdb0ab79Smaybee 	dmu_tx_t *tx;
1482cdb0ab79Smaybee 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1483cdb0ab79Smaybee 	zilog_t *zilog = zfsvfs->z_log;
1484cdb0ab79Smaybee 	int error;
1485cdb0ab79Smaybee 
1486cdb0ab79Smaybee 	if (off > zp->z_phys->zp_size) {
1487cdb0ab79Smaybee 		error =  zfs_extend(zp, off+len);
1488cdb0ab79Smaybee 		if (error == 0 && log)
1489cdb0ab79Smaybee 			goto log;
1490cdb0ab79Smaybee 		else
1491cdb0ab79Smaybee 			return (error);
1492cdb0ab79Smaybee 	}
1493cdb0ab79Smaybee 
1494cdb0ab79Smaybee 	/*
1495cdb0ab79Smaybee 	 * Check for any locks in the region to be freed.
1496cdb0ab79Smaybee 	 */
1497cdb0ab79Smaybee 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
1498cdb0ab79Smaybee 		uint64_t length = (len ? len : zp->z_phys->zp_size - off);
1499cdb0ab79Smaybee 		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1500cdb0ab79Smaybee 			return (error);
1501cdb0ab79Smaybee 	}
1502cdb0ab79Smaybee 
1503cdb0ab79Smaybee 	if (len == 0) {
1504cdb0ab79Smaybee 		error = zfs_trunc(zp, off);
1505cdb0ab79Smaybee 	} else {
1506cdb0ab79Smaybee 		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1507cdb0ab79Smaybee 		    off + len > zp->z_phys->zp_size)
1508cdb0ab79Smaybee 			error = zfs_extend(zp, off+len);
1509cdb0ab79Smaybee 	}
1510cdb0ab79Smaybee 	if (error || !log)
1511cdb0ab79Smaybee 		return (error);
1512cdb0ab79Smaybee log:
1513cdb0ab79Smaybee 	tx = dmu_tx_create(zfsvfs->z_os);
1514cdb0ab79Smaybee 	dmu_tx_hold_bonus(tx, zp->z_id);
15151209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1516cdb0ab79Smaybee 	if (error) {
15171209a471SNeil Perrin 		if (error == ERESTART) {
1518cdb0ab79Smaybee 			dmu_tx_wait(tx);
1519cdb0ab79Smaybee 			dmu_tx_abort(tx);
1520cdb0ab79Smaybee 			goto log;
1521cdb0ab79Smaybee 		}
1522cdb0ab79Smaybee 		dmu_tx_abort(tx);
1523cdb0ab79Smaybee 		return (error);
1524cdb0ab79Smaybee 	}
1525cdb0ab79Smaybee 
1526cdb0ab79Smaybee 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1527cdb0ab79Smaybee 	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1528cdb0ab79Smaybee 
1529cdb0ab79Smaybee 	dmu_tx_commit(tx);
1530cdb0ab79Smaybee 	return (0);
1531cdb0ab79Smaybee }
1532cdb0ab79Smaybee 
1533fa9e4066Sahrens void
1534de8267e0Stimh zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1535fa9e4066Sahrens {
1536fa9e4066Sahrens 	zfsvfs_t	zfsvfs;
1537088f3894Sahrens 	uint64_t	moid, doid, version;
1538de8267e0Stimh 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1539de8267e0Stimh 	uint64_t	norm = 0;
1540de8267e0Stimh 	nvpair_t	*elem;
1541fa9e4066Sahrens 	int		error;
1542fa9e4066Sahrens 	znode_t		*rootzp = NULL;
1543fa9e4066Sahrens 	vnode_t		*vp;
1544fa9e4066Sahrens 	vattr_t		vattr;
15454ccbb6e7Sahrens 	znode_t		*zp;
1546fa9e4066Sahrens 
1547fa9e4066Sahrens 	/*
1548fa9e4066Sahrens 	 * First attempt to create master node.
1549fa9e4066Sahrens 	 */
1550ea8dc4b6Seschrock 	/*
1551ea8dc4b6Seschrock 	 * In an empty objset, there are no blocks to read and thus
1552ea8dc4b6Seschrock 	 * there can be no i/o errors (which we assert below).
1553ea8dc4b6Seschrock 	 */
1554fa9e4066Sahrens 	moid = MASTER_NODE_OBJ;
1555fa9e4066Sahrens 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1556fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
1557fa9e4066Sahrens 	ASSERT(error == 0);
1558fa9e4066Sahrens 
1559fa9e4066Sahrens 	/*
1560fa9e4066Sahrens 	 * Set starting attributes.
1561fa9e4066Sahrens 	 */
1562088f3894Sahrens 	if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
1563088f3894Sahrens 		version = ZPL_VERSION;
1564088f3894Sahrens 	else
1565088f3894Sahrens 		version = ZPL_VERSION_FUID - 1;
1566088f3894Sahrens 	error = zap_update(os, moid, ZPL_VERSION_STR,
1567088f3894Sahrens 	    8, 1, &version, tx);
1568de8267e0Stimh 	elem = NULL;
1569de8267e0Stimh 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1570de8267e0Stimh 		/* For the moment we expect all zpl props to be uint64_ts */
1571de8267e0Stimh 		uint64_t val;
1572de8267e0Stimh 		char *name;
1573de8267e0Stimh 
1574de8267e0Stimh 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1575b3d911cbStimh 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1576de8267e0Stimh 		name = nvpair_name(elem);
1577de8267e0Stimh 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1578de8267e0Stimh 			version = val;
1579de8267e0Stimh 			error = zap_update(os, moid, ZPL_VERSION_STR,
1580de8267e0Stimh 			    8, 1, &version, tx);
1581de8267e0Stimh 		} else {
1582de8267e0Stimh 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1583de8267e0Stimh 		}
1584de8267e0Stimh 		ASSERT(error == 0);
1585de8267e0Stimh 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1586de8267e0Stimh 			norm = val;
1587de8267e0Stimh 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1588de8267e0Stimh 			sense = val;
1589de8267e0Stimh 	}
1590de8267e0Stimh 	ASSERT(version != 0);
1591fa9e4066Sahrens 
1592fa9e4066Sahrens 	/*
1593fa9e4066Sahrens 	 * Create a delete queue.
1594fa9e4066Sahrens 	 */
1595893a6d32Sahrens 	doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1596fa9e4066Sahrens 
1597893a6d32Sahrens 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx);
1598fa9e4066Sahrens 	ASSERT(error == 0);
1599fa9e4066Sahrens 
1600fa9e4066Sahrens 	/*
1601fa9e4066Sahrens 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1602fa9e4066Sahrens 	 * to allow zfs_mknode to work.
1603fa9e4066Sahrens 	 */
1604fa9e4066Sahrens 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1605fa9e4066Sahrens 	vattr.va_type = VDIR;
1606fa9e4066Sahrens 	vattr.va_mode = S_IFDIR|0755;
1607ecd6cf80Smarks 	vattr.va_uid = crgetuid(cr);
1608ecd6cf80Smarks 	vattr.va_gid = crgetgid(cr);
1609fa9e4066Sahrens 
1610fa9e4066Sahrens 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1611893a6d32Sahrens 	rootzp->z_unlinked = 0;
1612fa9e4066Sahrens 	rootzp->z_atime_dirty = 0;
1613fa9e4066Sahrens 
1614fa9e4066Sahrens 	vp = ZTOV(rootzp);
1615fa9e4066Sahrens 	vn_reinit(vp);
1616fa9e4066Sahrens 	vp->v_type = VDIR;
1617fa9e4066Sahrens 
1618fa9e4066Sahrens 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1619fa9e4066Sahrens 
1620fa9e4066Sahrens 	zfsvfs.z_os = os;
1621fa9e4066Sahrens 	zfsvfs.z_parent = &zfsvfs;
1622da6c28aaSamw 	zfsvfs.z_version = version;
1623da6c28aaSamw 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1624da6c28aaSamw 	zfsvfs.z_norm = norm;
1625de8267e0Stimh 	/*
1626de8267e0Stimh 	 * Fold case on file systems that are always or sometimes case
1627de8267e0Stimh 	 * insensitive.
1628de8267e0Stimh 	 */
1629de8267e0Stimh 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1630de8267e0Stimh 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1631fa9e4066Sahrens 
1632fa9e4066Sahrens 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1633fa9e4066Sahrens 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1634fa9e4066Sahrens 	    offsetof(znode_t, z_link_node));
1635fa9e4066Sahrens 
1636b5fca8f8Stomee 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1637b5fca8f8Stomee 	rootzp->z_zfsvfs = &zfsvfs;
1638874395d5Smaybee 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, NULL, NULL);
1639874395d5Smaybee 	ASSERT3P(zp, ==, rootzp);
1640b5fca8f8Stomee 	ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
16414ccbb6e7Sahrens 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1642fa9e4066Sahrens 	ASSERT(error == 0);
1643b5fca8f8Stomee 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1644fa9e4066Sahrens 
1645fa9e4066Sahrens 	ZTOV(rootzp)->v_count = 0;
1646874395d5Smaybee 	dmu_buf_rele(rootzp->z_dbuf, NULL);
1647874395d5Smaybee 	rootzp->z_dbuf = NULL;
1648fa9e4066Sahrens 	kmem_cache_free(znode_cache, rootzp);
1649743a77edSAlan Wright 
1650743a77edSAlan Wright 	/*
1651743a77edSAlan Wright 	 * Create shares directory
1652743a77edSAlan Wright 	 */
1653743a77edSAlan Wright 
1654743a77edSAlan Wright 	error = zfs_create_share_dir(&zfsvfs, tx);
1655743a77edSAlan Wright 	ASSERT(error == 0);
1656fa9e4066Sahrens }
165755434c77Sek 
1658da6c28aaSamw #endif /* _KERNEL */
165955434c77Sek /*
166055434c77Sek  * Given an object number, return its parent object number and whether
166155434c77Sek  * or not the object is an extended attribute directory.
166255434c77Sek  */
166355434c77Sek static int
166455434c77Sek zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
166555434c77Sek {
166655434c77Sek 	dmu_buf_t *db;
166755434c77Sek 	dmu_object_info_t doi;
166855434c77Sek 	znode_phys_t *zp;
166955434c77Sek 	int error;
167055434c77Sek 
167155434c77Sek 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
167255434c77Sek 		return (error);
167355434c77Sek 
167455434c77Sek 	dmu_object_info_from_db(db, &doi);
167555434c77Sek 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
167655434c77Sek 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
167755434c77Sek 		dmu_buf_rele(db, FTAG);
167855434c77Sek 		return (EINVAL);
167955434c77Sek 	}
168055434c77Sek 
168155434c77Sek 	zp = db->db_data;
168255434c77Sek 	*pobjp = zp->zp_parent;
168355434c77Sek 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
168455434c77Sek 	    S_ISDIR(zp->zp_mode);
168555434c77Sek 	dmu_buf_rele(db, FTAG);
168655434c77Sek 
168755434c77Sek 	return (0);
168855434c77Sek }
168955434c77Sek 
169055434c77Sek int
169155434c77Sek zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
169255434c77Sek {
169355434c77Sek 	char *path = buf + len - 1;
169455434c77Sek 	int error;
169555434c77Sek 
169655434c77Sek 	*path = '\0';
169755434c77Sek 
169855434c77Sek 	for (;;) {
169955434c77Sek 		uint64_t pobj;
170055434c77Sek 		char component[MAXNAMELEN + 2];
170155434c77Sek 		size_t complen;
170255434c77Sek 		int is_xattrdir;
170355434c77Sek 
170455434c77Sek 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
170555434c77Sek 		    &is_xattrdir)) != 0)
170655434c77Sek 			break;
170755434c77Sek 
170855434c77Sek 		if (pobj == obj) {
170955434c77Sek 			if (path[0] != '/')
171055434c77Sek 				*--path = '/';
171155434c77Sek 			break;
171255434c77Sek 		}
171355434c77Sek 
171455434c77Sek 		component[0] = '/';
171555434c77Sek 		if (is_xattrdir) {
171655434c77Sek 			(void) sprintf(component + 1, "<xattrdir>");
171755434c77Sek 		} else {
1718e7437265Sahrens 			error = zap_value_search(osp, pobj, obj,
1719e7437265Sahrens 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
172055434c77Sek 			if (error != 0)
172155434c77Sek 				break;
172255434c77Sek 		}
172355434c77Sek 
172455434c77Sek 		complen = strlen(component);
172555434c77Sek 		path -= complen;
172655434c77Sek 		ASSERT(path >= buf);
172755434c77Sek 		bcopy(component, path, complen);
172855434c77Sek 		obj = pobj;
172955434c77Sek 	}
173055434c77Sek 
173155434c77Sek 	if (error == 0)
173255434c77Sek 		(void) memmove(buf, path, buf + len - path);
173355434c77Sek 	return (error);
173455434c77Sek }
1735