blob: 508c9a13f088a6cfd899d7b4b7762c92f7fa2224 [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 Zhang446989a2021-12-08 13:49:07 -080031
32#include "erofs_iterate.h"
33#include "lz4diff/lz4diff.pb.h"
Kelvin Zhang8389dfe2022-01-13 12:47:11 -080034#include "lz4diff/lz4patch.h"
Kelvin Zhang446989a2021-12-08 13:49:07 -080035#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 Zhang8389dfe2022-01-13 12:47:11 -080039#include "update_engine/payload_generator/filesystem_interface.h"
Kelvin Zhang446989a2021-12-08 13:49:07 -080040
41namespace chromeos_update_engine {
42
43namespace {
44
45static 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
65static 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
74static 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 Zhang56c70692022-05-24 11:58:39 -070082static void FillExtentInfo(FilesystemInterface::File* p_file,
83 std::string_view image_filename,
Kelvin Zhang0503d7a2023-05-03 15:10:58 -070084 struct erofs_inode* inode,
85 size_t* const unaligned_bytes) {
Kelvin Zhang446989a2021-12-08 13:49:07 -080086 auto& file = *p_file;
Kelvin Zhang446989a2021-12-08 13:49:07 -080087
88 struct erofs_map_blocks block {};
89 block.m_la = 0;
90 block.index = UINT_MAX;
91
Kelvin Zhang446989a2021-12-08 13:49:07 -080092 auto& compressed_blocks = file.compressed_file_info.blocks;
93 auto last_pa = block.m_pa;
94 auto last_plen = 0;
Kelvin Zhang56c70692022-05-24 11:58:39 -070095 while (block.m_la < inode->i_size) {
Kelvin Zhang446989a2021-12-08 13:49:07 -080096 auto error = ErofsMapBlocks(inode, &block, EROFS_GET_BLOCKS_FIEMAP);
Kelvin Zhang0503d7a2023-05-03 15:10:58 -070097 DEFER {
98 block.m_la += block.m_llen;
99 };
Kelvin Zhang446989a2021-12-08 13:49:07 -0800100 if (error) {
101 LOG(FATAL) << "Failed to map blocks for " << file.name << " in "
102 << image_filename;
103 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700104 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 Zhang0503d7a2023-05-03 15:10:58 -0700114 << ", logical offset: " << block.m_la << ", remaining data: "
115 << inode->i_size - (block.m_la + block.m_llen);
Kelvin Zhang56c70692022-05-24 11:58:39 -0700116 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700117 (*unaligned_bytes) += block.m_plen;
Kelvin Zhang56c70692022-05-24 11:58:39 -0700118 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800119 // 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 Zhang56c70692022-05-24 11:58:39 -0700137 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 Zhang446989a2021-12-08 13:49:07 -0800149 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800150 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700151 if (last_plen != 0) {
152 file.extents.push_back(ExtentForRange(
153 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
154 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800155 return;
156}
157
Kelvin Zhang32b1d7b2023-05-16 09:40:11 -0700158bool 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 Zhang446989a2021-12-08 13:49:07 -0800167} // namespace
168
169static_assert(kBlockSize == EROFS_BLKSIZ);
170
171std::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 Zhang446989a2021-12-08 13:49:07 -0800176 // 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 Zhang56c70692022-05-24 11:58:39 -0700189 DEFER {
190 dev_close();
191 };
Kelvin Zhang446989a2021-12-08 13:49:07 -0800192
193 if (const auto err = erofs_read_superblock(); err) {
194 PLOG(INFO) << "Failed to parse " << filename << " as EROFS image";
195 return nullptr;
196 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700197 struct stat st {};
Kelvin Zhang446989a2021-12-08 13:49:07 -0800198 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 Zhang446989a2021-12-08 13:49:07 -0800203 std::vector<File> files;
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800204 if (!ErofsFilesystem::GetFiles(filename, &files, algo)) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800205 return nullptr;
206 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700207
208 LOG(INFO) << "Parsed EROFS image of size " << st.st_size << " built in "
209 << ctime(&time) << " " << filename
210 << ", number of files: " << files.size();
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800211 LOG(INFO) << "Using compression algo " << algo << " for " << filename;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800212 // 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
217bool ErofsFilesystem::GetFiles(std::vector<File>* files) const {
218 *files = files_;
219 return true;
220}
221
222bool ErofsFilesystem::GetFiles(const std::string& filename,
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800223 std::vector<File>* files,
224 const CompressionAlgorithm& algo) {
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700225 size_t unaligned_bytes = 0;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800226 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 Zhang56c70692022-05-24 11:58:39 -0700231 struct erofs_inode inode {};
Kelvin Zhang446989a2021-12-08 13:49:07 -0800232 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 Zhang0503d7a2023-05-03 15:10:58 -0700248 // 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 Zhang446989a2021-12-08 13:49:07 -0800252
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 Zhang0503d7a2023-05-03 15:10:58 -0700261 FillExtentInfo(&file, filename, &inode, &unaligned_bytes);
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800262 file.compressed_file_info.algo = algo;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800263
264 files->emplace_back(std::move(file));
265 return 0;
266 });
267
268 for (auto& file : *files) {
269 NormalizeExtents(&file.extents);
270 }
Kelvin Zhang0503d7a2023-05-03 15:10:58 -0700271 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 Zhang446989a2021-12-08 13:49:07 -0800276 return true;
277}
278
279} // namespace chromeos_update_engine