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

Commit 993ae1f2 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes Ifab22a4c,I03eb9c78

* changes:
  fs_mgr: add fs_mgr_has_shared_blocks and make default if overlayfs
  fs_mgr: add overlayfs for small-space filesystems
parents eff7027a 3ff87d89
Loading
Loading
Loading
Loading
+46 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <selinux/selinux.h>
#include <stdio.h>
@@ -23,7 +24,9 @@
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <unistd.h>

#include <map>
@@ -35,6 +38,8 @@
#include <android-base/macros.h>
#include <android-base/properties.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <ext4_utils/ext4_utils.h>
#include <fs_mgr_overlayfs.h>
#include <fstab/fstab.h>

@@ -96,9 +101,26 @@ std::string fs_mgr_get_context(const std::string& mount_point) {
    return "";
}

// At less than 1% free space return value of false,
// means we will try to wrap with overlayfs.
bool fs_mgr_filesystem_has_space(const char* mount_point) {
    // If we have access issues to find out space remaining, return true
    // to prevent us trying to override with overlayfs.
    struct statvfs vst;
    if (statvfs(mount_point, &vst)) return true;

    static constexpr int percent = 1;  // 1%

    return (vst.f_bfree >= (vst.f_blocks * percent / 100));
}

bool fs_mgr_overlayfs_enabled(const struct fstab_rec* fsrec) {
    // readonly filesystem, can not be mount -o remount,rw
    return "squashfs"s == fsrec->fs_type;
    // if squashfs or if free space is (near) zero making such a remount
    // virtually useless, or if there are shared blocks that prevent remount,rw
    return ("squashfs"s == fsrec->fs_type) ||
           fs_mgr_has_shared_blocks(fsrec->mount_point, fsrec->blk_device) ||
           !fs_mgr_filesystem_has_space(fsrec->mount_point);
}

constexpr char upper_name[] = "upper";
@@ -175,6 +197,7 @@ bool fs_mgr_wants_overlayfs(const fstab_rec* fsrec) {

    auto fsrec_mount_point = fsrec->mount_point;
    if (!fsrec_mount_point) return false;
    if (!fsrec->blk_device) return false;

    if (!fsrec->fs_type) return false;

@@ -477,3 +500,25 @@ bool fs_mgr_overlayfs_teardown(const char* mount_point, bool* change) {
}

#endif  // ALLOW_ADBD_DISABLE_VERITY != 0

bool fs_mgr_has_shared_blocks(const std::string& mount_point, const std::string& dev) {
    struct statfs fs;
    if ((statfs((mount_point + "/lost+found").c_str(), &fs) == -1) ||
        (fs.f_type != EXT4_SUPER_MAGIC)) {
        return false;
    }

    android::base::unique_fd fd(open(dev.c_str(), O_RDONLY | O_CLOEXEC));
    if (fd < 0) return false;

    struct ext4_super_block sb;
    if ((TEMP_FAILURE_RETRY(lseek64(fd, 1024, SEEK_SET)) < 0) ||
        (TEMP_FAILURE_RETRY(read(fd, &sb, sizeof(sb))) < 0)) {
        return false;
    }

    struct fs_info info;
    if (ext4_parse_sb(&sb, &info) < 0) return false;

    return (info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SHARED_BLOCKS) != 0;
}
+3 −0
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@

#include <fstab/fstab.h>

#include <string>

bool fs_mgr_overlayfs_mount_all();
bool fs_mgr_overlayfs_setup(const char* backing = nullptr, const char* mount_point = nullptr,
                            bool* change = nullptr);
bool fs_mgr_overlayfs_teardown(const char* mount_point = nullptr, bool* change = nullptr);
bool fs_mgr_has_shared_blocks(const std::string& mount_point, const std::string& dev);