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