blob: d37ec20902ad801bb93ad5cc1b03f01b5ec7ea1e [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"
20#include "ResponseCode.h"
21
Elliott Hughes7e128fb2015-12-04 15:50:53 -080022#include <android-base/stringprintf.h>
23#include <android-base/logging.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070024#include <cutils/properties.h>
25#include <fs_mgr.h>
26#include <private/android_filesystem_config.h>
27#include <hardware_legacy/power.h>
28
29#include <dirent.h>
30#include <sys/mount.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <fcntl.h>
35
36/* From a would-be kernel header */
37#define FIDTRIM _IOWR('f', 128, struct fstrim_range) /* Deep discard trim */
38
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070039using android::base::StringPrintf;
40
41namespace android {
42namespace vold {
43
44static const char* kWakeLock = "TrimTask";
45
Jeff Sharkey52f7a912017-09-15 12:57:44 -060046TrimTask::TrimTask(int flags, const android::sp<android::os::IVoldTaskListener>& listener) :
47 mFlags(flags), mListener(listener) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070048 // Collect both fstab and vold volumes
49 addFromFstab();
50
51 VolumeManager* vm = VolumeManager::Instance();
52 std::list<std::string> privateIds;
53 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070054 for (const auto& id : privateIds) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070055 auto vol = vm->findVolume(id);
56 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
57 mPaths.push_back(vol->getPath());
58 }
59 }
60}
61
62TrimTask::~TrimTask() {
63}
64
65void TrimTask::addFromFstab() {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +080066 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
67 fs_mgr_free_fstab);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070068 struct fstab_rec *prev_rec = NULL;
69
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070070 for (int i = 0; i < fstab->num_entries; i++) {
71 /* Skip raw partitions */
72 if (!strcmp(fstab->recs[i].fs_type, "emmc") ||
73 !strcmp(fstab->recs[i].fs_type, "mtd")) {
74 continue;
75 }
76 /* Skip read-only filesystems */
77 if (fstab->recs[i].flags & MS_RDONLY) {
78 continue;
79 }
80 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
81 continue; /* Should we trim fat32 filesystems? */
82 }
83 if (fs_mgr_is_notrim(&fstab->recs[i])) {
84 continue;
85 }
86
87 /* Skip the multi-type partitions, which are required to be following each other.
88 * See fs_mgr.c's mount_with_alternatives().
89 */
90 if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
91 continue;
92 }
93
94 mPaths.push_back(fstab->recs[i].mount_point);
95 prev_rec = &fstab->recs[i];
96 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070097}
98
99void TrimTask::start() {
100 mThread = std::thread(&TrimTask::run, this);
101}
102
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700103void TrimTask::run() {
104 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
105
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700106 for (const auto& path : mPaths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700107 LOG(DEBUG) << "Starting trim of " << path;
108
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600109 android::os::PersistableBundle extras;
110 extras.putString(String16("path"), String16(path.c_str()));
111
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700112 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
113 if (fd < 0) {
114 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600115 if (mListener) {
116 mListener->onStatus(-1, extras);
117 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700118 continue;
119 }
120
121 struct fstrim_range range;
122 memset(&range, 0, sizeof(range));
123 range.len = ULLONG_MAX;
124
125 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
126 if (ioctl(fd, (mFlags & Flags::kDeepTrim) ? FIDTRIM : FITRIM, &range)) {
127 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600128 if (mListener) {
129 mListener->onStatus(-1, extras);
130 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700131 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600132 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700133 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600134 << " in " << nanoseconds_to_milliseconds(time) << "ms";
135 extras.putLong(String16("bytes"), range.len);
136 extras.putLong(String16("time"), time);
137 if (mListener) {
138 mListener->onStatus(0, extras);
139 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700140 }
141 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600142 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700143
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600144 if (mListener) {
145 android::os::PersistableBundle extras;
146 mListener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700147 }
148
149 release_wake_lock(kWakeLock);
150}
151
152} // namespace vold
153} // namespace android