Skip to content

Commit f40b1f2

Browse files
Ming Leiaxboe
authored andcommitted
selftests: ublk: add test case for UBLK_U_CMD_UPDATE_SIZE
Add test generic_10 for covering new control command of UBLK_U_CMD_UPDATE_SIZE. Add 'update_size -s|--size size_in_bytes' sub-command on ublk utility for supporting this feature, then verify the feature via generic_10. Cc: Omri Mann <[email protected]> Cc: Jared Holzman <[email protected]> Signed-off-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 927244f commit f40b1f2

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

tools/testing/selftests/ublk/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ TEST_PROGS += test_generic_07.sh
1717

1818
TEST_PROGS += test_generic_08.sh
1919
TEST_PROGS += test_generic_09.sh
20+
TEST_PROGS += test_generic_10.sh
2021

2122
TEST_PROGS += test_null_01.sh
2223
TEST_PROGS += test_null_02.sh

tools/testing/selftests/ublk/kublk.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ static int ublk_ctrl_get_features(struct ublk_dev *dev,
216216
return __ublk_ctrl_cmd(dev, &data);
217217
}
218218

219+
static int ublk_ctrl_update_size(struct ublk_dev *dev,
220+
__u64 nr_sects)
221+
{
222+
struct ublk_ctrl_cmd_data data = {
223+
.cmd_op = UBLK_U_CMD_UPDATE_SIZE,
224+
.flags = CTRL_CMD_HAS_DATA,
225+
};
226+
227+
data.data[0] = nr_sects;
228+
return __ublk_ctrl_cmd(dev, &data);
229+
}
230+
219231
static const char *ublk_dev_state_desc(struct ublk_dev *dev)
220232
{
221233
switch (dev->dev_info.state) {
@@ -1236,6 +1248,7 @@ static int cmd_dev_get_features(void)
12361248
[const_ilog2(UBLK_F_USER_COPY)] = "USER_COPY",
12371249
[const_ilog2(UBLK_F_ZONED)] = "ZONED",
12381250
[const_ilog2(UBLK_F_USER_RECOVERY_FAIL_IO)] = "RECOVERY_FAIL_IO",
1251+
[const_ilog2(UBLK_F_UPDATE_SIZE)] = "UPDATE_SIZE",
12391252
[const_ilog2(UBLK_F_AUTO_BUF_REG)] = "AUTO_BUF_REG",
12401253
};
12411254
struct ublk_dev *dev;
@@ -1270,6 +1283,39 @@ static int cmd_dev_get_features(void)
12701283
return ret;
12711284
}
12721285

1286+
static int cmd_dev_update_size(struct dev_ctx *ctx)
1287+
{
1288+
struct ublk_dev *dev = ublk_ctrl_init();
1289+
struct ublk_params p;
1290+
int ret = -EINVAL;
1291+
1292+
if (!dev)
1293+
return -ENODEV;
1294+
1295+
if (ctx->dev_id < 0) {
1296+
fprintf(stderr, "device id isn't provided\n");
1297+
goto out;
1298+
}
1299+
1300+
dev->dev_info.dev_id = ctx->dev_id;
1301+
ret = ublk_ctrl_get_params(dev, &p);
1302+
if (ret < 0) {
1303+
ublk_err("failed to get params %d %s\n", ret, strerror(-ret));
1304+
goto out;
1305+
}
1306+
1307+
if (ctx->size & ((1 << p.basic.logical_bs_shift) - 1)) {
1308+
ublk_err("size isn't aligned with logical block size\n");
1309+
ret = -EINVAL;
1310+
goto out;
1311+
}
1312+
1313+
ret = ublk_ctrl_update_size(dev, ctx->size >> 9);
1314+
out:
1315+
ublk_ctrl_deinit(dev);
1316+
return ret;
1317+
}
1318+
12731319
static void __cmd_create_help(char *exe, bool recovery)
12741320
{
12751321
int i;
@@ -1312,6 +1358,7 @@ static int cmd_dev_help(char *exe)
13121358
printf("%s list [-n dev_id] -a \n", exe);
13131359
printf("\t -a list all devices, -n list specified device, default -a \n\n");
13141360
printf("%s features\n", exe);
1361+
printf("%s update_size -n dev_id -s|--size size_in_bytes \n", exe);
13151362
return 0;
13161363
}
13171364

