|
| 1 | +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
| 2 | +// vim: ts=8 sw=2 smarttab |
| 3 | + |
| 4 | +#include "BlueAdmin.h" |
| 5 | +#include "common/pretty_binary.h" |
| 6 | +#include "common/debug.h" |
| 7 | +#include <asm-generic/errno-base.h> |
| 8 | +#include <vector> |
| 9 | +#include <limits> |
| 10 | + |
| 11 | +#define dout_subsys ceph_subsys_bluestore |
| 12 | +#define dout_context store.cct |
| 13 | + |
| 14 | +using ceph::bufferlist; |
| 15 | +using ceph::Formatter; |
| 16 | +using ceph::common::cmd_getval; |
| 17 | + |
| 18 | +BlueStore::SocketHook::SocketHook(BlueStore& store) |
| 19 | + : store(store) |
| 20 | +{ |
| 21 | + AdminSocket *admin_socket = store.cct->get_admin_socket(); |
| 22 | + if (admin_socket) { |
| 23 | + int r = admin_socket->register_command( |
| 24 | + "bluestore collections", |
| 25 | + this, |
| 26 | + "list all collections"); |
| 27 | + if (r != 0) { |
| 28 | + dout(1) << __func__ << " cannot register SocketHook" << dendl; |
| 29 | + return; |
| 30 | + } |
| 31 | + r = admin_socket->register_command( |
| 32 | + "bluestore list " |
| 33 | + "name=collection,type=CephString,req=true " |
| 34 | + "name=start,type=CephString,req=false " |
| 35 | + "name=max,type=CephInt,req=false", |
| 36 | + this, |
| 37 | + "list objects in specific collection"); |
| 38 | + ceph_assert(r == 0); |
| 39 | + r = admin_socket->register_command( |
| 40 | + "bluestore onode metadata " |
| 41 | + "name=object_name,type=CephString,req=true", |
| 42 | + this, |
| 43 | + "print object internals"); |
| 44 | + ceph_assert(r == 0); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +BlueStore::SocketHook::~SocketHook() |
| 49 | +{ |
| 50 | + AdminSocket *admin_socket = store.cct->get_admin_socket(); |
| 51 | + if (admin_socket) { |
| 52 | + admin_socket->unregister_commands(this); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +int BlueStore::SocketHook::call( |
| 57 | + std::string_view command, |
| 58 | + const cmdmap_t& cmdmap, |
| 59 | + const bufferlist& inbl, |
| 60 | + Formatter *f, |
| 61 | + std::ostream& ss, |
| 62 | + bufferlist& out) |
| 63 | +{ |
| 64 | + int r = 0; |
| 65 | + if (command == "bluestore collections") { |
| 66 | + std::vector<coll_t> collections; |
| 67 | + store.list_collections(collections); |
| 68 | + std::stringstream result; |
| 69 | + for (const auto& c : collections) { |
| 70 | + result << c << std::endl; |
| 71 | + } |
| 72 | + out.append(result.str()); |
| 73 | + return 0; |
| 74 | + } else if (command == "bluestore list") { |
| 75 | + std::string coll; |
| 76 | + std::string start; |
| 77 | + int64_t max; |
| 78 | + cmd_getval(cmdmap, "collection", coll); |
| 79 | + cmd_getval(cmdmap, "start", start); |
| 80 | + if (!cmd_getval(cmdmap, "max", max)) { |
| 81 | + max = 100; |
| 82 | + } |
| 83 | + if (max == 0) { |
| 84 | + max = std::numeric_limits<int>::max(); |
| 85 | + } |
| 86 | + coll_t c; |
| 87 | + if (c.parse(coll) == false) { |
| 88 | + ss << "Cannot parse collection" << std::endl; |
| 89 | + return -EINVAL; |
| 90 | + } |
| 91 | + BlueStore::CollectionRef col = store._get_collection(c); |
| 92 | + if (!col) { |
| 93 | + ss << "No such collection" << std::endl; |
| 94 | + return -ENOENT; |
| 95 | + } |
| 96 | + ghobject_t start_object; |
| 97 | + if (start.length() > 0) { |
| 98 | + if (start_object.parse(start) == false) { |
| 99 | + ss << "Cannot parse start object"; |
| 100 | + return -EINVAL; |
| 101 | + } |
| 102 | + } |
| 103 | + std::vector<ghobject_t> list; |
| 104 | + { |
| 105 | + std::shared_lock l(col->lock); |
| 106 | + r = store._collection_list(col.get(), start_object, ghobject_t::get_max(), |
| 107 | + max, false, &list, nullptr); |
| 108 | + } |
| 109 | + if (r != 0) { |
| 110 | + return 0; |
| 111 | + } |
| 112 | + std::stringstream result; |
| 113 | + for (auto& obj : list) { |
| 114 | + result << obj << std::endl; |
| 115 | + } |
| 116 | + out.append(result.str()); |
| 117 | + return 0; |
| 118 | + } else if (command == "bluestore onode metadata") { |
| 119 | + std::string object_name; |
| 120 | + cmd_getval(cmdmap, "object_name", object_name); |
| 121 | + ghobject_t object; |
| 122 | + if (!object.parse(object_name)) { |
| 123 | + ss << "Cannot parse object" << std::endl; |
| 124 | + return -EINVAL; |
| 125 | + } |
| 126 | + std::shared_lock l(store.coll_lock); |
| 127 | + for (const auto& cp : store.coll_map) { |
| 128 | + if (cp.second->contains(object)) { |
| 129 | + std::shared_lock l(cp.second->lock); |
| 130 | + OnodeRef o = cp.second->get_onode(object, false); |
| 131 | + if (!o || !o->exists) { |
| 132 | + ss << "Object not found" << std::endl; |
| 133 | + return -ENOENT; |
| 134 | + } |
| 135 | + o->extent_map.fault_range(store.db, 0, 0xffffffff); |
| 136 | + using P = BlueStore::printer; |
| 137 | + std::stringstream result; |
| 138 | + result << o->print(P::PTR + P::DISK + P::USE + P::BUF + P::CHK + P::ATTRS) << std::endl; |
| 139 | + out.append(result.str()); |
| 140 | + return 0; |
| 141 | + } |
| 142 | + } |
| 143 | + r = -ENOENT; |
| 144 | + ss << "No collection that can hold such object" << std::endl; |
| 145 | + } else { |
| 146 | + ss << "Invalid command" << std::endl; |
| 147 | + r = -ENOSYS; |
| 148 | + } |
| 149 | + return r; |
| 150 | +} |
0 commit comments