81ae1fa9ac961fed5c58333e08d0ad3cc8a374df
[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  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <sys/ioctl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <linux/loop.h>
31
32 #include "utils_loop.h"
33
34 #define LOOP_DEV_MAJOR 7
35
36 #ifndef LO_FLAGS_AUTOCLEAR
37 #define LO_FLAGS_AUTOCLEAR 4
38 #endif
39
40 #ifndef LOOP_CTL_GET_FREE
41 #define LOOP_CTL_GET_FREE 0x4C82
42 #endif
43
44 static char *crypt_loop_get_device_old(void)
45 {
46         char dev[20];
47         int i, loop_fd;
48         struct loop_info64 lo64 = {0};
49
50         for (i = 0; i < 256; i++) {
51                 sprintf(dev, "/dev/loop%d", i);
52
53                 loop_fd = open(dev, O_RDONLY);
54                 if (loop_fd < 0)
55                         return NULL;
56
57                 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
58                     errno == ENXIO) {
59                         close(loop_fd);
60                         return strdup(dev);
61                 }
62                 close(loop_fd);
63         }
64
65         return NULL;
66 }
67
68 char *crypt_loop_get_device(void)
69 {
70         char dev[64];
71         int i, loop_fd;
72         struct stat st;
73
74         loop_fd = open("/dev/loop-control", O_RDONLY);
75         if (loop_fd < 0)
76                 return crypt_loop_get_device_old();
77
78         i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
79         if (i < 0) {
80                 close(loop_fd);
81                 return NULL;
82         }
83         close(loop_fd);
84
85         if (sprintf(dev, "/dev/loop%d", i) < 0)
86                 return NULL;
87
88         if (stat(dev, &st) || !S_ISBLK(st.st_mode))
89                 return NULL;
90
91         return strdup(dev);
92 }
93
94 int crypt_loop_attach(const char *loop, const char *file, int offset,
95                       int autoclear, int *readonly)
96 {
97         struct loop_info64 lo64 = {0};
98         int loop_fd = -1, file_fd = -1, r = 1;
99
100         file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
101         if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
102                 *readonly = 1;
103                 file_fd = open(file, O_RDONLY | O_EXCL);
104         }
105         if (file_fd < 0)
106                 goto out;
107
108         loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
109         if (loop_fd < 0)
110                 goto out;
111
112         strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
113         lo64.lo_offset = offset;
114         if (autoclear)
115                 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
116
117         if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
118                 goto out;
119
120         if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
121                 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
122                 goto out;
123         }
124
125         /* Verify that autoclear is really set */
126         if (autoclear) {
127                 memset(&lo64, 0, sizeof(lo64));
128                 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
129                    !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
130                 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
131                         goto out;
132                 }
133         }
134
135         r = 0;
136 out:
137         if (r && loop_fd >= 0)
138                 close(loop_fd);
139         if (file_fd >= 0)
140                 close(file_fd);
141         return r ? -1 : loop_fd;
142 }
143
144 int crypt_loop_detach(const char *loop)
145 {
146         int loop_fd = -1, r = 1;
147
148         loop_fd = open(loop, O_RDONLY);
149         if (loop_fd < 0)
150                 return 1;
151
152         if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
153                 r = 0;
154
155         close(loop_fd);
156         return r;
157 }
158
159 static char *_ioctl_backing_file(const char *loop)
160 {
161         struct loop_info64 lo64 = {0};
162         int loop_fd;
163
164         loop_fd = open(loop, O_RDONLY);
165         if (loop_fd < 0)
166                 return NULL;
167
168         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
169                 close(loop_fd);
170                 return NULL;
171         }
172
173         lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
174         lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
175
176         close(loop_fd);
177
178         return strdup((char*)lo64.lo_file_name);
179 }
180
181 static char *_sysfs_backing_file(const char *loop)
182 {
183         struct stat st;
184         char buf[PATH_MAX];
185         size_t len;
186         int fd;
187
188         if (stat(loop, &st) || !S_ISBLK(st.st_mode))
189                 return NULL;
190
191         snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
192                  major(st.st_rdev), minor(st.st_rdev));
193
194         fd = open(buf, O_RDONLY);
195         if (fd < 0)
196                 return NULL;
197
198         len = read(fd, buf, PATH_MAX);
199         close(fd);
200         if (len < 2)
201                 return NULL;
202
203         buf[len - 1] = '\0';
204         return strdup(buf);
205 }
206
207 char *crypt_loop_backing_file(const char *loop)
208 {
209         char *bf = _sysfs_backing_file(loop);
210         return bf ?: _ioctl_backing_file(loop);
211 }
212
213 int crypt_loop_device(const char *loop)
214 {
215         struct stat st;
216
217         if (!loop)
218                 return 0;
219
220         if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
221             major(st.st_rdev) != LOOP_DEV_MAJOR)
222                 return 0;
223
224         return 1;
225 }