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