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, int flags, 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_mode_changed_cb(void *arg, uint64_t newval)
369 zfsvfs_t *zfsvfs = arg;
371 zfsvfs->z_acl_mode = newval;
375 acl_inherit_changed_cb(void *arg, uint64_t newval)
377 zfsvfs_t *zfsvfs = arg;
379 zfsvfs->z_acl_inherit = newval;
383 zfs_register_callbacks(vfs_t *vfsp)
385 struct dsl_dataset *ds = NULL;
387 zfsvfs_t *zfsvfs = NULL;
389 int readonly, do_readonly = B_FALSE;
390 int setuid, do_setuid = B_FALSE;
391 int exec, do_exec = B_FALSE;
392 int xattr, do_xattr = B_FALSE;
393 int atime, do_atime = B_FALSE;
397 zfsvfs = vfsp->vfs_data;
402 * This function can be called for a snapshot when we update snapshot's
403 * mount point, which isn't really supported.
405 if (dmu_objset_is_snapshot(os))
409 * The act of registering our callbacks will destroy any mount
410 * options we may have. In order to enable temporary overrides
411 * of mount options, we stash away the current values and
412 * restore them after we register the callbacks.
414 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
415 !spa_writeable(dmu_objset_spa(os))) {
417 do_readonly = B_TRUE;
418 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
420 do_readonly = B_TRUE;
422 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
426 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
429 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
434 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
437 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
441 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
444 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
448 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
451 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
457 * nbmand is a special property. It can only be changed at
460 * This is weird, but it is documented to only be changeable
463 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
465 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
468 char osname[MAXNAMELEN];
470 dmu_objset_name(os, osname);
471 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
478 * Register property callbacks.
480 * It would probably be fine to just check for i/o error from
481 * the first prop_register(), but I guess I like to go
484 ds = dmu_objset_ds(os);
485 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
486 error = error ? error : dsl_prop_register(ds,
487 "xattr", xattr_changed_cb, zfsvfs);
488 error = error ? error : dsl_prop_register(ds,
489 "recordsize", blksz_changed_cb, zfsvfs);
490 error = error ? error : dsl_prop_register(ds,
491 "readonly", readonly_changed_cb, zfsvfs);
492 error = error ? error : dsl_prop_register(ds,
493 "setuid", setuid_changed_cb, zfsvfs);
494 error = error ? error : dsl_prop_register(ds,
495 "exec", exec_changed_cb, zfsvfs);
496 error = error ? error : dsl_prop_register(ds,
497 "snapdir", snapdir_changed_cb, zfsvfs);
498 error = error ? error : dsl_prop_register(ds,
499 "aclmode", acl_mode_changed_cb, zfsvfs);
500 error = error ? error : dsl_prop_register(ds,
501 "aclinherit", acl_inherit_changed_cb, zfsvfs);
502 error = error ? error : dsl_prop_register(ds,
503 "vscan", vscan_changed_cb, zfsvfs);
508 * Invoke our callbacks to restore temporary mount options.
511 readonly_changed_cb(zfsvfs, readonly);
513 setuid_changed_cb(zfsvfs, setuid);
515 exec_changed_cb(zfsvfs, exec);
517 xattr_changed_cb(zfsvfs, xattr);
519 atime_changed_cb(zfsvfs, atime);
521 nbmand_changed_cb(zfsvfs, nbmand);
527 * We may attempt to unregister some callbacks that are not
528 * registered, but this is OK; it will simply return ENOMSG,
529 * which we will ignore.
531 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
532 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
533 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
534 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
535 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
536 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
537 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
538 (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
539 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
541 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
547 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
548 uint64_t *userp, uint64_t *groupp)
550 znode_phys_t *znp = data;
554 * Is it a valid type of object to track?
556 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
560 * If we have a NULL data pointer
561 * then assume the id's aren't changing and
562 * return EEXIST to the dmu to let it know to
568 if (bonustype == DMU_OT_ZNODE) {
569 *userp = znp->zp_uid;
570 *groupp = znp->zp_gid;
574 ASSERT(bonustype == DMU_OT_SA);
575 hdrsize = sa_hdrsize(data);
578 *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
580 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
584 * This should only happen for newly created
585 * files that haven't had the znode data filled
596 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
597 char *domainbuf, int buflen, uid_t *ridp)
602 fuid = strtonum(fuidstr, NULL);
604 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
606 (void) strlcpy(domainbuf, domain, buflen);
609 *ridp = FUID_RID(fuid);
613 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
616 case ZFS_PROP_USERUSED:
617 return (DMU_USERUSED_OBJECT);
618 case ZFS_PROP_GROUPUSED:
619 return (DMU_GROUPUSED_OBJECT);
620 case ZFS_PROP_USERQUOTA:
621 return (zfsvfs->z_userquota_obj);
622 case ZFS_PROP_GROUPQUOTA:
623 return (zfsvfs->z_groupquota_obj);
629 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
630 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
635 zfs_useracct_t *buf = vbuf;
638 if (!dmu_objset_userspace_present(zfsvfs->z_os))
641 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
647 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
648 (error = zap_cursor_retrieve(&zc, &za)) == 0;
649 zap_cursor_advance(&zc)) {
650 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
654 fuidstr_to_sid(zfsvfs, za.za_name,
655 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
657 buf->zu_space = za.za_first_integer;
663 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
664 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
665 *cookiep = zap_cursor_serialize(&zc);
666 zap_cursor_fini(&zc);
671 * buf must be big enough (eg, 32 bytes)
674 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
675 char *buf, boolean_t addok)
680 if (domain && domain[0]) {
681 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
685 fuid = FUID_ENCODE(domainid, rid);
686 (void) sprintf(buf, "%llx", (longlong_t)fuid);
691 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
692 const char *domain, uint64_t rid, uint64_t *valp)
700 if (!dmu_objset_userspace_present(zfsvfs->z_os))
703 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
707 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
711 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
718 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
719 const char *domain, uint64_t rid, uint64_t quota)
725 boolean_t fuid_dirtied;
727 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
730 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
733 objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
734 &zfsvfs->z_groupquota_obj;
736 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
739 fuid_dirtied = zfsvfs->z_fuid_dirty;
741 tx = dmu_tx_create(zfsvfs->z_os);
742 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
744 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
745 zfs_userquota_prop_prefixes[type]);
748 zfs_fuid_txhold(zfsvfs, tx);
749 err = dmu_tx_assign(tx, TXG_WAIT);
755 mutex_enter(&zfsvfs->z_lock);
757 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
759 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
760 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
762 mutex_exit(&zfsvfs->z_lock);
765 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
769 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, "a, tx);
773 zfs_fuid_sync(zfsvfs, tx);
779 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
782 uint64_t used, quota, usedobj, quotaobj;
785 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
786 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
788 if (quotaobj == 0 || zfsvfs->z_replay)
791 (void) sprintf(buf, "%llx", (longlong_t)fuid);
792 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, "a);
796 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
799 return (used >= quota);
803 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
808 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
810 fuid = isgroup ? zp->z_gid : zp->z_uid;
812 if (quotaobj == 0 || zfsvfs->z_replay)
815 return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
819 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
827 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
830 * We claim to always be readonly so we can open snapshots;
831 * other ZPL code will prevent us from writing to snapshots.
833 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
835 kmem_free(zfsvfs, sizeof (zfsvfs_t));
840 * Initialize the zfs-specific filesystem structure.
841 * Should probably make this a kmem cache, shuffle fields,
842 * and just bzero up to z_hold_mtx[].
844 zfsvfs->z_vfs = NULL;
845 zfsvfs->z_parent = zfsvfs;
846 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
847 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
850 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
853 } else if (zfsvfs->z_version >
854 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
855 (void) printf("Can't mount a version %lld file system "
856 "on a version %lld pool\n. Pool must be upgraded to mount "
857 "this file system.", (u_longlong_t)zfsvfs->z_version,
858 (u_longlong_t)spa_version(dmu_objset_spa(os)));
862 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
864 zfsvfs->z_norm = (int)zval;
866 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
868 zfsvfs->z_utf8 = (zval != 0);
870 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
872 zfsvfs->z_case = (uint_t)zval;
875 * Fold case on file systems that are always or sometimes case
878 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
879 zfsvfs->z_case == ZFS_CASE_MIXED)
880 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
882 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
883 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
885 if (zfsvfs->z_use_sa) {
886 /* should either have both of these objects or none */
887 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
893 * Pre SA versions file systems should never touch
894 * either the attribute registration or layout objects.
899 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
900 &zfsvfs->z_attr_table);
904 if (zfsvfs->z_version >= ZPL_VERSION_SA)
905 sa_register_update_callback(os, zfs_sa_upgrade);
907 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
911 ASSERT(zfsvfs->z_root != 0);
913 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
914 &zfsvfs->z_unlinkedobj);
918 error = zap_lookup(os, MASTER_NODE_OBJ,
919 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
920 8, 1, &zfsvfs->z_userquota_obj);
921 if (error && error != ENOENT)
924 error = zap_lookup(os, MASTER_NODE_OBJ,
925 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
926 8, 1, &zfsvfs->z_groupquota_obj);
927 if (error && error != ENOENT)
930 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
931 &zfsvfs->z_fuid_obj);
932 if (error && error != ENOENT)
935 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
936 &zfsvfs->z_shares_dir);
937 if (error && error != ENOENT)
940 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
941 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
942 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
943 offsetof(znode_t, z_link_node));
944 rrw_init(&zfsvfs->z_teardown_lock);
945 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
946 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
947 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
948 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
954 dmu_objset_disown(os, zfsvfs);
956 kmem_free(zfsvfs, sizeof (zfsvfs_t));
961 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
965 error = zfs_register_callbacks(zfsvfs->z_vfs);
970 * Set the objset user_ptr to track its zfsvfs.
972 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
973 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
974 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
976 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
979 * If we are not mounting (ie: online recv), then we don't
980 * have to worry about replaying the log as we blocked all
981 * operations out since we closed the ZIL.
987 * During replay we remove the read only flag to
988 * allow replays to succeed.
990 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
992 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
994 zfs_unlinked_drain(zfsvfs);
997 * Parse and replay the intent log.
999 * Because of ziltest, this must be done after
1000 * zfs_unlinked_drain(). (Further note: ziltest
1001 * doesn't use readonly mounts, where
1002 * zfs_unlinked_drain() isn't called.) This is because
1003 * ziltest causes spa_sync() to think it's committed,
1004 * but actually it is not, so the intent log contains
1005 * many txg's worth of changes.
1007 * In particular, if object N is in the unlinked set in
1008 * the last txg to actually sync, then it could be
1009 * actually freed in a later txg and then reallocated
1010 * in a yet later txg. This would write a "create
1011 * object N" record to the intent log. Normally, this
1012 * would be fine because the spa_sync() would have
1013 * written out the fact that object N is free, before
1014 * we could write the "create object N" intent log
1017 * But when we are in ziltest mode, we advance the "open
1018 * txg" without actually spa_sync()-ing the changes to
1019 * disk. So we would see that object N is still
1020 * allocated and in the unlinked set, and there is an
1021 * intent log record saying to allocate it.
1023 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1024 if (zil_replay_disable) {
1025 zil_destroy(zfsvfs->z_log, B_FALSE);
1027 zfsvfs->z_replay = B_TRUE;
1028 zil_replay(zfsvfs->z_os, zfsvfs,
1030 zfsvfs->z_replay = B_FALSE;
1033 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1039 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1042 zfsvfs_free(zfsvfs_t *zfsvfs)
1047 * This is a barrier to prevent the filesystem from going away in
1048 * zfs_znode_move() until we can safely ensure that the filesystem is
1049 * not unmounted. We consider the filesystem valid before the barrier
1050 * and invalid after the barrier.
1052 rw_enter(&zfsvfs_lock, RW_READER);
1053 rw_exit(&zfsvfs_lock);
1055 zfs_fuid_destroy(zfsvfs);
1057 mutex_destroy(&zfsvfs->z_znodes_lock);
1058 mutex_destroy(&zfsvfs->z_lock);
1059 list_destroy(&zfsvfs->z_all_znodes);
1060 rrw_destroy(&zfsvfs->z_teardown_lock);
1061 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1062 rw_destroy(&zfsvfs->z_fuid_lock);
1063 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1064 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1065 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1069 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1071 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1072 if (zfsvfs->z_vfs) {
1073 if (zfsvfs->z_use_fuids) {
1074 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1075 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1076 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1077 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1078 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1079 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1081 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1082 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1083 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1084 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1085 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1086 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1089 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1093 zfs_domount(vfs_t *vfsp, char *osname)
1095 uint64_t recordsize, fsid_guid;
1103 error = zfsvfs_create(osname, &zfsvfs);
1106 zfsvfs->z_vfs = vfsp;
1108 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1111 zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE;
1112 zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize;
1114 vfsp->vfs_data = zfsvfs;
1115 vfsp->mnt_flag |= MNT_LOCAL;
1116 vfsp->mnt_kern_flag |= MNTK_MPSAFE;
1117 vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
1118 vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
1121 * The fsid is 64 bits, composed of an 8-bit fs type, which
1122 * separates our fsid from any other filesystem types, and a
1123 * 56-bit objset unique ID. The objset unique ID is unique to
1124 * all objsets open on this system, provided by unique_create().
1125 * The 8-bit fs type must be put in the low bits of fsid[1]
1126 * because that's where other Solaris filesystems put it.
1128 fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1129 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1130 vfsp->vfs_fsid.val[0] = fsid_guid;
1131 vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1132 vfsp->mnt_vfc->vfc_typenum & 0xFF;
1135 * Set features for file system.
1137 zfs_set_fuid_feature(zfsvfs);
1138 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1139 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1140 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1141 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1142 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1143 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1144 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1146 vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1148 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1151 atime_changed_cb(zfsvfs, B_FALSE);
1152 readonly_changed_cb(zfsvfs, B_TRUE);
1153 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1155 xattr_changed_cb(zfsvfs, pval);
1156 zfsvfs->z_issnap = B_TRUE;
1157 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1159 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1160 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1161 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1163 error = zfsvfs_setup(zfsvfs, B_TRUE);
1166 vfs_mountedfrom(vfsp, osname);
1167 /* Grab extra reference. */
1168 VERIFY(VFS_ROOT(vfsp, LK_EXCLUSIVE, &vp) == 0);
1171 if (!zfsvfs->z_issnap)
1172 zfsctl_create(zfsvfs);
1175 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1176 zfsvfs_free(zfsvfs);
1178 atomic_add_32(&zfs_active_fs_count, 1);
1185 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1187 objset_t *os = zfsvfs->z_os;
1188 struct dsl_dataset *ds;
1191 * Unregister properties.
1193 if (!dmu_objset_is_snapshot(os)) {
1194 ds = dmu_objset_ds(os);
1195 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
1198 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
1201 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
1204 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
1207 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
1210 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
1213 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
1216 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
1219 VERIFY(dsl_prop_unregister(ds, "aclinherit",
1220 acl_inherit_changed_cb, zfsvfs) == 0);
1222 VERIFY(dsl_prop_unregister(ds, "vscan",
1223 vscan_changed_cb, zfsvfs) == 0);
1229 * Convert a decimal digit string to a uint64_t integer.
1232 str_to_uint64(char *str, uint64_t *objnum)
1237 if (*str < '0' || *str > '9')
1240 num = num*10 + *str++ - '0';
1248 * The boot path passed from the boot loader is in the form of
1249 * "rootpool-name/root-filesystem-object-number'. Convert this
1250 * string to a dataset name: "rootpool-name/root-filesystem-name".
1253 zfs_parse_bootfs(char *bpath, char *outpath)
1259 if (*bpath == 0 || *bpath == '/')
1262 (void) strcpy(outpath, bpath);
1264 slashp = strchr(bpath, '/');
1266 /* if no '/', just return the pool name */
1267 if (slashp == NULL) {
1271 /* if not a number, just return the root dataset name */
1272 if (str_to_uint64(slashp+1, &objnum)) {
1277 error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1284 * zfs_check_global_label:
1285 * Check that the hex label string is appropriate for the dataset
1286 * being mounted into the global_zone proper.
1288 * Return an error if the hex label string is not default or
1289 * admin_low/admin_high. For admin_low labels, the corresponding
1290 * dataset must be readonly.
1293 zfs_check_global_label(const char *dsname, const char *hexsl)
1295 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1297 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1299 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1300 /* must be readonly */
1303 if (dsl_prop_get_integer(dsname,
1304 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1306 return (rdonly ? 0 : EACCES);
1312 * zfs_mount_label_policy:
1313 * Determine whether the mount is allowed according to MAC check.
1314 * by comparing (where appropriate) label of the dataset against
1315 * the label of the zone being mounted into. If the dataset has
1316 * no label, create one.
1319 * 0 : access allowed
1320 * >0 : error code, such as EACCES
1323 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1326 zone_t *mntzone = NULL;
1327 ts_label_t *mnt_tsl;
1330 char ds_hexsl[MAXNAMELEN];
1332 retv = EACCES; /* assume the worst */
1335 * Start by getting the dataset label if it exists.
1337 error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1338 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1343 * If labeling is NOT enabled, then disallow the mount of datasets
1344 * which have a non-default label already. No other label checks
1347 if (!is_system_labeled()) {
1348 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1354 * Get the label of the mountpoint. If mounting into the global
1355 * zone (i.e. mountpoint is not within an active zone and the
1356 * zoned property is off), the label must be default or
1357 * admin_low/admin_high only; no other checks are needed.
1359 mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1360 if (mntzone->zone_id == GLOBAL_ZONEID) {
1365 if (dsl_prop_get_integer(osname,
1366 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1369 return (zfs_check_global_label(osname, ds_hexsl));
1372 * This is the case of a zone dataset being mounted
1373 * initially, before the zone has been fully created;
1374 * allow this mount into global zone.
1379 mnt_tsl = mntzone->zone_slabel;
1380 ASSERT(mnt_tsl != NULL);
1381 label_hold(mnt_tsl);
1382 mnt_sl = label2bslabel(mnt_tsl);
1384 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1386 * The dataset doesn't have a real label, so fabricate one.
1390 if (l_to_str_internal(mnt_sl, &str) == 0 &&
1391 dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1392 ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0)
1395 kmem_free(str, strlen(str) + 1);
1396 } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1398 * Now compare labels to complete the MAC check. If the
1399 * labels are equal then allow access. If the mountpoint
1400 * label dominates the dataset label, allow readonly access.
1401 * Otherwise, access is denied.
1403 if (blequal(mnt_sl, &ds_sl))
1405 else if (bldominates(mnt_sl, &ds_sl)) {
1406 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1411 label_rele(mnt_tsl);
1415 #endif /* SECLABEL */
1417 #ifdef OPENSOLARIS_MOUNTROOT
1419 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1422 static int zfsrootdone = 0;
1423 zfsvfs_t *zfsvfs = NULL;
1432 * The filesystem that we mount as root is defined in the
1433 * boot property "zfs-bootfs" with a format of
1434 * "poolname/root-dataset-objnum".
1436 if (why == ROOT_INIT) {
1440 * the process of doing a spa_load will require the
1441 * clock to be set before we could (for example) do
1442 * something better by looking at the timestamp on
1443 * an uberblock, so just set it to -1.
1447 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1448 cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1452 zfs_devid = spa_get_bootprop("diskdevid");
1453 error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1455 spa_free_bootprop(zfs_devid);
1457 spa_free_bootprop(zfs_bootfs);
1458 cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1462 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1463 spa_free_bootprop(zfs_bootfs);
1464 cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1469 spa_free_bootprop(zfs_bootfs);
1471 if (error = vfs_lock(vfsp))
1474 if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1475 cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1479 zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1481 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1482 cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1487 mutex_enter(&vp->v_lock);
1488 vp->v_flag |= VROOT;
1489 mutex_exit(&vp->v_lock);
1493 * Leave rootvp held. The root file system is never unmounted.
1496 vfs_add((struct vnode *)0, vfsp,
1497 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1501 } else if (why == ROOT_REMOUNT) {
1502 readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1503 vfsp->vfs_flag |= VFS_REMOUNT;
1505 /* refresh mount options */
1506 zfs_unregister_callbacks(vfsp->vfs_data);
1507 return (zfs_register_callbacks(vfsp));
1509 } else if (why == ROOT_UNMOUNT) {
1510 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1511 (void) zfs_sync(vfsp, 0, 0);
1516 * if "why" is equal to anything else other than ROOT_INIT,
1517 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1521 #endif /* OPENSOLARIS_MOUNTROOT */
1525 zfs_mount(vfs_t *vfsp)
1527 kthread_t *td = curthread;
1528 vnode_t *mvp = vfsp->mnt_vnodecovered;
1529 cred_t *cr = td->td_ucred;
1534 if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
1538 * If full-owner-access is enabled and delegated administration is
1539 * turned on, we must set nosuid.
1541 if (zfs_super_owner &&
1542 dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
1543 secpolicy_fs_mount_clearopts(cr, vfsp);
1547 * Check for mount privilege?
1549 * If we don't have privilege then see if
1550 * we have local permission to allow it
1552 error = secpolicy_fs_mount(cr, mvp, vfsp);
1554 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != 0)
1557 if (!(vfsp->vfs_flag & MS_REMOUNT)) {
1561 * Make sure user is the owner of the mount point
1562 * or has sufficient privileges.
1565 vattr.va_mask = AT_UID;
1567 vn_lock(mvp, LK_SHARED | LK_RETRY);
1568 if (VOP_GETATTR(mvp, &vattr, cr)) {
1573 if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
1574 VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
1581 secpolicy_fs_mount_clearopts(cr, vfsp);
1585 * Refuse to mount a filesystem if we are in a local zone and the
1586 * dataset is not visible.
1588 if (!INGLOBALZONE(curthread) &&
1589 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1595 error = zfs_mount_label_policy(vfsp, osname);
1600 vfsp->vfs_flag |= MNT_NFS4ACLS;
1603 * When doing a remount, we simply refresh our temporary properties
1604 * according to those options set in the current VFS options.
1606 if (vfsp->vfs_flag & MS_REMOUNT) {
1607 /* refresh mount options */
1608 zfs_unregister_callbacks(vfsp->vfs_data);
1609 error = zfs_register_callbacks(vfsp);
1614 error = zfs_domount(vfsp, osname);
1619 * Add an extra VFS_HOLD on our parent vfs so that it can't
1620 * disappear due to a forced unmount.
1622 if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1623 VFS_HOLD(mvp->v_vfsp);
1631 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
1633 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1634 uint64_t refdbytes, availbytes, usedobjs, availobjs;
1636 statp->f_version = STATFS_VERSION;
1640 dmu_objset_space(zfsvfs->z_os,
1641 &refdbytes, &availbytes, &usedobjs, &availobjs);
1644 * The underlying storage pool actually uses multiple block sizes.
1645 * We report the fragsize as the smallest block size we support,
1646 * and we report our blocksize as the filesystem's maximum blocksize.
1648 statp->f_bsize = SPA_MINBLOCKSIZE;
1649 statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize;
1652 * The following report "total" blocks of various kinds in the
1653 * file system, but reported in terms of f_frsize - the
1657 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1658 statp->f_bfree = availbytes / statp->f_bsize;
1659 statp->f_bavail = statp->f_bfree; /* no root reservation */
1662 * statvfs() should really be called statufs(), because it assumes
1663 * static metadata. ZFS doesn't preallocate files, so the best
1664 * we can do is report the max that could possibly fit in f_files,
1665 * and that minus the number actually used in f_ffree.
1666 * For f_ffree, report the smaller of the number of object available
1667 * and the number of blocks (each object will take at least a block).
1669 statp->f_ffree = MIN(availobjs, statp->f_bfree);
1670 statp->f_files = statp->f_ffree + usedobjs;
1673 * We're a zfs filesystem.
1675 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
1677 strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
1678 sizeof(statp->f_mntfromname));
1679 strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
1680 sizeof(statp->f_mntonname));
1682 statp->f_namemax = ZFS_MAXNAMELEN;
1689 zfs_vnode_lock(vnode_t *vp, int flags)
1696 * Check if the file system wasn't forcibly unmounted in the meantime.
1698 error = vn_lock(vp, flags);
1699 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0) {
1708 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
1710 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1714 ZFS_ENTER_NOERROR(zfsvfs);
1716 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1718 *vpp = ZTOV(rootzp);
1723 error = zfs_vnode_lock(*vpp, flags);
1725 (*vpp)->v_vflag |= VV_ROOT;
1734 * Teardown the zfsvfs::z_os.
1736 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1737 * and 'z_teardown_inactive_lock' held.
1740 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1744 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1748 * We purge the parent filesystem's vfsp as the parent
1749 * filesystem and all of its snapshots have their vnode's
1750 * v_vfsp set to the parent's filesystem's vfsp. Note,
1751 * 'z_parent' is self referential for non-snapshots.
1753 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1754 #ifdef FREEBSD_NAMECACHE
1755 cache_purgevfs(zfsvfs->z_parent->z_vfs);
1760 * Close the zil. NB: Can't close the zil while zfs_inactive
1761 * threads are blocked as zil_close can call zfs_inactive.
1763 if (zfsvfs->z_log) {
1764 zil_close(zfsvfs->z_log);
1765 zfsvfs->z_log = NULL;
1768 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1771 * If we are not unmounting (ie: online recv) and someone already
1772 * unmounted this file system while we were doing the switcheroo,
1773 * or a reopen of z_os failed then just bail out now.
1775 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1776 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1777 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1782 * At this point there are no vops active, and any new vops will
1783 * fail with EIO since we have z_teardown_lock for writer (only
1784 * relavent for forced unmount).
1786 * Release all holds on dbufs.
1788 mutex_enter(&zfsvfs->z_znodes_lock);
1789 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1790 zp = list_next(&zfsvfs->z_all_znodes, zp))
1792 ASSERT(ZTOV(zp)->v_count >= 0);
1793 zfs_znode_dmu_fini(zp);
1795 mutex_exit(&zfsvfs->z_znodes_lock);
1798 * If we are unmounting, set the unmounted flag and let new vops
1799 * unblock. zfs_inactive will have the unmounted behavior, and all
1800 * other vops will fail with EIO.
1803 zfsvfs->z_unmounted = B_TRUE;
1804 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1805 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1809 * Some znodes might not be fully reclaimed, wait for them.
1811 mutex_enter(&zfsvfs->z_znodes_lock);
1812 while (list_head(&zfsvfs->z_all_znodes) != NULL) {
1813 msleep(zfsvfs, &zfsvfs->z_znodes_lock, 0,
1816 mutex_exit(&zfsvfs->z_znodes_lock);
1821 * z_os will be NULL if there was an error in attempting to reopen
1822 * zfsvfs, so just return as the properties had already been
1823 * unregistered and cached data had been evicted before.
1825 if (zfsvfs->z_os == NULL)
1829 * Unregister properties.
1831 zfs_unregister_callbacks(zfsvfs);
1836 if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
1837 if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1838 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1839 (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1846 zfs_umount(vfs_t *vfsp, int fflag)
1848 kthread_t *td = curthread;
1849 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1851 cred_t *cr = td->td_ucred;
1854 ret = secpolicy_fs_unmount(cr, vfsp);
1856 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1857 ZFS_DELEG_PERM_MOUNT, cr))
1862 * We purge the parent filesystem's vfsp as the parent filesystem
1863 * and all of its snapshots have their vnode's v_vfsp set to the
1864 * parent's filesystem's vfsp. Note, 'z_parent' is self
1865 * referential for non-snapshots.
1867 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1870 * Unmount any snapshots mounted under .zfs before unmounting the
1873 if (zfsvfs->z_ctldir != NULL) {
1874 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1876 ret = vflush(vfsp, 0, 0, td);
1877 ASSERT(ret == EBUSY);
1878 if (!(fflag & MS_FORCE)) {
1879 if (zfsvfs->z_ctldir->v_count > 1)
1881 ASSERT(zfsvfs->z_ctldir->v_count == 1);
1883 zfsctl_destroy(zfsvfs);
1884 ASSERT(zfsvfs->z_ctldir == NULL);
1887 if (fflag & MS_FORCE) {
1889 * Mark file system as unmounted before calling
1890 * vflush(FORCECLOSE). This way we ensure no future vnops
1891 * will be called and risk operating on DOOMED vnodes.
1893 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1894 zfsvfs->z_unmounted = B_TRUE;
1895 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1899 * Flush all the files.
1901 ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, td);
1903 if (!zfsvfs->z_issnap) {
1904 zfsctl_create(zfsvfs);
1905 ASSERT(zfsvfs->z_ctldir != NULL);
1910 if (!(fflag & MS_FORCE)) {
1912 * Check the number of active vnodes in the file system.
1913 * Our count is maintained in the vfs structure, but the
1914 * number is off by 1 to indicate a hold on the vfs
1917 * The '.zfs' directory maintains a reference of its
1918 * own, and any active references underneath are
1919 * reflected in the vnode count.
1921 if (zfsvfs->z_ctldir == NULL) {
1922 if (vfsp->vfs_count > 1)
1925 if (vfsp->vfs_count > 2 ||
1926 zfsvfs->z_ctldir->v_count > 1)
1931 vfsp->mnt_kern_flag |= MNTK_UNMOUNTF;
1935 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1939 * z_os will be NULL if there was an error in
1940 * attempting to reopen zfsvfs.
1944 * Unset the objset user_ptr.
1946 mutex_enter(&os->os_user_ptr_lock);
1947 dmu_objset_set_user(os, NULL);
1948 mutex_exit(&os->os_user_ptr_lock);
1951 * Finally release the objset
1953 dmu_objset_disown(os, zfsvfs);
1957 * We can now safely destroy the '.zfs' directory node.
1959 if (zfsvfs->z_ctldir != NULL)
1960 zfsctl_destroy(zfsvfs);
1961 if (zfsvfs->z_issnap) {
1962 vnode_t *svp = vfsp->mnt_vnodecovered;
1964 if (svp->v_count >= 2)
1973 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
1975 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1980 * zfs_zget() can't operate on virtual entries like .zfs/ or
1981 * .zfs/snapshot/ directories, that's why we return EOPNOTSUPP.
1982 * This will make NFS to switch to LOOKUP instead of using VGET.
1984 if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR)
1985 return (EOPNOTSUPP);
1988 err = zfs_zget(zfsvfs, ino, &zp);
1989 if (err == 0 && zp->z_unlinked) {
1997 err = zfs_vnode_lock(*vpp, flags);
2004 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
2005 struct ucred **credanonp, int *numsecflavors, int **secflavors)
2007 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2010 * If this is regular file system vfsp is the same as
2011 * zfsvfs->z_parent->z_vfs, but if it is snapshot,
2012 * zfsvfs->z_parent->z_vfs represents parent file system
2013 * which we have to use here, because only this file system
2014 * has mnt_export configured.
2016 return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
2017 credanonp, numsecflavors, secflavors));
2020 CTASSERT(SHORT_FID_LEN <= sizeof(struct fid));
2021 CTASSERT(LONG_FID_LEN <= sizeof(struct fid));
2024 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp)
2026 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2028 uint64_t object = 0;
2029 uint64_t fid_gen = 0;
2039 * On FreeBSD we can get snapshot's mount point or its parent file
2040 * system mount point depending if snapshot is already mounted or not.
2042 if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
2043 zfid_long_t *zlfid = (zfid_long_t *)fidp;
2044 uint64_t objsetid = 0;
2045 uint64_t setgen = 0;
2047 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2048 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2050 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2051 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2055 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
2061 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2062 zfid_short_t *zfid = (zfid_short_t *)fidp;
2064 for (i = 0; i < sizeof (zfid->zf_object); i++)
2065 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2067 for (i = 0; i < sizeof (zfid->zf_gen); i++)
2068 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2074 /* A zero fid_gen means we are in the .zfs control directories */
2076 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
2077 *vpp = zfsvfs->z_ctldir;
2078 ASSERT(*vpp != NULL);
2079 if (object == ZFSCTL_INO_SNAPDIR) {
2080 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
2081 0, NULL, NULL, NULL, NULL, NULL) == 0);
2086 err = zfs_vnode_lock(*vpp, flags | LK_RETRY);
2092 gen_mask = -1ULL >> (64 - 8 * i);
2094 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2095 if (err = zfs_zget(zfsvfs, object, &zp)) {
2099 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2101 zp_gen = zp_gen & gen_mask;
2104 if (zp->z_unlinked || zp_gen != fid_gen) {
2105 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2113 err = zfs_vnode_lock(*vpp, flags | LK_RETRY);
2115 vnode_create_vobject(*vpp, zp->z_size, curthread);
2122 * Block out VOPs and close zfsvfs_t::z_os
2124 * Note, if successful, then we return with the 'z_teardown_lock' and
2125 * 'z_teardown_inactive_lock' write held.
2128 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2132 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2134 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
2140 * Reopen zfsvfs_t::z_os and release VOPs.
2143 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
2147 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
2148 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2150 err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
2153 zfsvfs->z_os = NULL;
2156 uint64_t sa_obj = 0;
2159 * Make sure version hasn't changed
2162 err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION,
2163 &zfsvfs->z_version);
2168 err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
2169 ZFS_SA_ATTRS, 8, 1, &sa_obj);
2171 if (err && zfsvfs->z_version >= ZPL_VERSION_SA)
2174 if ((err = sa_setup(zfsvfs->z_os, sa_obj,
2175 zfs_attr_table, ZPL_END, &zfsvfs->z_attr_table)) != 0)
2178 if (zfsvfs->z_version >= ZPL_VERSION_SA)
2179 sa_register_update_callback(zfsvfs->z_os,
2182 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2184 zfs_set_fuid_feature(zfsvfs);
2187 * Attempt to re-establish all the active znodes with
2188 * their dbufs. If a zfs_rezget() fails, then we'll let
2189 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2190 * when they try to use their znode.
2192 mutex_enter(&zfsvfs->z_znodes_lock);
2193 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2194 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2195 (void) zfs_rezget(zp);
2197 mutex_exit(&zfsvfs->z_znodes_lock);
2201 /* release the VOPs */
2202 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2203 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
2207 * Since we couldn't reopen zfsvfs::z_os, or
2208 * setup the sa framework force unmount this file system.
2210 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2211 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
2217 zfs_freevfs(vfs_t *vfsp)
2219 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2223 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2224 * from zfs_mount(). Release it here. If we came through
2225 * zfs_mountroot() instead, we didn't grab an extra hold, so
2226 * skip the VFS_RELE for rootvfs.
2228 if (zfsvfs->z_issnap && (vfsp != rootvfs))
2229 VFS_RELE(zfsvfs->z_parent->z_vfs);
2232 zfsvfs_free(zfsvfs);
2234 atomic_add_32(&zfs_active_fs_count, -1);
2238 static int desiredvnodes_backup;
2242 zfs_vnodes_adjust(void)
2245 int newdesiredvnodes;
2247 desiredvnodes_backup = desiredvnodes;
2250 * We calculate newdesiredvnodes the same way it is done in
2251 * vntblinit(). If it is equal to desiredvnodes, it means that
2252 * it wasn't tuned by the administrator and we can tune it down.
2254 newdesiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 *
2255 vm_kmem_size / (5 * (sizeof(struct vm_object) +
2256 sizeof(struct vnode))));
2257 if (newdesiredvnodes == desiredvnodes)
2258 desiredvnodes = (3 * newdesiredvnodes) / 4;
2263 zfs_vnodes_adjust_back(void)
2267 desiredvnodes = desiredvnodes_backup;
2275 printf("ZFS filesystem version " ZPL_VERSION_STRING "\n");
2278 * Initialize .zfs directory structures
2283 * Initialize znode cache, vnode ops, etc...
2288 * Reduce number of vnodes. Originally number of vnodes is calculated
2289 * with UFS inode in mind. We reduce it here, because it's too big for
2292 zfs_vnodes_adjust();
2294 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2302 zfs_vnodes_adjust_back();
2308 return (zfs_active_fs_count != 0);
2312 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2315 objset_t *os = zfsvfs->z_os;
2318 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2321 if (newvers < zfsvfs->z_version)
2324 if (zfs_spa_version_map(newvers) >
2325 spa_version(dmu_objset_spa(zfsvfs->z_os)))
2328 tx = dmu_tx_create(os);
2329 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2330 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2331 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2333 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2335 error = dmu_tx_assign(tx, TXG_WAIT);
2341 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2342 8, 1, &newvers, tx);
2349 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2352 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2354 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2355 DMU_OT_NONE, 0, tx);
2357 error = zap_add(os, MASTER_NODE_OBJ,
2358 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2359 ASSERT3U(error, ==, 0);
2361 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2362 sa_register_update_callback(os, zfs_sa_upgrade);
2365 spa_history_log_internal(LOG_DS_UPGRADE,
2366 dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
2367 zfsvfs->z_version, newvers, dmu_objset_id(os));
2371 zfsvfs->z_version = newvers;
2373 zfs_set_fuid_feature(zfsvfs);
2379 * Read a property stored within the master node.
2382 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2388 * Look up the file system's value for the property. For the
2389 * version property, we look up a slightly different string.
2391 if (prop == ZFS_PROP_VERSION)
2392 pname = ZPL_VERSION_STR;
2394 pname = zfs_prop_to_name(prop);
2397 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2399 if (error == ENOENT) {
2400 /* No value set, use the default value */
2402 case ZFS_PROP_VERSION:
2403 *value = ZPL_VERSION;
2405 case ZFS_PROP_NORMALIZE:
2406 case ZFS_PROP_UTF8ONLY:
2410 *value = ZFS_CASE_SENSITIVE;