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