2 * loopback block device utilities
4 * Copyright (C) 2011, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/ioctl.h>
28 #include <linux/loop.h>
30 #include "utils_loop.h"
32 static char *crypt_loop_get_device_old(void)
37 struct loop_info64 lo64 = {0};
39 for (i = 0; i < 256; i++) {
40 sprintf(dev, "/dev/loop%d", i);
41 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
44 loop_fd = open(dev, O_RDONLY);
48 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
59 char *crypt_loop_get_device(void)
65 loop_fd = open("/dev/loop-control", O_RDONLY);
67 return crypt_loop_get_device_old();
69 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
76 if (sprintf(dev, "/dev/loop%d", i) < 0)
79 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
85 int crypt_loop_attach(const char *loop, const char *file, int offset,
86 int autoclear, int *readonly)
88 struct loop_info64 lo64 = {0};
89 int loop_fd = -1, file_fd = -1, r = 1;
91 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
92 if (file_fd < 0 && errno == EROFS && !*readonly) {
94 file_fd = open(file, O_RDONLY | O_EXCL);
99 loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
103 strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
104 lo64.lo_offset = offset;
106 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
108 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
111 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
112 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
116 /* Verify that autoclear is really set */
118 memset(&lo64, 0, sizeof(lo64));
119 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
120 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
121 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
128 if (r && loop_fd >= 0)
132 return r ? -1 : loop_fd;
135 int crypt_loop_detach(const char *loop)
137 int loop_fd = -1, r = 1;
139 loop_fd = open(loop, O_RDONLY);
143 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
150 static char *_ioctl_backing_file(const char *loop)
152 struct loop_info64 lo64 = {0};
155 loop_fd = open(loop, O_RDONLY);
159 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
164 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
165 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
169 return strdup((char*)lo64.lo_file_name);
172 static char *_sysfs_backing_file(const char *loop)
179 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
182 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
183 major(st.st_rdev), minor(st.st_rdev));
185 fd = open(buf, O_RDONLY);
189 len = read(fd, buf, PATH_MAX);
198 char *crypt_loop_backing_file(const char *loop)
200 char *bf = _sysfs_backing_file(loop);
201 return bf ?: _ioctl_backing_file(loop);
204 int crypt_loop_device(const char *loop)
211 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
212 major(st.st_rdev) != LOOP_DEV_MAJOR)