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"
47 * Debug stuff (based on include/debug.h)
49 static UL_DEBUG_DEFINE_MASK(loopdev
);
50 UL_DEBUG_DEFINE_MASKNAMES(loopdev
) = UL_DEBUG_EMPTY_MASKNAMES
;
52 #define LOOPDEV_DEBUG_INIT (1 << 1)
53 #define LOOPDEV_DEBUG_CXT (1 << 2)
54 #define LOOPDEV_DEBUG_ITER (1 << 3)
55 #define LOOPDEV_DEBUG_SETUP (1 << 4)
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 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(loopdev)
63 static void loopdev_init_debug(void)
65 if (loopdev_debug_mask
)
67 __UL_INIT_DEBUG_FROM_ENV(loopdev
, LOOPDEV_DEBUG_
, 0, LOOPDEV_DEBUG
);
73 #define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
74 #define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
75 && !loopcxt_ioctl_enabled(_lc)
79 * @device: device name, absolute device path or NULL to reset the current setting
81 * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
82 * names ("loop<N>") are converted to the path (/dev/loop<N> or to
85 * This sets the device name, but does not check if the device exists!
87 * Returns: <0 on error, 0 on success
89 int loopcxt_set_device(struct loopdev_cxt
*lc
, const char *device
)
96 DBG(CXT
, ul_debugobj(lc
, "closing old open fd"));
104 memset(&lc
->config
, 0, sizeof(lc
->config
));
108 if (*device
!= '/') {
109 const char *dir
= _PATH_DEV
;
111 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
112 if (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) {
113 if (strlen(device
) < 5)
116 dir
= _PATH_DEV_LOOP
"/"; /* _PATH_DEV uses tailing slash */
118 snprintf(lc
->device
, sizeof(lc
->device
), "%s%s",
121 xstrncpy(lc
->device
, device
, sizeof(lc
->device
));
123 DBG(CXT
, ul_debugobj(lc
, "%s name assigned", device
));
126 ul_unref_path(lc
->sysfs
);
131 int loopcxt_has_device(struct loopdev_cxt
*lc
)
133 return lc
&& *lc
->device
;
138 * @flags: LOOPDEV_FL_* flags
140 * Initialize loop handler.
142 * We have two sets of the flags:
144 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
146 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
148 * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
149 * syscall to open loop device. By default is the device open read-only.
151 * The exception is loopcxt_setup_device(), where the device is open read-write
152 * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
154 * Returns: <0 on error, 0 on success.
156 int loopcxt_init(struct loopdev_cxt
*lc
, int flags
)
160 struct loopdev_cxt dummy
= UL_LOOPDEVCXT_EMPTY
;
165 loopdev_init_debug();
166 DBG(CXT
, ul_debugobj(lc
, "initialize context"));
168 memcpy(lc
, &dummy
, sizeof(dummy
));
171 rc
= loopcxt_set_device(lc
, NULL
);
175 if (stat(_PATH_SYS_BLOCK
, &st
) || !S_ISDIR(st
.st_mode
)) {
176 lc
->flags
|= LOOPDEV_FL_NOSYSFS
;
177 lc
->flags
&= ~LOOPDEV_FL_NOIOCTL
;
178 DBG(CXT
, ul_debugobj(lc
, "init: disable /sys usage"));
181 if (!(lc
->flags
& LOOPDEV_FL_NOSYSFS
) &&
182 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
184 * Use only sysfs for basic information about loop devices
186 lc
->flags
|= LOOPDEV_FL_NOIOCTL
;
187 DBG(CXT
, ul_debugobj(lc
, "init: ignore ioctls"));
190 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
) && !stat(_PATH_DEV_LOOPCTL
, &st
)) {
191 lc
->flags
|= LOOPDEV_FL_CONTROL
;
192 DBG(CXT
, ul_debugobj(lc
, "init: loop-control detected "));
201 * Deinitialize loop context
203 void loopcxt_deinit(struct loopdev_cxt
*lc
)
210 DBG(CXT
, ul_debugobj(lc
, "de-initialize"));
215 ignore_result( loopcxt_set_device(lc
, NULL
) );
216 loopcxt_deinit_iterator(lc
);
224 * Returns newly allocated device path.
226 char *loopcxt_strdup_device(struct loopdev_cxt
*lc
)
228 if (!lc
|| !*lc
->device
)
230 return strdup(lc
->device
);
236 * Returns pointer device name in the @lc struct.
238 const char *loopcxt_get_device(struct loopdev_cxt
*lc
)
240 return lc
&& *lc
->device
? lc
->device
: NULL
;
246 * Returns pointer to the sysfs context (see lib/sysfs.c)
248 static struct path_cxt
*loopcxt_get_sysfs(struct loopdev_cxt
*lc
)
250 if (!lc
|| !*lc
->device
|| (lc
->flags
& LOOPDEV_FL_NOSYSFS
))
254 dev_t devno
= sysfs_devname_to_devno(lc
->device
);
256 DBG(CXT
, ul_debugobj(lc
, "sysfs: failed devname to devno"));
260 lc
->sysfs
= ul_new_sysfs_path(devno
, NULL
, NULL
);
262 DBG(CXT
, ul_debugobj(lc
, "sysfs: init failed"));
271 * Returns: file descriptor to the open loop device or <0 on error. The mode
272 * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
275 int loopcxt_get_fd(struct loopdev_cxt
*lc
)
277 if (!lc
|| !*lc
->device
)
281 lc
->mode
= lc
->flags
& LOOPDEV_FL_RDWR
? O_RDWR
: O_RDONLY
;
282 lc
->fd
= open(lc
->device
, lc
->mode
| O_CLOEXEC
);
283 DBG(CXT
, ul_debugobj(lc
, "open %s [%s]: %m", lc
->device
,
284 lc
->flags
& LOOPDEV_FL_RDWR
? "rw" : "ro"));
289 int loopcxt_set_fd(struct loopdev_cxt
*lc
, int fd
, int mode
)
301 * @flags: LOOPITER_FL_* flags
303 * Iterator can be used to scan list of the free or used loop devices.
305 * Returns: <0 on error, 0 on success
307 int loopcxt_init_iterator(struct loopdev_cxt
*lc
, int flags
)
309 struct loopdev_iter
*iter
;
317 DBG(ITER
, ul_debugobj(iter
, "initialize"));
321 memset(iter
, 0, sizeof(*iter
));
324 iter
->default_check
= 1;
326 if (!lc
->extra_check
) {
328 * Check for /dev/loop/<N> subdirectory
330 if (!(lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) &&
331 stat(_PATH_DEV_LOOP
, &st
) == 0 && S_ISDIR(st
.st_mode
))
332 lc
->flags
|= LOOPDEV_FL_DEVSUBDIR
;
342 * Returns: <0 on error, 0 on success
344 int loopcxt_deinit_iterator(struct loopdev_cxt
*lc
)
346 struct loopdev_iter
*iter
;
352 DBG(ITER
, ul_debugobj(iter
, "de-initialize"));
358 closedir(iter
->sysblock
);
360 memset(iter
, 0, sizeof(*iter
));
365 * Same as loopcxt_set_device, but also checks if the device is
366 * associated with any file.
368 * Returns: <0 on error, 0 on success, 1 device does not match with
369 * LOOPITER_FL_{USED,FREE} flags.
371 static int loopiter_set_device(struct loopdev_cxt
*lc
, const char *device
)
373 int rc
= loopcxt_set_device(lc
, device
);
379 if (!(lc
->iter
.flags
& LOOPITER_FL_USED
) &&
380 !(lc
->iter
.flags
& LOOPITER_FL_FREE
))
381 return 0; /* caller does not care about device status */
383 if (!is_loopdev(lc
->device
)) {
384 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s does not exist", lc
->device
));
388 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s exist", lc
->device
));
390 used
= loopcxt_get_offset(lc
, NULL
) == 0;
392 if ((lc
->iter
.flags
& LOOPITER_FL_USED
) && used
)
395 if ((lc
->iter
.flags
& LOOPITER_FL_FREE
) && !used
)
398 DBG(ITER
, ul_debugobj(&lc
->iter
, "failed to use %s device", lc
->device
));
400 ignore_result( loopcxt_set_device(lc
, NULL
) );
404 static int cmpnum(const void *p1
, const void *p2
)
406 return (((* (const int *) p1
) > (* (const int *) p2
)) -
407 ((* (const int *) p1
) < (* (const int *) p2
)));
411 * The classic scandir() is more expensive and less portable.
412 * We needn't full loop device names -- loop numbers (loop<N>)
415 static int loop_scandir(const char *dirname
, int **ary
, int hasprefix
)
419 unsigned int n
, count
= 0, arylen
= 0;
421 if (!dirname
|| !ary
)
424 DBG(ITER
, ul_debug("scan dir: %s", dirname
));
426 dir
= opendir(dirname
);
432 while((d
= readdir(dir
))) {
433 #ifdef _DIRENT_HAVE_D_TYPE
434 if (d
->d_type
!= DT_BLK
&& d
->d_type
!= DT_UNKNOWN
&&
438 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
443 if (sscanf(d
->d_name
, "loop%u", &n
) != 1)
450 n
= strtol(d
->d_name
, &end
, 10);
451 if (d
->d_name
== end
|| (end
&& *end
) || errno
)
454 if (n
< LOOPDEV_DEFAULT_NNODES
)
455 continue; /* ignore loop<0..7> */
457 if (count
+ 1 > arylen
) {
462 tmp
= realloc(*ary
, arylen
* sizeof(int));
475 qsort(*ary
, count
, sizeof(int), cmpnum
);
482 * Set the next *used* loop device according to /proc/partitions.
484 * Loop devices smaller than 512 bytes are invisible for this function.
486 static int loopcxt_next_from_proc(struct loopdev_cxt
*lc
)
488 struct loopdev_iter
*iter
= &lc
->iter
;
491 DBG(ITER
, ul_debugobj(iter
, "scan /proc/partitions"));
494 iter
->proc
= fopen(_PATH_PROC_PARTITIONS
, "r" UL_CLOEXECSTR
);
498 while (fgets(buf
, sizeof(buf
), iter
->proc
)) {
503 if (sscanf(buf
, " %u %*s %*s %128[^\n ]",
504 &m
, name
) != 2 || m
!= LOOPDEV_MAJOR
)
507 DBG(ITER
, ul_debugobj(iter
, "checking %s", name
));
509 if (loopiter_set_device(lc
, name
) == 0)
517 * Set the next *used* loop device according to
518 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
520 * This is preferred method.
522 static int loopcxt_next_from_sysfs(struct loopdev_cxt
*lc
)
524 struct loopdev_iter
*iter
= &lc
->iter
;
528 DBG(ITER
, ul_debugobj(iter
, "scanning /sys/block"));
531 iter
->sysblock
= opendir(_PATH_SYS_BLOCK
);
536 fd
= dirfd(iter
->sysblock
);
538 while ((d
= readdir(iter
->sysblock
))) {
539 char name
[NAME_MAX
+ 18 + 1];
542 DBG(ITER
, ul_debugobj(iter
, "check %s", d
->d_name
));
544 if (strcmp(d
->d_name
, ".") == 0
545 || strcmp(d
->d_name
, "..") == 0
546 || strncmp(d
->d_name
, "loop", 4) != 0)
549 snprintf(name
, sizeof(name
), "%s/loop/backing_file", d
->d_name
);
550 if (fstatat(fd
, name
, &st
, 0) != 0)
553 if (loopiter_set_device(lc
, d
->d_name
) == 0)
561 * @lc: context, has to initialized by loopcxt_init_iterator()
563 * Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
564 * about the current loop device are available by
565 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
567 int loopcxt_next(struct loopdev_cxt
*lc
)
569 struct loopdev_iter
*iter
;
579 DBG(ITER
, ul_debugobj(iter
, "next"));
581 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
583 if (iter
->flags
& LOOPITER_FL_USED
) {
586 if (loopcxt_sysfs_available(lc
))
587 rc
= loopcxt_next_from_sysfs(lc
);
589 rc
= loopcxt_next_from_proc(lc
);
595 /* B) Classic way, try first eight loop devices (default number
596 * of loop devices). This is enough for 99% of all cases.
598 if (iter
->default_check
) {
599 DBG(ITER
, ul_debugobj(iter
, "next: default check"));
600 for (++iter
->ncur
; iter
->ncur
< LOOPDEV_DEFAULT_NNODES
;
603 snprintf(name
, sizeof(name
), "loop%d", iter
->ncur
);
605 if (loopiter_set_device(lc
, name
) == 0)
608 iter
->default_check
= 0;
611 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
614 DBG(ITER
, ul_debugobj(iter
, "next: scanning /dev"));
615 iter
->nminors
= (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) ?
616 loop_scandir(_PATH_DEV_LOOP
, &iter
->minors
, 0) :
617 loop_scandir(_PATH_DEV
, &iter
->minors
, 1);
620 for (++iter
->ncur
; iter
->ncur
< iter
->nminors
; iter
->ncur
++) {
622 snprintf(name
, sizeof(name
), "loop%d", iter
->minors
[iter
->ncur
]);
624 if (loopiter_set_device(lc
, name
) == 0)
628 loopcxt_deinit_iterator(lc
);
633 * @device: path to device
635 int is_loopdev(const char *device
)
640 if (!device
|| stat(device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
642 else if (major(st
.st_rdev
) == LOOPDEV_MAJOR
)
644 else if (sysfs_devno_is_wholedisk(st
.st_rdev
)) {
645 /* It's possible that kernel creates a device with a different
646 * major number ... check by /sys it's really loop device.
648 char name
[PATH_MAX
], *cn
, *p
= NULL
;
650 snprintf(name
, sizeof(name
), _PATH_SYS_DEVBLOCK
"/%d:%d",
651 major(st
.st_rdev
), minor(st
.st_rdev
));
652 cn
= canonicalize_path(name
);
654 p
= stripoff_last_component(cn
);
655 rc
= p
&& startswith(p
, "loop");
667 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
669 struct loop_info64
*loopcxt_get_info(struct loopdev_cxt
*lc
)
673 if (!lc
|| lc
->info_failed
) {
679 return &lc
->config
.info
;
681 fd
= loopcxt_get_fd(lc
);
685 if (ioctl(fd
, LOOP_GET_STATUS64
, &lc
->config
.info
) == 0) {
688 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 OK"));
689 return &lc
->config
.info
;
693 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 FAILED"));
701 * Returns (allocated) string with path to the file associated
702 * with the current loop device.
704 char *loopcxt_get_backing_file(struct loopdev_cxt
*lc
)
706 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
711 * This is always preferred, the loop_info64
712 * has too small buffer for the filename.
714 ul_path_read_string(sysfs
, &res
, "loop/backing_file");
716 if (!res
&& loopcxt_ioctl_enabled(lc
)) {
717 struct loop_info64
*lo
= loopcxt_get_info(lc
);
720 lo
->lo_file_name
[LO_NAME_SIZE
- 2] = '*';
721 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
722 res
= strdup((char *) lo
->lo_file_name
);
726 DBG(CXT
, ul_debugobj(lc
, "get_backing_file [%s]", res
));
732 * @offset: returns offset number for the given device
734 * Returns: <0 on error, 0 on success
736 int loopcxt_get_offset(struct loopdev_cxt
*lc
, uint64_t *offset
)
738 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
742 rc
= ul_path_read_u64(sysfs
, offset
, "loop/offset");
744 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
745 struct loop_info64
*lo
= loopcxt_get_info(lc
);
748 *offset
= lo
->lo_offset
;
754 DBG(CXT
, ul_debugobj(lc
, "get_offset [rc=%d]", rc
));
760 * @blocksize: returns logical blocksize for the given device
762 * Returns: <0 on error, 0 on success
764 int loopcxt_get_blocksize(struct loopdev_cxt
*lc
, uint64_t *blocksize
)
766 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
770 rc
= ul_path_read_u64(sysfs
, blocksize
, "queue/logical_block_size");
772 /* Fallback based on BLKSSZGET ioctl */
774 int fd
= loopcxt_get_fd(lc
);
779 rc
= blkdev_get_sector_size(fd
, &sz
);
786 DBG(CXT
, ul_debugobj(lc
, "get_blocksize [rc=%d]", rc
));
792 * @sizelimit: returns size limit for the given device
794 * Returns: <0 on error, 0 on success
796 int loopcxt_get_sizelimit(struct loopdev_cxt
*lc
, uint64_t *size
)
798 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
802 rc
= ul_path_read_u64(sysfs
, size
, "loop/sizelimit");
804 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
805 struct loop_info64
*lo
= loopcxt_get_info(lc
);
808 *size
= lo
->lo_sizelimit
;
814 DBG(CXT
, ul_debugobj(lc
, "get_sizelimit [rc=%d]", rc
));
820 * @devno: returns encryption type
822 * Cryptoloop is DEPRECATED!
824 * Returns: <0 on error, 0 on success
826 int loopcxt_get_encrypt_type(struct loopdev_cxt
*lc
, uint32_t *type
)
828 struct loop_info64
*lo
= loopcxt_get_info(lc
);
831 /* not provided by sysfs */
834 *type
= lo
->lo_encrypt_type
;
839 DBG(CXT
, ul_debugobj(lc
, "get_encrypt_type [rc=%d]", rc
));
845 * @devno: returns crypt name
847 * Cryptoloop is DEPRECATED!
849 * Returns: <0 on error, 0 on success
851 const char *loopcxt_get_crypt_name(struct loopdev_cxt
*lc
)
853 struct loop_info64
*lo
= loopcxt_get_info(lc
);
856 return (char *) lo
->lo_crypt_name
;
858 DBG(CXT
, ul_debugobj(lc
, "get_crypt_name failed"));
864 * @devno: returns backing file devno
866 * Returns: <0 on error, 0 on success
868 int loopcxt_get_backing_devno(struct loopdev_cxt
*lc
, dev_t
*devno
)
870 struct loop_info64
*lo
= loopcxt_get_info(lc
);
875 *devno
= lo
->lo_device
;
880 DBG(CXT
, ul_debugobj(lc
, "get_backing_devno [rc=%d]", rc
));
886 * @ino: returns backing file inode
888 * Returns: <0 on error, 0 on success
890 int loopcxt_get_backing_inode(struct loopdev_cxt
*lc
, ino_t
*ino
)
892 struct loop_info64
*lo
= loopcxt_get_info(lc
);
902 DBG(CXT
, ul_debugobj(lc
, "get_backing_inode [rc=%d]", rc
));
907 * Check if the kernel supports partitioned loop devices.
910 * - kernels < 3.2 support partitioned loop devices and PT scanning
911 * only if max_part= module parameter is non-zero
913 * - kernels >= 3.2 always support partitioned loop devices
915 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
917 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
918 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
921 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
923 int loopmod_supports_partscan(void)
928 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
931 f
= fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR
);
934 rc
= fscanf(f
, "%d", &ret
);
936 return rc
== 1 ? ret
: 0;
942 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
943 * scanning is enabled for all loop devices.
945 int loopcxt_is_partscan(struct loopdev_cxt
*lc
)
947 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
952 if (ul_path_read_s32(sysfs
, &fl
, "loop/partscan") == 0)
956 /* old kernels (including kernels without loopN/loop/<flags> directory */
957 return loopmod_supports_partscan();
963 * Returns: 1 if the autoclear flags is set.
965 int loopcxt_is_autoclear(struct loopdev_cxt
*lc
)
967 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
971 if (ul_path_read_s32(sysfs
, &fl
, "loop/autoclear") == 0)
975 if (loopcxt_ioctl_enabled(lc
)) {
976 struct loop_info64
*lo
= loopcxt_get_info(lc
);
978 return lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
;
986 * Returns: 1 if the readonly flags is set.
988 int loopcxt_is_readonly(struct loopdev_cxt
*lc
)
990 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
994 if (ul_path_read_s32(sysfs
, &fl
, "ro") == 0)
998 if (loopcxt_ioctl_enabled(lc
)) {
999 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1001 return lo
->lo_flags
& LO_FLAGS_READ_ONLY
;
1009 * Returns: 1 if the dio flags is set.
1011 int loopcxt_is_dio(struct loopdev_cxt
*lc
)
1013 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1017 if (ul_path_read_s32(sysfs
, &fl
, "loop/dio") == 0)
1020 if (loopcxt_ioctl_enabled(lc
)) {
1021 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1023 return lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
1030 * @st: backing file stat or NULL
1031 * @backing_file: filename
1032 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
1033 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
1034 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
1036 * Returns 1 if the current @lc loopdev is associated with the given backing
1037 * file. Note that the preferred way is to use devno and inode number rather
1038 * than filename. The @backing_file filename is poor solution usable in case
1039 * that you don't have rights to call stat().
1041 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
1043 * Don't forget that old kernels provide very restricted (in size) backing
1044 * filename by LOOP_GET_STAT64 ioctl only.
1046 int loopcxt_is_used(struct loopdev_cxt
*lc
,
1048 const char *backing_file
,
1059 DBG(CXT
, ul_debugobj(lc
, "checking %s vs. %s",
1060 loopcxt_get_device(lc
),
1063 if (st
&& loopcxt_get_backing_inode(lc
, &ino
) == 0 &&
1064 loopcxt_get_backing_devno(lc
, &dev
) == 0) {
1066 if (ino
== st
->st_ino
&& dev
== st
->st_dev
)
1069 /* don't use filename if we have devno and inode */
1073 /* poor man's solution */
1075 char *name
= loopcxt_get_backing_file(lc
);
1076 int rc
= name
&& strcmp(name
, backing_file
) == 0;
1085 if (flags
& LOOPDEV_FL_OFFSET
) {
1088 int rc
= loopcxt_get_offset(lc
, &off
) == 0 && off
== offset
;
1090 if (rc
&& flags
& LOOPDEV_FL_SIZELIMIT
) {
1093 return loopcxt_get_sizelimit(lc
, &sz
) == 0 && sz
== sizelimit
;
1101 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1103 int loopcxt_set_offset(struct loopdev_cxt
*lc
, uint64_t offset
)
1107 lc
->config
.info
.lo_offset
= offset
;
1109 DBG(CXT
, ul_debugobj(lc
, "set offset=%jd", offset
));
1114 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1116 int loopcxt_set_sizelimit(struct loopdev_cxt
*lc
, uint64_t sizelimit
)
1120 lc
->config
.info
.lo_sizelimit
= sizelimit
;
1122 DBG(CXT
, ul_debugobj(lc
, "set sizelimit=%jd", sizelimit
));
1127 * The blocksize will be used by loopcxt_set_device(). For already exiting
1128 * devices use loopcxt_ioctl_blocksize().
1130 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1132 int loopcxt_set_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1136 lc
->blocksize
= blocksize
;
1138 DBG(CXT
, ul_debugobj(lc
, "set blocksize=%jd", blocksize
));
1144 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1146 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1148 * Returns: 0 on success, <0 on error.
1150 int loopcxt_set_flags(struct loopdev_cxt
*lc
, uint32_t flags
)
1154 lc
->config
.info
.lo_flags
= flags
;
1156 DBG(CXT
, ul_debugobj(lc
, "set flags=%u", (unsigned) flags
));
1162 * @filename: backing file path (the path will be canonicalized)
1164 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1166 * Returns: 0 on success, <0 on error.
1168 int loopcxt_set_backing_file(struct loopdev_cxt
*lc
, const char *filename
)
1173 lc
->filename
= canonicalize_path(filename
);
1177 xstrncpy((char *)lc
->config
.info
.lo_file_name
, lc
->filename
, LO_NAME_SIZE
);
1179 DBG(CXT
, ul_debugobj(lc
, "set backing file=%s", lc
->config
.info
.lo_file_name
));
1184 * In kernels prior to v3.9, if the offset or sizelimit options
1185 * are used, the block device's size won't be synced automatically.
1186 * blockdev --getsize64 and filesystems will use the backing
1187 * file size until the block device has been re-opened or the
1188 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1190 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1191 * the open file descriptor to the mount system call, we need to use
1192 * the ioctl. Calling losetup directly doesn't have this problem since
1193 * it closes the device when it exits and whatever consumes the device
1194 * next will re-open it, causing the resync.
1196 static int loopcxt_check_size(struct loopdev_cxt
*lc
, int file_fd
)
1198 uint64_t size
, expected_size
;
1202 if (!lc
->config
.info
.lo_offset
&& !lc
->config
.info
.lo_sizelimit
)
1205 if (fstat(file_fd
, &st
)) {
1206 DBG(CXT
, ul_debugobj(lc
, "failed to fstat backing file"));
1209 if (S_ISBLK(st
.st_mode
)) {
1210 if (blkdev_get_size(file_fd
,
1211 (unsigned long long *) &expected_size
)) {
1212 DBG(CXT
, ul_debugobj(lc
, "failed to determine device size"));
1216 expected_size
= st
.st_size
;
1218 if (expected_size
== 0 || expected_size
<= lc
->config
.info
.lo_offset
) {
1219 DBG(CXT
, ul_debugobj(lc
, "failed to determine expected size"));
1220 return 0; /* ignore this error */
1223 if (lc
->config
.info
.lo_offset
> 0)
1224 expected_size
-= lc
->config
.info
.lo_offset
;
1226 if (lc
->config
.info
.lo_sizelimit
> 0 && lc
->config
.info
.lo_sizelimit
< expected_size
)
1227 expected_size
= lc
->config
.info
.lo_sizelimit
;
1229 dev_fd
= loopcxt_get_fd(lc
);
1231 DBG(CXT
, ul_debugobj(lc
, "failed to get loop FD"));
1235 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
)) {
1236 DBG(CXT
, ul_debugobj(lc
, "failed to determine loopdev size"));
1240 /* It's block device, so, align to 512-byte sectors */
1241 if (expected_size
% 512) {
1242 DBG(CXT
, ul_debugobj(lc
, "expected size misaligned to 512-byte sectors"));
1243 expected_size
= (expected_size
>> 9) << 9;
1246 if (expected_size
!= size
) {
1247 DBG(CXT
, ul_debugobj(lc
, "warning: loopdev and expected "
1248 "size mismatch (%ju/%ju)",
1249 size
, expected_size
));
1251 if (loopcxt_ioctl_capacity(lc
)) {
1252 /* ioctl not available */
1253 if (errno
== ENOTTY
|| errno
== EINVAL
)
1258 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
))
1261 if (expected_size
!= size
) {
1263 DBG(CXT
, ul_debugobj(lc
, "failed to set loopdev size, "
1264 "size: %ju, expected: %ju",
1265 size
, expected_size
));
1276 * Associate the current device (see loopcxt_{set,get}_device()) with
1277 * a file (see loopcxt_set_backing_file()).
1279 * The device is initialized read-write by default. If you want read-only
1280 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1281 * flags are ignored and modified according to LO_FLAGS_*.
1283 * If the device is already open by loopcxt_get_fd() then this setup device
1284 * function will re-open the device to fix read/write mode.
1286 * The device is also initialized read-only if the backing file is not
1287 * possible to open read-write (e.g. read-only FS).
1289 * Returns: <0 on error, 0 on success.
1291 int loopcxt_setup_device(struct loopdev_cxt
*lc
)
1293 int file_fd
, dev_fd
, mode
= O_RDWR
, flags
= O_CLOEXEC
;
1294 int rc
= -1, cnt
= 0, err
, again
;
1298 if (!lc
|| !*lc
->device
|| !lc
->filename
)
1301 DBG(SETUP
, ul_debugobj(lc
, "device setup requested"));
1304 * Open backing file and device
1306 if (lc
->config
.info
.lo_flags
& LO_FLAGS_READ_ONLY
)
1309 if (lc
->config
.info
.lo_flags
& LO_FLAGS_DIRECT_IO
)
1312 if ((file_fd
= open(lc
->filename
, mode
| flags
)) < 0) {
1313 if (mode
!= O_RDONLY
&& (errno
== EROFS
|| errno
== EACCES
))
1314 file_fd
= open(lc
->filename
, (mode
= O_RDONLY
) | flags
);
1317 DBG(SETUP
, ul_debugobj(lc
, "open backing file failed: %m"));
1321 DBG(SETUP
, ul_debugobj(lc
, "backing file open: OK"));
1323 if (lc
->fd
!= -1 && lc
->mode
!= mode
) {
1324 DBG(SETUP
, ul_debugobj(lc
, "closing already open device (mode mismatch)"));
1330 if (mode
== O_RDONLY
) {
1331 lc
->flags
|= LOOPDEV_FL_RDONLY
; /* open() mode */
1332 lc
->config
.info
.lo_flags
|= LO_FLAGS_READ_ONLY
; /* kernel loopdev mode */
1334 lc
->flags
|= LOOPDEV_FL_RDWR
; /* open() mode */
1335 lc
->config
.info
.lo_flags
&= ~LO_FLAGS_READ_ONLY
;
1336 lc
->flags
&= ~LOOPDEV_FL_RDONLY
;
1341 dev_fd
= loopcxt_get_fd(lc
);
1342 if (dev_fd
>= 0 || lc
->control_ok
== 0)
1344 if (errno
!= EACCES
&& errno
!= ENOENT
)
1346 /* We have permissions to open /dev/loop-control, but open
1347 * /dev/loopN failed with EACCES, it's probably because udevd
1348 * does not applied chown yet. Let's wait a moment. */
1350 } while (cnt
++ < 16);
1357 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1360 * Atomic way to configure all by one ioctl call
1361 * -- since Linux v5.8-rc1, commit 3448914e8cc550ba792d4ccc74471d1ca4293aae
1363 lc
->config
.fd
= file_fd
;
1364 if (ioctl(dev_fd
, LOOP_CONFIGURE
, &lc
->config
) < 0) {
1367 if (errno
!= EINVAL
&& errno
!= ENOTTY
) {
1368 DBG(SETUP
, ul_debugobj(lc
, "LOOP_CONFIGURE failed: %m"));
1373 if (lc
->blocksize
> 0
1374 && (rc
= loopcxt_ioctl_blocksize(lc
, lc
->blocksize
)) < 0) {
1378 DBG(SETUP
, ul_debugobj(lc
, "LOOP_CONFIGURE: OK"));
1382 * Old deprecated way; first assign backing file FD and then in the
1383 * second step set loop device properties.
1386 if (ioctl(dev_fd
, LOOP_SET_FD
, file_fd
) < 0) {
1389 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD failed: %m"));
1393 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD: OK"));
1395 if (lc
->blocksize
> 0
1396 && (rc
= loopcxt_ioctl_blocksize(lc
, lc
->blocksize
)) < 0) {
1402 err
= ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->config
.info
);
1403 again
= err
&& errno
== EAGAIN
;
1411 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1415 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1418 if ((rc
= loopcxt_check_size(lc
, file_fd
)))
1423 memset(&lc
->config
, 0, sizeof(lc
->config
));
1425 lc
->info_failed
= 0;
1427 DBG(SETUP
, ul_debugobj(lc
, "success [rc=0]"));
1432 if (dev_fd
>= 0 && rc
!= -EBUSY
)
1433 ioctl(dev_fd
, LOOP_CLR_FD
, 0);
1437 DBG(SETUP
, ul_debugobj(lc
, "failed [rc=%d]", rc
));
1444 * Update status of the current device (see loopcxt_{set,get}_device()).
1446 * Note that once initialized, kernel accepts only selected changes:
1447 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1448 * For more see linux/drivers/block/loop.c:loop_set_status()
1450 * Returns: <0 on error, 0 on success.
1452 int loopcxt_ioctl_status(struct loopdev_cxt
*lc
)
1454 int dev_fd
, rc
= -1, err
, again
;
1457 dev_fd
= loopcxt_get_fd(lc
);
1463 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1466 err
= ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->config
.info
);
1467 again
= err
&& errno
== EAGAIN
;
1473 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1477 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1481 int loopcxt_ioctl_capacity(struct loopdev_cxt
*lc
)
1483 int fd
= loopcxt_get_fd(lc
);
1488 /* Kernels prior to v2.6.30 don't support this ioctl */
1489 if (ioctl(fd
, LOOP_SET_CAPACITY
, 0) < 0) {
1491 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_CAPACITY failed: %m"));
1495 DBG(CXT
, ul_debugobj(lc
, "capacity set"));
1499 int loopcxt_ioctl_dio(struct loopdev_cxt
*lc
, unsigned long use_dio
)
1501 int fd
= loopcxt_get_fd(lc
);
1506 /* Kernels prior to v4.4 don't support this ioctl */
1507 if (ioctl(fd
, LOOP_SET_DIRECT_IO
, use_dio
) < 0) {
1509 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_DIRECT_IO failed: %m"));
1513 DBG(CXT
, ul_debugobj(lc
, "direct io set"));
1518 * Kernel uses "unsigned long" as ioctl arg, but we use u64 for all sizes to
1519 * keep loopdev internal API simple.
1521 int loopcxt_ioctl_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1523 int fd
= loopcxt_get_fd(lc
);
1528 /* Kernels prior to v4.14 don't support this ioctl */
1529 if (ioctl(fd
, LOOP_SET_BLOCK_SIZE
, (unsigned long) blocksize
) < 0) {
1531 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_BLOCK_SIZE failed: %m"));
1535 DBG(CXT
, ul_debugobj(lc
, "logical block size set"));
1539 int loopcxt_delete_device(struct loopdev_cxt
*lc
)
1541 int fd
= loopcxt_get_fd(lc
);
1546 if (ioctl(fd
, LOOP_CLR_FD
, 0) < 0) {
1547 DBG(CXT
, ul_debugobj(lc
, "LOOP_CLR_FD failed: %m"));
1551 DBG(CXT
, ul_debugobj(lc
, "device removed"));
1555 int loopcxt_add_device(struct loopdev_cxt
*lc
)
1559 const char *p
, *dev
= loopcxt_get_device(lc
);
1564 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
)) {
1569 p
= strrchr(dev
, '/');
1570 if (!p
|| (sscanf(p
, "/loop%d", &nr
) != 1 && sscanf(p
, "/%d", &nr
) != 1)
1574 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1576 DBG(CXT
, ul_debugobj(lc
, "add_device %d", nr
));
1577 rc
= ioctl(ctl
, LOOP_CTL_ADD
, nr
);
1580 lc
->control_ok
= rc
>= 0 ? 1 : 0;
1582 DBG(CXT
, ul_debugobj(lc
, "add_device done [rc=%d]", rc
));
1587 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1588 * kernels we have to check all loop devices to found unused one.
1590 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1592 int loopcxt_find_unused(struct loopdev_cxt
*lc
)
1596 DBG(CXT
, ul_debugobj(lc
, "find_unused requested"));
1598 if (lc
->flags
& LOOPDEV_FL_CONTROL
) {
1601 DBG(CXT
, ul_debugobj(lc
, "using loop-control"));
1603 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1605 rc
= ioctl(ctl
, LOOP_CTL_GET_FREE
);
1608 snprintf(name
, sizeof(name
), "loop%d", rc
);
1610 rc
= loopiter_set_device(lc
, name
);
1612 lc
->control_ok
= ctl
>= 0 && rc
== 0 ? 1 : 0;
1615 DBG(CXT
, ul_debugobj(lc
, "find_unused by loop-control [rc=%d]", rc
));
1619 DBG(CXT
, ul_debugobj(lc
, "using loop scan"));
1620 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_FREE
);
1624 rc
= loopcxt_next(lc
);
1625 loopcxt_deinit_iterator(lc
);
1626 DBG(CXT
, ul_debugobj(lc
, "find_unused by scan [rc=%d]", rc
));
1634 * Return: TRUE/FALSE
1636 int loopdev_is_autoclear(const char *device
)
1638 struct loopdev_cxt lc
;
1644 rc
= loopcxt_init(&lc
, 0);
1646 rc
= loopcxt_set_device(&lc
, device
);
1648 rc
= loopcxt_is_autoclear(&lc
);
1650 loopcxt_deinit(&lc
);
1654 char *loopdev_get_backing_file(const char *device
)
1656 struct loopdev_cxt lc
;
1661 if (loopcxt_init(&lc
, 0))
1663 if (loopcxt_set_device(&lc
, device
) == 0)
1664 res
= loopcxt_get_backing_file(&lc
);
1666 loopcxt_deinit(&lc
);
1670 int loopdev_has_backing_file(const char *device
)
1672 char *tmp
= loopdev_get_backing_file(device
);
1682 * Returns: TRUE/FALSE
1684 int loopdev_is_used(const char *device
, const char *filename
,
1685 uint64_t offset
, uint64_t sizelimit
, int flags
)
1687 struct loopdev_cxt lc
;
1691 if (!device
|| !filename
)
1694 rc
= loopcxt_init(&lc
, 0);
1696 rc
= loopcxt_set_device(&lc
, device
);
1700 rc
= !stat(filename
, &st
);
1701 rc
= loopcxt_is_used(&lc
, rc
? &st
: NULL
, filename
, offset
, sizelimit
, flags
);
1703 loopcxt_deinit(&lc
);
1707 int loopdev_delete(const char *device
)
1709 struct loopdev_cxt lc
;
1715 rc
= loopcxt_init(&lc
, 0);
1717 rc
= loopcxt_set_device(&lc
, device
);
1719 rc
= loopcxt_delete_device(&lc
);
1720 loopcxt_deinit(&lc
);
1725 * Returns: 0 = success, < 0 error, 1 not found
1727 int loopcxt_find_by_backing_file(struct loopdev_cxt
*lc
, const char *filename
,
1728 uint64_t offset
, uint64_t sizelimit
, int flags
)
1736 hasst
= !stat(filename
, &st
);
1738 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1742 while ((rc
= loopcxt_next(lc
)) == 0) {
1744 if (loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1745 filename
, offset
, sizelimit
, flags
))
1749 loopcxt_deinit_iterator(lc
);
1754 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1756 int loopcxt_find_overlap(struct loopdev_cxt
*lc
, const char *filename
,
1757 uint64_t offset
, uint64_t sizelimit
)
1765 DBG(CXT
, ul_debugobj(lc
, "find_overlap requested"));
1766 hasst
= !stat(filename
, &st
);
1768 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1772 while ((rc
= loopcxt_next(lc
)) == 0) {
1773 uint64_t lc_sizelimit
, lc_offset
;
1775 rc
= loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1776 filename
, offset
, sizelimit
, 0);
1778 continue; /* unused */
1782 DBG(CXT
, ul_debugobj(lc
, "found %s backed by %s",
1783 loopcxt_get_device(lc
), filename
));
1785 rc
= loopcxt_get_offset(lc
, &lc_offset
);
1787 DBG(CXT
, ul_debugobj(lc
, "failed to get offset for device %s",
1788 loopcxt_get_device(lc
)));
1791 rc
= loopcxt_get_sizelimit(lc
, &lc_sizelimit
);
1793 DBG(CXT
, ul_debugobj(lc
, "failed to get sizelimit for device %s",
1794 loopcxt_get_device(lc
)));
1799 if (lc_sizelimit
== sizelimit
&& lc_offset
== offset
) {
1800 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s (full match)",
1801 loopcxt_get_device(lc
)));
1807 if (lc_sizelimit
!= 0 && offset
>= lc_offset
+ lc_sizelimit
)
1809 if (sizelimit
!= 0 && offset
+ sizelimit
<= lc_offset
)
1812 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s",
1813 loopcxt_get_device(lc
)));
1819 rc
= 0; /* not found */
1821 loopcxt_deinit_iterator(lc
);
1822 DBG(CXT
, ul_debugobj(lc
, "find_overlap done [rc=%d]", rc
));
1827 * Returns allocated string with device name
1829 char *loopdev_find_by_backing_file(const char *filename
, uint64_t offset
, uint64_t sizelimit
, int flags
)
1831 struct loopdev_cxt lc
;
1837 if (loopcxt_init(&lc
, 0))
1839 if (loopcxt_find_by_backing_file(&lc
, filename
, offset
, sizelimit
, flags
) == 0)
1840 res
= loopcxt_strdup_device(&lc
);
1841 loopcxt_deinit(&lc
);
1847 * Returns number of loop devices associated with @file, if only one loop
1848 * device is associated with the given @filename and @loopdev is not NULL then
1849 * @loopdev returns name of the device.
1851 int loopdev_count_by_backing_file(const char *filename
, char **loopdev
)
1853 struct loopdev_cxt lc
;
1859 rc
= loopcxt_init(&lc
, 0);
1862 if (loopcxt_init_iterator(&lc
, LOOPITER_FL_USED
))
1865 while(loopcxt_next(&lc
) == 0) {
1866 char *backing
= loopcxt_get_backing_file(&lc
);
1868 if (!backing
|| strcmp(backing
, filename
) != 0) {
1874 if (loopdev
&& count
== 0)
1875 *loopdev
= loopcxt_strdup_device(&lc
);
1879 loopcxt_deinit(&lc
);
1881 if (loopdev
&& count
> 1) {
1888 #ifdef TEST_PROGRAM_LOOPDEV
1889 int main(int argc
, char *argv
[])
1894 if (strcmp(argv
[1], "--is-loopdev") == 0 && argc
== 3)
1895 printf("%s: %s\n", argv
[2], is_loopdev(argv
[2]) ? "OK" : "FAIL");
1899 return EXIT_SUCCESS
;
1901 fprintf(stderr
, "usage: %1$s --is-loopdev <dev>\n",
1902 program_invocation_short_name
);
1903 return EXIT_FAILURE
;