zfs_znode.c revision 302738
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Integros [integros.com]
25 */
26
27/* Portions Copyright 2007 Jeremy Teo */
28/* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
29
30#ifdef _KERNEL
31#include <sys/types.h>
32#include <sys/param.h>
33#include <sys/time.h>
34#include <sys/systm.h>
35#include <sys/sysmacros.h>
36#include <sys/resource.h>
37#include <sys/mntent.h>
38#include <sys/u8_textprep.h>
39#include <sys/dsl_dataset.h>
40#include <sys/vfs.h>
41#include <sys/vnode.h>
42#include <sys/file.h>
43#include <sys/kmem.h>
44#include <sys/errno.h>
45#include <sys/unistd.h>
46#include <sys/atomic.h>
47#include <sys/zfs_dir.h>
48#include <sys/zfs_acl.h>
49#include <sys/zfs_ioctl.h>
50#include <sys/zfs_rlock.h>
51#include <sys/zfs_fuid.h>
52#include <sys/dnode.h>
53#include <sys/fs/zfs.h>
54#include <sys/kidmap.h>
55#endif /* _KERNEL */
56
57#include <sys/dmu.h>
58#include <sys/dmu_objset.h>
59#include <sys/refcount.h>
60#include <sys/stat.h>
61#include <sys/zap.h>
62#include <sys/zfs_znode.h>
63#include <sys/sa.h>
64#include <sys/zfs_sa.h>
65#include <sys/zfs_stat.h>
66#include <sys/refcount.h>
67
68#include "zfs_prop.h"
69#include "zfs_comutil.h"
70
71/* Used by fstat(1). */
72SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD,
73    SYSCTL_NULL_INT_PTR, sizeof(znode_t), "sizeof(znode_t)");
74
75/*
76 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
77 * turned on when DEBUG is also defined.
78 */
79#ifdef	DEBUG
80#define	ZNODE_STATS
81#endif	/* DEBUG */
82
83#ifdef	ZNODE_STATS
84#define	ZNODE_STAT_ADD(stat)			((stat)++)
85#else
86#define	ZNODE_STAT_ADD(stat)			/* nothing */
87#endif	/* ZNODE_STATS */
88
89/*
90 * Functions needed for userland (ie: libzpool) are not put under
91 * #ifdef_KERNEL; the rest of the functions have dependencies
92 * (such as VFS logic) that will not compile easily in userland.
93 */
94#ifdef _KERNEL
95/*
96 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
97 * be freed before it can be safely accessed.
98 */
99krwlock_t zfsvfs_lock;
100
101static kmem_cache_t *znode_cache = NULL;
102
103/*ARGSUSED*/
104static void
105znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
106{
107	/*
108	 * We should never drop all dbuf refs without first clearing
109	 * the eviction callback.
110	 */
111	panic("evicting znode %p\n", user_ptr);
112}
113
114extern struct vop_vector zfs_vnodeops;
115extern struct vop_vector zfs_fifoops;
116extern struct vop_vector zfs_shareops;
117
118static int
119zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
120{
121	znode_t *zp = buf;
122
123	POINTER_INVALIDATE(&zp->z_zfsvfs);
124
125	list_link_init(&zp->z_link_node);
126
127	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
128	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
129	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
130	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
131
132	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
133	avl_create(&zp->z_range_avl, zfs_range_compare,
134	    sizeof (rl_t), offsetof(rl_t, r_node));
135
136	zp->z_dirlocks = NULL;
137	zp->z_acl_cached = NULL;
138	zp->z_vnode = NULL;
139	zp->z_moved = 0;
140	return (0);
141}
142
143/*ARGSUSED*/
144static void
145zfs_znode_cache_destructor(void *buf, void *arg)
146{
147	znode_t *zp = buf;
148
149	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
150	ASSERT(ZTOV(zp) == NULL);
151	vn_free(ZTOV(zp));
152	ASSERT(!list_link_active(&zp->z_link_node));
153	mutex_destroy(&zp->z_lock);
154	rw_destroy(&zp->z_parent_lock);
155	rw_destroy(&zp->z_name_lock);
156	mutex_destroy(&zp->z_acl_lock);
157	avl_destroy(&zp->z_range_avl);
158	mutex_destroy(&zp->z_range_lock);
159
160	ASSERT(zp->z_dirlocks == NULL);
161	ASSERT(zp->z_acl_cached == NULL);
162}
163
164#ifdef	ZNODE_STATS
165static struct {
166	uint64_t zms_zfsvfs_invalid;
167	uint64_t zms_zfsvfs_recheck1;
168	uint64_t zms_zfsvfs_unmounted;
169	uint64_t zms_zfsvfs_recheck2;
170	uint64_t zms_obj_held;
171	uint64_t zms_vnode_locked;
172	uint64_t zms_not_only_dnlc;
173} znode_move_stats;
174#endif	/* ZNODE_STATS */
175
176#ifdef illumos
177static void
178zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
179{
180	vnode_t *vp;
181
182	/* Copy fields. */
183	nzp->z_zfsvfs = ozp->z_zfsvfs;
184
185	/* Swap vnodes. */
186	vp = nzp->z_vnode;
187	nzp->z_vnode = ozp->z_vnode;
188	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
189	ZTOV(ozp)->v_data = ozp;
190	ZTOV(nzp)->v_data = nzp;
191
192	nzp->z_id = ozp->z_id;
193	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
194	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
195	nzp->z_unlinked = ozp->z_unlinked;
196	nzp->z_atime_dirty = ozp->z_atime_dirty;
197	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
198	nzp->z_blksz = ozp->z_blksz;
199	nzp->z_seq = ozp->z_seq;
200	nzp->z_mapcnt = ozp->z_mapcnt;
201	nzp->z_gen = ozp->z_gen;
202	nzp->z_sync_cnt = ozp->z_sync_cnt;
203	nzp->z_is_sa = ozp->z_is_sa;
204	nzp->z_sa_hdl = ozp->z_sa_hdl;
205	bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
206	nzp->z_links = ozp->z_links;
207	nzp->z_size = ozp->z_size;
208	nzp->z_pflags = ozp->z_pflags;
209	nzp->z_uid = ozp->z_uid;
210	nzp->z_gid = ozp->z_gid;
211	nzp->z_mode = ozp->z_mode;
212
213	/*
214	 * Since this is just an idle znode and kmem is already dealing with
215	 * memory pressure, release any cached ACL.
216	 */
217	if (ozp->z_acl_cached) {
218		zfs_acl_free(ozp->z_acl_cached);
219		ozp->z_acl_cached = NULL;
220	}
221
222	sa_set_userp(nzp->z_sa_hdl, nzp);
223
224	/*
225	 * Invalidate the original znode by clearing fields that provide a
226	 * pointer back to the znode. Set the low bit of the vfs pointer to
227	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
228	 * subsequent callback.
229	 */
230	ozp->z_sa_hdl = NULL;
231	POINTER_INVALIDATE(&ozp->z_zfsvfs);
232
233	/*
234	 * Mark the znode.
235	 */
236	nzp->z_moved = 1;
237	ozp->z_moved = (uint8_t)-1;
238}
239
240/*ARGSUSED*/
241static kmem_cbrc_t
242zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
243{
244	znode_t *ozp = buf, *nzp = newbuf;
245	zfsvfs_t *zfsvfs;
246	vnode_t *vp;
247
248	/*
249	 * The znode is on the file system's list of known znodes if the vfs
250	 * pointer is valid. We set the low bit of the vfs pointer when freeing
251	 * the znode to invalidate it, and the memory patterns written by kmem
252	 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
253	 * created znode sets the vfs pointer last of all to indicate that the
254	 * znode is known and in a valid state to be moved by this function.
255	 */
256	zfsvfs = ozp->z_zfsvfs;
257	if (!POINTER_IS_VALID(zfsvfs)) {
258		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
259		return (KMEM_CBRC_DONT_KNOW);
260	}
261
262	/*
263	 * Close a small window in which it's possible that the filesystem could
264	 * be unmounted and freed, and zfsvfs, though valid in the previous
265	 * statement, could point to unrelated memory by the time we try to
266	 * prevent the filesystem from being unmounted.
267	 */
268	rw_enter(&zfsvfs_lock, RW_WRITER);
269	if (zfsvfs != ozp->z_zfsvfs) {
270		rw_exit(&zfsvfs_lock);
271		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
272		return (KMEM_CBRC_DONT_KNOW);
273	}
274
275	/*
276	 * If the znode is still valid, then so is the file system. We know that
277	 * no valid file system can be freed while we hold zfsvfs_lock, so we
278	 * can safely ensure that the filesystem is not and will not be
279	 * unmounted. The next statement is equivalent to ZFS_ENTER().
280	 */
281	rrm_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
282	if (zfsvfs->z_unmounted) {
283		ZFS_EXIT(zfsvfs);
284		rw_exit(&zfsvfs_lock);
285		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
286		return (KMEM_CBRC_DONT_KNOW);
287	}
288	rw_exit(&zfsvfs_lock);
289
290	mutex_enter(&zfsvfs->z_znodes_lock);
291	/*
292	 * Recheck the vfs pointer in case the znode was removed just before
293	 * acquiring the lock.
294	 */
295	if (zfsvfs != ozp->z_zfsvfs) {
296		mutex_exit(&zfsvfs->z_znodes_lock);
297		ZFS_EXIT(zfsvfs);
298		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
299		return (KMEM_CBRC_DONT_KNOW);
300	}
301
302	/*
303	 * At this point we know that as long as we hold z_znodes_lock, the
304	 * znode cannot be freed and fields within the znode can be safely
305	 * accessed. Now, prevent a race with zfs_zget().
306	 */
307	if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
308		mutex_exit(&zfsvfs->z_znodes_lock);
309		ZFS_EXIT(zfsvfs);
310		ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
311		return (KMEM_CBRC_LATER);
312	}
313
314	vp = ZTOV(ozp);
315	if (mutex_tryenter(&vp->v_lock) == 0) {
316		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
317		mutex_exit(&zfsvfs->z_znodes_lock);
318		ZFS_EXIT(zfsvfs);
319		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
320		return (KMEM_CBRC_LATER);
321	}
322
323	/* Only move znodes that are referenced _only_ by the DNLC. */
324	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
325		mutex_exit(&vp->v_lock);
326		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
327		mutex_exit(&zfsvfs->z_znodes_lock);
328		ZFS_EXIT(zfsvfs);
329		ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
330		return (KMEM_CBRC_LATER);
331	}
332
333	/*
334	 * The znode is known and in a valid state to move. We're holding the
335	 * locks needed to execute the critical section.
336	 */
337	zfs_znode_move_impl(ozp, nzp);
338	mutex_exit(&vp->v_lock);
339	ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
340
341	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
342	mutex_exit(&zfsvfs->z_znodes_lock);
343	ZFS_EXIT(zfsvfs);
344
345	return (KMEM_CBRC_YES);
346}
347#endif /* illumos */
348
349void
350zfs_znode_init(void)
351{
352	/*
353	 * Initialize zcache
354	 */
355	rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
356	ASSERT(znode_cache == NULL);
357	znode_cache = kmem_cache_create("zfs_znode_cache",
358	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
359	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
360	kmem_cache_set_move(znode_cache, zfs_znode_move);
361}
362
363void
364zfs_znode_fini(void)
365{
366#ifdef illumos
367	/*
368	 * Cleanup vfs & vnode ops
369	 */
370	zfs_remove_op_tables();
371#endif
372
373	/*
374	 * Cleanup zcache
375	 */
376	if (znode_cache)
377		kmem_cache_destroy(znode_cache);
378	znode_cache = NULL;
379	rw_destroy(&zfsvfs_lock);
380}
381
382#ifdef illumos
383struct vnodeops *zfs_dvnodeops;
384struct vnodeops *zfs_fvnodeops;
385struct vnodeops *zfs_symvnodeops;
386struct vnodeops *zfs_xdvnodeops;
387struct vnodeops *zfs_evnodeops;
388struct vnodeops *zfs_sharevnodeops;
389
390void
391zfs_remove_op_tables()
392{
393	/*
394	 * Remove vfs ops
395	 */
396	ASSERT(zfsfstype);
397	(void) vfs_freevfsops_by_type(zfsfstype);
398	zfsfstype = 0;
399
400	/*
401	 * Remove vnode ops
402	 */
403	if (zfs_dvnodeops)
404		vn_freevnodeops(zfs_dvnodeops);
405	if (zfs_fvnodeops)
406		vn_freevnodeops(zfs_fvnodeops);
407	if (zfs_symvnodeops)
408		vn_freevnodeops(zfs_symvnodeops);
409	if (zfs_xdvnodeops)
410		vn_freevnodeops(zfs_xdvnodeops);
411	if (zfs_evnodeops)
412		vn_freevnodeops(zfs_evnodeops);
413	if (zfs_sharevnodeops)
414		vn_freevnodeops(zfs_sharevnodeops);
415
416	zfs_dvnodeops = NULL;
417	zfs_fvnodeops = NULL;
418	zfs_symvnodeops = NULL;
419	zfs_xdvnodeops = NULL;
420	zfs_evnodeops = NULL;
421	zfs_sharevnodeops = NULL;
422}
423
424extern const fs_operation_def_t zfs_dvnodeops_template[];
425extern const fs_operation_def_t zfs_fvnodeops_template[];
426extern const fs_operation_def_t zfs_xdvnodeops_template[];
427extern const fs_operation_def_t zfs_symvnodeops_template[];
428extern const fs_operation_def_t zfs_evnodeops_template[];
429extern const fs_operation_def_t zfs_sharevnodeops_template[];
430
431int
432zfs_create_op_tables()
433{
434	int error;
435
436	/*
437	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
438	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
439	 * In this case we just return as the ops vectors are already set up.
440	 */
441	if (zfs_dvnodeops)
442		return (0);
443
444	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
445	    &zfs_dvnodeops);
446	if (error)
447		return (error);
448
449	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
450	    &zfs_fvnodeops);
451	if (error)
452		return (error);
453
454	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
455	    &zfs_symvnodeops);
456	if (error)
457		return (error);
458
459	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
460	    &zfs_xdvnodeops);
461	if (error)
462		return (error);
463
464	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
465	    &zfs_evnodeops);
466	if (error)
467		return (error);
468
469	error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
470	    &zfs_sharevnodeops);
471
472	return (error);
473}
474#endif	/* illumos */
475
476int
477zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
478{
479	zfs_acl_ids_t acl_ids;
480	vattr_t vattr;
481	znode_t *sharezp;
482	znode_t *zp;
483	int error;
484
485	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
486	vattr.va_type = VDIR;
487	vattr.va_mode = S_IFDIR|0555;
488	vattr.va_uid = crgetuid(kcred);
489	vattr.va_gid = crgetgid(kcred);
490
491	sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
492	ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
493	sharezp->z_moved = 0;
494	sharezp->z_unlinked = 0;
495	sharezp->z_atime_dirty = 0;
496	sharezp->z_zfsvfs = zfsvfs;
497	sharezp->z_is_sa = zfsvfs->z_use_sa;
498
499	VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
500	    kcred, NULL, &acl_ids));
501	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
502	ASSERT3P(zp, ==, sharezp);
503	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
504	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
505	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
506	zfsvfs->z_shares_dir = sharezp->z_id;
507
508	zfs_acl_ids_free(&acl_ids);
509	sa_handle_destroy(sharezp->z_sa_hdl);
510	kmem_cache_free(znode_cache, sharezp);
511
512	return (error);
513}
514
515/*
516 * define a couple of values we need available
517 * for both 64 and 32 bit environments.
518 */
519#ifndef NBITSMINOR64
520#define	NBITSMINOR64	32
521#endif
522#ifndef MAXMAJ64
523#define	MAXMAJ64	0xffffffffUL
524#endif
525#ifndef	MAXMIN64
526#define	MAXMIN64	0xffffffffUL
527#endif
528
529/*
530 * Create special expldev for ZFS private use.
531 * Can't use standard expldev since it doesn't do
532 * what we want.  The standard expldev() takes a
533 * dev32_t in LP64 and expands it to a long dev_t.
534 * We need an interface that takes a dev32_t in ILP32
535 * and expands it to a long dev_t.
536 */
537static uint64_t
538zfs_expldev(dev_t dev)
539{
540	return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
541}
542/*
543 * Special cmpldev for ZFS private use.
544 * Can't use standard cmpldev since it takes
545 * a long dev_t and compresses it to dev32_t in
546 * LP64.  We need to do a compaction of a long dev_t
547 * to a dev32_t in ILP32.
548 */
549dev_t
550zfs_cmpldev(uint64_t dev)
551{
552	return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
553}
554
555static void
556zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
557    dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
558{
559	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
560	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
561
562	mutex_enter(&zp->z_lock);
563
564	ASSERT(zp->z_sa_hdl == NULL);
565	ASSERT(zp->z_acl_cached == NULL);
566	if (sa_hdl == NULL) {
567		VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
568		    SA_HDL_SHARED, &zp->z_sa_hdl));
569	} else {
570		zp->z_sa_hdl = sa_hdl;
571		sa_set_userp(sa_hdl, zp);
572	}
573
574	zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
575
576	/*
577	 * Slap on VROOT if we are the root znode unless we are the root
578	 * node of a snapshot mounted under .zfs.
579	 */
580	if (zp->z_id == zfsvfs->z_root && zfsvfs->z_parent == zfsvfs)
581		ZTOV(zp)->v_flag |= VROOT;
582
583	mutex_exit(&zp->z_lock);
584	vn_exists(ZTOV(zp));
585}
586
587void
588zfs_znode_dmu_fini(znode_t *zp)
589{
590	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
591	    zp->z_unlinked ||
592	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
593
594	sa_handle_destroy(zp->z_sa_hdl);
595	zp->z_sa_hdl = NULL;
596}
597
598static void
599zfs_vnode_forget(vnode_t *vp)
600{
601
602	/* copied from insmntque_stddtr */
603	vp->v_data = NULL;
604	vp->v_op = &dead_vnodeops;
605	vgone(vp);
606	vput(vp);
607}
608
609/*
610 * Construct a new znode/vnode and intialize.
611 *
612 * This does not do a call to dmu_set_user() that is
613 * up to the caller to do, in case you don't want to
614 * return the znode
615 */
616static znode_t *
617zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
618    dmu_object_type_t obj_type, sa_handle_t *hdl)
619{
620	znode_t	*zp;
621	vnode_t *vp;
622	uint64_t mode;
623	uint64_t parent;
624	sa_bulk_attr_t bulk[9];
625	int count = 0;
626	int error;
627
628	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
629
630	KASSERT(curthread->td_vp_reserv > 0,
631	    ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
632	error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp);
633	if (error != 0) {
634		kmem_cache_free(znode_cache, zp);
635		return (NULL);
636	}
637	zp->z_vnode = vp;
638	vp->v_data = zp;
639
640	ASSERT(zp->z_dirlocks == NULL);
641	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
642	zp->z_moved = 0;
643
644	/*
645	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
646	 * the zfs_znode_move() callback.
647	 */
648	zp->z_sa_hdl = NULL;
649	zp->z_unlinked = 0;
650	zp->z_atime_dirty = 0;
651	zp->z_mapcnt = 0;
652	zp->z_id = db->db_object;
653	zp->z_blksz = blksz;
654	zp->z_seq = 0x7A4653;
655	zp->z_sync_cnt = 0;
656
657	vp = ZTOV(zp);
658
659	zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
660
661	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
662	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
663	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
664	    &zp->z_size, 8);
665	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
666	    &zp->z_links, 8);
667	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
668	    &zp->z_pflags, 8);
669	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
670	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
671	    &zp->z_atime, 16);
672	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
673	    &zp->z_uid, 8);
674	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
675	    &zp->z_gid, 8);
676
677	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
678		if (hdl == NULL)
679			sa_handle_destroy(zp->z_sa_hdl);
680		zfs_vnode_forget(vp);
681		zp->z_vnode = NULL;
682		kmem_cache_free(znode_cache, zp);
683		return (NULL);
684	}
685
686	zp->z_mode = mode;
687
688	vp->v_type = IFTOVT((mode_t)mode);
689
690	switch (vp->v_type) {
691	case VDIR:
692		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
693		break;
694#ifdef illumos
695	case VBLK:
696	case VCHR:
697		{
698			uint64_t rdev;
699			VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
700			    &rdev, sizeof (rdev)) == 0);
701
702			vp->v_rdev = zfs_cmpldev(rdev);
703		}
704		break;
705#endif
706	case VFIFO:
707#ifdef illumos
708	case VSOCK:
709	case VDOOR:
710#endif
711		vp->v_op = &zfs_fifoops;
712		break;
713	case VREG:
714		if (parent == zfsvfs->z_shares_dir) {
715			ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
716			vp->v_op = &zfs_shareops;
717		}
718		break;
719#ifdef illumos
720	case VLNK:
721		vn_setops(vp, zfs_symvnodeops);
722		break;
723	default:
724		vn_setops(vp, zfs_evnodeops);
725		break;
726#endif
727	}
728
729	mutex_enter(&zfsvfs->z_znodes_lock);
730	list_insert_tail(&zfsvfs->z_all_znodes, zp);
731	membar_producer();
732	/*
733	 * Everything else must be valid before assigning z_zfsvfs makes the
734	 * znode eligible for zfs_znode_move().
735	 */
736	zp->z_zfsvfs = zfsvfs;
737	mutex_exit(&zfsvfs->z_znodes_lock);
738
739	/*
740	 * Acquire vnode lock before making it available to the world.
741	 */
742	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
743	VN_LOCK_AREC(vp);
744	if (vp->v_type != VFIFO)
745		VN_LOCK_ASHARE(vp);
746
747#ifdef illumos
748	VFS_HOLD(zfsvfs->z_vfs);
749#endif
750	return (zp);
751}
752
753static uint64_t empty_xattr;
754static uint64_t pad[4];
755static zfs_acl_phys_t acl_phys;
756/*
757 * Create a new DMU object to hold a zfs znode.
758 *
759 *	IN:	dzp	- parent directory for new znode
760 *		vap	- file attributes for new znode
761 *		tx	- dmu transaction id for zap operations
762 *		cr	- credentials of caller
763 *		flag	- flags:
764 *			  IS_ROOT_NODE	- new object will be root
765 *			  IS_XATTR	- new object is an attribute
766 *		bonuslen - length of bonus buffer
767 *		setaclp  - File/Dir initial ACL
768 *		fuidp	 - Tracks fuid allocation.
769 *
770 *	OUT:	zpp	- allocated znode
771 *
772 */
773void
774zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
775    uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
776{
777	uint64_t	crtime[2], atime[2], mtime[2], ctime[2];
778	uint64_t	mode, size, links, parent, pflags;
779	uint64_t	dzp_pflags = 0;
780	uint64_t	rdev = 0;
781	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
782	dmu_buf_t	*db;
783	timestruc_t	now;
784	uint64_t	gen, obj;
785	int		err;
786	int		bonuslen;
787	sa_handle_t	*sa_hdl;
788	dmu_object_type_t obj_type;
789	sa_bulk_attr_t	sa_attrs[ZPL_END];
790	int		cnt = 0;
791	zfs_acl_locator_cb_t locate = { 0 };
792
793	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
794
795	if (zfsvfs->z_replay) {
796		obj = vap->va_nodeid;
797		now = vap->va_ctime;		/* see zfs_replay_create() */
798		gen = vap->va_nblocks;		/* ditto */
799	} else {
800		obj = 0;
801		vfs_timestamp(&now);
802		gen = dmu_tx_get_txg(tx);
803	}
804
805	obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
806	bonuslen = (obj_type == DMU_OT_SA) ?
807	    DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
808
809	/*
810	 * Create a new DMU object.
811	 */
812	/*
813	 * There's currently no mechanism for pre-reading the blocks that will
814	 * be needed to allocate a new object, so we accept the small chance
815	 * that there will be an i/o error and we will fail one of the
816	 * assertions below.
817	 */
818	if (vap->va_type == VDIR) {
819		if (zfsvfs->z_replay) {
820			VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj,
821			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
822			    obj_type, bonuslen, tx));
823		} else {
824			obj = zap_create_norm(zfsvfs->z_os,
825			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
826			    obj_type, bonuslen, tx);
827		}
828	} else {
829		if (zfsvfs->z_replay) {
830			VERIFY0(dmu_object_claim(zfsvfs->z_os, obj,
831			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
832			    obj_type, bonuslen, tx));
833		} else {
834			obj = dmu_object_alloc(zfsvfs->z_os,
835			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
836			    obj_type, bonuslen, tx);
837		}
838	}
839
840	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
841	VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
842
843	/*
844	 * If this is the root, fix up the half-initialized parent pointer
845	 * to reference the just-allocated physical data area.
846	 */
847	if (flag & IS_ROOT_NODE) {
848		dzp->z_id = obj;
849	} else {
850		dzp_pflags = dzp->z_pflags;
851	}
852
853	/*
854	 * If parent is an xattr, so am I.
855	 */
856	if (dzp_pflags & ZFS_XATTR) {
857		flag |= IS_XATTR;
858	}
859
860	if (zfsvfs->z_use_fuids)
861		pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
862	else
863		pflags = 0;
864
865	if (vap->va_type == VDIR) {
866		size = 2;		/* contents ("." and "..") */
867		links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
868	} else {
869		size = links = 0;
870	}
871
872	if (vap->va_type == VBLK || vap->va_type == VCHR) {
873		rdev = zfs_expldev(vap->va_rdev);
874	}
875
876	parent = dzp->z_id;
877	mode = acl_ids->z_mode;
878	if (flag & IS_XATTR)
879		pflags |= ZFS_XATTR;
880
881	/*
882	 * No execs denied will be deterimed when zfs_mode_compute() is called.
883	 */
884	pflags |= acl_ids->z_aclp->z_hints &
885	    (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
886	    ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
887
888	ZFS_TIME_ENCODE(&now, crtime);
889	ZFS_TIME_ENCODE(&now, ctime);
890
891	if (vap->va_mask & AT_ATIME) {
892		ZFS_TIME_ENCODE(&vap->va_atime, atime);
893	} else {
894		ZFS_TIME_ENCODE(&now, atime);
895	}
896
897	if (vap->va_mask & AT_MTIME) {
898		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
899	} else {
900		ZFS_TIME_ENCODE(&now, mtime);
901	}
902
903	/* Now add in all of the "SA" attributes */
904	VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
905	    &sa_hdl));
906
907	/*
908	 * Setup the array of attributes to be replaced/set on the new file
909	 *
910	 * order for  DMU_OT_ZNODE is critical since it needs to be constructed
911	 * in the old znode_phys_t format.  Don't change this ordering
912	 */
913
914	if (obj_type == DMU_OT_ZNODE) {
915		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
916		    NULL, &atime, 16);
917		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
918		    NULL, &mtime, 16);
919		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
920		    NULL, &ctime, 16);
921		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
922		    NULL, &crtime, 16);
923		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
924		    NULL, &gen, 8);
925		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
926		    NULL, &mode, 8);
927		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
928		    NULL, &size, 8);
929		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
930		    NULL, &parent, 8);
931	} else {
932		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
933		    NULL, &mode, 8);
934		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
935		    NULL, &size, 8);
936		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
937		    NULL, &gen, 8);
938		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
939		    &acl_ids->z_fuid, 8);
940		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
941		    &acl_ids->z_fgid, 8);
942		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
943		    NULL, &parent, 8);
944		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
945		    NULL, &pflags, 8);
946		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
947		    NULL, &atime, 16);
948		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
949		    NULL, &mtime, 16);
950		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
951		    NULL, &ctime, 16);
952		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
953		    NULL, &crtime, 16);
954	}
955
956	SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
957
958	if (obj_type == DMU_OT_ZNODE) {
959		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
960		    &empty_xattr, 8);
961	}
962	if (obj_type == DMU_OT_ZNODE ||
963	    (vap->va_type == VBLK || vap->va_type == VCHR)) {
964		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
965		    NULL, &rdev, 8);
966
967	}
968	if (obj_type == DMU_OT_ZNODE) {
969		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
970		    NULL, &pflags, 8);
971		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
972		    &acl_ids->z_fuid, 8);
973		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
974		    &acl_ids->z_fgid, 8);
975		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
976		    sizeof (uint64_t) * 4);
977		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
978		    &acl_phys, sizeof (zfs_acl_phys_t));
979	} else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
980		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
981		    &acl_ids->z_aclp->z_acl_count, 8);
982		locate.cb_aclp = acl_ids->z_aclp;
983		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
984		    zfs_acl_data_locator, &locate,
985		    acl_ids->z_aclp->z_acl_bytes);
986		mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
987		    acl_ids->z_fuid, acl_ids->z_fgid);
988	}
989
990	VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
991
992	if (!(flag & IS_ROOT_NODE)) {
993		*zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
994		ASSERT(*zpp != NULL);
995	} else {
996		/*
997		 * If we are creating the root node, the "parent" we
998		 * passed in is the znode for the root.
999		 */
1000		*zpp = dzp;
1001
1002		(*zpp)->z_sa_hdl = sa_hdl;
1003	}
1004
1005	(*zpp)->z_pflags = pflags;
1006	(*zpp)->z_mode = mode;
1007
1008	if (vap->va_mask & AT_XVATTR)
1009		zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
1010
1011	if (obj_type == DMU_OT_ZNODE ||
1012	    acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1013		VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
1014	}
1015	if (!(flag & IS_ROOT_NODE)) {
1016		vnode_t *vp;
1017
1018		vp = ZTOV(*zpp);
1019		vp->v_vflag |= VV_FORCEINSMQ;
1020		err = insmntque(vp, zfsvfs->z_vfs);
1021		vp->v_vflag &= ~VV_FORCEINSMQ;
1022		KASSERT(err == 0, ("insmntque() failed: error %d", err));
1023	}
1024	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1025}
1026
1027/*
1028 * Update in-core attributes.  It is assumed the caller will be doing an
1029 * sa_bulk_update to push the changes out.
1030 */
1031void
1032zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1033{
1034	xoptattr_t *xoap;
1035
1036	xoap = xva_getxoptattr(xvap);
1037	ASSERT(xoap);
1038
1039	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1040		uint64_t times[2];
1041		ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1042		(void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1043		    &times, sizeof (times), tx);
1044		XVA_SET_RTN(xvap, XAT_CREATETIME);
1045	}
1046	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1047		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1048		    zp->z_pflags, tx);
1049		XVA_SET_RTN(xvap, XAT_READONLY);
1050	}
1051	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1052		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1053		    zp->z_pflags, tx);
1054		XVA_SET_RTN(xvap, XAT_HIDDEN);
1055	}
1056	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1057		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1058		    zp->z_pflags, tx);
1059		XVA_SET_RTN(xvap, XAT_SYSTEM);
1060	}
1061	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1062		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1063		    zp->z_pflags, tx);
1064		XVA_SET_RTN(xvap, XAT_ARCHIVE);
1065	}
1066	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1067		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1068		    zp->z_pflags, tx);
1069		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1070	}
1071	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1072		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1073		    zp->z_pflags, tx);
1074		XVA_SET_RTN(xvap, XAT_NOUNLINK);
1075	}
1076	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1077		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1078		    zp->z_pflags, tx);
1079		XVA_SET_RTN(xvap, XAT_APPENDONLY);
1080	}
1081	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1082		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1083		    zp->z_pflags, tx);
1084		XVA_SET_RTN(xvap, XAT_NODUMP);
1085	}
1086	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1087		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1088		    zp->z_pflags, tx);
1089		XVA_SET_RTN(xvap, XAT_OPAQUE);
1090	}
1091	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1092		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1093		    xoap->xoa_av_quarantined, zp->z_pflags, tx);
1094		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1095	}
1096	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1097		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1098		    zp->z_pflags, tx);
1099		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1100	}
1101	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1102		zfs_sa_set_scanstamp(zp, xvap, tx);
1103		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1104	}
1105	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1106		ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1107		    zp->z_pflags, tx);
1108		XVA_SET_RTN(xvap, XAT_REPARSE);
1109	}
1110	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1111		ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1112		    zp->z_pflags, tx);
1113		XVA_SET_RTN(xvap, XAT_OFFLINE);
1114	}
1115	if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1116		ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1117		    zp->z_pflags, tx);
1118		XVA_SET_RTN(xvap, XAT_SPARSE);
1119	}
1120}
1121
1122int
1123zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1124{
1125	dmu_object_info_t doi;
1126	dmu_buf_t	*db;
1127	znode_t		*zp;
1128	vnode_t		*vp;
1129	sa_handle_t	*hdl;
1130	struct thread	*td;
1131	int locked;
1132	int err;
1133
1134	td = curthread;
1135	getnewvnode_reserve(1);
1136again:
1137	*zpp = NULL;
1138	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1139
1140	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1141	if (err) {
1142		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1143		getnewvnode_drop_reserve();
1144		return (err);
1145	}
1146
1147	dmu_object_info_from_db(db, &doi);
1148	if (doi.doi_bonus_type != DMU_OT_SA &&
1149	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1150	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1151	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1152		sa_buf_rele(db, NULL);
1153		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1154#ifdef __FreeBSD__
1155		getnewvnode_drop_reserve();
1156#endif
1157		return (SET_ERROR(EINVAL));
1158	}
1159
1160	hdl = dmu_buf_get_user(db);
1161	if (hdl != NULL) {
1162		zp  = sa_get_userdata(hdl);
1163
1164
1165		/*
1166		 * Since "SA" does immediate eviction we
1167		 * should never find a sa handle that doesn't
1168		 * know about the znode.
1169		 */
1170
1171		ASSERT3P(zp, !=, NULL);
1172
1173		mutex_enter(&zp->z_lock);
1174		ASSERT3U(zp->z_id, ==, obj_num);
1175		if (zp->z_unlinked) {
1176			err = SET_ERROR(ENOENT);
1177		} else {
1178			vp = ZTOV(zp);
1179			*zpp = zp;
1180			err = 0;
1181		}
1182
1183		/* Don't let the vnode disappear after ZFS_OBJ_HOLD_EXIT. */
1184		if (err == 0)
1185			VN_HOLD(vp);
1186
1187		mutex_exit(&zp->z_lock);
1188		sa_buf_rele(db, NULL);
1189		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1190
1191		if (err == 0) {
1192			locked = VOP_ISLOCKED(vp);
1193			VI_LOCK(vp);
1194			if ((vp->v_iflag & VI_DOOMED) != 0 &&
1195			    locked != LK_EXCLUSIVE) {
1196				/*
1197				 * The vnode is doomed and this thread doesn't
1198				 * hold the exclusive lock on it, so the vnode
1199				 * must be being reclaimed by another thread.
1200				 * Otherwise the doomed vnode is being reclaimed
1201				 * by this thread and zfs_zget is called from
1202				 * ZIL internals.
1203				 */
1204				VI_UNLOCK(vp);
1205				VN_RELE(vp);
1206				goto again;
1207			}
1208			VI_UNLOCK(vp);
1209		}
1210		getnewvnode_drop_reserve();
1211		return (err);
1212	}
1213
1214	/*
1215	 * Not found create new znode/vnode
1216	 * but only if file exists.
1217	 *
1218	 * There is a small window where zfs_vget() could
1219	 * find this object while a file create is still in
1220	 * progress.  This is checked for in zfs_znode_alloc()
1221	 *
1222	 * if zfs_znode_alloc() fails it will drop the hold on the
1223	 * bonus buffer.
1224	 */
1225	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1226	    doi.doi_bonus_type, NULL);
1227	if (zp == NULL) {
1228		err = SET_ERROR(ENOENT);
1229	} else {
1230		*zpp = zp;
1231	}
1232	if (err == 0) {
1233		vnode_t *vp = ZTOV(zp);
1234
1235		err = insmntque(vp, zfsvfs->z_vfs);
1236		if (err == 0) {
1237			vp->v_hash = obj_num;
1238			VOP_UNLOCK(vp, 0);
1239		} else {
1240			zp->z_vnode = NULL;
1241			zfs_znode_dmu_fini(zp);
1242			zfs_znode_free(zp);
1243			*zpp = NULL;
1244		}
1245	}
1246	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1247	getnewvnode_drop_reserve();
1248	return (err);
1249}
1250
1251int
1252zfs_rezget(znode_t *zp)
1253{
1254	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1255	dmu_object_info_t doi;
1256	dmu_buf_t *db;
1257	vnode_t *vp;
1258	uint64_t obj_num = zp->z_id;
1259	uint64_t mode, size;
1260	sa_bulk_attr_t bulk[8];
1261	int err;
1262	int count = 0;
1263	uint64_t gen;
1264
1265	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1266
1267	mutex_enter(&zp->z_acl_lock);
1268	if (zp->z_acl_cached) {
1269		zfs_acl_free(zp->z_acl_cached);
1270		zp->z_acl_cached = NULL;
1271	}
1272
1273	mutex_exit(&zp->z_acl_lock);
1274	ASSERT(zp->z_sa_hdl == NULL);
1275	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1276	if (err) {
1277		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1278		return (err);
1279	}
1280
1281	dmu_object_info_from_db(db, &doi);
1282	if (doi.doi_bonus_type != DMU_OT_SA &&
1283	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1284	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1285	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1286		sa_buf_rele(db, NULL);
1287		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1288		return (SET_ERROR(EINVAL));
1289	}
1290
1291	zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1292	size = zp->z_size;
1293
1294	/* reload cached values */
1295	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1296	    &gen, sizeof (gen));
1297	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1298	    &zp->z_size, sizeof (zp->z_size));
1299	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1300	    &zp->z_links, sizeof (zp->z_links));
1301	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1302	    &zp->z_pflags, sizeof (zp->z_pflags));
1303	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1304	    &zp->z_atime, sizeof (zp->z_atime));
1305	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1306	    &zp->z_uid, sizeof (zp->z_uid));
1307	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1308	    &zp->z_gid, sizeof (zp->z_gid));
1309	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1310	    &mode, sizeof (mode));
1311
1312	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1313		zfs_znode_dmu_fini(zp);
1314		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1315		return (SET_ERROR(EIO));
1316	}
1317
1318	zp->z_mode = mode;
1319
1320	if (gen != zp->z_gen) {
1321		zfs_znode_dmu_fini(zp);
1322		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1323		return (SET_ERROR(EIO));
1324	}
1325
1326	/*
1327	 * It is highly improbable but still quite possible that two
1328	 * objects in different datasets are created with the same
1329	 * object numbers and in transaction groups with the same
1330	 * numbers.  znodes corresponding to those objects would
1331	 * have the same z_id and z_gen, but their other attributes
1332	 * may be different.
1333	 * zfs recv -F may replace one of such objects with the other.
1334	 * As a result file properties recorded in the replaced
1335	 * object's vnode may no longer match the received object's
1336	 * properties.  At present the only cached property is the
1337	 * files type recorded in v_type.
1338	 * So, handle this case by leaving the old vnode and znode
1339	 * disassociated from the actual object.  A new vnode and a
1340	 * znode will be created if the object is accessed
1341	 * (e.g. via a look-up).  The old vnode and znode will be
1342	 * recycled when the last vnode reference is dropped.
1343	 */
1344	vp = ZTOV(zp);
1345	if (vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1346		zfs_znode_dmu_fini(zp);
1347		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1348		return (EIO);
1349	}
1350
1351	zp->z_unlinked = (zp->z_links == 0);
1352	zp->z_blksz = doi.doi_data_block_size;
1353	vn_pages_remove(vp, 0, 0);
1354	if (zp->z_size != size)
1355		vnode_pager_setsize(vp, zp->z_size);
1356
1357	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1358
1359	return (0);
1360}
1361
1362void
1363zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1364{
1365	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1366	objset_t *os = zfsvfs->z_os;
1367	uint64_t obj = zp->z_id;
1368	uint64_t acl_obj = zfs_external_acl(zp);
1369
1370	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1371	if (acl_obj) {
1372		VERIFY(!zp->z_is_sa);
1373		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1374	}
1375	VERIFY(0 == dmu_object_free(os, obj, tx));
1376	zfs_znode_dmu_fini(zp);
1377	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1378	zfs_znode_free(zp);
1379}
1380
1381void
1382zfs_zinactive(znode_t *zp)
1383{
1384	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1385	uint64_t z_id = zp->z_id;
1386
1387	ASSERT(zp->z_sa_hdl);
1388
1389	/*
1390	 * Don't allow a zfs_zget() while were trying to release this znode
1391	 */
1392	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1393
1394	mutex_enter(&zp->z_lock);
1395
1396	/*
1397	 * If this was the last reference to a file with no links,
1398	 * remove the file from the file system.
1399	 */
1400	if (zp->z_unlinked) {
1401		mutex_exit(&zp->z_lock);
1402		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1403		zfs_rmnode(zp);
1404		return;
1405	}
1406
1407	mutex_exit(&zp->z_lock);
1408	zfs_znode_dmu_fini(zp);
1409	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1410	zfs_znode_free(zp);
1411}
1412
1413void
1414zfs_znode_free(znode_t *zp)
1415{
1416	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1417
1418	ASSERT(zp->z_sa_hdl == NULL);
1419	zp->z_vnode = NULL;
1420	mutex_enter(&zfsvfs->z_znodes_lock);
1421	POINTER_INVALIDATE(&zp->z_zfsvfs);
1422	list_remove(&zfsvfs->z_all_znodes, zp);
1423	mutex_exit(&zfsvfs->z_znodes_lock);
1424
1425	if (zp->z_acl_cached) {
1426		zfs_acl_free(zp->z_acl_cached);
1427		zp->z_acl_cached = NULL;
1428	}
1429
1430	kmem_cache_free(znode_cache, zp);
1431
1432#ifdef illumos
1433	VFS_RELE(zfsvfs->z_vfs);
1434#endif
1435}
1436
1437void
1438zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1439    uint64_t ctime[2], boolean_t have_tx)
1440{
1441	timestruc_t	now;
1442
1443	vfs_timestamp(&now);
1444
1445	if (have_tx) {	/* will sa_bulk_update happen really soon? */
1446		zp->z_atime_dirty = 0;
1447		zp->z_seq++;
1448	} else {
1449		zp->z_atime_dirty = 1;
1450	}
1451
1452	if (flag & AT_ATIME) {
1453		ZFS_TIME_ENCODE(&now, zp->z_atime);
1454	}
1455
1456	if (flag & AT_MTIME) {
1457		ZFS_TIME_ENCODE(&now, mtime);
1458		if (zp->z_zfsvfs->z_use_fuids) {
1459			zp->z_pflags |= (ZFS_ARCHIVE |
1460			    ZFS_AV_MODIFIED);
1461		}
1462	}
1463
1464	if (flag & AT_CTIME) {
1465		ZFS_TIME_ENCODE(&now, ctime);
1466		if (zp->z_zfsvfs->z_use_fuids)
1467			zp->z_pflags |= ZFS_ARCHIVE;
1468	}
1469}
1470
1471/*
1472 * Grow the block size for a file.
1473 *
1474 *	IN:	zp	- znode of file to free data in.
1475 *		size	- requested block size
1476 *		tx	- open transaction.
1477 *
1478 * NOTE: this function assumes that the znode is write locked.
1479 */
1480void
1481zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1482{
1483	int		error;
1484	u_longlong_t	dummy;
1485
1486	if (size <= zp->z_blksz)
1487		return;
1488	/*
1489	 * If the file size is already greater than the current blocksize,
1490	 * we will not grow.  If there is more than one block in a file,
1491	 * the blocksize cannot change.
1492	 */
1493	if (zp->z_blksz && zp->z_size > zp->z_blksz)
1494		return;
1495
1496	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1497	    size, 0, tx);
1498
1499	if (error == ENOTSUP)
1500		return;
1501	ASSERT0(error);
1502
1503	/* What blocksize did we actually get? */
1504	dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1505}
1506
1507#ifdef illumos
1508/*
1509 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1510 * be calling back into the fs for a putpage().  E.g.: when truncating
1511 * a file, the pages being "thrown away* don't need to be written out.
1512 */
1513/* ARGSUSED */
1514static int
1515zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1516    int flags, cred_t *cr)
1517{
1518	ASSERT(0);
1519	return (0);
1520}
1521#endif
1522
1523/*
1524 * Increase the file length
1525 *
1526 *	IN:	zp	- znode of file to free data in.
1527 *		end	- new end-of-file
1528 *
1529 *	RETURN:	0 on success, error code on failure
1530 */
1531static int
1532zfs_extend(znode_t *zp, uint64_t end)
1533{
1534	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1535	dmu_tx_t *tx;
1536	rl_t *rl;
1537	uint64_t newblksz;
1538	int error;
1539
1540	/*
1541	 * We will change zp_size, lock the whole file.
1542	 */
1543	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1544
1545	/*
1546	 * Nothing to do if file already at desired length.
1547	 */
1548	if (end <= zp->z_size) {
1549		zfs_range_unlock(rl);
1550		return (0);
1551	}
1552	tx = dmu_tx_create(zfsvfs->z_os);
1553	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1554	zfs_sa_upgrade_txholds(tx, zp);
1555	if (end > zp->z_blksz &&
1556	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1557		/*
1558		 * We are growing the file past the current block size.
1559		 */
1560		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1561			/*
1562			 * File's blocksize is already larger than the
1563			 * "recordsize" property.  Only let it grow to
1564			 * the next power of 2.
1565			 */
1566			ASSERT(!ISP2(zp->z_blksz));
1567			newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1568		} else {
1569			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1570		}
1571		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1572	} else {
1573		newblksz = 0;
1574	}
1575
1576	error = dmu_tx_assign(tx, TXG_WAIT);
1577	if (error) {
1578		dmu_tx_abort(tx);
1579		zfs_range_unlock(rl);
1580		return (error);
1581	}
1582
1583	if (newblksz)
1584		zfs_grow_blocksize(zp, newblksz, tx);
1585
1586	zp->z_size = end;
1587
1588	VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1589	    &zp->z_size, sizeof (zp->z_size), tx));
1590
1591	vnode_pager_setsize(ZTOV(zp), end);
1592
1593	zfs_range_unlock(rl);
1594
1595	dmu_tx_commit(tx);
1596
1597	return (0);
1598}
1599
1600/*
1601 * Free space in a file.
1602 *
1603 *	IN:	zp	- znode of file to free data in.
1604 *		off	- start of section to free.
1605 *		len	- length of section to free.
1606 *
1607 *	RETURN:	0 on success, error code on failure
1608 */
1609static int
1610zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1611{
1612	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1613	rl_t *rl;
1614	int error;
1615
1616	/*
1617	 * Lock the range being freed.
1618	 */
1619	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1620
1621	/*
1622	 * Nothing to do if file already at desired length.
1623	 */
1624	if (off >= zp->z_size) {
1625		zfs_range_unlock(rl);
1626		return (0);
1627	}
1628
1629	if (off + len > zp->z_size)
1630		len = zp->z_size - off;
1631
1632	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1633
1634	if (error == 0) {
1635		/*
1636		 * In FreeBSD we cannot free block in the middle of a file,
1637		 * but only at the end of a file, so this code path should
1638		 * never happen.
1639		 */
1640		vnode_pager_setsize(ZTOV(zp), off);
1641	}
1642
1643	zfs_range_unlock(rl);
1644
1645	return (error);
1646}
1647
1648/*
1649 * Truncate a file
1650 *
1651 *	IN:	zp	- znode of file to free data in.
1652 *		end	- new end-of-file.
1653 *
1654 *	RETURN:	0 on success, error code on failure
1655 */
1656static int
1657zfs_trunc(znode_t *zp, uint64_t end)
1658{
1659	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1660	vnode_t *vp = ZTOV(zp);
1661	dmu_tx_t *tx;
1662	rl_t *rl;
1663	int error;
1664	sa_bulk_attr_t bulk[2];
1665	int count = 0;
1666
1667	/*
1668	 * We will change zp_size, lock the whole file.
1669	 */
1670	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1671
1672	/*
1673	 * Nothing to do if file already at desired length.
1674	 */
1675	if (end >= zp->z_size) {
1676		zfs_range_unlock(rl);
1677		return (0);
1678	}
1679
1680	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1681	if (error) {
1682		zfs_range_unlock(rl);
1683		return (error);
1684	}
1685	tx = dmu_tx_create(zfsvfs->z_os);
1686	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1687	zfs_sa_upgrade_txholds(tx, zp);
1688	dmu_tx_mark_netfree(tx);
1689	error = dmu_tx_assign(tx, TXG_WAIT);
1690	if (error) {
1691		dmu_tx_abort(tx);
1692		zfs_range_unlock(rl);
1693		return (error);
1694	}
1695
1696	zp->z_size = end;
1697	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1698	    NULL, &zp->z_size, sizeof (zp->z_size));
1699
1700	if (end == 0) {
1701		zp->z_pflags &= ~ZFS_SPARSE;
1702		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1703		    NULL, &zp->z_pflags, 8);
1704	}
1705	VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1706
1707	dmu_tx_commit(tx);
1708
1709	/*
1710	 * Clear any mapped pages in the truncated region.  This has to
1711	 * happen outside of the transaction to avoid the possibility of
1712	 * a deadlock with someone trying to push a page that we are
1713	 * about to invalidate.
1714	 */
1715	vnode_pager_setsize(vp, end);
1716
1717	zfs_range_unlock(rl);
1718
1719	return (0);
1720}
1721
1722/*
1723 * Free space in a file
1724 *
1725 *	IN:	zp	- znode of file to free data in.
1726 *		off	- start of range
1727 *		len	- end of range (0 => EOF)
1728 *		flag	- current file open mode flags.
1729 *		log	- TRUE if this action should be logged
1730 *
1731 *	RETURN:	0 on success, error code on failure
1732 */
1733int
1734zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1735{
1736	vnode_t *vp = ZTOV(zp);
1737	dmu_tx_t *tx;
1738	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1739	zilog_t *zilog = zfsvfs->z_log;
1740	uint64_t mode;
1741	uint64_t mtime[2], ctime[2];
1742	sa_bulk_attr_t bulk[3];
1743	int count = 0;
1744	int error;
1745
1746	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1747	    sizeof (mode))) != 0)
1748		return (error);
1749
1750	if (off > zp->z_size) {
1751		error =  zfs_extend(zp, off+len);
1752		if (error == 0 && log)
1753			goto log;
1754		else
1755			return (error);
1756	}
1757
1758	/*
1759	 * Check for any locks in the region to be freed.
1760	 */
1761
1762	if (MANDLOCK(vp, (mode_t)mode)) {
1763		uint64_t length = (len ? len : zp->z_size - off);
1764		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1765			return (error);
1766	}
1767
1768	if (len == 0) {
1769		error = zfs_trunc(zp, off);
1770	} else {
1771		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1772		    off + len > zp->z_size)
1773			error = zfs_extend(zp, off+len);
1774	}
1775	if (error || !log)
1776		return (error);
1777log:
1778	tx = dmu_tx_create(zfsvfs->z_os);
1779	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1780	zfs_sa_upgrade_txholds(tx, zp);
1781	error = dmu_tx_assign(tx, TXG_WAIT);
1782	if (error) {
1783		dmu_tx_abort(tx);
1784		return (error);
1785	}
1786
1787	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1788	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1789	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1790	    NULL, &zp->z_pflags, 8);
1791	zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1792	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1793	ASSERT(error == 0);
1794
1795	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1796
1797	dmu_tx_commit(tx);
1798	return (0);
1799}
1800
1801void
1802zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1803{
1804	uint64_t	moid, obj, sa_obj, version;
1805	uint64_t	sense = ZFS_CASE_SENSITIVE;
1806	uint64_t	norm = 0;
1807	nvpair_t	*elem;
1808	int		error;
1809	int		i;
1810	znode_t		*rootzp = NULL;
1811	zfsvfs_t	*zfsvfs;
1812	vattr_t		vattr;
1813	znode_t		*zp;
1814	zfs_acl_ids_t	acl_ids;
1815
1816	/*
1817	 * First attempt to create master node.
1818	 */
1819	/*
1820	 * In an empty objset, there are no blocks to read and thus
1821	 * there can be no i/o errors (which we assert below).
1822	 */
1823	moid = MASTER_NODE_OBJ;
1824	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1825	    DMU_OT_NONE, 0, tx);
1826	ASSERT(error == 0);
1827
1828	/*
1829	 * Set starting attributes.
1830	 */
1831	version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1832	elem = NULL;
1833	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1834		/* For the moment we expect all zpl props to be uint64_ts */
1835		uint64_t val;
1836		char *name;
1837
1838		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1839		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1840		name = nvpair_name(elem);
1841		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1842			if (val < version)
1843				version = val;
1844		} else {
1845			error = zap_update(os, moid, name, 8, 1, &val, tx);
1846		}
1847		ASSERT(error == 0);
1848		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1849			norm = val;
1850		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1851			sense = val;
1852	}
1853	ASSERT(version != 0);
1854	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1855
1856	/*
1857	 * Create zap object used for SA attribute registration
1858	 */
1859
1860	if (version >= ZPL_VERSION_SA) {
1861		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1862		    DMU_OT_NONE, 0, tx);
1863		error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1864		ASSERT(error == 0);
1865	} else {
1866		sa_obj = 0;
1867	}
1868	/*
1869	 * Create a delete queue.
1870	 */
1871	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1872
1873	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1874	ASSERT(error == 0);
1875
1876	/*
1877	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1878	 * to allow zfs_mknode to work.
1879	 */
1880	VATTR_NULL(&vattr);
1881	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1882	vattr.va_type = VDIR;
1883	vattr.va_mode = S_IFDIR|0755;
1884	vattr.va_uid = crgetuid(cr);
1885	vattr.va_gid = crgetgid(cr);
1886
1887	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1888
1889	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1890	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1891	rootzp->z_moved = 0;
1892	rootzp->z_unlinked = 0;
1893	rootzp->z_atime_dirty = 0;
1894	rootzp->z_is_sa = USE_SA(version, os);
1895
1896	zfsvfs->z_os = os;
1897	zfsvfs->z_parent = zfsvfs;
1898	zfsvfs->z_version = version;
1899	zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1900	zfsvfs->z_use_sa = USE_SA(version, os);
1901	zfsvfs->z_norm = norm;
1902
1903	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1904	    &zfsvfs->z_attr_table);
1905
1906	ASSERT(error == 0);
1907
1908	/*
1909	 * Fold case on file systems that are always or sometimes case
1910	 * insensitive.
1911	 */
1912	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1913		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1914
1915	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1916	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1917	    offsetof(znode_t, z_link_node));
1918
1919	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1920		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1921
1922	rootzp->z_zfsvfs = zfsvfs;
1923	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1924	    cr, NULL, &acl_ids));
1925	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1926	ASSERT3P(zp, ==, rootzp);
1927	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1928	ASSERT(error == 0);
1929	zfs_acl_ids_free(&acl_ids);
1930	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1931
1932	sa_handle_destroy(rootzp->z_sa_hdl);
1933	kmem_cache_free(znode_cache, rootzp);
1934
1935	/*
1936	 * Create shares directory
1937	 */
1938
1939	error = zfs_create_share_dir(zfsvfs, tx);
1940
1941	ASSERT(error == 0);
1942
1943	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1944		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1945	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1946}
1947
1948#endif /* _KERNEL */
1949
1950static int
1951zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1952{
1953	uint64_t sa_obj = 0;
1954	int error;
1955
1956	error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1957	if (error != 0 && error != ENOENT)
1958		return (error);
1959
1960	error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1961	return (error);
1962}
1963
1964static int
1965zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1966    dmu_buf_t **db, void *tag)
1967{
1968	dmu_object_info_t doi;
1969	int error;
1970
1971	if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1972		return (error);
1973
1974	dmu_object_info_from_db(*db, &doi);
1975	if ((doi.doi_bonus_type != DMU_OT_SA &&
1976	    doi.doi_bonus_type != DMU_OT_ZNODE) ||
1977	    doi.doi_bonus_type == DMU_OT_ZNODE &&
1978	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1979		sa_buf_rele(*db, tag);
1980		return (SET_ERROR(ENOTSUP));
1981	}
1982
1983	error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1984	if (error != 0) {
1985		sa_buf_rele(*db, tag);
1986		return (error);
1987	}
1988
1989	return (0);
1990}
1991
1992void
1993zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1994{
1995	sa_handle_destroy(hdl);
1996	sa_buf_rele(db, tag);
1997}
1998
1999/*
2000 * Given an object number, return its parent object number and whether
2001 * or not the object is an extended attribute directory.
2002 */
2003static int
2004zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2005    uint64_t *pobjp, int *is_xattrdir)
2006{
2007	uint64_t parent;
2008	uint64_t pflags;
2009	uint64_t mode;
2010	uint64_t parent_mode;
2011	sa_bulk_attr_t bulk[3];
2012	sa_handle_t *sa_hdl;
2013	dmu_buf_t *sa_db;
2014	int count = 0;
2015	int error;
2016
2017	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2018	    &parent, sizeof (parent));
2019	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2020	    &pflags, sizeof (pflags));
2021	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2022	    &mode, sizeof (mode));
2023
2024	if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2025		return (error);
2026
2027	/*
2028	 * When a link is removed its parent pointer is not changed and will
2029	 * be invalid.  There are two cases where a link is removed but the
2030	 * file stays around, when it goes to the delete queue and when there
2031	 * are additional links.
2032	 */
2033	error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2034	if (error != 0)
2035		return (error);
2036
2037	error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2038	zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2039	if (error != 0)
2040		return (error);
2041
2042	*is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2043
2044	/*
2045	 * Extended attributes can be applied to files, directories, etc.
2046	 * Otherwise the parent must be a directory.
2047	 */
2048	if (!*is_xattrdir && !S_ISDIR(parent_mode))
2049		return (SET_ERROR(EINVAL));
2050
2051	*pobjp = parent;
2052
2053	return (0);
2054}
2055
2056/*
2057 * Given an object number, return some zpl level statistics
2058 */
2059static int
2060zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2061    zfs_stat_t *sb)
2062{
2063	sa_bulk_attr_t bulk[4];
2064	int count = 0;
2065
2066	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2067	    &sb->zs_mode, sizeof (sb->zs_mode));
2068	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2069	    &sb->zs_gen, sizeof (sb->zs_gen));
2070	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2071	    &sb->zs_links, sizeof (sb->zs_links));
2072	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2073	    &sb->zs_ctime, sizeof (sb->zs_ctime));
2074
2075	return (sa_bulk_lookup(hdl, bulk, count));
2076}
2077
2078static int
2079zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2080    sa_attr_type_t *sa_table, char *buf, int len)
2081{
2082	sa_handle_t *sa_hdl;
2083	sa_handle_t *prevhdl = NULL;
2084	dmu_buf_t *prevdb = NULL;
2085	dmu_buf_t *sa_db = NULL;
2086	char *path = buf + len - 1;
2087	int error;
2088
2089	*path = '\0';
2090	sa_hdl = hdl;
2091
2092	for (;;) {
2093		uint64_t pobj;
2094		char component[MAXNAMELEN + 2];
2095		size_t complen;
2096		int is_xattrdir;
2097
2098		if (prevdb)
2099			zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2100
2101		if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2102		    &is_xattrdir)) != 0)
2103			break;
2104
2105		if (pobj == obj) {
2106			if (path[0] != '/')
2107				*--path = '/';
2108			break;
2109		}
2110
2111		component[0] = '/';
2112		if (is_xattrdir) {
2113			(void) sprintf(component + 1, "<xattrdir>");
2114		} else {
2115			error = zap_value_search(osp, pobj, obj,
2116			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
2117			if (error != 0)
2118				break;
2119		}
2120
2121		complen = strlen(component);
2122		path -= complen;
2123		ASSERT(path >= buf);
2124		bcopy(component, path, complen);
2125		obj = pobj;
2126
2127		if (sa_hdl != hdl) {
2128			prevhdl = sa_hdl;
2129			prevdb = sa_db;
2130		}
2131		error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2132		if (error != 0) {
2133			sa_hdl = prevhdl;
2134			sa_db = prevdb;
2135			break;
2136		}
2137	}
2138
2139	if (sa_hdl != NULL && sa_hdl != hdl) {
2140		ASSERT(sa_db != NULL);
2141		zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2142	}
2143
2144	if (error == 0)
2145		(void) memmove(buf, path, buf + len - path);
2146
2147	return (error);
2148}
2149
2150int
2151zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2152{
2153	sa_attr_type_t *sa_table;
2154	sa_handle_t *hdl;
2155	dmu_buf_t *db;
2156	int error;
2157
2158	error = zfs_sa_setup(osp, &sa_table);
2159	if (error != 0)
2160		return (error);
2161
2162	error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2163	if (error != 0)
2164		return (error);
2165
2166	error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2167
2168	zfs_release_sa_handle(hdl, db, FTAG);
2169	return (error);
2170}
2171
2172int
2173zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2174    char *buf, int len)
2175{
2176	char *path = buf + len - 1;
2177	sa_attr_type_t *sa_table;
2178	sa_handle_t *hdl;
2179	dmu_buf_t *db;
2180	int error;
2181
2182	*path = '\0';
2183
2184	error = zfs_sa_setup(osp, &sa_table);
2185	if (error != 0)
2186		return (error);
2187
2188	error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2189	if (error != 0)
2190		return (error);
2191
2192	error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2193	if (error != 0) {
2194		zfs_release_sa_handle(hdl, db, FTAG);
2195		return (error);
2196	}
2197
2198	error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2199
2200	zfs_release_sa_handle(hdl, db, FTAG);
2201	return (error);
2202}
2203