summaryrefslogtreecommitdiffstats
path: root/src/librbd/migration/QCOWFormat.h
blob: b365067165026565c1febb3106f1e08ecc2bd1c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_LIBRBD_MIGRATION_QCOW_FORMAT_H
#define CEPH_LIBRBD_MIGRATION_QCOW_FORMAT_H

#include "include/int_types.h"
#include "librbd/Types.h"
#include "librbd/migration/FormatInterface.h"
#include "librbd/migration/QCOW.h"
#include "acconfig.h"
#include "json_spirit/json_spirit.h"
#include <boost/asio/io_context_strand.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <deque>
#include <vector>
#include <memory>

struct Context;

namespace librbd {

struct AsioEngine;
struct ImageCtx;

namespace migration {

template <typename> struct SourceSpecBuilder;
struct StreamInterface;

namespace qcow_format {

struct LookupTable {
  LookupTable() {}
  LookupTable(uint32_t size) : size(size) {}

  bufferlist bl;
  uint64_t* cluster_offsets = nullptr;
  uint32_t size = 0;
  bool decoded = false;

  void init();
  void decode();
};

} // namespace qcow_format

template <typename ImageCtxT>
class QCOWFormat : public FormatInterface {
public:
  static QCOWFormat* create(
      ImageCtxT* image_ctx, const json_spirit::mObject& json_object,
      const SourceSpecBuilder<ImageCtxT>* source_spec_builder) {
    return new QCOWFormat(image_ctx, json_object, source_spec_builder);
  }

  QCOWFormat(ImageCtxT* image_ctx, const json_spirit::mObject& json_object,
             const SourceSpecBuilder<ImageCtxT>* source_spec_builder);
  QCOWFormat(const QCOWFormat&) = delete;
  QCOWFormat& operator=(const QCOWFormat&) = delete;

  void open(Context* on_finish) override;
  void close(Context* on_finish) override;

  void get_snapshots(SnapInfos* snap_infos, Context* on_finish) override;
  void get_image_size(uint64_t snap_id, uint64_t* size,
                      Context* on_finish) override;

  bool read(io::AioCompletion* aio_comp, uint64_t snap_id,
            io::Extents&& image_extents, io::ReadResult&& read_result,
            int op_flags, int read_flags,
            const ZTracer::Trace &parent_trace) override;

  void list_snaps(io::Extents&& image_extents, io::SnapIds&& snap_ids,
                  int list_snaps_flags, io::SnapshotDelta* snapshot_delta,
                  const ZTracer::Trace &parent_trace,
                  Context* on_finish) override;

private:
  /**
   * @verbatim
   *
   * <start>
   *    |
   *    v
   *  OPEN
   *    |
   *    v
   *  PROBE
   *    |
   *    |\---> READ V1 HEADER ----------\
   *    |                               |
   *    \----> READ V2 HEADER           |
   *              |                     |
   *              |     /----------\    |
   *              |     |          |    |
   *              v     v          |    |
   *           READ SNAPSHOT       |    |
   *              |                |    |
   *              v                |    |
   *           READ SNAPSHOT EXTRA |    |
   *              |                |    |
   *              v                |    |
   *           READ SNAPSHOT L1 TABLE   |
   *              |                     |
   *              \--------------------\|
   *                                    |
   *                                    v
   *                              READ L1 TABLE
   *                                    |
   *                                    v
   *                              READ BACKING FILE
   *                                    |
   *    /-------------------------------/
   *    |
   *    v
   * <opened>
   *
   * @endverbatim
   */

  struct Cluster;
  struct ClusterCache;
  struct L2TableCache;
  struct ReadRequest;
  struct ListSnapsRequest;

  struct Snapshot {
    std::string id;
    std::string name;

    utime_t timestamp;
    uint64_t size = 0;

    uint64_t l1_table_offset = 0;
    qcow_format::LookupTable l1_table;

    uint32_t extra_data_size = 0;
  };

  ImageCtxT* m_image_ctx;
  json_spirit::mObject m_json_object;
  const SourceSpecBuilder<ImageCtxT>* m_source_spec_builder;

  boost::asio::io_context::strand m_strand;
  std::shared_ptr<StreamInterface> m_stream;

  bufferlist m_bl;

  uint64_t m_size = 0;

  uint64_t m_backing_file_offset = 0;
  uint32_t m_backing_file_size = 0;

  uint32_t m_cluster_bits = 0;
  uint32_t m_cluster_size = 0;
  uint64_t m_cluster_offset_mask = 0;
  uint64_t m_cluster_mask = 0;

  uint32_t m_l1_shift = 0;
  uint64_t m_l1_table_offset = 0;
  qcow_format::LookupTable m_l1_table;

  uint32_t m_l2_bits = 0;
  uint32_t m_l2_size = 0;

  uint32_t m_snapshot_count = 0;
  uint64_t m_snapshots_offset = 0;
  std::map<uint64_t, Snapshot> m_snapshots;

  std::unique_ptr<L2TableCache> m_l2_table_cache;
  std::unique_ptr<ClusterCache> m_cluster_cache;

  void handle_open(int r, Context* on_finish);

  void probe(Context* on_finish);
  void handle_probe(int r, Context* on_finish);

#ifdef WITH_RBD_MIGRATION_FORMAT_QCOW_V1
  void read_v1_header(Context* on_finish);
  void handle_read_v1_header(int r, Context* on_finish);
#endif // WITH_RBD_MIGRATION_FORMAT_QCOW_V1

  void read_v2_header(Context* on_finish);
  void handle_read_v2_header(int r, Context* on_finish);

  void read_snapshot(Context* on_finish);
  void handle_read_snapshot(int r, Context* on_finish);

  void read_snapshot_extra(Context* on_finish);
  void handle_read_snapshot_extra(int r, Context* on_finish);

  void read_snapshot_l1_table(Context* on_finish);
  void handle_read_snapshot_l1_table(int r, Context* on_finish);

  void read_l1_table(Context* on_finish);
  void handle_read_l1_table(int r, Context* on_finish);

  void read_backing_file(Context* on_finish);

  void handle_list_snaps(int r, io::Extents&& image_extents,
                         io::SnapIds&& snap_ids,
                         io::SnapshotDelta* snapshot_delta, Context* on_finish);
};

} // namespace migration
} // namespace librbd

extern template class librbd::migration::QCOWFormat<librbd::ImageCtx>;

#endif // CEPH_LIBRBD_MIGRATION_QCOW_FORMAT_H