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 #ifndef LOOP_CTL_GET_FREE
33 #define LOOP_CTL_GET_FREE 0x4C82
36 char *crypt_loop_get_device(void)
41 struct loop_info64 lo64 = {0};
43 for (i = 0; i < 256; i++) {
44 sprintf(dev, "/dev/loop%d", i);
45 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
48 loop_fd = open(dev, O_RDONLY);
52 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
63 /* loop-control not yet upstream */
65 char *crypt_loop_get_device(void)
71 loop_fd = open("/dev/loop-control", O_RDONLY);
75 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
82 snprintf(dev, "/dev/loop%d", i, sizeof(dev));
84 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
91 int crypt_loop_attach(const char *loop, const char *file, int offset,
92 int autoclear, int *readonly)
94 struct loop_info64 lo64 = {0};
95 int loop_fd = -1, file_fd = -1, r = 1;
97 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
98 if (file_fd < 0 && errno == EROFS && !*readonly) {
100 file_fd = open(file, O_RDONLY | O_EXCL);
105 loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
109 strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
110 lo64.lo_offset = offset;
112 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
114 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
117 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
118 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
122 /* Verify that autoclear is really set */
124 memset(&lo64, 0, sizeof(lo64));
125 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
126 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
127 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
134 if (r && loop_fd >= 0)
138 return r ? -1 : loop_fd;
141 int crypt_loop_detach(const char *loop)
143 int loop_fd = -1, r = 1;
145 loop_fd = open(loop, O_RDONLY);
149 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
156 static char *_ioctl_backing_file(const char *loop)
158 struct loop_info64 lo64 = {0};
161 loop_fd = open(loop, O_RDONLY);
165 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
170 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
171 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
175 return strdup((char*)lo64.lo_file_name);
178 static char *_sysfs_backing_file(const char *loop)
185 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
188 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
189 major(st.st_rdev), minor(st.st_rdev));
191 fd = open(buf, O_RDONLY);
195 len = read(fd, buf, PATH_MAX);
204 char *crypt_loop_backing_file(const char *loop)
206 char *bf = _sysfs_backing_file(loop);
207 return bf ?: _ioctl_backing_file(loop);
210 int crypt_loop_device(const char *loop)
217 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
218 major(st.st_rdev) != LOOP_DEV_MAJOR)