]> git.proxmox.com Git - ceph.git/blame - ceph/src/mon/FSCommands.cc
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / mon / FSCommands.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) 2017 Red Hat Ltd
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15
16#include "OSDMonitor.h"
17#include "PGMonitor.h"
18
19#include "FSCommands.h"
20#include "MDSMonitor.h"
21
22
23
24static const string EXPERIMENTAL_WARNING("Warning! This feature is experimental."
25"It may cause problems up to and including data loss."
26"Consult the documentation at ceph.com, and if unsure, do not proceed."
27"Add --yes-i-really-mean-it if you are certain.");
28
29
30
31class FlagSetHandler : public FileSystemCommandHandler
32{
33 public:
34 FlagSetHandler()
35 : FileSystemCommandHandler("fs flag set")
36 {
37 }
38
39 int handle(
40 Monitor *mon,
41 FSMap &fsmap,
42 MonOpRequestRef op,
43 map<string, cmd_vartype> &cmdmap,
44 std::stringstream &ss) override
45 {
46 string flag_name;
f64942e4 47 cmd_getval_throws(g_ceph_context, cmdmap, "flag_name", flag_name);
7c673cae
FG
48
49 string flag_val;
f64942e4 50 cmd_getval_throws(g_ceph_context, cmdmap, "val", flag_val);
7c673cae
FG
51
52 string confirm;
f64942e4 53 cmd_getval_throws(g_ceph_context, cmdmap, "confirm", confirm);
7c673cae
FG
54
55 if (flag_name == "enable_multiple") {
56 bool flag_bool = false;
57 int r = parse_bool(flag_val, &flag_bool, ss);
58 if (r != 0) {
59 ss << "Invalid boolean value '" << flag_val << "'";
60 return r;
61 }
62
63 bool jewel = mon->get_quorum_con_features() & CEPH_FEATURE_SERVER_JEWEL;
64 if (flag_bool && !jewel) {
65 ss << "Multiple-filesystems are forbidden until all mons are updated";
66 return -EINVAL;
67 }
68 if (confirm != "--yes-i-really-mean-it") {
69 ss << EXPERIMENTAL_WARNING;
70 }
71 fsmap.set_enable_multiple(flag_bool);
72 return 0;
73 } else {
74 ss << "Unknown flag '" << flag_name << "'";
75 return -EINVAL;
76 }
77 }
78};
79
80class FsNewHandler : public FileSystemCommandHandler
81{
82 public:
c07f9fc5
FG
83 FsNewHandler(Paxos *paxos)
84 : FileSystemCommandHandler("fs new"), m_paxos(paxos)
7c673cae
FG
85 {
86 }
87
c07f9fc5
FG
88 bool batched_propose() override {
89 return true;
90 }
91
7c673cae
FG
92 int handle(
93 Monitor *mon,
94 FSMap &fsmap,
95 MonOpRequestRef op,
96 map<string, cmd_vartype> &cmdmap,
31f18b77 97 std::stringstream &ss) override
7c673cae 98 {
c07f9fc5
FG
99 assert(m_paxos->is_plugged());
100
7c673cae 101 string metadata_name;
f64942e4 102 cmd_getval_throws(g_ceph_context, cmdmap, "metadata", metadata_name);
7c673cae
FG
103 int64_t metadata = mon->osdmon()->osdmap.lookup_pg_pool_name(metadata_name);
104 if (metadata < 0) {
105 ss << "pool '" << metadata_name << "' does not exist";
106 return -ENOENT;
107 }
108
c07f9fc5 109 string force_str;
f64942e4 110 cmd_getval_throws(g_ceph_context,cmdmap, "force", force_str);
c07f9fc5 111 bool force = (force_str == "--force");
31f18b77
FG
112 const pool_stat_t *stat = mon->pgservice->get_pool_stat(metadata);
113 if (stat) {
114 int64_t metadata_num_objects = stat->stats.sum.num_objects;
c07f9fc5 115 if (!force && metadata_num_objects > 0) {
31f18b77
FG
116 ss << "pool '" << metadata_name
117 << "' already contains some objects. Use an empty pool instead.";
118 return -EINVAL;
119 }
7c673cae
FG
120 }
121
122 string data_name;
f64942e4 123 cmd_getval_throws(g_ceph_context, cmdmap, "data", data_name);
7c673cae
FG
124 int64_t data = mon->osdmon()->osdmap.lookup_pg_pool_name(data_name);
125 if (data < 0) {
126 ss << "pool '" << data_name << "' does not exist";
127 return -ENOENT;
128 }
129 if (data == 0) {
130 ss << "pool '" << data_name << "' has id 0, which CephFS does not allow. Use another pool or recreate it to get a non-zero pool id.";
131 return -EINVAL;
132 }
c07f9fc5 133
7c673cae 134 string fs_name;
f64942e4 135 cmd_getval_throws(g_ceph_context, cmdmap, "fs_name", fs_name);
7c673cae
FG
136 if (fs_name.empty()) {
137 // Ensure fs name is not empty so that we can implement
138 // commmands that refer to FS by name in future.
139 ss << "Filesystem name may not be empty";
140 return -EINVAL;
141 }
142
143 if (fsmap.get_filesystem(fs_name)) {
144 auto fs = fsmap.get_filesystem(fs_name);
145 if (*(fs->mds_map.get_data_pools().begin()) == data
146 && fs->mds_map.get_metadata_pool() == metadata) {
147 // Identical FS created already, this is a no-op
148 ss << "filesystem '" << fs_name << "' already exists";
149 return 0;
150 } else {
151 ss << "filesystem already exists with name '" << fs_name << "'";
152 return -EINVAL;
153 }
154 }
155
156 if (fsmap.filesystem_count() > 0
157 && !fsmap.get_enable_multiple()) {
158 ss << "Creation of multiple filesystems is disabled. To enable "
159 "this experimental feature, use 'ceph fs flag set enable_multiple "
160 "true'";
161 return -EINVAL;
162 }
163
164 for (auto fs : fsmap.get_filesystems()) {
31f18b77 165 const std::vector<int64_t> &data_pools = fs->mds_map.get_data_pools();
7c673cae 166 string sure;
31f18b77 167 if ((std::find(data_pools.begin(), data_pools.end(), data) != data_pools.end()
7c673cae 168 || fs->mds_map.get_metadata_pool() == metadata)
f64942e4 169 && ((!cmd_getval_throws(g_ceph_context, cmdmap, "sure", sure)
7c673cae
FG
170 || sure != "--allow-dangerous-metadata-overlay"))) {
171 ss << "Filesystem '" << fs_name
172 << "' is already using one of the specified RADOS pools. This should ONLY be done in emergencies and after careful reading of the documentation. Pass --allow-dangerous-metadata-overlay to permit this.";
173 return -EEXIST;
174 }
175 }
176
177 pg_pool_t const *data_pool = mon->osdmon()->osdmap.get_pg_pool(data);
178 assert(data_pool != NULL); // Checked it existed above
179 pg_pool_t const *metadata_pool = mon->osdmon()->osdmap.get_pg_pool(metadata);
180 assert(metadata_pool != NULL); // Checked it existed above
181
c07f9fc5 182 int r = _check_pool(mon->osdmon()->osdmap, data, false, force, &ss);
7c673cae
FG
183 if (r < 0) {
184 return r;
185 }
186
c07f9fc5 187 r = _check_pool(mon->osdmon()->osdmap, metadata, true, force, &ss);
7c673cae
FG
188 if (r < 0) {
189 return r;
190 }
35e4c445
FG
191
192 // if we're running as luminous, we have to set the pool application metadata
193 if (mon->osdmon()->osdmap.require_osd_release >= CEPH_RELEASE_LUMINOUS ||
194 mon->osdmon()->pending_inc.new_require_osd_release >= CEPH_RELEASE_LUMINOUS) {
195 if (!mon->osdmon()->is_writeable()) {
196 // not allowed to write yet, so retry when we can
197 mon->osdmon()->wait_for_writeable(op, new PaxosService::C_RetryMessage(mon->mdsmon(), op));
198 return -EAGAIN;
199 }
200 mon->osdmon()->do_application_enable(data,
201 pg_pool_t::APPLICATION_NAME_CEPHFS);
202 mon->osdmon()->do_application_enable(metadata,
203 pg_pool_t::APPLICATION_NAME_CEPHFS);
204 mon->osdmon()->propose_pending();
205 }
c07f9fc5 206
7c673cae
FG
207 // All checks passed, go ahead and create.
208 fsmap.create_filesystem(fs_name, metadata, data,
209 mon->get_quorum_con_features());
210 ss << "new fs with metadata pool " << metadata << " and data pool " << data;
211 return 0;
212 }
c07f9fc5
FG
213
214private:
215 Paxos *m_paxos;
7c673cae
FG
216};
217
218class SetHandler : public FileSystemCommandHandler
219{
220public:
221 SetHandler()
222 : FileSystemCommandHandler("fs set")
223 {}
224
225 int handle(
226 Monitor *mon,
227 FSMap &fsmap,
228 MonOpRequestRef op,
229 map<string, cmd_vartype> &cmdmap,
230 std::stringstream &ss) override
231 {
232 std::string fs_name;
f64942e4 233 if (!cmd_getval_throws(g_ceph_context, cmdmap, "fs_name", fs_name) || fs_name.empty()) {
7c673cae
FG
234 ss << "Missing filesystem name";
235 return -EINVAL;
236 }
237
238 auto fs = fsmap.get_filesystem(fs_name);
239 if (fs == nullptr) {
240 ss << "Not found: '" << fs_name << "'";
241 return -ENOENT;
242 }
243
244 string var;
f64942e4 245 if (!cmd_getval_throws(g_ceph_context, cmdmap, "var", var) || var.empty()) {
7c673cae
FG
246 ss << "Invalid variable";
247 return -EINVAL;
248 }
249 string val;
250 string interr;
251 int64_t n = 0;
f64942e4 252 if (!cmd_getval_throws(g_ceph_context, cmdmap, "val", val)) {
7c673cae
FG
253 return -EINVAL;
254 }
255 // we got a string. see if it contains an int.
256 n = strict_strtoll(val.c_str(), 10, &interr);
257 if (var == "max_mds") {
258 // NOTE: see also "mds set_max_mds", which can modify the same field.
259 if (interr.length()) {
260 ss << interr;
261 return -EINVAL;
262 }
263
264 if (n <= 0) {
265 ss << "You must specify at least one MDS";
266 return -EINVAL;
267 }
268
269 if (!fs->mds_map.allows_multimds() && n > fs->mds_map.get_max_mds() &&
270 n > 1) {
271 ss << "multi-MDS clusters are not enabled; set 'allow_multimds' to enable";
272 return -EINVAL;
273 }
274 if (n > MAX_MDS) {
275 ss << "may not have more than " << MAX_MDS << " MDS ranks";
276 return -EINVAL;
277 }
278 fsmap.modify_filesystem(
279 fs->fscid,
280 [n](std::shared_ptr<Filesystem> fs)
281 {
282 fs->mds_map.set_max_mds(n);
283 });
284 } else if (var == "inline_data") {
285 bool enable_inline = false;
286 int r = parse_bool(val, &enable_inline, ss);
287 if (r != 0) {
288 return r;
289 }
290
291 if (enable_inline) {
292 string confirm;
f64942e4 293 if (!cmd_getval_throws(g_ceph_context, cmdmap, "confirm", confirm) ||
7c673cae
FG
294 confirm != "--yes-i-really-mean-it") {
295 ss << EXPERIMENTAL_WARNING;
296 return -EPERM;
297 }
298 ss << "inline data enabled";
299
300 fsmap.modify_filesystem(
301 fs->fscid,
302 [](std::shared_ptr<Filesystem> fs)
303 {
304 fs->mds_map.set_inline_data_enabled(true);
305 });
306
307 // Update `compat`
308 CompatSet c = fsmap.get_compat();
309 c.incompat.insert(MDS_FEATURE_INCOMPAT_INLINE);
310 fsmap.update_compat(c);
311 } else {
312 ss << "inline data disabled";
313 fsmap.modify_filesystem(
314 fs->fscid,
315 [](std::shared_ptr<Filesystem> fs)
316 {
317 fs->mds_map.set_inline_data_enabled(false);
318 });
319 }
320 } else if (var == "balancer") {
31f18b77
FG
321 if (val.empty()) {
322 ss << "unsetting the metadata load balancer";
323 } else {
324 ss << "setting the metadata load balancer to " << val;
325 }
326 fsmap.modify_filesystem(
327 fs->fscid,
328 [val](std::shared_ptr<Filesystem> fs)
7c673cae
FG
329 {
330 fs->mds_map.set_balancer(val);
331 });
332 return true;
333 } else if (var == "max_file_size") {
334 if (interr.length()) {
335 ss << var << " requires an integer value";
336 return -EINVAL;
337 }
338 if (n < CEPH_MIN_STRIPE_UNIT) {
339 ss << var << " must at least " << CEPH_MIN_STRIPE_UNIT;
340 return -ERANGE;
341 }
342 fsmap.modify_filesystem(
343 fs->fscid,
344 [n](std::shared_ptr<Filesystem> fs)
345 {
346 fs->mds_map.set_max_filesize(n);
347 });
348 } else if (var == "allow_new_snaps") {
349 bool enable_snaps = false;
350 int r = parse_bool(val, &enable_snaps, ss);
351 if (r != 0) {
352 return r;
353 }
354
355 if (!enable_snaps) {
356 fsmap.modify_filesystem(
357 fs->fscid,
358 [](std::shared_ptr<Filesystem> fs)
359 {
360 fs->mds_map.clear_snaps_allowed();
361 });
362 ss << "disabled new snapshots";
363 } else {
364 string confirm;
365 if (!cmd_getval(g_ceph_context, cmdmap, "confirm", confirm) ||
366 confirm != "--yes-i-really-mean-it") {
367 ss << EXPERIMENTAL_WARNING;
368 return -EPERM;
369 }
370 fsmap.modify_filesystem(
371 fs->fscid,
372 [](std::shared_ptr<Filesystem> fs)
373 {
374 fs->mds_map.set_snaps_allowed();
375 });
376 ss << "enabled new snapshots";
377 }
378 } else if (var == "allow_multimds") {
379 bool enable_multimds = false;
380 int r = parse_bool(val, &enable_multimds, ss);
381 if (r != 0) {
382 return r;
383 }
384
385 if (!enable_multimds) {
386 fsmap.modify_filesystem(fs->fscid,
387 [](std::shared_ptr<Filesystem> fs)
388 {
389 fs->mds_map.clear_multimds_allowed();
390 });
391 ss << "disallowed increasing the cluster size past 1";
392 } else {
7c673cae
FG
393 fsmap.modify_filesystem(
394 fs->fscid,
395 [](std::shared_ptr<Filesystem> fs)
396 {
397 fs->mds_map.set_multimds_allowed();
398 });
399 ss << "enabled creation of more than 1 active MDS";
400 }
401 } else if (var == "allow_dirfrags") {
402 bool enable_dirfrags = false;
403 int r = parse_bool(val, &enable_dirfrags, ss);
404 if (r != 0) {
405 return r;
406 }
407
408 if (!enable_dirfrags) {
409 fsmap.modify_filesystem(fs->fscid,
410 [](std::shared_ptr<Filesystem> fs)
411 {
412 fs->mds_map.clear_dirfrags_allowed();
413 });
414 ss << "disallowed new directory fragmentation";
415 } else {
416 fsmap.modify_filesystem(
417 fs->fscid,
418 [](std::shared_ptr<Filesystem> fs)
419 {
420 fs->mds_map.set_dirfrags_allowed();
421 });
422 ss << "enabled directory fragmentation";
423 }
424 } else if (var == "cluster_down") {
425 bool is_down = false;
426 int r = parse_bool(val, &is_down, ss);
427 if (r != 0) {
428 return r;
429 }
430
431 fsmap.modify_filesystem(
432 fs->fscid,
433 [is_down](std::shared_ptr<Filesystem> fs)
434 {
435 if (is_down) {
436 fs->mds_map.set_flag(CEPH_MDSMAP_DOWN);
437 } else {
438 fs->mds_map.clear_flag(CEPH_MDSMAP_DOWN);
439 }
440 });
441
442 ss << "marked " << (is_down ? "down" : "up");
443 } else if (var == "standby_count_wanted") {
444 if (interr.length()) {
445 ss << var << " requires an integer value";
446 return -EINVAL;
447 }
448 if (n < 0) {
449 ss << var << " must be non-negative";
450 return -ERANGE;
451 }
452 fsmap.modify_filesystem(
453 fs->fscid,
454 [n](std::shared_ptr<Filesystem> fs)
455 {
456 fs->mds_map.set_standby_count_wanted(n);
457 });
f64942e4
AA
458 } else if (var == "session_timeout") {
459 if (interr.length()) {
460 ss << var << " requires an integer value";
461 return -EINVAL;
462 }
463 if (n < 30) {
464 ss << var << " must be at least 30s";
465 return -ERANGE;
466 }
467 fsmap.modify_filesystem(
468 fs->fscid,
469 [n](std::shared_ptr<Filesystem> fs)
470 {
471 fs->mds_map.set_session_timeout((uint32_t)n);
472 });
473 } else if (var == "session_autoclose") {
474 if (interr.length()) {
475 ss << var << " requires an integer value";
476 return -EINVAL;
477 }
478 if (n < 30) {
479 ss << var << " must be at least 30s";
480 return -ERANGE;
481 }
482 fsmap.modify_filesystem(
483 fs->fscid,
484 [n](std::shared_ptr<Filesystem> fs)
485 {
486 fs->mds_map.set_session_autoclose((uint32_t)n);
487 });
7c673cae
FG
488 } else {
489 ss << "unknown variable " << var;
490 return -EINVAL;
491 }
492
493 return 0;
494 }
495};
496
497class AddDataPoolHandler : public FileSystemCommandHandler
498{
499 public:
c07f9fc5
FG
500 AddDataPoolHandler(Paxos *paxos)
501 : FileSystemCommandHandler("fs add_data_pool"), m_paxos(paxos)
7c673cae
FG
502 {}
503
c07f9fc5
FG
504 bool batched_propose() override {
505 return true;
506 }
507
7c673cae
FG
508 int handle(
509 Monitor *mon,
510 FSMap &fsmap,
511 MonOpRequestRef op,
512 map<string, cmd_vartype> &cmdmap,
513 std::stringstream &ss) override
514 {
c07f9fc5
FG
515 assert(m_paxos->is_plugged());
516
7c673cae 517 string poolname;
f64942e4 518 cmd_getval_throws(g_ceph_context, cmdmap, "pool", poolname);
7c673cae
FG
519
520 std::string fs_name;
f64942e4 521 if (!cmd_getval_throws(g_ceph_context, cmdmap, "fs_name", fs_name)
7c673cae
FG
522 || fs_name.empty()) {
523 ss << "Missing filesystem name";
524 return -EINVAL;
525 }
526
527 auto fs = fsmap.get_filesystem(fs_name);
528 if (fs == nullptr) {
529 ss << "Not found: '" << fs_name << "'";
530 return -ENOENT;
531 }
532
533 int64_t poolid = mon->osdmon()->osdmap.lookup_pg_pool_name(poolname);
534 if (poolid < 0) {
535 string err;
536 poolid = strict_strtol(poolname.c_str(), 10, &err);
537 if (err.length()) {
538 ss << "pool '" << poolname << "' does not exist";
539 return -ENOENT;
540 }
541 }
542
c07f9fc5 543 int r = _check_pool(mon->osdmon()->osdmap, poolid, false, false, &ss);
7c673cae
FG
544 if (r != 0) {
545 return r;
546 }
547
31f18b77
FG
548 // no-op when the data_pool already on fs
549 if (fs->mds_map.is_data_pool(poolid)) {
550 ss << "data pool " << poolid << " is already on fs " << fs_name;
551 return 0;
552 }
553
b5b8bbf5
FG
554 // if we're running as luminous, we have to set the pool application metadata
555 if (mon->osdmon()->osdmap.require_osd_release >= CEPH_RELEASE_LUMINOUS ||
556 mon->osdmon()->pending_inc.new_require_osd_release >= CEPH_RELEASE_LUMINOUS) {
557 if (!mon->osdmon()->is_writeable()) {
558 // not allowed to write yet, so retry when we can
559 mon->osdmon()->wait_for_writeable(op, new PaxosService::C_RetryMessage(mon->mdsmon(), op));
560 return -EAGAIN;
561 }
562 mon->osdmon()->do_application_enable(poolid, pg_pool_t::APPLICATION_NAME_CEPHFS);
563 mon->osdmon()->propose_pending();
564 }
c07f9fc5 565
7c673cae
FG
566 fsmap.modify_filesystem(
567 fs->fscid,
568 [poolid](std::shared_ptr<Filesystem> fs)
569 {
570 fs->mds_map.add_data_pool(poolid);
571 });
572
573 ss << "added data pool " << poolid << " to fsmap";
574
575 return 0;
576 }
c07f9fc5
FG
577
578private:
579 Paxos *m_paxos;
7c673cae
FG
580};
581
582class SetDefaultHandler : public FileSystemCommandHandler
583{
584 public:
585 SetDefaultHandler()
586 : FileSystemCommandHandler("fs set-default")
587 {}
588
589 int handle(
590 Monitor *mon,
591 FSMap &fsmap,
592 MonOpRequestRef op,
593 map<string, cmd_vartype> &cmdmap,
594 std::stringstream &ss) override
595 {
596 std::string fs_name;
f64942e4 597 cmd_getval_throws(g_ceph_context, cmdmap, "fs_name", fs_name);
7c673cae
FG
598 auto fs = fsmap.get_filesystem(fs_name);
599 if (fs == nullptr) {
600 ss << "filesystem '" << fs_name << "' does not exist";
601 return -ENOENT;
602 }
603
604 fsmap.set_legacy_client_fscid(fs->fscid);
605 return 0;
606 }
607};
608
609class RemoveFilesystemHandler : public FileSystemCommandHandler
610{
611 public:
612 RemoveFilesystemHandler()
613 : FileSystemCommandHandler("fs rm")
614 {}
615
616 int handle(
617 Monitor *mon,
618 FSMap &fsmap,
619 MonOpRequestRef op,
620 map<string, cmd_vartype> &cmdmap,
621 std::stringstream &ss) override
622 {
a8e16298
TL
623 /* We may need to blacklist ranks. */
624 if (!mon->osdmon()->is_writeable()) {
625 // not allowed to write yet, so retry when we can
626 mon->osdmon()->wait_for_writeable(op, new PaxosService::C_RetryMessage(mon->mdsmon(), op));
627 return -EAGAIN;
628 }
629
7c673cae
FG
630 // Check caller has correctly named the FS to delete
631 // (redundant while there is only one FS, but command
632 // syntax should apply to multi-FS future)
633 string fs_name;
f64942e4 634 cmd_getval_throws(g_ceph_context, cmdmap, "fs_name", fs_name);
7c673cae
FG
635 auto fs = fsmap.get_filesystem(fs_name);
636 if (fs == nullptr) {
637 // Consider absence success to make deletes idempotent
638 ss << "filesystem '" << fs_name << "' does not exist";
639 return 0;
640 }
641
642 // Check that no MDS daemons are active
643 if (fs->mds_map.get_num_up_mds() > 0) {
644 ss << "all MDS daemons must be inactive before removing filesystem";
645 return -EINVAL;
646 }
647
648 // Check for confirmation flag
649 string sure;
f64942e4 650 cmd_getval_throws(g_ceph_context, cmdmap, "sure", sure);
7c673cae
FG
651 if (sure != "--yes-i-really-mean-it") {
652 ss << "this is a DESTRUCTIVE operation and will make data in your filesystem permanently" \
653 " inaccessible. Add --yes-i-really-mean-it if you are sure you wish to continue.";
654 return -EPERM;
655 }
656
657 if (fsmap.get_legacy_client_fscid() == fs->fscid) {
658 fsmap.set_legacy_client_fscid(FS_CLUSTER_ID_NONE);
659 }
660
661 std::vector<mds_gid_t> to_fail;
662 // There may be standby_replay daemons left here
663 for (const auto &i : fs->mds_map.get_mds_info()) {
664 assert(i.second.state == MDSMap::STATE_STANDBY_REPLAY);
665 to_fail.push_back(i.first);
666 }
667
668 for (const auto &gid : to_fail) {
669 // Standby replays don't write, so it isn't important to
670 // wait for an osdmap propose here: ignore return value.
1adf2230 671 mon->mdsmon()->fail_mds_gid(fsmap, gid);
7c673cae 672 }
a8e16298
TL
673 if (!to_fail.empty()) {
674 mon->osdmon()->propose_pending(); /* maybe new blacklists */
675 }
7c673cae
FG
676
677 fsmap.erase_filesystem(fs->fscid);
678
679 return 0;
680 }
681};
682
683class ResetFilesystemHandler : public FileSystemCommandHandler
684{
685 public:
686 ResetFilesystemHandler()
687 : FileSystemCommandHandler("fs reset")
688 {}
689
690 int handle(
691 Monitor *mon,
692 FSMap &fsmap,
693 MonOpRequestRef op,
694 map<string, cmd_vartype> &cmdmap,
695 std::stringstream &ss) override
696 {
697 string fs_name;
f64942e4 698 cmd_getval_throws(g_ceph_context, cmdmap, "fs_name", fs_name);
7c673cae
FG
699 auto fs = fsmap.get_filesystem(fs_name);
700 if (fs == nullptr) {
701 ss << "filesystem '" << fs_name << "' does not exist";
702 // Unlike fs rm, we consider this case an error
703 return -ENOENT;
704 }
705
706 // Check that no MDS daemons are active
707 if (fs->mds_map.get_num_up_mds() > 0) {
708 ss << "all MDS daemons must be inactive before resetting filesystem: set the cluster_down flag"
709 " and use `ceph mds fail` to make this so";
710 return -EINVAL;
711 }
712
713 // Check for confirmation flag
714 string sure;
f64942e4 715 cmd_getval_throws(g_ceph_context, cmdmap, "sure", sure);
7c673cae
FG
716 if (sure != "--yes-i-really-mean-it") {
717 ss << "this is a potentially destructive operation, only for use by experts in disaster recovery. "
718 "Add --yes-i-really-mean-it if you are sure you wish to continue.";
719 return -EPERM;
720 }
721
722 fsmap.reset_filesystem(fs->fscid);
723
724 return 0;
725 }
726};
727
728class RemoveDataPoolHandler : public FileSystemCommandHandler
729{
730 public:
731 RemoveDataPoolHandler()
732 : FileSystemCommandHandler("fs rm_data_pool")
733 {}
734
735 int handle(
736 Monitor *mon,
737 FSMap &fsmap,
738 MonOpRequestRef op,
739 map<string, cmd_vartype> &cmdmap,
740 std::stringstream &ss) override
741 {
742 string poolname;
f64942e4 743 cmd_getval_throws(g_ceph_context, cmdmap, "pool", poolname);
7c673cae
FG
744
745 std::string fs_name;
f64942e4 746 if (!cmd_getval_throws(g_ceph_context, cmdmap, "fs_name", fs_name)
7c673cae
FG
747 || fs_name.empty()) {
748 ss << "Missing filesystem name";
749 return -EINVAL;
750 }
751
752 auto fs = fsmap.get_filesystem(fs_name);
753 if (fs == nullptr) {
754 ss << "Not found: '" << fs_name << "'";
755 return -ENOENT;
756 }
757
758 int64_t poolid = mon->osdmon()->osdmap.lookup_pg_pool_name(poolname);
759 if (poolid < 0) {
760 string err;
761 poolid = strict_strtol(poolname.c_str(), 10, &err);
762 if (err.length()) {
763 ss << "pool '" << poolname << "' does not exist";
764 return -ENOENT;
765 } else if (poolid < 0) {
766 ss << "invalid pool id '" << poolid << "'";
767 return -EINVAL;
768 }
769 }
770
771 assert(poolid >= 0); // Checked by parsing code above
772
773 if (fs->mds_map.get_first_data_pool() == poolid) {
774 ss << "cannot remove default data pool";
775 return -EINVAL;
776 }
777
778
779 int r = 0;
780 fsmap.modify_filesystem(fs->fscid,
781 [&r, poolid](std::shared_ptr<Filesystem> fs)
782 {
783 r = fs->mds_map.remove_data_pool(poolid);
784 });
785 if (r == -ENOENT) {
786 // It was already removed, succeed in silence
787 return 0;
788 } else if (r == 0) {
789 // We removed it, succeed
790 ss << "removed data pool " << poolid << " from fsmap";
791 return 0;
792 } else {
793 // Unexpected error, bubble up
794 return r;
795 }
796 }
797};
798
799
800/**
801 * For commands that refer to a particular filesystem,
802 * enable wrapping to implement the legacy version of
803 * the command (like "mds add_data_pool" vs "fs add_data_pool")
804 *
805 * The wrapped handler must expect a fs_name argument in
806 * its command map.
807 */
808template<typename T>
809class LegacyHandler : public T
810{
811 std::string legacy_prefix;
812
813 public:
c07f9fc5
FG
814 template <typename... Args>
815 LegacyHandler(const std::string &new_prefix, Args&&... args)
816 : T(std::forward<Args>(args)...)
7c673cae
FG
817 {
818 legacy_prefix = new_prefix;
819 }
820
821 std::string const &get_prefix() override {return legacy_prefix;}
822
823 int handle(
824 Monitor *mon,
825 FSMap &fsmap,
826 MonOpRequestRef op,
827 map<string, cmd_vartype> &cmdmap,
828 std::stringstream &ss) override
829 {
830 auto fs = fsmap.get_legacy_filesystem();
831 if (fs == nullptr) {
832 ss << "No filesystem configured";
833 return -ENOENT;
834 }
835 std::map<string, cmd_vartype> modified = cmdmap;
94b18763 836 modified["fs_name"] = std::string(fs->mds_map.get_fs_name());
7c673cae
FG
837 return T::handle(mon, fsmap, op, modified, ss);
838 }
839};
840
841/**
842 * For commands with an alternative prefix
843 */
844template<typename T>
845class AliasHandler : public T
846{
847 std::string alias_prefix;
848
849 public:
850 AliasHandler(const std::string &new_prefix)
851 : T()
852 {
853 alias_prefix = new_prefix;
854 }
855
856 std::string const &get_prefix() override {return alias_prefix;}
857
858 int handle(
859 Monitor *mon,
860 FSMap &fsmap,
861 MonOpRequestRef op,
862 map<string, cmd_vartype> &cmdmap,
863 std::stringstream &ss) override
864 {
865 return T::handle(mon, fsmap, op, cmdmap, ss);
866 }
867};
868
869
c07f9fc5
FG
870std::list<std::shared_ptr<FileSystemCommandHandler> >
871FileSystemCommandHandler::load(Paxos *paxos)
7c673cae
FG
872{
873 std::list<std::shared_ptr<FileSystemCommandHandler> > handlers;
874
875 handlers.push_back(std::make_shared<SetHandler>());
876 handlers.push_back(std::make_shared<LegacyHandler<SetHandler> >("mds set"));
877 handlers.push_back(std::make_shared<FlagSetHandler>());
c07f9fc5 878 handlers.push_back(std::make_shared<AddDataPoolHandler>(paxos));
7c673cae 879 handlers.push_back(std::make_shared<LegacyHandler<AddDataPoolHandler> >(
c07f9fc5 880 "mds add_data_pool", paxos));
7c673cae
FG
881 handlers.push_back(std::make_shared<RemoveDataPoolHandler>());
882 handlers.push_back(std::make_shared<LegacyHandler<RemoveDataPoolHandler> >(
883 "mds remove_data_pool"));
884 handlers.push_back(std::make_shared<LegacyHandler<RemoveDataPoolHandler> >(
885 "mds rm_data_pool"));
c07f9fc5 886 handlers.push_back(std::make_shared<FsNewHandler>(paxos));
7c673cae
FG
887 handlers.push_back(std::make_shared<RemoveFilesystemHandler>());
888 handlers.push_back(std::make_shared<ResetFilesystemHandler>());
889
890 handlers.push_back(std::make_shared<SetDefaultHandler>());
891 handlers.push_back(std::make_shared<AliasHandler<SetDefaultHandler> >(
892 "fs set_default"));
893
894 return handlers;
895}
896
897int FileSystemCommandHandler::parse_bool(
898 const std::string &bool_str,
899 bool *result,
900 std::ostream &ss)
901{
902 assert(result != nullptr);
903
904 string interr;
905 int64_t n = strict_strtoll(bool_str.c_str(), 10, &interr);
906
907 if (bool_str == "false" || bool_str == "no"
908 || (interr.length() == 0 && n == 0)) {
909 *result = false;
910 return 0;
911 } else if (bool_str == "true" || bool_str == "yes"
912 || (interr.length() == 0 && n == 1)) {
913 *result = true;
914 return 0;
915 } else {
916 ss << "value must be false|no|0 or true|yes|1";
917 return -EINVAL;
918 }
919}
920
921int FileSystemCommandHandler::_check_pool(
922 OSDMap &osd_map,
923 const int64_t pool_id,
924 bool metadata,
c07f9fc5 925 bool force,
7c673cae
FG
926 std::stringstream *ss) const
927{
928 assert(ss != NULL);
929
930 const pg_pool_t *pool = osd_map.get_pg_pool(pool_id);
931 if (!pool) {
932 *ss << "pool id '" << pool_id << "' does not exist";
933 return -ENOENT;
934 }
935
936 const string& pool_name = osd_map.get_pool_name(pool_id);
937
938 if (pool->is_erasure() && metadata) {
939 *ss << "pool '" << pool_name << "' (id '" << pool_id << "')"
940 << " is an erasure-coded pool. Use of erasure-coded pools"
941 << " for CephFS metadata is not permitted";
942 return -EINVAL;
943 } else if (pool->is_erasure() && !pool->allows_ecoverwrites()) {
944 // non-overwriteable EC pools are only acceptable with a cache tier overlay
945 if (!pool->has_tiers() || !pool->has_read_tier() || !pool->has_write_tier()) {
946 *ss << "pool '" << pool_name << "' (id '" << pool_id << "')"
947 << " is an erasure-coded pool, with no overwrite support";
948 return -EINVAL;
949 }
950
951 // That cache tier overlay must be writeback, not readonly (it's the
952 // write operations like modify+truncate we care about support for)
953 const pg_pool_t *write_tier = osd_map.get_pg_pool(
954 pool->write_tier);
955 assert(write_tier != NULL); // OSDMonitor shouldn't allow DNE tier
956 if (write_tier->cache_mode == pg_pool_t::CACHEMODE_FORWARD
957 || write_tier->cache_mode == pg_pool_t::CACHEMODE_READONLY) {
958 *ss << "EC pool '" << pool_name << "' has a write tier ("
959 << osd_map.get_pool_name(pool->write_tier)
960 << ") that is configured "
961 "to forward writes. Use a cache mode such as 'writeback' for "
962 "CephFS";
963 return -EINVAL;
964 }
965 }
966
967 if (pool->is_tier()) {
968 *ss << " pool '" << pool_name << "' (id '" << pool_id
969 << "') is already in use as a cache tier.";
970 return -EINVAL;
971 }
972
c07f9fc5
FG
973 if (!force && !pool->application_metadata.empty() &&
974 pool->application_metadata.count(
975 pg_pool_t::APPLICATION_NAME_CEPHFS) == 0) {
976 *ss << " pool '" << pool_name << "' (id '" << pool_id
977 << "') has a non-CephFS application enabled.";
978 return -EINVAL;
979 }
980
7c673cae
FG
981 // Nothing special about this pool, so it is permissible
982 return 0;
983}
984