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