]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/loopdev.c
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"
46 * Debug stuff (based on include/debug.h)
48 static UL_DEBUG_DEFINE_MASK(loopdev
);
49 UL_DEBUG_DEFINE_MASKNAMES(loopdev
) = UL_DEBUG_EMPTY_MASKNAMES
;
51 #define LOOPDEV_DEBUG_INIT (1 << 1)
52 #define LOOPDEV_DEBUG_CXT (1 << 2)
53 #define LOOPDEV_DEBUG_ITER (1 << 3)
54 #define LOOPDEV_DEBUG_SETUP (1 << 4)
55 #define SFDISKPROG_DEBUG_ALL 0xFFFF
57 #define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x)
58 #define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x)
60 static void loopdev_init_debug(void)
62 if (loopdev_debug_mask
)
64 __UL_INIT_DEBUG(loopdev
, LOOPDEV_DEBUG_
, 0, LOOPDEV_DEBUG
);
70 #define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
71 #define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
72 && !loopcxt_ioctl_enabled(_lc)
76 * @device: device name, absolute device path or NULL to reset the current setting
78 * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
79 * names ("loop<N>") are converted to the path (/dev/loop<N> or to
82 * This sets the device name, but does not check if the device exists!
84 * Returns: <0 on error, 0 on success
86 int loopcxt_set_device(struct loopdev_cxt
*lc
, const char *device
)
93 DBG(CXT
, ul_debugobj(lc
, "closing old open fd"));
100 memset(&lc
->info
, 0, sizeof(lc
->info
));
104 if (*device
!= '/') {
105 const char *dir
= _PATH_DEV
;
107 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
108 if (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) {
109 if (strlen(device
) < 5)
112 dir
= _PATH_DEV_LOOP
"/"; /* _PATH_DEV uses tailing slash */
114 snprintf(lc
->device
, sizeof(lc
->device
), "%s%s",
117 strncpy(lc
->device
, device
, sizeof(lc
->device
));
118 lc
->device
[sizeof(lc
->device
) - 1] = '\0';
120 DBG(CXT
, ul_debugobj(lc
, "%s name assigned", device
));
123 sysfs_deinit(&lc
->sysfs
);
127 int loopcxt_has_device(struct loopdev_cxt
*lc
)
129 return lc
&& *lc
->device
;
134 * @flags: LOOPDEV_FL_* flags
136 * Initialize loop handler.
138 * We have two sets of the flags:
140 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
142 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
144 * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
145 * syscall to open loop device. By default is the device open read-only.
147 * The expection is loopcxt_setup_device(), where the device is open read-write
148 * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
150 * Returns: <0 on error, 0 on success.
152 int loopcxt_init(struct loopdev_cxt
*lc
, int flags
)
156 struct loopdev_cxt dummy
= UL_LOOPDEVCXT_EMPTY
;
161 loopdev_init_debug();
162 DBG(CXT
, ul_debugobj(lc
, "initialize context"));
164 memcpy(lc
, &dummy
, sizeof(dummy
));
167 rc
= loopcxt_set_device(lc
, NULL
);
171 if (stat(_PATH_SYS_BLOCK
, &st
) || !S_ISDIR(st
.st_mode
)) {
172 lc
->flags
|= LOOPDEV_FL_NOSYSFS
;
173 lc
->flags
&= ~LOOPDEV_FL_NOIOCTL
;
174 DBG(CXT
, ul_debugobj(lc
, "init: disable /sys usage"));
177 if (!(lc
->flags
& LOOPDEV_FL_NOSYSFS
) &&
178 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
180 * Use only sysfs for basic information about loop devices
182 lc
->flags
|= LOOPDEV_FL_NOIOCTL
;
183 DBG(CXT
, ul_debugobj(lc
, "init: ignore ioctls"));
186 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
) && !stat(_PATH_DEV_LOOPCTL
, &st
)) {
187 lc
->flags
|= LOOPDEV_FL_CONTROL
;
188 DBG(CXT
, ul_debugobj(lc
, "init: loop-control detected "));
197 * Deinitialize loop context
199 void loopcxt_deinit(struct loopdev_cxt
*lc
)
206 DBG(CXT
, ul_debugobj(lc
, "de-initialize"));
211 ignore_result( loopcxt_set_device(lc
, NULL
) );
212 loopcxt_deinit_iterator(lc
);
220 * Returns newly allocated device path.
222 char *loopcxt_strdup_device(struct loopdev_cxt
*lc
)
224 if (!lc
|| !*lc
->device
)
226 return strdup(lc
->device
);
232 * Returns pointer device name in the @lc struct.
234 const char *loopcxt_get_device(struct loopdev_cxt
*lc
)
236 return lc
&& *lc
->device
? lc
->device
: NULL
;
242 * Returns pointer to the sysfs context (see lib/sysfs.c)
244 struct sysfs_cxt
*loopcxt_get_sysfs(struct loopdev_cxt
*lc
)
246 if (!lc
|| !*lc
->device
|| (lc
->flags
& LOOPDEV_FL_NOSYSFS
))
249 if (!lc
->sysfs
.devno
) {
250 dev_t devno
= sysfs_devname_to_devno(lc
->device
, NULL
);
252 DBG(CXT
, ul_debugobj(lc
, "sysfs: failed devname to devno"));
255 if (sysfs_init(&lc
->sysfs
, devno
, NULL
)) {
256 DBG(CXT
, ul_debugobj(lc
, "sysfs: init failed"));
267 * Returns: file descriptor to the open loop device or <0 on error. The mode
268 * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
271 int loopcxt_get_fd(struct loopdev_cxt
*lc
)
273 if (!lc
|| !*lc
->device
)
277 lc
->mode
= lc
->flags
& LOOPDEV_FL_RDWR
? O_RDWR
: O_RDONLY
;
278 lc
->fd
= open(lc
->device
, lc
->mode
| O_CLOEXEC
);
279 DBG(CXT
, ul_debugobj(lc
, "open %s [%s]: %m", lc
->device
,
280 lc
->flags
& LOOPDEV_FL_RDWR
? "rw" : "ro"));
285 int loopcxt_set_fd(struct loopdev_cxt
*lc
, int fd
, int mode
)
297 * @flags: LOOPITER_FL_* flags
299 * Iterator allows to scan list of the free or used loop devices.
301 * Returns: <0 on error, 0 on success
303 int loopcxt_init_iterator(struct loopdev_cxt
*lc
, int flags
)
305 struct loopdev_iter
*iter
;
313 DBG(ITER
, ul_debugobj(iter
, "initialize"));
317 memset(iter
, 0, sizeof(*iter
));
320 iter
->default_check
= 1;
322 if (!lc
->extra_check
) {
324 * Check for /dev/loop/<N> subdirectory
326 if (!(lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) &&
327 stat(_PATH_DEV_LOOP
, &st
) == 0 && S_ISDIR(st
.st_mode
))
328 lc
->flags
|= LOOPDEV_FL_DEVSUBDIR
;
338 * Returns: <0 on error, 0 on success
340 int loopcxt_deinit_iterator(struct loopdev_cxt
*lc
)
342 struct loopdev_iter
*iter
;
348 DBG(ITER
, ul_debugobj(iter
, "de-initialize"));
354 closedir(iter
->sysblock
);
356 memset(iter
, 0, sizeof(*iter
));
361 * Same as loopcxt_set_device, but also checks if the device is
362 * associeted with any file.
364 * Returns: <0 on error, 0 on success, 1 device does not match with
365 * LOOPITER_FL_{USED,FREE} flags.
367 static int loopiter_set_device(struct loopdev_cxt
*lc
, const char *device
)
369 int rc
= loopcxt_set_device(lc
, device
);
375 if (!(lc
->iter
.flags
& LOOPITER_FL_USED
) &&
376 !(lc
->iter
.flags
& LOOPITER_FL_FREE
))
377 return 0; /* caller does not care about device status */
379 if (!is_loopdev(lc
->device
)) {
380 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s does not exist", lc
->device
));
384 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s exist", lc
->device
));
386 used
= loopcxt_get_offset(lc
, NULL
) == 0;
388 if ((lc
->iter
.flags
& LOOPITER_FL_USED
) && used
)
391 if ((lc
->iter
.flags
& LOOPITER_FL_FREE
) && !used
)
394 DBG(ITER
, ul_debugobj(&lc
->iter
, "failed to use %s device", lc
->device
));
396 ignore_result( loopcxt_set_device(lc
, NULL
) );
400 static int cmpnum(const void *p1
, const void *p2
)
402 return (((* (int *) p1
) > (* (int *) p2
)) -
403 ((* (int *) p1
) < (* (int *) p2
)));
407 * The classic scandir() is more expensive and less portable.
408 * We needn't full loop device names -- loop numbers (loop<N>)
411 static int loop_scandir(const char *dirname
, int **ary
, int hasprefix
)
415 unsigned int n
, count
= 0, arylen
= 0;
417 if (!dirname
|| !ary
)
420 DBG(ITER
, ul_debug("scan dir: %s", dirname
));
422 dir
= opendir(dirname
);
428 while((d
= readdir(dir
))) {
429 #ifdef _DIRENT_HAVE_D_TYPE
430 if (d
->d_type
!= DT_BLK
&& d
->d_type
!= DT_UNKNOWN
&&
434 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
439 if (sscanf(d
->d_name
, "loop%u", &n
) != 1)
446 n
= strtol(d
->d_name
, &end
, 10);
447 if (d
->d_name
== end
|| (end
&& *end
) || errno
)
450 if (n
< LOOPDEV_DEFAULT_NNODES
)
451 continue; /* ignore loop<0..7> */
453 if (count
+ 1 > arylen
) {
458 tmp
= realloc(*ary
, arylen
* sizeof(int));
471 qsort(*ary
, count
, sizeof(int), cmpnum
);
478 * Set the next *used* loop device according to /proc/partitions.
480 * Loop devices smaller than 512 bytes are invisible for this function.
482 static int loopcxt_next_from_proc(struct loopdev_cxt
*lc
)
484 struct loopdev_iter
*iter
= &lc
->iter
;
487 DBG(ITER
, ul_debugobj(iter
, "scan /proc/partitions"));
490 iter
->proc
= fopen(_PATH_PROC_PARTITIONS
, "r" UL_CLOEXECSTR
);
494 while (fgets(buf
, sizeof(buf
), iter
->proc
)) {
499 if (sscanf(buf
, " %u %*s %*s %128[^\n ]",
500 &m
, name
) != 2 || m
!= LOOPDEV_MAJOR
)
503 DBG(ITER
, ul_debugobj(iter
, "checking %s", name
));
505 if (loopiter_set_device(lc
, name
) == 0)
513 * Set the next *used* loop device according to
514 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
516 * This is preferred method.
518 static int loopcxt_next_from_sysfs(struct loopdev_cxt
*lc
)
520 struct loopdev_iter
*iter
= &lc
->iter
;
524 DBG(ITER
, ul_debugobj(iter
, "scanning /sys/block"));
527 iter
->sysblock
= opendir(_PATH_SYS_BLOCK
);
532 fd
= dirfd(iter
->sysblock
);
534 while ((d
= readdir(iter
->sysblock
))) {
535 char name
[NAME_MAX
+ 18 + 1];
538 DBG(ITER
, ul_debugobj(iter
, "check %s", d
->d_name
));
540 if (strcmp(d
->d_name
, ".") == 0
541 || strcmp(d
->d_name
, "..") == 0
542 || strncmp(d
->d_name
, "loop", 4) != 0)
545 snprintf(name
, sizeof(name
), "%s/loop/backing_file", d
->d_name
);
546 if (fstatat(fd
, name
, &st
, 0) != 0)
549 if (loopiter_set_device(lc
, d
->d_name
) == 0)
557 * @lc: context, has to initialized by loopcxt_init_iterator()
559 * Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
560 * about the current loop device are available by
561 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
563 int loopcxt_next(struct loopdev_cxt
*lc
)
565 struct loopdev_iter
*iter
;
575 DBG(ITER
, ul_debugobj(iter
, "next"));
577 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
579 if (iter
->flags
& LOOPITER_FL_USED
) {
582 if (loopcxt_sysfs_available(lc
))
583 rc
= loopcxt_next_from_sysfs(lc
);
585 rc
= loopcxt_next_from_proc(lc
);
591 /* B) Classic way, try first eight loop devices (default number
592 * of loop devices). This is enough for 99% of all cases.
594 if (iter
->default_check
) {
595 DBG(ITER
, ul_debugobj(iter
, "next: default check"));
596 for (++iter
->ncur
; iter
->ncur
< LOOPDEV_DEFAULT_NNODES
;
599 snprintf(name
, sizeof(name
), "loop%d", iter
->ncur
);
601 if (loopiter_set_device(lc
, name
) == 0)
604 iter
->default_check
= 0;
607 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
610 DBG(ITER
, ul_debugobj(iter
, "next: scanning /dev"));
611 iter
->nminors
= (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) ?
612 loop_scandir(_PATH_DEV_LOOP
, &iter
->minors
, 0) :
613 loop_scandir(_PATH_DEV
, &iter
->minors
, 1);
616 for (++iter
->ncur
; iter
->ncur
< iter
->nminors
; iter
->ncur
++) {
618 snprintf(name
, sizeof(name
), "loop%d", iter
->minors
[iter
->ncur
]);
620 if (loopiter_set_device(lc
, name
) == 0)
624 loopcxt_deinit_iterator(lc
);
629 * @device: path to device
631 int is_loopdev(const char *device
)
635 if (device
&& stat(device
, &st
) == 0 &&
636 S_ISBLK(st
.st_mode
) &&
637 major(st
.st_rdev
) == LOOPDEV_MAJOR
)
647 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
649 struct loop_info64
*loopcxt_get_info(struct loopdev_cxt
*lc
)
653 if (!lc
|| lc
->info_failed
) {
661 fd
= loopcxt_get_fd(lc
);
665 if (ioctl(fd
, LOOP_GET_STATUS64
, &lc
->info
) == 0) {
668 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 OK"));
673 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 FAILED"));
681 * Returns (allocated) string with path to the file assicieted
682 * with the current loop device.
684 char *loopcxt_get_backing_file(struct loopdev_cxt
*lc
)
686 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
691 * This is always preffered, the loop_info64
692 * has too small buffer for the filename.
694 res
= sysfs_strdup(sysfs
, "loop/backing_file");
696 if (!res
&& loopcxt_ioctl_enabled(lc
)) {
697 struct loop_info64
*lo
= loopcxt_get_info(lc
);
700 lo
->lo_file_name
[LO_NAME_SIZE
- 2] = '*';
701 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
702 res
= strdup((char *) lo
->lo_file_name
);
706 DBG(CXT
, ul_debugobj(lc
, "get_backing_file [%s]", res
));
712 * @offset: returns offset number for the given device
714 * Returns: <0 on error, 0 on success
716 int loopcxt_get_offset(struct loopdev_cxt
*lc
, uint64_t *offset
)
718 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
722 rc
= sysfs_read_u64(sysfs
, "loop/offset", offset
);
724 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
725 struct loop_info64
*lo
= loopcxt_get_info(lc
);
728 *offset
= lo
->lo_offset
;
734 DBG(CXT
, ul_debugobj(lc
, "get_offset [rc=%d]", rc
));
740 * @sizelimit: returns size limit for the given device
742 * Returns: <0 on error, 0 on success
744 int loopcxt_get_sizelimit(struct loopdev_cxt
*lc
, uint64_t *size
)
746 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
750 rc
= sysfs_read_u64(sysfs
, "loop/sizelimit", size
);
752 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
753 struct loop_info64
*lo
= loopcxt_get_info(lc
);
756 *size
= lo
->lo_sizelimit
;
762 DBG(CXT
, ul_debugobj(lc
, "get_sizelimit [rc=%d]", rc
));
768 * @devno: returns encryption type
770 * Cryptoloop is DEPRECATED!
772 * Returns: <0 on error, 0 on success
774 int loopcxt_get_encrypt_type(struct loopdev_cxt
*lc
, uint32_t *type
)
776 struct loop_info64
*lo
= loopcxt_get_info(lc
);
779 /* not provided by sysfs */
782 *type
= lo
->lo_encrypt_type
;
787 DBG(CXT
, ul_debugobj(lc
, "get_encrypt_type [rc=%d]", rc
));
793 * @devno: returns crypt name
795 * Cryptoloop is DEPRECATED!
797 * Returns: <0 on error, 0 on success
799 const char *loopcxt_get_crypt_name(struct loopdev_cxt
*lc
)
801 struct loop_info64
*lo
= loopcxt_get_info(lc
);
804 return (char *) lo
->lo_crypt_name
;
806 DBG(CXT
, ul_debugobj(lc
, "get_crypt_name failed"));
812 * @devno: returns backing file devno
814 * Returns: <0 on error, 0 on success
816 int loopcxt_get_backing_devno(struct loopdev_cxt
*lc
, dev_t
*devno
)
818 struct loop_info64
*lo
= loopcxt_get_info(lc
);
823 *devno
= lo
->lo_device
;
828 DBG(CXT
, ul_debugobj(lc
, "get_backing_devno [rc=%d]", rc
));
834 * @ino: returns backing file inode
836 * Returns: <0 on error, 0 on success
838 int loopcxt_get_backing_inode(struct loopdev_cxt
*lc
, ino_t
*ino
)
840 struct loop_info64
*lo
= loopcxt_get_info(lc
);
850 DBG(CXT
, ul_debugobj(lc
, "get_backing_inode [rc=%d]", rc
));
855 * Check if the kernel supports partitioned loop devices.
858 * - kernels < 3.2 support partitioned loop devices and PT scanning
859 * only if max_part= module parameter is non-zero
861 * - kernels >= 3.2 always support partitioned loop devices
863 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
865 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
866 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
869 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
871 int loopmod_supports_partscan(void)
876 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
879 f
= fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR
);
882 rc
= fscanf(f
, "%d", &ret
);
884 return rc
== 1 ? ret
: 0;
890 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
891 * scanning is enabled for all loop devices.
893 int loopcxt_is_partscan(struct loopdev_cxt
*lc
)
895 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
900 if (sysfs_read_int(sysfs
, "loop/partscan", &fl
) == 0)
904 /* old kernels (including kernels without loopN/loop/<flags> directory */
905 return loopmod_supports_partscan();
911 * Returns: 1 if the autoclear flags is set.
913 int loopcxt_is_autoclear(struct loopdev_cxt
*lc
)
915 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
919 if (sysfs_read_int(sysfs
, "loop/autoclear", &fl
) == 0)
923 if (loopcxt_ioctl_enabled(lc
)) {
924 struct loop_info64
*lo
= loopcxt_get_info(lc
);
926 return lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
;
934 * Returns: 1 if the readonly flags is set.
936 int loopcxt_is_readonly(struct loopdev_cxt
*lc
)
938 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
942 if (sysfs_read_int(sysfs
, "ro", &fl
) == 0)
946 if (loopcxt_ioctl_enabled(lc
)) {
947 struct loop_info64
*lo
= loopcxt_get_info(lc
);
949 return lo
->lo_flags
& LO_FLAGS_READ_ONLY
;
957 * Returns: 1 if the dio flags is set.
959 int loopcxt_is_dio(struct loopdev_cxt
*lc
)
961 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
965 if (sysfs_read_int(sysfs
, "loop/dio", &fl
) == 0)
968 if (loopcxt_ioctl_enabled(lc
)) {
969 struct loop_info64
*lo
= loopcxt_get_info(lc
);
971 return lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
978 * @st: backing file stat or NULL
979 * @backing_file: filename
980 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
981 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
982 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
984 * Returns 1 if the current @lc loopdev is associated with the given backing
985 * file. Note that the preferred way is to use devno and inode number rather
986 * than filename. The @backing_file filename is poor solution usable in case
987 * that you don't have rights to call stat().
989 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
991 * Don't forget that old kernels provide very restricted (in size) backing
992 * filename by LOOP_GET_STAT64 ioctl only.
994 int loopcxt_is_used(struct loopdev_cxt
*lc
,
996 const char *backing_file
,
1007 DBG(CXT
, ul_debugobj(lc
, "checking %s vs. %s",
1008 loopcxt_get_device(lc
),
1011 if (st
&& loopcxt_get_backing_inode(lc
, &ino
) == 0 &&
1012 loopcxt_get_backing_devno(lc
, &dev
) == 0) {
1014 if (ino
== st
->st_ino
&& dev
== st
->st_dev
)
1017 /* don't use filename if we have devno and inode */
1021 /* poor man's solution */
1023 char *name
= loopcxt_get_backing_file(lc
);
1024 int rc
= name
&& strcmp(name
, backing_file
) == 0;
1033 if (flags
& LOOPDEV_FL_OFFSET
) {
1036 int rc
= loopcxt_get_offset(lc
, &off
) == 0 && off
== offset
;
1038 if (rc
&& flags
& LOOPDEV_FL_SIZELIMIT
) {
1041 return loopcxt_get_sizelimit(lc
, &sz
) == 0 && sz
== sizelimit
;
1049 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1051 int loopcxt_set_offset(struct loopdev_cxt
*lc
, uint64_t offset
)
1055 lc
->info
.lo_offset
= offset
;
1057 DBG(CXT
, ul_debugobj(lc
, "set offset=%jd", offset
));
1062 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1064 int loopcxt_set_sizelimit(struct loopdev_cxt
*lc
, uint64_t sizelimit
)
1068 lc
->info
.lo_sizelimit
= sizelimit
;
1070 DBG(CXT
, ul_debugobj(lc
, "set sizelimit=%jd", sizelimit
));
1076 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1078 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1080 * Returns: 0 on success, <0 on error.
1082 int loopcxt_set_flags(struct loopdev_cxt
*lc
, uint32_t flags
)
1086 lc
->info
.lo_flags
= flags
;
1088 DBG(CXT
, ul_debugobj(lc
, "set flags=%u", (unsigned) flags
));
1094 * @filename: backing file path (the path will be canonicalized)
1096 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1098 * Returns: 0 on success, <0 on error.
1100 int loopcxt_set_backing_file(struct loopdev_cxt
*lc
, const char *filename
)
1105 lc
->filename
= canonicalize_path(filename
);
1109 strncpy((char *)lc
->info
.lo_file_name
, lc
->filename
, LO_NAME_SIZE
);
1110 lc
->info
.lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
1112 DBG(CXT
, ul_debugobj(lc
, "set backing file=%s", lc
->info
.lo_file_name
));
1117 * In kernels prior to v3.9, if the offset or sizelimit options
1118 * are used, the block device's size won't be synced automatically.
1119 * blockdev --getsize64 and filesystems will use the backing
1120 * file size until the block device has been re-opened or the
1121 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1123 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1124 * the open file descriptor to the mount system call, we need to use
1125 * the ioctl. Calling losetup directly doesn't have this problem since
1126 * it closes the device when it exits and whatever consumes the device
1127 * next will re-open it, causing the resync.
1129 static int loopcxt_check_size(struct loopdev_cxt
*lc
, int file_fd
)
1131 uint64_t size
, expected_size
;
1135 if (!lc
->info
.lo_offset
&& !lc
->info
.lo_sizelimit
)
1138 if (fstat(file_fd
, &st
)) {
1139 DBG(CXT
, ul_debugobj(lc
, "failed to fstat backing file"));
1142 if (S_ISBLK(st
.st_mode
)) {
1143 if (blkdev_get_size(file_fd
,
1144 (unsigned long long *) &expected_size
)) {
1145 DBG(CXT
, ul_debugobj(lc
, "failed to determine device size"));
1149 expected_size
= st
.st_size
;
1151 if (expected_size
== 0 || expected_size
<= lc
->info
.lo_offset
) {
1152 DBG(CXT
, ul_debugobj(lc
, "failed to determine expected size"));
1153 return 0; /* ignore this error */
1156 if (lc
->info
.lo_offset
> 0)
1157 expected_size
-= lc
->info
.lo_offset
;
1159 if (lc
->info
.lo_sizelimit
> 0 && lc
->info
.lo_sizelimit
< expected_size
)
1160 expected_size
= lc
->info
.lo_sizelimit
;
1162 dev_fd
= loopcxt_get_fd(lc
);
1164 DBG(CXT
, ul_debugobj(lc
, "failed to get loop FD"));
1168 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
)) {
1169 DBG(CXT
, ul_debugobj(lc
, "failed to determine loopdev size"));
1173 /* It's block device, so, align to 512-byte sectors */
1174 if (expected_size
% 512) {
1175 DBG(CXT
, ul_debugobj(lc
, "expected size misaligned to 512-byte sectors"));
1176 expected_size
= (expected_size
>> 9) << 9;
1179 if (expected_size
!= size
) {
1180 DBG(CXT
, ul_debugobj(lc
, "warning: loopdev and expected "
1181 "size mismatch (%ju/%ju)",
1182 size
, expected_size
));
1184 if (loopcxt_set_capacity(lc
)) {
1185 /* ioctl not available */
1186 if (errno
== ENOTTY
|| errno
== EINVAL
)
1191 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
))
1194 if (expected_size
!= size
) {
1196 DBG(CXT
, ul_debugobj(lc
, "failed to set loopdev size, "
1197 "size: %ju, expected: %ju",
1198 size
, expected_size
));
1209 * Associate the current device (see loopcxt_{set,get}_device()) with
1210 * a file (see loopcxt_set_backing_file()).
1212 * The device is initialized read-write by default. If you want read-only
1213 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1214 * flags are ignored and modified according to LO_FLAGS_*.
1216 * If the device is already open by loopcxt_get_fd() then this setup device
1217 * function will re-open the device to fix read/write mode.
1219 * The device is also initialized read-only if the backing file is not
1220 * possible to open read-write (e.g. read-only FS).
1222 * Returns: <0 on error, 0 on success.
1224 int loopcxt_setup_device(struct loopdev_cxt
*lc
)
1226 int file_fd
, dev_fd
, mode
= O_RDWR
, rc
= -1, cnt
= 0;
1229 if (!lc
|| !*lc
->device
|| !lc
->filename
)
1232 DBG(SETUP
, ul_debugobj(lc
, "device setup requested"));
1235 * Open backing file and device
1237 if (lc
->info
.lo_flags
& LO_FLAGS_READ_ONLY
)
1240 if ((file_fd
= open(lc
->filename
, mode
| O_CLOEXEC
)) < 0) {
1241 if (mode
!= O_RDONLY
&& (errno
== EROFS
|| errno
== EACCES
))
1242 file_fd
= open(lc
->filename
, mode
= O_RDONLY
);
1245 DBG(SETUP
, ul_debugobj(lc
, "open backing file failed: %m"));
1249 DBG(SETUP
, ul_debugobj(lc
, "backing file open: OK"));
1251 if (lc
->fd
!= -1 && lc
->mode
!= mode
) {
1252 DBG(SETUP
, ul_debugobj(lc
, "closing already open device (mode mismatch)"));
1258 if (mode
== O_RDONLY
) {
1259 lc
->flags
|= LOOPDEV_FL_RDONLY
; /* open() mode */
1260 lc
->info
.lo_flags
|= LO_FLAGS_READ_ONLY
; /* kernel loopdev mode */
1262 lc
->flags
|= LOOPDEV_FL_RDWR
; /* open() mode */
1263 lc
->info
.lo_flags
&= ~LO_FLAGS_READ_ONLY
;
1264 lc
->flags
&= ~LOOPDEV_FL_RDONLY
;
1269 dev_fd
= loopcxt_get_fd(lc
);
1270 if (dev_fd
>= 0 || lc
->control_ok
== 0)
1272 if (errno
!= EACCES
&& errno
!= ENOENT
)
1274 /* We have permissions to open /dev/loop-control, but open
1275 * /dev/loopN failed with EACCES, it's probably because udevd
1276 * does not applied chown yet. Let's wait a moment. */
1278 } while (cnt
++ < 16);
1285 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1290 if (ioctl(dev_fd
, LOOP_SET_FD
, file_fd
) < 0) {
1293 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD failed: %m"));
1297 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD: OK"));
1299 if (ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->info
)) {
1302 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1306 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1308 if ((rc
= loopcxt_check_size(lc
, file_fd
)))
1313 memset(&lc
->info
, 0, sizeof(lc
->info
));
1315 lc
->info_failed
= 0;
1317 DBG(SETUP
, ul_debugobj(lc
, "success [rc=0]"));
1322 if (dev_fd
>= 0 && rc
!= -EBUSY
)
1323 ioctl(dev_fd
, LOOP_CLR_FD
, 0);
1327 DBG(SETUP
, ul_debugobj(lc
, "failed [rc=%d]", rc
));
1334 * Update status of the current device (see loopcxt_{set,get}_device()).
1336 * Note that once initialized, kernel accepts only selected changes:
1337 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1338 * For more see linux/drivers/block/loop.c:loop_set_status()
1340 * Returns: <0 on error, 0 on success.
1342 int loopcxt_set_status(struct loopdev_cxt
*lc
)
1344 int dev_fd
, rc
= -1;
1347 dev_fd
= loopcxt_get_fd(lc
);
1353 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1355 if (ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->info
)) {
1357 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1361 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1365 int loopcxt_set_capacity(struct loopdev_cxt
*lc
)
1367 int fd
= loopcxt_get_fd(lc
);
1372 /* Kernels prior to v2.6.30 don't support this ioctl */
1373 if (ioctl(fd
, LOOP_SET_CAPACITY
, 0) < 0) {
1375 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_CAPACITY failed: %m"));
1379 DBG(CXT
, ul_debugobj(lc
, "capacity set"));
1383 int loopcxt_set_dio(struct loopdev_cxt
*lc
, unsigned long use_dio
)
1385 int fd
= loopcxt_get_fd(lc
);
1390 /* Kernels prior to v4.4 don't support this ioctl */
1391 if (ioctl(fd
, LOOP_SET_DIRECT_IO
, use_dio
) < 0) {
1393 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_DIRECT_IO failed: %m"));
1397 DBG(CXT
, ul_debugobj(lc
, "direct io set"));
1401 int loopcxt_delete_device(struct loopdev_cxt
*lc
)
1403 int fd
= loopcxt_get_fd(lc
);
1408 if (ioctl(fd
, LOOP_CLR_FD
, 0) < 0) {
1409 DBG(CXT
, ul_debugobj(lc
, "LOOP_CLR_FD failed: %m"));
1413 DBG(CXT
, ul_debugobj(lc
, "device removed"));
1417 int loopcxt_add_device(struct loopdev_cxt
*lc
)
1421 const char *p
, *dev
= loopcxt_get_device(lc
);
1426 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
)) {
1431 p
= strrchr(dev
, '/');
1432 if (!p
|| (sscanf(p
, "/loop%d", &nr
) != 1 && sscanf(p
, "/%d", &nr
) != 1)
1436 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1438 DBG(CXT
, ul_debugobj(lc
, "add_device %d", nr
));
1439 rc
= ioctl(ctl
, LOOP_CTL_ADD
, nr
);
1442 lc
->control_ok
= rc
>= 0 ? 1 : 0;
1444 DBG(CXT
, ul_debugobj(lc
, "add_device done [rc=%d]", rc
));
1449 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1450 * kernels we have to check all loop devices to found unused one.
1452 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1454 int loopcxt_find_unused(struct loopdev_cxt
*lc
)
1458 DBG(CXT
, ul_debugobj(lc
, "find_unused requested"));
1460 if (lc
->flags
& LOOPDEV_FL_CONTROL
) {
1463 DBG(CXT
, ul_debugobj(lc
, "using loop-control"));
1465 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1467 rc
= ioctl(ctl
, LOOP_CTL_GET_FREE
);
1470 snprintf(name
, sizeof(name
), "loop%d", rc
);
1472 rc
= loopiter_set_device(lc
, name
);
1474 lc
->control_ok
= ctl
>= 0 && rc
== 0 ? 1 : 0;
1477 DBG(CXT
, ul_debugobj(lc
, "find_unused by loop-control [rc=%d]", rc
));
1481 DBG(CXT
, ul_debugobj(lc
, "using loop scan"));
1482 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_FREE
);
1486 rc
= loopcxt_next(lc
);
1487 loopcxt_deinit_iterator(lc
);
1488 DBG(CXT
, ul_debugobj(lc
, "find_unused by scan [rc=%d]", rc
));
1496 * Return: TRUE/FALSE
1498 int loopdev_is_autoclear(const char *device
)
1500 struct loopdev_cxt lc
;
1506 rc
= loopcxt_init(&lc
, 0);
1508 rc
= loopcxt_set_device(&lc
, device
);
1510 rc
= loopcxt_is_autoclear(&lc
);
1512 loopcxt_deinit(&lc
);
1516 char *loopdev_get_backing_file(const char *device
)
1518 struct loopdev_cxt lc
;
1523 if (loopcxt_init(&lc
, 0))
1525 if (loopcxt_set_device(&lc
, device
) == 0)
1526 res
= loopcxt_get_backing_file(&lc
);
1528 loopcxt_deinit(&lc
);
1533 * Returns: TRUE/FALSE
1535 int loopdev_is_used(const char *device
, const char *filename
,
1536 uint64_t offset
, uint64_t sizelimit
, int flags
)
1538 struct loopdev_cxt lc
;
1542 if (!device
|| !filename
)
1545 rc
= loopcxt_init(&lc
, 0);
1547 rc
= loopcxt_set_device(&lc
, device
);
1551 rc
= !stat(filename
, &st
);
1552 rc
= loopcxt_is_used(&lc
, rc
? &st
: NULL
, filename
, offset
, sizelimit
, flags
);
1554 loopcxt_deinit(&lc
);
1558 int loopdev_delete(const char *device
)
1560 struct loopdev_cxt lc
;
1566 rc
= loopcxt_init(&lc
, 0);
1568 rc
= loopcxt_set_device(&lc
, device
);
1570 rc
= loopcxt_delete_device(&lc
);
1571 loopcxt_deinit(&lc
);
1576 * Returns: 0 = success, < 0 error, 1 not found
1578 int loopcxt_find_by_backing_file(struct loopdev_cxt
*lc
, const char *filename
,
1579 uint64_t offset
, uint64_t sizelimit
, int flags
)
1587 hasst
= !stat(filename
, &st
);
1589 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1593 while ((rc
= loopcxt_next(lc
)) == 0) {
1595 if (loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1596 filename
, offset
, sizelimit
, flags
))
1600 loopcxt_deinit_iterator(lc
);
1605 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1607 int loopcxt_find_overlap(struct loopdev_cxt
*lc
, const char *filename
,
1608 uint64_t offset
, uint64_t sizelimit
)
1616 DBG(CXT
, ul_debugobj(lc
, "find_overlap requested"));
1617 hasst
= !stat(filename
, &st
);
1619 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1623 while ((rc
= loopcxt_next(lc
)) == 0) {
1624 uint64_t lc_sizelimit
, lc_offset
;
1626 rc
= loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1627 filename
, offset
, sizelimit
, 0);
1629 continue; /* unused */
1633 DBG(CXT
, ul_debugobj(lc
, "found %s backed by %s",
1634 loopcxt_get_device(lc
), filename
));
1636 rc
= loopcxt_get_offset(lc
, &lc_offset
);
1638 DBG(CXT
, ul_debugobj(lc
, "failed to get offset for device %s",
1639 loopcxt_get_device(lc
)));
1642 rc
= loopcxt_get_sizelimit(lc
, &lc_sizelimit
);
1644 DBG(CXT
, ul_debugobj(lc
, "failed to get sizelimit for device %s",
1645 loopcxt_get_device(lc
)));
1650 if (lc_sizelimit
== sizelimit
&& lc_offset
== offset
) {
1651 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s (full match)",
1652 loopcxt_get_device(lc
)));
1658 if (lc_sizelimit
!= 0 && offset
>= lc_offset
+ lc_sizelimit
)
1660 if (sizelimit
!= 0 && offset
+ sizelimit
<= lc_offset
)
1663 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s",
1664 loopcxt_get_device(lc
)));
1670 rc
= 0; /* not found */
1672 loopcxt_deinit_iterator(lc
);
1673 DBG(CXT
, ul_debugobj(lc
, "find_overlap done [rc=%d]", rc
));
1678 * Returns allocated string with device name
1680 char *loopdev_find_by_backing_file(const char *filename
, uint64_t offset
, uint64_t sizelimit
, int flags
)
1682 struct loopdev_cxt lc
;
1688 if (loopcxt_init(&lc
, 0))
1690 if (loopcxt_find_by_backing_file(&lc
, filename
, offset
, sizelimit
, flags
) == 0)
1691 res
= loopcxt_strdup_device(&lc
);
1692 loopcxt_deinit(&lc
);
1698 * Returns number of loop devices associated with @file, if only one loop
1699 * device is associeted with the given @filename and @loopdev is not NULL then
1700 * @loopdev returns name of the device.
1702 int loopdev_count_by_backing_file(const char *filename
, char **loopdev
)
1704 struct loopdev_cxt lc
;
1710 rc
= loopcxt_init(&lc
, 0);
1713 if (loopcxt_init_iterator(&lc
, LOOPITER_FL_USED
))
1716 while(loopcxt_next(&lc
) == 0) {
1717 char *backing
= loopcxt_get_backing_file(&lc
);
1719 if (!backing
|| strcmp(backing
, filename
)) {
1725 if (loopdev
&& count
== 0)
1726 *loopdev
= loopcxt_strdup_device(&lc
);
1730 loopcxt_deinit(&lc
);
1732 if (loopdev
&& count
> 1) {