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