2 * loopback block device utilities
4 * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2009-2023 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/ioctl.h>
31 #ifdef HAVE_SYS_SYSMACROS_H
32 # include <sys/sysmacros.h> /* for major, minor */
34 #include <linux/types.h>
35 #include <linux/loop.h>
37 #include "utils_loop.h"
38 #include "libcryptsetup_macros.h"
40 #define LOOP_DEV_MAJOR 7
42 #ifndef LO_FLAGS_AUTOCLEAR
43 #define LO_FLAGS_AUTOCLEAR 4
46 #ifndef LOOP_CTL_GET_FREE
47 #define LOOP_CTL_GET_FREE 0x4C82
50 #ifndef LOOP_SET_CAPACITY
51 #define LOOP_SET_CAPACITY 0x4C07
54 #ifndef LOOP_SET_BLOCK_SIZE
55 #define LOOP_SET_BLOCK_SIZE 0x4C09
58 #ifndef LOOP_CONFIGURE
59 #define LOOP_CONFIGURE 0x4C0A
63 struct loop_info64 info;
68 static char *crypt_loop_get_device_old(void)
72 struct loop_info64 lo64 = {0};
74 for (i = 0; i < 256; i++) {
75 sprintf(dev, "/dev/loop%d", i);
77 loop_fd = open(dev, O_RDONLY);
81 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
92 static char *crypt_loop_get_device(void)
98 loop_fd = open("/dev/loop-control", O_RDONLY);
100 return crypt_loop_get_device_old();
102 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
109 if (sprintf(dev, "/dev/loop%d", i) < 0)
112 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
118 int crypt_loop_attach(char **loop, const char *file, int offset,
119 int autoclear, int *readonly, size_t blocksize)
121 struct loop_config config = {0};
123 int loop_fd = -1, file_fd = -1, r = 1;
128 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
129 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
131 file_fd = open(file, O_RDONLY | O_EXCL);
138 lo_file_name = (char*)config.info.lo_file_name;
139 lo_file_name[LO_NAME_SIZE-1] = '\0';
140 strncpy(lo_file_name, file, LO_NAME_SIZE-1);
141 config.info.lo_offset = offset;
143 config.info.lo_flags |= LO_FLAGS_AUTOCLEAR;
144 if (blocksize > SECTOR_SIZE)
145 config.block_size = blocksize;
147 while (loop_fd < 0) {
148 *loop = crypt_loop_get_device();
152 loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
155 if (ioctl(loop_fd, LOOP_CONFIGURE, &config) < 0) {
156 if (errno == EINVAL || errno == ENOTTY) {
163 /* kernel doesn't support LOOP_CONFIGURE */
178 while (loop_fd < 0) {
179 *loop = crypt_loop_get_device();
183 loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
186 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
197 if (blocksize > SECTOR_SIZE)
198 (void)ioctl(loop_fd, LOOP_SET_BLOCK_SIZE, (unsigned long)blocksize);
200 if (ioctl(loop_fd, LOOP_SET_STATUS64, &config.info) < 0) {
201 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
206 /* Verify that autoclear is really set */
208 memset(&config.info, 0, sizeof(config.info));
209 if (ioctl(loop_fd, LOOP_GET_STATUS64, &config.info) < 0 ||
210 !(config.info.lo_flags & LO_FLAGS_AUTOCLEAR)) {
211 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
218 if (r && loop_fd >= 0)
226 return r ? -1 : loop_fd;
229 int crypt_loop_detach(const char *loop)
231 int loop_fd = -1, r = 1;
233 loop_fd = open(loop, O_RDONLY);
237 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
244 int crypt_loop_resize(const char *loop)
246 int loop_fd = -1, r = 1;
248 loop_fd = open(loop, O_RDONLY);
252 if (!ioctl(loop_fd, LOOP_SET_CAPACITY, 0))
259 static char *_ioctl_backing_file(const char *loop)
261 struct loop_info64 lo64 = {0};
264 loop_fd = open(loop, O_RDONLY);
268 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
273 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
274 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
278 return strdup((char*)lo64.lo_file_name);
281 static char *_sysfs_backing_file(const char *loop)
288 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
291 if (snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
292 major(st.st_rdev), minor(st.st_rdev)) < 0)
295 fd = open(buf, O_RDONLY);
299 len = read(fd, buf, PATH_MAX);
308 char *crypt_loop_backing_file(const char *loop)
312 if (!crypt_loop_device(loop))
315 bf = _sysfs_backing_file(loop);
316 return bf ?: _ioctl_backing_file(loop);
319 int crypt_loop_device(const char *loop)
326 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
327 major(st.st_rdev) != LOOP_DEV_MAJOR)