Merge "fs_mgr: check available space before creating zram backing device" into main

This commit is contained in:
Richard Chang 2024-08-02 02:53:44 +00:00 committed by Gerrit Code Review
commit 96265b3420

View file

@ -28,6 +28,7 @@
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/swap.h>
#include <sys/types.h>
#include <sys/utsname.h>
@ -2093,11 +2094,45 @@ static bool InstallZramDevice(const std::string& device) {
return true;
}
/*
* Zram backing device can be created as long as /data has at least `size`
* free space, though we may want to leave some extra space for the remaining
* boot process and other system activities.
*/
static bool ZramBackingDeviceSizeAvailable(off64_t size) {
constexpr const char* data_path = "/data";
uint64_t min_free_mb =
android::base::GetUintProperty<uint64_t>("ro.zram_backing_device_min_free_mb", 0);
// No min_free property. Skip the available size check.
if (min_free_mb == 0) return true;
struct statvfs vst;
if (statvfs(data_path, &vst) < 0) {
PERROR << "Cannot check available space: " << data_path;
return false;
}
uint64_t size_free = static_cast<uint64_t>(vst.f_bfree) * vst.f_frsize;
uint64_t size_required = size + (min_free_mb * 1024 * 1024);
if (size_required > size_free) {
PERROR << "Free space is not enough for zram backing device: " << size_required << " > "
<< size_free;
return false;
}
return true;
}
static bool PrepareZramBackingDevice(off64_t size) {
constexpr const char* file_path = "/data/per_boot/zram_swap";
if (size == 0) return true;
// Check available space
if (!ZramBackingDeviceSizeAvailable(size)) {
PERROR << "No space for target path: " << file_path;
return false;
}
// Prepare target path
unique_fd target_fd(TEMP_FAILURE_RETRY(open(file_path, O_RDWR | O_CREAT | O_CLOEXEC, 0600)));
if (target_fd.get() == -1) {
@ -2106,6 +2141,7 @@ static bool PrepareZramBackingDevice(off64_t size) {
}
if (fallocate(target_fd.get(), 0, 0, size) < 0) {
PERROR << "Cannot truncate target path: " << file_path;
unlink(file_path);
return false;
}