1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51484Sek110237 * Common Development and Distribution License (the "License"). 61484Sek110237 * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 229030SMark.Shellenbaum@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/types.h> 27789Sahrens #include <sys/param.h> 28789Sahrens #include <sys/systm.h> 29789Sahrens #include <sys/sysmacros.h> 30789Sahrens #include <sys/kmem.h> 31789Sahrens #include <sys/pathname.h> 32789Sahrens #include <sys/vnode.h> 33789Sahrens #include <sys/vfs.h> 343898Srsb #include <sys/vfs_opreg.h> 35789Sahrens #include <sys/mntent.h> 36789Sahrens #include <sys/mount.h> 37789Sahrens #include <sys/cmn_err.h> 38789Sahrens #include "fs/fs_subr.h" 39789Sahrens #include <sys/zfs_znode.h> 403461Sahrens #include <sys/zfs_dir.h> 41789Sahrens #include <sys/zil.h> 42789Sahrens #include <sys/fs/zfs.h> 43789Sahrens #include <sys/dmu.h> 44789Sahrens #include <sys/dsl_prop.h> 453912Slling #include <sys/dsl_dataset.h> 464543Smarks #include <sys/dsl_deleg.h> 47789Sahrens #include <sys/spa.h> 48789Sahrens #include <sys/zap.h> 49789Sahrens #include <sys/varargs.h> 50789Sahrens #include <sys/policy.h> 51789Sahrens #include <sys/atomic.h> 52789Sahrens #include <sys/mkdev.h> 53789Sahrens #include <sys/modctl.h> 544543Smarks #include <sys/refstr.h> 55789Sahrens #include <sys/zfs_ioctl.h> 56789Sahrens #include <sys/zfs_ctldir.h> 575331Samw #include <sys/zfs_fuid.h> 581544Seschrock #include <sys/bootconf.h> 59849Sbonwick #include <sys/sunddi.h> 601484Sek110237 #include <sys/dnlc.h> 615326Sek110237 #include <sys/dmu_objset.h> 626423Sgw25295 #include <sys/spa_boot.h> 63789Sahrens 64789Sahrens int zfsfstype; 65789Sahrens vfsops_t *zfs_vfsops = NULL; 66849Sbonwick static major_t zfs_major; 67789Sahrens static minor_t zfs_minor; 68789Sahrens static kmutex_t zfs_dev_mtx; 69789Sahrens 709234SGeorge.Wilson@Sun.COM extern int sys_shutdown; 719234SGeorge.Wilson@Sun.COM 72789Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr); 73789Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr); 741544Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot); 75789Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp); 76789Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp); 77789Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp); 78789Sahrens static void zfs_freevfs(vfs_t *vfsp); 79789Sahrens 80789Sahrens static const fs_operation_def_t zfs_vfsops_template[] = { 813898Srsb VFSNAME_MOUNT, { .vfs_mount = zfs_mount }, 823898Srsb VFSNAME_MOUNTROOT, { .vfs_mountroot = zfs_mountroot }, 833898Srsb VFSNAME_UNMOUNT, { .vfs_unmount = zfs_umount }, 843898Srsb VFSNAME_ROOT, { .vfs_root = zfs_root }, 853898Srsb VFSNAME_STATVFS, { .vfs_statvfs = zfs_statvfs }, 863898Srsb VFSNAME_SYNC, { .vfs_sync = zfs_sync }, 873898Srsb VFSNAME_VGET, { .vfs_vget = zfs_vget }, 883898Srsb VFSNAME_FREEVFS, { .vfs_freevfs = zfs_freevfs }, 893898Srsb NULL, NULL 90789Sahrens }; 91789Sahrens 92789Sahrens static const fs_operation_def_t zfs_vfsops_eio_template[] = { 933898Srsb VFSNAME_FREEVFS, { .vfs_freevfs = zfs_freevfs }, 943898Srsb NULL, NULL 95789Sahrens }; 96789Sahrens 97789Sahrens /* 98789Sahrens * We need to keep a count of active fs's. 99789Sahrens * This is necessary to prevent our module 100789Sahrens * from being unloaded after a umount -f 101789Sahrens */ 102789Sahrens static uint32_t zfs_active_fs_count = 0; 103789Sahrens 104789Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL }; 105789Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL }; 1063234Sck153898 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL }; 1073234Sck153898 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL }; 108789Sahrens 1093234Sck153898 /* 1104596Slling * MO_DEFAULT is not used since the default value is determined 1114596Slling * by the equivalent property. 1123234Sck153898 */ 113789Sahrens static mntopt_t mntopts[] = { 1143234Sck153898 { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL }, 1153234Sck153898 { MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL }, 1164596Slling { MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL }, 117789Sahrens { MNTOPT_ATIME, atime_cancel, NULL, 0, NULL } 118789Sahrens }; 119789Sahrens 120789Sahrens static mntopts_t zfs_mntopts = { 121789Sahrens sizeof (mntopts) / sizeof (mntopt_t), 122789Sahrens mntopts 123789Sahrens }; 124789Sahrens 125789Sahrens /*ARGSUSED*/ 126789Sahrens int 127789Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr) 128789Sahrens { 129789Sahrens /* 130789Sahrens * Data integrity is job one. We don't want a compromised kernel 131789Sahrens * writing to the storage pool, so we never sync during panic. 132789Sahrens */ 133789Sahrens if (panicstr) 134789Sahrens return (0); 135789Sahrens 136789Sahrens /* 137789Sahrens * SYNC_ATTR is used by fsflush() to force old filesystems like UFS 138789Sahrens * to sync metadata, which they would otherwise cache indefinitely. 139789Sahrens * Semantically, the only requirement is that the sync be initiated. 140789Sahrens * The DMU syncs out txgs frequently, so there's nothing to do. 141789Sahrens */ 142789Sahrens if (flag & SYNC_ATTR) 143789Sahrens return (0); 144789Sahrens 145789Sahrens if (vfsp != NULL) { 146789Sahrens /* 147789Sahrens * Sync a specific filesystem. 148789Sahrens */ 149789Sahrens zfsvfs_t *zfsvfs = vfsp->vfs_data; 1509234SGeorge.Wilson@Sun.COM dsl_pool_t *dp; 151789Sahrens 152789Sahrens ZFS_ENTER(zfsvfs); 1539234SGeorge.Wilson@Sun.COM dp = dmu_objset_pool(zfsvfs->z_os); 1549234SGeorge.Wilson@Sun.COM 1559234SGeorge.Wilson@Sun.COM /* 1569234SGeorge.Wilson@Sun.COM * If the system is shutting down, then skip any 1579234SGeorge.Wilson@Sun.COM * filesystems which may exist on a suspended pool. 1589234SGeorge.Wilson@Sun.COM */ 1599234SGeorge.Wilson@Sun.COM if (sys_shutdown && spa_suspended(dp->dp_spa)) { 1609234SGeorge.Wilson@Sun.COM ZFS_EXIT(zfsvfs); 1619234SGeorge.Wilson@Sun.COM return (0); 1629234SGeorge.Wilson@Sun.COM } 1639234SGeorge.Wilson@Sun.COM 164789Sahrens if (zfsvfs->z_log != NULL) 1652638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, 0); 166789Sahrens else 1679234SGeorge.Wilson@Sun.COM txg_wait_synced(dp, 0); 168789Sahrens ZFS_EXIT(zfsvfs); 169789Sahrens } else { 170789Sahrens /* 171789Sahrens * Sync all ZFS filesystems. This is what happens when you 172789Sahrens * run sync(1M). Unlike other filesystems, ZFS honors the 173789Sahrens * request by waiting for all pools to commit all dirty data. 174789Sahrens */ 175789Sahrens spa_sync_allpools(); 176789Sahrens } 177789Sahrens 178789Sahrens return (0); 179789Sahrens } 180789Sahrens 1811544Seschrock static int 1821544Seschrock zfs_create_unique_device(dev_t *dev) 1831544Seschrock { 1841544Seschrock major_t new_major; 1851544Seschrock 1861544Seschrock do { 1871544Seschrock ASSERT3U(zfs_minor, <=, MAXMIN32); 1881544Seschrock minor_t start = zfs_minor; 1891544Seschrock do { 1901544Seschrock mutex_enter(&zfs_dev_mtx); 1911544Seschrock if (zfs_minor >= MAXMIN32) { 1921544Seschrock /* 1931544Seschrock * If we're still using the real major 1941544Seschrock * keep out of /dev/zfs and /dev/zvol minor 1951544Seschrock * number space. If we're using a getudev()'ed 1961544Seschrock * major number, we can use all of its minors. 1971544Seschrock */ 1981544Seschrock if (zfs_major == ddi_name_to_major(ZFS_DRIVER)) 1991544Seschrock zfs_minor = ZFS_MIN_MINOR; 2001544Seschrock else 2011544Seschrock zfs_minor = 0; 2021544Seschrock } else { 2031544Seschrock zfs_minor++; 2041544Seschrock } 2051544Seschrock *dev = makedevice(zfs_major, zfs_minor); 2061544Seschrock mutex_exit(&zfs_dev_mtx); 2071544Seschrock } while (vfs_devismounted(*dev) && zfs_minor != start); 2081544Seschrock if (zfs_minor == start) { 2091544Seschrock /* 2101544Seschrock * We are using all ~262,000 minor numbers for the 2111544Seschrock * current major number. Create a new major number. 2121544Seschrock */ 2131544Seschrock if ((new_major = getudev()) == (major_t)-1) { 2141544Seschrock cmn_err(CE_WARN, 2151544Seschrock "zfs_mount: Can't get unique major " 2161544Seschrock "device number."); 2171544Seschrock return (-1); 2181544Seschrock } 2191544Seschrock mutex_enter(&zfs_dev_mtx); 2201544Seschrock zfs_major = new_major; 2211544Seschrock zfs_minor = 0; 2221544Seschrock 2231544Seschrock mutex_exit(&zfs_dev_mtx); 2241544Seschrock } else { 2251544Seschrock break; 2261544Seschrock } 2271544Seschrock /* CONSTANTCONDITION */ 2281544Seschrock } while (1); 2291544Seschrock 2301544Seschrock return (0); 2311544Seschrock } 2321544Seschrock 233789Sahrens static void 234789Sahrens atime_changed_cb(void *arg, uint64_t newval) 235789Sahrens { 236789Sahrens zfsvfs_t *zfsvfs = arg; 237789Sahrens 238789Sahrens if (newval == TRUE) { 239789Sahrens zfsvfs->z_atime = TRUE; 240789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME); 241789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0); 242789Sahrens } else { 243789Sahrens zfsvfs->z_atime = FALSE; 244789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME); 245789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0); 246789Sahrens } 247789Sahrens } 248789Sahrens 249789Sahrens static void 2503234Sck153898 xattr_changed_cb(void *arg, uint64_t newval) 2513234Sck153898 { 2523234Sck153898 zfsvfs_t *zfsvfs = arg; 2533234Sck153898 2543234Sck153898 if (newval == TRUE) { 2553234Sck153898 /* XXX locking on vfs_flag? */ 2563234Sck153898 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR; 2573234Sck153898 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR); 2583234Sck153898 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0); 2593234Sck153898 } else { 2603234Sck153898 /* XXX locking on vfs_flag? */ 2613234Sck153898 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR; 2623234Sck153898 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR); 2633234Sck153898 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0); 2643234Sck153898 } 2653234Sck153898 } 2663234Sck153898 2673234Sck153898 static void 268789Sahrens blksz_changed_cb(void *arg, uint64_t newval) 269789Sahrens { 270789Sahrens zfsvfs_t *zfsvfs = arg; 271789Sahrens 272789Sahrens if (newval < SPA_MINBLOCKSIZE || 273789Sahrens newval > SPA_MAXBLOCKSIZE || !ISP2(newval)) 274789Sahrens newval = SPA_MAXBLOCKSIZE; 275789Sahrens 276789Sahrens zfsvfs->z_max_blksz = newval; 277789Sahrens zfsvfs->z_vfs->vfs_bsize = newval; 278789Sahrens } 279789Sahrens 280789Sahrens static void 281789Sahrens readonly_changed_cb(void *arg, uint64_t newval) 282789Sahrens { 283789Sahrens zfsvfs_t *zfsvfs = arg; 284789Sahrens 285789Sahrens if (newval) { 286789Sahrens /* XXX locking on vfs_flag? */ 287789Sahrens zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY; 288789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW); 289789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0); 290789Sahrens } else { 291789Sahrens /* XXX locking on vfs_flag? */ 292789Sahrens zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 293789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO); 294789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0); 295789Sahrens } 296789Sahrens } 297789Sahrens 298789Sahrens static void 299789Sahrens devices_changed_cb(void *arg, uint64_t newval) 300789Sahrens { 301789Sahrens zfsvfs_t *zfsvfs = arg; 302789Sahrens 303789Sahrens if (newval == FALSE) { 304789Sahrens zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES; 305789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES); 306789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0); 307789Sahrens } else { 308789Sahrens zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES; 309789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES); 310789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0); 311789Sahrens } 312789Sahrens } 313789Sahrens 314789Sahrens static void 315789Sahrens setuid_changed_cb(void *arg, uint64_t newval) 316789Sahrens { 317789Sahrens zfsvfs_t *zfsvfs = arg; 318789Sahrens 319789Sahrens if (newval == FALSE) { 320789Sahrens zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID; 321789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID); 322789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0); 323789Sahrens } else { 324789Sahrens zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID; 325789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID); 326789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0); 327789Sahrens } 328789Sahrens } 329789Sahrens 330789Sahrens static void 331789Sahrens exec_changed_cb(void *arg, uint64_t newval) 332789Sahrens { 333789Sahrens zfsvfs_t *zfsvfs = arg; 334789Sahrens 335789Sahrens if (newval == FALSE) { 336789Sahrens zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC; 337789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC); 338789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0); 339789Sahrens } else { 340789Sahrens zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC; 341789Sahrens vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC); 342789Sahrens vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0); 343789Sahrens } 344789Sahrens } 345789Sahrens 3465331Samw /* 3475331Samw * The nbmand mount option can be changed at mount time. 3485331Samw * We can't allow it to be toggled on live file systems or incorrect 3495331Samw * behavior may be seen from cifs clients 3505331Samw * 3515331Samw * This property isn't registered via dsl_prop_register(), but this callback 3525331Samw * will be called when a file system is first mounted 3535331Samw */ 3545331Samw static void 3555331Samw nbmand_changed_cb(void *arg, uint64_t newval) 3565331Samw { 3575331Samw zfsvfs_t *zfsvfs = arg; 3585331Samw if (newval == FALSE) { 3595331Samw vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND); 3605331Samw vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0); 3615331Samw } else { 3625331Samw vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND); 3635331Samw vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0); 3645331Samw } 3655331Samw } 3665331Samw 367789Sahrens static void 368789Sahrens snapdir_changed_cb(void *arg, uint64_t newval) 369789Sahrens { 370789Sahrens zfsvfs_t *zfsvfs = arg; 371789Sahrens 372789Sahrens zfsvfs->z_show_ctldir = newval; 373789Sahrens } 374789Sahrens 375789Sahrens static void 3765331Samw vscan_changed_cb(void *arg, uint64_t newval) 3775331Samw { 3785331Samw zfsvfs_t *zfsvfs = arg; 3795331Samw 3805331Samw zfsvfs->z_vscan = newval; 3815331Samw } 3825331Samw 3835331Samw static void 384789Sahrens acl_mode_changed_cb(void *arg, uint64_t newval) 385789Sahrens { 386789Sahrens zfsvfs_t *zfsvfs = arg; 387789Sahrens 388789Sahrens zfsvfs->z_acl_mode = newval; 389789Sahrens } 390789Sahrens 391789Sahrens static void 392789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval) 393789Sahrens { 394789Sahrens zfsvfs_t *zfsvfs = arg; 395789Sahrens 396789Sahrens zfsvfs->z_acl_inherit = newval; 397789Sahrens } 398789Sahrens 3991544Seschrock static int 4001544Seschrock zfs_register_callbacks(vfs_t *vfsp) 4011544Seschrock { 4021544Seschrock struct dsl_dataset *ds = NULL; 4031544Seschrock objset_t *os = NULL; 4041544Seschrock zfsvfs_t *zfsvfs = NULL; 4055331Samw uint64_t nbmand; 4065331Samw int readonly, do_readonly = B_FALSE; 4075331Samw int setuid, do_setuid = B_FALSE; 4085331Samw int exec, do_exec = B_FALSE; 4095331Samw int devices, do_devices = B_FALSE; 4105331Samw int xattr, do_xattr = B_FALSE; 4115331Samw int atime, do_atime = B_FALSE; 4121544Seschrock int error = 0; 4131544Seschrock 4141544Seschrock ASSERT(vfsp); 4151544Seschrock zfsvfs = vfsp->vfs_data; 4161544Seschrock ASSERT(zfsvfs); 4171544Seschrock os = zfsvfs->z_os; 4181544Seschrock 4191544Seschrock /* 4201544Seschrock * The act of registering our callbacks will destroy any mount 4211544Seschrock * options we may have. In order to enable temporary overrides 4223234Sck153898 * of mount options, we stash away the current values and 4231544Seschrock * restore them after we register the callbacks. 4241544Seschrock */ 4251544Seschrock if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) { 4261544Seschrock readonly = B_TRUE; 4271544Seschrock do_readonly = B_TRUE; 4281544Seschrock } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) { 4291544Seschrock readonly = B_FALSE; 4301544Seschrock do_readonly = B_TRUE; 4311544Seschrock } 4321544Seschrock if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) { 4331544Seschrock devices = B_FALSE; 4341544Seschrock setuid = B_FALSE; 4351544Seschrock do_devices = B_TRUE; 4361544Seschrock do_setuid = B_TRUE; 4371544Seschrock } else { 4381544Seschrock if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) { 4391544Seschrock devices = B_FALSE; 4401544Seschrock do_devices = B_TRUE; 4413912Slling } else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) { 4421544Seschrock devices = B_TRUE; 4431544Seschrock do_devices = B_TRUE; 4441544Seschrock } 4451544Seschrock 4461544Seschrock if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) { 4471544Seschrock setuid = B_FALSE; 4481544Seschrock do_setuid = B_TRUE; 4491544Seschrock } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) { 4501544Seschrock setuid = B_TRUE; 4511544Seschrock do_setuid = B_TRUE; 4521544Seschrock } 4531544Seschrock } 4541544Seschrock if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) { 4551544Seschrock exec = B_FALSE; 4561544Seschrock do_exec = B_TRUE; 4571544Seschrock } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) { 4581544Seschrock exec = B_TRUE; 4591544Seschrock do_exec = B_TRUE; 4601544Seschrock } 4613234Sck153898 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) { 4623234Sck153898 xattr = B_FALSE; 4633234Sck153898 do_xattr = B_TRUE; 4643234Sck153898 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) { 4653234Sck153898 xattr = B_TRUE; 4663234Sck153898 do_xattr = B_TRUE; 4673234Sck153898 } 4684596Slling if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) { 4694596Slling atime = B_FALSE; 4704596Slling do_atime = B_TRUE; 4714596Slling } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) { 4724596Slling atime = B_TRUE; 4734596Slling do_atime = B_TRUE; 4744596Slling } 4751544Seschrock 4761544Seschrock /* 4775331Samw * nbmand is a special property. It can only be changed at 4785331Samw * mount time. 4795331Samw * 4805331Samw * This is weird, but it is documented to only be changeable 4815331Samw * at mount time. 4825331Samw */ 4835331Samw if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) { 4845331Samw nbmand = B_FALSE; 4855331Samw } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) { 4865331Samw nbmand = B_TRUE; 4875331Samw } else { 4885331Samw char osname[MAXNAMELEN]; 4895331Samw 4905331Samw dmu_objset_name(os, osname); 4915331Samw if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand, 4927265Sahrens NULL)) { 4937265Sahrens return (error); 4947265Sahrens } 4955331Samw } 4965331Samw 4975331Samw /* 4981544Seschrock * Register property callbacks. 4991544Seschrock * 5001544Seschrock * It would probably be fine to just check for i/o error from 5011544Seschrock * the first prop_register(), but I guess I like to go 5021544Seschrock * overboard... 5031544Seschrock */ 5041544Seschrock ds = dmu_objset_ds(os); 5051544Seschrock error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs); 5061544Seschrock error = error ? error : dsl_prop_register(ds, 5073234Sck153898 "xattr", xattr_changed_cb, zfsvfs); 5083234Sck153898 error = error ? error : dsl_prop_register(ds, 5091544Seschrock "recordsize", blksz_changed_cb, zfsvfs); 5101544Seschrock error = error ? error : dsl_prop_register(ds, 5111544Seschrock "readonly", readonly_changed_cb, zfsvfs); 5121544Seschrock error = error ? error : dsl_prop_register(ds, 5131544Seschrock "devices", devices_changed_cb, zfsvfs); 5141544Seschrock error = error ? error : dsl_prop_register(ds, 5151544Seschrock "setuid", setuid_changed_cb, zfsvfs); 5161544Seschrock error = error ? error : dsl_prop_register(ds, 5171544Seschrock "exec", exec_changed_cb, zfsvfs); 5181544Seschrock error = error ? error : dsl_prop_register(ds, 5191544Seschrock "snapdir", snapdir_changed_cb, zfsvfs); 5201544Seschrock error = error ? error : dsl_prop_register(ds, 5211544Seschrock "aclmode", acl_mode_changed_cb, zfsvfs); 5221544Seschrock error = error ? error : dsl_prop_register(ds, 5231544Seschrock "aclinherit", acl_inherit_changed_cb, zfsvfs); 5245331Samw error = error ? error : dsl_prop_register(ds, 5255331Samw "vscan", vscan_changed_cb, zfsvfs); 5261544Seschrock if (error) 5271544Seschrock goto unregister; 5281544Seschrock 5291544Seschrock /* 5301544Seschrock * Invoke our callbacks to restore temporary mount options. 5311544Seschrock */ 5321544Seschrock if (do_readonly) 5331544Seschrock readonly_changed_cb(zfsvfs, readonly); 5341544Seschrock if (do_setuid) 5351544Seschrock setuid_changed_cb(zfsvfs, setuid); 5361544Seschrock if (do_exec) 5371544Seschrock exec_changed_cb(zfsvfs, exec); 5381544Seschrock if (do_devices) 5391544Seschrock devices_changed_cb(zfsvfs, devices); 5403234Sck153898 if (do_xattr) 5413234Sck153898 xattr_changed_cb(zfsvfs, xattr); 5424596Slling if (do_atime) 5434596Slling atime_changed_cb(zfsvfs, atime); 5441544Seschrock 5455331Samw nbmand_changed_cb(zfsvfs, nbmand); 5465331Samw 5471544Seschrock return (0); 5481544Seschrock 5491544Seschrock unregister: 5501544Seschrock /* 5511544Seschrock * We may attempt to unregister some callbacks that are not 5521544Seschrock * registered, but this is OK; it will simply return ENOMSG, 5531544Seschrock * which we will ignore. 5541544Seschrock */ 5551544Seschrock (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs); 5563234Sck153898 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs); 5571544Seschrock (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs); 5581544Seschrock (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs); 5591544Seschrock (void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs); 5601544Seschrock (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs); 5611544Seschrock (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs); 5621544Seschrock (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs); 5631544Seschrock (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs); 5641544Seschrock (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb, 5651544Seschrock zfsvfs); 5665331Samw (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs); 5671544Seschrock return (error); 5681544Seschrock 5691544Seschrock } 5701544Seschrock 5711544Seschrock static int 5725326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting) 5735326Sek110237 { 5745326Sek110237 int error; 5755326Sek110237 5765326Sek110237 error = zfs_register_callbacks(zfsvfs->z_vfs); 5775326Sek110237 if (error) 5785326Sek110237 return (error); 5795326Sek110237 5805326Sek110237 /* 5815326Sek110237 * Set the objset user_ptr to track its zfsvfs. 5825326Sek110237 */ 5835326Sek110237 mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock); 5845326Sek110237 dmu_objset_set_user(zfsvfs->z_os, zfsvfs); 5855326Sek110237 mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock); 5865326Sek110237 587*9292SNeil.Perrin@Sun.COM zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data); 588*9292SNeil.Perrin@Sun.COM if (zil_disable) { 589*9292SNeil.Perrin@Sun.COM zil_destroy(zfsvfs->z_log, 0); 590*9292SNeil.Perrin@Sun.COM zfsvfs->z_log = NULL; 591*9292SNeil.Perrin@Sun.COM } 592*9292SNeil.Perrin@Sun.COM 5935326Sek110237 /* 5945326Sek110237 * If we are not mounting (ie: online recv), then we don't 5955326Sek110237 * have to worry about replaying the log as we blocked all 5965326Sek110237 * operations out since we closed the ZIL. 5975326Sek110237 */ 5985326Sek110237 if (mounting) { 5997638SNeil.Perrin@Sun.COM boolean_t readonly; 6007638SNeil.Perrin@Sun.COM 6015326Sek110237 /* 6025326Sek110237 * During replay we remove the read only flag to 6035326Sek110237 * allow replays to succeed. 6045326Sek110237 */ 6055326Sek110237 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY; 6068227SNeil.Perrin@Sun.COM if (readonly != 0) 6078227SNeil.Perrin@Sun.COM zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 6088227SNeil.Perrin@Sun.COM else 6098227SNeil.Perrin@Sun.COM zfs_unlinked_drain(zfsvfs); 6105326Sek110237 611*9292SNeil.Perrin@Sun.COM if (zfsvfs->z_log) { 6128227SNeil.Perrin@Sun.COM /* 6138227SNeil.Perrin@Sun.COM * Parse and replay the intent log. 6148227SNeil.Perrin@Sun.COM * 6158227SNeil.Perrin@Sun.COM * Because of ziltest, this must be done after 6168227SNeil.Perrin@Sun.COM * zfs_unlinked_drain(). (Further note: ziltest 6178227SNeil.Perrin@Sun.COM * doesn't use readonly mounts, where 6188227SNeil.Perrin@Sun.COM * zfs_unlinked_drain() isn't called.) This is because 6198227SNeil.Perrin@Sun.COM * ziltest causes spa_sync() to think it's committed, 6208227SNeil.Perrin@Sun.COM * but actually it is not, so the intent log contains 6218227SNeil.Perrin@Sun.COM * many txg's worth of changes. 6228227SNeil.Perrin@Sun.COM * 6238227SNeil.Perrin@Sun.COM * In particular, if object N is in the unlinked set in 6248227SNeil.Perrin@Sun.COM * the last txg to actually sync, then it could be 6258227SNeil.Perrin@Sun.COM * actually freed in a later txg and then reallocated 6268227SNeil.Perrin@Sun.COM * in a yet later txg. This would write a "create 6278227SNeil.Perrin@Sun.COM * object N" record to the intent log. Normally, this 6288227SNeil.Perrin@Sun.COM * would be fine because the spa_sync() would have 6298227SNeil.Perrin@Sun.COM * written out the fact that object N is free, before 6308227SNeil.Perrin@Sun.COM * we could write the "create object N" intent log 6318227SNeil.Perrin@Sun.COM * record. 6328227SNeil.Perrin@Sun.COM * 6338227SNeil.Perrin@Sun.COM * But when we are in ziltest mode, we advance the "open 6348227SNeil.Perrin@Sun.COM * txg" without actually spa_sync()-ing the changes to 6358227SNeil.Perrin@Sun.COM * disk. So we would see that object N is still 6368227SNeil.Perrin@Sun.COM * allocated and in the unlinked set, and there is an 6378227SNeil.Perrin@Sun.COM * intent log record saying to allocate it. 6388227SNeil.Perrin@Sun.COM */ 6398227SNeil.Perrin@Sun.COM zfsvfs->z_replay = B_TRUE; 6408227SNeil.Perrin@Sun.COM zil_replay(zfsvfs->z_os, zfsvfs, zfs_replay_vector); 6418227SNeil.Perrin@Sun.COM zfsvfs->z_replay = B_FALSE; 6428227SNeil.Perrin@Sun.COM } 6435326Sek110237 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */ 6445326Sek110237 } 6455326Sek110237 6465326Sek110237 return (0); 6475326Sek110237 } 6485326Sek110237 6496083Sek110237 static void 6506083Sek110237 zfs_freezfsvfs(zfsvfs_t *zfsvfs) 6516083Sek110237 { 6526083Sek110237 mutex_destroy(&zfsvfs->z_znodes_lock); 6536083Sek110237 mutex_destroy(&zfsvfs->z_online_recv_lock); 6549030SMark.Shellenbaum@Sun.COM mutex_destroy(&zfsvfs->z_lock); 6556083Sek110237 list_destroy(&zfsvfs->z_all_znodes); 6566083Sek110237 rrw_destroy(&zfsvfs->z_teardown_lock); 6576083Sek110237 rw_destroy(&zfsvfs->z_teardown_inactive_lock); 6586083Sek110237 rw_destroy(&zfsvfs->z_fuid_lock); 6596083Sek110237 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 6606083Sek110237 } 6616083Sek110237 6625326Sek110237 static int 6637046Sahrens zfs_domount(vfs_t *vfsp, char *osname) 6641544Seschrock { 6651544Seschrock dev_t mount_dev; 6661544Seschrock uint64_t recordsize, readonly; 6671544Seschrock int error = 0; 6681544Seschrock int mode; 6691544Seschrock zfsvfs_t *zfsvfs; 6701544Seschrock znode_t *zp = NULL; 6711544Seschrock 6721544Seschrock ASSERT(vfsp); 6731544Seschrock ASSERT(osname); 6741544Seschrock 6751544Seschrock /* 6761544Seschrock * Initialize the zfs-specific filesystem structure. 6771544Seschrock * Should probably make this a kmem cache, shuffle fields, 6781544Seschrock * and just bzero up to z_hold_mtx[]. 6791544Seschrock */ 6801544Seschrock zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 6811544Seschrock zfsvfs->z_vfs = vfsp; 6821544Seschrock zfsvfs->z_parent = zfsvfs; 6831544Seschrock zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE; 6841544Seschrock zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE; 6859179SMark.Shellenbaum@Sun.COM zfsvfs->z_fuid_dirty = B_FALSE; 6861544Seschrock 6871544Seschrock mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 6886083Sek110237 mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL); 6899030SMark.Shellenbaum@Sun.COM mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL); 6901544Seschrock list_create(&zfsvfs->z_all_znodes, sizeof (znode_t), 6911544Seschrock offsetof(znode_t, z_link_node)); 6925326Sek110237 rrw_init(&zfsvfs->z_teardown_lock); 6935326Sek110237 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL); 6945498Stimh rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL); 6951544Seschrock 6961544Seschrock /* Initialize the generic filesystem structure. */ 6971544Seschrock vfsp->vfs_bcount = 0; 6981544Seschrock vfsp->vfs_data = NULL; 6991544Seschrock 7001544Seschrock if (zfs_create_unique_device(&mount_dev) == -1) { 7011544Seschrock error = ENODEV; 7021544Seschrock goto out; 7031544Seschrock } 7041544Seschrock ASSERT(vfs_devismounted(mount_dev) == 0); 7051544Seschrock 7061544Seschrock if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize, 7071544Seschrock NULL)) 7081544Seschrock goto out; 7091544Seschrock 7101544Seschrock vfsp->vfs_dev = mount_dev; 7111544Seschrock vfsp->vfs_fstype = zfsfstype; 7121544Seschrock vfsp->vfs_bsize = recordsize; 7131544Seschrock vfsp->vfs_flag |= VFS_NOTRUNC; 7141544Seschrock vfsp->vfs_data = zfsvfs; 7151544Seschrock 7161544Seschrock if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL)) 7171544Seschrock goto out; 7181544Seschrock 7196689Smaybee mode = DS_MODE_OWNER; 7201544Seschrock if (readonly) 7216689Smaybee mode |= DS_MODE_READONLY; 7221544Seschrock 7231544Seschrock error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os); 7241544Seschrock if (error == EROFS) { 7256689Smaybee mode = DS_MODE_OWNER | DS_MODE_READONLY; 7261544Seschrock error = dmu_objset_open(osname, DMU_OST_ZFS, mode, 7271544Seschrock &zfsvfs->z_os); 7281544Seschrock } 7291544Seschrock 7301544Seschrock if (error) 7311544Seschrock goto out; 7321544Seschrock 7337046Sahrens if (error = zfs_init_fs(zfsvfs, &zp)) 7341544Seschrock goto out; 7351544Seschrock 7361544Seschrock /* The call to zfs_init_fs leaves the vnode held, release it here. */ 7371544Seschrock VN_RELE(ZTOV(zp)); 7381544Seschrock 7395331Samw /* 7405331Samw * Set features for file system. 7415331Samw */ 7425331Samw zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os); 7435331Samw if (zfsvfs->z_use_fuids) { 7445331Samw vfs_set_feature(vfsp, VFSFT_XVATTR); 7457757SJanice.Chang@Sun.COM vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS); 7465331Samw vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS); 7475331Samw vfs_set_feature(vfsp, VFSFT_ACLONCREATE); 7485331Samw } 7495498Stimh if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 7505498Stimh vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS); 7515498Stimh vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE); 7525498Stimh vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE); 7535498Stimh } else if (zfsvfs->z_case == ZFS_CASE_MIXED) { 7545498Stimh vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS); 7555498Stimh vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE); 7565498Stimh } 7575331Samw 7581544Seschrock if (dmu_objset_is_snapshot(zfsvfs->z_os)) { 7595331Samw uint64_t pval; 7603234Sck153898 7611544Seschrock ASSERT(mode & DS_MODE_READONLY); 7621544Seschrock atime_changed_cb(zfsvfs, B_FALSE); 7631544Seschrock readonly_changed_cb(zfsvfs, B_TRUE); 7645331Samw if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL)) 7653234Sck153898 goto out; 7665331Samw xattr_changed_cb(zfsvfs, pval); 7671544Seschrock zfsvfs->z_issnap = B_TRUE; 7681544Seschrock } else { 7695326Sek110237 error = zfsvfs_setup(zfsvfs, B_TRUE); 7701544Seschrock } 7711544Seschrock 7721544Seschrock if (!zfsvfs->z_issnap) 7731544Seschrock zfsctl_create(zfsvfs); 7741544Seschrock out: 7751544Seschrock if (error) { 7761544Seschrock if (zfsvfs->z_os) 7771544Seschrock dmu_objset_close(zfsvfs->z_os); 7786083Sek110237 zfs_freezfsvfs(zfsvfs); 7791544Seschrock } else { 7801544Seschrock atomic_add_32(&zfs_active_fs_count, 1); 7811544Seschrock } 7821544Seschrock 7831544Seschrock return (error); 7841544Seschrock } 7851544Seschrock 7861544Seschrock void 7871544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs) 7881544Seschrock { 7891544Seschrock objset_t *os = zfsvfs->z_os; 7901544Seschrock struct dsl_dataset *ds; 7911544Seschrock 7921544Seschrock /* 7931544Seschrock * Unregister properties. 7941544Seschrock */ 7951544Seschrock if (!dmu_objset_is_snapshot(os)) { 7961544Seschrock ds = dmu_objset_ds(os); 7971544Seschrock VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb, 7981544Seschrock zfsvfs) == 0); 7991544Seschrock 8003234Sck153898 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb, 8013234Sck153898 zfsvfs) == 0); 8023234Sck153898 8031544Seschrock VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, 8041544Seschrock zfsvfs) == 0); 8051544Seschrock 8061544Seschrock VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb, 8071544Seschrock zfsvfs) == 0); 8081544Seschrock 8091544Seschrock VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb, 8101544Seschrock zfsvfs) == 0); 8111544Seschrock 8121544Seschrock VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb, 8131544Seschrock zfsvfs) == 0); 8141544Seschrock 8151544Seschrock VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb, 8161544Seschrock zfsvfs) == 0); 8171544Seschrock 8181544Seschrock VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, 8191544Seschrock zfsvfs) == 0); 8201544Seschrock 8211544Seschrock VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, 8221544Seschrock zfsvfs) == 0); 8231544Seschrock 8241544Seschrock VERIFY(dsl_prop_unregister(ds, "aclinherit", 8251544Seschrock acl_inherit_changed_cb, zfsvfs) == 0); 8265331Samw 8275331Samw VERIFY(dsl_prop_unregister(ds, "vscan", 8285331Samw vscan_changed_cb, zfsvfs) == 0); 8291544Seschrock } 8301544Seschrock } 8311544Seschrock 8323912Slling /* 8333912Slling * Convert a decimal digit string to a uint64_t integer. 8343912Slling */ 8353912Slling static int 8363912Slling str_to_uint64(char *str, uint64_t *objnum) 8373912Slling { 8383912Slling uint64_t num = 0; 8393912Slling 8403912Slling while (*str) { 8413912Slling if (*str < '0' || *str > '9') 8423912Slling return (EINVAL); 8433912Slling 8443912Slling num = num*10 + *str++ - '0'; 8453912Slling } 8463912Slling 8473912Slling *objnum = num; 8483912Slling return (0); 8493912Slling } 8503912Slling 8513912Slling /* 8523912Slling * The boot path passed from the boot loader is in the form of 8533912Slling * "rootpool-name/root-filesystem-object-number'. Convert this 8543912Slling * string to a dataset name: "rootpool-name/root-filesystem-name". 8553912Slling */ 8563912Slling static int 8576423Sgw25295 zfs_parse_bootfs(char *bpath, char *outpath) 8583912Slling { 8593912Slling char *slashp; 8603912Slling uint64_t objnum; 8613912Slling int error; 8623912Slling 8633912Slling if (*bpath == 0 || *bpath == '/') 8643912Slling return (EINVAL); 8653912Slling 8667656SSherry.Moore@Sun.COM (void) strcpy(outpath, bpath); 8677656SSherry.Moore@Sun.COM 8683912Slling slashp = strchr(bpath, '/'); 8693912Slling 8703912Slling /* if no '/', just return the pool name */ 8713912Slling if (slashp == NULL) { 8723912Slling return (0); 8733912Slling } 8743912Slling 8757656SSherry.Moore@Sun.COM /* if not a number, just return the root dataset name */ 8767656SSherry.Moore@Sun.COM if (str_to_uint64(slashp+1, &objnum)) { 8777656SSherry.Moore@Sun.COM return (0); 8787656SSherry.Moore@Sun.COM } 8793912Slling 8803912Slling *slashp = '\0'; 8813912Slling error = dsl_dsobj_to_dsname(bpath, objnum, outpath); 8823912Slling *slashp = '/'; 8833912Slling 8843912Slling return (error); 8853912Slling } 8863912Slling 8871544Seschrock static int 8881544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why) 8891544Seschrock { 8901544Seschrock int error = 0; 8911544Seschrock static int zfsrootdone = 0; 8921544Seschrock zfsvfs_t *zfsvfs = NULL; 8931544Seschrock znode_t *zp = NULL; 8941544Seschrock vnode_t *vp = NULL; 8956423Sgw25295 char *zfs_bootfs; 8967147Staylor char *zfs_devid; 8971544Seschrock 8981544Seschrock ASSERT(vfsp); 8991544Seschrock 9001544Seschrock /* 9013912Slling * The filesystem that we mount as root is defined in the 9026423Sgw25295 * boot property "zfs-bootfs" with a format of 9036423Sgw25295 * "poolname/root-dataset-objnum". 9041544Seschrock */ 9051544Seschrock if (why == ROOT_INIT) { 9061544Seschrock if (zfsrootdone++) 9071544Seschrock return (EBUSY); 9086423Sgw25295 /* 9096423Sgw25295 * the process of doing a spa_load will require the 9106423Sgw25295 * clock to be set before we could (for example) do 9116423Sgw25295 * something better by looking at the timestamp on 9126423Sgw25295 * an uberblock, so just set it to -1. 9136423Sgw25295 */ 9146423Sgw25295 clkset(-1); 9151544Seschrock 9167147Staylor if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) { 9177147Staylor cmn_err(CE_NOTE, "spa_get_bootfs: can not get " 9187147Staylor "bootfs name"); 9196423Sgw25295 return (EINVAL); 9205648Ssetje } 9217147Staylor zfs_devid = spa_get_bootprop("diskdevid"); 9227147Staylor error = spa_import_rootpool(rootfs.bo_name, zfs_devid); 9237147Staylor if (zfs_devid) 9247147Staylor spa_free_bootprop(zfs_devid); 9257147Staylor if (error) { 9267147Staylor spa_free_bootprop(zfs_bootfs); 9277147Staylor cmn_err(CE_NOTE, "spa_import_rootpool: error %d", 9287147Staylor error); 9297147Staylor return (error); 9307147Staylor } 9317147Staylor if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) { 9327147Staylor spa_free_bootprop(zfs_bootfs); 9337147Staylor cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d", 9346423Sgw25295 error); 9356423Sgw25295 return (error); 9366423Sgw25295 } 9373912Slling 9387147Staylor spa_free_bootprop(zfs_bootfs); 9391544Seschrock 9401544Seschrock if (error = vfs_lock(vfsp)) 9411544Seschrock return (error); 9421544Seschrock 9437046Sahrens if (error = zfs_domount(vfsp, rootfs.bo_name)) { 9447147Staylor cmn_err(CE_NOTE, "zfs_domount: error %d", error); 9451544Seschrock goto out; 9466423Sgw25295 } 9471544Seschrock 9481544Seschrock zfsvfs = (zfsvfs_t *)vfsp->vfs_data; 9491544Seschrock ASSERT(zfsvfs); 9506423Sgw25295 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) { 9517147Staylor cmn_err(CE_NOTE, "zfs_zget: error %d", error); 9521544Seschrock goto out; 9536423Sgw25295 } 9541544Seschrock 9551544Seschrock vp = ZTOV(zp); 9561544Seschrock mutex_enter(&vp->v_lock); 9571544Seschrock vp->v_flag |= VROOT; 9581544Seschrock mutex_exit(&vp->v_lock); 9591544Seschrock rootvp = vp; 9601544Seschrock 9611544Seschrock /* 9626570Smarks * Leave rootvp held. The root file system is never unmounted. 9631544Seschrock */ 9641544Seschrock 9651544Seschrock vfs_add((struct vnode *)0, vfsp, 9661544Seschrock (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0); 9671544Seschrock out: 9681544Seschrock vfs_unlock(vfsp); 9696423Sgw25295 return (error); 9701544Seschrock } else if (why == ROOT_REMOUNT) { 9711544Seschrock readonly_changed_cb(vfsp->vfs_data, B_FALSE); 9721544Seschrock vfsp->vfs_flag |= VFS_REMOUNT; 9734596Slling 9744596Slling /* refresh mount options */ 9754596Slling zfs_unregister_callbacks(vfsp->vfs_data); 9764596Slling return (zfs_register_callbacks(vfsp)); 9774596Slling 9781544Seschrock } else if (why == ROOT_UNMOUNT) { 9791544Seschrock zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data); 9801544Seschrock (void) zfs_sync(vfsp, 0, 0); 9811544Seschrock return (0); 9821544Seschrock } 9831544Seschrock 9841544Seschrock /* 9851544Seschrock * if "why" is equal to anything else other than ROOT_INIT, 9861544Seschrock * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it. 9871544Seschrock */ 9881544Seschrock return (ENOTSUP); 9891544Seschrock } 9901544Seschrock 991789Sahrens /*ARGSUSED*/ 992789Sahrens static int 993789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr) 994789Sahrens { 995789Sahrens char *osname; 996789Sahrens pathname_t spn; 997789Sahrens int error = 0; 998789Sahrens uio_seg_t fromspace = (uap->flags & MS_SYSSPACE) ? 9993912Slling UIO_SYSSPACE : UIO_USERSPACE; 1000789Sahrens int canwrite; 1001789Sahrens 1002789Sahrens if (mvp->v_type != VDIR) 1003789Sahrens return (ENOTDIR); 1004789Sahrens 1005789Sahrens mutex_enter(&mvp->v_lock); 1006789Sahrens if ((uap->flags & MS_REMOUNT) == 0 && 1007789Sahrens (uap->flags & MS_OVERLAY) == 0 && 1008789Sahrens (mvp->v_count != 1 || (mvp->v_flag & VROOT))) { 1009789Sahrens mutex_exit(&mvp->v_lock); 1010789Sahrens return (EBUSY); 1011789Sahrens } 1012789Sahrens mutex_exit(&mvp->v_lock); 1013789Sahrens 1014789Sahrens /* 1015789Sahrens * ZFS does not support passing unparsed data in via MS_DATA. 1016789Sahrens * Users should use the MS_OPTIONSTR interface; this means 1017789Sahrens * that all option parsing is already done and the options struct 1018789Sahrens * can be interrogated. 1019789Sahrens */ 1020789Sahrens if ((uap->flags & MS_DATA) && uap->datalen > 0) 1021789Sahrens return (EINVAL); 1022789Sahrens 1023789Sahrens /* 1024789Sahrens * Get the objset name (the "special" mount argument). 1025789Sahrens */ 1026789Sahrens if (error = pn_get(uap->spec, fromspace, &spn)) 1027789Sahrens return (error); 1028789Sahrens 1029789Sahrens osname = spn.pn_path; 1030789Sahrens 10314543Smarks /* 10324543Smarks * Check for mount privilege? 10334543Smarks * 10344543Smarks * If we don't have privilege then see if 10354543Smarks * we have local permission to allow it 10364543Smarks */ 10374543Smarks error = secpolicy_fs_mount(cr, mvp, vfsp); 10384543Smarks if (error) { 10394543Smarks error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr); 10404543Smarks if (error == 0) { 10414543Smarks vattr_t vattr; 10424543Smarks 10434543Smarks /* 10444543Smarks * Make sure user is the owner of the mount point 10454543Smarks * or has sufficient privileges. 10464543Smarks */ 10474543Smarks 10484543Smarks vattr.va_mask = AT_UID; 10494543Smarks 10505331Samw if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) { 10514543Smarks goto out; 10524543Smarks } 10534543Smarks 10545489Smarks if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 && 10555489Smarks VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) { 10565489Smarks error = EPERM; 10574543Smarks goto out; 10584543Smarks } 10594543Smarks 10604543Smarks secpolicy_fs_mount_clearopts(cr, vfsp); 10614543Smarks } else { 10624543Smarks goto out; 10634543Smarks } 10644543Smarks } 1065789Sahrens 1066789Sahrens /* 1067789Sahrens * Refuse to mount a filesystem if we are in a local zone and the 1068789Sahrens * dataset is not visible. 1069789Sahrens */ 1070789Sahrens if (!INGLOBALZONE(curproc) && 1071789Sahrens (!zone_dataset_visible(osname, &canwrite) || !canwrite)) { 1072789Sahrens error = EPERM; 1073789Sahrens goto out; 1074789Sahrens } 1075789Sahrens 10764596Slling /* 10774596Slling * When doing a remount, we simply refresh our temporary properties 10784596Slling * according to those options set in the current VFS options. 10794596Slling */ 10804596Slling if (uap->flags & MS_REMOUNT) { 10814596Slling /* refresh mount options */ 10824596Slling zfs_unregister_callbacks(vfsp->vfs_data); 10834596Slling error = zfs_register_callbacks(vfsp); 10844596Slling goto out; 10854596Slling } 10864596Slling 10877046Sahrens error = zfs_domount(vfsp, osname); 1088789Sahrens 10899214Schris.kirby@sun.com /* 10909214Schris.kirby@sun.com * Add an extra VFS_HOLD on our parent vfs so that it can't 10919214Schris.kirby@sun.com * disappear due to a forced unmount. 10929214Schris.kirby@sun.com */ 10939246Schris.kirby@sun.com if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap) 10949214Schris.kirby@sun.com VFS_HOLD(mvp->v_vfsp); 10959214Schris.kirby@sun.com 1096789Sahrens out: 1097789Sahrens pn_free(&spn); 1098789Sahrens return (error); 1099789Sahrens } 1100789Sahrens 1101789Sahrens static int 1102789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp) 1103789Sahrens { 1104789Sahrens zfsvfs_t *zfsvfs = vfsp->vfs_data; 1105789Sahrens dev32_t d32; 11062885Sahrens uint64_t refdbytes, availbytes, usedobjs, availobjs; 1107789Sahrens 1108789Sahrens ZFS_ENTER(zfsvfs); 1109789Sahrens 11102885Sahrens dmu_objset_space(zfsvfs->z_os, 11112885Sahrens &refdbytes, &availbytes, &usedobjs, &availobjs); 1112789Sahrens 1113789Sahrens /* 1114789Sahrens * The underlying storage pool actually uses multiple block sizes. 1115789Sahrens * We report the fragsize as the smallest block size we support, 1116789Sahrens * and we report our blocksize as the filesystem's maximum blocksize. 1117789Sahrens */ 1118789Sahrens statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT; 1119789Sahrens statp->f_bsize = zfsvfs->z_max_blksz; 1120789Sahrens 1121789Sahrens /* 1122789Sahrens * The following report "total" blocks of various kinds in the 1123789Sahrens * file system, but reported in terms of f_frsize - the 1124789Sahrens * "fragment" size. 1125789Sahrens */ 1126789Sahrens 11272885Sahrens statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT; 11282885Sahrens statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT; 1129789Sahrens statp->f_bavail = statp->f_bfree; /* no root reservation */ 1130789Sahrens 1131789Sahrens /* 1132789Sahrens * statvfs() should really be called statufs(), because it assumes 1133789Sahrens * static metadata. ZFS doesn't preallocate files, so the best 1134789Sahrens * we can do is report the max that could possibly fit in f_files, 1135789Sahrens * and that minus the number actually used in f_ffree. 1136789Sahrens * For f_ffree, report the smaller of the number of object available 1137789Sahrens * and the number of blocks (each object will take at least a block). 1138789Sahrens */ 11392885Sahrens statp->f_ffree = MIN(availobjs, statp->f_bfree); 1140789Sahrens statp->f_favail = statp->f_ffree; /* no "root reservation" */ 11412885Sahrens statp->f_files = statp->f_ffree + usedobjs; 1142789Sahrens 1143789Sahrens (void) cmpldev(&d32, vfsp->vfs_dev); 1144789Sahrens statp->f_fsid = d32; 1145789Sahrens 1146789Sahrens /* 1147789Sahrens * We're a zfs filesystem. 1148789Sahrens */ 1149789Sahrens (void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name); 1150789Sahrens 11511123Smarks statp->f_flag = vf_to_stf(vfsp->vfs_flag); 1152789Sahrens 1153789Sahrens statp->f_namemax = ZFS_MAXNAMELEN; 1154789Sahrens 1155789Sahrens /* 1156789Sahrens * We have all of 32 characters to stuff a string here. 1157789Sahrens * Is there anything useful we could/should provide? 1158789Sahrens */ 1159789Sahrens bzero(statp->f_fstr, sizeof (statp->f_fstr)); 1160789Sahrens 1161789Sahrens ZFS_EXIT(zfsvfs); 1162789Sahrens return (0); 1163789Sahrens } 1164789Sahrens 1165789Sahrens static int 1166789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp) 1167789Sahrens { 1168789Sahrens zfsvfs_t *zfsvfs = vfsp->vfs_data; 1169789Sahrens znode_t *rootzp; 1170789Sahrens int error; 1171789Sahrens 1172789Sahrens ZFS_ENTER(zfsvfs); 1173789Sahrens 1174789Sahrens error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp); 1175789Sahrens if (error == 0) 1176789Sahrens *vpp = ZTOV(rootzp); 1177789Sahrens 1178789Sahrens ZFS_EXIT(zfsvfs); 1179789Sahrens return (error); 1180789Sahrens } 1181789Sahrens 11825326Sek110237 /* 11835326Sek110237 * Teardown the zfsvfs::z_os. 11845326Sek110237 * 11855326Sek110237 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock' 11865326Sek110237 * and 'z_teardown_inactive_lock' held. 11875326Sek110237 */ 11885326Sek110237 static int 11895326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting) 11905326Sek110237 { 11915642Smaybee znode_t *zp; 11925326Sek110237 11935326Sek110237 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG); 11945326Sek110237 11955326Sek110237 if (!unmounting) { 11965326Sek110237 /* 11975326Sek110237 * We purge the parent filesystem's vfsp as the parent 11985326Sek110237 * filesystem and all of its snapshots have their vnode's 11995326Sek110237 * v_vfsp set to the parent's filesystem's vfsp. Note, 12005326Sek110237 * 'z_parent' is self referential for non-snapshots. 12015326Sek110237 */ 12025326Sek110237 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0); 12035326Sek110237 } 12045326Sek110237 12055326Sek110237 /* 12065326Sek110237 * Close the zil. NB: Can't close the zil while zfs_inactive 12075326Sek110237 * threads are blocked as zil_close can call zfs_inactive. 12085326Sek110237 */ 12095326Sek110237 if (zfsvfs->z_log) { 12105326Sek110237 zil_close(zfsvfs->z_log); 12115326Sek110237 zfsvfs->z_log = NULL; 12125326Sek110237 } 12135326Sek110237 12145326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER); 12155326Sek110237 12165326Sek110237 /* 12175326Sek110237 * If we are not unmounting (ie: online recv) and someone already 12185326Sek110237 * unmounted this file system while we were doing the switcheroo, 12195326Sek110237 * or a reopen of z_os failed then just bail out now. 12205326Sek110237 */ 12215326Sek110237 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) { 12225326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 12235326Sek110237 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 12245326Sek110237 return (EIO); 12255326Sek110237 } 12265326Sek110237 12275326Sek110237 /* 12285326Sek110237 * At this point there are no vops active, and any new vops will 12295326Sek110237 * fail with EIO since we have z_teardown_lock for writer (only 12305326Sek110237 * relavent for forced unmount). 12315326Sek110237 * 12325326Sek110237 * Release all holds on dbufs. 12335326Sek110237 */ 12345326Sek110237 mutex_enter(&zfsvfs->z_znodes_lock); 12355642Smaybee for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL; 12365642Smaybee zp = list_next(&zfsvfs->z_all_znodes, zp)) 12375446Sahrens if (zp->z_dbuf) { 12385642Smaybee ASSERT(ZTOV(zp)->v_count > 0); 12395642Smaybee zfs_znode_dmu_fini(zp); 12405326Sek110237 } 12415326Sek110237 mutex_exit(&zfsvfs->z_znodes_lock); 12425326Sek110237 12435326Sek110237 /* 12445326Sek110237 * If we are unmounting, set the unmounted flag and let new vops 12455326Sek110237 * unblock. zfs_inactive will have the unmounted behavior, and all 12465326Sek110237 * other vops will fail with EIO. 12475326Sek110237 */ 12485326Sek110237 if (unmounting) { 12495326Sek110237 zfsvfs->z_unmounted = B_TRUE; 12505326Sek110237 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 12515326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 12525326Sek110237 } 12535326Sek110237 12545326Sek110237 /* 12555326Sek110237 * z_os will be NULL if there was an error in attempting to reopen 12565326Sek110237 * zfsvfs, so just return as the properties had already been 12575326Sek110237 * unregistered and cached data had been evicted before. 12585326Sek110237 */ 12595326Sek110237 if (zfsvfs->z_os == NULL) 12605326Sek110237 return (0); 12615326Sek110237 12625326Sek110237 /* 12635326Sek110237 * Unregister properties. 12645326Sek110237 */ 12655326Sek110237 zfs_unregister_callbacks(zfsvfs); 12665326Sek110237 12675326Sek110237 /* 12685326Sek110237 * Evict cached data 12695326Sek110237 */ 12706083Sek110237 if (dmu_objset_evict_dbufs(zfsvfs->z_os)) { 12715429Smaybee txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0); 12726083Sek110237 (void) dmu_objset_evict_dbufs(zfsvfs->z_os); 12735429Smaybee } 12745326Sek110237 12755326Sek110237 return (0); 12765326Sek110237 } 12775326Sek110237 1278789Sahrens /*ARGSUSED*/ 1279789Sahrens static int 1280789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr) 1281789Sahrens { 1282789Sahrens zfsvfs_t *zfsvfs = vfsp->vfs_data; 12835326Sek110237 objset_t *os; 1284789Sahrens int ret; 1285789Sahrens 12864543Smarks ret = secpolicy_fs_unmount(cr, vfsp); 12874543Smarks if (ret) { 12884543Smarks ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource), 12894543Smarks ZFS_DELEG_PERM_MOUNT, cr); 12904543Smarks if (ret) 12914543Smarks return (ret); 12924543Smarks } 12931484Sek110237 12944736Sek110237 /* 12954736Sek110237 * We purge the parent filesystem's vfsp as the parent filesystem 12964736Sek110237 * and all of its snapshots have their vnode's v_vfsp set to the 12974736Sek110237 * parent's filesystem's vfsp. Note, 'z_parent' is self 12984736Sek110237 * referential for non-snapshots. 12994736Sek110237 */ 13004736Sek110237 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0); 13011484Sek110237 1302789Sahrens /* 1303789Sahrens * Unmount any snapshots mounted under .zfs before unmounting the 1304789Sahrens * dataset itself. 1305789Sahrens */ 1306789Sahrens if (zfsvfs->z_ctldir != NULL && 13074543Smarks (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) { 1308789Sahrens return (ret); 13094543Smarks } 1310789Sahrens 13114787Sahrens if (!(fflag & MS_FORCE)) { 13124480Sgw25295 /* 13134787Sahrens * Check the number of active vnodes in the file system. 13144787Sahrens * Our count is maintained in the vfs structure, but the 13154787Sahrens * number is off by 1 to indicate a hold on the vfs 13164787Sahrens * structure itself. 13174787Sahrens * 13184787Sahrens * The '.zfs' directory maintains a reference of its 13194787Sahrens * own, and any active references underneath are 13204787Sahrens * reflected in the vnode count. 1321789Sahrens */ 13224787Sahrens if (zfsvfs->z_ctldir == NULL) { 13234787Sahrens if (vfsp->vfs_count > 1) 13244787Sahrens return (EBUSY); 13254787Sahrens } else { 13264787Sahrens if (vfsp->vfs_count > 2 || 13275326Sek110237 zfsvfs->z_ctldir->v_count > 1) 13284787Sahrens return (EBUSY); 1329789Sahrens } 1330789Sahrens } 1331789Sahrens 1332789Sahrens vfsp->vfs_flag |= VFS_UNMOUNTED; 13334787Sahrens 13345326Sek110237 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0); 13355326Sek110237 os = zfsvfs->z_os; 13364787Sahrens 13374787Sahrens /* 13385326Sek110237 * z_os will be NULL if there was an error in 13395326Sek110237 * attempting to reopen zfsvfs. 13404787Sahrens */ 13415326Sek110237 if (os != NULL) { 13425326Sek110237 /* 13435326Sek110237 * Unset the objset user_ptr. 13445326Sek110237 */ 13455326Sek110237 mutex_enter(&os->os->os_user_ptr_lock); 13465326Sek110237 dmu_objset_set_user(os, NULL); 13475326Sek110237 mutex_exit(&os->os->os_user_ptr_lock); 13484787Sahrens 13495326Sek110237 /* 13506689Smaybee * Finally release the objset 13515326Sek110237 */ 13525326Sek110237 dmu_objset_close(os); 13534787Sahrens } 13544787Sahrens 13554787Sahrens /* 13564787Sahrens * We can now safely destroy the '.zfs' directory node. 13574787Sahrens */ 13584787Sahrens if (zfsvfs->z_ctldir != NULL) 13594787Sahrens zfsctl_destroy(zfsvfs); 1360789Sahrens 1361789Sahrens return (0); 1362789Sahrens } 1363789Sahrens 1364789Sahrens static int 1365789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp) 1366789Sahrens { 1367789Sahrens zfsvfs_t *zfsvfs = vfsp->vfs_data; 1368789Sahrens znode_t *zp; 1369789Sahrens uint64_t object = 0; 1370789Sahrens uint64_t fid_gen = 0; 1371789Sahrens uint64_t gen_mask; 1372789Sahrens uint64_t zp_gen; 1373789Sahrens int i, err; 1374789Sahrens 1375789Sahrens *vpp = NULL; 1376789Sahrens 1377789Sahrens ZFS_ENTER(zfsvfs); 1378789Sahrens 1379789Sahrens if (fidp->fid_len == LONG_FID_LEN) { 1380789Sahrens zfid_long_t *zlfid = (zfid_long_t *)fidp; 1381789Sahrens uint64_t objsetid = 0; 1382789Sahrens uint64_t setgen = 0; 1383789Sahrens 1384789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 1385789Sahrens objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i); 1386789Sahrens 1387789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 1388789Sahrens setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i); 1389789Sahrens 1390789Sahrens ZFS_EXIT(zfsvfs); 1391789Sahrens 1392789Sahrens err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs); 1393789Sahrens if (err) 1394789Sahrens return (EINVAL); 1395789Sahrens ZFS_ENTER(zfsvfs); 1396789Sahrens } 1397789Sahrens 1398789Sahrens if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) { 1399789Sahrens zfid_short_t *zfid = (zfid_short_t *)fidp; 1400789Sahrens 1401789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 1402789Sahrens object |= ((uint64_t)zfid->zf_object[i]) << (8 * i); 1403789Sahrens 1404789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 1405789Sahrens fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i); 1406789Sahrens } else { 1407789Sahrens ZFS_EXIT(zfsvfs); 1408789Sahrens return (EINVAL); 1409789Sahrens } 1410789Sahrens 1411789Sahrens /* A zero fid_gen means we are in the .zfs control directories */ 1412789Sahrens if (fid_gen == 0 && 1413789Sahrens (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) { 1414789Sahrens *vpp = zfsvfs->z_ctldir; 1415789Sahrens ASSERT(*vpp != NULL); 1416789Sahrens if (object == ZFSCTL_INO_SNAPDIR) { 1417789Sahrens VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL, 14185331Samw 0, NULL, NULL, NULL, NULL, NULL) == 0); 1419789Sahrens } else { 1420789Sahrens VN_HOLD(*vpp); 1421789Sahrens } 1422789Sahrens ZFS_EXIT(zfsvfs); 1423789Sahrens return (0); 1424789Sahrens } 1425789Sahrens 1426789Sahrens gen_mask = -1ULL >> (64 - 8 * i); 1427789Sahrens 1428789Sahrens dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask); 1429789Sahrens if (err = zfs_zget(zfsvfs, object, &zp)) { 1430789Sahrens ZFS_EXIT(zfsvfs); 1431789Sahrens return (err); 1432789Sahrens } 1433789Sahrens zp_gen = zp->z_phys->zp_gen & gen_mask; 1434789Sahrens if (zp_gen == 0) 1435789Sahrens zp_gen = 1; 14363461Sahrens if (zp->z_unlinked || zp_gen != fid_gen) { 1437789Sahrens dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen); 1438789Sahrens VN_RELE(ZTOV(zp)); 1439789Sahrens ZFS_EXIT(zfsvfs); 1440789Sahrens return (EINVAL); 1441789Sahrens } 1442789Sahrens 1443789Sahrens *vpp = ZTOV(zp); 1444789Sahrens ZFS_EXIT(zfsvfs); 1445789Sahrens return (0); 1446789Sahrens } 1447789Sahrens 14485326Sek110237 /* 14495326Sek110237 * Block out VOPs and close zfsvfs_t::z_os 14505326Sek110237 * 14515326Sek110237 * Note, if successful, then we return with the 'z_teardown_lock' and 14525326Sek110237 * 'z_teardown_inactive_lock' write held. 14535326Sek110237 */ 14545326Sek110237 int 14555326Sek110237 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode) 14565326Sek110237 { 14575326Sek110237 int error; 14585326Sek110237 14595326Sek110237 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0) 14605326Sek110237 return (error); 14615326Sek110237 14625326Sek110237 *mode = zfsvfs->z_os->os_mode; 14635326Sek110237 dmu_objset_name(zfsvfs->z_os, name); 14645326Sek110237 dmu_objset_close(zfsvfs->z_os); 14655326Sek110237 14665326Sek110237 return (0); 14675326Sek110237 } 14685326Sek110237 14695326Sek110237 /* 14705326Sek110237 * Reopen zfsvfs_t::z_os and release VOPs. 14715326Sek110237 */ 14725326Sek110237 int 14735326Sek110237 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode) 14745326Sek110237 { 14755326Sek110237 int err; 14765326Sek110237 14775326Sek110237 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock)); 14785326Sek110237 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)); 14795326Sek110237 14805326Sek110237 err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os); 14815326Sek110237 if (err) { 14825326Sek110237 zfsvfs->z_os = NULL; 14835326Sek110237 } else { 14845326Sek110237 znode_t *zp; 14855326Sek110237 14865326Sek110237 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0); 14875326Sek110237 14885326Sek110237 /* 14895326Sek110237 * Attempt to re-establish all the active znodes with 14905326Sek110237 * their dbufs. If a zfs_rezget() fails, then we'll let 14915326Sek110237 * any potential callers discover that via ZFS_ENTER_VERIFY_VP 14925326Sek110237 * when they try to use their znode. 14935326Sek110237 */ 14945326Sek110237 mutex_enter(&zfsvfs->z_znodes_lock); 14955326Sek110237 for (zp = list_head(&zfsvfs->z_all_znodes); zp; 14965326Sek110237 zp = list_next(&zfsvfs->z_all_znodes, zp)) { 14975326Sek110237 (void) zfs_rezget(zp); 14985326Sek110237 } 14995326Sek110237 mutex_exit(&zfsvfs->z_znodes_lock); 15005326Sek110237 15015326Sek110237 } 15025326Sek110237 15035326Sek110237 /* release the VOPs */ 15045326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 15055326Sek110237 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 15065326Sek110237 15075326Sek110237 if (err) { 15085326Sek110237 /* 15095326Sek110237 * Since we couldn't reopen zfsvfs::z_os, force 15105326Sek110237 * unmount this file system. 15115326Sek110237 */ 15125326Sek110237 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0) 15135326Sek110237 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED()); 15145326Sek110237 } 15155326Sek110237 return (err); 15165326Sek110237 } 15175326Sek110237 1518789Sahrens static void 1519789Sahrens zfs_freevfs(vfs_t *vfsp) 1520789Sahrens { 1521789Sahrens zfsvfs_t *zfsvfs = vfsp->vfs_data; 15224831Sgw25295 int i; 15234831Sgw25295 15244831Sgw25295 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 15254831Sgw25295 mutex_destroy(&zfsvfs->z_hold_mtx[i]); 1526789Sahrens 15275331Samw zfs_fuid_destroy(zfsvfs); 15289214Schris.kirby@sun.com 15299214Schris.kirby@sun.com /* 15309214Schris.kirby@sun.com * If this is a snapshot, we have an extra VFS_HOLD on our parent 15319214Schris.kirby@sun.com * from zfs_mount(). Release it here. 15329214Schris.kirby@sun.com */ 15339214Schris.kirby@sun.com if (zfsvfs->z_issnap) 15349214Schris.kirby@sun.com VFS_RELE(zfsvfs->z_parent->z_vfs); 15359214Schris.kirby@sun.com 15366083Sek110237 zfs_freezfsvfs(zfsvfs); 1537789Sahrens 1538789Sahrens atomic_add_32(&zfs_active_fs_count, -1); 1539789Sahrens } 1540789Sahrens 1541789Sahrens /* 1542789Sahrens * VFS_INIT() initialization. Note that there is no VFS_FINI(), 1543789Sahrens * so we can't safely do any non-idempotent initialization here. 1544789Sahrens * Leave that to zfs_init() and zfs_fini(), which are called 1545789Sahrens * from the module's _init() and _fini() entry points. 1546789Sahrens */ 1547789Sahrens /*ARGSUSED*/ 1548789Sahrens static int 1549789Sahrens zfs_vfsinit(int fstype, char *name) 1550789Sahrens { 1551789Sahrens int error; 1552789Sahrens 1553789Sahrens zfsfstype = fstype; 1554789Sahrens 1555789Sahrens /* 1556789Sahrens * Setup vfsops and vnodeops tables. 1557789Sahrens */ 1558789Sahrens error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops); 1559789Sahrens if (error != 0) { 1560789Sahrens cmn_err(CE_WARN, "zfs: bad vfs ops template"); 1561789Sahrens } 1562789Sahrens 1563789Sahrens error = zfs_create_op_tables(); 1564789Sahrens if (error) { 1565789Sahrens zfs_remove_op_tables(); 1566789Sahrens cmn_err(CE_WARN, "zfs: bad vnode ops template"); 1567789Sahrens (void) vfs_freevfsops_by_type(zfsfstype); 1568789Sahrens return (error); 1569789Sahrens } 1570789Sahrens 1571789Sahrens mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 1572789Sahrens 1573789Sahrens /* 1574849Sbonwick * Unique major number for all zfs mounts. 1575849Sbonwick * If we run out of 32-bit minors, we'll getudev() another major. 1576789Sahrens */ 1577849Sbonwick zfs_major = ddi_name_to_major(ZFS_DRIVER); 1578849Sbonwick zfs_minor = ZFS_MIN_MINOR; 1579789Sahrens 1580789Sahrens return (0); 1581789Sahrens } 1582789Sahrens 1583789Sahrens void 1584789Sahrens zfs_init(void) 1585789Sahrens { 1586789Sahrens /* 1587789Sahrens * Initialize .zfs directory structures 1588789Sahrens */ 1589789Sahrens zfsctl_init(); 1590789Sahrens 1591789Sahrens /* 1592789Sahrens * Initialize znode cache, vnode ops, etc... 1593789Sahrens */ 1594789Sahrens zfs_znode_init(); 1595789Sahrens } 1596789Sahrens 1597789Sahrens void 1598789Sahrens zfs_fini(void) 1599789Sahrens { 1600789Sahrens zfsctl_fini(); 1601789Sahrens zfs_znode_fini(); 1602789Sahrens } 1603789Sahrens 1604789Sahrens int 1605789Sahrens zfs_busy(void) 1606789Sahrens { 1607789Sahrens return (zfs_active_fs_count != 0); 1608789Sahrens } 1609789Sahrens 16104577Sahrens int 16114577Sahrens zfs_set_version(const char *name, uint64_t newvers) 16124577Sahrens { 16134577Sahrens int error; 16144577Sahrens objset_t *os; 16154577Sahrens dmu_tx_t *tx; 16164577Sahrens uint64_t curvers; 16174577Sahrens 16184577Sahrens /* 16194577Sahrens * XXX for now, require that the filesystem be unmounted. Would 16204577Sahrens * be nice to find the zfsvfs_t and just update that if 16214577Sahrens * possible. 16224577Sahrens */ 16234577Sahrens 16244577Sahrens if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION) 16254577Sahrens return (EINVAL); 16264577Sahrens 16276689Smaybee error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os); 16284577Sahrens if (error) 16294577Sahrens return (error); 16304577Sahrens 16314577Sahrens error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 16324577Sahrens 8, 1, &curvers); 16334577Sahrens if (error) 16344577Sahrens goto out; 16354577Sahrens if (newvers < curvers) { 16364577Sahrens error = EINVAL; 16374577Sahrens goto out; 16384577Sahrens } 16394577Sahrens 16404577Sahrens tx = dmu_tx_create(os); 16414577Sahrens dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR); 16424577Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 16434577Sahrens if (error) { 16444577Sahrens dmu_tx_abort(tx); 16454577Sahrens goto out; 16464577Sahrens } 16474577Sahrens error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1, 16484577Sahrens &newvers, tx); 16494577Sahrens 16504577Sahrens spa_history_internal_log(LOG_DS_UPGRADE, 16514577Sahrens dmu_objset_spa(os), tx, CRED(), 16524577Sahrens "oldver=%llu newver=%llu dataset = %llu", curvers, newvers, 16534577Sahrens dmu_objset_id(os)); 16544577Sahrens dmu_tx_commit(tx); 16554577Sahrens 16564577Sahrens out: 16574577Sahrens dmu_objset_close(os); 16584577Sahrens return (error); 16594577Sahrens } 16604577Sahrens 16615498Stimh /* 16625498Stimh * Read a property stored within the master node. 16635498Stimh */ 16645498Stimh int 16655498Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value) 16665498Stimh { 16675498Stimh const char *pname; 16687184Stimh int error = ENOENT; 16695498Stimh 16705498Stimh /* 16715498Stimh * Look up the file system's value for the property. For the 16725498Stimh * version property, we look up a slightly different string. 16735498Stimh */ 16745498Stimh if (prop == ZFS_PROP_VERSION) 16755498Stimh pname = ZPL_VERSION_STR; 16765498Stimh else 16775498Stimh pname = zfs_prop_to_name(prop); 16785498Stimh 16797184Stimh if (os != NULL) 16807184Stimh error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value); 16815498Stimh 16826404Smaybee if (error == ENOENT) { 16835498Stimh /* No value set, use the default value */ 16845498Stimh switch (prop) { 16856404Smaybee case ZFS_PROP_VERSION: 16866404Smaybee *value = ZPL_VERSION; 16876404Smaybee break; 16885498Stimh case ZFS_PROP_NORMALIZE: 16895498Stimh case ZFS_PROP_UTF8ONLY: 16905498Stimh *value = 0; 16915498Stimh break; 16925498Stimh case ZFS_PROP_CASE: 16935498Stimh *value = ZFS_CASE_SENSITIVE; 16945498Stimh break; 16955498Stimh default: 16966404Smaybee return (error); 16975498Stimh } 16986404Smaybee error = 0; 16995498Stimh } 17006404Smaybee return (error); 17015498Stimh } 17025498Stimh 1703789Sahrens static vfsdef_t vfw = { 1704789Sahrens VFSDEF_VERSION, 1705789Sahrens MNTTYPE_ZFS, 1706789Sahrens zfs_vfsinit, 17075331Samw VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS| 17085331Samw VSW_XID, 1709789Sahrens &zfs_mntopts 1710789Sahrens }; 1711789Sahrens 1712789Sahrens struct modlfs zfs_modlfs = { 17134577Sahrens &mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw 1714789Sahrens }; 1715