blob: 2510771f67481a6afa3c911c9bbb8a593885fcb7 [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
Jeff Sharkey11c2d382017-09-11 10:32:01 -060033#include <android-base/logging.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060034#include <android-base/strings.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060035#include <android-base/stringprintf.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060036#include <utils/Trace.h>
San Mehatd9a4e352010-03-12 13:32:47 -080037
San Mehatb78a32c2010-01-10 13:02:12 -080038#include "Devmapper.h"
39
Peter Bohm092aa1c2011-04-01 12:35:25 +020040#define DEVMAPPER_BUFFER_SIZE 4096
41
Jeff Sharkey11c2d382017-09-11 10:32:01 -060042using android::base::StringPrintf;
43
44static const char* kVoldPrefix = "vold:";
45
San Mehatb78a32c2010-01-10 13:02:12 -080046void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize,
47 const char *name, unsigned flags) {
48 memset(io, 0, dataSize);
49 io->data_size = dataSize;
50 io->data_start = sizeof(struct dm_ioctl);
51 io->version[0] = 4;
52 io->version[1] = 0;
53 io->version[2] = 0;
54 io->flags = flags;
San Mehatd9a4e352010-03-12 13:32:47 -080055 if (name) {
Chih-Wei Huang7929deb2013-02-10 22:57:14 +080056 size_t ret = strlcpy(io->name, name, sizeof(io->name));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070057 if (ret >= sizeof(io->name))
58 abort();
San Mehatd9a4e352010-03-12 13:32:47 -080059 }
San Mehatb78a32c2010-01-10 13:02:12 -080060}
61
Jeff Sharkey11c2d382017-09-11 10:32:01 -060062int Devmapper::create(const char *name_raw, const char *loopFile, const char *key,
Mateusz Nowaka4f48d02015-08-03 18:06:39 +020063 unsigned long numSectors, char *ubuffer, size_t len) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060064 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
65 const char* name = name_string.c_str();
66
Peter Bohm092aa1c2011-04-01 12:35:25 +020067 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -080068 if (!buffer) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060069 PLOG(ERROR) << "Failed malloc";
San Mehatb78a32c2010-01-10 13:02:12 -080070 return -1;
71 }
72
73 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070074 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060075 PLOG(ERROR) << "Failed open";
San Mehatb78a32c2010-01-10 13:02:12 -080076 free(buffer);
77 return -1;
78 }
79
80 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
81
82 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +020083 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -080084
85 if (ioctl(fd, DM_DEV_CREATE, io)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060086 PLOG(ERROR) << "Failed DM_DEV_CREATE";
San Mehatb78a32c2010-01-10 13:02:12 -080087 free(buffer);
88 close(fd);
89 return -1;
90 }
91
92 // Set the legacy geometry
Peter Bohm092aa1c2011-04-01 12:35:25 +020093 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -080094
95 char *geoParams = buffer + sizeof(struct dm_ioctl);
96 // 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 -060097 strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
San Mehatb78a32c2010-01-10 13:02:12 -080098 geoParams += strlen(geoParams) + 1;
99 geoParams = (char *) _align(geoParams, 8);
100 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600101 PLOG(ERROR) << "Failed DM_DEV_SET_GEOMETRY";
San Mehatb78a32c2010-01-10 13:02:12 -0800102 free(buffer);
103 close(fd);
104 return -1;
105 }
106
107 // Retrieve the device number we were allocated
Peter Bohm092aa1c2011-04-01 12:35:25 +0200108 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800109 if (ioctl(fd, DM_DEV_STATUS, io)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600110 PLOG(ERROR) << "Failed DM_DEV_STATUS";
San Mehatb78a32c2010-01-10 13:02:12 -0800111 free(buffer);
112 close(fd);
113 return -1;
114 }
115
116 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
San Mehatb78a32c2010-01-10 13:02:12 -0800117 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
118
119 // Load the table
120 struct dm_target_spec *tgt;
121 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
122
Peter Bohm092aa1c2011-04-01 12:35:25 +0200123 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
San Mehatb78a32c2010-01-10 13:02:12 -0800124 io->target_count = 1;
125 tgt->status = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -0800126
San Mehatb78a32c2010-01-10 13:02:12 -0800127 tgt->sector_start = 0;
San Mehat8b8f71b2010-01-11 09:17:25 -0800128 tgt->length = numSectors;
San Mehatfcf24fe2010-03-03 12:37:32 -0800129
Peter Bohm092aa1c2011-04-01 12:35:25 +0200130 strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
San Mehatb78a32c2010-01-10 13:02:12 -0800131
132 char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
Peter Bohm092aa1c2011-04-01 12:35:25 +0200133 snprintf(cryptParams,
134 DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
135 "twofish %s 0 %s 0", key, loopFile);
San Mehatb78a32c2010-01-10 13:02:12 -0800136 cryptParams += strlen(cryptParams) + 1;
137 cryptParams = (char *) _align(cryptParams, 8);
138 tgt->next = cryptParams - buffer;
139
140 if (ioctl(fd, DM_TABLE_LOAD, io)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600141 PLOG(ERROR) << "Failed DM_TABLE_LOAD";
San Mehatb78a32c2010-01-10 13:02:12 -0800142 free(buffer);
143 close(fd);
144 return -1;
145 }
146
147 // Resume the new table
Peter Bohm092aa1c2011-04-01 12:35:25 +0200148 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800149
150 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600151 PLOG(ERROR) << "Failed DM_DEV_SUSPEND";
San Mehatb78a32c2010-01-10 13:02:12 -0800152 free(buffer);
153 close(fd);
154 return -1;
155 }
156
157 free(buffer);
158
San Mehat8c940ef2010-02-13 14:19:53 -0800159 close(fd);
San Mehatb78a32c2010-01-10 13:02:12 -0800160 return 0;
161}
162
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600163int Devmapper::destroy(const char *name_raw) {
164 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
165 const char* name = name_string.c_str();
166
Peter Bohm092aa1c2011-04-01 12:35:25 +0200167 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800168 if (!buffer) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600169 PLOG(ERROR) << "Failed malloc";
San Mehatb78a32c2010-01-10 13:02:12 -0800170 return -1;
171 }
172
173 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700174 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600175 PLOG(ERROR) << "Failed open";
San Mehatb78a32c2010-01-10 13:02:12 -0800176 free(buffer);
177 return -1;
178 }
179
180 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
181
182 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +0200183 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800184
185 if (ioctl(fd, DM_DEV_REMOVE, io)) {
San Mehat0586d542010-01-12 15:38:59 -0800186 if (errno != ENXIO) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600187 PLOG(ERROR) << "Failed DM_DEV_REMOVE";
San Mehat0586d542010-01-12 15:38:59 -0800188 }
San Mehatb78a32c2010-01-10 13:02:12 -0800189 free(buffer);
190 close(fd);
191 return -1;
192 }
193
194 free(buffer);
195 close(fd);
196 return 0;
197}
198
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600199int Devmapper::destroyAll() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600200 ATRACE_NAME("Devmapper::destroyAll");
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600201 char *buffer = (char *) malloc(1024 * 64);
202 if (!buffer) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600203 PLOG(ERROR) << "Failed malloc";
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600204 return -1;
205 }
206 memset(buffer, 0, (1024 * 64));
207
208 char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
209 if (!buffer2) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600210 PLOG(ERROR) << "Failed malloc";
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600211 free(buffer);
212 return -1;
213 }
214
215 int fd;
216 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600217 PLOG(ERROR) << "Failed open";
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600218 free(buffer);
219 free(buffer2);
220 return -1;
221 }
222
223 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
224 ioctlInit(io, (1024 * 64), NULL, 0);
225
226 if (ioctl(fd, DM_LIST_DEVICES, io)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600227 PLOG(ERROR) << "Failed DM_LIST_DEVICES";
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600228 free(buffer);
229 free(buffer2);
230 close(fd);
231 return -1;
232 }
233
234 struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
235 if (!n->dev) {
236 free(buffer);
237 free(buffer2);
238 close(fd);
239 return 0;
240 }
241
242 unsigned nxt = 0;
243 do {
244 n = (struct dm_name_list *) (((char *) n) + nxt);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600245 auto name = std::string(n->name);
246 if (android::base::StartsWith(name, kVoldPrefix)) {
247 LOG(DEBUG) << "Tearing down stale dm device named " << name;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600248
249 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
250 struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
251 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
252 if (ioctl(fd, DM_DEV_REMOVE, io2)) {
253 if (errno != ENXIO) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600254 PLOG(WARNING) << "Failed to destroy dm device named " << name;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600255 }
256 }
257 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600258 LOG(VERBOSE) << "Found unmanaged dm device named " << name;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600259 }
260 nxt = n->next;
261 } while (nxt);
262
263 free(buffer);
264 free(buffer2);
265 close(fd);
266 return 0;
267}
268
San Mehatb78a32c2010-01-10 13:02:12 -0800269void *Devmapper::_align(void *ptr, unsigned int a)
270{
Tim Murray8439dc92014-12-15 11:56:11 -0800271 unsigned long agn = --a;
San Mehatb78a32c2010-01-10 13:02:12 -0800272
273 return (void *) (((unsigned long) ptr + agn) & ~agn);
274}