xref: /titanic_50/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 1a4cea1bca45d5775aeb88646a63db027947246f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Integros [integros.com]
25  */
26 
27 /* Portions Copyright 2010 Robert Milkowski */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/kmem.h>
34 #include <sys/pathname.h>
35 #include <sys/vnode.h>
36 #include <sys/vfs.h>
37 #include <sys/vfs_opreg.h>
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
40 #include <sys/cmn_err.h>
41 #include "fs/fs_subr.h"
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_dir.h>
44 #include <sys/zil.h>
45 #include <sys/fs/zfs.h>
46 #include <sys/dmu.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/spa.h>
51 #include <sys/zap.h>
52 #include <sys/sa.h>
53 #include <sys/sa_impl.h>
54 #include <sys/varargs.h>
55 #include <sys/policy.h>
56 #include <sys/atomic.h>
57 #include <sys/mkdev.h>
58 #include <sys/modctl.h>
59 #include <sys/refstr.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_fuid.h>
63 #include <sys/bootconf.h>
64 #include <sys/sunddi.h>
65 #include <sys/dnlc.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/spa_boot.h>
68 #include <sys/zfs_events.h>
69 #include "zfs_comutil.h"
70 
71 int zfsfstype;
72 vfsops_t *zfs_vfsops = NULL;
73 static major_t zfs_major;
74 static minor_t zfs_minor;
75 static kmutex_t	zfs_dev_mtx;
76 
77 extern int sys_shutdown;
78 
79 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
80 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
81 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
82 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
83 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
84 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
85 static void zfs_freevfs(vfs_t *vfsp);
86 
87 static const fs_operation_def_t zfs_vfsops_template[] = {
88 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
89 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
90 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
91 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
92 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
93 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
94 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
95 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
96 	NULL,			NULL
97 };
98 
99 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
100 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
101 	NULL,			NULL
102 };
103 
104 /*
105  * We need to keep a count of active fs's.
106  * This is necessary to prevent our module
107  * from being unloaded after a umount -f
108  */
109 static uint32_t	zfs_active_fs_count = 0;
110 
111 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
112 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
113 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
114 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
115 
116 /*
117  * MO_DEFAULT is not used since the default value is determined
118  * by the equivalent property.
119  */
120 static mntopt_t mntopts[] = {
121 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
122 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
123 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
124 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
125 };
126 
127 static mntopts_t zfs_mntopts = {
128 	sizeof (mntopts) / sizeof (mntopt_t),
129 	mntopts
130 };
131 
132 /*ARGSUSED*/
133 int
zfs_sync(vfs_t * vfsp,short flag,cred_t * cr)134 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
135 {
136 	/*
137 	 * Data integrity is job one.  We don't want a compromised kernel
138 	 * writing to the storage pool, so we never sync during panic.
139 	 */
140 	if (panicstr)
141 		return (0);
142 
143 	/*
144 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
145 	 * to sync metadata, which they would otherwise cache indefinitely.
146 	 * Semantically, the only requirement is that the sync be initiated.
147 	 * The DMU syncs out txgs frequently, so there's nothing to do.
148 	 */
149 	if (flag & SYNC_ATTR)
150 		return (0);
151 
152 	if (vfsp != NULL) {
153 		/*
154 		 * Sync a specific filesystem.
155 		 */
156 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
157 		dsl_pool_t *dp;
158 
159 		ZFS_ENTER(zfsvfs);
160 		dp = dmu_objset_pool(zfsvfs->z_os);
161 
162 		/*
163 		 * If the system is shutting down, then skip any
164 		 * filesystems which may exist on a suspended pool.
165 		 */
166 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
167 			ZFS_EXIT(zfsvfs);
168 			return (0);
169 		}
170 
171 		if (zfsvfs->z_log != NULL)
172 			zil_commit(zfsvfs->z_log, 0);
173 
174 		ZFS_EXIT(zfsvfs);
175 	} else {
176 		/*
177 		 * Sync all ZFS filesystems.  This is what happens when you
178 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
179 		 * request by waiting for all pools to commit all dirty data.
180 		 */
181 		spa_sync_allpools();
182 	}
183 
184 	return (0);
185 }
186 
187 static int
zfs_create_unique_device(dev_t * dev)188 zfs_create_unique_device(dev_t *dev)
189 {
190 	major_t new_major;
191 
192 	do {
193 		ASSERT3U(zfs_minor, <=, MAXMIN32);
194 		minor_t start = zfs_minor;
195 		do {
196 			mutex_enter(&zfs_dev_mtx);
197 			if (zfs_minor >= MAXMIN32) {
198 				/*
199 				 * If we're still using the real major
200 				 * keep out of /dev/zfs and /dev/zvol minor
201 				 * number space.  If we're using a getudev()'ed
202 				 * major number, we can use all of its minors.
203 				 */
204 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
205 					zfs_minor = ZFS_MIN_MINOR;
206 				else
207 					zfs_minor = 0;
208 			} else {
209 				zfs_minor++;
210 			}
211 			*dev = makedevice(zfs_major, zfs_minor);
212 			mutex_exit(&zfs_dev_mtx);
213 		} while (vfs_devismounted(*dev) && zfs_minor != start);
214 		if (zfs_minor == start) {
215 			/*
216 			 * We are using all ~262,000 minor numbers for the
217 			 * current major number.  Create a new major number.
218 			 */
219 			if ((new_major = getudev()) == (major_t)-1) {
220 				cmn_err(CE_WARN,
221 				    "zfs_mount: Can't get unique major "
222 				    "device number.");
223 				return (-1);
224 			}
225 			mutex_enter(&zfs_dev_mtx);
226 			zfs_major = new_major;
227 			zfs_minor = 0;
228 
229 			mutex_exit(&zfs_dev_mtx);
230 		} else {
231 			break;
232 		}
233 		/* CONSTANTCONDITION */
234 	} while (1);
235 
236 	return (0);
237 }
238 
239 static void
atime_changed_cb(void * arg,uint64_t newval)240 atime_changed_cb(void *arg, uint64_t newval)
241 {
242 	zfsvfs_t *zfsvfs = arg;
243 
244 	if (newval == TRUE) {
245 		zfsvfs->z_atime = TRUE;
246 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
247 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
248 	} else {
249 		zfsvfs->z_atime = FALSE;
250 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
251 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
252 	}
253 }
254 
255 static void
xattr_changed_cb(void * arg,uint64_t newval)256 xattr_changed_cb(void *arg, uint64_t newval)
257 {
258 	zfsvfs_t *zfsvfs = arg;
259 
260 	if (newval == TRUE) {
261 		/* XXX locking on vfs_flag? */
262 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
263 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
264 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
265 	} else {
266 		/* XXX locking on vfs_flag? */
267 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
268 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
269 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
270 	}
271 }
272 
273 static void
blksz_changed_cb(void * arg,uint64_t newval)274 blksz_changed_cb(void *arg, uint64_t newval)
275 {
276 	zfsvfs_t *zfsvfs = arg;
277 	ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
278 	ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
279 	ASSERT(ISP2(newval));
280 
281 	zfsvfs->z_max_blksz = newval;
282 	zfsvfs->z_vfs->vfs_bsize = newval;
283 }
284 
285 static void
readonly_changed_cb(void * arg,uint64_t newval)286 readonly_changed_cb(void *arg, uint64_t newval)
287 {
288 	zfsvfs_t *zfsvfs = arg;
289 
290 	if (newval) {
291 		/* XXX locking on vfs_flag? */
292 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
293 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
294 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
295 	} else {
296 		/* XXX locking on vfs_flag? */
297 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
298 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
299 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
300 	}
301 }
302 
303 static void
devices_changed_cb(void * arg,uint64_t newval)304 devices_changed_cb(void *arg, uint64_t newval)
305 {
306 	zfsvfs_t *zfsvfs = arg;
307 
308 	if (newval == FALSE) {
309 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
310 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
311 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
312 	} else {
313 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
314 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
315 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
316 	}
317 }
318 
319 static void
setuid_changed_cb(void * arg,uint64_t newval)320 setuid_changed_cb(void *arg, uint64_t newval)
321 {
322 	zfsvfs_t *zfsvfs = arg;
323 
324 	if (newval == FALSE) {
325 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
326 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
327 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
328 	} else {
329 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
330 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
331 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
332 	}
333 }
334 
335 static void
exec_changed_cb(void * arg,uint64_t newval)336 exec_changed_cb(void *arg, uint64_t newval)
337 {
338 	zfsvfs_t *zfsvfs = arg;
339 
340 	if (newval == FALSE) {
341 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
342 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
343 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
344 	} else {
345 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
346 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
347 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
348 	}
349 }
350 
351 static void
follow_changed_cb(void * arg,uint64_t newval)352 follow_changed_cb(void *arg, uint64_t newval)
353 {
354 	zfsvfs_t *zfsvfs = arg;
355 
356 	if (newval == FALSE) {
357 		zfsvfs->z_vfs->vfs_flag |= VFS_NOFOLLOW;
358 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_FOLLOW);
359 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOFOLLOW, NULL, 0);
360 	} else {
361 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOFOLLOW;
362 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOFOLLOW);
363 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_FOLLOW, NULL, 0);
364 	}
365 }
366 
367 /*
368  * The nbmand mount option can be changed at mount time.
369  * We can't allow it to be toggled on live file systems or incorrect
370  * behavior may be seen from cifs clients
371  *
372  * This property isn't registered via dsl_prop_register(), but this callback
373  * will be called when a file system is first mounted
374  */
375 static void
nbmand_changed_cb(void * arg,uint64_t newval)376 nbmand_changed_cb(void *arg, uint64_t newval)
377 {
378 	zfsvfs_t *zfsvfs = arg;
379 	if (newval == FALSE) {
380 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
381 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
382 	} else {
383 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
384 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
385 	}
386 }
387 
388 static void
snapdir_changed_cb(void * arg,uint64_t newval)389 snapdir_changed_cb(void *arg, uint64_t newval)
390 {
391 	zfsvfs_t *zfsvfs = arg;
392 
393 	zfsvfs->z_show_ctldir = newval;
394 }
395 
396 static void
vscan_changed_cb(void * arg,uint64_t newval)397 vscan_changed_cb(void *arg, uint64_t newval)
398 {
399 	zfsvfs_t *zfsvfs = arg;
400 
401 	zfsvfs->z_vscan = newval;
402 }
403 
404 static void
acl_mode_changed_cb(void * arg,uint64_t newval)405 acl_mode_changed_cb(void *arg, uint64_t newval)
406 {
407 	zfsvfs_t *zfsvfs = arg;
408 
409 	zfsvfs->z_acl_mode = newval;
410 }
411 
412 static void
acl_inherit_changed_cb(void * arg,uint64_t newval)413 acl_inherit_changed_cb(void *arg, uint64_t newval)
414 {
415 	zfsvfs_t *zfsvfs = arg;
416 
417 	zfsvfs->z_acl_inherit = newval;
418 }
419 
420 static int
zfs_register_callbacks(vfs_t * vfsp)421 zfs_register_callbacks(vfs_t *vfsp)
422 {
423 	struct dsl_dataset *ds = NULL;
424 	objset_t *os = NULL;
425 	zfsvfs_t *zfsvfs = NULL;
426 	uint64_t nbmand;
427 	boolean_t readonly = B_FALSE;
428 	boolean_t do_readonly = B_FALSE;
429 	boolean_t setuid = B_FALSE;
430 	boolean_t do_setuid = B_FALSE;
431 	boolean_t exec = B_FALSE;
432 	boolean_t do_exec = B_FALSE;
433 	boolean_t follow = B_FALSE;
434 	boolean_t do_follow = B_FALSE;
435 	boolean_t devices = B_FALSE;
436 	boolean_t do_devices = B_FALSE;
437 	boolean_t xattr = B_FALSE;
438 	boolean_t do_xattr = B_FALSE;
439 	boolean_t atime = B_FALSE;
440 	boolean_t do_atime = B_FALSE;
441 	int error = 0;
442 
443 	ASSERT(vfsp);
444 	zfsvfs = vfsp->vfs_data;
445 	ASSERT(zfsvfs);
446 	os = zfsvfs->z_os;
447 
448 	/*
449 	 * The act of registering our callbacks will destroy any mount
450 	 * options we may have.  In order to enable temporary overrides
451 	 * of mount options, we stash away the current values and
452 	 * restore them after we register the callbacks.
453 	 */
454 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
455 	    !spa_writeable(dmu_objset_spa(os))) {
456 		readonly = B_TRUE;
457 		do_readonly = B_TRUE;
458 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
459 		readonly = B_FALSE;
460 		do_readonly = B_TRUE;
461 	}
462 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
463 		devices = B_FALSE;
464 		setuid = B_FALSE;
465 		do_devices = B_TRUE;
466 		do_setuid = B_TRUE;
467 	} else {
468 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
469 			devices = B_FALSE;
470 			do_devices = B_TRUE;
471 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
472 			devices = B_TRUE;
473 			do_devices = B_TRUE;
474 		}
475 
476 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
477 			setuid = B_FALSE;
478 			do_setuid = B_TRUE;
479 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
480 			setuid = B_TRUE;
481 			do_setuid = B_TRUE;
482 		}
483 	}
484 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
485 		exec = B_FALSE;
486 		do_exec = B_TRUE;
487 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
488 		exec = B_TRUE;
489 		do_exec = B_TRUE;
490 	}
491 	if (vfs_optionisset(vfsp, MNTOPT_NOFOLLOW, NULL)) {
492 		follow = B_FALSE;
493 		do_follow = B_TRUE;
494 	} else if (vfs_optionisset(vfsp, MNTOPT_FOLLOW, NULL)) {
495 		follow = B_TRUE;
496 		do_follow = B_TRUE;
497 	}
498 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
499 		xattr = B_FALSE;
500 		do_xattr = B_TRUE;
501 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
502 		xattr = B_TRUE;
503 		do_xattr = B_TRUE;
504 	}
505 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
506 		atime = B_FALSE;
507 		do_atime = B_TRUE;
508 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
509 		atime = B_TRUE;
510 		do_atime = B_TRUE;
511 	}
512 
513 	/*
514 	 * nbmand is a special property.  It can only be changed at
515 	 * mount time.
516 	 *
517 	 * This is weird, but it is documented to only be changeable
518 	 * at mount time.
519 	 */
520 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
521 		nbmand = B_FALSE;
522 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
523 		nbmand = B_TRUE;
524 	} else {
525 		char osname[ZFS_MAX_DATASET_NAME_LEN];
526 
527 		dmu_objset_name(os, osname);
528 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
529 		    NULL)) {
530 			return (error);
531 		}
532 	}
533 
534 	/*
535 	 * Register property callbacks.
536 	 *
537 	 * It would probably be fine to just check for i/o error from
538 	 * the first prop_register(), but I guess I like to go
539 	 * overboard...
540 	 */
541 	ds = dmu_objset_ds(os);
542 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
543 	error = dsl_prop_register(ds,
544 	    zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
545 	error = error ? error : dsl_prop_register(ds,
546 	    zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
547 	error = error ? error : dsl_prop_register(ds,
548 	    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
549 	error = error ? error : dsl_prop_register(ds,
550 	    zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
551 	error = error ? error : dsl_prop_register(ds,
552 	    zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
553 	error = error ? error : dsl_prop_register(ds,
554 	    zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
555 	error = error ? error : dsl_prop_register(ds,
556 	    zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
557 	error = error ? error : dsl_prop_register(ds,
558 	    zfs_prop_to_name(ZFS_PROP_FOLLOW), follow_changed_cb, zfsvfs);
559 	error = error ? error : dsl_prop_register(ds,
560 	    zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
561 	error = error ? error : dsl_prop_register(ds,
562 	    zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
563 	error = error ? error : dsl_prop_register(ds,
564 	    zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
565 	    zfsvfs);
566 	error = error ? error : dsl_prop_register(ds,
567 	    zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
568 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
569 	if (error)
570 		goto unregister;
571 
572 	/*
573 	 * Invoke our callbacks to restore temporary mount options.
574 	 */
575 	if (do_readonly)
576 		readonly_changed_cb(zfsvfs, readonly);
577 	if (do_setuid)
578 		setuid_changed_cb(zfsvfs, setuid);
579 	if (do_exec)
580 		exec_changed_cb(zfsvfs, exec);
581 	if (do_follow)
582 		follow_changed_cb(zfsvfs, follow);
583 	if (do_devices)
584 		devices_changed_cb(zfsvfs, devices);
585 	if (do_xattr)
586 		xattr_changed_cb(zfsvfs, xattr);
587 	if (do_atime)
588 		atime_changed_cb(zfsvfs, atime);
589 
590 	nbmand_changed_cb(zfsvfs, nbmand);
591 
592 	return (0);
593 
594 unregister:
595 	dsl_prop_unregister_all(ds, zfsvfs);
596 	return (error);
597 }
598 
599 static int
zfs_space_delta_cb(dmu_object_type_t bonustype,void * data,uint64_t * userp,uint64_t * groupp)600 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
601     uint64_t *userp, uint64_t *groupp)
602 {
603 	/*
604 	 * Is it a valid type of object to track?
605 	 */
606 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
607 		return (SET_ERROR(ENOENT));
608 
609 	/*
610 	 * If we have a NULL data pointer
611 	 * then assume the id's aren't changing and
612 	 * return EEXIST to the dmu to let it know to
613 	 * use the same ids
614 	 */
615 	if (data == NULL)
616 		return (SET_ERROR(EEXIST));
617 
618 	if (bonustype == DMU_OT_ZNODE) {
619 		znode_phys_t *znp = data;
620 		*userp = znp->zp_uid;
621 		*groupp = znp->zp_gid;
622 	} else {
623 		int hdrsize;
624 		sa_hdr_phys_t *sap = data;
625 		sa_hdr_phys_t sa = *sap;
626 		boolean_t swap = B_FALSE;
627 
628 		ASSERT(bonustype == DMU_OT_SA);
629 
630 		if (sa.sa_magic == 0) {
631 			/*
632 			 * This should only happen for newly created
633 			 * files that haven't had the znode data filled
634 			 * in yet.
635 			 */
636 			*userp = 0;
637 			*groupp = 0;
638 			return (0);
639 		}
640 		if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
641 			sa.sa_magic = SA_MAGIC;
642 			sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
643 			swap = B_TRUE;
644 		} else {
645 			VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
646 		}
647 
648 		hdrsize = sa_hdrsize(&sa);
649 		VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
650 		*userp = *((uint64_t *)((uintptr_t)data + hdrsize +
651 		    SA_UID_OFFSET));
652 		*groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
653 		    SA_GID_OFFSET));
654 		if (swap) {
655 			*userp = BSWAP_64(*userp);
656 			*groupp = BSWAP_64(*groupp);
657 		}
658 	}
659 	return (0);
660 }
661 
662 static void
fuidstr_to_sid(zfsvfs_t * zfsvfs,const char * fuidstr,char * domainbuf,int buflen,uid_t * ridp)663 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
664     char *domainbuf, int buflen, uid_t *ridp)
665 {
666 	uint64_t fuid;
667 	const char *domain;
668 
669 	fuid = strtonum(fuidstr, NULL);
670 
671 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
672 	if (domain)
673 		(void) strlcpy(domainbuf, domain, buflen);
674 	else
675 		domainbuf[0] = '\0';
676 	*ridp = FUID_RID(fuid);
677 }
678 
679 static uint64_t
zfs_userquota_prop_to_obj(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type)680 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
681 {
682 	switch (type) {
683 	case ZFS_PROP_USERUSED:
684 		return (DMU_USERUSED_OBJECT);
685 	case ZFS_PROP_GROUPUSED:
686 		return (DMU_GROUPUSED_OBJECT);
687 	case ZFS_PROP_USERQUOTA:
688 		return (zfsvfs->z_userquota_obj);
689 	case ZFS_PROP_GROUPQUOTA:
690 		return (zfsvfs->z_groupquota_obj);
691 	}
692 	return (0);
693 }
694 
695 int
zfs_userspace_many(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,uint64_t * cookiep,void * vbuf,uint64_t * bufsizep)696 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
697     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
698 {
699 	int error;
700 	zap_cursor_t zc;
701 	zap_attribute_t za;
702 	zfs_useracct_t *buf = vbuf;
703 	uint64_t obj;
704 
705 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
706 		return (SET_ERROR(ENOTSUP));
707 
708 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
709 	if (obj == 0) {
710 		*bufsizep = 0;
711 		return (0);
712 	}
713 
714 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
715 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
716 	    zap_cursor_advance(&zc)) {
717 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
718 		    *bufsizep)
719 			break;
720 
721 		fuidstr_to_sid(zfsvfs, za.za_name,
722 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
723 
724 		buf->zu_space = za.za_first_integer;
725 		buf++;
726 	}
727 	if (error == ENOENT)
728 		error = 0;
729 
730 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
731 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
732 	*cookiep = zap_cursor_serialize(&zc);
733 	zap_cursor_fini(&zc);
734 	return (error);
735 }
736 
737 /*
738  * buf must be big enough (eg, 32 bytes)
739  */
740 static int
id_to_fuidstr(zfsvfs_t * zfsvfs,const char * domain,uid_t rid,char * buf,boolean_t addok)741 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
742     char *buf, boolean_t addok)
743 {
744 	uint64_t fuid;
745 	int domainid = 0;
746 
747 	if (domain && domain[0]) {
748 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
749 		if (domainid == -1)
750 			return (SET_ERROR(ENOENT));
751 	}
752 	fuid = FUID_ENCODE(domainid, rid);
753 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
754 	return (0);
755 }
756 
757 int
zfs_userspace_one(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t * valp)758 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
759     const char *domain, uint64_t rid, uint64_t *valp)
760 {
761 	char buf[32];
762 	int err;
763 	uint64_t obj;
764 
765 	*valp = 0;
766 
767 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
768 		return (SET_ERROR(ENOTSUP));
769 
770 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
771 	if (obj == 0)
772 		return (0);
773 
774 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
775 	if (err)
776 		return (err);
777 
778 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
779 	if (err == ENOENT)
780 		err = 0;
781 	return (err);
782 }
783 
784 int
zfs_set_userquota(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t quota)785 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
786     const char *domain, uint64_t rid, uint64_t quota)
787 {
788 	char buf[32];
789 	int err;
790 	dmu_tx_t *tx;
791 	uint64_t *objp;
792 	boolean_t fuid_dirtied;
793 
794 	if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
795 		return (SET_ERROR(EINVAL));
796 
797 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
798 		return (SET_ERROR(ENOTSUP));
799 
800 	objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
801 	    &zfsvfs->z_groupquota_obj;
802 
803 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
804 	if (err)
805 		return (err);
806 	fuid_dirtied = zfsvfs->z_fuid_dirty;
807 
808 	tx = dmu_tx_create(zfsvfs->z_os);
809 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
810 	if (*objp == 0) {
811 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
812 		    zfs_userquota_prop_prefixes[type]);
813 	}
814 	if (fuid_dirtied)
815 		zfs_fuid_txhold(zfsvfs, tx);
816 	err = dmu_tx_assign(tx, TXG_WAIT);
817 	if (err) {
818 		dmu_tx_abort(tx);
819 		return (err);
820 	}
821 
822 	mutex_enter(&zfsvfs->z_lock);
823 	if (*objp == 0) {
824 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
825 		    DMU_OT_NONE, 0, tx);
826 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
827 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
828 	}
829 	mutex_exit(&zfsvfs->z_lock);
830 
831 	if (quota == 0) {
832 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
833 		if (err == ENOENT)
834 			err = 0;
835 	} else {
836 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
837 	}
838 	ASSERT(err == 0);
839 	if (fuid_dirtied)
840 		zfs_fuid_sync(zfsvfs, tx);
841 	dmu_tx_commit(tx);
842 	return (err);
843 }
844 
845 boolean_t
zfs_fuid_overquota(zfsvfs_t * zfsvfs,boolean_t isgroup,uint64_t fuid)846 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
847 {
848 	char buf[32];
849 	uint64_t used, quota, usedobj, quotaobj;
850 	int err;
851 
852 	usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
853 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
854 
855 	if (quotaobj == 0 || zfsvfs->z_replay)
856 		return (B_FALSE);
857 
858 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
859 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
860 	if (err != 0)
861 		return (B_FALSE);
862 
863 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
864 	if (err != 0)
865 		return (B_FALSE);
866 	return (used >= quota);
867 }
868 
869 boolean_t
zfs_owner_overquota(zfsvfs_t * zfsvfs,znode_t * zp,boolean_t isgroup)870 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
871 {
872 	uint64_t fuid;
873 	uint64_t quotaobj;
874 
875 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
876 
877 	fuid = isgroup ? zp->z_gid : zp->z_uid;
878 
879 	if (quotaobj == 0 || zfsvfs->z_replay)
880 		return (B_FALSE);
881 
882 	return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
883 }
884 
885 int
zfsvfs_create(const char * osname,zfsvfs_t ** zfvp)886 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
887 {
888 	objset_t *os;
889 	zfsvfs_t *zfsvfs;
890 	uint64_t zval;
891 	int i, error;
892 	uint64_t sa_obj;
893 
894 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
895 
896 	/*
897 	 * We claim to always be readonly so we can open snapshots;
898 	 * other ZPL code will prevent us from writing to snapshots.
899 	 */
900 	error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
901 	if (error) {
902 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
903 		return (error);
904 	}
905 
906 	/*
907 	 * Initialize the zfs-specific filesystem structure.
908 	 * Should probably make this a kmem cache, shuffle fields,
909 	 * and just bzero up to z_hold_mtx[].
910 	 */
911 	zfsvfs->z_vfs = NULL;
912 	zfsvfs->z_parent = zfsvfs;
913 	zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
914 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
915 	zfsvfs->z_os = os;
916 
917 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
918 	if (error) {
919 		goto out;
920 	} else if (zfsvfs->z_version >
921 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
922 		(void) printf("Can't mount a version %lld file system "
923 		    "on a version %lld pool\n. Pool must be upgraded to mount "
924 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
925 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
926 		error = SET_ERROR(ENOTSUP);
927 		goto out;
928 	}
929 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
930 		goto out;
931 	zfsvfs->z_norm = (int)zval;
932 
933 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
934 		goto out;
935 	zfsvfs->z_utf8 = (zval != 0);
936 
937 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
938 		goto out;
939 	zfsvfs->z_case = (uint_t)zval;
940 
941 	/*
942 	 * Fold case on file systems that are always or sometimes case
943 	 * insensitive.
944 	 */
945 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
946 	    zfsvfs->z_case == ZFS_CASE_MIXED)
947 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
948 
949 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
950 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
951 
952 	if (zfsvfs->z_use_sa) {
953 		/* should either have both of these objects or none */
954 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
955 		    &sa_obj);
956 		if (error)
957 			goto out;
958 	} else {
959 		/*
960 		 * Pre SA versions file systems should never touch
961 		 * either the attribute registration or layout objects.
962 		 */
963 		sa_obj = 0;
964 	}
965 
966 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
967 	    &zfsvfs->z_attr_table);
968 	if (error)
969 		goto out;
970 
971 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
972 		sa_register_update_callback(os, zfs_sa_upgrade);
973 
974 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
975 	    &zfsvfs->z_root);
976 	if (error)
977 		goto out;
978 	ASSERT(zfsvfs->z_root != 0);
979 
980 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
981 	    &zfsvfs->z_unlinkedobj);
982 	if (error)
983 		goto out;
984 
985 	error = zap_lookup(os, MASTER_NODE_OBJ,
986 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
987 	    8, 1, &zfsvfs->z_userquota_obj);
988 	if (error && error != ENOENT)
989 		goto out;
990 
991 	error = zap_lookup(os, MASTER_NODE_OBJ,
992 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
993 	    8, 1, &zfsvfs->z_groupquota_obj);
994 	if (error && error != ENOENT)
995 		goto out;
996 
997 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
998 	    &zfsvfs->z_fuid_obj);
999 	if (error && error != ENOENT)
1000 		goto out;
1001 
1002 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
1003 	    &zfsvfs->z_shares_dir);
1004 	if (error && error != ENOENT)
1005 		goto out;
1006 
1007 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1008 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
1009 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1010 	    offsetof(znode_t, z_link_node));
1011 	rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
1012 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
1013 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
1014 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1015 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1016 
1017 	*zfvp = zfsvfs;
1018 	return (0);
1019 
1020 out:
1021 	dmu_objset_disown(os, zfsvfs);
1022 	*zfvp = NULL;
1023 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1024 	return (error);
1025 }
1026 
1027 static int
zfsvfs_setup(zfsvfs_t * zfsvfs,boolean_t mounting)1028 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1029 {
1030 	int error;
1031 
1032 	error = zfs_register_callbacks(zfsvfs->z_vfs);
1033 	if (error)
1034 		return (error);
1035 
1036 	/*
1037 	 * Set the objset user_ptr to track its zfsvfs.
1038 	 */
1039 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1040 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1041 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
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 		/*
1054 		 * During replay we remove the read only flag to
1055 		 * allow replays to succeed.
1056 		 */
1057 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1058 		if (readonly != 0)
1059 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1060 		else
1061 			zfs_unlinked_drain(zfsvfs);
1062 
1063 		/*
1064 		 * Parse and replay the intent log.
1065 		 *
1066 		 * Because of ziltest, this must be done after
1067 		 * zfs_unlinked_drain().  (Further note: ziltest
1068 		 * doesn't use readonly mounts, where
1069 		 * zfs_unlinked_drain() isn't called.)  This is because
1070 		 * ziltest causes spa_sync() to think it's committed,
1071 		 * but actually it is not, so the intent log contains
1072 		 * many txg's worth of changes.
1073 		 *
1074 		 * In particular, if object N is in the unlinked set in
1075 		 * the last txg to actually sync, then it could be
1076 		 * actually freed in a later txg and then reallocated
1077 		 * in a yet later txg.  This would write a "create
1078 		 * object N" record to the intent log.  Normally, this
1079 		 * would be fine because the spa_sync() would have
1080 		 * written out the fact that object N is free, before
1081 		 * we could write the "create object N" intent log
1082 		 * record.
1083 		 *
1084 		 * But when we are in ziltest mode, we advance the "open
1085 		 * txg" without actually spa_sync()-ing the changes to
1086 		 * disk.  So we would see that object N is still
1087 		 * allocated and in the unlinked set, and there is an
1088 		 * intent log record saying to allocate it.
1089 		 */
1090 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1091 			if (zil_replay_disable) {
1092 				zil_destroy(zfsvfs->z_log, B_FALSE);
1093 			} else {
1094 				zfsvfs->z_replay = B_TRUE;
1095 				zil_replay(zfsvfs->z_os, zfsvfs,
1096 				    zfs_replay_vector);
1097 				zfsvfs->z_replay = B_FALSE;
1098 			}
1099 		}
1100 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1101 	}
1102 
1103 	return (0);
1104 }
1105 
1106 void
zfsvfs_free(zfsvfs_t * zfsvfs)1107 zfsvfs_free(zfsvfs_t *zfsvfs)
1108 {
1109 	int i;
1110 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1111 
1112 	/*
1113 	 * This is a barrier to prevent the filesystem from going away in
1114 	 * zfs_znode_move() until we can safely ensure that the filesystem is
1115 	 * not unmounted. We consider the filesystem valid before the barrier
1116 	 * and invalid after the barrier.
1117 	 */
1118 	rw_enter(&zfsvfs_lock, RW_READER);
1119 	rw_exit(&zfsvfs_lock);
1120 
1121 	zfs_fuid_destroy(zfsvfs);
1122 
1123 	mutex_destroy(&zfsvfs->z_znodes_lock);
1124 	mutex_destroy(&zfsvfs->z_lock);
1125 	list_destroy(&zfsvfs->z_all_znodes);
1126 	rrm_destroy(&zfsvfs->z_teardown_lock);
1127 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1128 	rw_destroy(&zfsvfs->z_fuid_lock);
1129 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1130 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1131 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1132 }
1133 
1134 static void
zfs_set_fuid_feature(zfsvfs_t * zfsvfs)1135 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1136 {
1137 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1138 	if (zfsvfs->z_vfs) {
1139 		if (zfsvfs->z_use_fuids) {
1140 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1141 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1142 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1143 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1144 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1145 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1146 		} else {
1147 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1148 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1149 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1150 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1151 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1152 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1153 		}
1154 	}
1155 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1156 }
1157 
1158 static int
zfs_domount(vfs_t * vfsp,char * osname)1159 zfs_domount(vfs_t *vfsp, char *osname)
1160 {
1161 	dev_t mount_dev;
1162 	uint64_t recordsize, fsid_guid;
1163 	int error = 0;
1164 	zfsvfs_t *zfsvfs;
1165 
1166 	ASSERT(vfsp);
1167 	ASSERT(osname);
1168 
1169 	error = zfsvfs_create(osname, &zfsvfs);
1170 	if (error)
1171 		return (error);
1172 	zfsvfs->z_vfs = vfsp;
1173 
1174 	/* Initialize the generic filesystem structure. */
1175 	vfsp->vfs_bcount = 0;
1176 	vfsp->vfs_data = NULL;
1177 
1178 	if (zfs_create_unique_device(&mount_dev) == -1) {
1179 		error = SET_ERROR(ENODEV);
1180 		goto out;
1181 	}
1182 	ASSERT(vfs_devismounted(mount_dev) == 0);
1183 
1184 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1185 	    NULL))
1186 		goto out;
1187 
1188 	vfsp->vfs_dev = mount_dev;
1189 	vfsp->vfs_fstype = zfsfstype;
1190 	vfsp->vfs_bsize = recordsize;
1191 	vfsp->vfs_flag |= VFS_NOTRUNC;
1192 	vfsp->vfs_data = zfsvfs;
1193 
1194 	/*
1195 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
1196 	 * separates our fsid from any other filesystem types, and a
1197 	 * 56-bit objset unique ID.  The objset unique ID is unique to
1198 	 * all objsets open on this system, provided by unique_create().
1199 	 * The 8-bit fs type must be put in the low bits of fsid[1]
1200 	 * because that's where other Solaris filesystems put it.
1201 	 */
1202 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1203 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1204 	vfsp->vfs_fsid.val[0] = fsid_guid;
1205 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1206 	    zfsfstype & 0xFF;
1207 
1208 	/*
1209 	 * Set features for file system.
1210 	 */
1211 	zfs_set_fuid_feature(zfsvfs);
1212 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1213 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1214 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1215 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1216 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1217 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1218 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1219 	}
1220 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1221 
1222 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1223 		uint64_t pval;
1224 
1225 		atime_changed_cb(zfsvfs, B_FALSE);
1226 		readonly_changed_cb(zfsvfs, B_TRUE);
1227 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1228 			goto out;
1229 		xattr_changed_cb(zfsvfs, pval);
1230 		zfsvfs->z_issnap = B_TRUE;
1231 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1232 
1233 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1234 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1235 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1236 	} else {
1237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
1238 	}
1239 
1240 	if (!zfsvfs->z_issnap)
1241 		zfsctl_create(zfsvfs);
1242 out:
1243 	if (error) {
1244 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1245 		zfsvfs_free(zfsvfs);
1246 	} else {
1247 		atomic_inc_32(&zfs_active_fs_count);
1248 	}
1249 
1250 	return (error);
1251 }
1252 
1253 void
zfs_unregister_callbacks(zfsvfs_t * zfsvfs)1254 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1255 {
1256 	objset_t *os = zfsvfs->z_os;
1257 
1258 	if (!dmu_objset_is_snapshot(os))
1259 		dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1260 }
1261 
1262 /*
1263  * Convert a decimal digit string to a uint64_t integer.
1264  */
1265 static int
str_to_uint64(char * str,uint64_t * objnum)1266 str_to_uint64(char *str, uint64_t *objnum)
1267 {
1268 	uint64_t num = 0;
1269 
1270 	while (*str) {
1271 		if (*str < '0' || *str > '9')
1272 			return (SET_ERROR(EINVAL));
1273 
1274 		num = num*10 + *str++ - '0';
1275 	}
1276 
1277 	*objnum = num;
1278 	return (0);
1279 }
1280 
1281 /*
1282  * The boot path passed from the boot loader is in the form of
1283  * "rootpool-name/root-filesystem-object-number'. Convert this
1284  * string to a dataset name: "rootpool-name/root-filesystem-name".
1285  */
1286 static int
zfs_parse_bootfs(char * bpath,char * outpath)1287 zfs_parse_bootfs(char *bpath, char *outpath)
1288 {
1289 	char *slashp;
1290 	uint64_t objnum;
1291 	int error;
1292 
1293 	if (*bpath == 0 || *bpath == '/')
1294 		return (SET_ERROR(EINVAL));
1295 
1296 	(void) strcpy(outpath, bpath);
1297 
1298 	slashp = strchr(bpath, '/');
1299 
1300 	/* if no '/', just return the pool name */
1301 	if (slashp == NULL) {
1302 		return (0);
1303 	}
1304 
1305 	/* if not a number, just return the root dataset name */
1306 	if (str_to_uint64(slashp+1, &objnum)) {
1307 		return (0);
1308 	}
1309 
1310 	*slashp = '\0';
1311 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1312 	*slashp = '/';
1313 
1314 	return (error);
1315 }
1316 
1317 /*
1318  * Check that the hex label string is appropriate for the dataset being
1319  * mounted into the global_zone proper.
1320  *
1321  * Return an error if the hex label string is not default or
1322  * admin_low/admin_high.  For admin_low labels, the corresponding
1323  * dataset must be readonly.
1324  */
1325 int
zfs_check_global_label(const char * dsname,const char * hexsl)1326 zfs_check_global_label(const char *dsname, const char *hexsl)
1327 {
1328 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1329 		return (0);
1330 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1331 		return (0);
1332 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1333 		/* must be readonly */
1334 		uint64_t rdonly;
1335 
1336 		if (dsl_prop_get_integer(dsname,
1337 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1338 			return (SET_ERROR(EACCES));
1339 		return (rdonly ? 0 : EACCES);
1340 	}
1341 	return (SET_ERROR(EACCES));
1342 }
1343 
1344 /*
1345  * Determine whether the mount is allowed according to MAC check.
1346  * by comparing (where appropriate) label of the dataset against
1347  * the label of the zone being mounted into.  If the dataset has
1348  * no label, create one.
1349  *
1350  * Returns 0 if access allowed, error otherwise (e.g. EACCES)
1351  */
1352 static int
zfs_mount_label_policy(vfs_t * vfsp,char * osname)1353 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1354 {
1355 	int		error, retv;
1356 	zone_t		*mntzone = NULL;
1357 	ts_label_t	*mnt_tsl;
1358 	bslabel_t	*mnt_sl;
1359 	bslabel_t	ds_sl;
1360 	char		ds_hexsl[MAXNAMELEN];
1361 
1362 	retv = EACCES;				/* assume the worst */
1363 
1364 	/*
1365 	 * Start by getting the dataset label if it exists.
1366 	 */
1367 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1368 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1369 	if (error)
1370 		return (SET_ERROR(EACCES));
1371 
1372 	/*
1373 	 * If labeling is NOT enabled, then disallow the mount of datasets
1374 	 * which have a non-default label already.  No other label checks
1375 	 * are needed.
1376 	 */
1377 	if (!is_system_labeled()) {
1378 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1379 			return (0);
1380 		return (SET_ERROR(EACCES));
1381 	}
1382 
1383 	/*
1384 	 * Get the label of the mountpoint.  If mounting into the global
1385 	 * zone (i.e. mountpoint is not within an active zone and the
1386 	 * zoned property is off), the label must be default or
1387 	 * admin_low/admin_high only; no other checks are needed.
1388 	 */
1389 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1390 	if (mntzone->zone_id == GLOBAL_ZONEID) {
1391 		uint64_t zoned;
1392 
1393 		zone_rele(mntzone);
1394 
1395 		if (dsl_prop_get_integer(osname,
1396 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1397 			return (SET_ERROR(EACCES));
1398 		if (!zoned)
1399 			return (zfs_check_global_label(osname, ds_hexsl));
1400 		else
1401 			/*
1402 			 * This is the case of a zone dataset being mounted
1403 			 * initially, before the zone has been fully created;
1404 			 * allow this mount into global zone.
1405 			 */
1406 			return (0);
1407 	}
1408 
1409 	mnt_tsl = mntzone->zone_slabel;
1410 	ASSERT(mnt_tsl != NULL);
1411 	label_hold(mnt_tsl);
1412 	mnt_sl = label2bslabel(mnt_tsl);
1413 
1414 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1415 		/*
1416 		 * The dataset doesn't have a real label, so fabricate one.
1417 		 */
1418 		char *str = NULL;
1419 
1420 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
1421 		    dsl_prop_set_string(osname,
1422 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1423 		    ZPROP_SRC_LOCAL, str) == 0)
1424 			retv = 0;
1425 		if (str != NULL)
1426 			kmem_free(str, strlen(str) + 1);
1427 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1428 		/*
1429 		 * Now compare labels to complete the MAC check.  If the
1430 		 * labels are equal then allow access.  If the mountpoint
1431 		 * label dominates the dataset label, allow readonly access.
1432 		 * Otherwise, access is denied.
1433 		 */
1434 		if (blequal(mnt_sl, &ds_sl))
1435 			retv = 0;
1436 		else if (bldominates(mnt_sl, &ds_sl)) {
1437 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1438 			retv = 0;
1439 		}
1440 	}
1441 
1442 	label_rele(mnt_tsl);
1443 	zone_rele(mntzone);
1444 	return (retv);
1445 }
1446 
1447 static int
zfs_mountroot(vfs_t * vfsp,enum whymountroot why)1448 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1449 {
1450 	int error = 0;
1451 	static int zfsrootdone = 0;
1452 	zfsvfs_t *zfsvfs = NULL;
1453 	znode_t *zp = NULL;
1454 	vnode_t *vp = NULL;
1455 	char *zfs_bootfs;
1456 	char *zfs_devid;
1457 
1458 	ASSERT(vfsp);
1459 
1460 	/*
1461 	 * The filesystem that we mount as root is defined in the
1462 	 * boot property "zfs-bootfs" with a format of
1463 	 * "poolname/root-dataset-objnum".
1464 	 */
1465 	if (why == ROOT_INIT) {
1466 		if (zfsrootdone++)
1467 			return (SET_ERROR(EBUSY));
1468 		/*
1469 		 * the process of doing a spa_load will require the
1470 		 * clock to be set before we could (for example) do
1471 		 * something better by looking at the timestamp on
1472 		 * an uberblock, so just set it to -1.
1473 		 */
1474 		clkset(-1);
1475 
1476 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1477 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1478 			    "bootfs name");
1479 			return (SET_ERROR(EINVAL));
1480 		}
1481 		zfs_devid = spa_get_bootprop("diskdevid");
1482 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1483 		if (zfs_devid)
1484 			spa_free_bootprop(zfs_devid);
1485 		if (error) {
1486 			spa_free_bootprop(zfs_bootfs);
1487 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1488 			    error);
1489 			return (error);
1490 		}
1491 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1492 			spa_free_bootprop(zfs_bootfs);
1493 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1494 			    error);
1495 			return (error);
1496 		}
1497 
1498 		spa_free_bootprop(zfs_bootfs);
1499 
1500 		if (error = vfs_lock(vfsp))
1501 			return (error);
1502 
1503 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1504 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1505 			goto out;
1506 		}
1507 
1508 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1509 		ASSERT(zfsvfs);
1510 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1511 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1512 			goto out;
1513 		}
1514 
1515 		vp = ZTOV(zp);
1516 		mutex_enter(&vp->v_lock);
1517 		vp->v_flag |= VROOT;
1518 		mutex_exit(&vp->v_lock);
1519 		rootvp = vp;
1520 
1521 		/*
1522 		 * Leave rootvp held.  The root file system is never unmounted.
1523 		 */
1524 
1525 		vfs_add((struct vnode *)0, vfsp,
1526 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1527 out:
1528 		vfs_unlock(vfsp);
1529 		return (error);
1530 	} else if (why == ROOT_REMOUNT) {
1531 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1532 		vfsp->vfs_flag |= VFS_REMOUNT;
1533 
1534 		/* refresh mount options */
1535 		zfs_unregister_callbacks(vfsp->vfs_data);
1536 		return (zfs_register_callbacks(vfsp));
1537 
1538 	} else if (why == ROOT_UNMOUNT) {
1539 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1540 		(void) zfs_sync(vfsp, 0, 0);
1541 		return (0);
1542 	}
1543 
1544 	/*
1545 	 * if "why" is equal to anything else other than ROOT_INIT,
1546 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1547 	 */
1548 	return (SET_ERROR(ENOTSUP));
1549 }
1550 
1551 /*ARGSUSED*/
1552 static int
zfs_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)1553 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1554 {
1555 	char		*osname;
1556 	pathname_t	spn;
1557 	int		error = 0;
1558 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
1559 	    UIO_SYSSPACE : UIO_USERSPACE;
1560 	int		canwrite;
1561 
1562 	if (mvp->v_type != VDIR)
1563 		return (SET_ERROR(ENOTDIR));
1564 
1565 	mutex_enter(&mvp->v_lock);
1566 	if ((uap->flags & MS_REMOUNT) == 0 &&
1567 	    (uap->flags & MS_OVERLAY) == 0 &&
1568 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1569 		mutex_exit(&mvp->v_lock);
1570 		return (SET_ERROR(EBUSY));
1571 	}
1572 	mutex_exit(&mvp->v_lock);
1573 
1574 	/*
1575 	 * ZFS does not support passing unparsed data in via MS_DATA.
1576 	 * Users should use the MS_OPTIONSTR interface; this means
1577 	 * that all option parsing is already done and the options struct
1578 	 * can be interrogated.
1579 	 */
1580 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1581 		return (SET_ERROR(EINVAL));
1582 
1583 	/*
1584 	 * Get the objset name (the "special" mount argument).
1585 	 */
1586 	if (error = pn_get(uap->spec, fromspace, &spn))
1587 		return (error);
1588 
1589 	osname = spn.pn_path;
1590 
1591 	/*
1592 	 * Check for mount privilege?
1593 	 *
1594 	 * If we don't have privilege then see if
1595 	 * we have local permission to allow it
1596 	 */
1597 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1598 	if (error) {
1599 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1600 			vattr_t		vattr;
1601 
1602 			/*
1603 			 * Make sure user is the owner of the mount point
1604 			 * or has sufficient privileges.
1605 			 */
1606 
1607 			vattr.va_mask = AT_UID;
1608 
1609 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1610 				goto out;
1611 			}
1612 
1613 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1614 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1615 				goto out;
1616 			}
1617 			secpolicy_fs_mount_clearopts(cr, vfsp);
1618 		} else {
1619 			goto out;
1620 		}
1621 	}
1622 
1623 	/*
1624 	 * Refuse to mount a filesystem if we are in a local zone and the
1625 	 * dataset is not visible.
1626 	 */
1627 	if (!INGLOBALZONE(curproc) &&
1628 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1629 		error = SET_ERROR(EPERM);
1630 		goto out;
1631 	}
1632 
1633 	error = zfs_mount_label_policy(vfsp, osname);
1634 	if (error)
1635 		goto out;
1636 
1637 	/*
1638 	 * When doing a remount, we simply refresh our temporary properties
1639 	 * according to those options set in the current VFS options.
1640 	 */
1641 	if (uap->flags & MS_REMOUNT) {
1642 		/* refresh mount options */
1643 		zfs_unregister_callbacks(vfsp->vfs_data);
1644 		error = zfs_register_callbacks(vfsp);
1645 		goto out;
1646 	}
1647 
1648 	error = zfs_domount(vfsp, osname);
1649 
1650 	/*
1651 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
1652 	 * disappear due to a forced unmount.
1653 	 */
1654 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1655 		VFS_HOLD(mvp->v_vfsp);
1656 
1657 out:
1658 	if (error == 0) {
1659 		rw_enter(&rz_zev_rwlock, RW_READER);
1660 		if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zfs_mount)
1661 			rz_zev_callbacks->rz_zev_zfs_mount(vfsp, mvp, osname,
1662 				uap->flags & MS_REMOUNT ?  B_TRUE : B_FALSE);
1663 		rw_exit(&rz_zev_rwlock);
1664 	}
1665 	pn_free(&spn);
1666 	return (error);
1667 }
1668 
1669 static int
zfs_statvfs(vfs_t * vfsp,struct statvfs64 * statp)1670 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1671 {
1672 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1673 	dev32_t d32;
1674 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1675 
1676 	ZFS_ENTER(zfsvfs);
1677 
1678 	dmu_objset_space(zfsvfs->z_os,
1679 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1680 
1681 	/*
1682 	 * The underlying storage pool actually uses multiple block sizes.
1683 	 * We report the fragsize as the smallest block size we support,
1684 	 * and we report our blocksize as the filesystem's maximum blocksize.
1685 	 */
1686 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1687 	statp->f_bsize = zfsvfs->z_max_blksz;
1688 
1689 	/*
1690 	 * The following report "total" blocks of various kinds in the
1691 	 * file system, but reported in terms of f_frsize - the
1692 	 * "fragment" size.
1693 	 */
1694 
1695 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1696 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1697 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1698 
1699 	/*
1700 	 * statvfs() should really be called statufs(), because it assumes
1701 	 * static metadata.  ZFS doesn't preallocate files, so the best
1702 	 * we can do is report the max that could possibly fit in f_files,
1703 	 * and that minus the number actually used in f_ffree.
1704 	 * For f_ffree, report the smaller of the number of object available
1705 	 * and the number of blocks (each object will take at least a block).
1706 	 */
1707 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1708 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
1709 	statp->f_files = statp->f_ffree + usedobjs;
1710 
1711 	(void) cmpldev(&d32, vfsp->vfs_dev);
1712 	statp->f_fsid = d32;
1713 
1714 	/*
1715 	 * We're a zfs filesystem.
1716 	 */
1717 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1718 
1719 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1720 
1721 	statp->f_namemax = MAXNAMELEN - 1;
1722 
1723 	/*
1724 	 * We have all of 32 characters to stuff a string here.
1725 	 * Is there anything useful we could/should provide?
1726 	 */
1727 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1728 
1729 	ZFS_EXIT(zfsvfs);
1730 	return (0);
1731 }
1732 
1733 static int
zfs_root(vfs_t * vfsp,vnode_t ** vpp)1734 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1735 {
1736 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1737 	znode_t *rootzp;
1738 	int error;
1739 
1740 	ZFS_ENTER(zfsvfs);
1741 
1742 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1743 	if (error == 0)
1744 		*vpp = ZTOV(rootzp);
1745 
1746 	ZFS_EXIT(zfsvfs);
1747 	return (error);
1748 }
1749 
1750 /*
1751  * Teardown the zfsvfs::z_os.
1752  *
1753  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1754  * and 'z_teardown_inactive_lock' held.
1755  */
1756 static int
zfsvfs_teardown(zfsvfs_t * zfsvfs,boolean_t unmounting)1757 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1758 {
1759 	znode_t	*zp;
1760 
1761 	rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1762 
1763 	if (!unmounting) {
1764 		/*
1765 		 * We purge the parent filesystem's vfsp as the parent
1766 		 * filesystem and all of its snapshots have their vnode's
1767 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1768 		 * 'z_parent' is self referential for non-snapshots.
1769 		 */
1770 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1771 	}
1772 
1773 	/*
1774 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1775 	 * threads are blocked as zil_close can call zfs_inactive.
1776 	 */
1777 	if (zfsvfs->z_log) {
1778 		zil_close(zfsvfs->z_log);
1779 		zfsvfs->z_log = NULL;
1780 	}
1781 
1782 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1783 
1784 	/*
1785 	 * If we are not unmounting (ie: online recv) and someone already
1786 	 * unmounted this file system while we were doing the switcheroo,
1787 	 * or a reopen of z_os failed then just bail out now.
1788 	 */
1789 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1790 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1791 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1792 		return (SET_ERROR(EIO));
1793 	}
1794 
1795 	/*
1796 	 * At this point there are no vops active, and any new vops will
1797 	 * fail with EIO since we have z_teardown_lock for writer (only
1798 	 * relavent for forced unmount).
1799 	 *
1800 	 * Release all holds on dbufs.
1801 	 */
1802 	mutex_enter(&zfsvfs->z_znodes_lock);
1803 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1804 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
1805 		if (zp->z_sa_hdl) {
1806 			ASSERT(ZTOV(zp)->v_count > 0);
1807 			zfs_znode_dmu_fini(zp);
1808 		}
1809 	mutex_exit(&zfsvfs->z_znodes_lock);
1810 
1811 	/*
1812 	 * If we are unmounting, set the unmounted flag and let new vops
1813 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1814 	 * other vops will fail with EIO.
1815 	 */
1816 	if (unmounting) {
1817 		zfsvfs->z_unmounted = B_TRUE;
1818 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1819 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1820 	}
1821 
1822 	/*
1823 	 * z_os will be NULL if there was an error in attempting to reopen
1824 	 * zfsvfs, so just return as the properties had already been
1825 	 * unregistered and cached data had been evicted before.
1826 	 */
1827 	if (zfsvfs->z_os == NULL)
1828 		return (0);
1829 
1830 	/*
1831 	 * Unregister properties.
1832 	 */
1833 	zfs_unregister_callbacks(zfsvfs);
1834 
1835 	/*
1836 	 * Evict cached data
1837 	 */
1838 	if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
1839 	    !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1840 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1841 	dmu_objset_evict_dbufs(zfsvfs->z_os);
1842 
1843 	return (0);
1844 }
1845 
1846 /*ARGSUSED*/
1847 static int
zfs_umount(vfs_t * vfsp,int fflag,cred_t * cr)1848 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1849 {
1850 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1851 	objset_t *os;
1852 	int ret;
1853 
1854 	ret = secpolicy_fs_unmount(cr, vfsp);
1855 	if (ret) {
1856 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1857 		    ZFS_DELEG_PERM_MOUNT, cr))
1858 			return (ret);
1859 	}
1860 
1861 	/*
1862 	 * We purge the parent filesystem's vfsp as the parent filesystem
1863 	 * and all of its snapshots have their vnode's v_vfsp set to the
1864 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1865 	 * referential for non-snapshots.
1866 	 */
1867 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1868 
1869 	/*
1870 	 * Unmount any snapshots mounted under .zfs before unmounting the
1871 	 * dataset itself.
1872 	 */
1873 	if (zfsvfs->z_ctldir != NULL &&
1874 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1875 		return (ret);
1876 	}
1877 
1878 	if (!(fflag & MS_FORCE)) {
1879 		/*
1880 		 * Check the number of active vnodes in the file system.
1881 		 * Our count is maintained in the vfs structure, but the
1882 		 * number is off by 1 to indicate a hold on the vfs
1883 		 * structure itself.
1884 		 *
1885 		 * The '.zfs' directory maintains a reference of its
1886 		 * own, and any active references underneath are
1887 		 * reflected in the vnode count.
1888 		 */
1889 		if (zfsvfs->z_ctldir == NULL) {
1890 			if (vfsp->vfs_count > 1)
1891 				return (SET_ERROR(EBUSY));
1892 		} else {
1893 			if (vfsp->vfs_count > 2 ||
1894 			    zfsvfs->z_ctldir->v_count > 1)
1895 				return (SET_ERROR(EBUSY));
1896 		}
1897 	}
1898 
1899 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1900 
1901 	rw_enter(&rz_zev_rwlock, RW_READER);
1902 	if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zfs_umount)
1903 		rz_zev_callbacks->rz_zev_zfs_umount(vfsp);
1904 	rw_exit(&rz_zev_rwlock);
1905 
1906 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1907 	os = zfsvfs->z_os;
1908 
1909 	/*
1910 	 * z_os will be NULL if there was an error in
1911 	 * attempting to reopen zfsvfs.
1912 	 */
1913 	if (os != NULL) {
1914 		/*
1915 		 * Unset the objset user_ptr.
1916 		 */
1917 		mutex_enter(&os->os_user_ptr_lock);
1918 		dmu_objset_set_user(os, NULL);
1919 		mutex_exit(&os->os_user_ptr_lock);
1920 
1921 		/*
1922 		 * Finally release the objset
1923 		 */
1924 		dmu_objset_disown(os, zfsvfs);
1925 	}
1926 
1927 	/*
1928 	 * We can now safely destroy the '.zfs' directory node.
1929 	 */
1930 	if (zfsvfs->z_ctldir != NULL)
1931 		zfsctl_destroy(zfsvfs);
1932 
1933 	return (0);
1934 }
1935 
1936 static int
zfs_vget(vfs_t * vfsp,vnode_t ** vpp,fid_t * fidp)1937 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1938 {
1939 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1940 	znode_t		*zp;
1941 	uint64_t	object = 0;
1942 	uint64_t	fid_gen = 0;
1943 	uint64_t	gen_mask;
1944 	uint64_t	zp_gen;
1945 	int 		i, err;
1946 
1947 	*vpp = NULL;
1948 
1949 	ZFS_ENTER(zfsvfs);
1950 
1951 	if (fidp->fid_len == LONG_FID_LEN) {
1952 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1953 		uint64_t	objsetid = 0;
1954 		uint64_t	setgen = 0;
1955 
1956 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1957 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1958 
1959 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1960 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1961 
1962 		ZFS_EXIT(zfsvfs);
1963 
1964 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1965 		if (err)
1966 			return (SET_ERROR(EINVAL));
1967 		ZFS_ENTER(zfsvfs);
1968 	}
1969 
1970 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1971 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1972 
1973 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1974 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1975 
1976 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1977 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1978 	} else {
1979 		ZFS_EXIT(zfsvfs);
1980 		return (SET_ERROR(EINVAL));
1981 	}
1982 
1983 	/* A zero fid_gen means we are in the .zfs control directories */
1984 	if (fid_gen == 0 &&
1985 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1986 		*vpp = zfsvfs->z_ctldir;
1987 		ASSERT(*vpp != NULL);
1988 		if (object == ZFSCTL_INO_SNAPDIR) {
1989 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1990 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1991 		} else {
1992 			VN_HOLD(*vpp);
1993 		}
1994 		ZFS_EXIT(zfsvfs);
1995 		return (0);
1996 	}
1997 
1998 	gen_mask = -1ULL >> (64 - 8 * i);
1999 
2000 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2001 	if (err = zfs_zget(zfsvfs, object, &zp)) {
2002 		ZFS_EXIT(zfsvfs);
2003 		return (err);
2004 	}
2005 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2006 	    sizeof (uint64_t));
2007 	zp_gen = zp_gen & gen_mask;
2008 	if (zp_gen == 0)
2009 		zp_gen = 1;
2010 	if (zp->z_unlinked || zp_gen != fid_gen) {
2011 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2012 		VN_RELE(ZTOV(zp));
2013 		ZFS_EXIT(zfsvfs);
2014 		return (SET_ERROR(EINVAL));
2015 	}
2016 
2017 	*vpp = ZTOV(zp);
2018 	ZFS_EXIT(zfsvfs);
2019 	return (0);
2020 }
2021 
2022 /*
2023  * Block out VOPs and close zfsvfs_t::z_os
2024  *
2025  * Note, if successful, then we return with the 'z_teardown_lock' and
2026  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
2027  * dataset and objset intact so that they can be atomically handed off during
2028  * a subsequent rollback or recv operation and the resume thereafter.
2029  */
2030 int
zfs_suspend_fs(zfsvfs_t * zfsvfs)2031 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2032 {
2033 	int error;
2034 
2035 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2036 		return (error);
2037 
2038 	return (0);
2039 }
2040 
2041 /*
2042  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
2043  * is an invariant across any of the operations that can be performed while the
2044  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
2045  * are the same: the relevant objset and associated dataset are owned by
2046  * zfsvfs, held, and long held on entry.
2047  */
2048 int
zfs_resume_fs(zfsvfs_t * zfsvfs,const char * osname)2049 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
2050 {
2051 	int err;
2052 	znode_t *zp;
2053 	uint64_t sa_obj = 0;
2054 
2055 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2056 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2057 
2058 	/*
2059 	 * We already own this, so just hold and rele it to update the
2060 	 * objset_t, as the one we had before may have been evicted.
2061 	 */
2062 	VERIFY0(dmu_objset_hold(osname, zfsvfs, &zfsvfs->z_os));
2063 	VERIFY3P(zfsvfs->z_os->os_dsl_dataset->ds_owner, ==, zfsvfs);
2064 	VERIFY(dsl_dataset_long_held(zfsvfs->z_os->os_dsl_dataset));
2065 	dmu_objset_rele(zfsvfs->z_os, zfsvfs);
2066 
2067 	/*
2068 	 * Make sure version hasn't changed
2069 	 */
2070 
2071 	err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION,
2072 	    &zfsvfs->z_version);
2073 
2074 	if (err)
2075 		goto bail;
2076 
2077 	err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
2078 	    ZFS_SA_ATTRS, 8, 1, &sa_obj);
2079 
2080 	if (err && zfsvfs->z_version >= ZPL_VERSION_SA)
2081 		goto bail;
2082 
2083 	if ((err = sa_setup(zfsvfs->z_os, sa_obj,
2084 	    zfs_attr_table,  ZPL_END, &zfsvfs->z_attr_table)) != 0)
2085 		goto bail;
2086 
2087 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
2088 		sa_register_update_callback(zfsvfs->z_os,
2089 		    zfs_sa_upgrade);
2090 
2091 	VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2092 
2093 	zfs_set_fuid_feature(zfsvfs);
2094 
2095 	/*
2096 	 * Attempt to re-establish all the active znodes with
2097 	 * their dbufs.  If a zfs_rezget() fails, then we'll let
2098 	 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2099 	 * when they try to use their znode.
2100 	 */
2101 	mutex_enter(&zfsvfs->z_znodes_lock);
2102 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2103 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2104 		(void) zfs_rezget(zp);
2105 	}
2106 	mutex_exit(&zfsvfs->z_znodes_lock);
2107 
2108 bail:
2109 	/* release the VOPs */
2110 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
2111 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2112 
2113 	if (err) {
2114 		/*
2115 		 * Since we couldn't setup the sa framework, try to force
2116 		 * unmount this file system.
2117 		 */
2118 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2119 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2120 	}
2121 	return (err);
2122 }
2123 
2124 static void
zfs_freevfs(vfs_t * vfsp)2125 zfs_freevfs(vfs_t *vfsp)
2126 {
2127 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2128 
2129 	/*
2130 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2131 	 * from zfs_mount().  Release it here.  If we came through
2132 	 * zfs_mountroot() instead, we didn't grab an extra hold, so
2133 	 * skip the VFS_RELE for rootvfs.
2134 	 */
2135 	if (zfsvfs->z_issnap && (vfsp != rootvfs))
2136 		VFS_RELE(zfsvfs->z_parent->z_vfs);
2137 
2138 	zfsvfs_free(zfsvfs);
2139 
2140 	atomic_dec_32(&zfs_active_fs_count);
2141 }
2142 
2143 /*
2144  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2145  * so we can't safely do any non-idempotent initialization here.
2146  * Leave that to zfs_init() and zfs_fini(), which are called
2147  * from the module's _init() and _fini() entry points.
2148  */
2149 /*ARGSUSED*/
2150 static int
zfs_vfsinit(int fstype,char * name)2151 zfs_vfsinit(int fstype, char *name)
2152 {
2153 	int error;
2154 
2155 	zfsfstype = fstype;
2156 
2157 	/*
2158 	 * Setup vfsops and vnodeops tables.
2159 	 */
2160 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2161 	if (error != 0) {
2162 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2163 	}
2164 
2165 	error = zfs_create_op_tables();
2166 	if (error) {
2167 		zfs_remove_op_tables();
2168 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2169 		(void) vfs_freevfsops_by_type(zfsfstype);
2170 		return (error);
2171 	}
2172 
2173 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2174 
2175 	/*
2176 	 * Unique major number for all zfs mounts.
2177 	 * If we run out of 32-bit minors, we'll getudev() another major.
2178 	 */
2179 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2180 	zfs_minor = ZFS_MIN_MINOR;
2181 
2182 	return (0);
2183 }
2184 
2185 void
zfs_init(void)2186 zfs_init(void)
2187 {
2188 	/*
2189 	 * Initialize .zfs directory structures
2190 	 */
2191 	zfsctl_init();
2192 
2193 	/*
2194 	 * Initialize znode cache, vnode ops, etc...
2195 	 */
2196 	zfs_znode_init();
2197 
2198 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2199 }
2200 
2201 void
zfs_fini(void)2202 zfs_fini(void)
2203 {
2204 	zfsctl_fini();
2205 	zfs_znode_fini();
2206 }
2207 
2208 int
zfs_busy(void)2209 zfs_busy(void)
2210 {
2211 	return (zfs_active_fs_count != 0);
2212 }
2213 
2214 int
zfs_set_version(zfsvfs_t * zfsvfs,uint64_t newvers)2215 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2216 {
2217 	int error;
2218 	objset_t *os = zfsvfs->z_os;
2219 	dmu_tx_t *tx;
2220 
2221 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2222 		return (SET_ERROR(EINVAL));
2223 
2224 	if (newvers < zfsvfs->z_version)
2225 		return (SET_ERROR(EINVAL));
2226 
2227 	if (zfs_spa_version_map(newvers) >
2228 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
2229 		return (SET_ERROR(ENOTSUP));
2230 
2231 	tx = dmu_tx_create(os);
2232 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2233 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2234 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2235 		    ZFS_SA_ATTRS);
2236 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2237 	}
2238 	error = dmu_tx_assign(tx, TXG_WAIT);
2239 	if (error) {
2240 		dmu_tx_abort(tx);
2241 		return (error);
2242 	}
2243 
2244 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2245 	    8, 1, &newvers, tx);
2246 
2247 	if (error) {
2248 		dmu_tx_commit(tx);
2249 		return (error);
2250 	}
2251 
2252 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2253 		uint64_t sa_obj;
2254 
2255 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2256 		    SPA_VERSION_SA);
2257 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2258 		    DMU_OT_NONE, 0, tx);
2259 
2260 		error = zap_add(os, MASTER_NODE_OBJ,
2261 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2262 		ASSERT0(error);
2263 
2264 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
2265 		sa_register_update_callback(os, zfs_sa_upgrade);
2266 	}
2267 
2268 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2269 	    "from %llu to %llu", zfsvfs->z_version, newvers);
2270 
2271 	dmu_tx_commit(tx);
2272 
2273 	zfsvfs->z_version = newvers;
2274 
2275 	zfs_set_fuid_feature(zfsvfs);
2276 
2277 	return (0);
2278 }
2279 
2280 /*
2281  * Read a property stored within the master node.
2282  */
2283 int
zfs_get_zplprop(objset_t * os,zfs_prop_t prop,uint64_t * value)2284 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2285 {
2286 	const char *pname;
2287 	int error = ENOENT;
2288 
2289 	/*
2290 	 * Look up the file system's value for the property.  For the
2291 	 * version property, we look up a slightly different string.
2292 	 */
2293 	if (prop == ZFS_PROP_VERSION)
2294 		pname = ZPL_VERSION_STR;
2295 	else
2296 		pname = zfs_prop_to_name(prop);
2297 
2298 	if (os != NULL)
2299 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2300 
2301 	if (error == ENOENT) {
2302 		/* No value set, use the default value */
2303 		switch (prop) {
2304 		case ZFS_PROP_VERSION:
2305 			*value = ZPL_VERSION;
2306 			break;
2307 		case ZFS_PROP_NORMALIZE:
2308 		case ZFS_PROP_UTF8ONLY:
2309 			*value = 0;
2310 			break;
2311 		case ZFS_PROP_CASE:
2312 			*value = ZFS_CASE_SENSITIVE;
2313 			break;
2314 		default:
2315 			return (error);
2316 		}
2317 		error = 0;
2318 	}
2319 	return (error);
2320 }
2321 
2322 static vfsdef_t vfw = {
2323 	VFSDEF_VERSION,
2324 	MNTTYPE_ZFS,
2325 	zfs_vfsinit,
2326 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
2327 	    VSW_XID|VSW_ZMOUNT,
2328 	&zfs_mntopts
2329 };
2330 
2331 struct modlfs zfs_modlfs = {
2332 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2333 };
2334