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