blob: bf10d8c9268df5d16edc61e6ad14a75b20713786 [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
Kelvin Zhang56c70692022-05-24 11:58:39 -070079static void FillExtentInfo(FilesystemInterface::File* p_file,
80 std::string_view image_filename,
81 struct erofs_inode* inode) {
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 LOG(INFO) << file.name << ", isize: " << inode->i_size;
92 while (block.m_la < inode->i_size) {
Kelvin Zhang446989a2021-12-08 13:49:07 -080093 auto error = ErofsMapBlocks(inode, &block, EROFS_GET_BLOCKS_FIEMAP);
94 if (error) {
95 LOG(FATAL) << "Failed to map blocks for " << file.name << " in "
96 << image_filename;
97 }
Kelvin Zhang56c70692022-05-24 11:58:39 -070098 if (block.m_pa % kBlockSize != 0) {
99 // EROFS might put the last block on unalighed addresses, because the last
100 // block is often < 1 full block size. That is fine, we can usually
101 // tolerate small amount of data being unaligned.
102 if (block.m_llen >= kBlockSize ||
103 block.m_la + block.m_llen != inode->i_size) {
104 LOG(ERROR) << "File `" << file.name
105 << "` has unaligned blocks: at physical byte offset: "
106 << block.m_pa << ", "
107 << " length: " << block.m_plen
108 << ", logical offset: " << block.m_la;
109 }
110 break;
111 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800112 // Certain uncompressed blocks have physical size > logical size. Usually
113 // the physical block contains bunch of trailing zeros. Include thees
114 // bytes in the logical size as well.
115 if (!IsBlockCompressed(block)) {
116 CHECK_LE(block.m_llen, block.m_plen);
117 block.m_llen = block.m_plen;
118 }
119
120 if (last_pa + last_plen != block.m_pa) {
121 if (last_plen != 0) {
122 file.extents.push_back(ExtentForRange(
123 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
124 }
125 last_pa = block.m_pa;
126 last_plen = block.m_plen;
127 } else {
128 last_plen += block.m_plen;
129 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700130 if (file.is_compressed) {
131 // If logical size and physical size are the same, this block is
132 // uncompressed. Join consecutive uncompressed blocks to save a bit memory
133 // storing metadata.
134 if (block.m_llen == block.m_plen && !compressed_blocks.empty() &&
135 !compressed_blocks.back().IsCompressed()) {
136 compressed_blocks.back().compressed_length += block.m_llen;
137 compressed_blocks.back().uncompressed_length += block.m_llen;
138 } else {
139 compressed_blocks.push_back(
140 CompressedBlock(block.m_la, block.m_plen, block.m_llen));
141 }
Kelvin Zhang446989a2021-12-08 13:49:07 -0800142 }
143
144 block.m_la += block.m_llen;
145 }
146 file.extents.push_back(ExtentForRange(
147 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
148 return;
149}
150
151} // namespace
152
153static_assert(kBlockSize == EROFS_BLKSIZ);
154
155std::unique_ptr<ErofsFilesystem> ErofsFilesystem::CreateFromFile(
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800156 const std::string& filename, const CompressionAlgorithm& algo) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800157 // erofs-utils makes heavy use of global variables. Hence its functions aren't
158 // thread safe. For example, it stores a global int holding file descriptors
159 // to the opened EROFS image. It doesn't even support opening more than 1
160 // imaeg at a time.
161 // TODO(b/202784930) Replace erofs-utils with a cleaner and more C++ friendly
162 // library. (Or turn erofs-utils into one)
163 static std::mutex m;
164 std::lock_guard g{m};
165
166 if (const auto err = dev_open_ro(filename.c_str()); err) {
167 PLOG(INFO) << "Failed to open " << filename;
168 return nullptr;
169 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700170 DEFER {
171 dev_close();
172 };
Kelvin Zhang446989a2021-12-08 13:49:07 -0800173
174 if (const auto err = erofs_read_superblock(); err) {
175 PLOG(INFO) << "Failed to parse " << filename << " as EROFS image";
176 return nullptr;
177 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700178 struct stat st {};
Kelvin Zhang446989a2021-12-08 13:49:07 -0800179 if (const auto err = fstat(erofs_devfd, &st); err) {
180 PLOG(ERROR) << "Failed to stat() " << filename;
181 return nullptr;
182 }
183 const time_t time = sbi.build_time;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800184 std::vector<File> files;
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800185 if (!ErofsFilesystem::GetFiles(filename, &files, algo)) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800186 return nullptr;
187 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700188
189 LOG(INFO) << "Parsed EROFS image of size " << st.st_size << " built in "
190 << ctime(&time) << " " << filename
191 << ", number of files: " << files.size();
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800192 LOG(INFO) << "Using compression algo " << algo << " for " << filename;
Kelvin Zhang446989a2021-12-08 13:49:07 -0800193 // private ctor doesn't work with make_unique
194 return std::unique_ptr<ErofsFilesystem>(
195 new ErofsFilesystem(filename, st.st_size, std::move(files)));
196}
197
198bool ErofsFilesystem::GetFiles(std::vector<File>* files) const {
199 *files = files_;
200 return true;
201}
202
203bool ErofsFilesystem::GetFiles(const std::string& filename,
Kelvin Zhang8389dfe2022-01-13 12:47:11 -0800204 std::vector<File>* files,
205 const CompressionAlgorithm& algo) {
Kelvin Zhang446989a2021-12-08 13:49:07 -0800206 erofs_iterate_root_dir(&sbi, [&](struct erofs_iterate_dir_context* p_info) {
207 const auto& info = *p_info;
208 if (info.ctx.de_ftype != EROFS_FT_REG_FILE) {
209 return 0;
210 }
Kelvin Zhang56c70692022-05-24 11:58:39 -0700211 struct erofs_inode inode {};
Kelvin Zhang446989a2021-12-08 13:49:07 -0800212 inode.nid = info.ctx.de_nid;
213 int err = erofs_read_inode_from_disk(&inode);
214 if (err) {
215 LOG(ERROR) << "Failed to read inode " << inode.nid;
216 return err;
217 }
218 const auto uncompressed_size = inode.i_size;
219 erofs_off_t compressed_size = 0;
220 if (uncompressed_size == 0) {
221 return 0;
222 }
223 err = GetOccupiedSize(&inode, &compressed_size);
224 if (err) {
225 LOG(FATAL) << "Failed to get occupied size for " << filename;
226 return err;
227 }
228 // If data is packed inline, likely this node is stored on block unalighed
229 // addresses. OTA doesn't work for non-block aligned files. All blocks not
230 // reported by |GetFiles| will be updated in 1 operation. Ignore inline
231 // files for now.
232 // TODO(b/206729162) Support un-aligned files.
233 if (inode.datalayout == EROFS_INODE_FLAT_INLINE) {
234 return 0;
235 }
236
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 Zhang56c70692022-05-24 11:58:39 -0700245 FillExtentInfo(&file, filename, &inode);
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 }
255 return true;
256}
257
258} // namespace chromeos_update_engine