blob: 42d6cd390591136f86acbb4d6daa1dab33b8d99b [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>
Jeff Sharkey9c484982015-03-31 10:35:33 -070026#include <vector>
27#include <string>
Kenny Root344ca102012-04-03 17:23:01 -070028
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>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080034#include <sys/wait.h>
Kenny Root344ca102012-04-03 17:23:01 -070035
36#include <linux/kdev_t.h>
Kenny Root344ca102012-04-03 17:23:01 -070037
38#define LOG_TAG "Vold"
39
Jeff Sharkey9c484982015-03-31 10:35:33 -070040#include <base/stringprintf.h>
Kenny Root344ca102012-04-03 17:23:01 -070041#include <cutils/log.h>
42#include <cutils/properties.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080043#include <logwrap/logwrap.h>
44
Kenny Root344ca102012-04-03 17:23:01 -070045#include "Ext4.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070046#include "Utils.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080047#include "VoldUtil.h"
Kenny Root344ca102012-04-03 17:23:01 -070048
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070049#define RESIZE2FS_PATH "/system/bin/resize2fs"
Kenny Root344ca102012-04-03 17:23:01 -070050
Jeff Sharkey9c484982015-03-31 10:35:33 -070051using android::base::StringPrintf;
52
53static const char* kMkfsPath = "/system/bin/make_ext4fs";
54
55static const char* kFsckPath = "/system/bin/e2fsck";
56static const char* kFsckLogFile = "/dev/fscklogs/log";
57
58int Ext4::check(const char *fsPath, const char *mountPoint) {
59 // The following is shamelessly borrowed from fs_mgr.c, so it should be
60 // kept in sync with any changes over there.
61
62 char* blk_device = (char*) fsPath;
63 char* target = (char*) mountPoint;
64
65 int status;
66 int ret;
67 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
68 char *tmpmnt_opts = (char*) "nomblk_io_submit,errors=remount-ro";
69 char *e2fsck_argv[] = {
70 (char*) kFsckPath,
71 (char*) "-y",
72 blk_device
73 };
74
75 /*
76 * First try to mount and unmount the filesystem. We do this because
77 * the kernel is more efficient than e2fsck in running the journal and
78 * processing orphaned inodes, and on at least one device with a
79 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
80 * to do what the kernel does in about a second.
81 *
82 * After mounting and unmounting the filesystem, run e2fsck, and if an
83 * error is recorded in the filesystem superblock, e2fsck will do a full
84 * check. Otherwise, it does nothing. If the kernel cannot mount the
85 * filesytsem due to an error, e2fsck is still run to do a full check
86 * fix the filesystem.
87 */
88 ret = mount(blk_device, target, "ext4", tmpmnt_flags, tmpmnt_opts);
89 if (!ret) {
90 int i;
91 for (i = 0; i < 5; i++) {
92 // Try to umount 5 times before continuing on.
93 // Should we try rebooting if all attempts fail?
94 int result = umount(target);
95 if (result == 0) {
96 break;
97 }
98 ALOGW("%s(): umount(%s)=%d: %s\n", __func__, target, result, strerror(errno));
99 sleep(1);
100 }
101 }
102
103 /*
104 * Some system images do not have e2fsck for licensing reasons
105 * (e.g. recent SDK system images). Detect these and skip the check.
106 */
107 if (access(kFsckPath, X_OK)) {
108 ALOGD("Not running %s on %s (executable not in system image)\n",
109 kFsckPath, blk_device);
110 } else {
111 ALOGD("Running %s on %s\n", kFsckPath, blk_device);
112
113 ret = android_fork_execvp(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
114 &status, false, true);
115
116 if (ret < 0) {
117 /* No need to check for error in fork, we can't really handle it now */
118 ALOGW("Failed trying to run %s\n", kFsckPath);
119 return -1;
120 }
121 }
122
123 return 0;
124}
125
Kenny Root344ca102012-04-03 17:23:01 -0700126int Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount,
127 bool executable) {
128 int rc;
129 unsigned long flags;
130
131 flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
132
133 flags |= (executable ? 0 : MS_NOEXEC);
134 flags |= (ro ? MS_RDONLY : 0);
135 flags |= (remount ? MS_REMOUNT : 0);
136
137 rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
138
139 if (rc && errno == EROFS) {
140 SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
141 flags |= MS_RDONLY;
142 rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
143 }
144
145 return rc;
146}
147
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700148int Ext4::resize(const char *fspath, unsigned int numSectors) {
149 const char *args[4];
150 char* size_str;
151 int rc;
152 int status;
153
154 args[0] = RESIZE2FS_PATH;
155 args[1] = "-f";
156 args[2] = fspath;
157 if (asprintf(&size_str, "%ds", numSectors) < 0)
158 {
159 SLOGE("Filesystem (ext4) resize failed to set size");
160 return -1;
161 }
162 args[3] = size_str;
163 rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status, false,
164 true);
165 free(size_str);
166 if (rc != 0) {
167 SLOGE("Filesystem (ext4) resize failed due to logwrap error");
168 errno = EIO;
169 return -1;
170 }
171
172 if (!WIFEXITED(status)) {
173 SLOGE("Filesystem (ext4) resize did not exit properly");
174 errno = EIO;
175 return -1;
176 }
177
178 status = WEXITSTATUS(status);
179
180 if (status == 0) {
181 SLOGI("Filesystem (ext4) resized OK");
182 return 0;
183 } else {
184 SLOGE("Resize (ext4) failed (unknown exit code %d)", status);
185 errno = EIO;
186 return -1;
187 }
188 return 0;
189}
190
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700191int Ext4::format(const char *fsPath, unsigned int numSectors, const char *mountpoint) {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800192 int status;
Kenny Root344ca102012-04-03 17:23:01 -0700193
Jeff Sharkey9c484982015-03-31 10:35:33 -0700194 std::vector<std::string> cmd;
195 cmd.push_back(kMkfsPath);
196 cmd.push_back("-J");
197
198 cmd.push_back("-a");
199 cmd.push_back(mountpoint);
200
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700201 if (numSectors) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700202 cmd.push_back("-l");
203 cmd.push_back(StringPrintf("%u", numSectors * 512));
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700204 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700205
206 cmd.push_back(fsPath);
207
208 int rc = android::vold::ForkExecvp(cmd, &status, false, true);
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800209 if (rc != 0) {
210 SLOGE("Filesystem (ext4) format failed due to logwrap error");
211 errno = EIO;
212 return -1;
213 }
Kenny Root344ca102012-04-03 17:23:01 -0700214
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800215 if (!WIFEXITED(status)) {
216 SLOGE("Filesystem (ext4) format did not exit properly");
217 errno = EIO;
218 return -1;
219 }
220
221 status = WEXITSTATUS(status);
222
223 if (status == 0) {
Kenny Root344ca102012-04-03 17:23:01 -0700224 SLOGI("Filesystem (ext4) formatted OK");
225 return 0;
226 } else {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800227 SLOGE("Format (ext4) failed (unknown exit code %d)", status);
Kenny Root344ca102012-04-03 17:23:01 -0700228 errno = EIO;
229 return -1;
230 }
231 return 0;
232}