Home | History | Annotate | Download | only in fs
      1 /*
      2  * Copyright (C) 2012 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 <stdio.h>
     18 #include <stdlib.h>
     19 #include <fcntl.h>
     20 #include <unistd.h>
     21 #include <errno.h>
     22 #include <string.h>
     23 #include <dirent.h>
     24 #include <errno.h>
     25 #include <fcntl.h>
     26 #include <vector>
     27 #include <string>
     28 
     29 #include <sys/types.h>
     30 #include <sys/stat.h>
     31 #include <sys/types.h>
     32 #include <sys/mman.h>
     33 #include <sys/mount.h>
     34 #include <sys/wait.h>
     35 
     36 #include <linux/kdev_t.h>
     37 
     38 #define LOG_TAG "Vold"
     39 
     40 #include <android-base/logging.h>
     41 #include <android-base/stringprintf.h>
     42 #include <cutils/log.h>
     43 #include <cutils/properties.h>
     44 #include <logwrap/logwrap.h>
     45 #include <selinux/selinux.h>
     46 
     47 #include "Ext4.h"
     48 #include "Utils.h"
     49 #include "VoldUtil.h"
     50 
     51 using android::base::StringPrintf;
     52 
     53 namespace android {
     54 namespace vold {
     55 namespace ext4 {
     56 
     57 static const char* kResizefsPath = "/system/bin/resize2fs";
     58 static const char* kMkfsPath = "/system/bin/make_ext4fs";
     59 static const char* kFsckPath = "/system/bin/e2fsck";
     60 
     61 bool IsSupported() {
     62     return access(kMkfsPath, X_OK) == 0
     63             && access(kFsckPath, X_OK) == 0
     64             && IsFilesystemSupported("ext4");
     65 }
     66 
     67 status_t Check(const std::string& source, const std::string& target) {
     68     // The following is shamelessly borrowed from fs_mgr.c, so it should be
     69     // kept in sync with any changes over there.
     70 
     71     const char* c_source = source.c_str();
     72     const char* c_target = target.c_str();
     73 
     74     int status;
     75     int ret;
     76     long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
     77     char *tmpmnt_opts = (char*) "nomblk_io_submit,errors=remount-ro";
     78 
     79     /*
     80      * First try to mount and unmount the filesystem.  We do this because
     81      * the kernel is more efficient than e2fsck in running the journal and
     82      * processing orphaned inodes, and on at least one device with a
     83      * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
     84      * to do what the kernel does in about a second.
     85      *
     86      * After mounting and unmounting the filesystem, run e2fsck, and if an
     87      * error is recorded in the filesystem superblock, e2fsck will do a full
     88      * check.  Otherwise, it does nothing.  If the kernel cannot mount the
     89      * filesytsem due to an error, e2fsck is still run to do a full check
     90      * fix the filesystem.
     91      */
     92     ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts);
     93     if (!ret) {
     94         int i;
     95         for (i = 0; i < 5; i++) {
     96             // Try to umount 5 times before continuing on.
     97             // Should we try rebooting if all attempts fail?
     98             int result = umount(c_target);
     99             if (result == 0) {
    100                 break;
    101             }
    102             ALOGW("%s(): umount(%s)=%d: %s\n", __func__, c_target, result, strerror(errno));
    103             sleep(1);
    104         }
    105     }
    106 
    107     /*
    108      * Some system images do not have e2fsck for licensing reasons
    109      * (e.g. recent SDK system images). Detect these and skip the check.
    110      */
    111     if (access(kFsckPath, X_OK)) {
    112         ALOGD("Not running %s on %s (executable not in system image)\n",
    113                 kFsckPath, c_source);
    114     } else {
    115         ALOGD("Running %s on %s\n", kFsckPath, c_source);
    116 
    117         std::vector<std::string> cmd;
    118         cmd.push_back(kFsckPath);
    119         cmd.push_back("-y");
    120         cmd.push_back(c_source);
    121 
    122         // ext4 devices are currently always trusted
    123         return ForkExecvp(cmd, sFsckContext);
    124     }
    125 
    126     return 0;
    127 }
    128 
    129 status_t Mount(const std::string& source, const std::string& target, bool ro,
    130         bool remount, bool executable) {
    131     int rc;
    132     unsigned long flags;
    133 
    134     const char* c_source = source.c_str();
    135     const char* c_target = target.c_str();
    136 
    137     flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
    138 
    139     flags |= (executable ? 0 : MS_NOEXEC);
    140     flags |= (ro ? MS_RDONLY : 0);
    141     flags |= (remount ? MS_REMOUNT : 0);
    142 
    143     rc = mount(c_source, c_target, "ext4", flags, NULL);
    144 
    145     if (rc && errno == EROFS) {
    146         SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source);
    147         flags |= MS_RDONLY;
    148         rc = mount(c_source, c_target, "ext4", flags, NULL);
    149     }
    150 
    151     return rc;
    152 }
    153 
    154 status_t Resize(const std::string& source, unsigned long numSectors) {
    155     std::vector<std::string> cmd;
    156     cmd.push_back(kResizefsPath);
    157     cmd.push_back("-f");
    158     cmd.push_back(source);
    159     cmd.push_back(StringPrintf("%lu", numSectors));
    160 
    161     return ForkExecvp(cmd);
    162 }
    163 
    164 status_t Format(const std::string& source, unsigned long numSectors,
    165         const std::string& target) {
    166     std::vector<std::string> cmd;
    167     cmd.push_back(kMkfsPath);
    168     cmd.push_back("-J");
    169 
    170     cmd.push_back("-a");
    171     cmd.push_back(target);
    172 
    173     if (numSectors) {
    174         cmd.push_back("-l");
    175         cmd.push_back(StringPrintf("%lu", numSectors * 512));
    176     }
    177 
    178     // Always generate a real UUID
    179     cmd.push_back("-u");
    180     cmd.push_back(source);
    181 
    182     return ForkExecvp(cmd);
    183 }
    184 
    185 }  // namespace ext4
    186 }  // namespace vold
    187 }  // namespace android
    188