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 <linux/loop.h>
30 #include "utils_loop.h"
32 #define LOOP_DEV_MAJOR 7
34 #ifndef LO_FLAGS_AUTOCLEAR
35 #define LO_FLAGS_AUTOCLEAR 4
38 #ifndef LOOP_CTL_GET_FREE
39 #define LOOP_CTL_GET_FREE 0x4C82
42 static char *crypt_loop_get_device_old(void)
47 struct loop_info64 lo64 = {0};
49 for (i = 0; i < 256; i++) {
50 sprintf(dev, "/dev/loop%d", i);
51 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
54 loop_fd = open(dev, O_RDONLY);
58 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
69 char *crypt_loop_get_device(void)
75 loop_fd = open("/dev/loop-control", O_RDONLY);
77 return crypt_loop_get_device_old();
79 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
86 if (sprintf(dev, "/dev/loop%d", i) < 0)
89 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
95 int crypt_loop_attach(const char *loop, const char *file, int offset,
96 int autoclear, int *readonly)
98 struct loop_info64 lo64 = {0};
99 int loop_fd = -1, file_fd = -1, r = 1;
101 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
102 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
104 file_fd = open(file, O_RDONLY | O_EXCL);
109 loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
113 strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
114 lo64.lo_offset = offset;
116 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
118 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
121 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
122 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
126 /* Verify that autoclear is really set */
128 memset(&lo64, 0, sizeof(lo64));
129 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
130 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
131 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
138 if (r && loop_fd >= 0)
142 return r ? -1 : loop_fd;
145 int crypt_loop_detach(const char *loop)
147 int loop_fd = -1, r = 1;
149 loop_fd = open(loop, O_RDONLY);
153 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
160 static char *_ioctl_backing_file(const char *loop)
162 struct loop_info64 lo64 = {0};
165 loop_fd = open(loop, O_RDONLY);
169 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
174 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
175 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
179 return strdup((char*)lo64.lo_file_name);
182 static char *_sysfs_backing_file(const char *loop)
189 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
192 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
193 major(st.st_rdev), minor(st.st_rdev));
195 fd = open(buf, O_RDONLY);
199 len = read(fd, buf, PATH_MAX);
208 char *crypt_loop_backing_file(const char *loop)
210 char *bf = _sysfs_backing_file(loop);
211 return bf ?: _ioctl_backing_file(loop);
214 int crypt_loop_device(const char *loop)
221 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
222 major(st.st_rdev) != LOOP_DEV_MAJOR)