blob: 466cf19eca50951b0b8b844e97cad9d483925bd6 [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
27#include <android-base/chrono_utils.h>
28#include <android-base/file.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080029#include <android-base/logging.h>
Yifan Hong024a1242018-08-10 13:50:46 -070030#include <android-base/stringprintf.h>
31#include <android-base/strings.h>
Yifan Hong7a37c932018-09-19 10:28:16 -070032#include <android/hardware/health/storage/1.0/IStorage.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070033#include <fs_mgr.h>
Yifan Hong024a1242018-08-10 13:50:46 -070034#include <private/android_filesystem_config.h>
Tri Vo15bbe222019-06-21 12:21:48 -070035#include <wakelock/wakelock.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070036
37#include <dirent.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070038#include <fcntl.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070039#include <sys/mount.h>
40#include <sys/stat.h>
41#include <sys/types.h>
42#include <sys/wait.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070043
Jin Qiana370c142017-10-17 15:41:45 -070044using android::base::Basename;
45using android::base::ReadFileToString;
46using android::base::Realpath;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070047using android::base::StringPrintf;
Jin Qiana370c142017-10-17 15:41:45 -070048using android::base::Timer;
49using android::base::WriteStringToFile;
Yifan Hong024a1242018-08-10 13:50:46 -070050using android::hardware::Return;
51using android::hardware::Void;
Yifan Hong8f0d4542021-01-13 17:10:47 -080052using HStorage = android::hardware::health::storage::V1_0::IStorage;
53using HGarbageCollectCallback = android::hardware::health::storage::V1_0::IGarbageCollectCallback;
54using HResult = android::hardware::health::storage::V1_0::Result;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070055
56namespace android {
57namespace vold {
58
Jin Qiana370c142017-10-17 15:41:45 -070059enum class PathTypes {
60 kMountPoint = 1,
61 kBlkDevice,
62};
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070063
Jin Qiana370c142017-10-17 15:41:45 -070064enum class IdleMaintStats {
65 kStopped = 1,
66 kRunning,
67 kAbort,
68};
69
70static const char* kWakeLock = "IdleMaint";
71static const int DIRTY_SEGMENTS_THRESHOLD = 100;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -080072/*
73 * Timing policy:
74 * 1. F2FS_GC = 7 mins
75 * 2. Trim = 1 min
76 * 3. Dev GC = 2 mins
77 */
78static const int GC_TIMEOUT_SEC = 420;
79static const int DEVGC_TIMEOUT_SEC = 120;
Jin Qiana370c142017-10-17 15:41:45 -070080
81static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
82static std::condition_variable cv_abort, cv_stop;
83static std::mutex cv_m;
84
Paul Crowley14c8c072018-09-18 13:30:21 -070085static void addFromVolumeManager(std::list<std::string>* paths, PathTypes path_type) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070086 VolumeManager* vm = VolumeManager::Instance();
87 std::list<std::string> privateIds;
88 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070089 for (const auto& id : privateIds) {
Jin Qiana370c142017-10-17 15:41:45 -070090 PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070091 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
Jin Qiana370c142017-10-17 15:41:45 -070092 if (path_type == PathTypes::kMountPoint) {
93 paths->push_back(vol->getPath());
94 } else if (path_type == PathTypes::kBlkDevice) {
95 std::string gc_path;
96 const std::string& fs_type = vol->getFsType();
Jaegeuk Kim31e962f2018-07-29 06:56:57 -070097 if (fs_type == "f2fs" && (Realpath(vol->getRawDmDevPath(), &gc_path) ||
98 Realpath(vol->getRawDevPath(), &gc_path))) {
Paul Crowley14c8c072018-09-18 13:30:21 -070099 paths->push_back(std::string("/sys/fs/") + fs_type + "/" + Basename(gc_path));
Jin Qiana370c142017-10-17 15:41:45 -0700100 }
101 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700102 }
103 }
104}
105
Jin Qiana370c142017-10-17 15:41:45 -0700106static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800107 std::string previous_mount_point;
Eric Biggers019d5162020-10-15 16:54:38 -0700108 for (const auto& entry : fstab_default) {
Eric Biggers9a3dc8c2020-10-15 16:54:38 -0700109 // Skip raw partitions and swap space.
110 if (entry.fs_type == "emmc" || entry.fs_type == "mtd" || entry.fs_type == "swap") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700111 continue;
112 }
Eric Biggers9a3dc8c2020-10-15 16:54:38 -0700113 // Skip read-only filesystems and bind mounts.
114 if (entry.flags & (MS_RDONLY | MS_BIND)) {
115 continue;
116 }
117 // Skip anything without an underlying block device, e.g. virtiofs.
118 if (entry.blk_device[0] != '/') {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700119 continue;
120 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800121 if (entry.fs_mgr_flags.vold_managed) {
122 continue; // Should we trim fat32 filesystems?
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700123 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800124 if (entry.fs_mgr_flags.no_trim) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700125 continue;
126 }
127
Tom Cherry4c5bde22019-01-29 14:34:01 -0800128 // Skip the multi-type partitions, which are required to be following each other.
129 // See fs_mgr.c's mount_with_alternatives().
130 if (entry.mount_point == previous_mount_point) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700131 continue;
132 }
133
Jin Qiana370c142017-10-17 15:41:45 -0700134 if (path_type == PathTypes::kMountPoint) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800135 paths->push_back(entry.mount_point);
Jin Qiana370c142017-10-17 15:41:45 -0700136 } else if (path_type == PathTypes::kBlkDevice) {
137 std::string gc_path;
Tom Cherry4c5bde22019-01-29 14:34:01 -0800138 if (entry.fs_type == "f2fs" &&
139 Realpath(android::vold::BlockDeviceForPath(entry.mount_point + "/"), &gc_path)) {
140 paths->push_back("/sys/fs/" + entry.fs_type + "/" + Basename(gc_path));
Jin Qiana370c142017-10-17 15:41:45 -0700141 }
142 }
143
Tom Cherry4c5bde22019-01-29 14:34:01 -0800144 previous_mount_point = entry.mount_point;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700145 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700146}
147
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600148void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
Tri Vo15bbe222019-06-21 12:21:48 -0700149 android::wakelock::WakeLock wl{kWakeLock};
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700150
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600151 // Collect both fstab and vold volumes
152 std::list<std::string> paths;
Jin Qiana370c142017-10-17 15:41:45 -0700153 addFromFstab(&paths, PathTypes::kMountPoint);
154 addFromVolumeManager(&paths, PathTypes::kMountPoint);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600155
156 for (const auto& path : paths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700157 LOG(DEBUG) << "Starting trim of " << path;
158
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600159 android::os::PersistableBundle extras;
160 extras.putString(String16("path"), String16(path.c_str()));
161
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700162 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
163 if (fd < 0) {
164 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600165 if (listener) {
166 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600167 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700168 continue;
169 }
170
171 struct fstrim_range range;
172 memset(&range, 0, sizeof(range));
173 range.len = ULLONG_MAX;
174
175 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600176 if (ioctl(fd, FITRIM, &range)) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700177 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600178 if (listener) {
179 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600180 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700181 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600182 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Paul Crowley14c8c072018-09-18 13:30:21 -0700183 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in "
184 << nanoseconds_to_milliseconds(time) << "ms";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600185 extras.putLong(String16("bytes"), range.len);
186 extras.putLong(String16("time"), time);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600187 if (listener) {
188 listener->onStatus(0, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600189 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700190 }
191 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600192 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700193
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600194 if (listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600195 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600196 listener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700197 }
198
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700199}
200
Jin Qiana370c142017-10-17 15:41:45 -0700201static bool waitForGc(const std::list<std::string>& paths) {
202 std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
203 bool stop = false, aborted = false;
204 Timer timer;
205
206 while (!stop && !aborted) {
207 stop = true;
208 for (const auto& path : paths) {
209 std::string dirty_segments;
210 if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
211 PLOG(WARNING) << "Reading dirty_segments failed in " << path;
212 continue;
213 }
214 if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
215 stop = false;
216 break;
217 }
218 }
219
220 if (stop) break;
221
222 if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
223 LOG(WARNING) << "GC timeout";
224 break;
225 }
226
227 lk.lock();
Paul Crowley14c8c072018-09-18 13:30:21 -0700228 aborted =
229 cv_abort.wait_for(lk, 10s, [] { return idle_maint_stat == IdleMaintStats::kAbort; });
Jin Qiana370c142017-10-17 15:41:45 -0700230 lk.unlock();
231 }
232
233 return aborted;
234}
235
236static int startGc(const std::list<std::string>& paths) {
237 for (const auto& path : paths) {
238 LOG(DEBUG) << "Start GC on " << path;
239 if (!WriteStringToFile("1", path + "/gc_urgent")) {
240 PLOG(WARNING) << "Start GC failed on " << path;
241 }
242 }
243 return android::OK;
244}
245
246static int stopGc(const std::list<std::string>& paths) {
247 for (const auto& path : paths) {
248 LOG(DEBUG) << "Stop GC on " << path;
249 if (!WriteStringToFile("0", path + "/gc_urgent")) {
250 PLOG(WARNING) << "Stop GC failed on " << path;
251 }
252 }
253 return android::OK;
254}
255
Yifan Hong024a1242018-08-10 13:50:46 -0700256static void runDevGcFstab(void) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800257 std::string path;
Eric Biggers019d5162020-10-15 16:54:38 -0700258 for (const auto& entry : fstab_default) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800259 if (!entry.sysfs_path.empty()) {
260 path = entry.sysfs_path;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800261 break;
262 }
263 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800264
265 if (path.empty()) {
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800266 return;
267 }
268
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800269 path = path + "/manual_gc";
270 Timer timer;
271
272 LOG(DEBUG) << "Start Dev GC on " << path;
273 while (1) {
274 std::string require;
275 if (!ReadFileToString(path, &require)) {
276 PLOG(WARNING) << "Reading manual_gc failed in " << path;
277 break;
278 }
Yifan Hong024a1242018-08-10 13:50:46 -0700279 require = android::base::Trim(require);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800280 if (require == "" || require == "off" || require == "disabled") {
281 LOG(DEBUG) << "No more to do Dev GC";
282 break;
283 }
284
285 LOG(DEBUG) << "Trigger Dev GC on " << path;
286 if (!WriteStringToFile("1", path)) {
287 PLOG(WARNING) << "Start Dev GC failed on " << path;
288 break;
289 }
290
291 if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
292 LOG(WARNING) << "Dev GC timeout";
293 break;
294 }
295 sleep(2);
296 }
297 LOG(DEBUG) << "Stop Dev GC on " << path;
298 if (!WriteStringToFile("0", path)) {
299 PLOG(WARNING) << "Stop Dev GC failed on " << path;
300 }
301 return;
302}
303
Yifan Hong8f0d4542021-01-13 17:10:47 -0800304enum class IDL { HIDL };
305std::ostream& operator<<(std::ostream& os, IDL idl) {
306 return os << "HIDL";
307}
308
309template <IDL idl, typename Result>
310class GcCallbackImpl {
311 protected:
312 void onFinishInternal(Result result) {
Yifan Hong024a1242018-08-10 13:50:46 -0700313 std::unique_lock<std::mutex> lock(mMutex);
314 mFinished = true;
315 mResult = result;
316 lock.unlock();
317 mCv.notify_all();
Yifan Hong024a1242018-08-10 13:50:46 -0700318 }
Yifan Hong8f0d4542021-01-13 17:10:47 -0800319
320 public:
Yifan Hong024a1242018-08-10 13:50:46 -0700321 void wait(uint64_t seconds) {
322 std::unique_lock<std::mutex> lock(mMutex);
323 mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; });
324
325 if (!mFinished) {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800326 LOG(WARNING) << "Dev GC on " << idl << " HAL timeout";
Yifan Hong024a1242018-08-10 13:50:46 -0700327 } else if (mResult != Result::SUCCESS) {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800328 LOG(WARNING) << "Dev GC on " << idl << " HAL failed with " << toString(mResult);
Yifan Hong024a1242018-08-10 13:50:46 -0700329 } else {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800330 LOG(INFO) << "Dev GC on " << idl << " HAL successful";
Yifan Hong024a1242018-08-10 13:50:46 -0700331 }
332 }
333
334 private:
335 std::mutex mMutex;
336 std::condition_variable mCv;
337 bool mFinished{false};
338 Result mResult{Result::UNKNOWN_ERROR};
339};
340
Yifan Hong8f0d4542021-01-13 17:10:47 -0800341class HGcCallbackImpl : public HGarbageCollectCallback, public GcCallbackImpl<IDL::HIDL, HResult> {
342 Return<void> onFinish(HResult result) override {
343 onFinishInternal(result);
344 return Void();
345 }
346};
347
348template <IDL idl, typename Service, typename GcCallbackImpl, typename GetDescription>
349static void runDevGcOnHal(Service service, GcCallbackImpl cb, GetDescription get_description) {
350 LOG(DEBUG) << "Start Dev GC on " << idl << " HAL";
Yifan Hong024a1242018-08-10 13:50:46 -0700351 auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb);
352 if (!ret.isOk()) {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800353 LOG(WARNING) << "Cannot start Dev GC on " << idl
354 << " HAL: " << std::invoke(get_description, ret);
Yifan Hong024a1242018-08-10 13:50:46 -0700355 return;
356 }
357 cb->wait(DEVGC_TIMEOUT_SEC);
358}
359
360static void runDevGc(void) {
Yifan Hong8f0d4542021-01-13 17:10:47 -0800361 auto hidl_service = HStorage::getService();
362 if (hidl_service != nullptr) {
363 runDevGcOnHal<IDL::HIDL>(hidl_service, sp<HGcCallbackImpl>(new HGcCallbackImpl()),
364 &Return<void>::description);
365 return;
Yifan Hong024a1242018-08-10 13:50:46 -0700366 }
Yifan Hong8f0d4542021-01-13 17:10:47 -0800367 // fallback to legacy code path
368 runDevGcFstab();
Yifan Hong024a1242018-08-10 13:50:46 -0700369}
370
Jin Qiana370c142017-10-17 15:41:45 -0700371int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
372 std::unique_lock<std::mutex> lk(cv_m);
373 if (idle_maint_stat != IdleMaintStats::kStopped) {
374 LOG(DEBUG) << "idle maintenance is already running";
375 if (listener) {
376 android::os::PersistableBundle extras;
377 listener->onFinished(0, extras);
378 }
379 return android::OK;
380 }
381 idle_maint_stat = IdleMaintStats::kRunning;
382 lk.unlock();
383
384 LOG(DEBUG) << "idle maintenance started";
385
Tri Vo15bbe222019-06-21 12:21:48 -0700386 android::wakelock::WakeLock wl{kWakeLock};
Jin Qiana370c142017-10-17 15:41:45 -0700387
388 std::list<std::string> paths;
389 addFromFstab(&paths, PathTypes::kBlkDevice);
390 addFromVolumeManager(&paths, PathTypes::kBlkDevice);
391
392 startGc(paths);
393
394 bool gc_aborted = waitForGc(paths);
395
396 stopGc(paths);
397
398 lk.lock();
399 idle_maint_stat = IdleMaintStats::kStopped;
400 lk.unlock();
401
402 cv_stop.notify_one();
403
404 if (!gc_aborted) {
405 Trim(nullptr);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800406 runDevGc();
Jin Qiana370c142017-10-17 15:41:45 -0700407 }
408
409 if (listener) {
410 android::os::PersistableBundle extras;
411 listener->onFinished(0, extras);
412 }
413
414 LOG(DEBUG) << "idle maintenance completed";
415
Jin Qiana370c142017-10-17 15:41:45 -0700416 return android::OK;
417}
418
419int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
Tri Vo15bbe222019-06-21 12:21:48 -0700420 android::wakelock::WakeLock wl{kWakeLock};
Jin Qiana370c142017-10-17 15:41:45 -0700421
422 std::unique_lock<std::mutex> lk(cv_m);
423 if (idle_maint_stat != IdleMaintStats::kStopped) {
424 idle_maint_stat = IdleMaintStats::kAbort;
425 lk.unlock();
426 cv_abort.notify_one();
427 lk.lock();
428 LOG(DEBUG) << "aborting idle maintenance";
Paul Crowley14c8c072018-09-18 13:30:21 -0700429 cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; });
Jin Qiana370c142017-10-17 15:41:45 -0700430 }
431 lk.unlock();
432
433 if (listener) {
434 android::os::PersistableBundle extras;
435 listener->onFinished(0, extras);
436 }
437
Jin Qiana370c142017-10-17 15:41:45 -0700438 LOG(DEBUG) << "idle maintenance stopped";
439
440 return android::OK;
441}
442
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700443} // namespace vold
444} // namespace android