zfs_znode.c revision 300034
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
578	 */
579	if (zp->z_id == zfsvfs->z_root)
580		ZTOV(zp)->v_flag |= VROOT;
581
582	mutex_exit(&zp->z_lock);
583	vn_exists(ZTOV(zp));
584}
585
586void
587zfs_znode_dmu_fini(znode_t *zp)
588{
589	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
590	    zp->z_unlinked ||
591	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
592
593	sa_handle_destroy(zp->z_sa_hdl);
594	zp->z_sa_hdl = NULL;
595}
596
597static void
598zfs_vnode_forget(vnode_t *vp)
599{
600
601	/* copied from insmntque_stddtr */
602	vp->v_data = NULL;
603	vp->v_op = &dead_vnodeops;
604	vgone(vp);
605	vput(vp);
606}
607
608/*
609 * Construct a new znode/vnode and intialize.
610 *
611 * This does not do a call to dmu_set_user() that is
612 * up to the caller to do, in case you don't want to
613 * return the znode
614 */
615static znode_t *
616zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
617    dmu_object_type_t obj_type, sa_handle_t *hdl)
618{
619	znode_t	*zp;
620	vnode_t *vp;
621	uint64_t mode;
622	uint64_t parent;
623	sa_bulk_attr_t bulk[9];
624	int count = 0;
625	int error;
626
627	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
628
629	KASSERT(curthread->td_vp_reserv > 0,
630	    ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
631	error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp);
632	if (error != 0) {
633		kmem_cache_free(znode_cache, zp);
634		return (NULL);
635	}
636	zp->z_vnode = vp;
637	vp->v_data = zp;
638
639	ASSERT(zp->z_dirlocks == NULL);
640	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
641	zp->z_moved = 0;
642
643	/*
644	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
645	 * the zfs_znode_move() callback.
646	 */
647	zp->z_sa_hdl = NULL;
648	zp->z_unlinked = 0;
649	zp->z_atime_dirty = 0;
650	zp->z_mapcnt = 0;
651	zp->z_id = db->db_object;
652	zp->z_blksz = blksz;
653	zp->z_seq = 0x7A4653;
654	zp->z_sync_cnt = 0;
655
656	vp = ZTOV(zp);
657
658	zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
659
660	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
661	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
662	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
663	    &zp->z_size, 8);
664	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
665	    &zp->z_links, 8);
666	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
667	    &zp->z_pflags, 8);
668	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
669	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
670	    &zp->z_atime, 16);
671	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
672	    &zp->z_uid, 8);
673	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
674	    &zp->z_gid, 8);
675
676	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
677		if (hdl == NULL)
678			sa_handle_destroy(zp->z_sa_hdl);
679		zfs_vnode_forget(vp);
680		zp->z_vnode = NULL;
681		kmem_cache_free(znode_cache, zp);
682		return (NULL);
683	}
684
685	zp->z_mode = mode;
686
687	vp->v_type = IFTOVT((mode_t)mode);
688
689	switch (vp->v_type) {
690	case VDIR:
691		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
692		break;
693#ifdef illumos
694	case VBLK:
695	case VCHR:
696		{
697			uint64_t rdev;
698			VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
699			    &rdev, sizeof (rdev)) == 0);
700
701			vp->v_rdev = zfs_cmpldev(rdev);
702		}
703		break;
704#endif
705	case VFIFO:
706#ifdef illumos
707	case VSOCK:
708	case VDOOR:
709#endif
710		vp->v_op = &zfs_fifoops;
711		break;
712	case VREG:
713		if (parent == zfsvfs->z_shares_dir) {
714			ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
715			vp->v_op = &zfs_shareops;
716		}
717		break;
718#ifdef illumos
719	case VLNK:
720		vn_setops(vp, zfs_symvnodeops);
721		break;
722	default:
723		vn_setops(vp, zfs_evnodeops);
724		break;
725#endif
726	}
727
728	mutex_enter(&zfsvfs->z_znodes_lock);
729	list_insert_tail(&zfsvfs->z_all_znodes, zp);
730	membar_producer();
731	/*
732	 * Everything else must be valid before assigning z_zfsvfs makes the
733	 * znode eligible for zfs_znode_move().
734	 */
735	zp->z_zfsvfs = zfsvfs;
736	mutex_exit(&zfsvfs->z_znodes_lock);
737
738	/*
739	 * Acquire vnode lock before making it available to the world.
740	 */
741	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
742	VN_LOCK_AREC(vp);
743	if (vp->v_type != VFIFO)
744		VN_LOCK_ASHARE(vp);
745
746#ifdef illumos
747	VFS_HOLD(zfsvfs->z_vfs);
748#endif
749	return (zp);
750}
751
752static uint64_t empty_xattr;
753static uint64_t pad[4];
754static zfs_acl_phys_t acl_phys;
755/*
756 * Create a new DMU object to hold a zfs znode.
757 *
758 *	IN:	dzp	- parent directory for new znode
759 *		vap	- file attributes for new znode
760 *		tx	- dmu transaction id for zap operations
761 *		cr	- credentials of caller
762 *		flag	- flags:
763 *			  IS_ROOT_NODE	- new object will be root
764 *			  IS_XATTR	- new object is an attribute
765 *		bonuslen - length of bonus buffer
766 *		setaclp  - File/Dir initial ACL
767 *		fuidp	 - Tracks fuid allocation.
768 *
769 *	OUT:	zpp	- allocated znode
770 *
771 */
772void
773zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
774    uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
775{
776	uint64_t	crtime[2], atime[2], mtime[2], ctime[2];
777	uint64_t	mode, size, links, parent, pflags;
778	uint64_t	dzp_pflags = 0;
779	uint64_t	rdev = 0;
780	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
781	dmu_buf_t	*db;
782	timestruc_t	now;
783	uint64_t	gen, obj;
784	int		err;
785	int		bonuslen;
786	sa_handle_t	*sa_hdl;
787	dmu_object_type_t obj_type;
788	sa_bulk_attr_t	sa_attrs[ZPL_END];
789	int		cnt = 0;
790	zfs_acl_locator_cb_t locate = { 0 };
791
792	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
793
794	if (zfsvfs->z_replay) {
795		obj = vap->va_nodeid;
796		now = vap->va_ctime;		/* see zfs_replay_create() */
797		gen = vap->va_nblocks;		/* ditto */
798	} else {
799		obj = 0;
800		vfs_timestamp(&now);
801		gen = dmu_tx_get_txg(tx);
802	}
803
804	obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
805	bonuslen = (obj_type == DMU_OT_SA) ?
806	    DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
807
808	/*
809	 * Create a new DMU object.
810	 */
811	/*
812	 * There's currently no mechanism for pre-reading the blocks that will
813	 * be needed to allocate a new object, so we accept the small chance
814	 * that there will be an i/o error and we will fail one of the
815	 * assertions below.
816	 */
817	if (vap->va_type == VDIR) {
818		if (zfsvfs->z_replay) {
819			VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj,
820			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
821			    obj_type, bonuslen, tx));
822		} else {
823			obj = zap_create_norm(zfsvfs->z_os,
824			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
825			    obj_type, bonuslen, tx);
826		}
827	} else {
828		if (zfsvfs->z_replay) {
829			VERIFY0(dmu_object_claim(zfsvfs->z_os, obj,
830			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
831			    obj_type, bonuslen, tx));
832		} else {
833			obj = dmu_object_alloc(zfsvfs->z_os,
834			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
835			    obj_type, bonuslen, tx);
836		}
837	}
838
839	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
840	VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
841
842	/*
843	 * If this is the root, fix up the half-initialized parent pointer
844	 * to reference the just-allocated physical data area.
845	 */
846	if (flag & IS_ROOT_NODE) {
847		dzp->z_id = obj;
848	} else {
849		dzp_pflags = dzp->z_pflags;
850	}
851
852	/*
853	 * If parent is an xattr, so am I.
854	 */
855	if (dzp_pflags & ZFS_XATTR) {
856		flag |= IS_XATTR;
857	}
858
859	if (zfsvfs->z_use_fuids)
860		pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
861	else
862		pflags = 0;
863
864	if (vap->va_type == VDIR) {
865		size = 2;		/* contents ("." and "..") */
866		links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
867	} else {
868		size = links = 0;
869	}
870
871	if (vap->va_type == VBLK || vap->va_type == VCHR) {
872		rdev = zfs_expldev(vap->va_rdev);
873	}
874
875	parent = dzp->z_id;
876	mode = acl_ids->z_mode;
877	if (flag & IS_XATTR)
878		pflags |= ZFS_XATTR;
879
880	/*
881	 * No execs denied will be deterimed when zfs_mode_compute() is called.
882	 */
883	pflags |= acl_ids->z_aclp->z_hints &
884	    (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
885	    ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
886
887	ZFS_TIME_ENCODE(&now, crtime);
888	ZFS_TIME_ENCODE(&now, ctime);
889
890	if (vap->va_mask & AT_ATIME) {
891		ZFS_TIME_ENCODE(&vap->va_atime, atime);
892	} else {
893		ZFS_TIME_ENCODE(&now, atime);
894	}
895
896	if (vap->va_mask & AT_MTIME) {
897		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
898	} else {
899		ZFS_TIME_ENCODE(&now, mtime);
900	}
901
902	/* Now add in all of the "SA" attributes */
903	VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
904	    &sa_hdl));
905
906	/*
907	 * Setup the array of attributes to be replaced/set on the new file
908	 *
909	 * order for  DMU_OT_ZNODE is critical since it needs to be constructed
910	 * in the old znode_phys_t format.  Don't change this ordering
911	 */
912
913	if (obj_type == DMU_OT_ZNODE) {
914		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
915		    NULL, &atime, 16);
916		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
917		    NULL, &mtime, 16);
918		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
919		    NULL, &ctime, 16);
920		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
921		    NULL, &crtime, 16);
922		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
923		    NULL, &gen, 8);
924		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
925		    NULL, &mode, 8);
926		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
927		    NULL, &size, 8);
928		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
929		    NULL, &parent, 8);
930	} else {
931		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
932		    NULL, &mode, 8);
933		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
934		    NULL, &size, 8);
935		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
936		    NULL, &gen, 8);
937		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
938		    &acl_ids->z_fuid, 8);
939		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
940		    &acl_ids->z_fgid, 8);
941		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
942		    NULL, &parent, 8);
943		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
944		    NULL, &pflags, 8);
945		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
946		    NULL, &atime, 16);
947		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
948		    NULL, &mtime, 16);
949		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
950		    NULL, &ctime, 16);
951		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
952		    NULL, &crtime, 16);
953	}
954
955	SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
956
957	if (obj_type == DMU_OT_ZNODE) {
958		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
959		    &empty_xattr, 8);
960	}
961	if (obj_type == DMU_OT_ZNODE ||
962	    (vap->va_type == VBLK || vap->va_type == VCHR)) {
963		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
964		    NULL, &rdev, 8);
965
966	}
967	if (obj_type == DMU_OT_ZNODE) {
968		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
969		    NULL, &pflags, 8);
970		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
971		    &acl_ids->z_fuid, 8);
972		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
973		    &acl_ids->z_fgid, 8);
974		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
975		    sizeof (uint64_t) * 4);
976		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
977		    &acl_phys, sizeof (zfs_acl_phys_t));
978	} else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
979		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
980		    &acl_ids->z_aclp->z_acl_count, 8);
981		locate.cb_aclp = acl_ids->z_aclp;
982		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
983		    zfs_acl_data_locator, &locate,
984		    acl_ids->z_aclp->z_acl_bytes);
985		mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
986		    acl_ids->z_fuid, acl_ids->z_fgid);
987	}
988
989	VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
990
991	if (!(flag & IS_ROOT_NODE)) {
992		*zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
993		ASSERT(*zpp != NULL);
994	} else {
995		/*
996		 * If we are creating the root node, the "parent" we
997		 * passed in is the znode for the root.
998		 */
999		*zpp = dzp;
1000
1001		(*zpp)->z_sa_hdl = sa_hdl;
1002	}
1003
1004	(*zpp)->z_pflags = pflags;
1005	(*zpp)->z_mode = mode;
1006
1007	if (vap->va_mask & AT_XVATTR)
1008		zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
1009
1010	if (obj_type == DMU_OT_ZNODE ||
1011	    acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1012		VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
1013	}
1014	if (!(flag & IS_ROOT_NODE)) {
1015		vnode_t *vp;
1016
1017		vp = ZTOV(*zpp);
1018		vp->v_vflag |= VV_FORCEINSMQ;
1019		err = insmntque(vp, zfsvfs->z_vfs);
1020		vp->v_vflag &= ~VV_FORCEINSMQ;
1021		KASSERT(err == 0, ("insmntque() failed: error %d", err));
1022	}
1023	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1024}
1025
1026/*
1027 * Update in-core attributes.  It is assumed the caller will be doing an
1028 * sa_bulk_update to push the changes out.
1029 */
1030void
1031zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1032{
1033	xoptattr_t *xoap;
1034
1035	xoap = xva_getxoptattr(xvap);
1036	ASSERT(xoap);
1037
1038	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1039		uint64_t times[2];
1040		ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1041		(void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1042		    &times, sizeof (times), tx);
1043		XVA_SET_RTN(xvap, XAT_CREATETIME);
1044	}
1045	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1046		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1047		    zp->z_pflags, tx);
1048		XVA_SET_RTN(xvap, XAT_READONLY);
1049	}
1050	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1051		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1052		    zp->z_pflags, tx);
1053		XVA_SET_RTN(xvap, XAT_HIDDEN);
1054	}
1055	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1056		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1057		    zp->z_pflags, tx);
1058		XVA_SET_RTN(xvap, XAT_SYSTEM);
1059	}
1060	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1061		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1062		    zp->z_pflags, tx);
1063		XVA_SET_RTN(xvap, XAT_ARCHIVE);
1064	}
1065	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1066		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1067		    zp->z_pflags, tx);
1068		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1069	}
1070	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1071		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1072		    zp->z_pflags, tx);
1073		XVA_SET_RTN(xvap, XAT_NOUNLINK);
1074	}
1075	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1076		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1077		    zp->z_pflags, tx);
1078		XVA_SET_RTN(xvap, XAT_APPENDONLY);
1079	}
1080	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1081		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1082		    zp->z_pflags, tx);
1083		XVA_SET_RTN(xvap, XAT_NODUMP);
1084	}
1085	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1086		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1087		    zp->z_pflags, tx);
1088		XVA_SET_RTN(xvap, XAT_OPAQUE);
1089	}
1090	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1091		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1092		    xoap->xoa_av_quarantined, zp->z_pflags, tx);
1093		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1094	}
1095	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1096		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1097		    zp->z_pflags, tx);
1098		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1099	}
1100	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1101		zfs_sa_set_scanstamp(zp, xvap, tx);
1102		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1103	}
1104	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1105		ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1106		    zp->z_pflags, tx);
1107		XVA_SET_RTN(xvap, XAT_REPARSE);
1108	}
1109	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1110		ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1111		    zp->z_pflags, tx);
1112		XVA_SET_RTN(xvap, XAT_OFFLINE);
1113	}
1114	if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1115		ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1116		    zp->z_pflags, tx);
1117		XVA_SET_RTN(xvap, XAT_SPARSE);
1118	}
1119}
1120
1121int
1122zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1123{
1124	dmu_object_info_t doi;
1125	dmu_buf_t	*db;
1126	znode_t		*zp;
1127	vnode_t		*vp;
1128	sa_handle_t	*hdl;
1129	struct thread	*td;
1130	int locked;
1131	int err;
1132
1133	td = curthread;
1134	getnewvnode_reserve(1);
1135again:
1136	*zpp = NULL;
1137	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1138
1139	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1140	if (err) {
1141		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1142		getnewvnode_drop_reserve();
1143		return (err);
1144	}
1145
1146	dmu_object_info_from_db(db, &doi);
1147	if (doi.doi_bonus_type != DMU_OT_SA &&
1148	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1149	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1150	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1151		sa_buf_rele(db, NULL);
1152		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1153#ifdef __FreeBSD__
1154		getnewvnode_drop_reserve();
1155#endif
1156		return (SET_ERROR(EINVAL));
1157	}
1158
1159	hdl = dmu_buf_get_user(db);
1160	if (hdl != NULL) {
1161		zp  = sa_get_userdata(hdl);
1162
1163
1164		/*
1165		 * Since "SA" does immediate eviction we
1166		 * should never find a sa handle that doesn't
1167		 * know about the znode.
1168		 */
1169
1170		ASSERT3P(zp, !=, NULL);
1171
1172		mutex_enter(&zp->z_lock);
1173		ASSERT3U(zp->z_id, ==, obj_num);
1174		if (zp->z_unlinked) {
1175			err = SET_ERROR(ENOENT);
1176		} else {
1177			vp = ZTOV(zp);
1178			*zpp = zp;
1179			err = 0;
1180		}
1181
1182		/* Don't let the vnode disappear after ZFS_OBJ_HOLD_EXIT. */
1183		if (err == 0)
1184			VN_HOLD(vp);
1185
1186		mutex_exit(&zp->z_lock);
1187		sa_buf_rele(db, NULL);
1188		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1189
1190		if (err == 0) {
1191			locked = VOP_ISLOCKED(vp);
1192			VI_LOCK(vp);
1193			if ((vp->v_iflag & VI_DOOMED) != 0 &&
1194			    locked != LK_EXCLUSIVE) {
1195				/*
1196				 * The vnode is doomed and this thread doesn't
1197				 * hold the exclusive lock on it, so the vnode
1198				 * must be being reclaimed by another thread.
1199				 * Otherwise the doomed vnode is being reclaimed
1200				 * by this thread and zfs_zget is called from
1201				 * ZIL internals.
1202				 */
1203				VI_UNLOCK(vp);
1204				VN_RELE(vp);
1205				goto again;
1206			}
1207			VI_UNLOCK(vp);
1208		}
1209		getnewvnode_drop_reserve();
1210		return (err);
1211	}
1212
1213	/*
1214	 * Not found create new znode/vnode
1215	 * but only if file exists.
1216	 *
1217	 * There is a small window where zfs_vget() could
1218	 * find this object while a file create is still in
1219	 * progress.  This is checked for in zfs_znode_alloc()
1220	 *
1221	 * if zfs_znode_alloc() fails it will drop the hold on the
1222	 * bonus buffer.
1223	 */
1224	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1225	    doi.doi_bonus_type, NULL);
1226	if (zp == NULL) {
1227		err = SET_ERROR(ENOENT);
1228	} else {
1229		*zpp = zp;
1230	}
1231	if (err == 0) {
1232		vnode_t *vp = ZTOV(zp);
1233
1234		err = insmntque(vp, zfsvfs->z_vfs);
1235		if (err == 0) {
1236			vp->v_hash = obj_num;
1237			VOP_UNLOCK(vp, 0);
1238		} else {
1239			zp->z_vnode = NULL;
1240			zfs_znode_dmu_fini(zp);
1241			zfs_znode_free(zp);
1242			*zpp = NULL;
1243		}
1244	}
1245	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1246	getnewvnode_drop_reserve();
1247	return (err);
1248}
1249
1250int
1251zfs_rezget(znode_t *zp)
1252{
1253	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1254	dmu_object_info_t doi;
1255	dmu_buf_t *db;
1256	vnode_t *vp;
1257	uint64_t obj_num = zp->z_id;
1258	uint64_t mode, size;
1259	sa_bulk_attr_t bulk[8];
1260	int err;
1261	int count = 0;
1262	uint64_t gen;
1263
1264	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1265
1266	mutex_enter(&zp->z_acl_lock);
1267	if (zp->z_acl_cached) {
1268		zfs_acl_free(zp->z_acl_cached);
1269		zp->z_acl_cached = NULL;
1270	}
1271
1272	mutex_exit(&zp->z_acl_lock);
1273	ASSERT(zp->z_sa_hdl == NULL);
1274	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1275	if (err) {
1276		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1277		return (err);
1278	}
1279
1280	dmu_object_info_from_db(db, &doi);
1281	if (doi.doi_bonus_type != DMU_OT_SA &&
1282	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1283	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1284	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1285		sa_buf_rele(db, NULL);
1286		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1287		return (SET_ERROR(EINVAL));
1288	}
1289
1290	zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1291	size = zp->z_size;
1292
1293	/* reload cached values */
1294	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1295	    &gen, sizeof (gen));
1296	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1297	    &zp->z_size, sizeof (zp->z_size));
1298	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1299	    &zp->z_links, sizeof (zp->z_links));
1300	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1301	    &zp->z_pflags, sizeof (zp->z_pflags));
1302	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1303	    &zp->z_atime, sizeof (zp->z_atime));
1304	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1305	    &zp->z_uid, sizeof (zp->z_uid));
1306	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1307	    &zp->z_gid, sizeof (zp->z_gid));
1308	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1309	    &mode, sizeof (mode));
1310
1311	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1312		zfs_znode_dmu_fini(zp);
1313		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1314		return (SET_ERROR(EIO));
1315	}
1316
1317	zp->z_mode = mode;
1318
1319	if (gen != zp->z_gen) {
1320		zfs_znode_dmu_fini(zp);
1321		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1322		return (SET_ERROR(EIO));
1323	}
1324
1325	/*
1326	 * It is highly improbable but still quite possible that two
1327	 * objects in different datasets are created with the same
1328	 * object numbers and in transaction groups with the same
1329	 * numbers.  znodes corresponding to those objects would
1330	 * have the same z_id and z_gen, but their other attributes
1331	 * may be different.
1332	 * zfs recv -F may replace one of such objects with the other.
1333	 * As a result file properties recorded in the replaced
1334	 * object's vnode may no longer match the received object's
1335	 * properties.  At present the only cached property is the
1336	 * files type recorded in v_type.
1337	 * So, handle this case by leaving the old vnode and znode
1338	 * disassociated from the actual object.  A new vnode and a
1339	 * znode will be created if the object is accessed
1340	 * (e.g. via a look-up).  The old vnode and znode will be
1341	 * recycled when the last vnode reference is dropped.
1342	 */
1343	vp = ZTOV(zp);
1344	if (vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1345		zfs_znode_dmu_fini(zp);
1346		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1347		return (EIO);
1348	}
1349
1350	zp->z_unlinked = (zp->z_links == 0);
1351	zp->z_blksz = doi.doi_data_block_size;
1352	vn_pages_remove(vp, 0, 0);
1353	if (zp->z_size != size)
1354		vnode_pager_setsize(vp, zp->z_size);
1355
1356	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1357
1358	return (0);
1359}
1360
1361void
1362zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1363{
1364	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1365	objset_t *os = zfsvfs->z_os;
1366	uint64_t obj = zp->z_id;
1367	uint64_t acl_obj = zfs_external_acl(zp);
1368
1369	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1370	if (acl_obj) {
1371		VERIFY(!zp->z_is_sa);
1372		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1373	}
1374	VERIFY(0 == dmu_object_free(os, obj, tx));
1375	zfs_znode_dmu_fini(zp);
1376	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1377	zfs_znode_free(zp);
1378}
1379
1380void
1381zfs_zinactive(znode_t *zp)
1382{
1383	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1384	uint64_t z_id = zp->z_id;
1385
1386	ASSERT(zp->z_sa_hdl);
1387
1388	/*
1389	 * Don't allow a zfs_zget() while were trying to release this znode
1390	 */
1391	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1392
1393	mutex_enter(&zp->z_lock);
1394
1395	/*
1396	 * If this was the last reference to a file with no links,
1397	 * remove the file from the file system.
1398	 */
1399	if (zp->z_unlinked) {
1400		mutex_exit(&zp->z_lock);
1401		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1402		zfs_rmnode(zp);
1403		return;
1404	}
1405
1406	mutex_exit(&zp->z_lock);
1407	zfs_znode_dmu_fini(zp);
1408	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1409	zfs_znode_free(zp);
1410}
1411
1412void
1413zfs_znode_free(znode_t *zp)
1414{
1415	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1416
1417	ASSERT(zp->z_sa_hdl == NULL);
1418	zp->z_vnode = NULL;
1419	mutex_enter(&zfsvfs->z_znodes_lock);
1420	POINTER_INVALIDATE(&zp->z_zfsvfs);
1421	list_remove(&zfsvfs->z_all_znodes, zp);
1422	mutex_exit(&zfsvfs->z_znodes_lock);
1423
1424	if (zp->z_acl_cached) {
1425		zfs_acl_free(zp->z_acl_cached);
1426		zp->z_acl_cached = NULL;
1427	}
1428
1429	kmem_cache_free(znode_cache, zp);
1430
1431#ifdef illumos
1432	VFS_RELE(zfsvfs->z_vfs);
1433#endif
1434}
1435
1436void
1437zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1438    uint64_t ctime[2], boolean_t have_tx)
1439{
1440	timestruc_t	now;
1441
1442	vfs_timestamp(&now);
1443
1444	if (have_tx) {	/* will sa_bulk_update happen really soon? */
1445		zp->z_atime_dirty = 0;
1446		zp->z_seq++;
1447	} else {
1448		zp->z_atime_dirty = 1;
1449	}
1450
1451	if (flag & AT_ATIME) {
1452		ZFS_TIME_ENCODE(&now, zp->z_atime);
1453	}
1454
1455	if (flag & AT_MTIME) {
1456		ZFS_TIME_ENCODE(&now, mtime);
1457		if (zp->z_zfsvfs->z_use_fuids) {
1458			zp->z_pflags |= (ZFS_ARCHIVE |
1459			    ZFS_AV_MODIFIED);
1460		}
1461	}
1462
1463	if (flag & AT_CTIME) {
1464		ZFS_TIME_ENCODE(&now, ctime);
1465		if (zp->z_zfsvfs->z_use_fuids)
1466			zp->z_pflags |= ZFS_ARCHIVE;
1467	}
1468}
1469
1470/*
1471 * Grow the block size for a file.
1472 *
1473 *	IN:	zp	- znode of file to free data in.
1474 *		size	- requested block size
1475 *		tx	- open transaction.
1476 *
1477 * NOTE: this function assumes that the znode is write locked.
1478 */
1479void
1480zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1481{
1482	int		error;
1483	u_longlong_t	dummy;
1484
1485	if (size <= zp->z_blksz)
1486		return;
1487	/*
1488	 * If the file size is already greater than the current blocksize,
1489	 * we will not grow.  If there is more than one block in a file,
1490	 * the blocksize cannot change.
1491	 */
1492	if (zp->z_blksz && zp->z_size > zp->z_blksz)
1493		return;
1494
1495	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1496	    size, 0, tx);
1497
1498	if (error == ENOTSUP)
1499		return;
1500	ASSERT0(error);
1501
1502	/* What blocksize did we actually get? */
1503	dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1504}
1505
1506#ifdef illumos
1507/*
1508 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1509 * be calling back into the fs for a putpage().  E.g.: when truncating
1510 * a file, the pages being "thrown away* don't need to be written out.
1511 */
1512/* ARGSUSED */
1513static int
1514zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1515    int flags, cred_t *cr)
1516{
1517	ASSERT(0);
1518	return (0);
1519}
1520#endif
1521
1522/*
1523 * Increase the file length
1524 *
1525 *	IN:	zp	- znode of file to free data in.
1526 *		end	- new end-of-file
1527 *
1528 *	RETURN:	0 on success, error code on failure
1529 */
1530static int
1531zfs_extend(znode_t *zp, uint64_t end)
1532{
1533	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1534	dmu_tx_t *tx;
1535	rl_t *rl;
1536	uint64_t newblksz;
1537	int error;
1538
1539	/*
1540	 * We will change zp_size, lock the whole file.
1541	 */
1542	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1543
1544	/*
1545	 * Nothing to do if file already at desired length.
1546	 */
1547	if (end <= zp->z_size) {
1548		zfs_range_unlock(rl);
1549		return (0);
1550	}
1551	tx = dmu_tx_create(zfsvfs->z_os);
1552	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1553	zfs_sa_upgrade_txholds(tx, zp);
1554	if (end > zp->z_blksz &&
1555	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1556		/*
1557		 * We are growing the file past the current block size.
1558		 */
1559		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1560			/*
1561			 * File's blocksize is already larger than the
1562			 * "recordsize" property.  Only let it grow to
1563			 * the next power of 2.
1564			 */
1565			ASSERT(!ISP2(zp->z_blksz));
1566			newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1567		} else {
1568			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1569		}
1570		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1571	} else {
1572		newblksz = 0;
1573	}
1574
1575	error = dmu_tx_assign(tx, TXG_WAIT);
1576	if (error) {
1577		dmu_tx_abort(tx);
1578		zfs_range_unlock(rl);
1579		return (error);
1580	}
1581
1582	if (newblksz)
1583		zfs_grow_blocksize(zp, newblksz, tx);
1584
1585	zp->z_size = end;
1586
1587	VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1588	    &zp->z_size, sizeof (zp->z_size), tx));
1589
1590	vnode_pager_setsize(ZTOV(zp), end);
1591
1592	zfs_range_unlock(rl);
1593
1594	dmu_tx_commit(tx);
1595
1596	return (0);
1597}
1598
1599/*
1600 * Free space in a file.
1601 *
1602 *	IN:	zp	- znode of file to free data in.
1603 *		off	- start of section to free.
1604 *		len	- length of section to free.
1605 *
1606 *	RETURN:	0 on success, error code on failure
1607 */
1608static int
1609zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1610{
1611	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1612	rl_t *rl;
1613	int error;
1614
1615	/*
1616	 * Lock the range being freed.
1617	 */
1618	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1619
1620	/*
1621	 * Nothing to do if file already at desired length.
1622	 */
1623	if (off >= zp->z_size) {
1624		zfs_range_unlock(rl);
1625		return (0);
1626	}
1627
1628	if (off + len > zp->z_size)
1629		len = zp->z_size - off;
1630
1631	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1632
1633	if (error == 0) {
1634		/*
1635		 * In FreeBSD we cannot free block in the middle of a file,
1636		 * but only at the end of a file, so this code path should
1637		 * never happen.
1638		 */
1639		vnode_pager_setsize(ZTOV(zp), off);
1640	}
1641
1642	zfs_range_unlock(rl);
1643
1644	return (error);
1645}
1646
1647/*
1648 * Truncate a file
1649 *
1650 *	IN:	zp	- znode of file to free data in.
1651 *		end	- new end-of-file.
1652 *
1653 *	RETURN:	0 on success, error code on failure
1654 */
1655static int
1656zfs_trunc(znode_t *zp, uint64_t end)
1657{
1658	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1659	vnode_t *vp = ZTOV(zp);
1660	dmu_tx_t *tx;
1661	rl_t *rl;
1662	int error;
1663	sa_bulk_attr_t bulk[2];
1664	int count = 0;
1665
1666	/*
1667	 * We will change zp_size, lock the whole file.
1668	 */
1669	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1670
1671	/*
1672	 * Nothing to do if file already at desired length.
1673	 */
1674	if (end >= zp->z_size) {
1675		zfs_range_unlock(rl);
1676		return (0);
1677	}
1678
1679	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1680	if (error) {
1681		zfs_range_unlock(rl);
1682		return (error);
1683	}
1684	tx = dmu_tx_create(zfsvfs->z_os);
1685	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1686	zfs_sa_upgrade_txholds(tx, zp);
1687	dmu_tx_mark_netfree(tx);
1688	error = dmu_tx_assign(tx, TXG_WAIT);
1689	if (error) {
1690		dmu_tx_abort(tx);
1691		zfs_range_unlock(rl);
1692		return (error);
1693	}
1694
1695	zp->z_size = end;
1696	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1697	    NULL, &zp->z_size, sizeof (zp->z_size));
1698
1699	if (end == 0) {
1700		zp->z_pflags &= ~ZFS_SPARSE;
1701		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1702		    NULL, &zp->z_pflags, 8);
1703	}
1704	VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1705
1706	dmu_tx_commit(tx);
1707
1708	/*
1709	 * Clear any mapped pages in the truncated region.  This has to
1710	 * happen outside of the transaction to avoid the possibility of
1711	 * a deadlock with someone trying to push a page that we are
1712	 * about to invalidate.
1713	 */
1714	vnode_pager_setsize(vp, end);
1715
1716	zfs_range_unlock(rl);
1717
1718	return (0);
1719}
1720
1721/*
1722 * Free space in a file
1723 *
1724 *	IN:	zp	- znode of file to free data in.
1725 *		off	- start of range
1726 *		len	- end of range (0 => EOF)
1727 *		flag	- current file open mode flags.
1728 *		log	- TRUE if this action should be logged
1729 *
1730 *	RETURN:	0 on success, error code on failure
1731 */
1732int
1733zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1734{
1735	vnode_t *vp = ZTOV(zp);
1736	dmu_tx_t *tx;
1737	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1738	zilog_t *zilog = zfsvfs->z_log;
1739	uint64_t mode;
1740	uint64_t mtime[2], ctime[2];
1741	sa_bulk_attr_t bulk[3];
1742	int count = 0;
1743	int error;
1744
1745	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1746	    sizeof (mode))) != 0)
1747		return (error);
1748
1749	if (off > zp->z_size) {
1750		error =  zfs_extend(zp, off+len);
1751		if (error == 0 && log)
1752			goto log;
1753		else
1754			return (error);
1755	}
1756
1757	/*
1758	 * Check for any locks in the region to be freed.
1759	 */
1760
1761	if (MANDLOCK(vp, (mode_t)mode)) {
1762		uint64_t length = (len ? len : zp->z_size - off);
1763		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1764			return (error);
1765	}
1766
1767	if (len == 0) {
1768		error = zfs_trunc(zp, off);
1769	} else {
1770		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1771		    off + len > zp->z_size)
1772			error = zfs_extend(zp, off+len);
1773	}
1774	if (error || !log)
1775		return (error);
1776log:
1777	tx = dmu_tx_create(zfsvfs->z_os);
1778	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1779	zfs_sa_upgrade_txholds(tx, zp);
1780	error = dmu_tx_assign(tx, TXG_WAIT);
1781	if (error) {
1782		dmu_tx_abort(tx);
1783		return (error);
1784	}
1785
1786	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1787	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1788	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1789	    NULL, &zp->z_pflags, 8);
1790	zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1791	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1792	ASSERT(error == 0);
1793
1794	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1795
1796	dmu_tx_commit(tx);
1797	return (0);
1798}
1799
1800void
1801zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1802{
1803	uint64_t	moid, obj, sa_obj, version;
1804	uint64_t	sense = ZFS_CASE_SENSITIVE;
1805	uint64_t	norm = 0;
1806	nvpair_t	*elem;
1807	int		error;
1808	int		i;
1809	znode_t		*rootzp = NULL;
1810	zfsvfs_t	*zfsvfs;
1811	vattr_t		vattr;
1812	znode_t		*zp;
1813	zfs_acl_ids_t	acl_ids;
1814
1815	/*
1816	 * First attempt to create master node.
1817	 */
1818	/*
1819	 * In an empty objset, there are no blocks to read and thus
1820	 * there can be no i/o errors (which we assert below).
1821	 */
1822	moid = MASTER_NODE_OBJ;
1823	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1824	    DMU_OT_NONE, 0, tx);
1825	ASSERT(error == 0);
1826
1827	/*
1828	 * Set starting attributes.
1829	 */
1830	version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1831	elem = NULL;
1832	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1833		/* For the moment we expect all zpl props to be uint64_ts */
1834		uint64_t val;
1835		char *name;
1836
1837		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1838		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1839		name = nvpair_name(elem);
1840		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1841			if (val < version)
1842				version = val;
1843		} else {
1844			error = zap_update(os, moid, name, 8, 1, &val, tx);
1845		}
1846		ASSERT(error == 0);
1847		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1848			norm = val;
1849		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1850			sense = val;
1851	}
1852	ASSERT(version != 0);
1853	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1854
1855	/*
1856	 * Create zap object used for SA attribute registration
1857	 */
1858
1859	if (version >= ZPL_VERSION_SA) {
1860		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1861		    DMU_OT_NONE, 0, tx);
1862		error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1863		ASSERT(error == 0);
1864	} else {
1865		sa_obj = 0;
1866	}
1867	/*
1868	 * Create a delete queue.
1869	 */
1870	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1871
1872	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1873	ASSERT(error == 0);
1874
1875	/*
1876	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1877	 * to allow zfs_mknode to work.
1878	 */
1879	VATTR_NULL(&vattr);
1880	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1881	vattr.va_type = VDIR;
1882	vattr.va_mode = S_IFDIR|0755;
1883	vattr.va_uid = crgetuid(cr);
1884	vattr.va_gid = crgetgid(cr);
1885
1886	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1887
1888	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1889	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1890	rootzp->z_moved = 0;
1891	rootzp->z_unlinked = 0;
1892	rootzp->z_atime_dirty = 0;
1893	rootzp->z_is_sa = USE_SA(version, os);
1894
1895	zfsvfs->z_os = os;
1896	zfsvfs->z_parent = zfsvfs;
1897	zfsvfs->z_version = version;
1898	zfsvfs->z_use_fuids = USE_FUIDS(version, os);
1899	zfsvfs->z_use_sa = USE_SA(version, os);
1900	zfsvfs->z_norm = norm;
1901
1902	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1903	    &zfsvfs->z_attr_table);
1904
1905	ASSERT(error == 0);
1906
1907	/*
1908	 * Fold case on file systems that are always or sometimes case
1909	 * insensitive.
1910	 */
1911	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1912		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1913
1914	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1915	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1916	    offsetof(znode_t, z_link_node));
1917
1918	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1919		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1920
1921	rootzp->z_zfsvfs = zfsvfs;
1922	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1923	    cr, NULL, &acl_ids));
1924	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1925	ASSERT3P(zp, ==, rootzp);
1926	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1927	ASSERT(error == 0);
1928	zfs_acl_ids_free(&acl_ids);
1929	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1930
1931	sa_handle_destroy(rootzp->z_sa_hdl);
1932	kmem_cache_free(znode_cache, rootzp);
1933
1934	/*
1935	 * Create shares directory
1936	 */
1937
1938	error = zfs_create_share_dir(zfsvfs, tx);
1939
1940	ASSERT(error == 0);
1941
1942	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1943		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1944	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1945}
1946
1947#endif /* _KERNEL */
1948
1949static int
1950zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1951{
1952	uint64_t sa_obj = 0;
1953	int error;
1954
1955	error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1956	if (error != 0 && error != ENOENT)
1957		return (error);
1958
1959	error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1960	return (error);
1961}
1962
1963static int
1964zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1965    dmu_buf_t **db, void *tag)
1966{
1967	dmu_object_info_t doi;
1968	int error;
1969
1970	if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1971		return (error);
1972
1973	dmu_object_info_from_db(*db, &doi);
1974	if ((doi.doi_bonus_type != DMU_OT_SA &&
1975	    doi.doi_bonus_type != DMU_OT_ZNODE) ||
1976	    doi.doi_bonus_type == DMU_OT_ZNODE &&
1977	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1978		sa_buf_rele(*db, tag);
1979		return (SET_ERROR(ENOTSUP));
1980	}
1981
1982	error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1983	if (error != 0) {
1984		sa_buf_rele(*db, tag);
1985		return (error);
1986	}
1987
1988	return (0);
1989}
1990
1991void
1992zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1993{
1994	sa_handle_destroy(hdl);
1995	sa_buf_rele(db, tag);
1996}
1997
1998/*
1999 * Given an object number, return its parent object number and whether
2000 * or not the object is an extended attribute directory.
2001 */
2002static int
2003zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2004    uint64_t *pobjp, int *is_xattrdir)
2005{
2006	uint64_t parent;
2007	uint64_t pflags;
2008	uint64_t mode;
2009	uint64_t parent_mode;
2010	sa_bulk_attr_t bulk[3];
2011	sa_handle_t *sa_hdl;
2012	dmu_buf_t *sa_db;
2013	int count = 0;
2014	int error;
2015
2016	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2017	    &parent, sizeof (parent));
2018	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2019	    &pflags, sizeof (pflags));
2020	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2021	    &mode, sizeof (mode));
2022
2023	if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2024		return (error);
2025
2026	/*
2027	 * When a link is removed its parent pointer is not changed and will
2028	 * be invalid.  There are two cases where a link is removed but the
2029	 * file stays around, when it goes to the delete queue and when there
2030	 * are additional links.
2031	 */
2032	error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2033	if (error != 0)
2034		return (error);
2035
2036	error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2037	zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2038	if (error != 0)
2039		return (error);
2040
2041	*is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2042
2043	/*
2044	 * Extended attributes can be applied to files, directories, etc.
2045	 * Otherwise the parent must be a directory.
2046	 */
2047	if (!*is_xattrdir && !S_ISDIR(parent_mode))
2048		return (SET_ERROR(EINVAL));
2049
2050	*pobjp = parent;
2051
2052	return (0);
2053}
2054
2055/*
2056 * Given an object number, return some zpl level statistics
2057 */
2058static int
2059zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2060    zfs_stat_t *sb)
2061{
2062	sa_bulk_attr_t bulk[4];
2063	int count = 0;
2064
2065	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2066	    &sb->zs_mode, sizeof (sb->zs_mode));
2067	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2068	    &sb->zs_gen, sizeof (sb->zs_gen));
2069	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2070	    &sb->zs_links, sizeof (sb->zs_links));
2071	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2072	    &sb->zs_ctime, sizeof (sb->zs_ctime));
2073
2074	return (sa_bulk_lookup(hdl, bulk, count));
2075}
2076
2077static int
2078zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2079    sa_attr_type_t *sa_table, char *buf, int len)
2080{
2081	sa_handle_t *sa_hdl;
2082	sa_handle_t *prevhdl = NULL;
2083	dmu_buf_t *prevdb = NULL;
2084	dmu_buf_t *sa_db = NULL;
2085	char *path = buf + len - 1;
2086	int error;
2087
2088	*path = '\0';
2089	sa_hdl = hdl;
2090
2091	for (;;) {
2092		uint64_t pobj;
2093		char component[MAXNAMELEN + 2];
2094		size_t complen;
2095		int is_xattrdir;
2096
2097		if (prevdb)
2098			zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2099
2100		if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2101		    &is_xattrdir)) != 0)
2102			break;
2103
2104		if (pobj == obj) {
2105			if (path[0] != '/')
2106				*--path = '/';
2107			break;
2108		}
2109
2110		component[0] = '/';
2111		if (is_xattrdir) {
2112			(void) sprintf(component + 1, "<xattrdir>");
2113		} else {
2114			error = zap_value_search(osp, pobj, obj,
2115			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
2116			if (error != 0)
2117				break;
2118		}
2119
2120		complen = strlen(component);
2121		path -= complen;
2122		ASSERT(path >= buf);
2123		bcopy(component, path, complen);
2124		obj = pobj;
2125
2126		if (sa_hdl != hdl) {
2127			prevhdl = sa_hdl;
2128			prevdb = sa_db;
2129		}
2130		error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2131		if (error != 0) {
2132			sa_hdl = prevhdl;
2133			sa_db = prevdb;
2134			break;
2135		}
2136	}
2137
2138	if (sa_hdl != NULL && sa_hdl != hdl) {
2139		ASSERT(sa_db != NULL);
2140		zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2141	}
2142
2143	if (error == 0)
2144		(void) memmove(buf, path, buf + len - path);
2145
2146	return (error);
2147}
2148
2149int
2150zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2151{
2152	sa_attr_type_t *sa_table;
2153	sa_handle_t *hdl;
2154	dmu_buf_t *db;
2155	int error;
2156
2157	error = zfs_sa_setup(osp, &sa_table);
2158	if (error != 0)
2159		return (error);
2160
2161	error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2162	if (error != 0)
2163		return (error);
2164
2165	error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2166
2167	zfs_release_sa_handle(hdl, db, FTAG);
2168	return (error);
2169}
2170
2171int
2172zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2173    char *buf, int len)
2174{
2175	char *path = buf + len - 1;
2176	sa_attr_type_t *sa_table;
2177	sa_handle_t *hdl;
2178	dmu_buf_t *db;
2179	int error;
2180
2181	*path = '\0';
2182
2183	error = zfs_sa_setup(osp, &sa_table);
2184	if (error != 0)
2185		return (error);
2186
2187	error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2188	if (error != 0)
2189		return (error);
2190
2191	error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2192	if (error != 0) {
2193		zfs_release_sa_handle(hdl, db, FTAG);
2194		return (error);
2195	}
2196
2197	error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2198
2199	zfs_release_sa_handle(hdl, db, FTAG);
2200	return (error);
2201}
2202