xref: /aosp_15_r20/system/update_engine/payload_generator/erofs_filesystem.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1 //
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 <endian.h>
20 #include <fcntl.h>
21 #include <time.h>
22 
23 #include <array>
24 #include <string>
25 #include <mutex>
26 
27 #include <android-base/unique_fd.h>
28 #include <erofs/dir.h>
29 #include <erofs/io.h>
30 #include <erofs_fs.h>
31 #include <erofs/internal.h>
32 
33 #include "erofs_iterate.h"
34 #include "lz4diff/lz4diff.pb.h"
35 #include "lz4diff/lz4patch.h"
36 #include "update_engine/common/utils.h"
37 #include "update_engine/payload_generator/delta_diff_generator.h"
38 #include "update_engine/payload_generator/extent_ranges.h"
39 #include "update_engine/payload_generator/extent_utils.h"
40 #include "update_engine/payload_generator/filesystem_interface.h"
41 
42 namespace chromeos_update_engine {
43 
44 namespace {
45 
GetOccupiedSize(const struct erofs_inode * inode,size_t block_size,erofs_off_t * size)46 static constexpr int GetOccupiedSize(const struct erofs_inode* inode,
47                                      size_t block_size,
48                                      erofs_off_t* size) {
49   *size = 0;
50   switch (inode->datalayout) {
51     case EROFS_INODE_FLAT_INLINE:
52     case EROFS_INODE_FLAT_PLAIN:
53     case EROFS_INODE_CHUNK_BASED:
54       *size = inode->i_size;
55       break;
56     case EROFS_INODE_COMPRESSED_FULL:
57     case EROFS_INODE_COMPRESSED_COMPACT:
58       *size = inode->u.i_blocks * block_size;
59       break;
60     default:
61       LOG(ERROR) << "unknown datalayout " << inode->datalayout;
62       return -1;
63   }
64   return 0;
65 }
66 
ErofsMapBlocks(struct erofs_inode * inode,struct erofs_map_blocks * map,int flags)67 static int ErofsMapBlocks(struct erofs_inode* inode,
68                           struct erofs_map_blocks* map,
69                           int flags) {
70   if (erofs_inode_is_data_compressed(inode->datalayout)) {
71     return z_erofs_map_blocks_iter(inode, map, flags);
72   }
73   return erofs_map_blocks(inode, map, flags);
74 }
75 
IsBlockCompressed(const struct erofs_map_blocks & block)76 static constexpr bool IsBlockCompressed(const struct erofs_map_blocks& block) {
77   // Z_EROFS_COMPRESSION_SHIFTED means data inside this block are merely
78   // memmove()'ed in place, instead of going through some compression function
79   // like LZ4 or LZMA
80   return block.m_flags & EROFS_MAP_ENCODED &&
81          block.m_algorithmformat != Z_EROFS_COMPRESSION_SHIFTED;
82 }
83 
FillExtentInfo(FilesystemInterface::File * p_file,std::string_view image_filename,struct erofs_inode * inode,size_t * const unaligned_bytes)84 static void FillExtentInfo(FilesystemInterface::File* p_file,
85                            std::string_view image_filename,
86                            struct erofs_inode* inode,
87                            size_t* const unaligned_bytes) {
88   auto& file = *p_file;
89 
90   struct erofs_map_blocks block {};
91   block.m_la = 0;
92   block.index = UINT_MAX;
93 
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 < inode->i_size) {
98     auto error = ErofsMapBlocks(inode, &block, EROFS_GET_BLOCKS_FIEMAP);
99     DEFER {
100       block.m_la += block.m_llen;
101     };
102     if (error) {
103       LOG(FATAL) << "Failed to map blocks for " << file.name << " in "
104                  << image_filename;
105     }
106     if (block.m_pa % kBlockSize != 0) {
107       // EROFS might put the last block on unalighed addresses, because the last
108       // block is often < 1 full block size. That is fine, we can usually
109       // tolerate small amount of data being unaligned.
110       if (block.m_llen >= kBlockSize ||
111           block.m_la + block.m_llen != inode->i_size) {
112         LOG(ERROR) << "File `" << file.name
113                    << "` has unaligned blocks: at physical byte offset: "
114                    << block.m_pa << ", "
115                    << " length: " << block.m_plen
116                    << ", logical offset: " << block.m_la << ", remaining data: "
117                    << inode->i_size - (block.m_la + block.m_llen);
118       }
119       (*unaligned_bytes) += block.m_plen;
120     }
121     // Certain uncompressed blocks have physical size > logical size. Usually
122     // the physical block contains bunch of trailing zeros. Include thees
123     // bytes in the logical size as well.
124     if (!IsBlockCompressed(block)) {
125       CHECK_LE(block.m_llen, block.m_plen);
126       block.m_llen = block.m_plen;
127     }
128 
129     if (last_pa + last_plen != block.m_pa) {
130       if (last_plen != 0) {
131         file.extents.push_back(ExtentForRange(
132             last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
133       }
134       last_pa = block.m_pa;
135       last_plen = block.m_plen;
136     } else {
137       last_plen += block.m_plen;
138     }
139     if (file.is_compressed) {
140       // If logical size and physical size are the same, this block is
141       // uncompressed. Join consecutive uncompressed blocks to save a bit memory
142       // storing metadata.
143       if (block.m_llen == block.m_plen && !compressed_blocks.empty() &&
144           !compressed_blocks.back().IsCompressed()) {
145         compressed_blocks.back().compressed_length += block.m_llen;
146         compressed_blocks.back().uncompressed_length += block.m_llen;
147       } else {
148         compressed_blocks.push_back(
149             CompressedBlock(block.m_la, block.m_plen, block.m_llen));
150       }
151     }
152   }
153   if (last_plen != 0) {
154     file.extents.push_back(ExtentForRange(
155         last_pa / kBlockSize, utils::DivRoundUp(last_plen, kBlockSize)));
156   }
157   return;
158 }
159 
IsErofsImage(const char * path)160 bool IsErofsImage(const char* path) {
161   android::base::unique_fd fd(open(path, O_RDONLY));
162   uint32_t buf{};
163   if (pread(fd.get(), &buf, 4, EROFS_SUPER_OFFSET) < 0) {
164     return false;
165   }
166   return le32toh(buf) == EROFS_SUPER_MAGIC_V1;
167 }
168 
169 }  // namespace
170 
CreateFromFile(const std::string & filename,const CompressionAlgorithm & algo)171 std::unique_ptr<ErofsFilesystem> ErofsFilesystem::CreateFromFile(
172     const std::string& filename, const CompressionAlgorithm& algo) {
173   if (!IsErofsImage(filename.c_str())) {
174     return {};
175   }
176   struct erofs_sb_info sbi {};
177 
178   if (const auto err = erofs_dev_open(&sbi, filename.c_str(), O_RDONLY); err) {
179     PLOG(INFO) << "Failed to open " << filename;
180     return nullptr;
181   }
182   DEFER {
183     erofs_dev_close(&sbi);
184   };
185 
186   if (const auto err = erofs_read_superblock(&sbi); err) {
187     PLOG(INFO) << "Failed to parse " << filename << " as EROFS image";
188     return nullptr;
189   }
190   const auto block_size = 1UL << sbi.blkszbits;
191   struct stat st {};
192   if (const auto err = stat(filename.c_str(), &st); err) {
193     PLOG(ERROR) << "Failed to stat() " << filename;
194     return nullptr;
195   }
196   const time_t time = sbi.build_time;
197   std::vector<File> files;
198   CHECK(ErofsFilesystem::GetFiles(&sbi, filename, &files, algo))
199       << "Failed to parse EROFS image " << filename;
200 
201   LOG(INFO) << "Parsed EROFS image of size " << st.st_size << " built in "
202             << ctime(&time) << " " << filename
203             << ", number of files: " << files.size()
204             << ", block size: " << block_size;
205   LOG(INFO) << "Using compression algo " << algo << " for " << filename;
206   // private ctor doesn't work with make_unique
207   return std::unique_ptr<ErofsFilesystem>(
208       new ErofsFilesystem(filename, st.st_size, std::move(files)));
209 }
210 
GetFiles(std::vector<File> * files) const211 bool ErofsFilesystem::GetFiles(std::vector<File>* files) const {
212   *files = files_;
213   return true;
214 }
215 
GetFiles(struct erofs_sb_info * sbi,const std::string & filename,std::vector<File> * files,const CompressionAlgorithm & algo)216 bool ErofsFilesystem::GetFiles(struct erofs_sb_info* sbi,
217                                const std::string& filename,
218                                std::vector<File>* files,
219                                const CompressionAlgorithm& algo) {
220   size_t unaligned_bytes = 0;
221   const auto block_size = 1UL << sbi->blkszbits;
222   const auto err = erofs_iterate_root_dir(
223       sbi, [&](struct erofs_iterate_dir_context* p_info) {
224         const auto& info = *p_info;
225         if (info.ctx.de_ftype != EROFS_FT_REG_FILE) {
226           return 0;
227         }
228         struct erofs_inode inode {};
229         inode.nid = info.ctx.de_nid;
230         inode.sbi = sbi;
231         int err = erofs_read_inode_from_disk(&inode);
232         if (err) {
233           LOG(ERROR) << "Failed to read inode " << inode.nid;
234           return err;
235         }
236         const auto uncompressed_size = inode.i_size;
237         erofs_off_t compressed_size = 0;
238         if (uncompressed_size == 0) {
239           return 0;
240         }
241         err = GetOccupiedSize(&inode, block_size, &compressed_size);
242         if (err) {
243           LOG(FATAL) << "Failed to get occupied size for " << filename;
244           return err;
245         }
246         // For EROFS_INODE_FLAT_INLINE , most blocks are stored on aligned
247         // addresses. Except the last block, which is stored right after the
248         // inode. These nodes will have a slight amount of data unaligned, which
249         // is fine.
250 
251         File file;
252         file.name = info.path;
253         file.compressed_file_info.zero_padding_enabled =
254             erofs_sb_has_lz4_0padding(sbi);
255         file.is_compressed = compressed_size != uncompressed_size;
256 
257         file.file_stat.st_size = uncompressed_size;
258         file.file_stat.st_ino = inode.nid;
259         FillExtentInfo(&file, filename, &inode, &unaligned_bytes);
260         file.compressed_file_info.algo = algo;
261 
262         files->emplace_back(std::move(file));
263         return 0;
264       });
265   if (err) {
266     LOG(ERROR) << "EROFS files iteration filed " << strerror(-err);
267     return false;
268   }
269 
270   for (auto& file : *files) {
271     NormalizeExtents(&file.extents);
272   }
273   LOG(INFO) << "EROFS image " << filename << " has " << unaligned_bytes
274             << " unaligned bytes, which is "
275             << static_cast<float>(unaligned_bytes) / utils::FileSize(filename) *
276                    100.0f
277             << "% of partition data";
278   return true;
279 }
280 
281 }  // namespace chromeos_update_engine