blob: 2c24fc9b4cc0a5cb470b2cbb2a5d6dc84f702be8 [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>
Elliott Hughes721c3d02014-12-29 14:09:42 -080019#include <errno.h>
Ken Sumrallb87937c2013-03-19 21:46:39 -070020#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 Sumrallbc7d5082013-05-07 17:28:21 -070026#include <time.h>
Ken Sumrallb87937c2013-03-19 21:46:39 -070027#include <fs_mgr.h>
Ken Sumrall743a5ec2013-04-22 18:43:28 -070028#include <pthread.h>
Ken Sumrallb87937c2013-03-19 21:46:39 -070029#define LOG_TAG "fstrim"
30#include "cutils/log.h"
Ken Sumralle78cd4f2013-05-01 23:34:57 -070031#include "hardware_legacy/power.h"
32
Ken Sumrallbc7d5082013-05-07 17:28:21 -070033/* 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 Sumralle78cd4f2013-05-01 23:34:57 -070039#define FSTRIM_WAKELOCK "dofstrim"
Ken Sumrallb87937c2013-03-19 21:46:39 -070040
Mark Salyzyn3e971272014-01-21 13:27:04 -080041#define UNUSED __attribute__((unused))
42
JP Abgrall0cd6cfc2014-08-07 18:44:37 -070043/* From a would-be kernel header */
44#define FIDTRIM _IOWR('f', 128, struct fstrim_range) /* Deep discard trim */
45
Ken Sumrallbc7d5082013-05-07 17:28:21 -070046static 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 Abgrall422bdb72014-07-29 13:24:56 -070059static void *do_fstrim_filesystems(void *thread_arg)
Ken Sumrallb87937c2013-03-19 21:46:39 -070060{
61 int i;
62 int fd;
63 int ret = 0;
64 struct fstrim_range range = { 0 };
Ken Sumrallb87937c2013-03-19 21:46:39 -070065 extern struct fstab *fstab;
JP Abgrall422bdb72014-07-29 13:24:56 -070066 int deep_trim = !!thread_arg;
Ken Sumrallb87937c2013-03-19 21:46:39 -070067
68 SLOGI("Starting fstrim work...\n");
69
Young-ho Chae72cd592014-06-20 19:48:36 +090070 /* 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 Sumrallbc7d5082013-05-07 17:28:21 -070075 /* Log the start time in the event log */
76 LOG_EVENT_LONG(LOG_FSTRIM_START, get_boot_time_ms());
77
Ken Sumrallb87937c2013-03-19 21:46:39 -070078 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 Kralevich24751742015-03-05 12:50:23 -080092 fd = open(fstab->recs[i].mount_point, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
Ken Sumrallb87937c2013-03-19 21:46:39 -070093 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 Abgrall422bdb72014-07-29 13:24:56 -0700101 SLOGI("Invoking %s ioctl on %s", deep_trim ? "FIDTRIM" : "FITRIM", fstab->recs[i].mount_point);
JP Abgrall0cd6cfc2014-08-07 18:44:37 -0700102
JP Abgrall422bdb72014-07-29 13:24:56 -0700103 ret = ioctl(fd, deep_trim ? FIDTRIM : FITRIM, &range);
JP Abgrall422bdb72014-07-29 13:24:56 -0700104 if (ret) {
JP Abgrall55cdafd2014-08-07 16:50:44 -0700105 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 -0700106 ret = -1;
Ken Sumrall2c4b5632013-04-29 19:17:56 -0700107 } else {
108 SLOGI("Trimmed %llu bytes on %s\n", range.len, fstab->recs[i].mount_point);
Ken Sumrallb87937c2013-03-19 21:46:39 -0700109 }
Ken Sumrallb87937c2013-03-19 21:46:39 -0700110 close(fd);
111 }
Ken Sumrallbc7d5082013-05-07 17:28:21 -0700112
113 /* Log the finish time in the event log */
114 LOG_EVENT_LONG(LOG_FSTRIM_FINISH, get_boot_time_ms());
115
Ken Sumrallb87937c2013-03-19 21:46:39 -0700116 SLOGI("Finished fstrim work.\n");
117
Ken Sumralle78cd4f2013-05-01 23:34:57 -0700118 /* Release the wakelock that let us work */
119 release_wake_lock(FSTRIM_WAKELOCK);
120
Colin Cross346c5b22014-01-22 23:59:41 -0800121 return (void *)(uintptr_t)ret;
Ken Sumrallb87937c2013-03-19 21:46:39 -0700122}
123
JP Abgrall422bdb72014-07-29 13:24:56 -0700124int fstrim_filesystems(int deep_trim)
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700125{
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 Abgrall422bdb72014-07-29 13:24:56 -0700140 ret = pthread_create(&t, NULL, do_fstrim_filesystems, (void *)(intptr_t)deep_trim);
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700141 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}