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