xref: /titanic_52/usr/src/uts/common/fs/zfs/zfs_znode.c (revision 807e8f10a5bd74064a2c6ed98b73c845df2f834e)
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  *			  IS_REPLAY	- intent log replay
707  *		bonuslen - length of bonus buffer
708  *		setaclp  - File/Dir initial ACL
709  *		fuidp	 - Tracks fuid allocation.
710  *
711  *	OUT:	zpp	- allocated znode
712  *
713  */
714 void
715 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
716     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_ids_t *acl_ids)
717 {
718 	dmu_buf_t	*db;
719 	znode_phys_t	*pzp;
720 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
721 	timestruc_t	now;
722 	uint64_t	gen, obj;
723 	int		err;
724 
725 	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
726 
727 	if (zfsvfs->z_replay) {
728 		obj = vap->va_nodeid;
729 		flag |= IS_REPLAY;
730 		now = vap->va_ctime;		/* see zfs_replay_create() */
731 		gen = vap->va_nblocks;		/* ditto */
732 	} else {
733 		obj = 0;
734 		gethrestime(&now);
735 		gen = dmu_tx_get_txg(tx);
736 	}
737 
738 	/*
739 	 * Create a new DMU object.
740 	 */
741 	/*
742 	 * There's currently no mechanism for pre-reading the blocks that will
743 	 * be to needed allocate a new object, so we accept the small chance
744 	 * that there will be an i/o error and we will fail one of the
745 	 * assertions below.
746 	 */
747 	if (vap->va_type == VDIR) {
748 		if (flag & IS_REPLAY) {
749 			err = zap_create_claim_norm(zfsvfs->z_os, obj,
750 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
751 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
752 			ASSERT3U(err, ==, 0);
753 		} else {
754 			obj = zap_create_norm(zfsvfs->z_os,
755 			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
756 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
757 		}
758 	} else {
759 		if (flag & IS_REPLAY) {
760 			err = dmu_object_claim(zfsvfs->z_os, obj,
761 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
762 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
763 			ASSERT3U(err, ==, 0);
764 		} else {
765 			obj = dmu_object_alloc(zfsvfs->z_os,
766 			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
767 			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
768 		}
769 	}
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 
829 	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
830 	if (!(flag & IS_ROOT_NODE)) {
831 		ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
832 		*zpp = zfs_znode_alloc(zfsvfs, db, 0);
833 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
834 	} else {
835 		/*
836 		 * If we are creating the root node, the "parent" we
837 		 * passed in is the znode for the root.
838 		 */
839 		*zpp = dzp;
840 	}
841 	pzp->zp_uid = acl_ids->z_fuid;
842 	pzp->zp_gid = acl_ids->z_fgid;
843 	pzp->zp_mode = acl_ids->z_mode;
844 	VERIFY(0 == zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
845 	if (vap->va_mask & AT_XVATTR)
846 		zfs_xvattr_set(*zpp, (xvattr_t *)vap);
847 }
848 
849 void
850 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
851 {
852 	xoptattr_t *xoap;
853 
854 	xoap = xva_getxoptattr(xvap);
855 	ASSERT(xoap);
856 
857 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
858 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
859 		XVA_SET_RTN(xvap, XAT_CREATETIME);
860 	}
861 	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
862 		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
863 		XVA_SET_RTN(xvap, XAT_READONLY);
864 	}
865 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
866 		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
867 		XVA_SET_RTN(xvap, XAT_HIDDEN);
868 	}
869 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
870 		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
871 		XVA_SET_RTN(xvap, XAT_SYSTEM);
872 	}
873 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
874 		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
875 		XVA_SET_RTN(xvap, XAT_ARCHIVE);
876 	}
877 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
878 		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
879 		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
880 	}
881 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
882 		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
883 		XVA_SET_RTN(xvap, XAT_NOUNLINK);
884 	}
885 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
886 		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
887 		XVA_SET_RTN(xvap, XAT_APPENDONLY);
888 	}
889 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
890 		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
891 		XVA_SET_RTN(xvap, XAT_NODUMP);
892 	}
893 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
894 		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
895 		XVA_SET_RTN(xvap, XAT_OPAQUE);
896 	}
897 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
898 		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
899 		    xoap->xoa_av_quarantined);
900 		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
901 	}
902 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
903 		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
904 		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
905 	}
906 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
907 		(void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
908 		    sizeof (xoap->xoa_av_scanstamp));
909 		zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
910 		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
911 	}
912 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
913 		ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse);
914 		XVA_SET_RTN(xvap, XAT_REPARSE);
915 	}
916 }
917 
918 int
919 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
920 {
921 	dmu_object_info_t doi;
922 	dmu_buf_t	*db;
923 	znode_t		*zp;
924 	int err;
925 
926 	*zpp = NULL;
927 
928 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
929 
930 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
931 	if (err) {
932 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
933 		return (err);
934 	}
935 
936 	dmu_object_info_from_db(db, &doi);
937 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
938 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
939 		dmu_buf_rele(db, NULL);
940 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
941 		return (EINVAL);
942 	}
943 
944 	zp = dmu_buf_get_user(db);
945 	if (zp != NULL) {
946 		mutex_enter(&zp->z_lock);
947 
948 		/*
949 		 * Since we do immediate eviction of the z_dbuf, we
950 		 * should never find a dbuf with a znode that doesn't
951 		 * know about the dbuf.
952 		 */
953 		ASSERT3P(zp->z_dbuf, ==, db);
954 		ASSERT3U(zp->z_id, ==, obj_num);
955 		if (zp->z_unlinked) {
956 			err = ENOENT;
957 		} else {
958 			VN_HOLD(ZTOV(zp));
959 			*zpp = zp;
960 			err = 0;
961 		}
962 		dmu_buf_rele(db, NULL);
963 		mutex_exit(&zp->z_lock);
964 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
965 		return (err);
966 	}
967 
968 	/*
969 	 * Not found create new znode/vnode
970 	 */
971 	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
972 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
973 	*zpp = zp;
974 	return (0);
975 }
976 
977 int
978 zfs_rezget(znode_t *zp)
979 {
980 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
981 	dmu_object_info_t doi;
982 	dmu_buf_t *db;
983 	uint64_t obj_num = zp->z_id;
984 	int err;
985 
986 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
987 
988 	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
989 	if (err) {
990 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
991 		return (err);
992 	}
993 
994 	dmu_object_info_from_db(db, &doi);
995 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
996 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
997 		dmu_buf_rele(db, NULL);
998 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
999 		return (EINVAL);
1000 	}
1001 
1002 	if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
1003 		dmu_buf_rele(db, NULL);
1004 		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1005 		return (EIO);
1006 	}
1007 
1008 	mutex_enter(&zp->z_acl_lock);
1009 	if (zp->z_acl_cached) {
1010 		zfs_acl_free(zp->z_acl_cached);
1011 		zp->z_acl_cached = NULL;
1012 	}
1013 	mutex_exit(&zp->z_acl_lock);
1014 
1015 	zfs_znode_dmu_init(zfsvfs, zp, db);
1016 	zp->z_unlinked = (zp->z_phys->zp_links == 0);
1017 	zp->z_blksz = doi.doi_data_block_size;
1018 
1019 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1020 
1021 	return (0);
1022 }
1023 
1024 void
1025 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1026 {
1027 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1028 	objset_t *os = zfsvfs->z_os;
1029 	uint64_t obj = zp->z_id;
1030 	uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
1031 
1032 	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1033 	if (acl_obj)
1034 		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1035 	VERIFY(0 == dmu_object_free(os, obj, tx));
1036 	zfs_znode_dmu_fini(zp);
1037 	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1038 	zfs_znode_free(zp);
1039 }
1040 
1041 void
1042 zfs_zinactive(znode_t *zp)
1043 {
1044 	vnode_t	*vp = ZTOV(zp);
1045 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1046 	uint64_t z_id = zp->z_id;
1047 
1048 	ASSERT(zp->z_dbuf && zp->z_phys);
1049 
1050 	/*
1051 	 * Don't allow a zfs_zget() while were trying to release this znode
1052 	 */
1053 	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1054 
1055 	mutex_enter(&zp->z_lock);
1056 	mutex_enter(&vp->v_lock);
1057 	vp->v_count--;
1058 	if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1059 		/*
1060 		 * If the hold count is greater than zero, somebody has
1061 		 * obtained a new reference on this znode while we were
1062 		 * processing it here, so we are done.  If we still have
1063 		 * mapped pages then we are also done, since we don't
1064 		 * want to inactivate the znode until the pages get pushed.
1065 		 *
1066 		 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1067 		 * this seems like it would leave the znode hanging with
1068 		 * no chance to go inactive...
1069 		 */
1070 		mutex_exit(&vp->v_lock);
1071 		mutex_exit(&zp->z_lock);
1072 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1073 		return;
1074 	}
1075 	mutex_exit(&vp->v_lock);
1076 
1077 	/*
1078 	 * If this was the last reference to a file with no links,
1079 	 * remove the file from the file system.
1080 	 */
1081 	if (zp->z_unlinked) {
1082 		mutex_exit(&zp->z_lock);
1083 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1084 		zfs_rmnode(zp);
1085 		return;
1086 	}
1087 	mutex_exit(&zp->z_lock);
1088 	zfs_znode_dmu_fini(zp);
1089 	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1090 	zfs_znode_free(zp);
1091 }
1092 
1093 void
1094 zfs_znode_free(znode_t *zp)
1095 {
1096 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1097 
1098 	vn_invalid(ZTOV(zp));
1099 
1100 	ASSERT(ZTOV(zp)->v_count == 0);
1101 
1102 	mutex_enter(&zfsvfs->z_znodes_lock);
1103 	POINTER_INVALIDATE(&zp->z_zfsvfs);
1104 	list_remove(&zfsvfs->z_all_znodes, zp);
1105 	mutex_exit(&zfsvfs->z_znodes_lock);
1106 
1107 	if (zp->z_acl_cached) {
1108 		zfs_acl_free(zp->z_acl_cached);
1109 		zp->z_acl_cached = NULL;
1110 	}
1111 
1112 	kmem_cache_free(znode_cache, zp);
1113 
1114 	VFS_RELE(zfsvfs->z_vfs);
1115 }
1116 
1117 void
1118 zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1119 {
1120 	timestruc_t	now;
1121 
1122 	ASSERT(MUTEX_HELD(&zp->z_lock));
1123 
1124 	gethrestime(&now);
1125 
1126 	if (tx) {
1127 		dmu_buf_will_dirty(zp->z_dbuf, tx);
1128 		zp->z_atime_dirty = 0;
1129 		zp->z_seq++;
1130 	} else {
1131 		zp->z_atime_dirty = 1;
1132 	}
1133 
1134 	if (flag & AT_ATIME)
1135 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
1136 
1137 	if (flag & AT_MTIME) {
1138 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
1139 		if (zp->z_zfsvfs->z_use_fuids)
1140 			zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
1141 	}
1142 
1143 	if (flag & AT_CTIME) {
1144 		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
1145 		if (zp->z_zfsvfs->z_use_fuids)
1146 			zp->z_phys->zp_flags |= ZFS_ARCHIVE;
1147 	}
1148 }
1149 
1150 /*
1151  * Update the requested znode timestamps with the current time.
1152  * If we are in a transaction, then go ahead and mark the znode
1153  * dirty in the transaction so the timestamps will go to disk.
1154  * Otherwise, we will get pushed next time the znode is updated
1155  * in a transaction, or when this znode eventually goes inactive.
1156  *
1157  * Why is this OK?
1158  *  1 - Only the ACCESS time is ever updated outside of a transaction.
1159  *  2 - Multiple consecutive updates will be collapsed into a single
1160  *	znode update by the transaction grouping semantics of the DMU.
1161  */
1162 void
1163 zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1164 {
1165 	mutex_enter(&zp->z_lock);
1166 	zfs_time_stamper_locked(zp, flag, tx);
1167 	mutex_exit(&zp->z_lock);
1168 }
1169 
1170 /*
1171  * Grow the block size for a file.
1172  *
1173  *	IN:	zp	- znode of file to free data in.
1174  *		size	- requested block size
1175  *		tx	- open transaction.
1176  *
1177  * NOTE: this function assumes that the znode is write locked.
1178  */
1179 void
1180 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1181 {
1182 	int		error;
1183 	u_longlong_t	dummy;
1184 
1185 	if (size <= zp->z_blksz)
1186 		return;
1187 	/*
1188 	 * If the file size is already greater than the current blocksize,
1189 	 * we will not grow.  If there is more than one block in a file,
1190 	 * the blocksize cannot change.
1191 	 */
1192 	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1193 		return;
1194 
1195 	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1196 	    size, 0, tx);
1197 	if (error == ENOTSUP)
1198 		return;
1199 	ASSERT3U(error, ==, 0);
1200 
1201 	/* What blocksize did we actually get? */
1202 	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1203 }
1204 
1205 /*
1206  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1207  * be calling back into the fs for a putpage().  E.g.: when truncating
1208  * a file, the pages being "thrown away* don't need to be written out.
1209  */
1210 /* ARGSUSED */
1211 static int
1212 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1213     int flags, cred_t *cr)
1214 {
1215 	ASSERT(0);
1216 	return (0);
1217 }
1218 
1219 /*
1220  * Increase the file length
1221  *
1222  *	IN:	zp	- znode of file to free data in.
1223  *		end	- new end-of-file
1224  *
1225  * 	RETURN:	0 if success
1226  *		error code if failure
1227  */
1228 static int
1229 zfs_extend(znode_t *zp, uint64_t end)
1230 {
1231 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1232 	dmu_tx_t *tx;
1233 	rl_t *rl;
1234 	uint64_t newblksz;
1235 	int error;
1236 
1237 	/*
1238 	 * We will change zp_size, lock the whole file.
1239 	 */
1240 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1241 
1242 	/*
1243 	 * Nothing to do if file already at desired length.
1244 	 */
1245 	if (end <= zp->z_phys->zp_size) {
1246 		zfs_range_unlock(rl);
1247 		return (0);
1248 	}
1249 top:
1250 	tx = dmu_tx_create(zfsvfs->z_os);
1251 	dmu_tx_hold_bonus(tx, zp->z_id);
1252 	if (end > zp->z_blksz &&
1253 	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1254 		/*
1255 		 * We are growing the file past the current block size.
1256 		 */
1257 		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1258 			ASSERT(!ISP2(zp->z_blksz));
1259 			newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1260 		} else {
1261 			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1262 		}
1263 		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1264 	} else {
1265 		newblksz = 0;
1266 	}
1267 
1268 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1269 	if (error) {
1270 		if (error == ERESTART) {
1271 			dmu_tx_wait(tx);
1272 			dmu_tx_abort(tx);
1273 			goto top;
1274 		}
1275 		dmu_tx_abort(tx);
1276 		zfs_range_unlock(rl);
1277 		return (error);
1278 	}
1279 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1280 
1281 	if (newblksz)
1282 		zfs_grow_blocksize(zp, newblksz, tx);
1283 
1284 	zp->z_phys->zp_size = end;
1285 
1286 	zfs_range_unlock(rl);
1287 
1288 	dmu_tx_commit(tx);
1289 
1290 	return (0);
1291 }
1292 
1293 /*
1294  * Free space in a file.
1295  *
1296  *	IN:	zp	- znode of file to free data in.
1297  *		off	- start of section to free.
1298  *		len	- length of section to free.
1299  *
1300  * 	RETURN:	0 if success
1301  *		error code if failure
1302  */
1303 static int
1304 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1305 {
1306 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1307 	rl_t *rl;
1308 	int error;
1309 
1310 	/*
1311 	 * Lock the range being freed.
1312 	 */
1313 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1314 
1315 	/*
1316 	 * Nothing to do if file already at desired length.
1317 	 */
1318 	if (off >= zp->z_phys->zp_size) {
1319 		zfs_range_unlock(rl);
1320 		return (0);
1321 	}
1322 
1323 	if (off + len > zp->z_phys->zp_size)
1324 		len = zp->z_phys->zp_size - off;
1325 
1326 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1327 
1328 	zfs_range_unlock(rl);
1329 
1330 	return (error);
1331 }
1332 
1333 /*
1334  * Truncate a file
1335  *
1336  *	IN:	zp	- znode of file to free data in.
1337  *		end	- new end-of-file.
1338  *
1339  * 	RETURN:	0 if success
1340  *		error code if failure
1341  */
1342 static int
1343 zfs_trunc(znode_t *zp, uint64_t end)
1344 {
1345 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1346 	vnode_t *vp = ZTOV(zp);
1347 	dmu_tx_t *tx;
1348 	rl_t *rl;
1349 	int error;
1350 
1351 	/*
1352 	 * We will change zp_size, lock the whole file.
1353 	 */
1354 	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1355 
1356 	/*
1357 	 * Nothing to do if file already at desired length.
1358 	 */
1359 	if (end >= zp->z_phys->zp_size) {
1360 		zfs_range_unlock(rl);
1361 		return (0);
1362 	}
1363 
1364 	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1365 	if (error) {
1366 		zfs_range_unlock(rl);
1367 		return (error);
1368 	}
1369 top:
1370 	tx = dmu_tx_create(zfsvfs->z_os);
1371 	dmu_tx_hold_bonus(tx, zp->z_id);
1372 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1373 	if (error) {
1374 		if (error == ERESTART) {
1375 			dmu_tx_wait(tx);
1376 			dmu_tx_abort(tx);
1377 			goto top;
1378 		}
1379 		dmu_tx_abort(tx);
1380 		zfs_range_unlock(rl);
1381 		return (error);
1382 	}
1383 	dmu_buf_will_dirty(zp->z_dbuf, tx);
1384 
1385 	zp->z_phys->zp_size = end;
1386 
1387 	dmu_tx_commit(tx);
1388 
1389 	/*
1390 	 * Clear any mapped pages in the truncated region.  This has to
1391 	 * happen outside of the transaction to avoid the possibility of
1392 	 * a deadlock with someone trying to push a page that we are
1393 	 * about to invalidate.
1394 	 */
1395 	if (vn_has_cached_data(vp)) {
1396 		page_t *pp;
1397 		uint64_t start = end & PAGEMASK;
1398 		int poff = end & PAGEOFFSET;
1399 
1400 		if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1401 			/*
1402 			 * We need to zero a partial page.
1403 			 */
1404 			pagezero(pp, poff, PAGESIZE - poff);
1405 			start += PAGESIZE;
1406 			page_unlock(pp);
1407 		}
1408 		error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1409 		    B_INVAL | B_TRUNC, NULL);
1410 		ASSERT(error == 0);
1411 	}
1412 
1413 	zfs_range_unlock(rl);
1414 
1415 	return (0);
1416 }
1417 
1418 /*
1419  * Free space in a file
1420  *
1421  *	IN:	zp	- znode of file to free data in.
1422  *		off	- start of range
1423  *		len	- end of range (0 => EOF)
1424  *		flag	- current file open mode flags.
1425  *		log	- TRUE if this action should be logged
1426  *
1427  * 	RETURN:	0 if success
1428  *		error code if failure
1429  */
1430 int
1431 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1432 {
1433 	vnode_t *vp = ZTOV(zp);
1434 	dmu_tx_t *tx;
1435 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1436 	zilog_t *zilog = zfsvfs->z_log;
1437 	int error;
1438 
1439 	if (off > zp->z_phys->zp_size) {
1440 		error =  zfs_extend(zp, off+len);
1441 		if (error == 0 && log)
1442 			goto log;
1443 		else
1444 			return (error);
1445 	}
1446 
1447 	/*
1448 	 * Check for any locks in the region to be freed.
1449 	 */
1450 	if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
1451 		uint64_t length = (len ? len : zp->z_phys->zp_size - off);
1452 		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1453 			return (error);
1454 	}
1455 
1456 	if (len == 0) {
1457 		error = zfs_trunc(zp, off);
1458 	} else {
1459 		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1460 		    off + len > zp->z_phys->zp_size)
1461 			error = zfs_extend(zp, off+len);
1462 	}
1463 	if (error || !log)
1464 		return (error);
1465 log:
1466 	tx = dmu_tx_create(zfsvfs->z_os);
1467 	dmu_tx_hold_bonus(tx, zp->z_id);
1468 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1469 	if (error) {
1470 		if (error == ERESTART) {
1471 			dmu_tx_wait(tx);
1472 			dmu_tx_abort(tx);
1473 			goto log;
1474 		}
1475 		dmu_tx_abort(tx);
1476 		return (error);
1477 	}
1478 
1479 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1480 	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1481 
1482 	dmu_tx_commit(tx);
1483 	return (0);
1484 }
1485 
1486 void
1487 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1488 {
1489 	zfsvfs_t	zfsvfs;
1490 	uint64_t	moid, obj, version;
1491 	uint64_t	sense = ZFS_CASE_SENSITIVE;
1492 	uint64_t	norm = 0;
1493 	nvpair_t	*elem;
1494 	int		error;
1495 	znode_t		*rootzp = NULL;
1496 	vnode_t		*vp;
1497 	vattr_t		vattr;
1498 	znode_t		*zp;
1499 	zfs_acl_ids_t	acl_ids;
1500 
1501 	/*
1502 	 * First attempt to create master node.
1503 	 */
1504 	/*
1505 	 * In an empty objset, there are no blocks to read and thus
1506 	 * there can be no i/o errors (which we assert below).
1507 	 */
1508 	moid = MASTER_NODE_OBJ;
1509 	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1510 	    DMU_OT_NONE, 0, tx);
1511 	ASSERT(error == 0);
1512 
1513 	/*
1514 	 * Set starting attributes.
1515 	 */
1516 	if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_USERSPACE)
1517 		version = ZPL_VERSION;
1518 	else if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
1519 		version = ZPL_VERSION_USERSPACE - 1;
1520 	else
1521 		version = ZPL_VERSION_FUID - 1;
1522 	elem = NULL;
1523 	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1524 		/* For the moment we expect all zpl props to be uint64_ts */
1525 		uint64_t val;
1526 		char *name;
1527 
1528 		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1529 		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1530 		name = nvpair_name(elem);
1531 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1532 			if (val < version)
1533 				version = val;
1534 		} else {
1535 			error = zap_update(os, moid, name, 8, 1, &val, tx);
1536 		}
1537 		ASSERT(error == 0);
1538 		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1539 			norm = val;
1540 		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1541 			sense = val;
1542 	}
1543 	ASSERT(version != 0);
1544 	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1545 
1546 	/*
1547 	 * Create a delete queue.
1548 	 */
1549 	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1550 
1551 	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1552 	ASSERT(error == 0);
1553 
1554 	/*
1555 	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1556 	 * to allow zfs_mknode to work.
1557 	 */
1558 	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1559 	vattr.va_type = VDIR;
1560 	vattr.va_mode = S_IFDIR|0755;
1561 	vattr.va_uid = crgetuid(cr);
1562 	vattr.va_gid = crgetgid(cr);
1563 
1564 	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1565 	rootzp->z_unlinked = 0;
1566 	rootzp->z_atime_dirty = 0;
1567 
1568 	vp = ZTOV(rootzp);
1569 	vn_reinit(vp);
1570 	vp->v_type = VDIR;
1571 
1572 	bzero(&zfsvfs, sizeof (zfsvfs_t));
1573 
1574 	zfsvfs.z_os = os;
1575 	zfsvfs.z_parent = &zfsvfs;
1576 	zfsvfs.z_version = version;
1577 	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1578 	zfsvfs.z_norm = norm;
1579 	/*
1580 	 * Fold case on file systems that are always or sometimes case
1581 	 * insensitive.
1582 	 */
1583 	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1584 		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1585 
1586 	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1587 	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1588 	    offsetof(znode_t, z_link_node));
1589 
1590 	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1591 	rootzp->z_zfsvfs = &zfsvfs;
1592 	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1593 	    cr, NULL, &acl_ids));
1594 	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, &acl_ids);
1595 	ASSERT3P(zp, ==, rootzp);
1596 	ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
1597 	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1598 	ASSERT(error == 0);
1599 	zfs_acl_ids_free(&acl_ids);
1600 	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1601 
1602 	ZTOV(rootzp)->v_count = 0;
1603 	dmu_buf_rele(rootzp->z_dbuf, NULL);
1604 	rootzp->z_dbuf = NULL;
1605 	kmem_cache_free(znode_cache, rootzp);
1606 
1607 	/*
1608 	 * Create shares directory
1609 	 */
1610 
1611 	error = zfs_create_share_dir(&zfsvfs, tx);
1612 
1613 	ASSERT(error == 0);
1614 }
1615 
1616 #endif /* _KERNEL */
1617 /*
1618  * Given an object number, return its parent object number and whether
1619  * or not the object is an extended attribute directory.
1620  */
1621 static int
1622 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1623 {
1624 	dmu_buf_t *db;
1625 	dmu_object_info_t doi;
1626 	znode_phys_t *zp;
1627 	int error;
1628 
1629 	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1630 		return (error);
1631 
1632 	dmu_object_info_from_db(db, &doi);
1633 	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1634 	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1635 		dmu_buf_rele(db, FTAG);
1636 		return (EINVAL);
1637 	}
1638 
1639 	zp = db->db_data;
1640 	*pobjp = zp->zp_parent;
1641 	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1642 	    S_ISDIR(zp->zp_mode);
1643 	dmu_buf_rele(db, FTAG);
1644 
1645 	return (0);
1646 }
1647 
1648 int
1649 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1650 {
1651 	char *path = buf + len - 1;
1652 	int error;
1653 
1654 	*path = '\0';
1655 
1656 	for (;;) {
1657 		uint64_t pobj;
1658 		char component[MAXNAMELEN + 2];
1659 		size_t complen;
1660 		int is_xattrdir;
1661 
1662 		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1663 		    &is_xattrdir)) != 0)
1664 			break;
1665 
1666 		if (pobj == obj) {
1667 			if (path[0] != '/')
1668 				*--path = '/';
1669 			break;
1670 		}
1671 
1672 		component[0] = '/';
1673 		if (is_xattrdir) {
1674 			(void) sprintf(component + 1, "<xattrdir>");
1675 		} else {
1676 			error = zap_value_search(osp, pobj, obj,
1677 			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
1678 			if (error != 0)
1679 				break;
1680 		}
1681 
1682 		complen = strlen(component);
1683 		path -= complen;
1684 		ASSERT(path >= buf);
1685 		bcopy(component, path, complen);
1686 		obj = pobj;
1687 	}
1688 
1689 	if (error == 0)
1690 		(void) memmove(buf, path, buf + len - path);
1691 	return (error);
1692 }
1693