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