]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/PluginRegistry.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / librbd / PluginRegistry.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/PluginRegistry.h"
5 #include "include/Context.h"
6 #include "common/dout.h"
7 #include "librbd/cache/ImageWriteback.h"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/plugin/Api.h"
10 #include <boost/tokenizer.hpp>
11
12 #define dout_subsys ceph_subsys_rbd
13 #undef dout_prefix
14 #define dout_prefix *_dout << "librbd::PluginRegistry: " \
15 << this << " " << __func__ << ": "
16
17 namespace librbd {
18
19 template <typename I>
20 PluginRegistry<I>::PluginRegistry(I* image_ctx)
21 : m_image_ctx(image_ctx), m_plugin_api(std::make_unique<plugin::Api<I>>()),
22 m_image_writeback(std::make_unique<cache::ImageWriteback<I>>(*image_ctx)) {
23 }
24
25 template <typename I>
26 PluginRegistry<I>::~PluginRegistry() {
27 }
28
29 template <typename I>
30 void PluginRegistry<I>::init(const std::string& plugins, Context* on_finish) {
31 auto cct = m_image_ctx->cct;
32 auto plugin_registry = cct->get_plugin_registry();
33
34 auto gather_ctx = new C_Gather(cct, on_finish);
35
36 boost::tokenizer<boost::escaped_list_separator<char>> tokenizer(plugins);
37 for (auto token : tokenizer) {
38 ldout(cct, 5) << "attempting to load plugin: " << token << dendl;
39
40 auto ctx = gather_ctx->new_sub();
41
42 auto plugin = dynamic_cast<plugin::Interface<I>*>(
43 plugin_registry->get_with_load("librbd", "librbd_" + token));
44 if (plugin == nullptr) {
45 lderr(cct) << "failed to load plugin: " << token << dendl;
46 ctx->complete(-ENOSYS);
47 break;
48 }
49
50 plugin->init(
51 m_image_ctx, *m_plugin_api, *m_image_writeback, m_plugin_hook_points, ctx);
52 }
53
54 gather_ctx->activate();
55 }
56
57 template <typename I>
58 void PluginRegistry<I>::acquired_exclusive_lock(Context* on_finish) {
59 auto cct = m_image_ctx->cct;
60 ldout(cct, 20) << dendl;
61
62 auto gather_ctx = new C_Gather(cct, on_finish);
63
64 for (auto &hook : m_plugin_hook_points) {
65 auto ctx = gather_ctx->new_sub();
66 hook->acquired_exclusive_lock(ctx);
67 }
68 gather_ctx->activate();
69 }
70
71 template <typename I>
72 void PluginRegistry<I>::prerelease_exclusive_lock(Context* on_finish) {
73 auto cct = m_image_ctx->cct;
74 ldout(cct, 20) << dendl;
75
76 auto gather_ctx = new C_Gather(cct, on_finish);
77
78 for (auto &hook : m_plugin_hook_points) {
79 auto ctx = gather_ctx->new_sub();
80 hook->prerelease_exclusive_lock(ctx);
81 }
82 gather_ctx->activate();
83 }
84
85 template <typename I>
86 void PluginRegistry<I>::discard(Context* on_finish) {
87 auto cct = m_image_ctx->cct;
88 ldout(cct, 20) << dendl;
89
90 auto gather_ctx = new C_Gather(cct, on_finish);
91
92 for (auto &hook : m_plugin_hook_points) {
93 auto ctx = gather_ctx->new_sub();
94 hook->discard(ctx);
95 }
96 gather_ctx->activate();
97 }
98
99 } // namespace librbd
100
101 template class librbd::PluginRegistry<librbd::ImageCtx>;