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