Add sys/types.h for loop wrapper (required on new systems).
[platform/upstream/cryptsetup.git] / lib / utils_loop.c
1 /*
2  * loopback block device utilities
3  *
4  * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
5  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <sys/ioctl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <linux/loop.h>
30
31 #include "utils_loop.h"
32
33 #define LOOP_DEV_MAJOR 7
34
35 #ifndef LO_FLAGS_AUTOCLEAR
36 #define LO_FLAGS_AUTOCLEAR 4
37 #endif
38
39 #ifndef LOOP_CTL_GET_FREE
40 #define LOOP_CTL_GET_FREE 0x4C82
41 #endif
42
43 static char *crypt_loop_get_device_old(void)
44 {
45         char dev[20];
46         int i, loop_fd;
47         struct stat st;
48         struct loop_info64 lo64 = {0};
49
50         for (i = 0; i < 256; i++) {
51                 sprintf(dev, "/dev/loop%d", i);
52                 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
53                         return NULL;
54
55                 loop_fd = open(dev, O_RDONLY);
56                 if (loop_fd < 0)
57                         return NULL;
58
59                 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
60                     errno == ENXIO) {
61                         close(loop_fd);
62                         return strdup(dev);
63                 }
64                 close(loop_fd);
65         }
66
67         return NULL;
68 }
69
70 char *crypt_loop_get_device(void)
71 {
72         char dev[64];
73         int i, loop_fd;
74         struct stat st;
75
76         loop_fd = open("/dev/loop-control", O_RDONLY);
77         if (loop_fd < 0)
78                 return crypt_loop_get_device_old();
79
80         i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
81         if (i < 0) {
82                 close(loop_fd);
83                 return NULL;
84         }
85         close(loop_fd);
86
87         if (sprintf(dev, "/dev/loop%d", i) < 0)
88                 return NULL;
89
90         if (stat(dev, &st) || !S_ISBLK(st.st_mode))
91                 return NULL;
92
93         return strdup(dev);
94 }
95
96 int crypt_loop_attach(const char *loop, const char *file, int offset,
97                       int autoclear, int *readonly)
98 {
99         struct loop_info64 lo64 = {0};
100         int loop_fd = -1, file_fd = -1, r = 1;
101
102         file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
103         if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
104                 *readonly = 1;
105                 file_fd = open(file, O_RDONLY | O_EXCL);
106         }
107         if (file_fd < 0)
108                 goto out;
109
110         loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
111         if (loop_fd < 0)
112                 goto out;
113
114         strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
115         lo64.lo_offset = offset;
116         if (autoclear)
117                 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
118
119         if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
120                 goto out;
121
122         if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
123                 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
124                 goto out;
125         }
126
127         /* Verify that autoclear is really set */
128         if (autoclear) {
129                 memset(&lo64, 0, sizeof(lo64));
130                 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
131                    !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
132                 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
133                         goto out;
134                 }
135         }
136
137         r = 0;
138 out:
139         if (r && loop_fd >= 0)
140                 close(loop_fd);
141         if (file_fd >= 0)
142                 close(file_fd);
143         return r ? -1 : loop_fd;
144 }
145
146 int crypt_loop_detach(const char *loop)
147 {
148         int loop_fd = -1, r = 1;
149
150         loop_fd = open(loop, O_RDONLY);
151         if (loop_fd < 0)
152                 return 1;
153
154         if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
155                 r = 0;
156
157         close(loop_fd);
158         return r;
159 }
160
161 static char *_ioctl_backing_file(const char *loop)
162 {
163         struct loop_info64 lo64 = {0};
164         int loop_fd;
165
166         loop_fd = open(loop, O_RDONLY);
167         if (loop_fd < 0)
168                 return NULL;
169
170         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
171                 close(loop_fd);
172                 return NULL;
173         }
174
175         lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
176         lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
177
178         close(loop_fd);
179
180         return strdup((char*)lo64.lo_file_name);
181 }
182
183 static char *_sysfs_backing_file(const char *loop)
184 {
185         struct stat st;
186         char buf[PATH_MAX];
187         size_t len;
188         int fd;
189
190         if (stat(loop, &st) || !S_ISBLK(st.st_mode))
191                 return NULL;
192
193         snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
194                  major(st.st_rdev), minor(st.st_rdev));
195
196         fd = open(buf, O_RDONLY);
197         if (fd < 0)
198                 return NULL;
199
200         len = read(fd, buf, PATH_MAX);
201         close(fd);
202         if (len < 2)
203                 return NULL;
204
205         buf[len - 1] = '\0';
206         return strdup(buf);
207 }
208
209 char *crypt_loop_backing_file(const char *loop)
210 {
211         char *bf = _sysfs_backing_file(loop);
212         return bf ?: _ioctl_backing_file(loop);
213 }
214
215 int crypt_loop_device(const char *loop)
216 {
217         struct stat st;
218
219         if (!loop)
220                 return 0;
221
222         if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
223             major(st.st_rdev) != LOOP_DEV_MAJOR)
224                 return 0;
225
226         return 1;
227 }