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