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"));
124 memset(&lc
->config
, 0, sizeof(lc
->config
));
128 if (*device
!= '/') {
129 const char *dir
= _PATH_DEV
;
131 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
132 if (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) {
133 if (strlen(device
) < 5)
136 dir
= _PATH_DEV_LOOP
"/"; /* _PATH_DEV uses tailing slash */
138 snprintf(lc
->device
, sizeof(lc
->device
), "%s%s",
141 xstrncpy(lc
->device
, device
, sizeof(lc
->device
));
143 DBG(CXT
, ul_debugobj(lc
, "%s name assigned", device
));
146 ul_unref_path(lc
->sysfs
);
151 int loopcxt_has_device(struct loopdev_cxt
*lc
)
153 return lc
&& *lc
->device
;
158 * @flags: LOOPDEV_FL_* flags
160 * Initialize loop handler.
162 * We have two sets of the flags:
164 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
166 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
168 * Returns: <0 on error, 0 on success.
170 int loopcxt_init(struct loopdev_cxt
*lc
, int flags
)
174 struct loopdev_cxt dummy
= UL_LOOPDEVCXT_EMPTY
;
179 loopdev_init_debug();
180 DBG(CXT
, ul_debugobj(lc
, "initialize context"));
182 memcpy(lc
, &dummy
, sizeof(dummy
));
185 rc
= loopcxt_set_device(lc
, NULL
);
189 if (stat(_PATH_SYS_BLOCK
, &st
) || !S_ISDIR(st
.st_mode
)) {
190 lc
->flags
|= LOOPDEV_FL_NOSYSFS
;
191 lc
->flags
&= ~LOOPDEV_FL_NOIOCTL
;
192 DBG(CXT
, ul_debugobj(lc
, "init: disable /sys usage"));
195 if (!(lc
->flags
& LOOPDEV_FL_NOSYSFS
) &&
196 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
198 * Use only sysfs for basic information about loop devices
200 lc
->flags
|= LOOPDEV_FL_NOIOCTL
;
201 DBG(CXT
, ul_debugobj(lc
, "init: ignore ioctls"));
204 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
) && !stat(_PATH_DEV_LOOPCTL
, &st
)) {
205 lc
->flags
|= LOOPDEV_FL_CONTROL
;
206 DBG(CXT
, ul_debugobj(lc
, "init: loop-control detected "));
215 * Deinitialize loop context
217 void loopcxt_deinit(struct loopdev_cxt
*lc
)
224 DBG(CXT
, ul_debugobj(lc
, "de-initialize"));
229 ignore_result( loopcxt_set_device(lc
, NULL
) );
230 loopcxt_deinit_iterator(lc
);
238 * Returns newly allocated device path.
240 char *loopcxt_strdup_device(struct loopdev_cxt
*lc
)
242 if (!lc
|| !*lc
->device
)
244 return strdup(lc
->device
);
250 * Returns pointer device name in the @lc struct.
252 const char *loopcxt_get_device(struct loopdev_cxt
*lc
)
254 return lc
&& *lc
->device
? lc
->device
: NULL
;
260 * Returns pointer to the sysfs context (see lib/sysfs.c)
262 static struct path_cxt
*loopcxt_get_sysfs(struct loopdev_cxt
*lc
)
264 if (!lc
|| !*lc
->device
|| (lc
->flags
& LOOPDEV_FL_NOSYSFS
))
268 dev_t devno
= sysfs_devname_to_devno(lc
->device
);
270 DBG(CXT
, ul_debugobj(lc
, "sysfs: failed devname to devno"));
274 lc
->sysfs
= ul_new_sysfs_path(devno
, NULL
, NULL
);
276 DBG(CXT
, ul_debugobj(lc
, "sysfs: init failed"));
282 static int __loopcxt_get_fd(struct loopdev_cxt
*lc
, mode_t mode
)
286 if (!lc
|| !*lc
->device
)
289 /* It's okay to return a FD with read-write permissions if someone
290 * asked for read-only, but you shouldn't do the opposite.
292 * (O_RDONLY is a widely usable default.)
294 if (lc
->fd
>= 0 && mode
== O_RDWR
&& lc
->mode
== O_RDONLY
) {
295 DBG(CXT
, ul_debugobj(lc
, "closing already open device (mode mismatch)"));
302 lc
->fd
= open(lc
->device
, lc
->mode
| O_CLOEXEC
);
303 DBG(CXT
, ul_debugobj(lc
, "open %s [%s]: %m", lc
->device
,
304 mode
== O_RDONLY
? "ro" :
305 mode
== O_RDWR
? "rw" : "??"));
307 if (lc
->fd
< 0 && old
>= 0) {
308 /* restore original on error */
319 /* default is read-only file descriptor, it's enough for all ioctls */
320 int loopcxt_get_fd(struct loopdev_cxt
*lc
)
322 return __loopcxt_get_fd(lc
, O_RDONLY
);
325 int loopcxt_set_fd(struct loopdev_cxt
*lc
, int fd
, mode_t mode
)
337 * @flags: LOOPITER_FL_* flags
339 * Iterator can be used to scan list of the free or used loop devices.
341 * Returns: <0 on error, 0 on success
343 int loopcxt_init_iterator(struct loopdev_cxt
*lc
, int flags
)
345 struct loopdev_iter
*iter
;
353 DBG(ITER
, ul_debugobj(iter
, "initialize"));
357 memset(iter
, 0, sizeof(*iter
));
360 iter
->default_check
= 1;
362 if (!lc
->extra_check
) {
364 * Check for /dev/loop/<N> subdirectory
366 if (!(lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) &&
367 stat(_PATH_DEV_LOOP
, &st
) == 0 && S_ISDIR(st
.st_mode
))
368 lc
->flags
|= LOOPDEV_FL_DEVSUBDIR
;
378 * Returns: <0 on error, 0 on success
380 int loopcxt_deinit_iterator(struct loopdev_cxt
*lc
)
382 struct loopdev_iter
*iter
;
388 DBG(ITER
, ul_debugobj(iter
, "de-initialize"));
394 closedir(iter
->sysblock
);
396 memset(iter
, 0, sizeof(*iter
));
401 * Same as loopcxt_set_device, but also checks if the device is
402 * associated with any file.
404 * Returns: <0 on error, 0 on success, 1 device does not match with
405 * LOOPITER_FL_{USED,FREE} flags.
407 static int loopiter_set_device(struct loopdev_cxt
*lc
, const char *device
)
409 int rc
= loopcxt_set_device(lc
, device
);
415 if (!(lc
->iter
.flags
& LOOPITER_FL_USED
) &&
416 !(lc
->iter
.flags
& LOOPITER_FL_FREE
))
417 return 0; /* caller does not care about device status */
419 if (!is_loopdev(lc
->device
)) {
420 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s does not exist", lc
->device
));
424 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s exist", lc
->device
));
426 used
= loopcxt_get_offset(lc
, NULL
) == 0;
428 if ((lc
->iter
.flags
& LOOPITER_FL_USED
) && used
)
431 if ((lc
->iter
.flags
& LOOPITER_FL_FREE
) && !used
)
434 DBG(ITER
, ul_debugobj(&lc
->iter
, "failed to use %s device", lc
->device
));
436 ignore_result( loopcxt_set_device(lc
, NULL
) );
440 static int cmpnum(const void *p1
, const void *p2
)
442 return (((* (const int *) p1
) > (* (const int *) p2
)) -
443 ((* (const int *) p1
) < (* (const int *) p2
)));
447 * The classic scandir() is more expensive and less portable.
448 * We needn't full loop device names -- loop numbers (loop<N>)
451 static int loop_scandir(const char *dirname
, int **ary
, int hasprefix
)
455 unsigned int n
, count
= 0, arylen
= 0;
457 if (!dirname
|| !ary
)
460 DBG(ITER
, ul_debug("scan dir: %s", dirname
));
462 dir
= opendir(dirname
);
468 while((d
= readdir(dir
))) {
469 #ifdef _DIRENT_HAVE_D_TYPE
470 if (d
->d_type
!= DT_BLK
&& d
->d_type
!= DT_UNKNOWN
&&
474 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
479 if (sscanf(d
->d_name
, "loop%u", &n
) != 1)
486 n
= strtol(d
->d_name
, &end
, 10);
487 if (d
->d_name
== end
|| (end
&& *end
) || errno
)
490 if (n
< LOOPDEV_DEFAULT_NNODES
)
491 continue; /* ignore loop<0..7> */
493 if (count
+ 1 > arylen
) {
498 tmp
= reallocarray(*ary
, arylen
, sizeof(int));
511 qsort(*ary
, count
, sizeof(int), cmpnum
);
518 * Set the next *used* loop device according to /proc/partitions.
520 * Loop devices smaller than 512 bytes are invisible for this function.
522 static int loopcxt_next_from_proc(struct loopdev_cxt
*lc
)
524 struct loopdev_iter
*iter
= &lc
->iter
;
527 DBG(ITER
, ul_debugobj(iter
, "scan /proc/partitions"));
530 iter
->proc
= fopen(_PATH_PROC_PARTITIONS
, "r" UL_CLOEXECSTR
);
534 while (fgets(buf
, sizeof(buf
), iter
->proc
)) {
539 if (sscanf(buf
, " %u %*s %*s %128[^\n ]",
540 &m
, name
) != 2 || m
!= LOOPDEV_MAJOR
)
543 DBG(ITER
, ul_debugobj(iter
, "checking %s", name
));
545 if (loopiter_set_device(lc
, name
) == 0)
553 * Set the next *used* loop device according to
554 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
556 * This is preferred method.
558 static int loopcxt_next_from_sysfs(struct loopdev_cxt
*lc
)
560 struct loopdev_iter
*iter
= &lc
->iter
;
564 DBG(ITER
, ul_debugobj(iter
, "scanning /sys/block"));
567 iter
->sysblock
= opendir(_PATH_SYS_BLOCK
);
572 fd
= dirfd(iter
->sysblock
);
574 while ((d
= readdir(iter
->sysblock
))) {
575 char name
[NAME_MAX
+ 18 + 1];
578 DBG(ITER
, ul_debugobj(iter
, "check %s", d
->d_name
));
580 if (strcmp(d
->d_name
, ".") == 0
581 || strcmp(d
->d_name
, "..") == 0
582 || strncmp(d
->d_name
, "loop", 4) != 0)
585 snprintf(name
, sizeof(name
), "%s/loop/backing_file", d
->d_name
);
586 if (fstatat(fd
, name
, &st
, 0) != 0)
589 if (loopiter_set_device(lc
, d
->d_name
) == 0)
597 * @lc: context, has to initialized by loopcxt_init_iterator()
599 * Returns: 0 on success, < 0 on error, 1 at the end of scanning. The details
600 * about the current loop device are available by
601 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
603 int loopcxt_next(struct loopdev_cxt
*lc
)
605 struct loopdev_iter
*iter
;
615 DBG(ITER
, ul_debugobj(iter
, "next"));
617 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
619 if (iter
->flags
& LOOPITER_FL_USED
) {
622 if (loopcxt_sysfs_available(lc
))
623 rc
= loopcxt_next_from_sysfs(lc
);
625 rc
= loopcxt_next_from_proc(lc
);
631 /* B) Classic way, try first eight loop devices (default number
632 * of loop devices). This is enough for 99% of all cases.
634 if (iter
->default_check
) {
635 DBG(ITER
, ul_debugobj(iter
, "next: default check"));
636 for (++iter
->ncur
; iter
->ncur
< LOOPDEV_DEFAULT_NNODES
;
639 snprintf(name
, sizeof(name
), "loop%d", iter
->ncur
);
641 if (loopiter_set_device(lc
, name
) == 0)
644 iter
->default_check
= 0;
647 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
650 DBG(ITER
, ul_debugobj(iter
, "next: scanning /dev"));
651 iter
->nminors
= (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) ?
652 loop_scandir(_PATH_DEV_LOOP
, &iter
->minors
, 0) :
653 loop_scandir(_PATH_DEV
, &iter
->minors
, 1);
656 for (++iter
->ncur
; iter
->ncur
< iter
->nminors
; iter
->ncur
++) {
658 snprintf(name
, sizeof(name
), "loop%d", iter
->minors
[iter
->ncur
]);
660 if (loopiter_set_device(lc
, name
) == 0)
664 loopcxt_deinit_iterator(lc
);
669 * @device: path to device
671 int is_loopdev(const char *device
)
676 if (!device
|| stat(device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
678 else if (major(st
.st_rdev
) == LOOPDEV_MAJOR
)
680 else if (sysfs_devno_is_wholedisk(st
.st_rdev
)) {
681 /* It's possible that kernel creates a device with a different
682 * major number ... check by /sys it's really loop device.
684 char name
[PATH_MAX
], *cn
, *p
= NULL
;
686 snprintf(name
, sizeof(name
), _PATH_SYS_DEVBLOCK
"/%d:%d",
687 major(st
.st_rdev
), minor(st
.st_rdev
));
688 cn
= canonicalize_path(name
);
690 p
= stripoff_last_component(cn
);
691 rc
= p
&& startswith(p
, "loop");
703 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
705 struct loop_info64
*loopcxt_get_info(struct loopdev_cxt
*lc
)
709 if (!lc
|| lc
->info_failed
) {
715 return &lc
->config
.info
;
717 fd
= loopcxt_get_fd(lc
);
721 if (ioctl(fd
, LOOP_GET_STATUS64
, &lc
->config
.info
) == 0) {
724 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 OK"));
725 return &lc
->config
.info
;
729 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 FAILED"));
737 * Returns (allocated) string with path to the file associated
738 * with the current loop device.
740 char *loopcxt_get_backing_file(struct loopdev_cxt
*lc
)
742 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
747 * This is always preferred, the loop_info64
748 * has too small buffer for the filename.
750 ul_path_read_string(sysfs
, &res
, "loop/backing_file");
752 if (!res
&& loopcxt_ioctl_enabled(lc
)) {
753 struct loop_info64
*lo
= loopcxt_get_info(lc
);
756 lo
->lo_file_name
[LO_NAME_SIZE
- 2] = '*';
757 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
758 res
= strdup((char *) lo
->lo_file_name
);
762 DBG(CXT
, ul_debugobj(lc
, "get_backing_file [%s]", res
));
769 * Returns (allocated) string with loop reference. The same as backing file by
772 char *loopcxt_get_refname(struct loopdev_cxt
*lc
)
775 struct loop_info64
*lo
= loopcxt_get_info(lc
);
778 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
779 res
= strdup((char *) lo
->lo_file_name
);
782 DBG(CXT
, ul_debugobj(lc
, "get_refname [%s]", res
));
788 * @offset: returns offset number for the given device
790 * Returns: <0 on error, 0 on success
792 int loopcxt_get_offset(struct loopdev_cxt
*lc
, uint64_t *offset
)
794 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
798 if (ul_path_read_u64(sysfs
, offset
, "loop/offset") == 0)
801 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
802 struct loop_info64
*lo
= loopcxt_get_info(lc
);
805 *offset
= lo
->lo_offset
;
811 DBG(CXT
, ul_debugobj(lc
, "get_offset [rc=%d]", rc
));
817 * @blocksize: returns logical blocksize for the given device
819 * Returns: <0 on error, 0 on success
821 int loopcxt_get_blocksize(struct loopdev_cxt
*lc
, uint64_t *blocksize
)
823 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
827 if (ul_path_read_u64(sysfs
, blocksize
, "queue/logical_block_size") == 0)
830 /* Fallback based on BLKSSZGET ioctl */
832 int fd
= loopcxt_get_fd(lc
);
837 rc
= blkdev_get_sector_size(fd
, &sz
);
844 DBG(CXT
, ul_debugobj(lc
, "get_blocksize [rc=%d]", rc
));
850 * @sizelimit: returns size limit for the given device
852 * Returns: <0 on error, 0 on success
854 int loopcxt_get_sizelimit(struct loopdev_cxt
*lc
, uint64_t *size
)
856 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
860 if (ul_path_read_u64(sysfs
, size
, "loop/sizelimit") == 0)
863 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
864 struct loop_info64
*lo
= loopcxt_get_info(lc
);
867 *size
= lo
->lo_sizelimit
;
873 DBG(CXT
, ul_debugobj(lc
, "get_sizelimit [rc=%d]", rc
));
879 * @devno: returns encryption type
881 * Cryptoloop is DEPRECATED!
883 * Returns: <0 on error, 0 on success
885 int loopcxt_get_encrypt_type(struct loopdev_cxt
*lc
, uint32_t *type
)
887 struct loop_info64
*lo
= loopcxt_get_info(lc
);
890 /* not provided by sysfs */
893 *type
= lo
->lo_encrypt_type
;
898 DBG(CXT
, ul_debugobj(lc
, "get_encrypt_type [rc=%d]", rc
));
904 * @devno: returns crypt name
906 * Cryptoloop is DEPRECATED!
908 * Returns: <0 on error, 0 on success
910 const char *loopcxt_get_crypt_name(struct loopdev_cxt
*lc
)
912 struct loop_info64
*lo
= loopcxt_get_info(lc
);
915 return (char *) lo
->lo_crypt_name
;
917 DBG(CXT
, ul_debugobj(lc
, "get_crypt_name failed"));
923 * @devno: returns backing file devno
925 * Returns: <0 on error, 0 on success
927 int loopcxt_get_backing_devno(struct loopdev_cxt
*lc
, dev_t
*devno
)
929 struct loop_info64
*lo
= loopcxt_get_info(lc
);
934 *devno
= lo
->lo_device
;
939 DBG(CXT
, ul_debugobj(lc
, "get_backing_devno [rc=%d]", rc
));
945 * @ino: returns backing file inode
947 * Returns: <0 on error, 0 on success
949 int loopcxt_get_backing_inode(struct loopdev_cxt
*lc
, ino_t
*ino
)
951 struct loop_info64
*lo
= loopcxt_get_info(lc
);
961 DBG(CXT
, ul_debugobj(lc
, "get_backing_inode [rc=%d]", rc
));
966 * Check if the kernel supports partitioned loop devices.
969 * - kernels < 3.2 support partitioned loop devices and PT scanning
970 * only if max_part= module parameter is non-zero
972 * - kernels >= 3.2 always support partitioned loop devices
974 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
976 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
977 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
980 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
982 int loopmod_supports_partscan(void)
987 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
990 f
= fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR
);
993 rc
= fscanf(f
, "%d", &ret
);
995 return rc
== 1 ? ret
: 0;
1001 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
1002 * scanning is enabled for all loop devices.
1004 int loopcxt_is_partscan(struct loopdev_cxt
*lc
)
1006 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1011 if (ul_path_read_s32(sysfs
, &fl
, "loop/partscan") == 0)
1015 /* old kernels (including kernels without loopN/loop/<flags> directory */
1016 return loopmod_supports_partscan();
1022 * Returns: 1 if the autoclear flags is set.
1024 int loopcxt_is_autoclear(struct loopdev_cxt
*lc
)
1026 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1030 if (ul_path_read_s32(sysfs
, &fl
, "loop/autoclear") == 0)
1034 if (loopcxt_ioctl_enabled(lc
)) {
1035 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1037 return lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
;
1045 * Returns: 1 if the readonly flags is set.
1047 int loopcxt_is_readonly(struct loopdev_cxt
*lc
)
1049 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1053 if (ul_path_read_s32(sysfs
, &fl
, "ro") == 0)
1057 if (loopcxt_ioctl_enabled(lc
)) {
1058 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1060 return lo
->lo_flags
& LO_FLAGS_READ_ONLY
;
1068 * Returns: 1 if the dio flags is set.
1070 int loopcxt_is_dio(struct loopdev_cxt
*lc
)
1072 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1076 if (ul_path_read_s32(sysfs
, &fl
, "loop/dio") == 0)
1079 if (loopcxt_ioctl_enabled(lc
)) {
1080 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1082 return lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
1089 * @st: backing file stat or NULL
1090 * @backing_file: filename
1091 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
1092 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
1093 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
1095 * Returns 1 if the current @lc loopdev is associated with the given backing
1096 * file. Note that the preferred way is to use devno and inode number rather
1097 * than filename. The @backing_file filename is poor solution usable in case
1098 * that you don't have rights to call stat().
1100 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
1102 * Don't forget that old kernels provide very restricted (in size) backing
1103 * filename by LOOP_GET_STAT64 ioctl only.
1105 int loopcxt_is_used(struct loopdev_cxt
*lc
,
1107 const char *backing_file
,
1118 DBG(CXT
, ul_debugobj(lc
, "checking %s vs. %s",
1119 loopcxt_get_device(lc
),
1122 if (st
&& loopcxt_get_backing_inode(lc
, &ino
) == 0 &&
1123 loopcxt_get_backing_devno(lc
, &dev
) == 0) {
1125 if (ino
== st
->st_ino
&& dev
== st
->st_dev
)
1128 /* don't use filename if we have devno and inode */
1132 /* poor man's solution */
1134 char *name
= loopcxt_get_backing_file(lc
);
1135 int rc
= name
&& strcmp(name
, backing_file
) == 0;
1144 if (flags
& LOOPDEV_FL_OFFSET
) {
1147 int rc
= loopcxt_get_offset(lc
, &off
) == 0 && off
== offset
;
1149 if (rc
&& flags
& LOOPDEV_FL_SIZELIMIT
) {
1152 return loopcxt_get_sizelimit(lc
, &sz
) == 0 && sz
== sizelimit
;
1160 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1162 int loopcxt_set_offset(struct loopdev_cxt
*lc
, uint64_t offset
)
1166 lc
->config
.info
.lo_offset
= offset
;
1168 DBG(CXT
, ul_debugobj(lc
, "set offset=%jd", offset
));
1173 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1175 int loopcxt_set_sizelimit(struct loopdev_cxt
*lc
, uint64_t sizelimit
)
1179 lc
->config
.info
.lo_sizelimit
= sizelimit
;
1181 DBG(CXT
, ul_debugobj(lc
, "set sizelimit=%jd", sizelimit
));
1186 * The blocksize will be used by loopcxt_set_device(). For already exiting
1187 * devices use loopcxt_ioctl_blocksize().
1189 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1191 int loopcxt_set_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1195 lc
->blocksize
= blocksize
;
1197 DBG(CXT
, ul_debugobj(lc
, "set blocksize=%jd", blocksize
));
1203 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1205 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1207 * Returns: 0 on success, <0 on error.
1209 int loopcxt_set_flags(struct loopdev_cxt
*lc
, uint32_t flags
)
1213 lc
->config
.info
.lo_flags
= flags
;
1215 DBG(CXT
, ul_debugobj(lc
, "set flags=%u", (unsigned) flags
));
1221 * @refname: reference name (used to overwrite lo_file_name where is backing
1224 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1226 * Returns: 0 on success, <0 on error.
1228 int loopcxt_set_refname(struct loopdev_cxt
*lc
, const char *refname
)
1233 memset(lc
->config
.info
.lo_file_name
, 0, sizeof(lc
->config
.info
.lo_file_name
));
1235 xstrncpy((char *)lc
->config
.info
.lo_file_name
, refname
, LO_NAME_SIZE
);
1237 DBG(CXT
, ul_debugobj(lc
, "set refname=%s", (char *)lc
->config
.info
.lo_file_name
));
1243 * @filename: backing file path (the path will be canonicalized)
1245 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1247 * Returns: 0 on success, <0 on error.
1249 int loopcxt_set_backing_file(struct loopdev_cxt
*lc
, const char *filename
)
1254 lc
->filename
= canonicalize_path(filename
);
1258 if (!lc
->config
.info
.lo_file_name
[0])
1259 loopcxt_set_refname(lc
, lc
->filename
);
1261 DBG(CXT
, ul_debugobj(lc
, "set backing file=%s", lc
->filename
));
1266 * In kernels prior to v3.9, if the offset or sizelimit options
1267 * are used, the block device's size won't be synced automatically.
1268 * blockdev --getsize64 and filesystems will use the backing
1269 * file size until the block device has been re-opened or the
1270 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1272 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1273 * the open file descriptor to the mount system call, we need to use
1274 * the ioctl. Calling losetup directly doesn't have this problem since
1275 * it closes the device when it exits and whatever consumes the device
1276 * next will re-open it, causing the resync.
1278 static int loopcxt_check_size(struct loopdev_cxt
*lc
, int file_fd
)
1280 uint64_t size
, expected_size
;
1284 if (!lc
->config
.info
.lo_offset
&& !lc
->config
.info
.lo_sizelimit
)
1287 if (fstat(file_fd
, &st
)) {
1288 DBG(CXT
, ul_debugobj(lc
, "failed to fstat backing file"));
1291 if (S_ISBLK(st
.st_mode
)) {
1292 if (blkdev_get_size(file_fd
,
1293 (unsigned long long *) &expected_size
)) {
1294 DBG(CXT
, ul_debugobj(lc
, "failed to determine device size"));
1298 expected_size
= st
.st_size
;
1300 if (expected_size
== 0 || expected_size
<= lc
->config
.info
.lo_offset
) {
1301 DBG(CXT
, ul_debugobj(lc
, "failed to determine expected size"));
1302 return 0; /* ignore this error */
1305 if (lc
->config
.info
.lo_offset
> 0)
1306 expected_size
-= lc
->config
.info
.lo_offset
;
1308 if (lc
->config
.info
.lo_sizelimit
> 0 && lc
->config
.info
.lo_sizelimit
< expected_size
)
1309 expected_size
= lc
->config
.info
.lo_sizelimit
;
1311 dev_fd
= loopcxt_get_fd(lc
);
1313 DBG(CXT
, ul_debugobj(lc
, "failed to get loop FD"));
1317 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
)) {
1318 DBG(CXT
, ul_debugobj(lc
, "failed to determine loopdev size"));
1322 /* It's block device, so, align to 512-byte sectors */
1323 if (expected_size
% 512) {
1324 DBG(CXT
, ul_debugobj(lc
, "expected size misaligned to 512-byte sectors"));
1325 expected_size
= (expected_size
>> 9) << 9;
1328 if (expected_size
!= size
) {
1329 DBG(CXT
, ul_debugobj(lc
, "warning: loopdev and expected "
1330 "size mismatch (%ju/%ju)",
1331 size
, expected_size
));
1333 if (loopcxt_ioctl_capacity(lc
)) {
1334 /* ioctl not available */
1335 if (errno
== ENOTTY
|| errno
== EINVAL
)
1340 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
))
1343 if (expected_size
!= size
) {
1345 DBG(CXT
, ul_debugobj(lc
, "failed to set loopdev size, "
1346 "size: %ju, expected: %ju",
1347 size
, expected_size
));
1359 * Associate the current device (see loopcxt_{set,get}_device()) with
1360 * a file (see loopcxt_set_backing_file()).
1362 * The device is initialized read-write by default. If you want read-only
1363 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1364 * flags are ignored and modified according to LO_FLAGS_*.
1366 * If the device is already open by loopcxt_get_fd() then this setup device
1367 * function will re-open the device to fix read/write mode.
1369 * The device is also initialized read-only if the backing file is not
1370 * possible to open read-write (e.g. read-only FS).
1372 * Returns: <0 on error, 0 on success.
1374 int loopcxt_setup_device(struct loopdev_cxt
*lc
)
1376 int file_fd
, dev_fd
;
1377 mode_t flags
= O_CLOEXEC
, mode
= O_RDWR
;
1378 int rc
= -1, cnt
= 0;
1382 if (!lc
|| !*lc
->device
|| !lc
->filename
)
1385 DBG(SETUP
, ul_debugobj(lc
, "device setup requested"));
1388 * Open backing file and device
1390 if (lc
->config
.info
.lo_flags
& LO_FLAGS_READ_ONLY
)
1393 if (lc
->config
.info
.lo_flags
& LO_FLAGS_DIRECT_IO
)
1396 if ((file_fd
= open(lc
->filename
, mode
| flags
)) < 0) {
1397 if (mode
!= O_RDONLY
&& (errno
== EROFS
|| errno
== EACCES
))
1398 file_fd
= open(lc
->filename
, (mode
= O_RDONLY
) | flags
);
1401 DBG(SETUP
, ul_debugobj(lc
, "open backing file failed: %m"));
1405 DBG(SETUP
, ul_debugobj(lc
, "backing file open: OK"));
1407 if (mode
== O_RDONLY
)
1408 lc
->config
.info
.lo_flags
|= LO_FLAGS_READ_ONLY
; /* kernel loopdev mode */
1410 lc
->config
.info
.lo_flags
&= ~LO_FLAGS_READ_ONLY
;
1415 /* For the ioctls, it's enough to use O_RDONLY, but udevd
1416 * monitor devices by inotify, and udevd needs IN_CLOSE_WRITE
1417 * event to trigger probing of the new device.
1419 * The mode used for the device does not have to match the mode
1420 * used for the backing file.
1422 dev_fd
= __loopcxt_get_fd(lc
, O_RDWR
);
1423 if (dev_fd
>= 0 || lc
->control_ok
== 0)
1425 if (errno
!= EACCES
&& errno
!= ENOENT
)
1427 /* We have permissions to open /dev/loop-control, but open
1428 * /dev/loopN failed with EACCES, it's probably because udevd
1429 * does not applied chown yet. Let's wait a moment. */
1431 } while (cnt
++ < 16);
1438 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1441 * Atomic way to configure all by one ioctl call
1442 * -- since Linux v5.8-rc1, commit 3448914e8cc550ba792d4ccc74471d1ca4293aae
1444 lc
->config
.fd
= file_fd
;
1445 if (lc
->blocksize
> 0)
1446 lc
->config
.block_size
= lc
->blocksize
;
1448 rc
= repeat_on_eagain( ioctl(dev_fd
, LOOP_CONFIGURE
, &lc
->config
) );
1451 if (errno
!= EINVAL
&& errno
!= ENOTTY
&& errno
!= ENOSYS
) {
1452 DBG(SETUP
, ul_debugobj(lc
, "LOOP_CONFIGURE failed: %m"));
1457 DBG(SETUP
, ul_debugobj(lc
, "LOOP_CONFIGURE: OK"));
1461 * Old deprecated way; first assign backing file FD and then in the
1462 * second step set loop device properties.
1465 if (ioctl(dev_fd
, LOOP_SET_FD
, file_fd
) < 0) {
1468 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD failed: %m"));
1472 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD: OK"));
1474 if (lc
->blocksize
> 0
1475 && (rc
= loopcxt_ioctl_blocksize(lc
, lc
->blocksize
)) < 0) {
1480 if ((rc
= loopcxt_ioctl_status(lc
)) < 0) {
1486 if ((rc
= loopcxt_check_size(lc
, file_fd
)))
1491 memset(&lc
->config
, 0, sizeof(lc
->config
));
1493 lc
->info_failed
= 0;
1495 DBG(SETUP
, ul_debugobj(lc
, "success [rc=0]"));
1500 if (dev_fd
>= 0 && rc
!= -EBUSY
)
1501 ioctl(dev_fd
, LOOP_CLR_FD
, 0);
1505 DBG(SETUP
, ul_debugobj(lc
, "failed [rc=%d]", rc
));
1513 * Update status of the current device (see loopcxt_{set,get}_device()).
1515 * Note that once initialized, kernel accepts only selected changes:
1516 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1517 * For more see linux/drivers/block/loop.c:loop_set_status()
1519 * Returns: <0 on error, 0 on success.
1521 int loopcxt_ioctl_status(struct loopdev_cxt
*lc
)
1526 dev_fd
= loopcxt_get_fd(lc
);
1531 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_STATUS64"));
1533 rc
= repeat_on_eagain( ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->config
.info
) );
1535 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1539 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1543 int loopcxt_ioctl_capacity(struct loopdev_cxt
*lc
)
1545 int rc
, fd
= loopcxt_get_fd(lc
);
1550 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_CAPACITY"));
1552 /* Kernels prior to v2.6.30 don't support this ioctl */
1553 rc
= repeat_on_eagain( ioctl(fd
, LOOP_SET_CAPACITY
, 0) );
1555 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_CAPACITY failed: %m"));
1559 DBG(CXT
, ul_debugobj(lc
, "capacity set"));
1563 int loopcxt_ioctl_dio(struct loopdev_cxt
*lc
, unsigned long use_dio
)
1565 int rc
, fd
= loopcxt_get_fd(lc
);
1570 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_DIRECT_IO"));
1572 /* Kernels prior to v4.4 don't support this ioctl */
1573 rc
= repeat_on_eagain( ioctl(fd
, LOOP_SET_DIRECT_IO
, use_dio
) );
1575 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_DIRECT_IO failed: %m"));
1579 DBG(CXT
, ul_debugobj(lc
, "direct io set"));
1584 * Kernel uses "unsigned long" as ioctl arg, but we use u64 for all sizes to
1585 * keep loopdev internal API simple.
1587 int loopcxt_ioctl_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1589 int rc
, fd
= loopcxt_get_fd(lc
);
1594 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_BLOCK_SIZE"));
1596 rc
= repeat_on_eagain(
1597 ioctl(fd
, LOOP_SET_BLOCK_SIZE
, (unsigned long) blocksize
) );
1599 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_BLOCK_SIZE failed: %m"));
1603 DBG(CXT
, ul_debugobj(lc
, "logical block size set"));
1607 int loopcxt_delete_device(struct loopdev_cxt
*lc
)
1609 int rc
, fd
= loopcxt_get_fd(lc
);
1614 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_CLR_FD"));
1616 rc
= repeat_on_eagain( ioctl(fd
, LOOP_CLR_FD
, 0) );
1618 DBG(CXT
, ul_debugobj(lc
, "LOOP_CLR_FD failed: %m"));
1622 DBG(CXT
, ul_debugobj(lc
, "device removed"));
1626 int loopcxt_add_device(struct loopdev_cxt
*lc
)
1630 const char *p
, *dev
= loopcxt_get_device(lc
);
1635 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
)) {
1640 p
= strrchr(dev
, '/');
1641 if (!p
|| (sscanf(p
, "/loop%d", &nr
) != 1 && sscanf(p
, "/%d", &nr
) != 1)
1645 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1647 DBG(CXT
, ul_debugobj(lc
, "add_device %d", nr
));
1648 rc
= ioctl(ctl
, LOOP_CTL_ADD
, nr
);
1651 lc
->control_ok
= rc
>= 0 ? 1 : 0;
1653 DBG(CXT
, ul_debugobj(lc
, "add_device done [rc=%d]", rc
));
1658 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1659 * kernels we have to check all loop devices to found unused one.
1661 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1663 * Returns: 0 = success, < 0 error
1665 int loopcxt_find_unused(struct loopdev_cxt
*lc
)
1669 DBG(CXT
, ul_debugobj(lc
, "find_unused requested"));
1671 if (lc
->flags
& LOOPDEV_FL_CONTROL
) {
1674 DBG(CXT
, ul_debugobj(lc
, "using loop-control"));
1676 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1678 rc
= ioctl(ctl
, LOOP_CTL_GET_FREE
);
1683 snprintf(name
, sizeof(name
), "loop%d", rc
);
1685 rc
= loopiter_set_device(lc
, name
);
1687 lc
->control_ok
= ctl
>= 0 && rc
== 0 ? 1 : 0;
1690 DBG(CXT
, ul_debugobj(lc
, "find_unused by loop-control [rc=%d]", rc
));
1694 DBG(CXT
, ul_debugobj(lc
, "using loop scan"));
1695 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_FREE
);
1699 rc
= loopcxt_next(lc
);
1700 loopcxt_deinit_iterator(lc
);
1701 DBG(CXT
, ul_debugobj(lc
, "find_unused by scan [rc=%d]", rc
));
1711 * Return: TRUE/FALSE
1713 int loopdev_is_autoclear(const char *device
)
1715 struct loopdev_cxt lc
;
1721 rc
= loopcxt_init(&lc
, 0);
1723 rc
= loopcxt_set_device(&lc
, device
);
1725 rc
= loopcxt_is_autoclear(&lc
);
1727 loopcxt_deinit(&lc
);
1731 char *loopdev_get_backing_file(const char *device
)
1733 struct loopdev_cxt lc
;
1738 if (loopcxt_init(&lc
, 0))
1740 if (loopcxt_set_device(&lc
, device
) == 0)
1741 res
= loopcxt_get_backing_file(&lc
);
1743 loopcxt_deinit(&lc
);
1748 * Returns: TRUE/FALSE
1750 int loopdev_has_backing_file(const char *device
)
1752 char *tmp
= loopdev_get_backing_file(device
);
1762 * Returns: TRUE/FALSE
1764 int loopdev_is_used(const char *device
, const char *filename
,
1765 uint64_t offset
, uint64_t sizelimit
, int flags
)
1767 struct loopdev_cxt lc
;
1771 if (!device
|| !filename
)
1774 rc
= loopcxt_init(&lc
, 0);
1776 rc
= loopcxt_set_device(&lc
, device
);
1780 rc
= !stat(filename
, &st
);
1781 rc
= loopcxt_is_used(&lc
, rc
? &st
: NULL
, filename
, offset
, sizelimit
, flags
);
1783 loopcxt_deinit(&lc
);
1788 * Returns: 0 = success, < 0 error
1790 int loopdev_delete(const char *device
)
1792 struct loopdev_cxt lc
;
1798 rc
= loopcxt_init(&lc
, 0);
1800 rc
= loopcxt_set_device(&lc
, device
);
1802 rc
= loopcxt_delete_device(&lc
);
1803 loopcxt_deinit(&lc
);
1808 * Returns: 0 = success, < 0 error, 1 not found
1810 int loopcxt_find_by_backing_file(struct loopdev_cxt
*lc
, const char *filename
,
1811 uint64_t offset
, uint64_t sizelimit
, int flags
)
1819 hasst
= !stat(filename
, &st
);
1821 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1825 while ((rc
= loopcxt_next(lc
)) == 0) {
1827 if (loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1828 filename
, offset
, sizelimit
, flags
))
1832 loopcxt_deinit_iterator(lc
);
1837 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1839 int loopcxt_find_overlap(struct loopdev_cxt
*lc
, const char *filename
,
1840 uint64_t offset
, uint64_t sizelimit
)
1848 DBG(CXT
, ul_debugobj(lc
, "find_overlap requested"));
1849 hasst
= !stat(filename
, &st
);
1851 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1855 while ((rc
= loopcxt_next(lc
)) == 0) {
1856 uint64_t lc_sizelimit
, lc_offset
;
1858 rc
= loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1859 filename
, offset
, sizelimit
, 0);
1861 * Either the loopdev is unused or we've got an error which can
1862 * happen when we are racing with device autoclear. Just ignore
1868 DBG(CXT
, ul_debugobj(lc
, "found %s backed by %s",
1869 loopcxt_get_device(lc
), filename
));
1871 rc
= loopcxt_get_offset(lc
, &lc_offset
);
1873 DBG(CXT
, ul_debugobj(lc
, "failed to get offset for device %s",
1874 loopcxt_get_device(lc
)));
1877 rc
= loopcxt_get_sizelimit(lc
, &lc_sizelimit
);
1879 DBG(CXT
, ul_debugobj(lc
, "failed to get sizelimit for device %s",
1880 loopcxt_get_device(lc
)));
1885 if (lc_sizelimit
== sizelimit
&& lc_offset
== offset
) {
1886 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s (full match)",
1887 loopcxt_get_device(lc
)));
1893 if (lc_sizelimit
!= 0 && offset
>= lc_offset
+ lc_sizelimit
)
1895 if (sizelimit
!= 0 && offset
+ sizelimit
<= lc_offset
)
1898 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s",
1899 loopcxt_get_device(lc
)));
1905 rc
= 0; /* not found */
1907 loopcxt_deinit_iterator(lc
);
1908 DBG(CXT
, ul_debugobj(lc
, "find_overlap done [rc=%d]", rc
));
1913 * Returns allocated string with device name
1915 char *loopdev_find_by_backing_file(const char *filename
, uint64_t offset
, uint64_t sizelimit
, int flags
)
1917 struct loopdev_cxt lc
;
1923 if (loopcxt_init(&lc
, 0))
1925 if (loopcxt_find_by_backing_file(&lc
, filename
, offset
, sizelimit
, flags
) == 0)
1926 res
= loopcxt_strdup_device(&lc
);
1927 loopcxt_deinit(&lc
);
1933 * Returns number of loop devices associated with @file, if only one loop
1934 * device is associated with the given @filename and @loopdev is not NULL then
1935 * @loopdev returns name of the device.
1937 int loopdev_count_by_backing_file(const char *filename
, char **loopdev
)
1939 struct loopdev_cxt lc
;
1945 rc
= loopcxt_init(&lc
, 0);
1948 if (loopcxt_init_iterator(&lc
, LOOPITER_FL_USED
))
1951 while(loopcxt_next(&lc
) == 0) {
1952 char *backing
= loopcxt_get_backing_file(&lc
);
1954 if (!backing
|| strcmp(backing
, filename
) != 0) {
1960 if (loopdev
&& count
== 0)
1961 *loopdev
= loopcxt_strdup_device(&lc
);
1965 loopcxt_deinit(&lc
);
1967 if (loopdev
&& count
> 1) {
1974 #ifdef TEST_PROGRAM_LOOPDEV
1975 int main(int argc
, char *argv
[])
1980 if (strcmp(argv
[1], "--is-loopdev") == 0 && argc
== 3)
1981 printf("%s: %s\n", argv
[2], is_loopdev(argv
[2]) ? "OK" : "FAIL");
1985 return EXIT_SUCCESS
;
1987 fprintf(stderr
, "usage: %1$s --is-loopdev <dev>\n",
1988 program_invocation_short_name
);
1989 return EXIT_FAILURE
;