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