]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/loopdev.h
ec57a8c2f7185e2fa2ea1c7f11933b68ae677f61
[thirdparty/util-linux.git] / include / loopdev.h
1 /*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 */
5 #ifndef UTIL_LINUX_LOOPDEV_H
6 #define UTIL_LINUX_LOOPDEV_H
7
8 #include <stdbool.h>
9 #include "sysfs.h"
10
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
31 #define LOOP_SET_DIRECT_IO 0x4C08
32 #define LOOP_SET_BLOCK_SIZE 0x4C09
33
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
41 /*
42 * loop_info.lo_flags
43 */
44 enum {
45 LO_FLAGS_READ_ONLY = 1,
46 LO_FLAGS_USE_AOPS = 2,
47 LO_FLAGS_AUTOCLEAR = 4, /* kernel >= 2.6.25 */
48 LO_FLAGS_PARTSCAN = 8, /* kernel >= 3.2 */
49 LO_FLAGS_DIRECT_IO = 16, /* kernel >= 4.2 */
50 };
51
52 #define LO_NAME_SIZE 64
53 #define LO_KEY_SIZE 32
54
55 /*
56 * Linux LOOP_{SET,GET}_STATUS64 ioctl struct
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
74 #ifndef LOOP_CONFIGURE
75 /*
76 * Since Linux v5.8-rc1 (commit 3448914e8cc550ba792d4ccc74471d1ca4293aae)
77 */
78 # define LOOP_CONFIGURE 0x4C0A
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
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 */
92 DIR *sysblock; /* /sys/block */
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
99 bool done; /* scanning done */
100 bool default_check; /* check first LOOPDEV_NLOOPS */
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>) */
116 dev_t devno; /* loop device devno from /sys */
117 mode_t mode; /* fd mode O_{RDONLY,RDWR} */
118 uint64_t blocksize; /* used by loopcxt_setup_device() */
119
120 int flags; /* LOOPDEV_FL_* flags */
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 */
126
127 struct path_cxt *sysfs; /* pointer to /sys/dev/block/<maj:min>/ */
128 struct loop_config config; /* for GET/SET ioctl */
129 struct loopdev_iter iter; /* scans /sys or /dev for used/free devices */
130 };
131
132 #define UL_LOOPDEVCXT_EMPTY { .fd = -1 }
133
134 /*
135 * loopdev_cxt.flags
136 */
137 enum {
138 LOOPDEV_FL_OFFSET = (1 << 4),
139 LOOPDEV_FL_NOSYSFS = (1 << 5),
140 LOOPDEV_FL_NOIOCTL = (1 << 6),
141 LOOPDEV_FL_DEVSUBDIR = (1 << 7),
142 LOOPDEV_FL_CONTROL = (1 << 8), /* system with /dev/loop-control */
143 LOOPDEV_FL_SIZELIMIT = (1 << 9)
144 };
145
146 /*
147 * High-level
148 */
149 extern int loopmod_supports_partscan(void);
150
151 extern int is_loopdev(const char *device);
152 extern int loopdev_is_autoclear(const char *device);
153
154 extern char *loopdev_get_backing_file(const char *device);
155 extern dev_t loopcxt_get_devno(struct loopdev_cxt *lc);
156 extern int loopdev_has_backing_file(const char *device);
157 extern int loopcxt_is_lost(struct loopdev_cxt *lc);
158 extern int loopdev_is_used(const char *device, const char *filename,
159 uint64_t offset, uint64_t sizelimit, int flags);
160 extern char *loopdev_find_by_backing_file(const char *filename,
161 uint64_t offset, uint64_t sizelimit, int flags);
162 extern int loopcxt_find_unused(struct loopdev_cxt *lc);
163 extern int loopdev_delete(const char *device);
164 extern int loopdev_count_by_backing_file(const char *filename, char **loopdev);
165
166 /*
167 * Low-level
168 */
169 extern int loopcxt_init(struct loopdev_cxt *lc, int flags)
170 __attribute__ ((warn_unused_result));
171 extern void loopcxt_deinit(struct loopdev_cxt *lc);
172
173 extern int loopcxt_set_device(struct loopdev_cxt *lc, const char *device)
174 __attribute__ ((warn_unused_result));
175 extern int loopcxt_has_device(struct loopdev_cxt *lc);
176 extern int loopcxt_add_device(struct loopdev_cxt *lc);
177 extern char *loopcxt_strdup_device(struct loopdev_cxt *lc);
178 extern const char *loopcxt_get_device(struct loopdev_cxt *lc);
179 extern struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc);
180
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);
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);
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);
197 int loopcxt_set_blocksize(struct loopdev_cxt *lc, uint64_t blocksize);
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);
200 int loopcxt_set_refname(struct loopdev_cxt *lc, const char *refname);
201
202 extern char *loopcxt_get_backing_file(struct loopdev_cxt *lc);
203 extern char *loopcxt_get_refname(struct loopdev_cxt *lc);
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);
206 extern int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset);
207 extern int loopcxt_get_blocksize(struct loopdev_cxt *lc, uint64_t *blocksize);
208 extern int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size);
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);
211 extern int loopcxt_is_autoclear(struct loopdev_cxt *lc);
212 extern int loopcxt_is_readonly(struct loopdev_cxt *lc);
213 extern int loopcxt_is_dio(struct loopdev_cxt *lc);
214 extern int loopcxt_is_partscan(struct loopdev_cxt *lc);
215 extern int loopcxt_find_by_backing_file(struct loopdev_cxt *lc,
216 const char *filename,
217 uint64_t offset, uint64_t sizelimit,
218 int flags);
219 extern int loopcxt_find_overlap(struct loopdev_cxt *lc,
220 const char *filename,
221 uint64_t offset, uint64_t sizelimit);
222
223 extern int loopcxt_is_used(struct loopdev_cxt *lc,
224 struct stat *st,
225 const char *backing_file,
226 uint64_t offset,
227 uint64_t sizelimit,
228 int flags);
229
230 #endif /* UTIL_LINUX_LOOPDEV_H */