2010-01-10 22:02:12 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-09-22 01:08:43 +02:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2010-01-10 22:02:12 +01:00
|
|
|
#include <stdio.h>
|
2010-11-16 19:41:53 +01:00
|
|
|
#include <stdlib.h>
|
2010-01-10 22:02:12 +01:00
|
|
|
#include <string.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <unistd.h>
|
2010-01-10 22:02:12 +01:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <sys/types.h>
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2010-03-12 22:32:47 +01:00
|
|
|
#include <linux/kdev_t.h>
|
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <android-base/strings.h>
|
2017-09-22 01:08:43 +02:00
|
|
|
#include <utils/Trace.h>
|
2010-03-12 22:32:47 +01:00
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
#include "Devmapper.h"
|
|
|
|
|
2011-04-01 12:35:25 +02:00
|
|
|
#define DEVMAPPER_BUFFER_SIZE 4096
|
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
|
|
|
static const char* kVoldPrefix = "vold:";
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
void Devmapper::ioctlInit(struct dm_ioctl* io, size_t dataSize, const char* name, unsigned flags) {
|
2010-01-10 22:02:12 +01:00
|
|
|
memset(io, 0, dataSize);
|
|
|
|
io->data_size = dataSize;
|
|
|
|
io->data_start = sizeof(struct dm_ioctl);
|
|
|
|
io->version[0] = 4;
|
|
|
|
io->version[1] = 0;
|
|
|
|
io->version[2] = 0;
|
|
|
|
io->flags = flags;
|
2010-03-12 22:32:47 +01:00
|
|
|
if (name) {
|
2013-02-10 15:57:14 +01:00
|
|
|
size_t ret = strlcpy(io->name, name, sizeof(io->name));
|
2018-09-18 22:30:21 +02:00
|
|
|
if (ret >= sizeof(io->name)) abort();
|
2010-03-12 22:32:47 +01:00
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
int Devmapper::create(const char* name_raw, const char* loopFile, const char* key,
|
|
|
|
unsigned long numSectors, char* ubuffer, size_t len) {
|
2017-09-11 18:32:01 +02:00
|
|
|
auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
|
|
|
|
const char* name = name_string.c_str();
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
char* buffer = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
|
2010-01-10 22:02:12 +01:00
|
|
|
if (!buffer) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed malloc";
|
2010-01-10 22:02:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd;
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed open";
|
2010-01-10 22:02:12 +01:00
|
|
|
free(buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
struct dm_ioctl* io = (struct dm_ioctl*)buffer;
|
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
// Create the DM device
|
2011-04-01 12:35:25 +02:00
|
|
|
ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
|
2010-01-10 22:02:12 +01:00
|
|
|
|
|
|
|
if (ioctl(fd, DM_DEV_CREATE, io)) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed DM_DEV_CREATE";
|
2010-01-10 22:02:12 +01:00
|
|
|
free(buffer);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the legacy geometry
|
2011-04-01 12:35:25 +02:00
|
|
|
ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
char* geoParams = buffer + sizeof(struct dm_ioctl);
|
2010-01-10 22:02:12 +01:00
|
|
|
// 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
|
2017-03-28 00:18:50 +02:00
|
|
|
strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
|
2010-01-10 22:02:12 +01:00
|
|
|
geoParams += strlen(geoParams) + 1;
|
2018-09-18 22:30:21 +02:00
|
|
|
geoParams = (char*)_align(geoParams, 8);
|
2010-01-10 22:02:12 +01:00
|
|
|
if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed DM_DEV_SET_GEOMETRY";
|
2010-01-10 22:02:12 +01:00
|
|
|
free(buffer);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the device number we were allocated
|
2011-04-01 12:35:25 +02:00
|
|
|
ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
|
2010-01-10 22:02:12 +01:00
|
|
|
if (ioctl(fd, DM_DEV_STATUS, io)) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed DM_DEV_STATUS";
|
2010-01-10 22:02:12 +01:00
|
|
|
free(buffer);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
|
|
|
|
snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
|
|
|
|
|
|
|
|
// Load the table
|
2018-09-18 22:30:21 +02:00
|
|
|
struct dm_target_spec* tgt;
|
|
|
|
tgt = (struct dm_target_spec*)&buffer[sizeof(struct dm_ioctl)];
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2011-04-01 12:35:25 +02:00
|
|
|
ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
|
2010-01-10 22:02:12 +01:00
|
|
|
io->target_count = 1;
|
|
|
|
tgt->status = 0;
|
2010-03-03 21:37:32 +01:00
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
tgt->sector_start = 0;
|
2010-01-11 18:17:25 +01:00
|
|
|
tgt->length = numSectors;
|
2010-03-03 21:37:32 +01:00
|
|
|
|
2011-04-01 12:35:25 +02:00
|
|
|
strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
char* cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
|
2011-04-01 12:35:25 +02:00
|
|
|
snprintf(cryptParams,
|
2018-09-18 22:30:21 +02:00
|
|
|
DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
|
|
|
|
"twofish %s 0 %s 0", key, loopFile);
|
2010-01-10 22:02:12 +01:00
|
|
|
cryptParams += strlen(cryptParams) + 1;
|
2018-09-18 22:30:21 +02:00
|
|
|
cryptParams = (char*)_align(cryptParams, 8);
|
2010-01-10 22:02:12 +01:00
|
|
|
tgt->next = cryptParams - buffer;
|
|
|
|
|
|
|
|
if (ioctl(fd, DM_TABLE_LOAD, io)) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed DM_TABLE_LOAD";
|
2010-01-10 22:02:12 +01:00
|
|
|
free(buffer);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume the new table
|
2011-04-01 12:35:25 +02:00
|
|
|
ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
|
2010-01-10 22:02:12 +01:00
|
|
|
|
|
|
|
if (ioctl(fd, DM_DEV_SUSPEND, io)) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed DM_DEV_SUSPEND";
|
2010-01-10 22:02:12 +01:00
|
|
|
free(buffer);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
2010-02-13 23:19:53 +01:00
|
|
|
close(fd);
|
2010-01-10 22:02:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
int Devmapper::destroy(const char* name_raw) {
|
2017-09-11 18:32:01 +02:00
|
|
|
auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
|
|
|
|
const char* name = name_string.c_str();
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
char* buffer = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
|
2010-01-10 22:02:12 +01:00
|
|
|
if (!buffer) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed malloc";
|
2010-01-10 22:02:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd;
|
2015-04-09 06:07:21 +02:00
|
|
|
if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed open";
|
2010-01-10 22:02:12 +01:00
|
|
|
free(buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
struct dm_ioctl* io = (struct dm_ioctl*)buffer;
|
|
|
|
|
2010-01-10 22:02:12 +01:00
|
|
|
// Create the DM device
|
2011-04-01 12:35:25 +02:00
|
|
|
ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
|
2010-01-10 22:02:12 +01:00
|
|
|
|
|
|
|
if (ioctl(fd, DM_DEV_REMOVE, io)) {
|
2010-01-13 00:38:59 +01:00
|
|
|
if (errno != ENXIO) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed DM_DEV_REMOVE";
|
2010-01-13 00:38:59 +01:00
|
|
|
}
|
2010-01-10 22:02:12 +01:00
|
|
|
free(buffer);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
int Devmapper::destroyAll() {
|
2017-09-22 01:08:43 +02:00
|
|
|
ATRACE_NAME("Devmapper::destroyAll");
|
2018-09-18 22:30:21 +02:00
|
|
|
char* buffer = (char*)malloc(1024 * 64);
|
2017-09-11 18:32:01 +02:00
|
|
|
if (!buffer) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed malloc";
|
2017-09-11 18:32:01 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(buffer, 0, (1024 * 64));
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
char* buffer2 = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
|
2017-09-11 18:32:01 +02:00
|
|
|
if (!buffer2) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed malloc";
|
2017-09-11 18:32:01 +02:00
|
|
|
free(buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed open";
|
2017-09-11 18:32:01 +02:00
|
|
|
free(buffer);
|
|
|
|
free(buffer2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
struct dm_ioctl* io = (struct dm_ioctl*)buffer;
|
2017-09-11 18:32:01 +02:00
|
|
|
ioctlInit(io, (1024 * 64), NULL, 0);
|
|
|
|
|
|
|
|
if (ioctl(fd, DM_LIST_DEVICES, io)) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Failed DM_LIST_DEVICES";
|
2017-09-11 18:32:01 +02:00
|
|
|
free(buffer);
|
|
|
|
free(buffer2);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
struct dm_name_list* n = (struct dm_name_list*)(((char*)buffer) + io->data_start);
|
2017-09-11 18:32:01 +02:00
|
|
|
if (!n->dev) {
|
|
|
|
free(buffer);
|
|
|
|
free(buffer2);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned nxt = 0;
|
|
|
|
do {
|
2018-09-18 22:30:21 +02:00
|
|
|
n = (struct dm_name_list*)(((char*)n) + nxt);
|
2017-10-07 02:02:53 +02:00
|
|
|
auto name = std::string(n->name);
|
|
|
|
if (android::base::StartsWith(name, kVoldPrefix)) {
|
|
|
|
LOG(DEBUG) << "Tearing down stale dm device named " << name;
|
2017-09-11 18:32:01 +02:00
|
|
|
|
|
|
|
memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
|
2018-09-18 22:30:21 +02:00
|
|
|
struct dm_ioctl* io2 = (struct dm_ioctl*)buffer2;
|
2017-09-11 18:32:01 +02:00
|
|
|
ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
|
|
|
|
if (ioctl(fd, DM_DEV_REMOVE, io2)) {
|
|
|
|
if (errno != ENXIO) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(WARNING) << "Failed to destroy dm device named " << name;
|
2017-09-11 18:32:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-07 02:02:53 +02:00
|
|
|
LOG(VERBOSE) << "Found unmanaged dm device named " << name;
|
2017-09-11 18:32:01 +02:00
|
|
|
}
|
|
|
|
nxt = n->next;
|
|
|
|
} while (nxt);
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
free(buffer2);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
void* Devmapper::_align(void* ptr, unsigned int a) {
|
|
|
|
unsigned long agn = --a;
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
return (void*)(((unsigned long)ptr + agn) & ~agn);
|
2010-01-10 22:02:12 +01:00
|
|
|
}
|