2 * loopback block device utilities
4 * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2009-2012, 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 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 #include <linux/loop.h>
32 #include "utils_loop.h"
34 #define LOOP_DEV_MAJOR 7
36 #ifndef LO_FLAGS_AUTOCLEAR
37 #define LO_FLAGS_AUTOCLEAR 4
40 #ifndef LOOP_CTL_GET_FREE
41 #define LOOP_CTL_GET_FREE 0x4C82
44 static char *crypt_loop_get_device_old(void)
48 struct loop_info64 lo64 = {0};
50 for (i = 0; i < 256; i++) {
51 sprintf(dev, "/dev/loop%d", i);
53 loop_fd = open(dev, O_RDONLY);
57 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
68 char *crypt_loop_get_device(void)
74 loop_fd = open("/dev/loop-control", O_RDONLY);
76 return crypt_loop_get_device_old();
78 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
85 if (sprintf(dev, "/dev/loop%d", i) < 0)
88 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
94 int crypt_loop_attach(const char *loop, const char *file, int offset,
95 int autoclear, int *readonly)
97 struct loop_info64 lo64 = {0};
98 int loop_fd = -1, file_fd = -1, r = 1;
100 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
101 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
103 file_fd = open(file, O_RDONLY | O_EXCL);
108 loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
112 strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
113 lo64.lo_offset = offset;
115 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
117 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
120 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
121 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
125 /* Verify that autoclear is really set */
127 memset(&lo64, 0, sizeof(lo64));
128 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
129 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
130 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
137 if (r && loop_fd >= 0)
141 return r ? -1 : loop_fd;
144 int crypt_loop_detach(const char *loop)
146 int loop_fd = -1, r = 1;
148 loop_fd = open(loop, O_RDONLY);
152 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
159 static char *_ioctl_backing_file(const char *loop)
161 struct loop_info64 lo64 = {0};
164 loop_fd = open(loop, O_RDONLY);
168 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
173 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
174 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
178 return strdup((char*)lo64.lo_file_name);
181 static char *_sysfs_backing_file(const char *loop)
188 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
191 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
192 major(st.st_rdev), minor(st.st_rdev));
194 fd = open(buf, O_RDONLY);
198 len = read(fd, buf, PATH_MAX);
207 char *crypt_loop_backing_file(const char *loop)
209 char *bf = _sysfs_backing_file(loop);
210 return bf ?: _ioctl_backing_file(loop);
213 int crypt_loop_device(const char *loop)
220 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
221 major(st.st_rdev) != LOOP_DEV_MAJOR)