blob: c945e9f5d832fc36b46be44fecc457a3ba38212e [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#include <sysutils/SocketClient.h>
38
San Mehatb78a32c2010-01-10 13:02:12 -080039#include "Devmapper.h"
40
Peter Bohm092aa1c2011-04-01 12:35:25 +020041#define DEVMAPPER_BUFFER_SIZE 4096
42
Jeff Sharkey11c2d382017-09-11 10:32:01 -060043using android::base::StringPrintf;
44
45static const char* kVoldPrefix = "vold:";
46
San Mehatd9a4e352010-03-12 13:32:47 -080047int Devmapper::dumpState(SocketClient *c) {
48
49 char *buffer = (char *) malloc(1024 * 64);
50 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -070051 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080052 return -1;
53 }
54 memset(buffer, 0, (1024 * 64));
55
Peter Bohm092aa1c2011-04-01 12:35:25 +020056 char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatd9a4e352010-03-12 13:32:47 -080057 if (!buffer2) {
San Mehat97ac40e2010-03-24 10:24:19 -070058 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080059 free(buffer);
60 return -1;
61 }
62
63 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070064 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070065 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080066 free(buffer);
67 free(buffer2);
68 return -1;
69 }
70
71 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
72 ioctlInit(io, (1024 * 64), NULL, 0);
73
74 if (ioctl(fd, DM_LIST_DEVICES, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -070075 SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080076 free(buffer);
77 free(buffer2);
78 close(fd);
79 return -1;
80 }
81
82 struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
83 if (!n->dev) {
84 free(buffer);
85 free(buffer2);
86 close(fd);
87 return 0;
88 }
89
90 unsigned nxt = 0;
91 do {
92 n = (struct dm_name_list *) (((char *) n) + nxt);
93
Peter Bohm092aa1c2011-04-01 12:35:25 +020094 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
San Mehatd9a4e352010-03-12 13:32:47 -080095 struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
Peter Bohm092aa1c2011-04-01 12:35:25 +020096 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
San Mehatd9a4e352010-03-12 13:32:47 -080097 if (ioctl(fd, DM_DEV_STATUS, io2)) {
98 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -070099 SLOGE("DM_DEV_STATUS ioctl failed (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800100 }
101 io2 = NULL;
102 }
103
104 char *tmp;
105 if (!io2) {
106 asprintf(&tmp, "%s %llu:%llu (no status available)", n->name, MAJOR(n->dev), MINOR(n->dev));
107 } else {
108 asprintf(&tmp, "%s %llu:%llu %d %d 0x%.8x %llu:%llu", n->name, MAJOR(n->dev),
109 MINOR(n->dev), io2->target_count, io2->open_count, io2->flags, MAJOR(io2->dev),
110 MINOR(io2->dev));
111 }
112 c->sendMsg(0, tmp, false);
113 free(tmp);
114 nxt = n->next;
115 } while (nxt);
116
117 free(buffer);
118 free(buffer2);
119 close(fd);
120 return 0;
121}
122
San Mehatb78a32c2010-01-10 13:02:12 -0800123void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize,
124 const char *name, unsigned flags) {
125 memset(io, 0, dataSize);
126 io->data_size = dataSize;
127 io->data_start = sizeof(struct dm_ioctl);
128 io->version[0] = 4;
129 io->version[1] = 0;
130 io->version[2] = 0;
131 io->flags = flags;
San Mehatd9a4e352010-03-12 13:32:47 -0800132 if (name) {
Chih-Wei Huang7929deb2013-02-10 22:57:14 +0800133 size_t ret = strlcpy(io->name, name, sizeof(io->name));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700134 if (ret >= sizeof(io->name))
135 abort();
San Mehatd9a4e352010-03-12 13:32:47 -0800136 }
San Mehatb78a32c2010-01-10 13:02:12 -0800137}
138
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600139int Devmapper::lookupActive(const char *name_raw, char *ubuffer, size_t len) {
140 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
141 const char* name = name_string.c_str();
142
Peter Bohm092aa1c2011-04-01 12:35:25 +0200143 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800144 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700145 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800146 return -1;
147 }
148
149 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700150 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700151 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800152 free(buffer);
153 return -1;
154 }
155
156 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
157
Peter Bohm092aa1c2011-04-01 12:35:25 +0200158 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800159 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat8b8f71b2010-01-11 09:17:25 -0800160 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700161 SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800162 }
163 free(buffer);
164 close(fd);
165 return -1;
166 }
167 close(fd);
168
169 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
170 free(buffer);
San Mehatb78a32c2010-01-10 13:02:12 -0800171 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
172 return 0;
173}
174
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600175int Devmapper::create(const char *name_raw, const char *loopFile, const char *key,
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200176 unsigned long numSectors, char *ubuffer, size_t len) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600177 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
178 const char* name = name_string.c_str();
179
Peter Bohm092aa1c2011-04-01 12:35:25 +0200180 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800181 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700182 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800183 return -1;
184 }
185
186 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700187 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700188 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800189 free(buffer);
190 return -1;
191 }
192
193 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
194
195 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +0200196 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800197
198 if (ioctl(fd, DM_DEV_CREATE, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700199 SLOGE("Error creating device mapping (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800200 free(buffer);
201 close(fd);
202 return -1;
203 }
204
205 // Set the legacy geometry
Peter Bohm092aa1c2011-04-01 12:35:25 +0200206 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800207
208 char *geoParams = buffer + sizeof(struct dm_ioctl);
209 // 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 -0600210 strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
San Mehatb78a32c2010-01-10 13:02:12 -0800211 geoParams += strlen(geoParams) + 1;
212 geoParams = (char *) _align(geoParams, 8);
213 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700214 SLOGE("Error setting device geometry (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800215 free(buffer);
216 close(fd);
217 return -1;
218 }
219
220 // Retrieve the device number we were allocated
Peter Bohm092aa1c2011-04-01 12:35:25 +0200221 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800222 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700223 SLOGE("Error retrieving devmapper status (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800224 free(buffer);
225 close(fd);
226 return -1;
227 }
228
229 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
San Mehatb78a32c2010-01-10 13:02:12 -0800230 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
231
232 // Load the table
233 struct dm_target_spec *tgt;
234 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
235
Peter Bohm092aa1c2011-04-01 12:35:25 +0200236 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
San Mehatb78a32c2010-01-10 13:02:12 -0800237 io->target_count = 1;
238 tgt->status = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -0800239
San Mehatb78a32c2010-01-10 13:02:12 -0800240 tgt->sector_start = 0;
San Mehat8b8f71b2010-01-11 09:17:25 -0800241 tgt->length = numSectors;
San Mehatfcf24fe2010-03-03 12:37:32 -0800242
Peter Bohm092aa1c2011-04-01 12:35:25 +0200243 strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
San Mehatb78a32c2010-01-10 13:02:12 -0800244
245 char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
Peter Bohm092aa1c2011-04-01 12:35:25 +0200246 snprintf(cryptParams,
247 DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
248 "twofish %s 0 %s 0", key, loopFile);
San Mehatb78a32c2010-01-10 13:02:12 -0800249 cryptParams += strlen(cryptParams) + 1;
250 cryptParams = (char *) _align(cryptParams, 8);
251 tgt->next = cryptParams - buffer;
252
253 if (ioctl(fd, DM_TABLE_LOAD, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700254 SLOGE("Error loading mapping table (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800255 free(buffer);
256 close(fd);
257 return -1;
258 }
259
260 // Resume the new table
Peter Bohm092aa1c2011-04-01 12:35:25 +0200261 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800262
263 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700264 SLOGE("Error Resuming (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800265 free(buffer);
266 close(fd);
267 return -1;
268 }
269
270 free(buffer);
271
San Mehat8c940ef2010-02-13 14:19:53 -0800272 close(fd);
San Mehatb78a32c2010-01-10 13:02:12 -0800273 return 0;
274}
275
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600276int Devmapper::destroy(const char *name_raw) {
277 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
278 const char* name = name_string.c_str();
279
Peter Bohm092aa1c2011-04-01 12:35:25 +0200280 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800281 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700282 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800283 return -1;
284 }
285
286 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700287 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700288 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800289 free(buffer);
290 return -1;
291 }
292
293 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
294
295 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +0200296 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800297
298 if (ioctl(fd, DM_DEV_REMOVE, io)) {
San Mehat0586d542010-01-12 15:38:59 -0800299 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700300 SLOGE("Error destroying device mapping (%s)", strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800301 }
San Mehatb78a32c2010-01-10 13:02:12 -0800302 free(buffer);
303 close(fd);
304 return -1;
305 }
306
307 free(buffer);
308 close(fd);
309 return 0;
310}
311
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600312int Devmapper::destroyAll() {
313 char *buffer = (char *) malloc(1024 * 64);
314 if (!buffer) {
315 SLOGE("Error allocating memory (%s)", strerror(errno));
316 return -1;
317 }
318 memset(buffer, 0, (1024 * 64));
319
320 char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
321 if (!buffer2) {
322 SLOGE("Error allocating memory (%s)", strerror(errno));
323 free(buffer);
324 return -1;
325 }
326
327 int fd;
328 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
329 SLOGE("Error opening devmapper (%s)", strerror(errno));
330 free(buffer);
331 free(buffer2);
332 return -1;
333 }
334
335 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
336 ioctlInit(io, (1024 * 64), NULL, 0);
337
338 if (ioctl(fd, DM_LIST_DEVICES, io)) {
339 SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno));
340 free(buffer);
341 free(buffer2);
342 close(fd);
343 return -1;
344 }
345
346 struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
347 if (!n->dev) {
348 free(buffer);
349 free(buffer2);
350 close(fd);
351 return 0;
352 }
353
354 unsigned nxt = 0;
355 do {
356 n = (struct dm_name_list *) (((char *) n) + nxt);
357 if (strncmp(n->name, kVoldPrefix, strlen(kVoldPrefix)) == 0) {
358 LOG(DEBUG) << "Tearing down stale dm device named " << n->name;
359
360 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
361 struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
362 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
363 if (ioctl(fd, DM_DEV_REMOVE, io2)) {
364 if (errno != ENXIO) {
365 PLOG(WARNING) << "Failed to destroy dm device named " << n->name;
366 }
367 }
368 } else {
369 LOG(VERBOSE) << "Found unmanaged dm device named " << n->name;
370 }
371 nxt = n->next;
372 } while (nxt);
373
374 free(buffer);
375 free(buffer2);
376 close(fd);
377 return 0;
378}
379
San Mehatb78a32c2010-01-10 13:02:12 -0800380void *Devmapper::_align(void *ptr, unsigned int a)
381{
Tim Murray8439dc92014-12-15 11:56:11 -0800382 unsigned long agn = --a;
San Mehatb78a32c2010-01-10 13:02:12 -0800383
384 return (void *) (((unsigned long) ptr + agn) & ~agn);
385}