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>
36 #ifdef HAVE_SYS_SYSMACROS_H
37 #include <sys/sysmacros.h> /* for major, minor */
40 #include "utils_loop.h"
41 #include "libcryptsetup_macros.h"
43 #define LOOP_DEV_MAJOR 7
45 #ifndef LO_FLAGS_AUTOCLEAR
46 #define LO_FLAGS_AUTOCLEAR 4
49 #ifndef LOOP_CTL_GET_FREE
50 #define LOOP_CTL_GET_FREE 0x4C82
53 #ifndef LOOP_SET_CAPACITY
54 #define LOOP_SET_CAPACITY 0x4C07
57 #ifndef LOOP_SET_BLOCK_SIZE
58 #define LOOP_SET_BLOCK_SIZE 0x4C09
61 #ifndef LOOP_CONFIGURE
62 #define LOOP_CONFIGURE 0x4C0A
66 struct loop_info64 info;
71 static char *crypt_loop_get_device_old(void)
75 struct loop_info64 lo64 = {0};
77 for (i = 0; i < 256; i++) {
78 sprintf(dev, "/dev/loop%d", i);
80 loop_fd = open(dev, O_RDONLY);
84 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
95 static char *crypt_loop_get_device(void)
101 loop_fd = open("/dev/loop-control", O_RDONLY);
103 return crypt_loop_get_device_old();
105 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
112 if (sprintf(dev, "/dev/loop%d", i) < 0)
115 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
121 int crypt_loop_attach(char **loop, const char *file, int offset,
122 int autoclear, int *readonly, size_t blocksize)
124 struct loop_config config = {0};
126 int loop_fd = -1, file_fd = -1, r = 1;
131 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
132 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
134 file_fd = open(file, O_RDONLY | O_EXCL);
141 lo_file_name = (char*)config.info.lo_file_name;
142 lo_file_name[LO_NAME_SIZE-1] = '\0';
143 strncpy(lo_file_name, file, LO_NAME_SIZE-1);
144 config.info.lo_offset = offset;
146 config.info.lo_flags |= LO_FLAGS_AUTOCLEAR;
147 if (blocksize > SECTOR_SIZE)
148 config.block_size = blocksize;
150 while (loop_fd < 0) {
151 *loop = crypt_loop_get_device();
155 loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
158 if (ioctl(loop_fd, LOOP_CONFIGURE, &config) < 0) {
159 if (errno == EINVAL || errno == ENOTTY) {
166 /* kernel doesn't support LOOP_CONFIGURE */
181 while (loop_fd < 0) {
182 *loop = crypt_loop_get_device();
186 loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
189 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
200 if (blocksize > SECTOR_SIZE)
201 (void)ioctl(loop_fd, LOOP_SET_BLOCK_SIZE, (unsigned long)blocksize);
203 if (ioctl(loop_fd, LOOP_SET_STATUS64, &config.info) < 0) {
204 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
209 /* Verify that autoclear is really set */
211 memset(&config.info, 0, sizeof(config.info));
212 if (ioctl(loop_fd, LOOP_GET_STATUS64, &config.info) < 0 ||
213 !(config.info.lo_flags & LO_FLAGS_AUTOCLEAR)) {
214 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
221 if (r && loop_fd >= 0)
229 return r ? -1 : loop_fd;
232 int crypt_loop_detach(const char *loop)
234 int loop_fd = -1, r = 1;
236 loop_fd = open(loop, O_RDONLY);
240 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
247 int crypt_loop_resize(const char *loop)
249 int loop_fd = -1, r = 1;
251 loop_fd = open(loop, O_RDONLY);
255 if (!ioctl(loop_fd, LOOP_SET_CAPACITY, 0))
262 static char *_ioctl_backing_file(const char *loop)
264 struct loop_info64 lo64 = {0};
267 loop_fd = open(loop, O_RDONLY);
271 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
276 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
277 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
281 return strdup((char*)lo64.lo_file_name);
284 static char *_sysfs_backing_file(const char *loop)
291 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
294 if (snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
295 major(st.st_rdev), minor(st.st_rdev)) < 0)
298 fd = open(buf, O_RDONLY);
302 len = read(fd, buf, PATH_MAX);
311 char *crypt_loop_backing_file(const char *loop)
315 if (!crypt_loop_device(loop))
318 bf = _sysfs_backing_file(loop);
319 return bf ?: _ioctl_backing_file(loop);
322 int crypt_loop_device(const char *loop)
329 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
330 major(st.st_rdev) != LOOP_DEV_MAJOR)