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> 274f4378ccSAndrew 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 /* 954f4378ccSAndrew 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) { 213*ba6e7e65SSowrabha Gopal (void) close(dirfd); 214d420209dSAlex Reece return (B_TRUE); 215d420209dSAlex Reece } 216fa9e4066Sahrens 217fa9e4066Sahrens while ((dp = readdir64(dirp)) != NULL) { 218fa9e4066Sahrens 219fa9e4066Sahrens if (strcmp(dp->d_name, ".") == 0 || 220fa9e4066Sahrens strcmp(dp->d_name, "..") == 0) 221fa9e4066Sahrens continue; 222fa9e4066Sahrens 223fa9e4066Sahrens (void) closedir(dirp); 22499653d4eSeschrock return (B_FALSE); 225fa9e4066Sahrens } 226fa9e4066Sahrens 227fa9e4066Sahrens (void) closedir(dirp); 22899653d4eSeschrock return (B_TRUE); 229fa9e4066Sahrens } 230fa9e4066Sahrens 231873c4903SPrakash Surya /* 232873c4903SPrakash Surya * Returns true if the specified directory is empty. If we can't open the 233873c4903SPrakash Surya * directory at all, return true so that the mount can fail with a more 234873c4903SPrakash Surya * informative error message. 235873c4903SPrakash Surya */ 236873c4903SPrakash Surya static boolean_t 237873c4903SPrakash Surya dir_is_empty(const char *dirname) 238873c4903SPrakash Surya { 239873c4903SPrakash Surya struct statvfs64 st; 240873c4903SPrakash Surya 241873c4903SPrakash Surya /* 242873c4903SPrakash Surya * If the statvfs call fails or the filesystem is not a ZFS 243873c4903SPrakash Surya * filesystem, fall back to the slow path which uses readdir. 244873c4903SPrakash Surya */ 245873c4903SPrakash Surya if ((statvfs64(dirname, &st) != 0) || 246873c4903SPrakash Surya (strcmp(st.f_basetype, "zfs") != 0)) { 247873c4903SPrakash Surya return (dir_is_empty_readdir(dirname)); 248873c4903SPrakash Surya } 249873c4903SPrakash Surya 250873c4903SPrakash Surya /* 251873c4903SPrakash Surya * At this point, we know the provided path is on a ZFS 252873c4903SPrakash Surya * filesystem, so we can use stat instead of readdir to 253873c4903SPrakash Surya * determine if the directory is empty or not. We try to avoid 254873c4903SPrakash Surya * using readdir because that requires opening "dirname"; this 255873c4903SPrakash Surya * open file descriptor can potentially end up in a child 256873c4903SPrakash Surya * process if there's a concurrent fork, thus preventing the 257873c4903SPrakash Surya * zfs_mount() from otherwise succeeding (the open file 258873c4903SPrakash Surya * descriptor inherited by the child process will cause the 259873c4903SPrakash Surya * parent's mount to fail with EBUSY). The performance 260873c4903SPrakash Surya * implications of replacing the open, read, and close with a 261873c4903SPrakash Surya * single stat is nice; but is not the main motivation for the 262873c4903SPrakash Surya * added complexity. 263873c4903SPrakash Surya */ 264873c4903SPrakash Surya return (dir_is_empty_stat(dirname)); 265873c4903SPrakash Surya } 266873c4903SPrakash Surya 267fa9e4066Sahrens /* 268fa9e4066Sahrens * Checks to see if the mount is active. If the filesystem is mounted, we fill 269fa9e4066Sahrens * in 'where' with the current mountpoint, and return 1. Otherwise, we return 270fa9e4066Sahrens * 0. 271fa9e4066Sahrens */ 27299653d4eSeschrock boolean_t 27355434c77Sek is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 274fa9e4066Sahrens { 275ebedde84SEric Taylor struct mnttab entry; 276fa9e4066Sahrens 277ebedde84SEric Taylor if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0) 27899653d4eSeschrock return (B_FALSE); 279fa9e4066Sahrens 280fa9e4066Sahrens if (where != NULL) 28155434c77Sek *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 282fa9e4066Sahrens 28399653d4eSeschrock return (B_TRUE); 284fa9e4066Sahrens } 285fa9e4066Sahrens 28655434c77Sek boolean_t 28755434c77Sek zfs_is_mounted(zfs_handle_t *zhp, char **where) 28855434c77Sek { 28955434c77Sek return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 29055434c77Sek } 29155434c77Sek 292e9dbad6fSeschrock /* 293e9dbad6fSeschrock * Returns true if the given dataset is mountable, false otherwise. Returns the 294e9dbad6fSeschrock * mountpoint in 'buf'. 295e9dbad6fSeschrock */ 296e9dbad6fSeschrock static boolean_t 297e9dbad6fSeschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 298990b4856Slling zprop_source_t *source) 299e9dbad6fSeschrock { 3009adfa60dSMatthew Ahrens char sourceloc[MAXNAMELEN]; 301990b4856Slling zprop_source_t sourcetype; 302e9dbad6fSeschrock 303e9dbad6fSeschrock if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 304e9dbad6fSeschrock return (B_FALSE); 305e9dbad6fSeschrock 306e9dbad6fSeschrock verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 307e9dbad6fSeschrock &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 308e9dbad6fSeschrock 309e9dbad6fSeschrock if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 310e9dbad6fSeschrock strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 311e9dbad6fSeschrock return (B_FALSE); 312e9dbad6fSeschrock 313a227b7f4Shs if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF) 314e9dbad6fSeschrock return (B_FALSE); 315e9dbad6fSeschrock 316e9dbad6fSeschrock if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 317e9dbad6fSeschrock getzoneid() == GLOBAL_ZONEID) 318e9dbad6fSeschrock return (B_FALSE); 319e9dbad6fSeschrock 320e9dbad6fSeschrock if (source) 321e9dbad6fSeschrock *source = sourcetype; 322e9dbad6fSeschrock 323e9dbad6fSeschrock return (B_TRUE); 324e9dbad6fSeschrock } 325e9dbad6fSeschrock 326fa9e4066Sahrens /* 327fa9e4066Sahrens * Mount the given filesystem. 328fa9e4066Sahrens */ 329fa9e4066Sahrens int 330fa9e4066Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 331fa9e4066Sahrens { 332fa9e4066Sahrens struct stat buf; 333fa9e4066Sahrens char mountpoint[ZFS_MAXPROPLEN]; 334fa9e4066Sahrens char mntopts[MNT_LINE_MAX]; 33599653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 336fa9e4066Sahrens 337fa9e4066Sahrens if (options == NULL) 338fa9e4066Sahrens mntopts[0] = '\0'; 339fa9e4066Sahrens else 340fa9e4066Sahrens (void) strlcpy(mntopts, options, sizeof (mntopts)); 341fa9e4066Sahrens 342f9af39baSGeorge Wilson /* 343f9af39baSGeorge Wilson * If the pool is imported read-only then all mounts must be read-only 344f9af39baSGeorge Wilson */ 345f9af39baSGeorge Wilson if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL)) 346f9af39baSGeorge Wilson flags |= MS_RDONLY; 347f9af39baSGeorge Wilson 348e9dbad6fSeschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 34999653d4eSeschrock return (0); 350fa9e4066Sahrens 351fa9e4066Sahrens /* Create the directory if it doesn't already exist */ 352fa9e4066Sahrens if (lstat(mountpoint, &buf) != 0) { 353fa9e4066Sahrens if (mkdirp(mountpoint, 0755) != 0) { 35499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35599653d4eSeschrock "failed to create mountpoint")); 356ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 35799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 35899653d4eSeschrock mountpoint)); 359fa9e4066Sahrens } 360fa9e4066Sahrens } 361fa9e4066Sahrens 362fa9e4066Sahrens /* 363fa9e4066Sahrens * Determine if the mountpoint is empty. If so, refuse to perform the 364fa9e4066Sahrens * mount. We don't perform this check if MS_OVERLAY is specified, which 365fa9e4066Sahrens * would defeat the point. We also avoid this check if 'remount' is 366fa9e4066Sahrens * specified. 367fa9e4066Sahrens */ 368fa9e4066Sahrens if ((flags & MS_OVERLAY) == 0 && 369fa9e4066Sahrens strstr(mntopts, MNTOPT_REMOUNT) == NULL && 370fa9e4066Sahrens !dir_is_empty(mountpoint)) { 37199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37299653d4eSeschrock "directory is not empty")); 373ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 37499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 375fa9e4066Sahrens } 376fa9e4066Sahrens 377fa9e4066Sahrens /* perform the mount */ 378fa9e4066Sahrens if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 379fa9e4066Sahrens MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 380fa9e4066Sahrens /* 381fa9e4066Sahrens * Generic errors are nasty, but there are just way too many 382fa9e4066Sahrens * from mount(), and they're well-understood. We pick a few 383fa9e4066Sahrens * common ones to improve upon. 384fa9e4066Sahrens */ 385d8689a57Sdougm if (errno == EBUSY) { 38699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38799653d4eSeschrock "mountpoint or dataset is busy")); 388ecd6cf80Smarks } else if (errno == EPERM) { 389ecd6cf80Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 390ecd6cf80Smarks "Insufficient privileges")); 39154a91118SChris Kirby } else if (errno == ENOTSUP) { 39254a91118SChris Kirby char buf[256]; 393dc7cd546SMark Shellenbaum int spa_version; 39454a91118SChris Kirby 395dc7cd546SMark Shellenbaum VERIFY(zfs_spa_version(zhp, &spa_version) == 0); 39654a91118SChris Kirby (void) snprintf(buf, sizeof (buf), 397dc7cd546SMark Shellenbaum dgettext(TEXT_DOMAIN, "Can't mount a version %lld " 398dc7cd546SMark Shellenbaum "file system on a version %d pool. Pool must be" 399dc7cd546SMark Shellenbaum " upgraded to mount this file system."), 40054a91118SChris Kirby (u_longlong_t)zfs_prop_get_int(zhp, 401dc7cd546SMark Shellenbaum ZFS_PROP_VERSION), spa_version); 40254a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf)); 403d8689a57Sdougm } else { 40499653d4eSeschrock zfs_error_aux(hdl, strerror(errno)); 405d8689a57Sdougm } 406ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 40799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 40899653d4eSeschrock zhp->zfs_name)); 409fa9e4066Sahrens } 410fa9e4066Sahrens 411ebedde84SEric Taylor /* add the mounted entry into our cache */ 412ebedde84SEric Taylor libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, 413ebedde84SEric Taylor mntopts); 414fa9e4066Sahrens return (0); 415fa9e4066Sahrens } 416fa9e4066Sahrens 4173bb79becSeschrock /* 4183bb79becSeschrock * Unmount a single filesystem. 4193bb79becSeschrock */ 4203bb79becSeschrock static int 4213bb79becSeschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 4223bb79becSeschrock { 4233bb79becSeschrock if (umount2(mountpoint, flags) != 0) { 4243bb79becSeschrock zfs_error_aux(hdl, strerror(errno)); 425ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 4263bb79becSeschrock dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 4273bb79becSeschrock mountpoint)); 4283bb79becSeschrock } 4293bb79becSeschrock 4303bb79becSeschrock return (0); 4313bb79becSeschrock } 4323bb79becSeschrock 433fa9e4066Sahrens /* 434fa9e4066Sahrens * Unmount the given filesystem. 435fa9e4066Sahrens */ 436fa9e4066Sahrens int 437fa9e4066Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 438fa9e4066Sahrens { 439ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 440ebedde84SEric Taylor struct mnttab entry; 44167331909Sdougm char *mntpt = NULL; 442fa9e4066Sahrens 443ebedde84SEric Taylor /* check to see if we need to unmount the filesystem */ 444fa9e4066Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 445ebedde84SEric Taylor libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) { 44667331909Sdougm /* 44767331909Sdougm * mountpoint may have come from a call to 44867331909Sdougm * getmnt/getmntany if it isn't NULL. If it is NULL, 449ebedde84SEric Taylor * we know it comes from libzfs_mnttab_find which can 450ebedde84SEric Taylor * then get freed later. We strdup it to play it safe. 45167331909Sdougm */ 452fa9e4066Sahrens if (mountpoint == NULL) 453ebedde84SEric Taylor mntpt = zfs_strdup(hdl, entry.mnt_mountp); 45467331909Sdougm else 455ebedde84SEric Taylor mntpt = zfs_strdup(hdl, mountpoint); 456fa9e4066Sahrens 457fa9e4066Sahrens /* 4583bb79becSeschrock * Unshare and unmount the filesystem 459fa9e4066Sahrens */ 460da6c28aaSamw if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) 461eb66cf86Sth return (-1); 462eb66cf86Sth 463ebedde84SEric Taylor if (unmount_one(hdl, mntpt, flags) != 0) { 46467331909Sdougm free(mntpt); 465da6c28aaSamw (void) zfs_shareall(zhp); 466fa9e4066Sahrens return (-1); 46767331909Sdougm } 468ebedde84SEric Taylor libzfs_mnttab_remove(hdl, zhp->zfs_name); 46967331909Sdougm free(mntpt); 470fa9e4066Sahrens } 471fa9e4066Sahrens 472fa9e4066Sahrens return (0); 473fa9e4066Sahrens } 474fa9e4066Sahrens 475fa9e4066Sahrens /* 476fa9e4066Sahrens * Unmount this filesystem and any children inheriting the mountpoint property. 477fa9e4066Sahrens * To do this, just act like we're changing the mountpoint property, but don't 478fa9e4066Sahrens * remount the filesystems afterwards. 479fa9e4066Sahrens */ 480fa9e4066Sahrens int 481fa9e4066Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags) 482fa9e4066Sahrens { 483fa9e4066Sahrens prop_changelist_t *clp; 484fa9e4066Sahrens int ret; 485fa9e4066Sahrens 4860069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags); 487fa9e4066Sahrens if (clp == NULL) 488fa9e4066Sahrens return (-1); 489fa9e4066Sahrens 490fa9e4066Sahrens ret = changelist_prefix(clp); 491fa9e4066Sahrens changelist_free(clp); 492fa9e4066Sahrens 493fa9e4066Sahrens return (ret); 494fa9e4066Sahrens } 495fa9e4066Sahrens 496f3861e1aSahl boolean_t 497f3861e1aSahl zfs_is_shared(zfs_handle_t *zhp) 498f3861e1aSahl { 499da6c28aaSamw zfs_share_type_t rc = 0; 500da6c28aaSamw zfs_share_proto_t *curr_proto; 501da6c28aaSamw 502f3861e1aSahl if (ZFS_IS_VOLUME(zhp)) 503ab003da8SJim Dunham return (B_FALSE); 504f3861e1aSahl 505da6c28aaSamw for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 506da6c28aaSamw curr_proto++) 507da6c28aaSamw rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto); 508da6c28aaSamw 509da6c28aaSamw return (rc ? B_TRUE : B_FALSE); 510f3861e1aSahl } 511f3861e1aSahl 512f3861e1aSahl int 513f3861e1aSahl zfs_share(zfs_handle_t *zhp) 514f3861e1aSahl { 5159d9a58e3SEric Taylor assert(!ZFS_IS_VOLUME(zhp)); 516da6c28aaSamw return (zfs_share_proto(zhp, share_all_proto)); 517f3861e1aSahl } 518f3861e1aSahl 519f3861e1aSahl int 520f3861e1aSahl zfs_unshare(zfs_handle_t *zhp) 521f3861e1aSahl { 5229d9a58e3SEric Taylor assert(!ZFS_IS_VOLUME(zhp)); 523da6c28aaSamw return (zfs_unshareall(zhp)); 524f3861e1aSahl } 525f3861e1aSahl 526fa9e4066Sahrens /* 527fa9e4066Sahrens * Check to see if the filesystem is currently shared. 528fa9e4066Sahrens */ 529da6c28aaSamw zfs_share_type_t 530da6c28aaSamw zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto) 531fa9e4066Sahrens { 532fa9e4066Sahrens char *mountpoint; 533da6c28aaSamw zfs_share_type_t rc; 534fa9e4066Sahrens 535fa9e4066Sahrens if (!zfs_is_mounted(zhp, &mountpoint)) 536da6c28aaSamw return (SHARED_NOT_SHARED); 537fa9e4066Sahrens 53888f61deeSIgor Kozhukhov if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) 53988f61deeSIgor Kozhukhov != SHARED_NOT_SHARED) { 540fa9e4066Sahrens if (where != NULL) 541fa9e4066Sahrens *where = mountpoint; 542fa9e4066Sahrens else 543fa9e4066Sahrens free(mountpoint); 544da6c28aaSamw return (rc); 545fa9e4066Sahrens } else { 546fa9e4066Sahrens free(mountpoint); 547da6c28aaSamw return (SHARED_NOT_SHARED); 548fa9e4066Sahrens } 549fa9e4066Sahrens } 550fa9e4066Sahrens 551da6c28aaSamw boolean_t 552da6c28aaSamw zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 553da6c28aaSamw { 554da6c28aaSamw return (zfs_is_shared_proto(zhp, where, 555da6c28aaSamw PROTO_NFS) != SHARED_NOT_SHARED); 556da6c28aaSamw } 557da6c28aaSamw 558da6c28aaSamw boolean_t 559da6c28aaSamw zfs_is_shared_smb(zfs_handle_t *zhp, char **where) 560da6c28aaSamw { 561da6c28aaSamw return (zfs_is_shared_proto(zhp, where, 562da6c28aaSamw PROTO_SMB) != SHARED_NOT_SHARED); 563da6c28aaSamw } 564da6c28aaSamw 56567331909Sdougm /* 56667331909Sdougm * Make sure things will work if libshare isn't installed by using 56767331909Sdougm * wrapper functions that check to see that the pointers to functions 56867331909Sdougm * initialized in _zfs_init_libshare() are actually present. 56967331909Sdougm */ 57067331909Sdougm 57167331909Sdougm static sa_handle_t (*_sa_init)(int); 5728a981c33SDaniel Hoffman static sa_handle_t (*_sa_init_arg)(int, void *); 57367331909Sdougm static void (*_sa_fini)(sa_handle_t); 57467331909Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *); 57567331909Sdougm static int (*_sa_enable_share)(sa_share_t, char *); 57667331909Sdougm static int (*_sa_disable_share)(sa_share_t, char *); 57767331909Sdougm static char *(*_sa_errorstr)(int); 57867331909Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *); 5795b6e0c46Sdougm static boolean_t (*_sa_needs_refresh)(sa_handle_t *); 5805b6e0c46Sdougm static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t); 5815b6e0c46Sdougm static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t, 5825b6e0c46Sdougm char *, char *, zprop_source_t, char *, char *, char *); 5835b6e0c46Sdougm static void (*_sa_update_sharetab_ts)(sa_handle_t); 58467331909Sdougm 58567331909Sdougm /* 58667331909Sdougm * _zfs_init_libshare() 58767331909Sdougm * 58867331909Sdougm * Find the libshare.so.1 entry points that we use here and save the 58967331909Sdougm * values to be used later. This is triggered by the runtime loader. 59067331909Sdougm * Make sure the correct ISA version is loaded. 59167331909Sdougm */ 5925b6e0c46Sdougm 59367331909Sdougm #pragma init(_zfs_init_libshare) 59467331909Sdougm static void 59567331909Sdougm _zfs_init_libshare(void) 59667331909Sdougm { 59767331909Sdougm void *libshare; 59867331909Sdougm char path[MAXPATHLEN]; 59967331909Sdougm char isa[MAXISALEN]; 60067331909Sdougm 60167331909Sdougm #if defined(_LP64) 60267331909Sdougm if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 603d8689a57Sdougm isa[0] = '\0'; 60467331909Sdougm #else 60567331909Sdougm isa[0] = '\0'; 60667331909Sdougm #endif 60767331909Sdougm (void) snprintf(path, MAXPATHLEN, 608d8689a57Sdougm "/usr/lib/%s/libshare.so.1", isa); 60967331909Sdougm 61067331909Sdougm if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) { 611d8689a57Sdougm _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init"); 6128a981c33SDaniel Hoffman _sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare, 6138a981c33SDaniel Hoffman "sa_init_arg"); 614d8689a57Sdougm _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini"); 615d8689a57Sdougm _sa_find_share = (sa_share_t (*)(sa_handle_t, char *)) 616d8689a57Sdougm dlsym(libshare, "sa_find_share"); 617d8689a57Sdougm _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 618d8689a57Sdougm "sa_enable_share"); 619d8689a57Sdougm _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 620d8689a57Sdougm "sa_disable_share"); 621d8689a57Sdougm _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr"); 622d8689a57Sdougm _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *)) 623d8689a57Sdougm dlsym(libshare, "sa_parse_legacy_options"); 6245b6e0c46Sdougm _sa_needs_refresh = (boolean_t (*)(sa_handle_t *)) 6255b6e0c46Sdougm dlsym(libshare, "sa_needs_refresh"); 6265b6e0c46Sdougm _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t)) 6275b6e0c46Sdougm dlsym(libshare, "sa_get_zfs_handle"); 6285b6e0c46Sdougm _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t, 6295b6e0c46Sdougm sa_share_t, char *, char *, zprop_source_t, char *, 6305b6e0c46Sdougm char *, char *))dlsym(libshare, "sa_zfs_process_share"); 6315b6e0c46Sdougm _sa_update_sharetab_ts = (void (*)(sa_handle_t)) 6325b6e0c46Sdougm dlsym(libshare, "sa_update_sharetab_ts"); 6338a981c33SDaniel Hoffman if (_sa_init == NULL || _sa_init_arg == NULL || 6348a981c33SDaniel Hoffman _sa_fini == NULL || _sa_find_share == NULL || 6358a981c33SDaniel Hoffman _sa_enable_share == NULL || _sa_disable_share == NULL || 6368a981c33SDaniel Hoffman _sa_errorstr == NULL || _sa_parse_legacy_options == NULL || 6375b6e0c46Sdougm _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL || 6385b6e0c46Sdougm _sa_zfs_process_share == NULL || 6395b6e0c46Sdougm _sa_update_sharetab_ts == NULL) { 64057b448deSdougm _sa_init = NULL; 6418a981c33SDaniel Hoffman _sa_init_arg = NULL; 64257b448deSdougm _sa_fini = NULL; 64357b448deSdougm _sa_disable_share = NULL; 64457b448deSdougm _sa_enable_share = NULL; 64557b448deSdougm _sa_errorstr = NULL; 64657b448deSdougm _sa_parse_legacy_options = NULL; 64757b448deSdougm (void) dlclose(libshare); 6485b6e0c46Sdougm _sa_needs_refresh = NULL; 6495b6e0c46Sdougm _sa_get_zfs_handle = NULL; 6505b6e0c46Sdougm _sa_zfs_process_share = NULL; 6515b6e0c46Sdougm _sa_update_sharetab_ts = NULL; 65257b448deSdougm } 65367331909Sdougm } 65467331909Sdougm } 65567331909Sdougm 65667331909Sdougm /* 65767331909Sdougm * zfs_init_libshare(zhandle, service) 65867331909Sdougm * 65967331909Sdougm * Initialize the libshare API if it hasn't already been initialized. 66067331909Sdougm * In all cases it returns 0 if it succeeded and an error if not. The 66167331909Sdougm * service value is which part(s) of the API to initialize and is a 66267331909Sdougm * direct map to the libshare sa_init(service) interface. 66367331909Sdougm */ 6648a981c33SDaniel Hoffman static int 6658a981c33SDaniel Hoffman zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg) 66667331909Sdougm { 667d8689a57Sdougm if (_sa_init == NULL) 66809c9e6dcSChris Williamson return (SA_CONFIG_ERR); 669d8689a57Sdougm 67009c9e6dcSChris Williamson /* 67109c9e6dcSChris Williamson * Attempt to refresh libshare. This is necessary if there was a cache 67209c9e6dcSChris Williamson * miss for a new ZFS dataset that was just created, or if state of the 67309c9e6dcSChris Williamson * sharetab file has changed since libshare was last initialized. We 67409c9e6dcSChris Williamson * want to make sure so check timestamps to see if a different process 67509c9e6dcSChris Williamson * has updated any of the configuration. If there was some non-ZFS 67609c9e6dcSChris Williamson * change, we need to re-initialize the internal cache. 67709c9e6dcSChris Williamson */ 67809c9e6dcSChris Williamson if (_sa_needs_refresh != NULL && 67909c9e6dcSChris Williamson _sa_needs_refresh(zhandle->libzfs_sharehdl)) { 68009c9e6dcSChris Williamson zfs_uninit_libshare(zhandle); 6818a981c33SDaniel Hoffman zhandle->libzfs_sharehdl = _sa_init_arg(service, arg); 6825b6e0c46Sdougm } 6835b6e0c46Sdougm 68409c9e6dcSChris Williamson if (zhandle && zhandle->libzfs_sharehdl == NULL) 6858a981c33SDaniel Hoffman zhandle->libzfs_sharehdl = _sa_init_arg(service, arg); 686d8689a57Sdougm 68709c9e6dcSChris Williamson if (zhandle->libzfs_sharehdl == NULL) 68809c9e6dcSChris Williamson return (SA_NO_MEMORY); 689d8689a57Sdougm 69009c9e6dcSChris Williamson return (SA_OK); 69167331909Sdougm } 6928a981c33SDaniel Hoffman int 6938a981c33SDaniel Hoffman zfs_init_libshare(libzfs_handle_t *zhandle, int service) 6948a981c33SDaniel Hoffman { 6958a981c33SDaniel Hoffman return (zfs_init_libshare_impl(zhandle, service, NULL)); 6968a981c33SDaniel Hoffman } 6978a981c33SDaniel Hoffman 6988a981c33SDaniel Hoffman int 6998a981c33SDaniel Hoffman zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg) 7008a981c33SDaniel Hoffman { 7018a981c33SDaniel Hoffman return (zfs_init_libshare_impl(zhandle, service, arg)); 7028a981c33SDaniel Hoffman } 7038a981c33SDaniel Hoffman 70467331909Sdougm 70567331909Sdougm /* 70667331909Sdougm * zfs_uninit_libshare(zhandle) 70767331909Sdougm * 70867331909Sdougm * Uninitialize the libshare API if it hasn't already been 70967331909Sdougm * uninitialized. It is OK to call multiple times. 71067331909Sdougm */ 71167331909Sdougm void 71267331909Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle) 71367331909Sdougm { 71467331909Sdougm if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) { 715d8689a57Sdougm if (_sa_fini != NULL) 716d8689a57Sdougm _sa_fini(zhandle->libzfs_sharehdl); 717d8689a57Sdougm zhandle->libzfs_sharehdl = NULL; 71867331909Sdougm } 71967331909Sdougm } 72067331909Sdougm 72167331909Sdougm /* 72267331909Sdougm * zfs_parse_options(options, proto) 72367331909Sdougm * 72467331909Sdougm * Call the legacy parse interface to get the protocol specific 72567331909Sdougm * options using the NULL arg to indicate that this is a "parse" only. 72667331909Sdougm */ 72767331909Sdougm int 728da6c28aaSamw zfs_parse_options(char *options, zfs_share_proto_t proto) 72967331909Sdougm { 7303cb34c60Sahrens if (_sa_parse_legacy_options != NULL) { 7313cb34c60Sahrens return (_sa_parse_legacy_options(NULL, options, 7323cb34c60Sahrens proto_table[proto].p_name)); 7333cb34c60Sahrens } 7343cb34c60Sahrens return (SA_CONFIG_ERR); 73567331909Sdougm } 73667331909Sdougm 73767331909Sdougm /* 73867331909Sdougm * zfs_sa_find_share(handle, path) 73967331909Sdougm * 74067331909Sdougm * wrapper around sa_find_share to find a share path in the 74167331909Sdougm * configuration. 74267331909Sdougm */ 74367331909Sdougm static sa_share_t 74467331909Sdougm zfs_sa_find_share(sa_handle_t handle, char *path) 74567331909Sdougm { 74667331909Sdougm if (_sa_find_share != NULL) 747d8689a57Sdougm return (_sa_find_share(handle, path)); 74867331909Sdougm return (NULL); 74967331909Sdougm } 75067331909Sdougm 75167331909Sdougm /* 75267331909Sdougm * zfs_sa_enable_share(share, proto) 75367331909Sdougm * 75467331909Sdougm * Wrapper for sa_enable_share which enables a share for a specified 75567331909Sdougm * protocol. 75667331909Sdougm */ 75767331909Sdougm static int 75867331909Sdougm zfs_sa_enable_share(sa_share_t share, char *proto) 75967331909Sdougm { 76067331909Sdougm if (_sa_enable_share != NULL) 761d8689a57Sdougm return (_sa_enable_share(share, proto)); 76267331909Sdougm return (SA_CONFIG_ERR); 76367331909Sdougm } 76467331909Sdougm 76567331909Sdougm /* 76667331909Sdougm * zfs_sa_disable_share(share, proto) 76767331909Sdougm * 76867331909Sdougm * Wrapper for sa_enable_share which disables a share for a specified 76967331909Sdougm * protocol. 77067331909Sdougm */ 77167331909Sdougm static int 77267331909Sdougm zfs_sa_disable_share(sa_share_t share, char *proto) 77367331909Sdougm { 77467331909Sdougm if (_sa_disable_share != NULL) 775d8689a57Sdougm return (_sa_disable_share(share, proto)); 77667331909Sdougm return (SA_CONFIG_ERR); 77767331909Sdougm } 77867331909Sdougm 779fa9e4066Sahrens /* 780da6c28aaSamw * Share the given filesystem according to the options in the specified 781da6c28aaSamw * protocol specific properties (sharenfs, sharesmb). We rely 78267331909Sdougm * on "libshare" to the dirty work for us. 783fa9e4066Sahrens */ 784da6c28aaSamw static int 785da6c28aaSamw zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 786fa9e4066Sahrens { 787fa9e4066Sahrens char mountpoint[ZFS_MAXPROPLEN]; 788fa9e4066Sahrens char shareopts[ZFS_MAXPROPLEN]; 7895b6e0c46Sdougm char sourcestr[ZFS_MAXPROPLEN]; 79099653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 79167331909Sdougm sa_share_t share; 792da6c28aaSamw zfs_share_proto_t *curr_proto; 7935b6e0c46Sdougm zprop_source_t sourcetype; 79467331909Sdougm int ret; 795fa9e4066Sahrens 796e9dbad6fSeschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 797fa9e4066Sahrens return (0); 798fa9e4066Sahrens 799da6c28aaSamw for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 800da6c28aaSamw /* 801da6c28aaSamw * Return success if there are no share options. 802da6c28aaSamw */ 803da6c28aaSamw if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop, 8045b6e0c46Sdougm shareopts, sizeof (shareopts), &sourcetype, sourcestr, 8055b6e0c46Sdougm ZFS_MAXPROPLEN, B_FALSE) != 0 || 8065b6e0c46Sdougm strcmp(shareopts, "off") == 0) 807da6c28aaSamw continue; 8088a981c33SDaniel Hoffman ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE, 8098a981c33SDaniel Hoffman zhp); 81033cde0d0SMatthew Ahrens if (ret != SA_OK) { 81133cde0d0SMatthew Ahrens (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 81233cde0d0SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot share '%s': %s"), 81333cde0d0SMatthew Ahrens zfs_get_name(zhp), _sa_errorstr != NULL ? 81433cde0d0SMatthew Ahrens _sa_errorstr(ret) : ""); 81533cde0d0SMatthew Ahrens return (-1); 81633cde0d0SMatthew Ahrens } 81733cde0d0SMatthew Ahrens 818da6c28aaSamw /* 819da6c28aaSamw * If the 'zoned' property is set, then zfs_is_mountable() 820da6c28aaSamw * will have already bailed out if we are in the global zone. 821da6c28aaSamw * But local zones cannot be NFS servers, so we ignore it for 822da6c28aaSamw * local zones as well. 823da6c28aaSamw */ 824da6c28aaSamw if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 825da6c28aaSamw continue; 826da6c28aaSamw 827da6c28aaSamw share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint); 8285b6e0c46Sdougm if (share == NULL) { 8295b6e0c46Sdougm /* 8305b6e0c46Sdougm * This may be a new file system that was just 8315b6e0c46Sdougm * created so isn't in the internal cache 8325b6e0c46Sdougm * (second time through). Rather than 8335b6e0c46Sdougm * reloading the entire configuration, we can 8345b6e0c46Sdougm * assume ZFS has done the checking and it is 8355b6e0c46Sdougm * safe to add this to the internal 8365b6e0c46Sdougm * configuration. 8375b6e0c46Sdougm */ 8385b6e0c46Sdougm if (_sa_zfs_process_share(hdl->libzfs_sharehdl, 8395b6e0c46Sdougm NULL, NULL, mountpoint, 8405b6e0c46Sdougm proto_table[*curr_proto].p_name, sourcetype, 8415b6e0c46Sdougm shareopts, sourcestr, zhp->zfs_name) != SA_OK) { 8425b6e0c46Sdougm (void) zfs_error_fmt(hdl, 8435b6e0c46Sdougm proto_table[*curr_proto].p_share_err, 8445b6e0c46Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 8455b6e0c46Sdougm zfs_get_name(zhp)); 8465b6e0c46Sdougm return (-1); 8475b6e0c46Sdougm } 8485b6e0c46Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, 8495b6e0c46Sdougm mountpoint); 8505b6e0c46Sdougm } 851da6c28aaSamw if (share != NULL) { 852da6c28aaSamw int err; 853da6c28aaSamw err = zfs_sa_enable_share(share, 854da6c28aaSamw proto_table[*curr_proto].p_name); 855da6c28aaSamw if (err != SA_OK) { 856da6c28aaSamw (void) zfs_error_fmt(hdl, 857da6c28aaSamw proto_table[*curr_proto].p_share_err, 858da6c28aaSamw dgettext(TEXT_DOMAIN, "cannot share '%s'"), 859da6c28aaSamw zfs_get_name(zhp)); 860da6c28aaSamw return (-1); 861da6c28aaSamw } 862da6c28aaSamw } else { 863da6c28aaSamw (void) zfs_error_fmt(hdl, 864da6c28aaSamw proto_table[*curr_proto].p_share_err, 865d8689a57Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 866d8689a57Sdougm zfs_get_name(zhp)); 867d8689a57Sdougm return (-1); 868d8689a57Sdougm } 869fa9e4066Sahrens 870da6c28aaSamw } 871fa9e4066Sahrens return (0); 872fa9e4066Sahrens } 873fa9e4066Sahrens 874da6c28aaSamw 875da6c28aaSamw int 876da6c28aaSamw zfs_share_nfs(zfs_handle_t *zhp) 877da6c28aaSamw { 878da6c28aaSamw return (zfs_share_proto(zhp, nfs_only)); 879da6c28aaSamw } 880da6c28aaSamw 881da6c28aaSamw int 882da6c28aaSamw zfs_share_smb(zfs_handle_t *zhp) 883da6c28aaSamw { 884da6c28aaSamw return (zfs_share_proto(zhp, smb_only)); 885da6c28aaSamw } 886da6c28aaSamw 887da6c28aaSamw int 888da6c28aaSamw zfs_shareall(zfs_handle_t *zhp) 889da6c28aaSamw { 890da6c28aaSamw return (zfs_share_proto(zhp, share_all_proto)); 891da6c28aaSamw } 892da6c28aaSamw 8933bb79becSeschrock /* 8943bb79becSeschrock * Unshare a filesystem by mountpoint. 8953bb79becSeschrock */ 8963bb79becSeschrock static int 897da6c28aaSamw unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, 898da6c28aaSamw zfs_share_proto_t proto) 8993bb79becSeschrock { 90067331909Sdougm sa_share_t share; 90167331909Sdougm int err; 90267331909Sdougm char *mntpt; 9038a981c33SDaniel Hoffman 9043bb79becSeschrock /* 90567331909Sdougm * Mountpoint could get trashed if libshare calls getmntany 906ebedde84SEric Taylor * which it does during API initialization, so strdup the 90767331909Sdougm * value. 9083bb79becSeschrock */ 90967331909Sdougm mntpt = zfs_strdup(hdl, mountpoint); 9103bb79becSeschrock 9118a981c33SDaniel Hoffman /* 9128a981c33SDaniel Hoffman * make sure libshare initialized, initialize everything because we 9138a981c33SDaniel Hoffman * don't know what other unsharing may happen later. Functions up the 9148a981c33SDaniel Hoffman * stack are allowed to initialize instead a subset of shares at the 9158a981c33SDaniel Hoffman * time the set is known. 9168a981c33SDaniel Hoffman */ 9178a981c33SDaniel Hoffman if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME, 9188a981c33SDaniel Hoffman (void *)name)) != SA_OK) { 91967331909Sdougm free(mntpt); /* don't need the copy anymore */ 9204f4378ccSAndrew Stormont return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err, 921d8689a57Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 922d8689a57Sdougm name, _sa_errorstr(err))); 92367331909Sdougm } 9243bb79becSeschrock 92567331909Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt); 92667331909Sdougm free(mntpt); /* don't need the copy anymore */ 9273bb79becSeschrock 92867331909Sdougm if (share != NULL) { 929da6c28aaSamw err = zfs_sa_disable_share(share, proto_table[proto].p_name); 93067331909Sdougm if (err != SA_OK) { 9314f4378ccSAndrew Stormont return (zfs_error_fmt(hdl, 9324f4378ccSAndrew Stormont proto_table[proto].p_unshare_err, 933d8689a57Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 934d8689a57Sdougm name, _sa_errorstr(err))); 93567331909Sdougm } 93667331909Sdougm } else { 9374f4378ccSAndrew Stormont return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err, 938d8689a57Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"), 939d8689a57Sdougm name)); 9403bb79becSeschrock } 9413bb79becSeschrock return (0); 9423bb79becSeschrock } 9433bb79becSeschrock 944fa9e4066Sahrens /* 945fa9e4066Sahrens * Unshare the given filesystem. 946fa9e4066Sahrens */ 947fa9e4066Sahrens int 948da6c28aaSamw zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, 949da6c28aaSamw zfs_share_proto_t *proto) 950fa9e4066Sahrens { 951ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 952ebedde84SEric Taylor struct mnttab entry; 95367331909Sdougm char *mntpt = NULL; 954fa9e4066Sahrens 955fa9e4066Sahrens /* check to see if need to unmount the filesystem */ 95699653d4eSeschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 957d8689a57Sdougm if (mountpoint != NULL) 958ebedde84SEric Taylor mountpoint = mntpt = zfs_strdup(hdl, mountpoint); 959d8689a57Sdougm 960fa9e4066Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 961ebedde84SEric Taylor libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) { 962da6c28aaSamw zfs_share_proto_t *curr_proto; 963fa9e4066Sahrens 964fa9e4066Sahrens if (mountpoint == NULL) 965da6c28aaSamw mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 966fa9e4066Sahrens 967da6c28aaSamw for (curr_proto = proto; *curr_proto != PROTO_END; 968da6c28aaSamw curr_proto++) { 969da6c28aaSamw 970ebedde84SEric Taylor if (is_shared(hdl, mntpt, *curr_proto) && 971ebedde84SEric Taylor unshare_one(hdl, zhp->zfs_name, 972da6c28aaSamw mntpt, *curr_proto) != 0) { 973da6c28aaSamw if (mntpt != NULL) 974da6c28aaSamw free(mntpt); 975da6c28aaSamw return (-1); 976da6c28aaSamw } 97767331909Sdougm } 978fa9e4066Sahrens } 97967331909Sdougm if (mntpt != NULL) 980d8689a57Sdougm free(mntpt); 981fa9e4066Sahrens 982fa9e4066Sahrens return (0); 983fa9e4066Sahrens } 984fa9e4066Sahrens 985da6c28aaSamw int 986da6c28aaSamw zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 987da6c28aaSamw { 988da6c28aaSamw return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 989da6c28aaSamw } 990da6c28aaSamw 991da6c28aaSamw int 992da6c28aaSamw zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint) 993da6c28aaSamw { 994da6c28aaSamw return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 995da6c28aaSamw } 996da6c28aaSamw 997fa9e4066Sahrens /* 998da6c28aaSamw * Same as zfs_unmountall(), but for NFS and SMB unshares. 999fa9e4066Sahrens */ 1000fa9e4066Sahrens int 1001da6c28aaSamw zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 1002fa9e4066Sahrens { 1003fa9e4066Sahrens prop_changelist_t *clp; 1004fa9e4066Sahrens int ret; 1005fa9e4066Sahrens 10060069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0); 1007fa9e4066Sahrens if (clp == NULL) 1008fa9e4066Sahrens return (-1); 1009fa9e4066Sahrens 1010da6c28aaSamw ret = changelist_unshare(clp, proto); 1011fa9e4066Sahrens changelist_free(clp); 1012fa9e4066Sahrens 1013fa9e4066Sahrens return (ret); 1014fa9e4066Sahrens } 1015fa9e4066Sahrens 1016da6c28aaSamw int 1017da6c28aaSamw zfs_unshareall_nfs(zfs_handle_t *zhp) 1018da6c28aaSamw { 1019da6c28aaSamw return (zfs_unshareall_proto(zhp, nfs_only)); 1020da6c28aaSamw } 1021da6c28aaSamw 1022da6c28aaSamw int 1023da6c28aaSamw zfs_unshareall_smb(zfs_handle_t *zhp) 1024da6c28aaSamw { 1025da6c28aaSamw return (zfs_unshareall_proto(zhp, smb_only)); 1026da6c28aaSamw } 1027da6c28aaSamw 1028da6c28aaSamw int 1029da6c28aaSamw zfs_unshareall(zfs_handle_t *zhp) 1030da6c28aaSamw { 1031da6c28aaSamw return (zfs_unshareall_proto(zhp, share_all_proto)); 1032da6c28aaSamw } 1033da6c28aaSamw 1034da6c28aaSamw int 1035da6c28aaSamw zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint) 1036da6c28aaSamw { 1037da6c28aaSamw return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 1038da6c28aaSamw } 1039da6c28aaSamw 1040fa9e4066Sahrens /* 1041fa9e4066Sahrens * Remove the mountpoint associated with the current dataset, if necessary. 1042fa9e4066Sahrens * We only remove the underlying directory if: 1043fa9e4066Sahrens * 1044fa9e4066Sahrens * - The mountpoint is not 'none' or 'legacy' 1045fa9e4066Sahrens * - The mountpoint is non-empty 1046fa9e4066Sahrens * - The mountpoint is the default or inherited 1047fa9e4066Sahrens * - The 'zoned' property is set, or we're in a local zone 1048fa9e4066Sahrens * 1049fa9e4066Sahrens * Any other directories we leave alone. 1050fa9e4066Sahrens */ 1051fa9e4066Sahrens void 1052fa9e4066Sahrens remove_mountpoint(zfs_handle_t *zhp) 1053fa9e4066Sahrens { 1054fa9e4066Sahrens char mountpoint[ZFS_MAXPROPLEN]; 1055990b4856Slling zprop_source_t source; 1056fa9e4066Sahrens 1057e9dbad6fSeschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 1058e9dbad6fSeschrock &source)) 1059fa9e4066Sahrens return; 1060fa9e4066Sahrens 1061990b4856Slling if (source == ZPROP_SRC_DEFAULT || 1062990b4856Slling source == ZPROP_SRC_INHERITED) { 1063fa9e4066Sahrens /* 1064fa9e4066Sahrens * Try to remove the directory, silently ignoring any errors. 1065fa9e4066Sahrens * The filesystem may have since been removed or moved around, 1066e9dbad6fSeschrock * and this error isn't really useful to the administrator in 1067e9dbad6fSeschrock * any way. 1068fa9e4066Sahrens */ 1069fa9e4066Sahrens (void) rmdir(mountpoint); 1070fa9e4066Sahrens } 1071fa9e4066Sahrens } 10723bb79becSeschrock 10739d9a58e3SEric Taylor void 10749d9a58e3SEric Taylor libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp) 10759d9a58e3SEric Taylor { 10769d9a58e3SEric Taylor if (cbp->cb_alloc == cbp->cb_used) { 10779d9a58e3SEric Taylor size_t newsz; 10789d9a58e3SEric Taylor void *ptr; 10799d9a58e3SEric Taylor 10809d9a58e3SEric Taylor newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64; 10819d9a58e3SEric Taylor ptr = zfs_realloc(zhp->zfs_hdl, 10829d9a58e3SEric Taylor cbp->cb_handles, cbp->cb_alloc * sizeof (void *), 10839d9a58e3SEric Taylor newsz * sizeof (void *)); 10849d9a58e3SEric Taylor cbp->cb_handles = ptr; 10859d9a58e3SEric Taylor cbp->cb_alloc = newsz; 10869d9a58e3SEric Taylor } 10879d9a58e3SEric Taylor cbp->cb_handles[cbp->cb_used++] = zhp; 10889d9a58e3SEric Taylor } 10893bb79becSeschrock 10903bb79becSeschrock static int 10913bb79becSeschrock mount_cb(zfs_handle_t *zhp, void *data) 10923bb79becSeschrock { 10939d9a58e3SEric Taylor get_all_cb_t *cbp = data; 10943bb79becSeschrock 10959d9a58e3SEric Taylor if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) { 10963bb79becSeschrock zfs_close(zhp); 10973bb79becSeschrock return (0); 10983bb79becSeschrock } 10993bb79becSeschrock 1100a227b7f4Shs if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) { 1101a227b7f4Shs zfs_close(zhp); 1102a227b7f4Shs return (0); 1103a227b7f4Shs } 1104a227b7f4Shs 11059c3fd121SMatthew Ahrens /* 11069c3fd121SMatthew Ahrens * If this filesystem is inconsistent and has a receive resume 11079c3fd121SMatthew Ahrens * token, we can not mount it. 11089c3fd121SMatthew Ahrens */ 11099c3fd121SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) && 11109c3fd121SMatthew Ahrens zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 11119c3fd121SMatthew Ahrens NULL, 0, NULL, NULL, 0, B_TRUE) == 0) { 11129c3fd121SMatthew Ahrens zfs_close(zhp); 11139c3fd121SMatthew Ahrens return (0); 11149c3fd121SMatthew Ahrens } 11159c3fd121SMatthew Ahrens 11169d9a58e3SEric Taylor libzfs_add_handle(cbp, zhp); 11179d9a58e3SEric Taylor if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) { 11189d9a58e3SEric Taylor zfs_close(zhp); 11199d9a58e3SEric Taylor return (-1); 11203bb79becSeschrock } 11219d9a58e3SEric Taylor return (0); 11223bb79becSeschrock } 11233bb79becSeschrock 11249d9a58e3SEric Taylor int 11259d9a58e3SEric Taylor libzfs_dataset_cmp(const void *a, const void *b) 11263bb79becSeschrock { 11273bb79becSeschrock zfs_handle_t **za = (zfs_handle_t **)a; 11283bb79becSeschrock zfs_handle_t **zb = (zfs_handle_t **)b; 11293bb79becSeschrock char mounta[MAXPATHLEN]; 11303bb79becSeschrock char mountb[MAXPATHLEN]; 1131f3861e1aSahl boolean_t gota, gotb; 1132f3861e1aSahl 1133f3861e1aSahl if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 1134f3861e1aSahl verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 1135f3861e1aSahl sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 1136f3861e1aSahl if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 1137f3861e1aSahl verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 1138f3861e1aSahl sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 1139f3861e1aSahl 1140f3861e1aSahl if (gota && gotb) 1141f3861e1aSahl return (strcmp(mounta, mountb)); 11423bb79becSeschrock 1143f3861e1aSahl if (gota) 1144f3861e1aSahl return (-1); 1145f3861e1aSahl if (gotb) 1146f3861e1aSahl return (1); 11473bb79becSeschrock 1148f3861e1aSahl return (strcmp(zfs_get_name(a), zfs_get_name(b))); 11493bb79becSeschrock } 11503bb79becSeschrock 1151f3861e1aSahl /* 1152f3861e1aSahl * Mount and share all datasets within the given pool. This assumes that no 1153f3861e1aSahl * datasets within the pool are currently mounted. Because users can create 1154f3861e1aSahl * complicated nested hierarchies of mountpoints, we first gather all the 1155f3861e1aSahl * datasets and mountpoints within the pool, and sort them by mountpoint. Once 1156f3861e1aSahl * we have the list of all filesystems, we iterate over them in order and mount 1157f3861e1aSahl * and/or share each one. 1158f3861e1aSahl */ 1159f3861e1aSahl #pragma weak zpool_mount_datasets = zpool_enable_datasets 11603bb79becSeschrock int 1161f3861e1aSahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 11623bb79becSeschrock { 11639d9a58e3SEric Taylor get_all_cb_t cb = { 0 }; 11643bb79becSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 11653bb79becSeschrock zfs_handle_t *zfsp; 11663bb79becSeschrock int i, ret = -1; 116767331909Sdougm int *good; 11683bb79becSeschrock 11693bb79becSeschrock /* 1170d87468daSrm * Gather all non-snap datasets within the pool. 11713bb79becSeschrock */ 1172990b4856Slling if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL) 11733bb79becSeschrock goto out; 11743bb79becSeschrock 11759d9a58e3SEric Taylor libzfs_add_handle(&cb, zfsp); 1176d87468daSrm if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0) 11773bb79becSeschrock goto out; 11783bb79becSeschrock /* 11793bb79becSeschrock * Sort the datasets by mountpoint. 11803bb79becSeschrock */ 11819d9a58e3SEric Taylor qsort(cb.cb_handles, cb.cb_used, sizeof (void *), 11829d9a58e3SEric Taylor libzfs_dataset_cmp); 11833bb79becSeschrock 11843bb79becSeschrock /* 118567331909Sdougm * And mount all the datasets, keeping track of which ones 11866e77af0aSDavid Pacheco * succeeded or failed. 11873bb79becSeschrock */ 11886e77af0aSDavid Pacheco if ((good = zfs_alloc(zhp->zpool_hdl, 11896e77af0aSDavid Pacheco cb.cb_used * sizeof (int))) == NULL) 11906e77af0aSDavid Pacheco goto out; 11916e77af0aSDavid Pacheco 11923bb79becSeschrock ret = 0; 11933bb79becSeschrock for (i = 0; i < cb.cb_used; i++) { 11949d9a58e3SEric Taylor if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0) 11953bb79becSeschrock ret = -1; 1196d8689a57Sdougm else 119767331909Sdougm good[i] = 1; 11983bb79becSeschrock } 11995b6e0c46Sdougm 120067331909Sdougm /* 120167331909Sdougm * Then share all the ones that need to be shared. This needs 120267331909Sdougm * to be a separate pass in order to avoid excessive reloading 120367331909Sdougm * of the configuration. Good should never be NULL since 120467331909Sdougm * zfs_alloc is supposed to exit if memory isn't available. 120567331909Sdougm */ 120667331909Sdougm for (i = 0; i < cb.cb_used; i++) { 12079d9a58e3SEric Taylor if (good[i] && zfs_share(cb.cb_handles[i]) != 0) 120867331909Sdougm ret = -1; 120967331909Sdougm } 121067331909Sdougm 121167331909Sdougm free(good); 12123bb79becSeschrock 12133bb79becSeschrock out: 12143bb79becSeschrock for (i = 0; i < cb.cb_used; i++) 12159d9a58e3SEric Taylor zfs_close(cb.cb_handles[i]); 12169d9a58e3SEric Taylor free(cb.cb_handles); 12173bb79becSeschrock 12183bb79becSeschrock return (ret); 12193bb79becSeschrock } 12203bb79becSeschrock 12213bb79becSeschrock static int 12223bb79becSeschrock mountpoint_compare(const void *a, const void *b) 12233bb79becSeschrock { 12243bb79becSeschrock const char *mounta = *((char **)a); 12253bb79becSeschrock const char *mountb = *((char **)b); 12263bb79becSeschrock 12273bb79becSeschrock return (strcmp(mountb, mounta)); 12283bb79becSeschrock } 12293bb79becSeschrock 1230681d9761SEric Taylor /* alias for 2002/240 */ 1231681d9761SEric Taylor #pragma weak zpool_unmount_datasets = zpool_disable_datasets 1232f3861e1aSahl /* 1233f3861e1aSahl * Unshare and unmount all datasets within the given pool. We don't want to 1234f3861e1aSahl * rely on traversing the DSL to discover the filesystems within the pool, 1235f3861e1aSahl * because this may be expensive (if not all of them are mounted), and can fail 1236f3861e1aSahl * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 1237f3861e1aSahl * gather all the filesystems that are currently mounted. 1238f3861e1aSahl */ 12393bb79becSeschrock int 1240f3861e1aSahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 12413bb79becSeschrock { 12423bb79becSeschrock int used, alloc; 12433bb79becSeschrock struct mnttab entry; 12443bb79becSeschrock size_t namelen; 12453bb79becSeschrock char **mountpoints = NULL; 12463bb79becSeschrock zfs_handle_t **datasets = NULL; 12473bb79becSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 12483bb79becSeschrock int i; 12493bb79becSeschrock int ret = -1; 12503bb79becSeschrock int flags = (force ? MS_FORCE : 0); 12518a981c33SDaniel Hoffman sa_init_selective_arg_t sharearg; 12523bb79becSeschrock 12533bb79becSeschrock namelen = strlen(zhp->zpool_name); 12543bb79becSeschrock 12553bb79becSeschrock rewind(hdl->libzfs_mnttab); 12563bb79becSeschrock used = alloc = 0; 12573bb79becSeschrock while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 12583bb79becSeschrock /* 12593bb79becSeschrock * Ignore non-ZFS entries. 12603bb79becSeschrock */ 12613bb79becSeschrock if (entry.mnt_fstype == NULL || 12623bb79becSeschrock strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 12633bb79becSeschrock continue; 12643bb79becSeschrock 12653bb79becSeschrock /* 12663bb79becSeschrock * Ignore filesystems not within this pool. 12673bb79becSeschrock */ 12683bb79becSeschrock if (entry.mnt_mountp == NULL || 12693bb79becSeschrock strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 12703bb79becSeschrock (entry.mnt_special[namelen] != '/' && 12713bb79becSeschrock entry.mnt_special[namelen] != '\0')) 12723bb79becSeschrock continue; 12733bb79becSeschrock 12743bb79becSeschrock /* 12753bb79becSeschrock * At this point we've found a filesystem within our pool. Add 12763bb79becSeschrock * it to our growing list. 12773bb79becSeschrock */ 12783bb79becSeschrock if (used == alloc) { 12793bb79becSeschrock if (alloc == 0) { 12803bb79becSeschrock if ((mountpoints = zfs_alloc(hdl, 12813bb79becSeschrock 8 * sizeof (void *))) == NULL) 12823bb79becSeschrock goto out; 12833bb79becSeschrock 12843bb79becSeschrock if ((datasets = zfs_alloc(hdl, 12853bb79becSeschrock 8 * sizeof (void *))) == NULL) 12863bb79becSeschrock goto out; 12873bb79becSeschrock 12883bb79becSeschrock alloc = 8; 12893bb79becSeschrock } else { 1290e9dbad6fSeschrock void *ptr; 12913bb79becSeschrock 1292e9dbad6fSeschrock if ((ptr = zfs_realloc(hdl, mountpoints, 1293e9dbad6fSeschrock alloc * sizeof (void *), 12943bb79becSeschrock alloc * 2 * sizeof (void *))) == NULL) 12953bb79becSeschrock goto out; 1296e9dbad6fSeschrock mountpoints = ptr; 12973bb79becSeschrock 1298e9dbad6fSeschrock if ((ptr = zfs_realloc(hdl, datasets, 1299e9dbad6fSeschrock alloc * sizeof (void *), 13003bb79becSeschrock alloc * 2 * sizeof (void *))) == NULL) 13013bb79becSeschrock goto out; 1302e9dbad6fSeschrock datasets = ptr; 13033bb79becSeschrock 13043bb79becSeschrock alloc *= 2; 13053bb79becSeschrock } 13063bb79becSeschrock } 13073bb79becSeschrock 13083bb79becSeschrock if ((mountpoints[used] = zfs_strdup(hdl, 13093bb79becSeschrock entry.mnt_mountp)) == NULL) 13103bb79becSeschrock goto out; 13113bb79becSeschrock 13123bb79becSeschrock /* 13133bb79becSeschrock * This is allowed to fail, in case there is some I/O error. It 13143bb79becSeschrock * is only used to determine if we need to remove the underlying 13153bb79becSeschrock * mountpoint, so failure is not fatal. 13163bb79becSeschrock */ 13173bb79becSeschrock datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 13183bb79becSeschrock 13193bb79becSeschrock used++; 13203bb79becSeschrock } 13213bb79becSeschrock 13223bb79becSeschrock /* 13233bb79becSeschrock * At this point, we have the entire list of filesystems, so sort it by 13243bb79becSeschrock * mountpoint. 13253bb79becSeschrock */ 13268a981c33SDaniel Hoffman sharearg.zhandle_arr = datasets; 13278a981c33SDaniel Hoffman sharearg.zhandle_len = used; 13288a981c33SDaniel Hoffman ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE, 13298a981c33SDaniel Hoffman &sharearg); 13308a981c33SDaniel Hoffman if (ret != 0) 13318a981c33SDaniel Hoffman goto out; 13323bb79becSeschrock qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 13333bb79becSeschrock 13343bb79becSeschrock /* 13353bb79becSeschrock * Walk through and first unshare everything. 13363bb79becSeschrock */ 13373bb79becSeschrock for (i = 0; i < used; i++) { 1338da6c28aaSamw zfs_share_proto_t *curr_proto; 1339da6c28aaSamw for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 1340da6c28aaSamw curr_proto++) { 1341da6c28aaSamw if (is_shared(hdl, mountpoints[i], *curr_proto) && 1342da6c28aaSamw unshare_one(hdl, mountpoints[i], 1343da6c28aaSamw mountpoints[i], *curr_proto) != 0) 1344da6c28aaSamw goto out; 1345da6c28aaSamw } 13463bb79becSeschrock } 13473bb79becSeschrock 13483bb79becSeschrock /* 13493bb79becSeschrock * Now unmount everything, removing the underlying directories as 13503bb79becSeschrock * appropriate. 13513bb79becSeschrock */ 13523bb79becSeschrock for (i = 0; i < used; i++) { 13533bb79becSeschrock if (unmount_one(hdl, mountpoints[i], flags) != 0) 13543bb79becSeschrock goto out; 1355e9dbad6fSeschrock } 13563bb79becSeschrock 1357e9dbad6fSeschrock for (i = 0; i < used; i++) { 13583bb79becSeschrock if (datasets[i]) 13593bb79becSeschrock remove_mountpoint(datasets[i]); 13603bb79becSeschrock } 13613bb79becSeschrock 13623bb79becSeschrock ret = 0; 13633bb79becSeschrock out: 13643bb79becSeschrock for (i = 0; i < used; i++) { 13653bb79becSeschrock if (datasets[i]) 13663bb79becSeschrock zfs_close(datasets[i]); 13673bb79becSeschrock free(mountpoints[i]); 13683bb79becSeschrock } 13693bb79becSeschrock free(datasets); 13703bb79becSeschrock free(mountpoints); 13713bb79becSeschrock 13723bb79becSeschrock return (ret); 13733bb79becSeschrock } 1374