xref: /aosp_15_r20/external/ltp/lib/newlib_tests/tst_device.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Linux Test Project
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <stdint.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <lapi/loop.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define DEVBLOCKSIZE 2048
18*49cdfc7eSAndroid Build Coastguard Worker #define DEV_MIN_SIZE 310
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static char *mntpoint;
21*49cdfc7eSAndroid Build Coastguard Worker static uint64_t ltp_dev_size;
22*49cdfc7eSAndroid Build Coastguard Worker 
set_block_size(int fd)23*49cdfc7eSAndroid Build Coastguard Worker static int set_block_size(int fd)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker 	return ioctl(fd, LOOP_SET_BLOCK_SIZE, DEVBLOCKSIZE);
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)28*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
31*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	ret = asprintf(&mntpoint, "%s/mnt", tst_get_tmpdir());
34*49cdfc7eSAndroid Build Coastguard Worker 	if (ret < 0)
35*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "asprintf failure");
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(fd, BLKGETSIZE64, &ltp_dev_size);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	TST_RETRY_FN_EXP_BACKOFF(set_block_size(fd), TST_RETVAL_EQ0, 10);
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKFS(tst_device->dev, tst_device->fs_type, NULL, NULL);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(mntpoint, 0777);
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MOUNT(tst_device->dev, mntpoint, tst_device->fs_type, 0, 0);
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)51*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_is_mounted(mntpoint))
54*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UMOUNT(mntpoint);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker 
test_dev_min_size(void)57*49cdfc7eSAndroid Build Coastguard Worker static void test_dev_min_size(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t size;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	size = ltp_dev_size / 1024 / 1024;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	if (size == DEV_MIN_SIZE)
64*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Got expected device size %lu", size);
65*49cdfc7eSAndroid Build Coastguard Worker 	else
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Expected device size is %d but got %lu",
67*49cdfc7eSAndroid Build Coastguard Worker 			DEV_MIN_SIZE, size);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
test_tst_find_backing_dev(void)70*49cdfc7eSAndroid Build Coastguard Worker static void test_tst_find_backing_dev(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	char block_dev[100];
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	tst_find_backing_dev(mntpoint, block_dev, sizeof(block_dev));
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(tst_device->dev, block_dev))
77*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "%s belongs to %s block dev", mntpoint,
78*49cdfc7eSAndroid Build Coastguard Worker 			block_dev);
79*49cdfc7eSAndroid Build Coastguard Worker 	else
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s should belong to %s, but %s is returned",
81*49cdfc7eSAndroid Build Coastguard Worker 			mntpoint, tst_device->dev, block_dev);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker 
test_tst_dev_block_size(void)84*49cdfc7eSAndroid Build Coastguard Worker static void test_tst_dev_block_size(void)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker 	int block_size;
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	block_size = tst_dev_block_size(mntpoint);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	if (block_size == DEVBLOCKSIZE)
91*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "%s has %d block size", mntpoint, block_size);
92*49cdfc7eSAndroid Build Coastguard Worker 	else
93*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s has %d block size, but expected is %i",
94*49cdfc7eSAndroid Build Coastguard Worker 			mntpoint, block_size, DEVBLOCKSIZE);
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker 
do_test(void)97*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
98*49cdfc7eSAndroid Build Coastguard Worker {
99*49cdfc7eSAndroid Build Coastguard Worker 	test_dev_min_size();
100*49cdfc7eSAndroid Build Coastguard Worker 	test_tst_find_backing_dev();
101*49cdfc7eSAndroid Build Coastguard Worker 	test_tst_dev_block_size();
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
105*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
106*49cdfc7eSAndroid Build Coastguard Worker 	.needs_device = 1,
107*49cdfc7eSAndroid Build Coastguard Worker 	.dev_min_size = DEV_MIN_SIZE,
108*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = do_test,
109*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
110*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
111*49cdfc7eSAndroid Build Coastguard Worker 	.min_kver = "4.14",
112*49cdfc7eSAndroid Build Coastguard Worker };
113