Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 24 | #include <string> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 25 | #include <vector> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 26 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 27 | #include <sys/mman.h> |
| 28 | #include <sys/mount.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 31 | #include <sys/wait.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 32 | |
| 33 | #include <linux/kdev_t.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Jeff Sharkey | 95a92f9 | 2017-07-17 13:57:18 -0600 | [diff] [blame] | 36 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 39 | #include <fscrypt/fscrypt.h> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 40 | #include <logwrap/logwrap.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 41 | #include <selinux/selinux.h> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 42 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 43 | #include "Ext4.h" |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 44 | #include "FsCrypt.h" |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 45 | #include "Utils.h" |
Rom Lemarchand | 5593c85 | 2012-12-21 12:41:43 -0800 | [diff] [blame] | 46 | #include "VoldUtil.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 47 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 48 | using android::base::StringPrintf; |
| 49 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 50 | namespace android { |
| 51 | namespace vold { |
| 52 | namespace ext4 { |
| 53 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 54 | static const char* kResizefsPath = "/system/bin/resize2fs"; |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 55 | static const char* kMkfsPath = "/system/bin/mke2fs"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 56 | static const char* kFsckPath = "/system/bin/e2fsck"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 57 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 58 | bool IsSupported() { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 59 | return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 && |
| 60 | IsFilesystemSupported("ext4"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 63 | status_t Check(const std::string& source, const std::string& target) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 64 | // The following is shamelessly borrowed from fs_mgr.c, so it should be |
| 65 | // kept in sync with any changes over there. |
| 66 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 67 | const char* c_source = source.c_str(); |
| 68 | const char* c_target = target.c_str(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 69 | int ret; |
| 70 | long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 71 | char* tmpmnt_opts = (char*)"nomblk_io_submit,errors=remount-ro"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * First try to mount and unmount the filesystem. We do this because |
| 75 | * the kernel is more efficient than e2fsck in running the journal and |
| 76 | * processing orphaned inodes, and on at least one device with a |
| 77 | * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes |
| 78 | * to do what the kernel does in about a second. |
| 79 | * |
| 80 | * After mounting and unmounting the filesystem, run e2fsck, and if an |
| 81 | * error is recorded in the filesystem superblock, e2fsck will do a full |
| 82 | * check. Otherwise, it does nothing. If the kernel cannot mount the |
| 83 | * filesytsem due to an error, e2fsck is still run to do a full check |
| 84 | * fix the filesystem. |
| 85 | */ |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 86 | ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 87 | if (!ret) { |
| 88 | int i; |
| 89 | for (i = 0; i < 5; i++) { |
| 90 | // Try to umount 5 times before continuing on. |
| 91 | // Should we try rebooting if all attempts fail? |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 92 | int result = umount(c_target); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 93 | if (result == 0) { |
| 94 | break; |
| 95 | } |
Logan Chien | 188b0ab | 2018-04-23 13:37:39 +0800 | [diff] [blame] | 96 | LOG(WARNING) << __func__ << "(): umount(" << c_target << ")=" << result << ": " |
| 97 | << strerror(errno); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 98 | sleep(1); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Some system images do not have e2fsck for licensing reasons |
| 104 | * (e.g. recent SDK system images). Detect these and skip the check. |
| 105 | */ |
| 106 | if (access(kFsckPath, X_OK)) { |
Logan Chien | 188b0ab | 2018-04-23 13:37:39 +0800 | [diff] [blame] | 107 | LOG(DEBUG) << "Not running " << kFsckPath << " on " << c_source |
| 108 | << " (executable not in system image)"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 109 | } else { |
Logan Chien | 188b0ab | 2018-04-23 13:37:39 +0800 | [diff] [blame] | 110 | LOG(DEBUG) << "Running " << kFsckPath << " on " << c_source; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 111 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 112 | std::vector<std::string> cmd; |
| 113 | cmd.push_back(kFsckPath); |
| 114 | cmd.push_back("-y"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 115 | cmd.push_back(c_source); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 116 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 117 | // ext4 devices are currently always trusted |
| 118 | return ForkExecvp(cmd, nullptr, sFsckContext); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 124 | status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount, |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 125 | bool executable) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 126 | int rc; |
| 127 | unsigned long flags; |
| 128 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 129 | const char* c_source = source.c_str(); |
| 130 | const char* c_target = target.c_str(); |
| 131 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 132 | flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 133 | |
| 134 | flags |= (executable ? 0 : MS_NOEXEC); |
| 135 | flags |= (ro ? MS_RDONLY : 0); |
| 136 | flags |= (remount ? MS_REMOUNT : 0); |
| 137 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 138 | rc = mount(c_source, c_target, "ext4", flags, NULL); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 139 | |
| 140 | if (rc && errno == EROFS) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 141 | LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO"; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 142 | flags |= MS_RDONLY; |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 143 | rc = mount(c_source, c_target, "ext4", flags, NULL); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return rc; |
| 147 | } |
| 148 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 149 | status_t Resize(const std::string& source, unsigned long numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 150 | std::vector<std::string> cmd; |
| 151 | cmd.push_back(kResizefsPath); |
| 152 | cmd.push_back("-f"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 153 | cmd.push_back(source); |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 154 | cmd.push_back(StringPrintf("%lu", numSectors)); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 155 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 156 | return ForkExecvp(cmd); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 159 | status_t Format(const std::string& source, unsigned long numSectors, const std::string& target) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 160 | std::vector<std::string> cmd; |
| 161 | cmd.push_back(kMkfsPath); |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 162 | |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 163 | cmd.push_back("-b"); |
| 164 | cmd.push_back("4096"); |
| 165 | |
| 166 | cmd.push_back("-t"); |
| 167 | cmd.push_back("ext4"); |
| 168 | |
| 169 | cmd.push_back("-M"); |
| 170 | cmd.push_back(target); |
| 171 | |
Martijn Coenen | 97ff084 | 2020-04-15 11:42:47 +0200 | [diff] [blame] | 172 | bool needs_casefold = |
| 173 | android::base::GetBoolProperty("external_storage.casefold.enabled", false); |
Shikha Malhotra | f7bc495 | 2022-02-02 09:01:47 +0000 | [diff] [blame] | 174 | bool needs_projid = true; |
Daniel Rosenberg | 477f3e5 | 2019-11-19 20:30:46 -0800 | [diff] [blame] | 175 | |
| 176 | if (needs_projid) { |
| 177 | cmd.push_back("-I"); |
| 178 | cmd.push_back("512"); |
| 179 | } |
| 180 | |
Jeff Sharkey | 95a92f9 | 2017-07-17 13:57:18 -0600 | [diff] [blame] | 181 | std::string options("has_journal"); |
| 182 | if (android::base::GetBoolProperty("vold.has_quota", false)) { |
| 183 | options += ",quota"; |
| 184 | } |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 185 | if (fscrypt_is_native()) { |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 186 | options += ",encrypt"; |
| 187 | } |
Daniel Rosenberg | 477f3e5 | 2019-11-19 20:30:46 -0800 | [diff] [blame] | 188 | if (needs_casefold) { |
| 189 | options += ",casefold"; |
| 190 | } |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 191 | |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 192 | cmd.push_back("-O"); |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 193 | cmd.push_back(options); |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 194 | |
Daniel Rosenberg | 477f3e5 | 2019-11-19 20:30:46 -0800 | [diff] [blame] | 195 | if (needs_casefold || needs_projid) { |
| 196 | cmd.push_back("-E"); |
| 197 | std::string extopts = ""; |
| 198 | if (needs_casefold) extopts += "encoding=utf8,"; |
lin.gui | 3101ac0 | 2021-09-13 16:37:01 +0800 | [diff] [blame] | 199 | if (needs_projid) extopts += "quotatype=usrquota:grpquota:prjquota,"; |
Daniel Rosenberg | 477f3e5 | 2019-11-19 20:30:46 -0800 | [diff] [blame] | 200 | cmd.push_back(extopts); |
| 201 | } |
| 202 | |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 203 | cmd.push_back(source); |
| 204 | |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 205 | if (numSectors) { |
Jeff Sharkey | d794526 | 2017-06-26 16:09:11 -0600 | [diff] [blame] | 206 | cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512))); |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 207 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 208 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 209 | return ForkExecvp(cmd); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 210 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 211 | |
| 212 | } // namespace ext4 |
| 213 | } // namespace vold |
| 214 | } // namespace android |