Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 1 | // 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 Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 17 | #include "update_engine/payload_generator/extent_ranges.h" |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 18 | #include "update_engine/payload_generator/extent_utils.h" |
| 19 | #include "update_engine/update_metadata.pb.h" |
| 20 | #include "update_engine/utils.h" |
| 21 | |
| 22 | using std::set; |
| 23 | using std::string; |
| 24 | using std::unique_ptr; |
| 25 | using std::vector; |
| 26 | |
| 27 | namespace chromeos_update_engine { |
| 28 | |
| 29 | namespace { |
| 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(). |
| 32 | int 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(). |
| 46 | int 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 | |
| 66 | struct 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 | |
| 73 | int 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 | |
| 109 | unique_ptr<Ext2Filesystem> Ext2Filesystem::CreateFromFile( |
| 110 | const string& filename) { |
| 111 | if (filename.empty()) |
| 112 | return nullptr; |
| 113 | unique_ptr<Ext2Filesystem> result(new Ext2Filesystem()); |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame^] | 114 | result->filename_ = filename; |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 115 | |
| 116 | errcode_t err = ext2fs_open(filename.c_str(), |
| 117 | 0, // flags (read only) |
| 118 | 0, // superblock block number |
| 119 | 0, // block_size (autodetect) |
| 120 | unix_io_manager, |
| 121 | &result->filsys_); |
| 122 | if (err) { |
| 123 | LOG(ERROR) << "Opening ext2fs " << filename; |
| 124 | return nullptr; |
| 125 | } |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | Ext2Filesystem::~Ext2Filesystem() { |
| 130 | ext2fs_free(filsys_); |
| 131 | } |
| 132 | |
| 133 | size_t Ext2Filesystem::GetBlockSize() const { |
| 134 | return filsys_->blocksize; |
| 135 | } |
| 136 | |
| 137 | size_t Ext2Filesystem::GetBlockCount() const { |
| 138 | return ext2fs_blocks_count(filsys_->super); |
| 139 | } |
| 140 | |
| 141 | bool Ext2Filesystem::GetFiles(vector<File>* files) const { |
| 142 | TEST_AND_RETURN_FALSE_ERRCODE(ext2fs_read_inode_bitmap(filsys_)); |
| 143 | |
| 144 | ext2_inode_scan iscan; |
| 145 | TEST_AND_RETURN_FALSE_ERRCODE( |
| 146 | ext2fs_open_inode_scan(filsys_, 0 /* buffer_blocks */, &iscan)); |
| 147 | |
| 148 | std::map<ext2_ino_t, File> inodes; |
| 149 | |
| 150 | // List of directories. We need to first parse all the files in a directory |
| 151 | // to later fix the absolute paths. |
| 152 | vector<ext2_ino_t> directories; |
| 153 | |
| 154 | set<uint64_t> inode_blocks; |
| 155 | |
| 156 | // Iterator |
| 157 | ext2_ino_t it_ino; |
| 158 | ext2_inode it_inode; |
| 159 | |
| 160 | bool ok = true; |
| 161 | while (true) { |
| 162 | errcode_t error = ext2fs_get_next_inode(iscan, &it_ino, &it_inode); |
| 163 | if (error) { |
| 164 | LOG(ERROR) << "Failed to retrieve next inode (" << error << ")"; |
| 165 | ok = false; |
| 166 | break; |
| 167 | } |
| 168 | if (it_ino == 0) |
| 169 | break; |
| 170 | |
| 171 | // Skip inodes that are not in use. |
| 172 | if (!ext2fs_test_inode_bitmap(filsys_->inode_map, it_ino)) |
| 173 | continue; |
| 174 | |
| 175 | File& file = inodes[it_ino]; |
| 176 | if (it_ino == EXT2_RESIZE_INO) { |
| 177 | file.name = "<group-descriptors>"; |
| 178 | } else { |
| 179 | file.name = base::StringPrintf("<inode-%u>", it_ino); |
| 180 | } |
| 181 | |
| 182 | memset(&file.file_stat, 0, sizeof(file.file_stat)); |
| 183 | file.file_stat.st_ino = it_ino; |
| 184 | file.file_stat.st_mode = it_inode.i_mode; |
| 185 | file.file_stat.st_nlink = it_inode.i_links_count; |
| 186 | file.file_stat.st_uid = it_inode.i_uid; |
| 187 | file.file_stat.st_gid = it_inode.i_gid; |
| 188 | file.file_stat.st_size = it_inode.i_size; |
| 189 | file.file_stat.st_blksize = filsys_->blocksize; |
| 190 | file.file_stat.st_blocks = it_inode.i_blocks; |
| 191 | file.file_stat.st_atime = it_inode.i_atime; |
| 192 | file.file_stat.st_mtime = it_inode.i_mtime; |
| 193 | file.file_stat.st_ctime = it_inode.i_ctime; |
| 194 | |
| 195 | bool is_dir = (ext2fs_check_directory(filsys_, it_ino) == 0); |
| 196 | if (is_dir) |
| 197 | directories.push_back(it_ino); |
| 198 | |
| 199 | if (!ext2fs_inode_has_valid_blocks(&it_inode)) |
| 200 | continue; |
| 201 | |
| 202 | // Process the inode data and metadata blocks. |
| 203 | // For normal files, inode blocks are indirect, double indirect |
| 204 | // and triple indirect blocks (no data blocks). For directories and |
| 205 | // the journal, all blocks are considered metadata blocks. |
| 206 | int flags = it_ino < EXT2_GOOD_OLD_FIRST_INO ? 0 : BLOCK_FLAG_DATA_ONLY; |
| 207 | error = ext2fs_block_iterate2(filsys_, it_ino, flags, |
| 208 | nullptr, // block_buf |
| 209 | ProcessInodeAllBlocks, |
| 210 | &file.extents); |
| 211 | |
| 212 | if (error) { |
| 213 | LOG(ERROR) << "Failed to enumerate inode " << it_ino |
| 214 | << " blocks (" << error << ")"; |
| 215 | continue; |
| 216 | } |
| 217 | if (it_ino >= EXT2_GOOD_OLD_FIRST_INO) { |
| 218 | ext2fs_block_iterate2(filsys_, it_ino, 0, nullptr, |
| 219 | AddMetadataBlocks, |
| 220 | &inode_blocks); |
| 221 | } |
| 222 | } |
| 223 | ext2fs_close_inode_scan(iscan); |
| 224 | if (!ok) |
| 225 | return false; |
| 226 | |
| 227 | // The set of inodes already added to the output. There can be less elements |
| 228 | // here than in files since the later can contain repeated inodes due to |
| 229 | // hardlink files. |
| 230 | set<ext2_ino_t> used_inodes; |
| 231 | |
| 232 | UpdateFileAndAppendState priv_data; |
| 233 | priv_data.inodes = &inodes; |
| 234 | priv_data.used_inodes = &used_inodes; |
| 235 | priv_data.files = files; |
| 236 | priv_data.filsys = filsys_; |
| 237 | |
| 238 | files->clear(); |
| 239 | // Iterate over all the files of each directory to update the name and add it. |
| 240 | for (ext2_ino_t dir_ino : directories) { |
| 241 | char* dir_name = nullptr; |
| 242 | errcode_t error = ext2fs_get_pathname(filsys_, dir_ino, 0, &dir_name); |
| 243 | if (error) { |
| 244 | // Not being able to read a directory name is not a fatal error, it is |
| 245 | // just skiped. |
| 246 | LOG(WARNING) << "Reading directory name on inode " << dir_ino |
| 247 | << " (error " << error << ")"; |
| 248 | inodes[dir_ino].name = base::StringPrintf("<dir-%u>", dir_ino); |
| 249 | } else { |
| 250 | inodes[dir_ino].name = dir_name; |
| 251 | files->push_back(inodes[dir_ino]); |
| 252 | used_inodes.insert(dir_ino); |
| 253 | } |
Alex Deymo | a508705 | 2015-06-12 12:18:56 -0700 | [diff] [blame] | 254 | ext2fs_free_mem(&dir_name); |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 255 | |
| 256 | error = ext2fs_dir_iterate2( |
| 257 | filsys_, dir_ino, 0, nullptr /* block_buf */, |
| 258 | UpdateFileAndAppend, &priv_data); |
| 259 | if (error) { |
| 260 | LOG(WARNING) << "Failed to enumerate files in directory " |
| 261 | << inodes[dir_ino].name << " (error " << error << ")"; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Add <inode-blocks> file with the blocks that hold inodes. |
| 266 | File inode_file; |
| 267 | inode_file.name = "<inode-blocks>"; |
| 268 | for (uint64_t block : inode_blocks) { |
| 269 | AppendBlockToExtents(&inode_file.extents, block); |
| 270 | } |
| 271 | files->push_back(inode_file); |
| 272 | |
| 273 | // Add <free-spacce> blocs. |
| 274 | errcode_t error = ext2fs_read_block_bitmap(filsys_); |
| 275 | if (error) { |
| 276 | LOG(ERROR) << "Reading the blocks bitmap (error " << error << ")"; |
| 277 | } else { |
| 278 | File free_space; |
| 279 | free_space.name = "<free-space>"; |
| 280 | blk64_t blk_start = ext2fs_get_block_bitmap_start2(filsys_->block_map); |
| 281 | blk64_t blk_end = ext2fs_get_block_bitmap_end2(filsys_->block_map); |
| 282 | for (blk64_t block = blk_start; block < blk_end; block++) { |
| 283 | if (!ext2fs_test_block_bitmap2(filsys_->block_map, block)) |
| 284 | AppendBlockToExtents(&free_space.extents, block); |
| 285 | } |
| 286 | files->push_back(free_space); |
| 287 | } |
| 288 | |
| 289 | // Add all the unreachable files plus the pseudo-files with an inode. Since |
| 290 | // these inodes aren't files in the filesystem, ignore the empty ones. |
| 291 | for (const auto& ino_file : inodes) { |
| 292 | if (used_inodes.find(ino_file.first) != used_inodes.end()) |
| 293 | continue; |
| 294 | if (ino_file.second.extents.empty()) |
| 295 | continue; |
| 296 | |
| 297 | File file = ino_file.second; |
| 298 | ExtentRanges ranges; |
| 299 | ranges.AddExtents(file.extents); |
| 300 | file.extents = ranges.GetExtentsForBlockCount(ranges.blocks()); |
| 301 | |
| 302 | files->push_back(file); |
| 303 | } |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame^] | 308 | bool Ext2Filesystem::LoadSettings(chromeos::KeyValueStore* store) const { |
| 309 | // First search for the settings inode following symlinks if we find some. |
| 310 | ext2_ino_t ino_num = 0; |
| 311 | errcode_t err = ext2fs_namei_follow( |
| 312 | filsys_, EXT2_ROOT_INO /* root */, EXT2_ROOT_INO /* cwd */, |
| 313 | "/etc/update_engine.conf", &ino_num); |
| 314 | if (err != 0) |
| 315 | return false; |
| 316 | |
| 317 | ext2_inode ino_data; |
| 318 | if (ext2fs_read_inode(filsys_, ino_num, &ino_data) != 0) |
| 319 | return false; |
| 320 | |
| 321 | // Load the list of blocks and then the contents of the inodes. |
| 322 | vector<Extent> extents; |
| 323 | err = ext2fs_block_iterate2(filsys_, ino_num, BLOCK_FLAG_DATA_ONLY, |
| 324 | nullptr, // block_buf |
| 325 | ProcessInodeAllBlocks, |
| 326 | &extents); |
| 327 | if (err != 0) |
| 328 | return false; |
| 329 | |
| 330 | chromeos::Blob blob; |
| 331 | uint64_t physical_size = BlocksInExtents(extents) * filsys_->blocksize; |
| 332 | // Sparse holes in the settings file are not supported. |
| 333 | if (EXT2_I_SIZE(&ino_data) > physical_size) |
| 334 | return false; |
| 335 | if (!utils::ReadExtents(filename_, extents, &blob, physical_size, |
| 336 | filsys_->blocksize)) |
| 337 | return false; |
| 338 | |
| 339 | string text(blob.begin(), blob.begin() + EXT2_I_SIZE(&ino_data)); |
| 340 | return store->LoadFromString(text); |
| 341 | } |
| 342 | |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 343 | } // namespace chromeos_update_engine |