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