1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Inktank, Inc
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
19 #include "mon/Monitor.h"
20 #include "mon/ConfigKeyService.h"
21 #include "mon/MonitorDBStore.h"
22 #include "mon/OSDMonitor.h"
23 #include "common/errno.h"
24 #include "include/stringify.h"
26 #define dout_subsys ceph_subsys_mon
28 #define dout_prefix _prefix(_dout, mon, this)
29 static ostream& _prefix(std::ostream *_dout, const Monitor *mon,
30 const ConfigKeyService *service) {
31 return *_dout << "mon." << mon->name << "@" << mon->rank
32 << "(" << mon->get_state_name() << ")." << service->get_name()
33 << "(" << service->get_epoch() << ") ";
36 const string ConfigKeyService::STORE_PREFIX = "mon_config_key";
38 int ConfigKeyService::store_get(const string &key, bufferlist &bl)
40 return mon->store->get(STORE_PREFIX, key, bl);
43 void ConfigKeyService::get_store_prefixes(set<string>& s)
45 s.insert(STORE_PREFIX);
48 void ConfigKeyService::store_put(const string &key, bufferlist &bl, Context *cb)
50 MonitorDBStore::TransactionRef t = paxos->get_pending_transaction();
51 t->put(STORE_PREFIX, key, bl);
53 paxos->queue_pending_finisher(cb);
54 paxos->trigger_propose();
57 void ConfigKeyService::store_delete(const string &key, Context *cb)
59 MonitorDBStore::TransactionRef t = paxos->get_pending_transaction();
62 paxos->queue_pending_finisher(cb);
63 paxos->trigger_propose();
66 void ConfigKeyService::store_delete(
67 MonitorDBStore::TransactionRef t,
70 t->erase(STORE_PREFIX, key);
73 bool ConfigKeyService::store_exists(const string &key)
75 return mon->store->exists(STORE_PREFIX, key);
78 void ConfigKeyService::store_list(stringstream &ss)
80 KeyValueDB::Iterator iter =
81 mon->store->get_iterator(STORE_PREFIX);
83 JSONFormatter f(true);
84 f.open_array_section("keys");
86 while (iter->valid()) {
87 string key(iter->key());
88 f.dump_string("key", key);
95 bool ConfigKeyService::store_has_prefix(const string &prefix)
97 KeyValueDB::Iterator iter =
98 mon->store->get_iterator(STORE_PREFIX);
100 while (iter->valid()) {
101 string key(iter->key());
102 size_t p = key.find(prefix);
103 if (p != string::npos && p == 0) {
111 void ConfigKeyService::store_dump(stringstream &ss)
113 KeyValueDB::Iterator iter =
114 mon->store->get_iterator(STORE_PREFIX);
116 JSONFormatter f(true);
117 f.open_object_section("config-key store");
119 while (iter->valid()) {
120 f.dump_string(iter->key().c_str(), iter->value().to_str());
127 void ConfigKeyService::store_delete_prefix(
128 MonitorDBStore::TransactionRef t,
129 const string &prefix)
131 KeyValueDB::Iterator iter =
132 mon->store->get_iterator(STORE_PREFIX);
134 while (iter->valid()) {
135 string key(iter->key());
137 size_t p = key.find(prefix);
138 if (p != string::npos && p == 0) {
139 store_delete(t, key);
145 bool ConfigKeyService::service_dispatch(MonOpRequestRef op)
147 Message *m = op->get_req();
149 dout(10) << __func__ << " " << *m << dendl;
152 dout(1) << __func__ << " not in quorum -- waiting" << dendl;
153 paxos->wait_for_readable(op, new Monitor::C_RetryMessage(mon, op));
157 assert(m->get_type() == MSG_MON_COMMAND);
159 MMonCommand *cmd = static_cast<MMonCommand*>(m);
161 assert(!cmd->cmd.empty());
168 map<string, cmd_vartype> cmdmap;
170 if (!cmdmap_from_json(cmd->cmd, &cmdmap, ss)) {
174 cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
176 cmd_getval(g_ceph_context, cmdmap, "key", key);
178 if (prefix == "config-key get") {
179 ret = store_get(key, rdata);
181 assert(!rdata.length());
182 ss << "error obtaining '" << key << "': " << cpp_strerror(ret);
185 ss << "obtained '" << key << "'";
187 } else if (prefix == "config-key put" ||
188 prefix == "config-key set") {
189 if (!mon->is_leader()) {
190 mon->forward_request_leader(op);
191 // we forward the message; so return now.
197 if (cmd_getval(g_ceph_context, cmdmap, "val", val)) {
198 // they specified a value in the command instead of a file
200 } else if (cmd->get_data_len() > 0) {
201 // they specified '-i <file>'
202 data = cmd->get_data();
204 if (data.length() > (size_t) g_conf->mon_config_key_max_entry_size) {
205 ret = -EFBIG; // File too large
206 ss << "error: entry size limited to "
207 << g_conf->mon_config_key_max_entry_size << " bytes. "
208 << "Use 'mon config key max entry size' to manually adjust";
211 // we'll reply to the message once the proposal has been handled
214 new Monitor::C_Command(mon, op, 0, ss.str(), 0));
215 // return for now; we'll put the message once it's done.
218 } else if (prefix == "config-key del" ||
219 prefix == "config-key rm") {
220 if (!mon->is_leader()) {
221 mon->forward_request_leader(op);
225 if (!store_exists(key)) {
227 ss << "no such key '" << key << "'";
230 store_delete(key, new Monitor::C_Command(mon, op, 0, "key deleted", 0));
231 // return for now; we'll put the message once it's done
234 } else if (prefix == "config-key exists") {
235 bool exists = store_exists(key);
236 ss << "key '" << key << "'";
241 ss << " doesn't exist";
245 } else if (prefix == "config-key list" ||
246 prefix == "config-key ls") {
249 rdata.append(tmp_ss);
252 } else if (prefix == "config-key dump") {
255 rdata.append(tmp_ss);
261 if (!cmd->get_source().is_mon()) {
262 string rs = ss.str();
263 mon->reply_command(op, ret, rs, rdata, 0);
269 string _get_dmcrypt_prefix(const uuid_d& uuid, const string k)
271 return "dm-crypt/osd/" + stringify(uuid) + "/" + k;
274 int ConfigKeyService::validate_osd_destroy(
278 string dmcrypt_prefix = _get_dmcrypt_prefix(uuid, "");
279 string daemon_prefix =
280 "daemon-private/osd." + stringify(id) + "/";
282 if (!store_has_prefix(dmcrypt_prefix) &&
283 !store_has_prefix(daemon_prefix)) {
289 void ConfigKeyService::do_osd_destroy(int32_t id, uuid_d& uuid)
291 string dmcrypt_prefix = _get_dmcrypt_prefix(uuid, "");
292 string daemon_prefix =
293 "daemon-private/osd." + stringify(id) + "/";
295 MonitorDBStore::TransactionRef t = paxos->get_pending_transaction();
296 for (auto p : { dmcrypt_prefix, daemon_prefix }) {
297 store_delete_prefix(t, p);
300 paxos->trigger_propose();
303 int ConfigKeyService::validate_osd_new(
305 const string& dmcrypt_key,
308 string dmcrypt_prefix = _get_dmcrypt_prefix(uuid, "luks");
310 value.append(dmcrypt_key);
312 if (store_exists(dmcrypt_prefix)) {
313 bufferlist existing_value;
314 int err = store_get(dmcrypt_prefix, existing_value);
316 dout(10) << __func__ << " unable to get dm-crypt key from store (r = "
317 << err << ")" << dendl;
320 if (existing_value.contents_equal(value)) {
321 // both values match; this will be an idempotent op.
324 ss << "dm-crypt key already exists and does not match";
330 void ConfigKeyService::do_osd_new(
332 const string& dmcrypt_key)
334 assert(paxos->is_plugged());
336 string dmcrypt_key_prefix = _get_dmcrypt_prefix(uuid, "luks");
337 bufferlist dmcrypt_key_value;
338 dmcrypt_key_value.append(dmcrypt_key);
339 // store_put() will call trigger_propose
340 store_put(dmcrypt_key_prefix, dmcrypt_key_value, nullptr);