• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2020 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/common/cow_operation_convert.h"
18 
19 #include <base/logging.h>
20 
21 #include "update_engine/payload_generator/extent_ranges.h"
22 #include "update_engine/payload_generator/extent_utils.h"
23 #include "update_engine/update_metadata.pb.h"
24 
25 namespace chromeos_update_engine {
26 
ConvertToCowOperations(const::google::protobuf::RepeatedPtrField<::chromeos_update_engine::InstallOperation> & operations,const::google::protobuf::RepeatedPtrField<CowMergeOperation> & merge_operations)27 std::vector<CowOperation> ConvertToCowOperations(
28     const ::google::protobuf::RepeatedPtrField<
29         ::chromeos_update_engine::InstallOperation>& operations,
30     const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
31         merge_operations) {
32   ExtentRanges merge_extents;
33   std::vector<CowOperation> converted;
34   ExtentRanges modified_extents;
35 
36   // We want all CowCopy ops to be done first, before any COW_REPLACE happen.
37   // Therefore we add these ops in 2 separate loops. This is because during
38   // merge, a CowReplace might modify a block needed by CowCopy, so we always
39   // perform CowCopy first.
40 
41   // This loop handles CowCopy blocks within SOURCE_COPY, and the next loop
42   // converts the leftover blocks to CowReplace?
43   for (const auto& merge_op : merge_operations) {
44     if (merge_op.type() != CowMergeOperation::COW_COPY) {
45       continue;
46     }
47     merge_extents.AddExtent(merge_op.dst_extent());
48     const auto& src_extent = merge_op.src_extent();
49     const auto& dst_extent = merge_op.dst_extent();
50     // Add blocks in reverse order, because snapused specifically prefers this
51     // ordering. Since we already eliminated all self-overlapping SOURCE_COPY
52     // during delta generation, this should be safe to do.
53     for (uint64_t i = src_extent.num_blocks(); i > 0; i--) {
54       auto src_block = src_extent.start_block() + i - 1;
55       auto dst_block = dst_extent.start_block() + i - 1;
56       converted.push_back({CowOperation::CowCopy, src_block, dst_block});
57       modified_extents.AddBlock(dst_block);
58     }
59   }
60   // COW_REPLACE are added after COW_COPY, because replace might modify blocks
61   // needed by COW_COPY. Please don't merge this loop with the previous one.
62   for (const auto& operation : operations) {
63     if (operation.type() != InstallOperation::SOURCE_COPY) {
64       continue;
65     }
66     const auto& src_extents = operation.src_extents();
67     const auto& dst_extents = operation.dst_extents();
68     BlockIterator it1{src_extents};
69     BlockIterator it2{dst_extents};
70     while (!it1.is_end() && !it2.is_end()) {
71       auto src_block = *it1;
72       auto dst_block = *it2;
73       if (!merge_extents.ContainsBlock(dst_block)) {
74         converted.push_back({CowOperation::CowReplace, src_block, dst_block});
75       }
76       ++it1;
77       ++it2;
78     }
79   }
80   return converted;
81 }
82 }  // namespace chromeos_update_engine
83