blob: 14ac05621f29f5c0b21bd580ce2a3416b01bb9dc [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
Kenny Root344ca102012-04-03 17:23:01 -070017#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070024#include <string>
Paul Crowley14c8c072018-09-18 13:30:21 -070025#include <vector>
Kenny Root344ca102012-04-03 17:23:01 -070026
Kenny Root344ca102012-04-03 17:23:01 -070027#include <sys/mman.h>
28#include <sys/mount.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070029#include <sys/stat.h>
30#include <sys/types.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080031#include <sys/wait.h>
Kenny Root344ca102012-04-03 17:23:01 -070032
33#include <linux/kdev_t.h>
Kenny Root344ca102012-04-03 17:23:01 -070034
Elliott Hughes7e128fb2015-12-04 15:50:53 -080035#include <android-base/logging.h>
Jeff Sharkey95a92f92017-07-17 13:57:18 -060036#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080037#include <android-base/stringprintf.h>
Kenny Root344ca102012-04-03 17:23:01 -070038#include <cutils/properties.h>
Eric Biggersa701c452018-10-23 13:06:55 -070039#include <fscrypt/fscrypt.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080040#include <logwrap/logwrap.h>
Jani Lusikkac8d267d2016-01-15 22:25:47 +020041#include <private/android_filesystem_config.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070042#include <selinux/selinux.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080043
Kenny Root344ca102012-04-03 17:23:01 -070044#include "Ext4.h"
Eric Biggersa701c452018-10-23 13:06:55 -070045#include "FsCrypt.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
Jeff Sharkey9c484982015-03-31 10:35:33 -070049using android::base::StringPrintf;
50
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070051namespace android {
52namespace vold {
53namespace ext4 {
54
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070055static const char* kResizefsPath = "/system/bin/resize2fs";
Adrien Schildknechte0f409c2016-11-21 15:14:37 -080056static const char* kMkfsPath = "/system/bin/mke2fs";
Jeff Sharkey9c484982015-03-31 10:35:33 -070057static const char* kFsckPath = "/system/bin/e2fsck";
Jeff Sharkey9c484982015-03-31 10:35:33 -070058
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070059bool IsSupported() {
Paul Crowley14c8c072018-09-18 13:30:21 -070060 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
61 IsFilesystemSupported("ext4");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070062}
63
Michael Bestas86918bf2015-12-06 23:53:55 +020064status_t Check(const std::string& source, const std::string& target, bool trusted) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070065 // The following is shamelessly borrowed from fs_mgr.c, so it should be
66 // kept in sync with any changes over there.
67
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070068 const char* c_source = source.c_str();
69 const char* c_target = target.c_str();
Jeff Sharkey9c484982015-03-31 10:35:33 -070070
71 int status;
72 int ret;
73 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
Paul Crowley14c8c072018-09-18 13:30:21 -070074 char* tmpmnt_opts = (char*)"nomblk_io_submit,errors=remount-ro";
Jeff Sharkey9c484982015-03-31 10:35:33 -070075
76 /*
77 * First try to mount and unmount the filesystem. We do this because
78 * the kernel is more efficient than e2fsck in running the journal and
79 * processing orphaned inodes, and on at least one device with a
80 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
81 * to do what the kernel does in about a second.
82 *
83 * After mounting and unmounting the filesystem, run e2fsck, and if an
84 * error is recorded in the filesystem superblock, e2fsck will do a full
85 * check. Otherwise, it does nothing. If the kernel cannot mount the
86 * filesytsem due to an error, e2fsck is still run to do a full check
87 * fix the filesystem.
88 */
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070089 ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts);
Jeff Sharkey9c484982015-03-31 10:35:33 -070090 if (!ret) {
91 int i;
92 for (i = 0; i < 5; i++) {
93 // Try to umount 5 times before continuing on.
94 // Should we try rebooting if all attempts fail?
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070095 int result = umount(c_target);
Jeff Sharkey9c484982015-03-31 10:35:33 -070096 if (result == 0) {
97 break;
98 }
Logan Chien188b0ab2018-04-23 13:37:39 +080099 LOG(WARNING) << __func__ << "(): umount(" << c_target << ")=" << result << ": "
100 << strerror(errno);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700101 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)) {
Logan Chien188b0ab2018-04-23 13:37:39 +0800110 LOG(DEBUG) << "Not running " << kFsckPath << " on " << c_source
111 << " (executable not in system image)";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700112 } else {
Logan Chien188b0ab2018-04-23 13:37:39 +0800113 LOG(DEBUG) << "Running " << kFsckPath << " on " << c_source;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700114
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700115 std::vector<std::string> cmd;
116 cmd.push_back(kFsckPath);
117 cmd.push_back("-y");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700118 cmd.push_back(c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700119
Michael Bestas86918bf2015-12-06 23:53:55 +0200120 return ForkExecvp(cmd, nullptr, trusted ? sFsckContext : sFsckUntrustedContext);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700121 }
122
123 return 0;
124}
125
Paul Crowley14c8c072018-09-18 13:30:21 -0700126status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
Sam Mortimer09aef342015-11-27 15:27:03 -0800127 bool executable, const std::string& opts /* = "" */, bool trusted, bool portable) {
Kenny Root344ca102012-04-03 17:23:01 -0700128 int rc;
129 unsigned long flags;
130
Jani Lusikkac8d267d2016-01-15 22:25:47 +0200131 std::string data(opts);
132
133 if (portable) {
134 if (!data.empty()) {
135 data += ",";
136 }
137 data += "context=u:object_r:sdcard_posix:s0";
138 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700139 const char* c_source = source.c_str();
140 const char* c_target = target.c_str();
Jani Lusikkac8d267d2016-01-15 22:25:47 +0200141 const char* c_data = data.c_str();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700142
Sam Mortimer09aef342015-11-27 15:27:03 -0800143 flags = MS_NOATIME | MS_NODEV | MS_NOSUID;
144
145 // Only use MS_DIRSYNC if we're not mounting adopted storage
146 if (!trusted) {
147 flags |= MS_DIRSYNC;
148 }
Kenny Root344ca102012-04-03 17:23:01 -0700149
150 flags |= (executable ? 0 : MS_NOEXEC);
151 flags |= (ro ? MS_RDONLY : 0);
152 flags |= (remount ? MS_REMOUNT : 0);
153
Jani Lusikkac8d267d2016-01-15 22:25:47 +0200154 rc = mount(c_source, c_target, "ext4", flags, c_data);
155 if (portable && rc == 0) {
156 chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW);
Jani Lusikkabf60c5f2016-03-25 20:12:15 +0200157 chmod(c_target, 0775);
Jani Lusikkac8d267d2016-01-15 22:25:47 +0200158 }
Kenny Root344ca102012-04-03 17:23:01 -0700159
160 if (rc && errno == EROFS) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600161 LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
Kenny Root344ca102012-04-03 17:23:01 -0700162 flags |= MS_RDONLY;
Jani Lusikkac8d267d2016-01-15 22:25:47 +0200163 rc = mount(c_source, c_target, "ext4", flags, c_data);
Kenny Root344ca102012-04-03 17:23:01 -0700164 }
165
166 return rc;
167}
168
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200169status_t Resize(const std::string& source, unsigned long numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700170 std::vector<std::string> cmd;
171 cmd.push_back(kResizefsPath);
172 cmd.push_back("-f");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700173 cmd.push_back(source);
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200174 cmd.push_back(StringPrintf("%lu", numSectors));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700175
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700176 return ForkExecvp(cmd);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700177}
178
Paul Crowley14c8c072018-09-18 13:30:21 -0700179status_t Format(const std::string& source, unsigned long numSectors, const std::string& target) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700180 std::vector<std::string> cmd;
181 cmd.push_back(kMkfsPath);
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800182
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800183 cmd.push_back("-b");
184 cmd.push_back("4096");
185
186 cmd.push_back("-t");
187 cmd.push_back("ext4");
188
189 cmd.push_back("-M");
190 cmd.push_back(target);
191
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600192 std::string options("has_journal");
193 if (android::base::GetBoolProperty("vold.has_quota", false)) {
194 options += ",quota";
195 }
Eric Biggersa701c452018-10-23 13:06:55 -0700196 if (fscrypt_is_native()) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600197 options += ",encrypt";
198 }
199
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800200 cmd.push_back("-O");
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600201 cmd.push_back(options);
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800202
203 cmd.push_back(source);
204
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700205 if (numSectors) {
Jeff Sharkeyd7945262017-06-26 16:09:11 -0600206 cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512)));
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700207 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700208
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700209 return ForkExecvp(cmd);
Kenny Root344ca102012-04-03 17:23:01 -0700210}
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700211
212} // namespace ext4
213} // namespace vold
214} // namespace android