San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER |
| 18 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
Olivier Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 23 | #include <string.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 25 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 26 | #include <sys/stat.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 28 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 29 | #include <linux/kdev_t.h> |
| 30 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 31 | #include <android-base/logging.h> |
| 32 | #include <android-base/stringprintf.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 33 | #include <android-base/strings.h> |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 34 | #include <libdm/dm.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 35 | #include <utils/Trace.h> |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 36 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 37 | #include "Devmapper.h" |
| 38 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 39 | using android::base::StringPrintf; |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 40 | using namespace android::dm; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 41 | |
| 42 | static const char* kVoldPrefix = "vold:"; |
| 43 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 44 | int Devmapper::create(const char* name_raw, const char* loopFile, const char* key, |
| 45 | unsigned long numSectors, char* ubuffer, size_t len) { |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 46 | auto& dm = DeviceMapper::Instance(); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 47 | auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 48 | |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 49 | 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 Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 54 | return -1; |
| 55 | } |
| 56 | |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 57 | std::string path; |
| 58 | if (!dm.GetDmDevicePathByName(name_string, &path)) { |
| 59 | LOG(ERROR) << "Failed to get device-mapper device path for " << name_string; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 60 | return -1; |
| 61 | } |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 62 | snprintf(ubuffer, len, "%s", path.c_str()); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 66 | int Devmapper::destroy(const char* name_raw) { |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 67 | auto& dm = DeviceMapper::Instance(); |
| 68 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 69 | auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw); |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 70 | if (!dm.DeleteDevice(name_string)) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 71 | if (errno != ENXIO) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 72 | PLOG(ERROR) << "Failed DM_DEV_REMOVE"; |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 73 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 74 | return -1; |
| 75 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 79 | int Devmapper::destroyAll() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 80 | ATRACE_NAME("Devmapper::destroyAll"); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 81 | |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 82 | auto& dm = DeviceMapper::Instance(); |
| 83 | std::vector<DeviceMapper::DmBlockDevice> devices; |
| 84 | if (!dm.GetAvailableDevices(&devices)) { |
| 85 | LOG(ERROR) << "Failed to get dm devices"; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 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())) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 93 | if (errno != ENXIO) { |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 94 | PLOG(WARNING) << "Failed to destroy dm device named " << device.name(); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } else { |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 98 | LOG(DEBUG) << "Found unmanaged dm device named " << device.name(); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 99 | } |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 100 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 101 | return 0; |
| 102 | } |