summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd/action/MirrorPool.cc
blob: ff7c303192ec15a5f9f1cac3065ef5d117620b54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/MirrorDaemonServiceInfo.h"
#include "tools/rbd/Shell.h"
#include "tools/rbd/Utils.h"
#include "include/Context.h"
#include "include/stringify.h"
#include "include/rbd/librbd.hpp"
#include "common/ceph_json.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/Formatter.h"
#include "common/TextTable.h"
#include "common/Throttle.h"
#include "global/global_context.h"
#include <fstream>
#include <functional>
#include <iostream>
#include <regex>
#include <set>
#include <boost/program_options.hpp>
#include "include/ceph_assert.h"

#include <atomic>

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "rbd::action::MirrorPool: "

namespace rbd {
namespace action {
namespace mirror_pool {

namespace at = argument_types;
namespace po = boost::program_options;

static const std::string ALL_NAME("all");
static const std::string SITE_NAME("site-name");

namespace {

void add_site_name_optional(po::options_description *options) {
  options->add_options()
    (SITE_NAME.c_str(), po::value<std::string>(), "local site name");
}

int set_site_name(librados::Rados& rados, const std::string& site_name) {
  librbd::RBD rbd;
  int r = rbd.mirror_site_name_set(rados, site_name);
  if (r == -EOPNOTSUPP) {
    std::cerr << "rbd: cluster does not support site names" << std::endl;
    return r;
  } else if (r < 0) {
    std::cerr << "rbd: failed to set site name" << cpp_strerror(r)
              << std::endl;
    return r;
  }

  return 0;
}

struct MirrorPeerDirection {};

void validate(boost::any& v, const std::vector<std::string>& values,
              MirrorPeerDirection *target_type, int) {
  po::validators::check_first_occurrence(v);
  const std::string &s = po::validators::get_single_string(values);

  if (s == "rx-only") {
    v = boost::any(RBD_MIRROR_PEER_DIRECTION_RX);
  } else if (s == "rx-tx") {
    v = boost::any(RBD_MIRROR_PEER_DIRECTION_RX_TX);
  } else {
    throw po::validation_error(po::validation_error::invalid_option_value);
  }
}

int validate_mirroring_enabled(librados::IoCtx& io_ctx) {
  librbd::RBD rbd;
  rbd_mirror_mode_t mirror_mode;
  int r = rbd.mirror_mode_get(io_ctx, &mirror_mode);
  if (r < 0) {
    std::cerr << "rbd: failed to retrieve mirror mode: "
              << cpp_strerror(r) << std::endl;
    return r;
  }

  if (mirror_mode == RBD_MIRROR_MODE_DISABLED) {
    std::cerr << "rbd: mirroring not enabled on the pool" << std::endl;
    return -EINVAL;
  }
  return 0;
}

int validate_uuid(const std::string &uuid) {
  std::regex pattern("^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$",
                       std::regex::icase);
  std::smatch match;
  if (!std::regex_match(uuid, match, pattern)) {
    std::cerr << "rbd: invalid uuid '" << uuid << "'" << std::endl;
    return -EINVAL;
  }
  return 0;
}

int read_key_file(std::string path, std::string* key) {
  std::ifstream key_file;
  key_file.open(path);
  if (key_file.fail()) {
    std::cerr << "rbd: failed to open " << path << std::endl;
    return -EINVAL;
  }

  std::getline(key_file, *key);
  if (key_file.bad()) {
    std::cerr << "rbd: failed to read key from " << path << std::endl;
    return -EINVAL;
  }

  key_file.close();
  return 0;
}

void add_uuid_option(po::options_description *positional) {
  positional->add_options()
    ("uuid", po::value<std::string>(), "peer uuid");
}

int get_uuid(const po::variables_map &vm, size_t arg_index,
             std::string *uuid) {
  *uuid = utils::get_positional_argument(vm, arg_index);
  if (uuid->empty()) {
    std::cerr << "rbd: must specify peer uuid" << std::endl;
    return -EINVAL;
  }
  return validate_uuid(*uuid);
}

int get_remote_cluster_spec(const po::variables_map &vm,
                            const std::string &spec,
                            std::string *remote_client_name,
                            std::string *remote_cluster,
                            std::map<std::string, std::string>* attributes) {
  if (vm.count("remote-client-name")) {
    *remote_client_name = vm["remote-client-name"].as<std::string>();
  }
  if (vm.count("remote-cluster")) {
    *remote_cluster = vm["remote-cluster"].as<std::string>();
  }
  if (vm.count("remote-mon-host")) {
    (*attributes)["mon_host"] = vm["remote-mon-host"].as<std::string>();
  }
  if (vm.count("remote-key-file")) {
    std::string key;
    int r = read_key_file(vm["remote-key-file"].as<std::string>(), &key);
    if (r < 0) {
      return r;
    }
    (*attributes)["key"] = key;
  }

  if (!spec.empty()) {
    std::regex pattern("^(?:(client\\.[^@]+)@)?([^/@]+)$");
    std::smatch match;
    if (!std::regex_match(spec, match, pattern)) {
      std::cerr << "rbd: invalid spec '" << spec << "'" << std::endl;
      return -EINVAL;
    }
    if (match[1].matched) {
      *remote_client_name = match[1];
    }
    *remote_cluster = match[2];
  }

  if (remote_cluster->empty()) {
    std::cerr << "rbd: remote cluster was not specified" << std::endl;
    return -EINVAL;
  }
  return 0;
}

int set_peer_config_key(librados::IoCtx& io_ctx, const std::string& peer_uuid,
                        std::map<std::string, std::string>&& attributes) {
  librbd::RBD rbd;
  int r = rbd.mirror_peer_set_attributes(io_ctx, peer_uuid, attributes);
  if (r == -EPERM) {
    std::cerr << "rbd: permission denied attempting to set peer "
              << "config-key secrets in the monitor" << std::endl;
    return r;
  } else if (r < 0) {
    std::cerr << "rbd: failed to update mirroring peer config: "
              << cpp_strerror(r) << std::endl;
    return r;
  }
  return 0;
}

int get_peer_config_key(librados::IoCtx& io_ctx, const std::string& peer_uuid,
                        std::map<std::string, std::string>* attributes) {
  librbd::RBD rbd;
  int r = rbd.mirror_peer_get_attributes(io_ctx, peer_uuid, attributes);
  if (r == -ENOENT) {
    return r;
  } else if (r == -EPERM) {
    std::cerr << "rbd: permission denied attempting to access peer "
              << "config-key secrets from the monitor" << std::endl;
    return r;
  } else if (r == -EINVAL) {
    std::cerr << "rbd: corrupt mirroring peer config" << std::endl;
    return r;
  } else if (r < 0) {
    std::cerr << "rbd: error reading mirroring peer config: "
              << cpp_strerror(r) << std::endl;
    return r;
  }

  return 0;
}

int update_peer_config_key(librados::IoCtx& io_ctx,
                           const std::string& peer_uuid,
                           const std::string& key,
                           const std::string& value) {
  std::map<std::string, std::string> attributes;
  int r = get_peer_config_key(io_ctx, peer_uuid, &attributes);
  if (r == -ENOENT) {
    return set_peer_config_key(io_ctx, peer_uuid, {{key, value}});
  } else if (r < 0) {
    return r;
  }

  if (value.empty()) {
    attributes.erase(key);
  } else {
    attributes[key] = value;
  }
  return set_peer_config_key(io_ctx, peer_uuid, std::move(attributes));
}

int format_mirror_peers(librados::IoCtx& io_ctx,
                        at::Format::Formatter formatter,
                        const std::vector<librbd::mirror_peer_t> &peers,
                        bool config_key) {
  TextTable tbl;
  if (formatter != nullptr) {
    formatter->open_array_section("peers");
  } else {
    std::cout << "Peers: ";
    if (peers.empty()) {
      std::cout << "none" << std::endl;
    } else {
      tbl.define_column("", TextTable::LEFT, TextTable::LEFT);
      tbl.define_column("UUID", TextTable::LEFT, TextTable::LEFT);
      tbl.define_column("NAME", TextTable::LEFT, TextTable::LEFT);
      tbl.define_column("CLIENT", TextTable::LEFT, TextTable::LEFT);
      if (config_key) {
        tbl.define_column("MON_HOST", TextTable::LEFT, TextTable::LEFT);
        tbl.define_column("KEY", TextTable::LEFT, TextTable::LEFT);
      }
    }
  }

  for (auto &peer : peers) {
    std::map<std::string, std::string> attributes;
    if (config_key) {
      int r = get_peer_config_key(io_ctx, peer.uuid, &attributes);
      if (r < 0 && r != -ENOENT) {
        return r;
      }
    }

    if (formatter != nullptr) {
      formatter->open_object_section("peer");
      formatter->dump_string("uuid", peer.uuid);
      formatter->dump_string("cluster_name", peer.cluster_name);
      formatter->dump_string("client_name", peer.client_name);
      for (auto& pair : attributes) {
        formatter->dump_string(pair.first.c_str(), pair.second);
      }
      formatter->close_section();
    } else {
      tbl << " "
          << peer.uuid
          << peer.cluster_name
          << peer.client_name;
      if (config_key) {
        tbl << attributes["mon_host"]
            << attributes["key"];
      }
      tbl << TextTable::endrow;
    }
  }

  if (formatter != nullptr) {
    formatter->close_section();
  } else {
    std::cout << std::endl << tbl;
  }
  return 0;
}

class ImageRequestBase {
public:
  void send() {
    dout(20) << this << " " << __func__ << ": image_name=" << m_image_name
             << dendl;

    auto ctx = new FunctionContext([this](int r) {
        handle_finalize(r);
      });

    // will pause here until slots are available
    m_finalize_ctx = m_throttle.start_op(ctx);

    open_image();
  }

protected:
  ImageRequestBase(librados::IoCtx &io_ctx, OrderedThrottle &throttle,
                   const std::string &image_name)
    : m_io_ctx(io_ctx), m_throttle(throttle), m_image_name(image_name) {
  }
  virtual ~ImageRequestBase() {
  }

