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