Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2021 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 "update_engine/payload_generator/erofs_filesystem.h" |
| 18 | |
Kelvin Zhang | 32b1d7b | 2023-05-16 09:40:11 -0700 | [diff] [blame] | 19 | #include <endian.h> |
| 20 | #include <fcntl.h> |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 21 | #include <time.h> |
| 22 | |
Kelvin Zhang | 32b1d7b | 2023-05-16 09:40:11 -0700 | [diff] [blame] | 23 | #include <array> |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <mutex> |
| 26 | |
Kelvin Zhang | 32b1d7b | 2023-05-16 09:40:11 -0700 | [diff] [blame] | 27 | #include <android-base/unique_fd.h> |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 28 | #include <erofs/dir.h> |
| 29 | #include <erofs/io.h> |
Kelvin Zhang | 32b1d7b | 2023-05-16 09:40:11 -0700 | [diff] [blame] | 30 | #include <erofs_fs.h> |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 31 | #include <erofs/internal.h> |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 32 | |
| 33 | #include "erofs_iterate.h" |
| 34 | #include "lz4diff/lz4diff.pb.h" |
Kelvin Zhang | 8389dfe | 2022-01-13 12:47:11 -0800 | [diff] [blame] | 35 | #include "lz4diff/lz4patch.h" |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 36 | #include "update_engine/common/utils.h" |
| 37 | #include "update_engine/payload_generator/delta_diff_generator.h" |
| 38 | #include "update_engine/payload_generator/extent_ranges.h" |
| 39 | #include "update_engine/payload_generator/extent_utils.h" |
Kelvin Zhang | 8389dfe | 2022-01-13 12:47:11 -0800 | [diff] [blame] | 40 | #include "update_engine/payload_generator/filesystem_interface.h" |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 41 | |
| 42 | namespace chromeos_update_engine { |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | static constexpr int GetOccupiedSize(const struct erofs_inode* inode, |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 47 | size_t block_size, |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 48 | erofs_off_t* size) { |
| 49 | *size = 0; |
| 50 | switch (inode->datalayout) { |
| 51 | case EROFS_INODE_FLAT_INLINE: |
| 52 | case EROFS_INODE_FLAT_PLAIN: |
| 53 | case EROFS_INODE_CHUNK_BASED: |
| 54 | *size = inode->i_size; |
| 55 | break; |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 56 | case EROFS_INODE_COMPRESSED_FULL: |
| 57 | case EROFS_INODE_COMPRESSED_COMPACT: |
| 58 | *size = inode->u.i_blocks * block_size; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 59 | break; |
| 60 | default: |
| 61 | LOG(ERROR) << "unknown datalayout " << inode->datalayout; |
| 62 | return -1; |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int ErofsMapBlocks(struct erofs_inode* inode, |
| 68 | struct erofs_map_blocks* map, |
| 69 | int flags) { |
| 70 | if (erofs_inode_is_data_compressed(inode->datalayout)) { |
| 71 | return z_erofs_map_blocks_iter(inode, map, flags); |
| 72 | } |
| 73 | return erofs_map_blocks(inode, map, flags); |
| 74 | } |
| 75 | |
| 76 | static constexpr bool IsBlockCompressed(const struct erofs_map_blocks& block) { |
| 77 | // Z_EROFS_COMPRESSION_SHIFTED means data inside this block are merely |
| 78 | // memmove()'ed in place, instead of going through some compression function |
| 79 | // like LZ4 or LZMA |
| 80 | return block.m_flags & EROFS_MAP_ENCODED && |
| 81 | block.m_algorithmformat != Z_EROFS_COMPRESSION_SHIFTED; |
| 82 | } |
| 83 | |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 84 | static void FillExtentInfo(FilesystemInterface::File* p_file, |
| 85 | std::string_view image_filename, |
Kelvin Zhang | 0503d7a | 2023-05-03 15:10:58 -0700 | [diff] [blame] | 86 | struct erofs_inode* inode, |
| 87 | size_t* const unaligned_bytes) { |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 88 | auto& file = *p_file; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 89 | |
| 90 | struct erofs_map_blocks block {}; |
| 91 | block.m_la = 0; |
| 92 | block.index = UINT_MAX; |
| 93 | |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 94 | auto& compressed_blocks = file.compressed_file_info.blocks; |
| 95 | auto last_pa = block.m_pa; |
| 96 | auto last_plen = 0; |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 97 | while (block.m_la < inode->i_size) { |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 98 | auto error = ErofsMapBlocks(inode, &block, EROFS_GET_BLOCKS_FIEMAP); |
Kelvin Zhang | 0503d7a | 2023-05-03 15:10:58 -0700 | [diff] [blame] | 99 | DEFER { |
| 100 | block.m_la += block.m_llen; |
| 101 | }; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 102 | if (error) { |
| 103 | LOG(FATAL) << "Failed to map blocks for " << file.name << " in " |
| 104 | << image_filename; |
| 105 | } |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 106 | if (block.m_pa % kBlockSize != 0) { |
| 107 | // EROFS might put the last block on unalighed addresses, because the last |
| 108 | // block is often < 1 full block size. That is fine, we can usually |
| 109 | // tolerate small amount of data being unaligned. |
| 110 | if (block.m_llen >= kBlockSize || |
| 111 | block.m_la + block.m_llen != inode->i_size) { |
| 112 | LOG(ERROR) << "File `" << file.name |
| 113 | << "` has unaligned blocks: at physical byte offset: " |
| 114 | << block.m_pa << ", " |
| 115 | << " length: " << block.m_plen |
Kelvin Zhang | 0503d7a | 2023-05-03 15:10:58 -0700 | [diff] [blame] | 116 | << ", logical offset: " << block.m_la << ", remaining data: " |
| 117 | << inode->i_size - (block.m_la + block.m_llen); |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 118 | } |
Kelvin Zhang | 0503d7a | 2023-05-03 15:10:58 -0700 | [diff] [blame] | 119 | (*unaligned_bytes) += block.m_plen; |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 120 | } |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 121 | // Certain uncompressed blocks have physical size > logical size. Usually |
| 122 | // the physical block contains bunch of trailing zeros. Include thees |
| 123 | // bytes in the logical size as well. |
| 124 | if (!IsBlockCompressed(block)) { |
| 125 | CHECK_LE(block.m_llen, block.m_plen); |
| 126 | block.m_llen = block.m_plen; |
| 127 | } |
| 128 | |
| 129 | if (last_pa + last_plen != block.m_pa) { |
| 130 | if (last_plen != 0) { |
| 131 | file.extents.push_back(ExtentForRange( |
| 132 | last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize))); |
| 133 | } |
| 134 | last_pa = block.m_pa; |
| 135 | last_plen = block.m_plen; |
| 136 | } else { |
| 137 | last_plen += block.m_plen; |
| 138 | } |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 139 | if (file.is_compressed) { |
| 140 | // If logical size and physical size are the same, this block is |
| 141 | // uncompressed. Join consecutive uncompressed blocks to save a bit memory |
| 142 | // storing metadata. |
| 143 | if (block.m_llen == block.m_plen && !compressed_blocks.empty() && |
| 144 | !compressed_blocks.back().IsCompressed()) { |
| 145 | compressed_blocks.back().compressed_length += block.m_llen; |
| 146 | compressed_blocks.back().uncompressed_length += block.m_llen; |
| 147 | } else { |
| 148 | compressed_blocks.push_back( |
| 149 | CompressedBlock(block.m_la, block.m_plen, block.m_llen)); |
| 150 | } |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 151 | } |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 152 | } |
Kelvin Zhang | 0503d7a | 2023-05-03 15:10:58 -0700 | [diff] [blame] | 153 | if (last_plen != 0) { |
| 154 | file.extents.push_back(ExtentForRange( |
| 155 | last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize))); |
| 156 | } |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Kelvin Zhang | 32b1d7b | 2023-05-16 09:40:11 -0700 | [diff] [blame] | 160 | bool IsErofsImage(const char* path) { |
| 161 | android::base::unique_fd fd(open(path, O_RDONLY)); |
| 162 | uint32_t buf{}; |
| 163 | if (pread(fd.get(), &buf, 4, EROFS_SUPER_OFFSET) < 0) { |
| 164 | return false; |
| 165 | } |
| 166 | return le32toh(buf) == EROFS_SUPER_MAGIC_V1; |
| 167 | } |
| 168 | |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 169 | } // namespace |
| 170 | |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 171 | std::unique_ptr<ErofsFilesystem> ErofsFilesystem::CreateFromFile( |
Kelvin Zhang | 8389dfe | 2022-01-13 12:47:11 -0800 | [diff] [blame] | 172 | const std::string& filename, const CompressionAlgorithm& algo) { |
Kelvin Zhang | 32b1d7b | 2023-05-16 09:40:11 -0700 | [diff] [blame] | 173 | if (!IsErofsImage(filename.c_str())) { |
| 174 | return {}; |
| 175 | } |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 176 | struct erofs_sb_info sbi {}; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 177 | |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 178 | if (const auto err = dev_open_ro(&sbi, filename.c_str()); err) { |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 179 | PLOG(INFO) << "Failed to open " << filename; |
| 180 | return nullptr; |
| 181 | } |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 182 | DEFER { |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 183 | dev_close(&sbi); |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 184 | }; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 185 | |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 186 | if (const auto err = erofs_read_superblock(&sbi); err) { |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 187 | PLOG(INFO) << "Failed to parse " << filename << " as EROFS image"; |
| 188 | return nullptr; |
| 189 | } |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 190 | const auto block_size = 1UL << sbi.blkszbits; |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 191 | struct stat st {}; |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 192 | if (const auto err = fstat(sbi.devfd, &st); err) { |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 193 | PLOG(ERROR) << "Failed to stat() " << filename; |
| 194 | return nullptr; |
| 195 | } |
| 196 | const time_t time = sbi.build_time; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 197 | std::vector<File> files; |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 198 | CHECK(ErofsFilesystem::GetFiles(&sbi, filename, &files, algo)) |
| 199 | << "Failed to parse EROFS image " << filename; |
Kelvin Zhang | 56c7069 | 2022-05-24 11:58:39 -0700 | [diff] [blame] | 200 | |
| 201 | LOG(INFO) << "Parsed EROFS image of size " << st.st_size << " built in " |
| 202 | << ctime(&time) << " " << filename |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 203 | << ", number of files: " << files.size() |
| 204 | << ", block size: " << block_size; |
Kelvin Zhang | 8389dfe | 2022-01-13 12:47:11 -0800 | [diff] [blame] | 205 | LOG(INFO) << "Using compression algo " << algo << " for " << filename; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 206 | // private ctor doesn't work with make_unique |
| 207 | return std::unique_ptr<ErofsFilesystem>( |
| 208 | new ErofsFilesystem(filename, st.st_size, std::move(files))); |
| 209 | } |
| 210 | |
| 211 | bool ErofsFilesystem::GetFiles(std::vector<File>* files) const { |
| 212 | *files = files_; |
| 213 | return true; |
| 214 | } |
| 215 | |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 216 | bool ErofsFilesystem::GetFiles(struct erofs_sb_info* sbi, |
| 217 | const std::string& filename, |
Kelvin Zhang | 8389dfe | 2022-01-13 12:47:11 -0800 | [diff] [blame] | 218 | std::vector<File>* files, |
| 219 | const CompressionAlgorithm& algo) { |
Kelvin Zhang | 0503d7a | 2023-05-03 15:10:58 -0700 | [diff] [blame] | 220 | size_t unaligned_bytes = 0; |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 221 | const auto block_size = 1UL << sbi->blkszbits; |
| 222 | const auto err = erofs_iterate_root_dir( |
| 223 | sbi, [&](struct erofs_iterate_dir_context* p_info) { |
| 224 | const auto& info = *p_info; |
| 225 | if (info.ctx.de_ftype != EROFS_FT_REG_FILE) { |
| 226 | return 0; |
| 227 | } |
| 228 | struct erofs_inode inode {}; |
| 229 | inode.nid = info.ctx.de_nid; |
| 230 | inode.sbi = sbi; |
| 231 | int err = erofs_read_inode_from_disk(&inode); |
| 232 | if (err) { |
| 233 | LOG(ERROR) << "Failed to read inode " << inode.nid; |
| 234 | return err; |
| 235 | } |
| 236 | const auto uncompressed_size = inode.i_size; |
| 237 | erofs_off_t compressed_size = 0; |
| 238 | if (uncompressed_size == 0) { |
| 239 | return 0; |
| 240 | } |
| 241 | err = GetOccupiedSize(&inode, block_size, &compressed_size); |
| 242 | if (err) { |
| 243 | LOG(FATAL) << "Failed to get occupied size for " << filename; |
| 244 | return err; |
| 245 | } |
| 246 | // For EROFS_INODE_FLAT_INLINE , most blocks are stored on aligned |
| 247 | // addresses. Except the last block, which is stored right after the |
| 248 | // inode. These nodes will have a slight amount of data unaligned, which |
| 249 | // is fine. |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 250 | |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 251 | File file; |
| 252 | file.name = info.path; |
| 253 | file.compressed_file_info.zero_padding_enabled = |
| 254 | erofs_sb_has_lz4_0padding(sbi); |
| 255 | file.is_compressed = compressed_size != uncompressed_size; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 256 | |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 257 | file.file_stat.st_size = uncompressed_size; |
| 258 | file.file_stat.st_ino = inode.nid; |
| 259 | FillExtentInfo(&file, filename, &inode, &unaligned_bytes); |
| 260 | file.compressed_file_info.algo = algo; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 261 | |
Kelvin Zhang | 18e97e0 | 2023-09-25 09:54:16 -0700 | [diff] [blame^] | 262 | files->emplace_back(std::move(file)); |
| 263 | return 0; |
| 264 | }); |
| 265 | if (err) { |
| 266 | LOG(ERROR) << "EROFS files iteration filed " << strerror(-err); |
| 267 | return false; |
| 268 | } |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 269 | |
| 270 | for (auto& file : *files) { |
| 271 | NormalizeExtents(&file.extents); |
| 272 | } |
Kelvin Zhang | 0503d7a | 2023-05-03 15:10:58 -0700 | [diff] [blame] | 273 | LOG(INFO) << "EROFS image " << filename << " has " << unaligned_bytes |
| 274 | << " unaligned bytes, which is " |
| 275 | << static_cast<float>(unaligned_bytes) / utils::FileSize(filename) * |
| 276 | 100.0f |
| 277 | << "% of partition data"; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame] | 278 | return true; |
| 279 | } |
| 280 | |
| 281 | } // namespace chromeos_update_engine |