  virtual bool skip_get_info() const {
    return false;
  }
  virtual void get_info(librbd::Image &image, librbd::mirror_image_info_t *info,
                        librbd::RBD::AioCompletion *aio_comp) {
    image.aio_mirror_image_get_info(info, sizeof(librbd::mirror_image_info_t),
                                    aio_comp);
  }

  virtual bool skip_action(const librbd::mirror_image_info_t &info) const {
    return false;
  }
  virtual void execute_action(librbd::Image &image,
                              librbd::RBD::AioCompletion *aio_comp) = 0;
  virtual void handle_execute_action(int r) {
    dout(20) << this << " " << __func__ << ": r=" << r << dendl;

    if (r < 0 && r != -ENOENT) {
      std::cerr << "rbd: failed to " << get_action_type() << " image "
                << m_image_name << ": " << cpp_strerror(r) << std::endl;
      m_ret_val = r;
    }

    close_image();
  }

  virtual void finalize_action() {
  }
  virtual std::string get_action_type() const = 0;

private:
  /**
   * @verbatim
   *
   * <start>
   *    |
   *    v
   * OPEN_IMAGE
   *    |
   *    v
   * GET_INFO
   *    |
   *    v
   * EXECUTE_ACTION
   *    |
   *    v
   * CLOSE_IMAGE
   *    |
   *    v
   * FINALIZE_ACTION
   *    |
   *    v
   * <finish>
   *
   * @endverbatim
   */

