From 024a1241a68a912eab39e6898e3a96e4b9644845 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Fri, 10 Aug 2018 13:50:46 -0700 Subject: [PATCH] vold uses health filesystem HAL to runDevGc(). This fixes a Treble violation. Bug: 111655771 Test: manual Change-Id: I53d6db6ffa99e43e281f8086442151692c1826ca --- Android.bp | 8 +++++++ IdleMaint.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++---- main.cpp | 3 +++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/Android.bp b/Android.bp index 556784f..080c5fe 100644 --- a/Android.bp +++ b/Android.bp @@ -139,6 +139,9 @@ cc_library_static { ], }, }, + shared_libs: [ + "android.hardware.health.filesystem@1.0", + ], } cc_binary { @@ -168,6 +171,11 @@ cc_binary { "vold_prepare_subdirs", "wait_for_keymaster", ], + + shared_libs: [ + "android.hardware.health.filesystem@1.0", + "libhidltransport", + ], } cc_binary { diff --git a/IdleMaint.cpp b/IdleMaint.cpp index 7744024..b4b7746 100644 --- a/IdleMaint.cpp +++ b/IdleMaint.cpp @@ -23,11 +23,13 @@ #include #include -#include #include +#include +#include +#include #include -#include #include +#include #include #include @@ -42,6 +44,11 @@ using android::base::Realpath; using android::base::StringPrintf; using android::base::Timer; using android::base::WriteStringToFile; +using android::hardware::Return; +using android::hardware::Void; +using android::hardware::health::filesystem::V1_0::IFileSystem; +using android::hardware::health::filesystem::V1_0::IGarbageCollectCallback; +using android::hardware::health::filesystem::V1_0::Result; namespace android { namespace vold { @@ -255,7 +262,7 @@ static int stopGc(const std::list& paths) { return android::OK; } -static void runDevGc(void) { +static void runDevGcFstab(void) { std::unique_ptr fstab(fs_mgr_read_fstab_default(), fs_mgr_free_fstab); struct fstab_rec *rec = NULL; @@ -282,7 +289,7 @@ static void runDevGc(void) { PLOG(WARNING) << "Reading manual_gc failed in " << path; break; } - + require = android::base::Trim(require); if (require == "" || require == "off" || require == "disabled") { LOG(DEBUG) << "No more to do Dev GC"; break; @@ -307,6 +314,57 @@ static void runDevGc(void) { return; } +class GcCallback : public IGarbageCollectCallback { + public: + Return onFinish(Result result) override { + std::unique_lock lock(mMutex); + mFinished = true; + mResult = result; + lock.unlock(); + mCv.notify_all(); + return Void(); + } + void wait(uint64_t seconds) { + std::unique_lock lock(mMutex); + mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; }); + + if (!mFinished) { + LOG(WARNING) << "Dev GC on HAL timeout"; + } else if (mResult != Result::SUCCESS) { + LOG(WARNING) << "Dev GC on HAL failed with " << toString(mResult); + } else { + LOG(INFO) << "Dev GC on HAL successful"; + } + } + + private: + std::mutex mMutex; + std::condition_variable mCv; + bool mFinished{false}; + Result mResult{Result::UNKNOWN_ERROR}; +}; + +static void runDevGcOnHal(sp service) { + LOG(DEBUG) << "Start Dev GC on HAL"; + sp cb = new GcCallback(); + auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb); + if (!ret.isOk()) { + LOG(WARNING) << "Cannot start Dev GC on HAL: " << ret.description(); + return; + } + cb->wait(DEVGC_TIMEOUT_SEC); +} + +static void runDevGc(void) { + auto service = IFileSystem::getService(); + if (service != nullptr) { + runDevGcOnHal(service); + } else { + // fallback to legacy code path + runDevGcFstab(); + } +} + int RunIdleMaint(const android::sp& listener) { std::unique_lock lk(cv_m); if (idle_maint_stat != IdleMaintStats::kStopped) { diff --git a/main.cpp b/main.cpp index 5525e85..c4071d1 100644 --- a/main.cpp +++ b/main.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -108,6 +109,8 @@ int main(int argc, char** argv) { PLOG(ERROR) << "Error reading configuration... continuing anyways"; } + android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */); + ATRACE_BEGIN("VoldNativeService::start"); if (android::vold::VoldNativeService::start() != android::OK) { LOG(ERROR) << "Unable to start VoldNativeService";