]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/io/ImageRequestWQ.h
0ed843dafebd4ef649b7b8a157f2942315cea0ad
[ceph.git] / ceph / src / librbd / io / ImageRequestWQ.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_IO_IMAGE_REQUEST_WQ_H
5 #define CEPH_LIBRBD_IO_IMAGE_REQUEST_WQ_H
6
7 #include "include/Context.h"
8 #include "common/RWLock.h"
9 #include "common/WorkQueue.h"
10
11 #include <list>
12 #include <atomic>
13
14 namespace librbd {
15
16 class ImageCtx;
17
18 namespace io {
19
20 class AioCompletion;
21 template <typename> class ImageRequest;
22 class ReadResult;
23
24 class ImageRequestWQ : protected ThreadPool::PointerWQ<ImageRequest<ImageCtx> > {
25 public:
26 ImageRequestWQ(ImageCtx *image_ctx, const string &name, time_t ti,
27 ThreadPool *tp);
28
29 ssize_t read(uint64_t off, uint64_t len, ReadResult &&read_result,
30 int op_flags);
31 ssize_t write(uint64_t off, uint64_t len, bufferlist &&bl, int op_flags);
32 ssize_t discard(uint64_t off, uint64_t len, bool skip_partial_discard);
33 ssize_t writesame(uint64_t off, uint64_t len, bufferlist &&bl, int op_flags);
34
35 void aio_read(AioCompletion *c, uint64_t off, uint64_t len,
36 ReadResult &&read_result, int op_flags, bool native_async=true);
37 void aio_write(AioCompletion *c, uint64_t off, uint64_t len,
38 bufferlist &&bl, int op_flags, bool native_async=true);
39 void aio_discard(AioCompletion *c, uint64_t off, uint64_t len,
40 bool skip_partial_discard, bool native_async=true);
41 void aio_flush(AioCompletion *c, bool native_async=true);
42 void aio_writesame(AioCompletion *c, uint64_t off, uint64_t len,
43 bufferlist &&bl, int op_flags, bool native_async=true);
44
45 using ThreadPool::PointerWQ<ImageRequest<ImageCtx> >::drain;
46
47 using ThreadPool::PointerWQ<ImageRequest<ImageCtx> >::empty;
48
49 void shut_down(Context *on_shutdown);
50
51 bool is_lock_required() const;
52 bool is_lock_request_needed() const;
53
54 inline bool writes_blocked() const {
55 RWLock::RLocker locker(m_lock);
56 return (m_write_blockers > 0);
57 }
58
59 int block_writes();
60 void block_writes(Context *on_blocked);
61 void unblock_writes();
62
63 void set_require_lock_on_read();
64 void clear_require_lock_on_read();
65
66 protected:
67 void *_void_dequeue() override;
68 void process(ImageRequest<ImageCtx> *req) override;
69
70 private:
71 typedef std::list<Context *> Contexts;
72
73 struct C_RefreshFinish : public Context {
74 ImageRequestWQ *aio_work_queue;
75 ImageRequest<ImageCtx> *aio_image_request;
76
77 C_RefreshFinish(ImageRequestWQ *aio_work_queue,
78 ImageRequest<ImageCtx> *aio_image_request)
79 : aio_work_queue(aio_work_queue), aio_image_request(aio_image_request) {
80 }
81 void finish(int r) override {
82 aio_work_queue->handle_refreshed(r, aio_image_request);
83 }
84 };
85
86 struct C_BlockedWrites : public Context {
87 ImageRequestWQ *aio_work_queue;
88 C_BlockedWrites(ImageRequestWQ *_aio_work_queue)
89 : aio_work_queue(_aio_work_queue) {
90 }
91
92 void finish(int r) override {
93 aio_work_queue->handle_blocked_writes(r);
94 }
95 };
96
97 ImageCtx &m_image_ctx;
98 mutable RWLock m_lock;
99 Contexts m_write_blocker_contexts;
100 uint32_t m_write_blockers;
101 bool m_require_lock_on_read = false;
102 std::atomic<unsigned> m_in_progress_writes { 0 };
103 std::atomic<unsigned> m_queued_reads { 0 };
104 std::atomic<unsigned> m_queued_writes { 0 };
105 std::atomic<unsigned> m_in_flight_ops { 0 };
106
107 bool m_refresh_in_progress;
108
109 bool m_shutdown;
110 Context *m_on_shutdown;
111
112 inline bool writes_empty() const {
113 RWLock::RLocker locker(m_lock);
114 return (m_queued_writes == 0);
115 }
116
117 void finish_queued_op(ImageRequest<ImageCtx> *req);
118 void finish_in_progress_write();
119
120 int start_in_flight_op(AioCompletion *c);
121 void finish_in_flight_op();
122
123 void queue(ImageRequest<ImageCtx> *req);
124
125 void handle_refreshed(int r, ImageRequest<ImageCtx> *req);
126 void handle_blocked_writes(int r);
127 };
128
129 } // namespace io
130 } // namespace librbd
131
132 #endif // CEPH_LIBRBD_IO_IMAGE_REQUEST_WQ_H