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" |
Jaegeuk Kim | 31e962f | 2018-07-29 06:56:57 -0700 | [diff] [blame] | 18 | #include "FileDeviceUtils.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 19 | #include "Utils.h" |
Eric Biggers | 019d516 | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 20 | #include "VoldUtil.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 21 | #include "VolumeManager.h" |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 22 | #include "model/PrivateVolume.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 23 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 24 | #include <thread> |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 25 | #include <utility> |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 26 | |
| 27 | #include <android-base/chrono_utils.h> |
| 28 | #include <android-base/file.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
| 31 | #include <android-base/strings.h> |
Yifan Hong | 7a37c93 | 2018-09-19 10:28:16 -0700 | [diff] [blame] | 32 | #include <android/hardware/health/storage/1.0/IStorage.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 33 | #include <fs_mgr.h> |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 34 | #include <private/android_filesystem_config.h> |
Tri Vo | 15bbe22 | 2019-06-21 12:21:48 -0700 | [diff] [blame] | 35 | #include <wakelock/wakelock.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 36 | |
| 37 | #include <dirent.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 38 | #include <fcntl.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 39 | #include <sys/mount.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/wait.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 43 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 44 | using android::base::Basename; |
| 45 | using android::base::ReadFileToString; |
| 46 | using android::base::Realpath; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 47 | using android::base::StringPrintf; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 48 | using android::base::Timer; |
| 49 | using android::base::WriteStringToFile; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 50 | using android::hardware::Return; |
| 51 | using android::hardware::Void; |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 52 | using HStorage = android::hardware::health::storage::V1_0::IStorage; |
| 53 | using HGarbageCollectCallback = android::hardware::health::storage::V1_0::IGarbageCollectCallback; |
| 54 | using HResult = android::hardware::health::storage::V1_0::Result; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 55 | |
| 56 | namespace android { |
| 57 | namespace vold { |
| 58 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 59 | enum class PathTypes { |
| 60 | kMountPoint = 1, |
| 61 | kBlkDevice, |
| 62 | }; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 63 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 64 | enum class IdleMaintStats { |
| 65 | kStopped = 1, |
| 66 | kRunning, |
| 67 | kAbort, |
| 68 | }; |
| 69 | |
| 70 | static const char* kWakeLock = "IdleMaint"; |
| 71 | static const int DIRTY_SEGMENTS_THRESHOLD = 100; |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 72 | /* |
| 73 | * Timing policy: |
| 74 | * 1. F2FS_GC = 7 mins |
| 75 | * 2. Trim = 1 min |
| 76 | * 3. Dev GC = 2 mins |
| 77 | */ |
| 78 | static const int GC_TIMEOUT_SEC = 420; |
| 79 | static const int DEVGC_TIMEOUT_SEC = 120; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 80 | |
| 81 | static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped); |
| 82 | static std::condition_variable cv_abort, cv_stop; |
| 83 | static std::mutex cv_m; |
| 84 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 85 | static void addFromVolumeManager(std::list<std::string>* paths, PathTypes path_type) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 86 | VolumeManager* vm = VolumeManager::Instance(); |
| 87 | std::list<std::string> privateIds; |
| 88 | vm->listVolumes(VolumeBase::Type::kPrivate, privateIds); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 89 | for (const auto& id : privateIds) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 90 | PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get()); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 91 | if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 92 | if (path_type == PathTypes::kMountPoint) { |
| 93 | paths->push_back(vol->getPath()); |
| 94 | } else if (path_type == PathTypes::kBlkDevice) { |
| 95 | std::string gc_path; |
| 96 | const std::string& fs_type = vol->getFsType(); |
Jaegeuk Kim | 31e962f | 2018-07-29 06:56:57 -0700 | [diff] [blame] | 97 | if (fs_type == "f2fs" && (Realpath(vol->getRawDmDevPath(), &gc_path) || |
| 98 | Realpath(vol->getRawDevPath(), &gc_path))) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 99 | paths->push_back(std::string("/sys/fs/") + fs_type + "/" + Basename(gc_path)); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 106 | static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) { |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 107 | std::string previous_mount_point; |
Eric Biggers | 019d516 | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 108 | for (const auto& entry : fstab_default) { |
Eric Biggers | 9a3dc8c | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 109 | // Skip raw partitions and swap space. |
| 110 | if (entry.fs_type == "emmc" || entry.fs_type == "mtd" || entry.fs_type == "swap") { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 111 | continue; |
| 112 | } |
Eric Biggers | 9a3dc8c | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 113 | // Skip read-only filesystems and bind mounts. |
| 114 | if (entry.flags & (MS_RDONLY | MS_BIND)) { |
| 115 | continue; |
| 116 | } |
| 117 | // Skip anything without an underlying block device, e.g. virtiofs. |
| 118 | if (entry.blk_device[0] != '/') { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 119 | continue; |
| 120 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 121 | if (entry.fs_mgr_flags.vold_managed) { |
| 122 | continue; // Should we trim fat32 filesystems? |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 123 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 124 | if (entry.fs_mgr_flags.no_trim) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 125 | continue; |
| 126 | } |
| 127 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 128 | // Skip the multi-type partitions, which are required to be following each other. |
| 129 | // See fs_mgr.c's mount_with_alternatives(). |
| 130 | if (entry.mount_point == previous_mount_point) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 134 | if (path_type == PathTypes::kMountPoint) { |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 135 | paths->push_back(entry.mount_point); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 136 | } else if (path_type == PathTypes::kBlkDevice) { |
| 137 | std::string gc_path; |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 138 | if (entry.fs_type == "f2fs" && |
| 139 | Realpath(android::vold::BlockDeviceForPath(entry.mount_point + "/"), &gc_path)) { |
| 140 | paths->push_back("/sys/fs/" + entry.fs_type + "/" + Basename(gc_path)); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 144 | previous_mount_point = entry.mount_point; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 145 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 148 | void Trim(const android::sp<android::os::IVoldTaskListener>& listener) { |
Tri Vo | 15bbe22 | 2019-06-21 12:21:48 -0700 | [diff] [blame] | 149 | android::wakelock::WakeLock wl{kWakeLock}; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 150 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 151 | // Collect both fstab and vold volumes |
| 152 | std::list<std::string> paths; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 153 | addFromFstab(&paths, PathTypes::kMountPoint); |
| 154 | addFromVolumeManager(&paths, PathTypes::kMountPoint); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 155 | |
| 156 | for (const auto& path : paths) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 157 | LOG(DEBUG) << "Starting trim of " << path; |
| 158 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 159 | android::os::PersistableBundle extras; |
| 160 | extras.putString(String16("path"), String16(path.c_str())); |
| 161 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 162 | int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); |
| 163 | if (fd < 0) { |
| 164 | PLOG(WARNING) << "Failed to open " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 165 | if (listener) { |
| 166 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 167 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 168 | continue; |
| 169 | } |
| 170 | |
| 171 | struct fstrim_range range; |
| 172 | memset(&range, 0, sizeof(range)); |
| 173 | range.len = ULLONG_MAX; |
| 174 | |
| 175 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 176 | if (ioctl(fd, FITRIM, &range)) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 177 | PLOG(WARNING) << "Trim failed on " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 178 | if (listener) { |
| 179 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 180 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 181 | } else { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 182 | nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 183 | LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in " |
| 184 | << nanoseconds_to_milliseconds(time) << "ms"; |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 185 | extras.putLong(String16("bytes"), range.len); |
| 186 | extras.putLong(String16("time"), time); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 187 | if (listener) { |
| 188 | listener->onStatus(0, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 189 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 190 | } |
| 191 | close(fd); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 192 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 193 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 194 | if (listener) { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 195 | android::os::PersistableBundle extras; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 196 | listener->onFinished(0, extras); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 201 | static bool waitForGc(const std::list<std::string>& paths) { |
| 202 | std::unique_lock<std::mutex> lk(cv_m, std::defer_lock); |
| 203 | bool stop = false, aborted = false; |
| 204 | Timer timer; |
| 205 | |
| 206 | while (!stop && !aborted) { |
| 207 | stop = true; |
| 208 | for (const auto& path : paths) { |
| 209 | std::string dirty_segments; |
| 210 | if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) { |
| 211 | PLOG(WARNING) << "Reading dirty_segments failed in " << path; |
| 212 | continue; |
| 213 | } |
| 214 | if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) { |
| 215 | stop = false; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (stop) break; |
| 221 | |
| 222 | if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) { |
| 223 | LOG(WARNING) << "GC timeout"; |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | lk.lock(); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 228 | aborted = |
| 229 | cv_abort.wait_for(lk, 10s, [] { return idle_maint_stat == IdleMaintStats::kAbort; }); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 230 | lk.unlock(); |
| 231 | } |
| 232 | |
| 233 | return aborted; |
| 234 | } |
| 235 | |
| 236 | static int startGc(const std::list<std::string>& paths) { |
| 237 | for (const auto& path : paths) { |
| 238 | LOG(DEBUG) << "Start GC on " << path; |
| 239 | if (!WriteStringToFile("1", path + "/gc_urgent")) { |
| 240 | PLOG(WARNING) << "Start GC failed on " << path; |
| 241 | } |
| 242 | } |
| 243 | return android::OK; |
| 244 | } |
| 245 | |
| 246 | static int stopGc(const std::list<std::string>& paths) { |
| 247 | for (const auto& path : paths) { |
| 248 | LOG(DEBUG) << "Stop GC on " << path; |
| 249 | if (!WriteStringToFile("0", path + "/gc_urgent")) { |
| 250 | PLOG(WARNING) << "Stop GC failed on " << path; |
| 251 | } |
| 252 | } |
| 253 | return android::OK; |
| 254 | } |
| 255 | |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 256 | static void runDevGcFstab(void) { |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 257 | std::string path; |
Eric Biggers | 019d516 | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 258 | for (const auto& entry : fstab_default) { |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 259 | if (!entry.sysfs_path.empty()) { |
| 260 | path = entry.sysfs_path; |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 261 | break; |
| 262 | } |
| 263 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 264 | |
| 265 | if (path.empty()) { |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 269 | path = path + "/manual_gc"; |
| 270 | Timer timer; |
| 271 | |
| 272 | LOG(DEBUG) << "Start Dev GC on " << path; |
| 273 | while (1) { |
| 274 | std::string require; |
| 275 | if (!ReadFileToString(path, &require)) { |
| 276 | PLOG(WARNING) << "Reading manual_gc failed in " << path; |
| 277 | break; |
| 278 | } |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 279 | require = android::base::Trim(require); |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 280 | if (require == "" || require == "off" || require == "disabled") { |
| 281 | LOG(DEBUG) << "No more to do Dev GC"; |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | LOG(DEBUG) << "Trigger Dev GC on " << path; |
| 286 | if (!WriteStringToFile("1", path)) { |
| 287 | PLOG(WARNING) << "Start Dev GC failed on " << path; |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) { |
| 292 | LOG(WARNING) << "Dev GC timeout"; |
| 293 | break; |
| 294 | } |
| 295 | sleep(2); |
| 296 | } |
| 297 | LOG(DEBUG) << "Stop Dev GC on " << path; |
| 298 | if (!WriteStringToFile("0", path)) { |
| 299 | PLOG(WARNING) << "Stop Dev GC failed on " << path; |
| 300 | } |
| 301 | return; |
| 302 | } |
| 303 | |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 304 | enum class IDL { HIDL }; |
| 305 | std::ostream& operator<<(std::ostream& os, IDL idl) { |
| 306 | return os << "HIDL"; |
| 307 | } |
| 308 | |
| 309 | template <IDL idl, typename Result> |
| 310 | class GcCallbackImpl { |
| 311 | protected: |
| 312 | void onFinishInternal(Result result) { |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 313 | std::unique_lock<std::mutex> lock(mMutex); |
| 314 | mFinished = true; |
| 315 | mResult = result; |
| 316 | lock.unlock(); |
| 317 | mCv.notify_all(); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 318 | } |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 319 | |
| 320 | public: |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 321 | void wait(uint64_t seconds) { |
| 322 | std::unique_lock<std::mutex> lock(mMutex); |
| 323 | mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; }); |
| 324 | |
| 325 | if (!mFinished) { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 326 | LOG(WARNING) << "Dev GC on " << idl << " HAL timeout"; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 327 | } else if (mResult != Result::SUCCESS) { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 328 | LOG(WARNING) << "Dev GC on " << idl << " HAL failed with " << toString(mResult); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 329 | } else { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 330 | LOG(INFO) << "Dev GC on " << idl << " HAL successful"; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | std::mutex mMutex; |
| 336 | std::condition_variable mCv; |
| 337 | bool mFinished{false}; |
| 338 | Result mResult{Result::UNKNOWN_ERROR}; |
| 339 | }; |
| 340 | |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 341 | class HGcCallbackImpl : public HGarbageCollectCallback, public GcCallbackImpl<IDL::HIDL, HResult> { |
| 342 | Return<void> onFinish(HResult result) override { |
| 343 | onFinishInternal(result); |
| 344 | return Void(); |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | template <IDL idl, typename Service, typename GcCallbackImpl, typename GetDescription> |
| 349 | static void runDevGcOnHal(Service service, GcCallbackImpl cb, GetDescription get_description) { |
| 350 | LOG(DEBUG) << "Start Dev GC on " << idl << " HAL"; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 351 | auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb); |
| 352 | if (!ret.isOk()) { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 353 | LOG(WARNING) << "Cannot start Dev GC on " << idl |
| 354 | << " HAL: " << std::invoke(get_description, ret); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | cb->wait(DEVGC_TIMEOUT_SEC); |
| 358 | } |
| 359 | |
| 360 | static void runDevGc(void) { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 361 | auto hidl_service = HStorage::getService(); |
| 362 | if (hidl_service != nullptr) { |
| 363 | runDevGcOnHal<IDL::HIDL>(hidl_service, sp<HGcCallbackImpl>(new HGcCallbackImpl()), |
| 364 | &Return<void>::description); |
| 365 | return; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 366 | } |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame^] | 367 | // fallback to legacy code path |
| 368 | runDevGcFstab(); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 371 | int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) { |
| 372 | std::unique_lock<std::mutex> lk(cv_m); |
| 373 | if (idle_maint_stat != IdleMaintStats::kStopped) { |
| 374 | LOG(DEBUG) << "idle maintenance is already running"; |
| 375 | if (listener) { |
| 376 | android::os::PersistableBundle extras; |
| 377 | listener->onFinished(0, extras); |
| 378 | } |
| 379 | return android::OK; |
| 380 | } |
| 381 | idle_maint_stat = IdleMaintStats::kRunning; |
| 382 | lk.unlock(); |
| 383 | |
| 384 | LOG(DEBUG) << "idle maintenance started"; |
| 385 | |
Tri Vo | 15bbe22 | 2019-06-21 12:21:48 -0700 | [diff] [blame] | 386 | android::wakelock::WakeLock wl{kWakeLock}; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 387 | |
| 388 | std::list<std::string> paths; |
| 389 | addFromFstab(&paths, PathTypes::kBlkDevice); |
| 390 | addFromVolumeManager(&paths, PathTypes::kBlkDevice); |
| 391 | |
| 392 | startGc(paths); |
| 393 | |
| 394 | bool gc_aborted = waitForGc(paths); |
| 395 | |
| 396 | stopGc(paths); |
| 397 | |
| 398 | lk.lock(); |
| 399 | idle_maint_stat = IdleMaintStats::kStopped; |
| 400 | lk.unlock(); |
| 401 | |
| 402 | cv_stop.notify_one(); |
| 403 | |
| 404 | if (!gc_aborted) { |
| 405 | Trim(nullptr); |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 406 | runDevGc(); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (listener) { |
| 410 | android::os::PersistableBundle extras; |
| 411 | listener->onFinished(0, extras); |
| 412 | } |
| 413 | |
| 414 | LOG(DEBUG) << "idle maintenance completed"; |
| 415 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 416 | return android::OK; |
| 417 | } |
| 418 | |
| 419 | int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) { |
Tri Vo | 15bbe22 | 2019-06-21 12:21:48 -0700 | [diff] [blame] | 420 | android::wakelock::WakeLock wl{kWakeLock}; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 421 | |
| 422 | std::unique_lock<std::mutex> lk(cv_m); |
| 423 | if (idle_maint_stat != IdleMaintStats::kStopped) { |
| 424 | idle_maint_stat = IdleMaintStats::kAbort; |
| 425 | lk.unlock(); |
| 426 | cv_abort.notify_one(); |
| 427 | lk.lock(); |
| 428 | LOG(DEBUG) << "aborting idle maintenance"; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 429 | cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; }); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 430 | } |
| 431 | lk.unlock(); |
| 432 | |
| 433 | if (listener) { |
| 434 | android::os::PersistableBundle extras; |
| 435 | listener->onFinished(0, extras); |
| 436 | } |
| 437 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 438 | LOG(DEBUG) << "idle maintenance stopped"; |
| 439 | |
| 440 | return android::OK; |
| 441 | } |
| 442 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 443 | } // namespace vold |
| 444 | } // namespace android |