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