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