2 * loopback block device utilities
4 * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <linux/loop.h>
31 #include "utils_loop.h"
33 #define LOOP_DEV_MAJOR 7
35 #ifndef LO_FLAGS_AUTOCLEAR
36 #define LO_FLAGS_AUTOCLEAR 4
39 #ifndef LOOP_CTL_GET_FREE
40 #define LOOP_CTL_GET_FREE 0x4C82
43 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);
52 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
55 loop_fd = open(dev, O_RDONLY);
59 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
70 char *crypt_loop_get_device(void)
76 loop_fd = open("/dev/loop-control", O_RDONLY);
78 return crypt_loop_get_device_old();
80 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
87 if (sprintf(dev, "/dev/loop%d", i) < 0)
90 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
96 int crypt_loop_attach(const char *loop, const char *file, int offset,
97 int autoclear, int *readonly)
99 struct loop_info64 lo64 = {0};
100 int loop_fd = -1, file_fd = -1, r = 1;
102 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
103 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
105 file_fd = open(file, O_RDONLY | O_EXCL);
110 loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
114 strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
115 lo64.lo_offset = offset;
117 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
119 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
122 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
123 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
127 /* Verify that autoclear is really set */
129 memset(&lo64, 0, sizeof(lo64));
130 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
131 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
132 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
139 if (r && loop_fd >= 0)
143 return r ? -1 : loop_fd;
146 int crypt_loop_detach(const char *loop)
148 int loop_fd = -1, r = 1;
150 loop_fd = open(loop, O_RDONLY);
154 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
161 static char *_ioctl_backing_file(const char *loop)
163 struct loop_info64 lo64 = {0};
166 loop_fd = open(loop, O_RDONLY);
170 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
175 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
176 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
180 return strdup((char*)lo64.lo_file_name);
183 static char *_sysfs_backing_file(const char *loop)
190 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
193 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
194 major(st.st_rdev), minor(st.st_rdev));
196 fd = open(buf, O_RDONLY);
200 len = read(fd, buf, PATH_MAX);
209 char *crypt_loop_backing_file(const char *loop)
211 char *bf = _sysfs_backing_file(loop);
212 return bf ?: _ioctl_backing_file(loop);
215 int crypt_loop_device(const char *loop)
222 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
223 major(st.st_rdev) != LOOP_DEV_MAJOR)