blob: acf177755e847065679f2ecb1133d4c375ae8be4 [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>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080032#include <sys/wait.h>
Kenny Root344ca102012-04-03 17:23:01 -070033
34#include <linux/kdev_t.h>
Kenny Root344ca102012-04-03 17:23:01 -070035
36#define LOG_TAG "Vold"
37
38#include <cutils/log.h>
39#include <cutils/properties.h>
40
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080041#include <logwrap/logwrap.h>
42
Kenny Root344ca102012-04-03 17:23:01 -070043#include "Ext4.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080044#include "VoldUtil.h"
Kenny Root344ca102012-04-03 17:23:01 -070045
46#define MKEXT4FS_PATH "/system/bin/make_ext4fs";
47
Kenny Root344ca102012-04-03 17:23:01 -070048int Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount,
49 bool executable) {
50 int rc;
51 unsigned long flags;
52
53 flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
54
55 flags |= (executable ? 0 : MS_NOEXEC);
56 flags |= (ro ? MS_RDONLY : 0);
57 flags |= (remount ? MS_REMOUNT : 0);
58
59 rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
60
61 if (rc && errno == EROFS) {
62 SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
63 flags |= MS_RDONLY;
64 rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
65 }
66
67 return rc;
68}
69
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070070int Ext4::format(const char *fsPath, unsigned int numSectors, const char *mountpoint) {
Kenny Root344ca102012-04-03 17:23:01 -070071 int fd;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070072 const char *args[7];
Kenny Root344ca102012-04-03 17:23:01 -070073 int rc;
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080074 int status;
Kenny Root344ca102012-04-03 17:23:01 -070075
76 args[0] = MKEXT4FS_PATH;
77 args[1] = "-J";
rpcraiga54e13a2012-09-21 14:17:08 -040078 args[2] = "-a";
79 args[3] = mountpoint;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070080 if (numSectors) {
81 char tmp[32];
82 snprintf(tmp, sizeof(tmp), "%u", numSectors * 512);
83 const char *size = tmp;
84 args[4] = "-l";
85 args[5] = size;
86 args[6] = fsPath;
87 rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status, false, true);
88 } else {
89 args[4] = fsPath;
90 rc = android_fork_execvp(5, (char **)args, &status, false, true);
91 }
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080092 rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status, false,
93 true);
94 if (rc != 0) {
95 SLOGE("Filesystem (ext4) format failed due to logwrap error");
96 errno = EIO;
97 return -1;
98 }
Kenny Root344ca102012-04-03 17:23:01 -070099
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800100 if (!WIFEXITED(status)) {
101 SLOGE("Filesystem (ext4) format did not exit properly");
102 errno = EIO;
103 return -1;
104 }
105
106 status = WEXITSTATUS(status);
107
108 if (status == 0) {
Kenny Root344ca102012-04-03 17:23:01 -0700109 SLOGI("Filesystem (ext4) formatted OK");
110 return 0;
111 } else {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800112 SLOGE("Format (ext4) failed (unknown exit code %d)", status);
Kenny Root344ca102012-04-03 17:23:01 -0700113 errno = EIO;
114 return -1;
115 }
116 return 0;
117}