blob: 58fd15e9b726ec66f86c6f31b87663ac1e228453 [file] [log] [blame]
Ken Sumrallb87937c2013-03-19 21:46:39 -07001/*
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 Sumrallbc7d5082013-05-07 17:28:21 -070025#include <time.h>
Ken Sumrallb87937c2013-03-19 21:46:39 -070026#include <fs_mgr.h>
Ken Sumrall743a5ec2013-04-22 18:43:28 -070027#include <pthread.h>
Ken Sumrallb87937c2013-03-19 21:46:39 -070028#define LOG_TAG "fstrim"
29#include "cutils/log.h"
Ken Sumralle78cd4f2013-05-01 23:34:57 -070030#include "hardware_legacy/power.h"
31
Ken Sumrallbc7d5082013-05-07 17:28:21 -070032/* 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 Sumralle78cd4f2013-05-01 23:34:57 -070038#define FSTRIM_WAKELOCK "dofstrim"
Ken Sumrallb87937c2013-03-19 21:46:39 -070039
Mark Salyzyn3e971272014-01-21 13:27:04 -080040#define UNUSED __attribute__((unused))
41
Ken Sumrallbc7d5082013-05-07 17:28:21 -070042static unsigned long long get_boot_time_ms(void)
43{
44 struct timespec t;
45 unsigned long long time_ms;
46
47 t.tv_sec = 0;
48 t.tv_nsec = 0;
49 clock_gettime(CLOCK_BOOTTIME, &t);
50 time_ms = (t.tv_sec * 1000LL) + (t.tv_nsec / 1000000);
51
52 return time_ms;
53}
54
JP Abgrall422bdb72014-07-29 13:24:56 -070055static void *do_fstrim_filesystems(void *thread_arg)
Ken Sumrallb87937c2013-03-19 21:46:39 -070056{
57 int i;
58 int fd;
59 int ret = 0;
60 struct fstrim_range range = { 0 };
61 struct stat sb;
62 extern struct fstab *fstab;
JP Abgrall422bdb72014-07-29 13:24:56 -070063 int deep_trim = !!thread_arg;
Ken Sumrallb87937c2013-03-19 21:46:39 -070064
65 SLOGI("Starting fstrim work...\n");
66
Ken Sumrallbc7d5082013-05-07 17:28:21 -070067 /* Log the start time in the event log */
68 LOG_EVENT_LONG(LOG_FSTRIM_START, get_boot_time_ms());
69
Ken Sumrallb87937c2013-03-19 21:46:39 -070070 for (i = 0; i < fstab->num_entries; i++) {
71 /* Skip raw partitions */
72 if (!strcmp(fstab->recs[i].fs_type, "emmc") ||
73 !strcmp(fstab->recs[i].fs_type, "mtd")) {
74 continue;
75 }
76 /* Skip read-only filesystems */
77 if (fstab->recs[i].flags & MS_RDONLY) {
78 continue;
79 }
80 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
81 continue; /* Should we trim fat32 filesystems? */
82 }
83
84 if (stat(fstab->recs[i].mount_point, &sb) == -1) {
85 SLOGE("Cannot stat mount point %s\n", fstab->recs[i].mount_point);
86 ret = -1;
87 continue;
88 }
89 if (!S_ISDIR(sb.st_mode)) {
90 SLOGE("%s is not a directory\n", fstab->recs[i].mount_point);
91 ret = -1;
92 continue;
93 }
94
95 fd = open(fstab->recs[i].mount_point, O_RDONLY);
96 if (fd < 0) {
97 SLOGE("Cannot open %s for FITRIM\n", fstab->recs[i].mount_point);
98 ret = -1;
99 continue;
100 }
101
102 memset(&range, 0, sizeof(range));
103 range.len = ULLONG_MAX;
JP Abgrall422bdb72014-07-29 13:24:56 -0700104 SLOGI("Invoking %s ioctl on %s", deep_trim ? "FIDTRIM" : "FITRIM", fstab->recs[i].mount_point);
105#if defined(FIDTRIM)
106 ret = ioctl(fd, deep_trim ? FIDTRIM : FITRIM, &range);
107#else
108 if (deep_trim) {
109 ret = -1;
110 errno = EINVAL;
111 } else {
112 ret = ioctl(fd, FITRIM, &range);
113 }
114#endif
115 if (ret) {
JP Abgrall55cdafd2014-08-07 16:50:44 -0700116 SLOGE("%s ioctl failed on %s (error %d/%s)", deep_trim ? "FIDTRIM" : "FITRIM", fstab->recs[i].mount_point, errno, strerror(errno));
Ken Sumrallb87937c2013-03-19 21:46:39 -0700117 ret = -1;
Ken Sumrall2c4b5632013-04-29 19:17:56 -0700118 } else {
119 SLOGI("Trimmed %llu bytes on %s\n", range.len, fstab->recs[i].mount_point);
Ken Sumrallb87937c2013-03-19 21:46:39 -0700120 }
Ken Sumrallb87937c2013-03-19 21:46:39 -0700121 close(fd);
122 }
Ken Sumrallbc7d5082013-05-07 17:28:21 -0700123
124 /* Log the finish time in the event log */
125 LOG_EVENT_LONG(LOG_FSTRIM_FINISH, get_boot_time_ms());
126
Ken Sumrallb87937c2013-03-19 21:46:39 -0700127 SLOGI("Finished fstrim work.\n");
128
Ken Sumralle78cd4f2013-05-01 23:34:57 -0700129 /* Release the wakelock that let us work */
130 release_wake_lock(FSTRIM_WAKELOCK);
131
Colin Cross346c5b22014-01-22 23:59:41 -0800132 return (void *)(uintptr_t)ret;
Ken Sumrallb87937c2013-03-19 21:46:39 -0700133}
134
JP Abgrall422bdb72014-07-29 13:24:56 -0700135int fstrim_filesystems(int deep_trim)
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700136{
137 pthread_t t;
138 int ret;
139
Ken Sumralle78cd4f2013-05-01 23:34:57 -0700140 /* Get a wakelock as this may take a while, and we don't want the
141 * device to sleep on us.
142 */
143 acquire_wake_lock(PARTIAL_WAKE_LOCK, FSTRIM_WAKELOCK);
144
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700145 /* Depending on the emmc chip and size, this can take upwards
146 * of a few minutes. If done in the same thread as the caller
147 * of this function, that would block vold from accepting any
148 * commands until the trim is finished. So start another thread
149 * to do the work, and return immediately.
150 *
151 * This function should not be called more than once per day, but
152 * even if it is called a second time before the first one finishes,
153 * the kernel will "do the right thing" and split the work between
154 * the two ioctls invoked in separate threads.
155 */
JP Abgrall422bdb72014-07-29 13:24:56 -0700156 ret = pthread_create(&t, NULL, do_fstrim_filesystems, (void *)(intptr_t)deep_trim);
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700157 if (ret) {
158 SLOGE("Cannot create thread to do fstrim");
159 return ret;
160 }
161
162 ret = pthread_detach(t);
163 if (ret) {
164 SLOGE("Cannot detach thread doing fstrim");
165 return ret;
166 }
167
168 return 0;
169}