blob: 62086cda50b4107b7efa4c33e5fd2df67c521fae [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;
62static const int GC_TIMEOUT_SEC = 480;
63
64static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
65static std::condition_variable cv_abort, cv_stop;
66static std::mutex cv_m;
67
68static void addFromVolumeManager(std::list<std::string>* paths,
69 PathTypes path_type) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070070 VolumeManager* vm = VolumeManager::Instance();
71 std::list<std::string> privateIds;
72 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070073 for (const auto& id : privateIds) {
Jin Qiana370c142017-10-17 15:41:45 -070074 PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070075 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
Jin Qiana370c142017-10-17 15:41:45 -070076 if (path_type == PathTypes::kMountPoint) {
77 paths->push_back(vol->getPath());
78 } else if (path_type == PathTypes::kBlkDevice) {
79 std::string gc_path;
80 const std::string& fs_type = vol->getFsType();
81 if (fs_type == "f2fs" &&
82 Realpath(vol->getRawDevPath(), &gc_path)) {
83 paths->push_back(std::string("/sys/fs/") + fs_type +
84 "/" + Basename(gc_path));
85 }
86 }
87
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070088 }
89 }
90}
91
Jin Qiana370c142017-10-17 15:41:45 -070092static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +080093 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
94 fs_mgr_free_fstab);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070095 struct fstab_rec *prev_rec = NULL;
96
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070097 for (int i = 0; i < fstab->num_entries; i++) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060098 auto fs_type = std::string(fstab->recs[i].fs_type);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070099 /* Skip raw partitions */
Jeff Sharkey3472e522017-10-06 18:02:53 -0600100 if (fs_type == "emmc" || fs_type == "mtd") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700101 continue;
102 }
103 /* Skip read-only filesystems */
104 if (fstab->recs[i].flags & MS_RDONLY) {
105 continue;
106 }
107 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
108 continue; /* Should we trim fat32 filesystems? */
109 }
110 if (fs_mgr_is_notrim(&fstab->recs[i])) {
111 continue;
112 }
113
114 /* Skip the multi-type partitions, which are required to be following each other.
115 * See fs_mgr.c's mount_with_alternatives().
116 */
117 if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
118 continue;
119 }
120
Jin Qiana370c142017-10-17 15:41:45 -0700121 if (path_type == PathTypes::kMountPoint) {
122 paths->push_back(fstab->recs[i].mount_point);
123 } else if (path_type == PathTypes::kBlkDevice) {
124 std::string gc_path;
125 if (std::string(fstab->recs[i].fs_type) == "f2fs" &&
126 Realpath(fstab->recs[i].blk_device, &gc_path)) {
127 paths->push_back(std::string("/sys/fs/") + fstab->recs[i].fs_type +
128 "/" + Basename(gc_path));
129 }
130 }
131
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700132 prev_rec = &fstab->recs[i];
133 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700134}
135
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600136void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700137 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
138
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600139 // Collect both fstab and vold volumes
140 std::list<std::string> paths;
Jin Qiana370c142017-10-17 15:41:45 -0700141 addFromFstab(&paths, PathTypes::kMountPoint);
142 addFromVolumeManager(&paths, PathTypes::kMountPoint);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600143
144 for (const auto& path : paths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700145 LOG(DEBUG) << "Starting trim of " << path;
146
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600147 android::os::PersistableBundle extras;
148 extras.putString(String16("path"), String16(path.c_str()));
149
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700150 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
151 if (fd < 0) {
152 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600153 if (listener) {
154 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600155 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700156 continue;
157 }
158
159 struct fstrim_range range;
160 memset(&range, 0, sizeof(range));
161 range.len = ULLONG_MAX;
162
163 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600164 if (ioctl(fd, FITRIM, &range)) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700165 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600166 if (listener) {
167 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600168 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700169 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600170 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700171 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600172 << " in " << nanoseconds_to_milliseconds(time) << "ms";
173 extras.putLong(String16("bytes"), range.len);
174 extras.putLong(String16("time"), time);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600175 if (listener) {
176 listener->onStatus(0, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600177 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700178 }
179 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600180 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700181
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600182 if (listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600183 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600184 listener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700185 }
186
187 release_wake_lock(kWakeLock);
188}
189
Jin Qiana370c142017-10-17 15:41:45 -0700190static bool waitForGc(const std::list<std::string>& paths) {
191 std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
192 bool stop = false, aborted = false;
193 Timer timer;
194
195 while (!stop && !aborted) {
196 stop = true;
197 for (const auto& path : paths) {
198 std::string dirty_segments;
199 if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
200 PLOG(WARNING) << "Reading dirty_segments failed in " << path;
201 continue;
202 }
203 if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
204 stop = false;
205 break;
206 }
207 }
208
209 if (stop) break;
210
211 if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
212 LOG(WARNING) << "GC timeout";
213 break;
214 }
215
216 lk.lock();
217 aborted = cv_abort.wait_for(lk, 10s, []{
218 return idle_maint_stat == IdleMaintStats::kAbort;});
219 lk.unlock();
220 }
221
222 return aborted;
223}
224
225static int startGc(const std::list<std::string>& paths) {
226 for (const auto& path : paths) {
227 LOG(DEBUG) << "Start GC on " << path;
228 if (!WriteStringToFile("1", path + "/gc_urgent")) {
229 PLOG(WARNING) << "Start GC failed on " << path;
230 }
231 }
232 return android::OK;
233}
234
235static int stopGc(const std::list<std::string>& paths) {
236 for (const auto& path : paths) {
237 LOG(DEBUG) << "Stop GC on " << path;
238 if (!WriteStringToFile("0", path + "/gc_urgent")) {
239 PLOG(WARNING) << "Stop GC failed on " << path;
240 }
241 }
242 return android::OK;
243}
244
245int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
246 std::unique_lock<std::mutex> lk(cv_m);
247 if (idle_maint_stat != IdleMaintStats::kStopped) {
248 LOG(DEBUG) << "idle maintenance is already running";
249 if (listener) {
250 android::os::PersistableBundle extras;
251 listener->onFinished(0, extras);
252 }
253 return android::OK;
254 }
255 idle_maint_stat = IdleMaintStats::kRunning;
256 lk.unlock();
257
258 LOG(DEBUG) << "idle maintenance started";
259
260 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
261
262 std::list<std::string> paths;
263 addFromFstab(&paths, PathTypes::kBlkDevice);
264 addFromVolumeManager(&paths, PathTypes::kBlkDevice);
265
266 startGc(paths);
267
268 bool gc_aborted = waitForGc(paths);
269
270 stopGc(paths);
271
272 lk.lock();
273 idle_maint_stat = IdleMaintStats::kStopped;
274 lk.unlock();
275
276 cv_stop.notify_one();
277
278 if (!gc_aborted) {
279 Trim(nullptr);
280 }
281
282 if (listener) {
283 android::os::PersistableBundle extras;
284 listener->onFinished(0, extras);
285 }
286
287 LOG(DEBUG) << "idle maintenance completed";
288
289 release_wake_lock(kWakeLock);
290
291 return android::OK;
292}
293
294int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
295 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
296
297 std::unique_lock<std::mutex> lk(cv_m);
298 if (idle_maint_stat != IdleMaintStats::kStopped) {
299 idle_maint_stat = IdleMaintStats::kAbort;
300 lk.unlock();
301 cv_abort.notify_one();
302 lk.lock();
303 LOG(DEBUG) << "aborting idle maintenance";
304 cv_stop.wait(lk, []{
305 return idle_maint_stat == IdleMaintStats::kStopped;});
306 }
307 lk.unlock();
308
309 if (listener) {
310 android::os::PersistableBundle extras;
311 listener->onFinished(0, extras);
312 }
313
314 release_wake_lock(kWakeLock);
315
316 LOG(DEBUG) << "idle maintenance stopped";
317
318 return android::OK;
319}
320
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700321} // namespace vold
322} // namespace android