blob: 60c9f24c8f6cf3c865a116d160a49cb1eb8481b1 [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;
JP Abgrall0b9489b2015-03-16 12:42:57 -070067 struct fstab_rec *prev_rec = NULL;
Ken Sumrallb87937c2013-03-19 21:46:39 -070068
69 SLOGI("Starting fstrim work...\n");
70
Young-ho Chae72cd592014-06-20 19:48:36 +090071 /* 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 Sumrallbc7d5082013-05-07 17:28:21 -070076 /* Log the start time in the event log */
77 LOG_EVENT_LONG(LOG_FSTRIM_START, get_boot_time_ms());
78
Ken Sumrallb87937c2013-03-19 21:46:39 -070079 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 Abgrall6bbb3902015-03-05 17:30:20 -080092 if (fs_mgr_is_notrim(&fstab->recs[i])) {
93 continue;
94 }
Ken Sumrallb87937c2013-03-19 21:46:39 -070095
JP Abgrall0b9489b2015-03-16 12:42:57 -070096 /* 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 Kralevich24751742015-03-05 12:50:23 -0800103 fd = open(fstab->recs[i].mount_point, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
Ken Sumrallb87937c2013-03-19 21:46:39 -0700104 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 Abgrall422bdb72014-07-29 13:24:56 -0700112 SLOGI("Invoking %s ioctl on %s", deep_trim ? "FIDTRIM" : "FITRIM", fstab->recs[i].mount_point);
JP Abgrall0cd6cfc2014-08-07 18:44:37 -0700113
JP Abgrall422bdb72014-07-29 13:24:56 -0700114 ret = ioctl(fd, deep_trim ? FIDTRIM : FITRIM, &range);
JP Abgrall422bdb72014-07-29 13:24:56 -0700115 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);
JP Abgrall0b9489b2015-03-16 12:42:57 -0700122 prev_rec = &fstab->recs[i];
Ken Sumrallb87937c2013-03-19 21:46:39 -0700123 }
Ken Sumrallbc7d5082013-05-07 17:28:21 -0700124
125 /* Log the finish time in the event log */
126 LOG_EVENT_LONG(LOG_FSTRIM_FINISH, get_boot_time_ms());
127
Ken Sumrallb87937c2013-03-19 21:46:39 -0700128 SLOGI("Finished fstrim work.\n");
129
Ken Sumralle78cd4f2013-05-01 23:34:57 -0700130 /* Release the wakelock that let us work */
131 release_wake_lock(FSTRIM_WAKELOCK);
132
Colin Cross346c5b22014-01-22 23:59:41 -0800133 return (void *)(uintptr_t)ret;
Ken Sumrallb87937c2013-03-19 21:46:39 -0700134}
135
JP Abgrall422bdb72014-07-29 13:24:56 -0700136int fstrim_filesystems(int deep_trim)
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700137{
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 Abgrall422bdb72014-07-29 13:24:56 -0700152 ret = pthread_create(&t, NULL, do_fstrim_filesystems, (void *)(intptr_t)deep_trim);
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700153 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}