Skip to content

Commit 57e13a2

Browse files
Ming Leiaxboe
authored andcommitted
selftests: ublk: support user recovery
Add user recovery feature. Meantime add user recovery test: generic_04 and generic_05(zero copy) Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://lore.kernel.org/r/20250412023035.2649275-12-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 810b88f commit 57e13a2

File tree

6 files changed

+230
-10
lines changed

6 files changed

+230
-10
lines changed

tools/testing/selftests/ublk/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LDLIBS += -lpthread -lm -luring
66
TEST_PROGS := test_generic_01.sh
77
TEST_PROGS += test_generic_02.sh
88
TEST_PROGS += test_generic_03.sh
9+
TEST_PROGS += test_generic_04.sh
10+
TEST_PROGS += test_generic_05.sh
911

1012
TEST_PROGS += test_null_01.sh
1113
TEST_PROGS += test_null_02.sh

tools/testing/selftests/ublk/kublk.c

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ static int ublk_ctrl_start_dev(struct ublk_dev *dev,
119119
return __ublk_ctrl_cmd(dev, &data);
120120
}
121121

122+
static int ublk_ctrl_start_user_recovery(struct ublk_dev *dev)
123+
{
124+
struct ublk_ctrl_cmd_data data = {
125+
.cmd_op = UBLK_U_CMD_START_USER_RECOVERY,
126+
};
127+
128+
return __ublk_ctrl_cmd(dev, &data);
129+
}
130+
131+
static int ublk_ctrl_end_user_recovery(struct ublk_dev *dev, int daemon_pid)
132+
{
133+
struct ublk_ctrl_cmd_data data = {
134+
.cmd_op = UBLK_U_CMD_END_USER_RECOVERY,
135+
.flags = CTRL_CMD_HAS_DATA,
136+
};
137+
138+
dev->dev_info.ublksrv_pid = data.data[0] = daemon_pid;
139+
140+
return __ublk_ctrl_cmd(dev, &data);
141+
}
142+
122143
static int ublk_ctrl_add_dev(struct ublk_dev *dev)
123144
{
124145
struct ublk_ctrl_cmd_data data = {
@@ -812,8 +833,12 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
812833
free(affinity_buf);
813834

814835
/* everything is fine now, start us */
815-
ublk_set_parameters(dev);
816-
ret = ublk_ctrl_start_dev(dev, getpid());
836+
if (ctx->recovery)
837+
ret = ublk_ctrl_end_user_recovery(dev, getpid());
838+
else {
839+
ublk_set_parameters(dev);
840+
ret = ublk_ctrl_start_dev(dev, getpid());
841+
}
817842
if (ret < 0) {
818843
ublk_err("%s: ublk_ctrl_start_dev failed: %d\n", __func__, ret);
819844
goto fail;
@@ -988,7 +1013,10 @@ static int __cmd_dev_add(const struct dev_ctx *ctx)
9881013
}
9891014
}
9901015

991-
ret = ublk_ctrl_add_dev(dev);
1016+
if (ctx->recovery)
1017+
ret = ublk_ctrl_start_user_recovery(dev);
1018+
else
1019+
ret = ublk_ctrl_add_dev(dev);
9921020
if (ret < 0) {
9931021
ublk_err("%s: can't add dev id %d, type %s ret %d\n",
9941022
__func__, dev_id, tgt_type, ret);
@@ -1202,12 +1230,14 @@ static int cmd_dev_get_features(void)
12021230
return ret;
12031231
}
12041232

1205-
static int cmd_dev_help(char *exe)
1233+
static void __cmd_create_help(char *exe, bool recovery)
12061234
{
12071235
int i;
12081236

1209-
printf("%s add -t [null|loop|stripe] [-q nr_queues] [-d depth] [-n dev_id]\n", exe);
1210-
printf("\t[--foreground] [--quiet] [-z] [--debug_mask mask]\n");
1237+
printf("%s %s -t [null|loop|stripe] [-q nr_queues] [-d depth] [-n dev_id]\n",
1238+
exe, recovery ? "recover" : "add");
1239+
printf("\t[--foreground] [--quiet] [-z] [--debug_mask mask] [-r 0|1 ] [-g 0|1]\n");
1240+
printf("\t[-e 0|1 ] [-i 0|1]\n");
12111241
printf("\t[target options] [backfile1] [backfile2] ...\n");
12121242
printf("\tdefault: nr_queues=2(max 32), depth=128(max 1024), dev_id=-1(auto allocation)\n");
12131243

@@ -1217,7 +1247,25 @@ static int cmd_dev_help(char *exe)
12171247
if (ops->usage)
12181248
ops->usage(ops);
12191249
}
1250+
}
1251+
1252+
static void cmd_add_help(char *exe)
1253+
{
1254+
__cmd_create_help(exe, false);
1255+
printf("\n");
1256+
}
1257+
1258+
static void cmd_recover_help(char *exe)
1259+
{
1260+
__cmd_create_help(exe, true);
1261+
printf("\tPlease provide exact command line for creating this device with real dev_id\n");
12201262
printf("\n");
1263+
}
1264+
1265+
static int cmd_dev_help(char *exe)
1266+
{
1267+
cmd_add_help(exe);
1268+
cmd_recover_help(exe);
12211269

12221270
printf("%s del [-n dev_id] -a \n", exe);
12231271
printf("\t -a delete all devices -n delete specified device\n\n");
@@ -1239,6 +1287,10 @@ int main(int argc, char *argv[])
12391287
{ "quiet", 0, NULL, 0 },
12401288
{ "zero_copy", 0, NULL, 'z' },
12411289
{ "foreground", 0, NULL, 0 },
1290+
{ "recovery", 1, NULL, 'r' },
1291+
{ "recovery_fail_io", 1, NULL, 'e'},
1292+
{ "recovery_reissue", 1, NULL, 'i'},
1293+
{ "get_data", 1, NULL, 'g'},
12421294
{ 0, 0, 0, 0 }
12431295
};
12441296
const struct ublk_tgt_ops *ops = NULL;
@@ -1253,13 +1305,14 @@ int main(int argc, char *argv[])
12531305
int ret = -EINVAL, i;
12541306
int tgt_argc = 1;
12551307
char *tgt_argv[MAX_NR_TGT_ARG] = { NULL };
1308+
int value;
12561309

