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