blob: 9ab37fdd1291e6039e8b07fc4d2bd6aa4d6ee503 [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"
30#include "payload_generator/filesystem_interface.h"
31#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"
35
36namespace chromeos_update_engine {
37
38namespace {
39
40static constexpr int GetOccupiedSize(const struct erofs_inode* inode,
41 erofs_off_t* size) {
42 *size = 0;
43 switch (inode->datalayout) {
44 case EROFS_INODE_FLAT_INLINE:
45 case EROFS_INODE_FLAT_PLAIN:
46 case EROFS_INODE_CHUNK_BASED:
47 *size = inode->i_size;
48 break;
49 case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
50 case EROFS_INODE_FLAT_COMPRESSION:
51 *size = inode->u.i_blocks * EROFS_BLKSIZ;
52 break;
53 default:
54 LOG(ERROR) << "unknown datalayout " << inode->datalayout;
55 return -1;
56 }
57 return 0;
58}
59
60static int ErofsMapBlocks(struct erofs_inode* inode,
61 struct erofs_map_blocks* map,
62 int flags) {
63 if (erofs_inode_is_data_compressed(inode->datalayout)) {
64 return z_erofs_map_blocks_iter(inode, map, flags);
65 }
66 return erofs_map_blocks(inode, map, flags);
67}
68
69static constexpr bool IsBlockCompressed(const struct erofs_map_blocks& block) {
70 // Z_EROFS_COMPRESSION_SHIFTED means data inside this block are merely
71 // memmove()'ed in place, instead of going through some compression function
72 // like LZ4 or LZMA
73 return block.m_flags & EROFS_MAP_ENCODED &&
74 block.m_algorithmformat != Z_EROFS_COMPRESSION_SHIFTED;
75}
76
77static void FillCompressedBlockInfo(FilesystemInterface::File* p_file,
78 std::string_view image_filename,
79 struct erofs_inode* inode) {
80 auto& file = *p_file;
81 if (!file.is_compressed) {
82 return;
83 }
84 // TODO(b/206729162) Fill in compression algorithm info from input target
85 // files
86 file.compressed_file_info.algo.set_type(CompressionAlgorithm::LZ4HC);
87 file.compressed_file_info.algo.set_level(9);
88
89 struct erofs_map_blocks block {};
90 block.m_la = 0;
91 block.index = UINT_MAX;
92
93 const erofs_off_t uncompressed_size = file.file_stat.st_size;
94 auto& compressed_blocks = file.compressed_file_info.blocks;
95 auto last_pa = block.m_pa;
96 auto last_plen = 0;
97 while (block.m_la < uncompressed_size) {
98 auto error = ErofsMapBlocks(inode, &block, EROFS_GET_BLOCKS_FIEMAP);
99 if (error) {
100 LOG(FATAL) << "Failed to map blocks for " << file.name << " in "
101 << image_filename;
102 }
103 // Certain uncompressed blocks have physical size > logical size. Usually
104 // the physical block contains bunch of trailing zeros. Include thees
105 // bytes in the logical size as well.
106 if (!IsBlockCompressed(block)) {
107 CHECK_LE(block.m_llen, block.m_plen);
108 block.m_llen = block.m_plen;
109 }
110
111 if (last_pa + last_plen != block.m_pa) {
112 if (last_plen != 0) {
113 file.extents.push_back(ExtentForRange(
114 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
115 }
116 last_pa = block.m_pa;
117 last_plen = block.m_plen;
118 } else {
119 last_plen += block.m_plen;
120 }
121 // If logical size and physical size are the same, this block is
122 // uncompressed. Join consecutive uncompressed blocks to save a bit memory
123 // storing metadata.
124 if (block.m_llen == block.m_plen && !compressed_blocks.empty() &&
125 !compressed_blocks.back().IsCompressed()) {
126 compressed_blocks.back().compressed_length += block.m_llen;
127 compressed_blocks.back().uncompressed_length += block.m_llen;
128 } else {
129 compressed_blocks.push_back(
130 CompressedBlock(block.m_la, block.m_plen, block.m_llen));
131 }
132
133 block.m_la += block.m_llen;
134 }
135 file.extents.push_back(ExtentForRange(
136 last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
137 return;
138}
139
140} // namespace
141
142static_assert(kBlockSize == EROFS_BLKSIZ);
143
144std::unique_ptr<ErofsFilesystem> ErofsFilesystem::CreateFromFile(
145 const std::string& filename) {
146 // erofs-utils makes heavy use of global variables. Hence its functions aren't
147 // thread safe. For example, it stores a global int holding file descriptors
148 // to the opened EROFS image. It doesn't even support opening more than 1
149 // imaeg at a time.
150 // TODO(b/202784930) Replace erofs-utils with a cleaner and more C++ friendly
151 // library. (Or turn erofs-utils into one)
152 static std::mutex m;
153 std::lock_guard g{m};
154
155 if (const auto err = dev_open_ro(filename.c_str()); err) {
156 PLOG(INFO) << "Failed to open " << filename;
157 return nullptr;
158 }
159 DEFER { dev_close(); };
160
161 if (const auto err = erofs_read_superblock(); err) {
162 PLOG(INFO) << "Failed to parse " << filename << " as EROFS image";
163 return nullptr;
164 }
165 struct stat st;
166 if (const auto err = fstat(erofs_devfd, &st); err) {
167 PLOG(ERROR) << "Failed to stat() " << filename;
168 return nullptr;
169 }
170 const time_t time = sbi.build_time;
171 LOG(INFO) << "Parsed EROFS image of size " << st.st_size << " built in "
172 << ctime(&time) << " " << filename;
173 std::vector<File> files;
174 if (!ErofsFilesystem::GetFiles(filename, &files)) {
175 return nullptr;
176 }
177 // private ctor doesn't work with make_unique
178 return std::unique_ptr<ErofsFilesystem>(
179 new ErofsFilesystem(filename, st.st_size, std::move(files)));
180}
181
182bool ErofsFilesystem::GetFiles(std::vector<File>* files) const {
183 *files = files_;
184 return true;
185}
186
187bool ErofsFilesystem::GetFiles(const std::string& filename,
188 std::vector<File>* files) {
189 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);
229
230 files->emplace_back(std::move(file));
231 return 0;
232 });
233
234 for (auto& file : *files) {
235 NormalizeExtents(&file.extents);
236 }
237 return true;
238}
239
240} // namespace chromeos_update_engine