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