blob: ed498a3a383149d2801a8a3383863cfbe55890ce [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
17#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080018#include <stdlib.h>
San Mehatb78a32c2010-01-10 13:02:12 -080019#include <fcntl.h>
20#include <unistd.h>
21#include <errno.h>
22#include <string.h>
Peter Bohm092aa1c2011-04-01 12:35:25 +020023#include <stdlib.h>
San Mehatb78a32c2010-01-10 13:02:12 -080024
25#include <sys/types.h>
26#include <sys/ioctl.h>
27#include <sys/stat.h>
28
San Mehatd9a4e352010-03-12 13:32:47 -080029#include <linux/kdev_t.h>
30
San Mehatb78a32c2010-01-10 13:02:12 -080031#define LOG_TAG "Vold"
32
33#include <cutils/log.h>
34
Jeff Sharkey11c2d382017-09-11 10:32:01 -060035#include <android-base/logging.h>
36#include <android-base/stringprintf.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::lookupActive(const char *name_raw, char *ubuffer, size_t len) {
63 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
64 const char* name = name_string.c_str();
65
Peter Bohm092aa1c2011-04-01 12:35:25 +020066 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -080067 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -070068 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -080069 return -1;
70 }
71
72 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070073 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070074 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -080075 free(buffer);
76 return -1;
77 }
78
79 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
80
Peter Bohm092aa1c2011-04-01 12:35:25 +020081 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -080082 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat8b8f71b2010-01-11 09:17:25 -080083 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -070084 SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -080085 }
86 free(buffer);
87 close(fd);
88 return -1;
89 }
90 close(fd);
91
92 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
93 free(buffer);
San Mehatb78a32c2010-01-10 13:02:12 -080094 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
95 return 0;
96}
97
Jeff Sharkey11c2d382017-09-11 10:32:01 -060098int Devmapper::create(const char *name_raw, const char *loopFile, const char *key,
Mateusz Nowaka4f48d02015-08-03 18:06:39 +020099 unsigned long numSectors, char *ubuffer, size_t len) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600100 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
101 const char* name = name_string.c_str();
102
Peter Bohm092aa1c2011-04-01 12:35:25 +0200103 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800104 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700105 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800106 return -1;
107 }
108
109 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700110 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700111 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800112 free(buffer);
113 return -1;
114 }
115
116 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
117
118 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +0200119 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800120
121 if (ioctl(fd, DM_DEV_CREATE, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700122 SLOGE("Error creating device mapping (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800123 free(buffer);
124 close(fd);
125 return -1;
126 }
127
128 // Set the legacy geometry
Peter Bohm092aa1c2011-04-01 12:35:25 +0200129 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800130
131 char *geoParams = buffer + sizeof(struct dm_ioctl);
132 // 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 -0600133 strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
San Mehatb78a32c2010-01-10 13:02:12 -0800134 geoParams += strlen(geoParams) + 1;
135 geoParams = (char *) _align(geoParams, 8);
136 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700137 SLOGE("Error setting device geometry (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800138 free(buffer);
139 close(fd);
140 return -1;
141 }
142
143 // Retrieve the device number we were allocated
Peter Bohm092aa1c2011-04-01 12:35:25 +0200144 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800145 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700146 SLOGE("Error retrieving devmapper status (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800147 free(buffer);
148 close(fd);
149 return -1;
150 }
151
152 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
San Mehatb78a32c2010-01-10 13:02:12 -0800153 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
154
155 // Load the table
156 struct dm_target_spec *tgt;
157 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
158
Peter Bohm092aa1c2011-04-01 12:35:25 +0200159 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
San Mehatb78a32c2010-01-10 13:02:12 -0800160 io->target_count = 1;
161 tgt->status = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -0800162
San Mehatb78a32c2010-01-10 13:02:12 -0800163 tgt->sector_start = 0;
San Mehat8b8f71b2010-01-11 09:17:25 -0800164 tgt->length = numSectors;
San Mehatfcf24fe2010-03-03 12:37:32 -0800165
Peter Bohm092aa1c2011-04-01 12:35:25 +0200166 strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
San Mehatb78a32c2010-01-10 13:02:12 -0800167
168 char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
Peter Bohm092aa1c2011-04-01 12:35:25 +0200169 snprintf(cryptParams,
170 DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
171 "twofish %s 0 %s 0", key, loopFile);
San Mehatb78a32c2010-01-10 13:02:12 -0800172 cryptParams += strlen(cryptParams) + 1;
173 cryptParams = (char *) _align(cryptParams, 8);
174 tgt->next = cryptParams - buffer;
175
176 if (ioctl(fd, DM_TABLE_LOAD, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700177 SLOGE("Error loading mapping table (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800178 free(buffer);
179 close(fd);
180 return -1;
181 }
182
183 // Resume the new table
Peter Bohm092aa1c2011-04-01 12:35:25 +0200184 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800185
186 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700187 SLOGE("Error Resuming (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800188 free(buffer);
189 close(fd);
190 return -1;
191 }
192
193 free(buffer);
194
San Mehat8c940ef2010-02-13 14:19:53 -0800195 close(fd);
San Mehatb78a32c2010-01-10 13:02:12 -0800196 return 0;
197}
198
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600199int Devmapper::destroy(const char *name_raw) {
200 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
201 const char* name = name_string.c_str();
202
Peter Bohm092aa1c2011-04-01 12:35:25 +0200203 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800204 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700205 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800206 return -1;
207 }
208
209 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700210 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700211 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800212 free(buffer);
213 return -1;
214 }
215
216 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
217
218 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +0200219 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800220
221 if (ioctl(fd, DM_DEV_REMOVE, io)) {
San Mehat0586d542010-01-12 15:38:59 -0800222 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700223 SLOGE("Error destroying device mapping (%s)", strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800224 }
San Mehatb78a32c2010-01-10 13:02:12 -0800225 free(buffer);
226 close(fd);
227 return -1;
228 }
229
230 free(buffer);
231 close(fd);
232 return 0;
233}
234
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600235int Devmapper::destroyAll() {
236 char *buffer = (char *) malloc(1024 * 64);
237 if (!buffer) {
238 SLOGE("Error allocating memory (%s)", strerror(errno));
239 return -1;
240 }
241 memset(buffer, 0, (1024 * 64));
242
243 char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
244 if (!buffer2) {
245 SLOGE("Error allocating memory (%s)", strerror(errno));
246 free(buffer);
247 return -1;
248 }
249
250 int fd;
251 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
252 SLOGE("Error opening devmapper (%s)", strerror(errno));
253 free(buffer);
254 free(buffer2);
255 return -1;
256 }
257
258 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
259 ioctlInit(io, (1024 * 64), NULL, 0);
260
261 if (ioctl(fd, DM_LIST_DEVICES, io)) {
262 SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno));
263 free(buffer);
264 free(buffer2);
265 close(fd);
266 return -1;
267 }
268
269 struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
270 if (!n->dev) {
271 free(buffer);
272 free(buffer2);
273 close(fd);
274 return 0;
275 }
276
277 unsigned nxt = 0;
278 do {
279 n = (struct dm_name_list *) (((char *) n) + nxt);
280 if (strncmp(n->name, kVoldPrefix, strlen(kVoldPrefix)) == 0) {
281 LOG(DEBUG) << "Tearing down stale dm device named " << n->name;
282
283 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
284 struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
285 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
286 if (ioctl(fd, DM_DEV_REMOVE, io2)) {
287 if (errno != ENXIO) {
288 PLOG(WARNING) << "Failed to destroy dm device named " << n->name;
289 }
290 }
291 } else {
292 LOG(VERBOSE) << "Found unmanaged dm device named " << n->name;
293 }
294 nxt = n->next;
295 } while (nxt);
296
297 free(buffer);
298 free(buffer2);
299 close(fd);
300 return 0;
301}
302
San Mehatb78a32c2010-01-10 13:02:12 -0800303void *Devmapper::_align(void *ptr, unsigned int a)
304{
Tim Murray8439dc92014-12-15 11:56:11 -0800305 unsigned long agn = --a;
San Mehatb78a32c2010-01-10 13:02:12 -0800306
307 return (void *) (((unsigned long) ptr + agn) & ~agn);
308}