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 #ifndef __CORE_FS_MGR_DM_LINEAR_H 26 #define __CORE_FS_MGR_DM_LINEAR_H 27 28 #include <stdint.h> 29 30 #include <chrono> 31 #include <memory> 32 #include <optional> 33 #include <string> 34 #include <vector> 35 36 #include <libdm/dm.h> 37 #include <liblp/liblp.h> 38 39 namespace android { 40 namespace fs_mgr { 41 42 // Read metadata from the current slot. 43 std::unique_ptr<LpMetadata> ReadCurrentMetadata(const std::string& block_device); 44 45 // Create block devices for all logical partitions in the given metadata. The 46 // metadata must have been read from the current slot. 47 bool CreateLogicalPartitions(const LpMetadata& metadata, const std::string& block_device); 48 49 // Create block devices for all logical partitions. This is a convenience 50 // method for ReadMetadata and CreateLogicalPartitions. 51 bool CreateLogicalPartitions(const std::string& block_device); 52 53 struct CreateLogicalPartitionParams { 54 // Block device of the super partition. 55 std::string block_device; 56 57 // If |metadata| is null, the slot will be read using |metadata_slot|. 58 const LpMetadata* metadata = nullptr; 59 std::optional<uint32_t> metadata_slot; 60 61 // If |partition| is not set, it will be found via |partition_name|. 62 const LpMetadataPartition* partition = nullptr; 63 std::string partition_name; 64 65 // Force the device to be read-write even if it was specified as readonly 66 // in the metadata. 67 bool force_writable = false; 68 69 // If |timeout_ms| is non-zero, then CreateLogicalPartition will block for 70 // the given amount of time until the path returned in |path| is available. 71 std::chrono::milliseconds timeout_ms = {}; 72 73 // If this is non-empty, it will override the device mapper name (by 74 // default the partition name will be used). 75 std::string device_name; 76 77 // If non-null, this will use the specified IPartitionOpener rather than 78 // the default one. 79 const IPartitionOpener* partition_opener = nullptr; 80 81 // Helpers for determining the effective partition and device name. 82 std::string GetPartitionName() const; 83 std::string GetDeviceName() const; 84 85 // Specify ownership of fields. The ownership of these fields are managed 86 // by the caller of InitDefaults(). 87 // These are not declared in CreateLogicalPartitionParams so that the 88 // copy constructor is not deleted. 89 struct OwnedData { 90 std::unique_ptr<LpMetadata> metadata; 91 std::unique_ptr<IPartitionOpener> partition_opener; 92 }; 93 94 // Fill in default values for |params| that CreateLogicalPartition assumes. Caller does 95 // not need to call this before calling CreateLogicalPartition; CreateLogicalPartition sets 96 // values when they are missing. 97 // Caller is responsible for destroying owned_data when |this| is not used. 98 bool InitDefaults(OwnedData* owned); 99 }; 100 101 bool CreateLogicalPartition(CreateLogicalPartitionParams params, std::string* path); 102 103 // Destroy the block device for a logical partition, by name. If |timeout_ms| 104 // is non-zero, then this will block until the device path has been unlinked. 105 bool DestroyLogicalPartition(const std::string& name); 106 107 // Helper for populating a DmTable for a logical partition. 108 bool CreateDmTable(CreateLogicalPartitionParams params, android::dm::DmTable* table); 109 110 } // namespace fs_mgr 111 } // namespace android 112 113 #endif // __CORE_FS_MGR_DM_LINEAR_H 114