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>
26344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
27344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/types.h>
28344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/stat.h>
29344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/types.h>
30344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/mman.h>
31344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/mount.h>
32344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
33344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <linux/kdev_t.h>
34344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <linux/fs.h>
35344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
36344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#define LOG_TAG "Vold"
37344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
38344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <cutils/log.h>
39344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <cutils/properties.h>
40344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
41344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include "Ext4.h"
42344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
43344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#define MKEXT4FS_PATH "/system/bin/make_ext4fs";
44344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
45344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootextern "C" int logwrap(int argc, const char **argv, int background);
46344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
47344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
48344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootint Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount,
49344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        bool executable) {
50344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int rc;
51344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    unsigned long flags;
52344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
53344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
54344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
55344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    flags |= (executable ? 0 : MS_NOEXEC);
56344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    flags |= (ro ? MS_RDONLY : 0);
57344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    flags |= (remount ? MS_REMOUNT : 0);
58344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
59344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
60344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
61344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (rc && errno == EROFS) {
62344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
63344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        flags |= MS_RDONLY;
64344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
65344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
66344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
67344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    return rc;
68344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root}
69344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
70344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootint Ext4::format(const char *fsPath) {
71344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int fd;
72344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    const char *args[4];
73344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int rc;
74344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
75344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    args[0] = MKEXT4FS_PATH;
76344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    args[1] = "-J";
77344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    args[2] = fsPath;
78344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    args[3] = NULL;
79344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    rc = logwrap(3, args, 1);
80344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
81344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (rc == 0) {
82344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGI("Filesystem (ext4) formatted OK");
83344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return 0;
84344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    } else {
85344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Format (ext4) failed (unknown exit code %d)", rc);
86344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        errno = EIO;
87344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
88344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
89344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    return 0;
90344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root}
91