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