blob: 4911778ba253063b1d078dd8627e2fd3bace5a76 [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
Ken Sumrallbc7d5082013-05-07 17:28:21 -070040static 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 Sumrall743a5ec2013-04-22 18:43:28 -070053static void *do_fstrim_filesystems(void *ignored)
Ken Sumrallb87937c2013-03-19 21:46:39 -070054{
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 Sumrallbc7d5082013-05-07 17:28:21 -070064 /* Log the start time in the event log */
65 LOG_EVENT_LONG(LOG_FSTRIM_START, get_boot_time_ms());
66
Ken Sumrallb87937c2013-03-19 21:46:39 -070067 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;
105 }
106 SLOGI("Trimmed %llu bytes on %s\n", range.len, fstab->recs[i].mount_point);
107 close(fd);
108 }
Ken Sumrallbc7d5082013-05-07 17:28:21 -0700109
110 /* Log the finish time in the event log */
111 LOG_EVENT_LONG(LOG_FSTRIM_FINISH, get_boot_time_ms());
112
Ken Sumrallb87937c2013-03-19 21:46:39 -0700113 SLOGI("Finished fstrim work.\n");
114
Ken Sumralle78cd4f2013-05-01 23:34:57 -0700115 /* Release the wakelock that let us work */
116 release_wake_lock(FSTRIM_WAKELOCK);
117
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700118 return (void *)ret;
Ken Sumrallb87937c2013-03-19 21:46:39 -0700119}
120
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700121int fstrim_filesystems(void)
122{
123 pthread_t t;
124 int ret;
125
Ken Sumralle78cd4f2013-05-01 23:34:57 -0700126 /* Get a wakelock as this may take a while, and we don't want the
127 * device to sleep on us.
128 */
129 acquire_wake_lock(PARTIAL_WAKE_LOCK, FSTRIM_WAKELOCK);
130
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700131 /* Depending on the emmc chip and size, this can take upwards
132 * of a few minutes. If done in the same thread as the caller
133 * of this function, that would block vold from accepting any
134 * commands until the trim is finished. So start another thread
135 * to do the work, and return immediately.
136 *
137 * This function should not be called more than once per day, but
138 * even if it is called a second time before the first one finishes,
139 * the kernel will "do the right thing" and split the work between
140 * the two ioctls invoked in separate threads.
141 */
142 ret = pthread_create(&t, NULL, do_fstrim_filesystems, NULL);
143 if (ret) {
144 SLOGE("Cannot create thread to do fstrim");
145 return ret;
146 }
147
148 ret = pthread_detach(t);
149 if (ret) {
150 SLOGE("Cannot detach thread doing fstrim");
151 return ret;
152 }
153
154 return 0;
155}