blob: ed6374fafe519b60a1f2ca6fd4d01009848af3d2 [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"
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
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070034using android::base::StringPrintf;
35
36namespace android {
37namespace vold {
38
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060039static const char* kWakeLock = "IdleMaint";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070040
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060041static void addFromVolumeManager(std::list<std::string>* paths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070042 VolumeManager* vm = VolumeManager::Instance();
43 std::list<std::string> privateIds;
44 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -070045 for (const auto& id : privateIds) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070046 auto vol = vm->findVolume(id);
47 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060048 paths->push_back(vol->getPath());
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070049 }
50 }
51}
52
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060053static void addFromFstab(std::list<std::string>* paths) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +080054 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
55 fs_mgr_free_fstab);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070056 struct fstab_rec *prev_rec = NULL;
57
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070058 for (int i = 0; i < fstab->num_entries; i++) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060059 auto fs_type = std::string(fstab->recs[i].fs_type);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070060 /* Skip raw partitions */
Jeff Sharkey3472e522017-10-06 18:02:53 -060061 if (fs_type == "emmc" || fs_type == "mtd") {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070062 continue;
63 }
64 /* Skip read-only filesystems */
65 if (fstab->recs[i].flags & MS_RDONLY) {
66 continue;
67 }
68 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
69 continue; /* Should we trim fat32 filesystems? */
70 }
71 if (fs_mgr_is_notrim(&fstab->recs[i])) {
72 continue;
73 }
74
75 /* Skip the multi-type partitions, which are required to be following each other.
76 * See fs_mgr.c's mount_with_alternatives().
77 */
78 if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
79 continue;
80 }
81
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060082 paths->push_back(fstab->recs[i].mount_point);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070083 prev_rec = &fstab->recs[i];
84 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070085}
86
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060087void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070088 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
89
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060090 // Collect both fstab and vold volumes
91 std::list<std::string> paths;
92 addFromFstab(&paths);
93 addFromVolumeManager(&paths);
94
95 for (const auto& path : paths) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070096 LOG(DEBUG) << "Starting trim of " << path;
97
Jeff Sharkey52f7a912017-09-15 12:57:44 -060098 android::os::PersistableBundle extras;
99 extras.putString(String16("path"), String16(path.c_str()));
100
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700101 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
102 if (fd < 0) {
103 PLOG(WARNING) << "Failed to open " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600104 if (listener) {
105 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600106 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700107 continue;
108 }
109
110 struct fstrim_range range;
111 memset(&range, 0, sizeof(range));
112 range.len = ULLONG_MAX;
113
114 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600115 if (ioctl(fd, FITRIM, &range)) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700116 PLOG(WARNING) << "Trim failed on " << path;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600117 if (listener) {
118 listener->onStatus(-1, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600119 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700120 } else {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600121 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700122 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600123 << " in " << nanoseconds_to_milliseconds(time) << "ms";
124 extras.putLong(String16("bytes"), range.len);
125 extras.putLong(String16("time"), time);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600126 if (listener) {
127 listener->onStatus(0, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600128 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700129 }
130 close(fd);
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600131 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700132
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600133 if (listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600134 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600135 listener->onFinished(0, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700136 }
137
138 release_wake_lock(kWakeLock);
139}
140
141} // namespace vold
142} // namespace android