3 blkzone=$(type -p blkzone 2>/dev/null)
4 sg_inq=$(type -p sg_inq 2>/dev/null)
5 zbc_report_zones=$(type -p zbc_report_zones 2>/dev/null)
6 zbc_reset_zone=$(type -p zbc_reset_zone 2>/dev/null)
7 zbc_info=$(type -p zbc_info 2>/dev/null)
8 if [ -z "${blkzone}" ] &&
9 { [ -z "${zbc_report_zones}" ] || [ -z "${zbc_reset_zone}" ]; }; then
10 echo "Error: neither blkzone nor zbc_report_zones is available"
14 if [ -n "${use_libzbc}" ] &&
15 { [ -z "${zbc_report_zones}" ] || [ -z "${zbc_reset_zone}" ] ||
16 [ -z "${zbc_info}" ]; }; then
17 echo "Error: zbc_report_zones, or zbc_reset_zone or zbc_info is not available"
18 echo "Error: reinstall libzbc tools"
22 blkzone_reports_capacity() {
25 [[ -n "${blkzone}" ]] &&
26 "${blkzone}" report -c 1 -o 0 "${dev}" | grep -q 'cap '
29 # Whether or not $1 (/dev/...) is a NVME ZNS device.
33 s=/sys/block/$(basename "${1}")/device/subsystem
35 if [[ ! -h "${s}" || $(realpath "${s}") != /sys/class/nvme ]]; then
39 [[ $(</sys/block/$(basename "${1}")/queue/zoned) == host-managed ]]
42 # Whether or not $1 (/dev/...) is a null_blk device with zone capacity smaller
44 is_nullb_with_zone_cap() {
47 f=/sys/kernel/config/nullb/$(basename "${1}")
48 [[ -r "${f}/zone_capacity" &&
49 $(<"${f}/zone_capacity") -lt $(<"${f}/zone_size") ]]
52 # Check if blkzone is available and suitable for the test target device. If not
53 # available, print error message and return 1. Otherwise return 0.
57 # If the device supports zone capacity, mandate zone capacity report by
59 if (is_nvme_zns "${dev}" || is_nullb_with_zone_cap "${dev}") &&
60 ! blkzone_reports_capacity "${dev}"; then
61 echo "Error: blkzone does not report zone capacity"
62 echo "Error: install latest util-linux with blkzone"
67 # Reports the starting sector and length of the first sequential zone of device
69 first_sequential_zone() {
72 if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
73 ${blkzone} report "$dev" |
74 sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*zcond:\(14\|[[:blank:]][0-4]\)(.*type:[[:blank:]]\([2]\)(.*/\1 \2/p' |
76 read -r starting_sector length &&
77 # Convert from hex to decimal
78 echo $((starting_sector)) $((length))
81 ${zbc_report_zones} "$dev" |
82 sed -n 's/^Zone [0-9]*: type 0x2 .*,[[:blank:]]cond[[:blank:]]0x[0-4e][[:blank:]].*, sector \([0-9]*\), \([0-9]*\) sectors.*$/\1 \2/p' |
87 # Reports the summed zone capacity of $1 number of zones starting from offset $2
89 total_zone_capacity() {
91 local sector=$(($2 / 512))
96 if [ -z "$is_zbd" ]; then
97 # For regular block devices, handle zone size as zone capacity.
98 echo $((zone_size * nr_zones))
102 if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
103 if blkzone_reports_capacity "${dev}"; then
104 grep_str='cap \K[0-9a-zA-Z]*'
106 # If zone capacity is not reported, refer zone length.
107 grep_str='len \K[0-9a-zA-Z]*'
110 capacity=$((capacity + num))
111 done < <(${blkzone} report -c "$nr_zones" -o "$sector" "$dev" |
112 grep -Po "${grep_str}")
114 # ZBC devices do not have zone capacity. Use zone size.
116 capacity=$((capacity + num))
117 done < <(${zbc_report_zones} -nz "$nr_zones" -start "$sector" \
118 "$dev" | grep -Po 'sector [0-9]*, \K[0-9]*')
121 echo $((capacity * 512))
124 # Reports the starting sector and length of the first zone of device $1
125 # that is not in offline (or similar) condition.
126 first_online_zone() {
129 if [ -z "$is_zbd" ]; then
134 if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
135 ${blkzone} report "$dev" |
136 sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*zcond:\(14\|[[:blank:]][0-4]\)(.*type:[[:blank:]][12](.*/\1/p' |
139 read -r starting_sector &&
140 # Convert from hex to decimal
141 echo $((starting_sector))
144 ${zbc_report_zones} "$dev" |
145 sed -n 's/^Zone[[:blank:]][0-9]*:[[:blank:]]type[[:blank:]]0x[12][[:blank:]].*,[[:blank:]]cond[[:blank:]]0x[0-4e][[:blank:]].*,[[:blank:]]sector[[:blank:]]\([0-9]*\),.*$/\1/p' |
150 # Reports the starting sector and length of the last zone of device $1
151 # that is not in offline (or similar) condition.
155 if [ -z "$is_zbd" ]; then
160 if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
161 ${blkzone} report "$dev" |
162 sed -n 's/^[[:blank:]]*start:[[:blank:]]\([0-9a-zA-Z]*\),[[:blank:]]len[[:blank:]]\([0-9a-zA-Z]*\),.*zcond:\(14\|[[:blank:]][0-4]\)(.*type:[[:blank:]][12](.*/\1/p' |
165 read -r starting_sector &&
166 # Convert from hex to decimal
167 echo $((starting_sector))
170 ${zbc_report_zones} "$dev" |
171 sed -n 's/^Zone[[:blank:]][0-9]*:[[:blank:]]type[[:blank:]]0x[12][[:blank:]].*,[[:blank:]]cond[[:blank:]]0x[0-4e][[:blank:]].*,[[:blank:]]sector[[:blank:]]\([0-9]*\),.*$/\1/p' |
179 if [ -n "${sg_inq}" ] && [ ! -n "${use_libzbc}" ]; then
180 if ! ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" \
181 > /dev/null 2>&1; then
182 # Non scsi device such as null_blk can not return max open zones.
186 ${sg_inq} -e --page=0xB6 --len=20 --hex "$dev" | tail -1 |
188 read -r offset b0 b1 b2 b3 trailer || return $?
189 # Convert from hex to decimal
190 max_nr_open_zones=$((0x${b0}))
191 max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b1}))
192 max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b2}))
193 max_nr_open_zones=$((max_nr_open_zones * 256 + 0x${b3}))
194 echo ${max_nr_open_zones}
198 ${zbc_report_zones} "$dev" |
199 sed -n 's/^[[:blank:]]*Maximum number of open sequential write required zones:[[:blank:]]*//p'
206 [[ -z "$(${zbc_info} "$dev" | grep "is not a zoned block device")" ]]
209 zbc_logical_block_size() {
213 grep "logical blocks" |
214 sed -n 's/^[[:blank:]]*[0-9]* logical blocks of[[:blank:]]*//p' |
222 grep "512-bytes sectors" |
223 sed -e 's/[[:blank:]]*\([0-9]*\)512-bytes sectors.*/\1/'
226 # Reset the write pointer of one zone on device $1 at offset $2. The offset
227 # must be specified in units of 512 byte sectors. Offset -1 means reset all
230 local dev=$1 offset=$2 sectors
232 if [ -n "${blkzone}" ] && [ ! -n "${use_libzbc}" ]; then
233 if [ "$offset" -lt 0 ]; then
234 ${blkzone} reset "$dev"
236 ${blkzone} reset -o "${offset}" -c 1 "$dev"
239 if [ "$offset" -lt 0 ]; then
240 ${zbc_reset_zone} -all "$dev" >/dev/null
242 ${zbc_reset_zone} -sector "$dev" "${offset}" >/dev/null
247 # Extract the number of bytes that have been transferred from a line like
248 # READ: bw=6847KiB/s (7011kB/s), 6847KiB/s-6847KiB/s (7011kB/s-7011kB/s), io=257MiB (269MB), run=38406-38406msec
250 sed -n 's/^[[:blank:]]*'"$1"'.*, io=\([^[:blank:]]*\).*/\1/p' |
254 # Parse <number>.<number><suffix> into n1, n2 and s. See also
257 n1=${io%${io##*([0-9])}}
258 s=${io#${io%%*([a-zA-Z])}}
270 [ -n "$n1" ] || return 1
271 echo $(((n1 << m) + (n2 << m) / 1000))
286 count=$(sed -n 's/^.*write:[^;]*; \([0-9]*\) zone resets$/\1/p')