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