Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1526a46e authored by David Anderson's avatar David Anderson Committed by Gerrit Code Review
Browse files

Merge "fs_mgr: Move libfiemap back to fs_mgr from system/gsid."

parents e4b93114 4fe39801
Loading
Loading
Loading
Loading
+135 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

cc_library_headers {
    name: "libfiemap_headers",
    recovery_available: true,
    export_include_dirs: ["include"],
}

cc_defaults {
    name: "libfiemap_defaults",
    defaults: ["fs_mgr_defaults"],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],

    srcs: [
        "fiemap_writer.cpp",
        "image_manager.cpp",
        "metadata.cpp",
        "split_fiemap_writer.cpp",
        "utility.cpp",
    ],

    static_libs: [
        "libdm",
        "libext2_uuid",
        "libext4_utils",
        "liblp",
        "libfs_mgr",
    ],

    shared_libs: [
        "libbase",
    ],

    header_libs: [
        "libfiemap_headers",
        "liblog_headers",
    ],

    export_shared_lib_headers: [
        "libbase",
    ],

    export_header_lib_headers: [
        "libfiemap_headers",
    ],
}

// Open up a binder IImageManager interface.
cc_library_static {
    name: "libfiemap_binder",
    defaults: ["libfiemap_defaults"],
    srcs: [
        "binder.cpp",
    ],
    whole_static_libs: [
        "gsi_aidl_interface-cpp",
        "libgsi",
    ],
    shared_libs: [
        "libbinder",
    ],
}

// Open up a passthrough IImageManager interface. Use libfiemap_binder whenever
// possible. This should only be used when binder is not available.
cc_library_static {
    name: "libfiemap_passthrough",
    defaults: ["libfiemap_defaults"],
    recovery_available: true,
    srcs: [
        "passthrough.cpp",
    ],
}

cc_test {
    name: "fiemap_writer_test",
    defaults: ["libfiemap_defaults"],
    static_libs: [
        "libbase",
        "libdm",
        "liblog",
    ],

    data: [
        "testdata/unaligned_file",
        "testdata/file_4k",
        "testdata/file_32k",
    ],

    srcs: [
        "fiemap_writer_test.cpp",
    ],
}

cc_test {
    name: "fiemap_image_test",
    defaults: ["libfiemap_defaults"],
    static_libs: [
        "libdm",
        "libext4_utils",
        "libfs_mgr",
        "liblp",
    ],
    shared_libs: [
        "libcrypto",
        "libcrypto_utils",
        "libcutils",
        "liblog",
    ],
    srcs: [
        "image_test.cpp",
    ],
}

vts_config {
    name: "VtsFiemapWriterTest",
}
+30 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2019 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<configuration description="Config for VTS VtsFiemapWriterTest">
    <option name="config-descriptor:metadata" key="plan" value="vts-kernel" />
    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.VtsFilePusher">
        <option name="abort-on-push-failure" value="false"/>
        <option name="push-group" value="HostDrivenTest.push"/>
    </target_preparer>
    <test class="com.android.tradefed.testtype.VtsMultiDeviceTest">
      <option name="test-module-name" value="VtsFiemapWriterTest"/>
        <option name="binary-test-source" value="_32bit::DATA/nativetest/fiemap_writer_test/fiemap_writer_test" />
        <option name="binary-test-source" value="_64bit::DATA/nativetest64/fiemap_writer_test/fiemap_writer_test" />
        <option name="binary-test-type" value="gtest"/>
        <option name="precondition-first-api-level" value="29" />
        <option name="test-timeout" value="1m"/>
    </test>
</configuration>
+75 −0
Original line number Diff line number Diff line
libfiemap
=============

`libfiemap` is a library for creating block-devices that are backed by
storage in read-write partitions. It exists primary for gsid. Generally, the
library works by using `libfiemap_writer` to allocate large files within
filesystem, and then tracks their extents.

There are three main uses for `libfiemap`:
 - Creating images that will act as block devices. For example, gsid needs to
   create a `system_gsi` image to store Dynamic System Updates.
 - Mapping the image as a block device while /data is mounted. This is fairly
   tricky and is described in more detail below.
 - Mapping the image as a block device during first-stage init. This is simple
   because it uses the same logic from dynamic partitions.

