blob: 3ccb806689bde3ee046b10d548de6d0ac62bbd14 [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 Kim1251ef02018-07-29 06:56:57 -070018#include "FileDeviceUtils.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070019#include "Utils.h"
20#include "VolumeManager.h"
Jin Qiana370c142017-10-17 15:41:45 -070021#include "model/PrivateVolume.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070022
Jin Qiana370c142017-10-17 15:41:45 -070023#include <thread>
24
25#include <android-base/chrono_utils.h>
26#include <android-base/file.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080027#include <android-base/logging.h>
Yifan Hong024a1242018-08-10 13:50:46 -070028#include <android-base/stringprintf.h>
29#include <android-base/strings.h>
Yifan Hong91a68df2018-09-19 10:28:16 -070030#include <android/hardware/health/storage/1.0/IStorage.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070031#include <fs_mgr.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070032#include <hardware_legacy/power.h>
Yifan Hong024a1242018-08-10 13:50:46 -070033#include <private/android_filesystem_config.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070034
35#include <dirent.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070036#include <fcntl.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070037#include <sys/mount.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40#include <sys/wait.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070041
Jin Qiana370c142017-10-17 15:41:45 -070042using android::base::Basename;
43using android::base::ReadFileToString;
44using android::base::Realpath;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070045using android::base::StringPrintf;
Jin Qiana370c142017-10-17 15:41:45 -070046using android::base::Timer;
47using android::base::WriteStringToFile;
Tom Cherry4c5bde22019-01-29 14:34:01 -080048using android::fs_mgr::Fstab;
49using android::fs_mgr::ReadDefaultFstab;
Yifan Hong024a1242018-08-10 13:50:46 -070050using android::hardware::Return;
51using android::hardware::Void;
Yifan Hong91a68df2018-09-19 10:28:16 -070052using android::hardware::health::storage::V1_0::IGarbageCollectCallback;
53using android::hardware::health::storage::V1_0::IStorage;
54using 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 Kim1251ef02018-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 Fstab fstab;
108 ReadDefaultFstab(&fstab);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700109
Tom Cherry4c5bde22019-01-29 14:34:01 -0800110 std::string previous_mount_point;
111 for (const auto& entry : fstab) {
112 // Skip raw partitions.
113 if (entry.fs_type == "emmc" || entry.fs_type == "mtd") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700114 continue;
115 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800116 // Skip read-only filesystems
117 if (entry.flags & MS_RDONLY) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700118 continue;
119 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800120 if (entry.fs_mgr_flags.vold_managed) {
121 continue; // Should we trim fat32 filesystems?
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700122 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800123 if (entry.fs_mgr_flags.no_trim) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700124 continue;
125 }
126
Tom Cherry4c5bde22019-01-29 14:34:01 -0800127 // Skip the multi-type partitions, which are required to be following each other.
128 // See fs_mgr.c's mount_with_alternatives().
129 if (entry.mount_point == previous_mount_point) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700130 continue;
131 }
132
Jin Qiana370c142017-10-17 15:41:45 -0700133 if (path_type == PathTypes::kMountPoint) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800134 paths->push_back(entry.mount_point);
Jin Qiana370c142017-10-17 15:41:45 -0700135 } else if (path_type == PathTypes::kBlkDevice) {
136 std::string gc_path;
Tom Cherry4c5bde22019-01-29 14:34:01 -0800137 if (entry.fs_type == "f2fs" &&
138 Realpath(android::vold::BlockDeviceForPath(entry.mount_point + "/"), &gc_path)) {
139 paths->push_back("/sys/fs/" + entry.fs_type + "/" + Basename(gc_path));
Jin Qiana370c142017-10-17 15:41:45 -0700140 }
141 }
142
Tom Cherry4c5bde22019-01-29 14:34:01 -0800143 previous_mount_point = entry.mount_point;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700144 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700145}
146
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600147void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700148 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
149
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600150 // Collect both fstab and vold volumes
151 std::list<std::string> paths;
Jin Qiana370c142017-10-17 15:41:45 -0700152 addFromFstab(&paths, PathTypes::kMountPoint);
153 addFromVolumeManager(&paths, PathTypes::kMountPoint);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600154
155 for (const auto& path : paths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700156 LOG(DEBUG) << "Starting trim of " << path;
157
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600158 android::os::PersistableBundle extras;
159 extras.putString(String16("path"), String16(path.c_str()));
160
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700161 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
162 if (fd < 0) {
163 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600164 if (listener) {
165 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600166 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700167 continue;
168 }
169
170 struct fstrim_range range;
171 memset(&range, 0, sizeof(range));
172 range.len = ULLONG_MAX;
173
174 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600175 if (ioctl(fd, FITRIM, &range)) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700176 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600177 if (listener) {
178 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600179 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700180 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600181 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Paul Crowley14c8c072018-09-18 13:30:21 -0700182 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in "
183 << nanoseconds_to_milliseconds(time) << "ms";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600184 extras.putLong(String16("bytes"), range.len);
185 extras.putLong(String16("time"), time);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600186 if (listener) {
187 listener->onStatus(0, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600188 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700189 }
190 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600191 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700192
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600193 if (listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600194 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600195 listener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700196 }
197
198 release_wake_lock(kWakeLock);
199}
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;
Jaegeuk Kima6aae2f2018-02-17 06:02:30 -0800239 if (!WriteStringToFile("1", path + "/discard_granularity")) {
240 PLOG(WARNING) << "Set discard gralunarity failed on" << path;
241 }
Jin Qiana370c142017-10-17 15:41:45 -0700242 if (!WriteStringToFile("1", path + "/gc_urgent")) {
243 PLOG(WARNING) << "Start GC failed on " << path;
244 }
245 }
246 return android::OK;
247}
248
249static int stopGc(const std::list<std::string>& paths) {
250 for (const auto& path : paths) {
251 LOG(DEBUG) << "Stop GC on " << path;
252 if (!WriteStringToFile("0", path + "/gc_urgent")) {
253 PLOG(WARNING) << "Stop GC failed on " << path;
254 }
Jaegeuk Kima6aae2f2018-02-17 06:02:30 -0800255 if (!WriteStringToFile("16", path + "/discard_granularity")) {
256 PLOG(WARNING) << "Set discard gralunarity failed on" << path;
257 }
Jin Qiana370c142017-10-17 15:41:45 -0700258 }
259 return android::OK;
260}
261
Yifan Hong024a1242018-08-10 13:50:46 -0700262static void runDevGcFstab(void) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800263 Fstab fstab;
264 ReadDefaultFstab(&fstab);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800265
Tom Cherry4c5bde22019-01-29 14:34:01 -0800266 std::string path;
267 for (const auto& entry : fstab) {
268 if (!entry.sysfs_path.empty()) {
269 path = entry.sysfs_path;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800270 break;
271 }
272 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800273
274 if (path.empty()) {
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800275 return;
276 }
277
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800278 path = path + "/manual_gc";
279 Timer timer;
280
281 LOG(DEBUG) << "Start Dev GC on " << path;
282 while (1) {
283 std::string require;
284 if (!ReadFileToString(path, &require)) {
285 PLOG(WARNING) << "Reading manual_gc failed in " << path;
286 break;
287 }
Yifan Hong024a1242018-08-10 13:50:46 -0700288 require = android::base::Trim(require);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800289 if (require == "" || require == "off" || require == "disabled") {
290 LOG(DEBUG) << "No more to do Dev GC";
291 break;
292 }
293
294 LOG(DEBUG) << "Trigger Dev GC on " << path;
295 if (!WriteStringToFile("1", path)) {
296 PLOG(WARNING) << "Start Dev GC failed on " << path;
297 break;
298 }
299
300 if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
301 LOG(WARNING) << "Dev GC timeout";
302 break;
303 }
304 sleep(2);
305 }
306 LOG(DEBUG) << "Stop Dev GC on " << path;
307 if (!WriteStringToFile("0", path)) {
308 PLOG(WARNING) << "Stop Dev GC failed on " << path;
309 }
310 return;
311}
312
Yifan Hong024a1242018-08-10 13:50:46 -0700313class GcCallback : public IGarbageCollectCallback {
314 public:
315 Return<void> onFinish(Result result) override {
316 std::unique_lock<std::mutex> lock(mMutex);
317 mFinished = true;
318 mResult = result;
319 lock.unlock();
320 mCv.notify_all();
321 return Void();
322 }
323 void wait(uint64_t seconds) {
324 std::unique_lock<std::mutex> lock(mMutex);
325 mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; });
326
327 if (!mFinished) {
328 LOG(WARNING) << "Dev GC on HAL timeout";
329 } else if (mResult != Result::SUCCESS) {
330 LOG(WARNING) << "Dev GC on HAL failed with " << toString(mResult);
331 } else {
332 LOG(INFO) << "Dev GC on HAL successful";
333 }
334 }
335
336 private:
337 std::mutex mMutex;
338 std::condition_variable mCv;
339 bool mFinished{false};
340 Result mResult{Result::UNKNOWN_ERROR};
341};
342
Yifan Hong91a68df2018-09-19 10:28:16 -0700343static void runDevGcOnHal(sp<IStorage> service) {
Yifan Hong024a1242018-08-10 13:50:46 -0700344 LOG(DEBUG) << "Start Dev GC on HAL";
345 sp<GcCallback> cb = new GcCallback();
346 auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb);
347 if (!ret.isOk()) {
348 LOG(WARNING) << "Cannot start Dev GC on HAL: " << ret.description();
349 return;
350 }
351 cb->wait(DEVGC_TIMEOUT_SEC);
352}
353
354static void runDevGc(void) {
Yifan Hong91a68df2018-09-19 10:28:16 -0700355 auto service = IStorage::getService();
Yifan Hong024a1242018-08-10 13:50:46 -0700356 if (service != nullptr) {
357 runDevGcOnHal(service);
358 } else {
359 // fallback to legacy code path
360 runDevGcFstab();
361 }
362}
363
Jin Qiana370c142017-10-17 15:41:45 -0700364int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
365 std::unique_lock<std::mutex> lk(cv_m);
366 if (idle_maint_stat != IdleMaintStats::kStopped) {
367 LOG(DEBUG) << "idle maintenance is already running";
368 if (listener) {
369 android::os::PersistableBundle extras;
370 listener->onFinished(0, extras);
371 }
372 return android::OK;
373 }
374 idle_maint_stat = IdleMaintStats::kRunning;
375 lk.unlock();
376
377 LOG(DEBUG) << "idle maintenance started";
378
379 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
380
381 std::list<std::string> paths;
382 addFromFstab(&paths, PathTypes::kBlkDevice);
383 addFromVolumeManager(&paths, PathTypes::kBlkDevice);
384
385 startGc(paths);
386
387 bool gc_aborted = waitForGc(paths);
388
389 stopGc(paths);
390
391 lk.lock();
392 idle_maint_stat = IdleMaintStats::kStopped;
393 lk.unlock();
394
395 cv_stop.notify_one();
396
397 if (!gc_aborted) {
398 Trim(nullptr);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800399 runDevGc();
Jin Qiana370c142017-10-17 15:41:45 -0700400 }
401
402 if (listener) {
403 android::os::PersistableBundle extras;
404 listener->onFinished(0, extras);
405 }
406
407 LOG(DEBUG) << "idle maintenance completed";
408
409 release_wake_lock(kWakeLock);
410
411 return android::OK;
412}
413
414int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
415 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
416
417 std::unique_lock<std::mutex> lk(cv_m);
418 if (idle_maint_stat != IdleMaintStats::kStopped) {
419 idle_maint_stat = IdleMaintStats::kAbort;
420 lk.unlock();
421 cv_abort.notify_one();
422 lk.lock();
423 LOG(DEBUG) << "aborting idle maintenance";
Paul Crowley14c8c072018-09-18 13:30:21 -0700424 cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; });
Jin Qiana370c142017-10-17 15:41:45 -0700425 }
426 lk.unlock();
427
428 if (listener) {
429 android::os::PersistableBundle extras;
430 listener->onFinished(0, extras);
431 }
432
433 release_wake_lock(kWakeLock);
434
435 LOG(DEBUG) << "idle maintenance stopped";
436
437 return android::OK;
438}
439
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700440} // namespace vold
441} // namespace android