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> |
| 41 | #include <android-base/stringprintf.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 42 | #include <cutils/log.h> |
| 43 | #include <cutils/properties.h> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 44 | #include <logwrap/logwrap.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 45 | #include <selinux/selinux.h> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 46 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 47 | #include "Ext4.h" |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 48 | #include "Utils.h" |
Rom Lemarchand | 5593c85 | 2012-12-21 12:41:43 -0800 | [diff] [blame] | 49 | #include "VoldUtil.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 50 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 51 | using android::base::StringPrintf; |
| 52 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 53 | namespace android { |
| 54 | namespace vold { |
| 55 | namespace ext4 { |
| 56 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 57 | static const char* kResizefsPath = "/system/bin/resize2fs"; |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 58 | #ifdef TARGET_USES_MKE2FS |
| 59 | static const char* kMkfsPath = "/system/bin/mke2fs"; |
| 60 | #else |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 61 | static const char* kMkfsPath = "/system/bin/make_ext4fs"; |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 62 | #endif |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 63 | static const char* kFsckPath = "/system/bin/e2fsck"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 64 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 65 | bool IsSupported() { |
| 66 | return access(kMkfsPath, X_OK) == 0 |
| 67 | && access(kFsckPath, X_OK) == 0 |
| 68 | && IsFilesystemSupported("ext4"); |
| 69 | } |
| 70 | |
| 71 | status_t Check(const std::string& source, const std::string& target) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 72 | // The following is shamelessly borrowed from fs_mgr.c, so it should be |
| 73 | // kept in sync with any changes over there. |
| 74 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 75 | const char* c_source = source.c_str(); |
| 76 | const char* c_target = target.c_str(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 77 | |
| 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 Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 82 | |
| 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 Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 96 | ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 97 | 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 Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 102 | int result = umount(c_target); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 103 | if (result == 0) { |
| 104 | break; |
| 105 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 106 | 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] | 107 | 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 Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 117 | kFsckPath, c_source); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 118 | } else { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 119 | ALOGD("Running %s on %s\n", kFsckPath, c_source); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 120 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 121 | std::vector<std::string> cmd; |
| 122 | cmd.push_back(kFsckPath); |
| 123 | cmd.push_back("-y"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 124 | cmd.push_back(c_source); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 125 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 126 | // ext4 devices are currently always trusted |
| 127 | return ForkExecvp(cmd, sFsckContext); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 133 | status_t Mount(const std::string& source, const std::string& target, bool ro, |
| 134 | bool remount, bool executable) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 135 | int rc; |
| 136 | unsigned long flags; |
| 137 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 138 | const char* c_source = source.c_str(); |
| 139 | const char* c_target = target.c_str(); |
| 140 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 141 | flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC; |
| 142 | |
| 143 | flags |= (executable ? 0 : MS_NOEXEC); |
| 144 | flags |= (ro ? MS_RDONLY : 0); |
| 145 | flags |= (remount ? MS_REMOUNT : 0); |
| 146 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 147 | rc = mount(c_source, c_target, "ext4", flags, NULL); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 148 | |
| 149 | if (rc && errno == EROFS) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 150 | 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] | 151 | flags |= MS_RDONLY; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 152 | rc = mount(c_source, c_target, "ext4", flags, NULL); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return rc; |
| 156 | } |
| 157 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 158 | status_t Resize(const std::string& source, unsigned long numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 159 | std::vector<std::string> cmd; |
| 160 | cmd.push_back(kResizefsPath); |
| 161 | cmd.push_back("-f"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 162 | cmd.push_back(source); |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 163 | cmd.push_back(StringPrintf("%lu", numSectors)); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 164 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 165 | return ForkExecvp(cmd); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 168 | status_t Format(const std::string& source, unsigned long numSectors, |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 169 | const std::string& target) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 170 | std::vector<std::string> cmd; |
| 171 | cmd.push_back(kMkfsPath); |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 172 | |
| 173 | #ifdef TARGET_USES_MKE2FS |
| 174 | cmd.push_back("-b"); |
| 175 | cmd.push_back("4096"); |
| 176 | |
| 177 | cmd.push_back("-t"); |
| 178 | cmd.push_back("ext4"); |
| 179 | |
| 180 | cmd.push_back("-M"); |
| 181 | cmd.push_back(target); |
| 182 | |
| 183 | cmd.push_back("-O"); |
| 184 | cmd.push_back("^has_journal"); |
| 185 | |
| 186 | cmd.push_back(source); |
| 187 | |
| 188 | if (numSectors) |
| 189 | cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512))); |
| 190 | #else |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 191 | cmd.push_back("-J"); |
| 192 | |
| 193 | cmd.push_back("-a"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 194 | cmd.push_back(target); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 195 | |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 196 | if (numSectors) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 197 | cmd.push_back("-l"); |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 198 | cmd.push_back(StringPrintf("%lu", numSectors * 512)); |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 199 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 200 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 201 | // Always generate a real UUID |
| 202 | cmd.push_back("-u"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 203 | cmd.push_back(source); |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 204 | #endif |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 205 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 206 | return ForkExecvp(cmd); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 207 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 208 | |
| 209 | } // namespace ext4 |
| 210 | } // namespace vold |
| 211 | } // namespace android |