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