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 *); 884543Smarks static int (*iscsitgt_svc_online)(); 893134Sahl 903134Sahl #pragma init(zfs_iscsi_init) 913134Sahl static void 923134Sahl zfs_iscsi_init(void) 933134Sahl { 943134Sahl void *libiscsitgt; 953134Sahl 963134Sahl if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1", 973134Sahl RTLD_LAZY | RTLD_GLOBAL)) == NULL || 983134Sahl (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt, 993134Sahl "iscsitgt_zfs_share")) == NULL || 1003134Sahl (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt, 1013134Sahl "iscsitgt_zfs_unshare")) == NULL || 1023134Sahl (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt, 1034543Smarks "iscsitgt_zfs_is_shared")) == NULL || 1044543Smarks (iscsitgt_svc_online = (int (*)(const char *))dlsym(libiscsitgt, 1054543Smarks "iscsitgt_svc_online")) == NULL) { 1063134Sahl iscsitgt_zfs_share = NULL; 1073134Sahl iscsitgt_zfs_unshare = NULL; 1083134Sahl iscsitgt_zfs_is_shared = NULL; 1094543Smarks iscsitgt_svc_online = NULL; 1103134Sahl } 1113134Sahl } 1123134Sahl 113789Sahrens /* 1142082Seschrock * Search the sharetab for the given mountpoint, returning true if it is found. 115789Sahrens */ 1162082Seschrock static boolean_t 1172082Seschrock is_shared(libzfs_handle_t *hdl, const char *mountpoint) 118789Sahrens { 119789Sahrens char buf[MAXPATHLEN], *tab; 120789Sahrens 1212082Seschrock if (hdl->libzfs_sharetab == NULL) 122789Sahrens return (0); 123789Sahrens 1242082Seschrock (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET); 125789Sahrens 1262082Seschrock while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) { 127789Sahrens 128789Sahrens /* the mountpoint is the first entry on each line */ 129789Sahrens if ((tab = strchr(buf, '\t')) != NULL) { 130789Sahrens *tab = '\0'; 131789Sahrens if (strcmp(buf, mountpoint) == 0) 1322082Seschrock return (B_TRUE); 133789Sahrens } 134789Sahrens } 135789Sahrens 1362082Seschrock return (B_FALSE); 137789Sahrens } 138789Sahrens 139789Sahrens /* 1402082Seschrock * Returns true if the specified directory is empty. If we can't open the 1412082Seschrock * directory at all, return true so that the mount can fail with a more 142789Sahrens * informative error message. 143789Sahrens */ 1442082Seschrock static boolean_t 145789Sahrens dir_is_empty(const char *dirname) 146789Sahrens { 147789Sahrens DIR *dirp; 148789Sahrens struct dirent64 *dp; 149789Sahrens 150789Sahrens if ((dirp = opendir(dirname)) == NULL) 1512082Seschrock return (B_TRUE); 152789Sahrens 153789Sahrens while ((dp = readdir64(dirp)) != NULL) { 154789Sahrens 155789Sahrens if (strcmp(dp->d_name, ".") == 0 || 156789Sahrens strcmp(dp->d_name, "..") == 0) 157789Sahrens continue; 158789Sahrens 159789Sahrens (void) closedir(dirp); 1602082Seschrock return (B_FALSE); 161789Sahrens } 162789Sahrens 163789Sahrens (void) closedir(dirp); 1642082Seschrock return (B_TRUE); 165789Sahrens } 166789Sahrens 167789Sahrens /* 168789Sahrens * Checks to see if the mount is active. If the filesystem is mounted, we fill 169789Sahrens * in 'where' with the current mountpoint, and return 1. Otherwise, we return 170789Sahrens * 0. 171789Sahrens */ 1722082Seschrock boolean_t 1733444Sek110237 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 174789Sahrens { 175789Sahrens struct mnttab search = { 0 }, entry; 176789Sahrens 177789Sahrens /* 178789Sahrens * Search for the entry in /etc/mnttab. We don't bother getting the 179789Sahrens * mountpoint, as we can just search for the special device. This will 180789Sahrens * also let us find mounts when the mountpoint is 'legacy'. 181789Sahrens */ 1823444Sek110237 search.mnt_special = (char *)special; 1831407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 184789Sahrens 1853444Sek110237 rewind(zfs_hdl->libzfs_mnttab); 1863444Sek110237 if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0) 1872082Seschrock return (B_FALSE); 188789Sahrens 189789Sahrens if (where != NULL) 1903444Sek110237 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 191789Sahrens 1922082Seschrock return (B_TRUE); 193789Sahrens } 194789Sahrens 1953444Sek110237 boolean_t 1963444Sek110237 zfs_is_mounted(zfs_handle_t *zhp, char **where) 1973444Sek110237 { 1983444Sek110237 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 1993444Sek110237 } 2003444Sek110237 201789Sahrens /* 2022676Seschrock * Returns true if the given dataset is mountable, false otherwise. Returns the 2032676Seschrock * mountpoint in 'buf'. 2042676Seschrock */ 2052676Seschrock static boolean_t 2062676Seschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 207*5094Slling zprop_source_t *source) 2082676Seschrock { 2092676Seschrock char sourceloc[ZFS_MAXNAMELEN]; 210*5094Slling zprop_source_t sourcetype; 2112676Seschrock 2122676Seschrock if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 2132676Seschrock return (B_FALSE); 2142676Seschrock 2152676Seschrock verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 2162676Seschrock &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 2172676Seschrock 2182676Seschrock if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 2192676Seschrock strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 2202676Seschrock return (B_FALSE); 2212676Seschrock 2222676Seschrock if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT)) 2232676Seschrock return (B_FALSE); 2242676Seschrock 2252676Seschrock if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2262676Seschrock getzoneid() == GLOBAL_ZONEID) 2272676Seschrock return (B_FALSE); 2282676Seschrock 2292676Seschrock if (source) 2302676Seschrock *source = sourcetype; 2312676Seschrock 2322676Seschrock return (B_TRUE); 2332676Seschrock } 2342676Seschrock 2352676Seschrock /* 236789Sahrens * Mount the given filesystem. 237789Sahrens */ 238789Sahrens int 239789Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 240789Sahrens { 241789Sahrens struct stat buf; 242789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 243789Sahrens char mntopts[MNT_LINE_MAX]; 2442082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 245789Sahrens 246789Sahrens if (options == NULL) 247789Sahrens mntopts[0] = '\0'; 248789Sahrens else 249789Sahrens (void) strlcpy(mntopts, options, sizeof (mntopts)); 250789Sahrens 2512676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 2522082Seschrock return (0); 253789Sahrens 254789Sahrens /* Create the directory if it doesn't already exist */ 255789Sahrens if (lstat(mountpoint, &buf) != 0) { 256789Sahrens if (mkdirp(mountpoint, 0755) != 0) { 2572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2582082Seschrock "failed to create mountpoint")); 2593237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 2602082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 2612082Seschrock mountpoint)); 262789Sahrens } 263789Sahrens } 264789Sahrens 265789Sahrens /* 266789Sahrens * Determine if the mountpoint is empty. If so, refuse to perform the 267789Sahrens * mount. We don't perform this check if MS_OVERLAY is specified, which 268789Sahrens * would defeat the point. We also avoid this check if 'remount' is 269789Sahrens * specified. 270789Sahrens */ 271789Sahrens if ((flags & MS_OVERLAY) == 0 && 272789Sahrens strstr(mntopts, MNTOPT_REMOUNT) == NULL && 273789Sahrens !dir_is_empty(mountpoint)) { 2742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2752082Seschrock "directory is not empty")); 2763237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 2772082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 278789Sahrens } 279789Sahrens 280789Sahrens /* perform the mount */ 281789Sahrens if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 282789Sahrens MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 283789Sahrens /* 284789Sahrens * Generic errors are nasty, but there are just way too many 285789Sahrens * from mount(), and they're well-understood. We pick a few 286789Sahrens * common ones to improve upon. 287789Sahrens */ 2884302Sdougm if (errno == EBUSY) { 2892082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2902082Seschrock "mountpoint or dataset is busy")); 2914543Smarks } else if (errno == EPERM) { 2924543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2934543Smarks "Insufficient privileges")); 2944302Sdougm } else { 2952082Seschrock zfs_error_aux(hdl, strerror(errno)); 2964302Sdougm } 2972082Seschrock 2983237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 2992082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 3002082Seschrock zhp->zfs_name)); 301789Sahrens } 302789Sahrens 303789Sahrens return (0); 304789Sahrens } 305789Sahrens 306789Sahrens /* 3072474Seschrock * Unmount a single filesystem. 3082474Seschrock */ 3092474Seschrock static int 3102474Seschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 3112474Seschrock { 3122474Seschrock if (umount2(mountpoint, flags) != 0) { 3132474Seschrock zfs_error_aux(hdl, strerror(errno)); 3143237Slling return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 3152474Seschrock dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 3162474Seschrock mountpoint)); 3172474Seschrock } 3182474Seschrock 3192474Seschrock return (0); 3202474Seschrock } 3212474Seschrock 3222474Seschrock /* 323789Sahrens * Unmount the given filesystem. 324789Sahrens */ 325789Sahrens int 326789Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 327789Sahrens { 328789Sahrens struct mnttab search = { 0 }, entry; 3294180Sdougm char *mntpt = NULL; 330789Sahrens 331789Sahrens /* check to see if need to unmount the filesystem */ 3322474Seschrock search.mnt_special = zhp->zfs_name; 3331407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 3342082Seschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 335789Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 3362082Seschrock getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 337789Sahrens 3384180Sdougm /* 3394180Sdougm * mountpoint may have come from a call to 3404180Sdougm * getmnt/getmntany if it isn't NULL. If it is NULL, 3414180Sdougm * we know it comes from getmntany which can then get 3424180Sdougm * overwritten later. We strdup it to play it safe. 3434180Sdougm */ 344789Sahrens if (mountpoint == NULL) 3454302Sdougm mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 3464180Sdougm else 3474302Sdougm mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint); 348789Sahrens 349789Sahrens /* 3502474Seschrock * Unshare and unmount the filesystem 351789Sahrens */ 3524331Sth199096 if (zfs_unshare_nfs(zhp, mntpt) != 0) 3534331Sth199096 return (-1); 3544331Sth199096 3554331Sth199096 if (unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) { 3564180Sdougm free(mntpt); 3574337Sth199096 (void) zfs_share_nfs(zhp); 358789Sahrens return (-1); 3594180Sdougm } 3604180Sdougm free(mntpt); 361789Sahrens } 362789Sahrens 363789Sahrens return (0); 364789Sahrens } 365789Sahrens 366789Sahrens /* 367789Sahrens * Unmount this filesystem and any children inheriting the mountpoint property. 368789Sahrens * To do this, just act like we're changing the mountpoint property, but don't 369789Sahrens * remount the filesystems afterwards. 370789Sahrens */ 371789Sahrens int 372789Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags) 373789Sahrens { 374789Sahrens prop_changelist_t *clp; 375789Sahrens int ret; 376789Sahrens 377789Sahrens clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags); 378789Sahrens if (clp == NULL) 379789Sahrens return (-1); 380789Sahrens 381789Sahrens ret = changelist_prefix(clp); 382789Sahrens changelist_free(clp); 383789Sahrens 384789Sahrens return (ret); 385789Sahrens } 386789Sahrens 3873126Sahl boolean_t 3883126Sahl zfs_is_shared(zfs_handle_t *zhp) 3893126Sahl { 3903126Sahl if (ZFS_IS_VOLUME(zhp)) 3913126Sahl return (zfs_is_shared_iscsi(zhp)); 3923126Sahl 3933126Sahl return (zfs_is_shared_nfs(zhp, NULL)); 3943126Sahl } 3953126Sahl 3963126Sahl int 3973126Sahl zfs_share(zfs_handle_t *zhp) 3983126Sahl { 3993126Sahl if (ZFS_IS_VOLUME(zhp)) 4003126Sahl return (zfs_share_iscsi(zhp)); 4013126Sahl 4023126Sahl return (zfs_share_nfs(zhp)); 4033126Sahl } 4043126Sahl 4053126Sahl int 4063126Sahl zfs_unshare(zfs_handle_t *zhp) 4073126Sahl { 4083126Sahl if (ZFS_IS_VOLUME(zhp)) 4093126Sahl return (zfs_unshare_iscsi(zhp)); 4103126Sahl 4113126Sahl return (zfs_unshare_nfs(zhp, NULL)); 4123126Sahl } 4133126Sahl 414789Sahrens /* 415789Sahrens * Check to see if the filesystem is currently shared. 416789Sahrens */ 4172082Seschrock boolean_t 4183126Sahl zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 419789Sahrens { 420789Sahrens char *mountpoint; 421789Sahrens 422789Sahrens if (!zfs_is_mounted(zhp, &mountpoint)) 4232082Seschrock return (B_FALSE); 424789Sahrens 4252082Seschrock if (is_shared(zhp->zfs_hdl, mountpoint)) { 426789Sahrens if (where != NULL) 427789Sahrens *where = mountpoint; 428789Sahrens else 429789Sahrens free(mountpoint); 4302082Seschrock return (B_TRUE); 431789Sahrens } else { 432789Sahrens free(mountpoint); 4332082Seschrock return (B_FALSE); 434789Sahrens } 435789Sahrens } 436789Sahrens 437789Sahrens /* 4384180Sdougm * Make sure things will work if libshare isn't installed by using 4394180Sdougm * wrapper functions that check to see that the pointers to functions 4404180Sdougm * initialized in _zfs_init_libshare() are actually present. 4414180Sdougm */ 4424180Sdougm 4434180Sdougm static sa_handle_t (*_sa_init)(int); 4444180Sdougm static void (*_sa_fini)(sa_handle_t); 4454180Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *); 4464180Sdougm static int (*_sa_enable_share)(sa_share_t, char *); 4474180Sdougm static int (*_sa_disable_share)(sa_share_t, char *); 4484180Sdougm static char *(*_sa_errorstr)(int); 4494180Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *); 4504180Sdougm 4514180Sdougm /* 4524180Sdougm * _zfs_init_libshare() 4534180Sdougm * 4544180Sdougm * Find the libshare.so.1 entry points that we use here and save the 4554180Sdougm * values to be used later. This is triggered by the runtime loader. 4564180Sdougm * Make sure the correct ISA version is loaded. 4574180Sdougm */ 4584180Sdougm 4594180Sdougm #pragma init(_zfs_init_libshare) 4604180Sdougm static void 4614180Sdougm _zfs_init_libshare(void) 4624180Sdougm { 4634180Sdougm void *libshare; 4644180Sdougm char path[MAXPATHLEN]; 4654180Sdougm char isa[MAXISALEN]; 4664180Sdougm 4674180Sdougm #if defined(_LP64) 4684180Sdougm if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 4694302Sdougm isa[0] = '\0'; 4704180Sdougm #else 4714180Sdougm isa[0] = '\0'; 4724180Sdougm #endif 4734180Sdougm (void) snprintf(path, MAXPATHLEN, 4744302Sdougm "/usr/lib/%s/libshare.so.1", isa); 4754180Sdougm 4764180Sdougm if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) { 4774302Sdougm _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init"); 4784302Sdougm _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini"); 4794302Sdougm _sa_find_share = (sa_share_t (*)(sa_handle_t, char *)) 4804302Sdougm dlsym(libshare, "sa_find_share"); 4814302Sdougm _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 4824302Sdougm "sa_enable_share"); 4834302Sdougm _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 4844302Sdougm "sa_disable_share"); 4854302Sdougm _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr"); 4864302Sdougm _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *)) 4874302Sdougm dlsym(libshare, "sa_parse_legacy_options"); 4884327Sdougm if (_sa_init == NULL || _sa_fini == NULL || 4894327Sdougm _sa_find_share == NULL || _sa_enable_share == NULL || 4904327Sdougm _sa_disable_share == NULL || _sa_errorstr == NULL || 4914327Sdougm _sa_parse_legacy_options == NULL) { 4924327Sdougm _sa_init = NULL; 4934327Sdougm _sa_fini = NULL; 4944327Sdougm _sa_disable_share = NULL; 4954327Sdougm _sa_enable_share = NULL; 4964327Sdougm _sa_errorstr = NULL; 4974327Sdougm _sa_parse_legacy_options = NULL; 4984327Sdougm (void) dlclose(libshare); 4994327Sdougm } 5004180Sdougm } 5014180Sdougm } 5024180Sdougm 5034180Sdougm /* 5044180Sdougm * zfs_init_libshare(zhandle, service) 5054180Sdougm * 5064180Sdougm * Initialize the libshare API if it hasn't already been initialized. 5074180Sdougm * In all cases it returns 0 if it succeeded and an error if not. The 5084180Sdougm * service value is which part(s) of the API to initialize and is a 5094180Sdougm * direct map to the libshare sa_init(service) interface. 5104180Sdougm */ 5114180Sdougm 5124180Sdougm int 5134180Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service) 5144180Sdougm { 5154180Sdougm int ret = SA_OK; 5164180Sdougm 5174302Sdougm if (_sa_init == NULL) 5184302Sdougm ret = SA_CONFIG_ERR; 5194302Sdougm 5204302Sdougm if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL) 5214302Sdougm zhandle->libzfs_sharehdl = _sa_init(service); 5224302Sdougm 5234302Sdougm if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL) 5244302Sdougm ret = SA_NO_MEMORY; 5254302Sdougm 5264180Sdougm return (ret); 5274180Sdougm } 5284180Sdougm 5294180Sdougm /* 5304180Sdougm * zfs_uninit_libshare(zhandle) 5314180Sdougm * 5324180Sdougm * Uninitialize the libshare API if it hasn't already been 5334180Sdougm * uninitialized. It is OK to call multiple times. 5344180Sdougm */ 5354180Sdougm 5364180Sdougm void 5374180Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle) 5384180Sdougm { 5394180Sdougm 5404180Sdougm if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) { 5414302Sdougm if (_sa_fini != NULL) 5424302Sdougm _sa_fini(zhandle->libzfs_sharehdl); 5434302Sdougm zhandle->libzfs_sharehdl = NULL; 5444180Sdougm } 5454180Sdougm } 5464180Sdougm 5474180Sdougm /* 5484180Sdougm * zfs_parse_options(options, proto) 5494180Sdougm * 5504180Sdougm * Call the legacy parse interface to get the protocol specific 5514180Sdougm * options using the NULL arg to indicate that this is a "parse" only. 5524180Sdougm */ 5534180Sdougm 5544180Sdougm int 5554180Sdougm zfs_parse_options(char *options, char *proto) 5564180Sdougm { 5574180Sdougm int ret; 5584180Sdougm 5594180Sdougm if (_sa_parse_legacy_options != NULL) 5604302Sdougm ret = _sa_parse_legacy_options(NULL, options, proto); 5614180Sdougm else 5624302Sdougm ret = SA_CONFIG_ERR; 5634180Sdougm return (ret); 5644180Sdougm } 5654180Sdougm 5664180Sdougm /* 5674180Sdougm * zfs_sa_find_share(handle, path) 5684180Sdougm * 5694180Sdougm * wrapper around sa_find_share to find a share path in the 5704180Sdougm * configuration. 5714180Sdougm */ 5724180Sdougm 5734180Sdougm static sa_share_t 5744180Sdougm zfs_sa_find_share(sa_handle_t handle, char *path) 5754180Sdougm { 5764180Sdougm if (_sa_find_share != NULL) 5774302Sdougm return (_sa_find_share(handle, path)); 5784180Sdougm return (NULL); 5794180Sdougm } 5804180Sdougm 5814180Sdougm /* 5824180Sdougm * zfs_sa_enable_share(share, proto) 5834180Sdougm * 5844180Sdougm * Wrapper for sa_enable_share which enables a share for a specified 5854180Sdougm * protocol. 5864180Sdougm */ 5874180Sdougm 5884180Sdougm static int 5894180Sdougm zfs_sa_enable_share(sa_share_t share, char *proto) 5904180Sdougm { 5914180Sdougm if (_sa_enable_share != NULL) 5924302Sdougm return (_sa_enable_share(share, proto)); 5934180Sdougm return (SA_CONFIG_ERR); 5944180Sdougm } 5954180Sdougm 5964180Sdougm /* 5974180Sdougm * zfs_sa_disable_share(share, proto) 5984180Sdougm * 5994180Sdougm * Wrapper for sa_enable_share which disables a share for a specified 6004180Sdougm * protocol. 6014180Sdougm */ 6024180Sdougm 6034180Sdougm static int 6044180Sdougm zfs_sa_disable_share(sa_share_t share, char *proto) 6054180Sdougm { 6064180Sdougm if (_sa_disable_share != NULL) 6074302Sdougm return (_sa_disable_share(share, proto)); 6084180Sdougm return (SA_CONFIG_ERR); 6094180Sdougm } 6104180Sdougm 6114180Sdougm /* 612789Sahrens * Share the given filesystem according to the options in 'sharenfs'. We rely 6134180Sdougm * on "libshare" to the dirty work for us. 614789Sahrens */ 6154180Sdougm 616789Sahrens int 6173126Sahl zfs_share_nfs(zfs_handle_t *zhp) 618789Sahrens { 619789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 620789Sahrens char shareopts[ZFS_MAXPROPLEN]; 6212082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 6224180Sdougm sa_share_t share; 6234180Sdougm int ret; 624789Sahrens 6252676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 626789Sahrens return (0); 627789Sahrens 6283126Sahl /* 6293126Sahl * Return success if there are no share options. 6303126Sahl */ 631789Sahrens if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts), 6322082Seschrock NULL, NULL, 0, B_FALSE) != 0 || 633789Sahrens strcmp(shareopts, "off") == 0) 634789Sahrens return (0); 635789Sahrens 636789Sahrens /* 6372676Seschrock * If the 'zoned' property is set, then zfs_is_mountable() will have 6382676Seschrock * already bailed out if we are in the global zone. But local 6392676Seschrock * zones cannot be NFS servers, so we ignore it for local zones as well. 640789Sahrens */ 6412676Seschrock if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 642789Sahrens return (0); 643789Sahrens 6444180Sdougm if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 6454302Sdougm (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 6464302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s': %s"), 6474302Sdougm zfs_get_name(zhp), _sa_errorstr(ret)); 6484302Sdougm return (-1); 6494180Sdougm } 6504180Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint); 6514180Sdougm if (share != NULL) { 6524302Sdougm int err; 6534302Sdougm err = zfs_sa_enable_share(share, "nfs"); 6544302Sdougm if (err != SA_OK) { 6554302Sdougm (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 6564302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 6574302Sdougm zfs_get_name(zhp)); 6584302Sdougm return (-1); 6594302Sdougm } 6604180Sdougm } else { 6614180Sdougm (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 6624302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 6634302Sdougm zfs_get_name(zhp)); 664789Sahrens return (-1); 665789Sahrens } 666789Sahrens 667789Sahrens return (0); 668789Sahrens } 669789Sahrens 670789Sahrens /* 6712474Seschrock * Unshare a filesystem by mountpoint. 6722474Seschrock */ 6732474Seschrock static int 6742474Seschrock unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint) 6752474Seschrock { 6764180Sdougm sa_share_t share; 6774180Sdougm int err; 6784180Sdougm char *mntpt; 6792474Seschrock 6802474Seschrock /* 6814180Sdougm * Mountpoint could get trashed if libshare calls getmntany 6824180Sdougm * which id does during API initialization, so strdup the 6834180Sdougm * value. 6842474Seschrock */ 6854180Sdougm mntpt = zfs_strdup(hdl, mountpoint); 6862474Seschrock 6874180Sdougm /* make sure libshare initialized */ 6884180Sdougm if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 6894180Sdougm free(mntpt); /* don't need the copy anymore */ 6904180Sdougm return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 6914302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 6924302Sdougm name, _sa_errorstr(err))); 6932474Seschrock } 6942474Seschrock 6954180Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt); 6964180Sdougm free(mntpt); /* don't need the copy anymore */ 6972474Seschrock 6984180Sdougm if (share != NULL) { 6994180Sdougm err = zfs_sa_disable_share(share, "nfs"); 7004180Sdougm if (err != SA_OK) { 7014302Sdougm return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 7024302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 7034302Sdougm name, _sa_errorstr(err))); 7044180Sdougm } 7054180Sdougm } else { 7064180Sdougm return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 7074302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"), 7084302Sdougm name)); 7094180Sdougm } 7102474Seschrock return (0); 7112474Seschrock } 7122474Seschrock 7132474Seschrock /* 714789Sahrens * Unshare the given filesystem. 715789Sahrens */ 716789Sahrens int 7173126Sahl zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 718789Sahrens { 719789Sahrens struct mnttab search = { 0 }, entry; 7204180Sdougm char *mntpt = NULL; 721789Sahrens 722789Sahrens /* check to see if need to unmount the filesystem */ 723789Sahrens search.mnt_special = (char *)zfs_get_name(zhp); 7241407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 7252082Seschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 7264302Sdougm if (mountpoint != NULL) 7274302Sdougm mountpoint = mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint); 7284302Sdougm 729789Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 7302082Seschrock getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 731789Sahrens 732789Sahrens if (mountpoint == NULL) 733789Sahrens mountpoint = entry.mnt_mountp; 734789Sahrens 7352474Seschrock if (is_shared(zhp->zfs_hdl, mountpoint) && 7364180Sdougm unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) { 7374180Sdougm if (mntpt != NULL) 7384180Sdougm free(mntpt); 7392474Seschrock return (-1); 7404180Sdougm } 741789Sahrens } 7424180Sdougm if (mntpt != NULL) 7434302Sdougm free(mntpt); 744789Sahrens 745789Sahrens return (0); 746789Sahrens } 747789Sahrens 748789Sahrens /* 7493126Sahl * Same as zfs_unmountall(), but for NFS unshares. 750789Sahrens */ 751789Sahrens int 7523126Sahl zfs_unshareall_nfs(zfs_handle_t *zhp) 753789Sahrens { 754789Sahrens prop_changelist_t *clp; 755789Sahrens int ret; 756789Sahrens 757789Sahrens clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0); 758789Sahrens if (clp == NULL) 759789Sahrens return (-1); 760789Sahrens 761789Sahrens ret = changelist_unshare(clp); 762789Sahrens changelist_free(clp); 763789Sahrens 764789Sahrens return (ret); 765789Sahrens } 766789Sahrens 767789Sahrens /* 768789Sahrens * Remove the mountpoint associated with the current dataset, if necessary. 769789Sahrens * We only remove the underlying directory if: 770789Sahrens * 771789Sahrens * - The mountpoint is not 'none' or 'legacy' 772789Sahrens * - The mountpoint is non-empty 773789Sahrens * - The mountpoint is the default or inherited 774789Sahrens * - The 'zoned' property is set, or we're in a local zone 775789Sahrens * 776789Sahrens * Any other directories we leave alone. 777789Sahrens */ 778789Sahrens void 779789Sahrens remove_mountpoint(zfs_handle_t *zhp) 780789Sahrens { 781789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 782*5094Slling zprop_source_t source; 783789Sahrens 7842676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 7852676Seschrock &source)) 786789Sahrens return; 787789Sahrens 788*5094Slling if (source == ZPROP_SRC_DEFAULT || 789*5094Slling source == ZPROP_SRC_INHERITED) { 790789Sahrens /* 791789Sahrens * Try to remove the directory, silently ignoring any errors. 792789Sahrens * The filesystem may have since been removed or moved around, 7932676Seschrock * and this error isn't really useful to the administrator in 7942676Seschrock * any way. 795789Sahrens */ 796789Sahrens (void) rmdir(mountpoint); 797789Sahrens } 798789Sahrens } 7992474Seschrock 8003126Sahl boolean_t 8013126Sahl zfs_is_shared_iscsi(zfs_handle_t *zhp) 8023126Sahl { 8034543Smarks 8044543Smarks /* 8054543Smarks * If iscsi deamon isn't running then we aren't shared 8064543Smarks */ 8074543Smarks if (iscsitgt_svc_online && iscsitgt_svc_online() == 1) 8084543Smarks return (0); 8094543Smarks else 8104543Smarks return (iscsitgt_zfs_is_shared != NULL && 8114543Smarks iscsitgt_zfs_is_shared(zhp->zfs_name) != 0); 8123126Sahl } 8133126Sahl 8143126Sahl int 8153126Sahl zfs_share_iscsi(zfs_handle_t *zhp) 8163126Sahl { 8173126Sahl char shareopts[ZFS_MAXPROPLEN]; 8183126Sahl const char *dataset = zhp->zfs_name; 8193126Sahl libzfs_handle_t *hdl = zhp->zfs_hdl; 8203126Sahl 8213126Sahl /* 8223126Sahl * Return success if there are no share options. 8233126Sahl */ 8243126Sahl if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts, 8253126Sahl sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 || 8263126Sahl strcmp(shareopts, "off") == 0) 8273126Sahl return (0); 8283126Sahl 8294543Smarks if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) { 8304543Smarks int error = EZFS_SHAREISCSIFAILED; 8314543Smarks 8324543Smarks /* 8334543Smarks * If service isn't availabele and EPERM was 8344543Smarks * returned then use special error. 8354543Smarks */ 8364543Smarks if (iscsitgt_svc_online && errno == EPERM && 8374543Smarks (iscsitgt_svc_online() != 0)) 8384543Smarks error = EZFS_ISCSISVCUNAVAIL; 8394543Smarks 8404543Smarks return (zfs_error_fmt(hdl, error, 8413126Sahl dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset)); 8424543Smarks } 8433126Sahl 8443126Sahl return (0); 8453126Sahl } 8463126Sahl 8473126Sahl int 8483126Sahl zfs_unshare_iscsi(zfs_handle_t *zhp) 8493126Sahl { 8503126Sahl const char *dataset = zfs_get_name(zhp); 8513126Sahl libzfs_handle_t *hdl = zhp->zfs_hdl; 8523126Sahl 8533126Sahl /* 8543380Sgw25295 * Return if the volume is not shared 8553380Sgw25295 */ 8563380Sgw25295 if (!zfs_is_shared_iscsi(zhp)) 8573380Sgw25295 return (0); 8583380Sgw25295 8593380Sgw25295 /* 8603126Sahl * If this fails with ENODEV it indicates that zvol wasn't shared so 8613126Sahl * we should return success in that case. 8623126Sahl */ 8633134Sahl if (iscsitgt_zfs_unshare == NULL || 8644543Smarks (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) { 8654543Smarks if (errno == EPERM) 8664543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8674543Smarks "Insufficient privileges to unshare iscsi")); 8683237Slling return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED, 8693126Sahl dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset)); 8704543Smarks } 8713126Sahl 8723126Sahl return (0); 8733126Sahl } 8743126Sahl 8752474Seschrock typedef struct mount_cbdata { 8762474Seschrock zfs_handle_t **cb_datasets; 8772474Seschrock int cb_used; 8782474Seschrock int cb_alloc; 8792474Seschrock } mount_cbdata_t; 8802474Seschrock 8812474Seschrock static int 8822474Seschrock mount_cb(zfs_handle_t *zhp, void *data) 8832474Seschrock { 8842474Seschrock mount_cbdata_t *cbp = data; 8852474Seschrock 8863126Sahl if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) { 8872474Seschrock zfs_close(zhp); 8882474Seschrock return (0); 8892474Seschrock } 8902474Seschrock 8912474Seschrock if (cbp->cb_alloc == cbp->cb_used) { 8922676Seschrock void *ptr; 8932474Seschrock 8942676Seschrock if ((ptr = zfs_realloc(zhp->zfs_hdl, 8952676Seschrock cbp->cb_datasets, cbp->cb_alloc * sizeof (void *), 8962676Seschrock cbp->cb_alloc * 2 * sizeof (void *))) == NULL) 8972474Seschrock return (-1); 8982676Seschrock cbp->cb_datasets = ptr; 8992474Seschrock 9002676Seschrock cbp->cb_alloc *= 2; 9012474Seschrock } 9022474Seschrock 9032474Seschrock cbp->cb_datasets[cbp->cb_used++] = zhp; 9043126Sahl 9053126Sahl return (zfs_iter_children(zhp, mount_cb, cbp)); 9062474Seschrock } 9072474Seschrock 9082474Seschrock static int 9093126Sahl dataset_cmp(const void *a, const void *b) 9102474Seschrock { 9112474Seschrock zfs_handle_t **za = (zfs_handle_t **)a; 9122474Seschrock zfs_handle_t **zb = (zfs_handle_t **)b; 9132474Seschrock char mounta[MAXPATHLEN]; 9142474Seschrock char mountb[MAXPATHLEN]; 9153126Sahl boolean_t gota, gotb; 9162474Seschrock 9173126Sahl if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 9183126Sahl verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 9193126Sahl sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 9203126Sahl if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 9213126Sahl verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 9223126Sahl sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 9232474Seschrock 9243126Sahl if (gota && gotb) 9253126Sahl return (strcmp(mounta, mountb)); 9263126Sahl 9273126Sahl if (gota) 9283126Sahl return (-1); 9293126Sahl if (gotb) 9303126Sahl return (1); 9313126Sahl 9323126Sahl return (strcmp(zfs_get_name(a), zfs_get_name(b))); 9332474Seschrock } 9342474Seschrock 9353126Sahl /* 9363126Sahl * Mount and share all datasets within the given pool. This assumes that no 9373126Sahl * datasets within the pool are currently mounted. Because users can create 9383126Sahl * complicated nested hierarchies of mountpoints, we first gather all the 9393126Sahl * datasets and mountpoints within the pool, and sort them by mountpoint. Once 9403126Sahl * we have the list of all filesystems, we iterate over them in order and mount 9413126Sahl * and/or share each one. 9423126Sahl */ 9433126Sahl #pragma weak zpool_mount_datasets = zpool_enable_datasets 9442474Seschrock int 9453126Sahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 9462474Seschrock { 9472474Seschrock mount_cbdata_t cb = { 0 }; 9482474Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9492474Seschrock zfs_handle_t *zfsp; 9502474Seschrock int i, ret = -1; 9514180Sdougm int *good; 9522474Seschrock 9532474Seschrock /* 9542474Seschrock * Gather all datasets within the pool. 9552474Seschrock */ 9562474Seschrock if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL) 9572474Seschrock return (-1); 9582474Seschrock cb.cb_alloc = 4; 9592474Seschrock 960*5094Slling if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL) 9612474Seschrock goto out; 9622474Seschrock 9632474Seschrock cb.cb_datasets[0] = zfsp; 9642474Seschrock cb.cb_used = 1; 9652474Seschrock 9662474Seschrock if (zfs_iter_children(zfsp, mount_cb, &cb) != 0) 9672474Seschrock goto out; 9682474Seschrock 9692474Seschrock /* 9702474Seschrock * Sort the datasets by mountpoint. 9712474Seschrock */ 9723126Sahl qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp); 9732474Seschrock 9742474Seschrock /* 9754180Sdougm * And mount all the datasets, keeping track of which ones 9764180Sdougm * succeeded or failed. By using zfs_alloc(), the good pointer 9774180Sdougm * will always be non-NULL. 9782474Seschrock */ 9794180Sdougm good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int)); 9802474Seschrock ret = 0; 9812474Seschrock for (i = 0; i < cb.cb_used; i++) { 9824302Sdougm if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0) 9834180Sdougm ret = -1; 9844302Sdougm else 9854180Sdougm good[i] = 1; 9864180Sdougm } 9874180Sdougm /* 9884180Sdougm * Then share all the ones that need to be shared. This needs 9894180Sdougm * to be a separate pass in order to avoid excessive reloading 9904180Sdougm * of the configuration. Good should never be NULL since 9914180Sdougm * zfs_alloc is supposed to exit if memory isn't available. 9924180Sdougm */ 9934180Sdougm zfs_uninit_libshare(hdl); 9944180Sdougm for (i = 0; i < cb.cb_used; i++) { 9954180Sdougm if (good[i] && zfs_share(cb.cb_datasets[i]) != 0) 9962474Seschrock ret = -1; 9972474Seschrock } 9982474Seschrock 9994180Sdougm free(good); 10004180Sdougm 10012474Seschrock out: 10022474Seschrock for (i = 0; i < cb.cb_used; i++) 10032474Seschrock zfs_close(cb.cb_datasets[i]); 10042474Seschrock free(cb.cb_datasets); 10052474Seschrock 10062474Seschrock return (ret); 10072474Seschrock } 10082474Seschrock 10093126Sahl 10103126Sahl static int 10113126Sahl zvol_cb(const char *dataset, void *data) 10123126Sahl { 10133126Sahl libzfs_handle_t *hdl = data; 10143126Sahl zfs_handle_t *zhp; 10153126Sahl 10163126Sahl /* 10173126Sahl * Ignore snapshots and ignore failures from non-existant datasets. 10183126Sahl */ 10193126Sahl if (strchr(dataset, '@') != NULL || 10203126Sahl (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL) 10213126Sahl return (0); 10223126Sahl 10234543Smarks if (zfs_unshare_iscsi(zhp) != 0) 10244543Smarks return (-1); 10253126Sahl 10263126Sahl zfs_close(zhp); 10273126Sahl 10283126Sahl return (0); 10293126Sahl } 10303126Sahl 10312474Seschrock static int 10322474Seschrock mountpoint_compare(const void *a, const void *b) 10332474Seschrock { 10342474Seschrock const char *mounta = *((char **)a); 10352474Seschrock const char *mountb = *((char **)b); 10362474Seschrock 10372474Seschrock return (strcmp(mountb, mounta)); 10382474Seschrock } 10392474Seschrock 10403126Sahl /* 10413126Sahl * Unshare and unmount all datasets within the given pool. We don't want to 10423126Sahl * rely on traversing the DSL to discover the filesystems within the pool, 10433126Sahl * because this may be expensive (if not all of them are mounted), and can fail 10443126Sahl * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 10453126Sahl * gather all the filesystems that are currently mounted. 10463126Sahl */ 10473126Sahl #pragma weak zpool_unmount_datasets = zpool_disable_datasets 10482474Seschrock int 10493126Sahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 10502474Seschrock { 10512474Seschrock int used, alloc; 10522474Seschrock struct mnttab entry; 10532474Seschrock size_t namelen; 10542474Seschrock char **mountpoints = NULL; 10552474Seschrock zfs_handle_t **datasets = NULL; 10562474Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10572474Seschrock int i; 10582474Seschrock int ret = -1; 10592474Seschrock int flags = (force ? MS_FORCE : 0); 10602474Seschrock 10613126Sahl /* 10623126Sahl * First unshare all zvols. 10633126Sahl */ 10643126Sahl if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0) 10653126Sahl return (-1); 10663126Sahl 10672474Seschrock namelen = strlen(zhp->zpool_name); 10682474Seschrock 10692474Seschrock rewind(hdl->libzfs_mnttab); 10702474Seschrock used = alloc = 0; 10712474Seschrock while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 10722474Seschrock /* 10732474Seschrock * Ignore non-ZFS entries. 10742474Seschrock */ 10752474Seschrock if (entry.mnt_fstype == NULL || 10762474Seschrock strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 10772474Seschrock continue; 10782474Seschrock 10792474Seschrock /* 10802474Seschrock * Ignore filesystems not within this pool. 10812474Seschrock */ 10822474Seschrock if (entry.mnt_mountp == NULL || 10832474Seschrock strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 10842474Seschrock (entry.mnt_special[namelen] != '/' && 10852474Seschrock entry.mnt_special[namelen] != '\0')) 10862474Seschrock continue; 10872474Seschrock 10882474Seschrock /* 10892474Seschrock * At this point we've found a filesystem within our pool. Add 10902474Seschrock * it to our growing list. 10912474Seschrock */ 10922474Seschrock if (used == alloc) { 10932474Seschrock if (alloc == 0) { 10942474Seschrock if ((mountpoints = zfs_alloc(hdl, 10952474Seschrock 8 * sizeof (void *))) == NULL) 10962474Seschrock goto out; 10972474Seschrock 10982474Seschrock if ((datasets = zfs_alloc(hdl, 10992474Seschrock 8 * sizeof (void *))) == NULL) 11002474Seschrock goto out; 11012474Seschrock 11022474Seschrock alloc = 8; 11032474Seschrock } else { 11042676Seschrock void *ptr; 11052474Seschrock 11062676Seschrock if ((ptr = zfs_realloc(hdl, mountpoints, 11072676Seschrock alloc * sizeof (void *), 11082474Seschrock alloc * 2 * sizeof (void *))) == NULL) 11092474Seschrock goto out; 11102676Seschrock mountpoints = ptr; 11112474Seschrock 11122676Seschrock if ((ptr = zfs_realloc(hdl, datasets, 11132676Seschrock alloc * sizeof (void *), 11142474Seschrock alloc * 2 * sizeof (void *))) == NULL) 11152474Seschrock goto out; 11162676Seschrock datasets = ptr; 11172474Seschrock 11182474Seschrock alloc *= 2; 11192474Seschrock } 11202474Seschrock } 11212474Seschrock 11222474Seschrock if ((mountpoints[used] = zfs_strdup(hdl, 11232474Seschrock entry.mnt_mountp)) == NULL) 11242474Seschrock goto out; 11252474Seschrock 11262474Seschrock /* 11272474Seschrock * This is allowed to fail, in case there is some I/O error. It 11282474Seschrock * is only used to determine if we need to remove the underlying 11292474Seschrock * mountpoint, so failure is not fatal. 11302474Seschrock */ 11312474Seschrock datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 11322474Seschrock 11332474Seschrock used++; 11342474Seschrock } 11352474Seschrock 11362474Seschrock /* 11372474Seschrock * At this point, we have the entire list of filesystems, so sort it by 11382474Seschrock * mountpoint. 11392474Seschrock */ 11402474Seschrock qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 11412474Seschrock 11422474Seschrock /* 11432474Seschrock * Walk through and first unshare everything. 11442474Seschrock */ 11452474Seschrock for (i = 0; i < used; i++) { 11462474Seschrock if (is_shared(hdl, mountpoints[i]) && 11472676Seschrock unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0) 11482474Seschrock goto out; 11492474Seschrock } 11502474Seschrock 11512474Seschrock /* 11522474Seschrock * Now unmount everything, removing the underlying directories as 11532474Seschrock * appropriate. 11542474Seschrock */ 11552474Seschrock for (i = 0; i < used; i++) { 11562474Seschrock if (unmount_one(hdl, mountpoints[i], flags) != 0) 11572474Seschrock goto out; 11582676Seschrock } 11592474Seschrock 11602676Seschrock for (i = 0; i < used; i++) { 11612474Seschrock if (datasets[i]) 11622474Seschrock remove_mountpoint(datasets[i]); 11632474Seschrock } 11642474Seschrock 11652474Seschrock ret = 0; 11662474Seschrock out: 11672474Seschrock for (i = 0; i < used; i++) { 11682474Seschrock if (datasets[i]) 11692474Seschrock zfs_close(datasets[i]); 11702474Seschrock free(mountpoints[i]); 11712474Seschrock } 11722474Seschrock free(datasets); 11732474Seschrock free(mountpoints); 11742474Seschrock 11752474Seschrock return (ret); 11762474Seschrock } 1177