12571310
if (argc == 1)
12581311
return ret;
12591312

12601313
opterr = 0;
12611314
optind = 2;
1262-
while ((opt = getopt_long(argc, argv, "t:n:d:q:az",
1315+
while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:az",
12631316
longopts, &option_idx)) != -1) {
12641317
switch (opt) {
12651318
case 'a':
@@ -1281,6 +1334,25 @@ int main(int argc, char *argv[])
12811334
case 'z':
12821335
ctx.flags |= UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_USER_COPY;
12831336
break;
1337+
case 'r':
1338+
value = strtol(optarg, NULL, 10);
1339+
if (value)
1340+
ctx.flags |= UBLK_F_USER_RECOVERY;
1341+
break;
1342+
case 'e':
1343+
value = strtol(optarg, NULL, 10);
1344+
if (value)
1345+
ctx.flags |= UBLK_F_USER_RECOVERY | UBLK_F_USER_RECOVERY_FAIL_IO;
1346+
break;
1347+
case 'i':
1348+
value = strtol(optarg, NULL, 10);
1349+
if (value)
1350+
ctx.flags |= UBLK_F_USER_RECOVERY | UBLK_F_USER_RECOVERY_REISSUE;
1351+
break;
1352+
case 'g':
1353+
value = strtol(optarg, NULL, 10);
1354+
if (value)
1355+
ctx.flags |= UBLK_F_NEED_GET_DATA;
12841356
case 0:
12851357
if (!strcmp(longopts[option_idx].name, "debug_mask"))
12861358
ublk_dbg_mask = strtol(optarg, NULL, 16);
@@ -1326,7 +1398,15 @@ int main(int argc, char *argv[])
13261398

13271399
if (!strcmp(cmd, "add"))
13281400
ret = cmd_dev_add(&ctx);
1329-
else if (!strcmp(cmd, "del"))
1401+
else if (!strcmp(cmd, "recover")) {
1402+
if (ctx.dev_id < 0) {
1403+
fprintf(stderr, "device id isn't provided for recovering\n");
1404+
ret = -EINVAL;
1405+
} else {
1406+
ctx.recovery = 1;
1407+
ret = cmd_dev_add(&ctx);
1408+
}
1409+
} else if (!strcmp(cmd, "del"))
13301410
ret = cmd_dev_del(&ctx);
13311411
else if (!strcmp(cmd, "list")) {
13321412
ctx.all = 1;

tools/testing/selftests/ublk/kublk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct dev_ctx {
7979
unsigned int logging:1;
8080
unsigned int all:1;
8181
unsigned int fg:1;
82+
unsigned int recovery:1;
8283

8384
int _evtfd;
8485
int _shmid;

tools/testing/selftests/ublk/test_common.sh

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ _have_feature()
169169
return 1
170170
}
171171

172-
_add_ublk_dev() {
172+
_create_ublk_dev() {
173173
local dev_id;
174+
local cmd=$1
175+
176+
shift 1
174177

175178
if [ ! -c /dev/ublk-control ]; then
176179
return ${UBLK_SKIP_CODE}
@@ -181,7 +184,7 @@ _add_ublk_dev() {
181184
fi
182185
fi
183186

184-
if ! dev_id=$("${UBLK_PROG}" add "$@" | grep "dev id" | awk -F '[ :]' '{print $3}'); then
187+
if ! dev_id=$("${UBLK_PROG}" "$cmd" "$@" | grep "dev id" | awk -F '[ :]' '{print $3}'); then
185188
echo "fail to add ublk dev $*"
186189
return 255
187190
fi
@@ -194,6 +197,23 @@ _add_ublk_dev() {
194197
fi
195198
}
196199

200+
_add_ublk_dev() {
201+
_create_ublk_dev "add" "$@"
202+
}
203+
204+
_recover_ublk_dev() {
205+
local dev_id
206+
local state
207+
208+
dev_id=$(_create_ublk_dev "recover" "$@")
209+
for ((j=0;j<20;j++)); do
210+
state=$(_get_ublk_dev_state "${dev_id}")
211+
[ "$state" == "LIVE" ] && break
212+
sleep 1
213+
done
214+
echo "$state"
215+
}
216+
197217
# kill the ublk daemon and return ublk device state
198218
__ublk_kill_daemon()
199219
{
@@ -280,6 +300,39 @@ run_io_and_kill_daemon()
280300
fi
281301
}
282302

303+
run_io_and_recover()
304+
{
305+
local state
306+
local dev_id
307+
308+
dev_id=$(_add_ublk_dev "$@")
309+
_check_add_dev "$TID" $?
310+
311+
fio --name=job1 --filename=/dev/ublkb"${dev_id}" --ioengine=libaio \
312+
--rw=readwrite --iodepth=256 --size="${size}" --numjobs=4 \
313+
--runtime=20 --time_based > /dev/null 2>&1 &
314+
sleep 4
315+
316+
state=$(__ublk_kill_daemon "${dev_id}" "QUIESCED")
317+
if [ "$state" != "QUIESCED" ]; then
318+
echo "device isn't quiesced($state) after killing daemon"
319+
return 255
320+
fi
321+
322+
state=$(_recover_ublk_dev -n "$dev_id" "$@")
323+
if [ "$state" != "LIVE" ]; then
324+
echo "faile to recover to LIVE($state)"
325+
return 255
326+
fi
327+
328+
if ! __remove_ublk_dev_return "${dev_id}"; then
329+
echo "delete dev ${dev_id} failed"
330+
return 255
331+
fi
332+
wait
333+
}
334+
335+
283336
_ublk_test_top_dir()
284337
{
285338
cd "$(dirname "$0")" && pwd
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
5+
6+
TID="generic_04"
7+
ERR_CODE=0
8+
9+
ublk_run_recover_test()
10+
{
11+
run_io_and_recover "$@"
12+
ERR_CODE=$?
13+
if [ ${ERR_CODE} -ne 0 ]; then
14+
echo "$TID failure: $*"
15+
_show_result $TID $ERR_CODE
16+
fi
17+
}
18+
19+
if ! _have_program fio; then
20+
exit "$UBLK_SKIP_CODE"
21+
fi
22+
23+
_prep_test "recover" "basic recover function verification"
24+
25+
_create_backfile 0 256M
26+
_create_backfile 1 128M
27+
_create_backfile 2 128M
28+
29+
ublk_run_recover_test -t null -q 2 -r 1 &
30+
ublk_run_recover_test -t loop -q 2 -r 1 "${UBLK_BACKFILES[0]}" &
31+
ublk_run_recover_test -t stripe -q 2 -r 1 "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
32+
wait
33+
34+
ublk_run_recover_test -t null -q 2 -r 1 -i 1 &
35+
ublk_run_recover_test -t loop -q 2 -r 1 -i 1 "${UBLK_BACKFILES[0]}" &
36+
ublk_run_recover_test -t stripe -q 2 -r 1 -i 1 "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
37+
wait
38+
39+
_cleanup_test "recover"
40+
_show_result $TID $ERR_CODE
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
5+
6+
TID="generic_04"
7+
ERR_CODE=0
8+
9+
ublk_run_recover_test()
10+
{
11+
run_io_and_recover "$@"
12+
ERR_CODE=$?
13+
if [ ${ERR_CODE} -ne 0 ]; then
14+
echo "$TID failure: $*"
15+
_show_result $TID $ERR_CODE
16+
fi
17+
}
18+
19+
if ! _have_program fio; then
20+
exit "$UBLK_SKIP_CODE"
21+
fi
22+
23+
if ! _have_feature "ZERO_COPY"; then
24+
exit "$UBLK_SKIP_CODE"
25+
fi
26+
27+
_prep_test "recover" "basic recover function verification (zero copy)"
28+
29+
_create_backfile 0 256M
30+
_create_backfile 1 128M
31+
_create_backfile 2 128M
32+
33+
ublk_run_recover_test -t null -q 2 -r 1 -z &
34+
ublk_run_recover_test -t loop -q 2 -r 1 -z "${UBLK_BACKFILES[0]}" &
35+
ublk_run_recover_test -t stripe -q 2 -r 1 -z "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
36+
wait
37+
38+
ublk_run_recover_test -t null -q 2 -r 1 -z -i 1 &
39+
ublk_run_recover_test -t loop -q 2 -r 1 -z -i 1 "${UBLK_BACKFILES[0]}" &
40+
ublk_run_recover_test -t stripe -q 2 -r 1 -z -i 1 "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
41+
wait
42+
43+
_cleanup_test "recover"
44+
_show_result $TID $ERR_CODE

0 commit comments

Comments
 (0)