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)
47 struct loop_info64 lo64 = {0};
49 for (i = 0; i < 256; i++) {
50 sprintf(dev, "/dev/loop%d", i);
52 loop_fd = open(dev, O_RDONLY);
56 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
67 char *crypt_loop_get_device(void)
73 loop_fd = open("/dev/loop-control", O_RDONLY);
75 return crypt_loop_get_device_old();
77 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
84 if (sprintf(dev, "/dev/loop%d", i) < 0)
87 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
93 int crypt_loop_attach(const char *loop, const char *file, int offset,
94 int autoclear, int *readonly)
96 struct loop_info64 lo64 = {0};
97 int loop_fd = -1, file_fd = -1, r = 1;
99 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
100 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
102 file_fd = open(file, O_RDONLY | O_EXCL);
107 loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
111 strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
112 lo64.lo_offset = offset;
114 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
116 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
119 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
120 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
124 /* Verify that autoclear is really set */
126 memset(&lo64, 0, sizeof(lo64));
127 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
128 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
129 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
136 if (r && loop_fd >= 0)
140 return r ? -1 : loop_fd;
143 int crypt_loop_detach(const char *loop)
145 int loop_fd = -1, r = 1;
147 loop_fd = open(loop, O_RDONLY);
151 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
158 static char *_ioctl_backing_file(const char *loop)
160 struct loop_info64 lo64 = {0};
163 loop_fd = open(loop, O_RDONLY);
167 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
172 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
173 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
177 return strdup((char*)lo64.lo_file_name);
180 static char *_sysfs_backing_file(const char *loop)
187 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
190 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
191 major(st.st_rdev), minor(st.st_rdev));
193 fd = open(buf, O_RDONLY);
197 len = read(fd, buf, PATH_MAX);
206 char *crypt_loop_backing_file(const char *loop)
208 char *bf = _sysfs_backing_file(loop);
209 return bf ?: _ioctl_backing_file(loop);
212 int crypt_loop_device(const char *loop)
219 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
220 major(st.st_rdev) != LOOP_DEV_MAJOR)