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