]>
Commit | Line | Data |
---|---|---|
faeb1b64 KZ |
1 | /* |
2 | * No copyright is claimed. This code is in the public domain; do with | |
3 | * it what you wish. | |
4 | */ | |
10ee5932 KZ |
5 | #ifndef UTIL_LINUX_LOOPDEV_H |
6 | #define UTIL_LINUX_LOOPDEV_H | |
7 | ||
63d79371 | 8 | #include <stdbool.h> |
fd7f0718 KZ |
9 | #include "sysfs.h" |
10 | ||
10ee5932 KZ |
11 | /* |
12 | * loop_info.lo_encrypt_type | |
13 | */ | |
14 | #define LO_CRYPT_NONE 0 | |
15 | #define LO_CRYPT_XOR 1 | |
16 | #define LO_CRYPT_DES 2 | |
17 | #define LO_CRYPT_CRYPTOAPI 18 | |
18 | ||
19 | #define LOOP_SET_FD 0x4C00 | |
20 | #define LOOP_CLR_FD 0x4C01 | |
21 | /* | |
22 | * Obsolete (kernel < 2.6) | |
23 | * | |
24 | * #define LOOP_SET_STATUS 0x4C02 | |
25 | * #define LOOP_GET_STATUS 0x4C03 | |
26 | */ | |
27 | #define LOOP_SET_STATUS64 0x4C04 | |
28 | #define LOOP_GET_STATUS64 0x4C05 | |
29 | /* #define LOOP_CHANGE_FD 0x4C06 */ | |
30 | #define LOOP_SET_CAPACITY 0x4C07 | |
64c3bb3c | 31 | #define LOOP_SET_DIRECT_IO 0x4C08 |
a1a41597 | 32 | #define LOOP_SET_BLOCK_SIZE 0x4C09 |
10ee5932 | 33 | |
0b14bf7a KZ |
34 | /* /dev/loop-control interface */ |
35 | #ifndef LOOP_CTL_ADD | |
36 | # define LOOP_CTL_ADD 0x4C80 | |
37 | # define LOOP_CTL_REMOVE 0x4C81 | |
38 | # define LOOP_CTL_GET_FREE 0x4C82 | |
39 | #endif | |
40 | ||
10ee5932 KZ |
41 | /* |
42 | * loop_info.lo_flags | |
43 | */ | |
44 | enum { | |
45 | LO_FLAGS_READ_ONLY = 1, | |
46 | LO_FLAGS_USE_AOPS = 2, | |
916bf85e KZ |
47 | LO_FLAGS_AUTOCLEAR = 4, /* kernel >= 2.6.25 */ |
48 | LO_FLAGS_PARTSCAN = 8, /* kernel >= 3.2 */ | |
faeef4d2 | 49 | LO_FLAGS_DIRECT_IO = 16, /* kernel >= 4.2 */ |
10ee5932 KZ |
50 | }; |
51 | ||
52 | #define LO_NAME_SIZE 64 | |
53 | #define LO_KEY_SIZE 32 | |
54 | ||
55 | /* | |
a5c523a0 | 56 | * Linux LOOP_{SET,GET}_STATUS64 ioctl struct |
10ee5932 KZ |
57 | */ |
58 | struct loop_info64 { | |
59 | uint64_t lo_device; | |
60 | uint64_t lo_inode; | |
61 | uint64_t lo_rdevice; | |
62 | uint64_t lo_offset; | |
63 | uint64_t lo_sizelimit; /* bytes, 0 == max available */ | |
64 | uint32_t lo_number; | |
65 | uint32_t lo_encrypt_type; | |
66 | uint32_t lo_encrypt_key_size; | |
67 | uint32_t lo_flags; | |
68 | uint8_t lo_file_name[LO_NAME_SIZE]; | |
69 | uint8_t lo_crypt_name[LO_NAME_SIZE]; | |
70 | uint8_t lo_encrypt_key[LO_KEY_SIZE]; | |
71 | uint64_t lo_init[2]; | |
72 | }; | |
73 | ||
d5fd456c | 74 | #ifndef LOOP_CONFIGURE |
a61955ba KZ |
75 | /* |
76 | * Since Linux v5.8-rc1 (commit 3448914e8cc550ba792d4ccc74471d1ca4293aae) | |
77 | */ | |
78 | # define LOOP_CONFIGURE 0x4C0A | |
d5fd456c SK |
79 | struct loop_config { |
80 | uint32_t fd; | |
81 | uint32_t block_size; | |
82 | struct loop_info64 info; | |
83 | uint64_t __reserved[8]; | |
84 | }; | |
85 | #endif | |
86 | ||
10ee5932 KZ |
87 | #define LOOPDEV_MAJOR 7 /* loop major number */ |
88 | #define LOOPDEV_DEFAULT_NNODES 8 /* default number of loop devices */ | |
89 | ||
90 | struct loopdev_iter { | |
91 | FILE *proc; /* /proc/partitions */ | |
e4062c72 | 92 | DIR *sysblock; /* /sys/block */ |
10ee5932 KZ |
93 | int ncur; /* current position */ |
94 | int *minors; /* ary of minor numbers (when scan whole /dev) */ | |
95 | int nminors; /* number of items in *minors */ | |
96 | int ct_perm; /* count permission problems */ | |
97 | int ct_succ; /* count number of detected devices */ | |
98 | ||
63d79371 ZJS |
99 | bool done; /* scanning done */ |
100 | bool default_check; /* check first LOOPDEV_NLOOPS */ | |
10ee5932 KZ |
101 | int flags; /* LOOPITER_FL_* flags */ |
102 | }; | |
103 | ||
104 | enum { | |
105 | LOOPITER_FL_FREE = (1 << 0), | |
106 | LOOPITER_FL_USED = (1 << 1) | |
107 | }; | |
108 | ||
109 | /* | |
110 | * handler for work with loop devices | |
111 | */ | |
112 | struct loopdev_cxt { | |
113 | char device[128]; /* device path (e.g. /dev/loop<N>) */ | |
114 | char *filename; /* backing file for loopcxt_set_... */ | |
115 | int fd; /* open(/dev/looo<N>) */ | |
a6ca0456 | 116 | dev_t devno; /* loop device devno from /sys */ |
ced1142d | 117 | mode_t mode; /* fd mode O_{RDONLY,RDWR} */ |
75d239ff | 118 | uint64_t blocksize; /* used by loopcxt_setup_device() */ |
10ee5932 KZ |
119 | |
120 | int flags; /* LOOPDEV_FL_* flags */ | |
63d79371 ZJS |
121 | bool has_info; /* .info contains data */ |
122 | bool extra_check; /* unusual stuff for iterator */ | |
123 | bool info_failed; /* LOOP_GET_STATUS ioctl failed */ | |
124 | bool control_ok; /* /dev/loop-control success */ | |
125 | bool is_lost; /* device in /sys, but missing in /dev */ | |
10ee5932 | 126 | |
7604f85f | 127 | struct path_cxt *sysfs; /* pointer to /sys/dev/block/<maj:min>/ */ |
db8f7815 | 128 | struct loop_config config; /* for GET/SET ioctl */ |
10ee5932 KZ |
129 | struct loopdev_iter iter; /* scans /sys or /dev for used/free devices */ |
130 | }; | |
131 | ||
7604f85f | 132 | #define UL_LOOPDEVCXT_EMPTY { .fd = -1 } |
6219c25e | 133 | |
10ee5932 KZ |
134 | /* |
135 | * loopdev_cxt.flags | |
136 | */ | |
137 | enum { | |
10ee5932 KZ |
138 | LOOPDEV_FL_OFFSET = (1 << 4), |
139 | LOOPDEV_FL_NOSYSFS = (1 << 5), | |
140 | LOOPDEV_FL_NOIOCTL = (1 << 6), | |
0b14bf7a KZ |
141 | LOOPDEV_FL_DEVSUBDIR = (1 << 7), |
142 | LOOPDEV_FL_CONTROL = (1 << 8), /* system with /dev/loop-control */ | |
c7e0925d | 143 | LOOPDEV_FL_SIZELIMIT = (1 << 9) |
10ee5932 KZ |
144 | }; |
145 | ||
146 | /* | |
147 | * High-level | |
148 | */ | |
59d749c3 KZ |
149 | extern int loopmod_supports_partscan(void); |
150 | ||
10ee5932 KZ |
151 | extern int is_loopdev(const char *device); |
152 | extern int loopdev_is_autoclear(const char *device); | |
6c224de1 | 153 | |
10ee5932 | 154 | extern char *loopdev_get_backing_file(const char *device); |
a6ca0456 | 155 | extern dev_t loopcxt_get_devno(struct loopdev_cxt *lc); |
14bb8e3c | 156 | extern int loopdev_has_backing_file(const char *device); |
a6ca0456 | 157 | extern int loopcxt_is_lost(struct loopdev_cxt *lc); |
10ee5932 | 158 | extern int loopdev_is_used(const char *device, const char *filename, |
74a4705a | 159 | uint64_t offset, uint64_t sizelimit, int flags); |
10ee5932 | 160 | extern char *loopdev_find_by_backing_file(const char *filename, |
74a4705a | 161 | uint64_t offset, uint64_t sizelimit, int flags); |
10ee5932 KZ |
162 | extern int loopcxt_find_unused(struct loopdev_cxt *lc); |
163 | extern int loopdev_delete(const char *device); | |
d5688130 | 164 | extern int loopdev_count_by_backing_file(const char *filename, char **loopdev); |
10ee5932 KZ |
165 | |
166 | /* | |
167 | * Low-level | |
168 | */ | |
defa0710 KZ |
169 | extern int loopcxt_init(struct loopdev_cxt *lc, int flags) |
170 | __attribute__ ((warn_unused_result)); | |
10ee5932 KZ |
171 | extern void loopcxt_deinit(struct loopdev_cxt *lc); |
172 | ||
defa0710 KZ |
173 | extern int loopcxt_set_device(struct loopdev_cxt *lc, const char *device) |
174 | __attribute__ ((warn_unused_result)); | |
c7e0925d | 175 | extern int loopcxt_has_device(struct loopdev_cxt *lc); |
3cb2413b | 176 | extern int loopcxt_add_device(struct loopdev_cxt *lc); |
10ee5932 KZ |
177 | extern char *loopcxt_strdup_device(struct loopdev_cxt *lc); |
178 | extern const char *loopcxt_get_device(struct loopdev_cxt *lc); | |
c8ea3514 | 179 | extern struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc); |
fd7f0718 | 180 | |
10ee5932 KZ |
181 | extern int loopcxt_get_fd(struct loopdev_cxt *lc); |
182 | ||
183 | extern int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags); | |
184 | extern int loopcxt_deinit_iterator(struct loopdev_cxt *lc); | |
185 | extern int loopcxt_next(struct loopdev_cxt *lc); | |
186 | ||
187 | extern int loopcxt_setup_device(struct loopdev_cxt *lc); | |
188 | extern int loopcxt_delete_device(struct loopdev_cxt *lc); | |
9fcc8936 KZ |
189 | |
190 | extern int loopcxt_ioctl_status(struct loopdev_cxt *lc); | |
191 | extern int loopcxt_ioctl_capacity(struct loopdev_cxt *lc); | |
192 | extern int loopcxt_ioctl_dio(struct loopdev_cxt *lc, unsigned long use_dio); | |
193 | extern int loopcxt_ioctl_blocksize(struct loopdev_cxt *lc, uint64_t blocksize); | |
10ee5932 KZ |
194 | |
195 | int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset); | |
196 | int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit); | |
75d239ff | 197 | int loopcxt_set_blocksize(struct loopdev_cxt *lc, uint64_t blocksize); |
10ee5932 KZ |
198 | int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags); |
199 | int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename); | |
db8f7815 | 200 | int loopcxt_set_refname(struct loopdev_cxt *lc, const char *refname); |
10ee5932 KZ |
201 | |
202 | extern char *loopcxt_get_backing_file(struct loopdev_cxt *lc); | |
db8f7815 | 203 | extern char *loopcxt_get_refname(struct loopdev_cxt *lc); |
6c224de1 KZ |
204 | extern int loopcxt_get_backing_devno(struct loopdev_cxt *lc, dev_t *devno); |
205 | extern int loopcxt_get_backing_inode(struct loopdev_cxt *lc, ino_t *ino); | |
10ee5932 | 206 | extern int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset); |
a1a41597 | 207 | extern int loopcxt_get_blocksize(struct loopdev_cxt *lc, uint64_t *blocksize); |
10ee5932 | 208 | extern int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size); |
6c224de1 KZ |
209 | extern int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type); |
210 | extern const char *loopcxt_get_crypt_name(struct loopdev_cxt *lc); | |
10ee5932 | 211 | extern int loopcxt_is_autoclear(struct loopdev_cxt *lc); |
fd7f0718 | 212 | extern int loopcxt_is_readonly(struct loopdev_cxt *lc); |
faeef4d2 | 213 | extern int loopcxt_is_dio(struct loopdev_cxt *lc); |
59d749c3 | 214 | extern int loopcxt_is_partscan(struct loopdev_cxt *lc); |
10ee5932 KZ |
215 | extern int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, |
216 | const char *filename, | |
74a4705a SB |
217 | uint64_t offset, uint64_t sizelimit, |
218 | int flags); | |
c444a71b KZ |
219 | extern int loopcxt_find_overlap(struct loopdev_cxt *lc, |
220 | const char *filename, | |
221 | uint64_t offset, uint64_t sizelimit); | |
10ee5932 | 222 | |
6c224de1 KZ |
223 | extern int loopcxt_is_used(struct loopdev_cxt *lc, |
224 | struct stat *st, | |
225 | const char *backing_file, | |
226 | uint64_t offset, | |
74a4705a | 227 | uint64_t sizelimit, |
6c224de1 KZ |
228 | int flags); |
229 | ||
10ee5932 | 230 | #endif /* UTIL_LINUX_LOOPDEV_H */ |