]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/LibrbdAdminSocketHook.cc
f91bda3f0d2fbe23c73ffda53da141ea36e5a27c
[ceph.git] / ceph / src / librbd / LibrbdAdminSocketHook.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 "common/errno.h"
5
6 #include "librbd/ImageCtx.h"
7 #include "librbd/LibrbdAdminSocketHook.h"
8 #include "librbd/internal.h"
9 #include "librbd/api/Io.h"
10
11 #define dout_subsys ceph_subsys_rbd
12 #undef dout_prefix
13 #define dout_prefix *_dout << "librbdadminsocket: "
14
15 namespace librbd {
16
17 class LibrbdAdminSocketCommand {
18 public:
19 virtual ~LibrbdAdminSocketCommand() {}
20 virtual int call(Formatter *f) = 0;
21 };
22
23 class FlushCacheCommand : public LibrbdAdminSocketCommand {
24 public:
25 explicit FlushCacheCommand(ImageCtx *ictx) : ictx(ictx) {}
26
27 int call(Formatter *f) override {
28 return api::Io<>::flush(*ictx);
29 }
30
31 private:
32 ImageCtx *ictx;
33 };
34
35 struct InvalidateCacheCommand : public LibrbdAdminSocketCommand {
36 public:
37 explicit InvalidateCacheCommand(ImageCtx *ictx) : ictx(ictx) {}
38
39 int call(Formatter *f) override {
40 return invalidate_cache(ictx);
41 }
42
43 private:
44 ImageCtx *ictx;
45 };
46
47 LibrbdAdminSocketHook::LibrbdAdminSocketHook(ImageCtx *ictx) :
48 admin_socket(ictx->cct->get_admin_socket()) {
49
50 std::string command;
51 std::string imagename;
52 int r;
53
54 imagename = ictx->md_ctx.get_pool_name() + "/" + ictx->name;
55 command = "rbd cache flush " + imagename;
56
57 r = admin_socket->register_command(command, this,
58 "flush rbd image " + imagename +
59 " cache");
60 if (r == 0) {
61 commands[command] = new FlushCacheCommand(ictx);
62 }
63
64 command = "rbd cache invalidate " + imagename;
65 r = admin_socket->register_command(command, this,
66 "invalidate rbd image " + imagename +
67 " cache");
68 if (r == 0) {
69 commands[command] = new InvalidateCacheCommand(ictx);
70 }
71 }
72
73 LibrbdAdminSocketHook::~LibrbdAdminSocketHook() {
74 (void)admin_socket->unregister_commands(this);
75 for (Commands::const_iterator i = commands.begin(); i != commands.end();
76 ++i) {
77 delete i->second;
78 }
79 }
80
81 int LibrbdAdminSocketHook::call(std::string_view command,
82 const cmdmap_t& cmdmap,
83 Formatter *f,
84 std::ostream& errss,
85 bufferlist& out) {
86 Commands::const_iterator i = commands.find(command);
87 ceph_assert(i != commands.end());
88 return i->second->call(f);
89 }
90
91 } // namespace librbd