blob: 6f59ed36eb6e0e95e1d73434281ad4e0c5c94de0 [file] [log] [blame]
Chris Fries79f33842013-09-05 13:19:21 -05001/*
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>
William Roberts875476d2016-05-13 11:19:42 -070026
Daniel Rosenberg29bc5d42020-01-31 18:27:41 -080027#include <android-base/properties.h>
Tom Cherryc3e7bd32018-12-06 10:28:26 -080028#include <android-base/unique_fd.h>
Tao Bao6d881d62016-10-05 17:53:30 -070029#include <ext4_utils/ext4.h>
Jin Qianf71bc0c2017-04-21 16:56:34 -070030#include <ext4_utils/ext4_utils.h>
31#include <logwrap/logwrap.h>
William Roberts875476d2016-05-13 11:19:42 -070032#include <selinux/android.h>
Jin Qianf71bc0c2017-04-21 16:56:34 -070033#include <selinux/label.h>
34#include <selinux/selinux.h>
William Roberts875476d2016-05-13 11:19:42 -070035
Chris Fries79f33842013-09-05 13:19:21 -050036#include "fs_mgr_priv.h"
37
Tom Cherryc3e7bd32018-12-06 10:28:26 -080038using android::base::unique_fd;
Chris Fries79f33842013-09-05 13:19:21 -050039
Tom Cherrya3530e62019-01-30 13:25:35 -080040// Realistically, this file should be part of the android::fs_mgr namespace;
41using namespace android::fs_mgr;
42
Tom Cherryc3e7bd32018-12-06 10:28:26 -080043static int get_dev_sz(const std::string& fs_blkdev, uint64_t* dev_sz) {
44 unique_fd fd(TEMP_FAILURE_RETRY(open(fs_blkdev.c_str(), O_RDONLY | O_CLOEXEC)));
45
46 if (fd < 0) {
bowgotsai47878de2017-01-23 14:04:34 +080047 PERROR << "Cannot open block device";
Chris Fries79f33842013-09-05 13:19:21 -050048 return -1;
49 }
50
Jaegeuk Kima3674302017-06-12 16:57:56 -070051 if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) {
bowgotsai47878de2017-01-23 14:04:34 +080052 PERROR << "Cannot get block device size";
Chris Fries79f33842013-09-05 13:19:21 -050053 return -1;
54 }
55
Jaegeuk Kima3674302017-06-12 16:57:56 -070056 return 0;
57}
58
Tom Cherryc3e7bd32018-12-06 10:28:26 -080059static int format_ext4(const std::string& fs_blkdev, const std::string& fs_mnt_point,
Eric Biggers4d0c5ef2021-11-08 16:38:54 -080060 bool needs_projid, bool needs_metadata_csum) {
Jaegeuk Kima3674302017-06-12 16:57:56 -070061 uint64_t dev_sz;
62 int rc = 0;
Jaegeuk Kima3674302017-06-12 16:57:56 -070063
64 rc = get_dev_sz(fs_blkdev, &dev_sz);
65 if (rc) {
66 return rc;
67 }
Chris Fries79f33842013-09-05 13:19:21 -050068
Jin Qianf71bc0c2017-04-21 16:56:34 -070069 /* Format the partition using the calculated length */
Jin Qianf71bc0c2017-04-21 16:56:34 -070070
71 std::string size_str = std::to_string(dev_sz / 4096);
Jin Qianf71bc0c2017-04-21 16:56:34 -070072
Daniel Rosenberg29bc5d42020-01-31 18:27:41 -080073 std::vector<const char*> mke2fs_args = {"/system/bin/mke2fs", "-t", "ext4", "-b", "4096"};
74
75 // Project ID's require wider inodes. The Quotas themselves are enabled by tune2fs during boot.
76 if (needs_projid) {
77 mke2fs_args.push_back("-I");
78 mke2fs_args.push_back("512");
79 }
80 // casefolding is enabled via tune2fs during boot.
81
Jaegeuk Kim78f040c2020-02-11 15:22:46 -080082 if (needs_metadata_csum) {
83 mke2fs_args.push_back("-O");
84 mke2fs_args.push_back("metadata_csum");
85 // tune2fs recommends to enable 64bit and extent:
86 // Extents are not enabled. The file extent tree can be checksummed,
87 // whereas block maps cannot. Not enabling extents reduces the coverage
88 // of metadata checksumming. Re-run with -O extent to rectify.
89 // 64-bit filesystem support is not enabled. The larger fields afforded
90 // by this feature enable full-strength checksumming. Run resize2fs -b to rectify.
91 mke2fs_args.push_back("-O");
92 mke2fs_args.push_back("64bit");
93 mke2fs_args.push_back("-O");
94 mke2fs_args.push_back("extent");
95 }
96
Daniel Rosenberg29bc5d42020-01-31 18:27:41 -080097 mke2fs_args.push_back(fs_blkdev.c_str());
98 mke2fs_args.push_back(size_str.c_str());
99
Wei Wang02e89ec2020-02-14 14:22:16 -0800100 rc = logwrap_fork_execvp(mke2fs_args.size(), mke2fs_args.data(), nullptr, false, LOG_KLOG,
101 false, nullptr);
Jin Qianf71bc0c2017-04-21 16:56:34 -0700102 if (rc) {
103 LERROR << "mke2fs returned " << rc;
104 return rc;
105 }
106
107 const char* const e2fsdroid_args[] = {
Tom Cherryc3e7bd32018-12-06 10:28:26 -0800108 "/system/bin/e2fsdroid", "-e", "-a", fs_mnt_point.c_str(), fs_blkdev.c_str(), nullptr};
Jin Qianf71bc0c2017-04-21 16:56:34 -0700109
Tom Cherry3a803eb2019-09-25 16:23:50 -0700110 rc = logwrap_fork_execvp(arraysize(e2fsdroid_args), e2fsdroid_args, nullptr, false, LOG_KLOG,
Wei Wang02e89ec2020-02-14 14:22:16 -0800111 false, nullptr);
Jin Qianf71bc0c2017-04-21 16:56:34 -0700112 if (rc) {
113 LERROR << "e2fsdroid returned " << rc;
William Roberts875476d2016-05-13 11:19:42 -0700114 }
115
Chris Fries79f33842013-09-05 13:19:21 -0500116 return rc;
117}
118
Eric Biggers4d0c5ef2021-11-08 16:38:54 -0800119static int format_f2fs(const std::string& fs_blkdev, uint64_t dev_sz, bool needs_projid,
120 bool needs_casefold, bool fs_compress) {
Jaegeuk Kima3674302017-06-12 16:57:56 -0700121 if (!dev_sz) {
122 int rc = get_dev_sz(fs_blkdev, &dev_sz);
123 if (rc) {
124 return rc;
125 }
126 }
127
128 /* Format the partition using the calculated length */
Jaegeuk Kima3674302017-06-12 16:57:56 -0700129
130 std::string size_str = std::to_string(dev_sz / 4096);
Chris Fries79f33842013-09-05 13:19:21 -0500131
Daniel Rosenberg29bc5d42020-01-31 18:27:41 -0800132 std::vector<const char*> args = {"/system/bin/make_f2fs", "-g", "android"};
133 if (needs_projid) {
134 args.push_back("-O");
135 args.push_back("project_quota,extra_attr");
136 }
137 if (needs_casefold) {
138 args.push_back("-O");
139 args.push_back("casefold");
140 args.push_back("-C");
141 args.push_back("utf8");
142 }
Jaegeuk Kim4076dc92020-01-13 11:02:28 -0800143 if (fs_compress) {
144 args.push_back("-O");
145 args.push_back("compression");
146 args.push_back("-O");
147 args.push_back("extra_attr");
148 }
Daniel Rosenberg29bc5d42020-01-31 18:27:41 -0800149 args.push_back(fs_blkdev.c_str());
150 args.push_back(size_str.c_str());
151
Wei Wang02e89ec2020-02-14 14:22:16 -0800152 return logwrap_fork_execvp(args.size(), args.data(), nullptr, false, LOG_KLOG, false, nullptr);
Chris Fries79f33842013-09-05 13:19:21 -0500153}
154
Eric Biggers4d0c5ef2021-11-08 16:38:54 -0800155int fs_mgr_do_format(const FstabEntry& entry) {
Tom Cherry23319eb2018-11-30 16:16:05 -0800156 LERROR << __FUNCTION__ << ": Format " << entry.blk_device << " as '" << entry.fs_type << "'";
Chris Fries79f33842013-09-05 13:19:21 -0500157
Daniel Rosenberg29bc5d42020-01-31 18:27:41 -0800158 bool needs_casefold = false;
Shikha Malhotrabc9b8472021-12-29 16:26:18 +0000159 bool needs_projid = true;
Daniel Rosenberg29bc5d42020-01-31 18:27:41 -0800160
161 if (entry.mount_point == "/data") {
Martijn Coenenea751d92020-04-15 11:45:05 +0200162 needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
Daniel Rosenberg29bc5d42020-01-31 18:27:41 -0800163 }
164
Tom Cherry23319eb2018-11-30 16:16:05 -0800165 if (entry.fs_type == "f2fs") {
Eric Biggers4d0c5ef2021-11-08 16:38:54 -0800166 return format_f2fs(entry.blk_device, entry.length, needs_projid, needs_casefold,
167 entry.fs_mgr_flags.fs_compress);
Tom Cherry23319eb2018-11-30 16:16:05 -0800168 } else if (entry.fs_type == "ext4") {
Eric Biggers4d0c5ef2021-11-08 16:38:54 -0800169 return format_ext4(entry.blk_device, entry.mount_point, needs_projid,
Jaegeuk Kim78f040c2020-02-11 15:22:46 -0800170 entry.fs_mgr_flags.ext_meta_csum);
Chris Fries79f33842013-09-05 13:19:21 -0500171 } else {
Tom Cherry23319eb2018-11-30 16:16:05 -0800172 LERROR << "File system type '" << entry.fs_type << "' is not supported";
173 return -EINVAL;
Chris Fries79f33842013-09-05 13:19:21 -0500174 }
Tom Cherry23319eb2018-11-30 16:16:05 -0800175}