blob: 8005cf4b65ced2188b3005a7e488295a5a25b8b5 [file] [log] [blame]
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060017#include "IdleMaint.h"
Jaegeuk Kim31e962f2018-07-29 06:56:57 -070018#include "FileDeviceUtils.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070019#include "Utils.h"
Eric Biggers019d5162020-10-15 16:54:38 -070020#include "VoldUtil.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070021#include "VolumeManager.h"
Jin Qiana370c142017-10-17 15:41:45 -070022#include "model/PrivateVolume.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070023
Jin Qiana370c142017-10-17 15:41:45 -070024#include <thread>
Yifan Hong8f0d4542021-01-13 17:10:47 -080025#include <utility>
Jin Qiana370c142017-10-17 15:41:45 -070026
Yifan Honge1e49452021-01-13 17:27:42 -080027#include <aidl/android/hardware/health/storage/BnGarbageCollectCallback.h>
28#include <aidl/android/hardware/health/storage/IStorage.h>
Jin Qiana370c142017-10-17 15:41:45 -070029#include <android-base/chrono_utils.h>
30#include <android-base/file.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080031#include <android-base/logging.h>
Yifan Hong024a1242018-08-10 13:50:46 -070032#include <android-base/stringprintf.h>
33#include <android-base/strings.h>
Yifan Honge1e49452021-01-13 17:27:42 -080034#include <android/binder_manager.h>
Yifan Hong7a37c932018-09-19 10:28:16 -070035#include <android/hardware/health/storage/1.0/IStorage.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070036#include <fs_mgr.h>
Yifan Hong024a1242018-08-10 13:50:46 -070037#include <private/android_filesystem_config.h>
Tri Vo15bbe222019-06-21 12:21:48 -070038#include <wakelock/wakelock.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070039
40#include <dirent.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070041#include <fcntl.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070042#include <sys/mount.h>
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <sys/wait.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070046
Jin Qiana370c142017-10-17 15:41:45 -070047using android::base::Basename;
48using android::base::ReadFileToString;
49using android::base::Realpath;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070050using android::base::StringPrintf;
Jin Qiana370c142017-10-17 15:41:45 -070051using android::base::Timer;
52using android::base::WriteStringToFile;
Yifan Hong024a1242018-08-10 13:50:46 -070053using android::hardware::Return;
54using android::hardware::Void;
Yifan Honge1e49452021-01-13 17:27:42 -080055using AStorage = aidl::android::hardware::health::storage::IStorage;
56using ABnGarbageCollectCallback =
57 aidl::android::hardware::health::storage::BnGarbageCollectCallback;
58using AResult = aidl::android::hardware::health::storage::Result;
Yifan Hong8f0d4542021-01-13 17:10:47 -080059using HStorage = android::hardware::health::storage::V1_0::IStorage;
60using HGarbageCollectCallback = android::hardware::health::storage::V1_0::IGarbageCollectCallback;
61using HResult = android::hardware::health::storage::V1_0::Result;
Yifan Honge1e49452021-01-13 17:27:42 -080062using std::string_literals::operator""s;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070063
64namespace android {
65namespace vold {
66
Jin Qiana370c142017-10-17 15:41:45 -070067enum class PathTypes {
68 kMountPoint = 1,
69 kBlkDevice,
70};
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070071
Jin Qiana370c142017-10-17 15:41:45 -070072enum class IdleMaintStats {
73 kStopped = 1,
74 kRunning,
75 kAbort,
76};
77
78static const char* kWakeLock = "IdleMaint";
79static const int DIRTY_SEGMENTS_THRESHOLD = 100;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -080080/*
81 * Timing policy:
82 * 1. F2FS_GC = 7 mins
83 * 2. Trim = 1 min
84 * 3. Dev GC = 2 mins
85 */
86static const int GC_TIMEOUT_SEC = 420;
87static const int DEVGC_TIMEOUT_SEC = 120;
Jin Qiana370c142017-10-17 15:41:45 -070088
89static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
90static std::condition_variable cv_abort, cv_stop;
91static std::mutex cv_m;
92
Paul Crowley14c8c072018-09-18 13:30:21 -070093static void addFromVolumeManager(std::list<std::string>* paths, PathTypes path_type) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070094 VolumeManager* vm = VolumeManager::Instance();
95 std::list<std::string> privateIds;
96 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070097 for (const auto& id : privateIds) {
Jin Qiana370c142017-10-17 15:41:45 -070098 PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070099 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
Jin Qiana370c142017-10-17 15:41:45 -0700100 if (path_type == PathTypes::kMountPoint) {
101 paths->push_back(vol->getPath());
102 } else if (path_type == PathTypes::kBlkDevice) {
103 std::string gc_path;
104 const std::string& fs_type = vol->getFsType();
Jaegeuk Kim31e962f2018-07-29 06:56:57 -0700105 if (fs_type == "f2fs" && (Realpath(vol->getRawDmDevPath(), &gc_path) ||
106 Realpath(vol->getRawDevPath(), &gc_path))) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700107 paths->push_back(std::string("/sys/fs/") + fs_type + "/" + Basename(gc_path));
Jin Qiana370c142017-10-17 15:41:45 -0700108 }
109 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700110 }
111 }
112}
113
Jin Qiana370c142017-10-17 15:41:45 -0700114static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800115 std::string previous_mount_point;
Eric Biggers019d5162020-10-15 16:54:38 -0700116 for (const auto& entry : fstab_default) {
Eric Biggers9a3dc8c2020-10-15 16:54:38 -0700117 // Skip raw partitions and swap space.
118 if (entry.fs_type == "emmc" || entry.fs_type == "mtd" || entry.fs_type == "swap") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700119 continue;
120 }
Eric Biggers9a3dc8c2020-10-15 16:54:38 -0700121 // Skip read-only filesystems and bind mounts.
122 if (entry.flags & (MS_RDONLY | MS_BIND)) {
123 continue;
124 }
125 // Skip anything without an underlying block device, e.g. virtiofs.
126 if (entry.blk_device[0] != '/') {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700127 continue;
128 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800129 if (entry.fs_mgr_flags.vold_managed) {
130 continue; // Should we trim fat32 filesystems?
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700131 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800132 if (entry.fs_mgr_flags.no_trim) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700133 continue;
134 }
135
Tom Cherry4c5bde22019-01-29 14:34:01 -0800136 // Skip the multi-type partitions, which are required to be following each other.
137 // See fs_mgr.c's mount_with_alternatives().
138 if (entry.mount_point == previous_mount_point) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700139 continue;
140 }
141
Jin Qiana370c142017-10-17 15:41:45 -0700142 if (path_type == PathTypes::kMountPoint) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800143 paths->push_back(entry.mount_point);
Jin Qiana370c142017-10-17 15:41:45 -0700144 } else if (path_type == PathTypes::kBlkDevice) {
145 std::string gc_path;
Tom Cherry4c5bde22019-01-29 14:34:01 -0800146 if (entry.fs_type == "f2fs" &&
147 Realpath(android::vold::BlockDeviceForPath(entry.mount_point + "/"), &gc_path)) {
148 paths->push_back("/sys/fs/" + entry.fs_type + "/" + Basename(gc_path));
Jin Qiana370c142017-10-17 15:41:45 -0700149 }
150 }
151
Tom Cherry4c5bde22019-01-29 14:34:01 -0800152 previous_mount_point = entry.mount_point;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700153 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700154}
155
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600156void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
Kalesh Singh98062dc2021-02-22 15:10:45 -0500157 auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
158 if (!wl.has_value()) {
159 return;
160 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700161
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600162 // Collect both fstab and vold volumes
163 std::list<std::string> paths;
Jin Qiana370c142017-10-17 15:41:45 -0700164 addFromFstab(&paths, PathTypes::kMountPoint);
165 addFromVolumeManager(&paths, PathTypes::kMountPoint);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600166
167 for (const auto& path : paths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700168 LOG(DEBUG) << "Starting trim of " << path;
169
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600170 android::os::PersistableBundle extras;
171 extras.putString(String16("path"), String16(path.c_str()));
172
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700173 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
174 if (fd < 0) {
175 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600176 if (listener) {
177 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600178 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700179 continue;
180 }
181
182 struct fstrim_range range;
183 memset(&range, 0, sizeof(range));
184 range.len = ULLONG_MAX;
185
186 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600187 if (ioctl(fd, FITRIM, &range)) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700188 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600189 if (listener) {
190 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600191 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700192 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600193 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Paul Crowley14c8c072018-09-18 13:30:21 -0700194 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in "
195 << nanoseconds_to_milliseconds(time) << "ms";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600196 extras.putLong(String16("bytes"), range.len);
197 extras.putLong(String16("time"), time);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600198 if (listener) {
199 listener->onStatus(0, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600200 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700201 }
202 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600203 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700204
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600205 if (listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600206 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600207 listener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700208 }
209
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700210}
211
Jin Qiana370c142017-10-17 15:41:45 -0700212static bool waitForGc(const std::list<std::string>& paths) {
213 std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
214 bool stop = false, aborted = false;
215 Timer timer;
216
217 while (!stop && !aborted) {
218 stop = true;
219 for (const auto& path : paths) {
220 std::string dirty_segments;
221 if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
222 PLOG(WARNING) << "Reading dirty_segments failed in " << path;
223 continue;
224 }
225 if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
226 stop = false;
227 break;
228 }
229 }
230
231 if (stop) break;
232
233 if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
234 LOG(WARNING) << "GC timeout";
235 break;
236 }
237
238 lk.lock();
Paul Crowley14c8c072018-09-18 13:30:21 -0700239 aborted =
240 cv_abort.wait_for(lk, 10s, [] { return idle_maint_stat == IdleMaintStats::kAbort; });
Jin Qiana370c142017-10-17 15:41:45 -0700241 lk.unlock();
242 }
243
244 return aborted;
245}
246
247static int startGc(const std::list<std::string>& paths) {
248 for (const auto& path : paths) {
249 LOG(DEBUG) << "Start GC on " << path;
250 if (!WriteStringToFile("1", path + "/gc_urgent")) {
251 PLOG(WARNING) << "Start GC failed on " << path;
252 }
253 }
254 return android::OK;
255}
256
257static int stopGc(const std::list<std::string>& paths) {
258 for (const auto& path : paths) {
259 LOG(DEBUG) << "Stop GC on " << path;
260 if (!WriteStringToFile("0", path + "/gc_urgent")) {
261 PLOG(WARNING) << "Stop GC failed on " << path;
262 }
263 }
264 return android::OK;
265}
266
Yifan Hong024a1242018-08-10 13:50:46 -0700267static void runDevGcFstab(void) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800268 std::string path;
Eric Biggers019d5162020-10-15 16:54:38 -0700269 for (const auto& entry : fstab_default) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800270 if (!entry.sysfs_path.empty()) {
271 path = entry.sysfs_path;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800272 break;
273 }
274 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800275
276 if (path.empty()) {
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800277 return;
278 }
279
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800280 path = path + "/manual_gc";
281 Timer timer;
282
283 LOG(DEBUG) << "Start Dev GC on " << path;
284 while (1) {
285 std::string require;
286 if (!ReadFileToString(path, &require)) {
287 PLOG(WARNING) << "Reading manual_gc failed in " << path;
288 break;
289 }
Yifan Hong024a1242018-08-10 13:50:46 -0700290 require = android::base::Trim(require);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800291 if (require == "" || require == "off" || require == "disabled") {
292 LOG(DEBUG) << "No more to do Dev GC";
293 break;
294 }
295
296 LOG(DEBUG) << "Trigger Dev GC on " << path;
297 if (!WriteStringToFile("1", path)) {
298 PLOG(WARNING) << "Start Dev GC failed on " << path;
299 break;
300 }
301
302 if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
303 LOG(WARNING) << "Dev GC timeout";
304 break;
305 }
306 sleep(2);
307 }
308 LOG(DEBUG) << "Stop Dev GC on " << path;
309 if (!WriteStringToFile("0", path)) {
310 PLOG(WARNING) << "Stop Dev GC failed on " << path;
311 }
312 return;
313}
314
Yifan Honge1e49452021-01-13 17:27:42 -0800315enum class IDL { HIDL, AIDL };
Yifan Hong8f0d4542021-01-13 17:10:47 -0800316std::ostream& operator<<(std::ostream& os, IDL idl) {
Yifan Honge1e49452021-01-13 17:27:42 -0800317 return os << (idl == IDL::HIDL ? "HIDL" : "AIDL");
Yifan Hong8f0d4542021-01-13 17:10:47 -0800318}
319
320template <IDL idl, typename Result>
321class GcCallbackImpl {
322 protected:
323 void onFinishInternal(Result result) {
Yifan Hong024a1242018-08-10 13:50:46 -0700324 std::unique_lock<std::mutex> lock(mMutex);
325 mFinished = true;
326 mResult = result;
327 lock.unlock();
328 mCv.notify_all();
Yifan Hong024a1242018-08-10 13:50:46 -0700329 }
Yifan Hong8f0d4542021-01-13 17:10:47 -0800330
331 public:
Yifan Hong024a1242018-08-10 13:50:46 -0700332 void wait(uint64_t seconds) {
333 std::unique_lock<std::mutex> lock(mMutex);
334 mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; });
335
336 if (!mFinished) {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800337 LOG(WARNING) << "Dev GC on " << idl << " HAL timeout";
Yifan Hong024a1242018-08-10 13:50:46 -0700338 } else if (mResult != Result::SUCCESS) {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800339 LOG(WARNING) << "Dev GC on " << idl << " HAL failed with " << toString(mResult);
Yifan Hong024a1242018-08-10 13:50:46 -0700340 } else {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800341 LOG(INFO) << "Dev GC on " << idl << " HAL successful";
Yifan Hong024a1242018-08-10 13:50:46 -0700342 }
343 }
344
345 private:
346 std::mutex mMutex;
347 std::condition_variable mCv;
348 bool mFinished{false};
349 Result mResult{Result::UNKNOWN_ERROR};
350};
351
Yifan Honge1e49452021-01-13 17:27:42 -0800352class AGcCallbackImpl : public ABnGarbageCollectCallback,
353 public GcCallbackImpl<IDL::AIDL, AResult> {
354 ndk::ScopedAStatus onFinish(AResult result) override {
355 onFinishInternal(result);
356 return ndk::ScopedAStatus::ok();
357 }
358};
359
Yifan Hong8f0d4542021-01-13 17:10:47 -0800360class HGcCallbackImpl : public HGarbageCollectCallback, public GcCallbackImpl<IDL::HIDL, HResult> {
361 Return<void> onFinish(HResult result) override {
362 onFinishInternal(result);
363 return Void();
364 }
365};
366
367template <IDL idl, typename Service, typename GcCallbackImpl, typename GetDescription>
368static void runDevGcOnHal(Service service, GcCallbackImpl cb, GetDescription get_description) {
369 LOG(DEBUG) << "Start Dev GC on " << idl << " HAL";
Yifan Hong024a1242018-08-10 13:50:46 -0700370 auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb);
371 if (!ret.isOk()) {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800372 LOG(WARNING) << "Cannot start Dev GC on " << idl
373 << " HAL: " << std::invoke(get_description, ret);
Yifan Hong024a1242018-08-10 13:50:46 -0700374 return;
375 }
376 cb->wait(DEVGC_TIMEOUT_SEC);
377}
378
379static void runDevGc(void) {
Yifan Honge1e49452021-01-13 17:27:42 -0800380 auto aidl_service_name = AStorage::descriptor + "/default"s;
381 if (AServiceManager_isDeclared(aidl_service_name.c_str())) {
382 ndk::SpAIBinder binder(AServiceManager_waitForService(aidl_service_name.c_str()));
383 if (binder.get() != nullptr) {
384 std::shared_ptr<AStorage> aidl_service = AStorage::fromBinder(binder);
385 if (aidl_service != nullptr) {
386 runDevGcOnHal<IDL::AIDL>(aidl_service, ndk::SharedRefBase::make<AGcCallbackImpl>(),
387 &ndk::ScopedAStatus::getDescription);
388 return;
389 }
390 }
391 LOG(WARNING) << "Device declares " << aidl_service_name
392 << " but it is not running, skip dev GC on AIDL HAL";
393 return;
394 }
Yifan Hong8f0d4542021-01-13 17:10:47 -0800395 auto hidl_service = HStorage::getService();
396 if (hidl_service != nullptr) {
397 runDevGcOnHal<IDL::HIDL>(hidl_service, sp<HGcCallbackImpl>(new HGcCallbackImpl()),
398 &Return<void>::description);
399 return;
Yifan Hong024a1242018-08-10 13:50:46 -0700400 }
Yifan Hong8f0d4542021-01-13 17:10:47 -0800401 // fallback to legacy code path
402 runDevGcFstab();
Yifan Hong024a1242018-08-10 13:50:46 -0700403}
404
Jin Qiana370c142017-10-17 15:41:45 -0700405int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
406 std::unique_lock<std::mutex> lk(cv_m);
407 if (idle_maint_stat != IdleMaintStats::kStopped) {
408 LOG(DEBUG) << "idle maintenance is already running";
409 if (listener) {
410 android::os::PersistableBundle extras;
411 listener->onFinished(0, extras);
412 }
413 return android::OK;
414 }
415 idle_maint_stat = IdleMaintStats::kRunning;
416 lk.unlock();
417
418 LOG(DEBUG) << "idle maintenance started";
419
Kalesh Singh98062dc2021-02-22 15:10:45 -0500420 auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
421 if (!wl.has_value()) {
422 return android::UNEXPECTED_NULL;
423 }
Jin Qiana370c142017-10-17 15:41:45 -0700424
425 std::list<std::string> paths;
426 addFromFstab(&paths, PathTypes::kBlkDevice);
427 addFromVolumeManager(&paths, PathTypes::kBlkDevice);
428
429 startGc(paths);
430
431 bool gc_aborted = waitForGc(paths);
432
433 stopGc(paths);
434
435 lk.lock();
436 idle_maint_stat = IdleMaintStats::kStopped;
437 lk.unlock();
438
439 cv_stop.notify_one();
440
441 if (!gc_aborted) {
442 Trim(nullptr);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800443 runDevGc();
Jin Qiana370c142017-10-17 15:41:45 -0700444 }
445
446 if (listener) {
447 android::os::PersistableBundle extras;
448 listener->onFinished(0, extras);
449 }
450
451 LOG(DEBUG) << "idle maintenance completed";
452
Jin Qiana370c142017-10-17 15:41:45 -0700453 return android::OK;
454}
455
456int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
Kalesh Singh98062dc2021-02-22 15:10:45 -0500457 auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
458 if (!wl.has_value()) {
459 return android::UNEXPECTED_NULL;
460 }
Jin Qiana370c142017-10-17 15:41:45 -0700461
462 std::unique_lock<std::mutex> lk(cv_m);
463 if (idle_maint_stat != IdleMaintStats::kStopped) {
464 idle_maint_stat = IdleMaintStats::kAbort;
465 lk.unlock();
466 cv_abort.notify_one();
467 lk.lock();
468 LOG(DEBUG) << "aborting idle maintenance";
Paul Crowley14c8c072018-09-18 13:30:21 -0700469 cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; });
Jin Qiana370c142017-10-17 15:41:45 -0700470 }
471 lk.unlock();
472
473 if (listener) {
474 android::os::PersistableBundle extras;
475 listener->onFinished(0, extras);
476 }
477
Jin Qiana370c142017-10-17 15:41:45 -0700478 LOG(DEBUG) << "idle maintenance stopped";
479
480 return android::OK;
481}
482
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700483} // namespace vold
484} // namespace android