]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/ExclusiveLock.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / librbd / ExclusiveLock.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_EXCLUSIVE_LOCK_H
5 #define CEPH_LIBRBD_EXCLUSIVE_LOCK_H
6
7 #include "common/AsyncOpTracker.h"
8 #include "librbd/ManagedLock.h"
9 #include "librbd/exclusive_lock/Policy.h"
10 #include "common/RefCountedObj.h"
11
12 namespace librbd {
13
14 template <typename ImageCtxT = ImageCtx>
15 class ExclusiveLock : public RefCountedObject,
16 public ManagedLock<ImageCtxT> {
17 public:
18 static ExclusiveLock *create(ImageCtxT &image_ctx) {
19 return new ExclusiveLock<ImageCtxT>(image_ctx);
20 }
21
22 ExclusiveLock(ImageCtxT &image_ctx);
23
24 bool accept_request(exclusive_lock::OperationRequestType request_type,
25 int *ret_val) const;
26 bool accept_ops() const;
27
28 void block_requests(int r);
29 void unblock_requests();
30
31 void init(uint64_t features, Context *on_init);
32 void shut_down(Context *on_shutdown);
33
34 void handle_peer_notification(int r);
35
36 int get_unlocked_op_error() const;
37 Context *start_op(int* ret_val);
38
39 protected:
40 void shutdown_handler(int r, Context *on_finish) override;
41 void pre_acquire_lock_handler(Context *on_finish) override;
42 void post_acquire_lock_handler(int r, Context *on_finish) override;
43 void pre_release_lock_handler(bool shutting_down,
44 Context *on_finish) override;
45 void post_release_lock_handler(bool shutting_down, int r,
46 Context *on_finish) override;
47 void post_reacquire_lock_handler(int r, Context *on_finish) override;
48
49 private:
50
51 /**
52 * @verbatim
53 *
54 * <start> * * > WAITING_FOR_REGISTER --------\
55 * | * (watch not registered) |
56 * | * |
57 * | * * > WAITING_FOR_PEER ------------\
58 * | * (request_lock busy) |
59 * | * |
60 * | * * * * * * * * * * * * * * |
61 * | * |
62 * v (init) (try_lock/request_lock) * |
63 * UNINITIALIZED -------> UNLOCKED ------------------------> ACQUIRING <--/
64 * ^ |
65 * | v
66 * RELEASING POST_ACQUIRING
67 * | |
68 * | |
69 * | (release_lock) v
70 * PRE_RELEASING <------------------------ LOCKED
71 *
72 * <LOCKED state>
73 * |
74 * v
75 * REACQUIRING -------------------------------------> <finish>
76 * . ^
77 * . |
78 * . . . > <RELEASE action> ---> <ACQUIRE action> ---/
79 *
80 * <UNLOCKED/LOCKED states>
81 * |
82 * |
83 * v
84 * PRE_SHUTTING_DOWN ---> SHUTTING_DOWN ---> SHUTDOWN ---> <finish>
85 *
86 * @endverbatim
87 */
88
89 struct C_InitComplete;
90
91 ImageCtxT& m_image_ctx;
92 Context *m_pre_post_callback = nullptr;
93
94 AsyncOpTracker m_async_op_tracker;
95
96 uint32_t m_request_blocked_count = 0;
97 int m_request_blocked_ret_val = 0;
98
99 int m_acquire_lock_peer_ret_val = 0;
100
101 bool accept_ops(const ceph::mutex &lock) const;
102
103 void handle_init_complete(uint64_t features);
104 void handle_post_acquiring_lock(int r);
105 void handle_post_acquired_lock(int r);
106 };
107
108 } // namespace librbd
109
110 #endif // CEPH_LIBRBD_EXCLUSIVE_LOCK_H