1aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
2aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Copyright (C) 2015 The Android Open Source Project
3aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
4aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Licensed under the Apache License, Version 2.0 (the "License");
5aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// you may not use this file except in compliance with the License.
6aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// You may obtain a copy of the License at
7aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
8aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//      http://www.apache.org/licenses/LICENSE-2.0
9aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
10aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Unless required by applicable law or agreed to in writing, software
11aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// distributed under the License is distributed on an "AS IS" BASIS,
12aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// See the License for the specific language governing permissions and
14aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// limitations under the License.
15aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
16cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
17cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_INPLACE_GENERATOR_H_
18cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood#define UPDATE_ENGINE_PAYLOAD_GENERATOR_INPLACE_GENERATOR_H_
19cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
2014158570d3995008dc93a628004118b87a6bca01Alex Deymo#include <map>
21cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood#include <set>
22cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood#include <string>
23cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood#include <vector>
24cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
258cc502dacbccdab96824d42287f230ce04004784Sen Jiang#include "update_engine/payload_generator/blob_file_writer.h"
26cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood#include "update_engine/payload_generator/delta_diff_generator.h"
27cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood#include "update_engine/payload_generator/graph_types.h"
28477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymo#include "update_engine/payload_generator/operations_generator.h"
29cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
30cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood// InplaceGenerator contains all functionality related to the inplace algorithm
31cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood// for generating update payloads. These are the functions used when delta minor
32cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood// version is 1.
33cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
34cd514b531a38da1497630fa68b6e3ef45871893dAllie Woodnamespace chromeos_update_engine {
35cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
36f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// This struct stores all relevant info for an edge that is cut between
37f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// nodes old_src -> old_dst by creating new vertex new_vertex. The new
38f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// relationship is:
39f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// old_src -(read before)-> new_vertex <-(write before)- old_dst
40f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// new_vertex is a MOVE operation that moves some existing blocks into
41f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// temp space. The temp extents are, by necessity, stored in new_vertex
42f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// (as dst extents) and old_dst (as src extents), but they are also broken
43f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// out into tmp_extents, as the nodes themselves may contain many more
44f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo// extents.
45f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymostruct CutEdgeVertexes {
46f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo  Vertex::Index new_vertex;
47f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo  Vertex::Index old_src;
48f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo  Vertex::Index old_dst;
49f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo  std::vector<Extent> tmp_extents;
50f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo};
51f1cbe1783dd025bd7243a8df12f20548196cc023Alex Deymo
52477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymoclass InplaceGenerator : public OperationsGenerator {
53cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood public:
546b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo  // Represents a disk block on the install partition.
556b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo  struct Block {
566b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    // During install, each block on the install partition will be written
576b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    // and some may be read (in all likelihood, many will be read).
586b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    // The reading and writing will be performed by InstallOperations,
596b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    // each of which has a corresponding vertex in a graph.
606b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    // A Block object tells which vertex will read or write this block
616b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    // at install time.
626b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    // Generally, there will be a vector of Block objects whose length
636b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    // is the number of blocks on the install partition.
646b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    Block() : reader(Vertex::kInvalidIndex), writer(Vertex::kInvalidIndex) {}
656b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    Vertex::Index reader;
666b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo    Vertex::Index writer;
676b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo  };
686b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo
69477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymo  InplaceGenerator() = default;
70477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymo
71477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymo  // Checks all the operations in the graph have a type assigned.
72477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymo  static void CheckGraph(const Graph& graph);
73477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymo
74cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Modifies blocks read by 'op' so that any blocks referred to by
75cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // 'remove_extents' are replaced with blocks from 'replace_extents'.
76cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // 'remove_extents' and 'replace_extents' must be the same number of blocks.
77cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Blocks will be substituted in the order listed in the vectors.
78cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // E.g. if 'op' reads blocks 1, 2, 3, 4, 5, 6, 7, 8, remove_extents
79cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // contains blocks 6, 2, 3, 5, and replace blocks contains
80cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // 12, 13, 14, 15, then op will be changed to read from:
81cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // 1, 13, 14, 4, 15, 12, 7, 8
82cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static void SubstituteBlocks(Vertex* vertex,
83cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                               const std::vector<Extent>& remove_extents,
84cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                               const std::vector<Extent>& replace_extents);
85cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
86cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Cuts 'edges' from 'graph' according to the AU algorithm. This means
87cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // for each edge A->B, remove the dependency that B occur before A.
88cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Do this by creating a new operation X that copies from the blocks
89cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // specified by the edge's properties to temp space T. Modify B to read
90cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // from T rather than the blocks in the edge. Modify A to depend on X,
91cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // but not on B. Free space is found by looking in 'blocks'.
92cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Returns true on success.
93cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static bool CutEdges(Graph* graph,
94cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                       const std::set<Edge>& edges,
95cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                       std::vector<CutEdgeVertexes>* out_cuts);
96cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
97cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Creates all the edges for the graph. Writers of a block point to
98cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // readers of the same block. This is because for an edge A->B, B
99cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // must complete before A executes.
100cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static void CreateEdges(Graph* graph,
1016b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo                          const std::vector<Block>& blocks);
102cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
103cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Takes |op_indexes|, which is effectively a mapping from order in
104cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // which the op is performed -> graph vertex index, and produces the
105cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // reverse: a mapping from graph vertex index -> op_indexes index.
106cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static void GenerateReverseTopoOrderMap(
107cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood      const std::vector<Vertex::Index>& op_indexes,
108cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood      std::vector<std::vector<Vertex::Index>::size_type>* reverse_op_indexes);
109cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
110cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Sorts the vector |cuts| by its |cuts[].old_dest| member. Order is
111cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // determined by the order of elements in op_indexes.
112cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static void SortCutsByTopoOrder(
113cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood      const std::vector<Vertex::Index>& op_indexes,
114cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood      std::vector<CutEdgeVertexes>* cuts);
115cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
116cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Given a topologically sorted graph |op_indexes| and |graph|, alters
117cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // |op_indexes| to move all the full operations to the end of the vector.
118cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Full operations should not be depended on, so this is safe.
1193b2c7d0e6d859e6fc75c82b3417f87cf5968a49dAlex Deymo  static void MoveAndSortFullOpsToBack(Graph* graph,
120cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                                std::vector<Vertex::Index>* op_indexes);
121cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
122cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Returns true iff there are no extents in the graph that refer to temp
123cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // blocks. Temp blocks are in the range [kTempBlockStart, kSparseHole).
124cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static bool NoTempBlocksRemain(const Graph& graph);
125cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
126cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Takes a |graph|, which has edges that must be cut, as listed in
127cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // |cuts|.  Cuts the edges. Maintains a list in which the operations
128cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // will be performed (in |op_indexes|) and the reverse (in
129cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // |reverse_op_indexes|).  Cutting edges requires scratch space, and
130cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // if insufficient scratch is found, the file is reread and will be
131cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // send down (either as REPLACE or REPLACE_BZ).  Returns true on
132cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // success.
133cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static bool AssignTempBlocks(
134cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood      Graph* graph,
135568734533c25a5783ea004aeb0da38244dcd3e5bAllie Wood      const std::string& new_part,
1368cc502dacbccdab96824d42287f230ce04004784Sen Jiang      BlobFileWriter* blob_file,
137cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood      std::vector<Vertex::Index>* op_indexes,
138cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood      std::vector<std::vector<Vertex::Index>::size_type>* reverse_op_indexes,
139cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood      const std::vector<CutEdgeVertexes>& cuts);
140cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
141cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Handles allocation of temp blocks to a cut edge by converting the
142cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // dest node to a full op. This removes the need for temp blocks, but
143cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // comes at the cost of a worse compression ratio.
144cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // For example, say we have A->B->A. It would first be cut to form:
145cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // A->B->N<-A, where N copies blocks to temp space. If there are no
146cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // temp blocks, this function can be called to convert it to the form:
147cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // A->B. Now, A is a full operation.
148cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static bool ConvertCutToFullOp(Graph* graph,
149cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                                 const CutEdgeVertexes& cut,
150568734533c25a5783ea004aeb0da38244dcd3e5bAllie Wood                                 const std::string& new_part,
1518cc502dacbccdab96824d42287f230ce04004784Sen Jiang                                 BlobFileWriter* blob_file);
152cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
153cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Takes a graph, which is not a DAG, which represents the files just
154cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // read from disk, and converts it into a DAG by breaking all cycles
155cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // and finding temp space to resolve broken edges.
156cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // The final order of the nodes is given in |final_order|
157cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Some files may need to be reread from disk, thus |fd| and
158cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // |data_file_size| are be passed.
159cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // If |scratch_vertex| is not kInvalidIndex, removes it from
160cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // |final_order| before returning.
161cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Returns true on success.
162cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static bool ConvertGraphToDag(Graph* graph,
163568734533c25a5783ea004aeb0da38244dcd3e5bAllie Wood                                const std::string& new_part,
1648cc502dacbccdab96824d42287f230ce04004784Sen Jiang                                BlobFileWriter* blob_file,
165cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                                std::vector<Vertex::Index>* final_order,
166cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                                Vertex::Index scratch_vertex);
167cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
168cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // Creates a dummy REPLACE_BZ node in the given |vertex|. This can be used
169cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // to provide scratch space. The node writes |num_blocks| blocks starting at
170cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // |start_block|The node should be marked invalid before writing all nodes to
171cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // the output file.
172cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  static void CreateScratchNode(uint64_t start_block,
173cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                                uint64_t num_blocks,
174cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood                                Vertex* vertex);
175cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
176cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // The |blocks| vector contains a reader and writer for each block on the
177cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // filesystem that's being in-place updated. We populate the reader/writer
178cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // fields of |blocks| by calling this function.
179cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // For each block in |operation| that is read or written, find that block
180cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // in |blocks| and set the reader/writer field to the vertex passed.
181cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // |graph| is not strictly necessary, but useful for printing out
182cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood  // error messages.
183a12ee11c78ac6d7c2605921a4006b6a7416e0c35Alex Deymo  static bool AddInstallOpToBlocksVector(const InstallOperation& operation,
184a12ee11c78ac6d7c2605921a4006b6a7416e0c35Alex Deymo                                         const Graph& graph,
185a12ee11c78ac6d7c2605921a4006b6a7416e0c35Alex Deymo                                         Vertex::Index vertex,
186a12ee11c78ac6d7c2605921a4006b6a7416e0c35Alex Deymo                                         std::vector<Block>* blocks);
1876b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo
1886b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo  // Add a vertex (if |existing_vertex| is kInvalidVertex) or update an
1896b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo  // |existing_vertex| with the passed |operation|.
1906b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo  // This method will also register the vertex as the reader or writer of the
1916b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo  // blocks involved in the operation updating the |blocks| vector. The
1926b9e38ef1180efe55e4a82bb18536d1b53fe493dAlex Deymo  // |op_name| associated with the Vertex is used for logging purposes.
193a12ee11c78ac6d7c2605921a4006b6a7416e0c35Alex Deymo  static bool AddInstallOpToGraph(Graph* graph,
194a12ee11c78ac6d7c2605921a4006b6a7416e0c35Alex Deymo                                  Vertex::Index existing_vertex,
195a12ee11c78ac6d7c2605921a4006b6a7416e0c35Alex Deymo                                  std::vector<Block>* blocks,
1965c6bb1d8f89b87e782e26fe74789b6f510704394Chih-Hung Hsieh                                  const InstallOperation& operation,
197a12ee11c78ac6d7c2605921a4006b6a7416e0c35Alex Deymo                                  const std::string& op_name);
198cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
19914158570d3995008dc93a628004118b87a6bca01Alex Deymo  // Apply the transformation stored in |the_map| to the |collection| vector
20014158570d3995008dc93a628004118b87a6bca01Alex Deymo  // replacing the map keys found in |collection| with its associated value in
20114158570d3995008dc93a628004118b87a6bca01Alex Deymo  // |the_map|.
20214158570d3995008dc93a628004118b87a6bca01Alex Deymo  static void ApplyMap(std::vector<uint64_t>* collection,
20314158570d3995008dc93a628004118b87a6bca01Alex Deymo                       const std::map<uint64_t, uint64_t>& the_map);
20414158570d3995008dc93a628004118b87a6bca01Alex Deymo
205f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // Resolve all read-after-write dependencies in the operation list |aops|. The
206f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // operations in |aops| are such that they generate the desired |new_part| if
207f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // applied reading always from the original image. This function reorders the
208f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // operations and generates new operations when needed to make these
209f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // operations produce the same |new_part| result when applied in-place.
210f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // The new operations will create blobs in |data_file_fd| and update
211f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // the file size pointed by |data_file_size| if needed.
212f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // On success, stores the new operations in |aops| in the right order and
213f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  // returns true.
214f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo  static bool ResolveReadAfterWriteDependencies(
21505871fab8b32938ad66ce230250ff6db5563bd7aAlex Deymo      const PartitionConfig& old_part,
216f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo      const PartitionConfig& new_part,
217f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo      uint64_t partition_size,
218f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo      size_t block_size,
2198cc502dacbccdab96824d42287f230ce04004784Sen Jiang      BlobFileWriter* blob_file,
220f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo      std::vector<AnnotatedOperation>* aops);
221f6165357bfbb74b22a4da5780a474f439611e167Alex Deymo
222ebdf17d4202c67933764135bfc1cece629829201Sen Jiang  // Generate the update payload operations for the given partition using
2239b244df41f1bdaddd87b7dbd8e1559556059ed1bAlex Deymo  // only operations that read from the target and/or write to the target,
2249b244df41f1bdaddd87b7dbd8e1559556059ed1bAlex Deymo  // hence, applying the payload "in-place" in the target partition. This method
2259b244df41f1bdaddd87b7dbd8e1559556059ed1bAlex Deymo  // assumes that the contents of the source image are pre-copied to the target
2269b244df41f1bdaddd87b7dbd8e1559556059ed1bAlex Deymo  // partition, up to the size of the source image. Use this method to generate
2279b244df41f1bdaddd87b7dbd8e1559556059ed1bAlex Deymo  // a delta update with the minor version kInPlaceMinorPayloadVersion.
228ebdf17d4202c67933764135bfc1cece629829201Sen Jiang  // The operations are stored in |aops|. All the offsets in the operations
229ebdf17d4202c67933764135bfc1cece629829201Sen Jiang  // reference the data written to |blob_file|.
230477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymo  bool GenerateOperations(
2319b244df41f1bdaddd87b7dbd8e1559556059ed1bAlex Deymo      const PayloadGenerationConfig& config,
232ebdf17d4202c67933764135bfc1cece629829201Sen Jiang      const PartitionConfig& old_part,
233ebdf17d4202c67933764135bfc1cece629829201Sen Jiang      const PartitionConfig& new_part,
2348cc502dacbccdab96824d42287f230ce04004784Sen Jiang      BlobFileWriter* blob_file,
235ebdf17d4202c67933764135bfc1cece629829201Sen Jiang      std::vector<AnnotatedOperation>* aops) override;
2369b244df41f1bdaddd87b7dbd8e1559556059ed1bAlex Deymo
237cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood private:
238477aec2166a571cbe28081d806c3226e8b31b6e9Alex Deymo  DISALLOW_COPY_AND_ASSIGN(InplaceGenerator);
239cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood};
240cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
241cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood};  // namespace chromeos_update_engine
242cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood
243cd514b531a38da1497630fa68b6e3ef45871893dAllie Wood#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_INPLACE_GENERATOR_H_
244