]>
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 strncpy(lc
->device
, device
, sizeof(lc
->device
));
121 lc
->device
[sizeof(lc
->device
) - 1] = '\0';
123 DBG(CXT
, ul_debugobj(lc
, "%s name assigned", device
));
126 ul_unref_path(lc
->sysfs
);
131 inline 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 allows 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
)
639 if (device
&& stat(device
, &st
) == 0 &&
640 S_ISBLK(st
.st_mode
) &&
641 major(st
.st_rdev
) == LOOPDEV_MAJOR
)
651 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
653 struct loop_info64
*loopcxt_get_info(struct loopdev_cxt
*lc
)
657 if (!lc
|| lc
->info_failed
) {
665 fd
= loopcxt_get_fd(lc
);
669 if (ioctl(fd
, LOOP_GET_STATUS64
, &lc
->info
) == 0) {
672 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 OK"));
677 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 FAILED"));
685 * Returns (allocated) string with path to the file assicieted
686 * with the current loop device.
688 char *loopcxt_get_backing_file(struct loopdev_cxt
*lc
)
690 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
695 * This is always preferred, the loop_info64
696 * has too small buffer for the filename.
698 ul_path_read_string(sysfs
, &res
, "loop/backing_file");
700 if (!res
&& loopcxt_ioctl_enabled(lc
)) {
701 struct loop_info64
*lo
= loopcxt_get_info(lc
);
704 lo
->lo_file_name
[LO_NAME_SIZE
- 2] = '*';
705 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
706 res
= strdup((char *) lo
->lo_file_name
);
710 DBG(CXT
, ul_debugobj(lc
, "get_backing_file [%s]", res
));
716 * @offset: returns offset number for the given device
718 * Returns: <0 on error, 0 on success
720 int loopcxt_get_offset(struct loopdev_cxt
*lc
, uint64_t *offset
)
722 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
726 rc
= ul_path_read_u64(sysfs
, offset
, "loop/offset");
728 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
729 struct loop_info64
*lo
= loopcxt_get_info(lc
);
732 *offset
= lo
->lo_offset
;
738 DBG(CXT
, ul_debugobj(lc
, "get_offset [rc=%d]", rc
));
744 * @blocksize: returns logical blocksize for the given device
746 * Returns: <0 on error, 0 on success
748 int loopcxt_get_blocksize(struct loopdev_cxt
*lc
, uint64_t *blocksize
)
750 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
754 rc
= ul_path_read_u64(sysfs
, blocksize
, "queue/logical_block_size");
756 /* Fallback based on BLKSSZGET ioctl */
758 int fd
= loopcxt_get_fd(lc
);
763 rc
= blkdev_get_sector_size(fd
, &sz
);
770 DBG(CXT
, ul_debugobj(lc
, "get_blocksize [rc=%d]", rc
));
776 * @sizelimit: returns size limit for the given device
778 * Returns: <0 on error, 0 on success
780 int loopcxt_get_sizelimit(struct loopdev_cxt
*lc
, uint64_t *size
)
782 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
786 rc
= ul_path_read_u64(sysfs
, size
, "loop/sizelimit");
788 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
789 struct loop_info64
*lo
= loopcxt_get_info(lc
);
792 *size
= lo
->lo_sizelimit
;
798 DBG(CXT
, ul_debugobj(lc
, "get_sizelimit [rc=%d]", rc
));
804 * @devno: returns encryption type
806 * Cryptoloop is DEPRECATED!
808 * Returns: <0 on error, 0 on success
810 int loopcxt_get_encrypt_type(struct loopdev_cxt
*lc
, uint32_t *type
)
812 struct loop_info64
*lo
= loopcxt_get_info(lc
);
815 /* not provided by sysfs */
818 *type
= lo
->lo_encrypt_type
;
823 DBG(CXT
, ul_debugobj(lc
, "get_encrypt_type [rc=%d]", rc
));
829 * @devno: returns crypt name
831 * Cryptoloop is DEPRECATED!
833 * Returns: <0 on error, 0 on success
835 const char *loopcxt_get_crypt_name(struct loopdev_cxt
*lc
)
837 struct loop_info64
*lo
= loopcxt_get_info(lc
);
840 return (char *) lo
->lo_crypt_name
;
842 DBG(CXT
, ul_debugobj(lc
, "get_crypt_name failed"));
848 * @devno: returns backing file devno
850 * Returns: <0 on error, 0 on success
852 int loopcxt_get_backing_devno(struct loopdev_cxt
*lc
, dev_t
*devno
)
854 struct loop_info64
*lo
= loopcxt_get_info(lc
);
859 *devno
= lo
->lo_device
;
864 DBG(CXT
, ul_debugobj(lc
, "get_backing_devno [rc=%d]", rc
));
870 * @ino: returns backing file inode
872 * Returns: <0 on error, 0 on success
874 int loopcxt_get_backing_inode(struct loopdev_cxt
*lc
, ino_t
*ino
)
876 struct loop_info64
*lo
= loopcxt_get_info(lc
);
886 DBG(CXT
, ul_debugobj(lc
, "get_backing_inode [rc=%d]", rc
));
891 * Check if the kernel supports partitioned loop devices.
894 * - kernels < 3.2 support partitioned loop devices and PT scanning
895 * only if max_part= module parameter is non-zero
897 * - kernels >= 3.2 always support partitioned loop devices
899 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
901 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
902 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
905 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
907 int loopmod_supports_partscan(void)
912 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
915 f
= fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR
);
918 rc
= fscanf(f
, "%d", &ret
);
920 return rc
== 1 ? ret
: 0;
926 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
927 * scanning is enabled for all loop devices.
929 int loopcxt_is_partscan(struct loopdev_cxt
*lc
)
931 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
936 if (ul_path_read_s32(sysfs
, &fl
, "loop/partscan") == 0)
940 /* old kernels (including kernels without loopN/loop/<flags> directory */
941 return loopmod_supports_partscan();
947 * Returns: 1 if the autoclear flags is set.
949 int loopcxt_is_autoclear(struct loopdev_cxt
*lc
)
951 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
955 if (ul_path_read_s32(sysfs
, &fl
, "loop/autoclear") == 0)
959 if (loopcxt_ioctl_enabled(lc
)) {
960 struct loop_info64
*lo
= loopcxt_get_info(lc
);
962 return lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
;
970 * Returns: 1 if the readonly flags is set.
972 int loopcxt_is_readonly(struct loopdev_cxt
*lc
)
974 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
978 if (ul_path_read_s32(sysfs
, &fl
, "ro") == 0)
982 if (loopcxt_ioctl_enabled(lc
)) {
983 struct loop_info64
*lo
= loopcxt_get_info(lc
);
985 return lo
->lo_flags
& LO_FLAGS_READ_ONLY
;
993 * Returns: 1 if the dio flags is set.
995 int loopcxt_is_dio(struct loopdev_cxt
*lc
)
997 struct path_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
1001 if (ul_path_read_s32(sysfs
, &fl
, "loop/dio") == 0)
1004 if (loopcxt_ioctl_enabled(lc
)) {
1005 struct loop_info64
*lo
= loopcxt_get_info(lc
);
1007 return lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
1014 * @st: backing file stat or NULL
1015 * @backing_file: filename
1016 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
1017 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
1018 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
1020 * Returns 1 if the current @lc loopdev is associated with the given backing
1021 * file. Note that the preferred way is to use devno and inode number rather
1022 * than filename. The @backing_file filename is poor solution usable in case
1023 * that you don't have rights to call stat().
1025 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
1027 * Don't forget that old kernels provide very restricted (in size) backing
1028 * filename by LOOP_GET_STAT64 ioctl only.
1030 int loopcxt_is_used(struct loopdev_cxt
*lc
,
1032 const char *backing_file
,
1043 DBG(CXT
, ul_debugobj(lc
, "checking %s vs. %s",
1044 loopcxt_get_device(lc
),
1047 if (st
&& loopcxt_get_backing_inode(lc
, &ino
) == 0 &&
1048 loopcxt_get_backing_devno(lc
, &dev
) == 0) {
1050 if (ino
== st
->st_ino
&& dev
== st
->st_dev
)
1053 /* don't use filename if we have devno and inode */
1057 /* poor man's solution */
1059 char *name
= loopcxt_get_backing_file(lc
);
1060 int rc
= name
&& strcmp(name
, backing_file
) == 0;
1069 if (flags
& LOOPDEV_FL_OFFSET
) {
1072 int rc
= loopcxt_get_offset(lc
, &off
) == 0 && off
== offset
;
1074 if (rc
&& flags
& LOOPDEV_FL_SIZELIMIT
) {
1077 return loopcxt_get_sizelimit(lc
, &sz
) == 0 && sz
== sizelimit
;
1085 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1087 int loopcxt_set_offset(struct loopdev_cxt
*lc
, uint64_t offset
)
1091 lc
->info
.lo_offset
= offset
;
1093 DBG(CXT
, ul_debugobj(lc
, "set offset=%jd", offset
));
1098 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1100 int loopcxt_set_sizelimit(struct loopdev_cxt
*lc
, uint64_t sizelimit
)
1104 lc
->info
.lo_sizelimit
= sizelimit
;
1106 DBG(CXT
, ul_debugobj(lc
, "set sizelimit=%jd", sizelimit
));
1111 * The blocksize will be used by loopcxt_set_device(). For already exiting
1112 * devices use loopcxt_ioctl_blocksize().
1114 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1116 int loopcxt_set_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1120 lc
->blocksize
= blocksize
;
1122 DBG(CXT
, ul_debugobj(lc
, "set blocksize=%jd", blocksize
));
1128 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1130 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1132 * Returns: 0 on success, <0 on error.
1134 int loopcxt_set_flags(struct loopdev_cxt
*lc
, uint32_t flags
)
1138 lc
->info
.lo_flags
= flags
;
1140 DBG(CXT
, ul_debugobj(lc
, "set flags=%u", (unsigned) flags
));
1146 * @filename: backing file path (the path will be canonicalized)
1148 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1150 * Returns: 0 on success, <0 on error.
1152 int loopcxt_set_backing_file(struct loopdev_cxt
*lc
, const char *filename
)
1157 lc
->filename
= canonicalize_path(filename
);
1161 strncpy((char *)lc
->info
.lo_file_name
, lc
->filename
, LO_NAME_SIZE
);
1162 lc
->info
.lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
1164 DBG(CXT
, ul_debugobj(lc
, "set backing file=%s", lc
->info
.lo_file_name
));
1169 * In kernels prior to v3.9, if the offset or sizelimit options
1170 * are used, the block device's size won't be synced automatically.
1171 * blockdev --getsize64 and filesystems will use the backing
1172 * file size until the block device has been re-opened or the
1173 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1175 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1176 * the open file descriptor to the mount system call, we need to use
1177 * the ioctl. Calling losetup directly doesn't have this problem since
1178 * it closes the device when it exits and whatever consumes the device
1179 * next will re-open it, causing the resync.
1181 static int loopcxt_check_size(struct loopdev_cxt
*lc
, int file_fd
)
1183 uint64_t size
, expected_size
;
1187 if (!lc
->info
.lo_offset
&& !lc
->info
.lo_sizelimit
)
1190 if (fstat(file_fd
, &st
)) {
1191 DBG(CXT
, ul_debugobj(lc
, "failed to fstat backing file"));
1194 if (S_ISBLK(st
.st_mode
)) {
1195 if (blkdev_get_size(file_fd
,
1196 (unsigned long long *) &expected_size
)) {
1197 DBG(CXT
, ul_debugobj(lc
, "failed to determine device size"));
1201 expected_size
= st
.st_size
;
1203 if (expected_size
== 0 || expected_size
<= lc
->info
.lo_offset
) {
1204 DBG(CXT
, ul_debugobj(lc
, "failed to determine expected size"));
1205 return 0; /* ignore this error */
1208 if (lc
->info
.lo_offset
> 0)
1209 expected_size
-= lc
->info
.lo_offset
;
1211 if (lc
->info
.lo_sizelimit
> 0 && lc
->info
.lo_sizelimit
< expected_size
)
1212 expected_size
= lc
->info
.lo_sizelimit
;
1214 dev_fd
= loopcxt_get_fd(lc
);
1216 DBG(CXT
, ul_debugobj(lc
, "failed to get loop FD"));
1220 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
)) {
1221 DBG(CXT
, ul_debugobj(lc
, "failed to determine loopdev size"));
1225 /* It's block device, so, align to 512-byte sectors */
1226 if (expected_size
% 512) {
1227 DBG(CXT
, ul_debugobj(lc
, "expected size misaligned to 512-byte sectors"));
1228 expected_size
= (expected_size
>> 9) << 9;
1231 if (expected_size
!= size
) {
1232 DBG(CXT
, ul_debugobj(lc
, "warning: loopdev and expected "
1233 "size mismatch (%ju/%ju)",
1234 size
, expected_size
));
1236 if (loopcxt_ioctl_capacity(lc
)) {
1237 /* ioctl not available */
1238 if (errno
== ENOTTY
|| errno
== EINVAL
)
1243 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
))
1246 if (expected_size
!= size
) {
1248 DBG(CXT
, ul_debugobj(lc
, "failed to set loopdev size, "
1249 "size: %ju, expected: %ju",
1250 size
, expected_size
));
1261 * Associate the current device (see loopcxt_{set,get}_device()) with
1262 * a file (see loopcxt_set_backing_file()).
1264 * The device is initialized read-write by default. If you want read-only
1265 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1266 * flags are ignored and modified according to LO_FLAGS_*.
1268 * If the device is already open by loopcxt_get_fd() then this setup device
1269 * function will re-open the device to fix read/write mode.
1271 * The device is also initialized read-only if the backing file is not
1272 * possible to open read-write (e.g. read-only FS).
1274 * Returns: <0 on error, 0 on success.
1276 int loopcxt_setup_device(struct loopdev_cxt
*lc
)
1278 int file_fd
, dev_fd
, mode
= O_RDWR
, rc
= -1, cnt
= 0;
1281 if (!lc
|| !*lc
->device
|| !lc
->filename
)
1284 DBG(SETUP
, ul_debugobj(lc
, "device setup requested"));
1287 * Open backing file and device
1289 if (lc
->info
.lo_flags
& LO_FLAGS_READ_ONLY
)
1292 if ((file_fd
= open(lc
->filename
, mode
| O_CLOEXEC
)) < 0) {
1293 if (mode
!= O_RDONLY
&& (errno
== EROFS
|| errno
== EACCES
))
1294 file_fd
= open(lc
->filename
, mode
= O_RDONLY
);
1297 DBG(SETUP
, ul_debugobj(lc
, "open backing file failed: %m"));
1301 DBG(SETUP
, ul_debugobj(lc
, "backing file open: OK"));
1303 if (lc
->fd
!= -1 && lc
->mode
!= mode
) {
1304 DBG(SETUP
, ul_debugobj(lc
, "closing already open device (mode mismatch)"));
1310 if (mode
== O_RDONLY
) {
1311 lc
->flags
|= LOOPDEV_FL_RDONLY
; /* open() mode */
1312 lc
->info
.lo_flags
|= LO_FLAGS_READ_ONLY
; /* kernel loopdev mode */
1314 lc
->flags
|= LOOPDEV_FL_RDWR
; /* open() mode */
1315 lc
->info
.lo_flags
&= ~LO_FLAGS_READ_ONLY
;
1316 lc
->flags
&= ~LOOPDEV_FL_RDONLY
;
1321 dev_fd
= loopcxt_get_fd(lc
);
1322 if (dev_fd
>= 0 || lc
->control_ok
== 0)
1324 if (errno
!= EACCES
&& errno
!= ENOENT
)
1326 /* We have permissions to open /dev/loop-control, but open
1327 * /dev/loopN failed with EACCES, it's probably because udevd
1328 * does not applied chown yet. Let's wait a moment. */
1330 } while (cnt
++ < 16);
1337 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1342 if (ioctl(dev_fd
, LOOP_SET_FD
, file_fd
) < 0) {
1345 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD failed: %m"));
1349 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD: OK"));
1351 if (lc
->blocksize
> 0
1352 && (rc
= loopcxt_ioctl_blocksize(lc
, lc
->blocksize
)) < 0) {
1357 if (ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->info
)) {
1360 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1364 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1366 if ((rc
= loopcxt_check_size(lc
, file_fd
)))
1371 memset(&lc
->info
, 0, sizeof(lc
->info
));
1373 lc
->info_failed
= 0;
1375 DBG(SETUP
, ul_debugobj(lc
, "success [rc=0]"));
1380 if (dev_fd
>= 0 && rc
!= -EBUSY
)
1381 ioctl(dev_fd
, LOOP_CLR_FD
, 0);
1385 DBG(SETUP
, ul_debugobj(lc
, "failed [rc=%d]", rc
));
1392 * Update status of the current device (see loopcxt_{set,get}_device()).
1394 * Note that once initialized, kernel accepts only selected changes:
1395 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1396 * For more see linux/drivers/block/loop.c:loop_set_status()
1398 * Returns: <0 on error, 0 on success.
1400 int loopcxt_ioctl_status(struct loopdev_cxt
*lc
)
1402 int dev_fd
, rc
= -1;
1405 dev_fd
= loopcxt_get_fd(lc
);
1411 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1413 if (ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->info
)) {
1415 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1419 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1423 int loopcxt_ioctl_capacity(struct loopdev_cxt
*lc
)
1425 int fd
= loopcxt_get_fd(lc
);
1430 /* Kernels prior to v2.6.30 don't support this ioctl */
1431 if (ioctl(fd
, LOOP_SET_CAPACITY
, 0) < 0) {
1433 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_CAPACITY failed: %m"));
1437 DBG(CXT
, ul_debugobj(lc
, "capacity set"));
1441 int loopcxt_ioctl_dio(struct loopdev_cxt
*lc
, unsigned long use_dio
)
1443 int fd
= loopcxt_get_fd(lc
);
1448 /* Kernels prior to v4.4 don't support this ioctl */
1449 if (ioctl(fd
, LOOP_SET_DIRECT_IO
, use_dio
) < 0) {
1451 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_DIRECT_IO failed: %m"));
1455 DBG(CXT
, ul_debugobj(lc
, "direct io set"));
1460 * Kernel uses "unsigned long" as ioctl arg, but we use u64 for all sizes to
1461 * keep loopdev internal API simple.
1463 int loopcxt_ioctl_blocksize(struct loopdev_cxt
*lc
, uint64_t blocksize
)
1465 int fd
= loopcxt_get_fd(lc
);
1470 /* Kernels prior to v4.14 don't support this ioctl */
1471 if (ioctl(fd
, LOOP_SET_BLOCK_SIZE
, (unsigned long) blocksize
) < 0) {
1473 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_BLOCK_SIZE failed: %m"));
1477 DBG(CXT
, ul_debugobj(lc
, "logical block size set"));
1481 int loopcxt_delete_device(struct loopdev_cxt
*lc
)
1483 int fd
= loopcxt_get_fd(lc
);
1488 if (ioctl(fd
, LOOP_CLR_FD
, 0) < 0) {
1489 DBG(CXT
, ul_debugobj(lc
, "LOOP_CLR_FD failed: %m"));
1493 DBG(CXT
, ul_debugobj(lc
, "device removed"));
1497 int loopcxt_add_device(struct loopdev_cxt
*lc
)
1501 const char *p
, *dev
= loopcxt_get_device(lc
);
1506 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
)) {
1511 p
= strrchr(dev
, '/');
1512 if (!p
|| (sscanf(p
, "/loop%d", &nr
) != 1 && sscanf(p
, "/%d", &nr
) != 1)
1516 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1518 DBG(CXT
, ul_debugobj(lc
, "add_device %d", nr
));
1519 rc
= ioctl(ctl
, LOOP_CTL_ADD
, nr
);
1522 lc
->control_ok
= rc
>= 0 ? 1 : 0;
1524 DBG(CXT
, ul_debugobj(lc
, "add_device done [rc=%d]", rc
));
1529 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1530 * kernels we have to check all loop devices to found unused one.
1532 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1534 int loopcxt_find_unused(struct loopdev_cxt
*lc
)
1538 DBG(CXT
, ul_debugobj(lc
, "find_unused requested"));
1540 if (lc
->flags
& LOOPDEV_FL_CONTROL
) {
1543 DBG(CXT
, ul_debugobj(lc
, "using loop-control"));
1545 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1547 rc
= ioctl(ctl
, LOOP_CTL_GET_FREE
);
1550 snprintf(name
, sizeof(name
), "loop%d", rc
);
1552 rc
= loopiter_set_device(lc
, name
);
1554 lc
->control_ok
= ctl
>= 0 && rc
== 0 ? 1 : 0;
1557 DBG(CXT
, ul_debugobj(lc
, "find_unused by loop-control [rc=%d]", rc
));
1561 DBG(CXT
, ul_debugobj(lc
, "using loop scan"));
1562 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_FREE
);
1566 rc
= loopcxt_next(lc
);
1567 loopcxt_deinit_iterator(lc
);
1568 DBG(CXT
, ul_debugobj(lc
, "find_unused by scan [rc=%d]", rc
));
1576 * Return: TRUE/FALSE
1578 int loopdev_is_autoclear(const char *device
)
1580 struct loopdev_cxt lc
;
1586 rc
= loopcxt_init(&lc
, 0);
1588 rc
= loopcxt_set_device(&lc
, device
);
1590 rc
= loopcxt_is_autoclear(&lc
);
1592 loopcxt_deinit(&lc
);
1596 char *loopdev_get_backing_file(const char *device
)
1598 struct loopdev_cxt lc
;
1603 if (loopcxt_init(&lc
, 0))
1605 if (loopcxt_set_device(&lc
, device
) == 0)
1606 res
= loopcxt_get_backing_file(&lc
);
1608 loopcxt_deinit(&lc
);
1613 * Returns: TRUE/FALSE
1615 int loopdev_is_used(const char *device
, const char *filename
,
1616 uint64_t offset
, uint64_t sizelimit
, int flags
)
1618 struct loopdev_cxt lc
;
1622 if (!device
|| !filename
)
1625 rc
= loopcxt_init(&lc
, 0);
1627 rc
= loopcxt_set_device(&lc
, device
);
1631 rc
= !stat(filename
, &st
);
1632 rc
= loopcxt_is_used(&lc
, rc
? &st
: NULL
, filename
, offset
, sizelimit
, flags
);
1634 loopcxt_deinit(&lc
);
1638 int loopdev_delete(const char *device
)
1640 struct loopdev_cxt lc
;
1646 rc
= loopcxt_init(&lc
, 0);
1648 rc
= loopcxt_set_device(&lc
, device
);
1650 rc
= loopcxt_delete_device(&lc
);
1651 loopcxt_deinit(&lc
);
1656 * Returns: 0 = success, < 0 error, 1 not found
1658 int loopcxt_find_by_backing_file(struct loopdev_cxt
*lc
, const char *filename
,
1659 uint64_t offset
, uint64_t sizelimit
, int flags
)
1667 hasst
= !stat(filename
, &st
);
1669 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1673 while ((rc
= loopcxt_next(lc
)) == 0) {
1675 if (loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1676 filename
, offset
, sizelimit
, flags
))
1680 loopcxt_deinit_iterator(lc
);
1685 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1687 int loopcxt_find_overlap(struct loopdev_cxt
*lc
, const char *filename
,
1688 uint64_t offset
, uint64_t sizelimit
)
1696 DBG(CXT
, ul_debugobj(lc
, "find_overlap requested"));
1697 hasst
= !stat(filename
, &st
);
1699 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1703 while ((rc
= loopcxt_next(lc
)) == 0) {
1704 uint64_t lc_sizelimit
, lc_offset
;
1706 rc
= loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1707 filename
, offset
, sizelimit
, 0);
1709 continue; /* unused */
1713 DBG(CXT
, ul_debugobj(lc
, "found %s backed by %s",
1714 loopcxt_get_device(lc
), filename
));
1716 rc
= loopcxt_get_offset(lc
, &lc_offset
);
1718 DBG(CXT
, ul_debugobj(lc
, "failed to get offset for device %s",
1719 loopcxt_get_device(lc
)));
1722 rc
= loopcxt_get_sizelimit(lc
, &lc_sizelimit
);
1724 DBG(CXT
, ul_debugobj(lc
, "failed to get sizelimit for device %s",
1725 loopcxt_get_device(lc
)));
1730 if (lc_sizelimit
== sizelimit
&& lc_offset
== offset
) {
1731 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s (full match)",
1732 loopcxt_get_device(lc
)));
1738 if (lc_sizelimit
!= 0 && offset
>= lc_offset
+ lc_sizelimit
)
1740 if (sizelimit
!= 0 && offset
+ sizelimit
<= lc_offset
)
1743 DBG(CXT
, ul_debugobj(lc
, "overlapping loop device %s",
1744 loopcxt_get_device(lc
)));
1750 rc
= 0; /* not found */
1752 loopcxt_deinit_iterator(lc
);
1753 DBG(CXT
, ul_debugobj(lc
, "find_overlap done [rc=%d]", rc
));
1758 * Returns allocated string with device name
1760 char *loopdev_find_by_backing_file(const char *filename
, uint64_t offset
, uint64_t sizelimit
, int flags
)
1762 struct loopdev_cxt lc
;
1768 if (loopcxt_init(&lc
, 0))
1770 if (loopcxt_find_by_backing_file(&lc
, filename
, offset
, sizelimit
, flags
) == 0)
1771 res
= loopcxt_strdup_device(&lc
);
1772 loopcxt_deinit(&lc
);
1778 * Returns number of loop devices associated with @file, if only one loop
1779 * device is associated with the given @filename and @loopdev is not NULL then
1780 * @loopdev returns name of the device.
1782 int loopdev_count_by_backing_file(const char *filename
, char **loopdev
)
1784 struct loopdev_cxt lc
;
1790 rc
= loopcxt_init(&lc
, 0);
1793 if (loopcxt_init_iterator(&lc
, LOOPITER_FL_USED
))
1796 while(loopcxt_next(&lc
) == 0) {
1797 char *backing
= loopcxt_get_backing_file(&lc
);
1799 if (!backing
|| strcmp(backing
, filename
)) {
1805 if (loopdev
&& count
== 0)
1806 *loopdev
= loopcxt_strdup_device(&lc
);
1810 loopcxt_deinit(&lc
);
1812 if (loopdev
&& count
> 1) {