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() 48*5331Samw * zfs_is_shared_smb() 49*5331Samw * zfs_is_shared_iscsi() 50*5331Samw * zfs_share_proto() 51*5331Samw * zfs_shareall(); 52*5331Samw * zfs_share_iscsi() 533126Sahl * zfs_unshare_nfs() 54*5331Samw * zfs_unshare_smb() 553126Sahl * zfs_unshareall_nfs() 56*5331Samw * zfs_unshareall_smb() 57*5331Samw * zfs_unshareall() 58*5331Samw * zfs_unshareall_bypath() 593126Sahl * zfs_unshare_iscsi() 602474Seschrock * 612474Seschrock * The following functions are available for pool consumers, and will 623126Sahl * mount/unmount and share/unshare all datasets within pool: 632474Seschrock * 643126Sahl * zpool_enable_datasets() 653126Sahl * zpool_disable_datasets() 66789Sahrens */ 67789Sahrens 68789Sahrens #include <dirent.h> 693134Sahl #include <dlfcn.h> 70789Sahrens #include <errno.h> 71789Sahrens #include <libgen.h> 72789Sahrens #include <libintl.h> 73789Sahrens #include <stdio.h> 74789Sahrens #include <stdlib.h> 75789Sahrens #include <strings.h> 76789Sahrens #include <unistd.h> 77789Sahrens #include <zone.h> 78789Sahrens #include <sys/mntent.h> 79789Sahrens #include <sys/mnttab.h> 80789Sahrens #include <sys/mount.h> 81789Sahrens #include <sys/stat.h> 82789Sahrens 83789Sahrens #include <libzfs.h> 84789Sahrens 85789Sahrens #include "libzfs_impl.h" 86789Sahrens 874180Sdougm #include <libshare.h> 884180Sdougm #include <sys/systeminfo.h> 894180Sdougm #define MAXISALEN 257 /* based on sysinfo(2) man page */ 904180Sdougm 91*5331Samw static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *); 92*5331Samw zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **, 93*5331Samw zfs_share_proto_t); 94*5331Samw 953134Sahl static int (*iscsitgt_zfs_share)(const char *); 963134Sahl static int (*iscsitgt_zfs_unshare)(const char *); 973134Sahl static int (*iscsitgt_zfs_is_shared)(const char *); 984543Smarks static int (*iscsitgt_svc_online)(); 993134Sahl 100*5331Samw /* 101*5331Samw * The share protocols table must be in the same order as the zfs_share_prot_t 102*5331Samw * enum in libzfs_impl.h 103*5331Samw */ 104*5331Samw typedef struct { 105*5331Samw zfs_prop_t p_prop; 106*5331Samw char *p_name; 107*5331Samw int p_share_err; 108*5331Samw int p_unshare_err; 109*5331Samw } proto_table_t; 110*5331Samw 111*5331Samw proto_table_t proto_table[PROTO_END] = { 112*5331Samw {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED}, 113*5331Samw {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED}, 114*5331Samw }; 115*5331Samw 116*5331Samw zfs_share_proto_t nfs_only[] = { 117*5331Samw PROTO_NFS, 118*5331Samw PROTO_END 119*5331Samw }; 120*5331Samw 121*5331Samw zfs_share_proto_t smb_only[] = { 122*5331Samw PROTO_SMB, 123*5331Samw PROTO_END 124*5331Samw }; 125*5331Samw zfs_share_proto_t share_all_proto[] = { 126*5331Samw PROTO_NFS, 127*5331Samw PROTO_SMB, 128*5331Samw PROTO_END 129*5331Samw }; 130*5331Samw 1313134Sahl #pragma init(zfs_iscsi_init) 1323134Sahl static void 1333134Sahl zfs_iscsi_init(void) 1343134Sahl { 1353134Sahl void *libiscsitgt; 1363134Sahl 1373134Sahl if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1", 1383134Sahl RTLD_LAZY | RTLD_GLOBAL)) == NULL || 1393134Sahl (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt, 1403134Sahl "iscsitgt_zfs_share")) == NULL || 1413134Sahl (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt, 1423134Sahl "iscsitgt_zfs_unshare")) == NULL || 1433134Sahl (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt, 1444543Smarks "iscsitgt_zfs_is_shared")) == NULL || 1454543Smarks (iscsitgt_svc_online = (int (*)(const char *))dlsym(libiscsitgt, 1464543Smarks "iscsitgt_svc_online")) == NULL) { 1473134Sahl iscsitgt_zfs_share = NULL; 1483134Sahl iscsitgt_zfs_unshare = NULL; 1493134Sahl iscsitgt_zfs_is_shared = NULL; 1504543Smarks iscsitgt_svc_online = NULL; 1513134Sahl } 1523134Sahl } 1533134Sahl 154789Sahrens /* 155*5331Samw * Search the sharetab for the given mountpoint and protocol, returning 156*5331Samw * a zfs_share_type_t value. 157789Sahrens */ 158*5331Samw static zfs_share_type_t 159*5331Samw is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto) 160789Sahrens { 161789Sahrens char buf[MAXPATHLEN], *tab; 162*5331Samw char *ptr; 163789Sahrens 1642082Seschrock if (hdl->libzfs_sharetab == NULL) 165*5331Samw return (SHARED_NOT_SHARED); 166789Sahrens 1672082Seschrock (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET); 168789Sahrens 1692082Seschrock while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) { 170789Sahrens 171789Sahrens /* the mountpoint is the first entry on each line */ 172*5331Samw if ((tab = strchr(buf, '\t')) == NULL) 173*5331Samw continue; 174*5331Samw 175*5331Samw *tab = '\0'; 176*5331Samw if (strcmp(buf, mountpoint) == 0) { 177*5331Samw /* 178*5331Samw * the protocol field is the third field 179*5331Samw * skip over second field 180*5331Samw */ 181*5331Samw ptr = ++tab; 182*5331Samw if ((tab = strchr(ptr, '\t')) == NULL) 183*5331Samw continue; 184*5331Samw ptr = ++tab; 185*5331Samw if ((tab = strchr(ptr, '\t')) == NULL) 186*5331Samw continue; 187789Sahrens *tab = '\0'; 188*5331Samw if (strcmp(ptr, 189*5331Samw proto_table[proto].p_name) == 0) { 190*5331Samw switch (proto) { 191*5331Samw case PROTO_NFS: 192*5331Samw return (SHARED_NFS); 193*5331Samw case PROTO_SMB: 194*5331Samw return (SHARED_SMB); 195*5331Samw default: 196*5331Samw return (0); 197*5331Samw } 198*5331Samw } 199789Sahrens } 200789Sahrens } 201789Sahrens 202*5331Samw return (SHARED_NOT_SHARED); 203789Sahrens } 204789Sahrens 205789Sahrens /* 2062082Seschrock * Returns true if the specified directory is empty. If we can't open the 2072082Seschrock * directory at all, return true so that the mount can fail with a more 208789Sahrens * informative error message. 209789Sahrens */ 2102082Seschrock static boolean_t 211789Sahrens dir_is_empty(const char *dirname) 212789Sahrens { 213789Sahrens DIR *dirp; 214789Sahrens struct dirent64 *dp; 215789Sahrens 216789Sahrens if ((dirp = opendir(dirname)) == NULL) 2172082Seschrock return (B_TRUE); 218789Sahrens 219789Sahrens while ((dp = readdir64(dirp)) != NULL) { 220789Sahrens 221789Sahrens if (strcmp(dp->d_name, ".") == 0 || 222789Sahrens strcmp(dp->d_name, "..") == 0) 223789Sahrens continue; 224789Sahrens 225789Sahrens (void) closedir(dirp); 2262082Seschrock return (B_FALSE); 227789Sahrens } 228789Sahrens 229789Sahrens (void) closedir(dirp); 2302082Seschrock return (B_TRUE); 231789Sahrens } 232789Sahrens 233789Sahrens /* 234789Sahrens * Checks to see if the mount is active. If the filesystem is mounted, we fill 235789Sahrens * in 'where' with the current mountpoint, and return 1. Otherwise, we return 236789Sahrens * 0. 237789Sahrens */ 2382082Seschrock boolean_t 2393444Sek110237 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 240789Sahrens { 241789Sahrens struct mnttab search = { 0 }, entry; 242789Sahrens 243789Sahrens /* 244789Sahrens * Search for the entry in /etc/mnttab. We don't bother getting the 245789Sahrens * mountpoint, as we can just search for the special device. This will 246789Sahrens * also let us find mounts when the mountpoint is 'legacy'. 247789Sahrens */ 2483444Sek110237 search.mnt_special = (char *)special; 2491407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 250789Sahrens 2513444Sek110237 rewind(zfs_hdl->libzfs_mnttab); 2523444Sek110237 if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0) 2532082Seschrock return (B_FALSE); 254789Sahrens 255789Sahrens if (where != NULL) 2563444Sek110237 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 257789Sahrens 2582082Seschrock return (B_TRUE); 259789Sahrens } 260789Sahrens 2613444Sek110237 boolean_t 2623444Sek110237 zfs_is_mounted(zfs_handle_t *zhp, char **where) 2633444Sek110237 { 2643444Sek110237 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 2653444Sek110237 } 2663444Sek110237 267789Sahrens /* 2682676Seschrock * Returns true if the given dataset is mountable, false otherwise. Returns the 2692676Seschrock * mountpoint in 'buf'. 2702676Seschrock */ 2712676Seschrock static boolean_t 2722676Seschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 2735094Slling zprop_source_t *source) 2742676Seschrock { 2752676Seschrock char sourceloc[ZFS_MAXNAMELEN]; 2765094Slling zprop_source_t sourcetype; 2772676Seschrock 2782676Seschrock if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 2792676Seschrock return (B_FALSE); 2802676Seschrock 2812676Seschrock verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 2822676Seschrock &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 2832676Seschrock 2842676Seschrock if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 2852676Seschrock strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 2862676Seschrock return (B_FALSE); 2872676Seschrock 2882676Seschrock if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT)) 2892676Seschrock return (B_FALSE); 2902676Seschrock 2912676Seschrock if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2922676Seschrock getzoneid() == GLOBAL_ZONEID) 2932676Seschrock return (B_FALSE); 2942676Seschrock 2952676Seschrock if (source) 2962676Seschrock *source = sourcetype; 2972676Seschrock 2982676Seschrock return (B_TRUE); 2992676Seschrock } 3002676Seschrock 3012676Seschrock /* 302789Sahrens * Mount the given filesystem. 303789Sahrens */ 304789Sahrens int 305789Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 306789Sahrens { 307789Sahrens struct stat buf; 308789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 309789Sahrens char mntopts[MNT_LINE_MAX]; 3102082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 311789Sahrens 312789Sahrens if (options == NULL) 313789Sahrens mntopts[0] = '\0'; 314789Sahrens else 315789Sahrens (void) strlcpy(mntopts, options, sizeof (mntopts)); 316789Sahrens 3172676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 3182082Seschrock return (0); 319789Sahrens 320789Sahrens /* Create the directory if it doesn't already exist */ 321789Sahrens if (lstat(mountpoint, &buf) != 0) { 322789Sahrens if (mkdirp(mountpoint, 0755) != 0) { 3232082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3242082Seschrock "failed to create mountpoint")); 3253237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 3262082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 3272082Seschrock mountpoint)); 328789Sahrens } 329789Sahrens } 330789Sahrens 331789Sahrens /* 332789Sahrens * Determine if the mountpoint is empty. If so, refuse to perform the 333789Sahrens * mount. We don't perform this check if MS_OVERLAY is specified, which 334789Sahrens * would defeat the point. We also avoid this check if 'remount' is 335789Sahrens * specified. 336789Sahrens */ 337789Sahrens if ((flags & MS_OVERLAY) == 0 && 338789Sahrens strstr(mntopts, MNTOPT_REMOUNT) == NULL && 339789Sahrens !dir_is_empty(mountpoint)) { 3402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3412082Seschrock "directory is not empty")); 3423237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 3432082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 344789Sahrens } 345789Sahrens 346789Sahrens /* perform the mount */ 347789Sahrens if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 348789Sahrens MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 349789Sahrens /* 350789Sahrens * Generic errors are nasty, but there are just way too many 351789Sahrens * from mount(), and they're well-understood. We pick a few 352789Sahrens * common ones to improve upon. 353789Sahrens */ 3544302Sdougm if (errno == EBUSY) { 3552082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3562082Seschrock "mountpoint or dataset is busy")); 3574543Smarks } else if (errno == EPERM) { 3584543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3594543Smarks "Insufficient privileges")); 3604302Sdougm } else { 3612082Seschrock zfs_error_aux(hdl, strerror(errno)); 3624302Sdougm } 3632082Seschrock 3643237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 3652082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 3662082Seschrock zhp->zfs_name)); 367789Sahrens } 368789Sahrens 369789Sahrens return (0); 370789Sahrens } 371789Sahrens 372789Sahrens /* 3732474Seschrock * Unmount a single filesystem. 3742474Seschrock */ 3752474Seschrock static int 3762474Seschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 3772474Seschrock { 3782474Seschrock if (umount2(mountpoint, flags) != 0) { 3792474Seschrock zfs_error_aux(hdl, strerror(errno)); 3803237Slling return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 3812474Seschrock dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 3822474Seschrock mountpoint)); 3832474Seschrock } 3842474Seschrock 3852474Seschrock return (0); 3862474Seschrock } 3872474Seschrock 3882474Seschrock /* 389789Sahrens * Unmount the given filesystem. 390789Sahrens */ 391789Sahrens int 392789Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 393789Sahrens { 394789Sahrens struct mnttab search = { 0 }, entry; 3954180Sdougm char *mntpt = NULL; 396789Sahrens 397789Sahrens /* check to see if need to unmount the filesystem */ 3982474Seschrock search.mnt_special = zhp->zfs_name; 3991407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 4002082Seschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 401789Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 4022082Seschrock getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 403789Sahrens 4044180Sdougm /* 4054180Sdougm * mountpoint may have come from a call to 4064180Sdougm * getmnt/getmntany if it isn't NULL. If it is NULL, 4074180Sdougm * we know it comes from getmntany which can then get 4084180Sdougm * overwritten later. We strdup it to play it safe. 4094180Sdougm */ 410789Sahrens if (mountpoint == NULL) 4114302Sdougm mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 4124180Sdougm else 4134302Sdougm mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint); 414789Sahrens 415789Sahrens /* 4162474Seschrock * Unshare and unmount the filesystem 417789Sahrens */ 418*5331Samw if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) 4194331Sth199096 return (-1); 4204331Sth199096 4214331Sth199096 if (unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) { 4224180Sdougm free(mntpt); 423*5331Samw (void) zfs_shareall(zhp); 424789Sahrens return (-1); 4254180Sdougm } 4264180Sdougm free(mntpt); 427789Sahrens } 428789Sahrens 429789Sahrens return (0); 430789Sahrens } 431789Sahrens 432789Sahrens /* 433789Sahrens * Unmount this filesystem and any children inheriting the mountpoint property. 434789Sahrens * To do this, just act like we're changing the mountpoint property, but don't 435789Sahrens * remount the filesystems afterwards. 436789Sahrens */ 437789Sahrens int 438789Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags) 439789Sahrens { 440789Sahrens prop_changelist_t *clp; 441789Sahrens int ret; 442789Sahrens 443789Sahrens clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags); 444789Sahrens if (clp == NULL) 445789Sahrens return (-1); 446789Sahrens 447789Sahrens ret = changelist_prefix(clp); 448789Sahrens changelist_free(clp); 449789Sahrens 450789Sahrens return (ret); 451789Sahrens } 452789Sahrens 4533126Sahl boolean_t 4543126Sahl zfs_is_shared(zfs_handle_t *zhp) 4553126Sahl { 456*5331Samw zfs_share_type_t rc = 0; 457*5331Samw zfs_share_proto_t *curr_proto; 458*5331Samw 4593126Sahl if (ZFS_IS_VOLUME(zhp)) 4603126Sahl return (zfs_is_shared_iscsi(zhp)); 4613126Sahl 462*5331Samw for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 463*5331Samw curr_proto++) 464*5331Samw rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto); 465*5331Samw 466*5331Samw return (rc ? B_TRUE : B_FALSE); 4673126Sahl } 4683126Sahl 4693126Sahl int 4703126Sahl zfs_share(zfs_handle_t *zhp) 4713126Sahl { 4723126Sahl if (ZFS_IS_VOLUME(zhp)) 4733126Sahl return (zfs_share_iscsi(zhp)); 4743126Sahl 475*5331Samw return (zfs_share_proto(zhp, share_all_proto)); 4763126Sahl } 4773126Sahl 4783126Sahl int 4793126Sahl zfs_unshare(zfs_handle_t *zhp) 4803126Sahl { 4813126Sahl if (ZFS_IS_VOLUME(zhp)) 4823126Sahl return (zfs_unshare_iscsi(zhp)); 4833126Sahl 484*5331Samw return (zfs_unshareall(zhp)); 4853126Sahl } 4863126Sahl 487789Sahrens /* 488789Sahrens * Check to see if the filesystem is currently shared. 489789Sahrens */ 490*5331Samw zfs_share_type_t 491*5331Samw zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto) 492789Sahrens { 493789Sahrens char *mountpoint; 494*5331Samw zfs_share_type_t rc; 495789Sahrens 496789Sahrens if (!zfs_is_mounted(zhp, &mountpoint)) 497*5331Samw return (SHARED_NOT_SHARED); 498789Sahrens 499*5331Samw if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) { 500789Sahrens if (where != NULL) 501789Sahrens *where = mountpoint; 502789Sahrens else 503789Sahrens free(mountpoint); 504*5331Samw return (rc); 505789Sahrens } else { 506789Sahrens free(mountpoint); 507*5331Samw return (SHARED_NOT_SHARED); 508789Sahrens } 509789Sahrens } 510789Sahrens 511*5331Samw boolean_t 512*5331Samw zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 513*5331Samw { 514*5331Samw return (zfs_is_shared_proto(zhp, where, 515*5331Samw PROTO_NFS) != SHARED_NOT_SHARED); 516*5331Samw } 517*5331Samw 518*5331Samw boolean_t 519*5331Samw zfs_is_shared_smb(zfs_handle_t *zhp, char **where) 520*5331Samw { 521*5331Samw return (zfs_is_shared_proto(zhp, where, 522*5331Samw PROTO_SMB) != SHARED_NOT_SHARED); 523*5331Samw } 524*5331Samw 525789Sahrens /* 5264180Sdougm * Make sure things will work if libshare isn't installed by using 5274180Sdougm * wrapper functions that check to see that the pointers to functions 5284180Sdougm * initialized in _zfs_init_libshare() are actually present. 5294180Sdougm */ 5304180Sdougm 5314180Sdougm static sa_handle_t (*_sa_init)(int); 5324180Sdougm static void (*_sa_fini)(sa_handle_t); 5334180Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *); 5344180Sdougm static int (*_sa_enable_share)(sa_share_t, char *); 5354180Sdougm static int (*_sa_disable_share)(sa_share_t, char *); 5364180Sdougm static char *(*_sa_errorstr)(int); 5374180Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *); 5384180Sdougm 5394180Sdougm /* 5404180Sdougm * _zfs_init_libshare() 5414180Sdougm * 5424180Sdougm * Find the libshare.so.1 entry points that we use here and save the 5434180Sdougm * values to be used later. This is triggered by the runtime loader. 5444180Sdougm * Make sure the correct ISA version is loaded. 5454180Sdougm */ 5464180Sdougm 5474180Sdougm #pragma init(_zfs_init_libshare) 5484180Sdougm static void 5494180Sdougm _zfs_init_libshare(void) 5504180Sdougm { 5514180Sdougm void *libshare; 5524180Sdougm char path[MAXPATHLEN]; 5534180Sdougm char isa[MAXISALEN]; 5544180Sdougm 5554180Sdougm #if defined(_LP64) 5564180Sdougm if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 5574302Sdougm isa[0] = '\0'; 5584180Sdougm #else 5594180Sdougm isa[0] = '\0'; 5604180Sdougm #endif 5614180Sdougm (void) snprintf(path, MAXPATHLEN, 5624302Sdougm "/usr/lib/%s/libshare.so.1", isa); 5634180Sdougm 5644180Sdougm if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) { 5654302Sdougm _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init"); 5664302Sdougm _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini"); 5674302Sdougm _sa_find_share = (sa_share_t (*)(sa_handle_t, char *)) 5684302Sdougm dlsym(libshare, "sa_find_share"); 5694302Sdougm _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 5704302Sdougm "sa_enable_share"); 5714302Sdougm _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 5724302Sdougm "sa_disable_share"); 5734302Sdougm _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr"); 5744302Sdougm _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *)) 5754302Sdougm dlsym(libshare, "sa_parse_legacy_options"); 5764327Sdougm if (_sa_init == NULL || _sa_fini == NULL || 5774327Sdougm _sa_find_share == NULL || _sa_enable_share == NULL || 5784327Sdougm _sa_disable_share == NULL || _sa_errorstr == NULL || 5794327Sdougm _sa_parse_legacy_options == NULL) { 5804327Sdougm _sa_init = NULL; 5814327Sdougm _sa_fini = NULL; 5824327Sdougm _sa_disable_share = NULL; 5834327Sdougm _sa_enable_share = NULL; 5844327Sdougm _sa_errorstr = NULL; 5854327Sdougm _sa_parse_legacy_options = NULL; 5864327Sdougm (void) dlclose(libshare); 5874327Sdougm } 5884180Sdougm } 5894180Sdougm } 5904180Sdougm 5914180Sdougm /* 5924180Sdougm * zfs_init_libshare(zhandle, service) 5934180Sdougm * 5944180Sdougm * Initialize the libshare API if it hasn't already been initialized. 5954180Sdougm * In all cases it returns 0 if it succeeded and an error if not. The 5964180Sdougm * service value is which part(s) of the API to initialize and is a 5974180Sdougm * direct map to the libshare sa_init(service) interface. 5984180Sdougm */ 5994180Sdougm 6004180Sdougm int 6014180Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service) 6024180Sdougm { 6034180Sdougm int ret = SA_OK; 6044180Sdougm 6054302Sdougm if (_sa_init == NULL) 6064302Sdougm ret = SA_CONFIG_ERR; 6074302Sdougm 6084302Sdougm if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL) 6094302Sdougm zhandle->libzfs_sharehdl = _sa_init(service); 6104302Sdougm 6114302Sdougm if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL) 6124302Sdougm ret = SA_NO_MEMORY; 6134302Sdougm 6144180Sdougm return (ret); 6154180Sdougm } 6164180Sdougm 6174180Sdougm /* 6184180Sdougm * zfs_uninit_libshare(zhandle) 6194180Sdougm * 6204180Sdougm * Uninitialize the libshare API if it hasn't already been 6214180Sdougm * uninitialized. It is OK to call multiple times. 6224180Sdougm */ 6234180Sdougm 6244180Sdougm void 6254180Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle) 6264180Sdougm { 6274180Sdougm 6284180Sdougm if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) { 6294302Sdougm if (_sa_fini != NULL) 6304302Sdougm _sa_fini(zhandle->libzfs_sharehdl); 6314302Sdougm zhandle->libzfs_sharehdl = NULL; 6324180Sdougm } 6334180Sdougm } 6344180Sdougm 6354180Sdougm /* 6364180Sdougm * zfs_parse_options(options, proto) 6374180Sdougm * 6384180Sdougm * Call the legacy parse interface to get the protocol specific 6394180Sdougm * options using the NULL arg to indicate that this is a "parse" only. 6404180Sdougm */ 6414180Sdougm 6424180Sdougm int 643*5331Samw zfs_parse_options(char *options, zfs_share_proto_t proto) 6444180Sdougm { 6454180Sdougm int ret; 6464180Sdougm 6474180Sdougm if (_sa_parse_legacy_options != NULL) 648*5331Samw ret = _sa_parse_legacy_options(NULL, options, 649*5331Samw proto_table[proto].p_name); 6504180Sdougm else 6514302Sdougm ret = SA_CONFIG_ERR; 6524180Sdougm return (ret); 6534180Sdougm } 6544180Sdougm 6554180Sdougm /* 6564180Sdougm * zfs_sa_find_share(handle, path) 6574180Sdougm * 6584180Sdougm * wrapper around sa_find_share to find a share path in the 6594180Sdougm * configuration. 6604180Sdougm */ 6614180Sdougm 6624180Sdougm static sa_share_t 6634180Sdougm zfs_sa_find_share(sa_handle_t handle, char *path) 6644180Sdougm { 6654180Sdougm if (_sa_find_share != NULL) 6664302Sdougm return (_sa_find_share(handle, path)); 6674180Sdougm return (NULL); 6684180Sdougm } 6694180Sdougm 6704180Sdougm /* 6714180Sdougm * zfs_sa_enable_share(share, proto) 6724180Sdougm * 6734180Sdougm * Wrapper for sa_enable_share which enables a share for a specified 6744180Sdougm * protocol. 6754180Sdougm */ 6764180Sdougm 6774180Sdougm static int 6784180Sdougm zfs_sa_enable_share(sa_share_t share, char *proto) 6794180Sdougm { 6804180Sdougm if (_sa_enable_share != NULL) 6814302Sdougm return (_sa_enable_share(share, proto)); 6824180Sdougm return (SA_CONFIG_ERR); 6834180Sdougm } 6844180Sdougm 6854180Sdougm /* 6864180Sdougm * zfs_sa_disable_share(share, proto) 6874180Sdougm * 6884180Sdougm * Wrapper for sa_enable_share which disables a share for a specified 6894180Sdougm * protocol. 6904180Sdougm */ 6914180Sdougm 6924180Sdougm static int 6934180Sdougm zfs_sa_disable_share(sa_share_t share, char *proto) 6944180Sdougm { 6954180Sdougm if (_sa_disable_share != NULL) 6964302Sdougm return (_sa_disable_share(share, proto)); 6974180Sdougm return (SA_CONFIG_ERR); 6984180Sdougm } 6994180Sdougm 7004180Sdougm /* 701*5331Samw * Share the given filesystem according to the options in the specified 702*5331Samw * protocol specific properties (sharenfs, sharesmb). We rely 7034180Sdougm * on "libshare" to the dirty work for us. 704789Sahrens */ 7054180Sdougm 706*5331Samw static int 707*5331Samw zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 708789Sahrens { 709789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 710789Sahrens char shareopts[ZFS_MAXPROPLEN]; 7112082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 7124180Sdougm sa_share_t share; 713*5331Samw zfs_share_proto_t *curr_proto; 7144180Sdougm int ret; 715789Sahrens 7162676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 717789Sahrens return (0); 718789Sahrens 7194180Sdougm if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 7204302Sdougm (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 7214302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s': %s"), 7224302Sdougm zfs_get_name(zhp), _sa_errorstr(ret)); 7234302Sdougm return (-1); 7244180Sdougm } 725*5331Samw 726*5331Samw for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 727*5331Samw /* 728*5331Samw * Return success if there are no share options. 729*5331Samw */ 730*5331Samw if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop, 731*5331Samw shareopts, sizeof (shareopts), NULL, NULL, 732*5331Samw 0, B_FALSE) != 0 || strcmp(shareopts, "off") == 0) 733*5331Samw continue; 734*5331Samw 735*5331Samw /* 736*5331Samw * If the 'zoned' property is set, then zfs_is_mountable() 737*5331Samw * will have already bailed out if we are in the global zone. 738*5331Samw * But local zones cannot be NFS servers, so we ignore it for 739*5331Samw * local zones as well. 740*5331Samw */ 741*5331Samw if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 742*5331Samw continue; 743*5331Samw 744*5331Samw share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint); 745*5331Samw if (share != NULL) { 746*5331Samw int err; 747*5331Samw err = zfs_sa_enable_share(share, 748*5331Samw proto_table[*curr_proto].p_name); 749*5331Samw if (err != SA_OK) { 750*5331Samw (void) zfs_error_fmt(hdl, 751*5331Samw proto_table[*curr_proto].p_share_err, 752*5331Samw dgettext(TEXT_DOMAIN, "cannot share '%s'"), 753*5331Samw zfs_get_name(zhp)); 754*5331Samw return (-1); 755*5331Samw } 756*5331Samw } else { 757*5331Samw (void) zfs_error_fmt(hdl, 758*5331Samw proto_table[*curr_proto].p_share_err, 7594302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 7604302Sdougm zfs_get_name(zhp)); 7614302Sdougm return (-1); 7624302Sdougm } 763*5331Samw 764789Sahrens } 765*5331Samw return (0); 766*5331Samw } 767789Sahrens 768*5331Samw 769*5331Samw int 770*5331Samw zfs_share_nfs(zfs_handle_t *zhp) 771*5331Samw { 772*5331Samw return (zfs_share_proto(zhp, nfs_only)); 773*5331Samw } 774*5331Samw 775*5331Samw int 776*5331Samw zfs_share_smb(zfs_handle_t *zhp) 777*5331Samw { 778*5331Samw return (zfs_share_proto(zhp, smb_only)); 779*5331Samw } 780*5331Samw 781*5331Samw int 782*5331Samw zfs_shareall(zfs_handle_t *zhp) 783*5331Samw { 784*5331Samw return (zfs_share_proto(zhp, share_all_proto)); 785789Sahrens } 786789Sahrens 787789Sahrens /* 7882474Seschrock * Unshare a filesystem by mountpoint. 7892474Seschrock */ 7902474Seschrock static int 791*5331Samw unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, 792*5331Samw zfs_share_proto_t proto) 7932474Seschrock { 7944180Sdougm sa_share_t share; 7954180Sdougm int err; 7964180Sdougm char *mntpt; 7972474Seschrock /* 7984180Sdougm * Mountpoint could get trashed if libshare calls getmntany 7994180Sdougm * which id does during API initialization, so strdup the 8004180Sdougm * value. 8012474Seschrock */ 8024180Sdougm mntpt = zfs_strdup(hdl, mountpoint); 8032474Seschrock 8044180Sdougm /* make sure libshare initialized */ 8054180Sdougm if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 8064180Sdougm free(mntpt); /* don't need the copy anymore */ 8074180Sdougm return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 8084302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 8094302Sdougm name, _sa_errorstr(err))); 8102474Seschrock } 8112474Seschrock 8124180Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt); 8134180Sdougm free(mntpt); /* don't need the copy anymore */ 8142474Seschrock 8154180Sdougm if (share != NULL) { 816*5331Samw err = zfs_sa_disable_share(share, proto_table[proto].p_name); 8174180Sdougm if (err != SA_OK) { 8184302Sdougm return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 8194302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 8204302Sdougm name, _sa_errorstr(err))); 8214180Sdougm } 8224180Sdougm } else { 8234180Sdougm return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 8244302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"), 8254302Sdougm name)); 8264180Sdougm } 8272474Seschrock return (0); 8282474Seschrock } 8292474Seschrock 8302474Seschrock /* 831789Sahrens * Unshare the given filesystem. 832789Sahrens */ 833789Sahrens int 834*5331Samw zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, 835*5331Samw zfs_share_proto_t *proto) 836789Sahrens { 837789Sahrens struct mnttab search = { 0 }, entry; 8384180Sdougm char *mntpt = NULL; 839789Sahrens 840789Sahrens /* check to see if need to unmount the filesystem */ 841789Sahrens search.mnt_special = (char *)zfs_get_name(zhp); 8421407Snd150628 search.mnt_fstype = MNTTYPE_ZFS; 8432082Seschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 8444302Sdougm if (mountpoint != NULL) 845*5331Samw mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint); 8464302Sdougm 847789Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 8482082Seschrock getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 849*5331Samw zfs_share_proto_t *curr_proto; 850789Sahrens 851789Sahrens if (mountpoint == NULL) 852*5331Samw mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 853*5331Samw 854*5331Samw for (curr_proto = proto; *curr_proto != PROTO_END; 855*5331Samw curr_proto++) { 856789Sahrens 857*5331Samw if (is_shared(zhp->zfs_hdl, mntpt, *curr_proto) && 858*5331Samw unshare_one(zhp->zfs_hdl, zhp->zfs_name, 859*5331Samw mntpt, *curr_proto) != 0) { 860*5331Samw if (mntpt != NULL) 861*5331Samw free(mntpt); 862*5331Samw return (-1); 863*5331Samw } 8644180Sdougm } 865789Sahrens } 8664180Sdougm if (mntpt != NULL) 8674302Sdougm free(mntpt); 868789Sahrens 869789Sahrens return (0); 870789Sahrens } 871789Sahrens 872*5331Samw int 873*5331Samw zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 874*5331Samw { 875*5331Samw return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 876*5331Samw } 877*5331Samw 878*5331Samw int 879*5331Samw zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint) 880*5331Samw { 881*5331Samw return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 882*5331Samw } 883*5331Samw 884789Sahrens /* 885*5331Samw * Same as zfs_unmountall(), but for NFS and SMB unshares. 886789Sahrens */ 887789Sahrens int 888*5331Samw zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 889789Sahrens { 890789Sahrens prop_changelist_t *clp; 891789Sahrens int ret; 892789Sahrens 893789Sahrens clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0); 894789Sahrens if (clp == NULL) 895789Sahrens return (-1); 896789Sahrens 897*5331Samw ret = changelist_unshare(clp, proto); 898789Sahrens changelist_free(clp); 899789Sahrens 900789Sahrens return (ret); 901789Sahrens } 902789Sahrens 903*5331Samw int 904*5331Samw zfs_unshareall_nfs(zfs_handle_t *zhp) 905*5331Samw { 906*5331Samw return (zfs_unshareall_proto(zhp, nfs_only)); 907*5331Samw } 908*5331Samw 909*5331Samw int 910*5331Samw zfs_unshareall_smb(zfs_handle_t *zhp) 911*5331Samw { 912*5331Samw return (zfs_unshareall_proto(zhp, smb_only)); 913*5331Samw } 914*5331Samw 915*5331Samw int 916*5331Samw zfs_unshareall(zfs_handle_t *zhp) 917*5331Samw { 918*5331Samw return (zfs_unshareall_proto(zhp, share_all_proto)); 919*5331Samw } 920*5331Samw 921*5331Samw int 922*5331Samw zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint) 923*5331Samw { 924*5331Samw return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 925*5331Samw } 926*5331Samw 927789Sahrens /* 928789Sahrens * Remove the mountpoint associated with the current dataset, if necessary. 929789Sahrens * We only remove the underlying directory if: 930789Sahrens * 931789Sahrens * - The mountpoint is not 'none' or 'legacy' 932789Sahrens * - The mountpoint is non-empty 933789Sahrens * - The mountpoint is the default or inherited 934789Sahrens * - The 'zoned' property is set, or we're in a local zone 935789Sahrens * 936789Sahrens * Any other directories we leave alone. 937789Sahrens */ 938789Sahrens void 939789Sahrens remove_mountpoint(zfs_handle_t *zhp) 940789Sahrens { 941789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 9425094Slling zprop_source_t source; 943789Sahrens 9442676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 9452676Seschrock &source)) 946789Sahrens return; 947789Sahrens 9485094Slling if (source == ZPROP_SRC_DEFAULT || 9495094Slling source == ZPROP_SRC_INHERITED) { 950789Sahrens /* 951789Sahrens * Try to remove the directory, silently ignoring any errors. 952789Sahrens * The filesystem may have since been removed or moved around, 9532676Seschrock * and this error isn't really useful to the administrator in 9542676Seschrock * any way. 955789Sahrens */ 956789Sahrens (void) rmdir(mountpoint); 957789Sahrens } 958789Sahrens } 9592474Seschrock 9603126Sahl boolean_t 9613126Sahl zfs_is_shared_iscsi(zfs_handle_t *zhp) 9623126Sahl { 9634543Smarks 9644543Smarks /* 9654543Smarks * If iscsi deamon isn't running then we aren't shared 9664543Smarks */ 9674543Smarks if (iscsitgt_svc_online && iscsitgt_svc_online() == 1) 968*5331Samw return (B_FALSE); 9694543Smarks else 9704543Smarks return (iscsitgt_zfs_is_shared != NULL && 9714543Smarks iscsitgt_zfs_is_shared(zhp->zfs_name) != 0); 9723126Sahl } 9733126Sahl 9743126Sahl int 9753126Sahl zfs_share_iscsi(zfs_handle_t *zhp) 9763126Sahl { 9773126Sahl char shareopts[ZFS_MAXPROPLEN]; 9783126Sahl const char *dataset = zhp->zfs_name; 9793126Sahl libzfs_handle_t *hdl = zhp->zfs_hdl; 9803126Sahl 9813126Sahl /* 9823126Sahl * Return success if there are no share options. 9833126Sahl */ 9843126Sahl if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts, 9853126Sahl sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 || 9863126Sahl strcmp(shareopts, "off") == 0) 9873126Sahl return (0); 9883126Sahl 9894543Smarks if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) { 9904543Smarks int error = EZFS_SHAREISCSIFAILED; 9914543Smarks 9924543Smarks /* 9934543Smarks * If service isn't availabele and EPERM was 9944543Smarks * returned then use special error. 9954543Smarks */ 9964543Smarks if (iscsitgt_svc_online && errno == EPERM && 9974543Smarks (iscsitgt_svc_online() != 0)) 9984543Smarks error = EZFS_ISCSISVCUNAVAIL; 9994543Smarks 10004543Smarks return (zfs_error_fmt(hdl, error, 10013126Sahl dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset)); 10024543Smarks } 10033126Sahl 10043126Sahl return (0); 10053126Sahl } 10063126Sahl 10073126Sahl int 10083126Sahl zfs_unshare_iscsi(zfs_handle_t *zhp) 10093126Sahl { 10103126Sahl const char *dataset = zfs_get_name(zhp); 10113126Sahl libzfs_handle_t *hdl = zhp->zfs_hdl; 10123126Sahl 10133126Sahl /* 10143380Sgw25295 * Return if the volume is not shared 10153380Sgw25295 */ 1016*5331Samw if (zfs_is_shared_iscsi(zhp) != SHARED_ISCSI) 10173380Sgw25295 return (0); 10183380Sgw25295 10193380Sgw25295 /* 10203126Sahl * If this fails with ENODEV it indicates that zvol wasn't shared so 10213126Sahl * we should return success in that case. 10223126Sahl */ 10233134Sahl if (iscsitgt_zfs_unshare == NULL || 10244543Smarks (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) { 10254543Smarks if (errno == EPERM) 10264543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10274543Smarks "Insufficient privileges to unshare iscsi")); 10283237Slling return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED, 10293126Sahl dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset)); 10304543Smarks } 10313126Sahl 10323126Sahl return (0); 10333126Sahl } 10343126Sahl 10352474Seschrock typedef struct mount_cbdata { 10362474Seschrock zfs_handle_t **cb_datasets; 10372474Seschrock int cb_used; 10382474Seschrock int cb_alloc; 10392474Seschrock } mount_cbdata_t; 10402474Seschrock 10412474Seschrock static int 10422474Seschrock mount_cb(zfs_handle_t *zhp, void *data) 10432474Seschrock { 10442474Seschrock mount_cbdata_t *cbp = data; 10452474Seschrock 10463126Sahl if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) { 10472474Seschrock zfs_close(zhp); 10482474Seschrock return (0); 10492474Seschrock } 10502474Seschrock 10512474Seschrock if (cbp->cb_alloc == cbp->cb_used) { 10522676Seschrock void *ptr; 10532474Seschrock 10542676Seschrock if ((ptr = zfs_realloc(zhp->zfs_hdl, 10552676Seschrock cbp->cb_datasets, cbp->cb_alloc * sizeof (void *), 10562676Seschrock cbp->cb_alloc * 2 * sizeof (void *))) == NULL) 10572474Seschrock return (-1); 10582676Seschrock cbp->cb_datasets = ptr; 10592474Seschrock 10602676Seschrock cbp->cb_alloc *= 2; 10612474Seschrock } 10622474Seschrock 10632474Seschrock cbp->cb_datasets[cbp->cb_used++] = zhp; 10643126Sahl 10653126Sahl return (zfs_iter_children(zhp, mount_cb, cbp)); 10662474Seschrock } 10672474Seschrock 10682474Seschrock static int 10693126Sahl dataset_cmp(const void *a, const void *b) 10702474Seschrock { 10712474Seschrock zfs_handle_t **za = (zfs_handle_t **)a; 10722474Seschrock zfs_handle_t **zb = (zfs_handle_t **)b; 10732474Seschrock char mounta[MAXPATHLEN]; 10742474Seschrock char mountb[MAXPATHLEN]; 10753126Sahl boolean_t gota, gotb; 10762474Seschrock 10773126Sahl if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 10783126Sahl verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 10793126Sahl sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 10803126Sahl if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 10813126Sahl verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 10823126Sahl sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 10832474Seschrock 10843126Sahl if (gota && gotb) 10853126Sahl return (strcmp(mounta, mountb)); 10863126Sahl 10873126Sahl if (gota) 10883126Sahl return (-1); 10893126Sahl if (gotb) 10903126Sahl return (1); 10913126Sahl 10923126Sahl return (strcmp(zfs_get_name(a), zfs_get_name(b))); 10932474Seschrock } 10942474Seschrock 10953126Sahl /* 10963126Sahl * Mount and share all datasets within the given pool. This assumes that no 10973126Sahl * datasets within the pool are currently mounted. Because users can create 10983126Sahl * complicated nested hierarchies of mountpoints, we first gather all the 10993126Sahl * datasets and mountpoints within the pool, and sort them by mountpoint. Once 11003126Sahl * we have the list of all filesystems, we iterate over them in order and mount 11013126Sahl * and/or share each one. 11023126Sahl */ 11033126Sahl #pragma weak zpool_mount_datasets = zpool_enable_datasets 11042474Seschrock int 11053126Sahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 11062474Seschrock { 11072474Seschrock mount_cbdata_t cb = { 0 }; 11082474Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 11092474Seschrock zfs_handle_t *zfsp; 11102474Seschrock int i, ret = -1; 11114180Sdougm int *good; 11122474Seschrock 11132474Seschrock /* 11142474Seschrock * Gather all datasets within the pool. 11152474Seschrock */ 11162474Seschrock if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL) 11172474Seschrock return (-1); 11182474Seschrock cb.cb_alloc = 4; 11192474Seschrock 11205094Slling if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL) 11212474Seschrock goto out; 11222474Seschrock 11232474Seschrock cb.cb_datasets[0] = zfsp; 11242474Seschrock cb.cb_used = 1; 11252474Seschrock 11262474Seschrock if (zfs_iter_children(zfsp, mount_cb, &cb) != 0) 11272474Seschrock goto out; 11282474Seschrock 11292474Seschrock /* 11302474Seschrock * Sort the datasets by mountpoint. 11312474Seschrock */ 11323126Sahl qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp); 11332474Seschrock 11342474Seschrock /* 11354180Sdougm * And mount all the datasets, keeping track of which ones 11364180Sdougm * succeeded or failed. By using zfs_alloc(), the good pointer 11374180Sdougm * will always be non-NULL. 11382474Seschrock */ 11394180Sdougm good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int)); 11402474Seschrock ret = 0; 11412474Seschrock for (i = 0; i < cb.cb_used; i++) { 11424302Sdougm if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0) 11434180Sdougm ret = -1; 11444302Sdougm else 11454180Sdougm good[i] = 1; 11464180Sdougm } 11474180Sdougm /* 11484180Sdougm * Then share all the ones that need to be shared. This needs 11494180Sdougm * to be a separate pass in order to avoid excessive reloading 11504180Sdougm * of the configuration. Good should never be NULL since 11514180Sdougm * zfs_alloc is supposed to exit if memory isn't available. 11524180Sdougm */ 11534180Sdougm zfs_uninit_libshare(hdl); 11544180Sdougm for (i = 0; i < cb.cb_used; i++) { 11554180Sdougm if (good[i] && zfs_share(cb.cb_datasets[i]) != 0) 11562474Seschrock ret = -1; 11572474Seschrock } 11582474Seschrock 11594180Sdougm free(good); 11604180Sdougm 11612474Seschrock out: 11622474Seschrock for (i = 0; i < cb.cb_used; i++) 11632474Seschrock zfs_close(cb.cb_datasets[i]); 11642474Seschrock free(cb.cb_datasets); 11652474Seschrock 11662474Seschrock return (ret); 11672474Seschrock } 11682474Seschrock 11693126Sahl 11703126Sahl static int 11713126Sahl zvol_cb(const char *dataset, void *data) 11723126Sahl { 11733126Sahl libzfs_handle_t *hdl = data; 11743126Sahl zfs_handle_t *zhp; 11753126Sahl 11763126Sahl /* 11773126Sahl * Ignore snapshots and ignore failures from non-existant datasets. 11783126Sahl */ 11793126Sahl if (strchr(dataset, '@') != NULL || 11803126Sahl (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL) 11813126Sahl return (0); 11823126Sahl 11834543Smarks if (zfs_unshare_iscsi(zhp) != 0) 11844543Smarks return (-1); 11853126Sahl 11863126Sahl zfs_close(zhp); 11873126Sahl 11883126Sahl return (0); 11893126Sahl } 11903126Sahl 11912474Seschrock static int 11922474Seschrock mountpoint_compare(const void *a, const void *b) 11932474Seschrock { 11942474Seschrock const char *mounta = *((char **)a); 11952474Seschrock const char *mountb = *((char **)b); 11962474Seschrock 11972474Seschrock return (strcmp(mountb, mounta)); 11982474Seschrock } 11992474Seschrock 12003126Sahl /* 12013126Sahl * Unshare and unmount all datasets within the given pool. We don't want to 12023126Sahl * rely on traversing the DSL to discover the filesystems within the pool, 12033126Sahl * because this may be expensive (if not all of them are mounted), and can fail 12043126Sahl * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 12053126Sahl * gather all the filesystems that are currently mounted. 12063126Sahl */ 12073126Sahl #pragma weak zpool_unmount_datasets = zpool_disable_datasets 12082474Seschrock int 12093126Sahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 12102474Seschrock { 12112474Seschrock int used, alloc; 12122474Seschrock struct mnttab entry; 12132474Seschrock size_t namelen; 12142474Seschrock char **mountpoints = NULL; 12152474Seschrock zfs_handle_t **datasets = NULL; 12162474Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 12172474Seschrock int i; 12182474Seschrock int ret = -1; 12192474Seschrock int flags = (force ? MS_FORCE : 0); 12202474Seschrock 12213126Sahl /* 12223126Sahl * First unshare all zvols. 12233126Sahl */ 12243126Sahl if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0) 12253126Sahl return (-1); 12263126Sahl 12272474Seschrock namelen = strlen(zhp->zpool_name); 12282474Seschrock 12292474Seschrock rewind(hdl->libzfs_mnttab); 12302474Seschrock used = alloc = 0; 12312474Seschrock while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 12322474Seschrock /* 12332474Seschrock * Ignore non-ZFS entries. 12342474Seschrock */ 12352474Seschrock if (entry.mnt_fstype == NULL || 12362474Seschrock strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 12372474Seschrock continue; 12382474Seschrock 12392474Seschrock /* 12402474Seschrock * Ignore filesystems not within this pool. 12412474Seschrock */ 12422474Seschrock if (entry.mnt_mountp == NULL || 12432474Seschrock strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 12442474Seschrock (entry.mnt_special[namelen] != '/' && 12452474Seschrock entry.mnt_special[namelen] != '\0')) 12462474Seschrock continue; 12472474Seschrock 12482474Seschrock /* 12492474Seschrock * At this point we've found a filesystem within our pool. Add 12502474Seschrock * it to our growing list. 12512474Seschrock */ 12522474Seschrock if (used == alloc) { 12532474Seschrock if (alloc == 0) { 12542474Seschrock if ((mountpoints = zfs_alloc(hdl, 12552474Seschrock 8 * sizeof (void *))) == NULL) 12562474Seschrock goto out; 12572474Seschrock 12582474Seschrock if ((datasets = zfs_alloc(hdl, 12592474Seschrock 8 * sizeof (void *))) == NULL) 12602474Seschrock goto out; 12612474Seschrock 12622474Seschrock alloc = 8; 12632474Seschrock } else { 12642676Seschrock void *ptr; 12652474Seschrock 12662676Seschrock if ((ptr = zfs_realloc(hdl, mountpoints, 12672676Seschrock alloc * sizeof (void *), 12682474Seschrock alloc * 2 * sizeof (void *))) == NULL) 12692474Seschrock goto out; 12702676Seschrock mountpoints = ptr; 12712474Seschrock 12722676Seschrock if ((ptr = zfs_realloc(hdl, datasets, 12732676Seschrock alloc * sizeof (void *), 12742474Seschrock alloc * 2 * sizeof (void *))) == NULL) 12752474Seschrock goto out; 12762676Seschrock datasets = ptr; 12772474Seschrock 12782474Seschrock alloc *= 2; 12792474Seschrock } 12802474Seschrock } 12812474Seschrock 12822474Seschrock if ((mountpoints[used] = zfs_strdup(hdl, 12832474Seschrock entry.mnt_mountp)) == NULL) 12842474Seschrock goto out; 12852474Seschrock 12862474Seschrock /* 12872474Seschrock * This is allowed to fail, in case there is some I/O error. It 12882474Seschrock * is only used to determine if we need to remove the underlying 12892474Seschrock * mountpoint, so failure is not fatal. 12902474Seschrock */ 12912474Seschrock datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 12922474Seschrock 12932474Seschrock used++; 12942474Seschrock } 12952474Seschrock 12962474Seschrock /* 12972474Seschrock * At this point, we have the entire list of filesystems, so sort it by 12982474Seschrock * mountpoint. 12992474Seschrock */ 13002474Seschrock qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 13012474Seschrock 13022474Seschrock /* 13032474Seschrock * Walk through and first unshare everything. 13042474Seschrock */ 13052474Seschrock for (i = 0; i < used; i++) { 1306*5331Samw zfs_share_proto_t *curr_proto; 1307*5331Samw for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 1308*5331Samw curr_proto++) { 1309*5331Samw if (is_shared(hdl, mountpoints[i], *curr_proto) && 1310*5331Samw unshare_one(hdl, mountpoints[i], 1311*5331Samw mountpoints[i], *curr_proto) != 0) 1312*5331Samw goto out; 1313*5331Samw } 13142474Seschrock } 13152474Seschrock 13162474Seschrock /* 13172474Seschrock * Now unmount everything, removing the underlying directories as 13182474Seschrock * appropriate. 13192474Seschrock */ 13202474Seschrock for (i = 0; i < used; i++) { 13212474Seschrock if (unmount_one(hdl, mountpoints[i], flags) != 0) 13222474Seschrock goto out; 13232676Seschrock } 13242474Seschrock 13252676Seschrock for (i = 0; i < used; i++) { 13262474Seschrock if (datasets[i]) 13272474Seschrock remove_mountpoint(datasets[i]); 13282474Seschrock } 13292474Seschrock 13302474Seschrock ret = 0; 13312474Seschrock out: 13322474Seschrock for (i = 0; i < used; i++) { 13332474Seschrock if (datasets[i]) 13342474Seschrock zfs_close(datasets[i]); 13352474Seschrock free(mountpoints[i]); 13362474Seschrock } 13372474Seschrock free(datasets); 13382474Seschrock free(mountpoints); 13392474Seschrock 13402474Seschrock return (ret); 13412474Seschrock } 1342