blob: 2cbed863eb08aced5c372532bad9c557c4e5555c [file] [log] [blame]
San Mehatb78a32c2010-01-10 13:02:12 -08001/*
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
Jeff Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
San Mehatb78a32c2010-01-10 13:02:12 -080019#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080020#include <stdlib.h>
San Mehatb78a32c2010-01-10 13:02:12 -080021#include <fcntl.h>
22#include <unistd.h>
23#include <errno.h>
24#include <string.h>
Peter Bohm092aa1c2011-04-01 12:35:25 +020025#include <stdlib.h>
San Mehatb78a32c2010-01-10 13:02:12 -080026
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <sys/stat.h>
30
San Mehatd9a4e352010-03-12 13:32:47 -080031#include <linux/kdev_t.h>
32
San Mehatb78a32c2010-01-10 13:02:12 -080033#define LOG_TAG "Vold"
34
35#include <cutils/log.h>
36
Jeff Sharkey11c2d382017-09-11 10:32:01 -060037#include <android-base/logging.h>
38#include <android-base/stringprintf.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060039#include <utils/Trace.h>
San Mehatd9a4e352010-03-12 13:32:47 -080040
San Mehatb78a32c2010-01-10 13:02:12 -080041#include "Devmapper.h"
42
Peter Bohm092aa1c2011-04-01 12:35:25 +020043#define DEVMAPPER_BUFFER_SIZE 4096
44
Jeff Sharkey11c2d382017-09-11 10:32:01 -060045using android::base::StringPrintf;
46
47static const char* kVoldPrefix = "vold:";
48
San Mehatb78a32c2010-01-10 13:02:12 -080049void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize,
50 const char *name, unsigned flags) {
51 memset(io, 0, dataSize);
52 io->data_size = dataSize;
53 io->data_start = sizeof(struct dm_ioctl);
54 io->version[0] = 4;
55 io->version[1] = 0;
56 io->version[2] = 0;
57 io->flags = flags;
San Mehatd9a4e352010-03-12 13:32:47 -080058 if (name) {
Chih-Wei Huang7929deb2013-02-10 22:57:14 +080059 size_t ret = strlcpy(io->name, name, sizeof(io->name));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070060 if (ret >= sizeof(io->name))
61 abort();
San Mehatd9a4e352010-03-12 13:32:47 -080062 }
San Mehatb78a32c2010-01-10 13:02:12 -080063}
64
Jeff Sharkey11c2d382017-09-11 10:32:01 -060065int Devmapper::lookupActive(const char *name_raw, char *ubuffer, size_t len) {
66 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
67 const char* name = name_string.c_str();
68
Peter Bohm092aa1c2011-04-01 12:35:25 +020069 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -080070 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -070071 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -080072 return -1;
73 }
74
75 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070076 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070077 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -080078 free(buffer);
79 return -1;
80 }
81
82 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
83
Peter Bohm092aa1c2011-04-01 12:35:25 +020084 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -080085 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat8b8f71b2010-01-11 09:17:25 -080086 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -070087 SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -080088 }
89 free(buffer);
90 close(fd);
91 return -1;
92 }
93 close(fd);
94
95 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
96 free(buffer);
San Mehatb78a32c2010-01-10 13:02:12 -080097 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
98 return 0;
99}
100
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600101int Devmapper::create(const char *name_raw, const char *loopFile, const char *key,
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200102 unsigned long numSectors, char *ubuffer, size_t len) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600103 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
104 const char* name = name_string.c_str();
105
Peter Bohm092aa1c2011-04-01 12:35:25 +0200106 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800107 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700108 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800109 return -1;
110 }
111
112 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700113 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700114 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800115 free(buffer);
116 return -1;
117 }
118
119 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
120
121 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +0200122 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800123
124 if (ioctl(fd, DM_DEV_CREATE, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700125 SLOGE("Error creating device mapping (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800126 free(buffer);
127 close(fd);
128 return -1;
129 }
130
131 // Set the legacy geometry
Peter Bohm092aa1c2011-04-01 12:35:25 +0200132 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800133
134 char *geoParams = buffer + sizeof(struct dm_ioctl);
135 // 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
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600136 strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
San Mehatb78a32c2010-01-10 13:02:12 -0800137 geoParams += strlen(geoParams) + 1;
138 geoParams = (char *) _align(geoParams, 8);
139 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700140 SLOGE("Error setting device geometry (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800141 free(buffer);
142 close(fd);
143 return -1;
144 }
145
146 // Retrieve the device number we were allocated
Peter Bohm092aa1c2011-04-01 12:35:25 +0200147 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800148 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700149 SLOGE("Error retrieving devmapper status (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800150 free(buffer);
151 close(fd);
152 return -1;
153 }
154
155 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
San Mehatb78a32c2010-01-10 13:02:12 -0800156 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
157
158 // Load the table
159 struct dm_target_spec *tgt;
160 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
161
Peter Bohm092aa1c2011-04-01 12:35:25 +0200162 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
San Mehatb78a32c2010-01-10 13:02:12 -0800163 io->target_count = 1;
164 tgt->status = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -0800165
San Mehatb78a32c2010-01-10 13:02:12 -0800166 tgt->sector_start = 0;
San Mehat8b8f71b2010-01-11 09:17:25 -0800167 tgt->length = numSectors;
San Mehatfcf24fe2010-03-03 12:37:32 -0800168
Peter Bohm092aa1c2011-04-01 12:35:25 +0200169 strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
San Mehatb78a32c2010-01-10 13:02:12 -0800170
171 char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
Peter Bohm092aa1c2011-04-01 12:35:25 +0200172 snprintf(cryptParams,
173 DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
174 "twofish %s 0 %s 0", key, loopFile);
San Mehatb78a32c2010-01-10 13:02:12 -0800175 cryptParams += strlen(cryptParams) + 1;
176 cryptParams = (char *) _align(cryptParams, 8);
177 tgt->next = cryptParams - buffer;
178
179 if (ioctl(fd, DM_TABLE_LOAD, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700180 SLOGE("Error loading mapping table (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800181 free(buffer);
182 close(fd);
183 return -1;
184 }
185
186 // Resume the new table
Peter Bohm092aa1c2011-04-01 12:35:25 +0200187 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800188
189 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700190 SLOGE("Error Resuming (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800191 free(buffer);
192 close(fd);
193 return -1;
194 }
195
196 free(buffer);
197
San Mehat8c940ef2010-02-13 14:19:53 -0800198 close(fd);
San Mehatb78a32c2010-01-10 13:02:12 -0800199 return 0;
200}
201
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600202int Devmapper::destroy(const char *name_raw) {
203 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
204 const char* name = name_string.c_str();
205
Peter Bohm092aa1c2011-04-01 12:35:25 +0200206 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800207 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700208 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800209 return -1;
210 }
211
212 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700213 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700214 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800215 free(buffer);
216 return -1;
217 }
218
219 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
220
221 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +0200222 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800223
224 if (ioctl(fd, DM_DEV_REMOVE, io)) {
San Mehat0586d542010-01-12 15:38:59 -0800225 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700226 SLOGE("Error destroying device mapping (%s)", strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800227 }
San Mehatb78a32c2010-01-10 13:02:12 -0800228 free(buffer);
229 close(fd);
230 return -1;
231 }
232
233 free(buffer);
234 close(fd);
235 return 0;
236}
237
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600238int Devmapper::destroyAll() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600239 ATRACE_NAME("Devmapper::destroyAll");
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600240 char *buffer = (char *) malloc(1024 * 64);
241 if (!buffer) {
242 SLOGE("Error allocating memory (%s)", strerror(errno));
243 return -1;
244 }
245 memset(buffer, 0, (1024 * 64));
246
247 char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
248 if (!buffer2) {
249 SLOGE("Error allocating memory (%s)", strerror(errno));
250 free(buffer);
251 return -1;
252 }
253
254 int fd;
255 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
256 SLOGE("Error opening devmapper (%s)", strerror(errno));
257 free(buffer);
258 free(buffer2);
259 return -1;
260 }
261
262 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
263 ioctlInit(io, (1024 * 64), NULL, 0);
264
265 if (ioctl(fd, DM_LIST_DEVICES, io)) {
266 SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno));
267 free(buffer);
268 free(buffer2);
269 close(fd);
270 return -1;
271 }
272
273 struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
274 if (!n->dev) {
275 free(buffer);
276 free(buffer2);
277 close(fd);
278 return 0;
279 }
280
281 unsigned nxt = 0;
282 do {
283 n = (struct dm_name_list *) (((char *) n) + nxt);
284 if (strncmp(n->name, kVoldPrefix, strlen(kVoldPrefix)) == 0) {
285 LOG(DEBUG) << "Tearing down stale dm device named " << n->name;
286
287 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
288 struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
289 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
290 if (ioctl(fd, DM_DEV_REMOVE, io2)) {
291 if (errno != ENXIO) {
292 PLOG(WARNING) << "Failed to destroy dm device named " << n->name;
293 }
294 }
295 } else {
296 LOG(VERBOSE) << "Found unmanaged dm device named " << n->name;
297 }
298 nxt = n->next;
299 } while (nxt);
300
301 free(buffer);
302 free(buffer2);
303 close(fd);
304 return 0;
305}
306
San Mehatb78a32c2010-01-10 13:02:12 -0800307void *Devmapper::_align(void *ptr, unsigned int a)
308{
Tim Murray8439dc92014-12-15 11:56:11 -0800309 unsigned long agn = --a;
San Mehatb78a32c2010-01-10 13:02:12 -0800310
311 return (void *) (((unsigned long) ptr + agn) & ~agn);
312}