blob: 89b8414a7a0cf761f579558a716055dc80cb74b8 [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>
Jeff Sharkey95a92f92017-07-17 13:57:18 -060041#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080042#include <android-base/stringprintf.h>
Kenny Root344ca102012-04-03 17:23:01 -070043#include <cutils/log.h>
44#include <cutils/properties.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070045#include <ext4_utils/ext4_crypt.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080046#include <logwrap/logwrap.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070047#include <selinux/selinux.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080048
Kenny Root344ca102012-04-03 17:23:01 -070049#include "Ext4.h"
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060050#include "Ext4Crypt.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070051#include "Utils.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080052#include "VoldUtil.h"
Kenny Root344ca102012-04-03 17:23:01 -070053
Jeff Sharkey9c484982015-03-31 10:35:33 -070054using android::base::StringPrintf;
55
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070056namespace android {
57namespace vold {
58namespace ext4 {
59
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070060static const char* kResizefsPath = "/system/bin/resize2fs";
Adrien Schildknechte0f409c2016-11-21 15:14:37 -080061static const char* kMkfsPath = "/system/bin/mke2fs";
Jeff Sharkey9c484982015-03-31 10:35:33 -070062static const char* kFsckPath = "/system/bin/e2fsck";
Jeff Sharkey9c484982015-03-31 10:35:33 -070063
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070064bool IsSupported() {
65 return access(kMkfsPath, X_OK) == 0
66 && access(kFsckPath, X_OK) == 0
67 && IsFilesystemSupported("ext4");
68}
69
70status_t Check(const std::string& source, const std::string& target) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070071 // The following is shamelessly borrowed from fs_mgr.c, so it should be
72 // kept in sync with any changes over there.
73
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070074 const char* c_source = source.c_str();
75 const char* c_target = target.c_str();
Jeff Sharkey9c484982015-03-31 10:35:33 -070076
77 int status;
78 int ret;
79 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
80 char *tmpmnt_opts = (char*) "nomblk_io_submit,errors=remount-ro";
Jeff Sharkey9c484982015-03-31 10:35:33 -070081
82 /*
83 * First try to mount and unmount the filesystem. We do this because
84 * the kernel is more efficient than e2fsck in running the journal and
85 * processing orphaned inodes, and on at least one device with a
86 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
87 * to do what the kernel does in about a second.
88 *
89 * After mounting and unmounting the filesystem, run e2fsck, and if an
90 * error is recorded in the filesystem superblock, e2fsck will do a full
91 * check. Otherwise, it does nothing. If the kernel cannot mount the
92 * filesytsem due to an error, e2fsck is still run to do a full check
93 * fix the filesystem.
94 */
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070095 ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts);
Jeff Sharkey9c484982015-03-31 10:35:33 -070096 if (!ret) {
97 int i;
98 for (i = 0; i < 5; i++) {
99 // Try to umount 5 times before continuing on.
100 // Should we try rebooting if all attempts fail?
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700101 int result = umount(c_target);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700102 if (result == 0) {
103 break;
104 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700105 ALOGW("%s(): umount(%s)=%d: %s\n", __func__, c_target, result, strerror(errno));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700106 sleep(1);
107 }
108 }
109
110 /*
111 * Some system images do not have e2fsck for licensing reasons
112 * (e.g. recent SDK system images). Detect these and skip the check.
113 */
114 if (access(kFsckPath, X_OK)) {
115 ALOGD("Not running %s on %s (executable not in system image)\n",
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700116 kFsckPath, c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700117 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700118 ALOGD("Running %s on %s\n", kFsckPath, c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700119
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700120 std::vector<std::string> cmd;
121 cmd.push_back(kFsckPath);
122 cmd.push_back("-y");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700123 cmd.push_back(c_source);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700124
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700125 // ext4 devices are currently always trusted
126 return ForkExecvp(cmd, sFsckContext);
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,
133 bool remount, bool executable) {
Kenny Root344ca102012-04-03 17:23:01 -0700134 int rc;
135 unsigned long flags;
136
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700137 const char* c_source = source.c_str();
138 const char* c_target = target.c_str();
139
Kenny Root344ca102012-04-03 17:23:01 -0700140 flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
141
142 flags |= (executable ? 0 : MS_NOEXEC);
143 flags |= (ro ? MS_RDONLY : 0);
144 flags |= (remount ? MS_REMOUNT : 0);
145
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700146 rc = mount(c_source, c_target, "ext4", flags, NULL);
Kenny Root344ca102012-04-03 17:23:01 -0700147
148 if (rc && errno == EROFS) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600149 LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
Kenny Root344ca102012-04-03 17:23:01 -0700150 flags |= MS_RDONLY;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700151 rc = mount(c_source, c_target, "ext4", flags, NULL);
Kenny Root344ca102012-04-03 17:23:01 -0700152 }
153
154 return rc;
155}
156
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200157status_t Resize(const std::string& source, unsigned long numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700158 std::vector<std::string> cmd;
159 cmd.push_back(kResizefsPath);
160 cmd.push_back("-f");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700161 cmd.push_back(source);
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200162 cmd.push_back(StringPrintf("%lu", numSectors));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700163
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700164 return ForkExecvp(cmd);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700165}
166
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200167status_t Format(const std::string& source, unsigned long numSectors,
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700168 const std::string& target) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700169 std::vector<std::string> cmd;
170 cmd.push_back(kMkfsPath);
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800171
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800172 cmd.push_back("-b");
173 cmd.push_back("4096");
174
175 cmd.push_back("-t");
176 cmd.push_back("ext4");
177
178 cmd.push_back("-M");
179 cmd.push_back(target);
180
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600181 std::string options("has_journal");
182 if (android::base::GetBoolProperty("vold.has_quota", false)) {
183 options += ",quota";
184 }
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600185 if (e4crypt_is_native()) {
186 options += ",encrypt";
187 }
188
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800189 cmd.push_back("-O");
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600190 cmd.push_back(options);
Adrien Schildknechte0f409c2016-11-21 15:14:37 -0800191
192 cmd.push_back(source);
193
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700194 if (numSectors) {
Jeff Sharkeyd7945262017-06-26 16:09:11 -0600195 cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512)));
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700196 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700198 return ForkExecvp(cmd);
Kenny Root344ca102012-04-03 17:23:01 -0700199}
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700200
201} // namespace ext4
202} // namespace vold
203} // namespace android