Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 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 | #define LOG_TAG "IncrementalService" |
| 18 | |
| 19 | #include "IncrementalService.h" |
| 20 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Yurii Zubrytskyi | 0cd8012 | 2020-04-09 23:08:31 -0700 | [diff] [blame] | 22 | #include <android-base/no_destructor.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 23 | #include <android-base/properties.h> |
| 24 | #include <android-base/stringprintf.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 25 | #include <binder/AppOpsManager.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 26 | #include <binder/Status.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <uuid/uuid.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 29 | |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 30 | #include <charconv> |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 31 | #include <ctime> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 32 | #include <iterator> |
| 33 | #include <span> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 34 | #include <type_traits> |
| 35 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 36 | #include "IncrementalServiceValidation.h" |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 37 | #include "Metadata.pb.h" |
| 38 | |
| 39 | using namespace std::literals; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 40 | |
Alex Buynytskyy | 42d4ba4 | 2021-01-12 11:10:03 -0800 | [diff] [blame] | 41 | constexpr const char* kLoaderUsageStats = "android.permission.LOADER_USAGE_STATS"; |
Alex Buynytskyy | 119de1f | 2020-04-08 16:15:35 -0700 | [diff] [blame] | 42 | constexpr const char* kOpUsage = "android:loader_usage_stats"; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 43 | |
Alex Buynytskyy | 42d4ba4 | 2021-01-12 11:10:03 -0800 | [diff] [blame] | 44 | constexpr const char* kInteractAcrossUsers = "android.permission.INTERACT_ACROSS_USERS"; |
| 45 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 46 | namespace android::incremental { |
| 47 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 48 | using content::pm::DataLoaderParamsParcel; |
| 49 | using content::pm::FileSystemControlParcel; |
| 50 | using content::pm::IDataLoader; |
| 51 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 52 | namespace { |
| 53 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 54 | using IncrementalFileSystemControlParcel = os::incremental::IncrementalFileSystemControlParcel; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 55 | |
| 56 | struct Constants { |
| 57 | static constexpr auto backing = "backing_store"sv; |
| 58 | static constexpr auto mount = "mount"sv; |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 59 | static constexpr auto mountKeyPrefix = "MT_"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 60 | static constexpr auto storagePrefix = "st"sv; |
| 61 | static constexpr auto mountpointMdPrefix = ".mountpoint."sv; |
| 62 | static constexpr auto infoMdName = ".info"sv; |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 63 | static constexpr auto readLogsDisabledMarkerName = ".readlogs_disabled"sv; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 64 | static constexpr auto libDir = "lib"sv; |
| 65 | static constexpr auto libSuffix = ".so"sv; |
| 66 | static constexpr auto blockSize = 4096; |
Alex Buynytskyy | ea96c1f | 2020-05-18 10:06:01 -0700 | [diff] [blame] | 67 | static constexpr auto systemPackage = "android"sv; |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 68 | |
Alex Buynytskyy | 060c9d6 | 2021-02-18 20:55:17 -0800 | [diff] [blame] | 69 | static constexpr auto userStatusDelay = 100ms; |
| 70 | |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 71 | static constexpr auto progressUpdateInterval = 1000ms; |
| 72 | static constexpr auto perUidTimeoutOffset = progressUpdateInterval * 2; |
| 73 | static constexpr auto minPerUidTimeout = progressUpdateInterval * 3; |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 74 | |
| 75 | // If DL was up and not crashing for 10mins, we consider it healthy and reset all delays. |
| 76 | static constexpr auto healthyDataLoaderUptime = 10min; |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 77 | |
| 78 | // For healthy DLs, we'll retry every ~5secs for ~10min |
| 79 | static constexpr auto bindRetryInterval = 5s; |
| 80 | static constexpr auto bindGracePeriod = 10min; |
| 81 | |
| 82 | static constexpr auto bindingTimeout = 1min; |
| 83 | |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 84 | // 10s, 100s (~2min), 1000s (~15min), 10000s (~3hrs) |
| 85 | static constexpr auto minBindDelay = 10s; |
| 86 | static constexpr auto maxBindDelay = 10000s; |
| 87 | static constexpr auto bindDelayMultiplier = 10; |
| 88 | static constexpr auto bindDelayJitterDivider = 10; |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 89 | |
| 90 | // Max interval after system invoked the DL when readlog collection can be enabled. |
| 91 | static constexpr auto readLogsMaxInterval = 2h; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static const Constants& constants() { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 95 | static constexpr Constants c; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 96 | return c; |
| 97 | } |
| 98 | |
| 99 | template <base::LogSeverity level = base::ERROR> |
| 100 | bool mkdirOrLog(std::string_view name, int mode = 0770, bool allowExisting = true) { |
| 101 | auto cstr = path::c_str(name); |
| 102 | if (::mkdir(cstr, mode)) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 103 | if (!allowExisting || errno != EEXIST) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 104 | PLOG(level) << "Can't create directory '" << name << '\''; |
| 105 | return false; |
| 106 | } |
| 107 | struct stat st; |
| 108 | if (::stat(cstr, &st) || !S_ISDIR(st.st_mode)) { |
| 109 | PLOG(level) << "Path exists but is not a directory: '" << name << '\''; |
| 110 | return false; |
| 111 | } |
| 112 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 113 | if (::chmod(cstr, mode)) { |
| 114 | PLOG(level) << "Changing permission failed for '" << name << '\''; |
| 115 | return false; |
| 116 | } |
| 117 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | static std::string toMountKey(std::string_view path) { |
| 122 | if (path.empty()) { |
| 123 | return "@none"; |
| 124 | } |
| 125 | if (path == "/"sv) { |
| 126 | return "@root"; |
| 127 | } |
| 128 | if (path::isAbsolute(path)) { |
| 129 | path.remove_prefix(1); |
| 130 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 131 | if (path.size() > 16) { |
| 132 | path = path.substr(0, 16); |
| 133 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 134 | std::string res(path); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 135 | std::replace_if( |
| 136 | res.begin(), res.end(), [](char c) { return c == '/' || c == '@'; }, '_'); |
| 137 | return std::string(constants().mountKeyPrefix) += res; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static std::pair<std::string, std::string> makeMountDir(std::string_view incrementalDir, |
| 141 | std::string_view path) { |
| 142 | auto mountKey = toMountKey(path); |
| 143 | const auto prefixSize = mountKey.size(); |
| 144 | for (int counter = 0; counter < 1000; |
| 145 | mountKey.resize(prefixSize), base::StringAppendF(&mountKey, "%d", counter++)) { |
| 146 | auto mountRoot = path::join(incrementalDir, mountKey); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 147 | if (mkdirOrLog(mountRoot, 0777, false)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 148 | return {mountKey, mountRoot}; |
| 149 | } |
| 150 | } |
| 151 | return {}; |
| 152 | } |
| 153 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 154 | template <class Map> |
| 155 | typename Map::const_iterator findParentPath(const Map& map, std::string_view path) { |
| 156 | const auto nextIt = map.upper_bound(path); |
| 157 | if (nextIt == map.begin()) { |
| 158 | return map.end(); |
| 159 | } |
| 160 | const auto suspectIt = std::prev(nextIt); |
| 161 | if (!path::startsWith(path, suspectIt->first)) { |
| 162 | return map.end(); |
| 163 | } |
| 164 | return suspectIt; |
| 165 | } |
| 166 | |
| 167 | static base::unique_fd dup(base::borrowed_fd fd) { |
| 168 | const auto res = fcntl(fd.get(), F_DUPFD_CLOEXEC, 0); |
| 169 | return base::unique_fd(res); |
| 170 | } |
| 171 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 172 | template <class ProtoMessage, class Control> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 173 | static ProtoMessage parseFromIncfs(const IncFsWrapper* incfs, const Control& control, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 174 | std::string_view path) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 175 | auto md = incfs->getMetadata(control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 176 | ProtoMessage message; |
| 177 | return message.ParseFromArray(md.data(), md.size()) ? message : ProtoMessage{}; |
| 178 | } |
| 179 | |
| 180 | static bool isValidMountTarget(std::string_view path) { |
| 181 | return path::isAbsolute(path) && path::isEmptyDir(path).value_or(true); |
| 182 | } |
| 183 | |
| 184 | std::string makeBindMdName() { |
| 185 | static constexpr auto uuidStringSize = 36; |
| 186 | |
| 187 | uuid_t guid; |
| 188 | uuid_generate(guid); |
| 189 | |
| 190 | std::string name; |
| 191 | const auto prefixSize = constants().mountpointMdPrefix.size(); |
| 192 | name.reserve(prefixSize + uuidStringSize); |
| 193 | |
| 194 | name = constants().mountpointMdPrefix; |
| 195 | name.resize(prefixSize + uuidStringSize); |
| 196 | uuid_unparse(guid, name.data() + prefixSize); |
| 197 | |
| 198 | return name; |
| 199 | } |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 200 | |
| 201 | static bool checkReadLogsDisabledMarker(std::string_view root) { |
| 202 | const auto markerPath = path::c_str(path::join(root, constants().readLogsDisabledMarkerName)); |
| 203 | struct stat st; |
| 204 | return (::stat(markerPath, &st) == 0); |
| 205 | } |
| 206 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 207 | } // namespace |
| 208 | |
| 209 | IncrementalService::IncFsMount::~IncFsMount() { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 210 | if (dataLoaderStub) { |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 211 | dataLoaderStub->cleanupResources(); |
| 212 | dataLoaderStub = {}; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 213 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 214 | control.close(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 215 | LOG(INFO) << "Unmounting and cleaning up mount " << mountId << " with root '" << root << '\''; |
| 216 | for (auto&& [target, _] : bindPoints) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 217 | LOG(INFO) << " bind: " << target; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 218 | incrementalService.mVold->unmountIncFs(target); |
| 219 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 220 | LOG(INFO) << " root: " << root; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 221 | incrementalService.mVold->unmountIncFs(path::join(root, constants().mount)); |
| 222 | cleanupFilesystem(root); |
| 223 | } |
| 224 | |
| 225 | auto IncrementalService::IncFsMount::makeStorage(StorageId id) -> StorageMap::iterator { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 226 | std::string name; |
| 227 | for (int no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), i = 0; |
| 228 | i < 1024 && no >= 0; no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), ++i) { |
| 229 | name.clear(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 230 | base::StringAppendF(&name, "%.*s_%d_%d", int(constants().storagePrefix.size()), |
| 231 | constants().storagePrefix.data(), id, no); |
| 232 | auto fullName = path::join(root, constants().mount, name); |
Songchun Fan | 9610093 | 2020-02-03 19:20:58 -0800 | [diff] [blame] | 233 | if (auto err = incrementalService.mIncFs->makeDir(control, fullName, 0755); !err) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 234 | std::lock_guard l(lock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 235 | return storages.insert_or_assign(id, Storage{std::move(fullName)}).first; |
| 236 | } else if (err != EEXIST) { |
| 237 | LOG(ERROR) << __func__ << "(): failed to create dir |" << fullName << "| " << err; |
| 238 | break; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | nextStorageDirNo = 0; |
| 242 | return storages.end(); |
| 243 | } |
| 244 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 245 | template <class Func> |
| 246 | static auto makeCleanup(Func&& f) { |
| 247 | auto deleter = [f = std::move(f)](auto) { f(); }; |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 248 | // &f is a dangling pointer here, but we actually never use it as deleter moves it in. |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 249 | return std::unique_ptr<Func, decltype(deleter)>(&f, std::move(deleter)); |
| 250 | } |
| 251 | |
| 252 | static std::unique_ptr<DIR, decltype(&::closedir)> openDir(const char* dir) { |
| 253 | return {::opendir(dir), ::closedir}; |
| 254 | } |
| 255 | |
| 256 | static auto openDir(std::string_view dir) { |
| 257 | return openDir(path::c_str(dir)); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static int rmDirContent(const char* path) { |
| 261 | auto dir = openDir(path); |
| 262 | if (!dir) { |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | while (auto entry = ::readdir(dir.get())) { |
| 266 | if (entry->d_name == "."sv || entry->d_name == ".."sv) { |
| 267 | continue; |
| 268 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 269 | auto fullPath = base::StringPrintf("%s/%s", path, entry->d_name); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 270 | if (entry->d_type == DT_DIR) { |
| 271 | if (const auto err = rmDirContent(fullPath.c_str()); err != 0) { |
| 272 | PLOG(WARNING) << "Failed to delete " << fullPath << " content"; |
| 273 | return err; |
| 274 | } |
| 275 | if (const auto err = ::rmdir(fullPath.c_str()); err != 0) { |
| 276 | PLOG(WARNING) << "Failed to rmdir " << fullPath; |
| 277 | return err; |
| 278 | } |
| 279 | } else { |
| 280 | if (const auto err = ::unlink(fullPath.c_str()); err != 0) { |
| 281 | PLOG(WARNING) << "Failed to delete " << fullPath; |
| 282 | return err; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | return 0; |
| 287 | } |
| 288 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 289 | void IncrementalService::IncFsMount::cleanupFilesystem(std::string_view root) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 290 | rmDirContent(path::join(root, constants().backing).c_str()); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 291 | ::rmdir(path::join(root, constants().backing).c_str()); |
| 292 | ::rmdir(path::join(root, constants().mount).c_str()); |
| 293 | ::rmdir(path::c_str(root)); |
| 294 | } |
| 295 | |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 296 | void IncrementalService::IncFsMount::setReadLogsEnabled(bool value) { |
| 297 | if (value) { |
| 298 | flags |= StorageFlags::ReadLogsEnabled; |
| 299 | } else { |
| 300 | flags &= ~StorageFlags::ReadLogsEnabled; |
| 301 | } |
| 302 | } |
| 303 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 304 | IncrementalService::IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir) |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 305 | : mVold(sm.getVoldService()), |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 306 | mDataLoaderManager(sm.getDataLoaderManager()), |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 307 | mIncFs(sm.getIncFs()), |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 308 | mAppOpsManager(sm.getAppOpsManager()), |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 309 | mJni(sm.getJni()), |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 310 | mLooper(sm.getLooper()), |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 311 | mTimedQueue(sm.getTimedQueue()), |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 312 | mProgressUpdateJobQueue(sm.getProgressUpdateJobQueue()), |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 313 | mFs(sm.getFs()), |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 314 | mClock(sm.getClock()), |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 315 | mIncrementalDir(rootDir) { |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 316 | CHECK(mVold) << "Vold service is unavailable"; |
| 317 | CHECK(mDataLoaderManager) << "DataLoaderManagerService is unavailable"; |
| 318 | CHECK(mAppOpsManager) << "AppOpsManager is unavailable"; |
| 319 | CHECK(mJni) << "JNI is unavailable"; |
| 320 | CHECK(mLooper) << "Looper is unavailable"; |
| 321 | CHECK(mTimedQueue) << "TimedQueue is unavailable"; |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 322 | CHECK(mProgressUpdateJobQueue) << "mProgressUpdateJobQueue is unavailable"; |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 323 | CHECK(mFs) << "Fs is unavailable"; |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 324 | CHECK(mClock) << "Clock is unavailable"; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 325 | |
| 326 | mJobQueue.reserve(16); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 327 | mJobProcessor = std::thread([this]() { |
| 328 | mJni->initializeForCurrentThread(); |
| 329 | runJobProcessing(); |
| 330 | }); |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 331 | mCmdLooperThread = std::thread([this]() { |
| 332 | mJni->initializeForCurrentThread(); |
| 333 | runCmdLooper(); |
| 334 | }); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 335 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 336 | const auto mountedRootNames = adoptMountedInstances(); |
| 337 | mountExistingImages(mountedRootNames); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 340 | IncrementalService::~IncrementalService() { |
| 341 | { |
| 342 | std::lock_guard lock(mJobMutex); |
| 343 | mRunning = false; |
| 344 | } |
| 345 | mJobCondition.notify_all(); |
| 346 | mJobProcessor.join(); |
Alex Buynytskyy | b65a77f | 2020-09-22 11:39:53 -0700 | [diff] [blame] | 347 | mLooper->wake(); |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 348 | mCmdLooperThread.join(); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 349 | mTimedQueue->stop(); |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 350 | mProgressUpdateJobQueue->stop(); |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 351 | // Ensure that mounts are destroyed while the service is still valid. |
| 352 | mBindsByPath.clear(); |
| 353 | mMounts.clear(); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 354 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 355 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 356 | static const char* toString(IncrementalService::BindKind kind) { |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 357 | switch (kind) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 358 | case IncrementalService::BindKind::Temporary: |
| 359 | return "Temporary"; |
| 360 | case IncrementalService::BindKind::Permanent: |
| 361 | return "Permanent"; |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | void IncrementalService::onDump(int fd) { |
| 366 | dprintf(fd, "Incremental is %s\n", incfs::enabled() ? "ENABLED" : "DISABLED"); |
| 367 | dprintf(fd, "Incremental dir: %s\n", mIncrementalDir.c_str()); |
| 368 | |
| 369 | std::unique_lock l(mLock); |
| 370 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 371 | dprintf(fd, "Mounts (%d): {\n", int(mMounts.size())); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 372 | for (auto&& [id, ifs] : mMounts) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 373 | const IncFsMount& mnt = *ifs; |
| 374 | dprintf(fd, " [%d]: {\n", id); |
| 375 | if (id != mnt.mountId) { |
| 376 | dprintf(fd, " reference to mountId: %d\n", mnt.mountId); |
| 377 | } else { |
| 378 | dprintf(fd, " mountId: %d\n", mnt.mountId); |
| 379 | dprintf(fd, " root: %s\n", mnt.root.c_str()); |
| 380 | dprintf(fd, " nextStorageDirNo: %d\n", mnt.nextStorageDirNo.load()); |
| 381 | if (mnt.dataLoaderStub) { |
| 382 | mnt.dataLoaderStub->onDump(fd); |
| 383 | } else { |
| 384 | dprintf(fd, " dataLoader: null\n"); |
| 385 | } |
| 386 | dprintf(fd, " storages (%d): {\n", int(mnt.storages.size())); |
| 387 | for (auto&& [storageId, storage] : mnt.storages) { |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 388 | dprintf(fd, " [%d] -> [%s] (%d %% loaded) \n", storageId, storage.name.c_str(), |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 389 | (int)(getLoadingProgressFromPath(mnt, storage.name.c_str(), |
| 390 | /*stopOnFirstIncomplete=*/false) |
| 391 | .getProgress() * |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 392 | 100)); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 393 | } |
| 394 | dprintf(fd, " }\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 395 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 396 | dprintf(fd, " bindPoints (%d): {\n", int(mnt.bindPoints.size())); |
| 397 | for (auto&& [target, bind] : mnt.bindPoints) { |
| 398 | dprintf(fd, " [%s]->[%d]:\n", target.c_str(), bind.storage); |
| 399 | dprintf(fd, " savedFilename: %s\n", bind.savedFilename.c_str()); |
| 400 | dprintf(fd, " sourceDir: %s\n", bind.sourceDir.c_str()); |
| 401 | dprintf(fd, " kind: %s\n", toString(bind.kind)); |
| 402 | } |
| 403 | dprintf(fd, " }\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 404 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 405 | dprintf(fd, " }\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 406 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 407 | dprintf(fd, "}\n"); |
| 408 | dprintf(fd, "Sorted binds (%d): {\n", int(mBindsByPath.size())); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 409 | for (auto&& [target, mountPairIt] : mBindsByPath) { |
| 410 | const auto& bind = mountPairIt->second; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 411 | dprintf(fd, " [%s]->[%d]:\n", target.c_str(), bind.storage); |
| 412 | dprintf(fd, " savedFilename: %s\n", bind.savedFilename.c_str()); |
| 413 | dprintf(fd, " sourceDir: %s\n", bind.sourceDir.c_str()); |
| 414 | dprintf(fd, " kind: %s\n", toString(bind.kind)); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 415 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 416 | dprintf(fd, "}\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 419 | bool IncrementalService::needStartDataLoaderLocked(IncFsMount& ifs) { |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 420 | if (ifs.dataLoaderStub->isSystemDataLoader()) { |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | |
| 424 | // Check all permanent binds. |
| 425 | for (auto&& [_, bindPoint] : ifs.bindPoints) { |
| 426 | if (bindPoint.kind != BindKind::Permanent) { |
| 427 | continue; |
| 428 | } |
| 429 | const auto progress = getLoadingProgressFromPath(ifs, bindPoint.sourceDir, |
| 430 | /*stopOnFirstIncomplete=*/true); |
| 431 | if (!progress.isError() && !progress.fullyLoaded()) { |
| 432 | LOG(INFO) << "Non system mount: [" << bindPoint.sourceDir |
| 433 | << "], partial progress: " << progress.getProgress() * 100 << "%"; |
| 434 | return true; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return false; |
| 439 | } |
| 440 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 441 | void IncrementalService::onSystemReady() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 442 | if (mSystemReady.exchange(true)) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 443 | return; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | std::vector<IfsMountPtr> mounts; |
| 447 | { |
| 448 | std::lock_guard l(mLock); |
| 449 | mounts.reserve(mMounts.size()); |
| 450 | for (auto&& [id, ifs] : mMounts) { |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 451 | if (ifs->mountId != id) { |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | if (needStartDataLoaderLocked(*ifs)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 456 | mounts.push_back(ifs); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame] | 461 | if (mounts.empty()) { |
| 462 | return; |
| 463 | } |
| 464 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 465 | std::thread([this, mounts = std::move(mounts)]() { |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame] | 466 | mJni->initializeForCurrentThread(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 467 | for (auto&& ifs : mounts) { |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 468 | ifs->dataLoaderStub->requestStart(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 469 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 470 | }).detach(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator { |
| 474 | for (;;) { |
| 475 | if (mNextId == kMaxStorageId) { |
| 476 | mNextId = 0; |
| 477 | } |
| 478 | auto id = ++mNextId; |
| 479 | auto [it, inserted] = mMounts.try_emplace(id, nullptr); |
| 480 | if (inserted) { |
| 481 | return it; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 486 | StorageId IncrementalService::createStorage( |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 487 | std::string_view mountPoint, const content::pm::DataLoaderParamsParcel& dataLoaderParams, |
| 488 | CreateOptions options) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 489 | LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options); |
| 490 | if (!path::isAbsolute(mountPoint)) { |
| 491 | LOG(ERROR) << "path is not absolute: " << mountPoint; |
| 492 | return kInvalidStorageId; |
| 493 | } |
| 494 | |
| 495 | auto mountNorm = path::normalize(mountPoint); |
| 496 | { |
| 497 | const auto id = findStorageId(mountNorm); |
| 498 | if (id != kInvalidStorageId) { |
| 499 | if (options & CreateOptions::OpenExisting) { |
| 500 | LOG(INFO) << "Opened existing storage " << id; |
| 501 | return id; |
| 502 | } |
| 503 | LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id; |
| 504 | return kInvalidStorageId; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (!(options & CreateOptions::CreateNew)) { |
| 509 | LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint; |
| 510 | return kInvalidStorageId; |
| 511 | } |
| 512 | |
| 513 | if (!path::isEmptyDir(mountNorm)) { |
| 514 | LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm; |
| 515 | return kInvalidStorageId; |
| 516 | } |
| 517 | auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm); |
| 518 | if (mountRoot.empty()) { |
| 519 | LOG(ERROR) << "Bad mount point"; |
| 520 | return kInvalidStorageId; |
| 521 | } |
| 522 | // Make sure the code removes all crap it may create while still failing. |
| 523 | auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); }; |
| 524 | auto firstCleanupOnFailure = |
| 525 | std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup); |
| 526 | |
| 527 | auto mountTarget = path::join(mountRoot, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 528 | const auto backing = path::join(mountRoot, constants().backing); |
| 529 | if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 530 | return kInvalidStorageId; |
| 531 | } |
| 532 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 533 | IncFsMount::Control control; |
| 534 | { |
| 535 | std::lock_guard l(mMountOperationLock); |
| 536 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 537 | |
| 538 | if (auto err = rmDirContent(backing.c_str())) { |
| 539 | LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err; |
| 540 | return kInvalidStorageId; |
| 541 | } |
| 542 | if (!mkdirOrLog(path::join(backing, ".index"), 0777)) { |
| 543 | return kInvalidStorageId; |
| 544 | } |
Paul Lawrence | 87a92e1 | 2020-11-20 13:15:56 -0800 | [diff] [blame] | 545 | if (!mkdirOrLog(path::join(backing, ".incomplete"), 0777)) { |
| 546 | return kInvalidStorageId; |
| 547 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 548 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 549 | if (!status.isOk()) { |
| 550 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 551 | return kInvalidStorageId; |
| 552 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 553 | if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 || |
| 554 | controlParcel.log.get() < 0) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 555 | LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel."; |
| 556 | return kInvalidStorageId; |
| 557 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 558 | int cmd = controlParcel.cmd.release().release(); |
| 559 | int pendingReads = controlParcel.pendingReads.release().release(); |
| 560 | int logs = controlParcel.log.release().release(); |
Yurii Zubrytskyi | 5f69292 | 2020-12-08 07:35:24 -0800 | [diff] [blame] | 561 | int blocksWritten = |
| 562 | controlParcel.blocksWritten ? controlParcel.blocksWritten->release().release() : -1; |
| 563 | control = mIncFs->createControl(cmd, pendingReads, logs, blocksWritten); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | std::unique_lock l(mLock); |
| 567 | const auto mountIt = getStorageSlotLocked(); |
| 568 | const auto mountId = mountIt->first; |
| 569 | l.unlock(); |
| 570 | |
| 571 | auto ifs = |
| 572 | std::make_shared<IncFsMount>(std::move(mountRoot), mountId, std::move(control), *this); |
| 573 | // Now it's the |ifs|'s responsibility to clean up after itself, and the only cleanup we need |
| 574 | // is the removal of the |ifs|. |
| 575 | firstCleanupOnFailure.release(); |
| 576 | |
| 577 | auto secondCleanup = [this, &l](auto itPtr) { |
| 578 | if (!l.owns_lock()) { |
| 579 | l.lock(); |
| 580 | } |
| 581 | mMounts.erase(*itPtr); |
| 582 | }; |
| 583 | auto secondCleanupOnFailure = |
| 584 | std::unique_ptr<decltype(mountIt), decltype(secondCleanup)>(&mountIt, secondCleanup); |
| 585 | |
| 586 | const auto storageIt = ifs->makeStorage(ifs->mountId); |
| 587 | if (storageIt == ifs->storages.end()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 588 | LOG(ERROR) << "Can't create a default storage directory"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 589 | return kInvalidStorageId; |
| 590 | } |
| 591 | |
| 592 | { |
| 593 | metadata::Mount m; |
| 594 | m.mutable_storage()->set_id(ifs->mountId); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 595 | m.mutable_loader()->set_type((int)dataLoaderParams.type); |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 596 | m.mutable_loader()->set_package_name(dataLoaderParams.packageName); |
| 597 | m.mutable_loader()->set_class_name(dataLoaderParams.className); |
| 598 | m.mutable_loader()->set_arguments(dataLoaderParams.arguments); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 599 | const auto metadata = m.SerializeAsString(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 600 | if (auto err = |
| 601 | mIncFs->makeFile(ifs->control, |
| 602 | path::join(ifs->root, constants().mount, |
| 603 | constants().infoMdName), |
| 604 | 0777, idFromMetadata(metadata), |
| 605 | {.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 606 | LOG(ERROR) << "Saving mount metadata failed: " << -err; |
| 607 | return kInvalidStorageId; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | const auto bk = |
| 612 | (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 613 | if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name, |
| 614 | std::string(storageIt->second.name), std::move(mountNorm), bk, l); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 615 | err < 0) { |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 616 | LOG(ERROR) << "Adding bind mount failed: " << -err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 617 | return kInvalidStorageId; |
| 618 | } |
| 619 | |
| 620 | // Done here as well, all data structures are in good state. |
| 621 | secondCleanupOnFailure.release(); |
| 622 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 623 | mountIt->second = std::move(ifs); |
| 624 | l.unlock(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 625 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 626 | LOG(INFO) << "created storage " << mountId; |
| 627 | return mountId; |
| 628 | } |
| 629 | |
| 630 | StorageId IncrementalService::createLinkedStorage(std::string_view mountPoint, |
| 631 | StorageId linkedStorage, |
| 632 | IncrementalService::CreateOptions options) { |
| 633 | if (!isValidMountTarget(mountPoint)) { |
| 634 | LOG(ERROR) << "Mount point is invalid or missing"; |
| 635 | return kInvalidStorageId; |
| 636 | } |
| 637 | |
| 638 | std::unique_lock l(mLock); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 639 | auto ifs = getIfsLocked(linkedStorage); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 640 | if (!ifs) { |
| 641 | LOG(ERROR) << "Ifs unavailable"; |
| 642 | return kInvalidStorageId; |
| 643 | } |
| 644 | |
| 645 | const auto mountIt = getStorageSlotLocked(); |
| 646 | const auto storageId = mountIt->first; |
| 647 | const auto storageIt = ifs->makeStorage(storageId); |
| 648 | if (storageIt == ifs->storages.end()) { |
| 649 | LOG(ERROR) << "Can't create a new storage"; |
| 650 | mMounts.erase(mountIt); |
| 651 | return kInvalidStorageId; |
| 652 | } |
| 653 | |
| 654 | l.unlock(); |
| 655 | |
| 656 | const auto bk = |
| 657 | (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 658 | if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name, |
| 659 | std::string(storageIt->second.name), path::normalize(mountPoint), |
| 660 | bk, l); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 661 | err < 0) { |
| 662 | LOG(ERROR) << "bindMount failed with error: " << err; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 663 | (void)mIncFs->unlink(ifs->control, storageIt->second.name); |
| 664 | ifs->storages.erase(storageIt); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 665 | return kInvalidStorageId; |
| 666 | } |
| 667 | |
| 668 | mountIt->second = ifs; |
| 669 | return storageId; |
| 670 | } |
| 671 | |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 672 | bool IncrementalService::startLoading(StorageId storageId, |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 673 | content::pm::DataLoaderParamsParcel&& dataLoaderParams, |
| 674 | const DataLoaderStatusListener& statusListener, |
| 675 | StorageHealthCheckParams&& healthCheckParams, |
| 676 | const StorageHealthListener& healthListener, |
| 677 | const std::vector<PerUidReadTimeouts>& perUidReadTimeouts) { |
| 678 | // Per Uid timeouts. |
| 679 | if (!perUidReadTimeouts.empty()) { |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 680 | setUidReadTimeouts(storageId, perUidReadTimeouts); |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | // Re-initialize DataLoader. |
| 684 | std::unique_lock l(mLock); |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 685 | const auto ifs = getIfsLocked(storageId); |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 686 | if (!ifs) { |
| 687 | return false; |
| 688 | } |
| 689 | if (ifs->dataLoaderStub) { |
| 690 | ifs->dataLoaderStub->cleanupResources(); |
| 691 | ifs->dataLoaderStub = {}; |
| 692 | } |
| 693 | l.unlock(); |
| 694 | |
| 695 | // DataLoader. |
| 696 | auto dataLoaderStub = prepareDataLoader(*ifs, std::move(dataLoaderParams), &statusListener, |
| 697 | std::move(healthCheckParams), &healthListener); |
| 698 | CHECK(dataLoaderStub); |
| 699 | |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 700 | if (dataLoaderStub->isSystemDataLoader()) { |
| 701 | // Readlogs from system dataloader (adb) can always be collected. |
| 702 | ifs->startLoadingTs = TimePoint::max(); |
| 703 | } else { |
| 704 | // Assign time when installation wants the DL to start streaming. |
| 705 | const auto startLoadingTs = mClock->now(); |
| 706 | ifs->startLoadingTs = startLoadingTs; |
| 707 | // Setup a callback to disable the readlogs after max interval. |
| 708 | addTimedJob(*mTimedQueue, storageId, Constants::readLogsMaxInterval, |
| 709 | [this, storageId, startLoadingTs]() { |
| 710 | const auto ifs = getIfs(storageId); |
| 711 | if (!ifs) { |
| 712 | LOG(WARNING) << "Can't disable the readlogs, invalid storageId: " |
| 713 | << storageId; |
| 714 | return; |
| 715 | } |
| 716 | if (ifs->startLoadingTs != startLoadingTs) { |
| 717 | LOG(INFO) << "Can't disable the readlogs, timestamp mismatch (new " |
| 718 | "installation?): " |
| 719 | << storageId; |
| 720 | return; |
| 721 | } |
| 722 | setStorageParams(*ifs, storageId, /*enableReadLogs=*/false); |
| 723 | }); |
| 724 | } |
| 725 | |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 726 | return dataLoaderStub->requestStart(); |
| 727 | } |
| 728 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 729 | IncrementalService::BindPathMap::const_iterator IncrementalService::findStorageLocked( |
| 730 | std::string_view path) const { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 731 | return findParentPath(mBindsByPath, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | StorageId IncrementalService::findStorageId(std::string_view path) const { |
| 735 | std::lock_guard l(mLock); |
| 736 | auto it = findStorageLocked(path); |
| 737 | if (it == mBindsByPath.end()) { |
| 738 | return kInvalidStorageId; |
| 739 | } |
| 740 | return it->second->second.storage; |
| 741 | } |
| 742 | |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 743 | void IncrementalService::disallowReadLogs(StorageId storageId) { |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 744 | std::unique_lock l(mLock); |
| 745 | const auto ifs = getIfsLocked(storageId); |
| 746 | if (!ifs) { |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 747 | LOG(ERROR) << "disallowReadLogs failed, invalid storageId: " << storageId; |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 748 | return; |
| 749 | } |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 750 | if (!ifs->readLogsAllowed()) { |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 751 | return; |
| 752 | } |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 753 | ifs->disallowReadLogs(); |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 754 | l.unlock(); |
| 755 | |
| 756 | const auto metadata = constants().readLogsDisabledMarkerName; |
| 757 | if (auto err = mIncFs->makeFile(ifs->control, |
| 758 | path::join(ifs->root, constants().mount, |
| 759 | constants().readLogsDisabledMarkerName), |
| 760 | 0777, idFromMetadata(metadata), {})) { |
| 761 | //{.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) { |
| 762 | LOG(ERROR) << "Failed to make marker file for storageId: " << storageId; |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | setStorageParams(storageId, /*enableReadLogs=*/false); |
| 767 | } |
| 768 | |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 769 | int IncrementalService::setStorageParams(StorageId storageId, bool enableReadLogs) { |
| 770 | const auto ifs = getIfs(storageId); |
| 771 | if (!ifs) { |
Alex Buynytskyy | 5f9e3a0 | 2020-04-07 21:13:41 -0700 | [diff] [blame] | 772 | LOG(ERROR) << "setStorageParams failed, invalid storageId: " << storageId; |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 773 | return -EINVAL; |
| 774 | } |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 775 | return setStorageParams(*ifs, storageId, enableReadLogs); |
| 776 | } |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 777 | |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 778 | int IncrementalService::setStorageParams(IncFsMount& ifs, StorageId storageId, |
| 779 | bool enableReadLogs) { |
| 780 | const auto& params = ifs.dataLoaderStub->params(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 781 | if (enableReadLogs) { |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 782 | if (!ifs.readLogsAllowed()) { |
| 783 | LOG(ERROR) << "setStorageParams failed, readlogs disallowed for storageId: " |
| 784 | << storageId; |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 785 | return -EPERM; |
| 786 | } |
| 787 | |
Alex Buynytskyy | 42d4ba4 | 2021-01-12 11:10:03 -0800 | [diff] [blame] | 788 | // Check loader usage stats permission and apop. |
| 789 | if (auto status = mAppOpsManager->checkPermission(kLoaderUsageStats, kOpUsage, |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 790 | params.packageName.c_str()); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 791 | !status.isOk()) { |
Alex Buynytskyy | 42d4ba4 | 2021-01-12 11:10:03 -0800 | [diff] [blame] | 792 | LOG(ERROR) << " Permission: " << kLoaderUsageStats |
| 793 | << " check failed: " << status.toString8(); |
| 794 | return fromBinderStatus(status); |
| 795 | } |
| 796 | |
| 797 | // Check multiuser permission. |
| 798 | if (auto status = mAppOpsManager->checkPermission(kInteractAcrossUsers, nullptr, |
| 799 | params.packageName.c_str()); |
| 800 | !status.isOk()) { |
| 801 | LOG(ERROR) << " Permission: " << kInteractAcrossUsers |
| 802 | << " check failed: " << status.toString8(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 803 | return fromBinderStatus(status); |
| 804 | } |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 805 | |
| 806 | // Check installation time. |
| 807 | const auto now = mClock->now(); |
| 808 | const auto startLoadingTs = ifs.startLoadingTs; |
| 809 | if (startLoadingTs <= now && now - startLoadingTs > Constants::readLogsMaxInterval) { |
| 810 | LOG(ERROR) << "setStorageParams failed, readlogs can't be enabled at this time, " |
| 811 | "storageId: " |
| 812 | << storageId; |
| 813 | return -EPERM; |
| 814 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 817 | if (auto status = applyStorageParams(ifs, enableReadLogs); !status.isOk()) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 818 | LOG(ERROR) << "applyStorageParams failed: " << status.toString8(); |
| 819 | return fromBinderStatus(status); |
| 820 | } |
| 821 | |
| 822 | if (enableReadLogs) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 823 | registerAppOpsCallback(params.packageName); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | binder::Status IncrementalService::applyStorageParams(IncFsMount& ifs, bool enableReadLogs) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 830 | os::incremental::IncrementalFileSystemControlParcel control; |
| 831 | control.cmd.reset(dup(ifs.control.cmd())); |
| 832 | control.pendingReads.reset(dup(ifs.control.pendingReads())); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 833 | auto logsFd = ifs.control.logs(); |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 834 | if (logsFd >= 0) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 835 | control.log.reset(dup(logsFd)); |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | std::lock_guard l(mMountOperationLock); |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 839 | const auto status = mVold->setIncFsMountOptions(control, enableReadLogs); |
| 840 | if (status.isOk()) { |
| 841 | // Store enabled state. |
| 842 | ifs.setReadLogsEnabled(enableReadLogs); |
| 843 | } |
| 844 | return status; |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 847 | void IncrementalService::deleteStorage(StorageId storageId) { |
| 848 | const auto ifs = getIfs(storageId); |
| 849 | if (!ifs) { |
| 850 | return; |
| 851 | } |
| 852 | deleteStorage(*ifs); |
| 853 | } |
| 854 | |
| 855 | void IncrementalService::deleteStorage(IncrementalService::IncFsMount& ifs) { |
| 856 | std::unique_lock l(ifs.lock); |
| 857 | deleteStorageLocked(ifs, std::move(l)); |
| 858 | } |
| 859 | |
| 860 | void IncrementalService::deleteStorageLocked(IncrementalService::IncFsMount& ifs, |
| 861 | std::unique_lock<std::mutex>&& ifsLock) { |
| 862 | const auto storages = std::move(ifs.storages); |
| 863 | // Don't move the bind points out: Ifs's dtor will use them to unmount everything. |
| 864 | const auto bindPoints = ifs.bindPoints; |
| 865 | ifsLock.unlock(); |
| 866 | |
| 867 | std::lock_guard l(mLock); |
| 868 | for (auto&& [id, _] : storages) { |
| 869 | if (id != ifs.mountId) { |
| 870 | mMounts.erase(id); |
| 871 | } |
| 872 | } |
| 873 | for (auto&& [path, _] : bindPoints) { |
| 874 | mBindsByPath.erase(path); |
| 875 | } |
| 876 | mMounts.erase(ifs.mountId); |
| 877 | } |
| 878 | |
| 879 | StorageId IncrementalService::openStorage(std::string_view pathInMount) { |
| 880 | if (!path::isAbsolute(pathInMount)) { |
| 881 | return kInvalidStorageId; |
| 882 | } |
| 883 | |
| 884 | return findStorageId(path::normalize(pathInMount)); |
| 885 | } |
| 886 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 887 | IncrementalService::IfsMountPtr IncrementalService::getIfs(StorageId storage) const { |
| 888 | std::lock_guard l(mLock); |
| 889 | return getIfsLocked(storage); |
| 890 | } |
| 891 | |
| 892 | const IncrementalService::IfsMountPtr& IncrementalService::getIfsLocked(StorageId storage) const { |
| 893 | auto it = mMounts.find(storage); |
| 894 | if (it == mMounts.end()) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 895 | static const base::NoDestructor<IfsMountPtr> kEmpty{}; |
Yurii Zubrytskyi | 0cd8012 | 2020-04-09 23:08:31 -0700 | [diff] [blame] | 896 | return *kEmpty; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 897 | } |
| 898 | return it->second; |
| 899 | } |
| 900 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 901 | int IncrementalService::bind(StorageId storage, std::string_view source, std::string_view target, |
| 902 | BindKind kind) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 903 | if (!isValidMountTarget(target)) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 904 | LOG(ERROR) << __func__ << ": not a valid bind target " << target; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 905 | return -EINVAL; |
| 906 | } |
| 907 | |
| 908 | const auto ifs = getIfs(storage); |
| 909 | if (!ifs) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 910 | LOG(ERROR) << __func__ << ": no ifs object for storage " << storage; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 911 | return -EINVAL; |
| 912 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 913 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 914 | std::unique_lock l(ifs->lock); |
| 915 | const auto storageInfo = ifs->storages.find(storage); |
| 916 | if (storageInfo == ifs->storages.end()) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 917 | LOG(ERROR) << "no storage"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 918 | return -EINVAL; |
| 919 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 920 | std::string normSource = normalizePathToStorageLocked(*ifs, storageInfo, source); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 921 | if (normSource.empty()) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 922 | LOG(ERROR) << "invalid source path"; |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 923 | return -EINVAL; |
| 924 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 925 | l.unlock(); |
| 926 | std::unique_lock l2(mLock, std::defer_lock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 927 | return addBindMount(*ifs, storage, storageInfo->second.name, std::move(normSource), |
| 928 | path::normalize(target), kind, l2); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | int IncrementalService::unbind(StorageId storage, std::string_view target) { |
| 932 | if (!path::isAbsolute(target)) { |
| 933 | return -EINVAL; |
| 934 | } |
| 935 | |
Alex Buynytskyy | 4dbc060 | 2020-05-12 11:24:14 -0700 | [diff] [blame] | 936 | LOG(INFO) << "Removing bind point " << target << " for storage " << storage; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 937 | |
| 938 | // Here we should only look up by the exact target, not by a subdirectory of any existing mount, |
| 939 | // otherwise there's a chance to unmount something completely unrelated |
| 940 | const auto norm = path::normalize(target); |
| 941 | std::unique_lock l(mLock); |
| 942 | const auto storageIt = mBindsByPath.find(norm); |
| 943 | if (storageIt == mBindsByPath.end() || storageIt->second->second.storage != storage) { |
| 944 | return -EINVAL; |
| 945 | } |
| 946 | const auto bindIt = storageIt->second; |
| 947 | const auto storageId = bindIt->second.storage; |
| 948 | const auto ifs = getIfsLocked(storageId); |
| 949 | if (!ifs) { |
| 950 | LOG(ERROR) << "Internal error: storageId " << storageId << " for bound path " << target |
| 951 | << " is missing"; |
| 952 | return -EFAULT; |
| 953 | } |
| 954 | mBindsByPath.erase(storageIt); |
| 955 | l.unlock(); |
| 956 | |
| 957 | mVold->unmountIncFs(bindIt->first); |
| 958 | std::unique_lock l2(ifs->lock); |
| 959 | if (ifs->bindPoints.size() <= 1) { |
| 960 | ifs->bindPoints.clear(); |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 961 | deleteStorageLocked(*ifs, std::move(l2)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 962 | } else { |
| 963 | const std::string savedFile = std::move(bindIt->second.savedFilename); |
| 964 | ifs->bindPoints.erase(bindIt); |
| 965 | l2.unlock(); |
| 966 | if (!savedFile.empty()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 967 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, savedFile)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 968 | } |
| 969 | } |
Alex Buynytskyy | 0bdbccf | 2020-04-23 20:36:42 -0700 | [diff] [blame] | 970 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 971 | return 0; |
| 972 | } |
| 973 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 974 | std::string IncrementalService::normalizePathToStorageLocked( |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 975 | const IncFsMount& incfs, IncFsMount::StorageMap::const_iterator storageIt, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 976 | std::string_view path) const { |
| 977 | if (!path::isAbsolute(path)) { |
| 978 | return path::normalize(path::join(storageIt->second.name, path)); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 979 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 980 | auto normPath = path::normalize(path); |
| 981 | if (path::startsWith(normPath, storageIt->second.name)) { |
| 982 | return normPath; |
| 983 | } |
| 984 | // not that easy: need to find if any of the bind points match |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 985 | const auto bindIt = findParentPath(incfs.bindPoints, normPath); |
| 986 | if (bindIt == incfs.bindPoints.end()) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 987 | return {}; |
| 988 | } |
| 989 | return path::join(bindIt->second.sourceDir, path::relativize(bindIt->first, normPath)); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 992 | std::string IncrementalService::normalizePathToStorage(const IncFsMount& ifs, StorageId storage, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 993 | std::string_view path) const { |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 994 | std::unique_lock l(ifs.lock); |
| 995 | const auto storageInfo = ifs.storages.find(storage); |
| 996 | if (storageInfo == ifs.storages.end()) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 997 | return {}; |
| 998 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 999 | return normalizePathToStorageLocked(ifs, storageInfo, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1002 | int IncrementalService::makeFile(StorageId storage, std::string_view path, int mode, FileId id, |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1003 | incfs::NewFileParams params, std::span<const uint8_t> data) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1004 | if (auto ifs = getIfs(storage)) { |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1005 | std::string normPath = normalizePathToStorage(*ifs, storage, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 1006 | if (normPath.empty()) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1007 | LOG(ERROR) << "Internal error: storageId " << storage |
| 1008 | << " failed to normalize: " << path; |
Songchun Fan | 54c6aed | 2020-01-31 16:52:41 -0800 | [diff] [blame] | 1009 | return -EINVAL; |
| 1010 | } |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1011 | if (auto err = mIncFs->makeFile(ifs->control, normPath, mode, id, params); err) { |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 1012 | LOG(ERROR) << "Internal error: storageId " << storage << " failed to makeFile: " << err; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1013 | return err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1014 | } |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1015 | if (!data.empty()) { |
| 1016 | if (auto err = setFileContent(ifs, id, path, data); err) { |
| 1017 | return err; |
| 1018 | } |
| 1019 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1020 | return 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1021 | } |
| 1022 | return -EINVAL; |
| 1023 | } |
| 1024 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1025 | int IncrementalService::makeDir(StorageId storageId, std::string_view path, int mode) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1026 | if (auto ifs = getIfs(storageId)) { |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1027 | std::string normPath = normalizePathToStorage(*ifs, storageId, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 1028 | if (normPath.empty()) { |
| 1029 | return -EINVAL; |
| 1030 | } |
| 1031 | return mIncFs->makeDir(ifs->control, normPath, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1032 | } |
| 1033 | return -EINVAL; |
| 1034 | } |
| 1035 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1036 | int IncrementalService::makeDirs(StorageId storageId, std::string_view path, int mode) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1037 | const auto ifs = getIfs(storageId); |
| 1038 | if (!ifs) { |
| 1039 | return -EINVAL; |
| 1040 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1041 | return makeDirs(*ifs, storageId, path, mode); |
| 1042 | } |
| 1043 | |
| 1044 | int IncrementalService::makeDirs(const IncFsMount& ifs, StorageId storageId, std::string_view path, |
| 1045 | int mode) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 1046 | std::string normPath = normalizePathToStorage(ifs, storageId, path); |
| 1047 | if (normPath.empty()) { |
| 1048 | return -EINVAL; |
| 1049 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1050 | return mIncFs->makeDirs(ifs.control, normPath, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1053 | int IncrementalService::link(StorageId sourceStorageId, std::string_view oldPath, |
| 1054 | StorageId destStorageId, std::string_view newPath) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1055 | std::unique_lock l(mLock); |
| 1056 | auto ifsSrc = getIfsLocked(sourceStorageId); |
| 1057 | if (!ifsSrc) { |
| 1058 | return -EINVAL; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1059 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1060 | if (sourceStorageId != destStorageId && getIfsLocked(destStorageId) != ifsSrc) { |
| 1061 | return -EINVAL; |
| 1062 | } |
| 1063 | l.unlock(); |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1064 | std::string normOldPath = normalizePathToStorage(*ifsSrc, sourceStorageId, oldPath); |
| 1065 | std::string normNewPath = normalizePathToStorage(*ifsSrc, destStorageId, newPath); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1066 | if (normOldPath.empty() || normNewPath.empty()) { |
| 1067 | LOG(ERROR) << "Invalid paths in link(): " << normOldPath << " | " << normNewPath; |
| 1068 | return -EINVAL; |
| 1069 | } |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 1070 | if (auto err = mIncFs->link(ifsSrc->control, normOldPath, normNewPath); err < 0) { |
| 1071 | PLOG(ERROR) << "Failed to link " << oldPath << "[" << normOldPath << "]" |
| 1072 | << " to " << newPath << "[" << normNewPath << "]"; |
| 1073 | return err; |
| 1074 | } |
| 1075 | return 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1076 | } |
| 1077 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1078 | int IncrementalService::unlink(StorageId storage, std::string_view path) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1079 | if (auto ifs = getIfs(storage)) { |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1080 | std::string normOldPath = normalizePathToStorage(*ifs, storage, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 1081 | return mIncFs->unlink(ifs->control, normOldPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1082 | } |
| 1083 | return -EINVAL; |
| 1084 | } |
| 1085 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1086 | int IncrementalService::addBindMount(IncFsMount& ifs, StorageId storage, |
| 1087 | std::string_view storageRoot, std::string&& source, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1088 | std::string&& target, BindKind kind, |
| 1089 | std::unique_lock<std::mutex>& mainLock) { |
| 1090 | if (!isValidMountTarget(target)) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1091 | LOG(ERROR) << __func__ << ": invalid mount target " << target; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1092 | return -EINVAL; |
| 1093 | } |
| 1094 | |
| 1095 | std::string mdFileName; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1096 | std::string metadataFullPath; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1097 | if (kind != BindKind::Temporary) { |
| 1098 | metadata::BindPoint bp; |
| 1099 | bp.set_storage_id(storage); |
| 1100 | bp.set_allocated_dest_path(&target); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 1101 | bp.set_allocated_source_subdir(&source); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1102 | const auto metadata = bp.SerializeAsString(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1103 | bp.release_dest_path(); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 1104 | bp.release_source_subdir(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1105 | mdFileName = makeBindMdName(); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1106 | metadataFullPath = path::join(ifs.root, constants().mount, mdFileName); |
| 1107 | auto node = mIncFs->makeFile(ifs.control, metadataFullPath, 0444, idFromMetadata(metadata), |
| 1108 | {.metadata = {metadata.data(), (IncFsSize)metadata.size()}}); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1109 | if (node) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1110 | LOG(ERROR) << __func__ << ": couldn't create a mount node " << mdFileName; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1111 | return int(node); |
| 1112 | } |
| 1113 | } |
| 1114 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1115 | const auto res = addBindMountWithMd(ifs, storage, std::move(mdFileName), std::move(source), |
| 1116 | std::move(target), kind, mainLock); |
| 1117 | if (res) { |
| 1118 | mIncFs->unlink(ifs.control, metadataFullPath); |
| 1119 | } |
| 1120 | return res; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | int IncrementalService::addBindMountWithMd(IncrementalService::IncFsMount& ifs, StorageId storage, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1124 | std::string&& metadataName, std::string&& source, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1125 | std::string&& target, BindKind kind, |
| 1126 | std::unique_lock<std::mutex>& mainLock) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1127 | { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1128 | std::lock_guard l(mMountOperationLock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1129 | const auto status = mVold->bindMount(source, target); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1130 | if (!status.isOk()) { |
| 1131 | LOG(ERROR) << "Calling Vold::bindMount() failed: " << status.toString8(); |
| 1132 | return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC |
| 1133 | ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode() |
| 1134 | : status.serviceSpecificErrorCode() == 0 |
| 1135 | ? -EFAULT |
| 1136 | : status.serviceSpecificErrorCode() |
| 1137 | : -EIO; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | if (!mainLock.owns_lock()) { |
| 1142 | mainLock.lock(); |
| 1143 | } |
| 1144 | std::lock_guard l(ifs.lock); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1145 | addBindMountRecordLocked(ifs, storage, std::move(metadataName), std::move(source), |
| 1146 | std::move(target), kind); |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | void IncrementalService::addBindMountRecordLocked(IncFsMount& ifs, StorageId storage, |
| 1151 | std::string&& metadataName, std::string&& source, |
| 1152 | std::string&& target, BindKind kind) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1153 | const auto [it, _] = |
| 1154 | ifs.bindPoints.insert_or_assign(target, |
| 1155 | IncFsMount::Bind{storage, std::move(metadataName), |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1156 | std::move(source), kind}); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1157 | mBindsByPath[std::move(target)] = it; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | RawMetadata IncrementalService::getMetadata(StorageId storage, std::string_view path) const { |
| 1161 | const auto ifs = getIfs(storage); |
| 1162 | if (!ifs) { |
| 1163 | return {}; |
| 1164 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1165 | const auto normPath = normalizePathToStorage(*ifs, storage, path); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1166 | if (normPath.empty()) { |
| 1167 | return {}; |
| 1168 | } |
| 1169 | return mIncFs->getMetadata(ifs->control, normPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1172 | RawMetadata IncrementalService::getMetadata(StorageId storage, FileId node) const { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1173 | const auto ifs = getIfs(storage); |
| 1174 | if (!ifs) { |
| 1175 | return {}; |
| 1176 | } |
| 1177 | return mIncFs->getMetadata(ifs->control, node); |
| 1178 | } |
| 1179 | |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1180 | void IncrementalService::setUidReadTimeouts( |
| 1181 | StorageId storage, const std::vector<PerUidReadTimeouts>& perUidReadTimeouts) { |
| 1182 | using microseconds = std::chrono::microseconds; |
| 1183 | using milliseconds = std::chrono::milliseconds; |
| 1184 | |
| 1185 | auto maxPendingTimeUs = microseconds(0); |
| 1186 | for (const auto& timeouts : perUidReadTimeouts) { |
| 1187 | maxPendingTimeUs = std::max(maxPendingTimeUs, microseconds(timeouts.maxPendingTimeUs)); |
| 1188 | } |
| 1189 | if (maxPendingTimeUs < Constants::minPerUidTimeout) { |
Alex Buynytskyy | 2b2f5f7 | 2021-01-29 11:07:33 -0800 | [diff] [blame] | 1190 | LOG(ERROR) << "Skip setting read timeouts (maxPendingTime < Constants::minPerUidTimeout): " |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 1191 | << duration_cast<milliseconds>(maxPendingTimeUs).count() << "ms < " |
| 1192 | << Constants::minPerUidTimeout.count() << "ms"; |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | const auto ifs = getIfs(storage); |
| 1197 | if (!ifs) { |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 1198 | LOG(ERROR) << "Setting read timeouts failed: invalid storage id: " << storage; |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1199 | return; |
| 1200 | } |
| 1201 | |
| 1202 | if (auto err = mIncFs->setUidReadTimeouts(ifs->control, perUidReadTimeouts); err < 0) { |
| 1203 | LOG(ERROR) << "Setting read timeouts failed: " << -err; |
| 1204 | return; |
| 1205 | } |
| 1206 | |
| 1207 | const auto timeout = std::chrono::duration_cast<milliseconds>(maxPendingTimeUs) - |
| 1208 | Constants::perUidTimeoutOffset; |
| 1209 | updateUidReadTimeouts(storage, Clock::now() + timeout); |
| 1210 | } |
| 1211 | |
| 1212 | void IncrementalService::clearUidReadTimeouts(StorageId storage) { |
| 1213 | const auto ifs = getIfs(storage); |
| 1214 | if (!ifs) { |
| 1215 | return; |
| 1216 | } |
| 1217 | |
| 1218 | mIncFs->setUidReadTimeouts(ifs->control, {}); |
| 1219 | } |
| 1220 | |
| 1221 | void IncrementalService::updateUidReadTimeouts(StorageId storage, Clock::time_point timeLimit) { |
| 1222 | // Reached maximum timeout. |
| 1223 | if (Clock::now() >= timeLimit) { |
| 1224 | return clearUidReadTimeouts(storage); |
| 1225 | } |
| 1226 | |
| 1227 | // Still loading? |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 1228 | const auto progress = getLoadingProgress(storage, /*stopOnFirstIncomplete=*/true); |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1229 | if (progress.isError()) { |
| 1230 | // Something is wrong, abort. |
| 1231 | return clearUidReadTimeouts(storage); |
| 1232 | } |
| 1233 | |
| 1234 | if (progress.started() && progress.fullyLoaded()) { |
| 1235 | // Fully loaded, check readLogs collection. |
| 1236 | const auto ifs = getIfs(storage); |
| 1237 | if (!ifs->readLogsEnabled()) { |
| 1238 | return clearUidReadTimeouts(storage); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | const auto timeLeft = timeLimit - Clock::now(); |
| 1243 | if (timeLeft < Constants::progressUpdateInterval) { |
| 1244 | // Don't bother. |
| 1245 | return clearUidReadTimeouts(storage); |
| 1246 | } |
| 1247 | |
| 1248 | addTimedJob(*mTimedQueue, storage, Constants::progressUpdateInterval, |
| 1249 | [this, storage, timeLimit]() { updateUidReadTimeouts(storage, timeLimit); }); |
| 1250 | } |
| 1251 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1252 | std::unordered_set<std::string_view> IncrementalService::adoptMountedInstances() { |
| 1253 | std::unordered_set<std::string_view> mountedRootNames; |
| 1254 | mIncFs->listExistingMounts([this, &mountedRootNames](auto root, auto backingDir, auto binds) { |
| 1255 | LOG(INFO) << "Existing mount: " << backingDir << "->" << root; |
| 1256 | for (auto [source, target] : binds) { |
| 1257 | LOG(INFO) << " bind: '" << source << "'->'" << target << "'"; |
| 1258 | LOG(INFO) << " " << path::join(root, source); |
| 1259 | } |
| 1260 | |
| 1261 | // Ensure it's a kind of a mount that's managed by IncrementalService |
| 1262 | if (path::basename(root) != constants().mount || |
| 1263 | path::basename(backingDir) != constants().backing) { |
| 1264 | return; |
| 1265 | } |
| 1266 | const auto expectedRoot = path::dirname(root); |
| 1267 | if (path::dirname(backingDir) != expectedRoot) { |
| 1268 | return; |
| 1269 | } |
| 1270 | if (path::dirname(expectedRoot) != mIncrementalDir) { |
| 1271 | return; |
| 1272 | } |
| 1273 | if (!path::basename(expectedRoot).starts_with(constants().mountKeyPrefix)) { |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | LOG(INFO) << "Looks like an IncrementalService-owned: " << expectedRoot; |
| 1278 | |
| 1279 | // make sure we clean up the mount if it happens to be a bad one. |
| 1280 | // Note: unmounting needs to run first, so the cleanup object is created _last_. |
| 1281 | auto cleanupFiles = makeCleanup([&]() { |
| 1282 | LOG(INFO) << "Failed to adopt existing mount, deleting files: " << expectedRoot; |
| 1283 | IncFsMount::cleanupFilesystem(expectedRoot); |
| 1284 | }); |
| 1285 | auto cleanupMounts = makeCleanup([&]() { |
| 1286 | LOG(INFO) << "Failed to adopt existing mount, cleaning up: " << expectedRoot; |
| 1287 | for (auto&& [_, target] : binds) { |
| 1288 | mVold->unmountIncFs(std::string(target)); |
| 1289 | } |
| 1290 | mVold->unmountIncFs(std::string(root)); |
| 1291 | }); |
| 1292 | |
| 1293 | auto control = mIncFs->openMount(root); |
| 1294 | if (!control) { |
| 1295 | LOG(INFO) << "failed to open mount " << root; |
| 1296 | return; |
| 1297 | } |
| 1298 | |
| 1299 | auto mountRecord = |
| 1300 | parseFromIncfs<metadata::Mount>(mIncFs.get(), control, |
| 1301 | path::join(root, constants().infoMdName)); |
| 1302 | if (!mountRecord.has_loader() || !mountRecord.has_storage()) { |
| 1303 | LOG(ERROR) << "Bad mount metadata in mount at " << expectedRoot; |
| 1304 | return; |
| 1305 | } |
| 1306 | |
| 1307 | auto mountId = mountRecord.storage().id(); |
| 1308 | mNextId = std::max(mNextId, mountId + 1); |
| 1309 | |
| 1310 | DataLoaderParamsParcel dataLoaderParams; |
| 1311 | { |
| 1312 | const auto& loader = mountRecord.loader(); |
| 1313 | dataLoaderParams.type = (content::pm::DataLoaderType)loader.type(); |
| 1314 | dataLoaderParams.packageName = loader.package_name(); |
| 1315 | dataLoaderParams.className = loader.class_name(); |
| 1316 | dataLoaderParams.arguments = loader.arguments(); |
| 1317 | } |
| 1318 | |
| 1319 | auto ifs = std::make_shared<IncFsMount>(std::string(expectedRoot), mountId, |
| 1320 | std::move(control), *this); |
| 1321 | cleanupFiles.release(); // ifs will take care of that now |
| 1322 | |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 1323 | // Check if marker file present. |
| 1324 | if (checkReadLogsDisabledMarker(root)) { |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1325 | ifs->disallowReadLogs(); |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1328 | std::vector<std::pair<std::string, metadata::BindPoint>> permanentBindPoints; |
| 1329 | auto d = openDir(root); |
| 1330 | while (auto e = ::readdir(d.get())) { |
| 1331 | if (e->d_type == DT_REG) { |
| 1332 | auto name = std::string_view(e->d_name); |
| 1333 | if (name.starts_with(constants().mountpointMdPrefix)) { |
| 1334 | permanentBindPoints |
| 1335 | .emplace_back(name, |
| 1336 | parseFromIncfs<metadata::BindPoint>(mIncFs.get(), |
| 1337 | ifs->control, |
| 1338 | path::join(root, |
| 1339 | name))); |
| 1340 | if (permanentBindPoints.back().second.dest_path().empty() || |
| 1341 | permanentBindPoints.back().second.source_subdir().empty()) { |
| 1342 | permanentBindPoints.pop_back(); |
| 1343 | mIncFs->unlink(ifs->control, path::join(root, name)); |
| 1344 | } else { |
| 1345 | LOG(INFO) << "Permanent bind record: '" |
| 1346 | << permanentBindPoints.back().second.source_subdir() << "'->'" |
| 1347 | << permanentBindPoints.back().second.dest_path() << "'"; |
| 1348 | } |
| 1349 | } |
| 1350 | } else if (e->d_type == DT_DIR) { |
| 1351 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 1352 | continue; |
| 1353 | } |
| 1354 | auto name = std::string_view(e->d_name); |
| 1355 | if (name.starts_with(constants().storagePrefix)) { |
| 1356 | int storageId; |
| 1357 | const auto res = |
| 1358 | std::from_chars(name.data() + constants().storagePrefix.size() + 1, |
| 1359 | name.data() + name.size(), storageId); |
| 1360 | if (res.ec != std::errc{} || *res.ptr != '_') { |
| 1361 | LOG(WARNING) << "Ignoring storage with invalid name '" << name |
| 1362 | << "' for mount " << expectedRoot; |
| 1363 | continue; |
| 1364 | } |
| 1365 | auto [_, inserted] = mMounts.try_emplace(storageId, ifs); |
| 1366 | if (!inserted) { |
| 1367 | LOG(WARNING) << "Ignoring storage with duplicate id " << storageId |
| 1368 | << " for mount " << expectedRoot; |
| 1369 | continue; |
| 1370 | } |
| 1371 | ifs->storages.insert_or_assign(storageId, |
| 1372 | IncFsMount::Storage{path::join(root, name)}); |
| 1373 | mNextId = std::max(mNextId, storageId + 1); |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | if (ifs->storages.empty()) { |
| 1379 | LOG(WARNING) << "No valid storages in mount " << root; |
| 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | // now match the mounted directories with what we expect to have in the metadata |
| 1384 | { |
| 1385 | std::unique_lock l(mLock, std::defer_lock); |
| 1386 | for (auto&& [metadataFile, bindRecord] : permanentBindPoints) { |
| 1387 | auto mountedIt = std::find_if(binds.begin(), binds.end(), |
| 1388 | [&, bindRecord = bindRecord](auto&& bind) { |
| 1389 | return bind.second == bindRecord.dest_path() && |
| 1390 | path::join(root, bind.first) == |
| 1391 | bindRecord.source_subdir(); |
| 1392 | }); |
| 1393 | if (mountedIt != binds.end()) { |
| 1394 | LOG(INFO) << "Matched permanent bound " << bindRecord.source_subdir() |
| 1395 | << " to mount " << mountedIt->first; |
| 1396 | addBindMountRecordLocked(*ifs, bindRecord.storage_id(), std::move(metadataFile), |
| 1397 | std::move(*bindRecord.mutable_source_subdir()), |
| 1398 | std::move(*bindRecord.mutable_dest_path()), |
| 1399 | BindKind::Permanent); |
| 1400 | if (mountedIt != binds.end() - 1) { |
| 1401 | std::iter_swap(mountedIt, binds.end() - 1); |
| 1402 | } |
| 1403 | binds = binds.first(binds.size() - 1); |
| 1404 | } else { |
| 1405 | LOG(INFO) << "Didn't match permanent bound " << bindRecord.source_subdir() |
| 1406 | << ", mounting"; |
| 1407 | // doesn't exist - try mounting back |
| 1408 | if (addBindMountWithMd(*ifs, bindRecord.storage_id(), std::move(metadataFile), |
| 1409 | std::move(*bindRecord.mutable_source_subdir()), |
| 1410 | std::move(*bindRecord.mutable_dest_path()), |
| 1411 | BindKind::Permanent, l)) { |
| 1412 | mIncFs->unlink(ifs->control, metadataFile); |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | // if anything stays in |binds| those are probably temporary binds; system restarted since |
| 1419 | // they were mounted - so let's unmount them all. |
| 1420 | for (auto&& [source, target] : binds) { |
| 1421 | if (source.empty()) { |
| 1422 | continue; |
| 1423 | } |
| 1424 | mVold->unmountIncFs(std::string(target)); |
| 1425 | } |
| 1426 | cleanupMounts.release(); // ifs now manages everything |
| 1427 | |
| 1428 | if (ifs->bindPoints.empty()) { |
| 1429 | LOG(WARNING) << "No valid bind points for mount " << expectedRoot; |
| 1430 | deleteStorage(*ifs); |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | prepareDataLoaderLocked(*ifs, std::move(dataLoaderParams)); |
| 1435 | CHECK(ifs->dataLoaderStub); |
| 1436 | |
| 1437 | mountedRootNames.insert(path::basename(ifs->root)); |
| 1438 | |
| 1439 | // not locking here at all: we're still in the constructor, no other calls can happen |
| 1440 | mMounts[ifs->mountId] = std::move(ifs); |
| 1441 | }); |
| 1442 | |
| 1443 | return mountedRootNames; |
| 1444 | } |
| 1445 | |
| 1446 | void IncrementalService::mountExistingImages( |
| 1447 | const std::unordered_set<std::string_view>& mountedRootNames) { |
| 1448 | auto dir = openDir(mIncrementalDir); |
| 1449 | if (!dir) { |
| 1450 | PLOG(WARNING) << "Couldn't open the root incremental dir " << mIncrementalDir; |
| 1451 | return; |
| 1452 | } |
| 1453 | while (auto entry = ::readdir(dir.get())) { |
| 1454 | if (entry->d_type != DT_DIR) { |
| 1455 | continue; |
| 1456 | } |
| 1457 | std::string_view name = entry->d_name; |
| 1458 | if (!name.starts_with(constants().mountKeyPrefix)) { |
| 1459 | continue; |
| 1460 | } |
| 1461 | if (mountedRootNames.find(name) != mountedRootNames.end()) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1462 | continue; |
| 1463 | } |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 1464 | const auto root = path::join(mIncrementalDir, name); |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1465 | if (!mountExistingImage(root)) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1466 | IncFsMount::cleanupFilesystem(root); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1471 | bool IncrementalService::mountExistingImage(std::string_view root) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1472 | auto mountTarget = path::join(root, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1473 | const auto backing = path::join(root, constants().backing); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1474 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1475 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1476 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1477 | if (!status.isOk()) { |
| 1478 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 1479 | return false; |
| 1480 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 1481 | |
| 1482 | int cmd = controlParcel.cmd.release().release(); |
| 1483 | int pendingReads = controlParcel.pendingReads.release().release(); |
| 1484 | int logs = controlParcel.log.release().release(); |
Yurii Zubrytskyi | 5f69292 | 2020-12-08 07:35:24 -0800 | [diff] [blame] | 1485 | int blocksWritten = |
| 1486 | controlParcel.blocksWritten ? controlParcel.blocksWritten->release().release() : -1; |
| 1487 | IncFsMount::Control control = mIncFs->createControl(cmd, pendingReads, logs, blocksWritten); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1488 | |
| 1489 | auto ifs = std::make_shared<IncFsMount>(std::string(root), -1, std::move(control), *this); |
| 1490 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1491 | auto mount = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control, |
| 1492 | path::join(mountTarget, constants().infoMdName)); |
| 1493 | if (!mount.has_loader() || !mount.has_storage()) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1494 | LOG(ERROR) << "Bad mount metadata in mount at " << root; |
| 1495 | return false; |
| 1496 | } |
| 1497 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1498 | ifs->mountId = mount.storage().id(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1499 | mNextId = std::max(mNextId, ifs->mountId + 1); |
| 1500 | |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 1501 | // Check if marker file present. |
| 1502 | if (checkReadLogsDisabledMarker(mountTarget)) { |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1503 | ifs->disallowReadLogs(); |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1506 | // DataLoader params |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1507 | DataLoaderParamsParcel dataLoaderParams; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1508 | { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1509 | const auto& loader = mount.loader(); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1510 | dataLoaderParams.type = (content::pm::DataLoaderType)loader.type(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1511 | dataLoaderParams.packageName = loader.package_name(); |
| 1512 | dataLoaderParams.className = loader.class_name(); |
| 1513 | dataLoaderParams.arguments = loader.arguments(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 1516 | prepareDataLoader(*ifs, std::move(dataLoaderParams)); |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame] | 1517 | CHECK(ifs->dataLoaderStub); |
| 1518 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1519 | std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1520 | auto d = openDir(mountTarget); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1521 | while (auto e = ::readdir(d.get())) { |
| 1522 | if (e->d_type == DT_REG) { |
| 1523 | auto name = std::string_view(e->d_name); |
| 1524 | if (name.starts_with(constants().mountpointMdPrefix)) { |
| 1525 | bindPoints.emplace_back(name, |
| 1526 | parseFromIncfs<metadata::BindPoint>(mIncFs.get(), |
| 1527 | ifs->control, |
| 1528 | path::join(mountTarget, |
| 1529 | name))); |
| 1530 | if (bindPoints.back().second.dest_path().empty() || |
| 1531 | bindPoints.back().second.source_subdir().empty()) { |
| 1532 | bindPoints.pop_back(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1533 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | } else if (e->d_type == DT_DIR) { |
| 1537 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 1538 | continue; |
| 1539 | } |
| 1540 | auto name = std::string_view(e->d_name); |
| 1541 | if (name.starts_with(constants().storagePrefix)) { |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1542 | int storageId; |
| 1543 | const auto res = std::from_chars(name.data() + constants().storagePrefix.size() + 1, |
| 1544 | name.data() + name.size(), storageId); |
| 1545 | if (res.ec != std::errc{} || *res.ptr != '_') { |
| 1546 | LOG(WARNING) << "Ignoring storage with invalid name '" << name << "' for mount " |
| 1547 | << root; |
| 1548 | continue; |
| 1549 | } |
| 1550 | auto [_, inserted] = mMounts.try_emplace(storageId, ifs); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1551 | if (!inserted) { |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1552 | LOG(WARNING) << "Ignoring storage with duplicate id " << storageId |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1553 | << " for mount " << root; |
| 1554 | continue; |
| 1555 | } |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1556 | ifs->storages.insert_or_assign(storageId, |
| 1557 | IncFsMount::Storage{ |
| 1558 | path::join(root, constants().mount, name)}); |
| 1559 | mNextId = std::max(mNextId, storageId + 1); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | if (ifs->storages.empty()) { |
| 1565 | LOG(WARNING) << "No valid storages in mount " << root; |
| 1566 | return false; |
| 1567 | } |
| 1568 | |
| 1569 | int bindCount = 0; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1570 | { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1571 | std::unique_lock l(mLock, std::defer_lock); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1572 | for (auto&& bp : bindPoints) { |
| 1573 | bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first), |
| 1574 | std::move(*bp.second.mutable_source_subdir()), |
| 1575 | std::move(*bp.second.mutable_dest_path()), |
| 1576 | BindKind::Permanent, l); |
| 1577 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | if (bindCount == 0) { |
| 1581 | LOG(WARNING) << "No valid bind points for mount " << root; |
| 1582 | deleteStorage(*ifs); |
| 1583 | return false; |
| 1584 | } |
| 1585 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1586 | // not locking here at all: we're still in the constructor, no other calls can happen |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1587 | mMounts[ifs->mountId] = std::move(ifs); |
| 1588 | return true; |
| 1589 | } |
| 1590 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 1591 | void IncrementalService::runCmdLooper() { |
Alex Buynytskyy | b65a77f | 2020-09-22 11:39:53 -0700 | [diff] [blame] | 1592 | constexpr auto kTimeoutMsecs = -1; |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 1593 | while (mRunning.load(std::memory_order_relaxed)) { |
| 1594 | mLooper->pollAll(kTimeoutMsecs); |
| 1595 | } |
| 1596 | } |
| 1597 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1598 | IncrementalService::DataLoaderStubPtr IncrementalService::prepareDataLoader( |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1599 | IncFsMount& ifs, DataLoaderParamsParcel&& params, |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 1600 | const DataLoaderStatusListener* statusListener, |
| 1601 | StorageHealthCheckParams&& healthCheckParams, const StorageHealthListener* healthListener) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1602 | std::unique_lock l(ifs.lock); |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 1603 | prepareDataLoaderLocked(ifs, std::move(params), statusListener, std::move(healthCheckParams), |
| 1604 | healthListener); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1605 | return ifs.dataLoaderStub; |
| 1606 | } |
| 1607 | |
| 1608 | void IncrementalService::prepareDataLoaderLocked(IncFsMount& ifs, DataLoaderParamsParcel&& params, |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 1609 | const DataLoaderStatusListener* statusListener, |
| 1610 | StorageHealthCheckParams&& healthCheckParams, |
| 1611 | const StorageHealthListener* healthListener) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1612 | if (ifs.dataLoaderStub) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1613 | LOG(INFO) << "Skipped data loader preparation because it already exists"; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1614 | return; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1617 | FileSystemControlParcel fsControlParcel; |
Jooyung Han | 16bac85 | 2020-08-10 12:53:14 +0900 | [diff] [blame] | 1618 | fsControlParcel.incremental = std::make_optional<IncrementalFileSystemControlParcel>(); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1619 | fsControlParcel.incremental->cmd.reset(dup(ifs.control.cmd())); |
| 1620 | fsControlParcel.incremental->pendingReads.reset(dup(ifs.control.pendingReads())); |
| 1621 | fsControlParcel.incremental->log.reset(dup(ifs.control.logs())); |
Yurii Zubrytskyi | 5f69292 | 2020-12-08 07:35:24 -0800 | [diff] [blame] | 1622 | if (ifs.control.blocksWritten() >= 0) { |
| 1623 | fsControlParcel.incremental->blocksWritten.emplace(dup(ifs.control.blocksWritten())); |
| 1624 | } |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 1625 | fsControlParcel.service = new IncrementalServiceConnector(*this, ifs.mountId); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1626 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 1627 | ifs.dataLoaderStub = |
| 1628 | new DataLoaderStub(*this, ifs.mountId, std::move(params), std::move(fsControlParcel), |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 1629 | statusListener, std::move(healthCheckParams), healthListener, |
| 1630 | path::join(ifs.root, constants().mount)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1633 | template <class Duration> |
| 1634 | static long elapsedMcs(Duration start, Duration end) { |
| 1635 | return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); |
| 1636 | } |
| 1637 | |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 1638 | template <class Duration> |
| 1639 | static constexpr auto castToMs(Duration d) { |
| 1640 | return std::chrono::duration_cast<std::chrono::milliseconds>(d); |
| 1641 | } |
| 1642 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1643 | // Extract lib files from zip, create new files in incfs and write data to them |
Songchun Fan | c897531 | 2020-07-13 12:14:37 -0700 | [diff] [blame] | 1644 | // Lib files should be placed next to the APK file in the following matter: |
| 1645 | // Example: |
| 1646 | // /path/to/base.apk |
| 1647 | // /path/to/lib/arm/first.so |
| 1648 | // /path/to/lib/arm/second.so |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1649 | bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath, |
| 1650 | std::string_view libDirRelativePath, |
Songchun Fan | 14f6c3c | 2020-05-21 18:19:07 -0700 | [diff] [blame] | 1651 | std::string_view abi, bool extractNativeLibs) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1652 | auto start = Clock::now(); |
| 1653 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1654 | const auto ifs = getIfs(storage); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1655 | if (!ifs) { |
| 1656 | LOG(ERROR) << "Invalid storage " << storage; |
| 1657 | return false; |
| 1658 | } |
| 1659 | |
Songchun Fan | c897531 | 2020-07-13 12:14:37 -0700 | [diff] [blame] | 1660 | const auto targetLibPathRelativeToStorage = |
| 1661 | path::join(path::dirname(normalizePathToStorage(*ifs, storage, apkFullPath)), |
| 1662 | libDirRelativePath); |
| 1663 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1664 | // First prepare target directories if they don't exist yet |
Songchun Fan | c897531 | 2020-07-13 12:14:37 -0700 | [diff] [blame] | 1665 | if (auto res = makeDirs(*ifs, storage, targetLibPathRelativeToStorage, 0755)) { |
| 1666 | LOG(ERROR) << "Failed to prepare target lib directory " << targetLibPathRelativeToStorage |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1667 | << " errno: " << res; |
| 1668 | return false; |
| 1669 | } |
| 1670 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1671 | auto mkDirsTs = Clock::now(); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1672 | ZipArchiveHandle zipFileHandle; |
| 1673 | if (OpenArchive(path::c_str(apkFullPath), &zipFileHandle)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1674 | LOG(ERROR) << "Failed to open zip file at " << apkFullPath; |
| 1675 | return false; |
| 1676 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1677 | |
| 1678 | // Need a shared pointer: will be passing it into all unpacking jobs. |
| 1679 | std::shared_ptr<ZipArchive> zipFile(zipFileHandle, [](ZipArchiveHandle h) { CloseArchive(h); }); |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1680 | void* cookie = nullptr; |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 1681 | const auto libFilePrefix = path::join(constants().libDir, abi) += "/"; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1682 | if (StartIteration(zipFile.get(), &cookie, libFilePrefix, constants().libSuffix)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1683 | LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath; |
| 1684 | return false; |
| 1685 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1686 | auto endIteration = [](void* cookie) { EndIteration(cookie); }; |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1687 | auto iterationCleaner = std::unique_ptr<void, decltype(endIteration)>(cookie, endIteration); |
| 1688 | |
| 1689 | auto openZipTs = Clock::now(); |
| 1690 | |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 1691 | auto mapFiles = (mIncFs->features() & incfs::Features::v2); |
| 1692 | incfs::FileId sourceId; |
| 1693 | if (mapFiles) { |
| 1694 | sourceId = mIncFs->getFileId(ifs->control, apkFullPath); |
| 1695 | if (!incfs::isValidFileId(sourceId)) { |
| 1696 | LOG(WARNING) << "Error getting IncFS file ID for apk path '" << apkFullPath |
| 1697 | << "', mapping disabled"; |
| 1698 | mapFiles = false; |
| 1699 | } |
| 1700 | } |
| 1701 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1702 | std::vector<Job> jobQueue; |
| 1703 | ZipEntry entry; |
| 1704 | std::string_view fileName; |
| 1705 | while (!Next(cookie, &entry, &fileName)) { |
| 1706 | if (fileName.empty()) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1707 | continue; |
| 1708 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1709 | |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 1710 | const auto entryUncompressed = entry.method == kCompressStored; |
| 1711 | const auto entryPageAligned = (entry.offset & (constants().blockSize - 1)) == 0; |
| 1712 | |
Songchun Fan | 14f6c3c | 2020-05-21 18:19:07 -0700 | [diff] [blame] | 1713 | if (!extractNativeLibs) { |
| 1714 | // ensure the file is properly aligned and unpacked |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 1715 | if (!entryUncompressed) { |
Songchun Fan | 14f6c3c | 2020-05-21 18:19:07 -0700 | [diff] [blame] | 1716 | LOG(WARNING) << "Library " << fileName << " must be uncompressed to mmap it"; |
| 1717 | return false; |
| 1718 | } |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 1719 | if (!entryPageAligned) { |
Songchun Fan | 14f6c3c | 2020-05-21 18:19:07 -0700 | [diff] [blame] | 1720 | LOG(WARNING) << "Library " << fileName |
| 1721 | << " must be page-aligned to mmap it, offset = 0x" << std::hex |
| 1722 | << entry.offset; |
| 1723 | return false; |
| 1724 | } |
| 1725 | continue; |
| 1726 | } |
| 1727 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1728 | auto startFileTs = Clock::now(); |
| 1729 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1730 | const auto libName = path::basename(fileName); |
Songchun Fan | c897531 | 2020-07-13 12:14:37 -0700 | [diff] [blame] | 1731 | auto targetLibPath = path::join(targetLibPathRelativeToStorage, libName); |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1732 | const auto targetLibPathAbsolute = normalizePathToStorage(*ifs, storage, targetLibPath); |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1733 | // If the extract file already exists, skip |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1734 | if (access(targetLibPathAbsolute.c_str(), F_OK) == 0) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1735 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1736 | LOG(INFO) << "incfs: Native lib file already exists: " << targetLibPath |
| 1737 | << "; skipping extraction, spent " |
| 1738 | << elapsedMcs(startFileTs, Clock::now()) << "mcs"; |
| 1739 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1740 | continue; |
| 1741 | } |
| 1742 | |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 1743 | if (mapFiles && entryUncompressed && entryPageAligned && entry.uncompressed_length > 0) { |
| 1744 | incfs::NewMappedFileParams mappedFileParams = { |
| 1745 | .sourceId = sourceId, |
| 1746 | .sourceOffset = entry.offset, |
| 1747 | .size = entry.uncompressed_length, |
| 1748 | }; |
| 1749 | |
| 1750 | if (auto res = mIncFs->makeMappedFile(ifs->control, targetLibPathAbsolute, 0755, |
| 1751 | mappedFileParams); |
| 1752 | res == 0) { |
| 1753 | if (perfLoggingEnabled()) { |
| 1754 | auto doneTs = Clock::now(); |
| 1755 | LOG(INFO) << "incfs: Mapped " << libName << ": " |
| 1756 | << elapsedMcs(startFileTs, doneTs) << "mcs"; |
| 1757 | } |
| 1758 | continue; |
| 1759 | } else { |
| 1760 | LOG(WARNING) << "Failed to map file for: '" << targetLibPath << "' errno: " << res |
| 1761 | << "; falling back to full extraction"; |
| 1762 | } |
| 1763 | } |
| 1764 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1765 | // Create new lib file without signature info |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1766 | incfs::NewFileParams libFileParams = { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1767 | .size = entry.uncompressed_length, |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1768 | .signature = {}, |
| 1769 | // Metadata of the new lib file is its relative path |
| 1770 | .metadata = {targetLibPath.c_str(), (IncFsSize)targetLibPath.size()}, |
| 1771 | }; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1772 | incfs::FileId libFileId = idFromMetadata(targetLibPath); |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 1773 | if (auto res = mIncFs->makeFile(ifs->control, targetLibPathAbsolute, 0755, libFileId, |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1774 | libFileParams)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1775 | LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1776 | // If one lib file fails to be created, abort others as well |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1777 | return false; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1778 | } |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1779 | |
| 1780 | auto makeFileTs = Clock::now(); |
| 1781 | |
Songchun Fan | afaf6e9 | 2020-03-18 14:12:20 -0700 | [diff] [blame] | 1782 | // If it is a zero-byte file, skip data writing |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1783 | if (entry.uncompressed_length == 0) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1784 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1785 | LOG(INFO) << "incfs: Extracted " << libName |
| 1786 | << "(0 bytes): " << elapsedMcs(startFileTs, makeFileTs) << "mcs"; |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1787 | } |
Songchun Fan | afaf6e9 | 2020-03-18 14:12:20 -0700 | [diff] [blame] | 1788 | continue; |
| 1789 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1790 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 1791 | jobQueue.emplace_back([this, zipFile, entry, ifs = std::weak_ptr<IncFsMount>(ifs), |
| 1792 | libFileId, libPath = std::move(targetLibPath), |
| 1793 | makeFileTs]() mutable { |
| 1794 | extractZipFile(ifs.lock(), zipFile.get(), entry, libFileId, libPath, makeFileTs); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1795 | }); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1796 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1797 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1798 | auto prepareJobTs = Clock::now(); |
| 1799 | LOG(INFO) << "incfs: Processed " << libName << ": " |
| 1800 | << elapsedMcs(startFileTs, prepareJobTs) |
| 1801 | << "mcs, make file: " << elapsedMcs(startFileTs, makeFileTs) |
| 1802 | << " prepare job: " << elapsedMcs(makeFileTs, prepareJobTs); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1803 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1804 | } |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1805 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1806 | auto processedTs = Clock::now(); |
| 1807 | |
| 1808 | if (!jobQueue.empty()) { |
| 1809 | { |
| 1810 | std::lock_guard lock(mJobMutex); |
| 1811 | if (mRunning) { |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1812 | auto& existingJobs = mJobQueue[ifs->mountId]; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1813 | if (existingJobs.empty()) { |
| 1814 | existingJobs = std::move(jobQueue); |
| 1815 | } else { |
| 1816 | existingJobs.insert(existingJobs.end(), std::move_iterator(jobQueue.begin()), |
| 1817 | std::move_iterator(jobQueue.end())); |
| 1818 | } |
| 1819 | } |
| 1820 | } |
| 1821 | mJobCondition.notify_all(); |
| 1822 | } |
| 1823 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1824 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1825 | auto end = Clock::now(); |
| 1826 | LOG(INFO) << "incfs: configureNativeBinaries complete in " << elapsedMcs(start, end) |
| 1827 | << "mcs, make dirs: " << elapsedMcs(start, mkDirsTs) |
| 1828 | << " open zip: " << elapsedMcs(mkDirsTs, openZipTs) |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1829 | << " make files: " << elapsedMcs(openZipTs, processedTs) |
| 1830 | << " schedule jobs: " << elapsedMcs(processedTs, end); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | return true; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1834 | } |
| 1835 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1836 | void IncrementalService::extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile, |
| 1837 | ZipEntry& entry, const incfs::FileId& libFileId, |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1838 | std::string_view debugLibPath, |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1839 | Clock::time_point scheduledTs) { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 1840 | if (!ifs) { |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1841 | LOG(INFO) << "Skipping zip file " << debugLibPath << " extraction for an expired mount"; |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 1842 | return; |
| 1843 | } |
| 1844 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1845 | auto startedTs = Clock::now(); |
| 1846 | |
| 1847 | // Write extracted data to new file |
| 1848 | // NOTE: don't zero-initialize memory, it may take a while for nothing |
| 1849 | auto libData = std::unique_ptr<uint8_t[]>(new uint8_t[entry.uncompressed_length]); |
| 1850 | if (ExtractToMemory(zipFile, &entry, libData.get(), entry.uncompressed_length)) { |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1851 | LOG(ERROR) << "Failed to extract native lib zip entry: " << path::basename(debugLibPath); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1852 | return; |
| 1853 | } |
| 1854 | |
| 1855 | auto extractFileTs = Clock::now(); |
| 1856 | |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1857 | if (setFileContent(ifs, libFileId, debugLibPath, |
| 1858 | std::span(libData.get(), entry.uncompressed_length))) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1859 | return; |
| 1860 | } |
| 1861 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1862 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1863 | auto endFileTs = Clock::now(); |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1864 | LOG(INFO) << "incfs: Extracted " << path::basename(debugLibPath) << "(" |
| 1865 | << entry.compressed_length << " -> " << entry.uncompressed_length |
| 1866 | << " bytes): " << elapsedMcs(startedTs, endFileTs) |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1867 | << "mcs, scheduling delay: " << elapsedMcs(scheduledTs, startedTs) |
| 1868 | << " extract: " << elapsedMcs(startedTs, extractFileTs) |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1869 | << " open/prepare/write: " << elapsedMcs(extractFileTs, endFileTs); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | bool IncrementalService::waitForNativeBinariesExtraction(StorageId storage) { |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1874 | struct WaitPrinter { |
| 1875 | const Clock::time_point startTs = Clock::now(); |
| 1876 | ~WaitPrinter() noexcept { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1877 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1878 | const auto endTs = Clock::now(); |
| 1879 | LOG(INFO) << "incfs: waitForNativeBinariesExtraction() complete in " |
| 1880 | << elapsedMcs(startTs, endTs) << "mcs"; |
| 1881 | } |
| 1882 | } |
| 1883 | } waitPrinter; |
| 1884 | |
| 1885 | MountId mount; |
| 1886 | { |
| 1887 | auto ifs = getIfs(storage); |
| 1888 | if (!ifs) { |
| 1889 | return true; |
| 1890 | } |
| 1891 | mount = ifs->mountId; |
| 1892 | } |
| 1893 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1894 | std::unique_lock lock(mJobMutex); |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1895 | mJobCondition.wait(lock, [this, mount] { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1896 | return !mRunning || |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1897 | (mPendingJobsMount != mount && mJobQueue.find(mount) == mJobQueue.end()); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1898 | }); |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1899 | return mRunning; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Alex Buynytskyy | b39d13e | 2020-09-12 16:12:36 -0700 | [diff] [blame] | 1902 | int IncrementalService::setFileContent(const IfsMountPtr& ifs, const incfs::FileId& fileId, |
| 1903 | std::string_view debugFilePath, |
| 1904 | std::span<const uint8_t> data) const { |
| 1905 | auto startTs = Clock::now(); |
| 1906 | |
| 1907 | const auto writeFd = mIncFs->openForSpecialOps(ifs->control, fileId); |
| 1908 | if (!writeFd.ok()) { |
| 1909 | LOG(ERROR) << "Failed to open write fd for: " << debugFilePath |
| 1910 | << " errno: " << writeFd.get(); |
| 1911 | return writeFd.get(); |
| 1912 | } |
| 1913 | |
| 1914 | const auto dataLength = data.size(); |
| 1915 | |
| 1916 | auto openFileTs = Clock::now(); |
| 1917 | const int numBlocks = (data.size() + constants().blockSize - 1) / constants().blockSize; |
| 1918 | std::vector<IncFsDataBlock> instructions(numBlocks); |
| 1919 | for (int i = 0; i < numBlocks; i++) { |
| 1920 | const auto blockSize = std::min<long>(constants().blockSize, data.size()); |
| 1921 | instructions[i] = IncFsDataBlock{ |
| 1922 | .fileFd = writeFd.get(), |
| 1923 | .pageIndex = static_cast<IncFsBlockIndex>(i), |
| 1924 | .compression = INCFS_COMPRESSION_KIND_NONE, |
| 1925 | .kind = INCFS_BLOCK_KIND_DATA, |
| 1926 | .dataSize = static_cast<uint32_t>(blockSize), |
| 1927 | .data = reinterpret_cast<const char*>(data.data()), |
| 1928 | }; |
| 1929 | data = data.subspan(blockSize); |
| 1930 | } |
| 1931 | auto prepareInstsTs = Clock::now(); |
| 1932 | |
| 1933 | size_t res = mIncFs->writeBlocks(instructions); |
| 1934 | if (res != instructions.size()) { |
| 1935 | LOG(ERROR) << "Failed to write data into: " << debugFilePath; |
| 1936 | return res; |
| 1937 | } |
| 1938 | |
| 1939 | if (perfLoggingEnabled()) { |
| 1940 | auto endTs = Clock::now(); |
| 1941 | LOG(INFO) << "incfs: Set file content " << debugFilePath << "(" << dataLength |
| 1942 | << " bytes): " << elapsedMcs(startTs, endTs) |
| 1943 | << "mcs, open: " << elapsedMcs(startTs, openFileTs) |
| 1944 | << " prepare: " << elapsedMcs(openFileTs, prepareInstsTs) |
| 1945 | << " write: " << elapsedMcs(prepareInstsTs, endTs); |
| 1946 | } |
| 1947 | |
| 1948 | return 0; |
| 1949 | } |
| 1950 | |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1951 | int IncrementalService::isFileFullyLoaded(StorageId storage, std::string_view filePath) const { |
Alex Buynytskyy | bc0a7e6 | 2020-08-25 12:45:22 -0700 | [diff] [blame] | 1952 | std::unique_lock l(mLock); |
| 1953 | const auto ifs = getIfsLocked(storage); |
| 1954 | if (!ifs) { |
| 1955 | LOG(ERROR) << "isFileFullyLoaded failed, invalid storageId: " << storage; |
| 1956 | return -EINVAL; |
| 1957 | } |
| 1958 | const auto storageInfo = ifs->storages.find(storage); |
| 1959 | if (storageInfo == ifs->storages.end()) { |
| 1960 | LOG(ERROR) << "isFileFullyLoaded failed, no storage: " << storage; |
| 1961 | return -EINVAL; |
| 1962 | } |
| 1963 | l.unlock(); |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1964 | return isFileFullyLoadedFromPath(*ifs, filePath); |
Alex Buynytskyy | bc0a7e6 | 2020-08-25 12:45:22 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | int IncrementalService::isFileFullyLoadedFromPath(const IncFsMount& ifs, |
| 1968 | std::string_view filePath) const { |
| 1969 | const auto [filledBlocks, totalBlocks] = mIncFs->countFilledBlocks(ifs.control, filePath); |
| 1970 | if (filledBlocks < 0) { |
| 1971 | LOG(ERROR) << "isFileFullyLoadedFromPath failed to get filled blocks count for: " |
| 1972 | << filePath << " errno: " << filledBlocks; |
| 1973 | return filledBlocks; |
| 1974 | } |
| 1975 | if (totalBlocks < filledBlocks) { |
| 1976 | LOG(ERROR) << "isFileFullyLoadedFromPath failed to get total num of blocks"; |
| 1977 | return -EINVAL; |
| 1978 | } |
| 1979 | return totalBlocks - filledBlocks; |
| 1980 | } |
| 1981 | |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1982 | IncrementalService::LoadingProgress IncrementalService::getLoadingProgress( |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 1983 | StorageId storage, bool stopOnFirstIncomplete) const { |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 1984 | std::unique_lock l(mLock); |
| 1985 | const auto ifs = getIfsLocked(storage); |
| 1986 | if (!ifs) { |
| 1987 | LOG(ERROR) << "getLoadingProgress failed, invalid storageId: " << storage; |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1988 | return {-EINVAL, -EINVAL}; |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 1989 | } |
| 1990 | const auto storageInfo = ifs->storages.find(storage); |
| 1991 | if (storageInfo == ifs->storages.end()) { |
| 1992 | LOG(ERROR) << "getLoadingProgress failed, no storage: " << storage; |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1993 | return {-EINVAL, -EINVAL}; |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 1994 | } |
| 1995 | l.unlock(); |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 1996 | return getLoadingProgressFromPath(*ifs, storageInfo->second.name, stopOnFirstIncomplete); |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 1997 | } |
| 1998 | |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 1999 | IncrementalService::LoadingProgress IncrementalService::getLoadingProgressFromPath( |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 2000 | const IncFsMount& ifs, std::string_view storagePath, |
| 2001 | const bool stopOnFirstIncomplete) const { |
| 2002 | ssize_t totalBlocks = 0, filledBlocks = 0, error = 0; |
| 2003 | mFs->listFilesRecursive(storagePath, [&, this](auto filePath) { |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 2004 | const auto [filledBlocksCount, totalBlocksCount] = |
| 2005 | mIncFs->countFilledBlocks(ifs.control, filePath); |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 2006 | if (filledBlocksCount == -EOPNOTSUPP || filledBlocksCount == -ENOTSUP || |
| 2007 | filledBlocksCount == -ENOENT) { |
| 2008 | // a kind of a file that's not really being loaded, e.g. a mapped range |
| 2009 | // an older IncFS used to return ENOENT in this case, so handle it the same way |
| 2010 | return true; |
| 2011 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 2012 | if (filledBlocksCount < 0) { |
| 2013 | LOG(ERROR) << "getLoadingProgress failed to get filled blocks count for: " << filePath |
| 2014 | << " errno: " << filledBlocksCount; |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 2015 | error = filledBlocksCount; |
| 2016 | return false; |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 2017 | } |
| 2018 | totalBlocks += totalBlocksCount; |
| 2019 | filledBlocks += filledBlocksCount; |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 2020 | if (stopOnFirstIncomplete && filledBlocks < totalBlocks) { |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 2021 | return false; |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 2022 | } |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 2023 | return true; |
| 2024 | }); |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 2025 | |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 2026 | return error ? LoadingProgress{error, error} : LoadingProgress{filledBlocks, totalBlocks}; |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2029 | bool IncrementalService::updateLoadingProgress( |
| 2030 | StorageId storage, const StorageLoadingProgressListener& progressListener) { |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 2031 | const auto progress = getLoadingProgress(storage, /*stopOnFirstIncomplete=*/false); |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 2032 | if (progress.isError()) { |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2033 | // Failed to get progress from incfs, abort. |
| 2034 | return false; |
| 2035 | } |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 2036 | progressListener->onStorageLoadingProgressChanged(storage, progress.getProgress()); |
| 2037 | if (progress.fullyLoaded()) { |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2038 | // Stop updating progress once it is fully loaded |
| 2039 | return true; |
| 2040 | } |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 2041 | addTimedJob(*mProgressUpdateJobQueue, storage, |
| 2042 | Constants::progressUpdateInterval /* repeat after 1s */, |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2043 | [storage, progressListener, this]() { |
| 2044 | updateLoadingProgress(storage, progressListener); |
| 2045 | }); |
| 2046 | return true; |
| 2047 | } |
| 2048 | |
| 2049 | bool IncrementalService::registerLoadingProgressListener( |
| 2050 | StorageId storage, const StorageLoadingProgressListener& progressListener) { |
| 2051 | return updateLoadingProgress(storage, progressListener); |
| 2052 | } |
| 2053 | |
| 2054 | bool IncrementalService::unregisterLoadingProgressListener(StorageId storage) { |
| 2055 | return removeTimedJobs(*mProgressUpdateJobQueue, storage); |
| 2056 | } |
| 2057 | |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 2058 | bool IncrementalService::registerStorageHealthListener( |
| 2059 | StorageId storage, StorageHealthCheckParams&& healthCheckParams, |
| 2060 | const StorageHealthListener& healthListener) { |
| 2061 | DataLoaderStubPtr dataLoaderStub; |
| 2062 | { |
| 2063 | std::unique_lock l(mLock); |
| 2064 | const auto& ifs = getIfsLocked(storage); |
| 2065 | if (!ifs) { |
| 2066 | return false; |
| 2067 | } |
| 2068 | dataLoaderStub = ifs->dataLoaderStub; |
| 2069 | if (!dataLoaderStub) { |
| 2070 | return false; |
| 2071 | } |
| 2072 | } |
| 2073 | dataLoaderStub->setHealthListener(std::move(healthCheckParams), &healthListener); |
| 2074 | return true; |
| 2075 | } |
| 2076 | |
| 2077 | void IncrementalService::unregisterStorageHealthListener(StorageId storage) { |
| 2078 | StorageHealthCheckParams invalidCheckParams; |
| 2079 | invalidCheckParams.blockedTimeoutMs = -1; |
| 2080 | registerStorageHealthListener(storage, std::move(invalidCheckParams), {}); |
| 2081 | } |
| 2082 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 2083 | bool IncrementalService::perfLoggingEnabled() { |
| 2084 | static const bool enabled = base::GetBoolProperty("incremental.perflogging", false); |
| 2085 | return enabled; |
| 2086 | } |
| 2087 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 2088 | void IncrementalService::runJobProcessing() { |
| 2089 | for (;;) { |
| 2090 | std::unique_lock lock(mJobMutex); |
| 2091 | mJobCondition.wait(lock, [this]() { return !mRunning || !mJobQueue.empty(); }); |
| 2092 | if (!mRunning) { |
| 2093 | return; |
| 2094 | } |
| 2095 | |
| 2096 | auto it = mJobQueue.begin(); |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 2097 | mPendingJobsMount = it->first; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 2098 | auto queue = std::move(it->second); |
| 2099 | mJobQueue.erase(it); |
| 2100 | lock.unlock(); |
| 2101 | |
| 2102 | for (auto&& job : queue) { |
| 2103 | job(); |
| 2104 | } |
| 2105 | |
| 2106 | lock.lock(); |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 2107 | mPendingJobsMount = kInvalidStorageId; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 2108 | lock.unlock(); |
| 2109 | mJobCondition.notify_all(); |
| 2110 | } |
| 2111 | } |
| 2112 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 2113 | void IncrementalService::registerAppOpsCallback(const std::string& packageName) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 2114 | sp<IAppOpsCallback> listener; |
| 2115 | { |
| 2116 | std::unique_lock lock{mCallbacksLock}; |
| 2117 | auto& cb = mCallbackRegistered[packageName]; |
| 2118 | if (cb) { |
| 2119 | return; |
| 2120 | } |
| 2121 | cb = new AppOpsListener(*this, packageName); |
| 2122 | listener = cb; |
| 2123 | } |
| 2124 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 2125 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_GET_USAGE_STATS, |
| 2126 | String16(packageName.c_str()), listener); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | bool IncrementalService::unregisterAppOpsCallback(const std::string& packageName) { |
| 2130 | sp<IAppOpsCallback> listener; |
| 2131 | { |
| 2132 | std::unique_lock lock{mCallbacksLock}; |
| 2133 | auto found = mCallbackRegistered.find(packageName); |
| 2134 | if (found == mCallbackRegistered.end()) { |
| 2135 | return false; |
| 2136 | } |
| 2137 | listener = found->second; |
| 2138 | mCallbackRegistered.erase(found); |
| 2139 | } |
| 2140 | |
| 2141 | mAppOpsManager->stopWatchingMode(listener); |
| 2142 | return true; |
| 2143 | } |
| 2144 | |
| 2145 | void IncrementalService::onAppOpChanged(const std::string& packageName) { |
| 2146 | if (!unregisterAppOpsCallback(packageName)) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 2147 | return; |
| 2148 | } |
| 2149 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 2150 | std::vector<IfsMountPtr> affected; |
| 2151 | { |
| 2152 | std::lock_guard l(mLock); |
| 2153 | affected.reserve(mMounts.size()); |
| 2154 | for (auto&& [id, ifs] : mMounts) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2155 | if (ifs->mountId == id && ifs->dataLoaderStub->params().packageName == packageName) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 2156 | affected.push_back(ifs); |
| 2157 | } |
| 2158 | } |
| 2159 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 2160 | for (auto&& ifs : affected) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 2161 | applyStorageParams(*ifs, false); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 2162 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 2163 | } |
| 2164 | |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2165 | bool IncrementalService::addTimedJob(TimedQueueWrapper& timedQueue, MountId id, Milliseconds after, |
| 2166 | Job what) { |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2167 | if (id == kInvalidStorageId) { |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2168 | return false; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2169 | } |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2170 | timedQueue.addJob(id, after, std::move(what)); |
| 2171 | return true; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2174 | bool IncrementalService::removeTimedJobs(TimedQueueWrapper& timedQueue, MountId id) { |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2175 | if (id == kInvalidStorageId) { |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2176 | return false; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2177 | } |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2178 | timedQueue.removeJobs(id); |
| 2179 | return true; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Songchun Fan | 1b76ccf | 2021-02-24 22:25:59 +0000 | [diff] [blame] | 2182 | void IncrementalService::getMetrics(StorageId storageId, android::os::PersistableBundle* result) { |
| 2183 | const auto duration = getMillsSinceOldestPendingRead(storageId); |
| 2184 | if (duration >= 0) { |
| 2185 | const auto kMetricsMillisSinceOldestPendingRead = |
| 2186 | os::incremental::BnIncrementalService::METRICS_MILLIS_SINCE_OLDEST_PENDING_READ(); |
| 2187 | result->putLong(String16(kMetricsMillisSinceOldestPendingRead.data()), duration); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | long IncrementalService::getMillsSinceOldestPendingRead(StorageId storageId) { |
| 2192 | std::unique_lock l(mLock); |
| 2193 | const auto ifs = getIfsLocked(storageId); |
| 2194 | if (!ifs) { |
| 2195 | LOG(ERROR) << "getMillsSinceOldestPendingRead failed, invalid storageId: " << storageId; |
| 2196 | return -EINVAL; |
| 2197 | } |
| 2198 | if (!ifs->dataLoaderStub) { |
| 2199 | LOG(ERROR) << "getMillsSinceOldestPendingRead failed, no data loader: " << storageId; |
| 2200 | return -EINVAL; |
| 2201 | } |
| 2202 | return ifs->dataLoaderStub->elapsedMsSinceOldestPendingRead(); |
| 2203 | } |
| 2204 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2205 | IncrementalService::DataLoaderStub::DataLoaderStub(IncrementalService& service, MountId id, |
| 2206 | DataLoaderParamsParcel&& params, |
| 2207 | FileSystemControlParcel&& control, |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2208 | const DataLoaderStatusListener* statusListener, |
| 2209 | StorageHealthCheckParams&& healthCheckParams, |
| 2210 | const StorageHealthListener* healthListener, |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 2211 | std::string&& healthPath) |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2212 | : mService(service), |
| 2213 | mId(id), |
| 2214 | mParams(std::move(params)), |
| 2215 | mControl(std::move(control)), |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2216 | mStatusListener(statusListener ? *statusListener : DataLoaderStatusListener()), |
| 2217 | mHealthListener(healthListener ? *healthListener : StorageHealthListener()), |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2218 | mHealthPath(std::move(healthPath)), |
| 2219 | mHealthCheckParams(std::move(healthCheckParams)) { |
| 2220 | if (mHealthListener) { |
| 2221 | if (!isHealthParamsValid()) { |
| 2222 | mHealthListener = {}; |
| 2223 | } |
| 2224 | } else { |
| 2225 | // Disable advanced health check statuses. |
| 2226 | mHealthCheckParams.blockedTimeoutMs = -1; |
| 2227 | } |
| 2228 | updateHealthStatus(); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2231 | IncrementalService::DataLoaderStub::~DataLoaderStub() { |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2232 | if (isValid()) { |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2233 | cleanupResources(); |
| 2234 | } |
| 2235 | } |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 2236 | |
| 2237 | void IncrementalService::DataLoaderStub::cleanupResources() { |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2238 | auto now = Clock::now(); |
| 2239 | { |
| 2240 | std::unique_lock lock(mMutex); |
| 2241 | mHealthPath.clear(); |
| 2242 | unregisterFromPendingReads(); |
| 2243 | resetHealthControl(); |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2244 | mService.removeTimedJobs(*mService.mTimedQueue, mId); |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 2247 | requestDestroy(); |
Alex Buynytskyy | b0ea448 | 2020-05-04 18:39:58 -0700 | [diff] [blame] | 2248 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2249 | { |
| 2250 | std::unique_lock lock(mMutex); |
| 2251 | mParams = {}; |
| 2252 | mControl = {}; |
| 2253 | mHealthControl = {}; |
| 2254 | mHealthListener = {}; |
| 2255 | mStatusCondition.wait_until(lock, now + 60s, [this] { |
| 2256 | return mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_DESTROYED; |
| 2257 | }); |
| 2258 | mStatusListener = {}; |
| 2259 | mId = kInvalidStorageId; |
| 2260 | } |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2261 | } |
| 2262 | |
Alex Buynytskyy | 0bdbccf | 2020-04-23 20:36:42 -0700 | [diff] [blame] | 2263 | sp<content::pm::IDataLoader> IncrementalService::DataLoaderStub::getDataLoader() { |
| 2264 | sp<IDataLoader> dataloader; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2265 | auto status = mService.mDataLoaderManager->getDataLoader(id(), &dataloader); |
Alex Buynytskyy | 0bdbccf | 2020-04-23 20:36:42 -0700 | [diff] [blame] | 2266 | if (!status.isOk()) { |
| 2267 | LOG(ERROR) << "Failed to get dataloader: " << status.toString8(); |
| 2268 | return {}; |
| 2269 | } |
| 2270 | if (!dataloader) { |
| 2271 | LOG(ERROR) << "DataLoader is null: " << status.toString8(); |
| 2272 | return {}; |
| 2273 | } |
| 2274 | return dataloader; |
| 2275 | } |
| 2276 | |
Alex Buynytskyy | d7aa346 | 2021-03-14 22:20:20 -0700 | [diff] [blame] | 2277 | bool IncrementalService::DataLoaderStub::isSystemDataLoader() const { |
| 2278 | return (params().packageName == Constants::systemPackage); |
| 2279 | } |
| 2280 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2281 | bool IncrementalService::DataLoaderStub::requestCreate() { |
| 2282 | return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_CREATED); |
| 2283 | } |
| 2284 | |
| 2285 | bool IncrementalService::DataLoaderStub::requestStart() { |
| 2286 | return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_STARTED); |
| 2287 | } |
| 2288 | |
| 2289 | bool IncrementalService::DataLoaderStub::requestDestroy() { |
| 2290 | return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_DESTROYED); |
| 2291 | } |
| 2292 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 2293 | bool IncrementalService::DataLoaderStub::setTargetStatus(int newStatus) { |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 2294 | { |
Alex Buynytskyy | b0ea448 | 2020-05-04 18:39:58 -0700 | [diff] [blame] | 2295 | std::unique_lock lock(mMutex); |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2296 | setTargetStatusLocked(newStatus); |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 2297 | } |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2298 | return fsmStep(); |
| 2299 | } |
| 2300 | |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2301 | void IncrementalService::DataLoaderStub::setTargetStatusLocked(int status) { |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2302 | auto oldStatus = mTargetStatus; |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2303 | mTargetStatus = status; |
| 2304 | mTargetStatusTs = Clock::now(); |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2305 | LOG(DEBUG) << "Target status update for DataLoader " << id() << ": " << oldStatus << " -> " |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2306 | << status << " (current " << mCurrentStatus << ")"; |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2307 | } |
| 2308 | |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2309 | std::optional<Milliseconds> IncrementalService::DataLoaderStub::needToBind() { |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2310 | std::unique_lock lock(mMutex); |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2311 | |
| 2312 | const auto now = mService.mClock->now(); |
| 2313 | const bool healthy = (mPreviousBindDelay == 0ms); |
| 2314 | |
| 2315 | if (mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_BINDING && |
| 2316 | now - mCurrentStatusTs <= Constants::bindingTimeout) { |
| 2317 | LOG(INFO) << "Binding still in progress. " |
| 2318 | << (healthy ? "The DL is healthy/freshly bound, ok to retry for a few times." |
| 2319 | : "Already unhealthy, don't do anything."); |
| 2320 | // Binding still in progress. |
| 2321 | if (!healthy) { |
| 2322 | // Already unhealthy, don't do anything. |
| 2323 | return {}; |
| 2324 | } |
| 2325 | // The DL is healthy/freshly bound, ok to retry for a few times. |
| 2326 | if (now - mPreviousBindTs <= Constants::bindGracePeriod) { |
| 2327 | // Still within grace period. |
| 2328 | if (now - mCurrentStatusTs >= Constants::bindRetryInterval) { |
| 2329 | // Retry interval passed, retrying. |
| 2330 | mCurrentStatusTs = now; |
| 2331 | mPreviousBindDelay = 0ms; |
| 2332 | return 0ms; |
| 2333 | } |
| 2334 | return {}; |
| 2335 | } |
| 2336 | // fallthrough, mark as unhealthy, and retry with delay |
| 2337 | } |
| 2338 | |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2339 | const auto previousBindTs = mPreviousBindTs; |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2340 | mPreviousBindTs = now; |
| 2341 | |
| 2342 | const auto nonCrashingInterval = std::max(castToMs(now - previousBindTs), 100ms); |
| 2343 | if (previousBindTs.time_since_epoch() == Clock::duration::zero() || |
| 2344 | nonCrashingInterval > Constants::healthyDataLoaderUptime) { |
| 2345 | mPreviousBindDelay = 0ms; |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2346 | return 0ms; |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | constexpr auto minBindDelayMs = castToMs(Constants::minBindDelay); |
| 2350 | constexpr auto maxBindDelayMs = castToMs(Constants::maxBindDelay); |
| 2351 | |
| 2352 | const auto bindDelayMs = |
| 2353 | std::min(std::max(mPreviousBindDelay * Constants::bindDelayMultiplier, minBindDelayMs), |
| 2354 | maxBindDelayMs) |
| 2355 | .count(); |
| 2356 | const auto bindDelayJitterRangeMs = bindDelayMs / Constants::bindDelayJitterDivider; |
| 2357 | const auto bindDelayJitterMs = rand() % (bindDelayJitterRangeMs * 2) - bindDelayJitterRangeMs; |
| 2358 | mPreviousBindDelay = std::chrono::milliseconds(bindDelayMs + bindDelayJitterMs); |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2359 | return mPreviousBindDelay; |
| 2360 | } |
| 2361 | |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2362 | bool IncrementalService::DataLoaderStub::bind() { |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2363 | const auto maybeBindDelay = needToBind(); |
| 2364 | if (!maybeBindDelay) { |
| 2365 | LOG(DEBUG) << "Skipping bind to " << mParams.packageName << " because of pending bind."; |
| 2366 | return true; |
| 2367 | } |
| 2368 | const auto bindDelay = *maybeBindDelay; |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2369 | if (bindDelay > 1s) { |
| 2370 | LOG(INFO) << "Delaying bind to " << mParams.packageName << " by " |
| 2371 | << bindDelay.count() / 1000 << "s"; |
| 2372 | } |
| 2373 | |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2374 | bool result = false; |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2375 | auto status = mService.mDataLoaderManager->bindToDataLoader(id(), mParams, bindDelay.count(), |
| 2376 | this, &result); |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2377 | if (!status.isOk() || !result) { |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2378 | const bool healthy = (bindDelay == 0ms); |
| 2379 | LOG(ERROR) << "Failed to bind a data loader for mount " << id() |
| 2380 | << (healthy ? ", retrying." : ""); |
| 2381 | |
| 2382 | // Internal error, retry for healthy/new DLs. |
| 2383 | // Let needToBind migrate it to unhealthy after too many retries. |
| 2384 | if (healthy) { |
| 2385 | if (mService.addTimedJob(*mService.mTimedQueue, id(), Constants::bindRetryInterval, |
| 2386 | [this]() { fsmStep(); })) { |
| 2387 | // Mark as binding so that we know it's not the DL's fault. |
| 2388 | setCurrentStatus(IDataLoaderStatusListener::DATA_LOADER_BINDING); |
| 2389 | return true; |
| 2390 | } |
| 2391 | } |
| 2392 | |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2393 | return false; |
| 2394 | } |
| 2395 | return true; |
| 2396 | } |
| 2397 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2398 | bool IncrementalService::DataLoaderStub::create() { |
Alex Buynytskyy | 0bdbccf | 2020-04-23 20:36:42 -0700 | [diff] [blame] | 2399 | auto dataloader = getDataLoader(); |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2400 | if (!dataloader) { |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2401 | return false; |
| 2402 | } |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2403 | auto status = dataloader->create(id(), mParams, mControl, this); |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2404 | if (!status.isOk()) { |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2405 | LOG(ERROR) << "Failed to create DataLoader: " << status.toString8(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2406 | return false; |
| 2407 | } |
| 2408 | return true; |
| 2409 | } |
| 2410 | |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 2411 | bool IncrementalService::DataLoaderStub::start() { |
Alex Buynytskyy | 0bdbccf | 2020-04-23 20:36:42 -0700 | [diff] [blame] | 2412 | auto dataloader = getDataLoader(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2413 | if (!dataloader) { |
| 2414 | return false; |
| 2415 | } |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2416 | auto status = dataloader->start(id()); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2417 | if (!status.isOk()) { |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2418 | LOG(ERROR) << "Failed to start DataLoader: " << status.toString8(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2419 | return false; |
| 2420 | } |
| 2421 | return true; |
| 2422 | } |
| 2423 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2424 | bool IncrementalService::DataLoaderStub::destroy() { |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2425 | return mService.mDataLoaderManager->unbindFromDataLoader(id()).isOk(); |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 2426 | } |
| 2427 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2428 | bool IncrementalService::DataLoaderStub::fsmStep() { |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 2429 | if (!isValid()) { |
| 2430 | return false; |
| 2431 | } |
| 2432 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2433 | int currentStatus; |
| 2434 | int targetStatus; |
| 2435 | { |
Alex Buynytskyy | b0ea448 | 2020-05-04 18:39:58 -0700 | [diff] [blame] | 2436 | std::unique_lock lock(mMutex); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2437 | currentStatus = mCurrentStatus; |
| 2438 | targetStatus = mTargetStatus; |
| 2439 | } |
| 2440 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2441 | LOG(DEBUG) << "fsmStep: " << id() << ": " << currentStatus << " -> " << targetStatus; |
Alex Buynytskyy | 4dbc060 | 2020-05-12 11:24:14 -0700 | [diff] [blame] | 2442 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2443 | if (currentStatus == targetStatus) { |
| 2444 | return true; |
| 2445 | } |
| 2446 | |
| 2447 | switch (targetStatus) { |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2448 | case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE: |
| 2449 | // Do nothing, this is a reset state. |
| 2450 | break; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2451 | case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: { |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2452 | switch (currentStatus) { |
| 2453 | case IDataLoaderStatusListener::DATA_LOADER_BINDING: |
| 2454 | setCurrentStatus(IDataLoaderStatusListener::DATA_LOADER_DESTROYED); |
| 2455 | return true; |
| 2456 | default: |
| 2457 | return destroy(); |
| 2458 | } |
| 2459 | break; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2460 | } |
| 2461 | case IDataLoaderStatusListener::DATA_LOADER_STARTED: { |
| 2462 | switch (currentStatus) { |
| 2463 | case IDataLoaderStatusListener::DATA_LOADER_CREATED: |
| 2464 | case IDataLoaderStatusListener::DATA_LOADER_STOPPED: |
| 2465 | return start(); |
| 2466 | } |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 2467 | [[fallthrough]]; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2468 | } |
| 2469 | case IDataLoaderStatusListener::DATA_LOADER_CREATED: |
| 2470 | switch (currentStatus) { |
| 2471 | case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2472 | case IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE: |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2473 | case IDataLoaderStatusListener::DATA_LOADER_BINDING: |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2474 | return bind(); |
| 2475 | case IDataLoaderStatusListener::DATA_LOADER_BOUND: |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2476 | return create(); |
| 2477 | } |
| 2478 | break; |
| 2479 | default: |
| 2480 | LOG(ERROR) << "Invalid target status: " << targetStatus |
| 2481 | << ", current status: " << currentStatus; |
| 2482 | break; |
| 2483 | } |
| 2484 | return false; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | binder::Status IncrementalService::DataLoaderStub::onStatusChanged(MountId mountId, int newStatus) { |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 2488 | if (!isValid()) { |
| 2489 | return binder::Status:: |
| 2490 | fromServiceSpecificError(-EINVAL, "onStatusChange came to invalid DataLoaderStub"); |
| 2491 | } |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2492 | if (id() != mountId) { |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2493 | LOG(ERROR) << "onStatusChanged: mount ID mismatch: expected " << id() |
| 2494 | << ", but got: " << mountId; |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 2495 | return binder::Status::fromServiceSpecificError(-EPERM, "Mount ID mismatch."); |
| 2496 | } |
Alex Buynytskyy | 060c9d6 | 2021-02-18 20:55:17 -0800 | [diff] [blame] | 2497 | if (newStatus == IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE) { |
| 2498 | // User-provided status, let's postpone the handling to avoid possible deadlocks. |
| 2499 | mService.addTimedJob(*mService.mTimedQueue, id(), Constants::userStatusDelay, |
| 2500 | [this, newStatus]() { setCurrentStatus(newStatus); }); |
| 2501 | return binder::Status::ok(); |
| 2502 | } |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 2503 | |
Alex Buynytskyy | 060c9d6 | 2021-02-18 20:55:17 -0800 | [diff] [blame] | 2504 | setCurrentStatus(newStatus); |
| 2505 | return binder::Status::ok(); |
| 2506 | } |
| 2507 | |
| 2508 | void IncrementalService::DataLoaderStub::setCurrentStatus(int newStatus) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 2509 | int targetStatus, oldStatus; |
Alex Buynytskyy | b0ea448 | 2020-05-04 18:39:58 -0700 | [diff] [blame] | 2510 | DataLoaderStatusListener listener; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2511 | { |
Alex Buynytskyy | b0ea448 | 2020-05-04 18:39:58 -0700 | [diff] [blame] | 2512 | std::unique_lock lock(mMutex); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2513 | if (mCurrentStatus == newStatus) { |
Alex Buynytskyy | 060c9d6 | 2021-02-18 20:55:17 -0800 | [diff] [blame] | 2514 | return; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2515 | } |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2516 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 2517 | oldStatus = mCurrentStatus; |
| 2518 | targetStatus = mTargetStatus; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2519 | listener = mStatusListener; |
Alex Buynytskyy | b0ea448 | 2020-05-04 18:39:58 -0700 | [diff] [blame] | 2520 | |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2521 | // Change the status. |
| 2522 | mCurrentStatus = newStatus; |
| 2523 | mCurrentStatusTs = mService.mClock->now(); |
| 2524 | |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2525 | if (mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE || |
| 2526 | mCurrentStatus == IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE) { |
Alex Buynytskyy | 4dbc060 | 2020-05-12 11:24:14 -0700 | [diff] [blame] | 2527 | // For unavailable, unbind from DataLoader to ensure proper re-commit. |
| 2528 | setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_DESTROYED); |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2529 | } |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2530 | } |
| 2531 | |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2532 | LOG(DEBUG) << "Current status update for DataLoader " << id() << ": " << oldStatus << " -> " |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 2533 | << newStatus << " (target " << targetStatus << ")"; |
| 2534 | |
Alex Buynytskyy | b0ea448 | 2020-05-04 18:39:58 -0700 | [diff] [blame] | 2535 | if (listener) { |
Alex Buynytskyy | 060c9d6 | 2021-02-18 20:55:17 -0800 | [diff] [blame] | 2536 | listener->onStatusChanged(id(), newStatus); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2539 | fsmStep(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 2540 | |
Alex Buynytskyy | c2a645d | 2020-04-20 14:11:55 -0700 | [diff] [blame] | 2541 | mStatusCondition.notify_all(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 2542 | } |
| 2543 | |
Songchun Fan | 3309398 | 2020-09-10 13:12:39 -0700 | [diff] [blame] | 2544 | binder::Status IncrementalService::DataLoaderStub::reportStreamHealth(MountId mountId, |
| 2545 | int newStatus) { |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 2546 | if (!isValid()) { |
| 2547 | return binder::Status:: |
| 2548 | fromServiceSpecificError(-EINVAL, |
| 2549 | "reportStreamHealth came to invalid DataLoaderStub"); |
| 2550 | } |
| 2551 | if (id() != mountId) { |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2552 | LOG(ERROR) << "reportStreamHealth: mount ID mismatch: expected " << id() |
| 2553 | << ", but got: " << mountId; |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 2554 | return binder::Status::fromServiceSpecificError(-EPERM, "Mount ID mismatch."); |
| 2555 | } |
| 2556 | { |
| 2557 | std::lock_guard lock(mMutex); |
| 2558 | mStreamStatus = newStatus; |
| 2559 | } |
Songchun Fan | 3309398 | 2020-09-10 13:12:39 -0700 | [diff] [blame] | 2560 | return binder::Status::ok(); |
| 2561 | } |
| 2562 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2563 | bool IncrementalService::DataLoaderStub::isHealthParamsValid() const { |
| 2564 | return mHealthCheckParams.blockedTimeoutMs > 0 && |
| 2565 | mHealthCheckParams.blockedTimeoutMs < mHealthCheckParams.unhealthyTimeoutMs; |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2568 | void IncrementalService::DataLoaderStub::onHealthStatus(StorageHealthListener healthListener, |
| 2569 | int healthStatus) { |
| 2570 | LOG(DEBUG) << id() << ": healthStatus: " << healthStatus; |
| 2571 | if (healthListener) { |
| 2572 | healthListener->onHealthStatus(id(), healthStatus); |
| 2573 | } |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 2576 | static int adjustHealthStatus(int healthStatus, int streamStatus) { |
| 2577 | if (healthStatus == IStorageHealthListener::HEALTH_STATUS_OK) { |
| 2578 | // everything is good; no need to change status |
| 2579 | return healthStatus; |
| 2580 | } |
| 2581 | int newHeathStatus = healthStatus; |
| 2582 | switch (streamStatus) { |
| 2583 | case IDataLoaderStatusListener::STREAM_STORAGE_ERROR: |
| 2584 | // storage is limited and storage not healthy |
| 2585 | newHeathStatus = IStorageHealthListener::HEALTH_STATUS_UNHEALTHY_STORAGE; |
| 2586 | break; |
| 2587 | case IDataLoaderStatusListener::STREAM_INTEGRITY_ERROR: |
| 2588 | // fall through |
| 2589 | case IDataLoaderStatusListener::STREAM_SOURCE_ERROR: |
| 2590 | // fall through |
| 2591 | case IDataLoaderStatusListener::STREAM_TRANSPORT_ERROR: |
| 2592 | if (healthStatus == IStorageHealthListener::HEALTH_STATUS_UNHEALTHY) { |
| 2593 | newHeathStatus = IStorageHealthListener::HEALTH_STATUS_UNHEALTHY_TRANSPORT; |
| 2594 | } |
| 2595 | // pending/blocked status due to transportation issues is not regarded as unhealthy |
| 2596 | break; |
| 2597 | default: |
| 2598 | break; |
| 2599 | } |
| 2600 | return newHeathStatus; |
| 2601 | } |
| 2602 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2603 | void IncrementalService::DataLoaderStub::updateHealthStatus(bool baseline) { |
| 2604 | LOG(DEBUG) << id() << ": updateHealthStatus" << (baseline ? " (baseline)" : ""); |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 2605 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2606 | int healthStatusToReport = -1; |
| 2607 | StorageHealthListener healthListener; |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 2608 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2609 | { |
| 2610 | std::unique_lock lock(mMutex); |
| 2611 | unregisterFromPendingReads(); |
| 2612 | |
| 2613 | healthListener = mHealthListener; |
| 2614 | |
| 2615 | // Healthcheck depends on timestamp of the oldest pending read. |
| 2616 | // To get it, we need to re-open a pendingReads FD to get a full list of reads. |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 2617 | // Additionally we need to re-register for epoll with fresh FDs in case there are no |
| 2618 | // reads. |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2619 | const auto now = Clock::now(); |
| 2620 | const auto kernelTsUs = getOldestPendingReadTs(); |
| 2621 | if (baseline) { |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 2622 | // Updating baseline only on looper/epoll callback, i.e. on new set of pending |
| 2623 | // reads. |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2624 | mHealthBase = {now, kernelTsUs}; |
| 2625 | } |
| 2626 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2627 | if (kernelTsUs == kMaxBootClockTsUs || mHealthBase.kernelTsUs == kMaxBootClockTsUs || |
| 2628 | mHealthBase.userTs > now) { |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2629 | LOG(DEBUG) << id() << ": No pending reads or invalid base, report Ok and wait."; |
| 2630 | registerForPendingReads(); |
| 2631 | healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_OK; |
| 2632 | lock.unlock(); |
| 2633 | onHealthStatus(healthListener, healthStatusToReport); |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 2634 | return; |
| 2635 | } |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2636 | |
| 2637 | resetHealthControl(); |
| 2638 | |
| 2639 | // Always make sure the data loader is started. |
| 2640 | setTargetStatusLocked(IDataLoaderStatusListener::DATA_LOADER_STARTED); |
| 2641 | |
| 2642 | // Skip any further processing if health check params are invalid. |
| 2643 | if (!isHealthParamsValid()) { |
| 2644 | LOG(DEBUG) << id() |
| 2645 | << ": Skip any further processing if health check params are invalid."; |
| 2646 | healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_READS_PENDING; |
| 2647 | lock.unlock(); |
| 2648 | onHealthStatus(healthListener, healthStatusToReport); |
| 2649 | // Triggering data loader start. This is a one-time action. |
| 2650 | fsmStep(); |
| 2651 | return; |
| 2652 | } |
| 2653 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2654 | // Don't schedule timer job less than 500ms in advance. |
| 2655 | static constexpr auto kTolerance = 500ms; |
| 2656 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2657 | const auto blockedTimeout = std::chrono::milliseconds(mHealthCheckParams.blockedTimeoutMs); |
| 2658 | const auto unhealthyTimeout = |
| 2659 | std::chrono::milliseconds(mHealthCheckParams.unhealthyTimeoutMs); |
| 2660 | const auto unhealthyMonitoring = |
| 2661 | std::max(1000ms, |
| 2662 | std::chrono::milliseconds(mHealthCheckParams.unhealthyMonitoringMs)); |
| 2663 | |
Songchun Fan | 1b76ccf | 2021-02-24 22:25:59 +0000 | [diff] [blame] | 2664 | const auto delta = elapsedMsSinceKernelTs(now, kernelTsUs); |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2665 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2666 | Milliseconds checkBackAfter; |
| 2667 | if (delta + kTolerance < blockedTimeout) { |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2668 | LOG(DEBUG) << id() << ": Report reads pending and wait for blocked status."; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2669 | checkBackAfter = blockedTimeout - delta; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2670 | healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_READS_PENDING; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2671 | } else if (delta + kTolerance < unhealthyTimeout) { |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2672 | LOG(DEBUG) << id() << ": Report blocked and wait for unhealthy."; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2673 | checkBackAfter = unhealthyTimeout - delta; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2674 | healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_BLOCKED; |
| 2675 | } else { |
| 2676 | LOG(DEBUG) << id() << ": Report unhealthy and continue monitoring."; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2677 | checkBackAfter = unhealthyMonitoring; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2678 | healthStatusToReport = IStorageHealthListener::HEALTH_STATUS_UNHEALTHY; |
| 2679 | } |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 2680 | // Adjust health status based on stream status |
| 2681 | healthStatusToReport = adjustHealthStatus(healthStatusToReport, mStreamStatus); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2682 | LOG(DEBUG) << id() << ": updateHealthStatus in " << double(checkBackAfter.count()) / 1000.0 |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2683 | << "secs"; |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 2684 | mService.addTimedJob(*mService.mTimedQueue, id(), checkBackAfter, |
| 2685 | [this]() { updateHealthStatus(); }); |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2688 | // With kTolerance we are expecting these to execute before the next update. |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2689 | if (healthStatusToReport != -1) { |
| 2690 | onHealthStatus(healthListener, healthStatusToReport); |
| 2691 | } |
| 2692 | |
| 2693 | fsmStep(); |
| 2694 | } |
| 2695 | |
Songchun Fan | 1b76ccf | 2021-02-24 22:25:59 +0000 | [diff] [blame] | 2696 | Milliseconds IncrementalService::DataLoaderStub::elapsedMsSinceKernelTs(TimePoint now, |
| 2697 | BootClockTsUs kernelTsUs) { |
| 2698 | const auto kernelDeltaUs = kernelTsUs - mHealthBase.kernelTsUs; |
| 2699 | const auto userTs = mHealthBase.userTs + std::chrono::microseconds(kernelDeltaUs); |
| 2700 | return std::chrono::duration_cast<Milliseconds>(now - userTs); |
| 2701 | } |
| 2702 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2703 | const incfs::UniqueControl& IncrementalService::DataLoaderStub::initializeHealthControl() { |
| 2704 | if (mHealthPath.empty()) { |
| 2705 | resetHealthControl(); |
| 2706 | return mHealthControl; |
| 2707 | } |
| 2708 | if (mHealthControl.pendingReads() < 0) { |
| 2709 | mHealthControl = mService.mIncFs->openMount(mHealthPath); |
| 2710 | } |
| 2711 | if (mHealthControl.pendingReads() < 0) { |
| 2712 | LOG(ERROR) << "Failed to open health control for: " << id() << ", path: " << mHealthPath |
| 2713 | << "(" << mHealthControl.cmd() << ":" << mHealthControl.pendingReads() << ":" |
| 2714 | << mHealthControl.logs() << ")"; |
| 2715 | } |
| 2716 | return mHealthControl; |
| 2717 | } |
| 2718 | |
| 2719 | void IncrementalService::DataLoaderStub::resetHealthControl() { |
| 2720 | mHealthControl = {}; |
| 2721 | } |
| 2722 | |
| 2723 | BootClockTsUs IncrementalService::DataLoaderStub::getOldestPendingReadTs() { |
| 2724 | auto result = kMaxBootClockTsUs; |
| 2725 | |
| 2726 | const auto& control = initializeHealthControl(); |
| 2727 | if (control.pendingReads() < 0) { |
| 2728 | return result; |
| 2729 | } |
| 2730 | |
Songchun Fan | 6944f1e | 2020-11-06 15:24:24 -0800 | [diff] [blame] | 2731 | if (mService.mIncFs->waitForPendingReads(control, 0ms, &mLastPendingReads) != |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2732 | android::incfs::WaitResult::HaveData || |
Songchun Fan | 6944f1e | 2020-11-06 15:24:24 -0800 | [diff] [blame] | 2733 | mLastPendingReads.empty()) { |
Songchun Fan | 1b76ccf | 2021-02-24 22:25:59 +0000 | [diff] [blame] | 2734 | // Clear previous pending reads |
| 2735 | mLastPendingReads.clear(); |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2736 | return result; |
| 2737 | } |
| 2738 | |
| 2739 | LOG(DEBUG) << id() << ": pendingReads: " << control.pendingReads() << ", " |
Songchun Fan | 6944f1e | 2020-11-06 15:24:24 -0800 | [diff] [blame] | 2740 | << mLastPendingReads.size() << ": " << mLastPendingReads.front().bootClockTsUs; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2741 | |
Songchun Fan | 1b76ccf | 2021-02-24 22:25:59 +0000 | [diff] [blame] | 2742 | return getOldestTsFromLastPendingReads(); |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | void IncrementalService::DataLoaderStub::registerForPendingReads() { |
| 2746 | const auto pendingReadsFd = mHealthControl.pendingReads(); |
| 2747 | if (pendingReadsFd < 0) { |
| 2748 | return; |
| 2749 | } |
| 2750 | |
| 2751 | LOG(DEBUG) << id() << ": addFd(pendingReadsFd): " << pendingReadsFd; |
| 2752 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2753 | mService.mLooper->addFd( |
| 2754 | pendingReadsFd, android::Looper::POLL_CALLBACK, android::Looper::EVENT_INPUT, |
| 2755 | [](int, int, void* data) -> int { |
| 2756 | auto&& self = (DataLoaderStub*)data; |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2757 | self->updateHealthStatus(/*baseline=*/true); |
| 2758 | return 0; |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2759 | }, |
| 2760 | this); |
| 2761 | mService.mLooper->wake(); |
| 2762 | } |
| 2763 | |
Songchun Fan | 1b76ccf | 2021-02-24 22:25:59 +0000 | [diff] [blame] | 2764 | BootClockTsUs IncrementalService::DataLoaderStub::getOldestTsFromLastPendingReads() { |
| 2765 | auto result = kMaxBootClockTsUs; |
| 2766 | for (auto&& pendingRead : mLastPendingReads) { |
| 2767 | result = std::min(result, pendingRead.bootClockTsUs); |
| 2768 | } |
| 2769 | return result; |
| 2770 | } |
| 2771 | |
| 2772 | long IncrementalService::DataLoaderStub::elapsedMsSinceOldestPendingRead() { |
| 2773 | const auto oldestPendingReadKernelTs = getOldestTsFromLastPendingReads(); |
| 2774 | if (oldestPendingReadKernelTs == kMaxBootClockTsUs) { |
| 2775 | return 0; |
| 2776 | } |
| 2777 | return elapsedMsSinceKernelTs(Clock::now(), oldestPendingReadKernelTs).count(); |
| 2778 | } |
| 2779 | |
Alex Buynytskyy | d0855a3 | 2020-05-07 18:40:51 -0700 | [diff] [blame] | 2780 | void IncrementalService::DataLoaderStub::unregisterFromPendingReads() { |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2781 | const auto pendingReadsFd = mHealthControl.pendingReads(); |
| 2782 | if (pendingReadsFd < 0) { |
| 2783 | return; |
| 2784 | } |
| 2785 | |
Alex Buynytskyy | 4760d8f | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2786 | LOG(DEBUG) << id() << ": removeFd(pendingReadsFd): " << pendingReadsFd; |
| 2787 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2788 | mService.mLooper->removeFd(pendingReadsFd); |
| 2789 | mService.mLooper->wake(); |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 2790 | } |
| 2791 | |
Songchun Fan | 2570ec0 | 2020-10-08 17:22:33 -0700 | [diff] [blame] | 2792 | void IncrementalService::DataLoaderStub::setHealthListener( |
| 2793 | StorageHealthCheckParams&& healthCheckParams, const StorageHealthListener* healthListener) { |
| 2794 | std::lock_guard lock(mMutex); |
| 2795 | mHealthCheckParams = std::move(healthCheckParams); |
| 2796 | if (healthListener == nullptr) { |
| 2797 | // reset listener and params |
| 2798 | mHealthListener = {}; |
| 2799 | } else { |
| 2800 | mHealthListener = *healthListener; |
| 2801 | } |
| 2802 | } |
| 2803 | |
Songchun Fan | 6944f1e | 2020-11-06 15:24:24 -0800 | [diff] [blame] | 2804 | static std::string toHexString(const RawMetadata& metadata) { |
| 2805 | int n = metadata.size(); |
| 2806 | std::string res(n * 2, '\0'); |
| 2807 | // Same as incfs::toString(fileId) |
| 2808 | static constexpr char kHexChar[] = "0123456789abcdef"; |
| 2809 | for (int i = 0; i < n; ++i) { |
| 2810 | res[i * 2] = kHexChar[(metadata[i] & 0xf0) >> 4]; |
| 2811 | res[i * 2 + 1] = kHexChar[(metadata[i] & 0x0f)]; |
| 2812 | } |
| 2813 | return res; |
| 2814 | } |
| 2815 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2816 | void IncrementalService::DataLoaderStub::onDump(int fd) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 2817 | dprintf(fd, " dataLoader: {\n"); |
| 2818 | dprintf(fd, " currentStatus: %d\n", mCurrentStatus); |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 2819 | dprintf(fd, " currentStatusTs: %lldmcs\n", |
| 2820 | (long long)(elapsedMcs(mCurrentStatusTs, Clock::now()))); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 2821 | dprintf(fd, " targetStatus: %d\n", mTargetStatus); |
| 2822 | dprintf(fd, " targetStatusTs: %lldmcs\n", |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2823 | (long long)(elapsedMcs(mTargetStatusTs, Clock::now()))); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2824 | dprintf(fd, " health: {\n"); |
| 2825 | dprintf(fd, " path: %s\n", mHealthPath.c_str()); |
| 2826 | dprintf(fd, " base: %lldmcs (%lld)\n", |
| 2827 | (long long)(elapsedMcs(mHealthBase.userTs, Clock::now())), |
| 2828 | (long long)mHealthBase.kernelTsUs); |
| 2829 | dprintf(fd, " blockedTimeoutMs: %d\n", int(mHealthCheckParams.blockedTimeoutMs)); |
| 2830 | dprintf(fd, " unhealthyTimeoutMs: %d\n", int(mHealthCheckParams.unhealthyTimeoutMs)); |
| 2831 | dprintf(fd, " unhealthyMonitoringMs: %d\n", |
| 2832 | int(mHealthCheckParams.unhealthyMonitoringMs)); |
Songchun Fan | 6944f1e | 2020-11-06 15:24:24 -0800 | [diff] [blame] | 2833 | dprintf(fd, " lastPendingReads: \n"); |
| 2834 | const auto control = mService.mIncFs->openMount(mHealthPath); |
| 2835 | for (auto&& pendingRead : mLastPendingReads) { |
| 2836 | dprintf(fd, " fileId: %s\n", mService.mIncFs->toString(pendingRead.id).c_str()); |
| 2837 | const auto metadata = mService.mIncFs->getMetadata(control, pendingRead.id); |
| 2838 | dprintf(fd, " metadataHex: %s\n", toHexString(metadata).c_str()); |
| 2839 | dprintf(fd, " blockIndex: %d\n", pendingRead.block); |
| 2840 | dprintf(fd, " bootClockTsUs: %lld\n", (long long)pendingRead.bootClockTsUs); |
| 2841 | } |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 2842 | dprintf(fd, " bind: %llds ago (delay: %llds)\n", |
| 2843 | (long long)(elapsedMcs(mPreviousBindTs, Clock::now()) / 1000000), |
| 2844 | (long long)(mPreviousBindDelay.count() / 1000)); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 2845 | dprintf(fd, " }\n"); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2846 | const auto& params = mParams; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 2847 | dprintf(fd, " dataLoaderParams: {\n"); |
| 2848 | dprintf(fd, " type: %s\n", toString(params.type).c_str()); |
| 2849 | dprintf(fd, " packageName: %s\n", params.packageName.c_str()); |
| 2850 | dprintf(fd, " className: %s\n", params.className.c_str()); |
| 2851 | dprintf(fd, " arguments: %s\n", params.arguments.c_str()); |
| 2852 | dprintf(fd, " }\n"); |
| 2853 | dprintf(fd, " }\n"); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 2854 | } |
| 2855 | |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 2856 | void IncrementalService::AppOpsListener::opChanged(int32_t, const String16&) { |
| 2857 | incrementalService.onAppOpChanged(packageName); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 2858 | } |
| 2859 | |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 2860 | binder::Status IncrementalService::IncrementalServiceConnector::setStorageParams( |
| 2861 | bool enableReadLogs, int32_t* _aidl_return) { |
| 2862 | *_aidl_return = incrementalService.setStorageParams(storage, enableReadLogs); |
| 2863 | return binder::Status::ok(); |
| 2864 | } |
| 2865 | |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 2866 | FileId IncrementalService::idFromMetadata(std::span<const uint8_t> metadata) { |
| 2867 | return IncFs_FileIdFromMetadata({(const char*)metadata.data(), metadata.size()}); |
| 2868 | } |
| 2869 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 2870 | } // namespace android::incremental |