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