blob: 73705f9df9584874faadba625ef411961e4f8a86 [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;
Ken Sumrall2c4b5632013-04-29 19:17:56 -0700105 } else {
106 SLOGI("Trimmed %llu bytes on %s\n", range.len, fstab->recs[i].mount_point);
Ken Sumrallb87937c2013-03-19 21:46:39 -0700107 }
Ken Sumrallb87937c2013-03-19 21:46:39 -0700108 close(fd);
109 }
Ken Sumrallbc7d5082013-05-07 17:28:21 -0700110
111 /* Log the finish time in the event log */
112 LOG_EVENT_LONG(LOG_FSTRIM_FINISH, get_boot_time_ms());
113
Ken Sumrallb87937c2013-03-19 21:46:39 -0700114 SLOGI("Finished fstrim work.\n");
115
Ken Sumralle78cd4f2013-05-01 23:34:57 -0700116 /* Release the wakelock that let us work */
117 release_wake_lock(FSTRIM_WAKELOCK);
118
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700119 return (void *)ret;
Ken Sumrallb87937c2013-03-19 21:46:39 -0700120}
121
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700122int fstrim_filesystems(void)
123{
124 pthread_t t;
125 int ret;
126
Ken Sumralle78cd4f2013-05-01 23:34:57 -0700127 /* 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 Sumrall743a5ec2013-04-22 18:43:28 -0700132 /* 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}