2 * loopback block device utilities
4 * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2009-2020 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>
36 #ifdef HAVE_SYS_SYSMACROS_H
37 #include <sys/sysmacros.h> /* for major, minor */
40 #include "utils_loop.h"
42 #define LOOP_DEV_MAJOR 7
44 #ifndef LO_FLAGS_AUTOCLEAR
45 #define LO_FLAGS_AUTOCLEAR 4
48 #ifndef LOOP_CTL_GET_FREE
49 #define LOOP_CTL_GET_FREE 0x4C82
52 #ifndef LOOP_SET_CAPACITY
53 #define LOOP_SET_CAPACITY 0x4C07
56 static char *crypt_loop_get_device_old(void)
60 struct loop_info64 lo64 = {0};
62 for (i = 0; i < 256; i++) {
63 sprintf(dev, "/dev/loop%d", i);
65 loop_fd = open(dev, O_RDONLY);
69 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
80 static char *crypt_loop_get_device(void)
86 loop_fd = open("/dev/loop-control", O_RDONLY);
88 return crypt_loop_get_device_old();
90 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
97 if (sprintf(dev, "/dev/loop%d", i) < 0)
100 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
106 int crypt_loop_attach(char **loop, const char *file, int offset,
107 int autoclear, int *readonly)
109 struct loop_info64 lo64 = {0};
111 int loop_fd = -1, file_fd = -1, r = 1;
115 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
116 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
118 file_fd = open(file, O_RDONLY | O_EXCL);
123 while (loop_fd < 0) {
124 *loop = crypt_loop_get_device();
128 loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
132 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
143 lo_file_name = (char*)lo64.lo_file_name;
144 lo_file_name[LO_NAME_SIZE-1] = '\0';
145 strncpy(lo_file_name, file, LO_NAME_SIZE-1);
146 lo64.lo_offset = offset;
148 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
150 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
151 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
155 /* Verify that autoclear is really set */
157 memset(&lo64, 0, sizeof(lo64));
158 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
159 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
160 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
167 if (r && loop_fd >= 0)
175 return r ? -1 : loop_fd;
178 int crypt_loop_detach(const char *loop)
180 int loop_fd = -1, r = 1;
182 loop_fd = open(loop, O_RDONLY);
186 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
193 int crypt_loop_resize(const char *loop)
195 int loop_fd = -1, r = 1;
197 loop_fd = open(loop, O_RDONLY);
201 if (!ioctl(loop_fd, LOOP_SET_CAPACITY, 0))
208 static char *_ioctl_backing_file(const char *loop)
210 struct loop_info64 lo64 = {0};
213 loop_fd = open(loop, O_RDONLY);
217 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
222 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
223 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
227 return strdup((char*)lo64.lo_file_name);
230 static char *_sysfs_backing_file(const char *loop)
237 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
240 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
241 major(st.st_rdev), minor(st.st_rdev));
243 fd = open(buf, O_RDONLY);
247 len = read(fd, buf, PATH_MAX);
256 char *crypt_loop_backing_file(const char *loop)
260 if (!crypt_loop_device(loop))
263 bf = _sysfs_backing_file(loop);
264 return bf ?: _ioctl_backing_file(loop);
267 int crypt_loop_device(const char *loop)
274 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
275 major(st.st_rdev) != LOOP_DEV_MAJOR)