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 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 17 | #include "IdleMaint.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 <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 Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 34 | using android::base::StringPrintf; |
| 35 | |
| 36 | namespace android { |
| 37 | namespace vold { |
| 38 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 39 | static const char* kWakeLock = "IdleMaint"; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 40 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 41 | static void addFromVolumeManager(std::list<std::string>* paths) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 42 | VolumeManager* vm = VolumeManager::Instance(); |
| 43 | std::list<std::string> privateIds; |
| 44 | vm->listVolumes(VolumeBase::Type::kPrivate, privateIds); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 45 | for (const auto& id : privateIds) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 46 | auto vol = vm->findVolume(id); |
| 47 | if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) { |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 48 | paths->push_back(vol->getPath()); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 53 | static void addFromFstab(std::list<std::string>* paths) { |
Bowgo Tsai | e8fb6c3 | 2017-03-09 23:11:33 +0800 | [diff] [blame] | 54 | std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(), |
| 55 | fs_mgr_free_fstab); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 56 | struct fstab_rec *prev_rec = NULL; |
| 57 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 58 | for (int i = 0; i < fstab->num_entries; i++) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 59 | auto fs_type = std::string(fstab->recs[i].fs_type); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 60 | /* Skip raw partitions */ |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 61 | if (fs_type == "emmc" || fs_type == "mtd") { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 62 | 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 Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 82 | paths->push_back(fstab->recs[i].mount_point); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 83 | prev_rec = &fstab->recs[i]; |
| 84 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 87 | void Trim(const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 88 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 89 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 90 | // 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 Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 96 | LOG(DEBUG) << "Starting trim of " << path; |
| 97 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 98 | android::os::PersistableBundle extras; |
| 99 | extras.putString(String16("path"), String16(path.c_str())); |
| 100 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 101 | 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 Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 104 | if (listener) { |
| 105 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 106 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 107 | 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 Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 115 | if (ioctl(fd, FITRIM, &range)) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 116 | PLOG(WARNING) << "Trim failed on " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 117 | if (listener) { |
| 118 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 119 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 120 | } else { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 121 | nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 122 | LOG(INFO) << "Trimmed " << range.len << " bytes on " << path |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 123 | << " in " << nanoseconds_to_milliseconds(time) << "ms"; |
| 124 | extras.putLong(String16("bytes"), range.len); |
| 125 | extras.putLong(String16("time"), time); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 126 | if (listener) { |
| 127 | listener->onStatus(0, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 128 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 129 | } |
| 130 | close(fd); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 131 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 132 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 133 | if (listener) { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 134 | android::os::PersistableBundle extras; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 135 | listener->onFinished(0, extras); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | release_wake_lock(kWakeLock); |
| 139 | } |
| 140 | |
| 141 | } // namespace vold |
| 142 | } // namespace android |