]> git.proxmox.com Git - ceph.git/blame - ceph/src/mon/OSDMonitor.cc
update sources to v12.1.4
[ceph.git] / ceph / src / mon / OSDMonitor.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 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 * Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
8 * Copyright (C) 2014 Red Hat <contact@redhat.com>
9 *
10 * Author: Loic Dachary <loic@dachary.org>
11 *
12 * This is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License version 2.1, as published by the Free Software
15 * Foundation. See file COPYING.
16 *
17 */
18
19#include <algorithm>
224ce89b
WB
20#include <boost/algorithm/string.hpp>
21#include <locale>
7c673cae
FG
22#include <sstream>
23
31f18b77
FG
24#include "mon/OSDMonitor.h"
25#include "mon/Monitor.h"
26#include "mon/MDSMonitor.h"
27#include "mon/PGMonitor.h"
28#include "mon/MgrStatMonitor.h"
29#include "mon/AuthMonitor.h"
30#include "mon/ConfigKeyService.h"
7c673cae 31
31f18b77
FG
32#include "mon/MonitorDBStore.h"
33#include "mon/Session.h"
7c673cae
FG
34
35#include "crush/CrushWrapper.h"
36#include "crush/CrushTester.h"
37#include "crush/CrushTreeDumper.h"
38
39#include "messages/MOSDBeacon.h"
40#include "messages/MOSDFailure.h"
41#include "messages/MOSDMarkMeDown.h"
42#include "messages/MOSDFull.h"
43#include "messages/MOSDMap.h"
44#include "messages/MMonGetOSDMap.h"
45#include "messages/MOSDBoot.h"
46#include "messages/MOSDAlive.h"
47#include "messages/MPoolOp.h"
48#include "messages/MPoolOpReply.h"
49#include "messages/MOSDPGCreate.h"
50#include "messages/MOSDPGCreated.h"
51#include "messages/MOSDPGTemp.h"
52#include "messages/MMonCommand.h"
53#include "messages/MRemoveSnaps.h"
54#include "messages/MOSDScrub.h"
55#include "messages/MRoute.h"
56
57#include "common/TextTable.h"
58#include "common/Timer.h"
59#include "common/ceph_argparse.h"
60#include "common/perf_counters.h"
61#include "common/strtol.h"
62
63#include "common/config.h"
64#include "common/errno.h"
65
66#include "erasure-code/ErasureCodePlugin.h"
67#include "compressor/Compressor.h"
68#include "common/Checksummer.h"
69
70#include "include/compat.h"
71#include "include/assert.h"
72#include "include/stringify.h"
73#include "include/util.h"
74#include "common/cmdparse.h"
75#include "include/str_list.h"
76#include "include/str_map.h"
224ce89b 77#include "include/scope_guard.h"
7c673cae
FG
78
79#include "json_spirit/json_spirit_reader.h"
80
c07f9fc5
FG
81#include <boost/algorithm/string/predicate.hpp>
82
7c673cae
FG
83#define dout_subsys ceph_subsys_mon
84#define OSD_PG_CREATING_PREFIX "osd_pg_creating"
85
c07f9fc5
FG
86namespace {
87
88const uint32_t MAX_POOL_APPLICATIONS = 4;
89const uint32_t MAX_POOL_APPLICATION_KEYS = 64;
90const uint32_t MAX_POOL_APPLICATION_LENGTH = 128;
91
92} // anonymous namespace
93
7c673cae
FG
94void LastEpochClean::Lec::report(ps_t ps, epoch_t last_epoch_clean)
95{
96 if (epoch_by_pg.size() <= ps) {
97 epoch_by_pg.resize(ps + 1, 0);
98 }
99 const auto old_lec = epoch_by_pg[ps];
100 if (old_lec >= last_epoch_clean) {
101 // stale lec
102 return;
103 }
104 epoch_by_pg[ps] = last_epoch_clean;
105 if (last_epoch_clean < floor) {
106 floor = last_epoch_clean;
107 } else if (last_epoch_clean > floor) {
108 if (old_lec == floor) {
109 // probably should increase floor?
110 auto new_floor = std::min_element(std::begin(epoch_by_pg),
111 std::end(epoch_by_pg));
112 floor = *new_floor;
113 }
114 }
115 if (ps != next_missing) {
116 return;
117 }
118 for (; next_missing < epoch_by_pg.size(); next_missing++) {
119 if (epoch_by_pg[next_missing] == 0) {
120 break;
121 }
122 }
123}
124
125void LastEpochClean::remove_pool(uint64_t pool)
126{
127 report_by_pool.erase(pool);
128}
129
130void LastEpochClean::report(const pg_t& pg, epoch_t last_epoch_clean)
131{
132 auto& lec = report_by_pool[pg.pool()];
133 return lec.report(pg.ps(), last_epoch_clean);
134}
135
136epoch_t LastEpochClean::get_lower_bound(const OSDMap& latest) const
137{
138 auto floor = latest.get_epoch();
139 for (auto& pool : latest.get_pools()) {
140 auto reported = report_by_pool.find(pool.first);
141 if (reported == report_by_pool.end()) {
142 return 0;
143 }
144 if (reported->second.next_missing < pool.second.get_pg_num()) {
145 return 0;
146 }
147 if (reported->second.floor < floor) {
148 floor = reported->second.floor;
149 }
150 }
151 return floor;
152}
153
154
155struct C_UpdateCreatingPGs : public Context {
156 OSDMonitor *osdmon;
157 utime_t start;
158 epoch_t epoch;
159 C_UpdateCreatingPGs(OSDMonitor *osdmon, epoch_t e) :
160 osdmon(osdmon), start(ceph_clock_now()), epoch(e) {}
161 void finish(int r) override {
162 if (r >= 0) {
163 utime_t end = ceph_clock_now();
164 dout(10) << "osdmap epoch " << epoch << " mapping took "
165 << (end - start) << " seconds" << dendl;
166 osdmon->update_creating_pgs();
167 osdmon->check_pg_creates_subs();
168 }
169 }
170};
171
172#undef dout_prefix
173#define dout_prefix _prefix(_dout, mon, osdmap)
174static ostream& _prefix(std::ostream *_dout, Monitor *mon, const OSDMap& osdmap) {
175 return *_dout << "mon." << mon->name << "@" << mon->rank
176 << "(" << mon->get_state_name()
177 << ").osd e" << osdmap.get_epoch() << " ";
178}
179
180OSDMonitor::OSDMonitor(
181 CephContext *cct,
182 Monitor *mn,
183 Paxos *p,
184 const string& service_name)
185 : PaxosService(mn, p, service_name),
186 cct(cct),
187 inc_osd_cache(g_conf->mon_osd_cache_size),
188 full_osd_cache(g_conf->mon_osd_cache_size),
189 last_attempted_minwait_time(utime_t()),
190 mapper(mn->cct, &mn->cpu_tp),
191 op_tracker(cct, true, 1)
192{}
193
194bool OSDMonitor::_have_pending_crush()
195{
196 return pending_inc.crush.length() > 0;
197}
198
199CrushWrapper &OSDMonitor::_get_stable_crush()
200{
201 return *osdmap.crush;
202}
203
204void OSDMonitor::_get_pending_crush(CrushWrapper& newcrush)
205{
206 bufferlist bl;
207 if (pending_inc.crush.length())
208 bl = pending_inc.crush;
209 else
210 osdmap.crush->encode(bl, CEPH_FEATURES_SUPPORTED_DEFAULT);
211
212 bufferlist::iterator p = bl.begin();
213 newcrush.decode(p);
214}
215
216void OSDMonitor::create_initial()
217{
218 dout(10) << "create_initial for " << mon->monmap->fsid << dendl;
219
220 OSDMap newmap;
221
222 bufferlist bl;
223 mon->store->get("mkfs", "osdmap", bl);
224
225 if (bl.length()) {
226 newmap.decode(bl);
227 newmap.set_fsid(mon->monmap->fsid);
228 } else {
224ce89b 229 newmap.build_simple(g_ceph_context, 0, mon->monmap->fsid, 0);
7c673cae
FG
230 }
231 newmap.set_epoch(1);
232 newmap.created = newmap.modified = ceph_clock_now();
233
234 // new clusters should sort bitwise by default.
235 newmap.set_flag(CEPH_OSDMAP_SORTBITWISE);
236
237 // new cluster should require latest by default
31f18b77
FG
238 if (g_conf->mon_debug_no_require_luminous) {
239 newmap.require_osd_release = CEPH_RELEASE_KRAKEN;
240 derr << __func__ << " mon_debug_no_require_luminous=true" << dendl;
241 } else {
242 newmap.require_osd_release = CEPH_RELEASE_LUMINOUS;
c07f9fc5 243 newmap.flags |= CEPH_OSDMAP_RECOVERY_DELETES;
7c673cae
FG
244 newmap.full_ratio = g_conf->mon_osd_full_ratio;
245 if (newmap.full_ratio > 1.0) newmap.full_ratio /= 100;
246 newmap.backfillfull_ratio = g_conf->mon_osd_backfillfull_ratio;
247 if (newmap.backfillfull_ratio > 1.0) newmap.backfillfull_ratio /= 100;
248 newmap.nearfull_ratio = g_conf->mon_osd_nearfull_ratio;
249 if (newmap.nearfull_ratio > 1.0) newmap.nearfull_ratio /= 100;
31f18b77
FG
250 int r = ceph_release_from_name(
251 g_conf->mon_osd_initial_require_min_compat_client.c_str());
252 if (r <= 0) {
253 assert(0 == "mon_osd_initial_require_min_compat_client is not valid");
254 }
255 newmap.require_min_compat_client = r;
7c673cae
FG
256 }
257
258 // encode into pending incremental
259 newmap.encode(pending_inc.fullmap,
260 mon->get_quorum_con_features() | CEPH_FEATURE_RESERVED);
261 pending_inc.full_crc = newmap.get_crc();
262 dout(20) << " full crc " << pending_inc.full_crc << dendl;
263}
264
265void OSDMonitor::get_store_prefixes(std::set<string>& s)
266{
267 s.insert(service_name);
268 s.insert(OSD_PG_CREATING_PREFIX);
269}
270
271void OSDMonitor::update_from_paxos(bool *need_bootstrap)
272{
273 version_t version = get_last_committed();
274 if (version == osdmap.epoch)
275 return;
276 assert(version > osdmap.epoch);
277
278 dout(15) << "update_from_paxos paxos e " << version
279 << ", my e " << osdmap.epoch << dendl;
280
31f18b77
FG
281 if (mapping_job) {
282 if (!mapping_job->is_done()) {
283 dout(1) << __func__ << " mapping job "
284 << mapping_job.get() << " did not complete, "
285 << mapping_job->shards << " left, canceling" << dendl;
286 mapping_job->abort();
287 }
288 mapping_job.reset();
289 }
7c673cae 290
224ce89b
WB
291 load_health();
292
7c673cae
FG
293 /*
294 * We will possibly have a stashed latest that *we* wrote, and we will
295 * always be sure to have the oldest full map in the first..last range
296 * due to encode_trim_extra(), which includes the oldest full map in the trim
297 * transaction.
298 *
299 * encode_trim_extra() does not however write the full map's
300 * version to 'full_latest'. This is only done when we are building the
301 * full maps from the incremental versions. But don't panic! We make sure
302 * that the following conditions find whichever full map version is newer.
303 */
304 version_t latest_full = get_version_latest_full();
305 if (latest_full == 0 && get_first_committed() > 1)
306 latest_full = get_first_committed();
307
308 if (get_first_committed() > 1 &&
309 latest_full < get_first_committed()) {
310 // the monitor could be just sync'ed with its peer, and the latest_full key
311 // is not encoded in the paxos commits in encode_pending(), so we need to
312 // make sure we get it pointing to a proper version.
313 version_t lc = get_last_committed();
314 version_t fc = get_first_committed();
315
316 dout(10) << __func__ << " looking for valid full map in interval"
317 << " [" << fc << ", " << lc << "]" << dendl;
318
319 latest_full = 0;
320 for (version_t v = lc; v >= fc; v--) {
321 string full_key = "full_" + stringify(v);
322 if (mon->store->exists(get_service_name(), full_key)) {
323 dout(10) << __func__ << " found latest full map v " << v << dendl;
324 latest_full = v;
325 break;
326 }
327 }
328
329 assert(latest_full > 0);
330 auto t(std::make_shared<MonitorDBStore::Transaction>());
331 put_version_latest_full(t, latest_full);
332 mon->store->apply_transaction(t);
333 dout(10) << __func__ << " updated the on-disk full map version to "
334 << latest_full << dendl;
335 }
336
337 if ((latest_full > 0) && (latest_full > osdmap.epoch)) {
338 bufferlist latest_bl;
339 get_version_full(latest_full, latest_bl);
340 assert(latest_bl.length() != 0);
341 dout(7) << __func__ << " loading latest full map e" << latest_full << dendl;
342 osdmap.decode(latest_bl);
343 }
344
345 if (mon->monmap->get_required_features().contains_all(
346 ceph::features::mon::FEATURE_LUMINOUS)) {
347 bufferlist bl;
348 if (!mon->store->get(OSD_PG_CREATING_PREFIX, "creating", bl)) {
349 auto p = bl.begin();
350 std::lock_guard<std::mutex> l(creating_pgs_lock);
351 creating_pgs.decode(p);
352 dout(7) << __func__ << " loading creating_pgs last_scan_epoch "
353 << creating_pgs.last_scan_epoch
354 << " with " << creating_pgs.pgs.size() << " pgs" << dendl;
355 } else {
356 dout(1) << __func__ << " missing creating pgs; upgrade from post-kraken?"
357 << dendl;
358 }
359 }
360
31f18b77
FG
361 // make sure we're using the right pg service.. remove me post-luminous!
362 if (osdmap.require_osd_release >= CEPH_RELEASE_LUMINOUS) {
363 dout(10) << __func__ << " pgservice is mgrstat" << dendl;
364 mon->pgservice = mon->mgrstatmon()->get_pg_stat_service();
365 } else {
366 dout(10) << __func__ << " pgservice is pg" << dendl;
367 mon->pgservice = mon->pgmon()->get_pg_stat_service();
368 }
369
7c673cae
FG
370 // walk through incrementals
371 MonitorDBStore::TransactionRef t;
372 size_t tx_size = 0;
373 while (version > osdmap.epoch) {
374 bufferlist inc_bl;
375 int err = get_version(osdmap.epoch+1, inc_bl);
376 assert(err == 0);
377 assert(inc_bl.length());
378
379 dout(7) << "update_from_paxos applying incremental " << osdmap.epoch+1
380 << dendl;
381 OSDMap::Incremental inc(inc_bl);
382 err = osdmap.apply_incremental(inc);
383 assert(err == 0);
384
385 if (!t)
386 t.reset(new MonitorDBStore::Transaction);
387
388 // Write out the full map for all past epochs. Encode the full
389 // map with the same features as the incremental. If we don't
390 // know, use the quorum features. If we don't know those either,
391 // encode with all features.
392 uint64_t f = inc.encode_features;
393 if (!f)
394 f = mon->get_quorum_con_features();
395 if (!f)
396 f = -1;
397 bufferlist full_bl;
398 osdmap.encode(full_bl, f | CEPH_FEATURE_RESERVED);
399 tx_size += full_bl.length();
400
401 bufferlist orig_full_bl;
402 get_version_full(osdmap.epoch, orig_full_bl);
403 if (orig_full_bl.length()) {
404 // the primary provided the full map
405 assert(inc.have_crc);
406 if (inc.full_crc != osdmap.crc) {
407 // This will happen if the mons were running mixed versions in
408 // the past or some other circumstance made the full encoded
409 // maps divergent. Reloading here will bring us back into
410 // sync with the primary for this and all future maps. OSDs
411 // will also be brought back into sync when they discover the
412 // crc mismatch and request a full map from a mon.
413 derr << __func__ << " full map CRC mismatch, resetting to canonical"
414 << dendl;
415 osdmap = OSDMap();
416 osdmap.decode(orig_full_bl);
417 }
418 } else {
419 assert(!inc.have_crc);
420 put_version_full(t, osdmap.epoch, full_bl);
421 }
422 put_version_latest_full(t, osdmap.epoch);
423
424 // share
425 dout(1) << osdmap << dendl;
426
427 if (osdmap.epoch == 1) {
428 t->erase("mkfs", "osdmap");
429 }
430
31f18b77
FG
431 // make sure we're using the right pg service.. remove me post-luminous!
432 if (osdmap.require_osd_release >= CEPH_RELEASE_LUMINOUS) {
433 dout(10) << __func__ << " pgservice is mgrstat" << dendl;
434 mon->pgservice = mon->mgrstatmon()->get_pg_stat_service();
435 } else {
436 dout(10) << __func__ << " pgservice is pg" << dendl;
437 mon->pgservice = mon->pgmon()->get_pg_stat_service();
438 }
439
7c673cae
FG
440 if (tx_size > g_conf->mon_sync_max_payload_size*2) {
441 mon->store->apply_transaction(t);
442 t = MonitorDBStore::TransactionRef();
443 tx_size = 0;
444 }
445 if (mon->monmap->get_required_features().contains_all(
446 ceph::features::mon::FEATURE_LUMINOUS)) {
7c673cae
FG
447 for (const auto &osd_state : inc.new_state) {
448 if (osd_state.second & CEPH_OSD_UP) {
449 // could be marked up *or* down, but we're too lazy to check which
450 last_osd_report.erase(osd_state.first);
451 }
452 if (osd_state.second & CEPH_OSD_EXISTS) {
453 // could be created *or* destroyed, but we can safely drop it
454 osd_epochs.erase(osd_state.first);
455 }
456 }
457 }
458 }
459
460 if (t) {
461 mon->store->apply_transaction(t);
462 }
463
464 for (int o = 0; o < osdmap.get_max_osd(); o++) {
465 if (osdmap.is_out(o))
466 continue;
467 auto found = down_pending_out.find(o);
468 if (osdmap.is_down(o)) {
469 // populate down -> out map
470 if (found == down_pending_out.end()) {
471 dout(10) << " adding osd." << o << " to down_pending_out map" << dendl;
472 down_pending_out[o] = ceph_clock_now();
473 }
474 } else {
475 if (found != down_pending_out.end()) {
476 dout(10) << " removing osd." << o << " from down_pending_out map" << dendl;
477 down_pending_out.erase(found);
478 }
479 }
480 }
481 // XXX: need to trim MonSession connected with a osd whose id > max_osd?
482
483 if (mon->is_leader()) {
484 // kick pgmon, make sure it's seen the latest map
485 mon->pgmon()->check_osd_map(osdmap.epoch);
486 }
487
488 check_osdmap_subs();
489 check_pg_creates_subs();
490
491 share_map_with_random_osd();
492 update_logger();
493
494 process_failures();
495
496 // make sure our feature bits reflect the latest map
497 update_msgr_features();
498
499 if (!mon->is_leader()) {
500 // will be called by on_active() on the leader, avoid doing so twice
501 start_mapping();
502 }
503}
504
505void OSDMonitor::start_mapping()
506{
507 // initiate mapping job
508 if (mapping_job) {
509 dout(10) << __func__ << " canceling previous mapping_job " << mapping_job.get()
510 << dendl;
511 mapping_job->abort();
512 }
224ce89b
WB
513 if (!osdmap.get_pools().empty()) {
514 auto fin = new C_UpdateCreatingPGs(this, osdmap.get_epoch());
515 mapping_job = mapping.start_update(osdmap, mapper,
516 g_conf->mon_osd_mapping_pgs_per_chunk);
517 dout(10) << __func__ << " started mapping job " << mapping_job.get()
518 << " at " << fin->start << dendl;
519 mapping_job->set_finish_event(fin);
520 } else {
521 dout(10) << __func__ << " no pools, no mapping job" << dendl;
522 mapping_job = nullptr;
523 }
7c673cae
FG
524}
525
526void OSDMonitor::update_msgr_features()
527{
528 set<int> types;
529 types.insert((int)entity_name_t::TYPE_OSD);
530 types.insert((int)entity_name_t::TYPE_CLIENT);
531 types.insert((int)entity_name_t::TYPE_MDS);
532 types.insert((int)entity_name_t::TYPE_MON);
533 for (set<int>::iterator q = types.begin(); q != types.end(); ++q) {
534 uint64_t mask;
535 uint64_t features = osdmap.get_features(*q, &mask);
536 if ((mon->messenger->get_policy(*q).features_required & mask) != features) {
537 dout(0) << "crush map has features " << features << ", adjusting msgr requires" << dendl;
538 Messenger::Policy p = mon->messenger->get_policy(*q);
539 p.features_required = (p.features_required & ~mask) | features;
540 mon->messenger->set_policy(*q, p);
541 }
542 }
543}
544
545void OSDMonitor::on_active()
546{
547 update_logger();
548
549 if (mon->is_leader()) {
224ce89b 550 mon->clog->debug() << "osdmap " << osdmap;
7c673cae
FG
551 } else {
552 list<MonOpRequestRef> ls;
553 take_all_failures(ls);
554 while (!ls.empty()) {
555 MonOpRequestRef op = ls.front();
556 op->mark_osdmon_event(__func__);
557 dispatch(op);
558 ls.pop_front();
559 }
560 }
561 start_mapping();
562}
563
564void OSDMonitor::on_restart()
565{
566 last_osd_report.clear();
31f18b77
FG
567
568 if (mon->is_leader()) {
569 // fix ruleset != ruleid
570 if (osdmap.crush->has_legacy_rulesets() &&
571 !osdmap.crush->has_multirule_rulesets()) {
572 CrushWrapper newcrush;
573 _get_pending_crush(newcrush);
574 int r = newcrush.renumber_rules_by_ruleset();
575 if (r >= 0) {
576 dout(1) << __func__ << " crush map has ruleset != rule id; fixing" << dendl;
577 pending_inc.crush.clear();
578 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
579 } else {
580 dout(10) << __func__ << " unable to renumber rules by ruleset" << dendl;
581 }
582 }
583 }
7c673cae
FG
584}
585
586void OSDMonitor::on_shutdown()
587{
588 dout(10) << __func__ << dendl;
589 if (mapping_job) {
590 dout(10) << __func__ << " canceling previous mapping_job " << mapping_job.get()
591 << dendl;
592 mapping_job->abort();
593 }
594
595 // discard failure info, waiters
596 list<MonOpRequestRef> ls;
597 take_all_failures(ls);
598 ls.clear();
599}
600
601void OSDMonitor::update_logger()
602{
603 dout(10) << "update_logger" << dendl;
604
605 mon->cluster_logger->set(l_cluster_num_osd, osdmap.get_num_osds());
606 mon->cluster_logger->set(l_cluster_num_osd_up, osdmap.get_num_up_osds());
607 mon->cluster_logger->set(l_cluster_num_osd_in, osdmap.get_num_in_osds());
608 mon->cluster_logger->set(l_cluster_osd_epoch, osdmap.get_epoch());
609}
610
7c673cae
FG
611void OSDMonitor::create_pending()
612{
613 pending_inc = OSDMap::Incremental(osdmap.epoch+1);
614 pending_inc.fsid = mon->monmap->fsid;
615
616 dout(10) << "create_pending e " << pending_inc.epoch << dendl;
617
618 // clean up pg_temp, primary_temp
619 OSDMap::clean_temps(g_ceph_context, osdmap, &pending_inc);
620 dout(10) << "create_pending did clean_temps" << dendl;
621
622 // On upgrade OSDMap has new field set by mon_osd_backfillfull_ratio config
623 // instead of osd_backfill_full_ratio config
624 if (osdmap.backfillfull_ratio <= 0) {
625 pending_inc.new_backfillfull_ratio = g_conf->mon_osd_backfillfull_ratio;
626 if (pending_inc.new_backfillfull_ratio > 1.0)
627 pending_inc.new_backfillfull_ratio /= 100;
628 dout(1) << __func__ << " setting backfillfull_ratio = "
629 << pending_inc.new_backfillfull_ratio << dendl;
630 }
31f18b77
FG
631 if (osdmap.get_epoch() > 0 &&
632 osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
7c673cae 633 // transition full ratios from PGMap to OSDMap (on upgrade)
31f18b77
FG
634 float full_ratio = mon->pgservice->get_full_ratio();
635 float nearfull_ratio = mon->pgservice->get_nearfull_ratio();
636 if (osdmap.full_ratio != full_ratio) {
7c673cae 637 dout(10) << __func__ << " full_ratio " << osdmap.full_ratio
31f18b77
FG
638 << " -> " << full_ratio << " (from pgmap)" << dendl;
639 pending_inc.new_full_ratio = full_ratio;
7c673cae 640 }
31f18b77 641 if (osdmap.nearfull_ratio != nearfull_ratio) {
7c673cae 642 dout(10) << __func__ << " nearfull_ratio " << osdmap.nearfull_ratio
31f18b77
FG
643 << " -> " << nearfull_ratio << " (from pgmap)" << dendl;
644 pending_inc.new_nearfull_ratio = nearfull_ratio;
7c673cae
FG
645 }
646 } else {
647 // safety check (this shouldn't really happen)
648 if (osdmap.full_ratio <= 0) {
649 pending_inc.new_full_ratio = g_conf->mon_osd_full_ratio;
650 if (pending_inc.new_full_ratio > 1.0)
651 pending_inc.new_full_ratio /= 100;
652 dout(1) << __func__ << " setting full_ratio = "
653 << pending_inc.new_full_ratio << dendl;
654 }
655 if (osdmap.nearfull_ratio <= 0) {
656 pending_inc.new_nearfull_ratio = g_conf->mon_osd_nearfull_ratio;
657 if (pending_inc.new_nearfull_ratio > 1.0)
658 pending_inc.new_nearfull_ratio /= 100;
659 dout(1) << __func__ << " setting nearfull_ratio = "
660 << pending_inc.new_nearfull_ratio << dendl;
661 }
662 }
663}
664
665creating_pgs_t
666OSDMonitor::update_pending_pgs(const OSDMap::Incremental& inc)
667{
31f18b77 668 dout(10) << __func__ << dendl;
7c673cae
FG
669 creating_pgs_t pending_creatings;
670 {
671 std::lock_guard<std::mutex> l(creating_pgs_lock);
672 pending_creatings = creating_pgs;
673 }
31f18b77
FG
674 // check for new or old pools
675 if (pending_creatings.last_scan_epoch < inc.epoch) {
676 if (osdmap.get_epoch() &&
677 osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
678 auto added =
679 mon->pgservice->maybe_add_creating_pgs(creating_pgs.last_scan_epoch,
680 osdmap.get_pools(),
681 &pending_creatings);
682 dout(7) << __func__ << " " << added << " pgs added from pgmap" << dendl;
683 }
684 unsigned queued = 0;
685 queued += scan_for_creating_pgs(osdmap.get_pools(),
686 inc.old_pools,
687 inc.modified,
688 &pending_creatings);
689 queued += scan_for_creating_pgs(inc.new_pools,
690 inc.old_pools,
691 inc.modified,
692 &pending_creatings);
693 dout(10) << __func__ << " " << queued << " pools queued" << dendl;
694 for (auto deleted_pool : inc.old_pools) {
695 auto removed = pending_creatings.remove_pool(deleted_pool);
696 dout(10) << __func__ << " " << removed
697 << " pg removed because containing pool deleted: "
698 << deleted_pool << dendl;
699 last_epoch_clean.remove_pool(deleted_pool);
700 }
701 // pgmon updates its creating_pgs in check_osd_map() which is called by
702 // on_active() and check_osd_map() could be delayed if lease expires, so its
703 // creating_pgs could be stale in comparison with the one of osdmon. let's
704 // trim them here. otherwise, they will be added back after being erased.
705 unsigned removed = 0;
706 for (auto& pg : pending_created_pgs) {
707 dout(20) << __func__ << " noting created pg " << pg << dendl;
708 pending_creatings.created_pools.insert(pg.pool());
709 removed += pending_creatings.pgs.erase(pg);
710 }
711 pending_created_pgs.clear();
712 dout(10) << __func__ << " " << removed
713 << " pgs removed because they're created" << dendl;
714 pending_creatings.last_scan_epoch = osdmap.get_epoch();
715 }
716
717 // process queue
718 unsigned max = MAX(1, g_conf->mon_osd_max_creating_pgs);
719 const auto total = pending_creatings.pgs.size();
720 while (pending_creatings.pgs.size() < max &&
721 !pending_creatings.queue.empty()) {
722 auto p = pending_creatings.queue.begin();
723 int64_t poolid = p->first;
724 dout(10) << __func__ << " pool " << poolid
725 << " created " << p->second.created
726 << " modified " << p->second.modified
727 << " [" << p->second.start << "-" << p->second.end << ")"
728 << dendl;
729 int n = MIN(max - pending_creatings.pgs.size(),
730 p->second.end - p->second.start);
731 ps_t first = p->second.start;
732 ps_t end = first + n;
733 for (ps_t ps = first; ps < end; ++ps) {
734 const pg_t pgid{ps, static_cast<uint64_t>(poolid)};
735 // NOTE: use the *current* epoch as the PG creation epoch so that the
736 // OSD does not have to generate a long set of PastIntervals.
737 pending_creatings.pgs.emplace(pgid, make_pair(inc.epoch,
738 p->second.modified));
739 dout(10) << __func__ << " adding " << pgid << dendl;
740 }
741 p->second.start = end;
742 if (p->second.done()) {
743 dout(10) << __func__ << " done with queue for " << poolid << dendl;
744 pending_creatings.queue.erase(p);
745 } else {
746 dout(10) << __func__ << " pool " << poolid
747 << " now [" << p->second.start << "-" << p->second.end << ")"
748 << dendl;
749 }
750 }
751 dout(10) << __func__ << " queue remaining: " << pending_creatings.queue.size()
752 << " pools" << dendl;
c07f9fc5
FG
753 dout(10) << __func__
754 << " " << (pending_creatings.pgs.size() - total)
755 << "/" << pending_creatings.pgs.size()
31f18b77 756 << " pgs added from queued pools" << dendl;
7c673cae
FG
757 return pending_creatings;
758}
759
760void OSDMonitor::maybe_prime_pg_temp()
761{
762 bool all = false;
763 if (pending_inc.crush.length()) {
764 dout(10) << __func__ << " new crush map, all" << dendl;
765 all = true;
766 }
767
768 if (!pending_inc.new_up_client.empty()) {
769 dout(10) << __func__ << " new up osds, all" << dendl;
770 all = true;
771 }
772
773 // check for interesting OSDs
774 set<int> osds;
31f18b77 775 for (auto p = pending_inc.new_state.begin();
7c673cae
FG
776 !all && p != pending_inc.new_state.end();
777 ++p) {
778 if ((p->second & CEPH_OSD_UP) &&
779 osdmap.is_up(p->first)) {
780 osds.insert(p->first);
781 }
782 }
783 for (map<int32_t,uint32_t>::iterator p = pending_inc.new_weight.begin();
784 !all && p != pending_inc.new_weight.end();
785 ++p) {
786 if (p->second < osdmap.get_weight(p->first)) {
787 // weight reduction
788 osds.insert(p->first);
789 } else {
790 dout(10) << __func__ << " osd." << p->first << " weight increase, all"
791 << dendl;
792 all = true;
793 }
794 }
795
796 if (!all && osds.empty())
797 return;
798
799 if (!all) {
800 unsigned estimate =
801 mapping.get_osd_acting_pgs(*osds.begin()).size() * osds.size();
802 if (estimate > mapping.get_num_pgs() *
803 g_conf->mon_osd_prime_pg_temp_max_estimate) {
804 dout(10) << __func__ << " estimate " << estimate << " pgs on "
805 << osds.size() << " osds >= "
806 << g_conf->mon_osd_prime_pg_temp_max_estimate << " of total "
807 << mapping.get_num_pgs() << " pgs, all"
808 << dendl;
809 all = true;
810 } else {
811 dout(10) << __func__ << " estimate " << estimate << " pgs on "
812 << osds.size() << " osds" << dendl;
813 }
814 }
815
816 OSDMap next;
817 next.deepish_copy_from(osdmap);
818 next.apply_incremental(pending_inc);
819
224ce89b
WB
820 if (next.get_pools().empty()) {
821 dout(10) << __func__ << " no pools, no pg_temp priming" << dendl;
822 } else if (all) {
7c673cae
FG
823 PrimeTempJob job(next, this);
824 mapper.queue(&job, g_conf->mon_osd_mapping_pgs_per_chunk);
825 if (job.wait_for(g_conf->mon_osd_prime_pg_temp_max_time)) {
826 dout(10) << __func__ << " done in " << job.get_duration() << dendl;
827 } else {
828 dout(10) << __func__ << " did not finish in "
829 << g_conf->mon_osd_prime_pg_temp_max_time
830 << ", stopping" << dendl;
831 job.abort();
832 }
833 } else {
834 dout(10) << __func__ << " " << osds.size() << " interesting osds" << dendl;
835 utime_t stop = ceph_clock_now();
836 stop += g_conf->mon_osd_prime_pg_temp_max_time;
837 const int chunk = 1000;
838 int n = chunk;
839 std::unordered_set<pg_t> did_pgs;
840 for (auto osd : osds) {
841 auto& pgs = mapping.get_osd_acting_pgs(osd);
842 dout(20) << __func__ << " osd." << osd << " " << pgs << dendl;
843 for (auto pgid : pgs) {
844 if (!did_pgs.insert(pgid).second) {
845 continue;
846 }
847 prime_pg_temp(next, pgid);
848 if (--n <= 0) {
849 n = chunk;
850 if (ceph_clock_now() > stop) {
851 dout(10) << __func__ << " consumed more than "
852 << g_conf->mon_osd_prime_pg_temp_max_time
853 << " seconds, stopping"
854 << dendl;
855 return;
856 }
857 }
858 }
859 }
860 }
861}
862
863void OSDMonitor::prime_pg_temp(
864 const OSDMap& next,
865 pg_t pgid)
866{
867 if (mon->monmap->get_required_features().contains_all(
868 ceph::features::mon::FEATURE_LUMINOUS)) {
31f18b77 869 // TODO: remove this creating_pgs direct access?
7c673cae
FG
870 if (creating_pgs.pgs.count(pgid)) {
871 return;
872 }
873 } else {
31f18b77 874 if (mon->pgservice->is_creating_pg(pgid)) {
7c673cae
FG
875 return;
876 }
877 }
878 if (!osdmap.pg_exists(pgid)) {
879 return;
880 }
881
882 vector<int> up, acting;
883 mapping.get(pgid, &up, nullptr, &acting, nullptr);
884
885 vector<int> next_up, next_acting;
886 int next_up_primary, next_acting_primary;
887 next.pg_to_up_acting_osds(pgid, &next_up, &next_up_primary,
888 &next_acting, &next_acting_primary);
c07f9fc5 889 if (acting == next_acting && next_up != next_acting)
7c673cae
FG
890 return; // no change since last epoch
891
892 if (acting.empty())
893 return; // if previously empty now we can be no worse off
894 const pg_pool_t *pool = next.get_pg_pool(pgid.pool());
895 if (pool && acting.size() < pool->min_size)
896 return; // can be no worse off than before
897
c07f9fc5
FG
898 if (next_up == next_acting) {
899 acting.clear();
900 dout(20) << __func__ << "next_up === next_acting now, clear pg_temp"
901 << dendl;
902 }
903
7c673cae
FG
904 dout(20) << __func__ << " " << pgid << " " << up << "/" << acting
905 << " -> " << next_up << "/" << next_acting
906 << ", priming " << acting
907 << dendl;
908 {
909 Mutex::Locker l(prime_pg_temp_lock);
910 // do not touch a mapping if a change is pending
911 pending_inc.new_pg_temp.emplace(
912 pgid,
913 mempool::osdmap::vector<int>(acting.begin(), acting.end()));
914 }
915}
916
917/**
918 * @note receiving a transaction in this function gives a fair amount of
919 * freedom to the service implementation if it does need it. It shouldn't.
920 */
921void OSDMonitor::encode_pending(MonitorDBStore::TransactionRef t)
922{
923 dout(10) << "encode_pending e " << pending_inc.epoch
924 << dendl;
925
926 // finalize up pending_inc
927 pending_inc.modified = ceph_clock_now();
928
929 int r = pending_inc.propagate_snaps_to_tiers(g_ceph_context, osdmap);
930 assert(r == 0);
931
932 if (mapping_job) {
933 if (!mapping_job->is_done()) {
934 dout(1) << __func__ << " skipping prime_pg_temp; mapping job "
935 << mapping_job.get() << " did not complete, "
936 << mapping_job->shards << " left" << dendl;
937 mapping_job->abort();
938 } else if (mapping.get_epoch() < osdmap.get_epoch()) {
939 dout(1) << __func__ << " skipping prime_pg_temp; mapping job "
940 << mapping_job.get() << " is prior epoch "
941 << mapping.get_epoch() << dendl;
942 } else {
943 if (g_conf->mon_osd_prime_pg_temp) {
944 maybe_prime_pg_temp();
945 }
946 }
947 } else if (g_conf->mon_osd_prime_pg_temp) {
948 dout(1) << __func__ << " skipping prime_pg_temp; mapping job did not start"
949 << dendl;
950 }
951 mapping_job.reset();
952
c07f9fc5
FG
953 // ensure we don't have blank new_state updates. these are interrpeted as
954 // CEPH_OSD_UP (and almost certainly not what we want!).
955 auto p = pending_inc.new_state.begin();
956 while (p != pending_inc.new_state.end()) {
957 if (p->second == 0) {
958 dout(10) << "new_state for osd." << p->first << " is 0, removing" << dendl;
959 p = pending_inc.new_state.erase(p);
960 } else {
961 ++p;
962 }
963 }
964
7c673cae
FG
965 bufferlist bl;
966
967 {
968 OSDMap tmp;
969 tmp.deepish_copy_from(osdmap);
970 tmp.apply_incremental(pending_inc);
971
31f18b77 972 if (tmp.require_osd_release >= CEPH_RELEASE_LUMINOUS) {
7c673cae
FG
973 // set or clear full/nearfull?
974 int full, backfill, nearfull;
975 tmp.count_full_nearfull_osds(&full, &backfill, &nearfull);
976 if (full > 0) {
977 if (!tmp.test_flag(CEPH_OSDMAP_FULL)) {
978 dout(10) << __func__ << " setting full flag" << dendl;
979 add_flag(CEPH_OSDMAP_FULL);
980 remove_flag(CEPH_OSDMAP_NEARFULL);
981 }
982 } else {
983 if (tmp.test_flag(CEPH_OSDMAP_FULL)) {
984 dout(10) << __func__ << " clearing full flag" << dendl;
985 remove_flag(CEPH_OSDMAP_FULL);
986 }
987 if (nearfull > 0) {
988 if (!tmp.test_flag(CEPH_OSDMAP_NEARFULL)) {
989 dout(10) << __func__ << " setting nearfull flag" << dendl;
990 add_flag(CEPH_OSDMAP_NEARFULL);
991 }
992 } else {
993 if (tmp.test_flag(CEPH_OSDMAP_NEARFULL)) {
994 dout(10) << __func__ << " clearing nearfull flag" << dendl;
995 remove_flag(CEPH_OSDMAP_NEARFULL);
996 }
997 }
998 }
999
1000 // min_compat_client?
31f18b77 1001 if (tmp.require_min_compat_client == 0) {
7c673cae 1002 auto mv = tmp.get_min_compat_client();
31f18b77
FG
1003 dout(1) << __func__ << " setting require_min_compat_client to currently "
1004 << "required " << ceph_release_name(mv) << dendl;
1005 mon->clog->info() << "setting require_min_compat_client to currently "
1006 << "required " << ceph_release_name(mv);
1007 pending_inc.new_require_min_compat_client = mv;
7c673cae 1008 }
224ce89b
WB
1009
1010 if (osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
1011 // convert ec profile ruleset-* -> crush-*
1012 for (auto& p : tmp.erasure_code_profiles) {
1013 bool changed = false;
1014 map<string,string> newprofile;
1015 for (auto& q : p.second) {
1016 if (q.first.find("ruleset-") == 0) {
1017 string key = "crush-";
1018 key += q.first.substr(8);
1019 newprofile[key] = q.second;
1020 changed = true;
1021 dout(20) << " updating ec profile " << p.first
1022 << " key " << q.first << " -> " << key << dendl;
1023 } else {
1024 newprofile[q.first] = q.second;
1025 }
1026 }
1027 if (changed) {
1028 dout(10) << " updated ec profile " << p.first << ": "
1029 << newprofile << dendl;
1030 pending_inc.new_erasure_code_profiles[p.first] = newprofile;
1031 }
1032 }
c07f9fc5
FG
1033
1034 // auto-enable pool applications upon upgrade
1035 // NOTE: this can be removed post-Luminous assuming upgrades need to
1036 // proceed through Luminous
1037 for (auto &pool_pair : tmp.pools) {
1038 int64_t pool_id = pool_pair.first;
1039 pg_pool_t pg_pool = pool_pair.second;
1040 if (pg_pool.is_tier()) {
1041 continue;
1042 }
1043
1044 std::string pool_name = tmp.get_pool_name(pool_id);
1045 uint32_t match_count = 0;
1046
1047 // CephFS
1048 FSMap const &pending_fsmap = mon->mdsmon()->get_pending();
1049 if (pending_fsmap.pool_in_use(pool_id)) {
1050 dout(10) << __func__ << " auto-enabling CephFS on pool '"
1051 << pool_name << "'" << dendl;
1052 pg_pool.application_metadata.insert(
1053 {pg_pool_t::APPLICATION_NAME_CEPHFS, {}});
1054 ++match_count;
1055 }
1056
1057 // RBD heuristics (default OpenStack pool names from docs and
1058 // ceph-ansible)
1059 if (boost::algorithm::contains(pool_name, "rbd") ||
1060 pool_name == "images" || pool_name == "volumes" ||
1061 pool_name == "backups" || pool_name == "vms") {
1062 dout(10) << __func__ << " auto-enabling RBD on pool '"
1063 << pool_name << "'" << dendl;
1064 pg_pool.application_metadata.insert(
1065 {pg_pool_t::APPLICATION_NAME_RBD, {}});
1066 ++match_count;
1067 }
1068
1069 // RGW heuristics
1070 if (boost::algorithm::contains(pool_name, ".rgw") ||
1071 boost::algorithm::contains(pool_name, ".log") ||
1072 boost::algorithm::contains(pool_name, ".intent-log") ||
1073 boost::algorithm::contains(pool_name, ".usage") ||
1074 boost::algorithm::contains(pool_name, ".users")) {
1075 dout(10) << __func__ << " auto-enabling RGW on pool '"
1076 << pool_name << "'" << dendl;
1077 pg_pool.application_metadata.insert(
1078 {pg_pool_t::APPLICATION_NAME_RGW, {}});
1079 ++match_count;
1080 }
1081
1082 // OpenStack gnocchi (from ceph-ansible)
1083 if (pool_name == "metrics" && match_count == 0) {
1084 dout(10) << __func__ << " auto-enabling OpenStack Gnocchi on pool '"
1085 << pool_name << "'" << dendl;
1086 pg_pool.application_metadata.insert({"openstack_gnocchi", {}});
1087 ++match_count;
1088 }
1089
1090 if (match_count == 1) {
1091 pg_pool.last_change = pending_inc.epoch;
1092 pending_inc.new_pools[pool_id] = pg_pool;
1093 } else if (match_count > 1) {
1094 auto pstat = mon->pgservice->get_pool_stat(pool_id);
1095 if (pstat != nullptr && pstat->stats.sum.num_objects > 0) {
1096 mon->clog->info() << "unable to auto-enable application for pool "
1097 << "'" << pool_name << "'";
1098 }
1099 }
1100 }
224ce89b 1101 }
7c673cae
FG
1102 }
1103 }
1104
1105 // tell me about it
31f18b77 1106 for (auto i = pending_inc.new_state.begin();
7c673cae
FG
1107 i != pending_inc.new_state.end();
1108 ++i) {
1109 int s = i->second ? i->second : CEPH_OSD_UP;
1110 if (s & CEPH_OSD_UP)
1111 dout(2) << " osd." << i->first << " DOWN" << dendl;
1112 if (s & CEPH_OSD_EXISTS)
1113 dout(2) << " osd." << i->first << " DNE" << dendl;
1114 }
1115 for (map<int32_t,entity_addr_t>::iterator i = pending_inc.new_up_client.begin();
1116 i != pending_inc.new_up_client.end();
1117 ++i) {
1118 //FIXME: insert cluster addresses too
1119 dout(2) << " osd." << i->first << " UP " << i->second << dendl;
1120 }
1121 for (map<int32_t,uint32_t>::iterator i = pending_inc.new_weight.begin();
1122 i != pending_inc.new_weight.end();
1123 ++i) {
1124 if (i->second == CEPH_OSD_OUT) {
1125 dout(2) << " osd." << i->first << " OUT" << dendl;
1126 } else if (i->second == CEPH_OSD_IN) {
1127 dout(2) << " osd." << i->first << " IN" << dendl;
1128 } else {
1129 dout(2) << " osd." << i->first << " WEIGHT " << hex << i->second << dec << dendl;
1130 }
1131 }
1132
1133 // features for osdmap and its incremental
1134 uint64_t features = mon->get_quorum_con_features();
1135
1136 // encode full map and determine its crc
1137 OSDMap tmp;
1138 {
1139 tmp.deepish_copy_from(osdmap);
1140 tmp.apply_incremental(pending_inc);
1141
1142 // determine appropriate features
31f18b77 1143 if (tmp.require_osd_release < CEPH_RELEASE_LUMINOUS) {
7c673cae
FG
1144 dout(10) << __func__ << " encoding without feature SERVER_LUMINOUS"
1145 << dendl;
1146 features &= ~CEPH_FEATURE_SERVER_LUMINOUS;
1147 }
31f18b77 1148 if (tmp.require_osd_release < CEPH_RELEASE_KRAKEN) {
7c673cae
FG
1149 dout(10) << __func__ << " encoding without feature SERVER_KRAKEN | "
1150 << "MSG_ADDR2" << dendl;
1151 features &= ~(CEPH_FEATURE_SERVER_KRAKEN |
1152 CEPH_FEATURE_MSG_ADDR2);
1153 }
31f18b77
FG
1154 if (tmp.require_osd_release < CEPH_RELEASE_JEWEL) {
1155 dout(10) << __func__ << " encoding without feature SERVER_JEWEL" << dendl;
1156 features &= ~CEPH_FEATURE_SERVER_JEWEL;
1157 }
7c673cae
FG
1158 dout(10) << __func__ << " encoding full map with " << features << dendl;
1159
1160 bufferlist fullbl;
1161 ::encode(tmp, fullbl, features | CEPH_FEATURE_RESERVED);
1162 pending_inc.full_crc = tmp.get_crc();
1163
1164 // include full map in the txn. note that old monitors will
1165 // overwrite this. new ones will now skip the local full map
1166 // encode and reload from this.
1167 put_version_full(t, pending_inc.epoch, fullbl);
1168 }
1169
1170 // encode
1171 assert(get_last_committed() + 1 == pending_inc.epoch);
1172 ::encode(pending_inc, bl, features | CEPH_FEATURE_RESERVED);
1173
1174 dout(20) << " full_crc " << tmp.get_crc()
1175 << " inc_crc " << pending_inc.inc_crc << dendl;
1176
1177 /* put everything in the transaction */
1178 put_version(t, pending_inc.epoch, bl);
1179 put_last_committed(t, pending_inc.epoch);
1180
1181 // metadata, too!
1182 for (map<int,bufferlist>::iterator p = pending_metadata.begin();
1183 p != pending_metadata.end();
1184 ++p)
1185 t->put(OSD_METADATA_PREFIX, stringify(p->first), p->second);
1186 for (set<int>::iterator p = pending_metadata_rm.begin();
1187 p != pending_metadata_rm.end();
1188 ++p)
1189 t->erase(OSD_METADATA_PREFIX, stringify(*p));
1190 pending_metadata.clear();
1191 pending_metadata_rm.clear();
1192
1193 // and pg creating, also!
1194 if (mon->monmap->get_required_features().contains_all(
1195 ceph::features::mon::FEATURE_LUMINOUS)) {
1196 auto pending_creatings = update_pending_pgs(pending_inc);
31f18b77
FG
1197 if (osdmap.get_epoch() &&
1198 osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
7c673cae
FG
1199 dout(7) << __func__ << " in the middle of upgrading, "
1200 << " trimming pending creating_pgs using pgmap" << dendl;
31f18b77 1201 mon->pgservice->maybe_trim_creating_pgs(&pending_creatings);
7c673cae
FG
1202 }
1203 bufferlist creatings_bl;
1204 ::encode(pending_creatings, creatings_bl);
1205 t->put(OSD_PG_CREATING_PREFIX, "creating", creatings_bl);
1206 }
224ce89b
WB
1207
1208 // health
1209 health_check_map_t next;
1210 tmp.check_health(&next);
1211 encode_health(next, t);
7c673cae
FG
1212}
1213
1214void OSDMonitor::trim_creating_pgs(creating_pgs_t* creating_pgs,
31f18b77 1215 const ceph::unordered_map<pg_t,pg_stat_t>& pg_stat)
7c673cae
FG
1216{
1217 auto p = creating_pgs->pgs.begin();
1218 while (p != creating_pgs->pgs.end()) {
31f18b77
FG
1219 auto q = pg_stat.find(p->first);
1220 if (q != pg_stat.end() &&
7c673cae
FG
1221 !(q->second.state & PG_STATE_CREATING)) {
1222 dout(20) << __func__ << " pgmap shows " << p->first << " is created"
1223 << dendl;
1224 p = creating_pgs->pgs.erase(p);
7c673cae
FG
1225 } else {
1226 ++p;
1227 }
1228 }
1229}
1230
1231int OSDMonitor::load_metadata(int osd, map<string, string>& m, ostream *err)
1232{
1233 bufferlist bl;
1234 int r = mon->store->get(OSD_METADATA_PREFIX, stringify(osd), bl);
1235 if (r < 0)
1236 return r;
1237 try {
1238 bufferlist::iterator p = bl.begin();
1239 ::decode(m, p);
1240 }
1241 catch (buffer::error& e) {
1242 if (err)
1243 *err << "osd." << osd << " metadata is corrupt";
1244 return -EIO;
1245 }
1246 return 0;
1247}
1248
c07f9fc5 1249void OSDMonitor::count_metadata(const string& field, map<string,int> *out)
31f18b77 1250{
31f18b77
FG
1251 for (int osd = 0; osd < osdmap.get_max_osd(); ++osd) {
1252 if (osdmap.is_up(osd)) {
1253 map<string,string> meta;
1254 load_metadata(osd, meta, nullptr);
1255 auto p = meta.find(field);
1256 if (p == meta.end()) {
c07f9fc5 1257 (*out)["unknown"]++;
31f18b77 1258 } else {
c07f9fc5 1259 (*out)[p->second]++;
31f18b77
FG
1260 }
1261 }
1262 }
c07f9fc5
FG
1263}
1264
1265void OSDMonitor::count_metadata(const string& field, Formatter *f)
1266{
1267 map<string,int> by_val;
1268 count_metadata(field, &by_val);
31f18b77
FG
1269 f->open_object_section(field.c_str());
1270 for (auto& p : by_val) {
1271 f->dump_int(p.first.c_str(), p.second);
1272 }
1273 f->close_section();
1274}
1275
7c673cae
FG
1276int OSDMonitor::get_osd_objectstore_type(int osd, string *type)
1277{
1278 map<string, string> metadata;
1279 int r = load_metadata(osd, metadata, nullptr);
1280 if (r < 0)
1281 return r;
1282
1283 auto it = metadata.find("osd_objectstore");
1284 if (it == metadata.end())
1285 return -ENOENT;
1286 *type = it->second;
1287 return 0;
1288}
1289
1290bool OSDMonitor::is_pool_currently_all_bluestore(int64_t pool_id,
1291 const pg_pool_t &pool,
1292 ostream *err)
1293{
1294 // just check a few pgs for efficiency - this can't give a guarantee anyway,
1295 // since filestore osds could always join the pool later
1296 set<int> checked_osds;
1297 for (unsigned ps = 0; ps < MIN(8, pool.get_pg_num()); ++ps) {
1298 vector<int> up, acting;
1299 pg_t pgid(ps, pool_id, -1);
1300 osdmap.pg_to_up_acting_osds(pgid, up, acting);
1301 for (int osd : up) {
1302 if (checked_osds.find(osd) != checked_osds.end())
1303 continue;
1304 string objectstore_type;
1305 int r = get_osd_objectstore_type(osd, &objectstore_type);
1306 // allow with missing metadata, e.g. due to an osd never booting yet
1307 if (r < 0 || objectstore_type == "bluestore") {
1308 checked_osds.insert(osd);
1309 continue;
1310 }
1311 *err << "osd." << osd << " uses " << objectstore_type;
1312 return false;
1313 }
1314 }
1315 return true;
1316}
1317
1318int OSDMonitor::dump_osd_metadata(int osd, Formatter *f, ostream *err)
1319{
1320 map<string,string> m;
1321 if (int r = load_metadata(osd, m, err))
1322 return r;
1323 for (map<string,string>::iterator p = m.begin(); p != m.end(); ++p)
1324 f->dump_string(p->first.c_str(), p->second);
1325 return 0;
1326}
1327
1328void OSDMonitor::print_nodes(Formatter *f)
1329{
1330 // group OSDs by their hosts
1331 map<string, list<int> > osds; // hostname => osd
1332 for (int osd = 0; osd < osdmap.get_max_osd(); osd++) {
1333 map<string, string> m;
1334 if (load_metadata(osd, m, NULL)) {
1335 continue;
1336 }
1337 map<string, string>::iterator hostname = m.find("hostname");
1338 if (hostname == m.end()) {
1339 // not likely though
1340 continue;
1341 }
1342 osds[hostname->second].push_back(osd);
1343 }
1344
1345 dump_services(f, osds, "osd");
1346}
1347
1348void OSDMonitor::share_map_with_random_osd()
1349{
1350 if (osdmap.get_num_up_osds() == 0) {
1351 dout(10) << __func__ << " no up osds, don't share with anyone" << dendl;
1352 return;
1353 }
1354
1355 MonSession *s = mon->session_map.get_random_osd_session(&osdmap);
1356 if (!s) {
1357 dout(10) << __func__ << " no up osd on our session map" << dendl;
1358 return;
1359 }
1360
1361 dout(10) << "committed, telling random " << s->inst << " all about it" << dendl;
1362 // whatev, they'll request more if they need it
1363 MOSDMap *m = build_incremental(osdmap.get_epoch() - 1, osdmap.get_epoch());
1364 s->con->send_message(m);
1365 // NOTE: do *not* record osd has up to this epoch (as we do
1366 // elsewhere) as they may still need to request older values.
1367}
1368
1369version_t OSDMonitor::get_trim_to()
1370{
31f18b77
FG
1371 if (mon->get_quorum().empty()) {
1372 dout(10) << __func__ << ": quorum not formed" << dendl;
1373 return 0;
1374 }
7c673cae 1375
31f18b77 1376 epoch_t floor;
7c673cae
FG
1377 if (mon->monmap->get_required_features().contains_all(
1378 ceph::features::mon::FEATURE_LUMINOUS)) {
1379 {
31f18b77 1380 // TODO: Get this hidden in PGStatService
7c673cae
FG
1381 std::lock_guard<std::mutex> l(creating_pgs_lock);
1382 if (!creating_pgs.pgs.empty()) {
1383 return 0;
1384 }
1385 }
1386 floor = get_min_last_epoch_clean();
1387 } else {
31f18b77 1388 if (!mon->pgservice->is_readable())
7c673cae 1389 return 0;
31f18b77 1390 if (mon->pgservice->have_creating_pgs()) {
7c673cae
FG
1391 return 0;
1392 }
31f18b77 1393 floor = mon->pgservice->get_min_last_epoch_clean();
7c673cae
FG
1394 }
1395 {
1396 dout(10) << " min_last_epoch_clean " << floor << dendl;
1397 if (g_conf->mon_osd_force_trim_to > 0 &&
1398 g_conf->mon_osd_force_trim_to < (int)get_last_committed()) {
1399 floor = g_conf->mon_osd_force_trim_to;
1400 dout(10) << " explicit mon_osd_force_trim_to = " << floor << dendl;
1401 }
1402 unsigned min = g_conf->mon_min_osdmap_epochs;
1403 if (floor + min > get_last_committed()) {
1404 if (min < get_last_committed())
1405 floor = get_last_committed() - min;
1406 else
1407 floor = 0;
1408 }
1409 if (floor > get_first_committed())
1410 return floor;
1411 }
1412 return 0;
1413}
1414
1415epoch_t OSDMonitor::get_min_last_epoch_clean() const
1416{
1417 auto floor = last_epoch_clean.get_lower_bound(osdmap);
1418 // also scan osd epochs
1419 // don't trim past the oldest reported osd epoch
1420 for (auto& osd_epoch : osd_epochs) {
1421 if (osd_epoch.second < floor) {
1422 floor = osd_epoch.second;
1423 }
1424 }
1425 return floor;
1426}
1427
1428void OSDMonitor::encode_trim_extra(MonitorDBStore::TransactionRef tx,
1429 version_t first)
1430{
1431 dout(10) << __func__ << " including full map for e " << first << dendl;
1432 bufferlist bl;
1433 get_version_full(first, bl);
1434 put_version_full(tx, first, bl);
1435}
1436
1437// -------------
1438
1439bool OSDMonitor::preprocess_query(MonOpRequestRef op)
1440{
1441 op->mark_osdmon_event(__func__);
1442 Message *m = op->get_req();
1443 dout(10) << "preprocess_query " << *m << " from " << m->get_orig_source_inst() << dendl;
1444
1445 switch (m->get_type()) {
1446 // READs
1447 case MSG_MON_COMMAND:
1448 return preprocess_command(op);
1449 case CEPH_MSG_MON_GET_OSDMAP:
1450 return preprocess_get_osdmap(op);
1451
1452 // damp updates
1453 case MSG_OSD_MARK_ME_DOWN:
1454 return preprocess_mark_me_down(op);
1455 case MSG_OSD_FULL:
1456 return preprocess_full(op);
1457 case MSG_OSD_FAILURE:
1458 return preprocess_failure(op);
1459 case MSG_OSD_BOOT:
1460 return preprocess_boot(op);
1461 case MSG_OSD_ALIVE:
1462 return preprocess_alive(op);
1463 case MSG_OSD_PG_CREATED:
1464 return preprocess_pg_created(op);
1465 case MSG_OSD_PGTEMP:
1466 return preprocess_pgtemp(op);
1467 case MSG_OSD_BEACON:
1468 return preprocess_beacon(op);
1469
1470 case CEPH_MSG_POOLOP:
1471 return preprocess_pool_op(op);
1472
1473 case MSG_REMOVE_SNAPS:
1474 return preprocess_remove_snaps(op);
1475
1476 default:
1477 ceph_abort();
1478 return true;
1479 }
1480}
1481
1482bool OSDMonitor::prepare_update(MonOpRequestRef op)
1483{
1484 op->mark_osdmon_event(__func__);
1485 Message *m = op->get_req();
1486 dout(7) << "prepare_update " << *m << " from " << m->get_orig_source_inst() << dendl;
1487
1488 switch (m->get_type()) {
1489 // damp updates
1490 case MSG_OSD_MARK_ME_DOWN:
1491 return prepare_mark_me_down(op);
1492 case MSG_OSD_FULL:
1493 return prepare_full(op);
1494 case MSG_OSD_FAILURE:
1495 return prepare_failure(op);
1496 case MSG_OSD_BOOT:
1497 return prepare_boot(op);
1498 case MSG_OSD_ALIVE:
1499 return prepare_alive(op);
1500 case MSG_OSD_PG_CREATED:
1501 return prepare_pg_created(op);
1502 case MSG_OSD_PGTEMP:
1503 return prepare_pgtemp(op);
1504 case MSG_OSD_BEACON:
1505 return prepare_beacon(op);
1506
1507 case MSG_MON_COMMAND:
1508 return prepare_command(op);
1509
1510 case CEPH_MSG_POOLOP:
1511 return prepare_pool_op(op);
1512
1513 case MSG_REMOVE_SNAPS:
1514 return prepare_remove_snaps(op);
1515
1516
1517 default:
1518 ceph_abort();
1519 }
1520
1521 return false;
1522}
1523
1524bool OSDMonitor::should_propose(double& delay)
1525{
1526 dout(10) << "should_propose" << dendl;
1527
1528 // if full map, propose immediately! any subsequent changes will be clobbered.
1529 if (pending_inc.fullmap.length())
1530 return true;
1531
1532 // adjust osd weights?
1533 if (!osd_weight.empty() &&
1534 osd_weight.size() == (unsigned)osdmap.get_max_osd()) {
1535 dout(0) << " adjusting osd weights based on " << osd_weight << dendl;
1536 osdmap.adjust_osd_weights(osd_weight, pending_inc);
1537 delay = 0.0;
1538 osd_weight.clear();
1539 return true;
1540 }
1541
1542 // propose as fast as possible if updating up_thru or pg_temp
1543 // want to merge OSDMap changes as much as possible
1544 if ((pending_inc.new_primary_temp.size() == 1
1545 || pending_inc.new_up_thru.size() == 1)
1546 && pending_inc.new_state.size() < 2) {
1547 dout(15) << " propose as fast as possible for up_thru/pg_temp" << dendl;
1548
1549 utime_t now = ceph_clock_now();
1550 if (now - last_attempted_minwait_time > g_conf->paxos_propose_interval
1551 && now - paxos->get_last_commit_time() > g_conf->paxos_min_wait) {
1552 delay = g_conf->paxos_min_wait;
1553 last_attempted_minwait_time = now;
1554 return true;
1555 }
1556 }
1557
1558 return PaxosService::should_propose(delay);
1559}
1560
1561
1562
1563// ---------------------------
1564// READs
1565
1566bool OSDMonitor::preprocess_get_osdmap(MonOpRequestRef op)
1567{
1568 op->mark_osdmon_event(__func__);
1569 MMonGetOSDMap *m = static_cast<MMonGetOSDMap*>(op->get_req());
1570 dout(10) << __func__ << " " << *m << dendl;
1571 MOSDMap *reply = new MOSDMap(mon->monmap->fsid);
1572 epoch_t first = get_first_committed();
1573 epoch_t last = osdmap.get_epoch();
1574 int max = g_conf->osd_map_message_max;
1575 for (epoch_t e = MAX(first, m->get_full_first());
1576 e <= MIN(last, m->get_full_last()) && max > 0;
1577 ++e, --max) {
1578 int r = get_version_full(e, reply->maps[e]);
1579 assert(r >= 0);
1580 }
1581 for (epoch_t e = MAX(first, m->get_inc_first());
1582 e <= MIN(last, m->get_inc_last()) && max > 0;
1583 ++e, --max) {
1584 int r = get_version(e, reply->incremental_maps[e]);
1585 assert(r >= 0);
1586 }
1587 reply->oldest_map = first;
1588 reply->newest_map = last;
1589 mon->send_reply(op, reply);
1590 return true;
1591}
1592
1593
1594// ---------------------------
1595// UPDATEs
1596
1597// failure --
1598
1599bool OSDMonitor::check_source(PaxosServiceMessage *m, uuid_d fsid) {
1600 // check permissions
1601 MonSession *session = m->get_session();
1602 if (!session)
1603 return true;
1604 if (!session->is_capable("osd", MON_CAP_X)) {
1605 dout(0) << "got MOSDFailure from entity with insufficient caps "
1606 << session->caps << dendl;
1607 return true;
1608 }
1609 if (fsid != mon->monmap->fsid) {
1610 dout(0) << "check_source: on fsid " << fsid
1611 << " != " << mon->monmap->fsid << dendl;
1612 return true;
1613 }
1614 return false;
1615}
1616
1617
1618bool OSDMonitor::preprocess_failure(MonOpRequestRef op)
1619{
1620 op->mark_osdmon_event(__func__);
1621 MOSDFailure *m = static_cast<MOSDFailure*>(op->get_req());
1622 // who is target_osd
1623 int badboy = m->get_target().name.num();
1624
1625 // check permissions
1626 if (check_source(m, m->fsid))
1627 goto didit;
1628
1629 // first, verify the reporting host is valid
1630 if (m->get_orig_source().is_osd()) {
1631 int from = m->get_orig_source().num();
1632 if (!osdmap.exists(from) ||
1633 osdmap.get_addr(from) != m->get_orig_source_inst().addr ||
1634 (osdmap.is_down(from) && m->if_osd_failed())) {
1635 dout(5) << "preprocess_failure from dead osd." << from << ", ignoring" << dendl;
1636 send_incremental(op, m->get_epoch()+1);
1637 goto didit;
1638 }
1639 }
1640
1641
1642 // weird?
1643 if (osdmap.is_down(badboy)) {
1644 dout(5) << "preprocess_failure dne(/dup?): " << m->get_target() << ", from " << m->get_orig_source_inst() << dendl;
1645 if (m->get_epoch() < osdmap.get_epoch())
1646 send_incremental(op, m->get_epoch()+1);
1647 goto didit;
1648 }
1649 if (osdmap.get_inst(badboy) != m->get_target()) {
1650 dout(5) << "preprocess_failure wrong osd: report " << m->get_target() << " != map's " << osdmap.get_inst(badboy)
1651 << ", from " << m->get_orig_source_inst() << dendl;
1652 if (m->get_epoch() < osdmap.get_epoch())
1653 send_incremental(op, m->get_epoch()+1);
1654 goto didit;
1655 }
1656
1657 // already reported?
1658 if (osdmap.is_down(badboy) ||
1659 osdmap.get_up_from(badboy) > m->get_epoch()) {
1660 dout(5) << "preprocess_failure dup/old: " << m->get_target() << ", from " << m->get_orig_source_inst() << dendl;
1661 if (m->get_epoch() < osdmap.get_epoch())
1662 send_incremental(op, m->get_epoch()+1);
1663 goto didit;
1664 }
1665
1666 if (!can_mark_down(badboy)) {
1667 dout(5) << "preprocess_failure ignoring report of " << m->get_target() << " from " << m->get_orig_source_inst() << dendl;
1668 goto didit;
1669 }
1670
1671 dout(10) << "preprocess_failure new: " << m->get_target() << ", from " << m->get_orig_source_inst() << dendl;
1672 return false;
1673
1674 didit:
1675 return true;
1676}
1677
1678class C_AckMarkedDown : public C_MonOp {
1679 OSDMonitor *osdmon;
1680public:
1681 C_AckMarkedDown(
1682 OSDMonitor *osdmon,
1683 MonOpRequestRef op)
1684 : C_MonOp(op), osdmon(osdmon) {}
1685
1686 void _finish(int) override {
1687 MOSDMarkMeDown *m = static_cast<MOSDMarkMeDown*>(op->get_req());
1688 osdmon->mon->send_reply(
1689 op,
1690 new MOSDMarkMeDown(
1691 m->fsid,
1692 m->get_target(),
1693 m->get_epoch(),
1694 false)); // ACK itself does not request an ack
1695 }
1696 ~C_AckMarkedDown() override {
1697 }
1698};
1699
1700bool OSDMonitor::preprocess_mark_me_down(MonOpRequestRef op)
1701{
1702 op->mark_osdmon_event(__func__);
1703 MOSDMarkMeDown *m = static_cast<MOSDMarkMeDown*>(op->get_req());
1704 int requesting_down = m->get_target().name.num();
1705 int from = m->get_orig_source().num();
1706
1707 // check permissions
1708 if (check_source(m, m->fsid))
1709 goto reply;
1710
1711 // first, verify the reporting host is valid
1712 if (!m->get_orig_source().is_osd())
1713 goto reply;
1714
1715 if (!osdmap.exists(from) ||
1716 osdmap.is_down(from) ||
1717 osdmap.get_addr(from) != m->get_target().addr) {
1718 dout(5) << "preprocess_mark_me_down from dead osd."
1719 << from << ", ignoring" << dendl;
1720 send_incremental(op, m->get_epoch()+1);
1721 goto reply;
1722 }
1723
1724 // no down might be set
1725 if (!can_mark_down(requesting_down))
1726 goto reply;
1727
1728 dout(10) << "MOSDMarkMeDown for: " << m->get_target() << dendl;
1729 return false;
1730
1731 reply:
1732 if (m->request_ack) {
1733 Context *c(new C_AckMarkedDown(this, op));
1734 c->complete(0);
1735 }
1736 return true;
1737}
1738
1739bool OSDMonitor::prepare_mark_me_down(MonOpRequestRef op)
1740{
1741 op->mark_osdmon_event(__func__);
1742 MOSDMarkMeDown *m = static_cast<MOSDMarkMeDown*>(op->get_req());
1743 int target_osd = m->get_target().name.num();
1744
1745 assert(osdmap.is_up(target_osd));
1746 assert(osdmap.get_addr(target_osd) == m->get_target().addr);
1747
1748 mon->clog->info() << "osd." << target_osd << " marked itself down";
1749 pending_inc.new_state[target_osd] = CEPH_OSD_UP;
1750 if (m->request_ack)
1751 wait_for_finished_proposal(op, new C_AckMarkedDown(this, op));
1752 return true;
1753}
1754
1755bool OSDMonitor::can_mark_down(int i)
1756{
1757 if (osdmap.test_flag(CEPH_OSDMAP_NODOWN)) {
31f18b77
FG
1758 dout(5) << __func__ << " NODOWN flag set, will not mark osd." << i
1759 << " down" << dendl;
1760 return false;
1761 }
1762
1763 if (osdmap.is_nodown(i)) {
1764 dout(5) << __func__ << " osd." << i << " is marked as nodown, "
1765 << "will not mark it down" << dendl;
7c673cae
FG
1766 return false;
1767 }
31f18b77 1768
7c673cae
FG
1769 int num_osds = osdmap.get_num_osds();
1770 if (num_osds == 0) {
31f18b77 1771 dout(5) << __func__ << " no osds" << dendl;
7c673cae
FG
1772 return false;
1773 }
1774 int up = osdmap.get_num_up_osds() - pending_inc.get_net_marked_down(&osdmap);
1775 float up_ratio = (float)up / (float)num_osds;
1776 if (up_ratio < g_conf->mon_osd_min_up_ratio) {
31f18b77 1777 dout(2) << __func__ << " current up_ratio " << up_ratio << " < min "
7c673cae
FG
1778 << g_conf->mon_osd_min_up_ratio
1779 << ", will not mark osd." << i << " down" << dendl;
1780 return false;
1781 }
1782 return true;
1783}
1784
1785bool OSDMonitor::can_mark_up(int i)
1786{
1787 if (osdmap.test_flag(CEPH_OSDMAP_NOUP)) {
31f18b77
FG
1788 dout(5) << __func__ << " NOUP flag set, will not mark osd." << i
1789 << " up" << dendl;
1790 return false;
1791 }
1792
1793 if (osdmap.is_noup(i)) {
1794 dout(5) << __func__ << " osd." << i << " is marked as noup, "
1795 << "will not mark it up" << dendl;
7c673cae
FG
1796 return false;
1797 }
31f18b77 1798
7c673cae
FG
1799 return true;
1800}
1801
1802/**
1803 * @note the parameter @p i apparently only exists here so we can output the
1804 * osd's id on messages.
1805 */
1806bool OSDMonitor::can_mark_out(int i)
1807{
1808 if (osdmap.test_flag(CEPH_OSDMAP_NOOUT)) {
1809 dout(5) << __func__ << " NOOUT flag set, will not mark osds out" << dendl;
1810 return false;
1811 }
31f18b77
FG
1812
1813 if (osdmap.is_noout(i)) {
1814 dout(5) << __func__ << " osd." << i << " is marked as noout, "
1815 << "will not mark it out" << dendl;
1816 return false;
1817 }
1818
7c673cae
FG
1819 int num_osds = osdmap.get_num_osds();
1820 if (num_osds == 0) {
1821 dout(5) << __func__ << " no osds" << dendl;
1822 return false;
1823 }
1824 int in = osdmap.get_num_in_osds() - pending_inc.get_net_marked_out(&osdmap);
1825 float in_ratio = (float)in / (float)num_osds;
1826 if (in_ratio < g_conf->mon_osd_min_in_ratio) {
1827 if (i >= 0)
1828 dout(5) << __func__ << " current in_ratio " << in_ratio << " < min "
1829 << g_conf->mon_osd_min_in_ratio
1830 << ", will not mark osd." << i << " out" << dendl;
1831 else
1832 dout(5) << __func__ << " current in_ratio " << in_ratio << " < min "
1833 << g_conf->mon_osd_min_in_ratio
1834 << ", will not mark osds out" << dendl;
1835 return false;
1836 }
1837
1838 return true;
1839}
1840
1841bool OSDMonitor::can_mark_in(int i)
1842{
1843 if (osdmap.test_flag(CEPH_OSDMAP_NOIN)) {
31f18b77
FG
1844 dout(5) << __func__ << " NOIN flag set, will not mark osd." << i
1845 << " in" << dendl;
1846 return false;
1847 }
1848
1849 if (osdmap.is_noin(i)) {
1850 dout(5) << __func__ << " osd." << i << " is marked as noin, "
1851 << "will not mark it in" << dendl;
7c673cae
FG
1852 return false;
1853 }
31f18b77 1854
7c673cae
FG
1855 return true;
1856}
1857
1858bool OSDMonitor::check_failures(utime_t now)
1859{
1860 bool found_failure = false;
1861 for (map<int,failure_info_t>::iterator p = failure_info.begin();
1862 p != failure_info.end();
1863 ++p) {
1864 if (can_mark_down(p->first)) {
1865 found_failure |= check_failure(now, p->first, p->second);
1866 }
1867 }
1868 return found_failure;
1869}
1870
1871bool OSDMonitor::check_failure(utime_t now, int target_osd, failure_info_t& fi)
1872{
1873 // already pending failure?
1874 if (pending_inc.new_state.count(target_osd) &&
1875 pending_inc.new_state[target_osd] & CEPH_OSD_UP) {
1876 dout(10) << " already pending failure" << dendl;
1877 return true;
1878 }
1879
1880 set<string> reporters_by_subtree;
1881 string reporter_subtree_level = g_conf->mon_osd_reporter_subtree_level;
1882 utime_t orig_grace(g_conf->osd_heartbeat_grace, 0);
1883 utime_t max_failed_since = fi.get_failed_since();
1884 utime_t failed_for = now - max_failed_since;
1885
1886 utime_t grace = orig_grace;
1887 double my_grace = 0, peer_grace = 0;
1888 double decay_k = 0;
1889 if (g_conf->mon_osd_adjust_heartbeat_grace) {
1890 double halflife = (double)g_conf->mon_osd_laggy_halflife;
1891 decay_k = ::log(.5) / halflife;
1892
1893 // scale grace period based on historical probability of 'lagginess'
1894 // (false positive failures due to slowness).
1895 const osd_xinfo_t& xi = osdmap.get_xinfo(target_osd);
1896 double decay = exp((double)failed_for * decay_k);
1897 dout(20) << " halflife " << halflife << " decay_k " << decay_k
1898 << " failed_for " << failed_for << " decay " << decay << dendl;
1899 my_grace = decay * (double)xi.laggy_interval * xi.laggy_probability;
1900 grace += my_grace;
1901 }
1902
1903 // consider the peers reporting a failure a proxy for a potential
1904 // 'subcluster' over the overall cluster that is similarly
1905 // laggy. this is clearly not true in all cases, but will sometimes
1906 // help us localize the grace correction to a subset of the system
1907 // (say, a rack with a bad switch) that is unhappy.
1908 assert(fi.reporters.size());
1909 for (map<int,failure_reporter_t>::iterator p = fi.reporters.begin();
1910 p != fi.reporters.end();
1911 ++p) {
1912 // get the parent bucket whose type matches with "reporter_subtree_level".
1913 // fall back to OSD if the level doesn't exist.
1914 map<string, string> reporter_loc = osdmap.crush->get_full_location(p->first);
1915 map<string, string>::iterator iter = reporter_loc.find(reporter_subtree_level);
1916 if (iter == reporter_loc.end()) {
1917 reporters_by_subtree.insert("osd." + to_string(p->first));
1918 } else {
1919 reporters_by_subtree.insert(iter->second);
1920 }
1921 if (g_conf->mon_osd_adjust_heartbeat_grace) {
1922 const osd_xinfo_t& xi = osdmap.get_xinfo(p->first);
1923 utime_t elapsed = now - xi.down_stamp;
1924 double decay = exp((double)elapsed * decay_k);
1925 peer_grace += decay * (double)xi.laggy_interval * xi.laggy_probability;
1926 }
1927 }
1928
1929 if (g_conf->mon_osd_adjust_heartbeat_grace) {
1930 peer_grace /= (double)fi.reporters.size();
1931 grace += peer_grace;
1932 }
1933
1934 dout(10) << " osd." << target_osd << " has "
1935 << fi.reporters.size() << " reporters, "
1936 << grace << " grace (" << orig_grace << " + " << my_grace
1937 << " + " << peer_grace << "), max_failed_since " << max_failed_since
1938 << dendl;
1939
1940 if (failed_for >= grace &&
1941 (int)reporters_by_subtree.size() >= g_conf->mon_osd_min_down_reporters) {
1942 dout(1) << " we have enough reporters to mark osd." << target_osd
1943 << " down" << dendl;
1944 pending_inc.new_state[target_osd] = CEPH_OSD_UP;
1945
31f18b77
FG
1946 mon->clog->info() << "osd." << target_osd << " failed ("
1947 << osdmap.crush->get_full_location_ordered_string(
1948 target_osd)
1949 << ") ("
1950 << (int)reporters_by_subtree.size()
1951 << " reporters from different "
7c673cae
FG
1952 << reporter_subtree_level << " after "
1953 << failed_for << " >= grace " << grace << ")";
1954 return true;
1955 }
1956 return false;
1957}
1958
224ce89b 1959void OSDMonitor::force_failure(int target_osd, int by)
7c673cae
FG
1960{
1961 // already pending failure?
1962 if (pending_inc.new_state.count(target_osd) &&
1963 pending_inc.new_state[target_osd] & CEPH_OSD_UP) {
1964 dout(10) << " already pending failure" << dendl;
1965 return;
1966 }
1967
1968 dout(1) << " we're forcing failure of osd." << target_osd << dendl;
1969 pending_inc.new_state[target_osd] = CEPH_OSD_UP;
1970
31f18b77
FG
1971 mon->clog->info() << "osd." << target_osd << " failed ("
1972 << osdmap.crush->get_full_location_ordered_string(target_osd)
1973 << ") (connection refused reported by osd." << by << ")";
7c673cae
FG
1974 return;
1975}
1976
1977bool OSDMonitor::prepare_failure(MonOpRequestRef op)
1978{
1979 op->mark_osdmon_event(__func__);
1980 MOSDFailure *m = static_cast<MOSDFailure*>(op->get_req());
1981 dout(1) << "prepare_failure " << m->get_target()
1982 << " from " << m->get_orig_source_inst()
1983 << " is reporting failure:" << m->if_osd_failed() << dendl;
1984
1985 int target_osd = m->get_target().name.num();
1986 int reporter = m->get_orig_source().num();
1987 assert(osdmap.is_up(target_osd));
1988 assert(osdmap.get_addr(target_osd) == m->get_target().addr);
1989
1990 if (m->if_osd_failed()) {
1991 // calculate failure time
1992 utime_t now = ceph_clock_now();
1993 utime_t failed_since =
1994 m->get_recv_stamp() - utime_t(m->failed_for, 0);
1995
1996 // add a report
1997 if (m->is_immediate()) {
1998 mon->clog->debug() << m->get_target() << " reported immediately failed by "
1999 << m->get_orig_source_inst();
224ce89b 2000 force_failure(target_osd, reporter);
7c673cae
FG
2001 return true;
2002 }
2003 mon->clog->debug() << m->get_target() << " reported failed by "
2004 << m->get_orig_source_inst();
2005
2006 failure_info_t& fi = failure_info[target_osd];
2007 MonOpRequestRef old_op = fi.add_report(reporter, failed_since, op);
2008 if (old_op) {
2009 mon->no_reply(old_op);
2010 }
2011
2012 return check_failure(now, target_osd, fi);
2013 } else {
2014 // remove the report
2015 mon->clog->debug() << m->get_target() << " failure report canceled by "
2016 << m->get_orig_source_inst();
2017 if (failure_info.count(target_osd)) {
2018 failure_info_t& fi = failure_info[target_osd];
2019 MonOpRequestRef report_op = fi.cancel_report(reporter);
2020 if (report_op) {
2021 mon->no_reply(report_op);
2022 }
2023 if (fi.reporters.empty()) {
2024 dout(10) << " removing last failure_info for osd." << target_osd
2025 << dendl;
2026 failure_info.erase(target_osd);
2027 } else {
2028 dout(10) << " failure_info for osd." << target_osd << " now "
2029 << fi.reporters.size() << " reporters" << dendl;
2030 }
2031 } else {
2032 dout(10) << " no failure_info for osd." << target_osd << dendl;
2033 }
2034 mon->no_reply(op);
2035 }
2036
2037 return false;
2038}
2039
2040void OSDMonitor::process_failures()
2041{
2042 map<int,failure_info_t>::iterator p = failure_info.begin();
2043 while (p != failure_info.end()) {
2044 if (osdmap.is_up(p->first)) {
2045 ++p;
2046 } else {
2047 dout(10) << "process_failures osd." << p->first << dendl;
2048 list<MonOpRequestRef> ls;
2049 p->second.take_report_messages(ls);
2050 failure_info.erase(p++);
2051
2052 while (!ls.empty()) {
2053 MonOpRequestRef o = ls.front();
2054 if (o) {
2055 o->mark_event(__func__);
2056 MOSDFailure *m = o->get_req<MOSDFailure>();
2057 send_latest(o, m->get_epoch());
2058 }
2059 ls.pop_front();
2060 }
2061 }
2062 }
2063}
2064
2065void OSDMonitor::take_all_failures(list<MonOpRequestRef>& ls)
2066{
2067 dout(10) << __func__ << " on " << failure_info.size() << " osds" << dendl;
2068
2069 for (map<int,failure_info_t>::iterator p = failure_info.begin();
2070 p != failure_info.end();
2071 ++p) {
2072 p->second.take_report_messages(ls);
2073 }
2074 failure_info.clear();
2075}
2076
2077
2078// boot --
2079
2080bool OSDMonitor::preprocess_boot(MonOpRequestRef op)
2081{
2082 op->mark_osdmon_event(__func__);
2083 MOSDBoot *m = static_cast<MOSDBoot*>(op->get_req());
2084 int from = m->get_orig_source_inst().name.num();
2085
2086 // check permissions, ignore if failed (no response expected)
2087 MonSession *session = m->get_session();
2088 if (!session)
2089 goto ignore;
2090 if (!session->is_capable("osd", MON_CAP_X)) {
2091 dout(0) << "got preprocess_boot message from entity with insufficient caps"
2092 << session->caps << dendl;
2093 goto ignore;
2094 }
2095
2096 if (m->sb.cluster_fsid != mon->monmap->fsid) {
2097 dout(0) << "preprocess_boot on fsid " << m->sb.cluster_fsid
2098 << " != " << mon->monmap->fsid << dendl;
2099 goto ignore;
2100 }
2101
2102 if (m->get_orig_source_inst().addr.is_blank_ip()) {
2103 dout(0) << "preprocess_boot got blank addr for " << m->get_orig_source_inst() << dendl;
2104 goto ignore;
2105 }
2106
2107 assert(m->get_orig_source_inst().name.is_osd());
2108
2109 // check if osd has required features to boot
2110 if ((osdmap.get_features(CEPH_ENTITY_TYPE_OSD, NULL) &
2111 CEPH_FEATURE_OSD_ERASURE_CODES) &&
2112 !(m->get_connection()->get_features() & CEPH_FEATURE_OSD_ERASURE_CODES)) {
2113 dout(0) << __func__ << " osdmap requires erasure code but osd at "
2114 << m->get_orig_source_inst()
2115 << " doesn't announce support -- ignore" << dendl;
2116 goto ignore;
2117 }
2118
2119 if ((osdmap.get_features(CEPH_ENTITY_TYPE_OSD, NULL) &
2120 CEPH_FEATURE_ERASURE_CODE_PLUGINS_V2) &&
2121 !(m->get_connection()->get_features() & CEPH_FEATURE_ERASURE_CODE_PLUGINS_V2)) {
2122 dout(0) << __func__ << " osdmap requires erasure code plugins v2 but osd at "
2123 << m->get_orig_source_inst()
2124 << " doesn't announce support -- ignore" << dendl;
2125 goto ignore;
2126 }
2127
2128 if ((osdmap.get_features(CEPH_ENTITY_TYPE_OSD, NULL) &
2129 CEPH_FEATURE_ERASURE_CODE_PLUGINS_V3) &&
2130 !(m->get_connection()->get_features() & CEPH_FEATURE_ERASURE_CODE_PLUGINS_V3)) {
2131 dout(0) << __func__ << " osdmap requires erasure code plugins v3 but osd at "
2132 << m->get_orig_source_inst()
2133 << " doesn't announce support -- ignore" << dendl;
2134 goto ignore;
2135 }
2136
31f18b77 2137 if (osdmap.require_osd_release >= CEPH_RELEASE_LUMINOUS &&
7c673cae
FG
2138 !HAVE_FEATURE(m->osd_features, SERVER_LUMINOUS)) {
2139 mon->clog->info() << "disallowing boot of OSD "
2140 << m->get_orig_source_inst()
2141 << " because the osdmap requires"
2142 << " CEPH_FEATURE_SERVER_LUMINOUS"
2143 << " but the osd lacks CEPH_FEATURE_SERVER_LUMINOUS";
2144 goto ignore;
2145 }
2146
31f18b77 2147 if (osdmap.require_osd_release >= CEPH_RELEASE_JEWEL &&
7c673cae
FG
2148 !(m->osd_features & CEPH_FEATURE_SERVER_JEWEL)) {
2149 mon->clog->info() << "disallowing boot of OSD "
2150 << m->get_orig_source_inst()
2151 << " because the osdmap requires"
2152 << " CEPH_FEATURE_SERVER_JEWEL"
2153 << " but the osd lacks CEPH_FEATURE_SERVER_JEWEL";
2154 goto ignore;
2155 }
2156
31f18b77 2157 if (osdmap.require_osd_release >= CEPH_RELEASE_KRAKEN &&
7c673cae
FG
2158 !HAVE_FEATURE(m->osd_features, SERVER_KRAKEN)) {
2159 mon->clog->info() << "disallowing boot of OSD "
2160 << m->get_orig_source_inst()
2161 << " because the osdmap requires"
2162 << " CEPH_FEATURE_SERVER_KRAKEN"
2163 << " but the osd lacks CEPH_FEATURE_SERVER_KRAKEN";
2164 goto ignore;
2165 }
2166
2167 if (osdmap.test_flag(CEPH_OSDMAP_SORTBITWISE) &&
2168 !(m->osd_features & CEPH_FEATURE_OSD_BITWISE_HOBJ_SORT)) {
2169 mon->clog->info() << "disallowing boot of OSD "
2170 << m->get_orig_source_inst()
2171 << " because 'sortbitwise' osdmap flag is set and OSD lacks the OSD_BITWISE_HOBJ_SORT feature";
2172 goto ignore;
2173 }
2174
c07f9fc5
FG
2175 if (osdmap.test_flag(CEPH_OSDMAP_RECOVERY_DELETES) &&
2176 !(m->osd_features & CEPH_FEATURE_OSD_RECOVERY_DELETES)) {
2177 mon->clog->info() << "disallowing boot of OSD "
2178 << m->get_orig_source_inst()
2179 << " because 'recovery_deletes' osdmap flag is set and OSD lacks the OSD_RECOVERY_DELETES feature";
2180 goto ignore;
2181 }
2182
7c673cae
FG
2183 if (any_of(osdmap.get_pools().begin(),
2184 osdmap.get_pools().end(),
2185 [](const std::pair<int64_t,pg_pool_t>& pool)
2186 { return pool.second.use_gmt_hitset; })) {
2187 assert(osdmap.get_num_up_osds() == 0 ||
2188 osdmap.get_up_osd_features() & CEPH_FEATURE_OSD_HITSET_GMT);
2189 if (!(m->osd_features & CEPH_FEATURE_OSD_HITSET_GMT)) {
2190 dout(0) << __func__ << " one or more pools uses GMT hitsets but osd at "
2191 << m->get_orig_source_inst()
2192 << " doesn't announce support -- ignore" << dendl;
2193 goto ignore;
2194 }
2195 }
2196
2197 // make sure upgrades stop at luminous
2198 if (HAVE_FEATURE(m->osd_features, SERVER_M) &&
31f18b77 2199 osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
7c673cae
FG
2200 mon->clog->info() << "disallowing boot of post-luminous OSD "
2201 << m->get_orig_source_inst()
31f18b77 2202 << " because require_osd_release < luminous";
7c673cae
FG
2203 goto ignore;
2204 }
2205
2206 // make sure upgrades stop at jewel
2207 if (HAVE_FEATURE(m->osd_features, SERVER_KRAKEN) &&
31f18b77 2208 osdmap.require_osd_release < CEPH_RELEASE_JEWEL) {
7c673cae
FG
2209 mon->clog->info() << "disallowing boot of post-jewel OSD "
2210 << m->get_orig_source_inst()
31f18b77 2211 << " because require_osd_release < jewel";
7c673cae
FG
2212 goto ignore;
2213 }
2214
2215 // make sure upgrades stop at hammer
2216 // * HAMMER_0_94_4 is the required hammer feature
2217 // * MON_METADATA is the first post-hammer feature
2218 if (osdmap.get_num_up_osds() > 0) {
2219 if ((m->osd_features & CEPH_FEATURE_MON_METADATA) &&
2220 !(osdmap.get_up_osd_features() & CEPH_FEATURE_HAMMER_0_94_4)) {
2221 mon->clog->info() << "disallowing boot of post-hammer OSD "
2222 << m->get_orig_source_inst()
2223 << " because one or more up OSDs is pre-hammer v0.94.4";
2224 goto ignore;
2225 }
2226 if (!(m->osd_features & CEPH_FEATURE_HAMMER_0_94_4) &&
2227 (osdmap.get_up_osd_features() & CEPH_FEATURE_MON_METADATA)) {
2228 mon->clog->info() << "disallowing boot of pre-hammer v0.94.4 OSD "
2229 << m->get_orig_source_inst()
2230 << " because all up OSDs are post-hammer";
2231 goto ignore;
2232 }
2233 }
2234
2235 // already booted?
2236 if (osdmap.is_up(from) &&
2237 osdmap.get_inst(from) == m->get_orig_source_inst() &&
2238 osdmap.get_cluster_addr(from) == m->cluster_addr) {
2239 // yup.
2240 dout(7) << "preprocess_boot dup from " << m->get_orig_source_inst()
2241 << " == " << osdmap.get_inst(from) << dendl;
2242 _booted(op, false);
2243 return true;
2244 }
2245
2246 if (osdmap.exists(from) &&
2247 !osdmap.get_uuid(from).is_zero() &&
2248 osdmap.get_uuid(from) != m->sb.osd_fsid) {
2249 dout(7) << __func__ << " from " << m->get_orig_source_inst()
2250 << " clashes with existing osd: different fsid"
2251 << " (ours: " << osdmap.get_uuid(from)
2252 << " ; theirs: " << m->sb.osd_fsid << ")" << dendl;
2253 goto ignore;
2254 }
2255
2256 if (osdmap.exists(from) &&
2257 osdmap.get_info(from).up_from > m->version &&
2258 osdmap.get_most_recent_inst(from) == m->get_orig_source_inst()) {
2259 dout(7) << "prepare_boot msg from before last up_from, ignoring" << dendl;
2260 send_latest(op, m->sb.current_epoch+1);
2261 return true;
2262 }
2263
2264 // noup?
2265 if (!can_mark_up(from)) {
2266 dout(7) << "preprocess_boot ignoring boot from " << m->get_orig_source_inst() << dendl;
2267 send_latest(op, m->sb.current_epoch+1);
2268 return true;
2269 }
2270
2271 dout(10) << "preprocess_boot from " << m->get_orig_source_inst() << dendl;
2272 return false;
2273
2274 ignore:
2275 return true;
2276}
2277
2278bool OSDMonitor::prepare_boot(MonOpRequestRef op)
2279{
2280 op->mark_osdmon_event(__func__);
2281 MOSDBoot *m = static_cast<MOSDBoot*>(op->get_req());
2282 dout(7) << __func__ << " from " << m->get_orig_source_inst() << " sb " << m->sb
2283 << " cluster_addr " << m->cluster_addr
2284 << " hb_back_addr " << m->hb_back_addr
2285 << " hb_front_addr " << m->hb_front_addr
2286 << dendl;
2287
2288 assert(m->get_orig_source().is_osd());
2289 int from = m->get_orig_source().num();
2290
2291 // does this osd exist?
2292 if (from >= osdmap.get_max_osd()) {
2293 dout(1) << "boot from osd." << from << " >= max_osd "
2294 << osdmap.get_max_osd() << dendl;
2295 return false;
2296 }
2297
2298 int oldstate = osdmap.exists(from) ? osdmap.get_state(from) : CEPH_OSD_NEW;
2299 if (pending_inc.new_state.count(from))
2300 oldstate ^= pending_inc.new_state[from];
2301
2302 // already up? mark down first?
2303 if (osdmap.is_up(from)) {
2304 dout(7) << __func__ << " was up, first marking down "
2305 << osdmap.get_inst(from) << dendl;
2306 // preprocess should have caught these; if not, assert.
2307 assert(osdmap.get_inst(from) != m->get_orig_source_inst() ||
2308 osdmap.get_cluster_addr(from) != m->cluster_addr);
2309 assert(osdmap.get_uuid(from) == m->sb.osd_fsid);
2310
2311 if (pending_inc.new_state.count(from) == 0 ||
2312 (pending_inc.new_state[from] & CEPH_OSD_UP) == 0) {
2313 // mark previous guy down
2314 pending_inc.new_state[from] = CEPH_OSD_UP;
2315 }
2316 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
2317 } else if (pending_inc.new_up_client.count(from)) {
2318 // already prepared, just wait
2319 dout(7) << __func__ << " already prepared, waiting on "
2320 << m->get_orig_source_addr() << dendl;
2321 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
2322 } else {
2323 // mark new guy up.
2324 pending_inc.new_up_client[from] = m->get_orig_source_addr();
2325 if (!m->cluster_addr.is_blank_ip())
2326 pending_inc.new_up_cluster[from] = m->cluster_addr;
2327 pending_inc.new_hb_back_up[from] = m->hb_back_addr;
2328 if (!m->hb_front_addr.is_blank_ip())
2329 pending_inc.new_hb_front_up[from] = m->hb_front_addr;
2330
2331 down_pending_out.erase(from); // if any
2332
2333 if (m->sb.weight)
2334 osd_weight[from] = m->sb.weight;
2335
2336 // set uuid?
2337 dout(10) << " setting osd." << from << " uuid to " << m->sb.osd_fsid
2338 << dendl;
2339 if (!osdmap.exists(from) || osdmap.get_uuid(from) != m->sb.osd_fsid) {
2340 // preprocess should have caught this; if not, assert.
2341 assert(!osdmap.exists(from) || osdmap.get_uuid(from).is_zero());
2342 pending_inc.new_uuid[from] = m->sb.osd_fsid;
2343 }
2344
2345 // fresh osd?
2346 if (m->sb.newest_map == 0 && osdmap.exists(from)) {
2347 const osd_info_t& i = osdmap.get_info(from);
2348 if (i.up_from > i.lost_at) {
2349 dout(10) << " fresh osd; marking lost_at too" << dendl;
2350 pending_inc.new_lost[from] = osdmap.get_epoch();
2351 }
2352 }
2353
2354 // metadata
2355 bufferlist osd_metadata;
2356 ::encode(m->metadata, osd_metadata);
2357 pending_metadata[from] = osd_metadata;
31f18b77 2358 pending_metadata_rm.erase(from);
7c673cae
FG
2359
2360 // adjust last clean unmount epoch?
2361 const osd_info_t& info = osdmap.get_info(from);
2362 dout(10) << " old osd_info: " << info << dendl;
2363 if (m->sb.mounted > info.last_clean_begin ||
2364 (m->sb.mounted == info.last_clean_begin &&
2365 m->sb.clean_thru > info.last_clean_end)) {
2366 epoch_t begin = m->sb.mounted;
2367 epoch_t end = m->sb.clean_thru;
2368
2369 dout(10) << __func__ << " osd." << from << " last_clean_interval "
2370 << "[" << info.last_clean_begin << "," << info.last_clean_end
2371 << ") -> [" << begin << "-" << end << ")"
2372 << dendl;
2373 pending_inc.new_last_clean_interval[from] =
2374 pair<epoch_t,epoch_t>(begin, end);
2375 }
2376
2377 osd_xinfo_t xi = osdmap.get_xinfo(from);
2378 if (m->boot_epoch == 0) {
2379 xi.laggy_probability *= (1.0 - g_conf->mon_osd_laggy_weight);
2380 xi.laggy_interval *= (1.0 - g_conf->mon_osd_laggy_weight);
2381 dout(10) << " not laggy, new xi " << xi << dendl;
2382 } else {
2383 if (xi.down_stamp.sec()) {
2384 int interval = ceph_clock_now().sec() -
2385 xi.down_stamp.sec();
2386 if (g_conf->mon_osd_laggy_max_interval &&
2387 (interval > g_conf->mon_osd_laggy_max_interval)) {
2388 interval = g_conf->mon_osd_laggy_max_interval;
2389 }
2390 xi.laggy_interval =
2391 interval * g_conf->mon_osd_laggy_weight +
2392 xi.laggy_interval * (1.0 - g_conf->mon_osd_laggy_weight);
2393 }
2394 xi.laggy_probability =
2395 g_conf->mon_osd_laggy_weight +
2396 xi.laggy_probability * (1.0 - g_conf->mon_osd_laggy_weight);
2397 dout(10) << " laggy, now xi " << xi << dendl;
2398 }
2399
2400 // set features shared by the osd
2401 if (m->osd_features)
2402 xi.features = m->osd_features;
2403 else
2404 xi.features = m->get_connection()->get_features();
2405
2406 // mark in?
2407 if ((g_conf->mon_osd_auto_mark_auto_out_in &&
2408 (oldstate & CEPH_OSD_AUTOOUT)) ||
2409 (g_conf->mon_osd_auto_mark_new_in && (oldstate & CEPH_OSD_NEW)) ||
2410 (g_conf->mon_osd_auto_mark_in)) {
2411 if (can_mark_in(from)) {
2412 if (osdmap.osd_xinfo[from].old_weight > 0) {
2413 pending_inc.new_weight[from] = osdmap.osd_xinfo[from].old_weight;
2414 xi.old_weight = 0;
2415 } else {
2416 pending_inc.new_weight[from] = CEPH_OSD_IN;
2417 }
2418 } else {
2419 dout(7) << __func__ << " NOIN set, will not mark in "
2420 << m->get_orig_source_addr() << dendl;
2421 }
2422 }
2423
2424 pending_inc.new_xinfo[from] = xi;
2425
2426 // wait
2427 wait_for_finished_proposal(op, new C_Booted(this, op));
2428 }
2429 return true;
2430}
2431
2432void OSDMonitor::_booted(MonOpRequestRef op, bool logit)
2433{
2434 op->mark_osdmon_event(__func__);
2435 MOSDBoot *m = static_cast<MOSDBoot*>(op->get_req());
2436 dout(7) << "_booted " << m->get_orig_source_inst()
2437 << " w " << m->sb.weight << " from " << m->sb.current_epoch << dendl;
2438
2439 if (logit) {
2440 mon->clog->info() << m->get_orig_source_inst() << " boot";
2441 }
2442
2443 send_latest(op, m->sb.current_epoch+1);
2444}
2445
2446
2447// -------------
2448// full
2449
2450bool OSDMonitor::preprocess_full(MonOpRequestRef op)
2451{
2452 op->mark_osdmon_event(__func__);
2453 MOSDFull *m = static_cast<MOSDFull*>(op->get_req());
2454 int from = m->get_orig_source().num();
2455 set<string> state;
2456 unsigned mask = CEPH_OSD_NEARFULL | CEPH_OSD_BACKFILLFULL | CEPH_OSD_FULL;
2457
2458 // check permissions, ignore if failed
2459 MonSession *session = m->get_session();
2460 if (!session)
2461 goto ignore;
2462 if (!session->is_capable("osd", MON_CAP_X)) {
2463 dout(0) << "MOSDFull from entity with insufficient privileges:"
2464 << session->caps << dendl;
2465 goto ignore;
2466 }
2467
2468 // ignore a full message from the osd instance that already went down
2469 if (!osdmap.exists(from)) {
2470 dout(7) << __func__ << " ignoring full message from nonexistent "
2471 << m->get_orig_source_inst() << dendl;
2472 goto ignore;
2473 }
2474 if ((!osdmap.is_up(from) &&
2475 osdmap.get_most_recent_inst(from) == m->get_orig_source_inst()) ||
2476 (osdmap.is_up(from) &&
2477 osdmap.get_inst(from) != m->get_orig_source_inst())) {
2478 dout(7) << __func__ << " ignoring full message from down "
2479 << m->get_orig_source_inst() << dendl;
2480 goto ignore;
2481 }
2482
2483 OSDMap::calc_state_set(osdmap.get_state(from), state);
2484
2485 if ((osdmap.get_state(from) & mask) == m->state) {
2486 dout(7) << __func__ << " state already " << state << " for osd." << from
2487 << " " << m->get_orig_source_inst() << dendl;
2488 _reply_map(op, m->version);
2489 goto ignore;
2490 }
2491
2492 dout(10) << __func__ << " want state " << state << " for osd." << from
2493 << " " << m->get_orig_source_inst() << dendl;
2494 return false;
2495
2496 ignore:
2497 return true;
2498}
2499
2500bool OSDMonitor::prepare_full(MonOpRequestRef op)
2501{
2502 op->mark_osdmon_event(__func__);
2503 const MOSDFull *m = static_cast<MOSDFull*>(op->get_req());
2504 const int from = m->get_orig_source().num();
2505
2506 const unsigned mask = CEPH_OSD_NEARFULL | CEPH_OSD_BACKFILLFULL | CEPH_OSD_FULL;
2507 const unsigned want_state = m->state & mask; // safety first
2508
2509 unsigned cur_state = osdmap.get_state(from);
2510 auto p = pending_inc.new_state.find(from);
2511 if (p != pending_inc.new_state.end()) {
2512 cur_state ^= p->second;
2513 }
2514 cur_state &= mask;
2515
2516 set<string> want_state_set, cur_state_set;
2517 OSDMap::calc_state_set(want_state, want_state_set);
2518 OSDMap::calc_state_set(cur_state, cur_state_set);
2519
2520 if (cur_state != want_state) {
2521 if (p != pending_inc.new_state.end()) {
2522 p->second &= ~mask;
2523 } else {
2524 pending_inc.new_state[from] = 0;
2525 }
2526 pending_inc.new_state[from] |= (osdmap.get_state(from) & mask) ^ want_state;
2527 dout(7) << __func__ << " osd." << from << " " << cur_state_set
2528 << " -> " << want_state_set << dendl;
2529 } else {
2530 dout(7) << __func__ << " osd." << from << " " << cur_state_set
2531 << " = wanted " << want_state_set << ", just waiting" << dendl;
2532 }
2533
2534 wait_for_finished_proposal(op, new C_ReplyMap(this, op, m->version));
2535 return true;
2536}
2537
2538// -------------
2539// alive
2540
2541bool OSDMonitor::preprocess_alive(MonOpRequestRef op)
2542{
2543 op->mark_osdmon_event(__func__);
2544 MOSDAlive *m = static_cast<MOSDAlive*>(op->get_req());
2545 int from = m->get_orig_source().num();
2546
2547 // check permissions, ignore if failed
2548 MonSession *session = m->get_session();
2549 if (!session)
2550 goto ignore;
2551 if (!session->is_capable("osd", MON_CAP_X)) {
2552 dout(0) << "attempt to send MOSDAlive from entity with insufficient privileges:"
2553 << session->caps << dendl;
2554 goto ignore;
2555 }
2556
2557 if (!osdmap.is_up(from) ||
2558 osdmap.get_inst(from) != m->get_orig_source_inst()) {
2559 dout(7) << "preprocess_alive ignoring alive message from down " << m->get_orig_source_inst() << dendl;
2560 goto ignore;
2561 }
2562
2563 if (osdmap.get_up_thru(from) >= m->want) {
2564 // yup.
2565 dout(7) << "preprocess_alive want up_thru " << m->want << " dup from " << m->get_orig_source_inst() << dendl;
2566 _reply_map(op, m->version);
2567 return true;
2568 }
2569
2570 dout(10) << "preprocess_alive want up_thru " << m->want
2571 << " from " << m->get_orig_source_inst() << dendl;
2572 return false;
2573
2574 ignore:
2575 return true;
2576}
2577
2578bool OSDMonitor::prepare_alive(MonOpRequestRef op)
2579{
2580 op->mark_osdmon_event(__func__);
2581 MOSDAlive *m = static_cast<MOSDAlive*>(op->get_req());
2582 int from = m->get_orig_source().num();
2583
2584 if (0) { // we probably don't care much about these
2585 mon->clog->debug() << m->get_orig_source_inst() << " alive";
2586 }
2587
2588 dout(7) << "prepare_alive want up_thru " << m->want << " have " << m->version
2589 << " from " << m->get_orig_source_inst() << dendl;
2590
2591 update_up_thru(from, m->version); // set to the latest map the OSD has
2592 wait_for_finished_proposal(op, new C_ReplyMap(this, op, m->version));
2593 return true;
2594}
2595
2596void OSDMonitor::_reply_map(MonOpRequestRef op, epoch_t e)
2597{
2598 op->mark_osdmon_event(__func__);
2599 dout(7) << "_reply_map " << e
2600 << " from " << op->get_req()->get_orig_source_inst()
2601 << dendl;
2602 send_latest(op, e);
2603}
2604
2605// pg_created
2606bool OSDMonitor::preprocess_pg_created(MonOpRequestRef op)
2607{
2608 op->mark_osdmon_event(__func__);
2609 auto m = static_cast<MOSDPGCreated*>(op->get_req());
2610 dout(10) << __func__ << " " << *m << dendl;
2611 auto session = m->get_session();
2612 if (!session) {
2613 dout(10) << __func__ << ": no monitor session!" << dendl;
2614 return true;
2615 }
2616 if (!session->is_capable("osd", MON_CAP_X)) {
2617 derr << __func__ << " received from entity "
2618 << "with insufficient privileges " << session->caps << dendl;
2619 return true;
2620 }
2621 // always forward the "created!" to the leader
2622 return false;
2623}
2624
2625bool OSDMonitor::prepare_pg_created(MonOpRequestRef op)
2626{
2627 op->mark_osdmon_event(__func__);
2628 auto m = static_cast<MOSDPGCreated*>(op->get_req());
2629 dout(10) << __func__ << " " << *m << dendl;
2630 auto src = m->get_orig_source();
2631 auto from = src.num();
2632 if (!src.is_osd() ||
2633 !mon->osdmon()->osdmap.is_up(from) ||
2634 m->get_orig_source_inst() != mon->osdmon()->osdmap.get_inst(from)) {
2635 dout(1) << __func__ << " ignoring stats from non-active osd." << dendl;
2636 return false;
2637 }
2638 pending_created_pgs.push_back(m->pgid);
2639 return true;
2640}
2641
2642// -------------
2643// pg_temp changes
2644
2645bool OSDMonitor::preprocess_pgtemp(MonOpRequestRef op)
2646{
2647 MOSDPGTemp *m = static_cast<MOSDPGTemp*>(op->get_req());
2648 dout(10) << "preprocess_pgtemp " << *m << dendl;
2649 mempool::osdmap::vector<int> empty;
2650 int from = m->get_orig_source().num();
2651 size_t ignore_cnt = 0;
2652
2653 // check caps
2654 MonSession *session = m->get_session();
2655 if (!session)
2656 goto ignore;
2657 if (!session->is_capable("osd", MON_CAP_X)) {
2658 dout(0) << "attempt to send MOSDPGTemp from entity with insufficient caps "
2659 << session->caps << dendl;
2660 goto ignore;
2661 }
2662
2663 if (!osdmap.is_up(from) ||
2664 osdmap.get_inst(from) != m->get_orig_source_inst()) {
2665 dout(7) << "ignoring pgtemp message from down " << m->get_orig_source_inst() << dendl;
2666 goto ignore;
2667 }
2668
2669 for (auto p = m->pg_temp.begin(); p != m->pg_temp.end(); ++p) {
2670 dout(20) << " " << p->first
31f18b77 2671 << (osdmap.pg_temp->count(p->first) ? osdmap.pg_temp->get(p->first) : empty)
7c673cae
FG
2672 << " -> " << p->second << dendl;
2673
2674 // does the pool exist?
2675 if (!osdmap.have_pg_pool(p->first.pool())) {
2676 /*
2677 * 1. If the osdmap does not have the pool, it means the pool has been
2678 * removed in-between the osd sending this message and us handling it.
2679 * 2. If osdmap doesn't have the pool, it is safe to assume the pool does
2680 * not exist in the pending either, as the osds would not send a
2681 * message about a pool they know nothing about (yet).
2682 * 3. However, if the pool does exist in the pending, then it must be a
2683 * new pool, and not relevant to this message (see 1).
2684 */
2685 dout(10) << __func__ << " ignore " << p->first << " -> " << p->second
2686 << ": pool has been removed" << dendl;
2687 ignore_cnt++;
2688 continue;
2689 }
2690
2691 int acting_primary = -1;
2692 osdmap.pg_to_up_acting_osds(
2693 p->first, nullptr, nullptr, nullptr, &acting_primary);
2694 if (acting_primary != from) {
2695 /* If the source isn't the primary based on the current osdmap, we know
2696 * that the interval changed and that we can discard this message.
2697 * Indeed, we must do so to avoid 16127 since we can't otherwise determine
2698 * which of two pg temp mappings on the same pg is more recent.
2699 */
2700 dout(10) << __func__ << " ignore " << p->first << " -> " << p->second
2701 << ": primary has changed" << dendl;
2702 ignore_cnt++;
2703 continue;
2704 }
2705
2706 // removal?
2707 if (p->second.empty() && (osdmap.pg_temp->count(p->first) ||
2708 osdmap.primary_temp->count(p->first)))
2709 return false;
2710 // change?
2711 // NOTE: we assume that this will clear pg_primary, so consider
2712 // an existing pg_primary field to imply a change
2713 if (p->second.size() &&
2714 (osdmap.pg_temp->count(p->first) == 0 ||
31f18b77 2715 !vectors_equal(osdmap.pg_temp->get(p->first), p->second) ||
7c673cae
FG
2716 osdmap.primary_temp->count(p->first)))
2717 return false;
2718 }
2719
2720 // should we ignore all the pgs?
2721 if (ignore_cnt == m->pg_temp.size())
2722 goto ignore;
2723
2724 dout(7) << "preprocess_pgtemp e" << m->map_epoch << " no changes from " << m->get_orig_source_inst() << dendl;
2725 _reply_map(op, m->map_epoch);
2726 return true;
2727
2728 ignore:
2729 return true;
2730}
2731
2732void OSDMonitor::update_up_thru(int from, epoch_t up_thru)
2733{
2734 epoch_t old_up_thru = osdmap.get_up_thru(from);
2735 auto ut = pending_inc.new_up_thru.find(from);
2736 if (ut != pending_inc.new_up_thru.end()) {
2737 old_up_thru = ut->second;
2738 }
2739 if (up_thru > old_up_thru) {
2740 // set up_thru too, so the osd doesn't have to ask again
2741 pending_inc.new_up_thru[from] = up_thru;
2742 }
2743}
2744
2745bool OSDMonitor::prepare_pgtemp(MonOpRequestRef op)
2746{
2747 op->mark_osdmon_event(__func__);
2748 MOSDPGTemp *m = static_cast<MOSDPGTemp*>(op->get_req());
2749 int from = m->get_orig_source().num();
2750 dout(7) << "prepare_pgtemp e" << m->map_epoch << " from " << m->get_orig_source_inst() << dendl;
2751 for (map<pg_t,vector<int32_t> >::iterator p = m->pg_temp.begin(); p != m->pg_temp.end(); ++p) {
2752 uint64_t pool = p->first.pool();
2753 if (pending_inc.old_pools.count(pool)) {
2754 dout(10) << __func__ << " ignore " << p->first << " -> " << p->second
2755 << ": pool pending removal" << dendl;
2756 continue;
2757 }
2758 if (!osdmap.have_pg_pool(pool)) {
2759 dout(10) << __func__ << " ignore " << p->first << " -> " << p->second
2760 << ": pool has been removed" << dendl;
2761 continue;
2762 }
2763 pending_inc.new_pg_temp[p->first] =
2764 mempool::osdmap::vector<int>(p->second.begin(), p->second.end());
2765
2766 // unconditionally clear pg_primary (until this message can encode
2767 // a change for that, too.. at which point we need to also fix
2768 // preprocess_pg_temp)
2769 if (osdmap.primary_temp->count(p->first) ||
2770 pending_inc.new_primary_temp.count(p->first))
2771 pending_inc.new_primary_temp[p->first] = -1;
2772 }
2773
2774 // set up_thru too, so the osd doesn't have to ask again
2775 update_up_thru(from, m->map_epoch);
2776
2777 wait_for_finished_proposal(op, new C_ReplyMap(this, op, m->map_epoch));
2778 return true;
2779}
2780
2781
2782// ---
2783
2784bool OSDMonitor::preprocess_remove_snaps(MonOpRequestRef op)
2785{
2786 op->mark_osdmon_event(__func__);
2787 MRemoveSnaps *m = static_cast<MRemoveSnaps*>(op->get_req());
2788 dout(7) << "preprocess_remove_snaps " << *m << dendl;
2789
2790 // check privilege, ignore if failed
2791 MonSession *session = m->get_session();
2792 if (!session)
2793 goto ignore;
2794 if (!session->caps.is_capable(
2795 g_ceph_context,
2796 CEPH_ENTITY_TYPE_MON,
2797 session->entity_name,
2798 "osd", "osd pool rmsnap", {}, true, true, false)) {
2799 dout(0) << "got preprocess_remove_snaps from entity with insufficient caps "
2800 << session->caps << dendl;
2801 goto ignore;
2802 }
2803
2804 for (map<int, vector<snapid_t> >::iterator q = m->snaps.begin();
2805 q != m->snaps.end();
2806 ++q) {
2807 if (!osdmap.have_pg_pool(q->first)) {
2808 dout(10) << " ignoring removed_snaps " << q->second << " on non-existent pool " << q->first << dendl;
2809 continue;
2810 }
2811 const pg_pool_t *pi = osdmap.get_pg_pool(q->first);
2812 for (vector<snapid_t>::iterator p = q->second.begin();
2813 p != q->second.end();
2814 ++p) {
2815 if (*p > pi->get_snap_seq() ||
2816 !pi->removed_snaps.contains(*p))
2817 return false;
2818 }
2819 }
2820
2821 ignore:
2822 return true;
2823}
2824
2825bool OSDMonitor::prepare_remove_snaps(MonOpRequestRef op)
2826{
2827 op->mark_osdmon_event(__func__);
2828 MRemoveSnaps *m = static_cast<MRemoveSnaps*>(op->get_req());
2829 dout(7) << "prepare_remove_snaps " << *m << dendl;
2830
2831 for (map<int, vector<snapid_t> >::iterator p = m->snaps.begin();
2832 p != m->snaps.end();
2833 ++p) {
2834
2835 if (!osdmap.have_pg_pool(p->first)) {
2836 dout(10) << " ignoring removed_snaps " << p->second << " on non-existent pool " << p->first << dendl;
2837 continue;
2838 }
2839
2840 pg_pool_t& pi = osdmap.pools[p->first];
2841 for (vector<snapid_t>::iterator q = p->second.begin();
2842 q != p->second.end();
2843 ++q) {
2844 if (!pi.removed_snaps.contains(*q) &&
2845 (!pending_inc.new_pools.count(p->first) ||
2846 !pending_inc.new_pools[p->first].removed_snaps.contains(*q))) {
2847 pg_pool_t *newpi = pending_inc.get_new_pool(p->first, &pi);
2848 newpi->removed_snaps.insert(*q);
2849 dout(10) << " pool " << p->first << " removed_snaps added " << *q
2850 << " (now " << newpi->removed_snaps << ")" << dendl;
2851 if (*q > newpi->get_snap_seq()) {
2852 dout(10) << " pool " << p->first << " snap_seq " << newpi->get_snap_seq() << " -> " << *q << dendl;
2853 newpi->set_snap_seq(*q);
2854 }
2855 newpi->set_snap_epoch(pending_inc.epoch);
2856 }
2857 }
2858 }
2859 return true;
2860}
2861
2862// osd beacon
2863bool OSDMonitor::preprocess_beacon(MonOpRequestRef op)
2864{
2865 op->mark_osdmon_event(__func__);
2866 auto beacon = static_cast<MOSDBeacon*>(op->get_req());
2867 // check caps
2868 auto session = beacon->get_session();
2869 if (!session) {
2870 dout(10) << __func__ << " no monitor session!" << dendl;
2871 return true;
2872 }
2873 if (!session->is_capable("osd", MON_CAP_X)) {
2874 derr << __func__ << " received from entity "
2875 << "with insufficient privileges " << session->caps << dendl;
2876 return true;
2877 }
2878 // Always forward the beacon to the leader, even if they are the same as
2879 // the old one. The leader will mark as down osds that haven't sent
2880 // beacon for a few minutes.
2881 return false;
2882}
2883
2884bool OSDMonitor::prepare_beacon(MonOpRequestRef op)
2885{
2886 op->mark_osdmon_event(__func__);
2887 const auto beacon = static_cast<MOSDBeacon*>(op->get_req());
2888 const auto src = beacon->get_orig_source();
2889 dout(10) << __func__ << " " << *beacon
2890 << " from " << src << dendl;
2891 int from = src.num();
2892
2893 if (!src.is_osd() ||
2894 !osdmap.is_up(from) ||
2895 beacon->get_orig_source_inst() != osdmap.get_inst(from)) {
2896 dout(1) << " ignoring beacon from non-active osd." << dendl;
2897 return false;
2898 }
2899
2900 last_osd_report[from] = ceph_clock_now();
2901 osd_epochs[from] = beacon->version;
2902
2903 for (const auto& pg : beacon->pgs) {
2904 last_epoch_clean.report(pg, beacon->min_last_epoch_clean);
2905 }
2906 return false;
2907}
2908
2909// ---------------
2910// map helpers
2911
2912void OSDMonitor::send_latest(MonOpRequestRef op, epoch_t start)
2913{
2914 op->mark_osdmon_event(__func__);
2915 dout(5) << "send_latest to " << op->get_req()->get_orig_source_inst()
2916 << " start " << start << dendl;
2917 if (start == 0)
2918 send_full(op);
2919 else
2920 send_incremental(op, start);
2921}
2922
2923
2924MOSDMap *OSDMonitor::build_latest_full()
2925{
2926 MOSDMap *r = new MOSDMap(mon->monmap->fsid);
2927 get_version_full(osdmap.get_epoch(), r->maps[osdmap.get_epoch()]);
2928 r->oldest_map = get_first_committed();
2929 r->newest_map = osdmap.get_epoch();
2930 return r;
2931}
2932
2933MOSDMap *OSDMonitor::build_incremental(epoch_t from, epoch_t to)
2934{
2935 dout(10) << "build_incremental [" << from << ".." << to << "]" << dendl;
2936 MOSDMap *m = new MOSDMap(mon->monmap->fsid);
2937 m->oldest_map = get_first_committed();
2938 m->newest_map = osdmap.get_epoch();
2939
2940 for (epoch_t e = to; e >= from && e > 0; e--) {
2941 bufferlist bl;
2942 int err = get_version(e, bl);
2943 if (err == 0) {
2944 assert(bl.length());
2945 // if (get_version(e, bl) > 0) {
2946 dout(20) << "build_incremental inc " << e << " "
2947 << bl.length() << " bytes" << dendl;
2948 m->incremental_maps[e] = bl;
2949 } else {
2950 assert(err == -ENOENT);
2951 assert(!bl.length());
2952 get_version_full(e, bl);
2953 if (bl.length() > 0) {
2954 //else if (get_version("full", e, bl) > 0) {
2955 dout(20) << "build_incremental full " << e << " "
2956 << bl.length() << " bytes" << dendl;
2957 m->maps[e] = bl;
2958 } else {
2959 ceph_abort(); // we should have all maps.
2960 }
2961 }
2962 }
2963 return m;
2964}
2965
2966void OSDMonitor::send_full(MonOpRequestRef op)
2967{
2968 op->mark_osdmon_event(__func__);
2969 dout(5) << "send_full to " << op->get_req()->get_orig_source_inst() << dendl;
2970 mon->send_reply(op, build_latest_full());
2971}
2972
2973void OSDMonitor::send_incremental(MonOpRequestRef op, epoch_t first)
2974{
2975 op->mark_osdmon_event(__func__);
2976
2977 MonSession *s = op->get_session();
2978 assert(s);
2979
2980 if (s->proxy_con &&
2981 s->proxy_con->has_feature(CEPH_FEATURE_MON_ROUTE_OSDMAP)) {
2982 // oh, we can tell the other mon to do it
2983 dout(10) << __func__ << " asking proxying mon to send_incremental from "
2984 << first << dendl;
2985 MRoute *r = new MRoute(s->proxy_tid, NULL);
2986 r->send_osdmap_first = first;
2987 s->proxy_con->send_message(r);
2988 op->mark_event("reply: send routed send_osdmap_first reply");
2989 } else {
2990 // do it ourselves
2991 send_incremental(first, s, false, op);
2992 }
2993}
2994
2995void OSDMonitor::send_incremental(epoch_t first,
2996 MonSession *session,
2997 bool onetime,
2998 MonOpRequestRef req)
2999{
3000 dout(5) << "send_incremental [" << first << ".." << osdmap.get_epoch() << "]"
3001 << " to " << session->inst << dendl;
3002
3003 if (first <= session->osd_epoch) {
31f18b77 3004 dout(10) << __func__ << " " << session->inst << " should already have epoch "
7c673cae
FG
3005 << session->osd_epoch << dendl;
3006 first = session->osd_epoch + 1;
3007 }
3008
3009 if (first < get_first_committed()) {
3010 first = get_first_committed();
3011 bufferlist bl;
3012 int err = get_version_full(first, bl);
3013 assert(err == 0);
3014 assert(bl.length());
3015
3016 dout(20) << "send_incremental starting with base full "
3017 << first << " " << bl.length() << " bytes" << dendl;
3018
3019 MOSDMap *m = new MOSDMap(osdmap.get_fsid());
3020 m->oldest_map = get_first_committed();
3021 m->newest_map = osdmap.get_epoch();
3022 m->maps[first] = bl;
3023
3024 if (req) {
3025 mon->send_reply(req, m);
3026 session->osd_epoch = first;
3027 return;
3028 } else {
3029 session->con->send_message(m);
3030 session->osd_epoch = first;
3031 }
3032 first++;
3033 }
3034
3035 while (first <= osdmap.get_epoch()) {
3036 epoch_t last = MIN(first + g_conf->osd_map_message_max - 1,
3037 osdmap.get_epoch());
3038 MOSDMap *m = build_incremental(first, last);
3039
3040 if (req) {
3041 // send some maps. it may not be all of them, but it will get them
3042 // started.
3043 mon->send_reply(req, m);
3044 } else {
3045 session->con->send_message(m);
3046 first = last + 1;
3047 }
3048 session->osd_epoch = last;
3049 if (onetime || req)
3050 break;
3051 }
3052}
3053
3054int OSDMonitor::get_version(version_t ver, bufferlist& bl)
3055{
3056 if (inc_osd_cache.lookup(ver, &bl)) {
3057 return 0;
3058 }
3059 int ret = PaxosService::get_version(ver, bl);
3060 if (!ret) {
3061 inc_osd_cache.add(ver, bl);
3062 }
3063 return ret;
3064}
3065
3066int OSDMonitor::get_version_full(version_t ver, bufferlist& bl)
3067{
3068 if (full_osd_cache.lookup(ver, &bl)) {
3069 return 0;
3070 }
3071 int ret = PaxosService::get_version_full(ver, bl);
3072 if (!ret) {
3073 full_osd_cache.add(ver, bl);
3074 }
3075 return ret;
3076}
3077
3078epoch_t OSDMonitor::blacklist(const entity_addr_t& a, utime_t until)
3079{
3080 dout(10) << "blacklist " << a << " until " << until << dendl;
3081 pending_inc.new_blacklist[a] = until;
3082 return pending_inc.epoch;
3083}
3084
3085
3086void OSDMonitor::check_osdmap_subs()
3087{
3088 dout(10) << __func__ << dendl;
3089 if (!osdmap.get_epoch()) {
3090 return;
3091 }
3092 auto osdmap_subs = mon->session_map.subs.find("osdmap");
3093 if (osdmap_subs == mon->session_map.subs.end()) {
3094 return;
3095 }
3096 auto p = osdmap_subs->second->begin();
3097 while (!p.end()) {
3098 auto sub = *p;
3099 ++p;
3100 check_osdmap_sub(sub);
3101 }
3102}
3103
3104void OSDMonitor::check_osdmap_sub(Subscription *sub)
3105{
3106 dout(10) << __func__ << " " << sub << " next " << sub->next
3107 << (sub->onetime ? " (onetime)":" (ongoing)") << dendl;
3108 if (sub->next <= osdmap.get_epoch()) {
3109 if (sub->next >= 1)
3110 send_incremental(sub->next, sub->session, sub->incremental_onetime);
3111 else
3112 sub->session->con->send_message(build_latest_full());
3113 if (sub->onetime)
3114 mon->session_map.remove_sub(sub);
3115 else
3116 sub->next = osdmap.get_epoch() + 1;
3117 }
3118}
3119
3120void OSDMonitor::check_pg_creates_subs()
3121{
3122 if (!mon->monmap->get_required_features().contains_all(
3123 ceph::features::mon::FEATURE_LUMINOUS)) {
3124 // PGMonitor takes care of this in pre-luminous era.
3125 return;
3126 }
3127 if (!osdmap.get_num_up_osds()) {
3128 return;
3129 }
3130 assert(osdmap.get_up_osd_features() & CEPH_FEATURE_MON_STATEFUL_SUB);
3131 mon->with_session_map([this](const MonSessionMap& session_map) {
3132 auto pg_creates_subs = session_map.subs.find("osd_pg_creates");
3133 if (pg_creates_subs == session_map.subs.end()) {
3134 return;
3135 }
3136 for (auto sub : *pg_creates_subs->second) {
3137 check_pg_creates_sub(sub);
3138 }
3139 });
3140}
3141
3142void OSDMonitor::check_pg_creates_sub(Subscription *sub)
3143{
3144 dout(20) << __func__ << " .. " << sub->session->inst << dendl;
3145 assert(sub->type == "osd_pg_creates");
3146 // only send these if the OSD is up. we will check_subs() when they do
3147 // come up so they will get the creates then.
3148 if (sub->session->inst.name.is_osd() &&
3149 mon->osdmon()->osdmap.is_up(sub->session->inst.name.num())) {
3150 sub->next = send_pg_creates(sub->session->inst.name.num(),
3151 sub->session->con.get(),
3152 sub->next);
3153 }
3154}
3155
c07f9fc5
FG
3156void OSDMonitor::do_application_enable(int64_t pool_id,
3157 const std::string &app_name)
3158{
35e4c445 3159 assert(paxos->is_plugged() && is_writeable());
c07f9fc5
FG
3160
3161 dout(20) << __func__ << ": pool_id=" << pool_id << ", app_name=" << app_name
3162 << dendl;
3163
35e4c445
FG
3164 assert(osdmap.require_osd_release >= CEPH_RELEASE_LUMINOUS ||
3165 pending_inc.new_require_osd_release >= CEPH_RELEASE_LUMINOUS);
3166
c07f9fc5
FG
3167 auto pp = osdmap.get_pg_pool(pool_id);
3168 assert(pp != nullptr);
3169
3170 pg_pool_t p = *pp;
3171 if (pending_inc.new_pools.count(pool_id)) {
3172 p = pending_inc.new_pools[pool_id];
3173 }
3174
3175 p.application_metadata.insert({app_name, {}});
3176 p.last_change = pending_inc.epoch;
3177 pending_inc.new_pools[pool_id] = p;
3178}
3179
31f18b77 3180unsigned OSDMonitor::scan_for_creating_pgs(
7c673cae
FG
3181 const mempool::osdmap::map<int64_t,pg_pool_t>& pools,
3182 const mempool::osdmap::set<int64_t>& removed_pools,
3183 utime_t modified,
3184 creating_pgs_t* creating_pgs) const
3185{
31f18b77 3186 unsigned queued = 0;
7c673cae
FG
3187 for (auto& p : pools) {
3188 int64_t poolid = p.first;
3189 const pg_pool_t& pool = p.second;
31f18b77 3190 int ruleno = osdmap.crush->find_rule(pool.get_crush_rule(),
7c673cae
FG
3191 pool.get_type(), pool.get_size());
3192 if (ruleno < 0 || !osdmap.crush->rule_exists(ruleno))
3193 continue;
3194
3195 const auto last_scan_epoch = creating_pgs->last_scan_epoch;
3196 const auto created = pool.get_last_change();
3197 if (last_scan_epoch && created <= last_scan_epoch) {
3198 dout(10) << __func__ << " no change in pool " << poolid
3199 << " " << pool << dendl;
3200 continue;
3201 }
3202 if (removed_pools.count(poolid)) {
3203 dout(10) << __func__ << " pool is being removed: " << poolid
3204 << " " << pool << dendl;
3205 continue;
3206 }
31f18b77 3207 dout(10) << __func__ << " queueing pool create for " << poolid
7c673cae 3208 << " " << pool << dendl;
31f18b77
FG
3209 if (creating_pgs->create_pool(poolid, pool.get_pg_num(),
3210 created, modified)) {
3211 queued++;
7c673cae
FG
3212 }
3213 }
31f18b77 3214 return queued;
7c673cae
FG
3215}
3216
3217void OSDMonitor::update_creating_pgs()
3218{
31f18b77
FG
3219 dout(10) << __func__ << " " << creating_pgs.pgs.size() << " pgs creating, "
3220 << creating_pgs.queue.size() << " pools in queue" << dendl;
7c673cae
FG
3221 decltype(creating_pgs_by_osd_epoch) new_pgs_by_osd_epoch;
3222 std::lock_guard<std::mutex> l(creating_pgs_lock);
c07f9fc5 3223 for (const auto& pg : creating_pgs.pgs) {
7c673cae
FG
3224 int acting_primary = -1;
3225 auto pgid = pg.first;
3226 auto mapped = pg.second.first;
c07f9fc5 3227 dout(20) << __func__ << " looking up " << pgid << "@" << mapped << dendl;
7c673cae
FG
3228 mapping.get(pgid, nullptr, nullptr, nullptr, &acting_primary);
3229 // check the previous creating_pgs, look for the target to whom the pg was
3230 // previously mapped
3231 for (const auto& pgs_by_epoch : creating_pgs_by_osd_epoch) {
3232 const auto last_acting_primary = pgs_by_epoch.first;
3233 for (auto& pgs: pgs_by_epoch.second) {
3234 if (pgs.second.count(pgid)) {
3235 if (last_acting_primary == acting_primary) {
3236 mapped = pgs.first;
3237 } else {
3238 dout(20) << __func__ << " " << pgid << " "
3239 << " acting_primary:" << last_acting_primary
3240 << " -> " << acting_primary << dendl;
3241 // note epoch if the target of the create message changed.
3242 mapped = mapping.get_epoch();
3243 }
3244 break;
31f18b77
FG
3245 } else {
3246 // newly creating
3247 mapped = mapping.get_epoch();
3248 }
7c673cae
FG
3249 }
3250 }
3251 dout(10) << __func__ << " will instruct osd." << acting_primary
c07f9fc5 3252 << " to create " << pgid << "@" << mapped << dendl;
7c673cae
FG
3253 new_pgs_by_osd_epoch[acting_primary][mapped].insert(pgid);
3254 }
3255 creating_pgs_by_osd_epoch = std::move(new_pgs_by_osd_epoch);
3256 creating_pgs_epoch = mapping.get_epoch();
3257}
3258
c07f9fc5 3259epoch_t OSDMonitor::send_pg_creates(int osd, Connection *con, epoch_t next) const
7c673cae
FG
3260{
3261 dout(30) << __func__ << " osd." << osd << " next=" << next
3262 << " " << creating_pgs_by_osd_epoch << dendl;
3263 std::lock_guard<std::mutex> l(creating_pgs_lock);
3264 auto creating_pgs_by_epoch = creating_pgs_by_osd_epoch.find(osd);
3265 if (creating_pgs_by_epoch == creating_pgs_by_osd_epoch.end())
3266 return next;
3267 assert(!creating_pgs_by_epoch->second.empty());
3268
3269 MOSDPGCreate *m = nullptr;
3270 epoch_t last = 0;
3271 for (auto epoch_pgs = creating_pgs_by_epoch->second.lower_bound(next);
3272 epoch_pgs != creating_pgs_by_epoch->second.end(); ++epoch_pgs) {
3273 auto epoch = epoch_pgs->first;
3274 auto& pgs = epoch_pgs->second;
3275 dout(20) << __func__ << " osd." << osd << " from " << next
3276 << " : epoch " << epoch << " " << pgs.size() << " pgs" << dendl;
3277 last = epoch;
3278 for (auto& pg : pgs) {
3279 if (!m)
3280 m = new MOSDPGCreate(creating_pgs_epoch);
3281 // Need the create time from the monitor using its clock to set
3282 // last_scrub_stamp upon pg creation.
c07f9fc5
FG
3283 auto create = creating_pgs.pgs.find(pg);
3284 assert(create != creating_pgs.pgs.end());
3285 m->mkpg.emplace(pg, pg_create_t{create->second.first, pg, 0});
3286 m->ctimes.emplace(pg, create->second.second);
7c673cae 3287 dout(20) << __func__ << " will create " << pg
c07f9fc5 3288 << " at " << create->second.first << dendl;
7c673cae
FG
3289 }
3290 }
3291 if (!m) {
3292 dout(20) << __func__ << " osd." << osd << " from " << next
3293 << " has nothing to send" << dendl;
3294 return next;
3295 }
3296 con->send_message(m);
3297 // sub is current through last + 1
3298 return last + 1;
3299}
3300
3301// TICK
3302
3303
3304void OSDMonitor::tick()
3305{
3306 if (!is_active()) return;
3307
3308 dout(10) << osdmap << dendl;
3309
3310 if (!mon->is_leader()) return;
3311
3312 bool do_propose = false;
3313 utime_t now = ceph_clock_now();
3314
31f18b77 3315 if (osdmap.require_osd_release >= CEPH_RELEASE_LUMINOUS &&
7c673cae
FG
3316 mon->monmap->get_required_features().contains_all(
3317 ceph::features::mon::FEATURE_LUMINOUS)) {
3318 if (handle_osd_timeouts(now, last_osd_report)) {
3319 do_propose = true;
3320 }
3321 }
3322
3323 // mark osds down?
3324 if (check_failures(now))
3325 do_propose = true;
3326
3327 // mark down osds out?
3328
3329 /* can_mark_out() checks if we can mark osds as being out. The -1 has no
3330 * influence at all. The decision is made based on the ratio of "in" osds,
3331 * and the function returns false if this ratio is lower that the minimum
3332 * ratio set by g_conf->mon_osd_min_in_ratio. So it's not really up to us.
3333 */
3334 if (can_mark_out(-1)) {
3335 set<int> down_cache; // quick cache of down subtrees
3336
3337 map<int,utime_t>::iterator i = down_pending_out.begin();
3338 while (i != down_pending_out.end()) {
3339 int o = i->first;
3340 utime_t down = now;
3341 down -= i->second;
3342 ++i;
3343
3344 if (osdmap.is_down(o) &&
3345 osdmap.is_in(o) &&
3346 can_mark_out(o)) {
3347 utime_t orig_grace(g_conf->mon_osd_down_out_interval, 0);
3348 utime_t grace = orig_grace;
3349 double my_grace = 0.0;
3350
3351 if (g_conf->mon_osd_adjust_down_out_interval) {
3352 // scale grace period the same way we do the heartbeat grace.
3353 const osd_xinfo_t& xi = osdmap.get_xinfo(o);
3354 double halflife = (double)g_conf->mon_osd_laggy_halflife;
3355 double decay_k = ::log(.5) / halflife;
3356 double decay = exp((double)down * decay_k);
3357 dout(20) << "osd." << o << " laggy halflife " << halflife << " decay_k " << decay_k
3358 << " down for " << down << " decay " << decay << dendl;
3359 my_grace = decay * (double)xi.laggy_interval * xi.laggy_probability;
3360 grace += my_grace;
3361 }
3362
3363 // is this an entire large subtree down?
3364 if (g_conf->mon_osd_down_out_subtree_limit.length()) {
3365 int type = osdmap.crush->get_type_id(g_conf->mon_osd_down_out_subtree_limit);
3366 if (type > 0) {
3367 if (osdmap.containing_subtree_is_down(g_ceph_context, o, type, &down_cache)) {
3368 dout(10) << "tick entire containing " << g_conf->mon_osd_down_out_subtree_limit
3369 << " subtree for osd." << o << " is down; resetting timer" << dendl;
3370 // reset timer, too.
3371 down_pending_out[o] = now;
3372 continue;
3373 }
3374 }
3375 }
3376
c07f9fc5
FG
3377 bool down_out = !osdmap.is_destroyed(o) &&
3378 g_conf->mon_osd_down_out_interval > 0 && down.sec() >= grace;
3379 bool destroyed_out = osdmap.is_destroyed(o) &&
3380 g_conf->mon_osd_destroyed_out_interval > 0 &&
3381 // this is not precise enough as we did not make a note when this osd
3382 // was marked as destroyed, but let's not bother with that
3383 // complexity for now.
3384 down.sec() >= g_conf->mon_osd_destroyed_out_interval;
3385 if (down_out || destroyed_out) {
7c673cae
FG
3386 dout(10) << "tick marking osd." << o << " OUT after " << down
3387 << " sec (target " << grace << " = " << orig_grace << " + " << my_grace << ")" << dendl;
3388 pending_inc.new_weight[o] = CEPH_OSD_OUT;
3389
3390 // set the AUTOOUT bit.
3391 if (pending_inc.new_state.count(o) == 0)
3392 pending_inc.new_state[o] = 0;
3393 pending_inc.new_state[o] |= CEPH_OSD_AUTOOUT;
3394
3395 // remember previous weight
3396 if (pending_inc.new_xinfo.count(o) == 0)
3397 pending_inc.new_xinfo[o] = osdmap.osd_xinfo[o];
3398 pending_inc.new_xinfo[o].old_weight = osdmap.osd_weight[o];
3399
3400 do_propose = true;
3401
224ce89b
WB
3402 mon->clog->info() << "Marking osd." << o << " out (has been down for "
3403 << int(down.sec()) << " seconds)";
7c673cae
FG
3404 } else
3405 continue;
3406 }
3407
3408 down_pending_out.erase(o);
3409 }
3410 } else {
3411 dout(10) << "tick NOOUT flag set, not checking down osds" << dendl;
3412 }
3413
3414 // expire blacklisted items?
3415 for (ceph::unordered_map<entity_addr_t,utime_t>::iterator p = osdmap.blacklist.begin();
3416 p != osdmap.blacklist.end();
3417 ++p) {
3418 if (p->second < now) {
3419 dout(10) << "expiring blacklist item " << p->first << " expired " << p->second << " < now " << now << dendl;
3420 pending_inc.old_blacklist.push_back(p->first);
3421 do_propose = true;
3422 }
3423 }
3424
3425 // if map full setting has changed, get that info out there!
31f18b77
FG
3426 if (osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS &&
3427 mon->pgservice->is_readable()) {
7c673cae 3428 // for pre-luminous compat only!
31f18b77 3429 if (mon->pgservice->have_full_osds()) {
7c673cae
FG
3430 dout(5) << "There are full osds, setting full flag" << dendl;
3431 add_flag(CEPH_OSDMAP_FULL);
3432 } else if (osdmap.test_flag(CEPH_OSDMAP_FULL)){
3433 dout(10) << "No full osds, removing full flag" << dendl;
3434 remove_flag(CEPH_OSDMAP_FULL);
3435 }
3436
31f18b77 3437 if (mon->pgservice->have_nearfull_osds()) {
7c673cae
FG
3438 dout(5) << "There are near full osds, setting nearfull flag" << dendl;
3439 add_flag(CEPH_OSDMAP_NEARFULL);
3440 } else if (osdmap.test_flag(CEPH_OSDMAP_NEARFULL)){
3441 dout(10) << "No near full osds, removing nearfull flag" << dendl;
3442 remove_flag(CEPH_OSDMAP_NEARFULL);
3443 }
3444 if (pending_inc.new_flags != -1 &&
3445 (pending_inc.new_flags ^ osdmap.flags) & (CEPH_OSDMAP_FULL | CEPH_OSDMAP_NEARFULL)) {
3446 dout(1) << "New setting for" <<
3447 (pending_inc.new_flags & CEPH_OSDMAP_FULL ? " CEPH_OSDMAP_FULL" : "") <<
3448 (pending_inc.new_flags & CEPH_OSDMAP_NEARFULL ? " CEPH_OSDMAP_NEARFULL" : "")
3449 << " -- doing propose" << dendl;
3450 do_propose = true;
3451 }
3452 }
3453
3454 if (update_pools_status())
3455 do_propose = true;
3456
3457 if (do_propose ||
3458 !pending_inc.new_pg_temp.empty()) // also propose if we adjusted pg_temp
3459 propose_pending();
3460}
3461
3462bool OSDMonitor::handle_osd_timeouts(const utime_t &now,
3463 std::map<int,utime_t> &last_osd_report)
3464{
3465 utime_t timeo(g_conf->mon_osd_report_timeout, 0);
3466 if (now - mon->get_leader_since() < timeo) {
3467 // We haven't been the leader for long enough to consider OSD timeouts
3468 return false;
3469 }
3470
3471 int max_osd = osdmap.get_max_osd();
3472 bool new_down = false;
3473
3474 for (int i=0; i < max_osd; ++i) {
3475 dout(30) << __func__ << ": checking up on osd " << i << dendl;
c07f9fc5
FG
3476 if (!osdmap.exists(i)) {
3477 last_osd_report.erase(i); // if any
3478 continue;
3479 }
7c673cae
FG
3480 if (!osdmap.is_up(i))
3481 continue;
3482 const std::map<int,utime_t>::const_iterator t = last_osd_report.find(i);
3483 if (t == last_osd_report.end()) {
3484 // it wasn't in the map; start the timer.
3485 last_osd_report[i] = now;
3486 } else if (can_mark_down(i)) {
3487 utime_t diff = now - t->second;
3488 if (diff > timeo) {
31f18b77
FG
3489 mon->clog->info() << "osd." << i << " marked down after no beacon for "
3490 << diff << " seconds";
3491 derr << "no beacon from osd." << i << " since " << t->second
3492 << ", " << diff << " seconds ago. marking down" << dendl;
7c673cae
FG
3493 pending_inc.new_state[i] = CEPH_OSD_UP;
3494 new_down = true;
3495 }
3496 }
3497 }
3498 return new_down;
3499}
3500
3501void OSDMonitor::get_health(list<pair<health_status_t,string> >& summary,
3502 list<pair<health_status_t,string> > *detail,
3503 CephContext *cct) const
3504{
3505 int num_osds = osdmap.get_num_osds();
3506
3507 if (num_osds == 0) {
3508 summary.push_back(make_pair(HEALTH_ERR, "no osds"));
3509 } else {
3510 int num_in_osds = 0;
3511 int num_down_in_osds = 0;
3512 set<int> osds;
31f18b77
FG
3513 set<int> down_in_osds;
3514 set<int> up_in_osds;
3515 set<int> subtree_up;
3516 unordered_map<int, set<int> > subtree_type_down;
3517 unordered_map<int, int> num_osds_subtree;
3518 int max_type = osdmap.crush->get_max_type_id();
3519
7c673cae
FG
3520 for (int i = 0; i < osdmap.get_max_osd(); i++) {
3521 if (!osdmap.exists(i)) {
3522 if (osdmap.crush->item_exists(i)) {
3523 osds.insert(i);
3524 }
31f18b77 3525 continue;
224ce89b 3526 }
7c673cae
FG
3527 if (osdmap.is_out(i))
3528 continue;
3529 ++num_in_osds;
31f18b77
FG
3530 if (down_in_osds.count(i) || up_in_osds.count(i))
3531 continue;
7c673cae 3532 if (!osdmap.is_up(i)) {
31f18b77
FG
3533 down_in_osds.insert(i);
3534 int parent_id = 0;
3535 int current = i;
3536 for (int type = 0; type <= max_type; type++) {
3537 if (!osdmap.crush->get_type_name(type))
3538 continue;
3539 int r = osdmap.crush->get_immediate_parent_id(current, &parent_id);
3540 if (r == -ENOENT)
3541 break;
3542 // break early if this parent is already marked as up
3543 if (subtree_up.count(parent_id))
3544 break;
3545 type = osdmap.crush->get_bucket_type(parent_id);
3546 if (!osdmap.subtree_type_is_down(
3547 g_ceph_context, parent_id, type,
3548 &down_in_osds, &up_in_osds, &subtree_up, &subtree_type_down))
3549 break;
3550 current = parent_id;
3551 }
3552 }
3553 }
3554
3555 // calculate the number of down osds in each down subtree and
3556 // store it in num_osds_subtree
3557 for (int type = 1; type <= max_type; type++) {
3558 if (!osdmap.crush->get_type_name(type))
3559 continue;
3560 for (auto j = subtree_type_down[type].begin();
3561 j != subtree_type_down[type].end();
3562 ++j) {
3563 if (type == 1) {
3564 list<int> children;
3565 int num = osdmap.crush->get_children(*j, &children);
3566 num_osds_subtree[*j] = num;
3567 } else {
3568 list<int> children;
3569 int num = 0;
3570 int num_children = osdmap.crush->get_children(*j, &children);
3571 if (num_children == 0)
3572 continue;
3573 for (auto l = children.begin(); l != children.end(); ++l) {
3574 if (num_osds_subtree[*l] > 0) {
3575 num = num + num_osds_subtree[*l];
3576 }
3577 }
3578 num_osds_subtree[*j] = num;
7c673cae
FG
3579 }
3580 }
3581 }
31f18b77 3582 num_down_in_osds = down_in_osds.size();
7c673cae
FG
3583 assert(num_down_in_osds <= num_in_osds);
3584 if (num_down_in_osds > 0) {
31f18b77
FG
3585 // summary of down subtree types and osds
3586 for (int type = max_type; type > 0; type--) {
3587 if (!osdmap.crush->get_type_name(type))
3588 continue;
3589 if (subtree_type_down[type].size() > 0) {
3590 ostringstream ss;
3591 ss << subtree_type_down[type].size() << " "
3592 << osdmap.crush->get_type_name(type);
3593 if (subtree_type_down[type].size() > 1) {
3594 ss << "s";
3595 }
3596 int sum_down_osds = 0;
3597 for (auto j = subtree_type_down[type].begin();
3598 j != subtree_type_down[type].end();
3599 ++j) {
3600 sum_down_osds = sum_down_osds + num_osds_subtree[*j];
3601 }
3602 ss << " (" << sum_down_osds << " osds) down";
3603 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3604 }
3605 }
7c673cae 3606 ostringstream ss;
31f18b77 3607 ss << down_in_osds.size() << " osds down";
7c673cae 3608 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
31f18b77
FG
3609
3610 if (detail) {
3611 // details of down subtree types
3612 for (int type = max_type; type > 0; type--) {
3613 if (!osdmap.crush->get_type_name(type))
3614 continue;
3615 for (auto j = subtree_type_down[type].rbegin();
3616 j != subtree_type_down[type].rend();
3617 ++j) {
3618 ostringstream ss;
3619 ss << osdmap.crush->get_type_name(type);
3620 ss << " ";
3621 ss << osdmap.crush->get_item_name(*j);
3622 // at the top level, do not print location
3623 if (type != max_type) {
3624 ss << " (";
3625 ss << osdmap.crush->get_full_location_ordered_string(*j);
3626 ss << ")";
3627 }
3628 int num = num_osds_subtree[*j];
3629 ss << " (" << num << " osds)";
3630 ss << " is down";
3631 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3632 }
3633 }
3634 // details of down osds
3635 for (auto it = down_in_osds.begin(); it != down_in_osds.end(); ++it) {
3636 ostringstream ss;
3637 ss << "osd." << *it << " (";
3638 ss << osdmap.crush->get_full_location_ordered_string(*it);
3639 ss << ") is down";
3640 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3641 }
3642 }
7c673cae
FG
3643 }
3644
3645 if (!osds.empty()) {
3646 ostringstream ss;
31f18b77 3647 ss << osds.size() << " osds exist in the crush map but not in the osdmap";
7c673cae
FG
3648 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3649 if (detail) {
31f18b77 3650 ss << " (osds: " << osds << ")";
7c673cae
FG
3651 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3652 }
3653 }
3654
7c673cae
FG
3655 // note: we leave it to ceph-mgr to generate details health warnings
3656 // with actual osd utilizations
3657
3658 // warn about flags
3659 uint64_t warn_flags =
3660 CEPH_OSDMAP_FULL |
3661 CEPH_OSDMAP_PAUSERD |
3662 CEPH_OSDMAP_PAUSEWR |
3663 CEPH_OSDMAP_PAUSEREC |
3664 CEPH_OSDMAP_NOUP |
3665 CEPH_OSDMAP_NODOWN |
3666 CEPH_OSDMAP_NOIN |
3667 CEPH_OSDMAP_NOOUT |
3668 CEPH_OSDMAP_NOBACKFILL |
3669 CEPH_OSDMAP_NORECOVER |
3670 CEPH_OSDMAP_NOSCRUB |
3671 CEPH_OSDMAP_NODEEP_SCRUB |
3672 CEPH_OSDMAP_NOTIERAGENT |
3673 CEPH_OSDMAP_NOREBALANCE;
3674 if (osdmap.test_flag(warn_flags)) {
3675 ostringstream ss;
3676 ss << osdmap.get_flag_string(osdmap.get_flags() & warn_flags)
3677 << " flag(s) set";
3678 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3679 if (detail)
3680 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3681 }
3682
3683 // old crush tunables?
3684 if (g_conf->mon_warn_on_legacy_crush_tunables) {
3685 string min = osdmap.crush->get_min_required_version();
3686 if (min < g_conf->mon_crush_min_required_version) {
3687 ostringstream ss;
3688 ss << "crush map has legacy tunables (require " << min
3689 << ", min is " << g_conf->mon_crush_min_required_version << ")";
3690 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3691 if (detail) {
3692 ss << "; see http://docs.ceph.com/docs/master/rados/operations/crush-map/#tunables";
3693 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3694 }
3695 }
3696 }
3697 if (g_conf->mon_warn_on_crush_straw_calc_version_zero) {
3698 if (osdmap.crush->get_straw_calc_version() == 0) {
3699 ostringstream ss;
3700 ss << "crush map has straw_calc_version=0";
3701 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3702 if (detail) {
3703 ss << "; see http://docs.ceph.com/docs/master/rados/operations/crush-map/#tunables";
3704 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3705 }
3706 }
3707 }
3708
3709 // hit_set-less cache_mode?
3710 if (g_conf->mon_warn_on_cache_pools_without_hit_sets) {
3711 int problem_cache_pools = 0;
3712 for (map<int64_t, pg_pool_t>::const_iterator p = osdmap.pools.begin();
3713 p != osdmap.pools.end();
3714 ++p) {
3715 const pg_pool_t& info = p->second;
3716 if (info.cache_mode_requires_hit_set() &&
3717 info.hit_set_params.get_type() == HitSet::TYPE_NONE) {
3718 ++problem_cache_pools;
3719 if (detail) {
3720 ostringstream ss;
3721 ss << "pool '" << osdmap.get_pool_name(p->first)
3722 << "' with cache_mode " << info.get_cache_mode_name()
3723 << " needs hit_set_type to be set but it is not";
3724 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3725 }
3726 }
3727 }
3728 if (problem_cache_pools) {
3729 ostringstream ss;
3730 ss << problem_cache_pools << " cache pools are missing hit_sets";
3731 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3732 }
3733 }
3734
31f18b77
FG
3735 if (osdmap.crush->has_multirule_rulesets()) {
3736 ostringstream ss;
3737 ss << "CRUSH map contains multirule rulesets";
3738 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3739 if (detail) {
3740 ss << "; please manually fix the map";
3741 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3742 }
3743 }
3744
7c673cae
FG
3745 // Not using 'sortbitwise' and should be?
3746 if (!osdmap.test_flag(CEPH_OSDMAP_SORTBITWISE) &&
3747 (osdmap.get_up_osd_features() &
3748 CEPH_FEATURE_OSD_BITWISE_HOBJ_SORT)) {
3749 ostringstream ss;
3750 ss << "no legacy OSD present but 'sortbitwise' flag is not set";
3751 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3752 }
3753
3754 // Warn if 'mon_osd_down_out_interval' is set to zero.
3755 // Having this option set to zero on the leader acts much like the
3756 // 'noout' flag. It's hard to figure out what's going wrong with clusters
3757 // without the 'noout' flag set but acting like that just the same, so
3758 // we report a HEALTH_WARN in case this option is set to zero.
3759 // This is an ugly hack to get the warning out, but until we find a way
3760 // to spread global options throughout the mon cluster and have all mons
3761 // using a base set of the same options, we need to work around this sort
3762 // of things.
3763 // There's also the obvious drawback that if this is set on a single
3764 // monitor on a 3-monitor cluster, this warning will only be shown every
3765 // third monitor connection.
3766 if (g_conf->mon_warn_on_osd_down_out_interval_zero &&
3767 g_conf->mon_osd_down_out_interval == 0) {
3768 ostringstream ss;
3769 ss << "mon." << mon->name << " has mon_osd_down_out_interval set to 0";
3770 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3771 if (detail) {
3772 ss << "; this has the same effect as the 'noout' flag";
3773 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3774 }
3775 }
3776
3777 // warn about upgrade flags that can be set but are not.
3778 if (g_conf->mon_debug_no_require_luminous) {
3779 // ignore these checks
3780 } else if (HAVE_FEATURE(osdmap.get_up_osd_features(), SERVER_LUMINOUS) &&
31f18b77
FG
3781 osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
3782 string msg = "all OSDs are running luminous or later but"
3783 " require_osd_release < luminous";
7c673cae
FG
3784 summary.push_back(make_pair(HEALTH_WARN, msg));
3785 if (detail) {
3786 detail->push_back(make_pair(HEALTH_WARN, msg));
3787 }
3788 } else if (HAVE_FEATURE(osdmap.get_up_osd_features(), SERVER_KRAKEN) &&
31f18b77
FG
3789 osdmap.require_osd_release < CEPH_RELEASE_KRAKEN) {
3790 string msg = "all OSDs are running kraken or later but"
3791 " require_osd_release < kraken";
7c673cae
FG
3792 summary.push_back(make_pair(HEALTH_WARN, msg));
3793 if (detail) {
3794 detail->push_back(make_pair(HEALTH_WARN, msg));
3795 }
3796 } else if (HAVE_FEATURE(osdmap.get_up_osd_features(), SERVER_JEWEL) &&
31f18b77
FG
3797 osdmap.require_osd_release < CEPH_RELEASE_JEWEL) {
3798 string msg = "all OSDs are running jewel or later but"
3799 " require_osd_release < jewel";
7c673cae
FG
3800 summary.push_back(make_pair(HEALTH_WARN, msg));
3801 if (detail) {
3802 detail->push_back(make_pair(HEALTH_WARN, msg));
3803 }
3804 }
3805
224ce89b
WB
3806 for (auto it : osdmap.get_pools()) {
3807 const pg_pool_t &pool = it.second;
3808 if (pool.has_flag(pg_pool_t::FLAG_FULL)) {
3809 const string& pool_name = osdmap.get_pool_name(it.first);
3810 stringstream ss;
3811 ss << "pool '" << pool_name << "' is full";
3812 summary.push_back(make_pair(HEALTH_WARN, ss.str()));
3813 if (detail)
3814 detail->push_back(make_pair(HEALTH_WARN, ss.str()));
3815 }
3816 }
7c673cae
FG
3817 }
3818}
3819
3820void OSDMonitor::dump_info(Formatter *f)
3821{
3822 f->open_object_section("osdmap");
3823 osdmap.dump(f);
3824 f->close_section();
3825
3826 f->open_array_section("osd_metadata");
3827 for (int i=0; i<osdmap.get_max_osd(); ++i) {
3828 if (osdmap.exists(i)) {
3829 f->open_object_section("osd");
3830 f->dump_unsigned("id", i);
3831 dump_osd_metadata(i, f, NULL);
3832 f->close_section();
3833 }
3834 }
3835 f->close_section();
3836
3837 f->dump_unsigned("osdmap_first_committed", get_first_committed());
3838 f->dump_unsigned("osdmap_last_committed", get_last_committed());
3839
3840 f->open_object_section("crushmap");
3841 osdmap.crush->dump(f);
3842 f->close_section();
3843}
3844
3845namespace {
3846 enum osd_pool_get_choices {
3847 SIZE, MIN_SIZE, CRASH_REPLAY_INTERVAL,
31f18b77 3848 PG_NUM, PGP_NUM, CRUSH_RULE, HASHPSPOOL,
7c673cae
FG
3849 NODELETE, NOPGCHANGE, NOSIZECHANGE,
3850 WRITE_FADVISE_DONTNEED, NOSCRUB, NODEEP_SCRUB,
3851 HIT_SET_TYPE, HIT_SET_PERIOD, HIT_SET_COUNT, HIT_SET_FPP,
3852 USE_GMT_HITSET, AUID, TARGET_MAX_OBJECTS, TARGET_MAX_BYTES,
3853 CACHE_TARGET_DIRTY_RATIO, CACHE_TARGET_DIRTY_HIGH_RATIO,
3854 CACHE_TARGET_FULL_RATIO,
3855 CACHE_MIN_FLUSH_AGE, CACHE_MIN_EVICT_AGE,
3856 ERASURE_CODE_PROFILE, MIN_READ_RECENCY_FOR_PROMOTE,
3857 MIN_WRITE_RECENCY_FOR_PROMOTE, FAST_READ,
3858 HIT_SET_GRADE_DECAY_RATE, HIT_SET_SEARCH_LAST_N,
3859 SCRUB_MIN_INTERVAL, SCRUB_MAX_INTERVAL, DEEP_SCRUB_INTERVAL,
3860 RECOVERY_PRIORITY, RECOVERY_OP_PRIORITY, SCRUB_PRIORITY,
3861 COMPRESSION_MODE, COMPRESSION_ALGORITHM, COMPRESSION_REQUIRED_RATIO,
3862 COMPRESSION_MAX_BLOB_SIZE, COMPRESSION_MIN_BLOB_SIZE,
3863 CSUM_TYPE, CSUM_MAX_BLOCK, CSUM_MIN_BLOCK };
3864
3865 std::set<osd_pool_get_choices>
3866 subtract_second_from_first(const std::set<osd_pool_get_choices>& first,
3867 const std::set<osd_pool_get_choices>& second)
3868 {
3869 std::set<osd_pool_get_choices> result;
3870 std::set_difference(first.begin(), first.end(),
3871 second.begin(), second.end(),
3872 std::inserter(result, result.end()));
3873 return result;
3874 }
3875}
3876
3877
3878bool OSDMonitor::preprocess_command(MonOpRequestRef op)
3879{
3880 op->mark_osdmon_event(__func__);
3881 MMonCommand *m = static_cast<MMonCommand*>(op->get_req());
3882 int r = 0;
3883 bufferlist rdata;
3884 stringstream ss, ds;
3885
3886 map<string, cmd_vartype> cmdmap;
3887 if (!cmdmap_from_json(m->cmd, &cmdmap, ss)) {
3888 string rs = ss.str();
3889 mon->reply_command(op, -EINVAL, rs, get_last_committed());
3890 return true;
3891 }
3892
3893 MonSession *session = m->get_session();
3894 if (!session) {
3895 mon->reply_command(op, -EACCES, "access denied", get_last_committed());
3896 return true;
3897 }
3898
3899 string prefix;
3900 cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
3901
3902 string format;
3903 cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
3904 boost::scoped_ptr<Formatter> f(Formatter::create(format));
3905
3906 if (prefix == "osd stat") {
224ce89b 3907 osdmap.print_summary(f.get(), ds, "");
7c673cae
FG
3908 if (f)
3909 f->flush(rdata);
3910 else
3911 rdata.append(ds);
3912 }
3913 else if (prefix == "osd perf" ||
3914 prefix == "osd blocked-by") {
31f18b77
FG
3915 r = mon->pgservice->process_pg_command(prefix, cmdmap,
3916 osdmap, f.get(), &ss, &rdata);
7c673cae
FG
3917 }
3918 else if (prefix == "osd dump" ||
3919 prefix == "osd tree" ||
3920 prefix == "osd ls" ||
3921 prefix == "osd getmap" ||
31f18b77
FG
3922 prefix == "osd getcrushmap" ||
3923 prefix == "osd ls-tree") {
7c673cae
FG
3924 string val;
3925
3926 epoch_t epoch = 0;
3927 int64_t epochnum;
3928 cmd_getval(g_ceph_context, cmdmap, "epoch", epochnum, (int64_t)osdmap.get_epoch());
3929 epoch = epochnum;
3930
3931 bufferlist osdmap_bl;
3932 int err = get_version_full(epoch, osdmap_bl);
3933 if (err == -ENOENT) {
3934 r = -ENOENT;
3935 ss << "there is no map for epoch " << epoch;
3936 goto reply;
3937 }
3938 assert(err == 0);
3939 assert(osdmap_bl.length());
3940
3941 OSDMap *p;
3942 if (epoch == osdmap.get_epoch()) {
3943 p = &osdmap;
3944 } else {
3945 p = new OSDMap;
3946 p->decode(osdmap_bl);
3947 }
3948
224ce89b
WB
3949 auto sg = make_scope_guard([&] {
3950 if (p != &osdmap) {
3951 delete p;
3952 }
3953 });
3954
7c673cae
FG
3955 if (prefix == "osd dump") {
3956 stringstream ds;
3957 if (f) {
3958 f->open_object_section("osdmap");
3959 p->dump(f.get());
3960 f->close_section();
3961 f->flush(ds);
3962 } else {
3963 p->print(ds);
3964 }
3965 rdata.append(ds);
3966 if (!f)
3967 ds << " ";
3968 } else if (prefix == "osd ls") {
3969 if (f) {
3970 f->open_array_section("osds");
3971 for (int i = 0; i < osdmap.get_max_osd(); i++) {
3972 if (osdmap.exists(i)) {
3973 f->dump_int("osd", i);
3974 }
3975 }
3976 f->close_section();
3977 f->flush(ds);
3978 } else {
3979 bool first = true;
3980 for (int i = 0; i < osdmap.get_max_osd(); i++) {
3981 if (osdmap.exists(i)) {
3982 if (!first)
3983 ds << "\n";
3984 first = false;
3985 ds << i;
3986 }
3987 }
3988 }
3989 rdata.append(ds);
3990 } else if (prefix == "osd tree") {
31f18b77
FG
3991 vector<string> states;
3992 cmd_getval(g_ceph_context, cmdmap, "states", states);
3993 unsigned filter = 0;
3994 for (auto& s : states) {
3995 if (s == "up") {
3996 filter |= OSDMap::DUMP_UP;
3997 } else if (s == "down") {
3998 filter |= OSDMap::DUMP_DOWN;
3999 } else if (s == "in") {
4000 filter |= OSDMap::DUMP_IN;
4001 } else if (s == "out") {
4002 filter |= OSDMap::DUMP_OUT;
c07f9fc5
FG
4003 } else if (s == "destroyed") {
4004 filter |= OSDMap::DUMP_DESTROYED;
31f18b77
FG
4005 } else {
4006 ss << "unrecognized state '" << s << "'";
4007 r = -EINVAL;
4008 goto reply;
4009 }
4010 }
4011 if ((filter & (OSDMap::DUMP_IN|OSDMap::DUMP_OUT)) ==
c07f9fc5
FG
4012 (OSDMap::DUMP_IN|OSDMap::DUMP_OUT)) {
4013 ss << "cannot specify both 'in' and 'out'";
4014 r = -EINVAL;
4015 goto reply;
4016 }
4017 if (((filter & (OSDMap::DUMP_UP|OSDMap::DUMP_DOWN)) ==
4018 (OSDMap::DUMP_UP|OSDMap::DUMP_DOWN)) ||
4019 ((filter & (OSDMap::DUMP_UP|OSDMap::DUMP_DESTROYED)) ==
4020 (OSDMap::DUMP_UP|OSDMap::DUMP_DESTROYED)) ||
4021 ((filter & (OSDMap::DUMP_DOWN|OSDMap::DUMP_DESTROYED)) ==
4022 (OSDMap::DUMP_DOWN|OSDMap::DUMP_DESTROYED))) {
4023 ss << "can specify only one of 'up', 'down' and 'destroyed'";
31f18b77
FG
4024 r = -EINVAL;
4025 goto reply;
4026 }
7c673cae
FG
4027 if (f) {
4028 f->open_object_section("tree");
31f18b77 4029 p->print_tree(f.get(), NULL, filter);
7c673cae
FG
4030 f->close_section();
4031 f->flush(ds);
4032 } else {
31f18b77 4033 p->print_tree(NULL, &ds, filter);
7c673cae
FG
4034 }
4035 rdata.append(ds);
4036 } else if (prefix == "osd getmap") {
4037 rdata.append(osdmap_bl);
4038 ss << "got osdmap epoch " << p->get_epoch();
4039 } else if (prefix == "osd getcrushmap") {
4040 p->crush->encode(rdata, mon->get_quorum_con_features());
31f18b77
FG
4041 ss << p->get_crush_version();
4042 } else if (prefix == "osd ls-tree") {
4043 string bucket_name;
4044 cmd_getval(g_ceph_context, cmdmap, "name", bucket_name);
4045 set<int> osds;
4046 r = p->get_osds_by_bucket_name(bucket_name, &osds);
4047 if (r == -ENOENT) {
4048 ss << "\"" << bucket_name << "\" does not exist";
4049 goto reply;
4050 } else if (r < 0) {
4051 ss << "can not parse bucket name:\"" << bucket_name << "\"";
4052 goto reply;
4053 }
4054
4055 if (f) {
4056 f->open_array_section("osds");
4057 for (auto &i : osds) {
4058 if (osdmap.exists(i)) {
4059 f->dump_int("osd", i);
4060 }
4061 }
4062 f->close_section();
4063 f->flush(ds);
4064 } else {
4065 bool first = true;
4066 for (auto &i : osds) {
4067 if (osdmap.exists(i)) {
4068 if (!first)
4069 ds << "\n";
4070 first = false;
4071 ds << i;
4072 }
4073 }
4074 }
4075
4076 rdata.append(ds);
7c673cae 4077 }
7c673cae
FG
4078 } else if (prefix == "osd df") {
4079 string method;
4080 cmd_getval(g_ceph_context, cmdmap, "output_method", method);
31f18b77
FG
4081 print_osd_utilization(osdmap, mon->pgservice, ds,
4082 f.get(), method == "tree");
7c673cae
FG
4083 rdata.append(ds);
4084 } else if (prefix == "osd getmaxosd") {
4085 if (f) {
4086 f->open_object_section("getmaxosd");
4087 f->dump_unsigned("epoch", osdmap.get_epoch());
4088 f->dump_int("max_osd", osdmap.get_max_osd());
4089 f->close_section();
4090 f->flush(rdata);
4091 } else {
4092 ds << "max_osd = " << osdmap.get_max_osd() << " in epoch " << osdmap.get_epoch();
4093 rdata.append(ds);
4094 }
4095 } else if (prefix == "osd utilization") {
4096 string out;
4097 osdmap.summarize_mapping_stats(NULL, NULL, &out, f.get());
4098 if (f)
4099 f->flush(rdata);
4100 else
4101 rdata.append(out);
4102 r = 0;
4103 goto reply;
4104 } else if (prefix == "osd find") {
4105 int64_t osd;
4106 if (!cmd_getval(g_ceph_context, cmdmap, "id", osd)) {
4107 ss << "unable to parse osd id value '"
4108 << cmd_vartype_stringify(cmdmap["id"]) << "'";
4109 r = -EINVAL;
4110 goto reply;
4111 }
4112 if (!osdmap.exists(osd)) {
4113 ss << "osd." << osd << " does not exist";
4114 r = -ENOENT;
4115 goto reply;
4116 }
4117 string format;
4118 cmd_getval(g_ceph_context, cmdmap, "format", format);
4119 boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty", "json-pretty"));
4120 f->open_object_section("osd_location");
4121 f->dump_int("osd", osd);
4122 f->dump_stream("ip") << osdmap.get_addr(osd);
4123 f->open_object_section("crush_location");
4124 map<string,string> loc = osdmap.crush->get_full_location(osd);
4125 for (map<string,string>::iterator p = loc.begin(); p != loc.end(); ++p)
4126 f->dump_string(p->first.c_str(), p->second);
4127 f->close_section();
4128 f->close_section();
4129 f->flush(rdata);
4130 } else if (prefix == "osd metadata") {
4131 int64_t osd = -1;
4132 if (cmd_vartype_stringify(cmdmap["id"]).size() &&
4133 !cmd_getval(g_ceph_context, cmdmap, "id", osd)) {
4134 ss << "unable to parse osd id value '"
4135 << cmd_vartype_stringify(cmdmap["id"]) << "'";
4136 r = -EINVAL;
4137 goto reply;
4138 }
4139 if (osd >= 0 && !osdmap.exists(osd)) {
4140 ss << "osd." << osd << " does not exist";
4141 r = -ENOENT;
4142 goto reply;
4143 }
4144 string format;
4145 cmd_getval(g_ceph_context, cmdmap, "format", format);
4146 boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty", "json-pretty"));
4147 if (osd >= 0) {
4148 f->open_object_section("osd_metadata");
4149 f->dump_unsigned("id", osd);
4150 r = dump_osd_metadata(osd, f.get(), &ss);
4151 if (r < 0)
4152 goto reply;
4153 f->close_section();
4154 } else {
4155 r = 0;
4156 f->open_array_section("osd_metadata");
4157 for (int i=0; i<osdmap.get_max_osd(); ++i) {
4158 if (osdmap.exists(i)) {
4159 f->open_object_section("osd");
4160 f->dump_unsigned("id", i);
4161 r = dump_osd_metadata(i, f.get(), NULL);
4162 if (r == -EINVAL || r == -ENOENT) {
4163 // Drop error, continue to get other daemons' metadata
4164 dout(4) << "No metadata for osd." << i << dendl;
4165 r = 0;
4166 } else if (r < 0) {
4167 // Unexpected error
4168 goto reply;
4169 }
4170 f->close_section();
4171 }
4172 }
4173 f->close_section();
4174 }
4175 f->flush(rdata);
31f18b77
FG
4176 } else if (prefix == "osd versions") {
4177 if (!f)
4178 f.reset(Formatter::create("json-pretty"));
4179 count_metadata("ceph_version", f.get());
4180 f->flush(rdata);
4181 r = 0;
4182 } else if (prefix == "osd count-metadata") {
4183 if (!f)
4184 f.reset(Formatter::create("json-pretty"));
4185 string field;
4186 cmd_getval(g_ceph_context, cmdmap, "property", field);
4187 count_metadata(field, f.get());
4188 f->flush(rdata);
4189 r = 0;
7c673cae
FG
4190 } else if (prefix == "osd map") {
4191 string poolstr, objstr, namespacestr;
4192 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
4193 cmd_getval(g_ceph_context, cmdmap, "object", objstr);
4194 cmd_getval(g_ceph_context, cmdmap, "nspace", namespacestr);
4195
4196 int64_t pool = osdmap.lookup_pg_pool_name(poolstr.c_str());
4197 if (pool < 0) {
4198 ss << "pool " << poolstr << " does not exist";
4199 r = -ENOENT;
4200 goto reply;
4201 }
4202 object_locator_t oloc(pool, namespacestr);
4203 object_t oid(objstr);
4204 pg_t pgid = osdmap.object_locator_to_pg(oid, oloc);
4205 pg_t mpgid = osdmap.raw_pg_to_pg(pgid);
4206 vector<int> up, acting;
4207 int up_p, acting_p;
4208 osdmap.pg_to_up_acting_osds(mpgid, &up, &up_p, &acting, &acting_p);
4209
4210 string fullobjname;
4211 if (!namespacestr.empty())
4212 fullobjname = namespacestr + string("/") + oid.name;
4213 else
4214 fullobjname = oid.name;
4215 if (f) {
4216 f->open_object_section("osd_map");
4217 f->dump_unsigned("epoch", osdmap.get_epoch());
4218 f->dump_string("pool", poolstr);
4219 f->dump_int("pool_id", pool);
4220 f->dump_stream("objname") << fullobjname;
4221 f->dump_stream("raw_pgid") << pgid;
4222 f->dump_stream("pgid") << mpgid;
4223 f->open_array_section("up");
4224 for (vector<int>::iterator p = up.begin(); p != up.end(); ++p)
4225 f->dump_int("osd", *p);
4226 f->close_section();
4227 f->dump_int("up_primary", up_p);
4228 f->open_array_section("acting");
4229 for (vector<int>::iterator p = acting.begin(); p != acting.end(); ++p)
4230 f->dump_int("osd", *p);
4231 f->close_section();
4232 f->dump_int("acting_primary", acting_p);
4233 f->close_section(); // osd_map
4234 f->flush(rdata);
4235 } else {
4236 ds << "osdmap e" << osdmap.get_epoch()
4237 << " pool '" << poolstr << "' (" << pool << ")"
4238 << " object '" << fullobjname << "' ->"
4239 << " pg " << pgid << " (" << mpgid << ")"
4240 << " -> up (" << pg_vector_string(up) << ", p" << up_p << ") acting ("
4241 << pg_vector_string(acting) << ", p" << acting_p << ")";
4242 rdata.append(ds);
4243 }
4244
4245 } else if (prefix == "pg map") {
4246 pg_t pgid;
4247 string pgidstr;
4248 cmd_getval(g_ceph_context, cmdmap, "pgid", pgidstr);
4249 if (!pgid.parse(pgidstr.c_str())) {
4250 ss << "invalid pgid '" << pgidstr << "'";
4251 r = -EINVAL;
4252 goto reply;
4253 }
4254 vector<int> up, acting;
4255 if (!osdmap.have_pg_pool(pgid.pool())) {
4256 ss << "pg '" << pgidstr << "' does not exist";
4257 r = -ENOENT;
4258 goto reply;
4259 }
4260 pg_t mpgid = osdmap.raw_pg_to_pg(pgid);
4261 osdmap.pg_to_up_acting_osds(pgid, up, acting);
4262 if (f) {
4263 f->open_object_section("pg_map");
4264 f->dump_unsigned("epoch", osdmap.get_epoch());
4265 f->dump_stream("raw_pgid") << pgid;
4266 f->dump_stream("pgid") << mpgid;
4267 f->open_array_section("up");
4268 for (auto osd : up) {
4269 f->dump_int("up_osd", osd);
4270 }
4271 f->close_section();
4272 f->open_array_section("acting");
4273 for (auto osd : acting) {
4274 f->dump_int("acting_osd", osd);
4275 }
4276 f->close_section();
4277 f->close_section();
4278 f->flush(rdata);
4279 } else {
4280 ds << "osdmap e" << osdmap.get_epoch()
4281 << " pg " << pgid << " (" << mpgid << ")"
4282 << " -> up " << up << " acting " << acting;
4283 rdata.append(ds);
4284 }
4285 goto reply;
4286
224ce89b
WB
4287 } else if (prefix == "osd scrub" ||
4288 prefix == "osd deep-scrub" ||
4289 prefix == "osd repair") {
7c673cae
FG
4290 string whostr;
4291 cmd_getval(g_ceph_context, cmdmap, "who", whostr);
4292 vector<string> pvec;
4293 get_str_vec(prefix, pvec);
4294
224ce89b 4295 if (whostr == "*" || whostr == "all" || whostr == "any") {
7c673cae
FG
4296 ss << "osds ";
4297 int c = 0;
4298 for (int i = 0; i < osdmap.get_max_osd(); i++)
4299 if (osdmap.is_up(i)) {
4300 ss << (c++ ? "," : "") << i;
4301 mon->try_send_message(new MOSDScrub(osdmap.get_fsid(),
4302 pvec.back() == "repair",
4303 pvec.back() == "deep-scrub"),
4304 osdmap.get_inst(i));
4305 }
4306 r = 0;
4307 ss << " instructed to " << pvec.back();
4308 } else {
4309 long osd = parse_osd_id(whostr.c_str(), &ss);
4310 if (osd < 0) {
4311 r = -EINVAL;
4312 } else if (osdmap.is_up(osd)) {
4313 mon->try_send_message(new MOSDScrub(osdmap.get_fsid(),
4314 pvec.back() == "repair",
4315 pvec.back() == "deep-scrub"),
4316 osdmap.get_inst(osd));
4317 ss << "osd." << osd << " instructed to " << pvec.back();
4318 } else {
4319 ss << "osd." << osd << " is not up";
4320 r = -EAGAIN;
4321 }
4322 }
4323 } else if (prefix == "osd lspools") {
4324 int64_t auid;
4325 cmd_getval(g_ceph_context, cmdmap, "auid", auid, int64_t(0));
4326 if (f)
4327 f->open_array_section("pools");
4328 for (map<int64_t, pg_pool_t>::iterator p = osdmap.pools.begin();
4329 p != osdmap.pools.end();
4330 ++p) {
4331 if (!auid || p->second.auid == (uint64_t)auid) {
4332 if (f) {
4333 f->open_object_section("pool");
4334 f->dump_int("poolnum", p->first);
4335 f->dump_string("poolname", osdmap.pool_name[p->first]);
4336 f->close_section();
4337 } else {
4338 ds << p->first << ' ' << osdmap.pool_name[p->first] << ',';
4339 }
4340 }
4341 }
4342 if (f) {
4343 f->close_section();
4344 f->flush(ds);
4345 }
4346 rdata.append(ds);
4347 } else if (prefix == "osd blacklist ls") {
4348 if (f)
4349 f->open_array_section("blacklist");
4350
4351 for (ceph::unordered_map<entity_addr_t,utime_t>::iterator p = osdmap.blacklist.begin();
4352 p != osdmap.blacklist.end();
4353 ++p) {
4354 if (f) {
4355 f->open_object_section("entry");
4356 f->dump_stream("addr") << p->first;
4357 f->dump_stream("until") << p->second;
4358 f->close_section();
4359 } else {
4360 stringstream ss;
4361 string s;
4362 ss << p->first << " " << p->second;
4363 getline(ss, s);
4364 s += "\n";
4365 rdata.append(s);
4366 }
4367 }
4368 if (f) {
4369 f->close_section();
4370 f->flush(rdata);
4371 }
4372 ss << "listed " << osdmap.blacklist.size() << " entries";
4373
4374 } else if (prefix == "osd pool ls") {
4375 string detail;
4376 cmd_getval(g_ceph_context, cmdmap, "detail", detail);
4377 if (!f && detail == "detail") {
4378 ostringstream ss;
4379 osdmap.print_pools(ss);
4380 rdata.append(ss.str());
4381 } else {
4382 if (f)
4383 f->open_array_section("pools");
4384 for (map<int64_t,pg_pool_t>::const_iterator it = osdmap.get_pools().begin();
4385 it != osdmap.get_pools().end();
4386 ++it) {
4387 if (f) {
4388 if (detail == "detail") {
4389 f->open_object_section("pool");
4390 f->dump_string("pool_name", osdmap.get_pool_name(it->first));
4391 it->second.dump(f.get());
4392 f->close_section();
4393 } else {
4394 f->dump_string("pool_name", osdmap.get_pool_name(it->first));
4395 }
4396 } else {
4397 rdata.append(osdmap.get_pool_name(it->first) + "\n");
4398 }
4399 }
4400 if (f) {
4401 f->close_section();
4402 f->flush(rdata);
4403 }
4404 }
4405
4406 } else if (prefix == "osd crush get-tunable") {
4407 string tunable;
4408 cmd_getval(g_ceph_context, cmdmap, "tunable", tunable);
4409 ostringstream rss;
4410 if (f)
4411 f->open_object_section("tunable");
4412 if (tunable == "straw_calc_version") {
4413 if (f)
4414 f->dump_int(tunable.c_str(), osdmap.crush->get_straw_calc_version());
4415 else
4416 rss << osdmap.crush->get_straw_calc_version() << "\n";
4417 } else {
4418 r = -EINVAL;
4419 goto reply;
4420 }
4421 if (f) {
4422 f->close_section();
4423 f->flush(rdata);
4424 } else {
4425 rdata.append(rss.str());
4426 }
4427 r = 0;
4428
4429 } else if (prefix == "osd pool get") {
4430 string poolstr;
4431 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
4432 int64_t pool = osdmap.lookup_pg_pool_name(poolstr.c_str());
4433 if (pool < 0) {
4434 ss << "unrecognized pool '" << poolstr << "'";
4435 r = -ENOENT;
4436 goto reply;
4437 }
4438
4439 const pg_pool_t *p = osdmap.get_pg_pool(pool);
4440 string var;
4441 cmd_getval(g_ceph_context, cmdmap, "var", var);
4442
4443 typedef std::map<std::string, osd_pool_get_choices> choices_map_t;
4444 const choices_map_t ALL_CHOICES = {
4445 {"size", SIZE},
4446 {"min_size", MIN_SIZE},
4447 {"crash_replay_interval", CRASH_REPLAY_INTERVAL},
4448 {"pg_num", PG_NUM}, {"pgp_num", PGP_NUM},
4449 {"crush_rule", CRUSH_RULE},
7c673cae
FG
4450 {"hashpspool", HASHPSPOOL}, {"nodelete", NODELETE},
4451 {"nopgchange", NOPGCHANGE}, {"nosizechange", NOSIZECHANGE},
4452 {"noscrub", NOSCRUB}, {"nodeep-scrub", NODEEP_SCRUB},
4453 {"write_fadvise_dontneed", WRITE_FADVISE_DONTNEED},
4454 {"hit_set_type", HIT_SET_TYPE}, {"hit_set_period", HIT_SET_PERIOD},
4455 {"hit_set_count", HIT_SET_COUNT}, {"hit_set_fpp", HIT_SET_FPP},
4456 {"use_gmt_hitset", USE_GMT_HITSET},
4457 {"auid", AUID}, {"target_max_objects", TARGET_MAX_OBJECTS},
4458 {"target_max_bytes", TARGET_MAX_BYTES},
4459 {"cache_target_dirty_ratio", CACHE_TARGET_DIRTY_RATIO},
4460 {"cache_target_dirty_high_ratio", CACHE_TARGET_DIRTY_HIGH_RATIO},
4461 {"cache_target_full_ratio", CACHE_TARGET_FULL_RATIO},
4462 {"cache_min_flush_age", CACHE_MIN_FLUSH_AGE},
4463 {"cache_min_evict_age", CACHE_MIN_EVICT_AGE},
4464 {"erasure_code_profile", ERASURE_CODE_PROFILE},
4465 {"min_read_recency_for_promote", MIN_READ_RECENCY_FOR_PROMOTE},
4466 {"min_write_recency_for_promote", MIN_WRITE_RECENCY_FOR_PROMOTE},
4467 {"fast_read", FAST_READ},
4468 {"hit_set_grade_decay_rate", HIT_SET_GRADE_DECAY_RATE},
4469 {"hit_set_search_last_n", HIT_SET_SEARCH_LAST_N},
4470 {"scrub_min_interval", SCRUB_MIN_INTERVAL},
4471 {"scrub_max_interval", SCRUB_MAX_INTERVAL},
4472 {"deep_scrub_interval", DEEP_SCRUB_INTERVAL},
4473 {"recovery_priority", RECOVERY_PRIORITY},
4474 {"recovery_op_priority", RECOVERY_OP_PRIORITY},
4475 {"scrub_priority", SCRUB_PRIORITY},
4476 {"compression_mode", COMPRESSION_MODE},
4477 {"compression_algorithm", COMPRESSION_ALGORITHM},
4478 {"compression_required_ratio", COMPRESSION_REQUIRED_RATIO},
4479 {"compression_max_blob_size", COMPRESSION_MAX_BLOB_SIZE},
4480 {"compression_min_blob_size", COMPRESSION_MIN_BLOB_SIZE},
4481 {"csum_type", CSUM_TYPE},
4482 {"csum_max_block", CSUM_MAX_BLOCK},
4483 {"csum_min_block", CSUM_MIN_BLOCK},
4484 };
4485
4486 typedef std::set<osd_pool_get_choices> choices_set_t;
4487
4488 const choices_set_t ONLY_TIER_CHOICES = {
4489 HIT_SET_TYPE, HIT_SET_PERIOD, HIT_SET_COUNT, HIT_SET_FPP,
4490 TARGET_MAX_OBJECTS, TARGET_MAX_BYTES, CACHE_TARGET_FULL_RATIO,
4491 CACHE_TARGET_DIRTY_RATIO, CACHE_TARGET_DIRTY_HIGH_RATIO,
4492 CACHE_MIN_FLUSH_AGE, CACHE_MIN_EVICT_AGE,
4493 MIN_READ_RECENCY_FOR_PROMOTE,
c07f9fc5 4494 MIN_WRITE_RECENCY_FOR_PROMOTE,
7c673cae
FG
4495 HIT_SET_GRADE_DECAY_RATE, HIT_SET_SEARCH_LAST_N
4496 };
4497 const choices_set_t ONLY_ERASURE_CHOICES = {
4498 ERASURE_CODE_PROFILE
4499 };
4500
4501 choices_set_t selected_choices;
4502 if (var == "all") {
4503 for(choices_map_t::const_iterator it = ALL_CHOICES.begin();
4504 it != ALL_CHOICES.end(); ++it) {
4505 selected_choices.insert(it->second);
4506 }
4507
4508 if(!p->is_tier()) {
4509 selected_choices = subtract_second_from_first(selected_choices,
4510 ONLY_TIER_CHOICES);
4511 }
4512
4513 if(!p->is_erasure()) {
4514 selected_choices = subtract_second_from_first(selected_choices,
4515 ONLY_ERASURE_CHOICES);
4516 }
4517 } else /* var != "all" */ {
4518 choices_map_t::const_iterator found = ALL_CHOICES.find(var);
4519 osd_pool_get_choices selected = found->second;
4520
4521 if (!p->is_tier() &&
4522 ONLY_TIER_CHOICES.find(selected) != ONLY_TIER_CHOICES.end()) {
4523 ss << "pool '" << poolstr
4524 << "' is not a tier pool: variable not applicable";
4525 r = -EACCES;
4526 goto reply;
4527 }
4528
4529 if (!p->is_erasure() &&
4530 ONLY_ERASURE_CHOICES.find(selected)
4531 != ONLY_ERASURE_CHOICES.end()) {
4532 ss << "pool '" << poolstr
4533 << "' is not a erasure pool: variable not applicable";
4534 r = -EACCES;
4535 goto reply;
4536 }
4537
4538 selected_choices.insert(selected);
4539 }
4540
4541 if (f) {
4542 for(choices_set_t::const_iterator it = selected_choices.begin();
4543 it != selected_choices.end(); ++it) {
4544 choices_map_t::const_iterator i;
c07f9fc5
FG
4545 for (i = ALL_CHOICES.begin(); i != ALL_CHOICES.end(); ++i) {
4546 if (i->second == *it) {
4547 break;
4548 }
4549 }
4550 assert(i != ALL_CHOICES.end());
4551 bool pool_opt = pool_opts_t::is_opt_name(i->first);
4552 if (!pool_opt) {
4553 f->open_object_section("pool");
4554 f->dump_string("pool", poolstr);
4555 f->dump_int("pool_id", pool);
4556 }
7c673cae
FG
4557 switch(*it) {
4558 case PG_NUM:
4559 f->dump_int("pg_num", p->get_pg_num());
4560 break;
4561 case PGP_NUM:
4562 f->dump_int("pgp_num", p->get_pgp_num());
4563 break;
4564 case AUID:
4565 f->dump_int("auid", p->get_auid());
4566 break;
4567 case SIZE:
4568 f->dump_int("size", p->get_size());
4569 break;
4570 case MIN_SIZE:
4571 f->dump_int("min_size", p->get_min_size());
4572 break;
4573 case CRASH_REPLAY_INTERVAL:
4574 f->dump_int("crash_replay_interval",
4575 p->get_crash_replay_interval());
4576 break;
4577 case CRUSH_RULE:
31f18b77 4578 if (osdmap.crush->rule_exists(p->get_crush_rule())) {
7c673cae 4579 f->dump_string("crush_rule", osdmap.crush->get_rule_name(
31f18b77 4580 p->get_crush_rule()));
7c673cae 4581 } else {
31f18b77 4582 f->dump_string("crush_rule", stringify(p->get_crush_rule()));
7c673cae
FG
4583 }
4584 break;
7c673cae
FG
4585 case HASHPSPOOL:
4586 case NODELETE:
4587 case NOPGCHANGE:
4588 case NOSIZECHANGE:
4589 case WRITE_FADVISE_DONTNEED:
4590 case NOSCRUB:
4591 case NODEEP_SCRUB:
7c673cae
FG
4592 f->dump_string(i->first.c_str(),
4593 p->has_flag(pg_pool_t::get_flag_by_name(i->first)) ?
4594 "true" : "false");
4595 break;
4596 case HIT_SET_PERIOD:
4597 f->dump_int("hit_set_period", p->hit_set_period);
4598 break;
4599 case HIT_SET_COUNT:
4600 f->dump_int("hit_set_count", p->hit_set_count);
4601 break;
4602 case HIT_SET_TYPE:
4603 f->dump_string("hit_set_type",
4604 HitSet::get_type_name(p->hit_set_params.get_type()));
4605 break;
4606 case HIT_SET_FPP:
4607 {
4608 if (p->hit_set_params.get_type() == HitSet::TYPE_BLOOM) {
4609 BloomHitSet::Params *bloomp =
4610 static_cast<BloomHitSet::Params*>(p->hit_set_params.impl.get());
4611 f->dump_float("hit_set_fpp", bloomp->get_fpp());
4612 } else if(var != "all") {
4613 f->close_section();
4614 ss << "hit set is not of type Bloom; " <<
4615 "invalid to get a false positive rate!";
4616 r = -EINVAL;
4617 goto reply;
4618 }
4619 }
4620 break;
4621 case USE_GMT_HITSET:
4622 f->dump_bool("use_gmt_hitset", p->use_gmt_hitset);
4623 break;
4624 case TARGET_MAX_OBJECTS:
4625 f->dump_unsigned("target_max_objects", p->target_max_objects);
4626 break;
4627 case TARGET_MAX_BYTES:
4628 f->dump_unsigned("target_max_bytes", p->target_max_bytes);
4629 break;
4630 case CACHE_TARGET_DIRTY_RATIO:
4631 f->dump_unsigned("cache_target_dirty_ratio_micro",
4632 p->cache_target_dirty_ratio_micro);
4633 f->dump_float("cache_target_dirty_ratio",
4634 ((float)p->cache_target_dirty_ratio_micro/1000000));
4635 break;
4636 case CACHE_TARGET_DIRTY_HIGH_RATIO:
4637 f->dump_unsigned("cache_target_dirty_high_ratio_micro",
4638 p->cache_target_dirty_high_ratio_micro);
4639 f->dump_float("cache_target_dirty_high_ratio",
4640 ((float)p->cache_target_dirty_high_ratio_micro/1000000));
4641 break;
4642 case CACHE_TARGET_FULL_RATIO:
4643 f->dump_unsigned("cache_target_full_ratio_micro",
4644 p->cache_target_full_ratio_micro);
4645 f->dump_float("cache_target_full_ratio",
4646 ((float)p->cache_target_full_ratio_micro/1000000));
4647 break;
4648 case CACHE_MIN_FLUSH_AGE:
4649 f->dump_unsigned("cache_min_flush_age", p->cache_min_flush_age);
4650 break;
4651 case CACHE_MIN_EVICT_AGE:
4652 f->dump_unsigned("cache_min_evict_age", p->cache_min_evict_age);
4653 break;
4654 case ERASURE_CODE_PROFILE:
4655 f->dump_string("erasure_code_profile", p->erasure_code_profile);
4656 break;
4657 case MIN_READ_RECENCY_FOR_PROMOTE:
4658 f->dump_int("min_read_recency_for_promote",
4659 p->min_read_recency_for_promote);
4660 break;
4661 case MIN_WRITE_RECENCY_FOR_PROMOTE:
4662 f->dump_int("min_write_recency_for_promote",
4663 p->min_write_recency_for_promote);
4664 break;
4665 case FAST_READ:
4666 f->dump_int("fast_read", p->fast_read);
4667 break;
4668 case HIT_SET_GRADE_DECAY_RATE:
4669 f->dump_int("hit_set_grade_decay_rate",
4670 p->hit_set_grade_decay_rate);
4671 break;
4672 case HIT_SET_SEARCH_LAST_N:
4673 f->dump_int("hit_set_search_last_n",
4674 p->hit_set_search_last_n);
4675 break;
4676 case SCRUB_MIN_INTERVAL:
4677 case SCRUB_MAX_INTERVAL:
4678 case DEEP_SCRUB_INTERVAL:
4679 case RECOVERY_PRIORITY:
4680 case RECOVERY_OP_PRIORITY:
4681 case SCRUB_PRIORITY:
4682 case COMPRESSION_MODE:
4683 case COMPRESSION_ALGORITHM:
4684 case COMPRESSION_REQUIRED_RATIO:
4685 case COMPRESSION_MAX_BLOB_SIZE:
4686 case COMPRESSION_MIN_BLOB_SIZE:
4687 case CSUM_TYPE:
4688 case CSUM_MAX_BLOCK:
4689 case CSUM_MIN_BLOCK:
c07f9fc5
FG
4690 pool_opts_t::key_t key = pool_opts_t::get_opt_desc(i->first).key;
4691 if (p->opts.is_set(key)) {
4692 f->open_object_section("pool");
4693 f->dump_string("pool", poolstr);
4694 f->dump_int("pool_id", pool);
4695 if(*it == CSUM_TYPE) {
4696 int val;
4697 p->opts.get(pool_opts_t::CSUM_TYPE, &val);
4698 f->dump_string(i->first.c_str(), Checksummer::get_csum_type_string(val));
4699 } else {
4700 p->opts.dump(i->first, f.get());
4701 }
4702 f->close_section();
4703 f->flush(rdata);
7c673cae
FG
4704 }
4705 break;
4706 }
c07f9fc5
FG
4707 if (!pool_opt) {
4708 f->close_section();
4709 f->flush(rdata);
4710 }
7c673cae
FG
4711 }
4712
4713 } else /* !f */ {
4714 for(choices_set_t::const_iterator it = selected_choices.begin();
4715 it != selected_choices.end(); ++it) {
4716 choices_map_t::const_iterator i;
4717 switch(*it) {
4718 case PG_NUM:
4719 ss << "pg_num: " << p->get_pg_num() << "\n";
4720 break;
4721 case PGP_NUM:
4722 ss << "pgp_num: " << p->get_pgp_num() << "\n";
4723 break;
4724 case AUID:
4725 ss << "auid: " << p->get_auid() << "\n";
4726 break;
4727 case SIZE:
4728 ss << "size: " << p->get_size() << "\n";
4729 break;
4730 case MIN_SIZE:
4731 ss << "min_size: " << p->get_min_size() << "\n";
4732 break;
4733 case CRASH_REPLAY_INTERVAL:
4734 ss << "crash_replay_interval: " <<
4735 p->get_crash_replay_interval() << "\n";
4736 break;
4737 case CRUSH_RULE:
31f18b77 4738 if (osdmap.crush->rule_exists(p->get_crush_rule())) {
7c673cae 4739 ss << "crush_rule: " << osdmap.crush->get_rule_name(
31f18b77 4740 p->get_crush_rule()) << "\n";
7c673cae 4741 } else {
31f18b77 4742 ss << "crush_rule: " << p->get_crush_rule() << "\n";
7c673cae
FG
4743 }
4744 break;
7c673cae
FG
4745 case HIT_SET_PERIOD:
4746 ss << "hit_set_period: " << p->hit_set_period << "\n";
4747 break;
4748 case HIT_SET_COUNT:
4749 ss << "hit_set_count: " << p->hit_set_count << "\n";
4750 break;
4751 case HIT_SET_TYPE:
4752 ss << "hit_set_type: " <<
4753 HitSet::get_type_name(p->hit_set_params.get_type()) << "\n";
4754 break;
4755 case HIT_SET_FPP:
4756 {
4757 if (p->hit_set_params.get_type() == HitSet::TYPE_BLOOM) {
4758 BloomHitSet::Params *bloomp =
4759 static_cast<BloomHitSet::Params*>(p->hit_set_params.impl.get());
4760 ss << "hit_set_fpp: " << bloomp->get_fpp() << "\n";
4761 } else if(var != "all") {
4762 ss << "hit set is not of type Bloom; " <<
4763 "invalid to get a false positive rate!";
4764 r = -EINVAL;
4765 goto reply;
4766 }
4767 }
4768 break;
4769 case USE_GMT_HITSET:
4770 ss << "use_gmt_hitset: " << p->use_gmt_hitset << "\n";
4771 break;
4772 case TARGET_MAX_OBJECTS:
4773 ss << "target_max_objects: " << p->target_max_objects << "\n";
4774 break;
4775 case TARGET_MAX_BYTES:
4776 ss << "target_max_bytes: " << p->target_max_bytes << "\n";
4777 break;
4778 case CACHE_TARGET_DIRTY_RATIO:
4779 ss << "cache_target_dirty_ratio: "
4780 << ((float)p->cache_target_dirty_ratio_micro/1000000) << "\n";
4781 break;
4782 case CACHE_TARGET_DIRTY_HIGH_RATIO:
4783 ss << "cache_target_dirty_high_ratio: "
4784 << ((float)p->cache_target_dirty_high_ratio_micro/1000000) << "\n";
4785 break;
4786 case CACHE_TARGET_FULL_RATIO:
4787 ss << "cache_target_full_ratio: "
4788 << ((float)p->cache_target_full_ratio_micro/1000000) << "\n";
4789 break;
4790 case CACHE_MIN_FLUSH_AGE:
4791 ss << "cache_min_flush_age: " << p->cache_min_flush_age << "\n";
4792 break;
4793 case CACHE_MIN_EVICT_AGE:
4794 ss << "cache_min_evict_age: " << p->cache_min_evict_age << "\n";
4795 break;
4796 case ERASURE_CODE_PROFILE:
4797 ss << "erasure_code_profile: " << p->erasure_code_profile << "\n";
4798 break;
4799 case MIN_READ_RECENCY_FOR_PROMOTE:
4800 ss << "min_read_recency_for_promote: " <<
4801 p->min_read_recency_for_promote << "\n";
4802 break;
4803 case HIT_SET_GRADE_DECAY_RATE:
4804 ss << "hit_set_grade_decay_rate: " <<
4805 p->hit_set_grade_decay_rate << "\n";
4806 break;
4807 case HIT_SET_SEARCH_LAST_N:
4808 ss << "hit_set_search_last_n: " <<
4809 p->hit_set_search_last_n << "\n";
4810 break;
4811 case HASHPSPOOL:
4812 case NODELETE:
4813 case NOPGCHANGE:
4814 case NOSIZECHANGE:
4815 case WRITE_FADVISE_DONTNEED:
4816 case NOSCRUB:
4817 case NODEEP_SCRUB:
4818 for (i = ALL_CHOICES.begin(); i != ALL_CHOICES.end(); ++i) {
4819 if (i->second == *it)
4820 break;
4821 }
4822 assert(i != ALL_CHOICES.end());
4823 ss << i->first << ": " <<
4824 (p->has_flag(pg_pool_t::get_flag_by_name(i->first)) ?
4825 "true" : "false") << "\n";
4826 break;
4827 case MIN_WRITE_RECENCY_FOR_PROMOTE:
4828 ss << "min_write_recency_for_promote: " <<
4829 p->min_write_recency_for_promote << "\n";
4830 break;
4831 case FAST_READ:
4832 ss << "fast_read: " << p->fast_read << "\n";
4833 break;
4834 case SCRUB_MIN_INTERVAL:
4835 case SCRUB_MAX_INTERVAL:
4836 case DEEP_SCRUB_INTERVAL:
4837 case RECOVERY_PRIORITY:
4838 case RECOVERY_OP_PRIORITY:
4839 case SCRUB_PRIORITY:
4840 case COMPRESSION_MODE:
4841 case COMPRESSION_ALGORITHM:
4842 case COMPRESSION_REQUIRED_RATIO:
4843 case COMPRESSION_MAX_BLOB_SIZE:
4844 case COMPRESSION_MIN_BLOB_SIZE:
4845 case CSUM_TYPE:
4846 case CSUM_MAX_BLOCK:
4847 case CSUM_MIN_BLOCK:
4848 for (i = ALL_CHOICES.begin(); i != ALL_CHOICES.end(); ++i) {
4849 if (i->second == *it)
4850 break;
4851 }
4852 assert(i != ALL_CHOICES.end());
4853 {
4854 pool_opts_t::key_t key = pool_opts_t::get_opt_desc(i->first).key;
4855 if (p->opts.is_set(key)) {
4856 if(key == pool_opts_t::CSUM_TYPE) {
4857 int val;
4858 p->opts.get(key, &val);
4859 ss << i->first << ": " << Checksummer::get_csum_type_string(val) << "\n";
4860 } else {
4861 ss << i->first << ": " << p->opts.get(key) << "\n";
4862 }
4863 }
4864 }
4865 break;
4866 }
4867 rdata.append(ss.str());
4868 ss.str("");
4869 }
4870 }
4871 r = 0;
4872 } else if (prefix == "osd pool stats") {
31f18b77
FG
4873 r = mon->pgservice->process_pg_command(prefix, cmdmap,
4874 osdmap, f.get(), &ss, &rdata);
7c673cae
FG
4875 } else if (prefix == "osd pool get-quota") {
4876 string pool_name;
4877 cmd_getval(g_ceph_context, cmdmap, "pool", pool_name);
4878
4879 int64_t poolid = osdmap.lookup_pg_pool_name(pool_name);
4880 if (poolid < 0) {
4881 assert(poolid == -ENOENT);
4882 ss << "unrecognized pool '" << pool_name << "'";
4883 r = -ENOENT;
4884 goto reply;
4885 }
4886 const pg_pool_t *p = osdmap.get_pg_pool(poolid);
4887
4888 if (f) {
4889 f->open_object_section("pool_quotas");
4890 f->dump_string("pool_name", pool_name);
4891 f->dump_unsigned("pool_id", poolid);
4892 f->dump_unsigned("quota_max_objects", p->quota_max_objects);
4893 f->dump_unsigned("quota_max_bytes", p->quota_max_bytes);
4894 f->close_section();
4895 f->flush(rdata);
4896 } else {
4897 stringstream rs;
4898 rs << "quotas for pool '" << pool_name << "':\n"
4899 << " max objects: ";
4900 if (p->quota_max_objects == 0)
4901 rs << "N/A";
4902 else
4903 rs << si_t(p->quota_max_objects) << " objects";
4904 rs << "\n"
4905 << " max bytes : ";
4906 if (p->quota_max_bytes == 0)
4907 rs << "N/A";
4908 else
4909 rs << si_t(p->quota_max_bytes) << "B";
4910 rdata.append(rs.str());
4911 }
4912 rdata.append("\n");
4913 r = 0;
4914 } else if (prefix == "osd crush rule list" ||
4915 prefix == "osd crush rule ls") {
c07f9fc5
FG
4916 if (f) {
4917 f->open_array_section("rules");
4918 osdmap.crush->list_rules(f.get());
4919 f->close_section();
4920 f->flush(rdata);
4921 } else {
4922 ostringstream ss;
4923 osdmap.crush->list_rules(&ss);
4924 rdata.append(ss.str());
4925 }
7c673cae
FG
4926 } else if (prefix == "osd crush rule dump") {
4927 string name;
4928 cmd_getval(g_ceph_context, cmdmap, "name", name);
4929 string format;
4930 cmd_getval(g_ceph_context, cmdmap, "format", format);
4931 boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty", "json-pretty"));
4932 if (name == "") {
4933 f->open_array_section("rules");
4934 osdmap.crush->dump_rules(f.get());
4935 f->close_section();
4936 } else {
4937 int ruleno = osdmap.crush->get_rule_id(name);
4938 if (ruleno < 0) {
31f18b77 4939 ss << "unknown crush rule '" << name << "'";
7c673cae
FG
4940 r = ruleno;
4941 goto reply;
4942 }
4943 osdmap.crush->dump_rule(ruleno, f.get());
4944 }
4945 ostringstream rs;
4946 f->flush(rs);
4947 rs << "\n";
4948 rdata.append(rs.str());
4949 } else if (prefix == "osd crush dump") {
4950 string format;
4951 cmd_getval(g_ceph_context, cmdmap, "format", format);
4952 boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty", "json-pretty"));
4953 f->open_object_section("crush_map");
4954 osdmap.crush->dump(f.get());
4955 f->close_section();
4956 ostringstream rs;
4957 f->flush(rs);
4958 rs << "\n";
4959 rdata.append(rs.str());
4960 } else if (prefix == "osd crush show-tunables") {
4961 string format;
4962 cmd_getval(g_ceph_context, cmdmap, "format", format);
4963 boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty", "json-pretty"));
4964 f->open_object_section("crush_map_tunables");
4965 osdmap.crush->dump_tunables(f.get());
4966 f->close_section();
4967 ostringstream rs;
4968 f->flush(rs);
4969 rs << "\n";
4970 rdata.append(rs.str());
4971 } else if (prefix == "osd crush tree") {
c07f9fc5
FG
4972 string shadow;
4973 cmd_getval(g_ceph_context, cmdmap, "shadow", shadow);
4974 bool show_shadow = shadow == "--show-shadow";
4975 boost::scoped_ptr<Formatter> f(Formatter::create(format));
4976 if (f) {
4977 osdmap.crush->dump_tree(nullptr,
4978 f.get(),
4979 osdmap.get_pool_names(),
4980 show_shadow);
4981 f->flush(rdata);
4982 } else {
4983 ostringstream ss;
4984 osdmap.crush->dump_tree(&ss,
4985 nullptr,
4986 osdmap.get_pool_names(),
4987 show_shadow);
4988 rdata.append(ss.str());
4989 }
d2e6a577
FG
4990 } else if (prefix == "osd crush ls") {
4991 string name;
4992 if (!cmd_getval(g_ceph_context, cmdmap, "node", name)) {
4993 ss << "no node specified";
4994 r = -EINVAL;
4995 goto reply;
4996 }
4997 if (!osdmap.crush->name_exists(name)) {
4998 ss << "node '" << name << "' does not exist";
4999 r = -ENOENT;
5000 goto reply;
5001 }
5002 int id = osdmap.crush->get_item_id(name);
5003 list<int> result;
5004 if (id >= 0) {
5005 result.push_back(id);
5006 } else {
5007 int num = osdmap.crush->get_bucket_size(id);
5008 for (int i = 0; i < num; ++i) {
5009 result.push_back(osdmap.crush->get_bucket_item(id, i));
5010 }
5011 }
5012 if (f) {
5013 f->open_array_section("items");
5014 for (auto i : result) {
5015 f->dump_string("item", osdmap.crush->get_item_name(i));
5016 }
5017 f->close_section();
5018 f->flush(rdata);
5019 } else {
5020 ostringstream ss;
5021 for (auto i : result) {
5022 ss << osdmap.crush->get_item_name(i) << "\n";
5023 }
5024 rdata.append(ss.str());
5025 }
5026 r = 0;
7c673cae
FG
5027 } else if (prefix == "osd crush class ls") {
5028 boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty", "json-pretty"));
5029 f->open_array_section("crush_classes");
5030 for (auto i : osdmap.crush->class_name)
5031 f->dump_string("class", i.second);
5032 f->close_section();
5033 f->flush(rdata);
224ce89b
WB
5034 } else if (prefix == "osd crush class ls-osd") {
5035 string name;
5036 cmd_getval(g_ceph_context, cmdmap, "class", name);
5037 boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty", "json-pretty"));
5038 set<int> osds;
5039 osdmap.crush->get_devices_by_class(name, &osds);
5040 f->open_array_section("osds");
5041 for (auto& osd : osds)
5042 f->dump_int("osd", osd);
5043 f->close_section();
5044 f->flush(rdata);
7c673cae
FG
5045 } else if (prefix == "osd erasure-code-profile ls") {
5046 const auto &profiles = osdmap.get_erasure_code_profiles();
5047 if (f)
5048 f->open_array_section("erasure-code-profiles");
5049 for (auto i = profiles.begin(); i != profiles.end(); ++i) {
5050 if (f)
5051 f->dump_string("profile", i->first.c_str());
5052 else
5053 rdata.append(i->first + "\n");
5054 }
5055 if (f) {
5056 f->close_section();
5057 ostringstream rs;
5058 f->flush(rs);
5059 rs << "\n";
5060 rdata.append(rs.str());
5061 }
c07f9fc5
FG
5062 } else if (prefix == "osd crush weight-set ls") {
5063 boost::scoped_ptr<Formatter> f(Formatter::create(format));
5064 if (f) {
5065 f->open_array_section("weight_sets");
5066 if (osdmap.crush->have_choose_args(CrushWrapper::DEFAULT_CHOOSE_ARGS)) {
5067 f->dump_string("pool", "(compat)");
5068 }
5069 for (auto& i : osdmap.crush->choose_args) {
5070 if (i.first >= 0) {
5071 f->dump_string("pool", osdmap.get_pool_name(i.first));
5072 }
5073 }
5074 f->close_section();
5075 f->flush(rdata);
5076 } else {
5077 ostringstream rs;
5078 if (osdmap.crush->have_choose_args(CrushWrapper::DEFAULT_CHOOSE_ARGS)) {
5079 rs << "(compat)\n";
5080 }
5081 for (auto& i : osdmap.crush->choose_args) {
5082 if (i.first >= 0) {
5083 rs << osdmap.get_pool_name(i.first) << "\n";
5084 }
5085 }
5086 rdata.append(rs.str());
5087 }
5088 } else if (prefix == "osd crush weight-set dump") {
5089 boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty",
5090 "json-pretty"));
5091 osdmap.crush->dump_choose_args(f.get());
5092 f->flush(rdata);
7c673cae
FG
5093 } else if (prefix == "osd erasure-code-profile get") {
5094 string name;
5095 cmd_getval(g_ceph_context, cmdmap, "name", name);
5096 if (!osdmap.has_erasure_code_profile(name)) {
5097 ss << "unknown erasure code profile '" << name << "'";
5098 r = -ENOENT;
5099 goto reply;
5100 }
5101 const map<string,string> &profile = osdmap.get_erasure_code_profile(name);
5102 if (f)
5103 f->open_object_section("profile");
5104 for (map<string,string>::const_iterator i = profile.begin();
5105 i != profile.end();
5106 ++i) {
5107 if (f)
5108 f->dump_string(i->first.c_str(), i->second.c_str());
5109 else
5110 rdata.append(i->first + "=" + i->second + "\n");
5111 }
5112 if (f) {
5113 f->close_section();
5114 ostringstream rs;
5115 f->flush(rs);
5116 rs << "\n";
5117 rdata.append(rs.str());
5118 }
5119 } else {
5120 // try prepare update
5121 return false;
5122 }
5123
5124 reply:
5125 string rs;
5126 getline(ss, rs);
5127 mon->reply_command(op, r, rs, rdata, get_last_committed());
5128 return true;
5129}
5130
5131void OSDMonitor::update_pool_flags(int64_t pool_id, uint64_t flags)
5132{
5133 const pg_pool_t *pool = osdmap.get_pg_pool(pool_id);
5134 pending_inc.get_new_pool(pool_id, pool)->flags = flags;
5135}
5136
5137bool OSDMonitor::update_pools_status()
5138{
31f18b77 5139 if (!mon->pgservice->is_readable())
7c673cae
FG
5140 return false;
5141
5142 bool ret = false;
5143
5144 auto& pools = osdmap.get_pools();
5145 for (auto it = pools.begin(); it != pools.end(); ++it) {
31f18b77
FG
5146 const pool_stat_t *pstat = mon->pgservice->get_pool_stat(it->first);
5147 if (!pstat)
7c673cae 5148 continue;
31f18b77 5149 const object_stat_sum_t& sum = pstat->stats.sum;
7c673cae
FG
5150 const pg_pool_t &pool = it->second;
5151 const string& pool_name = osdmap.get_pool_name(it->first);
5152
5153 bool pool_is_full =
5154 (pool.quota_max_bytes > 0 && (uint64_t)sum.num_bytes >= pool.quota_max_bytes) ||
5155 (pool.quota_max_objects > 0 && (uint64_t)sum.num_objects >= pool.quota_max_objects);
5156
5157 if (pool.has_flag(pg_pool_t::FLAG_FULL)) {
5158 if (pool_is_full)
5159 continue;
5160
5161 mon->clog->info() << "pool '" << pool_name
5162 << "' no longer full; removing FULL flag";
5163
5164 update_pool_flags(it->first, pool.get_flags() & ~pg_pool_t::FLAG_FULL);
5165 ret = true;
5166 } else {
5167 if (!pool_is_full)
5168 continue;
5169
5170 if (pool.quota_max_bytes > 0 &&
5171 (uint64_t)sum.num_bytes >= pool.quota_max_bytes) {
5172 mon->clog->warn() << "pool '" << pool_name << "' is full"
5173 << " (reached quota's max_bytes: "
5174 << si_t(pool.quota_max_bytes) << ")";
5175 }
5176 if (pool.quota_max_objects > 0 &&
5177 (uint64_t)sum.num_objects >= pool.quota_max_objects) {
5178 mon->clog->warn() << "pool '" << pool_name << "' is full"
5179 << " (reached quota's max_objects: "
5180 << pool.quota_max_objects << ")";
5181 }
5182 update_pool_flags(it->first, pool.get_flags() | pg_pool_t::FLAG_FULL);
5183 ret = true;
5184 }
5185 }
5186 return ret;
5187}
5188
7c673cae
FG
5189int OSDMonitor::prepare_new_pool(MonOpRequestRef op)
5190{
5191 op->mark_osdmon_event(__func__);
5192 MPoolOp *m = static_cast<MPoolOp*>(op->get_req());
5193 dout(10) << "prepare_new_pool from " << m->get_connection() << dendl;
5194 MonSession *session = m->get_session();
5195 if (!session)
5196 return -EPERM;
5197 string erasure_code_profile;
5198 stringstream ss;
31f18b77 5199 string rule_name;
7c673cae 5200 if (m->auid)
31f18b77 5201 return prepare_new_pool(m->name, m->auid, m->crush_rule, rule_name,
7c673cae
FG
5202 0, 0,
5203 erasure_code_profile,
5204 pg_pool_t::TYPE_REPLICATED, 0, FAST_READ_OFF, &ss);
5205 else
31f18b77 5206 return prepare_new_pool(m->name, session->auid, m->crush_rule, rule_name,
7c673cae
FG
5207 0, 0,
5208 erasure_code_profile,
5209 pg_pool_t::TYPE_REPLICATED, 0, FAST_READ_OFF, &ss);
5210}
5211
5212int OSDMonitor::crush_rename_bucket(const string& srcname,
5213 const string& dstname,
5214 ostream *ss)
5215{
5216 int ret;
5217 //
5218 // Avoid creating a pending crush if it does not already exists and
5219 // the rename would fail.
5220 //
5221 if (!_have_pending_crush()) {
5222 ret = _get_stable_crush().can_rename_bucket(srcname,
5223 dstname,
5224 ss);
5225 if (ret)
5226 return ret;
5227 }
5228
5229 CrushWrapper newcrush;
5230 _get_pending_crush(newcrush);
5231
5232 ret = newcrush.rename_bucket(srcname,
5233 dstname,
5234 ss);
5235 if (ret)
5236 return ret;
5237
5238 pending_inc.crush.clear();
5239 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
5240 *ss << "renamed bucket " << srcname << " into " << dstname;
5241 return 0;
5242}
5243
5244void OSDMonitor::check_legacy_ec_plugin(const string& plugin, const string& profile) const
5245{
5246 string replacement = "";
5247
5248 if (plugin == "jerasure_generic" ||
5249 plugin == "jerasure_sse3" ||
5250 plugin == "jerasure_sse4" ||
5251 plugin == "jerasure_neon") {
5252 replacement = "jerasure";
5253 } else if (plugin == "shec_generic" ||
5254 plugin == "shec_sse3" ||
5255 plugin == "shec_sse4" ||
5256 plugin == "shec_neon") {
5257 replacement = "shec";
5258 }
5259
5260 if (replacement != "") {
5261 dout(0) << "WARNING: erasure coding profile " << profile << " uses plugin "
5262 << plugin << " that has been deprecated. Please use "
5263 << replacement << " instead." << dendl;
5264 }
5265}
5266
5267int OSDMonitor::normalize_profile(const string& profilename,
5268 ErasureCodeProfile &profile,
5269 bool force,
5270 ostream *ss)
5271{
5272 ErasureCodeInterfaceRef erasure_code;
5273 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
5274 ErasureCodeProfile::const_iterator plugin = profile.find("plugin");
5275 check_legacy_ec_plugin(plugin->second, profilename);
5276 int err = instance.factory(plugin->second,
5277 g_conf->get_val<std::string>("erasure_code_dir"),
5278 profile, &erasure_code, ss);
5279 if (err) {
5280 return err;
5281 }
5282
5283 err = erasure_code->init(profile, ss);
5284 if (err) {
5285 return err;
5286 }
5287
5288 auto it = profile.find("stripe_unit");
5289 if (it != profile.end()) {
5290 string err_str;
5291 uint32_t stripe_unit = strict_si_cast<uint32_t>(it->second.c_str(), &err_str);
5292 if (!err_str.empty()) {
5293 *ss << "could not parse stripe_unit '" << it->second
5294 << "': " << err_str << std::endl;
5295 return -EINVAL;
5296 }
5297 uint32_t data_chunks = erasure_code->get_data_chunk_count();
5298 uint32_t chunk_size = erasure_code->get_chunk_size(stripe_unit * data_chunks);
5299 if (chunk_size != stripe_unit) {
5300 *ss << "stripe_unit " << stripe_unit << " does not match ec profile "
5301 << "alignment. Would be padded to " << chunk_size
5302 << std::endl;
5303 return -EINVAL;
5304 }
5305 if ((stripe_unit % 4096) != 0 && !force) {
5306 *ss << "stripe_unit should be a multiple of 4096 bytes for best performance."
5307 << "use --force to override this check" << std::endl;
5308 return -EINVAL;
5309 }
5310 }
5311 return 0;
5312}
5313
31f18b77 5314int OSDMonitor::crush_rule_create_erasure(const string &name,
7c673cae 5315 const string &profile,
31f18b77 5316 int *rule,
7c673cae
FG
5317 ostream *ss)
5318{
5319 int ruleid = osdmap.crush->get_rule_id(name);
5320 if (ruleid != -ENOENT) {
31f18b77 5321 *rule = osdmap.crush->get_rule_mask_ruleset(ruleid);
7c673cae
FG
5322 return -EEXIST;
5323 }
5324
5325 CrushWrapper newcrush;
5326 _get_pending_crush(newcrush);
5327
5328 ruleid = newcrush.get_rule_id(name);
5329 if (ruleid != -ENOENT) {
31f18b77 5330 *rule = newcrush.get_rule_mask_ruleset(ruleid);
7c673cae
FG
5331 return -EALREADY;
5332 } else {
5333 ErasureCodeInterfaceRef erasure_code;
5334 int err = get_erasure_code(profile, &erasure_code, ss);
5335 if (err) {
5336 *ss << "failed to load plugin using profile " << profile << std::endl;
5337 return err;
5338 }
5339
224ce89b 5340 err = erasure_code->create_rule(name, newcrush, ss);
7c673cae
FG
5341 erasure_code.reset();
5342 if (err < 0)
5343 return err;
31f18b77 5344 *rule = err;
7c673cae
FG
5345 pending_inc.crush.clear();
5346 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
5347 return 0;
5348 }
5349}
5350
5351int OSDMonitor::get_erasure_code(const string &erasure_code_profile,
5352 ErasureCodeInterfaceRef *erasure_code,
5353 ostream *ss) const
5354{
5355 if (pending_inc.has_erasure_code_profile(erasure_code_profile))
5356 return -EAGAIN;
5357 ErasureCodeProfile profile =
5358 osdmap.get_erasure_code_profile(erasure_code_profile);
5359 ErasureCodeProfile::const_iterator plugin =
5360 profile.find("plugin");
5361 if (plugin == profile.end()) {
5362 *ss << "cannot determine the erasure code plugin"
5363 << " because there is no 'plugin' entry in the erasure_code_profile "
5364 << profile << std::endl;
5365 return -EINVAL;
5366 }
5367 check_legacy_ec_plugin(plugin->second, erasure_code_profile);
5368 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
5369 return instance.factory(plugin->second,
5370 g_conf->get_val<std::string>("erasure_code_dir"),
5371 profile, erasure_code, ss);
5372}
5373
5374int OSDMonitor::check_cluster_features(uint64_t features,
5375 stringstream &ss)
5376{
5377 stringstream unsupported_ss;
5378 int unsupported_count = 0;
5379 if ((mon->get_quorum_con_features() & features) != features) {
5380 unsupported_ss << "the monitor cluster";
5381 ++unsupported_count;
5382 }
5383
5384 set<int32_t> up_osds;
5385 osdmap.get_up_osds(up_osds);
5386 for (set<int32_t>::iterator it = up_osds.begin();
5387 it != up_osds.end(); ++it) {
5388 const osd_xinfo_t &xi = osdmap.get_xinfo(*it);
5389 if ((xi.features & features) != features) {
5390 if (unsupported_count > 0)
5391 unsupported_ss << ", ";
5392 unsupported_ss << "osd." << *it;
5393 unsupported_count ++;
5394 }
5395 }
5396
5397 if (unsupported_count > 0) {
5398 ss << "features " << features << " unsupported by: "
5399 << unsupported_ss.str();
5400 return -ENOTSUP;
5401 }
5402
5403 // check pending osd state, too!
5404 for (map<int32_t,osd_xinfo_t>::const_iterator p =
5405 pending_inc.new_xinfo.begin();
5406 p != pending_inc.new_xinfo.end(); ++p) {
5407 const osd_xinfo_t &xi = p->second;
5408 if ((xi.features & features) != features) {
5409 dout(10) << __func__ << " pending osd." << p->first
5410 << " features are insufficient; retry" << dendl;
5411 return -EAGAIN;
5412 }
5413 }
5414
5415 return 0;
5416}
5417
5418bool OSDMonitor::validate_crush_against_features(const CrushWrapper *newcrush,
5419 stringstream& ss)
5420{
5421 OSDMap::Incremental new_pending = pending_inc;
5422 ::encode(*newcrush, new_pending.crush, mon->get_quorum_con_features());
5423 OSDMap newmap;
5424 newmap.deepish_copy_from(osdmap);
5425 newmap.apply_incremental(new_pending);
5426
5427 // client compat
31f18b77 5428 if (newmap.require_min_compat_client > 0) {
7c673cae 5429 auto mv = newmap.get_min_compat_client();
31f18b77
FG
5430 if (mv > newmap.require_min_compat_client) {
5431 ss << "new crush map requires client version " << ceph_release_name(mv)
7c673cae 5432 << " but require_min_compat_client is "
31f18b77 5433 << ceph_release_name(newmap.require_min_compat_client);
7c673cae
FG
5434 return false;
5435 }
5436 }
5437
5438 // osd compat
5439 uint64_t features =
5440 newmap.get_features(CEPH_ENTITY_TYPE_MON, NULL) |
5441 newmap.get_features(CEPH_ENTITY_TYPE_OSD, NULL);
5442 stringstream features_ss;
5443 int r = check_cluster_features(features, features_ss);
5444 if (r) {
5445 ss << "Could not change CRUSH: " << features_ss.str();
5446 return false;
5447 }
5448
5449 return true;
5450}
5451
5452bool OSDMonitor::erasure_code_profile_in_use(
5453 const mempool::osdmap::map<int64_t, pg_pool_t> &pools,
5454 const string &profile,
5455 ostream *ss)
5456{
5457 bool found = false;
5458 for (map<int64_t, pg_pool_t>::const_iterator p = pools.begin();
5459 p != pools.end();
5460 ++p) {
5461 if (p->second.erasure_code_profile == profile) {
5462 *ss << osdmap.pool_name[p->first] << " ";
5463 found = true;
5464 }
5465 }
5466 if (found) {
5467 *ss << "pool(s) are using the erasure code profile '" << profile << "'";
5468 }
5469 return found;
5470}
5471
5472int OSDMonitor::parse_erasure_code_profile(const vector<string> &erasure_code_profile,
5473 map<string,string> *erasure_code_profile_map,
5474 ostream *ss)
5475{
5476 int r = get_json_str_map(g_conf->osd_pool_default_erasure_code_profile,
5477 *ss,
5478 erasure_code_profile_map);
5479 if (r)
5480 return r;
5481 assert((*erasure_code_profile_map).count("plugin"));
5482 string default_plugin = (*erasure_code_profile_map)["plugin"];
5483 map<string,string> user_map;
5484 for (vector<string>::const_iterator i = erasure_code_profile.begin();
5485 i != erasure_code_profile.end();
5486 ++i) {
5487 size_t equal = i->find('=');
5488 if (equal == string::npos) {
5489 user_map[*i] = string();
5490 (*erasure_code_profile_map)[*i] = string();
5491 } else {
5492 const string key = i->substr(0, equal);
5493 equal++;
5494 const string value = i->substr(equal);
5495 user_map[key] = value;
5496 (*erasure_code_profile_map)[key] = value;
5497 }
5498 }
5499
5500 if (user_map.count("plugin") && user_map["plugin"] != default_plugin)
5501 (*erasure_code_profile_map) = user_map;
5502
5503 return 0;
5504}
5505
5506int OSDMonitor::prepare_pool_size(const unsigned pool_type,
5507 const string &erasure_code_profile,
5508 unsigned *size, unsigned *min_size,
5509 ostream *ss)
5510{
5511 int err = 0;
5512 switch (pool_type) {
5513 case pg_pool_t::TYPE_REPLICATED:
5514 *size = g_conf->osd_pool_default_size;
5515 *min_size = g_conf->get_osd_pool_default_min_size();
5516 break;
5517 case pg_pool_t::TYPE_ERASURE:
5518 {
5519 ErasureCodeInterfaceRef erasure_code;
5520 err = get_erasure_code(erasure_code_profile, &erasure_code, ss);
5521 if (err == 0) {
5522 *size = erasure_code->get_chunk_count();
5523 *min_size = MIN(erasure_code->get_data_chunk_count() + 1, *size);
5524 }
5525 }
5526 break;
5527 default:
5528 *ss << "prepare_pool_size: " << pool_type << " is not a known pool type";
5529 err = -EINVAL;
5530 break;
5531 }
5532 return err;
5533}
5534
5535int OSDMonitor::prepare_pool_stripe_width(const unsigned pool_type,
5536 const string &erasure_code_profile,
5537 uint32_t *stripe_width,
5538 ostream *ss)
5539{
5540 int err = 0;
5541 switch (pool_type) {
5542 case pg_pool_t::TYPE_REPLICATED:
5543 // ignored
5544 break;
5545 case pg_pool_t::TYPE_ERASURE:
5546 {
5547 ErasureCodeProfile profile =
5548 osdmap.get_erasure_code_profile(erasure_code_profile);
5549 ErasureCodeInterfaceRef erasure_code;
5550 err = get_erasure_code(erasure_code_profile, &erasure_code, ss);
5551 if (err)
5552 break;
5553 uint32_t data_chunks = erasure_code->get_data_chunk_count();
5554 uint32_t stripe_unit = g_conf->osd_pool_erasure_code_stripe_unit;
5555 auto it = profile.find("stripe_unit");
5556 if (it != profile.end()) {
5557 string err_str;
5558 stripe_unit = strict_si_cast<uint32_t>(it->second.c_str(), &err_str);
5559 assert(err_str.empty());
5560 }
5561 *stripe_width = data_chunks *
5562 erasure_code->get_chunk_size(stripe_unit * data_chunks);
5563 }
5564 break;
5565 default:
5566 *ss << "prepare_pool_stripe_width: "
5567 << pool_type << " is not a known pool type";
5568 err = -EINVAL;
5569 break;
5570 }
5571 return err;
5572}
5573
31f18b77 5574int OSDMonitor::prepare_pool_crush_rule(const unsigned pool_type,
224ce89b
WB
5575 const string &erasure_code_profile,
5576 const string &rule_name,
5577 int *crush_rule,
5578 ostream *ss)
7c673cae
FG
5579{
5580
31f18b77 5581 if (*crush_rule < 0) {
7c673cae
FG
5582 switch (pool_type) {
5583 case pg_pool_t::TYPE_REPLICATED:
5584 {
31f18b77 5585 if (rule_name == "") {
224ce89b 5586 // Use default rule
31f18b77
FG
5587 *crush_rule = osdmap.crush->get_osd_pool_default_crush_replicated_ruleset(g_ceph_context);
5588 if (*crush_rule < 0) {
5589 // Errors may happen e.g. if no valid rule is available
5590 *ss << "No suitable CRUSH rule exists, check "
7c673cae
FG
5591 << "'osd pool default crush *' config options";
5592 return -ENOENT;
5593 }
5594 } else {
31f18b77 5595 return get_crush_rule(rule_name, crush_rule, ss);
7c673cae
FG
5596 }
5597 }
5598 break;
5599 case pg_pool_t::TYPE_ERASURE:
5600 {
31f18b77 5601 int err = crush_rule_create_erasure(rule_name,
7c673cae 5602 erasure_code_profile,
31f18b77 5603 crush_rule, ss);
7c673cae
FG
5604 switch (err) {
5605 case -EALREADY:
31f18b77
FG
5606 dout(20) << "prepare_pool_crush_rule: rule "
5607 << rule_name << " try again" << dendl;
7c673cae
FG
5608 // fall through
5609 case 0:
5610 // need to wait for the crush rule to be proposed before proceeding
5611 err = -EAGAIN;
5612 break;
5613 case -EEXIST:
5614 err = 0;
5615 break;
5616 }
5617 return err;
5618 }
5619 break;
5620 default:
31f18b77 5621 *ss << "prepare_pool_crush_rule: " << pool_type
7c673cae
FG
5622 << " is not a known pool type";
5623 return -EINVAL;
5624 break;
5625 }
5626 } else {
31f18b77
FG
5627 if (!osdmap.crush->ruleset_exists(*crush_rule)) {
5628 *ss << "CRUSH rule " << *crush_rule << " not found";
7c673cae
FG
5629 return -ENOENT;
5630 }
5631 }
5632
5633 return 0;
5634}
5635
31f18b77 5636int OSDMonitor::get_crush_rule(const string &rule_name,
224ce89b
WB
5637 int *crush_rule,
5638 ostream *ss)
7c673cae
FG
5639{
5640 int ret;
31f18b77 5641 ret = osdmap.crush->get_rule_id(rule_name);
7c673cae
FG
5642 if (ret != -ENOENT) {
5643 // found it, use it
31f18b77 5644 *crush_rule = ret;
7c673cae
FG
5645 } else {
5646 CrushWrapper newcrush;
5647 _get_pending_crush(newcrush);
5648
31f18b77 5649 ret = newcrush.get_rule_id(rule_name);
7c673cae
FG
5650 if (ret != -ENOENT) {
5651 // found it, wait for it to be proposed
31f18b77 5652 dout(20) << __func__ << ": rule " << rule_name
7c673cae
FG
5653 << " try again" << dendl;
5654 return -EAGAIN;
5655 } else {
224ce89b 5656 // Cannot find it , return error
31f18b77 5657 *ss << "specified rule " << rule_name << " doesn't exist";
7c673cae
FG
5658 return ret;
5659 }
5660 }
5661 return 0;
5662}
5663
5664/**
5665 * @param name The name of the new pool
5666 * @param auid The auid of the pool owner. Can be -1
31f18b77
FG
5667 * @param crush_rule The crush rule to use. If <0, will use the system default
5668 * @param crush_rule_name The crush rule to use, if crush_rulset <0
7c673cae
FG
5669 * @param pg_num The pg_num to use. If set to 0, will use the system default
5670 * @param pgp_num The pgp_num to use. If set to 0, will use the system default
5671 * @param erasure_code_profile The profile name in OSDMap to be used for erasure code
5672 * @param pool_type TYPE_ERASURE, or TYPE_REP
5673 * @param expected_num_objects expected number of objects on the pool
5674 * @param fast_read fast read type.
5675 * @param ss human readable error message, if any.
5676 *
5677 * @return 0 on success, negative errno on failure.
5678 */
5679int OSDMonitor::prepare_new_pool(string& name, uint64_t auid,
31f18b77
FG
5680 int crush_rule,
5681 const string &crush_rule_name,
7c673cae
FG
5682 unsigned pg_num, unsigned pgp_num,
5683 const string &erasure_code_profile,
5684 const unsigned pool_type,
5685 const uint64_t expected_num_objects,
5686 FastReadType fast_read,
5687 ostream *ss)
5688{
5689 if (name.length() == 0)
5690 return -EINVAL;
5691 if (pg_num == 0)
5692 pg_num = g_conf->osd_pool_default_pg_num;
5693 if (pgp_num == 0)
5694 pgp_num = g_conf->osd_pool_default_pgp_num;
5695 if (pg_num > (unsigned)g_conf->mon_max_pool_pg_num) {
5696 *ss << "'pg_num' must be greater than 0 and less than or equal to "
5697 << g_conf->mon_max_pool_pg_num
5698 << " (you may adjust 'mon max pool pg num' for higher values)";
5699 return -ERANGE;
5700 }
5701 if (pgp_num > pg_num) {
5702 *ss << "'pgp_num' must be greater than 0 and lower or equal than 'pg_num'"
5703 << ", which in this case is " << pg_num;
5704 return -ERANGE;
5705 }
5706 if (pool_type == pg_pool_t::TYPE_REPLICATED && fast_read == FAST_READ_ON) {
5707 *ss << "'fast_read' can only apply to erasure coding pool";
5708 return -EINVAL;
5709 }
5710 int r;
31f18b77
FG
5711 r = prepare_pool_crush_rule(pool_type, erasure_code_profile,
5712 crush_rule_name, &crush_rule, ss);
7c673cae 5713 if (r) {
31f18b77 5714 dout(10) << " prepare_pool_crush_rule returns " << r << dendl;
7c673cae
FG
5715 return r;
5716 }
224ce89b
WB
5717 if (g_conf->mon_osd_crush_smoke_test) {
5718 CrushWrapper newcrush;
5719 _get_pending_crush(newcrush);
5720 ostringstream err;
5721 CrushTester tester(newcrush, err);
5722 tester.set_max_x(50);
5723 tester.set_rule(crush_rule);
5724 r = tester.test_with_fork(g_conf->mon_lease);
5725 if (r < 0) {
5726 dout(10) << " tester.test_with_fork returns " << r
5727 << ": " << err.str() << dendl;
5728 *ss << "crush test failed with " << r << ": " << err.str();
5729 return r;
5730 }
7c673cae
FG
5731 }
5732 unsigned size, min_size;
5733 r = prepare_pool_size(pool_type, erasure_code_profile, &size, &min_size, ss);
5734 if (r) {
5735 dout(10) << " prepare_pool_size returns " << r << dendl;
5736 return r;
5737 }
5738
31f18b77 5739 if (!osdmap.crush->check_crush_rule(crush_rule, pool_type, size, *ss)) {
7c673cae
FG
5740 return -EINVAL;
5741 }
5742
5743 uint32_t stripe_width = 0;
5744 r = prepare_pool_stripe_width(pool_type, erasure_code_profile, &stripe_width, ss);
5745 if (r) {
5746 dout(10) << " prepare_pool_stripe_width returns " << r << dendl;
5747 return r;
5748 }
5749
5750 bool fread = false;
5751 if (pool_type == pg_pool_t::TYPE_ERASURE) {
5752 switch (fast_read) {
5753 case FAST_READ_OFF:
5754 fread = false;
5755 break;
5756 case FAST_READ_ON:
5757 fread = true;
5758 break;
5759 case FAST_READ_DEFAULT:
5760 fread = g_conf->mon_osd_pool_ec_fast_read;
5761 break;
5762 default:
5763 *ss << "invalid fast_read setting: " << fast_read;
5764 return -EINVAL;
5765 }
5766 }
5767
5768 for (map<int64_t,string>::iterator p = pending_inc.new_pool_names.begin();
5769 p != pending_inc.new_pool_names.end();
5770 ++p) {
5771 if (p->second == name)
5772 return 0;
5773 }
5774
5775 if (-1 == pending_inc.new_pool_max)
5776 pending_inc.new_pool_max = osdmap.pool_max;
5777 int64_t pool = ++pending_inc.new_pool_max;
5778 pg_pool_t empty;
5779 pg_pool_t *pi = pending_inc.get_new_pool(pool, &empty);
5780 pi->type = pool_type;
5781 pi->fast_read = fread;
5782 pi->flags = g_conf->osd_pool_default_flags;
5783 if (g_conf->osd_pool_default_flag_hashpspool)
5784 pi->set_flag(pg_pool_t::FLAG_HASHPSPOOL);
5785 if (g_conf->osd_pool_default_flag_nodelete)
5786 pi->set_flag(pg_pool_t::FLAG_NODELETE);
5787 if (g_conf->osd_pool_default_flag_nopgchange)
5788 pi->set_flag(pg_pool_t::FLAG_NOPGCHANGE);
5789 if (g_conf->osd_pool_default_flag_nosizechange)
5790 pi->set_flag(pg_pool_t::FLAG_NOSIZECHANGE);
5791 if (g_conf->osd_pool_use_gmt_hitset &&
5792 (osdmap.get_up_osd_features() & CEPH_FEATURE_OSD_HITSET_GMT))
5793 pi->use_gmt_hitset = true;
5794 else
5795 pi->use_gmt_hitset = false;
5796
5797 pi->size = size;
5798 pi->min_size = min_size;
31f18b77 5799 pi->crush_rule = crush_rule;
7c673cae
FG
5800 pi->expected_num_objects = expected_num_objects;
5801 pi->object_hash = CEPH_STR_HASH_RJENKINS;
5802 pi->set_pg_num(pg_num);
5803 pi->set_pgp_num(pgp_num);
5804 pi->last_change = pending_inc.epoch;
5805 pi->auid = auid;
5806 pi->erasure_code_profile = erasure_code_profile;
5807 pi->stripe_width = stripe_width;
5808 pi->cache_target_dirty_ratio_micro =
5809 g_conf->osd_pool_default_cache_target_dirty_ratio * 1000000;
5810 pi->cache_target_dirty_high_ratio_micro =
5811 g_conf->osd_pool_default_cache_target_dirty_high_ratio * 1000000;
5812 pi->cache_target_full_ratio_micro =
5813 g_conf->osd_pool_default_cache_target_full_ratio * 1000000;
5814 pi->cache_min_flush_age = g_conf->osd_pool_default_cache_min_flush_age;
5815 pi->cache_min_evict_age = g_conf->osd_pool_default_cache_min_evict_age;
5816 pending_inc.new_pool_names[pool] = name;
5817 return 0;
5818}
5819
5820bool OSDMonitor::prepare_set_flag(MonOpRequestRef op, int flag)
5821{
5822 op->mark_osdmon_event(__func__);
5823 ostringstream ss;
5824 if (pending_inc.new_flags < 0)
5825 pending_inc.new_flags = osdmap.get_flags();
5826 pending_inc.new_flags |= flag;
5827 ss << OSDMap::get_flag_string(flag) << " is set";
5828 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, ss.str(),
5829 get_last_committed() + 1));
5830 return true;
5831}
5832
5833bool OSDMonitor::prepare_unset_flag(MonOpRequestRef op, int flag)
5834{
5835 op->mark_osdmon_event(__func__);
5836 ostringstream ss;
5837 if (pending_inc.new_flags < 0)
5838 pending_inc.new_flags = osdmap.get_flags();
5839 pending_inc.new_flags &= ~flag;
5840 ss << OSDMap::get_flag_string(flag) << " is unset";
5841 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, ss.str(),
5842 get_last_committed() + 1));
5843 return true;
5844}
5845
7c673cae
FG
5846int OSDMonitor::prepare_command_pool_set(map<string,cmd_vartype> &cmdmap,
5847 stringstream& ss)
5848{
5849 string poolstr;
5850 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
5851 int64_t pool = osdmap.lookup_pg_pool_name(poolstr.c_str());
5852 if (pool < 0) {
5853 ss << "unrecognized pool '" << poolstr << "'";
5854 return -ENOENT;
5855 }
5856 string var;
5857 cmd_getval(g_ceph_context, cmdmap, "var", var);
5858
5859 pg_pool_t p = *osdmap.get_pg_pool(pool);
5860 if (pending_inc.new_pools.count(pool))
5861 p = pending_inc.new_pools[pool];
5862
5863 // accept val as a json string in the normal case (current
5864 // generation monitor). parse out int or float values from the
5865 // string as needed. however, if it is not a string, try to pull
5866 // out an int, in case an older monitor with an older json schema is
5867 // forwarding a request.
5868 string val;
5869 string interr, floaterr;
5870 int64_t n = 0;
5871 double f = 0;
5872 int64_t uf = 0; // micro-f
5873 if (!cmd_getval(g_ceph_context, cmdmap, "val", val)) {
5874 // wasn't a string; maybe an older mon forwarded json with an int?
5875 if (!cmd_getval(g_ceph_context, cmdmap, "val", n))
5876 return -EINVAL; // no value!
5877 } else {
5878 // we got a string. see if it contains an int.
5879 n = strict_strtoll(val.c_str(), 10, &interr);
5880 // or a float
5881 f = strict_strtod(val.c_str(), &floaterr);
5882 uf = llrintl(f * (double)1000000.0);
5883 }
5884
5885 if (!p.is_tier() &&
5886 (var == "hit_set_type" || var == "hit_set_period" ||
5887 var == "hit_set_count" || var == "hit_set_fpp" ||
5888 var == "target_max_objects" || var == "target_max_bytes" ||
5889 var == "cache_target_full_ratio" || var == "cache_target_dirty_ratio" ||
5890 var == "cache_target_dirty_high_ratio" || var == "use_gmt_hitset" ||
5891 var == "cache_min_flush_age" || var == "cache_min_evict_age" ||
5892 var == "hit_set_grade_decay_rate" || var == "hit_set_search_last_n" ||
5893 var == "min_read_recency_for_promote" || var == "min_write_recency_for_promote")) {
5894 return -EACCES;
5895 }
5896
5897 if (var == "size") {
5898 if (p.has_flag(pg_pool_t::FLAG_NOSIZECHANGE)) {
5899 ss << "pool size change is disabled; you must unset nosizechange flag for the pool first";
5900 return -EPERM;
5901 }
5902 if (p.type == pg_pool_t::TYPE_ERASURE) {
5903 ss << "can not change the size of an erasure-coded pool";
5904 return -ENOTSUP;
5905 }
5906 if (interr.length()) {
5907 ss << "error parsing integer value '" << val << "': " << interr;
5908 return -EINVAL;
5909 }
5910 if (n <= 0 || n > 10) {
5911 ss << "pool size must be between 1 and 10";
5912 return -EINVAL;
5913 }
5914 p.size = n;
5915 if (n < p.min_size)
5916 p.min_size = n;
5917 } else if (var == "min_size") {
5918 if (p.has_flag(pg_pool_t::FLAG_NOSIZECHANGE)) {
5919 ss << "pool min size change is disabled; you must unset nosizechange flag for the pool first";
5920 return -EPERM;
5921 }
5922 if (interr.length()) {
5923 ss << "error parsing integer value '" << val << "': " << interr;
5924 return -EINVAL;
5925 }
5926
5927 if (p.type != pg_pool_t::TYPE_ERASURE) {
5928 if (n < 1 || n > p.size) {
5929 ss << "pool min_size must be between 1 and " << (int)p.size;
5930 return -EINVAL;
5931 }
5932 } else {
5933 ErasureCodeInterfaceRef erasure_code;
5934 int k;
5935 stringstream tmp;
5936 int err = get_erasure_code(p.erasure_code_profile, &erasure_code, &tmp);
5937 if (err == 0) {
5938 k = erasure_code->get_data_chunk_count();
5939 } else {
5940 ss << __func__ << " get_erasure_code failed: " << tmp.rdbuf();
5941 return err;
5942 }
5943
5944 if (n < k || n > p.size) {
5945 ss << "pool min_size must be between " << k << " and " << (int)p.size;
5946 return -EINVAL;
5947 }
5948 }
5949 p.min_size = n;
5950 } else if (var == "auid") {
5951 if (interr.length()) {
5952 ss << "error parsing integer value '" << val << "': " << interr;
5953 return -EINVAL;
5954 }
5955 p.auid = n;
5956 } else if (var == "crash_replay_interval") {
5957 if (interr.length()) {
5958 ss << "error parsing integer value '" << val << "': " << interr;
5959 return -EINVAL;
5960 }
5961 p.crash_replay_interval = n;
5962 } else if (var == "pg_num") {
5963 if (p.has_flag(pg_pool_t::FLAG_NOPGCHANGE)) {
5964 ss << "pool pg_num change is disabled; you must unset nopgchange flag for the pool first";
5965 return -EPERM;
5966 }
5967 if (interr.length()) {
5968 ss << "error parsing integer value '" << val << "': " << interr;
5969 return -EINVAL;
5970 }
5971 if (n <= (int)p.get_pg_num()) {
5972 ss << "specified pg_num " << n << " <= current " << p.get_pg_num();
5973 if (n < (int)p.get_pg_num())
5974 return -EEXIST;
5975 return 0;
5976 }
c07f9fc5
FG
5977 if (n > (unsigned)g_conf->mon_max_pool_pg_num) {
5978 ss << "'pg_num' must be greater than 0 and less than or equal to "
5979 << g_conf->mon_max_pool_pg_num
5980 << " (you may adjust 'mon max pool pg num' for higher values)";
5981 return -ERANGE;
5982 }
7c673cae
FG
5983 string force;
5984 cmd_getval(g_ceph_context,cmdmap, "force", force);
5985 if (p.cache_mode != pg_pool_t::CACHEMODE_NONE &&
5986 force != "--yes-i-really-mean-it") {
5987 ss << "splits in cache pools must be followed by scrubs and leave sufficient free space to avoid overfilling. use --yes-i-really-mean-it to force.";
5988 return -EPERM;
5989 }
5990 int expected_osds = MIN(p.get_pg_num(), osdmap.get_num_osds());
5991 int64_t new_pgs = n - p.get_pg_num();
5992 if (new_pgs > g_conf->mon_osd_max_split_count * expected_osds) {
5993 ss << "specified pg_num " << n << " is too large (creating "
5994 << new_pgs << " new PGs on ~" << expected_osds
5995 << " OSDs exceeds per-OSD max of " << g_conf->mon_osd_max_split_count
5996 << ')';
5997 return -E2BIG;
5998 }
5999 p.set_pg_num(n);
6000 // force pre-luminous clients to resend their ops, since they
6001 // don't understand that split PGs now form a new interval.
6002 p.last_force_op_resend_preluminous = pending_inc.epoch;
6003 } else if (var == "pgp_num") {
6004 if (p.has_flag(pg_pool_t::FLAG_NOPGCHANGE)) {
6005 ss << "pool pgp_num change is disabled; you must unset nopgchange flag for the pool first";
6006 return -EPERM;
6007 }
6008 if (interr.length()) {
6009 ss << "error parsing integer value '" << val << "': " << interr;
6010 return -EINVAL;
6011 }
6012 if (n <= 0) {
6013 ss << "specified pgp_num must > 0, but you set to " << n;
6014 return -EINVAL;
6015 }
6016 if (n > (int)p.get_pg_num()) {
6017 ss << "specified pgp_num " << n << " > pg_num " << p.get_pg_num();
6018 return -EINVAL;
6019 }
6020 p.set_pgp_num(n);
6021 } else if (var == "crush_rule") {
6022 int id = osdmap.crush->get_rule_id(val);
6023 if (id == -ENOENT) {
6024 ss << "crush rule " << val << " does not exist";
6025 return -ENOENT;
6026 }
6027 if (id < 0) {
6028 ss << cpp_strerror(id);
6029 return -ENOENT;
6030 }
6031 if (!osdmap.crush->check_crush_rule(id, p.get_type(), p.get_size(), ss)) {
6032 return -EINVAL;
6033 }
31f18b77 6034 p.crush_rule = id;
7c673cae
FG
6035 } else if (var == "nodelete" || var == "nopgchange" ||
6036 var == "nosizechange" || var == "write_fadvise_dontneed" ||
6037 var == "noscrub" || var == "nodeep-scrub") {
6038 uint64_t flag = pg_pool_t::get_flag_by_name(var);
6039 // make sure we only compare against 'n' if we didn't receive a string
6040 if (val == "true" || (interr.empty() && n == 1)) {
6041 p.set_flag(flag);
6042 } else if (val == "false" || (interr.empty() && n == 0)) {
6043 p.unset_flag(flag);
6044 } else {
6045 ss << "expecting value 'true', 'false', '0', or '1'";
6046 return -EINVAL;
6047 }
6048 } else if (var == "hashpspool") {
6049 uint64_t flag = pg_pool_t::get_flag_by_name(var);
6050 string force;
6051 cmd_getval(g_ceph_context, cmdmap, "force", force);
6052 if (force != "--yes-i-really-mean-it") {
6053 ss << "are you SURE? this will remap all placement groups in this pool,"
6054 " this triggers large data movement,"
6055 " pass --yes-i-really-mean-it if you really do.";
6056 return -EPERM;
6057 }
6058 // make sure we only compare against 'n' if we didn't receive a string
6059 if (val == "true" || (interr.empty() && n == 1)) {
6060 p.set_flag(flag);
6061 } else if (val == "false" || (interr.empty() && n == 0)) {
6062 p.unset_flag(flag);
6063 } else {
6064 ss << "expecting value 'true', 'false', '0', or '1'";
6065 return -EINVAL;
6066 }
6067 } else if (var == "hit_set_type") {
6068 if (val == "none")
6069 p.hit_set_params = HitSet::Params();
6070 else {
6071 int err = check_cluster_features(CEPH_FEATURE_OSD_CACHEPOOL, ss);
6072 if (err)
6073 return err;
6074 if (val == "bloom") {
6075 BloomHitSet::Params *bsp = new BloomHitSet::Params;
6076 bsp->set_fpp(g_conf->osd_pool_default_hit_set_bloom_fpp);
6077 p.hit_set_params = HitSet::Params(bsp);
6078 } else if (val == "explicit_hash")
6079 p.hit_set_params = HitSet::Params(new ExplicitHashHitSet::Params);
6080 else if (val == "explicit_object")
6081 p.hit_set_params = HitSet::Params(new ExplicitObjectHitSet::Params);
6082 else {
6083 ss << "unrecognized hit_set type '" << val << "'";
6084 return -EINVAL;
6085 }
6086 }
6087 } else if (var == "hit_set_period") {
6088 if (interr.length()) {
6089 ss << "error parsing integer value '" << val << "': " << interr;
6090 return -EINVAL;
6091 }
6092 p.hit_set_period = n;
6093 } else if (var == "hit_set_count") {
6094 if (interr.length()) {
6095 ss << "error parsing integer value '" << val << "': " << interr;
6096 return -EINVAL;
6097 }
6098 p.hit_set_count = n;
6099 } else if (var == "hit_set_fpp") {
6100 if (floaterr.length()) {
6101 ss << "error parsing floating point value '" << val << "': " << floaterr;
6102 return -EINVAL;
6103 }
6104 if (p.hit_set_params.get_type() != HitSet::TYPE_BLOOM) {
6105 ss << "hit set is not of type Bloom; invalid to set a false positive rate!";
6106 return -EINVAL;
6107 }
6108 BloomHitSet::Params *bloomp = static_cast<BloomHitSet::Params*>(p.hit_set_params.impl.get());
6109 bloomp->set_fpp(f);
6110 } else if (var == "use_gmt_hitset") {
6111 if (val == "true" || (interr.empty() && n == 1)) {
6112 if (!(osdmap.get_up_osd_features() & CEPH_FEATURE_OSD_HITSET_GMT)) {
6113 ss << "not all OSDs support GMT hit set.";
6114 return -EINVAL;
6115 }
6116 p.use_gmt_hitset = true;
6117 } else {
6118 ss << "expecting value 'true' or '1'";
6119 return -EINVAL;
6120 }
6121 } else if (var == "allow_ec_overwrites") {
6122 if (!p.is_erasure()) {
6123 ss << "ec overwrites can only be enabled for an erasure coded pool";
6124 return -EINVAL;
6125 }
224ce89b
WB
6126 stringstream err;
6127 if (!g_conf->mon_debug_no_require_bluestore_for_ec_overwrites &&
6128 !is_pool_currently_all_bluestore(pool, p, &err)) {
6129 ss << "pool must only be stored on bluestore for scrubbing to work: " << err.str();
6130 return -EINVAL;
6131 }
7c673cae
FG
6132 if (val == "true" || (interr.empty() && n == 1)) {
6133 p.flags |= pg_pool_t::FLAG_EC_OVERWRITES;
6134 } else if (val == "false" || (interr.empty() && n == 0)) {
6135 ss << "ec overwrites cannot be disabled once enabled";
6136 return -EINVAL;
6137 } else {
6138 ss << "expecting value 'true', 'false', '0', or '1'";
6139 return -EINVAL;
6140 }
7c673cae
FG
6141 } else if (var == "target_max_objects") {
6142 if (interr.length()) {
6143 ss << "error parsing int '" << val << "': " << interr;
6144 return -EINVAL;
6145 }
6146 p.target_max_objects = n;
6147 } else if (var == "target_max_bytes") {
6148 if (interr.length()) {
6149 ss << "error parsing int '" << val << "': " << interr;
6150 return -EINVAL;
6151 }
6152 p.target_max_bytes = n;
6153 } else if (var == "cache_target_dirty_ratio") {
6154 if (floaterr.length()) {
6155 ss << "error parsing float '" << val << "': " << floaterr;
6156 return -EINVAL;
6157 }
6158 if (f < 0 || f > 1.0) {
6159 ss << "value must be in the range 0..1";
6160 return -ERANGE;
6161 }
6162 p.cache_target_dirty_ratio_micro = uf;
6163 } else if (var == "cache_target_dirty_high_ratio") {
6164 if (floaterr.length()) {
6165 ss << "error parsing float '" << val << "': " << floaterr;
6166 return -EINVAL;
6167 }
6168 if (f < 0 || f > 1.0) {
6169 ss << "value must be in the range 0..1";
6170 return -ERANGE;
6171 }
6172 p.cache_target_dirty_high_ratio_micro = uf;
6173 } else if (var == "cache_target_full_ratio") {
6174 if (floaterr.length()) {
6175 ss << "error parsing float '" << val << "': " << floaterr;
6176 return -EINVAL;
6177 }
6178 if (f < 0 || f > 1.0) {
6179 ss << "value must be in the range 0..1";
6180 return -ERANGE;
6181 }
6182 p.cache_target_full_ratio_micro = uf;
6183 } else if (var == "cache_min_flush_age") {
6184 if (interr.length()) {
6185 ss << "error parsing int '" << val << "': " << interr;
6186 return -EINVAL;
6187 }
6188 p.cache_min_flush_age = n;
6189 } else if (var == "cache_min_evict_age") {
6190 if (interr.length()) {
6191 ss << "error parsing int '" << val << "': " << interr;
6192 return -EINVAL;
6193 }
6194 p.cache_min_evict_age = n;
6195 } else if (var == "min_read_recency_for_promote") {
6196 if (interr.length()) {
6197 ss << "error parsing integer value '" << val << "': " << interr;
6198 return -EINVAL;
6199 }
6200 p.min_read_recency_for_promote = n;
6201 } else if (var == "hit_set_grade_decay_rate") {
6202 if (interr.length()) {
6203 ss << "error parsing integer value '" << val << "': " << interr;
6204 return -EINVAL;
6205 }
6206 if (n > 100 || n < 0) {
6207 ss << "value out of range,valid range is 0 - 100";
6208 return -EINVAL;
6209 }
6210 p.hit_set_grade_decay_rate = n;
6211 } else if (var == "hit_set_search_last_n") {
6212 if (interr.length()) {
6213 ss << "error parsing integer value '" << val << "': " << interr;
6214 return -EINVAL;
6215 }
6216 if (n > p.hit_set_count || n < 0) {
6217 ss << "value out of range,valid range is 0 - hit_set_count";
6218 return -EINVAL;
6219 }
6220 p.hit_set_search_last_n = n;
6221 } else if (var == "min_write_recency_for_promote") {
6222 if (interr.length()) {
6223 ss << "error parsing integer value '" << val << "': " << interr;
6224 return -EINVAL;
6225 }
6226 p.min_write_recency_for_promote = n;
6227 } else if (var == "fast_read") {
6228 if (p.is_replicated()) {
6229 ss << "fast read is not supported in replication pool";
6230 return -EINVAL;
6231 }
6232 if (val == "true" || (interr.empty() && n == 1)) {
6233 p.fast_read = true;
6234 } else if (val == "false" || (interr.empty() && n == 0)) {
6235 p.fast_read = false;
6236 } else {
6237 ss << "expecting value 'true', 'false', '0', or '1'";
6238 return -EINVAL;
6239 }
6240 } else if (pool_opts_t::is_opt_name(var)) {
224ce89b 6241 bool unset = val == "unset";
7c673cae 6242 if (var == "compression_mode") {
224ce89b
WB
6243 if (!unset) {
6244 auto cmode = Compressor::get_comp_mode_type(val);
6245 if (!cmode) {
6246 ss << "unrecognized compression mode '" << val << "'";
6247 return -EINVAL;
6248 }
7c673cae
FG
6249 }
6250 } else if (var == "compression_algorithm") {
224ce89b
WB
6251 if (!unset) {
6252 auto alg = Compressor::get_comp_alg_type(val);
6253 if (!alg) {
6254 ss << "unrecognized compression_algorithm '" << val << "'";
6255 return -EINVAL;
6256 }
7c673cae
FG
6257 }
6258 } else if (var == "compression_required_ratio") {
6259 if (floaterr.length()) {
6260 ss << "error parsing float value '" << val << "': " << floaterr;
6261 return -EINVAL;
6262 }
224ce89b 6263 if (f < 0 || f > 1) {
7c673cae 6264 ss << "compression_required_ratio is out of range (0-1): '" << val << "'";
224ce89b 6265 return -EINVAL;
7c673cae
FG
6266 }
6267 } else if (var == "csum_type") {
224ce89b 6268 auto t = unset ? 0 : Checksummer::get_csum_string_type(val);
7c673cae
FG
6269 if (t < 0 ) {
6270 ss << "unrecognized csum_type '" << val << "'";
224ce89b 6271 return -EINVAL;
7c673cae
FG
6272 }
6273 //preserve csum_type numeric value
6274 n = t;
6275 interr.clear();
6276 } else if (var == "compression_max_blob_size" ||
6277 var == "compression_min_blob_size" ||
6278 var == "csum_max_block" ||
6279 var == "csum_min_block") {
6280 if (interr.length()) {
6281 ss << "error parsing int value '" << val << "': " << interr;
6282 return -EINVAL;
6283 }
6284 }
6285
6286 pool_opts_t::opt_desc_t desc = pool_opts_t::get_opt_desc(var);
6287 switch (desc.type) {
6288 case pool_opts_t::STR:
224ce89b 6289 if (unset) {
7c673cae
FG
6290 p.opts.unset(desc.key);
6291 } else {
6292 p.opts.set(desc.key, static_cast<std::string>(val));
6293 }
6294 break;
6295 case pool_opts_t::INT:
6296 if (interr.length()) {
6297 ss << "error parsing integer value '" << val << "': " << interr;
6298 return -EINVAL;
6299 }
6300 if (n == 0) {
6301 p.opts.unset(desc.key);
6302 } else {
6303 p.opts.set(desc.key, static_cast<int>(n));
6304 }
6305 break;
6306 case pool_opts_t::DOUBLE:
6307 if (floaterr.length()) {
6308 ss << "error parsing floating point value '" << val << "': " << floaterr;
6309 return -EINVAL;
6310 }
6311 if (f == 0) {
6312 p.opts.unset(desc.key);
6313 } else {
6314 p.opts.set(desc.key, static_cast<double>(f));
6315 }
6316 break;
6317 default:
6318 assert(!"unknown type");
6319 }
6320 } else {
6321 ss << "unrecognized variable '" << var << "'";
6322 return -EINVAL;
6323 }
224ce89b
WB
6324 if (val != "unset") {
6325 ss << "set pool " << pool << " " << var << " to " << val;
6326 } else {
6327 ss << "unset pool " << pool << " " << var;
6328 }
7c673cae
FG
6329 p.last_change = pending_inc.epoch;
6330 pending_inc.new_pools[pool] = p;
6331 return 0;
6332}
6333
c07f9fc5
FG
6334int OSDMonitor::prepare_command_pool_application(const string &prefix,
6335 map<string,cmd_vartype> &cmdmap,
6336 stringstream& ss)
6337{
6338 string pool_name;
6339 cmd_getval(g_ceph_context, cmdmap, "pool", pool_name);
6340 int64_t pool = osdmap.lookup_pg_pool_name(pool_name.c_str());
6341 if (pool < 0) {
6342 ss << "unrecognized pool '" << pool_name << "'";
6343 return -ENOENT;
6344 }
6345
6346 pg_pool_t p = *osdmap.get_pg_pool(pool);
6347 if (pending_inc.new_pools.count(pool)) {
6348 p = pending_inc.new_pools[pool];
6349 }
6350
6351 string app;
6352 cmd_getval(g_ceph_context, cmdmap, "app", app);
6353 bool app_exists = (p.application_metadata.count(app) > 0);
6354
6355 if (boost::algorithm::ends_with(prefix, "enable")) {
6356 if (app.empty()) {
6357 ss << "application name must be provided";
6358 return -EINVAL;
6359 }
6360
6361 if (p.is_tier()) {
6362 ss << "application must be enabled on base tier";
6363 return -EINVAL;
6364 }
6365
6366 string force;
6367 cmd_getval(g_ceph_context, cmdmap, "force", force);
6368
6369 if (!app_exists && !p.application_metadata.empty() &&
6370 force != "--yes-i-really-mean-it") {
6371 ss << "Are you SURE? Pool '" << pool_name << "' already has an enabled "
6372 << "application; pass --yes-i-really-mean-it to proceed anyway";
6373 return -EPERM;
6374 }
6375
6376 if (!app_exists && p.application_metadata.size() >= MAX_POOL_APPLICATIONS) {
6377 ss << "too many enabled applications on pool '" << pool_name << "'; "
6378 << "max " << MAX_POOL_APPLICATIONS;
6379 return -EINVAL;
6380 }
6381
6382 if (app.length() > MAX_POOL_APPLICATION_LENGTH) {
6383 ss << "application name '" << app << "' too long; max length "
6384 << MAX_POOL_APPLICATION_LENGTH;
6385 return -EINVAL;
6386 }
6387
6388 if (!app_exists) {
6389 p.application_metadata[app] = {};
6390 }
6391 ss << "enabled application '" << app << "' on pool '" << pool_name << "'";
6392
6393 } else if (boost::algorithm::ends_with(prefix, "disable")) {
6394 string force;
6395 cmd_getval(g_ceph_context, cmdmap, "force", force);
6396
6397 if (force != "--yes-i-really-mean-it") {
6398 ss << "Are you SURE? Disabling an application within a pool might result "
6399 << "in loss of application functionality; pass "
6400 << "--yes-i-really-mean-it to proceed anyway";
6401 return -EPERM;
6402 }
6403
6404 if (!app_exists) {
6405 ss << "application '" << app << "' is not enabled on pool '" << pool_name
6406 << "'";
6407 return 0; // idempotent
6408 }
6409
6410 p.application_metadata.erase(app);
6411 ss << "disable application '" << app << "' on pool '" << pool_name << "'";
6412
6413 } else if (boost::algorithm::ends_with(prefix, "set")) {
6414 if (p.is_tier()) {
6415 ss << "application metadata must be set on base tier";
6416 return -EINVAL;
6417 }
6418
6419 if (!app_exists) {
6420 ss << "application '" << app << "' is not enabled on pool '" << pool_name
6421 << "'";
6422 return -ENOENT;
6423 }
6424
6425 string key;
6426 cmd_getval(g_ceph_context, cmdmap, "key", key);
6427
6428 if (key.empty()) {
6429 ss << "key must be provided";
6430 return -EINVAL;
6431 }
6432
6433 auto &app_keys = p.application_metadata[app];
6434 if (app_keys.count(key) == 0 &&
6435 app_keys.size() >= MAX_POOL_APPLICATION_KEYS) {
6436 ss << "too many keys set for application '" << app << "' on pool '"
6437 << pool_name << "'; max " << MAX_POOL_APPLICATION_KEYS;
6438 return -EINVAL;
6439 }
6440
6441 if (key.length() > MAX_POOL_APPLICATION_LENGTH) {
6442 ss << "key '" << app << "' too long; max length "
6443 << MAX_POOL_APPLICATION_LENGTH;
6444 return -EINVAL;
6445 }
6446
6447 string value;
6448 cmd_getval(g_ceph_context, cmdmap, "value", value);
6449 if (value.length() > MAX_POOL_APPLICATION_LENGTH) {
6450 ss << "value '" << value << "' too long; max length "
6451 << MAX_POOL_APPLICATION_LENGTH;
6452 return -EINVAL;
6453 }
6454
6455 p.application_metadata[app][key] = value;
6456 ss << "set application '" << app << "' key '" << key << "' to '"
6457 << value << "' on pool '" << pool_name << "'";
6458 } else if (boost::algorithm::ends_with(prefix, "rm")) {
6459 if (!app_exists) {
6460 ss << "application '" << app << "' is not enabled on pool '" << pool_name
6461 << "'";
6462 return -ENOENT;
6463 }
6464
6465 string key;
6466 cmd_getval(g_ceph_context, cmdmap, "key", key);
6467 auto it = p.application_metadata[app].find(key);
6468 if (it == p.application_metadata[app].end()) {
6469 ss << "application '" << app << "' on pool '" << pool_name
6470 << "' does not have key '" << key << "'";
6471 return 0; // idempotent
6472 }
6473
6474 p.application_metadata[app].erase(it);
6475 ss << "removed application '" << app << "' key '" << key << "' on pool '"
6476 << pool_name << "'";
6477 } else {
6478 assert(false);
6479 }
6480
6481 p.last_change = pending_inc.epoch;
6482 pending_inc.new_pools[pool] = p;
6483 return 0;
6484}
6485
31f18b77
FG
6486int OSDMonitor::_prepare_command_osd_crush_remove(
6487 CrushWrapper &newcrush,
6488 int32_t id,
6489 int32_t ancestor,
6490 bool has_ancestor,
6491 bool unlink_only)
6492{
6493 int err = 0;
6494
6495 if (has_ancestor) {
6496 err = newcrush.remove_item_under(g_ceph_context, id, ancestor,
6497 unlink_only);
6498 } else {
6499 err = newcrush.remove_item(g_ceph_context, id, unlink_only);
6500 }
6501 return err;
6502}
6503
6504void OSDMonitor::do_osd_crush_remove(CrushWrapper& newcrush)
6505{
6506 pending_inc.crush.clear();
6507 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
6508}
6509
6510int OSDMonitor::prepare_command_osd_crush_remove(
6511 CrushWrapper &newcrush,
6512 int32_t id,
6513 int32_t ancestor,
6514 bool has_ancestor,
6515 bool unlink_only)
6516{
6517 int err = _prepare_command_osd_crush_remove(
6518 newcrush, id, ancestor,
6519 has_ancestor, unlink_only);
6520
6521 if (err < 0)
6522 return err;
6523
6524 assert(err == 0);
6525 do_osd_crush_remove(newcrush);
6526
6527 return 0;
6528}
6529
6530int OSDMonitor::prepare_command_osd_remove(int32_t id)
6531{
6532 if (osdmap.is_up(id)) {
6533 return -EBUSY;
6534 }
6535
6536 pending_inc.new_state[id] = osdmap.get_state(id);
6537 pending_inc.new_uuid[id] = uuid_d();
6538 pending_metadata_rm.insert(id);
6539 pending_metadata.erase(id);
6540
6541 return 0;
6542}
6543
6544int32_t OSDMonitor::_allocate_osd_id(int32_t* existing_id)
6545{
6546 assert(existing_id);
6547 *existing_id = -1;
6548
6549 for (int32_t i = 0; i < osdmap.get_max_osd(); ++i) {
6550 if (!osdmap.exists(i) &&
6551 pending_inc.new_up_client.count(i) == 0 &&
6552 (pending_inc.new_state.count(i) == 0 ||
6553 (pending_inc.new_state[i] & CEPH_OSD_EXISTS) == 0)) {
6554 *existing_id = i;
6555 return -1;
6556 }
6557 }
6558
6559 if (pending_inc.new_max_osd < 0) {
6560 return osdmap.get_max_osd();
6561 }
6562 return pending_inc.new_max_osd;
6563}
6564
6565void OSDMonitor::do_osd_create(
6566 const int32_t id,
6567 const uuid_d& uuid,
6568 int32_t* new_id)
6569{
6570 dout(10) << __func__ << " uuid " << uuid << dendl;
6571 assert(new_id);
6572
6573 // We presume validation has been performed prior to calling this
6574 // function. We assert with prejudice.
6575
6576 int32_t allocated_id = -1; // declare here so we can jump
6577 int32_t existing_id = -1;
6578 if (!uuid.is_zero()) {
6579 existing_id = osdmap.identify_osd(uuid);
6580 if (existing_id >= 0) {
6581 assert(id < 0 || id == existing_id);
6582 *new_id = existing_id;
6583 goto out;
6584 } else if (id >= 0) {
6585 // uuid does not exist, and id has been provided, so just create
6586 // the new osd.id
6587 *new_id = id;
6588 goto out;
6589 }
6590 }
6591
6592 // allocate a new id
6593 allocated_id = _allocate_osd_id(&existing_id);
6594 dout(10) << __func__ << " allocated id " << allocated_id
6595 << " existing id " << existing_id << dendl;
6596 if (existing_id >= 0) {
6597 assert(existing_id < osdmap.get_max_osd());
6598 assert(allocated_id < 0);
6599 pending_inc.new_weight[existing_id] = CEPH_OSD_OUT;
6600 *new_id = existing_id;
6601
6602 } else if (allocated_id >= 0) {
6603 assert(existing_id < 0);
6604 // raise max_osd
6605 if (pending_inc.new_max_osd < 0) {
6606 pending_inc.new_max_osd = osdmap.get_max_osd() + 1;
6607 } else {
6608 ++pending_inc.new_max_osd;
6609 }
6610 *new_id = pending_inc.new_max_osd - 1;
6611 assert(*new_id == allocated_id);
6612 } else {
6613 assert(0 == "unexpected condition");
6614 }
6615
6616out:
6617 dout(10) << __func__ << " using id " << *new_id << dendl;
6618 if (osdmap.get_max_osd() <= *new_id && pending_inc.new_max_osd <= *new_id) {
6619 pending_inc.new_max_osd = *new_id + 1;
6620 }
6621
6622 pending_inc.new_state[*new_id] |= CEPH_OSD_EXISTS | CEPH_OSD_NEW;
6623 if (!uuid.is_zero())
6624 pending_inc.new_uuid[*new_id] = uuid;
6625}
6626
6627int OSDMonitor::validate_osd_create(
6628 const int32_t id,
6629 const uuid_d& uuid,
6630 const bool check_osd_exists,
6631 int32_t* existing_id,
6632 stringstream& ss)
6633{
6634
6635 dout(10) << __func__ << " id " << id << " uuid " << uuid
6636 << " check_osd_exists " << check_osd_exists << dendl;
6637
6638 assert(existing_id);
6639
6640 if (id < 0 && uuid.is_zero()) {
6641 // we have nothing to validate
6642 *existing_id = -1;
6643 return 0;
6644 } else if (uuid.is_zero()) {
6645 // we have an id but we will ignore it - because that's what
6646 // `osd create` does.
6647 return 0;
6648 }
6649
6650 /*
6651 * This function will be used to validate whether we are able to
6652 * create a new osd when the `uuid` is specified.
6653 *
6654 * It will be used by both `osd create` and `osd new`, as the checks
6655 * are basically the same when it pertains to osd id and uuid validation.
6656 * However, `osd create` presumes an `uuid` is optional, for legacy
6657 * reasons, while `osd new` requires the `uuid` to be provided. This
6658 * means that `osd create` will not be idempotent if an `uuid` is not
6659 * provided, but we will always guarantee the idempotency of `osd new`.
6660 */
6661
6662 assert(!uuid.is_zero());
6663 if (pending_inc.identify_osd(uuid) >= 0) {
6664 // osd is about to exist
6665 return -EAGAIN;
6666 }
6667
6668 int32_t i = osdmap.identify_osd(uuid);
6669 if (i >= 0) {
6670 // osd already exists
6671 if (id >= 0 && i != id) {
6672 ss << "uuid " << uuid << " already in use for different id " << i;
6673 return -EEXIST;
6674 }
6675 // return a positive errno to distinguish between a blocking error
6676 // and an error we consider to not be a problem (i.e., this would be
6677 // an idempotent operation).
6678 *existing_id = i;
6679 return EEXIST;
6680 }
6681 // i < 0
6682 if (id >= 0) {
6683 if (pending_inc.new_state.count(id)) {
6684 // osd is about to exist
6685 return -EAGAIN;
6686 }
6687 // we may not care if an osd exists if we are recreating a previously
6688 // destroyed osd.
6689 if (check_osd_exists && osdmap.exists(id)) {
6690 ss << "id " << id << " already in use and does not match uuid "
6691 << uuid;
6692 return -EINVAL;
6693 }
6694 }
6695 return 0;
6696}
6697
6698int OSDMonitor::prepare_command_osd_create(
6699 const int32_t id,
6700 const uuid_d& uuid,
6701 int32_t* existing_id,
6702 stringstream& ss)
6703{
6704 dout(10) << __func__ << " id " << id << " uuid " << uuid << dendl;
6705 assert(existing_id);
6706
6707 if (uuid.is_zero()) {
6708 dout(10) << __func__ << " no uuid; assuming legacy `osd create`" << dendl;
6709 }
6710
6711 return validate_osd_create(id, uuid, true, existing_id, ss);
6712}
6713
6714int OSDMonitor::prepare_command_osd_new(
6715 MonOpRequestRef op,
6716 const map<string,cmd_vartype>& cmdmap,
6717 const map<string,string>& secrets,
6718 stringstream &ss,
6719 Formatter *f)
6720{
6721 uuid_d uuid;
6722 string uuidstr;
6723 int64_t id = -1;
6724
6725 assert(paxos->is_plugged());
6726
6727 dout(10) << __func__ << " " << op << dendl;
6728
6729 /* validate command. abort now if something's wrong. */
6730
6731 /* `osd new` will expect a `uuid` to be supplied; `id` is optional.
6732 *
6733 * If `id` is not specified, we will identify any existing osd based
6734 * on `uuid`. Operation will be idempotent iff secrets match.
6735 *
6736 * If `id` is specified, we will identify any existing osd based on
6737 * `uuid` and match against `id`. If they match, operation will be
6738 * idempotent iff secrets match.
6739 *
6740 * `-i secrets.json` will be optional. If supplied, will be used
6741 * to check for idempotency when `id` and `uuid` match.
6742 *
6743 * If `id` is not specified, and `uuid` does not exist, an id will
6744 * be found or allocated for the osd.
6745 *
6746 * If `id` is specified, and the osd has been previously marked
6747 * as destroyed, then the `id` will be reused.
6748 */
6749 if (!cmd_getval(g_ceph_context, cmdmap, "uuid", uuidstr)) {
6750 ss << "requires the OSD's UUID to be specified.";
6751 return -EINVAL;
6752 } else if (!uuid.parse(uuidstr.c_str())) {
6753 ss << "invalid UUID value '" << uuidstr << "'.";
6754 return -EINVAL;
6755 }
6756
6757 if (cmd_getval(g_ceph_context, cmdmap, "id", id) &&
6758 (id < 0)) {
6759 ss << "invalid OSD id; must be greater or equal than zero.";
6760 return -EINVAL;
6761 }
6762
6763 // are we running an `osd create`-like command, or recreating
6764 // a previously destroyed osd?
6765
6766 bool is_recreate_destroyed = (id >= 0 && osdmap.is_destroyed(id));
6767
6768 // we will care about `id` to assess whether osd is `destroyed`, or
6769 // to create a new osd.
6770 // we will need an `id` by the time we reach auth.
6771
6772 int32_t existing_id = -1;
6773 int err = validate_osd_create(id, uuid, !is_recreate_destroyed,
6774 &existing_id, ss);
6775
6776 bool may_be_idempotent = false;
6777 if (err == EEXIST) {
6778 // this is idempotent from the osdmon's point-of-view
6779 may_be_idempotent = true;
6780 assert(existing_id >= 0);
6781 id = existing_id;
6782 } else if (err < 0) {
6783 return err;
6784 }
6785
6786 if (!may_be_idempotent) {
6787 // idempotency is out of the window. We are either creating a new
6788 // osd or recreating a destroyed osd.
6789 //
6790 // We now need to figure out if we have an `id` (and if it's valid),
6791 // of find an `id` if we don't have one.
6792
6793 // NOTE: we need to consider the case where the `id` is specified for
6794 // `osd create`, and we must honor it. So this means checking if
6795 // the `id` is destroyed, and if so assume the destroy; otherwise,
6796 // check if it `exists` - in which case we complain about not being
6797 // `destroyed`. In the end, if nothing fails, we must allow the
6798 // creation, so that we are compatible with `create`.
6799 if (id >= 0 && osdmap.exists(id) && !osdmap.is_destroyed(id)) {
6800 dout(10) << __func__ << " osd." << id << " isn't destroyed" << dendl;
6801 ss << "OSD " << id << " has not yet been destroyed";
6802 return -EINVAL;
6803 } else if (id < 0) {
6804 // find an `id`
6805 id = _allocate_osd_id(&existing_id);
6806 if (id < 0) {
6807 assert(existing_id >= 0);
6808 id = existing_id;
6809 }
6810 dout(10) << __func__ << " found id " << id << " to use" << dendl;
6811 } else if (id >= 0 && osdmap.is_destroyed(id)) {
6812 dout(10) << __func__ << " recreating osd." << id << dendl;
6813 } else {
6814 dout(10) << __func__ << " creating new osd." << id << dendl;
6815 }
6816 } else {
6817 assert(id >= 0);
6818 assert(osdmap.exists(id));
6819 }
6820
6821 // we are now able to either create a brand new osd or reuse an existing
6822 // osd that has been previously destroyed.
6823
6824 dout(10) << __func__ << " id " << id << " uuid " << uuid << dendl;
6825
6826 if (may_be_idempotent && secrets.empty()) {
6827 // nothing to do, really.
6828 dout(10) << __func__ << " idempotent and no secrets -- no op." << dendl;
6829 assert(id >= 0);
6830 if (f) {
6831 f->open_object_section("created_osd");
6832 f->dump_int("osdid", id);
6833 f->close_section();
6834 } else {
6835 ss << id;
6836 }
6837 return EEXIST;
6838 }
6839
6840 string cephx_secret, lockbox_secret, dmcrypt_key;
6841 bool has_lockbox = false;
6842 bool has_secrets = (!secrets.empty());
6843
6844 ConfigKeyService *svc = nullptr;
6845 AuthMonitor::auth_entity_t cephx_entity, lockbox_entity;
6846
6847 if (has_secrets) {
6848 if (secrets.count("cephx_secret") == 0) {
6849 ss << "requires a cephx secret.";
6850 return -EINVAL;
6851 }
6852 cephx_secret = secrets.at("cephx_secret");
6853
6854 bool has_lockbox_secret = (secrets.count("cephx_lockbox_secret") > 0);
6855 bool has_dmcrypt_key = (secrets.count("dmcrypt_key") > 0);
6856
6857 dout(10) << __func__ << " has lockbox " << has_lockbox_secret
6858 << " dmcrypt " << has_dmcrypt_key << dendl;
6859
6860 if (has_lockbox_secret && has_dmcrypt_key) {
6861 has_lockbox = true;
6862 lockbox_secret = secrets.at("cephx_lockbox_secret");
6863 dmcrypt_key = secrets.at("dmcrypt_key");
6864 } else if (!has_lockbox_secret != !has_dmcrypt_key) {
6865 ss << "requires both a cephx lockbox secret and a dm-crypt key.";
6866 return -EINVAL;
6867 }
6868
6869 dout(10) << __func__ << " validate secrets using osd id " << id << dendl;
6870
6871 err = mon->authmon()->validate_osd_new(id, uuid,
6872 cephx_secret,
6873 lockbox_secret,
6874 cephx_entity,
6875 lockbox_entity,
6876 ss);
6877 if (err < 0) {
6878 return err;
6879 } else if (may_be_idempotent && err != EEXIST) {
6880 // for this to be idempotent, `id` should already be >= 0; no need
6881 // to use validate_id.
6882 assert(id >= 0);
6883 ss << "osd." << id << " exists but secrets do not match";
6884 return -EEXIST;
6885 }
6886
6887 if (has_lockbox) {
6888 svc = (ConfigKeyService*)mon->config_key_service;
6889 err = svc->validate_osd_new(uuid, dmcrypt_key, ss);
6890 if (err < 0) {
6891 return err;
6892 } else if (may_be_idempotent && err != EEXIST) {
6893 assert(id >= 0);
6894 ss << "osd." << id << " exists but dm-crypt key does not match.";
6895 return -EEXIST;
6896 }
6897 }
6898 }
6899 assert(!has_secrets || !cephx_secret.empty());
6900 assert(!has_lockbox || !lockbox_secret.empty());
6901
6902 if (may_be_idempotent) {
6903 // we have nothing to do for either the osdmon or the authmon,
6904 // and we have no lockbox - so the config key service will not be
6905 // touched. This is therefore an idempotent operation, and we can
6906 // just return right away.
6907 dout(10) << __func__ << " idempotent -- no op." << dendl;
6908 assert(id >= 0);
6909 if (f) {
6910 f->open_object_section("created_osd");
6911 f->dump_int("osdid", id);
6912 f->close_section();
6913 } else {
6914 ss << id;
6915 }
6916 return EEXIST;
6917 }
6918 assert(!may_be_idempotent);
6919
6920 // perform updates.
6921 if (has_secrets) {
6922 assert(!cephx_secret.empty());
6923 assert((lockbox_secret.empty() && dmcrypt_key.empty()) ||
6924 (!lockbox_secret.empty() && !dmcrypt_key.empty()));
6925
6926 err = mon->authmon()->do_osd_new(cephx_entity,
6927 lockbox_entity,
6928 has_lockbox);
6929 assert(0 == err);
6930
6931 if (has_lockbox) {
6932 assert(nullptr != svc);
6933 svc->do_osd_new(uuid, dmcrypt_key);
6934 }
6935 }
6936
6937 if (is_recreate_destroyed) {
6938 assert(id >= 0);
6939 assert(osdmap.is_destroyed(id));
6940 pending_inc.new_weight[id] = CEPH_OSD_OUT;
6941 pending_inc.new_state[id] |= CEPH_OSD_DESTROYED | CEPH_OSD_NEW;
c07f9fc5
FG
6942 if (osdmap.get_state(id) & CEPH_OSD_UP) {
6943 // due to http://tracker.ceph.com/issues/20751 some clusters may
6944 // have UP set for non-existent OSDs; make sure it is cleared
6945 // for a newly created osd.
6946 pending_inc.new_state[id] |= CEPH_OSD_UP;
6947 }
31f18b77
FG
6948 pending_inc.new_uuid[id] = uuid;
6949 } else {
6950 assert(id >= 0);
6951 int32_t new_id = -1;
6952 do_osd_create(id, uuid, &new_id);
6953 assert(new_id >= 0);
6954 assert(id == new_id);
6955 }
6956
6957 if (f) {
6958 f->open_object_section("created_osd");
6959 f->dump_int("osdid", id);
6960 f->close_section();
6961 } else {
6962 ss << id;
6963 }
6964
6965 return 0;
6966}
6967
7c673cae
FG
6968bool OSDMonitor::prepare_command(MonOpRequestRef op)
6969{
6970 op->mark_osdmon_event(__func__);
6971 MMonCommand *m = static_cast<MMonCommand*>(op->get_req());
6972 stringstream ss;
6973 map<string, cmd_vartype> cmdmap;
6974 if (!cmdmap_from_json(m->cmd, &cmdmap, ss)) {
6975 string rs = ss.str();
6976 mon->reply_command(op, -EINVAL, rs, get_last_committed());
6977 return true;
6978 }
6979
6980 MonSession *session = m->get_session();
6981 if (!session) {
6982 mon->reply_command(op, -EACCES, "access denied", get_last_committed());
6983 return true;
6984 }
6985
6986 return prepare_command_impl(op, cmdmap);
6987}
6988
6989static int parse_reweights(CephContext *cct,
6990 const map<string,cmd_vartype> &cmdmap,
6991 const OSDMap& osdmap,
6992 map<int32_t, uint32_t>* weights)
6993{
6994 string weights_str;
6995 if (!cmd_getval(g_ceph_context, cmdmap, "weights", weights_str)) {
6996 return -EINVAL;
6997 }
6998 std::replace(begin(weights_str), end(weights_str), '\'', '"');
6999 json_spirit::mValue json_value;
7000 if (!json_spirit::read(weights_str, json_value)) {
7001 return -EINVAL;
7002 }
7003 if (json_value.type() != json_spirit::obj_type) {
7004 return -EINVAL;
7005 }
7006 const auto obj = json_value.get_obj();
7007 try {
7008 for (auto& osd_weight : obj) {
7009 auto osd_id = std::stoi(osd_weight.first);
7010 if (!osdmap.exists(osd_id)) {
7011 return -ENOENT;
7012 }
7013 if (osd_weight.second.type() != json_spirit::str_type) {
7014 return -EINVAL;
7015 }
7016 auto weight = std::stoul(osd_weight.second.get_str());
7017 weights->insert({osd_id, weight});
7018 }
7019 } catch (const std::logic_error& e) {
7020 return -EINVAL;
7021 }
7022 return 0;
7023}
7024
31f18b77
FG
7025int OSDMonitor::prepare_command_osd_destroy(
7026 int32_t id,
7027 stringstream& ss)
7028{
7029 assert(paxos->is_plugged());
7030
7031 // we check if the osd exists for the benefit of `osd purge`, which may
7032 // have previously removed the osd. If the osd does not exist, return
7033 // -ENOENT to convey this, and let the caller deal with it.
7034 //
7035 // we presume that all auth secrets and config keys were removed prior
7036 // to this command being called. if they exist by now, we also assume
7037 // they must have been created by some other command and do not pertain
7038 // to this non-existent osd.
7039 if (!osdmap.exists(id)) {
7040 dout(10) << __func__ << " osd." << id << " does not exist." << dendl;
7041 return -ENOENT;
7042 }
7043
7044 uuid_d uuid = osdmap.get_uuid(id);
7045 dout(10) << __func__ << " destroying osd." << id
7046 << " uuid " << uuid << dendl;
7047
7048 // if it has been destroyed, we assume our work here is done.
7049 if (osdmap.is_destroyed(id)) {
7050 ss << "destroyed osd." << id;
7051 return 0;
7052 }
7053
7054 EntityName cephx_entity, lockbox_entity;
7055 bool idempotent_auth = false, idempotent_cks = false;
7056
7057 int err = mon->authmon()->validate_osd_destroy(id, uuid,
7058 cephx_entity,
7059 lockbox_entity,
7060 ss);
7061 if (err < 0) {
7062 if (err == -ENOENT) {
7063 idempotent_auth = true;
31f18b77
FG
7064 } else {
7065 return err;
7066 }
7067 }
7068
7069 ConfigKeyService *svc = (ConfigKeyService*)mon->config_key_service;
7070 err = svc->validate_osd_destroy(id, uuid);
7071 if (err < 0) {
7072 assert(err == -ENOENT);
7073 err = 0;
7074 idempotent_cks = true;
7075 }
7076
7077 if (!idempotent_auth) {
7078 err = mon->authmon()->do_osd_destroy(cephx_entity, lockbox_entity);
7079 assert(0 == err);
7080 }
7081
7082 if (!idempotent_cks) {
7083 svc->do_osd_destroy(id, uuid);
7084 }
7085
7086 pending_inc.new_state[id] = CEPH_OSD_DESTROYED;
7087 pending_inc.new_uuid[id] = uuid_d();
7088
7089 // we can only propose_pending() once per service, otherwise we'll be
7090 // defying PaxosService and all laws of nature. Therefore, as we may
7091 // be used during 'osd purge', let's keep the caller responsible for
7092 // proposing.
7093 assert(err == 0);
7094 return 0;
7095}
7096
7097int OSDMonitor::prepare_command_osd_purge(
7098 int32_t id,
7099 stringstream& ss)
7100{
7101 assert(paxos->is_plugged());
7102 dout(10) << __func__ << " purging osd." << id << dendl;
7103
7104 assert(!osdmap.is_up(id));
7105
7106 /*
7107 * This may look a bit weird, but this is what's going to happen:
7108 *
7109 * 1. we make sure that removing from crush works
7110 * 2. we call `prepare_command_osd_destroy()`. If it returns an
7111 * error, then we abort the whole operation, as no updates
7112 * have been made. However, we this function will have
7113 * side-effects, thus we need to make sure that all operations
7114 * performed henceforth will *always* succeed.
7115 * 3. we call `prepare_command_osd_remove()`. Although this
7116 * function can return an error, it currently only checks if the
7117 * osd is up - and we have made sure that it is not so, so there
7118 * is no conflict, and it is effectively an update.
7119 * 4. finally, we call `do_osd_crush_remove()`, which will perform
7120 * the crush update we delayed from before.
7121 */
7122
7123 CrushWrapper newcrush;
7124 _get_pending_crush(newcrush);
7125
7126 bool may_be_idempotent = false;
7127
7128 int err = _prepare_command_osd_crush_remove(newcrush, id, 0, false, false);
7129 if (err == -ENOENT) {
7130 err = 0;
7131 may_be_idempotent = true;
7132 } else if (err < 0) {
7133 ss << "error removing osd." << id << " from crush";
7134 return err;
7135 }
7136
7137 // no point destroying the osd again if it has already been marked destroyed
7138 if (!osdmap.is_destroyed(id)) {
7139 err = prepare_command_osd_destroy(id, ss);
7140 if (err < 0) {
7141 if (err == -ENOENT) {
7142 err = 0;
7143 } else {
7144 return err;
7145 }
7146 } else {
7147 may_be_idempotent = false;
7148 }
7149 }
7150 assert(0 == err);
7151
7152 if (may_be_idempotent && !osdmap.exists(id)) {
7153 dout(10) << __func__ << " osd." << id << " does not exist and "
7154 << "we are idempotent." << dendl;
7155 return -ENOENT;
7156 }
7157
7158 err = prepare_command_osd_remove(id);
7159 // we should not be busy, as we should have made sure this id is not up.
7160 assert(0 == err);
7161
7162 do_osd_crush_remove(newcrush);
7163 return 0;
7164}
7165
7c673cae
FG
7166bool OSDMonitor::prepare_command_impl(MonOpRequestRef op,
7167 map<string,cmd_vartype> &cmdmap)
7168{
7169 op->mark_osdmon_event(__func__);
7170 MMonCommand *m = static_cast<MMonCommand*>(op->get_req());
7171 bool ret = false;
7172 stringstream ss;
7173 string rs;
7174 bufferlist rdata;
7175 int err = 0;
7176
7177 string format;
7178 cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
7179 boost::scoped_ptr<Formatter> f(Formatter::create(format));
7180
7181 string prefix;
7182 cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
7183
7184 int64_t osdid;
7185 string name;
7186 bool osdid_present = cmd_getval(g_ceph_context, cmdmap, "id", osdid);
7187 if (osdid_present) {
7188 ostringstream oss;
7189 oss << "osd." << osdid;
7190 name = oss.str();
7191 }
7192
7193 // Even if there's a pending state with changes that could affect
7194 // a command, considering that said state isn't yet committed, we
7195 // just don't care about those changes if the command currently being
7196 // handled acts as a no-op against the current committed state.
7197 // In a nutshell, we assume this command happens *before*.
7198 //
7199 // Let me make this clearer:
7200 //
7201 // - If we have only one client, and that client issues some
7202 // operation that would conflict with this operation but is
7203 // still on the pending state, then we would be sure that said
7204 // operation wouldn't have returned yet, so the client wouldn't
7205 // issue this operation (unless the client didn't wait for the
7206 // operation to finish, and that would be the client's own fault).
7207 //
7208 // - If we have more than one client, each client will observe
7209 // whatever is the state at the moment of the commit. So, if we
7210 // have two clients, one issuing an unlink and another issuing a
7211 // link, and if the link happens while the unlink is still on the
7212 // pending state, from the link's point-of-view this is a no-op.
7213 // If different clients are issuing conflicting operations and
7214 // they care about that, then the clients should make sure they
7215 // enforce some kind of concurrency mechanism -- from our
7216 // perspective that's what Douglas Adams would call an SEP.
7217 //
7218 // This should be used as a general guideline for most commands handled
7219 // in this function. Adapt as you see fit, but please bear in mind that
7220 // this is the expected behavior.
7221
7222
7223 if (prefix == "osd setcrushmap" ||
7224 (prefix == "osd crush set" && !osdid_present)) {
31f18b77
FG
7225 if (pending_inc.crush.length()) {
7226 dout(10) << __func__ << " waiting for pending crush update " << dendl;
7227 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
7228 return true;
7229 }
7c673cae
FG
7230 dout(10) << "prepare_command setting new crush map" << dendl;
7231 bufferlist data(m->get_data());
7232 CrushWrapper crush;
7233 try {
7234 bufferlist::iterator bl(data.begin());
7235 crush.decode(bl);
7236 }
7237 catch (const std::exception &e) {
7238 err = -EINVAL;
7239 ss << "Failed to parse crushmap: " << e.what();
7240 goto reply;
7241 }
31f18b77
FG
7242
7243 int64_t prior_version = 0;
7244 if (cmd_getval(g_ceph_context, cmdmap, "prior_version", prior_version)) {
7245 if (prior_version == osdmap.get_crush_version() - 1) {
7246 // see if we are a resend of the last update. this is imperfect
7247 // (multiple racing updaters may not both get reliable success)
7248 // but we expect crush updaters (via this interface) to be rare-ish.
7249 bufferlist current, proposed;
7250 osdmap.crush->encode(current, mon->get_quorum_con_features());
7251 crush.encode(proposed, mon->get_quorum_con_features());
7252 if (current.contents_equal(proposed)) {
7253 dout(10) << __func__
7254 << " proposed matches current and version equals previous"
7255 << dendl;
7256 err = 0;
7257 ss << osdmap.get_crush_version();
7258 goto reply;
7259 }
7260 }
7261 if (prior_version != osdmap.get_crush_version()) {
7262 err = -EPERM;
7263 ss << "prior_version " << prior_version << " != crush version "
7264 << osdmap.get_crush_version();
7265 goto reply;
7266 }
7267 }
7c673cae 7268
31f18b77
FG
7269 if (crush.has_legacy_rulesets()) {
7270 err = -EINVAL;
7271 ss << "crush maps with ruleset != ruleid are no longer allowed";
7272 goto reply;
7273 }
7c673cae
FG
7274 if (!validate_crush_against_features(&crush, ss)) {
7275 err = -EINVAL;
7276 goto reply;
7277 }
31f18b77 7278
7c673cae
FG
7279 const auto& osdmap_pools = osdmap.get_pools();
7280 for (auto pit = osdmap_pools.begin(); pit != osdmap_pools.end(); ++pit) {
7281 const int64_t pool_id = pit->first;
7282 const pg_pool_t &pool = pit->second;
31f18b77 7283 int ruleno = pool.get_crush_rule();
7c673cae
FG
7284 if (!crush.rule_exists(ruleno)) {
7285 ss << " the crush rule no "<< ruleno << " for pool id " << pool_id << " is in use";
7286 err = -EINVAL;
7287 goto reply;
7288 }
7289 }
7290
224ce89b
WB
7291 if (g_conf->mon_osd_crush_smoke_test) {
7292 // sanity check: test some inputs to make sure this map isn't
7293 // totally broken
7294 dout(10) << " testing map" << dendl;
7295 stringstream ess;
7296 CrushTester tester(crush, ess);
7297 tester.set_max_x(50);
7298 int r = tester.test_with_fork(g_conf->mon_lease);
7299 if (r < 0) {
7300 dout(10) << " tester.test_with_fork returns " << r
7301 << ": " << ess.str() << dendl;
7302 ss << "crush smoke test failed with " << r << ": " << ess.str();
7303 err = r;
7304 goto reply;
7305 }
7306 dout(10) << " crush test result " << ess.str() << dendl;
7c673cae
FG
7307 }
7308
7c673cae 7309 pending_inc.crush = data;
31f18b77 7310 ss << osdmap.get_crush_version() + 1;
7c673cae
FG
7311 goto update;
7312
7313 } else if (prefix == "osd crush set-device-class") {
224ce89b
WB
7314 if (osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
7315 ss << "you must complete the upgrade and 'ceph osd require-osd-release "
7316 << "luminous' before using crush device classes";
7317 err = -EPERM;
7c673cae
FG
7318 goto reply;
7319 }
7320
7321 string device_class;
7322 if (!cmd_getval(g_ceph_context, cmdmap, "class", device_class)) {
7323 err = -EINVAL; // no value!
7324 goto reply;
7325 }
7326
224ce89b
WB
7327 bool stop = false;
7328 vector<string> idvec;
7329 cmd_getval(g_ceph_context, cmdmap, "ids", idvec);
7c673cae
FG
7330 CrushWrapper newcrush;
7331 _get_pending_crush(newcrush);
224ce89b
WB
7332 set<int> updated;
7333 for (unsigned j = 0; j < idvec.size() && !stop; j++) {
7334 set<int> osds;
7335 // wildcard?
7336 if (j == 0 &&
7337 (idvec[0] == "any" || idvec[0] == "all" || idvec[0] == "*")) {
7338 osdmap.get_all_osds(osds);
7339 stop = true;
7340 } else {
7341 // try traditional single osd way
7342 long osd = parse_osd_id(idvec[j].c_str(), &ss);
7343 if (osd < 0) {
7344 // ss has reason for failure
7345 ss << ", unable to parse osd id:\"" << idvec[j] << "\". ";
7346 err = -EINVAL;
7347 continue;
7348 }
7349 osds.insert(osd);
7350 }
7c673cae 7351
224ce89b
WB
7352 for (auto &osd : osds) {
7353 if (!osdmap.exists(osd)) {
7354 ss << "osd." << osd << " does not exist. ";
7355 continue;
7356 }
7c673cae 7357
224ce89b
WB
7358 ostringstream oss;
7359 oss << "osd." << osd;
7360 string name = oss.str();
7c673cae 7361
224ce89b
WB
7362 string action;
7363 if (newcrush.item_exists(osd)) {
7364 action = "updating";
7365 } else {
7366 action = "creating";
7367 newcrush.set_item_name(osd, name);
7368 }
7c673cae 7369
224ce89b
WB
7370 dout(5) << action << " crush item id " << osd << " name '" << name
7371 << "' device_class '" << device_class << "'"
7372 << dendl;
7373 err = newcrush.update_device_class(osd, device_class, name, &ss);
7374 if (err < 0) {
7375 goto reply;
7376 }
7377 if (err == 0 && !_have_pending_crush()) {
7378 if (!stop) {
7379 // for single osd only, wildcard makes too much noise
7380 ss << "set-device-class item id " << osd << " name '" << name
7381 << "' device_class '" << device_class << "': no change";
7382 }
7383 } else {
7384 updated.insert(osd);
7385 }
7386 }
7c673cae
FG
7387 }
7388
224ce89b
WB
7389 if (!updated.empty()) {
7390 pending_inc.crush.clear();
7391 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7392 ss << "set osd(s) " << updated << " to class '" << device_class << "'";
7393 getline(ss, rs);
7394 wait_for_finished_proposal(op,
7395 new Monitor::C_Command(mon,op, 0, rs, get_last_committed() + 1));
7396 return true;
7397 }
7c673cae 7398
c07f9fc5
FG
7399 } else if (prefix == "osd crush rm-device-class") {
7400 bool stop = false;
7401 vector<string> idvec;
7402 cmd_getval(g_ceph_context, cmdmap, "ids", idvec);
7403 CrushWrapper newcrush;
7404 _get_pending_crush(newcrush);
7405 set<int> updated;
7406
7407 for (unsigned j = 0; j < idvec.size() && !stop; j++) {
7408 set<int> osds;
7409
7410 // wildcard?
7411 if (j == 0 &&
7412 (idvec[0] == "any" || idvec[0] == "all" || idvec[0] == "*")) {
7413 osdmap.get_all_osds(osds);
7414 stop = true;
7415 } else {
7416 // try traditional single osd way
7417 long osd = parse_osd_id(idvec[j].c_str(), &ss);
7418 if (osd < 0) {
7419 // ss has reason for failure
7420 ss << ", unable to parse osd id:\"" << idvec[j] << "\". ";
7421 err = -EINVAL;
7422 goto reply;
7423 }
7424 osds.insert(osd);
7425 }
7426
7427 for (auto &osd : osds) {
7428 if (!osdmap.exists(osd)) {
7429 ss << "osd." << osd << " does not exist. ";
7430 continue;
7431 }
7432
7433 auto class_name = newcrush.get_item_class(osd);
c07f9fc5
FG
7434 if (!class_name) {
7435 ss << "osd." << osd << " belongs to no class, ";
7436 continue;
7437 }
7438 // note that we do not verify if class_is_in_use here
7439 // in case the device is misclassified and user wants
7440 // to overridely reset...
7441
7442 err = newcrush.remove_device_class(g_ceph_context, osd, &ss);
7443 if (err < 0) {
7444 // ss has reason for failure
7445 goto reply;
7446 }
7447 updated.insert(osd);
7448 }
7449 }
7450
7451 if (!updated.empty()) {
7452 pending_inc.crush.clear();
7453 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7454 ss << "done removing class of osd(s): " << updated;
7455 getline(ss, rs);
7456 wait_for_finished_proposal(op,
7457 new Monitor::C_Command(mon,op, 0, rs, get_last_committed() + 1));
7458 return true;
7459 }
35e4c445
FG
7460 } else if (prefix == "osd crush class rename") {
7461 string srcname, dstname;
7462 if (!cmd_getval(g_ceph_context, cmdmap, "srcname", srcname)) {
7463 err = -EINVAL;
7464 goto reply;
7465 }
7466 if (!cmd_getval(g_ceph_context, cmdmap, "dstname", dstname)) {
7467 err = -EINVAL;
7468 goto reply;
7469 }
7470
7471 CrushWrapper newcrush;
7472 _get_pending_crush(newcrush);
7473
7474 if (!newcrush.class_exists(srcname)) {
7475 err = -ENOENT;
7476 ss << "class '" << srcname << "' does not exist";
7477 goto reply;
7478 }
7479
7480 if (newcrush.class_exists(dstname)) {
7481 err = -EEXIST;
7482 ss << "class '" << dstname << "' already exists";
7483 goto reply;
7484 }
c07f9fc5 7485
35e4c445
FG
7486 err = newcrush.rename_class(srcname, dstname);
7487 if (err < 0) {
7488 ss << "fail to rename '" << srcname << "' to '" << dstname << "' : "
7489 << cpp_strerror(err);
7490 goto reply;
7491 }
7492
7493 pending_inc.crush.clear();
7494 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7495 ss << "rename class '" << srcname << "' to '" << dstname << "'";
7496 goto update;
7c673cae
FG
7497 } else if (prefix == "osd crush add-bucket") {
7498 // os crush add-bucket <name> <type>
7499 string name, typestr;
7500 cmd_getval(g_ceph_context, cmdmap, "name", name);
7501 cmd_getval(g_ceph_context, cmdmap, "type", typestr);
7502
7503 if (!_have_pending_crush() &&
7504 _get_stable_crush().name_exists(name)) {
7505 ss << "bucket '" << name << "' already exists";
7506 goto reply;
7507 }
7508
7509 CrushWrapper newcrush;
7510 _get_pending_crush(newcrush);
7511
7512 if (newcrush.name_exists(name)) {
7513 ss << "bucket '" << name << "' already exists";
7514 goto update;
7515 }
7516 int type = newcrush.get_type_id(typestr);
7517 if (type < 0) {
7518 ss << "type '" << typestr << "' does not exist";
7519 err = -EINVAL;
7520 goto reply;
7521 }
7522 if (type == 0) {
7523 ss << "type '" << typestr << "' is for devices, not buckets";
7524 err = -EINVAL;
7525 goto reply;
7526 }
7527 int bucketno;
7528 err = newcrush.add_bucket(0, 0,
7529 CRUSH_HASH_DEFAULT, type, 0, NULL,
7530 NULL, &bucketno);
7531 if (err < 0) {
7532 ss << "add_bucket error: '" << cpp_strerror(err) << "'";
7533 goto reply;
7534 }
7535 err = newcrush.set_item_name(bucketno, name);
7536 if (err < 0) {
7537 ss << "error setting bucket name to '" << name << "'";
7538 goto reply;
7539 }
7540
7541 pending_inc.crush.clear();
7542 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7543 ss << "added bucket " << name << " type " << typestr
7544 << " to crush map";
7545 goto update;
7546 } else if (prefix == "osd crush rename-bucket") {
7547 string srcname, dstname;
7548 cmd_getval(g_ceph_context, cmdmap, "srcname", srcname);
7549 cmd_getval(g_ceph_context, cmdmap, "dstname", dstname);
7550
7551 err = crush_rename_bucket(srcname, dstname, &ss);
7552 if (err == -EALREADY) // equivalent to success for idempotency
7553 err = 0;
7554 if (err)
7555 goto reply;
7556 else
7557 goto update;
c07f9fc5
FG
7558 } else if (prefix == "osd crush weight-set create" ||
7559 prefix == "osd crush weight-set create-compat") {
7560 CrushWrapper newcrush;
7561 _get_pending_crush(newcrush);
7562 int64_t pool;
7563 int positions;
7564 if (newcrush.has_non_straw2_buckets()) {
7565 ss << "crush map contains one or more bucket(s) that are not straw2";
224ce89b
WB
7566 err = -EPERM;
7567 goto reply;
7568 }
c07f9fc5
FG
7569 if (prefix == "osd crush weight-set create") {
7570 if (osdmap.require_min_compat_client > 0 &&
7571 osdmap.require_min_compat_client < CEPH_RELEASE_LUMINOUS) {
7572 ss << "require_min_compat_client "
7573 << ceph_release_name(osdmap.require_min_compat_client)
7574 << " < luminous, which is required for per-pool weight-sets. "
7575 << "Try 'ceph osd set-require-min-compat-client luminous' "
7576 << "before using the new interface";
7577 err = -EPERM;
7578 goto reply;
7579 }
7580 string poolname, mode;
7581 cmd_getval(g_ceph_context, cmdmap, "pool", poolname);
7582 pool = osdmap.lookup_pg_pool_name(poolname.c_str());
7583 if (pool < 0) {
7584 ss << "pool '" << poolname << "' not found";
7585 err = -ENOENT;
7586 goto reply;
7587 }
7588 cmd_getval(g_ceph_context, cmdmap, "mode", mode);
7589 if (mode != "flat" && mode != "positional") {
7590 ss << "unrecognized weight-set mode '" << mode << "'";
7591 err = -EINVAL;
7592 goto reply;
7593 }
7594 positions = mode == "flat" ? 1 : osdmap.get_pg_pool(pool)->get_size();
7595 } else {
7596 pool = CrushWrapper::DEFAULT_CHOOSE_ARGS;
7597 positions = 1;
224ce89b 7598 }
c07f9fc5
FG
7599 newcrush.create_choose_args(pool, positions);
7600 pending_inc.crush.clear();
7601 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7602 goto update;
224ce89b 7603
c07f9fc5
FG
7604 } else if (prefix == "osd crush weight-set rm" ||
7605 prefix == "osd crush weight-set rm-compat") {
224ce89b
WB
7606 CrushWrapper newcrush;
7607 _get_pending_crush(newcrush);
c07f9fc5
FG
7608 int64_t pool;
7609 if (prefix == "osd crush weight-set rm") {
7610 string poolname;
7611 cmd_getval(g_ceph_context, cmdmap, "pool", poolname);
7612 pool = osdmap.lookup_pg_pool_name(poolname.c_str());
7613 if (pool < 0) {
7614 ss << "pool '" << poolname << "' not found";
7615 err = -ENOENT;
7616 goto reply;
7617 }
7618 } else {
7619 pool = CrushWrapper::DEFAULT_CHOOSE_ARGS;
224ce89b 7620 }
c07f9fc5
FG
7621 newcrush.rm_choose_args(pool);
7622 pending_inc.crush.clear();
7623 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7624 goto update;
224ce89b 7625
c07f9fc5
FG
7626 } else if (prefix == "osd crush weight-set reweight" ||
7627 prefix == "osd crush weight-set reweight-compat") {
7628 string poolname, item;
7629 vector<double> weight;
7630 cmd_getval(g_ceph_context, cmdmap, "pool", poolname);
7631 cmd_getval(g_ceph_context, cmdmap, "item", item);
7632 cmd_getval(g_ceph_context, cmdmap, "weight", weight);
7633 CrushWrapper newcrush;
7634 _get_pending_crush(newcrush);
7635 int64_t pool;
7636 if (prefix == "osd crush weight-set reweight") {
7637 pool = osdmap.lookup_pg_pool_name(poolname.c_str());
7638 if (pool < 0) {
7639 ss << "pool '" << poolname << "' not found";
7640 err = -ENOENT;
7641 goto reply;
7642 }
7643 if (!newcrush.have_choose_args(pool)) {
7644 ss << "no weight-set for pool '" << poolname << "'";
7645 err = -ENOENT;
7646 goto reply;
7647 }
7648 auto arg_map = newcrush.choose_args_get(pool);
7649 int positions = newcrush.get_choose_args_positions(arg_map);
7650 if (weight.size() != (size_t)positions) {
7651 ss << "must specify exact " << positions << " weight values";
7652 err = -EINVAL;
7653 goto reply;
7654 }
7655 } else {
7656 pool = CrushWrapper::DEFAULT_CHOOSE_ARGS;
7657 if (!newcrush.have_choose_args(pool)) {
7658 ss << "no backward-compatible weight-set";
7659 err = -ENOENT;
7660 goto reply;
7661 }
224ce89b 7662 }
c07f9fc5
FG
7663 if (!newcrush.name_exists(item)) {
7664 ss << "item '" << item << "' does not exist";
7665 err = -ENOENT;
224ce89b
WB
7666 goto reply;
7667 }
c07f9fc5
FG
7668 err = newcrush.choose_args_adjust_item_weightf(
7669 g_ceph_context,
7670 newcrush.choose_args_get(pool),
7671 newcrush.get_item_id(item),
7672 weight,
7673 &ss);
224ce89b 7674 if (err < 0) {
224ce89b
WB
7675 goto reply;
7676 }
c07f9fc5 7677 err = 0;
224ce89b
WB
7678 pending_inc.crush.clear();
7679 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
224ce89b 7680 goto update;
7c673cae
FG
7681 } else if (osdid_present &&
7682 (prefix == "osd crush set" || prefix == "osd crush add")) {
7683 // <OsdName> is 'osd.<id>' or '<id>', passed as int64_t id
7684 // osd crush set <OsdName> <weight> <loc1> [<loc2> ...]
7685 // osd crush add <OsdName> <weight> <loc1> [<loc2> ...]
7686
7687 if (!osdmap.exists(osdid)) {
7688 err = -ENOENT;
c07f9fc5 7689 ss << name << " does not exist. Create it before updating the crush map";
7c673cae
FG
7690 goto reply;
7691 }
7692
7693 double weight;
7694 if (!cmd_getval(g_ceph_context, cmdmap, "weight", weight)) {
7695 ss << "unable to parse weight value '"
7696 << cmd_vartype_stringify(cmdmap["weight"]) << "'";
7697 err = -EINVAL;
7698 goto reply;
7699 }
7700
7701 string args;
7702 vector<string> argvec;
7703 cmd_getval(g_ceph_context, cmdmap, "args", argvec);
7704 map<string,string> loc;
7705 CrushWrapper::parse_loc_map(argvec, &loc);
7706
7707 if (prefix == "osd crush set"
7708 && !_get_stable_crush().item_exists(osdid)) {
7709 err = -ENOENT;
7710 ss << "unable to set item id " << osdid << " name '" << name
7711 << "' weight " << weight << " at location " << loc
7712 << ": does not exist";
7713 goto reply;
7714 }
7715
7716 dout(5) << "adding/updating crush item id " << osdid << " name '"
7717 << name << "' weight " << weight << " at location "
7718 << loc << dendl;
7719 CrushWrapper newcrush;
7720 _get_pending_crush(newcrush);
7721
7722 string action;
7723 if (prefix == "osd crush set" ||
7724 newcrush.check_item_loc(g_ceph_context, osdid, loc, (int *)NULL)) {
7725 action = "set";
7726 err = newcrush.update_item(g_ceph_context, osdid, weight, name, loc);
7727 } else {
7728 action = "add";
7729 err = newcrush.insert_item(g_ceph_context, osdid, weight, name, loc);
7730 if (err == 0)
7731 err = 1;
7732 }
7733
7734 if (err < 0)
7735 goto reply;
7736
7737 if (err == 0 && !_have_pending_crush()) {
7738 ss << action << " item id " << osdid << " name '" << name << "' weight "
7739 << weight << " at location " << loc << ": no change";
7740 goto reply;
7741 }
7742
7743 pending_inc.crush.clear();
7744 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7745 ss << action << " item id " << osdid << " name '" << name << "' weight "
7746 << weight << " at location " << loc << " to crush map";
7747 getline(ss, rs);
7748 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
7749 get_last_committed() + 1));
7750 return true;
7751
7752 } else if (prefix == "osd crush create-or-move") {
7753 do {
7754 // osd crush create-or-move <OsdName> <initial_weight> <loc1> [<loc2> ...]
7755 if (!osdmap.exists(osdid)) {
7756 err = -ENOENT;
7757 ss << name << " does not exist. create it before updating the crush map";
7758 goto reply;
7759 }
7760
7761 double weight;
7762 if (!cmd_getval(g_ceph_context, cmdmap, "weight", weight)) {
7763 ss << "unable to parse weight value '"
7764 << cmd_vartype_stringify(cmdmap["weight"]) << "'";
7765 err = -EINVAL;
7766 goto reply;
7767 }
7768
7769 string args;
7770 vector<string> argvec;
7771 cmd_getval(g_ceph_context, cmdmap, "args", argvec);
7772 map<string,string> loc;
7773 CrushWrapper::parse_loc_map(argvec, &loc);
7774
7775 dout(0) << "create-or-move crush item name '" << name << "' initial_weight " << weight
7776 << " at location " << loc << dendl;
7777
7778 CrushWrapper newcrush;
7779 _get_pending_crush(newcrush);
7780
7781 err = newcrush.create_or_move_item(g_ceph_context, osdid, weight, name, loc);
7782 if (err == 0) {
7783 ss << "create-or-move updated item name '" << name << "' weight " << weight
7784 << " at location " << loc << " to crush map";
7785 break;
7786 }
7787 if (err > 0) {
7788 pending_inc.crush.clear();
7789 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7790 ss << "create-or-move updating item name '" << name << "' weight " << weight
7791 << " at location " << loc << " to crush map";
7792 getline(ss, rs);
7793 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
7794 get_last_committed() + 1));
7795 return true;
7796 }
7797 } while (false);
7798
7799 } else if (prefix == "osd crush move") {
7800 do {
7801 // osd crush move <name> <loc1> [<loc2> ...]
7802
7803 string args;
7804 vector<string> argvec;
7805 cmd_getval(g_ceph_context, cmdmap, "name", name);
7806 cmd_getval(g_ceph_context, cmdmap, "args", argvec);
7807 map<string,string> loc;
7808 CrushWrapper::parse_loc_map(argvec, &loc);
7809
7810 dout(0) << "moving crush item name '" << name << "' to location " << loc << dendl;
7811 CrushWrapper newcrush;
7812 _get_pending_crush(newcrush);
7813
7814 if (!newcrush.name_exists(name)) {
7815 err = -ENOENT;
7816 ss << "item " << name << " does not exist";
7817 break;
7818 }
7819 int id = newcrush.get_item_id(name);
7820
7821 if (!newcrush.check_item_loc(g_ceph_context, id, loc, (int *)NULL)) {
7822 if (id >= 0) {
7823 err = newcrush.create_or_move_item(g_ceph_context, id, 0, name, loc);
7824 } else {
7825 err = newcrush.move_bucket(g_ceph_context, id, loc);
7826 }
7827 if (err >= 0) {
7828 ss << "moved item id " << id << " name '" << name << "' to location " << loc << " in crush map";
7829 pending_inc.crush.clear();
7830 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7831 getline(ss, rs);
7832 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
7833 get_last_committed() + 1));
7834 return true;
7835 }
7836 } else {
7837 ss << "no need to move item id " << id << " name '" << name << "' to location " << loc << " in crush map";
7838 err = 0;
7839 }
7840 } while (false);
31f18b77
FG
7841 } else if (prefix == "osd crush swap-bucket") {
7842 string source, dest, force;
7843 cmd_getval(g_ceph_context, cmdmap, "source", source);
7844 cmd_getval(g_ceph_context, cmdmap, "dest", dest);
7845 cmd_getval(g_ceph_context, cmdmap, "force", force);
7846 CrushWrapper newcrush;
7847 _get_pending_crush(newcrush);
7848 if (!newcrush.name_exists(source)) {
7849 ss << "source item " << source << " does not exist";
7850 err = -ENOENT;
7851 goto reply;
7852 }
7853 if (!newcrush.name_exists(dest)) {
7854 ss << "dest item " << dest << " does not exist";
7855 err = -ENOENT;
7856 goto reply;
7857 }
7858 int sid = newcrush.get_item_id(source);
7859 int did = newcrush.get_item_id(dest);
7860 int sparent;
7861 if (newcrush.get_immediate_parent_id(sid, &sparent) == 0 &&
7862 force != "--yes-i-really-mean-it") {
7863 ss << "source item " << source << " is not an orphan bucket; pass --yes-i-really-mean-it to proceed anyway";
7864 err = -EPERM;
7865 goto reply;
7866 }
7867 if (newcrush.get_bucket_alg(sid) != newcrush.get_bucket_alg(did) &&
7868 force != "--yes-i-really-mean-it") {
7869 ss << "source bucket alg " << crush_alg_name(newcrush.get_bucket_alg(sid)) << " != "
7870 << "dest bucket alg " << crush_alg_name(newcrush.get_bucket_alg(did))
7871 << "; pass --yes-i-really-mean-it to proceed anyway";
7872 err = -EPERM;
7873 goto reply;
7874 }
7875 int r = newcrush.swap_bucket(g_ceph_context, sid, did);
7876 if (r < 0) {
7877 ss << "failed to swap bucket contents: " << cpp_strerror(r);
224ce89b 7878 err = r;
31f18b77
FG
7879 goto reply;
7880 }
7881 ss << "swapped bucket of " << source << " to " << dest;
7882 pending_inc.crush.clear();
7883 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7884 wait_for_finished_proposal(op,
7885 new Monitor::C_Command(mon, op, err, ss.str(),
7886 get_last_committed() + 1));
7887 return true;
7888 } else if (prefix == "osd crush link") {
7889 // osd crush link <name> <loc1> [<loc2> ...]
7890 string name;
7891 cmd_getval(g_ceph_context, cmdmap, "name", name);
7892 vector<string> argvec;
7893 cmd_getval(g_ceph_context, cmdmap, "args", argvec);
7894 map<string,string> loc;
7895 CrushWrapper::parse_loc_map(argvec, &loc);
7896
7897 // Need an explicit check for name_exists because get_item_id returns
7898 // 0 on unfound.
7899 int id = osdmap.crush->get_item_id(name);
7c673cae
FG
7900 if (!osdmap.crush->name_exists(name)) {
7901 err = -ENOENT;
7902 ss << "item " << name << " does not exist";
7903 goto reply;
7904 } else {
7905 dout(5) << "resolved crush name '" << name << "' to id " << id << dendl;
7906 }
7907 if (osdmap.crush->check_item_loc(g_ceph_context, id, loc, (int*) NULL)) {
7908 ss << "no need to move item id " << id << " name '" << name
7909 << "' to location " << loc << " in crush map";
7910 err = 0;
7911 goto reply;
7912 }
7913
7914 dout(5) << "linking crush item name '" << name << "' at location " << loc << dendl;
7915 CrushWrapper newcrush;
7916 _get_pending_crush(newcrush);
7917
7918 if (!newcrush.name_exists(name)) {
7919 err = -ENOENT;
7920 ss << "item " << name << " does not exist";
7921 goto reply;
7922 } else {
7923 int id = newcrush.get_item_id(name);
7924 if (!newcrush.check_item_loc(g_ceph_context, id, loc, (int *)NULL)) {
7925 err = newcrush.link_bucket(g_ceph_context, id, loc);
7926 if (err >= 0) {
7927 ss << "linked item id " << id << " name '" << name
7928 << "' to location " << loc << " in crush map";
7929 pending_inc.crush.clear();
7930 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
7931 } else {
7932 ss << "cannot link item id " << id << " name '" << name
7933 << "' to location " << loc;
7934 goto reply;
7935 }
7936 } else {
7937 ss << "no need to move item id " << id << " name '" << name
7938 << "' to location " << loc << " in crush map";
7939 err = 0;
7940 }
7941 }
7942 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, err, ss.str(),
7943 get_last_committed() + 1));
7944 return true;
7945 } else if (prefix == "osd crush rm" ||
7946 prefix == "osd crush remove" ||
7947 prefix == "osd crush unlink") {
7948 do {
7949 // osd crush rm <id> [ancestor]
7950 CrushWrapper newcrush;
7951 _get_pending_crush(newcrush);
7952
7953 string name;
7954 cmd_getval(g_ceph_context, cmdmap, "name", name);
7955
7956 if (!osdmap.crush->name_exists(name)) {
7957 err = 0;
7958 ss << "device '" << name << "' does not appear in the crush map";
7959 break;
7960 }
7961 if (!newcrush.name_exists(name)) {
7962 err = 0;
7963 ss << "device '" << name << "' does not appear in the crush map";
7964 getline(ss, rs);
7965 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
7966 get_last_committed() + 1));
7967 return true;
7968 }
7969 int id = newcrush.get_item_id(name);
31f18b77
FG
7970 int ancestor = 0;
7971
7c673cae
FG
7972 bool unlink_only = prefix == "osd crush unlink";
7973 string ancestor_str;
7974 if (cmd_getval(g_ceph_context, cmdmap, "ancestor", ancestor_str)) {
7975 if (!newcrush.name_exists(ancestor_str)) {
7976 err = -ENOENT;
7977 ss << "ancestor item '" << ancestor_str
7978 << "' does not appear in the crush map";
7979 break;
7980 }
31f18b77 7981 ancestor = newcrush.get_item_id(ancestor_str);
7c673cae 7982 }
31f18b77
FG
7983
7984 err = prepare_command_osd_crush_remove(
7985 newcrush,
7986 id, ancestor,
7987 (ancestor < 0), unlink_only);
7988
7c673cae
FG
7989 if (err == -ENOENT) {
7990 ss << "item " << id << " does not appear in that position";
7991 err = 0;
7992 break;
7993 }
7994 if (err == 0) {
7c673cae
FG
7995 ss << "removed item id " << id << " name '" << name << "' from crush map";
7996 getline(ss, rs);
7997 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
7998 get_last_committed() + 1));
7999 return true;
8000 }
8001 } while (false);
8002
8003 } else if (prefix == "osd crush reweight-all") {
7c673cae
FG
8004 CrushWrapper newcrush;
8005 _get_pending_crush(newcrush);
8006
8007 newcrush.reweight(g_ceph_context);
8008 pending_inc.crush.clear();
8009 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
8010 ss << "reweighted crush hierarchy";
8011 getline(ss, rs);
8012 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8013 get_last_committed() + 1));
8014 return true;
8015 } else if (prefix == "osd crush reweight") {
8016 // osd crush reweight <name> <weight>
8017 CrushWrapper newcrush;
8018 _get_pending_crush(newcrush);
8019
8020 string name;
8021 cmd_getval(g_ceph_context, cmdmap, "name", name);
8022 if (!newcrush.name_exists(name)) {
8023 err = -ENOENT;
8024 ss << "device '" << name << "' does not appear in the crush map";
8025 goto reply;
8026 }
8027
8028 int id = newcrush.get_item_id(name);
8029 if (id < 0) {
8030 ss << "device '" << name << "' is not a leaf in the crush map";
8031 err = -EINVAL;
8032 goto reply;
8033 }
8034 double w;
8035 if (!cmd_getval(g_ceph_context, cmdmap, "weight", w)) {
8036 ss << "unable to parse weight value '"
8037 << cmd_vartype_stringify(cmdmap["weight"]) << "'";
8038 err = -EINVAL;
8039 goto reply;
8040 }
8041
8042 err = newcrush.adjust_item_weightf(g_ceph_context, id, w);
8043 if (err < 0)
8044 goto reply;
8045 pending_inc.crush.clear();
8046 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
8047 ss << "reweighted item id " << id << " name '" << name << "' to " << w
8048 << " in crush map";
8049 getline(ss, rs);
8050 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8051 get_last_committed() + 1));
8052 return true;
8053 } else if (prefix == "osd crush reweight-subtree") {
8054 // osd crush reweight <name> <weight>
8055 CrushWrapper newcrush;
8056 _get_pending_crush(newcrush);
8057
8058 string name;
8059 cmd_getval(g_ceph_context, cmdmap, "name", name);
8060 if (!newcrush.name_exists(name)) {
8061 err = -ENOENT;
8062 ss << "device '" << name << "' does not appear in the crush map";
8063 goto reply;
8064 }
8065
8066 int id = newcrush.get_item_id(name);
8067 if (id >= 0) {
8068 ss << "device '" << name << "' is not a subtree in the crush map";
8069 err = -EINVAL;
8070 goto reply;
8071 }
8072 double w;
8073 if (!cmd_getval(g_ceph_context, cmdmap, "weight", w)) {
8074 ss << "unable to parse weight value '"
8075 << cmd_vartype_stringify(cmdmap["weight"]) << "'";
8076 err = -EINVAL;
8077 goto reply;
8078 }
8079
8080 err = newcrush.adjust_subtree_weightf(g_ceph_context, id, w);
8081 if (err < 0)
8082 goto reply;
8083 pending_inc.crush.clear();
8084 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
8085 ss << "reweighted subtree id " << id << " name '" << name << "' to " << w
8086 << " in crush map";
8087 getline(ss, rs);
8088 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8089 get_last_committed() + 1));
8090 return true;
8091 } else if (prefix == "osd crush tunables") {
8092 CrushWrapper newcrush;
8093 _get_pending_crush(newcrush);
8094
8095 err = 0;
8096 string profile;
8097 cmd_getval(g_ceph_context, cmdmap, "profile", profile);
8098 if (profile == "legacy" || profile == "argonaut") {
8099 newcrush.set_tunables_legacy();
8100 } else if (profile == "bobtail") {
8101 newcrush.set_tunables_bobtail();
8102 } else if (profile == "firefly") {
8103 newcrush.set_tunables_firefly();
8104 } else if (profile == "hammer") {
8105 newcrush.set_tunables_hammer();
8106 } else if (profile == "jewel") {
8107 newcrush.set_tunables_jewel();
8108 } else if (profile == "optimal") {
8109 newcrush.set_tunables_optimal();
8110 } else if (profile == "default") {
8111 newcrush.set_tunables_default();
8112 } else {
8113 ss << "unrecognized profile '" << profile << "'";
8114 err = -EINVAL;
8115 goto reply;
8116 }
8117
8118 if (!validate_crush_against_features(&newcrush, ss)) {
8119 err = -EINVAL;
8120 goto reply;
8121 }
8122
8123 pending_inc.crush.clear();
8124 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
8125 ss << "adjusted tunables profile to " << profile;
8126 getline(ss, rs);
8127 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8128 get_last_committed() + 1));
8129 return true;
8130 } else if (prefix == "osd crush set-tunable") {
8131 CrushWrapper newcrush;
8132 _get_pending_crush(newcrush);
8133
8134 err = 0;
8135 string tunable;
8136 cmd_getval(g_ceph_context, cmdmap, "tunable", tunable);
8137
8138 int64_t value = -1;
8139 if (!cmd_getval(g_ceph_context, cmdmap, "value", value)) {
8140 err = -EINVAL;
8141 ss << "failed to parse integer value " << cmd_vartype_stringify(cmdmap["value"]);
8142 goto reply;
8143 }
8144
8145 if (tunable == "straw_calc_version") {
224ce89b 8146 if (value != 0 && value != 1) {
7c673cae
FG
8147 ss << "value must be 0 or 1; got " << value;
8148 err = -EINVAL;
8149 goto reply;
8150 }
8151 newcrush.set_straw_calc_version(value);
8152 } else {
8153 ss << "unrecognized tunable '" << tunable << "'";
8154 err = -EINVAL;
8155 goto reply;
8156 }
8157
8158 if (!validate_crush_against_features(&newcrush, ss)) {
8159 err = -EINVAL;
8160 goto reply;
8161 }
8162
8163 pending_inc.crush.clear();
8164 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
8165 ss << "adjusted tunable " << tunable << " to " << value;
8166 getline(ss, rs);
8167 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8168 get_last_committed() + 1));
8169 return true;
8170
8171 } else if (prefix == "osd crush rule create-simple") {
8172 string name, root, type, mode;
8173 cmd_getval(g_ceph_context, cmdmap, "name", name);
8174 cmd_getval(g_ceph_context, cmdmap, "root", root);
8175 cmd_getval(g_ceph_context, cmdmap, "type", type);
8176 cmd_getval(g_ceph_context, cmdmap, "mode", mode);
8177 if (mode == "")
8178 mode = "firstn";
8179
8180 if (osdmap.crush->rule_exists(name)) {
31f18b77
FG
8181 // The name is uniquely associated to a ruleid and the rule it contains
8182 // From the user point of view, the rule is more meaningfull.
8183 ss << "rule " << name << " already exists";
7c673cae
FG
8184 err = 0;
8185 goto reply;
8186 }
8187
8188 CrushWrapper newcrush;
8189 _get_pending_crush(newcrush);
8190
8191 if (newcrush.rule_exists(name)) {
31f18b77
FG
8192 // The name is uniquely associated to a ruleid and the rule it contains
8193 // From the user point of view, the rule is more meaningfull.
8194 ss << "rule " << name << " already exists";
7c673cae
FG
8195 err = 0;
8196 } else {
224ce89b 8197 int ruleno = newcrush.add_simple_rule(name, root, type, "", mode,
7c673cae
FG
8198 pg_pool_t::TYPE_REPLICATED, &ss);
8199 if (ruleno < 0) {
8200 err = ruleno;
8201 goto reply;
8202 }
8203
8204 pending_inc.crush.clear();
8205 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
8206 }
8207 getline(ss, rs);
8208 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8209 get_last_committed() + 1));
8210 return true;
8211
224ce89b
WB
8212 } else if (prefix == "osd crush rule create-replicated") {
8213 string name, root, type, device_class;
8214 cmd_getval(g_ceph_context, cmdmap, "name", name);
8215 cmd_getval(g_ceph_context, cmdmap, "root", root);
8216 cmd_getval(g_ceph_context, cmdmap, "type", type);
8217 cmd_getval(g_ceph_context, cmdmap, "class", device_class);
8218
8219 if (!device_class.empty()) {
8220 if (osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
8221 ss << "you must complete the upgrade and 'ceph osd require-osd-release "
8222 << "luminous' before using crush device classes";
8223 err = -EPERM;
8224 goto reply;
8225 }
8226 }
8227
8228 if (osdmap.crush->rule_exists(name)) {
8229 // The name is uniquely associated to a ruleid and the rule it contains
8230 // From the user point of view, the rule is more meaningfull.
8231 ss << "rule " << name << " already exists";
8232 err = 0;
8233 goto reply;
8234 }
8235
8236 CrushWrapper newcrush;
8237 _get_pending_crush(newcrush);
8238
8239 if (newcrush.rule_exists(name)) {
8240 // The name is uniquely associated to a ruleid and the rule it contains
8241 // From the user point of view, the rule is more meaningfull.
8242 ss << "rule " << name << " already exists";
8243 err = 0;
8244 } else {
8245 int ruleno = newcrush.add_simple_rule(
8246 name, root, type, device_class,
8247 "firstn", pg_pool_t::TYPE_REPLICATED, &ss);
8248 if (ruleno < 0) {
8249 err = ruleno;
8250 goto reply;
8251 }
8252
8253 pending_inc.crush.clear();
8254 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
8255 }
8256 getline(ss, rs);
8257 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8258 get_last_committed() + 1));
8259 return true;
8260
7c673cae
FG
8261 } else if (prefix == "osd erasure-code-profile rm") {
8262 string name;
8263 cmd_getval(g_ceph_context, cmdmap, "name", name);
8264
8265 if (erasure_code_profile_in_use(pending_inc.new_pools, name, &ss))
8266 goto wait;
8267
8268 if (erasure_code_profile_in_use(osdmap.pools, name, &ss)) {
8269 err = -EBUSY;
8270 goto reply;
8271 }
8272
8273 if (osdmap.has_erasure_code_profile(name) ||
8274 pending_inc.new_erasure_code_profiles.count(name)) {
8275 if (osdmap.has_erasure_code_profile(name)) {
8276 pending_inc.old_erasure_code_profiles.push_back(name);
8277 } else {
8278 dout(20) << "erasure code profile rm " << name << ": creation canceled" << dendl;
8279 pending_inc.new_erasure_code_profiles.erase(name);
8280 }
8281
8282 getline(ss, rs);
8283 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8284 get_last_committed() + 1));
8285 return true;
8286 } else {
8287 ss << "erasure-code-profile " << name << " does not exist";
8288 err = 0;
8289 goto reply;
8290 }
8291
8292 } else if (prefix == "osd erasure-code-profile set") {
8293 string name;
8294 cmd_getval(g_ceph_context, cmdmap, "name", name);
8295 vector<string> profile;
8296 cmd_getval(g_ceph_context, cmdmap, "profile", profile);
8297 bool force;
8298 if (profile.size() > 0 && profile.back() == "--force") {
8299 profile.pop_back();
8300 force = true;
8301 } else {
8302 force = false;
8303 }
8304 map<string,string> profile_map;
8305 err = parse_erasure_code_profile(profile, &profile_map, &ss);
8306 if (err)
8307 goto reply;
8308 if (profile_map.find("plugin") == profile_map.end()) {
8309 ss << "erasure-code-profile " << profile_map
8310 << " must contain a plugin entry" << std::endl;
8311 err = -EINVAL;
8312 goto reply;
8313 }
8314 string plugin = profile_map["plugin"];
8315
8316 if (pending_inc.has_erasure_code_profile(name)) {
8317 dout(20) << "erasure code profile " << name << " try again" << dendl;
8318 goto wait;
8319 } else {
8320 if (plugin == "isa" || plugin == "lrc") {
8321 err = check_cluster_features(CEPH_FEATURE_ERASURE_CODE_PLUGINS_V2, ss);
8322 if (err == -EAGAIN)
8323 goto wait;
8324 if (err)
8325 goto reply;
8326 } else if (plugin == "shec") {
8327 err = check_cluster_features(CEPH_FEATURE_ERASURE_CODE_PLUGINS_V3, ss);
8328 if (err == -EAGAIN)
8329 goto wait;
8330 if (err)
8331 goto reply;
8332 }
8333 err = normalize_profile(name, profile_map, force, &ss);
8334 if (err)
8335 goto reply;
8336
8337 if (osdmap.has_erasure_code_profile(name)) {
8338 ErasureCodeProfile existing_profile_map =
8339 osdmap.get_erasure_code_profile(name);
8340 err = normalize_profile(name, existing_profile_map, force, &ss);
8341 if (err)
8342 goto reply;
8343
8344 if (existing_profile_map == profile_map) {
8345 err = 0;
8346 goto reply;
8347 }
8348 if (!force) {
8349 err = -EPERM;
8350 ss << "will not override erasure code profile " << name
8351 << " because the existing profile "
8352 << existing_profile_map
8353 << " is different from the proposed profile "
8354 << profile_map;
8355 goto reply;
8356 }
8357 }
8358
8359 dout(20) << "erasure code profile set " << name << "="
8360 << profile_map << dendl;
8361 pending_inc.set_erasure_code_profile(name, profile_map);
8362 }
8363
8364 getline(ss, rs);
8365 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8366 get_last_committed() + 1));
8367 return true;
8368
8369 } else if (prefix == "osd crush rule create-erasure") {
8370 err = check_cluster_features(CEPH_FEATURE_CRUSH_V2, ss);
8371 if (err == -EAGAIN)
8372 goto wait;
8373 if (err)
8374 goto reply;
8375 string name, poolstr;
8376 cmd_getval(g_ceph_context, cmdmap, "name", name);
8377 string profile;
8378 cmd_getval(g_ceph_context, cmdmap, "profile", profile);
8379 if (profile == "")
8380 profile = "default";
8381 if (profile == "default") {
8382 if (!osdmap.has_erasure_code_profile(profile)) {
8383 if (pending_inc.has_erasure_code_profile(profile)) {
8384 dout(20) << "erasure code profile " << profile << " already pending" << dendl;
8385 goto wait;
8386 }
8387
8388 map<string,string> profile_map;
8389 err = osdmap.get_erasure_code_profile_default(g_ceph_context,
8390 profile_map,
8391 &ss);
8392 if (err)
8393 goto reply;
8394 err = normalize_profile(name, profile_map, true, &ss);
8395 if (err)
8396 goto reply;
8397 dout(20) << "erasure code profile set " << profile << "="
8398 << profile_map << dendl;
8399 pending_inc.set_erasure_code_profile(profile, profile_map);
8400 goto wait;
8401 }
8402 }
8403
31f18b77
FG
8404 int rule;
8405 err = crush_rule_create_erasure(name, profile, &rule, &ss);
7c673cae
FG
8406 if (err < 0) {
8407 switch(err) {
8408 case -EEXIST: // return immediately
8409 ss << "rule " << name << " already exists";
8410 err = 0;
8411 goto reply;
8412 break;
8413 case -EALREADY: // wait for pending to be proposed
8414 ss << "rule " << name << " already exists";
8415 err = 0;
8416 break;
8417 default: // non recoverable error
8418 goto reply;
8419 break;
8420 }
8421 } else {
31f18b77 8422 ss << "created rule " << name << " at " << rule;
7c673cae
FG
8423 }
8424
8425 getline(ss, rs);
8426 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8427 get_last_committed() + 1));
8428 return true;
8429
8430 } else if (prefix == "osd crush rule rm") {
8431 string name;
8432 cmd_getval(g_ceph_context, cmdmap, "name", name);
8433
8434 if (!osdmap.crush->rule_exists(name)) {
8435 ss << "rule " << name << " does not exist";
8436 err = 0;
8437 goto reply;
8438 }
8439
8440 CrushWrapper newcrush;
8441 _get_pending_crush(newcrush);
8442
8443 if (!newcrush.rule_exists(name)) {
8444 ss << "rule " << name << " does not exist";
8445 err = 0;
8446 } else {
8447 int ruleno = newcrush.get_rule_id(name);
8448 assert(ruleno >= 0);
8449
8450 // make sure it is not in use.
8451 // FIXME: this is ok in some situations, but let's not bother with that
8452 // complexity now.
8453 int ruleset = newcrush.get_rule_mask_ruleset(ruleno);
8454 if (osdmap.crush_ruleset_in_use(ruleset)) {
8455 ss << "crush ruleset " << name << " " << ruleset << " is in use";
8456 err = -EBUSY;
8457 goto reply;
8458 }
8459
8460 err = newcrush.remove_rule(ruleno);
8461 if (err < 0) {
8462 goto reply;
8463 }
8464
8465 pending_inc.crush.clear();
8466 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
8467 }
8468 getline(ss, rs);
8469 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8470 get_last_committed() + 1));
8471 return true;
8472
8473 } else if (prefix == "osd setmaxosd") {
8474 int64_t newmax;
8475 if (!cmd_getval(g_ceph_context, cmdmap, "newmax", newmax)) {
8476 ss << "unable to parse 'newmax' value '"
8477 << cmd_vartype_stringify(cmdmap["newmax"]) << "'";
8478 err = -EINVAL;
8479 goto reply;
8480 }
8481
8482 if (newmax > g_conf->mon_max_osd) {
8483 err = -ERANGE;
8484 ss << "cannot set max_osd to " << newmax << " which is > conf.mon_max_osd ("
8485 << g_conf->mon_max_osd << ")";
8486 goto reply;
8487 }
8488
8489 // Don't allow shrinking OSD number as this will cause data loss
8490 // and may cause kernel crashes.
8491 // Note: setmaxosd sets the maximum OSD number and not the number of OSDs
8492 if (newmax < osdmap.get_max_osd()) {
8493 // Check if the OSDs exist between current max and new value.
8494 // If there are any OSDs exist, then don't allow shrinking number
8495 // of OSDs.
8496 for (int i = newmax; i < osdmap.get_max_osd(); i++) {
8497 if (osdmap.exists(i)) {
8498 err = -EBUSY;
8499 ss << "cannot shrink max_osd to " << newmax
8500 << " because osd." << i << " (and possibly others) still in use";
8501 goto reply;
8502 }
8503 }
8504 }
8505
8506 pending_inc.new_max_osd = newmax;
8507 ss << "set new max_osd = " << pending_inc.new_max_osd;
8508 getline(ss, rs);
8509 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8510 get_last_committed() + 1));
8511 return true;
8512
8513 } else if (prefix == "osd set-full-ratio" ||
8514 prefix == "osd set-backfillfull-ratio" ||
8515 prefix == "osd set-nearfull-ratio") {
31f18b77 8516 if (osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
224ce89b
WB
8517 ss << "you must complete the upgrade and 'ceph osd require-osd-release "
8518 << "luminous' before using the new interface";
7c673cae
FG
8519 err = -EPERM;
8520 goto reply;
8521 }
8522 double n;
8523 if (!cmd_getval(g_ceph_context, cmdmap, "ratio", n)) {
8524 ss << "unable to parse 'ratio' value '"
224ce89b 8525 << cmd_vartype_stringify(cmdmap["ratio"]) << "'";
7c673cae
FG
8526 err = -EINVAL;
8527 goto reply;
8528 }
8529 if (prefix == "osd set-full-ratio")
8530 pending_inc.new_full_ratio = n;
8531 else if (prefix == "osd set-backfillfull-ratio")
8532 pending_inc.new_backfillfull_ratio = n;
8533 else if (prefix == "osd set-nearfull-ratio")
8534 pending_inc.new_nearfull_ratio = n;
8535 ss << prefix << " " << n;
8536 getline(ss, rs);
8537 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8538 get_last_committed() + 1));
8539 return true;
8540 } else if (prefix == "osd set-require-min-compat-client") {
31f18b77 8541 if (osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
224ce89b
WB
8542 ss << "you must complete the upgrade and 'ceph osd require-osd-release "
8543 << "luminous' before using the new interface";
7c673cae
FG
8544 err = -EPERM;
8545 goto reply;
8546 }
8547 string v;
8548 cmd_getval(g_ceph_context, cmdmap, "version", v);
31f18b77
FG
8549 int vno = ceph_release_from_name(v.c_str());
8550 if (vno <= 0) {
7c673cae
FG
8551 ss << "version " << v << " is not recognized";
8552 err = -EINVAL;
8553 goto reply;
8554 }
8555 OSDMap newmap;
8556 newmap.deepish_copy_from(osdmap);
8557 newmap.apply_incremental(pending_inc);
31f18b77
FG
8558 newmap.require_min_compat_client = vno;
8559 auto mvno = newmap.get_min_compat_client();
8560 if (vno < mvno) {
8561 ss << "osdmap current utilizes features that require "
8562 << ceph_release_name(mvno)
8563 << "; cannot set require_min_compat_client below that to "
8564 << ceph_release_name(vno);
7c673cae
FG
8565 err = -EPERM;
8566 goto reply;
8567 }
31f18b77
FG
8568 string sure;
8569 cmd_getval(g_ceph_context, cmdmap, "sure", sure);
8570 if (sure != "--yes-i-really-mean-it") {
8571 FeatureMap m;
8572 mon->get_combined_feature_map(&m);
8573 uint64_t features = ceph_release_features(vno);
8574 bool first = true;
8575 bool ok = true;
8576 for (int type : {
8577 CEPH_ENTITY_TYPE_CLIENT,
8578 CEPH_ENTITY_TYPE_MDS,
8579 CEPH_ENTITY_TYPE_MGR }) {
8580 auto p = m.m.find(type);
8581 if (p == m.m.end()) {
8582 continue;
8583 }
8584 for (auto& q : p->second) {
8585 uint64_t missing = ~q.first & features;
8586 if (missing) {
8587 if (first) {
8588 ss << "cannot set require_min_compat_client to " << v << ": ";
8589 } else {
8590 ss << "; ";
8591 }
8592 first = false;
8593 ss << q.second << " connected " << ceph_entity_type_name(type)
8594 << "(s) look like " << ceph_release_name(
8595 ceph_release_from_features(q.first))
8596 << " (missing 0x" << std::hex << missing << std::dec << ")";
8597 ok = false;
8598 }
8599 }
8600 }
8601 if (!ok) {
8602 ss << "; add --yes-i-really-mean-it to do it anyway";
8603 err = -EPERM;
8604 goto reply;
8605 }
8606 }
8607 ss << "set require_min_compat_client to " << ceph_release_name(vno);
8608 pending_inc.new_require_min_compat_client = vno;
7c673cae
FG
8609 getline(ss, rs);
8610 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
8611 get_last_committed() + 1));
8612 return true;
8613 } else if (prefix == "osd pause") {
8614 return prepare_set_flag(op, CEPH_OSDMAP_PAUSERD | CEPH_OSDMAP_PAUSEWR);
8615
8616 } else if (prefix == "osd unpause") {
8617 return prepare_unset_flag(op, CEPH_OSDMAP_PAUSERD | CEPH_OSDMAP_PAUSEWR);
8618
8619 } else if (prefix == "osd set") {
8620 string key;
8621 cmd_getval(g_ceph_context, cmdmap, "key", key);
8622 if (key == "full")
8623 return prepare_set_flag(op, CEPH_OSDMAP_FULL);
8624 else if (key == "pause")
8625 return prepare_set_flag(op, CEPH_OSDMAP_PAUSERD | CEPH_OSDMAP_PAUSEWR);
8626 else if (key == "noup")
8627 return prepare_set_flag(op, CEPH_OSDMAP_NOUP);
8628 else if (key == "nodown")
8629 return prepare_set_flag(op, CEPH_OSDMAP_NODOWN);
8630 else if (key == "noout")
8631 return prepare_set_flag(op, CEPH_OSDMAP_NOOUT);
8632 else if (key == "noin")
8633 return prepare_set_flag(op, CEPH_OSDMAP_NOIN);
8634 else if (key == "nobackfill")
8635 return prepare_set_flag(op, CEPH_OSDMAP_NOBACKFILL);
8636 else if (key == "norebalance")
8637 return prepare_set_flag(op, CEPH_OSDMAP_NOREBALANCE);
8638 else if (key == "norecover")
8639 return prepare_set_flag(op, CEPH_OSDMAP_NORECOVER);
8640 else if (key == "noscrub")
8641 return prepare_set_flag(op, CEPH_OSDMAP_NOSCRUB);
8642 else if (key == "nodeep-scrub")
8643 return prepare_set_flag(op, CEPH_OSDMAP_NODEEP_SCRUB);
8644 else if (key == "notieragent")
8645 return prepare_set_flag(op, CEPH_OSDMAP_NOTIERAGENT);
8646 else if (key == "sortbitwise") {
8647 if (osdmap.get_up_osd_features() & CEPH_FEATURE_OSD_BITWISE_HOBJ_SORT) {
8648 return prepare_set_flag(op, CEPH_OSDMAP_SORTBITWISE);
8649 } else {
8650 ss << "not all up OSDs have OSD_BITWISE_HOBJ_SORT feature";
8651 err = -EPERM;
31f18b77 8652 goto reply;
7c673cae 8653 }
c07f9fc5
FG
8654 } else if (key == "recovery_deletes") {
8655 if (HAVE_FEATURE(osdmap.get_up_osd_features(), OSD_RECOVERY_DELETES)) {
8656 return prepare_set_flag(op, CEPH_OSDMAP_RECOVERY_DELETES);
8657 } else {
8658 ss << "not all up OSDs have OSD_RECOVERY_DELETES feature";
8659 err = -EPERM;
8660 goto reply;
8661 }
7c673cae
FG
8662 } else if (key == "require_jewel_osds") {
8663 if (!osdmap.test_flag(CEPH_OSDMAP_SORTBITWISE)) {
8664 ss << "the sortbitwise flag must be set before require_jewel_osds";
8665 err = -EPERM;
31f18b77
FG
8666 goto reply;
8667 } else if (osdmap.require_osd_release >= CEPH_RELEASE_JEWEL) {
8668 ss << "require_osd_release is already >= jewel";
8669 err = 0;
8670 goto reply;
7c673cae
FG
8671 } else if (HAVE_FEATURE(osdmap.get_up_osd_features(), SERVER_JEWEL)) {
8672 return prepare_set_flag(op, CEPH_OSDMAP_REQUIRE_JEWEL);
8673 } else {
8674 ss << "not all up OSDs have CEPH_FEATURE_SERVER_JEWEL feature";
8675 err = -EPERM;
8676 }
8677 } else if (key == "require_kraken_osds") {
8678 if (!osdmap.test_flag(CEPH_OSDMAP_SORTBITWISE)) {
8679 ss << "the sortbitwise flag must be set before require_kraken_osds";
8680 err = -EPERM;
31f18b77
FG
8681 goto reply;
8682 } else if (osdmap.require_osd_release >= CEPH_RELEASE_KRAKEN) {
8683 ss << "require_osd_release is already >= kraken";
8684 err = 0;
8685 goto reply;
7c673cae
FG
8686 } else if (HAVE_FEATURE(osdmap.get_up_osd_features(), SERVER_KRAKEN)) {
8687 bool r = prepare_set_flag(op, CEPH_OSDMAP_REQUIRE_KRAKEN);
8688 // ensure JEWEL is also set
8689 pending_inc.new_flags |= CEPH_OSDMAP_REQUIRE_JEWEL;
8690 return r;
8691 } else {
8692 ss << "not all up OSDs have CEPH_FEATURE_SERVER_KRAKEN feature";
8693 err = -EPERM;
8694 }
7c673cae
FG
8695 } else {
8696 ss << "unrecognized flag '" << key << "'";
8697 err = -EINVAL;
8698 }
8699
8700 } else if (prefix == "osd unset") {
8701 string key;
8702 cmd_getval(g_ceph_context, cmdmap, "key", key);
8703 if (key == "full")
8704 return prepare_unset_flag(op, CEPH_OSDMAP_FULL);
8705 else if (key == "pause")
8706 return prepare_unset_flag(op, CEPH_OSDMAP_PAUSERD | CEPH_OSDMAP_PAUSEWR);
8707 else if (key == "noup")
8708 return prepare_unset_flag(op, CEPH_OSDMAP_NOUP);
8709 else if (key == "nodown")
8710 return prepare_unset_flag(op, CEPH_OSDMAP_NODOWN);
8711 else if (key == "noout")
8712 return prepare_unset_flag(op, CEPH_OSDMAP_NOOUT);
8713 else if (key == "noin")
8714 return prepare_unset_flag(op, CEPH_OSDMAP_NOIN);
8715 else if (key == "nobackfill")
8716 return prepare_unset_flag(op, CEPH_OSDMAP_NOBACKFILL);
8717 else if (key == "norebalance")
8718 return prepare_unset_flag(op, CEPH_OSDMAP_NOREBALANCE);
8719 else if (key == "norecover")
8720 return prepare_unset_flag(op, CEPH_OSDMAP_NORECOVER);
8721 else if (key == "noscrub")
8722 return prepare_unset_flag(op, CEPH_OSDMAP_NOSCRUB);
8723 else if (key == "nodeep-scrub")
8724 return prepare_unset_flag(op, CEPH_OSDMAP_NODEEP_SCRUB);
8725 else if (key == "notieragent")
8726 return prepare_unset_flag(op, CEPH_OSDMAP_NOTIERAGENT);
224ce89b 8727 else {
7c673cae
FG
8728 ss << "unrecognized flag '" << key << "'";
8729 err = -EINVAL;
8730 }
8731
31f18b77
FG
8732 } else if (prefix == "osd require-osd-release") {
8733 string release;
8734 cmd_getval(g_ceph_context, cmdmap, "release", release);
8735 if (!osdmap.test_flag(CEPH_OSDMAP_SORTBITWISE)) {
8736 ss << "the sortbitwise flag must be set first";
8737 err = -EPERM;
8738 goto reply;
8739 }
8740 int rel = ceph_release_from_name(release.c_str());
8741 if (rel <= 0) {
8742 ss << "unrecognized release " << release;
8743 err = -EINVAL;
8744 goto reply;
8745 }
8746 if (rel < CEPH_RELEASE_LUMINOUS) {
8747 ss << "use this command only for luminous and later";
8748 err = -EINVAL;
8749 goto reply;
8750 }
d2e6a577
FG
8751 if (rel == osdmap.require_osd_release) {
8752 // idempotent
8753 err = 0;
8754 goto reply;
8755 }
31f18b77
FG
8756 if (rel == CEPH_RELEASE_LUMINOUS) {
8757 if (!HAVE_FEATURE(osdmap.get_up_osd_features(), SERVER_LUMINOUS)) {
8758 ss << "not all up OSDs have CEPH_FEATURE_SERVER_LUMINOUS feature";
8759 err = -EPERM;
8760 goto reply;
8761 }
8762 } else {
8763 ss << "not supported for this release yet";
8764 err = -EPERM;
8765 goto reply;
8766 }
8767 if (rel < osdmap.require_osd_release) {
8768 ss << "require_osd_release cannot be lowered once it has been set";
8769 err = -EPERM;
8770 goto reply;
8771 }
8772 pending_inc.new_require_osd_release = rel;
c07f9fc5
FG
8773 if (rel >= CEPH_RELEASE_LUMINOUS &&
8774 !osdmap.test_flag(CEPH_OSDMAP_RECOVERY_DELETES)) {
8775 return prepare_set_flag(op, CEPH_OSDMAP_RECOVERY_DELETES);
8776 }
31f18b77 8777 goto update;
7c673cae
FG
8778 } else if (prefix == "osd cluster_snap") {
8779 // ** DISABLE THIS FOR NOW **
8780 ss << "cluster snapshot currently disabled (broken implementation)";
8781 // ** DISABLE THIS FOR NOW **
8782
8783 } else if (prefix == "osd down" ||
8784 prefix == "osd out" ||
8785 prefix == "osd in" ||
8786 prefix == "osd rm") {
8787
8788 bool any = false;
31f18b77
FG
8789 bool stop = false;
8790 bool verbose = true;
7c673cae
FG
8791
8792 vector<string> idvec;
8793 cmd_getval(g_ceph_context, cmdmap, "ids", idvec);
31f18b77
FG
8794 for (unsigned j = 0; j < idvec.size() && !stop; j++) {
8795 set<int> osds;
8796
8797 // wildcard?
8798 if (j == 0 &&
8799 (idvec[0] == "any" || idvec[0] == "all" || idvec[0] == "*")) {
8800 if (prefix == "osd in") {
8801 // touch out osds only
8802 osdmap.get_out_osds(osds);
8803 } else {
8804 osdmap.get_all_osds(osds);
8805 }
8806 stop = true;
8807 verbose = false; // so the output is less noisy.
8808 } else {
8809 long osd = parse_osd_id(idvec[j].c_str(), &ss);
8810 if (osd < 0) {
8811 ss << "invalid osd id" << osd;
8812 err = -EINVAL;
8813 continue;
8814 } else if (!osdmap.exists(osd)) {
8815 ss << "osd." << osd << " does not exist. ";
8816 continue;
8817 }
8818
8819 osds.insert(osd);
7c673cae 8820 }
31f18b77
FG
8821
8822 for (auto &osd : osds) {
8823 if (prefix == "osd down") {
8824 if (osdmap.is_down(osd)) {
8825 if (verbose)
8826 ss << "osd." << osd << " is already down. ";
8827 } else {
8828 pending_inc.pending_osd_state_set(osd, CEPH_OSD_UP);
8829 ss << "marked down osd." << osd << ". ";
8830 any = true;
8831 }
8832 } else if (prefix == "osd out") {
8833 if (osdmap.is_out(osd)) {
8834 if (verbose)
8835 ss << "osd." << osd << " is already out. ";
8836 } else {
8837 pending_inc.new_weight[osd] = CEPH_OSD_OUT;
8838 if (osdmap.osd_weight[osd]) {
8839 if (pending_inc.new_xinfo.count(osd) == 0) {
8840 pending_inc.new_xinfo[osd] = osdmap.osd_xinfo[osd];
8841 }
8842 pending_inc.new_xinfo[osd].old_weight = osdmap.osd_weight[osd];
7c673cae 8843 }
31f18b77 8844 ss << "marked out osd." << osd << ". ";
224ce89b
WB
8845 std::ostringstream msg;
8846 msg << "Client " << op->get_session()->entity_name
8847 << " marked osd." << osd << " out";
8848 if (osdmap.is_up(osd)) {
8849 msg << ", while it was still marked up";
8850 } else {
8851 msg << ", after it was down for " << int(down_pending_out[osd].sec())
8852 << " seconds";
8853 }
8854
8855 mon->clog->info() << msg.str();
31f18b77 8856 any = true;
7c673cae 8857 }
31f18b77
FG
8858 } else if (prefix == "osd in") {
8859 if (osdmap.is_in(osd)) {
8860 if (verbose)
8861 ss << "osd." << osd << " is already in. ";
8862 } else {
8863 if (osdmap.osd_xinfo[osd].old_weight > 0) {
8864 pending_inc.new_weight[osd] = osdmap.osd_xinfo[osd].old_weight;
8865 if (pending_inc.new_xinfo.count(osd) == 0) {
8866 pending_inc.new_xinfo[osd] = osdmap.osd_xinfo[osd];
8867 }
8868 pending_inc.new_xinfo[osd].old_weight = 0;
8869 } else {
8870 pending_inc.new_weight[osd] = CEPH_OSD_IN;
7c673cae 8871 }
31f18b77
FG
8872 ss << "marked in osd." << osd << ". ";
8873 any = true;
8874 }
8875 } else if (prefix == "osd rm") {
8876 err = prepare_command_osd_remove(osd);
8877
8878 if (err == -EBUSY) {
8879 if (any)
8880 ss << ", ";
8881 ss << "osd." << osd << " is still up; must be down before removal. ";
7c673cae 8882 } else {
31f18b77
FG
8883 assert(err == 0);
8884 if (any) {
8885 ss << ", osd." << osd;
8886 } else {
8887 ss << "removed osd." << osd;
8888 }
8889 any = true;
7c673cae 8890 }
31f18b77
FG
8891 }
8892 }
8893 }
8894 if (any) {
8895 getline(ss, rs);
8896 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, err, rs,
8897 get_last_committed() + 1));
8898 return true;
8899 }
8900 } else if (prefix == "osd add-noup" ||
8901 prefix == "osd add-nodown" ||
8902 prefix == "osd add-noin" ||
8903 prefix == "osd add-noout") {
8904
8905 enum {
8906 OP_NOUP,
8907 OP_NODOWN,
8908 OP_NOIN,
8909 OP_NOOUT,
8910 } option;
8911
8912 if (prefix == "osd add-noup") {
8913 option = OP_NOUP;
8914 } else if (prefix == "osd add-nodown") {
8915 option = OP_NODOWN;
8916 } else if (prefix == "osd add-noin") {
8917 option = OP_NOIN;
8918 } else {
8919 option = OP_NOOUT;
8920 }
8921
8922 bool any = false;
8923 bool stop = false;
8924
8925 vector<string> idvec;
8926 cmd_getval(g_ceph_context, cmdmap, "ids", idvec);
8927 for (unsigned j = 0; j < idvec.size() && !stop; j++) {
8928
8929 set<int> osds;
8930
8931 // wildcard?
8932 if (j == 0 &&
8933 (idvec[0] == "any" || idvec[0] == "all" || idvec[0] == "*")) {
8934 osdmap.get_all_osds(osds);
8935 stop = true;
8936 } else {
8937 // try traditional single osd way
8938
8939 long osd = parse_osd_id(idvec[j].c_str(), &ss);
8940 if (osd < 0) {
8941 // ss has reason for failure
8942 ss << ", unable to parse osd id:\"" << idvec[j] << "\". ";
8943 err = -EINVAL;
8944 continue;
8945 }
8946
8947 osds.insert(osd);
8948 }
8949
8950 for (auto &osd : osds) {
8951
8952 if (!osdmap.exists(osd)) {
8953 ss << "osd." << osd << " does not exist. ";
8954 continue;
8955 }
8956
8957 switch (option) {
8958 case OP_NOUP:
8959 if (osdmap.is_up(osd)) {
8960 ss << "osd." << osd << " is already up. ";
8961 continue;
8962 }
8963
8964 if (osdmap.is_noup(osd)) {
8965 if (pending_inc.pending_osd_state_clear(osd, CEPH_OSD_NOUP))
8966 any = true;
7c673cae 8967 } else {
31f18b77
FG
8968 pending_inc.pending_osd_state_set(osd, CEPH_OSD_NOUP);
8969 any = true;
7c673cae 8970 }
31f18b77
FG
8971
8972 break;
8973
8974 case OP_NODOWN:
8975 if (osdmap.is_down(osd)) {
8976 ss << "osd." << osd << " is already down. ";
8977 continue;
8978 }
8979
8980 if (osdmap.is_nodown(osd)) {
8981 if (pending_inc.pending_osd_state_clear(osd, CEPH_OSD_NODOWN))
8982 any = true;
8983 } else {
8984 pending_inc.pending_osd_state_set(osd, CEPH_OSD_NODOWN);
8985 any = true;
8986 }
8987
8988 break;
8989
8990 case OP_NOIN:
8991 if (osdmap.is_in(osd)) {
8992 ss << "osd." << osd << " is already in. ";
8993 continue;
8994 }
8995
8996 if (osdmap.is_noin(osd)) {
8997 if (pending_inc.pending_osd_state_clear(osd, CEPH_OSD_NOIN))
8998 any = true;
8999 } else {
9000 pending_inc.pending_osd_state_set(osd, CEPH_OSD_NOIN);
9001 any = true;
9002 }
9003
9004 break;
9005
9006 case OP_NOOUT:
9007 if (osdmap.is_out(osd)) {
9008 ss << "osd." << osd << " is already out. ";
9009 continue;
9010 }
9011
9012 if (osdmap.is_noout(osd)) {
9013 if (pending_inc.pending_osd_state_clear(osd, CEPH_OSD_NOOUT))
9014 any = true;
9015 } else {
9016 pending_inc.pending_osd_state_set(osd, CEPH_OSD_NOOUT);
9017 any = true;
9018 }
9019
9020 break;
9021
9022 default:
9023 assert(0 == "invalid option");
9024 }
7c673cae
FG
9025 }
9026 }
31f18b77 9027
7c673cae
FG
9028 if (any) {
9029 getline(ss, rs);
9030 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, err, rs,
31f18b77
FG
9031 get_last_committed() + 1));
9032 return true;
9033 }
9034 } else if (prefix == "osd rm-noup" ||
9035 prefix == "osd rm-nodown" ||
9036 prefix == "osd rm-noin" ||
9037 prefix == "osd rm-noout") {
9038
9039 enum {
9040 OP_NOUP,
9041 OP_NODOWN,
9042 OP_NOIN,
9043 OP_NOOUT,
9044 } option;
9045
9046 if (prefix == "osd rm-noup") {
9047 option = OP_NOUP;
9048 } else if (prefix == "osd rm-nodown") {
9049 option = OP_NODOWN;
9050 } else if (prefix == "osd rm-noin") {
9051 option = OP_NOIN;
9052 } else {
9053 option = OP_NOOUT;
9054 }
9055
9056 bool any = false;
9057 bool stop = false;
9058
9059 vector<string> idvec;
9060 cmd_getval(g_ceph_context, cmdmap, "ids", idvec);
9061
9062 for (unsigned j = 0; j < idvec.size() && !stop; j++) {
9063
9064 vector<int> osds;
9065
9066 // wildcard?
9067 if (j == 0 &&
9068 (idvec[0] == "any" || idvec[0] == "all" || idvec[0] == "*")) {
9069
9070 // touch previous noup/nodown/noin/noout osds only
9071 switch (option) {
9072 case OP_NOUP:
9073 osdmap.get_noup_osds(&osds);
9074 break;
9075 case OP_NODOWN:
9076 osdmap.get_nodown_osds(&osds);
9077 break;
9078 case OP_NOIN:
9079 osdmap.get_noin_osds(&osds);
9080 break;
9081 case OP_NOOUT:
9082 osdmap.get_noout_osds(&osds);
9083 break;
9084 default:
9085 assert(0 == "invalid option");
9086 }
9087
9088 // cancel any pending noup/nodown/noin/noout requests too
9089 vector<int> pending_state_osds;
9090 (void) pending_inc.get_pending_state_osds(&pending_state_osds);
9091 for (auto &p : pending_state_osds) {
9092
9093 switch (option) {
9094 case OP_NOUP:
9095 if (!osdmap.is_noup(p) &&
9096 pending_inc.pending_osd_state_clear(p, CEPH_OSD_NOUP)) {
9097 any = true;
9098 }
9099 break;
9100
9101 case OP_NODOWN:
9102 if (!osdmap.is_nodown(p) &&
9103 pending_inc.pending_osd_state_clear(p, CEPH_OSD_NODOWN)) {
9104 any = true;
9105 }
9106 break;
9107
9108 case OP_NOIN:
9109 if (!osdmap.is_noin(p) &&
9110 pending_inc.pending_osd_state_clear(p, CEPH_OSD_NOIN)) {
9111 any = true;
9112 }
9113 break;
9114
9115 case OP_NOOUT:
9116 if (!osdmap.is_noout(p) &&
9117 pending_inc.pending_osd_state_clear(p, CEPH_OSD_NOOUT)) {
9118 any = true;
9119 }
9120 break;
9121
9122 default:
9123 assert(0 == "invalid option");
9124 }
9125 }
9126
9127 stop = true;
9128 } else {
9129 // try traditional single osd way
9130
9131 long osd = parse_osd_id(idvec[j].c_str(), &ss);
9132 if (osd < 0) {
9133 // ss has reason for failure
9134 ss << ", unable to parse osd id:\"" << idvec[j] << "\". ";
9135 err = -EINVAL;
9136 continue;
9137 }
9138
9139 osds.push_back(osd);
9140 }
9141
9142 for (auto &osd : osds) {
9143
9144 if (!osdmap.exists(osd)) {
9145 ss << "osd." << osd << " does not exist. ";
9146 continue;
9147 }
9148
9149 switch (option) {
9150 case OP_NOUP:
9151 if (osdmap.is_noup(osd)) {
9152 pending_inc.pending_osd_state_set(osd, CEPH_OSD_NOUP);
9153 any = true;
9154 } else if (pending_inc.pending_osd_state_clear(
9155 osd, CEPH_OSD_NOUP)) {
9156 any = true;
9157 }
9158 break;
9159
9160 case OP_NODOWN:
9161 if (osdmap.is_nodown(osd)) {
9162 pending_inc.pending_osd_state_set(osd, CEPH_OSD_NODOWN);
9163 any = true;
9164 } else if (pending_inc.pending_osd_state_clear(
9165 osd, CEPH_OSD_NODOWN)) {
9166 any = true;
9167 }
9168 break;
9169
9170 case OP_NOIN:
9171 if (osdmap.is_noin(osd)) {
9172 pending_inc.pending_osd_state_set(osd, CEPH_OSD_NOIN);
9173 any = true;
9174 } else if (pending_inc.pending_osd_state_clear(
9175 osd, CEPH_OSD_NOIN)) {
9176 any = true;
9177 }
9178 break;
9179
9180 case OP_NOOUT:
9181 if (osdmap.is_noout(osd)) {
9182 pending_inc.pending_osd_state_set(osd, CEPH_OSD_NOOUT);
9183 any = true;
9184 } else if (pending_inc.pending_osd_state_clear(
9185 osd, CEPH_OSD_NOOUT)) {
9186 any = true;
9187 }
9188 break;
9189
9190 default:
9191 assert(0 == "invalid option");
9192 }
9193 }
9194 }
9195
9196 if (any) {
9197 getline(ss, rs);
9198 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, err, rs,
9199 get_last_committed() + 1));
7c673cae
FG
9200 return true;
9201 }
9202 } else if (prefix == "osd pg-temp") {
9203 string pgidstr;
9204 if (!cmd_getval(g_ceph_context, cmdmap, "pgid", pgidstr)) {
9205 ss << "unable to parse 'pgid' value '"
9206 << cmd_vartype_stringify(cmdmap["pgid"]) << "'";
9207 err = -EINVAL;
9208 goto reply;
9209 }
9210 pg_t pgid;
9211 if (!pgid.parse(pgidstr.c_str())) {
9212 ss << "invalid pgid '" << pgidstr << "'";
9213 err = -EINVAL;
9214 goto reply;
9215 }
9216 if (!osdmap.pg_exists(pgid)) {
9217 ss << "pg " << pgid << " does not exist";
9218 err = -ENOENT;
9219 goto reply;
9220 }
9221 if (pending_inc.new_pg_temp.count(pgid)) {
9222 dout(10) << __func__ << " waiting for pending update on " << pgid << dendl;
9223 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
9224 return true;
9225 }
9226
9227 vector<int64_t> id_vec;
9228 vector<int32_t> new_pg_temp;
9229 if (!cmd_getval(g_ceph_context, cmdmap, "id", id_vec)) {
9230 ss << "unable to parse 'id' value(s) '"
9231 << cmd_vartype_stringify(cmdmap["id"]) << "'";
9232 err = -EINVAL;
9233 goto reply;
9234 }
9235 for (auto osd : id_vec) {
9236 if (!osdmap.exists(osd)) {
9237 ss << "osd." << osd << " does not exist";
9238 err = -ENOENT;
9239 goto reply;
9240 }
9241 new_pg_temp.push_back(osd);
9242 }
9243
224ce89b
WB
9244 int pool_min_size = osdmap.get_pg_pool_min_size(pgid);
9245 if ((int)new_pg_temp.size() < pool_min_size) {
9246 ss << "num of osds (" << new_pg_temp.size() <<") < pool min size ("
9247 << pool_min_size << ")";
9248 err = -EINVAL;
9249 goto reply;
9250 }
9251
9252 int pool_size = osdmap.get_pg_pool_size(pgid);
9253 if ((int)new_pg_temp.size() > pool_size) {
9254 ss << "num of osds (" << new_pg_temp.size() <<") > pool size ("
9255 << pool_size << ")";
9256 err = -EINVAL;
9257 goto reply;
9258 }
9259
7c673cae
FG
9260 pending_inc.new_pg_temp[pgid] = mempool::osdmap::vector<int>(
9261 new_pg_temp.begin(), new_pg_temp.end());
9262 ss << "set " << pgid << " pg_temp mapping to " << new_pg_temp;
9263 goto update;
9264 } else if (prefix == "osd primary-temp") {
9265 string pgidstr;
9266 if (!cmd_getval(g_ceph_context, cmdmap, "pgid", pgidstr)) {
9267 ss << "unable to parse 'pgid' value '"
9268 << cmd_vartype_stringify(cmdmap["pgid"]) << "'";
9269 err = -EINVAL;
9270 goto reply;
9271 }
9272 pg_t pgid;
9273 if (!pgid.parse(pgidstr.c_str())) {
9274 ss << "invalid pgid '" << pgidstr << "'";
9275 err = -EINVAL;
9276 goto reply;
9277 }
9278 if (!osdmap.pg_exists(pgid)) {
9279 ss << "pg " << pgid << " does not exist";
9280 err = -ENOENT;
9281 goto reply;
9282 }
9283
9284 int64_t osd;
9285 if (!cmd_getval(g_ceph_context, cmdmap, "id", osd)) {
9286 ss << "unable to parse 'id' value '"
9287 << cmd_vartype_stringify(cmdmap["id"]) << "'";
9288 err = -EINVAL;
9289 goto reply;
9290 }
9291 if (osd != -1 && !osdmap.exists(osd)) {
9292 ss << "osd." << osd << " does not exist";
9293 err = -ENOENT;
9294 goto reply;
9295 }
9296
31f18b77
FG
9297 if (osdmap.require_min_compat_client > 0 &&
9298 osdmap.require_min_compat_client < CEPH_RELEASE_FIREFLY) {
9299 ss << "require_min_compat_client "
9300 << ceph_release_name(osdmap.require_min_compat_client)
7c673cae
FG
9301 << " < firefly, which is required for primary-temp";
9302 err = -EPERM;
9303 goto reply;
9304 } else if (!g_conf->mon_osd_allow_primary_temp) {
9305 ss << "you must enable 'mon osd allow primary temp = true' on the mons before you can set primary_temp mappings. note that this is for developers only: older clients/OSDs will break and there is no feature bit infrastructure in place.";
9306 err = -EPERM;
9307 goto reply;
9308 }
9309
9310 pending_inc.new_primary_temp[pgid] = osd;
9311 ss << "set " << pgid << " primary_temp mapping to " << osd;
9312 goto update;
224ce89b
WB
9313 } else if (prefix == "osd pg-upmap" ||
9314 prefix == "osd rm-pg-upmap" ||
9315 prefix == "osd pg-upmap-items" ||
9316 prefix == "osd rm-pg-upmap-items") {
31f18b77 9317 if (osdmap.require_osd_release < CEPH_RELEASE_LUMINOUS) {
224ce89b
WB
9318 ss << "you must complete the upgrade and 'ceph osd require-osd-release "
9319 << "luminous' before using the new interface";
7c673cae
FG
9320 err = -EPERM;
9321 goto reply;
9322 }
31f18b77
FG
9323 if (osdmap.require_min_compat_client < CEPH_RELEASE_LUMINOUS) {
9324 ss << "min_compat_client "
9325 << ceph_release_name(osdmap.require_min_compat_client)
224ce89b
WB
9326 << " < luminous, which is required for pg-upmap. "
9327 << "Try 'ceph osd set-require-min-compat-client luminous' "
9328 << "before using the new interface";
7c673cae
FG
9329 err = -EPERM;
9330 goto reply;
9331 }
9332 err = check_cluster_features(CEPH_FEATUREMASK_OSDMAP_PG_UPMAP, ss);
9333 if (err == -EAGAIN)
9334 goto wait;
9335 if (err < 0)
9336 goto reply;
9337 string pgidstr;
9338 if (!cmd_getval(g_ceph_context, cmdmap, "pgid", pgidstr)) {
9339 ss << "unable to parse 'pgid' value '"
9340 << cmd_vartype_stringify(cmdmap["pgid"]) << "'";
9341 err = -EINVAL;
9342 goto reply;
9343 }
9344 pg_t pgid;
9345 if (!pgid.parse(pgidstr.c_str())) {
9346 ss << "invalid pgid '" << pgidstr << "'";
9347 err = -EINVAL;
9348 goto reply;
9349 }
9350 if (!osdmap.pg_exists(pgid)) {
9351 ss << "pg " << pgid << " does not exist";
9352 err = -ENOENT;
9353 goto reply;
9354 }
224ce89b
WB
9355
9356 enum {
9357 OP_PG_UPMAP,
9358 OP_RM_PG_UPMAP,
9359 OP_PG_UPMAP_ITEMS,
9360 OP_RM_PG_UPMAP_ITEMS,
9361 } option;
9362
9363 if (prefix == "osd pg-upmap") {
9364 option = OP_PG_UPMAP;
9365 } else if (prefix == "osd rm-pg-upmap") {
9366 option = OP_RM_PG_UPMAP;
9367 } else if (prefix == "osd pg-upmap-items") {
9368 option = OP_PG_UPMAP_ITEMS;
9369 } else {
9370 option = OP_RM_PG_UPMAP_ITEMS;
7c673cae 9371 }
224ce89b
WB
9372
9373 // check pending upmap changes
9374 switch (option) {
9375 case OP_PG_UPMAP: // fall through
9376 case OP_RM_PG_UPMAP:
9377 if (pending_inc.new_pg_upmap.count(pgid) ||
9378 pending_inc.old_pg_upmap.count(pgid)) {
9379 dout(10) << __func__ << " waiting for pending update on "
9380 << pgid << dendl;
9381 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
9382 return true;
7c673cae 9383 }
224ce89b 9384 break;
7c673cae 9385
224ce89b
WB
9386 case OP_PG_UPMAP_ITEMS: // fall through
9387 case OP_RM_PG_UPMAP_ITEMS:
9388 if (pending_inc.new_pg_upmap_items.count(pgid) ||
9389 pending_inc.old_pg_upmap_items.count(pgid)) {
9390 dout(10) << __func__ << " waiting for pending update on "
9391 << pgid << dendl;
9392 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
9393 return true;
9394 }
9395 break;
7c673cae 9396
224ce89b
WB
9397 default:
9398 assert(0 == "invalid option");
7c673cae 9399 }
224ce89b
WB
9400
9401 switch (option) {
9402 case OP_PG_UPMAP:
9403 {
9404 vector<int64_t> id_vec;
9405 if (!cmd_getval(g_ceph_context, cmdmap, "id", id_vec)) {
9406 ss << "unable to parse 'id' value(s) '"
9407 << cmd_vartype_stringify(cmdmap["id"]) << "'";
9408 err = -EINVAL;
9409 goto reply;
9410 }
9411
9412 int pool_min_size = osdmap.get_pg_pool_min_size(pgid);
9413 if ((int)id_vec.size() < pool_min_size) {
9414 ss << "num of osds (" << id_vec.size() <<") < pool min size ("
9415 << pool_min_size << ")";
9416 err = -EINVAL;
9417 goto reply;
9418 }
9419
9420 int pool_size = osdmap.get_pg_pool_size(pgid);
9421 if ((int)id_vec.size() > pool_size) {
9422 ss << "num of osds (" << id_vec.size() <<") > pool size ("
9423 << pool_size << ")";
9424 err = -EINVAL;
9425 goto reply;
9426 }
9427
9428 vector<int32_t> new_pg_upmap;
9429 for (auto osd : id_vec) {
9430 if (osd != CRUSH_ITEM_NONE && !osdmap.exists(osd)) {
9431 ss << "osd." << osd << " does not exist";
9432 err = -ENOENT;
9433 goto reply;
9434 }
9435 auto it = std::find(new_pg_upmap.begin(), new_pg_upmap.end(), osd);
9436 if (it != new_pg_upmap.end()) {
9437 ss << "osd." << osd << " already exists, ";
9438 continue;
9439 }
9440 new_pg_upmap.push_back(osd);
9441 }
9442
9443 if (new_pg_upmap.empty()) {
9444 ss << "no valid upmap items(pairs) is specified";
9445 err = -EINVAL;
9446 goto reply;
9447 }
9448
9449 pending_inc.new_pg_upmap[pgid] = mempool::osdmap::vector<int32_t>(
9450 new_pg_upmap.begin(), new_pg_upmap.end());
9451 ss << "set " << pgid << " pg_upmap mapping to " << new_pg_upmap;
7c673cae 9452 }
224ce89b
WB
9453 break;
9454
9455 case OP_RM_PG_UPMAP:
9456 {
9457 pending_inc.old_pg_upmap.insert(pgid);
9458 ss << "clear " << pgid << " pg_upmap mapping";
7c673cae 9459 }
224ce89b 9460 break;
7c673cae 9461
224ce89b
WB
9462 case OP_PG_UPMAP_ITEMS:
9463 {
9464 vector<int64_t> id_vec;
9465 if (!cmd_getval(g_ceph_context, cmdmap, "id", id_vec)) {
9466 ss << "unable to parse 'id' value(s) '"
9467 << cmd_vartype_stringify(cmdmap["id"]) << "'";
9468 err = -EINVAL;
9469 goto reply;
9470 }
9471
9472 if (id_vec.size() % 2) {
9473 ss << "you must specify pairs of osd ids to be remapped";
9474 err = -EINVAL;
9475 goto reply;
9476 }
9477
9478 int pool_size = osdmap.get_pg_pool_size(pgid);
9479 if ((int)(id_vec.size() / 2) > pool_size) {
9480 ss << "num of osd pairs (" << id_vec.size() / 2 <<") > pool size ("
9481 << pool_size << ")";
9482 err = -EINVAL;
9483 goto reply;
9484 }
9485
9486 vector<pair<int32_t,int32_t>> new_pg_upmap_items;
9487 ostringstream items;
9488 items << "[";
9489 for (auto p = id_vec.begin(); p != id_vec.end(); ++p) {
9490 int from = *p++;
9491 int to = *p;
9492 if (from == to) {
9493 ss << "from osd." << from << " == to osd." << to << ", ";
9494 continue;
9495 }
9496 if (!osdmap.exists(from)) {
9497 ss << "osd." << from << " does not exist";
9498 err = -ENOENT;
9499 goto reply;
9500 }
9501 if (to != CRUSH_ITEM_NONE && !osdmap.exists(to)) {
9502 ss << "osd." << to << " does not exist";
9503 err = -ENOENT;
9504 goto reply;
9505 }
c07f9fc5
FG
9506 pair<int32_t,int32_t> entry = make_pair(from, to);
9507 auto it = std::find(new_pg_upmap_items.begin(),
9508 new_pg_upmap_items.end(), entry);
9509 if (it != new_pg_upmap_items.end()) {
9510 ss << "osd." << from << " -> osd." << to << " already exists, ";
9511 continue;
9512 }
9513 new_pg_upmap_items.push_back(entry);
224ce89b
WB
9514 items << from << "->" << to << ",";
9515 }
9516 string out(items.str());
9517 out.resize(out.size() - 1); // drop last ','
9518 out += "]";
9519
9520 if (new_pg_upmap_items.empty()) {
9521 ss << "no valid upmap items(pairs) is specified";
9522 err = -EINVAL;
9523 goto reply;
9524 }
9525
9526 pending_inc.new_pg_upmap_items[pgid] =
9527 mempool::osdmap::vector<pair<int32_t,int32_t>>(
9528 new_pg_upmap_items.begin(), new_pg_upmap_items.end());
9529 ss << "set " << pgid << " pg_upmap_items mapping to " << out;
9530 }
9531 break;
9532
9533 case OP_RM_PG_UPMAP_ITEMS:
9534 {
9535 pending_inc.old_pg_upmap_items.insert(pgid);
9536 ss << "clear " << pgid << " pg_upmap_items mapping";
9537 }
9538 break;
9539
9540 default:
9541 assert(0 == "invalid option");
7c673cae
FG
9542 }
9543
7c673cae
FG
9544 goto update;
9545 } else if (prefix == "osd primary-affinity") {
9546 int64_t id;
9547 if (!cmd_getval(g_ceph_context, cmdmap, "id", id)) {
9548 ss << "invalid osd id value '"
9549 << cmd_vartype_stringify(cmdmap["id"]) << "'";
9550 err = -EINVAL;
9551 goto reply;
9552 }
9553 double w;
9554 if (!cmd_getval(g_ceph_context, cmdmap, "weight", w)) {
9555 ss << "unable to parse 'weight' value '"
9556 << cmd_vartype_stringify(cmdmap["weight"]) << "'";
9557 err = -EINVAL;
9558 goto reply;
9559 }
9560 long ww = (int)((double)CEPH_OSD_MAX_PRIMARY_AFFINITY*w);
9561 if (ww < 0L) {
9562 ss << "weight must be >= 0";
9563 err = -EINVAL;
9564 goto reply;
9565 }
31f18b77
FG
9566 if (osdmap.require_min_compat_client > 0 &&
9567 osdmap.require_min_compat_client < CEPH_RELEASE_FIREFLY) {
9568 ss << "require_min_compat_client "
9569 << ceph_release_name(osdmap.require_min_compat_client)
7c673cae
FG
9570 << " < firefly, which is required for primary-affinity";
9571 err = -EPERM;
9572 goto reply;
9573 } else if (!g_conf->mon_osd_allow_primary_affinity) {
9574 ss << "you must enable 'mon osd allow primary affinity = true' on the mons before you can adjust primary-affinity. note that older clients will no longer be able to communicate with the cluster.";
9575 err = -EPERM;
9576 goto reply;
9577 }
9578 err = check_cluster_features(CEPH_FEATURE_OSD_PRIMARY_AFFINITY, ss);
9579 if (err == -EAGAIN)
9580 goto wait;
9581 if (err < 0)
9582 goto reply;
9583 if (osdmap.exists(id)) {
9584 pending_inc.new_primary_affinity[id] = ww;
9585 ss << "set osd." << id << " primary-affinity to " << w << " (" << ios::hex << ww << ios::dec << ")";
9586 getline(ss, rs);
9587 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
9588 get_last_committed() + 1));
9589 return true;
9590 } else {
9591 ss << "osd." << id << " does not exist";
9592 err = -ENOENT;
9593 goto reply;
9594 }
9595 } else if (prefix == "osd reweight") {
9596 int64_t id;
9597 if (!cmd_getval(g_ceph_context, cmdmap, "id", id)) {
9598 ss << "unable to parse osd id value '"
9599 << cmd_vartype_stringify(cmdmap["id"]) << "'";
9600 err = -EINVAL;
9601 goto reply;
9602 }
9603 double w;
9604 if (!cmd_getval(g_ceph_context, cmdmap, "weight", w)) {
9605 ss << "unable to parse weight value '"
9606 << cmd_vartype_stringify(cmdmap["weight"]) << "'";
9607 err = -EINVAL;
9608 goto reply;
9609 }
9610 long ww = (int)((double)CEPH_OSD_IN*w);
9611 if (ww < 0L) {
9612 ss << "weight must be >= 0";
9613 err = -EINVAL;
9614 goto reply;
9615 }
9616 if (osdmap.exists(id)) {
9617 pending_inc.new_weight[id] = ww;
9618 ss << "reweighted osd." << id << " to " << w << " (" << std::hex << ww << std::dec << ")";
9619 getline(ss, rs);
9620 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
9621 get_last_committed() + 1));
9622 return true;
9623 } else {
9624 ss << "osd." << id << " does not exist";
9625 err = -ENOENT;
9626 goto reply;
9627 }
9628 } else if (prefix == "osd reweightn") {
9629 map<int32_t, uint32_t> weights;
9630 err = parse_reweights(g_ceph_context, cmdmap, osdmap, &weights);
9631 if (err) {
9632 ss << "unable to parse 'weights' value '"
9633 << cmd_vartype_stringify(cmdmap["weights"]) << "'";
9634 goto reply;
9635 }
9636 pending_inc.new_weight.insert(weights.begin(), weights.end());
9637 wait_for_finished_proposal(
9638 op,
9639 new Monitor::C_Command(mon, op, 0, rs, rdata, get_last_committed() + 1));
224ce89b 9640 return true;
7c673cae
FG
9641 } else if (prefix == "osd lost") {
9642 int64_t id;
9643 if (!cmd_getval(g_ceph_context, cmdmap, "id", id)) {
9644 ss << "unable to parse osd id value '"
9645 << cmd_vartype_stringify(cmdmap["id"]) << "'";
9646 err = -EINVAL;
9647 goto reply;
9648 }
9649 string sure;
9650 if (!cmd_getval(g_ceph_context, cmdmap, "sure", sure) || sure != "--yes-i-really-mean-it") {
9651 ss << "are you SURE? this might mean real, permanent data loss. pass "
9652 "--yes-i-really-mean-it if you really do.";
9653 err = -EPERM;
9654 goto reply;
9655 } else if (!osdmap.exists(id)) {
9656 ss << "osd." << id << " does not exist";
9657 err = -ENOENT;
9658 goto reply;
9659 } else if (!osdmap.is_down(id)) {
9660 ss << "osd." << id << " is not down";
9661 err = -EBUSY;
9662 goto reply;
9663 } else {
9664 epoch_t e = osdmap.get_info(id).down_at;
9665 pending_inc.new_lost[id] = e;
9666 ss << "marked osd lost in epoch " << e;
9667 getline(ss, rs);
9668 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
9669 get_last_committed() + 1));
9670 return true;
9671 }
9672
31f18b77
FG
9673 } else if (prefix == "osd destroy" || prefix == "osd purge") {
9674 /* Destroying an OSD means that we don't expect to further make use of
9675 * the OSDs data (which may even become unreadable after this operation),
9676 * and that we are okay with scrubbing all its cephx keys and config-key
9677 * data (which may include lockbox keys, thus rendering the osd's data
9678 * unreadable).
9679 *
9680 * The OSD will not be removed. Instead, we will mark it as destroyed,
9681 * such that a subsequent call to `create` will not reuse the osd id.
9682 * This will play into being able to recreate the OSD, at the same
9683 * crush location, with minimal data movement.
9684 */
9685
9686 // make sure authmon is writeable.
9687 if (!mon->authmon()->is_writeable()) {
9688 dout(10) << __func__ << " waiting for auth mon to be writeable for "
9689 << "osd destroy" << dendl;
9690 mon->authmon()->wait_for_writeable(op, new C_RetryMessage(this, op));
9691 return false;
9692 }
9693
9694 int64_t id;
9695 if (!cmd_getval(g_ceph_context, cmdmap, "id", id)) {
9696 ss << "unable to parse osd id value '"
9697 << cmd_vartype_stringify(cmdmap["id"]) << "";
9698 err = -EINVAL;
9699 goto reply;
9700 }
9701
9702 bool is_destroy = (prefix == "osd destroy");
9703 if (!is_destroy) {
9704 assert("osd purge" == prefix);
9705 }
9706
9707 string sure;
9708 if (!cmd_getval(g_ceph_context, cmdmap, "sure", sure) ||
9709 sure != "--yes-i-really-mean-it") {
9710 ss << "Are you SURE? This will mean real, permanent data loss, as well "
9711 << "as cephx and lockbox keys. Pass --yes-i-really-mean-it if you "
9712 << "really do.";
9713 err = -EPERM;
9714 goto reply;
d2e6a577 9715 } else if (!osdmap.exists(id)) {
31f18b77 9716 ss << "osd." << id << " does not exist";
d2e6a577 9717 err = 0; // idempotent
31f18b77
FG
9718 goto reply;
9719 } else if (osdmap.is_up(id)) {
9720 ss << "osd." << id << " is not `down`.";
9721 err = -EBUSY;
9722 goto reply;
9723 } else if (is_destroy && osdmap.is_destroyed(id)) {
9724 ss << "destroyed osd." << id;
9725 err = 0;
9726 goto reply;
9727 }
9728
9729 bool goto_reply = false;
9730
9731 paxos->plug();
9732 if (is_destroy) {
9733 err = prepare_command_osd_destroy(id, ss);
9734 // we checked above that it should exist.
9735 assert(err != -ENOENT);
9736 } else {
9737 err = prepare_command_osd_purge(id, ss);
9738 if (err == -ENOENT) {
9739 err = 0;
9740 ss << "osd." << id << " does not exist.";
9741 goto_reply = true;
9742 }
9743 }
9744 paxos->unplug();
9745
9746 if (err < 0 || goto_reply) {
9747 goto reply;
9748 }
9749
9750 if (is_destroy) {
9751 ss << "destroyed osd." << id;
9752 } else {
9753 ss << "purged osd." << id;
9754 }
9755
9756 getline(ss, rs);
9757 wait_for_finished_proposal(op,
9758 new Monitor::C_Command(mon, op, 0, rs, get_last_committed() + 1));
9759 force_immediate_propose();
9760 return true;
9761
9762 } else if (prefix == "osd new") {
9763
9764 // make sure authmon is writeable.
9765 if (!mon->authmon()->is_writeable()) {
9766 dout(10) << __func__ << " waiting for auth mon to be writeable for "
224ce89b 9767 << "osd new" << dendl;
31f18b77
FG
9768 mon->authmon()->wait_for_writeable(op, new C_RetryMessage(this, op));
9769 return false;
9770 }
9771
9772 map<string,string> secrets_map;
9773
9774 bufferlist bl = m->get_data();
9775 string secrets_json = bl.to_str();
9776 dout(20) << __func__ << " osd new json = " << secrets_json << dendl;
9777
9778 err = get_json_str_map(secrets_json, ss, &secrets_map);
9779 if (err < 0)
9780 goto reply;
9781
9782 dout(20) << __func__ << " osd new secrets " << secrets_map << dendl;
9783
9784 paxos->plug();
9785 err = prepare_command_osd_new(op, cmdmap, secrets_map, ss, f.get());
9786 paxos->unplug();
9787
9788 if (err < 0) {
9789 goto reply;
9790 }
9791
9792 if (f) {
9793 f->flush(rdata);
9794 } else {
9795 rdata.append(ss);
9796 }
9797
9798 if (err == EEXIST) {
9799 // idempotent operation
9800 err = 0;
9801 goto reply;
9802 }
9803
9804 wait_for_finished_proposal(op,
9805 new Monitor::C_Command(mon, op, 0, rs, rdata,
9806 get_last_committed() + 1));
9807 force_immediate_propose();
9808 return true;
9809
7c673cae 9810 } else if (prefix == "osd create") {
7c673cae
FG
9811
9812 // optional id provided?
31f18b77
FG
9813 int64_t id = -1, cmd_id = -1;
9814 if (cmd_getval(g_ceph_context, cmdmap, "id", cmd_id)) {
9815 if (cmd_id < 0) {
9816 ss << "invalid osd id value '" << cmd_id << "'";
7c673cae
FG
9817 err = -EINVAL;
9818 goto reply;
9819 }
31f18b77 9820 dout(10) << " osd create got id " << cmd_id << dendl;
7c673cae
FG
9821 }
9822
7c673cae
FG
9823 uuid_d uuid;
9824 string uuidstr;
9825 if (cmd_getval(g_ceph_context, cmdmap, "uuid", uuidstr)) {
9826 if (!uuid.parse(uuidstr.c_str())) {
31f18b77
FG
9827 ss << "invalid uuid value '" << uuidstr << "'";
9828 err = -EINVAL;
9829 goto reply;
7c673cae 9830 }
31f18b77
FG
9831 // we only care about the id if we also have the uuid, to
9832 // ensure the operation's idempotency.
9833 id = cmd_id;
7c673cae
FG
9834 }
9835
31f18b77
FG
9836 int32_t new_id = -1;
9837 err = prepare_command_osd_create(id, uuid, &new_id, ss);
9838 if (err < 0) {
9839 if (err == -EAGAIN) {
9840 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
9841 return true;
9842 }
9843 // a check has failed; reply to the user.
9844 goto reply;
9845
9846 } else if (err == EEXIST) {
9847 // this is an idempotent operation; we can go ahead and reply.
9848 if (f) {
9849 f->open_object_section("created_osd");
9850 f->dump_int("osdid", new_id);
9851 f->close_section();
9852 f->flush(rdata);
9853 } else {
9854 ss << new_id;
9855 rdata.append(ss);
7c673cae 9856 }
31f18b77
FG
9857 err = 0;
9858 goto reply;
7c673cae
FG
9859 }
9860
31f18b77
FG
9861 do_osd_create(id, uuid, &new_id);
9862
7c673cae
FG
9863 if (f) {
9864 f->open_object_section("created_osd");
31f18b77 9865 f->dump_int("osdid", new_id);
7c673cae
FG
9866 f->close_section();
9867 f->flush(rdata);
9868 } else {
31f18b77 9869 ss << new_id;
7c673cae
FG
9870 rdata.append(ss);
9871 }
31f18b77
FG
9872 wait_for_finished_proposal(op,
9873 new Monitor::C_Command(mon, op, 0, rs, rdata,
9874 get_last_committed() + 1));
7c673cae
FG
9875 return true;
9876
9877 } else if (prefix == "osd blacklist clear") {
9878 pending_inc.new_blacklist.clear();
9879 std::list<std::pair<entity_addr_t,utime_t > > blacklist;
9880 osdmap.get_blacklist(&blacklist);
9881 for (const auto &entry : blacklist) {
9882 pending_inc.old_blacklist.push_back(entry.first);
9883 }
9884 ss << " removed all blacklist entries";
9885 getline(ss, rs);
9886 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
9887 get_last_committed() + 1));
9888 return true;
9889 } else if (prefix == "osd blacklist") {
9890 string addrstr;
9891 cmd_getval(g_ceph_context, cmdmap, "addr", addrstr);
9892 entity_addr_t addr;
9893 if (!addr.parse(addrstr.c_str(), 0)) {
9894 ss << "unable to parse address " << addrstr;
9895 err = -EINVAL;
9896 goto reply;
9897 }
9898 else {
9899 string blacklistop;
9900 cmd_getval(g_ceph_context, cmdmap, "blacklistop", blacklistop);
9901 if (blacklistop == "add") {
9902 utime_t expires = ceph_clock_now();
9903 double d;
9904 // default one hour
224ce89b
WB
9905 cmd_getval(g_ceph_context, cmdmap, "expire", d,
9906 g_conf->mon_osd_blacklist_default_expire);
7c673cae
FG
9907 expires += d;
9908
9909 pending_inc.new_blacklist[addr] = expires;
224ce89b
WB
9910
9911 {
9912 // cancel any pending un-blacklisting request too
9913 auto it = std::find(pending_inc.old_blacklist.begin(),
9914 pending_inc.old_blacklist.end(), addr);
9915 if (it != pending_inc.old_blacklist.end()) {
9916 pending_inc.old_blacklist.erase(it);
9917 }
9918 }
9919
7c673cae
FG
9920 ss << "blacklisting " << addr << " until " << expires << " (" << d << " sec)";
9921 getline(ss, rs);
9922 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
9923 get_last_committed() + 1));
9924 return true;
9925 } else if (blacklistop == "rm") {
9926 if (osdmap.is_blacklisted(addr) ||
9927 pending_inc.new_blacklist.count(addr)) {
9928 if (osdmap.is_blacklisted(addr))
9929 pending_inc.old_blacklist.push_back(addr);
9930 else
9931 pending_inc.new_blacklist.erase(addr);
9932 ss << "un-blacklisting " << addr;
9933 getline(ss, rs);
9934 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
9935 get_last_committed() + 1));
9936 return true;
9937 }
9938 ss << addr << " isn't blacklisted";
9939 err = 0;
9940 goto reply;
9941 }
9942 }
9943 } else if (prefix == "osd pool mksnap") {
9944 string poolstr;
9945 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
9946 int64_t pool = osdmap.lookup_pg_pool_name(poolstr.c_str());
9947 if (pool < 0) {
9948 ss << "unrecognized pool '" << poolstr << "'";
9949 err = -ENOENT;
9950 goto reply;
9951 }
9952 string snapname;
9953 cmd_getval(g_ceph_context, cmdmap, "snap", snapname);
9954 const pg_pool_t *p = osdmap.get_pg_pool(pool);
9955 if (p->is_unmanaged_snaps_mode()) {
9956 ss << "pool " << poolstr << " is in unmanaged snaps mode";
9957 err = -EINVAL;
9958 goto reply;
9959 } else if (p->snap_exists(snapname.c_str())) {
9960 ss << "pool " << poolstr << " snap " << snapname << " already exists";
9961 err = 0;
9962 goto reply;
9963 } else if (p->is_tier()) {
9964 ss << "pool " << poolstr << " is a cache tier";
9965 err = -EINVAL;
9966 goto reply;
9967 }
9968 pg_pool_t *pp = 0;
9969 if (pending_inc.new_pools.count(pool))
9970 pp = &pending_inc.new_pools[pool];
9971 if (!pp) {
9972 pp = &pending_inc.new_pools[pool];
9973 *pp = *p;
9974 }
9975 if (pp->snap_exists(snapname.c_str())) {
9976 ss << "pool " << poolstr << " snap " << snapname << " already exists";
9977 } else {
9978 pp->add_snap(snapname.c_str(), ceph_clock_now());
9979 pp->set_snap_epoch(pending_inc.epoch);
9980 ss << "created pool " << poolstr << " snap " << snapname;
9981 }
9982 getline(ss, rs);
9983 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
9984 get_last_committed() + 1));
9985 return true;
9986 } else if (prefix == "osd pool rmsnap") {
9987 string poolstr;
9988 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
9989 int64_t pool = osdmap.lookup_pg_pool_name(poolstr.c_str());
9990 if (pool < 0) {
9991 ss << "unrecognized pool '" << poolstr << "'";
9992 err = -ENOENT;
9993 goto reply;
9994 }
9995 string snapname;
9996 cmd_getval(g_ceph_context, cmdmap, "snap", snapname);
9997 const pg_pool_t *p = osdmap.get_pg_pool(pool);
9998 if (p->is_unmanaged_snaps_mode()) {
9999 ss << "pool " << poolstr << " is in unmanaged snaps mode";
10000 err = -EINVAL;
10001 goto reply;
10002 } else if (!p->snap_exists(snapname.c_str())) {
10003 ss << "pool " << poolstr << " snap " << snapname << " does not exist";
10004 err = 0;
10005 goto reply;
10006 }
10007 pg_pool_t *pp = 0;
10008 if (pending_inc.new_pools.count(pool))
10009 pp = &pending_inc.new_pools[pool];
10010 if (!pp) {
10011 pp = &pending_inc.new_pools[pool];
10012 *pp = *p;
10013 }
10014 snapid_t sn = pp->snap_exists(snapname.c_str());
10015 if (sn) {
10016 pp->remove_snap(sn);
10017 pp->set_snap_epoch(pending_inc.epoch);
10018 ss << "removed pool " << poolstr << " snap " << snapname;
10019 } else {
10020 ss << "already removed pool " << poolstr << " snap " << snapname;
10021 }
10022 getline(ss, rs);
10023 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
10024 get_last_committed() + 1));
10025 return true;
10026 } else if (prefix == "osd pool create") {
10027 int64_t pg_num;
10028 int64_t pgp_num;
10029 cmd_getval(g_ceph_context, cmdmap, "pg_num", pg_num, int64_t(0));
10030 cmd_getval(g_ceph_context, cmdmap, "pgp_num", pgp_num, pg_num);
10031
10032 string pool_type_str;
10033 cmd_getval(g_ceph_context, cmdmap, "pool_type", pool_type_str);
10034 if (pool_type_str.empty())
224ce89b 10035 pool_type_str = g_conf->osd_pool_default_type;
7c673cae
FG
10036
10037 string poolstr;
10038 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10039 int64_t pool_id = osdmap.lookup_pg_pool_name(poolstr);
10040 if (pool_id >= 0) {
10041 const pg_pool_t *p = osdmap.get_pg_pool(pool_id);
10042 if (pool_type_str != p->get_type_name()) {
10043 ss << "pool '" << poolstr << "' cannot change to type " << pool_type_str;
10044 err = -EINVAL;
10045 } else {
10046 ss << "pool '" << poolstr << "' already exists";
10047 err = 0;
10048 }
10049 goto reply;
10050 }
10051
10052 int pool_type;
10053 if (pool_type_str == "replicated") {
10054 pool_type = pg_pool_t::TYPE_REPLICATED;
10055 } else if (pool_type_str == "erasure") {
10056 err = check_cluster_features(CEPH_FEATURE_CRUSH_V2 |
10057 CEPH_FEATURE_OSD_ERASURE_CODES,
10058 ss);
10059 if (err == -EAGAIN)
10060 goto wait;
10061 if (err)
10062 goto reply;
10063 pool_type = pg_pool_t::TYPE_ERASURE;
10064 } else {
10065 ss << "unknown pool type '" << pool_type_str << "'";
10066 err = -EINVAL;
10067 goto reply;
10068 }
10069
31f18b77
FG
10070 bool implicit_rule_creation = false;
10071 string rule_name;
10072 cmd_getval(g_ceph_context, cmdmap, "rule", rule_name);
7c673cae
FG
10073 string erasure_code_profile;
10074 cmd_getval(g_ceph_context, cmdmap, "erasure_code_profile", erasure_code_profile);
10075
10076 if (pool_type == pg_pool_t::TYPE_ERASURE) {
10077 if (erasure_code_profile == "")
10078 erasure_code_profile = "default";
10079 //handle the erasure code profile
10080 if (erasure_code_profile == "default") {
10081 if (!osdmap.has_erasure_code_profile(erasure_code_profile)) {
10082 if (pending_inc.has_erasure_code_profile(erasure_code_profile)) {
10083 dout(20) << "erasure code profile " << erasure_code_profile << " already pending" << dendl;
10084 goto wait;
10085 }
10086
10087 map<string,string> profile_map;
10088 err = osdmap.get_erasure_code_profile_default(g_ceph_context,
10089 profile_map,
10090 &ss);
10091 if (err)
10092 goto reply;
10093 dout(20) << "erasure code profile " << erasure_code_profile << " set" << dendl;
10094 pending_inc.set_erasure_code_profile(erasure_code_profile, profile_map);
10095 goto wait;
10096 }
10097 }
31f18b77
FG
10098 if (rule_name == "") {
10099 implicit_rule_creation = true;
7c673cae 10100 if (erasure_code_profile == "default") {
31f18b77 10101 rule_name = "erasure-code";
7c673cae 10102 } else {
31f18b77 10103 dout(1) << "implicitly use rule named after the pool: "
7c673cae 10104 << poolstr << dendl;
31f18b77 10105 rule_name = poolstr;
7c673cae
FG
10106 }
10107 }
10108 } else {
31f18b77
FG
10109 //NOTE:for replicated pool,cmd_map will put rule_name to erasure_code_profile field
10110 rule_name = erasure_code_profile;
7c673cae
FG
10111 }
10112
31f18b77
FG
10113 if (!implicit_rule_creation && rule_name != "") {
10114 int rule;
10115 err = get_crush_rule(rule_name, &rule, &ss);
7c673cae
FG
10116 if (err == -EAGAIN) {
10117 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
10118 return true;
10119 }
10120 if (err)
10121 goto reply;
10122 }
10123
10124 int64_t expected_num_objects;
10125 cmd_getval(g_ceph_context, cmdmap, "expected_num_objects", expected_num_objects, int64_t(0));
10126 if (expected_num_objects < 0) {
10127 ss << "'expected_num_objects' must be non-negative";
10128 err = -EINVAL;
10129 goto reply;
10130 }
10131
10132 int64_t fast_read_param;
10133 cmd_getval(g_ceph_context, cmdmap, "fast_read", fast_read_param, int64_t(-1));
10134 FastReadType fast_read = FAST_READ_DEFAULT;
10135 if (fast_read_param == 0)
10136 fast_read = FAST_READ_OFF;
10137 else if (fast_read_param > 0)
10138 fast_read = FAST_READ_ON;
10139
10140 err = prepare_new_pool(poolstr, 0, // auid=0 for admin created pool
10141 -1, // default crush rule
31f18b77 10142 rule_name,
7c673cae
FG
10143 pg_num, pgp_num,
10144 erasure_code_profile, pool_type,
10145 (uint64_t)expected_num_objects,
10146 fast_read,
10147 &ss);
10148 if (err < 0) {
10149 switch(err) {
10150 case -EEXIST:
10151 ss << "pool '" << poolstr << "' already exists";
10152 break;
10153 case -EAGAIN:
10154 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
10155 return true;
10156 case -ERANGE:
10157 goto reply;
10158 default:
10159 goto reply;
10160 break;
10161 }
10162 } else {
10163 ss << "pool '" << poolstr << "' created";
10164 }
10165 getline(ss, rs);
10166 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
10167 get_last_committed() + 1));
10168 return true;
10169
10170 } else if (prefix == "osd pool delete" ||
10171 prefix == "osd pool rm") {
10172 // osd pool delete/rm <poolname> <poolname again> --yes-i-really-really-mean-it
10173 string poolstr, poolstr2, sure;
10174 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10175 cmd_getval(g_ceph_context, cmdmap, "pool2", poolstr2);
10176 cmd_getval(g_ceph_context, cmdmap, "sure", sure);
10177 int64_t pool = osdmap.lookup_pg_pool_name(poolstr.c_str());
10178 if (pool < 0) {
10179 ss << "pool '" << poolstr << "' does not exist";
10180 err = 0;
10181 goto reply;
10182 }
10183
10184 bool force_no_fake = sure == "--yes-i-really-really-mean-it-not-faking";
10185 if (poolstr2 != poolstr ||
10186 (sure != "--yes-i-really-really-mean-it" && !force_no_fake)) {
10187 ss << "WARNING: this will *PERMANENTLY DESTROY* all data stored in pool " << poolstr
10188 << ". If you are *ABSOLUTELY CERTAIN* that is what you want, pass the pool name *twice*, "
10189 << "followed by --yes-i-really-really-mean-it.";
10190 err = -EPERM;
10191 goto reply;
10192 }
10193 err = _prepare_remove_pool(pool, &ss, force_no_fake);
10194 if (err == -EAGAIN) {
10195 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
10196 return true;
10197 }
10198 if (err < 0)
10199 goto reply;
10200 goto update;
10201 } else if (prefix == "osd pool rename") {
10202 string srcpoolstr, destpoolstr;
10203 cmd_getval(g_ceph_context, cmdmap, "srcpool", srcpoolstr);
10204 cmd_getval(g_ceph_context, cmdmap, "destpool", destpoolstr);
10205 int64_t pool_src = osdmap.lookup_pg_pool_name(srcpoolstr.c_str());
10206 int64_t pool_dst = osdmap.lookup_pg_pool_name(destpoolstr.c_str());
10207
10208 if (pool_src < 0) {
10209 if (pool_dst >= 0) {
10210 // src pool doesn't exist, dst pool does exist: to ensure idempotency
10211 // of operations, assume this rename succeeded, as it is not changing
10212 // the current state. Make sure we output something understandable
10213 // for whoever is issuing the command, if they are paying attention,
10214 // in case it was not intentional; or to avoid a "wtf?" and a bug
10215 // report in case it was intentional, while expecting a failure.
10216 ss << "pool '" << srcpoolstr << "' does not exist; pool '"
10217 << destpoolstr << "' does -- assuming successful rename";
10218 err = 0;
10219 } else {
10220 ss << "unrecognized pool '" << srcpoolstr << "'";
10221 err = -ENOENT;
10222 }
10223 goto reply;
10224 } else if (pool_dst >= 0) {
10225 // source pool exists and so does the destination pool
10226 ss << "pool '" << destpoolstr << "' already exists";
10227 err = -EEXIST;
10228 goto reply;
10229 }
10230
10231 int ret = _prepare_rename_pool(pool_src, destpoolstr);
10232 if (ret == 0) {
10233 ss << "pool '" << srcpoolstr << "' renamed to '" << destpoolstr << "'";
10234 } else {
10235 ss << "failed to rename pool '" << srcpoolstr << "' to '" << destpoolstr << "': "
10236 << cpp_strerror(ret);
10237 }
10238 getline(ss, rs);
10239 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, ret, rs,
10240 get_last_committed() + 1));
10241 return true;
10242
10243 } else if (prefix == "osd pool set") {
10244 err = prepare_command_pool_set(cmdmap, ss);
10245 if (err == -EAGAIN)
10246 goto wait;
10247 if (err < 0)
10248 goto reply;
10249
10250 getline(ss, rs);
10251 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
10252 get_last_committed() + 1));
10253 return true;
10254 } else if (prefix == "osd tier add") {
10255 err = check_cluster_features(CEPH_FEATURE_OSD_CACHEPOOL, ss);
10256 if (err == -EAGAIN)
10257 goto wait;
10258 if (err)
10259 goto reply;
10260 string poolstr;
10261 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10262 int64_t pool_id = osdmap.lookup_pg_pool_name(poolstr);
10263 if (pool_id < 0) {
10264 ss << "unrecognized pool '" << poolstr << "'";
10265 err = -ENOENT;
10266 goto reply;
10267 }
10268 string tierpoolstr;
10269 cmd_getval(g_ceph_context, cmdmap, "tierpool", tierpoolstr);
10270 int64_t tierpool_id = osdmap.lookup_pg_pool_name(tierpoolstr);
10271 if (tierpool_id < 0) {
10272 ss << "unrecognized pool '" << tierpoolstr << "'";
10273 err = -ENOENT;
10274 goto reply;
10275 }
10276 const pg_pool_t *p = osdmap.get_pg_pool(pool_id);
10277 assert(p);
10278 const pg_pool_t *tp = osdmap.get_pg_pool(tierpool_id);
10279 assert(tp);
10280
10281 if (!_check_become_tier(tierpool_id, tp, pool_id, p, &err, &ss)) {
10282 goto reply;
10283 }
10284
10285 // make sure new tier is empty
10286 string force_nonempty;
10287 cmd_getval(g_ceph_context, cmdmap, "force_nonempty", force_nonempty);
31f18b77
FG
10288 const pool_stat_t *pstats = mon->pgservice->get_pool_stat(tierpool_id);
10289 if (pstats && pstats->stats.sum.num_objects != 0 &&
7c673cae
FG
10290 force_nonempty != "--force-nonempty") {
10291 ss << "tier pool '" << tierpoolstr << "' is not empty; --force-nonempty to force";
10292 err = -ENOTEMPTY;
10293 goto reply;
10294 }
10295 if (tp->ec_pool()) {
10296 ss << "tier pool '" << tierpoolstr
10297 << "' is an ec pool, which cannot be a tier";
10298 err = -ENOTSUP;
10299 goto reply;
10300 }
10301 if ((!tp->removed_snaps.empty() || !tp->snaps.empty()) &&
10302 ((force_nonempty != "--force-nonempty") ||
10303 (!g_conf->mon_debug_unsafe_allow_tier_with_nonempty_snaps))) {
10304 ss << "tier pool '" << tierpoolstr << "' has snapshot state; it cannot be added as a tier without breaking the pool";
10305 err = -ENOTEMPTY;
10306 goto reply;
10307 }
10308 // go
10309 pg_pool_t *np = pending_inc.get_new_pool(pool_id, p);
10310 pg_pool_t *ntp = pending_inc.get_new_pool(tierpool_id, tp);
10311 if (np->tiers.count(tierpool_id) || ntp->is_tier()) {
10312 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
10313 return true;
10314 }
10315 np->tiers.insert(tierpool_id);
10316 np->set_snap_epoch(pending_inc.epoch); // tier will update to our snap info
10317 ntp->tier_of = pool_id;
10318 ss << "pool '" << tierpoolstr << "' is now (or already was) a tier of '" << poolstr << "'";
10319 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, ss.str(),
10320 get_last_committed() + 1));
10321 return true;
10322 } else if (prefix == "osd tier remove" ||
10323 prefix == "osd tier rm") {
10324 string poolstr;
10325 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10326 int64_t pool_id = osdmap.lookup_pg_pool_name(poolstr);
10327 if (pool_id < 0) {
10328 ss << "unrecognized pool '" << poolstr << "'";
10329 err = -ENOENT;
10330 goto reply;
10331 }
10332 string tierpoolstr;
10333 cmd_getval(g_ceph_context, cmdmap, "tierpool", tierpoolstr);
10334 int64_t tierpool_id = osdmap.lookup_pg_pool_name(tierpoolstr);
10335 if (tierpool_id < 0) {
10336 ss << "unrecognized pool '" << tierpoolstr << "'";
10337 err = -ENOENT;
10338 goto reply;
10339 }
10340 const pg_pool_t *p = osdmap.get_pg_pool(pool_id);
10341 assert(p);
10342 const pg_pool_t *tp = osdmap.get_pg_pool(tierpool_id);
10343 assert(tp);
10344
10345 if (!_check_remove_tier(pool_id, p, tp, &err, &ss)) {
10346 goto reply;
10347 }
10348
10349 if (p->tiers.count(tierpool_id) == 0) {
10350 ss << "pool '" << tierpoolstr << "' is now (or already was) not a tier of '" << poolstr << "'";
10351 err = 0;
10352 goto reply;
10353 }
10354 if (tp->tier_of != pool_id) {
10355 ss << "tier pool '" << tierpoolstr << "' is a tier of '"
10356 << osdmap.get_pool_name(tp->tier_of) << "': "
10357 // be scary about it; this is an inconsistency and bells must go off
10358 << "THIS SHOULD NOT HAVE HAPPENED AT ALL";
10359 err = -EINVAL;
10360 goto reply;
10361 }
10362 if (p->read_tier == tierpool_id) {
10363 ss << "tier pool '" << tierpoolstr << "' is the overlay for '" << poolstr << "'; please remove-overlay first";
10364 err = -EBUSY;
10365 goto reply;
10366 }
10367 // go
10368 pg_pool_t *np = pending_inc.get_new_pool(pool_id, p);
10369 pg_pool_t *ntp = pending_inc.get_new_pool(tierpool_id, tp);
10370 if (np->tiers.count(tierpool_id) == 0 ||
10371 ntp->tier_of != pool_id ||
10372 np->read_tier == tierpool_id) {
10373 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
10374 return true;
10375 }
10376 np->tiers.erase(tierpool_id);
10377 ntp->clear_tier();
10378 ss << "pool '" << tierpoolstr << "' is now (or already was) not a tier of '" << poolstr << "'";
10379 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, ss.str(),
10380 get_last_committed() + 1));
10381 return true;
10382 } else if (prefix == "osd tier set-overlay") {
10383 err = check_cluster_features(CEPH_FEATURE_OSD_CACHEPOOL, ss);
10384 if (err == -EAGAIN)
10385 goto wait;
10386 if (err)
10387 goto reply;
10388 string poolstr;
10389 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10390 int64_t pool_id = osdmap.lookup_pg_pool_name(poolstr);
10391 if (pool_id < 0) {
10392 ss << "unrecognized pool '" << poolstr << "'";
10393 err = -ENOENT;
10394 goto reply;
10395 }
10396 string overlaypoolstr;
10397 cmd_getval(g_ceph_context, cmdmap, "overlaypool", overlaypoolstr);
10398 int64_t overlaypool_id = osdmap.lookup_pg_pool_name(overlaypoolstr);
10399 if (overlaypool_id < 0) {
10400 ss << "unrecognized pool '" << overlaypoolstr << "'";
10401 err = -ENOENT;
10402 goto reply;
10403 }
10404 const pg_pool_t *p = osdmap.get_pg_pool(pool_id);
10405 assert(p);
10406 const pg_pool_t *overlay_p = osdmap.get_pg_pool(overlaypool_id);
10407 assert(overlay_p);
10408 if (p->tiers.count(overlaypool_id) == 0) {
10409 ss << "tier pool '" << overlaypoolstr << "' is not a tier of '" << poolstr << "'";
10410 err = -EINVAL;
10411 goto reply;
10412 }
10413 if (p->read_tier == overlaypool_id) {
10414 err = 0;
10415 ss << "overlay for '" << poolstr << "' is now (or already was) '" << overlaypoolstr << "'";
10416 goto reply;
10417 }
10418 if (p->has_read_tier()) {
10419 ss << "pool '" << poolstr << "' has overlay '"
10420 << osdmap.get_pool_name(p->read_tier)
10421 << "'; please remove-overlay first";
10422 err = -EINVAL;
10423 goto reply;
10424 }
10425
10426 // go
10427 pg_pool_t *np = pending_inc.get_new_pool(pool_id, p);
10428 np->read_tier = overlaypool_id;
10429 np->write_tier = overlaypool_id;
10430 np->set_last_force_op_resend(pending_inc.epoch);
10431 pg_pool_t *noverlay_p = pending_inc.get_new_pool(overlaypool_id, overlay_p);
10432 noverlay_p->set_last_force_op_resend(pending_inc.epoch);
10433 ss << "overlay for '" << poolstr << "' is now (or already was) '" << overlaypoolstr << "'";
10434 if (overlay_p->cache_mode == pg_pool_t::CACHEMODE_NONE)
10435 ss <<" (WARNING: overlay pool cache_mode is still NONE)";
10436 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, ss.str(),
10437 get_last_committed() + 1));
10438 return true;
10439 } else if (prefix == "osd tier remove-overlay" ||
10440 prefix == "osd tier rm-overlay") {
10441 string poolstr;
10442 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10443 int64_t pool_id = osdmap.lookup_pg_pool_name(poolstr);
10444 if (pool_id < 0) {
10445 ss << "unrecognized pool '" << poolstr << "'";
10446 err = -ENOENT;
10447 goto reply;
10448 }
10449 const pg_pool_t *p = osdmap.get_pg_pool(pool_id);
10450 assert(p);
10451 if (!p->has_read_tier()) {
10452 err = 0;
10453 ss << "there is now (or already was) no overlay for '" << poolstr << "'";
10454 goto reply;
10455 }
10456
10457 if (!_check_remove_tier(pool_id, p, NULL, &err, &ss)) {
10458 goto reply;
10459 }
10460
10461 // go
10462 pg_pool_t *np = pending_inc.get_new_pool(pool_id, p);
10463 if (np->has_read_tier()) {
10464 const pg_pool_t *op = osdmap.get_pg_pool(np->read_tier);
10465 pg_pool_t *nop = pending_inc.get_new_pool(np->read_tier,op);
10466 nop->set_last_force_op_resend(pending_inc.epoch);
10467 }
10468 if (np->has_write_tier()) {
10469 const pg_pool_t *op = osdmap.get_pg_pool(np->write_tier);
10470 pg_pool_t *nop = pending_inc.get_new_pool(np->write_tier, op);
10471 nop->set_last_force_op_resend(pending_inc.epoch);
10472 }
10473 np->clear_read_tier();
10474 np->clear_write_tier();
10475 np->set_last_force_op_resend(pending_inc.epoch);
10476 ss << "there is now (or already was) no overlay for '" << poolstr << "'";
10477 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, ss.str(),
10478 get_last_committed() + 1));
10479 return true;
10480 } else if (prefix == "osd tier cache-mode") {
10481 err = check_cluster_features(CEPH_FEATURE_OSD_CACHEPOOL, ss);
10482 if (err == -EAGAIN)
10483 goto wait;
10484 if (err)
10485 goto reply;
10486 string poolstr;
10487 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10488 int64_t pool_id = osdmap.lookup_pg_pool_name(poolstr);
10489 if (pool_id < 0) {
10490 ss << "unrecognized pool '" << poolstr << "'";
10491 err = -ENOENT;
10492 goto reply;
10493 }
10494 const pg_pool_t *p = osdmap.get_pg_pool(pool_id);
10495 assert(p);
10496 if (!p->is_tier()) {
10497 ss << "pool '" << poolstr << "' is not a tier";
10498 err = -EINVAL;
10499 goto reply;
10500 }
10501 string modestr;
10502 cmd_getval(g_ceph_context, cmdmap, "mode", modestr);
10503 pg_pool_t::cache_mode_t mode = pg_pool_t::get_cache_mode_from_str(modestr);
10504 if (mode < 0) {
10505 ss << "'" << modestr << "' is not a valid cache mode";
10506 err = -EINVAL;
10507 goto reply;
10508 }
10509
10510 string sure;
10511 cmd_getval(g_ceph_context, cmdmap, "sure", sure);
10512 if ((mode != pg_pool_t::CACHEMODE_WRITEBACK &&
10513 mode != pg_pool_t::CACHEMODE_NONE &&
10514 mode != pg_pool_t::CACHEMODE_PROXY &&
10515 mode != pg_pool_t::CACHEMODE_READPROXY) &&
10516 sure != "--yes-i-really-mean-it") {
10517 ss << "'" << modestr << "' is not a well-supported cache mode and may "
10518 << "corrupt your data. pass --yes-i-really-mean-it to force.";
10519 err = -EPERM;
10520 goto reply;
10521 }
10522
10523 // pool already has this cache-mode set and there are no pending changes
10524 if (p->cache_mode == mode &&
10525 (pending_inc.new_pools.count(pool_id) == 0 ||
10526 pending_inc.new_pools[pool_id].cache_mode == p->cache_mode)) {
10527 ss << "set cache-mode for pool '" << poolstr << "'"
10528 << " to " << pg_pool_t::get_cache_mode_name(mode);
10529 err = 0;
10530 goto reply;
10531 }
10532
10533 /* Mode description:
10534 *
10535 * none: No cache-mode defined
10536 * forward: Forward all reads and writes to base pool
10537 * writeback: Cache writes, promote reads from base pool
10538 * readonly: Forward writes to base pool
10539 * readforward: Writes are in writeback mode, Reads are in forward mode
10540 * proxy: Proxy all reads and writes to base pool
10541 * readproxy: Writes are in writeback mode, Reads are in proxy mode
10542 *
10543 * Hence, these are the allowed transitions:
10544 *
10545 * none -> any
10546 * forward -> proxy || readforward || readproxy || writeback || any IF num_objects_dirty == 0
10547 * proxy -> forward || readforward || readproxy || writeback || any IF num_objects_dirty == 0
10548 * readforward -> forward || proxy || readproxy || writeback || any IF num_objects_dirty == 0
10549 * readproxy -> forward || proxy || readforward || writeback || any IF num_objects_dirty == 0
10550 * writeback -> readforward || readproxy || forward || proxy
10551 * readonly -> any
10552 */
10553
10554 // We check if the transition is valid against the current pool mode, as
10555 // it is the only committed state thus far. We will blantly squash
10556 // whatever mode is on the pending state.
10557
10558 if (p->cache_mode == pg_pool_t::CACHEMODE_WRITEBACK &&
10559 (mode != pg_pool_t::CACHEMODE_FORWARD &&
10560 mode != pg_pool_t::CACHEMODE_PROXY &&
10561 mode != pg_pool_t::CACHEMODE_READFORWARD &&
10562 mode != pg_pool_t::CACHEMODE_READPROXY)) {
10563 ss << "unable to set cache-mode '" << pg_pool_t::get_cache_mode_name(mode)
10564 << "' on a '" << pg_pool_t::get_cache_mode_name(p->cache_mode)
10565 << "' pool; only '"
10566 << pg_pool_t::get_cache_mode_name(pg_pool_t::CACHEMODE_FORWARD)
10567 << "','"
10568 << pg_pool_t::get_cache_mode_name(pg_pool_t::CACHEMODE_PROXY)
10569 << "','"
10570 << pg_pool_t::get_cache_mode_name(pg_pool_t::CACHEMODE_READFORWARD)
10571 << "','"
10572 << pg_pool_t::get_cache_mode_name(pg_pool_t::CACHEMODE_READPROXY)
10573 << "' allowed.";
10574 err = -EINVAL;
10575 goto reply;
10576 }
10577 if ((p->cache_mode == pg_pool_t::CACHEMODE_READFORWARD &&
10578 (mode != pg_pool_t::CACHEMODE_WRITEBACK &&
10579 mode != pg_pool_t::CACHEMODE_FORWARD &&
10580 mode != pg_pool_t::CACHEMODE_PROXY &&
10581 mode != pg_pool_t::CACHEMODE_READPROXY)) ||
10582
10583 (p->cache_mode == pg_pool_t::CACHEMODE_READPROXY &&
10584 (mode != pg_pool_t::CACHEMODE_WRITEBACK &&
10585 mode != pg_pool_t::CACHEMODE_FORWARD &&
10586 mode != pg_pool_t::CACHEMODE_READFORWARD &&
10587 mode != pg_pool_t::CACHEMODE_PROXY)) ||
10588
10589 (p->cache_mode == pg_pool_t::CACHEMODE_PROXY &&
10590 (mode != pg_pool_t::CACHEMODE_WRITEBACK &&
10591 mode != pg_pool_t::CACHEMODE_FORWARD &&
10592 mode != pg_pool_t::CACHEMODE_READFORWARD &&
10593 mode != pg_pool_t::CACHEMODE_READPROXY)) ||
10594
10595 (p->cache_mode == pg_pool_t::CACHEMODE_FORWARD &&
10596 (mode != pg_pool_t::CACHEMODE_WRITEBACK &&
10597 mode != pg_pool_t::CACHEMODE_READFORWARD &&
10598 mode != pg_pool_t::CACHEMODE_PROXY &&
10599 mode != pg_pool_t::CACHEMODE_READPROXY))) {
10600
31f18b77
FG
10601 const pool_stat_t* pstats =
10602 mon->pgservice->get_pool_stat(pool_id);
7c673cae 10603
31f18b77 10604 if (pstats && pstats->stats.sum.num_objects_dirty > 0) {
7c673cae
FG
10605 ss << "unable to set cache-mode '"
10606 << pg_pool_t::get_cache_mode_name(mode) << "' on pool '" << poolstr
10607 << "': dirty objects found";
10608 err = -EBUSY;
10609 goto reply;
10610 }
10611 }
10612 // go
10613 pg_pool_t *np = pending_inc.get_new_pool(pool_id, p);
10614 np->cache_mode = mode;
10615 // set this both when moving to and from cache_mode NONE. this is to
10616 // capture legacy pools that were set up before this flag existed.
10617 np->flags |= pg_pool_t::FLAG_INCOMPLETE_CLONES;
10618 ss << "set cache-mode for pool '" << poolstr
10619 << "' to " << pg_pool_t::get_cache_mode_name(mode);
10620 if (mode == pg_pool_t::CACHEMODE_NONE) {
10621 const pg_pool_t *base_pool = osdmap.get_pg_pool(np->tier_of);
10622 assert(base_pool);
10623 if (base_pool->read_tier == pool_id ||
10624 base_pool->write_tier == pool_id)
10625 ss <<" (WARNING: pool is still configured as read or write tier)";
10626 }
10627 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, ss.str(),
10628 get_last_committed() + 1));
10629 return true;
10630 } else if (prefix == "osd tier add-cache") {
10631 err = check_cluster_features(CEPH_FEATURE_OSD_CACHEPOOL, ss);
10632 if (err == -EAGAIN)
10633 goto wait;
10634 if (err)
10635 goto reply;
10636 string poolstr;
10637 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10638 int64_t pool_id = osdmap.lookup_pg_pool_name(poolstr);
10639 if (pool_id < 0) {
10640 ss << "unrecognized pool '" << poolstr << "'";
10641 err = -ENOENT;
10642 goto reply;
10643 }
10644 string tierpoolstr;
10645 cmd_getval(g_ceph_context, cmdmap, "tierpool", tierpoolstr);
10646 int64_t tierpool_id = osdmap.lookup_pg_pool_name(tierpoolstr);
10647 if (tierpool_id < 0) {
10648 ss << "unrecognized pool '" << tierpoolstr << "'";
10649 err = -ENOENT;
10650 goto reply;
10651 }
10652 const pg_pool_t *p = osdmap.get_pg_pool(pool_id);
10653 assert(p);
10654 const pg_pool_t *tp = osdmap.get_pg_pool(tierpool_id);
10655 assert(tp);
10656
10657 if (!_check_become_tier(tierpool_id, tp, pool_id, p, &err, &ss)) {
10658 goto reply;
10659 }
10660
10661 int64_t size = 0;
10662 if (!cmd_getval(g_ceph_context, cmdmap, "size", size)) {
10663 ss << "unable to parse 'size' value '"
10664 << cmd_vartype_stringify(cmdmap["size"]) << "'";
10665 err = -EINVAL;
10666 goto reply;
10667 }
10668 // make sure new tier is empty
31f18b77
FG
10669 const pool_stat_t *pstats =
10670 mon->pgservice->get_pool_stat(tierpool_id);
10671 if (pstats && pstats->stats.sum.num_objects != 0) {
7c673cae
FG
10672 ss << "tier pool '" << tierpoolstr << "' is not empty";
10673 err = -ENOTEMPTY;
10674 goto reply;
10675 }
10676 string modestr = g_conf->osd_tier_default_cache_mode;
10677 pg_pool_t::cache_mode_t mode = pg_pool_t::get_cache_mode_from_str(modestr);
10678 if (mode < 0) {
10679 ss << "osd tier cache default mode '" << modestr << "' is not a valid cache mode";
10680 err = -EINVAL;
10681 goto reply;
10682 }
10683 HitSet::Params hsp;
10684 if (g_conf->osd_tier_default_cache_hit_set_type == "bloom") {
10685 BloomHitSet::Params *bsp = new BloomHitSet::Params;
10686 bsp->set_fpp(g_conf->osd_pool_default_hit_set_bloom_fpp);
10687 hsp = HitSet::Params(bsp);
10688 } else if (g_conf->osd_tier_default_cache_hit_set_type == "explicit_hash") {
10689 hsp = HitSet::Params(new ExplicitHashHitSet::Params);
10690 }
10691 else if (g_conf->osd_tier_default_cache_hit_set_type == "explicit_object") {
10692 hsp = HitSet::Params(new ExplicitObjectHitSet::Params);
10693 } else {
10694 ss << "osd tier cache default hit set type '" <<
10695 g_conf->osd_tier_default_cache_hit_set_type << "' is not a known type";
10696 err = -EINVAL;
10697 goto reply;
10698 }
10699 // go
10700 pg_pool_t *np = pending_inc.get_new_pool(pool_id, p);
10701 pg_pool_t *ntp = pending_inc.get_new_pool(tierpool_id, tp);
10702 if (np->tiers.count(tierpool_id) || ntp->is_tier()) {
10703 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
10704 return true;
10705 }
10706 np->tiers.insert(tierpool_id);
10707 np->read_tier = np->write_tier = tierpool_id;
10708 np->set_snap_epoch(pending_inc.epoch); // tier will update to our snap info
10709 np->set_last_force_op_resend(pending_inc.epoch);
10710 ntp->set_last_force_op_resend(pending_inc.epoch);
10711 ntp->tier_of = pool_id;
10712 ntp->cache_mode = mode;
10713 ntp->hit_set_count = g_conf->osd_tier_default_cache_hit_set_count;
10714 ntp->hit_set_period = g_conf->osd_tier_default_cache_hit_set_period;
10715 ntp->min_read_recency_for_promote = g_conf->osd_tier_default_cache_min_read_recency_for_promote;
10716 ntp->min_write_recency_for_promote = g_conf->osd_tier_default_cache_min_write_recency_for_promote;
10717 ntp->hit_set_grade_decay_rate = g_conf->osd_tier_default_cache_hit_set_grade_decay_rate;
10718 ntp->hit_set_search_last_n = g_conf->osd_tier_default_cache_hit_set_search_last_n;
10719 ntp->hit_set_params = hsp;
10720 ntp->target_max_bytes = size;
10721 ss << "pool '" << tierpoolstr << "' is now (or already was) a cache tier of '" << poolstr << "'";
10722 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, ss.str(),
10723 get_last_committed() + 1));
10724 return true;
10725 } else if (prefix == "osd pool set-quota") {
10726 string poolstr;
10727 cmd_getval(g_ceph_context, cmdmap, "pool", poolstr);
10728 int64_t pool_id = osdmap.lookup_pg_pool_name(poolstr);
10729 if (pool_id < 0) {
10730 ss << "unrecognized pool '" << poolstr << "'";
10731 err = -ENOENT;
10732 goto reply;
10733 }
10734
10735 string field;
10736 cmd_getval(g_ceph_context, cmdmap, "field", field);
10737 if (field != "max_objects" && field != "max_bytes") {
10738 ss << "unrecognized field '" << field << "'; should be 'max_bytes' or 'max_objects'";
10739 err = -EINVAL;
10740 goto reply;
10741 }
10742
10743 // val could contain unit designations, so we treat as a string
10744 string val;
10745 cmd_getval(g_ceph_context, cmdmap, "val", val);
10746 stringstream tss;
10747 int64_t value = unit_to_bytesize(val, &tss);
10748 if (value < 0) {
10749 ss << "error parsing value '" << value << "': " << tss.str();
10750 err = value;
10751 goto reply;
10752 }
10753
10754 pg_pool_t *pi = pending_inc.get_new_pool(pool_id, osdmap.get_pg_pool(pool_id));
10755 if (field == "max_objects") {
10756 pi->quota_max_objects = value;
10757 } else if (field == "max_bytes") {
10758 pi->quota_max_bytes = value;
10759 } else {
10760 assert(0 == "unrecognized option");
10761 }
10762 ss << "set-quota " << field << " = " << value << " for pool " << poolstr;
10763 rs = ss.str();
10764 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
10765 get_last_committed() + 1));
10766 return true;
c07f9fc5
FG
10767 } else if (prefix == "osd pool application enable" ||
10768 prefix == "osd pool application disable" ||
10769 prefix == "osd pool application set" ||
10770 prefix == "osd pool application rm") {
10771 err = prepare_command_pool_application(prefix, cmdmap, ss);
10772 if (err == -EAGAIN)
10773 goto wait;
10774 if (err < 0)
10775 goto reply;
7c673cae 10776
c07f9fc5
FG
10777 getline(ss, rs);
10778 wait_for_finished_proposal(
10779 op, new Monitor::C_Command(mon, op, 0, rs, get_last_committed() + 1));
10780 return true;
7c673cae
FG
10781 } else if (prefix == "osd reweight-by-pg" ||
10782 prefix == "osd reweight-by-utilization" ||
10783 prefix == "osd test-reweight-by-pg" ||
10784 prefix == "osd test-reweight-by-utilization") {
10785 bool by_pg =
10786 prefix == "osd reweight-by-pg" || prefix == "osd test-reweight-by-pg";
10787 bool dry_run =
10788 prefix == "osd test-reweight-by-pg" ||
10789 prefix == "osd test-reweight-by-utilization";
10790 int64_t oload;
10791 cmd_getval(g_ceph_context, cmdmap, "oload", oload, int64_t(120));
10792 set<int64_t> pools;
10793 vector<string> poolnamevec;
10794 cmd_getval(g_ceph_context, cmdmap, "pools", poolnamevec);
10795 for (unsigned j = 0; j < poolnamevec.size(); j++) {
10796 int64_t pool = osdmap.lookup_pg_pool_name(poolnamevec[j]);
10797 if (pool < 0) {
10798 ss << "pool '" << poolnamevec[j] << "' does not exist";
10799 err = -ENOENT;
10800 goto reply;
10801 }
10802 pools.insert(pool);
10803 }
10804 double max_change = g_conf->mon_reweight_max_change;
10805 cmd_getval(g_ceph_context, cmdmap, "max_change", max_change);
10806 if (max_change <= 0.0) {
10807 ss << "max_change " << max_change << " must be positive";
10808 err = -EINVAL;
10809 goto reply;
10810 }
10811 int64_t max_osds = g_conf->mon_reweight_max_osds;
10812 cmd_getval(g_ceph_context, cmdmap, "max_osds", max_osds);
10813 if (max_osds <= 0) {
10814 ss << "max_osds " << max_osds << " must be positive";
10815 err = -EINVAL;
10816 goto reply;
10817 }
10818 string no_increasing;
10819 cmd_getval(g_ceph_context, cmdmap, "no_increasing", no_increasing);
10820 string out_str;
10821 mempool::osdmap::map<int32_t, uint32_t> new_weights;
31f18b77
FG
10822 err = mon->pgservice->reweight_by_utilization(osdmap,
10823 oload,
10824 max_change,
10825 max_osds,
10826 by_pg,
10827 pools.empty() ? NULL : &pools,
10828 no_increasing == "--no-increasing",
10829 &new_weights,
10830 &ss, &out_str, f.get());
7c673cae
FG
10831 if (err >= 0) {
10832 dout(10) << "reweight::by_utilization: finished with " << out_str << dendl;
10833 }
10834 if (f)
10835 f->flush(rdata);
10836 else
10837 rdata.append(out_str);
10838 if (err < 0) {
10839 ss << "FAILED reweight-by-pg";
10840 } else if (err == 0 || dry_run) {
10841 ss << "no change";
10842 } else {
10843 ss << "SUCCESSFUL reweight-by-pg";
10844 pending_inc.new_weight = std::move(new_weights);
10845 wait_for_finished_proposal(
10846 op,
10847 new Monitor::C_Command(mon, op, 0, rs, rdata, get_last_committed() + 1));
10848 return true;
10849 }
c07f9fc5
FG
10850 } else if (prefix == "osd force-create-pg") {
10851 pg_t pgid;
10852 string pgidstr;
10853 cmd_getval(g_ceph_context, cmdmap, "pgid", pgidstr);
10854 if (!pgid.parse(pgidstr.c_str())) {
10855 ss << "invalid pgid '" << pgidstr << "'";
10856 err = -EINVAL;
10857 goto reply;
10858 }
10859 bool creating_now;
10860 {
10861 std::lock_guard<std::mutex> l(creating_pgs_lock);
10862 auto emplaced = creating_pgs.pgs.emplace(pgid,
10863 make_pair(osdmap.get_epoch(),
10864 ceph_clock_now()));
10865 creating_now = emplaced.second;
10866 }
10867 if (creating_now) {
10868 ss << "pg " << pgidstr << " now creating, ok";
10869 err = 0;
10870 goto update;
10871 } else {
10872 ss << "pg " << pgid << " already creating";
10873 err = 0;
10874 goto reply;
10875 }
7c673cae
FG
10876 } else {
10877 err = -EINVAL;
10878 }
10879
10880 reply:
10881 getline(ss, rs);
10882 if (err < 0 && rs.length() == 0)
10883 rs = cpp_strerror(err);
10884 mon->reply_command(op, err, rs, rdata, get_last_committed());
10885 return ret;
10886
10887 update:
10888 getline(ss, rs);
10889 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs,
10890 get_last_committed() + 1));
10891 return true;
10892
10893 wait:
10894 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
10895 return true;
10896}
10897
10898bool OSDMonitor::preprocess_pool_op(MonOpRequestRef op)
10899{
10900 op->mark_osdmon_event(__func__);
10901 MPoolOp *m = static_cast<MPoolOp*>(op->get_req());
10902
10903 if (m->fsid != mon->monmap->fsid) {
10904 dout(0) << __func__ << " drop message on fsid " << m->fsid
10905 << " != " << mon->monmap->fsid << " for " << *m << dendl;
10906 _pool_op_reply(op, -EINVAL, osdmap.get_epoch());
10907 return true;
10908 }
10909
10910 if (m->op == POOL_OP_CREATE)
10911 return preprocess_pool_op_create(op);
10912
10913 if (!osdmap.get_pg_pool(m->pool)) {
10914 dout(10) << "attempt to operate on non-existent pool id " << m->pool << dendl;
10915 _pool_op_reply(op, 0, osdmap.get_epoch());
10916 return true;
10917 }
10918
10919 // check if the snap and snapname exist
10920 bool snap_exists = false;
10921 const pg_pool_t *p = osdmap.get_pg_pool(m->pool);
10922 if (p->snap_exists(m->name.c_str()))
10923 snap_exists = true;
10924
10925 switch (m->op) {
10926 case POOL_OP_CREATE_SNAP:
10927 if (p->is_unmanaged_snaps_mode() || p->is_tier()) {
10928 _pool_op_reply(op, -EINVAL, osdmap.get_epoch());
10929 return true;
10930 }
10931 if (snap_exists) {
10932 _pool_op_reply(op, 0, osdmap.get_epoch());
10933 return true;
10934 }
10935 return false;
10936 case POOL_OP_CREATE_UNMANAGED_SNAP:
10937 if (p->is_pool_snaps_mode()) {
10938 _pool_op_reply(op, -EINVAL, osdmap.get_epoch());
10939 return true;
10940 }
10941 return false;
10942 case POOL_OP_DELETE_SNAP:
10943 if (p->is_unmanaged_snaps_mode()) {
10944 _pool_op_reply(op, -EINVAL, osdmap.get_epoch());
10945 return true;
10946 }
10947 if (!snap_exists) {
10948 _pool_op_reply(op, 0, osdmap.get_epoch());
10949 return true;
10950 }
10951 return false;
10952 case POOL_OP_DELETE_UNMANAGED_SNAP:
10953 if (p->is_pool_snaps_mode()) {
10954 _pool_op_reply(op, -EINVAL, osdmap.get_epoch());
10955 return true;
10956 }
10957 if (p->is_removed_snap(m->snapid)) {
10958 _pool_op_reply(op, 0, osdmap.get_epoch());
10959 return true;
10960 }
10961 return false;
10962 case POOL_OP_DELETE:
10963 if (osdmap.lookup_pg_pool_name(m->name.c_str()) >= 0) {
10964 _pool_op_reply(op, 0, osdmap.get_epoch());
10965 return true;
10966 }
10967 return false;
10968 case POOL_OP_AUID_CHANGE:
10969 return false;
10970 default:
10971 ceph_abort();
10972 break;
10973 }
10974
10975 return false;
10976}
10977
10978bool OSDMonitor::preprocess_pool_op_create(MonOpRequestRef op)
10979{
10980 op->mark_osdmon_event(__func__);
10981 MPoolOp *m = static_cast<MPoolOp*>(op->get_req());
10982 MonSession *session = m->get_session();
10983 if (!session) {
10984 _pool_op_reply(op, -EPERM, osdmap.get_epoch());
10985 return true;
10986 }
10987 if (!session->is_capable("osd", MON_CAP_W)) {
10988 dout(5) << "attempt to create new pool without sufficient auid privileges!"
10989 << "message: " << *m << std::endl
10990 << "caps: " << session->caps << dendl;
10991 _pool_op_reply(op, -EPERM, osdmap.get_epoch());
10992 return true;
10993 }
10994
10995 int64_t pool = osdmap.lookup_pg_pool_name(m->name.c_str());
10996 if (pool >= 0) {
10997 _pool_op_reply(op, 0, osdmap.get_epoch());
10998 return true;
10999 }
11000
11001 return false;
11002}
11003
11004bool OSDMonitor::prepare_pool_op(MonOpRequestRef op)
11005{
11006 op->mark_osdmon_event(__func__);
11007 MPoolOp *m = static_cast<MPoolOp*>(op->get_req());
11008 dout(10) << "prepare_pool_op " << *m << dendl;
11009 if (m->op == POOL_OP_CREATE) {
11010 return prepare_pool_op_create(op);
11011 } else if (m->op == POOL_OP_DELETE) {
11012 return prepare_pool_op_delete(op);
11013 }
11014
11015 int ret = 0;
11016 bool changed = false;
11017
11018 if (!osdmap.have_pg_pool(m->pool)) {
11019 _pool_op_reply(op, -ENOENT, osdmap.get_epoch());
11020 return false;
11021 }
11022
11023 const pg_pool_t *pool = osdmap.get_pg_pool(m->pool);
11024
11025 switch (m->op) {
11026 case POOL_OP_CREATE_SNAP:
11027 if (pool->is_tier()) {
11028 ret = -EINVAL;
11029 _pool_op_reply(op, ret, osdmap.get_epoch());
11030 return false;
11031 } // else, fall through
11032 case POOL_OP_DELETE_SNAP:
11033 if (!pool->is_unmanaged_snaps_mode()) {
11034 bool snap_exists = pool->snap_exists(m->name.c_str());
11035 if ((m->op == POOL_OP_CREATE_SNAP && snap_exists)
11036 || (m->op == POOL_OP_DELETE_SNAP && !snap_exists)) {
11037 ret = 0;
11038 } else {
11039 break;
11040 }
11041 } else {
11042 ret = -EINVAL;
11043 }
11044 _pool_op_reply(op, ret, osdmap.get_epoch());
11045 return false;
11046
11047 case POOL_OP_DELETE_UNMANAGED_SNAP:
11048 // we won't allow removal of an unmanaged snapshot from a pool
11049 // not in unmanaged snaps mode.
11050 if (!pool->is_unmanaged_snaps_mode()) {
11051 _pool_op_reply(op, -ENOTSUP, osdmap.get_epoch());
11052 return false;
11053 }
11054 /* fall-thru */
11055 case POOL_OP_CREATE_UNMANAGED_SNAP:
11056 // but we will allow creating an unmanaged snapshot on any pool
11057 // as long as it is not in 'pool' snaps mode.
11058 if (pool->is_pool_snaps_mode()) {
11059 _pool_op_reply(op, -EINVAL, osdmap.get_epoch());
11060 return false;
11061 }
11062 }
11063
11064 // projected pool info
11065 pg_pool_t pp;
11066 if (pending_inc.new_pools.count(m->pool))
11067 pp = pending_inc.new_pools[m->pool];
11068 else
11069 pp = *osdmap.get_pg_pool(m->pool);
11070
11071 bufferlist reply_data;
11072
11073 // pool snaps vs unmanaged snaps are mutually exclusive
11074 switch (m->op) {
11075 case POOL_OP_CREATE_SNAP:
11076 case POOL_OP_DELETE_SNAP:
11077 if (pp.is_unmanaged_snaps_mode()) {
11078 ret = -EINVAL;
11079 goto out;
11080 }
11081 break;
11082
11083 case POOL_OP_CREATE_UNMANAGED_SNAP:
11084 case POOL_OP_DELETE_UNMANAGED_SNAP:
11085 if (pp.is_pool_snaps_mode()) {
11086 ret = -EINVAL;
11087 goto out;
11088 }
11089 }
11090
11091 switch (m->op) {
11092 case POOL_OP_CREATE_SNAP:
11093 if (!pp.snap_exists(m->name.c_str())) {
11094 pp.add_snap(m->name.c_str(), ceph_clock_now());
11095 dout(10) << "create snap in pool " << m->pool << " " << m->name << " seq " << pp.get_snap_epoch() << dendl;
11096 changed = true;
11097 }
11098 break;
11099
11100 case POOL_OP_DELETE_SNAP:
11101 {
11102 snapid_t s = pp.snap_exists(m->name.c_str());
11103 if (s) {
11104 pp.remove_snap(s);
11105 changed = true;
11106 }
11107 }
11108 break;
11109
11110 case POOL_OP_CREATE_UNMANAGED_SNAP:
11111 {
11112 uint64_t snapid;
11113 pp.add_unmanaged_snap(snapid);
11114 ::encode(snapid, reply_data);
11115 changed = true;
11116 }
11117 break;
11118
11119 case POOL_OP_DELETE_UNMANAGED_SNAP:
11120 if (!pp.is_removed_snap(m->snapid)) {
11121 pp.remove_unmanaged_snap(m->snapid);
11122 changed = true;
11123 }
11124 break;
11125
11126 case POOL_OP_AUID_CHANGE:
11127 if (pp.auid != m->auid) {
11128 pp.auid = m->auid;
11129 changed = true;
11130 }
11131 break;
11132
11133 default:
11134 ceph_abort();
11135 break;
11136 }
11137
11138 if (changed) {
11139 pp.set_snap_epoch(pending_inc.epoch);
11140 pending_inc.new_pools[m->pool] = pp;
11141 }
11142
11143 out:
11144 wait_for_finished_proposal(op, new OSDMonitor::C_PoolOp(this, op, ret, pending_inc.epoch, &reply_data));
11145 return true;
11146}
11147
11148bool OSDMonitor::prepare_pool_op_create(MonOpRequestRef op)
11149{
11150 op->mark_osdmon_event(__func__);
11151 int err = prepare_new_pool(op);
11152 wait_for_finished_proposal(op, new OSDMonitor::C_PoolOp(this, op, err, pending_inc.epoch));
11153 return true;
11154}
11155
11156int OSDMonitor::_check_remove_pool(int64_t pool_id, const pg_pool_t& pool,
11157 ostream *ss)
11158{
11159 const string& poolstr = osdmap.get_pool_name(pool_id);
11160
11161 // If the Pool is in use by CephFS, refuse to delete it
11162 FSMap const &pending_fsmap = mon->mdsmon()->get_pending();
11163 if (pending_fsmap.pool_in_use(pool_id)) {
11164 *ss << "pool '" << poolstr << "' is in use by CephFS";
11165 return -EBUSY;
11166 }
11167
11168 if (pool.tier_of >= 0) {
11169 *ss << "pool '" << poolstr << "' is a tier of '"
11170 << osdmap.get_pool_name(pool.tier_of) << "'";
11171 return -EBUSY;
11172 }
11173 if (!pool.tiers.empty()) {
11174 *ss << "pool '" << poolstr << "' has tiers";
11175 for(auto tier : pool.tiers) {
11176 *ss << " " << osdmap.get_pool_name(tier);
11177 }
11178 return -EBUSY;
11179 }
11180
11181 if (!g_conf->mon_allow_pool_delete) {
11182 *ss << "pool deletion is disabled; you must first set the mon_allow_pool_delete config option to true before you can destroy a pool";
11183 return -EPERM;
11184 }
11185
11186 if (pool.has_flag(pg_pool_t::FLAG_NODELETE)) {
11187 *ss << "pool deletion is disabled; you must unset nodelete flag for the pool first";
11188 return -EPERM;
11189 }
11190
11191 *ss << "pool '" << poolstr << "' removed";
11192 return 0;
11193}
11194
11195/**
11196 * Check if it is safe to add a tier to a base pool
11197 *
11198 * @return
11199 * True if the operation should proceed, false if we should abort here
11200 * (abort doesn't necessarily mean error, could be idempotency)
11201 */
11202bool OSDMonitor::_check_become_tier(
11203 const int64_t tier_pool_id, const pg_pool_t *tier_pool,
11204 const int64_t base_pool_id, const pg_pool_t *base_pool,
11205 int *err,
11206 ostream *ss) const
11207{
11208 const std::string &tier_pool_name = osdmap.get_pool_name(tier_pool_id);
11209 const std::string &base_pool_name = osdmap.get_pool_name(base_pool_id);
11210
11211 const FSMap &pending_fsmap = mon->mdsmon()->get_pending();
11212 if (pending_fsmap.pool_in_use(tier_pool_id)) {
11213 *ss << "pool '" << tier_pool_name << "' is in use by CephFS";
11214 *err = -EBUSY;
11215 return false;
11216 }
11217
11218 if (base_pool->tiers.count(tier_pool_id)) {
11219 assert(tier_pool->tier_of == base_pool_id);
11220 *err = 0;
11221 *ss << "pool '" << tier_pool_name << "' is now (or already was) a tier of '"
11222 << base_pool_name << "'";
11223 return false;
11224 }
11225
11226 if (base_pool->is_tier()) {
11227 *ss << "pool '" << base_pool_name << "' is already a tier of '"
11228 << osdmap.get_pool_name(base_pool->tier_of) << "', "
11229 << "multiple tiers are not yet supported.";
11230 *err = -EINVAL;
11231 return false;
11232 }
11233
11234 if (tier_pool->has_tiers()) {
11235 *ss << "pool '" << tier_pool_name << "' has following tier(s) already:";
11236 for (set<uint64_t>::iterator it = tier_pool->tiers.begin();
11237 it != tier_pool->tiers.end(); ++it)
11238 *ss << "'" << osdmap.get_pool_name(*it) << "',";
11239 *ss << " multiple tiers are not yet supported.";
11240 *err = -EINVAL;
11241 return false;
11242 }
11243
11244 if (tier_pool->is_tier()) {
11245 *ss << "tier pool '" << tier_pool_name << "' is already a tier of '"
11246 << osdmap.get_pool_name(tier_pool->tier_of) << "'";
11247 *err = -EINVAL;
11248 return false;
11249 }
11250
11251 *err = 0;
11252 return true;
11253}
11254
11255
11256/**
11257 * Check if it is safe to remove a tier from this base pool
11258 *
11259 * @return
11260 * True if the operation should proceed, false if we should abort here
11261 * (abort doesn't necessarily mean error, could be idempotency)
11262 */
11263bool OSDMonitor::_check_remove_tier(
11264 const int64_t base_pool_id, const pg_pool_t *base_pool,
11265 const pg_pool_t *tier_pool,
11266 int *err, ostream *ss) const
11267{
11268 const std::string &base_pool_name = osdmap.get_pool_name(base_pool_id);
11269
11270 // Apply CephFS-specific checks
11271 const FSMap &pending_fsmap = mon->mdsmon()->get_pending();
11272 if (pending_fsmap.pool_in_use(base_pool_id)) {
11273 if (base_pool->type != pg_pool_t::TYPE_REPLICATED) {
11274 // If the underlying pool is erasure coded, we can't permit the
11275 // removal of the replicated tier that CephFS relies on to access it
11276 *ss << "pool '" << base_pool_name << "' is in use by CephFS via its tier";
11277 *err = -EBUSY;
11278 return false;
11279 }
11280
11281 if (tier_pool && tier_pool->cache_mode == pg_pool_t::CACHEMODE_WRITEBACK) {
11282 *ss << "pool '" << base_pool_name << "' is in use by CephFS, and this "
11283 "tier is still in use as a writeback cache. Change the cache "
11284 "mode and flush the cache before removing it";
11285 *err = -EBUSY;
11286 return false;
11287 }
11288 }
11289
11290 *err = 0;
11291 return true;
11292}
11293
11294int OSDMonitor::_prepare_remove_pool(
11295 int64_t pool, ostream *ss, bool no_fake)
11296{
224ce89b 11297 dout(10) << __func__ << " " << pool << dendl;
7c673cae
FG
11298 const pg_pool_t *p = osdmap.get_pg_pool(pool);
11299 int r = _check_remove_pool(pool, *p, ss);
11300 if (r < 0)
11301 return r;
11302
11303 auto new_pool = pending_inc.new_pools.find(pool);
11304 if (new_pool != pending_inc.new_pools.end()) {
11305 // if there is a problem with the pending info, wait and retry
11306 // this op.
11307 const auto& p = new_pool->second;
11308 int r = _check_remove_pool(pool, p, ss);
11309 if (r < 0)
11310 return -EAGAIN;
11311 }
11312
11313 if (pending_inc.old_pools.count(pool)) {
224ce89b 11314 dout(10) << __func__ << " " << pool << " already pending removal"
7c673cae
FG
11315 << dendl;
11316 return 0;
11317 }
11318
11319 if (g_conf->mon_fake_pool_delete && !no_fake) {
11320 string old_name = osdmap.get_pool_name(pool);
11321 string new_name = old_name + "." + stringify(pool) + ".DELETED";
11322 dout(1) << __func__ << " faking pool deletion: renaming " << pool << " "
11323 << old_name << " -> " << new_name << dendl;
11324 pending_inc.new_pool_names[pool] = new_name;
11325 return 0;
11326 }
11327
11328 // remove
11329 pending_inc.old_pools.insert(pool);
11330
224ce89b 11331 // remove any pg_temp mappings for this pool
7c673cae
FG
11332 for (auto p = osdmap.pg_temp->begin();
11333 p != osdmap.pg_temp->end();
11334 ++p) {
11335 if (p->first.pool() == (uint64_t)pool) {
224ce89b 11336 dout(10) << __func__ << " " << pool << " removing obsolete pg_temp "
7c673cae
FG
11337 << p->first << dendl;
11338 pending_inc.new_pg_temp[p->first].clear();
11339 }
11340 }
224ce89b 11341 // remove any primary_temp mappings for this pool
7c673cae
FG
11342 for (auto p = osdmap.primary_temp->begin();
11343 p != osdmap.primary_temp->end();
11344 ++p) {
11345 if (p->first.pool() == (uint64_t)pool) {
224ce89b 11346 dout(10) << __func__ << " " << pool
7c673cae
FG
11347 << " removing obsolete primary_temp" << p->first << dendl;
11348 pending_inc.new_primary_temp[p->first] = -1;
11349 }
11350 }
224ce89b
WB
11351 // remove any pg_upmap mappings for this pool
11352 for (auto& p : osdmap.pg_upmap) {
11353 if (p.first.pool() == (uint64_t)pool) {
11354 dout(10) << __func__ << " " << pool
11355 << " removing obsolete pg_upmap "
11356 << p.first << dendl;
11357 pending_inc.old_pg_upmap.insert(p.first);
11358 }
11359 }
11360 // remove any pg_upmap_items mappings for this pool
11361 for (auto& p : osdmap.pg_upmap_items) {
11362 if (p.first.pool() == (uint64_t)pool) {
11363 dout(10) << __func__ << " " << pool
11364 << " removing obsolete pg_upmap_items " << p.first
11365 << dendl;
11366 pending_inc.old_pg_upmap_items.insert(p.first);
11367 }
11368 }
35e4c445
FG
11369
11370 // remove any choose_args for this pool
11371 CrushWrapper newcrush;
11372 _get_pending_crush(newcrush);
11373 if (newcrush.have_choose_args(pool)) {
11374 dout(10) << __func__ << " removing choose_args for pool " << pool << dendl;
11375 newcrush.rm_choose_args(pool);
11376 pending_inc.crush.clear();
11377 newcrush.encode(pending_inc.crush, mon->get_quorum_con_features());
11378 }
7c673cae
FG
11379 return 0;
11380}
11381
11382int OSDMonitor::_prepare_rename_pool(int64_t pool, string newname)
11383{
11384 dout(10) << "_prepare_rename_pool " << pool << dendl;
11385 if (pending_inc.old_pools.count(pool)) {
11386 dout(10) << "_prepare_rename_pool " << pool << " pending removal" << dendl;
11387 return -ENOENT;
11388 }
11389 for (map<int64_t,string>::iterator p = pending_inc.new_pool_names.begin();
11390 p != pending_inc.new_pool_names.end();
11391 ++p) {
11392 if (p->second == newname && p->first != pool) {
11393 return -EEXIST;
11394 }
11395 }
11396
11397 pending_inc.new_pool_names[pool] = newname;
11398 return 0;
11399}
11400
11401bool OSDMonitor::prepare_pool_op_delete(MonOpRequestRef op)
11402{
11403 op->mark_osdmon_event(__func__);
11404 MPoolOp *m = static_cast<MPoolOp*>(op->get_req());
11405 ostringstream ss;
11406 int ret = _prepare_remove_pool(m->pool, &ss, false);
11407 if (ret == -EAGAIN) {
11408 wait_for_finished_proposal(op, new C_RetryMessage(this, op));
11409 return true;
11410 }
11411 if (ret < 0)
11412 dout(10) << __func__ << " got " << ret << " " << ss.str() << dendl;
11413 wait_for_finished_proposal(op, new OSDMonitor::C_PoolOp(this, op, ret,
11414 pending_inc.epoch));
11415 return true;
11416}
11417
11418void OSDMonitor::_pool_op_reply(MonOpRequestRef op,
11419 int ret, epoch_t epoch, bufferlist *blp)
11420{
11421 op->mark_osdmon_event(__func__);
11422 MPoolOp *m = static_cast<MPoolOp*>(op->get_req());
11423 dout(20) << "_pool_op_reply " << ret << dendl;
11424 MPoolOpReply *reply = new MPoolOpReply(m->fsid, m->get_tid(),
11425 ret, epoch, get_last_committed(), blp);
11426 mon->send_reply(op, reply);
11427}