Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [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 | |
Mark Salyzyn | d9e6c20 | 2018-06-20 14:08:09 -0700 | [diff] [blame] | 17 | #include "fs_mgr.h" |
| 18 | |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | #include <dirent.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
Tom Cherry | fafbb64 | 2018-11-29 13:24:09 -0800 | [diff] [blame] | 23 | #include <inttypes.h> |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 24 | #include <libgen.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 29 | #include <sys/mount.h> |
| 30 | #include <sys/stat.h> |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 31 | #include <sys/swap.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 34 | #include <time.h> |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 35 | #include <unistd.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 36 | |
Bart Van Assche | ec5f635 | 2021-07-29 13:50:43 -0700 | [diff] [blame] | 37 | #include <array> |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 38 | #include <chrono> |
Mark Salyzyn | b5065fc | 2018-06-27 14:57:10 -0700 | [diff] [blame] | 39 | #include <functional> |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 40 | #include <map> |
Bowgo Tsai | aaf70e7 | 2017-03-02 00:03:56 +0800 | [diff] [blame] | 41 | #include <memory> |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 42 | #include <string> |
Bart Van Assche | ec5f635 | 2021-07-29 13:50:43 -0700 | [diff] [blame] | 43 | #include <string_view> |
Jinguang Dong | 9d34496 | 2017-06-13 10:20:34 +0800 | [diff] [blame] | 44 | #include <thread> |
Mark Salyzyn | be29673 | 2018-10-05 09:00:55 -0700 | [diff] [blame] | 45 | #include <utility> |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 46 | #include <vector> |
Bowgo Tsai | aaf70e7 | 2017-03-02 00:03:56 +0800 | [diff] [blame] | 47 | |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 48 | #include <android-base/chrono_utils.h> |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 49 | #include <android-base/file.h> |
Bowgo Tsai | aaf70e7 | 2017-03-02 00:03:56 +0800 | [diff] [blame] | 50 | #include <android-base/properties.h> |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 51 | #include <android-base/stringprintf.h> |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 52 | #include <android-base/strings.h> |
bowgotsai | cea7ea7 | 2017-01-16 21:49:49 +0800 | [diff] [blame] | 53 | #include <android-base/unique_fd.h> |
Jeff Sharkey | 47dc236 | 2018-01-07 19:17:39 -0700 | [diff] [blame] | 54 | #include <cutils/android_filesystem_config.h> |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 55 | #include <cutils/android_reboot.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 56 | #include <cutils/partition_utils.h> |
| 57 | #include <cutils/properties.h> |
Tao Bao | 6d881d6 | 2016-10-05 17:53:30 -0700 | [diff] [blame] | 58 | #include <ext4_utils/ext4.h> |
Tao Bao | 6d881d6 | 2016-10-05 17:53:30 -0700 | [diff] [blame] | 59 | #include <ext4_utils/ext4_sb.h> |
| 60 | #include <ext4_utils/ext4_utils.h> |
| 61 | #include <ext4_utils/wipe.h> |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 62 | #include <fs_avb/fs_avb.h> |
David Anderson | 5c475c7 | 2019-06-11 17:40:49 -0700 | [diff] [blame] | 63 | #include <fs_mgr/file_wait.h> |
Mark Salyzyn | d9e6c20 | 2018-06-20 14:08:09 -0700 | [diff] [blame] | 64 | #include <fs_mgr_overlayfs.h> |
Paul Crowley | 7160fc1 | 2019-10-25 17:12:45 -0700 | [diff] [blame] | 65 | #include <fscrypt/fscrypt.h> |
David Anderson | 40b5948 | 2018-06-25 17:55:01 -0700 | [diff] [blame] | 66 | #include <libdm/dm.h> |
Nikita Ioffe | bcaeb70 | 2020-04-20 17:38:17 +0100 | [diff] [blame] | 67 | #include <libdm/loop_control.h> |
David Anderson | 5cbd2e4 | 2018-09-27 10:53:04 -0700 | [diff] [blame] | 68 | #include <liblp/metadata_format.h> |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 69 | #include <linux/fs.h> |
Mattias Nissler | 097b6bb | 2016-03-31 16:32:09 +0200 | [diff] [blame] | 70 | #include <linux/loop.h> |
Keun-young Park | 6000a3f | 2017-04-11 18:59:56 -0700 | [diff] [blame] | 71 | #include <linux/magic.h> |
Steven Moreland | d73be1b | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 72 | #include <log/log_properties.h> |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 73 | #include <logwrap/logwrap.h> |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 74 | |
Bart Van Assche | 0223cd8 | 2021-08-06 10:21:12 -0700 | [diff] [blame] | 75 | #include "blockdev.h" |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 76 | #include "fs_mgr_priv.h" |
| 77 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 78 | #define E2FSCK_BIN "/system/bin/e2fsck" |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 79 | #define F2FS_FSCK_BIN "/system/bin/fsck.f2fs" |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 80 | #define MKSWAP_BIN "/system/bin/mkswap" |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 81 | #define TUNE2FS_BIN "/system/bin/tune2fs" |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 82 | #define RESIZE2FS_BIN "/system/bin/resize2fs" |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 83 | |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 84 | #define FSCK_LOG_FILE "/dev/fscklogs/log" |
| 85 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 86 | #define ZRAM_CONF_DEV "/sys/block/zram0/disksize" |
Peter Enderborg | 4d217f0 | 2016-08-26 15:09:35 +0200 | [diff] [blame] | 87 | #define ZRAM_CONF_MCS "/sys/block/zram0/max_comp_streams" |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 88 | #define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev" |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 89 | |
Leo Liou | 0b721d3 | 2019-01-15 20:43:37 +0800 | [diff] [blame] | 90 | #define SYSFS_EXT4_VERITY "/sys/fs/ext4/features/verity" |
Daniel Rosenberg | 8775ce0 | 2019-11-19 20:30:06 -0800 | [diff] [blame] | 91 | #define SYSFS_EXT4_CASEFOLD "/sys/fs/ext4/features/casefold" |
Leo Liou | 0b721d3 | 2019-01-15 20:43:37 +0800 | [diff] [blame] | 92 | |
Paul Crowley | 7160fc1 | 2019-10-25 17:12:45 -0700 | [diff] [blame] | 93 | // FIXME: this should be in system/extras |
| 94 | #define EXT4_FEATURE_COMPAT_STABLE_INODES 0x0800 |
| 95 | |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 96 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 97 | |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 98 | using android::base::Basename; |
Nikita Ioffe | d572c80 | 2019-12-09 21:13:08 +0000 | [diff] [blame] | 99 | using android::base::GetBoolProperty; |
Nikita Ioffe | 7b41a15 | 2020-03-25 00:06:51 +0000 | [diff] [blame] | 100 | using android::base::GetUintProperty; |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 101 | using android::base::Realpath; |
Jaegeuk Kim | 24d2a29 | 2020-02-27 11:28:45 -0800 | [diff] [blame] | 102 | using android::base::SetProperty; |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 103 | using android::base::StartsWith; |
Bart Van Assche | ec5f635 | 2021-07-29 13:50:43 -0700 | [diff] [blame] | 104 | using android::base::StringPrintf; |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 105 | using android::base::Timer; |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 106 | using android::base::unique_fd; |
David Anderson | 3563862 | 2018-08-27 14:02:47 -0700 | [diff] [blame] | 107 | using android::dm::DeviceMapper; |
| 108 | using android::dm::DmDeviceState; |
David Anderson | 671bd81 | 2020-01-24 19:19:27 -0800 | [diff] [blame] | 109 | using android::dm::DmTargetLinear; |
Nikita Ioffe | bcaeb70 | 2020-04-20 17:38:17 +0100 | [diff] [blame] | 110 | using android::dm::LoopControl; |
Tom Cherry | a3530e6 | 2019-01-30 13:25:35 -0800 | [diff] [blame] | 111 | |
| 112 | // Realistically, this file should be part of the android::fs_mgr namespace; |
| 113 | using namespace android::fs_mgr; |
David Anderson | 40b5948 | 2018-06-25 17:55:01 -0700 | [diff] [blame] | 114 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 115 | using namespace std::literals; |
| 116 | |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 117 | // record fs stat |
| 118 | enum FsStatFlags { |
Keun-young Park | 6000a3f | 2017-04-11 18:59:56 -0700 | [diff] [blame] | 119 | FS_STAT_IS_EXT4 = 0x0001, |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 120 | FS_STAT_NEW_IMAGE_VERSION = 0x0002, |
Keun-young Park | 6000a3f | 2017-04-11 18:59:56 -0700 | [diff] [blame] | 121 | FS_STAT_E2FSCK_F_ALWAYS = 0x0004, |
| 122 | FS_STAT_UNCLEAN_SHUTDOWN = 0x0008, |
| 123 | FS_STAT_QUOTA_ENABLED = 0x0010, |
Keun-young Park | 6000a3f | 2017-04-11 18:59:56 -0700 | [diff] [blame] | 124 | FS_STAT_RO_MOUNT_FAILED = 0x0040, |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 125 | FS_STAT_RO_UNMOUNT_FAILED = 0x0080, |
| 126 | FS_STAT_FULL_MOUNT_FAILED = 0x0100, |
Keun-young Park | 6000a3f | 2017-04-11 18:59:56 -0700 | [diff] [blame] | 127 | FS_STAT_E2FSCK_FAILED = 0x0200, |
| 128 | FS_STAT_E2FSCK_FS_FIXED = 0x0400, |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 129 | FS_STAT_INVALID_MAGIC = 0x0800, |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 130 | FS_STAT_TOGGLE_QUOTAS_FAILED = 0x10000, |
| 131 | FS_STAT_SET_RESERVED_BLOCKS_FAILED = 0x20000, |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 132 | FS_STAT_ENABLE_ENCRYPTION_FAILED = 0x40000, |
Leo Liou | 0b721d3 | 2019-01-15 20:43:37 +0800 | [diff] [blame] | 133 | FS_STAT_ENABLE_VERITY_FAILED = 0x80000, |
Daniel Rosenberg | 8775ce0 | 2019-11-19 20:30:06 -0800 | [diff] [blame] | 134 | FS_STAT_ENABLE_CASEFOLD_FAILED = 0x100000, |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 135 | FS_STAT_ENABLE_METADATA_CSUM_FAILED = 0x200000, |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 138 | static void log_fs_stat(const std::string& blk_device, int fs_stat) { |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 139 | if ((fs_stat & FS_STAT_IS_EXT4) == 0) return; // only log ext4 |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 140 | std::string msg = |
| 141 | android::base::StringPrintf("\nfs_stat,%s,0x%x\n", blk_device.c_str(), fs_stat); |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 142 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(FSCK_LOG_FILE, O_WRONLY | O_CLOEXEC | |
| 143 | O_APPEND | O_CREAT, 0664))); |
| 144 | if (fd == -1 || !android::base::WriteStringToFd(msg, fd)) { |
| 145 | LWARNING << __FUNCTION__ << "() cannot log " << msg; |
| 146 | } |
| 147 | } |
| 148 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 149 | static bool is_extfs(const std::string& fs_type) { |
| 150 | return fs_type == "ext4" || fs_type == "ext3" || fs_type == "ext2"; |
| 151 | } |
| 152 | |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 153 | static bool is_f2fs(const std::string& fs_type) { |
| 154 | return fs_type == "f2fs"; |
| 155 | } |
| 156 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 157 | static std::string realpath(const std::string& blk_device) { |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 158 | std::string real_path; |
| 159 | if (!Realpath(blk_device, &real_path)) { |
| 160 | real_path = blk_device; |
| 161 | } |
| 162 | return real_path; |
| 163 | } |
| 164 | |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 165 | static bool should_force_check(int fs_stat) { |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 166 | return fs_stat & |
| 167 | (FS_STAT_E2FSCK_F_ALWAYS | FS_STAT_UNCLEAN_SHUTDOWN | FS_STAT_QUOTA_ENABLED | |
| 168 | FS_STAT_RO_MOUNT_FAILED | FS_STAT_RO_UNMOUNT_FAILED | FS_STAT_FULL_MOUNT_FAILED | |
| 169 | FS_STAT_E2FSCK_FAILED | FS_STAT_TOGGLE_QUOTAS_FAILED | |
| 170 | FS_STAT_SET_RESERVED_BLOCKS_FAILED | FS_STAT_ENABLE_ENCRYPTION_FAILED); |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jaegeuk Kim | bdb981d | 2021-12-07 11:23:52 -0800 | [diff] [blame] | 173 | static bool umount_retry(const std::string& mount_point) { |
| 174 | int retry_count = 5; |
| 175 | bool umounted = false; |
| 176 | |
| 177 | while (retry_count-- > 0) { |
| 178 | umounted = umount(mount_point.c_str()) == 0; |
| 179 | if (umounted) { |
| 180 | LINFO << __FUNCTION__ << "(): unmount(" << mount_point << ") succeeded"; |
| 181 | break; |
| 182 | } |
| 183 | PERROR << __FUNCTION__ << "(): umount(" << mount_point << ") failed"; |
| 184 | if (retry_count) sleep(1); |
| 185 | } |
| 186 | return umounted; |
| 187 | } |
| 188 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 189 | static void check_fs(const std::string& blk_device, const std::string& fs_type, |
| 190 | const std::string& target, int* fs_stat) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 191 | int status; |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 192 | int ret; |
| 193 | long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID; |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 194 | auto tmpmnt_opts = "errors=remount-ro"s; |
| 195 | const char* e2fsck_argv[] = {E2FSCK_BIN, "-y", blk_device.c_str()}; |
| 196 | const char* e2fsck_forced_argv[] = {E2FSCK_BIN, "-f", "-y", blk_device.c_str()}; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 197 | |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 198 | if (*fs_stat & FS_STAT_INVALID_MAGIC) { // will fail, so do not try |
| 199 | return; |
| 200 | } |
| 201 | |
Jaegeuk Kim | 24d2a29 | 2020-02-27 11:28:45 -0800 | [diff] [blame] | 202 | Timer t; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 203 | /* Check for the types of filesystems we know how to check */ |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 204 | if (is_extfs(fs_type)) { |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 205 | /* |
| 206 | * First try to mount and unmount the filesystem. We do this because |
| 207 | * the kernel is more efficient than e2fsck in running the journal and |
| 208 | * processing orphaned inodes, and on at least one device with a |
| 209 | * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes |
| 210 | * to do what the kernel does in about a second. |
| 211 | * |
| 212 | * After mounting and unmounting the filesystem, run e2fsck, and if an |
| 213 | * error is recorded in the filesystem superblock, e2fsck will do a full |
| 214 | * check. Otherwise, it does nothing. If the kernel cannot mount the |
| 215 | * filesytsem due to an error, e2fsck is still run to do a full check |
| 216 | * fix the filesystem. |
| 217 | */ |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 218 | if (!(*fs_stat & FS_STAT_FULL_MOUNT_FAILED)) { // already tried if full mount failed |
| 219 | errno = 0; |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 220 | if (fs_type == "ext4") { |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 221 | // This option is only valid with ext4 |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 222 | tmpmnt_opts += ",nomblk_io_submit"; |
Nick Kralevich | 7294eb6 | 2015-02-05 16:02:42 -0800 | [diff] [blame] | 223 | } |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 224 | ret = mount(blk_device.c_str(), target.c_str(), fs_type.c_str(), tmpmnt_flags, |
| 225 | tmpmnt_opts.c_str()); |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 226 | PINFO << __FUNCTION__ << "(): mount(" << blk_device << "," << target << "," << fs_type |
| 227 | << ")=" << ret; |
Jaegeuk Kim | bdb981d | 2021-12-07 11:23:52 -0800 | [diff] [blame] | 228 | if (ret) { |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 229 | *fs_stat |= FS_STAT_RO_MOUNT_FAILED; |
Jaegeuk Kim | bdb981d | 2021-12-07 11:23:52 -0800 | [diff] [blame] | 230 | } else if (!umount_retry(target)) { |
| 231 | // boot may fail but continue and leave it to later stage for now. |
| 232 | PERROR << __FUNCTION__ << "(): umount(" << target << ") timed out"; |
| 233 | *fs_stat |= FS_STAT_RO_UNMOUNT_FAILED; |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 234 | } |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 235 | } |
| 236 | |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 237 | /* |
| 238 | * Some system images do not have e2fsck for licensing reasons |
| 239 | * (e.g. recent SDK system images). Detect these and skip the check. |
| 240 | */ |
| 241 | if (access(E2FSCK_BIN, X_OK)) { |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 242 | LINFO << "Not running " << E2FSCK_BIN << " on " << realpath(blk_device) |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 243 | << " (executable not in system image)"; |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 244 | } else { |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 245 | LINFO << "Running " << E2FSCK_BIN << " on " << realpath(blk_device); |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 246 | if (should_force_check(*fs_stat)) { |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 247 | ret = logwrap_fork_execvp(ARRAY_SIZE(e2fsck_forced_argv), e2fsck_forced_argv, |
Wei Wang | 02e89ec | 2020-02-14 14:22:16 -0800 | [diff] [blame] | 248 | &status, false, LOG_KLOG | LOG_FILE, false, |
| 249 | FSCK_LOG_FILE); |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 250 | } else { |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 251 | ret = logwrap_fork_execvp(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, &status, false, |
Wei Wang | 02e89ec | 2020-02-14 14:22:16 -0800 | [diff] [blame] | 252 | LOG_KLOG | LOG_FILE, false, FSCK_LOG_FILE); |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 253 | } |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 254 | |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 255 | if (ret < 0) { |
| 256 | /* No need to check for error in fork, we can't really handle it now */ |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 257 | LERROR << "Failed trying to run " << E2FSCK_BIN; |
Keun-young Park | 3fbf94e | 2017-03-02 14:33:04 -0800 | [diff] [blame] | 258 | *fs_stat |= FS_STAT_E2FSCK_FAILED; |
| 259 | } else if (status != 0) { |
| 260 | LINFO << "e2fsck returned status 0x" << std::hex << status; |
| 261 | *fs_stat |= FS_STAT_E2FSCK_FS_FIXED; |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 262 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 263 | } |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 264 | } else if (is_f2fs(fs_type)) { |
Jaegeuk Kim | c461e16 | 2020-01-03 14:09:41 -0800 | [diff] [blame] | 265 | const char* f2fs_fsck_argv[] = {F2FS_FSCK_BIN, "-a", "-c", "10000", "--debug-cache", |
| 266 | blk_device.c_str()}; |
| 267 | const char* f2fs_fsck_forced_argv[] = { |
| 268 | F2FS_FSCK_BIN, "-f", "-c", "10000", "--debug-cache", blk_device.c_str()}; |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 269 | |
Sahitya Tummala | 5928e4f | 2019-05-23 16:23:58 +0530 | [diff] [blame] | 270 | if (should_force_check(*fs_stat)) { |
jiahao | bfcee28 | 2021-04-16 17:07:50 +0800 | [diff] [blame] | 271 | LINFO << "Running " << F2FS_FSCK_BIN << " -f -c 10000 --debug-cache " |
Jaegeuk Kim | c461e16 | 2020-01-03 14:09:41 -0800 | [diff] [blame] | 272 | << realpath(blk_device); |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 273 | ret = logwrap_fork_execvp(ARRAY_SIZE(f2fs_fsck_forced_argv), f2fs_fsck_forced_argv, |
Jaegeuk Kim | d595de5 | 2021-12-30 16:48:44 -0800 | [diff] [blame] | 274 | &status, false, LOG_KLOG | LOG_FILE, false, nullptr); |
Sahitya Tummala | 5928e4f | 2019-05-23 16:23:58 +0530 | [diff] [blame] | 275 | } else { |
jiahao | bfcee28 | 2021-04-16 17:07:50 +0800 | [diff] [blame] | 276 | LINFO << "Running " << F2FS_FSCK_BIN << " -a -c 10000 --debug-cache " |
Jaegeuk Kim | c461e16 | 2020-01-03 14:09:41 -0800 | [diff] [blame] | 277 | << realpath(blk_device); |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 278 | ret = logwrap_fork_execvp(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv, &status, false, |
Jaegeuk Kim | d595de5 | 2021-12-30 16:48:44 -0800 | [diff] [blame] | 279 | LOG_KLOG | LOG_FILE, false, nullptr); |
Sahitya Tummala | 5928e4f | 2019-05-23 16:23:58 +0530 | [diff] [blame] | 280 | } |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 281 | if (ret < 0) { |
| 282 | /* No need to check for error in fork, we can't really handle it now */ |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 283 | LERROR << "Failed trying to run " << F2FS_FSCK_BIN; |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 284 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 285 | } |
Jaegeuk Kim | 24d2a29 | 2020-02-27 11:28:45 -0800 | [diff] [blame] | 286 | android::base::SetProperty("ro.boottime.init.fsck." + Basename(target), |
| 287 | std::to_string(t.duration().count())); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 291 | static ext4_fsblk_t ext4_blocks_count(const struct ext4_super_block* es) { |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 292 | return ((ext4_fsblk_t)le32_to_cpu(es->s_blocks_count_hi) << 32) | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 293 | le32_to_cpu(es->s_blocks_count_lo); |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 294 | } |
| 295 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 296 | static ext4_fsblk_t ext4_r_blocks_count(const struct ext4_super_block* es) { |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 297 | return ((ext4_fsblk_t)le32_to_cpu(es->s_r_blocks_count_hi) << 32) | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 298 | le32_to_cpu(es->s_r_blocks_count_lo); |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 299 | } |
| 300 | |
katao | 4e8d73f | 2017-07-05 16:00:54 +0800 | [diff] [blame] | 301 | static bool is_ext4_superblock_valid(const struct ext4_super_block* es) { |
| 302 | if (es->s_magic != EXT4_SUPER_MAGIC) return false; |
| 303 | if (es->s_rev_level != EXT4_DYNAMIC_REV && es->s_rev_level != EXT4_GOOD_OLD_REV) return false; |
| 304 | if (EXT4_INODES_PER_GROUP(es) == 0) return false; |
| 305 | return true; |
| 306 | } |
| 307 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 308 | // Read the primary superblock from an ext4 filesystem. On failure return |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 309 | // false. If it's not an ext4 filesystem, also set FS_STAT_INVALID_MAGIC. |
Eric Biggers | dc3e897 | 2020-08-04 11:11:13 -0700 | [diff] [blame] | 310 | static bool read_ext4_superblock(const std::string& blk_device, struct ext4_super_block* sb, |
| 311 | int* fs_stat) { |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 312 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC))); |
Jeff Sharkey | 6d89610 | 2016-12-14 12:00:51 -0700 | [diff] [blame] | 313 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 314 | if (fd < 0) { |
| 315 | PERROR << "Failed to open '" << blk_device << "'"; |
| 316 | return false; |
Jeff Sharkey | 6d89610 | 2016-12-14 12:00:51 -0700 | [diff] [blame] | 317 | } |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 318 | |
Mark Salyzyn | 60a76f3 | 2019-03-14 15:34:35 -0700 | [diff] [blame] | 319 | if (TEMP_FAILURE_RETRY(pread(fd, sb, sizeof(*sb), 1024)) != sizeof(*sb)) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 320 | PERROR << "Can't read '" << blk_device << "' superblock"; |
| 321 | return false; |
| 322 | } |
| 323 | |
katao | 4e8d73f | 2017-07-05 16:00:54 +0800 | [diff] [blame] | 324 | if (!is_ext4_superblock_valid(sb)) { |
| 325 | LINFO << "Invalid ext4 superblock on '" << blk_device << "'"; |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 326 | // not a valid fs, tune2fs, fsck, and mount will all fail. |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 327 | *fs_stat |= FS_STAT_INVALID_MAGIC; |
Eric Biggers | dc3e897 | 2020-08-04 11:11:13 -0700 | [diff] [blame] | 328 | return false; |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 329 | } |
| 330 | *fs_stat |= FS_STAT_IS_EXT4; |
| 331 | LINFO << "superblock s_max_mnt_count:" << sb->s_max_mnt_count << "," << blk_device; |
| 332 | if (sb->s_max_mnt_count == 0xffff) { // -1 (int16) in ext2, but uint16 in ext4 |
| 333 | *fs_stat |= FS_STAT_NEW_IMAGE_VERSION; |
| 334 | } |
| 335 | return true; |
Jeff Sharkey | 6d89610 | 2016-12-14 12:00:51 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Mark Salyzyn | 60a76f3 | 2019-03-14 15:34:35 -0700 | [diff] [blame] | 338 | // exported silent version of the above that just answer the question is_ext4 |
| 339 | bool fs_mgr_is_ext4(const std::string& blk_device) { |
| 340 | android::base::ErrnoRestorer restore; |
| 341 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC))); |
| 342 | if (fd < 0) return false; |
| 343 | ext4_super_block sb; |
| 344 | if (TEMP_FAILURE_RETRY(pread(fd, &sb, sizeof(sb), 1024)) != sizeof(sb)) return false; |
| 345 | if (!is_ext4_superblock_valid(&sb)) return false; |
| 346 | return true; |
| 347 | } |
| 348 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 349 | // Some system images do not have tune2fs for licensing reasons. |
| 350 | // Detect these and skip running it. |
| 351 | static bool tune2fs_available(void) { |
| 352 | return access(TUNE2FS_BIN, X_OK) == 0; |
| 353 | } |
| 354 | |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 355 | static bool run_command(const char* argv[], int argc) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 356 | int ret; |
| 357 | |
Wei Wang | 02e89ec | 2020-02-14 14:22:16 -0800 | [diff] [blame] | 358 | ret = logwrap_fork_execvp(argc, argv, nullptr, false, LOG_KLOG, false, nullptr); |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 359 | return ret == 0; |
| 360 | } |
| 361 | |
| 362 | // Enable/disable quota support on the filesystem if needed. |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 363 | static void tune_quota(const std::string& blk_device, const FstabEntry& entry, |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 364 | const struct ext4_super_block* sb, int* fs_stat) { |
| 365 | bool has_quota = (sb->s_feature_ro_compat & cpu_to_le32(EXT4_FEATURE_RO_COMPAT_QUOTA)) != 0; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 366 | bool want_quota = entry.fs_mgr_flags.quota; |
Shikha Malhotra | bc9b847 | 2021-12-29 16:26:18 +0000 | [diff] [blame] | 367 | // Enable projid support by default |
| 368 | bool want_projid = true; |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 369 | if (has_quota == want_quota) { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | if (!tune2fs_available()) { |
| 374 | LERROR << "Unable to " << (want_quota ? "enable" : "disable") << " quotas on " << blk_device |
| 375 | << " because " TUNE2FS_BIN " is missing"; |
| 376 | return; |
| 377 | } |
| 378 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 379 | const char* argv[] = {TUNE2FS_BIN, nullptr, nullptr, blk_device.c_str()}; |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 380 | |
| 381 | if (want_quota) { |
| 382 | LINFO << "Enabling quotas on " << blk_device; |
| 383 | argv[1] = "-Oquota"; |
Daniel Rosenberg | 8775ce0 | 2019-11-19 20:30:06 -0800 | [diff] [blame] | 384 | // Once usr/grp unneeded, make just prjquota to save overhead |
| 385 | if (want_projid) |
| 386 | argv[2] = "-Qusrquota,grpquota,prjquota"; |
| 387 | else |
| 388 | argv[2] = "-Qusrquota,grpquota"; |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 389 | *fs_stat |= FS_STAT_QUOTA_ENABLED; |
| 390 | } else { |
| 391 | LINFO << "Disabling quotas on " << blk_device; |
| 392 | argv[1] = "-O^quota"; |
Daniel Rosenberg | 8775ce0 | 2019-11-19 20:30:06 -0800 | [diff] [blame] | 393 | argv[2] = "-Q^usrquota,^grpquota,^prjquota"; |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 396 | if (!run_command(argv, ARRAY_SIZE(argv))) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 397 | LERROR << "Failed to run " TUNE2FS_BIN " to " << (want_quota ? "enable" : "disable") |
| 398 | << " quotas on " << blk_device; |
| 399 | *fs_stat |= FS_STAT_TOGGLE_QUOTAS_FAILED; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Set the number of reserved filesystem blocks if needed. |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 404 | static void tune_reserved_size(const std::string& blk_device, const FstabEntry& entry, |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 405 | const struct ext4_super_block* sb, int* fs_stat) { |
Martijn Coenen | 20c4cfa | 2019-05-10 10:39:54 +0200 | [diff] [blame] | 406 | if (entry.reserved_size == 0) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
| 410 | // The size to reserve is given in the fstab, but we won't reserve more |
| 411 | // than 2% of the filesystem. |
| 412 | const uint64_t max_reserved_blocks = ext4_blocks_count(sb) * 0.02; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 413 | uint64_t reserved_blocks = entry.reserved_size / EXT4_BLOCK_SIZE(sb); |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 414 | |
| 415 | if (reserved_blocks > max_reserved_blocks) { |
| 416 | LWARNING << "Reserved blocks " << reserved_blocks << " is too large; " |
| 417 | << "capping to " << max_reserved_blocks; |
| 418 | reserved_blocks = max_reserved_blocks; |
| 419 | } |
| 420 | |
Jeff Sharkey | 47dc236 | 2018-01-07 19:17:39 -0700 | [diff] [blame] | 421 | if ((ext4_r_blocks_count(sb) == reserved_blocks) && (sb->s_def_resgid == AID_RESERVED_DISK)) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (!tune2fs_available()) { |
| 426 | LERROR << "Unable to set the number of reserved blocks on " << blk_device |
| 427 | << " because " TUNE2FS_BIN " is missing"; |
| 428 | return; |
| 429 | } |
| 430 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 431 | LINFO << "Setting reserved block count on " << blk_device << " to " << reserved_blocks; |
Jeff Sharkey | 47dc236 | 2018-01-07 19:17:39 -0700 | [diff] [blame] | 432 | |
| 433 | auto reserved_blocks_str = std::to_string(reserved_blocks); |
| 434 | auto reserved_gid_str = std::to_string(AID_RESERVED_DISK); |
| 435 | const char* argv[] = { |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 436 | TUNE2FS_BIN, "-r", reserved_blocks_str.c_str(), "-g", reserved_gid_str.c_str(), |
| 437 | blk_device.c_str()}; |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 438 | if (!run_command(argv, ARRAY_SIZE(argv))) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 439 | LERROR << "Failed to run " TUNE2FS_BIN " to set the number of reserved blocks on " |
| 440 | << blk_device; |
| 441 | *fs_stat |= FS_STAT_SET_RESERVED_BLOCKS_FAILED; |
| 442 | } |
| 443 | } |
| 444 | |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 445 | // Enable file-based encryption if needed. |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 446 | static void tune_encrypt(const std::string& blk_device, const FstabEntry& entry, |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 447 | const struct ext4_super_block* sb, int* fs_stat) { |
Paul Crowley | 7160fc1 | 2019-10-25 17:12:45 -0700 | [diff] [blame] | 448 | if (!entry.fs_mgr_flags.file_encryption) { |
| 449 | return; // Nothing needs done. |
| 450 | } |
| 451 | std::vector<std::string> features_needed; |
| 452 | if ((sb->s_feature_incompat & cpu_to_le32(EXT4_FEATURE_INCOMPAT_ENCRYPT)) == 0) { |
| 453 | features_needed.emplace_back("encrypt"); |
| 454 | } |
| 455 | android::fscrypt::EncryptionOptions options; |
| 456 | if (!android::fscrypt::ParseOptions(entry.encryption_options, &options)) { |
| 457 | LERROR << "Unable to parse encryption options on " << blk_device << ": " |
| 458 | << entry.encryption_options; |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 459 | return; |
| 460 | } |
Paul Crowley | c2f3768 | 2020-05-20 16:36:15 -0700 | [diff] [blame] | 461 | if ((options.flags & |
| 462 | (FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) != 0) { |
Paul Crowley | 7160fc1 | 2019-10-25 17:12:45 -0700 | [diff] [blame] | 463 | // We can only use this policy on ext4 if the "stable_inodes" feature |
| 464 | // is set on the filesystem, otherwise shrinking will break encrypted files. |
| 465 | if ((sb->s_feature_compat & cpu_to_le32(EXT4_FEATURE_COMPAT_STABLE_INODES)) == 0) { |
| 466 | features_needed.emplace_back("stable_inodes"); |
| 467 | } |
| 468 | } |
| 469 | if (features_needed.size() == 0) { |
| 470 | return; |
| 471 | } |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 472 | if (!tune2fs_available()) { |
| 473 | LERROR << "Unable to enable ext4 encryption on " << blk_device |
| 474 | << " because " TUNE2FS_BIN " is missing"; |
| 475 | return; |
| 476 | } |
| 477 | |
Paul Crowley | 7160fc1 | 2019-10-25 17:12:45 -0700 | [diff] [blame] | 478 | auto flags = android::base::Join(features_needed, ','); |
| 479 | auto flag_arg = "-O"s + flags; |
| 480 | const char* argv[] = {TUNE2FS_BIN, flag_arg.c_str(), blk_device.c_str()}; |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 481 | |
Paul Crowley | 7160fc1 | 2019-10-25 17:12:45 -0700 | [diff] [blame] | 482 | LINFO << "Enabling ext4 flags " << flags << " on " << blk_device; |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 483 | if (!run_command(argv, ARRAY_SIZE(argv))) { |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 484 | LERROR << "Failed to run " TUNE2FS_BIN " to enable " |
Paul Crowley | 7160fc1 | 2019-10-25 17:12:45 -0700 | [diff] [blame] | 485 | << "ext4 flags " << flags << " on " << blk_device; |
Eric Biggers | e9811f3 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 486 | *fs_stat |= FS_STAT_ENABLE_ENCRYPTION_FAILED; |
| 487 | } |
| 488 | } |
| 489 | |
Leo Liou | 0b721d3 | 2019-01-15 20:43:37 +0800 | [diff] [blame] | 490 | // Enable fs-verity if needed. |
| 491 | static void tune_verity(const std::string& blk_device, const FstabEntry& entry, |
| 492 | const struct ext4_super_block* sb, int* fs_stat) { |
| 493 | bool has_verity = (sb->s_feature_ro_compat & cpu_to_le32(EXT4_FEATURE_RO_COMPAT_VERITY)) != 0; |
| 494 | bool want_verity = entry.fs_mgr_flags.fs_verity; |
| 495 | |
| 496 | if (has_verity || !want_verity) { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | std::string verity_support; |
| 501 | if (!android::base::ReadFileToString(SYSFS_EXT4_VERITY, &verity_support)) { |
| 502 | LERROR << "Failed to open " << SYSFS_EXT4_VERITY; |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | if (!(android::base::Trim(verity_support) == "supported")) { |
| 507 | LERROR << "Current ext4 verity not supported by kernel"; |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | if (!tune2fs_available()) { |
| 512 | LERROR << "Unable to enable ext4 verity on " << blk_device |
| 513 | << " because " TUNE2FS_BIN " is missing"; |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | LINFO << "Enabling ext4 verity on " << blk_device; |
| 518 | |
| 519 | const char* argv[] = {TUNE2FS_BIN, "-O", "verity", blk_device.c_str()}; |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 520 | if (!run_command(argv, ARRAY_SIZE(argv))) { |
Leo Liou | 0b721d3 | 2019-01-15 20:43:37 +0800 | [diff] [blame] | 521 | LERROR << "Failed to run " TUNE2FS_BIN " to enable " |
| 522 | << "ext4 verity on " << blk_device; |
| 523 | *fs_stat |= FS_STAT_ENABLE_VERITY_FAILED; |
| 524 | } |
| 525 | } |
| 526 | |
Daniel Rosenberg | 8775ce0 | 2019-11-19 20:30:06 -0800 | [diff] [blame] | 527 | // Enable casefold if needed. |
Jaegeuk Kim | 5ba5b90 | 2020-08-14 12:34:36 -0700 | [diff] [blame] | 528 | static void tune_casefold(const std::string& blk_device, const FstabEntry& entry, |
| 529 | const struct ext4_super_block* sb, int* fs_stat) { |
Eric Biggers | 91bcfd8 | 2020-02-25 11:11:35 -0800 | [diff] [blame] | 530 | bool has_casefold = (sb->s_feature_incompat & cpu_to_le32(EXT4_FEATURE_INCOMPAT_CASEFOLD)) != 0; |
Martijn Coenen | ea751d9 | 2020-04-15 11:45:05 +0200 | [diff] [blame] | 531 | bool wants_casefold = |
| 532 | android::base::GetBoolProperty("external_storage.casefold.enabled", false); |
Daniel Rosenberg | 8775ce0 | 2019-11-19 20:30:06 -0800 | [diff] [blame] | 533 | |
Yongqin Liu | c007c43 | 2020-10-23 13:29:19 +0800 | [diff] [blame] | 534 | if (entry.mount_point != "/data" || !wants_casefold || has_casefold) return; |
Daniel Rosenberg | 8775ce0 | 2019-11-19 20:30:06 -0800 | [diff] [blame] | 535 | |
| 536 | std::string casefold_support; |
| 537 | if (!android::base::ReadFileToString(SYSFS_EXT4_CASEFOLD, &casefold_support)) { |
| 538 | LERROR << "Failed to open " << SYSFS_EXT4_CASEFOLD; |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | if (!(android::base::Trim(casefold_support) == "supported")) { |
| 543 | LERROR << "Current ext4 casefolding not supported by kernel"; |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | if (!tune2fs_available()) { |
| 548 | LERROR << "Unable to enable ext4 casefold on " << blk_device |
| 549 | << " because " TUNE2FS_BIN " is missing"; |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | LINFO << "Enabling ext4 casefold on " << blk_device; |
| 554 | |
| 555 | const char* argv[] = {TUNE2FS_BIN, "-O", "casefold", "-E", "encoding=utf8", blk_device.c_str()}; |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 556 | if (!run_command(argv, ARRAY_SIZE(argv))) { |
Daniel Rosenberg | 8775ce0 | 2019-11-19 20:30:06 -0800 | [diff] [blame] | 557 | LERROR << "Failed to run " TUNE2FS_BIN " to enable " |
| 558 | << "ext4 casefold on " << blk_device; |
| 559 | *fs_stat |= FS_STAT_ENABLE_CASEFOLD_FAILED; |
| 560 | } |
| 561 | } |
| 562 | |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 563 | static bool resize2fs_available(void) { |
| 564 | return access(RESIZE2FS_BIN, X_OK) == 0; |
| 565 | } |
| 566 | |
| 567 | // Enable metadata_csum |
| 568 | static void tune_metadata_csum(const std::string& blk_device, const FstabEntry& entry, |
| 569 | const struct ext4_super_block* sb, int* fs_stat) { |
| 570 | bool has_meta_csum = |
| 571 | (sb->s_feature_ro_compat & cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) != 0; |
| 572 | bool want_meta_csum = entry.fs_mgr_flags.ext_meta_csum; |
| 573 | |
| 574 | if (has_meta_csum || !want_meta_csum) return; |
| 575 | |
| 576 | if (!tune2fs_available()) { |
| 577 | LERROR << "Unable to enable metadata_csum on " << blk_device |
| 578 | << " because " TUNE2FS_BIN " is missing"; |
| 579 | return; |
| 580 | } |
| 581 | if (!resize2fs_available()) { |
| 582 | LERROR << "Unable to enable metadata_csum on " << blk_device |
| 583 | << " because " RESIZE2FS_BIN " is missing"; |
| 584 | return; |
| 585 | } |
| 586 | |
| 587 | LINFO << "Enabling ext4 metadata_csum on " << blk_device; |
| 588 | |
Tom Cherry | 2d45166 | 2020-07-27 11:20:29 -0700 | [diff] [blame] | 589 | // Must give `-T now` to prevent last_fsck_time from growing too large, |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 590 | // otherwise, tune2fs won't enable metadata_csum. |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 591 | const char* tune2fs_args[] = {TUNE2FS_BIN, "-O", "metadata_csum,64bit,extent", |
Jaegeuk Kim | 5283816 | 2020-02-24 19:58:22 -0800 | [diff] [blame] | 592 | "-T", "now", blk_device.c_str()}; |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 593 | const char* resize2fs_args[] = {RESIZE2FS_BIN, "-b", blk_device.c_str()}; |
| 594 | |
| 595 | if (!run_command(tune2fs_args, ARRAY_SIZE(tune2fs_args))) { |
| 596 | LERROR << "Failed to run " TUNE2FS_BIN " to enable " |
| 597 | << "ext4 metadata_csum on " << blk_device; |
| 598 | *fs_stat |= FS_STAT_ENABLE_METADATA_CSUM_FAILED; |
| 599 | } else if (!run_command(resize2fs_args, ARRAY_SIZE(resize2fs_args))) { |
| 600 | LERROR << "Failed to run " RESIZE2FS_BIN " to enable " |
| 601 | << "ext4 metadata_csum on " << blk_device; |
| 602 | *fs_stat |= FS_STAT_ENABLE_METADATA_CSUM_FAILED; |
| 603 | } |
| 604 | } |
| 605 | |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 606 | // Read the primary superblock from an f2fs filesystem. On failure return |
| 607 | // false. If it's not an f2fs filesystem, also set FS_STAT_INVALID_MAGIC. |
| 608 | #define F2FS_BLKSIZE 4096 |
| 609 | #define F2FS_SUPER_OFFSET 1024 |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 610 | static bool read_f2fs_superblock(const std::string& blk_device, int* fs_stat) { |
| 611 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC))); |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 612 | __le32 sb1, sb2; |
| 613 | |
| 614 | if (fd < 0) { |
| 615 | PERROR << "Failed to open '" << blk_device << "'"; |
| 616 | return false; |
| 617 | } |
| 618 | |
Mark Salyzyn | 60a76f3 | 2019-03-14 15:34:35 -0700 | [diff] [blame] | 619 | if (TEMP_FAILURE_RETRY(pread(fd, &sb1, sizeof(sb1), F2FS_SUPER_OFFSET)) != sizeof(sb1)) { |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 620 | PERROR << "Can't read '" << blk_device << "' superblock1"; |
| 621 | return false; |
| 622 | } |
Mark Salyzyn | 60a76f3 | 2019-03-14 15:34:35 -0700 | [diff] [blame] | 623 | if (TEMP_FAILURE_RETRY(pread(fd, &sb2, sizeof(sb2), F2FS_BLKSIZE + F2FS_SUPER_OFFSET)) != |
| 624 | sizeof(sb2)) { |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 625 | PERROR << "Can't read '" << blk_device << "' superblock2"; |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | if (sb1 != cpu_to_le32(F2FS_SUPER_MAGIC) && sb2 != cpu_to_le32(F2FS_SUPER_MAGIC)) { |
| 630 | LINFO << "Invalid f2fs superblock on '" << blk_device << "'"; |
| 631 | *fs_stat |= FS_STAT_INVALID_MAGIC; |
| 632 | return false; |
| 633 | } |
| 634 | return true; |
| 635 | } |
| 636 | |
Mark Salyzyn | 60a76f3 | 2019-03-14 15:34:35 -0700 | [diff] [blame] | 637 | // exported silent version of the above that just answer the question is_f2fs |
| 638 | bool fs_mgr_is_f2fs(const std::string& blk_device) { |
| 639 | android::base::ErrnoRestorer restore; |
| 640 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC))); |
| 641 | if (fd < 0) return false; |
| 642 | __le32 sb; |
| 643 | if (TEMP_FAILURE_RETRY(pread(fd, &sb, sizeof(sb), F2FS_SUPER_OFFSET)) != sizeof(sb)) { |
| 644 | return false; |
| 645 | } |
| 646 | if (sb == cpu_to_le32(F2FS_SUPER_MAGIC)) return true; |
| 647 | if (TEMP_FAILURE_RETRY(pread(fd, &sb, sizeof(sb), F2FS_BLKSIZE + F2FS_SUPER_OFFSET)) != |
| 648 | sizeof(sb)) { |
| 649 | return false; |
| 650 | } |
| 651 | return sb == cpu_to_le32(F2FS_SUPER_MAGIC); |
| 652 | } |
| 653 | |
Jaegeuk Kim | 05ca915 | 2021-04-02 21:28:51 -0700 | [diff] [blame] | 654 | static void SetReadAheadSize(const std::string& entry_block_device, off64_t size_kb) { |
| 655 | std::string block_device; |
| 656 | if (!Realpath(entry_block_device, &block_device)) { |
| 657 | PERROR << "Failed to realpath " << entry_block_device; |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | static constexpr std::string_view kDevBlockPrefix("/dev/block/"); |
| 662 | if (!android::base::StartsWith(block_device, kDevBlockPrefix)) { |
| 663 | LWARNING << block_device << " is not a block device"; |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | DeviceMapper& dm = DeviceMapper::Instance(); |
| 668 | while (true) { |
| 669 | std::string block_name = block_device; |
| 670 | if (android::base::StartsWith(block_device, kDevBlockPrefix)) { |
| 671 | block_name = block_device.substr(kDevBlockPrefix.length()); |
| 672 | } |
| 673 | std::string sys_partition = |
| 674 | android::base::StringPrintf("/sys/class/block/%s/partition", block_name.c_str()); |
| 675 | struct stat info; |
| 676 | if (lstat(sys_partition.c_str(), &info) == 0) { |
| 677 | // it has a partition like "sda12". |
| 678 | block_name += "/.."; |
| 679 | } |
| 680 | std::string sys_ra = android::base::StringPrintf("/sys/class/block/%s/queue/read_ahead_kb", |
| 681 | block_name.c_str()); |
| 682 | std::string size = android::base::StringPrintf("%llu", (long long)size_kb); |
| 683 | android::base::WriteStringToFile(size, sys_ra.c_str()); |
| 684 | LINFO << "Set readahead_kb: " << size << " on " << sys_ra; |
| 685 | |
| 686 | auto parent = dm.GetParentBlockDeviceByPath(block_device); |
| 687 | if (!parent) { |
| 688 | return; |
| 689 | } |
| 690 | block_device = *parent; |
| 691 | } |
| 692 | } |
| 693 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 694 | // |
| 695 | // Prepare the filesystem on the given block device to be mounted. |
| 696 | // |
| 697 | // If the "check" option was given in the fstab record, or it seems that the |
| 698 | // filesystem was uncleanly shut down, we'll run fsck on the filesystem. |
| 699 | // |
| 700 | // If needed, we'll also enable (or disable) filesystem features as specified by |
| 701 | // the fstab record. |
| 702 | // |
Tom Cherry | a7f1a9f | 2020-11-19 11:39:39 -0800 | [diff] [blame] | 703 | static int prepare_fs_for_mount(const std::string& blk_device, const FstabEntry& entry, |
| 704 | const std::string& alt_mount_point = "") { |
| 705 | auto& mount_point = alt_mount_point.empty() ? entry.mount_point : alt_mount_point; |
| 706 | // We need this because sometimes we have legacy symlinks that are |
| 707 | // lingering around and need cleaning up. |
| 708 | struct stat info; |
| 709 | if (lstat(mount_point.c_str(), &info) == 0 && (info.st_mode & S_IFMT) == S_IFLNK) { |
| 710 | unlink(mount_point.c_str()); |
| 711 | } |
| 712 | mkdir(mount_point.c_str(), 0755); |
| 713 | |
Jaegeuk Kim | 05ca915 | 2021-04-02 21:28:51 -0700 | [diff] [blame] | 714 | // Don't need to return error, since it's a salt |
| 715 | if (entry.readahead_size_kb != -1) { |
| 716 | SetReadAheadSize(blk_device, entry.readahead_size_kb); |
| 717 | } |
| 718 | |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 719 | int fs_stat = 0; |
| 720 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 721 | if (is_extfs(entry.fs_type)) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 722 | struct ext4_super_block sb; |
| 723 | |
Eric Biggers | dc3e897 | 2020-08-04 11:11:13 -0700 | [diff] [blame] | 724 | if (read_ext4_superblock(blk_device, &sb, &fs_stat)) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 725 | if ((sb.s_feature_incompat & EXT4_FEATURE_INCOMPAT_RECOVER) != 0 || |
| 726 | (sb.s_state & EXT4_VALID_FS) == 0) { |
| 727 | LINFO << "Filesystem on " << blk_device << " was not cleanly shutdown; " |
| 728 | << "state flags: 0x" << std::hex << sb.s_state << ", " |
| 729 | << "incompat feature flags: 0x" << std::hex << sb.s_feature_incompat; |
| 730 | fs_stat |= FS_STAT_UNCLEAN_SHUTDOWN; |
| 731 | } |
| 732 | |
| 733 | // Note: quotas should be enabled before running fsck. |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 734 | tune_quota(blk_device, entry, &sb, &fs_stat); |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 735 | } else { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 736 | return fs_stat; |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 737 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 738 | } else if (is_f2fs(entry.fs_type)) { |
Jaegeuk Kim | 5d14b98 | 2018-11-21 13:24:10 -0800 | [diff] [blame] | 739 | if (!read_f2fs_superblock(blk_device, &fs_stat)) { |
| 740 | return fs_stat; |
| 741 | } |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 742 | } |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 743 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 744 | if (entry.fs_mgr_flags.check || |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 745 | (fs_stat & (FS_STAT_UNCLEAN_SHUTDOWN | FS_STAT_QUOTA_ENABLED))) { |
Tom Cherry | a7f1a9f | 2020-11-19 11:39:39 -0800 | [diff] [blame] | 746 | check_fs(blk_device, entry.fs_type, mount_point, &fs_stat); |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 749 | if (is_extfs(entry.fs_type) && |
Tom Cherry | 685c2c7 | 2019-02-04 13:33:32 -0800 | [diff] [blame] | 750 | (entry.reserved_size != 0 || entry.fs_mgr_flags.file_encryption || |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 751 | entry.fs_mgr_flags.fs_verity || entry.fs_mgr_flags.ext_meta_csum)) { |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 752 | struct ext4_super_block sb; |
| 753 | |
Eric Biggers | dc3e897 | 2020-08-04 11:11:13 -0700 | [diff] [blame] | 754 | if (read_ext4_superblock(blk_device, &sb, &fs_stat)) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 755 | tune_reserved_size(blk_device, entry, &sb, &fs_stat); |
| 756 | tune_encrypt(blk_device, entry, &sb, &fs_stat); |
Leo Liou | 0b721d3 | 2019-01-15 20:43:37 +0800 | [diff] [blame] | 757 | tune_verity(blk_device, entry, &sb, &fs_stat); |
Jaegeuk Kim | 5ba5b90 | 2020-08-14 12:34:36 -0700 | [diff] [blame] | 758 | tune_casefold(blk_device, entry, &sb, &fs_stat); |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 759 | tune_metadata_csum(blk_device, entry, &sb, &fs_stat); |
Eric Biggers | 8d3bcd4 | 2017-07-05 12:21:15 -0700 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | return fs_stat; |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 764 | } |
| 765 | |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 766 | // Mark the given block device as read-only, using the BLKROSET ioctl. |
Yifan Hong | ccdba57 | 2019-01-08 13:33:52 -0800 | [diff] [blame] | 767 | bool fs_mgr_set_blk_ro(const std::string& blockdev, bool readonly) { |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 768 | unique_fd fd(TEMP_FAILURE_RETRY(open(blockdev.c_str(), O_RDONLY | O_CLOEXEC))); |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 769 | if (fd < 0) { |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 770 | return false; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Yifan Hong | ccdba57 | 2019-01-08 13:33:52 -0800 | [diff] [blame] | 773 | int ON = readonly; |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 774 | return ioctl(fd, BLKROSET, &ON) == 0; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Bowgo Tsai | d1fe3bd | 2017-07-05 15:37:15 +0800 | [diff] [blame] | 777 | // Orange state means the device is unlocked, see the following link for details. |
| 778 | // https://source.android.com/security/verifiedboot/verified-boot#device_state |
| 779 | bool fs_mgr_is_device_unlocked() { |
| 780 | std::string verified_boot_state; |
| 781 | if (fs_mgr_get_boot_config("verifiedbootstate", &verified_boot_state)) { |
| 782 | return verified_boot_state == "orange"; |
| 783 | } |
| 784 | return false; |
| 785 | } |
| 786 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 787 | // __mount(): wrapper around the mount() system call which also |
| 788 | // sets the underlying block device to read-only if the mount is read-only. |
| 789 | // See "man 2 mount" for return values. |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 790 | static int __mount(const std::string& source, const std::string& target, const FstabEntry& entry) { |
Mark Salyzyn | 69a5bd4 | 2017-06-28 13:36:52 -0700 | [diff] [blame] | 791 | errno = 0; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 792 | unsigned long mountflags = entry.flags; |
Daniel Rosenberg | 5620c6c | 2019-01-16 16:28:21 -0800 | [diff] [blame] | 793 | int ret = 0; |
| 794 | int save_errno = 0; |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 795 | int gc_allowance = 0; |
| 796 | std::string opts; |
Jerry Wong | 1f3e92c | 2021-07-09 09:53:21 -0700 | [diff] [blame] | 797 | std::string checkpoint_opts; |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 798 | bool try_f2fs_gc_allowance = is_f2fs(entry.fs_type) && entry.fs_checkpoint_opts.length() > 0; |
Jerry Wong | 1f3e92c | 2021-07-09 09:53:21 -0700 | [diff] [blame] | 799 | bool try_f2fs_fallback = false; |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 800 | Timer t; |
| 801 | |
Daniel Rosenberg | 5620c6c | 2019-01-16 16:28:21 -0800 | [diff] [blame] | 802 | do { |
Jerry Wong | 1f3e92c | 2021-07-09 09:53:21 -0700 | [diff] [blame] | 803 | if (save_errno == EINVAL && (try_f2fs_gc_allowance || try_f2fs_fallback)) { |
| 804 | PINFO << "Kernel does not support " << checkpoint_opts << ", trying without."; |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 805 | try_f2fs_gc_allowance = false; |
Jerry Wong | 1f3e92c | 2021-07-09 09:53:21 -0700 | [diff] [blame] | 806 | // Attempt without gc allowance before dropping. |
| 807 | try_f2fs_fallback = !try_f2fs_fallback; |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 808 | } |
| 809 | if (try_f2fs_gc_allowance) { |
Jerry Wong | 1f3e92c | 2021-07-09 09:53:21 -0700 | [diff] [blame] | 810 | checkpoint_opts = entry.fs_checkpoint_opts + ":" + std::to_string(gc_allowance) + "%"; |
| 811 | } else if (try_f2fs_fallback) { |
| 812 | checkpoint_opts = entry.fs_checkpoint_opts; |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 813 | } else { |
Jerry Wong | 1f3e92c | 2021-07-09 09:53:21 -0700 | [diff] [blame] | 814 | checkpoint_opts = ""; |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 815 | } |
Jerry Wong | 1f3e92c | 2021-07-09 09:53:21 -0700 | [diff] [blame] | 816 | opts = entry.fs_options + checkpoint_opts; |
Daniel Rosenberg | 5620c6c | 2019-01-16 16:28:21 -0800 | [diff] [blame] | 817 | if (save_errno == EAGAIN) { |
| 818 | PINFO << "Retrying mount (source=" << source << ",target=" << target |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 819 | << ",type=" << entry.fs_type << ", gc_allowance=" << gc_allowance << "%)=" << ret |
| 820 | << "(" << save_errno << ")"; |
Daniel Rosenberg | 5620c6c | 2019-01-16 16:28:21 -0800 | [diff] [blame] | 821 | } |
| 822 | ret = mount(source.c_str(), target.c_str(), entry.fs_type.c_str(), mountflags, |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 823 | opts.c_str()); |
Daniel Rosenberg | 5620c6c | 2019-01-16 16:28:21 -0800 | [diff] [blame] | 824 | save_errno = errno; |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 825 | if (try_f2fs_gc_allowance) gc_allowance += 10; |
| 826 | } while ((ret && save_errno == EAGAIN && gc_allowance <= 100) || |
Jerry Wong | 1f3e92c | 2021-07-09 09:53:21 -0700 | [diff] [blame] | 827 | (ret && save_errno == EINVAL && (try_f2fs_gc_allowance || try_f2fs_fallback))); |
Mark Salyzyn | 8243f66 | 2018-09-26 14:27:04 -0700 | [diff] [blame] | 828 | const char* target_missing = ""; |
| 829 | const char* source_missing = ""; |
| 830 | if (save_errno == ENOENT) { |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 831 | if (access(target.c_str(), F_OK)) { |
Mark Salyzyn | 8243f66 | 2018-09-26 14:27:04 -0700 | [diff] [blame] | 832 | target_missing = "(missing)"; |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 833 | } else if (access(source.c_str(), F_OK)) { |
Mark Salyzyn | 8243f66 | 2018-09-26 14:27:04 -0700 | [diff] [blame] | 834 | source_missing = "(missing)"; |
| 835 | } |
| 836 | errno = save_errno; |
| 837 | } |
| 838 | PINFO << __FUNCTION__ << "(source=" << source << source_missing << ",target=" << target |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 839 | << target_missing << ",type=" << entry.fs_type << ")=" << ret; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 840 | if ((ret == 0) && (mountflags & MS_RDONLY) != 0) { |
Sami Tolvanen | 214f33b | 2014-12-18 16:15:30 +0000 | [diff] [blame] | 841 | fs_mgr_set_blk_ro(source); |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 842 | } |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 843 | android::base::SetProperty("ro.boottime.init.mount." + Basename(target), |
| 844 | std::to_string(t.duration().count())); |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 845 | errno = save_errno; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 849 | static bool fs_match(const std::string& in1, const std::string& in2) { |
| 850 | if (in1.empty() || in2.empty()) { |
| 851 | return false; |
| 852 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 853 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 854 | auto in1_end = in1.size() - 1; |
| 855 | while (in1_end > 0 && in1[in1_end] == '/') { |
| 856 | in1_end--; |
| 857 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 858 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 859 | auto in2_end = in2.size() - 1; |
| 860 | while (in2_end > 0 && in2[in2_end] == '/') { |
| 861 | in2_end--; |
| 862 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 863 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 864 | if (in1_end != in2_end) { |
| 865 | return false; |
| 866 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 867 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 868 | for (size_t i = 0; i <= in1_end; ++i) { |
| 869 | if (in1[i] != in2[i]) { |
| 870 | return false; |
| 871 | } |
| 872 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 873 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 874 | return true; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 875 | } |
| 876 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 877 | // Tries to mount any of the consecutive fstab entries that match |
| 878 | // the mountpoint of the one given by fstab[start_idx]. |
| 879 | // |
| 880 | // end_idx: On return, will be the last entry that was looked at. |
| 881 | // attempted_idx: On return, will indicate which fstab entry |
| 882 | // succeeded. In case of failure, it will be the start_idx. |
| 883 | // Sets errno to match the 1st mount failure on failure. |
| 884 | static bool mount_with_alternatives(const Fstab& fstab, int start_idx, int* end_idx, |
| 885 | int* attempted_idx) { |
Yi Kong | e93040c | 2018-12-14 15:40:08 -0800 | [diff] [blame] | 886 | unsigned long i; |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 887 | int mount_errno = 0; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 888 | bool mounted = false; |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 889 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 890 | // Hunt down an fstab entry for the same mount point that might succeed. |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 891 | for (i = start_idx; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 892 | // We required that fstab entries for the same mountpoint be consecutive. |
| 893 | i < fstab.size() && fstab[start_idx].mount_point == fstab[i].mount_point; i++) { |
| 894 | // Don't try to mount/encrypt the same mount point again. |
| 895 | // Deal with alternate entries for the same point which are required to be all following |
| 896 | // each other. |
| 897 | if (mounted) { |
| 898 | LERROR << __FUNCTION__ << "(): skipping fstab dup mountpoint=" << fstab[i].mount_point |
| 899 | << " rec[" << i << "].fs_type=" << fstab[i].fs_type << " already mounted as " |
| 900 | << fstab[*attempted_idx].fs_type; |
| 901 | continue; |
| 902 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 903 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 904 | int fs_stat = prepare_fs_for_mount(fstab[i].blk_device, fstab[i]); |
| 905 | if (fs_stat & FS_STAT_INVALID_MAGIC) { |
| 906 | LERROR << __FUNCTION__ |
| 907 | << "(): skipping mount due to invalid magic, mountpoint=" << fstab[i].mount_point |
| 908 | << " blk_dev=" << realpath(fstab[i].blk_device) << " rec[" << i |
| 909 | << "].fs_type=" << fstab[i].fs_type; |
Eric Biggers | 63fb195 | 2021-11-08 16:38:53 -0800 | [diff] [blame] | 910 | mount_errno = EINVAL; // continue bootup for metadata encryption |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 911 | continue; |
| 912 | } |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 913 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 914 | int retry_count = 2; |
| 915 | while (retry_count-- > 0) { |
| 916 | if (!__mount(fstab[i].blk_device, fstab[i].mount_point, fstab[i])) { |
| 917 | *attempted_idx = i; |
| 918 | mounted = true; |
| 919 | if (i != start_idx) { |
| 920 | LERROR << __FUNCTION__ << "(): Mounted " << fstab[i].blk_device << " on " |
| 921 | << fstab[i].mount_point << " with fs_type=" << fstab[i].fs_type |
| 922 | << " instead of " << fstab[start_idx].fs_type; |
NIEJuhu | 5c31ffe | 2017-03-09 12:19:08 +0800 | [diff] [blame] | 923 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 924 | fs_stat &= ~FS_STAT_FULL_MOUNT_FAILED; |
| 925 | mount_errno = 0; |
| 926 | break; |
| 927 | } else { |
| 928 | if (retry_count <= 0) break; // run check_fs only once |
| 929 | fs_stat |= FS_STAT_FULL_MOUNT_FAILED; |
| 930 | // back up the first errno for crypto decisions. |
| 931 | if (mount_errno == 0) { |
| 932 | mount_errno = errno; |
| 933 | } |
| 934 | // retry after fsck |
| 935 | check_fs(fstab[i].blk_device, fstab[i].fs_type, fstab[i].mount_point, &fs_stat); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 936 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 937 | } |
| 938 | log_fs_stat(fstab[i].blk_device, fs_stat); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* Adjust i for the case where it was still withing the recs[] */ |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 942 | if (i < fstab.size()) --i; |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 943 | |
| 944 | *end_idx = i; |
| 945 | if (!mounted) { |
| 946 | *attempted_idx = start_idx; |
| 947 | errno = mount_errno; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 948 | return false; |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 949 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 950 | return true; |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 953 | static bool TranslateExtLabels(FstabEntry* entry) { |
| 954 | if (!StartsWith(entry->blk_device, "LABEL=")) { |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 955 | return true; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 956 | } |
| 957 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 958 | std::string label = entry->blk_device.substr(6); |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 959 | if (label.size() > 16) { |
| 960 | LERROR << "FS label is longer than allowed by filesystem"; |
| 961 | return false; |
| 962 | } |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 963 | |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 964 | auto blockdir = std::unique_ptr<DIR, decltype(&closedir)>{opendir("/dev/block"), closedir}; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 965 | if (!blockdir) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 966 | LERROR << "couldn't open /dev/block"; |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 967 | return false; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 968 | } |
| 969 | |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 970 | struct dirent* ent; |
| 971 | while ((ent = readdir(blockdir.get()))) { |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 972 | if (ent->d_type != DT_BLK) |
| 973 | continue; |
| 974 | |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 975 | unique_fd fd(TEMP_FAILURE_RETRY( |
| 976 | openat(dirfd(blockdir.get()), ent->d_name, O_RDONLY | O_CLOEXEC))); |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 977 | if (fd < 0) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 978 | LERROR << "Cannot open block device /dev/block/" << ent->d_name; |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 979 | return false; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 982 | ext4_super_block super_block; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 983 | if (TEMP_FAILURE_RETRY(lseek(fd, 1024, SEEK_SET)) < 0 || |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 984 | TEMP_FAILURE_RETRY(read(fd, &super_block, sizeof(super_block))) != |
| 985 | sizeof(super_block)) { |
| 986 | // Probably a loopback device or something else without a readable superblock. |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 987 | continue; |
| 988 | } |
| 989 | |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 990 | if (super_block.s_magic != EXT4_SUPER_MAGIC) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 991 | LINFO << "/dev/block/" << ent->d_name << " not ext{234}"; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 992 | continue; |
| 993 | } |
| 994 | |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 995 | if (label == super_block.s_volume_name) { |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 996 | std::string new_blk_device = "/dev/block/"s + ent->d_name; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 997 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 998 | LINFO << "resolved label " << entry->blk_device << " to " << new_blk_device; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 999 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1000 | entry->blk_device = new_blk_device; |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 1001 | return true; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
Tom Cherry | 0d2621f | 2018-12-04 13:15:09 -0800 | [diff] [blame] | 1005 | return false; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1008 | static bool should_use_metadata_encryption(const FstabEntry& entry) { |
Eric Biggers | 63fb195 | 2021-11-08 16:38:53 -0800 | [diff] [blame] | 1009 | return !entry.metadata_key_dir.empty() && entry.fs_mgr_flags.file_encryption; |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1012 | // Check to see if a mountable volume has encryption requirements |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1013 | static int handle_encryptable(const FstabEntry& entry) { |
Eric Biggers | 63fb195 | 2021-11-08 16:38:53 -0800 | [diff] [blame] | 1014 | if (should_use_metadata_encryption(entry)) { |
Jaegeuk Kim | bdb981d | 2021-12-07 11:23:52 -0800 | [diff] [blame] | 1015 | if (umount_retry(entry.mount_point)) { |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 1016 | return FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION; |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 1017 | } |
Jaegeuk Kim | bdb981d | 2021-12-07 11:23:52 -0800 | [diff] [blame] | 1018 | PERROR << "Could not umount " << entry.mount_point << " - fail since can't encrypt"; |
| 1019 | return FS_MGR_MNTALL_FAIL; |
Eric Biggers | 63fb195 | 2021-11-08 16:38:53 -0800 | [diff] [blame] | 1020 | } else if (entry.fs_mgr_flags.file_encryption) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1021 | LINFO << entry.mount_point << " is file encrypted"; |
Paul Lawrence | 6908018 | 2016-02-02 10:31:30 -0800 | [diff] [blame] | 1022 | return FS_MGR_MNTALL_DEV_FILE_ENCRYPTED; |
Paul Lawrence | 1098aac | 2016-03-04 15:52:33 -0800 | [diff] [blame] | 1023 | } else { |
| 1024 | return FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE; |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1025 | } |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Paul Crowley | 9acab5a | 2020-08-14 11:05:05 -0700 | [diff] [blame] | 1028 | static void set_type_property(int status) { |
| 1029 | switch (status) { |
Paul Crowley | 9acab5a | 2020-08-14 11:05:05 -0700 | [diff] [blame] | 1030 | case FS_MGR_MNTALL_DEV_FILE_ENCRYPTED: |
| 1031 | case FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED: |
| 1032 | case FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION: |
| 1033 | SetProperty("ro.crypto.type", "file"); |
| 1034 | break; |
| 1035 | } |
| 1036 | } |
| 1037 | |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 1038 | static bool call_vdc(const std::vector<std::string>& args, int* ret) { |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 1039 | std::vector<char const*> argv; |
| 1040 | argv.emplace_back("/system/bin/vdc"); |
| 1041 | for (auto& arg : args) { |
| 1042 | argv.emplace_back(arg.c_str()); |
| 1043 | } |
| 1044 | LOG(INFO) << "Calling: " << android::base::Join(argv, ' '); |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 1045 | int err = logwrap_fork_execvp(argv.size(), argv.data(), ret, false, LOG_ALOG, false, nullptr); |
Daniel Rosenberg | 4c93b25 | 2018-08-28 01:41:18 -0700 | [diff] [blame] | 1046 | if (err != 0) { |
| 1047 | LOG(ERROR) << "vdc call failed with error code: " << err; |
| 1048 | return false; |
| 1049 | } |
| 1050 | LOG(DEBUG) << "vdc finished successfully"; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 1051 | if (ret != nullptr) { |
| 1052 | *ret = WEXITSTATUS(*ret); |
| 1053 | } |
Daniel Rosenberg | 4c93b25 | 2018-08-28 01:41:18 -0700 | [diff] [blame] | 1054 | return true; |
| 1055 | } |
| 1056 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1057 | bool fs_mgr_update_logical_partition(FstabEntry* entry) { |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 1058 | // Logical partitions are specified with a named partition rather than a |
| 1059 | // block device, so if the block device is a path, then it has already |
| 1060 | // been updated. |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1061 | if (entry->blk_device[0] == '/') { |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
| 1064 | |
David Anderson | b5acb1a | 2018-06-26 17:07:55 -0700 | [diff] [blame] | 1065 | DeviceMapper& dm = DeviceMapper::Instance(); |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 1066 | std::string device_name; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1067 | if (!dm.GetDmDevicePathByName(entry->blk_device, &device_name)) { |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 1068 | return false; |
| 1069 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1070 | |
| 1071 | entry->blk_device = device_name; |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1075 | static bool SupportsCheckpoint(FstabEntry* entry) { |
| 1076 | return entry->fs_mgr_flags.checkpoint_blk || entry->fs_mgr_flags.checkpoint_fs; |
| 1077 | } |
| 1078 | |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1079 | class CheckpointManager { |
| 1080 | public: |
Paul Lawrence | 2055364 | 2020-06-12 07:32:23 -0700 | [diff] [blame] | 1081 | CheckpointManager(int needs_checkpoint = -1, bool metadata_encrypted = false) |
| 1082 | : needs_checkpoint_(needs_checkpoint), metadata_encrypted_(metadata_encrypted) {} |
Daniel Rosenberg | 4c93b25 | 2018-08-28 01:41:18 -0700 | [diff] [blame] | 1083 | |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1084 | bool NeedsCheckpoint() { |
| 1085 | if (needs_checkpoint_ != UNKNOWN) { |
| 1086 | return needs_checkpoint_ == YES; |
| 1087 | } |
| 1088 | if (!call_vdc({"checkpoint", "needsCheckpoint"}, &needs_checkpoint_)) { |
| 1089 | LERROR << "Failed to find if checkpointing is needed. Assuming no."; |
| 1090 | needs_checkpoint_ = NO; |
| 1091 | } |
| 1092 | return needs_checkpoint_ == YES; |
| 1093 | } |
| 1094 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1095 | bool Update(FstabEntry* entry, const std::string& block_device = std::string()) { |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1096 | if (!SupportsCheckpoint(entry)) { |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1097 | return true; |
Daniel Rosenberg | 4c93b25 | 2018-08-28 01:41:18 -0700 | [diff] [blame] | 1098 | } |
Paul Lawrence | 786d0b2 | 2018-09-21 10:49:38 -0700 | [diff] [blame] | 1099 | |
Paul Lawrence | 2055364 | 2020-06-12 07:32:23 -0700 | [diff] [blame] | 1100 | if (entry->fs_mgr_flags.checkpoint_blk && !metadata_encrypted_) { |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 1101 | call_vdc({"checkpoint", "restoreCheckpoint", entry->blk_device}, nullptr); |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1104 | if (!NeedsCheckpoint()) { |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1105 | return true; |
| 1106 | } |
| 1107 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1108 | if (!UpdateCheckpointPartition(entry, block_device)) { |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1109 | LERROR << "Could not set up checkpoint partition, skipping!"; |
Paul Lawrence | 786d0b2 | 2018-09-21 10:49:38 -0700 | [diff] [blame] | 1110 | return false; |
| 1111 | } |
| 1112 | |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1113 | return true; |
| 1114 | } |
| 1115 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1116 | bool Revert(FstabEntry* entry) { |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1117 | if (!SupportsCheckpoint(entry)) { |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1118 | return true; |
Paul Lawrence | 786d0b2 | 2018-09-21 10:49:38 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1121 | if (device_map_.find(entry->blk_device) == device_map_.end()) { |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1122 | return true; |
Paul Lawrence | 786d0b2 | 2018-09-21 10:49:38 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1125 | std::string bow_device = entry->blk_device; |
| 1126 | entry->blk_device = device_map_[bow_device]; |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1127 | device_map_.erase(bow_device); |
| 1128 | |
Paul Lawrence | 786d0b2 | 2018-09-21 10:49:38 -0700 | [diff] [blame] | 1129 | DeviceMapper& dm = DeviceMapper::Instance(); |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1130 | if (!dm.DeleteDevice("bow")) { |
| 1131 | PERROR << "Failed to remove bow device"; |
Paul Lawrence | 786d0b2 | 2018-09-21 10:49:38 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1134 | return true; |
Daniel Rosenberg | 4c93b25 | 2018-08-28 01:41:18 -0700 | [diff] [blame] | 1135 | } |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1136 | |
| 1137 | private: |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1138 | bool UpdateCheckpointPartition(FstabEntry* entry, const std::string& block_device) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1139 | if (entry->fs_mgr_flags.checkpoint_fs) { |
| 1140 | if (is_f2fs(entry->fs_type)) { |
Daniel Rosenberg | 7c59f1a | 2020-02-28 18:37:22 -0800 | [diff] [blame] | 1141 | entry->fs_checkpoint_opts = ",checkpoint=disable"; |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1142 | } else { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1143 | LERROR << entry->fs_type << " does not implement checkpoints."; |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1144 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1145 | } else if (entry->fs_mgr_flags.checkpoint_blk) { |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1146 | auto actual_block_device = block_device.empty() ? entry->blk_device : block_device; |
| 1147 | if (fs_mgr_find_bow_device(actual_block_device).empty()) { |
| 1148 | unique_fd fd( |
| 1149 | TEMP_FAILURE_RETRY(open(entry->blk_device.c_str(), O_RDONLY | O_CLOEXEC))); |
| 1150 | if (fd < 0) { |
| 1151 | PERROR << "Cannot open device " << entry->blk_device; |
| 1152 | return false; |
| 1153 | } |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1154 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1155 | uint64_t size = get_block_device_size(fd) / 512; |
| 1156 | if (!size) { |
| 1157 | PERROR << "Cannot get device size"; |
| 1158 | return false; |
| 1159 | } |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1160 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1161 | android::dm::DmTable table; |
Paul Lawrence | 2f0c6cb | 2020-06-01 12:58:04 -0700 | [diff] [blame] | 1162 | auto bowTarget = |
| 1163 | std::make_unique<android::dm::DmTargetBow>(0, size, entry->blk_device); |
| 1164 | |
| 1165 | // dm-bow uses the first block as a log record, and relocates the real first block |
| 1166 | // elsewhere. For metadata encrypted devices, dm-bow sits below dm-default-key, and |
| 1167 | // for post Android Q devices dm-default-key uses a block size of 4096 always. |
| 1168 | // So if dm-bow's block size, which by default is the block size of the underlying |
| 1169 | // hardware, is less than dm-default-key's, blocks will get broken up and I/O will |
| 1170 | // fail as it won't be data_unit_size aligned. |
| 1171 | // However, since it is possible there is an already shipping non |
| 1172 | // metadata-encrypted device with smaller blocks, we must not change this for |
| 1173 | // devices shipped with Q or earlier unless they explicitly selected dm-default-key |
| 1174 | // v2 |
Paul Lawrence | 2f0c6cb | 2020-06-01 12:58:04 -0700 | [diff] [blame] | 1175 | unsigned int options_format_version = android::base::GetUintProperty<unsigned int>( |
| 1176 | "ro.crypto.dm_default_key.options_format.version", |
Eric Biggers | 75ebdd9 | 2020-08-10 11:14:51 -0700 | [diff] [blame] | 1177 | (android::fscrypt::GetFirstApiLevel() <= __ANDROID_API_Q__ ? 1 : 2)); |
Paul Lawrence | 2f0c6cb | 2020-06-01 12:58:04 -0700 | [diff] [blame] | 1178 | if (options_format_version > 1) { |
| 1179 | bowTarget->SetBlockSize(4096); |
| 1180 | } |
| 1181 | |
| 1182 | if (!table.AddTarget(std::move(bowTarget))) { |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1183 | LERROR << "Failed to add bow target"; |
| 1184 | return false; |
| 1185 | } |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1186 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1187 | DeviceMapper& dm = DeviceMapper::Instance(); |
| 1188 | if (!dm.CreateDevice("bow", table)) { |
| 1189 | PERROR << "Failed to create bow device"; |
| 1190 | return false; |
| 1191 | } |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1192 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1193 | std::string name; |
| 1194 | if (!dm.GetDmDevicePathByName("bow", &name)) { |
| 1195 | PERROR << "Failed to get bow device name"; |
| 1196 | return false; |
| 1197 | } |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1198 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1199 | device_map_[name] = entry->blk_device; |
| 1200 | entry->blk_device = name; |
| 1201 | } |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
| 1206 | enum { UNKNOWN = -1, NO = 0, YES = 1 }; |
| 1207 | int needs_checkpoint_; |
Paul Lawrence | 2055364 | 2020-06-12 07:32:23 -0700 | [diff] [blame] | 1208 | bool metadata_encrypted_; |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1209 | std::map<std::string, std::string> device_map_; |
| 1210 | }; |
Daniel Rosenberg | 4c93b25 | 2018-08-28 01:41:18 -0700 | [diff] [blame] | 1211 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1212 | std::string fs_mgr_find_bow_device(const std::string& block_device) { |
| 1213 | if (block_device.substr(0, 5) != "/dev/") { |
| 1214 | LOG(ERROR) << "Expected block device, got " << block_device; |
| 1215 | return std::string(); |
| 1216 | } |
| 1217 | |
| 1218 | std::string sys_dir = std::string("/sys/") + block_device.substr(5); |
| 1219 | |
| 1220 | for (;;) { |
| 1221 | std::string name; |
| 1222 | if (!android::base::ReadFileToString(sys_dir + "/dm/name", &name)) { |
| 1223 | PLOG(ERROR) << block_device << " is not dm device"; |
| 1224 | return std::string(); |
| 1225 | } |
| 1226 | |
| 1227 | if (name == "bow\n") return sys_dir; |
| 1228 | |
| 1229 | std::string slaves = sys_dir + "/slaves"; |
| 1230 | std::unique_ptr<DIR, decltype(&closedir)> directory(opendir(slaves.c_str()), closedir); |
| 1231 | if (!directory) { |
| 1232 | PLOG(ERROR) << "Can't open slave directory " << slaves; |
| 1233 | return std::string(); |
| 1234 | } |
| 1235 | |
| 1236 | int count = 0; |
| 1237 | for (dirent* entry = readdir(directory.get()); entry; entry = readdir(directory.get())) { |
| 1238 | if (entry->d_type != DT_LNK) continue; |
| 1239 | |
| 1240 | if (count == 1) { |
| 1241 | LOG(ERROR) << "Too many slaves in " << slaves; |
| 1242 | return std::string(); |
| 1243 | } |
| 1244 | |
| 1245 | ++count; |
| 1246 | sys_dir = std::string("/sys/block/") + entry->d_name; |
| 1247 | } |
| 1248 | |
| 1249 | if (count != 1) { |
| 1250 | LOG(ERROR) << "No slave in " << slaves; |
| 1251 | return std::string(); |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
David Anderson | 671bd81 | 2020-01-24 19:19:27 -0800 | [diff] [blame] | 1256 | static constexpr const char* kUserdataWrapperName = "userdata-wrapper"; |
| 1257 | |
| 1258 | static void WrapUserdata(FstabEntry* entry, dev_t dev, const std::string& block_device) { |
| 1259 | DeviceMapper& dm = DeviceMapper::Instance(); |
| 1260 | if (dm.GetState(kUserdataWrapperName) != DmDeviceState::INVALID) { |
| 1261 | // This will report failure for us. If we do fail to get the path, |
| 1262 | // we leave the device unwrapped. |
| 1263 | dm.GetDmDevicePathByName(kUserdataWrapperName, &entry->blk_device); |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | unique_fd fd(open(block_device.c_str(), O_RDONLY | O_CLOEXEC)); |
| 1268 | if (fd < 0) { |
| 1269 | PLOG(ERROR) << "open failed: " << entry->blk_device; |
| 1270 | return; |
| 1271 | } |
| 1272 | |
| 1273 | auto dev_str = android::base::StringPrintf("%u:%u", major(dev), minor(dev)); |
| 1274 | uint64_t sectors = get_block_device_size(fd) / 512; |
| 1275 | |
| 1276 | android::dm::DmTable table; |
| 1277 | table.Emplace<DmTargetLinear>(0, sectors, dev_str, 0); |
| 1278 | |
| 1279 | std::string dm_path; |
| 1280 | if (!dm.CreateDevice(kUserdataWrapperName, table, &dm_path, 20s)) { |
| 1281 | LOG(ERROR) << "Failed to create userdata wrapper device"; |
| 1282 | return; |
| 1283 | } |
| 1284 | entry->blk_device = dm_path; |
| 1285 | } |
| 1286 | |
| 1287 | // When using Virtual A/B, partitions can be backed by /data and mapped with |
| 1288 | // device-mapper in first-stage init. This can happen when merging an OTA or |
| 1289 | // when using adb remount to house "scratch". In this case, /data cannot be |
| 1290 | // mounted directly off the userdata block device, and e2fsck will refuse to |
| 1291 | // scan it, because the kernel reports the block device as in-use. |
| 1292 | // |
| 1293 | // As a workaround, when mounting /data, we create a trivial dm-linear wrapper |
| 1294 | // if the underlying block device already has dependencies. Note that we make |
| 1295 | // an exception for metadata-encrypted devices, since dm-default-key is already |
| 1296 | // a wrapper. |
| 1297 | static void WrapUserdataIfNeeded(FstabEntry* entry, const std::string& actual_block_device = {}) { |
| 1298 | const auto& block_device = |
| 1299 | actual_block_device.empty() ? entry->blk_device : actual_block_device; |
Paul Crowley | 7823e32 | 2020-01-30 16:03:45 -0800 | [diff] [blame] | 1300 | if (entry->mount_point != "/data" || !entry->metadata_key_dir.empty() || |
David Anderson | 671bd81 | 2020-01-24 19:19:27 -0800 | [diff] [blame] | 1301 | android::base::StartsWith(block_device, "/dev/block/dm-")) { |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | struct stat st; |
| 1306 | if (stat(block_device.c_str(), &st) < 0) { |
| 1307 | PLOG(ERROR) << "stat failed: " << block_device; |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | std::string path = android::base::StringPrintf("/sys/dev/block/%u:%u/holders", |
| 1312 | major(st.st_rdev), minor(st.st_rdev)); |
| 1313 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); |
| 1314 | if (!dir) { |
| 1315 | PLOG(ERROR) << "opendir failed: " << path; |
| 1316 | return; |
| 1317 | } |
| 1318 | |
| 1319 | struct dirent* d; |
| 1320 | bool has_holders = false; |
| 1321 | while ((d = readdir(dir.get())) != nullptr) { |
| 1322 | if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) { |
| 1323 | has_holders = true; |
| 1324 | break; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | if (has_holders) { |
| 1329 | WrapUserdata(entry, st.st_rdev, block_device); |
| 1330 | } |
| 1331 | } |
| 1332 | |
David Anderson | 42c32bf | 2019-01-14 16:51:52 -0800 | [diff] [blame] | 1333 | static bool IsMountPointMounted(const std::string& mount_point) { |
| 1334 | // Check if this is already mounted. |
| 1335 | Fstab fstab; |
| 1336 | if (!ReadFstabFromFile("/proc/mounts", &fstab)) { |
| 1337 | return false; |
| 1338 | } |
Tom Cherry | 2e545f8 | 2019-01-29 08:49:57 -0800 | [diff] [blame] | 1339 | return GetEntryForMountPoint(&fstab, mount_point) != nullptr; |
David Anderson | 42c32bf | 2019-01-14 16:51:52 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1342 | // When multiple fstab records share the same mount_point, it will try to mount each |
| 1343 | // one in turn, and ignore any duplicates after a first successful mount. |
| 1344 | // Returns -1 on error, and FS_MGR_MNTALL_* otherwise. |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1345 | MountAllResult fs_mgr_mount_all(Fstab* fstab, int mount_mode) { |
Paul Lawrence | 1098aac | 2016-03-04 15:52:33 -0800 | [diff] [blame] | 1346 | int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 1347 | int error_count = 0; |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1348 | CheckpointManager checkpoint_manager; |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 1349 | AvbUniquePtr avb_handle(nullptr); |
Jaegeuk Kim | a763571 | 2020-12-15 08:46:44 -0800 | [diff] [blame] | 1350 | bool wiped = false; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1351 | |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1352 | bool userdata_mounted = false; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1353 | if (fstab->empty()) { |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1354 | return {FS_MGR_MNTALL_FAIL, userdata_mounted}; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1355 | } |
| 1356 | |
Woody Chow | 7fb93c1 | 2020-01-21 10:50:34 +0900 | [diff] [blame] | 1357 | // Keep i int to prevent unsigned integer overflow from (i = top_idx - 1), |
| 1358 | // where top_idx is 0. It will give SIGABRT |
| 1359 | for (int i = 0; i < static_cast<int>(fstab->size()); i++) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1360 | auto& current_entry = (*fstab)[i]; |
| 1361 | |
David Anderson | 42c32bf | 2019-01-14 16:51:52 -0800 | [diff] [blame] | 1362 | // If a filesystem should have been mounted in the first stage, we |
| 1363 | // ignore it here. With one exception, if the filesystem is |
| 1364 | // formattable, then it can only be formatted in the second stage, |
| 1365 | // so we allow it to mount here. |
| 1366 | if (current_entry.fs_mgr_flags.first_stage_mount && |
| 1367 | (!current_entry.fs_mgr_flags.formattable || |
| 1368 | IsMountPointMounted(current_entry.mount_point))) { |
| 1369 | continue; |
| 1370 | } |
| 1371 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1372 | // Don't mount entries that are managed by vold or not for the mount mode. |
| 1373 | if (current_entry.fs_mgr_flags.vold_managed || current_entry.fs_mgr_flags.recovery_only || |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1374 | ((mount_mode == MOUNT_MODE_LATE) && !current_entry.fs_mgr_flags.late_mount) || |
| 1375 | ((mount_mode == MOUNT_MODE_EARLY) && current_entry.fs_mgr_flags.late_mount)) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 1376 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1379 | // Skip swap and raw partition entries such as boot, recovery, etc. |
| 1380 | if (current_entry.fs_type == "swap" || current_entry.fs_type == "emmc" || |
| 1381 | current_entry.fs_type == "mtd") { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 1382 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1383 | } |
| 1384 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1385 | // Skip mounting the root partition, as it will already have been mounted. |
| 1386 | if (current_entry.mount_point == "/" || current_entry.mount_point == "/system") { |
| 1387 | if ((current_entry.flags & MS_RDONLY) != 0) { |
| 1388 | fs_mgr_set_blk_ro(current_entry.blk_device); |
Daniel Rosenberg | 31a4faf | 2015-06-29 17:33:05 -0700 | [diff] [blame] | 1389 | } |
| 1390 | continue; |
| 1391 | } |
| 1392 | |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 1393 | // Terrible hack to make it possible to remount /data. |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1394 | // TODO: refactor fs_mgr_mount_all and get rid of this. |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 1395 | if (mount_mode == MOUNT_MODE_ONLY_USERDATA && current_entry.mount_point != "/data") { |
| 1396 | continue; |
| 1397 | } |
| 1398 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1399 | // Translate LABEL= file system labels into block devices. |
| 1400 | if (is_extfs(current_entry.fs_type)) { |
| 1401 | if (!TranslateExtLabels(¤t_entry)) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 1402 | LERROR << "Could not translate label to block device"; |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 1403 | continue; |
| 1404 | } |
| 1405 | } |
| 1406 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1407 | if (current_entry.fs_mgr_flags.logical) { |
| 1408 | if (!fs_mgr_update_logical_partition(¤t_entry)) { |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 1409 | LERROR << "Could not set up logical partition, skipping!"; |
| 1410 | continue; |
| 1411 | } |
| 1412 | } |
| 1413 | |
David Anderson | 671bd81 | 2020-01-24 19:19:27 -0800 | [diff] [blame] | 1414 | WrapUserdataIfNeeded(¤t_entry); |
| 1415 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1416 | if (!checkpoint_manager.Update(¤t_entry)) { |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1417 | continue; |
Daniel Rosenberg | 4c93b25 | 2018-08-28 01:41:18 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
David Anderson | 5c475c7 | 2019-06-11 17:40:49 -0700 | [diff] [blame] | 1420 | if (current_entry.fs_mgr_flags.wait && !WaitForFile(current_entry.blk_device, 20s)) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1421 | LERROR << "Skipping '" << current_entry.blk_device << "' during mount_all"; |
Jinguang Dong | 9d34496 | 2017-06-13 10:20:34 +0800 | [diff] [blame] | 1422 | continue; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 1423 | } |
| 1424 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1425 | if (current_entry.fs_mgr_flags.avb) { |
Bowgo Tsai | 95c966a | 2017-03-30 18:42:54 +0800 | [diff] [blame] | 1426 | if (!avb_handle) { |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 1427 | avb_handle = AvbHandle::Open(); |
Bowgo Tsai | 95c966a | 2017-03-30 18:42:54 +0800 | [diff] [blame] | 1428 | if (!avb_handle) { |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 1429 | LERROR << "Failed to open AvbHandle"; |
Paul Crowley | 9acab5a | 2020-08-14 11:05:05 -0700 | [diff] [blame] | 1430 | set_type_property(encryptable); |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1431 | return {FS_MGR_MNTALL_FAIL, userdata_mounted}; |
Bowgo Tsai | 95c966a | 2017-03-30 18:42:54 +0800 | [diff] [blame] | 1432 | } |
| 1433 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1434 | if (avb_handle->SetUpAvbHashtree(¤t_entry, true /* wait_for_verity_dev */) == |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 1435 | AvbHashtreeResult::kFail) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1436 | LERROR << "Failed to set up AVB on partition: " << current_entry.mount_point |
| 1437 | << ", skipping!"; |
| 1438 | // Skips mounting the device. |
bowgotsai | b51722b | 2017-01-11 22:21:38 +0800 | [diff] [blame] | 1439 | continue; |
| 1440 | } |
Bowgo Tsai | f3e28e1 | 2019-02-20 21:53:47 +0800 | [diff] [blame] | 1441 | } else if (!current_entry.avb_keys.empty()) { |
Bowgo Tsai | defe1cb | 2019-01-25 12:07:09 +0800 | [diff] [blame] | 1442 | if (AvbHandle::SetUpStandaloneAvbHashtree(¤t_entry) == AvbHashtreeResult::kFail) { |
| 1443 | LERROR << "Failed to set up AVB on standalone partition: " |
| 1444 | << current_entry.mount_point << ", skipping!"; |
| 1445 | // Skips mounting the device. |
| 1446 | continue; |
| 1447 | } |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 1448 | } |
bowgotsai | b51722b | 2017-01-11 22:21:38 +0800 | [diff] [blame] | 1449 | |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 1450 | int last_idx_inspected; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 1451 | int top_idx = i; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1452 | int attempted_idx = -1; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 1453 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1454 | bool mret = mount_with_alternatives(*fstab, i, &last_idx_inspected, &attempted_idx); |
| 1455 | auto& attempted_entry = (*fstab)[attempted_idx]; |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 1456 | i = last_idx_inspected; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1457 | int mount_errno = errno; |
JP Abgrall | f786fe5 | 2014-06-18 07:28:14 +0000 | [diff] [blame] | 1458 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1459 | // Handle success and deal with encryptability. |
| 1460 | if (mret) { |
| 1461 | int status = handle_encryptable(attempted_entry); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1462 | |
| 1463 | if (status == FS_MGR_MNTALL_FAIL) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1464 | // Fatal error - no point continuing. |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1465 | return {status, userdata_mounted}; |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 1466 | } |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1467 | |
Paul Lawrence | 1098aac | 2016-03-04 15:52:33 -0800 | [diff] [blame] | 1468 | if (status != FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) { |
| 1469 | if (encryptable != FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) { |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1470 | // Log and continue |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 1471 | LERROR << "Only one encryptable/encrypted partition supported"; |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1472 | } |
| 1473 | encryptable = status; |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 1474 | if (status == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) { |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1475 | if (!call_vdc({"cryptfs", "encryptFstab", attempted_entry.blk_device, |
Jaegeuk Kim | a763571 | 2020-12-15 08:46:44 -0800 | [diff] [blame] | 1476 | attempted_entry.mount_point, wiped ? "true" : "false", |
| 1477 | attempted_entry.fs_type}, |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 1478 | nullptr)) { |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 1479 | LERROR << "Encryption failed"; |
Paul Crowley | 9acab5a | 2020-08-14 11:05:05 -0700 | [diff] [blame] | 1480 | set_type_property(encryptable); |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1481 | return {FS_MGR_MNTALL_FAIL, userdata_mounted}; |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 1482 | } |
| 1483 | } |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1486 | if (current_entry.mount_point == "/data") { |
| 1487 | userdata_mounted = true; |
| 1488 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1489 | // Success! Go get the next one. |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1490 | continue; |
| 1491 | } |
| 1492 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1493 | // Mounting failed, understand why and retry. |
Jaegeuk Kim | a763571 | 2020-12-15 08:46:44 -0800 | [diff] [blame] | 1494 | wiped = partition_wiped(current_entry.blk_device.c_str()); |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1495 | if (mount_errno != EBUSY && mount_errno != EACCES && |
| 1496 | current_entry.fs_mgr_flags.formattable && wiped) { |
| 1497 | // current_entry and attempted_entry point at the same partition, but sometimes |
| 1498 | // at two different lines in the fstab. Use current_entry for formatting |
| 1499 | // as that is the preferred one. |
| 1500 | LERROR << __FUNCTION__ << "(): " << realpath(current_entry.blk_device) |
| 1501 | << " is wiped and " << current_entry.mount_point << " " << current_entry.fs_type |
| 1502 | << " is formattable. Format it."; |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1503 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1504 | checkpoint_manager.Revert(¤t_entry); |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1505 | |
Jaegeuk Kim | a763571 | 2020-12-15 08:46:44 -0800 | [diff] [blame] | 1506 | // EncryptInplace will be used when vdc gives an error or needs to format partitions |
| 1507 | // other than /data |
| 1508 | if (should_use_metadata_encryption(current_entry) && |
| 1509 | current_entry.mount_point == "/data") { |
| 1510 | |
| 1511 | // vdc->Format requires "ro.crypto.type" to set an encryption flag |
| 1512 | encryptable = FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED; |
| 1513 | set_type_property(encryptable); |
| 1514 | |
| 1515 | if (!call_vdc({"cryptfs", "encryptFstab", current_entry.blk_device, |
| 1516 | current_entry.mount_point, "true" /* shouldFormat */, |
| 1517 | current_entry.fs_type}, |
| 1518 | nullptr)) { |
| 1519 | LERROR << "Encryption failed"; |
| 1520 | } else { |
| 1521 | userdata_mounted = true; |
| 1522 | continue; |
| 1523 | } |
| 1524 | } |
| 1525 | |
Eric Biggers | 4d0c5ef | 2021-11-08 16:38:54 -0800 | [diff] [blame] | 1526 | if (fs_mgr_do_format(current_entry) == 0) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1527 | // Let's replay the mount actions. |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 1528 | i = top_idx - 1; |
| 1529 | continue; |
Matthew Bouyack | 9c59cbc | 2016-05-02 15:55:30 -0700 | [diff] [blame] | 1530 | } else { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 1531 | LERROR << __FUNCTION__ << "(): Format failed. " |
| 1532 | << "Suggest recovery..."; |
Matthew Bouyack | 9c59cbc | 2016-05-02 15:55:30 -0700 | [diff] [blame] | 1533 | encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY; |
| 1534 | continue; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 1535 | } |
| 1536 | } |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 1537 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1538 | // mount(2) returned an error, handle the encryptable/formattable case. |
Eric Biggers | 63fb195 | 2021-11-08 16:38:53 -0800 | [diff] [blame] | 1539 | if (mount_errno != EBUSY && mount_errno != EACCES && |
| 1540 | should_use_metadata_encryption(attempted_entry)) { |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1541 | if (!call_vdc({"cryptfs", "mountFstab", attempted_entry.blk_device, |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 1542 | attempted_entry.mount_point}, |
| 1543 | nullptr)) { |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 1544 | ++error_count; |
Qilin Tan | 12d95df | 2020-11-02 11:25:09 +0800 | [diff] [blame] | 1545 | } else if (current_entry.mount_point == "/data") { |
| 1546 | userdata_mounted = true; |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 1547 | } |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 1548 | encryptable = FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED; |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 1549 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1550 | } else { |
Bowgo Tsai | 59af33c | 2017-05-24 18:51:36 +0800 | [diff] [blame] | 1551 | // fs_options might be null so we cannot use PERROR << directly. |
| 1552 | // Use StringPrintf to output "(null)" instead. |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1553 | if (attempted_entry.fs_mgr_flags.no_fail) { |
Bowgo Tsai | 59af33c | 2017-05-24 18:51:36 +0800 | [diff] [blame] | 1554 | PERROR << android::base::StringPrintf( |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1555 | "Ignoring failure to mount an un-encryptable or wiped " |
| 1556 | "partition on %s at %s options: %s", |
| 1557 | attempted_entry.blk_device.c_str(), attempted_entry.mount_point.c_str(), |
| 1558 | attempted_entry.fs_options.c_str()); |
Daniel Rosenberg | d38e3c5 | 2016-04-07 20:10:25 -0700 | [diff] [blame] | 1559 | } else { |
Bowgo Tsai | 59af33c | 2017-05-24 18:51:36 +0800 | [diff] [blame] | 1560 | PERROR << android::base::StringPrintf( |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1561 | "Failed to mount an un-encryptable or wiped partition " |
| 1562 | "on %s at %s options: %s", |
| 1563 | attempted_entry.blk_device.c_str(), attempted_entry.mount_point.c_str(), |
| 1564 | attempted_entry.fs_options.c_str()); |
Daniel Rosenberg | d38e3c5 | 2016-04-07 20:10:25 -0700 | [diff] [blame] | 1565 | ++error_count; |
| 1566 | } |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 1567 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
Paul Crowley | 9acab5a | 2020-08-14 11:05:05 -0700 | [diff] [blame] | 1571 | set_type_property(encryptable); |
| 1572 | |
Mark Salyzyn | d9e6c20 | 2018-06-20 14:08:09 -0700 | [diff] [blame] | 1573 | #if ALLOW_ADBD_DISABLE_VERITY == 1 // "userdebug" build |
Mark Salyzyn | f35db9b | 2018-09-20 15:23:00 -0700 | [diff] [blame] | 1574 | fs_mgr_overlayfs_mount_all(fstab); |
Mark Salyzyn | d9e6c20 | 2018-06-20 14:08:09 -0700 | [diff] [blame] | 1575 | #endif |
| 1576 | |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 1577 | if (error_count) { |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1578 | return {FS_MGR_MNTALL_FAIL, userdata_mounted}; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 1579 | } else { |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1580 | return {encryptable, userdata_mounted}; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 1581 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1582 | } |
| 1583 | |
Yifan Hong | 402633d | 2019-04-09 10:11:34 -0700 | [diff] [blame] | 1584 | int fs_mgr_umount_all(android::fs_mgr::Fstab* fstab) { |
| 1585 | AvbUniquePtr avb_handle(nullptr); |
| 1586 | int ret = FsMgrUmountStatus::SUCCESS; |
| 1587 | for (auto& current_entry : *fstab) { |
| 1588 | if (!IsMountPointMounted(current_entry.mount_point)) { |
| 1589 | continue; |
| 1590 | } |
| 1591 | |
| 1592 | if (umount(current_entry.mount_point.c_str()) == -1) { |
| 1593 | PERROR << "Failed to umount " << current_entry.mount_point; |
| 1594 | ret |= FsMgrUmountStatus::ERROR_UMOUNT; |
| 1595 | continue; |
| 1596 | } |
| 1597 | |
| 1598 | if (current_entry.fs_mgr_flags.logical) { |
| 1599 | if (!fs_mgr_update_logical_partition(¤t_entry)) { |
| 1600 | LERROR << "Could not get logical partition blk_device, skipping!"; |
| 1601 | ret |= FsMgrUmountStatus::ERROR_DEVICE_MAPPER; |
| 1602 | continue; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | if (current_entry.fs_mgr_flags.avb || !current_entry.avb_keys.empty()) { |
| 1607 | if (!AvbHandle::TearDownAvbHashtree(¤t_entry, true /* wait */)) { |
| 1608 | LERROR << "Failed to tear down AVB on mount point: " << current_entry.mount_point; |
| 1609 | ret |= FsMgrUmountStatus::ERROR_VERITY; |
| 1610 | continue; |
| 1611 | } |
Yifan Hong | 402633d | 2019-04-09 10:11:34 -0700 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | return ret; |
| 1615 | } |
| 1616 | |
Nikita Ioffe | 7b41a15 | 2020-03-25 00:06:51 +0000 | [diff] [blame] | 1617 | static std::chrono::milliseconds GetMillisProperty(const std::string& name, |
| 1618 | std::chrono::milliseconds default_value) { |
| 1619 | auto value = GetUintProperty(name, static_cast<uint64_t>(default_value.count())); |
| 1620 | return std::chrono::milliseconds(std::move(value)); |
| 1621 | } |
| 1622 | |
Nikita Ioffe | 7aa37f1 | 2020-04-06 23:28:40 +0100 | [diff] [blame] | 1623 | static bool fs_mgr_unmount_all_data_mounts(const std::string& data_block_device) { |
| 1624 | LINFO << __FUNCTION__ << "(): about to umount everything on top of " << data_block_device; |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 1625 | Timer t; |
Nikita Ioffe | 7b41a15 | 2020-03-25 00:06:51 +0000 | [diff] [blame] | 1626 | auto timeout = GetMillisProperty("init.userspace_reboot.userdata_remount.timeoutmillis", 5s); |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 1627 | while (true) { |
| 1628 | bool umount_done = true; |
| 1629 | Fstab proc_mounts; |
| 1630 | if (!ReadFstabFromFile("/proc/mounts", &proc_mounts)) { |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1631 | LERROR << __FUNCTION__ << "(): Can't read /proc/mounts"; |
Greg Kaiser | d8fdf60 | 2019-12-20 19:39:16 -0800 | [diff] [blame] | 1632 | return false; |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 1633 | } |
| 1634 | // Now proceed with other bind mounts on top of /data. |
| 1635 | for (const auto& entry : proc_mounts) { |
Nikita Ioffe | 7aa37f1 | 2020-04-06 23:28:40 +0100 | [diff] [blame] | 1636 | std::string block_device; |
| 1637 | if (StartsWith(entry.blk_device, "/dev/block") && |
| 1638 | !Realpath(entry.blk_device, &block_device)) { |
| 1639 | PWARNING << __FUNCTION__ << "(): failed to realpath " << entry.blk_device; |
| 1640 | block_device = entry.blk_device; |
| 1641 | } |
| 1642 | if (data_block_device == block_device) { |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 1643 | if (umount2(entry.mount_point.c_str(), 0) != 0) { |
Nikita Ioffe | b03e0cf | 2020-01-13 16:05:07 +0000 | [diff] [blame] | 1644 | PERROR << __FUNCTION__ << "(): Failed to umount " << entry.mount_point; |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 1645 | umount_done = false; |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | if (umount_done) { |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1650 | LINFO << __FUNCTION__ << "(): Unmounting /data took " << t; |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 1651 | return true; |
| 1652 | } |
| 1653 | if (t.duration() > timeout) { |
Nikita Ioffe | 7aa37f1 | 2020-04-06 23:28:40 +0100 | [diff] [blame] | 1654 | LERROR << __FUNCTION__ << "(): Timed out unmounting all mounts on " |
| 1655 | << data_block_device; |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1656 | Fstab remaining_mounts; |
| 1657 | if (!ReadFstabFromFile("/proc/mounts", &remaining_mounts)) { |
| 1658 | LERROR << __FUNCTION__ << "(): Can't read /proc/mounts"; |
| 1659 | } else { |
| 1660 | LERROR << __FUNCTION__ << "(): Following mounts remaining"; |
| 1661 | for (const auto& e : remaining_mounts) { |
| 1662 | LERROR << __FUNCTION__ << "(): mount point: " << e.mount_point |
| 1663 | << " block device: " << e.blk_device; |
| 1664 | } |
| 1665 | } |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 1666 | return false; |
| 1667 | } |
| 1668 | std::this_thread::sleep_for(50ms); |
| 1669 | } |
| 1670 | } |
| 1671 | |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1672 | static bool UnwindDmDeviceStack(const std::string& block_device, |
| 1673 | std::vector<std::string>* dm_stack) { |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1674 | if (!StartsWith(block_device, "/dev/block/")) { |
| 1675 | LWARNING << block_device << " is not a block device"; |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1676 | return false; |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1677 | } |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1678 | std::string current = block_device; |
| 1679 | DeviceMapper& dm = DeviceMapper::Instance(); |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1680 | while (true) { |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1681 | dm_stack->push_back(current); |
| 1682 | if (!dm.IsDmBlockDevice(current)) { |
| 1683 | break; |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1684 | } |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1685 | auto parent = dm.GetParentBlockDeviceByPath(current); |
| 1686 | if (!parent) { |
| 1687 | return false; |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1688 | } |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1689 | current = *parent; |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1690 | } |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1691 | return true; |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Nikita Ioffe | 7aa37f1 | 2020-04-06 23:28:40 +0100 | [diff] [blame] | 1694 | FstabEntry* fs_mgr_get_mounted_entry_for_userdata(Fstab* fstab, |
| 1695 | const std::string& data_block_device) { |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1696 | std::vector<std::string> dm_stack; |
Nikita Ioffe | 7aa37f1 | 2020-04-06 23:28:40 +0100 | [diff] [blame] | 1697 | if (!UnwindDmDeviceStack(data_block_device, &dm_stack)) { |
| 1698 | LERROR << "Failed to unwind dm-device stack for " << data_block_device; |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1699 | return nullptr; |
| 1700 | } |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1701 | for (auto& entry : *fstab) { |
| 1702 | if (entry.mount_point != "/data") { |
| 1703 | continue; |
| 1704 | } |
| 1705 | std::string block_device; |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1706 | if (entry.fs_mgr_flags.logical) { |
| 1707 | if (!fs_mgr_update_logical_partition(&entry)) { |
| 1708 | LERROR << "Failed to update logic partition " << entry.blk_device; |
| 1709 | continue; |
| 1710 | } |
| 1711 | block_device = entry.blk_device; |
Nikita Ioffe | 7aa37f1 | 2020-04-06 23:28:40 +0100 | [diff] [blame] | 1712 | } else if (!Realpath(entry.blk_device, &block_device)) { |
| 1713 | PWARNING << "Failed to realpath " << entry.blk_device; |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1714 | block_device = entry.blk_device; |
| 1715 | } |
Nikita Ioffe | 17824f0 | 2020-03-06 17:36:05 +0000 | [diff] [blame] | 1716 | if (std::find(dm_stack.begin(), dm_stack.end(), block_device) != dm_stack.end()) { |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1717 | return &entry; |
| 1718 | } |
| 1719 | } |
Nikita Ioffe | 7aa37f1 | 2020-04-06 23:28:40 +0100 | [diff] [blame] | 1720 | LERROR << "Didn't find entry that was used to mount /data onto " << data_block_device; |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1721 | return nullptr; |
| 1722 | } |
| 1723 | |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 1724 | // TODO(b/143970043): return different error codes based on which step failed. |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1725 | int fs_mgr_remount_userdata_into_checkpointing(Fstab* fstab) { |
Nikita Ioffe | 0dda423 | 2019-12-16 23:22:55 +0000 | [diff] [blame] | 1726 | Fstab proc_mounts; |
| 1727 | if (!ReadFstabFromFile("/proc/mounts", &proc_mounts)) { |
| 1728 | LERROR << "Can't read /proc/mounts"; |
| 1729 | return -1; |
| 1730 | } |
Nikita Ioffe | 30b0c01 | 2020-03-06 15:55:20 +0000 | [diff] [blame] | 1731 | auto mounted_entry = GetEntryForMountPoint(&proc_mounts, "/data"); |
| 1732 | if (mounted_entry == nullptr) { |
Nikita Ioffe | d572c80 | 2019-12-09 21:13:08 +0000 | [diff] [blame] | 1733 | LERROR << "/data is not mounted"; |
| 1734 | return -1; |
| 1735 | } |
Nikita Ioffe | 7aa37f1 | 2020-04-06 23:28:40 +0100 | [diff] [blame] | 1736 | std::string block_device; |
| 1737 | if (!Realpath(mounted_entry->blk_device, &block_device)) { |
| 1738 | PERROR << "Failed to realpath " << mounted_entry->blk_device; |
| 1739 | return -1; |
| 1740 | } |
| 1741 | auto fstab_entry = fs_mgr_get_mounted_entry_for_userdata(fstab, block_device); |
Nikita Ioffe | d572c80 | 2019-12-09 21:13:08 +0000 | [diff] [blame] | 1742 | if (fstab_entry == nullptr) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1743 | LERROR << "Can't find /data in fstab"; |
| 1744 | return -1; |
| 1745 | } |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1746 | bool force_umount = GetBoolProperty("sys.init.userdata_remount.force_umount", false); |
| 1747 | if (force_umount) { |
| 1748 | LINFO << "Will force an umount of userdata even if it's not required"; |
| 1749 | } |
| 1750 | if (!force_umount && !SupportsCheckpoint(fstab_entry)) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1751 | LINFO << "Userdata doesn't support checkpointing. Nothing to do"; |
| 1752 | return 0; |
| 1753 | } |
| 1754 | CheckpointManager checkpoint_manager; |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1755 | if (!force_umount && !checkpoint_manager.NeedsCheckpoint()) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1756 | LINFO << "Checkpointing not needed. Don't remount"; |
| 1757 | return 0; |
| 1758 | } |
Nikita Ioffe | cb0c92e | 2020-01-10 17:34:59 +0000 | [diff] [blame] | 1759 | if (!force_umount && fstab_entry->fs_mgr_flags.checkpoint_fs) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1760 | // Userdata is f2fs, simply remount it. |
Nikita Ioffe | d572c80 | 2019-12-09 21:13:08 +0000 | [diff] [blame] | 1761 | if (!checkpoint_manager.Update(fstab_entry)) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1762 | LERROR << "Failed to remount userdata in checkpointing mode"; |
| 1763 | return -1; |
| 1764 | } |
Nikita Ioffe | d572c80 | 2019-12-09 21:13:08 +0000 | [diff] [blame] | 1765 | if (mount(block_device.c_str(), fstab_entry->mount_point.c_str(), "none", |
| 1766 | MS_REMOUNT | fstab_entry->flags, fstab_entry->fs_options.c_str()) != 0) { |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 1767 | PERROR << "Failed to remount userdata in checkpointing mode"; |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1768 | return -1; |
| 1769 | } |
| 1770 | } else { |
Nikita Ioffe | d572c80 | 2019-12-09 21:13:08 +0000 | [diff] [blame] | 1771 | LINFO << "Unmounting /data before remounting into checkpointing mode"; |
Nikita Ioffe | 3d6a5fc | 2019-12-19 21:18:42 +0000 | [diff] [blame] | 1772 | if (!fs_mgr_unmount_all_data_mounts(block_device)) { |
| 1773 | LERROR << "Failed to umount /data"; |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 1774 | return -1; |
| 1775 | } |
| 1776 | DeviceMapper& dm = DeviceMapper::Instance(); |
Nikita Ioffe | d572c80 | 2019-12-09 21:13:08 +0000 | [diff] [blame] | 1777 | while (dm.IsDmBlockDevice(block_device)) { |
| 1778 | auto next_device = dm.GetParentBlockDeviceByPath(block_device); |
| 1779 | auto name = dm.GetDmDeviceNameByPath(block_device); |
| 1780 | if (!name) { |
| 1781 | LERROR << "Failed to get dm-name for " << block_device; |
| 1782 | return -1; |
| 1783 | } |
| 1784 | LINFO << "Deleting " << block_device << " named " << *name; |
| 1785 | if (!dm.DeleteDevice(*name, 3s)) { |
| 1786 | return -1; |
| 1787 | } |
| 1788 | if (!next_device) { |
| 1789 | LERROR << "Failed to find parent device for " << block_device; |
| 1790 | } |
| 1791 | block_device = *next_device; |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 1792 | } |
Nikita Ioffe | d572c80 | 2019-12-09 21:13:08 +0000 | [diff] [blame] | 1793 | LINFO << "Remounting /data"; |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 1794 | // TODO(b/143970043): remove this hack after fs_mgr_mount_all is refactored. |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 1795 | auto result = fs_mgr_mount_all(fstab, MOUNT_MODE_ONLY_USERDATA); |
| 1796 | return result.code == FS_MGR_MNTALL_FAIL ? -1 : 0; |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1797 | } |
| 1798 | return 0; |
| 1799 | } |
| 1800 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1801 | // wrapper to __mount() and expects a fully prepared fstab_rec, |
| 1802 | // unlike fs_mgr_do_mount which does more things with avb / verity etc. |
Tom Cherry | a7f1a9f | 2020-11-19 11:39:39 -0800 | [diff] [blame] | 1803 | int fs_mgr_do_mount_one(const FstabEntry& entry, const std::string& alt_mount_point) { |
Will Shiu | 589b7df | 2020-01-03 16:05:00 +0800 | [diff] [blame] | 1804 | // First check the filesystem if requested. |
| 1805 | if (entry.fs_mgr_flags.wait && !WaitForFile(entry.blk_device, 20s)) { |
| 1806 | LERROR << "Skipping mounting '" << entry.blk_device << "'"; |
| 1807 | } |
| 1808 | |
Tom Cherry | a7f1a9f | 2020-11-19 11:39:39 -0800 | [diff] [blame] | 1809 | auto& mount_point = alt_mount_point.empty() ? entry.mount_point : alt_mount_point; |
Tom Cherry | 81ae075 | 2018-07-30 16:23:49 -0700 | [diff] [blame] | 1810 | |
Tom Cherry | a7f1a9f | 2020-11-19 11:39:39 -0800 | [diff] [blame] | 1811 | // Run fsck if needed |
Jaegeuk Kim | 9547726 | 2021-12-29 14:44:22 -0800 | [diff] [blame] | 1812 | int ret = prepare_fs_for_mount(entry.blk_device, entry, mount_point); |
| 1813 | // Wiped case doesn't require to try __mount below. |
| 1814 | if (ret & FS_STAT_INVALID_MAGIC) { |
| 1815 | return FS_MGR_DOMNT_FAILED; |
| 1816 | } |
Tom Cherry | a7f1a9f | 2020-11-19 11:39:39 -0800 | [diff] [blame] | 1817 | |
Jaegeuk Kim | 9547726 | 2021-12-29 14:44:22 -0800 | [diff] [blame] | 1818 | ret = __mount(entry.blk_device, mount_point, entry); |
Sandeep Patil | 56f9ada | 2017-02-14 14:00:14 -0800 | [diff] [blame] | 1819 | if (ret) { |
| 1820 | ret = (errno == EBUSY) ? FS_MGR_DOMNT_BUSY : FS_MGR_DOMNT_FAILED; |
| 1821 | } |
| 1822 | |
| 1823 | return ret; |
| 1824 | } |
| 1825 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1826 | // If tmp_mount_point is non-null, mount the filesystem there. This is for the |
| 1827 | // tmp mount we do to check the user password |
| 1828 | // If multiple fstab entries are to be mounted on "n_name", it will try to mount each one |
| 1829 | // in turn, and stop on 1st success, or no more match. |
| 1830 | static int fs_mgr_do_mount_helper(Fstab* fstab, const std::string& n_name, |
| 1831 | const std::string& n_blk_device, const char* tmp_mount_point, |
Paul Lawrence | 2055364 | 2020-06-12 07:32:23 -0700 | [diff] [blame] | 1832 | int needs_checkpoint, bool metadata_encrypted) { |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 1833 | int mount_errors = 0; |
| 1834 | int first_mount_errno = 0; |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1835 | std::string mount_point; |
Paul Lawrence | 2055364 | 2020-06-12 07:32:23 -0700 | [diff] [blame] | 1836 | CheckpointManager checkpoint_manager(needs_checkpoint, metadata_encrypted); |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 1837 | AvbUniquePtr avb_handle(nullptr); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1838 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 1839 | if (!fstab) { |
Bowgo Tsai | 359bed3 | 2017-04-27 15:44:39 +0800 | [diff] [blame] | 1840 | return FS_MGR_DOMNT_FAILED; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1841 | } |
| 1842 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1843 | for (auto& fstab_entry : *fstab) { |
| 1844 | if (!fs_match(fstab_entry.mount_point, n_name)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1845 | continue; |
| 1846 | } |
| 1847 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1848 | // We found our match. |
| 1849 | // If this swap or a raw partition, report an error. |
| 1850 | if (fstab_entry.fs_type == "swap" || fstab_entry.fs_type == "emmc" || |
| 1851 | fstab_entry.fs_type == "mtd") { |
| 1852 | LERROR << "Cannot mount filesystem of type " << fstab_entry.fs_type << " on " |
| 1853 | << n_blk_device; |
Bowgo Tsai | 359bed3 | 2017-04-27 15:44:39 +0800 | [diff] [blame] | 1854 | return FS_MGR_DOMNT_FAILED; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1855 | } |
| 1856 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1857 | if (fstab_entry.fs_mgr_flags.logical) { |
| 1858 | if (!fs_mgr_update_logical_partition(&fstab_entry)) { |
David Anderson | 62e5b20 | 2018-05-01 17:09:17 -0700 | [diff] [blame] | 1859 | LERROR << "Could not set up logical partition, skipping!"; |
| 1860 | continue; |
| 1861 | } |
| 1862 | } |
| 1863 | |
David Anderson | 671bd81 | 2020-01-24 19:19:27 -0800 | [diff] [blame] | 1864 | WrapUserdataIfNeeded(&fstab_entry, n_blk_device); |
| 1865 | |
Paul Lawrence | 323959e | 2019-06-25 14:36:52 -0700 | [diff] [blame] | 1866 | if (!checkpoint_manager.Update(&fstab_entry, n_blk_device)) { |
Paul Lawrence | b920cb4 | 2018-10-04 08:37:17 -0700 | [diff] [blame] | 1867 | LERROR << "Could not set up checkpoint partition, skipping!"; |
| 1868 | continue; |
Daniel Rosenberg | 4c93b25 | 2018-08-28 01:41:18 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1871 | // First check the filesystem if requested. |
David Anderson | 5c475c7 | 2019-06-11 17:40:49 -0700 | [diff] [blame] | 1872 | if (fstab_entry.fs_mgr_flags.wait && !WaitForFile(n_blk_device, 20s)) { |
Jinguang Dong | 9d34496 | 2017-06-13 10:20:34 +0800 | [diff] [blame] | 1873 | LERROR << "Skipping mounting '" << n_blk_device << "'"; |
| 1874 | continue; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Tom Cherry | a7f1a9f | 2020-11-19 11:39:39 -0800 | [diff] [blame] | 1877 | // Now mount it where requested */ |
| 1878 | if (tmp_mount_point) { |
| 1879 | mount_point = tmp_mount_point; |
| 1880 | } else { |
| 1881 | mount_point = fstab_entry.mount_point; |
| 1882 | } |
| 1883 | |
| 1884 | int fs_stat = prepare_fs_for_mount(n_blk_device, fstab_entry, mount_point); |
liminghao | 9a0fd1d | 2016-07-22 11:48:14 +0800 | [diff] [blame] | 1885 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1886 | if (fstab_entry.fs_mgr_flags.avb) { |
Bowgo Tsai | 95c966a | 2017-03-30 18:42:54 +0800 | [diff] [blame] | 1887 | if (!avb_handle) { |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 1888 | avb_handle = AvbHandle::Open(); |
Bowgo Tsai | 95c966a | 2017-03-30 18:42:54 +0800 | [diff] [blame] | 1889 | if (!avb_handle) { |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 1890 | LERROR << "Failed to open AvbHandle"; |
Bowgo Tsai | 359bed3 | 2017-04-27 15:44:39 +0800 | [diff] [blame] | 1891 | return FS_MGR_DOMNT_FAILED; |
Bowgo Tsai | 95c966a | 2017-03-30 18:42:54 +0800 | [diff] [blame] | 1892 | } |
| 1893 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1894 | if (avb_handle->SetUpAvbHashtree(&fstab_entry, true /* wait_for_verity_dev */) == |
Bowgo Tsai | c1bc281 | 2018-11-26 17:49:23 +0800 | [diff] [blame] | 1895 | AvbHashtreeResult::kFail) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1896 | LERROR << "Failed to set up AVB on partition: " << fstab_entry.mount_point |
| 1897 | << ", skipping!"; |
| 1898 | // Skips mounting the device. |
bowgotsai | b51722b | 2017-01-11 22:21:38 +0800 | [diff] [blame] | 1899 | continue; |
| 1900 | } |
Bowgo Tsai | f3e28e1 | 2019-02-20 21:53:47 +0800 | [diff] [blame] | 1901 | } else if (!fstab_entry.avb_keys.empty()) { |
Bowgo Tsai | defe1cb | 2019-01-25 12:07:09 +0800 | [diff] [blame] | 1902 | if (AvbHandle::SetUpStandaloneAvbHashtree(&fstab_entry) == AvbHashtreeResult::kFail) { |
| 1903 | LERROR << "Failed to set up AVB on standalone partition: " |
| 1904 | << fstab_entry.mount_point << ", skipping!"; |
| 1905 | // Skips mounting the device. |
| 1906 | continue; |
| 1907 | } |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 1908 | } |
| 1909 | |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 1910 | int retry_count = 2; |
| 1911 | while (retry_count-- > 0) { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1912 | if (!__mount(n_blk_device, mount_point, fstab_entry)) { |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 1913 | fs_stat &= ~FS_STAT_FULL_MOUNT_FAILED; |
Bowgo Tsai | 359bed3 | 2017-04-27 15:44:39 +0800 | [diff] [blame] | 1914 | return FS_MGR_DOMNT_SUCCESS; |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 1915 | } else { |
| 1916 | if (retry_count <= 0) break; // run check_fs only once |
| 1917 | if (!first_mount_errno) first_mount_errno = errno; |
| 1918 | mount_errors++; |
| 1919 | fs_stat |= FS_STAT_FULL_MOUNT_FAILED; |
| 1920 | // try again after fsck |
Tom Cherry | a7f1a9f | 2020-11-19 11:39:39 -0800 | [diff] [blame] | 1921 | check_fs(n_blk_device, fstab_entry.fs_type, mount_point, &fs_stat); |
Keun-young Park | 40db04d | 2017-04-13 17:31:08 -0700 | [diff] [blame] | 1922 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1923 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1924 | log_fs_stat(fstab_entry.blk_device, fs_stat); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1925 | } |
Bowgo Tsai | 359bed3 | 2017-04-27 15:44:39 +0800 | [diff] [blame] | 1926 | |
| 1927 | // Reach here means the mount attempt fails. |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 1928 | if (mount_errors) { |
Bowgo Tsai | 359bed3 | 2017-04-27 15:44:39 +0800 | [diff] [blame] | 1929 | PERROR << "Cannot mount filesystem on " << n_blk_device << " at " << mount_point; |
| 1930 | if (first_mount_errno == EBUSY) return FS_MGR_DOMNT_BUSY; |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 1931 | } else { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 1932 | // We didn't find a match, say so and return an error. |
Bowgo Tsai | 359bed3 | 2017-04-27 15:44:39 +0800 | [diff] [blame] | 1933 | LERROR << "Cannot find mount point " << n_name << " in fstab"; |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 1934 | } |
Bowgo Tsai | 359bed3 | 2017-04-27 15:44:39 +0800 | [diff] [blame] | 1935 | return FS_MGR_DOMNT_FAILED; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1936 | } |
| 1937 | |
Tom Cherry | 2e545f8 | 2019-01-29 08:49:57 -0800 | [diff] [blame] | 1938 | int fs_mgr_do_mount(Fstab* fstab, const char* n_name, char* n_blk_device, char* tmp_mount_point) { |
Paul Lawrence | 2055364 | 2020-06-12 07:32:23 -0700 | [diff] [blame] | 1939 | return fs_mgr_do_mount_helper(fstab, n_name, n_blk_device, tmp_mount_point, -1, false); |
Tom Cherry | 2e545f8 | 2019-01-29 08:49:57 -0800 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | int fs_mgr_do_mount(Fstab* fstab, const char* n_name, char* n_blk_device, char* tmp_mount_point, |
Paul Lawrence | 2055364 | 2020-06-12 07:32:23 -0700 | [diff] [blame] | 1943 | bool needs_checkpoint, bool metadata_encrypted) { |
| 1944 | return fs_mgr_do_mount_helper(fstab, n_name, n_blk_device, tmp_mount_point, needs_checkpoint, |
| 1945 | metadata_encrypted); |
Tom Cherry | 2e545f8 | 2019-01-29 08:49:57 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1948 | /* |
| 1949 | * mount a tmpfs filesystem at the given point. |
| 1950 | * return 0 on success, non-zero on failure. |
| 1951 | */ |
Wei Wang | 4d71bc5 | 2017-02-24 17:41:38 -0800 | [diff] [blame] | 1952 | int fs_mgr_do_tmpfs_mount(const char *n_name) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1953 | { |
| 1954 | int ret; |
| 1955 | |
Luis Hector Chavez | f29b39d | 2017-07-10 15:25:55 -0700 | [diff] [blame] | 1956 | ret = mount("tmpfs", n_name, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV | MS_NOEXEC, |
| 1957 | CRYPTO_TMPFS_OPTIONS); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1958 | if (ret < 0) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 1959 | LERROR << "Cannot mount tmpfs filesystem at " << n_name; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1960 | return -1; |
| 1961 | } |
| 1962 | |
| 1963 | /* Success */ |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
Bart Van Assche | ec5f635 | 2021-07-29 13:50:43 -0700 | [diff] [blame] | 1967 | static bool ConfigureIoScheduler(const std::string& device_path) { |
| 1968 | if (!StartsWith(device_path, "/dev/")) { |
| 1969 | LERROR << __func__ << ": invalid argument " << device_path; |
| 1970 | return false; |
| 1971 | } |
| 1972 | |
| 1973 | const std::string iosched_path = |
| 1974 | StringPrintf("/sys/block/%s/queue/scheduler", Basename(device_path).c_str()); |
| 1975 | unique_fd iosched_fd(open(iosched_path.c_str(), O_RDWR | O_CLOEXEC)); |
| 1976 | if (iosched_fd.get() == -1) { |
| 1977 | PERROR << __func__ << ": failed to open " << iosched_path; |
| 1978 | return false; |
| 1979 | } |
| 1980 | |
| 1981 | // Kernels before v4.1 only support 'noop'. Kernels [v4.1, v5.0) support |
| 1982 | // 'noop' and 'none'. Kernels v5.0 and later only support 'none'. |
| 1983 | static constexpr const std::array<std::string_view, 2> kNoScheduler = {"none", "noop"}; |
| 1984 | |
| 1985 | for (const std::string_view& scheduler : kNoScheduler) { |
| 1986 | int ret = write(iosched_fd.get(), scheduler.data(), scheduler.size()); |
| 1987 | if (ret > 0) { |
| 1988 | return true; |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | PERROR << __func__ << ": failed to write to " << iosched_path; |
| 1993 | return false; |
| 1994 | } |
| 1995 | |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 1996 | static bool InstallZramDevice(const std::string& device) { |
| 1997 | if (!android::base::WriteStringToFile(device, ZRAM_BACK_DEV)) { |
| 1998 | PERROR << "Cannot write " << device << " in: " << ZRAM_BACK_DEV; |
| 1999 | return false; |
| 2000 | } |
| 2001 | LINFO << "Success to set " << device << " to " << ZRAM_BACK_DEV; |
| 2002 | return true; |
| 2003 | } |
| 2004 | |
Minchan Kim | 12e9615 | 2019-10-15 14:41:18 -0700 | [diff] [blame] | 2005 | static bool PrepareZramBackingDevice(off64_t size) { |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2006 | |
Minchan Kim | 12e9615 | 2019-10-15 14:41:18 -0700 | [diff] [blame] | 2007 | constexpr const char* file_path = "/data/per_boot/zram_swap"; |
| 2008 | if (size == 0) return true; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2009 | |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2010 | // Prepare target path |
Minchan Kim | 12e9615 | 2019-10-15 14:41:18 -0700 | [diff] [blame] | 2011 | unique_fd target_fd(TEMP_FAILURE_RETRY(open(file_path, O_RDWR | O_CREAT | O_CLOEXEC, 0600))); |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2012 | if (target_fd.get() == -1) { |
Minchan Kim | 12e9615 | 2019-10-15 14:41:18 -0700 | [diff] [blame] | 2013 | PERROR << "Cannot open target path: " << file_path; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2014 | return false; |
| 2015 | } |
| 2016 | if (fallocate(target_fd.get(), 0, 0, size) < 0) { |
Minchan Kim | 12e9615 | 2019-10-15 14:41:18 -0700 | [diff] [blame] | 2017 | PERROR << "Cannot truncate target path: " << file_path; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2018 | return false; |
| 2019 | } |
| 2020 | |
Nikita Ioffe | bcaeb70 | 2020-04-20 17:38:17 +0100 | [diff] [blame] | 2021 | // Allocate loop device and attach it to file_path. |
| 2022 | LoopControl loop_control; |
Bart Van Assche | 06b95de | 2021-07-29 13:53:53 -0700 | [diff] [blame] | 2023 | std::string loop_device; |
| 2024 | if (!loop_control.Attach(target_fd.get(), 5s, &loop_device)) { |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2025 | return false; |
| 2026 | } |
| 2027 | |
Bart Van Assche | ec5f635 | 2021-07-29 13:50:43 -0700 | [diff] [blame] | 2028 | ConfigureIoScheduler(loop_device); |
| 2029 | |
Jiyong Park | d185d4a | 2021-12-11 10:45:20 +0900 | [diff] [blame] | 2030 | if (auto ret = ConfigureQueueDepth(loop_device, "/"); !ret.ok()) { |
| 2031 | LOG(DEBUG) << "Failed to config queue depth: " << ret.error().message(); |
| 2032 | } |
Bart Van Assche | 0223cd8 | 2021-08-06 10:21:12 -0700 | [diff] [blame] | 2033 | |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2034 | // set block size & direct IO |
Bart Van Assche | 06b95de | 2021-07-29 13:53:53 -0700 | [diff] [blame] | 2035 | unique_fd loop_fd(TEMP_FAILURE_RETRY(open(loop_device.c_str(), O_RDWR | O_CLOEXEC))); |
| 2036 | if (loop_fd.get() == -1) { |
| 2037 | PERROR << "Cannot open " << loop_device; |
Nikita Ioffe | bcaeb70 | 2020-04-20 17:38:17 +0100 | [diff] [blame] | 2038 | return false; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2039 | } |
Liangcai Fan | d884ba5 | 2021-09-06 17:56:18 +0800 | [diff] [blame] | 2040 | if (!LoopControl::SetAutoClearStatus(loop_fd.get())) { |
| 2041 | PERROR << "Failed set LO_FLAGS_AUTOCLEAR for " << loop_device; |
| 2042 | } |
Bart Van Assche | 06b95de | 2021-07-29 13:53:53 -0700 | [diff] [blame] | 2043 | if (!LoopControl::EnableDirectIo(loop_fd.get())) { |
Nikita Ioffe | bcaeb70 | 2020-04-20 17:38:17 +0100 | [diff] [blame] | 2044 | return false; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
Bart Van Assche | 06b95de | 2021-07-29 13:53:53 -0700 | [diff] [blame] | 2047 | return InstallZramDevice(loop_device); |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 2048 | } |
| 2049 | |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2050 | bool fs_mgr_swapon_all(const Fstab& fstab) { |
| 2051 | bool ret = true; |
| 2052 | for (const auto& entry : fstab) { |
| 2053 | // Skip non-swap entries. |
| 2054 | if (entry.fs_type != "swap") { |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2055 | continue; |
| 2056 | } |
| 2057 | |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2058 | if (entry.zram_size > 0) { |
JeongHyeon Lee | c159edc | 2021-06-24 11:21:08 +0900 | [diff] [blame] | 2059 | if (!PrepareZramBackingDevice(entry.zram_backingdev_size)) { |
Minchan Kim | 12e9615 | 2019-10-15 14:41:18 -0700 | [diff] [blame] | 2060 | LERROR << "Failure of zram backing device file for '" << entry.blk_device << "'"; |
| 2061 | } |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2062 | // A zram_size was specified, so we need to configure the |
| 2063 | // device. There is no point in having multiple zram devices |
| 2064 | // on a system (all the memory comes from the same pool) so |
| 2065 | // we can assume the device number is 0. |
| 2066 | if (entry.max_comp_streams >= 0) { |
Tom Cherry | f274e78 | 2018-10-03 13:13:41 -0700 | [diff] [blame] | 2067 | auto zram_mcs_fp = std::unique_ptr<FILE, decltype(&fclose)>{ |
| 2068 | fopen(ZRAM_CONF_MCS, "re"), fclose}; |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2069 | if (zram_mcs_fp == nullptr) { |
Tom Cherry | f274e78 | 2018-10-03 13:13:41 -0700 | [diff] [blame] | 2070 | LERROR << "Unable to open zram conf comp device " << ZRAM_CONF_MCS; |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2071 | ret = false; |
Tom Cherry | f274e78 | 2018-10-03 13:13:41 -0700 | [diff] [blame] | 2072 | continue; |
| 2073 | } |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2074 | fprintf(zram_mcs_fp.get(), "%d\n", entry.max_comp_streams); |
Peter Enderborg | 4d217f0 | 2016-08-26 15:09:35 +0200 | [diff] [blame] | 2075 | } |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2076 | |
Tom Cherry | f274e78 | 2018-10-03 13:13:41 -0700 | [diff] [blame] | 2077 | auto zram_fp = |
| 2078 | std::unique_ptr<FILE, decltype(&fclose)>{fopen(ZRAM_CONF_DEV, "re+"), fclose}; |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2079 | if (zram_fp == nullptr) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 2080 | LERROR << "Unable to open zram conf device " << ZRAM_CONF_DEV; |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2081 | ret = false; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2082 | continue; |
| 2083 | } |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2084 | fprintf(zram_fp.get(), "%" PRId64 "\n", entry.zram_size); |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
David Anderson | 5c475c7 | 2019-06-11 17:40:49 -0700 | [diff] [blame] | 2087 | if (entry.fs_mgr_flags.wait && !WaitForFile(entry.blk_device, 20s)) { |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2088 | LERROR << "Skipping mkswap for '" << entry.blk_device << "'"; |
| 2089 | ret = false; |
Jinguang Dong | 9d34496 | 2017-06-13 10:20:34 +0800 | [diff] [blame] | 2090 | continue; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2093 | // Initialize the swap area. |
| 2094 | const char* mkswap_argv[2] = { |
| 2095 | MKSWAP_BIN, |
| 2096 | entry.blk_device.c_str(), |
| 2097 | }; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 2098 | int err = logwrap_fork_execvp(ARRAY_SIZE(mkswap_argv), mkswap_argv, nullptr, false, |
| 2099 | LOG_KLOG, false, nullptr); |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2100 | if (err) { |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2101 | LERROR << "mkswap failed for " << entry.blk_device; |
| 2102 | ret = false; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2103 | continue; |
| 2104 | } |
| 2105 | |
| 2106 | /* If -1, then no priority was specified in fstab, so don't set |
| 2107 | * SWAP_FLAG_PREFER or encode the priority */ |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2108 | int flags = 0; |
| 2109 | if (entry.swap_prio >= 0) { |
| 2110 | flags = (entry.swap_prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2111 | flags |= SWAP_FLAG_PREFER; |
| 2112 | } else { |
| 2113 | flags = 0; |
| 2114 | } |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2115 | err = swapon(entry.blk_device.c_str(), flags); |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2116 | if (err) { |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 2117 | LERROR << "swapon failed for " << entry.blk_device; |
| 2118 | ret = false; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | return ret; |
| 2123 | } |
| 2124 | |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 2125 | bool fs_mgr_is_verity_enabled(const FstabEntry& entry) { |
David Anderson | 6cdd9bd | 2021-10-29 16:13:22 -0700 | [diff] [blame] | 2126 | if (!entry.fs_mgr_flags.avb) { |
Bowgo Tsai | aaf70e7 | 2017-03-02 00:03:56 +0800 | [diff] [blame] | 2127 | return false; |
| 2128 | } |
| 2129 | |
David Anderson | 40b5948 | 2018-06-25 17:55:01 -0700 | [diff] [blame] | 2130 | DeviceMapper& dm = DeviceMapper::Instance(); |
| 2131 | |
David Anderson | 7082f68 | 2019-03-18 19:17:11 -0700 | [diff] [blame] | 2132 | std::string mount_point = GetVerityDeviceName(entry); |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 2133 | if (dm.GetState(mount_point) == DmDeviceState::INVALID) { |
| 2134 | return false; |
| 2135 | } |
| 2136 | |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 2137 | std::vector<DeviceMapper::TargetInfo> table; |
| 2138 | if (!dm.GetTableStatus(mount_point, &table) || table.empty() || table[0].data.empty()) { |
David Anderson | 6cdd9bd | 2021-10-29 16:13:22 -0700 | [diff] [blame] | 2139 | return false; |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 2140 | } |
| 2141 | |
David Anderson | 6cdd9bd | 2021-10-29 16:13:22 -0700 | [diff] [blame] | 2142 | auto status = table[0].data.c_str(); |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 2143 | if (*status == 'C' || *status == 'V') { |
| 2144 | return true; |
| 2145 | } |
| 2146 | |
| 2147 | return false; |
Bowgo Tsai | aaf70e7 | 2017-03-02 00:03:56 +0800 | [diff] [blame] | 2148 | } |
David Anderson | 5cbd2e4 | 2018-09-27 10:53:04 -0700 | [diff] [blame] | 2149 | |
Tianjie | 10bec65 | 2021-08-30 16:10:09 -0700 | [diff] [blame] | 2150 | std::optional<HashtreeInfo> fs_mgr_get_hashtree_info(const android::fs_mgr::FstabEntry& entry) { |
David Anderson | 6cdd9bd | 2021-10-29 16:13:22 -0700 | [diff] [blame] | 2151 | if (!entry.fs_mgr_flags.avb) { |
Tianjie | 10bec65 | 2021-08-30 16:10:09 -0700 | [diff] [blame] | 2152 | return {}; |
Tianjie | 327237d | 2021-01-20 19:02:34 -0800 | [diff] [blame] | 2153 | } |
| 2154 | DeviceMapper& dm = DeviceMapper::Instance(); |
| 2155 | std::string device = GetVerityDeviceName(entry); |
| 2156 | |
| 2157 | std::vector<DeviceMapper::TargetInfo> table; |
| 2158 | if (dm.GetState(device) == DmDeviceState::INVALID || !dm.GetTableInfo(device, &table)) { |
Tianjie | 10bec65 | 2021-08-30 16:10:09 -0700 | [diff] [blame] | 2159 | return {}; |
Tianjie | 327237d | 2021-01-20 19:02:34 -0800 | [diff] [blame] | 2160 | } |
| 2161 | for (const auto& target : table) { |
| 2162 | if (strcmp(target.spec.target_type, "verity") != 0) { |
| 2163 | continue; |
| 2164 | } |
| 2165 | |
| 2166 | // The format is stable for dm-verity version 0 & 1. And the data is expected to have |
| 2167 | // the fixed format: |
| 2168 | // <version> <dev> <hash_dev> <data_block_size> <hash_block_size> <num_data_blocks> |
| 2169 | // <hash_start_block> <algorithm> <digest> <salt> |
| 2170 | // Details in https://www.kernel.org/doc/html/latest/admin-guide/device-mapper/verity.html |
| 2171 | |
| 2172 | std::vector<std::string> tokens = android::base::Split(target.data, " \t\r\n"); |
| 2173 | if (tokens[0] != "0" && tokens[0] != "1") { |
| 2174 | LOG(WARNING) << "Unrecognized device mapper version in " << target.data; |
Tianjie | 10bec65 | 2021-08-30 16:10:09 -0700 | [diff] [blame] | 2175 | return {}; |
Tianjie | 327237d | 2021-01-20 19:02:34 -0800 | [diff] [blame] | 2176 | } |
| 2177 | |
Tianjie | 10bec65 | 2021-08-30 16:10:09 -0700 | [diff] [blame] | 2178 | // Hashtree algorithm & root digest are the 8th & 9th token in the output. |
| 2179 | return HashtreeInfo{.algorithm = android::base::Trim(tokens[7]), |
| 2180 | .root_digest = android::base::Trim(tokens[8])}; |
Tianjie | 327237d | 2021-01-20 19:02:34 -0800 | [diff] [blame] | 2181 | } |
| 2182 | |
Tianjie | 10bec65 | 2021-08-30 16:10:09 -0700 | [diff] [blame] | 2183 | return {}; |
Tianjie | 327237d | 2021-01-20 19:02:34 -0800 | [diff] [blame] | 2184 | } |
| 2185 | |
David Anderson | ee725f6 | 2019-03-13 19:10:48 -0700 | [diff] [blame] | 2186 | bool fs_mgr_verity_is_check_at_most_once(const android::fs_mgr::FstabEntry& entry) { |
David Anderson | 6cdd9bd | 2021-10-29 16:13:22 -0700 | [diff] [blame] | 2187 | if (!entry.fs_mgr_flags.avb) { |
David Anderson | ee725f6 | 2019-03-13 19:10:48 -0700 | [diff] [blame] | 2188 | return false; |
| 2189 | } |
| 2190 | |
| 2191 | DeviceMapper& dm = DeviceMapper::Instance(); |
David Anderson | 7082f68 | 2019-03-18 19:17:11 -0700 | [diff] [blame] | 2192 | std::string device = GetVerityDeviceName(entry); |
David Anderson | ee725f6 | 2019-03-13 19:10:48 -0700 | [diff] [blame] | 2193 | |
| 2194 | std::vector<DeviceMapper::TargetInfo> table; |
| 2195 | if (dm.GetState(device) == DmDeviceState::INVALID || !dm.GetTableInfo(device, &table)) { |
| 2196 | return false; |
| 2197 | } |
| 2198 | for (const auto& target : table) { |
| 2199 | if (strcmp(target.spec.target_type, "verity") == 0 && |
| 2200 | target.data.find("check_at_most_once") != std::string::npos) { |
| 2201 | return true; |
| 2202 | } |
| 2203 | } |
| 2204 | return false; |
| 2205 | } |
| 2206 | |
David Anderson | 0bfa1c8 | 2018-10-30 15:06:46 -0700 | [diff] [blame] | 2207 | std::string fs_mgr_get_super_partition_name(int slot) { |
| 2208 | // Devices upgrading to dynamic partitions are allowed to specify a super |
David Anderson | 0b0ee7a | 2019-04-18 14:14:33 -0700 | [diff] [blame] | 2209 | // partition name. This includes cuttlefish, which is a non-A/B device. |
David Anderson | 0bfa1c8 | 2018-10-30 15:06:46 -0700 | [diff] [blame] | 2210 | std::string super_partition; |
David Anderson | dac0476 | 2022-02-28 21:38:38 -0800 | [diff] [blame] | 2211 | if (fs_mgr_get_boot_config("force_super_partition", &super_partition)) { |
| 2212 | return super_partition; |
| 2213 | } |
| 2214 | if (fs_mgr_get_boot_config("super_partition", &super_partition)) { |
David Anderson | 0b0ee7a | 2019-04-18 14:14:33 -0700 | [diff] [blame] | 2215 | if (fs_mgr_get_slot_suffix().empty()) { |
| 2216 | return super_partition; |
| 2217 | } |
David Anderson | 0bfa1c8 | 2018-10-30 15:06:46 -0700 | [diff] [blame] | 2218 | std::string suffix; |
| 2219 | if (slot == 0) { |
| 2220 | suffix = "_a"; |
| 2221 | } else if (slot == 1) { |
| 2222 | suffix = "_b"; |
| 2223 | } else if (slot == -1) { |
| 2224 | suffix = fs_mgr_get_slot_suffix(); |
| 2225 | } |
David Anderson | 0bfa1c8 | 2018-10-30 15:06:46 -0700 | [diff] [blame] | 2226 | return super_partition + suffix; |
| 2227 | } |
David Anderson | 5cbd2e4 | 2018-09-27 10:53:04 -0700 | [diff] [blame] | 2228 | return LP_METADATA_DEFAULT_PARTITION_NAME; |
| 2229 | } |
Yi-Yo Chiang | 23816e8 | 2021-05-19 17:19:58 +0800 | [diff] [blame] | 2230 | |
Yi-Yo Chiang | 3431d52 | 2021-05-27 22:29:32 +0800 | [diff] [blame] | 2231 | bool fs_mgr_create_canonical_mount_point(const std::string& mount_point) { |
| 2232 | auto saved_errno = errno; |
| 2233 | auto ok = true; |
| 2234 | auto created_mount_point = !mkdir(mount_point.c_str(), 0755); |
| 2235 | std::string real_mount_point; |
| 2236 | if (!Realpath(mount_point, &real_mount_point)) { |
| 2237 | ok = false; |
| 2238 | PERROR << "failed to realpath(" << mount_point << ")"; |
| 2239 | } else if (mount_point != real_mount_point) { |
| 2240 | ok = false; |
| 2241 | LERROR << "mount point is not canonical: realpath(" << mount_point << ") -> " |
| 2242 | << real_mount_point; |
| 2243 | } |
| 2244 | if (!ok && created_mount_point) { |
| 2245 | rmdir(mount_point.c_str()); |
| 2246 | } |
| 2247 | errno = saved_errno; |
| 2248 | return ok; |
| 2249 | } |
| 2250 | |
Yi-Yo Chiang | 23816e8 | 2021-05-19 17:19:58 +0800 | [diff] [blame] | 2251 | bool fs_mgr_mount_overlayfs_fstab_entry(const FstabEntry& entry) { |
| 2252 | auto overlayfs_valid_result = fs_mgr_overlayfs_valid(); |
| 2253 | if (overlayfs_valid_result == OverlayfsValidResult::kNotSupported) { |
| 2254 | LERROR << __FUNCTION__ << "(): kernel does not support overlayfs"; |
| 2255 | return false; |
| 2256 | } |
| 2257 | |
| 2258 | #if ALLOW_ADBD_DISABLE_VERITY == 0 |
| 2259 | // Allowlist the mount point if user build. |
| 2260 | static const std::vector<const std::string> kAllowedPaths = { |
Ramji Jiyani | 023d897 | 2022-02-08 02:04:24 +0000 | [diff] [blame] | 2261 | "/odm", "/odm_dlkm", "/oem", "/product", |
| 2262 | "/system_dlkm", "/system_ext", "/vendor", "/vendor_dlkm", |
Yi-Yo Chiang | 23816e8 | 2021-05-19 17:19:58 +0800 | [diff] [blame] | 2263 | }; |
| 2264 | static const std::vector<const std::string> kAllowedPrefixes = { |
| 2265 | "/mnt/product/", |
| 2266 | "/mnt/vendor/", |
| 2267 | }; |
| 2268 | if (std::none_of(kAllowedPaths.begin(), kAllowedPaths.end(), |
| 2269 | [&entry](const auto& path) -> bool { |
| 2270 | return entry.mount_point == path || |
| 2271 | StartsWith(entry.mount_point, path + "/"); |
| 2272 | }) && |
| 2273 | std::none_of(kAllowedPrefixes.begin(), kAllowedPrefixes.end(), |
| 2274 | [&entry](const auto& prefix) -> bool { |
| 2275 | return entry.mount_point != prefix && |
| 2276 | StartsWith(entry.mount_point, prefix); |
| 2277 | })) { |
| 2278 | LERROR << __FUNCTION__ |
| 2279 | << "(): mount point is forbidden on user build: " << entry.mount_point; |
| 2280 | return false; |
| 2281 | } |
| 2282 | #endif // ALLOW_ADBD_DISABLE_VERITY == 0 |
| 2283 | |
Yi-Yo Chiang | 3431d52 | 2021-05-27 22:29:32 +0800 | [diff] [blame] | 2284 | if (!fs_mgr_create_canonical_mount_point(entry.mount_point)) { |
Yi-Yo Chiang | 23816e8 | 2021-05-19 17:19:58 +0800 | [diff] [blame] | 2285 | return false; |
| 2286 | } |
| 2287 | |
Yi-Yo Chiang | e7783a9 | 2021-08-03 01:02:31 +0800 | [diff] [blame] | 2288 | auto lowerdir = entry.lowerdir; |
| 2289 | if (entry.fs_mgr_flags.overlayfs_remove_missing_lowerdir) { |
| 2290 | bool removed_any = false; |
| 2291 | std::vector<std::string> lowerdirs; |
| 2292 | for (const auto& dir : android::base::Split(entry.lowerdir, ":")) { |
| 2293 | if (access(dir.c_str(), F_OK)) { |
| 2294 | PWARNING << __FUNCTION__ << "(): remove missing lowerdir '" << dir << "'"; |
| 2295 | removed_any = true; |
| 2296 | } else { |
| 2297 | lowerdirs.push_back(dir); |
| 2298 | } |
| 2299 | } |
| 2300 | if (removed_any) { |
| 2301 | lowerdir = android::base::Join(lowerdirs, ":"); |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | auto options = "lowerdir=" + lowerdir; |
Yi-Yo Chiang | 23816e8 | 2021-05-19 17:19:58 +0800 | [diff] [blame] | 2306 | if (overlayfs_valid_result == OverlayfsValidResult::kOverrideCredsRequired) { |
| 2307 | options += ",override_creds=off"; |
| 2308 | } |
| 2309 | |
| 2310 | // Use "overlay-" + entry.blk_device as the mount() source, so that adb-remout-test don't |
| 2311 | // confuse this with adb remount overlay, whose device name is "overlay". |
| 2312 | // Overlayfs is a pseudo filesystem, so the source device is a symbolic value and isn't used to |
| 2313 | // back the filesystem. However the device name would be shown in /proc/mounts. |
| 2314 | auto source = "overlay-" + entry.blk_device; |
| 2315 | auto report = "__mount(source=" + source + ",target=" + entry.mount_point + ",type=overlay," + |
| 2316 | options + ")="; |
| 2317 | auto ret = mount(source.c_str(), entry.mount_point.c_str(), "overlay", MS_RDONLY | MS_NOATIME, |
| 2318 | options.c_str()); |
| 2319 | if (ret) { |
| 2320 | PERROR << report << ret; |
| 2321 | return false; |
| 2322 | } |
| 2323 | LINFO << report << ret; |
| 2324 | return true; |
| 2325 | } |
David Anderson | 6cdd9bd | 2021-10-29 16:13:22 -0700 | [diff] [blame] | 2326 | |
| 2327 | bool fs_mgr_load_verity_state(int* mode) { |
| 2328 | // unless otherwise specified, use EIO mode. |
| 2329 | *mode = VERITY_MODE_EIO; |
| 2330 | |
| 2331 | // The bootloader communicates verity mode via the kernel commandline |
| 2332 | std::string verity_mode; |
| 2333 | if (!fs_mgr_get_boot_config("veritymode", &verity_mode)) { |
| 2334 | return false; |
| 2335 | } |
| 2336 | |
| 2337 | if (verity_mode == "enforcing") { |
| 2338 | *mode = VERITY_MODE_DEFAULT; |
| 2339 | } else if (verity_mode == "logging") { |
| 2340 | *mode = VERITY_MODE_LOGGING; |
| 2341 | } |
| 2342 | |
| 2343 | return true; |
| 2344 | } |