ext2_filesystem.cc 12 KB

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