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 #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. 70 * 71 * @dev_path Path to the loop device e.g. /dev/loop0 72 * @dev_fd a open fd for the loop device 73 * @return Zero on succes, non-zero otherwise. 74 */ 75 int tst_detach_device_by_fd(const char *dev_path, int dev_fd); 76 77 /* 78 * Detaches a file from a loop device. 79 * 80 * @dev_path Path to the loop device e.g. /dev/loop0 81 * @return Zero on succes, non-zero otherwise. 82 * 83 * Internally this function opens the device and calls 84 * tst_detach_device_by_fd(). If you keep device file descriptor open you 85 * have to call the by_fd() variant since having the device open twice will 86 * prevent it from being detached. 87 */ 88 int tst_detach_device(const char *dev_path); 89 90 /* 91 * To avoid FS deferred IO metadata/cache interference, so we do syncfs 92 * simply before the tst_dev_bytes_written invocation. For easy to use, 93 * we create this inline function tst_dev_sync. 94 */ 95 int tst_dev_sync(int fd); 96 97 /* 98 * Reads test block device stat file and returns the bytes written since the 99 * last call of this function. 100 * @dev: test block device 101 */ 102 unsigned long tst_dev_bytes_written(const char *dev); 103 104 /* 105 * Wipe the contents of given directory but keep the directory itself 106 */ 107 void tst_purge_dir(const char *path); 108 109 /* 110 * Find the file or path belongs to which block dev 111 * @path Path to find the backing dev 112 * @dev The buffer to store the block dev in 113 * @dev_size The length of the block dev buffer 114 */ 115 void tst_find_backing_dev(const char *path, char *dev, size_t dev_size); 116 117 /* 118 * Stat the device mounted on a given path. 119 */ 120 void tst_stat_mount_dev(const char *const mnt_path, struct stat *const st); 121 122 /* 123 * Returns the size of a physical device block size for the specific path 124 * @path Path to find the block size 125 * @return Size of the block size 126 */ 127 int tst_dev_block_size(const char *path); 128 129 #endif /* TST_DEVICE_H__ */ 130