Fix ASSERT in zfs_receive_one()

This commit fixes the following ASSERT in zfs_receive_one() when
receiving a send stream from a root dataset with the "-e" option:

    $ sudo zfs snap source@snap
    $ sudo zfs send source@snap | sudo zfs recv -e destination/recv
    chopprefix > drrb->drr_toname
    ASSERT at libzfs_sendrecv.c:3804:zfs_receive_one()

Reviewed-by: Tom Caputi <tcaputi@datto.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed by: Paul Dagnelie <pcd@delphix.com>
Signed-off-by: loli10K <ezomori.nozomu@gmail.com>
Closes #8121
This commit is contained in:
LOLi 2018-12-04 18:38:55 +01:00 committed by Brian Behlendorf
parent 7c9a42921e
commit bdbd5477bc
5 changed files with 128 additions and 5 deletions

View File

@ -4240,10 +4240,21 @@ zfs_do_receive(int argc, char **argv)
}
break;
case 'd':
if (flags.istail) {
(void) fprintf(stderr, gettext("invalid option "
"combination: -d and -e are mutually "
"exclusive\n"));
usage(B_FALSE);
}
flags.isprefix = B_TRUE;
break;
case 'e':
flags.isprefix = B_TRUE;
if (flags.isprefix) {
(void) fprintf(stderr, gettext("invalid option "
"combination: -d and -e are mutually "
"exclusive\n"));
usage(B_FALSE);
}
flags.istail = B_TRUE;
break;
case 'n':
@ -4279,6 +4290,10 @@ zfs_do_receive(int argc, char **argv)
argc -= optind;
argv += optind;
/* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
if (flags.istail)
flags.isprefix = B_TRUE;
/* check number of arguments */
if (argc < 1) {
(void) fprintf(stderr, gettext("missing snapshot argument\n"));

View File

@ -3801,8 +3801,9 @@ zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
}
ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
ASSERT(chopprefix > drrb->drr_toname);
ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
strchr(sendfs, '/') == NULL);
ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
chopprefix[0] == '\0');

View File

@ -206,7 +206,7 @@ tests = ['zfs_receive_001_pos', 'zfs_receive_002_pos', 'zfs_receive_003_pos',
'zfs_receive_013_pos', 'zfs_receive_014_pos', 'zfs_receive_015_pos',
'receive-o-x_props_override', 'zfs_receive_from_encrypted',
'zfs_receive_to_encrypted', 'zfs_receive_raw',
'zfs_receive_raw_incremental']
'zfs_receive_raw_incremental', 'zfs_receive_-e']
tags = ['functional', 'cli_root', 'zfs_receive']
[tests/functional/cli_root/zfs_remap]

View File

@ -21,4 +21,5 @@ dist_pkgdata_SCRIPTS = \
zfs_receive_from_encrypted.ksh \
zfs_receive_to_encrypted.ksh \
zfs_receive_raw.ksh \
zfs_receive_raw_incremental.ksh
zfs_receive_raw_incremental.ksh \
zfs_receive_-e.ksh

View File

@ -0,0 +1,106 @@
#!/bin/ksh -p
#
# CDDL HEADER START
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# CDDL HEADER END
#
#
# Copyright 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
#
. $STF_SUITE/include/libtest.shlib
#
# DESCRIPTION:
# ZFS receive '-e' option can be used to discard all but the last element of
# the sent snapshot's file system name
#
# STRATEGY:
# 1. Create a filesystem with children and snapshots
# 2. Verify 'zfs receive -e' rejects invalid options
# 3. Verify 'zfs receive -e' can receive the root dataset
# 4. Verify 'zfs receive -e' can receive a replication stream
# 5. Verify 'zfs receive -e' can receive an incremental replication stream
#
verify_runnable "both"
function cleanup
{
destroy_pool "$poolname"
log_must rm -f "$vdevfile"
log_must rm -f "$streamfile"
}
log_assert "ZFS receive '-e' option can be used to discard all but the last"\
"element of the sent snapshot's file system name"
log_onexit cleanup
poolname="$TESTPOOL-zfsrecv"
recvfs="$poolname/recv"
vdevfile="$TEST_BASE_DIR/vdevfile.$$"
streamfile="$TEST_BASE_DIR/streamfile.$$"
#
# 1. Create a filesystem with children and snapshots
# NOTE: set "mountpoint=none" just to speed up the test process
#
log_must truncate -s $MINVDEVSIZE "$vdevfile"
log_must zpool create -O mountpoint=none "$poolname" "$vdevfile"
log_must zfs create -p "$poolname/fs/a/b"
log_must zfs create "$recvfs"
log_must zfs snapshot -r "$poolname@full"
log_must zfs snapshot -r "$poolname@incr"
#
# 2. Verify 'zfs receive -e' rejects invalid options
#
log_must eval "zfs send $poolname/fs@full > $streamfile"
log_mustnot eval "zfs receive -e < $streamfile"
log_mustnot eval "zfs receive -e $recvfs@snap < $streamfile"
log_mustnot eval "zfs receive -e $recvfs/1/2/3 < $streamfile"
log_mustnot eval "zfs receive -A -e $recvfs < $streamfile"
log_mustnot eval "zfs receive -e -d $recvfs < $streamfile"
#
# 3. 'zfs receive -e' can receive the root dataset
#
recvfs_rootds="$recvfs/rootds"
log_must zfs create "$recvfs_rootds"
log_must eval "zfs send $poolname@full > $streamfile"
log_must eval "zfs receive -e $recvfs_rootds < $streamfile"
log_must datasetexists "$recvfs_rootds/$poolname"
log_must snapexists "$recvfs_rootds/$poolname@full"
#
# 4. 'zfs receive -e' can receive a replication stream
#
recvfs_fs="$recvfs/fs"
log_must zfs create "$recvfs_fs"
log_must eval "zfs send -R $poolname/fs/a@full > $streamfile"
log_must eval "zfs receive -e $recvfs_fs < $streamfile"
log_must datasetexists "$recvfs_fs/a"
log_must datasetexists "$recvfs_fs/a/b"
log_must snapexists "$recvfs_fs/a@full"
log_must snapexists "$recvfs_fs/a/b@full"
#
# 5. 'zfs receive -e' can receive an incremental replication stream
#
log_must eval "zfs send -R -i full $poolname/fs/a@incr > $streamfile"
log_must eval "zfs receive -e $recvfs_fs < $streamfile"
log_must snapexists "$recvfs_fs/a@incr"
log_must snapexists "$recvfs_fs/a/b@incr"
log_pass "ZFS receive '-e' discards all but the last element of the sent"\
"snapshot's file system name as expected"