xref: /titanic_41/usr/src/uts/common/fs/zfs/zfs_znode.c (revision d24234c24aeaca4ca56ee3ac2794507968f274c4)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /* Portions Copyright 2007 Jeremy Teo */
27 
28 #ifdef _KERNEL
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/mntent.h>
36 #include <sys/mkdev.h>
37 #include <sys/u8_textprep.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/vfs.h>
40 #include <sys/vfs_opreg.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/kmem.h>
44 #include <sys/errno.h>
45 #include <sys/unistd.h>
46 #include <sys/mode.h>
47 #include <sys/atomic.h>
48 #include <vm/pvn.h>
49 #include "fs/fs_subr.h"
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_rlock.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/kidmap.h>
57 #endif /* _KERNEL */
58 
59 #include <sys/dmu.h>
60 #include <sys/refcount.h>
61 #include <sys/stat.h>
62 #include <sys/zap.h>
63 #include <sys/zfs_znode.h>
64 
65 #include "zfs_prop.h"
66 
67 /*
68  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
69  * turned on when DEBUG is also defined.
70  */
71 #ifdef	DEBUG
72 #define	ZNODE_STATS
73 #endif	/* DEBUG */
74 
75 #ifdef	ZNODE_STATS
76 #define	ZNODE_STAT_ADD(stat)			((stat)++)
77 #else
78 #define	ZNODE_STAT_ADD(stat)			/* nothing */
79 #endif	/* ZNODE_STATS */
80 
81 #define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
82 #define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
83 
84 /*
85  * Functions needed for userland (ie: libzpool) are not put under
86  * #ifdef_KERNEL; the rest of the functions have dependencies
87  * (such as VFS logic) that will not compile easily in userland.
88  */
89 #ifdef _KERNEL
90 /*
91  * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
92  * be freed before it can be safely accessed.
93  */
94 krwlock_t zfsvfs_lock;
95 
96 static kmem_cache_t *znode_cache = NULL;
97 
98 /*ARGSUSED*/
99 static void
100 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
101 {
102 	/*
103 	 * We should never drop all dbuf refs without first clearing
104 	 * the eviction callback.
105 	 */
106 	panic("evicting znode %p\n", user_ptr);
107 }
108 
109 /*ARGSUSED*/
110 static int
111 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
112 {
113 	znode_t *zp = buf;
114 
115 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
116 
117 	zp->z_vnode = vn_alloc(kmflags);
118 	if (zp->z_vnode == NULL) {
119 		return (-1);
120 	}
121 	ZTOV(zp)->v_data = zp;
122 
123 	list_link_init(&zp->z_link_node);
124 
125 	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
126 	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
127 	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
128 	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
129 
130 	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
131 	avl_create(&zp->z_range_avl, zfs_range_compare,
132 	    sizeof (rl_t), offsetof(rl_t, r_node));
133 
134 	zp->z_dbuf = NULL;
135 	zp->z_dirlocks = NULL;
136 	zp->z_acl_cached = NULL;
137 	return (0);
138 }
139 
140 /*ARGSUSED*/
141 static void
142 zfs_znode_cache_destructor(void *buf, void *arg)
143 {
144 	znode_t *zp = buf;
145 
146 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
147 	ASSERT(ZTOV(zp)->v_data == zp);
148 	vn_free(ZTOV(zp));
149 	ASSERT(!list_link_active(&zp->z_link_node));
150 	mutex_destroy(&zp->z_lock);
151 	rw_destroy(&zp->z_parent_lock);
152 	rw_destroy(&zp->z_name_lock);
153 	mutex_destroy(&zp->z_acl_lock);
154 	avl_destroy(&zp->z_range_avl);
155 	mutex_destroy(&zp->z_range_lock);
156 
157 	ASSERT(zp->z_dbuf == NULL);
158 	ASSERT(zp->z_dirlocks == NULL);
159 	ASSERT(zp->z_acl_cached == NULL);
160 }
161 
162 #ifdef	ZNODE_STATS
163 static struct {
164 	uint64_t zms_zfsvfs_invalid;
165 	uint64_t zms_zfsvfs_recheck1;
166 	uint64_t zms_zfsvfs_unmounted;
167 	uint64_t zms_zfsvfs_recheck2;
168 	uint64_t zms_obj_held;
169 	uint64_t zms_vnode_locked;
170 	uint64_t zms_not_only_dnlc;
171 } znode_move_stats;
172 #endif	/* ZNODE_STATS */
173 
174 static void
175 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
176 {
177 	vnode_t *vp;
178 
179 	/* Copy fields. */
180 	nzp->z_zfsvfs = ozp->z_zfsvfs;
181 
182 	/* Swap vnodes. */
183 	vp = nzp->z_vnode;
184 	nzp->z_vnode = ozp->z_vnode;
185 	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
186 	ZTOV(ozp)->v_data = ozp;
187 	ZTOV(nzp)->v_data = nzp;
188 
189 	nzp->z_id = ozp->z_id;
190 	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
191 	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
192 	nzp->z_unlinked = ozp->z_unlinked;
193 	nzp->z_atime_dirty = ozp->z_atime_dirty;
194 	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
195 	nzp->z_blksz = ozp->z_blksz;
196 	nzp->z_seq = ozp->z_seq;
197 	nzp->z_mapcnt = ozp->z_mapcnt;
198 	nzp->z_last_itx = ozp->z_last_itx;
199 	nzp->z_gen = ozp->z_gen;
200 	nzp->z_sync_cnt = ozp->z_sync_cnt;
201 	nzp->z_phys = ozp->z_phys;
202 	nzp->z_dbuf = ozp->z_dbuf;
203 
204 	/*
205 	 * Since this is just an idle znode and kmem is already dealing with
206 	 * memory pressure, release any cached ACL.
207 	 */
208 	if (ozp->z_acl_cached) {
209 		zfs_acl_free(ozp->z_acl_cached);
210 		ozp->z_acl_cached = NULL;
211 	}
212 
213 	/* Update back pointers. */
214 	(void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys,
215 	    znode_evict_error);
216 
217 	/*
218 	 * Invalidate the original znode by clearing fields that provide a
219 	 * pointer back to the znode. Set the low bit of the vfs pointer to
220 	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
221 	 * subsequent callback.
222 	 */
223 	ozp->z_dbuf = NULL;
224 	POINTER_INVALIDATE(&ozp->z_zfsvfs);
225 }
226 
227 /*ARGSUSED*/
228 static kmem_cbrc_t
229 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
230 {
231 	znode_t *ozp = buf, *nzp = newbuf;
232 	zfsvfs_t *zfsvfs;
233 	vnode_t *vp;
234 
235 	/*
236 	 * The znode is on the file system's list of known znodes if the vfs
237 	 * pointer is valid. We set the low bit of the vfs pointer when freeing
238 	 * the znode to invalidate it, and the memory patterns written by kmem
239 	 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
240 	 * created znode sets the vfs pointer last of all to indicate that the
241 	 * znode is known and in a valid state to be moved by this function.
242 	 */
243 	zfsvfs = ozp->z_zfsvfs;
244 	if (!POINTER_IS_VALID(zfsvfs)) {
245 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
246 		return (KMEM_CBRC_DONT_KNOW);
247 	}
248 
249 	/*
250 	 * Close a small window in which it's possible that the filesystem could
251 	 * be unmounted and freed, and zfsvfs, though valid in the previous
252 	 * statement, could point to unrelated memory by the time we try to
253 	 * prevent the filesystem from being unmounted.
254 	 */
255 	rw_enter(&zfsvfs_lock, RW_WRITER);
256 	if (zfsvfs != ozp->z_zfsvfs) {
257 		rw_exit(&zfsvfs_lock);
258 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
259 		return (KMEM_CBRC_DONT_KNOW);
260 	}
261 
262 	/*
263 	 * If the znode is still valid, then so is the file system. We know that
264 	 * no valid file system can be freed while we hold zfsvfs_lock, so we
265 	 * can safely ensure that the filesystem is not and will not be
266 	 * unmounted. The next statement is equivalent to ZFS_ENTER().
267 	 */
268 	rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
269 	if (zfsvfs->z_unmounted) {
270 		ZFS_EXIT(zfsvfs);
271 		rw_exit(&zfsvfs_lock);
272 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
273 		return (KMEM_CBRC_DONT_KNOW);
274 	}
275 	rw_exit(&zfsvfs_lock);
276 
277 	mutex_enter(&zfsvfs->z_znodes_lock);
278 	/*
279 	 * Recheck the vfs pointer in case the znode was removed just before
280 	 * acquiring the lock.
281 	 */
282 	if (zfsvfs != ozp->z_zfsvfs) {
283 		mutex_exit(&zfsvfs->z_znodes_lock);
284 		ZFS_EXIT(zfsvfs);
285 		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
286 		return (KMEM_CBRC_DONT_KNOW);
287 	}
288 
289 	/*
290 	 * At this point we know that as long as we hold z_znodes_lock, the
291 	 * znode cannot be freed and fields within the znode can be safely
292 	 * accessed. Now, prevent a race with zfs_zget().
293 	 */
294 	if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
295 		mutex_exit(&zfsvfs->z_znodes_lock);
296 		ZFS_EXIT(zfsvfs);
297 		ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
298 		return (KMEM_CBRC_LATER);
299 	}
300 
301 	vp = ZTOV(ozp);
302 	if (mutex_tryenter(&vp->v_lock) == 0) {
303 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
304 		mutex_exit(&zfsvfs->z_znodes_lock);
305 		ZFS_EXIT(zfsvfs);
306 		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
307 		return (KMEM_CBRC_LATER);
308 	}
309 
310 	/* Only move znodes that are referenced _only_ by the DNLC. */
311 	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
312 		mutex_exit(&vp->v_lock);
313 		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
314 		mutex_exit(&zfsvfs->z_znodes_lock);
315 		ZFS_EXIT(zfsvfs);
316 		ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
317 		return (KMEM_CBRC_LATER);
318 	}
319 
320 	/*
321 	 * The znode is known and in a valid state to move. We're holding the
322 	 * locks needed to execute the critical section.
323 	 */
324 	zfs_znode_move_impl(ozp, nzp);
325 	mutex_exit(&vp->v_lock);
326 	ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
327 
328 	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
329 	mutex_exit(&zfsvfs->z_znodes_lock);
330 	ZFS_EXIT(zfsvfs);
331 
332 	return (KMEM_CBRC_YES);
333 }
334 
335 void
336 zfs_znode_init(void)
337 {
338 	/*
339 	 * Initialize zcache
340 	 */
341 	rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
342 	ASSERT(znode_cache == NULL);
343 	znode_cache = kmem_cache_create("zfs_znode_cache",
344 	    sizeof (znode_t), 0, zfs_znode_cache_constructor,
345 	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
346 	kmem_cache_set_move(znode_cache, zfs_znode_move);
347 }
348 
349 void
350 zfs_znode_fini(void)
351 {
352 	/*
353 	 * Cleanup vfs & vnode ops
354 	 */
355 	zfs_remove_op_tables();
356 
357 	/*
358 	 * Cleanup zcache
359 	 */
360 	if (znode_cache)
361 		kmem_cache_destroy(znode_cache);
362 	znode_cache = NULL;
363 	rw_destroy(&zfsvfs_lock);
364 }
365 
366 struct vnodeops *zfs_dvnodeops;
367 struct vnodeops *zfs_fvnodeops;
368 struct vnodeops *zfs_symvnodeops;
369 struct vnodeops *zfs_xdvnodeops;
370 struct vnodeops *zfs_evnodeops;
371 struct vnodeops *zfs_sharevnodeops;
372 
373 void
374 zfs_remove_op_tables()
375 {
376 	/*
377 	 * Remove vfs ops
378 	 */
379 	ASSERT(zfsfstype);
380 	(void) vfs_freevfsops_by_type(zfsfstype);
381 	zfsfstype = 0;
382 
383 	/*
384 	 * Remove vnode ops
385 	 */
386 	if (zfs_dvnodeops)
387 		vn_freevnodeops(zfs_dvnodeops);
388 	if (zfs_fvnodeops)
389 		vn_freevnodeops(zfs_fvnodeops);
390 	if (zfs_symvnodeops)
391 		vn_freevnodeops(zfs_symvnodeops);
392 	if (zfs_xdvnodeops)
393 		vn_freevnodeops(zfs_xdvnodeops);
394 	if (zfs_evnodeops)
395 		vn_freevnodeops(zfs_evnodeops);
396 	if (zfs_sharevnodeops)
397 		vn_freevnodeops(zfs_sharevnodeops);
398 
399 	zfs_dvnodeops = NULL;
400 	zfs_fvnodeops = NULL;
401 	zfs_symvnodeops = NULL;
402 	zfs_xdvnodeops = NULL;
403 	zfs_evnodeops = NULL;
404 	zfs_sharevnodeops = NULL;
405 }
406 
407 extern const fs_operation_def_t zfs_dvnodeops_template[];
408 extern const fs_operation_def_t zfs_fvnodeops_template[];
409 extern const fs_operation_def_t zfs_xdvnodeops_template[];
410 extern const fs_operation_def_t zfs_symvnodeops_template[];
411 extern const fs_operation_def_t zfs_evnodeops_template[];
412 extern const fs_operation_def_t zfs_sharevnodeops_template[];
413 
414 int
415 zfs_create_op_tables()
416 {
417 	int error;
418 
419 	/*
420 	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
421 	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
422 	 * In this case we just return as the ops vectors are already set up.
423 	 */
424 	if (zfs_dvnodeops)
425 		return (0);
426 
427 	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
428 	    &zfs_dvnodeops);
429 	if (error)
430 		return (error);
431 
432 	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
433 	    &zfs_fvnodeops);
434 	if (error)
435 		return (error);
436 
437 	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
438 	    &zfs_symvnodeops);
439 	if (error)
440 		return (error);
441 
442 	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
443 	    &zfs_xdvnodeops);
444 	if (error)
445 		return (error);
446 
447 	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
448 	    &zfs_evnodeops);
449 	if (error)
450 		return (error);
451 
452 	error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
453 	    &zfs_sharevnodeops);
454 
455 	return (error);
456 }
457 
458 int
459 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
460 {
461 	zfs_acl_ids_t acl_ids;
462 	vattr_t vattr;
463 	znode_t *sharezp;
464 	vnode_t *vp;
465 	znode_t *zp;
466 	int error;
467 
468 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
469 	vattr.va_type = VDIR;
470 	vattr.va_mode = S_IFDIR|0555;
471 	vattr.va_uid = crgetuid(kcred);
472 	vattr.va_gid = crgetgid(kcred);
473 
474 	sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
475 	sharezp->z_unlinked = 0;
476 	sharezp->z_atime_dirty = 0;
477 	sharezp->z_zfsvfs = zfsvfs;
478 
479 	vp = ZTOV(sharezp);
480 	vn_reinit(vp);
481 	vp->v_type = VDIR;
482 
483 	VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
484 	    kcred, NULL, &acl_ids));
485 	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE,
486 	    &zp, 0, &acl_ids);
487 	ASSERT3P(zp, ==, sharezp);
488 	ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
489 	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
490 	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
491 	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
492 	zfsvfs->z_shares_dir = sharezp->z_id;
493 
494 	zfs_acl_ids_free(&acl_ids);
495 	ZTOV(sharezp)->v_count = 0;
496 	dmu_buf_rele(sharezp->z_dbuf, NULL);
497 	sharezp->z_dbuf = NULL;
498 	kmem_cache_free(znode_cache, sharezp);
499 
500 	return (error);
501 }
502 
503 /*
504  * define a couple of values we need available
505  * for both 64 and 32 bit environments.
506  */
507 #ifndef NBITSMINOR64
508 #define	NBITSMINOR64	32
509 #endif
510 #ifndef MAXMAJ64
511 #define	MAXMAJ64	0xffffffffUL
512 #endif
513 #ifndef	MAXMIN64
514 #define	MAXMIN64	0xffffffffUL
515 #endif
516 
517 /*
518  * Create special expldev for ZFS private use.
519  * Can't use standard expldev since it doesn't do
520  * what we want.  The standard expldev() takes a
521  * dev32_t in LP64 and expands it to a long dev_t.
522  * We need an interface that takes a dev32_t in ILP32
523  * and expands it to a long dev_t.
524  */
525 static uint64_t
526 zfs_expldev(dev_t dev)
527 {
528 #ifndef _LP64
529 	major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
530 	return (((uint64_t)major << NBITSMINOR64) |
531 	    ((minor_t)dev & MAXMIN32));
532 #else
533 	return (dev);
534 #endif
535 }
536 
537 /*
538  * Special cmpldev for ZFS private use.
539  * Can't use standard cmpldev since it takes
540  * a long dev_t and compresses it to dev32_t in
541  * LP64.  We need to do a compaction of a long dev_t
542  * to a dev32_t in ILP32.
543  */
544 dev_t
545 zfs_cmpldev(uint64_t dev)
546 {
547 #ifndef _LP64
548 	minor_t minor = (minor_t)dev & MAXMIN64;
549 	major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
550 
551 	if (major > MAXMAJ32 || minor > MAXMIN32)
552 		return (NODEV32);
553 
554 	return (((dev32_t)major << NBITSMINOR32) | minor);
555 #else
556 	return (dev);
557 #endif
558 }
559 
560 static void
561 zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
562 {
563 	znode_t		*nzp;
564 
565 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
566 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
567 
568 	mutex_enter(&zp->z_lock);
569 
570 	ASSERT(zp->z_dbuf == NULL);
571 	ASSERT(zp->z_acl_cached == NULL);
572 	zp->z_dbuf = db;
573 	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error);
574 
575 	/*
576 	 * there should be no
577 	 * concurrent zgets on this object.
578 	 */
579 	if (nzp != NULL)
580 		panic("existing znode %p for dbuf %p", (void *)nzp, (void *)db);
581 
582 	/*
583 	 * Slap on VROOT if we are the root znode
584 	 */
585 	if (zp->z_id == zfsvfs->z_root)
586 		ZTOV(zp)->v_flag |= VROOT;
587 
588 	mutex_exit(&zp->z_lock);
589 	vn_exists(ZTOV(zp));
590 }
591 
592 void
593 zfs_znode_dmu_fini(znode_t *zp)
594 {
595 	dmu_buf_t *db = zp->z_dbuf;
596 	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
597 	    zp->z_unlinked ||
598 	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
599 	ASSERT(zp->z_dbuf != NULL);
600 	zp->z_dbuf = NULL;
601 	VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL));
602 	dmu_buf_rele(db, NULL);
603 }
604 
605 /*
606  * Construct a new znode/vnode and intialize.
607  *
608  * This does not do a call to dmu_set_user() that is
609  * up to the caller to do, in case you don't want to
610  * return the znode
611  */
612 static znode_t *
613 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
614 {
615 	znode_t	*zp;
616 	vnode_t *vp;
617 
618 	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
619 
620 	ASSERT(zp->z_dirlocks == NULL);
621 	ASSERT(zp->z_dbuf == NULL);
622 	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
623 
624 	/*
625 	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
626 	 * the zfs_znode_move() callback.
627 	 */
628 	zp->z_phys = NULL;
629 	zp->z_unlinked = 0;
630 	zp->z_atime_dirty = 0;
631 	zp->z_mapcnt = 0;
632 	zp->z_last_itx = 0;
633 	zp->z_id = db->db_object;
634 	zp->z_blksz = blksz;
635 	zp->z_seq = 0x7A4653;
636 	zp->z_sync_cnt = 0;
637 
638 	vp = ZTOV(zp);
639 	vn_reinit(vp);
640 
641 	zfs_znode_dmu_init(zfsvfs, zp, db);
642 
643 	zp->z_gen = zp->z_phys->zp_gen;
644 
645 	vp->v_vfsp = zfsvfs->z_parent->z_vfs;
646 	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
647 
648 	switch (vp->v_type) {
649 	case VDIR:
650 		if (zp->z_phys->zp_flags & ZFS_XATTR) {
651 			vn_setops(vp, zfs_xdvnodeops);
652 			vp->v_flag |= V_XATTRDIR;
653 		} else {
654 			vn_setops(vp, zfs_dvnodeops);
655 		}
656 		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
657 		break;
658 	case VBLK:
659 	case VCHR:
660 		vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
661 		/*FALLTHROUGH*/
662 	case VFIFO:
663 	case VSOCK:
664 	case VDOOR:
665 		vn_setops(vp, zfs_fvnodeops);
666 		break;
667 	case VREG:
668 		vp->v_flag |= VMODSORT;
669 		if (zp->z_phys->zp_parent == zfsvfs->z_shares_dir)
670 			vn_setops(vp, zfs_sharevnodeops);
671 		else
672 			vn_setops(vp, zfs_fvnodeops);
673 		break;
674 	case VLNK:
675 		vn_setops(vp, zfs_symvnodeops);
676 		break;
677 	default:
678 		vn_setops(vp, zfs_evnodeops);
679 		break;
680 	}
681 
682 	mutex_enter(&zfsvfs->z_znodes_lock);
683 	list_insert_tail(&zfsvfs->z_all_znodes, zp);
684 	membar_producer();
685 	/*
686 	 * Everything else must be valid before assigning z_zfsvfs makes the
687 	 * znode eligible for zfs_znode_move().
688 	 */
689 	zp->z_zfsvfs = zfsvfs;
690 	mutex_exit(&zfsvfs->z_znodes_lock);
691 
692 	VFS_HOLD(zfsvfs->z_vfs);
693 	return (zp);
694 }
695 
696 /*
697  * Create a new DMU object to hold a zfs znode.
698  *
699  *	IN:	dzp	- parent directory for new znode
700  *		vap	- file attributes for new znode
701  *		tx	- dmu transaction id for zap operations
702  *		cr	- credentials of caller
703  *		flag	- flags:
704  *			  IS_ROOT_NODE	- new object will be root
705  *			  IS_XATTR	- new object is an attribute
706  *		bonuslen - length of bonus buffer
707  *		setaclp  - File/Dir initial ACL
708  *		fuidp	 - Tracks fuid allocation.
709  *
710  *	OUT:	zpp	- allocated znode
711  *
712  */
713 void
714 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
715     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_ids_t *acl_ids)
716 {
717 	dmu_buf_t	*db;
718 	znode_phys_t	*pzp;
719 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
720 	timestruc_t	now;
721 	uint64_t	gen, obj;
722 	int		err;
723 
724 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
725 
726 	if (zfsvfs->z_replay) {
727 		obj = vap->va_nodeid;
728 		now = vap->va_ctime;		/* see zfs_replay_create() */
729 		gen = vap->va_nblocks;		/* ditto */
730 	} else {
731 		obj = 0;
732 		gethrestime(&now);
733 		gen = dmu_tx_get_txg(tx);
734 	}
735 
736 	/*
737 	 * Create a new DMU object.
738 	 */
739 	/*
740 	 * There's currently no mechanism for pre-reading the blocks that will
741 	 * be to needed allocate a new object, so we accept the small chance
742 	 * that there will be an i/o error and we will fail one of the
743 	 * assertions below.
744 	 */
745 	if (vap->va_type == VDIR) {
746 		if (zfsvfs->z_replay) {
747 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
748 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
749 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
750 			ASSERT3U(err, ==, 0);
751 		} else {
752 			obj = zap_create_norm(zfsvfs->z_os,
753 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
754 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
755 		}
756 	} else {
757 		if (zfsvfs->z_replay) {
758 			err = dmu_object_claim(zfsvfs->z_os, obj,
759 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
760 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
761 			ASSERT3U(err, ==, 0);
762 		} else {
763 			obj = dmu_object_alloc(zfsvfs->z_os,
764 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
765 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
766 		}
767 	}
768 
769 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
770 	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
771 	dmu_buf_will_dirty(db, tx);
772 
773 	/*
774 	 * Initialize the znode physical data to zero.
775 	 */
776 	ASSERT(db->db_size >= sizeof (znode_phys_t));
777 	bzero(db->db_data, db->db_size);
778 	pzp = db->db_data;
779 
780 	/*
781 	 * If this is the root, fix up the half-initialized parent pointer
782 	 * to reference the just-allocated physical data area.
783 	 */
784 	if (flag & IS_ROOT_NODE) {
785 		dzp->z_dbuf = db;
786 		dzp->z_phys = pzp;
787 		dzp->z_id = obj;
788 	}
789 
790 	/*
791 	 * If parent is an xattr, so am I.
792 	 */
793 	if (dzp->z_phys->zp_flags & ZFS_XATTR)
794 		flag |= IS_XATTR;
795 
796 	if (vap->va_type == VBLK || vap->va_type == VCHR) {
797 		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
798 	}
799 
800 	if (zfsvfs->z_use_fuids)
801 		pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
802 
803 	if (vap->va_type == VDIR) {
804 		pzp->zp_size = 2;		/* contents ("." and "..") */
805 		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
806 	}
807 
808 	pzp->zp_parent = dzp->z_id;
809 	if (flag & IS_XATTR)
810 		pzp->zp_flags |= ZFS_XATTR;
811 
812 	pzp->zp_gen = gen;
813 
814 	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
815 	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
816 
817 	if (vap->va_mask & AT_ATIME) {
818 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
819 	} else {
820 		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
821 	}
822 
823 	if (vap->va_mask & AT_MTIME) {
824 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
825 	} else {
826 		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
827 	}
828 	pzp->zp_uid = acl_ids->z_fuid;
829 	pzp->zp_gid = acl_ids->z_fgid;
830 	pzp->zp_mode = acl_ids->z_mode;
831 	if (!(flag & IS_ROOT_NODE)) {
832 		*zpp = zfs_znode_alloc(zfsvfs, db, 0);
833 	} else {
834 		/*
835 		 * If we are creating the root node, the "parent" we
836 		 * passed in is the znode for the root.
837 		 */
838 		*zpp = dzp;
839 	}
840 	VERIFY(0 == zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
841 	if (vap->va_mask & AT_XVATTR)
842 		zfs_xvattr_set(*zpp, (xvattr_t *)vap);
843 
844 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
845 }
846 
847 void
848 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
849 {
850 	xoptattr_t *xoap;
851 
852 	xoap = xva_getxoptattr(xvap);
853 	ASSERT(xoap);
854 
855 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
856 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
857 		XVA_SET_RTN(xvap, XAT_CREATETIME);
858 	}
859 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
860 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
861 		XVA_SET_RTN(xvap, XAT_READONLY);
862 	}
863 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
864 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
865 		XVA_SET_RTN(xvap, XAT_HIDDEN);
866 	}
867 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
868 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
869 		XVA_SET_RTN(xvap, XAT_SYSTEM);
870 	}
871 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
872 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
873 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
874 	}
875 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
876 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
877 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
878 	}
879 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
880 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
881 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
882 	}
883 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
884 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
885 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
886 	}
887 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
888 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
889 		XVA_SET_RTN(xvap, XAT_NODUMP);
890 	}
891 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
892 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
893 		XVA_SET_RTN(xvap, XAT_OPAQUE);
894 	}
895 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
896 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
897 		    xoap->xoa_av_quarantined);
898 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
899 	}
900 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
901 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
902 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
903 	}
904 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
905 		(void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
906 		    sizeof (xoap->xoa_av_scanstamp));
907 		zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
908 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
909 	}
910 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
911 		ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse);
912 		XVA_SET_RTN(xvap, XAT_REPARSE);
913 	}
914 }
915 
916 int
917 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
918 {
919 	dmu_object_info_t doi;
920 	dmu_buf_t	*db;
921 	znode_t		*zp;
922 	int err;
923 
924 	*zpp = NULL;
925 
926 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
927 
928 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
929 	if (err) {
930 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
931 		return (err);
932 	}
933 
934 	dmu_object_info_from_db(db, &doi);
935 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
936 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
937 		dmu_buf_rele(db, NULL);
938 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
939 		return (EINVAL);
940 	}
941 
942 	zp = dmu_buf_get_user(db);
943 	if (zp != NULL) {
944 		mutex_enter(&zp->z_lock);
945 
946 		/*
947 		 * Since we do immediate eviction of the z_dbuf, we
948 		 * should never find a dbuf with a znode that doesn't
949 		 * know about the dbuf.
950 		 */
951 		ASSERT3P(zp->z_dbuf, ==, db);
952 		ASSERT3U(zp->z_id, ==, obj_num);
953 		if (zp->z_unlinked) {
954 			err = ENOENT;
955 		} else {
956 			VN_HOLD(ZTOV(zp));
957 			*zpp = zp;
958 			err = 0;
959 		}
960 		dmu_buf_rele(db, NULL);
961 		mutex_exit(&zp->z_lock);
962 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
963 		return (err);
964 	}
965 
966 	/*
967 	 * Not found create new znode/vnode
968 	 * but only if file exists.
969 	 *
970 	 * There is a small window where zfs_vget() could
971 	 * find this object while a file create is still in
972 	 * progress.  Since a gen number can never be zero
973 	 * we will check that to determine if its an allocated
974 	 * file.
975 	 */
976 
977 	if (((znode_phys_t *)db->db_data)->zp_gen != 0) {
978 		zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
979 		*zpp = zp;
980 		err = 0;
981 	} else {
982 		dmu_buf_rele(db, NULL);
983 		err = ENOENT;
984 	}
985 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
986 	return (err);
987 }
988 
989 int
990 zfs_rezget(znode_t *zp)
991 {
992 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
993 	dmu_object_info_t doi;
994 	dmu_buf_t *db;
995 	uint64_t obj_num = zp->z_id;
996 	int err;
997 
998 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
999 
1000 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
1001 	if (err) {
1002 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1003 		return (err);
1004 	}
1005 
1006 	dmu_object_info_from_db(db, &doi);
1007 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1008 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1009 		dmu_buf_rele(db, NULL);
1010 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1011 		return (EINVAL);
1012 	}
1013 
1014 	if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
1015 		dmu_buf_rele(db, NULL);
1016 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1017 		return (EIO);
1018 	}
1019 
1020 	mutex_enter(&zp->z_acl_lock);
1021 	if (zp->z_acl_cached) {
1022 		zfs_acl_free(zp->z_acl_cached);
1023 		zp->z_acl_cached = NULL;
1024 	}
1025 	mutex_exit(&zp->z_acl_lock);
1026 
1027 	zfs_znode_dmu_init(zfsvfs, zp, db);
1028 	zp->z_unlinked = (zp->z_phys->zp_links == 0);
1029 	zp->z_blksz = doi.doi_data_block_size;
1030 
1031 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1032 
1033 	return (0);
1034 }
1035 
1036 void
1037 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1038 {
1039 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1040 	objset_t *os = zfsvfs->z_os;
1041 	uint64_t obj = zp->z_id;
1042 	uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
1043 
1044 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1045 	if (acl_obj)
1046 		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1047 	VERIFY(0 == dmu_object_free(os, obj, tx));
1048 	zfs_znode_dmu_fini(zp);
1049 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1050 	zfs_znode_free(zp);
1051 }
1052 
1053 void
1054 zfs_zinactive(znode_t *zp)
1055 {
1056 	vnode_t	*vp = ZTOV(zp);
1057 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1058 	uint64_t z_id = zp->z_id;
1059 
1060 	ASSERT(zp->z_dbuf && zp->z_phys);
1061 
1062 	/*
1063 	 * Don't allow a zfs_zget() while were trying to release this znode
1064 	 */
1065 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1066 
1067 	mutex_enter(&zp->z_lock);
1068 	mutex_enter(&vp->v_lock);
1069 	vp->v_count--;
1070 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1071 		/*
1072 		 * If the hold count is greater than zero, somebody has
1073 		 * obtained a new reference on this znode while we were
1074 		 * processing it here, so we are done.  If we still have
1075 		 * mapped pages then we are also done, since we don't
1076 		 * want to inactivate the znode until the pages get pushed.
1077 		 *
1078 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1079 		 * this seems like it would leave the znode hanging with
1080 		 * no chance to go inactive...
1081 		 */
1082 		mutex_exit(&vp->v_lock);
1083 		mutex_exit(&zp->z_lock);
1084 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1085 		return;
1086 	}
1087 	mutex_exit(&vp->v_lock);
1088 
1089 	/*
1090 	 * If this was the last reference to a file with no links,
1091 	 * remove the file from the file system.
1092 	 */
1093 	if (zp->z_unlinked) {
1094 		mutex_exit(&zp->z_lock);
1095 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1096 		zfs_rmnode(zp);
1097 		return;
1098 	}
1099 	mutex_exit(&zp->z_lock);
1100 	zfs_znode_dmu_fini(zp);
1101 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1102 	zfs_znode_free(zp);
1103 }
1104 
1105 void
1106 zfs_znode_free(znode_t *zp)
1107 {
1108 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1109 
1110 	vn_invalid(ZTOV(zp));
1111 
1112 	ASSERT(ZTOV(zp)->v_count == 0);
1113 
1114 	mutex_enter(&zfsvfs->z_znodes_lock);
1115 	POINTER_INVALIDATE(&zp->z_zfsvfs);
1116 	list_remove(&zfsvfs->z_all_znodes, zp);
1117 	mutex_exit(&zfsvfs->z_znodes_lock);
1118 
1119 	if (zp->z_acl_cached) {
1120 		zfs_acl_free(zp->z_acl_cached);
1121 		zp->z_acl_cached = NULL;
1122 	}
1123 
1124 	kmem_cache_free(znode_cache, zp);
1125 
1126 	VFS_RELE(zfsvfs->z_vfs);
1127 }
1128 
1129 void
1130 zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1131 {
1132 	timestruc_t	now;
1133 
1134 	ASSERT(MUTEX_HELD(&zp->z_lock));
1135 
1136 	gethrestime(&now);
1137 
1138 	if (tx) {
1139 		dmu_buf_will_dirty(zp->z_dbuf, tx);
1140 		zp->z_atime_dirty = 0;
1141 		zp->z_seq++;
1142 	} else {
1143 		zp->z_atime_dirty = 1;
1144 	}
1145 
1146 	if (flag & AT_ATIME)
1147 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
1148 
1149 	if (flag & AT_MTIME) {
1150 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
1151 		if (zp->z_zfsvfs->z_use_fuids)
1152 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
1153 	}
1154 
1155 	if (flag & AT_CTIME) {
1156 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
1157 		if (zp->z_zfsvfs->z_use_fuids)
1158 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
1159 	}
1160 }
1161 
1162 /*
1163  * Update the requested znode timestamps with the current time.
1164  * If we are in a transaction, then go ahead and mark the znode
1165  * dirty in the transaction so the timestamps will go to disk.
1166  * Otherwise, we will get pushed next time the znode is updated
1167  * in a transaction, or when this znode eventually goes inactive.
1168  *
1169  * Why is this OK?
1170  *  1 - Only the ACCESS time is ever updated outside of a transaction.
1171  *  2 - Multiple consecutive updates will be collapsed into a single
1172  *	znode update by the transaction grouping semantics of the DMU.
1173  */
1174 void
1175 zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1176 {
1177 	mutex_enter(&zp->z_lock);
1178 	zfs_time_stamper_locked(zp, flag, tx);
1179 	mutex_exit(&zp->z_lock);
1180 }
1181 
1182 /*
1183  * Grow the block size for a file.
1184  *
1185  *	IN:	zp	- znode of file to free data in.
1186  *		size	- requested block size
1187  *		tx	- open transaction.
1188  *
1189  * NOTE: this function assumes that the znode is write locked.
1190  */
1191 void
1192 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1193 {
1194 	int		error;
1195 	u_longlong_t	dummy;
1196 
1197 	if (size <= zp->z_blksz)
1198 		return;
1199 	/*
1200 	 * If the file size is already greater than the current blocksize,
1201 	 * we will not grow.  If there is more than one block in a file,
1202 	 * the blocksize cannot change.
1203 	 */
1204 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1205 		return;
1206 
1207 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1208 	    size, 0, tx);
1209 	if (error == ENOTSUP)
1210 		return;
1211 	ASSERT3U(error, ==, 0);
1212 
1213 	/* What blocksize did we actually get? */
1214 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1215 }
1216 
1217 /*
1218  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1219  * be calling back into the fs for a putpage().  E.g.: when truncating
1220  * a file, the pages being "thrown away* don't need to be written out.
1221  */
1222 /* ARGSUSED */
1223 static int
1224 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1225     int flags, cred_t *cr)
1226 {
1227 	ASSERT(0);
1228 	return (0);
1229 }
1230 
1231 /*
1232  * Increase the file length
1233  *
1234  *	IN:	zp	- znode of file to free data in.
1235  *		end	- new end-of-file
1236  *
1237  * 	RETURN:	0 if success
1238  *		error code if failure
1239  */
1240 static int
1241 zfs_extend(znode_t *zp, uint64_t end)
1242 {
1243 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1244 	dmu_tx_t *tx;
1245 	rl_t *rl;
1246 	uint64_t newblksz;
1247 	int error;
1248 
1249 	/*
1250 	 * We will change zp_size, lock the whole file.
1251 	 */
1252 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1253 
1254 	/*
1255 	 * Nothing to do if file already at desired length.
1256 	 */
1257 	if (end <= zp->z_phys->zp_size) {
1258 		zfs_range_unlock(rl);
1259 		return (0);
1260 	}
1261 top:
1262 	tx = dmu_tx_create(zfsvfs->z_os);
1263 	dmu_tx_hold_bonus(tx, zp->z_id);
1264 	if (end > zp->z_blksz &&
1265 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1266 		/*
1267 		 * We are growing the file past the current block size.
1268 		 */
1269 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1270 			ASSERT(!ISP2(zp->z_blksz));
1271 			newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1272 		} else {
1273 			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1274 		}
1275 		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1276 	} else {
1277 		newblksz = 0;
1278 	}
1279 
1280 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1281 	if (error) {
1282 		if (error == ERESTART) {
1283 			dmu_tx_wait(tx);
1284 			dmu_tx_abort(tx);
1285 			goto top;
1286 		}
1287 		dmu_tx_abort(tx);
1288 		zfs_range_unlock(rl);
1289 		return (error);
1290 	}
1291 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1292 
1293 	if (newblksz)
1294 		zfs_grow_blocksize(zp, newblksz, tx);
1295 
1296 	zp->z_phys->zp_size = end;
1297 
1298 	zfs_range_unlock(rl);
1299 
1300 	dmu_tx_commit(tx);
1301 
1302 	return (0);
1303 }
1304 
1305 /*
1306  * Free space in a file.
1307  *
1308  *	IN:	zp	- znode of file to free data in.
1309  *		off	- start of section to free.
1310  *		len	- length of section to free.
1311  *
1312  * 	RETURN:	0 if success
1313  *		error code if failure
1314  */
1315 static int
1316 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1317 {
1318 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1319 	rl_t *rl;
1320 	int error;
1321 
1322 	/*
1323 	 * Lock the range being freed.
1324 	 */
1325 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1326 
1327 	/*
1328 	 * Nothing to do if file already at desired length.
1329 	 */
1330 	if (off >= zp->z_phys->zp_size) {
1331 		zfs_range_unlock(rl);
1332 		return (0);
1333 	}
1334 
1335 	if (off + len > zp->z_phys->zp_size)
1336 		len = zp->z_phys->zp_size - off;
1337 
1338 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1339 
1340 	zfs_range_unlock(rl);
1341 
1342 	return (error);
1343 }
1344 
1345 /*
1346  * Truncate a file
1347  *
1348  *	IN:	zp	- znode of file to free data in.
1349  *		end	- new end-of-file.
1350  *
1351  * 	RETURN:	0 if success
1352  *		error code if failure
1353  */
1354 static int
1355 zfs_trunc(znode_t *zp, uint64_t end)
1356 {
1357 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1358 	vnode_t *vp = ZTOV(zp);
1359 	dmu_tx_t *tx;
1360 	rl_t *rl;
1361 	int error;
1362 
1363 	/*
1364 	 * We will change zp_size, lock the whole file.
1365 	 */
1366 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1367 
1368 	/*
1369 	 * Nothing to do if file already at desired length.
1370 	 */
1371 	if (end >= zp->z_phys->zp_size) {
1372 		zfs_range_unlock(rl);
1373 		return (0);
1374 	}
1375 
1376 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1377 	if (error) {
1378 		zfs_range_unlock(rl);
1379 		return (error);
1380 	}
1381 top:
1382 	tx = dmu_tx_create(zfsvfs->z_os);
1383 	dmu_tx_hold_bonus(tx, zp->z_id);
1384 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1385 	if (error) {
1386 		if (error == ERESTART) {
1387 			dmu_tx_wait(tx);
1388 			dmu_tx_abort(tx);
1389 			goto top;
1390 		}
1391 		dmu_tx_abort(tx);
1392 		zfs_range_unlock(rl);
1393 		return (error);
1394 	}
1395 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1396 
1397 	zp->z_phys->zp_size = end;
1398 
1399 	dmu_tx_commit(tx);
1400 
1401 	/*
1402 	 * Clear any mapped pages in the truncated region.  This has to
1403 	 * happen outside of the transaction to avoid the possibility of
1404 	 * a deadlock with someone trying to push a page that we are
1405 	 * about to invalidate.
1406 	 */
1407 	if (vn_has_cached_data(vp)) {
1408 		page_t *pp;
1409 		uint64_t start = end & PAGEMASK;
1410 		int poff = end & PAGEOFFSET;
1411 
1412 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1413 			/*
1414 			 * We need to zero a partial page.
1415 			 */
1416 			pagezero(pp, poff, PAGESIZE - poff);
1417 			start += PAGESIZE;
1418 			page_unlock(pp);
1419 		}
1420 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1421 		    B_INVAL | B_TRUNC, NULL);
1422 		ASSERT(error == 0);
1423 	}
1424 
1425 	zfs_range_unlock(rl);
1426 
1427 	return (0);
1428 }
1429 
1430 /*
1431  * Free space in a file
1432  *
1433  *	IN:	zp	- znode of file to free data in.
1434  *		off	- start of range
1435  *		len	- end of range (0 => EOF)
1436  *		flag	- current file open mode flags.
1437  *		log	- TRUE if this action should be logged
1438  *
1439  * 	RETURN:	0 if success
1440  *		error code if failure
1441  */
1442 int
1443 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1444 {
1445 	vnode_t *vp = ZTOV(zp);
1446 	dmu_tx_t *tx;
1447 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1448 	zilog_t *zilog = zfsvfs->z_log;
1449 	int error;
1450 
1451 	if (off > zp->z_phys->zp_size) {
1452 		error =  zfs_extend(zp, off+len);
1453 		if (error == 0 && log)
1454 			goto log;
1455 		else
1456 			return (error);
1457 	}
1458 
1459 	/*
1460 	 * Check for any locks in the region to be freed.
1461 	 */
1462 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
1463 		uint64_t length = (len ? len : zp->z_phys->zp_size - off);
1464 		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1465 			return (error);
1466 	}
1467 
1468 	if (len == 0) {
1469 		error = zfs_trunc(zp, off);
1470 	} else {
1471 		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1472 		    off + len > zp->z_phys->zp_size)
1473 			error = zfs_extend(zp, off+len);
1474 	}
1475 	if (error || !log)
1476 		return (error);
1477 log:
1478 	tx = dmu_tx_create(zfsvfs->z_os);
1479 	dmu_tx_hold_bonus(tx, zp->z_id);
1480 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1481 	if (error) {
1482 		if (error == ERESTART) {
1483 			dmu_tx_wait(tx);
1484 			dmu_tx_abort(tx);
1485 			goto log;
1486 		}
1487 		dmu_tx_abort(tx);
1488 		return (error);
1489 	}
1490 
1491 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1492 	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1493 
1494 	dmu_tx_commit(tx);
1495 	return (0);
1496 }
1497 
1498 void
1499 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1500 {
1501 	zfsvfs_t	zfsvfs;
1502 	uint64_t	moid, obj, version;
1503 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1504 	uint64_t	norm = 0;
1505 	nvpair_t	*elem;
1506 	int		error;
1507 	int		i;
1508 	znode_t		*rootzp = NULL;
1509 	vnode_t		*vp;
1510 	vattr_t		vattr;
1511 	znode_t		*zp;
1512 	zfs_acl_ids_t	acl_ids;
1513 
1514 	/*
1515 	 * First attempt to create master node.
1516 	 */
1517 	/*
1518 	 * In an empty objset, there are no blocks to read and thus
1519 	 * there can be no i/o errors (which we assert below).
1520 	 */
1521 	moid = MASTER_NODE_OBJ;
1522 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1523 	    DMU_OT_NONE, 0, tx);
1524 	ASSERT(error == 0);
1525 
1526 	/*
1527 	 * Set starting attributes.
1528 	 */
1529 	if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_USERSPACE)
1530 		version = ZPL_VERSION;
1531 	else if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
1532 		version = ZPL_VERSION_USERSPACE - 1;
1533 	else
1534 		version = ZPL_VERSION_FUID - 1;
1535 	elem = NULL;
1536 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1537 		/* For the moment we expect all zpl props to be uint64_ts */
1538 		uint64_t val;
1539 		char *name;
1540 
1541 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1542 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1543 		name = nvpair_name(elem);
1544 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1545 			if (val < version)
1546 				version = val;
1547 		} else {
1548 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1549 		}
1550 		ASSERT(error == 0);
1551 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1552 			norm = val;
1553 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1554 			sense = val;
1555 	}
1556 	ASSERT(version != 0);
1557 	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1558 
1559 	/*
1560 	 * Create a delete queue.
1561 	 */
1562 	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1563 
1564 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1565 	ASSERT(error == 0);
1566 
1567 	/*
1568 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1569 	 * to allow zfs_mknode to work.
1570 	 */
1571 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1572 	vattr.va_type = VDIR;
1573 	vattr.va_mode = S_IFDIR|0755;
1574 	vattr.va_uid = crgetuid(cr);
1575 	vattr.va_gid = crgetgid(cr);
1576 
1577 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1578 	rootzp->z_unlinked = 0;
1579 	rootzp->z_atime_dirty = 0;
1580 
1581 	vp = ZTOV(rootzp);
1582 	vn_reinit(vp);
1583 	vp->v_type = VDIR;
1584 
1585 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1586 
1587 	zfsvfs.z_os = os;
1588 	zfsvfs.z_parent = &zfsvfs;
1589 	zfsvfs.z_version = version;
1590 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1591 	zfsvfs.z_norm = norm;
1592 	/*
1593 	 * Fold case on file systems that are always or sometimes case
1594 	 * insensitive.
1595 	 */
1596 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1597 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1598 
1599 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1600 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1601 	    offsetof(znode_t, z_link_node));
1602 
1603 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1604 		mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1605 
1606 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1607 	rootzp->z_zfsvfs = &zfsvfs;
1608 	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1609 	    cr, NULL, &acl_ids));
1610 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, &acl_ids);
1611 	ASSERT3P(zp, ==, rootzp);
1612 	ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
1613 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1614 	ASSERT(error == 0);
1615 	zfs_acl_ids_free(&acl_ids);
1616 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1617 
1618 	ZTOV(rootzp)->v_count = 0;
1619 	dmu_buf_rele(rootzp->z_dbuf, NULL);
1620 	rootzp->z_dbuf = NULL;
1621 	kmem_cache_free(znode_cache, rootzp);
1622 
1623 	/*
1624 	 * Create shares directory
1625 	 */
1626 
1627 	error = zfs_create_share_dir(&zfsvfs, tx);
1628 
1629 	ASSERT(error == 0);
1630 
1631 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1632 		mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1633 }
1634 
1635 #endif /* _KERNEL */
1636 /*
1637  * Given an object number, return its parent object number and whether
1638  * or not the object is an extended attribute directory.
1639  */
1640 static int
1641 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1642 {
1643 	dmu_buf_t *db;
1644 	dmu_object_info_t doi;
1645 	znode_phys_t *zp;
1646 	int error;
1647 
1648 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1649 		return (error);
1650 
1651 	dmu_object_info_from_db(db, &doi);
1652 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1653 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1654 		dmu_buf_rele(db, FTAG);
1655 		return (EINVAL);
1656 	}
1657 
1658 	zp = db->db_data;
1659 	*pobjp = zp->zp_parent;
1660 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1661 	    S_ISDIR(zp->zp_mode);
1662 	dmu_buf_rele(db, FTAG);
1663 
1664 	return (0);
1665 }
1666 
1667 int
1668 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1669 {
1670 	char *path = buf + len - 1;
1671 	int error;
1672 
1673 	*path = '\0';
1674 
1675 	for (;;) {
1676 		uint64_t pobj;
1677 		char component[MAXNAMELEN + 2];
1678 		size_t complen;
1679 		int is_xattrdir;
1680 
1681 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1682 		    &is_xattrdir)) != 0)
1683 			break;
1684 
1685 		if (pobj == obj) {
1686 			if (path[0] != '/')
1687 				*--path = '/';
1688 			break;
1689 		}
1690 
1691 		component[0] = '/';
1692 		if (is_xattrdir) {
1693 			(void) sprintf(component + 1, "<xattrdir>");
1694 		} else {
1695 			error = zap_value_search(osp, pobj, obj,
1696 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
1697 			if (error != 0)
1698 				break;
1699 		}
1700 
1701 		complen = strlen(component);
1702 		path -= complen;
1703 		ASSERT(path >= buf);
1704 		bcopy(component, path, complen);
1705 		obj = pobj;
1706 	}
1707 
1708 	if (error == 0)
1709 		(void) memmove(buf, path, buf + len - path);
1710 	return (error);
1711 }
1712