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.
17 #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
26 #include <sys/ioctl.h>
28 #include <sys/types.h>
30 #include <linux/kdev_t.h>
32 #include <android-base/logging.h>
33 #include <android-base/stringprintf.h>
34 #include <android-base/strings.h>
35 #include <utils/Trace.h>
37 #include "Devmapper.h"
39 #define DEVMAPPER_BUFFER_SIZE 4096
41 using android::base::StringPrintf;
43 static const char* kVoldPrefix = "vold:";
45 void Devmapper::ioctlInit(struct dm_ioctl* io, size_t dataSize, const char* name, unsigned flags) {
46 memset(io, 0, dataSize);
47 io->data_size = dataSize;
48 io->data_start = sizeof(struct dm_ioctl);
54 size_t ret = strlcpy(io->name, name, sizeof(io->name));
55 if (ret >= sizeof(io->name)) abort();
59 int Devmapper::create(const char* name_raw, const char* loopFile, const char* key,
60 unsigned long numSectors, char* ubuffer, size_t len) {
61 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
62 const char* name = name_string.c_str();
64 char* buffer = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
66 PLOG(ERROR) << "Failed malloc";
71 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
72 PLOG(ERROR) << "Failed open";
77 struct dm_ioctl* io = (struct dm_ioctl*)buffer;
79 // Create the DM device
80 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
82 if (ioctl(fd, DM_DEV_CREATE, io)) {
83 PLOG(ERROR) << "Failed DM_DEV_CREATE";
89 // Set the legacy geometry
90 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
92 char* geoParams = buffer + sizeof(struct dm_ioctl);
93 // 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
94 strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
95 geoParams += strlen(geoParams) + 1;
96 geoParams = (char*)_align(geoParams, 8);
97 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
98 PLOG(ERROR) << "Failed DM_DEV_SET_GEOMETRY";
104 // Retrieve the device number we were allocated
105 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
106 if (ioctl(fd, DM_DEV_STATUS, io)) {
107 PLOG(ERROR) << "Failed DM_DEV_STATUS";
113 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
114 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
117 struct dm_target_spec* tgt;
118 tgt = (struct dm_target_spec*)&buffer[sizeof(struct dm_ioctl)];
120 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
121 io->target_count = 1;
124 tgt->sector_start = 0;
125 tgt->length = numSectors;
127 strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
129 char* cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
130 snprintf(cryptParams,
131 DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
132 "twofish %s 0 %s 0", key, loopFile);
133 cryptParams += strlen(cryptParams) + 1;
134 cryptParams = (char*)_align(cryptParams, 8);
135 tgt->next = cryptParams - buffer;
137 if (ioctl(fd, DM_TABLE_LOAD, io)) {
138 PLOG(ERROR) << "Failed DM_TABLE_LOAD";
144 // Resume the new table
145 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
147 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
148 PLOG(ERROR) << "Failed DM_DEV_SUSPEND";
160 int Devmapper::destroy(const char* name_raw) {
161 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
162 const char* name = name_string.c_str();
164 char* buffer = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
166 PLOG(ERROR) << "Failed malloc";
171 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
172 PLOG(ERROR) << "Failed open";
177 struct dm_ioctl* io = (struct dm_ioctl*)buffer;
179 // Create the DM device
180 ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
182 if (ioctl(fd, DM_DEV_REMOVE, io)) {
183 if (errno != ENXIO) {
184 PLOG(ERROR) << "Failed DM_DEV_REMOVE";
196 int Devmapper::destroyAll() {
197 ATRACE_NAME("Devmapper::destroyAll");
198 char* buffer = (char*)malloc(1024 * 64);
200 PLOG(ERROR) << "Failed malloc";
203 memset(buffer, 0, (1024 * 64));
205 char* buffer2 = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
207 PLOG(ERROR) << "Failed malloc";
213 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
214 PLOG(ERROR) << "Failed open";
220 struct dm_ioctl* io = (struct dm_ioctl*)buffer;
221 ioctlInit(io, (1024 * 64), NULL, 0);
223 if (ioctl(fd, DM_LIST_DEVICES, io)) {
224 PLOG(ERROR) << "Failed DM_LIST_DEVICES";
231 struct dm_name_list* n = (struct dm_name_list*)(((char*)buffer) + io->data_start);
241 n = (struct dm_name_list*)(((char*)n) + nxt);
242 auto name = std::string(n->name);
243 if (android::base::StartsWith(name, kVoldPrefix)) {
244 LOG(DEBUG) << "Tearing down stale dm device named " << name;
246 memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
247 struct dm_ioctl* io2 = (struct dm_ioctl*)buffer2;
248 ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
249 if (ioctl(fd, DM_DEV_REMOVE, io2)) {
250 if (errno != ENXIO) {
251 PLOG(WARNING) << "Failed to destroy dm device named " << name;
255 LOG(DEBUG) << "Found unmanaged dm device named " << name;
266 void* Devmapper::_align(void* ptr, unsigned int a) {
267 unsigned long agn = --a;
269 return (void*)(((unsigned long)ptr + agn) & ~agn);