Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 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> |
William Roberts | 875476d | 2016-05-13 11:19:42 -0700 | [diff] [blame] | 26 | |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 27 | #include <android-base/properties.h> |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 28 | #include <android-base/unique_fd.h> |
Tao Bao | 6d881d6 | 2016-10-05 17:53:30 -0700 | [diff] [blame] | 29 | #include <ext4_utils/ext4.h> |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 30 | #include <ext4_utils/ext4_utils.h> |
| 31 | #include <logwrap/logwrap.h> |
William Roberts | 875476d | 2016-05-13 11:19:42 -0700 | [diff] [blame] | 32 | #include <selinux/android.h> |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 33 | #include <selinux/label.h> |
| 34 | #include <selinux/selinux.h> |
William Roberts | 875476d | 2016-05-13 11:19:42 -0700 | [diff] [blame] | 35 | |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 36 | #include "fs_mgr_priv.h" |
Chuanxiao Dong | d78dff1 | 2016-03-08 15:54:55 +0100 | [diff] [blame] | 37 | #include "cryptfs.h" |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 38 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 39 | using android::base::unique_fd; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 40 | |
Tom Cherry | a3530e6 | 2019-01-30 13:25:35 -0800 | [diff] [blame] | 41 | // Realistically, this file should be part of the android::fs_mgr namespace; |
| 42 | using namespace android::fs_mgr; |
| 43 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 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) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 48 | PERROR << "Cannot open block device"; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 52 | if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 53 | PERROR << "Cannot get block device size"; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 54 | return -1; |
| 55 | } |
| 56 | |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 60 | static int format_ext4(const std::string& fs_blkdev, const std::string& fs_mnt_point, |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 61 | bool crypt_footer, bool needs_projid, bool needs_metadata_csum) { |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 62 | uint64_t dev_sz; |
| 63 | int rc = 0; |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 64 | |
| 65 | rc = get_dev_sz(fs_blkdev, &dev_sz); |
| 66 | if (rc) { |
| 67 | return rc; |
| 68 | } |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 69 | |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 70 | /* Format the partition using the calculated length */ |
| 71 | if (crypt_footer) { |
| 72 | dev_sz -= CRYPT_FOOTER_OFFSET; |
| 73 | } |
| 74 | |
| 75 | std::string size_str = std::to_string(dev_sz / 4096); |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 76 | |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 77 | std::vector<const char*> mke2fs_args = {"/system/bin/mke2fs", "-t", "ext4", "-b", "4096"}; |
| 78 | |
| 79 | // Project ID's require wider inodes. The Quotas themselves are enabled by tune2fs during boot. |
| 80 | if (needs_projid) { |
| 81 | mke2fs_args.push_back("-I"); |
| 82 | mke2fs_args.push_back("512"); |
| 83 | } |
| 84 | // casefolding is enabled via tune2fs during boot. |
| 85 | |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 86 | if (needs_metadata_csum) { |
| 87 | mke2fs_args.push_back("-O"); |
| 88 | mke2fs_args.push_back("metadata_csum"); |
| 89 | // tune2fs recommends to enable 64bit and extent: |
| 90 | // Extents are not enabled. The file extent tree can be checksummed, |
| 91 | // whereas block maps cannot. Not enabling extents reduces the coverage |
| 92 | // of metadata checksumming. Re-run with -O extent to rectify. |
| 93 | // 64-bit filesystem support is not enabled. The larger fields afforded |
| 94 | // by this feature enable full-strength checksumming. Run resize2fs -b to rectify. |
| 95 | mke2fs_args.push_back("-O"); |
| 96 | mke2fs_args.push_back("64bit"); |
| 97 | mke2fs_args.push_back("-O"); |
| 98 | mke2fs_args.push_back("extent"); |
| 99 | } |
| 100 | |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 101 | mke2fs_args.push_back(fs_blkdev.c_str()); |
| 102 | mke2fs_args.push_back(size_str.c_str()); |
| 103 | |
Wei Wang | 02e89ec | 2020-02-14 14:22:16 -0800 | [diff] [blame] | 104 | rc = logwrap_fork_execvp(mke2fs_args.size(), mke2fs_args.data(), nullptr, false, LOG_KLOG, |
| 105 | false, nullptr); |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 106 | if (rc) { |
| 107 | LERROR << "mke2fs returned " << rc; |
| 108 | return rc; |
| 109 | } |
| 110 | |
| 111 | const char* const e2fsdroid_args[] = { |
Tom Cherry | c3e7bd3 | 2018-12-06 10:28:26 -0800 | [diff] [blame] | 112 | "/system/bin/e2fsdroid", "-e", "-a", fs_mnt_point.c_str(), fs_blkdev.c_str(), nullptr}; |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 113 | |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 114 | rc = logwrap_fork_execvp(arraysize(e2fsdroid_args), e2fsdroid_args, nullptr, false, LOG_KLOG, |
Wei Wang | 02e89ec | 2020-02-14 14:22:16 -0800 | [diff] [blame] | 115 | false, nullptr); |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 116 | if (rc) { |
| 117 | LERROR << "e2fsdroid returned " << rc; |
William Roberts | 875476d | 2016-05-13 11:19:42 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 120 | return rc; |
| 121 | } |
| 122 | |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 123 | static int format_f2fs(const std::string& fs_blkdev, uint64_t dev_sz, bool crypt_footer, |
Jaegeuk Kim | 4076dc9 | 2020-01-13 11:02:28 -0800 | [diff] [blame] | 124 | bool needs_projid, bool needs_casefold, bool fs_compress) { |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 125 | if (!dev_sz) { |
| 126 | int rc = get_dev_sz(fs_blkdev, &dev_sz); |
| 127 | if (rc) { |
| 128 | return rc; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* Format the partition using the calculated length */ |
| 133 | if (crypt_footer) { |
| 134 | dev_sz -= CRYPT_FOOTER_OFFSET; |
| 135 | } |
| 136 | |
| 137 | std::string size_str = std::to_string(dev_sz / 4096); |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 138 | |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 139 | std::vector<const char*> args = {"/system/bin/make_f2fs", "-g", "android"}; |
| 140 | if (needs_projid) { |
| 141 | args.push_back("-O"); |
| 142 | args.push_back("project_quota,extra_attr"); |
| 143 | } |
| 144 | if (needs_casefold) { |
| 145 | args.push_back("-O"); |
| 146 | args.push_back("casefold"); |
| 147 | args.push_back("-C"); |
| 148 | args.push_back("utf8"); |
| 149 | } |
Jaegeuk Kim | 4076dc9 | 2020-01-13 11:02:28 -0800 | [diff] [blame] | 150 | if (fs_compress) { |
| 151 | args.push_back("-O"); |
| 152 | args.push_back("compression"); |
| 153 | args.push_back("-O"); |
| 154 | args.push_back("extra_attr"); |
| 155 | } |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 156 | args.push_back(fs_blkdev.c_str()); |
| 157 | args.push_back(size_str.c_str()); |
| 158 | |
Wei Wang | 02e89ec | 2020-02-14 14:22:16 -0800 | [diff] [blame] | 159 | return logwrap_fork_execvp(args.size(), args.data(), nullptr, false, LOG_KLOG, false, nullptr); |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 162 | int fs_mgr_do_format(const FstabEntry& entry, bool crypt_footer) { |
| 163 | LERROR << __FUNCTION__ << ": Format " << entry.blk_device << " as '" << entry.fs_type << "'"; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 164 | |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 165 | bool needs_casefold = false; |
| 166 | bool needs_projid = false; |
| 167 | |
| 168 | if (entry.mount_point == "/data") { |
Martijn Coenen | ea751d9 | 2020-04-15 11:45:05 +0200 | [diff] [blame^] | 169 | needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false); |
| 170 | needs_projid = android::base::GetBoolProperty("external_storage.projid.enabled", false); |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 173 | if (entry.fs_type == "f2fs") { |
Daniel Rosenberg | 29bc5d4 | 2020-01-31 18:27:41 -0800 | [diff] [blame] | 174 | return format_f2fs(entry.blk_device, entry.length, crypt_footer, needs_projid, |
Jaegeuk Kim | 4076dc9 | 2020-01-13 11:02:28 -0800 | [diff] [blame] | 175 | needs_casefold, entry.fs_mgr_flags.fs_compress); |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 176 | } else if (entry.fs_type == "ext4") { |
Jaegeuk Kim | 78f040c | 2020-02-11 15:22:46 -0800 | [diff] [blame] | 177 | return format_ext4(entry.blk_device, entry.mount_point, crypt_footer, needs_projid, |
| 178 | entry.fs_mgr_flags.ext_meta_csum); |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 179 | } else { |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 180 | LERROR << "File system type '" << entry.fs_type << "' is not supported"; |
| 181 | return -EINVAL; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 182 | } |
Tom Cherry | 23319eb | 2018-11-30 16:16:05 -0800 | [diff] [blame] | 183 | } |