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