Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 17 | #include "IdleMaint.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 18 | #include "Utils.h" |
| 19 | #include "VolumeManager.h" |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 20 | #include "model/PrivateVolume.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 21 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 22 | #include <thread> |
| 23 | |
| 24 | #include <android-base/chrono_utils.h> |
| 25 | #include <android-base/file.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
| 27 | #include <android-base/logging.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 28 | #include <fs_mgr.h> |
| 29 | #include <private/android_filesystem_config.h> |
| 30 | #include <hardware_legacy/power.h> |
| 31 | |
| 32 | #include <dirent.h> |
| 33 | #include <sys/mount.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/wait.h> |
| 37 | #include <fcntl.h> |
| 38 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 39 | using android::base::Basename; |
| 40 | using android::base::ReadFileToString; |
| 41 | using android::base::Realpath; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 42 | using android::base::StringPrintf; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 43 | using android::base::Timer; |
| 44 | using android::base::WriteStringToFile; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | namespace vold { |
| 48 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 49 | enum class PathTypes { |
| 50 | kMountPoint = 1, |
| 51 | kBlkDevice, |
| 52 | }; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 53 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 54 | enum class IdleMaintStats { |
| 55 | kStopped = 1, |
| 56 | kRunning, |
| 57 | kAbort, |
| 58 | }; |
| 59 | |
| 60 | static const char* kWakeLock = "IdleMaint"; |
| 61 | static const int DIRTY_SEGMENTS_THRESHOLD = 100; |
| 62 | static const int GC_TIMEOUT_SEC = 480; |
| 63 | |
| 64 | static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped); |
| 65 | static std::condition_variable cv_abort, cv_stop; |
| 66 | static std::mutex cv_m; |
| 67 | |
| 68 | static void addFromVolumeManager(std::list<std::string>* paths, |
| 69 | PathTypes path_type) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 70 | VolumeManager* vm = VolumeManager::Instance(); |
| 71 | std::list<std::string> privateIds; |
| 72 | vm->listVolumes(VolumeBase::Type::kPrivate, privateIds); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 73 | for (const auto& id : privateIds) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 74 | PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get()); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 75 | if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 76 | if (path_type == PathTypes::kMountPoint) { |
| 77 | paths->push_back(vol->getPath()); |
| 78 | } else if (path_type == PathTypes::kBlkDevice) { |
| 79 | std::string gc_path; |
| 80 | const std::string& fs_type = vol->getFsType(); |
| 81 | if (fs_type == "f2fs" && |
| 82 | Realpath(vol->getRawDevPath(), &gc_path)) { |
| 83 | paths->push_back(std::string("/sys/fs/") + fs_type + |
| 84 | "/" + Basename(gc_path)); |
| 85 | } |
| 86 | } |
| 87 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 92 | static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) { |
Bowgo Tsai | e8fb6c3 | 2017-03-09 23:11:33 +0800 | [diff] [blame] | 93 | std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(), |
| 94 | fs_mgr_free_fstab); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 95 | struct fstab_rec *prev_rec = NULL; |
| 96 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 97 | for (int i = 0; i < fstab->num_entries; i++) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 98 | auto fs_type = std::string(fstab->recs[i].fs_type); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 99 | /* Skip raw partitions */ |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 100 | if (fs_type == "emmc" || fs_type == "mtd") { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 101 | continue; |
| 102 | } |
| 103 | /* Skip read-only filesystems */ |
| 104 | if (fstab->recs[i].flags & MS_RDONLY) { |
| 105 | continue; |
| 106 | } |
| 107 | if (fs_mgr_is_voldmanaged(&fstab->recs[i])) { |
| 108 | continue; /* Should we trim fat32 filesystems? */ |
| 109 | } |
| 110 | if (fs_mgr_is_notrim(&fstab->recs[i])) { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | /* Skip the multi-type partitions, which are required to be following each other. |
| 115 | * See fs_mgr.c's mount_with_alternatives(). |
| 116 | */ |
| 117 | if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) { |
| 118 | continue; |
| 119 | } |
| 120 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 121 | if (path_type == PathTypes::kMountPoint) { |
| 122 | paths->push_back(fstab->recs[i].mount_point); |
| 123 | } else if (path_type == PathTypes::kBlkDevice) { |
| 124 | std::string gc_path; |
| 125 | if (std::string(fstab->recs[i].fs_type) == "f2fs" && |
| 126 | Realpath(fstab->recs[i].blk_device, &gc_path)) { |
| 127 | paths->push_back(std::string("/sys/fs/") + fstab->recs[i].fs_type + |
| 128 | "/" + Basename(gc_path)); |
| 129 | } |
| 130 | } |
| 131 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 132 | prev_rec = &fstab->recs[i]; |
| 133 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 136 | void Trim(const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 137 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 138 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 139 | // Collect both fstab and vold volumes |
| 140 | std::list<std::string> paths; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 141 | addFromFstab(&paths, PathTypes::kMountPoint); |
| 142 | addFromVolumeManager(&paths, PathTypes::kMountPoint); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 143 | |
| 144 | for (const auto& path : paths) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 145 | LOG(DEBUG) << "Starting trim of " << path; |
| 146 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 147 | android::os::PersistableBundle extras; |
| 148 | extras.putString(String16("path"), String16(path.c_str())); |
| 149 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 150 | int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); |
| 151 | if (fd < 0) { |
| 152 | PLOG(WARNING) << "Failed to open " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 153 | if (listener) { |
| 154 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 155 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 156 | continue; |
| 157 | } |
| 158 | |
| 159 | struct fstrim_range range; |
| 160 | memset(&range, 0, sizeof(range)); |
| 161 | range.len = ULLONG_MAX; |
| 162 | |
| 163 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 164 | if (ioctl(fd, FITRIM, &range)) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 165 | PLOG(WARNING) << "Trim failed on " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 166 | if (listener) { |
| 167 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 168 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 169 | } else { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 170 | nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 171 | LOG(INFO) << "Trimmed " << range.len << " bytes on " << path |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 172 | << " in " << nanoseconds_to_milliseconds(time) << "ms"; |
| 173 | extras.putLong(String16("bytes"), range.len); |
| 174 | extras.putLong(String16("time"), time); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 175 | if (listener) { |
| 176 | listener->onStatus(0, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 177 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 178 | } |
| 179 | close(fd); |
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 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 182 | if (listener) { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 183 | android::os::PersistableBundle extras; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 184 | listener->onFinished(0, extras); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | release_wake_lock(kWakeLock); |
| 188 | } |
| 189 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame^] | 190 | static bool waitForGc(const std::list<std::string>& paths) { |
| 191 | std::unique_lock<std::mutex> lk(cv_m, std::defer_lock); |
| 192 | bool stop = false, aborted = false; |
| 193 | Timer timer; |
| 194 | |
| 195 | while (!stop && !aborted) { |
| 196 | stop = true; |
| 197 | for (const auto& path : paths) { |
| 198 | std::string dirty_segments; |
| 199 | if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) { |
| 200 | PLOG(WARNING) << "Reading dirty_segments failed in " << path; |
| 201 | continue; |
| 202 | } |
| 203 | if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) { |
| 204 | stop = false; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (stop) break; |
| 210 | |
| 211 | if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) { |
| 212 | LOG(WARNING) << "GC timeout"; |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | lk.lock(); |
| 217 | aborted = cv_abort.wait_for(lk, 10s, []{ |
| 218 | return idle_maint_stat == IdleMaintStats::kAbort;}); |
| 219 | lk.unlock(); |
| 220 | } |
| 221 | |
| 222 | return aborted; |
| 223 | } |
| 224 | |
| 225 | static int startGc(const std::list<std::string>& paths) { |
| 226 | for (const auto& path : paths) { |
| 227 | LOG(DEBUG) << "Start GC on " << path; |
| 228 | if (!WriteStringToFile("1", path + "/gc_urgent")) { |
| 229 | PLOG(WARNING) << "Start GC failed on " << path; |
| 230 | } |
| 231 | } |
| 232 | return android::OK; |
| 233 | } |
| 234 | |
| 235 | static int stopGc(const std::list<std::string>& paths) { |
| 236 | for (const auto& path : paths) { |
| 237 | LOG(DEBUG) << "Stop GC on " << path; |
| 238 | if (!WriteStringToFile("0", path + "/gc_urgent")) { |
| 239 | PLOG(WARNING) << "Stop GC failed on " << path; |
| 240 | } |
| 241 | } |
| 242 | return android::OK; |
| 243 | } |
| 244 | |
| 245 | int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) { |
| 246 | std::unique_lock<std::mutex> lk(cv_m); |
| 247 | if (idle_maint_stat != IdleMaintStats::kStopped) { |
| 248 | LOG(DEBUG) << "idle maintenance is already running"; |
| 249 | if (listener) { |
| 250 | android::os::PersistableBundle extras; |
| 251 | listener->onFinished(0, extras); |
| 252 | } |
| 253 | return android::OK; |
| 254 | } |
| 255 | idle_maint_stat = IdleMaintStats::kRunning; |
| 256 | lk.unlock(); |
| 257 | |
| 258 | LOG(DEBUG) << "idle maintenance started"; |
| 259 | |
| 260 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 261 | |
| 262 | std::list<std::string> paths; |
| 263 | addFromFstab(&paths, PathTypes::kBlkDevice); |
| 264 | addFromVolumeManager(&paths, PathTypes::kBlkDevice); |
| 265 | |
| 266 | startGc(paths); |
| 267 | |
| 268 | bool gc_aborted = waitForGc(paths); |
| 269 | |
| 270 | stopGc(paths); |
| 271 | |
| 272 | lk.lock(); |
| 273 | idle_maint_stat = IdleMaintStats::kStopped; |
| 274 | lk.unlock(); |
| 275 | |
| 276 | cv_stop.notify_one(); |
| 277 | |
| 278 | if (!gc_aborted) { |
| 279 | Trim(nullptr); |
| 280 | } |
| 281 | |
| 282 | if (listener) { |
| 283 | android::os::PersistableBundle extras; |
| 284 | listener->onFinished(0, extras); |
| 285 | } |
| 286 | |
| 287 | LOG(DEBUG) << "idle maintenance completed"; |
| 288 | |
| 289 | release_wake_lock(kWakeLock); |
| 290 | |
| 291 | return android::OK; |
| 292 | } |
| 293 | |
| 294 | int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) { |
| 295 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 296 | |
| 297 | std::unique_lock<std::mutex> lk(cv_m); |
| 298 | if (idle_maint_stat != IdleMaintStats::kStopped) { |
| 299 | idle_maint_stat = IdleMaintStats::kAbort; |
| 300 | lk.unlock(); |
| 301 | cv_abort.notify_one(); |
| 302 | lk.lock(); |
| 303 | LOG(DEBUG) << "aborting idle maintenance"; |
| 304 | cv_stop.wait(lk, []{ |
| 305 | return idle_maint_stat == IdleMaintStats::kStopped;}); |
| 306 | } |
| 307 | lk.unlock(); |
| 308 | |
| 309 | if (listener) { |
| 310 | android::os::PersistableBundle extras; |
| 311 | listener->onFinished(0, extras); |
| 312 | } |
| 313 | |
| 314 | release_wake_lock(kWakeLock); |
| 315 | |
| 316 | LOG(DEBUG) << "idle maintenance stopped"; |
| 317 | |
| 318 | return android::OK; |
| 319 | } |
| 320 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 321 | } // namespace vold |
| 322 | } // namespace android |