1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <sys/ioctl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include <linux/kdev_t.h>
31
32 #include <android-base/logging.h>
33 #include <android-base/stringprintf.h>
34 #include <android-base/strings.h>
35 #include <utils/Trace.h>
36
37 #include "Devmapper.h"
38
39 #define DEVMAPPER_BUFFER_SIZE 4096
40
41 using android::base::StringPrintf;
42
43 static const char* kVoldPrefix = "vold:";
44
ioctlInit(struct dm_ioctl * io,size_t dataSize,const char * name,unsigned flags)45 void Devmapper::ioctlInit(struct dm_ioctl* io, size_t dataSize, const char* name, unsigned flags) {
46 memset(io, 0, dataSize);
47 io->data_size = dataSize;
48 io->data_start = sizeof(struct dm_ioctl);
49 io->version[0] = 4;
50 io->version[1] = 0;
51 io->version[2] = 0;
52 io->flags = flags;
53 if (name) {
54 size_t ret = strlcpy(io->name, name, sizeof(io->name));
55 if (ret >= sizeof(io->name)) abort();
56 }
57 }
58
create(const char * name_raw,const char * loopFile,const char * key,unsigned long numSectors,char * ubuffer,size_t len)59 int Devmapper::create(const char* name_raw, const char* loopFile, const char* key,
60 unsigned long numSectors, char* ubuffer, size_t len) {
61 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
62 const char* name = name_string.c_str();
63
64 char* buffer = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
65 if (!buffer) {
66 PLOG(ERROR) << "Failed malloc";
67 return -1;
68 }
69
70 int fd;
71 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
72 PLOG(ERROR) << "Failed open";
73 free(buffer);
74 return -1;
75 }
76
77 struct dm_ioctl* io = (struct dm_ioctl*)buffer;
78
79 // Create the DM device
80 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
81
82 if (ioctl(fd, DM_DEV_CREATE, io)) {
83 PLOG(ERROR) << "Failed DM_DEV_CREATE";
84 free(buffer);
85 close(fd);
86 return -1;
87 }
88
89 // Set the legacy geometry
90 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
91
92 char* geoParams = buffer + sizeof(struct dm_ioctl);
93 // bps=512 spc=8 res=32 nft=2 sec=8190 mid=0xf0 spt=63 hds=64 hid=0 bspf=8 rdcl=2 infs=1 bkbs=2
94 strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
95 geoParams += strlen(geoParams) + 1;
96 geoParams = (char*)_align(geoParams, 8);
97 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
98 PLOG(ERROR) << "Failed DM_DEV_SET_GEOMETRY";
99 free(buffer);
100 close(fd);
101 return -1;
102 }
103
104 // Retrieve the device number we were allocated
105 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
106 if (ioctl(fd, DM_DEV_STATUS, io)) {
107 PLOG(ERROR) << "Failed DM_DEV_STATUS";
108 free(buffer);
109 close(fd);
110 return -1;
111 }
112
113 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
114 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
115
116 // Load the table
117 struct dm_target_spec* tgt;
118 tgt = (struct dm_target_spec*)&buffer[sizeof(struct dm_ioctl)];
119
120 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
121 io->target_count = 1;
122 tgt->status = 0;
123
124 tgt->sector_start = 0;
125 tgt->length = numSectors;
126
127 strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
128
129 char* cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
130 snprintf(cryptParams,
131 DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
132 "twofish %s 0 %s 0", key, loopFile);
133 cryptParams += strlen(cryptParams) + 1;
134 cryptParams = (char*)_align(cryptParams, 8);
135 tgt->next = cryptParams - buffer;
136
137 if (ioctl(fd, DM_TABLE_LOAD, io)) {
138 PLOG(ERROR) << "Failed DM_TABLE_LOAD";
139 free(buffer);
140 close(fd);
141 return -1;
142 }
143
144 // Resume the new table
145 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
146
147 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
148 PLOG(ERROR) << "Failed DM_DEV_SUSPEND";
149 free(buffer);
150 close(fd);
151 return -1;
152 }
153
154 free(buffer);
155
156 close(fd);
157 return 0;
158 }
159
destroy(const char * name_raw)160 int Devmapper::destroy(const char* name_raw) {
161 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
162 const char* name = name_string.c_str();
163
164 char* buffer = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
165 if (!buffer) {
166 PLOG(ERROR) << "Failed malloc";
167 return -1;
168 }
169
170 int fd;
171 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
172 PLOG(ERROR) << "Failed open";
173 free(buffer);
174 return -1;
175 }
176
177 struct dm_ioctl* io = (struct dm_ioctl*)buffer;
178
179 // Create the DM device
180 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
181
182 if (ioctl(fd, DM_DEV_REMOVE, io)) {
183 if (errno != ENXIO) {
184 PLOG(ERROR) << "Failed DM_DEV_REMOVE";
185 }
186 free(buffer);
187 close(fd);
188 return -1;
189 }
190
191 free(buffer);
192 close(fd);
193 return 0;
194 }
195
destroyAll()196 int Devmapper::destroyAll() {
197 ATRACE_NAME("Devmapper::destroyAll");
198 char* buffer = (char*)malloc(1024 * 64);
199 if (!buffer) {
200 PLOG(ERROR) << "Failed malloc";
201 return -1;
202 }
203 memset(buffer, 0, (1024 * 64));
204
205 char* buffer2 = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
206 if (!buffer2) {
207 PLOG(ERROR) << "Failed malloc";
208 free(buffer);
209 return -1;
210 }
211
212 int fd;
213 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
214 PLOG(ERROR) << "Failed open";
215 free(buffer);
216 free(buffer2);
217 return -1;
218 }
219
220 struct dm_ioctl* io = (struct dm_ioctl*)buffer;
221 ioctlInit(io, (1024 * 64), NULL, 0);
222
223 if (ioctl(fd, DM_LIST_DEVICES, io)) {
224 PLOG(ERROR) << "Failed DM_LIST_DEVICES";
225 free(buffer);
226 free(buffer2);
227 close(fd);
228 return -1;
229 }
230
231 struct dm_name_list* n = (struct dm_name_list*)(((char*)buffer) + io->data_start);
232 if (!n->dev) {
233 free(buffer);
234 free(buffer2);
235 close(fd);
236 return 0;
237 }
238
239 unsigned nxt = 0;
240 do {
241 n = (struct dm_name_list*)(((char*)n) + nxt);
242 auto name = std::string(n->name);
243 if (android::base::StartsWith(name, kVoldPrefix)) {
244 LOG(DEBUG) << "Tearing down stale dm device named " << name;
245
246 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
247 struct dm_ioctl* io2 = (struct dm_ioctl*)buffer2;
248 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
249 if (ioctl(fd, DM_DEV_REMOVE, io2)) {
250 if (errno != ENXIO) {
251 PLOG(WARNING) << "Failed to destroy dm device named " << name;
252 }
253 }
254 } else {
255 LOG(DEBUG) << "Found unmanaged dm device named " << name;
256 }
257 nxt = n->next;
258 } while (nxt);
259
260 free(buffer);
261 free(buffer2);
262 close(fd);
263 return 0;
264 }
265
_align(void * ptr,unsigned int a)266 void* Devmapper::_align(void* ptr, unsigned int a) {
267 unsigned long agn = --a;
268
269 return (void*)(((unsigned long)ptr + agn) & ~agn);
270 }
271