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_IO_IMAGE_REQUEST_WQ_H
5 #define CEPH_LIBRBD_IO_IMAGE_REQUEST_WQ_H
7 #include "include/Context.h"
8 #include "common/RWLock.h"
9 #include "common/WorkQueue.h"
10 #include "librbd/io/Types.h"
22 template <typename
> class ImageRequest
;
25 template <typename ImageCtxT
= librbd::ImageCtx
>
27 : public ThreadPool::PointerWQ
<ImageRequest
<ImageCtxT
> > {
29 ImageRequestWQ(ImageCtxT
*image_ctx
, const string
&name
, time_t ti
,
32 ssize_t
read(uint64_t off
, uint64_t len
, ReadResult
&&read_result
,
34 ssize_t
write(uint64_t off
, uint64_t len
, bufferlist
&&bl
, int op_flags
);
35 ssize_t
discard(uint64_t off
, uint64_t len
, bool skip_partial_discard
);
36 ssize_t
writesame(uint64_t off
, uint64_t len
, bufferlist
&&bl
, int op_flags
);
38 void aio_read(AioCompletion
*c
, uint64_t off
, uint64_t len
,
39 ReadResult
&&read_result
, int op_flags
, bool native_async
=true);
40 void aio_write(AioCompletion
*c
, uint64_t off
, uint64_t len
,
41 bufferlist
&&bl
, int op_flags
, bool native_async
=true);
42 void aio_discard(AioCompletion
*c
, uint64_t off
, uint64_t len
,
43 bool skip_partial_discard
, bool native_async
=true);
44 void aio_flush(AioCompletion
*c
, bool native_async
=true);
45 void aio_writesame(AioCompletion
*c
, uint64_t off
, uint64_t len
,
46 bufferlist
&&bl
, int op_flags
, bool native_async
=true);
48 using ThreadPool::PointerWQ
<ImageRequest
<ImageCtxT
> >::drain
;
50 using ThreadPool::PointerWQ
<ImageRequest
<ImageCtxT
> >::empty
;
52 void shut_down(Context
*on_shutdown
);
54 inline bool writes_blocked() const {
55 RWLock::RLocker
locker(m_lock
);
56 return (m_write_blockers
> 0);
60 void block_writes(Context
*on_blocked
);
61 void unblock_writes();
63 void set_require_lock(Direction direction
, bool enabled
);
66 void *_void_dequeue() override
;
67 void process(ImageRequest
<ImageCtxT
> *req
) override
;
70 typedef std::list
<Context
*> Contexts
;
73 struct C_BlockedWrites
;
74 struct C_RefreshFinish
;
76 ImageCtxT
&m_image_ctx
;
77 mutable RWLock m_lock
;
78 Contexts m_write_blocker_contexts
;
79 uint32_t m_write_blockers
= 0;
80 bool m_require_lock_on_read
= false;
81 bool m_require_lock_on_write
= false;
82 std::atomic
<unsigned> m_queued_reads
{ 0 };
83 std::atomic
<unsigned> m_queued_writes
{ 0 };
84 std::atomic
<unsigned> m_in_flight_ios
{ 0 };
85 std::atomic
<unsigned> m_in_flight_writes
{ 0 };
86 std::atomic
<unsigned> m_io_blockers
{ 0 };
88 bool m_shutdown
= false;
89 Context
*m_on_shutdown
= nullptr;
91 bool is_lock_required(bool write_op
) const;
93 inline bool require_lock_on_read() const {
94 RWLock::RLocker
locker(m_lock
);
95 return m_require_lock_on_read
;
97 inline bool writes_empty() const {
98 RWLock::RLocker
locker(m_lock
);
99 return (m_queued_writes
== 0);
102 void finish_queued_io(ImageRequest
<ImageCtxT
> *req
);
103 void finish_in_flight_write();
105 int start_in_flight_io(AioCompletion
*c
);
106 void finish_in_flight_io();
107 void fail_in_flight_io(int r
, ImageRequest
<ImageCtxT
> *req
);
109 void queue(ImageRequest
<ImageCtxT
> *req
);
111 void handle_acquire_lock(int r
, ImageRequest
<ImageCtxT
> *req
);
112 void handle_refreshed(int r
, ImageRequest
<ImageCtxT
> *req
);
113 void handle_blocked_writes(int r
);
117 } // namespace librbd
119 extern template class librbd::io::ImageRequestWQ
<librbd::ImageCtx
>;
121 #endif // CEPH_LIBRBD_IO_IMAGE_REQUEST_WQ_H