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
27 #include <sys/types.h>
29 #include <linux/kdev_t.h>
31 #include <android-base/logging.h>
32 #include <android-base/stringprintf.h>
33 #include <android-base/strings.h>
35 #include <utils/Trace.h>
37 #include "Devmapper.h"
39 using android::base::StringPrintf;
40 using namespace android::dm;
42 static const char* kVoldPrefix = "vold:";
44 int Devmapper::create(const char* name_raw, const char* loopFile, const char* key,
45 unsigned long numSectors, char* ubuffer, size_t len) {
46 auto& dm = DeviceMapper::Instance();
47 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
50 table.Emplace<DmTargetCrypt>(0, numSectors, "twofish", key, 0, loopFile, 0);
52 if (!dm.CreateDevice(name_string, table)) {
53 LOG(ERROR) << "Failed to create device-mapper device " << name_string;
58 if (!dm.GetDmDevicePathByName(name_string, &path)) {
59 LOG(ERROR) << "Failed to get device-mapper device path for " << name_string;
62 snprintf(ubuffer, len, "%s", path.c_str());
66 int Devmapper::destroy(const char* name_raw) {
67 auto& dm = DeviceMapper::Instance();
69 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
70 if (!dm.DeleteDevice(name_string)) {
72 PLOG(ERROR) << "Failed DM_DEV_REMOVE";
79 int Devmapper::destroyAll() {
80 ATRACE_NAME("Devmapper::destroyAll");
82 auto& dm = DeviceMapper::Instance();
83 std::vector<DeviceMapper::DmBlockDevice> devices;
84 if (!dm.GetAvailableDevices(&devices)) {
85 LOG(ERROR) << "Failed to get dm devices";
89 for (const auto& device : devices) {
90 if (android::base::StartsWith(device.name(), kVoldPrefix)) {
91 LOG(DEBUG) << "Tearing down stale dm device named " << device.name();
92 if (!dm.DeleteDevice(device.name())) {
94 PLOG(WARNING) << "Failed to destroy dm device named " << device.name();
98 LOG(DEBUG) << "Found unmanaged dm device named " << device.name();