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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Portions Copyright 2010 Robert Milkowski */
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/sysmacros.h>
34 #include <sys/vnode.h>
36 #include <sys/mntent.h>
37 #include <sys/mount.h>
38 #include <sys/cmn_err.h>
39 #include <sys/zfs_znode.h>
40 #include <sys/zfs_dir.h>
42 #include <sys/fs/zfs.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dsl_dataset.h>
46 #include <sys/dsl_deleg.h>
50 #include <sys/varargs.h>
51 #include <sys/policy.h>
52 #include <sys/atomic.h>
53 #include <sys/zfs_ioctl.h>
54 #include <sys/zfs_ctldir.h>
55 #include <sys/zfs_fuid.h>
56 #include <sys/sunddi.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/spa_boot.h>
61 #include "zfs_comutil.h"
63 struct mtx zfs_debug_mtx;
64 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
66 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
69 SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
70 "File system owner can perform privileged operation on his file systems");
73 TUNABLE_INT("vfs.zfs.debug", &zfs_debug_level);
74 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RW, &zfs_debug_level, 0,
77 SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
78 static int zfs_version_acl = ZFS_ACL_VERSION;
79 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
81 static int zfs_version_spa = SPA_VERSION;
82 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
84 static int zfs_version_zpl = ZPL_VERSION;
85 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
88 static int zfs_mount(vfs_t *vfsp);
89 static int zfs_umount(vfs_t *vfsp, int fflag);
90 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
91 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
92 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
93 static int zfs_sync(vfs_t *vfsp, int waitfor);
94 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
95 struct ucred **credanonp, int *numsecflavors, int **secflavors);
96 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp);
97 static void zfs_objset_close(zfsvfs_t *zfsvfs);
98 static void zfs_freevfs(vfs_t *vfsp);
100 static struct vfsops zfs_vfsops = {
101 .vfs_mount = zfs_mount,
102 .vfs_unmount = zfs_umount,
103 .vfs_root = zfs_root,
104 .vfs_statfs = zfs_statfs,
105 .vfs_vget = zfs_vget,
106 .vfs_sync = zfs_sync,
107 .vfs_checkexp = zfs_checkexp,
108 .vfs_fhtovp = zfs_fhtovp,
111 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
114 * We need to keep a count of active fs's.
115 * This is necessary to prevent our module
116 * from being unloaded after a umount -f
118 static uint32_t zfs_active_fs_count = 0;
122 zfs_sync(vfs_t *vfsp, int waitfor)
126 * Data integrity is job one. We don't want a compromised kernel
127 * writing to the storage pool, so we never sync during panic.
134 * Sync a specific filesystem.
136 zfsvfs_t *zfsvfs = vfsp->vfs_data;
140 error = vfs_stdsync(vfsp, waitfor);
145 dp = dmu_objset_pool(zfsvfs->z_os);
148 * If the system is shutting down, then skip any
149 * filesystems which may exist on a suspended pool.
151 if (sys_shutdown && spa_suspended(dp->dp_spa)) {
156 if (zfsvfs->z_log != NULL)
157 zil_commit(zfsvfs->z_log, 0);
162 * Sync all ZFS filesystems. This is what happens when you
163 * run sync(1M). Unlike other filesystems, ZFS honors the
164 * request by waiting for all pools to commit all dirty data.
174 zfs_create_unique_device(dev_t *dev)
179 ASSERT3U(zfs_minor, <=, MAXMIN32);
180 minor_t start = zfs_minor;
182 mutex_enter(&zfs_dev_mtx);
183 if (zfs_minor >= MAXMIN32) {
185 * If we're still using the real major
186 * keep out of /dev/zfs and /dev/zvol minor
187 * number space. If we're using a getudev()'ed
188 * major number, we can use all of its minors.
190 if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
191 zfs_minor = ZFS_MIN_MINOR;
197 *dev = makedevice(zfs_major, zfs_minor);
198 mutex_exit(&zfs_dev_mtx);
199 } while (vfs_devismounted(*dev) && zfs_minor != start);
200 if (zfs_minor == start) {
202 * We are using all ~262,000 minor numbers for the
203 * current major number. Create a new major number.
205 if ((new_major = getudev()) == (major_t)-1) {
207 "zfs_mount: Can't get unique major "
211 mutex_enter(&zfs_dev_mtx);
212 zfs_major = new_major;
215 mutex_exit(&zfs_dev_mtx);
219 /* CONSTANTCONDITION */
224 #endif /* !__FreeBSD__ */
227 atime_changed_cb(void *arg, uint64_t newval)
229 zfsvfs_t *zfsvfs = arg;
231 if (newval == TRUE) {
232 zfsvfs->z_atime = TRUE;
233 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
234 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
235 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
237 zfsvfs->z_atime = FALSE;
238 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
239 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
240 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
245 xattr_changed_cb(void *arg, uint64_t newval)
247 zfsvfs_t *zfsvfs = arg;
249 if (newval == TRUE) {
250 /* XXX locking on vfs_flag? */
252 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
254 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
255 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
257 /* XXX locking on vfs_flag? */
259 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
261 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
262 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
267 blksz_changed_cb(void *arg, uint64_t newval)
269 zfsvfs_t *zfsvfs = arg;
271 if (newval < SPA_MINBLOCKSIZE ||
272 newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
273 newval = SPA_MAXBLOCKSIZE;
275 zfsvfs->z_max_blksz = newval;
276 zfsvfs->z_vfs->mnt_stat.f_iosize = newval;
280 readonly_changed_cb(void *arg, uint64_t newval)
282 zfsvfs_t *zfsvfs = arg;
285 /* XXX locking on vfs_flag? */
286 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
287 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
288 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
290 /* XXX locking on vfs_flag? */
291 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
292 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
293 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
298 setuid_changed_cb(void *arg, uint64_t newval)
300 zfsvfs_t *zfsvfs = arg;
302 if (newval == FALSE) {
303 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
304 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
305 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
307 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
308 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
309 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
314 exec_changed_cb(void *arg, uint64_t newval)
316 zfsvfs_t *zfsvfs = arg;
318 if (newval == FALSE) {
319 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
320 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
321 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
323 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
324 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
325 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
330 * The nbmand mount option can be changed at mount time.
331 * We can't allow it to be toggled on live file systems or incorrect
332 * behavior may be seen from cifs clients
334 * This property isn't registered via dsl_prop_register(), but this callback
335 * will be called when a file system is first mounted
338 nbmand_changed_cb(void *arg, uint64_t newval)
340 zfsvfs_t *zfsvfs = arg;
341 if (newval == FALSE) {
342 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
343 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
345 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
346 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
351 snapdir_changed_cb(void *arg, uint64_t newval)
353 zfsvfs_t *zfsvfs = arg;
355 zfsvfs->z_show_ctldir = newval;
359 vscan_changed_cb(void *arg, uint64_t newval)
361 zfsvfs_t *zfsvfs = arg;
363 zfsvfs->z_vscan = newval;
367 acl_inherit_changed_cb(void *arg, uint64_t newval)
369 zfsvfs_t *zfsvfs = arg;
371 zfsvfs->z_acl_inherit = newval;
375 zfs_register_callbacks(vfs_t *vfsp)
377 struct dsl_dataset *ds = NULL;
379 zfsvfs_t *zfsvfs = NULL;
381 int readonly, do_readonly = B_FALSE;
382 int setuid, do_setuid = B_FALSE;
383 int exec, do_exec = B_FALSE;
384 int xattr, do_xattr = B_FALSE;
385 int atime, do_atime = B_FALSE;
389 zfsvfs = vfsp->vfs_data;
394 * This function can be called for a snapshot when we update snapshot's
395 * mount point, which isn't really supported.
397 if (dmu_objset_is_snapshot(os))
401 * The act of registering our callbacks will destroy any mount
402 * options we may have. In order to enable temporary overrides
403 * of mount options, we stash away the current values and
404 * restore them after we register the callbacks.
406 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
407 !spa_writeable(dmu_objset_spa(os))) {
409 do_readonly = B_TRUE;
410 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
412 do_readonly = B_TRUE;
414 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
418 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
421 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
426 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
429 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
433 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
436 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
440 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
443 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
449 * nbmand is a special property. It can only be changed at
452 * This is weird, but it is documented to only be changeable
455 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
457 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
460 char osname[MAXNAMELEN];
462 dmu_objset_name(os, osname);
463 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
470 * Register property callbacks.
472 * It would probably be fine to just check for i/o error from
473 * the first prop_register(), but I guess I like to go
476 ds = dmu_objset_ds(os);
477 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
478 error = error ? error : dsl_prop_register(ds,
479 "xattr", xattr_changed_cb, zfsvfs);
480 error = error ? error : dsl_prop_register(ds,
481 "recordsize", blksz_changed_cb, zfsvfs);
482 error = error ? error : dsl_prop_register(ds,
483 "readonly", readonly_changed_cb, zfsvfs);
484 error = error ? error : dsl_prop_register(ds,
485 "setuid", setuid_changed_cb, zfsvfs);
486 error = error ? error : dsl_prop_register(ds,
487 "exec", exec_changed_cb, zfsvfs);
488 error = error ? error : dsl_prop_register(ds,
489 "snapdir", snapdir_changed_cb, zfsvfs);
490 error = error ? error : dsl_prop_register(ds,
491 "aclinherit", acl_inherit_changed_cb, zfsvfs);
492 error = error ? error : dsl_prop_register(ds,
493 "vscan", vscan_changed_cb, zfsvfs);
498 * Invoke our callbacks to restore temporary mount options.
501 readonly_changed_cb(zfsvfs, readonly);
503 setuid_changed_cb(zfsvfs, setuid);
505 exec_changed_cb(zfsvfs, exec);
507 xattr_changed_cb(zfsvfs, xattr);
509 atime_changed_cb(zfsvfs, atime);
511 nbmand_changed_cb(zfsvfs, nbmand);
517 * We may attempt to unregister some callbacks that are not
518 * registered, but this is OK; it will simply return ENOMSG,
519 * which we will ignore.
521 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
522 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
523 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
524 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
525 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
526 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
527 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
528 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
530 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
536 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
537 uint64_t *userp, uint64_t *groupp)
539 znode_phys_t *znp = data;
543 * Is it a valid type of object to track?
545 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
549 * If we have a NULL data pointer
550 * then assume the id's aren't changing and
551 * return EEXIST to the dmu to let it know to
557 if (bonustype == DMU_OT_ZNODE) {
558 *userp = znp->zp_uid;
559 *groupp = znp->zp_gid;
563 ASSERT(bonustype == DMU_OT_SA);
564 hdrsize = sa_hdrsize(data);
567 *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
569 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
573 * This should only happen for newly created
574 * files that haven't had the znode data filled
585 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
586 char *domainbuf, int buflen, uid_t *ridp)
591 fuid = strtonum(fuidstr, NULL);
593 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
595 (void) strlcpy(domainbuf, domain, buflen);
598 *ridp = FUID_RID(fuid);
602 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
605 case ZFS_PROP_USERUSED:
606 return (DMU_USERUSED_OBJECT);
607 case ZFS_PROP_GROUPUSED:
608 return (DMU_GROUPUSED_OBJECT);
609 case ZFS_PROP_USERQUOTA:
610 return (zfsvfs->z_userquota_obj);
611 case ZFS_PROP_GROUPQUOTA:
612 return (zfsvfs->z_groupquota_obj);
618 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
619 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
624 zfs_useracct_t *buf = vbuf;
627 if (!dmu_objset_userspace_present(zfsvfs->z_os))
630 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
636 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
637 (error = zap_cursor_retrieve(&zc, &za)) == 0;
638 zap_cursor_advance(&zc)) {
639 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
643 fuidstr_to_sid(zfsvfs, za.za_name,
644 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
646 buf->zu_space = za.za_first_integer;
652 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
653 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
654 *cookiep = zap_cursor_serialize(&zc);
655 zap_cursor_fini(&zc);
660 * buf must be big enough (eg, 32 bytes)
663 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
664 char *buf, boolean_t addok)
669 if (domain && domain[0]) {
670 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
674 fuid = FUID_ENCODE(domainid, rid);
675 (void) sprintf(buf, "%llx", (longlong_t)fuid);
680 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
681 const char *domain, uint64_t rid, uint64_t *valp)
689 if (!dmu_objset_userspace_present(zfsvfs->z_os))
692 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
696 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
700 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
707 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
708 const char *domain, uint64_t rid, uint64_t quota)
714 boolean_t fuid_dirtied;
716 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
719 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
722 objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
723 &zfsvfs->z_groupquota_obj;
725 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
728 fuid_dirtied = zfsvfs->z_fuid_dirty;
730 tx = dmu_tx_create(zfsvfs->z_os);
731 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
733 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
734 zfs_userquota_prop_prefixes[type]);
737 zfs_fuid_txhold(zfsvfs, tx);
738 err = dmu_tx_assign(tx, TXG_WAIT);
744 mutex_enter(&zfsvfs->z_lock);
746 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
748 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
749 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
751 mutex_exit(&zfsvfs->z_lock);
754 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
758 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, "a, tx);
762 zfs_fuid_sync(zfsvfs, tx);
768 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
771 uint64_t used, quota, usedobj, quotaobj;
774 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
775 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
777 if (quotaobj == 0 || zfsvfs->z_replay)
780 (void) sprintf(buf, "%llx", (longlong_t)fuid);
781 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, "a);
785 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
788 return (used >= quota);
792 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
797 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
799 fuid = isgroup ? zp->z_gid : zp->z_uid;
801 if (quotaobj == 0 || zfsvfs->z_replay)
804 return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
808 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
816 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
819 * We claim to always be readonly so we can open snapshots;
820 * other ZPL code will prevent us from writing to snapshots.
822 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
824 kmem_free(zfsvfs, sizeof (zfsvfs_t));
829 * Initialize the zfs-specific filesystem structure.
830 * Should probably make this a kmem cache, shuffle fields,
831 * and just bzero up to z_hold_mtx[].
833 zfsvfs->z_vfs = NULL;
834 zfsvfs->z_parent = zfsvfs;
835 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
836 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
839 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
842 } else if (zfsvfs->z_version >
843 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
844 (void) printf("Can't mount a version %lld file system "
845 "on a version %lld pool\n. Pool must be upgraded to mount "
846 "this file system.", (u_longlong_t)zfsvfs->z_version,
847 (u_longlong_t)spa_version(dmu_objset_spa(os)));
851 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
853 zfsvfs->z_norm = (int)zval;
855 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
857 zfsvfs->z_utf8 = (zval != 0);
859 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
861 zfsvfs->z_case = (uint_t)zval;
864 * Fold case on file systems that are always or sometimes case
867 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
868 zfsvfs->z_case == ZFS_CASE_MIXED)
869 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
871 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
872 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
874 if (zfsvfs->z_use_sa) {
875 /* should either have both of these objects or none */
876 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
882 * Pre SA versions file systems should never touch
883 * either the attribute registration or layout objects.
888 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
889 &zfsvfs->z_attr_table);
893 if (zfsvfs->z_version >= ZPL_VERSION_SA)
894 sa_register_update_callback(os, zfs_sa_upgrade);
896 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
900 ASSERT(zfsvfs->z_root != 0);
902 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
903 &zfsvfs->z_unlinkedobj);
907 error = zap_lookup(os, MASTER_NODE_OBJ,
908 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
909 8, 1, &zfsvfs->z_userquota_obj);
910 if (error && error != ENOENT)
913 error = zap_lookup(os, MASTER_NODE_OBJ,
914 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
915 8, 1, &zfsvfs->z_groupquota_obj);
916 if (error && error != ENOENT)
919 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
920 &zfsvfs->z_fuid_obj);
921 if (error && error != ENOENT)
924 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
925 &zfsvfs->z_shares_dir);
926 if (error && error != ENOENT)
929 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
930 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
931 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
932 offsetof(znode_t, z_link_node));
933 rrw_init(&zfsvfs->z_teardown_lock);
934 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
935 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
936 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
937 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
943 dmu_objset_disown(os, zfsvfs);
945 kmem_free(zfsvfs, sizeof (zfsvfs_t));
950 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
954 error = zfs_register_callbacks(zfsvfs->z_vfs);
959 * Set the objset user_ptr to track its zfsvfs.
961 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
962 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
963 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
965 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
968 * If we are not mounting (ie: online recv), then we don't
969 * have to worry about replaying the log as we blocked all
970 * operations out since we closed the ZIL.
976 * During replay we remove the read only flag to
977 * allow replays to succeed.
979 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
981 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
983 zfs_unlinked_drain(zfsvfs);
986 * Parse and replay the intent log.
988 * Because of ziltest, this must be done after
989 * zfs_unlinked_drain(). (Further note: ziltest
990 * doesn't use readonly mounts, where
991 * zfs_unlinked_drain() isn't called.) This is because
992 * ziltest causes spa_sync() to think it's committed,
993 * but actually it is not, so the intent log contains
994 * many txg's worth of changes.
996 * In particular, if object N is in the unlinked set in
997 * the last txg to actually sync, then it could be
998 * actually freed in a later txg and then reallocated
999 * in a yet later txg. This would write a "create
1000 * object N" record to the intent log. Normally, this
1001 * would be fine because the spa_sync() would have
1002 * written out the fact that object N is free, before
1003 * we could write the "create object N" intent log
1006 * But when we are in ziltest mode, we advance the "open
1007 * txg" without actually spa_sync()-ing the changes to
1008 * disk. So we would see that object N is still
1009 * allocated and in the unlinked set, and there is an
1010 * intent log record saying to allocate it.
1012 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1013 if (zil_replay_disable) {
1014 zil_destroy(zfsvfs->z_log, B_FALSE);
1016 zfsvfs->z_replay = B_TRUE;
1017 zil_replay(zfsvfs->z_os, zfsvfs,
1019 zfsvfs->z_replay = B_FALSE;
1022 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1028 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1031 zfsvfs_free(zfsvfs_t *zfsvfs)
1036 * This is a barrier to prevent the filesystem from going away in
1037 * zfs_znode_move() until we can safely ensure that the filesystem is
1038 * not unmounted. We consider the filesystem valid before the barrier
1039 * and invalid after the barrier.
1041 rw_enter(&zfsvfs_lock, RW_READER);
1042 rw_exit(&zfsvfs_lock);
1044 zfs_fuid_destroy(zfsvfs);
1046 mutex_destroy(&zfsvfs->z_znodes_lock);
1047 mutex_destroy(&zfsvfs->z_lock);
1048 list_destroy(&zfsvfs->z_all_znodes);
1049 rrw_destroy(&zfsvfs->z_teardown_lock);
1050 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1051 rw_destroy(&zfsvfs->z_fuid_lock);
1052 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1053 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1054 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1058 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1060 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1061 if (zfsvfs->z_vfs) {
1062 if (zfsvfs->z_use_fuids) {
1063 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1064 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1065 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1066 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1067 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1068 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1070 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1071 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1072 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1073 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1074 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1075 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1078 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1082 zfs_domount(vfs_t *vfsp, char *osname)
1084 uint64_t recordsize, fsid_guid;
1092 error = zfsvfs_create(osname, &zfsvfs);
1095 zfsvfs->z_vfs = vfsp;
1097 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1100 zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE;
1101 zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize;
1103 vfsp->vfs_data = zfsvfs;
1104 vfsp->mnt_flag |= MNT_LOCAL;
1105 vfsp->mnt_kern_flag |= MNTK_MPSAFE;
1106 vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
1107 vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
1110 * The fsid is 64 bits, composed of an 8-bit fs type, which
1111 * separates our fsid from any other filesystem types, and a
1112 * 56-bit objset unique ID. The objset unique ID is unique to
1113 * all objsets open on this system, provided by unique_create().
1114 * The 8-bit fs type must be put in the low bits of fsid[1]
1115 * because that's where other Solaris filesystems put it.
1117 fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1118 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1119 vfsp->vfs_fsid.val[0] = fsid_guid;
1120 vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1121 vfsp->mnt_vfc->vfc_typenum & 0xFF;
1124 * Set features for file system.
1126 zfs_set_fuid_feature(zfsvfs);
1127 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1128 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1129 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1130 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1131 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1132 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1133 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1135 vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1137 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1140 atime_changed_cb(zfsvfs, B_FALSE);
1141 readonly_changed_cb(zfsvfs, B_TRUE);
1142 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1144 xattr_changed_cb(zfsvfs, pval);
1145 zfsvfs->z_issnap = B_TRUE;
1146 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1148 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1149 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1150 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1152 error = zfsvfs_setup(zfsvfs, B_TRUE);
1155 vfs_mountedfrom(vfsp, osname);
1156 /* Grab extra reference. */
1157 VERIFY(VFS_ROOT(vfsp, LK_EXCLUSIVE, &vp) == 0);
1160 if (!zfsvfs->z_issnap)
1161 zfsctl_create(zfsvfs);
1164 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1165 zfsvfs_free(zfsvfs);
1167 atomic_add_32(&zfs_active_fs_count, 1);
1174 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1176 objset_t *os = zfsvfs->z_os;
1177 struct dsl_dataset *ds;
1180 * Unregister properties.
1182 if (!dmu_objset_is_snapshot(os)) {
1183 ds = dmu_objset_ds(os);
1184 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
1187 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
1190 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
1193 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
1196 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
1199 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
1202 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
1205 VERIFY(dsl_prop_unregister(ds, "aclinherit",
1206 acl_inherit_changed_cb, zfsvfs) == 0);
1208 VERIFY(dsl_prop_unregister(ds, "vscan",
1209 vscan_changed_cb, zfsvfs) == 0);
1215 * Convert a decimal digit string to a uint64_t integer.
1218 str_to_uint64(char *str, uint64_t *objnum)
1223 if (*str < '0' || *str > '9')
1226 num = num*10 + *str++ - '0';
1234 * The boot path passed from the boot loader is in the form of
1235 * "rootpool-name/root-filesystem-object-number'. Convert this
1236 * string to a dataset name: "rootpool-name/root-filesystem-name".
1239 zfs_parse_bootfs(char *bpath, char *outpath)
1245 if (*bpath == 0 || *bpath == '/')
1248 (void) strcpy(outpath, bpath);
1250 slashp = strchr(bpath, '/');
1252 /* if no '/', just return the pool name */
1253 if (slashp == NULL) {
1257 /* if not a number, just return the root dataset name */
1258 if (str_to_uint64(slashp+1, &objnum)) {
1263 error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1270 * zfs_check_global_label:
1271 * Check that the hex label string is appropriate for the dataset
1272 * being mounted into the global_zone proper.
1274 * Return an error if the hex label string is not default or
1275 * admin_low/admin_high. For admin_low labels, the corresponding
1276 * dataset must be readonly.
1279 zfs_check_global_label(const char *dsname, const char *hexsl)
1281 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1283 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1285 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1286 /* must be readonly */
1289 if (dsl_prop_get_integer(dsname,
1290 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1292 return (rdonly ? 0 : EACCES);
1298 * zfs_mount_label_policy:
1299 * Determine whether the mount is allowed according to MAC check.
1300 * by comparing (where appropriate) label of the dataset against
1301 * the label of the zone being mounted into. If the dataset has
1302 * no label, create one.
1305 * 0 : access allowed
1306 * >0 : error code, such as EACCES
1309 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1312 zone_t *mntzone = NULL;
1313 ts_label_t *mnt_tsl;
1316 char ds_hexsl[MAXNAMELEN];
1318 retv = EACCES; /* assume the worst */
1321 * Start by getting the dataset label if it exists.
1323 error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1324 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1329 * If labeling is NOT enabled, then disallow the mount of datasets
1330 * which have a non-default label already. No other label checks
1333 if (!is_system_labeled()) {
1334 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1340 * Get the label of the mountpoint. If mounting into the global
1341 * zone (i.e. mountpoint is not within an active zone and the
1342 * zoned property is off), the label must be default or
1343 * admin_low/admin_high only; no other checks are needed.
1345 mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1346 if (mntzone->zone_id == GLOBAL_ZONEID) {
1351 if (dsl_prop_get_integer(osname,
1352 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1355 return (zfs_check_global_label(osname, ds_hexsl));
1358 * This is the case of a zone dataset being mounted
1359 * initially, before the zone has been fully created;
1360 * allow this mount into global zone.
1365 mnt_tsl = mntzone->zone_slabel;
1366 ASSERT(mnt_tsl != NULL);
1367 label_hold(mnt_tsl);
1368 mnt_sl = label2bslabel(mnt_tsl);
1370 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1372 * The dataset doesn't have a real label, so fabricate one.
1376 if (l_to_str_internal(mnt_sl, &str) == 0 &&
1377 dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1378 ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0)
1381 kmem_free(str, strlen(str) + 1);
1382 } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1384 * Now compare labels to complete the MAC check. If the
1385 * labels are equal then allow access. If the mountpoint
1386 * label dominates the dataset label, allow readonly access.
1387 * Otherwise, access is denied.
1389 if (blequal(mnt_sl, &ds_sl))
1391 else if (bldominates(mnt_sl, &ds_sl)) {
1392 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1397 label_rele(mnt_tsl);
1401 #endif /* SECLABEL */
1403 #ifdef OPENSOLARIS_MOUNTROOT
1405 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1408 static int zfsrootdone = 0;
1409 zfsvfs_t *zfsvfs = NULL;
1418 * The filesystem that we mount as root is defined in the
1419 * boot property "zfs-bootfs" with a format of
1420 * "poolname/root-dataset-objnum".
1422 if (why == ROOT_INIT) {
1426 * the process of doing a spa_load will require the
1427 * clock to be set before we could (for example) do
1428 * something better by looking at the timestamp on
1429 * an uberblock, so just set it to -1.
1433 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1434 cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1438 zfs_devid = spa_get_bootprop("diskdevid");
1439 error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1441 spa_free_bootprop(zfs_devid);
1443 spa_free_bootprop(zfs_bootfs);
1444 cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1448 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1449 spa_free_bootprop(zfs_bootfs);
1450 cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1455 spa_free_bootprop(zfs_bootfs);
1457 if (error = vfs_lock(vfsp))
1460 if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1461 cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1465 zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1467 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1468 cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1473 mutex_enter(&vp->v_lock);
1474 vp->v_flag |= VROOT;
1475 mutex_exit(&vp->v_lock);
1479 * Leave rootvp held. The root file system is never unmounted.
1482 vfs_add((struct vnode *)0, vfsp,
1483 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1487 } else if (why == ROOT_REMOUNT) {
1488 readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1489 vfsp->vfs_flag |= VFS_REMOUNT;
1491 /* refresh mount options */
1492 zfs_unregister_callbacks(vfsp->vfs_data);
1493 return (zfs_register_callbacks(vfsp));
1495 } else if (why == ROOT_UNMOUNT) {
1496 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1497 (void) zfs_sync(vfsp, 0, 0);
1502 * if "why" is equal to anything else other than ROOT_INIT,
1503 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1507 #endif /* OPENSOLARIS_MOUNTROOT */
1511 zfs_mount(vfs_t *vfsp)
1513 kthread_t *td = curthread;
1514 vnode_t *mvp = vfsp->mnt_vnodecovered;
1515 cred_t *cr = td->td_ucred;
1520 if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
1524 * If full-owner-access is enabled and delegated administration is
1525 * turned on, we must set nosuid.
1527 if (zfs_super_owner &&
1528 dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
1529 secpolicy_fs_mount_clearopts(cr, vfsp);
1533 * Check for mount privilege?
1535 * If we don't have privilege then see if
1536 * we have local permission to allow it
1538 error = secpolicy_fs_mount(cr, mvp, vfsp);
1540 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != 0)
1543 if (!(vfsp->vfs_flag & MS_REMOUNT)) {
1547 * Make sure user is the owner of the mount point
1548 * or has sufficient privileges.
1551 vattr.va_mask = AT_UID;
1553 vn_lock(mvp, LK_SHARED | LK_RETRY);
1554 if (VOP_GETATTR(mvp, &vattr, cr)) {
1559 if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
1560 VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
1567 secpolicy_fs_mount_clearopts(cr, vfsp);
1571 * Refuse to mount a filesystem if we are in a local zone and the
1572 * dataset is not visible.
1574 if (!INGLOBALZONE(curthread) &&
1575 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1581 error = zfs_mount_label_policy(vfsp, osname);
1586 vfsp->vfs_flag |= MNT_NFS4ACLS;
1589 * When doing a remount, we simply refresh our temporary properties
1590 * according to those options set in the current VFS options.
1592 if (vfsp->vfs_flag & MS_REMOUNT) {
1593 /* refresh mount options */
1594 zfs_unregister_callbacks(vfsp->vfs_data);
1595 error = zfs_register_callbacks(vfsp);
1600 error = zfs_domount(vfsp, osname);
1605 * Add an extra VFS_HOLD on our parent vfs so that it can't
1606 * disappear due to a forced unmount.
1608 if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1609 VFS_HOLD(mvp->v_vfsp);
1617 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
1619 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1620 uint64_t refdbytes, availbytes, usedobjs, availobjs;
1622 statp->f_version = STATFS_VERSION;
1626 dmu_objset_space(zfsvfs->z_os,
1627 &refdbytes, &availbytes, &usedobjs, &availobjs);
1630 * The underlying storage pool actually uses multiple block sizes.
1631 * We report the fragsize as the smallest block size we support,
1632 * and we report our blocksize as the filesystem's maximum blocksize.
1634 statp->f_bsize = SPA_MINBLOCKSIZE;
1635 statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize;
1638 * The following report "total" blocks of various kinds in the
1639 * file system, but reported in terms of f_frsize - the
1643 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1644 statp->f_bfree = availbytes / statp->f_bsize;
1645 statp->f_bavail = statp->f_bfree; /* no root reservation */
1648 * statvfs() should really be called statufs(), because it assumes
1649 * static metadata. ZFS doesn't preallocate files, so the best
1650 * we can do is report the max that could possibly fit in f_files,
1651 * and that minus the number actually used in f_ffree.
1652 * For f_ffree, report the smaller of the number of object available
1653 * and the number of blocks (each object will take at least a block).
1655 statp->f_ffree = MIN(availobjs, statp->f_bfree);
1656 statp->f_files = statp->f_ffree + usedobjs;
1659 * We're a zfs filesystem.
1661 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
1663 strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
1664 sizeof(statp->f_mntfromname));
1665 strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
1666 sizeof(statp->f_mntonname));
1668 statp->f_namemax = ZFS_MAXNAMELEN;
1675 zfs_vnode_lock(vnode_t *vp, int flags)
1682 * Check if the file system wasn't forcibly unmounted in the meantime.
1684 error = vn_lock(vp, flags);
1685 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0) {
1694 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
1696 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1700 ZFS_ENTER_NOERROR(zfsvfs);
1702 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1704 *vpp = ZTOV(rootzp);
1709 error = zfs_vnode_lock(*vpp, flags);
1711 (*vpp)->v_vflag |= VV_ROOT;
1720 * Teardown the zfsvfs::z_os.
1722 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1723 * and 'z_teardown_inactive_lock' held.
1726 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1730 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1734 * We purge the parent filesystem's vfsp as the parent
1735 * filesystem and all of its snapshots have their vnode's
1736 * v_vfsp set to the parent's filesystem's vfsp. Note,
1737 * 'z_parent' is self referential for non-snapshots.
1739 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1740 #ifdef FREEBSD_NAMECACHE
1741 cache_purgevfs(zfsvfs->z_parent->z_vfs);
1746 * Close the zil. NB: Can't close the zil while zfs_inactive
1747 * threads are blocked as zil_close can call zfs_inactive.
1749 if (zfsvfs->z_log) {
1750 zil_close(zfsvfs->z_log);
1751 zfsvfs->z_log = NULL;
1754 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1757 * If we are not unmounting (ie: online recv) and someone already
1758 * unmounted this file system while we were doing the switcheroo,
1759 * or a reopen of z_os failed then just bail out now.
1761 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1762 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1763 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1768 * At this point there are no vops active, and any new vops will
1769 * fail with EIO since we have z_teardown_lock for writer (only
1770 * relavent for forced unmount).
1772 * Release all holds on dbufs.
1774 mutex_enter(&zfsvfs->z_znodes_lock);
1775 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1776 zp = list_next(&zfsvfs->z_all_znodes, zp))
1778 ASSERT(ZTOV(zp)->v_count >= 0);
1779 zfs_znode_dmu_fini(zp);
1781 mutex_exit(&zfsvfs->z_znodes_lock);
1784 * If we are unmounting, set the unmounted flag and let new vops
1785 * unblock. zfs_inactive will have the unmounted behavior, and all
1786 * other vops will fail with EIO.
1789 zfsvfs->z_unmounted = B_TRUE;
1790 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1791 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1795 * Some znodes might not be fully reclaimed, wait for them.
1797 mutex_enter(&zfsvfs->z_znodes_lock);
1798 while (list_head(&zfsvfs->z_all_znodes) != NULL) {
1799 msleep(zfsvfs, &zfsvfs->z_znodes_lock, 0,
1802 mutex_exit(&zfsvfs->z_znodes_lock);
1807 * z_os will be NULL if there was an error in attempting to reopen
1808 * zfsvfs, so just return as the properties had already been
1809 * unregistered and cached data had been evicted before.
1811 if (zfsvfs->z_os == NULL)
1815 * Unregister properties.
1817 zfs_unregister_callbacks(zfsvfs);
1822 if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
1823 if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1824 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1825 (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1832 zfs_umount(vfs_t *vfsp, int fflag)
1834 kthread_t *td = curthread;
1835 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1837 cred_t *cr = td->td_ucred;
1840 ret = secpolicy_fs_unmount(cr, vfsp);
1842 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1843 ZFS_DELEG_PERM_MOUNT, cr))
1848 * We purge the parent filesystem's vfsp as the parent filesystem
1849 * and all of its snapshots have their vnode's v_vfsp set to the
1850 * parent's filesystem's vfsp. Note, 'z_parent' is self
1851 * referential for non-snapshots.
1853 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1856 * Unmount any snapshots mounted under .zfs before unmounting the
1859 if (zfsvfs->z_ctldir != NULL) {
1860 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1862 ret = vflush(vfsp, 0, 0, td);
1863 ASSERT(ret == EBUSY);
1864 if (!(fflag & MS_FORCE)) {
1865 if (zfsvfs->z_ctldir->v_count > 1)
1867 ASSERT(zfsvfs->z_ctldir->v_count == 1);
1869 zfsctl_destroy(zfsvfs);
1870 ASSERT(zfsvfs->z_ctldir == NULL);
1873 if (fflag & MS_FORCE) {
1875 * Mark file system as unmounted before calling
1876 * vflush(FORCECLOSE). This way we ensure no future vnops
1877 * will be called and risk operating on DOOMED vnodes.
1879 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1880 zfsvfs->z_unmounted = B_TRUE;
1881 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1885 * Flush all the files.
1887 ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, td);
1889 if (!zfsvfs->z_issnap) {
1890 zfsctl_create(zfsvfs);
1891 ASSERT(zfsvfs->z_ctldir != NULL);
1896 if (!(fflag & MS_FORCE)) {
1898 * Check the number of active vnodes in the file system.
1899 * Our count is maintained in the vfs structure, but the
1900 * number is off by 1 to indicate a hold on the vfs
1903 * The '.zfs' directory maintains a reference of its
1904 * own, and any active references underneath are
1905 * reflected in the vnode count.
1907 if (zfsvfs->z_ctldir == NULL) {
1908 if (vfsp->vfs_count > 1)
1911 if (vfsp->vfs_count > 2 ||
1912 zfsvfs->z_ctldir->v_count > 1)
1917 vfsp->mnt_kern_flag |= MNTK_UNMOUNTF;
1921 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1925 * z_os will be NULL if there was an error in
1926 * attempting to reopen zfsvfs.
1930 * Unset the objset user_ptr.
1932 mutex_enter(&os->os_user_ptr_lock);
1933 dmu_objset_set_user(os, NULL);
1934 mutex_exit(&os->os_user_ptr_lock);
1937 * Finally release the objset
1939 dmu_objset_disown(os, zfsvfs);
1943 * We can now safely destroy the '.zfs' directory node.
1945 if (zfsvfs->z_ctldir != NULL)
1946 zfsctl_destroy(zfsvfs);
1947 if (zfsvfs->z_issnap) {
1948 vnode_t *svp = vfsp->mnt_vnodecovered;
1950 if (svp->v_count >= 2)
1959 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
1961 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1966 * zfs_zget() can't operate on virtual entries like .zfs/ or
1967 * .zfs/snapshot/ directories, that's why we return EOPNOTSUPP.
1968 * This will make NFS to switch to LOOKUP instead of using VGET.
1970 if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR)
1971 return (EOPNOTSUPP);
1974 err = zfs_zget(zfsvfs, ino, &zp);
1975 if (err == 0 && zp->z_unlinked) {
1983 err = zfs_vnode_lock(*vpp, flags);
1990 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
1991 struct ucred **credanonp, int *numsecflavors, int **secflavors)
1993 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1996 * If this is regular file system vfsp is the same as
1997 * zfsvfs->z_parent->z_vfs, but if it is snapshot,
1998 * zfsvfs->z_parent->z_vfs represents parent file system
1999 * which we have to use here, because only this file system
2000 * has mnt_export configured.
2002 return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
2003 credanonp, numsecflavors, secflavors));
2006 CTASSERT(SHORT_FID_LEN <= sizeof(struct fid));
2007 CTASSERT(LONG_FID_LEN <= sizeof(struct fid));
2010 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp)
2012 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2014 uint64_t object = 0;
2015 uint64_t fid_gen = 0;
2025 * On FreeBSD we can get snapshot's mount point or its parent file
2026 * system mount point depending if snapshot is already mounted or not.
2028 if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
2029 zfid_long_t *zlfid = (zfid_long_t *)fidp;
2030 uint64_t objsetid = 0;
2031 uint64_t setgen = 0;
2033 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2034 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2036 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2037 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2041 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
2047 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2048 zfid_short_t *zfid = (zfid_short_t *)fidp;
2050 for (i = 0; i < sizeof (zfid->zf_object); i++)
2051 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2053 for (i = 0; i < sizeof (zfid->zf_gen); i++)
2054 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2060 /* A zero fid_gen means we are in the .zfs control directories */
2062 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
2063 *vpp = zfsvfs->z_ctldir;
2064 ASSERT(*vpp != NULL);
2065 if (object == ZFSCTL_INO_SNAPDIR) {
2066 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
2067 0, NULL, NULL, NULL, NULL, NULL) == 0);
2072 err = zfs_vnode_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
2078 gen_mask = -1ULL >> (64 - 8 * i);
2080 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2081 if (err = zfs_zget(zfsvfs, object, &zp)) {
2085 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2087 zp_gen = zp_gen & gen_mask;
2090 if (zp->z_unlinked || zp_gen != fid_gen) {
2091 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2099 err = zfs_vnode_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
2101 vnode_create_vobject(*vpp, zp->z_size, curthread);
2108 * Block out VOPs and close zfsvfs_t::z_os
2110 * Note, if successful, then we return with the 'z_teardown_lock' and
2111 * 'z_teardown_inactive_lock' write held.
2114 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2118 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2120 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
2126 * Reopen zfsvfs_t::z_os and release VOPs.
2129 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
2133 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
2134 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2136 err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
2139 zfsvfs->z_os = NULL;
2142 uint64_t sa_obj = 0;
2145 * Make sure version hasn't changed
2148 err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION,
2149 &zfsvfs->z_version);
2154 err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
2155 ZFS_SA_ATTRS, 8, 1, &sa_obj);
2157 if (err && zfsvfs->z_version >= ZPL_VERSION_SA)
2160 if ((err = sa_setup(zfsvfs->z_os, sa_obj,
2161 zfs_attr_table, ZPL_END, &zfsvfs->z_attr_table)) != 0)
2164 if (zfsvfs->z_version >= ZPL_VERSION_SA)
2165 sa_register_update_callback(zfsvfs->z_os,
2168 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2170 zfs_set_fuid_feature(zfsvfs);
2173 * Attempt to re-establish all the active znodes with
2174 * their dbufs. If a zfs_rezget() fails, then we'll let
2175 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2176 * when they try to use their znode.
2178 mutex_enter(&zfsvfs->z_znodes_lock);
2179 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2180 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2181 (void) zfs_rezget(zp);
2183 mutex_exit(&zfsvfs->z_znodes_lock);
2187 /* release the VOPs */
2188 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2189 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
2193 * Since we couldn't reopen zfsvfs::z_os, or
2194 * setup the sa framework force unmount this file system.
2196 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2197 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
2203 zfs_freevfs(vfs_t *vfsp)
2205 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2209 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2210 * from zfs_mount(). Release it here. If we came through
2211 * zfs_mountroot() instead, we didn't grab an extra hold, so
2212 * skip the VFS_RELE for rootvfs.
2214 if (zfsvfs->z_issnap && (vfsp != rootvfs))
2215 VFS_RELE(zfsvfs->z_parent->z_vfs);
2218 zfsvfs_free(zfsvfs);
2220 atomic_add_32(&zfs_active_fs_count, -1);
2224 static int desiredvnodes_backup;
2228 zfs_vnodes_adjust(void)
2231 int newdesiredvnodes;
2233 desiredvnodes_backup = desiredvnodes;
2236 * We calculate newdesiredvnodes the same way it is done in
2237 * vntblinit(). If it is equal to desiredvnodes, it means that
2238 * it wasn't tuned by the administrator and we can tune it down.
2240 newdesiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 *
2241 vm_kmem_size / (5 * (sizeof(struct vm_object) +
2242 sizeof(struct vnode))));
2243 if (newdesiredvnodes == desiredvnodes)
2244 desiredvnodes = (3 * newdesiredvnodes) / 4;
2249 zfs_vnodes_adjust_back(void)
2253 desiredvnodes = desiredvnodes_backup;
2261 printf("ZFS filesystem version " ZPL_VERSION_STRING "\n");
2264 * Initialize .zfs directory structures
2269 * Initialize znode cache, vnode ops, etc...
2274 * Reduce number of vnodes. Originally number of vnodes is calculated
2275 * with UFS inode in mind. We reduce it here, because it's too big for
2278 zfs_vnodes_adjust();
2280 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2288 zfs_vnodes_adjust_back();
2294 return (zfs_active_fs_count != 0);
2298 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2301 objset_t *os = zfsvfs->z_os;
2304 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2307 if (newvers < zfsvfs->z_version)
2310 if (zfs_spa_version_map(newvers) >
2311 spa_version(dmu_objset_spa(zfsvfs->z_os)))
2314 tx = dmu_tx_create(os);
2315 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2316 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2317 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2319 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2321 error = dmu_tx_assign(tx, TXG_WAIT);
2327 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2328 8, 1, &newvers, tx);
2335 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2338 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2340 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2341 DMU_OT_NONE, 0, tx);
2343 error = zap_add(os, MASTER_NODE_OBJ,
2344 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2345 ASSERT3U(error, ==, 0);
2347 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2348 sa_register_update_callback(os, zfs_sa_upgrade);
2351 spa_history_log_internal(LOG_DS_UPGRADE,
2352 dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
2353 zfsvfs->z_version, newvers, dmu_objset_id(os));
2357 zfsvfs->z_version = newvers;
2359 zfs_set_fuid_feature(zfsvfs);
2365 * Read a property stored within the master node.
2368 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2374 * Look up the file system's value for the property. For the
2375 * version property, we look up a slightly different string.
2377 if (prop == ZFS_PROP_VERSION)
2378 pname = ZPL_VERSION_STR;
2380 pname = zfs_prop_to_name(prop);
2383 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2385 if (error == ENOENT) {
2386 /* No value set, use the default value */
2388 case ZFS_PROP_VERSION:
2389 *value = ZPL_VERSION;
2391 case ZFS_PROP_NORMALIZE:
2392 case ZFS_PROP_UTF8ONLY:
2396 *value = ZFS_CASE_SENSITIVE;