1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 213126Sahl 22789Sahrens /* 233380Sgw25295 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens /* 30789Sahrens * Routines to manage ZFS mounts. We separate all the nasty routines that have 313126Sahl * to deal with the OS. The following functions are the main entry points -- 323126Sahl * they are used by mount and unmount and when changing a filesystem's 333126Sahl * mountpoint. 34789Sahrens * 35789Sahrens * zfs_is_mounted() 36789Sahrens * zfs_mount() 37789Sahrens * zfs_unmount() 38789Sahrens * zfs_unmountall() 39789Sahrens * 403126Sahl * This file also contains the functions used to manage sharing filesystems via 413126Sahl * NFS and iSCSI: 42789Sahrens * 43789Sahrens * zfs_is_shared() 44789Sahrens * zfs_share() 45789Sahrens * zfs_unshare() 463126Sahl * 473126Sahl * zfs_is_shared_nfs() 483126Sahl * zfs_share_nfs() 493126Sahl * zfs_unshare_nfs() 503126Sahl * zfs_unshareall_nfs() 513126Sahl * zfs_is_shared_iscsi() 523126Sahl * zfs_share_iscsi() 533126Sahl * zfs_unshare_iscsi() 542474Seschrock * 552474Seschrock * The following functions are available for pool consumers, and will 563126Sahl * mount/unmount and share/unshare all datasets within pool: 572474Seschrock * 583126Sahl * zpool_enable_datasets() 593126Sahl * zpool_disable_datasets() 60789Sahrens */ 61789Sahrens 62789Sahrens #include <dirent.h> 633134Sahl #include <dlfcn.h> 64789Sahrens #include <errno.h> 65789Sahrens #include <libgen.h> 66789Sahrens #include <libintl.h> 67789Sahrens #include <stdio.h> 68789Sahrens #include <stdlib.h> 69789Sahrens #include <strings.h> 70789Sahrens #include <unistd.h> 71789Sahrens #include <zone.h> 72789Sahrens #include <sys/mntent.h> 73789Sahrens #include <sys/mnttab.h> 74789Sahrens #include <sys/mount.h> 75789Sahrens #include <sys/stat.h> 76789Sahrens 77789Sahrens #include <libzfs.h> 78789Sahrens 79789Sahrens #include "libzfs_impl.h" 80789Sahrens 814180Sdougm #include <libshare.h> 824180Sdougm #include <sys/systeminfo.h> 834180Sdougm #define MAXISALEN 257 /* based on sysinfo(2) man page */ 844180Sdougm 853134Sahl static int (*iscsitgt_zfs_share)(const char *); 863134Sahl static int (*iscsitgt_zfs_unshare)(const char *); 873134Sahl static int (*iscsitgt_zfs_is_shared)(const char *); 883134Sahl 893134Sahl #pragma init(zfs_iscsi_init) 903134Sahl static void 913134Sahl zfs_iscsi_init(void) 923134Sahl { 933134Sahl void *libiscsitgt; 943134Sahl 953134Sahl if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1", 963134Sahl RTLD_LAZY | RTLD_GLOBAL)) == NULL || 973134Sahl (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt, 983134Sahl "iscsitgt_zfs_share")) == NULL || 993134Sahl (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt, 1003134Sahl "iscsitgt_zfs_unshare")) == NULL || 1013134Sahl (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt, 1023134Sahl "iscsitgt_zfs_is_shared")) == NULL) { 1033134Sahl iscsitgt_zfs_share = NULL; 1043134Sahl iscsitgt_zfs_unshare = NULL; 1053134Sahl iscsitgt_zfs_is_shared = NULL; 1063134Sahl } 1073134Sahl } 1083134Sahl 109789Sahrens /* 1102082Seschrock * Search the sharetab for the given mountpoint, returning true if it is found. 111789Sahrens */ 1122082Seschrock static boolean_t 1132082Seschrock is_shared(libzfs_handle_t *hdl, const char *mountpoint) 114789Sahrens { 115789Sahrens char buf[MAXPATHLEN], *tab; 116789Sahrens 1172082Seschrock if (hdl->libzfs_sharetab == NULL) 118789Sahrens return (0); 119789Sahrens 1202082Seschrock (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET); 121789Sahrens 1222082Seschrock while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) { 123789Sahrens 124789Sahrens /* the mountpoint is the first entry on each line */ 125789Sahrens if ((tab = strchr(buf, '\t')) != NULL) { 126789Sahrens *tab = '\0'; 127789Sahrens if (strcmp(buf, mountpoint) == 0) 1282082Seschrock return (B_TRUE); 129789Sahrens } 130789Sahrens } 131789Sahrens 1322082Seschrock return (B_FALSE); 133789Sahrens } 134789Sahrens 135789Sahrens /* 1362082Seschrock * Returns true if the specified directory is empty. If we can't open the 1372082Seschrock * directory at all, return true so that the mount can fail with a more 138789Sahrens * informative error message. 139789Sahrens */ 1402082Seschrock static boolean_t 141789Sahrens dir_is_empty(const char *dirname) 142789Sahrens { 143789Sahrens DIR *dirp; 144789Sahrens struct dirent64 *dp; 145789Sahrens 146789Sahrens if ((dirp = opendir(dirname)) == NULL) 1472082Seschrock return (B_TRUE); 148789Sahrens 149789Sahrens while ((dp = readdir64(dirp)) != NULL) { 150789Sahrens 151789Sahrens if (strcmp(dp->d_name, ".") == 0 || 152789Sahrens strcmp(dp->d_name, "..") == 0) 153789Sahrens continue; 154789Sahrens 155789Sahrens (void) closedir(dirp); 1562082Seschrock return (B_FALSE); 157789Sahrens } 158789Sahrens 159789Sahrens (void) closedir(dirp); 1602082Seschrock return (B_TRUE); 161789Sahrens } 162789Sahrens 163789Sahrens /* 164789Sahrens * Checks to see if the mount is active. If the filesystem is mounted, we fill 165789Sahrens * in 'where' with the current mountpoint, and return 1. Otherwise, we return 166789Sahrens * 0. 167789Sahrens */ 1682082Seschrock boolean_t 1693444Sek110237 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 170789Sahrens { 171789Sahrens struct mnttab search = { 0 }, entry; 172789Sahrens 173789Sahrens /* 174789Sahrens * Search for the entry in /etc/mnttab. We don't bother getting the 175789Sahrens * mountpoint, as we can just search for the special device. This will 176789Sahrens * also let us find mounts when the mountpoint is 'legacy'. 177789Sahrens */ 1783444Sek110237 search.mnt_special = (char *)special; 1791407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 180789Sahrens 1813444Sek110237 rewind(zfs_hdl->libzfs_mnttab); 1823444Sek110237 if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0) 1832082Seschrock return (B_FALSE); 184789Sahrens 185789Sahrens if (where != NULL) 1863444Sek110237 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 187789Sahrens 1882082Seschrock return (B_TRUE); 189789Sahrens } 190789Sahrens 1913444Sek110237 boolean_t 1923444Sek110237 zfs_is_mounted(zfs_handle_t *zhp, char **where) 1933444Sek110237 { 1943444Sek110237 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 1953444Sek110237 } 1963444Sek110237 197789Sahrens /* 1982676Seschrock * Returns true if the given dataset is mountable, false otherwise. Returns the 1992676Seschrock * mountpoint in 'buf'. 2002676Seschrock */ 2012676Seschrock static boolean_t 2022676Seschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 2032676Seschrock zfs_source_t *source) 2042676Seschrock { 2052676Seschrock char sourceloc[ZFS_MAXNAMELEN]; 2062676Seschrock zfs_source_t sourcetype; 2072676Seschrock 2082676Seschrock if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 2092676Seschrock return (B_FALSE); 2102676Seschrock 2112676Seschrock verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 2122676Seschrock &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 2132676Seschrock 2142676Seschrock if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 2152676Seschrock strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 2162676Seschrock return (B_FALSE); 2172676Seschrock 2182676Seschrock if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT)) 2192676Seschrock return (B_FALSE); 2202676Seschrock 2212676Seschrock if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2222676Seschrock getzoneid() == GLOBAL_ZONEID) 2232676Seschrock return (B_FALSE); 2242676Seschrock 2252676Seschrock if (source) 2262676Seschrock *source = sourcetype; 2272676Seschrock 2282676Seschrock return (B_TRUE); 2292676Seschrock } 2302676Seschrock 2312676Seschrock /* 232789Sahrens * Mount the given filesystem. 233789Sahrens */ 234789Sahrens int 235789Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 236789Sahrens { 237789Sahrens struct stat buf; 238789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 239789Sahrens char mntopts[MNT_LINE_MAX]; 2402082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 241789Sahrens 242789Sahrens if (options == NULL) 243789Sahrens mntopts[0] = '\0'; 244789Sahrens else 245789Sahrens (void) strlcpy(mntopts, options, sizeof (mntopts)); 246789Sahrens 2472676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 2482082Seschrock return (0); 249789Sahrens 250789Sahrens /* Create the directory if it doesn't already exist */ 251789Sahrens if (lstat(mountpoint, &buf) != 0) { 252789Sahrens if (mkdirp(mountpoint, 0755) != 0) { 2532082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2542082Seschrock "failed to create mountpoint")); 2553237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 2562082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 2572082Seschrock mountpoint)); 258789Sahrens } 259789Sahrens } 260789Sahrens 261789Sahrens /* 262789Sahrens * Determine if the mountpoint is empty. If so, refuse to perform the 263789Sahrens * mount. We don't perform this check if MS_OVERLAY is specified, which 264789Sahrens * would defeat the point. We also avoid this check if 'remount' is 265789Sahrens * specified. 266789Sahrens */ 267789Sahrens if ((flags & MS_OVERLAY) == 0 && 268789Sahrens strstr(mntopts, MNTOPT_REMOUNT) == NULL && 269789Sahrens !dir_is_empty(mountpoint)) { 2702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2712082Seschrock "directory is not empty")); 2723237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 2732082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 274789Sahrens } 275789Sahrens 276789Sahrens /* perform the mount */ 277789Sahrens if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 278789Sahrens MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 279789Sahrens /* 280789Sahrens * Generic errors are nasty, but there are just way too many 281789Sahrens * from mount(), and they're well-understood. We pick a few 282789Sahrens * common ones to improve upon. 283789Sahrens */ 2844302Sdougm if (errno == EBUSY) { 2852082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2862082Seschrock "mountpoint or dataset is busy")); 2874302Sdougm } else { 2882082Seschrock zfs_error_aux(hdl, strerror(errno)); 2894302Sdougm } 2902082Seschrock 2913237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 2922082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 2932082Seschrock zhp->zfs_name)); 294789Sahrens } 295789Sahrens 296789Sahrens return (0); 297789Sahrens } 298789Sahrens 299789Sahrens /* 3002474Seschrock * Unmount a single filesystem. 3012474Seschrock */ 3022474Seschrock static int 3032474Seschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 3042474Seschrock { 3052474Seschrock if (umount2(mountpoint, flags) != 0) { 3062474Seschrock zfs_error_aux(hdl, strerror(errno)); 3073237Slling return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 3082474Seschrock dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 3092474Seschrock mountpoint)); 3102474Seschrock } 3112474Seschrock 3122474Seschrock return (0); 3132474Seschrock } 3142474Seschrock 3152474Seschrock /* 316789Sahrens * Unmount the given filesystem. 317789Sahrens */ 318789Sahrens int 319789Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 320789Sahrens { 321789Sahrens struct mnttab search = { 0 }, entry; 3224180Sdougm char *mntpt = NULL; 323789Sahrens 324789Sahrens /* check to see if need to unmount the filesystem */ 3252474Seschrock search.mnt_special = zhp->zfs_name; 3261407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 3272082Seschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 328789Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 3292082Seschrock getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 330789Sahrens 3314180Sdougm /* 3324180Sdougm * mountpoint may have come from a call to 3334180Sdougm * getmnt/getmntany if it isn't NULL. If it is NULL, 3344180Sdougm * we know it comes from getmntany which can then get 3354180Sdougm * overwritten later. We strdup it to play it safe. 3364180Sdougm */ 337789Sahrens if (mountpoint == NULL) 3384302Sdougm mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 3394180Sdougm else 3404302Sdougm mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint); 341789Sahrens 342789Sahrens /* 3432474Seschrock * Unshare and unmount the filesystem 344789Sahrens */ 3454180Sdougm if (zfs_unshare_nfs(zhp, mntpt) != 0 || 3464180Sdougm unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) { 3474180Sdougm free(mntpt); 348789Sahrens return (-1); 3494180Sdougm } 3504180Sdougm free(mntpt); 351789Sahrens } 352789Sahrens 353789Sahrens return (0); 354789Sahrens } 355789Sahrens 356789Sahrens /* 357789Sahrens * Unmount this filesystem and any children inheriting the mountpoint property. 358789Sahrens * To do this, just act like we're changing the mountpoint property, but don't 359789Sahrens * remount the filesystems afterwards. 360789Sahrens */ 361789Sahrens int 362789Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags) 363789Sahrens { 364789Sahrens prop_changelist_t *clp; 365789Sahrens int ret; 366789Sahrens 367789Sahrens clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags); 368789Sahrens if (clp == NULL) 369789Sahrens return (-1); 370789Sahrens 371789Sahrens ret = changelist_prefix(clp); 372789Sahrens changelist_free(clp); 373789Sahrens 374789Sahrens return (ret); 375789Sahrens } 376789Sahrens 3773126Sahl boolean_t 3783126Sahl zfs_is_shared(zfs_handle_t *zhp) 3793126Sahl { 3803126Sahl if (ZFS_IS_VOLUME(zhp)) 3813126Sahl return (zfs_is_shared_iscsi(zhp)); 3823126Sahl 3833126Sahl return (zfs_is_shared_nfs(zhp, NULL)); 3843126Sahl } 3853126Sahl 3863126Sahl int 3873126Sahl zfs_share(zfs_handle_t *zhp) 3883126Sahl { 3893126Sahl if (ZFS_IS_VOLUME(zhp)) 3903126Sahl return (zfs_share_iscsi(zhp)); 3913126Sahl 3923126Sahl return (zfs_share_nfs(zhp)); 3933126Sahl } 3943126Sahl 3953126Sahl int 3963126Sahl zfs_unshare(zfs_handle_t *zhp) 3973126Sahl { 3983126Sahl if (ZFS_IS_VOLUME(zhp)) 3993126Sahl return (zfs_unshare_iscsi(zhp)); 4003126Sahl 4013126Sahl return (zfs_unshare_nfs(zhp, NULL)); 4023126Sahl } 4033126Sahl 404789Sahrens /* 405789Sahrens * Check to see if the filesystem is currently shared. 406789Sahrens */ 4072082Seschrock boolean_t 4083126Sahl zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 409789Sahrens { 410789Sahrens char *mountpoint; 411789Sahrens 412789Sahrens if (!zfs_is_mounted(zhp, &mountpoint)) 4132082Seschrock return (B_FALSE); 414789Sahrens 4152082Seschrock if (is_shared(zhp->zfs_hdl, mountpoint)) { 416789Sahrens if (where != NULL) 417789Sahrens *where = mountpoint; 418789Sahrens else 419789Sahrens free(mountpoint); 4202082Seschrock return (B_TRUE); 421789Sahrens } else { 422789Sahrens free(mountpoint); 4232082Seschrock return (B_FALSE); 424789Sahrens } 425789Sahrens } 426789Sahrens 427789Sahrens /* 4284180Sdougm * Make sure things will work if libshare isn't installed by using 4294180Sdougm * wrapper functions that check to see that the pointers to functions 4304180Sdougm * initialized in _zfs_init_libshare() are actually present. 4314180Sdougm */ 4324180Sdougm 4334180Sdougm static sa_handle_t (*_sa_init)(int); 4344180Sdougm static void (*_sa_fini)(sa_handle_t); 4354180Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *); 4364180Sdougm static int (*_sa_enable_share)(sa_share_t, char *); 4374180Sdougm static int (*_sa_disable_share)(sa_share_t, char *); 4384180Sdougm static char *(*_sa_errorstr)(int); 4394180Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *); 4404180Sdougm 4414180Sdougm /* 4424180Sdougm * _zfs_init_libshare() 4434180Sdougm * 4444180Sdougm * Find the libshare.so.1 entry points that we use here and save the 4454180Sdougm * values to be used later. This is triggered by the runtime loader. 4464180Sdougm * Make sure the correct ISA version is loaded. 4474180Sdougm */ 4484180Sdougm 4494180Sdougm #pragma init(_zfs_init_libshare) 4504180Sdougm static void 4514180Sdougm _zfs_init_libshare(void) 4524180Sdougm { 4534180Sdougm void *libshare; 4544180Sdougm char path[MAXPATHLEN]; 4554180Sdougm char isa[MAXISALEN]; 4564180Sdougm 4574180Sdougm #if defined(_LP64) 4584180Sdougm if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 4594302Sdougm isa[0] = '\0'; 4604180Sdougm #else 4614180Sdougm isa[0] = '\0'; 4624180Sdougm #endif 4634180Sdougm (void) snprintf(path, MAXPATHLEN, 4644302Sdougm "/usr/lib/%s/libshare.so.1", isa); 4654180Sdougm 4664180Sdougm if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) { 4674302Sdougm _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init"); 4684302Sdougm _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini"); 4694302Sdougm _sa_find_share = (sa_share_t (*)(sa_handle_t, char *)) 4704302Sdougm dlsym(libshare, "sa_find_share"); 4714302Sdougm _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 4724302Sdougm "sa_enable_share"); 4734302Sdougm _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 4744302Sdougm "sa_disable_share"); 4754302Sdougm _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr"); 4764302Sdougm _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *)) 4774302Sdougm dlsym(libshare, "sa_parse_legacy_options"); 478*4327Sdougm if (_sa_init == NULL || _sa_fini == NULL || 479*4327Sdougm _sa_find_share == NULL || _sa_enable_share == NULL || 480*4327Sdougm _sa_disable_share == NULL || _sa_errorstr == NULL || 481*4327Sdougm _sa_parse_legacy_options == NULL) { 482*4327Sdougm _sa_init = NULL; 483*4327Sdougm _sa_fini = NULL; 484*4327Sdougm _sa_disable_share = NULL; 485*4327Sdougm _sa_enable_share = NULL; 486*4327Sdougm _sa_errorstr = NULL; 487*4327Sdougm _sa_parse_legacy_options = NULL; 488*4327Sdougm (void) dlclose(libshare); 489*4327Sdougm } 4904180Sdougm } 4914180Sdougm } 4924180Sdougm 4934180Sdougm /* 4944180Sdougm * zfs_init_libshare(zhandle, service) 4954180Sdougm * 4964180Sdougm * Initialize the libshare API if it hasn't already been initialized. 4974180Sdougm * In all cases it returns 0 if it succeeded and an error if not. The 4984180Sdougm * service value is which part(s) of the API to initialize and is a 4994180Sdougm * direct map to the libshare sa_init(service) interface. 5004180Sdougm */ 5014180Sdougm 5024180Sdougm int 5034180Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service) 5044180Sdougm { 5054180Sdougm int ret = SA_OK; 5064180Sdougm 5074302Sdougm if (_sa_init == NULL) 5084302Sdougm ret = SA_CONFIG_ERR; 5094302Sdougm 5104302Sdougm if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL) 5114302Sdougm zhandle->libzfs_sharehdl = _sa_init(service); 5124302Sdougm 5134302Sdougm if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL) 5144302Sdougm ret = SA_NO_MEMORY; 5154302Sdougm 5164180Sdougm return (ret); 5174180Sdougm } 5184180Sdougm 5194180Sdougm /* 5204180Sdougm * zfs_uninit_libshare(zhandle) 5214180Sdougm * 5224180Sdougm * Uninitialize the libshare API if it hasn't already been 5234180Sdougm * uninitialized. It is OK to call multiple times. 5244180Sdougm */ 5254180Sdougm 5264180Sdougm void 5274180Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle) 5284180Sdougm { 5294180Sdougm 5304180Sdougm if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) { 5314302Sdougm if (_sa_fini != NULL) 5324302Sdougm _sa_fini(zhandle->libzfs_sharehdl); 5334302Sdougm zhandle->libzfs_sharehdl = NULL; 5344180Sdougm } 5354180Sdougm } 5364180Sdougm 5374180Sdougm /* 5384180Sdougm * zfs_parse_options(options, proto) 5394180Sdougm * 5404180Sdougm * Call the legacy parse interface to get the protocol specific 5414180Sdougm * options using the NULL arg to indicate that this is a "parse" only. 5424180Sdougm */ 5434180Sdougm 5444180Sdougm int 5454180Sdougm zfs_parse_options(char *options, char *proto) 5464180Sdougm { 5474180Sdougm int ret; 5484180Sdougm 5494180Sdougm if (_sa_parse_legacy_options != NULL) 5504302Sdougm ret = _sa_parse_legacy_options(NULL, options, proto); 5514180Sdougm else 5524302Sdougm ret = SA_CONFIG_ERR; 5534180Sdougm return (ret); 5544180Sdougm } 5554180Sdougm 5564180Sdougm /* 5574180Sdougm * zfs_sa_find_share(handle, path) 5584180Sdougm * 5594180Sdougm * wrapper around sa_find_share to find a share path in the 5604180Sdougm * configuration. 5614180Sdougm */ 5624180Sdougm 5634180Sdougm static sa_share_t 5644180Sdougm zfs_sa_find_share(sa_handle_t handle, char *path) 5654180Sdougm { 5664180Sdougm if (_sa_find_share != NULL) 5674302Sdougm return (_sa_find_share(handle, path)); 5684180Sdougm return (NULL); 5694180Sdougm } 5704180Sdougm 5714180Sdougm /* 5724180Sdougm * zfs_sa_enable_share(share, proto) 5734180Sdougm * 5744180Sdougm * Wrapper for sa_enable_share which enables a share for a specified 5754180Sdougm * protocol. 5764180Sdougm */ 5774180Sdougm 5784180Sdougm static int 5794180Sdougm zfs_sa_enable_share(sa_share_t share, char *proto) 5804180Sdougm { 5814180Sdougm if (_sa_enable_share != NULL) 5824302Sdougm return (_sa_enable_share(share, proto)); 5834180Sdougm return (SA_CONFIG_ERR); 5844180Sdougm } 5854180Sdougm 5864180Sdougm /* 5874180Sdougm * zfs_sa_disable_share(share, proto) 5884180Sdougm * 5894180Sdougm * Wrapper for sa_enable_share which disables a share for a specified 5904180Sdougm * protocol. 5914180Sdougm */ 5924180Sdougm 5934180Sdougm static int 5944180Sdougm zfs_sa_disable_share(sa_share_t share, char *proto) 5954180Sdougm { 5964180Sdougm if (_sa_disable_share != NULL) 5974302Sdougm return (_sa_disable_share(share, proto)); 5984180Sdougm return (SA_CONFIG_ERR); 5994180Sdougm } 6004180Sdougm 6014180Sdougm /* 602789Sahrens * Share the given filesystem according to the options in 'sharenfs'. We rely 6034180Sdougm * on "libshare" to the dirty work for us. 604789Sahrens */ 6054180Sdougm 606789Sahrens int 6073126Sahl zfs_share_nfs(zfs_handle_t *zhp) 608789Sahrens { 609789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 610789Sahrens char shareopts[ZFS_MAXPROPLEN]; 6112082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 6124180Sdougm sa_share_t share; 6134180Sdougm int ret; 614789Sahrens 6152676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 616789Sahrens return (0); 617789Sahrens 6183126Sahl /* 6193126Sahl * Return success if there are no share options. 6203126Sahl */ 621789Sahrens if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts), 6222082Seschrock NULL, NULL, 0, B_FALSE) != 0 || 623789Sahrens strcmp(shareopts, "off") == 0) 624789Sahrens return (0); 625789Sahrens 626789Sahrens /* 6272676Seschrock * If the 'zoned' property is set, then zfs_is_mountable() will have 6282676Seschrock * already bailed out if we are in the global zone. But local 6292676Seschrock * zones cannot be NFS servers, so we ignore it for local zones as well. 630789Sahrens */ 6312676Seschrock if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 632789Sahrens return (0); 633789Sahrens 6344180Sdougm if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 6354302Sdougm (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 6364302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s': %s"), 6374302Sdougm zfs_get_name(zhp), _sa_errorstr(ret)); 6384302Sdougm return (-1); 6394180Sdougm } 6404180Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint); 6414180Sdougm if (share != NULL) { 6424302Sdougm int err; 6434302Sdougm err = zfs_sa_enable_share(share, "nfs"); 6444302Sdougm if (err != SA_OK) { 6454302Sdougm (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 6464302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 6474302Sdougm zfs_get_name(zhp)); 6484302Sdougm return (-1); 6494302Sdougm } 6504180Sdougm } else { 6514180Sdougm (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 6524302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 6534302Sdougm zfs_get_name(zhp)); 654789Sahrens return (-1); 655789Sahrens } 656789Sahrens 657789Sahrens return (0); 658789Sahrens } 659789Sahrens 660789Sahrens /* 6612474Seschrock * Unshare a filesystem by mountpoint. 6622474Seschrock */ 6632474Seschrock static int 6642474Seschrock unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint) 6652474Seschrock { 6664180Sdougm sa_share_t share; 6674180Sdougm int err; 6684180Sdougm char *mntpt; 6692474Seschrock 6702474Seschrock /* 6714180Sdougm * Mountpoint could get trashed if libshare calls getmntany 6724180Sdougm * which id does during API initialization, so strdup the 6734180Sdougm * value. 6742474Seschrock */ 6754180Sdougm mntpt = zfs_strdup(hdl, mountpoint); 6762474Seschrock 6774180Sdougm /* make sure libshare initialized */ 6784180Sdougm if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 6794180Sdougm free(mntpt); /* don't need the copy anymore */ 6804180Sdougm return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 6814302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 6824302Sdougm name, _sa_errorstr(err))); 6832474Seschrock } 6842474Seschrock 6854180Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt); 6864180Sdougm free(mntpt); /* don't need the copy anymore */ 6872474Seschrock 6884180Sdougm if (share != NULL) { 6894180Sdougm err = zfs_sa_disable_share(share, "nfs"); 6904180Sdougm if (err != SA_OK) { 6914302Sdougm return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 6924302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 6934302Sdougm name, _sa_errorstr(err))); 6944180Sdougm } 6954180Sdougm } else { 6964180Sdougm return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 6974302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"), 6984302Sdougm name)); 6994180Sdougm } 7002474Seschrock return (0); 7012474Seschrock } 7022474Seschrock 7032474Seschrock /* 704789Sahrens * Unshare the given filesystem. 705789Sahrens */ 706789Sahrens int 7073126Sahl zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 708789Sahrens { 709789Sahrens struct mnttab search = { 0 }, entry; 7104180Sdougm char *mntpt = NULL; 711789Sahrens 712789Sahrens /* check to see if need to unmount the filesystem */ 713789Sahrens search.mnt_special = (char *)zfs_get_name(zhp); 7141407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 7152082Seschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 7164302Sdougm if (mountpoint != NULL) 7174302Sdougm mountpoint = mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint); 7184302Sdougm 719789Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 7202082Seschrock getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 721789Sahrens 722789Sahrens if (mountpoint == NULL) 723789Sahrens mountpoint = entry.mnt_mountp; 724789Sahrens 7252474Seschrock if (is_shared(zhp->zfs_hdl, mountpoint) && 7264180Sdougm unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) { 7274180Sdougm if (mntpt != NULL) 7284180Sdougm free(mntpt); 7292474Seschrock return (-1); 7304180Sdougm } 731789Sahrens } 7324180Sdougm if (mntpt != NULL) 7334302Sdougm free(mntpt); 734789Sahrens 735789Sahrens return (0); 736789Sahrens } 737789Sahrens 738789Sahrens /* 7393126Sahl * Same as zfs_unmountall(), but for NFS unshares. 740789Sahrens */ 741789Sahrens int 7423126Sahl zfs_unshareall_nfs(zfs_handle_t *zhp) 743789Sahrens { 744789Sahrens prop_changelist_t *clp; 745789Sahrens int ret; 746789Sahrens 747789Sahrens clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0); 748789Sahrens if (clp == NULL) 749789Sahrens return (-1); 750789Sahrens 751789Sahrens ret = changelist_unshare(clp); 752789Sahrens changelist_free(clp); 753789Sahrens 754789Sahrens return (ret); 755789Sahrens } 756789Sahrens 757789Sahrens /* 758789Sahrens * Remove the mountpoint associated with the current dataset, if necessary. 759789Sahrens * We only remove the underlying directory if: 760789Sahrens * 761789Sahrens * - The mountpoint is not 'none' or 'legacy' 762789Sahrens * - The mountpoint is non-empty 763789Sahrens * - The mountpoint is the default or inherited 764789Sahrens * - The 'zoned' property is set, or we're in a local zone 765789Sahrens * 766789Sahrens * Any other directories we leave alone. 767789Sahrens */ 768789Sahrens void 769789Sahrens remove_mountpoint(zfs_handle_t *zhp) 770789Sahrens { 771789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 7722676Seschrock zfs_source_t source; 773789Sahrens 7742676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 7752676Seschrock &source)) 776789Sahrens return; 777789Sahrens 7782676Seschrock if (source == ZFS_SRC_DEFAULT || 7792676Seschrock source == ZFS_SRC_INHERITED) { 780789Sahrens /* 781789Sahrens * Try to remove the directory, silently ignoring any errors. 782789Sahrens * The filesystem may have since been removed or moved around, 7832676Seschrock * and this error isn't really useful to the administrator in 7842676Seschrock * any way. 785789Sahrens */ 786789Sahrens (void) rmdir(mountpoint); 787789Sahrens } 788789Sahrens } 7892474Seschrock 7903126Sahl boolean_t 7913126Sahl zfs_is_shared_iscsi(zfs_handle_t *zhp) 7923126Sahl { 7933134Sahl return (iscsitgt_zfs_is_shared != NULL && 7943134Sahl iscsitgt_zfs_is_shared(zhp->zfs_name) != 0); 7953126Sahl } 7963126Sahl 7973126Sahl int 7983126Sahl zfs_share_iscsi(zfs_handle_t *zhp) 7993126Sahl { 8003126Sahl char shareopts[ZFS_MAXPROPLEN]; 8013126Sahl const char *dataset = zhp->zfs_name; 8023126Sahl libzfs_handle_t *hdl = zhp->zfs_hdl; 8033126Sahl 8043126Sahl /* 8053126Sahl * Return success if there are no share options. 8063126Sahl */ 8073126Sahl if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts, 8083126Sahl sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 || 8093126Sahl strcmp(shareopts, "off") == 0) 8103126Sahl return (0); 8113126Sahl 8123134Sahl if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) 8133237Slling return (zfs_error_fmt(hdl, EZFS_SHAREISCSIFAILED, 8143126Sahl dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset)); 8153126Sahl 8163126Sahl return (0); 8173126Sahl } 8183126Sahl 8193126Sahl int 8203126Sahl zfs_unshare_iscsi(zfs_handle_t *zhp) 8213126Sahl { 8223126Sahl const char *dataset = zfs_get_name(zhp); 8233126Sahl libzfs_handle_t *hdl = zhp->zfs_hdl; 8243126Sahl 8253126Sahl /* 8263380Sgw25295 * Return if the volume is not shared 8273380Sgw25295 */ 8283380Sgw25295 if (!zfs_is_shared_iscsi(zhp)) 8293380Sgw25295 return (0); 8303380Sgw25295 8313380Sgw25295 /* 8323126Sahl * If this fails with ENODEV it indicates that zvol wasn't shared so 8333126Sahl * we should return success in that case. 8343126Sahl */ 8353134Sahl if (iscsitgt_zfs_unshare == NULL || 8363134Sahl (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) 8373237Slling return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED, 8383126Sahl dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset)); 8393126Sahl 8403126Sahl return (0); 8413126Sahl } 8423126Sahl 8432474Seschrock typedef struct mount_cbdata { 8442474Seschrock zfs_handle_t **cb_datasets; 8452474Seschrock int cb_used; 8462474Seschrock int cb_alloc; 8472474Seschrock } mount_cbdata_t; 8482474Seschrock 8492474Seschrock static int 8502474Seschrock mount_cb(zfs_handle_t *zhp, void *data) 8512474Seschrock { 8522474Seschrock mount_cbdata_t *cbp = data; 8532474Seschrock 8543126Sahl if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) { 8552474Seschrock zfs_close(zhp); 8562474Seschrock return (0); 8572474Seschrock } 8582474Seschrock 8592474Seschrock if (cbp->cb_alloc == cbp->cb_used) { 8602676Seschrock void *ptr; 8612474Seschrock 8622676Seschrock if ((ptr = zfs_realloc(zhp->zfs_hdl, 8632676Seschrock cbp->cb_datasets, cbp->cb_alloc * sizeof (void *), 8642676Seschrock cbp->cb_alloc * 2 * sizeof (void *))) == NULL) 8652474Seschrock return (-1); 8662676Seschrock cbp->cb_datasets = ptr; 8672474Seschrock 8682676Seschrock cbp->cb_alloc *= 2; 8692474Seschrock } 8702474Seschrock 8712474Seschrock cbp->cb_datasets[cbp->cb_used++] = zhp; 8723126Sahl 8733126Sahl return (zfs_iter_children(zhp, mount_cb, cbp)); 8742474Seschrock } 8752474Seschrock 8762474Seschrock static int 8773126Sahl dataset_cmp(const void *a, const void *b) 8782474Seschrock { 8792474Seschrock zfs_handle_t **za = (zfs_handle_t **)a; 8802474Seschrock zfs_handle_t **zb = (zfs_handle_t **)b; 8812474Seschrock char mounta[MAXPATHLEN]; 8822474Seschrock char mountb[MAXPATHLEN]; 8833126Sahl boolean_t gota, gotb; 8842474Seschrock 8853126Sahl if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 8863126Sahl verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 8873126Sahl sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 8883126Sahl if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 8893126Sahl verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 8903126Sahl sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 8912474Seschrock 8923126Sahl if (gota && gotb) 8933126Sahl return (strcmp(mounta, mountb)); 8943126Sahl 8953126Sahl if (gota) 8963126Sahl return (-1); 8973126Sahl if (gotb) 8983126Sahl return (1); 8993126Sahl 9003126Sahl return (strcmp(zfs_get_name(a), zfs_get_name(b))); 9012474Seschrock } 9022474Seschrock 9033126Sahl /* 9043126Sahl * Mount and share all datasets within the given pool. This assumes that no 9053126Sahl * datasets within the pool are currently mounted. Because users can create 9063126Sahl * complicated nested hierarchies of mountpoints, we first gather all the 9073126Sahl * datasets and mountpoints within the pool, and sort them by mountpoint. Once 9083126Sahl * we have the list of all filesystems, we iterate over them in order and mount 9093126Sahl * and/or share each one. 9103126Sahl */ 9113126Sahl #pragma weak zpool_mount_datasets = zpool_enable_datasets 9122474Seschrock int 9133126Sahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 9142474Seschrock { 9152474Seschrock mount_cbdata_t cb = { 0 }; 9162474Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9172474Seschrock zfs_handle_t *zfsp; 9182474Seschrock int i, ret = -1; 9194180Sdougm int *good; 9202474Seschrock 9212474Seschrock /* 9222474Seschrock * Gather all datasets within the pool. 9232474Seschrock */ 9242474Seschrock if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL) 9252474Seschrock return (-1); 9262474Seschrock cb.cb_alloc = 4; 9272474Seschrock 9282474Seschrock if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL) 9292474Seschrock goto out; 9302474Seschrock 9312474Seschrock cb.cb_datasets[0] = zfsp; 9322474Seschrock cb.cb_used = 1; 9332474Seschrock 9342474Seschrock if (zfs_iter_children(zfsp, mount_cb, &cb) != 0) 9352474Seschrock goto out; 9362474Seschrock 9372474Seschrock /* 9382474Seschrock * Sort the datasets by mountpoint. 9392474Seschrock */ 9403126Sahl qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp); 9412474Seschrock 9422474Seschrock /* 9434180Sdougm * And mount all the datasets, keeping track of which ones 9444180Sdougm * succeeded or failed. By using zfs_alloc(), the good pointer 9454180Sdougm * will always be non-NULL. 9462474Seschrock */ 9474180Sdougm good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int)); 9482474Seschrock ret = 0; 9492474Seschrock for (i = 0; i < cb.cb_used; i++) { 9504302Sdougm if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0) 9514180Sdougm ret = -1; 9524302Sdougm else 9534180Sdougm good[i] = 1; 9544180Sdougm } 9554180Sdougm /* 9564180Sdougm * Then share all the ones that need to be shared. This needs 9574180Sdougm * to be a separate pass in order to avoid excessive reloading 9584180Sdougm * of the configuration. Good should never be NULL since 9594180Sdougm * zfs_alloc is supposed to exit if memory isn't available. 9604180Sdougm */ 9614180Sdougm zfs_uninit_libshare(hdl); 9624180Sdougm for (i = 0; i < cb.cb_used; i++) { 9634180Sdougm if (good[i] && zfs_share(cb.cb_datasets[i]) != 0) 9642474Seschrock ret = -1; 9652474Seschrock } 9662474Seschrock 9674180Sdougm free(good); 9684180Sdougm 9692474Seschrock out: 9702474Seschrock for (i = 0; i < cb.cb_used; i++) 9712474Seschrock zfs_close(cb.cb_datasets[i]); 9722474Seschrock free(cb.cb_datasets); 9732474Seschrock 9742474Seschrock return (ret); 9752474Seschrock } 9762474Seschrock 9773126Sahl 9783126Sahl static int 9793126Sahl zvol_cb(const char *dataset, void *data) 9803126Sahl { 9813126Sahl libzfs_handle_t *hdl = data; 9823126Sahl zfs_handle_t *zhp; 9833126Sahl 9843126Sahl /* 9853126Sahl * Ignore snapshots and ignore failures from non-existant datasets. 9863126Sahl */ 9873126Sahl if (strchr(dataset, '@') != NULL || 9883126Sahl (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL) 9893126Sahl return (0); 9903126Sahl 9913126Sahl (void) zfs_unshare_iscsi(zhp); 9923126Sahl 9933126Sahl zfs_close(zhp); 9943126Sahl 9953126Sahl return (0); 9963126Sahl } 9973126Sahl 9982474Seschrock static int 9992474Seschrock mountpoint_compare(const void *a, const void *b) 10002474Seschrock { 10012474Seschrock const char *mounta = *((char **)a); 10022474Seschrock const char *mountb = *((char **)b); 10032474Seschrock 10042474Seschrock return (strcmp(mountb, mounta)); 10052474Seschrock } 10062474Seschrock 10073126Sahl /* 10083126Sahl * Unshare and unmount all datasets within the given pool. We don't want to 10093126Sahl * rely on traversing the DSL to discover the filesystems within the pool, 10103126Sahl * because this may be expensive (if not all of them are mounted), and can fail 10113126Sahl * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 10123126Sahl * gather all the filesystems that are currently mounted. 10133126Sahl */ 10143126Sahl #pragma weak zpool_unmount_datasets = zpool_disable_datasets 10152474Seschrock int 10163126Sahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 10172474Seschrock { 10182474Seschrock int used, alloc; 10192474Seschrock struct mnttab entry; 10202474Seschrock size_t namelen; 10212474Seschrock char **mountpoints = NULL; 10222474Seschrock zfs_handle_t **datasets = NULL; 10232474Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10242474Seschrock int i; 10252474Seschrock int ret = -1; 10262474Seschrock int flags = (force ? MS_FORCE : 0); 10272474Seschrock 10283126Sahl /* 10293126Sahl * First unshare all zvols. 10303126Sahl */ 10313126Sahl if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0) 10323126Sahl return (-1); 10333126Sahl 10342474Seschrock namelen = strlen(zhp->zpool_name); 10352474Seschrock 10362474Seschrock rewind(hdl->libzfs_mnttab); 10372474Seschrock used = alloc = 0; 10382474Seschrock while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 10392474Seschrock /* 10402474Seschrock * Ignore non-ZFS entries. 10412474Seschrock */ 10422474Seschrock if (entry.mnt_fstype == NULL || 10432474Seschrock strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 10442474Seschrock continue; 10452474Seschrock 10462474Seschrock /* 10472474Seschrock * Ignore filesystems not within this pool. 10482474Seschrock */ 10492474Seschrock if (entry.mnt_mountp == NULL || 10502474Seschrock strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 10512474Seschrock (entry.mnt_special[namelen] != '/' && 10522474Seschrock entry.mnt_special[namelen] != '\0')) 10532474Seschrock continue; 10542474Seschrock 10552474Seschrock /* 10562474Seschrock * At this point we've found a filesystem within our pool. Add 10572474Seschrock * it to our growing list. 10582474Seschrock */ 10592474Seschrock if (used == alloc) { 10602474Seschrock if (alloc == 0) { 10612474Seschrock if ((mountpoints = zfs_alloc(hdl, 10622474Seschrock 8 * sizeof (void *))) == NULL) 10632474Seschrock goto out; 10642474Seschrock 10652474Seschrock if ((datasets = zfs_alloc(hdl, 10662474Seschrock 8 * sizeof (void *))) == NULL) 10672474Seschrock goto out; 10682474Seschrock 10692474Seschrock alloc = 8; 10702474Seschrock } else { 10712676Seschrock void *ptr; 10722474Seschrock 10732676Seschrock if ((ptr = zfs_realloc(hdl, mountpoints, 10742676Seschrock alloc * sizeof (void *), 10752474Seschrock alloc * 2 * sizeof (void *))) == NULL) 10762474Seschrock goto out; 10772676Seschrock mountpoints = ptr; 10782474Seschrock 10792676Seschrock if ((ptr = zfs_realloc(hdl, datasets, 10802676Seschrock alloc * sizeof (void *), 10812474Seschrock alloc * 2 * sizeof (void *))) == NULL) 10822474Seschrock goto out; 10832676Seschrock datasets = ptr; 10842474Seschrock 10852474Seschrock alloc *= 2; 10862474Seschrock } 10872474Seschrock } 10882474Seschrock 10892474Seschrock if ((mountpoints[used] = zfs_strdup(hdl, 10902474Seschrock entry.mnt_mountp)) == NULL) 10912474Seschrock goto out; 10922474Seschrock 10932474Seschrock /* 10942474Seschrock * This is allowed to fail, in case there is some I/O error. It 10952474Seschrock * is only used to determine if we need to remove the underlying 10962474Seschrock * mountpoint, so failure is not fatal. 10972474Seschrock */ 10982474Seschrock datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 10992474Seschrock 11002474Seschrock used++; 11012474Seschrock } 11022474Seschrock 11032474Seschrock /* 11042474Seschrock * At this point, we have the entire list of filesystems, so sort it by 11052474Seschrock * mountpoint. 11062474Seschrock */ 11072474Seschrock qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 11082474Seschrock 11092474Seschrock /* 11102474Seschrock * Walk through and first unshare everything. 11112474Seschrock */ 11122474Seschrock for (i = 0; i < used; i++) { 11132474Seschrock if (is_shared(hdl, mountpoints[i]) && 11142676Seschrock unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0) 11152474Seschrock goto out; 11162474Seschrock } 11172474Seschrock 11182474Seschrock /* 11192474Seschrock * Now unmount everything, removing the underlying directories as 11202474Seschrock * appropriate. 11212474Seschrock */ 11222474Seschrock for (i = 0; i < used; i++) { 11232474Seschrock if (unmount_one(hdl, mountpoints[i], flags) != 0) 11242474Seschrock goto out; 11252676Seschrock } 11262474Seschrock 11272676Seschrock for (i = 0; i < used; i++) { 11282474Seschrock if (datasets[i]) 11292474Seschrock remove_mountpoint(datasets[i]); 11302474Seschrock } 11312474Seschrock 11322474Seschrock ret = 0; 11332474Seschrock out: 11342474Seschrock for (i = 0; i < used; i++) { 11352474Seschrock if (datasets[i]) 11362474Seschrock zfs_close(datasets[i]); 11372474Seschrock free(mountpoints[i]); 11382474Seschrock } 11392474Seschrock free(datasets); 11402474Seschrock free(mountpoints); 11412474Seschrock 11422474Seschrock return (ret); 11432474Seschrock } 1144