Image creation is done through `SplitFiemap`. Depending on the file system,
a large image may have to be split into multiple files. On Ext4 the limit is
16GiB and on FAT32 it's 4GiB. Images are saved into `/data/gsi/<name>/`
where `<name>` is chosen by the process requesting the image.

At the same time, a file called `/metadata/gsi/<name>/lp_metadata` is created.
This is a super partition header that allows first-stage init to create dynamic
partitions from the image files. It also tracks the canonical size of the image,
since the file size may be larger due to alignment.

Mapping
-------

It is easy to make block devices out of blocks on `/data` when it is not
mounted, so first-stage init has no issues mapping dynamic partitions from
images. After `/data` is mounted however, there are two problems:
 - `/data` is encrypted.
 - `/dev/block/by-name/data` may be marked as in-use.

We break the problem down into three scenarios.

### FDE and Metadata Encrypted Devices

When FDE or metadata encryption is used, `/data` is not mounted from
`/dev/block/by-name/data`. Instead, it is mounted from an intermediate
`dm-crypt` or `dm-default-key` device. This means the underlying device is
not marked in use, and we can create new dm-linear devices on top of it.

On these devices, a block device for an image will consist of a single
device-mapper device with a `dm-linear` table entry for each extent in the
backing file.

### Unencrypted and FBE-encrypted Devices

When a device is unencrypted, or is encrypted with FBE but not metadata
encryption, we instead use a loop device with `LOOP_SET_DIRECT_IO` enabled.
Since `/data/gsi` has encryption disabled, this means the raw blocks will be
unencrypted as well.

### Split Images

If an image was too large to store a single file on the underlying filesystem,
on an FBE/unencrypted device we will have multiple loop devices. In this case,
we create a device-mapper device as well. For each loop device it will have one
`dm-linear` table entry spanning the length of the device.

State Tracking
--------------

It's important that we know whether or not an image is currently in-use by a
block device. It could be catastrophic to write to a dm-linear device if the
underlying blocks are no longer owned by the original file. Thus, when mapping
an image, we create a property called `gsid.mapped_image.<name>` and set it to
the path of the block device.

Additionally, we create a `/metadata/gsi/<subdir>/<name>.status` file. Each
line in this file denotes a dependency on either a device-mapper node or a loop
device. When deleting a block device, this file is used to release all
resources.
+224 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#if !defined(__ANDROID_RECOVERY__)
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android/gsi/IGsiService.h>
#include <android/gsi/IGsid.h>
#include <binder/IServiceManager.h>
#include <libfiemap/image_manager.h>
#include <libgsi/libgsi.h>

namespace android {
namespace fiemap {

using namespace android::gsi;
using namespace std::chrono_literals;

class ImageManagerBinder final : public IImageManager {
  public:
    ImageManagerBinder(android::sp<IGsiService>&& service, android::sp<IImageService>&& manager);
    bool CreateBackingImage(const std::string& name, uint64_t size, int flags) override;
    bool DeleteBackingImage(const std::string& name) override;
    bool MapImageDevice(const std::string& name, const std::chrono::milliseconds& timeout_ms,
                        std::string* path) override;
    bool UnmapImageDevice(const std::string& name) override;
    bool BackingImageExists(const std::string& name) override;
    bool IsImageMapped(const std::string& name) override;
    bool MapImageWithDeviceMapper(const IPartitionOpener& opener, const std::string& name,
                                  std::string* dev) override;
    bool ZeroFillNewImage(const std::string& name, uint64_t bytes) override;
    bool RemoveAllImages() override;

    std::vector<std::string> GetAllBackingImages() override;

  private:
    android::sp<IGsiService> service_;
    android::sp<IImageService> manager_;
};

ImageManagerBinder::ImageManagerBinder(android::sp<IGsiService>&& service,
                                       android::sp<IImageService>&& manager)
    : service_(std::move(service)), manager_(std::move(manager)) {}

bool ImageManagerBinder::CreateBackingImage(const std::string& name, uint64_t size, int flags) {
    auto status = manager_->createBackingImage(name, size, flags);
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
        return false;
    }
    return true;
}

bool ImageManagerBinder::DeleteBackingImage(const std::string& name) {
    auto status = manager_->deleteBackingImage(name);
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
        return false;
    }
    return true;
}

