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