Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / operation / ResizeRequest.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 #ifndef CEPH_LIBRBD_OPERATION_RESIZE_REQUEST_H
4 #define CEPH_LIBRBD_OPERATION_RESIZE_REQUEST_H
5
6 #include "librbd/operation/Request.h"
7 #include "include/xlist.h"
8
9 namespace librbd
10 {
11
12 class ImageCtx;
13 class ProgressContext;
14
15 namespace operation {
16
17 template <typename ImageCtxT = ImageCtx>
18 class ResizeRequest : public Request<ImageCtxT> {
19 public:
20   static ResizeRequest *create(ImageCtxT &image_ctx, Context *on_finish,
21                                uint64_t new_size, bool allow_shrink,
22                                ProgressContext &prog_ctx, uint64_t journal_op_tid,
23                                bool disable_journal) {
24     return new ResizeRequest(image_ctx, on_finish, new_size, allow_shrink, prog_ctx,
25                              journal_op_tid, disable_journal);
26   }
27
28   ResizeRequest(ImageCtxT &image_ctx, Context *on_finish, uint64_t new_size,
29                 bool allow_shrink, ProgressContext &prog_ctx, uint64_t journal_op_tid,
30                 bool disable_journal);
31   ~ResizeRequest() override;
32
33   inline bool shrinking() const {
34     return (m_shrink_size_visible && m_new_size < m_original_size);
35   }
36
37   inline uint64_t get_image_size() const {
38     return m_new_size;
39   }
40
41   void send() override;
42
43 protected:
44   void send_op() override;
45   bool should_complete(int r) override {
46     return true;
47   }
48   bool can_affect_io() const override {
49     return true;
50   }
51   journal::Event create_event(uint64_t op_tid) const override {
52     return journal::ResizeEvent(op_tid, m_new_size);
53   }
54
55 private:
56   /**
57    * Resize goes through the following state machine to resize the image
58    * and update the object map:
59    *
60    * @verbatim
61    *
62    * <start>
63    *    |
64    *    v
65    * STATE_PRE_BLOCK_WRITES
66    *    |
67    *    v
68    * STATE_APPEND_OP_EVENT (skip if journaling
69    *    |                   disabled)
70    *    | (unblock writes)
71    *    |
72    *    |
73    *    | (grow)
74    *    |\--------> STATE_GROW_OBJECT_MAP (skip if object map
75    *    |                 |                disabled)
76    *    |                 v
77    *    |           STATE_POST_BLOCK_WRITES
78    *    |                 |
79    *    |                 v
80    *    |           STATE_UPDATE_HEADER ----------------------------\
81    *    |                                                           |
82    *    | (shrink)                                                  |
83    *    |\--------> STATE_FLUSH_CACHE                               |
84    *    |                 |                                         |
85    *    |                 v                                         |
86    *    |           STATE_INVALIDATE_CACHE                          |
87    *    |                 |                                         |
88    *    |                 v                                         |
89    *    |           STATE_TRIM_IMAGE                                |
90    *    |                 |                                         |
91    *    |                 v                                         |
92    *    |           STATE_POST_BLOCK_WRITES                         |
93    *    |                 |                                         |
94    *    |                 v                                         |
95    *    |           STATE_UPDATE_HEADER                             |
96    *    |                 |                                         |
97    *    |                 v                                         |
98    *    |           STATE_SHRINK_OBJECT_MAP (skip if object map     |
99    *    |                 |                  disabled)              |
100    *    |                 | (unblock writes)                        |
101    *    | (no change)     v                                         |
102    *    \------------> <finish> <-----------------------------------/
103    *
104    * @endverbatim
105    *
106    * The _OBJECT_MAP states are skipped if the object map isn't enabled.
107    * The state machine will immediately transition to _FINISHED if there
108    * are no objects to trim.
109    */
110
111   uint64_t m_original_size;
112   uint64_t m_new_size;
113   bool m_allow_shrink = true;
114   ProgressContext &m_prog_ctx;
115   uint64_t m_new_parent_overlap;
116   bool m_shrink_size_visible = false;
117   bool m_disable_journal = false;
118
119   typename xlist<ResizeRequest<ImageCtxT>*>::item m_xlist_item;
120
121   void send_pre_block_writes();
122   Context *handle_pre_block_writes(int *result);
123
124   Context *send_append_op_event();
125   Context *handle_append_op_event(int *result);
126
127   void send_flush_cache();
128   Context *handle_flush_cache(int *result);
129
130   void send_invalidate_cache();
131   Context *handle_invalidate_cache(int *result);
132
133   void send_trim_image();
134   Context *handle_trim_image(int *result);
135
136   Context *send_grow_object_map();
137   Context *handle_grow_object_map(int *result);
138
139   Context *send_shrink_object_map();
140   Context *handle_shrink_object_map(int *result);
141
142   void send_post_block_writes();
143   Context *handle_post_block_writes(int *result);
144
145   void send_update_header();
146   Context *handle_update_header(int *result);
147
148   void compute_parent_overlap();
149   void update_size_and_overlap();
150
151 };
152
153 } // namespace operation
154 } // namespace librbd
155
156 extern template class librbd::operation::ResizeRequest<librbd::ImageCtx>;
157
158 #endif // CEPH_LIBRBD_OPERATION_RESIZE_REQUEST_H