Home | History | Annotate | Download | only in vold
      1 /*
      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 <time.h>
     26 #include <fs_mgr.h>
     27 #include <pthread.h>
     28 #define LOG_TAG "fstrim"
     29 #include "cutils/log.h"
     30 #include "hardware_legacy/power.h"
     31 
     32 /* 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 
     38 #define FSTRIM_WAKELOCK "dofstrim"
     39 
     40 static 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 
     53 static void *do_fstrim_filesystems(void *ignored)
     54 {
     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 
     64     /* Log the start time in the event log */
     65     LOG_EVENT_LONG(LOG_FSTRIM_START, get_boot_time_ms());
     66 
     67     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     }
    109 
    110     /* Log the finish time in the event log */
    111     LOG_EVENT_LONG(LOG_FSTRIM_FINISH, get_boot_time_ms());
    112 
    113     SLOGI("Finished fstrim work.\n");
    114 
    115     /* Release the wakelock that let us work */
    116     release_wake_lock(FSTRIM_WAKELOCK);
    117 
    118     return (void *)ret;
    119 }
    120 
    121 int fstrim_filesystems(void)
    122 {
    123     pthread_t t;
    124     int ret;
    125 
    126     /* 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 
    131     /* 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 }
    156