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 char *crypt_loop_get_device(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 int crypt_loop_attach(const char *loop, const char *file, int offset,
60 int autoclear, int *readonly)
62 struct loop_info64 lo64 = {0};
63 int loop_fd = -1, file_fd = -1, r = 1;
65 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
66 if (file_fd < 0 && errno == EROFS && !*readonly) {
68 file_fd = open(file, O_RDONLY | O_EXCL);
73 loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
77 strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
78 lo64.lo_offset = offset;
80 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
82 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
85 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
86 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
90 /* Verify that autoclear is really set */
92 memset(&lo64, 0, sizeof(lo64));
93 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
94 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
95 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
102 if (r && loop_fd >= 0)
106 return r ? -1 : loop_fd;
109 int crypt_loop_detach(const char *loop)
111 int loop_fd = -1, r = 1;
113 loop_fd = open(loop, O_RDONLY);
117 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
124 static char *_ioctl_backing_file(const char *loop)
126 struct loop_info64 lo64 = {0};
129 loop_fd = open(loop, O_RDONLY);
133 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
138 lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
139 lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
143 return strdup((char*)lo64.lo_file_name);
146 static char *_sysfs_backing_file(const char *loop)
153 if (stat(loop, &st) || !S_ISBLK(st.st_mode))
156 snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
157 major(st.st_rdev), minor(st.st_rdev));
159 fd = open(buf, O_RDONLY);
163 len = read(fd, buf, PATH_MAX);
172 char *crypt_loop_backing_file(const char *loop)
174 char *bf = _sysfs_backing_file(loop);
175 return bf ?: _ioctl_backing_file(loop);
178 int crypt_loop_device(const char *loop)
185 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
186 major(st.st_rdev) != LOOP_DEV_MAJOR)