summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTreehugger Robot2018-06-21 14:01:49 -0500
committerGerrit Code Review2018-06-21 14:01:49 -0500
commit2e98fd23c37604ff753dcdd46dab62e7d708170a (patch)
tree6bbe8d7b5c23a71ae2dab19d80e4c825cc4e440f
parentbdc56b046e26d153c1775e73033c83498b69b741 (diff)
parente1c0744c2647fd48d1c57d9628a145d3f3284107 (diff)
downloadplatform-system-core-2e98fd23c37604ff753dcdd46dab62e7d708170a.tar.gz
platform-system-core-2e98fd23c37604ff753dcdd46dab62e7d708170a.tar.xz
platform-system-core-2e98fd23c37604ff753dcdd46dab62e7d708170a.zip
Merge "libdm: Implement LoadTableAndActivate."
-rw-r--r--fs_mgr/libdm/dm.cpp33
-rw-r--r--fs_mgr/libdm/dm_table.cpp4
-rw-r--r--fs_mgr/libdm/include/libdm/dm.h25
-rw-r--r--fs_mgr/libdm/include/libdm/dm_table.h9
-rw-r--r--fs_mgr/tools/dmctl.cpp5
5 files changed, 57 insertions, 19 deletions
diff --git a/fs_mgr/libdm/dm.cpp b/fs_mgr/libdm/dm.cpp
index 317ee1f61..5a2dfc67a 100644
--- a/fs_mgr/libdm/dm.cpp
+++ b/fs_mgr/libdm/dm.cpp
@@ -94,8 +94,37 @@ DmDeviceState DeviceMapper::state(const std::string& /* name */) const {
94 return DmDeviceState::INVALID; 94 return DmDeviceState::INVALID;
95} 95}
96 96
97bool DeviceMapper::LoadTableAndActivate(const std::string& /* name */, const DmTable& /* table */) { 97bool DeviceMapper::CreateDevice(const std::string& name, const DmTable& table) {
98 return false; 98 if (!CreateDevice(name)) {
99 return false;
100 }
101 if (!LoadTableAndActivate(name, table)) {
102 DeleteDevice(name);
103 return false;
104 }
105 return true;
106}
107
108bool DeviceMapper::LoadTableAndActivate(const std::string& name, const DmTable& table) {
109 std::string ioctl_buffer(sizeof(struct dm_ioctl), 0);
110 ioctl_buffer += table.Serialize();
111
112 struct dm_ioctl* io = reinterpret_cast<struct dm_ioctl*>(&ioctl_buffer[0]);
113 InitIo(io, name);
114 io->data_size = ioctl_buffer.size();
115 io->data_start = sizeof(struct dm_ioctl);
116 io->target_count = static_cast<uint32_t>(table.num_targets());
117 if (ioctl(fd_, DM_TABLE_LOAD, io)) {
118 PLOG(ERROR) << "DM_TABLE_LOAD failed";
119 return false;
120 }
121
122 InitIo(io, name);
123 if (ioctl(fd_, DM_DEV_SUSPEND, io)) {
124 PLOG(ERROR) << "DM_TABLE_SUSPEND resume failed";
125 return false;
126 }
127 return true;
99} 128}
100 129
101// Reads all the available device mapper targets and their corresponding 130// Reads all the available device mapper targets and their corresponding
diff --git a/fs_mgr/libdm/dm_table.cpp b/fs_mgr/libdm/dm_table.cpp
index 48cd332b1..868286e22 100644
--- a/fs_mgr/libdm/dm_table.cpp
+++ b/fs_mgr/libdm/dm_table.cpp
@@ -34,8 +34,8 @@ bool DmTable::valid() const {
34 return true; 34 return true;
35} 35}
36 36
37uint64_t DmTable::size() const { 37uint64_t DmTable::num_sectors() const {
38 return valid() ? size_ : 0; 38 return valid() ? num_sectors_ : 0;
39} 39}
40 40
41// Returns a string represnetation of the table that is ready to be passed 41// Returns a string represnetation of the table that is ready to be passed
diff --git a/fs_mgr/libdm/include/libdm/dm.h b/fs_mgr/libdm/include/libdm/dm.h
index c0b7fe9de..840777499 100644
--- a/fs_mgr/libdm/include/libdm/dm.h
+++ b/fs_mgr/libdm/include/libdm/dm.h
@@ -69,14 +69,6 @@ class DeviceMapper final {
69 uint64_t dev_; 69 uint64_t dev_;
70 }; 70 };
71 71
72 // Creates a device mapper device with given name.
73 // Return 'true' on success and 'false' on failure to
74 // create OR if a device mapper device with the same name already
75 // exists.
76 // TODO(b/110035986): Make this method private and to be only
77 // called through LoadTableAndActivate() below.
78 bool CreateDevice(const std::string& name);
79
80 // Removes a device mapper device with the given name. 72 // Removes a device mapper device with the given name.
81 // Returns 'true' on success, false otherwise. 73 // Returns 'true' on success, false otherwise.
82 bool DeleteDevice(const std::string& name); 74 bool DeleteDevice(const std::string& name);
@@ -90,9 +82,14 @@ class DeviceMapper final {
90 // One of INVALID, SUSPENDED or ACTIVE. 82 // One of INVALID, SUSPENDED or ACTIVE.
91 DmDeviceState state(const std::string& name) const; 83 DmDeviceState state(const std::string& name) const;
92 84
93 // Loads the device mapper table from parameter into the underlying 85 // Creates a device, loads the given table, and activates it. If the device
94 // device mapper device with given name and activate / resumes the device in the process. 86 // is not able to be activated, it is destroyed, and false is returned.
95 // If a device mapper device with the 'name', doesn't exist, it will be created. 87 bool CreateDevice(const std::string& name, const DmTable& table);
88
89 // Loads the device mapper table from parameter into the underlying device
90 // mapper device with given name and activate / resumes the device in the
91 // process. A device with the given name must already exist.
92 //
96 // Returns 'true' on success, false otherwise. 93 // Returns 'true' on success, false otherwise.
97 bool LoadTableAndActivate(const std::string& name, const DmTable& table); 94 bool LoadTableAndActivate(const std::string& name, const DmTable& table);
98 95
@@ -140,6 +137,12 @@ class DeviceMapper final {
140 } 137 }
141 } 138 }
142 139
140 // Creates a device mapper device with given name.
141 // Return 'true' on success and 'false' on failure to
142 // create OR if a device mapper device with the same name already
143 // exists.
144 bool CreateDevice(const std::string& name);
145
143 int fd_; 146 int fd_;
144 // Non-copyable & Non-movable 147 // Non-copyable & Non-movable
145 DeviceMapper(const DeviceMapper&) = delete; 148 DeviceMapper(const DeviceMapper&) = delete;
diff --git a/fs_mgr/libdm/include/libdm/dm_table.h b/fs_mgr/libdm/include/libdm/dm_table.h
index 0b1685db0..8a5c62400 100644
--- a/fs_mgr/libdm/include/libdm/dm_table.h
+++ b/fs_mgr/libdm/include/libdm/dm_table.h
@@ -30,7 +30,7 @@ namespace dm {
30 30
31class DmTable { 31class DmTable {
32 public: 32 public:
33 DmTable() : size_(0){}; 33 DmTable() : num_sectors_(0){};
34 34
35 // Adds a target to the device mapper table for a range specified in the target object. 35 // Adds a target to the device mapper table for a range specified in the target object.
36 // The function will return 'true' if the target was successfully added and doesn't overlap with 36 // The function will return 'true' if the target was successfully added and doesn't overlap with
@@ -48,9 +48,12 @@ class DmTable {
48 // table is malformed. 48 // table is malformed.
49 bool valid() const; 49 bool valid() const;
50 50
51 // Returns the toatl number of targets.
52 size_t num_targets() const { return targets_.size(); }
53
51 // Returns the total size represented by the table in terms of number of 512-byte sectors. 54 // Returns the total size represented by the table in terms of number of 512-byte sectors.
52 // NOTE: This function will overlook if there are any gaps in the targets added in the table. 55 // NOTE: This function will overlook if there are any gaps in the targets added in the table.
53 uint64_t size() const; 56 uint64_t num_sectors() const;
54 57
55 // Returns the string represntation of the table that is ready to be passed into the kernel 58 // Returns the string represntation of the table that is ready to be passed into the kernel
56 // as part of the DM_TABLE_LOAD ioctl. 59 // as part of the DM_TABLE_LOAD ioctl.
@@ -66,7 +69,7 @@ class DmTable {
66 69
67 // Total size in terms of # of sectors, as calculated by looking at the last and the first 70 // Total size in terms of # of sectors, as calculated by looking at the last and the first
68 // target in 'target_'. 71 // target in 'target_'.
69 uint64_t size_; 72 uint64_t num_sectors_;
70}; 73};
71 74
72} // namespace dm 75} // namespace dm
diff --git a/fs_mgr/tools/dmctl.cpp b/fs_mgr/tools/dmctl.cpp
index 8b2ca3fa0..d637dc67c 100644
--- a/fs_mgr/tools/dmctl.cpp
+++ b/fs_mgr/tools/dmctl.cpp
@@ -34,6 +34,7 @@
34#include <vector> 34#include <vector>
35 35
36using DeviceMapper = ::android::dm::DeviceMapper; 36using DeviceMapper = ::android::dm::DeviceMapper;
37using DmTable = ::android::dm::DmTable;
37using DmTarget = ::android::dm::DmTarget; 38using DmTarget = ::android::dm::DmTarget;
38using DmBlockDevice = ::android::dm::DeviceMapper::DmBlockDevice; 39using DmBlockDevice = ::android::dm::DeviceMapper::DmBlockDevice;
39 40
@@ -53,9 +54,11 @@ static int DmCreateCmdHandler(int argc, char** argv) {
53 return -EINVAL; 54 return -EINVAL;
54 } 55 }
55 56
57 DmTable table;
58
56 std::string name = argv[0]; 59 std::string name = argv[0];
57 DeviceMapper& dm = DeviceMapper::Instance(); 60 DeviceMapper& dm = DeviceMapper::Instance();
58 if (!dm.CreateDevice(name)) { 61 if (!dm.CreateDevice(name, table)) {
59 std::cerr << "Failed to create device-mapper device with name: " << name << std::endl; 62 std::cerr << "Failed to create device-mapper device with name: " << name << std::endl;
60 return -EIO; 63 return -EIO;
61 } 64 }