Ext4.cpp revision d0640f6358041f7e2657167560b357078db73526
1344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root/*
2344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * Copyright (C) 2012 The Android Open Source Project
3344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root *
4344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * Licensed under the Apache License, Version 2.0 (the "License");
5344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * you may not use this file except in compliance with the License.
6344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * You may obtain a copy of the License at
7344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root *
8344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root *      http://www.apache.org/licenses/LICENSE-2.0
9344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root *
10344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * Unless required by applicable law or agreed to in writing, software
11344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * distributed under the License is distributed on an "AS IS" BASIS,
12344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * See the License for the specific language governing permissions and
14344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * limitations under the License.
15344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root */
16344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
17344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <stdio.h>
18344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <stdlib.h>
19344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <fcntl.h>
20344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <unistd.h>
21344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <errno.h>
22344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <string.h>
23344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <dirent.h>
24344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <errno.h>
25344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <fcntl.h>
269c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey#include <vector>
279c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey#include <string>
28344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
29344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/types.h>
30344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/stat.h>
31344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/types.h>
32344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/mman.h>
33344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/mount.h>
342ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand#include <sys/wait.h>
35344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
36344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <linux/kdev_t.h>
37344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
38344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#define LOG_TAG "Vold"
39344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
4095c87cce353ca2da79aa57a3a5336b45b6f1d018Jeff Sharkey#include <base/logging.h>
419c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey#include <base/stringprintf.h>
42344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <cutils/log.h>
43344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <cutils/properties.h>
442ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand#include <logwrap/logwrap.h>
4595c87cce353ca2da79aa57a3a5336b45b6f1d018Jeff Sharkey#include <selinux/selinux.h>
462ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand
47344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include "Ext4.h"
489c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey#include "Utils.h"
495593c856f49684f149116a6f2f28ddf9338b8557Rom Lemarchand#include "VoldUtil.h"
50344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
519c48498f4529f623650c56d03e63324c8d813032Jeff Sharkeyusing android::base::StringPrintf;
529c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
53d0640f6358041f7e2657167560b357078db73526Jeff Sharkeynamespace android {
54d0640f6358041f7e2657167560b357078db73526Jeff Sharkeynamespace vold {
55d0640f6358041f7e2657167560b357078db73526Jeff Sharkeynamespace ext4 {
56d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
57ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkeystatic const char* kResizefsPath = "/system/bin/resize2fs";
589c48498f4529f623650c56d03e63324c8d813032Jeff Sharkeystatic const char* kMkfsPath = "/system/bin/make_ext4fs";
599c48498f4529f623650c56d03e63324c8d813032Jeff Sharkeystatic const char* kFsckPath = "/system/bin/e2fsck";
609c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
61d0640f6358041f7e2657167560b357078db73526Jeff Sharkeybool IsSupported() {
62d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    return access(kMkfsPath, X_OK) == 0
63d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            && access(kFsckPath, X_OK) == 0
64d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            && IsFilesystemSupported("ext4");
65d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}
66d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
67d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatus_t Check(const std::string& source, const std::string& target) {
689c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    // The following is shamelessly borrowed from fs_mgr.c, so it should be
699c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    // kept in sync with any changes over there.
709c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
71d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    const char* c_source = source.c_str();
72d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    const char* c_target = target.c_str();
739c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
749c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    int status;
759c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    int ret;
769c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
779c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    char *tmpmnt_opts = (char*) "nomblk_io_submit,errors=remount-ro";
789c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
799c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    /*
809c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * First try to mount and unmount the filesystem.  We do this because
819c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * the kernel is more efficient than e2fsck in running the journal and
829c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * processing orphaned inodes, and on at least one device with a
839c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
849c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * to do what the kernel does in about a second.
859c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     *
869c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * After mounting and unmounting the filesystem, run e2fsck, and if an
879c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * error is recorded in the filesystem superblock, e2fsck will do a full
889c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * check.  Otherwise, it does nothing.  If the kernel cannot mount the
899c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * filesytsem due to an error, e2fsck is still run to do a full check
909c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * fix the filesystem.
919c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     */
92d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts);
939c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    if (!ret) {
949c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey        int i;
959c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey        for (i = 0; i < 5; i++) {
969c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey            // Try to umount 5 times before continuing on.
979c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey            // Should we try rebooting if all attempts fail?
98d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            int result = umount(c_target);
999c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey            if (result == 0) {
1009c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey                break;
1019c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey            }
102d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            ALOGW("%s(): umount(%s)=%d: %s\n", __func__, c_target, result, strerror(errno));
1039c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey            sleep(1);
1049c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey        }
1059c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    }
1069c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
1079c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    /*
1089c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * Some system images do not have e2fsck for licensing reasons
1099c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     * (e.g. recent SDK system images). Detect these and skip the check.
1109c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey     */
1119c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    if (access(kFsckPath, X_OK)) {
1129c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey        ALOGD("Not running %s on %s (executable not in system image)\n",
113d0640f6358041f7e2657167560b357078db73526Jeff Sharkey                kFsckPath, c_source);
1149c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    } else {
115d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        ALOGD("Running %s on %s\n", kFsckPath, c_source);
1169c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
117ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey        std::vector<std::string> cmd;
118ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey        cmd.push_back(kFsckPath);
119ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey        cmd.push_back("-y");
120d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        cmd.push_back(c_source);
1219c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
122d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        // ext4 devices are currently always trusted
123d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        return ForkExecvp(cmd, sFsckContext);
1249c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    }
1259c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
1269c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    return 0;
1279c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey}
1289c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
129d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatus_t Mount(const std::string& source, const std::string& target, bool ro,
130d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        bool remount, bool executable) {
131344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int rc;
132344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    unsigned long flags;
133344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
134d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    const char* c_source = source.c_str();
135d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    const char* c_target = target.c_str();
136d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
137344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
138344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
139344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    flags |= (executable ? 0 : MS_NOEXEC);
140344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    flags |= (ro ? MS_RDONLY : 0);
141344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    flags |= (remount ? MS_REMOUNT : 0);
142344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
143d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    rc = mount(c_source, c_target, "ext4", flags, NULL);
144344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
145344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (rc && errno == EROFS) {
146d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source);
147344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        flags |= MS_RDONLY;
148d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        rc = mount(c_source, c_target, "ext4", flags, NULL);
149344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
150344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
151344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    return rc;
152344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root}
153344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
154d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatus_t Resize(const std::string& source, unsigned int numSectors) {
155ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    std::vector<std::string> cmd;
156ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    cmd.push_back(kResizefsPath);
157ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    cmd.push_back("-f");
158d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    cmd.push_back(source);
159ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    cmd.push_back(StringPrintf("%u", numSectors));
160fcd34a0ddd45db83b7bc71ff47cba9b789089fddDaniel Rosenberg
161d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    return ForkExecvp(cmd);
162fcd34a0ddd45db83b7bc71ff47cba9b789089fddDaniel Rosenberg}
163fcd34a0ddd45db83b7bc71ff47cba9b789089fddDaniel Rosenberg
164d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatus_t Format(const std::string& source, unsigned int numSectors,
165d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        const std::string& target) {
1669c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    std::vector<std::string> cmd;
1679c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    cmd.push_back(kMkfsPath);
1689c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    cmd.push_back("-J");
1699c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
1709c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    cmd.push_back("-a");
171d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    cmd.push_back(target);
1729c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
1736a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg    if (numSectors) {
1749c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey        cmd.push_back("-l");
1759c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey        cmd.push_back(StringPrintf("%u", numSectors * 512));
1766a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg    }
1779c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
178ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    // Always generate a real UUID
179ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    cmd.push_back("-u");
180d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    cmd.push_back(source);
1819c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey
182d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    return ForkExecvp(cmd);
183344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root}
184d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
185d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}  // namespace ext4
186d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}  // namespace vold
187d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}  // namespace android
188