blob: 703eade67db6dea321eeac3f380262d43628ae75 [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
San Mehatd9a4e352010-03-12 13:32:47 -080035#include <sysutils/SocketClient.h>
36
San Mehatb78a32c2010-01-10 13:02:12 -080037#include "Devmapper.h"
38
Peter Bohm092aa1c2011-04-01 12:35:25 +020039#define DEVMAPPER_BUFFER_SIZE 4096
40
San Mehatd9a4e352010-03-12 13:32:47 -080041int Devmapper::dumpState(SocketClient *c) {
42
43 char *buffer = (char *) malloc(1024 * 64);
44 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -070045 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080046 return -1;
47 }
48 memset(buffer, 0, (1024 * 64));
49
Peter Bohm092aa1c2011-04-01 12:35:25 +020050 char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatd9a4e352010-03-12 13:32:47 -080051 if (!buffer2) {
San Mehat97ac40e2010-03-24 10:24:19 -070052 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080053 free(buffer);
54 return -1;
55 }
56
57 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070058 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070059 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080060 free(buffer);
61 free(buffer2);
62 return -1;
63 }
64
65 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
66 ioctlInit(io, (1024 * 64), NULL, 0);
67
68 if (ioctl(fd, DM_LIST_DEVICES, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -070069 SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080070 free(buffer);
71 free(buffer2);
72 close(fd);
73 return -1;
74 }
75
76 struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
77 if (!n->dev) {
78 free(buffer);
79 free(buffer2);
80 close(fd);
81 return 0;
82 }
83
84 unsigned nxt = 0;
85 do {
86 n = (struct dm_name_list *) (((char *) n) + nxt);
87
Peter Bohm092aa1c2011-04-01 12:35:25 +020088 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
San Mehatd9a4e352010-03-12 13:32:47 -080089 struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
Peter Bohm092aa1c2011-04-01 12:35:25 +020090 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
San Mehatd9a4e352010-03-12 13:32:47 -080091 if (ioctl(fd, DM_DEV_STATUS, io2)) {
92 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -070093 SLOGE("DM_DEV_STATUS ioctl failed (%s)", strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -080094 }
95 io2 = NULL;
96 }
97
98 char *tmp;
99 if (!io2) {
100 asprintf(&tmp, "%s %llu:%llu (no status available)", n->name, MAJOR(n->dev), MINOR(n->dev));
101 } else {
102 asprintf(&tmp, "%s %llu:%llu %d %d 0x%.8x %llu:%llu", n->name, MAJOR(n->dev),
103 MINOR(n->dev), io2->target_count, io2->open_count, io2->flags, MAJOR(io2->dev),
104 MINOR(io2->dev));
105 }
106 c->sendMsg(0, tmp, false);
107 free(tmp);
108 nxt = n->next;
109 } while (nxt);
110
111 free(buffer);
112 free(buffer2);
113 close(fd);
114 return 0;
115}
116
San Mehatb78a32c2010-01-10 13:02:12 -0800117void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize,
118 const char *name, unsigned flags) {
119 memset(io, 0, dataSize);
120 io->data_size = dataSize;
121 io->data_start = sizeof(struct dm_ioctl);
122 io->version[0] = 4;
123 io->version[1] = 0;
124 io->version[2] = 0;
125 io->flags = flags;
San Mehatd9a4e352010-03-12 13:32:47 -0800126 if (name) {
Chih-Wei Huang7929deb2013-02-10 22:57:14 +0800127 size_t ret = strlcpy(io->name, name, sizeof(io->name));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700128 if (ret >= sizeof(io->name))
129 abort();
San Mehatd9a4e352010-03-12 13:32:47 -0800130 }
San Mehatb78a32c2010-01-10 13:02:12 -0800131}
132
133int Devmapper::lookupActive(const char *name, char *ubuffer, size_t len) {
Peter Bohm092aa1c2011-04-01 12:35:25 +0200134 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800135 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700136 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800137 return -1;
138 }
139
140 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700141 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700142 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800143 free(buffer);
144 return -1;
145 }
146
147 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
148
Peter Bohm092aa1c2011-04-01 12:35:25 +0200149 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800150 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat8b8f71b2010-01-11 09:17:25 -0800151 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700152 SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800153 }
154 free(buffer);
155 close(fd);
156 return -1;
157 }
158 close(fd);
159
160 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
161 free(buffer);
San Mehatb78a32c2010-01-10 13:02:12 -0800162 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
163 return 0;
164}
165
San Mehat8b8f71b2010-01-11 09:17:25 -0800166int Devmapper::create(const char *name, const char *loopFile, const char *key,
167 unsigned int numSectors, char *ubuffer, size_t len) {
Peter Bohm092aa1c2011-04-01 12:35:25 +0200168 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800169 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700170 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800171 return -1;
172 }
173
174 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700175 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700176 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800177 free(buffer);
178 return -1;
179 }
180
181 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
182
183 // Create the DM device
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_CREATE, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700187 SLOGE("Error creating device mapping (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800188 free(buffer);
189 close(fd);
190 return -1;
191 }
192
193 // Set the legacy geometry
Peter Bohm092aa1c2011-04-01 12:35:25 +0200194 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800195
196 char *geoParams = buffer + sizeof(struct dm_ioctl);
197 // 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
198 strcpy(geoParams, "0 64 63 0");
199 geoParams += strlen(geoParams) + 1;
200 geoParams = (char *) _align(geoParams, 8);
201 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700202 SLOGE("Error setting device geometry (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800203 free(buffer);
204 close(fd);
205 return -1;
206 }
207
208 // Retrieve the device number we were allocated
Peter Bohm092aa1c2011-04-01 12:35:25 +0200209 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800210 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700211 SLOGE("Error retrieving devmapper status (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800212 free(buffer);
213 close(fd);
214 return -1;
215 }
216
217 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
San Mehatb78a32c2010-01-10 13:02:12 -0800218 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
219
220 // Load the table
221 struct dm_target_spec *tgt;
222 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
223
Peter Bohm092aa1c2011-04-01 12:35:25 +0200224 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
San Mehatb78a32c2010-01-10 13:02:12 -0800225 io->target_count = 1;
226 tgt->status = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -0800227
San Mehatb78a32c2010-01-10 13:02:12 -0800228 tgt->sector_start = 0;
San Mehat8b8f71b2010-01-11 09:17:25 -0800229 tgt->length = numSectors;
San Mehatfcf24fe2010-03-03 12:37:32 -0800230
Peter Bohm092aa1c2011-04-01 12:35:25 +0200231 strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
San Mehatb78a32c2010-01-10 13:02:12 -0800232
233 char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
Peter Bohm092aa1c2011-04-01 12:35:25 +0200234 snprintf(cryptParams,
235 DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
236 "twofish %s 0 %s 0", key, loopFile);
San Mehatb78a32c2010-01-10 13:02:12 -0800237 cryptParams += strlen(cryptParams) + 1;
238 cryptParams = (char *) _align(cryptParams, 8);
239 tgt->next = cryptParams - buffer;
240
241 if (ioctl(fd, DM_TABLE_LOAD, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700242 SLOGE("Error loading mapping table (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800243 free(buffer);
244 close(fd);
245 return -1;
246 }
247
248 // Resume the new table
Peter Bohm092aa1c2011-04-01 12:35:25 +0200249 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800250
251 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700252 SLOGE("Error Resuming (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800253 free(buffer);
254 close(fd);
255 return -1;
256 }
257
258 free(buffer);
259
San Mehat8c940ef2010-02-13 14:19:53 -0800260 close(fd);
San Mehatb78a32c2010-01-10 13:02:12 -0800261 return 0;
262}
263
264int Devmapper::destroy(const char *name) {
Peter Bohm092aa1c2011-04-01 12:35:25 +0200265 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehatb78a32c2010-01-10 13:02:12 -0800266 if (!buffer) {
San Mehat97ac40e2010-03-24 10:24:19 -0700267 SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800268 return -1;
269 }
270
271 int fd;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700272 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700273 SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800274 free(buffer);
275 return -1;
276 }
277
278 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
279
280 // Create the DM device
Peter Bohm092aa1c2011-04-01 12:35:25 +0200281 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800282
283 if (ioctl(fd, DM_DEV_REMOVE, io)) {
San Mehat0586d542010-01-12 15:38:59 -0800284 if (errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700285 SLOGE("Error destroying device mapping (%s)", strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800286 }
San Mehatb78a32c2010-01-10 13:02:12 -0800287 free(buffer);
288 close(fd);
289 return -1;
290 }
291
292 free(buffer);
293 close(fd);
294 return 0;
295}
296
297void *Devmapper::_align(void *ptr, unsigned int a)
298{
Tim Murray8439dc92014-12-15 11:56:11 -0800299 unsigned long agn = --a;
San Mehatb78a32c2010-01-10 13:02:12 -0800300
301 return (void *) (((unsigned long) ptr + agn) & ~agn);
302}