blob: efee8004a8318268fb568a7428076b5e97ab4a98 [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;
Ken Sumrall2c4b5632013-04-29 19:17:56 -070079 } else {
80 SLOGI("Trimmed %llu bytes on %s\n", range.len, fstab->recs[i].mount_point);
Ken Sumrallb87937c2013-03-19 21:46:39 -070081 }
Ken Sumrallb87937c2013-03-19 21:46:39 -070082 close(fd);
83 }
84 SLOGI("Finished fstrim work.\n");
85
Ken Sumrall743a5ec2013-04-22 18:43:28 -070086 return (void *)ret;
Ken Sumrallb87937c2013-03-19 21:46:39 -070087}
88
Ken Sumrall743a5ec2013-04-22 18:43:28 -070089int fstrim_filesystems(void)
90{
91 pthread_t t;
92 int ret;
93
94 /* Depending on the emmc chip and size, this can take upwards
95 * of a few minutes. If done in the same thread as the caller
96 * of this function, that would block vold from accepting any
97 * commands until the trim is finished. So start another thread
98 * to do the work, and return immediately.
99 *
100 * This function should not be called more than once per day, but
101 * even if it is called a second time before the first one finishes,
102 * the kernel will "do the right thing" and split the work between
103 * the two ioctls invoked in separate threads.
104 */
105 ret = pthread_create(&t, NULL, do_fstrim_filesystems, NULL);
106 if (ret) {
107 SLOGE("Cannot create thread to do fstrim");
108 return ret;
109 }
110
111 ret = pthread_detach(t);
112 if (ret) {
113 SLOGE("Cannot detach thread doing fstrim");
114 return ret;
115 }
116
117 return 0;
118}