2015-06-26 23:02:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 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.
|
|
|
|
*/
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
#include "IdleMaint.h"
|
2015-06-26 23:02:09 +02:00
|
|
|
#include "Utils.h"
|
|
|
|
#include "VolumeManager.h"
|
2017-10-18 00:41:45 +02:00
|
|
|
#include "model/PrivateVolume.h"
|
2015-06-26 23:02:09 +02:00
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <android-base/chrono_utils.h>
|
|
|
|
#include <android-base/file.h>
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/logging.h>
|
2015-06-26 23:02:09 +02:00
|
|
|
#include <fs_mgr.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include <hardware_legacy/power.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
using android::base::Basename;
|
|
|
|
using android::base::ReadFileToString;
|
|
|
|
using android::base::Realpath;
|
2015-06-26 23:02:09 +02:00
|
|
|
using android::base::StringPrintf;
|
2017-10-18 00:41:45 +02:00
|
|
|
using android::base::Timer;
|
|
|
|
using android::base::WriteStringToFile;
|
2015-06-26 23:02:09 +02:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
enum class PathTypes {
|
|
|
|
kMountPoint = 1,
|
|
|
|
kBlkDevice,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class IdleMaintStats {
|
|
|
|
kStopped = 1,
|
|
|
|
kRunning,
|
|
|
|
kAbort,
|
|
|
|
};
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static const char* kWakeLock = "IdleMaint";
|
2017-10-18 00:41:45 +02:00
|
|
|
static const int DIRTY_SEGMENTS_THRESHOLD = 100;
|
|
|
|
static const int GC_TIMEOUT_SEC = 480;
|
2015-06-26 23:02:09 +02:00
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
|
|
|
|
static std::condition_variable cv_abort, cv_stop;
|
|
|
|
static std::mutex cv_m;
|
|
|
|
|
|
|
|
static void addFromVolumeManager(std::list<std::string>* paths,
|
|
|
|
PathTypes path_type) {
|
2015-06-26 23:02:09 +02:00
|
|
|
VolumeManager* vm = VolumeManager::Instance();
|
|
|
|
std::list<std::string> privateIds;
|
|
|
|
vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& id : privateIds) {
|
2017-10-18 00:41:45 +02:00
|
|
|
PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
|
2015-06-26 23:02:09 +02:00
|
|
|
if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
|
2017-10-18 00:41:45 +02:00
|
|
|
if (path_type == PathTypes::kMountPoint) {
|
|
|
|
paths->push_back(vol->getPath());
|
|
|
|
} else if (path_type == PathTypes::kBlkDevice) {
|
|
|
|
std::string gc_path;
|
|
|
|
const std::string& fs_type = vol->getFsType();
|
|
|
|
if (fs_type == "f2fs" &&
|
|
|
|
Realpath(vol->getRawDevPath(), &gc_path)) {
|
|
|
|
paths->push_back(std::string("/sys/fs/") + fs_type +
|
|
|
|
"/" + Basename(gc_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
|
2017-03-09 16:11:33 +01:00
|
|
|
std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
|
|
|
|
fs_mgr_free_fstab);
|
2015-06-26 23:02:09 +02:00
|
|
|
struct fstab_rec *prev_rec = NULL;
|
|
|
|
|
|
|
|
for (int i = 0; i < fstab->num_entries; i++) {
|
2017-10-07 02:02:53 +02:00
|
|
|
auto fs_type = std::string(fstab->recs[i].fs_type);
|
2015-06-26 23:02:09 +02:00
|
|
|
/* Skip raw partitions */
|
2017-10-07 02:02:53 +02:00
|
|
|
if (fs_type == "emmc" || fs_type == "mtd") {
|
2015-06-26 23:02:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Skip read-only filesystems */
|
|
|
|
if (fstab->recs[i].flags & MS_RDONLY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
|
|
|
|
continue; /* Should we trim fat32 filesystems? */
|
|
|
|
}
|
|
|
|
if (fs_mgr_is_notrim(&fstab->recs[i])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip the multi-type partitions, which are required to be following each other.
|
|
|
|
* See fs_mgr.c's mount_with_alternatives().
|
|
|
|
*/
|
|
|
|
if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
if (path_type == PathTypes::kMountPoint) {
|
|
|
|
paths->push_back(fstab->recs[i].mount_point);
|
|
|
|
} else if (path_type == PathTypes::kBlkDevice) {
|
|
|
|
std::string gc_path;
|
|
|
|
if (std::string(fstab->recs[i].fs_type) == "f2fs" &&
|
|
|
|
Realpath(fstab->recs[i].blk_device, &gc_path)) {
|
|
|
|
paths->push_back(std::string("/sys/fs/") + fstab->recs[i].fs_type +
|
|
|
|
"/" + Basename(gc_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
prev_rec = &fstab->recs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
|
2015-06-26 23:02:09 +02:00
|
|
|
acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
// Collect both fstab and vold volumes
|
|
|
|
std::list<std::string> paths;
|
2017-10-18 00:41:45 +02:00
|
|
|
addFromFstab(&paths, PathTypes::kMountPoint);
|
|
|
|
addFromVolumeManager(&paths, PathTypes::kMountPoint);
|
2017-10-18 00:06:32 +02:00
|
|
|
|
|
|
|
for (const auto& path : paths) {
|
2015-06-26 23:02:09 +02:00
|
|
|
LOG(DEBUG) << "Starting trim of " << path;
|
|
|
|
|
2017-09-15 20:57:44 +02:00
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
extras.putString(String16("path"), String16(path.c_str()));
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
|
|
|
|
if (fd < 0) {
|
|
|
|
PLOG(WARNING) << "Failed to open " << path;
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onStatus(-1, extras);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fstrim_range range;
|
|
|
|
memset(&range, 0, sizeof(range));
|
|
|
|
range.len = ULLONG_MAX;
|
|
|
|
|
|
|
|
nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
|
2017-10-18 00:06:32 +02:00
|
|
|
if (ioctl(fd, FITRIM, &range)) {
|
2015-06-26 23:02:09 +02:00
|
|
|
PLOG(WARNING) << "Trim failed on " << path;
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onStatus(-1, extras);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
} else {
|
2017-09-15 20:57:44 +02:00
|
|
|
nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
|
2015-06-26 23:02:09 +02:00
|
|
|
LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
|
2017-09-15 20:57:44 +02:00
|
|
|
<< " in " << nanoseconds_to_milliseconds(time) << "ms";
|
|
|
|
extras.putLong(String16("bytes"), range.len);
|
|
|
|
extras.putLong(String16("time"), time);
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onStatus(0, extras);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
|
|
|
close(fd);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
2017-09-15 20:57:44 +02:00
|
|
|
android::os::PersistableBundle extras;
|
2017-10-18 00:06:32 +02:00
|
|
|
listener->onFinished(0, extras);
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
release_wake_lock(kWakeLock);
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
static bool waitForGc(const std::list<std::string>& paths) {
|
|
|
|
std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
|
|
|
|
bool stop = false, aborted = false;
|
|
|
|
Timer timer;
|
|
|
|
|
|
|
|
while (!stop && !aborted) {
|
|
|
|
stop = true;
|
|
|
|
for (const auto& path : paths) {
|
|
|
|
std::string dirty_segments;
|
|
|
|
if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
|
|
|
|
PLOG(WARNING) << "Reading dirty_segments failed in " << path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
|
|
|
|
stop = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop) break;
|
|
|
|
|
|
|
|
if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
|
|
|
|
LOG(WARNING) << "GC timeout";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lk.lock();
|
|
|
|
aborted = cv_abort.wait_for(lk, 10s, []{
|
|
|
|
return idle_maint_stat == IdleMaintStats::kAbort;});
|
|
|
|
lk.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return aborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int startGc(const std::list<std::string>& paths) {
|
|
|
|
for (const auto& path : paths) {
|
|
|
|
LOG(DEBUG) << "Start GC on " << path;
|
2018-02-17 15:02:30 +01:00
|
|
|
if (!WriteStringToFile("1", path + "/discard_granularity")) {
|
|
|
|
PLOG(WARNING) << "Set discard gralunarity failed on" << path;
|
|
|
|
}
|
2017-10-18 00:41:45 +02:00
|
|
|
if (!WriteStringToFile("1", path + "/gc_urgent")) {
|
|
|
|
PLOG(WARNING) << "Start GC failed on " << path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stopGc(const std::list<std::string>& paths) {
|
|
|
|
for (const auto& path : paths) {
|
|
|
|
LOG(DEBUG) << "Stop GC on " << path;
|
|
|
|
if (!WriteStringToFile("0", path + "/gc_urgent")) {
|
|
|
|
PLOG(WARNING) << "Stop GC failed on " << path;
|
|
|
|
}
|
2018-02-17 15:02:30 +01:00
|
|
|
if (!WriteStringToFile("16", path + "/discard_granularity")) {
|
|
|
|
PLOG(WARNING) << "Set discard gralunarity failed on" << path;
|
|
|
|
}
|
2017-10-18 00:41:45 +02:00
|
|
|
}
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
|
|
|
|
std::unique_lock<std::mutex> lk(cv_m);
|
|
|
|
if (idle_maint_stat != IdleMaintStats::kStopped) {
|
|
|
|
LOG(DEBUG) << "idle maintenance is already running";
|
|
|
|
if (listener) {
|
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
listener->onFinished(0, extras);
|
|
|
|
}
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
idle_maint_stat = IdleMaintStats::kRunning;
|
|
|
|
lk.unlock();
|
|
|
|
|
|
|
|
LOG(DEBUG) << "idle maintenance started";
|
|
|
|
|
|
|
|
acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
|
|
|
|
|
|
|
|
std::list<std::string> paths;
|
|
|
|
addFromFstab(&paths, PathTypes::kBlkDevice);
|
|
|
|
addFromVolumeManager(&paths, PathTypes::kBlkDevice);
|
|
|
|
|
|
|
|
startGc(paths);
|
|
|
|
|
|
|
|
bool gc_aborted = waitForGc(paths);
|
|
|
|
|
|
|
|
stopGc(paths);
|
|
|
|
|
|
|
|
lk.lock();
|
|
|
|
idle_maint_stat = IdleMaintStats::kStopped;
|
|
|
|
lk.unlock();
|
|
|
|
|
|
|
|
cv_stop.notify_one();
|
|
|
|
|
|
|
|
if (!gc_aborted) {
|
|
|
|
Trim(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
listener->onFinished(0, extras);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "idle maintenance completed";
|
|
|
|
|
|
|
|
release_wake_lock(kWakeLock);
|
|
|
|
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
|
|
|
|
acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lk(cv_m);
|
|
|
|
if (idle_maint_stat != IdleMaintStats::kStopped) {
|
|
|
|
idle_maint_stat = IdleMaintStats::kAbort;
|
|
|
|
lk.unlock();
|
|
|
|
cv_abort.notify_one();
|
|
|
|
lk.lock();
|
|
|
|
LOG(DEBUG) << "aborting idle maintenance";
|
|
|
|
cv_stop.wait(lk, []{
|
|
|
|
return idle_maint_stat == IdleMaintStats::kStopped;});
|
|
|
|
}
|
|
|
|
lk.unlock();
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
listener->onFinished(0, extras);
|
|
|
|
}
|
|
|
|
|
|
|
|
release_wake_lock(kWakeLock);
|
|
|
|
|
|
|
|
LOG(DEBUG) << "idle maintenance stopped";
|
|
|
|
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|