David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person |
| 5 | * obtaining a copy of this software and associated documentation |
| 6 | * files (the "Software"), to deal in the Software without |
| 7 | * restriction, including without limitation the rights to use, copy, |
| 8 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | * of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "fs_mgr_dm_linear.h" |
| 26 | |
| 27 | #include <inttypes.h> |
| 28 | #include <linux/dm-ioctl.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include <sstream> |
| 35 | |
David Anderson | 9a53241 | 2018-10-12 13:51:52 -0700 | [diff] [blame] | 36 | #include <android-base/file.h> |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 37 | #include <android-base/logging.h> |
| 38 | #include <android-base/stringprintf.h> |
| 39 | #include <android-base/strings.h> |
| 40 | #include <android-base/unique_fd.h> |
David Anderson | 5c475c7 | 2019-06-11 17:40:49 -0700 | [diff] [blame] | 41 | #include <fs_mgr/file_wait.h> |
David Anderson | 6590df2 | 2018-06-14 15:03:53 -0700 | [diff] [blame] | 42 | #include <liblp/reader.h> |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 43 | |
| 44 | #include "fs_mgr_priv.h" |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | namespace fs_mgr { |
| 48 | |
David Anderson | 4b52440 | 2018-06-21 16:19:48 -0700 | [diff] [blame] | 49 | using DeviceMapper = android::dm::DeviceMapper; |
| 50 | using DmTable = android::dm::DmTable; |
| 51 | using DmTarget = android::dm::DmTarget; |
| 52 | using DmTargetZero = android::dm::DmTargetZero; |
| 53 | using DmTargetLinear = android::dm::DmTargetLinear; |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 54 | |
David Anderson | 0d2bcd4 | 2020-05-19 05:52:57 +0000 | [diff] [blame] | 55 | static bool GetPhysicalPartitionDevicePath(const CreateLogicalPartitionParams& params, |
Alistair Strachan | 1906d5f | 2018-11-27 17:39:00 -0800 | [diff] [blame] | 56 | const LpMetadataBlockDevice& block_device, |
David Anderson | 4929df0 | 2019-08-15 13:21:43 -0700 | [diff] [blame] | 57 | const std::string& super_device, std::string* result) { |
Alistair Strachan | 1906d5f | 2018-11-27 17:39:00 -0800 | [diff] [blame] | 58 | // If the super device is the source of this block device's metadata, |
| 59 | // make sure we use the correct super device (and not just "super", |
| 60 | // which might not exist.) |
David Anderson | 4929df0 | 2019-08-15 13:21:43 -0700 | [diff] [blame] | 61 | std::string name = GetBlockDevicePartitionName(block_device); |
David Anderson | 0d2bcd4 | 2020-05-19 05:52:57 +0000 | [diff] [blame] | 62 | if (android::base::StartsWith(name, "dm-")) { |
| 63 | // Device-mapper nodes are not normally allowed in LpMetadata, since |
| 64 | // they are not consistent across reboots. However for the purposes of |
| 65 | // testing it's useful to handle them. For example when running DSUs, |
| 66 | // userdata is a device-mapper device, and some stacking will result |
| 67 | // when using libfiemap. |
| 68 | *result = "/dev/block/" + name; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | auto opener = params.partition_opener; |
| 73 | std::string dev_string = opener->GetDeviceString(name); |
| 74 | if (GetMetadataSuperBlockDevice(*params.metadata) == &block_device) { |
| 75 | dev_string = opener->GetDeviceString(super_device); |
Alistair Strachan | 1906d5f | 2018-11-27 17:39:00 -0800 | [diff] [blame] | 76 | } |
David Anderson | 4929df0 | 2019-08-15 13:21:43 -0700 | [diff] [blame] | 77 | |
| 78 | // Note: device-mapper will not accept symlinks, so we must use realpath |
| 79 | // here. If the device string is a major:minor sequence, we don't need to |
| 80 | // to call Realpath (it would not work anyway). |
| 81 | if (android::base::StartsWith(dev_string, "/")) { |
| 82 | if (!android::base::Realpath(dev_string, result)) { |
| 83 | PERROR << "realpath: " << dev_string; |
| 84 | return false; |
| 85 | } |
| 86 | } else { |
| 87 | *result = dev_string; |
David Anderson | 9a53241 | 2018-10-12 13:51:52 -0700 | [diff] [blame] | 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 92 | bool CreateDmTableInternal(const CreateLogicalPartitionParams& params, DmTable* table) { |
| 93 | const auto& super_device = params.block_device; |
| 94 | |
David Anderson | 4b52440 | 2018-06-21 16:19:48 -0700 | [diff] [blame] | 95 | uint64_t sector = 0; |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 96 | for (size_t i = 0; i < params.partition->num_extents; i++) { |
| 97 | const auto& extent = params.metadata->extents[params.partition->first_extent_index + i]; |
David Anderson | 4b52440 | 2018-06-21 16:19:48 -0700 | [diff] [blame] | 98 | std::unique_ptr<DmTarget> target; |
| 99 | switch (extent.target_type) { |
| 100 | case LP_TARGET_TYPE_ZERO: |
| 101 | target = std::make_unique<DmTargetZero>(sector, extent.num_sectors); |
| 102 | break; |
David Anderson | 9a53241 | 2018-10-12 13:51:52 -0700 | [diff] [blame] | 103 | case LP_TARGET_TYPE_LINEAR: { |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 104 | const auto& block_device = params.metadata->block_devices[extent.target_source]; |
David Anderson | 4929df0 | 2019-08-15 13:21:43 -0700 | [diff] [blame] | 105 | std::string dev_string; |
David Anderson | 0d2bcd4 | 2020-05-19 05:52:57 +0000 | [diff] [blame] | 106 | if (!GetPhysicalPartitionDevicePath(params, block_device, super_device, |
| 107 | &dev_string)) { |
David Anderson | 9a53241 | 2018-10-12 13:51:52 -0700 | [diff] [blame] | 108 | LOG(ERROR) << "Unable to complete device-mapper table, unknown block device"; |
| 109 | return false; |
| 110 | } |
David Anderson | 4929df0 | 2019-08-15 13:21:43 -0700 | [diff] [blame] | 111 | target = std::make_unique<DmTargetLinear>(sector, extent.num_sectors, dev_string, |
David Anderson | 4b52440 | 2018-06-21 16:19:48 -0700 | [diff] [blame] | 112 | extent.target_data); |
| 113 | break; |
David Anderson | 9a53241 | 2018-10-12 13:51:52 -0700 | [diff] [blame] | 114 | } |
David Anderson | 4b52440 | 2018-06-21 16:19:48 -0700 | [diff] [blame] | 115 | default: |
| 116 | LOG(ERROR) << "Unknown target type in metadata: " << extent.target_type; |
| 117 | return false; |
| 118 | } |
| 119 | if (!table->AddTarget(std::move(target))) { |
| 120 | return false; |
| 121 | } |
| 122 | sector += extent.num_sectors; |
| 123 | } |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 124 | if (params.partition->attributes & LP_PARTITION_ATTR_READONLY) { |
David Anderson | 4b52440 | 2018-06-21 16:19:48 -0700 | [diff] [blame] | 125 | table->set_readonly(true); |
| 126 | } |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 127 | if (params.force_writable) { |
| 128 | table->set_readonly(false); |
| 129 | } |
David Anderson | 4b52440 | 2018-06-21 16:19:48 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 133 | bool CreateDmTable(CreateLogicalPartitionParams params, DmTable* table) { |
| 134 | CreateLogicalPartitionParams::OwnedData owned_data; |
| 135 | if (!params.InitDefaults(&owned_data)) return false; |
| 136 | return CreateDmTableInternal(params, table); |
| 137 | } |
| 138 | |
David Anderson | 6590df2 | 2018-06-14 15:03:53 -0700 | [diff] [blame] | 139 | bool CreateLogicalPartitions(const std::string& block_device) { |
| 140 | uint32_t slot = SlotNumberForSlotSuffix(fs_mgr_get_slot_suffix()); |
| 141 | auto metadata = ReadMetadata(block_device.c_str(), slot); |
| 142 | if (!metadata) { |
| 143 | LOG(ERROR) << "Could not read partition table."; |
| 144 | return true; |
| 145 | } |
Alistair Strachan | 1906d5f | 2018-11-27 17:39:00 -0800 | [diff] [blame] | 146 | return CreateLogicalPartitions(*metadata.get(), block_device); |
David Anderson | f89b1df | 2018-11-05 17:30:16 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | std::unique_ptr<LpMetadata> ReadCurrentMetadata(const std::string& block_device) { |
| 150 | uint32_t slot = SlotNumberForSlotSuffix(fs_mgr_get_slot_suffix()); |
| 151 | return ReadMetadata(block_device.c_str(), slot); |
| 152 | } |
| 153 | |
Alistair Strachan | 1906d5f | 2018-11-27 17:39:00 -0800 | [diff] [blame] | 154 | bool CreateLogicalPartitions(const LpMetadata& metadata, const std::string& super_device) { |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 155 | CreateLogicalPartitionParams params = { |
| 156 | .block_device = super_device, |
| 157 | .metadata = &metadata, |
| 158 | }; |
David Anderson | f89b1df | 2018-11-05 17:30:16 -0800 | [diff] [blame] | 159 | for (const auto& partition : metadata.partitions) { |
David Anderson | 6868cb9 | 2018-08-06 15:53:38 -0700 | [diff] [blame] | 160 | if (!partition.num_extents) { |
| 161 | LINFO << "Skipping zero-length logical partition: " << GetPartitionName(partition); |
| 162 | continue; |
| 163 | } |
David Anderson | f41c7bb | 2019-12-16 19:52:14 -0800 | [diff] [blame] | 164 | if (partition.attributes & LP_PARTITION_ATTR_DISABLED) { |
| 165 | LINFO << "Skipping disabled partition: " << GetPartitionName(partition); |
| 166 | continue; |
| 167 | } |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 168 | |
| 169 | params.partition = &partition; |
| 170 | |
| 171 | std::string ignore_path; |
| 172 | if (!CreateLogicalPartition(params, &ignore_path)) { |
David Anderson | 23de22d | 2018-07-20 16:28:04 -0700 | [diff] [blame] | 173 | LERROR << "Could not create logical partition: " << GetPartitionName(partition); |
| 174 | return false; |
| 175 | } |
David Anderson | 6590df2 | 2018-06-14 15:03:53 -0700 | [diff] [blame] | 176 | } |
David Anderson | 4b52440 | 2018-06-21 16:19:48 -0700 | [diff] [blame] | 177 | return true; |
David Anderson | 6590df2 | 2018-06-14 15:03:53 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 180 | bool CreateLogicalPartitionParams::InitDefaults(CreateLogicalPartitionParams::OwnedData* owned) { |
| 181 | if (block_device.empty()) { |
| 182 | LOG(ERROR) << "block_device is required for CreateLogicalPartition"; |
| 183 | return false; |
| 184 | } |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 185 | |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 186 | if (!partition_opener) { |
| 187 | owned->partition_opener = std::make_unique<PartitionOpener>(); |
| 188 | partition_opener = owned->partition_opener.get(); |
| 189 | } |
| 190 | |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 191 | // Read metadata if needed. |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 192 | if (!metadata) { |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 193 | if (!metadata_slot) { |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 194 | LOG(ERROR) << "Either metadata or a metadata slot must be specified."; |
| 195 | return false; |
| 196 | } |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 197 | auto slot = *metadata_slot; |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 198 | if (owned->metadata = ReadMetadata(*partition_opener, block_device, slot); |
| 199 | !owned->metadata) { |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 200 | LOG(ERROR) << "Could not read partition table for: " << block_device; |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 201 | return false; |
| 202 | } |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 203 | metadata = owned->metadata.get(); |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Find the partition by name if needed. |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 207 | if (!partition) { |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 208 | for (const auto& metadata_partition : metadata->partitions) { |
| 209 | if (android::fs_mgr::GetPartitionName(metadata_partition) == partition_name) { |
| 210 | partition = &metadata_partition; |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | } |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 214 | } |
| 215 | if (!partition) { |
| 216 | LERROR << "Could not find any partition with name: " << partition_name; |
| 217 | return false; |
| 218 | } |
| 219 | if (partition_name.empty()) { |
| 220 | partition_name = android::fs_mgr::GetPartitionName(*partition); |
| 221 | } else if (partition_name != android::fs_mgr::GetPartitionName(*partition)) { |
| 222 | LERROR << "Inconsistent partition_name " << partition_name << " with partition " |
| 223 | << android::fs_mgr::GetPartitionName(*partition); |
| 224 | return false; |
David Anderson | 908f07b | 2019-01-07 18:29:27 -0800 | [diff] [blame] | 225 | } |
David Anderson | 908f07b | 2019-01-07 18:29:27 -0800 | [diff] [blame] | 226 | |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 227 | if (device_name.empty()) { |
| 228 | device_name = partition_name; |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | bool CreateLogicalPartition(CreateLogicalPartitionParams params, std::string* path) { |
| 235 | CreateLogicalPartitionParams::OwnedData owned_data; |
| 236 | if (!params.InitDefaults(&owned_data)) return false; |
David Anderson | 4929df0 | 2019-08-15 13:21:43 -0700 | [diff] [blame] | 237 | |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 238 | DmTable table; |
Yifan Hong | c02509b | 2019-09-11 18:15:22 -0700 | [diff] [blame] | 239 | if (!CreateDmTableInternal(params, &table)) { |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 240 | return false; |
David Anderson | 23de22d | 2018-07-20 16:28:04 -0700 | [diff] [blame] | 241 | } |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 242 | |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 243 | DeviceMapper& dm = DeviceMapper::Instance(); |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 244 | if (!dm.CreateDevice(params.device_name, table, path, params.timeout_ms)) { |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
Yifan Hong | 1ada55e | 2019-08-16 15:31:12 -0700 | [diff] [blame] | 247 | LINFO << "Created logical partition " << params.device_name << " on device " << *path; |
David Anderson | 15aa954 | 2019-08-12 17:07:50 -0700 | [diff] [blame] | 248 | return true; |
David Anderson | 23de22d | 2018-07-20 16:28:04 -0700 | [diff] [blame] | 249 | } |
| 250 | |
David Anderson | 9267c3f | 2019-08-15 12:50:38 -0700 | [diff] [blame] | 251 | std::string CreateLogicalPartitionParams::GetDeviceName() const { |
| 252 | if (!device_name.empty()) return device_name; |
| 253 | return GetPartitionName(); |
| 254 | } |
| 255 | |
| 256 | std::string CreateLogicalPartitionParams::GetPartitionName() const { |
| 257 | if (!partition_name.empty()) return partition_name; |
| 258 | if (partition) return android::fs_mgr::GetPartitionName(*partition); |
| 259 | return "<unknown partition>"; |
| 260 | } |
| 261 | |
David Anderson | 470fe2b | 2019-07-10 18:09:50 -0700 | [diff] [blame] | 262 | bool UnmapDevice(const std::string& name) { |
David Anderson | 23de22d | 2018-07-20 16:28:04 -0700 | [diff] [blame] | 263 | DeviceMapper& dm = DeviceMapper::Instance(); |
David Anderson | b9f734c | 2018-08-03 10:12:16 -0700 | [diff] [blame] | 264 | if (!dm.DeleteDevice(name)) { |
| 265 | return false; |
| 266 | } |
Yifan Hong | 402633d | 2019-04-09 10:11:34 -0700 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | |
David Anderson | 470fe2b | 2019-07-10 18:09:50 -0700 | [diff] [blame] | 270 | bool DestroyLogicalPartition(const std::string& name) { |
| 271 | if (!UnmapDevice(name)) { |
Yifan Hong | 402633d | 2019-04-09 10:11:34 -0700 | [diff] [blame] | 272 | return false; |
| 273 | } |
David Anderson | b9f734c | 2018-08-03 10:12:16 -0700 | [diff] [blame] | 274 | LINFO << "Unmapped logical partition " << name; |
| 275 | return true; |
David Anderson | 23de22d | 2018-07-20 16:28:04 -0700 | [diff] [blame] | 276 | } |
| 277 | |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 278 | } // namespace fs_mgr |
| 279 | } // namespace android |