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