blob: 0467b806d58f655fd5314afb479da2a4727dfabe [file] [log] [blame]
Alex Deymo2b19cfb2015-03-26 00:35:07 -07001// Copyright 2015 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/payload_generator/ext2_filesystem.h"
6
7#include <et/com_err.h>
8#include <ext2fs/ext2_io.h>
9#include <ext2fs/ext2fs.h>
10
11#include <map>
12#include <set>
13
14#include <base/logging.h>
15#include <base/strings/stringprintf.h>
16
Alex Deymo1beda782015-06-07 23:01:25 +020017#include "update_engine/payload_generator/extent_ranges.h"
Alex Deymo2b19cfb2015-03-26 00:35:07 -070018#include "update_engine/payload_generator/extent_utils.h"
19#include "update_engine/update_metadata.pb.h"
20#include "update_engine/utils.h"
21
22using std::set;
23using std::string;
24using std::unique_ptr;
25using std::vector;
26
27namespace chromeos_update_engine {
28
29namespace {
30// Processes all blocks belonging to an inode and adds them to the extent list.
31// This function should match the prototype expected by ext2fs_block_iterate2().
32int ProcessInodeAllBlocks(ext2_filsys fs,
33 blk_t* blocknr,
34 e2_blkcnt_t blockcnt,
35 blk_t ref_blk,
36 int ref_offset,
37 void* priv) {
38 vector<Extent>* extents = static_cast<vector<Extent>*>(priv);
39 AppendBlockToExtents(extents, *blocknr);
40 return 0;
41}
42
43// Processes only indirect, double indirect or triple indirect metadata
44// blocks belonging to an inode. This function should match the prototype of
45// ext2fs_block_iterate2().
46int AddMetadataBlocks(ext2_filsys fs,
47 blk_t* blocknr,
48 e2_blkcnt_t blockcnt,
49 blk_t ref_blk,
50 int ref_offset,
51 void* priv) {
52 set<uint64_t>* blocks = static_cast<set<uint64_t>*>(priv);
53 // If |blockcnt| is non-negative, |blocknr| points to the physical block
54 // number.
55 // If |blockcnt| is negative, it is one of the values: BLOCK_COUNT_IND,
56 // BLOCK_COUNT_DIND, BLOCK_COUNT_TIND or BLOCK_COUNT_TRANSLATOR and
57 // |blocknr| points to a block in the first three cases. The last case is
58 // only used by GNU Hurd, so we shouldn't see those cases here.
59 if (blockcnt == BLOCK_COUNT_IND || blockcnt == BLOCK_COUNT_DIND ||
60 blockcnt == BLOCK_COUNT_TIND) {
61 blocks->insert(*blocknr);
62 }
63 return 0;
64}
65
66struct UpdateFileAndAppendState {
67 std::map<ext2_ino_t, FilesystemInterface::File>* inodes = nullptr;
68 set<ext2_ino_t>* used_inodes = nullptr;
69 vector<FilesystemInterface::File>* files = nullptr;
70 ext2_filsys filsys;
71};
72
73int UpdateFileAndAppend(ext2_ino_t dir,
74 int entry,
75 struct ext2_dir_entry *dirent,
76 int offset,
77 int blocksize,
78 char *buf,
79 void *priv_data) {
80 UpdateFileAndAppendState* state =
81 static_cast<UpdateFileAndAppendState*>(priv_data);
82 uint32_t file_type = dirent->name_len >> 8;
83 // Directories can't have hard links, and they are added from the outer loop.
84 if (file_type == EXT2_FT_DIR)
85 return 0;
86
87 auto ino_file = state->inodes->find(dirent->inode);
88 if (ino_file == state->inodes->end())
89 return 0;
90 auto dir_file = state->inodes->find(dir);
91 if (dir_file == state->inodes->end())
92 return 0;
93 string basename(dirent->name, dirent->name_len & 0xff);
94 ino_file->second.name = dir_file->second.name;
95 if (dir_file->second.name != "/")
96 ino_file->second.name += "/";
97 ino_file->second.name += basename;
98
99 // Append this file to the output. If the file has a hard link, it will be
100 // added twice to the output, but with different names, which is ok. That will
101 // help identify all the versions of the same file.
102 state->files->push_back(ino_file->second);
103 state->used_inodes->insert(dirent->inode);
104 return 0;
105}
106
107} // namespace
108
109unique_ptr<Ext2Filesystem> Ext2Filesystem::CreateFromFile(
110 const string& filename) {
111 if (filename.empty())
112 return nullptr;
113 unique_ptr<Ext2Filesystem> result(new Ext2Filesystem());
114
115 errcode_t err = ext2fs_open(filename.c_str(),
116 0, // flags (read only)
117 0, // superblock block number
118 0, // block_size (autodetect)
119 unix_io_manager,
120 &result->filsys_);
121 if (err) {
122 LOG(ERROR) << "Opening ext2fs " << filename;
123 return nullptr;
124 }
125 return result;
126}
127
128Ext2Filesystem::~Ext2Filesystem() {
129 ext2fs_free(filsys_);
130}
131
132size_t Ext2Filesystem::GetBlockSize() const {
133 return filsys_->blocksize;
134}
135
136size_t Ext2Filesystem::GetBlockCount() const {
137 return ext2fs_blocks_count(filsys_->super);
138}
139
140bool Ext2Filesystem::GetFiles(vector<File>* files) const {
141 TEST_AND_RETURN_FALSE_ERRCODE(ext2fs_read_inode_bitmap(filsys_));
142
143 ext2_inode_scan iscan;
144 TEST_AND_RETURN_FALSE_ERRCODE(
145 ext2fs_open_inode_scan(filsys_, 0 /* buffer_blocks */, &iscan));
146
147 std::map<ext2_ino_t, File> inodes;
148
149 // List of directories. We need to first parse all the files in a directory
150 // to later fix the absolute paths.
151 vector<ext2_ino_t> directories;
152
153 set<uint64_t> inode_blocks;
154
155 // Iterator
156 ext2_ino_t it_ino;
157 ext2_inode it_inode;
158
159 bool ok = true;
160 while (true) {
161 errcode_t error = ext2fs_get_next_inode(iscan, &it_ino, &it_inode);
162 if (error) {
163 LOG(ERROR) << "Failed to retrieve next inode (" << error << ")";
164 ok = false;
165 break;
166 }
167 if (it_ino == 0)
168 break;
169
170 // Skip inodes that are not in use.
171 if (!ext2fs_test_inode_bitmap(filsys_->inode_map, it_ino))
172 continue;
173
174 File& file = inodes[it_ino];
175 if (it_ino == EXT2_RESIZE_INO) {
176 file.name = "<group-descriptors>";
177 } else {
178 file.name = base::StringPrintf("<inode-%u>", it_ino);
179 }
180
181 memset(&file.file_stat, 0, sizeof(file.file_stat));
182 file.file_stat.st_ino = it_ino;
183 file.file_stat.st_mode = it_inode.i_mode;
184 file.file_stat.st_nlink = it_inode.i_links_count;
185 file.file_stat.st_uid = it_inode.i_uid;
186 file.file_stat.st_gid = it_inode.i_gid;
187 file.file_stat.st_size = it_inode.i_size;
188 file.file_stat.st_blksize = filsys_->blocksize;
189 file.file_stat.st_blocks = it_inode.i_blocks;
190 file.file_stat.st_atime = it_inode.i_atime;
191 file.file_stat.st_mtime = it_inode.i_mtime;
192 file.file_stat.st_ctime = it_inode.i_ctime;
193
194 bool is_dir = (ext2fs_check_directory(filsys_, it_ino) == 0);
195 if (is_dir)
196 directories.push_back(it_ino);
197
198 if (!ext2fs_inode_has_valid_blocks(&it_inode))
199 continue;
200
201 // Process the inode data and metadata blocks.
202 // For normal files, inode blocks are indirect, double indirect
203 // and triple indirect blocks (no data blocks). For directories and
204 // the journal, all blocks are considered metadata blocks.
205 int flags = it_ino < EXT2_GOOD_OLD_FIRST_INO ? 0 : BLOCK_FLAG_DATA_ONLY;
206 error = ext2fs_block_iterate2(filsys_, it_ino, flags,
207 nullptr, // block_buf
208 ProcessInodeAllBlocks,
209 &file.extents);
210
211 if (error) {
212 LOG(ERROR) << "Failed to enumerate inode " << it_ino
213 << " blocks (" << error << ")";
214 continue;
215 }
216 if (it_ino >= EXT2_GOOD_OLD_FIRST_INO) {
217 ext2fs_block_iterate2(filsys_, it_ino, 0, nullptr,
218 AddMetadataBlocks,
219 &inode_blocks);
220 }
221 }
222 ext2fs_close_inode_scan(iscan);
223 if (!ok)
224 return false;
225
226 // The set of inodes already added to the output. There can be less elements
227 // here than in files since the later can contain repeated inodes due to
228 // hardlink files.
229 set<ext2_ino_t> used_inodes;
230
231 UpdateFileAndAppendState priv_data;
232 priv_data.inodes = &inodes;
233 priv_data.used_inodes = &used_inodes;
234 priv_data.files = files;
235 priv_data.filsys = filsys_;
236
237 files->clear();
238 // Iterate over all the files of each directory to update the name and add it.
239 for (ext2_ino_t dir_ino : directories) {
240 char* dir_name = nullptr;
241 errcode_t error = ext2fs_get_pathname(filsys_, dir_ino, 0, &dir_name);
242 if (error) {
243 // Not being able to read a directory name is not a fatal error, it is
244 // just skiped.
245 LOG(WARNING) << "Reading directory name on inode " << dir_ino
246 << " (error " << error << ")";
247 inodes[dir_ino].name = base::StringPrintf("<dir-%u>", dir_ino);
248 } else {
249 inodes[dir_ino].name = dir_name;
250 files->push_back(inodes[dir_ino]);
251 used_inodes.insert(dir_ino);
252 }
253
254 error = ext2fs_dir_iterate2(
255 filsys_, dir_ino, 0, nullptr /* block_buf */,
256 UpdateFileAndAppend, &priv_data);
257 if (error) {
258 LOG(WARNING) << "Failed to enumerate files in directory "
259 << inodes[dir_ino].name << " (error " << error << ")";
260 }
261 }
262
263 // Add <inode-blocks> file with the blocks that hold inodes.
264 File inode_file;
265 inode_file.name = "<inode-blocks>";
266 for (uint64_t block : inode_blocks) {
267 AppendBlockToExtents(&inode_file.extents, block);
268 }
269 files->push_back(inode_file);
270
271 // Add <free-spacce> blocs.
272 errcode_t error = ext2fs_read_block_bitmap(filsys_);
273 if (error) {
274 LOG(ERROR) << "Reading the blocks bitmap (error " << error << ")";
275 } else {
276 File free_space;
277 free_space.name = "<free-space>";
278 blk64_t blk_start = ext2fs_get_block_bitmap_start2(filsys_->block_map);
279 blk64_t blk_end = ext2fs_get_block_bitmap_end2(filsys_->block_map);
280 for (blk64_t block = blk_start; block < blk_end; block++) {
281 if (!ext2fs_test_block_bitmap2(filsys_->block_map, block))
282 AppendBlockToExtents(&free_space.extents, block);
283 }
284 files->push_back(free_space);
285 }
286
287 // Add all the unreachable files plus the pseudo-files with an inode. Since
288 // these inodes aren't files in the filesystem, ignore the empty ones.
289 for (const auto& ino_file : inodes) {
290 if (used_inodes.find(ino_file.first) != used_inodes.end())
291 continue;
292 if (ino_file.second.extents.empty())
293 continue;
294
295 File file = ino_file.second;
296 ExtentRanges ranges;
297 ranges.AddExtents(file.extents);
298 file.extents = ranges.GetExtentsForBlockCount(ranges.blocks());
299
300 files->push_back(file);
301 }
302
303 return true;
304}
305
306} // namespace chromeos_update_engine