block_mapping.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/block_mapping.h"
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <functional>
  21. #include <string>
  22. #include <utility>
  23. #include <vector>
  24. #include "update_engine/common/utils.h"
  25. using std::string;
  26. using std::vector;
  27. namespace {
  28. size_t HashValue(const brillo::Blob& blob) {
  29. std::hash<string> hash_fn;
  30. return hash_fn(string(blob.begin(), blob.end()));
  31. }
  32. } // namespace
  33. namespace chromeos_update_engine {
  34. BlockMapping::BlockId BlockMapping::AddBlock(const brillo::Blob& block_data) {
  35. return AddBlock(-1, 0, block_data);
  36. }
  37. BlockMapping::BlockId BlockMapping::AddDiskBlock(int fd, off_t byte_offset) {
  38. brillo::Blob blob(block_size_);
  39. ssize_t bytes_read = 0;
  40. if (!utils::PReadAll(fd, blob.data(), block_size_, byte_offset, &bytes_read))
  41. return -1;
  42. if (static_cast<size_t>(bytes_read) != block_size_)
  43. return -1;
  44. return AddBlock(fd, byte_offset, blob);
  45. }
  46. bool BlockMapping::AddManyDiskBlocks(int fd,
  47. off_t initial_byte_offset,
  48. size_t num_blocks,
  49. vector<BlockId>* block_ids) {
  50. bool ret = true;
  51. block_ids->resize(num_blocks);
  52. for (size_t block = 0; block < num_blocks; block++) {
  53. (*block_ids)[block] =
  54. AddDiskBlock(fd, initial_byte_offset + block * block_size_);
  55. ret = ret && (*block_ids)[block] != -1;
  56. }
  57. return ret;
  58. }
  59. BlockMapping::BlockId BlockMapping::AddBlock(int fd,
  60. off_t byte_offset,
  61. const brillo::Blob& block_data) {
  62. if (block_data.size() != block_size_)
  63. return -1;
  64. size_t h = HashValue(block_data);
  65. // We either reuse a UniqueBlock or create a new one. If we need a new
  66. // UniqueBlock it could also be part of a new or existing bucket (if there is
  67. // a hash collision).
  68. vector<UniqueBlock>* bucket = nullptr;
  69. auto mapping_it = mapping_.find(h);
  70. if (mapping_it == mapping_.end()) {
  71. bucket = &mapping_[h];
  72. } else {
  73. for (UniqueBlock& existing_block : mapping_it->second) {
  74. bool equals = false;
  75. if (!existing_block.CompareData(block_data, &equals))
  76. return -1;
  77. if (equals)
  78. return existing_block.block_id;
  79. }
  80. bucket = &mapping_it->second;
  81. }
  82. // No existing block was found at this point, so we create and fill in a new
  83. // one.
  84. bucket->emplace_back();
  85. UniqueBlock* new_ublock = &bucket->back();
  86. new_ublock->times_read = 1;
  87. new_ublock->fd = fd;
  88. new_ublock->byte_offset = byte_offset;
  89. new_ublock->block_id = used_block_ids++;
  90. // We need to cache blocks that are not referencing any disk location.
  91. if (fd == -1)
  92. new_ublock->block_data = block_data;
  93. return new_ublock->block_id;
  94. }
  95. bool BlockMapping::UniqueBlock::CompareData(const brillo::Blob& other_block,
  96. bool* equals) {
  97. if (!block_data.empty()) {
  98. *equals = block_data == other_block;
  99. return true;
  100. }
  101. const size_t block_size = other_block.size();
  102. brillo::Blob blob(block_size);
  103. ssize_t bytes_read = 0;
  104. if (!utils::PReadAll(fd, blob.data(), block_size, byte_offset, &bytes_read))
  105. return false;
  106. if (static_cast<size_t>(bytes_read) != block_size)
  107. return false;
  108. *equals = blob == other_block;
  109. // We increase the number of times we had to read this block from disk and
  110. // we cache this block based on that. This caching method is optimized for
  111. // the common use case of having two partitions that share blocks between them
  112. // but have few repeated blocks inside each partition, such as the block
  113. // with all zeros or duplicated files.
  114. times_read++;
  115. if (times_read > 3)
  116. block_data = std::move(blob);
  117. return true;
  118. }
  119. bool MapPartitionBlocks(const string& old_part,
  120. const string& new_part,
  121. size_t old_size,
  122. size_t new_size,
  123. size_t block_size,
  124. vector<BlockMapping::BlockId>* old_block_ids,
  125. vector<BlockMapping::BlockId>* new_block_ids) {
  126. BlockMapping mapping(block_size);
  127. if (mapping.AddBlock(brillo::Blob(block_size, '\0')) != 0)
  128. return false;
  129. int old_fd = HANDLE_EINTR(open(old_part.c_str(), O_RDONLY));
  130. int new_fd = HANDLE_EINTR(open(new_part.c_str(), O_RDONLY));
  131. ScopedFdCloser old_fd_closer(&old_fd);
  132. ScopedFdCloser new_fd_closer(&new_fd);
  133. TEST_AND_RETURN_FALSE(mapping.AddManyDiskBlocks(
  134. old_fd, 0, old_size / block_size, old_block_ids));
  135. TEST_AND_RETURN_FALSE(mapping.AddManyDiskBlocks(
  136. new_fd, 0, new_size / block_size, new_block_ids));
  137. return true;
  138. }
  139. } // namespace chromeos_update_engine