1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21f3861e1aSahl 22fa9e4066Sahrens /* 23d1672efbSMarcel Telka * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 24dc7cd546SMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2509c9e6dcSChris Williamson * Copyright (c) 2014, 2016 by Delphix. All rights reserved. 2688f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 27*4f4378ccSAndrew Stormont * Copyright 2017 RackTop Systems. 28fa9e4066Sahrens */ 29fa9e4066Sahrens 30fa9e4066Sahrens /* 31fa9e4066Sahrens * Routines to manage ZFS mounts. We separate all the nasty routines that have 32f3861e1aSahl * to deal with the OS. The following functions are the main entry points -- 33f3861e1aSahl * they are used by mount and unmount and when changing a filesystem's 34f3861e1aSahl * mountpoint. 35fa9e4066Sahrens * 36fa9e4066Sahrens * zfs_is_mounted() 37fa9e4066Sahrens * zfs_mount() 38fa9e4066Sahrens * zfs_unmount() 39fa9e4066Sahrens * zfs_unmountall() 40fa9e4066Sahrens * 41f3861e1aSahl * This file also contains the functions used to manage sharing filesystems via 42f3861e1aSahl * NFS and iSCSI: 43fa9e4066Sahrens * 44fa9e4066Sahrens * zfs_is_shared() 45fa9e4066Sahrens * zfs_share() 46fa9e4066Sahrens * zfs_unshare() 47f3861e1aSahl * 48f3861e1aSahl * zfs_is_shared_nfs() 49da6c28aaSamw * zfs_is_shared_smb() 50da6c28aaSamw * zfs_share_proto() 51da6c28aaSamw * zfs_shareall(); 52da6c28aaSamw * zfs_unshare_nfs() 53da6c28aaSamw * zfs_unshare_smb() 54da6c28aaSamw * zfs_unshareall_nfs() 55da6c28aaSamw * zfs_unshareall_smb() 56da6c28aaSamw * zfs_unshareall() 57da6c28aaSamw * zfs_unshareall_bypath() 583bb79becSeschrock * 593bb79becSeschrock * The following functions are available for pool consumers, and will 60f3861e1aSahl * mount/unmount and share/unshare all datasets within pool: 613bb79becSeschrock * 62f3861e1aSahl * zpool_enable_datasets() 63f3861e1aSahl * zpool_disable_datasets() 64fa9e4066Sahrens */ 65fa9e4066Sahrens 66fa9e4066Sahrens #include <dirent.h> 67d8d59944Sahl #include <dlfcn.h> 68fa9e4066Sahrens #include <errno.h> 69d420209dSAlex Reece #include <fcntl.h> 70fa9e4066Sahrens #include <libgen.h> 71fa9e4066Sahrens #include <libintl.h> 72fa9e4066Sahrens #include <stdio.h> 73fa9e4066Sahrens #include <stdlib.h> 74fa9e4066Sahrens #include <strings.h> 75fa9e4066Sahrens #include <unistd.h> 76fa9e4066Sahrens #include <zone.h> 77fa9e4066Sahrens #include <sys/mntent.h> 78fa9e4066Sahrens #include <sys/mount.h> 79fa9e4066Sahrens #include <sys/stat.h> 80873c4903SPrakash Surya #include <sys/statvfs.h> 81fa9e4066Sahrens 82fa9e4066Sahrens #include <libzfs.h> 83fa9e4066Sahrens 84fa9e4066Sahrens #include "libzfs_impl.h" 85fa9e4066Sahrens 8667331909Sdougm #include <libshare.h> 8767331909Sdougm #include <sys/systeminfo.h> 8867331909Sdougm #define MAXISALEN 257 /* based on sysinfo(2) man page */ 8967331909Sdougm 90da6c28aaSamw static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *); 91da6c28aaSamw zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **, 92da6c28aaSamw zfs_share_proto_t); 93da6c28aaSamw 94da6c28aaSamw /* 95*4f4378ccSAndrew Stormont * The share protocols table must be in the same order as the zfs_share_proto_t 96da6c28aaSamw * enum in libzfs_impl.h 97da6c28aaSamw */ 98da6c28aaSamw typedef struct { 99da6c28aaSamw zfs_prop_t p_prop; 100da6c28aaSamw char *p_name; 101da6c28aaSamw int p_share_err; 102da6c28aaSamw int p_unshare_err; 103da6c28aaSamw } proto_table_t; 104da6c28aaSamw 105da6c28aaSamw proto_table_t proto_table[PROTO_END] = { 106da6c28aaSamw {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED}, 107da6c28aaSamw {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED}, 108da6c28aaSamw }; 109da6c28aaSamw 110da6c28aaSamw zfs_share_proto_t nfs_only[] = { 111da6c28aaSamw PROTO_NFS, 112da6c28aaSamw PROTO_END 113da6c28aaSamw }; 114da6c28aaSamw 115da6c28aaSamw zfs_share_proto_t smb_only[] = { 116da6c28aaSamw PROTO_SMB, 117da6c28aaSamw PROTO_END 118da6c28aaSamw }; 119da6c28aaSamw zfs_share_proto_t share_all_proto[] = { 120da6c28aaSamw PROTO_NFS, 121da6c28aaSamw PROTO_SMB, 122da6c28aaSamw PROTO_END 123da6c28aaSamw }; 124da6c28aaSamw 125fa9e4066Sahrens /* 126da6c28aaSamw * Search the sharetab for the given mountpoint and protocol, returning 127da6c28aaSamw * a zfs_share_type_t value. 128fa9e4066Sahrens */ 129da6c28aaSamw static zfs_share_type_t 130da6c28aaSamw is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto) 131fa9e4066Sahrens { 132fa9e4066Sahrens char buf[MAXPATHLEN], *tab; 133da6c28aaSamw char *ptr; 134fa9e4066Sahrens 13599653d4eSeschrock if (hdl->libzfs_sharetab == NULL) 136da6c28aaSamw return (SHARED_NOT_SHARED); 137fa9e4066Sahrens 13899653d4eSeschrock (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET); 139fa9e4066Sahrens 14099653d4eSeschrock while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) { 141fa9e4066Sahrens 142fa9e4066Sahrens /* the mountpoint is the first entry on each line */ 143da6c28aaSamw if ((tab = strchr(buf, '\t')) == NULL) 144da6c28aaSamw continue; 145da6c28aaSamw 146da6c28aaSamw *tab = '\0'; 147da6c28aaSamw if (strcmp(buf, mountpoint) == 0) { 148da6c28aaSamw /* 149da6c28aaSamw * the protocol field is the third field 150da6c28aaSamw * skip over second field 151da6c28aaSamw */ 152da6c28aaSamw ptr = ++tab; 153da6c28aaSamw if ((tab = strchr(ptr, '\t')) == NULL) 154da6c28aaSamw continue; 155da6c28aaSamw ptr = ++tab; 156da6c28aaSamw if ((tab = strchr(ptr, '\t')) == NULL) 157da6c28aaSamw continue; 158fa9e4066Sahrens *tab = '\0'; 159da6c28aaSamw if (strcmp(ptr, 160da6c28aaSamw proto_table[proto].p_name) == 0) { 161da6c28aaSamw switch (proto) { 162da6c28aaSamw case PROTO_NFS: 163da6c28aaSamw return (SHARED_NFS); 164da6c28aaSamw case PROTO_SMB: 165da6c28aaSamw return (SHARED_SMB); 166da6c28aaSamw default: 167da6c28aaSamw return (0); 168da6c28aaSamw } 169da6c28aaSamw } 170fa9e4066Sahrens } 171fa9e4066Sahrens } 172fa9e4066Sahrens 173da6c28aaSamw return (SHARED_NOT_SHARED); 174fa9e4066Sahrens } 175fa9e4066Sahrens 17699653d4eSeschrock static boolean_t 177873c4903SPrakash Surya dir_is_empty_stat(const char *dirname) 178873c4903SPrakash Surya { 179873c4903SPrakash Surya struct stat st; 180873c4903SPrakash Surya 181873c4903SPrakash Surya /* 182873c4903SPrakash Surya * We only want to return false if the given path is a non empty 183873c4903SPrakash Surya * directory, all other errors are handled elsewhere. 184873c4903SPrakash Surya */ 185873c4903SPrakash Surya if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) { 186873c4903SPrakash Surya return (B_TRUE); 187873c4903SPrakash Surya } 188873c4903SPrakash Surya 189873c4903SPrakash Surya /* 190873c4903SPrakash Surya * An empty directory will still have two entries in it, one 191873c4903SPrakash Surya * entry for each of "." and "..". 192873c4903SPrakash Surya */ 193873c4903SPrakash Surya if (st.st_size > 2) { 194873c4903SPrakash Surya return (B_FALSE); 195873c4903SPrakash Surya } 196873c4903SPrakash Surya 197873c4903SPrakash Surya return (B_TRUE); 198873c4903SPrakash Surya } 199873c4903SPrakash Surya 200873c4903SPrakash Surya static boolean_t 201873c4903SPrakash Surya dir_is_empty_readdir(const char *dirname) 202fa9e4066Sahrens { 203fa9e4066Sahrens DIR *dirp; 204fa9e4066Sahrens struct dirent64 *dp; 205d420209dSAlex Reece int dirfd; 206fa9e4066Sahrens 207d420209dSAlex Reece if ((dirfd = openat(AT_FDCWD, dirname, 208d420209dSAlex Reece O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) { 20999653d4eSeschrock return (B_TRUE); 210d420209dSAlex Reece } 211d420209dSAlex Reece 212d420209dSAlex Reece if ((dirp = fdopendir(dirfd)) == NULL) { 213d420209dSAlex Reece return (B_TRUE); 214d420209dSAlex Reece } 215fa9e4066Sahrens 216fa9e4066Sahrens while ((dp = readdir64(dirp)) != NULL) { 217fa9e4066Sahrens 218fa9e4066Sahrens if (strcmp(dp->d_name, ".") == 0 || 219fa9e4066Sahrens strcmp(dp->d_name, "..") == 0) 220fa9e4066Sahrens continue; 221fa9e4066Sahrens 222fa9e4066Sahrens (void) closedir(dirp); 22399653d4eSeschrock return (B_FALSE); 224fa9e4066Sahrens } 225fa9e4066Sahrens 226fa9e4066Sahrens (void) closedir(dirp); 22799653d4eSeschrock return (B_TRUE); 228fa9e4066Sahrens } 229fa9e4066Sahrens 230873c4903SPrakash Surya /* 231873c4903SPrakash Surya * Returns true if the specified directory is empty. If we can't open the 232873c4903SPrakash Surya * directory at all, return true so that the mount can fail with a more 233873c4903SPrakash Surya * informative error message. 234873c4903SPrakash Surya */ 235873c4903SPrakash Surya static boolean_t 236873c4903SPrakash Surya dir_is_empty(const char *dirname) 237873c4903SPrakash Surya { 238873c4903SPrakash Surya struct statvfs64 st; 239873c4903SPrakash Surya 240873c4903SPrakash Surya /* 241873c4903SPrakash Surya * If the statvfs call fails or the filesystem is not a ZFS 242873c4903SPrakash Surya * filesystem, fall back to the slow path which uses readdir. 243873c4903SPrakash Surya */ 244873c4903SPrakash Surya if ((statvfs64(dirname, &st) != 0) || 245873c4903SPrakash Surya (strcmp(st.f_basetype, "zfs") != 0)) { 246873c4903SPrakash Surya return (dir_is_empty_readdir(dirname)); 247873c4903SPrakash Surya } 248873c4903SPrakash Surya 249873c4903SPrakash Surya /* 250873c4903SPrakash Surya * At this point, we know the provided path is on a ZFS 251873c4903SPrakash Surya * filesystem, so we can use stat instead of readdir to 252873c4903SPrakash Surya * determine if the directory is empty or not. We try to avoid 253873c4903SPrakash Surya * using readdir because that requires opening "dirname"; this 254873c4903SPrakash Surya * open file descriptor can potentially end up in a child 255873c4903SPrakash Surya * process if there's a concurrent fork, thus preventing the 256873c4903SPrakash Surya * zfs_mount() from otherwise succeeding (the open file 257873c4903SPrakash Surya * descriptor inherited by the child process will cause the 258873c4903SPrakash Surya * parent's mount to fail with EBUSY). The performance 259873c4903SPrakash Surya * implications of replacing the open, read, and close with a 260873c4903SPrakash Surya * single stat is nice; but is not the main motivation for the 261873c4903SPrakash Surya * added complexity. 262873c4903SPrakash Surya */ 263873c4903SPrakash Surya return (dir_is_empty_stat(dirname)); 264873c4903SPrakash Surya } 265873c4903SPrakash Surya 266fa9e4066Sahrens /* 267fa9e4066Sahrens * Checks to see if the mount is active. If the filesystem is mounted, we fill 268fa9e4066Sahrens * in 'where' with the current mountpoint, and return 1. Otherwise, we return 269fa9e4066Sahrens * 0. 270fa9e4066Sahrens */ 27199653d4eSeschrock boolean_t 27255434c77Sek is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 273fa9e4066Sahrens { 274ebedde84SEric Taylor struct mnttab entry; 275fa9e4066Sahrens 276ebedde84SEric Taylor if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0) 27799653d4eSeschrock return (B_FALSE); 278fa9e4066Sahrens 279fa9e4066Sahrens if (where != NULL) 28055434c77Sek *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 281fa9e4066Sahrens 28299653d4eSeschrock return (B_TRUE); 283fa9e4066Sahrens } 284fa9e4066Sahrens 28555434c77Sek boolean_t 28655434c77Sek zfs_is_mounted(zfs_handle_t *zhp, char **where) 28755434c77Sek { 28855434c77Sek return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 28955434c77Sek } 29055434c77Sek 291e9dbad6fSeschrock /* 292e9dbad6fSeschrock * Returns true if the given dataset is mountable, false otherwise. Returns the 293e9dbad6fSeschrock * mountpoint in 'buf'. 294e9dbad6fSeschrock */ 295e9dbad6fSeschrock static boolean_t 296e9dbad6fSeschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 297990b4856Slling zprop_source_t *source) 298e9dbad6fSeschrock { 2999adfa60dSMatthew Ahrens char sourceloc[MAXNAMELEN]; 300990b4856Slling zprop_source_t sourcetype; 301e9dbad6fSeschrock 302e9dbad6fSeschrock if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 303e9dbad6fSeschrock return (B_FALSE); 304e9dbad6fSeschrock 305e9dbad6fSeschrock verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 306e9dbad6fSeschrock &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 307e9dbad6fSeschrock 308e9dbad6fSeschrock if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 309e9dbad6fSeschrock strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 310e9dbad6fSeschrock return (B_FALSE); 311e9dbad6fSeschrock 312a227b7f4Shs if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF) 313e9dbad6fSeschrock return (B_FALSE); 314e9dbad6fSeschrock 315e9dbad6fSeschrock if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 316e9dbad6fSeschrock getzoneid() == GLOBAL_ZONEID) 317e9dbad6fSeschrock return (B_FALSE); 318e9dbad6fSeschrock 319e9dbad6fSeschrock if (source) 320e9dbad6fSeschrock *source = sourcetype; 321e9dbad6fSeschrock 322e9dbad6fSeschrock return (B_TRUE); 323e9dbad6fSeschrock } 324e9dbad6fSeschrock 325fa9e4066Sahrens /* 326fa9e4066Sahrens * Mount the given filesystem. 327fa9e4066Sahrens */ 328fa9e4066Sahrens int 329fa9e4066Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 330fa9e4066Sahrens { 331fa9e4066Sahrens struct stat buf; 332fa9e4066Sahrens char mountpoint[ZFS_MAXPROPLEN]; 333fa9e4066Sahrens char mntopts[MNT_LINE_MAX]; 33499653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 335fa9e4066Sahrens 336fa9e4066Sahrens if (options == NULL) 337fa9e4066Sahrens mntopts[0] = '\0'; 338fa9e4066Sahrens else 339fa9e4066Sahrens (void) strlcpy(mntopts, options, sizeof (mntopts)); 340fa9e4066Sahrens 341f9af39baSGeorge Wilson /* 342f9af39baSGeorge Wilson * If the pool is imported read-only then all mounts must be read-only 343f9af39baSGeorge Wilson */ 344f9af39baSGeorge Wilson if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL)) 345f9af39baSGeorge Wilson flags |= MS_RDONLY; 346f9af39baSGeorge Wilson 347e9dbad6fSeschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 34899653d4eSeschrock return (0); 349fa9e4066Sahrens 350fa9e4066Sahrens /* Create the directory if it doesn't already exist */ 351fa9e4066Sahrens if (lstat(mountpoint, &buf) != 0) { 352fa9e4066Sahrens if (mkdirp(mountpoint, 0755) != 0) { 35399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35499653d4eSeschrock "failed to create mountpoint")); 355ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 35699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 35799653d4eSeschrock mountpoint)); 358fa9e4066Sahrens } 359fa9e4066Sahrens } 360fa9e4066Sahrens 361fa9e4066Sahrens /* 362fa9e4066Sahrens * Determine if the mountpoint is empty. If so, refuse to perform the 363fa9e4066Sahrens * mount. We don't perform this check if MS_OVERLAY is specified, which 364fa9e4066Sahrens * would defeat the point. We also avoid this check if 'remount' is 365fa9e4066Sahrens * specified. 366fa9e4066Sahrens */ 367fa9e4066Sahrens if ((flags & MS_OVERLAY) == 0 && 368fa9e4066Sahrens strstr(mntopts, MNTOPT_REMOUNT) == NULL && 369fa9e4066Sahrens !dir_is_empty(mountpoint)) { 37099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37199653d4eSeschrock "directory is not empty")); 372ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 37399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 374fa9e4066Sahrens } 375fa9e4066Sahrens 376fa9e4066Sahrens /* perform the mount */ 377fa9e4066Sahrens if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 378fa9e4066Sahrens MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 379fa9e4066Sahrens /* 380fa9e4066Sahrens * Generic errors are nasty, but there are just way too many 381fa9e4066Sahrens * from mount(), and they're well-understood. We pick a few 382fa9e4066Sahrens * common ones to improve upon. 383fa9e4066Sahrens */ 384d8689a57Sdougm if (errno == EBUSY) { 38599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38699653d4eSeschrock "mountpoint or dataset is busy")); 387ecd6cf80Smarks } else if (errno == EPERM) { 388ecd6cf80Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 389ecd6cf80Smarks "Insufficient privileges")); 39054a91118SChris Kirby } else if (errno == ENOTSUP) { 39154a91118SChris Kirby char buf[256]; 392dc7cd546SMark Shellenbaum int spa_version; 39354a91118SChris Kirby 394dc7cd546SMark Shellenbaum VERIFY(zfs_spa_version(zhp, &spa_version) == 0); 39554a91118SChris Kirby (void) snprintf(buf, sizeof (buf), 396dc7cd546SMark Shellenbaum dgettext(TEXT_DOMAIN, "Can't mount a version %lld " 397dc7cd546SMark Shellenbaum "file system on a version %d pool. Pool must be" 398dc7cd546SMark Shellenbaum " upgraded to mount this file system."), 39954a91118SChris Kirby (u_longlong_t)zfs_prop_get_int(zhp, 400dc7cd546SMark Shellenbaum ZFS_PROP_VERSION), spa_version); 40154a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf)); 402d8689a57Sdougm } else { 40399653d4eSeschrock zfs_error_aux(hdl, strerror(errno)); 404d8689a57Sdougm } 405ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 40699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 40799653d4eSeschrock zhp->zfs_name)); 408fa9e4066Sahrens } 409fa9e4066Sahrens 410ebedde84SEric Taylor /* add the mounted entry into our cache */ 411ebedde84SEric Taylor libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, 412ebedde84SEric Taylor mntopts); 413fa9e4066Sahrens return (0); 414fa9e4066Sahrens } 415fa9e4066Sahrens 4163bb79becSeschrock /* 4173bb79becSeschrock * Unmount a single filesystem. 4183bb79becSeschrock */ 4193bb79becSeschrock static int 4203bb79becSeschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 4213bb79becSeschrock { 4223bb79becSeschrock if (umount2(mountpoint, flags) != 0) { 4233bb79becSeschrock zfs_error_aux(hdl, strerror(errno)); 424ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 4253bb79becSeschrock dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 4263bb79becSeschrock mountpoint)); 4273bb79becSeschrock } 4283bb79becSeschrock 4293bb79becSeschrock return (0); 4303bb79becSeschrock } 4313bb79becSeschrock 432fa9e4066Sahrens /* 433fa9e4066Sahrens * Unmount the given filesystem. 434fa9e4066Sahrens */ 435fa9e4066Sahrens int 436fa9e4066Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 437fa9e4066Sahrens { 438ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 439ebedde84SEric Taylor struct mnttab entry; 44067331909Sdougm char *mntpt = NULL; 441fa9e4066Sahrens 442ebedde84SEric Taylor /* check to see if we need to unmount the filesystem */ 443fa9e4066Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 444ebedde84SEric Taylor libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) { 44567331909Sdougm /* 44667331909Sdougm * mountpoint may have come from a call to 44767331909Sdougm * getmnt/getmntany if it isn't NULL. If it is NULL, 448ebedde84SEric Taylor * we know it comes from libzfs_mnttab_find which can 449ebedde84SEric Taylor * then get freed later. We strdup it to play it safe. 45067331909Sdougm */ 451fa9e4066Sahrens if (mountpoint == NULL) 452ebedde84SEric Taylor mntpt = zfs_strdup(hdl, entry.mnt_mountp); 45367331909Sdougm else 454ebedde84SEric Taylor mntpt = zfs_strdup(hdl, mountpoint); 455fa9e4066Sahrens 456fa9e4066Sahrens /* 4573bb79becSeschrock * Unshare and unmount the filesystem 458fa9e4066Sahrens */ 459da6c28aaSamw if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) 460eb66cf86Sth return (-1); 461eb66cf86Sth 462ebedde84SEric Taylor if (unmount_one(hdl, mntpt, flags) != 0) { 46367331909Sdougm free(mntpt); 464da6c28aaSamw (void) zfs_shareall(zhp); 465fa9e4066Sahrens return (-1); 46667331909Sdougm } 467ebedde84SEric Taylor libzfs_mnttab_remove(hdl, zhp->zfs_name); 46867331909Sdougm free(mntpt); 469fa9e4066Sahrens } 470fa9e4066Sahrens 471fa9e4066Sahrens return (0); 472fa9e4066Sahrens } 473fa9e4066Sahrens 474fa9e4066Sahrens /* 475fa9e4066Sahrens * Unmount this filesystem and any children inheriting the mountpoint property. 476fa9e4066Sahrens * To do this, just act like we're changing the mountpoint property, but don't 477fa9e4066Sahrens * remount the filesystems afterwards. 478fa9e4066Sahrens */ 479fa9e4066Sahrens int 480fa9e4066Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags) 481fa9e4066Sahrens { 482fa9e4066Sahrens prop_changelist_t *clp; 483fa9e4066Sahrens int ret; 484fa9e4066Sahrens 4850069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags); 486fa9e4066Sahrens if (clp == NULL) 487fa9e4066Sahrens return (-1); 488fa9e4066Sahrens 489fa9e4066Sahrens ret = changelist_prefix(clp); 490fa9e4066Sahrens changelist_free(clp); 491fa9e4066Sahrens 492fa9e4066Sahrens return (ret); 493fa9e4066Sahrens } 494fa9e4066Sahrens 495f3861e1aSahl boolean_t 496f3861e1aSahl zfs_is_shared(zfs_handle_t *zhp) 497f3861e1aSahl { 498da6c28aaSamw zfs_share_type_t rc = 0; 499da6c28aaSamw zfs_share_proto_t *curr_proto; 500da6c28aaSamw 501f3861e1aSahl if (ZFS_IS_VOLUME(zhp)) 502ab003da8SJim Dunham return (B_FALSE); 503f3861e1aSahl 504da6c28aaSamw for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 505da6c28aaSamw curr_proto++) 506da6c28aaSamw rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto); 507da6c28aaSamw 508da6c28aaSamw return (rc ? B_TRUE : B_FALSE); 509f3861e1aSahl } 510f3861e1aSahl 511f3861e1aSahl int 512f3861e1aSahl zfs_share(zfs_handle_t *zhp) 513f3861e1aSahl { 5149d9a58e3SEric Taylor assert(!ZFS_IS_VOLUME(zhp)); 515da6c28aaSamw return (zfs_share_proto(zhp, share_all_proto)); 516f3861e1aSahl } 517f3861e1aSahl 518f3861e1aSahl int 519f3861e1aSahl zfs_unshare(zfs_handle_t *zhp) 520f3861e1aSahl { 5219d9a58e3SEric Taylor assert(!ZFS_IS_VOLUME(zhp)); 522da6c28aaSamw return (zfs_unshareall(zhp)); 523f3861e1aSahl } 524f3861e1aSahl 525fa9e4066Sahrens /* 526fa9e4066Sahrens * Check to see if the filesystem is currently shared. 527fa9e4066Sahrens */ 528da6c28aaSamw zfs_share_type_t 529da6c28aaSamw zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto) 530fa9e4066Sahrens { 531fa9e4066Sahrens char *mountpoint; 532da6c28aaSamw zfs_share_type_t rc; 533fa9e4066Sahrens 534fa9e4066Sahrens if (!zfs_is_mounted(zhp, &mountpoint)) 535da6c28aaSamw return (SHARED_NOT_SHARED); 536fa9e4066Sahrens 53788f61deeSIgor Kozhukhov if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) 53888f61deeSIgor Kozhukhov != SHARED_NOT_SHARED) { 539fa9e4066Sahrens if (where != NULL) 540fa9e4066Sahrens *where = mountpoint; 541fa9e4066Sahrens else 542fa9e4066Sahrens free(mountpoint); 543da6c28aaSamw return (rc); 544fa9e4066Sahrens } else { 545fa9e4066Sahrens free(mountpoint); 546da6c28aaSamw return (SHARED_NOT_SHARED); 547fa9e4066Sahrens } 548fa9e4066Sahrens } 549fa9e4066Sahrens 550da6c28aaSamw boolean_t 551da6c28aaSamw zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 552da6c28aaSamw { 553da6c28aaSamw return (zfs_is_shared_proto(zhp, where, 554da6c28aaSamw PROTO_NFS) != SHARED_NOT_SHARED); 555da6c28aaSamw } 556da6c28aaSamw 557da6c28aaSamw boolean_t 558da6c28aaSamw zfs_is_shared_smb(zfs_handle_t *zhp, char **where) 559da6c28aaSamw { 560da6c28aaSamw return (zfs_is_shared_proto(zhp, where, 561da6c28aaSamw PROTO_SMB) != SHARED_NOT_SHARED); 562da6c28aaSamw } 563da6c28aaSamw 56467331909Sdougm /* 56567331909Sdougm * Make sure things will work if libshare isn't installed by using 56667331909Sdougm * wrapper functions that check to see that the pointers to functions 56767331909Sdougm * initialized in _zfs_init_libshare() are actually present. 56867331909Sdougm */ 56967331909Sdougm 57067331909Sdougm static sa_handle_t (*_sa_init)(int); 5718a981c33SDaniel Hoffman static sa_handle_t (*_sa_init_arg)(int, void *); 57267331909Sdougm static void (*_sa_fini)(sa_handle_t); 57367331909Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *); 57467331909Sdougm static int (*_sa_enable_share)(sa_share_t, char *); 57567331909Sdougm static int (*_sa_disable_share)(sa_share_t, char *); 57667331909Sdougm static char *(*_sa_errorstr)(int); 57767331909Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *); 5785b6e0c46Sdougm static boolean_t (*_sa_needs_refresh)(sa_handle_t *); 5795b6e0c46Sdougm static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t); 5805b6e0c46Sdougm static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t, 5815b6e0c46Sdougm char *, char *, zprop_source_t, char *, char *, char *); 5825b6e0c46Sdougm static void (*_sa_update_sharetab_ts)(sa_handle_t); 58367331909Sdougm 58467331909Sdougm /* 58567331909Sdougm * _zfs_init_libshare() 58667331909Sdougm * 58767331909Sdougm * Find the libshare.so.1 entry points that we use here and save the 58867331909Sdougm * values to be used later. This is triggered by the runtime loader. 58967331909Sdougm * Make sure the correct ISA version is loaded. 59067331909Sdougm */ 5915b6e0c46Sdougm 59267331909Sdougm #pragma init(_zfs_init_libshare) 59367331909Sdougm static void 59467331909Sdougm _zfs_init_libshare(void) 59567331909Sdougm { 59667331909Sdougm void *libshare; 59767331909Sdougm char path[MAXPATHLEN]; 59867331909Sdougm char isa[MAXISALEN]; 59967331909Sdougm 60067331909Sdougm #if defined(_LP64) 60167331909Sdougm if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 602d8689a57Sdougm isa[0] = '\0'; 60367331909Sdougm #else 60467331909Sdougm isa[0] = '\0'; 60567331909Sdougm #endif 60667331909Sdougm (void) snprintf(path, MAXPATHLEN, 607d8689a57Sdougm "/usr/lib/%s/libshare.so.1", isa); 60867331909Sdougm 60967331909Sdougm if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) { 610d8689a57Sdougm _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init"); 6118a981c33SDaniel Hoffman _sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare, 6128a981c33SDaniel Hoffman "sa_init_arg"); 613d8689a57Sdougm _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini"); 614d8689a57Sdougm _sa_find_share = (sa_share_t (*)(sa_handle_t, char *)) 615d8689a57Sdougm dlsym(libshare, "sa_find_share"); 616d8689a57Sdougm _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 617d8689a57Sdougm "sa_enable_share"); 618d8689a57Sdougm _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 619d8689a57Sdougm "sa_disable_share"); 620d8689a57Sdougm _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr"); 621d8689a57Sdougm _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *)) 622d8689a57Sdougm dlsym(libshare, "sa_parse_legacy_options"); 6235b6e0c46Sdougm _sa_needs_refresh = (boolean_t (*)(sa_handle_t *)) 6245b6e0c46Sdougm dlsym(libshare, "sa_needs_refresh"); 6255b6e0c46Sdougm _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t)) 6265b6e0c46Sdougm dlsym(libshare, "sa_get_zfs_handle"); 6275b6e0c46Sdougm _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t, 6285b6e0c46Sdougm sa_share_t, char *, char *, zprop_source_t, char *, 6295b6e0c46Sdougm char *, char *))dlsym(libshare, "sa_zfs_process_share"); 6305b6e0c46Sdougm _sa_update_sharetab_ts = (void (*)(sa_handle_t)) 6315b6e0c46Sdougm dlsym(libshare, "sa_update_sharetab_ts"); 6328a981c33SDaniel Hoffman if (_sa_init == NULL || _sa_init_arg == NULL || 6338a981c33SDaniel Hoffman _sa_fini == NULL || _sa_find_share == NULL || 6348a981c33SDaniel Hoffman _sa_enable_share == NULL || _sa_disable_share == NULL || 6358a981c33SDaniel Hoffman _sa_errorstr == NULL || _sa_parse_legacy_options == NULL || 6365b6e0c46Sdougm _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL || 6375b6e0c46Sdougm _sa_zfs_process_share == NULL || 6385b6e0c46Sdougm _sa_update_sharetab_ts == NULL) { 63957b448deSdougm _sa_init = NULL; 6408a981c33SDaniel Hoffman _sa_init_arg = NULL; 64157b448deSdougm _sa_fini = NULL; 64257b448deSdougm _sa_disable_share = NULL; 64357b448deSdougm _sa_enable_share = NULL; 64457b448deSdougm _sa_errorstr = NULL; 64557b448deSdougm _sa_parse_legacy_options = NULL; 64657b448deSdougm (void) dlclose(libshare); 6475b6e0c46Sdougm _sa_needs_refresh = NULL; 6485b6e0c46Sdougm _sa_get_zfs_handle = NULL; 6495b6e0c46Sdougm _sa_zfs_process_share = NULL; 6505b6e0c46Sdougm _sa_update_sharetab_ts = NULL; 65157b448deSdougm } 65267331909Sdougm } 65367331909Sdougm } 65467331909Sdougm 65567331909Sdougm /* 65667331909Sdougm * zfs_init_libshare(zhandle, service) 65767331909Sdougm * 65867331909Sdougm * Initialize the libshare API if it hasn't already been initialized. 65967331909Sdougm * In all cases it returns 0 if it succeeded and an error if not. The 66067331909Sdougm * service value is which part(s) of the API to initialize and is a 66167331909Sdougm * direct map to the libshare sa_init(service) interface. 66267331909Sdougm */ 6638a981c33SDaniel Hoffman static int 6648a981c33SDaniel Hoffman zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg) 66567331909Sdougm { 666d8689a57Sdougm if (_sa_init == NULL) 66709c9e6dcSChris Williamson return (SA_CONFIG_ERR); 668d8689a57Sdougm 66909c9e6dcSChris Williamson /* 67009c9e6dcSChris Williamson * Attempt to refresh libshare. This is necessary if there was a cache 67109c9e6dcSChris Williamson * miss for a new ZFS dataset that was just created, or if state of the 67209c9e6dcSChris Williamson * sharetab file has changed since libshare was last initialized. We 67309c9e6dcSChris Williamson * want to make sure so check timestamps to see if a different process 67409c9e6dcSChris Williamson * has updated any of the configuration. If there was some non-ZFS 67509c9e6dcSChris Williamson * change, we need to re-initialize the internal cache. 67609c9e6dcSChris Williamson */ 67709c9e6dcSChris Williamson if (_sa_needs_refresh != NULL && 67809c9e6dcSChris Williamson _sa_needs_refresh(zhandle->libzfs_sharehdl)) { 67909c9e6dcSChris Williamson zfs_uninit_libshare(zhandle); 6808a981c33SDaniel Hoffman zhandle->libzfs_sharehdl = _sa_init_arg(service, arg); 6815b6e0c46Sdougm } 6825b6e0c46Sdougm 68309c9e6dcSChris Williamson if (zhandle && zhandle->libzfs_sharehdl == NULL) 6848a981c33SDaniel Hoffman zhandle->libzfs_sharehdl = _sa_init_arg(service, arg); 685d8689a57Sdougm 68609c9e6dcSChris Williamson if (zhandle->libzfs_sharehdl == NULL) 68709c9e6dcSChris Williamson return (SA_NO_MEMORY); 688d8689a57Sdougm 68909c9e6dcSChris Williamson return (SA_OK); 69067331909Sdougm } 6918a981c33SDaniel Hoffman int 6928a981c33SDaniel Hoffman zfs_init_libshare(libzfs_handle_t *zhandle, int service) 6938a981c33SDaniel Hoffman { 6948a981c33SDaniel Hoffman return (zfs_init_libshare_impl(zhandle, service, NULL)); 6958a981c33SDaniel Hoffman } 6968a981c33SDaniel Hoffman 6978a981c33SDaniel Hoffman int 6988a981c33SDaniel Hoffman zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg) 6998a981c33SDaniel Hoffman { 7008a981c33SDaniel Hoffman return (zfs_init_libshare_impl(zhandle, service, arg)); 7018a981c33SDaniel Hoffman } 7028a981c33SDaniel Hoffman 70367331909Sdougm 70467331909Sdougm /* 70567331909Sdougm * zfs_uninit_libshare(zhandle) 70667331909Sdougm * 70767331909Sdougm * Uninitialize the libshare API if it hasn't already been 70867331909Sdougm * uninitialized. It is OK to call multiple times. 70967331909Sdougm */ 71067331909Sdougm void 71167331909Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle) 71267331909Sdougm { 71367331909Sdougm if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) { 714d8689a57Sdougm if (_sa_fini != NULL) 715d8689a57Sdougm _sa_fini(zhandle->libzfs_sharehdl); 716d8689a57Sdougm zhandle->libzfs_sharehdl = NULL; 71767331909Sdougm } 71867331909Sdougm } 71967331909Sdougm 72067331909Sdougm /* 72167331909Sdougm * zfs_parse_options(options, proto) 72267331909Sdougm * 72367331909Sdougm * Call the legacy parse interface to get the protocol specific 72467331909Sdougm * options using the NULL arg to indicate that this is a "parse" only. 72567331909Sdougm */ 72667331909Sdougm int 727da6c28aaSamw zfs_parse_options(char *options, zfs_share_proto_t proto) 72867331909Sdougm { 7293cb34c60Sahrens if (_sa_parse_legacy_options != NULL) { 7303cb34c60Sahrens return (_sa_parse_legacy_options(NULL, options, 7313cb34c60Sahrens proto_table[proto].p_name)); 7323cb34c60Sahrens } 7333cb34c60Sahrens return (SA_CONFIG_ERR); 73467331909Sdougm } 73567331909Sdougm 73667331909Sdougm /* 73767331909Sdougm * zfs_sa_find_share(handle, path) 73867331909Sdougm * 73967331909Sdougm * wrapper around sa_find_share to find a share path in the 74067331909Sdougm * configuration. 74167331909Sdougm */ 74267331909Sdougm static sa_share_t 74367331909Sdougm zfs_sa_find_share(sa_handle_t handle, char *path) 74467331909Sdougm { 74567331909Sdougm if (_sa_find_share != NULL) 746d8689a57Sdougm return (_sa_find_share(handle, path)); 74767331909Sdougm return (NULL); 74867331909Sdougm } 74967331909Sdougm 75067331909Sdougm /* 75167331909Sdougm * zfs_sa_enable_share(share, proto) 75267331909Sdougm * 75367331909Sdougm * Wrapper for sa_enable_share which enables a share for a specified 75467331909Sdougm * protocol. 75567331909Sdougm */ 75667331909Sdougm static int 75767331909Sdougm zfs_sa_enable_share(sa_share_t share, char *proto) 75867331909Sdougm { 75967331909Sdougm if (_sa_enable_share != NULL) 760d8689a57Sdougm return (_sa_enable_share(share, proto)); 76167331909Sdougm return (SA_CONFIG_ERR); 76267331909Sdougm } 76367331909Sdougm 76467331909Sdougm /* 76567331909Sdougm * zfs_sa_disable_share(share, proto) 76667331909Sdougm * 76767331909Sdougm * Wrapper for sa_enable_share which disables a share for a specified 76867331909Sdougm * protocol. 76967331909Sdougm */ 77067331909Sdougm static int 77167331909Sdougm zfs_sa_disable_share(sa_share_t share, char *proto) 77267331909Sdougm { 77367331909Sdougm if (_sa_disable_share != NULL) 774d8689a57Sdougm return (_sa_disable_share(share, proto)); 77567331909Sdougm return (SA_CONFIG_ERR); 77667331909Sdougm } 77767331909Sdougm 778fa9e4066Sahrens /* 779da6c28aaSamw * Share the given filesystem according to the options in the specified 780da6c28aaSamw * protocol specific properties (sharenfs, sharesmb). We rely 78167331909Sdougm * on "libshare" to the dirty work for us. 782fa9e4066Sahrens */ 783da6c28aaSamw static int 784da6c28aaSamw zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 785fa9e4066Sahrens { 786fa9e4066Sahrens char mountpoint[ZFS_MAXPROPLEN]; 787fa9e4066Sahrens char shareopts[ZFS_MAXPROPLEN]; 7885b6e0c46Sdougm char sourcestr[ZFS_MAXPROPLEN]; 78999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 79067331909Sdougm sa_share_t share; 791da6c28aaSamw zfs_share_proto_t *curr_proto; 7925b6e0c46Sdougm zprop_source_t sourcetype; 79367331909Sdougm int ret; 794fa9e4066Sahrens 795e9dbad6fSeschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 796fa9e4066Sahrens return (0); 797fa9e4066Sahrens 798da6c28aaSamw for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 799da6c28aaSamw /* 800da6c28aaSamw * Return success if there are no share options. 801da6c28aaSamw */ 802da6c28aaSamw if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop, 8035b6e0c46Sdougm shareopts, sizeof (shareopts), &sourcetype, sourcestr, 8045b6e0c46Sdougm ZFS_MAXPROPLEN, B_FALSE) != 0 || 8055b6e0c46Sdougm strcmp(shareopts, "off") == 0) 806da6c28aaSamw continue; 8078a981c33SDaniel Hoffman ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE, 8088a981c33SDaniel Hoffman zhp); 80933cde0d0SMatthew Ahrens if (ret != SA_OK) { 81033cde0d0SMatthew Ahrens (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 81133cde0d0SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot share '%s': %s"), 81233cde0d0SMatthew Ahrens zfs_get_name(zhp), _sa_errorstr != NULL ? 81333cde0d0SMatthew Ahrens _sa_errorstr(ret) : ""); 81433cde0d0SMatthew Ahrens return (-1); 81533cde0d0SMatthew Ahrens } 81633cde0d0SMatthew Ahrens 817da6c28aaSamw /* 818da6c28aaSamw * If the 'zoned' property is set, then zfs_is_mountable() 819da6c28aaSamw * will have already bailed out if we are in the global zone. 820da6c28aaSamw * But local zones cannot be NFS servers, so we ignore it for 821da6c28aaSamw * local zones as well. 822da6c28aaSamw */ 823da6c28aaSamw if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 824da6c28aaSamw continue; 825da6c28aaSamw 826da6c28aaSamw share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint); 8275b6e0c46Sdougm if (share == NULL) { 8285b6e0c46Sdougm /* 8295b6e0c46Sdougm * This may be a new file system that was just 8305b6e0c46Sdougm * created so isn't in the internal cache 8315b6e0c46Sdougm * (second time through). Rather than 8325b6e0c46Sdougm * reloading the entire configuration, we can 8335b6e0c46Sdougm * assume ZFS has done the checking and it is 8345b6e0c46Sdougm * safe to add this to the internal 8355b6e0c46Sdougm * configuration. 8365b6e0c46Sdougm */ 8375b6e0c46Sdougm if (_sa_zfs_process_share(hdl->libzfs_sharehdl, 8385b6e0c46Sdougm NULL, NULL, mountpoint, 8395b6e0c46Sdougm proto_table[*curr_proto].p_name, sourcetype, 8405b6e0c46Sdougm shareopts, sourcestr, zhp->zfs_name) != SA_OK) { 8415b6e0c46Sdougm (void) zfs_error_fmt(hdl, 8425b6e0c46Sdougm proto_table[*curr_proto].p_share_err, 8435b6e0c46Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 8445b6e0c46Sdougm zfs_get_name(zhp)); 8455b6e0c46Sdougm return (-1); 8465b6e0c46Sdougm } 8475b6e0c46Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, 8485b6e0c46Sdougm mountpoint); 8495b6e0c46Sdougm } 850da6c28aaSamw if (share != NULL) { 851da6c28aaSamw int err; 852da6c28aaSamw err = zfs_sa_enable_share(share, 853da6c28aaSamw proto_table[*curr_proto].p_name); 854da6c28aaSamw if (err != SA_OK) { 855da6c28aaSamw (void) zfs_error_fmt(hdl, 856da6c28aaSamw proto_table[*curr_proto].p_share_err, 857da6c28aaSamw dgettext(TEXT_DOMAIN, "cannot share '%s'"), 858da6c28aaSamw zfs_get_name(zhp)); 859da6c28aaSamw return (-1); 860da6c28aaSamw } 861da6c28aaSamw } else { 862da6c28aaSamw (void) zfs_error_fmt(hdl, 863da6c28aaSamw proto_table[*curr_proto].p_share_err, 864d8689a57Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 865d8689a57Sdougm zfs_get_name(zhp)); 866d8689a57Sdougm return (-1); 867d8689a57Sdougm } 868fa9e4066Sahrens 869da6c28aaSamw } 870fa9e4066Sahrens return (0); 871fa9e4066Sahrens } 872fa9e4066Sahrens 873da6c28aaSamw 874da6c28aaSamw int 875da6c28aaSamw zfs_share_nfs(zfs_handle_t *zhp) 876da6c28aaSamw { 877da6c28aaSamw return (zfs_share_proto(zhp, nfs_only)); 878da6c28aaSamw } 879da6c28aaSamw 880da6c28aaSamw int 881da6c28aaSamw zfs_share_smb(zfs_handle_t *zhp) 882da6c28aaSamw { 883da6c28aaSamw return (zfs_share_proto(zhp, smb_only)); 884da6c28aaSamw } 885da6c28aaSamw 886da6c28aaSamw int 887da6c28aaSamw zfs_shareall(zfs_handle_t *zhp) 888da6c28aaSamw { 889da6c28aaSamw return (zfs_share_proto(zhp, share_all_proto)); 890da6c28aaSamw } 891da6c28aaSamw 8923bb79becSeschrock /* 8933bb79becSeschrock * Unshare a filesystem by mountpoint. 8943bb79becSeschrock */ 8953bb79becSeschrock static int 896da6c28aaSamw unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, 897da6c28aaSamw zfs_share_proto_t proto) 8983bb79becSeschrock { 89967331909Sdougm sa_share_t share; 90067331909Sdougm int err; 90167331909Sdougm char *mntpt; 9028a981c33SDaniel Hoffman 9033bb79becSeschrock /* 90467331909Sdougm * Mountpoint could get trashed if libshare calls getmntany 905ebedde84SEric Taylor * which it does during API initialization, so strdup the 90667331909Sdougm * value. 9073bb79becSeschrock */ 90867331909Sdougm mntpt = zfs_strdup(hdl, mountpoint); 9093bb79becSeschrock 9108a981c33SDaniel Hoffman /* 9118a981c33SDaniel Hoffman * make sure libshare initialized, initialize everything because we 9128a981c33SDaniel Hoffman * don't know what other unsharing may happen later. Functions up the 9138a981c33SDaniel Hoffman * stack are allowed to initialize instead a subset of shares at the 9148a981c33SDaniel Hoffman * time the set is known. 9158a981c33SDaniel Hoffman */ 9168a981c33SDaniel Hoffman if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME, 9178a981c33SDaniel Hoffman (void *)name)) != SA_OK) { 91867331909Sdougm free(mntpt); /* don't need the copy anymore */ 919*4f4378ccSAndrew Stormont return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err, 920d8689a57Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 921d8689a57Sdougm name, _sa_errorstr(err))); 92267331909Sdougm } 9233bb79becSeschrock 92467331909Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt); 92567331909Sdougm free(mntpt); /* don't need the copy anymore */ 9263bb79becSeschrock 92767331909Sdougm if (share != NULL) { 928da6c28aaSamw err = zfs_sa_disable_share(share, proto_table[proto].p_name); 92967331909Sdougm if (err != SA_OK) { 930*4f4378ccSAndrew Stormont return (zfs_error_fmt(hdl, 931*4f4378ccSAndrew Stormont proto_table[proto].p_unshare_err, 932d8689a57Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 933d8689a57Sdougm name, _sa_errorstr(err))); 93467331909Sdougm } 93567331909Sdougm } else { 936*4f4378ccSAndrew Stormont return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err, 937d8689a57Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"), 938d8689a57Sdougm name)); 9393bb79becSeschrock } 9403bb79becSeschrock return (0); 9413bb79becSeschrock } 9423bb79becSeschrock 943fa9e4066Sahrens /* 944fa9e4066Sahrens * Unshare the given filesystem. 945fa9e4066Sahrens */ 946fa9e4066Sahrens int 947da6c28aaSamw zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, 948da6c28aaSamw zfs_share_proto_t *proto) 949fa9e4066Sahrens { 950ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 951ebedde84SEric Taylor struct mnttab entry; 95267331909Sdougm char *mntpt = NULL; 953fa9e4066Sahrens 954fa9e4066Sahrens /* check to see if need to unmount the filesystem */ 95599653d4eSeschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 956d8689a57Sdougm if (mountpoint != NULL) 957ebedde84SEric Taylor mountpoint = mntpt = zfs_strdup(hdl, mountpoint); 958d8689a57Sdougm 959fa9e4066Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 960ebedde84SEric Taylor libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) { 961da6c28aaSamw zfs_share_proto_t *curr_proto; 962fa9e4066Sahrens 963fa9e4066Sahrens if (mountpoint == NULL) 964da6c28aaSamw mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 965fa9e4066Sahrens 966da6c28aaSamw for (curr_proto = proto; *curr_proto != PROTO_END; 967da6c28aaSamw curr_proto++) { 968da6c28aaSamw 969ebedde84SEric Taylor if (is_shared(hdl, mntpt, *curr_proto) && 970ebedde84SEric Taylor unshare_one(hdl, zhp->zfs_name, 971da6c28aaSamw mntpt, *curr_proto) != 0) { 972da6c28aaSamw if (mntpt != NULL) 973da6c28aaSamw free(mntpt); 974da6c28aaSamw return (-1); 975da6c28aaSamw } 97667331909Sdougm } 977fa9e4066Sahrens } 97867331909Sdougm if (mntpt != NULL) 979d8689a57Sdougm free(mntpt); 980fa9e4066Sahrens 981fa9e4066Sahrens return (0); 982fa9e4066Sahrens } 983fa9e4066Sahrens 984da6c28aaSamw int 985da6c28aaSamw zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 986da6c28aaSamw { 987da6c28aaSamw return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 988da6c28aaSamw } 989da6c28aaSamw 990da6c28aaSamw int 991da6c28aaSamw zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint) 992da6c28aaSamw { 993da6c28aaSamw return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 994da6c28aaSamw } 995da6c28aaSamw 996fa9e4066Sahrens /* 997da6c28aaSamw * Same as zfs_unmountall(), but for NFS and SMB unshares. 998fa9e4066Sahrens */ 999fa9e4066Sahrens int 1000da6c28aaSamw zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 1001fa9e4066Sahrens { 1002fa9e4066Sahrens prop_changelist_t *clp; 1003fa9e4066Sahrens int ret; 1004fa9e4066Sahrens 10050069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0); 1006fa9e4066Sahrens if (clp == NULL) 1007fa9e4066Sahrens return (-1); 1008fa9e4066Sahrens 1009da6c28aaSamw ret = changelist_unshare(clp, proto); 1010fa9e4066Sahrens changelist_free(clp); 1011fa9e4066Sahrens 1012fa9e4066Sahrens return (ret); 1013fa9e4066Sahrens } 1014fa9e4066Sahrens 1015da6c28aaSamw int 1016da6c28aaSamw zfs_unshareall_nfs(zfs_handle_t *zhp) 1017da6c28aaSamw { 1018da6c28aaSamw return (zfs_unshareall_proto(zhp, nfs_only)); 1019da6c28aaSamw } 1020da6c28aaSamw 1021da6c28aaSamw int 1022da6c28aaSamw zfs_unshareall_smb(zfs_handle_t *zhp) 1023da6c28aaSamw { 1024da6c28aaSamw return (zfs_unshareall_proto(zhp, smb_only)); 1025da6c28aaSamw } 1026da6c28aaSamw 1027da6c28aaSamw int 1028da6c28aaSamw zfs_unshareall(zfs_handle_t *zhp) 1029da6c28aaSamw { 1030da6c28aaSamw return (zfs_unshareall_proto(zhp, share_all_proto)); 1031da6c28aaSamw } 1032da6c28aaSamw 1033da6c28aaSamw int 1034da6c28aaSamw zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint) 1035da6c28aaSamw { 1036da6c28aaSamw return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 1037da6c28aaSamw } 1038da6c28aaSamw 1039fa9e4066Sahrens /* 1040fa9e4066Sahrens * Remove the mountpoint associated with the current dataset, if necessary. 1041fa9e4066Sahrens * We only remove the underlying directory if: 1042fa9e4066Sahrens * 1043fa9e4066Sahrens * - The mountpoint is not 'none' or 'legacy' 1044fa9e4066Sahrens * - The mountpoint is non-empty 1045fa9e4066Sahrens * - The mountpoint is the default or inherited 1046fa9e4066Sahrens * - The 'zoned' property is set, or we're in a local zone 1047fa9e4066Sahrens * 1048fa9e4066Sahrens * Any other directories we leave alone. 1049fa9e4066Sahrens */ 1050fa9e4066Sahrens void 1051fa9e4066Sahrens remove_mountpoint(zfs_handle_t *zhp) 1052fa9e4066Sahrens { 1053fa9e4066Sahrens char mountpoint[ZFS_MAXPROPLEN]; 1054990b4856Slling zprop_source_t source; 1055fa9e4066Sahrens 1056e9dbad6fSeschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 1057e9dbad6fSeschrock &source)) 1058fa9e4066Sahrens return; 1059fa9e4066Sahrens 1060990b4856Slling if (source == ZPROP_SRC_DEFAULT || 1061990b4856Slling source == ZPROP_SRC_INHERITED) { 1062fa9e4066Sahrens /* 1063fa9e4066Sahrens * Try to remove the directory, silently ignoring any errors. 1064fa9e4066Sahrens * The filesystem may have since been removed or moved around, 1065e9dbad6fSeschrock * and this error isn't really useful to the administrator in 1066e9dbad6fSeschrock * any way. 1067fa9e4066Sahrens */ 1068fa9e4066Sahrens (void) rmdir(mountpoint); 1069fa9e4066Sahrens } 1070fa9e4066Sahrens } 10713bb79becSeschrock 10729d9a58e3SEric Taylor void 10739d9a58e3SEric Taylor libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp) 10749d9a58e3SEric Taylor { 10759d9a58e3SEric Taylor if (cbp->cb_alloc == cbp->cb_used) { 10769d9a58e3SEric Taylor size_t newsz; 10779d9a58e3SEric Taylor void *ptr; 10789d9a58e3SEric Taylor 10799d9a58e3SEric Taylor newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64; 10809d9a58e3SEric Taylor ptr = zfs_realloc(zhp->zfs_hdl, 10819d9a58e3SEric Taylor cbp->cb_handles, cbp->cb_alloc * sizeof (void *), 10829d9a58e3SEric Taylor newsz * sizeof (void *)); 10839d9a58e3SEric Taylor cbp->cb_handles = ptr; 10849d9a58e3SEric Taylor cbp->cb_alloc = newsz; 10859d9a58e3SEric Taylor } 10869d9a58e3SEric Taylor cbp->cb_handles[cbp->cb_used++] = zhp; 10879d9a58e3SEric Taylor } 10883bb79becSeschrock 10893bb79becSeschrock static int 10903bb79becSeschrock mount_cb(zfs_handle_t *zhp, void *data) 10913bb79becSeschrock { 10929d9a58e3SEric Taylor get_all_cb_t *cbp = data; 10933bb79becSeschrock 10949d9a58e3SEric Taylor if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) { 10953bb79becSeschrock zfs_close(zhp); 10963bb79becSeschrock return (0); 10973bb79becSeschrock } 10983bb79becSeschrock 1099a227b7f4Shs if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) { 1100a227b7f4Shs zfs_close(zhp); 1101a227b7f4Shs return (0); 1102a227b7f4Shs } 1103a227b7f4Shs 11049c3fd121SMatthew Ahrens /* 11059c3fd121SMatthew Ahrens * If this filesystem is inconsistent and has a receive resume 11069c3fd121SMatthew Ahrens * token, we can not mount it. 11079c3fd121SMatthew Ahrens */ 11089c3fd121SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) && 11099c3fd121SMatthew Ahrens zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 11109c3fd121SMatthew Ahrens NULL, 0, NULL, NULL, 0, B_TRUE) == 0) { 11119c3fd121SMatthew Ahrens zfs_close(zhp); 11129c3fd121SMatthew Ahrens return (0); 11139c3fd121SMatthew Ahrens } 11149c3fd121SMatthew Ahrens 11159d9a58e3SEric Taylor libzfs_add_handle(cbp, zhp); 11169d9a58e3SEric Taylor if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) { 11179d9a58e3SEric Taylor zfs_close(zhp); 11189d9a58e3SEric Taylor return (-1); 11193bb79becSeschrock } 11209d9a58e3SEric Taylor return (0); 11213bb79becSeschrock } 11223bb79becSeschrock 11239d9a58e3SEric Taylor int 11249d9a58e3SEric Taylor libzfs_dataset_cmp(const void *a, const void *b) 11253bb79becSeschrock { 11263bb79becSeschrock zfs_handle_t **za = (zfs_handle_t **)a; 11273bb79becSeschrock zfs_handle_t **zb = (zfs_handle_t **)b; 11283bb79becSeschrock char mounta[MAXPATHLEN]; 11293bb79becSeschrock char mountb[MAXPATHLEN]; 1130f3861e1aSahl boolean_t gota, gotb; 1131f3861e1aSahl 1132f3861e1aSahl if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 1133f3861e1aSahl verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 1134f3861e1aSahl sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 1135f3861e1aSahl if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 1136f3861e1aSahl verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 1137f3861e1aSahl sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 1138f3861e1aSahl 1139f3861e1aSahl if (gota && gotb) 1140f3861e1aSahl return (strcmp(mounta, mountb)); 11413bb79becSeschrock 1142f3861e1aSahl if (gota) 1143f3861e1aSahl return (-1); 1144f3861e1aSahl if (gotb) 1145f3861e1aSahl return (1); 11463bb79becSeschrock 1147f3861e1aSahl return (strcmp(zfs_get_name(a), zfs_get_name(b))); 11483bb79becSeschrock } 11493bb79becSeschrock 1150f3861e1aSahl /* 1151f3861e1aSahl * Mount and share all datasets within the given pool. This assumes that no 1152f3861e1aSahl * datasets within the pool are currently mounted. Because users can create 1153f3861e1aSahl * complicated nested hierarchies of mountpoints, we first gather all the 1154f3861e1aSahl * datasets and mountpoints within the pool, and sort them by mountpoint. Once 1155f3861e1aSahl * we have the list of all filesystems, we iterate over them in order and mount 1156f3861e1aSahl * and/or share each one. 1157f3861e1aSahl */ 1158f3861e1aSahl #pragma weak zpool_mount_datasets = zpool_enable_datasets 11593bb79becSeschrock int 1160f3861e1aSahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 11613bb79becSeschrock { 11629d9a58e3SEric Taylor get_all_cb_t cb = { 0 }; 11633bb79becSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 11643bb79becSeschrock zfs_handle_t *zfsp; 11653bb79becSeschrock int i, ret = -1; 116667331909Sdougm int *good; 11673bb79becSeschrock 11683bb79becSeschrock /* 1169d87468daSrm * Gather all non-snap datasets within the pool. 11703bb79becSeschrock */ 1171990b4856Slling if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL) 11723bb79becSeschrock goto out; 11733bb79becSeschrock 11749d9a58e3SEric Taylor libzfs_add_handle(&cb, zfsp); 1175d87468daSrm if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0) 11763bb79becSeschrock goto out; 11773bb79becSeschrock /* 11783bb79becSeschrock * Sort the datasets by mountpoint. 11793bb79becSeschrock */ 11809d9a58e3SEric Taylor qsort(cb.cb_handles, cb.cb_used, sizeof (void *), 11819d9a58e3SEric Taylor libzfs_dataset_cmp); 11823bb79becSeschrock 11833bb79becSeschrock /* 118467331909Sdougm * And mount all the datasets, keeping track of which ones 11856e77af0aSDavid Pacheco * succeeded or failed. 11863bb79becSeschrock */ 11876e77af0aSDavid Pacheco if ((good = zfs_alloc(zhp->zpool_hdl, 11886e77af0aSDavid Pacheco cb.cb_used * sizeof (int))) == NULL) 11896e77af0aSDavid Pacheco goto out; 11906e77af0aSDavid Pacheco 11913bb79becSeschrock ret = 0; 11923bb79becSeschrock for (i = 0; i < cb.cb_used; i++) { 11939d9a58e3SEric Taylor if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0) 11943bb79becSeschrock ret = -1; 1195d8689a57Sdougm else 119667331909Sdougm good[i] = 1; 11973bb79becSeschrock } 11985b6e0c46Sdougm 119967331909Sdougm /* 120067331909Sdougm * Then share all the ones that need to be shared. This needs 120167331909Sdougm * to be a separate pass in order to avoid excessive reloading 120267331909Sdougm * of the configuration. Good should never be NULL since 120367331909Sdougm * zfs_alloc is supposed to exit if memory isn't available. 120467331909Sdougm */ 120567331909Sdougm for (i = 0; i < cb.cb_used; i++) { 12069d9a58e3SEric Taylor if (good[i] && zfs_share(cb.cb_handles[i]) != 0) 120767331909Sdougm ret = -1; 120867331909Sdougm } 120967331909Sdougm 121067331909Sdougm free(good); 12113bb79becSeschrock 12123bb79becSeschrock out: 12133bb79becSeschrock for (i = 0; i < cb.cb_used; i++) 12149d9a58e3SEric Taylor zfs_close(cb.cb_handles[i]); 12159d9a58e3SEric Taylor free(cb.cb_handles); 12163bb79becSeschrock 12173bb79becSeschrock return (ret); 12183bb79becSeschrock } 12193bb79becSeschrock 12203bb79becSeschrock static int 12213bb79becSeschrock mountpoint_compare(const void *a, const void *b) 12223bb79becSeschrock { 12233bb79becSeschrock const char *mounta = *((char **)a); 12243bb79becSeschrock const char *mountb = *((char **)b); 12253bb79becSeschrock 12263bb79becSeschrock return (strcmp(mountb, mounta)); 12273bb79becSeschrock } 12283bb79becSeschrock 1229681d9761SEric Taylor /* alias for 2002/240 */ 1230681d9761SEric Taylor #pragma weak zpool_unmount_datasets = zpool_disable_datasets 1231f3861e1aSahl /* 1232f3861e1aSahl * Unshare and unmount all datasets within the given pool. We don't want to 1233f3861e1aSahl * rely on traversing the DSL to discover the filesystems within the pool, 1234f3861e1aSahl * because this may be expensive (if not all of them are mounted), and can fail 1235f3861e1aSahl * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 1236f3861e1aSahl * gather all the filesystems that are currently mounted. 1237f3861e1aSahl */ 12383bb79becSeschrock int 1239f3861e1aSahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 12403bb79becSeschrock { 12413bb79becSeschrock int used, alloc; 12423bb79becSeschrock struct mnttab entry; 12433bb79becSeschrock size_t namelen; 12443bb79becSeschrock char **mountpoints = NULL; 12453bb79becSeschrock zfs_handle_t **datasets = NULL; 12463bb79becSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 12473bb79becSeschrock int i; 12483bb79becSeschrock int ret = -1; 12493bb79becSeschrock int flags = (force ? MS_FORCE : 0); 12508a981c33SDaniel Hoffman sa_init_selective_arg_t sharearg; 12513bb79becSeschrock 12523bb79becSeschrock namelen = strlen(zhp->zpool_name); 12533bb79becSeschrock 12543bb79becSeschrock rewind(hdl->libzfs_mnttab); 12553bb79becSeschrock used = alloc = 0; 12563bb79becSeschrock while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 12573bb79becSeschrock /* 12583bb79becSeschrock * Ignore non-ZFS entries. 12593bb79becSeschrock */ 12603bb79becSeschrock if (entry.mnt_fstype == NULL || 12613bb79becSeschrock strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 12623bb79becSeschrock continue; 12633bb79becSeschrock 12643bb79becSeschrock /* 12653bb79becSeschrock * Ignore filesystems not within this pool. 12663bb79becSeschrock */ 12673bb79becSeschrock if (entry.mnt_mountp == NULL || 12683bb79becSeschrock strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 12693bb79becSeschrock (entry.mnt_special[namelen] != '/' && 12703bb79becSeschrock entry.mnt_special[namelen] != '\0')) 12713bb79becSeschrock continue; 12723bb79becSeschrock 12733bb79becSeschrock /* 12743bb79becSeschrock * At this point we've found a filesystem within our pool. Add 12753bb79becSeschrock * it to our growing list. 12763bb79becSeschrock */ 12773bb79becSeschrock if (used == alloc) { 12783bb79becSeschrock if (alloc == 0) { 12793bb79becSeschrock if ((mountpoints = zfs_alloc(hdl, 12803bb79becSeschrock 8 * sizeof (void *))) == NULL) 12813bb79becSeschrock goto out; 12823bb79becSeschrock 12833bb79becSeschrock if ((datasets = zfs_alloc(hdl, 12843bb79becSeschrock 8 * sizeof (void *))) == NULL) 12853bb79becSeschrock goto out; 12863bb79becSeschrock 12873bb79becSeschrock alloc = 8; 12883bb79becSeschrock } else { 1289e9dbad6fSeschrock void *ptr; 12903bb79becSeschrock 1291e9dbad6fSeschrock if ((ptr = zfs_realloc(hdl, mountpoints, 1292e9dbad6fSeschrock alloc * sizeof (void *), 12933bb79becSeschrock alloc * 2 * sizeof (void *))) == NULL) 12943bb79becSeschrock goto out; 1295e9dbad6fSeschrock mountpoints = ptr; 12963bb79becSeschrock 1297e9dbad6fSeschrock if ((ptr = zfs_realloc(hdl, datasets, 1298e9dbad6fSeschrock alloc * sizeof (void *), 12993bb79becSeschrock alloc * 2 * sizeof (void *))) == NULL) 13003bb79becSeschrock goto out; 1301e9dbad6fSeschrock datasets = ptr; 13023bb79becSeschrock 13033bb79becSeschrock alloc *= 2; 13043bb79becSeschrock } 13053bb79becSeschrock } 13063bb79becSeschrock 13073bb79becSeschrock if ((mountpoints[used] = zfs_strdup(hdl, 13083bb79becSeschrock entry.mnt_mountp)) == NULL) 13093bb79becSeschrock goto out; 13103bb79becSeschrock 13113bb79becSeschrock /* 13123bb79becSeschrock * This is allowed to fail, in case there is some I/O error. It 13133bb79becSeschrock * is only used to determine if we need to remove the underlying 13143bb79becSeschrock * mountpoint, so failure is not fatal. 13153bb79becSeschrock */ 13163bb79becSeschrock datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 13173bb79becSeschrock 13183bb79becSeschrock used++; 13193bb79becSeschrock } 13203bb79becSeschrock 13213bb79becSeschrock /* 13223bb79becSeschrock * At this point, we have the entire list of filesystems, so sort it by 13233bb79becSeschrock * mountpoint. 13243bb79becSeschrock */ 13258a981c33SDaniel Hoffman sharearg.zhandle_arr = datasets; 13268a981c33SDaniel Hoffman sharearg.zhandle_len = used; 13278a981c33SDaniel Hoffman ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE, 13288a981c33SDaniel Hoffman &sharearg); 13298a981c33SDaniel Hoffman if (ret != 0) 13308a981c33SDaniel Hoffman goto out; 13313bb79becSeschrock qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 13323bb79becSeschrock 13333bb79becSeschrock /* 13343bb79becSeschrock * Walk through and first unshare everything. 13353bb79becSeschrock */ 13363bb79becSeschrock for (i = 0; i < used; i++) { 1337da6c28aaSamw zfs_share_proto_t *curr_proto; 1338da6c28aaSamw for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 1339da6c28aaSamw curr_proto++) { 1340da6c28aaSamw if (is_shared(hdl, mountpoints[i], *curr_proto) && 1341da6c28aaSamw unshare_one(hdl, mountpoints[i], 1342da6c28aaSamw mountpoints[i], *curr_proto) != 0) 1343da6c28aaSamw goto out; 1344da6c28aaSamw } 13453bb79becSeschrock } 13463bb79becSeschrock 13473bb79becSeschrock /* 13483bb79becSeschrock * Now unmount everything, removing the underlying directories as 13493bb79becSeschrock * appropriate. 13503bb79becSeschrock */ 13513bb79becSeschrock for (i = 0; i < used; i++) { 13523bb79becSeschrock if (unmount_one(hdl, mountpoints[i], flags) != 0) 13533bb79becSeschrock goto out; 1354e9dbad6fSeschrock } 13553bb79becSeschrock 1356e9dbad6fSeschrock for (i = 0; i < used; i++) { 13573bb79becSeschrock if (datasets[i]) 13583bb79becSeschrock remove_mountpoint(datasets[i]); 13593bb79becSeschrock } 13603bb79becSeschrock 13613bb79becSeschrock ret = 0; 13623bb79becSeschrock out: 13633bb79becSeschrock for (i = 0; i < used; i++) { 13643bb79becSeschrock if (datasets[i]) 13653bb79becSeschrock zfs_close(datasets[i]); 13663bb79becSeschrock free(mountpoints[i]); 13673bb79becSeschrock } 13683bb79becSeschrock free(datasets); 13693bb79becSeschrock free(mountpoints); 13703bb79becSeschrock 13713bb79becSeschrock return (ret); 13723bb79becSeschrock } 1373