blob: d55d92d10ac3184c66754cf34b8acb95729345b3 [file] [log] [blame]
San Mehatb78a32c2010-01-10 13:02:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Jeff Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Paul Crowley14c8c072018-09-18 13:30:21 -070019#include <errno.h>
20#include <fcntl.h>
San Mehatb78a32c2010-01-10 13:02:12 -080021#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080022#include <stdlib.h>
San Mehatb78a32c2010-01-10 13:02:12 -080023#include <string.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070024#include <unistd.h>
San Mehatb78a32c2010-01-10 13:02:12 -080025
San Mehatb78a32c2010-01-10 13:02:12 -080026#include <sys/stat.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070027#include <sys/types.h>
San Mehatb78a32c2010-01-10 13:02:12 -080028
San Mehatd9a4e352010-03-12 13:32:47 -080029#include <linux/kdev_t.h>
30
Jeff Sharkey11c2d382017-09-11 10:32:01 -060031#include <android-base/logging.h>
32#include <android-base/stringprintf.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070033#include <android-base/strings.h>
David Andersonb9224732019-05-13 13:02:54 -070034#include <libdm/dm.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060035#include <utils/Trace.h>
San Mehatd9a4e352010-03-12 13:32:47 -080036
San Mehatb78a32c2010-01-10 13:02:12 -080037#include "Devmapper.h"
38
Jeff Sharkey11c2d382017-09-11 10:32:01 -060039using android::base::StringPrintf;
David Andersonb9224732019-05-13 13:02:54 -070040using namespace android::dm;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060041
42static const char* kVoldPrefix = "vold:";
43
Paul Crowley14c8c072018-09-18 13:30:21 -070044int Devmapper::create(const char* name_raw, const char* loopFile, const char* key,
45 unsigned long numSectors, char* ubuffer, size_t len) {
David Andersonb9224732019-05-13 13:02:54 -070046 auto& dm = DeviceMapper::Instance();
Jeff Sharkey11c2d382017-09-11 10:32:01 -060047 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
Jeff Sharkey11c2d382017-09-11 10:32:01 -060048
David Andersonb9224732019-05-13 13:02:54 -070049 DmTable table;
50 table.Emplace<DmTargetCrypt>(0, numSectors, "twofish", key, 0, loopFile, 0);
51
52 if (!dm.CreateDevice(name_string, table)) {
53 LOG(ERROR) << "Failed to create device-mapper device " << name_string;
San Mehatb78a32c2010-01-10 13:02:12 -080054 return -1;
55 }
56
David Andersonb9224732019-05-13 13:02:54 -070057 std::string path;
58 if (!dm.GetDmDevicePathByName(name_string, &path)) {
59 LOG(ERROR) << "Failed to get device-mapper device path for " << name_string;
San Mehatb78a32c2010-01-10 13:02:12 -080060 return -1;
61 }
David Andersonb9224732019-05-13 13:02:54 -070062 snprintf(ubuffer, len, "%s", path.c_str());
San Mehatb78a32c2010-01-10 13:02:12 -080063 return 0;
64}
65
Paul Crowley14c8c072018-09-18 13:30:21 -070066int Devmapper::destroy(const char* name_raw) {
David Andersonb9224732019-05-13 13:02:54 -070067 auto& dm = DeviceMapper::Instance();
68
Jeff Sharkey11c2d382017-09-11 10:32:01 -060069 auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
David Andersonb9224732019-05-13 13:02:54 -070070 if (!dm.DeleteDevice(name_string)) {
San Mehat0586d542010-01-12 15:38:59 -080071 if (errno != ENXIO) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060072 PLOG(ERROR) << "Failed DM_DEV_REMOVE";
San Mehat0586d542010-01-12 15:38:59 -080073 }
San Mehatb78a32c2010-01-10 13:02:12 -080074 return -1;
75 }
San Mehatb78a32c2010-01-10 13:02:12 -080076 return 0;
77}
78
Jeff Sharkey11c2d382017-09-11 10:32:01 -060079int Devmapper::destroyAll() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -060080 ATRACE_NAME("Devmapper::destroyAll");
Jeff Sharkey11c2d382017-09-11 10:32:01 -060081
David Andersonb9224732019-05-13 13:02:54 -070082 auto& dm = DeviceMapper::Instance();
83 std::vector<DeviceMapper::DmBlockDevice> devices;
84 if (!dm.GetAvailableDevices(&devices)) {
85 LOG(ERROR) << "Failed to get dm devices";
Jeff Sharkey11c2d382017-09-11 10:32:01 -060086 return -1;
87 }
88
David Andersonb9224732019-05-13 13:02:54 -070089 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())) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060093 if (errno != ENXIO) {
David Andersonb9224732019-05-13 13:02:54 -070094 PLOG(WARNING) << "Failed to destroy dm device named " << device.name();
Jeff Sharkey11c2d382017-09-11 10:32:01 -060095 }
96 }
97 } else {
David Andersonb9224732019-05-13 13:02:54 -070098 LOG(DEBUG) << "Found unmanaged dm device named " << device.name();
Jeff Sharkey11c2d382017-09-11 10:32:01 -060099 }
David Andersonb9224732019-05-13 13:02:54 -0700100 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600101 return 0;
102}