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