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