blob: 2718095ca87cd4d2ca3984a424d6166d53ef117a [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"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070018#include "Utils.h"
19#include "VolumeManager.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070020
Elliott Hughes7e128fb2015-12-04 15:50:53 -080021#include <android-base/stringprintf.h>
22#include <android-base/logging.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070023#include <fs_mgr.h>
24#include <private/android_filesystem_config.h>
25#include <hardware_legacy/power.h>
26
27#include <dirent.h>
28#include <sys/mount.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32#include <fcntl.h>
33
34/* From a would-be kernel header */
35#define FIDTRIM _IOWR('f', 128, struct fstrim_range) /* Deep discard trim */
36
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070037using android::base::StringPrintf;
38
39namespace android {
40namespace vold {
41
42static const char* kWakeLock = "TrimTask";
43
Jeff Sharkey52f7a912017-09-15 12:57:44 -060044TrimTask::TrimTask(int flags, const android::sp<android::os::IVoldTaskListener>& listener) :
45 mFlags(flags), mListener(listener) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070046 // Collect both fstab and vold volumes
47 addFromFstab();
48
49 VolumeManager* vm = VolumeManager::Instance();
50 std::list<std::string> privateIds;
51 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070052 for (const auto& id : privateIds) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070053 auto vol = vm->findVolume(id);
54 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
55 mPaths.push_back(vol->getPath());
56 }
57 }
58}
59
60TrimTask::~TrimTask() {
61}
62
63void TrimTask::addFromFstab() {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +080064 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
65 fs_mgr_free_fstab);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070066 struct fstab_rec *prev_rec = NULL;
67
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070068 for (int i = 0; i < fstab->num_entries; i++) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060069 auto fs_type = std::string(fstab->recs[i].fs_type);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070070 /* Skip raw partitions */
Jeff Sharkey3472e522017-10-06 18:02:53 -060071 if (fs_type == "emmc" || fs_type == "mtd") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070072 continue;
73 }
74 /* Skip read-only filesystems */
75 if (fstab->recs[i].flags & MS_RDONLY) {
76 continue;
77 }
78 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
79 continue; /* Should we trim fat32 filesystems? */
80 }
81 if (fs_mgr_is_notrim(&fstab->recs[i])) {
82 continue;
83 }
84
85 /* Skip the multi-type partitions, which are required to be following each other.
86 * See fs_mgr.c's mount_with_alternatives().
87 */
88 if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
89 continue;
90 }
91
92 mPaths.push_back(fstab->recs[i].mount_point);
93 prev_rec = &fstab->recs[i];
94 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070095}
96
97void TrimTask::start() {
98 mThread = std::thread(&TrimTask::run, this);
99}
100
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700101void TrimTask::run() {
102 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
103
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700104 for (const auto& path : mPaths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700105 LOG(DEBUG) << "Starting trim of " << path;
106
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600107 android::os::PersistableBundle extras;
108 extras.putString(String16("path"), String16(path.c_str()));
109
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700110 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
111 if (fd < 0) {
112 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600113 if (mListener) {
114 mListener->onStatus(-1, extras);
115 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700116 continue;
117 }
118
119 struct fstrim_range range;
120 memset(&range, 0, sizeof(range));
121 range.len = ULLONG_MAX;
122
123 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
124 if (ioctl(fd, (mFlags & Flags::kDeepTrim) ? FIDTRIM : FITRIM, &range)) {
125 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600126 if (mListener) {
127 mListener->onStatus(-1, extras);
128 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700129 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600130 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700131 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600132 << " in " << nanoseconds_to_milliseconds(time) << "ms";
133 extras.putLong(String16("bytes"), range.len);
134 extras.putLong(String16("time"), time);
135 if (mListener) {
136 mListener->onStatus(0, extras);
137 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700138 }
139 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600140 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700141
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600142 if (mListener) {
143 android::os::PersistableBundle extras;
144 mListener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700145 }
146
147 release_wake_lock(kWakeLock);
148}
149
150} // namespace vold
151} // namespace android