Skip to content

Commit

Permalink
lib: tst_test: Add per filesystem mkfs and mount opts
Browse files Browse the repository at this point in the history
This commit does:

* Group the filesystem type, mkfs and mount options into a separate
  structure

* Add an array of these structures to be able to define per filesystem
  mkfs and mount options

The details on the usage should be hopefully clear from the
documentation comments for the struct tst_test.

Reviewed-by: Avinesh Kumar <[email protected]>
Reviewed-by: Petr Vorel <[email protected]>
Acked-by: Andrea Cervesato <[email protected]>
Signed-off-by: Cyril Hrubis <[email protected]>
  • Loading branch information
metan-ucw committed Jul 4, 2024
1 parent 522a452 commit cce6188
Show file tree
Hide file tree
Showing 20 changed files with 235 additions and 87 deletions.
68 changes: 43 additions & 25 deletions include/tst_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,36 @@ struct tst_ulimit_val {
rlim_t rlim_cur;
};

/**
* struct tst_fs - A file system type, mkfs and mount options
*
* @type A filesystem type to use.
*
* @mkfs_opts: A NULL terminated array of options passed to mkfs in the case
* of 'tst_test.format_device'. These options are passed to mkfs
* before the device path.
*
* @mkfs_size_opt: An option passed to mkfs in the case of
* 'tst_test.format_device'. The device size in blocks is
* passed to mkfs after the device path and can be used to
* limit the file system not to use the whole block device.
*
* @mnt_flags: MS_* flags passed to mount(2) when the test library mounts a
* device in the case of 'tst_test.mount_device'.
*
* @mnt_data: The data passed to mount(2) when the test library mounts a device
* in the case of 'tst_test.mount_device'.
*/
struct tst_fs {
const char *type;

const char *const *mkfs_opts;
const char *mkfs_size_opt;

const unsigned int mnt_flags;
const void *mnt_data;
};

/**
* struct tst_test - A test description.
*
Expand Down Expand Up @@ -290,7 +320,7 @@ struct tst_ulimit_val {
* file system at tst_test.mntpoint.
*
* @format_device: Does all tst_test.needs_device would do and also formats
* the device with tst_test.dev_fs_type file system as well.
* the device with a file system as well.
*
* @mount_device: Does all tst_test.format_device would do and also mounts the
* device at tst_test.mntpoint.
Expand Down Expand Up @@ -377,29 +407,22 @@ struct tst_ulimit_val {
*
* @dev_min_size: A minimal device size in megabytes.
*
* @dev_fs_type: If set overrides the default file system type for the device and
* sets the tst_device.fs_type.
*
* @dev_fs_opts: A NULL terminated array of options passed to mkfs in the case
* of 'tst_test.format_device'. These options are passed to mkfs
* before the device path.
*
* @dev_extra_opts: A NULL terminated array of extra options passed to mkfs in
* the case of 'tst_test.format_device'. Extra options are
* passed to mkfs after the device path. Commonly the option
* after mkfs is the number of blocks and can be used to limit
* the file system not to use the whole block device.
* @filesystems: A NULL type terminated array of per file system type
* parameters for mkfs and mount. If the first entry type is NULL
* it describes a default parameters for all file system tests.
* The rest of the entries the describes per file system type
* parameters. If tst_test.all_filesystems is set, the test runs
* for all filesystems and uses the array to lookup the mkfs
* and mount options. If tst_test.all_filesystems is not set
* the test iterates over file system types defined in the array.
* If there is only a single entry in the array with a NULL type,
* the test runs just once for the default file sytem i.e.
* $TST_FS_TYPE.
*
* @mntpoint: A mount point where the test library mounts requested file system.
* The directory is created by the library, the test must not create
* it itself.
*
* @mnt_flags: MS_* flags passed to mount(2) when the test library mounts a
* device in the case of 'tst_test.mount_device'.
*
* @mnt_data: The data passed to mount(2) when the test library mounts a device
* in the case of 'tst_test.mount_device'.
*
* @max_runtime: Maximal test runtime in seconds. Any test that runs for more
* than a second or two should set this and also use
* tst_remaining_runtime() to exit when runtime was used up.
Expand Down Expand Up @@ -516,14 +539,9 @@ struct tst_ulimit_val {

unsigned int dev_min_size;

const char *dev_fs_type;

const char *const *dev_fs_opts;
const char *const *dev_extra_opts;
struct tst_fs *filesystems;

const char *mntpoint;
unsigned int mnt_flags;
void *mnt_data;

int max_runtime;

Expand Down
117 changes: 93 additions & 24 deletions lib/tst_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1097,15 +1097,15 @@ static const char *get_device_name(const char *fs_type)
return tdev.dev;
}

static void prepare_device(void)
static void prepare_device(struct tst_fs *fs)
{
const char *mnt_data;
char buf[1024];

if (tst_test->format_device) {
SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
tst_test->dev_extra_opts);
}
const char *const extra[] = {fs->mkfs_size_opt, NULL};

if (tst_test->format_device)
SAFE_MKFS(tdev.dev, tdev.fs_type, fs->mkfs_opts, extra);

if (tst_test->needs_rofs) {
prepare_and_mount_ro_fs(tdev.dev, tst_test->mntpoint,
Expand All @@ -1114,11 +1114,11 @@ static void prepare_device(void)
}

if (tst_test->mount_device) {
mnt_data = limit_tmpfs_mount_size(tst_test->mnt_data,
mnt_data = limit_tmpfs_mount_size(fs->mnt_data,
buf, sizeof(buf), tdev.fs_type);

SAFE_MOUNT(get_device_name(tdev.fs_type), tst_test->mntpoint,
tdev.fs_type, tst_test->mnt_flags, mnt_data);
tdev.fs_type, fs->mnt_flags, mnt_data);
mntpoint_mounted = 1;
}
}
Expand Down Expand Up @@ -1160,6 +1160,38 @@ static void set_ulimit_(const char *file, const int lineno, const struct tst_uli
safe_setrlimit(file, lineno, conf->resource, &rlim);
}

static unsigned int count_fs_descs(void)
{
unsigned int ret = 0;

if (!tst_test->filesystems)
return 0;

/*
* First entry is special, if it has zero type it's the default entry
* and is either followed by a terminating entry or by filesystem
* description(s) plus terminating entry.
*/
if (!tst_test->filesystems[0].type)
ret = 1;

