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