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