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