blob: adb8f2ee17ceb92551cb977e3b88d499f9d7e62e [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
Elliott Hughes7e128fb2015-12-04 15:50:53 -080040#include <android-base/logging.h>
41#include <android-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 Sharkey46bb69f2017-06-21 13:52:23 -060048#include "Ext4Crypt.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070049#include "Utils.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080050#include "VoldUtil.h"
Kenny Root344ca102012-04-03 17:23:01 -070051
Jeff Sharkey9c484982015-03-31 10:35:33 -070052using android::base::StringPrintf;
53
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070054namespace android {
55namespace vold {
56namespace ext4 {
57
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070058static const char* kResizefsPath = "/system/bin/resize2fs";
Adrien Schildknechte0f409c2016-11-21 15:14:37 -080059static const char* kMkfsPath = "/system/bin/mke2fs";
Jeff Sharkey9c484982015-03-31 10:35:33 -070060static const char* kFsckPath = "/system/bin/e2fsck";
Jeff Sharkey9c484982015-03-31 10:35:33 -070061
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070062bool IsSupported() {
63 return access(kMkfsPath, X_OK) == 0
64 && access(kFsckPath, X_OK) == 0
65 && IsFilesystemSupported("ext4");
66}
67
68status_t Check(const std::string& source, const std::string& target) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070069 // The following is shamelessly borrowed from fs_mgr.c, so it should be
70 // kept in sync with any changes over there.
71
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070072 const char* c_source = source.c_str();
73 const char* c_target = target.c_str();
Jeff Sharkey9c484982015-03-31 10:35:33 -070074
75 int status;
76 int ret;
77 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
78 char *tmpmnt_opts = (char*) "nomblk_io_submit,errors=remount-ro";
Jeff Sharkey9c484982015-03-31 10:35:33 -070079
80 /*
81 * First try to mount and unmount the filesystem. We do this because
82 * the kernel is more efficient than e2fsck in running the journal and
83 * processing orphaned inodes, and on at least one device with a
84 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
85 * to do what the kernel does in about a second.
86 *
87 * After mounting and unmounting the filesystem, run e2fsck, and if an
88 * error is recorded in the filesystem superblock, e2fsck will do a full
89 * check. Otherwise, it does nothing. If the kernel cannot mount the
90 * filesytsem due to an error, e2fsck is still run to do a full check
91 * fix the filesystem.
92 */
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070093 ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts);
Jeff Sharkey9c484982015-03-31 10:35:33 -070094 if (!ret) {
95 int i;
96 for (i = 0; i < 5; i++) {
97 // Try to umount 5 times before continuing on.
98 // Should we try rebooting if all attempts fail?
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070099 int result = umount(c_target);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700100 if (result == 0) {
101 break;
102 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700103 ALOGW("%s(): umount(%s)=%d: %s\n", __func__, c_target, result, strerror(errno));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700104 sleep(1);
105 }
106 }
107
108 /*
109 * Some system images do not have e2fsck for licensing reasons
110 * (e.g. recent SDK system images). Detect these and skip the check.
111 */
112 if (access(kFsckPath, X_OK)) {
113 ALOGD("Not running %s on %s (executable not in system image)\n",
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700114 kFsckPath, c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700115 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700116 ALOGD("Running %s on %s\n", kFsckPath, c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700117
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700118 std::vector<std::string> cmd;
119 cmd.push_back(kFsckPath);
120 cmd.push_back("-y");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700121 cmd.push_back(c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700122
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700123 // ext4 devices are currently always trusted
124 return ForkExecvp(cmd, sFsckContext);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700125 }
126
127 return 0;
128}
129
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700130status_t Mount(const std::string& source, const std::string& target, bool ro,
131 bool remount, bool executable) {
Kenny Root344ca102012-04-03 17:23:01 -0700132 int rc;
133 unsigned long flags;
134
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700135 const char* c_source = source.c_str();
136 const char* c_target = target.c_str();
137
Kenny Root344ca102012-04-03 17:23:01 -0700138 flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
139
140 flags |= (executable ? 0 : MS_NOEXEC);
141 flags |= (ro ? MS_RDONLY : 0);
142 flags |= (remount ? MS_REMOUNT : 0);
143
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700144 rc = mount(c_source, c_target, "ext4", flags, NULL);
Kenny Root344ca102012-04-03 17:23:01 -0700145
146 if (rc && errno == EROFS) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700147 SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source);
Kenny Root344ca102012-04-03 17:23:01 -0700148 flags |= MS_RDONLY;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700149 rc = mount(c_source, c_target, "ext4", flags, NULL);
Kenny Root344ca102012-04-03 17:23:01 -0700150 }
151
152 return rc;
153}
154
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200155status_t Resize(const std::string& source, unsigned long numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700156 std::vector<std::string> cmd;
157 cmd.push_back(kResizefsPath);
158 cmd.push_back("-f");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700159 cmd.push_back(source);
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200160 cmd.push_back(StringPrintf("%lu", numSectors));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700161
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700162 return ForkExecvp(cmd);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700163}
164
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200165status_t Format(const std::string& source, unsigned long numSectors,
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700166 const std::string& target) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700167 std::vector<std::string> cmd;
168 cmd.push_back(kMkfsPath);
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800169
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800170 cmd.push_back("-b");
171 cmd.push_back("4096");
172
173 cmd.push_back("-t");
174 cmd.push_back("ext4");
175
176 cmd.push_back("-M");
177 cmd.push_back(target);
178
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600179 std::string options("has_journal,quota");
180 if (e4crypt_is_native()) {
181 options += ",encrypt";
182 }
183
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800184 cmd.push_back("-O");
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600185 cmd.push_back(options);
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800186
187 cmd.push_back(source);
188
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700189 if (numSectors) {
Jeff Sharkeyd7945262017-06-26 16:09:11 -0600190 cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512)));
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700191 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700193 return ForkExecvp(cmd);
Kenny Root344ca102012-04-03 17:23:01 -0700194}
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700195
196} // namespace ext4
197} // namespace vold
198} // namespace android