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