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