  librados::IoCtx &m_io_ctx;
  OrderedThrottle &m_throttle;
  const std::string m_image_name;

  librbd::Image m_image;
  Context *m_finalize_ctx = nullptr;

  librbd::mirror_image_info_t m_mirror_image_info;

  int m_ret_val = 0;

  void open_image() {
    dout(20) << this << " " << __func__ << dendl;

    librbd::RBD rbd;
    auto aio_completion = utils::create_aio_completion<
      ImageRequestBase, &ImageRequestBase::handle_open_image>(this);
    rbd.aio_open(m_io_ctx, m_image, m_image_name.c_str(), nullptr,
                 aio_completion);
  }

  void handle_open_image(int r) {
    dout(20) << this << " " << __func__ << ": r=" << r << dendl;

    if (r < 0) {
      std::cerr << "rbd: failed to open image "
                << m_image_name << ": " << cpp_strerror(r) << std::endl;
      m_finalize_ctx->complete(r);
      return;
    }

    get_info();
  }

  void get_info() {
    if (skip_get_info()) {
      execute_action();
      return;
    }
    dout(20) << this << " " << __func__ << dendl;

    auto aio_completion = utils::create_aio_completion<
      ImageRequestBase, &ImageRequestBase::handle_get_info>(this);
    get_info(m_image, &m_mirror_image_info, aio_completion);
  }

  void handle_get_info(int r) {
    dout(20) << this << " " << __func__ << ": r=" << r << dendl;

    if (r == -ENOENT) {
      close_image();
      return;
    } else if (r < 0) {
      std::cerr << "rbd: failed to retrieve mirror image info for "
                << m_image_name << ": " << cpp_strerror(r) << std::endl;
      m_ret_val = r;
      close_image();
      return;
    }

    execute_action();
  }

  void execute_action() {
    if (skip_action(m_mirror_image_info)) {
      close_image();
      return;
    }
    dout(20) << this << " " << __func__ << dendl;

    auto aio_completion = utils::create_aio_completion<
      ImageRequestBase, &ImageRequestBase::handle_execute_action>(this);
    execute_action(m_image, aio_completion);
  }

  void close_image() {
    dout(20) << this << " " << __func__ << dendl;

    auto aio_completion = utils::create_aio_completion<
      ImageRequestBase, &ImageRequestBase::handle_close_image>(this);
    m_image.aio_close(aio_completion);
  }

  void handle_close_image(int r) {
    dout(20) << this << " " << __func__ << ": r=" << r << dendl;

    if (r < 0) {
      std::cerr << "rbd: failed to close image "
                << m_image_name << ": " << cpp_strerror(r) << std::endl;
    }

    m_finalize_ctx->complete(r);
  }

  void handle_finalize(int r) {
    dout(20) << this << " " << __func__ << ": r=" << r << dendl;

    if (r == 0 && m_ret_val < 0) {
      r = m_ret_val;
    }
    if (r >= 0) {
      finalize_action();
    }
    m_throttle.end_op(r);
    delete this;
  }

};

class PromoteImageRequest : public ImageRequestBase {
public:
  PromoteImageRequest(librados::IoCtx &io_ctx, OrderedThrottle &throttle,
                      const std::string &image_name, std::atomic<unsigned> *counter,
                      bool force)
    : ImageRequestBase(io_ctx, throttle, image_name), m_counter(counter),
      m_force(force) {
  }

protected:
  bool skip_action(const librbd::mirror_image_info_t &info) const override {
    return (info.state != RBD_MIRROR_IMAGE_ENABLED || info.primary);
  }

  void execute_action(librbd::Image &image,
                      librbd::RBD::AioCompletion *aio_comp) override {
    image.aio_mirror_image_promote(m_force, aio_comp);
  }

  void handle_execute_action(int r) override {
    if (r >= 0) {
      (*m_counter)++;
    }
    ImageRequestBase::handle_execute_action(r);
  }

  std::string get_action_type() const override {
    return "promote";
  }

private:
  std::atomic<unsigned> *m_counter = nullptr;
  bool m_force;
};

class DemoteImageRequest : public ImageRequestBase {
public:
  DemoteImageRequest(librados::IoCtx &io_ctx, OrderedThrottle &throttle,
                     const std::string &image_name, std::atomic<unsigned> *counter)
    : ImageRequestBase(io_ctx, throttle, image_name), m_counter(counter) {
  }

protected:
  bool skip_action(const librbd::mirror_image_info_t &info) const override {
    return (info.state != RBD_MIRROR_IMAGE_ENABLED || !info.primary);
  }

