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