blob: c2c39799beaeb190b69be30364c7b933fc1a6285 [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
19#include <time.h>
20
21#include <string>
22#include <mutex>
23
24#include <erofs/internal.h>
25#include <erofs/dir.h>
26#include <erofs/io.h>
27
28#include "erofs_iterate.h"
29#include "lz4diff/lz4diff.pb.h"
Kelvin Zhang8389dfe2022-01-13 12:47:11 -080030#include "lz4diff/lz4patch.h"
Kelvin Zhang446989a2021-12-08 13:49:07 -080031#include "update_engine/common/utils.h"
32#include "update_engine/payload_generator/delta_diff_generator.h"
33#include "update_engine/payload_generator/extent_ranges.h"
34#include "update_engine/payload_generator/extent_utils.h"
Kelvin Zhang8389dfe2022-01-13 12:47:11 -080035#include "update_engine/payload_generator/filesystem_interface.h"
Kelvin Zhang446989a2021-12-08 13:49:07 -080036
37namespace chromeos_update_engine {
38
39namespace {
40
41static constexpr int GetOccupiedSize(const struct erofs_inode* inode,
42 erofs_off_t* size) {
43 *size = 0;
44 switch (inode->datalayout) {
45 case EROFS_INODE_FLAT_INLINE:
46 case EROFS_INODE_FLAT_PLAIN:
47 case EROFS_INODE_CHUNK_BASED:
48 *size = inode->i_size;
49 break;
50 case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
51 case EROFS_INODE_FLAT_COMPRESSION:
52 *size = inode->u.i_blocks * EROFS_BLKSIZ;
53 break;
54 default:
55 LOG(ERROR) << "unknown datalayout " << inode->datalayout;
56 return -1;
57 }
58 return 0;
59}
60
61static int ErofsMapBlocks(struct erofs_inode* inode,
62 struct erofs_map_blocks* map,
63 int flags) {
64 if (erofs_inode_is_data_compressed(inode->datalayout)) {
65 return z_erofs_map_blocks_iter(inode, map, flags);
66 }
67 return erofs_map_blocks(inode, map, flags);
68}
69
70static constexpr bool IsBlockCompressed(const struct erofs_map_blocks& block) {
71 // Z_EROFS_COMPRESSION_SHIFTED means data inside this block are merely
72 // memmove()'ed in place, instead of going through some compression function
73 // like LZ4 or LZMA
74 return block.m_flags & EROFS_MAP_ENCODED &&
75 block.m_algorithmformat != Z_EROFS_COMPRESSION_SHIFTED;
76}
77
Kelvin Zhang56c70692022-05-24 11:58:39 -070078static void FillExtentInfo(FilesystemInterface::File* p_file,
79 std::string_view image_filename,
Kelvin Zhang0503d7a2023-05-03 15:10:58 -070080 struct erofs_inode* inode,
81 size_t* const unaligned_bytes) {
Kelvin Zhang446989a2021-12-08 13:49:07 -080082 auto& file = *p_file;
Kelvin Zhang446989a2021-12-08 13:49:07 -080083
84 struct erofs_map_blocks block {};
85 block.m_la = 0;
86 block.index = UINT_MAX;
87
Kelvin Zhang446989a2021-12-08 13:49:07 -080088 auto& compressed_blocks = file.compressed_file_info.blocks;
89 auto last_pa = block.m_pa;
90 auto last_plen = 0;
Kelvin Zhang56c70692022-05-24 11:58:39 -070091 while (block.m_la < inode->i_size) {
Kelvin Zhang446989a2021-12-08 13:49:07 -080092 auto error = ErofsMapBlocks(inode, &block, EROFS_GET_BLOCKS_FIEMAP);
Kelvin Zhang0503d7a2023-05-03 15:10:58 -070093 DEFER {
94 block.m_la += block.m_llen;
95 };
Kelvin Zhang446989a2021-12-08 13:49:07 -080096 if (error) {
97 LOG(FATAL) << "Failed to map blocks for " << file.name << " in "
98 << image_filename;
99 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700100 if (block.m_pa % kBlockSize != 0) {
101 // EROFS might put the last block on unalighed addresses, because the last
102 // block is often < 1 full block size. That is fine, we can usually
103 // tolerate small amount of data being unaligned.
104 if (block.m_llen >= kBlockSize ||
105 block.m_la + block.m_llen != inode->i_size) {
106 LOG(ERROR) << "File `" << file.name
107 << "` has unaligned blocks: at physical byte offset: "
108 << block.m_pa << ", "
109 << " length: " << block.m_plen
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700110 << ", logical offset: " << block.m_la << ", remaining data: "
111 << inode->i_size - (block.m_la + block.m_llen);
Kelvin Zhang56c70692022-05-24 11:58:39 -0700112 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700113 (*unaligned_bytes) += block.m_plen;
Kelvin Zhang56c70692022-05-24 11:58:39 -0700114 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800115 // Certain uncompressed blocks have physical size > logical size. Usually
116 // the physical block contains bunch of trailing zeros. Include thees
117 // bytes in the logical size as well.
118 if (!IsBlockCompressed(block)) {
119 CHECK_LE(block.m_llen, block.m_plen);
120 block.m_llen = block.m_plen;
121 }
122
123 if (last_pa + last_plen != block.m_pa) {
124 if (last_plen != 0) {
125 file.extents.push_back(ExtentForRange(
126 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
127 }
128 last_pa = block.m_pa;
129 last_plen = block.m_plen;
130 } else {
131 last_plen += block.m_plen;
132 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700133 if (file.is_compressed) {
134 // If logical size and physical size are the same, this block is
135 // uncompressed. Join consecutive uncompressed blocks to save a bit memory
136 // storing metadata.
137 if (block.m_llen == block.m_plen && !compressed_blocks.empty() &&
138 !compressed_blocks.back().IsCompressed()) {
139 compressed_blocks.back().compressed_length += block.m_llen;
140 compressed_blocks.back().uncompressed_length += block.m_llen;
141 } else {
142 compressed_blocks.push_back(
143 CompressedBlock(block.m_la, block.m_plen, block.m_llen));
144 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800145 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800146 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700147 if (last_plen != 0) {
148 file.extents.push_back(ExtentForRange(
149 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
150 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800151 return;
152}
153
154} // namespace
155
156static_assert(kBlockSize == EROFS_BLKSIZ);
157
158std::unique_ptr<ErofsFilesystem> ErofsFilesystem::CreateFromFile(
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800159 const std::string& filename, const CompressionAlgorithm& algo) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800160 // erofs-utils makes heavy use of global variables. Hence its functions aren't
161 // thread safe. For example, it stores a global int holding file descriptors
162 // to the opened EROFS image. It doesn't even support opening more than 1
163 // imaeg at a time.
164 // TODO(b/202784930) Replace erofs-utils with a cleaner and more C++ friendly
165 // library. (Or turn erofs-utils into one)
166 static std::mutex m;
167 std::lock_guard g{m};
168
169 if (const auto err = dev_open_ro(filename.c_str()); err) {
170 PLOG(INFO) << "Failed to open " << filename;
171 return nullptr;
172 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700173 DEFER {
174 dev_close();
175 };
Kelvin Zhang446989a2021-12-08 13:49:07 -0800176
177 if (const auto err = erofs_read_superblock(); err) {
178 PLOG(INFO) << "Failed to parse " << filename << " as EROFS image";
179 return nullptr;
180 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700181 struct stat st {};
Kelvin Zhang446989a2021-12-08 13:49:07 -0800182 if (const auto err = fstat(erofs_devfd, &st); err) {
183 PLOG(ERROR) << "Failed to stat() " << filename;
184 return nullptr;
185 }
186 const time_t time = sbi.build_time;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800187 std::vector<File> files;
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800188 if (!ErofsFilesystem::GetFiles(filename, &files, algo)) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800189 return nullptr;
190 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700191
192 LOG(INFO) << "Parsed EROFS image of size " << st.st_size << " built in "
193 << ctime(&time) << " " << filename
194 << ", number of files: " << files.size();
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800195 LOG(INFO) << "Using compression algo " << algo << " for " << filename;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800196 // private ctor doesn't work with make_unique
197 return std::unique_ptr<ErofsFilesystem>(
198 new ErofsFilesystem(filename, st.st_size, std::move(files)));
199}
200
201bool ErofsFilesystem::GetFiles(std::vector<File>* files) const {
202 *files = files_;
203 return true;
204}
205
206bool ErofsFilesystem::GetFiles(const std::string& filename,
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800207 std::vector<File>* files,
208 const CompressionAlgorithm& algo) {
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700209 size_t unaligned_bytes = 0;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800210 erofs_iterate_root_dir(&sbi, [&](struct erofs_iterate_dir_context* p_info) {
211 const auto& info = *p_info;
212 if (info.ctx.de_ftype != EROFS_FT_REG_FILE) {
213 return 0;
214 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700215 struct erofs_inode inode {};
Kelvin Zhang446989a2021-12-08 13:49:07 -0800216 inode.nid = info.ctx.de_nid;
217 int err = erofs_read_inode_from_disk(&inode);
218 if (err) {
219 LOG(ERROR) << "Failed to read inode " << inode.nid;
220 return err;
221 }
222 const auto uncompressed_size = inode.i_size;
223 erofs_off_t compressed_size = 0;
224 if (uncompressed_size == 0) {
225 return 0;
226 }
227 err = GetOccupiedSize(&inode, &compressed_size);
228 if (err) {
229 LOG(FATAL) << "Failed to get occupied size for " << filename;
230 return err;
231 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700232 // For EROFS_INODE_FLAT_INLINE , most blocks are stored on aligned
233 // addresses. Except the last block, which is stored right after the
234 // inode. These nodes will have a slight amount of data unaligned, which
235 // is fine.
Kelvin Zhang446989a2021-12-08 13:49:07 -0800236
237 File file;
238 file.name = info.path;
239 file.compressed_file_info.zero_padding_enabled =
240 erofs_sb_has_lz4_0padding();
241 file.is_compressed = compressed_size != uncompressed_size;
242
243 file.file_stat.st_size = uncompressed_size;
244 file.file_stat.st_ino = inode.nid;
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700245 FillExtentInfo(&file, filename, &inode, &unaligned_bytes);
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800246 file.compressed_file_info.algo = algo;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800247
248 files->emplace_back(std::move(file));
249 return 0;
250 });
251
252 for (auto& file : *files) {
253 NormalizeExtents(&file.extents);
254 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700255 LOG(INFO) << "EROFS image " << filename << " has " << unaligned_bytes
256 << " unaligned bytes, which is "
257 << static_cast<float>(unaligned_bytes) / utils::FileSize(filename) *
258 100.0f
259 << "% of partition data";
Kelvin Zhang446989a2021-12-08 13:49:07 -0800260 return true;
261}
262
263} // namespace chromeos_update_engine