@@ -1333,6 +1380,7 @@ int main(int argc, char *argv[])
13331380
{ "get_data", 1, NULL, 'g'},
13341381
{ "auto_zc", 0, NULL, 0 },
13351382
{ "auto_zc_fallback", 0, NULL, 0 },
1383+
{ "size", 1, NULL, 's'},
13361384
{ 0, 0, 0, 0 }
13371385
};
13381386
const struct ublk_tgt_ops *ops = NULL;
@@ -1354,7 +1402,7 @@ int main(int argc, char *argv[])
13541402

13551403
opterr = 0;
13561404
optind = 2;
1357-
while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:gaz",
1405+
while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:s:gaz",
13581406
longopts, &option_idx)) != -1) {
13591407
switch (opt) {
13601408
case 'a':
@@ -1394,6 +1442,9 @@ int main(int argc, char *argv[])
13941442
case 'g':
13951443
ctx.flags |= UBLK_F_NEED_GET_DATA;
13961444
break;
1445+
case 's':
1446+
ctx.size = strtoull(optarg, NULL, 10);
1447+
break;
13971448
case 0:
13981449
if (!strcmp(longopts[option_idx].name, "debug_mask"))
13991450
ublk_dbg_mask = strtol(optarg, NULL, 16);
@@ -1470,6 +1521,8 @@ int main(int argc, char *argv[])
14701521
ret = cmd_dev_help(argv[0]);
14711522
else if (!strcmp(cmd, "features"))
14721523
ret = cmd_dev_get_features();
1524+
else if (!strcmp(cmd, "update_size"))
1525+
ret = cmd_dev_update_size(&ctx);
14731526
else
14741527
cmd_dev_help(argv[0]);
14751528

tools/testing/selftests/ublk/kublk.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ struct dev_ctx {
9292
/* built from shmem, only for ublk_dump_dev() */
9393
struct ublk_dev *shadow_dev;
9494

95+
/* for 'update_size' command */
96+
unsigned long long size;
97+
9598
union {
9699
struct stripe_ctx stripe;
97100
struct fault_inject_ctx fault_inject;

tools/testing/selftests/ublk/test_common.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ _get_disk_dev_t() {
2323
echo $(( (major & 0xfff) << 20 | (minor & 0xfffff) ))
2424
}
2525

26+
_get_disk_size()
27+
{
28+
lsblk -b -o SIZE -n "$1"
29+
}
30+
2631
_run_fio_verify_io() {
2732
fio --name=verify --rw=randwrite --direct=1 --ioengine=libaio \
2833
--bs=8k --iodepth=32 --verify=crc32c --do_verify=1 \
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
5+
6+
TID="generic_10"
7+
ERR_CODE=0
8+
9+
if ! _have_feature "UPDATE_SIZE"; then
10+
exit "$UBLK_SKIP_CODE"
11+
fi
12+
13+
_prep_test "null" "check update size"
14+
15+
dev_id=$(_add_ublk_dev -t null)
16+
_check_add_dev $TID $?
17+
18+
size=$(_get_disk_size /dev/ublkb"${dev_id}")
19+
size=$(( size / 2 ))
20+
if ! "$UBLK_PROG" update_size -n "$dev_id" -s "$size"; then
21+
ERR_CODE=255
22+
fi
23+
24+
new_size=$(_get_disk_size /dev/ublkb"${dev_id}")
25+
if [ "$new_size" != "$size" ]; then
26+
ERR_CODE=255
27+
fi
28+
29+
_cleanup_test "null"
30+
_show_result $TID $ERR_CODE

0 commit comments

Comments
 (0)