xref: /aosp_15_r20/external/ltp/include/tst_device.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016-2019 Cyril Hrubis <[email protected]>
4  */
5 
6 #ifndef TST_DEVICE_H__
7 #define TST_DEVICE_H__
8 
9 #include <unistd.h>
10 #include <stdint.h>
11 #include <sys/stat.h>
12 
13 struct tst_device {
14 	const char *dev;
15 	const char *fs_type;
16 	uint64_t size;
17 };
18 
19 /*
20  * Automatically initialized if test.needs_device is set.
21  */
22 extern struct tst_device *tst_device;
23 
24 /*
25  * Just like umount() but retries several times on failure.
26  * @path: Path to umount
27  */
28 int tst_umount(const char *path);
29 
30 /*
31  * Verifies if an earlier mount is successful or not.
32  * @path: Mount path to verify
33  */
34 int tst_is_mounted(const char *path);
35 int tst_is_mounted_at_tmpdir(const char *path);
36 
37 /*
38  * Clears a first few blocks of the device. This is needed when device has
39  * already been formatted with a filesystems, subset of mkfs.foo utils aborts
40  * the operation if it finds a filesystem signature there.
41  *
42  * Note that this is called from tst_mkfs() automatically, so you probably will
43  * not need to use this from the test yourself.
44  */
45 int tst_clear_device(const char *dev);
46 
47 /*
48  * Finds a free loop device for use and returns the free loopdev minor(-1 for no
49  * free loopdev). If path is non-NULL, it will be filled with free loopdev path.
50  *
51  */
52 int tst_find_free_loopdev(char *path, size_t path_len);
53 
54 /*
55  * Attaches a file to a loop device.
56  *
57  * @dev_path Path to the loop device e.g. /dev/loop0
58  * @file_path Path to a file e.g. disk.img
59  * @return Zero on success, non-zero otherwise.
60  */
61 int tst_attach_device(const char *dev_path, const char *file_path);
62 
63 /*
64  * Get size (in MB) of the given device
65  */
66 uint64_t tst_get_device_size(const char *dev_path);
67 
68 /*
69  * Detaches a file from a loop device fd. @dev_fd needs to be the
70  * last descriptor opened. Call to this function will close it,
71  * it is up to caller to open it again for further usage.
72  *
73  * @dev_path Path to the loop device e.g. /dev/loop0
74  * @dev_fd a open fd for the loop device
75  * @return Zero on succes, non-zero otherwise.
76  */
77 int tst_detach_device_by_fd(const char *dev_path, int dev_fd);
78 
79 /*
80  * Detaches a file from a loop device.
81  *
82  * @dev_path Path to the loop device e.g. /dev/loop0
83  * @return Zero on succes, non-zero otherwise.
84  *
85  * Internally this function opens the device and calls
86  * tst_detach_device_by_fd(). If you keep device file descriptor open you
87  * have to call the by_fd() variant since having the device open twice will
88  * prevent it from being detached.
89  */
90 int tst_detach_device(const char *dev_path);
91 
92 /*
93  * To avoid FS deferred IO metadata/cache interference, so we do syncfs
94  * simply before the tst_dev_bytes_written invocation. For easy to use,
95  * we create this inline function tst_dev_sync.
96  */
97 int tst_dev_sync(int fd);
98 
99 /*
100  * Reads test block device stat file and returns the bytes written since the
101  * last call of this function.
102  * @dev: test block device
103  */
104 unsigned long tst_dev_bytes_written(const char *dev);
105 
106 /*
107  * Wipe the contents of given directory but keep the directory itself
108  */
109 void tst_purge_dir(const char *path);
110 
111 /*
112  * Find the file or path belongs to which block dev
113  * @path       Path to find the backing dev
114  * @dev        The buffer to store the block dev in
115  * @dev_size   The length of the block dev buffer
116  */
117 void tst_find_backing_dev(const char *path, char *dev, size_t dev_size);
118 
119 /*
120  * Stat the device mounted on a given path.
121  */
122 void tst_stat_mount_dev(const char *const mnt_path, struct stat *const st);
123 
124 /*
125  * Returns the size of a physical device block size for the specific path
126  * @path   Path to find the block size
127  * @return Size of the block size
128  */
129 int tst_dev_block_size(const char *path);
130 
131 #endif /* TST_DEVICE_H__ */
132