2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Routines to manage ZFS mounts. We separate all the nasty routines that have
2N/A * to deal with the OS. The following functions are the main entry points --
2N/A * they are used by mount and unmount and when changing a filesystem's
2N/A * mountpoint.
2N/A *
2N/A * zfs_is_mounted()
2N/A * zfs_mount()
2N/A * zfs_mountall()
2N/A * zfs_unmount()
2N/A * zfs_unmountall()
2N/A *
2N/A * This file also contains the functions used to manage sharing filesystems via
2N/A * NFS and iSCSI:
2N/A *
2N/A * zfs_is_shared()
2N/A * zfs_share()
2N/A * zfs_unshare()
2N/A *
2N/A * zfs_is_shared_nfs()
2N/A * zfs_is_shared_smb()
2N/A * zfs_share_proto()
2N/A * zfs_shareall();
2N/A * zfs_unshare_nfs()
2N/A * zfs_unshare_smb()
2N/A * zfs_unshareall_nfs()
2N/A * zfs_unshareall_smb()
2N/A * zfs_unshareall()
2N/A * zfs_unshareall_bypath()
2N/A *
2N/A * The following functions are available for pool consumers, and will
2N/A * mount/unmount and share/unshare all datasets within pool:
2N/A *
2N/A * zpool_enable_datasets()
2N/A * zpool_disable_datasets()
2N/A */
2N/A
2N/A#include <dirent.h>
2N/A#include <dlfcn.h>
2N/A#include <errno.h>
2N/A#include <libgen.h>
2N/A#include <libintl.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <strings.h>
2N/A#include <unistd.h>
2N/A#include <zone.h>
2N/A#include <sys/mntent.h>
2N/A#include <sys/mount.h>
2N/A#include <sys/stat.h>
2N/A#include <string.h>
2N/A
2N/A#include <libzfs.h>
2N/A
2N/A#include "libzfs_impl.h"
2N/A
2N/A#include <sys/systeminfo.h>
2N/A#define MAXISALEN 257 /* based on sysinfo(2) man page */
2N/A
2N/Astatic int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
2N/Azfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
2N/A zfs_share_proto_t);
2N/A
2N/A/*
2N/A * The share protocols table must be in the same order as the zfs_share_prot_t
2N/A * enum in libzfs_impl.h
2N/A */
2N/Atypedef struct {
2N/A zfs_prop_t p_prop;
2N/A char *p_name;
2N/A sa_proto_t p_sa_type;
2N/A} proto_table_t;
2N/A
2N/Aproto_table_t proto_table[PROTO_END] = {
2N/A {ZFS_PROP_SHARENFS, "nfs", SA_PROT_NFS},
2N/A {ZFS_PROP_SHARESMB, "smb", SA_PROT_SMB},
2N/A};
2N/A
2N/Azfs_share_proto_t nfs_only[] = {
2N/A PROTO_NFS,
2N/A PROTO_END
2N/A};
2N/A
2N/Azfs_share_proto_t smb_only[] = {
2N/A PROTO_SMB,
2N/A PROTO_END
2N/A};
2N/Azfs_share_proto_t share_all_proto[] = {
2N/A PROTO_NFS,
2N/A PROTO_SMB,
2N/A PROTO_END
2N/A};
2N/A
2N/A/*
2N/A * search the sharetab cache for the given mountpoint and protocol, returning
2N/A * a zfs_share_type_t value.
2N/A */
2N/Astatic zfs_share_type_t
2N/Ais_shared(const char *mountpoint, zfs_share_proto_t proto)
2N/A{
2N/A void *sa_hdl;
2N/A nvlist_t *share;
2N/A boolean_t found = B_FALSE;
2N/A sa_proto_t sa_prot;
2N/A sa_proto_t status;
2N/A
2N/A sa_prot = proto_table[proto].p_sa_type;
2N/A
2N/A if (sa_share_find_init(mountpoint, sa_prot, &sa_hdl) != SA_OK)
2N/A return (SHARED_NOT_SHARED);
2N/A
2N/A while (!found && sa_share_find_next(sa_hdl, &share) == SA_OK) {
2N/A status = sa_share_get_status(share);
2N/A if (status & sa_prot)
2N/A found = B_TRUE;
2N/A sa_share_free(share);
2N/A }
2N/A sa_share_find_fini(sa_hdl);
2N/A
2N/A if (!found)
2N/A return (SHARED_NOT_SHARED);
2N/A
2N/A switch (proto) {
2N/A case PROTO_NFS:
2N/A return (SHARED_NFS);
2N/A case PROTO_SMB:
2N/A return (SHARED_SMB);
2N/A default:
2N/A return (0);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Returns true if the specified directory is empty. If we can't open the
2N/A * directory at all, return true so that the mount can fail with a more
2N/A * informative error message.
2N/A */
2N/Astatic boolean_t
2N/Adir_is_empty(const char *dirname)
2N/A{
2N/A DIR *dirp;
2N/A struct dirent64 *dp;
2N/A
2N/A if ((dirp = opendir(dirname)) == NULL)
2N/A return (B_TRUE);
2N/A
2N/A while ((dp = readdir64(dirp)) != NULL) {
2N/A
2N/A if (strcmp(dp->d_name, ".") == 0 ||
2N/A strcmp(dp->d_name, "..") == 0)
2N/A continue;
2N/A
2N/A (void) closedir(dirp);
2N/A return (B_FALSE);
2N/A }
2N/A
2N/A (void) closedir(dirp);
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * Checks to see if the mount is active. If the filesystem is mounted, we fill
2N/A * in 'where' with the current mountpoint, and return 1. Otherwise, we return
2N/A * 0.
2N/A */
2N/Aboolean_t
2N/Ais_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
2N/A{
2N/A struct mnttab entry;
2N/A
2N/A if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
2N/A return (B_FALSE);
2N/A
2N/A if (where != NULL)
2N/A *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
2N/A
2N/A return (B_TRUE);
2N/A}
2N/A
2N/Aboolean_t
2N/Azfs_is_mounted(zfs_handle_t *zhp, char **where)
2N/A{
2N/A if ((zhp->zfs_type & ZFS_TYPE_SHAREABLE) == 0)
2N/A return (B_FALSE);
2N/A
2N/A if (zfs_gather_mount_props(zhp) != 0 || zhp->zfs_mountp == NULL)
2N/A return (B_FALSE);
2N/A
2N/A if (where != NULL)
2N/A *where = zfs_strdup(zhp->zfs_hdl, zhp->zfs_mountp);
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * Returns true if the given dataset is mountable, false otherwise. Returns the
2N/A * mountpoint in 'buf'.
2N/A */
2N/Astatic boolean_t
2N/Azfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
2N/A zprop_source_t *source, boolean_t is_temp)
2N/A{
2N/A char sourceloc[ZFS_MAXNAMELEN];
2N/A zprop_source_t sourcetype;
2N/A
2N/A if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
2N/A return (B_FALSE);
2N/A
2N/A verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
2N/A &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
2N/A
2N/A if ((strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 && !is_temp) ||
2N/A strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
2N/A return (B_FALSE);
2N/A
2N/A if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
2N/A return (B_FALSE);
2N/A
2N/A if (!is_temp && zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
2N/A getzoneid() == GLOBAL_ZONEID)
2N/A return (B_FALSE);
2N/A
2N/A if (source)
2N/A *source = sourcetype;
2N/A
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Mount the given filesystem.
2N/A */
2N/Aint
2N/Azfs_mount(zfs_handle_t *zhp, const char *options, int flags)
2N/A{
2N/A struct stat buf;
2N/A char mountpoint[ZFS_MAXPROPLEN];
2N/A char mntopts[MNT_LINE_MAX];
2N/A libzfs_handle_t *hdl = zhp->zfs_hdl;
2N/A char *tmpmount = NULL;
2N/A boolean_t is_tmpmount = B_FALSE;
2N/A boolean_t is_shadow;
2N/A zprop_source_t source;
2N/A zfs_handle_t *parent_zhp;
2N/A char *tmpopts, *opts;
2N/A
2N/A if (options == NULL)
2N/A mntopts[0] = '\0';
2N/A else
2N/A (void) strlcpy(mntopts, options, sizeof (mntopts));
2N/A
2N/A /*
2N/A * If the pool is imported read-only then all mounts must be read-only
2N/A */
2N/A if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
2N/A flags |= MS_RDONLY;
2N/A
2N/A /*
2N/A * See if there's a temporary mount point defined in the
2N/A * mount options.
2N/A */
2N/A tmpopts = zfs_strdup(zhp->zfs_hdl, mntopts);
2N/A opts = tmpopts;
2N/A while (*opts != '\0') {
2N/A static char *type_subopts[] = { "mountpoint", NULL };
2N/A char *value;
2N/A
2N/A switch (getsubopt(&opts, type_subopts, &value)) {
2N/A case 0:
2N/A is_tmpmount = B_TRUE;
2N/A tmpmount = zfs_strdup(zhp->zfs_hdl, value);
2N/A }
2N/A }
2N/A free(tmpopts);
2N/A
2N/A /*
2N/A * zfs_is_mountable() checks to see whether the dataset's
2N/A * "mountpoint", "canmount", and "zoned" properties are consistent
2N/A * with being mounted. It does not do a full evaluation of all
2N/A * possible obstacles to mounting.
2N/A */
2N/A if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), &source,
2N/A is_tmpmount)) {
2N/A if (tmpmount)
2N/A free(tmpmount);
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * For encrypted datasets attempt to load the key.
2N/A * zfs_key_load will return -1 and set errno to ENOTTY if
2N/A * we need to prompt and we are in the SMF start method
2N/A * for svc:/system/filesystem/local. In hindsight all datasets
2N/A * that have keysource=passphrase,prompt should also have had
2N/A * canmount=noauto. To preserve that partiuclar behaviour
2N/A * we check errno but any other key loading failure we report.
2N/A * Also the zfs_key_load used to be in the zfs_is_mountable()
2N/A * function but it got moved out because that function is also
2N/A * called during dataset destroy and we don't want to attempt
2N/A * key load while destroying a dataset.
2N/A */
2N/A if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
2N/A ZFS_CRYPT_KEY_UNAVAILABLE &&
2N/A zfs_key_load(zhp, B_FALSE, B_FALSE, B_FALSE) != 0)
2N/A return (errno == ENOTTY ? 0 : -1);
2N/A
2N/A
2N/A if (is_tmpmount) {
2N/A (void) strlcpy(mountpoint, tmpmount, ZFS_MAXPROPLEN);
2N/A free(tmpmount);
2N/A }
2N/A
2N/A /*
2N/A * If a dataset being mounted with a regular (i.e., not
2N/A * temporary) mount inherits its mountpoint from its
2N/A * parent, make sure the parent isn't temp-mounted.
2N/A */
2N/A if (!is_tmpmount &&
2N/A (source == ZPROP_SRC_INHERITED || source == ZPROP_SRC_DEFAULT) &&
2N/A strrchr(zfs_get_name(zhp), '/') != NULL) {
2N/A
2N/A char *cpt, *parentname;
2N/A char *where = NULL;
2N/A
2N/A /* lop off last component to construct the parent name */
2N/A parentname = zfs_strdup(hdl, zfs_get_name(zhp));
2N/A cpt = strrchr(parentname, '/');
2N/A *cpt = '\0';
2N/A
2N/A if ((parent_zhp = zfs_cache_get(zhp->zfs_hdl, parentname,
2N/A ZFS_TYPE_FILESYSTEM)) == NULL) {
2N/A free(parentname);
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "failure opening parent"));
2N/A return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot mount '%s' on '%s'"),
2N/A zfs_get_name(zhp), mountpoint));
2N/A }
2N/A
2N/A /*
2N/A * Don't mount parent datasets or check for a temp-mounted
2N/A * parent dataset if we're operating in a local
2N/A * zone and the parent of the dataset is not zoned.
2N/A * In other words, if the parent dataset is across the "zone
2N/A * boundary" from the child dataset, the parent zone
2N/A * can't be mounted anyway, so no point in trying. Nor is
2N/A * there a problem if the parent is temp-mounted. A
2N/A * temporary mount in the global zone doesn't interfere
2N/A * with the mount in local zone.
2N/A */
2N/A if (getzoneid() == GLOBAL_ZONEID ||
2N/A zfs_prop_get_int(parent_zhp, ZFS_PROP_ZONED)) {
2N/A
2N/A if (is_mounted(zhp->zfs_hdl, parentname, &where)) {
2N/A free(where);
2N/A if (zfs_prop_get_int(parent_zhp,
2N/A ZFS_PROP_TMPMOUNTED)) {
2N/A zfs_close_uncached(parent_zhp);
2N/A free(parentname);
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "parent dataset has a temporary "
2N/A "mountpoint"));
2N/A return (zfs_error_fmt(hdl,
2N/A EZFS_MOUNTFAILED,
2N/A dgettext(TEXT_DOMAIN,
2N/A "cannot mount '%s' on '%s'"),
2N/A zfs_get_name(zhp), mountpoint));
2N/A }
2N/A
2N/A } else {
2N/A /*
2N/A * Mount the parent dataset. We do this to
2N/A * lessen the likelihood that mounts will be
2N/A * done out of order, which can cause
2N/A * mountpoint directories to contain
2N/A * intermediate directories, which prevents
2N/A * mounts from succeeding (because zfs won't do
2N/A * a mount on a non-empty directory).
2N/A *
2N/A * We call zfs_mount() recursively to accomplish
2N/A * this, working our way up the dataset
2N/A * hierarchy. The recursion will stop when a
2N/A * dataset is reached which is already mounted
2N/A * or which has a "local" (i.e., not inherited)
2N/A * mountpoint or which has no parent (i.e.,
2N/A * the root of the hierarchy). Then the
2N/A * recursion will unwind, mounting each dataset
2N/A * as it goes down the tree. If a temporarily-
2N/A * mounted dataset is encountered above the one
2N/A * we're trying to mount, report an error and
2N/A * don't mount anything. We don't permit regular
2N/A * zfs mounts under temporary mounts.
2N/A */
2N/A
2N/A int tflag = 0;
2N/A
2N/A if (flags & MS_RDONLY)
2N/A tflag = MS_RDONLY;
2N/A if (zfs_mount(parent_zhp, NULL, tflag) != 0) {
2N/A zfs_close_uncached(parent_zhp);
2N/A free(parentname);
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "failure mounting parent dataset"));
2N/A return (zfs_error_fmt(hdl,
2N/A EZFS_MOUNTFAILED,
2N/A dgettext(TEXT_DOMAIN,
2N/A "cannot mount '%s' on '%s'"),
2N/A zfs_get_name(zhp), mountpoint));
2N/A }
2N/A }
2N/A }
2N/A zfs_close_uncached(parent_zhp);
2N/A free(parentname);
2N/A }
2N/A
2N/A /* Create the directory if it doesn't already exist */
2N/A if (lstat(mountpoint, &buf) != 0) {
2N/A /* temp mounts require that the mount point already exist */
2N/A if (is_tmpmount) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "mountpoint must already exist "
2N/A "for temporary mounts"));
2N/A return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot mount '%s' on '%s'"),
2N/A zfs_get_name(zhp), mountpoint));
2N/A }
2N/A if (mkdirp(mountpoint, 0755) != 0) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "failed to create mountpoint, "
2N/A "mkdir failed : '%s'"), strerror(errno));
2N/A return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot mount '%s' on '%s'"),
2N/A zfs_get_name(zhp), mountpoint));
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Determine if the mountpoint is empty. If so, refuse to perform the
2N/A * mount. We don't perform this check if MS_OVERLAY is specified, which
2N/A * would defeat the point. We also avoid this check if 'remount' is
2N/A * specified.
2N/A */
2N/A if ((flags & MS_OVERLAY) == 0 &&
2N/A strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
2N/A !dir_is_empty(mountpoint)) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "directory is not empty"));
2N/A return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot mount '%s' on '%s'"),
2N/A zfs_get_name(zhp), mountpoint));
2N/A }
2N/A
2N/A /*
2N/A * Check to see if this a shadow mount and adjust the mount options as
2N/A * necessary.
2N/A */
2N/A if (zfs_shadow_mount(zhp, mntopts, &is_shadow) != 0)
2N/A return (-1);
2N/A
2N/A /* perform the mount */
2N/A if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
2N/A MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
2N/A /*
2N/A * Generic errors are nasty, but there are just way too many
2N/A * from mount(), and they're well-understood. We pick a few
2N/A * common ones to improve upon.
2N/A */
2N/A if (errno == EBUSY) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "mountpoint or dataset is busy"));
2N/A } else if (errno == EEXIST) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "Can't mount file system, FSID conflicts "
2N/A "with another mounted file system"));
2N/A } else if (errno == EPERM) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "Insufficient privileges"));
2N/A } else if (errno == ENOTSUP) {
2N/A char buf[256];
2N/A int spa_version;
2N/A
2N/A VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
2N/A (void) snprintf(buf, sizeof (buf),
2N/A dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
2N/A "file system on a version %d pool. Pool must be"
2N/A " upgraded to mount this file system."),
2N/A (u_longlong_t)zfs_prop_get_int(zhp,
2N/A ZFS_PROP_VERSION), spa_version);
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
2N/A } else {
2N/A zfs_error_aux(hdl, strerror(errno));
2N/A }
2N/A
2N/A if (is_shadow)
2N/A (void) zfs_shadow_unmount(zhp);
2N/A
2N/A return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot mount '%s' on '%s'"),
2N/A zfs_get_name(zhp), mountpoint));
2N/A }
2N/A
2N/A /*
2N/A * Add the mounted entry into our cache. Because it may
2N/A * be a remount, remove it first.
2N/A */
2N/A libzfs_mnttab_remove(hdl, zfs_get_name(zhp));
2N/A zfs_clear_mount_props(zhp);
2N/A
2N/A /*
2N/A * Add this newly mounted fs into the cache. The cache must be a
2N/A * complete representation of all known fs's. Note that the mntopts
2N/A * string cannot be added to this entry since it is not complete.
2N/A * mount() only returns the values of properties that were requested.
2N/A * This would miss properties added by the system such as zone=ZONENAME
2N/A * and sharezone=ZONEID. So we leave the mntopts out of the cache
2N/A * entry and add them later if and when they are needed.
2N/A */
2N/A libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
2N/A NULL);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Unmount a single filesystem.
2N/A */
2N/Astatic int
2N/Aunmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
2N/A{
2N/A if (umount2(mountpoint, flags) != 0) {
2N/A zfs_error_aux(hdl, strerror(errno));
2N/A return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
2N/A mountpoint));
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Unmount the given filesystem.
2N/A */
2N/Aint
2N/Azfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
2N/A{
2N/A libzfs_handle_t *hdl = zhp->zfs_hdl;
2N/A char *mntpt = NULL;
2N/A
2N/A /* check to see if we need to unmount the filesystem */
2N/A if (mountpoint != NULL) {
2N/A /*
2N/A * mountpoint may have come from a call to getmnt/getmntany
2N/A * if it isn't NULL. This could get overwritten.
2N/A */
2N/A mntpt = zfs_strdup(hdl, mountpoint);
2N/A } else if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM ||
2N/A !zfs_is_mounted(zhp, &mntpt)) {
2N/A return (0);
2N/A }
2N/A /*
2N/A * Unshare and unmount the filesystem
2N/A */
2N/A if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) {
2N/A free(mntpt);
2N/A return (-1);
2N/A }
2N/A
2N/A if (unmount_one(hdl, mntpt, flags) != 0) {
2N/A free(mntpt);
2N/A (void) zfs_shareall(zhp);
2N/A return (-1);
2N/A }
2N/A libzfs_mnttab_remove(hdl, zhp->zfs_name);
2N/A zfs_clear_mount_props(zhp);
2N/A free(mntpt);
2N/A
2N/A /*
2N/A * Unmount the shadow filesystem if necessary.
2N/A */
2N/A if (zfs_shadow_unmount(zhp) != 0) {
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * mount this filesystem and any children inheriting the mountpoint property.
2N/A * To do this, just act like we're changing the mountpoint property, but don't
2N/A * unmount the filesystems first.
2N/A */
2N/Aint
2N/Azfs_mountall(zfs_handle_t *zhp, int mflags)
2N/A{
2N/A prop_changelist_t *clp;
2N/A int ret;
2N/A
2N/A clp = changelist_gather(zhp,
2N/A zfs_zprop(zfs_get_handle(zhp), ZFS_PROP_MOUNTPOINT),
2N/A CL_GATHER_MOUNT_ALWAYS, mflags);
2N/A if (clp == NULL)
2N/A return (-1);
2N/A
2N/A ret = changelist_postfix(clp);
2N/A changelist_free(clp);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Unmount this filesystem and any children inheriting the mountpoint property.
2N/A * To do this, just act like we're changing the mountpoint property, but don't
2N/A * remount the filesystems afterwards.
2N/A */
2N/Aint
2N/Azfs_unmountall(zfs_handle_t *zhp, int flags)
2N/A{
2N/A prop_changelist_t *clp;
2N/A int ret;
2N/A
2N/A if (zfs_prop_get_int(zhp, ZFS_PROP_TMPMOUNTED))
2N/A return (zfs_unmount(zhp, NULL, flags));
2N/A
2N/A clp = changelist_gather(zhp,
2N/A zfs_zprop(zfs_get_handle(zhp), ZFS_PROP_MOUNTPOINT), 0, flags);
2N/A if (clp == NULL)
2N/A return (-1);
2N/A
2N/A ret = changelist_prefix(clp);
2N/A changelist_free(clp);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Aboolean_t
2N/Azfs_is_shared(zfs_handle_t *zhp)
2N/A{
2N/A zfs_share_type_t rc = 0;
2N/A zfs_share_proto_t *curr_proto;
2N/A
2N/A if (ZFS_IS_VOLUME(zhp))
2N/A return (B_FALSE);
2N/A
2N/A for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
2N/A curr_proto++)
2N/A rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
2N/A
2N/A return (rc ? B_TRUE : B_FALSE);
2N/A}
2N/A
2N/Aint
2N/Azfs_share(zfs_handle_t *zhp)
2N/A{
2N/A assert(!ZFS_IS_VOLUME(zhp));
2N/A if (zhp->zfs_head_type == ZFS_TYPE_FILESYSTEM)
2N/A return (zfs_share_proto(zhp, share_all_proto));
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Azfs_unshare(zfs_handle_t *zhp)
2N/A{
2N/A assert(!ZFS_IS_VOLUME(zhp));
2N/A if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM)
2N/A return (zfs_unshare_proto(zhp, NULL, share_all_proto));
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Check to see if the filesystem is currently shared.
2N/A */
2N/Azfs_share_type_t
2N/Azfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
2N/A{
2N/A char *mountpoint;
2N/A zfs_share_type_t rc;
2N/A
2N/A if (!zfs_is_mounted(zhp, &mountpoint))
2N/A return (SHARED_NOT_SHARED);
2N/A
2N/A if (rc = is_shared(mountpoint, proto)) {
2N/A if (where != NULL)
2N/A *where = mountpoint;
2N/A else
2N/A free(mountpoint);
2N/A return (rc);
2N/A } else {
2N/A free(mountpoint);
2N/A return (SHARED_NOT_SHARED);
2N/A }
2N/A}
2N/A
2N/Aboolean_t
2N/Azfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
2N/A{
2N/A return (zfs_is_shared_proto(zhp, where,
2N/A PROTO_NFS) != SHARED_NOT_SHARED);
2N/A}
2N/A
2N/Aboolean_t
2N/Azfs_is_shared_smb(zfs_handle_t *zhp, char **where)
2N/A{
2N/A return (zfs_is_shared_proto(zhp, where,
2N/A PROTO_SMB) != SHARED_NOT_SHARED);
2N/A}
2N/A
2N/A/*
2N/A * Share the given filesystem according to the options in the specified
2N/A * protocol specific properties (sharenfs, sharesmb). We rely
2N/A * on "libshare" to do the dirty work for us.
2N/A */
2N/Astatic int
2N/Azfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
2N/A{
2N/A char mountpoint[ZFS_MAXPROPLEN];
2N/A libzfs_handle_t *hdl = zhp->zfs_hdl;
2N/A int rc;
2N/A sa_proto_t share_prot = 0;
2N/A zfs_share_proto_t *curr_proto;
2N/A libshare_handle_t *shdl = ZFS_LIBSHARE_HDL(zhp);
2N/A int zoned;
2N/A
2N/A if (!zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM)
2N/A return (0);
2N/A
2N/A if (!zfs_is_mounted(zhp, NULL))
2N/A return (0);
2N/A
2N/A if (!zfs_is_shareable(zhp, mountpoint, sizeof (mountpoint),
2N/A B_FALSE))
2N/A return (0);
2N/A
2N/A /*
2N/A * Perform sharing upgrades
2N/A */
2N/A (void) zfs_sharing_upgrade_impl(zhp, mountpoint);
2N/A
2N/A zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2N/A share_prot = SA_PROT_NONE;
2N/A for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
2N/A /*
2N/A * If the 'zoned' property is set, then zfs_is_shareable()
2N/A * will have already bailed out if we are in the global zone.
2N/A * NFS can be a server in a local zone, but nothing else
2N/A */
2N/A if (zoned && (*curr_proto != PROTO_NFS))
2N/A continue;
2N/A
2N/A /*
2N/A * Skip this protocol, if protocol is not supported
2N/A */
2N/A if (sa_protocol_valid(proto_table[*curr_proto].p_name)
2N/A == SA_OK) {
2N/A share_prot |=
2N/A proto_table[*curr_proto].p_sa_type;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Now publish all shares on this mountpoint/dataset.
2N/A * Wait for completion.
2N/A */
2N/A if (share_prot != SA_PROT_NONE) {
2N/A if ((rc = sa_fs_publish(shdl, mountpoint, share_prot, 1)) !=
2N/A SA_OK) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%s"), sa_strerror(rc));
2N/A
2N/A (void) zfs_error_fmt(hdl, EZFS_SHAREFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot share '%s'"),
2N/A zfs_get_name(zhp));
2N/A return (-1);
2N/A }
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Azfs_share_nfs(zfs_handle_t *zhp)
2N/A{
2N/A return (zfs_share_proto(zhp, nfs_only));
2N/A}
2N/A
2N/Aint
2N/Azfs_share_smb(zfs_handle_t *zhp)
2N/A{
2N/A return (zfs_share_proto(zhp, smb_only));
2N/A}
2N/A
2N/Aint
2N/Azfs_shareall(zfs_handle_t *zhp)
2N/A{
2N/A return (zfs_share_proto(zhp, share_all_proto));
2N/A}
2N/A
2N/A/*
2N/A * Unshare a filesystem by mountpoint.
2N/A */
2N/Astatic int
2N/Aunshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
2N/A zfs_share_proto_t *proto)
2N/A{
2N/A zfs_share_proto_t *curr_proto;
2N/A sa_proto_t sa_prot = SA_PROT_NONE;
2N/A int rc;
2N/A
2N/A for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++)
2N/A sa_prot |= proto_table[*curr_proto].p_sa_type;
2N/A
2N/A if (sa_prot == SA_PROT_NONE)
2N/A return (0);
2N/A
2N/A if ((rc = sa_fs_unpublish(libzfs_get_libshare(hdl), mountpoint,
2N/A sa_prot, 1)) != SA_OK) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%s"), sa_strerror(rc));
2N/A
2N/A return (zfs_error_fmt(hdl, EZFS_UNSHAREFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot unshare '%s'"),
2N/A name));
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Unshare the given filesystem.
2N/A */
2N/Aint
2N/Azfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
2N/A zfs_share_proto_t *proto)
2N/A{
2N/A libzfs_handle_t *hdl = zhp->zfs_hdl;
2N/A char *mntpt;
2N/A int ret;
2N/A
2N/A ASSERT(zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM);
2N/A
2N/A /* check to see if need to unmount the filesystem */
2N/A if (mountpoint != NULL) {
2N/A /*
2N/A * mountpoint may have come from a call to getmnt/getmntany
2N/A * if it isn't NULL. This could get overwritten.
2N/A */
2N/A mntpt = zfs_strdup(hdl, mountpoint);
2N/A } else if (!zfs_is_mounted(zhp, &mntpt)) {
2N/A return (0);
2N/A }
2N/A ret = unshare_one(hdl, zhp->zfs_name, mntpt, proto);
2N/A free(mntpt);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Aint
2N/Azfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
2N/A{
2N/A return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
2N/A}
2N/A
2N/Aint
2N/Azfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
2N/A{
2N/A return (zfs_unshare_proto(zhp, mountpoint, smb_only));
2N/A}
2N/A
2N/A/*
2N/A * Same as zfs_unmountall(), but for NFS and SMB unshares.
2N/A */
2N/Aint
2N/Azfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
2N/A{
2N/A prop_changelist_t *clp;
2N/A int ret;
2N/A
2N/A /* Any share prop will do -- it is just to gather the right FSs */
2N/A clp = changelist_gather(zhp,
2N/A zfs_zprop(zfs_get_handle(zhp), ZFS_PROP_SHARENFS), 0, 0);
2N/A if (clp == NULL)
2N/A return (-1);
2N/A
2N/A ret = changelist_unshare(clp, proto);
2N/A changelist_free(clp);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Aint
2N/Azfs_unshareall_nfs(zfs_handle_t *zhp)
2N/A{
2N/A return (zfs_unshareall_proto(zhp, nfs_only));
2N/A}
2N/A
2N/Aint
2N/Azfs_unshareall_smb(zfs_handle_t *zhp)
2N/A{
2N/A return (zfs_unshareall_proto(zhp, smb_only));
2N/A}
2N/A
2N/Aint
2N/Azfs_unshareall(zfs_handle_t *zhp)
2N/A{
2N/A return (zfs_unshareall_proto(zhp, share_all_proto));
2N/A}
2N/A
2N/Aint
2N/Azfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
2N/A{
2N/A return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
2N/A}
2N/A
2N/A/*
2N/A * Remove the mountpoint associated with the current dataset, if necessary.
2N/A * We only remove the underlying directory if:
2N/A *
2N/A * - The mountpoint is not 'none' or 'legacy'
2N/A * - The mountpoint is non-empty
2N/A * - The mountpoint is the default or inherited
2N/A * - The 'zoned' property is set, or we're in a local zone
2N/A *
2N/A * Any other directories we leave alone.
2N/A */
2N/Avoid
2N/Aremove_mountpoint(zfs_handle_t *zhp)
2N/A{
2N/A char mountpoint[ZFS_MAXPROPLEN];
2N/A zprop_source_t source;
2N/A
2N/A if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
2N/A &source, B_FALSE))
2N/A return;
2N/A
2N/A if (source == ZPROP_SRC_DEFAULT ||
2N/A source == ZPROP_SRC_INHERITED) {
2N/A /*
2N/A * Try to remove the directory, silently ignoring any errors.
2N/A * The filesystem may have since been removed or moved around,
2N/A * and this error isn't really useful to the administrator in
2N/A * any way.
2N/A */
2N/A (void) rmdir(mountpoint);
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Alibzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
2N/A{
2N/A if (cbp->cb_alloc == cbp->cb_used) {
2N/A size_t newsz;
2N/A void *ptr;
2N/A
2N/A newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
2N/A ptr = zfs_realloc(zhp->zfs_hdl,
2N/A cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
2N/A newsz * sizeof (void *));
2N/A cbp->cb_handles = ptr;
2N/A cbp->cb_alloc = newsz;
2N/A }
2N/A cbp->cb_handles[cbp->cb_used++] = zhp;
2N/A}
2N/A
2N/Astatic int
2N/Amount_cb(zfs_handle_t *zhp, void *data)
2N/A{
2N/A get_all_cb_t *cbp = data;
2N/A
2N/A /*
2N/A * Since we are iterating over the datasets in the pool
2N/A * in dataset hierarchy we do a prelim attempt at getting
2N/A * encryption keys loaded if they are needed.
2N/A * This is needed for cases where a dataset's mountpoint
2N/A * is lexically before that of the dataset it inherits
2N/A * the keysource property (and thus its wrapping key) from,
2N/A * eg. pool-0/local/default/1 mountpoint=/export/1
2N/A * but it inherits keysource from pool-0/local/default
2N/A * If we were to return a failure here we would terminate the
2N/A * dataset iteration at this point, so worst case no datasets
2N/A * would get mounted if the top level is encrypted and its key
2N/A * wasn't available but keys for other datasets are.
2N/A */
2N/A if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
2N/A ZFS_CRYPT_KEY_UNAVAILABLE) {
2N/A if (zfs_key_load(zhp, B_FALSE, B_FALSE, B_TRUE) != 0) {
2N/A zfs_close(zhp);
2N/A return (0);
2N/A }
2N/A }
2N/A
2N/A if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
2N/A zfs_close(zhp);
2N/A return (0);
2N/A }
2N/A
2N/A if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
2N/A zfs_close(zhp);
2N/A return (0);
2N/A }
2N/A
2N/A libzfs_add_handle(cbp, zhp);
2N/A if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
2N/A zfs_close(zhp);
2N/A return (-1);
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Alibzfs_dataset_cmp(const void *a, const void *b)
2N/A{
2N/A zfs_handle_t **za = (zfs_handle_t **)a;
2N/A zfs_handle_t **zb = (zfs_handle_t **)b;
2N/A char mounta[MAXPATHLEN];
2N/A char mountb[MAXPATHLEN];
2N/A boolean_t gota, gotb;
2N/A
2N/A if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
2N/A verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2N/A sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2N/A if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
2N/A verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2N/A sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2N/A
2N/A if (gota && gotb)
2N/A return (strcmp(mounta, mountb));
2N/A
2N/A if (gota)
2N/A return (-1);
2N/A if (gotb)
2N/A return (1);
2N/A
2N/A return (strcmp(zfs_get_name(a), zfs_get_name(b)));
2N/A}
2N/A
2N/A/*
2N/A * Mount and share all datasets within the given pool. This assumes that no
2N/A * datasets within the pool are currently mounted. Because users can create
2N/A * complicated nested hierarchies of mountpoints, we first gather all the
2N/A * datasets and mountpoints within the pool, and sort them by mountpoint. Once
2N/A * we have the list of all filesystems, we iterate over them in order and mount
2N/A * and/or share each one.
2N/A */
2N/A#pragma weak zpool_mount_datasets = zpool_enable_datasets
2N/Aint
2N/Azpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
2N/A{
2N/A get_all_cb_t cb = { 0 };
2N/A libzfs_handle_t *hdl = zhp->zpool_hdl;
2N/A zfs_handle_t *zfsp;
2N/A int i, ret = -1;
2N/A int *good;
2N/A
2N/A /*
2N/A * Gather all non-snap datasets within the pool.
2N/A */
2N/A if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
2N/A goto out;
2N/A
2N/A libzfs_add_handle(&cb, zfsp);
2N/A /*
2N/A * If the top level dataset is encrypted load its keys.
2N/A */
2N/A if (zfs_prop_get_int(zfsp, ZFS_PROP_KEYSTATUS) ==
2N/A ZFS_CRYPT_KEY_UNAVAILABLE) {
2N/A (void) zfs_key_load(zfsp, B_FALSE, B_FALSE, B_TRUE);
2N/A }
2N/A
2N/A if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
2N/A goto out;
2N/A /*
2N/A * Sort the datasets by mountpoint.
2N/A */
2N/A qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
2N/A libzfs_dataset_cmp);
2N/A
2N/A /*
2N/A * And mount all the datasets, keeping track of which ones succeeded or
2N/A * failed. Treat an already mounted dataset the same as success.
2N/A */
2N/A if ((good = zfs_alloc(zhp->zpool_hdl,
2N/A cb.cb_used * sizeof (int))) == NULL)
2N/A goto out;
2N/A
2N/A ret = 0;
2N/A for (i = 0; i < cb.cb_used; i++) {
2N/A int err = zfs_mount(cb.cb_handles[i], mntopts, flags);
2N/A
2N/A if (err != 0 && errno == EBUSY &&
2N/A zfs_is_mounted(cb.cb_handles[i], NULL))
2N/A good[i] = 1;
2N/A else if (err == 0)
2N/A good[i] = 1;
2N/A else
2N/A ret = -1;
2N/A }
2N/A
2N/A /*
2N/A * Then share all the ones that need to be shared. This needs
2N/A * to be a separate pass in order to avoid excessive reloading
2N/A * of the configuration. Good should never be NULL since
2N/A * zfs_alloc is supposed to exit if memory isn't available.
2N/A */
2N/A for (i = 0; i < cb.cb_used; i++) {
2N/A if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
2N/A ret = -1;
2N/A }
2N/A
2N/A free(good);
2N/A
2N/Aout:
2N/A for (i = 0; i < cb.cb_used; i++)
2N/A zfs_close(cb.cb_handles[i]);
2N/A free(cb.cb_handles);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Atypedef struct share_info {
2N/A char *si_mountpoint;
2N/A zfs_handle_t *si_zhp;
2N/A} share_info_t;
2N/A
2N/Astatic int
2N/Amountpoint_compare(const void *a, const void *b)
2N/A{
2N/A share_info_t *sa = (share_info_t *)a;
2N/A share_info_t *sb = (share_info_t *)b;
2N/A
2N/A return (strcmp(sb->si_mountpoint, sa->si_mountpoint));
2N/A}
2N/A
2N/A/* alias for 2002/240 */
2N/A#pragma weak zpool_unmount_datasets = zpool_disable_datasets
2N/A/*
2N/A * Unshare and unmount all datasets within the given pool. We don't want to
2N/A * rely on traversing the DSL to discover the filesystems within the pool,
2N/A * because this may be expensive (if not all of them are mounted), and can fail
2N/A * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
2N/A * gather all the filesystems that are currently mounted.
2N/A */
2N/Aint
2N/Azpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
2N/A{
2N/A int used, alloc;
2N/A struct mnttab entry;
2N/A size_t namelen;
2N/A share_info_t *shares;
2N/A libzfs_handle_t *hdl = zhp->zpool_hdl;
2N/A int i;
2N/A int ret = -1;
2N/A int flags = (force ? MS_FORCE : 0);
2N/A
2N/A namelen = strlen(zhp->zpool_name);
2N/A
2N/A rewind(hdl->libzfs_mnttab);
2N/A shares = NULL;
2N/A used = alloc = 0;
2N/A while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
2N/A /*
2N/A * Ignore non-ZFS entries.
2N/A */
2N/A if (entry.mnt_fstype == NULL ||
2N/A strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
2N/A continue;
2N/A
2N/A /*
2N/A * Ignore filesystems not within this pool.
2N/A */
2N/A if (entry.mnt_mountp == NULL ||
2N/A strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
2N/A (entry.mnt_special[namelen] != '/' &&
2N/A entry.mnt_special[namelen] != '\0'))
2N/A continue;
2N/A
2N/A /*
2N/A * At this point we've found a filesystem within our pool. Add
2N/A * it to our growing list.
2N/A */
2N/A if (used == alloc) {
2N/A if (alloc == 0) {
2N/A if ((shares = zfs_alloc(hdl,
2N/A 8 * sizeof (share_info_t))) == NULL)
2N/A goto out;
2N/A
2N/A alloc = 8;
2N/A } else {
2N/A void *ptr;
2N/A
2N/A if ((ptr = zfs_realloc(hdl, shares,
2N/A alloc * sizeof (share_info_t),
2N/A alloc * 2 * sizeof (share_info_t))) == NULL)
2N/A goto out;
2N/A shares = ptr;
2N/A
2N/A alloc *= 2;
2N/A }
2N/A }
2N/A
2N/A if ((shares[used].si_mountpoint = zfs_strdup(hdl,
2N/A entry.mnt_mountp)) == NULL)
2N/A goto out;
2N/A
2N/A /*
2N/A * This is allowed to fail, in case there is some I/O error. It
2N/A * is only used to determine if we need to remove the underlying
2N/A * mountpoint, so failure is not fatal.
2N/A */
2N/A shares[used].si_zhp = make_dataset_handle(hdl,
2N/A entry.mnt_special);
2N/A
2N/A used++;
2N/A }
2N/A
2N/A /*
2N/A * At this point, we have the entire list of filesystems, so sort it by
2N/A * mountpoint.
2N/A */
2N/A qsort(shares, used, sizeof (share_info_t), mountpoint_compare);
2N/A
2N/A /*
2N/A * Walk through and first unshare everything.
2N/A */
2N/A for (i = 0; i < used; i++) {
2N/A if (shares[i].si_zhp != NULL &&
2N/A unshare_one(hdl, zfs_get_name(shares[i].si_zhp),
2N/A shares[i].si_mountpoint, share_all_proto) != 0) {
2N/A (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2N/A "Failed to unshare %s\n"),
2N/A zfs_get_name(shares[i].si_zhp));
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Now unmount everything, removing the underlying directories as
2N/A * appropriate.
2N/A */
2N/A for (i = 0; i < used; i++) {
2N/A if (shares[i].si_zhp != NULL) {
2N/A if (unmount_one(hdl, shares[i].si_mountpoint,
2N/A flags) != 0)
2N/A goto out;
2N/A
2N/A if (zfs_shadow_unmount(shares[i].si_zhp) != 0)
2N/A goto out;
2N/A }
2N/A }
2N/A for (i = 0; i < used; i++) {
2N/A if (shares[i].si_zhp != NULL)
2N/A remove_mountpoint(shares[i].si_zhp);
2N/A }
2N/A
2N/A ret = 0;
2N/Aout:
2N/A for (i = 0; i < used; i++) {
2N/A if (shares[i].si_zhp != NULL)
2N/A zfs_close(shares[i].si_zhp);
2N/A free(shares[i].si_mountpoint);
2N/A }
2N/A free(shares);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Aint
2N/Azfs_get_mountpoint(zfs_handle_t *zhp, char *mountpoint, size_t len)
2N/A{
2N/A zfs_prop_t prop;
2N/A int err;
2N/A
2N/A prop = (zfs_is_share(zhp) ? ZFS_PROP_SHARE_MOUNTPOINT :
2N/A ZFS_PROP_MOUNTPOINT);
2N/A
2N/A if ((err = zfs_prop_get(zhp, prop, mountpoint, len, NULL, NULL, 0,
2N/A B_FALSE)) != 0)
2N/A return (err);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Azfs_get_sharepoint(zfs_handle_t *zhp, char *sharepoint, size_t len)
2N/A{
2N/A int err;
2N/A
2N/A if (!zfs_is_share(zhp))
2N/A return (zfs_get_mountpoint(zhp, sharepoint, len));
2N/A
2N/A if ((err = zfs_prop_get(zhp, ZFS_PROP_SHARE_POINT, sharepoint, len,
2N/A NULL, NULL, 0, B_FALSE)) != 0)
2N/A return (err);
2N/A
2N/A return (0);
2N/A}