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" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 18 | #include "Utils.h" |
| 19 | #include "VolumeManager.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 20 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
| 22 | #include <android-base/logging.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 23 | #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 Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 38 | using android::base::StringPrintf; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace vold { |
| 42 | |
| 43 | static const char* kWakeLock = "TrimTask"; |
| 44 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 45 | TrimTask::TrimTask(int flags, const android::sp<android::os::IVoldTaskListener>& listener) : |
| 46 | mFlags(flags), mListener(listener) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 102 | void TrimTask::run() { |
| 103 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 104 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 105 | for (const auto& path : mPaths) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 106 | LOG(DEBUG) << "Starting trim of " << path; |
| 107 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 108 | android::os::PersistableBundle extras; |
| 109 | extras.putString(String16("path"), String16(path.c_str())); |
| 110 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 111 | 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 Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 114 | if (mListener) { |
| 115 | mListener->onStatus(-1, extras); |
| 116 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 117 | 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 Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 127 | if (mListener) { |
| 128 | mListener->onStatus(-1, extras); |
| 129 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 130 | } else { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 131 | nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 132 | LOG(INFO) << "Trimmed " << range.len << " bytes on " << path |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 133 | << " 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 Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 139 | } |
| 140 | close(fd); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 141 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 142 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 143 | if (mListener) { |
| 144 | android::os::PersistableBundle extras; |
| 145 | mListener->onFinished(0, extras); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | release_wake_lock(kWakeLock); |
| 149 | } |
| 150 | |
| 151 | } // namespace vold |
| 152 | } // namespace android |