summaryrefslogtreecommitdiffstats
path: root/src/librbd/image/RemoveRequest.h
blob: b03f8fc7c21cb48c4d4bd4b4585c9120fa39fd79 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_LIBRBD_IMAGE_REMOVE_REQUEST_H
#define CEPH_LIBRBD_IMAGE_REMOVE_REQUEST_H

#include "include/rados/librados.hpp"
#include "librbd/ImageCtx.h"
#include "librbd/image/TypeTraits.h"
#include "common/Timer.h"

#include <list>

class Context;

namespace librbd {

class ProgressContext;

namespace image {

template<typename ImageCtxT = ImageCtx>
class RemoveRequest {
private:
  // mock unit testing support
  typedef ::librbd::image::TypeTraits<ImageCtxT> TypeTraits;
  typedef typename TypeTraits::ContextWQ ContextWQ;
public:
  static RemoveRequest *create(librados::IoCtx &ioctx,
                               const std::string &image_name,
                               const std::string &image_id,
                               bool force, bool from_trash_remove,
                               ProgressContext &prog_ctx,
                               ContextWQ *op_work_queue,
                               Context *on_finish) {
    return new RemoveRequest(ioctx, image_name, image_id, force,
                             from_trash_remove, prog_ctx, op_work_queue,
                             on_finish);
  }

  static RemoveRequest *create(librados::IoCtx &ioctx, ImageCtxT *image_ctx,
                               bool force, bool from_trash_remove,
                               ProgressContext &prog_ctx,
                               ContextWQ *op_work_queue,
                               Context *on_finish) {
    return new RemoveRequest(ioctx, image_ctx, force, from_trash_remove,
                             prog_ctx, op_work_queue, on_finish);
  }

  void send();

private:
  /**
   * @verbatim
   *
   *                                  <start>
   *                                     |
   *                                     v
   *       (skip if already opened) OPEN IMAGE------------------\
   *                                     |                      |
   *                                     v                      |
   *                              PRE REMOVE IMAGE * * *        |
   *                                     |             *        |
   *                                     v             *        |
   *    (skip if invalid data pool) TRIM IMAGE * * * * *        |
   *                                     |             *        |
   *                                     v             *        |
   *                                DETACH CHILD       *        |
   *                                     |             *        |
   *                                     v             *        v
   *                               CLOSE IMAGE < * * * *        |
   *                                     |                      |
   *                               error v                      |
   * /------<--------\              REMOVE HEADER<--------------/
   * |               |                /  |
   * |               |-------<-------/   |
   * |               |                   v
   * |               |              REMOVE JOURNAL
   * |               |                /  |
   * |               |-------<-------/   |
   * |               |                   v
   * v               ^          REMOVE OBJECTMAP
   * |               |                /  |
   * |               |-------<-------/   |
   * |               |                   v
   * |               |    REMOVE MIRROR IMAGE
   * |               |                /  |
   * |               |-------<-------/   |
   * |               |                   v
   * |               |            REMOVE ID OBJECT
   * |               |                /  |
   * |               |-------<-------/   |
   * |               |                   v
   * |               |              REMOVE IMAGE
   * |               |                /  |
   * |               \-------<-------/   |
   * |                                   v
   * \------------------>------------<finish>
   *
   * @endverbatim
   */

  RemoveRequest(librados::IoCtx &ioctx, const std::string &image_name,
                const std::string &image_id, bool force, bool from_trash_remove,
                ProgressContext &prog_ctx, ContextWQ *op_work_queue,
                Context *on_finish);

  RemoveRequest(librados::IoCtx &ioctx, ImageCtxT *image_ctx, bool force,
                bool from_trash_remove, ProgressContext &prog_ctx,
                ContextWQ *op_work_queue, Context *on_finish);

  librados::IoCtx &m_ioctx;
  std::string m_image_name;
  std::string m_image_id;
  ImageCtxT *m_image_ctx = nullptr;
  bool m_force;
  bool m_from_trash_remove;
  ProgressContext &m_prog_ctx;
  ContextWQ *m_op_work_queue;
  Context *m_on_finish;

  CephContext *m_cct;
  std::string m_header_oid;
  bool m_old_format = false;
  bool m_unknown_format = true;

  librados::IoCtx m_parent_io_ctx;

  decltype(m_image_ctx->exclusive_lock) m_exclusive_lock = nullptr;

  int m_ret_val = 0;
  bufferlist m_out_bl;
  std::list<obj_watch_t> m_watchers;

  std::map<uint64_t, SnapInfo> m_snap_infos;

  void open_image();
  void handle_open_image(int r);

  void send_journal_remove();
  void handle_journal_remove(int r);

  void send_object_map_remove();
  void handle_object_map_remove(int r);

  void mirror_image_remove();
  void handle_mirror_image_remove(int r);

  void pre_remove_image();
  void handle_pre_remove_image(int r);

  void trim_image();
  void handle_trim_image(int r);

  void detach_child();
  void handle_detach_child(int r);

  void send_disable_mirror();
  void handle_disable_mirror(int r);

  void send_close_image(int r);
  void handle_send_close_image(int r);

  void remove_header();
  void handle_remove_header(int r);

  void remove_header_v2();
  void handle_remove_header_v2(int r);

  void remove_image();

  void remove_v1_image();
  void handle_remove_v1_image(int r);

  void remove_v2_image();

  void dir_get_image_id();
  void handle_dir_get_image_id(int r);

  void dir_get_image_name();
  void handle_dir_get_image_name(int r);

  void remove_id_object();
  void handle_remove_id_object(int r);

  void dir_remove_image();
  void handle_dir_remove_image(int r);

  void finish(int r);
};

} // namespace image
} // namespace librbd

extern template class librbd::image::RemoveRequest<librbd::ImageCtx>;

#endif // CEPH_LIBRBD_IMAGE_REMOVE_REQUEST_H