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