xref: /titanic_52/usr/src/uts/common/fs/zfs/zfs_znode.c (revision 8d61a337e3509e92fccb0fdeb26db712c21d43cd)
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 
67 #include "zfs_prop.h"
68 #include "zfs_comutil.h"
69 
70 /*
71  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
72  * turned on when DEBUG is also defined.
73  */
74 #ifdef	DEBUG
75 #define	ZNODE_STATS
76 #endif	/* DEBUG */
77 
78 #ifdef	ZNODE_STATS
79 #define	ZNODE_STAT_ADD(stat)			((stat)++)
80 #else
81 #define	ZNODE_STAT_ADD(stat)			/* nothing */
82 #endif	/* ZNODE_STATS */
83 
84 #define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
85 #define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
86 
87 /*
88  * Functions needed for userland (ie: libzpool) are not put under
89  * #ifdef_KERNEL; the rest of the functions have dependencies
90  * (such as VFS logic) that will not compile easily in userland.
91  */
92 #ifdef _KERNEL
93 /*
94  * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
95  * be freed before it can be safely accessed.
96  */
97 krwlock_t zfsvfs_lock;
98 
99 static kmem_cache_t *znode_cache = NULL;
100 
101 /*ARGSUSED*/
102 static void
103 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
104 {
105 	/*
106 	 * We should never drop all dbuf refs without first clearing
107 	 * the eviction callback.
108 	 */
109 	panic("evicting znode %p\n", user_ptr);
110 }
111 
112 /*ARGSUSED*/
113 static int
114 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
115 {
116 	znode_t *zp = buf;
117 
118 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
119 
120 	zp->z_vnode = vn_alloc(kmflags);
121 	if (zp->z_vnode == NULL) {
122 		return (-1);
123 	}
124 	ZTOV(zp)->v_data = zp;
125 
126 	list_link_init(&zp->z_link_node);
127 
128 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
129 	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
130 	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
131 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
132 
133 	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
134 	avl_create(&zp->z_range_avl, zfs_range_compare,
135 	    sizeof (rl_t), offsetof(rl_t, r_node));
136 
137 	zp->z_dirlocks = NULL;
138 	zp->z_acl_cached = NULL;
139 	return (0);
140 }
141 
142 /*ARGSUSED*/
143 static void
144 zfs_znode_cache_destructor(void *buf, void *arg)
145 {
146 	znode_t *zp = buf;
147 
148 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
149 	ASSERT(ZTOV(zp)->v_data == zp);
150 	vn_free(ZTOV(zp));
151 	ASSERT(!list_link_active(&zp->z_link_node));
152 	mutex_destroy(&zp->z_lock);
153 	rw_destroy(&zp->z_parent_lock);
154 	rw_destroy(&zp->z_name_lock);
155 	mutex_destroy(&zp->z_acl_lock);
156 	avl_destroy(&zp->z_range_avl);
157 	mutex_destroy(&zp->z_range_lock);
158 
159 	ASSERT(zp->z_dirlocks == NULL);
160 	ASSERT(zp->z_acl_cached == NULL);
161 }
162 
163 #ifdef	ZNODE_STATS
164 static struct {
165 	uint64_t zms_zfsvfs_invalid;
166 	uint64_t zms_zfsvfs_recheck1;
167 	uint64_t zms_zfsvfs_unmounted;
168 	uint64_t zms_zfsvfs_recheck2;
169 	uint64_t zms_obj_held;
170 	uint64_t zms_vnode_locked;
171 	uint64_t zms_not_only_dnlc;
172 } znode_move_stats;
173 #endif	/* ZNODE_STATS */
174 
175 static void
176 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
177 {
178 	vnode_t *vp;
179 
180 	/* Copy fields. */
181 	nzp->z_zfsvfs = ozp->z_zfsvfs;
182 
183 	/* Swap vnodes. */
184 	vp = nzp->z_vnode;
185 	nzp->z_vnode = ozp->z_vnode;
186 	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
187 	ZTOV(ozp)->v_data = ozp;
188 	ZTOV(nzp)->v_data = nzp;
189 
190 	nzp->z_id = ozp->z_id;
191 	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
192 	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
193 	nzp->z_unlinked = ozp->z_unlinked;
194 	nzp->z_atime_dirty = ozp->z_atime_dirty;
195 	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
196 	nzp->z_blksz = ozp->z_blksz;
197 	nzp->z_seq = ozp->z_seq;
198 	nzp->z_mapcnt = ozp->z_mapcnt;
199 	nzp->z_last_itx = ozp->z_last_itx;
200 	nzp->z_gen = ozp->z_gen;
201 	nzp->z_sync_cnt = ozp->z_sync_cnt;
202 	nzp->z_is_sa = ozp->z_is_sa;
203 	nzp->z_sa_hdl = ozp->z_sa_hdl;
204 	bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
205 	nzp->z_links = ozp->z_links;
206 	nzp->z_size = ozp->z_size;
207 	nzp->z_pflags = ozp->z_pflags;
208 	nzp->z_uid = ozp->z_uid;
209 	nzp->z_gid = ozp->z_gid;
210 	nzp->z_mode = ozp->z_mode;
211 
212 	/*
213 	 * Since this is just an idle znode and kmem is already dealing with
214 	 * memory pressure, release any cached ACL.
215 	 */
216 	if (ozp->z_acl_cached) {
217 		zfs_acl_free(ozp->z_acl_cached);
218 		ozp->z_acl_cached = NULL;
219 	}
220 
221 	sa_set_userp(nzp->z_sa_hdl, nzp);
222 
223 	/*
224 	 * Invalidate the original znode by clearing fields that provide a
225 	 * pointer back to the znode. Set the low bit of the vfs pointer to
226 	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
227 	 * subsequent callback.
228 	 */
229 	ozp->z_sa_hdl = NULL;
230 	POINTER_INVALIDATE(&ozp->z_zfsvfs);
231 }
232 
233 /*ARGSUSED*/
234 static kmem_cbrc_t
235 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
236 {
237 	znode_t *ozp = buf, *nzp = newbuf;
238 	zfsvfs_t *zfsvfs;
239 	vnode_t *vp;
240 
241 	/*
242 	 * The znode is on the file system's list of known znodes if the vfs
243 	 * pointer is valid. We set the low bit of the vfs pointer when freeing
244 	 * the znode to invalidate it, and the memory patterns written by kmem
245 	 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
246 	 * created znode sets the vfs pointer last of all to indicate that the
247 	 * znode is known and in a valid state to be moved by this function.
248 	 */
249 	zfsvfs = ozp->z_zfsvfs;
250 	if (!POINTER_IS_VALID(zfsvfs)) {
251 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
252 		return (KMEM_CBRC_DONT_KNOW);
253 	}
254 
255 	/*
256 	 * Close a small window in which it's possible that the filesystem could
257 	 * be unmounted and freed, and zfsvfs, though valid in the previous
258 	 * statement, could point to unrelated memory by the time we try to
259 	 * prevent the filesystem from being unmounted.
260 	 */
261 	rw_enter(&zfsvfs_lock, RW_WRITER);
262 	if (zfsvfs != ozp->z_zfsvfs) {
263 		rw_exit(&zfsvfs_lock);
264 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
265 		return (KMEM_CBRC_DONT_KNOW);
266 	}
267 
268 	/*
269 	 * If the znode is still valid, then so is the file system. We know that
270 	 * no valid file system can be freed while we hold zfsvfs_lock, so we
271 	 * can safely ensure that the filesystem is not and will not be
272 	 * unmounted. The next statement is equivalent to ZFS_ENTER().
273 	 */
274 	rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
275 	if (zfsvfs->z_unmounted) {
276 		ZFS_EXIT(zfsvfs);
277 		rw_exit(&zfsvfs_lock);
278 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
279 		return (KMEM_CBRC_DONT_KNOW);
280 	}
281 	rw_exit(&zfsvfs_lock);
282 
283 	mutex_enter(&zfsvfs->z_znodes_lock);
284 	/*
285 	 * Recheck the vfs pointer in case the znode was removed just before
286 	 * acquiring the lock.
287 	 */
288 	if (zfsvfs != ozp->z_zfsvfs) {
289 		mutex_exit(&zfsvfs->z_znodes_lock);
290 		ZFS_EXIT(zfsvfs);
291 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
292 		return (KMEM_CBRC_DONT_KNOW);
293 	}
294 
295 	/*
296 	 * At this point we know that as long as we hold z_znodes_lock, the
297 	 * znode cannot be freed and fields within the znode can be safely
298 	 * accessed. Now, prevent a race with zfs_zget().
299 	 */
300 	if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
301 		mutex_exit(&zfsvfs->z_znodes_lock);
302 		ZFS_EXIT(zfsvfs);
303 		ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
304 		return (KMEM_CBRC_LATER);
305 	}
306 
307 	vp = ZTOV(ozp);
308 	if (mutex_tryenter(&vp->v_lock) == 0) {
309 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
310 		mutex_exit(&zfsvfs->z_znodes_lock);
311 		ZFS_EXIT(zfsvfs);
312 		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
313 		return (KMEM_CBRC_LATER);
314 	}
315 
316 	/* Only move znodes that are referenced _only_ by the DNLC. */
317 	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
318 		mutex_exit(&vp->v_lock);
319 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
320 		mutex_exit(&zfsvfs->z_znodes_lock);
321 		ZFS_EXIT(zfsvfs);
322 		ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
323 		return (KMEM_CBRC_LATER);
324 	}
325 
326 	/*
327 	 * The znode is known and in a valid state to move. We're holding the
328 	 * locks needed to execute the critical section.
329 	 */
330 	zfs_znode_move_impl(ozp, nzp);
331 	mutex_exit(&vp->v_lock);
332 	ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
333 
334 	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
335 	mutex_exit(&zfsvfs->z_znodes_lock);
336 	ZFS_EXIT(zfsvfs);
337 
338 	return (KMEM_CBRC_YES);
339 }
340 
341 void
342 zfs_znode_init(void)
343 {
344 	/*
345 	 * Initialize zcache
346 	 */
347 	rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
348 	ASSERT(znode_cache == NULL);
349 	znode_cache = kmem_cache_create("zfs_znode_cache",
350 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
351 	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
352 	kmem_cache_set_move(znode_cache, zfs_znode_move);
353 }
354 
355 void
356 zfs_znode_fini(void)
357 {
358 	/*
359 	 * Cleanup vfs & vnode ops
360 	 */
361 	zfs_remove_op_tables();
362 
363 	/*
364 	 * Cleanup zcache
365 	 */
366 	if (znode_cache)
367 		kmem_cache_destroy(znode_cache);
368 	znode_cache = NULL;
369 	rw_destroy(&zfsvfs_lock);
370 }
371 
372 struct vnodeops *zfs_dvnodeops;
373 struct vnodeops *zfs_fvnodeops;
374 struct vnodeops *zfs_symvnodeops;
375 struct vnodeops *zfs_xdvnodeops;
376 struct vnodeops *zfs_evnodeops;
377 struct vnodeops *zfs_sharevnodeops;
378 
379 void
380 zfs_remove_op_tables()
381 {
382 	/*
383 	 * Remove vfs ops
384 	 */
385 	ASSERT(zfsfstype);
386 	(void) vfs_freevfsops_by_type(zfsfstype);
387 	zfsfstype = 0;
388 
389 	/*
390 	 * Remove vnode ops
391 	 */
392 	if (zfs_dvnodeops)
393 		vn_freevnodeops(zfs_dvnodeops);
394 	if (zfs_fvnodeops)
395 		vn_freevnodeops(zfs_fvnodeops);
396 	if (zfs_symvnodeops)
397 		vn_freevnodeops(zfs_symvnodeops);
398 	if (zfs_xdvnodeops)
399 		vn_freevnodeops(zfs_xdvnodeops);
400 	if (zfs_evnodeops)
401 		vn_freevnodeops(zfs_evnodeops);
402 	if (zfs_sharevnodeops)
403 		vn_freevnodeops(zfs_sharevnodeops);
404 
405 	zfs_dvnodeops = NULL;
406 	zfs_fvnodeops = NULL;
407 	zfs_symvnodeops = NULL;
408 	zfs_xdvnodeops = NULL;
409 	zfs_evnodeops = NULL;
410 	zfs_sharevnodeops = NULL;
411 }
412 
413 extern const fs_operation_def_t zfs_dvnodeops_template[];
414 extern const fs_operation_def_t zfs_fvnodeops_template[];
415 extern const fs_operation_def_t zfs_xdvnodeops_template[];
416 extern const fs_operation_def_t zfs_symvnodeops_template[];
417 extern const fs_operation_def_t zfs_evnodeops_template[];
418 extern const fs_operation_def_t zfs_sharevnodeops_template[];
419 
420 int
421 zfs_create_op_tables()
422 {
423 	int error;
424 
425 	/*
426 	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
427 	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
428 	 * In this case we just return as the ops vectors are already set up.
429 	 */
430 	if (zfs_dvnodeops)
431 		return (0);
432 
433 	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
434 	    &zfs_dvnodeops);
435 	if (error)
436 		return (error);
437 
438 	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
439 	    &zfs_fvnodeops);
440 	if (error)
441 		return (error);
442 
443 	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
444 	    &zfs_symvnodeops);
445 	if (error)
446 		return (error);
447 
448 	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
449 	    &zfs_xdvnodeops);
450 	if (error)
451 		return (error);
452 
453 	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
454 	    &zfs_evnodeops);
455 	if (error)
456 		return (error);
457 
458 	error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
459 	    &zfs_sharevnodeops);
460 
461 	return (error);
462 }
463 
464 int
465 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
466 {
467 	zfs_acl_ids_t acl_ids;
468 	vattr_t vattr;
469 	znode_t *sharezp;
470 	vnode_t *vp;
471 	znode_t *zp;
472 	int error;
473 
474 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
475 	vattr.va_type = VDIR;
476 	vattr.va_mode = S_IFDIR|0555;
477 	vattr.va_uid = crgetuid(kcred);
478 	vattr.va_gid = crgetgid(kcred);
479 
480 	sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
481 	sharezp->z_unlinked = 0;
482 	sharezp->z_atime_dirty = 0;
483 	sharezp->z_zfsvfs = zfsvfs;
484 	sharezp->z_is_sa = zfsvfs->z_use_sa;
485 
486 	vp = ZTOV(sharezp);
487 	vn_reinit(vp);
488 	vp->v_type = VDIR;
489 
490 	VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
491 	    kcred, NULL, &acl_ids));
492 	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
493 	ASSERT3P(zp, ==, sharezp);
494 	ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
495 	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
496 	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
497 	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
498 	zfsvfs->z_shares_dir = sharezp->z_id;
499 
500 	zfs_acl_ids_free(&acl_ids);
501 	ZTOV(sharezp)->v_count = 0;
502 	sa_handle_destroy(sharezp->z_sa_hdl);
503 	kmem_cache_free(znode_cache, sharezp);
504 
505 	return (error);
506 }
507 
508 /*
509  * define a couple of values we need available
510  * for both 64 and 32 bit environments.
511  */
512 #ifndef NBITSMINOR64
513 #define	NBITSMINOR64	32
514 #endif
515 #ifndef MAXMAJ64
516 #define	MAXMAJ64	0xffffffffUL
517 #endif
518 #ifndef	MAXMIN64
519 #define	MAXMIN64	0xffffffffUL
520 #endif
521 
522 /*
523  * Create special expldev for ZFS private use.
524  * Can't use standard expldev since it doesn't do
525  * what we want.  The standard expldev() takes a
526  * dev32_t in LP64 and expands it to a long dev_t.
527  * We need an interface that takes a dev32_t in ILP32
528  * and expands it to a long dev_t.
529  */
530 static uint64_t
531 zfs_expldev(dev_t dev)
532 {
533 #ifndef _LP64
534 	major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
535 	return (((uint64_t)major << NBITSMINOR64) |
536 	    ((minor_t)dev & MAXMIN32));
537 #else
538 	return (dev);
539 #endif
540 }
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  */
549 dev_t
550 zfs_cmpldev(uint64_t dev)
551 {
552 #ifndef _LP64
553 	minor_t minor = (minor_t)dev & MAXMIN64;
554 	major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
555 
556 	if (major > MAXMAJ32 || minor > MAXMIN32)
557 		return (NODEV32);
558 
559 	return (((dev32_t)major << NBITSMINOR32) | minor);
560 #else
561 	return (dev);
562 #endif
563 }
564 
565 static void
566 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
567     dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
568 {
569 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
570 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
571 
572 	mutex_enter(&zp->z_lock);
573 
574 	ASSERT(zp->z_sa_hdl == NULL);
575 	ASSERT(zp->z_acl_cached == NULL);
576 	if (sa_hdl == NULL) {
577 		VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
578 		    SA_HDL_SHARED, &zp->z_sa_hdl));
579 	} else {
580 		zp->z_sa_hdl = sa_hdl;
581 		sa_set_userp(sa_hdl, zp);
582 	}
583 
584 	zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
585 
586 	/*
587 	 * Slap on VROOT if we are the root znode
588 	 */
589 	if (zp->z_id == zfsvfs->z_root)
590 		ZTOV(zp)->v_flag |= VROOT;
591 
592 	mutex_exit(&zp->z_lock);
593 	vn_exists(ZTOV(zp));
594 }
595 
596 void
597 zfs_znode_dmu_fini(znode_t *zp)
598 {
599 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
600 	    zp->z_unlinked ||
601 	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
602 
603 	sa_handle_destroy(zp->z_sa_hdl);
604 	zp->z_sa_hdl = NULL;
605 }
606 
607 /*
608  * Construct a new znode/vnode and intialize.
609  *
610  * This does not do a call to dmu_set_user() that is
611  * up to the caller to do, in case you don't want to
612  * return the znode
613  */
614 static znode_t *
615 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
616     dmu_object_type_t obj_type, sa_handle_t *hdl)
617 {
618 	znode_t	*zp;
619 	vnode_t *vp;
620 	uint64_t mode;
621 	uint64_t parent;
622 	uint64_t uid, gid;
623 	sa_bulk_attr_t bulk[9];
624 	int count = 0;
625 
626 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
627 
628 	ASSERT(zp->z_dirlocks == NULL);
629 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
630 
631 	/*
632 	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
633 	 * the zfs_znode_move() callback.
634 	 */
635 	zp->z_sa_hdl = NULL;
636 	zp->z_unlinked = 0;
637 	zp->z_atime_dirty = 0;
638 	zp->z_mapcnt = 0;
639 	zp->z_last_itx = 0;
640 	zp->z_id = db->db_object;
641 	zp->z_blksz = blksz;
642 	zp->z_seq = 0x7A4653;
643 	zp->z_sync_cnt = 0;
644 
645 	vp = ZTOV(zp);
646 	vn_reinit(vp);
647 
648 	zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
649 
650 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
651 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
652 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
653 	    &zp->z_size, 8);
654 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
655 	    &zp->z_links, 8);
656 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
657 	    &zp->z_pflags, 8);
658 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
659 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
660 	    &zp->z_atime, 16);
661 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
662 	    &uid, 8);
663 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
664 	    &gid, 8);
665 
666 	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
667 		if (hdl == NULL)
668 			sa_handle_destroy(zp->z_sa_hdl);
669 		kmem_cache_free(znode_cache, zp);
670 		return (NULL);
671 	}
672 
673 	zp->z_uid = zfs_fuid_map_id(zfsvfs, uid, CRED(), ZFS_OWNER);
674 	zp->z_gid = zfs_fuid_map_id(zfsvfs, gid, CRED(), ZFS_GROUP);
675 	zp->z_mode = mode;
676 	vp->v_vfsp = zfsvfs->z_parent->z_vfs;
677 
678 	vp->v_type = IFTOVT((mode_t)mode);
679 
680 	switch (vp->v_type) {
681 	case VDIR:
682 		if (zp->z_pflags & ZFS_XATTR) {
683 			vn_setops(vp, zfs_xdvnodeops);
684 			vp->v_flag |= V_XATTRDIR;
685 		} else {
686 			vn_setops(vp, zfs_dvnodeops);
687 		}
688 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
689 		break;
690 	case VBLK:
691 	case VCHR:
692 		{
693 			uint64_t rdev;
694 			VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
695 			    &rdev, sizeof (rdev)) == 0);
696 
697 			vp->v_rdev = zfs_cmpldev(rdev);
698 		}
699 		/*FALLTHROUGH*/
700 	case VFIFO:
701 	case VSOCK:
702 	case VDOOR:
703 		vn_setops(vp, zfs_fvnodeops);
704 		break;
705 	case VREG:
706 		vp->v_flag |= VMODSORT;
707 		if (parent == zfsvfs->z_shares_dir) {
708 			ASSERT(uid == 0 && gid == 0);
709 			vn_setops(vp, zfs_sharevnodeops);
710 		} else {
711 			vn_setops(vp, zfs_fvnodeops);
712 		}
713 		break;
714 	case VLNK:
715 		vn_setops(vp, zfs_symvnodeops);
716 		break;
717 	default:
718 		vn_setops(vp, zfs_evnodeops);
719 		break;
720 	}
721 
722 	mutex_enter(&zfsvfs->z_znodes_lock);
723 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
724 	membar_producer();
725 	/*
726 	 * Everything else must be valid before assigning z_zfsvfs makes the
727 	 * znode eligible for zfs_znode_move().
728 	 */
729 	zp->z_zfsvfs = zfsvfs;
730 	mutex_exit(&zfsvfs->z_znodes_lock);
731 
732 	VFS_HOLD(zfsvfs->z_vfs);
733 	return (zp);
734 }
735 
736 static uint64_t empty_xattr;
737 static uint64_t pad[4];
738 static zfs_acl_phys_t acl_phys;
739 /*
740  * Create a new DMU object to hold a zfs znode.
741  *
742  *	IN:	dzp	- parent directory for new znode
743  *		vap	- file attributes for new znode
744  *		tx	- dmu transaction id for zap operations
745  *		cr	- credentials of caller
746  *		flag	- flags:
747  *			  IS_ROOT_NODE	- new object will be root
748  *			  IS_XATTR	- new object is an attribute
749  *		bonuslen - length of bonus buffer
750  *		setaclp  - File/Dir initial ACL
751  *		fuidp	 - Tracks fuid allocation.
752  *
753  *	OUT:	zpp	- allocated znode
754  *
755  */
756 void
757 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
758     uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
759 {
760 	uint64_t	crtime[2], atime[2], mtime[2], ctime[2];
761 	uint64_t	mode, size, links, parent, pflags;
762 	uint64_t 	dzp_pflags = 0;
763 	uint64_t	rdev = 0;
764 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
765 	dmu_buf_t	*db;
766 	timestruc_t	now;
767 	uint64_t	gen, obj;
768 	int		err;
769 	int		bonuslen;
770 	sa_handle_t	*sa_hdl;
771 	dmu_object_type_t obj_type;
772 	sa_bulk_attr_t	sa_attrs[ZPL_END];
773 	int		cnt = 0;
774 	zfs_acl_locator_cb_t locate = { 0 };
775 
776 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
777 
778 	if (zfsvfs->z_replay) {
779 		obj = vap->va_nodeid;
780 		now = vap->va_ctime;		/* see zfs_replay_create() */
781 		gen = vap->va_nblocks;		/* ditto */
782 	} else {
783 		obj = 0;
784 		gethrestime(&now);
785 		gen = dmu_tx_get_txg(tx);
786 	}
787 
788 	obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
789 	bonuslen = (obj_type == DMU_OT_SA) ?
790 	    DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
791 
792 	/*
793 	 * Create a new DMU object.
794 	 */
795 	/*
796 	 * There's currently no mechanism for pre-reading the blocks that will
797 	 * be to needed allocate a new object, so we accept the small chance
798 	 * that there will be an i/o error and we will fail one of the
799 	 * assertions below.
800 	 */
801 	if (vap->va_type == VDIR) {
802 		if (zfsvfs->z_replay) {
803 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
804 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
805 			    obj_type, bonuslen, tx);
806 			ASSERT3U(err, ==, 0);
807 		} else {
808 			obj = zap_create_norm(zfsvfs->z_os,
809 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
810 			    obj_type, bonuslen, tx);
811 		}
812 	} else {
813 		if (zfsvfs->z_replay) {
814 			err = dmu_object_claim(zfsvfs->z_os, obj,
815 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
816 			    obj_type, bonuslen, tx);
817 			ASSERT3U(err, ==, 0);
818 		} else {
819 			obj = dmu_object_alloc(zfsvfs->z_os,
820 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
821 			    obj_type, bonuslen, tx);
822 		}
823 	}
824 
825 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
826 	VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
827 
828 	/*
829 	 * If this is the root, fix up the half-initialized parent pointer
830 	 * to reference the just-allocated physical data area.
831 	 */
832 	if (flag & IS_ROOT_NODE) {
833 		dzp->z_id = obj;
834 	} else {
835 		dzp_pflags = dzp->z_pflags;
836 	}
837 
838 	/*
839 	 * If parent is an xattr, so am I.
840 	 */
841 	if (dzp_pflags & ZFS_XATTR) {
842 		flag |= IS_XATTR;
843 	}
844 
845 	if (zfsvfs->z_use_fuids)
846 		pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
847 	else
848 		pflags = 0;
849 
850 	if (vap->va_type == VDIR) {
851 		size = 2;		/* contents ("." and "..") */
852 		links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
853 	} else {
854 		size = links = 0;
855 	}
856 
857 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
858 		rdev = zfs_expldev(vap->va_rdev);
859 	}
860 
861 	parent = dzp->z_id;
862 	mode = acl_ids->z_mode;
863 	if (flag & IS_XATTR)
864 		pflags |= ZFS_XATTR;
865 
866 	/*
867 	 * No execs denied will be deterimed when zfs_mode_compute() is called.
868 	 */
869 	pflags |= acl_ids->z_aclp->z_hints &
870 	    (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
871 	    ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
872 
873 	ZFS_TIME_ENCODE(&now, crtime);
874 	ZFS_TIME_ENCODE(&now, ctime);
875 
876 	if (vap->va_mask & AT_ATIME) {
877 		ZFS_TIME_ENCODE(&vap->va_atime, atime);
878 	} else {
879 		ZFS_TIME_ENCODE(&now, atime);
880 	}
881 
882 	if (vap->va_mask & AT_MTIME) {
883 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
884 	} else {
885 		ZFS_TIME_ENCODE(&now, mtime);
886 	}
887 
888 	/* Now add in all of the "SA" attributes */
889 	VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
890 	    &sa_hdl));
891 
892 	/*
893 	 * Setup the array of attributes to be replaced/set on the new file
894 	 *
895 	 * order for  DMU_OT_ZNODE is critical since it needs to be constructed
896 	 * in the old znode_phys_t format.  Don't change this ordering
897 	 */
898 
899 	if (obj_type == DMU_OT_ZNODE) {
900 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
901 		    NULL, &atime, 16);
902 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
903 		    NULL, &mtime, 16);
904 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
905 		    NULL, &ctime, 16);
906 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
907 		    NULL, &crtime, 16);
908 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
909 		    NULL, &gen, 8);
910 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
911 		    NULL, &mode, 8);
912 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
913 		    NULL, &size, 8);
914 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
915 		    NULL, &parent, 8);
916 	} else {
917 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
918 		    NULL, &mode, 8);
919 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
920 		    NULL, &size, 8);
921 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
922 		    NULL, &gen, 8);
923 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
924 		    &acl_ids->z_fuid, 8);
925 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
926 		    &acl_ids->z_fgid, 8);
927 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
928 		    NULL, &parent, 8);
929 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
930 		    NULL, &pflags, 8);
931 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
932 		    NULL, &atime, 16);
933 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
934 		    NULL, &mtime, 16);
935 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
936 		    NULL, &ctime, 16);
937 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
938 		    NULL, &crtime, 16);
939 	}
940 
941 	SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
942 
943 	if (obj_type == DMU_OT_ZNODE) {
944 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
945 		    &empty_xattr, 8);
946 	}
947 	if (obj_type == DMU_OT_ZNODE ||
948 	    (vap->va_type == VBLK || vap->va_type == VCHR)) {
949 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
950 		    NULL, &rdev, 8);
951 
952 	}
953 	if (obj_type == DMU_OT_ZNODE) {
954 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
955 		    NULL, &pflags, 8);
956 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
957 		    &acl_ids->z_fuid, 8);
958 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
959 		    &acl_ids->z_fgid, 8);
960 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
961 		    sizeof (uint64_t) * 4);
962 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
963 		    &acl_phys, sizeof (zfs_acl_phys_t));
964 	} else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
965 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
966 		    &acl_ids->z_aclp->z_acl_count, 8);
967 		locate.cb_aclp = acl_ids->z_aclp;
968 		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
969 		    zfs_acl_data_locator, &locate,
970 		    acl_ids->z_aclp->z_acl_bytes);
971 		mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
972 		    acl_ids->z_fuid, acl_ids->z_fgid);
973 	}
974 
975 	VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
976 
977 	if (!(flag & IS_ROOT_NODE)) {
978 		*zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
979 		ASSERT(*zpp != NULL);
980 	} else {
981 		/*
982 		 * If we are creating the root node, the "parent" we
983 		 * passed in is the znode for the root.
984 		 */
985 		*zpp = dzp;
986 
987 		(*zpp)->z_sa_hdl = sa_hdl;
988 	}
989 
990 	(*zpp)->z_pflags = pflags;
991 	(*zpp)->z_mode = mode;
992 
993 	if (vap->va_mask & AT_XVATTR)
994 		zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
995 
996 	if (obj_type == DMU_OT_ZNODE ||
997 	    acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
998 		err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
999 		ASSERT3P(err, ==, 0);
1000 	}
1001 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1002 }
1003 
1004 /*
1005  * zfs_xvattr_set only updates the in-core attributes
1006  * it is assumed the caller will be doing an sa_bulk_update
1007  * to push the changes out
1008  */
1009 void
1010 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1011 {
1012 	xoptattr_t *xoap;
1013 
1014 	xoap = xva_getxoptattr(xvap);
1015 	ASSERT(xoap);
1016 
1017 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1018 		uint64_t times[2];
1019 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1020 		(void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1021 		    &times, sizeof (times), tx);
1022 		XVA_SET_RTN(xvap, XAT_CREATETIME);
1023 	}
1024 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1025 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1026 		    zp->z_pflags, tx);
1027 		XVA_SET_RTN(xvap, XAT_READONLY);
1028 	}
1029 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1030 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1031 		    zp->z_pflags, tx);
1032 		XVA_SET_RTN(xvap, XAT_HIDDEN);
1033 	}
1034 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1035 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1036 		    zp->z_pflags, tx);
1037 		XVA_SET_RTN(xvap, XAT_SYSTEM);
1038 	}
1039 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1040 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1041 		    zp->z_pflags, tx);
1042 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
1043 	}
1044 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1045 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1046 		    zp->z_pflags, tx);
1047 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1048 	}
1049 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1050 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1051 		    zp->z_pflags, tx);
1052 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
1053 	}
1054 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1055 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1056 		    zp->z_pflags, tx);
1057 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
1058 	}
1059 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1060 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1061 		    zp->z_pflags, tx);
1062 		XVA_SET_RTN(xvap, XAT_NODUMP);
1063 	}
1064 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1065 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1066 		    zp->z_pflags, tx);
1067 		XVA_SET_RTN(xvap, XAT_OPAQUE);
1068 	}
1069 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1070 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1071 		    xoap->xoa_av_quarantined, zp->z_pflags, tx);
1072 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1073 	}
1074 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1075 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1076 		    zp->z_pflags, tx);
1077 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1078 	}
1079 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1080 		zfs_sa_set_scanstamp(zp, xvap, tx);
1081 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1082 	}
1083 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1084 		ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1085 		    zp->z_pflags, tx);
1086 		XVA_SET_RTN(xvap, XAT_REPARSE);
1087 	}
1088 }
1089 
1090 int
1091 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1092 {
1093 	dmu_object_info_t doi;
1094 	dmu_buf_t	*db;
1095 	znode_t		*zp;
1096 	int err;
1097 	sa_handle_t	*hdl;
1098 
1099 	*zpp = NULL;
1100 
1101 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1102 
1103 	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1104 	if (err) {
1105 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1106 		return (err);
1107 	}
1108 
1109 	dmu_object_info_from_db(db, &doi);
1110 	if (doi.doi_bonus_type != DMU_OT_SA &&
1111 	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1112 	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1113 	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1114 		sa_buf_rele(db, NULL);
1115 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1116 		return (EINVAL);
1117 	}
1118 
1119 	hdl = dmu_buf_get_user(db);
1120 	if (hdl != NULL) {
1121 		zp  = sa_get_userdata(hdl);
1122 
1123 
1124 		/*
1125 		 * Since "SA" does immediate eviction we
1126 		 * should never find a sa handle that doesn't
1127 		 * know about the znode.
1128 		 */
1129 
1130 		ASSERT3P(zp, !=, NULL);
1131 
1132 		mutex_enter(&zp->z_lock);
1133 		ASSERT3U(zp->z_id, ==, obj_num);
1134 		if (zp->z_unlinked) {
1135 			err = ENOENT;
1136 		} else {
1137 			VN_HOLD(ZTOV(zp));
1138 			*zpp = zp;
1139 			err = 0;
1140 		}
1141 		sa_buf_rele(db, NULL);
1142 		mutex_exit(&zp->z_lock);
1143 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1144 		return (err);
1145 	}
1146 
1147 	/*
1148 	 * Not found create new znode/vnode
1149 	 * but only if file exists.
1150 	 *
1151 	 * There is a small window where zfs_vget() could
1152 	 * find this object while a file create is still in
1153 	 * progress.  This is checked for in zfs_znode_alloc()
1154 	 *
1155 	 * if zfs_znode_alloc() fails it will drop the hold on the
1156 	 * bonus buffer.
1157 	 */
1158 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1159 	    doi.doi_bonus_type, NULL);
1160 	if (zp == NULL) {
1161 		err = ENOENT;
1162 	} else {
1163 		*zpp = zp;
1164 	}
1165 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1166 	return (err);
1167 }
1168 
1169 int
1170 zfs_rezget(znode_t *zp)
1171 {
1172 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1173 	dmu_object_info_t doi;
1174 	dmu_buf_t *db;
1175 	uint64_t obj_num = zp->z_id;
1176 	uint64_t mode;
1177 	uint64_t uid, gid;
1178 	sa_bulk_attr_t bulk[8];
1179 	int err;
1180 	int count = 0;
1181 	uint64_t gen;
1182 
1183 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1184 
1185 	mutex_enter(&zp->z_acl_lock);
1186 	if (zp->z_acl_cached) {
1187 		zfs_acl_free(zp->z_acl_cached);
1188 		zp->z_acl_cached = NULL;
1189 	}
1190 
1191 	mutex_exit(&zp->z_acl_lock);
1192 	ASSERT(zp->z_sa_hdl == NULL);
1193 	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1194 	if (err) {
1195 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1196 		return (err);
1197 	}
1198 
1199 	dmu_object_info_from_db(db, &doi);
1200 	if (doi.doi_bonus_type != DMU_OT_SA &&
1201 	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1202 	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1203 	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1204 		sa_buf_rele(db, NULL);
1205 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1206 		return (EINVAL);
1207 	}
1208 
1209 	zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1210 
1211 	/* reload cached values */
1212 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1213 	    &gen, sizeof (gen));
1214 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1215 	    &zp->z_size, sizeof (zp->z_size));
1216 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1217 	    &zp->z_links, sizeof (zp->z_links));
1218 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1219 	    &zp->z_pflags, sizeof (zp->z_pflags));
1220 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1221 	    &zp->z_atime, sizeof (zp->z_atime));
1222 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1223 	    &uid, sizeof (uid));
1224 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1225 	    &gid, sizeof (gid));
1226 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1227 	    &mode, sizeof (mode));
1228 
1229 	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1230 		zfs_znode_dmu_fini(zp);
1231 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1232 		return (EIO);
1233 	}
1234 
1235 	zp->z_mode = mode;
1236 
1237 	if (gen != zp->z_gen) {
1238 		zfs_znode_dmu_fini(zp);
1239 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1240 		return (EIO);
1241 	}
1242 
1243 	zp->z_uid = zfs_fuid_map_id(zfsvfs, uid, CRED(), ZFS_OWNER);
1244 	zp->z_gid = zfs_fuid_map_id(zfsvfs, gid, CRED(), ZFS_GROUP);
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 	rootzp->z_unlinked = 0;
1811 	rootzp->z_atime_dirty = 0;
1812 	rootzp->z_is_sa = USE_SA(version, os);
1813 
1814 	vp = ZTOV(rootzp);
1815 	vn_reinit(vp);
1816 	vp->v_type = VDIR;
1817 
1818 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1819 
1820 	zfsvfs.z_os = os;
1821 	zfsvfs.z_parent = &zfsvfs;
1822 	zfsvfs.z_version = version;
1823 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1824 	zfsvfs.z_use_sa = USE_SA(version, os);
1825 	zfsvfs.z_norm = norm;
1826 
1827 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1828 	    &zfsvfs.z_attr_table);
1829 
1830 	ASSERT(error == 0);
1831 
1832 	/*
1833 	 * Fold case on file systems that are always or sometimes case
1834 	 * insensitive.
1835 	 */
1836 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1837 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1838 
1839 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1840 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1841 	    offsetof(znode_t, z_link_node));
1842 
1843 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1844 		mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1845 
1846 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1847 	rootzp->z_zfsvfs = &zfsvfs;
1848 	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1849 	    cr, NULL, &acl_ids));
1850 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1851 	ASSERT3P(zp, ==, rootzp);
1852 	ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
1853 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1854 	ASSERT(error == 0);
1855 	zfs_acl_ids_free(&acl_ids);
1856 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1857 
1858 	ZTOV(rootzp)->v_count = 0;
1859 	sa_handle_destroy(rootzp->z_sa_hdl);
1860 	kmem_cache_free(znode_cache, rootzp);
1861 
1862 	/*
1863 	 * Create shares directory
1864 	 */
1865 
1866 	error = zfs_create_share_dir(&zfsvfs, tx);
1867 
1868 	ASSERT(error == 0);
1869 
1870 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1871 		mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1872 }
1873 
1874 #endif /* _KERNEL */
1875 
1876 /*
1877  * Given an object number, return its parent object number and whether
1878  * or not the object is an extended attribute directory.
1879  */
1880 static int
1881 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir,
1882     sa_attr_type_t *sa_table)
1883 {
1884 	dmu_buf_t *db;
1885 	dmu_object_info_t doi;
1886 	int error;
1887 	uint64_t parent;
1888 	uint64_t pflags;
1889 	uint64_t mode;
1890 	sa_bulk_attr_t bulk[3];
1891 	sa_handle_t *hdl;
1892 	int count = 0;
1893 
1894 	if ((error = sa_buf_hold(osp, obj, FTAG, &db)) != 0)
1895 		return (error);
1896 
1897 	dmu_object_info_from_db(db, &doi);
1898 	if ((doi.doi_bonus_type != DMU_OT_SA &&
1899 	    doi.doi_bonus_type != DMU_OT_ZNODE) ||
1900 	    doi.doi_bonus_type == DMU_OT_ZNODE &&
1901 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1902 		sa_buf_rele(db, FTAG);
1903 		return (EINVAL);
1904 	}
1905 
1906 	if ((error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE,
1907 	    &hdl)) != 0) {
1908 		sa_buf_rele(db, FTAG);
1909 		return (error);
1910 	}
1911 
1912 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT],
1913 	    NULL, &parent, 8);
1914 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1915 	    &pflags, 8);
1916 	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1917 	    &mode, 8);
1918 
1919 	if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0) {
1920 		sa_buf_rele(db, FTAG);
1921 		sa_handle_destroy(hdl);
1922 		return (error);
1923 	}
1924 	*pobjp = parent;
1925 	*is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1926 	sa_handle_destroy(hdl);
1927 	sa_buf_rele(db, FTAG);
1928 
1929 	return (0);
1930 }
1931 
1932 int
1933 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1934 {
1935 	char *path = buf + len - 1;
1936 	sa_attr_type_t *sa_table;
1937 	int error;
1938 	uint64_t sa_obj = 0;
1939 
1940 	*path = '\0';
1941 
1942 	error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1943 
1944 	if (error != 0 && error != ENOENT)
1945 		return (error);
1946 
1947 	if ((error = sa_setup(osp, sa_obj, zfs_attr_table,
1948 	    ZPL_END, &sa_table)) != 0)
1949 		return (error);
1950 
1951 	for (;;) {
1952 		uint64_t pobj;
1953 		char component[MAXNAMELEN + 2];
1954 		size_t complen;
1955 		int is_xattrdir;
1956 
1957 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1958 		    &is_xattrdir, sa_table)) != 0)
1959 			break;
1960 
1961 		if (pobj == obj) {
1962 			if (path[0] != '/')
1963 				*--path = '/';
1964 			break;
1965 		}
1966 
1967 		component[0] = '/';
1968 		if (is_xattrdir) {
1969 			(void) sprintf(component + 1, "<xattrdir>");
1970 		} else {
1971 			error = zap_value_search(osp, pobj, obj,
1972 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
1973 			if (error != 0)
1974 				break;
1975 		}
1976 
1977 		complen = strlen(component);
1978 		path -= complen;
1979 		ASSERT(path >= buf);
1980 		bcopy(component, path, complen);
1981 		obj = pobj;
1982 	}
1983 
1984 	if (error == 0)
1985 		(void) memmove(buf, path, buf + len - path);
1986 
1987 	return (error);
1988 }
1989