]> git.proxmox.com Git - ceph.git/blame - ceph/src/librbd/LibrbdAdminSocketHook.cc
import ceph quincy 17.2.6
[ceph.git] / ceph / src / librbd / LibrbdAdminSocketHook.cc
CommitLineData
7c673cae
FG
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"
f67539c2 9#include "librbd/api/Io.h"
7c673cae
FG
10
11#define dout_subsys ceph_subsys_rbd
12#undef dout_prefix
13#define dout_prefix *_dout << "librbdadminsocket: "
14
15namespace librbd {
16
17class LibrbdAdminSocketCommand {
18public:
19 virtual ~LibrbdAdminSocketCommand() {}
9f95a23c 20 virtual int call(Formatter *f) = 0;
7c673cae
FG
21};
22
23class FlushCacheCommand : public LibrbdAdminSocketCommand {
24public:
25 explicit FlushCacheCommand(ImageCtx *ictx) : ictx(ictx) {}
26
9f95a23c 27 int call(Formatter *f) override {
f67539c2 28 return api::Io<>::flush(*ictx);
7c673cae
FG
29 }
30
31private:
32 ImageCtx *ictx;
33};
34
35struct InvalidateCacheCommand : public LibrbdAdminSocketCommand {
36public:
37 explicit InvalidateCacheCommand(ImageCtx *ictx) : ictx(ictx) {}
38
9f95a23c
TL
39 int call(Formatter *f) override {
40 return invalidate_cache(ictx);
7c673cae
FG
41 }
42
43private:
44 ImageCtx *ictx;
45};
46
47LibrbdAdminSocketHook::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
9f95a23c 57 r = admin_socket->register_command(command, this,
7c673cae
FG
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;
9f95a23c 65 r = admin_socket->register_command(command, this,
7c673cae
FG
66 "invalidate rbd image " + imagename +
67 " cache");
68 if (r == 0) {
69 commands[command] = new InvalidateCacheCommand(ictx);
70 }
71}
72
73LibrbdAdminSocketHook::~LibrbdAdminSocketHook() {
9f95a23c 74 (void)admin_socket->unregister_commands(this);
7c673cae
FG
75 for (Commands::const_iterator i = commands.begin(); i != commands.end();
76 ++i) {
7c673cae
FG
77 delete i->second;
78 }
79}
80
9f95a23c
TL
81int LibrbdAdminSocketHook::call(std::string_view command,
82 const cmdmap_t& cmdmap,
39ae355f 83 const bufferlist&,
9f95a23c
TL
84 Formatter *f,
85 std::ostream& errss,
86 bufferlist& out) {
7c673cae 87 Commands::const_iterator i = commands.find(command);
11fdf7f2 88 ceph_assert(i != commands.end());
9f95a23c 89 return i->second->call(f);
7c673cae
FG
90}
91
92} // namespace librbd