2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
2022-07-11 21:16:13 +00:00
|
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
2008-11-20 20:01:55 +00:00
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2016-01-10 22:35:29 +00:00
|
|
|
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
|
2010-05-28 20:45:14 +00:00
|
|
|
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
|
2021-03-08 16:46:45 +00:00
|
|
|
* Copyright (c) 2014, 2021 by Delphix. All rights reserved.
|
2017-02-07 22:02:27 +00:00
|
|
|
* Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
|
2017-06-12 16:56:09 +00:00
|
|
|
* Copyright 2017 RackTop Systems.
|
2018-10-11 04:13:13 +00:00
|
|
|
* Copyright (c) 2018 Datto Inc.
|
2018-10-20 21:48:39 +00:00
|
|
|
* Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines to manage ZFS mounts. We separate all the nasty routines that have
|
|
|
|
* to deal with the OS. The following functions are the main entry points --
|
|
|
|
* they are used by mount and unmount and when changing a filesystem's
|
|
|
|
* mountpoint.
|
|
|
|
*
|
2018-11-05 15:40:05 +00:00
|
|
|
* zfs_is_mounted()
|
|
|
|
* zfs_mount()
|
2020-01-14 16:49:54 +00:00
|
|
|
* zfs_mount_at()
|
2018-11-05 15:40:05 +00:00
|
|
|
* zfs_unmount()
|
|
|
|
* zfs_unmountall()
|
2008-11-20 20:01:55 +00:00
|
|
|
*
|
2022-02-28 15:52:07 +00:00
|
|
|
* This file also contains the functions used to manage sharing filesystems:
|
2008-11-20 20:01:55 +00:00
|
|
|
*
|
2018-11-05 15:40:05 +00:00
|
|
|
* zfs_is_shared()
|
|
|
|
* zfs_share()
|
|
|
|
* zfs_unshare()
|
2008-11-20 20:01:55 +00:00
|
|
|
* zfs_unshareall()
|
2022-02-28 15:52:07 +00:00
|
|
|
* zfs_commit_shares()
|
2008-11-20 20:01:55 +00:00
|
|
|
*
|
|
|
|
* The following functions are available for pool consumers, and will
|
|
|
|
* mount/unmount and share/unshare all datasets within pool:
|
|
|
|
*
|
2018-11-05 15:40:05 +00:00
|
|
|
* zpool_enable_datasets()
|
|
|
|
* zpool_disable_datasets()
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
2017-01-23 18:07:09 +00:00
|
|
|
#include <fcntl.h>
|
2008-11-20 20:01:55 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <libintl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-01-22 00:56:46 +00:00
|
|
|
#include <string.h>
|
2008-11-20 20:01:55 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <zone.h>
|
|
|
|
#include <sys/mntent.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
2017-01-26 20:28:29 +00:00
|
|
|
#include <sys/vfs.h>
|
Native Encryption for ZFS on Linux
This change incorporates three major pieces:
The first change is a keystore that manages wrapping
and encryption keys for encrypted datasets. These
commands mostly involve manipulating the new
DSL Crypto Key ZAP Objects that live in the MOS. Each
encrypted dataset has its own DSL Crypto Key that is
protected with a user's key. This level of indirection
allows users to change their keys without re-encrypting
their entire datasets. The change implements the new
subcommands "zfs load-key", "zfs unload-key" and
"zfs change-key" which allow the user to manage their
encryption keys and settings. In addition, several new
flags and properties have been added to allow dataset
creation and to make mounting and unmounting more
convenient.
The second piece of this patch provides the ability to
encrypt, decyrpt, and authenticate protected datasets.
Each object set maintains a Merkel tree of Message
Authentication Codes that protect the lower layers,
similarly to how checksums are maintained. This part
impacts the zio layer, which handles the actual
encryption and generation of MACs, as well as the ARC
and DMU, which need to be able to handle encrypted
buffers and protected data.
The last addition is the ability to do raw, encrypted
sends and receives. The idea here is to send raw
encrypted and compressed data and receive it exactly
as is on a backup system. This means that the dataset
on the receiving system is protected using the same
user key that is in use on the sending side. By doing
so, datasets can be efficiently backed up to an
untrusted system without fear of data being
compromised.
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Jorgen Lundman <lundman@lundman.net>
Signed-off-by: Tom Caputi <tcaputi@datto.com>
Closes #494
Closes #5769
2017-08-14 17:36:48 +00:00
|
|
|
#include <sys/dsl_crypt.h>
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
#include <libzfs.h>
|
|
|
|
|
|
|
|
#include "libzfs_impl.h"
|
2018-11-05 15:40:05 +00:00
|
|
|
#include <thread_pool.h>
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
#include <libshare.h>
|
|
|
|
#include <sys/systeminfo.h>
|
|
|
|
#define MAXISALEN 257 /* based on sysinfo(2) man page */
|
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */
|
|
|
|
|
|
|
|
static void zfs_mount_task(void *);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2022-02-28 14:46:25 +00:00
|
|
|
static const proto_table_t proto_table[SA_PROTOCOL_COUNT] = {
|
|
|
|
[SA_PROTOCOL_NFS] =
|
|
|
|
{ZFS_PROP_SHARENFS, EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
|
|
|
|
[SA_PROTOCOL_SMB] =
|
|
|
|
{ZFS_PROP_SHARESMB, EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
|
2008-11-20 20:01:55 +00:00
|
|
|
};
|
|
|
|
|
2022-02-28 14:46:25 +00:00
|
|
|
static const enum sa_protocol share_all_proto[SA_PROTOCOL_COUNT + 1] = {
|
|
|
|
SA_PROTOCOL_NFS,
|
|
|
|
SA_PROTOCOL_SMB,
|
|
|
|
SA_NO_PROTOCOL
|
2008-11-20 20:01:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static boolean_t
|
2017-01-26 20:28:29 +00:00
|
|
|
dir_is_empty_stat(const char *dirname)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only want to return false if the given path is a non empty
|
|
|
|
* directory, all other errors are handled elsewhere.
|
|
|
|
*/
|
|
|
|
if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An empty directory will still have two entries in it, one
|
|
|
|
* entry for each of "." and "..".
|
|
|
|
*/
|
|
|
|
if (st.st_size > 2) {
|
|
|
|
return (B_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean_t
|
|
|
|
dir_is_empty_readdir(const char *dirname)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent64 *dp;
|
2017-01-23 18:07:09 +00:00
|
|
|
int dirfd;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2017-01-23 18:07:09 +00:00
|
|
|
if ((dirfd = openat(AT_FDCWD, dirname,
|
|
|
|
O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
|
2008-11-20 20:01:55 +00:00
|
|
|
return (B_TRUE);
|
2017-01-23 18:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((dirp = fdopendir(dirfd)) == NULL) {
|
2017-06-01 20:27:02 +00:00
|
|
|
(void) close(dirfd);
|
2017-01-23 18:07:09 +00:00
|
|
|
return (B_TRUE);
|
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
while ((dp = readdir64(dirp)) != NULL) {
|
|
|
|
|
|
|
|
if (strcmp(dp->d_name, ".") == 0 ||
|
|
|
|
strcmp(dp->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
(void) closedir(dirp);
|
|
|
|
return (B_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) closedir(dirp);
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
2017-01-26 20:28:29 +00:00
|
|
|
/*
|
|
|
|
* Returns true if the specified directory is empty. If we can't open the
|
|
|
|
* directory at all, return true so that the mount can fail with a more
|
|
|
|
* informative error message.
|
|
|
|
*/
|
|
|
|
static boolean_t
|
|
|
|
dir_is_empty(const char *dirname)
|
|
|
|
{
|
|
|
|
struct statfs64 st;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the statvfs call fails or the filesystem is not a ZFS
|
|
|
|
* filesystem, fall back to the slow path which uses readdir.
|
|
|
|
*/
|
|
|
|
if ((statfs64(dirname, &st) != 0) ||
|
|
|
|
(st.f_type != ZFS_SUPER_MAGIC)) {
|
|
|
|
return (dir_is_empty_readdir(dirname));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, we know the provided path is on a ZFS
|
|
|
|
* filesystem, so we can use stat instead of readdir to
|
|
|
|
* determine if the directory is empty or not. We try to avoid
|
|
|
|
* using readdir because that requires opening "dirname"; this
|
|
|
|
* open file descriptor can potentially end up in a child
|
|
|
|
* process if there's a concurrent fork, thus preventing the
|
|
|
|
* zfs_mount() from otherwise succeeding (the open file
|
|
|
|
* descriptor inherited by the child process will cause the
|
|
|
|
* parent's mount to fail with EBUSY). The performance
|
|
|
|
* implications of replacing the open, read, and close with a
|
|
|
|
* single stat is nice; but is not the main motivation for the
|
|
|
|
* added complexity.
|
|
|
|
*/
|
|
|
|
return (dir_is_empty_stat(dirname));
|
|
|
|
}
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Checks to see if the mount is active. If the filesystem is mounted, we fill
|
|
|
|
* in 'where' with the current mountpoint, and return 1. Otherwise, we return
|
|
|
|
* 0.
|
|
|
|
*/
|
|
|
|
boolean_t
|
|
|
|
is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
|
|
|
|
{
|
2009-01-15 21:59:39 +00:00
|
|
|
struct mnttab entry;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2009-01-15 21:59:39 +00:00
|
|
|
if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
|
2008-11-20 20:01:55 +00:00
|
|
|
return (B_FALSE);
|
|
|
|
|
|
|
|
if (where != NULL)
|
|
|
|
*where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
|
|
|
|
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean_t
|
|
|
|
zfs_is_mounted(zfs_handle_t *zhp, char **where)
|
|
|
|
{
|
|
|
|
return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:49:54 +00:00
|
|
|
/*
|
|
|
|
* Checks any higher order concerns about whether the given dataset is
|
|
|
|
* mountable, false otherwise. zfs_is_mountable_internal specifically assumes
|
|
|
|
* that the caller has verified the sanity of mounting the dataset at
|
2021-12-12 14:38:17 +00:00
|
|
|
* its mountpoint to the extent the caller wants.
|
2020-01-14 16:49:54 +00:00
|
|
|
*/
|
|
|
|
static boolean_t
|
2021-12-12 14:38:17 +00:00
|
|
|
zfs_is_mountable_internal(zfs_handle_t *zhp)
|
2020-01-14 16:49:54 +00:00
|
|
|
{
|
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
|
|
|
|
getzoneid() == GLOBAL_ZONEID)
|
|
|
|
return (B_FALSE);
|
|
|
|
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Returns true if the given dataset is mountable, false otherwise. Returns the
|
|
|
|
* mountpoint in 'buf'.
|
|
|
|
*/
|
2022-02-28 15:52:07 +00:00
|
|
|
static boolean_t
|
2008-11-20 20:01:55 +00:00
|
|
|
zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
|
Implement Redacted Send/Receive
Redacted send/receive allows users to send subsets of their data to
a target system. One possible use case for this feature is to not
transmit sensitive information to a data warehousing, test/dev, or
analytics environment. Another is to save space by not replicating
unimportant data within a given dataset, for example in backup tools
like zrepl.
Redacted send/receive is a three-stage process. First, a clone (or
clones) is made of the snapshot to be sent to the target. In this
clone (or clones), all unnecessary or unwanted data is removed or
modified. This clone is then snapshotted to create the "redaction
snapshot" (or snapshots). Second, the new zfs redact command is used
to create a redaction bookmark. The redaction bookmark stores the
list of blocks in a snapshot that were modified by the redaction
snapshot(s). Finally, the redaction bookmark is passed as a parameter
to zfs send. When sending to the snapshot that was redacted, the
redaction bookmark is used to filter out blocks that contain sensitive
or unwanted information, and those blocks are not included in the send
stream. When sending from the redaction bookmark, the blocks it
contains are considered as candidate blocks in addition to those
blocks in the destination snapshot that were modified since the
creation_txg of the redaction bookmark. This step is necessary to
allow the target to rehydrate data in the case where some blocks are
accidentally or unnecessarily modified in the redaction snapshot.
The changes to bookmarks to enable fast space estimation involve
adding deadlists to bookmarks. There is also logic to manage the
life cycles of these deadlists.
The new size estimation process operates in cases where previously
an accurate estimate could not be provided. In those cases, a send
is performed where no data blocks are read, reducing the runtime
significantly and providing a byte-accurate size estimate.
Reviewed-by: Dan Kimmel <dan.kimmel@delphix.com>
Reviewed-by: Matt Ahrens <mahrens@delphix.com>
Reviewed-by: Prashanth Sreenivasa <pks@delphix.com>
Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Reviewed-by: George Wilson <george.wilson@delphix.com>
Reviewed-by: Chris Williamson <chris.williamson@delphix.com>
Reviewed-by: Pavel Zhakarov <pavel.zakharov@delphix.com>
Reviewed-by: Sebastien Roy <sebastien.roy@delphix.com>
Reviewed-by: Prakash Surya <prakash.surya@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Paul Dagnelie <pcd@delphix.com>
Closes #7958
2019-06-19 16:48:13 +00:00
|
|
|
zprop_source_t *source, int flags)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2016-06-15 21:28:36 +00:00
|
|
|
char sourceloc[MAXNAMELEN];
|
undocumented libzfs API changes broke "zfs list"
While OpenZFS does permit breaking changes to the libzfs API, we should
avoid these changes when reasonably possible, and take steps to mitigate
the impact to consumers when changes are necessary.
Commit e4288a8397bb1f made a libzfs API change that is especially
difficult for consumers because there is no change to the function
signatures, only to their behavior. Therefore, consumers can't notice
that there was a change at compile time. Also, the API change was
incompletely and incorrectly documented.
The commit message mentions `zfs_get_prop()` [sic], but all callers of
`get_numeric_property()` are impacted: `zfs_prop_get()`,
`zfs_prop_get_numeric()`, and `zfs_prop_get_int()`.
`zfs_prop_get_int()` always calls `get_numeric_property(src=NULL)`, so
it assumes that the filesystem is not mounted. This means that e.g.
`zfs_prop_get_int(ZFS_PROP_MOUNTED)` always returns 0.
The documentation says that to preserve the previous behavior, callers
should initialize `*src=ZPROP_SRC_NONE`, and some callers were changed
to do that. However, the existing behavior is actually preserved by
initializing `*src=ZPROP_SRC_ALL`, not `NONE`.
The code comment above `zfs_prop_get()` says, "src: ... NULL will be
treated as ZPROP_SRC_ALL.". However, the code actually treats NULL as
ZPROP_SRC_NONE. i.e. `zfs_prop_get(src=NULL)` assumes that the
filesystem is not mounted.
There are several existing calls which use `src=NULL` which are impacted
by the API change, most noticeably those used by `zfs list`, which now
assumes that filesystems are not mounted. For example,
`zfs list -o name,mounted` previously indicated whether a filesystem was
mounted or not, but now it always (incorrectly) indicates that the
filesystem is not mounted (`MOUNTED: no`). Similarly, properties that
are set at mount time are ignored. E.g. `zfs list -o name,atime` may
display an incorrect value if it was set at mount time.
To address these problems, this commit reverts commit e4288a8397bb1f:
"zfs get: don't lookup mount options when using "-s local""
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #11999
2021-05-06 18:24:56 +00:00
|
|
|
zprop_source_t sourcetype;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2014-04-21 18:22:08 +00:00
|
|
|
if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type,
|
|
|
|
B_FALSE))
|
2008-11-20 20:01:55 +00:00
|
|
|
return (B_FALSE);
|
|
|
|
|
|
|
|
verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
|
|
|
|
&sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
|
|
|
|
|
|
|
|
if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
|
|
|
|
strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
|
|
|
|
return (B_FALSE);
|
|
|
|
|
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
|
|
|
|
return (B_FALSE);
|
|
|
|
|
2021-12-12 14:38:17 +00:00
|
|
|
if (!zfs_is_mountable_internal(zhp))
|
2008-11-20 20:01:55 +00:00
|
|
|
return (B_FALSE);
|
|
|
|
|
Implement Redacted Send/Receive
Redacted send/receive allows users to send subsets of their data to
a target system. One possible use case for this feature is to not
transmit sensitive information to a data warehousing, test/dev, or
analytics environment. Another is to save space by not replicating
unimportant data within a given dataset, for example in backup tools
like zrepl.
Redacted send/receive is a three-stage process. First, a clone (or
clones) is made of the snapshot to be sent to the target. In this
clone (or clones), all unnecessary or unwanted data is removed or
modified. This clone is then snapshotted to create the "redaction
snapshot" (or snapshots). Second, the new zfs redact command is used
to create a redaction bookmark. The redaction bookmark stores the
list of blocks in a snapshot that were modified by the redaction
snapshot(s). Finally, the redaction bookmark is passed as a parameter
to zfs send. When sending to the snapshot that was redacted, the
redaction bookmark is used to filter out blocks that contain sensitive
or unwanted information, and those blocks are not included in the send
stream. When sending from the redaction bookmark, the blocks it
contains are considered as candidate blocks in addition to those
blocks in the destination snapshot that were modified since the
creation_txg of the redaction bookmark. This step is necessary to
allow the target to rehydrate data in the case where some blocks are
accidentally or unnecessarily modified in the redaction snapshot.
The changes to bookmarks to enable fast space estimation involve
adding deadlists to bookmarks. There is also logic to manage the
life cycles of these deadlists.
The new size estimation process operates in cases where previously
an accurate estimate could not be provided. In those cases, a send
is performed where no data blocks are read, reducing the runtime
significantly and providing a byte-accurate size estimate.
Reviewed-by: Dan Kimmel <dan.kimmel@delphix.com>
Reviewed-by: Matt Ahrens <mahrens@delphix.com>
Reviewed-by: Prashanth Sreenivasa <pks@delphix.com>
Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Reviewed-by: George Wilson <george.wilson@delphix.com>
Reviewed-by: Chris Williamson <chris.williamson@delphix.com>
Reviewed-by: Pavel Zhakarov <pavel.zakharov@delphix.com>
Reviewed-by: Sebastien Roy <sebastien.roy@delphix.com>
Reviewed-by: Prakash Surya <prakash.surya@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Paul Dagnelie <pcd@delphix.com>
Closes #7958
2019-06-19 16:48:13 +00:00
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE))
|
|
|
|
return (B_FALSE);
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
if (source)
|
|
|
|
*source = sourcetype;
|
|
|
|
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
2010-12-17 00:16:25 +00:00
|
|
|
/*
|
|
|
|
* The filesystem is mounted by invoking the system mount utility rather
|
|
|
|
* than by the system call mount(2). This ensures that the /etc/mtab
|
|
|
|
* file is correctly locked for the update. Performing our own locking
|
|
|
|
* and /etc/mtab update requires making an unsafe assumption about how
|
|
|
|
* the mount utility performs its locking. Unfortunately, this also means
|
|
|
|
* in the case of a mount failure we do not have the exact errno. We must
|
|
|
|
* make due with return value from the mount process.
|
|
|
|
*
|
|
|
|
* In the long term a shared library called libmount is under development
|
|
|
|
* which provides a common API to address the locking and errno issues.
|
|
|
|
* Once the standard mount utility has been updated to use this library
|
|
|
|
* we can add an autoconf check to conditionally use it.
|
|
|
|
*
|
|
|
|
* http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
|
|
|
|
*/
|
|
|
|
|
2011-05-19 18:44:07 +00:00
|
|
|
static int
|
|
|
|
zfs_add_option(zfs_handle_t *zhp, char *options, int len,
|
2022-04-19 18:38:30 +00:00
|
|
|
zfs_prop_t prop, const char *on, const char *off)
|
2011-05-19 18:44:07 +00:00
|
|
|
{
|
|
|
|
char *source;
|
|
|
|
uint64_t value;
|
|
|
|
|
|
|
|
/* Skip adding duplicate default options */
|
|
|
|
if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
2016-09-20 17:07:58 +00:00
|
|
|
* zfs_prop_get_int() is not used to ensure our mount options
|
|
|
|
* are not influenced by the current /proc/self/mounts contents.
|
2011-05-19 18:44:07 +00:00
|
|
|
*/
|
|
|
|
value = getprop_uint64(zhp, prop, &source);
|
|
|
|
|
|
|
|
(void) strlcat(options, ",", len);
|
|
|
|
(void) strlcat(options, value ? on : off, len);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
zfs_add_options(zfs_handle_t *zhp, char *options, int len)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
error = zfs_add_option(zhp, options, len,
|
|
|
|
ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME);
|
2016-04-01 20:12:06 +00:00
|
|
|
/*
|
|
|
|
* don't add relatime/strictatime when atime=off, otherwise strictatime
|
|
|
|
* will force atime=on
|
|
|
|
*/
|
|
|
|
if (strstr(options, MNTOPT_NOATIME) == NULL) {
|
|
|
|
error = zfs_add_option(zhp, options, len,
|
|
|
|
ZFS_PROP_RELATIME, MNTOPT_RELATIME, MNTOPT_STRICTATIME);
|
|
|
|
}
|
2011-05-19 18:44:07 +00:00
|
|
|
error = error ? error : zfs_add_option(zhp, options, len,
|
|
|
|
ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES);
|
|
|
|
error = error ? error : zfs_add_option(zhp, options, len,
|
|
|
|
ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC);
|
|
|
|
error = error ? error : zfs_add_option(zhp, options, len,
|
|
|
|
ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW);
|
|
|
|
error = error ? error : zfs_add_option(zhp, options, len,
|
|
|
|
ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID);
|
|
|
|
error = error ? error : zfs_add_option(zhp, options, len,
|
|
|
|
ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:49:54 +00:00
|
|
|
int
|
|
|
|
zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
|
|
|
|
{
|
|
|
|
char mountpoint[ZFS_MAXPROPLEN];
|
|
|
|
|
|
|
|
if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL,
|
|
|
|
flags))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (zfs_mount_at(zhp, options, flags, mountpoint));
|
|
|
|
}
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Mount the given filesystem.
|
|
|
|
*/
|
|
|
|
int
|
2020-01-14 16:49:54 +00:00
|
|
|
zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags,
|
|
|
|
const char *mountpoint)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
char mntopts[MNT_LINE_MAX];
|
2015-03-26 19:10:26 +00:00
|
|
|
char overlay[ZFS_MAXPROPLEN];
|
2021-04-13 04:26:55 +00:00
|
|
|
char prop_encroot[MAXNAMELEN];
|
|
|
|
boolean_t is_encroot;
|
|
|
|
zfs_handle_t *encroot_hp = zhp;
|
2008-11-20 20:01:55 +00:00
|
|
|
libzfs_handle_t *hdl = zhp->zfs_hdl;
|
Native Encryption for ZFS on Linux
This change incorporates three major pieces:
The first change is a keystore that manages wrapping
and encryption keys for encrypted datasets. These
commands mostly involve manipulating the new
DSL Crypto Key ZAP Objects that live in the MOS. Each
encrypted dataset has its own DSL Crypto Key that is
protected with a user's key. This level of indirection
allows users to change their keys without re-encrypting
their entire datasets. The change implements the new
subcommands "zfs load-key", "zfs unload-key" and
"zfs change-key" which allow the user to manage their
encryption keys and settings. In addition, several new
flags and properties have been added to allow dataset
creation and to make mounting and unmounting more
convenient.
The second piece of this patch provides the ability to
encrypt, decyrpt, and authenticate protected datasets.
Each object set maintains a Merkel tree of Message
Authentication Codes that protect the lower layers,
similarly to how checksums are maintained. This part
impacts the zio layer, which handles the actual
encryption and generation of MACs, as well as the ARC
and DMU, which need to be able to handle encrypted
buffers and protected data.
The last addition is the ability to do raw, encrypted
sends and receives. The idea here is to send raw
encrypted and compressed data and receive it exactly
as is on a backup system. This means that the dataset
on the receiving system is protected using the same
user key that is in use on the sending side. By doing
so, datasets can be efficiently backed up to an
untrusted system without fear of data being
compromised.
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Jorgen Lundman <lundman@lundman.net>
Signed-off-by: Tom Caputi <tcaputi@datto.com>
Closes #494
Closes #5769
2017-08-14 17:36:48 +00:00
|
|
|
uint64_t keystatus;
|
2011-05-19 18:44:07 +00:00
|
|
|
int remount = 0, rc;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2011-05-19 18:44:07 +00:00
|
|
|
if (options == NULL) {
|
2010-12-17 00:16:25 +00:00
|
|
|
(void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
|
2011-05-19 18:44:07 +00:00
|
|
|
} else {
|
2008-11-20 20:01:55 +00:00
|
|
|
(void) strlcpy(mntopts, options, sizeof (mntopts));
|
2011-05-19 18:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(mntopts, MNTOPT_REMOUNT) != NULL)
|
|
|
|
remount = 1;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2020-01-14 16:49:54 +00:00
|
|
|
/* Potentially duplicates some checks if invoked by zfs_mount(). */
|
2021-12-12 14:38:17 +00:00
|
|
|
if (!zfs_is_mountable_internal(zhp))
|
2020-01-14 16:49:54 +00:00
|
|
|
return (0);
|
|
|
|
|
2010-08-26 21:24:34 +00:00
|
|
|
/*
|
|
|
|
* If the pool is imported read-only then all mounts must be read-only
|
|
|
|
*/
|
|
|
|
if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
|
2010-12-17 00:16:25 +00:00
|
|
|
(void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
|
|
|
|
|
2011-05-19 18:44:07 +00:00
|
|
|
/*
|
|
|
|
* Append default mount options which apply to the mount point.
|
|
|
|
* This is done because under Linux (unlike Solaris) multiple mount
|
|
|
|
* points may reference a single super block. This means that just
|
|
|
|
* given a super block there is no back reference to update the per
|
|
|
|
* mount point options.
|
|
|
|
*/
|
|
|
|
rc = zfs_add_options(zhp, mntopts, sizeof (mntopts));
|
|
|
|
if (rc) {
|
|
|
|
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
|
|
|
"default options unavailable"));
|
|
|
|
return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
|
|
|
|
dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
|
|
|
|
mountpoint));
|
|
|
|
}
|
|
|
|
|
Native Encryption for ZFS on Linux
This change incorporates three major pieces:
The first change is a keystore that manages wrapping
and encryption keys for encrypted datasets. These
commands mostly involve manipulating the new
DSL Crypto Key ZAP Objects that live in the MOS. Each
encrypted dataset has its own DSL Crypto Key that is
protected with a user's key. This level of indirection
allows users to change their keys without re-encrypting
their entire datasets. The change implements the new
subcommands "zfs load-key", "zfs unload-key" and
"zfs change-key" which allow the user to manage their
encryption keys and settings. In addition, several new
flags and properties have been added to allow dataset
creation and to make mounting and unmounting more
convenient.
The second piece of this patch provides the ability to
encrypt, decyrpt, and authenticate protected datasets.
Each object set maintains a Merkel tree of Message
Authentication Codes that protect the lower layers,
similarly to how checksums are maintained. This part
impacts the zio layer, which handles the actual
encryption and generation of MACs, as well as the ARC
and DMU, which need to be able to handle encrypted
buffers and protected data.
The last addition is the ability to do raw, encrypted
sends and receives. The idea here is to send raw
encrypted and compressed data and receive it exactly
as is on a backup system. This means that the dataset
on the receiving system is protected using the same
user key that is in use on the sending side. By doing
so, datasets can be efficiently backed up to an
untrusted system without fear of data being
compromised.
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Jorgen Lundman <lundman@lundman.net>
Signed-off-by: Tom Caputi <tcaputi@datto.com>
Closes #494
Closes #5769
2017-08-14 17:36:48 +00:00
|
|
|
/*
|
|
|
|
* If the filesystem is encrypted the key must be loaded in order to
|
|
|
|
* mount. If the key isn't loaded, the MS_CRYPT flag decides whether
|
|
|
|
* or not we attempt to load the keys. Note: we must call
|
|
|
|
* zfs_refresh_properties() here since some callers of this function
|
|
|
|
* (most notably zpool_enable_datasets()) may implicitly load our key
|
|
|
|
* by loading the parent's key first.
|
|
|
|
*/
|
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
|
|
|
|
zfs_refresh_properties(zhp);
|
|
|
|
keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the key is unavailable and MS_CRYPT is set give the
|
|
|
|
* user a chance to enter the key. Otherwise just fail
|
|
|
|
* immediately.
|
|
|
|
*/
|
|
|
|
if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
|
|
|
|
if (flags & MS_CRYPT) {
|
2021-04-13 04:26:55 +00:00
|
|
|
rc = zfs_crypto_get_encryption_root(zhp,
|
|
|
|
&is_encroot, prop_encroot);
|
|
|
|
if (rc) {
|
|
|
|
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
|
|
|
"Failed to get encryption root for "
|
|
|
|
"'%s'."), zfs_get_name(zhp));
|
|
|
|
return (rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_encroot) {
|
|
|
|
encroot_hp = zfs_open(hdl, prop_encroot,
|
|
|
|
ZFS_TYPE_DATASET);
|
|
|
|
if (encroot_hp == NULL)
|
|
|
|
return (hdl->libzfs_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = zfs_crypto_load_key(encroot_hp,
|
|
|
|
B_FALSE, NULL);
|
|
|
|
|
|
|
|
if (!is_encroot)
|
|
|
|
zfs_close(encroot_hp);
|
Native Encryption for ZFS on Linux
This change incorporates three major pieces:
The first change is a keystore that manages wrapping
and encryption keys for encrypted datasets. These
commands mostly involve manipulating the new
DSL Crypto Key ZAP Objects that live in the MOS. Each
encrypted dataset has its own DSL Crypto Key that is
protected with a user's key. This level of indirection
allows users to change their keys without re-encrypting
their entire datasets. The change implements the new
subcommands "zfs load-key", "zfs unload-key" and
"zfs change-key" which allow the user to manage their
encryption keys and settings. In addition, several new
flags and properties have been added to allow dataset
creation and to make mounting and unmounting more
convenient.
The second piece of this patch provides the ability to
encrypt, decyrpt, and authenticate protected datasets.
Each object set maintains a Merkel tree of Message
Authentication Codes that protect the lower layers,
similarly to how checksums are maintained. This part
impacts the zio layer, which handles the actual
encryption and generation of MACs, as well as the ARC
and DMU, which need to be able to handle encrypted
buffers and protected data.
The last addition is the ability to do raw, encrypted
sends and receives. The idea here is to send raw
encrypted and compressed data and receive it exactly
as is on a backup system. This means that the dataset
on the receiving system is protected using the same
user key that is in use on the sending side. By doing
so, datasets can be efficiently backed up to an
untrusted system without fear of data being
compromised.
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Jorgen Lundman <lundman@lundman.net>
Signed-off-by: Tom Caputi <tcaputi@datto.com>
Closes #494
Closes #5769
2017-08-14 17:36:48 +00:00
|
|
|
if (rc)
|
|
|
|
return (rc);
|
|
|
|
} else {
|
|
|
|
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
|
|
|
"encryption key not loaded"));
|
|
|
|
return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
|
|
|
|
dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
|
|
|
|
mountpoint));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-17 00:16:25 +00:00
|
|
|
/*
|
|
|
|
* Append zfsutil option so the mount helper allow the mount
|
|
|
|
*/
|
|
|
|
strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
|
2010-08-26 21:24:34 +00:00
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
/* Create the directory if it doesn't already exist */
|
|
|
|
if (lstat(mountpoint, &buf) != 0) {
|
|
|
|
if (mkdirp(mountpoint, 0755) != 0) {
|
|
|
|
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
2020-07-12 00:16:13 +00:00
|
|
|
"failed to create mountpoint: %s"),
|
|
|
|
strerror(errno));
|
2008-11-20 20:01:55 +00:00
|
|
|
return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
|
|
|
|
dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
|
|
|
|
mountpoint));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 19:10:26 +00:00
|
|
|
/*
|
2020-03-06 17:28:19 +00:00
|
|
|
* Overlay mounts are enabled by default but may be disabled
|
|
|
|
* via the 'overlay' property. The -O flag remains for compatibility.
|
2015-03-26 19:10:26 +00:00
|
|
|
*/
|
|
|
|
if (!(flags & MS_OVERLAY)) {
|
|
|
|
if (zfs_prop_get(zhp, ZFS_PROP_OVERLAY, overlay,
|
2016-12-12 18:46:26 +00:00
|
|
|
sizeof (overlay), NULL, NULL, 0, B_FALSE) == 0) {
|
2015-03-26 19:10:26 +00:00
|
|
|
if (strcmp(overlay, "on") == 0) {
|
|
|
|
flags |= MS_OVERLAY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Determine if the mountpoint is empty. If so, refuse to perform the
|
2012-01-12 00:48:02 +00:00
|
|
|
* mount. We don't perform this check if 'remount' is
|
2020-03-06 17:28:19 +00:00
|
|
|
* specified or if overlay option (-O) is given
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2012-01-12 00:48:02 +00:00
|
|
|
if ((flags & MS_OVERLAY) == 0 && !remount &&
|
|
|
|
!dir_is_empty(mountpoint)) {
|
2008-11-20 20:01:55 +00:00
|
|
|
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
|
|
|
"directory is not empty"));
|
|
|
|
return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
|
|
|
|
dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* perform the mount */
|
2020-05-21 01:02:41 +00:00
|
|
|
rc = do_mount(zhp, mountpoint, mntopts, flags);
|
2010-12-17 00:16:25 +00:00
|
|
|
if (rc) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Generic errors are nasty, but there are just way too many
|
|
|
|
* from mount(), and they're well-understood. We pick a few
|
|
|
|
* common ones to improve upon.
|
|
|
|
*/
|
2010-12-17 00:16:25 +00:00
|
|
|
if (rc == EBUSY) {
|
2008-11-20 20:01:55 +00:00
|
|
|
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
|
|
|
"mountpoint or dataset is busy"));
|
2010-12-17 00:16:25 +00:00
|
|
|
} else if (rc == EPERM) {
|
2008-11-20 20:01:55 +00:00
|
|
|
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
|
|
|
"Insufficient privileges"));
|
2010-12-17 00:16:25 +00:00
|
|
|
} else if (rc == ENOTSUP) {
|
2010-05-28 20:45:14 +00:00
|
|
|
int spa_version;
|
|
|
|
|
|
|
|
VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
|
2021-05-15 10:23:45 +00:00
|
|
|
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
|
|
|
"Can't mount a version %llu "
|
2010-05-28 20:45:14 +00:00
|
|
|
"file system on a version %d pool. Pool must be"
|
|
|
|
" upgraded to mount this file system."),
|
|
|
|
(u_longlong_t)zfs_prop_get_int(zhp,
|
|
|
|
ZFS_PROP_VERSION), spa_version);
|
2008-11-20 20:01:55 +00:00
|
|
|
} else {
|
2021-05-15 10:23:45 +00:00
|
|
|
zfs_error_aux(hdl, "%s", strerror(rc));
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
|
|
|
|
dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
|
|
|
|
zhp->zfs_name));
|
|
|
|
}
|
|
|
|
|
2011-05-19 18:44:07 +00:00
|
|
|
/* remove the mounted entry before re-adding on remount */
|
|
|
|
if (remount)
|
|
|
|
libzfs_mnttab_remove(hdl, zhp->zfs_name);
|
|
|
|
|
2009-01-15 21:59:39 +00:00
|
|
|
/* add the mounted entry into our cache */
|
2010-12-17 00:16:25 +00:00
|
|
|
libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
|
2008-11-20 20:01:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmount a single filesystem.
|
|
|
|
*/
|
|
|
|
static int
|
2021-07-15 18:31:00 +00:00
|
|
|
unmount_one(zfs_handle_t *zhp, const char *mountpoint, int flags)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2011-03-07 18:10:20 +00:00
|
|
|
int error;
|
|
|
|
|
2021-07-15 18:31:00 +00:00
|
|
|
error = do_unmount(zhp, mountpoint, flags);
|
2011-03-07 18:10:20 +00:00
|
|
|
if (error != 0) {
|
2021-03-08 16:46:45 +00:00
|
|
|
int libzfs_err;
|
|
|
|
|
|
|
|
switch (error) {
|
|
|
|
case EBUSY:
|
|
|
|
libzfs_err = EZFS_BUSY;
|
|
|
|
break;
|
|
|
|
case EIO:
|
|
|
|
libzfs_err = EZFS_IO;
|
|
|
|
break;
|
|
|
|
case ENOENT:
|
|
|
|
libzfs_err = EZFS_NOENT;
|
|
|
|
break;
|
|
|
|
case ENOMEM:
|
|
|
|
libzfs_err = EZFS_NOMEM;
|
|
|
|
break;
|
|
|
|
case EPERM:
|
|
|
|
libzfs_err = EZFS_PERM;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
libzfs_err = EZFS_UMOUNTFAILED;
|
|
|
|
}
|
2021-11-30 18:36:36 +00:00
|
|
|
if (zhp) {
|
|
|
|
return (zfs_error_fmt(zhp->zfs_hdl, libzfs_err,
|
|
|
|
dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
|
|
|
|
mountpoint));
|
|
|
|
} else {
|
|
|
|
return (-1);
|
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmount the given filesystem.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
|
|
|
|
{
|
2009-01-15 21:59:39 +00:00
|
|
|
libzfs_handle_t *hdl = zhp->zfs_hdl;
|
|
|
|
struct mnttab entry;
|
2008-11-20 20:01:55 +00:00
|
|
|
char *mntpt = NULL;
|
2019-06-28 19:38:37 +00:00
|
|
|
boolean_t encroot, unmounted = B_FALSE;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2009-01-15 21:59:39 +00:00
|
|
|
/* check to see if we need to unmount the filesystem */
|
2008-11-20 20:01:55 +00:00
|
|
|
if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
|
2009-01-15 21:59:39 +00:00
|
|
|
libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* mountpoint may have come from a call to
|
|
|
|
* getmnt/getmntany if it isn't NULL. If it is NULL,
|
2009-01-15 21:59:39 +00:00
|
|
|
* we know it comes from libzfs_mnttab_find which can
|
|
|
|
* then get freed later. We strdup it to play it safe.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
|
|
|
if (mountpoint == NULL)
|
Fix "zfs destroy" when "sharenfs=on" is used
When using "zfs destroy" on a dataset that is using "sharenfs=on" and
has been automatically exported (by libzfs), the dataset will not be
automatically unexported as it should be. This workflow appears to have
been broken by this commit: 3fd3e56cfd543d7d7a1bf502bfc0db6e24139668
In that change, the "zfs_unmount" function was modified to use the
"mnt.mnt_special" field when determining the mount point that is being
unmounted, rather than "mnt.mnt_mountp".
As a result, when "mntpt" is passed into "zfs_unshare_proto", it's value
is now the dataset name rather than the mountpoint. Thus, when this
value is used with the "is_shared" function (via "zfs_unshare_proto") it
will not find a match (since that function assumes it'll be passed the
mountpoint) and incorrectly reports that the dataset is not shared.
This can be easily reproduced with the following commands:
$ sudo zpool create tank xvdb
$ sudo zfs create -o sharenfs=on tank/fish
$ sudo zfs destroy tank/fish
$ sudo zfs list -r tank
NAME USED AVAIL REFER MOUNTPOINT
tank 97.5K 7.27G 24K /tank
$ sudo exportfs
/tank/fish <world>
$ sudo cat /etc/dfs/sharetab
/tank/fish - nfs rw,crossmnt
At this point, the "tank/fish" filesystem doesn't exist, but it's still
listed as exported when looking at "exportfs" and "/etc/dfs/sharetab".
Also note, this change brings us back in-sync with the illumos code, as
it pertains to this one line; on illumos, "mnt.mnt_mountp" is used.
Reviewed by: loli10K <ezomori.nozomu@gmail.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Co-authored-by: George Wilson <george.wilson@delphix.com>
Signed-off-by: Prakash Surya <prakash.surya@delphix.com>
Issue #6143
Closes #7941
2018-09-21 15:47:42 +00:00
|
|
|
mntpt = zfs_strdup(hdl, entry.mnt_mountp);
|
2008-11-20 20:01:55 +00:00
|
|
|
else
|
2009-01-15 21:59:39 +00:00
|
|
|
mntpt = zfs_strdup(hdl, mountpoint);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unshare and unmount the filesystem
|
|
|
|
*/
|
2022-02-28 15:52:07 +00:00
|
|
|
if (zfs_unshare(zhp, mntpt, share_all_proto) != 0) {
|
2016-08-31 10:35:52 +00:00
|
|
|
free(mntpt);
|
2008-11-20 20:01:55 +00:00
|
|
|
return (-1);
|
2016-08-31 10:35:52 +00:00
|
|
|
}
|
2022-02-28 15:52:07 +00:00
|
|
|
zfs_commit_shares(NULL);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2021-07-15 18:31:00 +00:00
|
|
|
if (unmount_one(zhp, mntpt, flags) != 0) {
|
2008-11-20 20:01:55 +00:00
|
|
|
free(mntpt);
|
2022-02-28 15:52:07 +00:00
|
|
|
(void) zfs_share(zhp, NULL);
|
|
|
|
zfs_commit_shares(NULL);
|
2008-11-20 20:01:55 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2019-06-28 19:38:37 +00:00
|
|
|
|
2009-01-15 21:59:39 +00:00
|
|
|
libzfs_mnttab_remove(hdl, zhp->zfs_name);
|
2008-11-20 20:01:55 +00:00
|
|
|
free(mntpt);
|
2019-06-28 19:38:37 +00:00
|
|
|
unmounted = B_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the MS_CRYPT flag is provided we must ensure we attempt to
|
|
|
|
* unload the dataset's key regardless of whether we did any work
|
|
|
|
* to unmount it. We only do this for encryption roots.
|
|
|
|
*/
|
|
|
|
if ((flags & MS_CRYPT) != 0 &&
|
|
|
|
zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
|
|
|
|
zfs_refresh_properties(zhp);
|
|
|
|
|
|
|
|
if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0 &&
|
|
|
|
unmounted) {
|
|
|
|
(void) zfs_mount(zhp, NULL, 0);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encroot && zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
|
|
|
|
ZFS_KEYSTATUS_AVAILABLE &&
|
|
|
|
zfs_crypto_unload_key(zhp) != 0) {
|
|
|
|
(void) zfs_mount(zhp, NULL, 0);
|
|
|
|
return (-1);
|
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:56:00 +00:00
|
|
|
zpool_disable_volume_os(zhp->zfs_name);
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmount this filesystem and any children inheriting the mountpoint property.
|
|
|
|
* To do this, just act like we're changing the mountpoint property, but don't
|
|
|
|
* remount the filesystems afterwards.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
zfs_unmountall(zfs_handle_t *zhp, int flags)
|
|
|
|
{
|
|
|
|
prop_changelist_t *clp;
|
|
|
|
int ret;
|
|
|
|
|
2018-10-11 04:13:13 +00:00
|
|
|
clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
|
2019-06-28 19:38:37 +00:00
|
|
|
CL_GATHER_ITER_MOUNTED, flags);
|
2008-11-20 20:01:55 +00:00
|
|
|
if (clp == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
ret = changelist_prefix(clp);
|
|
|
|
changelist_free(clp);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2020-07-13 16:19:18 +00:00
|
|
|
/*
|
|
|
|
* Unshare a filesystem by mountpoint.
|
|
|
|
*/
|
2022-02-28 15:52:07 +00:00
|
|
|
static int
|
2020-07-13 16:19:18 +00:00
|
|
|
unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
|
2022-02-28 14:46:25 +00:00
|
|
|
enum sa_protocol proto)
|
2020-07-13 16:19:18 +00:00
|
|
|
{
|
2022-02-28 14:46:25 +00:00
|
|
|
int err = sa_disable_share(mountpoint, proto);
|
|
|
|
if (err != SA_OK)
|
2020-07-13 16:19:18 +00:00
|
|
|
return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
|
|
|
|
dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
|
|
|
|
name, sa_errorstr(err)));
|
2022-02-28 14:46:25 +00:00
|
|
|
|
2020-07-13 16:19:18 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Share the given filesystem according to the options in the specified
|
|
|
|
* protocol specific properties (sharenfs, sharesmb). We rely
|
|
|
|
* on "libshare" to do the dirty work for us.
|
|
|
|
*/
|
|
|
|
int
|
2022-02-28 15:52:07 +00:00
|
|
|
zfs_share(zfs_handle_t *zhp, const enum sa_protocol *proto)
|
2020-07-13 16:19:18 +00:00
|
|
|
{
|
|
|
|
char mountpoint[ZFS_MAXPROPLEN];
|
|
|
|
char shareopts[ZFS_MAXPROPLEN];
|
|
|
|
char sourcestr[ZFS_MAXPROPLEN];
|
2022-02-28 14:46:25 +00:00
|
|
|
const enum sa_protocol *curr_proto;
|
undocumented libzfs API changes broke "zfs list"
While OpenZFS does permit breaking changes to the libzfs API, we should
avoid these changes when reasonably possible, and take steps to mitigate
the impact to consumers when changes are necessary.
Commit e4288a8397bb1f made a libzfs API change that is especially
difficult for consumers because there is no change to the function
signatures, only to their behavior. Therefore, consumers can't notice
that there was a change at compile time. Also, the API change was
incompletely and incorrectly documented.
The commit message mentions `zfs_get_prop()` [sic], but all callers of
`get_numeric_property()` are impacted: `zfs_prop_get()`,
`zfs_prop_get_numeric()`, and `zfs_prop_get_int()`.
`zfs_prop_get_int()` always calls `get_numeric_property(src=NULL)`, so
it assumes that the filesystem is not mounted. This means that e.g.
`zfs_prop_get_int(ZFS_PROP_MOUNTED)` always returns 0.
The documentation says that to preserve the previous behavior, callers
should initialize `*src=ZPROP_SRC_NONE`, and some callers were changed
to do that. However, the existing behavior is actually preserved by
initializing `*src=ZPROP_SRC_ALL`, not `NONE`.
The code comment above `zfs_prop_get()` says, "src: ... NULL will be
treated as ZPROP_SRC_ALL.". However, the code actually treats NULL as
ZPROP_SRC_NONE. i.e. `zfs_prop_get(src=NULL)` assumes that the
filesystem is not mounted.
There are several existing calls which use `src=NULL` which are impacted
by the API change, most noticeably those used by `zfs list`, which now
assumes that filesystems are not mounted. For example,
`zfs list -o name,mounted` previously indicated whether a filesystem was
mounted or not, but now it always (incorrectly) indicates that the
filesystem is not mounted (`MOUNTED: no`). Similarly, properties that
are set at mount time are ignored. E.g. `zfs list -o name,atime` may
display an incorrect value if it was set at mount time.
To address these problems, this commit reverts commit e4288a8397bb1f:
"zfs get: don't lookup mount options when using "-s local""
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #11999
2021-05-06 18:24:56 +00:00
|
|
|
zprop_source_t sourcetype;
|
2020-07-13 16:19:18 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
if (proto == NULL)
|
|
|
|
proto = share_all_proto;
|
|
|
|
|
2020-07-13 16:19:18 +00:00
|
|
|
if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0))
|
|
|
|
return (0);
|
|
|
|
|
2022-02-28 14:46:25 +00:00
|
|
|
for (curr_proto = proto; *curr_proto != SA_NO_PROTOCOL; curr_proto++) {
|
2020-07-13 16:19:18 +00:00
|
|
|
/*
|
|
|
|
* Return success if there are no share options.
|
|
|
|
*/
|
|
|
|
if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
|
|
|
|
shareopts, sizeof (shareopts), &sourcetype, sourcestr,
|
|
|
|
ZFS_MAXPROPLEN, B_FALSE) != 0 ||
|
|
|
|
strcmp(shareopts, "off") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the 'zoned' property is set, then zfs_is_mountable()
|
|
|
|
* will have already bailed out if we are in the global zone.
|
|
|
|
* But local zones cannot be NFS servers, so we ignore it for
|
|
|
|
* local zones as well.
|
|
|
|
*/
|
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
err = sa_enable_share(zfs_get_name(zhp), mountpoint, shareopts,
|
2022-02-28 14:46:25 +00:00
|
|
|
*curr_proto);
|
2020-07-13 16:19:18 +00:00
|
|
|
if (err != SA_OK) {
|
|
|
|
return (zfs_error_fmt(zhp->zfs_hdl,
|
|
|
|
proto_table[*curr_proto].p_share_err,
|
|
|
|
dgettext(TEXT_DOMAIN, "cannot share '%s: %s'"),
|
|
|
|
zfs_get_name(zhp), sa_errorstr(err)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Check to see if the filesystem is currently shared.
|
|
|
|
*/
|
|
|
|
boolean_t
|
2022-02-28 15:52:07 +00:00
|
|
|
zfs_is_shared(zfs_handle_t *zhp, char **where,
|
|
|
|
const enum sa_protocol *proto)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2022-02-28 15:52:07 +00:00
|
|
|
char *mountpoint;
|
|
|
|
if (proto == NULL)
|
|
|
|
proto = share_all_proto;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
if (ZFS_IS_VOLUME(zhp))
|
|
|
|
return (B_FALSE);
|
2020-07-13 16:19:18 +00:00
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
if (!zfs_is_mounted(zhp, &mountpoint))
|
|
|
|
return (B_FALSE);
|
2020-07-13 16:19:18 +00:00
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
for (const enum sa_protocol *p = proto; *p != SA_NO_PROTOCOL; ++p)
|
|
|
|
if (sa_is_shared(mountpoint, *p)) {
|
|
|
|
if (where != NULL)
|
|
|
|
*where = mountpoint;
|
|
|
|
else
|
|
|
|
free(mountpoint);
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
2020-07-13 16:19:18 +00:00
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
free(mountpoint);
|
|
|
|
return (B_FALSE);
|
2020-07-13 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-02-28 15:52:07 +00:00
|
|
|
zfs_commit_shares(const enum sa_protocol *proto)
|
2020-07-13 16:19:18 +00:00
|
|
|
{
|
|
|
|
if (proto == NULL)
|
2022-02-28 15:52:07 +00:00
|
|
|
proto = share_all_proto;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
for (const enum sa_protocol *p = proto; *p != SA_NO_PROTOCOL; ++p)
|
|
|
|
sa_commit_shares(*p);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unshare the given filesystem.
|
|
|
|
*/
|
|
|
|
int
|
2022-02-28 15:52:07 +00:00
|
|
|
zfs_unshare(zfs_handle_t *zhp, const char *mountpoint,
|
2022-02-28 14:46:25 +00:00
|
|
|
const enum sa_protocol *proto)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2009-01-15 21:59:39 +00:00
|
|
|
libzfs_handle_t *hdl = zhp->zfs_hdl;
|
|
|
|
struct mnttab entry;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
if (proto == NULL)
|
|
|
|
proto = share_all_proto;
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
|
2009-01-15 21:59:39 +00:00
|
|
|
libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2022-03-18 17:40:13 +00:00
|
|
|
/* check to see if need to unmount the filesystem */
|
|
|
|
const char *mntpt = mountpoint ?: entry.mnt_mountp;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
for (const enum sa_protocol *curr_proto = proto;
|
2022-02-28 15:55:16 +00:00
|
|
|
*curr_proto != SA_NO_PROTOCOL; curr_proto++)
|
|
|
|
if (sa_is_shared(mntpt, *curr_proto) &&
|
|
|
|
unshare_one(hdl, zhp->zfs_name,
|
|
|
|
mntpt, *curr_proto) != 0)
|
2020-07-13 16:19:18 +00:00
|
|
|
return (-1);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same as zfs_unmountall(), but for NFS and SMB unshares.
|
|
|
|
*/
|
2022-02-28 15:52:07 +00:00
|
|
|
int
|
|
|
|
zfs_unshareall(zfs_handle_t *zhp, const enum sa_protocol *proto)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
|
|
|
prop_changelist_t *clp;
|
|
|
|
int ret;
|
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
if (proto == NULL)
|
|
|
|
proto = share_all_proto;
|
|
|
|
|
2008-12-03 20:09:06 +00:00
|
|
|
clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
|
2008-11-20 20:01:55 +00:00
|
|
|
if (clp == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
ret = changelist_unshare(clp, proto);
|
|
|
|
changelist_free(clp);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the mountpoint associated with the current dataset, if necessary.
|
|
|
|
* We only remove the underlying directory if:
|
|
|
|
*
|
|
|
|
* - The mountpoint is not 'none' or 'legacy'
|
|
|
|
* - The mountpoint is non-empty
|
|
|
|
* - The mountpoint is the default or inherited
|
|
|
|
* - The 'zoned' property is set, or we're in a local zone
|
|
|
|
*
|
|
|
|
* Any other directories we leave alone.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
remove_mountpoint(zfs_handle_t *zhp)
|
|
|
|
{
|
|
|
|
char mountpoint[ZFS_MAXPROPLEN];
|
|
|
|
zprop_source_t source;
|
|
|
|
|
2019-10-03 17:33:16 +00:00
|
|
|
if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
|
|
|
|
&source, 0))
|
2008-11-20 20:01:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (source == ZPROP_SRC_DEFAULT ||
|
|
|
|
source == ZPROP_SRC_INHERITED) {
|
|
|
|
/*
|
|
|
|
* Try to remove the directory, silently ignoring any errors.
|
|
|
|
* The filesystem may have since been removed or moved around,
|
|
|
|
* and this error isn't really useful to the administrator in
|
|
|
|
* any way.
|
|
|
|
*/
|
|
|
|
(void) rmdir(mountpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
/*
|
|
|
|
* Add the given zfs handle to the cb_handles array, dynamically reallocating
|
|
|
|
* the array if it is out of space.
|
|
|
|
*/
|
2010-08-26 21:24:34 +00:00
|
|
|
void
|
|
|
|
libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
|
|
|
|
{
|
|
|
|
if (cbp->cb_alloc == cbp->cb_used) {
|
|
|
|
size_t newsz;
|
2018-11-05 15:40:05 +00:00
|
|
|
zfs_handle_t **newhandles;
|
2010-08-26 21:24:34 +00:00
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
|
|
|
|
newhandles = zfs_realloc(zhp->zfs_hdl,
|
|
|
|
cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
|
|
|
|
newsz * sizeof (zfs_handle_t *));
|
|
|
|
cbp->cb_handles = newhandles;
|
2010-08-26 21:24:34 +00:00
|
|
|
cbp->cb_alloc = newsz;
|
|
|
|
}
|
|
|
|
cbp->cb_handles[cbp->cb_used++] = zhp;
|
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
/*
|
|
|
|
* Recursive helper function used during file system enumeration
|
|
|
|
*/
|
2008-11-20 20:01:55 +00:00
|
|
|
static int
|
2018-11-05 15:40:05 +00:00
|
|
|
zfs_iter_cb(zfs_handle_t *zhp, void *data)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2010-08-26 21:24:34 +00:00
|
|
|
get_all_cb_t *cbp = data;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2010-08-26 21:24:34 +00:00
|
|
|
if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
|
2008-11-20 20:01:55 +00:00
|
|
|
zfs_close(zhp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
|
|
|
|
zfs_close(zhp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
Native Encryption for ZFS on Linux
This change incorporates three major pieces:
The first change is a keystore that manages wrapping
and encryption keys for encrypted datasets. These
commands mostly involve manipulating the new
DSL Crypto Key ZAP Objects that live in the MOS. Each
encrypted dataset has its own DSL Crypto Key that is
protected with a user's key. This level of indirection
allows users to change their keys without re-encrypting
their entire datasets. The change implements the new
subcommands "zfs load-key", "zfs unload-key" and
"zfs change-key" which allow the user to manage their
encryption keys and settings. In addition, several new
flags and properties have been added to allow dataset
creation and to make mounting and unmounting more
convenient.
The second piece of this patch provides the ability to
encrypt, decyrpt, and authenticate protected datasets.
Each object set maintains a Merkel tree of Message
Authentication Codes that protect the lower layers,
similarly to how checksums are maintained. This part
impacts the zio layer, which handles the actual
encryption and generation of MACs, as well as the ARC
and DMU, which need to be able to handle encrypted
buffers and protected data.
The last addition is the ability to do raw, encrypted
sends and receives. The idea here is to send raw
encrypted and compressed data and receive it exactly
as is on a backup system. This means that the dataset
on the receiving system is protected using the same
user key that is in use on the sending side. By doing
so, datasets can be efficiently backed up to an
untrusted system without fear of data being
compromised.
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Jorgen Lundman <lundman@lundman.net>
Signed-off-by: Tom Caputi <tcaputi@datto.com>
Closes #494
Closes #5769
2017-08-14 17:36:48 +00:00
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
|
|
|
|
ZFS_KEYSTATUS_UNAVAILABLE) {
|
|
|
|
zfs_close(zhp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:22:48 +00:00
|
|
|
/*
|
|
|
|
* If this filesystem is inconsistent and has a receive resume
|
|
|
|
* token, we can not mount it.
|
|
|
|
*/
|
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
|
|
|
|
zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
|
|
|
|
NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
|
|
|
|
zfs_close(zhp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-08-26 21:24:34 +00:00
|
|
|
libzfs_add_handle(cbp, zhp);
|
2022-01-06 19:12:53 +00:00
|
|
|
if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) {
|
2010-08-26 21:24:34 +00:00
|
|
|
zfs_close(zhp);
|
|
|
|
return (-1);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2010-08-26 21:24:34 +00:00
|
|
|
return (0);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
/*
|
|
|
|
* Sort comparator that compares two mountpoint paths. We sort these paths so
|
|
|
|
* that subdirectories immediately follow their parents. This means that we
|
2018-10-20 21:48:39 +00:00
|
|
|
* effectively treat the '/' character as the lowest value non-nul char.
|
|
|
|
* Since filesystems from non-global zones can have the same mountpoint
|
|
|
|
* as other filesystems, the comparator sorts global zone filesystems to
|
|
|
|
* the top of the list. This means that the global zone will traverse the
|
|
|
|
* filesystem list in the correct order and can stop when it sees the
|
|
|
|
* first zoned filesystem. In a non-global zone, only the delegated
|
|
|
|
* filesystems are seen.
|
|
|
|
*
|
|
|
|
* An example sorted list using this comparator would look like:
|
2018-11-05 15:40:05 +00:00
|
|
|
*
|
|
|
|
* /foo
|
|
|
|
* /foo/bar
|
|
|
|
* /foo/bar/baz
|
|
|
|
* /foo/baz
|
|
|
|
* /foo.bar
|
2018-10-20 21:48:39 +00:00
|
|
|
* /foo (NGZ1)
|
|
|
|
* /foo (NGZ2)
|
2018-11-05 15:40:05 +00:00
|
|
|
*
|
|
|
|
* The mounting code depends on this ordering to deterministically iterate
|
|
|
|
* over filesystems in order to spawn parallel mount tasks.
|
|
|
|
*/
|
2018-10-20 21:48:39 +00:00
|
|
|
static int
|
2018-11-05 15:40:05 +00:00
|
|
|
mountpoint_cmp(const void *arga, const void *argb)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2018-11-05 15:40:05 +00:00
|
|
|
zfs_handle_t *const *zap = arga;
|
|
|
|
zfs_handle_t *za = *zap;
|
|
|
|
zfs_handle_t *const *zbp = argb;
|
|
|
|
zfs_handle_t *zb = *zbp;
|
2008-11-20 20:01:55 +00:00
|
|
|
char mounta[MAXPATHLEN];
|
|
|
|
char mountb[MAXPATHLEN];
|
2018-11-05 15:40:05 +00:00
|
|
|
const char *a = mounta;
|
|
|
|
const char *b = mountb;
|
2008-11-20 20:01:55 +00:00
|
|
|
boolean_t gota, gotb;
|
2018-10-20 21:48:39 +00:00
|
|
|
uint64_t zoneda, zonedb;
|
|
|
|
|
|
|
|
zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
|
|
|
|
zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
|
|
|
|
if (zoneda && !zonedb)
|
|
|
|
return (1);
|
|
|
|
if (!zoneda && zonedb)
|
|
|
|
return (-1);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
|
|
|
|
if (gota) {
|
|
|
|
verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
|
2008-11-20 20:01:55 +00:00
|
|
|
sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
|
2018-11-05 15:40:05 +00:00
|
|
|
}
|
|
|
|
gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
|
|
|
|
if (gotb) {
|
|
|
|
verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
|
2008-11-20 20:01:55 +00:00
|
|
|
sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
|
2018-11-05 15:40:05 +00:00
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
if (gota && gotb) {
|
|
|
|
while (*a != '\0' && (*a == *b)) {
|
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
if (*a == *b)
|
|
|
|
return (0);
|
|
|
|
if (*a == '\0')
|
|
|
|
return (-1);
|
|
|
|
if (*b == '\0')
|
|
|
|
return (1);
|
|
|
|
if (*a == '/')
|
|
|
|
return (-1);
|
|
|
|
if (*b == '/')
|
|
|
|
return (1);
|
|
|
|
return (*a < *b ? -1 : *a > *b);
|
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
if (gota)
|
|
|
|
return (-1);
|
|
|
|
if (gotb)
|
|
|
|
return (1);
|
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
/*
|
|
|
|
* If neither filesystem has a mountpoint, revert to sorting by
|
|
|
|
* dataset name.
|
|
|
|
*/
|
|
|
|
return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Fix race in parallel mount's thread dispatching algorithm
Strategy of parallel mount is as follows.
1) Initial thread dispatching is to select sets of mount points that
don't have dependencies on other sets, hence threads can/should run
lock-less and shouldn't race with other threads for other sets. Each
thread dispatched corresponds to top level directory which may or may
not have datasets to be mounted on sub directories.
2) Subsequent recursive thread dispatching for each thread from 1)
is to mount datasets for each set of mount points. The mount points
within each set have dependencies (i.e. child directories), so child
directories are processed only after parent directory completes.
The problem is that the initial thread dispatching in
zfs_foreach_mountpoint() can be multi-threaded when it needs to be
single-threaded, and this puts threads under race condition. This race
appeared as mount/unmount issues on ZoL for ZoL having different
timing regarding mount(2) execution due to fork(2)/exec(2) of mount(8).
`zfs unmount -a` which expects proper mount order can't unmount if the
mounts were reordered by the race condition.
There are currently two known patterns of input list `handles` in
`zfs_foreach_mountpoint(..,handles,..)` which cause the race condition.
1) #8833 case where input is `/a /a /a/b` after sorting.
The problem is that libzfs_path_contains() can't correctly handle an
input list with two same top level directories.
There is a race between two POSIX threads A and B,
* ThreadA for "/a" for test1 and "/a/b"
* ThreadB for "/a" for test0/a
and in case of #8833, ThreadA won the race. Two threads were created
because "/a" wasn't considered as `"/a" contains "/a"`.
2) #8450 case where input is `/ /var/data /var/data/test` after sorting.
The problem is that libzfs_path_contains() can't correctly handle an
input list containing "/".
There is a race between two POSIX threads A and B,
* ThreadA for "/" and "/var/data/test"
* ThreadB for "/var/data"
and in case of #8450, ThreadA won the race. Two threads were created
because "/var/data" wasn't considered as `"/" contains "/var/data"`.
In other words, if there is (at least one) "/" in the input list,
the initial thread dispatching must be single-threaded since every
directory is a child of "/", meaning they all directly or indirectly
depend on "/".
In both cases, the first non_descendant_idx() call fails to correctly
determine "path1-contains-path2", and as a result the initial thread
dispatching creates another thread when it needs to be single-threaded.
Fix a conditional in libzfs_path_contains() to consider above two.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed by: Sebastien Roy <sebastien.roy@delphix.com>
Signed-off-by: Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
Closes #8450
Closes #8833
Closes #8878
2019-07-09 16:31:46 +00:00
|
|
|
* Return true if path2 is a child of path1 or path2 equals path1 or
|
|
|
|
* path1 is "/" (path2 is always a child of "/").
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2018-11-05 15:40:05 +00:00
|
|
|
static boolean_t
|
|
|
|
libzfs_path_contains(const char *path1, const char *path2)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
Fix race in parallel mount's thread dispatching algorithm
Strategy of parallel mount is as follows.
1) Initial thread dispatching is to select sets of mount points that
don't have dependencies on other sets, hence threads can/should run
lock-less and shouldn't race with other threads for other sets. Each
thread dispatched corresponds to top level directory which may or may
not have datasets to be mounted on sub directories.
2) Subsequent recursive thread dispatching for each thread from 1)
is to mount datasets for each set of mount points. The mount points
within each set have dependencies (i.e. child directories), so child
directories are processed only after parent directory completes.
The problem is that the initial thread dispatching in
zfs_foreach_mountpoint() can be multi-threaded when it needs to be
single-threaded, and this puts threads under race condition. This race
appeared as mount/unmount issues on ZoL for ZoL having different
timing regarding mount(2) execution due to fork(2)/exec(2) of mount(8).
`zfs unmount -a` which expects proper mount order can't unmount if the
mounts were reordered by the race condition.
There are currently two known patterns of input list `handles` in
`zfs_foreach_mountpoint(..,handles,..)` which cause the race condition.
1) #8833 case where input is `/a /a /a/b` after sorting.
The problem is that libzfs_path_contains() can't correctly handle an
input list with two same top level directories.
There is a race between two POSIX threads A and B,
* ThreadA for "/a" for test1 and "/a/b"
* ThreadB for "/a" for test0/a
and in case of #8833, ThreadA won the race. Two threads were created
because "/a" wasn't considered as `"/a" contains "/a"`.
2) #8450 case where input is `/ /var/data /var/data/test` after sorting.
The problem is that libzfs_path_contains() can't correctly handle an
input list containing "/".
There is a race between two POSIX threads A and B,
* ThreadA for "/" and "/var/data/test"
* ThreadB for "/var/data"
and in case of #8450, ThreadA won the race. Two threads were created
because "/var/data" wasn't considered as `"/" contains "/var/data"`.
In other words, if there is (at least one) "/" in the input list,
the initial thread dispatching must be single-threaded since every
directory is a child of "/", meaning they all directly or indirectly
depend on "/".
In both cases, the first non_descendant_idx() call fails to correctly
determine "path1-contains-path2", and as a result the initial thread
dispatching creates another thread when it needs to be single-threaded.
Fix a conditional in libzfs_path_contains() to consider above two.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed by: Sebastien Roy <sebastien.roy@delphix.com>
Signed-off-by: Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
Closes #8450
Closes #8833
Closes #8878
2019-07-09 16:31:46 +00:00
|
|
|
return (strcmp(path1, path2) == 0 || strcmp(path1, "/") == 0 ||
|
|
|
|
(strstr(path2, path1) == path2 && path2[strlen(path1)] == '/'));
|
2018-11-05 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a mountpoint specified by idx in the handles array, find the first
|
|
|
|
* non-descendent of that mountpoint and return its index. Descendant paths
|
|
|
|
* start with the parent's path. This function relies on the ordering
|
|
|
|
* enforced by mountpoint_cmp().
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
|
|
|
|
{
|
|
|
|
char parent[ZFS_MAXPROPLEN];
|
|
|
|
char child[ZFS_MAXPROPLEN];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
|
|
|
|
sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
|
|
|
|
|
|
|
|
for (i = idx + 1; i < num_handles; i++) {
|
|
|
|
verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
|
|
|
|
sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
|
|
|
|
if (!libzfs_path_contains(parent, child))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct mnt_param {
|
|
|
|
libzfs_handle_t *mnt_hdl;
|
|
|
|
tpool_t *mnt_tp;
|
|
|
|
zfs_handle_t **mnt_zhps; /* filesystems to mount */
|
|
|
|
size_t mnt_num_handles;
|
|
|
|
int mnt_idx; /* Index of selected entry to mount */
|
|
|
|
zfs_iter_f mnt_func;
|
|
|
|
void *mnt_data;
|
|
|
|
} mnt_param_t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate and populate the parameter struct for mount function, and
|
|
|
|
* schedule mounting of the entry selected by idx.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
|
|
|
|
size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp)
|
|
|
|
{
|
|
|
|
mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
mnt_param->mnt_hdl = hdl;
|
|
|
|
mnt_param->mnt_tp = tp;
|
|
|
|
mnt_param->mnt_zhps = handles;
|
|
|
|
mnt_param->mnt_num_handles = num_handles;
|
|
|
|
mnt_param->mnt_idx = idx;
|
|
|
|
mnt_param->mnt_func = func;
|
|
|
|
mnt_param->mnt_data = data;
|
|
|
|
|
|
|
|
(void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the structure used to keep state of mounting or sharing operations
|
|
|
|
* during a call to zpool_enable_datasets().
|
|
|
|
*/
|
|
|
|
typedef struct mount_state {
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
2018-11-05 15:40:05 +00:00
|
|
|
* ms_mntstatus is set to -1 if any mount fails. While multiple threads
|
|
|
|
* could update this variable concurrently, no synchronization is
|
|
|
|
* needed as it's only ever set to -1.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2018-11-05 15:40:05 +00:00
|
|
|
int ms_mntstatus;
|
|
|
|
int ms_mntflags;
|
|
|
|
const char *ms_mntopts;
|
|
|
|
} mount_state_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
zfs_mount_one(zfs_handle_t *zhp, void *arg)
|
|
|
|
{
|
|
|
|
mount_state_t *ms = arg;
|
|
|
|
int ret = 0;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
2018-11-05 15:40:05 +00:00
|
|
|
* don't attempt to mount encrypted datasets with
|
|
|
|
* unloaded keys
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2018-11-05 15:40:05 +00:00
|
|
|
if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
|
|
|
|
ZFS_KEYSTATUS_UNAVAILABLE)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
|
|
|
|
ret = ms->ms_mntstatus = -1;
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
zfs_share_one(zfs_handle_t *zhp, void *arg)
|
|
|
|
{
|
|
|
|
mount_state_t *ms = arg;
|
|
|
|
int ret = 0;
|
|
|
|
|
2022-02-28 15:52:07 +00:00
|
|
|
if (zfs_share(zhp, NULL) != 0)
|
2018-11-05 15:40:05 +00:00
|
|
|
ret = ms->ms_mntstatus = -1;
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread pool function to mount one file system. On completion, it finds and
|
|
|
|
* schedules its children to be mounted. This depends on the sorting done in
|
|
|
|
* zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
|
|
|
|
* each descending from the previous) will have no parallelism since we always
|
|
|
|
* have to wait for the parent to finish mounting before we can schedule
|
|
|
|
* its children.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
zfs_mount_task(void *arg)
|
|
|
|
{
|
|
|
|
mnt_param_t *mp = arg;
|
|
|
|
int idx = mp->mnt_idx;
|
|
|
|
zfs_handle_t **handles = mp->mnt_zhps;
|
|
|
|
size_t num_handles = mp->mnt_num_handles;
|
|
|
|
char mountpoint[ZFS_MAXPROPLEN];
|
|
|
|
|
|
|
|
verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
|
|
|
|
sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
|
|
|
|
|
|
|
|
if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
|
2022-01-12 23:50:15 +00:00
|
|
|
goto out;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
2018-11-05 15:40:05 +00:00
|
|
|
* We dispatch tasks to mount filesystems with mountpoints underneath
|
|
|
|
* this one. We do this by dispatching the next filesystem with a
|
|
|
|
* descendant mountpoint of the one we just mounted, then skip all of
|
|
|
|
* its descendants, dispatch the next descendant mountpoint, and so on.
|
|
|
|
* The non_descendant_idx() function skips over filesystems that are
|
|
|
|
* descendants of the filesystem we just dispatched.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2018-11-05 15:40:05 +00:00
|
|
|
for (int i = idx + 1; i < num_handles;
|
|
|
|
i = non_descendant_idx(handles, num_handles, i)) {
|
|
|
|
char child[ZFS_MAXPROPLEN];
|
|
|
|
verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
|
|
|
|
child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
|
|
|
|
|
|
|
|
if (!libzfs_path_contains(mountpoint, child))
|
|
|
|
break; /* not a descendant, return */
|
|
|
|
zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
|
|
|
|
mp->mnt_func, mp->mnt_data, mp->mnt_tp);
|
|
|
|
}
|
2022-01-12 23:50:15 +00:00
|
|
|
|
|
|
|
out:
|
2018-11-05 15:40:05 +00:00
|
|
|
free(mp);
|
|
|
|
}
|
2009-02-18 20:51:31 +00:00
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
/*
|
|
|
|
* Issue the func callback for each ZFS handle contained in the handles
|
|
|
|
* array. This function is used to mount all datasets, and so this function
|
|
|
|
* guarantees that filesystems for parent mountpoints are called before their
|
|
|
|
* children. As such, before issuing any callbacks, we first sort the array
|
|
|
|
* of handles by mountpoint.
|
|
|
|
*
|
|
|
|
* Callbacks are issued in one of two ways:
|
|
|
|
*
|
|
|
|
* 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT
|
|
|
|
* environment variable is set, then we issue callbacks sequentially.
|
|
|
|
*
|
|
|
|
* 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT
|
|
|
|
* environment variable is not set, then we use a tpool to dispatch threads
|
|
|
|
* to mount filesystems in parallel. This function dispatches tasks to mount
|
|
|
|
* the filesystems at the top-level mountpoints, and these tasks in turn
|
|
|
|
* are responsible for recursively mounting filesystems in their children
|
|
|
|
* mountpoints.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
|
|
|
|
size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel)
|
|
|
|
{
|
2018-10-20 21:48:39 +00:00
|
|
|
zoneid_t zoneid = getzoneid();
|
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
/*
|
|
|
|
* The ZFS_SERIAL_MOUNT environment variable is an undocumented
|
|
|
|
* variable that can be used as a convenience to do a/b comparison
|
|
|
|
* of serial vs. parallel mounting.
|
|
|
|
*/
|
|
|
|
boolean_t serial_mount = !parallel ||
|
|
|
|
(getenv("ZFS_SERIAL_MOUNT") != NULL);
|
Native Encryption for ZFS on Linux
This change incorporates three major pieces:
The first change is a keystore that manages wrapping
and encryption keys for encrypted datasets. These
commands mostly involve manipulating the new
DSL Crypto Key ZAP Objects that live in the MOS. Each
encrypted dataset has its own DSL Crypto Key that is
protected with a user's key. This level of indirection
allows users to change their keys without re-encrypting
their entire datasets. The change implements the new
subcommands "zfs load-key", "zfs unload-key" and
"zfs change-key" which allow the user to manage their
encryption keys and settings. In addition, several new
flags and properties have been added to allow dataset
creation and to make mounting and unmounting more
convenient.
The second piece of this patch provides the ability to
encrypt, decyrpt, and authenticate protected datasets.
Each object set maintains a Merkel tree of Message
Authentication Codes that protect the lower layers,
similarly to how checksums are maintained. This part
impacts the zio layer, which handles the actual
encryption and generation of MACs, as well as the ARC
and DMU, which need to be able to handle encrypted
buffers and protected data.
The last addition is the ability to do raw, encrypted
sends and receives. The idea here is to send raw
encrypted and compressed data and receive it exactly
as is on a backup system. This means that the dataset
on the receiving system is protected using the same
user key that is in use on the sending side. By doing
so, datasets can be efficiently backed up to an
untrusted system without fear of data being
compromised.
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Jorgen Lundman <lundman@lundman.net>
Signed-off-by: Tom Caputi <tcaputi@datto.com>
Closes #494
Closes #5769
2017-08-14 17:36:48 +00:00
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
/*
|
|
|
|
* Sort the datasets by mountpoint. See mountpoint_cmp for details
|
|
|
|
* of how these are sorted.
|
|
|
|
*/
|
|
|
|
qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
|
|
|
|
|
|
|
|
if (serial_mount) {
|
|
|
|
for (int i = 0; i < num_handles; i++) {
|
|
|
|
func(handles[i], data);
|
|
|
|
}
|
|
|
|
return;
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-11-05 15:40:05 +00:00
|
|
|
* Issue the callback function for each dataset using a parallel
|
|
|
|
* algorithm that uses a thread pool to manage threads.
|
|
|
|
*/
|
|
|
|
tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There may be multiple "top level" mountpoints outside of the pool's
|
|
|
|
* root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
|
|
|
|
* these.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2018-11-05 15:40:05 +00:00
|
|
|
for (int i = 0; i < num_handles;
|
|
|
|
i = non_descendant_idx(handles, num_handles, i)) {
|
2018-10-20 21:48:39 +00:00
|
|
|
/*
|
|
|
|
* Since the mountpoints have been sorted so that the zoned
|
|
|
|
* filesystems are at the end, a zoned filesystem seen from
|
|
|
|
* the global zone means that we're done.
|
|
|
|
*/
|
|
|
|
if (zoneid == GLOBAL_ZONEID &&
|
|
|
|
zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
|
|
|
|
break;
|
2018-11-05 15:40:05 +00:00
|
|
|
zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
|
|
|
|
tp);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:40:05 +00:00
|
|
|
tpool_wait(tp); /* wait for all scheduled mounts to complete */
|
|
|
|
tpool_destroy(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mount and share all datasets within the given pool. This assumes that no
|
|
|
|
* datasets within the pool are currently mounted.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
|
|
|
|
{
|
|
|
|
get_all_cb_t cb = { 0 };
|
|
|
|
mount_state_t ms = { 0 };
|
|
|
|
zfs_handle_t *zfsp;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
|
|
|
|
ZFS_TYPE_DATASET)) == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gather all non-snapshot datasets within the pool. Start by adding
|
|
|
|
* the root filesystem for this pool to the list, and then iterate
|
|
|
|
* over all child filesystems.
|
|
|
|
*/
|
|
|
|
libzfs_add_handle(&cb, zfsp);
|
2022-01-06 19:12:53 +00:00
|
|
|
if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0)
|
2018-11-05 15:40:05 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mount all filesystems
|
|
|
|
*/
|
|
|
|
ms.ms_mntopts = mntopts;
|
|
|
|
ms.ms_mntflags = flags;
|
|
|
|
zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
|
|
|
|
zfs_mount_one, &ms, B_TRUE);
|
|
|
|
if (ms.ms_mntstatus != 0)
|
|
|
|
ret = ms.ms_mntstatus;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Share all filesystems that need to be shared. This needs to be
|
|
|
|
* a separate pass because libshare is not mt-safe, and so we need
|
|
|
|
* to share serially.
|
|
|
|
*/
|
|
|
|
ms.ms_mntstatus = 0;
|
|
|
|
zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
|
|
|
|
zfs_share_one, &ms, B_FALSE);
|
|
|
|
if (ms.ms_mntstatus != 0)
|
|
|
|
ret = ms.ms_mntstatus;
|
2020-07-13 16:19:18 +00:00
|
|
|
else
|
2022-02-28 15:52:07 +00:00
|
|
|
zfs_commit_shares(NULL);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
out:
|
2018-11-05 15:40:05 +00:00
|
|
|
for (int i = 0; i < cb.cb_used; i++)
|
2010-08-26 21:24:34 +00:00
|
|
|
zfs_close(cb.cb_handles[i]);
|
|
|
|
free(cb.cb_handles);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2021-07-15 18:31:00 +00:00
|
|
|
struct sets_s {
|
|
|
|
char *mountpoint;
|
|
|
|
zfs_handle_t *dataset;
|
|
|
|
};
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
static int
|
|
|
|
mountpoint_compare(const void *a, const void *b)
|
|
|
|
{
|
2021-07-15 18:31:00 +00:00
|
|
|
const struct sets_s *mounta = (struct sets_s *)a;
|
|
|
|
const struct sets_s *mountb = (struct sets_s *)b;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2021-07-15 18:31:00 +00:00
|
|
|
return (strcmp(mountb->mountpoint, mounta->mountpoint));
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unshare and unmount all datasets within the given pool. We don't want to
|
|
|
|
* rely on traversing the DSL to discover the filesystems within the pool,
|
|
|
|
* because this may be expensive (if not all of them are mounted), and can fail
|
2016-09-20 17:07:58 +00:00
|
|
|
* arbitrarily (on I/O error, for example). Instead, we walk /proc/self/mounts
|
|
|
|
* and gather all the filesystems that are currently mounted.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
|
|
|
|
{
|
|
|
|
int used, alloc;
|
2021-04-08 22:44:23 +00:00
|
|
|
FILE *mnttab;
|
2008-11-20 20:01:55 +00:00
|
|
|
struct mnttab entry;
|
|
|
|
size_t namelen;
|
2021-07-15 18:31:00 +00:00
|
|
|
struct sets_s *sets = NULL;
|
2008-11-20 20:01:55 +00:00
|
|
|
libzfs_handle_t *hdl = zhp->zpool_hdl;
|
|
|
|
int i;
|
|
|
|
int ret = -1;
|
|
|
|
int flags = (force ? MS_FORCE : 0);
|
|
|
|
|
|
|
|
namelen = strlen(zhp->zpool_name);
|
|
|
|
|
2021-04-08 22:44:23 +00:00
|
|
|
if ((mnttab = fopen(MNTTAB, "re")) == NULL)
|
2014-03-26 17:17:17 +00:00
|
|
|
return (ENOENT);
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
used = alloc = 0;
|
2021-04-08 22:44:23 +00:00
|
|
|
while (getmntent(mnttab, &entry) == 0) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Ignore non-ZFS entries.
|
|
|
|
*/
|
|
|
|
if (entry.mnt_fstype == NULL ||
|
|
|
|
strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore filesystems not within this pool.
|
|
|
|
*/
|
|
|
|
if (entry.mnt_mountp == NULL ||
|
|
|
|
strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
|
|
|
|
(entry.mnt_special[namelen] != '/' &&
|
|
|
|
entry.mnt_special[namelen] != '\0'))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point we've found a filesystem within our pool. Add
|
|
|
|
* it to our growing list.
|
|
|
|
*/
|
|
|
|
if (used == alloc) {
|
|
|
|
if (alloc == 0) {
|
2022-03-16 18:51:28 +00:00
|
|
|
sets = zfs_alloc(hdl,
|
|
|
|
8 * sizeof (struct sets_s));
|
2008-11-20 20:01:55 +00:00
|
|
|
alloc = 8;
|
|
|
|
} else {
|
2022-03-16 18:51:28 +00:00
|
|
|
sets = zfs_realloc(hdl, sets,
|
2021-07-15 18:31:00 +00:00
|
|
|
alloc * sizeof (struct sets_s),
|
2022-03-16 18:51:28 +00:00
|
|
|
alloc * 2 * sizeof (struct sets_s));
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
alloc *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:51:28 +00:00
|
|
|
sets[used].mountpoint = zfs_strdup(hdl, entry.mnt_mountp);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is allowed to fail, in case there is some I/O error. It
|
|
|
|
* is only used to determine if we need to remove the underlying
|
|
|
|
* mountpoint, so failure is not fatal.
|
|
|
|
*/
|
2021-07-15 18:31:00 +00:00
|
|
|
sets[used].dataset = make_dataset_handle(hdl,
|
|
|
|
entry.mnt_special);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
used++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, we have the entire list of filesystems, so sort it by
|
|
|
|
* mountpoint.
|
|
|
|
*/
|
2022-02-03 22:35:38 +00:00
|
|
|
if (used != 0)
|
|
|
|
qsort(sets, used, sizeof (struct sets_s), mountpoint_compare);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk through and first unshare everything.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < used; i++) {
|
2022-02-28 14:46:25 +00:00
|
|
|
for (enum sa_protocol i = 0; i < SA_PROTOCOL_COUNT; ++i) {
|
2022-02-28 15:52:07 +00:00
|
|
|
if (sa_is_shared(sets[i].mountpoint, i) &&
|
2021-07-15 18:31:00 +00:00
|
|
|
unshare_one(hdl, sets[i].mountpoint,
|
2022-02-28 14:46:25 +00:00
|
|
|
sets[i].mountpoint, i) != 0)
|
2008-11-20 20:01:55 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 15:52:07 +00:00
|
|
|
zfs_commit_shares(NULL);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now unmount everything, removing the underlying directories as
|
|
|
|
* appropriate.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < used; i++) {
|
2021-07-15 18:31:00 +00:00
|
|
|
if (unmount_one(sets[i].dataset, sets[i].mountpoint,
|
|
|
|
flags) != 0)
|
2008-11-20 20:01:55 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < used; i++) {
|
2021-07-15 18:31:00 +00:00
|
|
|
if (sets[i].dataset)
|
|
|
|
remove_mountpoint(sets[i].dataset);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:56:00 +00:00
|
|
|
zpool_disable_datasets_os(zhp, force);
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
ret = 0;
|
|
|
|
out:
|
2021-04-08 22:44:23 +00:00
|
|
|
(void) fclose(mnttab);
|
2008-11-20 20:01:55 +00:00
|
|
|
for (i = 0; i < used; i++) {
|
2021-07-15 18:31:00 +00:00
|
|
|
if (sets[i].dataset)
|
|
|
|
zfs_close(sets[i].dataset);
|
|
|
|
free(sets[i].mountpoint);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2021-07-15 18:31:00 +00:00
|
|
|
free(sets);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|