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 | 1251ef0 | 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" |
| 20 | #include "VolumeManager.h" |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 21 | #include "model/PrivateVolume.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 22 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 23 | #include <thread> |
| 24 | |
| 25 | #include <android-base/chrono_utils.h> |
| 26 | #include <android-base/file.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
| 29 | #include <android-base/strings.h> |
Yifan Hong | 91a68df | 2018-09-19 10:28:16 -0700 | [diff] [blame] | 30 | #include <android/hardware/health/storage/1.0/IStorage.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 31 | #include <fs_mgr.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 32 | #include <hardware_legacy/power.h> |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 33 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 34 | |
| 35 | #include <dirent.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 36 | #include <fcntl.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 37 | #include <sys/mount.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/wait.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 41 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 42 | using android::base::Basename; |
| 43 | using android::base::ReadFileToString; |
| 44 | using android::base::Realpath; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 45 | using android::base::StringPrintf; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 46 | using android::base::Timer; |
| 47 | using android::base::WriteStringToFile; |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 48 | using android::fs_mgr::Fstab; |
| 49 | using android::fs_mgr::ReadDefaultFstab; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 50 | using android::hardware::Return; |
| 51 | using android::hardware::Void; |
Yifan Hong | 91a68df | 2018-09-19 10:28:16 -0700 | [diff] [blame] | 52 | using android::hardware::health::storage::V1_0::IGarbageCollectCallback; |
| 53 | using android::hardware::health::storage::V1_0::IStorage; |
| 54 | using 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 | 1251ef0 | 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 | Fstab fstab; |
| 108 | ReadDefaultFstab(&fstab); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 109 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 110 | std::string previous_mount_point; |
| 111 | for (const auto& entry : fstab) { |
| 112 | // Skip raw partitions. |
| 113 | if (entry.fs_type == "emmc" || entry.fs_type == "mtd") { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 114 | continue; |
| 115 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 116 | // Skip read-only filesystems |
| 117 | if (entry.flags & MS_RDONLY) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 118 | continue; |
| 119 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 120 | if (entry.fs_mgr_flags.vold_managed) { |
| 121 | continue; // Should we trim fat32 filesystems? |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 122 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 123 | if (entry.fs_mgr_flags.no_trim) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 124 | continue; |
| 125 | } |
| 126 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 127 | // Skip the multi-type partitions, which are required to be following each other. |
| 128 | // See fs_mgr.c's mount_with_alternatives(). |
| 129 | if (entry.mount_point == previous_mount_point) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 130 | continue; |
| 131 | } |
| 132 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 133 | if (path_type == PathTypes::kMountPoint) { |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 134 | paths->push_back(entry.mount_point); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 135 | } else if (path_type == PathTypes::kBlkDevice) { |
| 136 | std::string gc_path; |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 137 | if (entry.fs_type == "f2fs" && |
| 138 | Realpath(android::vold::BlockDeviceForPath(entry.mount_point + "/"), &gc_path)) { |
| 139 | paths->push_back("/sys/fs/" + entry.fs_type + "/" + Basename(gc_path)); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 143 | previous_mount_point = entry.mount_point; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 144 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 147 | void Trim(const android::sp<android::os::IVoldTaskListener>& listener) { |
Ruslan Tkhakokhov | a37c017 | 2019-06-21 10:31:11 +0000 | [diff] [blame] | 148 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 149 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 150 | // Collect both fstab and vold volumes |
| 151 | std::list<std::string> paths; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 152 | addFromFstab(&paths, PathTypes::kMountPoint); |
| 153 | addFromVolumeManager(&paths, PathTypes::kMountPoint); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 154 | |
| 155 | for (const auto& path : paths) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 156 | LOG(DEBUG) << "Starting trim of " << path; |
| 157 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 158 | android::os::PersistableBundle extras; |
| 159 | extras.putString(String16("path"), String16(path.c_str())); |
| 160 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 161 | int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); |
| 162 | if (fd < 0) { |
| 163 | PLOG(WARNING) << "Failed to open " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 164 | if (listener) { |
| 165 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 166 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 167 | continue; |
| 168 | } |
| 169 | |
| 170 | struct fstrim_range range; |
| 171 | memset(&range, 0, sizeof(range)); |
| 172 | range.len = ULLONG_MAX; |
| 173 | |
| 174 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 175 | if (ioctl(fd, FITRIM, &range)) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 176 | PLOG(WARNING) << "Trim failed on " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 177 | if (listener) { |
| 178 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 179 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 180 | } else { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 181 | nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 182 | LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in " |
| 183 | << nanoseconds_to_milliseconds(time) << "ms"; |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 184 | extras.putLong(String16("bytes"), range.len); |
| 185 | extras.putLong(String16("time"), time); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 186 | if (listener) { |
| 187 | listener->onStatus(0, 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 | } |
| 190 | close(fd); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 191 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 192 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 193 | if (listener) { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 194 | android::os::PersistableBundle extras; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 195 | listener->onFinished(0, extras); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Ruslan Tkhakokhov | a37c017 | 2019-06-21 10:31:11 +0000 | [diff] [blame] | 198 | release_wake_lock(kWakeLock); |
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 | Fstab fstab; |
| 258 | ReadDefaultFstab(&fstab); |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 259 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 260 | std::string path; |
| 261 | for (const auto& entry : fstab) { |
| 262 | if (!entry.sysfs_path.empty()) { |
| 263 | path = entry.sysfs_path; |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 267 | |
| 268 | if (path.empty()) { |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 272 | path = path + "/manual_gc"; |
| 273 | Timer timer; |
| 274 | |
| 275 | LOG(DEBUG) << "Start Dev GC on " << path; |
| 276 | while (1) { |
| 277 | std::string require; |
| 278 | if (!ReadFileToString(path, &require)) { |
| 279 | PLOG(WARNING) << "Reading manual_gc failed in " << path; |
| 280 | break; |
| 281 | } |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 282 | require = android::base::Trim(require); |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 283 | if (require == "" || require == "off" || require == "disabled") { |
| 284 | LOG(DEBUG) << "No more to do Dev GC"; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | LOG(DEBUG) << "Trigger Dev GC on " << path; |
| 289 | if (!WriteStringToFile("1", path)) { |
| 290 | PLOG(WARNING) << "Start Dev GC failed on " << path; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) { |
| 295 | LOG(WARNING) << "Dev GC timeout"; |
| 296 | break; |
| 297 | } |
| 298 | sleep(2); |
| 299 | } |
| 300 | LOG(DEBUG) << "Stop Dev GC on " << path; |
| 301 | if (!WriteStringToFile("0", path)) { |
| 302 | PLOG(WARNING) << "Stop Dev GC failed on " << path; |
| 303 | } |
| 304 | return; |
| 305 | } |
| 306 | |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 307 | class GcCallback : public IGarbageCollectCallback { |
| 308 | public: |
| 309 | Return<void> onFinish(Result result) override { |
| 310 | std::unique_lock<std::mutex> lock(mMutex); |
| 311 | mFinished = true; |
| 312 | mResult = result; |
| 313 | lock.unlock(); |
| 314 | mCv.notify_all(); |
| 315 | return Void(); |
| 316 | } |
| 317 | void wait(uint64_t seconds) { |
| 318 | std::unique_lock<std::mutex> lock(mMutex); |
| 319 | mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; }); |
| 320 | |
| 321 | if (!mFinished) { |
| 322 | LOG(WARNING) << "Dev GC on HAL timeout"; |
| 323 | } else if (mResult != Result::SUCCESS) { |
| 324 | LOG(WARNING) << "Dev GC on HAL failed with " << toString(mResult); |
| 325 | } else { |
| 326 | LOG(INFO) << "Dev GC on HAL successful"; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | private: |
| 331 | std::mutex mMutex; |
| 332 | std::condition_variable mCv; |
| 333 | bool mFinished{false}; |
| 334 | Result mResult{Result::UNKNOWN_ERROR}; |
| 335 | }; |
| 336 | |
Yifan Hong | 91a68df | 2018-09-19 10:28:16 -0700 | [diff] [blame] | 337 | static void runDevGcOnHal(sp<IStorage> service) { |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 338 | LOG(DEBUG) << "Start Dev GC on HAL"; |
| 339 | sp<GcCallback> cb = new GcCallback(); |
| 340 | auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb); |
| 341 | if (!ret.isOk()) { |
| 342 | LOG(WARNING) << "Cannot start Dev GC on HAL: " << ret.description(); |
| 343 | return; |
| 344 | } |
| 345 | cb->wait(DEVGC_TIMEOUT_SEC); |
| 346 | } |
| 347 | |
| 348 | static void runDevGc(void) { |
Yifan Hong | 91a68df | 2018-09-19 10:28:16 -0700 | [diff] [blame] | 349 | auto service = IStorage::getService(); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 350 | if (service != nullptr) { |
| 351 | runDevGcOnHal(service); |
| 352 | } else { |
| 353 | // fallback to legacy code path |
| 354 | runDevGcFstab(); |
| 355 | } |
| 356 | } |
| 357 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 358 | int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) { |
| 359 | std::unique_lock<std::mutex> lk(cv_m); |
| 360 | if (idle_maint_stat != IdleMaintStats::kStopped) { |
| 361 | LOG(DEBUG) << "idle maintenance is already running"; |
| 362 | if (listener) { |
| 363 | android::os::PersistableBundle extras; |
| 364 | listener->onFinished(0, extras); |
| 365 | } |
| 366 | return android::OK; |
| 367 | } |
| 368 | idle_maint_stat = IdleMaintStats::kRunning; |
| 369 | lk.unlock(); |
| 370 | |
| 371 | LOG(DEBUG) << "idle maintenance started"; |
| 372 | |
Ruslan Tkhakokhov | a37c017 | 2019-06-21 10:31:11 +0000 | [diff] [blame] | 373 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 374 | |
| 375 | std::list<std::string> paths; |
| 376 | addFromFstab(&paths, PathTypes::kBlkDevice); |
| 377 | addFromVolumeManager(&paths, PathTypes::kBlkDevice); |
| 378 | |
| 379 | startGc(paths); |
| 380 | |
| 381 | bool gc_aborted = waitForGc(paths); |
| 382 | |
| 383 | stopGc(paths); |
| 384 | |
| 385 | lk.lock(); |
| 386 | idle_maint_stat = IdleMaintStats::kStopped; |
| 387 | lk.unlock(); |
| 388 | |
| 389 | cv_stop.notify_one(); |
| 390 | |
| 391 | if (!gc_aborted) { |
| 392 | Trim(nullptr); |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 393 | runDevGc(); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | if (listener) { |
| 397 | android::os::PersistableBundle extras; |
| 398 | listener->onFinished(0, extras); |
| 399 | } |
| 400 | |
| 401 | LOG(DEBUG) << "idle maintenance completed"; |
| 402 | |
Ruslan Tkhakokhov | a37c017 | 2019-06-21 10:31:11 +0000 | [diff] [blame] | 403 | release_wake_lock(kWakeLock); |
| 404 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 405 | return android::OK; |
| 406 | } |
| 407 | |
| 408 | int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) { |
Ruslan Tkhakokhov | a37c017 | 2019-06-21 10:31:11 +0000 | [diff] [blame] | 409 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 410 | |
| 411 | std::unique_lock<std::mutex> lk(cv_m); |
| 412 | if (idle_maint_stat != IdleMaintStats::kStopped) { |
| 413 | idle_maint_stat = IdleMaintStats::kAbort; |
| 414 | lk.unlock(); |
| 415 | cv_abort.notify_one(); |
| 416 | lk.lock(); |
| 417 | LOG(DEBUG) << "aborting idle maintenance"; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 418 | cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; }); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 419 | } |
| 420 | lk.unlock(); |
| 421 | |
| 422 | if (listener) { |
| 423 | android::os::PersistableBundle extras; |
| 424 | listener->onFinished(0, extras); |
| 425 | } |
| 426 | |
Ruslan Tkhakokhov | a37c017 | 2019-06-21 10:31:11 +0000 | [diff] [blame] | 427 | release_wake_lock(kWakeLock); |
| 428 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 429 | LOG(DEBUG) << "idle maintenance stopped"; |
| 430 | |
| 431 | return android::OK; |
| 432 | } |
| 433 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 434 | } // namespace vold |
| 435 | } // namespace android |