xref: /aosp_15_r20/system/core/fs_mgr/fs_mgr_format.cpp (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <sys/wait.h>
23 #include <errno.h>
24 #include <cutils/partition_utils.h>
25 #include <sys/mount.h>
26 
27 #include <android-base/properties.h>
28 #include <android-base/unique_fd.h>
29 #include <ext4_utils/ext4.h>
30 #include <ext4_utils/ext4_utils.h>
31 #include <logwrap/logwrap.h>
32 #include <selinux/android.h>
33 #include <selinux/label.h>
34 #include <selinux/selinux.h>
35 #include <filesystem>
36 #include <string>
37 
38 #include "fs_mgr_priv.h"
39 
40 using android::base::unique_fd;
41 
42 // Realistically, this file should be part of the android::fs_mgr namespace;
43 using namespace android::fs_mgr;
44 
get_dev_sz(const std::string & fs_blkdev,uint64_t * dev_sz)45 static int get_dev_sz(const std::string& fs_blkdev, uint64_t* dev_sz) {
46     unique_fd fd(TEMP_FAILURE_RETRY(open(fs_blkdev.c_str(), O_RDONLY | O_CLOEXEC)));
47 
48     if (fd < 0) {
49         PERROR << "Cannot open block device";
50         return -1;
51     }
52 
53     if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) {
54         PERROR << "Cannot get block device size";
55         return -1;
56     }
57 
58     return 0;
59 }
60 
format_ext4(const std::string & fs_blkdev,const std::string & fs_mnt_point,bool needs_projid,bool needs_metadata_csum)61 static int format_ext4(const std::string& fs_blkdev, const std::string& fs_mnt_point,
62                        bool needs_projid, bool needs_metadata_csum) {
63     uint64_t dev_sz;
64     int rc = 0;
65 
66     rc = get_dev_sz(fs_blkdev, &dev_sz);
67     if (rc) {
68         return rc;
69     }
70 
71     /* Format the partition using the calculated length */
72 
73     // EXT4 supports 4K block size on 16K page sizes. A 4K block
74     // size formatted EXT4 partition will mount fine on both 4K and 16K page
75     // size kernels.
76     // However, EXT4 does not support 16K block size on 4K systems.
77     // If we want the same userspace code to work on both 4k/16k kernels,
78     // using a hardcoded 4096 block size is a simple solution. Using
79     // getpagesize() here would work as well, but 4096 is simpler.
80     std::string size_str = std::to_string(dev_sz / 4096);
81 
82     std::vector<const char*> mke2fs_args = {"/system/bin/mke2fs", "-t", "ext4", "-b", "4096"};
83 
84     // Project ID's require wider inodes. The Quotas themselves are enabled by tune2fs during boot.
85     if (needs_projid) {
86         mke2fs_args.push_back("-I");
87         mke2fs_args.push_back("512");
88     }
89     // casefolding is enabled via tune2fs during boot.
90 
91     if (needs_metadata_csum) {
92         mke2fs_args.push_back("-O");
93         mke2fs_args.push_back("metadata_csum");
94         // tune2fs recommends to enable 64bit and extent:
95         //  Extents are not enabled.  The file extent tree can be checksummed,
96         //  whereas block maps cannot. Not enabling extents reduces the coverage
97         //  of metadata checksumming.  Re-run with -O extent to rectify.
98         //  64-bit filesystem support is not enabled.  The larger fields afforded
99         //  by this feature enable full-strength checksumming.  Run resize2fs -b to rectify.
100         mke2fs_args.push_back("-O");
101         mke2fs_args.push_back("64bit");
102         mke2fs_args.push_back("-O");
103         mke2fs_args.push_back("extent");
104     }
105 
106     mke2fs_args.push_back(fs_blkdev.c_str());
107     mke2fs_args.push_back(size_str.c_str());
108 
109     rc = logwrap_fork_execvp(mke2fs_args.size(), mke2fs_args.data(), nullptr, false, LOG_KLOG,
110                              false, nullptr);
111     if (rc) {
112         LERROR << "mke2fs returned " << rc;
113         return rc;
114     }
115 
116     const char* const e2fsdroid_args[] = {
117             "/system/bin/e2fsdroid", "-e", "-a", fs_mnt_point.c_str(), fs_blkdev.c_str(), nullptr};
118 
119     rc = logwrap_fork_execvp(arraysize(e2fsdroid_args), e2fsdroid_args, nullptr, false, LOG_KLOG,
120                              false, nullptr);
121     if (rc) {
122         LERROR << "e2fsdroid returned " << rc;
123     }
124 
125     return rc;
126 }
127 
format_f2fs(const std::string & fs_blkdev,uint64_t dev_sz,bool needs_projid,bool needs_casefold,bool fs_compress,bool is_zoned,const std::vector<std::string> & user_devices,const std::vector<int> & device_aliased)128 static int format_f2fs(const std::string& fs_blkdev, uint64_t dev_sz, bool needs_projid,
129                        bool needs_casefold, bool fs_compress, bool is_zoned,
130                        const std::vector<std::string>& user_devices,
131                        const std::vector<int>& device_aliased) {
132     if (!dev_sz) {
133         int rc = get_dev_sz(fs_blkdev, &dev_sz);
134         if (rc) {
135             return rc;
136         }
137     }
138 
139     /* Format the partition using the calculated length */
140 
141     const auto size_str = std::to_string(dev_sz / getpagesize());
142     std::string block_size = std::to_string(getpagesize());
143 
144     std::vector<const char*> args = {"/system/bin/make_f2fs", "-g", "android"};
145     if (needs_projid) {
146         args.push_back("-O");
147         args.push_back("project_quota,extra_attr");
148     }
149     if (needs_casefold) {
150         args.push_back("-O");
151         args.push_back("casefold");
152         args.push_back("-C");
153         args.push_back("utf8");
154     }
155     if (fs_compress) {
156         args.push_back("-O");
157         args.push_back("compression");
158         args.push_back("-O");
159         args.push_back("extra_attr");
160     }
161     args.push_back("-w");
162     args.push_back(block_size.c_str());
163     args.push_back("-b");
164     args.push_back(block_size.c_str());
165 
166     if (is_zoned) {
167         args.push_back("-m");
168     }
169     for (size_t i = 0; i < user_devices.size(); i++) {
170         std::string device_name = user_devices[i];
171 
172         args.push_back("-c");
173         if (device_aliased[i]) {
174             std::filesystem::path path = device_name;
175             device_name += "@" + path.filename().string();
176         }
177         args.push_back(device_name.c_str());
178     }
179 
180     if (user_devices.empty()) {
181         args.push_back(fs_blkdev.c_str());
182         args.push_back(size_str.c_str());
183     } else {
184         args.push_back(fs_blkdev.c_str());
185     }
186     return logwrap_fork_execvp(args.size(), args.data(), nullptr, false, LOG_KLOG, false, nullptr);
187 }
188 
fs_mgr_do_format(const FstabEntry & entry)189 int fs_mgr_do_format(const FstabEntry& entry) {
190     LERROR << __FUNCTION__ << ": Format " << entry.blk_device << " as '" << entry.fs_type << "'";
191 
192     bool needs_casefold = false;
193     bool needs_projid = true;
194 
195     if (entry.mount_point == "/data") {
196         needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
197     }
198 
199     if (entry.fs_type == "f2fs") {
200         return format_f2fs(entry.blk_device, entry.length, needs_projid, needs_casefold,
201                            entry.fs_mgr_flags.fs_compress, entry.fs_mgr_flags.is_zoned,
202                            entry.user_devices, entry.device_aliased);
203     } else if (entry.fs_type == "ext4") {
204         return format_ext4(entry.blk_device, entry.mount_point, needs_projid,
205                            entry.fs_mgr_flags.ext_meta_csum);
206     } else {
207         LERROR << "File system type '" << entry.fs_type << "' is not supported";
208         return -EINVAL;
209     }
210 }
211