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 "FileDeviceUtils.h"
19 #include "Utils.h"
20 #include "VolumeManager.h"
21 #include "model/PrivateVolume.h"
22 
23 #include <thread>
24 
25 #include <android-base/chrono_utils.h>
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
28 #include <android-base/stringprintf.h>
29 #include <android-base/strings.h>
30 #include <android/hardware/health/storage/1.0/IStorage.h>
31 #include <fs_mgr.h>
32 #include <private/android_filesystem_config.h>
33 #include <wakelock/wakelock.h>
34 
35 #include <dirent.h>
36 #include <fcntl.h>
37 #include <sys/mount.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 
42 using android::base::Basename;
43 using android::base::ReadFileToString;
44 using android::base::Realpath;
45 using android::base::StringPrintf;
46 using android::base::Timer;
47 using android::base::WriteStringToFile;
48 using android::fs_mgr::Fstab;
49 using android::fs_mgr::ReadDefaultFstab;
50 using android::hardware::Return;
51 using android::hardware::Void;
52 using android::hardware::health::storage::V1_0::IStorage;
53 using android::hardware::health::storage::V1_0::IGarbageCollectCallback;
54 using android::hardware::health::storage::V1_0::Result;
55 
56 namespace android {
57 namespace vold {
58 
59 enum class PathTypes {
60     kMountPoint = 1,
61     kBlkDevice,
62 };
63 
64 enum class IdleMaintStats {
65     kStopped = 1,
66     kRunning,
67     kAbort,
68 };
69 
70 static const char* kWakeLock = "IdleMaint";
71 static const int DIRTY_SEGMENTS_THRESHOLD = 100;
72 /*
73  * Timing policy:
74  *  1. F2FS_GC = 7 mins
75  *  2. Trim = 1 min
76  *  3. Dev GC = 2 mins
77  */
78 static const int GC_TIMEOUT_SEC = 420;
79 static const int DEVGC_TIMEOUT_SEC = 120;
80 
81 static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
82 static std::condition_variable cv_abort, cv_stop;
83 static std::mutex cv_m;
84 
addFromVolumeManager(std::list<std::string> * paths,PathTypes path_type)85 static void addFromVolumeManager(std::list<std::string>* paths, PathTypes path_type) {
86     VolumeManager* vm = VolumeManager::Instance();
87     std::list<std::string> privateIds;
88     vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
89     for (const auto& id : privateIds) {
90         PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
91         if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
92             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();
97                 if (fs_type == "f2fs" && (Realpath(vol->getRawDmDevPath(), &gc_path) ||
98                                           Realpath(vol->getRawDevPath(), &gc_path))) {
99                     paths->push_back(std::string("/sys/fs/") + fs_type + "/" + Basename(gc_path));
100                 }
101             }
102         }
103     }
104 }
105 
addFromFstab(std::list<std::string> * paths,PathTypes path_type)106 static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
107     Fstab fstab;
108     ReadDefaultFstab(&fstab);
109 
110     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") {
114             continue;
115         }
116         // Skip read-only filesystems
117         if (entry.flags & MS_RDONLY) {
118             continue;
119         }
120         if (entry.fs_mgr_flags.vold_managed) {
121             continue;  // Should we trim fat32 filesystems?
122         }
123         if (entry.fs_mgr_flags.no_trim) {
124             continue;
125         }
126 
127         // 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) {
130             continue;
131         }
132 
133         if (path_type == PathTypes::kMountPoint) {
134             paths->push_back(entry.mount_point);
135         } else if (path_type == PathTypes::kBlkDevice) {
136             std::string gc_path;
137             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));
140             }
141         }
142 
143         previous_mount_point = entry.mount_point;
144     }
145 }
146 
Trim(const android::sp<android::os::IVoldTaskListener> & listener)147 void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
148     android::wakelock::WakeLock wl{kWakeLock};
149 
150     // Collect both fstab and vold volumes
151     std::list<std::string> paths;
152     addFromFstab(&paths, PathTypes::kMountPoint);
153     addFromVolumeManager(&paths, PathTypes::kMountPoint);
154 
155     for (const auto& path : paths) {
156         LOG(DEBUG) << "Starting trim of " << path;
157 
158         android::os::PersistableBundle extras;
159         extras.putString(String16("path"), String16(path.c_str()));
160 
161         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;
164             if (listener) {
165                 listener->onStatus(-1, extras);
166             }
167             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);
175         if (ioctl(fd, FITRIM, &range)) {
176             PLOG(WARNING) << "Trim failed on " << path;
177             if (listener) {
178                 listener->onStatus(-1, extras);
179             }
180         } else {
181             nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
182             LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in "
183                       << nanoseconds_to_milliseconds(time) << "ms";
184             extras.putLong(String16("bytes"), range.len);
185             extras.putLong(String16("time"), time);
186             if (listener) {
187                 listener->onStatus(0, extras);
188             }
189         }
190         close(fd);
191     }
192 
193     if (listener) {
194         android::os::PersistableBundle extras;
195         listener->onFinished(0, extras);
196     }
197 
198 }
199 
waitForGc(const std::list<std::string> & paths)200 static 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();
227         aborted =
228             cv_abort.wait_for(lk, 10s, [] { return idle_maint_stat == IdleMaintStats::kAbort; });
229         lk.unlock();
230     }
231 
232     return aborted;
233 }
234 
startGc(const std::list<std::string> & paths)235 static 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 
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     }
252     return android::OK;
253 }
254 
runDevGcFstab(void)255 static void runDevGcFstab(void) {
256     Fstab fstab;
257     ReadDefaultFstab(&fstab);
258 
259     std::string path;
260     for (const auto& entry : fstab) {
261         if (!entry.sysfs_path.empty()) {
262             path = entry.sysfs_path;
263             break;
264         }
265     }
266 
267     if (path.empty()) {
268         return;
269     }
270 
271     path = path + "/manual_gc";
272     Timer timer;
273 
274     LOG(DEBUG) << "Start Dev GC on " << path;
275     while (1) {
276         std::string require;
277         if (!ReadFileToString(path, &require)) {
278             PLOG(WARNING) << "Reading manual_gc failed in " << path;
279             break;
280         }
281         require = android::base::Trim(require);
282         if (require == "" || require == "off" || require == "disabled") {
283             LOG(DEBUG) << "No more to do Dev GC";
284             break;
285         }
286 
287         LOG(DEBUG) << "Trigger Dev GC on " << path;
288         if (!WriteStringToFile("1", path)) {
289             PLOG(WARNING) << "Start Dev GC failed on " << path;
290             break;
291         }
292 
293         if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
294             LOG(WARNING) << "Dev GC timeout";
295             break;
296         }
297         sleep(2);
298     }
299     LOG(DEBUG) << "Stop Dev GC on " << path;
300     if (!WriteStringToFile("0", path)) {
301         PLOG(WARNING) << "Stop Dev GC failed on " << path;
302     }
303     return;
304 }
305 
306 class GcCallback : public IGarbageCollectCallback {
307   public:
onFinish(Result result)308     Return<void> onFinish(Result result) override {
309         std::unique_lock<std::mutex> lock(mMutex);
310         mFinished = true;
311         mResult = result;
312         lock.unlock();
313         mCv.notify_all();
314         return Void();
315     }
wait(uint64_t seconds)316     void wait(uint64_t seconds) {
317         std::unique_lock<std::mutex> lock(mMutex);
318         mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; });
319 
320         if (!mFinished) {
321             LOG(WARNING) << "Dev GC on HAL timeout";
322         } else if (mResult != Result::SUCCESS) {
323             LOG(WARNING) << "Dev GC on HAL failed with " << toString(mResult);
324         } else {
325             LOG(INFO) << "Dev GC on HAL successful";
326         }
327     }
328 
329   private:
330     std::mutex mMutex;
331     std::condition_variable mCv;
332     bool mFinished{false};
333     Result mResult{Result::UNKNOWN_ERROR};
334 };
335 
runDevGcOnHal(sp<IStorage> service)336 static void runDevGcOnHal(sp<IStorage> service) {
337     LOG(DEBUG) << "Start Dev GC on HAL";
338     sp<GcCallback> cb = new GcCallback();
339     auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb);
340     if (!ret.isOk()) {
341         LOG(WARNING) << "Cannot start Dev GC on HAL: " << ret.description();
342         return;
343     }
344     cb->wait(DEVGC_TIMEOUT_SEC);
345 }
346 
runDevGc(void)347 static void runDevGc(void) {
348     auto service = IStorage::getService();
349     if (service != nullptr) {
350         runDevGcOnHal(service);
351     } else {
352         // fallback to legacy code path
353         runDevGcFstab();
354     }
355 }
356 
RunIdleMaint(const android::sp<android::os::IVoldTaskListener> & listener)357 int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
358     std::unique_lock<std::mutex> lk(cv_m);
359     if (idle_maint_stat != IdleMaintStats::kStopped) {
360         LOG(DEBUG) << "idle maintenance is already running";
361         if (listener) {
362             android::os::PersistableBundle extras;
363             listener->onFinished(0, extras);
364         }
365         return android::OK;
366     }
367     idle_maint_stat = IdleMaintStats::kRunning;
368     lk.unlock();
369 
370     LOG(DEBUG) << "idle maintenance started";
371 
372     android::wakelock::WakeLock wl{kWakeLock};
373 
374     std::list<std::string> paths;
375     addFromFstab(&paths, PathTypes::kBlkDevice);
376     addFromVolumeManager(&paths, PathTypes::kBlkDevice);
377 
378     startGc(paths);
379 
380     bool gc_aborted = waitForGc(paths);
381 
382     stopGc(paths);
383 
384     lk.lock();
385     idle_maint_stat = IdleMaintStats::kStopped;
386     lk.unlock();
387 
388     cv_stop.notify_one();
389 
390     if (!gc_aborted) {
391         Trim(nullptr);
392         runDevGc();
393     }
394 
395     if (listener) {
396         android::os::PersistableBundle extras;
397         listener->onFinished(0, extras);
398     }
399 
400     LOG(DEBUG) << "idle maintenance completed";
401 
402     return android::OK;
403 }
404 
AbortIdleMaint(const android::sp<android::os::IVoldTaskListener> & listener)405 int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
406     android::wakelock::WakeLock wl{kWakeLock};
407 
408     std::unique_lock<std::mutex> lk(cv_m);
409     if (idle_maint_stat != IdleMaintStats::kStopped) {
410         idle_maint_stat = IdleMaintStats::kAbort;
411         lk.unlock();
412         cv_abort.notify_one();
413         lk.lock();
414         LOG(DEBUG) << "aborting idle maintenance";
415         cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; });
416     }
417     lk.unlock();
418 
419     if (listener) {
420         android::os::PersistableBundle extras;
421         listener->onFinished(0, extras);
422     }
423 
424     LOG(DEBUG) << "idle maintenance stopped";
425 
426     return android::OK;
427 }
428 
429 }  // namespace vold
430 }  // namespace android
431