]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/io/AsyncOperation.cc
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / librbd / io / AsyncOperation.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "librbd/io/AsyncOperation.h"
5 #include "include/ceph_assert.h"
6 #include "common/dout.h"
7 #include "librbd/AsioEngine.h"
8 #include "librbd/ImageCtx.h"
9
10 #define dout_subsys ceph_subsys_rbd
11 #undef dout_prefix
12 #define dout_prefix *_dout << "librbd::io::AsyncOperation: "
13
14 namespace librbd {
15 namespace io {
16
17 namespace {
18
19 struct C_CompleteFlushes : public Context {
20 ImageCtx *image_ctx;
21 std::list<Context *> flush_contexts;
22
23 explicit C_CompleteFlushes(ImageCtx *image_ctx,
24 std::list<Context *> &&flush_contexts)
25 : image_ctx(image_ctx), flush_contexts(std::move(flush_contexts)) {
26 }
27 void finish(int r) override {
28 std::shared_lock owner_locker{image_ctx->owner_lock};
29 while (!flush_contexts.empty()) {
30 Context *flush_ctx = flush_contexts.front();
31 flush_contexts.pop_front();
32
33 ldout(image_ctx->cct, 20) << "completed flush: " << flush_ctx << dendl;
34 flush_ctx->complete(0);
35 }
36 }
37 };
38
39 } // anonymous namespace
40
41 void AsyncOperation::start_op(ImageCtx &image_ctx) {
42 ceph_assert(m_image_ctx == NULL);
43 m_image_ctx = &image_ctx;
44
45 ldout(m_image_ctx->cct, 20) << this << " " << __func__ << dendl;
46 std::lock_guard l{m_image_ctx->async_ops_lock};
47 m_image_ctx->async_ops.push_front(&m_xlist_item);
48 }
49
50 void AsyncOperation::finish_op() {
51 ldout(m_image_ctx->cct, 20) << this << " " << __func__ << dendl;
52
53 {
54 std::lock_guard l{m_image_ctx->async_ops_lock};
55 xlist<AsyncOperation *>::iterator iter(&m_xlist_item);
56 ++iter;
57 ceph_assert(m_xlist_item.remove_myself());
58
59 // linked list stored newest -> oldest ops
60 if (!iter.end() && !m_flush_contexts.empty()) {
61 ldout(m_image_ctx->cct, 20) << "moving flush contexts to previous op: "
62 << *iter << dendl;
63 (*iter)->m_flush_contexts.insert((*iter)->m_flush_contexts.end(),
64 m_flush_contexts.begin(),
65 m_flush_contexts.end());
66 return;
67 }
68 }
69
70 if (!m_flush_contexts.empty()) {
71 C_CompleteFlushes *ctx = new C_CompleteFlushes(m_image_ctx,
72 std::move(m_flush_contexts));
73 m_image_ctx->asio_engine->post(ctx, 0);
74 }
75 }
76
77 void AsyncOperation::flush(Context* on_finish) {
78 {
79 std::lock_guard locker{m_image_ctx->async_ops_lock};
80 xlist<AsyncOperation *>::iterator iter(&m_xlist_item);
81 ++iter;
82
83 // linked list stored newest -> oldest ops
84 if (!iter.end()) {
85 (*iter)->m_flush_contexts.push_back(on_finish);
86 return;
87 }
88 }
89
90 m_image_ctx->asio_engine->post(on_finish, 0);
91 }
92
93 } // namespace io
94 } // namespace librbd