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