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