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 * 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.
28 #include <sys/ioctl.h>
30 #include <sys/types.h>
31 #include <linux/loop.h>
32 #ifdef HAVE_SYS_SYSMACROS_H
33 #include <sys/sysmacros.h> /* for major, minor */
36 #include "utils_loop.h"
38 #define LOOP_DEV_MAJOR 7
40 #ifndef LO_FLAGS_AUTOCLEAR
41 #define LO_FLAGS_AUTOCLEAR 4
44 #ifndef LOOP_CTL_GET_FREE
45 #define LOOP_CTL_GET_FREE 0x4C82
48 static char *crypt_loop_get_device_old(void)
52 struct loop_info64 lo64 = {0};
54 for (i = 0; i < 256; i++) {
55 sprintf(dev, "/dev/loop%d", i);
57 loop_fd = open(dev, O_RDONLY);
61 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
72 char *crypt_loop_get_device(void)
78 loop_fd = open("/dev/loop-control", O_RDONLY);
80 return crypt_loop_get_device_old();
82 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
89 if (sprintf(dev, "/dev/loop%d", i) < 0)
92 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
98 int crypt_loop_attach(const char *loop, const char *file, int offset,
99 int autoclear, int *readonly)
101 struct loop_info64 lo64 = {0};
102 int loop_fd = -1, file_fd = -1, r = 1;
104 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
105 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
107 file_fd = open(file, O_RDONLY | O_EXCL);
112 loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
116 strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
117 lo64.lo_offset = offset;
119 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
121 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
124 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
125 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
129 /* Verify that autoclear is really set */
131 memset(&lo64, 0, sizeof(lo64));
132 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
133 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
134 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
141 if (r && loop_fd >= 0)
145 return r ? -1 : loop_fd;
148 int crypt_loop_detach(const char *loop)
150 int loop_fd = -1, r = 1;
152 loop_fd = open(loop, O_RDONLY);
156 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
163 static char *_ioctl_backing_file(const char *loop)
165 struct loop_info64 lo64 = {0};
168 loop_fd = open(loop, O_RDONLY);
172 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
177 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
178 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
182 return strdup((char*)lo64.lo_file_name);
185 static char *_sysfs_backing_file(const char *loop)
192 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
195 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
196 major(st.st_rdev), minor(st.st_rdev));
198 fd = open(buf, O_RDONLY);
202 len = read(fd, buf, PATH_MAX);
211 char *crypt_loop_backing_file(const char *loop)
213 char *bf = _sysfs_backing_file(loop);
214 return bf ?: _ioctl_backing_file(loop);
217 int crypt_loop_device(const char *loop)
224 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
225 major(st.st_rdev) != LOOP_DEV_MAJOR)