2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
29 #include <linux/kdev_t.h>
31 #define LOG_TAG "Vold"
33 #include <cutils/log.h>
35 #include <sysutils/SocketClient.h>
37 #include "Devmapper.h"
39 #define DEVMAPPER_BUFFER_SIZE 4096
41 int Devmapper::dumpState(SocketClient *c) {
43 char *buffer = (char *) malloc(1024 * 64);
45 SLOGE("Error allocating memory (%s)", strerror(errno));
48 memset(buffer, 0, (1024 * 64));
50 char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
52 SLOGE("Error allocating memory (%s)", strerror(errno));
58 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
59 SLOGE("Error opening devmapper (%s)", strerror(errno));
65 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
66 ioctlInit(io, (1024 * 64), NULL, 0);
68 if (ioctl(fd, DM_LIST_DEVICES, io)) {
69 SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno));
76 struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
86 n = (struct dm_name_list *) (((char *) n) + nxt);
88 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
89 struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
90 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
91 if (ioctl(fd, DM_DEV_STATUS, io2)) {
93 SLOGE("DM_DEV_STATUS ioctl failed (%s)", strerror(errno));
100 asprintf(&tmp, "%s %llu:%llu (no status available)", n->name, MAJOR(n->dev), MINOR(n->dev));
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),
106 c->sendMsg(0, tmp, false);
117 void 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);
127 size_t ret = strlcpy(io->name, name, sizeof(io->name));
128 if (ret >= sizeof(io->name))
133 int Devmapper::lookupActive(const char *name, char *ubuffer, size_t len) {
134 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
136 SLOGE("Error allocating memory (%s)", strerror(errno));
141 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
142 SLOGE("Error opening devmapper (%s)", strerror(errno));
147 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
149 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
150 if (ioctl(fd, DM_DEV_STATUS, io)) {
151 if (errno != ENXIO) {
152 SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
160 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
162 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
166 int Devmapper::create(const char *name, const char *loopFile, const char *key,
167 unsigned long numSectors, char *ubuffer, size_t len) {
168 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
170 SLOGE("Error allocating memory (%s)", strerror(errno));
175 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
176 SLOGE("Error opening devmapper (%s)", strerror(errno));
181 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
183 // Create the DM device
184 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
186 if (ioctl(fd, DM_DEV_CREATE, io)) {
187 SLOGE("Error creating device mapping (%s)", strerror(errno));
193 // Set the legacy geometry
194 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
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 strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
199 geoParams += strlen(geoParams) + 1;
200 geoParams = (char *) _align(geoParams, 8);
201 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
202 SLOGE("Error setting device geometry (%s)", strerror(errno));
208 // Retrieve the device number we were allocated
209 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
210 if (ioctl(fd, DM_DEV_STATUS, io)) {
211 SLOGE("Error retrieving devmapper status (%s)", strerror(errno));
217 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
218 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
221 struct dm_target_spec *tgt;
222 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
224 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
225 io->target_count = 1;
228 tgt->sector_start = 0;
229 tgt->length = numSectors;
231 strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
233 char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
234 snprintf(cryptParams,
235 DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
236 "twofish %s 0 %s 0", key, loopFile);
237 cryptParams += strlen(cryptParams) + 1;
238 cryptParams = (char *) _align(cryptParams, 8);
239 tgt->next = cryptParams - buffer;
241 if (ioctl(fd, DM_TABLE_LOAD, io)) {
242 SLOGE("Error loading mapping table (%s)", strerror(errno));
248 // Resume the new table
249 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
251 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
252 SLOGE("Error Resuming (%s)", strerror(errno));
264 int Devmapper::destroy(const char *name) {
265 char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
267 SLOGE("Error allocating memory (%s)", strerror(errno));
272 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
273 SLOGE("Error opening devmapper (%s)", strerror(errno));
278 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
280 // Create the DM device
281 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
283 if (ioctl(fd, DM_DEV_REMOVE, io)) {
284 if (errno != ENXIO) {
285 SLOGE("Error destroying device mapping (%s)", strerror(errno));
297 void *Devmapper::_align(void *ptr, unsigned int a)
299 unsigned long agn = --a;
301 return (void *) (((unsigned long) ptr + agn) & ~agn);