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