-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbd.cc
2757 lines (2447 loc) · 72.8 KB
/
rbd.cc
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2012 Sage Weil <[email protected]> and others
*
* LGPL2. See file COPYING.
*
*/
#include "include/int_types.h"
#include "mon/MonClient.h"
#include "common/config.h"
#include "common/errno.h"
#include "common/ceph_argparse.h"
#include "common/strtol.h"
#include "global/global_init.h"
#include "common/safe_io.h"
#include "include/krbd.h"
#include "include/stringify.h"
#include "include/rados/librados.hpp"
#include "include/rbd/librbd.hpp"
#include "include/byteorder.h"
#include "include/intarith.h"
#include "include/compat.h"
#include "common/blkdev.h"
#include <boost/scoped_ptr.hpp>
#include <errno.h>
#include <iostream>
#include <memory>
#include <sstream>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include "include/memory.h"
#include <sys/ioctl.h>
#include "include/rbd_types.h"
#include "common/TextTable.h"
#include "include/util.h"
#include "common/Formatter.h"
#if defined(__linux__)
#include <linux/fs.h>
#endif
#if defined(__FreeBSD__)
#include <sys/param.h>
#endif
#define MAX_SECRET_LEN 1000
#define MAX_POOL_NAME_SIZE 128
#define RBD_DIFF_BANNER "rbd diff v1\n"
static string dir_oid = RBD_DIRECTORY;
static string dir_info_oid = RBD_INFO;
bool progress = true;
bool resize_allow_shrink = false;
map<string, string> map_options; // -o / --options map
#define dout_subsys ceph_subsys_rbd
void usage()
{
cout <<
"usage: rbd [-n <auth user>] [OPTIONS] <cmd> ...\n"
"where 'pool' is a rados pool name (default is 'rbd') and 'cmd' is one of:\n"
" (ls | list) [-l | --long ] [pool-name] list rbd images\n"
" (-l includes snapshots/clones)\n"
" info <image-name> show information about image size,\n"
" striping, etc.\n"
" create [--order <bits>] --size <MB> <name> create an empty image\n"
" clone [--order <bits>] <parentsnap> <clonename>\n"
" clone a snapshot into a COW\n"
" child image\n"
" children <snap-name> display children of snapshot\n"
" flatten <image-name> fill clone with parent data\n"
" (make it independent)\n"
" resize --size <MB> <image-name> resize (expand or contract) image\n"
" rm <image-name> delete an image\n"
" export <image-name> <path> export image to file\n"
" \"-\" for stdout\n"
" import <path> <image-name> import image from file\n"
" (dest defaults\n"
" as the filename part of file)\n"
" \"-\" for stdin\n"
" diff <image-name> [--from-snap <snap-name>] print extents that differ since\n"
" a previous snap, or image creation\n"
" export-diff <image-name> [--from-snap <snap-name>] <path>\n"
" export an incremental diff to\n"
" path, or \"-\" for stdout\n"
" import-diff <path> <image-name> import an incremental diff from\n"
" path or \"-\" for stdin\n"
" (cp | copy) <src> <dest> copy src image to dest\n"
" (mv | rename) <src> <dest> rename src image to dest\n"
" snap ls <image-name> dump list of image snapshots\n"
" snap create <snap-name> create a snapshot\n"
" snap rollback <snap-name> rollback image to snapshot\n"
" snap rm <snap-name> deletes a snapshot\n"
" snap purge <image-name> deletes all snapshots\n"
" snap protect <snap-name> prevent a snapshot from being deleted\n"
" snap unprotect <snap-name> allow a snapshot to be deleted\n"
" watch <image-name> watch events on image\n"
" map <image-name> map image to a block device\n"
" using the kernel\n"
" unmap <device> unmap a rbd device that was\n"
" mapped by the kernel\n"
" showmapped show the rbd images mapped\n"
" by the kernel\n"
" lock list <image-name> show locks held on an image\n"
" lock add <image-name> <id> [--shared <tag>] take a lock called id on an image\n"
" lock remove <image-name> <id> <locker> release a lock on an image\n"
" bench-write <image-name> simple write benchmark\n"
" --io-size <bytes> write size\n"
" --io-threads <num> ios in flight\n"
" --io-total <bytes> total bytes to write\n"
" --io-pattern <seq|rand> write pattern\n"
"\n"
"<image-name>, <snap-name> are [pool/]name[@snap], or you may specify\n"
"individual pieces of names with -p/--pool, --image, and/or --snap.\n"
"\n"
"Other input options:\n"
" -p, --pool <pool> source pool name\n"
" --image <image-name> image name\n"
" --dest <image-name> destination [pool and] image name\n"
" --snap <snap-name> snapshot name\n"
" --dest-pool <name> destination pool name\n"
" --path <path-name> path name for import/export\n"
" --size <size in MB> size of image for create and resize\n"
" --order <bits> the object size in bits; object size will be\n"
" (1 << order) bytes. Default is 22 (4 MB).\n"
" --image-format <format-number> format to use when creating an image\n"
" format 1 is the original format (default)\n"
" format 2 supports cloning\n"
" --id <username> rados user (without 'client.'prefix) to\n"
" authenticate as\n"
" --keyfile <path> file containing secret key for use with cephx\n"
" --shared <tag> take a shared (rather than exclusive) lock\n"
" --format <output-format> output format (default: plain, json, xml)\n"
" --pretty-format make json or xml output more readable\n"
" --no-progress do not show progress for long-running commands\n"
" -o, --options <map-options> options to use when mapping an image\n"
" --read-only set device readonly when mapping image\n"
" --allow-shrink allow shrinking of an image when resizing\n";
}
static string feature_str(uint64_t feature)
{
switch (feature) {
case RBD_FEATURE_LAYERING:
return "layering";
case RBD_FEATURE_STRIPINGV2:
return "striping";
default:
return "";
}
}
static string features_str(uint64_t features)
{
string s = "";
for (uint64_t feature = 1; feature <= RBD_FEATURE_STRIPINGV2;
feature <<= 1) {
if (feature & features) {
if (s.size())
s += ", ";
s += feature_str(feature);
}
}
return s;
}
static void format_features(Formatter *f, uint64_t features)
{
f->open_array_section("features");
for (uint64_t feature = 1; feature <= RBD_FEATURE_STRIPINGV2;
feature <<= 1) {
f->dump_string("feature", feature_str(feature));
}
f->close_section();
}
struct MyProgressContext : public librbd::ProgressContext {
const char *operation;
int last_pc;
MyProgressContext(const char *o) : operation(o), last_pc(0) {
}
int update_progress(uint64_t offset, uint64_t total) {
if (progress) {
int pc = total ? (offset * 100ull / total) : 0;
if (pc != last_pc) {
cerr << "\r" << operation << ": "
// << offset << " / " << total << " "
<< pc << "% complete...";
cerr.flush();
last_pc = pc;
}
}
return 0;
}
void finish() {
if (progress) {
cerr << "\r" << operation << ": 100% complete...done." << std::endl;
}
}
void fail() {
if (progress) {
cerr << "\r" << operation << ": " << last_pc << "% complete...failed."
<< std::endl;
}
}
};
static int get_outfmt(const char *output_format,
bool pretty,
boost::scoped_ptr<Formatter> *f)
{
if (!strcmp(output_format, "json")) {
f->reset(new JSONFormatter(pretty));
} else if (!strcmp(output_format, "xml")) {
f->reset(new XMLFormatter(pretty));
} else if (strcmp(output_format, "plain")) {
cerr << "rbd: unknown format '" << output_format << "'" << std::endl;
return -EINVAL;
}
return 0;
}
static int do_list(librbd::RBD &rbd, librados::IoCtx& io_ctx, bool lflag,
Formatter *f)
{
std::vector<string> names;
int r = rbd.list(io_ctx, names);
if (r == -ENOENT)
r = 0;
if (r < 0)
return r;
if (!lflag) {
if (f)
f->open_array_section("images");
for (std::vector<string>::const_iterator i = names.begin();
i != names.end(); ++i) {
if (f)
f->dump_string("name", *i);
else
cout << *i << std::endl;
}
if (f) {
f->close_section();
f->flush(cout);
}
return 0;
}
TextTable tbl;
if (f) {
f->open_array_section("images");
} else {
tbl.define_column("NAME", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("SIZE", TextTable::RIGHT, TextTable::RIGHT);
tbl.define_column("PARENT", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("FMT", TextTable::RIGHT, TextTable::RIGHT);
tbl.define_column("PROT", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("LOCK", TextTable::LEFT, TextTable::LEFT);
}
string pool, image, snap, parent;
for (std::vector<string>::const_iterator i = names.begin();
i != names.end(); ++i) {
librbd::image_info_t info;
librbd::Image im;
r = rbd.open_read_only(io_ctx, im, i->c_str(), NULL);
// image might disappear between rbd.list() and rbd.open(); ignore
// that, warn about other possible errors (EPERM, say, for opening
// an old-format image, because you need execute permission for the
// class method)
if (r < 0) {
if (r != -ENOENT) {
cerr << "rbd: error opening " << *i << ": " << cpp_strerror(r)
<< std::endl;
}
// in any event, continue to next image
continue;
}
// handle second-nth trips through loop
parent.clear();
r = im.parent_info(&pool, &image, &snap);
if (r < 0 && r != -ENOENT)
return r;
bool has_parent = false;
if (r != -ENOENT) {
parent = pool + "/" + image + "@" + snap;
has_parent = true;
}
if (im.stat(info, sizeof(info)) < 0)
return -EINVAL;
uint8_t old_format;
im.old_format(&old_format);
list<librbd::locker_t> lockers;
bool exclusive;
r = im.list_lockers(&lockers, &exclusive, NULL);
if (r < 0)
return r;
string lockstr;
if (!lockers.empty()) {
lockstr = (exclusive) ? "excl" : "shr";
}
if (f) {
f->open_object_section("image");
f->dump_string("image", *i);
f->dump_unsigned("size", info.size);
if (has_parent) {
f->open_object_section("parent");
f->dump_string("pool", pool);
f->dump_string("image", image);
f->dump_string("snapshot", snap);
f->close_section();
}
f->dump_int("format", old_format ? 1 : 2);
if (!lockers.empty())
f->dump_string("lock_type", exclusive ? "exclusive" : "shared");
f->close_section();
} else {
tbl << *i
<< stringify(si_t(info.size))
<< parent
<< ((old_format) ? '1' : '2')
<< "" // protect doesn't apply to images
<< lockstr
<< TextTable::endrow;
}
vector<librbd::snap_info_t> snaplist;
if (im.snap_list(snaplist) >= 0 && !snaplist.empty()) {
for (std::vector<librbd::snap_info_t>::iterator s = snaplist.begin();
s != snaplist.end(); ++s) {
bool is_protected;
bool has_parent = false;
parent.clear();
im.snap_set(s->name.c_str());
r = im.snap_is_protected(s->name.c_str(), &is_protected);
if (r < 0)
return r;
if (im.parent_info(&pool, &image, &snap) >= 0) {
parent = pool + "/" + image + "@" + snap;
has_parent = true;
}
if (f) {
f->open_object_section("snapshot");
f->dump_string("image", *i);
f->dump_string("snapshot", s->name);
f->dump_unsigned("size", s->size);
if (has_parent) {
f->open_object_section("parent");
f->dump_string("pool", pool);
f->dump_string("image", image);
f->dump_string("snapshot", snap);
f->close_section();
}
f->dump_int("format", old_format ? 1 : 2);
f->dump_string("protected", is_protected ? "true" : "false");
f->close_section();
} else {
tbl << *i + "@" + s->name
<< stringify(si_t(s->size))
<< parent
<< ((old_format) ? '1' : '2')
<< (is_protected ? "yes" : "")
<< "" // locks don't apply to snaps
<< TextTable::endrow;
}
}
}
}
if (f) {
f->close_section();
f->flush(cout);
} else if (!names.empty()) {
cout << tbl;
}
return 0;
}
static int do_create(librbd::RBD &rbd, librados::IoCtx& io_ctx,
const char *imgname, uint64_t size, int *order,
int format, uint64_t features,
uint64_t stripe_unit, uint64_t stripe_count)
{
int r;
if (format == 1) {
// weird striping not allowed with format 1!
if ((stripe_unit || stripe_count) &&
(stripe_unit != (1ull << *order) && stripe_count != 1)) {
cerr << "non-default striping not allowed with format 1; use --format 2"
<< std::endl;
return -EINVAL;
}
r = rbd.create(io_ctx, imgname, size, order);
} else {
if (features == 0) {
features = RBD_FEATURE_LAYERING;
}
if ((stripe_unit || stripe_count) &&
(stripe_unit != (1ull << *order) && stripe_count != 1)) {
features |= RBD_FEATURE_STRIPINGV2;
}
r = rbd.create3(io_ctx, imgname, size, features, order,
stripe_unit, stripe_count);
}
if (r < 0)
return r;
return 0;
}
static int do_clone(librbd::RBD &rbd, librados::IoCtx &p_ioctx,
const char *p_name, const char *p_snapname,
librados::IoCtx &c_ioctx, const char *c_name,
uint64_t features, int *c_order)
{
if (features == 0)
features = RBD_FEATURES_ALL;
else if ((features & RBD_FEATURE_LAYERING) != RBD_FEATURE_LAYERING)
return -EINVAL;
return rbd.clone(p_ioctx, p_name, p_snapname, c_ioctx, c_name, features,
c_order);
}
static int do_flatten(librbd::Image& image)
{
MyProgressContext pc("Image flatten");
int r = image.flatten_with_progress(pc);
if (r < 0) {
pc.fail();
return r;
}
pc.finish();
return 0;
}
static int do_rename(librbd::RBD &rbd, librados::IoCtx& io_ctx,
const char *imgname, const char *destname)
{
int r = rbd.rename(io_ctx, imgname, destname);
if (r < 0)
return r;
return 0;
}
static int do_show_info(const char *imgname, librbd::Image& image,
const char *snapname, Formatter *f)
{
librbd::image_info_t info;
string parent_pool, parent_name, parent_snapname;
uint8_t old_format;
uint64_t overlap, features;
bool snap_protected = false;
int r;
r = image.stat(info, sizeof(info));
if (r < 0)
return r;
r = image.old_format(&old_format);
if (r < 0)
return r;
r = image.overlap(&overlap);
if (r < 0)
return r;
r = image.features(&features);
if (r < 0)
return r;
if (snapname) {
r = image.snap_is_protected(snapname, &snap_protected);
if (r < 0)
return r;
}
char prefix[RBD_MAX_BLOCK_NAME_SIZE + 1];
strncpy(prefix, info.block_name_prefix, RBD_MAX_BLOCK_NAME_SIZE);
prefix[RBD_MAX_BLOCK_NAME_SIZE] = '\0';
if (f) {
f->open_object_section("image");
f->dump_string("name", imgname);
f->dump_unsigned("size", info.size);
f->dump_unsigned("objects", info.num_objs);
f->dump_int("order", info.order);
f->dump_unsigned("object_size", info.obj_size);
f->dump_string("block_name_prefix", prefix);
f->dump_int("format", (old_format ? 1 : 2));
} else {
cout << "rbd image '" << imgname << "':\n"
<< "\tsize " << prettybyte_t(info.size) << " in "
<< info.num_objs << " objects"
<< std::endl
<< "\torder " << info.order
<< " (" << prettybyte_t(info.obj_size) << " objects)"
<< std::endl
<< "\tblock_name_prefix: " << prefix
<< std::endl
<< "\tformat: " << (old_format ? "1" : "2")
<< std::endl;
}
if (!old_format) {
if (f)
format_features(f, features);
else
cout << "\tfeatures: " << features_str(features) << std::endl;
}
// snapshot info, if present
if (snapname) {
if (f) {
f->dump_string("protected", snap_protected ? "true" : "false");
} else {
cout << "\tprotected: " << (snap_protected ? "True" : "False")
<< std::endl;
}
}
// parent info, if present
if ((image.parent_info(&parent_pool, &parent_name, &parent_snapname) == 0) &&
parent_name.length() > 0) {
if (f) {
f->open_object_section("parent");
f->dump_string("pool", parent_pool);
f->dump_string("image", parent_name);
f->dump_string("snapshot", parent_snapname);
f->dump_unsigned("overlap", overlap);
f->close_section();
} else {
cout << "\tparent: " << parent_pool << "/" << parent_name
<< "@" << parent_snapname << std::endl;
cout << "\toverlap: " << prettybyte_t(overlap) << std::endl;
}
}
// striping info, if feature is set
if (features & RBD_FEATURE_STRIPINGV2) {
if (f) {
f->dump_unsigned("stripe_unit", image.get_stripe_unit());
f->dump_unsigned("stripe_count", image.get_stripe_count());
} else {
cout << "\tstripe unit: " << prettybyte_t(image.get_stripe_unit())
<< std::endl
<< "\tstripe count: " << image.get_stripe_count() << std::endl;
}
}
if (f) {
f->close_section();
f->flush(cout);
}
return 0;
}
static int do_delete(librbd::RBD &rbd, librados::IoCtx& io_ctx,
const char *imgname)
{
MyProgressContext pc("Removing image");
int r = rbd.remove_with_progress(io_ctx, imgname, pc);
if (r < 0) {
pc.fail();
return r;
}
pc.finish();
return 0;
}
static int do_resize(librbd::Image& image, uint64_t size)
{
MyProgressContext pc("Resizing image");
int r = image.resize_with_progress(size, pc);
if (r < 0) {
pc.fail();
return r;
}
pc.finish();
return 0;
}
static int do_list_snaps(librbd::Image& image, Formatter *f)
{
std::vector<librbd::snap_info_t> snaps;
TextTable t;
int r;
r = image.snap_list(snaps);
if (r < 0)
return r;
if (f) {
f->open_array_section("snapshots");
} else {
t.define_column("SNAPID", TextTable::RIGHT, TextTable::RIGHT);
t.define_column("NAME", TextTable::LEFT, TextTable::LEFT);
t.define_column("SIZE", TextTable::RIGHT, TextTable::RIGHT);
}
for (std::vector<librbd::snap_info_t>::iterator s = snaps.begin();
s != snaps.end(); ++s) {
if (f) {
f->open_object_section("snapshot");
f->dump_unsigned("id", s->id);
f->dump_string("name", s->name);
f->dump_unsigned("size", s->size);
f->close_section();
} else {
t << s->id << s->name << stringify(prettybyte_t(s->size))
<< TextTable::endrow;
}
}
if (f) {
f->close_section();
f->flush(cout);
} else if (snaps.size()) {
cout << t;
}
return 0;
}
static int do_add_snap(librbd::Image& image, const char *snapname)
{
int r = image.snap_create(snapname);
if (r < 0)
return r;
return 0;
}
static int do_remove_snap(librbd::Image& image, const char *snapname)
{
int r = image.snap_remove(snapname);
if (r < 0)
return r;
return 0;
}
static int do_rollback_snap(librbd::Image& image, const char *snapname)
{
MyProgressContext pc("Rolling back to snapshot");
int r = image.snap_rollback_with_progress(snapname, pc);
if (r < 0) {
pc.fail();
return r;
}
pc.finish();
return 0;
}
static int do_purge_snaps(librbd::Image& image)
{
MyProgressContext pc("Removing all snapshots");
std::vector<librbd::snap_info_t> snaps;
int r = image.snap_list(snaps);
if (r < 0) {
pc.fail();
return r;
}
for (size_t i = 0; i < snaps.size(); ++i) {
r = image.snap_remove(snaps[i].name.c_str());
if (r < 0) {
pc.fail();
return r;
}
pc.update_progress(i + 1, snaps.size());
}
pc.finish();
return 0;
}
static int do_protect_snap(librbd::Image& image, const char *snapname)
{
int r = image.snap_protect(snapname);
if (r < 0)
return r;
return 0;
}
static int do_unprotect_snap(librbd::Image& image, const char *snapname)
{
int r = image.snap_unprotect(snapname);
if (r < 0)
return r;
return 0;
}
static int do_list_children(librbd::Image &image, Formatter *f)
{
set<pair<string, string> > children;
int r;
r = image.list_children(&children);
if (r < 0)
return r;
if (f)
f->open_array_section("children");
for (set<pair<string, string> >::const_iterator child_it = children.begin();
child_it != children.end(); child_it++) {
if (f) {
f->open_object_section("child");
f->dump_string("pool", child_it->first);
f->dump_string("image", child_it->second);
f->close_section();
} else {
cout << child_it->first << "/" << child_it->second << std::endl;
}
}
if (f) {
f->close_section();
f->flush(cout);
}
return 0;
}
static int do_lock_list(librbd::Image& image, Formatter *f)
{
list<librbd::locker_t> lockers;
bool exclusive;
string tag;
TextTable tbl;
int r;
r = image.list_lockers(&lockers, &exclusive, &tag);
if (r < 0)
return r;
if (f) {
f->open_object_section("locks");
} else {
tbl.define_column("Locker", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("ID", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("Address", TextTable::LEFT, TextTable::LEFT);
}
if (lockers.size()) {
bool one = (lockers.size() == 1);
if (!f) {
cout << "There " << (one ? "is " : "are ") << lockers.size()
<< (exclusive ? " exclusive" : " shared")
<< " lock" << (one ? "" : "s") << " on this image.\n";
if (!exclusive)
cout << "Lock tag: " << tag << "\n";
}
for (list<librbd::locker_t>::const_iterator it = lockers.begin();
it != lockers.end(); ++it) {
if (f) {
f->open_object_section(it->cookie.c_str());
f->dump_string("locker", it->client);
f->dump_string("address", it->address);
f->close_section();
} else {
tbl << it->client << it->cookie << it->address << TextTable::endrow;
}
}
if (!f)
cout << tbl;
}
if (f) {
f->close_section();
f->flush(cout);
}
return 0;
}
static int do_lock_add(librbd::Image& image, const char *cookie,
const char *tag)
{
if (tag)
return image.lock_shared(cookie, tag);
else
return image.lock_exclusive(cookie);
}
static int do_lock_remove(librbd::Image& image, const char *client,
const char *cookie)
{
return image.break_lock(client, cookie);
}
static void rbd_bencher_completion(void *c, void *pc);
struct rbd_bencher;
struct rbd_bencher {
librbd::Image *image;
Mutex lock;
Cond cond;
int in_flight;
rbd_bencher(librbd::Image *i)
: image(i),
lock("rbd_bencher::lock"),
in_flight(0)
{ }
bool start_write(int max, uint64_t off, uint64_t len, bufferlist& bl)
{
{
Mutex::Locker l(lock);
if (in_flight >= max)
return false;
in_flight++;
}
librbd::RBD::AioCompletion *c =
new librbd::RBD::AioCompletion((void *)this, rbd_bencher_completion);
image->aio_write(off, len, bl, c);
//cout << "start " << c << " at " << off << "~" << len << std::endl;
return true;
}
void wait_for(int max) {
Mutex::Locker l(lock);
while (in_flight > max) {
utime_t dur;
dur.set_from_double(.2);
cond.WaitInterval(g_ceph_context, lock, dur);
}
}
};
void rbd_bencher_completion(void *vc, void *pc)
{
librbd::RBD::AioCompletion *c = (librbd::RBD::AioCompletion *)vc;
rbd_bencher *b = static_cast<rbd_bencher *>(pc);
//cout << "complete " << c << std::endl;
int ret = c->get_return_value();
if (ret != 0) {
cout << "write error: " << cpp_strerror(ret) << std::endl;
assert(0 == ret);
}
b->lock.Lock();
b->in_flight--;
b->cond.Signal();
b->lock.Unlock();
c->release();
}
static int do_bench_write(librbd::Image& image, uint64_t io_size,
uint64_t io_threads, uint64_t io_bytes,
string pattern)
{
rbd_bencher b(&image);
cout << "bench-write "
<< " io_size " << io_size
<< " io_threads " << io_threads
<< " bytes " << io_bytes
<< " pattern " << pattern
<< std::endl;
if (pattern != "rand" && pattern != "seq")
return -EINVAL;
srand(time(NULL) % (unsigned long) -1);
bufferptr bp(io_size);
memset(bp.c_str(), rand() & 0xff, io_size);
bufferlist bl;
bl.push_back(bp);
utime_t start = ceph_clock_now(NULL);
utime_t last;
unsigned ios = 0;
uint64_t size = 0;
image.size(&size);
vector<uint64_t> thread_offset;
uint64_t i;
uint64_t start_pos;
// disturb all thread's offset, used by seq write
for (i = 0; i < io_threads; i++) {
start_pos = (rand() % (size / io_size)) * io_size;
thread_offset.push_back(start_pos);
}
printf(" SEC OPS OPS/SEC BYTES/SEC\n");
uint64_t off;
for (off = 0; off < io_bytes; off += io_size) {
b.wait_for(io_threads - 1);
i = 0;
while (i < io_threads && off < io_bytes &&
b.start_write(io_threads, thread_offset[i], io_size, bl)) {
++i;
++ios;
off += io_size;
if (pattern == "rand") {
thread_offset[i] = (rand() % (size / io_size)) * io_size;
} else {
thread_offset[i] += io_size;
if (thread_offset[i] + io_size > size)
thread_offset[i] = 0;
}
}
utime_t now = ceph_clock_now(NULL);
utime_t elapsed = now - start;
if (elapsed.sec() != last.sec()) {
printf("%5d %8d %8.2lf %8.2lf\n",
(int)elapsed,
(int)(ios - io_threads),
(double)(ios - io_threads) / elapsed,
(double)(off - io_threads * io_size) / elapsed);
last = elapsed;
}
}
b.wait_for(0);
int r = image.flush();
if (r < 0) {
cerr << "Error flushing data at the end: " << cpp_strerror(r) << std::endl;
}
utime_t now = ceph_clock_now(NULL);
double elapsed = now - start;
printf("elapsed: %5d ops: %8d ops/sec: %8.2lf bytes/sec: %8.2lf\n",
(int)elapsed, ios, (double)ios / elapsed, (double)off / elapsed);
return 0;
}
struct ExportContext {
librbd::Image *image;
int fd;
uint64_t totalsize;
MyProgressContext pc;
ExportContext(librbd::Image *i, int f, uint64_t t) :
image(i),
fd(f),
totalsize(t),
pc("Exporting image")
{}
};
static int export_read_cb(uint64_t ofs, size_t len, const char *buf, void *arg)
{
ssize_t ret;
ExportContext *ec = static_cast<ExportContext *>(arg);
int fd = ec->fd;
static char *localbuf = NULL;
static size_t maplen = 0;
if (fd == 1) {
if (!buf) {
// can't seek stdout; need actual data to write
if (maplen < len) {
// never mapped, or need to map larger
int r;
if (localbuf != NULL){
if ((r = munmap(localbuf, len)) < 0) {