3 * No copyright is claimed. This code is in the public domain; do with
6 * Written by Karel Zak <kzak@redhat.com>
8 * -- based on mount/losetup.c
10 * Simple library for work with loop devices.
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
30 #include <sys/ioctl.h>
36 #include "linux_version.h"
39 #include "pathnames.h"
41 #include "canonicalize.h"
44 #include "fileutils.h"
46 #define LOOPDEV_MAX_TRIES 10
49 * Debug stuff (based on include/debug.h)
51 static UL_DEBUG_DEFINE_MASK(loopdev
);
52 UL_DEBUG_DEFINE_MASKNAMES(loopdev
) = UL_DEBUG_EMPTY_MASKNAMES
;
54 #define LOOPDEV_DEBUG_INIT (1 << 1)
55 #define LOOPDEV_DEBUG_CXT (1 << 2)
56 #define LOOPDEV_DEBUG_ITER (1 << 3)
57 #define LOOPDEV_DEBUG_SETUP (1 << 4)
59 #define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x)
60 #define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x)
62 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(loopdev)
65 static void loopdev_init_debug(void)
67 if (loopdev_debug_mask
)
69 __UL_INIT_DEBUG_FROM_ENV(loopdev
, LOOPDEV_DEBUG_
, 0, LOOPDEV_DEBUG
);
75 #define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
76 #define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
77 && !loopcxt_ioctl_enabled(_lc)
80 * Calls @x and repeat on EAGAIN
82 #define repeat_on_eagain(x) __extension__ ({ \
87 if (_e == 0 || errno != EAGAIN) \
89 if (_c >= LOOPDEV_MAX_TRIES) \
94 _e == 0 ? 0 : errno ? -errno : -1; \
99 * @device: device name, absolute device path or NULL to reset the current setting
101 * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
102 * names ("loop<N>") are converted to the path (/dev/loop<N> or to
105 * This sets the device name, but does not check if the device exists!
107 * Returns: <0 on error, 0 on success
109 int loopcxt_set_device(struct loopdev_cxt
*lc
, const char *device
)
116 DBG(CXT
, ul_debugobj(lc
, "closing old open fd"));
126 memset(&lc
->config
, 0, sizeof(lc
->config
));
130 if (*device
!= '/') {
131 const char *dir
= _PATH_DEV
;
133 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
134 if (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) {
135 if (strlen(device
) < 5)
138 dir
= _PATH_DEV_LOOP
"/"; /* _PATH_DEV uses trailing slash */
140 snprintf(lc
->device
, sizeof(lc
->device
), "%s%s",
143 xstrncpy(lc
->device
, device
, sizeof(lc
->device
));
145 DBG(CXT
, ul_debugobj(lc
, "%s name assigned", device
));
148 ul_unref_path(lc
->sysfs
);
153 int loopcxt_has_device(struct loopdev_cxt
*lc
)
155 return lc
&& *lc
->device
;
158 dev_t
loopcxt_get_devno(struct loopdev_cxt
*lc
)
160 if (!lc
|| !loopcxt_has_device(lc
))
163 lc
->devno
= sysfs_devname_to_devno(lc
->device
);
167 int loopcxt_is_lost(struct loopdev_cxt
*lc
)
169 if (!lc
|| !loopcxt_has_device(lc
))
174 lc
->is_lost
= access(lc
->device
, F_OK
) != 0
175 && loopcxt_get_devno(lc
) != 0;
182 * @flags: LOOPDEV_FL_* flags
184 * Initialize loop handler.
186 * We have two sets of the flags:
188 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
190 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
192 * Returns: <0 on error, 0 on success.
194 int loopcxt_init(struct loopdev_cxt
*lc
, int flags
)
198 struct loopdev_cxt dummy
= UL_LOOPDEVCXT_EMPTY
;
203 loopdev_init_debug();
204 DBG(CXT
, ul_debugobj(lc
, "initialize context"));
206 memcpy(lc
, &dummy
, sizeof(dummy
));
209 rc
= loopcxt_set_device(lc
, NULL
);
213 if (stat(_PATH_SYS_BLOCK
, &st
) || !S_ISDIR(st
.st_mode
)) {
214 lc
->flags
|= LOOPDEV_FL_NOSYSFS
;
215 lc
->flags
&= ~LOOPDEV_FL_NOIOCTL
;
216 DBG(CXT
, ul_debugobj(lc
, "init: disable /sys usage"));
219 if (!(lc
->flags
& LOOPDEV_FL_NOSYSFS
) &&
220 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
222 * Use only sysfs for basic information about loop devices
224 lc
->flags
|= LOOPDEV_FL_NOIOCTL
;
225 DBG(CXT
, ul_debugobj(lc
, "init: ignore ioctls"));
228 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
) && !stat(_PATH_DEV_LOOPCTL
, &st
)) {
229 lc
->flags
|= LOOPDEV_FL_CONTROL
;
230 DBG(CXT
, ul_debugobj(lc
, "init: loop-control detected "));
239 * Deinitialize loop context
241 void loopcxt_deinit(struct loopdev_cxt
*lc
)
248 DBG(CXT
, ul_debugobj(lc
, "de-initialize"));
253 ignore_result( loopcxt_set_device(lc
, NULL
) );
254 loopcxt_deinit_iterator(lc
);
262 * Returns newly allocated device path.
264 char *loopcxt_strdup_device(struct loopdev_cxt
*lc
)
266 if (!lc
|| !*lc
->device
)
268 return strdup(lc
->device
);
274 * Returns pointer device name in the @lc struct.
276 const char *loopcxt_get_device(struct loopdev_cxt
*lc
)
278 return lc
&& *lc
->device
? lc
->device
: NULL
;
284 * Returns pointer to the sysfs context (see lib/sysfs.c)
286 static struct path_cxt
*loopcxt_get_sysfs(struct loopdev_cxt
*lc
)
288 if (!lc
|| !*lc
->device
|| (lc
->flags
& LOOPDEV_FL_NOSYSFS
))
292 dev_t devno
= loopcxt_get_devno(lc
);
294 DBG(CXT
, ul_debugobj(lc
, "sysfs: failed devname to devno"));
298 lc
->sysfs
= ul_new_sysfs_path(devno
, NULL
, NULL
);
300 DBG(CXT
, ul_debugobj(lc
, "sysfs: init failed"));
306 static int __loopcxt_get_fd(struct loopdev_cxt
*lc
, mode_t mode
)
310 if (!lc
|| !*lc
->device
)
313 /* It's okay to return a FD with read-write permissions if someone
314 * asked for read-only, but you shouldn't do the opposite.
316 * (O_RDONLY is a widely usable default.)
318 if (lc
->fd
>= 0 && mode
== O_RDWR
&& lc
->mode
== O_RDONLY
) {
319 DBG(CXT
, ul_debugobj(lc
, "closing already open device (mode mismatch)"));
326 lc
->fd
= open(lc
->device
, lc
->mode
| O_CLOEXEC
);
327 DBG(CXT
, ul_debugobj(lc
, "open %s [%s]: %m", lc
->device
,
328 mode
== O_RDONLY
? "ro" :
329 mode
== O_RDWR
? "rw" : "??"));
331 if (lc
->fd
< 0 && old
>= 0) {
332 /* restore original on error */
343 /* default is read-only file descriptor, it's enough for all ioctls */
344 int loopcxt_get_fd(struct loopdev_cxt
*lc
)
346 return __loopcxt_get_fd(lc
, O_RDONLY
);
351 * @flags: LOOPITER_FL_* flags
353 * Iterator can be used to scan list of the free or used loop devices.
355 * Returns: <0 on error, 0 on success
357 int loopcxt_init_iterator(struct loopdev_cxt
*lc
, int flags
)
359 struct loopdev_iter
*iter
;
367 DBG(ITER
, ul_debugobj(iter
, "initialize"));
371 memset(iter
, 0, sizeof(*iter
));
374 iter
->default_check
= 1;
376 if (!lc
->extra_check
) {
378 * Check for /dev/loop/<N> subdirectory
380 if (!(lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) &&
381 stat(_PATH_DEV_LOOP
, &st
) == 0 && S_ISDIR(st
.st_mode
))
382 lc
->flags
|= LOOPDEV_FL_DEVSUBDIR
;
392 * Returns: <0 on error, 0 on success
394 int loopcxt_deinit_iterator(struct loopdev_cxt
*lc
)
396 struct loopdev_iter
*iter
;
402 DBG(ITER
, ul_debugobj(iter
, "de-initialize"));
408 closedir(iter
->sysblock
);
410 memset(iter
, 0, sizeof(*iter
));
415 * Same as loopcxt_set_device, but also checks if the device is
416 * associated with any file.
418 * Returns: <0 on error, 0 on success, 1 device does not match with
419 * LOOPITER_FL_{USED,FREE} flags.
421 static int loopiter_set_device(struct loopdev_cxt
*lc
, const char *device
)
423 int rc
= loopcxt_set_device(lc
, device
);
429 if (!(lc
->iter
.flags
& LOOPITER_FL_USED
) &&
430 !(lc
->iter
.flags
& LOOPITER_FL_FREE
))
431 return 0; /* caller does not care about device status */
433 used
= loopcxt_get_offset(lc
, NULL
) == 0;
435 if ((lc
->iter
.flags
& LOOPITER_FL_USED
) && used
)
438 if ((lc
->iter
.flags
& LOOPITER_FL_FREE
) && !used
)
441 DBG(ITER
, ul_debugobj(&lc
->iter
, "failed to use %s device", lc
->device
));
443 ignore_result( loopcxt_set_device(lc
, NULL
) );
447 static int cmpnum(const void *p1
, const void *p2
)
449 return (((* (const int *) p1
) > (* (const int *) p2
)) -
450 ((* (const int *) p1
) < (* (const int *) p2
)));
454 * The classic scandir() is more expensive and less portable.
455 * We needn't full loop device names -- loop numbers (loop<N>)
458 static int loop_scandir(const char *dirname
, int **ary
, int hasprefix
)
462 unsigned int n
, count
= 0, arylen
= 0;
464 if (!dirname
|| !ary
)
467 DBG(ITER
, ul_debug("scan dir: %s", dirname
));
469 dir
= opendir(dirname
);
475 while((d
= readdir(dir
))) {
476 #ifdef _DIRENT_HAVE_D_TYPE
477 if (d
->d_type
!= DT_BLK
&& d
->d_type
!= DT_UNKNOWN
&&
481 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
486 if (sscanf(d
->d_name
, "loop%u", &n
) != 1)
493 n
= strtol(d
->d_name
, &end
, 10);
494 if (d
->d_name
== end
|| (end
&& *end
) || errno
)
497 if (n
< LOOPDEV_DEFAULT_NNODES
)
498 continue; /* ignore loop<0..7> */
500 if (count
+ 1 > arylen
) {
505 tmp
= reallocarray(*ary
, arylen
, sizeof(int));
518 qsort(*ary
, count
, sizeof(int), cmpnum
);
525 * Set the next *used* loop device according to /proc/partitions.
527 * Loop devices smaller than 512 bytes are invisible for this function.
529 static int loopcxt_next_from_proc(struct loopdev_cxt
*lc
)
531 struct loopdev_iter
*iter
= &lc
->iter
;
534 DBG(ITER
, ul_debugobj(iter
, "scan /proc/partitions"));
537 iter
->proc
= fopen(_PATH_PROC_PARTITIONS
, "r" UL_CLOEXECSTR
);
541 while (fgets(buf
, sizeof(buf
), iter
->proc
)) {
546 if (sscanf(buf
, " %u %*s %*s %128[^\n ]",
547 &m
, name
) != 2 || m
!= LOOPDEV_MAJOR
)
550 DBG(ITER
, ul_debugobj(iter
, "checking %s", name
));
552 if (loopiter_set_device(lc
, name
) == 0)
560 * Set the next *used* loop device according to
561 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
563 * This is preferred method.
565 static int loopcxt_next_from_sysfs(struct loopdev_cxt
*lc
)
567 struct loopdev_iter
*iter
= &lc
->iter
;
571 DBG(ITER
, ul_debugobj(iter
, "scanning /sys/block"));
574 iter
->sysblock
= opendir(_PATH_SYS_BLOCK
);
579 fd
= dirfd(iter
->sysblock
);
581 while ((d
= readdir(iter
->sysblock
))) {
582 char name
[NAME_MAX
+ 18 + 1];
585 DBG(ITER
, ul_debugobj(iter
, "check %s", d
->d_name
));
587 if (strcmp(d
->d_name
, ".") == 0
588 || strcmp(d
->d_name
, "..") == 0
589 || strncmp(d
->d_name
, "loop", 4) != 0)
592 snprintf(name
, sizeof(name
), "%s/loop/backing_file", d
->d_name
);
593 if (fstatat(fd
, name
, &st
, 0) != 0)
596 if (loopiter_set_device(lc
, d
->d_name
) == 0)
604 * @lc: context, has to initialized by loopcxt_init_iterator()
606 * Returns: 0 on success, < 0 on error, 1 at the end of scanning. The details
607 * about the current loop device are available by
608 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
610 int loopcxt_next(struct loopdev_cxt
*lc
)
612 struct loopdev_iter
*iter
;
622 DBG(ITER
, ul_debugobj(iter
, "next"));
624 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
626 if (iter
->flags
& LOOPITER_FL_USED
) {
629 if (loopcxt_sysfs_available(lc
))
630 rc
= loopcxt_next_from_sysfs(lc
);
632 rc
= loopcxt_next_from_proc(lc
);
638 /* B) Classic way, try first eight loop devices (default number
639 * of loop devices). This is enough for 99% of all cases.
641 if (iter
->default_check
) {
642 DBG(ITER
, ul_debugobj(iter
, "next: default check"));
643 for (++iter
->ncur
; iter
->ncur
< LOOPDEV_DEFAULT_NNODES
;
646 snprintf(name
, sizeof(name
), "loop%d", iter
->ncur
);
648 if (loopiter_set_device(lc
, name
) == 0)
651 iter
->default_check
= 0;
654 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
657 DBG(ITER
, ul_debugobj(iter
, "next: scanning /dev"));
658 iter
->nminors
= (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) ?
659 loop_scandir(_PATH_DEV_LOOP
, &iter
->minors
, 0) :
660 loop_scandir(_PATH_DEV
, &iter
->minors
, 1);
663 for (++iter
->ncur
; iter
->ncur
< iter
->nminors
; iter
->ncur
++) {
665 snprintf(name
, sizeof(name
), "loop%d", iter
->minors
[iter
->ncur
]);
667 if (loopiter_set_device(lc
, name
) == 0)
671 loopcxt_deinit_iterator(lc
);
676 * @device: path to device
678 int is_loopdev(const char *device
)
683 if (!device
|| stat(device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
685 else if (major(st
.st_rdev
) == LOOPDEV_MAJOR
)
687 else if (sysfs_devno_is_wholedisk(st
.st_rdev
)) {
688 /* It's possible that kernel creates a device with a different
689 * major number ... check by /sys it's really loop device.
691 char name
[PATH_MAX
], *cn
, *p
= NULL
;
693 snprintf(name
, sizeof(name
), _PATH_SYS_DEVBLOCK
"/%d:%d",
694 major(st
.st_rdev
), minor(st
.st_rdev
));
695 cn
= canonicalize_path(name
);
697 p
= stripoff_last_component(cn
);
698 rc
= p
&& ul_startswith(p
, "loop");
710 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
712 struct loop_info64
*loopcxt_get_info(struct loopdev_cxt
*lc
)
716 if (!lc
|| lc
->info_failed
) {
722 return &lc
->config
.info
;
724 fd
= loopcxt_get_fd(lc
);
728 if (ioctl(fd
, LOOP_GET_STATUS64
, &lc
->config
.info
) == 0) {
731 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 OK"));
732 return &lc
->config
.info
;
736 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 FAILED"));
744 * Returns (allocated) string with path to the file associated
745 * with the current loop device.
747 char *loopcxt_get_backing_file(struct loopdev_cxt
*lc
)
749 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
754 * This is always preferred, the loop_info64
755 * has too small buffer for the filename.
757 ul_path_read_string(sysfs
, &res
, "loop/backing_file");
759 if (!res
&& loopcxt_ioctl_enabled(lc
)) {
760 struct loop_info64
*lo
= loopcxt_get_info(lc
);
763 lo
->lo_file_name
[LO_NAME_SIZE
- 2] = '*';
764 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
765 res
= strdup((char *) lo
->lo_file_name
);
769 DBG(CXT
, ul_debugobj(lc
, "get_backing_file [%s]", res
));
776 * Returns (allocated) string with loop reference. The same as backing file by
779 char *loopcxt_get_refname(struct loopdev_cxt
*lc
)
782 struct loop_info64
*lo
= loopcxt_get_info(lc
);
785 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
786 res
= strdup((char *) lo
->lo_file_name
);
789 DBG(CXT
, ul_debugobj(lc
, "get_refname [%s]", res
));
795 * @offset: returns offset number for the given device
797 * Returns: <0 on error, 0 on success
799 int loopcxt_get_offset(struct loopdev_cxt
*lc
, uint64_t *offset
)
801 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
805 if (ul_path_read_u64(sysfs
, offset
, "loop/offset") == 0)
808 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
809 struct loop_info64
*lo
= loopcxt_get_info(lc
);
812 *offset
= lo
->lo_offset
;
818 DBG(CXT
, ul_debugobj(lc
, "get_offset [rc=%d]", rc
));
824 * @blocksize: returns logical blocksize for the given device
826 * Returns: <0 on error, 0 on success
828 int loopcxt_get_blocksize(struct loopdev_cxt
*lc
, uint64_t *blocksize
)
830 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
834 if (ul_path_read_u64(sysfs
, blocksize
, "queue/logical_block_size") == 0)
837 /* Fallback based on BLKSSZGET ioctl */
839 int fd
= loopcxt_get_fd(lc
);
844 rc
= blkdev_get_sector_size(fd
, &sz
);
851 DBG(CXT
, ul_debugobj(lc
, "get_blocksize [rc=%d]", rc
));
857 * @sizelimit: returns size limit for the given device
859 * Returns: <0 on error, 0 on success
861 int loopcxt_get_sizelimit(struct loopdev_cxt
*lc
, uint64_t *size
)
863 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
867 if (ul_path_read_u64(sysfs
, size
, "loop/sizelimit") == 0)
870 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
871 struct loop_info64
*lo
= loopcxt_get_info(lc
);
874 *size
= lo
->lo_sizelimit
;
880 DBG(CXT
, ul_debugobj(lc
, "get_sizelimit [rc=%d]", rc
));
886 * @type: returns encryption type
888 * Cryptoloop is DEPRECATED!
890 * Returns: <0 on error, 0 on success
892 int loopcxt_get_encrypt_type(struct loopdev_cxt
*lc
, uint32_t *type
)
894 struct loop_info64
*lo
= loopcxt_get_info(lc
);
897 /* not provided by sysfs */
900 *type
= lo
->lo_encrypt_type
;
905 DBG(CXT
, ul_debugobj(lc
, "get_encrypt_type [rc=%d]", rc
));
912 * Cryptoloop is DEPRECATED!
914 * Returns: <0 on error, 0 on success
916 const char *loopcxt_get_crypt_name(struct loopdev_cxt
*lc
)
918 struct loop_info64
*lo
= loopcxt_get_info(lc
);
921 return (char *) lo
->lo_crypt_name
;
923 DBG(CXT
, ul_debugobj(lc
, "get_crypt_name failed"));
929 * @devno: returns backing file devno
931 * Returns: <0 on error, 0 on success
933 int loopcxt_get_backing_devno(struct loopdev_cxt
*lc
, dev_t
*devno
)
935 struct loop_info64
*lo
= loopcxt_get_info(lc
);
940 *devno
= lo
->lo_device
;
945 DBG(CXT
, ul_debugobj(lc
, "get_backing_devno [rc=%d]", rc
));
951 * @ino: returns backing file inode
953 * Returns: <0 on error, 0 on success
955 int loopcxt_get_backing_inode(struct loopdev_cxt
*lc
, ino_t
*ino
)
957 struct loop_info64
*lo
= loopcxt_get_info(lc
);
967 DBG(CXT
, ul_debugobj(lc
, "get_backing_inode [rc=%d]", rc
));
972 * Check if the kernel supports partitioned loop devices.
975 * - kernels < 3.2 support partitioned loop devices and PT scanning
976 * only if max_part= module parameter is non-zero
978 * - kernels >= 3.2 always support partitioned loop devices
980 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
982 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
983 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
986 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
988 int loopmod_supports_partscan(void)
993 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
996 f
= fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR
);
999 rc
= fscanf(f
, "%d", &ret
);
1001 return rc
== 1 ? ret
: 0;
1007 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
1008 * scanning is enabled for all loop devices.
1010 int loopcxt_is_partscan(struct loopdev_cxt
*lc
)
1012 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1017 if (ul_path_read_s32(sysfs
, &fl
, "loop/partscan") == 0)
1021 /* old kernels (including kernels without loopN/loop/<flags> directory */
1022 return loopmod_supports_partscan();
1028 * Returns: 1 if the autoclear flags is set.
1030 int loopcxt_is_autoclear(struct loopdev_cxt
*lc
)
1032 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1036 if (ul_path_read_s32(sysfs
, &fl
, "loop/autoclear") == 0)
1040 if (loopcxt_ioctl_enabled(lc
)) {
1041 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1043 return lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
;
1051 * Returns: 1 if the readonly flags is set.
1053 int loopcxt_is_readonly(struct loopdev_cxt
*lc
)
1055 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1059 if (ul_path_read_s32(sysfs
, &fl
, "ro") == 0)
1063 if (loopcxt_ioctl_enabled(lc
)) {
1064 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1066 return lo
->lo_flags
& LO_FLAGS_READ_ONLY
;
1074 * Returns: 1 if the dio flags is set.
1076 int loopcxt_is_dio(struct loopdev_cxt
*lc
)
1078 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1082 if (ul_path_read_s32(sysfs
, &fl
, "loop/dio") == 0)
1085 if (loopcxt_ioctl_enabled(lc
)) {
1086 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1088 return lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
1095 * @st: backing file stat or NULL
1096 * @backing_file: filename
1097 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
1098 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
1099 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
1101 * Returns 1 if the current @lc loopdev is associated with the given backing
1102 * file. Note that the preferred way is to use devno and inode number rather
1103 * than filename. The @backing_file filename is poor solution usable in case
1104 * that you don't have rights to call stat().
1106 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
1108 * Don't forget that old kernels provide very restricted (in size) backing
1109 * filename by LOOP_GET_STAT64 ioctl only.
1111 int loopcxt_is_used(struct loopdev_cxt
*lc
,
1113 const char *backing_file
,
1124 DBG(CXT
, ul_debugobj(lc
, "checking %s vs. %s",
1125 loopcxt_get_device(lc
),
1128 if (st
&& loopcxt_get_backing_inode(lc
, &ino
) == 0 &&
1129 loopcxt_get_backing_devno(lc
, &dev
) == 0) {
1131 if (ino
== st
->st_ino
&& dev
== st
->st_dev
)
1134 /* don't use filename if we have devno and inode */
1138 /* poor man's solution */
1140 char *name
= loopcxt_get_backing_file(lc
);
1141 int rc
= name
&& strcmp(name
, backing_file
) == 0;
1150 if (flags
& LOOPDEV_FL_OFFSET
) {
1153 int rc
= loopcxt_get_offset(lc
, &off
) == 0 && off
== offset
;
1155 if (rc
&& flags
& LOOPDEV_FL_SIZELIMIT
) {
1158 return loopcxt_get_sizelimit(lc
, &sz
) == 0 && sz
== sizelimit
;
1166 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1168 int loopcxt_set_offset(struct loopdev_cxt
*lc
, uint64_t offset
)
1172 lc
->config
.info
.lo_offset
= offset
;
1174 DBG(CXT
, ul_debugobj(lc
, "set offset=%jd", offset
));
1179 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1181 int loopcxt_set_sizelimit(struct loopdev_cxt
*lc
, uint64_t sizelimit
)
1185 lc
->config
.info
.lo_sizelimit
= sizelimit
;
1187 DBG(CXT
, ul_debugobj(lc
, "set sizelimit=%jd", sizelimit
));
1192 * The blocksize will be used by loopcxt_set_device(). For already exiting
1193 * devices use loopcxt_ioctl_blocksize().
1195 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1197 int loopcxt_set_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1201 lc
->blocksize
= blocksize
;
1203 DBG(CXT
, ul_debugobj(lc
, "set blocksize=%jd", blocksize
));
1209 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1211 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1213 * Returns: 0 on success, <0 on error.
1215 int loopcxt_set_flags(struct loopdev_cxt
*lc
, uint32_t flags
)
1219 lc
->config
.info
.lo_flags
= flags
;
1221 DBG(CXT
, ul_debugobj(lc
, "set flags=%u", (unsigned) flags
));
1227 * @refname: reference name (used to overwrite lo_file_name where is backing
1230 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1232 * Returns: 0 on success, <0 on error.
1234 int loopcxt_set_refname(struct loopdev_cxt
*lc
, const char *refname
)
1239 memset(lc
->config
.info
.lo_file_name
, 0, sizeof(lc
->config
.info
.lo_file_name
));
1241 xstrncpy((char *)lc
->config
.info
.lo_file_name
, refname
, LO_NAME_SIZE
);
1243 DBG(CXT
, ul_debugobj(lc
, "set refname=%s", (char *)lc
->config
.info
.lo_file_name
));
1249 * @filename: backing file path (the path will be canonicalized)
1251 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1253 * Returns: 0 on success, <0 on error.
1255 int loopcxt_set_backing_file(struct loopdev_cxt
*lc
, const char *filename
)
1260 lc
->filename
= canonicalize_path(filename
);
1264 if (!lc
->config
.info
.lo_file_name
[0])
1265 loopcxt_set_refname(lc
, lc
->filename
);
1267 DBG(CXT
, ul_debugobj(lc
, "set backing file=%s", lc
->filename
));
1272 * In kernels prior to v3.9, if the offset or sizelimit options
1273 * are used, the block device's size won't be synced automatically.
1274 * blockdev --getsize64 and filesystems will use the backing
1275 * file size until the block device has been re-opened or the
1276 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1278 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1279 * the open file descriptor to the mount system call, we need to use
1280 * the ioctl. Calling losetup directly doesn't have this problem since
1281 * it closes the device when it exits and whatever consumes the device
1282 * next will re-open it, causing the resync.
1284 static int loopcxt_check_size(struct loopdev_cxt
*lc
, int file_fd
)
1286 uint64_t size
, expected_size
;
1290 if (!lc
->config
.info
.lo_offset
&& !lc
->config
.info
.lo_sizelimit
)
1293 if (fstat(file_fd
, &st
)) {
1294 DBG(CXT
, ul_debugobj(lc
, "failed to fstat backing file"));
1297 if (S_ISBLK(st
.st_mode
)) {
1298 if (blkdev_get_size(file_fd
,
1299 (unsigned long long *) &expected_size
)) {
1300 DBG(CXT
, ul_debugobj(lc
, "failed to determine device size"));
1304 expected_size
= st
.st_size
;
1306 if (expected_size
== 0 || expected_size
<= lc
->config
.info
.lo_offset
) {
1307 DBG(CXT
, ul_debugobj(lc
, "failed to determine expected size"));
1308 return 0; /* ignore this error */
1311 if (lc
->config
.info
.lo_offset
> 0)
1312 expected_size
-= lc
->config
.info
.lo_offset
;
1314 if (lc
->config
.info
.lo_sizelimit
> 0 && lc
->config
.info
.lo_sizelimit
< expected_size
)
1315 expected_size
= lc
->config
.info
.lo_sizelimit
;
1317 dev_fd
= loopcxt_get_fd(lc
);
1319 DBG(CXT
, ul_debugobj(lc
, "failed to get loop FD"));
1323 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
)) {
1324 DBG(CXT
, ul_debugobj(lc
, "failed to determine loopdev size"));
1328 /* It's block device, so, align to 512-byte sectors */
1329 if (expected_size
% 512) {
1330 DBG(CXT
, ul_debugobj(lc
, "expected size misaligned to 512-byte sectors"));
1331 expected_size
= (expected_size
>> 9) << 9;
1334 if (expected_size
!= size
) {
1335 DBG(CXT
, ul_debugobj(lc
, "warning: loopdev and expected "
1336 "size mismatch (%ju/%ju)",
1337 size
, expected_size
));
1339 if (loopcxt_ioctl_capacity(lc
)) {
1340 /* ioctl not available */
1341 if (errno
== ENOTTY
|| errno
== EINVAL
)
1346 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
))
1349 if (expected_size
!= size
) {
1351 DBG(CXT
, ul_debugobj(lc
, "failed to set loopdev size, "
1352 "size: %ju, expected: %ju",
1353 size
, expected_size
));
1365 * Associate the current device (see loopcxt_{set,get}_device()) with
1366 * a file (see loopcxt_set_backing_file()).
1368 * The device is initialized read-write by default. If you want read-only
1369 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1370 * flags are ignored and modified according to LO_FLAGS_*.
1372 * If the device is already open by loopcxt_get_fd() then this setup device
1373 * function will re-open the device to fix read/write mode.
1375 * The device is also initialized read-only if the backing file is not
1376 * possible to open read-write (e.g. read-only FS).
1378 * Returns: <0 on error, 0 on success.
1380 int loopcxt_setup_device(struct loopdev_cxt
*lc
)
1382 int file_fd
, dev_fd
;
1383 mode_t flags
= O_CLOEXEC
, mode
= O_RDWR
;
1384 int rc
= -1, cnt
= 0;
1388 if (!lc
|| !*lc
->device
|| !lc
->filename
)
1391 DBG(SETUP
, ul_debugobj(lc
, "device setup requested"));
1394 * Open backing file and device
1396 if (lc
->config
.info
.lo_flags
& LO_FLAGS_READ_ONLY
)
1399 if (lc
->config
.info
.lo_flags
& LO_FLAGS_DIRECT_IO
)
1402 if ((file_fd
= open(lc
->filename
, mode
| flags
)) < 0) {
1403 if (mode
!= O_RDONLY
&& (errno
== EROFS
|| errno
== EACCES
))
1404 file_fd
= open(lc
->filename
, (mode
= O_RDONLY
) | flags
);
1407 DBG(SETUP
, ul_debugobj(lc
, "open backing file failed: %m"));
1411 DBG(SETUP
, ul_debugobj(lc
, "backing file open: OK"));
1413 if (mode
== O_RDONLY
)
1414 lc
->config
.info
.lo_flags
|= LO_FLAGS_READ_ONLY
; /* kernel loopdev mode */
1416 lc
->config
.info
.lo_flags
&= ~LO_FLAGS_READ_ONLY
;
1421 dev_fd
= __loopcxt_get_fd(lc
, mode
);
1422 if (dev_fd
>= 0 || lc
->control_ok
== 0)
1424 if (errno
!= EACCES
&& errno
!= ENOENT
)
1426 /* We have permissions to open /dev/loop-control, but open
1427 * /dev/loopN failed with EACCES, it's probably because udevd
1428 * does not applied chown yet. Let's wait a moment. */
1430 } while (cnt
++ < 16);
1437 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1440 * Atomic way to configure all by one ioctl call
1441 * -- since Linux v5.8-rc1, commit 3448914e8cc550ba792d4ccc74471d1ca4293aae
1443 lc
->config
.fd
= file_fd
;
1444 if (lc
->blocksize
> 0)
1445 lc
->config
.block_size
= lc
->blocksize
;
1447 rc
= repeat_on_eagain( ioctl(dev_fd
, LOOP_CONFIGURE
, &lc
->config
) );
1450 if (errno
!= EINVAL
&& errno
!= ENOTTY
&& errno
!= ENOSYS
) {
1451 DBG(SETUP
, ul_debugobj(lc
, "LOOP_CONFIGURE failed: %m"));
1456 DBG(SETUP
, ul_debugobj(lc
, "LOOP_CONFIGURE: OK"));
1460 * Old deprecated way; first assign backing file FD and then in the
1461 * second step set loop device properties.
1464 if (ioctl(dev_fd
, LOOP_SET_FD
, file_fd
) < 0) {
1467 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD failed: %m"));
1471 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD: OK"));
1473 if (lc
->blocksize
> 0
1474 && (rc
= loopcxt_ioctl_blocksize(lc
, lc
->blocksize
)) < 0) {
1479 if ((rc
= loopcxt_ioctl_status(lc
)) < 0) {
1485 if ((rc
= loopcxt_check_size(lc
, file_fd
)))
1490 memset(&lc
->config
, 0, sizeof(lc
->config
));
1492 lc
->info_failed
= 0;
1494 DBG(SETUP
, ul_debugobj(lc
, "success [rc=0]"));
1499 if (dev_fd
>= 0 && rc
!= -EBUSY
)
1500 ioctl(dev_fd
, LOOP_CLR_FD
, 0);
1504 DBG(SETUP
, ul_debugobj(lc
, "failed [rc=%d]", rc
));
1512 * Update status of the current device (see loopcxt_{set,get}_device()).
1514 * Note that once initialized, kernel accepts only selected changes:
1515 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1516 * For more see linux/drivers/block/loop.c:loop_set_status()
1518 * Returns: <0 on error, 0 on success.
1520 int loopcxt_ioctl_status(struct loopdev_cxt
*lc
)
1525 dev_fd
= loopcxt_get_fd(lc
);
1530 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_STATUS64"));
1532 rc
= repeat_on_eagain( ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->config
.info
) );
1534 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1538 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1542 int loopcxt_ioctl_capacity(struct loopdev_cxt
*lc
)
1544 int rc
, fd
= loopcxt_get_fd(lc
);
1549 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_CAPACITY"));
1551 /* Kernels prior to v2.6.30 don't support this ioctl */
1552 rc
= repeat_on_eagain( ioctl(fd
, LOOP_SET_CAPACITY
, 0) );
1554 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_CAPACITY failed: %m"));
1558 DBG(CXT
, ul_debugobj(lc
, "capacity set"));
1562 int loopcxt_ioctl_dio(struct loopdev_cxt
*lc
, unsigned long use_dio
)
1564 int rc
, fd
= loopcxt_get_fd(lc
);
1569 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_DIRECT_IO"));
1571 /* Kernels prior to v4.4 don't support this ioctl */
1572 rc
= repeat_on_eagain( ioctl(fd
, LOOP_SET_DIRECT_IO
, use_dio
) );
1574 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_DIRECT_IO failed: %m"));
1578 DBG(CXT
, ul_debugobj(lc
, "direct io set"));
1583 * Kernel uses "unsigned long" as ioctl arg, but we use u64 for all sizes to
1584 * keep loopdev internal API simple.
1586 int loopcxt_ioctl_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1588 int rc
, fd
= loopcxt_get_fd(lc
);
1593 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_BLOCK_SIZE"));
1595 rc
= repeat_on_eagain(
1596 ioctl(fd
, LOOP_SET_BLOCK_SIZE
, (unsigned long) blocksize
) );
1598 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_BLOCK_SIZE failed: %m"));
1602 DBG(CXT
, ul_debugobj(lc
, "logical block size set"));
1606 int loopcxt_delete_device(struct loopdev_cxt
*lc
)
1608 int rc
, fd
= loopcxt_get_fd(lc
);
1613 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_CLR_FD"));
1615 rc
= repeat_on_eagain( ioctl(fd
, LOOP_CLR_FD
, 0) );
1617 DBG(CXT
, ul_debugobj(lc
, "LOOP_CLR_FD failed: %m"));
1621 DBG(CXT
, ul_debugobj(lc
, "device removed"));
1625 int loopcxt_add_device(struct loopdev_cxt
*lc
)
1629 const char *p
, *dev
= loopcxt_get_device(lc
);
1634 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
)) {
1639 p
= strrchr(dev
, '/');
1640 if (!p
|| (sscanf(p
, "/loop%d", &nr
) != 1 && sscanf(p
, "/%d", &nr
) != 1)
1644 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1646 DBG(CXT
, ul_debugobj(lc
, "add_device %d", nr
));
1647 rc
= ioctl(ctl
, LOOP_CTL_ADD
, nr
);
1650 lc
->control_ok
= rc
>= 0 ? 1 : 0;
1652 DBG(CXT
, ul_debugobj(lc
, "add_device done [rc=%d]", rc
));
1657 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1658 * kernels we have to check all loop devices to found unused one.
1660 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1662 * Returns: 0 = success, < 0 error
1664 int loopcxt_find_unused(struct loopdev_cxt
*lc
)
1668 DBG(CXT
, ul_debugobj(lc
, "find_unused requested"));
1670 if (lc
->flags
& LOOPDEV_FL_CONTROL
) {
1673 DBG(CXT
, ul_debugobj(lc
, "using loop-control"));
1675 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1677 rc
= ioctl(ctl
, LOOP_CTL_GET_FREE
);
1682 snprintf(name
, sizeof(name
), "loop%d", rc
);
1684 rc
= loopiter_set_device(lc
, name
);
1686 lc
->control_ok
= ctl
>= 0 && rc
== 0 ? 1 : 0;
1689 DBG(CXT
, ul_debugobj(lc
, "find_unused by loop-control [rc=%d]", rc
));
1693 DBG(CXT
, ul_debugobj(lc
, "using loop scan"));
1694 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_FREE
);
1698 rc
= loopcxt_next(lc
);
1699 loopcxt_deinit_iterator(lc
);
1700 DBG(CXT
, ul_debugobj(lc
, "find_unused by scan [rc=%d]", rc
));
1710 * Return: TRUE/FALSE
1712 int loopdev_is_autoclear(const char *device
)
1714 struct loopdev_cxt lc
;
1720 rc
= loopcxt_init(&lc
, 0);
1722 rc
= loopcxt_set_device(&lc
, device
);
1724 rc
= loopcxt_is_autoclear(&lc
);
1726 loopcxt_deinit(&lc
);
1730 char *loopdev_get_backing_file(const char *device
)
1732 struct loopdev_cxt lc
;
1737 if (loopcxt_init(&lc
, 0))
1739 if (loopcxt_set_device(&lc
, device
) == 0)
1740 res
= loopcxt_get_backing_file(&lc
);
1742 loopcxt_deinit(&lc
);
1747 * Returns: TRUE/FALSE
1749 int loopdev_has_backing_file(const char *device
)
1751 char *tmp
= loopdev_get_backing_file(device
);
1761 * Returns: TRUE/FALSE
1763 int loopdev_is_used(const char *device
, const char *filename
,
1764 uint64_t offset
, uint64_t sizelimit
, int flags
)
1766 struct loopdev_cxt lc
;
1770 if (!device
|| !filename
)
1773 rc
= loopcxt_init(&lc
, 0);
1775 rc
= loopcxt_set_device(&lc
, device
);
1779 rc
= !stat(filename
, &st
);
1780 rc
= loopcxt_is_used(&lc
, rc
? &st
: NULL
, filename
, offset
, sizelimit
, flags
);
1782 loopcxt_deinit(&lc
);
1787 * Returns: 0 = success, < 0 error
1789 int loopdev_delete(const char *device
)
1791 struct loopdev_cxt lc
;
1797 rc
= loopcxt_init(&lc
, 0);
1799 rc
= loopcxt_set_device(&lc
, device
);
1801 rc
= loopcxt_delete_device(&lc
);
1802 loopcxt_deinit(&lc
);
1807 * Returns: 0 = success, < 0 error, 1 not found
1809 int loopcxt_find_by_backing_file(struct loopdev_cxt
*lc
, const char *filename
,
1810 uint64_t offset
, uint64_t sizelimit
, int flags
)
1818 hasst
= !stat(filename
, &st
);
1820 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1824 while ((rc
= loopcxt_next(lc
)) == 0) {
1826 if (loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1827 filename
, offset
, sizelimit
, flags
))
1831 loopcxt_deinit_iterator(lc
);
1836 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1838 int loopcxt_find_overlap(struct loopdev_cxt
*lc
, const char *filename
,
1839 uint64_t offset
, uint64_t sizelimit
)
1847 DBG(CXT
, ul_debugobj(lc
, "find_overlap requested"));
1848 hasst
= !stat(filename
, &st
);
1850 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1854 while ((rc
= loopcxt_next(lc
)) == 0) {
1855 uint64_t lc_sizelimit
, lc_offset
;
1857 rc
= loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1858 filename
, offset
, sizelimit
, 0);
1860 * Either the loopdev is unused or we've got an error which can
1861 * happen when we are racing with device autoclear. Just ignore
1867 DBG(CXT
, ul_debugobj(lc
, "found %s backed by %s",
1868 loopcxt_get_device(lc
), filename
));
1870 rc
= loopcxt_get_offset(lc
, &lc_offset
);
1872 DBG(CXT
, ul_debugobj(lc
, "failed to get offset for device %s",
1873 loopcxt_get_device(lc
)));
1876 rc
= loopcxt_get_sizelimit(lc
, &lc_sizelimit
);
1878 DBG(CXT
, ul_debugobj(lc
, "failed to get sizelimit for device %s",
1879 loopcxt_get_device(lc
)));
1884 if (lc_sizelimit
== sizelimit
&& lc_offset
== offset
) {
1885 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s (full match)",
1886 loopcxt_get_device(lc
)));
1892 if (lc_sizelimit
!= 0 && offset
>= lc_offset
+ lc_sizelimit
)
1894 if (sizelimit
!= 0 && offset
+ sizelimit
<= lc_offset
)
1897 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s",
1898 loopcxt_get_device(lc
)));
1904 rc
= 0; /* not found */
1906 loopcxt_deinit_iterator(lc
);
1907 DBG(CXT
, ul_debugobj(lc
, "find_overlap done [rc=%d]", rc
));
1912 * Returns allocated string with device name
1914 char *loopdev_find_by_backing_file(const char *filename
, uint64_t offset
, uint64_t sizelimit
, int flags
)
1916 struct loopdev_cxt lc
;
1922 if (loopcxt_init(&lc
, 0))
1924 if (loopcxt_find_by_backing_file(&lc
, filename
, offset
, sizelimit
, flags
) == 0)
1925 res
= loopcxt_strdup_device(&lc
);
1926 loopcxt_deinit(&lc
);
1932 * Returns number of loop devices associated with @file, if only one loop
1933 * device is associated with the given @filename and @loopdev is not NULL then
1934 * @loopdev returns name of the device.
1936 int loopdev_count_by_backing_file(const char *filename
, char **loopdev
)
1938 struct loopdev_cxt lc
;
1944 rc
= loopcxt_init(&lc
, 0);
1947 if (loopcxt_init_iterator(&lc
, LOOPITER_FL_USED
))
1950 while(loopcxt_next(&lc
) == 0) {
1951 char *backing
= loopcxt_get_backing_file(&lc
);
1953 if (!backing
|| strcmp(backing
, filename
) != 0) {
1959 if (loopdev
&& count
== 0)
1960 *loopdev
= loopcxt_strdup_device(&lc
);
1964 loopcxt_deinit(&lc
);
1966 if (loopdev
&& count
> 1) {
1973 #ifdef TEST_PROGRAM_LOOPDEV
1974 int main(int argc
, char *argv
[])
1979 if (strcmp(argv
[1], "--is-loopdev") == 0 && argc
== 3)
1980 printf("%s: %s\n", argv
[2], is_loopdev(argv
[2]) ? "OK" : "FAIL");
1984 return EXIT_SUCCESS
;
1986 fprintf(stderr
, "usage: %1$s --is-loopdev <dev>\n",
1987 program_invocation_short_name
);
1988 return EXIT_FAILURE
;