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