Skip to content

Commit 308fc16

Browse files
xuyang0410metan-ucw
authored andcommitted
syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test
LOOP_SET_CAPACITY can update a live loop device size when we change the size of the underlying backing file. Also check sys value. Signed-off-by: Yang Xu <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent ed90cb6 commit 308fc16

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ ioctl08 ioctl08
532532
ioctl_loop01 ioctl_loop01
533533
ioctl_loop02 ioctl_loop02
534534
ioctl_loop03 ioctl_loop03
535+
ioctl_loop04 ioctl_loop04
535536

536537
ioctl_ns01 ioctl_ns01
537538
ioctl_ns02 ioctl_ns02

testcases/kernel/syscalls/ioctl/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/ioctl_loop01
1010
/ioctl_loop02
1111
/ioctl_loop03
12+
/ioctl_loop04
1213
/ioctl_ns01
1314
/ioctl_ns02
1415
/ioctl_ns03
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4+
* Author: Yang Xu <[email protected]>
5+
*
6+
* This is a basic ioctl test about loopdevice.
7+
*
8+
* It is designed to test LOOP_SET_CAPACITY can update a live
9+
* loop device size when we change the size of the underlying
10+
* backing file. Also check sys value.
11+
*/
12+
#include <stdio.h>
13+
#include <unistd.h>
14+
#include <string.h>
15+
#include <stdlib.h>
16+
#include "lapi/loop.h"
17+
#include "tst_test.h"
18+
19+
#define OLD_SIZE 10240
20+
#define NEW_SIZE 5120
21+
22+
static char dev_path[1024], sys_loop_sizepath[1024];
23+
static char *wrbuf;
24+
static int dev_num, dev_fd, file_fd, attach_flag;
25+
26+
static void verify_ioctl_loop(void)
27+
{
28+
struct loop_info loopinfoget;
29+
30+
memset(&loopinfoget, 0, sizeof(loopinfoget));
31+
tst_fill_file("test.img", 0, 1024, OLD_SIZE/1024);
32+
tst_attach_device(dev_path, "test.img");
33+
attach_flag = 1;
34+
35+
TST_ASSERT_INT(sys_loop_sizepath, OLD_SIZE/512);
36+
dev_fd = SAFE_OPEN(dev_path, O_RDWR);
37+
file_fd = SAFE_OPEN("test.img", O_RDWR);
38+
SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
39+
40+
if (loopinfoget.lo_flags & LO_FLAGS_READ_ONLY)
41+
tst_brk(TCONF, "Current environment has unexpected LO_FLAGS_READ_ONLY flag");
42+
43+
SAFE_TRUNCATE("test.img", NEW_SIZE);
44+
SAFE_IOCTL(dev_fd, LOOP_SET_CAPACITY);
45+
46+
/*check that we can't write data beyond 5K into loop device*/
47+
TEST(write(dev_fd, wrbuf, OLD_SIZE));
48+
if (TST_RET == NEW_SIZE) {
49+
tst_res(TPASS, "LOOP_SET_CAPACITY set loop size to %d", NEW_SIZE);
50+
} else {
51+
tst_res(TFAIL, "LOOP_SET_CAPACITY didn't set loop size to %d, its size is %ld",
52+
NEW_SIZE, TST_RET);
53+
}
54+
55+
TST_ASSERT_INT(sys_loop_sizepath, NEW_SIZE/512);
56+
57+
SAFE_CLOSE(file_fd);
58+
SAFE_CLOSE(dev_fd);
59+
tst_detach_device(dev_path);
60+
unlink("test.img");
61+
attach_flag = 0;
62+
}
63+
64+
static void setup(void)
65+
{
66+
dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
67+
if (dev_num < 0)
68+
tst_brk(TBROK, "Failed to find free loop device");
69+
70+
wrbuf = SAFE_MALLOC(OLD_SIZE);
71+
memset(wrbuf, 'x', OLD_SIZE);
72+
sprintf(sys_loop_sizepath, "/sys/block/loop%d/size", dev_num);
73+
}
74+
75+
static void cleanup(void)
76+
{
77+
if (dev_fd > 0)
78+
SAFE_CLOSE(dev_fd);
79+
if (file_fd > 0)
80+
SAFE_CLOSE(file_fd);
81+
if (wrbuf)
82+
free(wrbuf);
83+
if (attach_flag)
84+
tst_detach_device(dev_path);
85+
}
86+
87+
static struct tst_test test = {
88+
.setup = setup,
89+
.cleanup = cleanup,
90+
.test_all = verify_ioctl_loop,
91+
.needs_root = 1,
92+
.needs_tmpdir = 1,
93+
.needs_drivers = (const char *const []) {
94+
"loop",
95+
NULL
96+
}
97+
};

0 commit comments

Comments
 (0)