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> |
| 23 | #include <android-base/properties.h> |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <android-base/strings.h> |
| 26 | #include <android/content/pm/IDataLoaderStatusListener.h> |
| 27 | #include <android/os/IVold.h> |
| 28 | #include <androidfw/ZipFileRO.h> |
| 29 | #include <androidfw/ZipUtils.h> |
| 30 | #include <binder/BinderService.h> |
| 31 | #include <binder/ParcelFileDescriptor.h> |
| 32 | #include <binder/Status.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <uuid/uuid.h> |
| 35 | #include <zlib.h> |
| 36 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 37 | #include <ctime> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 38 | #include <iterator> |
| 39 | #include <span> |
| 40 | #include <stack> |
| 41 | #include <thread> |
| 42 | #include <type_traits> |
| 43 | |
| 44 | #include "Metadata.pb.h" |
| 45 | |
| 46 | using namespace std::literals; |
| 47 | using namespace android::content::pm; |
| 48 | |
| 49 | namespace android::incremental { |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | using IncrementalFileSystemControlParcel = |
| 54 | ::android::os::incremental::IncrementalFileSystemControlParcel; |
| 55 | |
| 56 | struct Constants { |
| 57 | static constexpr auto backing = "backing_store"sv; |
| 58 | static constexpr auto mount = "mount"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 59 | static constexpr auto storagePrefix = "st"sv; |
| 60 | static constexpr auto mountpointMdPrefix = ".mountpoint."sv; |
| 61 | static constexpr auto infoMdName = ".info"sv; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 62 | static constexpr auto libDir = "lib"sv; |
| 63 | static constexpr auto libSuffix = ".so"sv; |
| 64 | static constexpr auto blockSize = 4096; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static const Constants& constants() { |
| 68 | static Constants c; |
| 69 | return c; |
| 70 | } |
| 71 | |
| 72 | template <base::LogSeverity level = base::ERROR> |
| 73 | bool mkdirOrLog(std::string_view name, int mode = 0770, bool allowExisting = true) { |
| 74 | auto cstr = path::c_str(name); |
| 75 | if (::mkdir(cstr, mode)) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 76 | if (!allowExisting || errno != EEXIST) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 77 | PLOG(level) << "Can't create directory '" << name << '\''; |
| 78 | return false; |
| 79 | } |
| 80 | struct stat st; |
| 81 | if (::stat(cstr, &st) || !S_ISDIR(st.st_mode)) { |
| 82 | PLOG(level) << "Path exists but is not a directory: '" << name << '\''; |
| 83 | return false; |
| 84 | } |
| 85 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 86 | if (::chmod(cstr, mode)) { |
| 87 | PLOG(level) << "Changing permission failed for '" << name << '\''; |
| 88 | return false; |
| 89 | } |
| 90 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
| 94 | static std::string toMountKey(std::string_view path) { |
| 95 | if (path.empty()) { |
| 96 | return "@none"; |
| 97 | } |
| 98 | if (path == "/"sv) { |
| 99 | return "@root"; |
| 100 | } |
| 101 | if (path::isAbsolute(path)) { |
| 102 | path.remove_prefix(1); |
| 103 | } |
| 104 | std::string res(path); |
| 105 | std::replace(res.begin(), res.end(), '/', '_'); |
| 106 | std::replace(res.begin(), res.end(), '@', '_'); |
| 107 | return res; |
| 108 | } |
| 109 | |
| 110 | static std::pair<std::string, std::string> makeMountDir(std::string_view incrementalDir, |
| 111 | std::string_view path) { |
| 112 | auto mountKey = toMountKey(path); |
| 113 | const auto prefixSize = mountKey.size(); |
| 114 | for (int counter = 0; counter < 1000; |
| 115 | mountKey.resize(prefixSize), base::StringAppendF(&mountKey, "%d", counter++)) { |
| 116 | auto mountRoot = path::join(incrementalDir, mountKey); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 117 | if (mkdirOrLog(mountRoot, 0777, false)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 118 | return {mountKey, mountRoot}; |
| 119 | } |
| 120 | } |
| 121 | return {}; |
| 122 | } |
| 123 | |
| 124 | template <class ProtoMessage, class Control> |
| 125 | static ProtoMessage parseFromIncfs(const IncFsWrapper* incfs, Control&& control, |
| 126 | std::string_view path) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 127 | auto md = incfs->getMetadata(control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 128 | ProtoMessage message; |
| 129 | return message.ParseFromArray(md.data(), md.size()) ? message : ProtoMessage{}; |
| 130 | } |
| 131 | |
| 132 | static bool isValidMountTarget(std::string_view path) { |
| 133 | return path::isAbsolute(path) && path::isEmptyDir(path).value_or(true); |
| 134 | } |
| 135 | |
| 136 | std::string makeBindMdName() { |
| 137 | static constexpr auto uuidStringSize = 36; |
| 138 | |
| 139 | uuid_t guid; |
| 140 | uuid_generate(guid); |
| 141 | |
| 142 | std::string name; |
| 143 | const auto prefixSize = constants().mountpointMdPrefix.size(); |
| 144 | name.reserve(prefixSize + uuidStringSize); |
| 145 | |
| 146 | name = constants().mountpointMdPrefix; |
| 147 | name.resize(prefixSize + uuidStringSize); |
| 148 | uuid_unparse(guid, name.data() + prefixSize); |
| 149 | |
| 150 | return name; |
| 151 | } |
| 152 | } // namespace |
| 153 | |
| 154 | IncrementalService::IncFsMount::~IncFsMount() { |
| 155 | incrementalService.mIncrementalManager->destroyDataLoader(mountId); |
| 156 | control.reset(); |
| 157 | LOG(INFO) << "Unmounting and cleaning up mount " << mountId << " with root '" << root << '\''; |
| 158 | for (auto&& [target, _] : bindPoints) { |
| 159 | LOG(INFO) << "\tbind: " << target; |
| 160 | incrementalService.mVold->unmountIncFs(target); |
| 161 | } |
| 162 | LOG(INFO) << "\troot: " << root; |
| 163 | incrementalService.mVold->unmountIncFs(path::join(root, constants().mount)); |
| 164 | cleanupFilesystem(root); |
| 165 | } |
| 166 | |
| 167 | auto IncrementalService::IncFsMount::makeStorage(StorageId id) -> StorageMap::iterator { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 168 | std::string name; |
| 169 | for (int no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), i = 0; |
| 170 | i < 1024 && no >= 0; no = nextStorageDirNo.fetch_add(1, std::memory_order_relaxed), ++i) { |
| 171 | name.clear(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 172 | base::StringAppendF(&name, "%.*s_%d_%d", int(constants().storagePrefix.size()), |
| 173 | constants().storagePrefix.data(), id, no); |
| 174 | auto fullName = path::join(root, constants().mount, name); |
Songchun Fan | 9610093 | 2020-02-03 19:20:58 -0800 | [diff] [blame] | 175 | if (auto err = incrementalService.mIncFs->makeDir(control, fullName, 0755); !err) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 176 | std::lock_guard l(lock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 177 | return storages.insert_or_assign(id, Storage{std::move(fullName)}).first; |
| 178 | } else if (err != EEXIST) { |
| 179 | LOG(ERROR) << __func__ << "(): failed to create dir |" << fullName << "| " << err; |
| 180 | break; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | nextStorageDirNo = 0; |
| 184 | return storages.end(); |
| 185 | } |
| 186 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 187 | static std::unique_ptr<DIR, decltype(&::closedir)> openDir(const char* path) { |
| 188 | return {::opendir(path), ::closedir}; |
| 189 | } |
| 190 | |
| 191 | static int rmDirContent(const char* path) { |
| 192 | auto dir = openDir(path); |
| 193 | if (!dir) { |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | while (auto entry = ::readdir(dir.get())) { |
| 197 | if (entry->d_name == "."sv || entry->d_name == ".."sv) { |
| 198 | continue; |
| 199 | } |
| 200 | auto fullPath = android::base::StringPrintf("%s/%s", path, entry->d_name); |
| 201 | if (entry->d_type == DT_DIR) { |
| 202 | if (const auto err = rmDirContent(fullPath.c_str()); err != 0) { |
| 203 | PLOG(WARNING) << "Failed to delete " << fullPath << " content"; |
| 204 | return err; |
| 205 | } |
| 206 | if (const auto err = ::rmdir(fullPath.c_str()); err != 0) { |
| 207 | PLOG(WARNING) << "Failed to rmdir " << fullPath; |
| 208 | return err; |
| 209 | } |
| 210 | } else { |
| 211 | if (const auto err = ::unlink(fullPath.c_str()); err != 0) { |
| 212 | PLOG(WARNING) << "Failed to delete " << fullPath; |
| 213 | return err; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 220 | void IncrementalService::IncFsMount::cleanupFilesystem(std::string_view root) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 221 | rmDirContent(path::join(root, constants().backing).c_str()); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 222 | ::rmdir(path::join(root, constants().backing).c_str()); |
| 223 | ::rmdir(path::join(root, constants().mount).c_str()); |
| 224 | ::rmdir(path::c_str(root)); |
| 225 | } |
| 226 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 227 | IncrementalService::IncrementalService(ServiceManagerWrapper&& sm, std::string_view rootDir) |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 228 | : mVold(sm.getVoldService()), |
| 229 | mIncrementalManager(sm.getIncrementalManager()), |
| 230 | mIncFs(sm.getIncFs()), |
| 231 | mIncrementalDir(rootDir) { |
| 232 | if (!mVold) { |
| 233 | LOG(FATAL) << "Vold service is unavailable"; |
| 234 | } |
| 235 | if (!mIncrementalManager) { |
| 236 | LOG(FATAL) << "IncrementalManager service is unavailable"; |
| 237 | } |
| 238 | // TODO(b/136132412): check that root dir should already exist |
| 239 | // TODO(b/136132412): enable mount existing dirs after SELinux rules are merged |
| 240 | // mountExistingImages(); |
| 241 | } |
| 242 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 243 | FileId IncrementalService::idFromMetadata(std::span<const uint8_t> metadata) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame^] | 244 | return IncFs_FileIdFromMetadata({(const char*)metadata.data(), metadata.size()}); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 247 | IncrementalService::~IncrementalService() = default; |
| 248 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 249 | inline const char* toString(TimePoint t) { |
| 250 | using SystemClock = std::chrono::system_clock; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 251 | time_t time = SystemClock::to_time_t( |
| 252 | SystemClock::now() + |
| 253 | std::chrono::duration_cast<SystemClock::duration>(t - Clock::now())); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 254 | return std::ctime(&time); |
| 255 | } |
| 256 | |
| 257 | inline const char* toString(IncrementalService::BindKind kind) { |
| 258 | switch (kind) { |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 259 | case IncrementalService::BindKind::Temporary: |
| 260 | return "Temporary"; |
| 261 | case IncrementalService::BindKind::Permanent: |
| 262 | return "Permanent"; |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | void IncrementalService::onDump(int fd) { |
| 267 | dprintf(fd, "Incremental is %s\n", incfs::enabled() ? "ENABLED" : "DISABLED"); |
| 268 | dprintf(fd, "Incremental dir: %s\n", mIncrementalDir.c_str()); |
| 269 | |
| 270 | std::unique_lock l(mLock); |
| 271 | |
| 272 | dprintf(fd, "Mounts (%d):\n", int(mMounts.size())); |
| 273 | for (auto&& [id, ifs] : mMounts) { |
| 274 | const IncFsMount& mnt = *ifs.get(); |
| 275 | dprintf(fd, "\t[%d]:\n", id); |
| 276 | dprintf(fd, "\t\tmountId: %d\n", mnt.mountId); |
| 277 | dprintf(fd, "\t\tnextStorageDirNo: %d\n", mnt.nextStorageDirNo.load()); |
| 278 | dprintf(fd, "\t\tdataLoaderStatus: %d\n", mnt.dataLoaderStatus.load()); |
| 279 | dprintf(fd, "\t\tconnectionLostTime: %s\n", toString(mnt.connectionLostTime)); |
| 280 | if (mnt.savedDataLoaderParams) { |
| 281 | const auto& params = mnt.savedDataLoaderParams.value(); |
| 282 | dprintf(fd, "\t\tsavedDataLoaderParams:\n"); |
| 283 | dprintf(fd, "\t\t\ttype: %s\n", toString(params.type).c_str()); |
| 284 | dprintf(fd, "\t\t\tpackageName: %s\n", params.packageName.c_str()); |
| 285 | dprintf(fd, "\t\t\tclassName: %s\n", params.className.c_str()); |
| 286 | dprintf(fd, "\t\t\targuments: %s\n", params.arguments.c_str()); |
| 287 | dprintf(fd, "\t\t\tdynamicArgs: %d\n", int(params.dynamicArgs.size())); |
| 288 | } |
| 289 | dprintf(fd, "\t\tstorages (%d):\n", int(mnt.storages.size())); |
| 290 | for (auto&& [storageId, storage] : mnt.storages) { |
| 291 | dprintf(fd, "\t\t\t[%d] -> [%s]\n", storageId, storage.name.c_str()); |
| 292 | } |
| 293 | |
| 294 | dprintf(fd, "\t\tbindPoints (%d):\n", int(mnt.bindPoints.size())); |
| 295 | for (auto&& [target, bind] : mnt.bindPoints) { |
| 296 | dprintf(fd, "\t\t\t[%s]->[%d]:\n", target.c_str(), bind.storage); |
| 297 | dprintf(fd, "\t\t\t\tsavedFilename: %s\n", bind.savedFilename.c_str()); |
| 298 | dprintf(fd, "\t\t\t\tsourceDir: %s\n", bind.sourceDir.c_str()); |
| 299 | dprintf(fd, "\t\t\t\tkind: %s\n", toString(bind.kind)); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | dprintf(fd, "Sorted binds (%d):\n", int(mBindsByPath.size())); |
| 304 | for (auto&& [target, mountPairIt] : mBindsByPath) { |
| 305 | const auto& bind = mountPairIt->second; |
| 306 | dprintf(fd, "\t\t[%s]->[%d]:\n", target.c_str(), bind.storage); |
| 307 | dprintf(fd, "\t\t\tsavedFilename: %s\n", bind.savedFilename.c_str()); |
| 308 | dprintf(fd, "\t\t\tsourceDir: %s\n", bind.sourceDir.c_str()); |
| 309 | dprintf(fd, "\t\t\tkind: %s\n", toString(bind.kind)); |
| 310 | } |
| 311 | } |
| 312 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 313 | std::optional<std::future<void>> IncrementalService::onSystemReady() { |
| 314 | std::promise<void> threadFinished; |
| 315 | if (mSystemReady.exchange(true)) { |
| 316 | return {}; |
| 317 | } |
| 318 | |
| 319 | std::vector<IfsMountPtr> mounts; |
| 320 | { |
| 321 | std::lock_guard l(mLock); |
| 322 | mounts.reserve(mMounts.size()); |
| 323 | for (auto&& [id, ifs] : mMounts) { |
| 324 | if (ifs->mountId == id) { |
| 325 | mounts.push_back(ifs); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | std::thread([this, mounts = std::move(mounts)]() { |
| 331 | std::vector<IfsMountPtr> failedLoaderMounts; |
| 332 | for (auto&& ifs : mounts) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame^] | 333 | if (prepareDataLoader(*ifs)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 334 | LOG(INFO) << "Successfully started data loader for mount " << ifs->mountId; |
| 335 | } else { |
| 336 | LOG(WARNING) << "Failed to start data loader for mount " << ifs->mountId; |
| 337 | failedLoaderMounts.push_back(std::move(ifs)); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | while (!failedLoaderMounts.empty()) { |
| 342 | LOG(WARNING) << "Deleting failed mount " << failedLoaderMounts.back()->mountId; |
| 343 | deleteStorage(*failedLoaderMounts.back()); |
| 344 | failedLoaderMounts.pop_back(); |
| 345 | } |
| 346 | mPrepareDataLoaders.set_value_at_thread_exit(); |
| 347 | }).detach(); |
| 348 | return mPrepareDataLoaders.get_future(); |
| 349 | } |
| 350 | |
| 351 | auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator { |
| 352 | for (;;) { |
| 353 | if (mNextId == kMaxStorageId) { |
| 354 | mNextId = 0; |
| 355 | } |
| 356 | auto id = ++mNextId; |
| 357 | auto [it, inserted] = mMounts.try_emplace(id, nullptr); |
| 358 | if (inserted) { |
| 359 | return it; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | StorageId IncrementalService::createStorage(std::string_view mountPoint, |
| 365 | DataLoaderParamsParcel&& dataLoaderParams, |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame^] | 366 | const DataLoaderStatusListener& dataLoaderStatusListener, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 367 | CreateOptions options) { |
| 368 | LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options); |
| 369 | if (!path::isAbsolute(mountPoint)) { |
| 370 | LOG(ERROR) << "path is not absolute: " << mountPoint; |
| 371 | return kInvalidStorageId; |
| 372 | } |
| 373 | |
| 374 | auto mountNorm = path::normalize(mountPoint); |
| 375 | { |
| 376 | const auto id = findStorageId(mountNorm); |
| 377 | if (id != kInvalidStorageId) { |
| 378 | if (options & CreateOptions::OpenExisting) { |
| 379 | LOG(INFO) << "Opened existing storage " << id; |
| 380 | return id; |
| 381 | } |
| 382 | LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id; |
| 383 | return kInvalidStorageId; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (!(options & CreateOptions::CreateNew)) { |
| 388 | LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint; |
| 389 | return kInvalidStorageId; |
| 390 | } |
| 391 | |
| 392 | if (!path::isEmptyDir(mountNorm)) { |
| 393 | LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm; |
| 394 | return kInvalidStorageId; |
| 395 | } |
| 396 | auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm); |
| 397 | if (mountRoot.empty()) { |
| 398 | LOG(ERROR) << "Bad mount point"; |
| 399 | return kInvalidStorageId; |
| 400 | } |
| 401 | // Make sure the code removes all crap it may create while still failing. |
| 402 | auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); }; |
| 403 | auto firstCleanupOnFailure = |
| 404 | std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup); |
| 405 | |
| 406 | auto mountTarget = path::join(mountRoot, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 407 | const auto backing = path::join(mountRoot, constants().backing); |
| 408 | if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 409 | return kInvalidStorageId; |
| 410 | } |
| 411 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 412 | IncFsMount::Control control; |
| 413 | { |
| 414 | std::lock_guard l(mMountOperationLock); |
| 415 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 416 | |
| 417 | if (auto err = rmDirContent(backing.c_str())) { |
| 418 | LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err; |
| 419 | return kInvalidStorageId; |
| 420 | } |
| 421 | if (!mkdirOrLog(path::join(backing, ".index"), 0777)) { |
| 422 | return kInvalidStorageId; |
| 423 | } |
| 424 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 425 | if (!status.isOk()) { |
| 426 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 427 | return kInvalidStorageId; |
| 428 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 429 | if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 || |
| 430 | controlParcel.log.get() < 0) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 431 | LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel."; |
| 432 | return kInvalidStorageId; |
| 433 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 434 | control.cmd = controlParcel.cmd.release().release(); |
| 435 | control.pendingReads = controlParcel.pendingReads.release().release(); |
| 436 | control.logs = controlParcel.log.release().release(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | std::unique_lock l(mLock); |
| 440 | const auto mountIt = getStorageSlotLocked(); |
| 441 | const auto mountId = mountIt->first; |
| 442 | l.unlock(); |
| 443 | |
| 444 | auto ifs = |
| 445 | std::make_shared<IncFsMount>(std::move(mountRoot), mountId, std::move(control), *this); |
| 446 | // Now it's the |ifs|'s responsibility to clean up after itself, and the only cleanup we need |
| 447 | // is the removal of the |ifs|. |
| 448 | firstCleanupOnFailure.release(); |
| 449 | |
| 450 | auto secondCleanup = [this, &l](auto itPtr) { |
| 451 | if (!l.owns_lock()) { |
| 452 | l.lock(); |
| 453 | } |
| 454 | mMounts.erase(*itPtr); |
| 455 | }; |
| 456 | auto secondCleanupOnFailure = |
| 457 | std::unique_ptr<decltype(mountIt), decltype(secondCleanup)>(&mountIt, secondCleanup); |
| 458 | |
| 459 | const auto storageIt = ifs->makeStorage(ifs->mountId); |
| 460 | if (storageIt == ifs->storages.end()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 461 | LOG(ERROR) << "Can't create a default storage directory"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 462 | return kInvalidStorageId; |
| 463 | } |
| 464 | |
| 465 | { |
| 466 | metadata::Mount m; |
| 467 | m.mutable_storage()->set_id(ifs->mountId); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 468 | m.mutable_loader()->set_type((int)dataLoaderParams.type); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 469 | m.mutable_loader()->set_package_name(dataLoaderParams.packageName); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 470 | m.mutable_loader()->set_class_name(dataLoaderParams.className); |
| 471 | m.mutable_loader()->set_arguments(dataLoaderParams.arguments); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 472 | const auto metadata = m.SerializeAsString(); |
| 473 | m.mutable_loader()->release_arguments(); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 474 | m.mutable_loader()->release_class_name(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 475 | m.mutable_loader()->release_package_name(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 476 | if (auto err = |
| 477 | mIncFs->makeFile(ifs->control, |
| 478 | path::join(ifs->root, constants().mount, |
| 479 | constants().infoMdName), |
| 480 | 0777, idFromMetadata(metadata), |
| 481 | {.metadata = {metadata.data(), (IncFsSize)metadata.size()}})) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 482 | LOG(ERROR) << "Saving mount metadata failed: " << -err; |
| 483 | return kInvalidStorageId; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | const auto bk = |
| 488 | (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 489 | if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name, |
| 490 | std::string(storageIt->second.name), std::move(mountNorm), bk, l); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 491 | err < 0) { |
| 492 | LOG(ERROR) << "adding bind mount failed: " << -err; |
| 493 | return kInvalidStorageId; |
| 494 | } |
| 495 | |
| 496 | // Done here as well, all data structures are in good state. |
| 497 | secondCleanupOnFailure.release(); |
| 498 | |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame^] | 499 | if (!prepareDataLoader(*ifs, &dataLoaderParams, &dataLoaderStatusListener)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 500 | LOG(ERROR) << "prepareDataLoader() failed"; |
| 501 | deleteStorageLocked(*ifs, std::move(l)); |
| 502 | return kInvalidStorageId; |
| 503 | } |
| 504 | |
| 505 | mountIt->second = std::move(ifs); |
| 506 | l.unlock(); |
| 507 | LOG(INFO) << "created storage " << mountId; |
| 508 | return mountId; |
| 509 | } |
| 510 | |
| 511 | StorageId IncrementalService::createLinkedStorage(std::string_view mountPoint, |
| 512 | StorageId linkedStorage, |
| 513 | IncrementalService::CreateOptions options) { |
| 514 | if (!isValidMountTarget(mountPoint)) { |
| 515 | LOG(ERROR) << "Mount point is invalid or missing"; |
| 516 | return kInvalidStorageId; |
| 517 | } |
| 518 | |
| 519 | std::unique_lock l(mLock); |
| 520 | const auto& ifs = getIfsLocked(linkedStorage); |
| 521 | if (!ifs) { |
| 522 | LOG(ERROR) << "Ifs unavailable"; |
| 523 | return kInvalidStorageId; |
| 524 | } |
| 525 | |
| 526 | const auto mountIt = getStorageSlotLocked(); |
| 527 | const auto storageId = mountIt->first; |
| 528 | const auto storageIt = ifs->makeStorage(storageId); |
| 529 | if (storageIt == ifs->storages.end()) { |
| 530 | LOG(ERROR) << "Can't create a new storage"; |
| 531 | mMounts.erase(mountIt); |
| 532 | return kInvalidStorageId; |
| 533 | } |
| 534 | |
| 535 | l.unlock(); |
| 536 | |
| 537 | const auto bk = |
| 538 | (options & CreateOptions::PermanentBind) ? BindKind::Permanent : BindKind::Temporary; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 539 | if (auto err = addBindMount(*ifs, storageIt->first, storageIt->second.name, |
| 540 | std::string(storageIt->second.name), path::normalize(mountPoint), |
| 541 | bk, l); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 542 | err < 0) { |
| 543 | LOG(ERROR) << "bindMount failed with error: " << err; |
| 544 | return kInvalidStorageId; |
| 545 | } |
| 546 | |
| 547 | mountIt->second = ifs; |
| 548 | return storageId; |
| 549 | } |
| 550 | |
| 551 | IncrementalService::BindPathMap::const_iterator IncrementalService::findStorageLocked( |
| 552 | std::string_view path) const { |
| 553 | auto bindPointIt = mBindsByPath.upper_bound(path); |
| 554 | if (bindPointIt == mBindsByPath.begin()) { |
| 555 | return mBindsByPath.end(); |
| 556 | } |
| 557 | --bindPointIt; |
| 558 | if (!path::startsWith(path, bindPointIt->first)) { |
| 559 | return mBindsByPath.end(); |
| 560 | } |
| 561 | return bindPointIt; |
| 562 | } |
| 563 | |
| 564 | StorageId IncrementalService::findStorageId(std::string_view path) const { |
| 565 | std::lock_guard l(mLock); |
| 566 | auto it = findStorageLocked(path); |
| 567 | if (it == mBindsByPath.end()) { |
| 568 | return kInvalidStorageId; |
| 569 | } |
| 570 | return it->second->second.storage; |
| 571 | } |
| 572 | |
| 573 | void IncrementalService::deleteStorage(StorageId storageId) { |
| 574 | const auto ifs = getIfs(storageId); |
| 575 | if (!ifs) { |
| 576 | return; |
| 577 | } |
| 578 | deleteStorage(*ifs); |
| 579 | } |
| 580 | |
| 581 | void IncrementalService::deleteStorage(IncrementalService::IncFsMount& ifs) { |
| 582 | std::unique_lock l(ifs.lock); |
| 583 | deleteStorageLocked(ifs, std::move(l)); |
| 584 | } |
| 585 | |
| 586 | void IncrementalService::deleteStorageLocked(IncrementalService::IncFsMount& ifs, |
| 587 | std::unique_lock<std::mutex>&& ifsLock) { |
| 588 | const auto storages = std::move(ifs.storages); |
| 589 | // Don't move the bind points out: Ifs's dtor will use them to unmount everything. |
| 590 | const auto bindPoints = ifs.bindPoints; |
| 591 | ifsLock.unlock(); |
| 592 | |
| 593 | std::lock_guard l(mLock); |
| 594 | for (auto&& [id, _] : storages) { |
| 595 | if (id != ifs.mountId) { |
| 596 | mMounts.erase(id); |
| 597 | } |
| 598 | } |
| 599 | for (auto&& [path, _] : bindPoints) { |
| 600 | mBindsByPath.erase(path); |
| 601 | } |
| 602 | mMounts.erase(ifs.mountId); |
| 603 | } |
| 604 | |
| 605 | StorageId IncrementalService::openStorage(std::string_view pathInMount) { |
| 606 | if (!path::isAbsolute(pathInMount)) { |
| 607 | return kInvalidStorageId; |
| 608 | } |
| 609 | |
| 610 | return findStorageId(path::normalize(pathInMount)); |
| 611 | } |
| 612 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 613 | FileId IncrementalService::nodeFor(StorageId storage, std::string_view subpath) const { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 614 | const auto ifs = getIfs(storage); |
| 615 | if (!ifs) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 616 | return kIncFsInvalidFileId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 617 | } |
| 618 | std::unique_lock l(ifs->lock); |
| 619 | auto storageIt = ifs->storages.find(storage); |
| 620 | if (storageIt == ifs->storages.end()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 621 | return kIncFsInvalidFileId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 622 | } |
| 623 | if (subpath.empty() || subpath == "."sv) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 624 | return kIncFsInvalidFileId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 625 | } |
| 626 | auto path = path::join(ifs->root, constants().mount, storageIt->second.name, subpath); |
| 627 | l.unlock(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 628 | return mIncFs->getFileId(ifs->control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 631 | std::pair<FileId, std::string_view> IncrementalService::parentAndNameFor( |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 632 | StorageId storage, std::string_view subpath) const { |
| 633 | auto name = path::basename(subpath); |
| 634 | if (name.empty()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 635 | return {kIncFsInvalidFileId, {}}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 636 | } |
| 637 | auto dir = path::dirname(subpath); |
| 638 | if (dir.empty() || dir == "/"sv) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 639 | return {kIncFsInvalidFileId, {}}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 640 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 641 | auto id = nodeFor(storage, dir); |
| 642 | return {id, name}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | IncrementalService::IfsMountPtr IncrementalService::getIfs(StorageId storage) const { |
| 646 | std::lock_guard l(mLock); |
| 647 | return getIfsLocked(storage); |
| 648 | } |
| 649 | |
| 650 | const IncrementalService::IfsMountPtr& IncrementalService::getIfsLocked(StorageId storage) const { |
| 651 | auto it = mMounts.find(storage); |
| 652 | if (it == mMounts.end()) { |
| 653 | static const IfsMountPtr kEmpty = {}; |
| 654 | return kEmpty; |
| 655 | } |
| 656 | return it->second; |
| 657 | } |
| 658 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 659 | int IncrementalService::bind(StorageId storage, std::string_view source, std::string_view target, |
| 660 | BindKind kind) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 661 | if (!isValidMountTarget(target)) { |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | |
| 665 | const auto ifs = getIfs(storage); |
| 666 | if (!ifs) { |
| 667 | return -EINVAL; |
| 668 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 669 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 670 | std::unique_lock l(ifs->lock); |
| 671 | const auto storageInfo = ifs->storages.find(storage); |
| 672 | if (storageInfo == ifs->storages.end()) { |
| 673 | return -EINVAL; |
| 674 | } |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 675 | std::string normSource = normalizePathToStorage(ifs, storage, source); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 676 | l.unlock(); |
| 677 | std::unique_lock l2(mLock, std::defer_lock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 678 | return addBindMount(*ifs, storage, storageInfo->second.name, std::move(normSource), |
| 679 | path::normalize(target), kind, l2); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | int IncrementalService::unbind(StorageId storage, std::string_view target) { |
| 683 | if (!path::isAbsolute(target)) { |
| 684 | return -EINVAL; |
| 685 | } |
| 686 | |
| 687 | LOG(INFO) << "Removing bind point " << target; |
| 688 | |
| 689 | // Here we should only look up by the exact target, not by a subdirectory of any existing mount, |
| 690 | // otherwise there's a chance to unmount something completely unrelated |
| 691 | const auto norm = path::normalize(target); |
| 692 | std::unique_lock l(mLock); |
| 693 | const auto storageIt = mBindsByPath.find(norm); |
| 694 | if (storageIt == mBindsByPath.end() || storageIt->second->second.storage != storage) { |
| 695 | return -EINVAL; |
| 696 | } |
| 697 | const auto bindIt = storageIt->second; |
| 698 | const auto storageId = bindIt->second.storage; |
| 699 | const auto ifs = getIfsLocked(storageId); |
| 700 | if (!ifs) { |
| 701 | LOG(ERROR) << "Internal error: storageId " << storageId << " for bound path " << target |
| 702 | << " is missing"; |
| 703 | return -EFAULT; |
| 704 | } |
| 705 | mBindsByPath.erase(storageIt); |
| 706 | l.unlock(); |
| 707 | |
| 708 | mVold->unmountIncFs(bindIt->first); |
| 709 | std::unique_lock l2(ifs->lock); |
| 710 | if (ifs->bindPoints.size() <= 1) { |
| 711 | ifs->bindPoints.clear(); |
| 712 | deleteStorageLocked(*ifs, std::move(l2)); |
| 713 | } else { |
| 714 | const std::string savedFile = std::move(bindIt->second.savedFilename); |
| 715 | ifs->bindPoints.erase(bindIt); |
| 716 | l2.unlock(); |
| 717 | if (!savedFile.empty()) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 718 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, savedFile)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | return 0; |
| 722 | } |
| 723 | |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 724 | std::string IncrementalService::normalizePathToStorage(const IncrementalService::IfsMountPtr ifs, |
| 725 | StorageId storage, std::string_view path) { |
| 726 | const auto storageInfo = ifs->storages.find(storage); |
| 727 | if (storageInfo == ifs->storages.end()) { |
| 728 | return {}; |
| 729 | } |
| 730 | std::string normPath; |
| 731 | if (path::isAbsolute(path)) { |
| 732 | normPath = path::normalize(path); |
| 733 | } else { |
| 734 | normPath = path::normalize(path::join(storageInfo->second.name, path)); |
| 735 | } |
| 736 | if (!path::startsWith(normPath, storageInfo->second.name)) { |
| 737 | return {}; |
| 738 | } |
| 739 | return normPath; |
| 740 | } |
| 741 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 742 | int IncrementalService::makeFile(StorageId storage, std::string_view path, int mode, FileId id, |
| 743 | incfs::NewFileParams params) { |
| 744 | if (auto ifs = getIfs(storage)) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 745 | std::string normPath = normalizePathToStorage(ifs, storage, path); |
| 746 | if (normPath.empty()) { |
Songchun Fan | 54c6aed | 2020-01-31 16:52:41 -0800 | [diff] [blame] | 747 | return -EINVAL; |
| 748 | } |
| 749 | auto err = mIncFs->makeFile(ifs->control, normPath, mode, id, params); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 750 | if (err) { |
| 751 | return err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 752 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 753 | std::vector<uint8_t> metadataBytes; |
| 754 | if (params.metadata.data && params.metadata.size > 0) { |
| 755 | metadataBytes.assign(params.metadata.data, params.metadata.data + params.metadata.size); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 756 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 757 | return 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 758 | } |
| 759 | return -EINVAL; |
| 760 | } |
| 761 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 762 | int IncrementalService::makeDir(StorageId storageId, std::string_view path, int mode) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 763 | if (auto ifs = getIfs(storageId)) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 764 | std::string normPath = normalizePathToStorage(ifs, storageId, path); |
| 765 | if (normPath.empty()) { |
| 766 | return -EINVAL; |
| 767 | } |
| 768 | return mIncFs->makeDir(ifs->control, normPath, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 769 | } |
| 770 | return -EINVAL; |
| 771 | } |
| 772 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 773 | int IncrementalService::makeDirs(StorageId storageId, std::string_view path, int mode) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 774 | const auto ifs = getIfs(storageId); |
| 775 | if (!ifs) { |
| 776 | return -EINVAL; |
| 777 | } |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 778 | std::string normPath = normalizePathToStorage(ifs, storageId, path); |
| 779 | if (normPath.empty()) { |
| 780 | return -EINVAL; |
| 781 | } |
| 782 | auto err = mIncFs->makeDir(ifs->control, normPath, mode); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 783 | if (err == -EEXIST) { |
| 784 | return 0; |
| 785 | } else if (err != -ENOENT) { |
| 786 | return err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 787 | } |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 788 | if (auto err = makeDirs(storageId, path::dirname(normPath), mode)) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 789 | return err; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 790 | } |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 791 | return mIncFs->makeDir(ifs->control, normPath, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 792 | } |
| 793 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 794 | int IncrementalService::link(StorageId sourceStorageId, std::string_view oldPath, |
| 795 | StorageId destStorageId, std::string_view newPath) { |
| 796 | if (auto ifsSrc = getIfs(sourceStorageId), ifsDest = getIfs(destStorageId); |
| 797 | ifsSrc && ifsSrc == ifsDest) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 798 | std::string normOldPath = normalizePathToStorage(ifsSrc, sourceStorageId, oldPath); |
| 799 | std::string normNewPath = normalizePathToStorage(ifsDest, destStorageId, newPath); |
| 800 | if (normOldPath.empty() || normNewPath.empty()) { |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | return mIncFs->link(ifsSrc->control, normOldPath, normNewPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 804 | } |
| 805 | return -EINVAL; |
| 806 | } |
| 807 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 808 | int IncrementalService::unlink(StorageId storage, std::string_view path) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 809 | if (auto ifs = getIfs(storage)) { |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 810 | std::string normOldPath = normalizePathToStorage(ifs, storage, path); |
| 811 | return mIncFs->unlink(ifs->control, normOldPath); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 812 | } |
| 813 | return -EINVAL; |
| 814 | } |
| 815 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 816 | int IncrementalService::addBindMount(IncFsMount& ifs, StorageId storage, |
| 817 | std::string_view storageRoot, std::string&& source, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 818 | std::string&& target, BindKind kind, |
| 819 | std::unique_lock<std::mutex>& mainLock) { |
| 820 | if (!isValidMountTarget(target)) { |
| 821 | return -EINVAL; |
| 822 | } |
| 823 | |
| 824 | std::string mdFileName; |
| 825 | if (kind != BindKind::Temporary) { |
| 826 | metadata::BindPoint bp; |
| 827 | bp.set_storage_id(storage); |
| 828 | bp.set_allocated_dest_path(&target); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 829 | bp.set_source_subdir(std::string(path::relativize(storageRoot, source))); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 830 | const auto metadata = bp.SerializeAsString(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 831 | bp.release_dest_path(); |
| 832 | mdFileName = makeBindMdName(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 833 | auto node = |
| 834 | mIncFs->makeFile(ifs.control, path::join(ifs.root, constants().mount, mdFileName), |
| 835 | 0444, idFromMetadata(metadata), |
| 836 | {.metadata = {metadata.data(), (IncFsSize)metadata.size()}}); |
| 837 | if (node) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 838 | return int(node); |
| 839 | } |
| 840 | } |
| 841 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 842 | return addBindMountWithMd(ifs, storage, std::move(mdFileName), std::move(source), |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 843 | std::move(target), kind, mainLock); |
| 844 | } |
| 845 | |
| 846 | int IncrementalService::addBindMountWithMd(IncrementalService::IncFsMount& ifs, StorageId storage, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 847 | std::string&& metadataName, std::string&& source, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 848 | std::string&& target, BindKind kind, |
| 849 | std::unique_lock<std::mutex>& mainLock) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 850 | { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 851 | std::lock_guard l(mMountOperationLock); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 852 | const auto status = mVold->bindMount(source, target); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 853 | if (!status.isOk()) { |
| 854 | LOG(ERROR) << "Calling Vold::bindMount() failed: " << status.toString8(); |
| 855 | return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC |
| 856 | ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode() |
| 857 | : status.serviceSpecificErrorCode() == 0 |
| 858 | ? -EFAULT |
| 859 | : status.serviceSpecificErrorCode() |
| 860 | : -EIO; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | if (!mainLock.owns_lock()) { |
| 865 | mainLock.lock(); |
| 866 | } |
| 867 | std::lock_guard l(ifs.lock); |
| 868 | const auto [it, _] = |
| 869 | ifs.bindPoints.insert_or_assign(target, |
| 870 | IncFsMount::Bind{storage, std::move(metadataName), |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 871 | std::move(source), kind}); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 872 | mBindsByPath[std::move(target)] = it; |
| 873 | return 0; |
| 874 | } |
| 875 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 876 | RawMetadata IncrementalService::getMetadata(StorageId storage, FileId node) const { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 877 | const auto ifs = getIfs(storage); |
| 878 | if (!ifs) { |
| 879 | return {}; |
| 880 | } |
| 881 | return mIncFs->getMetadata(ifs->control, node); |
| 882 | } |
| 883 | |
| 884 | std::vector<std::string> IncrementalService::listFiles(StorageId storage) const { |
| 885 | const auto ifs = getIfs(storage); |
| 886 | if (!ifs) { |
| 887 | return {}; |
| 888 | } |
| 889 | |
| 890 | std::unique_lock l(ifs->lock); |
| 891 | auto subdirIt = ifs->storages.find(storage); |
| 892 | if (subdirIt == ifs->storages.end()) { |
| 893 | return {}; |
| 894 | } |
| 895 | auto dir = path::join(ifs->root, constants().mount, subdirIt->second.name); |
| 896 | l.unlock(); |
| 897 | |
| 898 | const auto prefixSize = dir.size() + 1; |
| 899 | std::vector<std::string> todoDirs{std::move(dir)}; |
| 900 | std::vector<std::string> result; |
| 901 | do { |
| 902 | auto currDir = std::move(todoDirs.back()); |
| 903 | todoDirs.pop_back(); |
| 904 | |
| 905 | auto d = |
| 906 | std::unique_ptr<DIR, decltype(&::closedir)>(::opendir(currDir.c_str()), ::closedir); |
| 907 | while (auto e = ::readdir(d.get())) { |
| 908 | if (e->d_type == DT_REG) { |
| 909 | result.emplace_back( |
| 910 | path::join(std::string_view(currDir).substr(prefixSize), e->d_name)); |
| 911 | continue; |
| 912 | } |
| 913 | if (e->d_type == DT_DIR) { |
| 914 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 915 | continue; |
| 916 | } |
| 917 | todoDirs.emplace_back(path::join(currDir, e->d_name)); |
| 918 | continue; |
| 919 | } |
| 920 | } |
| 921 | } while (!todoDirs.empty()); |
| 922 | return result; |
| 923 | } |
| 924 | |
| 925 | bool IncrementalService::startLoading(StorageId storage) const { |
| 926 | const auto ifs = getIfs(storage); |
| 927 | if (!ifs) { |
| 928 | return false; |
| 929 | } |
| 930 | bool started = false; |
| 931 | std::unique_lock l(ifs->lock); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 932 | if (ifs->dataLoaderStatus != IDataLoaderStatusListener::DATA_LOADER_CREATED) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 933 | if (ifs->dataLoaderReady.wait_for(l, Seconds(5)) == std::cv_status::timeout) { |
| 934 | LOG(ERROR) << "Timeout waiting for data loader to be ready"; |
| 935 | return false; |
| 936 | } |
| 937 | } |
| 938 | auto status = mIncrementalManager->startDataLoader(ifs->mountId, &started); |
| 939 | if (!status.isOk()) { |
| 940 | return false; |
| 941 | } |
| 942 | return started; |
| 943 | } |
| 944 | |
| 945 | void IncrementalService::mountExistingImages() { |
| 946 | auto d = std::unique_ptr<DIR, decltype(&::closedir)>(::opendir(mIncrementalDir.c_str()), |
| 947 | ::closedir); |
| 948 | while (auto e = ::readdir(d.get())) { |
| 949 | if (e->d_type != DT_DIR) { |
| 950 | continue; |
| 951 | } |
| 952 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 953 | continue; |
| 954 | } |
| 955 | auto root = path::join(mIncrementalDir, e->d_name); |
| 956 | if (!mountExistingImage(root, e->d_name)) { |
| 957 | IncFsMount::cleanupFilesystem(root); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | bool IncrementalService::mountExistingImage(std::string_view root, std::string_view key) { |
| 963 | LOG(INFO) << "Trying to mount: " << key; |
| 964 | |
| 965 | auto mountTarget = path::join(root, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 966 | const auto backing = path::join(root, constants().backing); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 967 | |
| 968 | IncFsMount::Control control; |
| 969 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 970 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 971 | if (!status.isOk()) { |
| 972 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 973 | return false; |
| 974 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 975 | control.cmd = controlParcel.cmd.release().release(); |
| 976 | control.pendingReads = controlParcel.pendingReads.release().release(); |
| 977 | control.logs = controlParcel.log.release().release(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 978 | |
| 979 | auto ifs = std::make_shared<IncFsMount>(std::string(root), -1, std::move(control), *this); |
| 980 | |
| 981 | auto m = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control, |
| 982 | path::join(mountTarget, constants().infoMdName)); |
| 983 | if (!m.has_loader() || !m.has_storage()) { |
| 984 | LOG(ERROR) << "Bad mount metadata in mount at " << root; |
| 985 | return false; |
| 986 | } |
| 987 | |
| 988 | ifs->mountId = m.storage().id(); |
| 989 | mNextId = std::max(mNextId, ifs->mountId + 1); |
| 990 | |
| 991 | std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 992 | auto d = openDir(path::c_str(mountTarget)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 993 | while (auto e = ::readdir(d.get())) { |
| 994 | if (e->d_type == DT_REG) { |
| 995 | auto name = std::string_view(e->d_name); |
| 996 | if (name.starts_with(constants().mountpointMdPrefix)) { |
| 997 | bindPoints.emplace_back(name, |
| 998 | parseFromIncfs<metadata::BindPoint>(mIncFs.get(), |
| 999 | ifs->control, |
| 1000 | path::join(mountTarget, |
| 1001 | name))); |
| 1002 | if (bindPoints.back().second.dest_path().empty() || |
| 1003 | bindPoints.back().second.source_subdir().empty()) { |
| 1004 | bindPoints.pop_back(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1005 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | } else if (e->d_type == DT_DIR) { |
| 1009 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 1010 | continue; |
| 1011 | } |
| 1012 | auto name = std::string_view(e->d_name); |
| 1013 | if (name.starts_with(constants().storagePrefix)) { |
| 1014 | auto md = parseFromIncfs<metadata::Storage>(mIncFs.get(), ifs->control, |
| 1015 | path::join(mountTarget, name)); |
| 1016 | auto [_, inserted] = mMounts.try_emplace(md.id(), ifs); |
| 1017 | if (!inserted) { |
| 1018 | LOG(WARNING) << "Ignoring storage with duplicate id " << md.id() |
| 1019 | << " for mount " << root; |
| 1020 | continue; |
| 1021 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1022 | ifs->storages.insert_or_assign(md.id(), IncFsMount::Storage{std::string(name)}); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1023 | mNextId = std::max(mNextId, md.id() + 1); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | if (ifs->storages.empty()) { |
| 1029 | LOG(WARNING) << "No valid storages in mount " << root; |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | int bindCount = 0; |
| 1034 | for (auto&& bp : bindPoints) { |
| 1035 | std::unique_lock l(mLock, std::defer_lock); |
| 1036 | bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first), |
| 1037 | std::move(*bp.second.mutable_source_subdir()), |
| 1038 | std::move(*bp.second.mutable_dest_path()), |
| 1039 | BindKind::Permanent, l); |
| 1040 | } |
| 1041 | |
| 1042 | if (bindCount == 0) { |
| 1043 | LOG(WARNING) << "No valid bind points for mount " << root; |
| 1044 | deleteStorage(*ifs); |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
| 1048 | DataLoaderParamsParcel dlParams; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1049 | dlParams.type = (DataLoaderType)m.loader().type(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1050 | dlParams.packageName = std::move(*m.mutable_loader()->mutable_package_name()); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1051 | dlParams.className = std::move(*m.mutable_loader()->mutable_class_name()); |
| 1052 | dlParams.arguments = std::move(*m.mutable_loader()->mutable_arguments()); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1053 | if (!prepareDataLoader(*ifs, &dlParams)) { |
| 1054 | deleteStorage(*ifs); |
| 1055 | return false; |
| 1056 | } |
| 1057 | |
| 1058 | mMounts[ifs->mountId] = std::move(ifs); |
| 1059 | return true; |
| 1060 | } |
| 1061 | |
| 1062 | bool IncrementalService::prepareDataLoader(IncrementalService::IncFsMount& ifs, |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame^] | 1063 | DataLoaderParamsParcel* params, |
| 1064 | const DataLoaderStatusListener* externalListener) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1065 | if (!mSystemReady.load(std::memory_order_relaxed)) { |
| 1066 | std::unique_lock l(ifs.lock); |
| 1067 | if (params) { |
| 1068 | if (ifs.savedDataLoaderParams) { |
| 1069 | LOG(WARNING) << "Trying to pass second set of data loader parameters, ignored it"; |
| 1070 | } else { |
| 1071 | ifs.savedDataLoaderParams = std::move(*params); |
| 1072 | } |
| 1073 | } else { |
| 1074 | if (!ifs.savedDataLoaderParams) { |
| 1075 | LOG(ERROR) << "Mount " << ifs.mountId |
| 1076 | << " is broken: no data loader params (system is not ready yet)"; |
| 1077 | return false; |
| 1078 | } |
| 1079 | } |
| 1080 | return true; // eventually... |
| 1081 | } |
| 1082 | if (base::GetBoolProperty("incremental.skip_loader", false)) { |
| 1083 | LOG(INFO) << "Skipped data loader because of incremental.skip_loader property"; |
| 1084 | std::unique_lock l(ifs.lock); |
| 1085 | ifs.savedDataLoaderParams.reset(); |
| 1086 | return true; |
| 1087 | } |
| 1088 | |
| 1089 | std::unique_lock l(ifs.lock); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1090 | if (ifs.dataLoaderStatus == IDataLoaderStatusListener::DATA_LOADER_CREATED) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1091 | LOG(INFO) << "Skipped data loader preparation because it already exists"; |
| 1092 | return true; |
| 1093 | } |
| 1094 | |
| 1095 | auto* dlp = params ? params |
| 1096 | : ifs.savedDataLoaderParams ? &ifs.savedDataLoaderParams.value() : nullptr; |
| 1097 | if (!dlp) { |
| 1098 | LOG(ERROR) << "Mount " << ifs.mountId << " is broken: no data loader params"; |
| 1099 | return false; |
| 1100 | } |
| 1101 | FileSystemControlParcel fsControlParcel; |
| 1102 | fsControlParcel.incremental = std::make_unique<IncrementalFileSystemControlParcel>(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1103 | fsControlParcel.incremental->cmd.reset(base::unique_fd(::dup(ifs.control.cmd))); |
| 1104 | fsControlParcel.incremental->pendingReads.reset( |
| 1105 | base::unique_fd(::dup(ifs.control.pendingReads))); |
| 1106 | fsControlParcel.incremental->log.reset(base::unique_fd(::dup(ifs.control.logs))); |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame^] | 1107 | sp<IncrementalDataLoaderListener> listener = new IncrementalDataLoaderListener(*this, *externalListener); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1108 | bool created = false; |
| 1109 | auto status = mIncrementalManager->prepareDataLoader(ifs.mountId, fsControlParcel, *dlp, |
| 1110 | listener, &created); |
| 1111 | if (!status.isOk() || !created) { |
| 1112 | LOG(ERROR) << "Failed to create a data loader for mount " << ifs.mountId; |
| 1113 | return false; |
| 1114 | } |
| 1115 | ifs.savedDataLoaderParams.reset(); |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1119 | // Extract lib filse from zip, create new files in incfs and write data to them |
| 1120 | bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath, |
| 1121 | std::string_view libDirRelativePath, |
| 1122 | std::string_view abi) { |
| 1123 | const auto ifs = getIfs(storage); |
| 1124 | // First prepare target directories if they don't exist yet |
| 1125 | if (auto res = makeDirs(storage, libDirRelativePath, 0755)) { |
| 1126 | LOG(ERROR) << "Failed to prepare target lib directory " << libDirRelativePath |
| 1127 | << " errno: " << res; |
| 1128 | return false; |
| 1129 | } |
| 1130 | |
| 1131 | std::unique_ptr<ZipFileRO> zipFile(ZipFileRO::open(apkFullPath.data())); |
| 1132 | if (!zipFile) { |
| 1133 | LOG(ERROR) << "Failed to open zip file at " << apkFullPath; |
| 1134 | return false; |
| 1135 | } |
| 1136 | void* cookie = nullptr; |
| 1137 | const auto libFilePrefix = path::join(constants().libDir, abi); |
| 1138 | if (!zipFile.get()->startIteration(&cookie, libFilePrefix.c_str() /* prefix */, |
| 1139 | constants().libSuffix.data() /* suffix */)) { |
| 1140 | LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath; |
| 1141 | return false; |
| 1142 | } |
| 1143 | ZipEntryRO entry = nullptr; |
| 1144 | bool success = true; |
| 1145 | while ((entry = zipFile.get()->nextEntry(cookie)) != nullptr) { |
| 1146 | char fileName[PATH_MAX]; |
| 1147 | if (zipFile.get()->getEntryFileName(entry, fileName, sizeof(fileName))) { |
| 1148 | continue; |
| 1149 | } |
| 1150 | const auto libName = path::basename(fileName); |
| 1151 | const auto targetLibPath = path::join(libDirRelativePath, libName); |
| 1152 | const auto targetLibPathAbsolute = normalizePathToStorage(ifs, storage, targetLibPath); |
| 1153 | // If the extract file already exists, skip |
| 1154 | struct stat st; |
| 1155 | if (stat(targetLibPathAbsolute.c_str(), &st) == 0) { |
| 1156 | LOG(INFO) << "Native lib file already exists: " << targetLibPath |
| 1157 | << "; skipping extraction"; |
| 1158 | continue; |
| 1159 | } |
| 1160 | |
| 1161 | uint32_t uncompressedLen; |
| 1162 | if (!zipFile.get()->getEntryInfo(entry, nullptr, &uncompressedLen, nullptr, nullptr, |
| 1163 | nullptr, nullptr)) { |
| 1164 | LOG(ERROR) << "Failed to read native lib entry: " << fileName; |
| 1165 | success = false; |
| 1166 | break; |
| 1167 | } |
| 1168 | |
| 1169 | // Create new lib file without signature info |
| 1170 | incfs::NewFileParams libFileParams; |
| 1171 | libFileParams.size = uncompressedLen; |
| 1172 | libFileParams.verification.hashAlgorithm = INCFS_HASH_NONE; |
| 1173 | // Metadata of the new lib file is its relative path |
| 1174 | IncFsSpan libFileMetadata; |
| 1175 | libFileMetadata.data = targetLibPath.c_str(); |
| 1176 | libFileMetadata.size = targetLibPath.size(); |
| 1177 | libFileParams.metadata = libFileMetadata; |
| 1178 | incfs::FileId libFileId = idFromMetadata(targetLibPath); |
| 1179 | if (auto res = makeFile(storage, targetLibPath, 0777, libFileId, libFileParams)) { |
| 1180 | LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res; |
| 1181 | success = false; |
| 1182 | // If one lib file fails to be created, abort others as well |
| 1183 | break; |
| 1184 | } |
| 1185 | |
| 1186 | // Write extracted data to new file |
| 1187 | std::vector<uint8_t> libData(uncompressedLen); |
| 1188 | if (!zipFile.get()->uncompressEntry(entry, &libData[0], uncompressedLen)) { |
| 1189 | LOG(ERROR) << "Failed to extract native lib zip entry: " << fileName; |
| 1190 | success = false; |
| 1191 | break; |
| 1192 | } |
| 1193 | android::base::unique_fd writeFd(mIncFs->openWrite(ifs->control, libFileId)); |
| 1194 | if (writeFd < 0) { |
| 1195 | LOG(ERROR) << "Failed to open write fd for: " << targetLibPath << " errno: " << writeFd; |
| 1196 | success = false; |
| 1197 | break; |
| 1198 | } |
| 1199 | const int numBlocks = uncompressedLen / constants().blockSize + 1; |
| 1200 | std::vector<IncFsDataBlock> instructions; |
| 1201 | auto remainingData = std::span(libData); |
| 1202 | for (int i = 0; i < numBlocks - 1; i++) { |
| 1203 | auto inst = IncFsDataBlock{ |
| 1204 | .fileFd = writeFd, |
| 1205 | .pageIndex = static_cast<IncFsBlockIndex>(i), |
| 1206 | .compression = INCFS_COMPRESSION_KIND_NONE, |
| 1207 | .kind = INCFS_BLOCK_KIND_DATA, |
| 1208 | .dataSize = static_cast<uint16_t>(constants().blockSize), |
| 1209 | .data = reinterpret_cast<const char*>(remainingData.data()), |
| 1210 | }; |
| 1211 | instructions.push_back(inst); |
| 1212 | remainingData = remainingData.subspan(constants().blockSize); |
| 1213 | } |
| 1214 | // Last block |
| 1215 | auto inst = IncFsDataBlock{ |
| 1216 | .fileFd = writeFd, |
| 1217 | .pageIndex = static_cast<IncFsBlockIndex>(numBlocks - 1), |
| 1218 | .compression = INCFS_COMPRESSION_KIND_NONE, |
| 1219 | .kind = INCFS_BLOCK_KIND_DATA, |
| 1220 | .dataSize = static_cast<uint16_t>(remainingData.size()), |
| 1221 | .data = reinterpret_cast<const char*>(remainingData.data()), |
| 1222 | }; |
| 1223 | instructions.push_back(inst); |
| 1224 | size_t res = mIncFs->writeBlocks(instructions); |
| 1225 | if (res != instructions.size()) { |
| 1226 | LOG(ERROR) << "Failed to write data into: " << targetLibPath; |
| 1227 | success = false; |
| 1228 | } |
| 1229 | instructions.clear(); |
| 1230 | } |
| 1231 | zipFile.get()->endIteration(cookie); |
| 1232 | return success; |
| 1233 | } |
| 1234 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1235 | binder::Status IncrementalService::IncrementalDataLoaderListener::onStatusChanged(MountId mountId, |
| 1236 | int newStatus) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame^] | 1237 | if (externalListener) { |
| 1238 | // Give an external listener a chance to act before we destroy something. |
| 1239 | externalListener->onStatusChanged(mountId, newStatus); |
| 1240 | } |
| 1241 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1242 | std::unique_lock l(incrementalService.mLock); |
| 1243 | const auto& ifs = incrementalService.getIfsLocked(mountId); |
| 1244 | if (!ifs) { |
| 1245 | LOG(WARNING) << "Received data loader status " << int(newStatus) << " for unknown mount " |
| 1246 | << mountId; |
| 1247 | return binder::Status::ok(); |
| 1248 | } |
| 1249 | ifs->dataLoaderStatus = newStatus; |
| 1250 | switch (newStatus) { |
| 1251 | case IDataLoaderStatusListener::DATA_LOADER_NO_CONNECTION: { |
| 1252 | auto now = Clock::now(); |
| 1253 | if (ifs->connectionLostTime.time_since_epoch().count() == 0) { |
| 1254 | ifs->connectionLostTime = now; |
| 1255 | break; |
| 1256 | } |
| 1257 | auto duration = |
| 1258 | std::chrono::duration_cast<Seconds>(now - ifs->connectionLostTime).count(); |
| 1259 | if (duration >= 10) { |
| 1260 | incrementalService.mIncrementalManager->showHealthBlockedUI(mountId); |
| 1261 | } |
| 1262 | break; |
| 1263 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1264 | case IDataLoaderStatusListener::DATA_LOADER_CONNECTION_OK: { |
| 1265 | ifs->dataLoaderStatus = IDataLoaderStatusListener::DATA_LOADER_STARTED; |
| 1266 | break; |
| 1267 | } |
| 1268 | case IDataLoaderStatusListener::DATA_LOADER_CREATED: { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1269 | ifs->dataLoaderReady.notify_one(); |
| 1270 | break; |
| 1271 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1272 | case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1273 | ifs->dataLoaderStatus = IDataLoaderStatusListener::DATA_LOADER_STOPPED; |
| 1274 | incrementalService.deleteStorageLocked(*ifs, std::move(l)); |
| 1275 | break; |
| 1276 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1277 | case IDataLoaderStatusListener::DATA_LOADER_STARTED: { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1278 | break; |
| 1279 | } |
| 1280 | case IDataLoaderStatusListener::DATA_LOADER_STOPPED: { |
| 1281 | break; |
| 1282 | } |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame^] | 1283 | case IDataLoaderStatusListener::DATA_LOADER_IMAGE_READY: { |
| 1284 | break; |
| 1285 | } |
| 1286 | case IDataLoaderStatusListener::DATA_LOADER_IMAGE_NOT_READY: { |
| 1287 | break; |
| 1288 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1289 | default: { |
| 1290 | LOG(WARNING) << "Unknown data loader status: " << newStatus |
| 1291 | << " for mount: " << mountId; |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | return binder::Status::ok(); |
| 1297 | } |
| 1298 | |
| 1299 | } // namespace android::incremental |