  void execute_action(librbd::Image &image,
                      librbd::RBD::AioCompletion *aio_comp) override {
    image.aio_mirror_image_demote(aio_comp);
  }
  void handle_execute_action(int r) override {
    if (r >= 0) {
      (*m_counter)++;
    }
    ImageRequestBase::handle_execute_action(r);
  }

  std::string get_action_type() const override {
    return "demote";
  }

private:
  std::atomic<unsigned> *m_counter = nullptr;
};

class StatusImageRequest : public ImageRequestBase {
public:
  StatusImageRequest(
      librados::IoCtx &io_ctx, OrderedThrottle &throttle,
      const std::string &image_name,
      const std::map<std::string, std::string> &instance_ids,
      const MirrorDaemonServiceInfo &daemon_service_info,
      at::Format::Formatter formatter)
    : ImageRequestBase(io_ctx, throttle, image_name),
      m_instance_ids(instance_ids), m_daemon_service_info(daemon_service_info),
      m_formatter(formatter) {
  }

protected:
  bool skip_get_info() const override {
    return true;
  }

  void execute_action(librbd::Image &image,
                      librbd::RBD::AioCompletion *aio_comp) override {
    image.get_id(&m_image_id);
    image.aio_mirror_image_get_status(&m_mirror_image_status,
                                      sizeof(m_mirror_image_status), aio_comp);
  }

  void finalize_action() override {
    if (m_mirror_image_status.info.global_id.empty()) {
      return;
    }

    std::string state = utils::mirror_image_status_state(m_mirror_image_status);
    std::string instance_id = (m_mirror_image_status.up &&
                               m_instance_ids.count(m_image_id)) ?
        m_instance_ids.find(m_image_id)->second : "";
    std::string last_update = (
      m_mirror_image_status.last_update == 0 ?
        "" : utils::timestr(m_mirror_image_status.last_update));

    if (m_formatter != nullptr) {
      m_formatter->open_object_section("image");
      m_formatter->dump_string("name", m_mirror_image_status.name);
      m_formatter->dump_string("global_id",
                               m_mirror_image_status.info.global_id);
      m_formatter->dump_string("state", state);
      m_formatter->dump_string("description",
                               m_mirror_image_status.description);
      m_daemon_service_info.dump(instance_id, m_formatter);
      m_formatter->dump_string("last_update", last_update);
      m_formatter->close_section(); // image
    } else {
      std::cout << "\n" << m_mirror_image_status.name << ":\n"
	        << "  global_id:   "
                << m_mirror_image_status.info.global_id << "\n"
	        << "  state:       " << state << "\n"
	        << "  description: "
                << m_mirror_image_status.description << "\n";
      if (!instance_id.empty()) {
        std::cout << "  service:     "
                  << m_daemon_service_info.get_description(instance_id) << "\n";
      }
      std::cout << "  last_update: " << last_update << std::endl;
    }
  }

  std::string get_action_type() const override {
    return "status";
  }

private:
  const std::map<std::string, std::string> &m_instance_ids;
  const MirrorDaemonServiceInfo &m_daemon_service_info;
  at::Format::Formatter m_formatter;
  std::string m_image_id;
  librbd::mirror_image_status_t m_mirror_image_status;
};

template <typename RequestT>
class ImageRequestAllocator {
public:
  template <class... Args>
  RequestT *operator()(librados::IoCtx &io_ctx, OrderedThrottle &throttle,
                       const std::string &image_name, Args&&... args) {
    return new RequestT(io_ctx, throttle, image_name,
                        std::forward<Args>(args)...);
  }
};

template <typename RequestT>
class ImageRequestGenerator {
public:
  template <class... Args>
  ImageRequestGenerator(librados::IoCtx &io_ctx, Args&&... args)
    : m_io_ctx(io_ctx),
      m_factory(std::bind(ImageRequestAllocator<RequestT>(),
                          std::ref(m_io_ctx), std::ref(m_throttle),
                          std::placeholders::_1, std::forward<Args>(args)...)),
      m_throttle(g_conf().get_val<uint64_t>("rbd_concurrent_management_ops"),
                 true) {
  }

  int execute() {
    // use the alphabetical list of image names for pool-level
    // mirror image operations
    librbd::RBD rbd;
    int r = rbd.list2(m_io_ctx, &m_images);
    if (r < 0 && r != -ENOENT) {
      std::cerr << "rbd: failed to list images within pool" << std::endl;
      return r;
    }

    for (auto &image : m_images) {
      auto request = m_factory(image.name);
      request->send();
    }

    return m_throttle.wait_for_ret();
  }
private:
  typedef std::function<RequestT*(const std::string&)>  Factory;

  librados::IoCtx &m_io_ctx;
  Factory m_factory;

  OrderedThrottle m_throttle;

