]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/loopdev.c
Merge branch 'chfn_gecos' of https://github.com/mator/util-linux
[thirdparty/util-linux.git] / lib / loopdev.c
CommitLineData
c444a71b 1
10ee5932 2/*
0f23ee0c
KZ
3 * No copyright is claimed. This code is in the public domain; do with
4 * it what you wish.
5 *
6 * Written by Karel Zak <kzak@redhat.com>
10ee5932
KZ
7 *
8 * -- based on mount/losetup.c
9 *
10 * Simple library for work with loop devices.
11 *
12 * - requires kernel 2.6.x
13 * - reads info from /sys/block/loop<N>/loop/<attr> (new kernels)
14 * - reads info by ioctl
15 * - supports *unlimited* number of loop devices
16 * - supports /dev/loop<N> as well as /dev/loop/<N>
17 * - minimize overhead (fd, loopinfo, ... are shared for all operations)
18 * - setup (associate device and backing file)
19 * - delete (dis-associate file)
20 * - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported
21 * - extendible
22 */
23#include <stdio.h>
24#include <stdint.h>
25#include <string.h>
26#include <ctype.h>
27#include <fcntl.h>
10ee5932
KZ
28#include <stdlib.h>
29#include <unistd.h>
30#include <sys/ioctl.h>
31#include <sys/stat.h>
32#include <sys/mman.h>
10ee5932
KZ
33#include <inttypes.h>
34#include <dirent.h>
10ee5932
KZ
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"
fbc2fe82 44#include "fileutils.h"
10ee5932 45
0ae7bb11
KZ
46#define LOOPDEV_MAX_TRIES 10
47
0bf03740
KZ
48/*
49 * Debug stuff (based on include/debug.h)
50 */
2ba641e5 51static UL_DEBUG_DEFINE_MASK(loopdev);
0bf03740 52UL_DEBUG_DEFINE_MASKNAMES(loopdev) = UL_DEBUG_EMPTY_MASKNAMES;
aee31ddc 53
0bf03740
KZ
54#define LOOPDEV_DEBUG_INIT (1 << 1)
55#define LOOPDEV_DEBUG_CXT (1 << 2)
56#define LOOPDEV_DEBUG_ITER (1 << 3)
57#define LOOPDEV_DEBUG_SETUP (1 << 4)
aee31ddc 58
0bf03740
KZ
59#define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x)
60#define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x)
aee31ddc 61
6d00cfb2
KZ
62#define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(loopdev)
63#include "debugobj.h"
64
0bf03740 65static void loopdev_init_debug(void)
aee31ddc 66{
0bf03740
KZ
67 if (loopdev_debug_mask)
68 return;
a15dca2f 69 __UL_INIT_DEBUG_FROM_ENV(loopdev, LOOPDEV_DEBUG_, 0, LOOPDEV_DEBUG);
aee31ddc
KZ
70}
71
e4062c72
KZ
72/*
73 * see loopcxt_init()
74 */
10ee5932 75#define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
e4062c72
KZ
76#define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
77 && !loopcxt_ioctl_enabled(_lc)
10ee5932 78
9d5424c2
KZ
79/*
80 * Calls @x and repeat on EAGAIN
81 */
82#define repeat_on_eagain(x) __extension__ ({ \
83 int _c = 0, _e; \
84 do { \
85 errno = 0; \
86 _e = x; \
87 if (_e == 0 || errno != EAGAIN) \
88 break; \
89 if (_c >= LOOPDEV_MAX_TRIES) \
90 break; \
91 xusleep(250000); \
92 _c++; \
93 } while (1); \
94 _e == 0 ? 0 : errno ? -errno : -1; \
95 })
96
10ee5932
KZ
97/*
98 * @lc: context
99 * @device: device name, absolute device path or NULL to reset the current setting
100 *
101 * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
102 * names ("loop<N>") are converted to the path (/dev/loop<N> or to
103 * /dev/loop/<N>)
104 *
85794fb0
KZ
105 * This sets the device name, but does not check if the device exists!
106 *
10ee5932
KZ
107 * Returns: <0 on error, 0 on success
108 */
109int loopcxt_set_device(struct loopdev_cxt *lc, const char *device)
110{
111 if (!lc)
112 return -EINVAL;
113
f4bf9592 114 if (lc->fd >= 0) {
10ee5932 115 close(lc->fd);
0bf03740 116 DBG(CXT, ul_debugobj(lc, "closing old open fd"));
f4bf9592 117 }
10ee5932 118 lc->fd = -1;
ced1142d 119 lc->mode = O_RDONLY;
75d239ff 120 lc->blocksize = 0;
10ee5932 121 lc->has_info = 0;
6c224de1 122 lc->info_failed = 0;
10ee5932 123 *lc->device = '\0';
d5fd456c 124 memset(&lc->config, 0, sizeof(lc->config));
10ee5932
KZ
125
126 /* set new */
127 if (device) {
128 if (*device != '/') {
129 const char *dir = _PATH_DEV;
130
131 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
132 if (lc->flags & LOOPDEV_FL_DEVSUBDIR) {
133 if (strlen(device) < 5)
134 return -1;
135 device += 4;
136 dir = _PATH_DEV_LOOP "/"; /* _PATH_DEV uses tailing slash */
137 }
138 snprintf(lc->device, sizeof(lc->device), "%s%s",
139 dir, device);
91072cd4
KZ
140 } else
141 xstrncpy(lc->device, device, sizeof(lc->device));
142
0bf03740 143 DBG(CXT, ul_debugobj(lc, "%s name assigned", device));
10ee5932
KZ
144 }
145
7604f85f
KZ
146 ul_unref_path(lc->sysfs);
147 lc->sysfs = NULL;
10ee5932
KZ
148 return 0;
149}
150
dab13032 151int loopcxt_has_device(struct loopdev_cxt *lc)
c7e0925d
KZ
152{
153 return lc && *lc->device;
154}
155
10ee5932
KZ
156/*
157 * @lc: context
158 * @flags: LOOPDEV_FL_* flags
159 *
3fd1f771 160 * Initialize loop handler.
10ee5932 161 *
fd7f0718
KZ
162 * We have two sets of the flags:
163 *
164 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
165 *
166 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
167 *
10ee5932
KZ
168 * Returns: <0 on error, 0 on success.
169 */
170int loopcxt_init(struct loopdev_cxt *lc, int flags)
171{
defa0710 172 int rc;
0b14bf7a 173 struct stat st;
6219c25e 174 struct loopdev_cxt dummy = UL_LOOPDEVCXT_EMPTY;
0b14bf7a 175
10ee5932
KZ
176 if (!lc)
177 return -EINVAL;
178
0bf03740
KZ
179 loopdev_init_debug();
180 DBG(CXT, ul_debugobj(lc, "initialize context"));
181
6219c25e 182 memcpy(lc, &dummy, sizeof(dummy));
10ee5932 183 lc->flags = flags;
defa0710
KZ
184
185 rc = loopcxt_set_device(lc, NULL);
186 if (rc)
187 return rc;
10ee5932 188
df0f2ad7
KZ
189 if (stat(_PATH_SYS_BLOCK, &st) || !S_ISDIR(st.st_mode)) {
190 lc->flags |= LOOPDEV_FL_NOSYSFS;
191 lc->flags &= ~LOOPDEV_FL_NOIOCTL;
0bf03740 192 DBG(CXT, ul_debugobj(lc, "init: disable /sys usage"));
df0f2ad7
KZ
193 }
194
82b4082e 195 if (!(lc->flags & LOOPDEV_FL_NOSYSFS) &&
f4bf9592 196 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
10ee5932
KZ
197 /*
198 * Use only sysfs for basic information about loop devices
199 */
200 lc->flags |= LOOPDEV_FL_NOIOCTL;
0bf03740 201 DBG(CXT, ul_debugobj(lc, "init: ignore ioctls"));
f4bf9592 202 }
10ee5932 203
f4bf9592 204 if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) {
0b14bf7a 205 lc->flags |= LOOPDEV_FL_CONTROL;
0bf03740 206 DBG(CXT, ul_debugobj(lc, "init: loop-control detected "));
f4bf9592 207 }
0b14bf7a 208
10ee5932
KZ
209 return 0;
210}
211
212/*
213 * @lc: context
214 *
215 * Deinitialize loop context
216 */
217void loopcxt_deinit(struct loopdev_cxt *lc)
218{
82756a74
KZ
219 int errsv = errno;
220
10ee5932
KZ
221 if (!lc)
222 return;
223
0bf03740 224 DBG(CXT, ul_debugobj(lc, "de-initialize"));
aee31ddc 225
10ee5932
KZ
226 free(lc->filename);
227 lc->filename = NULL;
228
defa0710 229 ignore_result( loopcxt_set_device(lc, NULL) );
10ee5932 230 loopcxt_deinit_iterator(lc);
82756a74
KZ
231
232 errno = errsv;
10ee5932
KZ
233}
234
235/*
236 * @lc: context
237 *
238 * Returns newly allocated device path.
239 */
240char *loopcxt_strdup_device(struct loopdev_cxt *lc)
241{
cba69bb5 242 if (!lc || !*lc->device)
10ee5932
KZ
243 return NULL;
244 return strdup(lc->device);
245}
246
247/*
248 * @lc: context
249 *
250 * Returns pointer device name in the @lc struct.
251 */
252const char *loopcxt_get_device(struct loopdev_cxt *lc)
253{
cba69bb5 254 return lc && *lc->device ? lc->device : NULL;
10ee5932
KZ
255}
256
257/*
258 * @lc: context
259 *
260 * Returns pointer to the sysfs context (see lib/sysfs.c)
261 */
7604f85f 262static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)
10ee5932
KZ
263{
264 if (!lc || !*lc->device || (lc->flags & LOOPDEV_FL_NOSYSFS))
265 return NULL;
266
7604f85f
KZ
267 if (!lc->sysfs) {
268 dev_t devno = sysfs_devname_to_devno(lc->device);
aee31ddc 269 if (!devno) {
0bf03740 270 DBG(CXT, ul_debugobj(lc, "sysfs: failed devname to devno"));
10ee5932 271 return NULL;
aee31ddc 272 }
7604f85f
KZ
273
274 lc->sysfs = ul_new_sysfs_path(devno, NULL, NULL);
275 if (!lc->sysfs)
0bf03740 276 DBG(CXT, ul_debugobj(lc, "sysfs: init failed"));
10ee5932 277 }
aee31ddc 278
7604f85f 279 return lc->sysfs;
10ee5932
KZ
280}
281
ced1142d 282static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode)
10ee5932 283{
ced1142d
KZ
284 int old = -1;
285
10ee5932 286 if (!lc || !*lc->device)
fd7f0718 287 return -EINVAL;
10ee5932 288
ced1142d
KZ
289 /* It's okay to return a FD with read-write permissions if someone
290 * asked for read-only, but you shouldn't do the opposite.
291 *
292 * (O_RDONLY is a widely usable default.)
293 */
294 if (lc->fd >= 0 && mode == O_RDWR && lc->mode == O_RDONLY) {
295 DBG(CXT, ul_debugobj(lc, "closing already open device (mode mismatch)"));
296 old = lc->fd;
297 lc->fd = -1;
298 }
299
fd7f0718 300 if (lc->fd < 0) {
ced1142d 301 lc->mode = mode;
b1fa3e22 302 lc->fd = open(lc->device, lc->mode | O_CLOEXEC);
0bf03740 303 DBG(CXT, ul_debugobj(lc, "open %s [%s]: %m", lc->device,
ced1142d
KZ
304 mode == O_RDONLY ? "ro" :
305 mode == O_RDWR ? "rw" : "??"));
306
307 if (lc->fd < 0 && old >= 0) {
308 /* restore original on error */
309 lc->fd = old;
310 old = -1;
311 }
fd7f0718 312 }
ced1142d
KZ
313
314 if (old >= 0)
315 close(old);
10ee5932
KZ
316 return lc->fd;
317}
318
ced1142d
KZ
319/* default is read-only file descriptor, it's enough for all ioctls */
320int loopcxt_get_fd(struct loopdev_cxt *lc)
321{
322 return __loopcxt_get_fd(lc, O_RDONLY);
323}
324
325int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, mode_t mode)
fd7f0718
KZ
326{
327 if (!lc)
328 return -EINVAL;
329
330 lc->fd = fd;
331 lc->mode = mode;
332 return 0;
333}
334
10ee5932
KZ
335/*
336 * @lc: context
337 * @flags: LOOPITER_FL_* flags
338 *
29e204d1 339 * Iterator can be used to scan list of the free or used loop devices.
10ee5932
KZ
340 *
341 * Returns: <0 on error, 0 on success
342 */
343int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags)
344{
345 struct loopdev_iter *iter;
346 struct stat st;
347
348 if (!lc)
349 return -EINVAL;
350
aee31ddc 351
10ee5932 352 iter = &lc->iter;
0bf03740 353 DBG(ITER, ul_debugobj(iter, "initialize"));
10ee5932
KZ
354
355 /* always zeroize
356 */
357 memset(iter, 0, sizeof(*iter));
358 iter->ncur = -1;
359 iter->flags = flags;
360 iter->default_check = 1;
361
362 if (!lc->extra_check) {
363 /*
364 * Check for /dev/loop/<N> subdirectory
365 */
82b4082e 366 if (!(lc->flags & LOOPDEV_FL_DEVSUBDIR) &&
10ee5932
KZ
367 stat(_PATH_DEV_LOOP, &st) == 0 && S_ISDIR(st.st_mode))
368 lc->flags |= LOOPDEV_FL_DEVSUBDIR;
369
370 lc->extra_check = 1;
371 }
372 return 0;
373}
374
375/*
376 * @lc: context
377 *
378 * Returns: <0 on error, 0 on success
379 */
380int loopcxt_deinit_iterator(struct loopdev_cxt *lc)
381{
7552258a 382 struct loopdev_iter *iter;
10ee5932
KZ
383
384 if (!lc)
385 return -EINVAL;
386
387 iter = &lc->iter;
0bf03740 388 DBG(ITER, ul_debugobj(iter, "de-initialize"));
10ee5932
KZ
389
390 free(iter->minors);
391 if (iter->proc)
392 fclose(iter->proc);
e4062c72
KZ
393 if (iter->sysblock)
394 closedir(iter->sysblock);
9a94b634
KZ
395
396 memset(iter, 0, sizeof(*iter));
10ee5932
KZ
397 return 0;
398}
399
400/*
401 * Same as loopcxt_set_device, but also checks if the device is
11026083 402 * associated with any file.
10ee5932
KZ
403 *
404 * Returns: <0 on error, 0 on success, 1 device does not match with
405 * LOOPITER_FL_{USED,FREE} flags.
406 */
407static int loopiter_set_device(struct loopdev_cxt *lc, const char *device)
408{
409 int rc = loopcxt_set_device(lc, device);
410 int used;
411
412 if (rc)
413 return rc;
414
415 if (!(lc->iter.flags & LOOPITER_FL_USED) &&
416 !(lc->iter.flags & LOOPITER_FL_FREE))
417 return 0; /* caller does not care about device status */
418
85794fb0 419 if (!is_loopdev(lc->device)) {
0bf03740 420 DBG(ITER, ul_debugobj(&lc->iter, "%s does not exist", lc->device));
85794fb0
KZ
421 return -errno;
422 }
423
0bf03740 424 DBG(ITER, ul_debugobj(&lc->iter, "%s exist", lc->device));
85794fb0 425
10ee5932
KZ
426 used = loopcxt_get_offset(lc, NULL) == 0;
427
428 if ((lc->iter.flags & LOOPITER_FL_USED) && used)
429 return 0;
430
431 if ((lc->iter.flags & LOOPITER_FL_FREE) && !used)
432 return 0;
433
0bf03740
KZ
434 DBG(ITER, ul_debugobj(&lc->iter, "failed to use %s device", lc->device));
435
defa0710 436 ignore_result( loopcxt_set_device(lc, NULL) );
10ee5932
KZ
437 return 1;
438}
439
440static int cmpnum(const void *p1, const void *p2)
441{
0145c00a
KZ
442 return (((* (const int *) p1) > (* (const int *) p2)) -
443 ((* (const int *) p1) < (* (const int *) p2)));
10ee5932
KZ
444}
445
446/*
447 * The classic scandir() is more expensive and less portable.
448 * We needn't full loop device names -- loop numbers (loop<N>)
449 * are enough.
450 */
451static int loop_scandir(const char *dirname, int **ary, int hasprefix)
452{
453 DIR *dir;
454 struct dirent *d;
455 unsigned int n, count = 0, arylen = 0;
456
457 if (!dirname || !ary)
458 return 0;
0bf03740
KZ
459
460 DBG(ITER, ul_debug("scan dir: %s", dirname));
461
10ee5932
KZ
462 dir = opendir(dirname);
463 if (!dir)
464 return 0;
10ee5932
KZ
465 free(*ary);
466 *ary = NULL;
467
468 while((d = readdir(dir))) {
469#ifdef _DIRENT_HAVE_D_TYPE
470 if (d->d_type != DT_BLK && d->d_type != DT_UNKNOWN &&
471 d->d_type != DT_LNK)
472 continue;
473#endif
474 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
475 continue;
476
477 if (hasprefix) {
478 /* /dev/loop<N> */
479 if (sscanf(d->d_name, "loop%u", &n) != 1)
480 continue;
481 } else {
482 /* /dev/loop/<N> */
483 char *end = NULL;
484
f0fe8ab5 485 errno = 0;
10ee5932
KZ
486 n = strtol(d->d_name, &end, 10);
487 if (d->d_name == end || (end && *end) || errno)
488 continue;
489 }
490 if (n < LOOPDEV_DEFAULT_NNODES)
491 continue; /* ignore loop<0..7> */
492
493 if (count + 1 > arylen) {
494 int *tmp;
495
496 arylen += 1;
497
64d6d400 498 tmp = reallocarray(*ary, arylen, sizeof(int));
10ee5932
KZ
499 if (!tmp) {
500 free(*ary);
34f6177a 501 *ary = NULL;
2e566efd 502 closedir(dir);
10ee5932
KZ
503 return -1;
504 }
505 *ary = tmp;
506 }
fc8b1f36
KZ
507 if (*ary)
508 (*ary)[count++] = n;
10ee5932 509 }
fc8b1f36 510 if (count && *ary)
10ee5932
KZ
511 qsort(*ary, count, sizeof(int), cmpnum);
512
513 closedir(dir);
514 return count;
515}
516
e4062c72
KZ
517/*
518 * Set the next *used* loop device according to /proc/partitions.
519 *
520 * Loop devices smaller than 512 bytes are invisible for this function.
521 */
522static int loopcxt_next_from_proc(struct loopdev_cxt *lc)
523{
524 struct loopdev_iter *iter = &lc->iter;
525 char buf[BUFSIZ];
526
0bf03740 527 DBG(ITER, ul_debugobj(iter, "scan /proc/partitions"));
e4062c72
KZ
528
529 if (!iter->proc)
b1fa3e22 530 iter->proc = fopen(_PATH_PROC_PARTITIONS, "r" UL_CLOEXECSTR);
e4062c72
KZ
531 if (!iter->proc)
532 return 1;
533
534 while (fgets(buf, sizeof(buf), iter->proc)) {
535 unsigned int m;
657d9adb 536 char name[128 + 1];
e4062c72
KZ
537
538
539 if (sscanf(buf, " %u %*s %*s %128[^\n ]",
540 &m, name) != 2 || m != LOOPDEV_MAJOR)
541 continue;
542
0bf03740 543 DBG(ITER, ul_debugobj(iter, "checking %s", name));
e4062c72
KZ
544
545 if (loopiter_set_device(lc, name) == 0)
546 return 0;
547 }
548
549 return 1;
550}
551
552/*
553 * Set the next *used* loop device according to
554 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
555 *
556 * This is preferred method.
557 */
558static int loopcxt_next_from_sysfs(struct loopdev_cxt *lc)
559{
560 struct loopdev_iter *iter = &lc->iter;
561 struct dirent *d;
562 int fd;
563
0bf03740 564 DBG(ITER, ul_debugobj(iter, "scanning /sys/block"));
e4062c72
KZ
565
566 if (!iter->sysblock)
567 iter->sysblock = opendir(_PATH_SYS_BLOCK);
568
569 if (!iter->sysblock)
570 return 1;
571
572 fd = dirfd(iter->sysblock);
573
574 while ((d = readdir(iter->sysblock))) {
acecab61 575 char name[NAME_MAX + 18 + 1];
e4062c72
KZ
576 struct stat st;
577
0bf03740 578 DBG(ITER, ul_debugobj(iter, "check %s", d->d_name));
e4062c72
KZ
579
580 if (strcmp(d->d_name, ".") == 0
581 || strcmp(d->d_name, "..") == 0
582 || strncmp(d->d_name, "loop", 4) != 0)
583 continue;
584
585 snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name);
2208b3cc 586 if (fstatat(fd, name, &st, 0) != 0)
e4062c72
KZ
587 continue;
588
589 if (loopiter_set_device(lc, d->d_name) == 0)
590 return 0;
591 }
592
593 return 1;
594}
595
10ee5932
KZ
596/*
597 * @lc: context, has to initialized by loopcxt_init_iterator()
598 *
af5efcd8 599 * Returns: 0 on success, < 0 on error, 1 at the end of scanning. The details
10ee5932
KZ
600 * about the current loop device are available by
601 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
602 */
603int loopcxt_next(struct loopdev_cxt *lc)
604{
605 struct loopdev_iter *iter;
606
607 if (!lc)
608 return -EINVAL;
aee31ddc 609
aee31ddc 610
10ee5932
KZ
611 iter = &lc->iter;
612 if (iter->done)
613 return 1;
614
0bf03740
KZ
615 DBG(ITER, ul_debugobj(iter, "next"));
616
10ee5932
KZ
617 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
618 */
619 if (iter->flags & LOOPITER_FL_USED) {
e4062c72
KZ
620 int rc;
621
622 if (loopcxt_sysfs_available(lc))
623 rc = loopcxt_next_from_sysfs(lc);
624 else
625 rc = loopcxt_next_from_proc(lc);
626 if (rc == 0)
627 return 0;
10ee5932
KZ
628 goto done;
629 }
630
631 /* B) Classic way, try first eight loop devices (default number
632 * of loop devices). This is enough for 99% of all cases.
633 */
634 if (iter->default_check) {
0bf03740 635 DBG(ITER, ul_debugobj(iter, "next: default check"));
10ee5932
KZ
636 for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES;
637 iter->ncur++) {
638 char name[16];
639 snprintf(name, sizeof(name), "loop%d", iter->ncur);
640
641 if (loopiter_set_device(lc, name) == 0)
642 return 0;
643 }
644 iter->default_check = 0;
645 }
646
647 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
648 */
649 if (!iter->minors) {
0bf03740 650 DBG(ITER, ul_debugobj(iter, "next: scanning /dev"));
10ee5932
KZ
651 iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ?
652 loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) :
653 loop_scandir(_PATH_DEV, &iter->minors, 1);
654 iter->ncur = -1;
655 }
656 for (++iter->ncur; iter->ncur < iter->nminors; iter->ncur++) {
657 char name[16];
658 snprintf(name, sizeof(name), "loop%d", iter->minors[iter->ncur]);
659
660 if (loopiter_set_device(lc, name) == 0)
661 return 0;
662 }
663done:
664 loopcxt_deinit_iterator(lc);
665 return 1;
666}
667
668/*
669 * @device: path to device
670 */
671int is_loopdev(const char *device)
672{
673 struct stat st;
fbc2fe82
KZ
674 int rc = 0;
675
676 if (!device || stat(device, &st) != 0 || !S_ISBLK(st.st_mode))
677 rc = 0;
678 else if (major(st.st_rdev) == LOOPDEV_MAJOR)
679 rc = 1;
d4423cce 680 else if (sysfs_devno_is_wholedisk(st.st_rdev)) {
fbc2fe82
KZ
681 /* It's possible that kernel creates a device with a different
682 * major number ... check by /sys it's really loop device.
683 */
684 char name[PATH_MAX], *cn, *p = NULL;
685
686 snprintf(name, sizeof(name), _PATH_SYS_DEVBLOCK "/%d:%d",
687 major(st.st_rdev), minor(st.st_rdev));
688 cn = canonicalize_path(name);
689 if (cn)
690 p = stripoff_last_component(cn);
691 rc = p && startswith(p, "loop");
692 free(cn);
693 }
10ee5932 694
fbc2fe82
KZ
695 if (rc == 0)
696 errno = ENODEV;
697 return rc;
10ee5932
KZ
698}
699
700/*
701 * @lc: context
702 *
703 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
704 */
705struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc)
706{
707 int fd;
708
85794fb0
KZ
709 if (!lc || lc->info_failed) {
710 errno = EINVAL;
10ee5932 711 return NULL;
85794fb0
KZ
712 }
713 errno = 0;
10ee5932 714 if (lc->has_info)
d5fd456c 715 return &lc->config.info;
10ee5932
KZ
716
717 fd = loopcxt_get_fd(lc);
718 if (fd < 0)
719 return NULL;
720
d5fd456c 721 if (ioctl(fd, LOOP_GET_STATUS64, &lc->config.info) == 0) {
10ee5932 722 lc->has_info = 1;
6c224de1 723 lc->info_failed = 0;
0bf03740 724 DBG(CXT, ul_debugobj(lc, "reading loop_info64 OK"));
d5fd456c 725 return &lc->config.info;
10ee5932
KZ
726 }
727
85794fb0 728 lc->info_failed = 1;
0bf03740 729 DBG(CXT, ul_debugobj(lc, "reading loop_info64 FAILED"));
85794fb0 730
10ee5932
KZ
731 return NULL;
732}
733
734/*
735 * @lc: context
736 *
311e33af 737 * Returns (allocated) string with path to the file associated
10ee5932
KZ
738 * with the current loop device.
739 */
740char *loopcxt_get_backing_file(struct loopdev_cxt *lc)
741{
7604f85f 742 struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
10ee5932
KZ
743 char *res = NULL;
744
745 if (sysfs)
fd7f0718 746 /*
11026083 747 * This is always preferred, the loop_info64
fd7f0718
KZ
748 * has too small buffer for the filename.
749 */
7604f85f 750 ul_path_read_string(sysfs, &res, "loop/backing_file");
10ee5932
KZ
751
752 if (!res && loopcxt_ioctl_enabled(lc)) {
753 struct loop_info64 *lo = loopcxt_get_info(lc);
754
755 if (lo) {
756 lo->lo_file_name[LO_NAME_SIZE - 2] = '*';
757 lo->lo_file_name[LO_NAME_SIZE - 1] = '\0';
758 res = strdup((char *) lo->lo_file_name);
759 }
760 }
aee31ddc 761
0bf03740 762 DBG(CXT, ul_debugobj(lc, "get_backing_file [%s]", res));
10ee5932
KZ
763 return res;
764}
765
db8f7815
KZ
766/*
767 * @lc: context
768 *
769 * Returns (allocated) string with loop reference. The same as backing file by
770 * default.
771 */
772char *loopcxt_get_refname(struct loopdev_cxt *lc)
773{
774 char *res = NULL;
775 struct loop_info64 *lo = loopcxt_get_info(lc);
776
777 if (lo) {
778 lo->lo_file_name[LO_NAME_SIZE - 1] = '\0';
779 res = strdup((char *) lo->lo_file_name);
780 }
781
782 DBG(CXT, ul_debugobj(lc, "get_refname [%s]", res));
783 return res;
784}
785
10ee5932
KZ
786/*
787 * @lc: context
788 * @offset: returns offset number for the given device
789 *
790 * Returns: <0 on error, 0 on success
791 */
792int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset)
793{
7604f85f 794 struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
10ee5932
KZ
795 int rc = -EINVAL;
796
797 if (sysfs)
fb4b6b11
JK
798 if (ul_path_read_u64(sysfs, offset, "loop/offset") == 0)
799 rc = 0;
10ee5932
KZ
800
801 if (rc && loopcxt_ioctl_enabled(lc)) {
802 struct loop_info64 *lo = loopcxt_get_info(lc);
803 if (lo) {
804 if (offset)
805 *offset = lo->lo_offset;
6c224de1 806 rc = 0;
85794fb0
KZ
807 } else
808 rc = -errno;
10ee5932
KZ
809 }
810
0bf03740 811 DBG(CXT, ul_debugobj(lc, "get_offset [rc=%d]", rc));
10ee5932
KZ
812 return rc;
813}
814
a1a41597
SB
815/*
816 * @lc: context
817 * @blocksize: returns logical blocksize for the given device
818 *
819 * Returns: <0 on error, 0 on success
820 */
821int loopcxt_get_blocksize(struct loopdev_cxt *lc, uint64_t *blocksize)
822{
7604f85f 823 struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
a1a41597
SB
824 int rc = -EINVAL;
825
826 if (sysfs)
fb4b6b11
JK
827 if (ul_path_read_u64(sysfs, blocksize, "queue/logical_block_size") == 0)
828 rc = 0;
a1a41597
SB
829
830 /* Fallback based on BLKSSZGET ioctl */
831 if (rc) {
832 int fd = loopcxt_get_fd(lc);
833 int sz = 0;
834
835 if (fd < 0)
836 return -EINVAL;
837 rc = blkdev_get_sector_size(fd, &sz);
838 if (rc)
839 return rc;
840
841 *blocksize = sz;
842 }
843
844 DBG(CXT, ul_debugobj(lc, "get_blocksize [rc=%d]", rc));
845 return rc;
846}
847
10ee5932
KZ
848/*
849 * @lc: context
850 * @sizelimit: returns size limit for the given device
851 *
852 * Returns: <0 on error, 0 on success
853 */
854int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size)
855{
7604f85f 856 struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
10ee5932
KZ
857 int rc = -EINVAL;
858
859 if (sysfs)
fb4b6b11
JK
860 if (ul_path_read_u64(sysfs, size, "loop/sizelimit") == 0)
861 rc = 0;
10ee5932
KZ
862
863 if (rc && loopcxt_ioctl_enabled(lc)) {
864 struct loop_info64 *lo = loopcxt_get_info(lc);
865 if (lo) {
866 if (size)
867 *size = lo->lo_sizelimit;
6c224de1 868 rc = 0;
85794fb0
KZ
869 } else
870 rc = -errno;
10ee5932
KZ
871 }
872
0bf03740 873 DBG(CXT, ul_debugobj(lc, "get_sizelimit [rc=%d]", rc));
6c224de1
KZ
874 return rc;
875}
876
877/*
878 * @lc: context
879 * @devno: returns encryption type
880 *
881 * Cryptoloop is DEPRECATED!
882 *
883 * Returns: <0 on error, 0 on success
884 */
885int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type)
886{
887 struct loop_info64 *lo = loopcxt_get_info(lc);
85794fb0 888 int rc;
6c224de1 889
85794fb0 890 /* not provided by sysfs */
6c224de1
KZ
891 if (lo) {
892 if (type)
893 *type = lo->lo_encrypt_type;
894 rc = 0;
85794fb0
KZ
895 } else
896 rc = -errno;
897
0bf03740 898 DBG(CXT, ul_debugobj(lc, "get_encrypt_type [rc=%d]", rc));
6c224de1
KZ
899 return rc;
900}
901
902/*
903 * @lc: context
904 * @devno: returns crypt name
905 *
906 * Cryptoloop is DEPRECATED!
907 *
908 * Returns: <0 on error, 0 on success
909 */
910const char *loopcxt_get_crypt_name(struct loopdev_cxt *lc)
911{
912 struct loop_info64 *lo = loopcxt_get_info(lc);
913
914 if (lo)
915 return (char *) lo->lo_crypt_name;
916
0bf03740 917 DBG(CXT, ul_debugobj(lc, "get_crypt_name failed"));
6c224de1
KZ
918 return NULL;
919}
920
921/*
922 * @lc: context
923 * @devno: returns backing file devno
924 *
925 * Returns: <0 on error, 0 on success
926 */
927int loopcxt_get_backing_devno(struct loopdev_cxt *lc, dev_t *devno)
928{
929 struct loop_info64 *lo = loopcxt_get_info(lc);
85794fb0 930 int rc;
6c224de1
KZ
931
932 if (lo) {
933 if (devno)
934 *devno = lo->lo_device;
935 rc = 0;
85794fb0
KZ
936 } else
937 rc = -errno;
938
0bf03740 939 DBG(CXT, ul_debugobj(lc, "get_backing_devno [rc=%d]", rc));
6c224de1
KZ
940 return rc;
941}
942
943/*
944 * @lc: context
945 * @ino: returns backing file inode
946 *
947 * Returns: <0 on error, 0 on success
948 */
949int loopcxt_get_backing_inode(struct loopdev_cxt *lc, ino_t *ino)
950{
951 struct loop_info64 *lo = loopcxt_get_info(lc);
85794fb0 952 int rc;
6c224de1
KZ
953
954 if (lo) {
955 if (ino)
956 *ino = lo->lo_inode;
957 rc = 0;
85794fb0
KZ
958 } else
959 rc = -errno;
960
0bf03740 961 DBG(CXT, ul_debugobj(lc, "get_backing_inode [rc=%d]", rc));
10ee5932
KZ
962 return rc;
963}
964
59d749c3
KZ
965/*
966 * Check if the kernel supports partitioned loop devices.
967 *
968 * Notes:
969 * - kernels < 3.2 support partitioned loop devices and PT scanning
9e930041 970 * only if max_part= module parameter is non-zero
59d749c3
KZ
971 *
972 * - kernels >= 3.2 always support partitioned loop devices
973 *
974 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
975 *
976 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
977 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
978 * by default.
979 *
980 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
981 */
982int loopmod_supports_partscan(void)
983{
984 int rc, ret = 0;
985 FILE *f;
986
987 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
988 return 1;
989
b1fa3e22 990 f = fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR);
59d749c3
KZ
991 if (!f)
992 return 0;
993 rc = fscanf(f, "%d", &ret);
994 fclose(f);
8b04761d 995 return rc == 1 ? ret : 0;
59d749c3
KZ
996}
997
10ee5932
KZ
998/*
999 * @lc: context
1000 *
59d749c3 1001 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
9e930041 1002 * scanning is enabled for all loop devices.
59d749c3
KZ
1003 */
1004int loopcxt_is_partscan(struct loopdev_cxt *lc)
1005{
7604f85f 1006 struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
59d749c3
KZ
1007
1008 if (sysfs) {
1009 /* kernel >= 3.2 */
1010 int fl;
7604f85f 1011 if (ul_path_read_s32(sysfs, &fl, "loop/partscan") == 0)
59d749c3
KZ
1012 return fl;
1013 }
1014
1015 /* old kernels (including kernels without loopN/loop/<flags> directory */
1016 return loopmod_supports_partscan();
1017}
1018
1019/*
1020 * @lc: context
1021 *
1022 * Returns: 1 if the autoclear flags is set.
10ee5932
KZ
1023 */
1024int loopcxt_is_autoclear(struct loopdev_cxt *lc)
1025{
7604f85f 1026 struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
10ee5932
KZ
1027
1028 if (sysfs) {
1029 int fl;
7604f85f 1030 if (ul_path_read_s32(sysfs, &fl, "loop/autoclear") == 0)
10ee5932
KZ
1031 return fl;
1032 }
1033
1034 if (loopcxt_ioctl_enabled(lc)) {
1035 struct loop_info64 *lo = loopcxt_get_info(lc);
1036 if (lo)
1037 return lo->lo_flags & LO_FLAGS_AUTOCLEAR;
1038 }
1039 return 0;
1040}
1041
fd7f0718
KZ
1042/*
1043 * @lc: context
1044 *
59d749c3 1045 * Returns: 1 if the readonly flags is set.
fd7f0718
KZ
1046 */
1047int loopcxt_is_readonly(struct loopdev_cxt *lc)
1048{
7604f85f 1049 struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
fd7f0718
KZ
1050
1051 if (sysfs) {
1052 int fl;
7604f85f 1053 if (ul_path_read_s32(sysfs, &fl, "ro") == 0)
fd7f0718
KZ
1054 return fl;
1055 }
1056
1057 if (loopcxt_ioctl_enabled(lc)) {
1058 struct loop_info64 *lo = loopcxt_get_info(lc);
1059 if (lo)
1060 return lo->lo_flags & LO_FLAGS_READ_ONLY;
1061 }
1062 return 0;
1063}
1064
faeef4d2
ML
1065/*
1066 * @lc: context
1067 *
1068 * Returns: 1 if the dio flags is set.
1069 */
1070int loopcxt_is_dio(struct loopdev_cxt *lc)
1071{
7604f85f 1072 struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
faeef4d2
ML
1073
1074 if (sysfs) {
1075 int fl;
7604f85f 1076 if (ul_path_read_s32(sysfs, &fl, "loop/dio") == 0)
faeef4d2
ML
1077 return fl;
1078 }
1079 if (loopcxt_ioctl_enabled(lc)) {
1080 struct loop_info64 *lo = loopcxt_get_info(lc);
1081 if (lo)
1082 return lo->lo_flags & LO_FLAGS_DIRECT_IO;
1083 }
1084 return 0;
1085}
1086
6c224de1
KZ
1087/*
1088 * @lc: context
1089 * @st: backing file stat or NULL
1090 * @backing_file: filename
c444a71b
KZ
1091 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
1092 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
1093 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
6c224de1
KZ
1094 *
1095 * Returns 1 if the current @lc loopdev is associated with the given backing
1096 * file. Note that the preferred way is to use devno and inode number rather
1097 * than filename. The @backing_file filename is poor solution usable in case
1098 * that you don't have rights to call stat().
1099 *
74a4705a
SB
1100 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
1101 *
6c224de1
KZ
1102 * Don't forget that old kernels provide very restricted (in size) backing
1103 * filename by LOOP_GET_STAT64 ioctl only.
1104 */
1105int loopcxt_is_used(struct loopdev_cxt *lc,
1106 struct stat *st,
1107 const char *backing_file,
1108 uint64_t offset,
74a4705a 1109 uint64_t sizelimit,
6c224de1
KZ
1110 int flags)
1111{
3011b381
KZ
1112 ino_t ino = 0;
1113 dev_t dev = 0;
6c224de1
KZ
1114
1115 if (!lc)
1116 return 0;
1117
0bf03740 1118 DBG(CXT, ul_debugobj(lc, "checking %s vs. %s",
6c224de1
KZ
1119 loopcxt_get_device(lc),
1120 backing_file));
1121
1122 if (st && loopcxt_get_backing_inode(lc, &ino) == 0 &&
1123 loopcxt_get_backing_devno(lc, &dev) == 0) {
1124
1125 if (ino == st->st_ino && dev == st->st_dev)
1126 goto found;
1127
1128 /* don't use filename if we have devno and inode */
1129 return 0;
1130 }
1131
1132 /* poor man's solution */
1133 if (backing_file) {
1134 char *name = loopcxt_get_backing_file(lc);
1135 int rc = name && strcmp(name, backing_file) == 0;
1136
1137 free(name);
1138 if (rc)
1139 goto found;
1140 }
1141
1142 return 0;
1143found:
1144 if (flags & LOOPDEV_FL_OFFSET) {
3011b381 1145 uint64_t off = 0;
6c224de1 1146
74a4705a
SB
1147 int rc = loopcxt_get_offset(lc, &off) == 0 && off == offset;
1148
1149 if (rc && flags & LOOPDEV_FL_SIZELIMIT) {
3011b381 1150 uint64_t sz = 0;
74a4705a
SB
1151
1152 return loopcxt_get_sizelimit(lc, &sz) == 0 && sz == sizelimit;
042f62df
RP
1153 }
1154 return rc;
6c224de1
KZ
1155 }
1156 return 1;
1157}
1158
fd7f0718
KZ
1159/*
1160 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1161 */
10ee5932
KZ
1162int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset)
1163{
1164 if (!lc)
1165 return -EINVAL;
d5fd456c 1166 lc->config.info.lo_offset = offset;
aee31ddc 1167
0bf03740 1168 DBG(CXT, ul_debugobj(lc, "set offset=%jd", offset));
10ee5932
KZ
1169 return 0;
1170}
1171
fd7f0718
KZ
1172/*
1173 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1174 */
10ee5932
KZ
1175int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit)
1176{
1177 if (!lc)
1178 return -EINVAL;
d5fd456c 1179 lc->config.info.lo_sizelimit = sizelimit;
aee31ddc 1180
0bf03740 1181 DBG(CXT, ul_debugobj(lc, "set sizelimit=%jd", sizelimit));
10ee5932
KZ
1182 return 0;
1183}
1184
75d239ff
KZ
1185/*
1186 * The blocksize will be used by loopcxt_set_device(). For already exiting
1187 * devices use loopcxt_ioctl_blocksize().
1188 *
1189 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1190 */
1191int loopcxt_set_blocksize(struct loopdev_cxt *lc, uint64_t blocksize)
1192{
1193 if (!lc)
1194 return -EINVAL;
1195 lc->blocksize = blocksize;
1196
1197 DBG(CXT, ul_debugobj(lc, "set blocksize=%jd", blocksize));
1198 return 0;
1199}
1200
10ee5932
KZ
1201/*
1202 * @lc: context
1203 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1204 *
fd7f0718
KZ
1205 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1206 *
10ee5932
KZ
1207 * Returns: 0 on success, <0 on error.
1208 */
1209int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags)
1210{
1211 if (!lc)
1212 return -EINVAL;
d5fd456c 1213 lc->config.info.lo_flags = flags;
aee31ddc 1214
0bf03740 1215 DBG(CXT, ul_debugobj(lc, "set flags=%u", (unsigned) flags));
10ee5932
KZ
1216 return 0;
1217}
1218
db8f7815
KZ
1219/*
1220 * @lc: context
1221 * @refname: reference name (used to overwrite lo_file_name where is backing
1222 * file by default)
1223 *
1224 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1225 *
1226 * Returns: 0 on success, <0 on error.
1227 */
1228int loopcxt_set_refname(struct loopdev_cxt *lc, const char *refname)
1229{
1230 if (!lc)
1231 return -EINVAL;
1232
1233 memset(lc->config.info.lo_file_name, 0, sizeof(lc->config.info.lo_file_name));
1234 if (refname)
1235 xstrncpy((char *)lc->config.info.lo_file_name, refname, LO_NAME_SIZE);
1236
1237 DBG(CXT, ul_debugobj(lc, "set refname=%s", (char *)lc->config.info.lo_file_name));
1238 return 0;
1239}
1240
10ee5932
KZ
1241/*
1242 * @lc: context
1243 * @filename: backing file path (the path will be canonicalized)
1244 *
fd7f0718
KZ
1245 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1246 *
10ee5932
KZ
1247 * Returns: 0 on success, <0 on error.
1248 */
1249int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename)
1250{
1251 if (!lc)
1252 return -EINVAL;
1253
1254 lc->filename = canonicalize_path(filename);
1255 if (!lc->filename)
1256 return -errno;
1257
db8f7815
KZ
1258 if (!lc->config.info.lo_file_name[0])
1259 loopcxt_set_refname(lc, lc->filename);
10ee5932 1260
db8f7815 1261 DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->filename));
10ee5932
KZ
1262 return 0;
1263}
1264
293714c0
JM
1265/*
1266 * In kernels prior to v3.9, if the offset or sizelimit options
1267 * are used, the block device's size won't be synced automatically.
1268 * blockdev --getsize64 and filesystems will use the backing
1269 * file size until the block device has been re-opened or the
1270 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1271 *
1272 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1273 * the open file descriptor to the mount system call, we need to use
1274 * the ioctl. Calling losetup directly doesn't have this problem since
1275 * it closes the device when it exits and whatever consumes the device
1276 * next will re-open it, causing the resync.
1277 */
1278static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
1279{
1280 uint64_t size, expected_size;
1281 int dev_fd;
1282 struct stat st;
1283
d5fd456c 1284 if (!lc->config.info.lo_offset && !lc->config.info.lo_sizelimit)
293714c0
JM
1285 return 0;
1286
01307ecf 1287 if (fstat(file_fd, &st)) {
0bf03740 1288 DBG(CXT, ul_debugobj(lc, "failed to fstat backing file"));
293714c0 1289 return -errno;
01307ecf 1290 }
e3b6cb87
KZ
1291 if (S_ISBLK(st.st_mode)) {
1292 if (blkdev_get_size(file_fd,
01307ecf 1293 (unsigned long long *) &expected_size)) {
0bf03740 1294 DBG(CXT, ul_debugobj(lc, "failed to determine device size"));
e3b6cb87 1295 return -errno;
01307ecf 1296 }
e3b6cb87
KZ
1297 } else
1298 expected_size = st.st_size;
1299
d5fd456c 1300 if (expected_size == 0 || expected_size <= lc->config.info.lo_offset) {
0bf03740 1301 DBG(CXT, ul_debugobj(lc, "failed to determine expected size"));
e3b6cb87
KZ
1302 return 0; /* ignore this error */
1303 }
293714c0 1304
d5fd456c
SK
1305 if (lc->config.info.lo_offset > 0)
1306 expected_size -= lc->config.info.lo_offset;
293714c0 1307
d5fd456c
SK
1308 if (lc->config.info.lo_sizelimit > 0 && lc->config.info.lo_sizelimit < expected_size)
1309 expected_size = lc->config.info.lo_sizelimit;
293714c0
JM
1310
1311 dev_fd = loopcxt_get_fd(lc);
01307ecf 1312 if (dev_fd < 0) {
0bf03740 1313 DBG(CXT, ul_debugobj(lc, "failed to get loop FD"));
293714c0 1314 return -errno;
01307ecf 1315 }
293714c0 1316
01307ecf 1317 if (blkdev_get_size(dev_fd, (unsigned long long *) &size)) {
0bf03740 1318 DBG(CXT, ul_debugobj(lc, "failed to determine loopdev size"));
293714c0 1319 return -errno;
01307ecf 1320 }
293714c0 1321
a7d5202b
KZ
1322 /* It's block device, so, align to 512-byte sectors */
1323 if (expected_size % 512) {
0bf03740 1324 DBG(CXT, ul_debugobj(lc, "expected size misaligned to 512-byte sectors"));
a7d5202b
KZ
1325 expected_size = (expected_size >> 9) << 9;
1326 }
1327
293714c0 1328 if (expected_size != size) {
0bf03740 1329 DBG(CXT, ul_debugobj(lc, "warning: loopdev and expected "
9e930041 1330 "size mismatch (%ju/%ju)",
e3b6cb87
KZ
1331 size, expected_size));
1332
9fcc8936 1333 if (loopcxt_ioctl_capacity(lc)) {
293714c0
JM
1334 /* ioctl not available */
1335 if (errno == ENOTTY || errno == EINVAL)
1336 errno = ERANGE;
1337 return -errno;
1338 }
1339
1340 if (blkdev_get_size(dev_fd, (unsigned long long *) &size))
1341 return -errno;
1342
01307ecf
KZ
1343 if (expected_size != size) {
1344 errno = ERANGE;
0bf03740 1345 DBG(CXT, ul_debugobj(lc, "failed to set loopdev size, "
01307ecf
KZ
1346 "size: %ju, expected: %ju",
1347 size, expected_size));
1348 return -errno;
1349 }
293714c0
JM
1350 }
1351
1352 return 0;
1353}
1354
9d5424c2 1355
10ee5932 1356/*
bfd4e1f7 1357 * @lc: context
10ee5932
KZ
1358 *
1359 * Associate the current device (see loopcxt_{set,get}_device()) with
1360 * a file (see loopcxt_set_backing_file()).
1361 *
fd7f0718
KZ
1362 * The device is initialized read-write by default. If you want read-only
1363 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1364 * flags are ignored and modified according to LO_FLAGS_*.
1365 *
1366 * If the device is already open by loopcxt_get_fd() then this setup device
1367 * function will re-open the device to fix read/write mode.
1368 *
1369 * The device is also initialized read-only if the backing file is not
1370 * possible to open read-write (e.g. read-only FS).
10ee5932
KZ
1371 *
1372 * Returns: <0 on error, 0 on success.
1373 */
1374int loopcxt_setup_device(struct loopdev_cxt *lc)
1375{
ced1142d
KZ
1376 int file_fd, dev_fd;
1377 mode_t flags = O_CLOEXEC, mode = O_RDWR;
1328e0f7 1378 int rc = -1, cnt = 0;
2cde9865 1379 int errsv = 0;
d5fd456c 1380 int fallback = 0;
10ee5932
KZ
1381
1382 if (!lc || !*lc->device || !lc->filename)
1383 return -EINVAL;
1384
0bf03740 1385 DBG(SETUP, ul_debugobj(lc, "device setup requested"));
aee31ddc 1386
10ee5932
KZ
1387 /*
1388 * Open backing file and device
1389 */
d5fd456c 1390 if (lc->config.info.lo_flags & LO_FLAGS_READ_ONLY)
fd7f0718 1391 mode = O_RDONLY;
10ee5932 1392
d53346ed
AXH
1393 if (lc->config.info.lo_flags & LO_FLAGS_DIRECT_IO)
1394 flags |= O_DIRECT;
1395
1396 if ((file_fd = open(lc->filename, mode | flags)) < 0) {
10ee5932 1397 if (mode != O_RDONLY && (errno == EROFS || errno == EACCES))
d53346ed 1398 file_fd = open(lc->filename, (mode = O_RDONLY) | flags);
10ee5932 1399
aee31ddc 1400 if (file_fd < 0) {
0bf03740 1401 DBG(SETUP, ul_debugobj(lc, "open backing file failed: %m"));
10ee5932 1402 return -errno;
aee31ddc 1403 }
10ee5932 1404 }
0bf03740 1405 DBG(SETUP, ul_debugobj(lc, "backing file open: OK"));
10ee5932 1406
ced1142d 1407 if (mode == O_RDONLY)
d5fd456c 1408 lc->config.info.lo_flags |= LO_FLAGS_READ_ONLY; /* kernel loopdev mode */
ced1142d 1409 else
d5fd456c 1410 lc->config.info.lo_flags &= ~LO_FLAGS_READ_ONLY;
10ee5932 1411
663bf040
KZ
1412 do {
1413 errno = 0;
ced1142d
KZ
1414
1415 /* For the ioctls, it's enough to use O_RDONLY, but udevd
1416 * monitor devices by inotify, and udevd needs IN_CLOSE_WRITE
1417 * event to trigger probing of the new device.
1418 *
1419 * The mode used for the device does not have to match the mode
1420 * used for the backing file.
1421 */
1422 dev_fd = __loopcxt_get_fd(lc, O_RDWR);
663bf040
KZ
1423 if (dev_fd >= 0 || lc->control_ok == 0)
1424 break;
1425 if (errno != EACCES && errno != ENOENT)
1426 break;
1427 /* We have permissions to open /dev/loop-control, but open
1428 * /dev/loopN failed with EACCES, it's probably because udevd
1429 * does not applied chown yet. Let's wait a moment. */
1430 xusleep(25000);
1431 } while (cnt++ < 16);
1432
10ee5932
KZ
1433 if (dev_fd < 0) {
1434 rc = -errno;
1435 goto err;
1436 }
1437
0bf03740 1438 DBG(SETUP, ul_debugobj(lc, "device open: OK"));
d356c5d2 1439
a61955ba
KZ
1440 /*
1441 * Atomic way to configure all by one ioctl call
1442 * -- since Linux v5.8-rc1, commit 3448914e8cc550ba792d4ccc74471d1ca4293aae
1443 */
d5fd456c 1444 lc->config.fd = file_fd;
854abba0
HE
1445 if (lc->blocksize > 0)
1446 lc->config.block_size = lc->blocksize;
9d5424c2
KZ
1447
1448 rc = repeat_on_eagain( ioctl(dev_fd, LOOP_CONFIGURE, &lc->config) );
1449 if (rc != 0) {
2cde9865 1450 errsv = errno;
f6385a6a 1451 if (errno != EINVAL && errno != ENOTTY && errno != ENOSYS) {
d5fd456c
SK
1452 DBG(SETUP, ul_debugobj(lc, "LOOP_CONFIGURE failed: %m"));
1453 goto err;
1454 }
1455 fallback = 1;
a61955ba 1456 } else {
d5fd456c 1457 DBG(SETUP, ul_debugobj(lc, "LOOP_CONFIGURE: OK"));
75d239ff
KZ
1458 }
1459
a61955ba
KZ
1460 /*
1461 * Old deprecated way; first assign backing file FD and then in the
1462 * second step set loop device properties.
1463 */
d5fd456c 1464 if (fallback) {
d5fd456c
SK
1465 if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
1466 rc = -errno;
1467 errsv = errno;
1468 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD failed: %m"));
1469 goto err;
1470 }
d356c5d2 1471
d5fd456c
SK
1472 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD: OK"));
1473
1474 if (lc->blocksize > 0
1475 && (rc = loopcxt_ioctl_blocksize(lc, lc->blocksize)) < 0) {
1476 errsv = -rc;
1477 goto err;
1478 }
1479
1328e0f7
KZ
1480 if ((rc = loopcxt_ioctl_status(lc)) < 0) {
1481 errsv = -rc;
d5fd456c
SK
1482 goto err;
1483 }
d5fd456c 1484 }
d356c5d2 1485
293714c0
JM
1486 if ((rc = loopcxt_check_size(lc, file_fd)))
1487 goto err;
1488
1489 close(file_fd);
293714c0 1490
d5fd456c 1491 memset(&lc->config, 0, sizeof(lc->config));
10ee5932 1492 lc->has_info = 0;
6c224de1 1493 lc->info_failed = 0;
10ee5932 1494
0bf03740 1495 DBG(SETUP, ul_debugobj(lc, "success [rc=0]"));
10ee5932
KZ
1496 return 0;
1497err:
1498 if (file_fd >= 0)
1499 close(file_fd);
f7e21185 1500 if (dev_fd >= 0 && rc != -EBUSY)
10ee5932 1501 ioctl(dev_fd, LOOP_CLR_FD, 0);
2cde9865
KZ
1502 if (errsv)
1503 errno = errsv;
10ee5932 1504
0bf03740 1505 DBG(SETUP, ul_debugobj(lc, "failed [rc=%d]", rc));
10ee5932
KZ
1506 return rc;
1507}
1508
9d5424c2 1509
bfd4e1f7
SB
1510/*
1511 * @lc: context
1512 *
1513 * Update status of the current device (see loopcxt_{set,get}_device()).
1514 *
1515 * Note that once initialized, kernel accepts only selected changes:
1516 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1517 * For more see linux/drivers/block/loop.c:loop_set_status()
1518 *
1519 * Returns: <0 on error, 0 on success.
1520 */
9fcc8936 1521int loopcxt_ioctl_status(struct loopdev_cxt *lc)
bfd4e1f7 1522{
9d5424c2 1523 int dev_fd, rc;
bfd4e1f7
SB
1524
1525 errno = 0;
1526 dev_fd = loopcxt_get_fd(lc);
1527
9d5424c2
KZ
1528 if (dev_fd < 0)
1529 return -errno;
bfd4e1f7 1530
9d5424c2 1531 DBG(SETUP, ul_debugobj(lc, "calling LOOP_SET_STATUS64"));
0ae7bb11 1532
9d5424c2
KZ
1533 rc = repeat_on_eagain( ioctl(dev_fd, LOOP_SET_STATUS64, &lc->config.info) );
1534 if (rc != 0) {
bfd4e1f7
SB
1535 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m"));
1536 return rc;
1537 }
1538
1539 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64: OK"));
1540 return 0;
1541}
1542
9fcc8936 1543int loopcxt_ioctl_capacity(struct loopdev_cxt *lc)
293714c0 1544{
9d5424c2 1545 int rc, fd = loopcxt_get_fd(lc);
293714c0
JM
1546
1547 if (fd < 0)
1548 return -EINVAL;
1549
9d5424c2
KZ
1550 DBG(SETUP, ul_debugobj(lc, "calling LOOP_SET_CAPACITY"));
1551
293714c0 1552 /* Kernels prior to v2.6.30 don't support this ioctl */
9d5424c2
KZ
1553 rc = repeat_on_eagain( ioctl(fd, LOOP_SET_CAPACITY, 0) );
1554 if (rc != 0) {
0bf03740 1555 DBG(CXT, ul_debugobj(lc, "LOOP_SET_CAPACITY failed: %m"));
293714c0
JM
1556 return rc;
1557 }
1558
0bf03740 1559 DBG(CXT, ul_debugobj(lc, "capacity set"));
293714c0
JM
1560 return 0;
1561}
1562
9fcc8936 1563int loopcxt_ioctl_dio(struct loopdev_cxt *lc, unsigned long use_dio)
64c3bb3c 1564{
9d5424c2 1565 int rc, fd = loopcxt_get_fd(lc);
64c3bb3c
ML
1566
1567 if (fd < 0)
1568 return -EINVAL;
1569
9d5424c2
KZ
1570 DBG(SETUP, ul_debugobj(lc, "calling LOOP_SET_DIRECT_IO"));
1571
64c3bb3c 1572 /* Kernels prior to v4.4 don't support this ioctl */
9d5424c2
KZ
1573 rc = repeat_on_eagain( ioctl(fd, LOOP_SET_DIRECT_IO, use_dio) );
1574 if (rc != 0) {
64c3bb3c
ML
1575 DBG(CXT, ul_debugobj(lc, "LOOP_SET_DIRECT_IO failed: %m"));
1576 return rc;
1577 }
1578
1579 DBG(CXT, ul_debugobj(lc, "direct io set"));
1580 return 0;
1581}
1582
c4e60bc0
KZ
1583/*
1584 * Kernel uses "unsigned long" as ioctl arg, but we use u64 for all sizes to
1585 * keep loopdev internal API simple.
1586 */
9fcc8936 1587int loopcxt_ioctl_blocksize(struct loopdev_cxt *lc, uint64_t blocksize)
a1a41597 1588{
9d5424c2 1589 int rc, fd = loopcxt_get_fd(lc);
a1a41597
SB
1590
1591 if (fd < 0)
1592 return -EINVAL;
1593
9d5424c2
KZ
1594 DBG(SETUP, ul_debugobj(lc, "calling LOOP_SET_BLOCK_SIZE"));
1595
1596 rc = repeat_on_eagain(
1597 ioctl(fd, LOOP_SET_BLOCK_SIZE, (unsigned long) blocksize) );
1598 if (rc != 0) {
1599 DBG(CXT, ul_debugobj(lc, "LOOP_SET_BLOCK_SIZE failed: %m"));
1600 return rc;
1601 }
a1a41597
SB
1602
1603 DBG(CXT, ul_debugobj(lc, "logical block size set"));
1604 return 0;
1605}
1606
10ee5932
KZ
1607int loopcxt_delete_device(struct loopdev_cxt *lc)
1608{
9d5424c2 1609 int rc, fd = loopcxt_get_fd(lc);
10ee5932
KZ
1610
1611 if (fd < 0)
1612 return -EINVAL;
1613
9d5424c2
KZ
1614 DBG(SETUP, ul_debugobj(lc, "calling LOOP_SET_CLR_FD"));
1615
1616 rc = repeat_on_eagain( ioctl(fd, LOOP_CLR_FD, 0) );
1617 if (rc != 0) {
0bf03740 1618 DBG(CXT, ul_debugobj(lc, "LOOP_CLR_FD failed: %m"));
9d5424c2 1619 return rc;
aee31ddc
KZ
1620 }
1621
0bf03740 1622 DBG(CXT, ul_debugobj(lc, "device removed"));
10ee5932
KZ
1623 return 0;
1624}
1625
3cb2413b
KZ
1626int loopcxt_add_device(struct loopdev_cxt *lc)
1627{
1628 int rc = -EINVAL;
1629 int ctl, nr = -1;
1630 const char *p, *dev = loopcxt_get_device(lc);
1631
1632 if (!dev)
1633 goto done;
1634
1635 if (!(lc->flags & LOOPDEV_FL_CONTROL)) {
1636 rc = -ENOSYS;
1637 goto done;
1638 }
1639
1640 p = strrchr(dev, '/');
1641 if (!p || (sscanf(p, "/loop%d", &nr) != 1 && sscanf(p, "/%d", &nr) != 1)
1642 || nr < 0)
1643 goto done;
1644
1645 ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
1646 if (ctl >= 0) {
0bf03740 1647 DBG(CXT, ul_debugobj(lc, "add_device %d", nr));
3cb2413b
KZ
1648 rc = ioctl(ctl, LOOP_CTL_ADD, nr);
1649 close(ctl);
1650 }
663bf040 1651 lc->control_ok = rc >= 0 ? 1 : 0;
3cb2413b 1652done:
0bf03740 1653 DBG(CXT, ul_debugobj(lc, "add_device done [rc=%d]", rc));
3cb2413b
KZ
1654 return rc;
1655}
1656
59d749c3
KZ
1657/*
1658 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1659 * kernels we have to check all loop devices to found unused one.
1660 *
1661 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
3c540264
TW
1662 *
1663 * Returns: 0 = success, < 0 error
59d749c3 1664 */
10ee5932
KZ
1665int loopcxt_find_unused(struct loopdev_cxt *lc)
1666{
0b14bf7a 1667 int rc = -1;
10ee5932 1668
0bf03740 1669 DBG(CXT, ul_debugobj(lc, "find_unused requested"));
aee31ddc 1670
0b14bf7a 1671 if (lc->flags & LOOPDEV_FL_CONTROL) {
9a94b634
KZ
1672 int ctl;
1673
1674 DBG(CXT, ul_debugobj(lc, "using loop-control"));
10ee5932 1675
9a94b634 1676 ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
0b14bf7a
KZ
1677 if (ctl >= 0)
1678 rc = ioctl(ctl, LOOP_CTL_GET_FREE);
3c540264
TW
1679 else
1680 rc = -errno;
0b14bf7a
KZ
1681 if (rc >= 0) {
1682 char name[16];
1683 snprintf(name, sizeof(name), "loop%d", rc);
1684
1685 rc = loopiter_set_device(lc, name);
1686 }
663bf040 1687 lc->control_ok = ctl >= 0 && rc == 0 ? 1 : 0;
0b14bf7a
KZ
1688 if (ctl >= 0)
1689 close(ctl);
0bf03740 1690 DBG(CXT, ul_debugobj(lc, "find_unused by loop-control [rc=%d]", rc));
0b14bf7a
KZ
1691 }
1692
1693 if (rc < 0) {
9a94b634 1694 DBG(CXT, ul_debugobj(lc, "using loop scan"));
0b14bf7a
KZ
1695 rc = loopcxt_init_iterator(lc, LOOPITER_FL_FREE);
1696 if (rc)
1697 return rc;
10ee5932 1698
0b14bf7a
KZ
1699 rc = loopcxt_next(lc);
1700 loopcxt_deinit_iterator(lc);
0bf03740 1701 DBG(CXT, ul_debugobj(lc, "find_unused by scan [rc=%d]", rc));
3c540264
TW
1702 if (rc)
1703 return -ENOENT;
0b14bf7a 1704 }
10ee5932
KZ
1705 return rc;
1706}
1707
1708
1709
1710/*
1711 * Return: TRUE/FALSE
1712 */
1713int loopdev_is_autoclear(const char *device)
1714{
1715 struct loopdev_cxt lc;
1716 int rc;
1717
1718 if (!device)
1719 return 0;
1720
defa0710
KZ
1721 rc = loopcxt_init(&lc, 0);
1722 if (!rc)
1723 rc = loopcxt_set_device(&lc, device);
1724 if (!rc)
1725 rc = loopcxt_is_autoclear(&lc);
10ee5932 1726
defa0710 1727 loopcxt_deinit(&lc);
10ee5932
KZ
1728 return rc;
1729}
1730
1731char *loopdev_get_backing_file(const char *device)
1732{
1733 struct loopdev_cxt lc;
defa0710 1734 char *res = NULL;
10ee5932
KZ
1735
1736 if (!device)
1737 return NULL;
defa0710
KZ
1738 if (loopcxt_init(&lc, 0))
1739 return NULL;
1740 if (loopcxt_set_device(&lc, device) == 0)
1741 res = loopcxt_get_backing_file(&lc);
10ee5932 1742
10ee5932 1743 loopcxt_deinit(&lc);
10ee5932
KZ
1744 return res;
1745}
1746
af5efcd8
TW
1747/*
1748 * Returns: TRUE/FALSE
1749 */
14bb8e3c
KZ
1750int loopdev_has_backing_file(const char *device)
1751{
1752 char *tmp = loopdev_get_backing_file(device);
1753
1754 if (tmp) {
1755 free(tmp);
1756 return 1;
1757 }
1758 return 0;
1759}
1760
10ee5932
KZ
1761/*
1762 * Returns: TRUE/FALSE
1763 */
1764int loopdev_is_used(const char *device, const char *filename,
74a4705a 1765 uint64_t offset, uint64_t sizelimit, int flags)
10ee5932
KZ
1766{
1767 struct loopdev_cxt lc;
6c224de1 1768 struct stat st;
10ee5932
KZ
1769 int rc = 0;
1770
8b470b20 1771 if (!device || !filename)
10ee5932
KZ
1772 return 0;
1773
defa0710
KZ
1774 rc = loopcxt_init(&lc, 0);
1775 if (!rc)
1776 rc = loopcxt_set_device(&lc, device);
1777 if (rc)
1778 return rc;
10ee5932 1779
6c224de1 1780 rc = !stat(filename, &st);
74a4705a 1781 rc = loopcxt_is_used(&lc, rc ? &st : NULL, filename, offset, sizelimit, flags);
10ee5932 1782
10ee5932 1783 loopcxt_deinit(&lc);
10ee5932
KZ
1784 return rc;
1785}
1786
af5efcd8
TW
1787/*
1788 * Returns: 0 = success, < 0 error
1789 */
10ee5932
KZ
1790int loopdev_delete(const char *device)
1791{
1792 struct loopdev_cxt lc;
1793 int rc;
1794
8b470b20
KZ
1795 if (!device)
1796 return -EINVAL;
1797
defa0710
KZ
1798 rc = loopcxt_init(&lc, 0);
1799 if (!rc)
1800 rc = loopcxt_set_device(&lc, device);
10ee5932
KZ
1801 if (!rc)
1802 rc = loopcxt_delete_device(&lc);
1803 loopcxt_deinit(&lc);
1804 return rc;
1805}
1806
1807/*
1808 * Returns: 0 = success, < 0 error, 1 not found
1809 */
1810int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, const char *filename,
74a4705a 1811 uint64_t offset, uint64_t sizelimit, int flags)
10ee5932 1812{
6c224de1
KZ
1813 int rc, hasst;
1814 struct stat st;
10ee5932
KZ
1815
1816 if (!filename)
1817 return -EINVAL;
1818
6c224de1
KZ
1819 hasst = !stat(filename, &st);
1820
10ee5932
KZ
1821 rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
1822 if (rc)
1823 return rc;
1824
6c224de1 1825 while ((rc = loopcxt_next(lc)) == 0) {
10ee5932 1826
6c224de1 1827 if (loopcxt_is_used(lc, hasst ? &st : NULL,
74a4705a 1828 filename, offset, sizelimit, flags))
6c224de1 1829 break;
10ee5932
KZ
1830 }
1831
1832 loopcxt_deinit_iterator(lc);
1833 return rc;
1834}
1835
211e1d46 1836/*
dff7e160 1837 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
211e1d46 1838 */
c444a71b 1839int loopcxt_find_overlap(struct loopdev_cxt *lc, const char *filename,
211e1d46
SB
1840 uint64_t offset, uint64_t sizelimit)
1841{
1842 int rc, hasst;
1843 struct stat st;
1844
1845 if (!filename)
1846 return -EINVAL;
1847
9a94b634 1848 DBG(CXT, ul_debugobj(lc, "find_overlap requested"));
211e1d46
SB
1849 hasst = !stat(filename, &st);
1850
1851 rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
1852 if (rc)
1853 return rc;
1854
1855 while ((rc = loopcxt_next(lc)) == 0) {
1856 uint64_t lc_sizelimit, lc_offset;
1857
1858 rc = loopcxt_is_used(lc, hasst ? &st : NULL,
1859 filename, offset, sizelimit, 0);
562990b5
JK
1860 /*
1861 * Either the loopdev is unused or we've got an error which can
1862 * happen when we are racing with device autoclear. Just ignore
1863 * this loopdev...
1864 */
1865 if (rc <= 0)
1866 continue;
c444a71b 1867
211e1d46
SB
1868 DBG(CXT, ul_debugobj(lc, "found %s backed by %s",
1869 loopcxt_get_device(lc), filename));
c444a71b 1870
211e1d46
SB
1871 rc = loopcxt_get_offset(lc, &lc_offset);
1872 if (rc) {
1873 DBG(CXT, ul_debugobj(lc, "failed to get offset for device %s",
1874 loopcxt_get_device(lc)));
562990b5 1875 continue;
211e1d46
SB
1876 }
1877 rc = loopcxt_get_sizelimit(lc, &lc_sizelimit);
1878 if (rc) {
1879 DBG(CXT, ul_debugobj(lc, "failed to get sizelimit for device %s",
1880 loopcxt_get_device(lc)));
562990b5 1881 continue;
211e1d46
SB
1882 }
1883
dff7e160
KZ
1884 /* full match */
1885 if (lc_sizelimit == sizelimit && lc_offset == offset) {
1886 DBG(CXT, ul_debugobj(lc, "overlapping loop device %s (full match)",
1887 loopcxt_get_device(lc)));
1888 rc = 2;
1889 goto found;
1890 }
1891
1892 /* overlap */
211e1d46
SB
1893 if (lc_sizelimit != 0 && offset >= lc_offset + lc_sizelimit)
1894 continue;
1895 if (sizelimit != 0 && offset + sizelimit <= lc_offset)
1896 continue;
dff7e160 1897
211e1d46
SB
1898 DBG(CXT, ul_debugobj(lc, "overlapping loop device %s",
1899 loopcxt_get_device(lc)));
dff7e160
KZ
1900 rc = 1;
1901 goto found;
211e1d46 1902 }
dff7e160
KZ
1903
1904 if (rc == 1)
1905 rc = 0; /* not found */
1906found:
211e1d46 1907 loopcxt_deinit_iterator(lc);
9a94b634 1908 DBG(CXT, ul_debugobj(lc, "find_overlap done [rc=%d]", rc));
211e1d46
SB
1909 return rc;
1910}
1911
10ee5932
KZ
1912/*
1913 * Returns allocated string with device name
1914 */
74a4705a 1915char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, uint64_t sizelimit, int flags)
10ee5932
KZ
1916{
1917 struct loopdev_cxt lc;
1918 char *res = NULL;
1919
1920 if (!filename)
1921 return NULL;
1922
defa0710
KZ
1923 if (loopcxt_init(&lc, 0))
1924 return NULL;
74a4705a 1925 if (loopcxt_find_by_backing_file(&lc, filename, offset, sizelimit, flags) == 0)
10ee5932
KZ
1926 res = loopcxt_strdup_device(&lc);
1927 loopcxt_deinit(&lc);
1928
1929 return res;
1930}
1931
d5688130
KZ
1932/*
1933 * Returns number of loop devices associated with @file, if only one loop
11026083 1934 * device is associated with the given @filename and @loopdev is not NULL then
d5688130
KZ
1935 * @loopdev returns name of the device.
1936 */
1937int loopdev_count_by_backing_file(const char *filename, char **loopdev)
1938{
1939 struct loopdev_cxt lc;
defa0710 1940 int count = 0, rc;
d5688130
KZ
1941
1942 if (!filename)
1943 return -1;
1944
defa0710
KZ
1945 rc = loopcxt_init(&lc, 0);
1946 if (rc)
1947 return rc;
d5688130
KZ
1948 if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED))
1949 return -1;
1950
1951 while(loopcxt_next(&lc) == 0) {
1952 char *backing = loopcxt_get_backing_file(&lc);
1953
ad296391 1954 if (!backing || strcmp(backing, filename) != 0) {
d5688130
KZ
1955 free(backing);
1956 continue;
1957 }
1958
1959 free(backing);
1960 if (loopdev && count == 0)
1961 *loopdev = loopcxt_strdup_device(&lc);
1962 count++;
1963 }
1964
1965 loopcxt_deinit(&lc);
1966
1967 if (loopdev && count > 1) {
1968 free(*loopdev);
1969 *loopdev = NULL;
1970 }
1971 return count;
1972}
1973
d4423cce
KZ
1974#ifdef TEST_PROGRAM_LOOPDEV
1975int main(int argc, char *argv[])
1976{
1977 if (argc < 2)
1978 goto usage;
1979
1980 if (strcmp(argv[1], "--is-loopdev") == 0 && argc == 3)
1981 printf("%s: %s\n", argv[2], is_loopdev(argv[2]) ? "OK" : "FAIL");
1982 else
1983 goto usage;
1984
1985 return EXIT_SUCCESS;
1986usage:
1987 fprintf(stderr, "usage: %1$s --is-loopdev <dev>\n",
1988 program_invocation_short_name);
1989 return EXIT_FAILURE;
1990}
1991#endif
1992