blob: 7744024a5c5f3ddb10e13c3c5c30ecb9006c56b8 [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"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070018#include "Utils.h"
19#include "VolumeManager.h"
Jin Qiana370c142017-10-17 15:41:45 -070020#include "model/PrivateVolume.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070021
Jin Qiana370c142017-10-17 15:41:45 -070022#include <thread>
23
24#include <android-base/chrono_utils.h>
25#include <android-base/file.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/stringprintf.h>
27#include <android-base/logging.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070028#include <fs_mgr.h>
29#include <private/android_filesystem_config.h>
30#include <hardware_legacy/power.h>
31
32#include <dirent.h>
33#include <sys/mount.h>
34#include <sys/stat.h>
35#include <sys/types.h>
36#include <sys/wait.h>
37#include <fcntl.h>
38
Jin Qiana370c142017-10-17 15:41:45 -070039using android::base::Basename;
40using android::base::ReadFileToString;
41using android::base::Realpath;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070042using android::base::StringPrintf;
Jin Qiana370c142017-10-17 15:41:45 -070043using android::base::Timer;
44using android::base::WriteStringToFile;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070045
46namespace android {
47namespace vold {
48
Jin Qiana370c142017-10-17 15:41:45 -070049enum class PathTypes {
50 kMountPoint = 1,
51 kBlkDevice,
52};
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070053
Jin Qiana370c142017-10-17 15:41:45 -070054enum class IdleMaintStats {
55 kStopped = 1,
56 kRunning,
57 kAbort,
58};
59
60static const char* kWakeLock = "IdleMaint";
61static const int DIRTY_SEGMENTS_THRESHOLD = 100;
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -080062/*
63 * Timing policy:
64 * 1. F2FS_GC = 7 mins
65 * 2. Trim = 1 min
66 * 3. Dev GC = 2 mins
67 */
68static const int GC_TIMEOUT_SEC = 420;
69static const int DEVGC_TIMEOUT_SEC = 120;
Jin Qiana370c142017-10-17 15:41:45 -070070
71static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
72static std::condition_variable cv_abort, cv_stop;
73static std::mutex cv_m;
74
75static void addFromVolumeManager(std::list<std::string>* paths,
76 PathTypes path_type) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070077 VolumeManager* vm = VolumeManager::Instance();
78 std::list<std::string> privateIds;
79 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070080 for (const auto& id : privateIds) {
Jin Qiana370c142017-10-17 15:41:45 -070081 PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070082 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
Jin Qiana370c142017-10-17 15:41:45 -070083 if (path_type == PathTypes::kMountPoint) {
84 paths->push_back(vol->getPath());
85 } else if (path_type == PathTypes::kBlkDevice) {
86 std::string gc_path;
87 const std::string& fs_type = vol->getFsType();
88 if (fs_type == "f2fs" &&
89 Realpath(vol->getRawDevPath(), &gc_path)) {
90 paths->push_back(std::string("/sys/fs/") + fs_type +
91 "/" + Basename(gc_path));
92 }
93 }
94
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070095 }
96 }
97}
98
Jin Qiana370c142017-10-17 15:41:45 -070099static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800100 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
101 fs_mgr_free_fstab);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700102 struct fstab_rec *prev_rec = NULL;
103
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700104 for (int i = 0; i < fstab->num_entries; i++) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600105 auto fs_type = std::string(fstab->recs[i].fs_type);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700106 /* Skip raw partitions */
Jeff Sharkey3472e522017-10-06 18:02:53 -0600107 if (fs_type == "emmc" || fs_type == "mtd") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700108 continue;
109 }
110 /* Skip read-only filesystems */
111 if (fstab->recs[i].flags & MS_RDONLY) {
112 continue;
113 }
114 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
115 continue; /* Should we trim fat32 filesystems? */
116 }
117 if (fs_mgr_is_notrim(&fstab->recs[i])) {
118 continue;
119 }
120
121 /* Skip the multi-type partitions, which are required to be following each other.
122 * See fs_mgr.c's mount_with_alternatives().
123 */
124 if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
125 continue;
126 }
127
Jin Qiana370c142017-10-17 15:41:45 -0700128 if (path_type == PathTypes::kMountPoint) {
129 paths->push_back(fstab->recs[i].mount_point);
130 } else if (path_type == PathTypes::kBlkDevice) {
131 std::string gc_path;
132 if (std::string(fstab->recs[i].fs_type) == "f2fs" &&
133 Realpath(fstab->recs[i].blk_device, &gc_path)) {
134 paths->push_back(std::string("/sys/fs/") + fstab->recs[i].fs_type +
135 "/" + Basename(gc_path));
136 }
137 }
138
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700139 prev_rec = &fstab->recs[i];
140 }
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) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700144 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
145
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;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700178 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600179 << " in " << nanoseconds_to_milliseconds(time) << "ms";
180 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
194 release_wake_lock(kWakeLock);
195}
196
Jin Qiana370c142017-10-17 15:41:45 -0700197static bool waitForGc(const std::list<std::string>& paths) {
198 std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
199 bool stop = false, aborted = false;
200 Timer timer;
201
202 while (!stop && !aborted) {
203 stop = true;
204 for (const auto& path : paths) {
205 std::string dirty_segments;
206 if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
207 PLOG(WARNING) << "Reading dirty_segments failed in " << path;
208 continue;
209 }
210 if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
211 stop = false;
212 break;
213 }
214 }
215
216 if (stop) break;
217
218 if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
219 LOG(WARNING) << "GC timeout";
220 break;
221 }
222
223 lk.lock();
224 aborted = cv_abort.wait_for(lk, 10s, []{
225 return idle_maint_stat == IdleMaintStats::kAbort;});
226 lk.unlock();
227 }
228
229 return aborted;
230}
231
232static int startGc(const std::list<std::string>& paths) {
233 for (const auto& path : paths) {
234 LOG(DEBUG) << "Start GC on " << path;
Jaegeuk Kima6aae2f2018-02-17 06:02:30 -0800235 if (!WriteStringToFile("1", path + "/discard_granularity")) {
236 PLOG(WARNING) << "Set discard gralunarity failed on" << path;
237 }
Jin Qiana370c142017-10-17 15:41:45 -0700238 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 }
Jaegeuk Kima6aae2f2018-02-17 06:02:30 -0800251 if (!WriteStringToFile("16", path + "/discard_granularity")) {
252 PLOG(WARNING) << "Set discard gralunarity failed on" << path;
253 }
Jin Qiana370c142017-10-17 15:41:45 -0700254 }
255 return android::OK;
256}
257
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800258static void runDevGc(void) {
259 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
260 fs_mgr_free_fstab);
261 struct fstab_rec *rec = NULL;
262
263 for (int i = 0; i < fstab->num_entries; i++) {
264 if (fs_mgr_has_sysfs_path(&fstab->recs[i])) {
265 rec = &fstab->recs[i];
266 break;
267 }
268 }
269 if (!rec) {
270 return;
271 }
272
273 std::string path;
274 path.append(rec->sysfs_path);
275 path = path + "/manual_gc";
276 Timer timer;
277
278 LOG(DEBUG) << "Start Dev GC on " << path;
279 while (1) {
280 std::string require;
281 if (!ReadFileToString(path, &require)) {
282 PLOG(WARNING) << "Reading manual_gc failed in " << path;
283 break;
284 }
285
286 if (require == "" || require == "off" || require == "disabled") {
287 LOG(DEBUG) << "No more to do Dev GC";
288 break;
289 }
290
291 LOG(DEBUG) << "Trigger Dev GC on " << path;
292 if (!WriteStringToFile("1", path)) {
293 PLOG(WARNING) << "Start Dev GC failed on " << path;
294 break;
295 }
296
297 if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
298 LOG(WARNING) << "Dev GC timeout";
299 break;
300 }
301 sleep(2);
302 }
303 LOG(DEBUG) << "Stop Dev GC on " << path;
304 if (!WriteStringToFile("0", path)) {
305 PLOG(WARNING) << "Stop Dev GC failed on " << path;
306 }
307 return;
308}
309
Jin Qiana370c142017-10-17 15:41:45 -0700310int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
311 std::unique_lock<std::mutex> lk(cv_m);
312 if (idle_maint_stat != IdleMaintStats::kStopped) {
313 LOG(DEBUG) << "idle maintenance is already running";
314 if (listener) {
315 android::os::PersistableBundle extras;
316 listener->onFinished(0, extras);
317 }
318 return android::OK;
319 }
320 idle_maint_stat = IdleMaintStats::kRunning;
321 lk.unlock();
322
323 LOG(DEBUG) << "idle maintenance started";
324
325 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
326
327 std::list<std::string> paths;
328 addFromFstab(&paths, PathTypes::kBlkDevice);
329 addFromVolumeManager(&paths, PathTypes::kBlkDevice);
330
331 startGc(paths);
332
333 bool gc_aborted = waitForGc(paths);
334
335 stopGc(paths);
336
337 lk.lock();
338 idle_maint_stat = IdleMaintStats::kStopped;
339 lk.unlock();
340
341 cv_stop.notify_one();
342
343 if (!gc_aborted) {
344 Trim(nullptr);
Jaegeuk Kimeefc5ee2018-02-12 21:57:04 -0800345 runDevGc();
Jin Qiana370c142017-10-17 15:41:45 -0700346 }
347
348 if (listener) {
349 android::os::PersistableBundle extras;
350 listener->onFinished(0, extras);
351 }
352
353 LOG(DEBUG) << "idle maintenance completed";
354
355 release_wake_lock(kWakeLock);
356
357 return android::OK;
358}
359
360int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
361 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
362
363 std::unique_lock<std::mutex> lk(cv_m);
364 if (idle_maint_stat != IdleMaintStats::kStopped) {
365 idle_maint_stat = IdleMaintStats::kAbort;
366 lk.unlock();
367 cv_abort.notify_one();
368 lk.lock();
369 LOG(DEBUG) << "aborting idle maintenance";
370 cv_stop.wait(lk, []{
371 return idle_maint_stat == IdleMaintStats::kStopped;});
372 }
373 lk.unlock();
374
375 if (listener) {
376 android::os::PersistableBundle extras;
377 listener->onFinished(0, extras);
378 }
379
380 release_wake_lock(kWakeLock);
381
382 LOG(DEBUG) << "idle maintenance stopped";
383
384 return android::OK;
385}
386
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700387} // namespace vold
388} // namespace android