blob: 2835dea3b6679566a7d39cdffa8f9fe1b15063c2 [file] [log] [blame]
Kelvin Zhang446989a2021-12-08 13:49:07 -08001//
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 Zhang32b1d7b2023-05-16 09:40:11 -070019#include <endian.h>
20#include <fcntl.h>
Kelvin Zhang446989a2021-12-08 13:49:07 -080021#include <time.h>
22
Kelvin Zhang32b1d7b2023-05-16 09:40:11 -070023#include <array>
Kelvin Zhang446989a2021-12-08 13:49:07 -080024#include <string>
25#include <mutex>
26
Kelvin Zhang32b1d7b2023-05-16 09:40:11 -070027#include <android-base/unique_fd.h>
Kelvin Zhang446989a2021-12-08 13:49:07 -080028#include <erofs/dir.h>
29#include <erofs/io.h>
Kelvin Zhang32b1d7b2023-05-16 09:40:11 -070030#include <erofs_fs.h>
Kelvin Zhang18e97e02023-09-25 09:54:16 -070031#include <erofs/internal.h>
Kelvin Zhang446989a2021-12-08 13:49:07 -080032
33#include "erofs_iterate.h"
34#include "lz4diff/lz4diff.pb.h"
Kelvin Zhang8389dfe2022-01-13 12:47:11 -080035#include "lz4diff/lz4patch.h"
Kelvin Zhang446989a2021-12-08 13:49:07 -080036#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 Zhang8389dfe2022-01-13 12:47:11 -080040#include "update_engine/payload_generator/filesystem_interface.h"
Kelvin Zhang446989a2021-12-08 13:49:07 -080041
42namespace chromeos_update_engine {
43
44namespace {
45
46static constexpr int GetOccupiedSize(const struct erofs_inode* inode,
Kelvin Zhang18e97e02023-09-25 09:54:16 -070047 size_t block_size,
Kelvin Zhang446989a2021-12-08 13:49:07 -080048 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 Zhang18e97e02023-09-25 09:54:16 -070056 case EROFS_INODE_COMPRESSED_FULL:
57 case EROFS_INODE_COMPRESSED_COMPACT:
58 *size = inode->u.i_blocks * block_size;
Kelvin Zhang446989a2021-12-08 13:49:07 -080059 break;
60 default:
61 LOG(ERROR) << "unknown datalayout " << inode->datalayout;
62 return -1;
63 }
64 return 0;
65}
66
67static 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
76static 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 Zhang56c70692022-05-24 11:58:39 -070084static void FillExtentInfo(FilesystemInterface::File* p_file,
85 std::string_view image_filename,
Kelvin Zhang0503d7a2023-05-03 15:10:58 -070086 struct erofs_inode* inode,
87 size_t* const unaligned_bytes) {
Kelvin Zhang446989a2021-12-08 13:49:07 -080088 auto& file = *p_file;
Kelvin Zhang446989a2021-12-08 13:49:07 -080089
90 struct erofs_map_blocks block {};
91 block.m_la = 0;
92 block.index = UINT_MAX;
93
Kelvin Zhang446989a2021-12-08 13:49:07 -080094 auto& compressed_blocks = file.compressed_file_info.blocks;
95 auto last_pa = block.m_pa;
96 auto last_plen = 0;
Kelvin Zhang56c70692022-05-24 11:58:39 -070097 while (block.m_la < inode->i_size) {
Kelvin Zhang446989a2021-12-08 13:49:07 -080098 auto error = ErofsMapBlocks(inode, &block, EROFS_GET_BLOCKS_FIEMAP);
Kelvin Zhang0503d7a2023-05-03 15:10:58 -070099 DEFER {
100 block.m_la += block.m_llen;
101 };
Kelvin Zhang446989a2021-12-08 13:49:07 -0800102 if (error) {
103 LOG(FATAL) << "Failed to map blocks for " << file.name << " in "
104 << image_filename;
105 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700106 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 Zhang0503d7a2023-05-03 15:10:58 -0700116 << ", logical offset: " << block.m_la << ", remaining data: "
117 << inode->i_size - (block.m_la + block.m_llen);
Kelvin Zhang56c70692022-05-24 11:58:39 -0700118 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700119 (*unaligned_bytes) += block.m_plen;
Kelvin Zhang56c70692022-05-24 11:58:39 -0700120 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800121 // 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 Zhang56c70692022-05-24 11:58:39 -0700139 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 Zhang446989a2021-12-08 13:49:07 -0800151 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800152 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700153 if (last_plen != 0) {
154 file.extents.push_back(ExtentForRange(
155 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
156 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800157 return;
158}
159
Kelvin Zhang32b1d7b2023-05-16 09:40:11 -0700160bool 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 Zhang446989a2021-12-08 13:49:07 -0800169} // namespace
170
Kelvin Zhang446989a2021-12-08 13:49:07 -0800171std::unique_ptr<ErofsFilesystem> ErofsFilesystem::CreateFromFile(
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800172 const std::string& filename, const CompressionAlgorithm& algo) {
Kelvin Zhang32b1d7b2023-05-16 09:40:11 -0700173 if (!IsErofsImage(filename.c_str())) {
174 return {};
175 }
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700176 struct erofs_sb_info sbi {};
Kelvin Zhang446989a2021-12-08 13:49:07 -0800177
Kelvin Zhang8dd1ef12024-08-09 13:02:24 -0700178 if (const auto err = erofs_dev_open(&sbi, filename.c_str(), O_RDONLY); err) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800179 PLOG(INFO) << "Failed to open " << filename;
180 return nullptr;
181 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700182 DEFER {
Kelvin Zhang8dd1ef12024-08-09 13:02:24 -0700183 erofs_dev_close(&sbi);
Kelvin Zhang56c70692022-05-24 11:58:39 -0700184 };
Kelvin Zhang446989a2021-12-08 13:49:07 -0800185
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700186 if (const auto err = erofs_read_superblock(&sbi); err) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800187 PLOG(INFO) << "Failed to parse " << filename << " as EROFS image";
188 return nullptr;
189 }
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700190 const auto block_size = 1UL << sbi.blkszbits;
Kelvin Zhang56c70692022-05-24 11:58:39 -0700191 struct stat st {};
Kelvin Zhang8dd1ef12024-08-09 13:02:24 -0700192 if (const auto err = stat(filename.c_str(), &st); err) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800193 PLOG(ERROR) << "Failed to stat() " << filename;
194 return nullptr;
195 }
196 const time_t time = sbi.build_time;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800197 std::vector<File> files;
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700198 CHECK(ErofsFilesystem::GetFiles(&sbi, filename, &files, algo))
199 << "Failed to parse EROFS image " << filename;
Kelvin Zhang56c70692022-05-24 11:58:39 -0700200
201 LOG(INFO) << "Parsed EROFS image of size " << st.st_size << " built in "
202 << ctime(&time) << " " << filename
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700203 << ", number of files: " << files.size()
204 << ", block size: " << block_size;
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800205 LOG(INFO) << "Using compression algo " << algo << " for " << filename;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800206 // 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
211bool ErofsFilesystem::GetFiles(std::vector<File>* files) const {
212 *files = files_;
213 return true;
214}
215
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700216bool ErofsFilesystem::GetFiles(struct erofs_sb_info* sbi,
217 const std::string& filename,
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800218 std::vector<File>* files,
219 const CompressionAlgorithm& algo) {
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700220 size_t unaligned_bytes = 0;
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700221 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 Zhang446989a2021-12-08 13:49:07 -0800250
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700251 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 Zhang446989a2021-12-08 13:49:07 -0800256
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700257 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 Zhang446989a2021-12-08 13:49:07 -0800261
Kelvin Zhang18e97e02023-09-25 09:54:16 -0700262 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 Zhang446989a2021-12-08 13:49:07 -0800269
270 for (auto& file : *files) {
271 NormalizeExtents(&file.extents);
272 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700273 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 Zhang446989a2021-12-08 13:49:07 -0800278 return true;
279}
280
281} // namespace chromeos_update_engine