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/sysmacros.h>
32 #include <sys/pathname.h>
33 #include <sys/vnode.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/mntent.h>
37 #include <sys/mount.h>
38 #include <sys/cmn_err.h>
39 #include "fs/fs_subr.h"
40 #include <sys/zfs_znode.h>
41 #include <sys/zfs_dir.h>
43 #include <sys/fs/zfs.h>
45 #include <sys/dsl_prop.h>
46 #include <sys/dsl_dataset.h>
47 #include <sys/dsl_deleg.h>
51 #include <sys/varargs.h>
52 #include <sys/policy.h>
53 #include <sys/atomic.h>
54 #include <sys/mkdev.h>
55 #include <sys/modctl.h>
56 #include <sys/refstr.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/zfs_ctldir.h>
59 #include <sys/zfs_fuid.h>
60 #include <sys/bootconf.h>
61 #include <sys/sunddi.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/spa_boot.h>
66 #include "zfs_comutil.h"
70 static major_t zfs_major;
71 static minor_t zfs_minor;
72 static kmutex_t zfs_dev_mtx;
74 extern int sys_shutdown;
76 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
77 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
78 static void zfs_freevfs(vfs_t *vfsp);
81 * We need to keep a count of active fs's.
82 * This is necessary to prevent our module
83 * from being unloaded after a umount -f
85 static uint32_t zfs_active_fs_count = 0;
87 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
88 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
89 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
90 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
93 * MO_DEFAULT is not used since the default value is determined
94 * by the equivalent property.
96 static mntopt_t mntopts[] = {
97 { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
98 { MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
99 { MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
100 { MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
103 static mntopts_t zfs_mntopts = {
104 sizeof (mntopts) / sizeof (mntopt_t),
110 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
113 * Data integrity is job one. We don't want a compromised kernel
114 * writing to the storage pool, so we never sync during panic.
120 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
121 * to sync metadata, which they would otherwise cache indefinitely.
122 * Semantically, the only requirement is that the sync be initiated.
123 * The DMU syncs out txgs frequently, so there's nothing to do.
125 if (flag & SYNC_ATTR)
130 * Sync a specific filesystem.
132 zfsvfs_t *zfsvfs = vfsp->vfs_data;
136 dp = dmu_objset_pool(zfsvfs->z_os);
139 * If the system is shutting down, then skip any
140 * filesystems which may exist on a suspended pool.
142 if (sys_shutdown && spa_suspended(dp->dp_spa)) {
147 if (zfsvfs->z_log != NULL)
148 zil_commit(zfsvfs->z_log, 0);
153 * Sync all ZFS filesystems. This is what happens when you
154 * run sync(1M). Unlike other filesystems, ZFS honors the
155 * request by waiting for all pools to commit all dirty data.
162 EXPORT_SYMBOL(zfs_sync);
165 zfs_create_unique_device(dev_t *dev)
170 ASSERT3U(zfs_minor, <=, MAXMIN32);
171 minor_t start = zfs_minor;
173 mutex_enter(&zfs_dev_mtx);
174 if (zfs_minor >= MAXMIN32) {
176 * If we're still using the real major
177 * keep out of /dev/zfs and /dev/zvol minor
178 * number space. If we're using a getudev()'ed
179 * major number, we can use all of its minors.
181 if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
182 zfs_minor = ZFS_MIN_MINOR;
188 *dev = makedevice(zfs_major, zfs_minor);
189 mutex_exit(&zfs_dev_mtx);
190 } while (vfs_devismounted(*dev) && zfs_minor != start);
191 if (zfs_minor == start) {
193 * We are using all ~262,000 minor numbers for the
194 * current major number. Create a new major number.
196 if ((new_major = getudev()) == (major_t)-1) {
198 "zfs_mount: Can't get unique major "
202 mutex_enter(&zfs_dev_mtx);
203 zfs_major = new_major;
206 mutex_exit(&zfs_dev_mtx);
210 /* CONSTANTCONDITION */
217 atime_changed_cb(void *arg, uint64_t newval)
219 zfsvfs_t *zfsvfs = arg;
221 if (newval == TRUE) {
222 zfsvfs->z_atime = TRUE;
223 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
224 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
226 zfsvfs->z_atime = FALSE;
227 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
228 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
233 xattr_changed_cb(void *arg, uint64_t newval)
235 zfsvfs_t *zfsvfs = arg;
237 if (newval == TRUE) {
238 /* XXX locking on vfs_flag? */
239 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
240 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
241 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
243 /* XXX locking on vfs_flag? */
244 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
245 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
246 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
251 blksz_changed_cb(void *arg, uint64_t newval)
253 zfsvfs_t *zfsvfs = arg;
255 if (newval < SPA_MINBLOCKSIZE ||
256 newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
257 newval = SPA_MAXBLOCKSIZE;
259 zfsvfs->z_max_blksz = newval;
260 zfsvfs->z_vfs->vfs_bsize = newval;
264 readonly_changed_cb(void *arg, uint64_t newval)
266 zfsvfs_t *zfsvfs = arg;
269 /* XXX locking on vfs_flag? */
270 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
271 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
272 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
274 /* XXX locking on vfs_flag? */
275 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
276 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
277 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
282 devices_changed_cb(void *arg, uint64_t newval)
284 zfsvfs_t *zfsvfs = arg;
286 if (newval == FALSE) {
287 zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
288 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
289 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
291 zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
292 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
293 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, 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 devices, do_devices = B_FALSE;
385 int xattr, do_xattr = B_FALSE;
386 int atime, do_atime = B_FALSE;
390 zfsvfs = vfsp->vfs_data;
395 * The act of registering our callbacks will destroy any mount
396 * options we may have. In order to enable temporary overrides
397 * of mount options, we stash away the current values and
398 * restore them after we register the callbacks.
400 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
401 !spa_writeable(dmu_objset_spa(os))) {
403 do_readonly = B_TRUE;
404 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
406 do_readonly = B_TRUE;
408 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
414 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
417 } else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
422 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
425 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
430 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
433 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
437 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
440 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
444 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
447 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
453 * nbmand is a special property. It can only be changed at
456 * This is weird, but it is documented to only be changeable
459 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
461 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
464 char osname[MAXNAMELEN];
466 dmu_objset_name(os, osname);
467 if ((error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
474 * Register property callbacks.
476 * It would probably be fine to just check for i/o error from
477 * the first prop_register(), but I guess I like to go
480 ds = dmu_objset_ds(os);
481 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
482 error = error ? error : dsl_prop_register(ds,
483 "xattr", xattr_changed_cb, zfsvfs);
484 error = error ? error : dsl_prop_register(ds,
485 "recordsize", blksz_changed_cb, zfsvfs);
486 error = error ? error : dsl_prop_register(ds,
487 "readonly", readonly_changed_cb, zfsvfs);
488 error = error ? error : dsl_prop_register(ds,
489 "devices", devices_changed_cb, zfsvfs);
490 error = error ? error : dsl_prop_register(ds,
491 "setuid", setuid_changed_cb, zfsvfs);
492 error = error ? error : dsl_prop_register(ds,
493 "exec", exec_changed_cb, zfsvfs);
494 error = error ? error : dsl_prop_register(ds,
495 "snapdir", snapdir_changed_cb, zfsvfs);
496 error = error ? error : dsl_prop_register(ds,
497 "aclinherit", acl_inherit_changed_cb, zfsvfs);
498 error = error ? error : dsl_prop_register(ds,
499 "vscan", vscan_changed_cb, zfsvfs);
504 * Invoke our callbacks to restore temporary mount options.
507 readonly_changed_cb(zfsvfs, readonly);
509 setuid_changed_cb(zfsvfs, setuid);
511 exec_changed_cb(zfsvfs, exec);
513 devices_changed_cb(zfsvfs, devices);
515 xattr_changed_cb(zfsvfs, xattr);
517 atime_changed_cb(zfsvfs, atime);
519 nbmand_changed_cb(zfsvfs, nbmand);
525 * We may attempt to unregister some callbacks that are not
526 * registered, but this is OK; it will simply return ENOMSG,
527 * which we will ignore.
529 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
530 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
531 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
532 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
533 (void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
534 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
535 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
536 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
537 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
539 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
543 EXPORT_SYMBOL(zfs_register_callbacks);
544 #endif /* HAVE_ZPL */
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
597 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
598 char *domainbuf, int buflen, uid_t *ridp)
603 fuid = strtonum(fuidstr, NULL);
605 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
607 (void) strlcpy(domainbuf, domain, buflen);
610 *ridp = FUID_RID(fuid);
614 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
617 case ZFS_PROP_USERUSED:
618 return (DMU_USERUSED_OBJECT);
619 case ZFS_PROP_GROUPUSED:
620 return (DMU_GROUPUSED_OBJECT);
621 case ZFS_PROP_USERQUOTA:
622 return (zfsvfs->z_userquota_obj);
623 case ZFS_PROP_GROUPQUOTA:
624 return (zfsvfs->z_groupquota_obj);
632 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
633 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
638 zfs_useracct_t *buf = vbuf;
641 if (!dmu_objset_userspace_present(zfsvfs->z_os))
644 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
650 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
651 (error = zap_cursor_retrieve(&zc, &za)) == 0;
652 zap_cursor_advance(&zc)) {
653 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
657 fuidstr_to_sid(zfsvfs, za.za_name,
658 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
660 buf->zu_space = za.za_first_integer;
666 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
667 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
668 *cookiep = zap_cursor_serialize(&zc);
669 zap_cursor_fini(&zc);
672 EXPORT_SYMBOL(zfs_userspace_many);
675 * buf must be big enough (eg, 32 bytes)
678 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
679 char *buf, boolean_t addok)
684 if (domain && domain[0]) {
685 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
689 fuid = FUID_ENCODE(domainid, rid);
690 (void) sprintf(buf, "%llx", (longlong_t)fuid);
695 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
696 const char *domain, uint64_t rid, uint64_t *valp)
704 if (!dmu_objset_userspace_present(zfsvfs->z_os))
707 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
711 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
715 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
720 EXPORT_SYMBOL(zfs_userspace_one);
723 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
724 const char *domain, uint64_t rid, uint64_t quota)
730 boolean_t fuid_dirtied;
732 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
735 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
738 objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
739 &zfsvfs->z_groupquota_obj;
741 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
744 fuid_dirtied = zfsvfs->z_fuid_dirty;
746 tx = dmu_tx_create(zfsvfs->z_os);
747 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
749 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
750 zfs_userquota_prop_prefixes[type]);
753 zfs_fuid_txhold(zfsvfs, tx);
754 err = dmu_tx_assign(tx, TXG_WAIT);
760 mutex_enter(&zfsvfs->z_lock);
762 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
764 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
765 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
767 mutex_exit(&zfsvfs->z_lock);
770 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
774 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, "a, tx);
778 zfs_fuid_sync(zfsvfs, tx);
782 EXPORT_SYMBOL(zfs_set_userquota);
785 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
788 uint64_t used, quota, usedobj, quotaobj;
791 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
792 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
794 if (quotaobj == 0 || zfsvfs->z_replay)
797 (void) sprintf(buf, "%llx", (longlong_t)fuid);
798 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, "a);
802 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
805 return (used >= quota);
807 EXPORT_SYMBOL(zfs_fuid_overquota);
810 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
815 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
817 fuid = isgroup ? zp->z_gid : zp->z_uid;
819 if (quotaobj == 0 || zfsvfs->z_replay)
822 return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
824 EXPORT_SYMBOL(zfs_owner_overquota);
827 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
835 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
838 * We claim to always be readonly so we can open snapshots;
839 * other ZPL code will prevent us from writing to snapshots.
841 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
843 kmem_free(zfsvfs, sizeof (zfsvfs_t));
848 * Initialize the zfs-specific filesystem structure.
849 * Should probably make this a kmem cache, shuffle fields,
850 * and just bzero up to z_hold_mtx[].
852 zfsvfs->z_vfs = NULL;
853 zfsvfs->z_parent = zfsvfs;
854 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
855 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
858 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
861 } else if (zfsvfs->z_version >
862 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
863 (void) printk("Can't mount a version %lld file system "
864 "on a version %lld pool\n. Pool must be upgraded to mount "
865 "this file system.", (u_longlong_t)zfsvfs->z_version,
866 (u_longlong_t)spa_version(dmu_objset_spa(os)));
870 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
872 zfsvfs->z_norm = (int)zval;
874 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
876 zfsvfs->z_utf8 = (zval != 0);
878 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
880 zfsvfs->z_case = (uint_t)zval;
883 * Fold case on file systems that are always or sometimes case
886 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
887 zfsvfs->z_case == ZFS_CASE_MIXED)
888 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
890 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
891 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
893 if (zfsvfs->z_use_sa) {
894 /* should either have both of these objects or none */
895 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
901 * Pre SA versions file systems should never touch
902 * either the attribute registration or layout objects.
907 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
908 &zfsvfs->z_attr_table);
912 if (zfsvfs->z_version >= ZPL_VERSION_SA)
913 sa_register_update_callback(os, zfs_sa_upgrade);
915 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
919 ASSERT(zfsvfs->z_root != 0);
921 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
922 &zfsvfs->z_unlinkedobj);
926 error = zap_lookup(os, MASTER_NODE_OBJ,
927 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
928 8, 1, &zfsvfs->z_userquota_obj);
929 if (error && error != ENOENT)
932 error = zap_lookup(os, MASTER_NODE_OBJ,
933 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
934 8, 1, &zfsvfs->z_groupquota_obj);
935 if (error && error != ENOENT)
938 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
939 &zfsvfs->z_fuid_obj);
940 if (error && error != ENOENT)
943 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
944 &zfsvfs->z_shares_dir);
945 if (error && error != ENOENT)
948 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
949 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
950 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
951 offsetof(znode_t, z_link_node));
952 rrw_init(&zfsvfs->z_teardown_lock);
953 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
954 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
955 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
956 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
962 dmu_objset_disown(os, zfsvfs);
964 kmem_free(zfsvfs, sizeof (zfsvfs_t));
969 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
973 error = zfs_register_callbacks(zfsvfs->z_vfs);
978 * Set the objset user_ptr to track its zfsvfs.
980 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
981 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
982 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
984 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
987 * If we are not mounting (ie: online recv), then we don't
988 * have to worry about replaying the log as we blocked all
989 * operations out since we closed the ZIL.
995 * During replay we remove the read only flag to
996 * allow replays to succeed.
998 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1000 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1002 zfs_unlinked_drain(zfsvfs);
1005 * Parse and replay the intent log.
1007 * Because of ziltest, this must be done after
1008 * zfs_unlinked_drain(). (Further note: ziltest
1009 * doesn't use readonly mounts, where
1010 * zfs_unlinked_drain() isn't called.) This is because
1011 * ziltest causes spa_sync() to think it's committed,
1012 * but actually it is not, so the intent log contains
1013 * many txg's worth of changes.
1015 * In particular, if object N is in the unlinked set in
1016 * the last txg to actually sync, then it could be
1017 * actually freed in a later txg and then reallocated
1018 * in a yet later txg. This would write a "create
1019 * object N" record to the intent log. Normally, this
1020 * would be fine because the spa_sync() would have
1021 * written out the fact that object N is free, before
1022 * we could write the "create object N" intent log
1025 * But when we are in ziltest mode, we advance the "open
1026 * txg" without actually spa_sync()-ing the changes to
1027 * disk. So we would see that object N is still
1028 * allocated and in the unlinked set, and there is an
1029 * intent log record saying to allocate it.
1031 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1032 if (zil_replay_disable) {
1033 zil_destroy(zfsvfs->z_log, B_FALSE);
1035 zfsvfs->z_replay = B_TRUE;
1036 zil_replay(zfsvfs->z_os, zfsvfs,
1038 zfsvfs->z_replay = B_FALSE;
1041 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1048 zfsvfs_free(zfsvfs_t *zfsvfs)
1051 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1054 * This is a barrier to prevent the filesystem from going away in
1055 * zfs_znode_move() until we can safely ensure that the filesystem is
1056 * not unmounted. We consider the filesystem valid before the barrier
1057 * and invalid after the barrier.
1059 rw_enter(&zfsvfs_lock, RW_READER);
1060 rw_exit(&zfsvfs_lock);
1062 zfs_fuid_destroy(zfsvfs);
1064 mutex_destroy(&zfsvfs->z_znodes_lock);
1065 mutex_destroy(&zfsvfs->z_lock);
1066 list_destroy(&zfsvfs->z_all_znodes);
1067 rrw_destroy(&zfsvfs->z_teardown_lock);
1068 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1069 rw_destroy(&zfsvfs->z_fuid_lock);
1070 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1071 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1072 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1076 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1078 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1079 if (zfsvfs->z_use_fuids && zfsvfs->z_vfs) {
1080 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1081 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1082 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1083 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1084 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1085 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1087 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1091 zfs_domount(vfs_t *vfsp, char *osname)
1094 uint64_t recordsize, fsid_guid;
1101 error = zfsvfs_create(osname, &zfsvfs);
1104 zfsvfs->z_vfs = vfsp;
1106 /* Initialize the generic filesystem structure. */
1107 vfsp->vfs_bcount = 0;
1108 vfsp->vfs_data = NULL;
1110 if (zfs_create_unique_device(&mount_dev) == -1) {
1114 ASSERT(vfs_devismounted(mount_dev) == 0);
1116 if ((error = dsl_prop_get_integer(osname, "recordsize",
1117 &recordsize, NULL)))
1120 vfsp->vfs_dev = mount_dev;
1121 vfsp->vfs_fstype = zfsfstype;
1122 vfsp->vfs_bsize = recordsize;
1123 vfsp->vfs_flag |= VFS_NOTRUNC;
1124 vfsp->vfs_data = zfsvfs;
1127 * The fsid is 64 bits, composed of an 8-bit fs type, which
1128 * separates our fsid from any other filesystem types, and a
1129 * 56-bit objset unique ID. The objset unique ID is unique to
1130 * all objsets open on this system, provided by unique_create().
1131 * The 8-bit fs type must be put in the low bits of fsid[1]
1132 * because that's where other Solaris filesystems put it.
1134 fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1135 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1136 vfsp->vfs_fsid.val[0] = fsid_guid;
1137 vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1141 * Set features for file system.
1143 zfs_set_fuid_feature(zfsvfs);
1144 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1145 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1146 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1147 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1148 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1149 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1150 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1152 vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1154 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1157 atime_changed_cb(zfsvfs, B_FALSE);
1158 readonly_changed_cb(zfsvfs, B_TRUE);
1159 if ((error = dsl_prop_get_integer(osname,"xattr",&pval,NULL)))
1161 xattr_changed_cb(zfsvfs, pval);
1162 zfsvfs->z_issnap = B_TRUE;
1163 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1165 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1166 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1167 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1169 error = zfsvfs_setup(zfsvfs, B_TRUE);
1172 if (!zfsvfs->z_issnap)
1173 zfsctl_create(zfsvfs);
1176 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1177 zfsvfs_free(zfsvfs);
1179 atomic_add_32(&zfs_active_fs_count, 1);
1184 EXPORT_SYMBOL(zfs_domount);
1187 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1189 objset_t *os = zfsvfs->z_os;
1190 struct dsl_dataset *ds;
1193 * Unregister properties.
1195 if (!dmu_objset_is_snapshot(os)) {
1196 ds = dmu_objset_ds(os);
1197 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
1200 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
1203 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
1206 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
1209 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
1212 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
1215 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
1218 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
1221 VERIFY(dsl_prop_unregister(ds, "aclinherit",
1222 acl_inherit_changed_cb, zfsvfs) == 0);
1224 VERIFY(dsl_prop_unregister(ds, "vscan",
1225 vscan_changed_cb, zfsvfs) == 0);
1228 EXPORT_SYMBOL(zfs_unregister_callbacks);
1230 #ifdef HAVE_MLSLABEL
1232 * zfs_check_global_label:
1233 * Check that the hex label string is appropriate for the dataset
1234 * being mounted into the global_zone proper.
1236 * Return an error if the hex label string is not default or
1237 * admin_low/admin_high. For admin_low labels, the corresponding
1238 * dataset must be readonly.
1241 zfs_check_global_label(const char *dsname, const char *hexsl)
1243 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1245 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1247 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1248 /* must be readonly */
1251 if (dsl_prop_get_integer(dsname,
1252 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1254 return (rdonly ? 0 : EACCES);
1258 #endif /* HAVE_MLSLABEL */
1261 * zfs_mount_label_policy:
1262 * Determine whether the mount is allowed according to MAC check.
1263 * by comparing (where appropriate) label of the dataset against
1264 * the label of the zone being mounted into. If the dataset has
1265 * no label, create one.
1268 * 0 : access allowed
1269 * >0 : error code, such as EACCES
1272 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1275 zone_t *mntzone = NULL;
1276 ts_label_t *mnt_tsl;
1279 char ds_hexsl[MAXNAMELEN];
1281 retv = EACCES; /* assume the worst */
1284 * Start by getting the dataset label if it exists.
1286 error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1287 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1292 * If labeling is NOT enabled, then disallow the mount of datasets
1293 * which have a non-default label already. No other label checks
1296 if (!is_system_labeled()) {
1297 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1303 * Get the label of the mountpoint. If mounting into the global
1304 * zone (i.e. mountpoint is not within an active zone and the
1305 * zoned property is off), the label must be default or
1306 * admin_low/admin_high only; no other checks are needed.
1308 mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1309 if (mntzone->zone_id == GLOBAL_ZONEID) {
1314 if (dsl_prop_get_integer(osname,
1315 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1318 return (zfs_check_global_label(osname, ds_hexsl));
1321 * This is the case of a zone dataset being mounted
1322 * initially, before the zone has been fully created;
1323 * allow this mount into global zone.
1328 mnt_tsl = mntzone->zone_slabel;
1329 ASSERT(mnt_tsl != NULL);
1330 label_hold(mnt_tsl);
1331 mnt_sl = label2bslabel(mnt_tsl);
1333 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1335 * The dataset doesn't have a real label, so fabricate one.
1339 if (l_to_str_internal(mnt_sl, &str) == 0 &&
1340 dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1341 ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0)
1344 kmem_free(str, strlen(str) + 1);
1345 } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1347 * Now compare labels to complete the MAC check. If the
1348 * labels are equal then allow access. If the mountpoint
1349 * label dominates the dataset label, allow readonly access.
1350 * Otherwise, access is denied.
1352 if (blequal(mnt_sl, &ds_sl))
1354 else if (bldominates(mnt_sl, &ds_sl)) {
1355 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1360 label_rele(mnt_tsl);
1366 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1369 static int zfsrootdone = 0;
1370 zfsvfs_t *zfsvfs = NULL;
1379 * The filesystem that we mount as root is defined in the
1380 * boot property "zfs-bootfs" with a format of
1381 * "poolname/root-dataset-objnum".
1383 if (why == ROOT_INIT) {
1387 * the process of doing a spa_load will require the
1388 * clock to be set before we could (for example) do
1389 * something better by looking at the timestamp on
1390 * an uberblock, so just set it to -1.
1394 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1395 cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1399 zfs_devid = spa_get_bootprop("diskdevid");
1400 error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1402 spa_free_bootprop(zfs_devid);
1404 spa_free_bootprop(zfs_bootfs);
1405 cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1409 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1410 spa_free_bootprop(zfs_bootfs);
1411 cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1416 spa_free_bootprop(zfs_bootfs);
1418 if (error = vfs_lock(vfsp))
1421 if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1422 cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1426 zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1428 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1429 cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1434 mutex_enter(&vp->v_lock);
1435 vp->v_flag |= VROOT;
1436 mutex_exit(&vp->v_lock);
1440 * Leave rootvp held. The root file system is never unmounted.
1443 vfs_add((struct vnode *)0, vfsp,
1444 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1448 } else if (why == ROOT_REMOUNT) {
1449 readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1450 vfsp->vfs_flag |= VFS_REMOUNT;
1452 /* refresh mount options */
1453 zfs_unregister_callbacks(vfsp->vfs_data);
1454 return (zfs_register_callbacks(vfsp));
1456 } else if (why == ROOT_UNMOUNT) {
1457 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1458 (void) zfs_sync(vfsp, 0, 0);
1463 * if "why" is equal to anything else other than ROOT_INIT,
1464 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1471 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1476 uio_seg_t fromspace = (uap->flags & MS_SYSSPACE) ?
1477 UIO_SYSSPACE : UIO_USERSPACE;
1480 if (mvp->v_type != VDIR)
1483 mutex_enter(&mvp->v_lock);
1484 if ((uap->flags & MS_REMOUNT) == 0 &&
1485 (uap->flags & MS_OVERLAY) == 0 &&
1486 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1487 mutex_exit(&mvp->v_lock);
1490 mutex_exit(&mvp->v_lock);
1493 * ZFS does not support passing unparsed data in via MS_DATA.
1494 * Users should use the MS_OPTIONSTR interface; this means
1495 * that all option parsing is already done and the options struct
1496 * can be interrogated.
1498 if ((uap->flags & MS_DATA) && uap->datalen > 0)
1502 * Get the objset name (the "special" mount argument).
1504 if ((error = pn_get(uap->spec, fromspace, &spn)))
1507 osname = spn.pn_path;
1510 * Check for mount privilege?
1512 * If we don't have privilege then see if
1513 * we have local permission to allow it
1515 error = secpolicy_fs_mount(cr, mvp, vfsp);
1517 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1521 * Make sure user is the owner of the mount point
1522 * or has sufficient privileges.
1525 vattr.va_mask = AT_UID;
1527 if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1531 if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1532 VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1535 secpolicy_fs_mount_clearopts(cr, vfsp);
1542 * Refuse to mount a filesystem if we are in a local zone and the
1543 * dataset is not visible.
1545 if (!INGLOBALZONE(curproc) &&
1546 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1551 error = zfs_mount_label_policy(vfsp, osname);
1556 * When doing a remount, we simply refresh our temporary properties
1557 * according to those options set in the current VFS options.
1559 if (uap->flags & MS_REMOUNT) {
1560 /* refresh mount options */
1561 zfs_unregister_callbacks(vfsp->vfs_data);
1562 error = zfs_register_callbacks(vfsp);
1566 error = zfs_domount(vfsp, osname);
1569 * Add an extra VFS_HOLD on our parent vfs so that it can't
1570 * disappear due to a forced unmount.
1572 if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1573 VFS_HOLD(mvp->v_vfsp);
1581 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1583 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1585 uint64_t refdbytes, availbytes, usedobjs, availobjs;
1589 dmu_objset_space(zfsvfs->z_os,
1590 &refdbytes, &availbytes, &usedobjs, &availobjs);
1593 * The underlying storage pool actually uses multiple block sizes.
1594 * We report the fragsize as the smallest block size we support,
1595 * and we report our blocksize as the filesystem's maximum blocksize.
1597 statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1598 statp->f_bsize = zfsvfs->z_max_blksz;
1601 * The following report "total" blocks of various kinds in the
1602 * file system, but reported in terms of f_frsize - the
1606 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1607 statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1608 statp->f_bavail = statp->f_bfree; /* no root reservation */
1611 * statvfs() should really be called statufs(), because it assumes
1612 * static metadata. ZFS doesn't preallocate files, so the best
1613 * we can do is report the max that could possibly fit in f_files,
1614 * and that minus the number actually used in f_ffree.
1615 * For f_ffree, report the smaller of the number of object available
1616 * and the number of blocks (each object will take at least a block).
1618 statp->f_ffree = MIN(availobjs, statp->f_bfree);
1619 statp->f_favail = statp->f_ffree; /* no "root reservation" */
1620 statp->f_files = statp->f_ffree + usedobjs;
1622 (void) cmpldev(&d32, vfsp->vfs_dev);
1623 statp->f_fsid = d32;
1626 * We're a zfs filesystem.
1628 (void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1630 statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1632 statp->f_namemax = ZFS_MAXNAMELEN;
1635 * We have all of 32 characters to stuff a string here.
1636 * Is there anything useful we could/should provide?
1638 bzero(statp->f_fstr, sizeof (statp->f_fstr));
1643 EXPORT_SYMBOL(zfs_statvfs);
1646 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1648 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1654 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1656 *vpp = ZTOV(rootzp);
1661 EXPORT_SYMBOL(zfs_root);
1664 * Teardown the zfsvfs::z_os.
1666 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1667 * and 'z_teardown_inactive_lock' held.
1670 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1674 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1678 * We purge the parent filesystem's vfsp as the parent
1679 * filesystem and all of its snapshots have their vnode's
1680 * v_vfsp set to the parent's filesystem's vfsp. Note,
1681 * 'z_parent' is self referential for non-snapshots.
1683 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1687 * Close the zil. NB: Can't close the zil while zfs_inactive
1688 * threads are blocked as zil_close can call zfs_inactive.
1690 if (zfsvfs->z_log) {
1691 zil_close(zfsvfs->z_log);
1692 zfsvfs->z_log = NULL;
1695 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1698 * If we are not unmounting (ie: online recv) and someone already
1699 * unmounted this file system while we were doing the switcheroo,
1700 * or a reopen of z_os failed then just bail out now.
1702 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1703 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1704 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1709 * At this point there are no vops active, and any new vops will
1710 * fail with EIO since we have z_teardown_lock for writer (only
1711 * relavent for forced unmount).
1713 * Release all holds on dbufs.
1715 mutex_enter(&zfsvfs->z_znodes_lock);
1716 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1717 zp = list_next(&zfsvfs->z_all_znodes, zp))
1719 ASSERT(ZTOV(zp)->v_count > 0);
1720 zfs_znode_dmu_fini(zp);
1722 mutex_exit(&zfsvfs->z_znodes_lock);
1725 * If we are unmounting, set the unmounted flag and let new vops
1726 * unblock. zfs_inactive will have the unmounted behavior, and all
1727 * other vops will fail with EIO.
1730 zfsvfs->z_unmounted = B_TRUE;
1731 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1732 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1736 * z_os will be NULL if there was an error in attempting to reopen
1737 * zfsvfs, so just return as the properties had already been
1738 * unregistered and cached data had been evicted before.
1740 if (zfsvfs->z_os == NULL)
1744 * Unregister properties.
1746 zfs_unregister_callbacks(zfsvfs);
1751 if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
1752 if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1753 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1754 (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1761 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1763 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1767 ret = secpolicy_fs_unmount(cr, vfsp);
1769 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1770 ZFS_DELEG_PERM_MOUNT, cr))
1775 * We purge the parent filesystem's vfsp as the parent filesystem
1776 * and all of its snapshots have their vnode's v_vfsp set to the
1777 * parent's filesystem's vfsp. Note, 'z_parent' is self
1778 * referential for non-snapshots.
1780 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1783 * Unmount any snapshots mounted under .zfs before unmounting the
1786 if (zfsvfs->z_ctldir != NULL &&
1787 (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1791 if (!(fflag & MS_FORCE)) {
1793 * Check the number of active vnodes in the file system.
1794 * Our count is maintained in the vfs structure, but the
1795 * number is off by 1 to indicate a hold on the vfs
1798 * The '.zfs' directory maintains a reference of its
1799 * own, and any active references underneath are
1800 * reflected in the vnode count.
1802 if (zfsvfs->z_ctldir == NULL) {
1803 if (vfsp->vfs_count > 1)
1806 if (vfsp->vfs_count > 2 ||
1807 zfsvfs->z_ctldir->v_count > 1)
1812 vfsp->vfs_flag |= VFS_UNMOUNTED;
1814 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1818 * z_os will be NULL if there was an error in
1819 * attempting to reopen zfsvfs.
1823 * Unset the objset user_ptr.
1825 mutex_enter(&os->os_user_ptr_lock);
1826 dmu_objset_set_user(os, NULL);
1827 mutex_exit(&os->os_user_ptr_lock);
1830 * Finally release the objset
1832 dmu_objset_disown(os, zfsvfs);
1836 * We can now safely destroy the '.zfs' directory node.
1838 if (zfsvfs->z_ctldir != NULL)
1839 zfsctl_destroy(zfsvfs);
1843 EXPORT_SYMBOL(zfs_umount);
1846 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1848 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1850 uint64_t object = 0;
1851 uint64_t fid_gen = 0;
1860 if (fidp->fid_len == LONG_FID_LEN) {
1861 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1862 uint64_t objsetid = 0;
1863 uint64_t setgen = 0;
1865 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1866 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1868 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1869 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1873 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1879 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1880 zfid_short_t *zfid = (zfid_short_t *)fidp;
1882 for (i = 0; i < sizeof (zfid->zf_object); i++)
1883 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1885 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1886 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1892 /* A zero fid_gen means we are in the .zfs control directories */
1894 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1895 *vpp = zfsvfs->z_ctldir;
1896 ASSERT(*vpp != NULL);
1897 if (object == ZFSCTL_INO_SNAPDIR) {
1898 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1899 0, NULL, NULL, NULL, NULL, NULL) == 0);
1907 gen_mask = -1ULL >> (64 - 8 * i);
1909 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1910 if ((err = zfs_zget(zfsvfs, object, &zp))) {
1914 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1916 zp_gen = zp_gen & gen_mask;
1919 if (zp->z_unlinked || zp_gen != fid_gen) {
1920 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1928 zfs_inode_update(VTOZ(*vpp));
1933 EXPORT_SYMBOL(zfs_vget);
1936 * Block out VOPs and close zfsvfs_t::z_os
1938 * Note, if successful, then we return with the 'z_teardown_lock' and
1939 * 'z_teardown_inactive_lock' write held.
1942 zfs_suspend_fs(zfsvfs_t *zfsvfs)
1946 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1948 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1952 EXPORT_SYMBOL(zfs_suspend_fs);
1955 * Reopen zfsvfs_t::z_os and release VOPs.
1958 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
1962 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1963 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1965 err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
1968 zfsvfs->z_os = NULL;
1971 uint64_t sa_obj = 0;
1973 err2 = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
1974 ZFS_SA_ATTRS, 8, 1, &sa_obj);
1976 if ((err || err2) && zfsvfs->z_version >= ZPL_VERSION_SA)
1980 if ((err = sa_setup(zfsvfs->z_os, sa_obj,
1981 zfs_attr_table, ZPL_END, &zfsvfs->z_attr_table)) != 0)
1984 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1987 * Attempt to re-establish all the active znodes with
1988 * their dbufs. If a zfs_rezget() fails, then we'll let
1989 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1990 * when they try to use their znode.
1992 mutex_enter(&zfsvfs->z_znodes_lock);
1993 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1994 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1995 (void) zfs_rezget(zp);
1997 mutex_exit(&zfsvfs->z_znodes_lock);
2002 /* release the VOPs */
2003 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2004 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
2008 * Since we couldn't reopen zfsvfs::z_os, force
2009 * unmount this file system.
2011 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2012 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2016 EXPORT_SYMBOL(zfs_resume_fs);
2019 zfs_freevfs(vfs_t *vfsp)
2021 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2024 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2025 * from zfs_mount(). Release it here. If we came through
2026 * zfs_mountroot() instead, we didn't grab an extra hold, so
2027 * skip the VFS_RELE for rootvfs.
2029 if (zfsvfs->z_issnap && (vfsp != rootvfs))
2030 VFS_RELE(zfsvfs->z_parent->z_vfs);
2032 zfsvfs_free(zfsvfs);
2034 atomic_add_32(&zfs_active_fs_count, -1);
2038 * VFS_INIT() initialization. Note that there is no VFS_FINI(),
2039 * so we can't safely do any non-idempotent initialization here.
2040 * Leave that to zfs_init() and zfs_fini(), which are called
2041 * from the module's _init() and _fini() entry points.
2045 zfs_vfsinit(int fstype, char *name)
2050 mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2053 * Unique major number for all zfs mounts.
2054 * If we run out of 32-bit minors, we'll getudev() another major.
2056 zfs_major = ddi_name_to_major(ZFS_DRIVER);
2057 zfs_minor = ZFS_MIN_MINOR;
2061 #endif /* HAVE_ZPL */
2068 * Initialize .zfs directory structures
2073 * Initialize znode cache, vnode ops, etc...
2076 #endif /* HAVE_ZPL */
2078 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2087 #endif /* HAVE_ZPL */
2092 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2095 objset_t *os = zfsvfs->z_os;
2098 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2101 if (newvers < zfsvfs->z_version)
2104 if (zfs_spa_version_map(newvers) >
2105 spa_version(dmu_objset_spa(zfsvfs->z_os)))
2108 tx = dmu_tx_create(os);
2109 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2110 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2111 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2113 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2115 error = dmu_tx_assign(tx, TXG_WAIT);
2121 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2122 8, 1, &newvers, tx);
2129 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2132 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2134 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2135 DMU_OT_NONE, 0, tx);
2137 error = zap_add(os, MASTER_NODE_OBJ,
2138 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2139 ASSERT3U(error, ==, 0);
2141 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2142 sa_register_update_callback(os, zfs_sa_upgrade);
2145 spa_history_log_internal(LOG_DS_UPGRADE,
2146 dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
2147 zfsvfs->z_version, newvers, dmu_objset_id(os));
2151 zfsvfs->z_version = newvers;
2153 if (zfsvfs->z_version >= ZPL_VERSION_FUID)
2154 zfs_set_fuid_feature(zfsvfs);
2158 EXPORT_SYMBOL(zfs_set_version);
2159 #endif /* HAVE_ZPL */
2162 * Read a property stored within the master node.
2165 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2171 * Look up the file system's value for the property. For the
2172 * version property, we look up a slightly different string.
2174 if (prop == ZFS_PROP_VERSION)
2175 pname = ZPL_VERSION_STR;
2177 pname = zfs_prop_to_name(prop);
2180 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2182 if (error == ENOENT) {
2183 /* No value set, use the default value */
2185 case ZFS_PROP_VERSION:
2186 *value = ZPL_VERSION;
2188 case ZFS_PROP_NORMALIZE:
2189 case ZFS_PROP_UTF8ONLY:
2193 *value = ZFS_CASE_SENSITIVE;