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