bool ImageManagerBinder::MapImageDevice(const std::string& name,
                                        const std::chrono::milliseconds& timeout_ms,
                                        std::string* path) {
    int32_t timeout_ms_count =
            static_cast<int32_t>(std::clamp<typename std::chrono::milliseconds::rep>(
                    timeout_ms.count(), INT32_MIN, INT32_MAX));
    MappedImage map;
    auto status = manager_->mapImageDevice(name, timeout_ms_count, &map);
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
        return false;
    }
    *path = map.path;
    return true;
}

bool ImageManagerBinder::UnmapImageDevice(const std::string& name) {
    auto status = manager_->unmapImageDevice(name);
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
        return false;
    }
    return true;
}

bool ImageManagerBinder::BackingImageExists(const std::string& name) {
    bool retval;
    auto status = manager_->backingImageExists(name, &retval);
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
        return false;
    }
    return retval;
}

bool ImageManagerBinder::IsImageMapped(const std::string& name) {
    bool retval;
    auto status = manager_->isImageMapped(name, &retval);
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
        return false;
    }
    return retval;
}

bool ImageManagerBinder::MapImageWithDeviceMapper(const IPartitionOpener& opener,
                                                  const std::string& name, std::string* dev) {
    (void)opener;
    (void)name;
    (void)dev;
    LOG(ERROR) << "MapImageWithDeviceMapper is not available over binder.";
    return false;
}

std::vector<std::string> ImageManagerBinder::GetAllBackingImages() {
    std::vector<std::string> retval;
    auto status = manager_->getAllBackingImages(&retval);
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
    }
    return retval;
}

bool ImageManagerBinder::ZeroFillNewImage(const std::string& name, uint64_t bytes) {
    auto status = manager_->zeroFillNewImage(name, bytes);
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
        return false;
    }
    return true;
}

bool ImageManagerBinder::RemoveAllImages() {
    auto status = manager_->removeAllImages();
    if (!status.isOk()) {
        LOG(ERROR) << __PRETTY_FUNCTION__
                   << " binder returned: " << status.exceptionMessage().string();
        return false;
    }
    return true;
}

static android::sp<IGsid> AcquireIGsid(const std::chrono::milliseconds& timeout_ms) {
    if (android::base::GetProperty("init.svc.gsid", "") != "running") {
        if (!android::base::SetProperty("ctl.start", "gsid") ||
            !android::base::WaitForProperty("init.svc.gsid", "running", timeout_ms)) {
            LOG(ERROR) << "Could not start the gsid service";
            return nullptr;
        }
        // Sleep for 250ms to give the service time to register.
        usleep(250 * 1000);
    }
    auto sm = android::defaultServiceManager();
    auto name = android::String16(kGsiServiceName);
    auto service = sm->checkService(name);
    return android::interface_cast<IGsid>(service);
}

static android::sp<IGsid> GetGsiService(const std::chrono::milliseconds& timeout_ms) {
    auto start_time = std::chrono::steady_clock::now();

    std::chrono::milliseconds elapsed = std::chrono::milliseconds::zero();
    do {
        if (auto gsid = AcquireIGsid(timeout_ms - elapsed); gsid != nullptr) {
            return gsid;
        }
        auto now = std::chrono::steady_clock::now();
        elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
    } while (elapsed <= timeout_ms);

    LOG(ERROR) << "Timed out trying to acquire IGsid interface";
    return nullptr;
}

std::unique_ptr<IImageManager> IImageManager::Open(const std::string& dir,
                                                   const std::chrono::milliseconds& timeout_ms) {
    auto gsid = GetGsiService(timeout_ms);
    if (!gsid) {
        return nullptr;
    }

    android::sp<IGsiService> service;
    auto status = gsid->getClient(&service);
    if (!status.isOk() || !service) {
        LOG(ERROR) << "Could not acquire IGsiService";
        return nullptr;
    }

    android::sp<IImageService> manager;
    status = service->openImageService(dir, &manager);
    if (!status.isOk() || !manager) {
        LOG(ERROR) << "Could not acquire IImageManager: " << status.exceptionMessage().string();
        return nullptr;
    }
    return std::make_unique<ImageManagerBinder>(std::move(service), std::move(manager));
}

}  // namespace fiemap
}  // namespace android

#endif  // __ANDROID_RECOVERY__
+782 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading