]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/zfs-tests/tests/functional/cli_root/zdb/zdb_object_range_pos.ksh
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / tests / zfs-tests / tests / functional / cli_root / zdb / zdb_object_range_pos.ksh
1 #!/bin/ksh -p
2 #
3 # CDDL HEADER START
4 #
5 # This file and its contents are supplied under the terms of the
6 # Common Development and Distribution License ("CDDL"), version 1.0.
7 # You may only use this file in accordance with the terms of version
8 # 1.0 of the CDDL.
9 #
10 # A full copy of the text of the CDDL should have accompanied this
11 # source.  A copy of the CDDL is also available via the Internet at
12 # http://www.illumos.org/license/CDDL.
13 #
14 # CDDL HEADER END
15 #
16 # Copyright (c) 2020 Lawrence Livermore National Security, LLC.
17
18 . $STF_SUITE/include/libtest.shlib
19
20 #
21 # Description:
22 # Object range parameters passed to zdb -dd work correctly.
23 #
24 # Strategy:
25 # 1. Create a pool
26 # 2. Create some files
27 # 3. Run zdb -dd with assorted object range arguments and verify output
28
29 function cleanup
30 {
31         datasetexists $TESTPOOL && destroy_pool $TESTPOOL
32 }
33
34 #
35 # Print objects in @dataset with identifiers greater than or equal to
36 # @begin and less than or equal to @end, without using object range
37 # parameters.
38 #
39 function get_object_list_range
40 {
41         dataset=$1
42         begin=$2
43         end=$3
44         get_object_list $dataset |
45         while read line; do
46                 obj=$(echo $line | awk '{print $1}')
47                 if [[ $obj -ge $begin && $obj -le $end ]] ; then
48                         echo "$line"
49                 elif [[ $obj -gt $end ]] ; then
50                         break
51                 fi
52         done
53 }
54
55 #
56 # Print just the list of objects from 'zdb -dd' with leading whitespace
57 # trimmed, discarding other zdb output, sorted by object identifier.
58 # Caller must pass in the dataset argument at minimum.
59 #
60 function get_object_list
61 {
62         zdb -P -dd $@ 2>/dev/null |
63         egrep "^ +-?([0-9]+ +){7}" |
64         sed 's/^[[:space:]]*//' |
65         sort -n
66 }
67
68 log_assert "Verify zdb -dd object range arguments work correctly."
69 log_onexit cleanup
70 verify_runnable "both"
71 verify_disk_count "$DISKS" 2
72 default_mirror_setup_noexit $DISKS
73
74 for x in $(seq 0 7); do
75         touch $TESTDIR/file$x
76         mkdir $TESTDIR/dir$x
77 done
78
79 log_must zpool sync
80
81 # Get list of all objects, but filter out user/group objects which don't
82 # appear when using object or object range arguments
83 all_objects=$(get_object_list $TESTPOOL/$TESTFS | grep -v 'used$')
84
85 # Range 0:-1 gets all objects
86 expected=$all_objects
87 actual=$(get_object_list $TESTPOOL/$TESTFS 0:-1)
88 log_must test "\n$actual\n" == "\n$expected\n"
89
90 # Range 0:-1:A gets all objects
91 expected=$all_objects
92 actual=$(get_object_list $TESTPOOL/$TESTFS 0:-1:A)
93 log_must test "\n$actual\n" == "\n$expected\n"
94
95 # Range 0:-1:f must output all file objects
96 expected=$(grep "ZFS plain file" <<< $all_objects)
97 actual=$(get_object_list $TESTPOOL/$TESTFS 0:-1:f)
98 log_must test "\n$actual\n" == "\n$expected\n"
99
100 # Range 0:-1:d must output all directory objects
101 expected=$(grep "ZFS directory" <<< $all_objects)
102 actual=$(get_object_list $TESTPOOL/$TESTFS 0:-1:d)
103 log_must test "\n$actual\n" == "\n$expected\n"
104
105 # Range 0:-1:df must output all directory and file objects
106 expected=$(grep -e "ZFS directory" -e "ZFS plain file" <<< $all_objects)
107 actual=$(get_object_list $TESTPOOL/$TESTFS 0:-1:df)
108 log_must test "\n$actual\n" == "\n$expected\n"
109
110 # Range 0:-1:A-f-d must output all non-files and non-directories
111 expected=$(grep -v -e "ZFS plain file" -e "ZFS directory" <<< $all_objects)
112 actual=$(get_object_list $TESTPOOL/$TESTFS 0:-1:A-f-d)
113 log_must test "\n$actual\n" == "\n$expected\n"
114
115 # Specifying multiple ranges works
116 set -A obj_ids $(ls -i $TESTDIR | awk '{print $1}' | sort -n)
117 start1=${obj_ids[0]}
118 end1=${obj_ids[5]}
119 start2=${obj_ids[8]}
120 end2=${obj_ids[13]}
121 expected=$(get_object_list_range $TESTPOOL/$TESTFS $start1 $end1;
122     get_object_list_range $TESTPOOL/$TESTFS $start2 $end2)
123 actual=$(get_object_list $TESTPOOL/$TESTFS $start1:$end1 $start2:$end2)
124 log_must test "\n$actual\n" == "\n$expected\n"
125
126 # Combining ranges with individual object IDs works
127 expected=$(get_object_list_range $TESTPOOL/$TESTFS $start1 $end1;
128     get_object_list $TESTPOOL/$TESTFS $start2 $end2)
129 actual=$(get_object_list $TESTPOOL/$TESTFS $start1:$end1 $start2 $end2)
130 log_must test "\n$actual\n" == "\n$expected\n"
131
132 # Hex conversion must work for ranges and individual object identifiers
133 # (this test uses expected result from previous test).
134 start1_hex=$(printf "0x%x" $start1)
135 end1_hex=$(printf "0x%x" $end1)
136 start2_hex=$(printf "0x%x" $start2)
137 end2_hex=$(printf "0x%x" $end2)
138 actual=$(get_object_list $TESTPOOL/$TESTFS $start1_hex:$end1_hex \
139     $start2_hex $end2_hex)
140 log_must test "\n$actual\n" == "\n$expected\n"
141
142 # Specifying individual object IDs works
143 objects="$start1 $end1 $start2 $end2"
144 expected="$objects"
145 actual=$(get_object_list $TESTPOOL/$TESTFS $objects | awk '{print $1}' | xargs)
146 log_must test "$actual" == "$expected"
147
148 # Get all objects in the meta-objset to test m (spacemap) and z (zap) flags
149 all_mos_objects=$(get_object_list $TESTPOOL 0:-1)
150
151 # Range 0:-1:m must output all space map objects
152 expected=$(grep "SPA space map" <<< $all_mos_objects)
153 actual=$(get_object_list $TESTPOOL 0:-1:m)
154 log_must test "\n$actual\n" == "\n$expected\n"
155
156 # Range 0:-1:z must output all zap objects
157 expected=$(grep "zap" <<< $all_mos_objects)
158 actual=$(get_object_list $TESTPOOL 0:-1:z)
159 log_must test "\n$actual\n" == "\n$expected\n"
160
161 # Range 0:-1:A-m-z must output all non-space maps and non-zaps
162 expected=$(grep -v -e "zap" -e "SPA space map" <<< $all_mos_objects)
163 actual=$(get_object_list $TESTPOOL 0:-1:A-m-z)
164 log_must test "\n$actual\n" == "\n$expected\n"
165
166 # Range 0:-1:mz must output all space maps and zaps
167 expected=$(grep -e "SPA space map" -e "zap" <<< $all_mos_objects)
168 actual=$(get_object_list $TESTPOOL 0:-1:mz)
169 log_must test "\n$actual\n" == "\n$expected\n"
170
171 log_pass "zdb -dd object range arguments work correctly"