bcache: add bcache/001
Subsystems (3)
v2 cover bcache: add bcache/001
I've got it working. The problem is that udevd sometimes doesn't register the devices in a timely fashion. The workaround is to explicitly register the devices within the tests. This races with udevd, thus some logic is in there to filter out 'already registered warnings'. Eventually someone should look at this...
Also made the wiping of the disks a bit more robust by stealing the ideas from Stephen's test case[1]. Thanks!
As requested I also added an entry in running-tests.md. There is already a section on TEST_CASE_DEV_ARRAY so it's a bit redudant but given it took me a while to figure this out, I think it's worth to have it around.
Now, the test runs very stable in my VM. No issues in hundres of of runs:
bcache/001 => nvme0n1 vdb vdc (test bcache setup and teardown) [passed]
runtime 4.659s ... 3.555s
The runtime is not really constant, one of the symptions with all the workarounds in place to get it working (retry loops, white loops etc).
[ 5327.588585][T100690] run blktests bcache/001 at 2026-01-21 14:33:45 [ 5327.742119][T100766] bcache: run_cache_set() invalidating existing data [ 5327.747790][T100766] bcache: register_cache() registered cache device nvme0n1 [ 5327.782118][T100690] bcache: register_bcache() error : device already registered [ 5327.790339][T100690] bcache: register_bdev() registered backing device vdb [ 5327.798633][T100690] bcache: bch_cached_dev_attach() Caching vdb as bcache0 on set 33fc88e8-3251-433e-be43-620f625c [ 5329.598292][T100396] bcache: bcache_device_free() bcache0 stopped [ 5329.898146][T94893] bcache: cache_set_free() Cache set 33fc88e8-3251-433e-be43-620f6253e0fc unregistered [ 5330.315859][T100803] bcache: run_cache_set() invalidating existing data [ 5330.324537][T100803] bcache: register_cache() registered cache device nvme0n1 [ 5330.330337][T100809] bcache: register_bdev() registered backing device vdc [ 5330.339168][T100809] bcache: bch_cached_dev_attach() Caching vdc as bcache0 on set 986fa332-8e02-4a1e-942c-b6f3458e [ 5330.345113][T100813] bcache: register_bdev() registered backing device vdb [ 5330.362462][T100813] bcache: bch_cached_dev_attach() Caching vdb as bcache1 on set 986fa332-8e02-4a1e-942c-b6f3458e [ 5330.466850][T100690] bcache: register_bcache() error : device already registered [ 5330.469039][T100690] bcache: register_bcache() error : device already registered [ 5330.470933][T100690] bcache: register_bcache() error : device already registered [ 5330.472971][T100690] bcache: register_bcache() error : device already registered [ 5330.474847][T100690] bcache: register_bcache() error : device already registered [ 5330.522678][T11701] bcache: bcache_device_free() bcache1 stopped [ 5330.571838][T11701] bcache: bcache_device_free() bcache0 stopped [ 5330.805725][T80064] bcache: cache_set_free() Cache set 986fa332-8e02-4a1e-942c-b6f3458470fe unregistered
The next step is to figure out how to hand back the devices to calling test, so the test doesn't have to hardcode the test device. From Stephen's test:
[...]
# 2. Detach
log "Detaching backing device..."
BDEV_NAME=$(basename $BCACHE_DEV)
echo 1 | sudo tee /sys/block/$BDEV_NAME/bcache/detach > /dev/null
[...]
I am sure there is some bash-way to do this, which makes me blind. But hey, if it works, I wont complain.
---
Changes in v2:
- fixed whitespace damage
- added documentation on how to configure for bcache tests
- do registering explicitly
- made disk wiping more robust
- Link to v1: https://patch.msgid.link/[email protected]
---
Daniel Wagner (2):
bcache: add bcache/001
doc: document how to configure bcache tests
Documentation/running-tests.md | 16 +++ tests/bcache/001 | 32 +++++ tests/bcache/001.out | 1 + tests/bcache/rc | 259 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 308 insertions(+)
---
base-commit: e387a7e0169cc012eb6a7140a0561d2901c92a76
change-id: 20260120-bcache-35ec7368c8f4
Best regards,
Signature · 1 line
Daniel Wagner <[email protected]>
PATCH v2 1/2 bcache: add bcache/001
So far we are missing tests for bcache. Besides a relative simple setup/teardown tests add also the corresponding infrastructure. More tests are to be expected to depend on this.
_create_bcache/_remove_bcache are tracking the resources and if anything is missing it will complain.
--- tests/bcache/001 | 32 +++++++ tests/bcache/001.out | 1 + tests/bcache/rc | 259 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 292 insertions(+)
tests/bcache/001 new file +32 -0
@@ -0,0 +1,32 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Daniel Wagner, SUSE Labs +# +# Test bcache setup and teardown + +. tests/bcache/rc + +DESCRIPTION="test bcache setup and teardown" + +requires() { + _bcache_requires +} + +test_device_array() { + echo "Running ${TEST_NAME}" + + if [[ ${#TEST_DEV_ARRAY[@]} -lt 3 ]]; then + SKIP_REASONS+=("requires at least 3 devices") + return 1 + fi + + _create_bcache \ + --cache "${TEST_DEV_ARRAY[0]##*/}" \ + --bdev "${TEST_DEV_ARRAY[1]##*/}" + _remove_bcache + + _create_bcache \ + --cache "${TEST_DEV_ARRAY[0]##*/}" \ + --bdev "${TEST_DEV_ARRAY[1]##*/}" "${TEST_DEV_ARRAY[2]##*/}" + _remove_bcache +}
tests/bcache/001.out new file +1 -0
@@ -0,0 +1 @@ +Running bcache/001
tests/bcache/rc new file +259 -0
259 lines not on this page · raw message
Signature · 1 line
2.52.0
tests/bcache/rc line 9
Quoted 3 lines
tests/bcache/rc new file +3
-0+declare -a BCACHE_DEVS=() +declare -a BCACHE_BDEVS=() +declare -a BCACHE_CSETS=()
I've change _create_bcache so it returns the bcache device, thus I can hand those into the _remove_bcache function thus no need for globals anymore. So don't spend too much in reviewing this :)
Hi Daniel, thank you for working on this. It's great to extend the test coverage :)
Please find my review comments in line. I ran the new test case, and observed a failure. I noted my findings about the failure as one of the inline comments.
tests/bcache/001
Quoted 16 lines · Daniel Wagner
So far we are missing tests for bcache. Besides a relative simple setup/teardown tests add also the corresponding infrastructure. More tests are to be expected to depend on this.
_create_bcache/_remove_bcache are tracking the resources and if anything is missing it will complain.
Signed-off-by Daniel Wagner <[email protected]>--- tests/bcache/001 | 32 +++++++ tests/bcache/001.out | 1 + tests/bcache/rc | 259 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 292 insertions(+)tests/bcache/001 new file +0
-0
To be consistent with other test case files, I suggest to set the file mode 755.
tests/bcache/001 line 2
Quoted 6 lines
index 000000000000..4a6e01113b6b
tests/bcache/001 new file +2
-0@@ -0,0 +1,32 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0
Most of blktests files specifies GPL-3.0+. If there is no specific reason, I suggest GPL-3.0+. Same comment for tests/bcache/rc.
tests/bcache/001 line 13
Quoted 11 lines
tests/bcache/001 new file +11
-0+# Copyright (C) 2026 Daniel Wagner, SUSE Labs +# +# Test bcache setup and teardown + +. tests/bcache/rc + +DESCRIPTION="test bcache setup and teardown" + +requires() { + _bcache_requires +}
Do you foresee that all bcache/* test cases have this _bcache_requires call? If so, I suggest to,
- rename _bcache_requires() in tests/bcache/rc to group_requires(), and
- drop the three lines above from bcache/001.group_requires() is called once before start testing this bcache group.
Quoted 39 lines
tests/bcache/001 new file +19
-0+ +test_device_array() { + echo "Running ${TEST_NAME}" + + if [[ ${#TEST_DEV_ARRAY[@]} -lt 3 ]]; then + SKIP_REASONS+=("requires at least 3 devices") + return 1 + fi + + _create_bcache \ + --cache "${TEST_DEV_ARRAY[0]##*/}" \ + --bdev "${TEST_DEV_ARRAY[1]##*/}" + _remove_bcache + + _create_bcache \ + --cache "${TEST_DEV_ARRAY[0]##*/}" \ + --bdev "${TEST_DEV_ARRAY[1]##*/}" "${TEST_DEV_ARRAY[2]##*/}" + _remove_bcache +}tests/bcache/001.out new file +1
-0@@ -0,0 +1 @@ +Running bcache/001tests/bcache/rc new file +7
-0@@ -0,0 +1,259 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Daniel Wagner, SUSE Labs + +. common/rc + +declare -a BCACHE_DEVS=()
tests/bcache/rc line 17
Quoted 10 lines
tests/bcache/rc new file +10
-0+declare -a BCACHE_BDEVS=() +declare -a BCACHE_CSETS=() + +BCACHE_MAX_RETRIES=5 + +_bcache_requires() { + _have_kernel_options MD BCACHE BCACHE_DEBUG AUTOFS_FS + _have_program make-bcache + _have_crypto_algorithm crc32c +}
As noted above, you may want to rename _bcache_requires() to group_requires().
tests/bcache/rc line 19
Quoted 2 lines
tests/bcache/rc new file +2
-0+ +_bcache_wipe_devs() {
Nit: "local dev" can be added here.
Quoted 9 lines
tests/bcache/rc new file +9
-0+ for dev in "${BCACHE_DEVS[@]}"; do + # Attempt a clean wipe first + if wipefs --all --quiet "${dev}" 2>/dev/null; then + continue + fi + + # Overwrite the first 10MB to clear stubborn partition tables or metadata + if ! dd if=/dev/zero of="${dev}" bs=1M count=10 conv=notrunc status=none; then + echo "Error: dd failed on ${dev}" >&2
tests/bcache/rc line 38
Quoted 10 lines
tests/bcache/rc new file +10
-0+ fi + + # Try wiping again after clearing the headers + if ! wipefs --all --quiet --force "${dev}"; then + echo "Warning: Failed to wipe ${dev} even after dd." >&2 + fi + done +} + +_bcache_register() {
When I did trial run, the test case bcache/001 failed with the message below.
bcache/001 => nvme1n1 nvme2n1 nvme3n1 nvme4n1 (test bcache setup and teardown) [failed]
runtime 8.406s ... 12.789s
--- tests/bcache/001.out 2026-01-22 13:55:00.510094421 +0900
+++ /home/shin/Blktests/blktests/results/nvme1n1_nvme2n1_nvme3n1_nvme4n1/bcache/001.out.bad 2026-01-22 16:21:47.686976578 +0900
@@ -1 +1,3 @@
Running bcache/001
+ERROR: bcache registration interface not found.
+WARNING: Could not find device node for UUID b854b766-8e08-42b5-b7cc-e137fd78ca51 after 5sI found that when the bcache driver is loadable and not yet loaded, the make-bcache command call loads the driver. However, it takes time for kernel to prepare /sys/fs/bcache/register. Hence the error message above. I suggest to add a hunk below here, to wait for the register file get ready. With this change, the failure disappeared in my test environment.
local dev err_msg timeout=0
while [[ ! -w /sys/fs/bcache/register ]] && (( timeout < 10 )); do
sleep 1
(( timeout ++ ))
doneQuoted 65 lines
tests/bcache/rc new file +65
-0+ if [[ ! -w /sys/fs/bcache/register ]]; then + echo "ERROR: bcache registration interface not found." >&2 + return 1 + fi + + for dev in "${BCACHE_DEVS[@]}"; do + if ! echo "${dev}" > /sys/fs/bcache/register 2>/tmp/bcache_err; then + err_msg=$(< /tmp/bcache_err) + + if [[ "${err_msg}" != *"Device or resource busy"* ]]; then + echo "ERROR: Failed to register ${dev}: ${err_msg:-"Unknown error"}" >&2 + fi + fi + done +} + +_create_bcache() { + local -a cdevs=() + local -a bdevs=() + local -a ARGS=() + local bucket_size="64k" + local block_size="4k" + + _register_test_cleanup _cleanup_bcache + + while [[ $# -gt 0 ]]; do + case $1 in + --cache) + shift + # Collect arguments until the next flag or end of input + while [[ $# -gt 0 && ! $1 =~ ^-- ]]; do + cdevs+=("$1") + shift + done + ;; + --bdev) + shift + # Collect arguments until the next flag or end of input + while [[ $# -gt 0 && ! $1 =~ ^-- ]]; do + bdevs+=("$1") + shift + done + ;; + --bucket-size) + bucket_size="$2" + shift 2 + ;; + --block-size) + block_size="$2" + shift 2 + ;; + --writeback) + ARGS+=(--writeback) + shift 1 + ;; + --discard) + ARGS+=(--discard) + shift 1 + ;; + *) + echo "WARNING: unknown argument: $1" + shift + ;; + esac + done
tests/bcache/rc line 113
Quoted 10 lines
tests/bcache/rc new file +10
-0+ + # add /dev prefix to device names + cdevs=( "${cdevs[@]/#/\/dev\/}" ) + bdevs=( "${bdevs[@]/#/\/dev\/}" ) + + # make-bcache expects empty/cleared devices + BCACHE_DEVS+=("${cdevs[@]}" "${bdevs[@]}") + _bcache_wipe_devs + + local -a cdevs_args=()
Nit: "local dev" can be added here.
tests/bcache/rc line 123
Quoted 10 lines
tests/bcache/rc new file +10
-0+ for dev in "${cdevs[@]}"; do + cdevs_args+=("--cache" "${dev}") + done + + local -a bdevs_args=() + for dev in "${bdevs[@]}"; do + bdevs_args+=("--bdev" "${dev}") + done + + local output cmd
"cmd" is array, then, I guess you meant,
local -a cmd
local output
Quoted 44 lines
tests/bcache/rc new file +44
-0+ cmd=(make-bcache \ + --wipe-bcache \ + --bucket "${bucket_size}" \ + --block "${block_size}" \ + "${cdevs_args[@]}" \ + "${bdevs_args[@]}" \ + "${ARGS[@]}") + + output=$("${cmd[@]}" 2>&1) + local rc=$? + if [[ "${rc}" -ne 0 ]]; then + echo "ERROR: make-bcache failed:" >&2 + echo "$output" >&2 + return 1 + fi + + local cset_uuid + cset_uuid=$(echo "$output" | awk '/Set UUID:/ {print $3}' | head -n 1) + if [[ -z "${cset_uuid}" ]]; then + echo "ERROR: Could not extract cset UUID from make-bcache output" >&2 + return 1 + fi + BCACHE_CSETS+=("${cset_uuid}") + + local -a bdev_uuids + mapfile -t bdev_uuids < <(echo "$output" | awk ' + $1 == "UUID:" { last_uuid = $2 } + $1 == "version:" && $2 == "1" { print last_uuid} + ') + + udevadm settle + + _bcache_register + + for uuid in "${bdev_uuids[@]}"; do + local link found attempt + + link=/dev/bcache/by-uuid/"${uuid}" + found=false + attempt=0 + + while (( attempt < BCACHE_MAX_RETRIES )); do + if [[ -L "$link" ]]; then + BCACHE_BDEVS+=("${uuid}")
tests/bcache/rc line 177
Quoted 10 lines
tests/bcache/rc new file +10
-0+ found=true + break + fi + + (( attempt++ )) + sleep 1 + done + + if [[ "$found" == "false" ]]; then + echo "WARNING: Could not find device node for UUID ${uuid} after ${BCACHE_MAX_RETRIES}s" >&2
Two spaces are used for the indent above.
Quoted 7 lines
tests/bcache/rc new file +7
-0+ fi + done +} + +_remove_bcache() { + local uuid +
tests/bcache/rc line 194
Quoted 10 lines
tests/bcache/rc new file +10
-0+ for uuid in "${BCACHE_BDEVS[@]}"; do + local dev_path + + dev_path=$(blkid -U "${uuid}") + if [ -n "$dev_path" ]; then + local dev_name + + dev_name="${dev_path##*/}" + if [ -f "/sys/block/${dev_name}/bcache/stop" ] ; then + echo 1 > "/sys/block/${dev_name}/bcache/stop"
Same here.
tests/bcache/rc line 201
Quoted 7 lines
tests/bcache/rc new file +7
-0+ fi + fi + done + + for uuid in "${BCACHE_CSETS[@]}"; do + if [ -f /sys/fs/bcache/"${uuid}"/unregister ] ; then + echo 1 > /sys/fs/bcache/"${uuid}"/unregister
Same here.
Quoted 22 lines
tests/bcache/rc new file +22
-0+ fi + done + + udevadm settle + + local timeout + timeout=0 + for uuid in "${BCACHE_CSETS[@]}"; do + while [[ -d "/sys/fs/bcache/${uuid}" ]] && (( timeout < 10 )); do + sleep 0.5 + (( timeout++ )) + done + done + + _bcache_wipe_devs + + BCACHE_CSETS=() + BCACHE_BDEVS=() +} + +_cleanup_bcache() { + local cset dev
tests/bcache/rc line 233
Quoted 10 lines
tests/bcache/rc new file +10
-0+ + shopt -s nullglob + for dev in /sys/block/bcache* ; do + [ -e "${dev}" ] || continue + + dev=$(basename "${dev}") + echo "WARNING: bcache device ${dev} found" + + if [[ -f /sys/block/"${dev}"/bcache/stop ]]; then + echo 1 > /sys/block/"${dev}"/bcache/stop 2>/dev/null || true
Do we need "|| true" here?
tests/bcache/rc line 240
Quoted 7 lines
tests/bcache/rc new file +7
-0+ fi + done + + for cset in /sys/fs/bcache/*-*-*-*-*; do + if [[ -d "${cset}" ]]; then + echo "WARNING: Unregistering cset $(basename "${cset}")" + echo 1 > "${cset}"/unregister 2>/dev/null || true
Same here.
Quoted 23 lines
tests/bcache/rc new file +19
-0+ fi + done + shopt -u nullglob + + udevadm settle + + local timeout + timeout=0 + for cset in /sys/fs/bcache/*-*-*-*-*; do + while [[ -d "${cset}" ]] && (( timeout < 10 )); do + sleep 0.5 + (( timeout++ )) + done + done + + _bcache_wipe_devs + + BCACHE_DEVS=() +}Signature · 2 lines
-- 2.52.0
Quoted 6 lines · Shinichiro Kawasaki
Hi Daniel, thank you for working on this. It's great to extend the test coverage :)
Please find my review comments in line. I ran the new test case, and observed a failure. I noted my findings about the failure as one of the inline comments.
Ah sorry, saw your response after sending out v3. I'll work on v4 after I am back from my vacation.
PATCH v2 2/2 doc: document how to configure bcache tests
In reply to Daniel WagnerAdd a bcache entry in running-tests which explains how to configure blktests for the bcache tests.
--- Documentation/running-tests.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
Documentation/running-tests.md +16 -0
@@ -189,6 +189,22 @@ THROTL_BLKDEV_TYPES="sdebug" ./check throtl/ THROTL_BLKDEV_TYPES="nullb sdebug" ./check throtl/ ``` +### bcache test configuration + +The bcache tests require multiple devices to run simultaneously. By default, +blktests executes each test case iteratively for every individual device listed +in TEST_DEVS. This standard behavior makes it impossible to pass a group of +devices into a single test via TEST_DEVS. + +To solve this, the TEST_CASE_DEV_ARRAY was introduced. This allows for custom +device configurations on a per-test basis. For bcache tests, a minimum of three +devices is required. Configuration Example + +Add the following to your configuration to define the devices used for all +bcache tests: + +TEST_CASE_DEV_ARRAY[bcache/*]="/dev/nvme0n1 /dev/vdb /dev/vdc" + ### Normal user To run test cases which require normal user privilege, prepare a user and
Signature · 1 line
2.52.0
Re: [PATCH blktests v2 2/2] doc: document how to configure bcache tests
Quoted 19 lines · Daniel Wagner
Add a bcache entry in running-tests which explains how to configure blktests for the bcache tests.
Signed-off-by Daniel Wagner <[email protected]>--- Documentation/running-tests.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)Documentation/running-tests.md +3
-0@@ -189,6 +189,22 @@ THROTL_BLKDEV_TYPES="sdebug" ./check throtl/ THROTL_BLKDEV_TYPES="nullb sdebug" ./check throtl/ ``` +### bcache test configuration + +The bcache tests require multiple devices to run simultaneously. By default,
Documentation/running-tests.md THROTL_BLKDEV_TYPES="sdebug" ./check throtl/ · line 204
Quoted 10 lines
Documentation/running-tests.md +10
-0+blktests executes each test case iteratively for every individual device listed +in TEST_DEVS. This standard behavior makes it impossible to pass a group of +devices into a single test via TEST_DEVS. + +To solve this, the TEST_CASE_DEV_ARRAY was introduced. This allows for custom +device configurations on a per-test basis. For bcache tests, a minimum of three +devices is required. Configuration Example + +Add the following to your configuration to define the devices used for all +bcache tests:
As you noted in the cover letter, TEST_CASE_DEV_ARRAY is already described in running-tests.md. So I think this section can be some more concise. My quick rewrite is as follows. If it makes sense for you, please consider to pick it up.
### Bcache test configuration
The bcache tests require multiple devices to run simultaneously. By default, blktests run each test case for each device in TEST_DEVS. This behavior prevents testing with multiple devices. The TEST_CASE_DEV_ARRAY resolves this by enabling multiple device configurations per test. Bcache tests need at least three devices, which can be specified in your configuration as follows:
Quoted 10 lines
Documentation/running-tests.md +3
-0+ +TEST_CASE_DEV_ARRAY[bcache/*]="/dev/nvme0n1 /dev/vdb /dev/vdc" + ### Normal user To run test cases which require normal user privilege, prepare a user andSignature · 2 lines
-- 2.52.0