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