Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / operation / SnapshotCreateRequest.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_LIBRBD_OPERATION_SNAPSHOT_CREATE_REQUEST_H
5 #define CEPH_LIBRBD_OPERATION_SNAPSHOT_CREATE_REQUEST_H
6
7 #include "cls/rbd/cls_rbd_types.h"
8 #include "librbd/Types.h"
9 #include "librbd/operation/Request.h"
10 #include <string>
11
12 class Context;
13
14 namespace librbd {
15
16 class ImageCtx;
17
18 namespace operation {
19
20 template <typename ImageCtxT = ImageCtx>
21 class SnapshotCreateRequest : public Request<ImageCtxT> {
22 public:
23   /**
24    * Snap Create goes through the following state machine:
25    *
26    * @verbatim
27    *
28    *            <start>
29    *               |
30    *               v
31    *           STATE_SUSPEND_REQUESTS
32    *               |
33    *               v
34    *           STATE_SUSPEND_AIO * * * * * * * * * * * * *
35    *               |                                     *
36    *               v                                     *
37    *           STATE_APPEND_OP_EVENT (skip if journal    *
38    *               |                  disabled)          *
39    *   (retry)     v                                     *
40    *   . . . > STATE_ALLOCATE_SNAP_ID                    *
41    *   .           |                                     *
42    *   .           v                                     *
43    *   . . . . STATE_CREATE_SNAP * * * * * * * * * *     *
44    *               |                               *     *
45    *               v                               *     *
46    *           STATE_CREATE_OBJECT_MAP (skip if    *     *
47    *               |                    disabled)  *     *
48    *               |                               *     *
49    *               |                               v     *
50    *               |              STATE_RELEASE_SNAP_ID  *
51    *               |                     |               *
52    *               |                     v               *
53    *               \----------------> <finish> < * * * * *
54    *
55    * @endverbatim
56    *
57    * The _CREATE_STATE state may repeat back to the _ALLOCATE_SNAP_ID state
58    * if a stale snapshot context is allocated. If the create operation needs
59    * to abort, the error path is followed to record the result in the journal
60    * (if enabled) and bubble the originating error code back to the client.
61    */
62   SnapshotCreateRequest(ImageCtxT &image_ctx, Context *on_finish,
63                         const cls::rbd::SnapshotNamespace &snap_namespace,
64                         const std::string &snap_name,
65                         uint64_t journal_op_tid,
66                         bool skip_object_map);
67
68 protected:
69   void send_op() override;
70   bool should_complete(int r) override {
71     return true;
72   }
73   bool can_affect_io() const override {
74     return true;
75   }
76   journal::Event create_event(uint64_t op_tid) const override {
77     return journal::SnapCreateEvent(op_tid, m_snap_namespace, m_snap_name);
78   }
79
80 private:
81   cls::rbd::SnapshotNamespace m_snap_namespace;
82   std::string m_snap_name;
83   bool m_skip_object_map;
84
85   int m_ret_val;
86
87   uint64_t m_snap_id;
88   uint64_t m_size;
89   ParentInfo m_parent_info;
90
91   void send_suspend_requests();
92   Context *handle_suspend_requests(int *result);
93
94   void send_suspend_aio();
95   Context *handle_suspend_aio(int *result);
96
97   void send_append_op_event();
98   Context *handle_append_op_event(int *result);
99
100   void send_allocate_snap_id();
101   Context *handle_allocate_snap_id(int *result);
102
103   void send_create_snap();
104   Context *handle_create_snap(int *result);
105
106   Context *send_create_object_map();
107   Context *handle_create_object_map(int *result);
108
109   void send_release_snap_id();
110   Context *handle_release_snap_id(int *result);
111
112   void update_snap_context();
113
114   void save_result(int *result) {
115     if (m_ret_val == 0 && *result < 0) {
116       m_ret_val = *result;
117     }
118   }
119 };
120
121 } // namespace operation
122 } // namespace librbd
123
124 extern template class librbd::operation::SnapshotCreateRequest<librbd::ImageCtx>;
125
126 #endif // CEPH_LIBRBD_OPERATION_SNAPSHOT_CREATE_REQUEST_H