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