blob: e4a18063adb88cd4aa5b85092c1259375181aee3 [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>
25
26#include <android-base/chrono_utils.h>
27#include <android-base/file.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080028#include <android-base/logging.h>
Yifan Hong024a1242018-08-10 13:50:46 -070029#include <android-base/stringprintf.h>
30#include <android-base/strings.h>
Yifan Hong7a37c932018-09-19 10:28:16 -070031#include <android/hardware/health/storage/1.0/IStorage.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070032#include <fs_mgr.h>
Yifan Hong024a1242018-08-10 13:50:46 -070033#include <private/android_filesystem_config.h>
Tri Vo15bbe222019-06-21 12:21:48 -070034#include <wakelock/wakelock.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070035
36#include <dirent.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070037#include <fcntl.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070038#include <sys/mount.h>
39#include <sys/stat.h>
40#include <sys/types.h>
41#include <sys/wait.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070042
Jin Qiana370c142017-10-17 15:41:45 -070043using android::base::Basename;
44using android::base::ReadFileToString;
45using android::base::Realpath;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070046using android::base::StringPrintf;
Jin Qiana370c142017-10-17 15:41:45 -070047using android::base::Timer;
48using android::base::WriteStringToFile;
Yifan Hong024a1242018-08-10 13:50:46 -070049using android::hardware::Return;
50using android::hardware::Void;
Yifan Hong7a37c932018-09-19 10:28:16 -070051using android::hardware::health::storage::V1_0::IStorage;
52using android::hardware::health::storage::V1_0::IGarbageCollectCallback;
53using android::hardware::health::storage::V1_0::Result;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070054
55namespace android {
56namespace vold {
57
Jin Qiana370c142017-10-17 15:41:45 -070058enum class PathTypes {
59 kMountPoint = 1,
60 kBlkDevice,
61};
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070062
Jin Qiana370c142017-10-17 15:41:45 -070063enum class IdleMaintStats {
64 kStopped = 1,
65 kRunning,
66 kAbort,
67};
68
69static const char* kWakeLock = "IdleMaint";
70static const int DIRTY_SEGMENTS_THRESHOLD = 100;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -080071/*
72 * Timing policy:
73 * 1. F2FS_GC = 7 mins
74 * 2. Trim = 1 min
75 * 3. Dev GC = 2 mins
76 */
77static const int GC_TIMEOUT_SEC = 420;
78static const int DEVGC_TIMEOUT_SEC = 120;
Jin Qiana370c142017-10-17 15:41:45 -070079
80static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
81static std::condition_variable cv_abort, cv_stop;
82static std::mutex cv_m;
83
Paul Crowley14c8c072018-09-18 13:30:21 -070084static void addFromVolumeManager(std::list<std::string>* paths, PathTypes path_type) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070085 VolumeManager* vm = VolumeManager::Instance();
86 std::list<std::string> privateIds;
87 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070088 for (const auto& id : privateIds) {
Jin Qiana370c142017-10-17 15:41:45 -070089 PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070090 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
Jin Qiana370c142017-10-17 15:41:45 -070091 if (path_type == PathTypes::kMountPoint) {
92 paths->push_back(vol->getPath());
93 } else if (path_type == PathTypes::kBlkDevice) {
94 std::string gc_path;
95 const std::string& fs_type = vol->getFsType();
Jaegeuk Kim31e962f2018-07-29 06:56:57 -070096 if (fs_type == "f2fs" && (Realpath(vol->getRawDmDevPath(), &gc_path) ||
97 Realpath(vol->getRawDevPath(), &gc_path))) {
Paul Crowley14c8c072018-09-18 13:30:21 -070098 paths->push_back(std::string("/sys/fs/") + fs_type + "/" + Basename(gc_path));
Jin Qiana370c142017-10-17 15:41:45 -070099 }
100 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700101 }
102 }
103}
104
Jin Qiana370c142017-10-17 15:41:45 -0700105static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800106 std::string previous_mount_point;
Eric Biggers019d5162020-10-15 16:54:38 -0700107 for (const auto& entry : fstab_default) {
Eric Biggers9a3dc8c2020-10-15 16:54:38 -0700108 // Skip raw partitions and swap space.
109 if (entry.fs_type == "emmc" || entry.fs_type == "mtd" || entry.fs_type == "swap") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700110 continue;
111 }
Eric Biggers9a3dc8c2020-10-15 16:54:38 -0700112 // Skip read-only filesystems and bind mounts.
113 if (entry.flags & (MS_RDONLY | MS_BIND)) {
114 continue;
115 }
116 // Skip anything without an underlying block device, e.g. virtiofs.
117 if (entry.blk_device[0] != '/') {
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) {
Tri Vo15bbe222019-06-21 12:21:48 -0700148 android::wakelock::WakeLock wl{kWakeLock};
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700149
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
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700198}
199
Jin Qiana370c142017-10-17 15:41:45 -0700200static bool waitForGc(const std::list<std::string>& paths) {
201 std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
202 bool stop = false, aborted = false;
203 Timer timer;
204
205 while (!stop && !aborted) {
206 stop = true;
207 for (const auto& path : paths) {
208 std::string dirty_segments;
209 if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
210 PLOG(WARNING) << "Reading dirty_segments failed in " << path;
211 continue;
212 }
213 if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
214 stop = false;
215 break;
216 }
217 }
218
219 if (stop) break;
220
221 if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
222 LOG(WARNING) << "GC timeout";
223 break;
224 }
225
226 lk.lock();
Paul Crowley14c8c072018-09-18 13:30:21 -0700227 aborted =
228 cv_abort.wait_for(lk, 10s, [] { return idle_maint_stat == IdleMaintStats::kAbort; });
Jin Qiana370c142017-10-17 15:41:45 -0700229 lk.unlock();
230 }
231
232 return aborted;
233}
234
235static int startGc(const std::list<std::string>& paths) {
236 for (const auto& path : paths) {
237 LOG(DEBUG) << "Start GC on " << path;
238 if (!WriteStringToFile("1", path + "/gc_urgent")) {
239 PLOG(WARNING) << "Start GC failed on " << path;
240 }
241 }
242 return android::OK;
243}
244
245static int stopGc(const std::list<std::string>& paths) {
246 for (const auto& path : paths) {
247 LOG(DEBUG) << "Stop GC on " << path;
248 if (!WriteStringToFile("0", path + "/gc_urgent")) {
249 PLOG(WARNING) << "Stop GC failed on " << path;
250 }
251 }
252 return android::OK;
253}
254
Yifan Hong024a1242018-08-10 13:50:46 -0700255static void runDevGcFstab(void) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800256 std::string path;
Eric Biggers019d5162020-10-15 16:54:38 -0700257 for (const auto& entry : fstab_default) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800258 if (!entry.sysfs_path.empty()) {
259 path = entry.sysfs_path;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800260 break;
261 }
262 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800263
264 if (path.empty()) {
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800265 return;
266 }
267
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800268 path = path + "/manual_gc";
269 Timer timer;
270
271 LOG(DEBUG) << "Start Dev GC on " << path;
272 while (1) {
273 std::string require;
274 if (!ReadFileToString(path, &require)) {
275 PLOG(WARNING) << "Reading manual_gc failed in " << path;
276 break;
277 }
Yifan Hong024a1242018-08-10 13:50:46 -0700278 require = android::base::Trim(require);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800279 if (require == "" || require == "off" || require == "disabled") {
280 LOG(DEBUG) << "No more to do Dev GC";
281 break;
282 }
283
284 LOG(DEBUG) << "Trigger Dev GC on " << path;
285 if (!WriteStringToFile("1", path)) {
286 PLOG(WARNING) << "Start Dev GC failed on " << path;
287 break;
288 }
289
290 if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
291 LOG(WARNING) << "Dev GC timeout";
292 break;
293 }
294 sleep(2);
295 }
296 LOG(DEBUG) << "Stop Dev GC on " << path;
297 if (!WriteStringToFile("0", path)) {
298 PLOG(WARNING) << "Stop Dev GC failed on " << path;
299 }
300 return;
301}
302
Yifan Hong024a1242018-08-10 13:50:46 -0700303class GcCallback : public IGarbageCollectCallback {
304 public:
305 Return<void> onFinish(Result result) override {
306 std::unique_lock<std::mutex> lock(mMutex);
307 mFinished = true;
308 mResult = result;
309 lock.unlock();
310 mCv.notify_all();
311 return Void();
312 }
313 void wait(uint64_t seconds) {
314 std::unique_lock<std::mutex> lock(mMutex);
315 mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; });
316
317 if (!mFinished) {
318 LOG(WARNING) << "Dev GC on HAL timeout";
319 } else if (mResult != Result::SUCCESS) {
320 LOG(WARNING) << "Dev GC on HAL failed with " << toString(mResult);
321 } else {
322 LOG(INFO) << "Dev GC on HAL successful";
323 }
324 }
325
326 private:
327 std::mutex mMutex;
328 std::condition_variable mCv;
329 bool mFinished{false};
330 Result mResult{Result::UNKNOWN_ERROR};
331};
332
Yifan Hong7a37c932018-09-19 10:28:16 -0700333static void runDevGcOnHal(sp<IStorage> service) {
Yifan Hong024a1242018-08-10 13:50:46 -0700334 LOG(DEBUG) << "Start Dev GC on HAL";
335 sp<GcCallback> cb = new GcCallback();
336 auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb);
337 if (!ret.isOk()) {
338 LOG(WARNING) << "Cannot start Dev GC on HAL: " << ret.description();
339 return;
340 }
341 cb->wait(DEVGC_TIMEOUT_SEC);
342}
343
344static void runDevGc(void) {
Yifan Hong7a37c932018-09-19 10:28:16 -0700345 auto service = IStorage::getService();
Yifan Hong024a1242018-08-10 13:50:46 -0700346 if (service != nullptr) {
347 runDevGcOnHal(service);
348 } else {
349 // fallback to legacy code path
350 runDevGcFstab();
351 }
352}
353
Jin Qiana370c142017-10-17 15:41:45 -0700354int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
355 std::unique_lock<std::mutex> lk(cv_m);
356 if (idle_maint_stat != IdleMaintStats::kStopped) {
357 LOG(DEBUG) << "idle maintenance is already running";
358 if (listener) {
359 android::os::PersistableBundle extras;
360 listener->onFinished(0, extras);
361 }
362 return android::OK;
363 }
364 idle_maint_stat = IdleMaintStats::kRunning;
365 lk.unlock();
366
367 LOG(DEBUG) << "idle maintenance started";
368
Tri Vo15bbe222019-06-21 12:21:48 -0700369 android::wakelock::WakeLock wl{kWakeLock};
Jin Qiana370c142017-10-17 15:41:45 -0700370
371 std::list<std::string> paths;
372 addFromFstab(&paths, PathTypes::kBlkDevice);
373 addFromVolumeManager(&paths, PathTypes::kBlkDevice);
374
375 startGc(paths);
376
377 bool gc_aborted = waitForGc(paths);
378
379 stopGc(paths);
380
381 lk.lock();
382 idle_maint_stat = IdleMaintStats::kStopped;
383 lk.unlock();
384
385 cv_stop.notify_one();
386
387 if (!gc_aborted) {
388 Trim(nullptr);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800389 runDevGc();
Jin Qiana370c142017-10-17 15:41:45 -0700390 }
391
392 if (listener) {
393 android::os::PersistableBundle extras;
394 listener->onFinished(0, extras);
395 }
396
397 LOG(DEBUG) << "idle maintenance completed";
398
Jin Qiana370c142017-10-17 15:41:45 -0700399 return android::OK;
400}
401
402int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
Tri Vo15bbe222019-06-21 12:21:48 -0700403 android::wakelock::WakeLock wl{kWakeLock};
Jin Qiana370c142017-10-17 15:41:45 -0700404
405 std::unique_lock<std::mutex> lk(cv_m);
406 if (idle_maint_stat != IdleMaintStats::kStopped) {
407 idle_maint_stat = IdleMaintStats::kAbort;
408 lk.unlock();
409 cv_abort.notify_one();
410 lk.lock();
411 LOG(DEBUG) << "aborting idle maintenance";
Paul Crowley14c8c072018-09-18 13:30:21 -0700412 cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; });
Jin Qiana370c142017-10-17 15:41:45 -0700413 }
414 lk.unlock();
415
416 if (listener) {
417 android::os::PersistableBundle extras;
418 listener->onFinished(0, extras);
419 }
420
Jin Qiana370c142017-10-17 15:41:45 -0700421 LOG(DEBUG) << "idle maintenance stopped";
422
423 return android::OK;
424}
425
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700426} // namespace vold
427} // namespace android