blob: 73a791b8ffc42182bd57e4fe0683117253a91b90 [file] [log] [blame]
Allie Woodcd514b52015-02-19 13:56:07 -08001// Copyright 2015 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_INPLACE_GENERATOR_H_
6#define UPDATE_ENGINE_PAYLOAD_GENERATOR_INPLACE_GENERATOR_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
12#include "update_engine/payload_generator/delta_diff_generator.h"
13#include "update_engine/payload_generator/graph_types.h"
14
15// InplaceGenerator contains all functionality related to the inplace algorithm
16// for generating update payloads. These are the functions used when delta minor
17// version is 1.
18
19namespace chromeos_update_engine {
20
21class InplaceGenerator {
22 public:
23 // Modifies blocks read by 'op' so that any blocks referred to by
24 // 'remove_extents' are replaced with blocks from 'replace_extents'.
25 // 'remove_extents' and 'replace_extents' must be the same number of blocks.
26 // Blocks will be substituted in the order listed in the vectors.
27 // E.g. if 'op' reads blocks 1, 2, 3, 4, 5, 6, 7, 8, remove_extents
28 // contains blocks 6, 2, 3, 5, and replace blocks contains
29 // 12, 13, 14, 15, then op will be changed to read from:
30 // 1, 13, 14, 4, 15, 12, 7, 8
31 static void SubstituteBlocks(Vertex* vertex,
32 const std::vector<Extent>& remove_extents,
33 const std::vector<Extent>& replace_extents);
34
35 // Cuts 'edges' from 'graph' according to the AU algorithm. This means
36 // for each edge A->B, remove the dependency that B occur before A.
37 // Do this by creating a new operation X that copies from the blocks
38 // specified by the edge's properties to temp space T. Modify B to read
39 // from T rather than the blocks in the edge. Modify A to depend on X,
40 // but not on B. Free space is found by looking in 'blocks'.
41 // Returns true on success.
42 static bool CutEdges(Graph* graph,
43 const std::set<Edge>& edges,
44 std::vector<CutEdgeVertexes>* out_cuts);
45
46 // Creates all the edges for the graph. Writers of a block point to
47 // readers of the same block. This is because for an edge A->B, B
48 // must complete before A executes.
49 static void CreateEdges(Graph* graph,
50 const std::vector<DeltaDiffGenerator::Block>& blocks);
51
52 // Takes |op_indexes|, which is effectively a mapping from order in
53 // which the op is performed -> graph vertex index, and produces the
54 // reverse: a mapping from graph vertex index -> op_indexes index.
55 static void GenerateReverseTopoOrderMap(
56 const std::vector<Vertex::Index>& op_indexes,
57 std::vector<std::vector<Vertex::Index>::size_type>* reverse_op_indexes);
58
59 // Sorts the vector |cuts| by its |cuts[].old_dest| member. Order is
60 // determined by the order of elements in op_indexes.
61 static void SortCutsByTopoOrder(
62 const std::vector<Vertex::Index>& op_indexes,
63 std::vector<CutEdgeVertexes>* cuts);
64
65 // Given a topologically sorted graph |op_indexes| and |graph|, alters
66 // |op_indexes| to move all the full operations to the end of the vector.
67 // Full operations should not be depended on, so this is safe.
68 static void MoveFullOpsToBack(Graph* graph,
69 std::vector<Vertex::Index>* op_indexes);
70
71 // Returns true iff there are no extents in the graph that refer to temp
72 // blocks. Temp blocks are in the range [kTempBlockStart, kSparseHole).
73 static bool NoTempBlocksRemain(const Graph& graph);
74
75 // Takes a |graph|, which has edges that must be cut, as listed in
76 // |cuts|. Cuts the edges. Maintains a list in which the operations
77 // will be performed (in |op_indexes|) and the reverse (in
78 // |reverse_op_indexes|). Cutting edges requires scratch space, and
79 // if insufficient scratch is found, the file is reread and will be
80 // send down (either as REPLACE or REPLACE_BZ). Returns true on
81 // success.
82 static bool AssignTempBlocks(
83 Graph* graph,
84 const std::string& new_root,
85 int data_fd,
86 off_t* data_file_size,
87 std::vector<Vertex::Index>* op_indexes,
88 std::vector<std::vector<Vertex::Index>::size_type>* reverse_op_indexes,
89 const std::vector<CutEdgeVertexes>& cuts);
90
91 // Handles allocation of temp blocks to a cut edge by converting the
92 // dest node to a full op. This removes the need for temp blocks, but
93 // comes at the cost of a worse compression ratio.
94 // For example, say we have A->B->A. It would first be cut to form:
95 // A->B->N<-A, where N copies blocks to temp space. If there are no
96 // temp blocks, this function can be called to convert it to the form:
97 // A->B. Now, A is a full operation.
98 static bool ConvertCutToFullOp(Graph* graph,
99 const CutEdgeVertexes& cut,
100 const std::string& new_root,
101 int data_fd,
102 off_t* data_file_size);
103
104 // Takes a graph, which is not a DAG, which represents the files just
105 // read from disk, and converts it into a DAG by breaking all cycles
106 // and finding temp space to resolve broken edges.
107 // The final order of the nodes is given in |final_order|
108 // Some files may need to be reread from disk, thus |fd| and
109 // |data_file_size| are be passed.
110 // If |scratch_vertex| is not kInvalidIndex, removes it from
111 // |final_order| before returning.
112 // Returns true on success.
113 static bool ConvertGraphToDag(Graph* graph,
114 const std::string& new_root,
115 int fd,
116 off_t* data_file_size,
117 std::vector<Vertex::Index>* final_order,
118 Vertex::Index scratch_vertex);
119
120 // Creates a dummy REPLACE_BZ node in the given |vertex|. This can be used
121 // to provide scratch space. The node writes |num_blocks| blocks starting at
122 // |start_block|The node should be marked invalid before writing all nodes to
123 // the output file.
124 static void CreateScratchNode(uint64_t start_block,
125 uint64_t num_blocks,
126 Vertex* vertex);
127
128 // The |blocks| vector contains a reader and writer for each block on the
129 // filesystem that's being in-place updated. We populate the reader/writer
130 // fields of |blocks| by calling this function.
131 // For each block in |operation| that is read or written, find that block
132 // in |blocks| and set the reader/writer field to the vertex passed.
133 // |graph| is not strictly necessary, but useful for printing out
134 // error messages.
135 static bool AddInstallOpToBlocksVector(
136 const DeltaArchiveManifest_InstallOperation& operation,
137 const Graph& graph,
138 Vertex::Index vertex,
139 std::vector<DeltaDiffGenerator::Block>* blocks);
140
141 private:
142 // This should never be constructed.
143 DISALLOW_IMPLICIT_CONSTRUCTORS(InplaceGenerator);
144};
145
146}; // namespace chromeos_update_engine
147
148#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_INPLACE_GENERATOR_H_