blob: 1ffd3129e37bb29a6804192ff2a3ca883460fcac [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>
25#include <fs_mgr.h>
Ken Sumrall743a5ec2013-04-22 18:43:28 -070026#include <pthread.h>
Ken Sumrallb87937c2013-03-19 21:46:39 -070027#define LOG_TAG "fstrim"
28#include "cutils/log.h"
29
Ken Sumrall743a5ec2013-04-22 18:43:28 -070030static void *do_fstrim_filesystems(void *ignored)
Ken Sumrallb87937c2013-03-19 21:46:39 -070031{
32 int i;
33 int fd;
34 int ret = 0;
35 struct fstrim_range range = { 0 };
36 struct stat sb;
37 extern struct fstab *fstab;
38
39 SLOGI("Starting fstrim work...\n");
40
41 for (i = 0; i < fstab->num_entries; i++) {
42 /* Skip raw partitions */
43 if (!strcmp(fstab->recs[i].fs_type, "emmc") ||
44 !strcmp(fstab->recs[i].fs_type, "mtd")) {
45 continue;
46 }
47 /* Skip read-only filesystems */
48 if (fstab->recs[i].flags & MS_RDONLY) {
49 continue;
50 }
51 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
52 continue; /* Should we trim fat32 filesystems? */
53 }
54
55 if (stat(fstab->recs[i].mount_point, &sb) == -1) {
56 SLOGE("Cannot stat mount point %s\n", fstab->recs[i].mount_point);
57 ret = -1;
58 continue;
59 }
60 if (!S_ISDIR(sb.st_mode)) {
61 SLOGE("%s is not a directory\n", fstab->recs[i].mount_point);
62 ret = -1;
63 continue;
64 }
65
66 fd = open(fstab->recs[i].mount_point, O_RDONLY);
67 if (fd < 0) {
68 SLOGE("Cannot open %s for FITRIM\n", fstab->recs[i].mount_point);
69 ret = -1;
70 continue;
71 }
72
73 memset(&range, 0, sizeof(range));
74 range.len = ULLONG_MAX;
75 SLOGI("Invoking FITRIM ioctl on %s", fstab->recs[i].mount_point);
76 if (ioctl(fd, FITRIM, &range)) {
77 SLOGE("FITRIM ioctl failed on %s", fstab->recs[i].mount_point);
78 ret = -1;
79 }
80 SLOGI("Trimmed %llu bytes on %s\n", range.len, fstab->recs[i].mount_point);
81 close(fd);
82 }
83 SLOGI("Finished fstrim work.\n");
84
Ken Sumrall743a5ec2013-04-22 18:43:28 -070085 return (void *)ret;
Ken Sumrallb87937c2013-03-19 21:46:39 -070086}
87
Ken Sumrall743a5ec2013-04-22 18:43:28 -070088int fstrim_filesystems(void)
89{
90 pthread_t t;
91 int ret;
92
93 /* Depending on the emmc chip and size, this can take upwards
94 * of a few minutes. If done in the same thread as the caller
95 * of this function, that would block vold from accepting any
96 * commands until the trim is finished. So start another thread
97 * to do the work, and return immediately.
98 *
99 * This function should not be called more than once per day, but
100 * even if it is called a second time before the first one finishes,
101 * the kernel will "do the right thing" and split the work between
102 * the two ioctls invoked in separate threads.
103 */
104 ret = pthread_create(&t, NULL, do_fstrim_filesystems, NULL);
105 if (ret) {
106 SLOGE("Cannot create thread to do fstrim");
107 return ret;
108 }
109
110 ret = pthread_detach(t);
111 if (ret) {
112 SLOGE("Cannot detach thread doing fstrim");
113 return ret;
114 }
115
116 return 0;
117}