blob: fafb3452476c4d7d12d5fac78d9fb0e8c758a87f [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 <cutils/properties.h>
24#include <fs_mgr.h>
25#include <private/android_filesystem_config.h>
26#include <hardware_legacy/power.h>
27
28#include <dirent.h>
29#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32#include <sys/wait.h>
33#include <fcntl.h>
34
35/* From a would-be kernel header */
36#define FIDTRIM _IOWR('f', 128, struct fstrim_range) /* Deep discard trim */
37
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070038using android::base::StringPrintf;
39
40namespace android {
41namespace vold {
42
43static const char* kWakeLock = "TrimTask";
44
Jeff Sharkey52f7a912017-09-15 12:57:44 -060045TrimTask::TrimTask(int flags, const android::sp<android::os::IVoldTaskListener>& listener) :
46 mFlags(flags), mListener(listener) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070047 // 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
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700102void TrimTask::run() {
103 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
104
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700105 for (const auto& path : mPaths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700106 LOG(DEBUG) << "Starting trim of " << path;
107
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600108 android::os::PersistableBundle extras;
109 extras.putString(String16("path"), String16(path.c_str()));
110
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700111 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
112 if (fd < 0) {
113 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600114 if (mListener) {
115 mListener->onStatus(-1, extras);
116 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700117 continue;
118 }
119
120 struct fstrim_range range;
121 memset(&range, 0, sizeof(range));
122 range.len = ULLONG_MAX;
123
124 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
125 if (ioctl(fd, (mFlags & Flags::kDeepTrim) ? FIDTRIM : FITRIM, &range)) {
126 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600127 if (mListener) {
128 mListener->onStatus(-1, extras);
129 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700130 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600131 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700132 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600133 << " in " << nanoseconds_to_milliseconds(time) << "ms";
134 extras.putLong(String16("bytes"), range.len);
135 extras.putLong(String16("time"), time);
136 if (mListener) {
137 mListener->onStatus(0, extras);
138 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700139 }
140 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600141 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700142
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600143 if (mListener) {
144 android::os::PersistableBundle extras;
145 mListener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700146 }
147
148 release_wake_lock(kWakeLock);
149}
150
151} // namespace vold
152} // namespace android