xref: /aosp_15_r20/system/core/fs_mgr/include/fs_mgr.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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 #pragma once
18 
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <linux/dm-ioctl.h>
23 
24 #include <functional>
25 #include <optional>
26 #include <string>
27 
28 #include <fstab/fstab.h>
29 
30 // Magic number at start of verity metadata
31 #define VERITY_METADATA_MAGIC_NUMBER 0xb001b001
32 
33 // Replacement magic number at start of verity metadata to cleanly
34 // turn verity off in userdebug builds.
35 #define VERITY_METADATA_MAGIC_DISABLE 0x46464f56 // "VOFF"
36 
37 // Verity modes
38 enum verity_mode {
39     VERITY_MODE_EIO = 0,
40     VERITY_MODE_LOGGING = 1,
41     VERITY_MODE_RESTART = 2,
42     VERITY_MODE_LAST = VERITY_MODE_RESTART,
43     VERITY_MODE_DEFAULT = VERITY_MODE_RESTART
44 };
45 
46 // Mount modes
47 enum mount_mode {
48     MOUNT_MODE_DEFAULT = 0,
49     MOUNT_MODE_EARLY = 1,
50     MOUNT_MODE_LATE = 2,
51     // TODO(b/135984674): remove this after refactoring fs_mgr_mount_all.
52     MOUNT_MODE_ONLY_USERDATA = 3
53 };
54 
55 #define FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED 7
56 #define FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION 6
57 #define FS_MGR_MNTALL_DEV_FILE_ENCRYPTED 5
58 #define FS_MGR_MNTALL_DEV_NEEDS_RECOVERY 4
59 #define FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE 0
60 #define FS_MGR_MNTALL_FAIL (-1)
61 // fs_mgr_mount_all() updates fstab entries that reference device-mapper.
62 int fs_mgr_mount_all(android::fs_mgr::Fstab* fstab, int mount_mode);
63 
64 struct HashtreeInfo {
65     // The hash algorithm used to build the merkle tree.
66     std::string algorithm;
67     // The root digest of the merkle tree.
68     std::string root_digest;
69     // If check_at_most_once is enabled.
70     bool check_at_most_once;
71 };
72 
73 #define FS_MGR_DOMNT_FAILED (-1)
74 #define FS_MGR_DOMNT_BUSY (-2)
75 #define FS_MGR_DOMNT_SUCCESS 0
76 int fs_mgr_do_mount(android::fs_mgr::Fstab* fstab, const std::string& n_name,
77                     const std::string& n_blk_device, int needs_checkpoint, bool needs_encrypt);
78 int fs_mgr_do_mount_one(const android::fs_mgr::FstabEntry& entry,
79                         const std::string& mount_point = "");
80 bool fs_mgr_load_verity_state(int* mode);
81 // Returns true if verity is enabled on this particular FstabEntry.
82 bool fs_mgr_is_verity_enabled(const android::fs_mgr::FstabEntry& entry);
83 // Returns the verity hashtree information of this particular FstabEntry. Returns std::nullopt
84 // if the input isn't a dm-verity entry, or if there is an error.
85 std::optional<HashtreeInfo> fs_mgr_get_hashtree_info(const android::fs_mgr::FstabEntry& entry);
86 
87 bool fs_mgr_swapon_all(const android::fs_mgr::Fstab& fstab);
88 bool fs_mgr_update_logical_partition(android::fs_mgr::FstabEntry* entry);
89 
90 // Returns true if the given fstab entry has verity enabled, *and* the verity
91 // device is in "check_at_most_once" mode.
92 bool fs_mgr_verity_is_check_at_most_once(const android::fs_mgr::FstabEntry& entry);
93 
94 int fs_mgr_do_format(const android::fs_mgr::FstabEntry& entry);
95 
96 #define FS_MGR_SETUP_VERITY_SKIPPED  (-3)
97 #define FS_MGR_SETUP_VERITY_DISABLED (-2)
98 #define FS_MGR_SETUP_VERITY_FAIL (-1)
99 #define FS_MGR_SETUP_VERITY_SUCCESS 0
100 int fs_mgr_setup_verity(android::fs_mgr::FstabEntry* fstab, bool wait_for_verity_dev);
101 
102 // Return the name of the super partition if it exists. If a slot number is
103 // specified, the super partition for the corresponding metadata slot will be
104 // returned. Otherwise, it will use the current slot.
105 std::string fs_mgr_get_super_partition_name(int slot = -1);
106 
107 // Set readonly for the block device
108 bool fs_mgr_set_blk_ro(const std::string& blockdev, bool readonly = true);
109 
110 // Check if the block device has ext4 filesystem
111 bool fs_mgr_is_ext4(const std::string& blk_device);
112 
113 enum FsMgrUmountStatus : int {
114     SUCCESS = 0,
115     ERROR_UNKNOWN = 1 << 0,
116     ERROR_UMOUNT = 1 << 1,
117     ERROR_VERITY = 1 << 2,
118     ERROR_DEVICE_MAPPER = 1 << 3,
119 };
120 // fs_mgr_umount_all() is the reverse of fs_mgr_mount_all. In particular,
121 // it destroys verity devices from device mapper after the device is unmounted.
122 int fs_mgr_umount_all(android::fs_mgr::Fstab* fstab);
123 
124 // Finds the dm_bow device on which this block device is stacked, or returns
125 // empty string
126 std::string fs_mgr_find_bow_device(const std::string& block_device);
127 
128 // Creates mount point if not already existed, and checks that mount point is a
129 // canonical path that doesn't contain any symbolic link or /../.
130 bool fs_mgr_create_canonical_mount_point(const std::string& mount_point);
131 
132 // Like fs_mgr_do_mount_one() but for overlayfs fstab entries.
133 // Unlike fs_mgr_overlayfs, mount overlayfs without upperdir and workdir, so the
134 // filesystem cannot be remount read-write.
135 bool fs_mgr_mount_overlayfs_fstab_entry(const android::fs_mgr::FstabEntry& entry);
136 
137 // File name used to track if encryption was interrupted, leading to a known bad fs state
138 std::string fs_mgr_metadata_encryption_in_progress_file_name(
139         const android::fs_mgr::FstabEntry& entry);
140 
141 // Returns the ideal block size for make_f2fs. Returns -1 on failure.
142 int fs_mgr_f2fs_ideal_block_size();
143