blob: a9f5f0a1fb3852c92f9c2172146775804081a215 [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"
Ken Sumralle78cd4f2013-05-01 23:34:57 -070029#include "hardware_legacy/power.h"
30
31#define FSTRIM_WAKELOCK "dofstrim"
Ken Sumrallb87937c2013-03-19 21:46:39 -070032
Ken Sumrall743a5ec2013-04-22 18:43:28 -070033static void *do_fstrim_filesystems(void *ignored)
Ken Sumrallb87937c2013-03-19 21:46:39 -070034{
35 int i;
36 int fd;
37 int ret = 0;
38 struct fstrim_range range = { 0 };
39 struct stat sb;
40 extern struct fstab *fstab;
41
42 SLOGI("Starting fstrim work...\n");
43
44 for (i = 0; i < fstab->num_entries; i++) {
45 /* Skip raw partitions */
46 if (!strcmp(fstab->recs[i].fs_type, "emmc") ||
47 !strcmp(fstab->recs[i].fs_type, "mtd")) {
48 continue;
49 }
50 /* Skip read-only filesystems */
51 if (fstab->recs[i].flags & MS_RDONLY) {
52 continue;
53 }
54 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
55 continue; /* Should we trim fat32 filesystems? */
56 }
57
58 if (stat(fstab->recs[i].mount_point, &sb) == -1) {
59 SLOGE("Cannot stat mount point %s\n", fstab->recs[i].mount_point);
60 ret = -1;
61 continue;
62 }
63 if (!S_ISDIR(sb.st_mode)) {
64 SLOGE("%s is not a directory\n", fstab->recs[i].mount_point);
65 ret = -1;
66 continue;
67 }
68
69 fd = open(fstab->recs[i].mount_point, O_RDONLY);
70 if (fd < 0) {
71 SLOGE("Cannot open %s for FITRIM\n", fstab->recs[i].mount_point);
72 ret = -1;
73 continue;
74 }
75
76 memset(&range, 0, sizeof(range));
77 range.len = ULLONG_MAX;
78 SLOGI("Invoking FITRIM ioctl on %s", fstab->recs[i].mount_point);
79 if (ioctl(fd, FITRIM, &range)) {
80 SLOGE("FITRIM ioctl failed on %s", fstab->recs[i].mount_point);
81 ret = -1;
82 }
83 SLOGI("Trimmed %llu bytes on %s\n", range.len, fstab->recs[i].mount_point);
84 close(fd);
85 }
86 SLOGI("Finished fstrim work.\n");
87
Ken Sumralle78cd4f2013-05-01 23:34:57 -070088 /* Release the wakelock that let us work */
89 release_wake_lock(FSTRIM_WAKELOCK);
90
Ken Sumrall743a5ec2013-04-22 18:43:28 -070091 return (void *)ret;
Ken Sumrallb87937c2013-03-19 21:46:39 -070092}
93
Ken Sumrall743a5ec2013-04-22 18:43:28 -070094int fstrim_filesystems(void)
95{
96 pthread_t t;
97 int ret;
98
Ken Sumralle78cd4f2013-05-01 23:34:57 -070099 /* Get a wakelock as this may take a while, and we don't want the
100 * device to sleep on us.
101 */
102 acquire_wake_lock(PARTIAL_WAKE_LOCK, FSTRIM_WAKELOCK);
103
Ken Sumrall743a5ec2013-04-22 18:43:28 -0700104 /* Depending on the emmc chip and size, this can take upwards
105 * of a few minutes. If done in the same thread as the caller
106 * of this function, that would block vold from accepting any
107 * commands until the trim is finished. So start another thread
108 * to do the work, and return immediately.
109 *
110 * This function should not be called more than once per day, but
111 * even if it is called a second time before the first one finishes,
112 * the kernel will "do the right thing" and split the work between
113 * the two ioctls invoked in separate threads.
114 */
115 ret = pthread_create(&t, NULL, do_fstrim_filesystems, NULL);
116 if (ret) {
117 SLOGE("Cannot create thread to do fstrim");
118 return ret;
119 }
120
121 ret = pthread_detach(t);
122 if (ret) {
123 SLOGE("Cannot detach thread doing fstrim");
124 return ret;
125 }
126
127 return 0;
128}