Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
| 24 | #include <android-base/logging.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 25 | #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 Sharkey | 1a20a64 | 2015-09-09 14:55:45 -0700 | [diff] [blame] | 37 | #define BENCHMARK_ENABLED 1 |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 38 | |
| 39 | using android::base::StringPrintf; |
| 40 | |
| 41 | namespace android { |
| 42 | namespace vold { |
| 43 | |
| 44 | static const char* kWakeLock = "TrimTask"; |
| 45 | |
| 46 | TrimTask::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 Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 53 | for (const auto& id : privateIds) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 54 | auto vol = vm->findVolume(id); |
| 55 | if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) { |
| 56 | mPaths.push_back(vol->getPath()); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | TrimTask::~TrimTask() { |
| 62 | } |
| 63 | |
| 64 | void TrimTask::addFromFstab() { |
Bowgo Tsai | e8fb6c3 | 2017-03-09 23:11:33 +0800 | [diff] [blame] | 65 | std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(), |
| 66 | fs_mgr_free_fstab); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 67 | struct fstab_rec *prev_rec = NULL; |
| 68 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 69 | 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 Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void TrimTask::start() { |
| 99 | mThread = std::thread(&TrimTask::run, this); |
| 100 | } |
| 101 | |
| 102 | static 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 | |
| 110 | void TrimTask::run() { |
| 111 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 112 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 113 | for (const auto& path : mPaths) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 114 | 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 Sharkey | 9b73845 | 2018-01-12 10:42:13 -0700 | [diff] [blame] | 127 | if (ioctl(fd, FITRIM, &range)) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 128 | 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 |