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 /* 2311814SChris.Kirby@sun.com * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens /* 28789Sahrens * Routines to manage ZFS mounts. We separate all the nasty routines that have 293126Sahl * to deal with the OS. The following functions are the main entry points -- 303126Sahl * they are used by mount and unmount and when changing a filesystem's 313126Sahl * mountpoint. 32789Sahrens * 33789Sahrens * zfs_is_mounted() 34789Sahrens * zfs_mount() 35789Sahrens * zfs_unmount() 36789Sahrens * zfs_unmountall() 37789Sahrens * 383126Sahl * This file also contains the functions used to manage sharing filesystems via 393126Sahl * NFS and iSCSI: 40789Sahrens * 41789Sahrens * zfs_is_shared() 42789Sahrens * zfs_share() 43789Sahrens * zfs_unshare() 443126Sahl * 453126Sahl * zfs_is_shared_nfs() 465331Samw * zfs_is_shared_smb() 475331Samw * zfs_share_proto() 485331Samw * zfs_shareall(); 493126Sahl * zfs_unshare_nfs() 505331Samw * zfs_unshare_smb() 513126Sahl * zfs_unshareall_nfs() 525331Samw * zfs_unshareall_smb() 535331Samw * zfs_unshareall() 545331Samw * zfs_unshareall_bypath() 552474Seschrock * 562474Seschrock * The following functions are available for pool consumers, and will 573126Sahl * mount/unmount and share/unshare all datasets within pool: 582474Seschrock * 593126Sahl * zpool_enable_datasets() 603126Sahl * zpool_disable_datasets() 61789Sahrens */ 62789Sahrens 63789Sahrens #include <dirent.h> 643134Sahl #include <dlfcn.h> 65789Sahrens #include <errno.h> 66789Sahrens #include <libgen.h> 67789Sahrens #include <libintl.h> 68789Sahrens #include <stdio.h> 69789Sahrens #include <stdlib.h> 70789Sahrens #include <strings.h> 71789Sahrens #include <unistd.h> 72789Sahrens #include <zone.h> 73789Sahrens #include <sys/mntent.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 855331Samw static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *); 865331Samw zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **, 875331Samw zfs_share_proto_t); 885331Samw 895331Samw /* 905331Samw * The share protocols table must be in the same order as the zfs_share_prot_t 915331Samw * enum in libzfs_impl.h 925331Samw */ 935331Samw typedef struct { 945331Samw zfs_prop_t p_prop; 955331Samw char *p_name; 965331Samw int p_share_err; 975331Samw int p_unshare_err; 985331Samw } proto_table_t; 995331Samw 1005331Samw proto_table_t proto_table[PROTO_END] = { 1015331Samw {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED}, 1025331Samw {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED}, 1035331Samw }; 1045331Samw 1055331Samw zfs_share_proto_t nfs_only[] = { 1065331Samw PROTO_NFS, 1075331Samw PROTO_END 1085331Samw }; 1095331Samw 1105331Samw zfs_share_proto_t smb_only[] = { 1115331Samw PROTO_SMB, 1125331Samw PROTO_END 1135331Samw }; 1145331Samw zfs_share_proto_t share_all_proto[] = { 1155331Samw PROTO_NFS, 1165331Samw PROTO_SMB, 1175331Samw PROTO_END 1185331Samw }; 1195331Samw 120789Sahrens /* 1215331Samw * Search the sharetab for the given mountpoint and protocol, returning 1225331Samw * a zfs_share_type_t value. 123789Sahrens */ 1245331Samw static zfs_share_type_t 1255331Samw is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto) 126789Sahrens { 127789Sahrens char buf[MAXPATHLEN], *tab; 1285331Samw char *ptr; 129789Sahrens 1302082Seschrock if (hdl->libzfs_sharetab == NULL) 1315331Samw return (SHARED_NOT_SHARED); 132789Sahrens 1332082Seschrock (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET); 134789Sahrens 1352082Seschrock while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) { 136789Sahrens 137789Sahrens /* the mountpoint is the first entry on each line */ 1385331Samw if ((tab = strchr(buf, '\t')) == NULL) 1395331Samw continue; 1405331Samw 1415331Samw *tab = '\0'; 1425331Samw if (strcmp(buf, mountpoint) == 0) { 1435331Samw /* 1445331Samw * the protocol field is the third field 1455331Samw * skip over second field 1465331Samw */ 1475331Samw ptr = ++tab; 1485331Samw if ((tab = strchr(ptr, '\t')) == NULL) 1495331Samw continue; 1505331Samw ptr = ++tab; 1515331Samw if ((tab = strchr(ptr, '\t')) == NULL) 1525331Samw continue; 153789Sahrens *tab = '\0'; 1545331Samw if (strcmp(ptr, 1555331Samw proto_table[proto].p_name) == 0) { 1565331Samw switch (proto) { 1575331Samw case PROTO_NFS: 1585331Samw return (SHARED_NFS); 1595331Samw case PROTO_SMB: 1605331Samw return (SHARED_SMB); 1615331Samw default: 1625331Samw return (0); 1635331Samw } 1645331Samw } 165789Sahrens } 166789Sahrens } 167789Sahrens 1685331Samw return (SHARED_NOT_SHARED); 169789Sahrens } 170789Sahrens 171789Sahrens /* 1722082Seschrock * Returns true if the specified directory is empty. If we can't open the 1732082Seschrock * directory at all, return true so that the mount can fail with a more 174789Sahrens * informative error message. 175789Sahrens */ 1762082Seschrock static boolean_t 177789Sahrens dir_is_empty(const char *dirname) 178789Sahrens { 179789Sahrens DIR *dirp; 180789Sahrens struct dirent64 *dp; 181789Sahrens 182789Sahrens if ((dirp = opendir(dirname)) == NULL) 1832082Seschrock return (B_TRUE); 184789Sahrens 185789Sahrens while ((dp = readdir64(dirp)) != NULL) { 186789Sahrens 187789Sahrens if (strcmp(dp->d_name, ".") == 0 || 188789Sahrens strcmp(dp->d_name, "..") == 0) 189789Sahrens continue; 190789Sahrens 191789Sahrens (void) closedir(dirp); 1922082Seschrock return (B_FALSE); 193789Sahrens } 194789Sahrens 195789Sahrens (void) closedir(dirp); 1962082Seschrock return (B_TRUE); 197789Sahrens } 198789Sahrens 199789Sahrens /* 200789Sahrens * Checks to see if the mount is active. If the filesystem is mounted, we fill 201789Sahrens * in 'where' with the current mountpoint, and return 1. Otherwise, we return 202789Sahrens * 0. 203789Sahrens */ 2042082Seschrock boolean_t 2053444Sek110237 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 206789Sahrens { 2078228SEric.Taylor@Sun.COM struct mnttab entry; 208789Sahrens 2098228SEric.Taylor@Sun.COM if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0) 2102082Seschrock return (B_FALSE); 211789Sahrens 212789Sahrens if (where != NULL) 2133444Sek110237 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 214789Sahrens 2152082Seschrock return (B_TRUE); 216789Sahrens } 217789Sahrens 2183444Sek110237 boolean_t 2193444Sek110237 zfs_is_mounted(zfs_handle_t *zhp, char **where) 2203444Sek110237 { 2213444Sek110237 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 2223444Sek110237 } 2233444Sek110237 224789Sahrens /* 2252676Seschrock * Returns true if the given dataset is mountable, false otherwise. Returns the 2262676Seschrock * mountpoint in 'buf'. 2272676Seschrock */ 2282676Seschrock static boolean_t 2292676Seschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 2305094Slling zprop_source_t *source) 2312676Seschrock { 2322676Seschrock char sourceloc[ZFS_MAXNAMELEN]; 2335094Slling zprop_source_t sourcetype; 2342676Seschrock 2352676Seschrock if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 2362676Seschrock return (B_FALSE); 2372676Seschrock 2382676Seschrock verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 2392676Seschrock &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 2402676Seschrock 2412676Seschrock if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 2422676Seschrock strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 2432676Seschrock return (B_FALSE); 2442676Seschrock 2456168Shs24103 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF) 2462676Seschrock return (B_FALSE); 2472676Seschrock 2482676Seschrock if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2492676Seschrock getzoneid() == GLOBAL_ZONEID) 2502676Seschrock return (B_FALSE); 2512676Seschrock 2522676Seschrock if (source) 2532676Seschrock *source = sourcetype; 2542676Seschrock 2552676Seschrock return (B_TRUE); 2562676Seschrock } 2572676Seschrock 2582676Seschrock /* 259789Sahrens * Mount the given filesystem. 260789Sahrens */ 261789Sahrens int 262789Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 263789Sahrens { 264789Sahrens struct stat buf; 265789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 266789Sahrens char mntopts[MNT_LINE_MAX]; 2672082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 268789Sahrens 269789Sahrens if (options == NULL) 270789Sahrens mntopts[0] = '\0'; 271789Sahrens else 272789Sahrens (void) strlcpy(mntopts, options, sizeof (mntopts)); 273789Sahrens 2742676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 2752082Seschrock return (0); 276789Sahrens 277789Sahrens /* Create the directory if it doesn't already exist */ 278789Sahrens if (lstat(mountpoint, &buf) != 0) { 279789Sahrens if (mkdirp(mountpoint, 0755) != 0) { 2802082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2812082Seschrock "failed to create mountpoint")); 2823237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 2832082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 2842082Seschrock mountpoint)); 285789Sahrens } 286789Sahrens } 287789Sahrens 288789Sahrens /* 289789Sahrens * Determine if the mountpoint is empty. If so, refuse to perform the 290789Sahrens * mount. We don't perform this check if MS_OVERLAY is specified, which 291789Sahrens * would defeat the point. We also avoid this check if 'remount' is 292789Sahrens * specified. 293789Sahrens */ 294789Sahrens if ((flags & MS_OVERLAY) == 0 && 295789Sahrens strstr(mntopts, MNTOPT_REMOUNT) == NULL && 296789Sahrens !dir_is_empty(mountpoint)) { 2972082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2982082Seschrock "directory is not empty")); 2993237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 3002082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 301789Sahrens } 302789Sahrens 303789Sahrens /* perform the mount */ 304789Sahrens if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 305789Sahrens MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 306789Sahrens /* 307789Sahrens * Generic errors are nasty, but there are just way too many 308789Sahrens * from mount(), and they're well-understood. We pick a few 309789Sahrens * common ones to improve upon. 310789Sahrens */ 3114302Sdougm if (errno == EBUSY) { 3122082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3132082Seschrock "mountpoint or dataset is busy")); 3144543Smarks } else if (errno == EPERM) { 3154543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3164543Smarks "Insufficient privileges")); 31711814SChris.Kirby@sun.com } else if (errno == ENOTSUP) { 31811814SChris.Kirby@sun.com char buf[256]; 31911814SChris.Kirby@sun.com 32011814SChris.Kirby@sun.com (void) snprintf(buf, sizeof (buf), 32111814SChris.Kirby@sun.com dgettext(TEXT_DOMAIN, "Mismatched versions: File " 32211814SChris.Kirby@sun.com "system is version %llu on-disk format, which is " 32311814SChris.Kirby@sun.com "incompatible with this software version %lld!"), 32411814SChris.Kirby@sun.com (u_longlong_t)zfs_prop_get_int(zhp, 32511814SChris.Kirby@sun.com ZFS_PROP_VERSION), ZPL_VERSION); 32611814SChris.Kirby@sun.com zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf)); 3274302Sdougm } else { 3282082Seschrock zfs_error_aux(hdl, strerror(errno)); 3294302Sdougm } 3303237Slling return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 3312082Seschrock dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 3322082Seschrock zhp->zfs_name)); 333789Sahrens } 334789Sahrens 3358228SEric.Taylor@Sun.COM /* add the mounted entry into our cache */ 3368228SEric.Taylor@Sun.COM libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, 3378228SEric.Taylor@Sun.COM mntopts); 338789Sahrens return (0); 339789Sahrens } 340789Sahrens 341789Sahrens /* 3422474Seschrock * Unmount a single filesystem. 3432474Seschrock */ 3442474Seschrock static int 3452474Seschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 3462474Seschrock { 3472474Seschrock if (umount2(mountpoint, flags) != 0) { 3482474Seschrock zfs_error_aux(hdl, strerror(errno)); 3493237Slling return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 3502474Seschrock dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 3512474Seschrock mountpoint)); 3522474Seschrock } 3532474Seschrock 3542474Seschrock return (0); 3552474Seschrock } 3562474Seschrock 3572474Seschrock /* 358789Sahrens * Unmount the given filesystem. 359789Sahrens */ 360789Sahrens int 361789Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 362789Sahrens { 3638228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 3648228SEric.Taylor@Sun.COM struct mnttab entry; 3654180Sdougm char *mntpt = NULL; 366789Sahrens 3678228SEric.Taylor@Sun.COM /* check to see if we need to unmount the filesystem */ 368789Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 3698228SEric.Taylor@Sun.COM libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) { 3704180Sdougm /* 3714180Sdougm * mountpoint may have come from a call to 3724180Sdougm * getmnt/getmntany if it isn't NULL. If it is NULL, 3738228SEric.Taylor@Sun.COM * we know it comes from libzfs_mnttab_find which can 3748228SEric.Taylor@Sun.COM * then get freed later. We strdup it to play it safe. 3754180Sdougm */ 376789Sahrens if (mountpoint == NULL) 3778228SEric.Taylor@Sun.COM mntpt = zfs_strdup(hdl, entry.mnt_mountp); 3784180Sdougm else 3798228SEric.Taylor@Sun.COM mntpt = zfs_strdup(hdl, mountpoint); 380789Sahrens 381789Sahrens /* 3822474Seschrock * Unshare and unmount the filesystem 383789Sahrens */ 3845331Samw if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) 3854331Sth199096 return (-1); 3864331Sth199096 3878228SEric.Taylor@Sun.COM if (unmount_one(hdl, mntpt, flags) != 0) { 3884180Sdougm free(mntpt); 3895331Samw (void) zfs_shareall(zhp); 390789Sahrens return (-1); 3914180Sdougm } 3928228SEric.Taylor@Sun.COM libzfs_mnttab_remove(hdl, zhp->zfs_name); 3934180Sdougm free(mntpt); 394789Sahrens } 395789Sahrens 396789Sahrens return (0); 397789Sahrens } 398789Sahrens 399789Sahrens /* 400789Sahrens * Unmount this filesystem and any children inheriting the mountpoint property. 401789Sahrens * To do this, just act like we're changing the mountpoint property, but don't 402789Sahrens * remount the filesystems afterwards. 403789Sahrens */ 404789Sahrens int 405789Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags) 406789Sahrens { 407789Sahrens prop_changelist_t *clp; 408789Sahrens int ret; 409789Sahrens 4107366STim.Haley@Sun.COM clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags); 411789Sahrens if (clp == NULL) 412789Sahrens return (-1); 413789Sahrens 414789Sahrens ret = changelist_prefix(clp); 415789Sahrens changelist_free(clp); 416789Sahrens 417789Sahrens return (ret); 418789Sahrens } 419789Sahrens 4203126Sahl boolean_t 4213126Sahl zfs_is_shared(zfs_handle_t *zhp) 4223126Sahl { 4235331Samw zfs_share_type_t rc = 0; 4245331Samw zfs_share_proto_t *curr_proto; 4255331Samw 4263126Sahl if (ZFS_IS_VOLUME(zhp)) 427*11876SJames.Dunham@Sun.COM return (B_FALSE); 4283126Sahl 4295331Samw for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 4305331Samw curr_proto++) 4315331Samw rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto); 4325331Samw 4335331Samw return (rc ? B_TRUE : B_FALSE); 4343126Sahl } 4353126Sahl 4363126Sahl int 4373126Sahl zfs_share(zfs_handle_t *zhp) 4383126Sahl { 4393126Sahl if (ZFS_IS_VOLUME(zhp)) 440*11876SJames.Dunham@Sun.COM return (0); 4413126Sahl 4425331Samw return (zfs_share_proto(zhp, share_all_proto)); 4433126Sahl } 4443126Sahl 4453126Sahl int 4463126Sahl zfs_unshare(zfs_handle_t *zhp) 4473126Sahl { 4483126Sahl if (ZFS_IS_VOLUME(zhp)) 449*11876SJames.Dunham@Sun.COM return (0); 4503126Sahl 4515331Samw return (zfs_unshareall(zhp)); 4523126Sahl } 4533126Sahl 454789Sahrens /* 455789Sahrens * Check to see if the filesystem is currently shared. 456789Sahrens */ 4575331Samw zfs_share_type_t 4585331Samw zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto) 459789Sahrens { 460789Sahrens char *mountpoint; 4615331Samw zfs_share_type_t rc; 462789Sahrens 463789Sahrens if (!zfs_is_mounted(zhp, &mountpoint)) 4645331Samw return (SHARED_NOT_SHARED); 465789Sahrens 4665331Samw if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) { 467789Sahrens if (where != NULL) 468789Sahrens *where = mountpoint; 469789Sahrens else 470789Sahrens free(mountpoint); 4715331Samw return (rc); 472789Sahrens } else { 473789Sahrens free(mountpoint); 4745331Samw return (SHARED_NOT_SHARED); 475789Sahrens } 476789Sahrens } 477789Sahrens 4785331Samw boolean_t 4795331Samw zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 4805331Samw { 4815331Samw return (zfs_is_shared_proto(zhp, where, 4825331Samw PROTO_NFS) != SHARED_NOT_SHARED); 4835331Samw } 4845331Samw 4855331Samw boolean_t 4865331Samw zfs_is_shared_smb(zfs_handle_t *zhp, char **where) 4875331Samw { 4885331Samw return (zfs_is_shared_proto(zhp, where, 4895331Samw PROTO_SMB) != SHARED_NOT_SHARED); 4905331Samw } 4915331Samw 492789Sahrens /* 4934180Sdougm * Make sure things will work if libshare isn't installed by using 4944180Sdougm * wrapper functions that check to see that the pointers to functions 4954180Sdougm * initialized in _zfs_init_libshare() are actually present. 4964180Sdougm */ 4974180Sdougm 4984180Sdougm static sa_handle_t (*_sa_init)(int); 4994180Sdougm static void (*_sa_fini)(sa_handle_t); 5004180Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *); 5014180Sdougm static int (*_sa_enable_share)(sa_share_t, char *); 5024180Sdougm static int (*_sa_disable_share)(sa_share_t, char *); 5034180Sdougm static char *(*_sa_errorstr)(int); 5044180Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *); 5055951Sdougm static boolean_t (*_sa_needs_refresh)(sa_handle_t *); 5065951Sdougm static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t); 5075951Sdougm static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t, 5085951Sdougm char *, char *, zprop_source_t, char *, char *, char *); 5095951Sdougm static void (*_sa_update_sharetab_ts)(sa_handle_t); 5104180Sdougm 5114180Sdougm /* 5124180Sdougm * _zfs_init_libshare() 5134180Sdougm * 5144180Sdougm * Find the libshare.so.1 entry points that we use here and save the 5154180Sdougm * values to be used later. This is triggered by the runtime loader. 5164180Sdougm * Make sure the correct ISA version is loaded. 5174180Sdougm */ 5185951Sdougm 5194180Sdougm #pragma init(_zfs_init_libshare) 5204180Sdougm static void 5214180Sdougm _zfs_init_libshare(void) 5224180Sdougm { 5234180Sdougm void *libshare; 5244180Sdougm char path[MAXPATHLEN]; 5254180Sdougm char isa[MAXISALEN]; 5264180Sdougm 5274180Sdougm #if defined(_LP64) 5284180Sdougm if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 5294302Sdougm isa[0] = '\0'; 5304180Sdougm #else 5314180Sdougm isa[0] = '\0'; 5324180Sdougm #endif 5334180Sdougm (void) snprintf(path, MAXPATHLEN, 5344302Sdougm "/usr/lib/%s/libshare.so.1", isa); 5354180Sdougm 5364180Sdougm if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) { 5374302Sdougm _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init"); 5384302Sdougm _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini"); 5394302Sdougm _sa_find_share = (sa_share_t (*)(sa_handle_t, char *)) 5404302Sdougm dlsym(libshare, "sa_find_share"); 5414302Sdougm _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 5424302Sdougm "sa_enable_share"); 5434302Sdougm _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 5444302Sdougm "sa_disable_share"); 5454302Sdougm _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr"); 5464302Sdougm _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *)) 5474302Sdougm dlsym(libshare, "sa_parse_legacy_options"); 5485951Sdougm _sa_needs_refresh = (boolean_t (*)(sa_handle_t *)) 5495951Sdougm dlsym(libshare, "sa_needs_refresh"); 5505951Sdougm _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t)) 5515951Sdougm dlsym(libshare, "sa_get_zfs_handle"); 5525951Sdougm _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t, 5535951Sdougm sa_share_t, char *, char *, zprop_source_t, char *, 5545951Sdougm char *, char *))dlsym(libshare, "sa_zfs_process_share"); 5555951Sdougm _sa_update_sharetab_ts = (void (*)(sa_handle_t)) 5565951Sdougm dlsym(libshare, "sa_update_sharetab_ts"); 5574327Sdougm if (_sa_init == NULL || _sa_fini == NULL || 5584327Sdougm _sa_find_share == NULL || _sa_enable_share == NULL || 5594327Sdougm _sa_disable_share == NULL || _sa_errorstr == NULL || 5605951Sdougm _sa_parse_legacy_options == NULL || 5615951Sdougm _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL || 5625951Sdougm _sa_zfs_process_share == NULL || 5635951Sdougm _sa_update_sharetab_ts == NULL) { 5644327Sdougm _sa_init = NULL; 5654327Sdougm _sa_fini = NULL; 5664327Sdougm _sa_disable_share = NULL; 5674327Sdougm _sa_enable_share = NULL; 5684327Sdougm _sa_errorstr = NULL; 5694327Sdougm _sa_parse_legacy_options = NULL; 5704327Sdougm (void) dlclose(libshare); 5715951Sdougm _sa_needs_refresh = NULL; 5725951Sdougm _sa_get_zfs_handle = NULL; 5735951Sdougm _sa_zfs_process_share = NULL; 5745951Sdougm _sa_update_sharetab_ts = NULL; 5754327Sdougm } 5764180Sdougm } 5774180Sdougm } 5784180Sdougm 5794180Sdougm /* 5804180Sdougm * zfs_init_libshare(zhandle, service) 5814180Sdougm * 5824180Sdougm * Initialize the libshare API if it hasn't already been initialized. 5834180Sdougm * In all cases it returns 0 if it succeeded and an error if not. The 5844180Sdougm * service value is which part(s) of the API to initialize and is a 5854180Sdougm * direct map to the libshare sa_init(service) interface. 5864180Sdougm */ 5874180Sdougm int 5884180Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service) 5894180Sdougm { 5904180Sdougm int ret = SA_OK; 5914180Sdougm 5924302Sdougm if (_sa_init == NULL) 5934302Sdougm ret = SA_CONFIG_ERR; 5944302Sdougm 5955951Sdougm if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) { 5965951Sdougm /* 5975951Sdougm * We had a cache miss. Most likely it is a new ZFS 5985951Sdougm * dataset that was just created. We want to make sure 5995951Sdougm * so check timestamps to see if a different process 6005951Sdougm * has updated any of the configuration. If there was 6015951Sdougm * some non-ZFS change, we need to re-initialize the 6025951Sdougm * internal cache. 6035951Sdougm */ 6045951Sdougm zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS; 6055951Sdougm if (_sa_needs_refresh != NULL && 6065951Sdougm _sa_needs_refresh(zhandle->libzfs_sharehdl)) { 6075951Sdougm zfs_uninit_libshare(zhandle); 6085951Sdougm zhandle->libzfs_sharehdl = _sa_init(service); 6095951Sdougm } 6105951Sdougm } 6115951Sdougm 6124302Sdougm if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL) 6134302Sdougm zhandle->libzfs_sharehdl = _sa_init(service); 6144302Sdougm 6154302Sdougm if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL) 6164302Sdougm ret = SA_NO_MEMORY; 6174302Sdougm 6184180Sdougm return (ret); 6194180Sdougm } 6204180Sdougm 6214180Sdougm /* 6224180Sdougm * zfs_uninit_libshare(zhandle) 6234180Sdougm * 6244180Sdougm * Uninitialize the libshare API if it hasn't already been 6254180Sdougm * uninitialized. It is OK to call multiple times. 6264180Sdougm */ 6274180Sdougm void 6284180Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle) 6294180Sdougm { 6304180Sdougm if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) { 6314302Sdougm if (_sa_fini != NULL) 6324302Sdougm _sa_fini(zhandle->libzfs_sharehdl); 6334302Sdougm zhandle->libzfs_sharehdl = NULL; 6344180Sdougm } 6354180Sdougm } 6364180Sdougm 6374180Sdougm /* 6384180Sdougm * zfs_parse_options(options, proto) 6394180Sdougm * 6404180Sdougm * Call the legacy parse interface to get the protocol specific 6414180Sdougm * options using the NULL arg to indicate that this is a "parse" only. 6424180Sdougm */ 6434180Sdougm int 6445331Samw zfs_parse_options(char *options, zfs_share_proto_t proto) 6454180Sdougm { 6465367Sahrens if (_sa_parse_legacy_options != NULL) { 6475367Sahrens return (_sa_parse_legacy_options(NULL, options, 6485367Sahrens proto_table[proto].p_name)); 6495367Sahrens } 6505367Sahrens return (SA_CONFIG_ERR); 6514180Sdougm } 6524180Sdougm 6534180Sdougm /* 6544180Sdougm * zfs_sa_find_share(handle, path) 6554180Sdougm * 6564180Sdougm * wrapper around sa_find_share to find a share path in the 6574180Sdougm * configuration. 6584180Sdougm */ 6594180Sdougm static sa_share_t 6604180Sdougm zfs_sa_find_share(sa_handle_t handle, char *path) 6614180Sdougm { 6624180Sdougm if (_sa_find_share != NULL) 6634302Sdougm return (_sa_find_share(handle, path)); 6644180Sdougm return (NULL); 6654180Sdougm } 6664180Sdougm 6674180Sdougm /* 6684180Sdougm * zfs_sa_enable_share(share, proto) 6694180Sdougm * 6704180Sdougm * Wrapper for sa_enable_share which enables a share for a specified 6714180Sdougm * protocol. 6724180Sdougm */ 6734180Sdougm static int 6744180Sdougm zfs_sa_enable_share(sa_share_t share, char *proto) 6754180Sdougm { 6764180Sdougm if (_sa_enable_share != NULL) 6774302Sdougm return (_sa_enable_share(share, proto)); 6784180Sdougm return (SA_CONFIG_ERR); 6794180Sdougm } 6804180Sdougm 6814180Sdougm /* 6824180Sdougm * zfs_sa_disable_share(share, proto) 6834180Sdougm * 6844180Sdougm * Wrapper for sa_enable_share which disables a share for a specified 6854180Sdougm * protocol. 6864180Sdougm */ 6874180Sdougm static int 6884180Sdougm zfs_sa_disable_share(sa_share_t share, char *proto) 6894180Sdougm { 6904180Sdougm if (_sa_disable_share != NULL) 6914302Sdougm return (_sa_disable_share(share, proto)); 6924180Sdougm return (SA_CONFIG_ERR); 6934180Sdougm } 6944180Sdougm 6954180Sdougm /* 6965331Samw * Share the given filesystem according to the options in the specified 6975331Samw * protocol specific properties (sharenfs, sharesmb). We rely 6984180Sdougm * on "libshare" to the dirty work for us. 699789Sahrens */ 7005331Samw static int 7015331Samw zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 702789Sahrens { 703789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 704789Sahrens char shareopts[ZFS_MAXPROPLEN]; 7055951Sdougm char sourcestr[ZFS_MAXPROPLEN]; 7062082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 7074180Sdougm sa_share_t share; 7085331Samw zfs_share_proto_t *curr_proto; 7095951Sdougm zprop_source_t sourcetype; 7104180Sdougm int ret; 711789Sahrens 7122676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 713789Sahrens return (0); 714789Sahrens 7154180Sdougm if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 7164302Sdougm (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 7174302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s': %s"), 7185951Sdougm zfs_get_name(zhp), _sa_errorstr != NULL ? 7195951Sdougm _sa_errorstr(ret) : ""); 7204302Sdougm return (-1); 7214180Sdougm } 7225331Samw 7235331Samw for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 7245331Samw /* 7255331Samw * Return success if there are no share options. 7265331Samw */ 7275331Samw if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop, 7285951Sdougm shareopts, sizeof (shareopts), &sourcetype, sourcestr, 7295951Sdougm ZFS_MAXPROPLEN, B_FALSE) != 0 || 7305951Sdougm strcmp(shareopts, "off") == 0) 7315331Samw continue; 7325331Samw 7335331Samw /* 7345331Samw * If the 'zoned' property is set, then zfs_is_mountable() 7355331Samw * will have already bailed out if we are in the global zone. 7365331Samw * But local zones cannot be NFS servers, so we ignore it for 7375331Samw * local zones as well. 7385331Samw */ 7395331Samw if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 7405331Samw continue; 7415331Samw 7425331Samw share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint); 7435951Sdougm if (share == NULL) { 7445951Sdougm /* 7455951Sdougm * This may be a new file system that was just 7465951Sdougm * created so isn't in the internal cache 7475951Sdougm * (second time through). Rather than 7485951Sdougm * reloading the entire configuration, we can 7495951Sdougm * assume ZFS has done the checking and it is 7505951Sdougm * safe to add this to the internal 7515951Sdougm * configuration. 7525951Sdougm */ 7535951Sdougm if (_sa_zfs_process_share(hdl->libzfs_sharehdl, 7545951Sdougm NULL, NULL, mountpoint, 7555951Sdougm proto_table[*curr_proto].p_name, sourcetype, 7565951Sdougm shareopts, sourcestr, zhp->zfs_name) != SA_OK) { 7575951Sdougm (void) zfs_error_fmt(hdl, 7585951Sdougm proto_table[*curr_proto].p_share_err, 7595951Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 7605951Sdougm zfs_get_name(zhp)); 7615951Sdougm return (-1); 7625951Sdougm } 7635951Sdougm hdl->libzfs_shareflags |= ZFSSHARE_MISS; 7645951Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, 7655951Sdougm mountpoint); 7665951Sdougm } 7675331Samw if (share != NULL) { 7685331Samw int err; 7695331Samw err = zfs_sa_enable_share(share, 7705331Samw proto_table[*curr_proto].p_name); 7715331Samw if (err != SA_OK) { 7725331Samw (void) zfs_error_fmt(hdl, 7735331Samw proto_table[*curr_proto].p_share_err, 7745331Samw dgettext(TEXT_DOMAIN, "cannot share '%s'"), 7755331Samw zfs_get_name(zhp)); 7765331Samw return (-1); 7775331Samw } 7785331Samw } else { 7795331Samw (void) zfs_error_fmt(hdl, 7805331Samw proto_table[*curr_proto].p_share_err, 7814302Sdougm dgettext(TEXT_DOMAIN, "cannot share '%s'"), 7824302Sdougm zfs_get_name(zhp)); 7834302Sdougm return (-1); 7844302Sdougm } 7855331Samw 786789Sahrens } 7875331Samw return (0); 7885331Samw } 789789Sahrens 7905331Samw 7915331Samw int 7925331Samw zfs_share_nfs(zfs_handle_t *zhp) 7935331Samw { 7945331Samw return (zfs_share_proto(zhp, nfs_only)); 7955331Samw } 7965331Samw 7975331Samw int 7985331Samw zfs_share_smb(zfs_handle_t *zhp) 7995331Samw { 8005331Samw return (zfs_share_proto(zhp, smb_only)); 8015331Samw } 8025331Samw 8035331Samw int 8045331Samw zfs_shareall(zfs_handle_t *zhp) 8055331Samw { 8065331Samw return (zfs_share_proto(zhp, share_all_proto)); 807789Sahrens } 808789Sahrens 809789Sahrens /* 8102474Seschrock * Unshare a filesystem by mountpoint. 8112474Seschrock */ 8122474Seschrock static int 8135331Samw unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, 8145331Samw zfs_share_proto_t proto) 8152474Seschrock { 8164180Sdougm sa_share_t share; 8174180Sdougm int err; 8184180Sdougm char *mntpt; 8192474Seschrock /* 8204180Sdougm * Mountpoint could get trashed if libshare calls getmntany 8218228SEric.Taylor@Sun.COM * which it does during API initialization, so strdup the 8224180Sdougm * value. 8232474Seschrock */ 8244180Sdougm mntpt = zfs_strdup(hdl, mountpoint); 8252474Seschrock 8264180Sdougm /* make sure libshare initialized */ 8274180Sdougm if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 8284180Sdougm free(mntpt); /* don't need the copy anymore */ 8294180Sdougm return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 8304302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 8314302Sdougm name, _sa_errorstr(err))); 8322474Seschrock } 8332474Seschrock 8344180Sdougm share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt); 8354180Sdougm free(mntpt); /* don't need the copy anymore */ 8362474Seschrock 8374180Sdougm if (share != NULL) { 8385331Samw err = zfs_sa_disable_share(share, proto_table[proto].p_name); 8394180Sdougm if (err != SA_OK) { 8404302Sdougm return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 8414302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 8424302Sdougm name, _sa_errorstr(err))); 8434180Sdougm } 8444180Sdougm } else { 8454180Sdougm return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 8464302Sdougm dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"), 8474302Sdougm name)); 8484180Sdougm } 8492474Seschrock return (0); 8502474Seschrock } 8512474Seschrock 8522474Seschrock /* 853789Sahrens * Unshare the given filesystem. 854789Sahrens */ 855789Sahrens int 8565331Samw zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, 8575331Samw zfs_share_proto_t *proto) 858789Sahrens { 8598228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 8608228SEric.Taylor@Sun.COM struct mnttab entry; 8614180Sdougm char *mntpt = NULL; 862789Sahrens 863789Sahrens /* check to see if need to unmount the filesystem */ 8642082Seschrock rewind(zhp->zfs_hdl->libzfs_mnttab); 8654302Sdougm if (mountpoint != NULL) 8668228SEric.Taylor@Sun.COM mountpoint = mntpt = zfs_strdup(hdl, mountpoint); 8674302Sdougm 868789Sahrens if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 8698228SEric.Taylor@Sun.COM libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) { 8705331Samw zfs_share_proto_t *curr_proto; 871789Sahrens 872789Sahrens if (mountpoint == NULL) 8735331Samw mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 8745331Samw 8755331Samw for (curr_proto = proto; *curr_proto != PROTO_END; 8765331Samw curr_proto++) { 877789Sahrens 8788228SEric.Taylor@Sun.COM if (is_shared(hdl, mntpt, *curr_proto) && 8798228SEric.Taylor@Sun.COM unshare_one(hdl, zhp->zfs_name, 8805331Samw mntpt, *curr_proto) != 0) { 8815331Samw if (mntpt != NULL) 8825331Samw free(mntpt); 8835331Samw return (-1); 8845331Samw } 8854180Sdougm } 886789Sahrens } 8874180Sdougm if (mntpt != NULL) 8884302Sdougm free(mntpt); 889789Sahrens 890789Sahrens return (0); 891789Sahrens } 892789Sahrens 8935331Samw int 8945331Samw zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 8955331Samw { 8965331Samw return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 8975331Samw } 8985331Samw 8995331Samw int 9005331Samw zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint) 9015331Samw { 9025331Samw return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 9035331Samw } 9045331Samw 905789Sahrens /* 9065331Samw * Same as zfs_unmountall(), but for NFS and SMB unshares. 907789Sahrens */ 908789Sahrens int 9095331Samw zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 910789Sahrens { 911789Sahrens prop_changelist_t *clp; 912789Sahrens int ret; 913789Sahrens 9147366STim.Haley@Sun.COM clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0); 915789Sahrens if (clp == NULL) 916789Sahrens return (-1); 917789Sahrens 9185331Samw ret = changelist_unshare(clp, proto); 919789Sahrens changelist_free(clp); 920789Sahrens 921789Sahrens return (ret); 922789Sahrens } 923789Sahrens 9245331Samw int 9255331Samw zfs_unshareall_nfs(zfs_handle_t *zhp) 9265331Samw { 9275331Samw return (zfs_unshareall_proto(zhp, nfs_only)); 9285331Samw } 9295331Samw 9305331Samw int 9315331Samw zfs_unshareall_smb(zfs_handle_t *zhp) 9325331Samw { 9335331Samw return (zfs_unshareall_proto(zhp, smb_only)); 9345331Samw } 9355331Samw 9365331Samw int 9375331Samw zfs_unshareall(zfs_handle_t *zhp) 9385331Samw { 9395331Samw return (zfs_unshareall_proto(zhp, share_all_proto)); 9405331Samw } 9415331Samw 9425331Samw int 9435331Samw zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint) 9445331Samw { 9455331Samw return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 9465331Samw } 9475331Samw 948789Sahrens /* 949789Sahrens * Remove the mountpoint associated with the current dataset, if necessary. 950789Sahrens * We only remove the underlying directory if: 951789Sahrens * 952789Sahrens * - The mountpoint is not 'none' or 'legacy' 953789Sahrens * - The mountpoint is non-empty 954789Sahrens * - The mountpoint is the default or inherited 955789Sahrens * - The 'zoned' property is set, or we're in a local zone 956789Sahrens * 957789Sahrens * Any other directories we leave alone. 958789Sahrens */ 959789Sahrens void 960789Sahrens remove_mountpoint(zfs_handle_t *zhp) 961789Sahrens { 962789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 9635094Slling zprop_source_t source; 964789Sahrens 9652676Seschrock if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 9662676Seschrock &source)) 967789Sahrens return; 968789Sahrens 9695094Slling if (source == ZPROP_SRC_DEFAULT || 9705094Slling source == ZPROP_SRC_INHERITED) { 971789Sahrens /* 972789Sahrens * Try to remove the directory, silently ignoring any errors. 973789Sahrens * The filesystem may have since been removed or moved around, 9742676Seschrock * and this error isn't really useful to the administrator in 9752676Seschrock * any way. 976789Sahrens */ 977789Sahrens (void) rmdir(mountpoint); 978789Sahrens } 979789Sahrens } 9802474Seschrock 9812474Seschrock typedef struct mount_cbdata { 9822474Seschrock zfs_handle_t **cb_datasets; 9832474Seschrock int cb_used; 9842474Seschrock int cb_alloc; 9852474Seschrock } mount_cbdata_t; 9862474Seschrock 9872474Seschrock static int 9882474Seschrock mount_cb(zfs_handle_t *zhp, void *data) 9892474Seschrock { 9902474Seschrock mount_cbdata_t *cbp = data; 9912474Seschrock 9923126Sahl if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) { 9932474Seschrock zfs_close(zhp); 9942474Seschrock return (0); 9952474Seschrock } 9962474Seschrock 9976168Shs24103 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) { 9986168Shs24103 zfs_close(zhp); 9996168Shs24103 return (0); 10006168Shs24103 } 10016168Shs24103 10022474Seschrock if (cbp->cb_alloc == cbp->cb_used) { 10032676Seschrock void *ptr; 10042474Seschrock 10052676Seschrock if ((ptr = zfs_realloc(zhp->zfs_hdl, 10062676Seschrock cbp->cb_datasets, cbp->cb_alloc * sizeof (void *), 10072676Seschrock cbp->cb_alloc * 2 * sizeof (void *))) == NULL) 10082474Seschrock return (-1); 10092676Seschrock cbp->cb_datasets = ptr; 10102474Seschrock 10112676Seschrock cbp->cb_alloc *= 2; 10122474Seschrock } 10132474Seschrock 10142474Seschrock cbp->cb_datasets[cbp->cb_used++] = zhp; 10153126Sahl 10166027Srm160521 return (zfs_iter_filesystems(zhp, mount_cb, cbp)); 10172474Seschrock } 10182474Seschrock 10192474Seschrock static int 10203126Sahl dataset_cmp(const void *a, const void *b) 10212474Seschrock { 10222474Seschrock zfs_handle_t **za = (zfs_handle_t **)a; 10232474Seschrock zfs_handle_t **zb = (zfs_handle_t **)b; 10242474Seschrock char mounta[MAXPATHLEN]; 10252474Seschrock char mountb[MAXPATHLEN]; 10263126Sahl boolean_t gota, gotb; 10272474Seschrock 10283126Sahl if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 10293126Sahl verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 10303126Sahl sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 10313126Sahl if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 10323126Sahl verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 10333126Sahl sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 10342474Seschrock 10353126Sahl if (gota && gotb) 10363126Sahl return (strcmp(mounta, mountb)); 10373126Sahl 10383126Sahl if (gota) 10393126Sahl return (-1); 10403126Sahl if (gotb) 10413126Sahl return (1); 10423126Sahl 10433126Sahl return (strcmp(zfs_get_name(a), zfs_get_name(b))); 10442474Seschrock } 10452474Seschrock 10463126Sahl /* 10473126Sahl * Mount and share all datasets within the given pool. This assumes that no 10483126Sahl * datasets within the pool are currently mounted. Because users can create 10493126Sahl * complicated nested hierarchies of mountpoints, we first gather all the 10503126Sahl * datasets and mountpoints within the pool, and sort them by mountpoint. Once 10513126Sahl * we have the list of all filesystems, we iterate over them in order and mount 10523126Sahl * and/or share each one. 10533126Sahl */ 10543126Sahl #pragma weak zpool_mount_datasets = zpool_enable_datasets 10552474Seschrock int 10563126Sahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 10572474Seschrock { 10582474Seschrock mount_cbdata_t cb = { 0 }; 10592474Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10602474Seschrock zfs_handle_t *zfsp; 10612474Seschrock int i, ret = -1; 10624180Sdougm int *good; 10632474Seschrock 10642474Seschrock /* 10656027Srm160521 * Gather all non-snap datasets within the pool. 10662474Seschrock */ 10672474Seschrock if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL) 10682474Seschrock return (-1); 10692474Seschrock cb.cb_alloc = 4; 10702474Seschrock 10715094Slling if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL) 10722474Seschrock goto out; 10732474Seschrock 10742474Seschrock cb.cb_datasets[0] = zfsp; 10752474Seschrock cb.cb_used = 1; 10762474Seschrock 10776027Srm160521 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0) 10782474Seschrock goto out; 10792474Seschrock 10802474Seschrock /* 10812474Seschrock * Sort the datasets by mountpoint. 10822474Seschrock */ 10833126Sahl qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp); 10842474Seschrock 10852474Seschrock /* 10864180Sdougm * And mount all the datasets, keeping track of which ones 10878536SDavid.Pacheco@Sun.COM * succeeded or failed. 10882474Seschrock */ 10898536SDavid.Pacheco@Sun.COM if ((good = zfs_alloc(zhp->zpool_hdl, 10908536SDavid.Pacheco@Sun.COM cb.cb_used * sizeof (int))) == NULL) 10918536SDavid.Pacheco@Sun.COM goto out; 10928536SDavid.Pacheco@Sun.COM 10932474Seschrock ret = 0; 10942474Seschrock for (i = 0; i < cb.cb_used; i++) { 10954302Sdougm if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0) 10964180Sdougm ret = -1; 10974302Sdougm else 10984180Sdougm good[i] = 1; 10994180Sdougm } 11005951Sdougm 11014180Sdougm /* 11024180Sdougm * Then share all the ones that need to be shared. This needs 11034180Sdougm * to be a separate pass in order to avoid excessive reloading 11044180Sdougm * of the configuration. Good should never be NULL since 11054180Sdougm * zfs_alloc is supposed to exit if memory isn't available. 11064180Sdougm */ 11074180Sdougm for (i = 0; i < cb.cb_used; i++) { 11084180Sdougm if (good[i] && zfs_share(cb.cb_datasets[i]) != 0) 11092474Seschrock ret = -1; 11102474Seschrock } 11112474Seschrock 11124180Sdougm free(good); 11134180Sdougm 11142474Seschrock out: 11152474Seschrock for (i = 0; i < cb.cb_used; i++) 11162474Seschrock zfs_close(cb.cb_datasets[i]); 11172474Seschrock free(cb.cb_datasets); 11182474Seschrock 11192474Seschrock return (ret); 11202474Seschrock } 11212474Seschrock 11222474Seschrock static int 11232474Seschrock mountpoint_compare(const void *a, const void *b) 11242474Seschrock { 11252474Seschrock const char *mounta = *((char **)a); 11262474Seschrock const char *mountb = *((char **)b); 11272474Seschrock 11282474Seschrock return (strcmp(mountb, mounta)); 11292474Seschrock } 11302474Seschrock 113110588SEric.Taylor@Sun.COM /* alias for 2002/240 */ 113210588SEric.Taylor@Sun.COM #pragma weak zpool_unmount_datasets = zpool_disable_datasets 11333126Sahl /* 11343126Sahl * Unshare and unmount all datasets within the given pool. We don't want to 11353126Sahl * rely on traversing the DSL to discover the filesystems within the pool, 11363126Sahl * because this may be expensive (if not all of them are mounted), and can fail 11373126Sahl * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 11383126Sahl * gather all the filesystems that are currently mounted. 11393126Sahl */ 11402474Seschrock int 11413126Sahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 11422474Seschrock { 11432474Seschrock int used, alloc; 11442474Seschrock struct mnttab entry; 11452474Seschrock size_t namelen; 11462474Seschrock char **mountpoints = NULL; 11472474Seschrock zfs_handle_t **datasets = NULL; 11482474Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 11492474Seschrock int i; 11502474Seschrock int ret = -1; 11512474Seschrock int flags = (force ? MS_FORCE : 0); 11522474Seschrock 11532474Seschrock namelen = strlen(zhp->zpool_name); 11542474Seschrock 11552474Seschrock rewind(hdl->libzfs_mnttab); 11562474Seschrock used = alloc = 0; 11572474Seschrock while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 11582474Seschrock /* 11592474Seschrock * Ignore non-ZFS entries. 11602474Seschrock */ 11612474Seschrock if (entry.mnt_fstype == NULL || 11622474Seschrock strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 11632474Seschrock continue; 11642474Seschrock 11652474Seschrock /* 11662474Seschrock * Ignore filesystems not within this pool. 11672474Seschrock */ 11682474Seschrock if (entry.mnt_mountp == NULL || 11692474Seschrock strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 11702474Seschrock (entry.mnt_special[namelen] != '/' && 11712474Seschrock entry.mnt_special[namelen] != '\0')) 11722474Seschrock continue; 11732474Seschrock 11742474Seschrock /* 11752474Seschrock * At this point we've found a filesystem within our pool. Add 11762474Seschrock * it to our growing list. 11772474Seschrock */ 11782474Seschrock if (used == alloc) { 11792474Seschrock if (alloc == 0) { 11802474Seschrock if ((mountpoints = zfs_alloc(hdl, 11812474Seschrock 8 * sizeof (void *))) == NULL) 11822474Seschrock goto out; 11832474Seschrock 11842474Seschrock if ((datasets = zfs_alloc(hdl, 11852474Seschrock 8 * sizeof (void *))) == NULL) 11862474Seschrock goto out; 11872474Seschrock 11882474Seschrock alloc = 8; 11892474Seschrock } else { 11902676Seschrock void *ptr; 11912474Seschrock 11922676Seschrock if ((ptr = zfs_realloc(hdl, mountpoints, 11932676Seschrock alloc * sizeof (void *), 11942474Seschrock alloc * 2 * sizeof (void *))) == NULL) 11952474Seschrock goto out; 11962676Seschrock mountpoints = ptr; 11972474Seschrock 11982676Seschrock if ((ptr = zfs_realloc(hdl, datasets, 11992676Seschrock alloc * sizeof (void *), 12002474Seschrock alloc * 2 * sizeof (void *))) == NULL) 12012474Seschrock goto out; 12022676Seschrock datasets = ptr; 12032474Seschrock 12042474Seschrock alloc *= 2; 12052474Seschrock } 12062474Seschrock } 12072474Seschrock 12082474Seschrock if ((mountpoints[used] = zfs_strdup(hdl, 12092474Seschrock entry.mnt_mountp)) == NULL) 12102474Seschrock goto out; 12112474Seschrock 12122474Seschrock /* 12132474Seschrock * This is allowed to fail, in case there is some I/O error. It 12142474Seschrock * is only used to determine if we need to remove the underlying 12152474Seschrock * mountpoint, so failure is not fatal. 12162474Seschrock */ 12172474Seschrock datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 12182474Seschrock 12192474Seschrock used++; 12202474Seschrock } 12212474Seschrock 12222474Seschrock /* 12232474Seschrock * At this point, we have the entire list of filesystems, so sort it by 12242474Seschrock * mountpoint. 12252474Seschrock */ 12262474Seschrock qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 12272474Seschrock 12282474Seschrock /* 12292474Seschrock * Walk through and first unshare everything. 12302474Seschrock */ 12312474Seschrock for (i = 0; i < used; i++) { 12325331Samw zfs_share_proto_t *curr_proto; 12335331Samw for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 12345331Samw curr_proto++) { 12355331Samw if (is_shared(hdl, mountpoints[i], *curr_proto) && 12365331Samw unshare_one(hdl, mountpoints[i], 12375331Samw mountpoints[i], *curr_proto) != 0) 12385331Samw goto out; 12395331Samw } 12402474Seschrock } 12412474Seschrock 12422474Seschrock /* 12432474Seschrock * Now unmount everything, removing the underlying directories as 12442474Seschrock * appropriate. 12452474Seschrock */ 12462474Seschrock for (i = 0; i < used; i++) { 12472474Seschrock if (unmount_one(hdl, mountpoints[i], flags) != 0) 12482474Seschrock goto out; 12492676Seschrock } 12502474Seschrock 12512676Seschrock for (i = 0; i < used; i++) { 12522474Seschrock if (datasets[i]) 12532474Seschrock remove_mountpoint(datasets[i]); 12542474Seschrock } 12552474Seschrock 12562474Seschrock ret = 0; 12572474Seschrock out: 12582474Seschrock for (i = 0; i < used; i++) { 12592474Seschrock if (datasets[i]) 12602474Seschrock zfs_close(datasets[i]); 12612474Seschrock free(mountpoints[i]); 12622474Seschrock } 12632474Seschrock free(datasets); 12642474Seschrock free(mountpoints); 12652474Seschrock 12662474Seschrock return (ret); 12672474Seschrock } 1268