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/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>
|
2019-05-13 22:02:54 +02:00
|
|
|
#include <libdm/dm.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"
|
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
using android::base::StringPrintf;
|
2019-05-13 22:02:54 +02:00
|
|
|
using namespace android::dm;
|
2017-09-11 18:32:01 +02:00
|
|
|
|
|
|
|
static const char* kVoldPrefix = "vold:";
|
|
|
|
|
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) {
|
2019-05-13 22:02:54 +02:00
|
|
|
auto& dm = DeviceMapper::Instance();
|
2017-09-11 18:32:01 +02:00
|
|
|
auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
DmTable table;
|
|
|
|
table.Emplace<DmTargetCrypt>(0, numSectors, "twofish", key, 0, loopFile, 0);
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
if (!dm.CreateDevice(name_string, table)) {
|
|
|
|
LOG(ERROR) << "Failed to create device-mapper device " << name_string;
|
2010-01-10 22:02:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
std::string path;
|
|
|
|
if (!dm.GetDmDevicePathByName(name_string, &path)) {
|
|
|
|
LOG(ERROR) << "Failed to get device-mapper device path for " << name_string;
|
2010-01-10 22:02:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2019-05-13 22:02:54 +02:00
|
|
|
snprintf(ubuffer, len, "%s", path.c_str());
|
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) {
|
2019-05-13 22:02:54 +02:00
|
|
|
auto& dm = DeviceMapper::Instance();
|
2010-01-10 22:02:12 +01:00
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
|
|
|
|
if (!dm.DeleteDevice(name_string)) {
|
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
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
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");
|
2017-09-11 18:32:01 +02:00
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
auto& dm = DeviceMapper::Instance();
|
|
|
|
std::vector<DeviceMapper::DmBlockDevice> devices;
|
|
|
|
if (!dm.GetAvailableDevices(&devices)) {
|
|
|
|
LOG(ERROR) << "Failed to get dm devices";
|
2017-09-11 18:32:01 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
for (const auto& device : devices) {
|
|
|
|
if (android::base::StartsWith(device.name(), kVoldPrefix)) {
|
|
|
|
LOG(DEBUG) << "Tearing down stale dm device named " << device.name();
|
|
|
|
if (!dm.DeleteDevice(device.name())) {
|
2017-09-11 18:32:01 +02:00
|
|
|
if (errno != ENXIO) {
|
2019-05-13 22:02:54 +02:00
|
|
|
PLOG(WARNING) << "Failed to destroy dm device named " << device.name();
|
2017-09-11 18:32:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-13 22:02:54 +02:00
|
|
|
LOG(DEBUG) << "Found unmanaged dm device named " << device.name();
|
2017-09-11 18:32:01 +02:00
|
|
|
}
|
2019-05-13 22:02:54 +02:00
|
|
|
}
|
2017-09-11 18:32:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|