2
* Copyright (C) 2011 Karel Zak <kzak@redhat.com>
4
* -- based on mount/losetup.c
6
* Simple library for work with loop devices.
8
* - requires kernel 2.6.x
9
* - reads info from /sys/block/loop<N>/loop/<attr> (new kernels)
10
* - reads info by ioctl
11
* - supports *unlimited* number of loop devices
12
* - supports /dev/loop<N> as well as /dev/loop/<N>
13
* - minimize overhead (fd, loopinfo, ... are shared for all operations)
14
* - setup (associate device and backing file)
15
* - delete (dis-associate file)
16
* - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported
26
#include <sys/ioctl.h>
29
#include <sys/sysmacros.h>
32
#include <linux/posix_types.h>
34
#include "linux_version.h"
37
#include "pathnames.h"
39
#include "canonicalize.h"
41
#define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
45
* @device: device name, absolute device path or NULL to reset the current setting
47
* Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
48
* names ("loop<N>") are converted to the path (/dev/loop<N> or to
51
* Returns: <0 on error, 0 on success
53
int loopcxt_set_device(struct loopdev_cxt *lc, const char *device)
64
memset(&lc->info, 0, sizeof(lc->info));
69
const char *dir = _PATH_DEV;
71
/* compose device name for /dev/loop<n> or /dev/loop/<n> */
72
if (lc->flags & LOOPDEV_FL_DEVSUBDIR) {
73
if (strlen(device) < 5)
76
dir = _PATH_DEV_LOOP "/"; /* _PATH_DEV uses tailing slash */
78
snprintf(lc->device, sizeof(lc->device), "%s%s",
81
strncpy(lc->device, device, sizeof(lc->device));
82
lc->device[sizeof(lc->device) - 1] = '\0';
86
sysfs_deinit(&lc->sysfs);
92
* @flags: LOOPDEV_FL_* flags
94
* Initilize loop handler.
96
* We have two sets of the flags:
98
* * LOOPDEV_FL_* flags control loopcxt_* API behavior
100
* * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
102
* Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
103
* syscall to open loop device. By default is the device open read-only.
105
* The expection is loopcxt_setup_device(), where the device is open read-write
106
* if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
108
* Returns: <0 on error, 0 on success.
110
int loopcxt_init(struct loopdev_cxt *lc, int flags)
115
memset(lc, 0, sizeof(*lc));
117
loopcxt_set_device(lc, NULL);
119
if (!(lc->flags & LOOPDEV_FL_NOSYSFS) &&
120
get_linux_version() >= KERNEL_VERSION(2,6,37))
122
* Use only sysfs for basic information about loop devices
124
lc->flags |= LOOPDEV_FL_NOIOCTL;
132
* Deinitialize loop context
134
void loopcxt_deinit(struct loopdev_cxt *lc)
142
loopcxt_set_device(lc, NULL);
143
loopcxt_deinit_iterator(lc);
149
* Returns newly allocated device path.
151
char *loopcxt_strdup_device(struct loopdev_cxt *lc)
153
if (!lc || !*lc->device)
155
return strdup(lc->device);
161
* Returns pointer device name in the @lc struct.
163
const char *loopcxt_get_device(struct loopdev_cxt *lc)
165
return lc ? lc->device : NULL;
171
* Returns pointer to the sysfs context (see lib/sysfs.c)
173
struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)
175
if (!lc || !*lc->device || (lc->flags & LOOPDEV_FL_NOSYSFS))
178
if (!lc->sysfs.devno) {
179
dev_t devno = sysfs_devname_to_devno(lc->device, NULL);
183
if (sysfs_init(&lc->sysfs, devno, NULL))
192
* Returns: file descriptor to the open loop device or <0 on error. The mode
193
* depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
196
int loopcxt_get_fd(struct loopdev_cxt *lc)
198
if (!lc || !*lc->device)
202
lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY;
203
lc->fd = open(lc->device, lc->mode);
208
int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, int mode)
220
* @flags: LOOPITER_FL_* flags
222
* Iterator allows to scan list of the free or used loop devices.
224
* Returns: <0 on error, 0 on success
226
int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags)
228
struct loopdev_iter *iter;
238
memset(iter, 0, sizeof(*iter));
241
iter->default_check = 1;
243
if (!lc->extra_check) {
245
* Check for /dev/loop/<N> subdirectory
247
if (!(lc->flags & LOOPDEV_FL_DEVSUBDIR) &&
248
stat(_PATH_DEV_LOOP, &st) == 0 && S_ISDIR(st.st_mode))
249
lc->flags |= LOOPDEV_FL_DEVSUBDIR;
259
* Returns: <0 on error, 0 on success
261
int loopcxt_deinit_iterator(struct loopdev_cxt *lc)
263
struct loopdev_iter *iter = &lc->iter;
280
* Same as loopcxt_set_device, but also checks if the device is
281
* associeted with any file.
283
* Returns: <0 on error, 0 on success, 1 device does not match with
284
* LOOPITER_FL_{USED,FREE} flags.
286
static int loopiter_set_device(struct loopdev_cxt *lc, const char *device)
288
int rc = loopcxt_set_device(lc, device);
294
if (!(lc->iter.flags & LOOPITER_FL_USED) &&
295
!(lc->iter.flags & LOOPITER_FL_FREE))
296
return 0; /* caller does not care about device status */
298
used = loopcxt_get_offset(lc, NULL) == 0;
300
if ((lc->iter.flags & LOOPITER_FL_USED) && used)
303
if ((lc->iter.flags & LOOPITER_FL_FREE) && !used)
306
loopcxt_set_device(lc, NULL);
310
static int cmpnum(const void *p1, const void *p2)
312
return (((* (int *) p1) > (* (int *) p2)) -
313
((* (int *) p1) < (* (int *) p2)));
317
* The classic scandir() is more expensive and less portable.
318
* We needn't full loop device names -- loop numbers (loop<N>)
321
static int loop_scandir(const char *dirname, int **ary, int hasprefix)
325
unsigned int n, count = 0, arylen = 0;
327
if (!dirname || !ary)
329
dir = opendir(dirname);
336
while((d = readdir(dir))) {
337
#ifdef _DIRENT_HAVE_D_TYPE
338
if (d->d_type != DT_BLK && d->d_type != DT_UNKNOWN &&
342
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
347
if (sscanf(d->d_name, "loop%u", &n) != 1)
353
n = strtol(d->d_name, &end, 10);
354
if (d->d_name == end || (end && *end) || errno)
357
if (n < LOOPDEV_DEFAULT_NNODES)
358
continue; /* ignore loop<0..7> */
360
if (count + 1 > arylen) {
365
tmp = realloc(*ary, arylen * sizeof(int));
376
qsort(*ary, count, sizeof(int), cmpnum);
383
* @lc: context, has to initialized by loopcxt_init_iterator()
385
* Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
386
* about the current loop device are available by
387
* loopcxt_get_{fd,backing_file,device,offset, ...} functions.
389
int loopcxt_next(struct loopdev_cxt *lc)
391
struct loopdev_iter *iter;
399
/* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
401
if (iter->flags & LOOPITER_FL_USED) {
405
iter->proc = fopen(_PATH_PROC_PARTITIONS, "r");
407
while (iter->proc && fgets(buf, sizeof(buf), iter->proc)) {
411
if (sscanf(buf, " %u %*s %*s %128[^\n ]",
412
&m, name) != 2 || m != LOOPDEV_MAJOR)
415
if (loopiter_set_device(lc, name) == 0)
422
/* B) Classic way, try first eight loop devices (default number
423
* of loop devices). This is enough for 99% of all cases.
425
if (iter->default_check) {
426
for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES;
429
snprintf(name, sizeof(name), "loop%d", iter->ncur);
431
if (loopiter_set_device(lc, name) == 0)
434
iter->default_check = 0;
437
/* C) the worst possibility, scan whole /dev or /dev/loop/<N>
440
iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ?
441
loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) :
442
loop_scandir(_PATH_DEV, &iter->minors, 1);
445
for (++iter->ncur; iter->ncur < iter->nminors; iter->ncur++) {
447
snprintf(name, sizeof(name), "loop%d", iter->minors[iter->ncur]);
449
if (loopiter_set_device(lc, name) == 0)
453
loopcxt_deinit_iterator(lc);
458
* @device: path to device
460
int is_loopdev(const char *device)
467
return (stat(device, &st) == 0 &&
468
S_ISBLK(st.st_mode) &&
469
major(st.st_rdev) == LOOPDEV_MAJOR);
475
* Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
477
struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc)
486
fd = loopcxt_get_fd(lc);
490
if (ioctl(fd, LOOP_GET_STATUS64, &lc->info) == 0) {
501
* Returns (allocated) string with path to the file assicieted
502
* with the current loop device.
504
char *loopcxt_get_backing_file(struct loopdev_cxt *lc)
506
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
511
* This is always preffered, the loop_info64
512
* has too small buffer for the filename.
514
res = sysfs_strdup(sysfs, "loop/backing_file");
516
if (!res && loopcxt_ioctl_enabled(lc)) {
517
struct loop_info64 *lo = loopcxt_get_info(lc);
520
lo->lo_file_name[LO_NAME_SIZE - 2] = '*';
521
lo->lo_file_name[LO_NAME_SIZE - 1] = '\0';
522
res = strdup((char *) lo->lo_file_name);
530
* @offset: returns offset number for the given device
532
* Returns: <0 on error, 0 on success
534
int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset)
536
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
540
rc = sysfs_read_u64(sysfs, "loop/offset", offset);
542
if (rc && loopcxt_ioctl_enabled(lc)) {
543
struct loop_info64 *lo = loopcxt_get_info(lc);
546
*offset = lo->lo_offset;
556
* @sizelimit: returns size limit for the given device
558
* Returns: <0 on error, 0 on success
560
int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size)
562
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
566
rc = sysfs_read_u64(sysfs, "loop/sizelimit", size);
568
if (rc && loopcxt_ioctl_enabled(lc)) {
569
struct loop_info64 *lo = loopcxt_get_info(lc);
572
*size = lo->lo_sizelimit;
583
* Returns: 1 of the autoclear flags is set.
585
int loopcxt_is_autoclear(struct loopdev_cxt *lc)
587
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
591
if (sysfs_read_int(sysfs, "loop/autoclear", &fl) == 0)
595
if (loopcxt_ioctl_enabled(lc)) {
596
struct loop_info64 *lo = loopcxt_get_info(lc);
598
return lo->lo_flags & LO_FLAGS_AUTOCLEAR;
606
* Returns: 1 of the readonly flags is set.
608
int loopcxt_is_readonly(struct loopdev_cxt *lc)
610
struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
614
if (sysfs_read_int(sysfs, "ro", &fl) == 0)
618
if (loopcxt_ioctl_enabled(lc)) {
619
struct loop_info64 *lo = loopcxt_get_info(lc);
621
return lo->lo_flags & LO_FLAGS_READ_ONLY;
627
* The setting is removed by loopcxt_set_device() loopcxt_next()!
629
int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset)
633
lc->info.lo_offset = offset;
638
* The setting is removed by loopcxt_set_device() loopcxt_next()!
640
int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit)
644
lc->info.lo_sizelimit = sizelimit;
650
* @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
652
* The setting is removed by loopcxt_set_device() loopcxt_next()!
654
* Returns: 0 on success, <0 on error.
656
int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags)
660
lc->info.lo_flags = flags;
666
* @filename: backing file path (the path will be canonicalized)
668
* The setting is removed by loopcxt_set_device() loopcxt_next()!
670
* Returns: 0 on success, <0 on error.
672
int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename)
677
lc->filename = canonicalize_path(filename);
681
strncpy((char *)lc->info.lo_file_name, lc->filename, LO_NAME_SIZE);
682
lc->info.lo_file_name[LO_NAME_SIZE- 1] = '\0';
687
static int digits_only(const char *s)
697
* @encryption: encryption name / type (see lopsetup man page)
700
* Note that the encryption functionality is deprecated an unmaintained. Use
701
* cryptsetup (it also supports AES-loops).
703
* The setting is removed by loopcxt_set_device() loopcxt_next()!
705
* Returns: 0 on success, <0 on error.
707
int loopcxt_set_encryption(struct loopdev_cxt *lc,
708
const char *encryption,
709
const char *password)
714
if (encryption && *encryption) {
715
if (digits_only(encryption)) {
716
lc->info.lo_encrypt_type = atoi(encryption);
718
lc->info.lo_encrypt_type = LO_CRYPT_CRYPTOAPI;
719
snprintf((char *)lc->info.lo_crypt_name, LO_NAME_SIZE,
724
switch (lc->info.lo_encrypt_type) {
726
lc->info.lo_encrypt_key_size = 0;
729
memset(lc->info.lo_encrypt_key, 0, LO_KEY_SIZE);
730
strncpy((char *)lc->info.lo_encrypt_key, password, LO_KEY_SIZE);
731
lc->info.lo_encrypt_key[LO_KEY_SIZE - 1] = '\0';
732
lc->info.lo_encrypt_key_size = LO_KEY_SIZE;
741
* Associate the current device (see loopcxt_{set,get}_device()) with
742
* a file (see loopcxt_set_backing_file()).
744
* The device is initialized read-write by default. If you want read-only
745
* device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
746
* flags are ignored and modified according to LO_FLAGS_*.
748
* If the device is already open by loopcxt_get_fd() then this setup device
749
* function will re-open the device to fix read/write mode.
751
* The device is also initialized read-only if the backing file is not
752
* possible to open read-write (e.g. read-only FS).
754
* Returns: <0 on error, 0 on success.
756
int loopcxt_setup_device(struct loopdev_cxt *lc)
758
int file_fd, dev_fd, mode = O_RDWR, rc = -1;
760
if (!lc || !*lc->device || !lc->filename)
764
* Open backing file and device
766
if (lc->info.lo_flags & LO_FLAGS_READ_ONLY)
769
if ((file_fd = open(lc->filename, mode)) < 0) {
770
if (mode != O_RDONLY && (errno == EROFS || errno == EACCES))
771
file_fd = open(lc->filename, mode = O_RDONLY);
777
if (lc->fd != -1 && lc->mode != mode) {
783
if (mode == O_RDONLY) {
784
lc->flags |= LOOPDEV_FL_RDONLY; /* open() mode */
785
lc->info.lo_flags |= LO_FLAGS_READ_ONLY; /* kernel loopdev mode */
787
lc->flags |= LOOPDEV_FL_RDWR; /* open() mode */
788
lc->info.lo_flags &= ~LO_FLAGS_READ_ONLY;
789
lc->flags &= ~LOOPDEV_FL_RDONLY;
792
dev_fd = loopcxt_get_fd(lc);
801
if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
808
if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info))
811
memset(&lc->info, 0, sizeof(lc->info));
819
ioctl(dev_fd, LOOP_CLR_FD, 0);
824
int loopcxt_delete_device(struct loopdev_cxt *lc)
826
int fd = loopcxt_get_fd(lc);
831
if (ioctl(fd, LOOP_CLR_FD, 0) < 0)
836
int loopcxt_find_unused(struct loopdev_cxt *lc)
840
rc = loopcxt_init_iterator(lc, LOOPITER_FL_FREE);
844
rc = loopcxt_next(lc);
845
loopcxt_deinit_iterator(lc);
855
int loopdev_is_autoclear(const char *device)
857
struct loopdev_cxt lc;
863
loopcxt_init(&lc, 0);
864
loopcxt_set_device(&lc, device);
865
rc = loopcxt_is_autoclear(&lc);
871
char *loopdev_get_backing_file(const char *device)
873
struct loopdev_cxt lc;
879
loopcxt_init(&lc, 0);
880
loopcxt_set_device(&lc, device);
881
res = loopcxt_get_backing_file(&lc);
888
* Returns: TRUE/FALSE
890
int loopdev_is_used(const char *device, const char *filename,
891
uint64_t offset, int flags)
893
struct loopdev_cxt lc;
894
char *backing = NULL;
900
loopcxt_init(&lc, 0);
901
loopcxt_set_device(&lc, device);
903
backing = loopcxt_get_backing_file(&lc);
906
if (filename && strcmp(filename, backing) != 0)
908
if (flags & LOOPDEV_FL_OFFSET) {
911
if (loopcxt_get_offset(&lc, &off) != 0 || off != offset)
923
int loopdev_delete(const char *device)
925
struct loopdev_cxt lc;
928
loopcxt_init(&lc, 0);
929
rc = loopcxt_set_device(&lc, device);
931
rc = loopcxt_delete_device(&lc);
937
* Returns: 0 = success, < 0 error, 1 not found
939
int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, const char *filename,
940
uint64_t offset, int flags)
947
rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
951
while((rc = loopcxt_next(lc)) == 0) {
952
char *backing = loopcxt_get_backing_file(lc);
954
if (!backing || strcmp(backing, filename)) {
961
if (flags & LOOPDEV_FL_OFFSET) {
963
if (loopcxt_get_offset(lc, &off) != 0 || offset != off)
971
loopcxt_deinit_iterator(lc);
976
* Returns allocated string with device name
978
char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int flags)
980
struct loopdev_cxt lc;
986
loopcxt_init(&lc, 0);
987
if (loopcxt_find_by_backing_file(&lc, filename, offset, flags))
988
res = loopcxt_strdup_device(&lc);
995
#ifdef TEST_PROGRAM_LOOPDEV
999
static void test_loop_info(const char *device, int flags)
1001
struct loopdev_cxt lc;
1005
loopcxt_init(&lc, flags);
1006
if (loopcxt_set_device(&lc, device))
1007
err(EXIT_FAILURE, "failed to set device");
1009
p = loopcxt_get_backing_file(&lc);
1010
printf("\tBACKING FILE: %s\n", p);
1013
if (loopcxt_get_offset(&lc, &u64) == 0)
1014
printf("\tOFFSET: %jd\n", u64);
1016
if (loopcxt_get_sizelimit(&lc, &u64) == 0)
1017
printf("\tSIZE LIMIT: %jd\n", u64);
1019
printf("\tAUTOCLEAR: %s\n", loopcxt_is_autoclear(&lc) ? "YES" : "NOT");
1021
loopcxt_deinit(&lc);
1024
static void test_loop_scan(int flags)
1026
struct loopdev_cxt lc;
1029
loopcxt_init(&lc, 0);
1031
if (loopcxt_init_iterator(&lc, flags))
1032
err(EXIT_FAILURE, "iterator initlization failed");
1034
while((rc = loopcxt_next(&lc)) == 0) {
1035
const char *device = loopcxt_get_device(&lc);
1037
if (flags & LOOPITER_FL_USED) {
1038
char *backing = loopcxt_get_backing_file(&lc);
1039
printf("\t%s: %s\n", device, backing);
1042
printf("\t%s\n", device);
1046
err(EXIT_FAILURE, "loopdevs scanning failed");
1048
loopcxt_deinit(&lc);
1051
static int test_loop_setup(const char *filename, const char *device)
1053
struct loopdev_cxt lc;
1056
loopcxt_init(&lc, 0);
1059
rc = loopcxt_set_device(&lc, device);
1061
err(EXIT_FAILURE, "failed to set device: %s", device);
1066
rc = loopcxt_find_unused(&lc);
1068
err(EXIT_FAILURE, "failed to find unused device");
1069
printf("Trying to use '%s'\n", loopcxt_get_device(&lc));
1072
if (loopcxt_set_backing_file(&lc, filename))
1073
err(EXIT_FAILURE, "failed to set backing file");
1075
rc = loopcxt_setup_device(&lc);
1077
break; /* success */
1079
if (device || rc != -EBUSY)
1080
err(EXIT_FAILURE, "failed to setup device for %s",
1083
printf("device stolen...trying again\n");
1086
loopcxt_deinit(&lc);
1091
int main(int argc, char *argv[])
1096
if (argc == 3 && strcmp(argv[1], "--info") == 0) {
1097
printf("---sysfs & ioctl:---\n");
1098
test_loop_info(argv[2], 0);
1099
printf("---sysfs only:---\n");
1100
test_loop_info(argv[2], LOOPDEV_FL_NOIOCTL);
1101
printf("---ioctl only:---\n");
1102
test_loop_info(argv[2], LOOPDEV_FL_NOSYSFS);
1104
} else if (argc == 2 && strcmp(argv[1], "--used") == 0) {
1105
printf("---all used devices---\n");
1106
test_loop_scan(LOOPITER_FL_USED);
1108
} else if (argc == 2 && strcmp(argv[1], "--free") == 0) {
1109
printf("---all free devices---\n");
1110
test_loop_scan(LOOPITER_FL_FREE);
1112
} else if (argc >= 3 && strcmp(argv[1], "--setup") == 0) {
1113
test_loop_setup(argv[2], argv[3]);
1115
} else if (argc == 3 && strcmp(argv[1], "--delete") == 0) {
1116
if (loopdev_delete(argv[2]))
1117
errx(EXIT_FAILURE, "failed to deinitialize device %s", argv[2]);
1121
return EXIT_SUCCESS;
1124
errx(EXIT_FAILURE, "usage: \n"
1125
" %1$s --info <device>\n"
1128
" %1$s --setup <filename> [<device>]\n"
1133
#endif /* TEST_PROGRAM */