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