1#!/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
15# Copyright (c) 2020 by Datto Inc. All rights reserved.
16#
17
18#
19# DESCRIPTION
20# verify 'zfs snapshot <list of snapshots>' works correctly
21#
22# STRATEGY
23# 1. Create multiple datasets
24# 2. Create multiple snapshots with a list of valid and invalid
25#    snapshot names
26# 3. Verify the valid snapshot creation
27# 4. Verify creation of snapshots report the correct numbers by
28#    performing a snapshot directory listing
29
30. $STF_SUITE/include/libtest.shlib
31
32ZFS_MAX_DATASET_NAME_LEN=256
33
34function cleanup
35{
36	for ds in $datasets; do
37		datasetexists $ds && log_must zfs destroy -r $ds
38	done
39	zfs destroy -r $TESTPOOL/TESTFS4
40	zfs destroy -r $TESTPOOL/TESTFS5
41}
42datasets="$TESTPOOL/$TESTFS1 $TESTPOOL/$TESTFS2
43    $TESTPOOL/$TESTFS3"
44
45# We subtract 3 for slash (/), at (@), and the terminating nul (\0)
46SNAPSHOT_XXX=$(printf 'x%.0s' \
47    {1..$(($ZFS_MAX_DATASET_NAME_LEN - ${#TESTPOOL} - ${#TESTFS1} - 3))})
48
49invalid_args=("$TESTPOOL/$TESTFS1@now $TESTPOOL/$TESTFS2@now \
50    $TESTPOOL/$TESTFS@blah?" "$TESTPOOL/$TESTFS1@blah* \
51    $TESTPOOL/$TESTFS2@blah? $TESTPOOL/$TESTFS3@blah%" \
52    "$TESTPOOL/$TESTFS1@x$SNAPSHOT_XXX $TESTPOOL/$TESTFS2@300 \
53    $TESTPOOL/$TESTFS3@300")
54
55valid_args=("$TESTPOOL/$TESTFS1@snap $TESTPOOL/$TESTFS2@snap \
56    $TESTPOOL/$TESTFS3@snap" "$TESTPOOL/$TESTFS1@$SNAPSHOT_XXX \
57    $TESTPOOL/$TESTFS2@2 $TESTPOOL/$TESTFS3@s")
58
59log_assert "verify zfs supports multiple consistent snapshots"
60log_onexit cleanup
61typeset -i i=1
62test_data=$STF_SUITE/tests/functional/cli_root/zpool_upgrade/blockfiles/*.bz2
63
64log_note "destroy a list of valid snapshots"
65for ds in $datasets; do
66	log_must zfs create $ds
67	log_must cp -r $test_data /$ds
68done
69i=0
70while (( i < ${#valid_args[*]} )); do
71	log_must zfs snapshot ${valid_args[i]}
72	for token in ${valid_args[i]}; do
73		log_must snapexists $token && \
74		    log_must zfs destroy $token
75	done
76	((i = i + 1))
77done
78log_note "destroy a list of invalid snapshots"
79i=0
80while (( i < ${#invalid_args[*]} )); do
81	log_mustnot zfs snapshot ${invalid_args[i]}
82	for token in ${invalid_args[i]}; do
83		log_mustnot snapexists $token
84	done
85	((i = i + 1))
86done
87log_note "verify multiple snapshot transaction group"
88txg_group=$(zdb -Pd $TESTPOOL | grep snap | awk '{print $7}')
89for i in 1 2 3; do
90	txg_tag=$(echo "$txg_group" | nawk -v j=$i 'FNR == j {print}')
91	[[ $txg_tag != $(echo "$txg_group" | \
92	    nawk -v j=$i 'FNR == j {print}') ]] \
93	    && log_fail "snapshots belong to different transaction groups"
94done
95log_note "verify snapshot contents"
96for ds in $datasets; do
97	diff -q -r /$ds /$ds/.zfs/snapshot/snap > /dev/null 2>&1
98	if [[ $? -eq 1 ]]; then
99		log_fail "snapshot contents are different from" \
100		    "the filesystem"
101	fi
102done
103
104# We subtract 3 + 7 + 7 + 1 = 18 for three slashes (/), strlen("TESTFSA") == 7,
105# strlen("TESTFSB") == 7, and the terminating nul (\0)
106DATASET_XXX=$(printf 'x%.0s' \
107    {1..$(($ZFS_MAX_DATASET_NAME_LEN - ${#TESTPOOL} - ${#TESTFS3} - 18))})
108
109log_note "verify multiple snapshot with -r option"
110log_must zfs create $TESTPOOL/TESTFS4
111log_must zfs create -p $TESTPOOL/$TESTFS3/TESTFSA$DATASET_XXX/TESTFSB
112log_mustnot zfs snapshot -r $TESTPOOL/$TESTFS1@snap1 $TESTPOOL/$TESTFS2@snap1 \
113        $TESTPOOL/$TESTFS3@snap1 $TESTPOOL/TESTFS4@snap1
114log_must zfs rename $TESTPOOL/$TESTFS3/TESTFSA$DATASET_XXX \
115    $TESTPOOL/$TESTFS3/TESTFSA
116log_must zfs snapshot -r $TESTPOOL/$TESTFS1@snap1 $TESTPOOL/$TESTFS2@snap1 \
117        $TESTPOOL/$TESTFS3@snap1 $TESTPOOL/TESTFS4@snap1
118
119MYTEST="TESTFS5"
120ITERATIONS=10
121NUM_SNAPS=5
122for x in {1..$ITERATIONS}; do
123	log_must zfs create $TESTPOOL/$MYTEST
124	for y in {1..$NUM_SNAPS}; do
125		log_must zfs snapshot $TESTPOOL/$MYTEST@$y
126	done;
127	n=$(ls -1 /$TESTPOOL/$MYTEST/.zfs/snapshot | wc -l)
128	verify_eq $n $NUM_SNAPS "count"
129	zfs destroy -r $TESTPOOL/$MYTEST;
130done;
131
132log_pass "zfs multiple snapshot verified correctly"
133