blob: 677b473a0591363028fb6a5a173061aab742b8ce [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"
31#include "lz4diff/lz4diff.h"
Kelvin Zhang446989a2021-12-08 13:49:07 -080032#include "update_engine/common/utils.h"
33#include "update_engine/payload_generator/delta_diff_generator.h"
34#include "update_engine/payload_generator/extent_ranges.h"
35#include "update_engine/payload_generator/extent_utils.h"
Kelvin Zhang8389dfe2022-01-13 12:47:11 -080036#include "update_engine/payload_generator/filesystem_interface.h"
Kelvin Zhang446989a2021-12-08 13:49:07 -080037
38namespace chromeos_update_engine {
39
40namespace {
41
42static constexpr int GetOccupiedSize(const struct erofs_inode* inode,
43 erofs_off_t* size) {
44 *size = 0;
45 switch (inode->datalayout) {
46 case EROFS_INODE_FLAT_INLINE:
47 case EROFS_INODE_FLAT_PLAIN:
48 case EROFS_INODE_CHUNK_BASED:
49 *size = inode->i_size;
50 break;
51 case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
52 case EROFS_INODE_FLAT_COMPRESSION:
53 *size = inode->u.i_blocks * EROFS_BLKSIZ;
54 break;
55 default:
56 LOG(ERROR) << "unknown datalayout " << inode->datalayout;
57 return -1;
58 }
59 return 0;
60}
61
62static int ErofsMapBlocks(struct erofs_inode* inode,
63 struct erofs_map_blocks* map,
64 int flags) {
65 if (erofs_inode_is_data_compressed(inode->datalayout)) {
66 return z_erofs_map_blocks_iter(inode, map, flags);
67 }
68 return erofs_map_blocks(inode, map, flags);
69}
70
71static constexpr bool IsBlockCompressed(const struct erofs_map_blocks& block) {
72 // Z_EROFS_COMPRESSION_SHIFTED means data inside this block are merely
73 // memmove()'ed in place, instead of going through some compression function
74 // like LZ4 or LZMA
75 return block.m_flags & EROFS_MAP_ENCODED &&
76 block.m_algorithmformat != Z_EROFS_COMPRESSION_SHIFTED;
77}
78
79static void FillCompressedBlockInfo(FilesystemInterface::File* p_file,
80 std::string_view image_filename,
81 struct erofs_inode* inode) {
82 auto& file = *p_file;
83 if (!file.is_compressed) {
84 return;
85 }
Kelvin Zhang446989a2021-12-08 13:49:07 -080086
87 struct erofs_map_blocks block {};
88 block.m_la = 0;
89 block.index = UINT_MAX;
90
91 const erofs_off_t uncompressed_size = file.file_stat.st_size;
92 auto& compressed_blocks = file.compressed_file_info.blocks;
93 auto last_pa = block.m_pa;
94 auto last_plen = 0;
95 while (block.m_la < uncompressed_size) {
96 auto error = ErofsMapBlocks(inode, &block, EROFS_GET_BLOCKS_FIEMAP);
97 if (error) {
98 LOG(FATAL) << "Failed to map blocks for " << file.name << " in "
99 << image_filename;
100 }
101 // Certain uncompressed blocks have physical size > logical size. Usually
102 // the physical block contains bunch of trailing zeros. Include thees
103 // bytes in the logical size as well.
104 if (!IsBlockCompressed(block)) {
105 CHECK_LE(block.m_llen, block.m_plen);
106 block.m_llen = block.m_plen;
107 }
108
109 if (last_pa + last_plen != block.m_pa) {
110 if (last_plen != 0) {
111 file.extents.push_back(ExtentForRange(
112 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
113 }
114 last_pa = block.m_pa;
115 last_plen = block.m_plen;
116 } else {
117 last_plen += block.m_plen;
118 }
119 // If logical size and physical size are the same, this block is
120 // uncompressed. Join consecutive uncompressed blocks to save a bit memory
121 // storing metadata.
122 if (block.m_llen == block.m_plen && !compressed_blocks.empty() &&
123 !compressed_blocks.back().IsCompressed()) {
124 compressed_blocks.back().compressed_length += block.m_llen;
125 compressed_blocks.back().uncompressed_length += block.m_llen;
126 } else {
127 compressed_blocks.push_back(
128 CompressedBlock(block.m_la, block.m_plen, block.m_llen));
129 }
130
131 block.m_la += block.m_llen;
132 }
133 file.extents.push_back(ExtentForRange(
134 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
135 return;
136}
137
138} // namespace
139
140static_assert(kBlockSize == EROFS_BLKSIZ);
141
142std::unique_ptr<ErofsFilesystem> ErofsFilesystem::CreateFromFile(
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800143 const std::string& filename, const CompressionAlgorithm& algo) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800144 // erofs-utils makes heavy use of global variables. Hence its functions aren't
145 // thread safe. For example, it stores a global int holding file descriptors
146 // to the opened EROFS image. It doesn't even support opening more than 1
147 // imaeg at a time.
148 // TODO(b/202784930) Replace erofs-utils with a cleaner and more C++ friendly
149 // library. (Or turn erofs-utils into one)
150 static std::mutex m;
151 std::lock_guard g{m};
152
153 if (const auto err = dev_open_ro(filename.c_str()); err) {
154 PLOG(INFO) << "Failed to open " << filename;
155 return nullptr;
156 }
157 DEFER { dev_close(); };
158
159 if (const auto err = erofs_read_superblock(); err) {
160 PLOG(INFO) << "Failed to parse " << filename << " as EROFS image";
161 return nullptr;
162 }
163 struct stat st;
164 if (const auto err = fstat(erofs_devfd, &st); err) {
165 PLOG(ERROR) << "Failed to stat() " << filename;
166 return nullptr;
167 }
168 const time_t time = sbi.build_time;
169 LOG(INFO) << "Parsed EROFS image of size " << st.st_size << " built in "
170 << ctime(&time) << " " << filename;
171 std::vector<File> files;
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800172 if (!ErofsFilesystem::GetFiles(filename, &files, algo)) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800173 return nullptr;
174 }
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800175 LOG(INFO) << "Using compression algo " << algo << " for " << filename;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800176 // private ctor doesn't work with make_unique
177 return std::unique_ptr<ErofsFilesystem>(
178 new ErofsFilesystem(filename, st.st_size, std::move(files)));
179}
180
181bool ErofsFilesystem::GetFiles(std::vector<File>* files) const {
182 *files = files_;
183 return true;
184}
185
186bool ErofsFilesystem::GetFiles(const std::string& filename,
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800187 std::vector<File>* files,
188 const CompressionAlgorithm& algo) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800189 erofs_iterate_root_dir(&sbi, [&](struct erofs_iterate_dir_context* p_info) {
190 const auto& info = *p_info;
191 if (info.ctx.de_ftype != EROFS_FT_REG_FILE) {
192 return 0;
193 }
194 struct erofs_inode inode;
195 inode.nid = info.ctx.de_nid;
196 int err = erofs_read_inode_from_disk(&inode);
197 if (err) {
198 LOG(ERROR) << "Failed to read inode " << inode.nid;
199 return err;
200 }
201 const auto uncompressed_size = inode.i_size;
202 erofs_off_t compressed_size = 0;
203 if (uncompressed_size == 0) {
204 return 0;
205 }
206 err = GetOccupiedSize(&inode, &compressed_size);
207 if (err) {
208 LOG(FATAL) << "Failed to get occupied size for " << filename;
209 return err;
210 }
211 // If data is packed inline, likely this node is stored on block unalighed
212 // addresses. OTA doesn't work for non-block aligned files. All blocks not
213 // reported by |GetFiles| will be updated in 1 operation. Ignore inline
214 // files for now.
215 // TODO(b/206729162) Support un-aligned files.
216 if (inode.datalayout == EROFS_INODE_FLAT_INLINE) {
217 return 0;
218 }
219
220 File file;
221 file.name = info.path;
222 file.compressed_file_info.zero_padding_enabled =
223 erofs_sb_has_lz4_0padding();
224 file.is_compressed = compressed_size != uncompressed_size;
225
226 file.file_stat.st_size = uncompressed_size;
227 file.file_stat.st_ino = inode.nid;
228 FillCompressedBlockInfo(&file, filename, &inode);
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800229 file.compressed_file_info.algo = algo;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800230
231 files->emplace_back(std::move(file));
232 return 0;
233 });
234
235 for (auto& file : *files) {
236 NormalizeExtents(&file.extents);
237 }
238 return true;
239}
240
241} // namespace chromeos_update_engine