1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_LIBRBD_IMAGE_STATE_H
5 #define CEPH_LIBRBD_IMAGE_STATE_H
7 #include "include/int_types.h"
8 #include "common/Mutex.h"
12 #include "cls/rbd/cls_rbd_types.h"
20 class ImageUpdateWatchers;
23 template <typename ImageCtxT = ImageCtx>
26 ImageState(ImageCtxT *image_ctx);
29 int open(bool skip_open_parent);
30 void open(bool skip_open_parent, Context *on_finish);
33 void close(Context *on_finish);
35 void handle_update_notification();
37 bool is_refresh_required() const;
40 int refresh_if_required();
41 void refresh(Context *on_finish);
43 void snap_set(const cls::rbd::SnapshotNamespace &snap_namespace,
44 const std::string &snap_name,
47 void prepare_lock(Context *on_ready);
48 void handle_prepare_lock_complete();
50 int register_update_watcher(UpdateWatchCtx *watcher, uint64_t *handle);
51 int unregister_update_watcher(uint64_t handle);
52 void flush_update_watchers(Context *on_finish);
53 void shut_down_update_watchers(Context *on_finish);
76 ActionType action_type;
77 uint64_t refresh_seq = 0;
78 cls::rbd::SnapshotNamespace snap_namespace;
79 std::string snap_name;
80 Context *on_ready = nullptr;
82 Action(ActionType action_type) : action_type(action_type) {
84 inline bool operator==(const Action &action) const {
85 if (action_type != action.action_type) {
88 switch (action_type) {
89 case ACTION_TYPE_REFRESH:
90 return (refresh_seq == action.refresh_seq);
91 case ACTION_TYPE_SET_SNAP:
92 return (snap_name == action.snap_name) && (snap_namespace == action.snap_namespace);
93 case ACTION_TYPE_LOCK:
101 typedef std::list<Context *> Contexts;
102 typedef std::pair<Action, Contexts> ActionContexts;
103 typedef std::list<ActionContexts> ActionsContexts;
105 ImageCtxT *m_image_ctx;
108 mutable Mutex m_lock;
109 ActionsContexts m_actions_contexts;
111 uint64_t m_last_refresh;
112 uint64_t m_refresh_seq;
114 ImageUpdateWatchers *m_update_watchers;
116 bool m_skip_open_parent_image;
118 bool is_transition_state() const;
119 bool is_closed() const;
121 const Action *find_pending_refresh() const;
123 void append_context(const Action &action, Context *context);
124 void execute_next_action_unlock();
125 void execute_action_unlock(const Action &action, Context *context);
126 void complete_action_unlock(State next_state, int r);
128 void send_open_unlock();
129 void handle_open(int r);
131 void send_close_unlock();
132 void handle_close(int r);
134 void send_refresh_unlock();
135 void handle_refresh(int r);
137 void send_set_snap_unlock();
138 void handle_set_snap(int r);
140 void send_prepare_lock_unlock();
144 } // namespace librbd
146 extern template class librbd::ImageState<librbd::ImageCtx>;
148 #endif // CEPH_LIBRBD_IMAGE_STATE_H