blob: 872db204d0e4e8a05e3f85aefd43eda5648fc475 [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"
Jani Lusikkaf95af8c2016-01-15 22:25:47 +020039#include <private/android_filesystem_config.h>
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 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
Jeff Sharkey9c484982015-03-31 10:35:33 -070051using android::base::StringPrintf;
52
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070053namespace android {
54namespace vold {
55namespace ext4 {
56
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070057static const char* kResizefsPath = "/system/bin/resize2fs";
Adrien Schildknechte0f409c2016-11-21 15:14:37 -080058#ifdef TARGET_USES_MKE2FS
59static const char* kMkfsPath = "/system/bin/mke2fs";
60#else
Jeff Sharkey9c484982015-03-31 10:35:33 -070061static const char* kMkfsPath = "/system/bin/make_ext4fs";
Adrien Schildknechte0f409c2016-11-21 15:14:37 -080062#endif
Jeff Sharkey9c484982015-03-31 10:35:33 -070063static const char* kFsckPath = "/system/bin/e2fsck";
Jeff Sharkey9c484982015-03-31 10:35:33 -070064
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070065bool IsSupported() {
66 return access(kMkfsPath, X_OK) == 0
67 && access(kFsckPath, X_OK) == 0
68 && IsFilesystemSupported("ext4");
69}
70
Michael Bestas7e0bc982015-12-06 23:53:55 +020071status_t Check(const std::string& source, const std::string& target, bool trusted) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070072 // The following is shamelessly borrowed from fs_mgr.c, so it should be
73 // kept in sync with any changes over there.
74
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070075 const char* c_source = source.c_str();
76 const char* c_target = target.c_str();
Jeff Sharkey9c484982015-03-31 10:35:33 -070077
78 int status;
79 int ret;
80 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
81 char *tmpmnt_opts = (char*) "nomblk_io_submit,errors=remount-ro";
Jeff Sharkey9c484982015-03-31 10:35:33 -070082
83 /*
84 * First try to mount and unmount the filesystem. We do this because
85 * the kernel is more efficient than e2fsck in running the journal and
86 * processing orphaned inodes, and on at least one device with a
87 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
88 * to do what the kernel does in about a second.
89 *
90 * After mounting and unmounting the filesystem, run e2fsck, and if an
91 * error is recorded in the filesystem superblock, e2fsck will do a full
92 * check. Otherwise, it does nothing. If the kernel cannot mount the
93 * filesytsem due to an error, e2fsck is still run to do a full check
94 * fix the filesystem.
95 */
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070096 ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts);
Jeff Sharkey9c484982015-03-31 10:35:33 -070097 if (!ret) {
98 int i;
99 for (i = 0; i < 5; i++) {
100 // Try to umount 5 times before continuing on.
101 // Should we try rebooting if all attempts fail?
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700102 int result = umount(c_target);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700103 if (result == 0) {
104 break;
105 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700106 ALOGW("%s(): umount(%s)=%d: %s\n", __func__, c_target, result, strerror(errno));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700107 sleep(1);
108 }
109 }
110
111 /*
112 * Some system images do not have e2fsck for licensing reasons
113 * (e.g. recent SDK system images). Detect these and skip the check.
114 */
115 if (access(kFsckPath, X_OK)) {
116 ALOGD("Not running %s on %s (executable not in system image)\n",
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700117 kFsckPath, c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700118 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700119 ALOGD("Running %s on %s\n", kFsckPath, c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700120
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700121 std::vector<std::string> cmd;
122 cmd.push_back(kFsckPath);
123 cmd.push_back("-y");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700124 cmd.push_back(c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700125
Michael Bestas7e0bc982015-12-06 23:53:55 +0200126 return ForkExecvp(cmd, trusted ? sFsckContext : sFsckUntrustedContext);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700127 }
128
129 return 0;
130}
131
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700132status_t Mount(const std::string& source, const std::string& target, bool ro,
Sam Mortimer11a9e062015-11-27 15:27:03 -0800133 bool remount, bool executable, const std::string& opts /* = "" */,
134 bool trusted, bool portable) {
Kenny Root344ca102012-04-03 17:23:01 -0700135 int rc;
136 unsigned long flags;
137
Jani Lusikkaf95af8c2016-01-15 22:25:47 +0200138 std::string data(opts);
139
140 if (portable) {
141 if (!data.empty()) {
142 data += ",";
143 }
144 data += "context=u:object_r:sdcard_posix:s0";
145 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700146 const char* c_source = source.c_str();
147 const char* c_target = target.c_str();
Jani Lusikkaf95af8c2016-01-15 22:25:47 +0200148 const char* c_data = data.c_str();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700149
Sam Mortimer11a9e062015-11-27 15:27:03 -0800150 flags = MS_NOATIME | MS_NODEV | MS_NOSUID;
151
152 // Only use MS_DIRSYNC if we're not mounting adopted storage
153 if (!trusted) {
154 flags |= MS_DIRSYNC;
155 }
Kenny Root344ca102012-04-03 17:23:01 -0700156
157 flags |= (executable ? 0 : MS_NOEXEC);
158 flags |= (ro ? MS_RDONLY : 0);
159 flags |= (remount ? MS_REMOUNT : 0);
160
Jani Lusikkaf95af8c2016-01-15 22:25:47 +0200161 rc = mount(c_source, c_target, "ext4", flags, c_data);
162 if (portable && rc == 0) {
163 chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW);
Jani Lusikka63a4f9c2016-03-25 20:12:15 +0200164 chmod(c_target, 0775);
Jani Lusikkaf95af8c2016-01-15 22:25:47 +0200165 }
Kenny Root344ca102012-04-03 17:23:01 -0700166
167 if (rc && errno == EROFS) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700168 SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source);
Kenny Root344ca102012-04-03 17:23:01 -0700169 flags |= MS_RDONLY;
Jani Lusikkaf95af8c2016-01-15 22:25:47 +0200170 rc = mount(c_source, c_target, "ext4", flags, c_data);
Kenny Root344ca102012-04-03 17:23:01 -0700171 }
172
173 return rc;
174}
175
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200176status_t Resize(const std::string& source, unsigned long numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700177 std::vector<std::string> cmd;
178 cmd.push_back(kResizefsPath);
179 cmd.push_back("-f");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700180 cmd.push_back(source);
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200181 cmd.push_back(StringPrintf("%lu", numSectors));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700182
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700183 return ForkExecvp(cmd);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700184}
185
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200186status_t Format(const std::string& source, unsigned long numSectors,
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700187 const std::string& target) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700188 std::vector<std::string> cmd;
189 cmd.push_back(kMkfsPath);
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800190
191#ifdef TARGET_USES_MKE2FS
192 cmd.push_back("-b");
193 cmd.push_back("4096");
194
195 cmd.push_back("-t");
196 cmd.push_back("ext4");
197
198 cmd.push_back("-M");
199 cmd.push_back(target);
200
201 cmd.push_back("-O");
202 cmd.push_back("^has_journal");
203
204 cmd.push_back(source);
205
206 if (numSectors)
207 cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512)));
208#else
Jeff Sharkey9c484982015-03-31 10:35:33 -0700209 cmd.push_back("-J");
210
211 cmd.push_back("-a");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700212 cmd.push_back(target);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700213
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700214 if (numSectors) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700215 cmd.push_back("-l");
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200216 cmd.push_back(StringPrintf("%lu", numSectors * 512));
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700217 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700218
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700219 // Always generate a real UUID
220 cmd.push_back("-u");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700221 cmd.push_back(source);
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800222#endif
Jeff Sharkey9c484982015-03-31 10:35:33 -0700223
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700224 return ForkExecvp(cmd);
Kenny Root344ca102012-04-03 17:23:01 -0700225}
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700226
227} // namespace ext4
228} // namespace vold
229} // namespace android