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