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 * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
169 * syscall to open loop device. By default is the device open read-only.
171 * The exception is loopcxt_setup_device(), where the device is open read-write
172 * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
174 * Returns: <0 on error, 0 on success.
176 int loopcxt_init(struct loopdev_cxt
*lc
, int flags
)
180 struct loopdev_cxt dummy
= UL_LOOPDEVCXT_EMPTY
;
185 loopdev_init_debug();
186 DBG(CXT
, ul_debugobj(lc
, "initialize context"));
188 memcpy(lc
, &dummy
, sizeof(dummy
));
191 rc
= loopcxt_set_device(lc
, NULL
);
195 if (stat(_PATH_SYS_BLOCK
, &st
) || !S_ISDIR(st
.st_mode
)) {
196 lc
->flags
|= LOOPDEV_FL_NOSYSFS
;
197 lc
->flags
&= ~LOOPDEV_FL_NOIOCTL
;
198 DBG(CXT
, ul_debugobj(lc
, "init: disable /sys usage"));
201 if (!(lc
->flags
& LOOPDEV_FL_NOSYSFS
) &&
202 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
204 * Use only sysfs for basic information about loop devices
206 lc
->flags
|= LOOPDEV_FL_NOIOCTL
;
207 DBG(CXT
, ul_debugobj(lc
, "init: ignore ioctls"));
210 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
) && !stat(_PATH_DEV_LOOPCTL
, &st
)) {
211 lc
->flags
|= LOOPDEV_FL_CONTROL
;
212 DBG(CXT
, ul_debugobj(lc
, "init: loop-control detected "));
221 * Deinitialize loop context
223 void loopcxt_deinit(struct loopdev_cxt
*lc
)
230 DBG(CXT
, ul_debugobj(lc
, "de-initialize"));
235 ignore_result( loopcxt_set_device(lc
, NULL
) );
236 loopcxt_deinit_iterator(lc
);
244 * Returns newly allocated device path.
246 char *loopcxt_strdup_device(struct loopdev_cxt
*lc
)
248 if (!lc
|| !*lc
->device
)
250 return strdup(lc
->device
);
256 * Returns pointer device name in the @lc struct.
258 const char *loopcxt_get_device(struct loopdev_cxt
*lc
)
260 return lc
&& *lc
->device
? lc
->device
: NULL
;
266 * Returns pointer to the sysfs context (see lib/sysfs.c)
268 static struct path_cxt
*loopcxt_get_sysfs(struct loopdev_cxt
*lc
)
270 if (!lc
|| !*lc
->device
|| (lc
->flags
& LOOPDEV_FL_NOSYSFS
))
274 dev_t devno
= sysfs_devname_to_devno(lc
->device
);
276 DBG(CXT
, ul_debugobj(lc
, "sysfs: failed devname to devno"));
280 lc
->sysfs
= ul_new_sysfs_path(devno
, NULL
, NULL
);
282 DBG(CXT
, ul_debugobj(lc
, "sysfs: init failed"));
291 * Returns: file descriptor to the open loop device or <0 on error. The mode
292 * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
295 int loopcxt_get_fd(struct loopdev_cxt
*lc
)
297 if (!lc
|| !*lc
->device
)
301 lc
->mode
= lc
->flags
& LOOPDEV_FL_RDWR
? O_RDWR
: O_RDONLY
;
302 lc
->fd
= open(lc
->device
, lc
->mode
| O_CLOEXEC
);
303 DBG(CXT
, ul_debugobj(lc
, "open %s [%s]: %m", lc
->device
,
304 lc
->flags
& LOOPDEV_FL_RDWR
? "rw" : "ro"));
309 int loopcxt_set_fd(struct loopdev_cxt
*lc
, int fd
, int mode
)
321 * @flags: LOOPITER_FL_* flags
323 * Iterator can be used to scan list of the free or used loop devices.
325 * Returns: <0 on error, 0 on success
327 int loopcxt_init_iterator(struct loopdev_cxt
*lc
, int flags
)
329 struct loopdev_iter
*iter
;
337 DBG(ITER
, ul_debugobj(iter
, "initialize"));
341 memset(iter
, 0, sizeof(*iter
));
344 iter
->default_check
= 1;
346 if (!lc
->extra_check
) {
348 * Check for /dev/loop/<N> subdirectory
350 if (!(lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) &&
351 stat(_PATH_DEV_LOOP
, &st
) == 0 && S_ISDIR(st
.st_mode
))
352 lc
->flags
|= LOOPDEV_FL_DEVSUBDIR
;
362 * Returns: <0 on error, 0 on success
364 int loopcxt_deinit_iterator(struct loopdev_cxt
*lc
)
366 struct loopdev_iter
*iter
;
372 DBG(ITER
, ul_debugobj(iter
, "de-initialize"));
378 closedir(iter
->sysblock
);
380 memset(iter
, 0, sizeof(*iter
));
385 * Same as loopcxt_set_device, but also checks if the device is
386 * associated with any file.
388 * Returns: <0 on error, 0 on success, 1 device does not match with
389 * LOOPITER_FL_{USED,FREE} flags.
391 static int loopiter_set_device(struct loopdev_cxt
*lc
, const char *device
)
393 int rc
= loopcxt_set_device(lc
, device
);
399 if (!(lc
->iter
.flags
& LOOPITER_FL_USED
) &&
400 !(lc
->iter
.flags
& LOOPITER_FL_FREE
))
401 return 0; /* caller does not care about device status */
403 if (!is_loopdev(lc
->device
)) {
404 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s does not exist", lc
->device
));
408 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s exist", lc
->device
));
410 used
= loopcxt_get_offset(lc
, NULL
) == 0;
412 if ((lc
->iter
.flags
& LOOPITER_FL_USED
) && used
)
415 if ((lc
->iter
.flags
& LOOPITER_FL_FREE
) && !used
)
418 DBG(ITER
, ul_debugobj(&lc
->iter
, "failed to use %s device", lc
->device
));
420 ignore_result( loopcxt_set_device(lc
, NULL
) );
424 static int cmpnum(const void *p1
, const void *p2
)
426 return (((* (const int *) p1
) > (* (const int *) p2
)) -
427 ((* (const int *) p1
) < (* (const int *) p2
)));
431 * The classic scandir() is more expensive and less portable.
432 * We needn't full loop device names -- loop numbers (loop<N>)
435 static int loop_scandir(const char *dirname
, int **ary
, int hasprefix
)
439 unsigned int n
, count
= 0, arylen
= 0;
441 if (!dirname
|| !ary
)
444 DBG(ITER
, ul_debug("scan dir: %s", dirname
));
446 dir
= opendir(dirname
);
452 while((d
= readdir(dir
))) {
453 #ifdef _DIRENT_HAVE_D_TYPE
454 if (d
->d_type
!= DT_BLK
&& d
->d_type
!= DT_UNKNOWN
&&
458 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
463 if (sscanf(d
->d_name
, "loop%u", &n
) != 1)
470 n
= strtol(d
->d_name
, &end
, 10);
471 if (d
->d_name
== end
|| (end
&& *end
) || errno
)
474 if (n
< LOOPDEV_DEFAULT_NNODES
)
475 continue; /* ignore loop<0..7> */
477 if (count
+ 1 > arylen
) {
482 tmp
= realloc(*ary
, arylen
* sizeof(int));
495 qsort(*ary
, count
, sizeof(int), cmpnum
);
502 * Set the next *used* loop device according to /proc/partitions.
504 * Loop devices smaller than 512 bytes are invisible for this function.
506 static int loopcxt_next_from_proc(struct loopdev_cxt
*lc
)
508 struct loopdev_iter
*iter
= &lc
->iter
;
511 DBG(ITER
, ul_debugobj(iter
, "scan /proc/partitions"));
514 iter
->proc
= fopen(_PATH_PROC_PARTITIONS
, "r" UL_CLOEXECSTR
);
518 while (fgets(buf
, sizeof(buf
), iter
->proc
)) {
523 if (sscanf(buf
, " %u %*s %*s %128[^\n ]",
524 &m
, name
) != 2 || m
!= LOOPDEV_MAJOR
)
527 DBG(ITER
, ul_debugobj(iter
, "checking %s", name
));
529 if (loopiter_set_device(lc
, name
) == 0)
537 * Set the next *used* loop device according to
538 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
540 * This is preferred method.
542 static int loopcxt_next_from_sysfs(struct loopdev_cxt
*lc
)
544 struct loopdev_iter
*iter
= &lc
->iter
;
548 DBG(ITER
, ul_debugobj(iter
, "scanning /sys/block"));
551 iter
->sysblock
= opendir(_PATH_SYS_BLOCK
);
556 fd
= dirfd(iter
->sysblock
);
558 while ((d
= readdir(iter
->sysblock
))) {
559 char name
[NAME_MAX
+ 18 + 1];
562 DBG(ITER
, ul_debugobj(iter
, "check %s", d
->d_name
));
564 if (strcmp(d
->d_name
, ".") == 0
565 || strcmp(d
->d_name
, "..") == 0
566 || strncmp(d
->d_name
, "loop", 4) != 0)
569 snprintf(name
, sizeof(name
), "%s/loop/backing_file", d
->d_name
);
570 if (fstatat(fd
, name
, &st
, 0) != 0)
573 if (loopiter_set_device(lc
, d
->d_name
) == 0)
581 * @lc: context, has to initialized by loopcxt_init_iterator()
583 * Returns: 0 on success, < 0 on error, 1 at the end of scanning. The details
584 * about the current loop device are available by
585 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
587 int loopcxt_next(struct loopdev_cxt
*lc
)
589 struct loopdev_iter
*iter
;
599 DBG(ITER
, ul_debugobj(iter
, "next"));
601 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
603 if (iter
->flags
& LOOPITER_FL_USED
) {
606 if (loopcxt_sysfs_available(lc
))
607 rc
= loopcxt_next_from_sysfs(lc
);
609 rc
= loopcxt_next_from_proc(lc
);
615 /* B) Classic way, try first eight loop devices (default number
616 * of loop devices). This is enough for 99% of all cases.
618 if (iter
->default_check
) {
619 DBG(ITER
, ul_debugobj(iter
, "next: default check"));
620 for (++iter
->ncur
; iter
->ncur
< LOOPDEV_DEFAULT_NNODES
;
623 snprintf(name
, sizeof(name
), "loop%d", iter
->ncur
);
625 if (loopiter_set_device(lc
, name
) == 0)
628 iter
->default_check
= 0;
631 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
634 DBG(ITER
, ul_debugobj(iter
, "next: scanning /dev"));
635 iter
->nminors
= (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) ?
636 loop_scandir(_PATH_DEV_LOOP
, &iter
->minors
, 0) :
637 loop_scandir(_PATH_DEV
, &iter
->minors
, 1);
640 for (++iter
->ncur
; iter
->ncur
< iter
->nminors
; iter
->ncur
++) {
642 snprintf(name
, sizeof(name
), "loop%d", iter
->minors
[iter
->ncur
]);
644 if (loopiter_set_device(lc
, name
) == 0)
648 loopcxt_deinit_iterator(lc
);
653 * @device: path to device
655 int is_loopdev(const char *device
)
660 if (!device
|| stat(device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
662 else if (major(st
.st_rdev
) == LOOPDEV_MAJOR
)
664 else if (sysfs_devno_is_wholedisk(st
.st_rdev
)) {
665 /* It's possible that kernel creates a device with a different
666 * major number ... check by /sys it's really loop device.
668 char name
[PATH_MAX
], *cn
, *p
= NULL
;
670 snprintf(name
, sizeof(name
), _PATH_SYS_DEVBLOCK
"/%d:%d",
671 major(st
.st_rdev
), minor(st
.st_rdev
));
672 cn
= canonicalize_path(name
);
674 p
= stripoff_last_component(cn
);
675 rc
= p
&& startswith(p
, "loop");
687 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
689 struct loop_info64
*loopcxt_get_info(struct loopdev_cxt
*lc
)
693 if (!lc
|| lc
->info_failed
) {
699 return &lc
->config
.info
;
701 fd
= loopcxt_get_fd(lc
);
705 if (ioctl(fd
, LOOP_GET_STATUS64
, &lc
->config
.info
) == 0) {
708 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 OK"));
709 return &lc
->config
.info
;
713 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 FAILED"));
721 * Returns (allocated) string with path to the file associated
722 * with the current loop device.
724 char *loopcxt_get_backing_file(struct loopdev_cxt
*lc
)
726 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
731 * This is always preferred, the loop_info64
732 * has too small buffer for the filename.
734 ul_path_read_string(sysfs
, &res
, "loop/backing_file");
736 if (!res
&& loopcxt_ioctl_enabled(lc
)) {
737 struct loop_info64
*lo
= loopcxt_get_info(lc
);
740 lo
->lo_file_name
[LO_NAME_SIZE
- 2] = '*';
741 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
742 res
= strdup((char *) lo
->lo_file_name
);
746 DBG(CXT
, ul_debugobj(lc
, "get_backing_file [%s]", res
));
753 * Returns (allocated) string with loop reference. The same as backing file by
756 char *loopcxt_get_refname(struct loopdev_cxt
*lc
)
759 struct loop_info64
*lo
= loopcxt_get_info(lc
);
762 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
763 res
= strdup((char *) lo
->lo_file_name
);
766 DBG(CXT
, ul_debugobj(lc
, "get_refname [%s]", res
));
772 * @offset: returns offset number for the given device
774 * Returns: <0 on error, 0 on success
776 int loopcxt_get_offset(struct loopdev_cxt
*lc
, uint64_t *offset
)
778 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
782 if (ul_path_read_u64(sysfs
, offset
, "loop/offset") == 0)
785 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
786 struct loop_info64
*lo
= loopcxt_get_info(lc
);
789 *offset
= lo
->lo_offset
;
795 DBG(CXT
, ul_debugobj(lc
, "get_offset [rc=%d]", rc
));
801 * @blocksize: returns logical blocksize for the given device
803 * Returns: <0 on error, 0 on success
805 int loopcxt_get_blocksize(struct loopdev_cxt
*lc
, uint64_t *blocksize
)
807 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
811 if (ul_path_read_u64(sysfs
, blocksize
, "queue/logical_block_size") == 0)
814 /* Fallback based on BLKSSZGET ioctl */
816 int fd
= loopcxt_get_fd(lc
);
821 rc
= blkdev_get_sector_size(fd
, &sz
);
828 DBG(CXT
, ul_debugobj(lc
, "get_blocksize [rc=%d]", rc
));
834 * @sizelimit: returns size limit for the given device
836 * Returns: <0 on error, 0 on success
838 int loopcxt_get_sizelimit(struct loopdev_cxt
*lc
, uint64_t *size
)
840 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
844 if (ul_path_read_u64(sysfs
, size
, "loop/sizelimit") == 0)
847 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
848 struct loop_info64
*lo
= loopcxt_get_info(lc
);
851 *size
= lo
->lo_sizelimit
;
857 DBG(CXT
, ul_debugobj(lc
, "get_sizelimit [rc=%d]", rc
));
863 * @devno: returns encryption type
865 * Cryptoloop is DEPRECATED!
867 * Returns: <0 on error, 0 on success
869 int loopcxt_get_encrypt_type(struct loopdev_cxt
*lc
, uint32_t *type
)
871 struct loop_info64
*lo
= loopcxt_get_info(lc
);
874 /* not provided by sysfs */
877 *type
= lo
->lo_encrypt_type
;
882 DBG(CXT
, ul_debugobj(lc
, "get_encrypt_type [rc=%d]", rc
));
888 * @devno: returns crypt name
890 * Cryptoloop is DEPRECATED!
892 * Returns: <0 on error, 0 on success
894 const char *loopcxt_get_crypt_name(struct loopdev_cxt
*lc
)
896 struct loop_info64
*lo
= loopcxt_get_info(lc
);
899 return (char *) lo
->lo_crypt_name
;
901 DBG(CXT
, ul_debugobj(lc
, "get_crypt_name failed"));
907 * @devno: returns backing file devno
909 * Returns: <0 on error, 0 on success
911 int loopcxt_get_backing_devno(struct loopdev_cxt
*lc
, dev_t
*devno
)
913 struct loop_info64
*lo
= loopcxt_get_info(lc
);
918 *devno
= lo
->lo_device
;
923 DBG(CXT
, ul_debugobj(lc
, "get_backing_devno [rc=%d]", rc
));
929 * @ino: returns backing file inode
931 * Returns: <0 on error, 0 on success
933 int loopcxt_get_backing_inode(struct loopdev_cxt
*lc
, ino_t
*ino
)
935 struct loop_info64
*lo
= loopcxt_get_info(lc
);
945 DBG(CXT
, ul_debugobj(lc
, "get_backing_inode [rc=%d]", rc
));
950 * Check if the kernel supports partitioned loop devices.
953 * - kernels < 3.2 support partitioned loop devices and PT scanning
954 * only if max_part= module parameter is non-zero
956 * - kernels >= 3.2 always support partitioned loop devices
958 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
960 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
961 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
964 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
966 int loopmod_supports_partscan(void)
971 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
974 f
= fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR
);
977 rc
= fscanf(f
, "%d", &ret
);
979 return rc
== 1 ? ret
: 0;
985 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
986 * scanning is enabled for all loop devices.
988 int loopcxt_is_partscan(struct loopdev_cxt
*lc
)
990 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
995 if (ul_path_read_s32(sysfs
, &fl
, "loop/partscan") == 0)
999 /* old kernels (including kernels without loopN/loop/<flags> directory */
1000 return loopmod_supports_partscan();
1006 * Returns: 1 if the autoclear flags is set.
1008 int loopcxt_is_autoclear(struct loopdev_cxt
*lc
)
1010 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1014 if (ul_path_read_s32(sysfs
, &fl
, "loop/autoclear") == 0)
1018 if (loopcxt_ioctl_enabled(lc
)) {
1019 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1021 return lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
;
1029 * Returns: 1 if the readonly flags is set.
1031 int loopcxt_is_readonly(struct loopdev_cxt
*lc
)
1033 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1037 if (ul_path_read_s32(sysfs
, &fl
, "ro") == 0)
1041 if (loopcxt_ioctl_enabled(lc
)) {
1042 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1044 return lo
->lo_flags
& LO_FLAGS_READ_ONLY
;
1052 * Returns: 1 if the dio flags is set.
1054 int loopcxt_is_dio(struct loopdev_cxt
*lc
)
1056 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1060 if (ul_path_read_s32(sysfs
, &fl
, "loop/dio") == 0)
1063 if (loopcxt_ioctl_enabled(lc
)) {
1064 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1066 return lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
1073 * @st: backing file stat or NULL
1074 * @backing_file: filename
1075 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
1076 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
1077 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
1079 * Returns 1 if the current @lc loopdev is associated with the given backing
1080 * file. Note that the preferred way is to use devno and inode number rather
1081 * than filename. The @backing_file filename is poor solution usable in case
1082 * that you don't have rights to call stat().
1084 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
1086 * Don't forget that old kernels provide very restricted (in size) backing
1087 * filename by LOOP_GET_STAT64 ioctl only.
1089 int loopcxt_is_used(struct loopdev_cxt
*lc
,
1091 const char *backing_file
,
1102 DBG(CXT
, ul_debugobj(lc
, "checking %s vs. %s",
1103 loopcxt_get_device(lc
),
1106 if (st
&& loopcxt_get_backing_inode(lc
, &ino
) == 0 &&
1107 loopcxt_get_backing_devno(lc
, &dev
) == 0) {
1109 if (ino
== st
->st_ino
&& dev
== st
->st_dev
)
1112 /* don't use filename if we have devno and inode */
1116 /* poor man's solution */
1118 char *name
= loopcxt_get_backing_file(lc
);
1119 int rc
= name
&& strcmp(name
, backing_file
) == 0;
1128 if (flags
& LOOPDEV_FL_OFFSET
) {
1131 int rc
= loopcxt_get_offset(lc
, &off
) == 0 && off
== offset
;
1133 if (rc
&& flags
& LOOPDEV_FL_SIZELIMIT
) {
1136 return loopcxt_get_sizelimit(lc
, &sz
) == 0 && sz
== sizelimit
;
1144 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1146 int loopcxt_set_offset(struct loopdev_cxt
*lc
, uint64_t offset
)
1150 lc
->config
.info
.lo_offset
= offset
;
1152 DBG(CXT
, ul_debugobj(lc
, "set offset=%jd", offset
));
1157 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1159 int loopcxt_set_sizelimit(struct loopdev_cxt
*lc
, uint64_t sizelimit
)
1163 lc
->config
.info
.lo_sizelimit
= sizelimit
;
1165 DBG(CXT
, ul_debugobj(lc
, "set sizelimit=%jd", sizelimit
));
1170 * The blocksize will be used by loopcxt_set_device(). For already exiting
1171 * devices use loopcxt_ioctl_blocksize().
1173 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1175 int loopcxt_set_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1179 lc
->blocksize
= blocksize
;
1181 DBG(CXT
, ul_debugobj(lc
, "set blocksize=%jd", blocksize
));
1187 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1189 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1191 * Returns: 0 on success, <0 on error.
1193 int loopcxt_set_flags(struct loopdev_cxt
*lc
, uint32_t flags
)
1197 lc
->config
.info
.lo_flags
= flags
;
1199 DBG(CXT
, ul_debugobj(lc
, "set flags=%u", (unsigned) flags
));
1205 * @refname: reference name (used to overwrite lo_file_name where is backing
1208 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1210 * Returns: 0 on success, <0 on error.
1212 int loopcxt_set_refname(struct loopdev_cxt
*lc
, const char *refname
)
1217 memset(lc
->config
.info
.lo_file_name
, 0, sizeof(lc
->config
.info
.lo_file_name
));
1219 xstrncpy((char *)lc
->config
.info
.lo_file_name
, refname
, LO_NAME_SIZE
);
1221 DBG(CXT
, ul_debugobj(lc
, "set refname=%s", (char *)lc
->config
.info
.lo_file_name
));
1227 * @filename: backing file path (the path will be canonicalized)
1229 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1231 * Returns: 0 on success, <0 on error.
1233 int loopcxt_set_backing_file(struct loopdev_cxt
*lc
, const char *filename
)
1238 lc
->filename
= canonicalize_path(filename
);
1242 if (!lc
->config
.info
.lo_file_name
[0])
1243 loopcxt_set_refname(lc
, lc
->filename
);
1245 DBG(CXT
, ul_debugobj(lc
, "set backing file=%s", lc
->filename
));
1250 * In kernels prior to v3.9, if the offset or sizelimit options
1251 * are used, the block device's size won't be synced automatically.
1252 * blockdev --getsize64 and filesystems will use the backing
1253 * file size until the block device has been re-opened or the
1254 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1256 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1257 * the open file descriptor to the mount system call, we need to use
1258 * the ioctl. Calling losetup directly doesn't have this problem since
1259 * it closes the device when it exits and whatever consumes the device
1260 * next will re-open it, causing the resync.
1262 static int loopcxt_check_size(struct loopdev_cxt
*lc
, int file_fd
)
1264 uint64_t size
, expected_size
;
1268 if (!lc
->config
.info
.lo_offset
&& !lc
->config
.info
.lo_sizelimit
)
1271 if (fstat(file_fd
, &st
)) {
1272 DBG(CXT
, ul_debugobj(lc
, "failed to fstat backing file"));
1275 if (S_ISBLK(st
.st_mode
)) {
1276 if (blkdev_get_size(file_fd
,
1277 (unsigned long long *) &expected_size
)) {
1278 DBG(CXT
, ul_debugobj(lc
, "failed to determine device size"));
1282 expected_size
= st
.st_size
;
1284 if (expected_size
== 0 || expected_size
<= lc
->config
.info
.lo_offset
) {
1285 DBG(CXT
, ul_debugobj(lc
, "failed to determine expected size"));
1286 return 0; /* ignore this error */
1289 if (lc
->config
.info
.lo_offset
> 0)
1290 expected_size
-= lc
->config
.info
.lo_offset
;
1292 if (lc
->config
.info
.lo_sizelimit
> 0 && lc
->config
.info
.lo_sizelimit
< expected_size
)
1293 expected_size
= lc
->config
.info
.lo_sizelimit
;
1295 dev_fd
= loopcxt_get_fd(lc
);
1297 DBG(CXT
, ul_debugobj(lc
, "failed to get loop FD"));
1301 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
)) {
1302 DBG(CXT
, ul_debugobj(lc
, "failed to determine loopdev size"));
1306 /* It's block device, so, align to 512-byte sectors */
1307 if (expected_size
% 512) {
1308 DBG(CXT
, ul_debugobj(lc
, "expected size misaligned to 512-byte sectors"));
1309 expected_size
= (expected_size
>> 9) << 9;
1312 if (expected_size
!= size
) {
1313 DBG(CXT
, ul_debugobj(lc
, "warning: loopdev and expected "
1314 "size mismatch (%ju/%ju)",
1315 size
, expected_size
));
1317 if (loopcxt_ioctl_capacity(lc
)) {
1318 /* ioctl not available */
1319 if (errno
== ENOTTY
|| errno
== EINVAL
)
1324 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
))
1327 if (expected_size
!= size
) {
1329 DBG(CXT
, ul_debugobj(lc
, "failed to set loopdev size, "
1330 "size: %ju, expected: %ju",
1331 size
, expected_size
));
1343 * Associate the current device (see loopcxt_{set,get}_device()) with
1344 * a file (see loopcxt_set_backing_file()).
1346 * The device is initialized read-write by default. If you want read-only
1347 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1348 * flags are ignored and modified according to LO_FLAGS_*.
1350 * If the device is already open by loopcxt_get_fd() then this setup device
1351 * function will re-open the device to fix read/write mode.
1353 * The device is also initialized read-only if the backing file is not
1354 * possible to open read-write (e.g. read-only FS).
1356 * Returns: <0 on error, 0 on success.
1358 int loopcxt_setup_device(struct loopdev_cxt
*lc
)
1360 int file_fd
, dev_fd
, mode
= O_RDWR
, flags
= O_CLOEXEC
;
1361 int rc
= -1, cnt
= 0;
1365 if (!lc
|| !*lc
->device
|| !lc
->filename
)
1368 DBG(SETUP
, ul_debugobj(lc
, "device setup requested"));
1371 * Open backing file and device
1373 if (lc
->config
.info
.lo_flags
& LO_FLAGS_READ_ONLY
)
1376 if (lc
->config
.info
.lo_flags
& LO_FLAGS_DIRECT_IO
)
1379 if ((file_fd
= open(lc
->filename
, mode
| flags
)) < 0) {
1380 if (mode
!= O_RDONLY
&& (errno
== EROFS
|| errno
== EACCES
))
1381 file_fd
= open(lc
->filename
, (mode
= O_RDONLY
) | flags
);
1384 DBG(SETUP
, ul_debugobj(lc
, "open backing file failed: %m"));
1388 DBG(SETUP
, ul_debugobj(lc
, "backing file open: OK"));
1390 if (lc
->fd
!= -1 && lc
->mode
!= mode
) {
1391 DBG(SETUP
, ul_debugobj(lc
, "closing already open device (mode mismatch)"));
1397 if (mode
== O_RDONLY
) {
1398 lc
->flags
|= LOOPDEV_FL_RDONLY
; /* open() mode */
1399 lc
->config
.info
.lo_flags
|= LO_FLAGS_READ_ONLY
; /* kernel loopdev mode */
1401 lc
->flags
|= LOOPDEV_FL_RDWR
; /* open() mode */
1402 lc
->config
.info
.lo_flags
&= ~LO_FLAGS_READ_ONLY
;
1403 lc
->flags
&= ~LOOPDEV_FL_RDONLY
;
1408 dev_fd
= loopcxt_get_fd(lc
);
1409 if (dev_fd
>= 0 || lc
->control_ok
== 0)
1411 if (errno
!= EACCES
&& errno
!= ENOENT
)
1413 /* We have permissions to open /dev/loop-control, but open
1414 * /dev/loopN failed with EACCES, it's probably because udevd
1415 * does not applied chown yet. Let's wait a moment. */
1417 } while (cnt
++ < 16);
1424 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1427 * Atomic way to configure all by one ioctl call
1428 * -- since Linux v5.8-rc1, commit 3448914e8cc550ba792d4ccc74471d1ca4293aae
1430 lc
->config
.fd
= file_fd
;
1431 if (lc
->blocksize
> 0)
1432 lc
->config
.block_size
= lc
->blocksize
;
1434 rc
= repeat_on_eagain( ioctl(dev_fd
, LOOP_CONFIGURE
, &lc
->config
) );
1437 if (errno
!= EINVAL
&& errno
!= ENOTTY
&& errno
!= ENOSYS
) {
1438 DBG(SETUP
, ul_debugobj(lc
, "LOOP_CONFIGURE failed: %m"));
1443 DBG(SETUP
, ul_debugobj(lc
, "LOOP_CONFIGURE: OK"));
1447 * Old deprecated way; first assign backing file FD and then in the
1448 * second step set loop device properties.
1451 if (ioctl(dev_fd
, LOOP_SET_FD
, file_fd
) < 0) {
1454 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD failed: %m"));
1458 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD: OK"));
1460 if (lc
->blocksize
> 0
1461 && (rc
= loopcxt_ioctl_blocksize(lc
, lc
->blocksize
)) < 0) {
1466 if ((rc
= loopcxt_ioctl_status(lc
)) < 0) {
1472 if ((rc
= loopcxt_check_size(lc
, file_fd
)))
1477 memset(&lc
->config
, 0, sizeof(lc
->config
));
1479 lc
->info_failed
= 0;
1481 DBG(SETUP
, ul_debugobj(lc
, "success [rc=0]"));
1486 if (dev_fd
>= 0 && rc
!= -EBUSY
)
1487 ioctl(dev_fd
, LOOP_CLR_FD
, 0);
1491 DBG(SETUP
, ul_debugobj(lc
, "failed [rc=%d]", rc
));
1499 * Update status of the current device (see loopcxt_{set,get}_device()).
1501 * Note that once initialized, kernel accepts only selected changes:
1502 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1503 * For more see linux/drivers/block/loop.c:loop_set_status()
1505 * Returns: <0 on error, 0 on success.
1507 int loopcxt_ioctl_status(struct loopdev_cxt
*lc
)
1512 dev_fd
= loopcxt_get_fd(lc
);
1517 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_STATUS64"));
1519 rc
= repeat_on_eagain( ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->config
.info
) );
1521 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1525 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1529 int loopcxt_ioctl_capacity(struct loopdev_cxt
*lc
)
1531 int rc
, fd
= loopcxt_get_fd(lc
);
1536 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_CAPACITY"));
1538 /* Kernels prior to v2.6.30 don't support this ioctl */
1539 rc
= repeat_on_eagain( ioctl(fd
, LOOP_SET_CAPACITY
, 0) );
1541 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_CAPACITY failed: %m"));
1545 DBG(CXT
, ul_debugobj(lc
, "capacity set"));
1549 int loopcxt_ioctl_dio(struct loopdev_cxt
*lc
, unsigned long use_dio
)
1551 int rc
, fd
= loopcxt_get_fd(lc
);
1556 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_DIRECT_IO"));
1558 /* Kernels prior to v4.4 don't support this ioctl */
1559 rc
= repeat_on_eagain( ioctl(fd
, LOOP_SET_DIRECT_IO
, use_dio
) );
1561 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_DIRECT_IO failed: %m"));
1565 DBG(CXT
, ul_debugobj(lc
, "direct io set"));
1570 * Kernel uses "unsigned long" as ioctl arg, but we use u64 for all sizes to
1571 * keep loopdev internal API simple.
1573 int loopcxt_ioctl_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1575 int rc
, fd
= loopcxt_get_fd(lc
);
1580 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_BLOCK_SIZE"));
1582 rc
= repeat_on_eagain(
1583 ioctl(fd
, LOOP_SET_BLOCK_SIZE
, (unsigned long) blocksize
) );
1585 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_BLOCK_SIZE failed: %m"));
1589 DBG(CXT
, ul_debugobj(lc
, "logical block size set"));
1593 int loopcxt_delete_device(struct loopdev_cxt
*lc
)
1595 int rc
, fd
= loopcxt_get_fd(lc
);
1600 DBG(SETUP
, ul_debugobj(lc
, "calling LOOP_SET_CLR_FD"));
1602 rc
= repeat_on_eagain( ioctl(fd
, LOOP_CLR_FD
, 0) );
1604 DBG(CXT
, ul_debugobj(lc
, "LOOP_CLR_FD failed: %m"));
1608 DBG(CXT
, ul_debugobj(lc
, "device removed"));
1612 int loopcxt_add_device(struct loopdev_cxt
*lc
)
1616 const char *p
, *dev
= loopcxt_get_device(lc
);
1621 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
)) {
1626 p
= strrchr(dev
, '/');
1627 if (!p
|| (sscanf(p
, "/loop%d", &nr
) != 1 && sscanf(p
, "/%d", &nr
) != 1)
1631 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1633 DBG(CXT
, ul_debugobj(lc
, "add_device %d", nr
));
1634 rc
= ioctl(ctl
, LOOP_CTL_ADD
, nr
);
1637 lc
->control_ok
= rc
>= 0 ? 1 : 0;
1639 DBG(CXT
, ul_debugobj(lc
, "add_device done [rc=%d]", rc
));
1644 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1645 * kernels we have to check all loop devices to found unused one.
1647 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1649 * Returns: 0 = success, < 0 error
1651 int loopcxt_find_unused(struct loopdev_cxt
*lc
)
1655 DBG(CXT
, ul_debugobj(lc
, "find_unused requested"));
1657 if (lc
->flags
& LOOPDEV_FL_CONTROL
) {
1660 DBG(CXT
, ul_debugobj(lc
, "using loop-control"));
1662 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1664 rc
= ioctl(ctl
, LOOP_CTL_GET_FREE
);
1669 snprintf(name
, sizeof(name
), "loop%d", rc
);
1671 rc
= loopiter_set_device(lc
, name
);
1673 lc
->control_ok
= ctl
>= 0 && rc
== 0 ? 1 : 0;
1676 DBG(CXT
, ul_debugobj(lc
, "find_unused by loop-control [rc=%d]", rc
));
1680 DBG(CXT
, ul_debugobj(lc
, "using loop scan"));
1681 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_FREE
);
1685 rc
= loopcxt_next(lc
);
1686 loopcxt_deinit_iterator(lc
);
1687 DBG(CXT
, ul_debugobj(lc
, "find_unused by scan [rc=%d]", rc
));
1697 * Return: TRUE/FALSE
1699 int loopdev_is_autoclear(const char *device
)
1701 struct loopdev_cxt lc
;
1707 rc
= loopcxt_init(&lc
, 0);
1709 rc
= loopcxt_set_device(&lc
, device
);
1711 rc
= loopcxt_is_autoclear(&lc
);
1713 loopcxt_deinit(&lc
);
1717 char *loopdev_get_backing_file(const char *device
)
1719 struct loopdev_cxt lc
;
1724 if (loopcxt_init(&lc
, 0))
1726 if (loopcxt_set_device(&lc
, device
) == 0)
1727 res
= loopcxt_get_backing_file(&lc
);
1729 loopcxt_deinit(&lc
);
1734 * Returns: TRUE/FALSE
1736 int loopdev_has_backing_file(const char *device
)
1738 char *tmp
= loopdev_get_backing_file(device
);
1748 * Returns: TRUE/FALSE
1750 int loopdev_is_used(const char *device
, const char *filename
,
1751 uint64_t offset
, uint64_t sizelimit
, int flags
)
1753 struct loopdev_cxt lc
;
1757 if (!device
|| !filename
)
1760 rc
= loopcxt_init(&lc
, 0);
1762 rc
= loopcxt_set_device(&lc
, device
);
1766 rc
= !stat(filename
, &st
);
1767 rc
= loopcxt_is_used(&lc
, rc
? &st
: NULL
, filename
, offset
, sizelimit
, flags
);
1769 loopcxt_deinit(&lc
);
1774 * Returns: 0 = success, < 0 error
1776 int loopdev_delete(const char *device
)
1778 struct loopdev_cxt lc
;
1784 rc
= loopcxt_init(&lc
, 0);
1786 rc
= loopcxt_set_device(&lc
, device
);
1788 rc
= loopcxt_delete_device(&lc
);
1789 loopcxt_deinit(&lc
);
1794 * Returns: 0 = success, < 0 error, 1 not found
1796 int loopcxt_find_by_backing_file(struct loopdev_cxt
*lc
, const char *filename
,
1797 uint64_t offset
, uint64_t sizelimit
, int flags
)
1805 hasst
= !stat(filename
, &st
);
1807 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1811 while ((rc
= loopcxt_next(lc
)) == 0) {
1813 if (loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1814 filename
, offset
, sizelimit
, flags
))
1818 loopcxt_deinit_iterator(lc
);
1823 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1825 int loopcxt_find_overlap(struct loopdev_cxt
*lc
, const char *filename
,
1826 uint64_t offset
, uint64_t sizelimit
)
1834 DBG(CXT
, ul_debugobj(lc
, "find_overlap requested"));
1835 hasst
= !stat(filename
, &st
);
1837 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1841 while ((rc
= loopcxt_next(lc
)) == 0) {
1842 uint64_t lc_sizelimit
, lc_offset
;
1844 rc
= loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1845 filename
, offset
, sizelimit
, 0);
1847 * Either the loopdev is unused or we've got an error which can
1848 * happen when we are racing with device autoclear. Just ignore
1854 DBG(CXT
, ul_debugobj(lc
, "found %s backed by %s",
1855 loopcxt_get_device(lc
), filename
));
1857 rc
= loopcxt_get_offset(lc
, &lc_offset
);
1859 DBG(CXT
, ul_debugobj(lc
, "failed to get offset for device %s",
1860 loopcxt_get_device(lc
)));
1863 rc
= loopcxt_get_sizelimit(lc
, &lc_sizelimit
);
1865 DBG(CXT
, ul_debugobj(lc
, "failed to get sizelimit for device %s",
1866 loopcxt_get_device(lc
)));
1871 if (lc_sizelimit
== sizelimit
&& lc_offset
== offset
) {
1872 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s (full match)",
1873 loopcxt_get_device(lc
)));
1879 if (lc_sizelimit
!= 0 && offset
>= lc_offset
+ lc_sizelimit
)
1881 if (sizelimit
!= 0 && offset
+ sizelimit
<= lc_offset
)
1884 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s",
1885 loopcxt_get_device(lc
)));
1891 rc
= 0; /* not found */
1893 loopcxt_deinit_iterator(lc
);
1894 DBG(CXT
, ul_debugobj(lc
, "find_overlap done [rc=%d]", rc
));
1899 * Returns allocated string with device name
1901 char *loopdev_find_by_backing_file(const char *filename
, uint64_t offset
, uint64_t sizelimit
, int flags
)
1903 struct loopdev_cxt lc
;
1909 if (loopcxt_init(&lc
, 0))
1911 if (loopcxt_find_by_backing_file(&lc
, filename
, offset
, sizelimit
, flags
) == 0)
1912 res
= loopcxt_strdup_device(&lc
);
1913 loopcxt_deinit(&lc
);
1919 * Returns number of loop devices associated with @file, if only one loop
1920 * device is associated with the given @filename and @loopdev is not NULL then
1921 * @loopdev returns name of the device.
1923 int loopdev_count_by_backing_file(const char *filename
, char **loopdev
)
1925 struct loopdev_cxt lc
;
1931 rc
= loopcxt_init(&lc
, 0);
1934 if (loopcxt_init_iterator(&lc
, LOOPITER_FL_USED
))
1937 while(loopcxt_next(&lc
) == 0) {
1938 char *backing
= loopcxt_get_backing_file(&lc
);
1940 if (!backing
|| strcmp(backing
, filename
) != 0) {
1946 if (loopdev
&& count
== 0)
1947 *loopdev
= loopcxt_strdup_device(&lc
);
1951 loopcxt_deinit(&lc
);
1953 if (loopdev
&& count
> 1) {
1960 #ifdef TEST_PROGRAM_LOOPDEV
1961 int main(int argc
, char *argv
[])
1966 if (strcmp(argv
[1], "--is-loopdev") == 0 && argc
== 3)
1967 printf("%s: %s\n", argv
[2], is_loopdev(argv
[2]) ? "OK" : "FAIL");
1971 return EXIT_SUCCESS
;
1973 fprintf(stderr
, "usage: %1$s --is-loopdev <dev>\n",
1974 program_invocation_short_name
);
1975 return EXIT_FAILURE
;