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; |
JP Abgrall | 0b9489b | 2015-03-16 12:42:57 -0700 | [diff] [blame] | 67 | struct fstab_rec *prev_rec = NULL; |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 68 | |
| 69 | SLOGI("Starting fstrim work...\n"); |
| 70 | |
Young-ho Cha | e72cd59 | 2014-06-20 19:48:36 +0900 | [diff] [blame] | 71 | /* Get a wakelock as this may take a while, and we don't want the |
| 72 | * device to sleep on us. |
| 73 | */ |
| 74 | acquire_wake_lock(PARTIAL_WAKE_LOCK, FSTRIM_WAKELOCK); |
| 75 | |
Ken Sumrall | bc7d508 | 2013-05-07 17:28:21 -0700 | [diff] [blame] | 76 | /* Log the start time in the event log */ |
| 77 | LOG_EVENT_LONG(LOG_FSTRIM_START, get_boot_time_ms()); |
| 78 | |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 79 | for (i = 0; i < fstab->num_entries; i++) { |
| 80 | /* Skip raw partitions */ |
| 81 | if (!strcmp(fstab->recs[i].fs_type, "emmc") || |
| 82 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 83 | continue; |
| 84 | } |
| 85 | /* Skip read-only filesystems */ |
| 86 | if (fstab->recs[i].flags & MS_RDONLY) { |
| 87 | continue; |
| 88 | } |
| 89 | if (fs_mgr_is_voldmanaged(&fstab->recs[i])) { |
| 90 | continue; /* Should we trim fat32 filesystems? */ |
| 91 | } |
JP Abgrall | 6bbb390 | 2015-03-05 17:30:20 -0800 | [diff] [blame] | 92 | if (fs_mgr_is_notrim(&fstab->recs[i])) { |
| 93 | continue; |
| 94 | } |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 95 | |
JP Abgrall | 0b9489b | 2015-03-16 12:42:57 -0700 | [diff] [blame] | 96 | /* Skip the multi-type partitions, which are required to be following each other. |
| 97 | * See fs_mgr.c's mount_with_alternatives(). |
| 98 | */ |
| 99 | if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) { |
| 100 | continue; |
| 101 | } |
| 102 | |
Nick Kralevich | 2475174 | 2015-03-05 12:50:23 -0800 | [diff] [blame] | 103 | 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] | 104 | if (fd < 0) { |
| 105 | SLOGE("Cannot open %s for FITRIM\n", fstab->recs[i].mount_point); |
| 106 | ret = -1; |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | memset(&range, 0, sizeof(range)); |
| 111 | range.len = ULLONG_MAX; |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 112 | 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] | 113 | |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 114 | ret = ioctl(fd, deep_trim ? FIDTRIM : FITRIM, &range); |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 115 | if (ret) { |
JP Abgrall | 55cdafd | 2014-08-07 16:50:44 -0700 | [diff] [blame] | 116 | 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] | 117 | ret = -1; |
Ken Sumrall | 2c4b563 | 2013-04-29 19:17:56 -0700 | [diff] [blame] | 118 | } else { |
| 119 | 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] | 120 | } |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 121 | close(fd); |
JP Abgrall | 0b9489b | 2015-03-16 12:42:57 -0700 | [diff] [blame] | 122 | prev_rec = &fstab->recs[i]; |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 123 | } |
Ken Sumrall | bc7d508 | 2013-05-07 17:28:21 -0700 | [diff] [blame] | 124 | |
| 125 | /* Log the finish time in the event log */ |
| 126 | LOG_EVENT_LONG(LOG_FSTRIM_FINISH, get_boot_time_ms()); |
| 127 | |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 128 | SLOGI("Finished fstrim work.\n"); |
| 129 | |
Ken Sumrall | e78cd4f | 2013-05-01 23:34:57 -0700 | [diff] [blame] | 130 | /* Release the wakelock that let us work */ |
| 131 | release_wake_lock(FSTRIM_WAKELOCK); |
| 132 | |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 133 | return (void *)(uintptr_t)ret; |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 134 | } |
| 135 | |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 136 | int fstrim_filesystems(int deep_trim) |
Ken Sumrall | 743a5ec | 2013-04-22 18:43:28 -0700 | [diff] [blame] | 137 | { |
| 138 | pthread_t t; |
| 139 | int ret; |
| 140 | |
| 141 | /* Depending on the emmc chip and size, this can take upwards |
| 142 | * of a few minutes. If done in the same thread as the caller |
| 143 | * of this function, that would block vold from accepting any |
| 144 | * commands until the trim is finished. So start another thread |
| 145 | * to do the work, and return immediately. |
| 146 | * |
| 147 | * This function should not be called more than once per day, but |
| 148 | * even if it is called a second time before the first one finishes, |
| 149 | * the kernel will "do the right thing" and split the work between |
| 150 | * the two ioctls invoked in separate threads. |
| 151 | */ |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 152 | 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] | 153 | if (ret) { |
| 154 | SLOGE("Cannot create thread to do fstrim"); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | ret = pthread_detach(t); |
| 159 | if (ret) { |
| 160 | SLOGE("Cannot detach thread doing fstrim"); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |