• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
36 
37 #include "fs_mgr_priv.h"
38 
39 using android::base::unique_fd;
40 
41 // Realistically, this file should be part of the android::fs_mgr namespace;
42 using namespace android::fs_mgr;
43 
get_dev_sz(const std::string & fs_blkdev,uint64_t * dev_sz)44 static int get_dev_sz(const std::string& fs_blkdev, uint64_t* dev_sz) {
45     unique_fd fd(TEMP_FAILURE_RETRY(open(fs_blkdev.c_str(), O_RDONLY | O_CLOEXEC)));
46 
47     if (fd < 0) {
48         PERROR << "Cannot open block device";
49         return -1;
50     }
51 
52     if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) {
53         PERROR << "Cannot get block device size";
54         return -1;
55     }
56 
57     return 0;
58 }
59 
format_ext4(const std::string & fs_blkdev,const std::string & fs_mnt_point,bool needs_projid,bool needs_metadata_csum)60 static int format_ext4(const std::string& fs_blkdev, const std::string& fs_mnt_point,
61                        bool needs_projid, bool needs_metadata_csum) {
62     uint64_t dev_sz;
63     int rc = 0;
64 
65     rc = get_dev_sz(fs_blkdev, &dev_sz);
66     if (rc) {
67         return rc;
68     }
69 
70     /* Format the partition using the calculated length */
71 
72     // EXT4 supports 4K block size on 16K page sizes. A 4K block
73     // size formatted EXT4 partition will mount fine on both 4K and 16K page
74     // size kernels.
75     // However, EXT4 does not support 16K block size on 4K systems.
76     // If we want the same userspace code to work on both 4k/16k kernels,
77     // using a hardcoded 4096 block size is a simple solution. Using
78     // getpagesize() here would work as well, but 4096 is simpler.
79     std::string size_str = std::to_string(dev_sz / 4096);
80 
81     std::vector<const char*> mke2fs_args = {"/system/bin/mke2fs", "-t", "ext4", "-b", "4096"};
82 
83     // Project ID's require wider inodes. The Quotas themselves are enabled by tune2fs during boot.
84     if (needs_projid) {
85         mke2fs_args.push_back("-I");
86         mke2fs_args.push_back("512");
87     }
88     // casefolding is enabled via tune2fs during boot.
89 
90     if (needs_metadata_csum) {
91         mke2fs_args.push_back("-O");
92         mke2fs_args.push_back("metadata_csum");
93         // tune2fs recommends to enable 64bit and extent:
94         //  Extents are not enabled.  The file extent tree can be checksummed,
95         //  whereas block maps cannot. Not enabling extents reduces the coverage
96         //  of metadata checksumming.  Re-run with -O extent to rectify.
97         //  64-bit filesystem support is not enabled.  The larger fields afforded
98         //  by this feature enable full-strength checksumming.  Run resize2fs -b to rectify.
99         mke2fs_args.push_back("-O");
100         mke2fs_args.push_back("64bit");
101         mke2fs_args.push_back("-O");
102         mke2fs_args.push_back("extent");
103     }
104 
105     mke2fs_args.push_back(fs_blkdev.c_str());
106     mke2fs_args.push_back(size_str.c_str());
107 
108     rc = logwrap_fork_execvp(mke2fs_args.size(), mke2fs_args.data(), nullptr, false, LOG_KLOG,
109                              false, nullptr);
110     if (rc) {
111         LERROR << "mke2fs returned " << rc;
112         return rc;
113     }
114 
115     const char* const e2fsdroid_args[] = {
116             "/system/bin/e2fsdroid", "-e", "-a", fs_mnt_point.c_str(), fs_blkdev.c_str(), nullptr};
117 
118     rc = logwrap_fork_execvp(arraysize(e2fsdroid_args), e2fsdroid_args, nullptr, false, LOG_KLOG,
119                              false, nullptr);
120     if (rc) {
121         LERROR << "e2fsdroid returned " << rc;
122     }
123 
124     return rc;
125 }
126 
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)127 static int format_f2fs(const std::string& fs_blkdev, uint64_t dev_sz, bool needs_projid,
128                        bool needs_casefold, bool fs_compress, bool is_zoned,
129                        const std::vector<std::string>& user_devices) {
130     if (!dev_sz) {
131         int rc = get_dev_sz(fs_blkdev, &dev_sz);
132         if (rc) {
133             return rc;
134         }
135     }
136 
137     /* Format the partition using the calculated length */
138 
139     const auto size_str = std::to_string(dev_sz / getpagesize());
140     std::string block_size = std::to_string(getpagesize());
141 
142     std::vector<const char*> args = {"/system/bin/make_f2fs", "-g", "android"};
143     if (needs_projid) {
144         args.push_back("-O");
145         args.push_back("project_quota,extra_attr");
146     }
147     if (needs_casefold) {
148         args.push_back("-O");
149         args.push_back("casefold");
150         args.push_back("-C");
151         args.push_back("utf8");
152     }
153     if (fs_compress) {
154         args.push_back("-O");
155         args.push_back("compression");
156         args.push_back("-O");
157         args.push_back("extra_attr");
158     }
159     args.push_back("-w");
160     args.push_back(block_size.c_str());
161     args.push_back("-b");
162     args.push_back(block_size.c_str());
163 
164     if (is_zoned) {
165         args.push_back("-m");
166     }
167     for (auto& device : user_devices) {
168         args.push_back("-c");
169         args.push_back(device.c_str());
170     }
171 
172     if (user_devices.empty()) {
173         args.push_back(fs_blkdev.c_str());
174         args.push_back(size_str.c_str());
175     } else {
176         args.push_back(fs_blkdev.c_str());
177     }
178     return logwrap_fork_execvp(args.size(), args.data(), nullptr, false, LOG_KLOG, false, nullptr);
179 }
180 
fs_mgr_do_format(const FstabEntry & entry)181 int fs_mgr_do_format(const FstabEntry& entry) {
182     LERROR << __FUNCTION__ << ": Format " << entry.blk_device << " as '" << entry.fs_type << "'";
183 
184     bool needs_casefold = false;
185     bool needs_projid = true;
186 
187     if (entry.mount_point == "/data") {
188         needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
189     }
190 
191     if (entry.fs_type == "f2fs") {
192         return format_f2fs(entry.blk_device, entry.length, needs_projid, needs_casefold,
193                            entry.fs_mgr_flags.fs_compress, entry.fs_mgr_flags.is_zoned,
194                            entry.user_devices);
195     } else if (entry.fs_type == "ext4") {
196         return format_ext4(entry.blk_device, entry.mount_point, needs_projid,
197                            entry.fs_mgr_flags.ext_meta_csum);
198     } else {
199         LERROR << "File system type '" << entry.fs_type << "' is not supported";
200         return -EINVAL;
201     }
202 }
203