| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 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_consumer/block_extent_writer.h" | 
|  | 18 |  | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 20 |  | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 21 | #include <algorithm> | 
|  | 22 |  | 
|  | 23 | #include "update_engine/common/utils.h" | 
|  | 24 | #include "update_engine/payload_generator/delta_diff_generator.h" | 
|  | 25 | #include "update_engine/payload_generator/extent_ranges.h" | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 26 | #include "update_engine/update_metadata.pb.h" | 
|  | 27 |  | 
|  | 28 | namespace chromeos_update_engine { | 
|  | 29 |  | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 30 | bool BlockExtentWriter::Init( | 
|  | 31 | const google::protobuf::RepeatedPtrField<Extent>& extents, | 
|  | 32 | uint32_t block_size) { | 
| Kelvin Zhang | 243d2a1 | 2022-01-26 09:48:40 -0800 | [diff] [blame] | 33 | TEST_NE(extents.size(), 0); | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 34 | extents_ = extents; | 
|  | 35 | cur_extent_idx_ = 0; | 
|  | 36 | buffer_.clear(); | 
|  | 37 | buffer_.reserve(block_size); | 
|  | 38 | block_size_ = block_size; | 
|  | 39 | return true; | 
|  | 40 | } | 
|  | 41 |  | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 42 | bool BlockExtentWriter::WriteExtent(const void* bytes, const size_t count) { | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 43 | const auto& cur_extent = extents_[cur_extent_idx_]; | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 44 | const auto write_extent = | 
|  | 45 | ExtentForRange(cur_extent.start_block() + offset_in_extent_ / block_size_, | 
|  | 46 | count / kBlockSize); | 
|  | 47 | offset_in_extent_ += count; | 
|  | 48 | if (offset_in_extent_ == cur_extent.num_blocks() * block_size_) { | 
|  | 49 | NextExtent(); | 
|  | 50 | } | 
|  | 51 | return WriteExtent(bytes, write_extent, block_size_); | 
|  | 52 | } | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 53 |  | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 54 | size_t BlockExtentWriter::ConsumeWithBuffer(const uint8_t* const data, | 
|  | 55 | const size_t count) { | 
|  | 56 | if (cur_extent_idx_ >= static_cast<size_t>(extents_.size())) { | 
|  | 57 | if (count > 0) { | 
|  | 58 | LOG(ERROR) << "Exhausted all blocks, but still have " << count | 
|  | 59 | << " bytes pending for write"; | 
|  | 60 | } | 
|  | 61 | return 0; | 
|  | 62 | } | 
|  | 63 | const auto& cur_extent = extents_[cur_extent_idx_]; | 
|  | 64 | const auto cur_extent_size = | 
|  | 65 | static_cast<size_t>(cur_extent.num_blocks() * block_size_); | 
|  | 66 |  | 
|  | 67 | const auto write_size = | 
|  | 68 | std::min(cur_extent_size - offset_in_extent_, BUFFER_SIZE); | 
|  | 69 | if (buffer_.empty() && count >= write_size) { | 
|  | 70 | if (!WriteExtent(data, write_size)) { | 
| Kelvin Zhang | 636ba2f | 2022-02-09 14:40:22 -0800 | [diff] [blame] | 71 | LOG(ERROR) << "WriteExtent(" << cur_extent.start_block() << ", " | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 72 | << static_cast<const void*>(data) << ", " << write_size | 
| Kelvin Zhang | 636ba2f | 2022-02-09 14:40:22 -0800 | [diff] [blame] | 73 | << ") failed."; | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 74 | // return value is expected to be greater than 0. Return 0 to signal error | 
|  | 75 | // condition | 
|  | 76 | return 0; | 
|  | 77 | } | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 78 | return write_size; | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 79 | } | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 80 | if (buffer_.size() >= write_size) { | 
| Kelvin Zhang | 243d2a1 | 2022-01-26 09:48:40 -0800 | [diff] [blame] | 81 | LOG(ERROR) | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 82 | << "Data left in buffer should never be >= write_size, otherwise " | 
| Kelvin Zhang | 243d2a1 | 2022-01-26 09:48:40 -0800 | [diff] [blame] | 83 | "we should have send that data to CowWriter. Buffer size: " | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 84 | << buffer_.size() << " write_size: " << write_size; | 
| Kelvin Zhang | 243d2a1 | 2022-01-26 09:48:40 -0800 | [diff] [blame] | 85 | } | 
|  | 86 | const size_t bytes_to_copy = | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 87 | std::min<size_t>(count, write_size - buffer_.size()); | 
| Kelvin Zhang | 243d2a1 | 2022-01-26 09:48:40 -0800 | [diff] [blame] | 88 | TEST_GT(bytes_to_copy, 0U); | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 89 |  | 
|  | 90 | buffer_.insert(buffer_.end(), data, data + bytes_to_copy); | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 91 | TEST_LE(buffer_.size(), write_size); | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 92 |  | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 93 | if (buffer_.size() == write_size) { | 
|  | 94 | if (!WriteExtent(buffer_.data(), write_size)) { | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 95 | LOG(ERROR) << "WriteExtent(" << buffer_.data() << ", " | 
|  | 96 | << cur_extent.start_block() << ", " << cur_extent.num_blocks() | 
|  | 97 | << ") failed."; | 
|  | 98 | return 0; | 
|  | 99 | } | 
|  | 100 | buffer_.clear(); | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 101 | } | 
|  | 102 | return bytes_to_copy; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | // Returns true on success. | 
|  | 106 | // This will construct a COW_REPLACE operation and forward it to CowWriter. It | 
|  | 107 | // is important that caller does not perform SOURCE_COPY operation on this | 
|  | 108 | // class, otherwise raw data will be stored. Caller should find ways to use | 
|  | 109 | // COW_COPY whenever possible. | 
|  | 110 | bool BlockExtentWriter::Write(const void* bytes, size_t count) { | 
|  | 111 | if (count == 0) { | 
|  | 112 | return true; | 
|  | 113 | } | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 114 |  | 
|  | 115 | auto data = static_cast<const uint8_t*>(bytes); | 
|  | 116 | while (count > 0) { | 
| Kelvin Zhang | 243d2a1 | 2022-01-26 09:48:40 -0800 | [diff] [blame] | 117 | const auto bytes_written = ConsumeWithBuffer(data, count); | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 118 | TEST_AND_RETURN_FALSE(bytes_written > 0); | 
|  | 119 | data += bytes_written; | 
|  | 120 | count -= bytes_written; | 
|  | 121 | } | 
|  | 122 | return true; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | bool BlockExtentWriter::NextExtent() { | 
|  | 126 | cur_extent_idx_++; | 
| Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 127 | offset_in_extent_ = 0; | 
| Kelvin Zhang | b170676 | 2021-06-25 15:05:22 -0400 | [diff] [blame] | 128 | return cur_extent_idx_ < static_cast<size_t>(extents_.size()); | 
|  | 129 | } | 
|  | 130 | }  // namespace chromeos_update_engine |