]>
Commit | Line | Data |
---|---|---|
10ee5932 | 1 | /* |
0f23ee0c KZ |
2 | * No copyright is claimed. This code is in the public domain; do with |
3 | * it what you wish. | |
4 | * | |
5 | * Written by Karel Zak <kzak@redhat.com> | |
10ee5932 KZ |
6 | * |
7 | * -- based on mount/losetup.c | |
8 | * | |
9 | * Simple library for work with loop devices. | |
10 | * | |
11 | * - requires kernel 2.6.x | |
12 | * - reads info from /sys/block/loop<N>/loop/<attr> (new kernels) | |
13 | * - reads info by ioctl | |
14 | * - supports *unlimited* number of loop devices | |
15 | * - supports /dev/loop<N> as well as /dev/loop/<N> | |
16 | * - minimize overhead (fd, loopinfo, ... are shared for all operations) | |
17 | * - setup (associate device and backing file) | |
18 | * - delete (dis-associate file) | |
19 | * - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported | |
20 | * - extendible | |
21 | */ | |
22 | #include <stdio.h> | |
23 | #include <stdint.h> | |
24 | #include <string.h> | |
25 | #include <ctype.h> | |
26 | #include <fcntl.h> | |
10ee5932 KZ |
27 | #include <stdlib.h> |
28 | #include <unistd.h> | |
29 | #include <sys/ioctl.h> | |
30 | #include <sys/stat.h> | |
31 | #include <sys/mman.h> | |
10ee5932 KZ |
32 | #include <inttypes.h> |
33 | #include <dirent.h> | |
34 | #include <linux/posix_types.h> | |
35 | ||
36 | #include "linux_version.h" | |
37 | #include "c.h" | |
38 | #include "sysfs.h" | |
39 | #include "pathnames.h" | |
40 | #include "loopdev.h" | |
41 | #include "canonicalize.h" | |
293714c0 | 42 | #include "blkdev.h" |
0bf03740 | 43 | #include "debug.h" |
10ee5932 | 44 | |
0bf03740 KZ |
45 | /* |
46 | * Debug stuff (based on include/debug.h) | |
47 | */ | |
48 | UL_DEBUG_DEFINE_MASK(loopdev); | |
49 | UL_DEBUG_DEFINE_MASKNAMES(loopdev) = UL_DEBUG_EMPTY_MASKNAMES; | |
aee31ddc | 50 | |
0bf03740 KZ |
51 | #define LOOPDEV_DEBUG_INIT (1 << 1) |
52 | #define LOOPDEV_DEBUG_CXT (1 << 2) | |
53 | #define LOOPDEV_DEBUG_ITER (1 << 3) | |
54 | #define LOOPDEV_DEBUG_SETUP (1 << 4) | |
55 | #define SFDISKPROG_DEBUG_ALL 0xFFFF | |
aee31ddc | 56 | |
0bf03740 KZ |
57 | #define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x) |
58 | #define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x) | |
aee31ddc | 59 | |
0bf03740 | 60 | static void loopdev_init_debug(void) |
aee31ddc | 61 | { |
0bf03740 KZ |
62 | if (loopdev_debug_mask) |
63 | return; | |
64 | __UL_INIT_DEBUG(loopdev, LOOPDEV_DEBUG_, 0, LOOPDEV_DEBUG); | |
aee31ddc KZ |
65 | } |
66 | ||
e4062c72 KZ |
67 | /* |
68 | * see loopcxt_init() | |
69 | */ | |
10ee5932 | 70 | #define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL)) |
e4062c72 KZ |
71 | #define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \ |
72 | && !loopcxt_ioctl_enabled(_lc) | |
10ee5932 KZ |
73 | |
74 | /* | |
75 | * @lc: context | |
76 | * @device: device name, absolute device path or NULL to reset the current setting | |
77 | * | |
78 | * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device | |
79 | * names ("loop<N>") are converted to the path (/dev/loop<N> or to | |
80 | * /dev/loop/<N>) | |
81 | * | |
85794fb0 KZ |
82 | * This sets the device name, but does not check if the device exists! |
83 | * | |
10ee5932 KZ |
84 | * Returns: <0 on error, 0 on success |
85 | */ | |
86 | int loopcxt_set_device(struct loopdev_cxt *lc, const char *device) | |
87 | { | |
88 | if (!lc) | |
89 | return -EINVAL; | |
90 | ||
f4bf9592 | 91 | if (lc->fd >= 0) { |
10ee5932 | 92 | close(lc->fd); |
0bf03740 | 93 | DBG(CXT, ul_debugobj(lc, "closing old open fd")); |
f4bf9592 | 94 | } |
10ee5932 | 95 | lc->fd = -1; |
fd7f0718 | 96 | lc->mode = 0; |
10ee5932 | 97 | lc->has_info = 0; |
6c224de1 | 98 | lc->info_failed = 0; |
10ee5932 | 99 | *lc->device = '\0'; |
fd7f0718 | 100 | memset(&lc->info, 0, sizeof(lc->info)); |
10ee5932 KZ |
101 | |
102 | /* set new */ | |
103 | if (device) { | |
104 | if (*device != '/') { | |
105 | const char *dir = _PATH_DEV; | |
106 | ||
107 | /* compose device name for /dev/loop<n> or /dev/loop/<n> */ | |
108 | if (lc->flags & LOOPDEV_FL_DEVSUBDIR) { | |
109 | if (strlen(device) < 5) | |
110 | return -1; | |
111 | device += 4; | |
112 | dir = _PATH_DEV_LOOP "/"; /* _PATH_DEV uses tailing slash */ | |
113 | } | |
114 | snprintf(lc->device, sizeof(lc->device), "%s%s", | |
115 | dir, device); | |
116 | } else { | |
117 | strncpy(lc->device, device, sizeof(lc->device)); | |
118 | lc->device[sizeof(lc->device) - 1] = '\0'; | |
119 | } | |
0bf03740 | 120 | DBG(CXT, ul_debugobj(lc, "%s name assigned", device)); |
10ee5932 KZ |
121 | } |
122 | ||
123 | sysfs_deinit(&lc->sysfs); | |
124 | return 0; | |
125 | } | |
126 | ||
c7e0925d KZ |
127 | int loopcxt_has_device(struct loopdev_cxt *lc) |
128 | { | |
129 | return lc && *lc->device; | |
130 | } | |
131 | ||
10ee5932 KZ |
132 | /* |
133 | * @lc: context | |
134 | * @flags: LOOPDEV_FL_* flags | |
135 | * | |
3fd1f771 | 136 | * Initialize loop handler. |
10ee5932 | 137 | * |
fd7f0718 KZ |
138 | * We have two sets of the flags: |
139 | * | |
140 | * * LOOPDEV_FL_* flags control loopcxt_* API behavior | |
141 | * | |
142 | * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls | |
143 | * | |
144 | * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2) | |
145 | * syscall to open loop device. By default is the device open read-only. | |
146 | * | |
147 | * The expection is loopcxt_setup_device(), where the device is open read-write | |
148 | * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()). | |
149 | * | |
10ee5932 KZ |
150 | * Returns: <0 on error, 0 on success. |
151 | */ | |
152 | int loopcxt_init(struct loopdev_cxt *lc, int flags) | |
153 | { | |
defa0710 | 154 | int rc; |
0b14bf7a | 155 | struct stat st; |
6219c25e | 156 | struct loopdev_cxt dummy = UL_LOOPDEVCXT_EMPTY; |
0b14bf7a | 157 | |
10ee5932 KZ |
158 | if (!lc) |
159 | return -EINVAL; | |
160 | ||
0bf03740 KZ |
161 | loopdev_init_debug(); |
162 | DBG(CXT, ul_debugobj(lc, "initialize context")); | |
163 | ||
6219c25e | 164 | memcpy(lc, &dummy, sizeof(dummy)); |
10ee5932 | 165 | lc->flags = flags; |
defa0710 KZ |
166 | |
167 | rc = loopcxt_set_device(lc, NULL); | |
168 | if (rc) | |
169 | return rc; | |
10ee5932 | 170 | |
df0f2ad7 KZ |
171 | if (stat(_PATH_SYS_BLOCK, &st) || !S_ISDIR(st.st_mode)) { |
172 | lc->flags |= LOOPDEV_FL_NOSYSFS; | |
173 | lc->flags &= ~LOOPDEV_FL_NOIOCTL; | |
0bf03740 | 174 | DBG(CXT, ul_debugobj(lc, "init: disable /sys usage")); |
df0f2ad7 KZ |
175 | } |
176 | ||
82b4082e | 177 | if (!(lc->flags & LOOPDEV_FL_NOSYSFS) && |
f4bf9592 | 178 | get_linux_version() >= KERNEL_VERSION(2,6,37)) { |
10ee5932 KZ |
179 | /* |
180 | * Use only sysfs for basic information about loop devices | |
181 | */ | |
182 | lc->flags |= LOOPDEV_FL_NOIOCTL; | |
0bf03740 | 183 | DBG(CXT, ul_debugobj(lc, "init: ignore ioctls")); |
f4bf9592 | 184 | } |
10ee5932 | 185 | |
f4bf9592 | 186 | if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) { |
0b14bf7a | 187 | lc->flags |= LOOPDEV_FL_CONTROL; |
0bf03740 | 188 | DBG(CXT, ul_debugobj(lc, "init: loop-control detected ")); |
f4bf9592 | 189 | } |
0b14bf7a | 190 | |
10ee5932 KZ |
191 | return 0; |
192 | } | |
193 | ||
194 | /* | |
195 | * @lc: context | |
196 | * | |
197 | * Deinitialize loop context | |
198 | */ | |
199 | void loopcxt_deinit(struct loopdev_cxt *lc) | |
200 | { | |
82756a74 KZ |
201 | int errsv = errno; |
202 | ||
10ee5932 KZ |
203 | if (!lc) |
204 | return; | |
205 | ||
0bf03740 | 206 | DBG(CXT, ul_debugobj(lc, "de-initialize")); |
aee31ddc | 207 | |
10ee5932 KZ |
208 | free(lc->filename); |
209 | lc->filename = NULL; | |
210 | ||
defa0710 | 211 | ignore_result( loopcxt_set_device(lc, NULL) ); |
10ee5932 | 212 | loopcxt_deinit_iterator(lc); |
82756a74 KZ |
213 | |
214 | errno = errsv; | |
10ee5932 KZ |
215 | } |
216 | ||
217 | /* | |
218 | * @lc: context | |
219 | * | |
220 | * Returns newly allocated device path. | |
221 | */ | |
222 | char *loopcxt_strdup_device(struct loopdev_cxt *lc) | |
223 | { | |
cba69bb5 | 224 | if (!lc || !*lc->device) |
10ee5932 KZ |
225 | return NULL; |
226 | return strdup(lc->device); | |
227 | } | |
228 | ||
229 | /* | |
230 | * @lc: context | |
231 | * | |
232 | * Returns pointer device name in the @lc struct. | |
233 | */ | |
234 | const char *loopcxt_get_device(struct loopdev_cxt *lc) | |
235 | { | |
cba69bb5 | 236 | return lc && *lc->device ? lc->device : NULL; |
10ee5932 KZ |
237 | } |
238 | ||
239 | /* | |
240 | * @lc: context | |
241 | * | |
242 | * Returns pointer to the sysfs context (see lib/sysfs.c) | |
243 | */ | |
244 | struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) | |
245 | { | |
246 | if (!lc || !*lc->device || (lc->flags & LOOPDEV_FL_NOSYSFS)) | |
247 | return NULL; | |
248 | ||
249 | if (!lc->sysfs.devno) { | |
250 | dev_t devno = sysfs_devname_to_devno(lc->device, NULL); | |
aee31ddc | 251 | if (!devno) { |
0bf03740 | 252 | DBG(CXT, ul_debugobj(lc, "sysfs: failed devname to devno")); |
10ee5932 | 253 | return NULL; |
aee31ddc KZ |
254 | } |
255 | if (sysfs_init(&lc->sysfs, devno, NULL)) { | |
0bf03740 | 256 | DBG(CXT, ul_debugobj(lc, "sysfs: init failed")); |
10ee5932 | 257 | return NULL; |
aee31ddc | 258 | } |
10ee5932 | 259 | } |
aee31ddc | 260 | |
10ee5932 KZ |
261 | return &lc->sysfs; |
262 | } | |
263 | ||
264 | /* | |
265 | * @lc: context | |
266 | * | |
267 | * Returns: file descriptor to the open loop device or <0 on error. The mode | |
268 | * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is | |
269 | * read-only. | |
270 | */ | |
271 | int loopcxt_get_fd(struct loopdev_cxt *lc) | |
272 | { | |
273 | if (!lc || !*lc->device) | |
fd7f0718 | 274 | return -EINVAL; |
10ee5932 | 275 | |
fd7f0718 KZ |
276 | if (lc->fd < 0) { |
277 | lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY; | |
b1fa3e22 | 278 | lc->fd = open(lc->device, lc->mode | O_CLOEXEC); |
0bf03740 | 279 | DBG(CXT, ul_debugobj(lc, "open %s [%s]: %m", lc->device, |
663bf040 | 280 | lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro")); |
fd7f0718 | 281 | } |
10ee5932 KZ |
282 | return lc->fd; |
283 | } | |
284 | ||
fd7f0718 KZ |
285 | int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, int mode) |
286 | { | |
287 | if (!lc) | |
288 | return -EINVAL; | |
289 | ||
290 | lc->fd = fd; | |
291 | lc->mode = mode; | |
292 | return 0; | |
293 | } | |
294 | ||
10ee5932 KZ |
295 | /* |
296 | * @lc: context | |
297 | * @flags: LOOPITER_FL_* flags | |
298 | * | |
299 | * Iterator allows to scan list of the free or used loop devices. | |
300 | * | |
301 | * Returns: <0 on error, 0 on success | |
302 | */ | |
303 | int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags) | |
304 | { | |
305 | struct loopdev_iter *iter; | |
306 | struct stat st; | |
307 | ||
308 | if (!lc) | |
309 | return -EINVAL; | |
310 | ||
aee31ddc | 311 | |
10ee5932 | 312 | iter = &lc->iter; |
0bf03740 | 313 | DBG(ITER, ul_debugobj(iter, "initialize")); |
10ee5932 KZ |
314 | |
315 | /* always zeroize | |
316 | */ | |
317 | memset(iter, 0, sizeof(*iter)); | |
318 | iter->ncur = -1; | |
319 | iter->flags = flags; | |
320 | iter->default_check = 1; | |
321 | ||
322 | if (!lc->extra_check) { | |
323 | /* | |
324 | * Check for /dev/loop/<N> subdirectory | |
325 | */ | |
82b4082e | 326 | if (!(lc->flags & LOOPDEV_FL_DEVSUBDIR) && |
10ee5932 KZ |
327 | stat(_PATH_DEV_LOOP, &st) == 0 && S_ISDIR(st.st_mode)) |
328 | lc->flags |= LOOPDEV_FL_DEVSUBDIR; | |
329 | ||
330 | lc->extra_check = 1; | |
331 | } | |
332 | return 0; | |
333 | } | |
334 | ||
335 | /* | |
336 | * @lc: context | |
337 | * | |
338 | * Returns: <0 on error, 0 on success | |
339 | */ | |
340 | int loopcxt_deinit_iterator(struct loopdev_cxt *lc) | |
341 | { | |
7552258a | 342 | struct loopdev_iter *iter; |
10ee5932 KZ |
343 | |
344 | if (!lc) | |
345 | return -EINVAL; | |
346 | ||
347 | iter = &lc->iter; | |
0bf03740 | 348 | DBG(ITER, ul_debugobj(iter, "de-initialize")); |
10ee5932 KZ |
349 | |
350 | free(iter->minors); | |
351 | if (iter->proc) | |
352 | fclose(iter->proc); | |
e4062c72 KZ |
353 | if (iter->sysblock) |
354 | closedir(iter->sysblock); | |
10ee5932 KZ |
355 | iter->minors = NULL; |
356 | iter->proc = NULL; | |
e4062c72 | 357 | iter->sysblock = NULL; |
10ee5932 KZ |
358 | iter->done = 1; |
359 | return 0; | |
360 | } | |
361 | ||
362 | /* | |
363 | * Same as loopcxt_set_device, but also checks if the device is | |
364 | * associeted with any file. | |
365 | * | |
366 | * Returns: <0 on error, 0 on success, 1 device does not match with | |
367 | * LOOPITER_FL_{USED,FREE} flags. | |
368 | */ | |
369 | static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) | |
370 | { | |
371 | int rc = loopcxt_set_device(lc, device); | |
372 | int used; | |
373 | ||
374 | if (rc) | |
375 | return rc; | |
376 | ||
377 | if (!(lc->iter.flags & LOOPITER_FL_USED) && | |
378 | !(lc->iter.flags & LOOPITER_FL_FREE)) | |
379 | return 0; /* caller does not care about device status */ | |
380 | ||
85794fb0 | 381 | if (!is_loopdev(lc->device)) { |
0bf03740 | 382 | DBG(ITER, ul_debugobj(&lc->iter, "%s does not exist", lc->device)); |
85794fb0 KZ |
383 | return -errno; |
384 | } | |
385 | ||
0bf03740 | 386 | DBG(ITER, ul_debugobj(&lc->iter, "%s exist", lc->device)); |
85794fb0 | 387 | |
10ee5932 KZ |
388 | used = loopcxt_get_offset(lc, NULL) == 0; |
389 | ||
390 | if ((lc->iter.flags & LOOPITER_FL_USED) && used) | |
391 | return 0; | |
392 | ||
393 | if ((lc->iter.flags & LOOPITER_FL_FREE) && !used) | |
394 | return 0; | |
395 | ||
0bf03740 KZ |
396 | DBG(ITER, ul_debugobj(&lc->iter, "failed to use %s device", lc->device)); |
397 | ||
defa0710 | 398 | ignore_result( loopcxt_set_device(lc, NULL) ); |
10ee5932 KZ |
399 | return 1; |
400 | } | |
401 | ||
402 | static int cmpnum(const void *p1, const void *p2) | |
403 | { | |
33487796 KZ |
404 | return (((* (int *) p1) > (* (int *) p2)) - |
405 | ((* (int *) p1) < (* (int *) p2))); | |
10ee5932 KZ |
406 | } |
407 | ||
408 | /* | |
409 | * The classic scandir() is more expensive and less portable. | |
410 | * We needn't full loop device names -- loop numbers (loop<N>) | |
411 | * are enough. | |
412 | */ | |
413 | static int loop_scandir(const char *dirname, int **ary, int hasprefix) | |
414 | { | |
415 | DIR *dir; | |
416 | struct dirent *d; | |
417 | unsigned int n, count = 0, arylen = 0; | |
418 | ||
419 | if (!dirname || !ary) | |
420 | return 0; | |
0bf03740 KZ |
421 | |
422 | DBG(ITER, ul_debug("scan dir: %s", dirname)); | |
423 | ||
10ee5932 KZ |
424 | dir = opendir(dirname); |
425 | if (!dir) | |
426 | return 0; | |
10ee5932 KZ |
427 | free(*ary); |
428 | *ary = NULL; | |
429 | ||
430 | while((d = readdir(dir))) { | |
431 | #ifdef _DIRENT_HAVE_D_TYPE | |
432 | if (d->d_type != DT_BLK && d->d_type != DT_UNKNOWN && | |
433 | d->d_type != DT_LNK) | |
434 | continue; | |
435 | #endif | |
436 | if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) | |
437 | continue; | |
438 | ||
439 | if (hasprefix) { | |
440 | /* /dev/loop<N> */ | |
441 | if (sscanf(d->d_name, "loop%u", &n) != 1) | |
442 | continue; | |
443 | } else { | |
444 | /* /dev/loop/<N> */ | |
445 | char *end = NULL; | |
446 | ||
f0fe8ab5 | 447 | errno = 0; |
10ee5932 KZ |
448 | n = strtol(d->d_name, &end, 10); |
449 | if (d->d_name == end || (end && *end) || errno) | |
450 | continue; | |
451 | } | |
452 | if (n < LOOPDEV_DEFAULT_NNODES) | |
453 | continue; /* ignore loop<0..7> */ | |
454 | ||
455 | if (count + 1 > arylen) { | |
456 | int *tmp; | |
457 | ||
458 | arylen += 1; | |
459 | ||
460 | tmp = realloc(*ary, arylen * sizeof(int)); | |
461 | if (!tmp) { | |
462 | free(*ary); | |
34f6177a | 463 | *ary = NULL; |
2e566efd | 464 | closedir(dir); |
10ee5932 KZ |
465 | return -1; |
466 | } | |
467 | *ary = tmp; | |
468 | } | |
fc8b1f36 KZ |
469 | if (*ary) |
470 | (*ary)[count++] = n; | |
10ee5932 | 471 | } |
fc8b1f36 | 472 | if (count && *ary) |
10ee5932 KZ |
473 | qsort(*ary, count, sizeof(int), cmpnum); |
474 | ||
475 | closedir(dir); | |
476 | return count; | |
477 | } | |
478 | ||
e4062c72 KZ |
479 | /* |
480 | * Set the next *used* loop device according to /proc/partitions. | |
481 | * | |
482 | * Loop devices smaller than 512 bytes are invisible for this function. | |
483 | */ | |
484 | static int loopcxt_next_from_proc(struct loopdev_cxt *lc) | |
485 | { | |
486 | struct loopdev_iter *iter = &lc->iter; | |
487 | char buf[BUFSIZ]; | |
488 | ||
0bf03740 | 489 | DBG(ITER, ul_debugobj(iter, "scan /proc/partitions")); |
e4062c72 KZ |
490 | |
491 | if (!iter->proc) | |
b1fa3e22 | 492 | iter->proc = fopen(_PATH_PROC_PARTITIONS, "r" UL_CLOEXECSTR); |
e4062c72 KZ |
493 | if (!iter->proc) |
494 | return 1; | |
495 | ||
496 | while (fgets(buf, sizeof(buf), iter->proc)) { | |
497 | unsigned int m; | |
657d9adb | 498 | char name[128 + 1]; |
e4062c72 KZ |
499 | |
500 | ||
501 | if (sscanf(buf, " %u %*s %*s %128[^\n ]", | |
502 | &m, name) != 2 || m != LOOPDEV_MAJOR) | |
503 | continue; | |
504 | ||
0bf03740 | 505 | DBG(ITER, ul_debugobj(iter, "checking %s", name)); |
e4062c72 KZ |
506 | |
507 | if (loopiter_set_device(lc, name) == 0) | |
508 | return 0; | |
509 | } | |
510 | ||
511 | return 1; | |
512 | } | |
513 | ||
514 | /* | |
515 | * Set the next *used* loop device according to | |
516 | * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required). | |
517 | * | |
518 | * This is preferred method. | |
519 | */ | |
520 | static int loopcxt_next_from_sysfs(struct loopdev_cxt *lc) | |
521 | { | |
522 | struct loopdev_iter *iter = &lc->iter; | |
523 | struct dirent *d; | |
524 | int fd; | |
525 | ||
0bf03740 | 526 | DBG(ITER, ul_debugobj(iter, "scanning /sys/block")); |
e4062c72 KZ |
527 | |
528 | if (!iter->sysblock) | |
529 | iter->sysblock = opendir(_PATH_SYS_BLOCK); | |
530 | ||
531 | if (!iter->sysblock) | |
532 | return 1; | |
533 | ||
534 | fd = dirfd(iter->sysblock); | |
535 | ||
536 | while ((d = readdir(iter->sysblock))) { | |
537 | char name[256]; | |
538 | struct stat st; | |
539 | ||
0bf03740 | 540 | DBG(ITER, ul_debugobj(iter, "check %s", d->d_name)); |
e4062c72 KZ |
541 | |
542 | if (strcmp(d->d_name, ".") == 0 | |
543 | || strcmp(d->d_name, "..") == 0 | |
544 | || strncmp(d->d_name, "loop", 4) != 0) | |
545 | continue; | |
546 | ||
547 | snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name); | |
2208b3cc | 548 | if (fstatat(fd, name, &st, 0) != 0) |
e4062c72 KZ |
549 | continue; |
550 | ||
551 | if (loopiter_set_device(lc, d->d_name) == 0) | |
552 | return 0; | |
553 | } | |
554 | ||
555 | return 1; | |
556 | } | |
557 | ||
10ee5932 KZ |
558 | /* |
559 | * @lc: context, has to initialized by loopcxt_init_iterator() | |
560 | * | |
561 | * Returns: 0 on success, -1 on error, 1 at the end of scanning. The details | |
562 | * about the current loop device are available by | |
563 | * loopcxt_get_{fd,backing_file,device,offset, ...} functions. | |
564 | */ | |
565 | int loopcxt_next(struct loopdev_cxt *lc) | |
566 | { | |
567 | struct loopdev_iter *iter; | |
568 | ||
569 | if (!lc) | |
570 | return -EINVAL; | |
aee31ddc | 571 | |
aee31ddc | 572 | |
10ee5932 KZ |
573 | iter = &lc->iter; |
574 | if (iter->done) | |
575 | return 1; | |
576 | ||
0bf03740 KZ |
577 | DBG(ITER, ul_debugobj(iter, "next")); |
578 | ||
10ee5932 KZ |
579 | /* A) Look for used loop devices in /proc/partitions ("losetup -a" only) |
580 | */ | |
581 | if (iter->flags & LOOPITER_FL_USED) { | |
e4062c72 KZ |
582 | int rc; |
583 | ||
584 | if (loopcxt_sysfs_available(lc)) | |
585 | rc = loopcxt_next_from_sysfs(lc); | |
586 | else | |
587 | rc = loopcxt_next_from_proc(lc); | |
588 | if (rc == 0) | |
589 | return 0; | |
10ee5932 KZ |
590 | goto done; |
591 | } | |
592 | ||
593 | /* B) Classic way, try first eight loop devices (default number | |
594 | * of loop devices). This is enough for 99% of all cases. | |
595 | */ | |
596 | if (iter->default_check) { | |
0bf03740 | 597 | DBG(ITER, ul_debugobj(iter, "next: default check")); |
10ee5932 KZ |
598 | for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES; |
599 | iter->ncur++) { | |
600 | char name[16]; | |
601 | snprintf(name, sizeof(name), "loop%d", iter->ncur); | |
602 | ||
603 | if (loopiter_set_device(lc, name) == 0) | |
604 | return 0; | |
605 | } | |
606 | iter->default_check = 0; | |
607 | } | |
608 | ||
609 | /* C) the worst possibility, scan whole /dev or /dev/loop/<N> | |
610 | */ | |
611 | if (!iter->minors) { | |
0bf03740 | 612 | DBG(ITER, ul_debugobj(iter, "next: scanning /dev")); |
10ee5932 KZ |
613 | iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ? |
614 | loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) : | |
615 | loop_scandir(_PATH_DEV, &iter->minors, 1); | |
616 | iter->ncur = -1; | |
617 | } | |
618 | for (++iter->ncur; iter->ncur < iter->nminors; iter->ncur++) { | |
619 | char name[16]; | |
620 | snprintf(name, sizeof(name), "loop%d", iter->minors[iter->ncur]); | |
621 | ||
622 | if (loopiter_set_device(lc, name) == 0) | |
623 | return 0; | |
624 | } | |
625 | done: | |
626 | loopcxt_deinit_iterator(lc); | |
627 | return 1; | |
628 | } | |
629 | ||
630 | /* | |
631 | * @device: path to device | |
632 | */ | |
633 | int is_loopdev(const char *device) | |
634 | { | |
635 | struct stat st; | |
636 | ||
637 | if (!device) | |
638 | return 0; | |
639 | ||
640 | return (stat(device, &st) == 0 && | |
641 | S_ISBLK(st.st_mode) && | |
642 | major(st.st_rdev) == LOOPDEV_MAJOR); | |
643 | } | |
644 | ||
645 | /* | |
646 | * @lc: context | |
647 | * | |
648 | * Returns result from LOOP_GET_STAT64 ioctl or NULL on error. | |
649 | */ | |
650 | struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc) | |
651 | { | |
652 | int fd; | |
653 | ||
85794fb0 KZ |
654 | if (!lc || lc->info_failed) { |
655 | errno = EINVAL; | |
10ee5932 | 656 | return NULL; |
85794fb0 KZ |
657 | } |
658 | errno = 0; | |
10ee5932 KZ |
659 | if (lc->has_info) |
660 | return &lc->info; | |
661 | ||
662 | fd = loopcxt_get_fd(lc); | |
663 | if (fd < 0) | |
664 | return NULL; | |
665 | ||
666 | if (ioctl(fd, LOOP_GET_STATUS64, &lc->info) == 0) { | |
667 | lc->has_info = 1; | |
6c224de1 | 668 | lc->info_failed = 0; |
0bf03740 | 669 | DBG(CXT, ul_debugobj(lc, "reading loop_info64 OK")); |
10ee5932 KZ |
670 | return &lc->info; |
671 | } | |
672 | ||
85794fb0 | 673 | lc->info_failed = 1; |
0bf03740 | 674 | DBG(CXT, ul_debugobj(lc, "reading loop_info64 FAILED")); |
85794fb0 | 675 | |
10ee5932 KZ |
676 | return NULL; |
677 | } | |
678 | ||
679 | /* | |
680 | * @lc: context | |
681 | * | |
682 | * Returns (allocated) string with path to the file assicieted | |
683 | * with the current loop device. | |
684 | */ | |
685 | char *loopcxt_get_backing_file(struct loopdev_cxt *lc) | |
686 | { | |
687 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); | |
688 | char *res = NULL; | |
689 | ||
690 | if (sysfs) | |
fd7f0718 KZ |
691 | /* |
692 | * This is always preffered, the loop_info64 | |
693 | * has too small buffer for the filename. | |
694 | */ | |
10ee5932 KZ |
695 | res = sysfs_strdup(sysfs, "loop/backing_file"); |
696 | ||
697 | if (!res && loopcxt_ioctl_enabled(lc)) { | |
698 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
699 | ||
700 | if (lo) { | |
701 | lo->lo_file_name[LO_NAME_SIZE - 2] = '*'; | |
702 | lo->lo_file_name[LO_NAME_SIZE - 1] = '\0'; | |
703 | res = strdup((char *) lo->lo_file_name); | |
704 | } | |
705 | } | |
aee31ddc | 706 | |
0bf03740 | 707 | DBG(CXT, ul_debugobj(lc, "get_backing_file [%s]", res)); |
10ee5932 KZ |
708 | return res; |
709 | } | |
710 | ||
711 | /* | |
712 | * @lc: context | |
713 | * @offset: returns offset number for the given device | |
714 | * | |
715 | * Returns: <0 on error, 0 on success | |
716 | */ | |
717 | int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset) | |
718 | { | |
719 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); | |
720 | int rc = -EINVAL; | |
721 | ||
722 | if (sysfs) | |
723 | rc = sysfs_read_u64(sysfs, "loop/offset", offset); | |
724 | ||
725 | if (rc && loopcxt_ioctl_enabled(lc)) { | |
726 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
727 | if (lo) { | |
728 | if (offset) | |
729 | *offset = lo->lo_offset; | |
6c224de1 | 730 | rc = 0; |
85794fb0 KZ |
731 | } else |
732 | rc = -errno; | |
10ee5932 KZ |
733 | } |
734 | ||
0bf03740 | 735 | DBG(CXT, ul_debugobj(lc, "get_offset [rc=%d]", rc)); |
10ee5932 KZ |
736 | return rc; |
737 | } | |
738 | ||
739 | /* | |
740 | * @lc: context | |
741 | * @sizelimit: returns size limit for the given device | |
742 | * | |
743 | * Returns: <0 on error, 0 on success | |
744 | */ | |
745 | int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size) | |
746 | { | |
747 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); | |
748 | int rc = -EINVAL; | |
749 | ||
750 | if (sysfs) | |
751 | rc = sysfs_read_u64(sysfs, "loop/sizelimit", size); | |
752 | ||
753 | if (rc && loopcxt_ioctl_enabled(lc)) { | |
754 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
755 | if (lo) { | |
756 | if (size) | |
757 | *size = lo->lo_sizelimit; | |
6c224de1 | 758 | rc = 0; |
85794fb0 KZ |
759 | } else |
760 | rc = -errno; | |
10ee5932 KZ |
761 | } |
762 | ||
0bf03740 | 763 | DBG(CXT, ul_debugobj(lc, "get_sizelimit [rc=%d]", rc)); |
6c224de1 KZ |
764 | return rc; |
765 | } | |
766 | ||
767 | /* | |
768 | * @lc: context | |
769 | * @devno: returns encryption type | |
770 | * | |
771 | * Cryptoloop is DEPRECATED! | |
772 | * | |
773 | * Returns: <0 on error, 0 on success | |
774 | */ | |
775 | int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type) | |
776 | { | |
777 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
85794fb0 | 778 | int rc; |
6c224de1 | 779 | |
85794fb0 | 780 | /* not provided by sysfs */ |
6c224de1 KZ |
781 | if (lo) { |
782 | if (type) | |
783 | *type = lo->lo_encrypt_type; | |
784 | rc = 0; | |
85794fb0 KZ |
785 | } else |
786 | rc = -errno; | |
787 | ||
0bf03740 | 788 | DBG(CXT, ul_debugobj(lc, "get_encrypt_type [rc=%d]", rc)); |
6c224de1 KZ |
789 | return rc; |
790 | } | |
791 | ||
792 | /* | |
793 | * @lc: context | |
794 | * @devno: returns crypt name | |
795 | * | |
796 | * Cryptoloop is DEPRECATED! | |
797 | * | |
798 | * Returns: <0 on error, 0 on success | |
799 | */ | |
800 | const char *loopcxt_get_crypt_name(struct loopdev_cxt *lc) | |
801 | { | |
802 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
803 | ||
804 | if (lo) | |
805 | return (char *) lo->lo_crypt_name; | |
806 | ||
0bf03740 | 807 | DBG(CXT, ul_debugobj(lc, "get_crypt_name failed")); |
6c224de1 KZ |
808 | return NULL; |
809 | } | |
810 | ||
811 | /* | |
812 | * @lc: context | |
813 | * @devno: returns backing file devno | |
814 | * | |
815 | * Returns: <0 on error, 0 on success | |
816 | */ | |
817 | int loopcxt_get_backing_devno(struct loopdev_cxt *lc, dev_t *devno) | |
818 | { | |
819 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
85794fb0 | 820 | int rc; |
6c224de1 KZ |
821 | |
822 | if (lo) { | |
823 | if (devno) | |
824 | *devno = lo->lo_device; | |
825 | rc = 0; | |
85794fb0 KZ |
826 | } else |
827 | rc = -errno; | |
828 | ||
0bf03740 | 829 | DBG(CXT, ul_debugobj(lc, "get_backing_devno [rc=%d]", rc)); |
6c224de1 KZ |
830 | return rc; |
831 | } | |
832 | ||
833 | /* | |
834 | * @lc: context | |
835 | * @ino: returns backing file inode | |
836 | * | |
837 | * Returns: <0 on error, 0 on success | |
838 | */ | |
839 | int loopcxt_get_backing_inode(struct loopdev_cxt *lc, ino_t *ino) | |
840 | { | |
841 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
85794fb0 | 842 | int rc; |
6c224de1 KZ |
843 | |
844 | if (lo) { | |
845 | if (ino) | |
846 | *ino = lo->lo_inode; | |
847 | rc = 0; | |
85794fb0 KZ |
848 | } else |
849 | rc = -errno; | |
850 | ||
0bf03740 | 851 | DBG(CXT, ul_debugobj(lc, "get_backing_inode [rc=%d]", rc)); |
10ee5932 KZ |
852 | return rc; |
853 | } | |
854 | ||
59d749c3 KZ |
855 | /* |
856 | * Check if the kernel supports partitioned loop devices. | |
857 | * | |
858 | * Notes: | |
859 | * - kernels < 3.2 support partitioned loop devices and PT scanning | |
860 | * only if max_part= module paremeter is non-zero | |
861 | * | |
862 | * - kernels >= 3.2 always support partitioned loop devices | |
863 | * | |
864 | * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls | |
865 | * | |
866 | * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the | |
867 | * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled | |
868 | * by default. | |
869 | * | |
870 | * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98. | |
871 | */ | |
872 | int loopmod_supports_partscan(void) | |
873 | { | |
874 | int rc, ret = 0; | |
875 | FILE *f; | |
876 | ||
877 | if (get_linux_version() >= KERNEL_VERSION(3,2,0)) | |
878 | return 1; | |
879 | ||
b1fa3e22 | 880 | f = fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR); |
59d749c3 KZ |
881 | if (!f) |
882 | return 0; | |
883 | rc = fscanf(f, "%d", &ret); | |
884 | fclose(f); | |
8b04761d | 885 | return rc == 1 ? ret : 0; |
59d749c3 KZ |
886 | } |
887 | ||
10ee5932 KZ |
888 | /* |
889 | * @lc: context | |
890 | * | |
59d749c3 KZ |
891 | * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions |
892 | * scannig is enabled for all loop devices. | |
893 | */ | |
894 | int loopcxt_is_partscan(struct loopdev_cxt *lc) | |
895 | { | |
896 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); | |
897 | ||
898 | if (sysfs) { | |
899 | /* kernel >= 3.2 */ | |
900 | int fl; | |
901 | if (sysfs_read_int(sysfs, "loop/partscan", &fl) == 0) | |
902 | return fl; | |
903 | } | |
904 | ||
905 | /* old kernels (including kernels without loopN/loop/<flags> directory */ | |
906 | return loopmod_supports_partscan(); | |
907 | } | |
908 | ||
909 | /* | |
910 | * @lc: context | |
911 | * | |
912 | * Returns: 1 if the autoclear flags is set. | |
10ee5932 KZ |
913 | */ |
914 | int loopcxt_is_autoclear(struct loopdev_cxt *lc) | |
915 | { | |
916 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); | |
917 | ||
918 | if (sysfs) { | |
919 | int fl; | |
920 | if (sysfs_read_int(sysfs, "loop/autoclear", &fl) == 0) | |
921 | return fl; | |
922 | } | |
923 | ||
924 | if (loopcxt_ioctl_enabled(lc)) { | |
925 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
926 | if (lo) | |
927 | return lo->lo_flags & LO_FLAGS_AUTOCLEAR; | |
928 | } | |
929 | return 0; | |
930 | } | |
931 | ||
fd7f0718 KZ |
932 | /* |
933 | * @lc: context | |
934 | * | |
59d749c3 | 935 | * Returns: 1 if the readonly flags is set. |
fd7f0718 KZ |
936 | */ |
937 | int loopcxt_is_readonly(struct loopdev_cxt *lc) | |
938 | { | |
939 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); | |
940 | ||
941 | if (sysfs) { | |
942 | int fl; | |
943 | if (sysfs_read_int(sysfs, "ro", &fl) == 0) | |
944 | return fl; | |
945 | } | |
946 | ||
947 | if (loopcxt_ioctl_enabled(lc)) { | |
948 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
949 | if (lo) | |
950 | return lo->lo_flags & LO_FLAGS_READ_ONLY; | |
951 | } | |
952 | return 0; | |
953 | } | |
954 | ||
faeef4d2 ML |
955 | /* |
956 | * @lc: context | |
957 | * | |
958 | * Returns: 1 if the dio flags is set. | |
959 | */ | |
960 | int loopcxt_is_dio(struct loopdev_cxt *lc) | |
961 | { | |
962 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); | |
963 | ||
964 | if (sysfs) { | |
965 | int fl; | |
966 | if (sysfs_read_int(sysfs, "loop/dio", &fl) == 0) | |
967 | return fl; | |
968 | } | |
969 | if (loopcxt_ioctl_enabled(lc)) { | |
970 | struct loop_info64 *lo = loopcxt_get_info(lc); | |
971 | if (lo) | |
972 | return lo->lo_flags & LO_FLAGS_DIRECT_IO; | |
973 | } | |
974 | return 0; | |
975 | } | |
976 | ||
6c224de1 KZ |
977 | /* |
978 | * @lc: context | |
979 | * @st: backing file stat or NULL | |
980 | * @backing_file: filename | |
981 | * @offset: offset | |
982 | * @flags: LOOPDEV_FL_OFFSET if @offset should not be ignored | |
983 | * | |
984 | * Returns 1 if the current @lc loopdev is associated with the given backing | |
985 | * file. Note that the preferred way is to use devno and inode number rather | |
986 | * than filename. The @backing_file filename is poor solution usable in case | |
987 | * that you don't have rights to call stat(). | |
988 | * | |
989 | * Don't forget that old kernels provide very restricted (in size) backing | |
990 | * filename by LOOP_GET_STAT64 ioctl only. | |
991 | */ | |
992 | int loopcxt_is_used(struct loopdev_cxt *lc, | |
993 | struct stat *st, | |
994 | const char *backing_file, | |
995 | uint64_t offset, | |
996 | int flags) | |
997 | { | |
998 | ino_t ino; | |
999 | dev_t dev; | |
1000 | ||
1001 | if (!lc) | |
1002 | return 0; | |
1003 | ||
0bf03740 | 1004 | DBG(CXT, ul_debugobj(lc, "checking %s vs. %s", |
6c224de1 KZ |
1005 | loopcxt_get_device(lc), |
1006 | backing_file)); | |
1007 | ||
1008 | if (st && loopcxt_get_backing_inode(lc, &ino) == 0 && | |
1009 | loopcxt_get_backing_devno(lc, &dev) == 0) { | |
1010 | ||
1011 | if (ino == st->st_ino && dev == st->st_dev) | |
1012 | goto found; | |
1013 | ||
1014 | /* don't use filename if we have devno and inode */ | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | /* poor man's solution */ | |
1019 | if (backing_file) { | |
1020 | char *name = loopcxt_get_backing_file(lc); | |
1021 | int rc = name && strcmp(name, backing_file) == 0; | |
1022 | ||
1023 | free(name); | |
1024 | if (rc) | |
1025 | goto found; | |
1026 | } | |
1027 | ||
1028 | return 0; | |
1029 | found: | |
1030 | if (flags & LOOPDEV_FL_OFFSET) { | |
1031 | uint64_t off; | |
1032 | ||
1033 | return loopcxt_get_offset(lc, &off) == 0 && off == offset; | |
1034 | } | |
1035 | return 1; | |
1036 | } | |
1037 | ||
fd7f0718 KZ |
1038 | /* |
1039 | * The setting is removed by loopcxt_set_device() loopcxt_next()! | |
1040 | */ | |
10ee5932 KZ |
1041 | int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset) |
1042 | { | |
1043 | if (!lc) | |
1044 | return -EINVAL; | |
1045 | lc->info.lo_offset = offset; | |
aee31ddc | 1046 | |
0bf03740 | 1047 | DBG(CXT, ul_debugobj(lc, "set offset=%jd", offset)); |
10ee5932 KZ |
1048 | return 0; |
1049 | } | |
1050 | ||
fd7f0718 KZ |
1051 | /* |
1052 | * The setting is removed by loopcxt_set_device() loopcxt_next()! | |
1053 | */ | |
10ee5932 KZ |
1054 | int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit) |
1055 | { | |
1056 | if (!lc) | |
1057 | return -EINVAL; | |
1058 | lc->info.lo_sizelimit = sizelimit; | |
aee31ddc | 1059 | |
0bf03740 | 1060 | DBG(CXT, ul_debugobj(lc, "set sizelimit=%jd", sizelimit)); |
10ee5932 KZ |
1061 | return 0; |
1062 | } | |
1063 | ||
1064 | /* | |
1065 | * @lc: context | |
1066 | * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags | |
1067 | * | |
fd7f0718 KZ |
1068 | * The setting is removed by loopcxt_set_device() loopcxt_next()! |
1069 | * | |
10ee5932 KZ |
1070 | * Returns: 0 on success, <0 on error. |
1071 | */ | |
1072 | int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags) | |
1073 | { | |
1074 | if (!lc) | |
1075 | return -EINVAL; | |
1076 | lc->info.lo_flags = flags; | |
aee31ddc | 1077 | |
0bf03740 | 1078 | DBG(CXT, ul_debugobj(lc, "set flags=%u", (unsigned) flags)); |
10ee5932 KZ |
1079 | return 0; |
1080 | } | |
1081 | ||
1082 | /* | |
1083 | * @lc: context | |
1084 | * @filename: backing file path (the path will be canonicalized) | |
1085 | * | |
fd7f0718 KZ |
1086 | * The setting is removed by loopcxt_set_device() loopcxt_next()! |
1087 | * | |
10ee5932 KZ |
1088 | * Returns: 0 on success, <0 on error. |
1089 | */ | |
1090 | int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename) | |
1091 | { | |
1092 | if (!lc) | |
1093 | return -EINVAL; | |
1094 | ||
1095 | lc->filename = canonicalize_path(filename); | |
1096 | if (!lc->filename) | |
1097 | return -errno; | |
1098 | ||
1099 | strncpy((char *)lc->info.lo_file_name, lc->filename, LO_NAME_SIZE); | |
1100 | lc->info.lo_file_name[LO_NAME_SIZE- 1] = '\0'; | |
1101 | ||
0bf03740 | 1102 | DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->info.lo_file_name)); |
10ee5932 KZ |
1103 | return 0; |
1104 | } | |
1105 | ||
293714c0 JM |
1106 | /* |
1107 | * In kernels prior to v3.9, if the offset or sizelimit options | |
1108 | * are used, the block device's size won't be synced automatically. | |
1109 | * blockdev --getsize64 and filesystems will use the backing | |
1110 | * file size until the block device has been re-opened or the | |
1111 | * LOOP_SET_CAPACITY ioctl is called to sync the sizes. | |
1112 | * | |
1113 | * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes | |
1114 | * the open file descriptor to the mount system call, we need to use | |
1115 | * the ioctl. Calling losetup directly doesn't have this problem since | |
1116 | * it closes the device when it exits and whatever consumes the device | |
1117 | * next will re-open it, causing the resync. | |
1118 | */ | |
1119 | static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd) | |
1120 | { | |
1121 | uint64_t size, expected_size; | |
1122 | int dev_fd; | |
1123 | struct stat st; | |
1124 | ||
1125 | if (!lc->info.lo_offset && !lc->info.lo_sizelimit) | |
1126 | return 0; | |
1127 | ||
01307ecf | 1128 | if (fstat(file_fd, &st)) { |
0bf03740 | 1129 | DBG(CXT, ul_debugobj(lc, "failed to fstat backing file")); |
293714c0 | 1130 | return -errno; |
01307ecf | 1131 | } |
e3b6cb87 KZ |
1132 | if (S_ISBLK(st.st_mode)) { |
1133 | if (blkdev_get_size(file_fd, | |
01307ecf | 1134 | (unsigned long long *) &expected_size)) { |
0bf03740 | 1135 | DBG(CXT, ul_debugobj(lc, "failed to determine device size")); |
e3b6cb87 | 1136 | return -errno; |
01307ecf | 1137 | } |
e3b6cb87 KZ |
1138 | } else |
1139 | expected_size = st.st_size; | |
1140 | ||
1141 | if (expected_size == 0 || expected_size <= lc->info.lo_offset) { | |
0bf03740 | 1142 | DBG(CXT, ul_debugobj(lc, "failed to determine expected size")); |
e3b6cb87 KZ |
1143 | return 0; /* ignore this error */ |
1144 | } | |
293714c0 JM |
1145 | |
1146 | if (lc->info.lo_offset > 0) | |
1147 | expected_size -= lc->info.lo_offset; | |
1148 | ||
1149 | if (lc->info.lo_sizelimit > 0 && lc->info.lo_sizelimit < expected_size) | |
1150 | expected_size = lc->info.lo_sizelimit; | |
1151 | ||
1152 | dev_fd = loopcxt_get_fd(lc); | |
01307ecf | 1153 | if (dev_fd < 0) { |
0bf03740 | 1154 | DBG(CXT, ul_debugobj(lc, "failed to get loop FD")); |
293714c0 | 1155 | return -errno; |
01307ecf | 1156 | } |
293714c0 | 1157 | |
01307ecf | 1158 | if (blkdev_get_size(dev_fd, (unsigned long long *) &size)) { |
0bf03740 | 1159 | DBG(CXT, ul_debugobj(lc, "failed to determine loopdev size")); |
293714c0 | 1160 | return -errno; |
01307ecf | 1161 | } |
293714c0 | 1162 | |
a7d5202b KZ |
1163 | /* It's block device, so, align to 512-byte sectors */ |
1164 | if (expected_size % 512) { | |
0bf03740 | 1165 | DBG(CXT, ul_debugobj(lc, "expected size misaligned to 512-byte sectors")); |
a7d5202b KZ |
1166 | expected_size = (expected_size >> 9) << 9; |
1167 | } | |
1168 | ||
293714c0 | 1169 | if (expected_size != size) { |
0bf03740 | 1170 | DBG(CXT, ul_debugobj(lc, "warning: loopdev and expected " |
e3b6cb87 KZ |
1171 | "size dismatch (%ju/%ju)", |
1172 | size, expected_size)); | |
1173 | ||
293714c0 JM |
1174 | if (loopcxt_set_capacity(lc)) { |
1175 | /* ioctl not available */ | |
1176 | if (errno == ENOTTY || errno == EINVAL) | |
1177 | errno = ERANGE; | |
1178 | return -errno; | |
1179 | } | |
1180 | ||
1181 | if (blkdev_get_size(dev_fd, (unsigned long long *) &size)) | |
1182 | return -errno; | |
1183 | ||
01307ecf KZ |
1184 | if (expected_size != size) { |
1185 | errno = ERANGE; | |
0bf03740 | 1186 | DBG(CXT, ul_debugobj(lc, "failed to set loopdev size, " |
01307ecf KZ |
1187 | "size: %ju, expected: %ju", |
1188 | size, expected_size)); | |
1189 | return -errno; | |
1190 | } | |
293714c0 JM |
1191 | } |
1192 | ||
1193 | return 0; | |
1194 | } | |
1195 | ||
10ee5932 KZ |
1196 | /* |
1197 | * @cl: context | |
1198 | * | |
1199 | * Associate the current device (see loopcxt_{set,get}_device()) with | |
1200 | * a file (see loopcxt_set_backing_file()). | |
1201 | * | |
fd7f0718 KZ |
1202 | * The device is initialized read-write by default. If you want read-only |
1203 | * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_* | |
1204 | * flags are ignored and modified according to LO_FLAGS_*. | |
1205 | * | |
1206 | * If the device is already open by loopcxt_get_fd() then this setup device | |
1207 | * function will re-open the device to fix read/write mode. | |
1208 | * | |
1209 | * The device is also initialized read-only if the backing file is not | |
1210 | * possible to open read-write (e.g. read-only FS). | |
10ee5932 KZ |
1211 | * |
1212 | * Returns: <0 on error, 0 on success. | |
1213 | */ | |
1214 | int loopcxt_setup_device(struct loopdev_cxt *lc) | |
1215 | { | |
663bf040 | 1216 | int file_fd, dev_fd, mode = O_RDWR, rc = -1, cnt = 0; |
2cde9865 | 1217 | int errsv = 0; |
10ee5932 KZ |
1218 | |
1219 | if (!lc || !*lc->device || !lc->filename) | |
1220 | return -EINVAL; | |
1221 | ||
0bf03740 | 1222 | DBG(SETUP, ul_debugobj(lc, "device setup requested")); |
aee31ddc | 1223 | |
10ee5932 KZ |
1224 | /* |
1225 | * Open backing file and device | |
1226 | */ | |
fd7f0718 KZ |
1227 | if (lc->info.lo_flags & LO_FLAGS_READ_ONLY) |
1228 | mode = O_RDONLY; | |
10ee5932 | 1229 | |
b1fa3e22 | 1230 | if ((file_fd = open(lc->filename, mode | O_CLOEXEC)) < 0) { |
10ee5932 KZ |
1231 | if (mode != O_RDONLY && (errno == EROFS || errno == EACCES)) |
1232 | file_fd = open(lc->filename, mode = O_RDONLY); | |
1233 | ||
aee31ddc | 1234 | if (file_fd < 0) { |
0bf03740 | 1235 | DBG(SETUP, ul_debugobj(lc, "open backing file failed: %m")); |
10ee5932 | 1236 | return -errno; |
aee31ddc | 1237 | } |
10ee5932 | 1238 | } |
0bf03740 | 1239 | DBG(SETUP, ul_debugobj(lc, "backing file open: OK")); |
10ee5932 | 1240 | |
fd7f0718 | 1241 | if (lc->fd != -1 && lc->mode != mode) { |
0bf03740 | 1242 | DBG(SETUP, ul_debugobj(lc, "closing already open device (mode mismatch)")); |
fd7f0718 KZ |
1243 | close(lc->fd); |
1244 | lc->fd = -1; | |
1245 | lc->mode = 0; | |
1246 | } | |
1247 | ||
10ee5932 | 1248 | if (mode == O_RDONLY) { |
fd7f0718 KZ |
1249 | lc->flags |= LOOPDEV_FL_RDONLY; /* open() mode */ |
1250 | lc->info.lo_flags |= LO_FLAGS_READ_ONLY; /* kernel loopdev mode */ | |
1251 | } else { | |
1252 | lc->flags |= LOOPDEV_FL_RDWR; /* open() mode */ | |
1253 | lc->info.lo_flags &= ~LO_FLAGS_READ_ONLY; | |
1254 | lc->flags &= ~LOOPDEV_FL_RDONLY; | |
1255 | } | |
10ee5932 | 1256 | |
663bf040 KZ |
1257 | do { |
1258 | errno = 0; | |
1259 | dev_fd = loopcxt_get_fd(lc); | |
1260 | if (dev_fd >= 0 || lc->control_ok == 0) | |
1261 | break; | |
1262 | if (errno != EACCES && errno != ENOENT) | |
1263 | break; | |
1264 | /* We have permissions to open /dev/loop-control, but open | |
1265 | * /dev/loopN failed with EACCES, it's probably because udevd | |
1266 | * does not applied chown yet. Let's wait a moment. */ | |
1267 | xusleep(25000); | |
1268 | } while (cnt++ < 16); | |
1269 | ||
10ee5932 KZ |
1270 | if (dev_fd < 0) { |
1271 | rc = -errno; | |
1272 | goto err; | |
1273 | } | |
1274 | ||
0bf03740 | 1275 | DBG(SETUP, ul_debugobj(lc, "device open: OK")); |
d356c5d2 | 1276 | |
10ee5932 KZ |
1277 | /* |
1278 | * Set FD | |
1279 | */ | |
1280 | if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) { | |
1281 | rc = -errno; | |
2cde9865 | 1282 | errsv = errno; |
0bf03740 | 1283 | DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD failed: %m")); |
10ee5932 KZ |
1284 | goto err; |
1285 | } | |
d356c5d2 | 1286 | |
0bf03740 | 1287 | DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD: OK")); |
d356c5d2 | 1288 | |
aee31ddc | 1289 | if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info)) { |
2cde9865 KZ |
1290 | rc = -errno; |
1291 | errsv = errno; | |
0bf03740 | 1292 | DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m")); |
10ee5932 | 1293 | goto err; |
aee31ddc | 1294 | } |
d356c5d2 | 1295 | |
0bf03740 | 1296 | DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64: OK")); |
d356c5d2 | 1297 | |
293714c0 JM |
1298 | if ((rc = loopcxt_check_size(lc, file_fd))) |
1299 | goto err; | |
1300 | ||
1301 | close(file_fd); | |
293714c0 | 1302 | |
10ee5932 KZ |
1303 | memset(&lc->info, 0, sizeof(lc->info)); |
1304 | lc->has_info = 0; | |
6c224de1 | 1305 | lc->info_failed = 0; |
10ee5932 | 1306 | |
0bf03740 | 1307 | DBG(SETUP, ul_debugobj(lc, "success [rc=0]")); |
10ee5932 KZ |
1308 | return 0; |
1309 | err: | |
1310 | if (file_fd >= 0) | |
1311 | close(file_fd); | |
f7e21185 | 1312 | if (dev_fd >= 0 && rc != -EBUSY) |
10ee5932 | 1313 | ioctl(dev_fd, LOOP_CLR_FD, 0); |
2cde9865 KZ |
1314 | if (errsv) |
1315 | errno = errsv; | |
10ee5932 | 1316 | |
0bf03740 | 1317 | DBG(SETUP, ul_debugobj(lc, "failed [rc=%d]", rc)); |
10ee5932 KZ |
1318 | return rc; |
1319 | } | |
1320 | ||
293714c0 JM |
1321 | int loopcxt_set_capacity(struct loopdev_cxt *lc) |
1322 | { | |
1323 | int fd = loopcxt_get_fd(lc); | |
1324 | ||
1325 | if (fd < 0) | |
1326 | return -EINVAL; | |
1327 | ||
1328 | /* Kernels prior to v2.6.30 don't support this ioctl */ | |
1329 | if (ioctl(fd, LOOP_SET_CAPACITY, 0) < 0) { | |
1330 | int rc = -errno; | |
0bf03740 | 1331 | DBG(CXT, ul_debugobj(lc, "LOOP_SET_CAPACITY failed: %m")); |
293714c0 JM |
1332 | return rc; |
1333 | } | |
1334 | ||
0bf03740 | 1335 | DBG(CXT, ul_debugobj(lc, "capacity set")); |
293714c0 JM |
1336 | return 0; |
1337 | } | |
1338 | ||
64c3bb3c ML |
1339 | int loopcxt_set_dio(struct loopdev_cxt *lc, unsigned long use_dio) |
1340 | { | |
1341 | int fd = loopcxt_get_fd(lc); | |
1342 | ||
1343 | if (fd < 0) | |
1344 | return -EINVAL; | |
1345 | ||
1346 | /* Kernels prior to v4.4 don't support this ioctl */ | |
1347 | if (ioctl(fd, LOOP_SET_DIRECT_IO, use_dio) < 0) { | |
1348 | int rc = -errno; | |
1349 | DBG(CXT, ul_debugobj(lc, "LOOP_SET_DIRECT_IO failed: %m")); | |
1350 | return rc; | |
1351 | } | |
1352 | ||
1353 | DBG(CXT, ul_debugobj(lc, "direct io set")); | |
1354 | return 0; | |
1355 | } | |
1356 | ||
10ee5932 KZ |
1357 | int loopcxt_delete_device(struct loopdev_cxt *lc) |
1358 | { | |
1359 | int fd = loopcxt_get_fd(lc); | |
1360 | ||
1361 | if (fd < 0) | |
1362 | return -EINVAL; | |
1363 | ||
aee31ddc | 1364 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { |
0bf03740 | 1365 | DBG(CXT, ul_debugobj(lc, "LOOP_CLR_FD failed: %m")); |
10ee5932 | 1366 | return -errno; |
aee31ddc KZ |
1367 | } |
1368 | ||
0bf03740 | 1369 | DBG(CXT, ul_debugobj(lc, "device removed")); |
10ee5932 KZ |
1370 | return 0; |
1371 | } | |
1372 | ||
3cb2413b KZ |
1373 | int loopcxt_add_device(struct loopdev_cxt *lc) |
1374 | { | |
1375 | int rc = -EINVAL; | |
1376 | int ctl, nr = -1; | |
1377 | const char *p, *dev = loopcxt_get_device(lc); | |
1378 | ||
1379 | if (!dev) | |
1380 | goto done; | |
1381 | ||
1382 | if (!(lc->flags & LOOPDEV_FL_CONTROL)) { | |
1383 | rc = -ENOSYS; | |
1384 | goto done; | |
1385 | } | |
1386 | ||
1387 | p = strrchr(dev, '/'); | |
1388 | if (!p || (sscanf(p, "/loop%d", &nr) != 1 && sscanf(p, "/%d", &nr) != 1) | |
1389 | || nr < 0) | |
1390 | goto done; | |
1391 | ||
1392 | ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC); | |
1393 | if (ctl >= 0) { | |
0bf03740 | 1394 | DBG(CXT, ul_debugobj(lc, "add_device %d", nr)); |
3cb2413b KZ |
1395 | rc = ioctl(ctl, LOOP_CTL_ADD, nr); |
1396 | close(ctl); | |
1397 | } | |
663bf040 | 1398 | lc->control_ok = rc >= 0 ? 1 : 0; |
3cb2413b | 1399 | done: |
0bf03740 | 1400 | DBG(CXT, ul_debugobj(lc, "add_device done [rc=%d]", rc)); |
3cb2413b KZ |
1401 | return rc; |
1402 | } | |
1403 | ||
59d749c3 KZ |
1404 | /* |
1405 | * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older | |
1406 | * kernels we have to check all loop devices to found unused one. | |
1407 | * | |
1408 | * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484. | |
1409 | */ | |
10ee5932 KZ |
1410 | int loopcxt_find_unused(struct loopdev_cxt *lc) |
1411 | { | |
0b14bf7a | 1412 | int rc = -1; |
10ee5932 | 1413 | |
0bf03740 | 1414 | DBG(CXT, ul_debugobj(lc, "find_unused requested")); |
aee31ddc | 1415 | |
0b14bf7a | 1416 | if (lc->flags & LOOPDEV_FL_CONTROL) { |
b1fa3e22 | 1417 | int ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC); |
10ee5932 | 1418 | |
0b14bf7a KZ |
1419 | if (ctl >= 0) |
1420 | rc = ioctl(ctl, LOOP_CTL_GET_FREE); | |
1421 | if (rc >= 0) { | |
1422 | char name[16]; | |
1423 | snprintf(name, sizeof(name), "loop%d", rc); | |
1424 | ||
1425 | rc = loopiter_set_device(lc, name); | |
1426 | } | |
663bf040 | 1427 | lc->control_ok = ctl >= 0 && rc == 0 ? 1 : 0; |
0b14bf7a KZ |
1428 | if (ctl >= 0) |
1429 | close(ctl); | |
0bf03740 | 1430 | DBG(CXT, ul_debugobj(lc, "find_unused by loop-control [rc=%d]", rc)); |
0b14bf7a KZ |
1431 | } |
1432 | ||
1433 | if (rc < 0) { | |
1434 | rc = loopcxt_init_iterator(lc, LOOPITER_FL_FREE); | |
1435 | if (rc) | |
1436 | return rc; | |
10ee5932 | 1437 | |
0b14bf7a KZ |
1438 | rc = loopcxt_next(lc); |
1439 | loopcxt_deinit_iterator(lc); | |
0bf03740 | 1440 | DBG(CXT, ul_debugobj(lc, "find_unused by scan [rc=%d]", rc)); |
0b14bf7a | 1441 | } |
10ee5932 KZ |
1442 | return rc; |
1443 | } | |
1444 | ||
1445 | ||
1446 | ||
1447 | /* | |
1448 | * Return: TRUE/FALSE | |
1449 | */ | |
1450 | int loopdev_is_autoclear(const char *device) | |
1451 | { | |
1452 | struct loopdev_cxt lc; | |
1453 | int rc; | |
1454 | ||
1455 | if (!device) | |
1456 | return 0; | |
1457 | ||
defa0710 KZ |
1458 | rc = loopcxt_init(&lc, 0); |
1459 | if (!rc) | |
1460 | rc = loopcxt_set_device(&lc, device); | |
1461 | if (!rc) | |
1462 | rc = loopcxt_is_autoclear(&lc); | |
10ee5932 | 1463 | |
defa0710 | 1464 | loopcxt_deinit(&lc); |
10ee5932 KZ |
1465 | return rc; |
1466 | } | |
1467 | ||
1468 | char *loopdev_get_backing_file(const char *device) | |
1469 | { | |
1470 | struct loopdev_cxt lc; | |
defa0710 | 1471 | char *res = NULL; |
10ee5932 KZ |
1472 | |
1473 | if (!device) | |
1474 | return NULL; | |
defa0710 KZ |
1475 | if (loopcxt_init(&lc, 0)) |
1476 | return NULL; | |
1477 | if (loopcxt_set_device(&lc, device) == 0) | |
1478 | res = loopcxt_get_backing_file(&lc); | |
10ee5932 | 1479 | |
10ee5932 | 1480 | loopcxt_deinit(&lc); |
10ee5932 KZ |
1481 | return res; |
1482 | } | |
1483 | ||
1484 | /* | |
1485 | * Returns: TRUE/FALSE | |
1486 | */ | |
1487 | int loopdev_is_used(const char *device, const char *filename, | |
1488 | uint64_t offset, int flags) | |
1489 | { | |
1490 | struct loopdev_cxt lc; | |
6c224de1 | 1491 | struct stat st; |
10ee5932 KZ |
1492 | int rc = 0; |
1493 | ||
8b470b20 | 1494 | if (!device || !filename) |
10ee5932 KZ |
1495 | return 0; |
1496 | ||
defa0710 KZ |
1497 | rc = loopcxt_init(&lc, 0); |
1498 | if (!rc) | |
1499 | rc = loopcxt_set_device(&lc, device); | |
1500 | if (rc) | |
1501 | return rc; | |
10ee5932 | 1502 | |
6c224de1 KZ |
1503 | rc = !stat(filename, &st); |
1504 | rc = loopcxt_is_used(&lc, rc ? &st : NULL, filename, offset, flags); | |
10ee5932 | 1505 | |
10ee5932 | 1506 | loopcxt_deinit(&lc); |
10ee5932 KZ |
1507 | return rc; |
1508 | } | |
1509 | ||
1510 | int loopdev_delete(const char *device) | |
1511 | { | |
1512 | struct loopdev_cxt lc; | |
1513 | int rc; | |
1514 | ||
8b470b20 KZ |
1515 | if (!device) |
1516 | return -EINVAL; | |
1517 | ||
defa0710 KZ |
1518 | rc = loopcxt_init(&lc, 0); |
1519 | if (!rc) | |
1520 | rc = loopcxt_set_device(&lc, device); | |
10ee5932 KZ |
1521 | if (!rc) |
1522 | rc = loopcxt_delete_device(&lc); | |
1523 | loopcxt_deinit(&lc); | |
1524 | return rc; | |
1525 | } | |
1526 | ||
1527 | /* | |
1528 | * Returns: 0 = success, < 0 error, 1 not found | |
1529 | */ | |
1530 | int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, const char *filename, | |
1531 | uint64_t offset, int flags) | |
1532 | { | |
6c224de1 KZ |
1533 | int rc, hasst; |
1534 | struct stat st; | |
10ee5932 KZ |
1535 | |
1536 | if (!filename) | |
1537 | return -EINVAL; | |
1538 | ||
6c224de1 KZ |
1539 | hasst = !stat(filename, &st); |
1540 | ||
10ee5932 KZ |
1541 | rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED); |
1542 | if (rc) | |
1543 | return rc; | |
1544 | ||
6c224de1 | 1545 | while ((rc = loopcxt_next(lc)) == 0) { |
10ee5932 | 1546 | |
6c224de1 KZ |
1547 | if (loopcxt_is_used(lc, hasst ? &st : NULL, |
1548 | filename, offset, flags)) | |
1549 | break; | |
10ee5932 KZ |
1550 | } |
1551 | ||
1552 | loopcxt_deinit_iterator(lc); | |
1553 | return rc; | |
1554 | } | |
1555 | ||
1556 | /* | |
1557 | * Returns allocated string with device name | |
1558 | */ | |
1559 | char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int flags) | |
1560 | { | |
1561 | struct loopdev_cxt lc; | |
1562 | char *res = NULL; | |
1563 | ||
1564 | if (!filename) | |
1565 | return NULL; | |
1566 | ||
defa0710 KZ |
1567 | if (loopcxt_init(&lc, 0)) |
1568 | return NULL; | |
06f588f7 | 1569 | if (loopcxt_find_by_backing_file(&lc, filename, offset, flags) == 0) |
10ee5932 KZ |
1570 | res = loopcxt_strdup_device(&lc); |
1571 | loopcxt_deinit(&lc); | |
1572 | ||
1573 | return res; | |
1574 | } | |
1575 | ||
d5688130 KZ |
1576 | /* |
1577 | * Returns number of loop devices associated with @file, if only one loop | |
1578 | * device is associeted with the given @filename and @loopdev is not NULL then | |
1579 | * @loopdev returns name of the device. | |
1580 | */ | |
1581 | int loopdev_count_by_backing_file(const char *filename, char **loopdev) | |
1582 | { | |
1583 | struct loopdev_cxt lc; | |
defa0710 | 1584 | int count = 0, rc; |
d5688130 KZ |
1585 | |
1586 | if (!filename) | |
1587 | return -1; | |
1588 | ||
defa0710 KZ |
1589 | rc = loopcxt_init(&lc, 0); |
1590 | if (rc) | |
1591 | return rc; | |
d5688130 KZ |
1592 | if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED)) |
1593 | return -1; | |
1594 | ||
1595 | while(loopcxt_next(&lc) == 0) { | |
1596 | char *backing = loopcxt_get_backing_file(&lc); | |
1597 | ||
1598 | if (!backing || strcmp(backing, filename)) { | |
1599 | free(backing); | |
1600 | continue; | |
1601 | } | |
1602 | ||
1603 | free(backing); | |
1604 | if (loopdev && count == 0) | |
1605 | *loopdev = loopcxt_strdup_device(&lc); | |
1606 | count++; | |
1607 | } | |
1608 | ||
1609 | loopcxt_deinit(&lc); | |
1610 | ||
1611 | if (loopdev && count > 1) { | |
1612 | free(*loopdev); | |
1613 | *loopdev = NULL; | |
1614 | } | |
1615 | return count; | |
1616 | } | |
1617 |