while (tst_test->filesystems[ret].type)
ret++;

return ret;
}

static const char *default_fs_type(void)
{
if (!tst_test->filesystems)
return tst_dev_fs_type();

if (tst_test->filesystems[0].type)
return tst_test->filesystems[0].type;

return tst_dev_fs_type();
}

static void do_setup(int argc, char *argv[])
{
char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
Expand Down Expand Up @@ -1321,13 +1353,10 @@ static void do_setup(int argc, char *argv[])

tst_device = &tdev;

if (tst_test->dev_fs_type)
tdev.fs_type = tst_test->dev_fs_type;
else
tdev.fs_type = tst_dev_fs_type();
tdev.fs_type = default_fs_type();

if (!tst_test->all_filesystems)
prepare_device();
if (!tst_test->all_filesystems && count_fs_descs() == 1)
prepare_device(&tst_test->filesystems[0]);
}

if (tst_test->needs_overlay && !tst_test->mount_device)
Expand Down Expand Up @@ -1680,6 +1709,52 @@ static int fork_testrun(void)
return 0;
}

static struct tst_fs *lookup_fs_desc(const char *fs_type, int all_filesystems)
{
struct tst_fs *fs = tst_test->filesystems;
static struct tst_fs empty;

if (!fs)
goto ret;

for (; fs->type; fs++) {

if (!fs->type)
continue;

if (!strcmp(fs_type, fs->type))
return fs;
}

ret:
if (!all_filesystems)
return NULL;

if (!tst_test->filesystems || tst_test->filesystems[0].type)
return &empty;

return &tst_test->filesystems[0];
}

static int run_tcase_on_fs(struct tst_fs *fs, const char *fs_type)
{
int ret;

tst_res(TINFO, "=== Testing on %s ===", fs_type);
tdev.fs_type = fs_type;

prepare_device(fs);

ret = fork_testrun();

if (mntpoint_mounted) {
tst_umount(tst_test->mntpoint);
mntpoint_mounted = 0;
}

return ret;
}

