]>
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)
56 #define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x)
57 #define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x)
59 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(loopdev)
62 static void loopdev_init_debug(void)
64 if (loopdev_debug_mask
)
66 __UL_INIT_DEBUG_FROM_ENV(loopdev
, LOOPDEV_DEBUG_
, 0, LOOPDEV_DEBUG
);
72 #define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
73 #define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
74 && !loopcxt_ioctl_enabled(_lc)
78 * @device: device name, absolute device path or NULL to reset the current setting
80 * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
81 * names ("loop<N>") are converted to the path (/dev/loop<N> or to
84 * This sets the device name, but does not check if the device exists!
86 * Returns: <0 on error, 0 on success
88 int loopcxt_set_device(struct loopdev_cxt
*lc
, const char *device
)
95 DBG(CXT
, ul_debugobj(lc
, "closing old open fd"));
103 memset(&lc
->info
, 0, sizeof(lc
->info
));
107 if (*device
!= '/') {
108 const char *dir
= _PATH_DEV
;
110 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
111 if (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) {
112 if (strlen(device
) < 5)
115 dir
= _PATH_DEV_LOOP
"/"; /* _PATH_DEV uses tailing slash */
117 snprintf(lc
->device
, sizeof(lc
->device
), "%s%s",
120 xstrncpy(lc
->device
, device
, sizeof(lc
->device
));
122 DBG(CXT
, ul_debugobj(lc
, "%s name assigned", device
));
125 ul_unref_path(lc
->sysfs
);
130 int loopcxt_has_device(struct loopdev_cxt
*lc
)
132 return lc
&& *lc
->device
;
137 * @flags: LOOPDEV_FL_* flags
139 * Initialize loop handler.
141 * We have two sets of the flags:
143 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
145 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
147 * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
148 * syscall to open loop device. By default is the device open read-only.
150 * The exception is loopcxt_setup_device(), where the device is open read-write
151 * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
153 * Returns: <0 on error, 0 on success.
155 int loopcxt_init(struct loopdev_cxt
*lc
, int flags
)
159 struct loopdev_cxt dummy
= UL_LOOPDEVCXT_EMPTY
;
164 loopdev_init_debug();
165 DBG(CXT
, ul_debugobj(lc
, "initialize context"));
167 memcpy(lc
, &dummy
, sizeof(dummy
));
170 rc
= loopcxt_set_device(lc
, NULL
);
174 if (stat(_PATH_SYS_BLOCK
, &st
) || !S_ISDIR(st
.st_mode
)) {
175 lc
->flags
|= LOOPDEV_FL_NOSYSFS
;
176 lc
->flags
&= ~LOOPDEV_FL_NOIOCTL
;
177 DBG(CXT
, ul_debugobj(lc
, "init: disable /sys usage"));
180 if (!(lc
->flags
& LOOPDEV_FL_NOSYSFS
) &&
181 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
183 * Use only sysfs for basic information about loop devices
185 lc
->flags
|= LOOPDEV_FL_NOIOCTL
;
186 DBG(CXT
, ul_debugobj(lc
, "init: ignore ioctls"));
189 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
) && !stat(_PATH_DEV_LOOPCTL
, &st
)) {
190 lc
->flags
|= LOOPDEV_FL_CONTROL
;
191 DBG(CXT
, ul_debugobj(lc
, "init: loop-control detected "));
200 * Deinitialize loop context
202 void loopcxt_deinit(struct loopdev_cxt
*lc
)
209 DBG(CXT
, ul_debugobj(lc
, "de-initialize"));
214 ignore_result( loopcxt_set_device(lc
, NULL
) );
215 loopcxt_deinit_iterator(lc
);
223 * Returns newly allocated device path.
225 char *loopcxt_strdup_device(struct loopdev_cxt
*lc
)
227 if (!lc
|| !*lc
->device
)
229 return strdup(lc
->device
);
235 * Returns pointer device name in the @lc struct.
237 const char *loopcxt_get_device(struct loopdev_cxt
*lc
)
239 return lc
&& *lc
->device
? lc
->device
: NULL
;
245 * Returns pointer to the sysfs context (see lib/sysfs.c)
247 static struct path_cxt
*loopcxt_get_sysfs(struct loopdev_cxt
*lc
)
249 if (!lc
|| !*lc
->device
|| (lc
->flags
& LOOPDEV_FL_NOSYSFS
))
253 dev_t devno
= sysfs_devname_to_devno(lc
->device
);
255 DBG(CXT
, ul_debugobj(lc
, "sysfs: failed devname to devno"));
259 lc
->sysfs
= ul_new_sysfs_path(devno
, NULL
, NULL
);
261 DBG(CXT
, ul_debugobj(lc
, "sysfs: init failed"));
270 * Returns: file descriptor to the open loop device or <0 on error. The mode
271 * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
274 int loopcxt_get_fd(struct loopdev_cxt
*lc
)
276 if (!lc
|| !*lc
->device
)
280 lc
->mode
= lc
->flags
& LOOPDEV_FL_RDWR
? O_RDWR
: O_RDONLY
;
281 lc
->fd
= open(lc
->device
, lc
->mode
| O_CLOEXEC
);
282 DBG(CXT
, ul_debugobj(lc
, "open %s [%s]: %m", lc
->device
,
283 lc
->flags
& LOOPDEV_FL_RDWR
? "rw" : "ro"));
288 int loopcxt_set_fd(struct loopdev_cxt
*lc
, int fd
, int mode
)
300 * @flags: LOOPITER_FL_* flags
302 * Iterator allows to scan list of the free or used loop devices.
304 * Returns: <0 on error, 0 on success
306 int loopcxt_init_iterator(struct loopdev_cxt
*lc
, int flags
)
308 struct loopdev_iter
*iter
;
316 DBG(ITER
, ul_debugobj(iter
, "initialize"));
320 memset(iter
, 0, sizeof(*iter
));
323 iter
->default_check
= 1;
325 if (!lc
->extra_check
) {
327 * Check for /dev/loop/<N> subdirectory
329 if (!(lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) &&
330 stat(_PATH_DEV_LOOP
, &st
) == 0 && S_ISDIR(st
.st_mode
))
331 lc
->flags
|= LOOPDEV_FL_DEVSUBDIR
;
341 * Returns: <0 on error, 0 on success
343 int loopcxt_deinit_iterator(struct loopdev_cxt
*lc
)
345 struct loopdev_iter
*iter
;
351 DBG(ITER
, ul_debugobj(iter
, "de-initialize"));
357 closedir(iter
->sysblock
);
359 memset(iter
, 0, sizeof(*iter
));
364 * Same as loopcxt_set_device, but also checks if the device is
365 * associated with any file.
367 * Returns: <0 on error, 0 on success, 1 device does not match with
368 * LOOPITER_FL_{USED,FREE} flags.
370 static int loopiter_set_device(struct loopdev_cxt
*lc
, const char *device
)
372 int rc
= loopcxt_set_device(lc
, device
);
378 if (!(lc
->iter
.flags
& LOOPITER_FL_USED
) &&
379 !(lc
->iter
.flags
& LOOPITER_FL_FREE
))
380 return 0; /* caller does not care about device status */
382 if (!is_loopdev(lc
->device
)) {
383 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s does not exist", lc
->device
));
387 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s exist", lc
->device
));
389 used
= loopcxt_get_offset(lc
, NULL
) == 0;
391 if ((lc
->iter
.flags
& LOOPITER_FL_USED
) && used
)
394 if ((lc
->iter
.flags
& LOOPITER_FL_FREE
) && !used
)
397 DBG(ITER
, ul_debugobj(&lc
->iter
, "failed to use %s device", lc
->device
));
399 ignore_result( loopcxt_set_device(lc
, NULL
) );
403 static int cmpnum(const void *p1
, const void *p2
)
405 return (((* (const int *) p1
) > (* (const int *) p2
)) -
406 ((* (const int *) p1
) < (* (const int *) p2
)));
410 * The classic scandir() is more expensive and less portable.
411 * We needn't full loop device names -- loop numbers (loop<N>)
414 static int loop_scandir(const char *dirname
, int **ary
, int hasprefix
)
418 unsigned int n
, count
= 0, arylen
= 0;
420 if (!dirname
|| !ary
)
423 DBG(ITER
, ul_debug("scan dir: %s", dirname
));
425 dir
= opendir(dirname
);
431 while((d
= readdir(dir
))) {
432 #ifdef _DIRENT_HAVE_D_TYPE
433 if (d
->d_type
!= DT_BLK
&& d
->d_type
!= DT_UNKNOWN
&&
437 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
442 if (sscanf(d
->d_name
, "loop%u", &n
) != 1)
449 n
= strtol(d
->d_name
, &end
, 10);
450 if (d
->d_name
== end
|| (end
&& *end
) || errno
)
453 if (n
< LOOPDEV_DEFAULT_NNODES
)
454 continue; /* ignore loop<0..7> */
456 if (count
+ 1 > arylen
) {
461 tmp
= realloc(*ary
, arylen
* sizeof(int));
474 qsort(*ary
, count
, sizeof(int), cmpnum
);
481 * Set the next *used* loop device according to /proc/partitions.
483 * Loop devices smaller than 512 bytes are invisible for this function.
485 static int loopcxt_next_from_proc(struct loopdev_cxt
*lc
)
487 struct loopdev_iter
*iter
= &lc
->iter
;
490 DBG(ITER
, ul_debugobj(iter
, "scan /proc/partitions"));
493 iter
->proc
= fopen(_PATH_PROC_PARTITIONS
, "r" UL_CLOEXECSTR
);
497 while (fgets(buf
, sizeof(buf
), iter
->proc
)) {
502 if (sscanf(buf
, " %u %*s %*s %128[^\n ]",
503 &m
, name
) != 2 || m
!= LOOPDEV_MAJOR
)
506 DBG(ITER
, ul_debugobj(iter
, "checking %s", name
));
508 if (loopiter_set_device(lc
, name
) == 0)
516 * Set the next *used* loop device according to
517 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
519 * This is preferred method.
521 static int loopcxt_next_from_sysfs(struct loopdev_cxt
*lc
)
523 struct loopdev_iter
*iter
= &lc
->iter
;
527 DBG(ITER
, ul_debugobj(iter
, "scanning /sys/block"));
530 iter
->sysblock
= opendir(_PATH_SYS_BLOCK
);
535 fd
= dirfd(iter
->sysblock
);
537 while ((d
= readdir(iter
->sysblock
))) {
538 char name
[NAME_MAX
+ 18 + 1];
541 DBG(ITER
, ul_debugobj(iter
, "check %s", d
->d_name
));
543 if (strcmp(d
->d_name
, ".") == 0
544 || strcmp(d
->d_name
, "..") == 0
545 || strncmp(d
->d_name
, "loop", 4) != 0)
548 snprintf(name
, sizeof(name
), "%s/loop/backing_file", d
->d_name
);
549 if (fstatat(fd
, name
, &st
, 0) != 0)
552 if (loopiter_set_device(lc
, d
->d_name
) == 0)
560 * @lc: context, has to initialized by loopcxt_init_iterator()
562 * Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
563 * about the current loop device are available by
564 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
566 int loopcxt_next(struct loopdev_cxt
*lc
)
568 struct loopdev_iter
*iter
;
578 DBG(ITER
, ul_debugobj(iter
, "next"));
580 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
582 if (iter
->flags
& LOOPITER_FL_USED
) {
585 if (loopcxt_sysfs_available(lc
))
586 rc
= loopcxt_next_from_sysfs(lc
);
588 rc
= loopcxt_next_from_proc(lc
);
594 /* B) Classic way, try first eight loop devices (default number
595 * of loop devices). This is enough for 99% of all cases.
597 if (iter
->default_check
) {
598 DBG(ITER
, ul_debugobj(iter
, "next: default check"));
599 for (++iter
->ncur
; iter
->ncur
< LOOPDEV_DEFAULT_NNODES
;
602 snprintf(name
, sizeof(name
), "loop%d", iter
->ncur
);
604 if (loopiter_set_device(lc
, name
) == 0)
607 iter
->default_check
= 0;
610 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
613 DBG(ITER
, ul_debugobj(iter
, "next: scanning /dev"));
614 iter
->nminors
= (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) ?
615 loop_scandir(_PATH_DEV_LOOP
, &iter
->minors
, 0) :
616 loop_scandir(_PATH_DEV
, &iter
->minors
, 1);
619 for (++iter
->ncur
; iter
->ncur
< iter
->nminors
; iter
->ncur
++) {
621 snprintf(name
, sizeof(name
), "loop%d", iter
->minors
[iter
->ncur
]);
623 if (loopiter_set_device(lc
, name
) == 0)
627 loopcxt_deinit_iterator(lc
);
632 * @device: path to device
634 int is_loopdev(const char *device
)
638 if (device
&& stat(device
, &st
) == 0 &&
639 S_ISBLK(st
.st_mode
) &&
640 major(st
.st_rdev
) == LOOPDEV_MAJOR
)
650 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
652 struct loop_info64
*loopcxt_get_info(struct loopdev_cxt
*lc
)
656 if (!lc
|| lc
->info_failed
) {
664 fd
= loopcxt_get_fd(lc
);
668 if (ioctl(fd
, LOOP_GET_STATUS64
, &lc
->info
) == 0) {
671 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 OK"));
676 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 FAILED"));
684 * Returns (allocated) string with path to the file assicieted
685 * with the current loop device.
687 char *loopcxt_get_backing_file(struct loopdev_cxt
*lc
)
689 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
694 * This is always preferred, the loop_info64
695 * has too small buffer for the filename.
697 ul_path_read_string(sysfs
, &res
, "loop/backing_file");
699 if (!res
&& loopcxt_ioctl_enabled(lc
)) {
700 struct loop_info64
*lo
= loopcxt_get_info(lc
);
703 lo
->lo_file_name
[LO_NAME_SIZE
- 2] = '*';
704 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
705 res
= strdup((char *) lo
->lo_file_name
);
709 DBG(CXT
, ul_debugobj(lc
, "get_backing_file [%s]", res
));
715 * @offset: returns offset number for the given device
717 * Returns: <0 on error, 0 on success
719 int loopcxt_get_offset(struct loopdev_cxt
*lc
, uint64_t *offset
)
721 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
725 rc
= ul_path_read_u64(sysfs
, offset
, "loop/offset");
727 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
728 struct loop_info64
*lo
= loopcxt_get_info(lc
);
731 *offset
= lo
->lo_offset
;
737 DBG(CXT
, ul_debugobj(lc
, "get_offset [rc=%d]", rc
));
743 * @blocksize: returns logical blocksize for the given device
745 * Returns: <0 on error, 0 on success
747 int loopcxt_get_blocksize(struct loopdev_cxt
*lc
, uint64_t *blocksize
)
749 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
753 rc
= ul_path_read_u64(sysfs
, blocksize
, "queue/logical_block_size");
755 /* Fallback based on BLKSSZGET ioctl */
757 int fd
= loopcxt_get_fd(lc
);
762 rc
= blkdev_get_sector_size(fd
, &sz
);
769 DBG(CXT
, ul_debugobj(lc
, "get_blocksize [rc=%d]", rc
));
775 * @sizelimit: returns size limit for the given device
777 * Returns: <0 on error, 0 on success
779 int loopcxt_get_sizelimit(struct loopdev_cxt
*lc
, uint64_t *size
)
781 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
785 rc
= ul_path_read_u64(sysfs
, size
, "loop/sizelimit");
787 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
788 struct loop_info64
*lo
= loopcxt_get_info(lc
);
791 *size
= lo
->lo_sizelimit
;
797 DBG(CXT
, ul_debugobj(lc
, "get_sizelimit [rc=%d]", rc
));
803 * @devno: returns encryption type
805 * Cryptoloop is DEPRECATED!
807 * Returns: <0 on error, 0 on success
809 int loopcxt_get_encrypt_type(struct loopdev_cxt
*lc
, uint32_t *type
)
811 struct loop_info64
*lo
= loopcxt_get_info(lc
);
814 /* not provided by sysfs */
817 *type
= lo
->lo_encrypt_type
;
822 DBG(CXT
, ul_debugobj(lc
, "get_encrypt_type [rc=%d]", rc
));
828 * @devno: returns crypt name
830 * Cryptoloop is DEPRECATED!
832 * Returns: <0 on error, 0 on success
834 const char *loopcxt_get_crypt_name(struct loopdev_cxt
*lc
)
836 struct loop_info64
*lo
= loopcxt_get_info(lc
);
839 return (char *) lo
->lo_crypt_name
;
841 DBG(CXT
, ul_debugobj(lc
, "get_crypt_name failed"));
847 * @devno: returns backing file devno
849 * Returns: <0 on error, 0 on success
851 int loopcxt_get_backing_devno(struct loopdev_cxt
*lc
, dev_t
*devno
)
853 struct loop_info64
*lo
= loopcxt_get_info(lc
);
858 *devno
= lo
->lo_device
;
863 DBG(CXT
, ul_debugobj(lc
, "get_backing_devno [rc=%d]", rc
));
869 * @ino: returns backing file inode
871 * Returns: <0 on error, 0 on success
873 int loopcxt_get_backing_inode(struct loopdev_cxt
*lc
, ino_t
*ino
)
875 struct loop_info64
*lo
= loopcxt_get_info(lc
);
885 DBG(CXT
, ul_debugobj(lc
, "get_backing_inode [rc=%d]", rc
));
890 * Check if the kernel supports partitioned loop devices.
893 * - kernels < 3.2 support partitioned loop devices and PT scanning
894 * only if max_part= module parameter is non-zero
896 * - kernels >= 3.2 always support partitioned loop devices
898 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
900 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
901 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
904 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
906 int loopmod_supports_partscan(void)
911 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
914 f
= fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR
);
917 rc
= fscanf(f
, "%d", &ret
);
919 return rc
== 1 ? ret
: 0;
925 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
926 * scanning is enabled for all loop devices.
928 int loopcxt_is_partscan(struct loopdev_cxt
*lc
)
930 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
935 if (ul_path_read_s32(sysfs
, &fl
, "loop/partscan") == 0)
939 /* old kernels (including kernels without loopN/loop/<flags> directory */
940 return loopmod_supports_partscan();
946 * Returns: 1 if the autoclear flags is set.
948 int loopcxt_is_autoclear(struct loopdev_cxt
*lc
)
950 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
954 if (ul_path_read_s32(sysfs
, &fl
, "loop/autoclear") == 0)
958 if (loopcxt_ioctl_enabled(lc
)) {
959 struct loop_info64
*lo
= loopcxt_get_info(lc
);
961 return lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
;
969 * Returns: 1 if the readonly flags is set.
971 int loopcxt_is_readonly(struct loopdev_cxt
*lc
)
973 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
977 if (ul_path_read_s32(sysfs
, &fl
, "ro") == 0)
981 if (loopcxt_ioctl_enabled(lc
)) {
982 struct loop_info64
*lo
= loopcxt_get_info(lc
);
984 return lo
->lo_flags
& LO_FLAGS_READ_ONLY
;
992 * Returns: 1 if the dio flags is set.
994 int loopcxt_is_dio(struct loopdev_cxt
*lc
)
996 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1000 if (ul_path_read_s32(sysfs
, &fl
, "loop/dio") == 0)
1003 if (loopcxt_ioctl_enabled(lc
)) {
1004 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1006 return lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
1013 * @st: backing file stat or NULL
1014 * @backing_file: filename
1015 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
1016 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
1017 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
1019 * Returns 1 if the current @lc loopdev is associated with the given backing
1020 * file. Note that the preferred way is to use devno and inode number rather
1021 * than filename. The @backing_file filename is poor solution usable in case
1022 * that you don't have rights to call stat().
1024 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
1026 * Don't forget that old kernels provide very restricted (in size) backing
1027 * filename by LOOP_GET_STAT64 ioctl only.
1029 int loopcxt_is_used(struct loopdev_cxt
*lc
,
1031 const char *backing_file
,
1042 DBG(CXT
, ul_debugobj(lc
, "checking %s vs. %s",
1043 loopcxt_get_device(lc
),
1046 if (st
&& loopcxt_get_backing_inode(lc
, &ino
) == 0 &&
1047 loopcxt_get_backing_devno(lc
, &dev
) == 0) {
1049 if (ino
== st
->st_ino
&& dev
== st
->st_dev
)
1052 /* don't use filename if we have devno and inode */
1056 /* poor man's solution */
1058 char *name
= loopcxt_get_backing_file(lc
);
1059 int rc
= name
&& strcmp(name
, backing_file
) == 0;
1068 if (flags
& LOOPDEV_FL_OFFSET
) {
1071 int rc
= loopcxt_get_offset(lc
, &off
) == 0 && off
== offset
;
1073 if (rc
&& flags
& LOOPDEV_FL_SIZELIMIT
) {
1076 return loopcxt_get_sizelimit(lc
, &sz
) == 0 && sz
== sizelimit
;
1084 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1086 int loopcxt_set_offset(struct loopdev_cxt
*lc
, uint64_t offset
)
1090 lc
->info
.lo_offset
= offset
;
1092 DBG(CXT
, ul_debugobj(lc
, "set offset=%jd", offset
));
1097 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1099 int loopcxt_set_sizelimit(struct loopdev_cxt
*lc
, uint64_t sizelimit
)
1103 lc
->info
.lo_sizelimit
= sizelimit
;
1105 DBG(CXT
, ul_debugobj(lc
, "set sizelimit=%jd", sizelimit
));
1110 * The blocksize will be used by loopcxt_set_device(). For already exiting
1111 * devices use loopcxt_ioctl_blocksize().
1113 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1115 int loopcxt_set_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1119 lc
->blocksize
= blocksize
;
1121 DBG(CXT
, ul_debugobj(lc
, "set blocksize=%jd", blocksize
));
1127 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1129 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1131 * Returns: 0 on success, <0 on error.
1133 int loopcxt_set_flags(struct loopdev_cxt
*lc
, uint32_t flags
)
1137 lc
->info
.lo_flags
= flags
;
1139 DBG(CXT
, ul_debugobj(lc
, "set flags=%u", (unsigned) flags
));
1145 * @filename: backing file path (the path will be canonicalized)
1147 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1149 * Returns: 0 on success, <0 on error.
1151 int loopcxt_set_backing_file(struct loopdev_cxt
*lc
, const char *filename
)
1156 lc
->filename
= canonicalize_path(filename
);
1160 xstrncpy((char *)lc
->info
.lo_file_name
, lc
->filename
, LO_NAME_SIZE
);
1162 DBG(CXT
, ul_debugobj(lc
, "set backing file=%s", lc
->info
.lo_file_name
));
1167 * In kernels prior to v3.9, if the offset or sizelimit options
1168 * are used, the block device's size won't be synced automatically.
1169 * blockdev --getsize64 and filesystems will use the backing
1170 * file size until the block device has been re-opened or the
1171 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1173 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1174 * the open file descriptor to the mount system call, we need to use
1175 * the ioctl. Calling losetup directly doesn't have this problem since
1176 * it closes the device when it exits and whatever consumes the device
1177 * next will re-open it, causing the resync.
1179 static int loopcxt_check_size(struct loopdev_cxt
*lc
, int file_fd
)
1181 uint64_t size
, expected_size
;
1185 if (!lc
->info
.lo_offset
&& !lc
->info
.lo_sizelimit
)
1188 if (fstat(file_fd
, &st
)) {
1189 DBG(CXT
, ul_debugobj(lc
, "failed to fstat backing file"));
1192 if (S_ISBLK(st
.st_mode
)) {
1193 if (blkdev_get_size(file_fd
,
1194 (unsigned long long *) &expected_size
)) {
1195 DBG(CXT
, ul_debugobj(lc
, "failed to determine device size"));
1199 expected_size
= st
.st_size
;
1201 if (expected_size
== 0 || expected_size
<= lc
->info
.lo_offset
) {
1202 DBG(CXT
, ul_debugobj(lc
, "failed to determine expected size"));
1203 return 0; /* ignore this error */
1206 if (lc
->info
.lo_offset
> 0)
1207 expected_size
-= lc
->info
.lo_offset
;
1209 if (lc
->info
.lo_sizelimit
> 0 && lc
->info
.lo_sizelimit
< expected_size
)
1210 expected_size
= lc
->info
.lo_sizelimit
;
1212 dev_fd
= loopcxt_get_fd(lc
);
1214 DBG(CXT
, ul_debugobj(lc
, "failed to get loop FD"));
1218 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
)) {
1219 DBG(CXT
, ul_debugobj(lc
, "failed to determine loopdev size"));
1223 /* It's block device, so, align to 512-byte sectors */
1224 if (expected_size
% 512) {
1225 DBG(CXT
, ul_debugobj(lc
, "expected size misaligned to 512-byte sectors"));
1226 expected_size
= (expected_size
>> 9) << 9;
1229 if (expected_size
!= size
) {
1230 DBG(CXT
, ul_debugobj(lc
, "warning: loopdev and expected "
1231 "size mismatch (%ju/%ju)",
1232 size
, expected_size
));
1234 if (loopcxt_ioctl_capacity(lc
)) {
1235 /* ioctl not available */
1236 if (errno
== ENOTTY
|| errno
== EINVAL
)
1241 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
))
1244 if (expected_size
!= size
) {
1246 DBG(CXT
, ul_debugobj(lc
, "failed to set loopdev size, "
1247 "size: %ju, expected: %ju",
1248 size
, expected_size
));
1259 * Associate the current device (see loopcxt_{set,get}_device()) with
1260 * a file (see loopcxt_set_backing_file()).
1262 * The device is initialized read-write by default. If you want read-only
1263 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1264 * flags are ignored and modified according to LO_FLAGS_*.
1266 * If the device is already open by loopcxt_get_fd() then this setup device
1267 * function will re-open the device to fix read/write mode.
1269 * The device is also initialized read-only if the backing file is not
1270 * possible to open read-write (e.g. read-only FS).
1272 * Returns: <0 on error, 0 on success.
1274 int loopcxt_setup_device(struct loopdev_cxt
*lc
)
1276 int file_fd
, dev_fd
, mode
= O_RDWR
, rc
= -1, cnt
= 0, err
, again
;
1279 if (!lc
|| !*lc
->device
|| !lc
->filename
)
1282 DBG(SETUP
, ul_debugobj(lc
, "device setup requested"));
1285 * Open backing file and device
1287 if (lc
->info
.lo_flags
& LO_FLAGS_READ_ONLY
)
1290 if ((file_fd
= open(lc
->filename
, mode
| O_CLOEXEC
)) < 0) {
1291 if (mode
!= O_RDONLY
&& (errno
== EROFS
|| errno
== EACCES
))
1292 file_fd
= open(lc
->filename
, mode
= O_RDONLY
);
1295 DBG(SETUP
, ul_debugobj(lc
, "open backing file failed: %m"));
1299 DBG(SETUP
, ul_debugobj(lc
, "backing file open: OK"));
1301 if (lc
->fd
!= -1 && lc
->mode
!= mode
) {
1302 DBG(SETUP
, ul_debugobj(lc
, "closing already open device (mode mismatch)"));
1308 if (mode
== O_RDONLY
) {
1309 lc
->flags
|= LOOPDEV_FL_RDONLY
; /* open() mode */
1310 lc
->info
.lo_flags
|= LO_FLAGS_READ_ONLY
; /* kernel loopdev mode */
1312 lc
->flags
|= LOOPDEV_FL_RDWR
; /* open() mode */
1313 lc
->info
.lo_flags
&= ~LO_FLAGS_READ_ONLY
;
1314 lc
->flags
&= ~LOOPDEV_FL_RDONLY
;
1319 dev_fd
= loopcxt_get_fd(lc
);
1320 if (dev_fd
>= 0 || lc
->control_ok
== 0)
1322 if (errno
!= EACCES
&& errno
!= ENOENT
)
1324 /* We have permissions to open /dev/loop-control, but open
1325 * /dev/loopN failed with EACCES, it's probably because udevd
1326 * does not applied chown yet. Let's wait a moment. */
1328 } while (cnt
++ < 16);
1335 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1340 if (ioctl(dev_fd
, LOOP_SET_FD
, file_fd
) < 0) {
1343 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD failed: %m"));
1347 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD: OK"));
1349 if (lc
->blocksize
> 0
1350 && (rc
= loopcxt_ioctl_blocksize(lc
, lc
->blocksize
)) < 0) {
1356 err
= ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->info
);
1357 again
= err
&& errno
== EAGAIN
;
1364 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1368 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1370 if ((rc
= loopcxt_check_size(lc
, file_fd
)))
1375 memset(&lc
->info
, 0, sizeof(lc
->info
));
1377 lc
->info_failed
= 0;
1379 DBG(SETUP
, ul_debugobj(lc
, "success [rc=0]"));
1384 if (dev_fd
>= 0 && rc
!= -EBUSY
)
1385 ioctl(dev_fd
, LOOP_CLR_FD
, 0);
1389 DBG(SETUP
, ul_debugobj(lc
, "failed [rc=%d]", rc
));
1396 * Update status of the current device (see loopcxt_{set,get}_device()).
1398 * Note that once initialized, kernel accepts only selected changes:
1399 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1400 * For more see linux/drivers/block/loop.c:loop_set_status()
1402 * Returns: <0 on error, 0 on success.
1404 int loopcxt_ioctl_status(struct loopdev_cxt
*lc
)
1406 int dev_fd
, rc
= -1, err
, again
;
1409 dev_fd
= loopcxt_get_fd(lc
);
1415 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1418 err
= ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->info
);
1419 again
= err
&& errno
== EAGAIN
;
1425 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1429 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1433 int loopcxt_ioctl_capacity(struct loopdev_cxt
*lc
)
1435 int fd
= loopcxt_get_fd(lc
);
1440 /* Kernels prior to v2.6.30 don't support this ioctl */
1441 if (ioctl(fd
, LOOP_SET_CAPACITY
, 0) < 0) {
1443 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_CAPACITY failed: %m"));
1447 DBG(CXT
, ul_debugobj(lc
, "capacity set"));
1451 int loopcxt_ioctl_dio(struct loopdev_cxt
*lc
, unsigned long use_dio
)
1453 int fd
= loopcxt_get_fd(lc
);
1458 /* Kernels prior to v4.4 don't support this ioctl */
1459 if (ioctl(fd
, LOOP_SET_DIRECT_IO
, use_dio
) < 0) {
1461 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_DIRECT_IO failed: %m"));
1465 DBG(CXT
, ul_debugobj(lc
, "direct io set"));
1470 * Kernel uses "unsigned long" as ioctl arg, but we use u64 for all sizes to
1471 * keep loopdev internal API simple.
1473 int loopcxt_ioctl_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1475 int fd
= loopcxt_get_fd(lc
);
1480 /* Kernels prior to v4.14 don't support this ioctl */
1481 if (ioctl(fd
, LOOP_SET_BLOCK_SIZE
, (unsigned long) blocksize
) < 0) {
1483 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_BLOCK_SIZE failed: %m"));
1487 DBG(CXT
, ul_debugobj(lc
, "logical block size set"));
1491 int loopcxt_delete_device(struct loopdev_cxt
*lc
)
1493 int fd
= loopcxt_get_fd(lc
);
1498 if (ioctl(fd
, LOOP_CLR_FD
, 0) < 0) {
1499 DBG(CXT
, ul_debugobj(lc
, "LOOP_CLR_FD failed: %m"));
1503 DBG(CXT
, ul_debugobj(lc
, "device removed"));
1507 int loopcxt_add_device(struct loopdev_cxt
*lc
)
1511 const char *p
, *dev
= loopcxt_get_device(lc
);
1516 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
)) {
1521 p
= strrchr(dev
, '/');
1522 if (!p
|| (sscanf(p
, "/loop%d", &nr
) != 1 && sscanf(p
, "/%d", &nr
) != 1)
1526 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1528 DBG(CXT
, ul_debugobj(lc
, "add_device %d", nr
));
1529 rc
= ioctl(ctl
, LOOP_CTL_ADD
, nr
);
1532 lc
->control_ok
= rc
>= 0 ? 1 : 0;
1534 DBG(CXT
, ul_debugobj(lc
, "add_device done [rc=%d]", rc
));
1539 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1540 * kernels we have to check all loop devices to found unused one.
1542 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1544 int loopcxt_find_unused(struct loopdev_cxt
*lc
)
1548 DBG(CXT
, ul_debugobj(lc
, "find_unused requested"));
1550 if (lc
->flags
& LOOPDEV_FL_CONTROL
) {
1553 DBG(CXT
, ul_debugobj(lc
, "using loop-control"));
1555 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1557 rc
= ioctl(ctl
, LOOP_CTL_GET_FREE
);
1560 snprintf(name
, sizeof(name
), "loop%d", rc
);
1562 rc
= loopiter_set_device(lc
, name
);
1564 lc
->control_ok
= ctl
>= 0 && rc
== 0 ? 1 : 0;
1567 DBG(CXT
, ul_debugobj(lc
, "find_unused by loop-control [rc=%d]", rc
));
1571 DBG(CXT
, ul_debugobj(lc
, "using loop scan"));
1572 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_FREE
);
1576 rc
= loopcxt_next(lc
);
1577 loopcxt_deinit_iterator(lc
);
1578 DBG(CXT
, ul_debugobj(lc
, "find_unused by scan [rc=%d]", rc
));
1586 * Return: TRUE/FALSE
1588 int loopdev_is_autoclear(const char *device
)
1590 struct loopdev_cxt lc
;
1596 rc
= loopcxt_init(&lc
, 0);
1598 rc
= loopcxt_set_device(&lc
, device
);
1600 rc
= loopcxt_is_autoclear(&lc
);
1602 loopcxt_deinit(&lc
);
1606 char *loopdev_get_backing_file(const char *device
)
1608 struct loopdev_cxt lc
;
1613 if (loopcxt_init(&lc
, 0))
1615 if (loopcxt_set_device(&lc
, device
) == 0)
1616 res
= loopcxt_get_backing_file(&lc
);
1618 loopcxt_deinit(&lc
);
1623 * Returns: TRUE/FALSE
1625 int loopdev_is_used(const char *device
, const char *filename
,
1626 uint64_t offset
, uint64_t sizelimit
, int flags
)
1628 struct loopdev_cxt lc
;
1632 if (!device
|| !filename
)
1635 rc
= loopcxt_init(&lc
, 0);
1637 rc
= loopcxt_set_device(&lc
, device
);
1641 rc
= !stat(filename
, &st
);
1642 rc
= loopcxt_is_used(&lc
, rc
? &st
: NULL
, filename
, offset
, sizelimit
, flags
);
1644 loopcxt_deinit(&lc
);
1648 int loopdev_delete(const char *device
)
1650 struct loopdev_cxt lc
;
1656 rc
= loopcxt_init(&lc
, 0);
1658 rc
= loopcxt_set_device(&lc
, device
);
1660 rc
= loopcxt_delete_device(&lc
);
1661 loopcxt_deinit(&lc
);
1666 * Returns: 0 = success, < 0 error, 1 not found
1668 int loopcxt_find_by_backing_file(struct loopdev_cxt
*lc
, const char *filename
,
1669 uint64_t offset
, uint64_t sizelimit
, int flags
)
1677 hasst
= !stat(filename
, &st
);
1679 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1683 while ((rc
= loopcxt_next(lc
)) == 0) {
1685 if (loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1686 filename
, offset
, sizelimit
, flags
))
1690 loopcxt_deinit_iterator(lc
);
1695 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1697 int loopcxt_find_overlap(struct loopdev_cxt
*lc
, const char *filename
,
1698 uint64_t offset
, uint64_t sizelimit
)
1706 DBG(CXT
, ul_debugobj(lc
, "find_overlap requested"));
1707 hasst
= !stat(filename
, &st
);
1709 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1713 while ((rc
= loopcxt_next(lc
)) == 0) {
1714 uint64_t lc_sizelimit
, lc_offset
;
1716 rc
= loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1717 filename
, offset
, sizelimit
, 0);
1719 continue; /* unused */
1723 DBG(CXT
, ul_debugobj(lc
, "found %s backed by %s",
1724 loopcxt_get_device(lc
), filename
));
1726 rc
= loopcxt_get_offset(lc
, &lc_offset
);
1728 DBG(CXT
, ul_debugobj(lc
, "failed to get offset for device %s",
1729 loopcxt_get_device(lc
)));
1732 rc
= loopcxt_get_sizelimit(lc
, &lc_sizelimit
);
1734 DBG(CXT
, ul_debugobj(lc
, "failed to get sizelimit for device %s",
1735 loopcxt_get_device(lc
)));
1740 if (lc_sizelimit
== sizelimit
&& lc_offset
== offset
) {
1741 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s (full match)",
1742 loopcxt_get_device(lc
)));
1748 if (lc_sizelimit
!= 0 && offset
>= lc_offset
+ lc_sizelimit
)
1750 if (sizelimit
!= 0 && offset
+ sizelimit
<= lc_offset
)
1753 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s",
1754 loopcxt_get_device(lc
)));
1760 rc
= 0; /* not found */
1762 loopcxt_deinit_iterator(lc
);
1763 DBG(CXT
, ul_debugobj(lc
, "find_overlap done [rc=%d]", rc
));
1768 * Returns allocated string with device name
1770 char *loopdev_find_by_backing_file(const char *filename
, uint64_t offset
, uint64_t sizelimit
, int flags
)
1772 struct loopdev_cxt lc
;
1778 if (loopcxt_init(&lc
, 0))
1780 if (loopcxt_find_by_backing_file(&lc
, filename
, offset
, sizelimit
, flags
) == 0)
1781 res
= loopcxt_strdup_device(&lc
);
1782 loopcxt_deinit(&lc
);
1788 * Returns number of loop devices associated with @file, if only one loop
1789 * device is associated with the given @filename and @loopdev is not NULL then
1790 * @loopdev returns name of the device.
1792 int loopdev_count_by_backing_file(const char *filename
, char **loopdev
)
1794 struct loopdev_cxt lc
;
1800 rc
= loopcxt_init(&lc
, 0);
1803 if (loopcxt_init_iterator(&lc
, LOOPITER_FL_USED
))
1806 while(loopcxt_next(&lc
) == 0) {
1807 char *backing
= loopcxt_get_backing_file(&lc
);
1809 if (!backing
|| strcmp(backing
, filename
)) {
1815 if (loopdev
&& count
== 0)
1816 *loopdev
= loopcxt_strdup_device(&lc
);
1820 loopcxt_deinit(&lc
);
1822 if (loopdev
&& count
> 1) {