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 | |
| 21 | #include <android-base/file.h> |
| 22 | #include <android-base/logging.h> |
Yurii Zubrytskyi | 0cd8012 | 2020-04-09 23:08:31 -0700 | [diff] [blame] | 23 | #include <android-base/no_destructor.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 24 | #include <android-base/properties.h> |
| 25 | #include <android-base/stringprintf.h> |
| 26 | #include <android-base/strings.h> |
| 27 | #include <android/content/pm/IDataLoaderStatusListener.h> |
| 28 | #include <android/os/IVold.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 29 | #include <binder/BinderService.h> |
Jooyung Han | 66c567a | 2020-03-07 21:47:09 +0900 | [diff] [blame] | 30 | #include <binder/Nullable.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 31 | #include <binder/ParcelFileDescriptor.h> |
| 32 | #include <binder/Status.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <uuid/uuid.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 35 | |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 36 | #include <charconv> |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 37 | #include <ctime> |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 38 | #include <filesystem> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 39 | #include <iterator> |
| 40 | #include <span> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 41 | #include <type_traits> |
| 42 | |
| 43 | #include "Metadata.pb.h" |
| 44 | |
| 45 | using namespace std::literals; |
| 46 | using namespace android::content::pm; |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 47 | namespace fs = std::filesystem; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 48 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 49 | constexpr const char* kDataUsageStats = "android.permission.LOADER_USAGE_STATS"; |
Alex Buynytskyy | 119de1f | 2020-04-08 16:15:35 -0700 | [diff] [blame] | 50 | constexpr const char* kOpUsage = "android:loader_usage_stats"; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 51 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 52 | namespace android::incremental { |
| 53 | |
| 54 | namespace { |
| 55 | |
| 56 | using IncrementalFileSystemControlParcel = |
| 57 | ::android::os::incremental::IncrementalFileSystemControlParcel; |
| 58 | |
| 59 | struct Constants { |
| 60 | static constexpr auto backing = "backing_store"sv; |
| 61 | static constexpr auto mount = "mount"sv; |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 62 | static constexpr auto mountKeyPrefix = "MT_"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 63 | static constexpr auto storagePrefix = "st"sv; |
| 64 | static constexpr auto mountpointMdPrefix = ".mountpoint."sv; |
| 65 | static constexpr auto infoMdName = ".info"sv; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 66 | static constexpr auto libDir = "lib"sv; |
| 67 | static constexpr auto libSuffix = ".so"sv; |
| 68 | static constexpr auto blockSize = 4096; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static const Constants& constants() { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 72 | static constexpr Constants c; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 73 | return c; |
| 74 | } |
| 75 | |
| 76 | template <base::LogSeverity level = base::ERROR> |
| 77 | bool mkdirOrLog(std::string_view name, int mode = 0770, bool allowExisting = true) { |
| 78 | auto cstr = path::c_str(name); |
| 79 | if (::mkdir(cstr, mode)) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 80 | if (!allowExisting || errno != EEXIST) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 81 | PLOG(level) << "Can't create directory '" << name << '\''; |
| 82 | return false; |
| 83 | } |
| 84 | struct stat st; |
| 85 | if (::stat(cstr, &st) || !S_ISDIR(st.st_mode)) { |
| 86 | PLOG(level) << "Path exists but is not a directory: '" << name << '\''; |
| 87 | return false; |
| 88 | } |
| 89 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 90 | if (::chmod(cstr, mode)) { |
| 91 | PLOG(level) << "Changing permission failed for '" << name << '\''; |
| 92 | return false; |
| 93 | } |
| 94 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
| 98 | static std::string toMountKey(std::string_view path) { |
| 99 | if (path.empty()) { |
| 100 | return "@none"; |
| 101 | } |
| 102 | if (path == "/"sv) { |
| 103 | return "@root"; |
| 104 | } |
| 105 | if (path::isAbsolute(path)) { |
| 106 | path.remove_prefix(1); |
| 107 | } |
| 108 | std::string res(path); |
| 109 | std::replace(res.begin(), res.end(), '/', '_'); |
| 110 | std::replace(res.begin(), res.end(), '@', '_'); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 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 | |
| 128 | template <class ProtoMessage, class Control> |
| 129 | static ProtoMessage parseFromIncfs(const IncFsWrapper* incfs, Control&& control, |
| 130 | std::string_view path) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 131 | auto md = incfs->getMetadata(control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 132 | ProtoMessage message; |
| 133 | return message.ParseFromArray(md.data(), md.size()) ? message : ProtoMessage{}; |
| 134 | } |
| 135 | |
| 136 | static bool isValidMountTarget(std::string_view path) { |
| 137 | return path::isAbsolute(path) && path::isEmptyDir(path).value_or(true); |
| 138 | } |
| 139 | |
| 140 | std::string makeBindMdName() { |
| 141 | static constexpr auto uuidStringSize = 36; |
| 142 | |
| 143 | uuid_t guid; |
| 144 | uuid_generate(guid); |
| 145 | |
| 146 | std::string name; |
| 147 | const auto prefixSize = constants().mountpointMdPrefix.size(); |
| 148 | name.reserve(prefixSize + uuidStringSize); |
| 149 | |
| 150 | name = constants().mountpointMdPrefix; |
| 151 | name.resize(prefixSize + uuidStringSize); |
| 152 | uuid_unparse(guid, name.data() + prefixSize); |
| 153 | |
| 154 | return name; |
| 155 | } |
| 156 | } // namespace |
| 157 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 158 | const bool IncrementalService::sEnablePerfLogging = |
| 159 | android::base::GetBoolProperty("incremental.perflogging", false); |
| 160 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 161 | IncrementalService::IncFsMount::~IncFsMount() { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 162 | if (dataLoaderStub) { |
| 163 | dataLoaderStub->destroy(); |
| 164 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 165 | LOG(INFO) << "Unmounting and cleaning up mount " << mountId << " with root '" << root << '\''; |
| 166 | for (auto&& [target, _] : bindPoints) { |
| 167 | LOG(INFO) << "\tbind: " << target; |
| 168 | incrementalService.mVold->unmountIncFs(target); |
| 169 | } |
| 170 | LOG(INFO) << "\troot: " << root; |
| 171 | incrementalService.mVold->unmountIncFs(path::join(root, constants().mount)); |
| 172 | cleanupFilesystem(root); |
| 173 | } |
| 174 | |
| 175 | auto IncrementalService::IncFsMount::makeStorage(StorageId id) -> StorageMap::iterator { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 176 | std::string name; |
| 177 | for (int no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), i = 0; |
| 178 | i < 1024 && no >= 0; no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), ++i) { |
| 179 | name.clear(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 180 | base::StringAppendF(&name, "%.*s_%d_%d", int(constants().storagePrefix.size()), |
| 181 | constants().storagePrefix.data(), id, no); |
| 182 | auto fullName = path::join(root, constants().mount, name); |
Songchun Fan | 9610093 | 2020-02-03 19:20:58 -0800 | [diff] [blame] | 183 | if (auto err = incrementalService.mIncFs->makeDir(control, fullName, 0755); !err) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 184 | std::lock_guard l(lock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 185 | return storages.insert_or_assign(id, Storage{std::move(fullName)}).first; |
| 186 | } else if (err != EEXIST) { |
| 187 | LOG(ERROR) << __func__ << "(): failed to create dir |" << fullName << "| " << err; |
| 188 | break; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | nextStorageDirNo = 0; |
| 192 | return storages.end(); |
| 193 | } |
| 194 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 195 | static std::unique_ptr<DIR, decltype(&::closedir)> openDir(const char* path) { |
| 196 | return {::opendir(path), ::closedir}; |
| 197 | } |
| 198 | |
| 199 | static int rmDirContent(const char* path) { |
| 200 | auto dir = openDir(path); |
| 201 | if (!dir) { |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | while (auto entry = ::readdir(dir.get())) { |
| 205 | if (entry->d_name == "."sv || entry->d_name == ".."sv) { |
| 206 | continue; |
| 207 | } |
| 208 | auto fullPath = android::base::StringPrintf("%s/%s", path, entry->d_name); |
| 209 | if (entry->d_type == DT_DIR) { |
| 210 | if (const auto err = rmDirContent(fullPath.c_str()); err != 0) { |
| 211 | PLOG(WARNING) << "Failed to delete " << fullPath << " content"; |
| 212 | return err; |
| 213 | } |
| 214 | if (const auto err = ::rmdir(fullPath.c_str()); err != 0) { |
| 215 | PLOG(WARNING) << "Failed to rmdir " << fullPath; |
| 216 | return err; |
| 217 | } |
| 218 | } else { |
| 219 | if (const auto err = ::unlink(fullPath.c_str()); err != 0) { |
| 220 | PLOG(WARNING) << "Failed to delete " << fullPath; |
| 221 | return err; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 228 | void IncrementalService::IncFsMount::cleanupFilesystem(std::string_view root) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 229 | rmDirContent(path::join(root, constants().backing).c_str()); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 230 | ::rmdir(path::join(root, constants().backing).c_str()); |
| 231 | ::rmdir(path::join(root, constants().mount).c_str()); |
| 232 | ::rmdir(path::c_str(root)); |
| 233 | } |
| 234 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 235 | IncrementalService::IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir) |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 236 | : mVold(sm.getVoldService()), |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 237 | mDataLoaderManager(sm.getDataLoaderManager()), |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 238 | mIncFs(sm.getIncFs()), |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 239 | mAppOpsManager(sm.getAppOpsManager()), |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 240 | mJni(sm.getJni()), |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 241 | mIncrementalDir(rootDir) { |
| 242 | if (!mVold) { |
| 243 | LOG(FATAL) << "Vold service is unavailable"; |
| 244 | } |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 245 | if (!mDataLoaderManager) { |
| 246 | LOG(FATAL) << "DataLoaderManagerService is unavailable"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 247 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 248 | if (!mAppOpsManager) { |
| 249 | LOG(FATAL) << "AppOpsManager is unavailable"; |
| 250 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 251 | |
| 252 | mJobQueue.reserve(16); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 253 | mJobProcessor = std::thread([this]() { |
| 254 | mJni->initializeForCurrentThread(); |
| 255 | runJobProcessing(); |
| 256 | }); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 257 | |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 258 | mountExistingImages(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 261 | FileId IncrementalService::idFromMetadata(std::span<const uint8_t> metadata) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 262 | return IncFs_FileIdFromMetadata({(const char*)metadata.data(), metadata.size()}); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 265 | IncrementalService::~IncrementalService() { |
| 266 | { |
| 267 | std::lock_guard lock(mJobMutex); |
| 268 | mRunning = false; |
| 269 | } |
| 270 | mJobCondition.notify_all(); |
| 271 | mJobProcessor.join(); |
| 272 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 273 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 274 | inline const char* toString(TimePoint t) { |
| 275 | using SystemClock = std::chrono::system_clock; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 276 | time_t time = SystemClock::to_time_t( |
| 277 | SystemClock::now() + |
| 278 | std::chrono::duration_cast<SystemClock::duration>(t - Clock::now())); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 279 | return std::ctime(&time); |
| 280 | } |
| 281 | |
| 282 | inline const char* toString(IncrementalService::BindKind kind) { |
| 283 | switch (kind) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 284 | case IncrementalService::BindKind::Temporary: |
| 285 | return "Temporary"; |
| 286 | case IncrementalService::BindKind::Permanent: |
| 287 | return "Permanent"; |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | void IncrementalService::onDump(int fd) { |
| 292 | dprintf(fd, "Incremental is %s\n", incfs::enabled() ? "ENABLED" : "DISABLED"); |
| 293 | dprintf(fd, "Incremental dir: %s\n", mIncrementalDir.c_str()); |
| 294 | |
| 295 | std::unique_lock l(mLock); |
| 296 | |
| 297 | dprintf(fd, "Mounts (%d):\n", int(mMounts.size())); |
| 298 | for (auto&& [id, ifs] : mMounts) { |
| 299 | const IncFsMount& mnt = *ifs.get(); |
| 300 | dprintf(fd, "\t[%d]:\n", id); |
| 301 | dprintf(fd, "\t\tmountId: %d\n", mnt.mountId); |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 302 | dprintf(fd, "\t\troot: %s\n", mnt.root.c_str()); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 303 | dprintf(fd, "\t\tnextStorageDirNo: %d\n", mnt.nextStorageDirNo.load()); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 304 | if (mnt.dataLoaderStub) { |
| 305 | const auto& dataLoaderStub = *mnt.dataLoaderStub; |
| 306 | dprintf(fd, "\t\tdataLoaderStatus: %d\n", dataLoaderStub.status()); |
| 307 | dprintf(fd, "\t\tdataLoaderStartRequested: %s\n", |
| 308 | dataLoaderStub.startRequested() ? "true" : "false"); |
| 309 | const auto& params = dataLoaderStub.params(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 310 | dprintf(fd, "\t\tdataLoaderParams:\n"); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 311 | dprintf(fd, "\t\t\ttype: %s\n", toString(params.type).c_str()); |
| 312 | dprintf(fd, "\t\t\tpackageName: %s\n", params.packageName.c_str()); |
| 313 | dprintf(fd, "\t\t\tclassName: %s\n", params.className.c_str()); |
| 314 | dprintf(fd, "\t\t\targuments: %s\n", params.arguments.c_str()); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 315 | } |
| 316 | dprintf(fd, "\t\tstorages (%d):\n", int(mnt.storages.size())); |
| 317 | for (auto&& [storageId, storage] : mnt.storages) { |
| 318 | dprintf(fd, "\t\t\t[%d] -> [%s]\n", storageId, storage.name.c_str()); |
| 319 | } |
| 320 | |
| 321 | dprintf(fd, "\t\tbindPoints (%d):\n", int(mnt.bindPoints.size())); |
| 322 | for (auto&& [target, bind] : mnt.bindPoints) { |
| 323 | dprintf(fd, "\t\t\t[%s]->[%d]:\n", target.c_str(), bind.storage); |
| 324 | dprintf(fd, "\t\t\t\tsavedFilename: %s\n", bind.savedFilename.c_str()); |
| 325 | dprintf(fd, "\t\t\t\tsourceDir: %s\n", bind.sourceDir.c_str()); |
| 326 | dprintf(fd, "\t\t\t\tkind: %s\n", toString(bind.kind)); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | dprintf(fd, "Sorted binds (%d):\n", int(mBindsByPath.size())); |
| 331 | for (auto&& [target, mountPairIt] : mBindsByPath) { |
| 332 | const auto& bind = mountPairIt->second; |
| 333 | dprintf(fd, "\t\t[%s]->[%d]:\n", target.c_str(), bind.storage); |
| 334 | dprintf(fd, "\t\t\tsavedFilename: %s\n", bind.savedFilename.c_str()); |
| 335 | dprintf(fd, "\t\t\tsourceDir: %s\n", bind.sourceDir.c_str()); |
| 336 | dprintf(fd, "\t\t\tkind: %s\n", toString(bind.kind)); |
| 337 | } |
| 338 | } |
| 339 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 340 | void IncrementalService::onSystemReady() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 341 | if (mSystemReady.exchange(true)) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 342 | return; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | std::vector<IfsMountPtr> mounts; |
| 346 | { |
| 347 | std::lock_guard l(mLock); |
| 348 | mounts.reserve(mMounts.size()); |
| 349 | for (auto&& [id, ifs] : mMounts) { |
| 350 | if (ifs->mountId == id) { |
| 351 | mounts.push_back(ifs); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame^] | 356 | if (mounts.empty()) { |
| 357 | return; |
| 358 | } |
| 359 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 360 | std::thread([this, mounts = std::move(mounts)]() { |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame^] | 361 | mJni->initializeForCurrentThread(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 362 | for (auto&& ifs : mounts) { |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame^] | 363 | if (ifs->dataLoaderStub->create()) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 364 | LOG(INFO) << "Successfully started data loader for mount " << ifs->mountId; |
| 365 | } else { |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 366 | // TODO(b/133435829): handle data loader start failures |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 367 | LOG(WARNING) << "Failed to start data loader for mount " << ifs->mountId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 370 | }).detach(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator { |
| 374 | for (;;) { |
| 375 | if (mNextId == kMaxStorageId) { |
| 376 | mNextId = 0; |
| 377 | } |
| 378 | auto id = ++mNextId; |
| 379 | auto [it, inserted] = mMounts.try_emplace(id, nullptr); |
| 380 | if (inserted) { |
| 381 | return it; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 386 | StorageId IncrementalService::createStorage( |
| 387 | std::string_view mountPoint, DataLoaderParamsParcel&& dataLoaderParams, |
| 388 | const DataLoaderStatusListener& dataLoaderStatusListener, CreateOptions options) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 389 | LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options); |
| 390 | if (!path::isAbsolute(mountPoint)) { |
| 391 | LOG(ERROR) << "path is not absolute: " << mountPoint; |
| 392 | return kInvalidStorageId; |
| 393 | } |
| 394 | |
| 395 | auto mountNorm = path::normalize(mountPoint); |
| 396 | { |
| 397 | const auto id = findStorageId(mountNorm); |
| 398 | if (id != kInvalidStorageId) { |
| 399 | if (options & CreateOptions::OpenExisting) { |
| 400 | LOG(INFO) << "Opened existing storage " << id; |
| 401 | return id; |
| 402 | } |
| 403 | LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id; |
| 404 | return kInvalidStorageId; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if (!(options & CreateOptions::CreateNew)) { |
| 409 | LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint; |
| 410 | return kInvalidStorageId; |
| 411 | } |
| 412 | |
| 413 | if (!path::isEmptyDir(mountNorm)) { |
| 414 | LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm; |
| 415 | return kInvalidStorageId; |
| 416 | } |
| 417 | auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm); |
| 418 | if (mountRoot.empty()) { |
| 419 | LOG(ERROR) << "Bad mount point"; |
| 420 | return kInvalidStorageId; |
| 421 | } |
| 422 | // Make sure the code removes all crap it may create while still failing. |
| 423 | auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); }; |
| 424 | auto firstCleanupOnFailure = |
| 425 | std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup); |
| 426 | |
| 427 | auto mountTarget = path::join(mountRoot, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 428 | const auto backing = path::join(mountRoot, constants().backing); |
| 429 | if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 430 | return kInvalidStorageId; |
| 431 | } |
| 432 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 433 | IncFsMount::Control control; |
| 434 | { |
| 435 | std::lock_guard l(mMountOperationLock); |
| 436 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 437 | |
| 438 | if (auto err = rmDirContent(backing.c_str())) { |
| 439 | LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err; |
| 440 | return kInvalidStorageId; |
| 441 | } |
| 442 | if (!mkdirOrLog(path::join(backing, ".index"), 0777)) { |
| 443 | return kInvalidStorageId; |
| 444 | } |
| 445 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 446 | if (!status.isOk()) { |
| 447 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 448 | return kInvalidStorageId; |
| 449 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 450 | if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 || |
| 451 | controlParcel.log.get() < 0) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 452 | LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel."; |
| 453 | return kInvalidStorageId; |
| 454 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 455 | int cmd = controlParcel.cmd.release().release(); |
| 456 | int pendingReads = controlParcel.pendingReads.release().release(); |
| 457 | int logs = controlParcel.log.release().release(); |
| 458 | control = mIncFs->createControl(cmd, pendingReads, logs); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | std::unique_lock l(mLock); |
| 462 | const auto mountIt = getStorageSlotLocked(); |
| 463 | const auto mountId = mountIt->first; |
| 464 | l.unlock(); |
| 465 | |
| 466 | auto ifs = |
| 467 | std::make_shared<IncFsMount>(std::move(mountRoot), mountId, std::move(control), *this); |
| 468 | // Now it's the |ifs|'s responsibility to clean up after itself, and the only cleanup we need |
| 469 | // is the removal of the |ifs|. |
| 470 | firstCleanupOnFailure.release(); |
| 471 | |
| 472 | auto secondCleanup = [this, &l](auto itPtr) { |
| 473 | if (!l.owns_lock()) { |
| 474 | l.lock(); |
| 475 | } |
| 476 | mMounts.erase(*itPtr); |
| 477 | }; |
| 478 | auto secondCleanupOnFailure = |
| 479 | std::unique_ptr<decltype(mountIt), decltype(secondCleanup)>(&mountIt, secondCleanup); |
| 480 | |
| 481 | const auto storageIt = ifs->makeStorage(ifs->mountId); |
| 482 | if (storageIt == ifs->storages.end()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 483 | LOG(ERROR) << "Can't create a default storage directory"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 484 | return kInvalidStorageId; |
| 485 | } |
| 486 | |
| 487 | { |
| 488 | metadata::Mount m; |
| 489 | m.mutable_storage()->set_id(ifs->mountId); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 490 | m.mutable_loader()->set_type((int)dataLoaderParams.type); |
| 491 | m.mutable_loader()->set_package_name(dataLoaderParams.packageName); |
| 492 | m.mutable_loader()->set_class_name(dataLoaderParams.className); |
| 493 | m.mutable_loader()->set_arguments(dataLoaderParams.arguments); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 494 | const auto metadata = m.SerializeAsString(); |
| 495 | m.mutable_loader()->release_arguments(); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 496 | m.mutable_loader()->release_class_name(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 497 | m.mutable_loader()->release_package_name(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 498 | if (auto err = |
| 499 | mIncFs->makeFile(ifs->control, |
| 500 | path::join(ifs->root, constants().mount, |
| 501 | constants().infoMdName), |
| 502 | 0777, idFromMetadata(metadata), |
| 503 | {.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 504 | LOG(ERROR) << "Saving mount metadata failed: " << -err; |
| 505 | return kInvalidStorageId; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | const auto bk = |
| 510 | (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 511 | if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name, |
| 512 | std::string(storageIt->second.name), std::move(mountNorm), bk, l); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 513 | err < 0) { |
| 514 | LOG(ERROR) << "adding bind mount failed: " << -err; |
| 515 | return kInvalidStorageId; |
| 516 | } |
| 517 | |
| 518 | // Done here as well, all data structures are in good state. |
| 519 | secondCleanupOnFailure.release(); |
| 520 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 521 | auto dataLoaderStub = |
| 522 | prepareDataLoader(*ifs, std::move(dataLoaderParams), &dataLoaderStatusListener); |
| 523 | CHECK(dataLoaderStub); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 524 | |
| 525 | mountIt->second = std::move(ifs); |
| 526 | l.unlock(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 527 | |
| 528 | if (mSystemReady.load(std::memory_order_relaxed) && !dataLoaderStub->create()) { |
| 529 | // failed to create data loader |
| 530 | LOG(ERROR) << "initializeDataLoader() failed"; |
| 531 | deleteStorage(dataLoaderStub->id()); |
| 532 | return kInvalidStorageId; |
| 533 | } |
| 534 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 535 | LOG(INFO) << "created storage " << mountId; |
| 536 | return mountId; |
| 537 | } |
| 538 | |
| 539 | StorageId IncrementalService::createLinkedStorage(std::string_view mountPoint, |
| 540 | StorageId linkedStorage, |
| 541 | IncrementalService::CreateOptions options) { |
| 542 | if (!isValidMountTarget(mountPoint)) { |
| 543 | LOG(ERROR) << "Mount point is invalid or missing"; |
| 544 | return kInvalidStorageId; |
| 545 | } |
| 546 | |
| 547 | std::unique_lock l(mLock); |
| 548 | const auto& ifs = getIfsLocked(linkedStorage); |
| 549 | if (!ifs) { |
| 550 | LOG(ERROR) << "Ifs unavailable"; |
| 551 | return kInvalidStorageId; |
| 552 | } |
| 553 | |
| 554 | const auto mountIt = getStorageSlotLocked(); |
| 555 | const auto storageId = mountIt->first; |
| 556 | const auto storageIt = ifs->makeStorage(storageId); |
| 557 | if (storageIt == ifs->storages.end()) { |
| 558 | LOG(ERROR) << "Can't create a new storage"; |
| 559 | mMounts.erase(mountIt); |
| 560 | return kInvalidStorageId; |
| 561 | } |
| 562 | |
| 563 | l.unlock(); |
| 564 | |
| 565 | const auto bk = |
| 566 | (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 567 | if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name, |
| 568 | std::string(storageIt->second.name), path::normalize(mountPoint), |
| 569 | bk, l); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 570 | err < 0) { |
| 571 | LOG(ERROR) << "bindMount failed with error: " << err; |
| 572 | return kInvalidStorageId; |
| 573 | } |
| 574 | |
| 575 | mountIt->second = ifs; |
| 576 | return storageId; |
| 577 | } |
| 578 | |
| 579 | IncrementalService::BindPathMap::const_iterator IncrementalService::findStorageLocked( |
| 580 | std::string_view path) const { |
| 581 | auto bindPointIt = mBindsByPath.upper_bound(path); |
| 582 | if (bindPointIt == mBindsByPath.begin()) { |
| 583 | return mBindsByPath.end(); |
| 584 | } |
| 585 | --bindPointIt; |
| 586 | if (!path::startsWith(path, bindPointIt->first)) { |
| 587 | return mBindsByPath.end(); |
| 588 | } |
| 589 | return bindPointIt; |
| 590 | } |
| 591 | |
| 592 | StorageId IncrementalService::findStorageId(std::string_view path) const { |
| 593 | std::lock_guard l(mLock); |
| 594 | auto it = findStorageLocked(path); |
| 595 | if (it == mBindsByPath.end()) { |
| 596 | return kInvalidStorageId; |
| 597 | } |
| 598 | return it->second->second.storage; |
| 599 | } |
| 600 | |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 601 | int IncrementalService::setStorageParams(StorageId storageId, bool enableReadLogs) { |
| 602 | const auto ifs = getIfs(storageId); |
| 603 | if (!ifs) { |
Alex Buynytskyy | 5f9e3a0 | 2020-04-07 21:13:41 -0700 | [diff] [blame] | 604 | LOG(ERROR) << "setStorageParams failed, invalid storageId: " << storageId; |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 605 | return -EINVAL; |
| 606 | } |
| 607 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 608 | const auto& params = ifs->dataLoaderStub->params(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 609 | if (enableReadLogs) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 610 | if (auto status = mAppOpsManager->checkPermission(kDataUsageStats, kOpUsage, |
| 611 | params.packageName.c_str()); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 612 | !status.isOk()) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 613 | LOG(ERROR) << "checkPermission failed: " << status.toString8(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 614 | return fromBinderStatus(status); |
| 615 | } |
| 616 | } |
| 617 | |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 618 | if (auto status = applyStorageParams(*ifs, enableReadLogs); !status.isOk()) { |
| 619 | LOG(ERROR) << "applyStorageParams failed: " << status.toString8(); |
| 620 | return fromBinderStatus(status); |
| 621 | } |
| 622 | |
| 623 | if (enableReadLogs) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 624 | registerAppOpsCallback(params.packageName); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | binder::Status IncrementalService::applyStorageParams(IncFsMount& ifs, bool enableReadLogs) { |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 631 | using unique_fd = ::android::base::unique_fd; |
| 632 | ::android::os::incremental::IncrementalFileSystemControlParcel control; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 633 | control.cmd.reset(unique_fd(dup(ifs.control.cmd()))); |
| 634 | control.pendingReads.reset(unique_fd(dup(ifs.control.pendingReads()))); |
| 635 | auto logsFd = ifs.control.logs(); |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 636 | if (logsFd >= 0) { |
| 637 | control.log.reset(unique_fd(dup(logsFd))); |
| 638 | } |
| 639 | |
| 640 | std::lock_guard l(mMountOperationLock); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 641 | return mVold->setIncFsMountOptions(control, enableReadLogs); |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 644 | void IncrementalService::deleteStorage(StorageId storageId) { |
| 645 | const auto ifs = getIfs(storageId); |
| 646 | if (!ifs) { |
| 647 | return; |
| 648 | } |
| 649 | deleteStorage(*ifs); |
| 650 | } |
| 651 | |
| 652 | void IncrementalService::deleteStorage(IncrementalService::IncFsMount& ifs) { |
| 653 | std::unique_lock l(ifs.lock); |
| 654 | deleteStorageLocked(ifs, std::move(l)); |
| 655 | } |
| 656 | |
| 657 | void IncrementalService::deleteStorageLocked(IncrementalService::IncFsMount& ifs, |
| 658 | std::unique_lock<std::mutex>&& ifsLock) { |
| 659 | const auto storages = std::move(ifs.storages); |
| 660 | // Don't move the bind points out: Ifs's dtor will use them to unmount everything. |
| 661 | const auto bindPoints = ifs.bindPoints; |
| 662 | ifsLock.unlock(); |
| 663 | |
| 664 | std::lock_guard l(mLock); |
| 665 | for (auto&& [id, _] : storages) { |
| 666 | if (id != ifs.mountId) { |
| 667 | mMounts.erase(id); |
| 668 | } |
| 669 | } |
| 670 | for (auto&& [path, _] : bindPoints) { |
| 671 | mBindsByPath.erase(path); |
| 672 | } |
| 673 | mMounts.erase(ifs.mountId); |
| 674 | } |
| 675 | |
| 676 | StorageId IncrementalService::openStorage(std::string_view pathInMount) { |
| 677 | if (!path::isAbsolute(pathInMount)) { |
| 678 | return kInvalidStorageId; |
| 679 | } |
| 680 | |
| 681 | return findStorageId(path::normalize(pathInMount)); |
| 682 | } |
| 683 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 684 | FileId IncrementalService::nodeFor(StorageId storage, std::string_view subpath) const { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 685 | const auto ifs = getIfs(storage); |
| 686 | if (!ifs) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 687 | return kIncFsInvalidFileId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 688 | } |
| 689 | std::unique_lock l(ifs->lock); |
| 690 | auto storageIt = ifs->storages.find(storage); |
| 691 | if (storageIt == ifs->storages.end()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 692 | return kIncFsInvalidFileId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 693 | } |
| 694 | if (subpath.empty() || subpath == "."sv) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 695 | return kIncFsInvalidFileId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 696 | } |
| 697 | auto path = path::join(ifs->root, constants().mount, storageIt->second.name, subpath); |
| 698 | l.unlock(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 699 | return mIncFs->getFileId(ifs->control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 702 | std::pair<FileId, std::string_view> IncrementalService::parentAndNameFor( |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 703 | StorageId storage, std::string_view subpath) const { |
| 704 | auto name = path::basename(subpath); |
| 705 | if (name.empty()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 706 | return {kIncFsInvalidFileId, {}}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 707 | } |
| 708 | auto dir = path::dirname(subpath); |
| 709 | if (dir.empty() || dir == "/"sv) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 710 | return {kIncFsInvalidFileId, {}}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 711 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 712 | auto id = nodeFor(storage, dir); |
| 713 | return {id, name}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | IncrementalService::IfsMountPtr IncrementalService::getIfs(StorageId storage) const { |
| 717 | std::lock_guard l(mLock); |
| 718 | return getIfsLocked(storage); |
| 719 | } |
| 720 | |
| 721 | const IncrementalService::IfsMountPtr& IncrementalService::getIfsLocked(StorageId storage) const { |
| 722 | auto it = mMounts.find(storage); |
| 723 | if (it == mMounts.end()) { |
Yurii Zubrytskyi | 0cd8012 | 2020-04-09 23:08:31 -0700 | [diff] [blame] | 724 | static const android::base::NoDestructor<IfsMountPtr> kEmpty{}; |
| 725 | return *kEmpty; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 726 | } |
| 727 | return it->second; |
| 728 | } |
| 729 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 730 | int IncrementalService::bind(StorageId storage, std::string_view source, std::string_view target, |
| 731 | BindKind kind) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 732 | if (!isValidMountTarget(target)) { |
| 733 | return -EINVAL; |
| 734 | } |
| 735 | |
| 736 | const auto ifs = getIfs(storage); |
| 737 | if (!ifs) { |
| 738 | return -EINVAL; |
| 739 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 740 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 741 | std::unique_lock l(ifs->lock); |
| 742 | const auto storageInfo = ifs->storages.find(storage); |
| 743 | if (storageInfo == ifs->storages.end()) { |
| 744 | return -EINVAL; |
| 745 | } |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 746 | std::string normSource = normalizePathToStorageLocked(storageInfo, source); |
| 747 | if (normSource.empty()) { |
| 748 | return -EINVAL; |
| 749 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 750 | l.unlock(); |
| 751 | std::unique_lock l2(mLock, std::defer_lock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 752 | return addBindMount(*ifs, storage, storageInfo->second.name, std::move(normSource), |
| 753 | path::normalize(target), kind, l2); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | int IncrementalService::unbind(StorageId storage, std::string_view target) { |
| 757 | if (!path::isAbsolute(target)) { |
| 758 | return -EINVAL; |
| 759 | } |
| 760 | |
| 761 | LOG(INFO) << "Removing bind point " << target; |
| 762 | |
| 763 | // Here we should only look up by the exact target, not by a subdirectory of any existing mount, |
| 764 | // otherwise there's a chance to unmount something completely unrelated |
| 765 | const auto norm = path::normalize(target); |
| 766 | std::unique_lock l(mLock); |
| 767 | const auto storageIt = mBindsByPath.find(norm); |
| 768 | if (storageIt == mBindsByPath.end() || storageIt->second->second.storage != storage) { |
| 769 | return -EINVAL; |
| 770 | } |
| 771 | const auto bindIt = storageIt->second; |
| 772 | const auto storageId = bindIt->second.storage; |
| 773 | const auto ifs = getIfsLocked(storageId); |
| 774 | if (!ifs) { |
| 775 | LOG(ERROR) << "Internal error: storageId " << storageId << " for bound path " << target |
| 776 | << " is missing"; |
| 777 | return -EFAULT; |
| 778 | } |
| 779 | mBindsByPath.erase(storageIt); |
| 780 | l.unlock(); |
| 781 | |
| 782 | mVold->unmountIncFs(bindIt->first); |
| 783 | std::unique_lock l2(ifs->lock); |
| 784 | if (ifs->bindPoints.size() <= 1) { |
| 785 | ifs->bindPoints.clear(); |
| 786 | deleteStorageLocked(*ifs, std::move(l2)); |
| 787 | } else { |
| 788 | const std::string savedFile = std::move(bindIt->second.savedFilename); |
| 789 | ifs->bindPoints.erase(bindIt); |
| 790 | l2.unlock(); |
| 791 | if (!savedFile.empty()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 792 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, savedFile)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | return 0; |
| 796 | } |
| 797 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 798 | std::string IncrementalService::normalizePathToStorageLocked( |
| 799 | IncFsMount::StorageMap::iterator storageIt, std::string_view path) { |
| 800 | std::string normPath; |
| 801 | if (path::isAbsolute(path)) { |
| 802 | normPath = path::normalize(path); |
| 803 | if (!path::startsWith(normPath, storageIt->second.name)) { |
| 804 | return {}; |
| 805 | } |
| 806 | } else { |
| 807 | normPath = path::normalize(path::join(storageIt->second.name, path)); |
| 808 | } |
| 809 | return normPath; |
| 810 | } |
| 811 | |
| 812 | std::string IncrementalService::normalizePathToStorage(const IncrementalService::IfsMountPtr& ifs, |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 813 | StorageId storage, std::string_view path) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 814 | std::unique_lock l(ifs->lock); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 815 | const auto storageInfo = ifs->storages.find(storage); |
| 816 | if (storageInfo == ifs->storages.end()) { |
| 817 | return {}; |
| 818 | } |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 819 | return normalizePathToStorageLocked(storageInfo, path); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 822 | int IncrementalService::makeFile(StorageId storage, std::string_view path, int mode, FileId id, |
| 823 | incfs::NewFileParams params) { |
| 824 | if (auto ifs = getIfs(storage)) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 825 | std::string normPath = normalizePathToStorage(ifs, storage, path); |
| 826 | if (normPath.empty()) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 827 | LOG(ERROR) << "Internal error: storageId " << storage |
| 828 | << " failed to normalize: " << path; |
Songchun Fan | 54c6aed | 2020-01-31 16:52:41 -0800 | [diff] [blame] | 829 | return -EINVAL; |
| 830 | } |
| 831 | auto err = mIncFs->makeFile(ifs->control, normPath, mode, id, params); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 832 | if (err) { |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 833 | LOG(ERROR) << "Internal error: storageId " << storage << " failed to makeFile: " << err; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 834 | return err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 835 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 836 | return 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 837 | } |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 841 | int IncrementalService::makeDir(StorageId storageId, std::string_view path, int mode) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 842 | if (auto ifs = getIfs(storageId)) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 843 | std::string normPath = normalizePathToStorage(ifs, storageId, path); |
| 844 | if (normPath.empty()) { |
| 845 | return -EINVAL; |
| 846 | } |
| 847 | return mIncFs->makeDir(ifs->control, normPath, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 848 | } |
| 849 | return -EINVAL; |
| 850 | } |
| 851 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 852 | int IncrementalService::makeDirs(StorageId storageId, std::string_view path, int mode) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 853 | const auto ifs = getIfs(storageId); |
| 854 | if (!ifs) { |
| 855 | return -EINVAL; |
| 856 | } |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 857 | std::string normPath = normalizePathToStorage(ifs, storageId, path); |
| 858 | if (normPath.empty()) { |
| 859 | return -EINVAL; |
| 860 | } |
| 861 | auto err = mIncFs->makeDir(ifs->control, normPath, mode); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 862 | if (err == -EEXIST) { |
| 863 | return 0; |
| 864 | } else if (err != -ENOENT) { |
| 865 | return err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 866 | } |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 867 | if (auto err = makeDirs(storageId, path::dirname(normPath), mode)) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 868 | return err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 869 | } |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 870 | return mIncFs->makeDir(ifs->control, normPath, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 871 | } |
| 872 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 873 | int IncrementalService::link(StorageId sourceStorageId, std::string_view oldPath, |
| 874 | StorageId destStorageId, std::string_view newPath) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 875 | auto ifsSrc = getIfs(sourceStorageId); |
| 876 | auto ifsDest = sourceStorageId == destStorageId ? ifsSrc : getIfs(destStorageId); |
| 877 | if (ifsSrc && ifsSrc == ifsDest) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 878 | std::string normOldPath = normalizePathToStorage(ifsSrc, sourceStorageId, oldPath); |
| 879 | std::string normNewPath = normalizePathToStorage(ifsDest, destStorageId, newPath); |
| 880 | if (normOldPath.empty() || normNewPath.empty()) { |
| 881 | return -EINVAL; |
| 882 | } |
| 883 | return mIncFs->link(ifsSrc->control, normOldPath, normNewPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 884 | } |
| 885 | return -EINVAL; |
| 886 | } |
| 887 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 888 | int IncrementalService::unlink(StorageId storage, std::string_view path) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 889 | if (auto ifs = getIfs(storage)) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 890 | std::string normOldPath = normalizePathToStorage(ifs, storage, path); |
| 891 | return mIncFs->unlink(ifs->control, normOldPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 892 | } |
| 893 | return -EINVAL; |
| 894 | } |
| 895 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 896 | int IncrementalService::addBindMount(IncFsMount& ifs, StorageId storage, |
| 897 | std::string_view storageRoot, std::string&& source, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 898 | std::string&& target, BindKind kind, |
| 899 | std::unique_lock<std::mutex>& mainLock) { |
| 900 | if (!isValidMountTarget(target)) { |
| 901 | return -EINVAL; |
| 902 | } |
| 903 | |
| 904 | std::string mdFileName; |
| 905 | if (kind != BindKind::Temporary) { |
| 906 | metadata::BindPoint bp; |
| 907 | bp.set_storage_id(storage); |
| 908 | bp.set_allocated_dest_path(&target); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 909 | bp.set_allocated_source_subdir(&source); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 910 | const auto metadata = bp.SerializeAsString(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 911 | bp.release_dest_path(); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 912 | bp.release_source_subdir(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 913 | mdFileName = makeBindMdName(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 914 | auto node = |
| 915 | mIncFs->makeFile(ifs.control, path::join(ifs.root, constants().mount, mdFileName), |
| 916 | 0444, idFromMetadata(metadata), |
| 917 | {.metadata = {metadata.data(), (IncFsSize)metadata.size()}}); |
| 918 | if (node) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 919 | return int(node); |
| 920 | } |
| 921 | } |
| 922 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 923 | return addBindMountWithMd(ifs, storage, std::move(mdFileName), std::move(source), |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 924 | std::move(target), kind, mainLock); |
| 925 | } |
| 926 | |
| 927 | int IncrementalService::addBindMountWithMd(IncrementalService::IncFsMount& ifs, StorageId storage, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 928 | std::string&& metadataName, std::string&& source, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 929 | std::string&& target, BindKind kind, |
| 930 | std::unique_lock<std::mutex>& mainLock) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 931 | { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 932 | std::lock_guard l(mMountOperationLock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 933 | const auto status = mVold->bindMount(source, target); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 934 | if (!status.isOk()) { |
| 935 | LOG(ERROR) << "Calling Vold::bindMount() failed: " << status.toString8(); |
| 936 | return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC |
| 937 | ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode() |
| 938 | : status.serviceSpecificErrorCode() == 0 |
| 939 | ? -EFAULT |
| 940 | : status.serviceSpecificErrorCode() |
| 941 | : -EIO; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (!mainLock.owns_lock()) { |
| 946 | mainLock.lock(); |
| 947 | } |
| 948 | std::lock_guard l(ifs.lock); |
| 949 | const auto [it, _] = |
| 950 | ifs.bindPoints.insert_or_assign(target, |
| 951 | IncFsMount::Bind{storage, std::move(metadataName), |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 952 | std::move(source), kind}); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 953 | mBindsByPath[std::move(target)] = it; |
| 954 | return 0; |
| 955 | } |
| 956 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 957 | RawMetadata IncrementalService::getMetadata(StorageId storage, FileId node) const { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 958 | const auto ifs = getIfs(storage); |
| 959 | if (!ifs) { |
| 960 | return {}; |
| 961 | } |
| 962 | return mIncFs->getMetadata(ifs->control, node); |
| 963 | } |
| 964 | |
| 965 | std::vector<std::string> IncrementalService::listFiles(StorageId storage) const { |
| 966 | const auto ifs = getIfs(storage); |
| 967 | if (!ifs) { |
| 968 | return {}; |
| 969 | } |
| 970 | |
| 971 | std::unique_lock l(ifs->lock); |
| 972 | auto subdirIt = ifs->storages.find(storage); |
| 973 | if (subdirIt == ifs->storages.end()) { |
| 974 | return {}; |
| 975 | } |
| 976 | auto dir = path::join(ifs->root, constants().mount, subdirIt->second.name); |
| 977 | l.unlock(); |
| 978 | |
| 979 | const auto prefixSize = dir.size() + 1; |
| 980 | std::vector<std::string> todoDirs{std::move(dir)}; |
| 981 | std::vector<std::string> result; |
| 982 | do { |
| 983 | auto currDir = std::move(todoDirs.back()); |
| 984 | todoDirs.pop_back(); |
| 985 | |
| 986 | auto d = |
| 987 | std::unique_ptr<DIR, decltype(&::closedir)>(::opendir(currDir.c_str()), ::closedir); |
| 988 | while (auto e = ::readdir(d.get())) { |
| 989 | if (e->d_type == DT_REG) { |
| 990 | result.emplace_back( |
| 991 | path::join(std::string_view(currDir).substr(prefixSize), e->d_name)); |
| 992 | continue; |
| 993 | } |
| 994 | if (e->d_type == DT_DIR) { |
| 995 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 996 | continue; |
| 997 | } |
| 998 | todoDirs.emplace_back(path::join(currDir, e->d_name)); |
| 999 | continue; |
| 1000 | } |
| 1001 | } |
| 1002 | } while (!todoDirs.empty()); |
| 1003 | return result; |
| 1004 | } |
| 1005 | |
| 1006 | bool IncrementalService::startLoading(StorageId storage) const { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1007 | DataLoaderStubPtr dataLoaderStub; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1008 | { |
| 1009 | std::unique_lock l(mLock); |
| 1010 | const auto& ifs = getIfsLocked(storage); |
| 1011 | if (!ifs) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1012 | return false; |
| 1013 | } |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1014 | dataLoaderStub = ifs->dataLoaderStub; |
| 1015 | if (!dataLoaderStub) { |
| 1016 | return false; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1017 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1018 | } |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1019 | return dataLoaderStub->start(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | void IncrementalService::mountExistingImages() { |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 1023 | for (const auto& entry : fs::directory_iterator(mIncrementalDir)) { |
| 1024 | const auto path = entry.path().u8string(); |
| 1025 | const auto name = entry.path().filename().u8string(); |
| 1026 | if (!base::StartsWith(name, constants().mountKeyPrefix)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1027 | continue; |
| 1028 | } |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 1029 | const auto root = path::join(mIncrementalDir, name); |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1030 | if (!mountExistingImage(root)) { |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 1031 | IncFsMount::cleanupFilesystem(path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1036 | bool IncrementalService::mountExistingImage(std::string_view root) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1037 | auto mountTarget = path::join(root, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1038 | const auto backing = path::join(root, constants().backing); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1039 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1040 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1041 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1042 | if (!status.isOk()) { |
| 1043 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 1044 | return false; |
| 1045 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 1046 | |
| 1047 | int cmd = controlParcel.cmd.release().release(); |
| 1048 | int pendingReads = controlParcel.pendingReads.release().release(); |
| 1049 | int logs = controlParcel.log.release().release(); |
| 1050 | IncFsMount::Control control = mIncFs->createControl(cmd, pendingReads, logs); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1051 | |
| 1052 | auto ifs = std::make_shared<IncFsMount>(std::string(root), -1, std::move(control), *this); |
| 1053 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1054 | auto mount = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control, |
| 1055 | path::join(mountTarget, constants().infoMdName)); |
| 1056 | if (!mount.has_loader() || !mount.has_storage()) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1057 | LOG(ERROR) << "Bad mount metadata in mount at " << root; |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1061 | ifs->mountId = mount.storage().id(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1062 | mNextId = std::max(mNextId, ifs->mountId + 1); |
| 1063 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1064 | // DataLoader params |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1065 | DataLoaderParamsParcel dataLoaderParams; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1066 | { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1067 | const auto& loader = mount.loader(); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1068 | dataLoaderParams.type = (android::content::pm::DataLoaderType)loader.type(); |
| 1069 | dataLoaderParams.packageName = loader.package_name(); |
| 1070 | dataLoaderParams.className = loader.class_name(); |
| 1071 | dataLoaderParams.arguments = loader.arguments(); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Alex Buynytskyy | 6994166 | 2020-04-11 21:40:37 -0700 | [diff] [blame^] | 1074 | prepareDataLoader(*ifs, std::move(dataLoaderParams), nullptr); |
| 1075 | CHECK(ifs->dataLoaderStub); |
| 1076 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1077 | std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1078 | auto d = openDir(path::c_str(mountTarget)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1079 | while (auto e = ::readdir(d.get())) { |
| 1080 | if (e->d_type == DT_REG) { |
| 1081 | auto name = std::string_view(e->d_name); |
| 1082 | if (name.starts_with(constants().mountpointMdPrefix)) { |
| 1083 | bindPoints.emplace_back(name, |
| 1084 | parseFromIncfs<metadata::BindPoint>(mIncFs.get(), |
| 1085 | ifs->control, |
| 1086 | path::join(mountTarget, |
| 1087 | name))); |
| 1088 | if (bindPoints.back().second.dest_path().empty() || |
| 1089 | bindPoints.back().second.source_subdir().empty()) { |
| 1090 | bindPoints.pop_back(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1091 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | } else if (e->d_type == DT_DIR) { |
| 1095 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 1096 | continue; |
| 1097 | } |
| 1098 | auto name = std::string_view(e->d_name); |
| 1099 | if (name.starts_with(constants().storagePrefix)) { |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1100 | int storageId; |
| 1101 | const auto res = std::from_chars(name.data() + constants().storagePrefix.size() + 1, |
| 1102 | name.data() + name.size(), storageId); |
| 1103 | if (res.ec != std::errc{} || *res.ptr != '_') { |
| 1104 | LOG(WARNING) << "Ignoring storage with invalid name '" << name << "' for mount " |
| 1105 | << root; |
| 1106 | continue; |
| 1107 | } |
| 1108 | auto [_, inserted] = mMounts.try_emplace(storageId, ifs); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1109 | if (!inserted) { |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1110 | LOG(WARNING) << "Ignoring storage with duplicate id " << storageId |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1111 | << " for mount " << root; |
| 1112 | continue; |
| 1113 | } |
Yurii Zubrytskyi | 107ae35 | 2020-04-03 13:12:51 -0700 | [diff] [blame] | 1114 | ifs->storages.insert_or_assign(storageId, |
| 1115 | IncFsMount::Storage{ |
| 1116 | path::join(root, constants().mount, name)}); |
| 1117 | mNextId = std::max(mNextId, storageId + 1); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | if (ifs->storages.empty()) { |
| 1123 | LOG(WARNING) << "No valid storages in mount " << root; |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | int bindCount = 0; |
| 1128 | for (auto&& bp : bindPoints) { |
| 1129 | std::unique_lock l(mLock, std::defer_lock); |
| 1130 | bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first), |
| 1131 | std::move(*bp.second.mutable_source_subdir()), |
| 1132 | std::move(*bp.second.mutable_dest_path()), |
| 1133 | BindKind::Permanent, l); |
| 1134 | } |
| 1135 | |
| 1136 | if (bindCount == 0) { |
| 1137 | LOG(WARNING) << "No valid bind points for mount " << root; |
| 1138 | deleteStorage(*ifs); |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1142 | mMounts[ifs->mountId] = std::move(ifs); |
| 1143 | return true; |
| 1144 | } |
| 1145 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1146 | IncrementalService::DataLoaderStubPtr IncrementalService::prepareDataLoader( |
| 1147 | IncrementalService::IncFsMount& ifs, DataLoaderParamsParcel&& params, |
| 1148 | const DataLoaderStatusListener* externalListener) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1149 | std::unique_lock l(ifs.lock); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1150 | if (ifs.dataLoaderStub) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1151 | LOG(INFO) << "Skipped data loader preparation because it already exists"; |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1152 | return ifs.dataLoaderStub; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1155 | FileSystemControlParcel fsControlParcel; |
Jooyung Han | 66c567a | 2020-03-07 21:47:09 +0900 | [diff] [blame] | 1156 | fsControlParcel.incremental = aidl::make_nullable<IncrementalFileSystemControlParcel>(); |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 1157 | fsControlParcel.incremental->cmd.reset(base::unique_fd(::dup(ifs.control.cmd()))); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1158 | fsControlParcel.incremental->pendingReads.reset( |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 1159 | base::unique_fd(::dup(ifs.control.pendingReads()))); |
| 1160 | fsControlParcel.incremental->log.reset(base::unique_fd(::dup(ifs.control.logs()))); |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 1161 | fsControlParcel.service = new IncrementalServiceConnector(*this, ifs.mountId); |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1162 | |
| 1163 | ifs.dataLoaderStub = new DataLoaderStub(*this, ifs.mountId, std::move(params), |
| 1164 | std::move(fsControlParcel), externalListener); |
| 1165 | return ifs.dataLoaderStub; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1168 | template <class Duration> |
| 1169 | static long elapsedMcs(Duration start, Duration end) { |
| 1170 | return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); |
| 1171 | } |
| 1172 | |
| 1173 | // 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] | 1174 | bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath, |
| 1175 | std::string_view libDirRelativePath, |
| 1176 | std::string_view abi) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1177 | auto start = Clock::now(); |
| 1178 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1179 | const auto ifs = getIfs(storage); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1180 | if (!ifs) { |
| 1181 | LOG(ERROR) << "Invalid storage " << storage; |
| 1182 | return false; |
| 1183 | } |
| 1184 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1185 | // First prepare target directories if they don't exist yet |
| 1186 | if (auto res = makeDirs(storage, libDirRelativePath, 0755)) { |
| 1187 | LOG(ERROR) << "Failed to prepare target lib directory " << libDirRelativePath |
| 1188 | << " errno: " << res; |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1192 | auto mkDirsTs = Clock::now(); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1193 | ZipArchiveHandle zipFileHandle; |
| 1194 | if (OpenArchive(path::c_str(apkFullPath), &zipFileHandle)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1195 | LOG(ERROR) << "Failed to open zip file at " << apkFullPath; |
| 1196 | return false; |
| 1197 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1198 | |
| 1199 | // Need a shared pointer: will be passing it into all unpacking jobs. |
| 1200 | std::shared_ptr<ZipArchive> zipFile(zipFileHandle, [](ZipArchiveHandle h) { CloseArchive(h); }); |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1201 | void* cookie = nullptr; |
| 1202 | const auto libFilePrefix = path::join(constants().libDir, abi); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1203 | if (StartIteration(zipFile.get(), &cookie, libFilePrefix, constants().libSuffix)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1204 | LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath; |
| 1205 | return false; |
| 1206 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1207 | auto endIteration = [](void* cookie) { EndIteration(cookie); }; |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1208 | auto iterationCleaner = std::unique_ptr<void, decltype(endIteration)>(cookie, endIteration); |
| 1209 | |
| 1210 | auto openZipTs = Clock::now(); |
| 1211 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1212 | std::vector<Job> jobQueue; |
| 1213 | ZipEntry entry; |
| 1214 | std::string_view fileName; |
| 1215 | while (!Next(cookie, &entry, &fileName)) { |
| 1216 | if (fileName.empty()) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1217 | continue; |
| 1218 | } |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1219 | |
| 1220 | auto startFileTs = Clock::now(); |
| 1221 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1222 | const auto libName = path::basename(fileName); |
| 1223 | const auto targetLibPath = path::join(libDirRelativePath, libName); |
| 1224 | const auto targetLibPathAbsolute = normalizePathToStorage(ifs, storage, targetLibPath); |
| 1225 | // If the extract file already exists, skip |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1226 | if (access(targetLibPathAbsolute.c_str(), F_OK) == 0) { |
| 1227 | if (sEnablePerfLogging) { |
| 1228 | LOG(INFO) << "incfs: Native lib file already exists: " << targetLibPath |
| 1229 | << "; skipping extraction, spent " |
| 1230 | << elapsedMcs(startFileTs, Clock::now()) << "mcs"; |
| 1231 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1232 | continue; |
| 1233 | } |
| 1234 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1235 | // Create new lib file without signature info |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1236 | incfs::NewFileParams libFileParams = { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1237 | .size = entry.uncompressed_length, |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1238 | .signature = {}, |
| 1239 | // Metadata of the new lib file is its relative path |
| 1240 | .metadata = {targetLibPath.c_str(), (IncFsSize)targetLibPath.size()}, |
| 1241 | }; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1242 | incfs::FileId libFileId = idFromMetadata(targetLibPath); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1243 | if (auto res = mIncFs->makeFile(ifs->control, targetLibPathAbsolute, 0777, libFileId, |
| 1244 | libFileParams)) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1245 | LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1246 | // If one lib file fails to be created, abort others as well |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1247 | return false; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1248 | } |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1249 | |
| 1250 | auto makeFileTs = Clock::now(); |
| 1251 | |
Songchun Fan | afaf6e9 | 2020-03-18 14:12:20 -0700 | [diff] [blame] | 1252 | // If it is a zero-byte file, skip data writing |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1253 | if (entry.uncompressed_length == 0) { |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1254 | if (sEnablePerfLogging) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1255 | LOG(INFO) << "incfs: Extracted " << libName |
| 1256 | << "(0 bytes): " << elapsedMcs(startFileTs, makeFileTs) << "mcs"; |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1257 | } |
Songchun Fan | afaf6e9 | 2020-03-18 14:12:20 -0700 | [diff] [blame] | 1258 | continue; |
| 1259 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1260 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 1261 | jobQueue.emplace_back([this, zipFile, entry, ifs = std::weak_ptr<IncFsMount>(ifs), |
| 1262 | libFileId, libPath = std::move(targetLibPath), |
| 1263 | makeFileTs]() mutable { |
| 1264 | extractZipFile(ifs.lock(), zipFile.get(), entry, libFileId, libPath, makeFileTs); |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1265 | }); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1266 | |
| 1267 | if (sEnablePerfLogging) { |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1268 | auto prepareJobTs = Clock::now(); |
| 1269 | LOG(INFO) << "incfs: Processed " << libName << ": " |
| 1270 | << elapsedMcs(startFileTs, prepareJobTs) |
| 1271 | << "mcs, make file: " << elapsedMcs(startFileTs, makeFileTs) |
| 1272 | << " prepare job: " << elapsedMcs(makeFileTs, prepareJobTs); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1273 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1274 | } |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1275 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1276 | auto processedTs = Clock::now(); |
| 1277 | |
| 1278 | if (!jobQueue.empty()) { |
| 1279 | { |
| 1280 | std::lock_guard lock(mJobMutex); |
| 1281 | if (mRunning) { |
| 1282 | auto& existingJobs = mJobQueue[storage]; |
| 1283 | if (existingJobs.empty()) { |
| 1284 | existingJobs = std::move(jobQueue); |
| 1285 | } else { |
| 1286 | existingJobs.insert(existingJobs.end(), std::move_iterator(jobQueue.begin()), |
| 1287 | std::move_iterator(jobQueue.end())); |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | mJobCondition.notify_all(); |
| 1292 | } |
| 1293 | |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1294 | if (sEnablePerfLogging) { |
| 1295 | auto end = Clock::now(); |
| 1296 | LOG(INFO) << "incfs: configureNativeBinaries complete in " << elapsedMcs(start, end) |
| 1297 | << "mcs, make dirs: " << elapsedMcs(start, mkDirsTs) |
| 1298 | << " open zip: " << elapsedMcs(mkDirsTs, openZipTs) |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1299 | << " make files: " << elapsedMcs(openZipTs, processedTs) |
| 1300 | << " schedule jobs: " << elapsedMcs(processedTs, end); |
Yurii Zubrytskyi | 3787c9f | 2020-04-06 23:10:28 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | return true; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1306 | void IncrementalService::extractZipFile(const IfsMountPtr& ifs, ZipArchiveHandle zipFile, |
| 1307 | ZipEntry& entry, const incfs::FileId& libFileId, |
| 1308 | std::string_view targetLibPath, |
| 1309 | Clock::time_point scheduledTs) { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 1310 | if (!ifs) { |
| 1311 | LOG(INFO) << "Skipping zip file " << targetLibPath << " extraction for an expired mount"; |
| 1312 | return; |
| 1313 | } |
| 1314 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1315 | auto libName = path::basename(targetLibPath); |
| 1316 | auto startedTs = Clock::now(); |
| 1317 | |
| 1318 | // Write extracted data to new file |
| 1319 | // NOTE: don't zero-initialize memory, it may take a while for nothing |
| 1320 | auto libData = std::unique_ptr<uint8_t[]>(new uint8_t[entry.uncompressed_length]); |
| 1321 | if (ExtractToMemory(zipFile, &entry, libData.get(), entry.uncompressed_length)) { |
| 1322 | LOG(ERROR) << "Failed to extract native lib zip entry: " << libName; |
| 1323 | return; |
| 1324 | } |
| 1325 | |
| 1326 | auto extractFileTs = Clock::now(); |
| 1327 | |
| 1328 | const auto writeFd = mIncFs->openForSpecialOps(ifs->control, libFileId); |
| 1329 | if (!writeFd.ok()) { |
| 1330 | LOG(ERROR) << "Failed to open write fd for: " << targetLibPath << " errno: " << writeFd; |
| 1331 | return; |
| 1332 | } |
| 1333 | |
| 1334 | auto openFileTs = Clock::now(); |
| 1335 | const int numBlocks = |
| 1336 | (entry.uncompressed_length + constants().blockSize - 1) / constants().blockSize; |
| 1337 | std::vector<IncFsDataBlock> instructions(numBlocks); |
| 1338 | auto remainingData = std::span(libData.get(), entry.uncompressed_length); |
| 1339 | for (int i = 0; i < numBlocks; i++) { |
| 1340 | const auto blockSize = std::min<uint16_t>(constants().blockSize, remainingData.size()); |
| 1341 | instructions[i] = IncFsDataBlock{ |
| 1342 | .fileFd = writeFd.get(), |
| 1343 | .pageIndex = static_cast<IncFsBlockIndex>(i), |
| 1344 | .compression = INCFS_COMPRESSION_KIND_NONE, |
| 1345 | .kind = INCFS_BLOCK_KIND_DATA, |
| 1346 | .dataSize = blockSize, |
| 1347 | .data = reinterpret_cast<const char*>(remainingData.data()), |
| 1348 | }; |
| 1349 | remainingData = remainingData.subspan(blockSize); |
| 1350 | } |
| 1351 | auto prepareInstsTs = Clock::now(); |
| 1352 | |
| 1353 | size_t res = mIncFs->writeBlocks(instructions); |
| 1354 | if (res != instructions.size()) { |
| 1355 | LOG(ERROR) << "Failed to write data into: " << targetLibPath; |
| 1356 | return; |
| 1357 | } |
| 1358 | |
| 1359 | if (sEnablePerfLogging) { |
| 1360 | auto endFileTs = Clock::now(); |
| 1361 | LOG(INFO) << "incfs: Extracted " << libName << "(" << entry.compressed_length << " -> " |
| 1362 | << entry.uncompressed_length << " bytes): " << elapsedMcs(startedTs, endFileTs) |
| 1363 | << "mcs, scheduling delay: " << elapsedMcs(scheduledTs, startedTs) |
| 1364 | << " extract: " << elapsedMcs(startedTs, extractFileTs) |
| 1365 | << " open: " << elapsedMcs(extractFileTs, openFileTs) |
| 1366 | << " prepare: " << elapsedMcs(openFileTs, prepareInstsTs) |
| 1367 | << " write: " << elapsedMcs(prepareInstsTs, endFileTs); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | bool IncrementalService::waitForNativeBinariesExtraction(StorageId storage) { |
| 1372 | std::unique_lock lock(mJobMutex); |
| 1373 | mJobCondition.wait(lock, [this, storage] { |
| 1374 | return !mRunning || |
| 1375 | (mPendingJobsStorage != storage && mJobQueue.find(storage) == mJobQueue.end()); |
| 1376 | }); |
| 1377 | return mPendingJobsStorage != storage && mJobQueue.find(storage) == mJobQueue.end(); |
| 1378 | } |
| 1379 | |
| 1380 | void IncrementalService::runJobProcessing() { |
| 1381 | for (;;) { |
| 1382 | std::unique_lock lock(mJobMutex); |
| 1383 | mJobCondition.wait(lock, [this]() { return !mRunning || !mJobQueue.empty(); }); |
| 1384 | if (!mRunning) { |
| 1385 | return; |
| 1386 | } |
| 1387 | |
| 1388 | auto it = mJobQueue.begin(); |
| 1389 | mPendingJobsStorage = it->first; |
| 1390 | auto queue = std::move(it->second); |
| 1391 | mJobQueue.erase(it); |
| 1392 | lock.unlock(); |
| 1393 | |
| 1394 | for (auto&& job : queue) { |
| 1395 | job(); |
| 1396 | } |
| 1397 | |
| 1398 | lock.lock(); |
| 1399 | mPendingJobsStorage = kInvalidStorageId; |
| 1400 | lock.unlock(); |
| 1401 | mJobCondition.notify_all(); |
| 1402 | } |
| 1403 | } |
| 1404 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1405 | void IncrementalService::registerAppOpsCallback(const std::string& packageName) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 1406 | sp<IAppOpsCallback> listener; |
| 1407 | { |
| 1408 | std::unique_lock lock{mCallbacksLock}; |
| 1409 | auto& cb = mCallbackRegistered[packageName]; |
| 1410 | if (cb) { |
| 1411 | return; |
| 1412 | } |
| 1413 | cb = new AppOpsListener(*this, packageName); |
| 1414 | listener = cb; |
| 1415 | } |
| 1416 | |
Yurii Zubrytskyi | da20801 | 2020-04-07 15:35:21 -0700 | [diff] [blame] | 1417 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_GET_USAGE_STATS, |
| 1418 | String16(packageName.c_str()), listener); |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | bool IncrementalService::unregisterAppOpsCallback(const std::string& packageName) { |
| 1422 | sp<IAppOpsCallback> listener; |
| 1423 | { |
| 1424 | std::unique_lock lock{mCallbacksLock}; |
| 1425 | auto found = mCallbackRegistered.find(packageName); |
| 1426 | if (found == mCallbackRegistered.end()) { |
| 1427 | return false; |
| 1428 | } |
| 1429 | listener = found->second; |
| 1430 | mCallbackRegistered.erase(found); |
| 1431 | } |
| 1432 | |
| 1433 | mAppOpsManager->stopWatchingMode(listener); |
| 1434 | return true; |
| 1435 | } |
| 1436 | |
| 1437 | void IncrementalService::onAppOpChanged(const std::string& packageName) { |
| 1438 | if (!unregisterAppOpsCallback(packageName)) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1439 | return; |
| 1440 | } |
| 1441 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1442 | std::vector<IfsMountPtr> affected; |
| 1443 | { |
| 1444 | std::lock_guard l(mLock); |
| 1445 | affected.reserve(mMounts.size()); |
| 1446 | for (auto&& [id, ifs] : mMounts) { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1447 | if (ifs->mountId == id && ifs->dataLoaderStub->params().packageName == packageName) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1448 | affected.push_back(ifs); |
| 1449 | } |
| 1450 | } |
| 1451 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1452 | for (auto&& ifs : affected) { |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 1453 | applyStorageParams(*ifs, false); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1454 | } |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1457 | IncrementalService::DataLoaderStub::~DataLoaderStub() { |
| 1458 | CHECK(mStatus == -1 || mStatus == IDataLoaderStatusListener::DATA_LOADER_DESTROYED) |
| 1459 | << "Dataloader has to be destroyed prior to destructor: " << mId |
| 1460 | << ", status: " << mStatus; |
| 1461 | } |
| 1462 | |
| 1463 | bool IncrementalService::DataLoaderStub::create() { |
| 1464 | bool created = false; |
| 1465 | auto status = mService.mDataLoaderManager->initializeDataLoader(mId, mParams, mControl, this, |
| 1466 | &created); |
| 1467 | if (!status.isOk() || !created) { |
| 1468 | LOG(ERROR) << "Failed to create a data loader for mount " << mId; |
| 1469 | return false; |
| 1470 | } |
| 1471 | return true; |
| 1472 | } |
| 1473 | |
| 1474 | bool IncrementalService::DataLoaderStub::start() { |
| 1475 | if (mStatus != IDataLoaderStatusListener::DATA_LOADER_CREATED) { |
| 1476 | mStartRequested = true; |
| 1477 | return true; |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 1478 | } |
| 1479 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1480 | sp<IDataLoader> dataloader; |
| 1481 | auto status = mService.mDataLoaderManager->getDataLoader(mId, &dataloader); |
| 1482 | if (!status.isOk()) { |
| 1483 | return false; |
| 1484 | } |
| 1485 | if (!dataloader) { |
| 1486 | return false; |
| 1487 | } |
| 1488 | status = dataloader->start(mId); |
| 1489 | if (!status.isOk()) { |
| 1490 | return false; |
| 1491 | } |
| 1492 | return true; |
| 1493 | } |
| 1494 | |
| 1495 | void IncrementalService::DataLoaderStub::destroy() { |
| 1496 | mDestroyRequested = true; |
| 1497 | mService.mDataLoaderManager->destroyDataLoader(mId); |
| 1498 | } |
| 1499 | |
| 1500 | binder::Status IncrementalService::DataLoaderStub::onStatusChanged(MountId mountId, int newStatus) { |
| 1501 | if (mStatus == newStatus) { |
| 1502 | return binder::Status::ok(); |
| 1503 | } |
| 1504 | |
| 1505 | if (mListener) { |
| 1506 | // Give an external listener a chance to act before we destroy something. |
| 1507 | mListener->onStatusChanged(mountId, newStatus); |
| 1508 | } |
| 1509 | |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1510 | { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1511 | std::unique_lock l(mService.mLock); |
| 1512 | const auto& ifs = mService.getIfsLocked(mountId); |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1513 | if (!ifs) { |
Songchun Fan | 306b7df | 2020-03-17 12:37:07 -0700 | [diff] [blame] | 1514 | LOG(WARNING) << "Received data loader status " << int(newStatus) |
| 1515 | << " for unknown mount " << mountId; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1516 | return binder::Status::ok(); |
| 1517 | } |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1518 | mStatus = newStatus; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1519 | |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1520 | if (!mDestroyRequested && newStatus == IDataLoaderStatusListener::DATA_LOADER_DESTROYED) { |
| 1521 | mService.deleteStorageLocked(*ifs, std::move(l)); |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1522 | return binder::Status::ok(); |
| 1523 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1524 | } |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1525 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1526 | switch (newStatus) { |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1527 | case IDataLoaderStatusListener::DATA_LOADER_CREATED: { |
Alex Buynytskyy | 0ea4ff4 | 2020-04-09 17:25:42 -0700 | [diff] [blame] | 1528 | if (mStartRequested) { |
| 1529 | start(); |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1530 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1531 | break; |
| 1532 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1533 | case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1534 | break; |
| 1535 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1536 | case IDataLoaderStatusListener::DATA_LOADER_STARTED: { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1537 | break; |
| 1538 | } |
| 1539 | case IDataLoaderStatusListener::DATA_LOADER_STOPPED: { |
| 1540 | break; |
| 1541 | } |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 1542 | case IDataLoaderStatusListener::DATA_LOADER_IMAGE_READY: { |
| 1543 | break; |
| 1544 | } |
| 1545 | case IDataLoaderStatusListener::DATA_LOADER_IMAGE_NOT_READY: { |
| 1546 | break; |
| 1547 | } |
Alex Buynytskyy | 2cf1d18 | 2020-03-17 09:33:45 -0700 | [diff] [blame] | 1548 | case IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE: { |
| 1549 | // Nothing for now. Rely on externalListener to handle this. |
| 1550 | break; |
| 1551 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1552 | default: { |
| 1553 | LOG(WARNING) << "Unknown data loader status: " << newStatus |
| 1554 | << " for mount: " << mountId; |
| 1555 | break; |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | return binder::Status::ok(); |
| 1560 | } |
| 1561 | |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 1562 | void IncrementalService::AppOpsListener::opChanged(int32_t, const String16&) { |
| 1563 | incrementalService.onAppOpChanged(packageName); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
Alex Buynytskyy | f415679 | 2020-04-07 14:26:55 -0700 | [diff] [blame] | 1566 | binder::Status IncrementalService::IncrementalServiceConnector::setStorageParams( |
| 1567 | bool enableReadLogs, int32_t* _aidl_return) { |
| 1568 | *_aidl_return = incrementalService.setStorageParams(storage, enableReadLogs); |
| 1569 | return binder::Status::ok(); |
| 1570 | } |
| 1571 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1572 | } // namespace android::incremental |