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