1 /* 2 * Copyright 2018 Google, Inc 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 17 #ifndef _LIBDM_DMTABLE_H_ 18 #define _LIBDM_DMTABLE_H_ 19 20 #include <stdint.h> 21 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include "dm_target.h" 27 28 namespace android { 29 namespace dm { 30 31 class DmTable { 32 public: DmTable()33 DmTable() : num_sectors_(0), readonly_(false) {} 34 DmTable(DmTable&& other) = default; 35 36 // Adds a target to the device mapper table for a range specified in the target object. 37 // The function will return 'true' if the target was successfully added and doesn't overlap with 38 // any of the existing targets in the table. Gaps are allowed. The final check, including 39 // overlaps and gaps are done before loading the table. Returns 'false' on failure. 40 bool AddTarget(std::unique_ptr<DmTarget>&& target); 41 42 // Removes a target from the table for the range specified in the target object. Returns 'false' 43 // if the target name doesn't match with the one in the table. Returns 'true' if target is 44 // successfully removed. 45 bool RemoveTarget(std::unique_ptr<DmTarget>&& target); 46 47 // Adds a target, constructing it in-place for convenience. For example, 48 // 49 // table.Emplace<DmTargetZero>(0, num_sectors); 50 template <typename T, typename... Args> Emplace(Args &&...args)51 bool Emplace(Args&&... args) { 52 return AddTarget(std::make_unique<T>(std::forward<Args>(args)...)); 53 } 54 55 // Checks the table to make sure it is valid. i.e. Checks for range overlaps, range gaps 56 // and returns 'true' if the table is ready to be loaded into kernel. Returns 'false' if the 57 // table is malformed. 58 bool valid() const; 59 60 // Returns the total number of targets. num_targets()61 size_t num_targets() const { return targets_.size(); } 62 63 // Returns the total size represented by the table in terms of number of 512-byte sectors. 64 // NOTE: This function will overlook if there are any gaps in the targets added in the table. 65 uint64_t num_sectors() const; 66 67 // Returns the string represntation of the table that is ready to be passed into the kernel 68 // as part of the DM_TABLE_LOAD ioctl. 69 std::string Serialize() const; 70 set_readonly(bool readonly)71 void set_readonly(bool readonly) { readonly_ = readonly; } readonly()72 bool readonly() const { return readonly_; } 73 74 DmTable& operator=(DmTable&& other) = default; 75 76 ~DmTable() = default; 77 78 private: 79 // list of targets defined in this table sorted by 80 // their start and end sectors. 81 // Note: Overlapping targets MUST never be added in this list. 82 std::vector<std::unique_ptr<DmTarget>> targets_; 83 84 // Total size in terms of # of sectors, as calculated by looking at the last and the first 85 // target in 'target_'. 86 uint64_t num_sectors_; 87 88 // True if the device should be read-only; false otherwise. 89 bool readonly_; 90 }; 91 92 } // namespace dm 93 } // namespace android 94 95 #endif /* _LIBDM_DMTABLE_H_ */ 96