diskutil.h (2845B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef ARUU_DISKUTIL_H 3 #define ARUU_DISKUTIL_H 4 5 #include "paths.h" 6 7 #include <stddef.h> 8 #include <stdint.h> 9 #include <sys/stat.h> 10 11 /* The formatter uses 4 KiB blocks and 32-bit on-disk block counts. */ 12 #define MKFS_EXT4_BLOCK_SIZE 4096 13 static inline int 14 mkfs_ext4_size_ok(uint64_t bytes) 15 { 16 return bytes >= (8U << 20) && bytes / MKFS_EXT4_BLOCK_SIZE <= UINT32_MAX; 17 } 18 19 struct BlockDev { 20 int fd; 21 uint64_t size; 22 size_t sec_size; 23 int dirty; // written to: the kernel's view of the table is stale 24 }; 25 26 struct BlockDevInfo { 27 char name[32]; 28 char path[128]; 29 char type[16]; 30 uint64_t size; 31 int major; 32 int minor; 33 char mountpoint[256]; 34 char fstype[32]; 35 char model[64]; // what the disk calls itself, "" when it does not say 36 int removable; // made to be pulled out: a USB stick, a card. a partition takes its disk's 37 struct BlockDevInfo *parts; 38 struct BlockDevInfo *next; 39 }; 40 41 int blockdev_open(struct BlockDev *dev, const char *path, int writable); 42 int blockdev_read(struct BlockDev *dev, uint64_t sector, void *buf, size_t sector_count); 43 int blockdev_write(struct BlockDev *dev, uint64_t sector, const void *buf, size_t sector_count); 44 void blockdev_close(struct BlockDev *dev); 45 46 int blockdev_detect_fs( 47 struct BlockDev *dev, 48 char *type, 49 size_t type_sz, 50 char *label, 51 size_t label_sz, 52 char *uuid, 53 size_t uuid_sz 54 ); 55 56 struct BlockDevInfo *blockdev_get_list(void); 57 void blockdev_free_list(struct BlockDevInfo *list); 58 59 /* a backend per kernel: the size of a device, rereading a table written 60 * to it, and the list of disks. blockdev_probe_list opens the names most 61 * systems give disks, for a kernel that has no way to list them */ 62 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX) 63 void blockdev_check_loop(const struct stat *, const char *); 64 #endif 65 void blockdev_sys_size(struct BlockDev *dev); 66 void blockdev_sys_reread(struct BlockDev *dev); 67 struct BlockDevInfo *blockdev_probe_list(void); 68 69 /* mounting, by backend too. the flags are these whatever a kernel calls 70 * them, and data is the filesystem's own options, NULL for none. each is 71 * -1 with errno set where it fails, ENOSYS where the kernel has no backend */ 72 #define BLOCKDEV_RDONLY 0x1 73 #define BLOCKDEV_NOSUID 0x2 74 #define BLOCKDEV_NODEV 0x4 75 #define BLOCKDEV_NOEXEC 0x8 76 int 77 blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data); 78 int blockdev_unmount(const char *dir); 79 /* the unit holding disk, a name as blockdev_get_list gives it, let go and 80 * powered off so it can be pulled. its filesystems must be unmounted first */ 81 int blockdev_poweroff(const char *disk); 82 83 #endif