2 * loopback block device utilities
4 * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2009-2021 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 #include <sys/types.h>
32 #ifdef HAVE_SYS_SYSMACROS_H
33 # include <sys/sysmacros.h> /* for major, minor */
35 #include <linux/loop.h>
37 #include "utils_loop.h"
39 #define LOOP_DEV_MAJOR 7
41 #ifndef LO_FLAGS_AUTOCLEAR
42 #define LO_FLAGS_AUTOCLEAR 4
45 #ifndef LOOP_CTL_GET_FREE
46 #define LOOP_CTL_GET_FREE 0x4C82
49 #ifndef LOOP_SET_CAPACITY
50 #define LOOP_SET_CAPACITY 0x4C07
53 static char *crypt_loop_get_device_old(void)
57 struct loop_info64 lo64 = {0};
59 for (i = 0; i < 256; i++) {
60 sprintf(dev, "/dev/loop%d", i);
62 loop_fd = open(dev, O_RDONLY);
66 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
77 static char *crypt_loop_get_device(void)
83 loop_fd = open("/dev/loop-control", O_RDONLY);
85 return crypt_loop_get_device_old();
87 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
94 if (sprintf(dev, "/dev/loop%d", i) < 0)
97 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
103 int crypt_loop_attach(char **loop, const char *file, int offset,
104 int autoclear, int *readonly)
106 struct loop_info64 lo64 = {0};
108 int loop_fd = -1, file_fd = -1, r = 1;
112 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
113 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
115 file_fd = open(file, O_RDONLY | O_EXCL);
120 while (loop_fd < 0) {
121 *loop = crypt_loop_get_device();
125 loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
129 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
140 lo_file_name = (char*)lo64.lo_file_name;
141 lo_file_name[LO_NAME_SIZE-1] = '\0';
142 strncpy(lo_file_name, file, LO_NAME_SIZE-1);
143 lo64.lo_offset = offset;
145 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
147 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
148 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
152 /* Verify that autoclear is really set */
154 memset(&lo64, 0, sizeof(lo64));
155 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
156 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
157 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
164 if (r && loop_fd >= 0)
172 return r ? -1 : loop_fd;
175 int crypt_loop_detach(const char *loop)
177 int loop_fd = -1, r = 1;
179 loop_fd = open(loop, O_RDONLY);
183 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
190 int crypt_loop_resize(const char *loop)
192 int loop_fd = -1, r = 1;
194 loop_fd = open(loop, O_RDONLY);
198 if (!ioctl(loop_fd, LOOP_SET_CAPACITY, 0))
205 static char *_ioctl_backing_file(const char *loop)
207 struct loop_info64 lo64 = {0};
210 loop_fd = open(loop, O_RDONLY);
214 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
219 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
220 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
224 return strdup((char*)lo64.lo_file_name);
227 static char *_sysfs_backing_file(const char *loop)
234 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
237 if (snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
238 major(st.st_rdev), minor(st.st_rdev)) < 0)
241 fd = open(buf, O_RDONLY);
245 len = read(fd, buf, PATH_MAX);
254 char *crypt_loop_backing_file(const char *loop)
258 if (!crypt_loop_device(loop))
261 bf = _sysfs_backing_file(loop);
262 return bf ?: _ioctl_backing_file(loop);
265 int crypt_loop_device(const char *loop)
272 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
273 major(st.st_rdev) != LOOP_DEV_MAJOR)