Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
Elliott Hughes | 721c3d0 | 2014-12-29 14:09:42 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <unistd.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | #include <string.h> |
| 24 | #include <limits.h> |
| 25 | #include <linux/fs.h> |
Ken Sumrall | bc7d508 | 2013-05-07 17:28:21 -0700 | [diff] [blame] | 26 | #include <time.h> |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 27 | #include <fs_mgr.h> |
Ken Sumrall | 743a5ec | 2013-04-22 18:43:28 -0700 | [diff] [blame] | 28 | #include <pthread.h> |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 29 | #define LOG_TAG "fstrim" |
| 30 | #include "cutils/log.h" |
Ken Sumrall | e78cd4f | 2013-05-01 23:34:57 -0700 | [diff] [blame] | 31 | #include "hardware_legacy/power.h" |
| 32 | |
Ken Sumrall | bc7d508 | 2013-05-07 17:28:21 -0700 | [diff] [blame] | 33 | /* These numbers must match what the MountService specified in |
| 34 | * frameworks/base/services/java/com/android/server/EventLogTags.logtags |
| 35 | */ |
| 36 | #define LOG_FSTRIM_START 2755 |
| 37 | #define LOG_FSTRIM_FINISH 2756 |
| 38 | |
Ken Sumrall | e78cd4f | 2013-05-01 23:34:57 -0700 | [diff] [blame] | 39 | #define FSTRIM_WAKELOCK "dofstrim" |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 40 | |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 41 | #define UNUSED __attribute__((unused)) |
| 42 | |
JP Abgrall | 0cd6cfc | 2014-08-07 18:44:37 -0700 | [diff] [blame] | 43 | /* From a would-be kernel header */ |
| 44 | #define FIDTRIM _IOWR('f', 128, struct fstrim_range) /* Deep discard trim */ |
| 45 | |
Ken Sumrall | bc7d508 | 2013-05-07 17:28:21 -0700 | [diff] [blame] | 46 | static unsigned long long get_boot_time_ms(void) |
| 47 | { |
| 48 | struct timespec t; |
| 49 | unsigned long long time_ms; |
| 50 | |
| 51 | t.tv_sec = 0; |
| 52 | t.tv_nsec = 0; |
| 53 | clock_gettime(CLOCK_BOOTTIME, &t); |
| 54 | time_ms = (t.tv_sec * 1000LL) + (t.tv_nsec / 1000000); |
| 55 | |
| 56 | return time_ms; |
| 57 | } |
| 58 | |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 59 | static void *do_fstrim_filesystems(void *thread_arg) |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 60 | { |
| 61 | int i; |
| 62 | int fd; |
| 63 | int ret = 0; |
| 64 | struct fstrim_range range = { 0 }; |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 65 | extern struct fstab *fstab; |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 66 | int deep_trim = !!thread_arg; |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 67 | |
| 68 | SLOGI("Starting fstrim work...\n"); |
| 69 | |
Young-ho Cha | e72cd59 | 2014-06-20 19:48:36 +0900 | [diff] [blame] | 70 | /* Get a wakelock as this may take a while, and we don't want the |
| 71 | * device to sleep on us. |
| 72 | */ |
| 73 | acquire_wake_lock(PARTIAL_WAKE_LOCK, FSTRIM_WAKELOCK); |
| 74 | |
Ken Sumrall | bc7d508 | 2013-05-07 17:28:21 -0700 | [diff] [blame] | 75 | /* Log the start time in the event log */ |
| 76 | LOG_EVENT_LONG(LOG_FSTRIM_START, get_boot_time_ms()); |
| 77 | |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 78 | for (i = 0; i < fstab->num_entries; i++) { |
| 79 | /* Skip raw partitions */ |
| 80 | if (!strcmp(fstab->recs[i].fs_type, "emmc") || |
| 81 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 82 | continue; |
| 83 | } |
| 84 | /* Skip read-only filesystems */ |
| 85 | if (fstab->recs[i].flags & MS_RDONLY) { |
| 86 | continue; |
| 87 | } |
| 88 | if (fs_mgr_is_voldmanaged(&fstab->recs[i])) { |
| 89 | continue; /* Should we trim fat32 filesystems? */ |
| 90 | } |
| 91 | |
Nick Kralevich | 2475174 | 2015-03-05 12:50:23 -0800 | [diff] [blame] | 92 | fd = open(fstab->recs[i].mount_point, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 93 | if (fd < 0) { |
| 94 | SLOGE("Cannot open %s for FITRIM\n", fstab->recs[i].mount_point); |
| 95 | ret = -1; |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | memset(&range, 0, sizeof(range)); |
| 100 | range.len = ULLONG_MAX; |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 101 | SLOGI("Invoking %s ioctl on %s", deep_trim ? "FIDTRIM" : "FITRIM", fstab->recs[i].mount_point); |
JP Abgrall | 0cd6cfc | 2014-08-07 18:44:37 -0700 | [diff] [blame] | 102 | |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 103 | ret = ioctl(fd, deep_trim ? FIDTRIM : FITRIM, &range); |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 104 | if (ret) { |
JP Abgrall | 55cdafd | 2014-08-07 16:50:44 -0700 | [diff] [blame] | 105 | SLOGE("%s ioctl failed on %s (error %d/%s)", deep_trim ? "FIDTRIM" : "FITRIM", fstab->recs[i].mount_point, errno, strerror(errno)); |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 106 | ret = -1; |
Ken Sumrall | 2c4b563 | 2013-04-29 19:17:56 -0700 | [diff] [blame] | 107 | } else { |
| 108 | SLOGI("Trimmed %llu bytes on %s\n", range.len, fstab->recs[i].mount_point); |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 109 | } |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 110 | close(fd); |
| 111 | } |
Ken Sumrall | bc7d508 | 2013-05-07 17:28:21 -0700 | [diff] [blame] | 112 | |
| 113 | /* Log the finish time in the event log */ |
| 114 | LOG_EVENT_LONG(LOG_FSTRIM_FINISH, get_boot_time_ms()); |
| 115 | |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 116 | SLOGI("Finished fstrim work.\n"); |
| 117 | |
Ken Sumrall | e78cd4f | 2013-05-01 23:34:57 -0700 | [diff] [blame] | 118 | /* Release the wakelock that let us work */ |
| 119 | release_wake_lock(FSTRIM_WAKELOCK); |
| 120 | |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 121 | return (void *)(uintptr_t)ret; |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 122 | } |
| 123 | |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 124 | int fstrim_filesystems(int deep_trim) |
Ken Sumrall | 743a5ec | 2013-04-22 18:43:28 -0700 | [diff] [blame] | 125 | { |
| 126 | pthread_t t; |
| 127 | int ret; |
| 128 | |
| 129 | /* Depending on the emmc chip and size, this can take upwards |
| 130 | * of a few minutes. If done in the same thread as the caller |
| 131 | * of this function, that would block vold from accepting any |
| 132 | * commands until the trim is finished. So start another thread |
| 133 | * to do the work, and return immediately. |
| 134 | * |
| 135 | * This function should not be called more than once per day, but |
| 136 | * even if it is called a second time before the first one finishes, |
| 137 | * the kernel will "do the right thing" and split the work between |
| 138 | * the two ioctls invoked in separate threads. |
| 139 | */ |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 140 | ret = pthread_create(&t, NULL, do_fstrim_filesystems, (void *)(intptr_t)deep_trim); |
Ken Sumrall | 743a5ec | 2013-04-22 18:43:28 -0700 | [diff] [blame] | 141 | if (ret) { |
| 142 | SLOGE("Cannot create thread to do fstrim"); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | ret = pthread_detach(t); |
| 147 | if (ret) { |
| 148 | SLOGE("Cannot detach thread doing fstrim"); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |