1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * CDDL HEADER START 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5*eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6*eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7*eda14cbcSMatt Macy * 8*eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10*eda14cbcSMatt Macy * See the License for the specific language governing permissions 11*eda14cbcSMatt Macy * and limitations under the License. 12*eda14cbcSMatt Macy * 13*eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14*eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16*eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17*eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18*eda14cbcSMatt Macy * 19*eda14cbcSMatt Macy * CDDL HEADER END 20*eda14cbcSMatt Macy */ 21*eda14cbcSMatt Macy 22*eda14cbcSMatt Macy /* 23*eda14cbcSMatt Macy * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 24*eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 25*eda14cbcSMatt Macy * Copyright (c) 2014, 2020 by Delphix. All rights reserved. 26*eda14cbcSMatt Macy * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 27*eda14cbcSMatt Macy * Copyright 2017 RackTop Systems. 28*eda14cbcSMatt Macy * Copyright (c) 2018 Datto Inc. 29*eda14cbcSMatt Macy * Copyright 2018 OmniOS Community Edition (OmniOSce) Association. 30*eda14cbcSMatt Macy */ 31*eda14cbcSMatt Macy 32*eda14cbcSMatt Macy /* 33*eda14cbcSMatt Macy * Routines to manage ZFS mounts. We separate all the nasty routines that have 34*eda14cbcSMatt Macy * to deal with the OS. The following functions are the main entry points -- 35*eda14cbcSMatt Macy * they are used by mount and unmount and when changing a filesystem's 36*eda14cbcSMatt Macy * mountpoint. 37*eda14cbcSMatt Macy * 38*eda14cbcSMatt Macy * zfs_is_mounted() 39*eda14cbcSMatt Macy * zfs_mount() 40*eda14cbcSMatt Macy * zfs_mount_at() 41*eda14cbcSMatt Macy * zfs_unmount() 42*eda14cbcSMatt Macy * zfs_unmountall() 43*eda14cbcSMatt Macy * 44*eda14cbcSMatt Macy * This file also contains the functions used to manage sharing filesystems via 45*eda14cbcSMatt Macy * NFS and iSCSI: 46*eda14cbcSMatt Macy * 47*eda14cbcSMatt Macy * zfs_is_shared() 48*eda14cbcSMatt Macy * zfs_share() 49*eda14cbcSMatt Macy * zfs_unshare() 50*eda14cbcSMatt Macy * 51*eda14cbcSMatt Macy * zfs_is_shared_nfs() 52*eda14cbcSMatt Macy * zfs_is_shared_smb() 53*eda14cbcSMatt Macy * zfs_share_proto() 54*eda14cbcSMatt Macy * zfs_shareall(); 55*eda14cbcSMatt Macy * zfs_unshare_nfs() 56*eda14cbcSMatt Macy * zfs_unshare_smb() 57*eda14cbcSMatt Macy * zfs_unshareall_nfs() 58*eda14cbcSMatt Macy * zfs_unshareall_smb() 59*eda14cbcSMatt Macy * zfs_unshareall() 60*eda14cbcSMatt Macy * zfs_unshareall_bypath() 61*eda14cbcSMatt Macy * 62*eda14cbcSMatt Macy * The following functions are available for pool consumers, and will 63*eda14cbcSMatt Macy * mount/unmount and share/unshare all datasets within pool: 64*eda14cbcSMatt Macy * 65*eda14cbcSMatt Macy * zpool_enable_datasets() 66*eda14cbcSMatt Macy * zpool_disable_datasets() 67*eda14cbcSMatt Macy */ 68*eda14cbcSMatt Macy 69*eda14cbcSMatt Macy #include <dirent.h> 70*eda14cbcSMatt Macy #include <dlfcn.h> 71*eda14cbcSMatt Macy #include <errno.h> 72*eda14cbcSMatt Macy #include <fcntl.h> 73*eda14cbcSMatt Macy #include <libgen.h> 74*eda14cbcSMatt Macy #include <libintl.h> 75*eda14cbcSMatt Macy #include <stdio.h> 76*eda14cbcSMatt Macy #include <stdlib.h> 77*eda14cbcSMatt Macy #include <strings.h> 78*eda14cbcSMatt Macy #include <unistd.h> 79*eda14cbcSMatt Macy #include <zone.h> 80*eda14cbcSMatt Macy #include <sys/mntent.h> 81*eda14cbcSMatt Macy #include <sys/mount.h> 82*eda14cbcSMatt Macy #include <sys/stat.h> 83*eda14cbcSMatt Macy #include <sys/vfs.h> 84*eda14cbcSMatt Macy #include <sys/dsl_crypt.h> 85*eda14cbcSMatt Macy 86*eda14cbcSMatt Macy #include <libzfs.h> 87*eda14cbcSMatt Macy 88*eda14cbcSMatt Macy #include "libzfs_impl.h" 89*eda14cbcSMatt Macy #include <thread_pool.h> 90*eda14cbcSMatt Macy 91*eda14cbcSMatt Macy #include <libshare.h> 92*eda14cbcSMatt Macy #include <sys/systeminfo.h> 93*eda14cbcSMatt Macy #define MAXISALEN 257 /* based on sysinfo(2) man page */ 94*eda14cbcSMatt Macy 95*eda14cbcSMatt Macy static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */ 96*eda14cbcSMatt Macy 97*eda14cbcSMatt Macy static void zfs_mount_task(void *); 98*eda14cbcSMatt Macy zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **, 99*eda14cbcSMatt Macy zfs_share_proto_t); 100*eda14cbcSMatt Macy 101*eda14cbcSMatt Macy /* 102*eda14cbcSMatt Macy * The share protocols table must be in the same order as the zfs_share_proto_t 103*eda14cbcSMatt Macy * enum in libzfs_impl.h 104*eda14cbcSMatt Macy */ 105*eda14cbcSMatt Macy proto_table_t proto_table[PROTO_END] = { 106*eda14cbcSMatt Macy {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED}, 107*eda14cbcSMatt Macy {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED}, 108*eda14cbcSMatt Macy }; 109*eda14cbcSMatt Macy 110*eda14cbcSMatt Macy zfs_share_proto_t nfs_only[] = { 111*eda14cbcSMatt Macy PROTO_NFS, 112*eda14cbcSMatt Macy PROTO_END 113*eda14cbcSMatt Macy }; 114*eda14cbcSMatt Macy 115*eda14cbcSMatt Macy zfs_share_proto_t smb_only[] = { 116*eda14cbcSMatt Macy PROTO_SMB, 117*eda14cbcSMatt Macy PROTO_END 118*eda14cbcSMatt Macy }; 119*eda14cbcSMatt Macy zfs_share_proto_t share_all_proto[] = { 120*eda14cbcSMatt Macy PROTO_NFS, 121*eda14cbcSMatt Macy PROTO_SMB, 122*eda14cbcSMatt Macy PROTO_END 123*eda14cbcSMatt Macy }; 124*eda14cbcSMatt Macy 125*eda14cbcSMatt Macy 126*eda14cbcSMatt Macy 127*eda14cbcSMatt Macy static boolean_t 128*eda14cbcSMatt Macy dir_is_empty_stat(const char *dirname) 129*eda14cbcSMatt Macy { 130*eda14cbcSMatt Macy struct stat st; 131*eda14cbcSMatt Macy 132*eda14cbcSMatt Macy /* 133*eda14cbcSMatt Macy * We only want to return false if the given path is a non empty 134*eda14cbcSMatt Macy * directory, all other errors are handled elsewhere. 135*eda14cbcSMatt Macy */ 136*eda14cbcSMatt Macy if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) { 137*eda14cbcSMatt Macy return (B_TRUE); 138*eda14cbcSMatt Macy } 139*eda14cbcSMatt Macy 140*eda14cbcSMatt Macy /* 141*eda14cbcSMatt Macy * An empty directory will still have two entries in it, one 142*eda14cbcSMatt Macy * entry for each of "." and "..". 143*eda14cbcSMatt Macy */ 144*eda14cbcSMatt Macy if (st.st_size > 2) { 145*eda14cbcSMatt Macy return (B_FALSE); 146*eda14cbcSMatt Macy } 147*eda14cbcSMatt Macy 148*eda14cbcSMatt Macy return (B_TRUE); 149*eda14cbcSMatt Macy } 150*eda14cbcSMatt Macy 151*eda14cbcSMatt Macy static boolean_t 152*eda14cbcSMatt Macy dir_is_empty_readdir(const char *dirname) 153*eda14cbcSMatt Macy { 154*eda14cbcSMatt Macy DIR *dirp; 155*eda14cbcSMatt Macy struct dirent64 *dp; 156*eda14cbcSMatt Macy int dirfd; 157*eda14cbcSMatt Macy 158*eda14cbcSMatt Macy if ((dirfd = openat(AT_FDCWD, dirname, 159*eda14cbcSMatt Macy O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) { 160*eda14cbcSMatt Macy return (B_TRUE); 161*eda14cbcSMatt Macy } 162*eda14cbcSMatt Macy 163*eda14cbcSMatt Macy if ((dirp = fdopendir(dirfd)) == NULL) { 164*eda14cbcSMatt Macy (void) close(dirfd); 165*eda14cbcSMatt Macy return (B_TRUE); 166*eda14cbcSMatt Macy } 167*eda14cbcSMatt Macy 168*eda14cbcSMatt Macy while ((dp = readdir64(dirp)) != NULL) { 169*eda14cbcSMatt Macy 170*eda14cbcSMatt Macy if (strcmp(dp->d_name, ".") == 0 || 171*eda14cbcSMatt Macy strcmp(dp->d_name, "..") == 0) 172*eda14cbcSMatt Macy continue; 173*eda14cbcSMatt Macy 174*eda14cbcSMatt Macy (void) closedir(dirp); 175*eda14cbcSMatt Macy return (B_FALSE); 176*eda14cbcSMatt Macy } 177*eda14cbcSMatt Macy 178*eda14cbcSMatt Macy (void) closedir(dirp); 179*eda14cbcSMatt Macy return (B_TRUE); 180*eda14cbcSMatt Macy } 181*eda14cbcSMatt Macy 182*eda14cbcSMatt Macy /* 183*eda14cbcSMatt Macy * Returns true if the specified directory is empty. If we can't open the 184*eda14cbcSMatt Macy * directory at all, return true so that the mount can fail with a more 185*eda14cbcSMatt Macy * informative error message. 186*eda14cbcSMatt Macy */ 187*eda14cbcSMatt Macy static boolean_t 188*eda14cbcSMatt Macy dir_is_empty(const char *dirname) 189*eda14cbcSMatt Macy { 190*eda14cbcSMatt Macy struct statfs64 st; 191*eda14cbcSMatt Macy 192*eda14cbcSMatt Macy /* 193*eda14cbcSMatt Macy * If the statvfs call fails or the filesystem is not a ZFS 194*eda14cbcSMatt Macy * filesystem, fall back to the slow path which uses readdir. 195*eda14cbcSMatt Macy */ 196*eda14cbcSMatt Macy if ((statfs64(dirname, &st) != 0) || 197*eda14cbcSMatt Macy (st.f_type != ZFS_SUPER_MAGIC)) { 198*eda14cbcSMatt Macy return (dir_is_empty_readdir(dirname)); 199*eda14cbcSMatt Macy } 200*eda14cbcSMatt Macy 201*eda14cbcSMatt Macy /* 202*eda14cbcSMatt Macy * At this point, we know the provided path is on a ZFS 203*eda14cbcSMatt Macy * filesystem, so we can use stat instead of readdir to 204*eda14cbcSMatt Macy * determine if the directory is empty or not. We try to avoid 205*eda14cbcSMatt Macy * using readdir because that requires opening "dirname"; this 206*eda14cbcSMatt Macy * open file descriptor can potentially end up in a child 207*eda14cbcSMatt Macy * process if there's a concurrent fork, thus preventing the 208*eda14cbcSMatt Macy * zfs_mount() from otherwise succeeding (the open file 209*eda14cbcSMatt Macy * descriptor inherited by the child process will cause the 210*eda14cbcSMatt Macy * parent's mount to fail with EBUSY). The performance 211*eda14cbcSMatt Macy * implications of replacing the open, read, and close with a 212*eda14cbcSMatt Macy * single stat is nice; but is not the main motivation for the 213*eda14cbcSMatt Macy * added complexity. 214*eda14cbcSMatt Macy */ 215*eda14cbcSMatt Macy return (dir_is_empty_stat(dirname)); 216*eda14cbcSMatt Macy } 217*eda14cbcSMatt Macy 218*eda14cbcSMatt Macy /* 219*eda14cbcSMatt Macy * Checks to see if the mount is active. If the filesystem is mounted, we fill 220*eda14cbcSMatt Macy * in 'where' with the current mountpoint, and return 1. Otherwise, we return 221*eda14cbcSMatt Macy * 0. 222*eda14cbcSMatt Macy */ 223*eda14cbcSMatt Macy boolean_t 224*eda14cbcSMatt Macy is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 225*eda14cbcSMatt Macy { 226*eda14cbcSMatt Macy struct mnttab entry; 227*eda14cbcSMatt Macy 228*eda14cbcSMatt Macy if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0) 229*eda14cbcSMatt Macy return (B_FALSE); 230*eda14cbcSMatt Macy 231*eda14cbcSMatt Macy if (where != NULL) 232*eda14cbcSMatt Macy *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 233*eda14cbcSMatt Macy 234*eda14cbcSMatt Macy return (B_TRUE); 235*eda14cbcSMatt Macy } 236*eda14cbcSMatt Macy 237*eda14cbcSMatt Macy boolean_t 238*eda14cbcSMatt Macy zfs_is_mounted(zfs_handle_t *zhp, char **where) 239*eda14cbcSMatt Macy { 240*eda14cbcSMatt Macy return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 241*eda14cbcSMatt Macy } 242*eda14cbcSMatt Macy 243*eda14cbcSMatt Macy /* 244*eda14cbcSMatt Macy * Checks any higher order concerns about whether the given dataset is 245*eda14cbcSMatt Macy * mountable, false otherwise. zfs_is_mountable_internal specifically assumes 246*eda14cbcSMatt Macy * that the caller has verified the sanity of mounting the dataset at 247*eda14cbcSMatt Macy * mountpoint to the extent the caller wants. 248*eda14cbcSMatt Macy */ 249*eda14cbcSMatt Macy static boolean_t 250*eda14cbcSMatt Macy zfs_is_mountable_internal(zfs_handle_t *zhp, const char *mountpoint) 251*eda14cbcSMatt Macy { 252*eda14cbcSMatt Macy 253*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 254*eda14cbcSMatt Macy getzoneid() == GLOBAL_ZONEID) 255*eda14cbcSMatt Macy return (B_FALSE); 256*eda14cbcSMatt Macy 257*eda14cbcSMatt Macy return (B_TRUE); 258*eda14cbcSMatt Macy } 259*eda14cbcSMatt Macy 260*eda14cbcSMatt Macy /* 261*eda14cbcSMatt Macy * Returns true if the given dataset is mountable, false otherwise. Returns the 262*eda14cbcSMatt Macy * mountpoint in 'buf'. 263*eda14cbcSMatt Macy */ 264*eda14cbcSMatt Macy boolean_t 265*eda14cbcSMatt Macy zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 266*eda14cbcSMatt Macy zprop_source_t *source, int flags) 267*eda14cbcSMatt Macy { 268*eda14cbcSMatt Macy char sourceloc[MAXNAMELEN]; 269*eda14cbcSMatt Macy zprop_source_t sourcetype; 270*eda14cbcSMatt Macy 271*eda14cbcSMatt Macy if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type, 272*eda14cbcSMatt Macy B_FALSE)) 273*eda14cbcSMatt Macy return (B_FALSE); 274*eda14cbcSMatt Macy 275*eda14cbcSMatt Macy verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 276*eda14cbcSMatt Macy &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 277*eda14cbcSMatt Macy 278*eda14cbcSMatt Macy if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 279*eda14cbcSMatt Macy strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 280*eda14cbcSMatt Macy return (B_FALSE); 281*eda14cbcSMatt Macy 282*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF) 283*eda14cbcSMatt Macy return (B_FALSE); 284*eda14cbcSMatt Macy 285*eda14cbcSMatt Macy if (!zfs_is_mountable_internal(zhp, buf)) 286*eda14cbcSMatt Macy return (B_FALSE); 287*eda14cbcSMatt Macy 288*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE)) 289*eda14cbcSMatt Macy return (B_FALSE); 290*eda14cbcSMatt Macy 291*eda14cbcSMatt Macy if (source) 292*eda14cbcSMatt Macy *source = sourcetype; 293*eda14cbcSMatt Macy 294*eda14cbcSMatt Macy return (B_TRUE); 295*eda14cbcSMatt Macy } 296*eda14cbcSMatt Macy 297*eda14cbcSMatt Macy /* 298*eda14cbcSMatt Macy * The filesystem is mounted by invoking the system mount utility rather 299*eda14cbcSMatt Macy * than by the system call mount(2). This ensures that the /etc/mtab 300*eda14cbcSMatt Macy * file is correctly locked for the update. Performing our own locking 301*eda14cbcSMatt Macy * and /etc/mtab update requires making an unsafe assumption about how 302*eda14cbcSMatt Macy * the mount utility performs its locking. Unfortunately, this also means 303*eda14cbcSMatt Macy * in the case of a mount failure we do not have the exact errno. We must 304*eda14cbcSMatt Macy * make due with return value from the mount process. 305*eda14cbcSMatt Macy * 306*eda14cbcSMatt Macy * In the long term a shared library called libmount is under development 307*eda14cbcSMatt Macy * which provides a common API to address the locking and errno issues. 308*eda14cbcSMatt Macy * Once the standard mount utility has been updated to use this library 309*eda14cbcSMatt Macy * we can add an autoconf check to conditionally use it. 310*eda14cbcSMatt Macy * 311*eda14cbcSMatt Macy * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html 312*eda14cbcSMatt Macy */ 313*eda14cbcSMatt Macy 314*eda14cbcSMatt Macy static int 315*eda14cbcSMatt Macy zfs_add_option(zfs_handle_t *zhp, char *options, int len, 316*eda14cbcSMatt Macy zfs_prop_t prop, char *on, char *off) 317*eda14cbcSMatt Macy { 318*eda14cbcSMatt Macy char *source; 319*eda14cbcSMatt Macy uint64_t value; 320*eda14cbcSMatt Macy 321*eda14cbcSMatt Macy /* Skip adding duplicate default options */ 322*eda14cbcSMatt Macy if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL)) 323*eda14cbcSMatt Macy return (0); 324*eda14cbcSMatt Macy 325*eda14cbcSMatt Macy /* 326*eda14cbcSMatt Macy * zfs_prop_get_int() is not used to ensure our mount options 327*eda14cbcSMatt Macy * are not influenced by the current /proc/self/mounts contents. 328*eda14cbcSMatt Macy */ 329*eda14cbcSMatt Macy value = getprop_uint64(zhp, prop, &source); 330*eda14cbcSMatt Macy 331*eda14cbcSMatt Macy (void) strlcat(options, ",", len); 332*eda14cbcSMatt Macy (void) strlcat(options, value ? on : off, len); 333*eda14cbcSMatt Macy 334*eda14cbcSMatt Macy return (0); 335*eda14cbcSMatt Macy } 336*eda14cbcSMatt Macy 337*eda14cbcSMatt Macy static int 338*eda14cbcSMatt Macy zfs_add_options(zfs_handle_t *zhp, char *options, int len) 339*eda14cbcSMatt Macy { 340*eda14cbcSMatt Macy int error = 0; 341*eda14cbcSMatt Macy 342*eda14cbcSMatt Macy error = zfs_add_option(zhp, options, len, 343*eda14cbcSMatt Macy ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME); 344*eda14cbcSMatt Macy /* 345*eda14cbcSMatt Macy * don't add relatime/strictatime when atime=off, otherwise strictatime 346*eda14cbcSMatt Macy * will force atime=on 347*eda14cbcSMatt Macy */ 348*eda14cbcSMatt Macy if (strstr(options, MNTOPT_NOATIME) == NULL) { 349*eda14cbcSMatt Macy error = zfs_add_option(zhp, options, len, 350*eda14cbcSMatt Macy ZFS_PROP_RELATIME, MNTOPT_RELATIME, MNTOPT_STRICTATIME); 351*eda14cbcSMatt Macy } 352*eda14cbcSMatt Macy error = error ? error : zfs_add_option(zhp, options, len, 353*eda14cbcSMatt Macy ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES); 354*eda14cbcSMatt Macy error = error ? error : zfs_add_option(zhp, options, len, 355*eda14cbcSMatt Macy ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC); 356*eda14cbcSMatt Macy error = error ? error : zfs_add_option(zhp, options, len, 357*eda14cbcSMatt Macy ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW); 358*eda14cbcSMatt Macy error = error ? error : zfs_add_option(zhp, options, len, 359*eda14cbcSMatt Macy ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID); 360*eda14cbcSMatt Macy error = error ? error : zfs_add_option(zhp, options, len, 361*eda14cbcSMatt Macy ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND); 362*eda14cbcSMatt Macy 363*eda14cbcSMatt Macy return (error); 364*eda14cbcSMatt Macy } 365*eda14cbcSMatt Macy 366*eda14cbcSMatt Macy int 367*eda14cbcSMatt Macy zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 368*eda14cbcSMatt Macy { 369*eda14cbcSMatt Macy char mountpoint[ZFS_MAXPROPLEN]; 370*eda14cbcSMatt Macy 371*eda14cbcSMatt Macy if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 372*eda14cbcSMatt Macy flags)) 373*eda14cbcSMatt Macy return (0); 374*eda14cbcSMatt Macy 375*eda14cbcSMatt Macy return (zfs_mount_at(zhp, options, flags, mountpoint)); 376*eda14cbcSMatt Macy } 377*eda14cbcSMatt Macy 378*eda14cbcSMatt Macy /* 379*eda14cbcSMatt Macy * Mount the given filesystem. 380*eda14cbcSMatt Macy */ 381*eda14cbcSMatt Macy int 382*eda14cbcSMatt Macy zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags, 383*eda14cbcSMatt Macy const char *mountpoint) 384*eda14cbcSMatt Macy { 385*eda14cbcSMatt Macy struct stat buf; 386*eda14cbcSMatt Macy char mntopts[MNT_LINE_MAX]; 387*eda14cbcSMatt Macy char overlay[ZFS_MAXPROPLEN]; 388*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zfs_hdl; 389*eda14cbcSMatt Macy uint64_t keystatus; 390*eda14cbcSMatt Macy int remount = 0, rc; 391*eda14cbcSMatt Macy 392*eda14cbcSMatt Macy if (options == NULL) { 393*eda14cbcSMatt Macy (void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts)); 394*eda14cbcSMatt Macy } else { 395*eda14cbcSMatt Macy (void) strlcpy(mntopts, options, sizeof (mntopts)); 396*eda14cbcSMatt Macy } 397*eda14cbcSMatt Macy 398*eda14cbcSMatt Macy if (strstr(mntopts, MNTOPT_REMOUNT) != NULL) 399*eda14cbcSMatt Macy remount = 1; 400*eda14cbcSMatt Macy 401*eda14cbcSMatt Macy /* Potentially duplicates some checks if invoked by zfs_mount(). */ 402*eda14cbcSMatt Macy if (!zfs_is_mountable_internal(zhp, mountpoint)) 403*eda14cbcSMatt Macy return (0); 404*eda14cbcSMatt Macy 405*eda14cbcSMatt Macy /* 406*eda14cbcSMatt Macy * If the pool is imported read-only then all mounts must be read-only 407*eda14cbcSMatt Macy */ 408*eda14cbcSMatt Macy if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL)) 409*eda14cbcSMatt Macy (void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts)); 410*eda14cbcSMatt Macy 411*eda14cbcSMatt Macy /* 412*eda14cbcSMatt Macy * Append default mount options which apply to the mount point. 413*eda14cbcSMatt Macy * This is done because under Linux (unlike Solaris) multiple mount 414*eda14cbcSMatt Macy * points may reference a single super block. This means that just 415*eda14cbcSMatt Macy * given a super block there is no back reference to update the per 416*eda14cbcSMatt Macy * mount point options. 417*eda14cbcSMatt Macy */ 418*eda14cbcSMatt Macy rc = zfs_add_options(zhp, mntopts, sizeof (mntopts)); 419*eda14cbcSMatt Macy if (rc) { 420*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 421*eda14cbcSMatt Macy "default options unavailable")); 422*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 423*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 424*eda14cbcSMatt Macy mountpoint)); 425*eda14cbcSMatt Macy } 426*eda14cbcSMatt Macy 427*eda14cbcSMatt Macy /* 428*eda14cbcSMatt Macy * If the filesystem is encrypted the key must be loaded in order to 429*eda14cbcSMatt Macy * mount. If the key isn't loaded, the MS_CRYPT flag decides whether 430*eda14cbcSMatt Macy * or not we attempt to load the keys. Note: we must call 431*eda14cbcSMatt Macy * zfs_refresh_properties() here since some callers of this function 432*eda14cbcSMatt Macy * (most notably zpool_enable_datasets()) may implicitly load our key 433*eda14cbcSMatt Macy * by loading the parent's key first. 434*eda14cbcSMatt Macy */ 435*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) { 436*eda14cbcSMatt Macy zfs_refresh_properties(zhp); 437*eda14cbcSMatt Macy keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS); 438*eda14cbcSMatt Macy 439*eda14cbcSMatt Macy /* 440*eda14cbcSMatt Macy * If the key is unavailable and MS_CRYPT is set give the 441*eda14cbcSMatt Macy * user a chance to enter the key. Otherwise just fail 442*eda14cbcSMatt Macy * immediately. 443*eda14cbcSMatt Macy */ 444*eda14cbcSMatt Macy if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) { 445*eda14cbcSMatt Macy if (flags & MS_CRYPT) { 446*eda14cbcSMatt Macy rc = zfs_crypto_load_key(zhp, B_FALSE, NULL); 447*eda14cbcSMatt Macy if (rc) 448*eda14cbcSMatt Macy return (rc); 449*eda14cbcSMatt Macy } else { 450*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 451*eda14cbcSMatt Macy "encryption key not loaded")); 452*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 453*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 454*eda14cbcSMatt Macy mountpoint)); 455*eda14cbcSMatt Macy } 456*eda14cbcSMatt Macy } 457*eda14cbcSMatt Macy 458*eda14cbcSMatt Macy } 459*eda14cbcSMatt Macy 460*eda14cbcSMatt Macy /* 461*eda14cbcSMatt Macy * Append zfsutil option so the mount helper allow the mount 462*eda14cbcSMatt Macy */ 463*eda14cbcSMatt Macy strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts)); 464*eda14cbcSMatt Macy 465*eda14cbcSMatt Macy /* Create the directory if it doesn't already exist */ 466*eda14cbcSMatt Macy if (lstat(mountpoint, &buf) != 0) { 467*eda14cbcSMatt Macy if (mkdirp(mountpoint, 0755) != 0) { 468*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 469*eda14cbcSMatt Macy "failed to create mountpoint: %s"), 470*eda14cbcSMatt Macy strerror(errno)); 471*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 472*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 473*eda14cbcSMatt Macy mountpoint)); 474*eda14cbcSMatt Macy } 475*eda14cbcSMatt Macy } 476*eda14cbcSMatt Macy 477*eda14cbcSMatt Macy /* 478*eda14cbcSMatt Macy * Overlay mounts are enabled by default but may be disabled 479*eda14cbcSMatt Macy * via the 'overlay' property. The -O flag remains for compatibility. 480*eda14cbcSMatt Macy */ 481*eda14cbcSMatt Macy if (!(flags & MS_OVERLAY)) { 482*eda14cbcSMatt Macy if (zfs_prop_get(zhp, ZFS_PROP_OVERLAY, overlay, 483*eda14cbcSMatt Macy sizeof (overlay), NULL, NULL, 0, B_FALSE) == 0) { 484*eda14cbcSMatt Macy if (strcmp(overlay, "on") == 0) { 485*eda14cbcSMatt Macy flags |= MS_OVERLAY; 486*eda14cbcSMatt Macy } 487*eda14cbcSMatt Macy } 488*eda14cbcSMatt Macy } 489*eda14cbcSMatt Macy 490*eda14cbcSMatt Macy /* 491*eda14cbcSMatt Macy * Determine if the mountpoint is empty. If so, refuse to perform the 492*eda14cbcSMatt Macy * mount. We don't perform this check if 'remount' is 493*eda14cbcSMatt Macy * specified or if overlay option (-O) is given 494*eda14cbcSMatt Macy */ 495*eda14cbcSMatt Macy if ((flags & MS_OVERLAY) == 0 && !remount && 496*eda14cbcSMatt Macy !dir_is_empty(mountpoint)) { 497*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 498*eda14cbcSMatt Macy "directory is not empty")); 499*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 500*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 501*eda14cbcSMatt Macy } 502*eda14cbcSMatt Macy 503*eda14cbcSMatt Macy /* perform the mount */ 504*eda14cbcSMatt Macy rc = do_mount(zhp, mountpoint, mntopts, flags); 505*eda14cbcSMatt Macy if (rc) { 506*eda14cbcSMatt Macy /* 507*eda14cbcSMatt Macy * Generic errors are nasty, but there are just way too many 508*eda14cbcSMatt Macy * from mount(), and they're well-understood. We pick a few 509*eda14cbcSMatt Macy * common ones to improve upon. 510*eda14cbcSMatt Macy */ 511*eda14cbcSMatt Macy if (rc == EBUSY) { 512*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 513*eda14cbcSMatt Macy "mountpoint or dataset is busy")); 514*eda14cbcSMatt Macy } else if (rc == EPERM) { 515*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 516*eda14cbcSMatt Macy "Insufficient privileges")); 517*eda14cbcSMatt Macy } else if (rc == ENOTSUP) { 518*eda14cbcSMatt Macy char buf[256]; 519*eda14cbcSMatt Macy int spa_version; 520*eda14cbcSMatt Macy 521*eda14cbcSMatt Macy VERIFY(zfs_spa_version(zhp, &spa_version) == 0); 522*eda14cbcSMatt Macy (void) snprintf(buf, sizeof (buf), 523*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "Can't mount a version %lld " 524*eda14cbcSMatt Macy "file system on a version %d pool. Pool must be" 525*eda14cbcSMatt Macy " upgraded to mount this file system."), 526*eda14cbcSMatt Macy (u_longlong_t)zfs_prop_get_int(zhp, 527*eda14cbcSMatt Macy ZFS_PROP_VERSION), spa_version); 528*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf)); 529*eda14cbcSMatt Macy } else { 530*eda14cbcSMatt Macy zfs_error_aux(hdl, strerror(rc)); 531*eda14cbcSMatt Macy } 532*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 533*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 534*eda14cbcSMatt Macy zhp->zfs_name)); 535*eda14cbcSMatt Macy } 536*eda14cbcSMatt Macy 537*eda14cbcSMatt Macy /* remove the mounted entry before re-adding on remount */ 538*eda14cbcSMatt Macy if (remount) 539*eda14cbcSMatt Macy libzfs_mnttab_remove(hdl, zhp->zfs_name); 540*eda14cbcSMatt Macy 541*eda14cbcSMatt Macy /* add the mounted entry into our cache */ 542*eda14cbcSMatt Macy libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts); 543*eda14cbcSMatt Macy return (0); 544*eda14cbcSMatt Macy } 545*eda14cbcSMatt Macy 546*eda14cbcSMatt Macy /* 547*eda14cbcSMatt Macy * Unmount a single filesystem. 548*eda14cbcSMatt Macy */ 549*eda14cbcSMatt Macy static int 550*eda14cbcSMatt Macy unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 551*eda14cbcSMatt Macy { 552*eda14cbcSMatt Macy int error; 553*eda14cbcSMatt Macy 554*eda14cbcSMatt Macy error = do_unmount(mountpoint, flags); 555*eda14cbcSMatt Macy if (error != 0) { 556*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 557*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 558*eda14cbcSMatt Macy mountpoint)); 559*eda14cbcSMatt Macy } 560*eda14cbcSMatt Macy 561*eda14cbcSMatt Macy return (0); 562*eda14cbcSMatt Macy } 563*eda14cbcSMatt Macy 564*eda14cbcSMatt Macy /* 565*eda14cbcSMatt Macy * Unmount the given filesystem. 566*eda14cbcSMatt Macy */ 567*eda14cbcSMatt Macy int 568*eda14cbcSMatt Macy zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 569*eda14cbcSMatt Macy { 570*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zfs_hdl; 571*eda14cbcSMatt Macy struct mnttab entry; 572*eda14cbcSMatt Macy char *mntpt = NULL; 573*eda14cbcSMatt Macy boolean_t encroot, unmounted = B_FALSE; 574*eda14cbcSMatt Macy 575*eda14cbcSMatt Macy /* check to see if we need to unmount the filesystem */ 576*eda14cbcSMatt Macy if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 577*eda14cbcSMatt Macy libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) { 578*eda14cbcSMatt Macy /* 579*eda14cbcSMatt Macy * mountpoint may have come from a call to 580*eda14cbcSMatt Macy * getmnt/getmntany if it isn't NULL. If it is NULL, 581*eda14cbcSMatt Macy * we know it comes from libzfs_mnttab_find which can 582*eda14cbcSMatt Macy * then get freed later. We strdup it to play it safe. 583*eda14cbcSMatt Macy */ 584*eda14cbcSMatt Macy if (mountpoint == NULL) 585*eda14cbcSMatt Macy mntpt = zfs_strdup(hdl, entry.mnt_mountp); 586*eda14cbcSMatt Macy else 587*eda14cbcSMatt Macy mntpt = zfs_strdup(hdl, mountpoint); 588*eda14cbcSMatt Macy 589*eda14cbcSMatt Macy /* 590*eda14cbcSMatt Macy * Unshare and unmount the filesystem 591*eda14cbcSMatt Macy */ 592*eda14cbcSMatt Macy if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) { 593*eda14cbcSMatt Macy free(mntpt); 594*eda14cbcSMatt Macy return (-1); 595*eda14cbcSMatt Macy } 596*eda14cbcSMatt Macy zfs_commit_all_shares(); 597*eda14cbcSMatt Macy 598*eda14cbcSMatt Macy if (unmount_one(hdl, mntpt, flags) != 0) { 599*eda14cbcSMatt Macy free(mntpt); 600*eda14cbcSMatt Macy (void) zfs_shareall(zhp); 601*eda14cbcSMatt Macy zfs_commit_all_shares(); 602*eda14cbcSMatt Macy return (-1); 603*eda14cbcSMatt Macy } 604*eda14cbcSMatt Macy 605*eda14cbcSMatt Macy libzfs_mnttab_remove(hdl, zhp->zfs_name); 606*eda14cbcSMatt Macy free(mntpt); 607*eda14cbcSMatt Macy unmounted = B_TRUE; 608*eda14cbcSMatt Macy } 609*eda14cbcSMatt Macy 610*eda14cbcSMatt Macy /* 611*eda14cbcSMatt Macy * If the MS_CRYPT flag is provided we must ensure we attempt to 612*eda14cbcSMatt Macy * unload the dataset's key regardless of whether we did any work 613*eda14cbcSMatt Macy * to unmount it. We only do this for encryption roots. 614*eda14cbcSMatt Macy */ 615*eda14cbcSMatt Macy if ((flags & MS_CRYPT) != 0 && 616*eda14cbcSMatt Macy zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) { 617*eda14cbcSMatt Macy zfs_refresh_properties(zhp); 618*eda14cbcSMatt Macy 619*eda14cbcSMatt Macy if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0 && 620*eda14cbcSMatt Macy unmounted) { 621*eda14cbcSMatt Macy (void) zfs_mount(zhp, NULL, 0); 622*eda14cbcSMatt Macy return (-1); 623*eda14cbcSMatt Macy } 624*eda14cbcSMatt Macy 625*eda14cbcSMatt Macy if (encroot && zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) == 626*eda14cbcSMatt Macy ZFS_KEYSTATUS_AVAILABLE && 627*eda14cbcSMatt Macy zfs_crypto_unload_key(zhp) != 0) { 628*eda14cbcSMatt Macy (void) zfs_mount(zhp, NULL, 0); 629*eda14cbcSMatt Macy return (-1); 630*eda14cbcSMatt Macy } 631*eda14cbcSMatt Macy } 632*eda14cbcSMatt Macy 633*eda14cbcSMatt Macy return (0); 634*eda14cbcSMatt Macy } 635*eda14cbcSMatt Macy 636*eda14cbcSMatt Macy /* 637*eda14cbcSMatt Macy * Unmount this filesystem and any children inheriting the mountpoint property. 638*eda14cbcSMatt Macy * To do this, just act like we're changing the mountpoint property, but don't 639*eda14cbcSMatt Macy * remount the filesystems afterwards. 640*eda14cbcSMatt Macy */ 641*eda14cbcSMatt Macy int 642*eda14cbcSMatt Macy zfs_unmountall(zfs_handle_t *zhp, int flags) 643*eda14cbcSMatt Macy { 644*eda14cbcSMatt Macy prop_changelist_t *clp; 645*eda14cbcSMatt Macy int ret; 646*eda14cbcSMatt Macy 647*eda14cbcSMatt Macy clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 648*eda14cbcSMatt Macy CL_GATHER_ITER_MOUNTED, flags); 649*eda14cbcSMatt Macy if (clp == NULL) 650*eda14cbcSMatt Macy return (-1); 651*eda14cbcSMatt Macy 652*eda14cbcSMatt Macy ret = changelist_prefix(clp); 653*eda14cbcSMatt Macy changelist_free(clp); 654*eda14cbcSMatt Macy 655*eda14cbcSMatt Macy return (ret); 656*eda14cbcSMatt Macy } 657*eda14cbcSMatt Macy 658*eda14cbcSMatt Macy boolean_t 659*eda14cbcSMatt Macy zfs_is_shared(zfs_handle_t *zhp) 660*eda14cbcSMatt Macy { 661*eda14cbcSMatt Macy zfs_share_type_t rc = 0; 662*eda14cbcSMatt Macy zfs_share_proto_t *curr_proto; 663*eda14cbcSMatt Macy 664*eda14cbcSMatt Macy if (ZFS_IS_VOLUME(zhp)) 665*eda14cbcSMatt Macy return (B_FALSE); 666*eda14cbcSMatt Macy 667*eda14cbcSMatt Macy for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 668*eda14cbcSMatt Macy curr_proto++) 669*eda14cbcSMatt Macy rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto); 670*eda14cbcSMatt Macy 671*eda14cbcSMatt Macy return (rc ? B_TRUE : B_FALSE); 672*eda14cbcSMatt Macy } 673*eda14cbcSMatt Macy 674*eda14cbcSMatt Macy /* 675*eda14cbcSMatt Macy * Unshare a filesystem by mountpoint. 676*eda14cbcSMatt Macy */ 677*eda14cbcSMatt Macy int 678*eda14cbcSMatt Macy unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, 679*eda14cbcSMatt Macy zfs_share_proto_t proto) 680*eda14cbcSMatt Macy { 681*eda14cbcSMatt Macy int err; 682*eda14cbcSMatt Macy 683*eda14cbcSMatt Macy err = sa_disable_share(mountpoint, proto_table[proto].p_name); 684*eda14cbcSMatt Macy if (err != SA_OK) { 685*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err, 686*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 687*eda14cbcSMatt Macy name, sa_errorstr(err))); 688*eda14cbcSMatt Macy } 689*eda14cbcSMatt Macy return (0); 690*eda14cbcSMatt Macy } 691*eda14cbcSMatt Macy 692*eda14cbcSMatt Macy /* 693*eda14cbcSMatt Macy * Query libshare for the given mountpoint and protocol, returning 694*eda14cbcSMatt Macy * a zfs_share_type_t value. 695*eda14cbcSMatt Macy */ 696*eda14cbcSMatt Macy zfs_share_type_t 697*eda14cbcSMatt Macy is_shared(const char *mountpoint, zfs_share_proto_t proto) 698*eda14cbcSMatt Macy { 699*eda14cbcSMatt Macy if (sa_is_shared(mountpoint, proto_table[proto].p_name)) { 700*eda14cbcSMatt Macy switch (proto) { 701*eda14cbcSMatt Macy case PROTO_NFS: 702*eda14cbcSMatt Macy return (SHARED_NFS); 703*eda14cbcSMatt Macy case PROTO_SMB: 704*eda14cbcSMatt Macy return (SHARED_SMB); 705*eda14cbcSMatt Macy default: 706*eda14cbcSMatt Macy return (SHARED_NOT_SHARED); 707*eda14cbcSMatt Macy } 708*eda14cbcSMatt Macy } 709*eda14cbcSMatt Macy return (SHARED_NOT_SHARED); 710*eda14cbcSMatt Macy } 711*eda14cbcSMatt Macy 712*eda14cbcSMatt Macy /* 713*eda14cbcSMatt Macy * Share the given filesystem according to the options in the specified 714*eda14cbcSMatt Macy * protocol specific properties (sharenfs, sharesmb). We rely 715*eda14cbcSMatt Macy * on "libshare" to do the dirty work for us. 716*eda14cbcSMatt Macy */ 717*eda14cbcSMatt Macy int 718*eda14cbcSMatt Macy zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 719*eda14cbcSMatt Macy { 720*eda14cbcSMatt Macy char mountpoint[ZFS_MAXPROPLEN]; 721*eda14cbcSMatt Macy char shareopts[ZFS_MAXPROPLEN]; 722*eda14cbcSMatt Macy char sourcestr[ZFS_MAXPROPLEN]; 723*eda14cbcSMatt Macy zfs_share_proto_t *curr_proto; 724*eda14cbcSMatt Macy zprop_source_t sourcetype; 725*eda14cbcSMatt Macy int err = 0; 726*eda14cbcSMatt Macy 727*eda14cbcSMatt Macy if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0)) 728*eda14cbcSMatt Macy return (0); 729*eda14cbcSMatt Macy 730*eda14cbcSMatt Macy for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 731*eda14cbcSMatt Macy /* 732*eda14cbcSMatt Macy * Return success if there are no share options. 733*eda14cbcSMatt Macy */ 734*eda14cbcSMatt Macy if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop, 735*eda14cbcSMatt Macy shareopts, sizeof (shareopts), &sourcetype, sourcestr, 736*eda14cbcSMatt Macy ZFS_MAXPROPLEN, B_FALSE) != 0 || 737*eda14cbcSMatt Macy strcmp(shareopts, "off") == 0) 738*eda14cbcSMatt Macy continue; 739*eda14cbcSMatt Macy 740*eda14cbcSMatt Macy /* 741*eda14cbcSMatt Macy * If the 'zoned' property is set, then zfs_is_mountable() 742*eda14cbcSMatt Macy * will have already bailed out if we are in the global zone. 743*eda14cbcSMatt Macy * But local zones cannot be NFS servers, so we ignore it for 744*eda14cbcSMatt Macy * local zones as well. 745*eda14cbcSMatt Macy */ 746*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 747*eda14cbcSMatt Macy continue; 748*eda14cbcSMatt Macy 749*eda14cbcSMatt Macy err = sa_enable_share(zfs_get_name(zhp), mountpoint, shareopts, 750*eda14cbcSMatt Macy proto_table[*curr_proto].p_name); 751*eda14cbcSMatt Macy if (err != SA_OK) { 752*eda14cbcSMatt Macy return (zfs_error_fmt(zhp->zfs_hdl, 753*eda14cbcSMatt Macy proto_table[*curr_proto].p_share_err, 754*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot share '%s: %s'"), 755*eda14cbcSMatt Macy zfs_get_name(zhp), sa_errorstr(err))); 756*eda14cbcSMatt Macy } 757*eda14cbcSMatt Macy 758*eda14cbcSMatt Macy } 759*eda14cbcSMatt Macy return (0); 760*eda14cbcSMatt Macy } 761*eda14cbcSMatt Macy 762*eda14cbcSMatt Macy int 763*eda14cbcSMatt Macy zfs_share(zfs_handle_t *zhp) 764*eda14cbcSMatt Macy { 765*eda14cbcSMatt Macy assert(!ZFS_IS_VOLUME(zhp)); 766*eda14cbcSMatt Macy return (zfs_share_proto(zhp, share_all_proto)); 767*eda14cbcSMatt Macy } 768*eda14cbcSMatt Macy 769*eda14cbcSMatt Macy int 770*eda14cbcSMatt Macy zfs_unshare(zfs_handle_t *zhp) 771*eda14cbcSMatt Macy { 772*eda14cbcSMatt Macy assert(!ZFS_IS_VOLUME(zhp)); 773*eda14cbcSMatt Macy return (zfs_unshareall(zhp)); 774*eda14cbcSMatt Macy } 775*eda14cbcSMatt Macy 776*eda14cbcSMatt Macy /* 777*eda14cbcSMatt Macy * Check to see if the filesystem is currently shared. 778*eda14cbcSMatt Macy */ 779*eda14cbcSMatt Macy zfs_share_type_t 780*eda14cbcSMatt Macy zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto) 781*eda14cbcSMatt Macy { 782*eda14cbcSMatt Macy char *mountpoint; 783*eda14cbcSMatt Macy zfs_share_type_t rc; 784*eda14cbcSMatt Macy 785*eda14cbcSMatt Macy if (!zfs_is_mounted(zhp, &mountpoint)) 786*eda14cbcSMatt Macy return (SHARED_NOT_SHARED); 787*eda14cbcSMatt Macy 788*eda14cbcSMatt Macy if ((rc = is_shared(mountpoint, proto)) 789*eda14cbcSMatt Macy != SHARED_NOT_SHARED) { 790*eda14cbcSMatt Macy if (where != NULL) 791*eda14cbcSMatt Macy *where = mountpoint; 792*eda14cbcSMatt Macy else 793*eda14cbcSMatt Macy free(mountpoint); 794*eda14cbcSMatt Macy return (rc); 795*eda14cbcSMatt Macy } else { 796*eda14cbcSMatt Macy free(mountpoint); 797*eda14cbcSMatt Macy return (SHARED_NOT_SHARED); 798*eda14cbcSMatt Macy } 799*eda14cbcSMatt Macy } 800*eda14cbcSMatt Macy 801*eda14cbcSMatt Macy boolean_t 802*eda14cbcSMatt Macy zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 803*eda14cbcSMatt Macy { 804*eda14cbcSMatt Macy return (zfs_is_shared_proto(zhp, where, 805*eda14cbcSMatt Macy PROTO_NFS) != SHARED_NOT_SHARED); 806*eda14cbcSMatt Macy } 807*eda14cbcSMatt Macy 808*eda14cbcSMatt Macy boolean_t 809*eda14cbcSMatt Macy zfs_is_shared_smb(zfs_handle_t *zhp, char **where) 810*eda14cbcSMatt Macy { 811*eda14cbcSMatt Macy return (zfs_is_shared_proto(zhp, where, 812*eda14cbcSMatt Macy PROTO_SMB) != SHARED_NOT_SHARED); 813*eda14cbcSMatt Macy } 814*eda14cbcSMatt Macy 815*eda14cbcSMatt Macy /* 816*eda14cbcSMatt Macy * zfs_parse_options(options, proto) 817*eda14cbcSMatt Macy * 818*eda14cbcSMatt Macy * Call the legacy parse interface to get the protocol specific 819*eda14cbcSMatt Macy * options using the NULL arg to indicate that this is a "parse" only. 820*eda14cbcSMatt Macy */ 821*eda14cbcSMatt Macy int 822*eda14cbcSMatt Macy zfs_parse_options(char *options, zfs_share_proto_t proto) 823*eda14cbcSMatt Macy { 824*eda14cbcSMatt Macy return (sa_validate_shareopts(options, proto_table[proto].p_name)); 825*eda14cbcSMatt Macy } 826*eda14cbcSMatt Macy 827*eda14cbcSMatt Macy void 828*eda14cbcSMatt Macy zfs_commit_proto(zfs_share_proto_t *proto) 829*eda14cbcSMatt Macy { 830*eda14cbcSMatt Macy zfs_share_proto_t *curr_proto; 831*eda14cbcSMatt Macy for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 832*eda14cbcSMatt Macy sa_commit_shares(proto_table[*curr_proto].p_name); 833*eda14cbcSMatt Macy } 834*eda14cbcSMatt Macy } 835*eda14cbcSMatt Macy 836*eda14cbcSMatt Macy void 837*eda14cbcSMatt Macy zfs_commit_nfs_shares(void) 838*eda14cbcSMatt Macy { 839*eda14cbcSMatt Macy zfs_commit_proto(nfs_only); 840*eda14cbcSMatt Macy } 841*eda14cbcSMatt Macy 842*eda14cbcSMatt Macy void 843*eda14cbcSMatt Macy zfs_commit_smb_shares(void) 844*eda14cbcSMatt Macy { 845*eda14cbcSMatt Macy zfs_commit_proto(smb_only); 846*eda14cbcSMatt Macy } 847*eda14cbcSMatt Macy 848*eda14cbcSMatt Macy void 849*eda14cbcSMatt Macy zfs_commit_all_shares(void) 850*eda14cbcSMatt Macy { 851*eda14cbcSMatt Macy zfs_commit_proto(share_all_proto); 852*eda14cbcSMatt Macy } 853*eda14cbcSMatt Macy 854*eda14cbcSMatt Macy void 855*eda14cbcSMatt Macy zfs_commit_shares(const char *proto) 856*eda14cbcSMatt Macy { 857*eda14cbcSMatt Macy if (proto == NULL) 858*eda14cbcSMatt Macy zfs_commit_proto(share_all_proto); 859*eda14cbcSMatt Macy else if (strcmp(proto, "nfs") == 0) 860*eda14cbcSMatt Macy zfs_commit_proto(nfs_only); 861*eda14cbcSMatt Macy else if (strcmp(proto, "smb") == 0) 862*eda14cbcSMatt Macy zfs_commit_proto(smb_only); 863*eda14cbcSMatt Macy } 864*eda14cbcSMatt Macy 865*eda14cbcSMatt Macy int 866*eda14cbcSMatt Macy zfs_share_nfs(zfs_handle_t *zhp) 867*eda14cbcSMatt Macy { 868*eda14cbcSMatt Macy return (zfs_share_proto(zhp, nfs_only)); 869*eda14cbcSMatt Macy } 870*eda14cbcSMatt Macy 871*eda14cbcSMatt Macy int 872*eda14cbcSMatt Macy zfs_share_smb(zfs_handle_t *zhp) 873*eda14cbcSMatt Macy { 874*eda14cbcSMatt Macy return (zfs_share_proto(zhp, smb_only)); 875*eda14cbcSMatt Macy } 876*eda14cbcSMatt Macy 877*eda14cbcSMatt Macy int 878*eda14cbcSMatt Macy zfs_shareall(zfs_handle_t *zhp) 879*eda14cbcSMatt Macy { 880*eda14cbcSMatt Macy return (zfs_share_proto(zhp, share_all_proto)); 881*eda14cbcSMatt Macy } 882*eda14cbcSMatt Macy 883*eda14cbcSMatt Macy /* 884*eda14cbcSMatt Macy * Unshare the given filesystem. 885*eda14cbcSMatt Macy */ 886*eda14cbcSMatt Macy int 887*eda14cbcSMatt Macy zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, 888*eda14cbcSMatt Macy zfs_share_proto_t *proto) 889*eda14cbcSMatt Macy { 890*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zfs_hdl; 891*eda14cbcSMatt Macy struct mnttab entry; 892*eda14cbcSMatt Macy char *mntpt = NULL; 893*eda14cbcSMatt Macy 894*eda14cbcSMatt Macy /* check to see if need to unmount the filesystem */ 895*eda14cbcSMatt Macy if (mountpoint != NULL) 896*eda14cbcSMatt Macy mntpt = zfs_strdup(hdl, mountpoint); 897*eda14cbcSMatt Macy 898*eda14cbcSMatt Macy if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 899*eda14cbcSMatt Macy libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) { 900*eda14cbcSMatt Macy zfs_share_proto_t *curr_proto; 901*eda14cbcSMatt Macy 902*eda14cbcSMatt Macy if (mountpoint == NULL) 903*eda14cbcSMatt Macy mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 904*eda14cbcSMatt Macy 905*eda14cbcSMatt Macy for (curr_proto = proto; *curr_proto != PROTO_END; 906*eda14cbcSMatt Macy curr_proto++) { 907*eda14cbcSMatt Macy 908*eda14cbcSMatt Macy if (is_shared(mntpt, *curr_proto)) { 909*eda14cbcSMatt Macy if (unshare_one(hdl, zhp->zfs_name, 910*eda14cbcSMatt Macy mntpt, *curr_proto) != 0) { 911*eda14cbcSMatt Macy if (mntpt != NULL) 912*eda14cbcSMatt Macy free(mntpt); 913*eda14cbcSMatt Macy return (-1); 914*eda14cbcSMatt Macy } 915*eda14cbcSMatt Macy } 916*eda14cbcSMatt Macy } 917*eda14cbcSMatt Macy } 918*eda14cbcSMatt Macy if (mntpt != NULL) 919*eda14cbcSMatt Macy free(mntpt); 920*eda14cbcSMatt Macy 921*eda14cbcSMatt Macy return (0); 922*eda14cbcSMatt Macy } 923*eda14cbcSMatt Macy 924*eda14cbcSMatt Macy int 925*eda14cbcSMatt Macy zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 926*eda14cbcSMatt Macy { 927*eda14cbcSMatt Macy return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 928*eda14cbcSMatt Macy } 929*eda14cbcSMatt Macy 930*eda14cbcSMatt Macy int 931*eda14cbcSMatt Macy zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint) 932*eda14cbcSMatt Macy { 933*eda14cbcSMatt Macy return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 934*eda14cbcSMatt Macy } 935*eda14cbcSMatt Macy 936*eda14cbcSMatt Macy /* 937*eda14cbcSMatt Macy * Same as zfs_unmountall(), but for NFS and SMB unshares. 938*eda14cbcSMatt Macy */ 939*eda14cbcSMatt Macy static int 940*eda14cbcSMatt Macy zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 941*eda14cbcSMatt Macy { 942*eda14cbcSMatt Macy prop_changelist_t *clp; 943*eda14cbcSMatt Macy int ret; 944*eda14cbcSMatt Macy 945*eda14cbcSMatt Macy clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0); 946*eda14cbcSMatt Macy if (clp == NULL) 947*eda14cbcSMatt Macy return (-1); 948*eda14cbcSMatt Macy 949*eda14cbcSMatt Macy ret = changelist_unshare(clp, proto); 950*eda14cbcSMatt Macy changelist_free(clp); 951*eda14cbcSMatt Macy 952*eda14cbcSMatt Macy return (ret); 953*eda14cbcSMatt Macy } 954*eda14cbcSMatt Macy 955*eda14cbcSMatt Macy int 956*eda14cbcSMatt Macy zfs_unshareall_nfs(zfs_handle_t *zhp) 957*eda14cbcSMatt Macy { 958*eda14cbcSMatt Macy return (zfs_unshareall_proto(zhp, nfs_only)); 959*eda14cbcSMatt Macy } 960*eda14cbcSMatt Macy 961*eda14cbcSMatt Macy int 962*eda14cbcSMatt Macy zfs_unshareall_smb(zfs_handle_t *zhp) 963*eda14cbcSMatt Macy { 964*eda14cbcSMatt Macy return (zfs_unshareall_proto(zhp, smb_only)); 965*eda14cbcSMatt Macy } 966*eda14cbcSMatt Macy 967*eda14cbcSMatt Macy int 968*eda14cbcSMatt Macy zfs_unshareall(zfs_handle_t *zhp) 969*eda14cbcSMatt Macy { 970*eda14cbcSMatt Macy return (zfs_unshareall_proto(zhp, share_all_proto)); 971*eda14cbcSMatt Macy } 972*eda14cbcSMatt Macy 973*eda14cbcSMatt Macy int 974*eda14cbcSMatt Macy zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint) 975*eda14cbcSMatt Macy { 976*eda14cbcSMatt Macy return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 977*eda14cbcSMatt Macy } 978*eda14cbcSMatt Macy 979*eda14cbcSMatt Macy int 980*eda14cbcSMatt Macy zfs_unshareall_bytype(zfs_handle_t *zhp, const char *mountpoint, 981*eda14cbcSMatt Macy const char *proto) 982*eda14cbcSMatt Macy { 983*eda14cbcSMatt Macy if (proto == NULL) 984*eda14cbcSMatt Macy return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 985*eda14cbcSMatt Macy if (strcmp(proto, "nfs") == 0) 986*eda14cbcSMatt Macy return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 987*eda14cbcSMatt Macy else if (strcmp(proto, "smb") == 0) 988*eda14cbcSMatt Macy return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 989*eda14cbcSMatt Macy else 990*eda14cbcSMatt Macy return (1); 991*eda14cbcSMatt Macy } 992*eda14cbcSMatt Macy 993*eda14cbcSMatt Macy /* 994*eda14cbcSMatt Macy * Remove the mountpoint associated with the current dataset, if necessary. 995*eda14cbcSMatt Macy * We only remove the underlying directory if: 996*eda14cbcSMatt Macy * 997*eda14cbcSMatt Macy * - The mountpoint is not 'none' or 'legacy' 998*eda14cbcSMatt Macy * - The mountpoint is non-empty 999*eda14cbcSMatt Macy * - The mountpoint is the default or inherited 1000*eda14cbcSMatt Macy * - The 'zoned' property is set, or we're in a local zone 1001*eda14cbcSMatt Macy * 1002*eda14cbcSMatt Macy * Any other directories we leave alone. 1003*eda14cbcSMatt Macy */ 1004*eda14cbcSMatt Macy void 1005*eda14cbcSMatt Macy remove_mountpoint(zfs_handle_t *zhp) 1006*eda14cbcSMatt Macy { 1007*eda14cbcSMatt Macy char mountpoint[ZFS_MAXPROPLEN]; 1008*eda14cbcSMatt Macy zprop_source_t source; 1009*eda14cbcSMatt Macy 1010*eda14cbcSMatt Macy if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 1011*eda14cbcSMatt Macy &source, 0)) 1012*eda14cbcSMatt Macy return; 1013*eda14cbcSMatt Macy 1014*eda14cbcSMatt Macy if (source == ZPROP_SRC_DEFAULT || 1015*eda14cbcSMatt Macy source == ZPROP_SRC_INHERITED) { 1016*eda14cbcSMatt Macy /* 1017*eda14cbcSMatt Macy * Try to remove the directory, silently ignoring any errors. 1018*eda14cbcSMatt Macy * The filesystem may have since been removed or moved around, 1019*eda14cbcSMatt Macy * and this error isn't really useful to the administrator in 1020*eda14cbcSMatt Macy * any way. 1021*eda14cbcSMatt Macy */ 1022*eda14cbcSMatt Macy (void) rmdir(mountpoint); 1023*eda14cbcSMatt Macy } 1024*eda14cbcSMatt Macy } 1025*eda14cbcSMatt Macy 1026*eda14cbcSMatt Macy /* 1027*eda14cbcSMatt Macy * Add the given zfs handle to the cb_handles array, dynamically reallocating 1028*eda14cbcSMatt Macy * the array if it is out of space. 1029*eda14cbcSMatt Macy */ 1030*eda14cbcSMatt Macy void 1031*eda14cbcSMatt Macy libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp) 1032*eda14cbcSMatt Macy { 1033*eda14cbcSMatt Macy if (cbp->cb_alloc == cbp->cb_used) { 1034*eda14cbcSMatt Macy size_t newsz; 1035*eda14cbcSMatt Macy zfs_handle_t **newhandles; 1036*eda14cbcSMatt Macy 1037*eda14cbcSMatt Macy newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64; 1038*eda14cbcSMatt Macy newhandles = zfs_realloc(zhp->zfs_hdl, 1039*eda14cbcSMatt Macy cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *), 1040*eda14cbcSMatt Macy newsz * sizeof (zfs_handle_t *)); 1041*eda14cbcSMatt Macy cbp->cb_handles = newhandles; 1042*eda14cbcSMatt Macy cbp->cb_alloc = newsz; 1043*eda14cbcSMatt Macy } 1044*eda14cbcSMatt Macy cbp->cb_handles[cbp->cb_used++] = zhp; 1045*eda14cbcSMatt Macy } 1046*eda14cbcSMatt Macy 1047*eda14cbcSMatt Macy /* 1048*eda14cbcSMatt Macy * Recursive helper function used during file system enumeration 1049*eda14cbcSMatt Macy */ 1050*eda14cbcSMatt Macy static int 1051*eda14cbcSMatt Macy zfs_iter_cb(zfs_handle_t *zhp, void *data) 1052*eda14cbcSMatt Macy { 1053*eda14cbcSMatt Macy get_all_cb_t *cbp = data; 1054*eda14cbcSMatt Macy 1055*eda14cbcSMatt Macy if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) { 1056*eda14cbcSMatt Macy zfs_close(zhp); 1057*eda14cbcSMatt Macy return (0); 1058*eda14cbcSMatt Macy } 1059*eda14cbcSMatt Macy 1060*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) { 1061*eda14cbcSMatt Macy zfs_close(zhp); 1062*eda14cbcSMatt Macy return (0); 1063*eda14cbcSMatt Macy } 1064*eda14cbcSMatt Macy 1065*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) == 1066*eda14cbcSMatt Macy ZFS_KEYSTATUS_UNAVAILABLE) { 1067*eda14cbcSMatt Macy zfs_close(zhp); 1068*eda14cbcSMatt Macy return (0); 1069*eda14cbcSMatt Macy } 1070*eda14cbcSMatt Macy 1071*eda14cbcSMatt Macy /* 1072*eda14cbcSMatt Macy * If this filesystem is inconsistent and has a receive resume 1073*eda14cbcSMatt Macy * token, we can not mount it. 1074*eda14cbcSMatt Macy */ 1075*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) && 1076*eda14cbcSMatt Macy zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 1077*eda14cbcSMatt Macy NULL, 0, NULL, NULL, 0, B_TRUE) == 0) { 1078*eda14cbcSMatt Macy zfs_close(zhp); 1079*eda14cbcSMatt Macy return (0); 1080*eda14cbcSMatt Macy } 1081*eda14cbcSMatt Macy 1082*eda14cbcSMatt Macy libzfs_add_handle(cbp, zhp); 1083*eda14cbcSMatt Macy if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) { 1084*eda14cbcSMatt Macy zfs_close(zhp); 1085*eda14cbcSMatt Macy return (-1); 1086*eda14cbcSMatt Macy } 1087*eda14cbcSMatt Macy return (0); 1088*eda14cbcSMatt Macy } 1089*eda14cbcSMatt Macy 1090*eda14cbcSMatt Macy /* 1091*eda14cbcSMatt Macy * Sort comparator that compares two mountpoint paths. We sort these paths so 1092*eda14cbcSMatt Macy * that subdirectories immediately follow their parents. This means that we 1093*eda14cbcSMatt Macy * effectively treat the '/' character as the lowest value non-nul char. 1094*eda14cbcSMatt Macy * Since filesystems from non-global zones can have the same mountpoint 1095*eda14cbcSMatt Macy * as other filesystems, the comparator sorts global zone filesystems to 1096*eda14cbcSMatt Macy * the top of the list. This means that the global zone will traverse the 1097*eda14cbcSMatt Macy * filesystem list in the correct order and can stop when it sees the 1098*eda14cbcSMatt Macy * first zoned filesystem. In a non-global zone, only the delegated 1099*eda14cbcSMatt Macy * filesystems are seen. 1100*eda14cbcSMatt Macy * 1101*eda14cbcSMatt Macy * An example sorted list using this comparator would look like: 1102*eda14cbcSMatt Macy * 1103*eda14cbcSMatt Macy * /foo 1104*eda14cbcSMatt Macy * /foo/bar 1105*eda14cbcSMatt Macy * /foo/bar/baz 1106*eda14cbcSMatt Macy * /foo/baz 1107*eda14cbcSMatt Macy * /foo.bar 1108*eda14cbcSMatt Macy * /foo (NGZ1) 1109*eda14cbcSMatt Macy * /foo (NGZ2) 1110*eda14cbcSMatt Macy * 1111*eda14cbcSMatt Macy * The mounting code depends on this ordering to deterministically iterate 1112*eda14cbcSMatt Macy * over filesystems in order to spawn parallel mount tasks. 1113*eda14cbcSMatt Macy */ 1114*eda14cbcSMatt Macy static int 1115*eda14cbcSMatt Macy mountpoint_cmp(const void *arga, const void *argb) 1116*eda14cbcSMatt Macy { 1117*eda14cbcSMatt Macy zfs_handle_t *const *zap = arga; 1118*eda14cbcSMatt Macy zfs_handle_t *za = *zap; 1119*eda14cbcSMatt Macy zfs_handle_t *const *zbp = argb; 1120*eda14cbcSMatt Macy zfs_handle_t *zb = *zbp; 1121*eda14cbcSMatt Macy char mounta[MAXPATHLEN]; 1122*eda14cbcSMatt Macy char mountb[MAXPATHLEN]; 1123*eda14cbcSMatt Macy const char *a = mounta; 1124*eda14cbcSMatt Macy const char *b = mountb; 1125*eda14cbcSMatt Macy boolean_t gota, gotb; 1126*eda14cbcSMatt Macy uint64_t zoneda, zonedb; 1127*eda14cbcSMatt Macy 1128*eda14cbcSMatt Macy zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED); 1129*eda14cbcSMatt Macy zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED); 1130*eda14cbcSMatt Macy if (zoneda && !zonedb) 1131*eda14cbcSMatt Macy return (1); 1132*eda14cbcSMatt Macy if (!zoneda && zonedb) 1133*eda14cbcSMatt Macy return (-1); 1134*eda14cbcSMatt Macy 1135*eda14cbcSMatt Macy gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM); 1136*eda14cbcSMatt Macy if (gota) { 1137*eda14cbcSMatt Macy verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta, 1138*eda14cbcSMatt Macy sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 1139*eda14cbcSMatt Macy } 1140*eda14cbcSMatt Macy gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM); 1141*eda14cbcSMatt Macy if (gotb) { 1142*eda14cbcSMatt Macy verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb, 1143*eda14cbcSMatt Macy sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 1144*eda14cbcSMatt Macy } 1145*eda14cbcSMatt Macy 1146*eda14cbcSMatt Macy if (gota && gotb) { 1147*eda14cbcSMatt Macy while (*a != '\0' && (*a == *b)) { 1148*eda14cbcSMatt Macy a++; 1149*eda14cbcSMatt Macy b++; 1150*eda14cbcSMatt Macy } 1151*eda14cbcSMatt Macy if (*a == *b) 1152*eda14cbcSMatt Macy return (0); 1153*eda14cbcSMatt Macy if (*a == '\0') 1154*eda14cbcSMatt Macy return (-1); 1155*eda14cbcSMatt Macy if (*b == '\0') 1156*eda14cbcSMatt Macy return (1); 1157*eda14cbcSMatt Macy if (*a == '/') 1158*eda14cbcSMatt Macy return (-1); 1159*eda14cbcSMatt Macy if (*b == '/') 1160*eda14cbcSMatt Macy return (1); 1161*eda14cbcSMatt Macy return (*a < *b ? -1 : *a > *b); 1162*eda14cbcSMatt Macy } 1163*eda14cbcSMatt Macy 1164*eda14cbcSMatt Macy if (gota) 1165*eda14cbcSMatt Macy return (-1); 1166*eda14cbcSMatt Macy if (gotb) 1167*eda14cbcSMatt Macy return (1); 1168*eda14cbcSMatt Macy 1169*eda14cbcSMatt Macy /* 1170*eda14cbcSMatt Macy * If neither filesystem has a mountpoint, revert to sorting by 1171*eda14cbcSMatt Macy * dataset name. 1172*eda14cbcSMatt Macy */ 1173*eda14cbcSMatt Macy return (strcmp(zfs_get_name(za), zfs_get_name(zb))); 1174*eda14cbcSMatt Macy } 1175*eda14cbcSMatt Macy 1176*eda14cbcSMatt Macy /* 1177*eda14cbcSMatt Macy * Return true if path2 is a child of path1 or path2 equals path1 or 1178*eda14cbcSMatt Macy * path1 is "/" (path2 is always a child of "/"). 1179*eda14cbcSMatt Macy */ 1180*eda14cbcSMatt Macy static boolean_t 1181*eda14cbcSMatt Macy libzfs_path_contains(const char *path1, const char *path2) 1182*eda14cbcSMatt Macy { 1183*eda14cbcSMatt Macy return (strcmp(path1, path2) == 0 || strcmp(path1, "/") == 0 || 1184*eda14cbcSMatt Macy (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/')); 1185*eda14cbcSMatt Macy } 1186*eda14cbcSMatt Macy 1187*eda14cbcSMatt Macy /* 1188*eda14cbcSMatt Macy * Given a mountpoint specified by idx in the handles array, find the first 1189*eda14cbcSMatt Macy * non-descendent of that mountpoint and return its index. Descendant paths 1190*eda14cbcSMatt Macy * start with the parent's path. This function relies on the ordering 1191*eda14cbcSMatt Macy * enforced by mountpoint_cmp(). 1192*eda14cbcSMatt Macy */ 1193*eda14cbcSMatt Macy static int 1194*eda14cbcSMatt Macy non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx) 1195*eda14cbcSMatt Macy { 1196*eda14cbcSMatt Macy char parent[ZFS_MAXPROPLEN]; 1197*eda14cbcSMatt Macy char child[ZFS_MAXPROPLEN]; 1198*eda14cbcSMatt Macy int i; 1199*eda14cbcSMatt Macy 1200*eda14cbcSMatt Macy verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent, 1201*eda14cbcSMatt Macy sizeof (parent), NULL, NULL, 0, B_FALSE) == 0); 1202*eda14cbcSMatt Macy 1203*eda14cbcSMatt Macy for (i = idx + 1; i < num_handles; i++) { 1204*eda14cbcSMatt Macy verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child, 1205*eda14cbcSMatt Macy sizeof (child), NULL, NULL, 0, B_FALSE) == 0); 1206*eda14cbcSMatt Macy if (!libzfs_path_contains(parent, child)) 1207*eda14cbcSMatt Macy break; 1208*eda14cbcSMatt Macy } 1209*eda14cbcSMatt Macy return (i); 1210*eda14cbcSMatt Macy } 1211*eda14cbcSMatt Macy 1212*eda14cbcSMatt Macy typedef struct mnt_param { 1213*eda14cbcSMatt Macy libzfs_handle_t *mnt_hdl; 1214*eda14cbcSMatt Macy tpool_t *mnt_tp; 1215*eda14cbcSMatt Macy zfs_handle_t **mnt_zhps; /* filesystems to mount */ 1216*eda14cbcSMatt Macy size_t mnt_num_handles; 1217*eda14cbcSMatt Macy int mnt_idx; /* Index of selected entry to mount */ 1218*eda14cbcSMatt Macy zfs_iter_f mnt_func; 1219*eda14cbcSMatt Macy void *mnt_data; 1220*eda14cbcSMatt Macy } mnt_param_t; 1221*eda14cbcSMatt Macy 1222*eda14cbcSMatt Macy /* 1223*eda14cbcSMatt Macy * Allocate and populate the parameter struct for mount function, and 1224*eda14cbcSMatt Macy * schedule mounting of the entry selected by idx. 1225*eda14cbcSMatt Macy */ 1226*eda14cbcSMatt Macy static void 1227*eda14cbcSMatt Macy zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles, 1228*eda14cbcSMatt Macy size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp) 1229*eda14cbcSMatt Macy { 1230*eda14cbcSMatt Macy mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t)); 1231*eda14cbcSMatt Macy 1232*eda14cbcSMatt Macy mnt_param->mnt_hdl = hdl; 1233*eda14cbcSMatt Macy mnt_param->mnt_tp = tp; 1234*eda14cbcSMatt Macy mnt_param->mnt_zhps = handles; 1235*eda14cbcSMatt Macy mnt_param->mnt_num_handles = num_handles; 1236*eda14cbcSMatt Macy mnt_param->mnt_idx = idx; 1237*eda14cbcSMatt Macy mnt_param->mnt_func = func; 1238*eda14cbcSMatt Macy mnt_param->mnt_data = data; 1239*eda14cbcSMatt Macy 1240*eda14cbcSMatt Macy (void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param); 1241*eda14cbcSMatt Macy } 1242*eda14cbcSMatt Macy 1243*eda14cbcSMatt Macy /* 1244*eda14cbcSMatt Macy * This is the structure used to keep state of mounting or sharing operations 1245*eda14cbcSMatt Macy * during a call to zpool_enable_datasets(). 1246*eda14cbcSMatt Macy */ 1247*eda14cbcSMatt Macy typedef struct mount_state { 1248*eda14cbcSMatt Macy /* 1249*eda14cbcSMatt Macy * ms_mntstatus is set to -1 if any mount fails. While multiple threads 1250*eda14cbcSMatt Macy * could update this variable concurrently, no synchronization is 1251*eda14cbcSMatt Macy * needed as it's only ever set to -1. 1252*eda14cbcSMatt Macy */ 1253*eda14cbcSMatt Macy int ms_mntstatus; 1254*eda14cbcSMatt Macy int ms_mntflags; 1255*eda14cbcSMatt Macy const char *ms_mntopts; 1256*eda14cbcSMatt Macy } mount_state_t; 1257*eda14cbcSMatt Macy 1258*eda14cbcSMatt Macy static int 1259*eda14cbcSMatt Macy zfs_mount_one(zfs_handle_t *zhp, void *arg) 1260*eda14cbcSMatt Macy { 1261*eda14cbcSMatt Macy mount_state_t *ms = arg; 1262*eda14cbcSMatt Macy int ret = 0; 1263*eda14cbcSMatt Macy 1264*eda14cbcSMatt Macy /* 1265*eda14cbcSMatt Macy * don't attempt to mount encrypted datasets with 1266*eda14cbcSMatt Macy * unloaded keys 1267*eda14cbcSMatt Macy */ 1268*eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) == 1269*eda14cbcSMatt Macy ZFS_KEYSTATUS_UNAVAILABLE) 1270*eda14cbcSMatt Macy return (0); 1271*eda14cbcSMatt Macy 1272*eda14cbcSMatt Macy if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0) 1273*eda14cbcSMatt Macy ret = ms->ms_mntstatus = -1; 1274*eda14cbcSMatt Macy return (ret); 1275*eda14cbcSMatt Macy } 1276*eda14cbcSMatt Macy 1277*eda14cbcSMatt Macy static int 1278*eda14cbcSMatt Macy zfs_share_one(zfs_handle_t *zhp, void *arg) 1279*eda14cbcSMatt Macy { 1280*eda14cbcSMatt Macy mount_state_t *ms = arg; 1281*eda14cbcSMatt Macy int ret = 0; 1282*eda14cbcSMatt Macy 1283*eda14cbcSMatt Macy if (zfs_share(zhp) != 0) 1284*eda14cbcSMatt Macy ret = ms->ms_mntstatus = -1; 1285*eda14cbcSMatt Macy return (ret); 1286*eda14cbcSMatt Macy } 1287*eda14cbcSMatt Macy 1288*eda14cbcSMatt Macy /* 1289*eda14cbcSMatt Macy * Thread pool function to mount one file system. On completion, it finds and 1290*eda14cbcSMatt Macy * schedules its children to be mounted. This depends on the sorting done in 1291*eda14cbcSMatt Macy * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries 1292*eda14cbcSMatt Macy * each descending from the previous) will have no parallelism since we always 1293*eda14cbcSMatt Macy * have to wait for the parent to finish mounting before we can schedule 1294*eda14cbcSMatt Macy * its children. 1295*eda14cbcSMatt Macy */ 1296*eda14cbcSMatt Macy static void 1297*eda14cbcSMatt Macy zfs_mount_task(void *arg) 1298*eda14cbcSMatt Macy { 1299*eda14cbcSMatt Macy mnt_param_t *mp = arg; 1300*eda14cbcSMatt Macy int idx = mp->mnt_idx; 1301*eda14cbcSMatt Macy zfs_handle_t **handles = mp->mnt_zhps; 1302*eda14cbcSMatt Macy size_t num_handles = mp->mnt_num_handles; 1303*eda14cbcSMatt Macy char mountpoint[ZFS_MAXPROPLEN]; 1304*eda14cbcSMatt Macy 1305*eda14cbcSMatt Macy verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint, 1306*eda14cbcSMatt Macy sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 1307*eda14cbcSMatt Macy 1308*eda14cbcSMatt Macy if (mp->mnt_func(handles[idx], mp->mnt_data) != 0) 1309*eda14cbcSMatt Macy return; 1310*eda14cbcSMatt Macy 1311*eda14cbcSMatt Macy /* 1312*eda14cbcSMatt Macy * We dispatch tasks to mount filesystems with mountpoints underneath 1313*eda14cbcSMatt Macy * this one. We do this by dispatching the next filesystem with a 1314*eda14cbcSMatt Macy * descendant mountpoint of the one we just mounted, then skip all of 1315*eda14cbcSMatt Macy * its descendants, dispatch the next descendant mountpoint, and so on. 1316*eda14cbcSMatt Macy * The non_descendant_idx() function skips over filesystems that are 1317*eda14cbcSMatt Macy * descendants of the filesystem we just dispatched. 1318*eda14cbcSMatt Macy */ 1319*eda14cbcSMatt Macy for (int i = idx + 1; i < num_handles; 1320*eda14cbcSMatt Macy i = non_descendant_idx(handles, num_handles, i)) { 1321*eda14cbcSMatt Macy char child[ZFS_MAXPROPLEN]; 1322*eda14cbcSMatt Macy verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, 1323*eda14cbcSMatt Macy child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0); 1324*eda14cbcSMatt Macy 1325*eda14cbcSMatt Macy if (!libzfs_path_contains(mountpoint, child)) 1326*eda14cbcSMatt Macy break; /* not a descendant, return */ 1327*eda14cbcSMatt Macy zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i, 1328*eda14cbcSMatt Macy mp->mnt_func, mp->mnt_data, mp->mnt_tp); 1329*eda14cbcSMatt Macy } 1330*eda14cbcSMatt Macy free(mp); 1331*eda14cbcSMatt Macy } 1332*eda14cbcSMatt Macy 1333*eda14cbcSMatt Macy /* 1334*eda14cbcSMatt Macy * Issue the func callback for each ZFS handle contained in the handles 1335*eda14cbcSMatt Macy * array. This function is used to mount all datasets, and so this function 1336*eda14cbcSMatt Macy * guarantees that filesystems for parent mountpoints are called before their 1337*eda14cbcSMatt Macy * children. As such, before issuing any callbacks, we first sort the array 1338*eda14cbcSMatt Macy * of handles by mountpoint. 1339*eda14cbcSMatt Macy * 1340*eda14cbcSMatt Macy * Callbacks are issued in one of two ways: 1341*eda14cbcSMatt Macy * 1342*eda14cbcSMatt Macy * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT 1343*eda14cbcSMatt Macy * environment variable is set, then we issue callbacks sequentially. 1344*eda14cbcSMatt Macy * 1345*eda14cbcSMatt Macy * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT 1346*eda14cbcSMatt Macy * environment variable is not set, then we use a tpool to dispatch threads 1347*eda14cbcSMatt Macy * to mount filesystems in parallel. This function dispatches tasks to mount 1348*eda14cbcSMatt Macy * the filesystems at the top-level mountpoints, and these tasks in turn 1349*eda14cbcSMatt Macy * are responsible for recursively mounting filesystems in their children 1350*eda14cbcSMatt Macy * mountpoints. 1351*eda14cbcSMatt Macy */ 1352*eda14cbcSMatt Macy void 1353*eda14cbcSMatt Macy zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles, 1354*eda14cbcSMatt Macy size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel) 1355*eda14cbcSMatt Macy { 1356*eda14cbcSMatt Macy zoneid_t zoneid = getzoneid(); 1357*eda14cbcSMatt Macy 1358*eda14cbcSMatt Macy /* 1359*eda14cbcSMatt Macy * The ZFS_SERIAL_MOUNT environment variable is an undocumented 1360*eda14cbcSMatt Macy * variable that can be used as a convenience to do a/b comparison 1361*eda14cbcSMatt Macy * of serial vs. parallel mounting. 1362*eda14cbcSMatt Macy */ 1363*eda14cbcSMatt Macy boolean_t serial_mount = !parallel || 1364*eda14cbcSMatt Macy (getenv("ZFS_SERIAL_MOUNT") != NULL); 1365*eda14cbcSMatt Macy 1366*eda14cbcSMatt Macy /* 1367*eda14cbcSMatt Macy * Sort the datasets by mountpoint. See mountpoint_cmp for details 1368*eda14cbcSMatt Macy * of how these are sorted. 1369*eda14cbcSMatt Macy */ 1370*eda14cbcSMatt Macy qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp); 1371*eda14cbcSMatt Macy 1372*eda14cbcSMatt Macy if (serial_mount) { 1373*eda14cbcSMatt Macy for (int i = 0; i < num_handles; i++) { 1374*eda14cbcSMatt Macy func(handles[i], data); 1375*eda14cbcSMatt Macy } 1376*eda14cbcSMatt Macy return; 1377*eda14cbcSMatt Macy } 1378*eda14cbcSMatt Macy 1379*eda14cbcSMatt Macy /* 1380*eda14cbcSMatt Macy * Issue the callback function for each dataset using a parallel 1381*eda14cbcSMatt Macy * algorithm that uses a thread pool to manage threads. 1382*eda14cbcSMatt Macy */ 1383*eda14cbcSMatt Macy tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL); 1384*eda14cbcSMatt Macy 1385*eda14cbcSMatt Macy /* 1386*eda14cbcSMatt Macy * There may be multiple "top level" mountpoints outside of the pool's 1387*eda14cbcSMatt Macy * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of 1388*eda14cbcSMatt Macy * these. 1389*eda14cbcSMatt Macy */ 1390*eda14cbcSMatt Macy for (int i = 0; i < num_handles; 1391*eda14cbcSMatt Macy i = non_descendant_idx(handles, num_handles, i)) { 1392*eda14cbcSMatt Macy /* 1393*eda14cbcSMatt Macy * Since the mountpoints have been sorted so that the zoned 1394*eda14cbcSMatt Macy * filesystems are at the end, a zoned filesystem seen from 1395*eda14cbcSMatt Macy * the global zone means that we're done. 1396*eda14cbcSMatt Macy */ 1397*eda14cbcSMatt Macy if (zoneid == GLOBAL_ZONEID && 1398*eda14cbcSMatt Macy zfs_prop_get_int(handles[i], ZFS_PROP_ZONED)) 1399*eda14cbcSMatt Macy break; 1400*eda14cbcSMatt Macy zfs_dispatch_mount(hdl, handles, num_handles, i, func, data, 1401*eda14cbcSMatt Macy tp); 1402*eda14cbcSMatt Macy } 1403*eda14cbcSMatt Macy 1404*eda14cbcSMatt Macy tpool_wait(tp); /* wait for all scheduled mounts to complete */ 1405*eda14cbcSMatt Macy tpool_destroy(tp); 1406*eda14cbcSMatt Macy } 1407*eda14cbcSMatt Macy 1408*eda14cbcSMatt Macy /* 1409*eda14cbcSMatt Macy * Mount and share all datasets within the given pool. This assumes that no 1410*eda14cbcSMatt Macy * datasets within the pool are currently mounted. 1411*eda14cbcSMatt Macy */ 1412*eda14cbcSMatt Macy #pragma weak zpool_mount_datasets = zpool_enable_datasets 1413*eda14cbcSMatt Macy int 1414*eda14cbcSMatt Macy zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 1415*eda14cbcSMatt Macy { 1416*eda14cbcSMatt Macy get_all_cb_t cb = { 0 }; 1417*eda14cbcSMatt Macy mount_state_t ms = { 0 }; 1418*eda14cbcSMatt Macy zfs_handle_t *zfsp; 1419*eda14cbcSMatt Macy int ret = 0; 1420*eda14cbcSMatt Macy 1421*eda14cbcSMatt Macy if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 1422*eda14cbcSMatt Macy ZFS_TYPE_DATASET)) == NULL) 1423*eda14cbcSMatt Macy goto out; 1424*eda14cbcSMatt Macy 1425*eda14cbcSMatt Macy /* 1426*eda14cbcSMatt Macy * Gather all non-snapshot datasets within the pool. Start by adding 1427*eda14cbcSMatt Macy * the root filesystem for this pool to the list, and then iterate 1428*eda14cbcSMatt Macy * over all child filesystems. 1429*eda14cbcSMatt Macy */ 1430*eda14cbcSMatt Macy libzfs_add_handle(&cb, zfsp); 1431*eda14cbcSMatt Macy if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0) 1432*eda14cbcSMatt Macy goto out; 1433*eda14cbcSMatt Macy 1434*eda14cbcSMatt Macy /* 1435*eda14cbcSMatt Macy * Mount all filesystems 1436*eda14cbcSMatt Macy */ 1437*eda14cbcSMatt Macy ms.ms_mntopts = mntopts; 1438*eda14cbcSMatt Macy ms.ms_mntflags = flags; 1439*eda14cbcSMatt Macy zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used, 1440*eda14cbcSMatt Macy zfs_mount_one, &ms, B_TRUE); 1441*eda14cbcSMatt Macy if (ms.ms_mntstatus != 0) 1442*eda14cbcSMatt Macy ret = ms.ms_mntstatus; 1443*eda14cbcSMatt Macy 1444*eda14cbcSMatt Macy /* 1445*eda14cbcSMatt Macy * Share all filesystems that need to be shared. This needs to be 1446*eda14cbcSMatt Macy * a separate pass because libshare is not mt-safe, and so we need 1447*eda14cbcSMatt Macy * to share serially. 1448*eda14cbcSMatt Macy */ 1449*eda14cbcSMatt Macy ms.ms_mntstatus = 0; 1450*eda14cbcSMatt Macy zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used, 1451*eda14cbcSMatt Macy zfs_share_one, &ms, B_FALSE); 1452*eda14cbcSMatt Macy if (ms.ms_mntstatus != 0) 1453*eda14cbcSMatt Macy ret = ms.ms_mntstatus; 1454*eda14cbcSMatt Macy else 1455*eda14cbcSMatt Macy zfs_commit_all_shares(); 1456*eda14cbcSMatt Macy 1457*eda14cbcSMatt Macy out: 1458*eda14cbcSMatt Macy for (int i = 0; i < cb.cb_used; i++) 1459*eda14cbcSMatt Macy zfs_close(cb.cb_handles[i]); 1460*eda14cbcSMatt Macy free(cb.cb_handles); 1461*eda14cbcSMatt Macy 1462*eda14cbcSMatt Macy return (ret); 1463*eda14cbcSMatt Macy } 1464*eda14cbcSMatt Macy 1465*eda14cbcSMatt Macy static int 1466*eda14cbcSMatt Macy mountpoint_compare(const void *a, const void *b) 1467*eda14cbcSMatt Macy { 1468*eda14cbcSMatt Macy const char *mounta = *((char **)a); 1469*eda14cbcSMatt Macy const char *mountb = *((char **)b); 1470*eda14cbcSMatt Macy 1471*eda14cbcSMatt Macy return (strcmp(mountb, mounta)); 1472*eda14cbcSMatt Macy } 1473*eda14cbcSMatt Macy 1474*eda14cbcSMatt Macy /* alias for 2002/240 */ 1475*eda14cbcSMatt Macy #pragma weak zpool_unmount_datasets = zpool_disable_datasets 1476*eda14cbcSMatt Macy /* 1477*eda14cbcSMatt Macy * Unshare and unmount all datasets within the given pool. We don't want to 1478*eda14cbcSMatt Macy * rely on traversing the DSL to discover the filesystems within the pool, 1479*eda14cbcSMatt Macy * because this may be expensive (if not all of them are mounted), and can fail 1480*eda14cbcSMatt Macy * arbitrarily (on I/O error, for example). Instead, we walk /proc/self/mounts 1481*eda14cbcSMatt Macy * and gather all the filesystems that are currently mounted. 1482*eda14cbcSMatt Macy */ 1483*eda14cbcSMatt Macy int 1484*eda14cbcSMatt Macy zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 1485*eda14cbcSMatt Macy { 1486*eda14cbcSMatt Macy int used, alloc; 1487*eda14cbcSMatt Macy struct mnttab entry; 1488*eda14cbcSMatt Macy size_t namelen; 1489*eda14cbcSMatt Macy char **mountpoints = NULL; 1490*eda14cbcSMatt Macy zfs_handle_t **datasets = NULL; 1491*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 1492*eda14cbcSMatt Macy int i; 1493*eda14cbcSMatt Macy int ret = -1; 1494*eda14cbcSMatt Macy int flags = (force ? MS_FORCE : 0); 1495*eda14cbcSMatt Macy 1496*eda14cbcSMatt Macy namelen = strlen(zhp->zpool_name); 1497*eda14cbcSMatt Macy 1498*eda14cbcSMatt Macy /* Reopen MNTTAB to prevent reading stale data from open file */ 1499*eda14cbcSMatt Macy if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL) 1500*eda14cbcSMatt Macy return (ENOENT); 1501*eda14cbcSMatt Macy 1502*eda14cbcSMatt Macy used = alloc = 0; 1503*eda14cbcSMatt Macy while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 1504*eda14cbcSMatt Macy /* 1505*eda14cbcSMatt Macy * Ignore non-ZFS entries. 1506*eda14cbcSMatt Macy */ 1507*eda14cbcSMatt Macy if (entry.mnt_fstype == NULL || 1508*eda14cbcSMatt Macy strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 1509*eda14cbcSMatt Macy continue; 1510*eda14cbcSMatt Macy 1511*eda14cbcSMatt Macy /* 1512*eda14cbcSMatt Macy * Ignore filesystems not within this pool. 1513*eda14cbcSMatt Macy */ 1514*eda14cbcSMatt Macy if (entry.mnt_mountp == NULL || 1515*eda14cbcSMatt Macy strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 1516*eda14cbcSMatt Macy (entry.mnt_special[namelen] != '/' && 1517*eda14cbcSMatt Macy entry.mnt_special[namelen] != '\0')) 1518*eda14cbcSMatt Macy continue; 1519*eda14cbcSMatt Macy 1520*eda14cbcSMatt Macy /* 1521*eda14cbcSMatt Macy * At this point we've found a filesystem within our pool. Add 1522*eda14cbcSMatt Macy * it to our growing list. 1523*eda14cbcSMatt Macy */ 1524*eda14cbcSMatt Macy if (used == alloc) { 1525*eda14cbcSMatt Macy if (alloc == 0) { 1526*eda14cbcSMatt Macy if ((mountpoints = zfs_alloc(hdl, 1527*eda14cbcSMatt Macy 8 * sizeof (void *))) == NULL) 1528*eda14cbcSMatt Macy goto out; 1529*eda14cbcSMatt Macy 1530*eda14cbcSMatt Macy if ((datasets = zfs_alloc(hdl, 1531*eda14cbcSMatt Macy 8 * sizeof (void *))) == NULL) 1532*eda14cbcSMatt Macy goto out; 1533*eda14cbcSMatt Macy 1534*eda14cbcSMatt Macy alloc = 8; 1535*eda14cbcSMatt Macy } else { 1536*eda14cbcSMatt Macy void *ptr; 1537*eda14cbcSMatt Macy 1538*eda14cbcSMatt Macy if ((ptr = zfs_realloc(hdl, mountpoints, 1539*eda14cbcSMatt Macy alloc * sizeof (void *), 1540*eda14cbcSMatt Macy alloc * 2 * sizeof (void *))) == NULL) 1541*eda14cbcSMatt Macy goto out; 1542*eda14cbcSMatt Macy mountpoints = ptr; 1543*eda14cbcSMatt Macy 1544*eda14cbcSMatt Macy if ((ptr = zfs_realloc(hdl, datasets, 1545*eda14cbcSMatt Macy alloc * sizeof (void *), 1546*eda14cbcSMatt Macy alloc * 2 * sizeof (void *))) == NULL) 1547*eda14cbcSMatt Macy goto out; 1548*eda14cbcSMatt Macy datasets = ptr; 1549*eda14cbcSMatt Macy 1550*eda14cbcSMatt Macy alloc *= 2; 1551*eda14cbcSMatt Macy } 1552*eda14cbcSMatt Macy } 1553*eda14cbcSMatt Macy 1554*eda14cbcSMatt Macy if ((mountpoints[used] = zfs_strdup(hdl, 1555*eda14cbcSMatt Macy entry.mnt_mountp)) == NULL) 1556*eda14cbcSMatt Macy goto out; 1557*eda14cbcSMatt Macy 1558*eda14cbcSMatt Macy /* 1559*eda14cbcSMatt Macy * This is allowed to fail, in case there is some I/O error. It 1560*eda14cbcSMatt Macy * is only used to determine if we need to remove the underlying 1561*eda14cbcSMatt Macy * mountpoint, so failure is not fatal. 1562*eda14cbcSMatt Macy */ 1563*eda14cbcSMatt Macy datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 1564*eda14cbcSMatt Macy 1565*eda14cbcSMatt Macy used++; 1566*eda14cbcSMatt Macy } 1567*eda14cbcSMatt Macy 1568*eda14cbcSMatt Macy /* 1569*eda14cbcSMatt Macy * At this point, we have the entire list of filesystems, so sort it by 1570*eda14cbcSMatt Macy * mountpoint. 1571*eda14cbcSMatt Macy */ 1572*eda14cbcSMatt Macy qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 1573*eda14cbcSMatt Macy 1574*eda14cbcSMatt Macy /* 1575*eda14cbcSMatt Macy * Walk through and first unshare everything. 1576*eda14cbcSMatt Macy */ 1577*eda14cbcSMatt Macy for (i = 0; i < used; i++) { 1578*eda14cbcSMatt Macy zfs_share_proto_t *curr_proto; 1579*eda14cbcSMatt Macy for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 1580*eda14cbcSMatt Macy curr_proto++) { 1581*eda14cbcSMatt Macy if (is_shared(mountpoints[i], *curr_proto) && 1582*eda14cbcSMatt Macy unshare_one(hdl, mountpoints[i], 1583*eda14cbcSMatt Macy mountpoints[i], *curr_proto) != 0) 1584*eda14cbcSMatt Macy goto out; 1585*eda14cbcSMatt Macy } 1586*eda14cbcSMatt Macy } 1587*eda14cbcSMatt Macy zfs_commit_all_shares(); 1588*eda14cbcSMatt Macy 1589*eda14cbcSMatt Macy /* 1590*eda14cbcSMatt Macy * Now unmount everything, removing the underlying directories as 1591*eda14cbcSMatt Macy * appropriate. 1592*eda14cbcSMatt Macy */ 1593*eda14cbcSMatt Macy for (i = 0; i < used; i++) { 1594*eda14cbcSMatt Macy if (unmount_one(hdl, mountpoints[i], flags) != 0) 1595*eda14cbcSMatt Macy goto out; 1596*eda14cbcSMatt Macy } 1597*eda14cbcSMatt Macy 1598*eda14cbcSMatt Macy for (i = 0; i < used; i++) { 1599*eda14cbcSMatt Macy if (datasets[i]) 1600*eda14cbcSMatt Macy remove_mountpoint(datasets[i]); 1601*eda14cbcSMatt Macy } 1602*eda14cbcSMatt Macy 1603*eda14cbcSMatt Macy ret = 0; 1604*eda14cbcSMatt Macy out: 1605*eda14cbcSMatt Macy for (i = 0; i < used; i++) { 1606*eda14cbcSMatt Macy if (datasets[i]) 1607*eda14cbcSMatt Macy zfs_close(datasets[i]); 1608*eda14cbcSMatt Macy free(mountpoints[i]); 1609*eda14cbcSMatt Macy } 1610*eda14cbcSMatt Macy free(datasets); 1611*eda14cbcSMatt Macy free(mountpoints); 1612*eda14cbcSMatt Macy 1613*eda14cbcSMatt Macy return (ret); 1614*eda14cbcSMatt Macy } 1615