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