1 /*
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
17 #include "IdleMaint.h"
18 #include "Utils.h"
19 #include "VolumeManager.h"
20 #include "model/PrivateVolume.h"
21
22 #include <thread>
23
24 #include <android-base/chrono_utils.h>
25 #include <android-base/file.h>
26 #include <android-base/stringprintf.h>
27 #include <android-base/logging.h>
28 #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
39 using android::base::Basename;
40 using android::base::ReadFileToString;
41 using android::base::Realpath;
42 using android::base::StringPrintf;
43 using android::base::Timer;
44 using android::base::WriteStringToFile;
45
46 namespace android {
47 namespace vold {
48
49 enum class PathTypes {
50 kMountPoint = 1,
51 kBlkDevice,
52 };
53
54 enum class IdleMaintStats {
55 kStopped = 1,
56 kRunning,
57 kAbort,
58 };
59
60 static const char* kWakeLock = "IdleMaint";
61 static const int DIRTY_SEGMENTS_THRESHOLD = 100;
62 /*
63 * Timing policy:
64 * 1. F2FS_GC = 7 mins
65 * 2. Trim = 1 min
66 * 3. Dev GC = 2 mins
67 */
68 static const int GC_TIMEOUT_SEC = 420;
69 static const int DEVGC_TIMEOUT_SEC = 120;
70
71 static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
72 static std::condition_variable cv_abort, cv_stop;
73 static std::mutex cv_m;
74
addFromVolumeManager(std::list<std::string> * paths,PathTypes path_type)75 static void addFromVolumeManager(std::list<std::string>* paths,
76 PathTypes path_type) {
77 VolumeManager* vm = VolumeManager::Instance();
78 std::list<std::string> privateIds;
79 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
80 for (const auto& id : privateIds) {
81 PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
82 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
83 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
95 }
96 }
97 }
98
addFromFstab(std::list<std::string> * paths,PathTypes path_type)99 static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
100 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
101 fs_mgr_free_fstab);
102 struct fstab_rec *prev_rec = NULL;
103
104 for (int i = 0; i < fstab->num_entries; i++) {
105 auto fs_type = std::string(fstab->recs[i].fs_type);
106 /* Skip raw partitions */
107 if (fs_type == "emmc" || fs_type == "mtd") {
108 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
128 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
139 prev_rec = &fstab->recs[i];
140 }
141 }
142
Trim(const android::sp<android::os::IVoldTaskListener> & listener)143 void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
144 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
145
146 // Collect both fstab and vold volumes
147 std::list<std::string> paths;
148 addFromFstab(&paths, PathTypes::kMountPoint);
149 addFromVolumeManager(&paths, PathTypes::kMountPoint);
150
151 for (const auto& path : paths) {
152 LOG(DEBUG) << "Starting trim of " << path;
153
154 android::os::PersistableBundle extras;
155 extras.putString(String16("path"), String16(path.c_str()));
156
157 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;
160 if (listener) {
161 listener->onStatus(-1, extras);
162 }
163 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);
171 if (ioctl(fd, FITRIM, &range)) {
172 PLOG(WARNING) << "Trim failed on " << path;
173 if (listener) {
174 listener->onStatus(-1, extras);
175 }
176 } else {
177 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
178 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
179 << " in " << nanoseconds_to_milliseconds(time) << "ms";
180 extras.putLong(String16("bytes"), range.len);
181 extras.putLong(String16("time"), time);
182 if (listener) {
183 listener->onStatus(0, extras);
184 }
185 }
186 close(fd);
187 }
188
189 if (listener) {
190 android::os::PersistableBundle extras;
191 listener->onFinished(0, extras);
192 }
193
194 release_wake_lock(kWakeLock);
195 }
196
waitForGc(const std::list<std::string> & paths)197 static 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
startGc(const std::list<std::string> & paths)232 static int startGc(const std::list<std::string>& paths) {
233 for (const auto& path : paths) {
234 LOG(DEBUG) << "Start GC on " << path;
235 if (!WriteStringToFile("1", path + "/discard_granularity")) {
236 PLOG(WARNING) << "Set discard gralunarity failed on" << path;
237 }
238 if (!WriteStringToFile("1", path + "/gc_urgent")) {
239 PLOG(WARNING) << "Start GC failed on " << path;
240 }
241 }
242 return android::OK;
243 }
244
stopGc(const std::list<std::string> & paths)245 static 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 if (!WriteStringToFile("16", path + "/discard_granularity")) {
252 PLOG(WARNING) << "Set discard gralunarity failed on" << path;
253 }
254 }
255 return android::OK;
256 }
257
runDevGc(void)258 static 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
RunIdleMaint(const android::sp<android::os::IVoldTaskListener> & listener)310 int 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);
345 runDevGc();
346 }
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
AbortIdleMaint(const android::sp<android::os::IVoldTaskListener> & listener)360 int 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
387 } // namespace vold
388 } // namespace android
389