blob: 0eea715320c86813500ff05964c51c81ef807a2c [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
17#include "TrimTask.h"
18#include "Benchmark.h"
19#include "Utils.h"
20#include "VolumeManager.h"
21#include "ResponseCode.h"
22
Elliott Hughes7e128fb2015-12-04 15:50:53 -080023#include <android-base/stringprintf.h>
24#include <android-base/logging.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070025#include <cutils/properties.h>
26#include <fs_mgr.h>
27#include <private/android_filesystem_config.h>
28#include <hardware_legacy/power.h>
29
30#include <dirent.h>
31#include <sys/mount.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35#include <fcntl.h>
36
Jeff Sharkey1a20a642015-09-09 14:55:45 -070037#define BENCHMARK_ENABLED 1
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070038
39using android::base::StringPrintf;
40
41namespace android {
42namespace vold {
43
44static const char* kWakeLock = "TrimTask";
45
46TrimTask::TrimTask(int flags) : mFlags(flags) {
47 // Collect both fstab and vold volumes
48 addFromFstab();
49
50 VolumeManager* vm = VolumeManager::Instance();
51 std::list<std::string> privateIds;
52 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070053 for (const auto& id : privateIds) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070054 auto vol = vm->findVolume(id);
55 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
56 mPaths.push_back(vol->getPath());
57 }
58 }
59}
60
61TrimTask::~TrimTask() {
62}
63
64void TrimTask::addFromFstab() {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +080065 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
66 fs_mgr_free_fstab);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070067 struct fstab_rec *prev_rec = NULL;
68
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070069 for (int i = 0; i < fstab->num_entries; i++) {
70 /* Skip raw partitions */
71 if (!strcmp(fstab->recs[i].fs_type, "emmc") ||
72 !strcmp(fstab->recs[i].fs_type, "mtd")) {
73 continue;
74 }
75 /* Skip read-only filesystems */
76 if (fstab->recs[i].flags & MS_RDONLY) {
77 continue;
78 }
79 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
80 continue; /* Should we trim fat32 filesystems? */
81 }
82 if (fs_mgr_is_notrim(&fstab->recs[i])) {
83 continue;
84 }
85
86 /* Skip the multi-type partitions, which are required to be following each other.
87 * See fs_mgr.c's mount_with_alternatives().
88 */
89 if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
90 continue;
91 }
92
93 mPaths.push_back(fstab->recs[i].mount_point);
94 prev_rec = &fstab->recs[i];
95 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070096}
97
98void TrimTask::start() {
99 mThread = std::thread(&TrimTask::run, this);
100}
101
102static void notifyResult(const std::string& path, int64_t bytes, int64_t delta) {
103 std::string res(path
104 + " " + std::to_string(bytes)
105 + " " + std::to_string(delta));
106 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
107 ResponseCode::TrimResult, res.c_str(), false);
108}
109
110void TrimTask::run() {
111 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
112
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700113 for (const auto& path : mPaths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700114 LOG(DEBUG) << "Starting trim of " << path;
115
116 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
117 if (fd < 0) {
118 PLOG(WARNING) << "Failed to open " << path;
119 continue;
120 }
121
122 struct fstrim_range range;
123 memset(&range, 0, sizeof(range));
124 range.len = ULLONG_MAX;
125
126 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey9b738452018-01-12 10:42:13 -0700127 if (ioctl(fd, FITRIM, &range)) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700128 PLOG(WARNING) << "Trim failed on " << path;
129 notifyResult(path, -1, -1);
130 } else {
131 nsecs_t delta = systemTime(SYSTEM_TIME_BOOTTIME) - start;
132 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
133 << " in " << nanoseconds_to_milliseconds(delta) << "ms";
134 notifyResult(path, range.len, delta);
135 }
136 close(fd);
137
138 if (mFlags & Flags::kBenchmarkAfter) {
139#if BENCHMARK_ENABLED
140 BenchmarkPrivate(path);
141#else
142 LOG(DEBUG) << "Benchmark disabled";
143#endif
144 }
145 }
146
147 release_wake_lock(kWakeLock);
148}
149
150} // namespace vold
151} // namespace android