1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <chrono>
16 #include <string_view>
17 #include <vector>
18
19 #include <android-base/chrono_utils.h>
20 #include <android-base/logging.h>
21 #include <android-base/strings.h>
22 #include <fs_mgr.h>
23
24 #include "block_dev_initializer.h"
25
26 namespace android {
27 namespace init {
28
29 using android::base::Timer;
30 using namespace std::chrono_literals;
31
BlockDevInitializer()32 BlockDevInitializer::BlockDevInitializer() : uevent_listener_(16 * 1024 * 1024) {
33 auto boot_devices = android::fs_mgr::GetBootDevices();
34 device_handler_ = std::make_unique<DeviceHandler>(
35 std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, std::vector<Subsystem>{},
36 std::move(boot_devices), false);
37 }
38
InitDeviceMapper()39 bool BlockDevInitializer::InitDeviceMapper() {
40 return InitMiscDevice("device-mapper");
41 }
42
InitDmUser()43 bool BlockDevInitializer::InitDmUser() {
44 return InitMiscDevice("dm-user");
45 }
46
InitMiscDevice(const std::string & name)47 bool BlockDevInitializer::InitMiscDevice(const std::string& name) {
48 const std::string dm_path = "/devices/virtual/misc/" + name;
49 bool found = false;
50 auto dm_callback = [this, &dm_path, &found](const Uevent& uevent) {
51 if (uevent.path == dm_path) {
52 device_handler_->HandleUevent(uevent);
53 found = true;
54 return ListenerAction::kStop;
55 }
56 return ListenerAction::kContinue;
57 };
58 uevent_listener_.RegenerateUeventsForPath("/sys" + dm_path, dm_callback);
59 if (!found) {
60 LOG(INFO) << name << " device not found in /sys, waiting for its uevent";
61 Timer t;
62 uevent_listener_.Poll(dm_callback, 10s);
63 LOG(INFO) << "Wait for " << name << " returned after " << t;
64 }
65 if (!found) {
66 LOG(ERROR) << name << " device not found after polling timeout";
67 return false;
68 }
69 return true;
70 }
71
HandleUevent(const Uevent & uevent,std::set<std::string> * devices)72 ListenerAction BlockDevInitializer::HandleUevent(const Uevent& uevent,
73 std::set<std::string>* devices) {
74 // Ignore everything that is not a block device.
75 if (uevent.subsystem != "block") {
76 return ListenerAction::kContinue;
77 }
78
79 auto name = uevent.partition_name;
80 if (name.empty()) {
81 size_t base_idx = uevent.path.rfind('/');
82 if (base_idx == std::string::npos) {
83 return ListenerAction::kContinue;
84 }
85 name = uevent.path.substr(base_idx + 1);
86 }
87
88 auto iter = devices->find(name);
89 if (iter == devices->end()) {
90 return ListenerAction::kContinue;
91 }
92
93 LOG(VERBOSE) << __PRETTY_FUNCTION__ << ": found partition: " << name;
94
95 devices->erase(iter);
96 device_handler_->HandleUevent(uevent);
97 return devices->empty() ? ListenerAction::kStop : ListenerAction::kContinue;
98 }
99
InitDevices(std::set<std::string> devices)100 bool BlockDevInitializer::InitDevices(std::set<std::string> devices) {
101 auto uevent_callback = [&, this](const Uevent& uevent) -> ListenerAction {
102 return HandleUevent(uevent, &devices);
103 };
104 uevent_listener_.RegenerateUevents(uevent_callback);
105
106 // UeventCallback() will remove found partitions from |devices|. So if it
107 // isn't empty here, it means some partitions are not found.
108 if (!devices.empty()) {
109 LOG(INFO) << __PRETTY_FUNCTION__
110 << ": partition(s) not found in /sys, waiting for their uevent(s): "
111 << android::base::Join(devices, ", ");
112 Timer t;
113 uevent_listener_.Poll(uevent_callback, 10s);
114 LOG(INFO) << "Wait for partitions returned after " << t;
115 }
116
117 if (!devices.empty()) {
118 LOG(ERROR) << __PRETTY_FUNCTION__ << ": partition(s) not found after polling timeout: "
119 << android::base::Join(devices, ", ");
120 return false;
121 }
122 return true;
123 }
124
125 // Creates "/dev/block/dm-XX" for dm nodes by running coldboot on /sys/block/dm-XX.
InitDmDevice(const std::string & device)126 bool BlockDevInitializer::InitDmDevice(const std::string& device) {
127 const std::string device_name(basename(device.c_str()));
128 const std::string syspath = "/sys/block/" + device_name;
129 bool found = false;
130
131 auto uevent_callback = [&device_name, &device, this, &found](const Uevent& uevent) {
132 if (uevent.device_name == device_name) {
133 LOG(VERBOSE) << "Creating device-mapper device : " << device;
134 device_handler_->HandleUevent(uevent);
135 found = true;
136 return ListenerAction::kStop;
137 }
138 return ListenerAction::kContinue;
139 };
140
141 uevent_listener_.RegenerateUeventsForPath(syspath, uevent_callback);
142 if (!found) {
143 LOG(INFO) << "dm device '" << device << "' not found in /sys, waiting for its uevent";
144 Timer t;
145 uevent_listener_.Poll(uevent_callback, 10s);
146 LOG(INFO) << "wait for dm device '" << device << "' returned after " << t;
147 }
148 if (!found) {
149 LOG(ERROR) << "dm device '" << device << "' not found after polling timeout";
150 return false;
151 }
152 return true;
153 }
154
155 } // namespace init
156 } // namespace android
157