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 if (!mon
->is_leader()) {
189 mon
->forward_request_leader(op
);
190 // we forward the message; so return now.
196 if (cmd_getval(g_ceph_context
, cmdmap
, "val", val
)) {
197 // they specified a value in the command instead of a file
199 } else if (cmd
->get_data_len() > 0) {
200 // they specified '-i <file>'
201 data
= cmd
->get_data();
203 if (data
.length() > (size_t) g_conf
->mon_config_key_max_entry_size
) {
204 ret
= -EFBIG
; // File too large
205 ss
<< "error: entry size limited to "
206 << g_conf
->mon_config_key_max_entry_size
<< " bytes. "
207 << "Use 'mon config key max entry size' to manually adjust";
210 // we'll reply to the message once the proposal has been handled
213 new Monitor::C_Command(mon
, op
, 0, ss
.str(), 0));
214 // return for now; we'll put the message once it's done.
217 } else if (prefix
== "config-key del" ||
218 prefix
== "config-key rm") {
219 if (!mon
->is_leader()) {
220 mon
->forward_request_leader(op
);
224 if (!store_exists(key
)) {
226 ss
<< "no such key '" << key
<< "'";
229 store_delete(key
, new Monitor::C_Command(mon
, op
, 0, "key deleted", 0));
230 // return for now; we'll put the message once it's done
233 } else if (prefix
== "config-key exists") {
234 bool exists
= store_exists(key
);
235 ss
<< "key '" << key
<< "'";
240 ss
<< " doesn't exist";
244 } else if (prefix
== "config-key list") {
247 rdata
.append(tmp_ss
);
250 } else if (prefix
== "config-key dump") {
253 rdata
.append(tmp_ss
);
259 if (!cmd
->get_source().is_mon()) {
260 string rs
= ss
.str();
261 mon
->reply_command(op
, ret
, rs
, rdata
, 0);
267 string
_get_dmcrypt_prefix(const uuid_d
& uuid
, const string k
)
269 return "dm-crypt/osd/" + stringify(uuid
) + "/" + k
;
272 int ConfigKeyService::validate_osd_destroy(
276 string dmcrypt_prefix
= _get_dmcrypt_prefix(uuid
, "");
277 string daemon_prefix
=
278 "daemon-private/osd." + stringify(id
) + "/";
280 if (!store_has_prefix(dmcrypt_prefix
) &&
281 !store_has_prefix(daemon_prefix
)) {
287 void ConfigKeyService::do_osd_destroy(int32_t id
, uuid_d
& uuid
)
289 string dmcrypt_prefix
= _get_dmcrypt_prefix(uuid
, "");
290 string daemon_prefix
=
291 "daemon-private/osd." + stringify(id
) + "/";
293 MonitorDBStore::TransactionRef t
= paxos
->get_pending_transaction();
294 for (auto p
: { dmcrypt_prefix
, daemon_prefix
}) {
295 store_delete_prefix(t
, p
);
298 paxos
->trigger_propose();
301 int ConfigKeyService::validate_osd_new(
303 const string
& dmcrypt_key
,
306 string dmcrypt_prefix
= _get_dmcrypt_prefix(uuid
, "luks");
308 value
.append(dmcrypt_key
);
310 if (store_exists(dmcrypt_prefix
)) {
311 bufferlist existing_value
;
312 int err
= store_get(dmcrypt_prefix
, existing_value
);
314 dout(10) << __func__
<< " unable to get dm-crypt key from store (r = "
315 << err
<< ")" << dendl
;
318 if (existing_value
.contents_equal(value
)) {
319 // both values match; this will be an idempotent op.
322 ss
<< "dm-crypt key already exists and does not match";
328 void ConfigKeyService::do_osd_new(
330 const string
& dmcrypt_key
)
332 assert(paxos
->is_plugged());
334 string dmcrypt_key_prefix
= _get_dmcrypt_prefix(uuid
, "luks");
335 bufferlist dmcrypt_key_value
;
336 dmcrypt_key_value
.append(dmcrypt_key
);
337 // store_put() will call trigger_propose
338 store_put(dmcrypt_key_prefix
, dmcrypt_key_value
, nullptr);