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