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> |
Jooyung Han | 66c567a | 2020-03-07 21:47:09 +0900 | [diff] [blame] | 26 | #include <binder/Nullable.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 27 | #include <binder/Status.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <uuid/uuid.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 30 | |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 31 | #include <charconv> |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 32 | #include <ctime> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 33 | #include <iterator> |
| 34 | #include <span> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 35 | #include <type_traits> |
| 36 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 37 | #include "IncrementalServiceValidation.h" |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 38 | #include "Metadata.pb.h" |
| 39 | |
| 40 | using namespace std::literals; |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 41 | namespace fs = std::filesystem; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 42 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 43 | constexpr const char* kDataUsageStats = "android.permission.LOADER_USAGE_STATS"; |
Alex Buynytskyy | 119de1f | 2020-04-08 16:15:35 -0700 | [diff] [blame] | 44 | constexpr const char* kOpUsage = "android:loader_usage_stats"; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 45 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 46 | namespace android::incremental { |
| 47 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 48 | using content::pm::DataLoaderParamsParcel; |
| 49 | using content::pm::FileSystemControlParcel; |
| 50 | using content::pm::IDataLoader; |
| 51 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 52 | namespace { |
| 53 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 54 | using IncrementalFileSystemControlParcel = os::incremental::IncrementalFileSystemControlParcel; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 55 | |
| 56 | struct Constants { |
| 57 | static constexpr auto backing = "backing_store"sv; |
| 58 | static constexpr auto mount = "mount"sv; |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 59 | static constexpr auto mountKeyPrefix = "MT_"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 60 | static constexpr auto storagePrefix = "st"sv; |
| 61 | static constexpr auto mountpointMdPrefix = ".mountpoint."sv; |
| 62 | static constexpr auto infoMdName = ".info"sv; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 63 | static constexpr auto libDir = "lib"sv; |
| 64 | static constexpr auto libSuffix = ".so"sv; |
| 65 | static constexpr auto blockSize = 4096; |
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 | } |
| 174 | } // namespace |
| 175 | |
| 176 | IncrementalService::IncFsMount::~IncFsMount() { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 177 | if (dataLoaderStub) { |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 178 | dataLoaderStub->cleanupResources(); |
| 179 | dataLoaderStub = {}; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 180 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 181 | control.close(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 182 | LOG(INFO) << "Unmounting and cleaning up mount " << mountId << " with root '" << root << '\''; |
| 183 | for (auto&& [target, _] : bindPoints) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 184 | LOG(INFO) << " bind: " << target; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 185 | incrementalService.mVold->unmountIncFs(target); |
| 186 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 187 | LOG(INFO) << " root: " << root; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 188 | incrementalService.mVold->unmountIncFs(path::join(root, constants().mount)); |
| 189 | cleanupFilesystem(root); |
| 190 | } |
| 191 | |
| 192 | auto IncrementalService::IncFsMount::makeStorage(StorageId id) -> StorageMap::iterator { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 193 | std::string name; |
| 194 | for (int no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), i = 0; |
| 195 | i < 1024 && no >= 0; no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), ++i) { |
| 196 | name.clear(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 197 | base::StringAppendF(&name, "%.*s_%d_%d", int(constants().storagePrefix.size()), |
| 198 | constants().storagePrefix.data(), id, no); |
| 199 | auto fullName = path::join(root, constants().mount, name); |
Songchun Fan | 9610093 | 2020-02-03 19:20:58 -0800 | [diff] [blame] | 200 | if (auto err = incrementalService.mIncFs->makeDir(control, fullName, 0755); !err) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 201 | std::lock_guard l(lock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 202 | return storages.insert_or_assign(id, Storage{std::move(fullName)}).first; |
| 203 | } else if (err != EEXIST) { |
| 204 | LOG(ERROR) << __func__ << "(): failed to create dir |" << fullName << "| " << err; |
| 205 | break; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | nextStorageDirNo = 0; |
| 209 | return storages.end(); |
| 210 | } |
| 211 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 212 | template <class Func> |
| 213 | static auto makeCleanup(Func&& f) { |
| 214 | auto deleter = [f = std::move(f)](auto) { f(); }; |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 215 | // &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] | 216 | return std::unique_ptr<Func, decltype(deleter)>(&f, std::move(deleter)); |
| 217 | } |
| 218 | |
| 219 | static std::unique_ptr<DIR, decltype(&::closedir)> openDir(const char* dir) { |
| 220 | return {::opendir(dir), ::closedir}; |
| 221 | } |
| 222 | |
| 223 | static auto openDir(std::string_view dir) { |
| 224 | return openDir(path::c_str(dir)); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int rmDirContent(const char* path) { |
| 228 | auto dir = openDir(path); |
| 229 | if (!dir) { |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | while (auto entry = ::readdir(dir.get())) { |
| 233 | if (entry->d_name == "."sv || entry->d_name == ".."sv) { |
| 234 | continue; |
| 235 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 236 | auto fullPath = base::StringPrintf("%s/%s", path, entry->d_name); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 237 | if (entry->d_type == DT_DIR) { |
| 238 | if (const auto err = rmDirContent(fullPath.c_str()); err != 0) { |
| 239 | PLOG(WARNING) << "Failed to delete " << fullPath << " content"; |
| 240 | return err; |
| 241 | } |
| 242 | if (const auto err = ::rmdir(fullPath.c_str()); err != 0) { |
| 243 | PLOG(WARNING) << "Failed to rmdir " << fullPath; |
| 244 | return err; |
| 245 | } |
| 246 | } else { |
| 247 | if (const auto err = ::unlink(fullPath.c_str()); err != 0) { |
| 248 | PLOG(WARNING) << "Failed to delete " << fullPath; |
| 249 | return err; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | return 0; |
| 254 | } |
| 255 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 256 | void IncrementalService::IncFsMount::cleanupFilesystem(std::string_view root) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 257 | rmDirContent(path::join(root, constants().backing).c_str()); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 258 | ::rmdir(path::join(root, constants().backing).c_str()); |
| 259 | ::rmdir(path::join(root, constants().mount).c_str()); |
| 260 | ::rmdir(path::c_str(root)); |
| 261 | } |
| 262 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 263 | IncrementalService::IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir) |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 264 | : mVold(sm.getVoldService()), |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 265 | mDataLoaderManager(sm.getDataLoaderManager()), |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 266 | mIncFs(sm.getIncFs()), |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 267 | mAppOpsManager(sm.getAppOpsManager()), |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 268 | mJni(sm.getJni()), |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 269 | mIncrementalDir(rootDir) { |
| 270 | if (!mVold) { |
| 271 | LOG(FATAL) << "Vold service is unavailable"; |
| 272 | } |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 273 | if (!mDataLoaderManager) { |
| 274 | LOG(FATAL) << "DataLoaderManagerService is unavailable"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 275 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 276 | if (!mAppOpsManager) { |
| 277 | LOG(FATAL) << "AppOpsManager is unavailable"; |
| 278 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 279 | |
| 280 | mJobQueue.reserve(16); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 281 | mJobProcessor = std::thread([this]() { |
| 282 | mJni->initializeForCurrentThread(); |
| 283 | runJobProcessing(); |
| 284 | }); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 285 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 286 | const auto mountedRootNames = adoptMountedInstances(); |
| 287 | mountExistingImages(mountedRootNames); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 290 | IncrementalService::~IncrementalService() { |
| 291 | { |
| 292 | std::lock_guard lock(mJobMutex); |
| 293 | mRunning = false; |
| 294 | } |
| 295 | mJobCondition.notify_all(); |
| 296 | mJobProcessor.join(); |
| 297 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 298 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 299 | static const char* toString(IncrementalService::BindKind kind) { |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 300 | switch (kind) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 301 | case IncrementalService::BindKind::Temporary: |
| 302 | return "Temporary"; |
| 303 | case IncrementalService::BindKind::Permanent: |
| 304 | return "Permanent"; |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | void IncrementalService::onDump(int fd) { |
| 309 | dprintf(fd, "Incremental is %s\n", incfs::enabled() ? "ENABLED" : "DISABLED"); |
| 310 | dprintf(fd, "Incremental dir: %s\n", mIncrementalDir.c_str()); |
| 311 | |
| 312 | std::unique_lock l(mLock); |
| 313 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 314 | dprintf(fd, "Mounts (%d): {\n", int(mMounts.size())); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 315 | for (auto&& [id, ifs] : mMounts) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 316 | const IncFsMount& mnt = *ifs; |
| 317 | dprintf(fd, " [%d]: {\n", id); |
| 318 | if (id != mnt.mountId) { |
| 319 | dprintf(fd, " reference to mountId: %d\n", mnt.mountId); |
| 320 | } else { |
| 321 | dprintf(fd, " mountId: %d\n", mnt.mountId); |
| 322 | dprintf(fd, " root: %s\n", mnt.root.c_str()); |
| 323 | dprintf(fd, " nextStorageDirNo: %d\n", mnt.nextStorageDirNo.load()); |
| 324 | if (mnt.dataLoaderStub) { |
| 325 | mnt.dataLoaderStub->onDump(fd); |
| 326 | } else { |
| 327 | dprintf(fd, " dataLoader: null\n"); |
| 328 | } |
| 329 | dprintf(fd, " storages (%d): {\n", int(mnt.storages.size())); |
| 330 | for (auto&& [storageId, storage] : mnt.storages) { |
| 331 | dprintf(fd, " [%d] -> [%s]\n", storageId, storage.name.c_str()); |
| 332 | } |
| 333 | dprintf(fd, " }\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 334 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 335 | dprintf(fd, " bindPoints (%d): {\n", int(mnt.bindPoints.size())); |
| 336 | for (auto&& [target, bind] : mnt.bindPoints) { |
| 337 | dprintf(fd, " [%s]->[%d]:\n", target.c_str(), bind.storage); |
| 338 | dprintf(fd, " savedFilename: %s\n", bind.savedFilename.c_str()); |
| 339 | dprintf(fd, " sourceDir: %s\n", bind.sourceDir.c_str()); |
| 340 | dprintf(fd, " kind: %s\n", toString(bind.kind)); |
| 341 | } |
| 342 | dprintf(fd, " }\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 343 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 344 | dprintf(fd, " }\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 345 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 346 | dprintf(fd, "}\n"); |
| 347 | dprintf(fd, "Sorted binds (%d): {\n", int(mBindsByPath.size())); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 348 | for (auto&& [target, mountPairIt] : mBindsByPath) { |
| 349 | const auto& bind = mountPairIt->second; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 350 | dprintf(fd, " [%s]->[%d]:\n", target.c_str(), bind.storage); |
| 351 | dprintf(fd, " savedFilename: %s\n", bind.savedFilename.c_str()); |
| 352 | dprintf(fd, " sourceDir: %s\n", bind.sourceDir.c_str()); |
| 353 | dprintf(fd, " kind: %s\n", toString(bind.kind)); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 354 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 355 | dprintf(fd, "}\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 358 | void IncrementalService::onSystemReady() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 359 | if (mSystemReady.exchange(true)) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 360 | return; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | std::vector<IfsMountPtr> mounts; |
| 364 | { |
| 365 | std::lock_guard l(mLock); |
| 366 | mounts.reserve(mMounts.size()); |
| 367 | for (auto&& [id, ifs] : mMounts) { |
| 368 | if (ifs->mountId == id) { |
| 369 | mounts.push_back(ifs); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame] | 374 | if (mounts.empty()) { |
| 375 | return; |
| 376 | } |
| 377 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 378 | std::thread([this, mounts = std::move(mounts)]() { |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame] | 379 | mJni->initializeForCurrentThread(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 380 | for (auto&& ifs : mounts) { |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 381 | ifs->dataLoaderStub->requestStart(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 382 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 383 | }).detach(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator { |
| 387 | for (;;) { |
| 388 | if (mNextId == kMaxStorageId) { |
| 389 | mNextId = 0; |
| 390 | } |
| 391 | auto id = ++mNextId; |
| 392 | auto [it, inserted] = mMounts.try_emplace(id, nullptr); |
| 393 | if (inserted) { |
| 394 | return it; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 399 | StorageId IncrementalService::createStorage( |
| 400 | std::string_view mountPoint, DataLoaderParamsParcel&& dataLoaderParams, |
| 401 | const DataLoaderStatusListener& dataLoaderStatusListener, CreateOptions options) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 402 | LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options); |
| 403 | if (!path::isAbsolute(mountPoint)) { |
| 404 | LOG(ERROR) << "path is not absolute: " << mountPoint; |
| 405 | return kInvalidStorageId; |
| 406 | } |
| 407 | |
| 408 | auto mountNorm = path::normalize(mountPoint); |
| 409 | { |
| 410 | const auto id = findStorageId(mountNorm); |
| 411 | if (id != kInvalidStorageId) { |
| 412 | if (options & CreateOptions::OpenExisting) { |
| 413 | LOG(INFO) << "Opened existing storage " << id; |
| 414 | return id; |
| 415 | } |
| 416 | LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id; |
| 417 | return kInvalidStorageId; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (!(options & CreateOptions::CreateNew)) { |
| 422 | LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint; |
| 423 | return kInvalidStorageId; |
| 424 | } |
| 425 | |
| 426 | if (!path::isEmptyDir(mountNorm)) { |
| 427 | LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm; |
| 428 | return kInvalidStorageId; |
| 429 | } |
| 430 | auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm); |
| 431 | if (mountRoot.empty()) { |
| 432 | LOG(ERROR) << "Bad mount point"; |
| 433 | return kInvalidStorageId; |
| 434 | } |
| 435 | // Make sure the code removes all crap it may create while still failing. |
| 436 | auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); }; |
| 437 | auto firstCleanupOnFailure = |
| 438 | std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup); |
| 439 | |
| 440 | auto mountTarget = path::join(mountRoot, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 441 | const auto backing = path::join(mountRoot, constants().backing); |
| 442 | if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 443 | return kInvalidStorageId; |
| 444 | } |
| 445 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 446 | IncFsMount::Control control; |
| 447 | { |
| 448 | std::lock_guard l(mMountOperationLock); |
| 449 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 450 | |
| 451 | if (auto err = rmDirContent(backing.c_str())) { |
| 452 | LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err; |
| 453 | return kInvalidStorageId; |
| 454 | } |
| 455 | if (!mkdirOrLog(path::join(backing, ".index"), 0777)) { |
| 456 | return kInvalidStorageId; |
| 457 | } |
| 458 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 459 | if (!status.isOk()) { |
| 460 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 461 | return kInvalidStorageId; |
| 462 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 463 | if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 || |
| 464 | controlParcel.log.get() < 0) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 465 | LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel."; |
| 466 | return kInvalidStorageId; |
| 467 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 468 | int cmd = controlParcel.cmd.release().release(); |
| 469 | int pendingReads = controlParcel.pendingReads.release().release(); |
| 470 | int logs = controlParcel.log.release().release(); |
| 471 | control = mIncFs->createControl(cmd, pendingReads, logs); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | std::unique_lock l(mLock); |
| 475 | const auto mountIt = getStorageSlotLocked(); |
| 476 | const auto mountId = mountIt->first; |
| 477 | l.unlock(); |
| 478 | |
| 479 | auto ifs = |
| 480 | std::make_shared<IncFsMount>(std::move(mountRoot), mountId, std::move(control), *this); |
| 481 | // Now it's the |ifs|'s responsibility to clean up after itself, and the only cleanup we need |
| 482 | // is the removal of the |ifs|. |
| 483 | firstCleanupOnFailure.release(); |
| 484 | |
| 485 | auto secondCleanup = [this, &l](auto itPtr) { |
| 486 | if (!l.owns_lock()) { |
| 487 | l.lock(); |
| 488 | } |
| 489 | mMounts.erase(*itPtr); |
| 490 | }; |
| 491 | auto secondCleanupOnFailure = |
| 492 | std::unique_ptr<decltype(mountIt), decltype(secondCleanup)>(&mountIt, secondCleanup); |
| 493 | |
| 494 | const auto storageIt = ifs->makeStorage(ifs->mountId); |
| 495 | if (storageIt == ifs->storages.end()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 496 | LOG(ERROR) << "Can't create a default storage directory"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 497 | return kInvalidStorageId; |
| 498 | } |
| 499 | |
| 500 | { |
| 501 | metadata::Mount m; |
| 502 | m.mutable_storage()->set_id(ifs->mountId); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 503 | m.mutable_loader()->set_type((int)dataLoaderParams.type); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 504 | m.mutable_loader()->set_allocated_package_name(&dataLoaderParams.packageName); |
| 505 | m.mutable_loader()->set_allocated_class_name(&dataLoaderParams.className); |
| 506 | m.mutable_loader()->set_allocated_arguments(&dataLoaderParams.arguments); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 507 | const auto metadata = m.SerializeAsString(); |
| 508 | m.mutable_loader()->release_arguments(); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 509 | m.mutable_loader()->release_class_name(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 510 | m.mutable_loader()->release_package_name(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 511 | if (auto err = |
| 512 | mIncFs->makeFile(ifs->control, |
| 513 | path::join(ifs->root, constants().mount, |
| 514 | constants().infoMdName), |
| 515 | 0777, idFromMetadata(metadata), |
| 516 | {.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 517 | LOG(ERROR) << "Saving mount metadata failed: " << -err; |
| 518 | return kInvalidStorageId; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | const auto bk = |
| 523 | (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 524 | if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name, |
| 525 | std::string(storageIt->second.name), std::move(mountNorm), bk, l); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 526 | err < 0) { |
| 527 | LOG(ERROR) << "adding bind mount failed: " << -err; |
| 528 | return kInvalidStorageId; |
| 529 | } |
| 530 | |
| 531 | // Done here as well, all data structures are in good state. |
| 532 | secondCleanupOnFailure.release(); |
| 533 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 534 | auto dataLoaderStub = |
| 535 | prepareDataLoader(*ifs, std::move(dataLoaderParams), &dataLoaderStatusListener); |
| 536 | CHECK(dataLoaderStub); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 537 | |
| 538 | mountIt->second = std::move(ifs); |
| 539 | l.unlock(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 540 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 541 | if (mSystemReady.load(std::memory_order_relaxed) && !dataLoaderStub->requestCreate()) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 542 | // failed to create data loader |
| 543 | LOG(ERROR) << "initializeDataLoader() failed"; |
| 544 | deleteStorage(dataLoaderStub->id()); |
| 545 | return kInvalidStorageId; |
| 546 | } |
| 547 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 548 | LOG(INFO) << "created storage " << mountId; |
| 549 | return mountId; |
| 550 | } |
| 551 | |
| 552 | StorageId IncrementalService::createLinkedStorage(std::string_view mountPoint, |
| 553 | StorageId linkedStorage, |
| 554 | IncrementalService::CreateOptions options) { |
| 555 | if (!isValidMountTarget(mountPoint)) { |
| 556 | LOG(ERROR) << "Mount point is invalid or missing"; |
| 557 | return kInvalidStorageId; |
| 558 | } |
| 559 | |
| 560 | std::unique_lock l(mLock); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 561 | auto ifs = getIfsLocked(linkedStorage); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 562 | if (!ifs) { |
| 563 | LOG(ERROR) << "Ifs unavailable"; |
| 564 | return kInvalidStorageId; |
| 565 | } |
| 566 | |
| 567 | const auto mountIt = getStorageSlotLocked(); |
| 568 | const auto storageId = mountIt->first; |
| 569 | const auto storageIt = ifs->makeStorage(storageId); |
| 570 | if (storageIt == ifs->storages.end()) { |
| 571 | LOG(ERROR) << "Can't create a new storage"; |
| 572 | mMounts.erase(mountIt); |
| 573 | return kInvalidStorageId; |
| 574 | } |
| 575 | |
| 576 | l.unlock(); |
| 577 | |
| 578 | const auto bk = |
| 579 | (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 580 | if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name, |
| 581 | std::string(storageIt->second.name), path::normalize(mountPoint), |
| 582 | bk, l); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 583 | err < 0) { |
| 584 | LOG(ERROR) << "bindMount failed with error: " << err; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 585 | (void)mIncFs->unlink(ifs->control, storageIt->second.name); |
| 586 | ifs->storages.erase(storageIt); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 587 | return kInvalidStorageId; |
| 588 | } |
| 589 | |
| 590 | mountIt->second = ifs; |
| 591 | return storageId; |
| 592 | } |
| 593 | |
| 594 | IncrementalService::BindPathMap::const_iterator IncrementalService::findStorageLocked( |
| 595 | std::string_view path) const { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 596 | return findParentPath(mBindsByPath, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | StorageId IncrementalService::findStorageId(std::string_view path) const { |
| 600 | std::lock_guard l(mLock); |
| 601 | auto it = findStorageLocked(path); |
| 602 | if (it == mBindsByPath.end()) { |
| 603 | return kInvalidStorageId; |
| 604 | } |
| 605 | return it->second->second.storage; |
| 606 | } |
| 607 | |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 608 | int IncrementalService::setStorageParams(StorageId storageId, bool enableReadLogs) { |
| 609 | const auto ifs = getIfs(storageId); |
| 610 | if (!ifs) { |
Alex Buynytskyy | 5f9e3a0 | 2020-04-07 21:13:41 -0700 | [diff] [blame] | 611 | LOG(ERROR) << "setStorageParams failed, invalid storageId: " << storageId; |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 612 | return -EINVAL; |
| 613 | } |
| 614 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 615 | const auto& params = ifs->dataLoaderStub->params(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 616 | if (enableReadLogs) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 617 | if (auto status = mAppOpsManager->checkPermission(kDataUsageStats, kOpUsage, |
| 618 | params.packageName.c_str()); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 619 | !status.isOk()) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 620 | LOG(ERROR) << "checkPermission failed: " << status.toString8(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 621 | return fromBinderStatus(status); |
| 622 | } |
| 623 | } |
| 624 | |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 625 | if (auto status = applyStorageParams(*ifs, enableReadLogs); !status.isOk()) { |
| 626 | LOG(ERROR) << "applyStorageParams failed: " << status.toString8(); |
| 627 | return fromBinderStatus(status); |
| 628 | } |
| 629 | |
| 630 | if (enableReadLogs) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 631 | registerAppOpsCallback(params.packageName); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | binder::Status IncrementalService::applyStorageParams(IncFsMount& ifs, bool enableReadLogs) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 638 | os::incremental::IncrementalFileSystemControlParcel control; |
| 639 | control.cmd.reset(dup(ifs.control.cmd())); |
| 640 | control.pendingReads.reset(dup(ifs.control.pendingReads())); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 641 | auto logsFd = ifs.control.logs(); |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 642 | if (logsFd >= 0) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 643 | control.log.reset(dup(logsFd)); |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | std::lock_guard l(mMountOperationLock); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 647 | return mVold->setIncFsMountOptions(control, enableReadLogs); |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 650 | void IncrementalService::deleteStorage(StorageId storageId) { |
| 651 | const auto ifs = getIfs(storageId); |
| 652 | if (!ifs) { |
| 653 | return; |
| 654 | } |
| 655 | deleteStorage(*ifs); |
| 656 | } |
| 657 | |
| 658 | void IncrementalService::deleteStorage(IncrementalService::IncFsMount& ifs) { |
| 659 | std::unique_lock l(ifs.lock); |
| 660 | deleteStorageLocked(ifs, std::move(l)); |
| 661 | } |
| 662 | |
| 663 | void IncrementalService::deleteStorageLocked(IncrementalService::IncFsMount& ifs, |
| 664 | std::unique_lock<std::mutex>&& ifsLock) { |
| 665 | const auto storages = std::move(ifs.storages); |
| 666 | // Don't move the bind points out: Ifs's dtor will use them to unmount everything. |
| 667 | const auto bindPoints = ifs.bindPoints; |
| 668 | ifsLock.unlock(); |
| 669 | |
| 670 | std::lock_guard l(mLock); |
| 671 | for (auto&& [id, _] : storages) { |
| 672 | if (id != ifs.mountId) { |
| 673 | mMounts.erase(id); |
| 674 | } |
| 675 | } |
| 676 | for (auto&& [path, _] : bindPoints) { |
| 677 | mBindsByPath.erase(path); |
| 678 | } |
| 679 | mMounts.erase(ifs.mountId); |
| 680 | } |
| 681 | |
| 682 | StorageId IncrementalService::openStorage(std::string_view pathInMount) { |
| 683 | if (!path::isAbsolute(pathInMount)) { |
| 684 | return kInvalidStorageId; |
| 685 | } |
| 686 | |
| 687 | return findStorageId(path::normalize(pathInMount)); |
| 688 | } |
| 689 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 690 | IncrementalService::IfsMountPtr IncrementalService::getIfs(StorageId storage) const { |
| 691 | std::lock_guard l(mLock); |
| 692 | return getIfsLocked(storage); |
| 693 | } |
| 694 | |
| 695 | const IncrementalService::IfsMountPtr& IncrementalService::getIfsLocked(StorageId storage) const { |
| 696 | auto it = mMounts.find(storage); |
| 697 | if (it == mMounts.end()) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 698 | static const base::NoDestructor<IfsMountPtr> kEmpty{}; |
Yurii Zubrytskyi | 0cd8012 | 2020-04-09 23:08:31 -0700 | [diff] [blame] | 699 | return *kEmpty; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 700 | } |
| 701 | return it->second; |
| 702 | } |
| 703 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 704 | int IncrementalService::bind(StorageId storage, std::string_view source, std::string_view target, |
| 705 | BindKind kind) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 706 | if (!isValidMountTarget(target)) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 707 | LOG(ERROR) << __func__ << ": not a valid bind target " << target; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 708 | return -EINVAL; |
| 709 | } |
| 710 | |
| 711 | const auto ifs = getIfs(storage); |
| 712 | if (!ifs) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 713 | LOG(ERROR) << __func__ << ": no ifs object for storage " << storage; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 714 | return -EINVAL; |
| 715 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 716 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 717 | std::unique_lock l(ifs->lock); |
| 718 | const auto storageInfo = ifs->storages.find(storage); |
| 719 | if (storageInfo == ifs->storages.end()) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 720 | LOG(ERROR) << "no storage"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 721 | return -EINVAL; |
| 722 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 723 | std::string normSource = normalizePathToStorageLocked(*ifs, storageInfo, source); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 724 | if (normSource.empty()) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 725 | LOG(ERROR) << "invalid source path"; |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 726 | return -EINVAL; |
| 727 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 728 | l.unlock(); |
| 729 | std::unique_lock l2(mLock, std::defer_lock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 730 | return addBindMount(*ifs, storage, storageInfo->second.name, std::move(normSource), |
| 731 | path::normalize(target), kind, l2); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | int IncrementalService::unbind(StorageId storage, std::string_view target) { |
| 735 | if (!path::isAbsolute(target)) { |
| 736 | return -EINVAL; |
| 737 | } |
| 738 | |
| 739 | LOG(INFO) << "Removing bind point " << target; |
| 740 | |
| 741 | // Here we should only look up by the exact target, not by a subdirectory of any existing mount, |
| 742 | // otherwise there's a chance to unmount something completely unrelated |
| 743 | const auto norm = path::normalize(target); |
| 744 | std::unique_lock l(mLock); |
| 745 | const auto storageIt = mBindsByPath.find(norm); |
| 746 | if (storageIt == mBindsByPath.end() || storageIt->second->second.storage != storage) { |
| 747 | return -EINVAL; |
| 748 | } |
| 749 | const auto bindIt = storageIt->second; |
| 750 | const auto storageId = bindIt->second.storage; |
| 751 | const auto ifs = getIfsLocked(storageId); |
| 752 | if (!ifs) { |
| 753 | LOG(ERROR) << "Internal error: storageId " << storageId << " for bound path " << target |
| 754 | << " is missing"; |
| 755 | return -EFAULT; |
| 756 | } |
| 757 | mBindsByPath.erase(storageIt); |
| 758 | l.unlock(); |
| 759 | |
| 760 | mVold->unmountIncFs(bindIt->first); |
| 761 | std::unique_lock l2(ifs->lock); |
| 762 | if (ifs->bindPoints.size() <= 1) { |
| 763 | ifs->bindPoints.clear(); |
| 764 | deleteStorageLocked(*ifs, std::move(l2)); |
| 765 | } else { |
| 766 | const std::string savedFile = std::move(bindIt->second.savedFilename); |
| 767 | ifs->bindPoints.erase(bindIt); |
| 768 | l2.unlock(); |
| 769 | if (!savedFile.empty()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 770 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, savedFile)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | return 0; |
| 774 | } |
| 775 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 776 | std::string IncrementalService::normalizePathToStorageLocked( |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 777 | const IncFsMount& incfs, IncFsMount::StorageMap::const_iterator storageIt, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 778 | std::string_view path) const { |
| 779 | if (!path::isAbsolute(path)) { |
| 780 | return path::normalize(path::join(storageIt->second.name, path)); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 781 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 782 | auto normPath = path::normalize(path); |
| 783 | if (path::startsWith(normPath, storageIt->second.name)) { |
| 784 | return normPath; |
| 785 | } |
| 786 | // 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] | 787 | const auto bindIt = findParentPath(incfs.bindPoints, normPath); |
| 788 | if (bindIt == incfs.bindPoints.end()) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 789 | return {}; |
| 790 | } |
| 791 | return path::join(bindIt->second.sourceDir, path::relativize(bindIt->first, normPath)); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 794 | std::string IncrementalService::normalizePathToStorage(const IncFsMount& ifs, StorageId storage, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 795 | std::string_view path) const { |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 796 | std::unique_lock l(ifs.lock); |
| 797 | const auto storageInfo = ifs.storages.find(storage); |
| 798 | if (storageInfo == ifs.storages.end()) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 799 | return {}; |
| 800 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 801 | return normalizePathToStorageLocked(ifs, storageInfo, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 804 | int IncrementalService::makeFile(StorageId storage, std::string_view path, int mode, FileId id, |
| 805 | incfs::NewFileParams params) { |
| 806 | if (auto ifs = getIfs(storage)) { |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 807 | std::string normPath = normalizePathToStorage(*ifs, storage, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 808 | if (normPath.empty()) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 809 | LOG(ERROR) << "Internal error: storageId " << storage |
| 810 | << " failed to normalize: " << path; |
Songchun Fan | 54c6aed | 2020-01-31 16:52:41 -0800 | [diff] [blame] | 811 | return -EINVAL; |
| 812 | } |
| 813 | auto err = mIncFs->makeFile(ifs->control, normPath, mode, id, params); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 814 | if (err) { |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 815 | LOG(ERROR) << "Internal error: storageId " << storage << " failed to makeFile: " << err; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 816 | return err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 817 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 818 | return 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 819 | } |
| 820 | return -EINVAL; |
| 821 | } |
| 822 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 823 | int IncrementalService::makeDir(StorageId storageId, std::string_view path, int mode) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 824 | if (auto ifs = getIfs(storageId)) { |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 825 | std::string normPath = normalizePathToStorage(*ifs, storageId, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 826 | if (normPath.empty()) { |
| 827 | return -EINVAL; |
| 828 | } |
| 829 | return mIncFs->makeDir(ifs->control, normPath, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 830 | } |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 834 | int IncrementalService::makeDirs(StorageId storageId, std::string_view path, int mode) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 835 | const auto ifs = getIfs(storageId); |
| 836 | if (!ifs) { |
| 837 | return -EINVAL; |
| 838 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 839 | return makeDirs(*ifs, storageId, path, mode); |
| 840 | } |
| 841 | |
| 842 | int IncrementalService::makeDirs(const IncFsMount& ifs, StorageId storageId, std::string_view path, |
| 843 | int mode) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 844 | std::string normPath = normalizePathToStorage(ifs, storageId, path); |
| 845 | if (normPath.empty()) { |
| 846 | return -EINVAL; |
| 847 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 848 | return mIncFs->makeDirs(ifs.control, normPath, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 849 | } |
| 850 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 851 | int IncrementalService::link(StorageId sourceStorageId, std::string_view oldPath, |
| 852 | StorageId destStorageId, std::string_view newPath) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 853 | std::unique_lock l(mLock); |
| 854 | auto ifsSrc = getIfsLocked(sourceStorageId); |
| 855 | if (!ifsSrc) { |
| 856 | return -EINVAL; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 857 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 858 | if (sourceStorageId != destStorageId && getIfsLocked(destStorageId) != ifsSrc) { |
| 859 | return -EINVAL; |
| 860 | } |
| 861 | l.unlock(); |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 862 | std::string normOldPath = normalizePathToStorage(*ifsSrc, sourceStorageId, oldPath); |
| 863 | std::string normNewPath = normalizePathToStorage(*ifsSrc, destStorageId, newPath); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 864 | if (normOldPath.empty() || normNewPath.empty()) { |
| 865 | LOG(ERROR) << "Invalid paths in link(): " << normOldPath << " | " << normNewPath; |
| 866 | return -EINVAL; |
| 867 | } |
| 868 | return mIncFs->link(ifsSrc->control, normOldPath, normNewPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 871 | int IncrementalService::unlink(StorageId storage, std::string_view path) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 872 | if (auto ifs = getIfs(storage)) { |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 873 | std::string normOldPath = normalizePathToStorage(*ifs, storage, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 874 | return mIncFs->unlink(ifs->control, normOldPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 875 | } |
| 876 | return -EINVAL; |
| 877 | } |
| 878 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 879 | int IncrementalService::addBindMount(IncFsMount& ifs, StorageId storage, |
| 880 | std::string_view storageRoot, std::string&& source, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 881 | std::string&& target, BindKind kind, |
| 882 | std::unique_lock<std::mutex>& mainLock) { |
| 883 | if (!isValidMountTarget(target)) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 884 | LOG(ERROR) << __func__ << ": invalid mount target " << target; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 885 | return -EINVAL; |
| 886 | } |
| 887 | |
| 888 | std::string mdFileName; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 889 | std::string metadataFullPath; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 890 | if (kind != BindKind::Temporary) { |
| 891 | metadata::BindPoint bp; |
| 892 | bp.set_storage_id(storage); |
| 893 | bp.set_allocated_dest_path(&target); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 894 | bp.set_allocated_source_subdir(&source); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 895 | const auto metadata = bp.SerializeAsString(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 896 | bp.release_dest_path(); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 897 | bp.release_source_subdir(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 898 | mdFileName = makeBindMdName(); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 899 | metadataFullPath = path::join(ifs.root, constants().mount, mdFileName); |
| 900 | auto node = mIncFs->makeFile(ifs.control, metadataFullPath, 0444, idFromMetadata(metadata), |
| 901 | {.metadata = {metadata.data(), (IncFsSize)metadata.size()}}); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 902 | if (node) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 903 | LOG(ERROR) << __func__ << ": couldn't create a mount node " << mdFileName; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 904 | return int(node); |
| 905 | } |
| 906 | } |
| 907 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 908 | const auto res = addBindMountWithMd(ifs, storage, std::move(mdFileName), std::move(source), |
| 909 | std::move(target), kind, mainLock); |
| 910 | if (res) { |
| 911 | mIncFs->unlink(ifs.control, metadataFullPath); |
| 912 | } |
| 913 | return res; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | int IncrementalService::addBindMountWithMd(IncrementalService::IncFsMount& ifs, StorageId storage, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 917 | std::string&& metadataName, std::string&& source, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 918 | std::string&& target, BindKind kind, |
| 919 | std::unique_lock<std::mutex>& mainLock) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 920 | { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 921 | std::lock_guard l(mMountOperationLock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 922 | const auto status = mVold->bindMount(source, target); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 923 | if (!status.isOk()) { |
| 924 | LOG(ERROR) << "Calling Vold::bindMount() failed: " << status.toString8(); |
| 925 | return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC |
| 926 | ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode() |
| 927 | : status.serviceSpecificErrorCode() == 0 |
| 928 | ? -EFAULT |
| 929 | : status.serviceSpecificErrorCode() |
| 930 | : -EIO; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | if (!mainLock.owns_lock()) { |
| 935 | mainLock.lock(); |
| 936 | } |
| 937 | std::lock_guard l(ifs.lock); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 938 | addBindMountRecordLocked(ifs, storage, std::move(metadataName), std::move(source), |
| 939 | std::move(target), kind); |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | void IncrementalService::addBindMountRecordLocked(IncFsMount& ifs, StorageId storage, |
| 944 | std::string&& metadataName, std::string&& source, |
| 945 | std::string&& target, BindKind kind) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 946 | const auto [it, _] = |
| 947 | ifs.bindPoints.insert_or_assign(target, |
| 948 | IncFsMount::Bind{storage, std::move(metadataName), |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 949 | std::move(source), kind}); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 950 | mBindsByPath[std::move(target)] = it; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | RawMetadata IncrementalService::getMetadata(StorageId storage, std::string_view path) const { |
| 954 | const auto ifs = getIfs(storage); |
| 955 | if (!ifs) { |
| 956 | return {}; |
| 957 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 958 | const auto normPath = normalizePathToStorage(*ifs, storage, path); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 959 | if (normPath.empty()) { |
| 960 | return {}; |
| 961 | } |
| 962 | return mIncFs->getMetadata(ifs->control, normPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 963 | } |
| 964 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 965 | RawMetadata IncrementalService::getMetadata(StorageId storage, FileId node) const { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 966 | const auto ifs = getIfs(storage); |
| 967 | if (!ifs) { |
| 968 | return {}; |
| 969 | } |
| 970 | return mIncFs->getMetadata(ifs->control, node); |
| 971 | } |
| 972 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 973 | bool IncrementalService::startLoading(StorageId storage) const { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 974 | DataLoaderStubPtr dataLoaderStub; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 975 | { |
| 976 | std::unique_lock l(mLock); |
| 977 | const auto& ifs = getIfsLocked(storage); |
| 978 | if (!ifs) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 979 | return false; |
| 980 | } |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 981 | dataLoaderStub = ifs->dataLoaderStub; |
| 982 | if (!dataLoaderStub) { |
| 983 | return false; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 984 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 985 | } |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 986 | dataLoaderStub->requestStart(); |
| 987 | return true; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 990 | std::unordered_set<std::string_view> IncrementalService::adoptMountedInstances() { |
| 991 | std::unordered_set<std::string_view> mountedRootNames; |
| 992 | mIncFs->listExistingMounts([this, &mountedRootNames](auto root, auto backingDir, auto binds) { |
| 993 | LOG(INFO) << "Existing mount: " << backingDir << "->" << root; |
| 994 | for (auto [source, target] : binds) { |
| 995 | LOG(INFO) << " bind: '" << source << "'->'" << target << "'"; |
| 996 | LOG(INFO) << " " << path::join(root, source); |
| 997 | } |
| 998 | |
| 999 | // Ensure it's a kind of a mount that's managed by IncrementalService |
| 1000 | if (path::basename(root) != constants().mount || |
| 1001 | path::basename(backingDir) != constants().backing) { |
| 1002 | return; |
| 1003 | } |
| 1004 | const auto expectedRoot = path::dirname(root); |
| 1005 | if (path::dirname(backingDir) != expectedRoot) { |
| 1006 | return; |
| 1007 | } |
| 1008 | if (path::dirname(expectedRoot) != mIncrementalDir) { |
| 1009 | return; |
| 1010 | } |
| 1011 | if (!path::basename(expectedRoot).starts_with(constants().mountKeyPrefix)) { |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | LOG(INFO) << "Looks like an IncrementalService-owned: " << expectedRoot; |
| 1016 | |
| 1017 | // make sure we clean up the mount if it happens to be a bad one. |
| 1018 | // Note: unmounting needs to run first, so the cleanup object is created _last_. |
| 1019 | auto cleanupFiles = makeCleanup([&]() { |
| 1020 | LOG(INFO) << "Failed to adopt existing mount, deleting files: " << expectedRoot; |
| 1021 | IncFsMount::cleanupFilesystem(expectedRoot); |
| 1022 | }); |
| 1023 | auto cleanupMounts = makeCleanup([&]() { |
| 1024 | LOG(INFO) << "Failed to adopt existing mount, cleaning up: " << expectedRoot; |
| 1025 | for (auto&& [_, target] : binds) { |
| 1026 | mVold->unmountIncFs(std::string(target)); |
| 1027 | } |
| 1028 | mVold->unmountIncFs(std::string(root)); |
| 1029 | }); |
| 1030 | |
| 1031 | auto control = mIncFs->openMount(root); |
| 1032 | if (!control) { |
| 1033 | LOG(INFO) << "failed to open mount " << root; |
| 1034 | return; |
| 1035 | } |
| 1036 | |
| 1037 | auto mountRecord = |
| 1038 | parseFromIncfs<metadata::Mount>(mIncFs.get(), control, |
| 1039 | path::join(root, constants().infoMdName)); |
| 1040 | if (!mountRecord.has_loader() || !mountRecord.has_storage()) { |
| 1041 | LOG(ERROR) << "Bad mount metadata in mount at " << expectedRoot; |
| 1042 | return; |
| 1043 | } |
| 1044 | |
| 1045 | auto mountId = mountRecord.storage().id(); |
| 1046 | mNextId = std::max(mNextId, mountId + 1); |
| 1047 | |
| 1048 | DataLoaderParamsParcel dataLoaderParams; |
| 1049 | { |
| 1050 | const auto& loader = mountRecord.loader(); |
| 1051 | dataLoaderParams.type = (content::pm::DataLoaderType)loader.type(); |
| 1052 | dataLoaderParams.packageName = loader.package_name(); |
| 1053 | dataLoaderParams.className = loader.class_name(); |
| 1054 | dataLoaderParams.arguments = loader.arguments(); |
| 1055 | } |
| 1056 | |
| 1057 | auto ifs = std::make_shared<IncFsMount>(std::string(expectedRoot), mountId, |
| 1058 | std::move(control), *this); |
| 1059 | cleanupFiles.release(); // ifs will take care of that now |
| 1060 | |
| 1061 | std::vector<std::pair<std::string, metadata::BindPoint>> permanentBindPoints; |
| 1062 | auto d = openDir(root); |
| 1063 | while (auto e = ::readdir(d.get())) { |
| 1064 | if (e->d_type == DT_REG) { |
| 1065 | auto name = std::string_view(e->d_name); |
| 1066 | if (name.starts_with(constants().mountpointMdPrefix)) { |
| 1067 | permanentBindPoints |
| 1068 | .emplace_back(name, |
| 1069 | parseFromIncfs<metadata::BindPoint>(mIncFs.get(), |
| 1070 | ifs->control, |
| 1071 | path::join(root, |
| 1072 | name))); |
| 1073 | if (permanentBindPoints.back().second.dest_path().empty() || |
| 1074 | permanentBindPoints.back().second.source_subdir().empty()) { |
| 1075 | permanentBindPoints.pop_back(); |
| 1076 | mIncFs->unlink(ifs->control, path::join(root, name)); |
| 1077 | } else { |
| 1078 | LOG(INFO) << "Permanent bind record: '" |
| 1079 | << permanentBindPoints.back().second.source_subdir() << "'->'" |
| 1080 | << permanentBindPoints.back().second.dest_path() << "'"; |
| 1081 | } |
| 1082 | } |
| 1083 | } else if (e->d_type == DT_DIR) { |
| 1084 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 1085 | continue; |
| 1086 | } |
| 1087 | auto name = std::string_view(e->d_name); |
| 1088 | if (name.starts_with(constants().storagePrefix)) { |
| 1089 | int storageId; |
| 1090 | const auto res = |
| 1091 | std::from_chars(name.data() + constants().storagePrefix.size() + 1, |
| 1092 | name.data() + name.size(), storageId); |
| 1093 | if (res.ec != std::errc{} || *res.ptr != '_') { |
| 1094 | LOG(WARNING) << "Ignoring storage with invalid name '" << name |
| 1095 | << "' for mount " << expectedRoot; |
| 1096 | continue; |
| 1097 | } |
| 1098 | auto [_, inserted] = mMounts.try_emplace(storageId, ifs); |
| 1099 | if (!inserted) { |
| 1100 | LOG(WARNING) << "Ignoring storage with duplicate id " << storageId |
| 1101 | << " for mount " << expectedRoot; |
| 1102 | continue; |
| 1103 | } |
| 1104 | ifs->storages.insert_or_assign(storageId, |
| 1105 | IncFsMount::Storage{path::join(root, name)}); |
| 1106 | mNextId = std::max(mNextId, storageId + 1); |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | if (ifs->storages.empty()) { |
| 1112 | LOG(WARNING) << "No valid storages in mount " << root; |
| 1113 | return; |
| 1114 | } |
| 1115 | |
| 1116 | // now match the mounted directories with what we expect to have in the metadata |
| 1117 | { |
| 1118 | std::unique_lock l(mLock, std::defer_lock); |
| 1119 | for (auto&& [metadataFile, bindRecord] : permanentBindPoints) { |
| 1120 | auto mountedIt = std::find_if(binds.begin(), binds.end(), |
| 1121 | [&, bindRecord = bindRecord](auto&& bind) { |
| 1122 | return bind.second == bindRecord.dest_path() && |
| 1123 | path::join(root, bind.first) == |
| 1124 | bindRecord.source_subdir(); |
| 1125 | }); |
| 1126 | if (mountedIt != binds.end()) { |
| 1127 | LOG(INFO) << "Matched permanent bound " << bindRecord.source_subdir() |
| 1128 | << " to mount " << mountedIt->first; |
| 1129 | addBindMountRecordLocked(*ifs, bindRecord.storage_id(), std::move(metadataFile), |
| 1130 | std::move(*bindRecord.mutable_source_subdir()), |
| 1131 | std::move(*bindRecord.mutable_dest_path()), |
| 1132 | BindKind::Permanent); |
| 1133 | if (mountedIt != binds.end() - 1) { |
| 1134 | std::iter_swap(mountedIt, binds.end() - 1); |
| 1135 | } |
| 1136 | binds = binds.first(binds.size() - 1); |
| 1137 | } else { |
| 1138 | LOG(INFO) << "Didn't match permanent bound " << bindRecord.source_subdir() |
| 1139 | << ", mounting"; |
| 1140 | // doesn't exist - try mounting back |
| 1141 | if (addBindMountWithMd(*ifs, bindRecord.storage_id(), std::move(metadataFile), |
| 1142 | std::move(*bindRecord.mutable_source_subdir()), |
| 1143 | std::move(*bindRecord.mutable_dest_path()), |
| 1144 | BindKind::Permanent, l)) { |
| 1145 | mIncFs->unlink(ifs->control, metadataFile); |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | // if anything stays in |binds| those are probably temporary binds; system restarted since |
| 1152 | // they were mounted - so let's unmount them all. |
| 1153 | for (auto&& [source, target] : binds) { |
| 1154 | if (source.empty()) { |
| 1155 | continue; |
| 1156 | } |
| 1157 | mVold->unmountIncFs(std::string(target)); |
| 1158 | } |
| 1159 | cleanupMounts.release(); // ifs now manages everything |
| 1160 | |
| 1161 | if (ifs->bindPoints.empty()) { |
| 1162 | LOG(WARNING) << "No valid bind points for mount " << expectedRoot; |
| 1163 | deleteStorage(*ifs); |
| 1164 | return; |
| 1165 | } |
| 1166 | |
| 1167 | prepareDataLoaderLocked(*ifs, std::move(dataLoaderParams)); |
| 1168 | CHECK(ifs->dataLoaderStub); |
| 1169 | |
| 1170 | mountedRootNames.insert(path::basename(ifs->root)); |
| 1171 | |
| 1172 | // not locking here at all: we're still in the constructor, no other calls can happen |
| 1173 | mMounts[ifs->mountId] = std::move(ifs); |
| 1174 | }); |
| 1175 | |
| 1176 | return mountedRootNames; |
| 1177 | } |
| 1178 | |
| 1179 | void IncrementalService::mountExistingImages( |
| 1180 | const std::unordered_set<std::string_view>& mountedRootNames) { |
| 1181 | auto dir = openDir(mIncrementalDir); |
| 1182 | if (!dir) { |
| 1183 | PLOG(WARNING) << "Couldn't open the root incremental dir " << mIncrementalDir; |
| 1184 | return; |
| 1185 | } |
| 1186 | while (auto entry = ::readdir(dir.get())) { |
| 1187 | if (entry->d_type != DT_DIR) { |
| 1188 | continue; |
| 1189 | } |
| 1190 | std::string_view name = entry->d_name; |
| 1191 | if (!name.starts_with(constants().mountKeyPrefix)) { |
| 1192 | continue; |
| 1193 | } |
| 1194 | if (mountedRootNames.find(name) != mountedRootNames.end()) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1195 | continue; |
| 1196 | } |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 1197 | const auto root = path::join(mIncrementalDir, name); |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1198 | if (!mountExistingImage(root)) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1199 | IncFsMount::cleanupFilesystem(root); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1204 | bool IncrementalService::mountExistingImage(std::string_view root) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1205 | auto mountTarget = path::join(root, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1206 | const auto backing = path::join(root, constants().backing); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1207 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1208 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1209 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1210 | if (!status.isOk()) { |
| 1211 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 1212 | return false; |
| 1213 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 1214 | |
| 1215 | int cmd = controlParcel.cmd.release().release(); |
| 1216 | int pendingReads = controlParcel.pendingReads.release().release(); |
| 1217 | int logs = controlParcel.log.release().release(); |
| 1218 | IncFsMount::Control control = mIncFs->createControl(cmd, pendingReads, logs); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1219 | |
| 1220 | auto ifs = std::make_shared<IncFsMount>(std::string(root), -1, std::move(control), *this); |
| 1221 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1222 | auto mount = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control, |
| 1223 | path::join(mountTarget, constants().infoMdName)); |
| 1224 | if (!mount.has_loader() || !mount.has_storage()) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1225 | LOG(ERROR) << "Bad mount metadata in mount at " << root; |
| 1226 | return false; |
| 1227 | } |
| 1228 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1229 | ifs->mountId = mount.storage().id(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1230 | mNextId = std::max(mNextId, ifs->mountId + 1); |
| 1231 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1232 | // DataLoader params |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1233 | DataLoaderParamsParcel dataLoaderParams; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1234 | { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1235 | const auto& loader = mount.loader(); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1236 | dataLoaderParams.type = (content::pm::DataLoaderType)loader.type(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1237 | dataLoaderParams.packageName = loader.package_name(); |
| 1238 | dataLoaderParams.className = loader.class_name(); |
| 1239 | dataLoaderParams.arguments = loader.arguments(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame] | 1242 | prepareDataLoader(*ifs, std::move(dataLoaderParams), nullptr); |
| 1243 | CHECK(ifs->dataLoaderStub); |
| 1244 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1245 | std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1246 | auto d = openDir(mountTarget); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1247 | while (auto e = ::readdir(d.get())) { |
| 1248 | if (e->d_type == DT_REG) { |
| 1249 | auto name = std::string_view(e->d_name); |
| 1250 | if (name.starts_with(constants().mountpointMdPrefix)) { |
| 1251 | bindPoints.emplace_back(name, |
| 1252 | parseFromIncfs<metadata::BindPoint>(mIncFs.get(), |
| 1253 | ifs->control, |
| 1254 | path::join(mountTarget, |
| 1255 | name))); |
| 1256 | if (bindPoints.back().second.dest_path().empty() || |
| 1257 | bindPoints.back().second.source_subdir().empty()) { |
| 1258 | bindPoints.pop_back(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1259 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | } else if (e->d_type == DT_DIR) { |
| 1263 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 1264 | continue; |
| 1265 | } |
| 1266 | auto name = std::string_view(e->d_name); |
| 1267 | if (name.starts_with(constants().storagePrefix)) { |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1268 | int storageId; |
| 1269 | const auto res = std::from_chars(name.data() + constants().storagePrefix.size() + 1, |
| 1270 | name.data() + name.size(), storageId); |
| 1271 | if (res.ec != std::errc{} || *res.ptr != '_') { |
| 1272 | LOG(WARNING) << "Ignoring storage with invalid name '" << name << "' for mount " |
| 1273 | << root; |
| 1274 | continue; |
| 1275 | } |
| 1276 | auto [_, inserted] = mMounts.try_emplace(storageId, ifs); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1277 | if (!inserted) { |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1278 | LOG(WARNING) << "Ignoring storage with duplicate id " << storageId |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1279 | << " for mount " << root; |
| 1280 | continue; |
| 1281 | } |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1282 | ifs->storages.insert_or_assign(storageId, |
| 1283 | IncFsMount::Storage{ |
| 1284 | path::join(root, constants().mount, name)}); |
| 1285 | mNextId = std::max(mNextId, storageId + 1); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | if (ifs->storages.empty()) { |
| 1291 | LOG(WARNING) << "No valid storages in mount " << root; |
| 1292 | return false; |
| 1293 | } |
| 1294 | |
| 1295 | int bindCount = 0; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1296 | { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1297 | std::unique_lock l(mLock, std::defer_lock); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1298 | for (auto&& bp : bindPoints) { |
| 1299 | bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first), |
| 1300 | std::move(*bp.second.mutable_source_subdir()), |
| 1301 | std::move(*bp.second.mutable_dest_path()), |
| 1302 | BindKind::Permanent, l); |
| 1303 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | if (bindCount == 0) { |
| 1307 | LOG(WARNING) << "No valid bind points for mount " << root; |
| 1308 | deleteStorage(*ifs); |
| 1309 | return false; |
| 1310 | } |
| 1311 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1312 | // 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] | 1313 | mMounts[ifs->mountId] = std::move(ifs); |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1317 | IncrementalService::DataLoaderStubPtr IncrementalService::prepareDataLoader( |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1318 | IncFsMount& ifs, DataLoaderParamsParcel&& params, |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1319 | const DataLoaderStatusListener* externalListener) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1320 | std::unique_lock l(ifs.lock); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1321 | prepareDataLoaderLocked(ifs, std::move(params), externalListener); |
| 1322 | return ifs.dataLoaderStub; |
| 1323 | } |
| 1324 | |
| 1325 | void IncrementalService::prepareDataLoaderLocked(IncFsMount& ifs, DataLoaderParamsParcel&& params, |
| 1326 | const DataLoaderStatusListener* externalListener) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1327 | if (ifs.dataLoaderStub) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1328 | LOG(INFO) << "Skipped data loader preparation because it already exists"; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1329 | return; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1330 | } |
| 1331 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1332 | FileSystemControlParcel fsControlParcel; |
Jooyung Han | 66c567a | 2020-03-07 21:47:09 +0900 | [diff] [blame] | 1333 | fsControlParcel.incremental = aidl::make_nullable<IncrementalFileSystemControlParcel>(); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1334 | fsControlParcel.incremental->cmd.reset(dup(ifs.control.cmd())); |
| 1335 | fsControlParcel.incremental->pendingReads.reset(dup(ifs.control.pendingReads())); |
| 1336 | fsControlParcel.incremental->log.reset(dup(ifs.control.logs())); |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 1337 | fsControlParcel.service = new IncrementalServiceConnector(*this, ifs.mountId); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1338 | |
| 1339 | ifs.dataLoaderStub = new DataLoaderStub(*this, ifs.mountId, std::move(params), |
| 1340 | std::move(fsControlParcel), externalListener); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1343 | template <class Duration> |
| 1344 | static long elapsedMcs(Duration start, Duration end) { |
| 1345 | return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); |
| 1346 | } |
| 1347 | |
| 1348 | // Extract lib files from zip, create new files in incfs and write data to them |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1349 | bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath, |
| 1350 | std::string_view libDirRelativePath, |
| 1351 | std::string_view abi) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1352 | auto start = Clock::now(); |
| 1353 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1354 | const auto ifs = getIfs(storage); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1355 | if (!ifs) { |
| 1356 | LOG(ERROR) << "Invalid storage " << storage; |
| 1357 | return false; |
| 1358 | } |
| 1359 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1360 | // First prepare target directories if they don't exist yet |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1361 | if (auto res = makeDirs(*ifs, storage, libDirRelativePath, 0755)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1362 | LOG(ERROR) << "Failed to prepare target lib directory " << libDirRelativePath |
| 1363 | << " errno: " << res; |
| 1364 | return false; |
| 1365 | } |
| 1366 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1367 | auto mkDirsTs = Clock::now(); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1368 | ZipArchiveHandle zipFileHandle; |
| 1369 | if (OpenArchive(path::c_str(apkFullPath), &zipFileHandle)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1370 | LOG(ERROR) << "Failed to open zip file at " << apkFullPath; |
| 1371 | return false; |
| 1372 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1373 | |
| 1374 | // Need a shared pointer: will be passing it into all unpacking jobs. |
| 1375 | std::shared_ptr<ZipArchive> zipFile(zipFileHandle, [](ZipArchiveHandle h) { CloseArchive(h); }); |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1376 | void* cookie = nullptr; |
| 1377 | const auto libFilePrefix = path::join(constants().libDir, abi); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1378 | if (StartIteration(zipFile.get(), &cookie, libFilePrefix, constants().libSuffix)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1379 | LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath; |
| 1380 | return false; |
| 1381 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1382 | auto endIteration = [](void* cookie) { EndIteration(cookie); }; |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1383 | auto iterationCleaner = std::unique_ptr<void, decltype(endIteration)>(cookie, endIteration); |
| 1384 | |
| 1385 | auto openZipTs = Clock::now(); |
| 1386 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1387 | std::vector<Job> jobQueue; |
| 1388 | ZipEntry entry; |
| 1389 | std::string_view fileName; |
| 1390 | while (!Next(cookie, &entry, &fileName)) { |
| 1391 | if (fileName.empty()) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1392 | continue; |
| 1393 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1394 | |
| 1395 | auto startFileTs = Clock::now(); |
| 1396 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1397 | const auto libName = path::basename(fileName); |
Yurii Zubrytskyi | 510037b | 2020-04-22 15:46:21 -0700 | [diff] [blame] | 1398 | auto targetLibPath = path::join(libDirRelativePath, libName); |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 1399 | const auto targetLibPathAbsolute = normalizePathToStorage(*ifs, storage, targetLibPath); |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1400 | // If the extract file already exists, skip |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1401 | if (access(targetLibPathAbsolute.c_str(), F_OK) == 0) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1402 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1403 | LOG(INFO) << "incfs: Native lib file already exists: " << targetLibPath |
| 1404 | << "; skipping extraction, spent " |
| 1405 | << elapsedMcs(startFileTs, Clock::now()) << "mcs"; |
| 1406 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1407 | continue; |
| 1408 | } |
| 1409 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1410 | // Create new lib file without signature info |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1411 | incfs::NewFileParams libFileParams = { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1412 | .size = entry.uncompressed_length, |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1413 | .signature = {}, |
| 1414 | // Metadata of the new lib file is its relative path |
| 1415 | .metadata = {targetLibPath.c_str(), (IncFsSize)targetLibPath.size()}, |
| 1416 | }; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1417 | incfs::FileId libFileId = idFromMetadata(targetLibPath); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1418 | if (auto res = mIncFs->makeFile(ifs->control, targetLibPathAbsolute, 0777, libFileId, |
| 1419 | libFileParams)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1420 | LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1421 | // If one lib file fails to be created, abort others as well |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1422 | return false; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1423 | } |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1424 | |
| 1425 | auto makeFileTs = Clock::now(); |
| 1426 | |
Songchun Fan | afaf6e9 | 2020-03-18 14:12:20 -0700 | [diff] [blame] | 1427 | // If it is a zero-byte file, skip data writing |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1428 | if (entry.uncompressed_length == 0) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1429 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1430 | LOG(INFO) << "incfs: Extracted " << libName |
| 1431 | << "(0 bytes): " << elapsedMcs(startFileTs, makeFileTs) << "mcs"; |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1432 | } |
Songchun Fan | afaf6e9 | 2020-03-18 14:12:20 -0700 | [diff] [blame] | 1433 | continue; |
| 1434 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1435 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 1436 | jobQueue.emplace_back([this, zipFile, entry, ifs = std::weak_ptr<IncFsMount>(ifs), |
| 1437 | libFileId, libPath = std::move(targetLibPath), |
| 1438 | makeFileTs]() mutable { |
| 1439 | extractZipFile(ifs.lock(), zipFile.get(), entry, libFileId, libPath, makeFileTs); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1440 | }); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1441 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1442 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1443 | auto prepareJobTs = Clock::now(); |
| 1444 | LOG(INFO) << "incfs: Processed " << libName << ": " |
| 1445 | << elapsedMcs(startFileTs, prepareJobTs) |
| 1446 | << "mcs, make file: " << elapsedMcs(startFileTs, makeFileTs) |
| 1447 | << " prepare job: " << elapsedMcs(makeFileTs, prepareJobTs); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1448 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1449 | } |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1450 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1451 | auto processedTs = Clock::now(); |
| 1452 | |
| 1453 | if (!jobQueue.empty()) { |
| 1454 | { |
| 1455 | std::lock_guard lock(mJobMutex); |
| 1456 | if (mRunning) { |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1457 | auto& existingJobs = mJobQueue[ifs->mountId]; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1458 | if (existingJobs.empty()) { |
| 1459 | existingJobs = std::move(jobQueue); |
| 1460 | } else { |
| 1461 | existingJobs.insert(existingJobs.end(), std::move_iterator(jobQueue.begin()), |
| 1462 | std::move_iterator(jobQueue.end())); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | mJobCondition.notify_all(); |
| 1467 | } |
| 1468 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1469 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1470 | auto end = Clock::now(); |
| 1471 | LOG(INFO) << "incfs: configureNativeBinaries complete in " << elapsedMcs(start, end) |
| 1472 | << "mcs, make dirs: " << elapsedMcs(start, mkDirsTs) |
| 1473 | << " open zip: " << elapsedMcs(mkDirsTs, openZipTs) |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1474 | << " make files: " << elapsedMcs(openZipTs, processedTs) |
| 1475 | << " schedule jobs: " << elapsedMcs(processedTs, end); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | return true; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1481 | void IncrementalService::extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile, |
| 1482 | ZipEntry& entry, const incfs::FileId& libFileId, |
| 1483 | std::string_view targetLibPath, |
| 1484 | Clock::time_point scheduledTs) { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 1485 | if (!ifs) { |
| 1486 | LOG(INFO) << "Skipping zip file " << targetLibPath << " extraction for an expired mount"; |
| 1487 | return; |
| 1488 | } |
| 1489 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1490 | auto libName = path::basename(targetLibPath); |
| 1491 | auto startedTs = Clock::now(); |
| 1492 | |
| 1493 | // Write extracted data to new file |
| 1494 | // NOTE: don't zero-initialize memory, it may take a while for nothing |
| 1495 | auto libData = std::unique_ptr<uint8_t[]>(new uint8_t[entry.uncompressed_length]); |
| 1496 | if (ExtractToMemory(zipFile, &entry, libData.get(), entry.uncompressed_length)) { |
| 1497 | LOG(ERROR) << "Failed to extract native lib zip entry: " << libName; |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | auto extractFileTs = Clock::now(); |
| 1502 | |
| 1503 | const auto writeFd = mIncFs->openForSpecialOps(ifs->control, libFileId); |
| 1504 | if (!writeFd.ok()) { |
| 1505 | LOG(ERROR) << "Failed to open write fd for: " << targetLibPath << " errno: " << writeFd; |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | auto openFileTs = Clock::now(); |
| 1510 | const int numBlocks = |
| 1511 | (entry.uncompressed_length + constants().blockSize - 1) / constants().blockSize; |
| 1512 | std::vector<IncFsDataBlock> instructions(numBlocks); |
| 1513 | auto remainingData = std::span(libData.get(), entry.uncompressed_length); |
| 1514 | for (int i = 0; i < numBlocks; i++) { |
Yurii Zubrytskyi | 6c65a56 | 2020-04-14 15:25:49 -0700 | [diff] [blame] | 1515 | const auto blockSize = std::min<long>(constants().blockSize, remainingData.size()); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1516 | instructions[i] = IncFsDataBlock{ |
| 1517 | .fileFd = writeFd.get(), |
| 1518 | .pageIndex = static_cast<IncFsBlockIndex>(i), |
| 1519 | .compression = INCFS_COMPRESSION_KIND_NONE, |
| 1520 | .kind = INCFS_BLOCK_KIND_DATA, |
Yurii Zubrytskyi | 6c65a56 | 2020-04-14 15:25:49 -0700 | [diff] [blame] | 1521 | .dataSize = static_cast<uint32_t>(blockSize), |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1522 | .data = reinterpret_cast<const char*>(remainingData.data()), |
| 1523 | }; |
| 1524 | remainingData = remainingData.subspan(blockSize); |
| 1525 | } |
| 1526 | auto prepareInstsTs = Clock::now(); |
| 1527 | |
| 1528 | size_t res = mIncFs->writeBlocks(instructions); |
| 1529 | if (res != instructions.size()) { |
| 1530 | LOG(ERROR) << "Failed to write data into: " << targetLibPath; |
| 1531 | return; |
| 1532 | } |
| 1533 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1534 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1535 | auto endFileTs = Clock::now(); |
| 1536 | LOG(INFO) << "incfs: Extracted " << libName << "(" << entry.compressed_length << " -> " |
| 1537 | << entry.uncompressed_length << " bytes): " << elapsedMcs(startedTs, endFileTs) |
| 1538 | << "mcs, scheduling delay: " << elapsedMcs(scheduledTs, startedTs) |
| 1539 | << " extract: " << elapsedMcs(startedTs, extractFileTs) |
| 1540 | << " open: " << elapsedMcs(extractFileTs, openFileTs) |
| 1541 | << " prepare: " << elapsedMcs(openFileTs, prepareInstsTs) |
| 1542 | << " write: " << elapsedMcs(prepareInstsTs, endFileTs); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | bool IncrementalService::waitForNativeBinariesExtraction(StorageId storage) { |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1547 | struct WaitPrinter { |
| 1548 | const Clock::time_point startTs = Clock::now(); |
| 1549 | ~WaitPrinter() noexcept { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1550 | if (perfLoggingEnabled()) { |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1551 | const auto endTs = Clock::now(); |
| 1552 | LOG(INFO) << "incfs: waitForNativeBinariesExtraction() complete in " |
| 1553 | << elapsedMcs(startTs, endTs) << "mcs"; |
| 1554 | } |
| 1555 | } |
| 1556 | } waitPrinter; |
| 1557 | |
| 1558 | MountId mount; |
| 1559 | { |
| 1560 | auto ifs = getIfs(storage); |
| 1561 | if (!ifs) { |
| 1562 | return true; |
| 1563 | } |
| 1564 | mount = ifs->mountId; |
| 1565 | } |
| 1566 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1567 | std::unique_lock lock(mJobMutex); |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1568 | mJobCondition.wait(lock, [this, mount] { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1569 | return !mRunning || |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1570 | (mPendingJobsMount != mount && mJobQueue.find(mount) == mJobQueue.end()); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1571 | }); |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1572 | return mRunning; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1575 | bool IncrementalService::perfLoggingEnabled() { |
| 1576 | static const bool enabled = base::GetBoolProperty("incremental.perflogging", false); |
| 1577 | return enabled; |
| 1578 | } |
| 1579 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1580 | void IncrementalService::runJobProcessing() { |
| 1581 | for (;;) { |
| 1582 | std::unique_lock lock(mJobMutex); |
| 1583 | mJobCondition.wait(lock, [this]() { return !mRunning || !mJobQueue.empty(); }); |
| 1584 | if (!mRunning) { |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | auto it = mJobQueue.begin(); |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1589 | mPendingJobsMount = it->first; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1590 | auto queue = std::move(it->second); |
| 1591 | mJobQueue.erase(it); |
| 1592 | lock.unlock(); |
| 1593 | |
| 1594 | for (auto&& job : queue) { |
| 1595 | job(); |
| 1596 | } |
| 1597 | |
| 1598 | lock.lock(); |
Yurii Zubrytskyi | 721ac4d | 2020-04-13 11:34:32 -0700 | [diff] [blame] | 1599 | mPendingJobsMount = kInvalidStorageId; |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1600 | lock.unlock(); |
| 1601 | mJobCondition.notify_all(); |
| 1602 | } |
| 1603 | } |
| 1604 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1605 | void IncrementalService::registerAppOpsCallback(const std::string& packageName) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 1606 | sp<IAppOpsCallback> listener; |
| 1607 | { |
| 1608 | std::unique_lock lock{mCallbacksLock}; |
| 1609 | auto& cb = mCallbackRegistered[packageName]; |
| 1610 | if (cb) { |
| 1611 | return; |
| 1612 | } |
| 1613 | cb = new AppOpsListener(*this, packageName); |
| 1614 | listener = cb; |
| 1615 | } |
| 1616 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1617 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_GET_USAGE_STATS, |
| 1618 | String16(packageName.c_str()), listener); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | bool IncrementalService::unregisterAppOpsCallback(const std::string& packageName) { |
| 1622 | sp<IAppOpsCallback> listener; |
| 1623 | { |
| 1624 | std::unique_lock lock{mCallbacksLock}; |
| 1625 | auto found = mCallbackRegistered.find(packageName); |
| 1626 | if (found == mCallbackRegistered.end()) { |
| 1627 | return false; |
| 1628 | } |
| 1629 | listener = found->second; |
| 1630 | mCallbackRegistered.erase(found); |
| 1631 | } |
| 1632 | |
| 1633 | mAppOpsManager->stopWatchingMode(listener); |
| 1634 | return true; |
| 1635 | } |
| 1636 | |
| 1637 | void IncrementalService::onAppOpChanged(const std::string& packageName) { |
| 1638 | if (!unregisterAppOpsCallback(packageName)) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1639 | return; |
| 1640 | } |
| 1641 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1642 | std::vector<IfsMountPtr> affected; |
| 1643 | { |
| 1644 | std::lock_guard l(mLock); |
| 1645 | affected.reserve(mMounts.size()); |
| 1646 | for (auto&& [id, ifs] : mMounts) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1647 | if (ifs->mountId == id && ifs->dataLoaderStub->params().packageName == packageName) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1648 | affected.push_back(ifs); |
| 1649 | } |
| 1650 | } |
| 1651 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1652 | for (auto&& ifs : affected) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 1653 | applyStorageParams(*ifs, false); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1654 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1657 | IncrementalService::DataLoaderStub::DataLoaderStub(IncrementalService& service, MountId id, |
| 1658 | DataLoaderParamsParcel&& params, |
| 1659 | FileSystemControlParcel&& control, |
| 1660 | const DataLoaderStatusListener* externalListener) |
| 1661 | : mService(service), |
| 1662 | mId(id), |
| 1663 | mParams(std::move(params)), |
| 1664 | mControl(std::move(control)), |
| 1665 | mListener(externalListener ? *externalListener : DataLoaderStatusListener()) { |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 1668 | IncrementalService::DataLoaderStub::~DataLoaderStub() = default; |
| 1669 | |
| 1670 | void IncrementalService::DataLoaderStub::cleanupResources() { |
| 1671 | requestDestroy(); |
| 1672 | mParams = {}; |
| 1673 | mControl = {}; |
| 1674 | waitForStatus(IDataLoaderStatusListener::DATA_LOADER_DESTROYED, std::chrono::seconds(60)); |
| 1675 | mListener = {}; |
| 1676 | mId = kInvalidStorageId; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1679 | bool IncrementalService::DataLoaderStub::requestCreate() { |
| 1680 | return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_CREATED); |
| 1681 | } |
| 1682 | |
| 1683 | bool IncrementalService::DataLoaderStub::requestStart() { |
| 1684 | return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_STARTED); |
| 1685 | } |
| 1686 | |
| 1687 | bool IncrementalService::DataLoaderStub::requestDestroy() { |
| 1688 | return setTargetStatus(IDataLoaderStatusListener::DATA_LOADER_DESTROYED); |
| 1689 | } |
| 1690 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1691 | bool IncrementalService::DataLoaderStub::setTargetStatus(int newStatus) { |
| 1692 | int oldStatus, curStatus; |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 1693 | { |
| 1694 | std::unique_lock lock(mStatusMutex); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1695 | oldStatus = mTargetStatus; |
| 1696 | mTargetStatus = newStatus; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1697 | mTargetStatusTs = Clock::now(); |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1698 | curStatus = mCurrentStatus; |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 1699 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1700 | LOG(DEBUG) << "Target status update for DataLoader " << mId << ": " << oldStatus << " -> " |
| 1701 | << newStatus << " (current " << curStatus << ")"; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1702 | return fsmStep(); |
| 1703 | } |
| 1704 | |
| 1705 | bool IncrementalService::DataLoaderStub::waitForStatus(int status, Clock::duration duration) { |
| 1706 | auto now = Clock::now(); |
| 1707 | std::unique_lock lock(mStatusMutex); |
| 1708 | return mStatusCondition.wait_until(lock, now + duration, |
| 1709 | [this, status] { return mCurrentStatus == status; }); |
| 1710 | } |
| 1711 | |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame^] | 1712 | bool IncrementalService::DataLoaderStub::bind() { |
| 1713 | bool result = false; |
| 1714 | auto status = mService.mDataLoaderManager->bindToDataLoader(mId, mParams, this, &result); |
| 1715 | if (!status.isOk() || !result) { |
| 1716 | LOG(ERROR) << "Failed to bind a data loader for mount " << mId; |
| 1717 | return false; |
| 1718 | } |
| 1719 | return true; |
| 1720 | } |
| 1721 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1722 | bool IncrementalService::DataLoaderStub::create() { |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame^] | 1723 | sp<IDataLoader> dataloader; |
| 1724 | auto status = mService.mDataLoaderManager->getDataLoader(mId, &dataloader); |
| 1725 | if (!status.isOk()) { |
| 1726 | LOG(ERROR) << "Failed to get dataloader: " << status.toString8(); |
| 1727 | return false; |
| 1728 | } |
| 1729 | if (!dataloader) { |
| 1730 | LOG(ERROR) << "DataLoader is null: " << status.toString8(); |
| 1731 | return false; |
| 1732 | } |
| 1733 | status = dataloader->create(mId, mParams, mControl, this); |
| 1734 | if (!status.isOk()) { |
| 1735 | LOG(ERROR) << "Failed to start DataLoader: " << status.toString8(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1736 | return false; |
| 1737 | } |
| 1738 | return true; |
| 1739 | } |
| 1740 | |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 1741 | bool IncrementalService::DataLoaderStub::start() { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1742 | sp<IDataLoader> dataloader; |
| 1743 | auto status = mService.mDataLoaderManager->getDataLoader(mId, &dataloader); |
| 1744 | if (!status.isOk()) { |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1745 | LOG(ERROR) << "Failed to get dataloader: " << status.toString8(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1746 | return false; |
| 1747 | } |
| 1748 | if (!dataloader) { |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1749 | LOG(ERROR) << "DataLoader is null: " << status.toString8(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1750 | return false; |
| 1751 | } |
| 1752 | status = dataloader->start(mId); |
| 1753 | if (!status.isOk()) { |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1754 | LOG(ERROR) << "Failed to start DataLoader: " << status.toString8(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1755 | return false; |
| 1756 | } |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1760 | bool IncrementalService::DataLoaderStub::destroy() { |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame^] | 1761 | mService.mDataLoaderManager->unbindFromDataLoader(mId); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1762 | return true; |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1765 | bool IncrementalService::DataLoaderStub::fsmStep() { |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 1766 | if (!isValid()) { |
| 1767 | return false; |
| 1768 | } |
| 1769 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1770 | int currentStatus; |
| 1771 | int targetStatus; |
| 1772 | { |
| 1773 | std::unique_lock lock(mStatusMutex); |
| 1774 | currentStatus = mCurrentStatus; |
| 1775 | targetStatus = mTargetStatus; |
| 1776 | } |
| 1777 | |
| 1778 | if (currentStatus == targetStatus) { |
| 1779 | return true; |
| 1780 | } |
| 1781 | |
| 1782 | switch (targetStatus) { |
| 1783 | case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: { |
| 1784 | return destroy(); |
| 1785 | } |
| 1786 | case IDataLoaderStatusListener::DATA_LOADER_STARTED: { |
| 1787 | switch (currentStatus) { |
| 1788 | case IDataLoaderStatusListener::DATA_LOADER_CREATED: |
| 1789 | case IDataLoaderStatusListener::DATA_LOADER_STOPPED: |
| 1790 | return start(); |
| 1791 | } |
| 1792 | // fallthrough |
| 1793 | } |
| 1794 | case IDataLoaderStatusListener::DATA_LOADER_CREATED: |
| 1795 | switch (currentStatus) { |
| 1796 | case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame^] | 1797 | return bind(); |
| 1798 | case IDataLoaderStatusListener::DATA_LOADER_BOUND: |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1799 | return create(); |
| 1800 | } |
| 1801 | break; |
| 1802 | default: |
| 1803 | LOG(ERROR) << "Invalid target status: " << targetStatus |
| 1804 | << ", current status: " << currentStatus; |
| 1805 | break; |
| 1806 | } |
| 1807 | return false; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | binder::Status IncrementalService::DataLoaderStub::onStatusChanged(MountId mountId, int newStatus) { |
Alex Buynytskyy | 9a54579a | 2020-04-17 15:34:47 -0700 | [diff] [blame] | 1811 | if (!isValid()) { |
| 1812 | return binder::Status:: |
| 1813 | fromServiceSpecificError(-EINVAL, "onStatusChange came to invalid DataLoaderStub"); |
| 1814 | } |
| 1815 | if (mId != mountId) { |
| 1816 | LOG(ERROR) << "Mount ID mismatch: expected " << mId << ", but got: " << mountId; |
| 1817 | return binder::Status::fromServiceSpecificError(-EPERM, "Mount ID mismatch."); |
| 1818 | } |
| 1819 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1820 | int targetStatus, oldStatus; |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1821 | { |
| 1822 | std::unique_lock lock(mStatusMutex); |
| 1823 | if (mCurrentStatus == newStatus) { |
| 1824 | return binder::Status::ok(); |
| 1825 | } |
| 1826 | mCurrentStatus = newStatus; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1827 | oldStatus = mCurrentStatus; |
| 1828 | targetStatus = mTargetStatus; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1831 | LOG(DEBUG) << "Current status update for DataLoader " << mId << ": " << oldStatus << " -> " |
| 1832 | << newStatus << " (target " << targetStatus << ")"; |
| 1833 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1834 | if (mListener) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1835 | mListener->onStatusChanged(mountId, newStatus); |
| 1836 | } |
| 1837 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1838 | fsmStep(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1839 | |
Alex Buynytskyy | c2a645d | 2020-04-20 14:11:55 -0700 | [diff] [blame] | 1840 | mStatusCondition.notify_all(); |
| 1841 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1842 | return binder::Status::ok(); |
| 1843 | } |
| 1844 | |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1845 | void IncrementalService::DataLoaderStub::onDump(int fd) { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1846 | dprintf(fd, " dataLoader: {\n"); |
| 1847 | dprintf(fd, " currentStatus: %d\n", mCurrentStatus); |
| 1848 | dprintf(fd, " targetStatus: %d\n", mTargetStatus); |
| 1849 | dprintf(fd, " targetStatusTs: %lldmcs\n", |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1850 | (long long)(elapsedMcs(mTargetStatusTs, Clock::now()))); |
| 1851 | const auto& params = mParams; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 1852 | dprintf(fd, " dataLoaderParams: {\n"); |
| 1853 | dprintf(fd, " type: %s\n", toString(params.type).c_str()); |
| 1854 | dprintf(fd, " packageName: %s\n", params.packageName.c_str()); |
| 1855 | dprintf(fd, " className: %s\n", params.className.c_str()); |
| 1856 | dprintf(fd, " arguments: %s\n", params.arguments.c_str()); |
| 1857 | dprintf(fd, " }\n"); |
| 1858 | dprintf(fd, " }\n"); |
Alex Buynytskyy | ab65cb1 | 2020-04-17 10:01:47 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 1861 | void IncrementalService::AppOpsListener::opChanged(int32_t, const String16&) { |
| 1862 | incrementalService.onAppOpChanged(packageName); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 1865 | binder::Status IncrementalService::IncrementalServiceConnector::setStorageParams( |
| 1866 | bool enableReadLogs, int32_t* _aidl_return) { |
| 1867 | *_aidl_return = incrementalService.setStorageParams(storage, enableReadLogs); |
| 1868 | return binder::Status::ok(); |
| 1869 | } |
| 1870 | |
Alex Buynytskyy | 0b20266 | 2020-04-13 09:53:04 -0700 | [diff] [blame] | 1871 | FileId IncrementalService::idFromMetadata(std::span<const uint8_t> metadata) { |
| 1872 | return IncFs_FileIdFromMetadata({(const char*)metadata.data(), metadata.size()}); |
| 1873 | } |
| 1874 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1875 | } // namespace android::incremental |