]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/loopdev.c
2 * No copyright is claimed. This code is in the public domain; do with
5 * Written by Karel Zak <kzak@redhat.com>
7 * -- based on mount/losetup.c
9 * Simple library for work with loop devices.
11 * - requires kernel 2.6.x
12 * - reads info from /sys/block/loop<N>/loop/<attr> (new kernels)
13 * - reads info by ioctl
14 * - supports *unlimited* number of loop devices
15 * - supports /dev/loop<N> as well as /dev/loop/<N>
16 * - minimize overhead (fd, loopinfo, ... are shared for all operations)
17 * - setup (associate device and backing file)
18 * - delete (dis-associate file)
19 * - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported
29 #include <sys/ioctl.h>
34 #include <linux/posix_types.h>
36 #include "linux_version.h"
39 #include "pathnames.h"
41 #include "canonicalize.h"
46 * Debug stuff (based on include/debug.h)
48 UL_DEBUG_DEFINE_MASK(loopdev
);
49 UL_DEBUG_DEFINE_MASKNAMES(loopdev
) = UL_DEBUG_EMPTY_MASKNAMES
;
51 #define LOOPDEV_DEBUG_INIT (1 << 1)
52 #define LOOPDEV_DEBUG_CXT (1 << 2)
53 #define LOOPDEV_DEBUG_ITER (1 << 3)
54 #define LOOPDEV_DEBUG_SETUP (1 << 4)
55 #define SFDISKPROG_DEBUG_ALL 0xFFFF
57 #define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x)
58 #define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x)
60 static void loopdev_init_debug(void)
62 if (loopdev_debug_mask
)
64 __UL_INIT_DEBUG(loopdev
, LOOPDEV_DEBUG_
, 0, LOOPDEV_DEBUG
);
70 #define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
71 #define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
72 && !loopcxt_ioctl_enabled(_lc)
76 * @device: device name, absolute device path or NULL to reset the current setting
78 * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
79 * names ("loop<N>") are converted to the path (/dev/loop<N> or to
82 * This sets the device name, but does not check if the device exists!
84 * Returns: <0 on error, 0 on success
86 int loopcxt_set_device(struct loopdev_cxt
*lc
, const char *device
)
93 DBG(CXT
, ul_debugobj(lc
, "closing old open fd"));
100 memset(&lc
->info
, 0, sizeof(lc
->info
));
104 if (*device
!= '/') {
105 const char *dir
= _PATH_DEV
;
107 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
108 if (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) {
109 if (strlen(device
) < 5)
112 dir
= _PATH_DEV_LOOP
"/"; /* _PATH_DEV uses tailing slash */
114 snprintf(lc
->device
, sizeof(lc
->device
), "%s%s",
117 strncpy(lc
->device
, device
, sizeof(lc
->device
));
118 lc
->device
[sizeof(lc
->device
) - 1] = '\0';
120 DBG(CXT
, ul_debugobj(lc
, "%s name assigned", device
));
123 sysfs_deinit(&lc
->sysfs
);
127 int loopcxt_has_device(struct loopdev_cxt
*lc
)
129 return lc
&& *lc
->device
;
134 * @flags: LOOPDEV_FL_* flags
136 * Initialize loop handler.
138 * We have two sets of the flags:
140 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
142 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
144 * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
145 * syscall to open loop device. By default is the device open read-only.
147 * The expection is loopcxt_setup_device(), where the device is open read-write
148 * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
150 * Returns: <0 on error, 0 on success.
152 int loopcxt_init(struct loopdev_cxt
*lc
, int flags
)
156 struct loopdev_cxt dummy
= UL_LOOPDEVCXT_EMPTY
;
161 loopdev_init_debug();
162 DBG(CXT
, ul_debugobj(lc
, "initialize context"));
164 memcpy(lc
, &dummy
, sizeof(dummy
));
167 rc
= loopcxt_set_device(lc
, NULL
);
171 if (stat(_PATH_SYS_BLOCK
, &st
) || !S_ISDIR(st
.st_mode
)) {
172 lc
->flags
|= LOOPDEV_FL_NOSYSFS
;
173 lc
->flags
&= ~LOOPDEV_FL_NOIOCTL
;
174 DBG(CXT
, ul_debugobj(lc
, "init: disable /sys usage"));
177 if (!(lc
->flags
& LOOPDEV_FL_NOSYSFS
) &&
178 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
180 * Use only sysfs for basic information about loop devices
182 lc
->flags
|= LOOPDEV_FL_NOIOCTL
;
183 DBG(CXT
, ul_debugobj(lc
, "init: ignore ioctls"));
186 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
) && !stat(_PATH_DEV_LOOPCTL
, &st
)) {
187 lc
->flags
|= LOOPDEV_FL_CONTROL
;
188 DBG(CXT
, ul_debugobj(lc
, "init: loop-control detected "));
197 * Deinitialize loop context
199 void loopcxt_deinit(struct loopdev_cxt
*lc
)
206 DBG(CXT
, ul_debugobj(lc
, "de-initialize"));
211 ignore_result( loopcxt_set_device(lc
, NULL
) );
212 loopcxt_deinit_iterator(lc
);
220 * Returns newly allocated device path.
222 char *loopcxt_strdup_device(struct loopdev_cxt
*lc
)
224 if (!lc
|| !*lc
->device
)
226 return strdup(lc
->device
);
232 * Returns pointer device name in the @lc struct.
234 const char *loopcxt_get_device(struct loopdev_cxt
*lc
)
236 return lc
&& *lc
->device
? lc
->device
: NULL
;
242 * Returns pointer to the sysfs context (see lib/sysfs.c)
244 struct sysfs_cxt
*loopcxt_get_sysfs(struct loopdev_cxt
*lc
)
246 if (!lc
|| !*lc
->device
|| (lc
->flags
& LOOPDEV_FL_NOSYSFS
))
249 if (!lc
->sysfs
.devno
) {
250 dev_t devno
= sysfs_devname_to_devno(lc
->device
, NULL
);
252 DBG(CXT
, ul_debugobj(lc
, "sysfs: failed devname to devno"));
255 if (sysfs_init(&lc
->sysfs
, devno
, NULL
)) {
256 DBG(CXT
, ul_debugobj(lc
, "sysfs: init failed"));
267 * Returns: file descriptor to the open loop device or <0 on error. The mode
268 * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
271 int loopcxt_get_fd(struct loopdev_cxt
*lc
)
273 if (!lc
|| !*lc
->device
)
277 lc
->mode
= lc
->flags
& LOOPDEV_FL_RDWR
? O_RDWR
: O_RDONLY
;
278 lc
->fd
= open(lc
->device
, lc
->mode
| O_CLOEXEC
);
279 DBG(CXT
, ul_debugobj(lc
, "open %s [%s]: %m", lc
->device
,
280 lc
->flags
& LOOPDEV_FL_RDWR
? "rw" : "ro"));
285 int loopcxt_set_fd(struct loopdev_cxt
*lc
, int fd
, int mode
)
297 * @flags: LOOPITER_FL_* flags
299 * Iterator allows to scan list of the free or used loop devices.
301 * Returns: <0 on error, 0 on success
303 int loopcxt_init_iterator(struct loopdev_cxt
*lc
, int flags
)
305 struct loopdev_iter
*iter
;
313 DBG(ITER
, ul_debugobj(iter
, "initialize"));
317 memset(iter
, 0, sizeof(*iter
));
320 iter
->default_check
= 1;
322 if (!lc
->extra_check
) {
324 * Check for /dev/loop/<N> subdirectory
326 if (!(lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) &&
327 stat(_PATH_DEV_LOOP
, &st
) == 0 && S_ISDIR(st
.st_mode
))
328 lc
->flags
|= LOOPDEV_FL_DEVSUBDIR
;
338 * Returns: <0 on error, 0 on success
340 int loopcxt_deinit_iterator(struct loopdev_cxt
*lc
)
342 struct loopdev_iter
*iter
;
348 DBG(ITER
, ul_debugobj(iter
, "de-initialize"));
354 closedir(iter
->sysblock
);
357 iter
->sysblock
= NULL
;
363 * Same as loopcxt_set_device, but also checks if the device is
364 * associeted with any file.
366 * Returns: <0 on error, 0 on success, 1 device does not match with
367 * LOOPITER_FL_{USED,FREE} flags.
369 static int loopiter_set_device(struct loopdev_cxt
*lc
, const char *device
)
371 int rc
= loopcxt_set_device(lc
, device
);
377 if (!(lc
->iter
.flags
& LOOPITER_FL_USED
) &&
378 !(lc
->iter
.flags
& LOOPITER_FL_FREE
))
379 return 0; /* caller does not care about device status */
381 if (!is_loopdev(lc
->device
)) {
382 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s does not exist", lc
->device
));
386 DBG(ITER
, ul_debugobj(&lc
->iter
, "%s exist", lc
->device
));
388 used
= loopcxt_get_offset(lc
, NULL
) == 0;
390 if ((lc
->iter
.flags
& LOOPITER_FL_USED
) && used
)
393 if ((lc
->iter
.flags
& LOOPITER_FL_FREE
) && !used
)
396 DBG(ITER
, ul_debugobj(&lc
->iter
, "failed to use %s device", lc
->device
));
398 ignore_result( loopcxt_set_device(lc
, NULL
) );
402 static int cmpnum(const void *p1
, const void *p2
)
404 return (((* (int *) p1
) > (* (int *) p2
)) -
405 ((* (int *) p1
) < (* (int *) p2
)));
409 * The classic scandir() is more expensive and less portable.
410 * We needn't full loop device names -- loop numbers (loop<N>)
413 static int loop_scandir(const char *dirname
, int **ary
, int hasprefix
)
417 unsigned int n
, count
= 0, arylen
= 0;
419 if (!dirname
|| !ary
)
422 DBG(ITER
, ul_debug("scan dir: %s", dirname
));
424 dir
= opendir(dirname
);
430 while((d
= readdir(dir
))) {
431 #ifdef _DIRENT_HAVE_D_TYPE
432 if (d
->d_type
!= DT_BLK
&& d
->d_type
!= DT_UNKNOWN
&&
436 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
441 if (sscanf(d
->d_name
, "loop%u", &n
) != 1)
448 n
= strtol(d
->d_name
, &end
, 10);
449 if (d
->d_name
== end
|| (end
&& *end
) || errno
)
452 if (n
< LOOPDEV_DEFAULT_NNODES
)
453 continue; /* ignore loop<0..7> */
455 if (count
+ 1 > arylen
) {
460 tmp
= realloc(*ary
, arylen
* sizeof(int));
473 qsort(*ary
, count
, sizeof(int), cmpnum
);
480 * Set the next *used* loop device according to /proc/partitions.
482 * Loop devices smaller than 512 bytes are invisible for this function.
484 static int loopcxt_next_from_proc(struct loopdev_cxt
*lc
)
486 struct loopdev_iter
*iter
= &lc
->iter
;
489 DBG(ITER
, ul_debugobj(iter
, "scan /proc/partitions"));
492 iter
->proc
= fopen(_PATH_PROC_PARTITIONS
, "r" UL_CLOEXECSTR
);
496 while (fgets(buf
, sizeof(buf
), iter
->proc
)) {
501 if (sscanf(buf
, " %u %*s %*s %128[^\n ]",
502 &m
, name
) != 2 || m
!= LOOPDEV_MAJOR
)
505 DBG(ITER
, ul_debugobj(iter
, "checking %s", name
));
507 if (loopiter_set_device(lc
, name
) == 0)
515 * Set the next *used* loop device according to
516 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
518 * This is preferred method.
520 static int loopcxt_next_from_sysfs(struct loopdev_cxt
*lc
)
522 struct loopdev_iter
*iter
= &lc
->iter
;
526 DBG(ITER
, ul_debugobj(iter
, "scanning /sys/block"));
529 iter
->sysblock
= opendir(_PATH_SYS_BLOCK
);
534 fd
= dirfd(iter
->sysblock
);
536 while ((d
= readdir(iter
->sysblock
))) {
540 DBG(ITER
, ul_debugobj(iter
, "check %s", d
->d_name
));
542 if (strcmp(d
->d_name
, ".") == 0
543 || strcmp(d
->d_name
, "..") == 0
544 || strncmp(d
->d_name
, "loop", 4) != 0)
547 snprintf(name
, sizeof(name
), "%s/loop/backing_file", d
->d_name
);
548 if (fstatat(fd
, name
, &st
, 0) != 0)
551 if (loopiter_set_device(lc
, d
->d_name
) == 0)
559 * @lc: context, has to initialized by loopcxt_init_iterator()
561 * Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
562 * about the current loop device are available by
563 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
565 int loopcxt_next(struct loopdev_cxt
*lc
)
567 struct loopdev_iter
*iter
;
577 DBG(ITER
, ul_debugobj(iter
, "next"));
579 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
581 if (iter
->flags
& LOOPITER_FL_USED
) {
584 if (loopcxt_sysfs_available(lc
))
585 rc
= loopcxt_next_from_sysfs(lc
);
587 rc
= loopcxt_next_from_proc(lc
);
593 /* B) Classic way, try first eight loop devices (default number
594 * of loop devices). This is enough for 99% of all cases.
596 if (iter
->default_check
) {
597 DBG(ITER
, ul_debugobj(iter
, "next: default check"));
598 for (++iter
->ncur
; iter
->ncur
< LOOPDEV_DEFAULT_NNODES
;
601 snprintf(name
, sizeof(name
), "loop%d", iter
->ncur
);
603 if (loopiter_set_device(lc
, name
) == 0)
606 iter
->default_check
= 0;
609 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
612 DBG(ITER
, ul_debugobj(iter
, "next: scanning /dev"));
613 iter
->nminors
= (lc
->flags
& LOOPDEV_FL_DEVSUBDIR
) ?
614 loop_scandir(_PATH_DEV_LOOP
, &iter
->minors
, 0) :
615 loop_scandir(_PATH_DEV
, &iter
->minors
, 1);
618 for (++iter
->ncur
; iter
->ncur
< iter
->nminors
; iter
->ncur
++) {
620 snprintf(name
, sizeof(name
), "loop%d", iter
->minors
[iter
->ncur
]);
622 if (loopiter_set_device(lc
, name
) == 0)
626 loopcxt_deinit_iterator(lc
);
631 * @device: path to device
633 int is_loopdev(const char *device
)
640 return (stat(device
, &st
) == 0 &&
641 S_ISBLK(st
.st_mode
) &&
642 major(st
.st_rdev
) == LOOPDEV_MAJOR
);
648 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
650 struct loop_info64
*loopcxt_get_info(struct loopdev_cxt
*lc
)
654 if (!lc
|| lc
->info_failed
) {
662 fd
= loopcxt_get_fd(lc
);
666 if (ioctl(fd
, LOOP_GET_STATUS64
, &lc
->info
) == 0) {
669 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 OK"));
674 DBG(CXT
, ul_debugobj(lc
, "reading loop_info64 FAILED"));
682 * Returns (allocated) string with path to the file assicieted
683 * with the current loop device.
685 char *loopcxt_get_backing_file(struct loopdev_cxt
*lc
)
687 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
692 * This is always preffered, the loop_info64
693 * has too small buffer for the filename.
695 res
= sysfs_strdup(sysfs
, "loop/backing_file");
697 if (!res
&& loopcxt_ioctl_enabled(lc
)) {
698 struct loop_info64
*lo
= loopcxt_get_info(lc
);
701 lo
->lo_file_name
[LO_NAME_SIZE
- 2] = '*';
702 lo
->lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
703 res
= strdup((char *) lo
->lo_file_name
);
707 DBG(CXT
, ul_debugobj(lc
, "get_backing_file [%s]", res
));
713 * @offset: returns offset number for the given device
715 * Returns: <0 on error, 0 on success
717 int loopcxt_get_offset(struct loopdev_cxt
*lc
, uint64_t *offset
)
719 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
723 rc
= sysfs_read_u64(sysfs
, "loop/offset", offset
);
725 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
726 struct loop_info64
*lo
= loopcxt_get_info(lc
);
729 *offset
= lo
->lo_offset
;
735 DBG(CXT
, ul_debugobj(lc
, "get_offset [rc=%d]", rc
));
741 * @sizelimit: returns size limit for the given device
743 * Returns: <0 on error, 0 on success
745 int loopcxt_get_sizelimit(struct loopdev_cxt
*lc
, uint64_t *size
)
747 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
751 rc
= sysfs_read_u64(sysfs
, "loop/sizelimit", size
);
753 if (rc
&& loopcxt_ioctl_enabled(lc
)) {
754 struct loop_info64
*lo
= loopcxt_get_info(lc
);
757 *size
= lo
->lo_sizelimit
;
763 DBG(CXT
, ul_debugobj(lc
, "get_sizelimit [rc=%d]", rc
));
769 * @devno: returns encryption type
771 * Cryptoloop is DEPRECATED!
773 * Returns: <0 on error, 0 on success
775 int loopcxt_get_encrypt_type(struct loopdev_cxt
*lc
, uint32_t *type
)
777 struct loop_info64
*lo
= loopcxt_get_info(lc
);
780 /* not provided by sysfs */
783 *type
= lo
->lo_encrypt_type
;
788 DBG(CXT
, ul_debugobj(lc
, "get_encrypt_type [rc=%d]", rc
));
794 * @devno: returns crypt name
796 * Cryptoloop is DEPRECATED!
798 * Returns: <0 on error, 0 on success
800 const char *loopcxt_get_crypt_name(struct loopdev_cxt
*lc
)
802 struct loop_info64
*lo
= loopcxt_get_info(lc
);
805 return (char *) lo
->lo_crypt_name
;
807 DBG(CXT
, ul_debugobj(lc
, "get_crypt_name failed"));
813 * @devno: returns backing file devno
815 * Returns: <0 on error, 0 on success
817 int loopcxt_get_backing_devno(struct loopdev_cxt
*lc
, dev_t
*devno
)
819 struct loop_info64
*lo
= loopcxt_get_info(lc
);
824 *devno
= lo
->lo_device
;
829 DBG(CXT
, ul_debugobj(lc
, "get_backing_devno [rc=%d]", rc
));
835 * @ino: returns backing file inode
837 * Returns: <0 on error, 0 on success
839 int loopcxt_get_backing_inode(struct loopdev_cxt
*lc
, ino_t
*ino
)
841 struct loop_info64
*lo
= loopcxt_get_info(lc
);
851 DBG(CXT
, ul_debugobj(lc
, "get_backing_inode [rc=%d]", rc
));
856 * Check if the kernel supports partitioned loop devices.
859 * - kernels < 3.2 support partitioned loop devices and PT scanning
860 * only if max_part= module parameter is non-zero
862 * - kernels >= 3.2 always support partitioned loop devices
864 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
866 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
867 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
870 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
872 int loopmod_supports_partscan(void)
877 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
880 f
= fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR
);
883 rc
= fscanf(f
, "%d", &ret
);
885 return rc
== 1 ? ret
: 0;
891 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
892 * scanning is enabled for all loop devices.
894 int loopcxt_is_partscan(struct loopdev_cxt
*lc
)
896 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
901 if (sysfs_read_int(sysfs
, "loop/partscan", &fl
) == 0)
905 /* old kernels (including kernels without loopN/loop/<flags> directory */
906 return loopmod_supports_partscan();
912 * Returns: 1 if the autoclear flags is set.
914 int loopcxt_is_autoclear(struct loopdev_cxt
*lc
)
916 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
920 if (sysfs_read_int(sysfs
, "loop/autoclear", &fl
) == 0)
924 if (loopcxt_ioctl_enabled(lc
)) {
925 struct loop_info64
*lo
= loopcxt_get_info(lc
);
927 return lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
;
935 * Returns: 1 if the readonly flags is set.
937 int loopcxt_is_readonly(struct loopdev_cxt
*lc
)
939 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
943 if (sysfs_read_int(sysfs
, "ro", &fl
) == 0)
947 if (loopcxt_ioctl_enabled(lc
)) {
948 struct loop_info64
*lo
= loopcxt_get_info(lc
);
950 return lo
->lo_flags
& LO_FLAGS_READ_ONLY
;
958 * Returns: 1 if the dio flags is set.
960 int loopcxt_is_dio(struct loopdev_cxt
*lc
)
962 struct sysfs_cxt
*sysfs
= loopcxt_get_sysfs(lc
);
966 if (sysfs_read_int(sysfs
, "loop/dio", &fl
) == 0)
969 if (loopcxt_ioctl_enabled(lc
)) {
970 struct loop_info64
*lo
= loopcxt_get_info(lc
);
972 return lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
979 * @st: backing file stat or NULL
980 * @backing_file: filename
982 * @flags: LOOPDEV_FL_OFFSET if @offset should not be ignored
984 * Returns 1 if the current @lc loopdev is associated with the given backing
985 * file. Note that the preferred way is to use devno and inode number rather
986 * than filename. The @backing_file filename is poor solution usable in case
987 * that you don't have rights to call stat().
989 * Don't forget that old kernels provide very restricted (in size) backing
990 * filename by LOOP_GET_STAT64 ioctl only.
992 int loopcxt_is_used(struct loopdev_cxt
*lc
,
994 const char *backing_file
,
1004 DBG(CXT
, ul_debugobj(lc
, "checking %s vs. %s",
1005 loopcxt_get_device(lc
),
1008 if (st
&& loopcxt_get_backing_inode(lc
, &ino
) == 0 &&
1009 loopcxt_get_backing_devno(lc
, &dev
) == 0) {
1011 if (ino
== st
->st_ino
&& dev
== st
->st_dev
)
1014 /* don't use filename if we have devno and inode */
1018 /* poor man's solution */
1020 char *name
= loopcxt_get_backing_file(lc
);
1021 int rc
= name
&& strcmp(name
, backing_file
) == 0;
1030 if (flags
& LOOPDEV_FL_OFFSET
) {
1033 return loopcxt_get_offset(lc
, &off
) == 0 && off
== offset
;
1039 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1041 int loopcxt_set_offset(struct loopdev_cxt
*lc
, uint64_t offset
)
1045 lc
->info
.lo_offset
= offset
;
1047 DBG(CXT
, ul_debugobj(lc
, "set offset=%jd", offset
));
1052 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1054 int loopcxt_set_sizelimit(struct loopdev_cxt
*lc
, uint64_t sizelimit
)
1058 lc
->info
.lo_sizelimit
= sizelimit
;
1060 DBG(CXT
, ul_debugobj(lc
, "set sizelimit=%jd", sizelimit
));
1066 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1068 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1070 * Returns: 0 on success, <0 on error.
1072 int loopcxt_set_flags(struct loopdev_cxt
*lc
, uint32_t flags
)
1076 lc
->info
.lo_flags
= flags
;
1078 DBG(CXT
, ul_debugobj(lc
, "set flags=%u", (unsigned) flags
));
1084 * @filename: backing file path (the path will be canonicalized)
1086 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1088 * Returns: 0 on success, <0 on error.
1090 int loopcxt_set_backing_file(struct loopdev_cxt
*lc
, const char *filename
)
1095 lc
->filename
= canonicalize_path(filename
);
1099 strncpy((char *)lc
->info
.lo_file_name
, lc
->filename
, LO_NAME_SIZE
);
1100 lc
->info
.lo_file_name
[LO_NAME_SIZE
- 1] = '\0';
1102 DBG(CXT
, ul_debugobj(lc
, "set backing file=%s", lc
->info
.lo_file_name
));
1107 * In kernels prior to v3.9, if the offset or sizelimit options
1108 * are used, the block device's size won't be synced automatically.
1109 * blockdev --getsize64 and filesystems will use the backing
1110 * file size until the block device has been re-opened or the
1111 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1113 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1114 * the open file descriptor to the mount system call, we need to use
1115 * the ioctl. Calling losetup directly doesn't have this problem since
1116 * it closes the device when it exits and whatever consumes the device
1117 * next will re-open it, causing the resync.
1119 static int loopcxt_check_size(struct loopdev_cxt
*lc
, int file_fd
)
1121 uint64_t size
, expected_size
;
1125 if (!lc
->info
.lo_offset
&& !lc
->info
.lo_sizelimit
)
1128 if (fstat(file_fd
, &st
)) {
1129 DBG(CXT
, ul_debugobj(lc
, "failed to fstat backing file"));
1132 if (S_ISBLK(st
.st_mode
)) {
1133 if (blkdev_get_size(file_fd
,
1134 (unsigned long long *) &expected_size
)) {
1135 DBG(CXT
, ul_debugobj(lc
, "failed to determine device size"));
1139 expected_size
= st
.st_size
;
1141 if (expected_size
== 0 || expected_size
<= lc
->info
.lo_offset
) {
1142 DBG(CXT
, ul_debugobj(lc
, "failed to determine expected size"));
1143 return 0; /* ignore this error */
1146 if (lc
->info
.lo_offset
> 0)
1147 expected_size
-= lc
->info
.lo_offset
;
1149 if (lc
->info
.lo_sizelimit
> 0 && lc
->info
.lo_sizelimit
< expected_size
)
1150 expected_size
= lc
->info
.lo_sizelimit
;
1152 dev_fd
= loopcxt_get_fd(lc
);
1154 DBG(CXT
, ul_debugobj(lc
, "failed to get loop FD"));
1158 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
)) {
1159 DBG(CXT
, ul_debugobj(lc
, "failed to determine loopdev size"));
1163 /* It's block device, so, align to 512-byte sectors */
1164 if (expected_size
% 512) {
1165 DBG(CXT
, ul_debugobj(lc
, "expected size misaligned to 512-byte sectors"));
1166 expected_size
= (expected_size
>> 9) << 9;
1169 if (expected_size
!= size
) {
1170 DBG(CXT
, ul_debugobj(lc
, "warning: loopdev and expected "
1171 "size mismatch (%ju/%ju)",
1172 size
, expected_size
));
1174 if (loopcxt_set_capacity(lc
)) {
1175 /* ioctl not available */
1176 if (errno
== ENOTTY
|| errno
== EINVAL
)
1181 if (blkdev_get_size(dev_fd
, (unsigned long long *) &size
))
1184 if (expected_size
!= size
) {
1186 DBG(CXT
, ul_debugobj(lc
, "failed to set loopdev size, "
1187 "size: %ju, expected: %ju",
1188 size
, expected_size
));
1199 * Associate the current device (see loopcxt_{set,get}_device()) with
1200 * a file (see loopcxt_set_backing_file()).
1202 * The device is initialized read-write by default. If you want read-only
1203 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1204 * flags are ignored and modified according to LO_FLAGS_*.
1206 * If the device is already open by loopcxt_get_fd() then this setup device
1207 * function will re-open the device to fix read/write mode.
1209 * The device is also initialized read-only if the backing file is not
1210 * possible to open read-write (e.g. read-only FS).
1212 * Returns: <0 on error, 0 on success.
1214 int loopcxt_setup_device(struct loopdev_cxt
*lc
)
1216 int file_fd
, dev_fd
, mode
= O_RDWR
, rc
= -1, cnt
= 0;
1219 if (!lc
|| !*lc
->device
|| !lc
->filename
)
1222 DBG(SETUP
, ul_debugobj(lc
, "device setup requested"));
1225 * Open backing file and device
1227 if (lc
->info
.lo_flags
& LO_FLAGS_READ_ONLY
)
1230 if ((file_fd
= open(lc
->filename
, mode
| O_CLOEXEC
)) < 0) {
1231 if (mode
!= O_RDONLY
&& (errno
== EROFS
|| errno
== EACCES
))
1232 file_fd
= open(lc
->filename
, mode
= O_RDONLY
);
1235 DBG(SETUP
, ul_debugobj(lc
, "open backing file failed: %m"));
1239 DBG(SETUP
, ul_debugobj(lc
, "backing file open: OK"));
1241 if (lc
->fd
!= -1 && lc
->mode
!= mode
) {
1242 DBG(SETUP
, ul_debugobj(lc
, "closing already open device (mode mismatch)"));
1248 if (mode
== O_RDONLY
) {
1249 lc
->flags
|= LOOPDEV_FL_RDONLY
; /* open() mode */
1250 lc
->info
.lo_flags
|= LO_FLAGS_READ_ONLY
; /* kernel loopdev mode */
1252 lc
->flags
|= LOOPDEV_FL_RDWR
; /* open() mode */
1253 lc
->info
.lo_flags
&= ~LO_FLAGS_READ_ONLY
;
1254 lc
->flags
&= ~LOOPDEV_FL_RDONLY
;
1259 dev_fd
= loopcxt_get_fd(lc
);
1260 if (dev_fd
>= 0 || lc
->control_ok
== 0)
1262 if (errno
!= EACCES
&& errno
!= ENOENT
)
1264 /* We have permissions to open /dev/loop-control, but open
1265 * /dev/loopN failed with EACCES, it's probably because udevd
1266 * does not applied chown yet. Let's wait a moment. */
1268 } while (cnt
++ < 16);
1275 DBG(SETUP
, ul_debugobj(lc
, "device open: OK"));
1280 if (ioctl(dev_fd
, LOOP_SET_FD
, file_fd
) < 0) {
1283 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD failed: %m"));
1287 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_FD: OK"));
1289 if (ioctl(dev_fd
, LOOP_SET_STATUS64
, &lc
->info
)) {
1292 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64 failed: %m"));
1296 DBG(SETUP
, ul_debugobj(lc
, "LOOP_SET_STATUS64: OK"));
1298 if ((rc
= loopcxt_check_size(lc
, file_fd
)))
1303 memset(&lc
->info
, 0, sizeof(lc
->info
));
1305 lc
->info_failed
= 0;
1307 DBG(SETUP
, ul_debugobj(lc
, "success [rc=0]"));
1312 if (dev_fd
>= 0 && rc
!= -EBUSY
)
1313 ioctl(dev_fd
, LOOP_CLR_FD
, 0);
1317 DBG(SETUP
, ul_debugobj(lc
, "failed [rc=%d]", rc
));
1321 int loopcxt_set_capacity(struct loopdev_cxt
*lc
)
1323 int fd
= loopcxt_get_fd(lc
);
1328 /* Kernels prior to v2.6.30 don't support this ioctl */
1329 if (ioctl(fd
, LOOP_SET_CAPACITY
, 0) < 0) {
1331 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_CAPACITY failed: %m"));
1335 DBG(CXT
, ul_debugobj(lc
, "capacity set"));
1339 int loopcxt_set_dio(struct loopdev_cxt
*lc
, unsigned long use_dio
)
1341 int fd
= loopcxt_get_fd(lc
);
1346 /* Kernels prior to v4.4 don't support this ioctl */
1347 if (ioctl(fd
, LOOP_SET_DIRECT_IO
, use_dio
) < 0) {
1349 DBG(CXT
, ul_debugobj(lc
, "LOOP_SET_DIRECT_IO failed: %m"));
1353 DBG(CXT
, ul_debugobj(lc
, "direct io set"));
1357 int loopcxt_delete_device(struct loopdev_cxt
*lc
)
1359 int fd
= loopcxt_get_fd(lc
);
1364 if (ioctl(fd
, LOOP_CLR_FD
, 0) < 0) {
1365 DBG(CXT
, ul_debugobj(lc
, "LOOP_CLR_FD failed: %m"));
1369 DBG(CXT
, ul_debugobj(lc
, "device removed"));
1373 int loopcxt_add_device(struct loopdev_cxt
*lc
)
1377 const char *p
, *dev
= loopcxt_get_device(lc
);
1382 if (!(lc
->flags
& LOOPDEV_FL_CONTROL
)) {
1387 p
= strrchr(dev
, '/');
1388 if (!p
|| (sscanf(p
, "/loop%d", &nr
) != 1 && sscanf(p
, "/%d", &nr
) != 1)
1392 ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1394 DBG(CXT
, ul_debugobj(lc
, "add_device %d", nr
));
1395 rc
= ioctl(ctl
, LOOP_CTL_ADD
, nr
);
1398 lc
->control_ok
= rc
>= 0 ? 1 : 0;
1400 DBG(CXT
, ul_debugobj(lc
, "add_device done [rc=%d]", rc
));
1405 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1406 * kernels we have to check all loop devices to found unused one.
1408 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1410 int loopcxt_find_unused(struct loopdev_cxt
*lc
)
1414 DBG(CXT
, ul_debugobj(lc
, "find_unused requested"));
1416 if (lc
->flags
& LOOPDEV_FL_CONTROL
) {
1417 int ctl
= open(_PATH_DEV_LOOPCTL
, O_RDWR
|O_CLOEXEC
);
1420 rc
= ioctl(ctl
, LOOP_CTL_GET_FREE
);
1423 snprintf(name
, sizeof(name
), "loop%d", rc
);
1425 rc
= loopiter_set_device(lc
, name
);
1427 lc
->control_ok
= ctl
>= 0 && rc
== 0 ? 1 : 0;
1430 DBG(CXT
, ul_debugobj(lc
, "find_unused by loop-control [rc=%d]", rc
));
1434 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_FREE
);
1438 rc
= loopcxt_next(lc
);
1439 loopcxt_deinit_iterator(lc
);
1440 DBG(CXT
, ul_debugobj(lc
, "find_unused by scan [rc=%d]", rc
));
1448 * Return: TRUE/FALSE
1450 int loopdev_is_autoclear(const char *device
)
1452 struct loopdev_cxt lc
;
1458 rc
= loopcxt_init(&lc
, 0);
1460 rc
= loopcxt_set_device(&lc
, device
);
1462 rc
= loopcxt_is_autoclear(&lc
);
1464 loopcxt_deinit(&lc
);
1468 char *loopdev_get_backing_file(const char *device
)
1470 struct loopdev_cxt lc
;
1475 if (loopcxt_init(&lc
, 0))
1477 if (loopcxt_set_device(&lc
, device
) == 0)
1478 res
= loopcxt_get_backing_file(&lc
);
1480 loopcxt_deinit(&lc
);
1485 * Returns: TRUE/FALSE
1487 int loopdev_is_used(const char *device
, const char *filename
,
1488 uint64_t offset
, int flags
)
1490 struct loopdev_cxt lc
;
1494 if (!device
|| !filename
)
1497 rc
= loopcxt_init(&lc
, 0);
1499 rc
= loopcxt_set_device(&lc
, device
);
1503 rc
= !stat(filename
, &st
);
1504 rc
= loopcxt_is_used(&lc
, rc
? &st
: NULL
, filename
, offset
, flags
);
1506 loopcxt_deinit(&lc
);
1510 int loopdev_delete(const char *device
)
1512 struct loopdev_cxt lc
;
1518 rc
= loopcxt_init(&lc
, 0);
1520 rc
= loopcxt_set_device(&lc
, device
);
1522 rc
= loopcxt_delete_device(&lc
);
1523 loopcxt_deinit(&lc
);
1528 * Returns: 0 = success, < 0 error, 1 not found
1530 int loopcxt_find_by_backing_file(struct loopdev_cxt
*lc
, const char *filename
,
1531 uint64_t offset
, int flags
)
1539 hasst
= !stat(filename
, &st
);
1541 rc
= loopcxt_init_iterator(lc
, LOOPITER_FL_USED
);
1545 while ((rc
= loopcxt_next(lc
)) == 0) {
1547 if (loopcxt_is_used(lc
, hasst
? &st
: NULL
,
1548 filename
, offset
, flags
))
1552 loopcxt_deinit_iterator(lc
);
1557 * Returns allocated string with device name
1559 char *loopdev_find_by_backing_file(const char *filename
, uint64_t offset
, int flags
)
1561 struct loopdev_cxt lc
;
1567 if (loopcxt_init(&lc
, 0))
1569 if (loopcxt_find_by_backing_file(&lc
, filename
, offset
, flags
) == 0)
1570 res
= loopcxt_strdup_device(&lc
);
1571 loopcxt_deinit(&lc
);
1577 * Returns number of loop devices associated with @file, if only one loop
1578 * device is associeted with the given @filename and @loopdev is not NULL then
1579 * @loopdev returns name of the device.
1581 int loopdev_count_by_backing_file(const char *filename
, char **loopdev
)
1583 struct loopdev_cxt lc
;
1589 rc
= loopcxt_init(&lc
, 0);
1592 if (loopcxt_init_iterator(&lc
, LOOPITER_FL_USED
))
1595 while(loopcxt_next(&lc
) == 0) {
1596 char *backing
= loopcxt_get_backing_file(&lc
);
1598 if (!backing
|| strcmp(backing
, filename
)) {
1604 if (loopdev
&& count
== 0)
1605 *loopdev
= loopcxt_strdup_device(&lc
);
1609 loopcxt_deinit(&lc
);
1611 if (loopdev
&& count
> 1) {