xref: /freebsd-src/sys/contrib/openzfs/module/os/linux/zfs/zfs_vfsops.c (revision 16d6b3b3da62aa5baaf3c66c8d4e6f8c8f70aeb7)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24  */
25 
26 /* Portions Copyright 2010 Robert Milkowski */
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/sysmacros.h>
31 #include <sys/kmem.h>
32 #include <sys/pathname.h>
33 #include <sys/vnode.h>
34 #include <sys/vfs.h>
35 #include <sys/mntent.h>
36 #include <sys/cmn_err.h>
37 #include <sys/zfs_znode.h>
38 #include <sys/zfs_vnops.h>
39 #include <sys/zfs_dir.h>
40 #include <sys/zil.h>
41 #include <sys/fs/zfs.h>
42 #include <sys/dmu.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/dsl_dataset.h>
45 #include <sys/dsl_deleg.h>
46 #include <sys/spa.h>
47 #include <sys/zap.h>
48 #include <sys/sa.h>
49 #include <sys/sa_impl.h>
50 #include <sys/policy.h>
51 #include <sys/atomic.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_ctldir.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/zfs_quota.h>
56 #include <sys/sunddi.h>
57 #include <sys/dmu_objset.h>
58 #include <sys/dsl_dir.h>
59 #include <sys/spa_boot.h>
60 #include <sys/objlist.h>
61 #include <sys/zpl.h>
62 #include <linux/vfs_compat.h>
63 #include "zfs_comutil.h"
64 
65 enum {
66 	TOKEN_RO,
67 	TOKEN_RW,
68 	TOKEN_SETUID,
69 	TOKEN_NOSETUID,
70 	TOKEN_EXEC,
71 	TOKEN_NOEXEC,
72 	TOKEN_DEVICES,
73 	TOKEN_NODEVICES,
74 	TOKEN_DIRXATTR,
75 	TOKEN_SAXATTR,
76 	TOKEN_XATTR,
77 	TOKEN_NOXATTR,
78 	TOKEN_ATIME,
79 	TOKEN_NOATIME,
80 	TOKEN_RELATIME,
81 	TOKEN_NORELATIME,
82 	TOKEN_NBMAND,
83 	TOKEN_NONBMAND,
84 	TOKEN_MNTPOINT,
85 	TOKEN_LAST,
86 };
87 
88 static const match_table_t zpl_tokens = {
89 	{ TOKEN_RO,		MNTOPT_RO },
90 	{ TOKEN_RW,		MNTOPT_RW },
91 	{ TOKEN_SETUID,		MNTOPT_SETUID },
92 	{ TOKEN_NOSETUID,	MNTOPT_NOSETUID },
93 	{ TOKEN_EXEC,		MNTOPT_EXEC },
94 	{ TOKEN_NOEXEC,		MNTOPT_NOEXEC },
95 	{ TOKEN_DEVICES,	MNTOPT_DEVICES },
96 	{ TOKEN_NODEVICES,	MNTOPT_NODEVICES },
97 	{ TOKEN_DIRXATTR,	MNTOPT_DIRXATTR },
98 	{ TOKEN_SAXATTR,	MNTOPT_SAXATTR },
99 	{ TOKEN_XATTR,		MNTOPT_XATTR },
100 	{ TOKEN_NOXATTR,	MNTOPT_NOXATTR },
101 	{ TOKEN_ATIME,		MNTOPT_ATIME },
102 	{ TOKEN_NOATIME,	MNTOPT_NOATIME },
103 	{ TOKEN_RELATIME,	MNTOPT_RELATIME },
104 	{ TOKEN_NORELATIME,	MNTOPT_NORELATIME },
105 	{ TOKEN_NBMAND,		MNTOPT_NBMAND },
106 	{ TOKEN_NONBMAND,	MNTOPT_NONBMAND },
107 	{ TOKEN_MNTPOINT,	MNTOPT_MNTPOINT "=%s" },
108 	{ TOKEN_LAST,		NULL },
109 };
110 
111 static void
112 zfsvfs_vfs_free(vfs_t *vfsp)
113 {
114 	if (vfsp != NULL) {
115 		if (vfsp->vfs_mntpoint != NULL)
116 			kmem_strfree(vfsp->vfs_mntpoint);
117 
118 		kmem_free(vfsp, sizeof (vfs_t));
119 	}
120 }
121 
122 static int
123 zfsvfs_parse_option(char *option, int token, substring_t *args, vfs_t *vfsp)
124 {
125 	switch (token) {
126 	case TOKEN_RO:
127 		vfsp->vfs_readonly = B_TRUE;
128 		vfsp->vfs_do_readonly = B_TRUE;
129 		break;
130 	case TOKEN_RW:
131 		vfsp->vfs_readonly = B_FALSE;
132 		vfsp->vfs_do_readonly = B_TRUE;
133 		break;
134 	case TOKEN_SETUID:
135 		vfsp->vfs_setuid = B_TRUE;
136 		vfsp->vfs_do_setuid = B_TRUE;
137 		break;
138 	case TOKEN_NOSETUID:
139 		vfsp->vfs_setuid = B_FALSE;
140 		vfsp->vfs_do_setuid = B_TRUE;
141 		break;
142 	case TOKEN_EXEC:
143 		vfsp->vfs_exec = B_TRUE;
144 		vfsp->vfs_do_exec = B_TRUE;
145 		break;
146 	case TOKEN_NOEXEC:
147 		vfsp->vfs_exec = B_FALSE;
148 		vfsp->vfs_do_exec = B_TRUE;
149 		break;
150 	case TOKEN_DEVICES:
151 		vfsp->vfs_devices = B_TRUE;
152 		vfsp->vfs_do_devices = B_TRUE;
153 		break;
154 	case TOKEN_NODEVICES:
155 		vfsp->vfs_devices = B_FALSE;
156 		vfsp->vfs_do_devices = B_TRUE;
157 		break;
158 	case TOKEN_DIRXATTR:
159 		vfsp->vfs_xattr = ZFS_XATTR_DIR;
160 		vfsp->vfs_do_xattr = B_TRUE;
161 		break;
162 	case TOKEN_SAXATTR:
163 		vfsp->vfs_xattr = ZFS_XATTR_SA;
164 		vfsp->vfs_do_xattr = B_TRUE;
165 		break;
166 	case TOKEN_XATTR:
167 		vfsp->vfs_xattr = ZFS_XATTR_DIR;
168 		vfsp->vfs_do_xattr = B_TRUE;
169 		break;
170 	case TOKEN_NOXATTR:
171 		vfsp->vfs_xattr = ZFS_XATTR_OFF;
172 		vfsp->vfs_do_xattr = B_TRUE;
173 		break;
174 	case TOKEN_ATIME:
175 		vfsp->vfs_atime = B_TRUE;
176 		vfsp->vfs_do_atime = B_TRUE;
177 		break;
178 	case TOKEN_NOATIME:
179 		vfsp->vfs_atime = B_FALSE;
180 		vfsp->vfs_do_atime = B_TRUE;
181 		break;
182 	case TOKEN_RELATIME:
183 		vfsp->vfs_relatime = B_TRUE;
184 		vfsp->vfs_do_relatime = B_TRUE;
185 		break;
186 	case TOKEN_NORELATIME:
187 		vfsp->vfs_relatime = B_FALSE;
188 		vfsp->vfs_do_relatime = B_TRUE;
189 		break;
190 	case TOKEN_NBMAND:
191 		vfsp->vfs_nbmand = B_TRUE;
192 		vfsp->vfs_do_nbmand = B_TRUE;
193 		break;
194 	case TOKEN_NONBMAND:
195 		vfsp->vfs_nbmand = B_FALSE;
196 		vfsp->vfs_do_nbmand = B_TRUE;
197 		break;
198 	case TOKEN_MNTPOINT:
199 		vfsp->vfs_mntpoint = match_strdup(&args[0]);
200 		if (vfsp->vfs_mntpoint == NULL)
201 			return (SET_ERROR(ENOMEM));
202 
203 		break;
204 	default:
205 		break;
206 	}
207 
208 	return (0);
209 }
210 
211 /*
212  * Parse the raw mntopts and return a vfs_t describing the options.
213  */
214 static int
215 zfsvfs_parse_options(char *mntopts, vfs_t **vfsp)
216 {
217 	vfs_t *tmp_vfsp;
218 	int error;
219 
220 	tmp_vfsp = kmem_zalloc(sizeof (vfs_t), KM_SLEEP);
221 
222 	if (mntopts != NULL) {
223 		substring_t args[MAX_OPT_ARGS];
224 		char *tmp_mntopts, *p, *t;
225 		int token;
226 
227 		tmp_mntopts = t = kmem_strdup(mntopts);
228 		if (tmp_mntopts == NULL)
229 			return (SET_ERROR(ENOMEM));
230 
231 		while ((p = strsep(&t, ",")) != NULL) {
232 			if (!*p)
233 				continue;
234 
235 			args[0].to = args[0].from = NULL;
236 			token = match_token(p, zpl_tokens, args);
237 			error = zfsvfs_parse_option(p, token, args, tmp_vfsp);
238 			if (error) {
239 				kmem_strfree(tmp_mntopts);
240 				zfsvfs_vfs_free(tmp_vfsp);
241 				return (error);
242 			}
243 		}
244 
245 		kmem_strfree(tmp_mntopts);
246 	}
247 
248 	*vfsp = tmp_vfsp;
249 
250 	return (0);
251 }
252 
253 boolean_t
254 zfs_is_readonly(zfsvfs_t *zfsvfs)
255 {
256 	return (!!(zfsvfs->z_sb->s_flags & SB_RDONLY));
257 }
258 
259 /*ARGSUSED*/
260 int
261 zfs_sync(struct super_block *sb, int wait, cred_t *cr)
262 {
263 	zfsvfs_t *zfsvfs = sb->s_fs_info;
264 
265 	/*
266 	 * Semantically, the only requirement is that the sync be initiated.
267 	 * The DMU syncs out txgs frequently, so there's nothing to do.
268 	 */
269 	if (!wait)
270 		return (0);
271 
272 	if (zfsvfs != NULL) {
273 		/*
274 		 * Sync a specific filesystem.
275 		 */
276 		dsl_pool_t *dp;
277 
278 		ZFS_ENTER(zfsvfs);
279 		dp = dmu_objset_pool(zfsvfs->z_os);
280 
281 		/*
282 		 * If the system is shutting down, then skip any
283 		 * filesystems which may exist on a suspended pool.
284 		 */
285 		if (spa_suspended(dp->dp_spa)) {
286 			ZFS_EXIT(zfsvfs);
287 			return (0);
288 		}
289 
290 		if (zfsvfs->z_log != NULL)
291 			zil_commit(zfsvfs->z_log, 0);
292 
293 		ZFS_EXIT(zfsvfs);
294 	} else {
295 		/*
296 		 * Sync all ZFS filesystems.  This is what happens when you
297 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
298 		 * request by waiting for all pools to commit all dirty data.
299 		 */
300 		spa_sync_allpools();
301 	}
302 
303 	return (0);
304 }
305 
306 static void
307 atime_changed_cb(void *arg, uint64_t newval)
308 {
309 	zfsvfs_t *zfsvfs = arg;
310 	struct super_block *sb = zfsvfs->z_sb;
311 
312 	if (sb == NULL)
313 		return;
314 	/*
315 	 * Update SB_NOATIME bit in VFS super block.  Since atime update is
316 	 * determined by atime_needs_update(), atime_needs_update() needs to
317 	 * return false if atime is turned off, and not unconditionally return
318 	 * false if atime is turned on.
319 	 */
320 	if (newval)
321 		sb->s_flags &= ~SB_NOATIME;
322 	else
323 		sb->s_flags |= SB_NOATIME;
324 }
325 
326 static void
327 relatime_changed_cb(void *arg, uint64_t newval)
328 {
329 	((zfsvfs_t *)arg)->z_relatime = newval;
330 }
331 
332 static void
333 xattr_changed_cb(void *arg, uint64_t newval)
334 {
335 	zfsvfs_t *zfsvfs = arg;
336 
337 	if (newval == ZFS_XATTR_OFF) {
338 		zfsvfs->z_flags &= ~ZSB_XATTR;
339 	} else {
340 		zfsvfs->z_flags |= ZSB_XATTR;
341 
342 		if (newval == ZFS_XATTR_SA)
343 			zfsvfs->z_xattr_sa = B_TRUE;
344 		else
345 			zfsvfs->z_xattr_sa = B_FALSE;
346 	}
347 }
348 
349 static void
350 acltype_changed_cb(void *arg, uint64_t newval)
351 {
352 	zfsvfs_t *zfsvfs = arg;
353 
354 	switch (newval) {
355 	case ZFS_ACLTYPE_OFF:
356 		zfsvfs->z_acl_type = ZFS_ACLTYPE_OFF;
357 		zfsvfs->z_sb->s_flags &= ~SB_POSIXACL;
358 		break;
359 	case ZFS_ACLTYPE_POSIXACL:
360 #ifdef CONFIG_FS_POSIX_ACL
361 		zfsvfs->z_acl_type = ZFS_ACLTYPE_POSIXACL;
362 		zfsvfs->z_sb->s_flags |= SB_POSIXACL;
363 #else
364 		zfsvfs->z_acl_type = ZFS_ACLTYPE_OFF;
365 		zfsvfs->z_sb->s_flags &= ~SB_POSIXACL;
366 #endif /* CONFIG_FS_POSIX_ACL */
367 		break;
368 	default:
369 		break;
370 	}
371 }
372 
373 static void
374 blksz_changed_cb(void *arg, uint64_t newval)
375 {
376 	zfsvfs_t *zfsvfs = arg;
377 	ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
378 	ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
379 	ASSERT(ISP2(newval));
380 
381 	zfsvfs->z_max_blksz = newval;
382 }
383 
384 static void
385 readonly_changed_cb(void *arg, uint64_t newval)
386 {
387 	zfsvfs_t *zfsvfs = arg;
388 	struct super_block *sb = zfsvfs->z_sb;
389 
390 	if (sb == NULL)
391 		return;
392 
393 	if (newval)
394 		sb->s_flags |= SB_RDONLY;
395 	else
396 		sb->s_flags &= ~SB_RDONLY;
397 }
398 
399 static void
400 devices_changed_cb(void *arg, uint64_t newval)
401 {
402 }
403 
404 static void
405 setuid_changed_cb(void *arg, uint64_t newval)
406 {
407 }
408 
409 static void
410 exec_changed_cb(void *arg, uint64_t newval)
411 {
412 }
413 
414 static void
415 nbmand_changed_cb(void *arg, uint64_t newval)
416 {
417 	zfsvfs_t *zfsvfs = arg;
418 	struct super_block *sb = zfsvfs->z_sb;
419 
420 	if (sb == NULL)
421 		return;
422 
423 	if (newval == TRUE)
424 		sb->s_flags |= SB_MANDLOCK;
425 	else
426 		sb->s_flags &= ~SB_MANDLOCK;
427 }
428 
429 static void
430 snapdir_changed_cb(void *arg, uint64_t newval)
431 {
432 	((zfsvfs_t *)arg)->z_show_ctldir = newval;
433 }
434 
435 static void
436 vscan_changed_cb(void *arg, uint64_t newval)
437 {
438 	((zfsvfs_t *)arg)->z_vscan = newval;
439 }
440 
441 static void
442 acl_mode_changed_cb(void *arg, uint64_t newval)
443 {
444 	zfsvfs_t *zfsvfs = arg;
445 
446 	zfsvfs->z_acl_mode = newval;
447 }
448 
449 static void
450 acl_inherit_changed_cb(void *arg, uint64_t newval)
451 {
452 	((zfsvfs_t *)arg)->z_acl_inherit = newval;
453 }
454 
455 static int
456 zfs_register_callbacks(vfs_t *vfsp)
457 {
458 	struct dsl_dataset *ds = NULL;
459 	objset_t *os = NULL;
460 	zfsvfs_t *zfsvfs = NULL;
461 	int error = 0;
462 
463 	ASSERT(vfsp);
464 	zfsvfs = vfsp->vfs_data;
465 	ASSERT(zfsvfs);
466 	os = zfsvfs->z_os;
467 
468 	/*
469 	 * The act of registering our callbacks will destroy any mount
470 	 * options we may have.  In order to enable temporary overrides
471 	 * of mount options, we stash away the current values and
472 	 * restore them after we register the callbacks.
473 	 */
474 	if (zfs_is_readonly(zfsvfs) || !spa_writeable(dmu_objset_spa(os))) {
475 		vfsp->vfs_do_readonly = B_TRUE;
476 		vfsp->vfs_readonly = B_TRUE;
477 	}
478 
479 	/*
480 	 * Register property callbacks.
481 	 *
482 	 * It would probably be fine to just check for i/o error from
483 	 * the first prop_register(), but I guess I like to go
484 	 * overboard...
485 	 */
486 	ds = dmu_objset_ds(os);
487 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
488 	error = dsl_prop_register(ds,
489 	    zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
490 	error = error ? error : dsl_prop_register(ds,
491 	    zfs_prop_to_name(ZFS_PROP_RELATIME), relatime_changed_cb, zfsvfs);
492 	error = error ? error : dsl_prop_register(ds,
493 	    zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
494 	error = error ? error : dsl_prop_register(ds,
495 	    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
496 	error = error ? error : dsl_prop_register(ds,
497 	    zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
498 	error = error ? error : dsl_prop_register(ds,
499 	    zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
500 	error = error ? error : dsl_prop_register(ds,
501 	    zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
502 	error = error ? error : dsl_prop_register(ds,
503 	    zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
504 	error = error ? error : dsl_prop_register(ds,
505 	    zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
506 	error = error ? error : dsl_prop_register(ds,
507 	    zfs_prop_to_name(ZFS_PROP_ACLTYPE), acltype_changed_cb, zfsvfs);
508 	error = error ? error : dsl_prop_register(ds,
509 	    zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
510 	error = error ? error : dsl_prop_register(ds,
511 	    zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
512 	    zfsvfs);
513 	error = error ? error : dsl_prop_register(ds,
514 	    zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
515 	error = error ? error : dsl_prop_register(ds,
516 	    zfs_prop_to_name(ZFS_PROP_NBMAND), nbmand_changed_cb, zfsvfs);
517 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
518 	if (error)
519 		goto unregister;
520 
521 	/*
522 	 * Invoke our callbacks to restore temporary mount options.
523 	 */
524 	if (vfsp->vfs_do_readonly)
525 		readonly_changed_cb(zfsvfs, vfsp->vfs_readonly);
526 	if (vfsp->vfs_do_setuid)
527 		setuid_changed_cb(zfsvfs, vfsp->vfs_setuid);
528 	if (vfsp->vfs_do_exec)
529 		exec_changed_cb(zfsvfs, vfsp->vfs_exec);
530 	if (vfsp->vfs_do_devices)
531 		devices_changed_cb(zfsvfs, vfsp->vfs_devices);
532 	if (vfsp->vfs_do_xattr)
533 		xattr_changed_cb(zfsvfs, vfsp->vfs_xattr);
534 	if (vfsp->vfs_do_atime)
535 		atime_changed_cb(zfsvfs, vfsp->vfs_atime);
536 	if (vfsp->vfs_do_relatime)
537 		relatime_changed_cb(zfsvfs, vfsp->vfs_relatime);
538 	if (vfsp->vfs_do_nbmand)
539 		nbmand_changed_cb(zfsvfs, vfsp->vfs_nbmand);
540 
541 	return (0);
542 
543 unregister:
544 	dsl_prop_unregister_all(ds, zfsvfs);
545 	return (error);
546 }
547 
548 /*
549  * Takes a dataset, a property, a value and that value's setpoint as
550  * found in the ZAP. Checks if the property has been changed in the vfs.
551  * If so, val and setpoint will be overwritten with updated content.
552  * Otherwise, they are left unchanged.
553  */
554 int
555 zfs_get_temporary_prop(dsl_dataset_t *ds, zfs_prop_t zfs_prop, uint64_t *val,
556     char *setpoint)
557 {
558 	int error;
559 	zfsvfs_t *zfvp;
560 	vfs_t *vfsp;
561 	objset_t *os;
562 	uint64_t tmp = *val;
563 
564 	error = dmu_objset_from_ds(ds, &os);
565 	if (error != 0)
566 		return (error);
567 
568 	if (dmu_objset_type(os) != DMU_OST_ZFS)
569 		return (EINVAL);
570 
571 	mutex_enter(&os->os_user_ptr_lock);
572 	zfvp = dmu_objset_get_user(os);
573 	mutex_exit(&os->os_user_ptr_lock);
574 	if (zfvp == NULL)
575 		return (ESRCH);
576 
577 	vfsp = zfvp->z_vfs;
578 
579 	switch (zfs_prop) {
580 	case ZFS_PROP_ATIME:
581 		if (vfsp->vfs_do_atime)
582 			tmp = vfsp->vfs_atime;
583 		break;
584 	case ZFS_PROP_RELATIME:
585 		if (vfsp->vfs_do_relatime)
586 			tmp = vfsp->vfs_relatime;
587 		break;
588 	case ZFS_PROP_DEVICES:
589 		if (vfsp->vfs_do_devices)
590 			tmp = vfsp->vfs_devices;
591 		break;
592 	case ZFS_PROP_EXEC:
593 		if (vfsp->vfs_do_exec)
594 			tmp = vfsp->vfs_exec;
595 		break;
596 	case ZFS_PROP_SETUID:
597 		if (vfsp->vfs_do_setuid)
598 			tmp = vfsp->vfs_setuid;
599 		break;
600 	case ZFS_PROP_READONLY:
601 		if (vfsp->vfs_do_readonly)
602 			tmp = vfsp->vfs_readonly;
603 		break;
604 	case ZFS_PROP_XATTR:
605 		if (vfsp->vfs_do_xattr)
606 			tmp = vfsp->vfs_xattr;
607 		break;
608 	case ZFS_PROP_NBMAND:
609 		if (vfsp->vfs_do_nbmand)
610 			tmp = vfsp->vfs_nbmand;
611 		break;
612 	default:
613 		return (ENOENT);
614 	}
615 
616 	if (tmp != *val) {
617 		(void) strcpy(setpoint, "temporary");
618 		*val = tmp;
619 	}
620 	return (0);
621 }
622 
623 /*
624  * Associate this zfsvfs with the given objset, which must be owned.
625  * This will cache a bunch of on-disk state from the objset in the
626  * zfsvfs.
627  */
628 static int
629 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
630 {
631 	int error;
632 	uint64_t val;
633 
634 	zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
635 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
636 	zfsvfs->z_os = os;
637 
638 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
639 	if (error != 0)
640 		return (error);
641 	if (zfsvfs->z_version >
642 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
643 		(void) printk("Can't mount a version %lld file system "
644 		    "on a version %lld pool\n. Pool must be upgraded to mount "
645 		    "this file system.\n", (u_longlong_t)zfsvfs->z_version,
646 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
647 		return (SET_ERROR(ENOTSUP));
648 	}
649 	error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
650 	if (error != 0)
651 		return (error);
652 	zfsvfs->z_norm = (int)val;
653 
654 	error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
655 	if (error != 0)
656 		return (error);
657 	zfsvfs->z_utf8 = (val != 0);
658 
659 	error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
660 	if (error != 0)
661 		return (error);
662 	zfsvfs->z_case = (uint_t)val;
663 
664 	if ((error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &val)) != 0)
665 		return (error);
666 	zfsvfs->z_acl_type = (uint_t)val;
667 
668 	/*
669 	 * Fold case on file systems that are always or sometimes case
670 	 * insensitive.
671 	 */
672 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
673 	    zfsvfs->z_case == ZFS_CASE_MIXED)
674 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
675 
676 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
677 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
678 
679 	uint64_t sa_obj = 0;
680 	if (zfsvfs->z_use_sa) {
681 		/* should either have both of these objects or none */
682 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
683 		    &sa_obj);
684 		if (error != 0)
685 			return (error);
686 
687 		error = zfs_get_zplprop(os, ZFS_PROP_XATTR, &val);
688 		if ((error == 0) && (val == ZFS_XATTR_SA))
689 			zfsvfs->z_xattr_sa = B_TRUE;
690 	}
691 
692 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
693 	    &zfsvfs->z_root);
694 	if (error != 0)
695 		return (error);
696 	ASSERT(zfsvfs->z_root != 0);
697 
698 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
699 	    &zfsvfs->z_unlinkedobj);
700 	if (error != 0)
701 		return (error);
702 
703 	error = zap_lookup(os, MASTER_NODE_OBJ,
704 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
705 	    8, 1, &zfsvfs->z_userquota_obj);
706 	if (error == ENOENT)
707 		zfsvfs->z_userquota_obj = 0;
708 	else if (error != 0)
709 		return (error);
710 
711 	error = zap_lookup(os, MASTER_NODE_OBJ,
712 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
713 	    8, 1, &zfsvfs->z_groupquota_obj);
714 	if (error == ENOENT)
715 		zfsvfs->z_groupquota_obj = 0;
716 	else if (error != 0)
717 		return (error);
718 
719 	error = zap_lookup(os, MASTER_NODE_OBJ,
720 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA],
721 	    8, 1, &zfsvfs->z_projectquota_obj);
722 	if (error == ENOENT)
723 		zfsvfs->z_projectquota_obj = 0;
724 	else if (error != 0)
725 		return (error);
726 
727 	error = zap_lookup(os, MASTER_NODE_OBJ,
728 	    zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA],
729 	    8, 1, &zfsvfs->z_userobjquota_obj);
730 	if (error == ENOENT)
731 		zfsvfs->z_userobjquota_obj = 0;
732 	else if (error != 0)
733 		return (error);
734 
735 	error = zap_lookup(os, MASTER_NODE_OBJ,
736 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA],
737 	    8, 1, &zfsvfs->z_groupobjquota_obj);
738 	if (error == ENOENT)
739 		zfsvfs->z_groupobjquota_obj = 0;
740 	else if (error != 0)
741 		return (error);
742 
743 	error = zap_lookup(os, MASTER_NODE_OBJ,
744 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA],
745 	    8, 1, &zfsvfs->z_projectobjquota_obj);
746 	if (error == ENOENT)
747 		zfsvfs->z_projectobjquota_obj = 0;
748 	else if (error != 0)
749 		return (error);
750 
751 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
752 	    &zfsvfs->z_fuid_obj);
753 	if (error == ENOENT)
754 		zfsvfs->z_fuid_obj = 0;
755 	else if (error != 0)
756 		return (error);
757 
758 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
759 	    &zfsvfs->z_shares_dir);
760 	if (error == ENOENT)
761 		zfsvfs->z_shares_dir = 0;
762 	else if (error != 0)
763 		return (error);
764 
765 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
766 	    &zfsvfs->z_attr_table);
767 	if (error != 0)
768 		return (error);
769 
770 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
771 		sa_register_update_callback(os, zfs_sa_upgrade);
772 
773 	return (0);
774 }
775 
776 int
777 zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp)
778 {
779 	objset_t *os;
780 	zfsvfs_t *zfsvfs;
781 	int error;
782 	boolean_t ro = (readonly || (strchr(osname, '@') != NULL));
783 
784 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
785 
786 	error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs, &os);
787 	if (error != 0) {
788 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
789 		return (error);
790 	}
791 
792 	error = zfsvfs_create_impl(zfvp, zfsvfs, os);
793 	if (error != 0) {
794 		dmu_objset_disown(os, B_TRUE, zfsvfs);
795 	}
796 	return (error);
797 }
798 
799 
800 /*
801  * Note: zfsvfs is assumed to be malloc'd, and will be freed by this function
802  * on a failure.  Do not pass in a statically allocated zfsvfs.
803  */
804 int
805 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
806 {
807 	int error;
808 
809 	zfsvfs->z_vfs = NULL;
810 	zfsvfs->z_sb = NULL;
811 	zfsvfs->z_parent = zfsvfs;
812 
813 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
814 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
815 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
816 	    offsetof(znode_t, z_link_node));
817 	rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
818 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
819 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
820 
821 	int size = MIN(1 << (highbit64(zfs_object_mutex_size) - 1),
822 	    ZFS_OBJ_MTX_MAX);
823 	zfsvfs->z_hold_size = size;
824 	zfsvfs->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size,
825 	    KM_SLEEP);
826 	zfsvfs->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
827 	for (int i = 0; i != size; i++) {
828 		avl_create(&zfsvfs->z_hold_trees[i], zfs_znode_hold_compare,
829 		    sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
830 		mutex_init(&zfsvfs->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
831 	}
832 
833 	error = zfsvfs_init(zfsvfs, os);
834 	if (error != 0) {
835 		*zfvp = NULL;
836 		zfsvfs_free(zfsvfs);
837 		return (error);
838 	}
839 
840 	zfsvfs->z_drain_task = TASKQID_INVALID;
841 	zfsvfs->z_draining = B_FALSE;
842 	zfsvfs->z_drain_cancel = B_TRUE;
843 
844 	*zfvp = zfsvfs;
845 	return (0);
846 }
847 
848 static int
849 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
850 {
851 	int error;
852 	boolean_t readonly = zfs_is_readonly(zfsvfs);
853 
854 	error = zfs_register_callbacks(zfsvfs->z_vfs);
855 	if (error)
856 		return (error);
857 
858 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
859 
860 	/*
861 	 * If we are not mounting (ie: online recv), then we don't
862 	 * have to worry about replaying the log as we blocked all
863 	 * operations out since we closed the ZIL.
864 	 */
865 	if (mounting) {
866 		ASSERT3P(zfsvfs->z_kstat.dk_kstats, ==, NULL);
867 		dataset_kstats_create(&zfsvfs->z_kstat, zfsvfs->z_os);
868 
869 		/*
870 		 * During replay we remove the read only flag to
871 		 * allow replays to succeed.
872 		 */
873 		if (readonly != 0) {
874 			readonly_changed_cb(zfsvfs, B_FALSE);
875 		} else {
876 			zap_stats_t zs;
877 			if (zap_get_stats(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
878 			    &zs) == 0) {
879 				dataset_kstats_update_nunlinks_kstat(
880 				    &zfsvfs->z_kstat, zs.zs_num_entries);
881 				dprintf_ds(zfsvfs->z_os->os_dsl_dataset,
882 				    "num_entries in unlinked set: %llu",
883 				    zs.zs_num_entries);
884 			}
885 			zfs_unlinked_drain(zfsvfs);
886 			dsl_dir_t *dd = zfsvfs->z_os->os_dsl_dataset->ds_dir;
887 			dd->dd_activity_cancelled = B_FALSE;
888 		}
889 
890 		/*
891 		 * Parse and replay the intent log.
892 		 *
893 		 * Because of ziltest, this must be done after
894 		 * zfs_unlinked_drain().  (Further note: ziltest
895 		 * doesn't use readonly mounts, where
896 		 * zfs_unlinked_drain() isn't called.)  This is because
897 		 * ziltest causes spa_sync() to think it's committed,
898 		 * but actually it is not, so the intent log contains
899 		 * many txg's worth of changes.
900 		 *
901 		 * In particular, if object N is in the unlinked set in
902 		 * the last txg to actually sync, then it could be
903 		 * actually freed in a later txg and then reallocated
904 		 * in a yet later txg.  This would write a "create
905 		 * object N" record to the intent log.  Normally, this
906 		 * would be fine because the spa_sync() would have
907 		 * written out the fact that object N is free, before
908 		 * we could write the "create object N" intent log
909 		 * record.
910 		 *
911 		 * But when we are in ziltest mode, we advance the "open
912 		 * txg" without actually spa_sync()-ing the changes to
913 		 * disk.  So we would see that object N is still
914 		 * allocated and in the unlinked set, and there is an
915 		 * intent log record saying to allocate it.
916 		 */
917 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
918 			if (zil_replay_disable) {
919 				zil_destroy(zfsvfs->z_log, B_FALSE);
920 			} else {
921 				zfsvfs->z_replay = B_TRUE;
922 				zil_replay(zfsvfs->z_os, zfsvfs,
923 				    zfs_replay_vector);
924 				zfsvfs->z_replay = B_FALSE;
925 			}
926 		}
927 
928 		/* restore readonly bit */
929 		if (readonly != 0)
930 			readonly_changed_cb(zfsvfs, B_TRUE);
931 	}
932 
933 	/*
934 	 * Set the objset user_ptr to track its zfsvfs.
935 	 */
936 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
937 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
938 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
939 
940 	return (0);
941 }
942 
943 void
944 zfsvfs_free(zfsvfs_t *zfsvfs)
945 {
946 	int i, size = zfsvfs->z_hold_size;
947 
948 	zfs_fuid_destroy(zfsvfs);
949 
950 	mutex_destroy(&zfsvfs->z_znodes_lock);
951 	mutex_destroy(&zfsvfs->z_lock);
952 	list_destroy(&zfsvfs->z_all_znodes);
953 	rrm_destroy(&zfsvfs->z_teardown_lock);
954 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
955 	rw_destroy(&zfsvfs->z_fuid_lock);
956 	for (i = 0; i != size; i++) {
957 		avl_destroy(&zfsvfs->z_hold_trees[i]);
958 		mutex_destroy(&zfsvfs->z_hold_locks[i]);
959 	}
960 	vmem_free(zfsvfs->z_hold_trees, sizeof (avl_tree_t) * size);
961 	vmem_free(zfsvfs->z_hold_locks, sizeof (kmutex_t) * size);
962 	zfsvfs_vfs_free(zfsvfs->z_vfs);
963 	dataset_kstats_destroy(&zfsvfs->z_kstat);
964 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
965 }
966 
967 static void
968 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
969 {
970 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
971 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
972 }
973 
974 static void
975 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
976 {
977 	objset_t *os = zfsvfs->z_os;
978 
979 	if (!dmu_objset_is_snapshot(os))
980 		dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
981 }
982 
983 #ifdef HAVE_MLSLABEL
984 /*
985  * Check that the hex label string is appropriate for the dataset being
986  * mounted into the global_zone proper.
987  *
988  * Return an error if the hex label string is not default or
989  * admin_low/admin_high.  For admin_low labels, the corresponding
990  * dataset must be readonly.
991  */
992 int
993 zfs_check_global_label(const char *dsname, const char *hexsl)
994 {
995 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
996 		return (0);
997 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
998 		return (0);
999 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1000 		/* must be readonly */
1001 		uint64_t rdonly;
1002 
1003 		if (dsl_prop_get_integer(dsname,
1004 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1005 			return (SET_ERROR(EACCES));
1006 		return (rdonly ? 0 : SET_ERROR(EACCES));
1007 	}
1008 	return (SET_ERROR(EACCES));
1009 }
1010 #endif /* HAVE_MLSLABEL */
1011 
1012 static int
1013 zfs_statfs_project(zfsvfs_t *zfsvfs, znode_t *zp, struct kstatfs *statp,
1014     uint32_t bshift)
1015 {
1016 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
1017 	uint64_t offset = DMU_OBJACCT_PREFIX_LEN;
1018 	uint64_t quota;
1019 	uint64_t used;
1020 	int err;
1021 
1022 	strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1);
1023 	err = zfs_id_to_fuidstr(zfsvfs, NULL, zp->z_projid, buf + offset,
1024 	    sizeof (buf) - offset, B_FALSE);
1025 	if (err)
1026 		return (err);
1027 
1028 	if (zfsvfs->z_projectquota_obj == 0)
1029 		goto objs;
1030 
1031 	err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectquota_obj,
1032 	    buf + offset, 8, 1, &quota);
1033 	if (err == ENOENT)
1034 		goto objs;
1035 	else if (err)
1036 		return (err);
1037 
1038 	err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1039 	    buf + offset, 8, 1, &used);
1040 	if (unlikely(err == ENOENT)) {
1041 		uint32_t blksize;
1042 		u_longlong_t nblocks;
1043 
1044 		/*
1045 		 * Quota accounting is async, so it is possible race case.
1046 		 * There is at least one object with the given project ID.
1047 		 */
1048 		sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
1049 		if (unlikely(zp->z_blksz == 0))
1050 			blksize = zfsvfs->z_max_blksz;
1051 
1052 		used = blksize * nblocks;
1053 	} else if (err) {
1054 		return (err);
1055 	}
1056 
1057 	statp->f_blocks = quota >> bshift;
1058 	statp->f_bfree = (quota > used) ? ((quota - used) >> bshift) : 0;
1059 	statp->f_bavail = statp->f_bfree;
1060 
1061 objs:
1062 	if (zfsvfs->z_projectobjquota_obj == 0)
1063 		return (0);
1064 
1065 	err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectobjquota_obj,
1066 	    buf + offset, 8, 1, &quota);
1067 	if (err == ENOENT)
1068 		return (0);
1069 	else if (err)
1070 		return (err);
1071 
1072 	err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1073 	    buf, 8, 1, &used);
1074 	if (unlikely(err == ENOENT)) {
1075 		/*
1076 		 * Quota accounting is async, so it is possible race case.
1077 		 * There is at least one object with the given project ID.
1078 		 */
1079 		used = 1;
1080 	} else if (err) {
1081 		return (err);
1082 	}
1083 
1084 	statp->f_files = quota;
1085 	statp->f_ffree = (quota > used) ? (quota - used) : 0;
1086 
1087 	return (0);
1088 }
1089 
1090 int
1091 zfs_statvfs(struct inode *ip, struct kstatfs *statp)
1092 {
1093 	zfsvfs_t *zfsvfs = ITOZSB(ip);
1094 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1095 	int err = 0;
1096 
1097 	ZFS_ENTER(zfsvfs);
1098 
1099 	dmu_objset_space(zfsvfs->z_os,
1100 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1101 
1102 	uint64_t fsid = dmu_objset_fsid_guid(zfsvfs->z_os);
1103 	/*
1104 	 * The underlying storage pool actually uses multiple block
1105 	 * size.  Under Solaris frsize (fragment size) is reported as
1106 	 * the smallest block size we support, and bsize (block size)
1107 	 * as the filesystem's maximum block size.  Unfortunately,
1108 	 * under Linux the fragment size and block size are often used
1109 	 * interchangeably.  Thus we are forced to report both of them
1110 	 * as the filesystem's maximum block size.
1111 	 */
1112 	statp->f_frsize = zfsvfs->z_max_blksz;
1113 	statp->f_bsize = zfsvfs->z_max_blksz;
1114 	uint32_t bshift = fls(statp->f_bsize) - 1;
1115 
1116 	/*
1117 	 * The following report "total" blocks of various kinds in
1118 	 * the file system, but reported in terms of f_bsize - the
1119 	 * "preferred" size.
1120 	 */
1121 
1122 	/* Round up so we never have a filesystem using 0 blocks. */
1123 	refdbytes = P2ROUNDUP(refdbytes, statp->f_bsize);
1124 	statp->f_blocks = (refdbytes + availbytes) >> bshift;
1125 	statp->f_bfree = availbytes >> bshift;
1126 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1127 
1128 	/*
1129 	 * statvfs() should really be called statufs(), because it assumes
1130 	 * static metadata.  ZFS doesn't preallocate files, so the best
1131 	 * we can do is report the max that could possibly fit in f_files,
1132 	 * and that minus the number actually used in f_ffree.
1133 	 * For f_ffree, report the smaller of the number of objects available
1134 	 * and the number of blocks (each object will take at least a block).
1135 	 */
1136 	statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT);
1137 	statp->f_files = statp->f_ffree + usedobjs;
1138 	statp->f_fsid.val[0] = (uint32_t)fsid;
1139 	statp->f_fsid.val[1] = (uint32_t)(fsid >> 32);
1140 	statp->f_type = ZFS_SUPER_MAGIC;
1141 	statp->f_namelen = MAXNAMELEN - 1;
1142 
1143 	/*
1144 	 * We have all of 40 characters to stuff a string here.
1145 	 * Is there anything useful we could/should provide?
1146 	 */
1147 	bzero(statp->f_spare, sizeof (statp->f_spare));
1148 
1149 	if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
1150 	    dmu_objset_projectquota_present(zfsvfs->z_os)) {
1151 		znode_t *zp = ITOZ(ip);
1152 
1153 		if (zp->z_pflags & ZFS_PROJINHERIT && zp->z_projid &&
1154 		    zpl_is_valid_projid(zp->z_projid))
1155 			err = zfs_statfs_project(zfsvfs, zp, statp, bshift);
1156 	}
1157 
1158 	ZFS_EXIT(zfsvfs);
1159 	return (err);
1160 }
1161 
1162 static int
1163 zfs_root(zfsvfs_t *zfsvfs, struct inode **ipp)
1164 {
1165 	znode_t *rootzp;
1166 	int error;
1167 
1168 	ZFS_ENTER(zfsvfs);
1169 
1170 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1171 	if (error == 0)
1172 		*ipp = ZTOI(rootzp);
1173 
1174 	ZFS_EXIT(zfsvfs);
1175 	return (error);
1176 }
1177 
1178 /*
1179  * Linux kernels older than 3.1 do not support a per-filesystem shrinker.
1180  * To accommodate this we must improvise and manually walk the list of znodes
1181  * attempting to prune dentries in order to be able to drop the inodes.
1182  *
1183  * To avoid scanning the same znodes multiple times they are always rotated
1184  * to the end of the z_all_znodes list.  New znodes are inserted at the
1185  * end of the list so we're always scanning the oldest znodes first.
1186  */
1187 static int
1188 zfs_prune_aliases(zfsvfs_t *zfsvfs, unsigned long nr_to_scan)
1189 {
1190 	znode_t **zp_array, *zp;
1191 	int max_array = MIN(nr_to_scan, PAGE_SIZE * 8 / sizeof (znode_t *));
1192 	int objects = 0;
1193 	int i = 0, j = 0;
1194 
1195 	zp_array = kmem_zalloc(max_array * sizeof (znode_t *), KM_SLEEP);
1196 
1197 	mutex_enter(&zfsvfs->z_znodes_lock);
1198 	while ((zp = list_head(&zfsvfs->z_all_znodes)) != NULL) {
1199 
1200 		if ((i++ > nr_to_scan) || (j >= max_array))
1201 			break;
1202 
1203 		ASSERT(list_link_active(&zp->z_link_node));
1204 		list_remove(&zfsvfs->z_all_znodes, zp);
1205 		list_insert_tail(&zfsvfs->z_all_znodes, zp);
1206 
1207 		/* Skip active znodes and .zfs entries */
1208 		if (MUTEX_HELD(&zp->z_lock) || zp->z_is_ctldir)
1209 			continue;
1210 
1211 		if (igrab(ZTOI(zp)) == NULL)
1212 			continue;
1213 
1214 		zp_array[j] = zp;
1215 		j++;
1216 	}
1217 	mutex_exit(&zfsvfs->z_znodes_lock);
1218 
1219 	for (i = 0; i < j; i++) {
1220 		zp = zp_array[i];
1221 
1222 		ASSERT3P(zp, !=, NULL);
1223 		d_prune_aliases(ZTOI(zp));
1224 
1225 		if (atomic_read(&ZTOI(zp)->i_count) == 1)
1226 			objects++;
1227 
1228 		zrele(zp);
1229 	}
1230 
1231 	kmem_free(zp_array, max_array * sizeof (znode_t *));
1232 
1233 	return (objects);
1234 }
1235 
1236 /*
1237  * The ARC has requested that the filesystem drop entries from the dentry
1238  * and inode caches.  This can occur when the ARC needs to free meta data
1239  * blocks but can't because they are all pinned by entries in these caches.
1240  */
1241 int
1242 zfs_prune(struct super_block *sb, unsigned long nr_to_scan, int *objects)
1243 {
1244 	zfsvfs_t *zfsvfs = sb->s_fs_info;
1245 	int error = 0;
1246 	struct shrinker *shrinker = &sb->s_shrink;
1247 	struct shrink_control sc = {
1248 		.nr_to_scan = nr_to_scan,
1249 		.gfp_mask = GFP_KERNEL,
1250 	};
1251 
1252 	ZFS_ENTER(zfsvfs);
1253 
1254 #if defined(HAVE_SPLIT_SHRINKER_CALLBACK) && \
1255 	defined(SHRINK_CONTROL_HAS_NID) && \
1256 	defined(SHRINKER_NUMA_AWARE)
1257 	if (sb->s_shrink.flags & SHRINKER_NUMA_AWARE) {
1258 		*objects = 0;
1259 		for_each_online_node(sc.nid) {
1260 			*objects += (*shrinker->scan_objects)(shrinker, &sc);
1261 		}
1262 	} else {
1263 			*objects = (*shrinker->scan_objects)(shrinker, &sc);
1264 	}
1265 
1266 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1267 	*objects = (*shrinker->scan_objects)(shrinker, &sc);
1268 #elif defined(HAVE_SINGLE_SHRINKER_CALLBACK)
1269 	*objects = (*shrinker->shrink)(shrinker, &sc);
1270 #elif defined(HAVE_D_PRUNE_ALIASES)
1271 #define	D_PRUNE_ALIASES_IS_DEFAULT
1272 	*objects = zfs_prune_aliases(zfsvfs, nr_to_scan);
1273 #else
1274 #error "No available dentry and inode cache pruning mechanism."
1275 #endif
1276 
1277 #if defined(HAVE_D_PRUNE_ALIASES) && !defined(D_PRUNE_ALIASES_IS_DEFAULT)
1278 #undef	D_PRUNE_ALIASES_IS_DEFAULT
1279 	/*
1280 	 * Fall back to zfs_prune_aliases if the kernel's per-superblock
1281 	 * shrinker couldn't free anything, possibly due to the inodes being
1282 	 * allocated in a different memcg.
1283 	 */
1284 	if (*objects == 0)
1285 		*objects = zfs_prune_aliases(zfsvfs, nr_to_scan);
1286 #endif
1287 
1288 	ZFS_EXIT(zfsvfs);
1289 
1290 	dprintf_ds(zfsvfs->z_os->os_dsl_dataset,
1291 	    "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1292 	    nr_to_scan, *objects, error);
1293 
1294 	return (error);
1295 }
1296 
1297 /*
1298  * Teardown the zfsvfs_t.
1299  *
1300  * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1301  * and 'z_teardown_inactive_lock' held.
1302  */
1303 static int
1304 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1305 {
1306 	znode_t	*zp;
1307 
1308 	zfs_unlinked_drain_stop_wait(zfsvfs);
1309 
1310 	/*
1311 	 * If someone has not already unmounted this file system,
1312 	 * drain the zrele_taskq to ensure all active references to the
1313 	 * zfsvfs_t have been handled only then can it be safely destroyed.
1314 	 */
1315 	if (zfsvfs->z_os) {
1316 		/*
1317 		 * If we're unmounting we have to wait for the list to
1318 		 * drain completely.
1319 		 *
1320 		 * If we're not unmounting there's no guarantee the list
1321 		 * will drain completely, but iputs run from the taskq
1322 		 * may add the parents of dir-based xattrs to the taskq
1323 		 * so we want to wait for these.
1324 		 *
1325 		 * We can safely read z_nr_znodes without locking because the
1326 		 * VFS has already blocked operations which add to the
1327 		 * z_all_znodes list and thus increment z_nr_znodes.
1328 		 */
1329 		int round = 0;
1330 		while (zfsvfs->z_nr_znodes > 0) {
1331 			taskq_wait_outstanding(dsl_pool_zrele_taskq(
1332 			    dmu_objset_pool(zfsvfs->z_os)), 0);
1333 			if (++round > 1 && !unmounting)
1334 				break;
1335 		}
1336 	}
1337 
1338 	rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1339 
1340 	if (!unmounting) {
1341 		/*
1342 		 * We purge the parent filesystem's super block as the
1343 		 * parent filesystem and all of its snapshots have their
1344 		 * inode's super block set to the parent's filesystem's
1345 		 * super block.  Note,  'z_parent' is self referential
1346 		 * for non-snapshots.
1347 		 */
1348 		shrink_dcache_sb(zfsvfs->z_parent->z_sb);
1349 	}
1350 
1351 	/*
1352 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1353 	 * threads are blocked as zil_close can call zfs_inactive.
1354 	 */
1355 	if (zfsvfs->z_log) {
1356 		zil_close(zfsvfs->z_log);
1357 		zfsvfs->z_log = NULL;
1358 	}
1359 
1360 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1361 
1362 	/*
1363 	 * If we are not unmounting (ie: online recv) and someone already
1364 	 * unmounted this file system while we were doing the switcheroo,
1365 	 * or a reopen of z_os failed then just bail out now.
1366 	 */
1367 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1368 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1369 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1370 		return (SET_ERROR(EIO));
1371 	}
1372 
1373 	/*
1374 	 * At this point there are no VFS ops active, and any new VFS ops
1375 	 * will fail with EIO since we have z_teardown_lock for writer (only
1376 	 * relevant for forced unmount).
1377 	 *
1378 	 * Release all holds on dbufs. We also grab an extra reference to all
1379 	 * the remaining inodes so that the kernel does not attempt to free
1380 	 * any inodes of a suspended fs. This can cause deadlocks since the
1381 	 * zfs_resume_fs() process may involve starting threads, which might
1382 	 * attempt to free unreferenced inodes to free up memory for the new
1383 	 * thread.
1384 	 */
1385 	if (!unmounting) {
1386 		mutex_enter(&zfsvfs->z_znodes_lock);
1387 		for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1388 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1389 			if (zp->z_sa_hdl)
1390 				zfs_znode_dmu_fini(zp);
1391 			if (igrab(ZTOI(zp)) != NULL)
1392 				zp->z_suspended = B_TRUE;
1393 
1394 		}
1395 		mutex_exit(&zfsvfs->z_znodes_lock);
1396 	}
1397 
1398 	/*
1399 	 * If we are unmounting, set the unmounted flag and let new VFS ops
1400 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1401 	 * other VFS ops will fail with EIO.
1402 	 */
1403 	if (unmounting) {
1404 		zfsvfs->z_unmounted = B_TRUE;
1405 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1406 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1407 	}
1408 
1409 	/*
1410 	 * z_os will be NULL if there was an error in attempting to reopen
1411 	 * zfsvfs, so just return as the properties had already been
1412 	 *
1413 	 * unregistered and cached data had been evicted before.
1414 	 */
1415 	if (zfsvfs->z_os == NULL)
1416 		return (0);
1417 
1418 	/*
1419 	 * Unregister properties.
1420 	 */
1421 	zfs_unregister_callbacks(zfsvfs);
1422 
1423 	/*
1424 	 * Evict cached data. We must write out any dirty data before
1425 	 * disowning the dataset.
1426 	 */
1427 	objset_t *os = zfsvfs->z_os;
1428 	boolean_t os_dirty = B_FALSE;
1429 	for (int t = 0; t < TXG_SIZE; t++) {
1430 		if (dmu_objset_is_dirty(os, t)) {
1431 			os_dirty = B_TRUE;
1432 			break;
1433 		}
1434 	}
1435 	if (!zfs_is_readonly(zfsvfs) && os_dirty) {
1436 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1437 	}
1438 	dmu_objset_evict_dbufs(zfsvfs->z_os);
1439 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1440 	dsl_dir_cancel_waiters(dd);
1441 
1442 	return (0);
1443 }
1444 
1445 #if defined(HAVE_SUPER_SETUP_BDI_NAME)
1446 atomic_long_t zfs_bdi_seq = ATOMIC_LONG_INIT(0);
1447 #endif
1448 
1449 int
1450 zfs_domount(struct super_block *sb, zfs_mnt_t *zm, int silent)
1451 {
1452 	const char *osname = zm->mnt_osname;
1453 	struct inode *root_inode;
1454 	uint64_t recordsize;
1455 	int error = 0;
1456 	zfsvfs_t *zfsvfs = NULL;
1457 	vfs_t *vfs = NULL;
1458 
1459 	ASSERT(zm);
1460 	ASSERT(osname);
1461 
1462 	error = zfsvfs_parse_options(zm->mnt_data, &vfs);
1463 	if (error)
1464 		return (error);
1465 
1466 	error = zfsvfs_create(osname, vfs->vfs_readonly, &zfsvfs);
1467 	if (error) {
1468 		zfsvfs_vfs_free(vfs);
1469 		goto out;
1470 	}
1471 
1472 	if ((error = dsl_prop_get_integer(osname, "recordsize",
1473 	    &recordsize, NULL))) {
1474 		zfsvfs_vfs_free(vfs);
1475 		goto out;
1476 	}
1477 
1478 	vfs->vfs_data = zfsvfs;
1479 	zfsvfs->z_vfs = vfs;
1480 	zfsvfs->z_sb = sb;
1481 	sb->s_fs_info = zfsvfs;
1482 	sb->s_magic = ZFS_SUPER_MAGIC;
1483 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1484 	sb->s_time_gran = 1;
1485 	sb->s_blocksize = recordsize;
1486 	sb->s_blocksize_bits = ilog2(recordsize);
1487 
1488 	error = -zpl_bdi_setup(sb, "zfs");
1489 	if (error)
1490 		goto out;
1491 
1492 	sb->s_bdi->ra_pages = 0;
1493 
1494 	/* Set callback operations for the file system. */
1495 	sb->s_op = &zpl_super_operations;
1496 	sb->s_xattr = zpl_xattr_handlers;
1497 	sb->s_export_op = &zpl_export_operations;
1498 	sb->s_d_op = &zpl_dentry_operations;
1499 
1500 	/* Set features for file system. */
1501 	zfs_set_fuid_feature(zfsvfs);
1502 
1503 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1504 		uint64_t pval;
1505 
1506 		atime_changed_cb(zfsvfs, B_FALSE);
1507 		readonly_changed_cb(zfsvfs, B_TRUE);
1508 		if ((error = dsl_prop_get_integer(osname,
1509 		    "xattr", &pval, NULL)))
1510 			goto out;
1511 		xattr_changed_cb(zfsvfs, pval);
1512 		if ((error = dsl_prop_get_integer(osname,
1513 		    "acltype", &pval, NULL)))
1514 			goto out;
1515 		acltype_changed_cb(zfsvfs, pval);
1516 		zfsvfs->z_issnap = B_TRUE;
1517 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1518 		zfsvfs->z_snap_defer_time = jiffies;
1519 
1520 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1521 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1522 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1523 	} else {
1524 		if ((error = zfsvfs_setup(zfsvfs, B_TRUE)))
1525 			goto out;
1526 	}
1527 
1528 	/* Allocate a root inode for the filesystem. */
1529 	error = zfs_root(zfsvfs, &root_inode);
1530 	if (error) {
1531 		(void) zfs_umount(sb);
1532 		goto out;
1533 	}
1534 
1535 	/* Allocate a root dentry for the filesystem */
1536 	sb->s_root = d_make_root(root_inode);
1537 	if (sb->s_root == NULL) {
1538 		(void) zfs_umount(sb);
1539 		error = SET_ERROR(ENOMEM);
1540 		goto out;
1541 	}
1542 
1543 	if (!zfsvfs->z_issnap)
1544 		zfsctl_create(zfsvfs);
1545 
1546 	zfsvfs->z_arc_prune = arc_add_prune_callback(zpl_prune_sb, sb);
1547 out:
1548 	if (error) {
1549 		if (zfsvfs != NULL) {
1550 			dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1551 			zfsvfs_free(zfsvfs);
1552 		}
1553 		/*
1554 		 * make sure we don't have dangling sb->s_fs_info which
1555 		 * zfs_preumount will use.
1556 		 */
1557 		sb->s_fs_info = NULL;
1558 	}
1559 
1560 	return (error);
1561 }
1562 
1563 /*
1564  * Called when an unmount is requested and certain sanity checks have
1565  * already passed.  At this point no dentries or inodes have been reclaimed
1566  * from their respective caches.  We drop the extra reference on the .zfs
1567  * control directory to allow everything to be reclaimed.  All snapshots
1568  * must already have been unmounted to reach this point.
1569  */
1570 void
1571 zfs_preumount(struct super_block *sb)
1572 {
1573 	zfsvfs_t *zfsvfs = sb->s_fs_info;
1574 
1575 	/* zfsvfs is NULL when zfs_domount fails during mount */
1576 	if (zfsvfs) {
1577 		zfs_unlinked_drain_stop_wait(zfsvfs);
1578 		zfsctl_destroy(sb->s_fs_info);
1579 		/*
1580 		 * Wait for zrele_async before entering evict_inodes in
1581 		 * generic_shutdown_super. The reason we must finish before
1582 		 * evict_inodes is when lazytime is on, or when zfs_purgedir
1583 		 * calls zfs_zget, zrele would bump i_count from 0 to 1. This
1584 		 * would race with the i_count check in evict_inodes. This means
1585 		 * it could destroy the inode while we are still using it.
1586 		 *
1587 		 * We wait for two passes. xattr directories in the first pass
1588 		 * may add xattr entries in zfs_purgedir, so in the second pass
1589 		 * we wait for them. We don't use taskq_wait here because it is
1590 		 * a pool wide taskq. Other mounted filesystems can constantly
1591 		 * do zrele_async and there's no guarantee when taskq will be
1592 		 * empty.
1593 		 */
1594 		taskq_wait_outstanding(dsl_pool_zrele_taskq(
1595 		    dmu_objset_pool(zfsvfs->z_os)), 0);
1596 		taskq_wait_outstanding(dsl_pool_zrele_taskq(
1597 		    dmu_objset_pool(zfsvfs->z_os)), 0);
1598 	}
1599 }
1600 
1601 /*
1602  * Called once all other unmount released tear down has occurred.
1603  * It is our responsibility to release any remaining infrastructure.
1604  */
1605 /*ARGSUSED*/
1606 int
1607 zfs_umount(struct super_block *sb)
1608 {
1609 	zfsvfs_t *zfsvfs = sb->s_fs_info;
1610 	objset_t *os;
1611 
1612 	if (zfsvfs->z_arc_prune != NULL)
1613 		arc_remove_prune_callback(zfsvfs->z_arc_prune);
1614 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1615 	os = zfsvfs->z_os;
1616 	zpl_bdi_destroy(sb);
1617 
1618 	/*
1619 	 * z_os will be NULL if there was an error in
1620 	 * attempting to reopen zfsvfs.
1621 	 */
1622 	if (os != NULL) {
1623 		/*
1624 		 * Unset the objset user_ptr.
1625 		 */
1626 		mutex_enter(&os->os_user_ptr_lock);
1627 		dmu_objset_set_user(os, NULL);
1628 		mutex_exit(&os->os_user_ptr_lock);
1629 
1630 		/*
1631 		 * Finally release the objset
1632 		 */
1633 		dmu_objset_disown(os, B_TRUE, zfsvfs);
1634 	}
1635 
1636 	zfsvfs_free(zfsvfs);
1637 	return (0);
1638 }
1639 
1640 int
1641 zfs_remount(struct super_block *sb, int *flags, zfs_mnt_t *zm)
1642 {
1643 	zfsvfs_t *zfsvfs = sb->s_fs_info;
1644 	vfs_t *vfsp;
1645 	boolean_t issnap = dmu_objset_is_snapshot(zfsvfs->z_os);
1646 	int error;
1647 
1648 	if ((issnap || !spa_writeable(dmu_objset_spa(zfsvfs->z_os))) &&
1649 	    !(*flags & SB_RDONLY)) {
1650 		*flags |= SB_RDONLY;
1651 		return (EROFS);
1652 	}
1653 
1654 	error = zfsvfs_parse_options(zm->mnt_data, &vfsp);
1655 	if (error)
1656 		return (error);
1657 
1658 	if (!zfs_is_readonly(zfsvfs) && (*flags & SB_RDONLY))
1659 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1660 
1661 	zfs_unregister_callbacks(zfsvfs);
1662 	zfsvfs_vfs_free(zfsvfs->z_vfs);
1663 
1664 	vfsp->vfs_data = zfsvfs;
1665 	zfsvfs->z_vfs = vfsp;
1666 	if (!issnap)
1667 		(void) zfs_register_callbacks(vfsp);
1668 
1669 	return (error);
1670 }
1671 
1672 int
1673 zfs_vget(struct super_block *sb, struct inode **ipp, fid_t *fidp)
1674 {
1675 	zfsvfs_t	*zfsvfs = sb->s_fs_info;
1676 	znode_t		*zp;
1677 	uint64_t	object = 0;
1678 	uint64_t	fid_gen = 0;
1679 	uint64_t	gen_mask;
1680 	uint64_t	zp_gen;
1681 	int		i, err;
1682 
1683 	*ipp = NULL;
1684 
1685 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1686 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1687 
1688 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1689 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1690 
1691 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1692 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1693 	} else {
1694 		return (SET_ERROR(EINVAL));
1695 	}
1696 
1697 	/* LONG_FID_LEN means snapdirs */
1698 	if (fidp->fid_len == LONG_FID_LEN) {
1699 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1700 		uint64_t	objsetid = 0;
1701 		uint64_t	setgen = 0;
1702 
1703 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1704 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1705 
1706 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1707 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1708 
1709 		if (objsetid != ZFSCTL_INO_SNAPDIRS - object) {
1710 			dprintf("snapdir fid: objsetid (%llu) != "
1711 			    "ZFSCTL_INO_SNAPDIRS (%llu) - object (%llu)\n",
1712 			    objsetid, ZFSCTL_INO_SNAPDIRS, object);
1713 
1714 			return (SET_ERROR(EINVAL));
1715 		}
1716 
1717 		if (fid_gen > 1 || setgen != 0) {
1718 			dprintf("snapdir fid: fid_gen (%llu) and setgen "
1719 			    "(%llu)\n", fid_gen, setgen);
1720 			return (SET_ERROR(EINVAL));
1721 		}
1722 
1723 		return (zfsctl_snapdir_vget(sb, objsetid, fid_gen, ipp));
1724 	}
1725 
1726 	ZFS_ENTER(zfsvfs);
1727 	/* A zero fid_gen means we are in the .zfs control directories */
1728 	if (fid_gen == 0 &&
1729 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1730 		*ipp = zfsvfs->z_ctldir;
1731 		ASSERT(*ipp != NULL);
1732 		if (object == ZFSCTL_INO_SNAPDIR) {
1733 			VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp,
1734 			    0, kcred, NULL, NULL) == 0);
1735 		} else {
1736 			igrab(*ipp);
1737 		}
1738 		ZFS_EXIT(zfsvfs);
1739 		return (0);
1740 	}
1741 
1742 	gen_mask = -1ULL >> (64 - 8 * i);
1743 
1744 	dprintf("getting %llu [%llu mask %llx]\n", object, fid_gen, gen_mask);
1745 	if ((err = zfs_zget(zfsvfs, object, &zp))) {
1746 		ZFS_EXIT(zfsvfs);
1747 		return (err);
1748 	}
1749 
1750 	/* Don't export xattr stuff */
1751 	if (zp->z_pflags & ZFS_XATTR) {
1752 		zrele(zp);
1753 		ZFS_EXIT(zfsvfs);
1754 		return (SET_ERROR(ENOENT));
1755 	}
1756 
1757 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1758 	    sizeof (uint64_t));
1759 	zp_gen = zp_gen & gen_mask;
1760 	if (zp_gen == 0)
1761 		zp_gen = 1;
1762 	if ((fid_gen == 0) && (zfsvfs->z_root == object))
1763 		fid_gen = zp_gen;
1764 	if (zp->z_unlinked || zp_gen != fid_gen) {
1765 		dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen,
1766 		    fid_gen);
1767 		zrele(zp);
1768 		ZFS_EXIT(zfsvfs);
1769 		return (SET_ERROR(ENOENT));
1770 	}
1771 
1772 	*ipp = ZTOI(zp);
1773 	if (*ipp)
1774 		zfs_inode_update(ITOZ(*ipp));
1775 
1776 	ZFS_EXIT(zfsvfs);
1777 	return (0);
1778 }
1779 
1780 /*
1781  * Block out VFS ops and close zfsvfs_t
1782  *
1783  * Note, if successful, then we return with the 'z_teardown_lock' and
1784  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
1785  * dataset and objset intact so that they can be atomically handed off during
1786  * a subsequent rollback or recv operation and the resume thereafter.
1787  */
1788 int
1789 zfs_suspend_fs(zfsvfs_t *zfsvfs)
1790 {
1791 	int error;
1792 
1793 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1794 		return (error);
1795 
1796 	return (0);
1797 }
1798 
1799 /*
1800  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
1801  * is an invariant across any of the operations that can be performed while the
1802  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
1803  * are the same: the relevant objset and associated dataset are owned by
1804  * zfsvfs, held, and long held on entry.
1805  */
1806 int
1807 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
1808 {
1809 	int err, err2;
1810 	znode_t *zp;
1811 
1812 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
1813 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1814 
1815 	/*
1816 	 * We already own this, so just update the objset_t, as the one we
1817 	 * had before may have been evicted.
1818 	 */
1819 	objset_t *os;
1820 	VERIFY3P(ds->ds_owner, ==, zfsvfs);
1821 	VERIFY(dsl_dataset_long_held(ds));
1822 	dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds));
1823 	dsl_pool_config_enter(dp, FTAG);
1824 	VERIFY0(dmu_objset_from_ds(ds, &os));
1825 	dsl_pool_config_exit(dp, FTAG);
1826 
1827 	err = zfsvfs_init(zfsvfs, os);
1828 	if (err != 0)
1829 		goto bail;
1830 
1831 	ds->ds_dir->dd_activity_cancelled = B_FALSE;
1832 	VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1833 
1834 	zfs_set_fuid_feature(zfsvfs);
1835 	zfsvfs->z_rollback_time = jiffies;
1836 
1837 	/*
1838 	 * Attempt to re-establish all the active inodes with their
1839 	 * dbufs.  If a zfs_rezget() fails, then we unhash the inode
1840 	 * and mark it stale.  This prevents a collision if a new
1841 	 * inode/object is created which must use the same inode
1842 	 * number.  The stale inode will be be released when the
1843 	 * VFS prunes the dentry holding the remaining references
1844 	 * on the stale inode.
1845 	 */
1846 	mutex_enter(&zfsvfs->z_znodes_lock);
1847 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1848 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1849 		err2 = zfs_rezget(zp);
1850 		if (err2) {
1851 			remove_inode_hash(ZTOI(zp));
1852 			zp->z_is_stale = B_TRUE;
1853 		}
1854 
1855 		/* see comment in zfs_suspend_fs() */
1856 		if (zp->z_suspended) {
1857 			zfs_zrele_async(zp);
1858 			zp->z_suspended = B_FALSE;
1859 		}
1860 	}
1861 	mutex_exit(&zfsvfs->z_znodes_lock);
1862 
1863 	if (!zfs_is_readonly(zfsvfs) && !zfsvfs->z_unmounted) {
1864 		/*
1865 		 * zfs_suspend_fs() could have interrupted freeing
1866 		 * of dnodes. We need to restart this freeing so
1867 		 * that we don't "leak" the space.
1868 		 */
1869 		zfs_unlinked_drain(zfsvfs);
1870 	}
1871 
1872 	/*
1873 	 * Most of the time zfs_suspend_fs is used for changing the contents
1874 	 * of the underlying dataset. ZFS rollback and receive operations
1875 	 * might create files for which negative dentries are present in
1876 	 * the cache. Since walking the dcache would require a lot of GPL-only
1877 	 * code duplication, it's much easier on these rather rare occasions
1878 	 * just to flush the whole dcache for the given dataset/filesystem.
1879 	 */
1880 	shrink_dcache_sb(zfsvfs->z_sb);
1881 
1882 bail:
1883 	if (err != 0)
1884 		zfsvfs->z_unmounted = B_TRUE;
1885 
1886 	/* release the VFS ops */
1887 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
1888 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1889 
1890 	if (err != 0) {
1891 		/*
1892 		 * Since we couldn't setup the sa framework, try to force
1893 		 * unmount this file system.
1894 		 */
1895 		if (zfsvfs->z_os)
1896 			(void) zfs_umount(zfsvfs->z_sb);
1897 	}
1898 	return (err);
1899 }
1900 
1901 /*
1902  * Release VOPs and unmount a suspended filesystem.
1903  */
1904 int
1905 zfs_end_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
1906 {
1907 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
1908 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1909 
1910 	/*
1911 	 * We already own this, so just hold and rele it to update the
1912 	 * objset_t, as the one we had before may have been evicted.
1913 	 */
1914 	objset_t *os;
1915 	VERIFY3P(ds->ds_owner, ==, zfsvfs);
1916 	VERIFY(dsl_dataset_long_held(ds));
1917 	dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds));
1918 	dsl_pool_config_enter(dp, FTAG);
1919 	VERIFY0(dmu_objset_from_ds(ds, &os));
1920 	dsl_pool_config_exit(dp, FTAG);
1921 	zfsvfs->z_os = os;
1922 
1923 	/* release the VOPs */
1924 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
1925 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1926 
1927 	/*
1928 	 * Try to force unmount this file system.
1929 	 */
1930 	(void) zfs_umount(zfsvfs->z_sb);
1931 	zfsvfs->z_unmounted = B_TRUE;
1932 	return (0);
1933 }
1934 
1935 /*
1936  * Automounted snapshots rely on periodic revalidation
1937  * to defer snapshots from being automatically unmounted.
1938  */
1939 
1940 inline void
1941 zfs_exit_fs(zfsvfs_t *zfsvfs)
1942 {
1943 	if (!zfsvfs->z_issnap)
1944 		return;
1945 
1946 	if (time_after(jiffies, zfsvfs->z_snap_defer_time +
1947 	    MAX(zfs_expire_snapshot * HZ / 2, HZ))) {
1948 		zfsvfs->z_snap_defer_time = jiffies;
1949 		zfsctl_snapshot_unmount_delay(zfsvfs->z_os->os_spa,
1950 		    dmu_objset_id(zfsvfs->z_os),
1951 		    zfs_expire_snapshot);
1952 	}
1953 }
1954 
1955 int
1956 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
1957 {
1958 	int error;
1959 	objset_t *os = zfsvfs->z_os;
1960 	dmu_tx_t *tx;
1961 
1962 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1963 		return (SET_ERROR(EINVAL));
1964 
1965 	if (newvers < zfsvfs->z_version)
1966 		return (SET_ERROR(EINVAL));
1967 
1968 	if (zfs_spa_version_map(newvers) >
1969 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
1970 		return (SET_ERROR(ENOTSUP));
1971 
1972 	tx = dmu_tx_create(os);
1973 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
1974 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
1975 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
1976 		    ZFS_SA_ATTRS);
1977 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1978 	}
1979 	error = dmu_tx_assign(tx, TXG_WAIT);
1980 	if (error) {
1981 		dmu_tx_abort(tx);
1982 		return (error);
1983 	}
1984 
1985 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1986 	    8, 1, &newvers, tx);
1987 
1988 	if (error) {
1989 		dmu_tx_commit(tx);
1990 		return (error);
1991 	}
1992 
1993 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
1994 		uint64_t sa_obj;
1995 
1996 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
1997 		    SPA_VERSION_SA);
1998 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1999 		    DMU_OT_NONE, 0, tx);
2000 
2001 		error = zap_add(os, MASTER_NODE_OBJ,
2002 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2003 		ASSERT0(error);
2004 
2005 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
2006 		sa_register_update_callback(os, zfs_sa_upgrade);
2007 	}
2008 
2009 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2010 	    "from %llu to %llu", zfsvfs->z_version, newvers);
2011 
2012 	dmu_tx_commit(tx);
2013 
2014 	zfsvfs->z_version = newvers;
2015 	os->os_version = newvers;
2016 
2017 	zfs_set_fuid_feature(zfsvfs);
2018 
2019 	return (0);
2020 }
2021 
2022 /*
2023  * Read a property stored within the master node.
2024  */
2025 int
2026 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2027 {
2028 	uint64_t *cached_copy = NULL;
2029 
2030 	/*
2031 	 * Figure out where in the objset_t the cached copy would live, if it
2032 	 * is available for the requested property.
2033 	 */
2034 	if (os != NULL) {
2035 		switch (prop) {
2036 		case ZFS_PROP_VERSION:
2037 			cached_copy = &os->os_version;
2038 			break;
2039 		case ZFS_PROP_NORMALIZE:
2040 			cached_copy = &os->os_normalization;
2041 			break;
2042 		case ZFS_PROP_UTF8ONLY:
2043 			cached_copy = &os->os_utf8only;
2044 			break;
2045 		case ZFS_PROP_CASE:
2046 			cached_copy = &os->os_casesensitivity;
2047 			break;
2048 		default:
2049 			break;
2050 		}
2051 	}
2052 	if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
2053 		*value = *cached_copy;
2054 		return (0);
2055 	}
2056 
2057 	/*
2058 	 * If the property wasn't cached, look up the file system's value for
2059 	 * the property. For the version property, we look up a slightly
2060 	 * different string.
2061 	 */
2062 	const char *pname;
2063 	int error = ENOENT;
2064 	if (prop == ZFS_PROP_VERSION)
2065 		pname = ZPL_VERSION_STR;
2066 	else
2067 		pname = zfs_prop_to_name(prop);
2068 
2069 	if (os != NULL) {
2070 		ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2071 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2072 	}
2073 
2074 	if (error == ENOENT) {
2075 		/* No value set, use the default value */
2076 		switch (prop) {
2077 		case ZFS_PROP_VERSION:
2078 			*value = ZPL_VERSION;
2079 			break;
2080 		case ZFS_PROP_NORMALIZE:
2081 		case ZFS_PROP_UTF8ONLY:
2082 			*value = 0;
2083 			break;
2084 		case ZFS_PROP_CASE:
2085 			*value = ZFS_CASE_SENSITIVE;
2086 			break;
2087 		case ZFS_PROP_ACLTYPE:
2088 			*value = ZFS_ACLTYPE_OFF;
2089 			break;
2090 		default:
2091 			return (error);
2092 		}
2093 		error = 0;
2094 	}
2095 
2096 	/*
2097 	 * If one of the methods for getting the property value above worked,
2098 	 * copy it into the objset_t's cache.
2099 	 */
2100 	if (error == 0 && cached_copy != NULL) {
2101 		*cached_copy = *value;
2102 	}
2103 
2104 	return (error);
2105 }
2106 
2107 /*
2108  * Return true if the corresponding vfs's unmounted flag is set.
2109  * Otherwise return false.
2110  * If this function returns true we know VFS unmount has been initiated.
2111  */
2112 boolean_t
2113 zfs_get_vfs_flag_unmounted(objset_t *os)
2114 {
2115 	zfsvfs_t *zfvp;
2116 	boolean_t unmounted = B_FALSE;
2117 
2118 	ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2119 
2120 	mutex_enter(&os->os_user_ptr_lock);
2121 	zfvp = dmu_objset_get_user(os);
2122 	if (zfvp != NULL && zfvp->z_unmounted)
2123 		unmounted = B_TRUE;
2124 	mutex_exit(&os->os_user_ptr_lock);
2125 
2126 	return (unmounted);
2127 }
2128 
2129 /*ARGSUSED*/
2130 void
2131 zfsvfs_update_fromname(const char *oldname, const char *newname)
2132 {
2133 	/*
2134 	 * We don't need to do anything here, the devname is always current by
2135 	 * virtue of zfsvfs->z_sb->s_op->show_devname.
2136 	 */
2137 }
2138 
2139 void
2140 zfs_init(void)
2141 {
2142 	zfsctl_init();
2143 	zfs_znode_init();
2144 	dmu_objset_register_type(DMU_OST_ZFS, zpl_get_file_info);
2145 	register_filesystem(&zpl_fs_type);
2146 }
2147 
2148 void
2149 zfs_fini(void)
2150 {
2151 	/*
2152 	 * we don't use outstanding because zpl_posix_acl_free might add more.
2153 	 */
2154 	taskq_wait(system_delay_taskq);
2155 	taskq_wait(system_taskq);
2156 	unregister_filesystem(&zpl_fs_type);
2157 	zfs_znode_fini();
2158 	zfsctl_fini();
2159 }
2160 
2161 #if defined(_KERNEL)
2162 EXPORT_SYMBOL(zfs_suspend_fs);
2163 EXPORT_SYMBOL(zfs_resume_fs);
2164 EXPORT_SYMBOL(zfs_set_version);
2165 EXPORT_SYMBOL(zfsvfs_create);
2166 EXPORT_SYMBOL(zfsvfs_free);
2167 EXPORT_SYMBOL(zfs_is_readonly);
2168 EXPORT_SYMBOL(zfs_domount);
2169 EXPORT_SYMBOL(zfs_preumount);
2170 EXPORT_SYMBOL(zfs_umount);
2171 EXPORT_SYMBOL(zfs_remount);
2172 EXPORT_SYMBOL(zfs_statvfs);
2173 EXPORT_SYMBOL(zfs_vget);
2174 EXPORT_SYMBOL(zfs_prune);
2175 #endif
2176