static int run_tcases_per_fs(void)
{
int ret = 0;
Expand All @@ -1690,18 +1765,12 @@ static int run_tcases_per_fs(void)
tst_brk(TCONF, "There are no supported filesystems");

for (i = 0; filesystems[i]; i++) {
struct tst_fs *fs = lookup_fs_desc(filesystems[i], tst_test->all_filesystems);

tst_res(TINFO, "=== Testing on %s ===", filesystems[i]);
tdev.fs_type = filesystems[i];

prepare_device();

ret = fork_testrun();
if (!fs)
continue;

if (mntpoint_mounted) {
tst_umount(tst_test->mntpoint);
mntpoint_mounted = 0;
}
run_tcase_on_fs(fs, filesystems[i]);

if (ret == TCONF)
continue;
Expand Down Expand Up @@ -1742,7 +1811,7 @@ void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
test_variants = tst_test->test_variants;

for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
if (tst_test->all_filesystems)
if (tst_test->all_filesystems || count_fs_descs() > 1)
ret |= run_tcases_per_fs();
else
ret |= fork_testrun();
Expand Down
5 changes: 4 additions & 1 deletion testcases/kernel/syscalls/fanotify/fanotify22.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ static struct tst_test test = {
.mount_device = 1,
.mntpoint = MOUNT_PATH,
.needs_root = 1,
.dev_fs_type = "ext4",
.filesystems = (struct tst_fs []){
{.type = "ext4"},
{}
},
.tags = (const struct tst_tag[]) {
{"linux-git", "124e7c61deb2"},
{}
Expand Down
5 changes: 4 additions & 1 deletion testcases/kernel/syscalls/fanotify/fanotify23.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ static struct tst_test test = {
.mount_device = 1,
.mntpoint = MOUNT_PATH,
/* Shrinkers on other fs do not work reliably enough to guarantee mark eviction on drop_caches */
.dev_fs_type = "ext2",
.filesystems = (struct tst_fs []){
{.type = "ext2"},
{}
},
};

#else
Expand Down
5 changes: 4 additions & 1 deletion testcases/kernel/syscalls/getxattr/getxattr04.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ static void setup(void)
static struct tst_test test = {
.needs_root = 1,
.mount_device = 1,
.dev_fs_type = "xfs",
.filesystems = (struct tst_fs []){
{.type = "xfs"},
{}
},
.mntpoint = MNTPOINT,
.forks_child = 1,
.test_all = verify_getxattr,
Expand Down
5 changes: 4 additions & 1 deletion testcases/kernel/syscalls/ioctl/ioctl08.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ static struct tst_test test = {
.needs_root = 1,
.mount_device = 1,
.mntpoint = MNTPOINT,
.dev_fs_type = "btrfs",
.filesystems = (struct tst_fs []) {
{.type = "btrfs"},
{}
},
.needs_drivers = (const char *const[]) {
"btrfs",
NULL,
Expand Down
22 changes: 9 additions & 13 deletions testcases/kernel/syscalls/mmap/mmap16.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,15 @@ static struct tst_test test = {
.needs_checkpoints = 1,
.mount_device = 1,
.mntpoint = MNTPOINT,
.dev_fs_type = "ext4",
.dev_fs_opts = (const char *const[]){
"-b",
"1024",
NULL,
},
.dev_extra_opts = (const char *const[]){
"10240",
NULL,
},
.needs_cmds = (const char *const[]){
"mkfs.ext4",
NULL,
.filesystems = (struct tst_fs []) {
{
.type = "ext4",
.mkfs_opts = (const char *const[]){
"-b", "1024", NULL
},
.mkfs_size_opt = "10240",
},
{}
},
.tags = (const struct tst_tag[]){
{"linux-git", "d6320cbfc929"},
Expand Down
9 changes: 7 additions & 2 deletions testcases/kernel/syscalls/quotactl/quotactl01.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,14 @@ static struct tst_test test = {
.test = verify_quota,
.tcnt = ARRAY_SIZE(tcases),
.mount_device = 1,
.dev_fs_type = "ext4",
.filesystems = (struct tst_fs []) {
{
.type = "ext4",
.mnt_data = "usrquota,grpquota",
},
{}
},
.mntpoint = MNTPOINT,
.mnt_data = "usrquota,grpquota",
.needs_cmds = (const char *const []) {
"quotacheck",
NULL
Expand Down
9 changes: 7 additions & 2 deletions testcases/kernel/syscalls/quotactl/quotactl02.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ static struct tst_test test = {
.test = verify_quota,
.tcnt = ARRAY_SIZE(tcases),
.mount_device = 1,
.dev_fs_type = "xfs",
.filesystems = (struct tst_fs []) {
{
.type = "xfs",
.mnt_data = "usrquota,grpquota",
},
{}
},
.mntpoint = MNTPOINT,
.mnt_data = "usrquota,grpquota",
.setup = setup,
.cleanup = cleanup,
.test_variants = QUOTACTL_SYSCALL_VARIANTS,
Expand Down
Loading

0 comments on commit cce6188

Please sign in to comment.