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> |
Jooyung Han | 66c567a | 2020-03-07 21:47:09 +0900 | [diff] [blame] | 31 | #include <binder/Nullable.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 32 | #include <binder/ParcelFileDescriptor.h> |
| 33 | #include <binder/Status.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <uuid/uuid.h> |
| 36 | #include <zlib.h> |
| 37 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 38 | #include <ctime> |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 39 | #include <filesystem> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 40 | #include <iterator> |
| 41 | #include <span> |
| 42 | #include <stack> |
| 43 | #include <thread> |
| 44 | #include <type_traits> |
| 45 | |
| 46 | #include "Metadata.pb.h" |
| 47 | |
| 48 | using namespace std::literals; |
| 49 | using namespace android::content::pm; |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 50 | namespace fs = std::filesystem; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 51 | |
| 52 | namespace android::incremental { |
| 53 | |
| 54 | namespace { |
| 55 | |
| 56 | using IncrementalFileSystemControlParcel = |
| 57 | ::android::os::incremental::IncrementalFileSystemControlParcel; |
| 58 | |
| 59 | struct Constants { |
| 60 | static constexpr auto backing = "backing_store"sv; |
| 61 | static constexpr auto mount = "mount"sv; |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 62 | static constexpr auto mountKeyPrefix = "MT_"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 63 | static constexpr auto storagePrefix = "st"sv; |
| 64 | static constexpr auto mountpointMdPrefix = ".mountpoint."sv; |
| 65 | static constexpr auto infoMdName = ".info"sv; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 66 | static constexpr auto libDir = "lib"sv; |
| 67 | static constexpr auto libSuffix = ".so"sv; |
| 68 | static constexpr auto blockSize = 4096; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static const Constants& constants() { |
| 72 | static Constants c; |
| 73 | return c; |
| 74 | } |
| 75 | |
| 76 | template <base::LogSeverity level = base::ERROR> |
| 77 | bool mkdirOrLog(std::string_view name, int mode = 0770, bool allowExisting = true) { |
| 78 | auto cstr = path::c_str(name); |
| 79 | if (::mkdir(cstr, mode)) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 80 | if (!allowExisting || errno != EEXIST) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 81 | PLOG(level) << "Can't create directory '" << name << '\''; |
| 82 | return false; |
| 83 | } |
| 84 | struct stat st; |
| 85 | if (::stat(cstr, &st) || !S_ISDIR(st.st_mode)) { |
| 86 | PLOG(level) << "Path exists but is not a directory: '" << name << '\''; |
| 87 | return false; |
| 88 | } |
| 89 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 90 | if (::chmod(cstr, mode)) { |
| 91 | PLOG(level) << "Changing permission failed for '" << name << '\''; |
| 92 | return false; |
| 93 | } |
| 94 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
| 98 | static std::string toMountKey(std::string_view path) { |
| 99 | if (path.empty()) { |
| 100 | return "@none"; |
| 101 | } |
| 102 | if (path == "/"sv) { |
| 103 | return "@root"; |
| 104 | } |
| 105 | if (path::isAbsolute(path)) { |
| 106 | path.remove_prefix(1); |
| 107 | } |
| 108 | std::string res(path); |
| 109 | std::replace(res.begin(), res.end(), '/', '_'); |
| 110 | std::replace(res.begin(), res.end(), '@', '_'); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 111 | return std::string(constants().mountKeyPrefix) + res; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static std::pair<std::string, std::string> makeMountDir(std::string_view incrementalDir, |
| 115 | std::string_view path) { |
| 116 | auto mountKey = toMountKey(path); |
| 117 | const auto prefixSize = mountKey.size(); |
| 118 | for (int counter = 0; counter < 1000; |
| 119 | mountKey.resize(prefixSize), base::StringAppendF(&mountKey, "%d", counter++)) { |
| 120 | auto mountRoot = path::join(incrementalDir, mountKey); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 121 | if (mkdirOrLog(mountRoot, 0777, false)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 122 | return {mountKey, mountRoot}; |
| 123 | } |
| 124 | } |
| 125 | return {}; |
| 126 | } |
| 127 | |
| 128 | template <class ProtoMessage, class Control> |
| 129 | static ProtoMessage parseFromIncfs(const IncFsWrapper* incfs, Control&& control, |
| 130 | std::string_view path) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 131 | auto md = incfs->getMetadata(control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 132 | ProtoMessage message; |
| 133 | return message.ParseFromArray(md.data(), md.size()) ? message : ProtoMessage{}; |
| 134 | } |
| 135 | |
| 136 | static bool isValidMountTarget(std::string_view path) { |
| 137 | return path::isAbsolute(path) && path::isEmptyDir(path).value_or(true); |
| 138 | } |
| 139 | |
| 140 | std::string makeBindMdName() { |
| 141 | static constexpr auto uuidStringSize = 36; |
| 142 | |
| 143 | uuid_t guid; |
| 144 | uuid_generate(guid); |
| 145 | |
| 146 | std::string name; |
| 147 | const auto prefixSize = constants().mountpointMdPrefix.size(); |
| 148 | name.reserve(prefixSize + uuidStringSize); |
| 149 | |
| 150 | name = constants().mountpointMdPrefix; |
| 151 | name.resize(prefixSize + uuidStringSize); |
| 152 | uuid_unparse(guid, name.data() + prefixSize); |
| 153 | |
| 154 | return name; |
| 155 | } |
| 156 | } // namespace |
| 157 | |
| 158 | IncrementalService::IncFsMount::~IncFsMount() { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 159 | incrementalService.mDataLoaderManager->destroyDataLoader(mountId); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 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()); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 288 | } |
| 289 | dprintf(fd, "\t\tstorages (%d):\n", int(mnt.storages.size())); |
| 290 | for (auto&& [storageId, storage] : mnt.storages) { |
| 291 | dprintf(fd, "\t\t\t[%d] -> [%s]\n", storageId, storage.name.c_str()); |
| 292 | } |
| 293 | |
| 294 | dprintf(fd, "\t\tbindPoints (%d):\n", int(mnt.bindPoints.size())); |
| 295 | for (auto&& [target, bind] : mnt.bindPoints) { |
| 296 | dprintf(fd, "\t\t\t[%s]->[%d]:\n", target.c_str(), bind.storage); |
| 297 | dprintf(fd, "\t\t\t\tsavedFilename: %s\n", bind.savedFilename.c_str()); |
| 298 | dprintf(fd, "\t\t\t\tsourceDir: %s\n", bind.sourceDir.c_str()); |
| 299 | dprintf(fd, "\t\t\t\tkind: %s\n", toString(bind.kind)); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | dprintf(fd, "Sorted binds (%d):\n", int(mBindsByPath.size())); |
| 304 | for (auto&& [target, mountPairIt] : mBindsByPath) { |
| 305 | const auto& bind = mountPairIt->second; |
| 306 | dprintf(fd, "\t\t[%s]->[%d]:\n", target.c_str(), bind.storage); |
| 307 | dprintf(fd, "\t\t\tsavedFilename: %s\n", bind.savedFilename.c_str()); |
| 308 | dprintf(fd, "\t\t\tsourceDir: %s\n", bind.sourceDir.c_str()); |
| 309 | dprintf(fd, "\t\t\tkind: %s\n", toString(bind.kind)); |
| 310 | } |
| 311 | } |
| 312 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 313 | std::optional<std::future<void>> IncrementalService::onSystemReady() { |
| 314 | std::promise<void> threadFinished; |
| 315 | if (mSystemReady.exchange(true)) { |
| 316 | return {}; |
| 317 | } |
| 318 | |
| 319 | std::vector<IfsMountPtr> mounts; |
| 320 | { |
| 321 | std::lock_guard l(mLock); |
| 322 | mounts.reserve(mMounts.size()); |
| 323 | for (auto&& [id, ifs] : mMounts) { |
| 324 | if (ifs->mountId == id) { |
| 325 | mounts.push_back(ifs); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | std::thread([this, mounts = std::move(mounts)]() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 331 | for (auto&& ifs : mounts) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 332 | if (prepareDataLoader(*ifs)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 333 | LOG(INFO) << "Successfully started data loader for mount " << ifs->mountId; |
| 334 | } else { |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 335 | // TODO(b/133435829): handle data loader start failures |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 336 | LOG(WARNING) << "Failed to start data loader for mount " << ifs->mountId; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 339 | mPrepareDataLoaders.set_value_at_thread_exit(); |
| 340 | }).detach(); |
| 341 | return mPrepareDataLoaders.get_future(); |
| 342 | } |
| 343 | |
| 344 | auto IncrementalService::getStorageSlotLocked() -> MountMap::iterator { |
| 345 | for (;;) { |
| 346 | if (mNextId == kMaxStorageId) { |
| 347 | mNextId = 0; |
| 348 | } |
| 349 | auto id = ++mNextId; |
| 350 | auto [it, inserted] = mMounts.try_emplace(id, nullptr); |
| 351 | if (inserted) { |
| 352 | return it; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 357 | StorageId IncrementalService::createStorage( |
| 358 | std::string_view mountPoint, DataLoaderParamsParcel&& dataLoaderParams, |
| 359 | const DataLoaderStatusListener& dataLoaderStatusListener, CreateOptions options) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 360 | LOG(INFO) << "createStorage: " << mountPoint << " | " << int(options); |
| 361 | if (!path::isAbsolute(mountPoint)) { |
| 362 | LOG(ERROR) << "path is not absolute: " << mountPoint; |
| 363 | return kInvalidStorageId; |
| 364 | } |
| 365 | |
| 366 | auto mountNorm = path::normalize(mountPoint); |
| 367 | { |
| 368 | const auto id = findStorageId(mountNorm); |
| 369 | if (id != kInvalidStorageId) { |
| 370 | if (options & CreateOptions::OpenExisting) { |
| 371 | LOG(INFO) << "Opened existing storage " << id; |
| 372 | return id; |
| 373 | } |
| 374 | LOG(ERROR) << "Directory " << mountPoint << " is already mounted at storage " << id; |
| 375 | return kInvalidStorageId; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (!(options & CreateOptions::CreateNew)) { |
| 380 | LOG(ERROR) << "not requirested create new storage, and it doesn't exist: " << mountPoint; |
| 381 | return kInvalidStorageId; |
| 382 | } |
| 383 | |
| 384 | if (!path::isEmptyDir(mountNorm)) { |
| 385 | LOG(ERROR) << "Mounting over existing non-empty directory is not supported: " << mountNorm; |
| 386 | return kInvalidStorageId; |
| 387 | } |
| 388 | auto [mountKey, mountRoot] = makeMountDir(mIncrementalDir, mountNorm); |
| 389 | if (mountRoot.empty()) { |
| 390 | LOG(ERROR) << "Bad mount point"; |
| 391 | return kInvalidStorageId; |
| 392 | } |
| 393 | // Make sure the code removes all crap it may create while still failing. |
| 394 | auto firstCleanup = [](const std::string* ptr) { IncFsMount::cleanupFilesystem(*ptr); }; |
| 395 | auto firstCleanupOnFailure = |
| 396 | std::unique_ptr<std::string, decltype(firstCleanup)>(&mountRoot, firstCleanup); |
| 397 | |
| 398 | auto mountTarget = path::join(mountRoot, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 399 | const auto backing = path::join(mountRoot, constants().backing); |
| 400 | if (!mkdirOrLog(backing, 0777) || !mkdirOrLog(mountTarget)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 401 | return kInvalidStorageId; |
| 402 | } |
| 403 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 404 | IncFsMount::Control control; |
| 405 | { |
| 406 | std::lock_guard l(mMountOperationLock); |
| 407 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 408 | |
| 409 | if (auto err = rmDirContent(backing.c_str())) { |
| 410 | LOG(ERROR) << "Coudn't clean the backing directory " << backing << ": " << err; |
| 411 | return kInvalidStorageId; |
| 412 | } |
| 413 | if (!mkdirOrLog(path::join(backing, ".index"), 0777)) { |
| 414 | return kInvalidStorageId; |
| 415 | } |
| 416 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 417 | if (!status.isOk()) { |
| 418 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 419 | return kInvalidStorageId; |
| 420 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 421 | if (controlParcel.cmd.get() < 0 || controlParcel.pendingReads.get() < 0 || |
| 422 | controlParcel.log.get() < 0) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 423 | LOG(ERROR) << "Vold::mountIncFs() returned invalid control parcel."; |
| 424 | return kInvalidStorageId; |
| 425 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 426 | int cmd = controlParcel.cmd.release().release(); |
| 427 | int pendingReads = controlParcel.pendingReads.release().release(); |
| 428 | int logs = controlParcel.log.release().release(); |
| 429 | control = mIncFs->createControl(cmd, pendingReads, logs); |
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 { |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 920 | { |
| 921 | std::unique_lock l(mLock); |
| 922 | const auto& ifs = getIfsLocked(storage); |
| 923 | if (!ifs) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 924 | return false; |
| 925 | } |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 926 | if (ifs->dataLoaderStatus != IDataLoaderStatusListener::DATA_LOADER_CREATED) { |
| 927 | ifs->dataLoaderStartRequested = true; |
| 928 | return true; |
| 929 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 930 | } |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 931 | return startDataLoader(storage); |
| 932 | } |
| 933 | |
| 934 | bool IncrementalService::startDataLoader(MountId mountId) const { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 935 | sp<IDataLoader> dataloader; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 936 | auto status = mDataLoaderManager->getDataLoader(mountId, &dataloader); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 937 | if (!status.isOk()) { |
| 938 | return false; |
| 939 | } |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 940 | if (!dataloader) { |
| 941 | return false; |
| 942 | } |
Alex Buynytskyy | b6e02f7 | 2020-03-17 18:12:23 -0700 | [diff] [blame] | 943 | status = dataloader->start(mountId); |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 944 | if (!status.isOk()) { |
| 945 | return false; |
| 946 | } |
| 947 | return true; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | void IncrementalService::mountExistingImages() { |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 951 | for (const auto& entry : fs::directory_iterator(mIncrementalDir)) { |
| 952 | const auto path = entry.path().u8string(); |
| 953 | const auto name = entry.path().filename().u8string(); |
| 954 | if (!base::StartsWith(name, constants().mountKeyPrefix)) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 955 | continue; |
| 956 | } |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 957 | const auto root = path::join(mIncrementalDir, name); |
| 958 | if (!mountExistingImage(root, name)) { |
| 959 | IncFsMount::cleanupFilesystem(path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | bool IncrementalService::mountExistingImage(std::string_view root, std::string_view key) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 965 | auto mountTarget = path::join(root, constants().mount); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 966 | const auto backing = path::join(root, constants().backing); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 967 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 968 | IncrementalFileSystemControlParcel controlParcel; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 969 | auto status = mVold->mountIncFs(backing, mountTarget, 0, &controlParcel); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 970 | if (!status.isOk()) { |
| 971 | LOG(ERROR) << "Vold::mountIncFs() failed: " << status.toString8(); |
| 972 | return false; |
| 973 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 974 | |
| 975 | int cmd = controlParcel.cmd.release().release(); |
| 976 | int pendingReads = controlParcel.pendingReads.release().release(); |
| 977 | int logs = controlParcel.log.release().release(); |
| 978 | IncFsMount::Control control = mIncFs->createControl(cmd, pendingReads, logs); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 979 | |
| 980 | auto ifs = std::make_shared<IncFsMount>(std::string(root), -1, std::move(control), *this); |
| 981 | |
| 982 | auto m = parseFromIncfs<metadata::Mount>(mIncFs.get(), ifs->control, |
| 983 | path::join(mountTarget, constants().infoMdName)); |
| 984 | if (!m.has_loader() || !m.has_storage()) { |
| 985 | LOG(ERROR) << "Bad mount metadata in mount at " << root; |
| 986 | return false; |
| 987 | } |
| 988 | |
| 989 | ifs->mountId = m.storage().id(); |
| 990 | mNextId = std::max(mNextId, ifs->mountId + 1); |
| 991 | |
| 992 | std::vector<std::pair<std::string, metadata::BindPoint>> bindPoints; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 993 | auto d = openDir(path::c_str(mountTarget)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 994 | while (auto e = ::readdir(d.get())) { |
| 995 | if (e->d_type == DT_REG) { |
| 996 | auto name = std::string_view(e->d_name); |
| 997 | if (name.starts_with(constants().mountpointMdPrefix)) { |
| 998 | bindPoints.emplace_back(name, |
| 999 | parseFromIncfs<metadata::BindPoint>(mIncFs.get(), |
| 1000 | ifs->control, |
| 1001 | path::join(mountTarget, |
| 1002 | name))); |
| 1003 | if (bindPoints.back().second.dest_path().empty() || |
| 1004 | bindPoints.back().second.source_subdir().empty()) { |
| 1005 | bindPoints.pop_back(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1006 | mIncFs->unlink(ifs->control, path::join(ifs->root, constants().mount, name)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | } else if (e->d_type == DT_DIR) { |
| 1010 | if (e->d_name == "."sv || e->d_name == ".."sv) { |
| 1011 | continue; |
| 1012 | } |
| 1013 | auto name = std::string_view(e->d_name); |
| 1014 | if (name.starts_with(constants().storagePrefix)) { |
| 1015 | auto md = parseFromIncfs<metadata::Storage>(mIncFs.get(), ifs->control, |
| 1016 | path::join(mountTarget, name)); |
| 1017 | auto [_, inserted] = mMounts.try_emplace(md.id(), ifs); |
| 1018 | if (!inserted) { |
| 1019 | LOG(WARNING) << "Ignoring storage with duplicate id " << md.id() |
| 1020 | << " for mount " << root; |
| 1021 | continue; |
| 1022 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1023 | ifs->storages.insert_or_assign(md.id(), IncFsMount::Storage{std::string(name)}); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1024 | mNextId = std::max(mNextId, md.id() + 1); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (ifs->storages.empty()) { |
| 1030 | LOG(WARNING) << "No valid storages in mount " << root; |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | int bindCount = 0; |
| 1035 | for (auto&& bp : bindPoints) { |
| 1036 | std::unique_lock l(mLock, std::defer_lock); |
| 1037 | bindCount += !addBindMountWithMd(*ifs, bp.second.storage_id(), std::move(bp.first), |
| 1038 | std::move(*bp.second.mutable_source_subdir()), |
| 1039 | std::move(*bp.second.mutable_dest_path()), |
| 1040 | BindKind::Permanent, l); |
| 1041 | } |
| 1042 | |
| 1043 | if (bindCount == 0) { |
| 1044 | LOG(WARNING) << "No valid bind points for mount " << root; |
| 1045 | deleteStorage(*ifs); |
| 1046 | return false; |
| 1047 | } |
| 1048 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1049 | mMounts[ifs->mountId] = std::move(ifs); |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | bool IncrementalService::prepareDataLoader(IncrementalService::IncFsMount& ifs, |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 1054 | DataLoaderParamsParcel* params, |
| 1055 | const DataLoaderStatusListener* externalListener) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1056 | if (!mSystemReady.load(std::memory_order_relaxed)) { |
| 1057 | std::unique_lock l(ifs.lock); |
| 1058 | if (params) { |
| 1059 | if (ifs.savedDataLoaderParams) { |
| 1060 | LOG(WARNING) << "Trying to pass second set of data loader parameters, ignored it"; |
| 1061 | } else { |
| 1062 | ifs.savedDataLoaderParams = std::move(*params); |
| 1063 | } |
| 1064 | } else { |
| 1065 | if (!ifs.savedDataLoaderParams) { |
| 1066 | LOG(ERROR) << "Mount " << ifs.mountId |
| 1067 | << " is broken: no data loader params (system is not ready yet)"; |
| 1068 | return false; |
| 1069 | } |
| 1070 | } |
| 1071 | return true; // eventually... |
| 1072 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1073 | |
| 1074 | std::unique_lock l(ifs.lock); |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1075 | if (ifs.dataLoaderStatus != -1) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1076 | LOG(INFO) << "Skipped data loader preparation because it already exists"; |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | auto* dlp = params ? params |
| 1081 | : ifs.savedDataLoaderParams ? &ifs.savedDataLoaderParams.value() : nullptr; |
| 1082 | if (!dlp) { |
| 1083 | LOG(ERROR) << "Mount " << ifs.mountId << " is broken: no data loader params"; |
| 1084 | return false; |
| 1085 | } |
| 1086 | FileSystemControlParcel fsControlParcel; |
Jooyung Han | 66c567a | 2020-03-07 21:47:09 +0900 | [diff] [blame] | 1087 | fsControlParcel.incremental = aidl::make_nullable<IncrementalFileSystemControlParcel>(); |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 1088 | fsControlParcel.incremental->cmd.reset(base::unique_fd(::dup(ifs.control.cmd()))); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 1089 | fsControlParcel.incremental->pendingReads.reset( |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 1090 | base::unique_fd(::dup(ifs.control.pendingReads()))); |
| 1091 | fsControlParcel.incremental->log.reset(base::unique_fd(::dup(ifs.control.logs()))); |
Songchun Fan | 1124fd3 | 2020-02-10 12:49:41 -0800 | [diff] [blame] | 1092 | sp<IncrementalDataLoaderListener> listener = |
Songchun Fan | 306b7df | 2020-03-17 12:37:07 -0700 | [diff] [blame] | 1093 | new IncrementalDataLoaderListener(*this, |
| 1094 | externalListener ? *externalListener |
| 1095 | : DataLoaderStatusListener()); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1096 | bool created = false; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 1097 | auto status = mDataLoaderManager->initializeDataLoader(ifs.mountId, *dlp, fsControlParcel, |
| 1098 | listener, &created); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1099 | if (!status.isOk() || !created) { |
| 1100 | LOG(ERROR) << "Failed to create a data loader for mount " << ifs.mountId; |
| 1101 | return false; |
| 1102 | } |
| 1103 | ifs.savedDataLoaderParams.reset(); |
| 1104 | return true; |
| 1105 | } |
| 1106 | |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1107 | // Extract lib filse from zip, create new files in incfs and write data to them |
| 1108 | bool IncrementalService::configureNativeBinaries(StorageId storage, std::string_view apkFullPath, |
| 1109 | std::string_view libDirRelativePath, |
| 1110 | std::string_view abi) { |
| 1111 | const auto ifs = getIfs(storage); |
| 1112 | // First prepare target directories if they don't exist yet |
| 1113 | if (auto res = makeDirs(storage, libDirRelativePath, 0755)) { |
| 1114 | LOG(ERROR) << "Failed to prepare target lib directory " << libDirRelativePath |
| 1115 | << " errno: " << res; |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
| 1119 | std::unique_ptr<ZipFileRO> zipFile(ZipFileRO::open(apkFullPath.data())); |
| 1120 | if (!zipFile) { |
| 1121 | LOG(ERROR) << "Failed to open zip file at " << apkFullPath; |
| 1122 | return false; |
| 1123 | } |
| 1124 | void* cookie = nullptr; |
| 1125 | const auto libFilePrefix = path::join(constants().libDir, abi); |
| 1126 | if (!zipFile.get()->startIteration(&cookie, libFilePrefix.c_str() /* prefix */, |
| 1127 | constants().libSuffix.data() /* suffix */)) { |
| 1128 | LOG(ERROR) << "Failed to start zip iteration for " << apkFullPath; |
| 1129 | return false; |
| 1130 | } |
| 1131 | ZipEntryRO entry = nullptr; |
| 1132 | bool success = true; |
| 1133 | while ((entry = zipFile.get()->nextEntry(cookie)) != nullptr) { |
| 1134 | char fileName[PATH_MAX]; |
| 1135 | if (zipFile.get()->getEntryFileName(entry, fileName, sizeof(fileName))) { |
| 1136 | continue; |
| 1137 | } |
| 1138 | const auto libName = path::basename(fileName); |
| 1139 | const auto targetLibPath = path::join(libDirRelativePath, libName); |
| 1140 | const auto targetLibPathAbsolute = normalizePathToStorage(ifs, storage, targetLibPath); |
| 1141 | // If the extract file already exists, skip |
| 1142 | struct stat st; |
| 1143 | if (stat(targetLibPathAbsolute.c_str(), &st) == 0) { |
| 1144 | LOG(INFO) << "Native lib file already exists: " << targetLibPath |
| 1145 | << "; skipping extraction"; |
| 1146 | continue; |
| 1147 | } |
| 1148 | |
| 1149 | uint32_t uncompressedLen; |
| 1150 | if (!zipFile.get()->getEntryInfo(entry, nullptr, &uncompressedLen, nullptr, nullptr, |
| 1151 | nullptr, nullptr)) { |
| 1152 | LOG(ERROR) << "Failed to read native lib entry: " << fileName; |
| 1153 | success = false; |
| 1154 | break; |
| 1155 | } |
| 1156 | |
| 1157 | // Create new lib file without signature info |
George Burgess IV | dd5275d | 2020-02-10 11:18:07 -0800 | [diff] [blame] | 1158 | incfs::NewFileParams libFileParams{}; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1159 | libFileParams.size = uncompressedLen; |
Alex Buynytskyy | f5e605a | 2020-03-13 13:31:12 -0700 | [diff] [blame] | 1160 | libFileParams.signature = {}; |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1161 | // Metadata of the new lib file is its relative path |
| 1162 | IncFsSpan libFileMetadata; |
| 1163 | libFileMetadata.data = targetLibPath.c_str(); |
| 1164 | libFileMetadata.size = targetLibPath.size(); |
| 1165 | libFileParams.metadata = libFileMetadata; |
| 1166 | incfs::FileId libFileId = idFromMetadata(targetLibPath); |
| 1167 | if (auto res = makeFile(storage, targetLibPath, 0777, libFileId, libFileParams)) { |
| 1168 | LOG(ERROR) << "Failed to make file for: " << targetLibPath << " errno: " << res; |
| 1169 | success = false; |
| 1170 | // If one lib file fails to be created, abort others as well |
| 1171 | break; |
| 1172 | } |
Songchun Fan | afaf6e9 | 2020-03-18 14:12:20 -0700 | [diff] [blame] | 1173 | // If it is a zero-byte file, skip data writing |
| 1174 | if (uncompressedLen == 0) { |
| 1175 | continue; |
| 1176 | } |
Songchun Fan | 0f8b6fe | 2020-02-05 17:41:25 -0800 | [diff] [blame] | 1177 | |
| 1178 | // Write extracted data to new file |
| 1179 | std::vector<uint8_t> libData(uncompressedLen); |
| 1180 | if (!zipFile.get()->uncompressEntry(entry, &libData[0], uncompressedLen)) { |
| 1181 | LOG(ERROR) << "Failed to extract native lib zip entry: " << fileName; |
| 1182 | success = false; |
| 1183 | break; |
| 1184 | } |
| 1185 | android::base::unique_fd writeFd(mIncFs->openWrite(ifs->control, libFileId)); |
| 1186 | if (writeFd < 0) { |
| 1187 | LOG(ERROR) << "Failed to open write fd for: " << targetLibPath << " errno: " << writeFd; |
| 1188 | success = false; |
| 1189 | break; |
| 1190 | } |
| 1191 | const int numBlocks = uncompressedLen / constants().blockSize + 1; |
| 1192 | std::vector<IncFsDataBlock> instructions; |
| 1193 | auto remainingData = std::span(libData); |
| 1194 | for (int i = 0; i < numBlocks - 1; i++) { |
| 1195 | auto inst = IncFsDataBlock{ |
| 1196 | .fileFd = writeFd, |
| 1197 | .pageIndex = static_cast<IncFsBlockIndex>(i), |
| 1198 | .compression = INCFS_COMPRESSION_KIND_NONE, |
| 1199 | .kind = INCFS_BLOCK_KIND_DATA, |
| 1200 | .dataSize = static_cast<uint16_t>(constants().blockSize), |
| 1201 | .data = reinterpret_cast<const char*>(remainingData.data()), |
| 1202 | }; |
| 1203 | instructions.push_back(inst); |
| 1204 | remainingData = remainingData.subspan(constants().blockSize); |
| 1205 | } |
| 1206 | // Last block |
| 1207 | auto inst = IncFsDataBlock{ |
| 1208 | .fileFd = writeFd, |
| 1209 | .pageIndex = static_cast<IncFsBlockIndex>(numBlocks - 1), |
| 1210 | .compression = INCFS_COMPRESSION_KIND_NONE, |
| 1211 | .kind = INCFS_BLOCK_KIND_DATA, |
| 1212 | .dataSize = static_cast<uint16_t>(remainingData.size()), |
| 1213 | .data = reinterpret_cast<const char*>(remainingData.data()), |
| 1214 | }; |
| 1215 | instructions.push_back(inst); |
| 1216 | size_t res = mIncFs->writeBlocks(instructions); |
| 1217 | if (res != instructions.size()) { |
| 1218 | LOG(ERROR) << "Failed to write data into: " << targetLibPath; |
| 1219 | success = false; |
| 1220 | } |
| 1221 | instructions.clear(); |
| 1222 | } |
| 1223 | zipFile.get()->endIteration(cookie); |
| 1224 | return success; |
| 1225 | } |
| 1226 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1227 | binder::Status IncrementalService::IncrementalDataLoaderListener::onStatusChanged(MountId mountId, |
| 1228 | int newStatus) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 1229 | if (externalListener) { |
| 1230 | // Give an external listener a chance to act before we destroy something. |
| 1231 | externalListener->onStatusChanged(mountId, newStatus); |
| 1232 | } |
| 1233 | |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1234 | bool startRequested = false; |
| 1235 | { |
| 1236 | std::unique_lock l(incrementalService.mLock); |
| 1237 | const auto& ifs = incrementalService.getIfsLocked(mountId); |
| 1238 | if (!ifs) { |
Songchun Fan | 306b7df | 2020-03-17 12:37:07 -0700 | [diff] [blame] | 1239 | LOG(WARNING) << "Received data loader status " << int(newStatus) |
| 1240 | << " for unknown mount " << mountId; |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1241 | return binder::Status::ok(); |
| 1242 | } |
| 1243 | ifs->dataLoaderStatus = newStatus; |
| 1244 | |
| 1245 | if (newStatus == IDataLoaderStatusListener::DATA_LOADER_DESTROYED) { |
| 1246 | ifs->dataLoaderStatus = IDataLoaderStatusListener::DATA_LOADER_STOPPED; |
| 1247 | incrementalService.deleteStorageLocked(*ifs, std::move(l)); |
| 1248 | return binder::Status::ok(); |
| 1249 | } |
| 1250 | |
| 1251 | startRequested = ifs->dataLoaderStartRequested; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1252 | } |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1253 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1254 | switch (newStatus) { |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1255 | case IDataLoaderStatusListener::DATA_LOADER_CREATED: { |
Alex Buynytskyy | bf1c063 | 2020-03-10 15:49:29 -0700 | [diff] [blame] | 1256 | if (startRequested) { |
| 1257 | incrementalService.startDataLoader(mountId); |
| 1258 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1259 | break; |
| 1260 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1261 | case IDataLoaderStatusListener::DATA_LOADER_DESTROYED: { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1262 | break; |
| 1263 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1264 | case IDataLoaderStatusListener::DATA_LOADER_STARTED: { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1265 | break; |
| 1266 | } |
| 1267 | case IDataLoaderStatusListener::DATA_LOADER_STOPPED: { |
| 1268 | break; |
| 1269 | } |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 1270 | case IDataLoaderStatusListener::DATA_LOADER_IMAGE_READY: { |
| 1271 | break; |
| 1272 | } |
| 1273 | case IDataLoaderStatusListener::DATA_LOADER_IMAGE_NOT_READY: { |
| 1274 | break; |
| 1275 | } |
Alex Buynytskyy | 2cf1d18 | 2020-03-17 09:33:45 -0700 | [diff] [blame] | 1276 | case IDataLoaderStatusListener::DATA_LOADER_UNRECOVERABLE: { |
| 1277 | // Nothing for now. Rely on externalListener to handle this. |
| 1278 | break; |
| 1279 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1280 | default: { |
| 1281 | LOG(WARNING) << "Unknown data loader status: " << newStatus |
| 1282 | << " for mount: " << mountId; |
| 1283 | break; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | return binder::Status::ok(); |
| 1288 | } |
| 1289 | |
| 1290 | } // namespace android::incremental |