  std::vector<librbd::image_spec_t> m_images;

};

} // anonymous namespace

void get_peer_bootstrap_create_arguments(po::options_description *positional,
                                         po::options_description *options) {
  at::add_pool_options(positional, options, false);
  options->add_options()
    (SITE_NAME.c_str(), po::value<std::string>(), "local site name");
}

int execute_peer_bootstrap_create(
    const po::variables_map &vm,
    const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  r = validate_mirroring_enabled(io_ctx);
  if (r < 0) {
    return r;
  }

  if (vm.count(SITE_NAME)) {
    r = set_site_name(rados, vm[SITE_NAME].as<std::string>());
    if (r < 0) {
      return r;
    }
  }

  librbd::RBD rbd;
  std::string token;
  r = rbd.mirror_peer_bootstrap_create(io_ctx, &token);
  if (r == -EEXIST) {
    std::cerr << "rbd: mismatch with pre-existing RBD mirroring peer user caps"
              << std::endl;
  } else if (r < 0) {
    std::cerr << "rbd: failed to create mirroring bootstrap token: "
              << cpp_strerror(r) << std::endl;
    return r;
  }

  std::cout << token << std::endl;
  return 0;
}

void get_peer_bootstrap_import_arguments(po::options_description *positional,
                                         po::options_description *options) {
  at::add_pool_options(positional, options, false);
  options->add_options()
    (SITE_NAME.c_str(), po::value<std::string>(), "local site name");
  positional->add_options()
    ("token-path", po::value<std::string>(),
     "bootstrap token file (or '-' for stdin)");
  options->add_options()
    ("token-path", po::value<std::string>(),
     "bootstrap token file (or '-' for stdin)")
    ("direction", po::value<MirrorPeerDirection>(),
     "mirroring direction (rx-only, rx-tx)\n"
     "[default: rx-tx]");
}

int execute_peer_bootstrap_import(
    const po::variables_map &vm,
    const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  std::string token_path;
  if (vm.count("token-path")) {
    token_path = vm["token-path"].as<std::string>();
  } else {
    token_path = utils::get_positional_argument(vm, arg_index++);
  }

  if (token_path.empty()) {
    std::cerr << "rbd: token path was not specified" << std::endl;
    return -EINVAL;
  }

  rbd_mirror_peer_direction_t mirror_peer_direction =
    RBD_MIRROR_PEER_DIRECTION_RX_TX;
  if (vm.count("direction")) {
    mirror_peer_direction = vm["direction"].as<rbd_mirror_peer_direction_t>();
  }

  int fd = STDIN_FILENO;
  if (token_path != "-") {
    fd = open(token_path.c_str(), O_RDONLY);
    if (fd < 0) {
      r = -errno;
      std::cerr << "rbd: error opening " << token_path << std::endl;
      return r;
    }
  }

  char token[1024];
  memset(token, 0, sizeof(token));
  r = safe_read(fd, token, sizeof(token) - 1);
  if (fd != STDIN_FILENO) {
    VOID_TEMP_FAILURE_RETRY(close(fd));
  }

  if (r < 0) {
    std::cerr << "rbd: error reading token file: " << cpp_strerror(r)
              << std::endl;
    return r;
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  if (vm.count(SITE_NAME)) {
    r = set_site_name(rados, vm[SITE_NAME].as<std::string>());
    if (r < 0) {
      return r;
    }
  }

  librbd::RBD rbd;
  r = rbd.mirror_peer_bootstrap_import(io_ctx, mirror_peer_direction, token);
  if (r == -ENOSYS) {
    std::cerr << "rbd: mirroring is not enabled on remote peer" << std::endl;
    return r;
  } else if (r < 0) {
    std::cerr << "rbd: failed to import peer bootstrap token" << std::endl;
    return r;
  }

  return 0;
}

void get_peer_add_arguments(po::options_description *positional,
                            po::options_description *options) {
  at::add_pool_options(positional, options, false);
  positional->add_options()
    ("remote-cluster-spec", "remote cluster spec\n"
     "(example: [<client name>@]<cluster name>)");
  options->add_options()
    ("remote-client-name", po::value<std::string>(), "remote client name")
    ("remote-cluster", po::value<std::string>(), "remote cluster name")
    ("remote-mon-host", po::value<std::string>(), "remote mon host(s)")
    ("remote-key-file", po::value<std::string>(),
     "path to file containing remote key");
}

int execute_peer_add(const po::variables_map &vm,
                     const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  std::string remote_client_name = g_ceph_context->_conf->name.to_str();
  std::string remote_cluster;
  std::map<std::string, std::string> attributes;
  r = get_remote_cluster_spec(
    vm, utils::get_positional_argument(vm, arg_index),
    &remote_client_name, &remote_cluster, &attributes);
  if (r < 0) {
    return r;
  }

  // TODO support namespaces
  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  r = validate_mirroring_enabled(io_ctx);
  if (r < 0) {
    return r;
  }

  // TODO: temporary restriction to prevent adding multiple peers
  // until rbd-mirror daemon can properly handle the scenario
  librbd::RBD rbd;
  std::vector<librbd::mirror_peer_t> mirror_peers;
  r = rbd.mirror_peer_list(io_ctx, &mirror_peers);
  if (r < 0) {
    std::cerr << "rbd: failed to list mirror peers" << std::endl;
    return r;
  }
  if (!mirror_peers.empty()) {
    std::cerr << "rbd: multiple peers are not currently supported" << std::endl;
    return -EINVAL;
  }

  std::string uuid;
  r = rbd.mirror_peer_add(io_ctx, &uuid, remote_cluster, remote_client_name);
  if (r < 0) {
    std::cerr << "rbd: error adding mirror peer" << std::endl;
    return r;
  }

  if (!attributes.empty()) {
    r = set_peer_config_key(io_ctx, uuid, std::move(attributes));
    if (r < 0) {
      return r;
    }
  }

  std::cout << uuid << std::endl;
  return 0;
}

void get_peer_remove_arguments(po::options_description *positional,
                               po::options_description *options) {
  at::add_pool_options(positional, options, false);
  add_uuid_option(positional);
}

int execute_peer_remove(const po::variables_map &vm,
                        const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  std::string uuid;
  r = get_uuid(vm, arg_index, &uuid);
  if (r < 0) {
    return r;
  }

  // TODO support namespaces
  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  r = validate_mirroring_enabled(io_ctx);
  if (r < 0) {
    return r;
  }

  librbd::RBD rbd;
  r = rbd.mirror_peer_remove(io_ctx, uuid);
  if (r < 0) {
    std::cerr << "rbd: error removing mirror peer" << std::endl;
    return r;
  }
  return 0;
}

void get_peer_set_arguments(po::options_description *positional,
                            po::options_description *options) {
  at::add_pool_options(positional, options, false);
  add_uuid_option(positional);
  positional->add_options()
    ("key", "peer parameter [client, cluster, mon-host, key-file]")
    ("value", "new value for specified key");
}

int execute_peer_set(const po::variables_map &vm,
                     const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  std::string uuid;
  r = get_uuid(vm, arg_index++, &uuid);
  if (r < 0) {
    return r;
  }

  std::set<std::string> valid_keys{{"client", "cluster", "mon-host",
                                    "key-file"}};
  std::string key = utils::get_positional_argument(vm, arg_index++);
  if (valid_keys.find(key) == valid_keys.end()) {
    std::cerr << "rbd: must specify ";
    for (auto& valid_key : valid_keys) {
      std::cerr << "'" << valid_key << "'";
      if (&valid_key != &(*valid_keys.rbegin())) {
        std::cerr << ", ";
      }
    }
    std::cerr <<  " key." << std::endl;
    return -EINVAL;
  }

  std::string value = utils::get_positional_argument(vm, arg_index++);
  if (value.empty() && (key == "client" || key == "cluster")) {
    std::cerr << "rbd: must specify new " << key << " value." << std::endl;
  } else if (key == "key-file") {
    key = "key";
    r = read_key_file(value, &value);
    if (r < 0) {
      return r;
    }
  } else if (key == "mon-host") {
    key = "mon_host";
  }

  // TODO support namespaces
  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  r = validate_mirroring_enabled(io_ctx);
  if (r < 0) {
    return r;
  }

  librbd::RBD rbd;
  if (key == "client") {
    r = rbd.mirror_peer_set_client(io_ctx, uuid.c_str(), value.c_str());
  } else if (key == "cluster") {
    r = rbd.mirror_peer_set_cluster(io_ctx, uuid.c_str(), value.c_str());
  } else {
    r = update_peer_config_key(io_ctx, uuid, key, value);
    if (r  == -ENOENT) {
      std::cerr << "rbd: mirror peer " << uuid << " does not exist"
                << std::endl;
    }
  }

  if (r < 0) {
    return r;
  }
  return 0;
}

void get_disable_arguments(po::options_description *positional,
                           po::options_description *options) {
  at::add_pool_options(positional, options, false);
}

void get_enable_arguments(po::options_description *positional,
                          po::options_description *options) {
  at::add_pool_options(positional, options, false);
  positional->add_options()
    ("mode", "mirror mode [image or pool]");
  add_site_name_optional(options);
}

int execute_enable_disable(librados::IoCtx& io_ctx,
                           rbd_mirror_mode_t next_mirror_mode,
                           const std::string &mode, bool ignore_no_update) {
  librbd::RBD rbd;
  rbd_mirror_mode_t current_mirror_mode;
  int r = rbd.mirror_mode_get(io_ctx, &current_mirror_mode);
  if (r < 0) {
    std::cerr << "rbd: failed to retrieve mirror mode: "
              << cpp_strerror(r) << std::endl;
    return r;
  }

  if (current_mirror_mode == next_mirror_mode) {
    if (!ignore_no_update) {
      if (mode == "disabled") {
        std::cout << "rbd: mirroring is already " << mode << std::endl;
      } else {
        std::cout << "rbd: mirroring is already configured for "
                  << mode << " mode" << std::endl;
      }
    }
    return 0;
  } else if (next_mirror_mode == RBD_MIRROR_MODE_IMAGE &&
             current_mirror_mode == RBD_MIRROR_MODE_POOL) {
    std::cout << "note: changing mirroring mode from pool to image"
              << std::endl;
  } else if (next_mirror_mode == RBD_MIRROR_MODE_POOL &&
             current_mirror_mode == RBD_MIRROR_MODE_IMAGE) {
    std::cout << "note: changing mirroring mode from image to pool"
              << std::endl;
  }

  r = rbd.mirror_mode_set(io_ctx, next_mirror_mode);
  if (r < 0) {
    return r;
  }
  return 0;
}

int execute_disable(const po::variables_map &vm,
                    const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;

  // TODO support namespaces
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  return execute_enable_disable(io_ctx, RBD_MIRROR_MODE_DISABLED, "disabled",
                                false);
}

int execute_enable(const po::variables_map &vm,
                   const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  rbd_mirror_mode_t mirror_mode;
  std::string mode = utils::get_positional_argument(vm, arg_index++);
  if (mode == "image") {
    mirror_mode = RBD_MIRROR_MODE_IMAGE;
  } else if (mode == "pool") {
    mirror_mode = RBD_MIRROR_MODE_POOL;
  } else {
    std::cerr << "rbd: must specify 'image' or 'pool' mode." << std::endl;
    return -EINVAL;
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;

  // TODO support namespaces
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  bool updated = false;
  if (vm.count(SITE_NAME)) {
    librbd::RBD rbd;

    auto site_name = vm[SITE_NAME].as<std::string>();
    std::string original_site_name;
    r = rbd.mirror_site_name_get(rados, &original_site_name);
    updated = (r >= 0 && site_name != original_site_name);

    r = set_site_name(rados, site_name);
    if (r < 0) {
      return r;
    }
  }

  return execute_enable_disable(io_ctx, mirror_mode, mode, updated);
}

void get_info_arguments(po::options_description *positional,
                        po::options_description *options) {
  at::add_pool_options(positional, options, false);
  at::add_format_options(options);
  options->add_options()
    (ALL_NAME.c_str(), po::bool_switch(), "list all attributes");
}

int execute_info(const po::variables_map &vm,
                 const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, false, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  at::Format::Formatter formatter;
  r = utils::get_formatter(vm, &formatter);
  if (r < 0) {
    return r;
  }

  // TODO support namespaces
  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  librbd::RBD rbd;
  rbd_mirror_mode_t mirror_mode;
  r = rbd.mirror_mode_get(io_ctx, &mirror_mode);
  if (r < 0) {
    return r;
  }

  std::string site_name;
  r = rbd.mirror_site_name_get(rados, &site_name);
  if (r < 0 && r != -EOPNOTSUPP) {
    return r;
  }

  std::vector<librbd::mirror_peer_t> mirror_peers;
  r = rbd.mirror_peer_list(io_ctx, &mirror_peers);
  if (r < 0) {
    return r;
  }

  std::string mirror_mode_desc;
  switch (mirror_mode) {
  case RBD_MIRROR_MODE_DISABLED:
    mirror_mode_desc = "disabled";
    break;
  case RBD_MIRROR_MODE_IMAGE:
    mirror_mode_desc = "image";
    break;
  case RBD_MIRROR_MODE_POOL:
    mirror_mode_desc = "pool";
    break;
  default:
    mirror_mode_desc = "unknown";
    break;
  }

  if (formatter != nullptr) {
    formatter->open_object_section("mirror");
    formatter->dump_string("mode", mirror_mode_desc);
  } else {
    std::cout << "Mode: " << mirror_mode_desc << std::endl;
  }

  if (mirror_mode != RBD_MIRROR_MODE_DISABLED) {
    if (formatter != nullptr) {
      formatter->dump_string("site_name", site_name);
    } else {
      std::cout << "Site Name: " << site_name << std::endl;
    }

    r = format_mirror_peers(io_ctx, formatter, mirror_peers,
                            vm[ALL_NAME].as<bool>());
    if (r < 0) {
      return r;
    }
  }
  if (formatter != nullptr) {
    formatter->close_section();
    formatter->flush(std::cout);
  }
  return 0;
}

void get_status_arguments(po::options_description *positional,
			  po::options_description *options) {
  at::add_pool_options(positional, options, false);
  at::add_format_options(options);
  at::add_verbose_option(options);
}

int execute_status(const po::variables_map &vm,
                   const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, false, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  at::Format::Formatter formatter;
  r = utils::get_formatter(vm, &formatter);
  if (r < 0) {
    return r;
  }

  bool verbose = vm[at::VERBOSE].as<bool>();

  // TODO support namespaces
  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  r = validate_mirroring_enabled(io_ctx);
  if (r < 0) {
    return r;
  }

  librbd::RBD rbd;

  std::map<librbd::mirror_image_status_state_t, int> states;
  r = rbd.mirror_image_status_summary(io_ctx, &states);
  if (r < 0) {
    std::cerr << "rbd: failed to get status summary for mirrored images: "
	      << cpp_strerror(r) << std::endl;
    return r;
  }

  if (formatter != nullptr) {
    formatter->open_object_section("status");
  }

  enum Health {Ok = 0, Warning = 1, Error = 2} health = Ok;
  const char *names[] = {"OK", "WARNING", "ERROR"};
  int total = 0;

  for (auto &it : states) {
    auto &state = it.first;
    if (health < Warning &&
	(state != MIRROR_IMAGE_STATUS_STATE_REPLAYING &&
	 state != MIRROR_IMAGE_STATUS_STATE_STOPPED)) {
      health = Warning;
    }
    if (health < Error &&
	state == MIRROR_IMAGE_STATUS_STATE_ERROR) {
      health = Error;
    }
    total += it.second;
  }

  if (formatter != nullptr) {
    formatter->open_object_section("summary");
    formatter->dump_string("health", names[health]);
    formatter->open_object_section("states");
    for (auto &it : states) {
      std::string state_name = utils::mirror_image_status_state(it.first);
      formatter->dump_int(state_name.c_str(), it.second);
    }
    formatter->close_section(); // states
    formatter->close_section(); // summary
  } else {
    std::cout << "health: " << names[health] << std::endl;
    std::cout << "images: " << total << " total" << std::endl;
    for (auto &it : states) {
      std::cout << "    " << it.second << " "
		<< utils::mirror_image_status_state(it.first) << std::endl;
    }
  }

  int ret = 0;

  if (verbose) {
    if (formatter != nullptr) {
      formatter->open_array_section("images");
    }

    std::map<std::string, std::string> instance_ids;
    MirrorDaemonServiceInfo daemon_service_info(io_ctx);

    std::string start_image_id;
    while (true) {
      std::map<std::string, std::string> ids;
      r = rbd.mirror_image_instance_id_list(io_ctx, start_image_id, 1024, &ids);
      if (r < 0) {
        if (r == -EOPNOTSUPP) {
          std::cerr << "rbd: newer release of Ceph OSDs required to map image "
                    << "to rbd-mirror daemon instance" << std::endl;
        } else {
          std::cerr << "rbd: failed to get instance id list: "
                    << cpp_strerror(r) << std::endl;
        }
        // not fatal
        break;
      }
      if (ids.empty()) {
        break;
      }
      instance_ids.insert(ids.begin(), ids.end());
      start_image_id = ids.rbegin()->first;
    }

    if (!instance_ids.empty()) {
      daemon_service_info.init();
    }

    ImageRequestGenerator<StatusImageRequest> generator(
        io_ctx, instance_ids, daemon_service_info, formatter);
    ret = generator.execute();

    if (formatter != nullptr) {
      formatter->close_section(); // images
    }
  }

  if (formatter != nullptr) {
    formatter->close_section(); // status
    formatter->flush(std::cout);
  }

  return ret;
}

void get_promote_arguments(po::options_description *positional,
			   po::options_description *options) {
  options->add_options()
    ("force", po::bool_switch(),
     "promote even if not cleanly demoted by remote cluster");
  at::add_pool_options(positional, options, false);
}

int execute_promote(const po::variables_map &vm,
                    const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  // TODO support namespaces
  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  r = validate_mirroring_enabled(io_ctx);
  if (r < 0) {
    return r;
  }

  utils::disable_cache();

  std::atomic<unsigned> counter = { 0 };
  ImageRequestGenerator<PromoteImageRequest> generator(io_ctx, &counter,
                                                       vm["force"].as<bool>());
  r = generator.execute();

  std::cout << "Promoted " << counter.load() << " mirrored images" << std::endl;
  return r;
}

void get_demote_arguments(po::options_description *positional,
			   po::options_description *options) {
  at::add_pool_options(positional, options, false);
}

int execute_demote(const po::variables_map &vm,
                   const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, true, true, &pool_name,
                                              nullptr, &arg_index);
  if (r < 0) {
    return r;
  }

  // TODO support namespaces
  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, "", &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  r = validate_mirroring_enabled(io_ctx);
  if (r < 0) {
    return r;
  }

  utils::disable_cache();

  std::atomic<unsigned> counter { 0 };
  ImageRequestGenerator<DemoteImageRequest> generator(io_ctx, &counter);
  r = generator.execute();

  std::cout << "Demoted " << counter.load() << " mirrored images" << std::endl;
  return r;
}

Shell::Action action_bootstrap_create(
  {"mirror", "pool", "peer", "bootstrap", "create"}, {},
  "Create a peer bootstrap token to import in a remote cluster", "",
  &get_peer_bootstrap_create_arguments, &execute_peer_bootstrap_create);
Shell::Action action_bootstreap_import(
  {"mirror", "pool", "peer", "bootstrap", "import"}, {},
  "Import a peer bootstrap token created from a remote cluster", "",
  &get_peer_bootstrap_import_arguments, &execute_peer_bootstrap_import);

Shell::Action action_add(
  {"mirror", "pool", "peer", "add"}, {},
  "Add a mirroring peer to a pool.", "",
  &get_peer_add_arguments, &execute_peer_add);
Shell::Action action_remove(
  {"mirror", "pool", "peer", "remove"}, {},
  "Remove a mirroring peer from a pool.", "",
  &get_peer_remove_arguments, &execute_peer_remove);
Shell::Action action_set(
  {"mirror", "pool", "peer", "set"}, {},
  "Update mirroring peer settings.", "",
  &get_peer_set_arguments, &execute_peer_set);

Shell::Action action_disable(
  {"mirror", "pool", "disable"}, {},
  "Disable RBD mirroring by default within a pool.", "",
  &get_disable_arguments, &execute_disable);
Shell::Action action_enable(
  {"mirror", "pool", "enable"}, {},
  "Enable RBD mirroring by default within a pool.", "",
  &get_enable_arguments, &execute_enable);
Shell::Action action_info(
  {"mirror", "pool", "info"}, {},
  "Show information about the pool mirroring configuration.", {},
  &get_info_arguments, &execute_info);
Shell::Action action_status(
  {"mirror", "pool", "status"}, {},
  "Show status for all mirrored images in the pool.", {},
  &get_status_arguments, &execute_status);
Shell::Action action_promote(
  {"mirror", "pool", "promote"}, {},
  "Promote all non-primary images in the pool.", {},
  &get_promote_arguments, &execute_promote);
Shell::Action action_demote(
  {"mirror", "pool", "demote"}, {},
  "Demote all primary images in the pool.", {},
  &get_demote_arguments, &execute_demote);

} // namespace mirror_pool
} // namespace action
} // namespace rbd