blob: 24a1ef93a9ba892fcb7322626d3a334b45eb9b50 [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) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800108 // Skip raw partitions.
109 if (entry.fs_type == "emmc" || entry.fs_type == "mtd") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700110 continue;
111 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800112 // Skip read-only filesystems
113 if (entry.flags & MS_RDONLY) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700114 continue;
115 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800116 if (entry.fs_mgr_flags.vold_managed) {
117 continue; // Should we trim fat32 filesystems?
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700118 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800119 if (entry.fs_mgr_flags.no_trim) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700120 continue;
121 }
122
Tom Cherry4c5bde22019-01-29 14:34:01 -0800123 // Skip the multi-type partitions, which are required to be following each other.
124 // See fs_mgr.c's mount_with_alternatives().
125 if (entry.mount_point == previous_mount_point) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700126 continue;
127 }
128
Jin Qiana370c142017-10-17 15:41:45 -0700129 if (path_type == PathTypes::kMountPoint) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800130 paths->push_back(entry.mount_point);
Jin Qiana370c142017-10-17 15:41:45 -0700131 } else if (path_type == PathTypes::kBlkDevice) {
132 std::string gc_path;
Tom Cherry4c5bde22019-01-29 14:34:01 -0800133 if (entry.fs_type == "f2fs" &&
134 Realpath(android::vold::BlockDeviceForPath(entry.mount_point + "/"), &gc_path)) {
135 paths->push_back("/sys/fs/" + entry.fs_type + "/" + Basename(gc_path));
Jin Qiana370c142017-10-17 15:41:45 -0700136 }
137 }
138
Tom Cherry4c5bde22019-01-29 14:34:01 -0800139 previous_mount_point = entry.mount_point;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700140 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700141}
142
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600143void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
Tri Vo15bbe222019-06-21 12:21:48 -0700144 android::wakelock::WakeLock wl{kWakeLock};
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700145
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600146 // Collect both fstab and vold volumes
147 std::list<std::string> paths;
Jin Qiana370c142017-10-17 15:41:45 -0700148 addFromFstab(&paths, PathTypes::kMountPoint);
149 addFromVolumeManager(&paths, PathTypes::kMountPoint);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600150
151 for (const auto& path : paths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700152 LOG(DEBUG) << "Starting trim of " << path;
153
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600154 android::os::PersistableBundle extras;
155 extras.putString(String16("path"), String16(path.c_str()));
156
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700157 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
158 if (fd < 0) {
159 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600160 if (listener) {
161 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600162 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700163 continue;
164 }
165
166 struct fstrim_range range;
167 memset(&range, 0, sizeof(range));
168 range.len = ULLONG_MAX;
169
170 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600171 if (ioctl(fd, FITRIM, &range)) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700172 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600173 if (listener) {
174 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600175 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700176 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600177 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Paul Crowley14c8c072018-09-18 13:30:21 -0700178 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in "
179 << nanoseconds_to_milliseconds(time) << "ms";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600180 extras.putLong(String16("bytes"), range.len);
181 extras.putLong(String16("time"), time);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600182 if (listener) {
183 listener->onStatus(0, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600184 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700185 }
186 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600187 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700188
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600189 if (listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600190 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600191 listener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700192 }
193
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700194}
195
Jin Qiana370c142017-10-17 15:41:45 -0700196static bool waitForGc(const std::list<std::string>& paths) {
197 std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
198 bool stop = false, aborted = false;
199 Timer timer;
200
201 while (!stop && !aborted) {
202 stop = true;
203 for (const auto& path : paths) {
204 std::string dirty_segments;
205 if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
206 PLOG(WARNING) << "Reading dirty_segments failed in " << path;
207 continue;
208 }
209 if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
210 stop = false;
211 break;
212 }
213 }
214
215 if (stop) break;
216
217 if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
218 LOG(WARNING) << "GC timeout";
219 break;
220 }
221
222 lk.lock();
Paul Crowley14c8c072018-09-18 13:30:21 -0700223 aborted =
224 cv_abort.wait_for(lk, 10s, [] { return idle_maint_stat == IdleMaintStats::kAbort; });
Jin Qiana370c142017-10-17 15:41:45 -0700225 lk.unlock();
226 }
227
228 return aborted;
229}
230
231static int startGc(const std::list<std::string>& paths) {
232 for (const auto& path : paths) {
233 LOG(DEBUG) << "Start GC on " << path;
234 if (!WriteStringToFile("1", path + "/gc_urgent")) {
235 PLOG(WARNING) << "Start GC failed on " << path;
236 }
237 }
238 return android::OK;
239}
240
241static int stopGc(const std::list<std::string>& paths) {
242 for (const auto& path : paths) {
243 LOG(DEBUG) << "Stop GC on " << path;
244 if (!WriteStringToFile("0", path + "/gc_urgent")) {
245 PLOG(WARNING) << "Stop GC failed on " << path;
246 }
247 }
248 return android::OK;
249}
250
Yifan Hong024a1242018-08-10 13:50:46 -0700251static void runDevGcFstab(void) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800252 std::string path;
Eric Biggers019d5162020-10-15 16:54:38 -0700253 for (const auto& entry : fstab_default) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800254 if (!entry.sysfs_path.empty()) {
255 path = entry.sysfs_path;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800256 break;
257 }
258 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800259
260 if (path.empty()) {
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800261 return;
262 }
263
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800264 path = path + "/manual_gc";
265 Timer timer;
266
267 LOG(DEBUG) << "Start Dev GC on " << path;
268 while (1) {
269 std::string require;
270 if (!ReadFileToString(path, &require)) {
271 PLOG(WARNING) << "Reading manual_gc failed in " << path;
272 break;
273 }
Yifan Hong024a1242018-08-10 13:50:46 -0700274 require = android::base::Trim(require);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800275 if (require == "" || require == "off" || require == "disabled") {
276 LOG(DEBUG) << "No more to do Dev GC";
277 break;
278 }
279
280 LOG(DEBUG) << "Trigger Dev GC on " << path;
281 if (!WriteStringToFile("1", path)) {
282 PLOG(WARNING) << "Start Dev GC failed on " << path;
283 break;
284 }
285
286 if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
287 LOG(WARNING) << "Dev GC timeout";
288 break;
289 }
290 sleep(2);
291 }
292 LOG(DEBUG) << "Stop Dev GC on " << path;
293 if (!WriteStringToFile("0", path)) {
294 PLOG(WARNING) << "Stop Dev GC failed on " << path;
295 }
296 return;
297}
298
Yifan Hong024a1242018-08-10 13:50:46 -0700299class GcCallback : public IGarbageCollectCallback {
300 public:
301 Return<void> onFinish(Result result) override {
302 std::unique_lock<std::mutex> lock(mMutex);
303 mFinished = true;
304 mResult = result;
305 lock.unlock();
306 mCv.notify_all();
307 return Void();
308 }
309 void wait(uint64_t seconds) {
310 std::unique_lock<std::mutex> lock(mMutex);
311 mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; });
312
313 if (!mFinished) {
314 LOG(WARNING) << "Dev GC on HAL timeout";
315 } else if (mResult != Result::SUCCESS) {
316 LOG(WARNING) << "Dev GC on HAL failed with " << toString(mResult);
317 } else {
318 LOG(INFO) << "Dev GC on HAL successful";
319 }
320 }
321
322 private:
323 std::mutex mMutex;
324 std::condition_variable mCv;
325 bool mFinished{false};
326 Result mResult{Result::UNKNOWN_ERROR};
327};
328
Yifan Hong7a37c932018-09-19 10:28:16 -0700329static void runDevGcOnHal(sp<IStorage> service) {
Yifan Hong024a1242018-08-10 13:50:46 -0700330 LOG(DEBUG) << "Start Dev GC on HAL";
331 sp<GcCallback> cb = new GcCallback();
332 auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb);
333 if (!ret.isOk()) {
334 LOG(WARNING) << "Cannot start Dev GC on HAL: " << ret.description();
335 return;
336 }
337 cb->wait(DEVGC_TIMEOUT_SEC);
338}
339
340static void runDevGc(void) {
Yifan Hong7a37c932018-09-19 10:28:16 -0700341 auto service = IStorage::getService();
Yifan Hong024a1242018-08-10 13:50:46 -0700342 if (service != nullptr) {
343 runDevGcOnHal(service);
344 } else {
345 // fallback to legacy code path
346 runDevGcFstab();
347 }
348}
349
Jin Qiana370c142017-10-17 15:41:45 -0700350int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
351 std::unique_lock<std::mutex> lk(cv_m);
352 if (idle_maint_stat != IdleMaintStats::kStopped) {
353 LOG(DEBUG) << "idle maintenance is already running";
354 if (listener) {
355 android::os::PersistableBundle extras;
356 listener->onFinished(0, extras);
357 }
358 return android::OK;
359 }
360 idle_maint_stat = IdleMaintStats::kRunning;
361 lk.unlock();
362
363 LOG(DEBUG) << "idle maintenance started";
364
Tri Vo15bbe222019-06-21 12:21:48 -0700365 android::wakelock::WakeLock wl{kWakeLock};
Jin Qiana370c142017-10-17 15:41:45 -0700366
367 std::list<std::string> paths;
368 addFromFstab(&paths, PathTypes::kBlkDevice);
369 addFromVolumeManager(&paths, PathTypes::kBlkDevice);
370
371 startGc(paths);
372
373 bool gc_aborted = waitForGc(paths);
374
375 stopGc(paths);
376
377 lk.lock();
378 idle_maint_stat = IdleMaintStats::kStopped;
379 lk.unlock();
380
381 cv_stop.notify_one();
382
383 if (!gc_aborted) {
384 Trim(nullptr);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800385 runDevGc();
Jin Qiana370c142017-10-17 15:41:45 -0700386 }
387
388 if (listener) {
389 android::os::PersistableBundle extras;
390 listener->onFinished(0, extras);
391 }
392
393 LOG(DEBUG) << "idle maintenance completed";
394
Jin Qiana370c142017-10-17 15:41:45 -0700395 return android::OK;
396}
397
398int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
Tri Vo15bbe222019-06-21 12:21:48 -0700399 android::wakelock::WakeLock wl{kWakeLock};
Jin Qiana370c142017-10-17 15:41:45 -0700400
401 std::unique_lock<std::mutex> lk(cv_m);
402 if (idle_maint_stat != IdleMaintStats::kStopped) {
403 idle_maint_stat = IdleMaintStats::kAbort;
404 lk.unlock();
405 cv_abort.notify_one();
406 lk.lock();
407 LOG(DEBUG) << "aborting idle maintenance";
Paul Crowley14c8c072018-09-18 13:30:21 -0700408 cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; });
Jin Qiana370c142017-10-17 15:41:45 -0700409 }
410 lk.unlock();
411
412 if (listener) {
413 android::os::PersistableBundle extras;
414 listener->onFinished(0, extras);
415 }
416
Jin Qiana370c142017-10-17 15:41:45 -0700417 LOG(DEBUG) << "idle maintenance stopped";
418
419 return android::OK;
420}
421
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700422} // namespace vold
423} // namespace android