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