blob: 613d6233da79b2ac10c92af0481258042ac0ec75 [file] [log] [blame]
Kenny Root344ca102012-04-03 17:23:01 -07001/*
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
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <sys/mman.h>
31#include <sys/mount.h>
32
33#include <linux/kdev_t.h>
Kenny Root344ca102012-04-03 17:23:01 -070034
35#define LOG_TAG "Vold"
36
37#include <cutils/log.h>
38#include <cutils/properties.h>
39
40#include "Ext4.h"
41
42#define MKEXT4FS_PATH "/system/bin/make_ext4fs";
43
44extern "C" int logwrap(int argc, const char **argv, int background);
45
46
47int Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount,
48 bool executable) {
49 int rc;
50 unsigned long flags;
51
52 flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
53
54 flags |= (executable ? 0 : MS_NOEXEC);
55 flags |= (ro ? MS_RDONLY : 0);
56 flags |= (remount ? MS_REMOUNT : 0);
57
58 rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
59
60 if (rc && errno == EROFS) {
61 SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
62 flags |= MS_RDONLY;
63 rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
64 }
65
66 return rc;
67}
68
rpcraiga54e13a2012-09-21 14:17:08 -040069int Ext4::format(const char *fsPath, const char *mountpoint) {
Kenny Root344ca102012-04-03 17:23:01 -070070 int fd;
rpcraiga54e13a2012-09-21 14:17:08 -040071 const char *args[6];
Kenny Root344ca102012-04-03 17:23:01 -070072 int rc;
73
74 args[0] = MKEXT4FS_PATH;
75 args[1] = "-J";
rpcraiga54e13a2012-09-21 14:17:08 -040076 args[2] = "-a";
77 args[3] = mountpoint;
78 args[4] = fsPath;
79 args[5] = NULL;
80 rc = logwrap(5, args, 1);
Kenny Root344ca102012-04-03 17:23:01 -070081
82 if (rc == 0) {
83 SLOGI("Filesystem (ext4) formatted OK");
84 return 0;
85 } else {
86 SLOGE("Format (ext4) failed (unknown exit code %d)", rc);
87 errno = EIO;
88 return -1;
89 }
90 return 0;
91}