bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | #ifndef UTIL_LINUX_LOOPDEV_H |
| 2 | #define UTIL_LINUX_LOOPDEV_H |
| 3 | |
| 4 | #include "sysfs.h" |
| 5 | |
| 6 | /* |
| 7 | * loop_info.lo_encrypt_type |
| 8 | */ |
| 9 | #define LO_CRYPT_NONE 0 |
| 10 | #define LO_CRYPT_XOR 1 |
| 11 | #define LO_CRYPT_DES 2 |
| 12 | #define LO_CRYPT_CRYPTOAPI 18 |
| 13 | |
| 14 | #define LOOP_SET_FD 0x4C00 |
| 15 | #define LOOP_CLR_FD 0x4C01 |
| 16 | /* |
| 17 | * Obsolete (kernel < 2.6) |
| 18 | * |
| 19 | * #define LOOP_SET_STATUS 0x4C02 |
| 20 | * #define LOOP_GET_STATUS 0x4C03 |
| 21 | */ |
| 22 | #define LOOP_SET_STATUS64 0x4C04 |
| 23 | #define LOOP_GET_STATUS64 0x4C05 |
| 24 | /* #define LOOP_CHANGE_FD 0x4C06 */ |
| 25 | #define LOOP_SET_CAPACITY 0x4C07 |
| 26 | |
| 27 | /* /dev/loop-control interface */ |
| 28 | #ifndef LOOP_CTL_ADD |
| 29 | # define LOOP_CTL_ADD 0x4C80 |
| 30 | # define LOOP_CTL_REMOVE 0x4C81 |
| 31 | # define LOOP_CTL_GET_FREE 0x4C82 |
| 32 | #endif |
| 33 | |
| 34 | /* |
| 35 | * loop_info.lo_flags |
| 36 | */ |
| 37 | enum { |
| 38 | LO_FLAGS_READ_ONLY = 1, |
| 39 | LO_FLAGS_USE_AOPS = 2, |
| 40 | LO_FLAGS_AUTOCLEAR = 4, /* kernel >= 2.6.25 */ |
| 41 | LO_FLAGS_PARTSCAN = 8, /* kernel >= 3.2 */ |
| 42 | }; |
| 43 | |
| 44 | #define LO_NAME_SIZE 64 |
| 45 | #define LO_KEY_SIZE 32 |
| 46 | |
| 47 | /* |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 48 | * Linux LOOP_{SET,GET}_STATUS64 ioctl struct |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 49 | */ |
| 50 | struct loop_info64 { |
| 51 | uint64_t lo_device; |
| 52 | uint64_t lo_inode; |
| 53 | uint64_t lo_rdevice; |
| 54 | uint64_t lo_offset; |
| 55 | uint64_t lo_sizelimit; /* bytes, 0 == max available */ |
| 56 | uint32_t lo_number; |
| 57 | uint32_t lo_encrypt_type; |
| 58 | uint32_t lo_encrypt_key_size; |
| 59 | uint32_t lo_flags; |
| 60 | uint8_t lo_file_name[LO_NAME_SIZE]; |
| 61 | uint8_t lo_crypt_name[LO_NAME_SIZE]; |
| 62 | uint8_t lo_encrypt_key[LO_KEY_SIZE]; |
| 63 | uint64_t lo_init[2]; |
| 64 | }; |
| 65 | |
| 66 | #define LOOPDEV_MAJOR 7 /* loop major number */ |
| 67 | #define LOOPDEV_DEFAULT_NNODES 8 /* default number of loop devices */ |
| 68 | |
| 69 | struct loopdev_iter { |
| 70 | FILE *proc; /* /proc/partitions */ |
| 71 | DIR *sysblock; /* /sys/block */ |
| 72 | int ncur; /* current position */ |
| 73 | int *minors; /* ary of minor numbers (when scan whole /dev) */ |
| 74 | int nminors; /* number of items in *minors */ |
| 75 | int ct_perm; /* count permission problems */ |
| 76 | int ct_succ; /* count number of detected devices */ |
| 77 | |
| 78 | unsigned int done:1; /* scanning done */ |
| 79 | unsigned int default_check:1;/* check first LOOPDEV_NLOOPS */ |
| 80 | int flags; /* LOOPITER_FL_* flags */ |
| 81 | }; |
| 82 | |
| 83 | enum { |
| 84 | LOOPITER_FL_FREE = (1 << 0), |
| 85 | LOOPITER_FL_USED = (1 << 1) |
| 86 | }; |
| 87 | |
| 88 | /* |
| 89 | * handler for work with loop devices |
| 90 | */ |
| 91 | struct loopdev_cxt { |
| 92 | char device[128]; /* device path (e.g. /dev/loop<N>) */ |
| 93 | char *filename; /* backing file for loopcxt_set_... */ |
| 94 | int fd; /* open(/dev/looo<N>) */ |
| 95 | int mode; /* fd mode O_{RDONLY,RDWR} */ |
| 96 | |
| 97 | int flags; /* LOOPDEV_FL_* flags */ |
| 98 | unsigned int has_info:1; /* .info contains data */ |
| 99 | unsigned int extra_check:1; /* unusual stuff for iterator */ |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 100 | unsigned int info_failed:1; /* LOOP_GET_STATUS ioctl failed */ |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 101 | unsigned int control_ok:1; /* /dev/loop-control success */ |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 102 | |
| 103 | struct sysfs_cxt sysfs; /* pointer to /sys/dev/block/<maj:min>/ */ |
| 104 | struct loop_info64 info; /* for GET/SET ioctl */ |
| 105 | struct loopdev_iter iter; /* scans /sys or /dev for used/free devices */ |
| 106 | }; |
| 107 | |
| 108 | #define UL_LOOPDEVCXT_EMPTY { .fd = -1, .sysfs = UL_SYSFSCXT_EMPTY } |
| 109 | |
| 110 | /* |
| 111 | * loopdev_cxt.flags |
| 112 | */ |
| 113 | enum { |
| 114 | LOOPDEV_FL_RDONLY = (1 << 0), /* open(/dev/loop) mode; default */ |
| 115 | LOOPDEV_FL_RDWR = (1 << 1), /* necessary for loop setup only */ |
| 116 | LOOPDEV_FL_OFFSET = (1 << 4), |
| 117 | LOOPDEV_FL_NOSYSFS = (1 << 5), |
| 118 | LOOPDEV_FL_NOIOCTL = (1 << 6), |
| 119 | LOOPDEV_FL_DEVSUBDIR = (1 << 7), |
| 120 | LOOPDEV_FL_CONTROL = (1 << 8), /* system with /dev/loop-control */ |
| 121 | LOOPDEV_FL_SIZELIMIT = (1 << 9) |
| 122 | }; |
| 123 | |
| 124 | /* |
| 125 | * High-level |
| 126 | */ |
| 127 | extern int loopmod_supports_partscan(void); |
| 128 | |
| 129 | extern int is_loopdev(const char *device); |
| 130 | extern int loopdev_is_autoclear(const char *device); |
| 131 | |
| 132 | extern char *loopdev_get_backing_file(const char *device); |
| 133 | extern int loopdev_is_used(const char *device, const char *filename, |
| 134 | uint64_t offset, int flags); |
| 135 | extern char *loopdev_find_by_backing_file(const char *filename, |
| 136 | uint64_t offset, int flags); |
| 137 | extern int loopcxt_find_unused(struct loopdev_cxt *lc); |
| 138 | extern int loopdev_delete(const char *device); |
| 139 | extern int loopdev_count_by_backing_file(const char *filename, char **loopdev); |
| 140 | |
| 141 | /* |
| 142 | * Low-level |
| 143 | */ |
| 144 | extern int loopcxt_init(struct loopdev_cxt *lc, int flags) |
| 145 | __attribute__ ((warn_unused_result)); |
| 146 | extern void loopcxt_deinit(struct loopdev_cxt *lc); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 147 | |
| 148 | extern int loopcxt_set_device(struct loopdev_cxt *lc, const char *device) |
| 149 | __attribute__ ((warn_unused_result)); |
| 150 | extern int loopcxt_has_device(struct loopdev_cxt *lc); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 151 | extern int loopcxt_add_device(struct loopdev_cxt *lc); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 152 | extern char *loopcxt_strdup_device(struct loopdev_cxt *lc); |
| 153 | extern const char *loopcxt_get_device(struct loopdev_cxt *lc); |
| 154 | extern struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc); |
| 155 | extern struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc); |
| 156 | |
| 157 | extern int loopcxt_get_fd(struct loopdev_cxt *lc); |
| 158 | extern int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, int mode); |
| 159 | |
| 160 | extern int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags); |
| 161 | extern int loopcxt_deinit_iterator(struct loopdev_cxt *lc); |
| 162 | extern int loopcxt_next(struct loopdev_cxt *lc); |
| 163 | |
| 164 | extern int loopcxt_setup_device(struct loopdev_cxt *lc); |
| 165 | extern int loopcxt_delete_device(struct loopdev_cxt *lc); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 166 | extern int loopcxt_set_capacity(struct loopdev_cxt *lc); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 167 | |
| 168 | int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset); |
| 169 | int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit); |
| 170 | int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags); |
| 171 | int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename); |
| 172 | |
| 173 | extern char *loopcxt_get_backing_file(struct loopdev_cxt *lc); |
| 174 | extern int loopcxt_get_backing_devno(struct loopdev_cxt *lc, dev_t *devno); |
| 175 | extern int loopcxt_get_backing_inode(struct loopdev_cxt *lc, ino_t *ino); |
| 176 | extern int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset); |
| 177 | extern int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size); |
| 178 | extern int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type); |
| 179 | extern const char *loopcxt_get_crypt_name(struct loopdev_cxt *lc); |
| 180 | extern int loopcxt_is_autoclear(struct loopdev_cxt *lc); |
| 181 | extern int loopcxt_is_readonly(struct loopdev_cxt *lc); |
| 182 | extern int loopcxt_is_partscan(struct loopdev_cxt *lc); |
| 183 | extern int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, |
| 184 | const char *filename, |
| 185 | uint64_t offset, int flags); |
| 186 | |
| 187 | extern int loopcxt_is_used(struct loopdev_cxt *lc, |
| 188 | struct stat *st, |
| 189 | const char *backing_file, |
| 190 | uint64_t offset, |
| 191 | int flags); |
| 192 | |
| 193 | #endif /* UTIL_LINUX_LOOPDEV_H */ |