1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011 Pawel Jakub Dawidek <[email protected]>.
24  * All rights reserved.
25  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
26  * Copyright (c) 2014 Integros [integros.com]
27  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28  */
29 
30 /* Portions Copyright 2010 Robert Milkowski */
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysmacros.h>
37 #include <sys/kmem.h>
38 #include <sys/acl.h>
39 #include <sys/vnode.h>
40 #include <sys/vfs.h>
41 #include <sys/mntent.h>
42 #include <sys/mount.h>
43 #include <sys/cmn_err.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/zfs_vnops.h>
46 #include <sys/zfs_dir.h>
47 #include <sys/zil.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/dmu.h>
50 #include <sys/dsl_prop.h>
51 #include <sys/dsl_dataset.h>
52 #include <sys/dsl_deleg.h>
53 #include <sys/spa.h>
54 #include <sys/zap.h>
55 #include <sys/sa.h>
56 #include <sys/sa_impl.h>
57 #include <sys/policy.h>
58 #include <sys/atomic.h>
59 #include <sys/zfs_ioctl.h>
60 #include <sys/zfs_ctldir.h>
61 #include <sys/zfs_fuid.h>
62 #include <sys/sunddi.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/dsl_dir.h>
65 #include <sys/spa_boot.h>
66 #include <sys/jail.h>
67 #include <ufs/ufs/quota.h>
68 #include <sys/zfs_quota.h>
69 
70 #include "zfs_comutil.h"
71 
72 #ifndef	MNTK_VMSETSIZE_BUG
73 #define	MNTK_VMSETSIZE_BUG	0
74 #endif
75 #ifndef	MNTK_NOMSYNC
76 #define	MNTK_NOMSYNC	8
77 #endif
78 
79 /* BEGIN CSTYLED */
80 struct mtx zfs_debug_mtx;
81 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
82 
83 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
84 
85 int zfs_super_owner;
86 SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
87     "File system owner can perform privileged operation on his file systems");
88 
89 int zfs_debug_level;
90 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RWTUN, &zfs_debug_level, 0,
91 	"Debug level");
92 
93 SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
94 static int zfs_version_acl = ZFS_ACL_VERSION;
95 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
96     "ZFS_ACL_VERSION");
97 static int zfs_version_spa = SPA_VERSION;
98 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
99     "SPA_VERSION");
100 static int zfs_version_zpl = ZPL_VERSION;
101 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
102     "ZPL_VERSION");
103 /* END CSTYLED */
104 
105 #if __FreeBSD_version >= 1400018
106 static int zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg,
107     bool *mp_busy);
108 #else
109 static int zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg);
110 #endif
111 static int zfs_mount(vfs_t *vfsp);
112 static int zfs_umount(vfs_t *vfsp, int fflag);
113 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
114 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
115 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
116 static int zfs_sync(vfs_t *vfsp, int waitfor);
117 #if __FreeBSD_version >= 1300098
118 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, uint64_t *extflagsp,
119     struct ucred **credanonp, int *numsecflavors, int *secflavors);
120 #else
121 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
122     struct ucred **credanonp, int *numsecflavors, int **secflavors);
123 #endif
124 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp);
125 static void zfs_freevfs(vfs_t *vfsp);
126 
127 struct vfsops zfs_vfsops = {
128 	.vfs_mount =		zfs_mount,
129 	.vfs_unmount =		zfs_umount,
130 #if __FreeBSD_version >= 1300049
131 	.vfs_root =		vfs_cache_root,
132 	.vfs_cachedroot = zfs_root,
133 #else
134 	.vfs_root =		zfs_root,
135 #endif
136 	.vfs_statfs =		zfs_statfs,
137 	.vfs_vget =		zfs_vget,
138 	.vfs_sync =		zfs_sync,
139 	.vfs_checkexp =		zfs_checkexp,
140 	.vfs_fhtovp =		zfs_fhtovp,
141 	.vfs_quotactl =		zfs_quotactl,
142 };
143 
144 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
145 
146 /*
147  * We need to keep a count of active fs's.
148  * This is necessary to prevent our module
149  * from being unloaded after a umount -f
150  */
151 static uint32_t	zfs_active_fs_count = 0;
152 
153 int
zfs_get_temporary_prop(dsl_dataset_t * ds,zfs_prop_t zfs_prop,uint64_t * val,char * setpoint)154 zfs_get_temporary_prop(dsl_dataset_t *ds, zfs_prop_t zfs_prop, uint64_t *val,
155     char *setpoint)
156 {
157 	int error;
158 	zfsvfs_t *zfvp;
159 	vfs_t *vfsp;
160 	objset_t *os;
161 	uint64_t tmp = *val;
162 
163 	error = dmu_objset_from_ds(ds, &os);
164 	if (error != 0)
165 		return (error);
166 
167 	error = getzfsvfs_impl(os, &zfvp);
168 	if (error != 0)
169 		return (error);
170 	if (zfvp == NULL)
171 		return (ENOENT);
172 	vfsp = zfvp->z_vfs;
173 	switch (zfs_prop) {
174 	case ZFS_PROP_ATIME:
175 		if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL))
176 			tmp = 0;
177 		if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL))
178 			tmp = 1;
179 		break;
180 	case ZFS_PROP_DEVICES:
181 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
182 			tmp = 0;
183 		if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL))
184 			tmp = 1;
185 		break;
186 	case ZFS_PROP_EXEC:
187 		if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
188 			tmp = 0;
189 		if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL))
190 			tmp = 1;
191 		break;
192 	case ZFS_PROP_SETUID:
193 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
194 			tmp = 0;
195 		if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL))
196 			tmp = 1;
197 		break;
198 	case ZFS_PROP_READONLY:
199 		if (vfs_optionisset(vfsp, MNTOPT_RW, NULL))
200 			tmp = 0;
201 		if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
202 			tmp = 1;
203 		break;
204 	case ZFS_PROP_XATTR:
205 		if (zfvp->z_flags & ZSB_XATTR)
206 			tmp = zfvp->z_xattr;
207 		break;
208 	case ZFS_PROP_NBMAND:
209 		if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL))
210 			tmp = 0;
211 		if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
212 			tmp = 1;
213 		break;
214 	default:
215 		vfs_unbusy(vfsp);
216 		return (ENOENT);
217 	}
218 
219 	vfs_unbusy(vfsp);
220 	if (tmp != *val) {
221 		(void) strcpy(setpoint, "temporary");
222 		*val = tmp;
223 	}
224 	return (0);
225 }
226 
227 static int
zfs_getquota(zfsvfs_t * zfsvfs,uid_t id,int isgroup,struct dqblk64 * dqp)228 zfs_getquota(zfsvfs_t *zfsvfs, uid_t id, int isgroup, struct dqblk64 *dqp)
229 {
230 	int error = 0;
231 	char buf[32];
232 	uint64_t usedobj, quotaobj;
233 	uint64_t quota, used = 0;
234 	timespec_t now;
235 
236 	usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
237 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
238 
239 	if (quotaobj == 0 || zfsvfs->z_replay) {
240 		error = ENOENT;
241 		goto done;
242 	}
243 	(void) sprintf(buf, "%llx", (longlong_t)id);
244 	if ((error = zap_lookup(zfsvfs->z_os, quotaobj,
245 	    buf, sizeof (quota), 1, &quota)) != 0) {
246 		dprintf("%s(%d): quotaobj lookup failed\n",
247 		    __FUNCTION__, __LINE__);
248 		goto done;
249 	}
250 	/*
251 	 * quota(8) uses bsoftlimit as "quoota", and hardlimit as "limit".
252 	 * So we set them to be the same.
253 	 */
254 	dqp->dqb_bsoftlimit = dqp->dqb_bhardlimit = btodb(quota);
255 	error = zap_lookup(zfsvfs->z_os, usedobj, buf, sizeof (used), 1, &used);
256 	if (error && error != ENOENT) {
257 		dprintf("%s(%d):  usedobj failed; %d\n",
258 		    __FUNCTION__, __LINE__, error);
259 		goto done;
260 	}
261 	dqp->dqb_curblocks = btodb(used);
262 	dqp->dqb_ihardlimit = dqp->dqb_isoftlimit = 0;
263 	vfs_timestamp(&now);
264 	/*
265 	 * Setting this to 0 causes FreeBSD quota(8) to print
266 	 * the number of days since the epoch, which isn't
267 	 * particularly useful.
268 	 */
269 	dqp->dqb_btime = dqp->dqb_itime = now.tv_sec;
270 done:
271 	return (error);
272 }
273 
274 static int
275 #if __FreeBSD_version >= 1400018
zfs_quotactl(vfs_t * vfsp,int cmds,uid_t id,void * arg,bool * mp_busy)276 zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg, bool *mp_busy)
277 #else
278 zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg)
279 #endif
280 {
281 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
282 	struct thread *td;
283 	int cmd, type, error = 0;
284 	int bitsize;
285 	zfs_userquota_prop_t quota_type;
286 	struct dqblk64 dqblk = { 0 };
287 
288 	td = curthread;
289 	cmd = cmds >> SUBCMDSHIFT;
290 	type = cmds & SUBCMDMASK;
291 
292 	ZFS_ENTER(zfsvfs);
293 	if (id == -1) {
294 		switch (type) {
295 		case USRQUOTA:
296 			id = td->td_ucred->cr_ruid;
297 			break;
298 		case GRPQUOTA:
299 			id = td->td_ucred->cr_rgid;
300 			break;
301 		default:
302 			error = EINVAL;
303 #if __FreeBSD_version < 1400018
304 			if (cmd == Q_QUOTAON || cmd == Q_QUOTAOFF)
305 				vfs_unbusy(vfsp);
306 #endif
307 			goto done;
308 		}
309 	}
310 	/*
311 	 * Map BSD type to:
312 	 * ZFS_PROP_USERUSED,
313 	 * ZFS_PROP_USERQUOTA,
314 	 * ZFS_PROP_GROUPUSED,
315 	 * ZFS_PROP_GROUPQUOTA
316 	 */
317 	switch (cmd) {
318 	case Q_SETQUOTA:
319 	case Q_SETQUOTA32:
320 		if (type == USRQUOTA)
321 			quota_type = ZFS_PROP_USERQUOTA;
322 		else if (type == GRPQUOTA)
323 			quota_type = ZFS_PROP_GROUPQUOTA;
324 		else
325 			error = EINVAL;
326 		break;
327 	case Q_GETQUOTA:
328 	case Q_GETQUOTA32:
329 		if (type == USRQUOTA)
330 			quota_type = ZFS_PROP_USERUSED;
331 		else if (type == GRPQUOTA)
332 			quota_type = ZFS_PROP_GROUPUSED;
333 		else
334 			error = EINVAL;
335 		break;
336 	}
337 
338 	/*
339 	 * Depending on the cmd, we may need to get
340 	 * the ruid and domain (see fuidstr_to_sid?),
341 	 * the fuid (how?), or other information.
342 	 * Create fuid using zfs_fuid_create(zfsvfs, id,
343 	 * ZFS_OWNER or ZFS_GROUP, cr, &fuidp)?
344 	 * I think I can use just the id?
345 	 *
346 	 * Look at zfs_id_overquota() to look up a quota.
347 	 * zap_lookup(something, quotaobj, fuidstring,
348 	 *     sizeof (long long), 1, &quota)
349 	 *
350 	 * See zfs_set_userquota() to set a quota.
351 	 */
352 	if ((uint32_t)type >= MAXQUOTAS) {
353 		error = EINVAL;
354 		goto done;
355 	}
356 
357 	switch (cmd) {
358 	case Q_GETQUOTASIZE:
359 		bitsize = 64;
360 		error = copyout(&bitsize, arg, sizeof (int));
361 		break;
362 	case Q_QUOTAON:
363 		// As far as I can tell, you can't turn quotas on or off on zfs
364 		error = 0;
365 #if __FreeBSD_version < 1400018
366 		vfs_unbusy(vfsp);
367 #endif
368 		break;
369 	case Q_QUOTAOFF:
370 		error = ENOTSUP;
371 #if __FreeBSD_version < 1400018
372 		vfs_unbusy(vfsp);
373 #endif
374 		break;
375 	case Q_SETQUOTA:
376 		error = copyin(arg, &dqblk, sizeof (dqblk));
377 		if (error == 0)
378 			error = zfs_set_userquota(zfsvfs, quota_type,
379 			    "", id, dbtob(dqblk.dqb_bhardlimit));
380 		break;
381 	case Q_GETQUOTA:
382 		error = zfs_getquota(zfsvfs, id, type == GRPQUOTA, &dqblk);
383 		if (error == 0)
384 			error = copyout(&dqblk, arg, sizeof (dqblk));
385 		break;
386 	default:
387 		error = EINVAL;
388 		break;
389 	}
390 done:
391 	ZFS_EXIT(zfsvfs);
392 	return (error);
393 }
394 
395 
396 boolean_t
zfs_is_readonly(zfsvfs_t * zfsvfs)397 zfs_is_readonly(zfsvfs_t *zfsvfs)
398 {
399 	return (!!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY));
400 }
401 
402 /*ARGSUSED*/
403 static int
zfs_sync(vfs_t * vfsp,int waitfor)404 zfs_sync(vfs_t *vfsp, int waitfor)
405 {
406 
407 	/*
408 	 * Data integrity is job one.  We don't want a compromised kernel
409 	 * writing to the storage pool, so we never sync during panic.
410 	 */
411 	if (panicstr)
412 		return (0);
413 
414 	/*
415 	 * Ignore the system syncher.  ZFS already commits async data
416 	 * at zfs_txg_timeout intervals.
417 	 */
418 	if (waitfor == MNT_LAZY)
419 		return (0);
420 
421 	if (vfsp != NULL) {
422 		/*
423 		 * Sync a specific filesystem.
424 		 */
425 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
426 		dsl_pool_t *dp;
427 		int error;
428 
429 		error = vfs_stdsync(vfsp, waitfor);
430 		if (error != 0)
431 			return (error);
432 
433 		ZFS_ENTER(zfsvfs);
434 		dp = dmu_objset_pool(zfsvfs->z_os);
435 
436 		/*
437 		 * If the system is shutting down, then skip any
438 		 * filesystems which may exist on a suspended pool.
439 		 */
440 		if (rebooting && spa_suspended(dp->dp_spa)) {
441 			ZFS_EXIT(zfsvfs);
442 			return (0);
443 		}
444 
445 		if (zfsvfs->z_log != NULL)
446 			zil_commit(zfsvfs->z_log, 0);
447 
448 		ZFS_EXIT(zfsvfs);
449 	} else {
450 		/*
451 		 * Sync all ZFS filesystems.  This is what happens when you
452 		 * run sync(8).  Unlike other filesystems, ZFS honors the
453 		 * request by waiting for all pools to commit all dirty data.
454 		 */
455 		spa_sync_allpools();
456 	}
457 
458 	return (0);
459 }
460 
461 static void
atime_changed_cb(void * arg,uint64_t newval)462 atime_changed_cb(void *arg, uint64_t newval)
463 {
464 	zfsvfs_t *zfsvfs = arg;
465 
466 	if (newval == TRUE) {
467 		zfsvfs->z_atime = TRUE;
468 		zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
469 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
470 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
471 	} else {
472 		zfsvfs->z_atime = FALSE;
473 		zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
474 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
475 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
476 	}
477 }
478 
479 static void
xattr_changed_cb(void * arg,uint64_t newval)480 xattr_changed_cb(void *arg, uint64_t newval)
481 {
482 	zfsvfs_t *zfsvfs = arg;
483 
484 	if (newval == ZFS_XATTR_OFF) {
485 		zfsvfs->z_flags &= ~ZSB_XATTR;
486 	} else {
487 		zfsvfs->z_flags |= ZSB_XATTR;
488 
489 		if (newval == ZFS_XATTR_SA)
490 			zfsvfs->z_xattr_sa = B_TRUE;
491 		else
492 			zfsvfs->z_xattr_sa = B_FALSE;
493 	}
494 }
495 
496 static void
blksz_changed_cb(void * arg,uint64_t newval)497 blksz_changed_cb(void *arg, uint64_t newval)
498 {
499 	zfsvfs_t *zfsvfs = arg;
500 	ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
501 	ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
502 	ASSERT(ISP2(newval));
503 
504 	zfsvfs->z_max_blksz = newval;
505 	zfsvfs->z_vfs->mnt_stat.f_iosize = newval;
506 }
507 
508 static void
readonly_changed_cb(void * arg,uint64_t newval)509 readonly_changed_cb(void *arg, uint64_t newval)
510 {
511 	zfsvfs_t *zfsvfs = arg;
512 
513 	if (newval) {
514 		/* XXX locking on vfs_flag? */
515 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
516 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
517 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
518 	} else {
519 		/* XXX locking on vfs_flag? */
520 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
521 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
522 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
523 	}
524 }
525 
526 static void
setuid_changed_cb(void * arg,uint64_t newval)527 setuid_changed_cb(void *arg, uint64_t newval)
528 {
529 	zfsvfs_t *zfsvfs = arg;
530 
531 	if (newval == FALSE) {
532 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
533 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
534 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
535 	} else {
536 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
537 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
538 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
539 	}
540 }
541 
542 static void
exec_changed_cb(void * arg,uint64_t newval)543 exec_changed_cb(void *arg, uint64_t newval)
544 {
545 	zfsvfs_t *zfsvfs = arg;
546 
547 	if (newval == FALSE) {
548 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
549 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
550 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
551 	} else {
552 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
553 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
554 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
555 	}
556 }
557 
558 /*
559  * The nbmand mount option can be changed at mount time.
560  * We can't allow it to be toggled on live file systems or incorrect
561  * behavior may be seen from cifs clients
562  *
563  * This property isn't registered via dsl_prop_register(), but this callback
564  * will be called when a file system is first mounted
565  */
566 static void
nbmand_changed_cb(void * arg,uint64_t newval)567 nbmand_changed_cb(void *arg, uint64_t newval)
568 {
569 	zfsvfs_t *zfsvfs = arg;
570 	if (newval == FALSE) {
571 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
572 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
573 	} else {
574 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
575 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
576 	}
577 }
578 
579 static void
snapdir_changed_cb(void * arg,uint64_t newval)580 snapdir_changed_cb(void *arg, uint64_t newval)
581 {
582 	zfsvfs_t *zfsvfs = arg;
583 
584 	zfsvfs->z_show_ctldir = newval;
585 }
586 
587 static void
vscan_changed_cb(void * arg,uint64_t newval)588 vscan_changed_cb(void *arg, uint64_t newval)
589 {
590 	zfsvfs_t *zfsvfs = arg;
591 
592 	zfsvfs->z_vscan = newval;
593 }
594 
595 static void
acl_mode_changed_cb(void * arg,uint64_t newval)596 acl_mode_changed_cb(void *arg, uint64_t newval)
597 {
598 	zfsvfs_t *zfsvfs = arg;
599 
600 	zfsvfs->z_acl_mode = newval;
601 }
602 
603 static void
acl_inherit_changed_cb(void * arg,uint64_t newval)604 acl_inherit_changed_cb(void *arg, uint64_t newval)
605 {
606 	zfsvfs_t *zfsvfs = arg;
607 
608 	zfsvfs->z_acl_inherit = newval;
609 }
610 
611 static void
acl_type_changed_cb(void * arg,uint64_t newval)612 acl_type_changed_cb(void *arg, uint64_t newval)
613 {
614 	zfsvfs_t *zfsvfs = arg;
615 
616 	zfsvfs->z_acl_type = newval;
617 }
618 
619 static int
zfs_register_callbacks(vfs_t * vfsp)620 zfs_register_callbacks(vfs_t *vfsp)
621 {
622 	struct dsl_dataset *ds = NULL;
623 	objset_t *os = NULL;
624 	zfsvfs_t *zfsvfs = NULL;
625 	uint64_t nbmand;
626 	boolean_t readonly = B_FALSE;
627 	boolean_t do_readonly = B_FALSE;
628 	boolean_t setuid = B_FALSE;
629 	boolean_t do_setuid = B_FALSE;
630 	boolean_t exec = B_FALSE;
631 	boolean_t do_exec = B_FALSE;
632 	boolean_t xattr = B_FALSE;
633 	boolean_t atime = B_FALSE;
634 	boolean_t do_atime = B_FALSE;
635 	boolean_t do_xattr = B_FALSE;
636 	int error = 0;
637 
638 	ASSERT3P(vfsp, !=, NULL);
639 	zfsvfs = vfsp->vfs_data;
640 	ASSERT3P(zfsvfs, !=, NULL);
641 	os = zfsvfs->z_os;
642 
643 	/*
644 	 * This function can be called for a snapshot when we update snapshot's
645 	 * mount point, which isn't really supported.
646 	 */
647 	if (dmu_objset_is_snapshot(os))
648 		return (EOPNOTSUPP);
649 
650 	/*
651 	 * The act of registering our callbacks will destroy any mount
652 	 * options we may have.  In order to enable temporary overrides
653 	 * of mount options, we stash away the current values and
654 	 * restore them after we register the callbacks.
655 	 */
656 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
657 	    !spa_writeable(dmu_objset_spa(os))) {
658 		readonly = B_TRUE;
659 		do_readonly = B_TRUE;
660 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
661 		readonly = B_FALSE;
662 		do_readonly = B_TRUE;
663 	}
664 	if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
665 		setuid = B_FALSE;
666 		do_setuid = B_TRUE;
667 	} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
668 		setuid = B_TRUE;
669 		do_setuid = B_TRUE;
670 	}
671 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
672 		exec = B_FALSE;
673 		do_exec = B_TRUE;
674 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
675 		exec = B_TRUE;
676 		do_exec = B_TRUE;
677 	}
678 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
679 		zfsvfs->z_xattr = xattr = ZFS_XATTR_OFF;
680 		do_xattr = B_TRUE;
681 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
682 		zfsvfs->z_xattr = xattr = ZFS_XATTR_DIR;
683 		do_xattr = B_TRUE;
684 	} else if (vfs_optionisset(vfsp, MNTOPT_DIRXATTR, NULL)) {
685 		zfsvfs->z_xattr = xattr = ZFS_XATTR_DIR;
686 		do_xattr = B_TRUE;
687 	} else if (vfs_optionisset(vfsp, MNTOPT_SAXATTR, NULL)) {
688 		zfsvfs->z_xattr = xattr = ZFS_XATTR_SA;
689 		do_xattr = B_TRUE;
690 	}
691 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
692 		atime = B_FALSE;
693 		do_atime = B_TRUE;
694 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
695 		atime = B_TRUE;
696 		do_atime = B_TRUE;
697 	}
698 
699 	/*
700 	 * We need to enter pool configuration here, so that we can use
701 	 * dsl_prop_get_int_ds() to handle the special nbmand property below.
702 	 * dsl_prop_get_integer() can not be used, because it has to acquire
703 	 * spa_namespace_lock and we can not do that because we already hold
704 	 * z_teardown_lock.  The problem is that spa_write_cachefile() is called
705 	 * with spa_namespace_lock held and the function calls ZFS vnode
706 	 * operations to write the cache file and thus z_teardown_lock is
707 	 * acquired after spa_namespace_lock.
708 	 */
709 	ds = dmu_objset_ds(os);
710 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
711 
712 	/*
713 	 * nbmand is a special property.  It can only be changed at
714 	 * mount time.
715 	 *
716 	 * This is weird, but it is documented to only be changeable
717 	 * at mount time.
718 	 */
719 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
720 		nbmand = B_FALSE;
721 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
722 		nbmand = B_TRUE;
723 	} else if ((error = dsl_prop_get_int_ds(ds, "nbmand", &nbmand) != 0)) {
724 		dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
725 		return (error);
726 	}
727 
728 	/*
729 	 * Register property callbacks.
730 	 *
731 	 * It would probably be fine to just check for i/o error from
732 	 * the first prop_register(), but I guess I like to go
733 	 * overboard...
734 	 */
735 	error = dsl_prop_register(ds,
736 	    zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
737 	error = error ? error : dsl_prop_register(ds,
738 	    zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
739 	error = error ? error : dsl_prop_register(ds,
740 	    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
741 	error = error ? error : dsl_prop_register(ds,
742 	    zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
743 	error = error ? error : dsl_prop_register(ds,
744 	    zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
745 	error = error ? error : dsl_prop_register(ds,
746 	    zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
747 	error = error ? error : dsl_prop_register(ds,
748 	    zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
749 	error = error ? error : dsl_prop_register(ds,
750 	    zfs_prop_to_name(ZFS_PROP_ACLTYPE), acl_type_changed_cb, zfsvfs);
751 	error = error ? error : dsl_prop_register(ds,
752 	    zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
753 	error = error ? error : dsl_prop_register(ds,
754 	    zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
755 	    zfsvfs);
756 	error = error ? error : dsl_prop_register(ds,
757 	    zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
758 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
759 	if (error)
760 		goto unregister;
761 
762 	/*
763 	 * Invoke our callbacks to restore temporary mount options.
764 	 */
765 	if (do_readonly)
766 		readonly_changed_cb(zfsvfs, readonly);
767 	if (do_setuid)
768 		setuid_changed_cb(zfsvfs, setuid);
769 	if (do_exec)
770 		exec_changed_cb(zfsvfs, exec);
771 	if (do_xattr)
772 		xattr_changed_cb(zfsvfs, xattr);
773 	if (do_atime)
774 		atime_changed_cb(zfsvfs, atime);
775 
776 	nbmand_changed_cb(zfsvfs, nbmand);
777 
778 	return (0);
779 
780 unregister:
781 	dsl_prop_unregister_all(ds, zfsvfs);
782 	return (error);
783 }
784 
785 /*
786  * Associate this zfsvfs with the given objset, which must be owned.
787  * This will cache a bunch of on-disk state from the objset in the
788  * zfsvfs.
789  */
790 static int
zfsvfs_init(zfsvfs_t * zfsvfs,objset_t * os)791 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
792 {
793 	int error;
794 	uint64_t val;
795 
796 	zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
797 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
798 	zfsvfs->z_os = os;
799 
800 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
801 	if (error != 0)
802 		return (error);
803 	if (zfsvfs->z_version >
804 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
805 		(void) printf("Can't mount a version %lld file system "
806 		    "on a version %lld pool\n. Pool must be upgraded to mount "
807 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
808 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
809 		return (SET_ERROR(ENOTSUP));
810 	}
811 	error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
812 	if (error != 0)
813 		return (error);
814 	zfsvfs->z_norm = (int)val;
815 
816 	error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
817 	if (error != 0)
818 		return (error);
819 	zfsvfs->z_utf8 = (val != 0);
820 
821 	error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
822 	if (error != 0)
823 		return (error);
824 	zfsvfs->z_case = (uint_t)val;
825 
826 	error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &val);
827 	if (error != 0)
828 		return (error);
829 	zfsvfs->z_acl_type = (uint_t)val;
830 
831 	/*
832 	 * Fold case on file systems that are always or sometimes case
833 	 * insensitive.
834 	 */
835 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
836 	    zfsvfs->z_case == ZFS_CASE_MIXED)
837 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
838 
839 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
840 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
841 
842 	uint64_t sa_obj = 0;
843 	if (zfsvfs->z_use_sa) {
844 		/* should either have both of these objects or none */
845 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
846 		    &sa_obj);
847 		if (error != 0)
848 			return (error);
849 
850 		error = zfs_get_zplprop(os, ZFS_PROP_XATTR, &val);
851 		if (error == 0 && val == ZFS_XATTR_SA)
852 			zfsvfs->z_xattr_sa = B_TRUE;
853 	}
854 
855 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
856 	    &zfsvfs->z_attr_table);
857 	if (error != 0)
858 		return (error);
859 
860 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
861 		sa_register_update_callback(os, zfs_sa_upgrade);
862 
863 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
864 	    &zfsvfs->z_root);
865 	if (error != 0)
866 		return (error);
867 	ASSERT3U(zfsvfs->z_root, !=, 0);
868 
869 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
870 	    &zfsvfs->z_unlinkedobj);
871 	if (error != 0)
872 		return (error);
873 
874 	error = zap_lookup(os, MASTER_NODE_OBJ,
875 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
876 	    8, 1, &zfsvfs->z_userquota_obj);
877 	if (error == ENOENT)
878 		zfsvfs->z_userquota_obj = 0;
879 	else if (error != 0)
880 		return (error);
881 
882 	error = zap_lookup(os, MASTER_NODE_OBJ,
883 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
884 	    8, 1, &zfsvfs->z_groupquota_obj);
885 	if (error == ENOENT)
886 		zfsvfs->z_groupquota_obj = 0;
887 	else if (error != 0)
888 		return (error);
889 
890 	error = zap_lookup(os, MASTER_NODE_OBJ,
891 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA],
892 	    8, 1, &zfsvfs->z_projectquota_obj);
893 	if (error == ENOENT)
894 		zfsvfs->z_projectquota_obj = 0;
895 	else if (error != 0)
896 		return (error);
897 
898 	error = zap_lookup(os, MASTER_NODE_OBJ,
899 	    zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA],
900 	    8, 1, &zfsvfs->z_userobjquota_obj);
901 	if (error == ENOENT)
902 		zfsvfs->z_userobjquota_obj = 0;
903 	else if (error != 0)
904 		return (error);
905 
906 	error = zap_lookup(os, MASTER_NODE_OBJ,
907 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA],
908 	    8, 1, &zfsvfs->z_groupobjquota_obj);
909 	if (error == ENOENT)
910 		zfsvfs->z_groupobjquota_obj = 0;
911 	else if (error != 0)
912 		return (error);
913 
914 	error = zap_lookup(os, MASTER_NODE_OBJ,
915 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA],
916 	    8, 1, &zfsvfs->z_projectobjquota_obj);
917 	if (error == ENOENT)
918 		zfsvfs->z_projectobjquota_obj = 0;
919 	else if (error != 0)
920 		return (error);
921 
922 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
923 	    &zfsvfs->z_fuid_obj);
924 	if (error == ENOENT)
925 		zfsvfs->z_fuid_obj = 0;
926 	else if (error != 0)
927 		return (error);
928 
929 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
930 	    &zfsvfs->z_shares_dir);
931 	if (error == ENOENT)
932 		zfsvfs->z_shares_dir = 0;
933 	else if (error != 0)
934 		return (error);
935 
936 	/*
937 	 * Only use the name cache if we are looking for a
938 	 * name on a file system that does not require normalization
939 	 * or case folding.  We can also look there if we happen to be
940 	 * on a non-normalizing, mixed sensitivity file system IF we
941 	 * are looking for the exact name (which is always the case on
942 	 * FreeBSD).
943 	 */
944 	zfsvfs->z_use_namecache = !zfsvfs->z_norm ||
945 	    ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
946 	    !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER));
947 
948 	return (0);
949 }
950 
951 taskq_t *zfsvfs_taskq;
952 
953 static void
zfsvfs_task_unlinked_drain(void * context,int pending __unused)954 zfsvfs_task_unlinked_drain(void *context, int pending __unused)
955 {
956 
957 	zfs_unlinked_drain((zfsvfs_t *)context);
958 }
959 
960 int
zfsvfs_create(const char * osname,boolean_t readonly,zfsvfs_t ** zfvp)961 zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp)
962 {
963 	objset_t *os;
964 	zfsvfs_t *zfsvfs;
965 	int error;
966 	boolean_t ro = (readonly || (strchr(osname, '@') != NULL));
967 
968 	/*
969 	 * XXX: Fix struct statfs so this isn't necessary!
970 	 *
971 	 * The 'osname' is used as the filesystem's special node, which means
972 	 * it must fit in statfs.f_mntfromname, or else it can't be
973 	 * enumerated, so libzfs_mnttab_find() returns NULL, which causes
974 	 * 'zfs unmount' to think it's not mounted when it is.
975 	 */
976 	if (strlen(osname) >= MNAMELEN)
977 		return (SET_ERROR(ENAMETOOLONG));
978 
979 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
980 
981 	error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs,
982 	    &os);
983 	if (error != 0) {
984 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
985 		return (error);
986 	}
987 
988 	error = zfsvfs_create_impl(zfvp, zfsvfs, os);
989 
990 	return (error);
991 }
992 
993 
994 int
zfsvfs_create_impl(zfsvfs_t ** zfvp,zfsvfs_t * zfsvfs,objset_t * os)995 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
996 {
997 	int error;
998 
999 	zfsvfs->z_vfs = NULL;
1000 	zfsvfs->z_parent = zfsvfs;
1001 
1002 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1003 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
1004 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1005 	    offsetof(znode_t, z_link_node));
1006 	TASK_INIT(&zfsvfs->z_unlinked_drain_task, 0,
1007 	    zfsvfs_task_unlinked_drain, zfsvfs);
1008 	ZFS_TEARDOWN_INIT(zfsvfs);
1009 	ZFS_TEARDOWN_INACTIVE_INIT(zfsvfs);
1010 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
1011 	for (int i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1012 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1013 
1014 	error = zfsvfs_init(zfsvfs, os);
1015 	if (error != 0) {
1016 		dmu_objset_disown(os, B_TRUE, zfsvfs);
1017 		*zfvp = NULL;
1018 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
1019 		return (error);
1020 	}
1021 
1022 	*zfvp = zfsvfs;
1023 	return (0);
1024 }
1025 
1026 static int
zfsvfs_setup(zfsvfs_t * zfsvfs,boolean_t mounting)1027 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1028 {
1029 	int error;
1030 
1031 	/*
1032 	 * Check for a bad on-disk format version now since we
1033 	 * lied about owning the dataset readonly before.
1034 	 */
1035 	if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) &&
1036 	    dmu_objset_incompatible_encryption_version(zfsvfs->z_os))
1037 		return (SET_ERROR(EROFS));
1038 
1039 	error = zfs_register_callbacks(zfsvfs->z_vfs);
1040 	if (error)
1041 		return (error);
1042 
1043 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1044 
1045 	/*
1046 	 * If we are not mounting (ie: online recv), then we don't
1047 	 * have to worry about replaying the log as we blocked all
1048 	 * operations out since we closed the ZIL.
1049 	 */
1050 	if (mounting) {
1051 		boolean_t readonly;
1052 
1053 		ASSERT3P(zfsvfs->z_kstat.dk_kstats, ==, NULL);
1054 		dataset_kstats_create(&zfsvfs->z_kstat, zfsvfs->z_os);
1055 
1056 		/*
1057 		 * During replay we remove the read only flag to
1058 		 * allow replays to succeed.
1059 		 */
1060 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1061 		if (readonly != 0) {
1062 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1063 		} else {
1064 			dsl_dir_t *dd;
1065 			zap_stats_t zs;
1066 
1067 			if (zap_get_stats(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
1068 			    &zs) == 0) {
1069 				dataset_kstats_update_nunlinks_kstat(
1070 				    &zfsvfs->z_kstat, zs.zs_num_entries);
1071 				dprintf_ds(zfsvfs->z_os->os_dsl_dataset,
1072 				    "num_entries in unlinked set: %llu",
1073 				    (u_longlong_t)zs.zs_num_entries);
1074 			}
1075 
1076 			zfs_unlinked_drain(zfsvfs);
1077 			dd = zfsvfs->z_os->os_dsl_dataset->ds_dir;
1078 			dd->dd_activity_cancelled = B_FALSE;
1079 		}
1080 
1081 		/*
1082 		 * Parse and replay the intent log.
1083 		 *
1084 		 * Because of ziltest, this must be done after
1085 		 * zfs_unlinked_drain().  (Further note: ziltest
1086 		 * doesn't use readonly mounts, where
1087 		 * zfs_unlinked_drain() isn't called.)  This is because
1088 		 * ziltest causes spa_sync() to think it's committed,
1089 		 * but actually it is not, so the intent log contains
1090 		 * many txg's worth of changes.
1091 		 *
1092 		 * In particular, if object N is in the unlinked set in
1093 		 * the last txg to actually sync, then it could be
1094 		 * actually freed in a later txg and then reallocated
1095 		 * in a yet later txg.  This would write a "create
1096 		 * object N" record to the intent log.  Normally, this
1097 		 * would be fine because the spa_sync() would have
1098 		 * written out the fact that object N is free, before
1099 		 * we could write the "create object N" intent log
1100 		 * record.
1101 		 *
1102 		 * But when we are in ziltest mode, we advance the "open
1103 		 * txg" without actually spa_sync()-ing the changes to
1104 		 * disk.  So we would see that object N is still
1105 		 * allocated and in the unlinked set, and there is an
1106 		 * intent log record saying to allocate it.
1107 		 */
1108 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1109 			if (zil_replay_disable) {
1110 				zil_destroy(zfsvfs->z_log, B_FALSE);
1111 			} else {
1112 				boolean_t use_nc = zfsvfs->z_use_namecache;
1113 				zfsvfs->z_use_namecache = B_FALSE;
1114 				zfsvfs->z_replay = B_TRUE;
1115 				zil_replay(zfsvfs->z_os, zfsvfs,
1116 				    zfs_replay_vector);
1117 				zfsvfs->z_replay = B_FALSE;
1118 				zfsvfs->z_use_namecache = use_nc;
1119 			}
1120 		}
1121 
1122 		/* restore readonly bit */
1123 		if (readonly != 0)
1124 			zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
1125 	}
1126 
1127 	/*
1128 	 * Set the objset user_ptr to track its zfsvfs.
1129 	 */
1130 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1131 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1132 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1133 
1134 	return (0);
1135 }
1136 
1137 void
zfsvfs_free(zfsvfs_t * zfsvfs)1138 zfsvfs_free(zfsvfs_t *zfsvfs)
1139 {
1140 	int i;
1141 
1142 	zfs_fuid_destroy(zfsvfs);
1143 
1144 	mutex_destroy(&zfsvfs->z_znodes_lock);
1145 	mutex_destroy(&zfsvfs->z_lock);
1146 	ASSERT3U(zfsvfs->z_nr_znodes, ==, 0);
1147 	list_destroy(&zfsvfs->z_all_znodes);
1148 	ZFS_TEARDOWN_DESTROY(zfsvfs);
1149 	ZFS_TEARDOWN_INACTIVE_DESTROY(zfsvfs);
1150 	rw_destroy(&zfsvfs->z_fuid_lock);
1151 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1152 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1153 	dataset_kstats_destroy(&zfsvfs->z_kstat);
1154 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1155 }
1156 
1157 static void
zfs_set_fuid_feature(zfsvfs_t * zfsvfs)1158 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1159 {
1160 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1161 	if (zfsvfs->z_vfs) {
1162 		if (zfsvfs->z_use_fuids) {
1163 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1164 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1165 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1166 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1167 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1168 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1169 		} else {
1170 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1171 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1172 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1173 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1174 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1175 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1176 		}
1177 	}
1178 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1179 }
1180 
1181 static int
zfs_domount(vfs_t * vfsp,char * osname)1182 zfs_domount(vfs_t *vfsp, char *osname)
1183 {
1184 	uint64_t recordsize, fsid_guid;
1185 	int error = 0;
1186 	zfsvfs_t *zfsvfs;
1187 
1188 	ASSERT3P(vfsp, !=, NULL);
1189 	ASSERT3P(osname, !=, NULL);
1190 
1191 	error = zfsvfs_create(osname, vfsp->mnt_flag & MNT_RDONLY, &zfsvfs);
1192 	if (error)
1193 		return (error);
1194 	zfsvfs->z_vfs = vfsp;
1195 
1196 	if ((error = dsl_prop_get_integer(osname,
1197 	    "recordsize", &recordsize, NULL)))
1198 		goto out;
1199 	zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE;
1200 	zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize;
1201 
1202 	vfsp->vfs_data = zfsvfs;
1203 	vfsp->mnt_flag |= MNT_LOCAL;
1204 	vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
1205 	vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
1206 	vfsp->mnt_kern_flag |= MNTK_EXTENDED_SHARED;
1207 	/*
1208 	 * This can cause a loss of coherence between ARC and page cache
1209 	 * on ZoF - unclear if the problem is in FreeBSD or ZoF
1210 	 */
1211 	vfsp->mnt_kern_flag |= MNTK_NO_IOPF;	/* vn_io_fault can be used */
1212 	vfsp->mnt_kern_flag |= MNTK_NOMSYNC;
1213 	vfsp->mnt_kern_flag |= MNTK_VMSETSIZE_BUG;
1214 
1215 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
1216 	vfsp->mnt_kern_flag |= MNTK_FPLOOKUP;
1217 #endif
1218 	/*
1219 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
1220 	 * separates our fsid from any other filesystem types, and a
1221 	 * 56-bit objset unique ID.  The objset unique ID is unique to
1222 	 * all objsets open on this system, provided by unique_create().
1223 	 * The 8-bit fs type must be put in the low bits of fsid[1]
1224 	 * because that's where other Solaris filesystems put it.
1225 	 */
1226 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1227 	ASSERT3U((fsid_guid & ~((1ULL << 56) - 1)), ==, 0);
1228 	vfsp->vfs_fsid.val[0] = fsid_guid;
1229 	vfsp->vfs_fsid.val[1] = ((fsid_guid >> 32) << 8) |
1230 	    (vfsp->mnt_vfc->vfc_typenum & 0xFF);
1231 
1232 	/*
1233 	 * Set features for file system.
1234 	 */
1235 	zfs_set_fuid_feature(zfsvfs);
1236 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1237 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1238 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1239 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1240 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1241 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1242 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1243 	}
1244 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1245 
1246 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1247 		uint64_t pval;
1248 
1249 		atime_changed_cb(zfsvfs, B_FALSE);
1250 		readonly_changed_cb(zfsvfs, B_TRUE);
1251 		if ((error = dsl_prop_get_integer(osname,
1252 		    "xattr", &pval, NULL)))
1253 			goto out;
1254 		xattr_changed_cb(zfsvfs, pval);
1255 		if ((error = dsl_prop_get_integer(osname,
1256 		    "acltype", &pval, NULL)))
1257 			goto out;
1258 		acl_type_changed_cb(zfsvfs, pval);
1259 		zfsvfs->z_issnap = B_TRUE;
1260 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1261 
1262 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1263 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1264 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1265 	} else {
1266 		if ((error = zfsvfs_setup(zfsvfs, B_TRUE)))
1267 			goto out;
1268 	}
1269 
1270 	vfs_mountedfrom(vfsp, osname);
1271 
1272 	if (!zfsvfs->z_issnap)
1273 		zfsctl_create(zfsvfs);
1274 out:
1275 	if (error) {
1276 		dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1277 		zfsvfs_free(zfsvfs);
1278 	} else {
1279 		atomic_inc_32(&zfs_active_fs_count);
1280 	}
1281 
1282 	return (error);
1283 }
1284 
1285 static void
zfs_unregister_callbacks(zfsvfs_t * zfsvfs)1286 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1287 {
1288 	objset_t *os = zfsvfs->z_os;
1289 
1290 	if (!dmu_objset_is_snapshot(os))
1291 		dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1292 }
1293 
1294 static int
getpoolname(const char * osname,char * poolname)1295 getpoolname(const char *osname, char *poolname)
1296 {
1297 	char *p;
1298 
1299 	p = strchr(osname, '/');
1300 	if (p == NULL) {
1301 		if (strlen(osname) >= MAXNAMELEN)
1302 			return (ENAMETOOLONG);
1303 		(void) strcpy(poolname, osname);
1304 	} else {
1305 		if (p - osname >= MAXNAMELEN)
1306 			return (ENAMETOOLONG);
1307 		(void) strncpy(poolname, osname, p - osname);
1308 		poolname[p - osname] = '\0';
1309 	}
1310 	return (0);
1311 }
1312 
1313 static void
fetch_osname_options(char * name,bool * checkpointrewind)1314 fetch_osname_options(char *name, bool *checkpointrewind)
1315 {
1316 
1317 	if (name[0] == '!') {
1318 		*checkpointrewind = true;
1319 		memmove(name, name + 1, strlen(name));
1320 	} else {
1321 		*checkpointrewind = false;
1322 	}
1323 }
1324 
1325 /*ARGSUSED*/
1326 static int
zfs_mount(vfs_t * vfsp)1327 zfs_mount(vfs_t *vfsp)
1328 {
1329 	kthread_t	*td = curthread;
1330 	vnode_t		*mvp = vfsp->mnt_vnodecovered;
1331 	cred_t		*cr = td->td_ucred;
1332 	char		*osname;
1333 	int		error = 0;
1334 	int		canwrite;
1335 	bool		checkpointrewind;
1336 
1337 	if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
1338 		return (SET_ERROR(EINVAL));
1339 
1340 	/*
1341 	 * If full-owner-access is enabled and delegated administration is
1342 	 * turned on, we must set nosuid.
1343 	 */
1344 	if (zfs_super_owner &&
1345 	    dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
1346 		secpolicy_fs_mount_clearopts(cr, vfsp);
1347 	}
1348 
1349 	fetch_osname_options(osname, &checkpointrewind);
1350 
1351 	/*
1352 	 * Check for mount privilege?
1353 	 *
1354 	 * If we don't have privilege then see if
1355 	 * we have local permission to allow it
1356 	 */
1357 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1358 	if (error) {
1359 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != 0)
1360 			goto out;
1361 
1362 		if (!(vfsp->vfs_flag & MS_REMOUNT)) {
1363 			vattr_t		vattr;
1364 
1365 			/*
1366 			 * Make sure user is the owner of the mount point
1367 			 * or has sufficient privileges.
1368 			 */
1369 
1370 			vattr.va_mask = AT_UID;
1371 
1372 			vn_lock(mvp, LK_SHARED | LK_RETRY);
1373 			if (VOP_GETATTR(mvp, &vattr, cr)) {
1374 				VOP_UNLOCK1(mvp);
1375 				goto out;
1376 			}
1377 
1378 			if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
1379 			    VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
1380 				VOP_UNLOCK1(mvp);
1381 				goto out;
1382 			}
1383 			VOP_UNLOCK1(mvp);
1384 		}
1385 
1386 		secpolicy_fs_mount_clearopts(cr, vfsp);
1387 	}
1388 
1389 	/*
1390 	 * Refuse to mount a filesystem if we are in a local zone and the
1391 	 * dataset is not visible.
1392 	 */
1393 	if (!INGLOBALZONE(curproc) &&
1394 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1395 		error = SET_ERROR(EPERM);
1396 		goto out;
1397 	}
1398 
1399 	vfsp->vfs_flag |= MNT_NFS4ACLS;
1400 
1401 	/*
1402 	 * When doing a remount, we simply refresh our temporary properties
1403 	 * according to those options set in the current VFS options.
1404 	 */
1405 	if (vfsp->vfs_flag & MS_REMOUNT) {
1406 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
1407 
1408 		/*
1409 		 * Refresh mount options with z_teardown_lock blocking I/O while
1410 		 * the filesystem is in an inconsistent state.
1411 		 * The lock also serializes this code with filesystem
1412 		 * manipulations between entry to zfs_suspend_fs() and return
1413 		 * from zfs_resume_fs().
1414 		 */
1415 		ZFS_TEARDOWN_ENTER_WRITE(zfsvfs, FTAG);
1416 		zfs_unregister_callbacks(zfsvfs);
1417 		error = zfs_register_callbacks(vfsp);
1418 		ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1419 		goto out;
1420 	}
1421 
1422 	/* Initial root mount: try hard to import the requested root pool. */
1423 	if ((vfsp->vfs_flag & MNT_ROOTFS) != 0 &&
1424 	    (vfsp->vfs_flag & MNT_UPDATE) == 0) {
1425 		char pname[MAXNAMELEN];
1426 
1427 		error = getpoolname(osname, pname);
1428 		if (error == 0)
1429 			error = spa_import_rootpool(pname, checkpointrewind);
1430 		if (error)
1431 			goto out;
1432 	}
1433 	DROP_GIANT();
1434 	error = zfs_domount(vfsp, osname);
1435 	PICKUP_GIANT();
1436 
1437 out:
1438 	return (error);
1439 }
1440 
1441 static int
zfs_statfs(vfs_t * vfsp,struct statfs * statp)1442 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
1443 {
1444 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1445 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1446 
1447 	statp->f_version = STATFS_VERSION;
1448 
1449 	ZFS_ENTER(zfsvfs);
1450 
1451 	dmu_objset_space(zfsvfs->z_os,
1452 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1453 
1454 	/*
1455 	 * The underlying storage pool actually uses multiple block sizes.
1456 	 * We report the fragsize as the smallest block size we support,
1457 	 * and we report our blocksize as the filesystem's maximum blocksize.
1458 	 */
1459 	statp->f_bsize = SPA_MINBLOCKSIZE;
1460 	statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize;
1461 
1462 	/*
1463 	 * The following report "total" blocks of various kinds in the
1464 	 * file system, but reported in terms of f_frsize - the
1465 	 * "fragment" size.
1466 	 */
1467 
1468 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1469 	statp->f_bfree = availbytes / statp->f_bsize;
1470 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1471 
1472 	/*
1473 	 * statvfs() should really be called statufs(), because it assumes
1474 	 * static metadata.  ZFS doesn't preallocate files, so the best
1475 	 * we can do is report the max that could possibly fit in f_files,
1476 	 * and that minus the number actually used in f_ffree.
1477 	 * For f_ffree, report the smaller of the number of object available
1478 	 * and the number of blocks (each object will take at least a block).
1479 	 */
1480 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1481 	statp->f_files = statp->f_ffree + usedobjs;
1482 
1483 	/*
1484 	 * We're a zfs filesystem.
1485 	 */
1486 	strlcpy(statp->f_fstypename, "zfs",
1487 	    sizeof (statp->f_fstypename));
1488 
1489 	strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
1490 	    sizeof (statp->f_mntfromname));
1491 	strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
1492 	    sizeof (statp->f_mntonname));
1493 
1494 	statp->f_namemax = MAXNAMELEN - 1;
1495 
1496 	ZFS_EXIT(zfsvfs);
1497 	return (0);
1498 }
1499 
1500 static int
zfs_root(vfs_t * vfsp,int flags,vnode_t ** vpp)1501 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
1502 {
1503 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1504 	znode_t *rootzp;
1505 	int error;
1506 
1507 	ZFS_ENTER(zfsvfs);
1508 
1509 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1510 	if (error == 0)
1511 		*vpp = ZTOV(rootzp);
1512 
1513 	ZFS_EXIT(zfsvfs);
1514 
1515 	if (error == 0) {
1516 		error = vn_lock(*vpp, flags);
1517 		if (error != 0) {
1518 			VN_RELE(*vpp);
1519 			*vpp = NULL;
1520 		}
1521 	}
1522 	return (error);
1523 }
1524 
1525 /*
1526  * Teardown the zfsvfs::z_os.
1527  *
1528  * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1529  * and 'z_teardown_inactive_lock' held.
1530  */
1531 static int
zfsvfs_teardown(zfsvfs_t * zfsvfs,boolean_t unmounting)1532 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1533 {
1534 	znode_t	*zp;
1535 	dsl_dir_t *dd;
1536 
1537 	/*
1538 	 * If someone has not already unmounted this file system,
1539 	 * drain the zrele_taskq to ensure all active references to the
1540 	 * zfsvfs_t have been handled only then can it be safely destroyed.
1541 	 */
1542 	if (zfsvfs->z_os) {
1543 		/*
1544 		 * If we're unmounting we have to wait for the list to
1545 		 * drain completely.
1546 		 *
1547 		 * If we're not unmounting there's no guarantee the list
1548 		 * will drain completely, but zreles run from the taskq
1549 		 * may add the parents of dir-based xattrs to the taskq
1550 		 * so we want to wait for these.
1551 		 *
1552 		 * We can safely read z_nr_znodes without locking because the
1553 		 * VFS has already blocked operations which add to the
1554 		 * z_all_znodes list and thus increment z_nr_znodes.
1555 		 */
1556 		int round = 0;
1557 		while (zfsvfs->z_nr_znodes > 0) {
1558 			taskq_wait_outstanding(dsl_pool_zrele_taskq(
1559 			    dmu_objset_pool(zfsvfs->z_os)), 0);
1560 			if (++round > 1 && !unmounting)
1561 				break;
1562 		}
1563 	}
1564 	ZFS_TEARDOWN_ENTER_WRITE(zfsvfs, FTAG);
1565 
1566 	if (!unmounting) {
1567 		/*
1568 		 * We purge the parent filesystem's vfsp as the parent
1569 		 * filesystem and all of its snapshots have their vnode's
1570 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1571 		 * 'z_parent' is self referential for non-snapshots.
1572 		 */
1573 #ifdef FREEBSD_NAMECACHE
1574 #if __FreeBSD_version >= 1300117
1575 		cache_purgevfs(zfsvfs->z_parent->z_vfs);
1576 #else
1577 		cache_purgevfs(zfsvfs->z_parent->z_vfs, true);
1578 #endif
1579 #endif
1580 	}
1581 
1582 	/*
1583 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1584 	 * threads are blocked as zil_close can call zfs_inactive.
1585 	 */
1586 	if (zfsvfs->z_log) {
1587 		zil_close(zfsvfs->z_log);
1588 		zfsvfs->z_log = NULL;
1589 	}
1590 
1591 	ZFS_TEARDOWN_INACTIVE_ENTER_WRITE(zfsvfs);
1592 
1593 	/*
1594 	 * If we are not unmounting (ie: online recv) and someone already
1595 	 * unmounted this file system while we were doing the switcheroo,
1596 	 * or a reopen of z_os failed then just bail out now.
1597 	 */
1598 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1599 		ZFS_TEARDOWN_INACTIVE_EXIT_WRITE(zfsvfs);
1600 		ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1601 		return (SET_ERROR(EIO));
1602 	}
1603 
1604 	/*
1605 	 * At this point there are no vops active, and any new vops will
1606 	 * fail with EIO since we have z_teardown_lock for writer (only
1607 	 * relevant for forced unmount).
1608 	 *
1609 	 * Release all holds on dbufs.
1610 	 */
1611 	mutex_enter(&zfsvfs->z_znodes_lock);
1612 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1613 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1614 		if (zp->z_sa_hdl != NULL) {
1615 			zfs_znode_dmu_fini(zp);
1616 		}
1617 	}
1618 	mutex_exit(&zfsvfs->z_znodes_lock);
1619 
1620 	/*
1621 	 * If we are unmounting, set the unmounted flag and let new vops
1622 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1623 	 * other vops will fail with EIO.
1624 	 */
1625 	if (unmounting) {
1626 		zfsvfs->z_unmounted = B_TRUE;
1627 		ZFS_TEARDOWN_INACTIVE_EXIT_WRITE(zfsvfs);
1628 		ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1629 	}
1630 
1631 	/*
1632 	 * z_os will be NULL if there was an error in attempting to reopen
1633 	 * zfsvfs, so just return as the properties had already been
1634 	 * unregistered and cached data had been evicted before.
1635 	 */
1636 	if (zfsvfs->z_os == NULL)
1637 		return (0);
1638 
1639 	/*
1640 	 * Unregister properties.
1641 	 */
1642 	zfs_unregister_callbacks(zfsvfs);
1643 
1644 	/*
1645 	 * Evict cached data
1646 	 */
1647 	if (!zfs_is_readonly(zfsvfs))
1648 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1649 	dmu_objset_evict_dbufs(zfsvfs->z_os);
1650 	dd = zfsvfs->z_os->os_dsl_dataset->ds_dir;
1651 	dsl_dir_cancel_waiters(dd);
1652 
1653 	return (0);
1654 }
1655 
1656 /*ARGSUSED*/
1657 static int
zfs_umount(vfs_t * vfsp,int fflag)1658 zfs_umount(vfs_t *vfsp, int fflag)
1659 {
1660 	kthread_t *td = curthread;
1661 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1662 	objset_t *os;
1663 	cred_t *cr = td->td_ucred;
1664 	int ret;
1665 
1666 	ret = secpolicy_fs_unmount(cr, vfsp);
1667 	if (ret) {
1668 		if (dsl_deleg_access((char *)vfsp->vfs_resource,
1669 		    ZFS_DELEG_PERM_MOUNT, cr))
1670 			return (ret);
1671 	}
1672 
1673 	/*
1674 	 * Unmount any snapshots mounted under .zfs before unmounting the
1675 	 * dataset itself.
1676 	 */
1677 	if (zfsvfs->z_ctldir != NULL) {
1678 		if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1679 			return (ret);
1680 	}
1681 
1682 	if (fflag & MS_FORCE) {
1683 		/*
1684 		 * Mark file system as unmounted before calling
1685 		 * vflush(FORCECLOSE). This way we ensure no future vnops
1686 		 * will be called and risk operating on DOOMED vnodes.
1687 		 */
1688 		ZFS_TEARDOWN_ENTER_WRITE(zfsvfs, FTAG);
1689 		zfsvfs->z_unmounted = B_TRUE;
1690 		ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1691 	}
1692 
1693 	/*
1694 	 * Flush all the files.
1695 	 */
1696 	ret = vflush(vfsp, 0, (fflag & MS_FORCE) ? FORCECLOSE : 0, td);
1697 	if (ret != 0)
1698 		return (ret);
1699 	while (taskqueue_cancel(zfsvfs_taskq->tq_queue,
1700 	    &zfsvfs->z_unlinked_drain_task, NULL) != 0)
1701 		taskqueue_drain(zfsvfs_taskq->tq_queue,
1702 		    &zfsvfs->z_unlinked_drain_task);
1703 
1704 	VERIFY0(zfsvfs_teardown(zfsvfs, B_TRUE));
1705 	os = zfsvfs->z_os;
1706 
1707 	/*
1708 	 * z_os will be NULL if there was an error in
1709 	 * attempting to reopen zfsvfs.
1710 	 */
1711 	if (os != NULL) {
1712 		/*
1713 		 * Unset the objset user_ptr.
1714 		 */
1715 		mutex_enter(&os->os_user_ptr_lock);
1716 		dmu_objset_set_user(os, NULL);
1717 		mutex_exit(&os->os_user_ptr_lock);
1718 
1719 		/*
1720 		 * Finally release the objset
1721 		 */
1722 		dmu_objset_disown(os, B_TRUE, zfsvfs);
1723 	}
1724 
1725 	/*
1726 	 * We can now safely destroy the '.zfs' directory node.
1727 	 */
1728 	if (zfsvfs->z_ctldir != NULL)
1729 		zfsctl_destroy(zfsvfs);
1730 	zfs_freevfs(vfsp);
1731 
1732 	return (0);
1733 }
1734 
1735 static int
zfs_vget(vfs_t * vfsp,ino_t ino,int flags,vnode_t ** vpp)1736 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
1737 {
1738 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1739 	znode_t		*zp;
1740 	int 		err;
1741 
1742 	/*
1743 	 * zfs_zget() can't operate on virtual entries like .zfs/ or
1744 	 * .zfs/snapshot/ directories, that's why we return EOPNOTSUPP.
1745 	 * This will make NFS to switch to LOOKUP instead of using VGET.
1746 	 */
1747 	if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR ||
1748 	    (zfsvfs->z_shares_dir != 0 && ino == zfsvfs->z_shares_dir))
1749 		return (EOPNOTSUPP);
1750 
1751 	ZFS_ENTER(zfsvfs);
1752 	err = zfs_zget(zfsvfs, ino, &zp);
1753 	if (err == 0 && zp->z_unlinked) {
1754 		vrele(ZTOV(zp));
1755 		err = EINVAL;
1756 	}
1757 	if (err == 0)
1758 		*vpp = ZTOV(zp);
1759 	ZFS_EXIT(zfsvfs);
1760 	if (err == 0) {
1761 		err = vn_lock(*vpp, flags);
1762 		if (err != 0)
1763 			vrele(*vpp);
1764 	}
1765 	if (err != 0)
1766 		*vpp = NULL;
1767 	return (err);
1768 }
1769 
1770 static int
1771 #if __FreeBSD_version >= 1300098
zfs_checkexp(vfs_t * vfsp,struct sockaddr * nam,uint64_t * extflagsp,struct ucred ** credanonp,int * numsecflavors,int * secflavors)1772 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, uint64_t *extflagsp,
1773     struct ucred **credanonp, int *numsecflavors, int *secflavors)
1774 #else
1775 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
1776     struct ucred **credanonp, int *numsecflavors, int **secflavors)
1777 #endif
1778 {
1779 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1780 
1781 	/*
1782 	 * If this is regular file system vfsp is the same as
1783 	 * zfsvfs->z_parent->z_vfs, but if it is snapshot,
1784 	 * zfsvfs->z_parent->z_vfs represents parent file system
1785 	 * which we have to use here, because only this file system
1786 	 * has mnt_export configured.
1787 	 */
1788 	return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
1789 	    credanonp, numsecflavors, secflavors));
1790 }
1791 
1792 CTASSERT(SHORT_FID_LEN <= sizeof (struct fid));
1793 CTASSERT(LONG_FID_LEN <= sizeof (struct fid));
1794 
1795 static int
zfs_fhtovp(vfs_t * vfsp,fid_t * fidp,int flags,vnode_t ** vpp)1796 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp)
1797 {
1798 	struct componentname cn;
1799 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1800 	znode_t		*zp;
1801 	vnode_t		*dvp;
1802 	uint64_t	object = 0;
1803 	uint64_t	fid_gen = 0;
1804 	uint64_t	setgen = 0;
1805 	uint64_t	gen_mask;
1806 	uint64_t	zp_gen;
1807 	int 		i, err;
1808 
1809 	*vpp = NULL;
1810 
1811 	ZFS_ENTER(zfsvfs);
1812 
1813 	/*
1814 	 * On FreeBSD we can get snapshot's mount point or its parent file
1815 	 * system mount point depending if snapshot is already mounted or not.
1816 	 */
1817 	if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
1818 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1819 		uint64_t	objsetid = 0;
1820 
1821 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1822 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1823 
1824 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1825 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1826 
1827 		ZFS_EXIT(zfsvfs);
1828 
1829 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1830 		if (err)
1831 			return (SET_ERROR(EINVAL));
1832 		ZFS_ENTER(zfsvfs);
1833 	}
1834 
1835 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1836 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1837 
1838 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1839 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1840 
1841 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1842 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1843 	} else {
1844 		ZFS_EXIT(zfsvfs);
1845 		return (SET_ERROR(EINVAL));
1846 	}
1847 
1848 	if (fidp->fid_len == LONG_FID_LEN && setgen != 0) {
1849 		ZFS_EXIT(zfsvfs);
1850 		dprintf("snapdir fid: fid_gen (%llu) and setgen (%llu)\n",
1851 		    (u_longlong_t)fid_gen, (u_longlong_t)setgen);
1852 		return (SET_ERROR(EINVAL));
1853 	}
1854 
1855 	/*
1856 	 * A zero fid_gen means we are in .zfs or the .zfs/snapshot
1857 	 * directory tree. If the object == zfsvfs->z_shares_dir, then
1858 	 * we are in the .zfs/shares directory tree.
1859 	 */
1860 	if ((fid_gen == 0 &&
1861 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) ||
1862 	    (zfsvfs->z_shares_dir != 0 && object == zfsvfs->z_shares_dir)) {
1863 		ZFS_EXIT(zfsvfs);
1864 		VERIFY0(zfsctl_root(zfsvfs, LK_SHARED, &dvp));
1865 		if (object == ZFSCTL_INO_SNAPDIR) {
1866 			cn.cn_nameptr = "snapshot";
1867 			cn.cn_namelen = strlen(cn.cn_nameptr);
1868 			cn.cn_nameiop = LOOKUP;
1869 			cn.cn_flags = ISLASTCN | LOCKLEAF;
1870 			cn.cn_lkflags = flags;
1871 			VERIFY0(VOP_LOOKUP(dvp, vpp, &cn));
1872 			vput(dvp);
1873 		} else if (object == zfsvfs->z_shares_dir) {
1874 			/*
1875 			 * XXX This branch must not be taken,
1876 			 * if it is, then the lookup below will
1877 			 * explode.
1878 			 */
1879 			cn.cn_nameptr = "shares";
1880 			cn.cn_namelen = strlen(cn.cn_nameptr);
1881 			cn.cn_nameiop = LOOKUP;
1882 			cn.cn_flags = ISLASTCN;
1883 			cn.cn_lkflags = flags;
1884 			VERIFY0(VOP_LOOKUP(dvp, vpp, &cn));
1885 			vput(dvp);
1886 		} else {
1887 			*vpp = dvp;
1888 		}
1889 		return (err);
1890 	}
1891 
1892 	gen_mask = -1ULL >> (64 - 8 * i);
1893 
1894 	dprintf("getting %llu [%llu mask %llx]\n", (u_longlong_t)object,
1895 	    (u_longlong_t)fid_gen,
1896 	    (u_longlong_t)gen_mask);
1897 	if ((err = zfs_zget(zfsvfs, object, &zp))) {
1898 		ZFS_EXIT(zfsvfs);
1899 		return (err);
1900 	}
1901 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1902 	    sizeof (uint64_t));
1903 	zp_gen = zp_gen & gen_mask;
1904 	if (zp_gen == 0)
1905 		zp_gen = 1;
1906 	if (zp->z_unlinked || zp_gen != fid_gen) {
1907 		dprintf("znode gen (%llu) != fid gen (%llu)\n",
1908 		    (u_longlong_t)zp_gen, (u_longlong_t)fid_gen);
1909 		vrele(ZTOV(zp));
1910 		ZFS_EXIT(zfsvfs);
1911 		return (SET_ERROR(EINVAL));
1912 	}
1913 
1914 	*vpp = ZTOV(zp);
1915 	ZFS_EXIT(zfsvfs);
1916 	err = vn_lock(*vpp, flags);
1917 	if (err == 0)
1918 		vnode_create_vobject(*vpp, zp->z_size, curthread);
1919 	else
1920 		*vpp = NULL;
1921 	return (err);
1922 }
1923 
1924 /*
1925  * Block out VOPs and close zfsvfs_t::z_os
1926  *
1927  * Note, if successful, then we return with the 'z_teardown_lock' and
1928  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
1929  * dataset and objset intact so that they can be atomically handed off during
1930  * a subsequent rollback or recv operation and the resume thereafter.
1931  */
1932 int
zfs_suspend_fs(zfsvfs_t * zfsvfs)1933 zfs_suspend_fs(zfsvfs_t *zfsvfs)
1934 {
1935 	int error;
1936 
1937 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1938 		return (error);
1939 
1940 	return (0);
1941 }
1942 
1943 /*
1944  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
1945  * is an invariant across any of the operations that can be performed while the
1946  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
1947  * are the same: the relevant objset and associated dataset are owned by
1948  * zfsvfs, held, and long held on entry.
1949  */
1950 int
zfs_resume_fs(zfsvfs_t * zfsvfs,dsl_dataset_t * ds)1951 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
1952 {
1953 	int err;
1954 	znode_t *zp;
1955 
1956 	ASSERT(ZFS_TEARDOWN_WRITE_HELD(zfsvfs));
1957 	ASSERT(ZFS_TEARDOWN_INACTIVE_WRITE_HELD(zfsvfs));
1958 
1959 	/*
1960 	 * We already own this, so just update the objset_t, as the one we
1961 	 * had before may have been evicted.
1962 	 */
1963 	objset_t *os;
1964 	VERIFY3P(ds->ds_owner, ==, zfsvfs);
1965 	VERIFY(dsl_dataset_long_held(ds));
1966 	dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds));
1967 	dsl_pool_config_enter(dp, FTAG);
1968 	VERIFY0(dmu_objset_from_ds(ds, &os));
1969 	dsl_pool_config_exit(dp, FTAG);
1970 
1971 	err = zfsvfs_init(zfsvfs, os);
1972 	if (err != 0)
1973 		goto bail;
1974 
1975 	ds->ds_dir->dd_activity_cancelled = B_FALSE;
1976 	VERIFY0(zfsvfs_setup(zfsvfs, B_FALSE));
1977 
1978 	zfs_set_fuid_feature(zfsvfs);
1979 
1980 	/*
1981 	 * Attempt to re-establish all the active znodes with
1982 	 * their dbufs.  If a zfs_rezget() fails, then we'll let
1983 	 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1984 	 * when they try to use their znode.
1985 	 */
1986 	mutex_enter(&zfsvfs->z_znodes_lock);
1987 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1988 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1989 		(void) zfs_rezget(zp);
1990 	}
1991 	mutex_exit(&zfsvfs->z_znodes_lock);
1992 
1993 bail:
1994 	/* release the VOPs */
1995 	ZFS_TEARDOWN_INACTIVE_EXIT_WRITE(zfsvfs);
1996 	ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1997 
1998 	if (err) {
1999 		/*
2000 		 * Since we couldn't setup the sa framework, try to force
2001 		 * unmount this file system.
2002 		 */
2003 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0) {
2004 			vfs_ref(zfsvfs->z_vfs);
2005 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
2006 		}
2007 	}
2008 	return (err);
2009 }
2010 
2011 static void
zfs_freevfs(vfs_t * vfsp)2012 zfs_freevfs(vfs_t *vfsp)
2013 {
2014 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2015 
2016 	zfsvfs_free(zfsvfs);
2017 
2018 	atomic_dec_32(&zfs_active_fs_count);
2019 }
2020 
2021 #ifdef __i386__
2022 static int desiredvnodes_backup;
2023 #include <sys/vmmeter.h>
2024 
2025 
2026 #include <vm/vm_page.h>
2027 #include <vm/vm_object.h>
2028 #include <vm/vm_kern.h>
2029 #include <vm/vm_map.h>
2030 #endif
2031 
2032 static void
zfs_vnodes_adjust(void)2033 zfs_vnodes_adjust(void)
2034 {
2035 #ifdef __i386__
2036 	int newdesiredvnodes;
2037 
2038 	desiredvnodes_backup = desiredvnodes;
2039 
2040 	/*
2041 	 * We calculate newdesiredvnodes the same way it is done in
2042 	 * vntblinit(). If it is equal to desiredvnodes, it means that
2043 	 * it wasn't tuned by the administrator and we can tune it down.
2044 	 */
2045 	newdesiredvnodes = min(maxproc + vm_cnt.v_page_count / 4, 2 *
2046 	    vm_kmem_size / (5 * (sizeof (struct vm_object) +
2047 	    sizeof (struct vnode))));
2048 	if (newdesiredvnodes == desiredvnodes)
2049 		desiredvnodes = (3 * newdesiredvnodes) / 4;
2050 #endif
2051 }
2052 
2053 static void
zfs_vnodes_adjust_back(void)2054 zfs_vnodes_adjust_back(void)
2055 {
2056 
2057 #ifdef __i386__
2058 	desiredvnodes = desiredvnodes_backup;
2059 #endif
2060 }
2061 
2062 void
zfs_init(void)2063 zfs_init(void)
2064 {
2065 
2066 	printf("ZFS filesystem version: " ZPL_VERSION_STRING "\n");
2067 
2068 	/*
2069 	 * Initialize .zfs directory structures
2070 	 */
2071 	zfsctl_init();
2072 
2073 	/*
2074 	 * Initialize znode cache, vnode ops, etc...
2075 	 */
2076 	zfs_znode_init();
2077 
2078 	/*
2079 	 * Reduce number of vnodes. Originally number of vnodes is calculated
2080 	 * with UFS inode in mind. We reduce it here, because it's too big for
2081 	 * ZFS/i386.
2082 	 */
2083 	zfs_vnodes_adjust();
2084 
2085 	dmu_objset_register_type(DMU_OST_ZFS, zpl_get_file_info);
2086 
2087 	zfsvfs_taskq = taskq_create("zfsvfs", 1, minclsyspri, 0, 0, 0);
2088 }
2089 
2090 void
zfs_fini(void)2091 zfs_fini(void)
2092 {
2093 	taskq_destroy(zfsvfs_taskq);
2094 	zfsctl_fini();
2095 	zfs_znode_fini();
2096 	zfs_vnodes_adjust_back();
2097 }
2098 
2099 int
zfs_busy(void)2100 zfs_busy(void)
2101 {
2102 	return (zfs_active_fs_count != 0);
2103 }
2104 
2105 /*
2106  * Release VOPs and unmount a suspended filesystem.
2107  */
2108 int
zfs_end_fs(zfsvfs_t * zfsvfs,dsl_dataset_t * ds)2109 zfs_end_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2110 {
2111 	ASSERT(ZFS_TEARDOWN_WRITE_HELD(zfsvfs));
2112 	ASSERT(ZFS_TEARDOWN_INACTIVE_WRITE_HELD(zfsvfs));
2113 
2114 	/*
2115 	 * We already own this, so just hold and rele it to update the
2116 	 * objset_t, as the one we had before may have been evicted.
2117 	 */
2118 	objset_t *os;
2119 	VERIFY3P(ds->ds_owner, ==, zfsvfs);
2120 	VERIFY(dsl_dataset_long_held(ds));
2121 	dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds));
2122 	dsl_pool_config_enter(dp, FTAG);
2123 	VERIFY0(dmu_objset_from_ds(ds, &os));
2124 	dsl_pool_config_exit(dp, FTAG);
2125 	zfsvfs->z_os = os;
2126 
2127 	/* release the VOPs */
2128 	ZFS_TEARDOWN_INACTIVE_EXIT_WRITE(zfsvfs);
2129 	ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
2130 
2131 	/*
2132 	 * Try to force unmount this file system.
2133 	 */
2134 	(void) zfs_umount(zfsvfs->z_vfs, 0);
2135 	zfsvfs->z_unmounted = B_TRUE;
2136 	return (0);
2137 }
2138 
2139 int
zfs_set_version(zfsvfs_t * zfsvfs,uint64_t newvers)2140 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2141 {
2142 	int error;
2143 	objset_t *os = zfsvfs->z_os;
2144 	dmu_tx_t *tx;
2145 
2146 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2147 		return (SET_ERROR(EINVAL));
2148 
2149 	if (newvers < zfsvfs->z_version)
2150 		return (SET_ERROR(EINVAL));
2151 
2152 	if (zfs_spa_version_map(newvers) >
2153 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
2154 		return (SET_ERROR(ENOTSUP));
2155 
2156 	tx = dmu_tx_create(os);
2157 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2158 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2159 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2160 		    ZFS_SA_ATTRS);
2161 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2162 	}
2163 	error = dmu_tx_assign(tx, TXG_WAIT);
2164 	if (error) {
2165 		dmu_tx_abort(tx);
2166 		return (error);
2167 	}
2168 
2169 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2170 	    8, 1, &newvers, tx);
2171 
2172 	if (error) {
2173 		dmu_tx_commit(tx);
2174 		return (error);
2175 	}
2176 
2177 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2178 		uint64_t sa_obj;
2179 
2180 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2181 		    SPA_VERSION_SA);
2182 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2183 		    DMU_OT_NONE, 0, tx);
2184 
2185 		error = zap_add(os, MASTER_NODE_OBJ,
2186 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2187 		ASSERT0(error);
2188 
2189 		VERIFY0(sa_set_sa_object(os, sa_obj));
2190 		sa_register_update_callback(os, zfs_sa_upgrade);
2191 	}
2192 
2193 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2194 	    "from %ju to %ju", (uintmax_t)zfsvfs->z_version,
2195 	    (uintmax_t)newvers);
2196 	dmu_tx_commit(tx);
2197 
2198 	zfsvfs->z_version = newvers;
2199 	os->os_version = newvers;
2200 
2201 	zfs_set_fuid_feature(zfsvfs);
2202 
2203 	return (0);
2204 }
2205 
2206 /*
2207  * Read a property stored within the master node.
2208  */
2209 int
zfs_get_zplprop(objset_t * os,zfs_prop_t prop,uint64_t * value)2210 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2211 {
2212 	uint64_t *cached_copy = NULL;
2213 
2214 	/*
2215 	 * Figure out where in the objset_t the cached copy would live, if it
2216 	 * is available for the requested property.
2217 	 */
2218 	if (os != NULL) {
2219 		switch (prop) {
2220 		case ZFS_PROP_VERSION:
2221 			cached_copy = &os->os_version;
2222 			break;
2223 		case ZFS_PROP_NORMALIZE:
2224 			cached_copy = &os->os_normalization;
2225 			break;
2226 		case ZFS_PROP_UTF8ONLY:
2227 			cached_copy = &os->os_utf8only;
2228 			break;
2229 		case ZFS_PROP_CASE:
2230 			cached_copy = &os->os_casesensitivity;
2231 			break;
2232 		default:
2233 			break;
2234 		}
2235 	}
2236 	if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
2237 		*value = *cached_copy;
2238 		return (0);
2239 	}
2240 
2241 	/*
2242 	 * If the property wasn't cached, look up the file system's value for
2243 	 * the property. For the version property, we look up a slightly
2244 	 * different string.
2245 	 */
2246 	const char *pname;
2247 	int error = ENOENT;
2248 	if (prop == ZFS_PROP_VERSION) {
2249 		pname = ZPL_VERSION_STR;
2250 	} else {
2251 		pname = zfs_prop_to_name(prop);
2252 	}
2253 
2254 	if (os != NULL) {
2255 		ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2256 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2257 	}
2258 
2259 	if (error == ENOENT) {
2260 		/* No value set, use the default value */
2261 		switch (prop) {
2262 		case ZFS_PROP_VERSION:
2263 			*value = ZPL_VERSION;
2264 			break;
2265 		case ZFS_PROP_NORMALIZE:
2266 		case ZFS_PROP_UTF8ONLY:
2267 			*value = 0;
2268 			break;
2269 		case ZFS_PROP_CASE:
2270 			*value = ZFS_CASE_SENSITIVE;
2271 			break;
2272 		case ZFS_PROP_ACLTYPE:
2273 			*value = ZFS_ACLTYPE_NFSV4;
2274 			break;
2275 		default:
2276 			return (error);
2277 		}
2278 		error = 0;
2279 	}
2280 
2281 	/*
2282 	 * If one of the methods for getting the property value above worked,
2283 	 * copy it into the objset_t's cache.
2284 	 */
2285 	if (error == 0 && cached_copy != NULL) {
2286 		*cached_copy = *value;
2287 	}
2288 
2289 	return (error);
2290 }
2291 
2292 /*
2293  * Return true if the corresponding vfs's unmounted flag is set.
2294  * Otherwise return false.
2295  * If this function returns true we know VFS unmount has been initiated.
2296  */
2297 boolean_t
zfs_get_vfs_flag_unmounted(objset_t * os)2298 zfs_get_vfs_flag_unmounted(objset_t *os)
2299 {
2300 	zfsvfs_t *zfvp;
2301 	boolean_t unmounted = B_FALSE;
2302 
2303 	ASSERT3U(dmu_objset_type(os), ==, DMU_OST_ZFS);
2304 
2305 	mutex_enter(&os->os_user_ptr_lock);
2306 	zfvp = dmu_objset_get_user(os);
2307 	if (zfvp != NULL && zfvp->z_vfs != NULL &&
2308 	    (zfvp->z_vfs->mnt_kern_flag & MNTK_UNMOUNT))
2309 		unmounted = B_TRUE;
2310 	mutex_exit(&os->os_user_ptr_lock);
2311 
2312 	return (unmounted);
2313 }
2314 
2315 #ifdef _KERNEL
2316 void
zfsvfs_update_fromname(const char * oldname,const char * newname)2317 zfsvfs_update_fromname(const char *oldname, const char *newname)
2318 {
2319 	char tmpbuf[MAXPATHLEN];
2320 	struct mount *mp;
2321 	char *fromname;
2322 	size_t oldlen;
2323 
2324 	oldlen = strlen(oldname);
2325 
2326 	mtx_lock(&mountlist_mtx);
2327 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2328 		fromname = mp->mnt_stat.f_mntfromname;
2329 		if (strcmp(fromname, oldname) == 0) {
2330 			(void) strlcpy(fromname, newname,
2331 			    sizeof (mp->mnt_stat.f_mntfromname));
2332 			continue;
2333 		}
2334 		if (strncmp(fromname, oldname, oldlen) == 0 &&
2335 		    (fromname[oldlen] == '/' || fromname[oldlen] == '@')) {
2336 			(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s%s",
2337 			    newname, fromname + oldlen);
2338 			(void) strlcpy(fromname, tmpbuf,
2339 			    sizeof (mp->mnt_stat.f_mntfromname));
2340 			continue;
2341 		}
2342 	}
2343 	mtx_unlock(&mountlist_mtx);
2344 }
2345 #endif
2346