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