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