/
ceph_objectstore_tool.cc
3085 lines (2698 loc) · 80.8 KB
/
ceph_objectstore_tool.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) 2013 Inktank
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/scoped_ptr.hpp>
#include <stdlib.h>
#include "common/Formatter.h"
#include "common/errno.h"
#include "common/ceph_argparse.h"
#include "global/global_init.h"
#include "os/ObjectStore.h"
#include "os/FileStore.h"
#include "osd/PGLog.h"
#include "osd/OSD.h"
#include "json_spirit/json_spirit_value.h"
#include "json_spirit/json_spirit_reader.h"
#include "include/rados/librados.hpp"
namespace po = boost::program_options;
using namespace std;
enum {
TYPE_NONE = 0,
TYPE_PG_BEGIN,
TYPE_PG_END,
TYPE_OBJECT_BEGIN,
TYPE_OBJECT_END,
TYPE_DATA,
TYPE_ATTRS,
TYPE_OMAP_HDR,
TYPE_OMAP,
TYPE_PG_METADATA,
END_OF_TYPES, //Keep at the end
};
//#define INTERNAL_TEST
//#define INTERNAL_TEST2
//#define INTERNAL_TEST3
#ifdef INTERNAL_TEST
CompatSet get_test_compat_set() {
CompatSet::FeatureSet ceph_osd_feature_compat;
CompatSet::FeatureSet ceph_osd_feature_ro_compat;
CompatSet::FeatureSet ceph_osd_feature_incompat;
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_BASE);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_PGINFO);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_OLOC);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEC);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_CATEGORIES);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_HOBJECTPOOL);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_BIGINFO);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBINFO);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBLOG);
#ifdef INTERNAL_TEST2
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_SNAPMAPPER);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_SHARDS);
#endif
return CompatSet(ceph_osd_feature_compat, ceph_osd_feature_ro_compat,
ceph_osd_feature_incompat);
}
#endif
typedef uint8_t sectiontype_t;
typedef uint32_t mymagic_t;
typedef int64_t mysize_t;
const ssize_t max_read = 1024 * 1024;
const uint16_t shortmagic = 0xffce; //goes into stream as "ceff"
//endmagic goes into stream as "ceff ffec"
const mymagic_t endmagic = (0xecff << 16) | shortmagic;
const int fd_none = INT_MIN;
bool outistty;
//The first FIXED_LENGTH bytes are a fixed
//portion of the export output. This includes the overall
//version number, and size of header and footer.
//THIS STRUCTURE CAN ONLY BE APPENDED TO. If it needs to expand,
//the version can be bumped and then anything
//can be added to the export format.
struct super_header {
static const uint32_t super_magic = (shortmagic << 16) | shortmagic;
// ver = 1, Initial version
// ver = 2, Add OSDSuperblock to pg_begin
static const uint32_t super_ver = 2;
static const uint32_t FIXED_LENGTH = 16;
uint32_t magic;
uint32_t version;
uint32_t header_size;
uint32_t footer_size;
super_header() : magic(0), version(0), header_size(0), footer_size(0) { }
int read_super();
void encode(bufferlist& bl) const {
::encode(magic, bl);
::encode(version, bl);
::encode(header_size, bl);
::encode(footer_size, bl);
}
void decode(bufferlist::iterator& bl) {
::decode(magic, bl);
::decode(version, bl);
::decode(header_size, bl);
::decode(footer_size, bl);
}
};
struct header {
sectiontype_t type;
mysize_t size;
header(sectiontype_t type, mysize_t size) :
type(type), size(size) { }
header(): type(0), size(0) { }
int get_header();
void encode(bufferlist& bl) const {
uint32_t debug_type = (type << 24) | (type << 16) | shortmagic;
ENCODE_START(1, 1, bl);
::encode(debug_type, bl);
::encode(size, bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::iterator& bl) {
uint32_t debug_type;
DECODE_START(1, bl);
::decode(debug_type, bl);
type = debug_type >> 24;
::decode(size, bl);
DECODE_FINISH(bl);
}
};
struct footer {
mymagic_t magic;
footer() : magic(endmagic) { }
int get_footer();
void encode(bufferlist& bl) const {
ENCODE_START(1, 1, bl);
::encode(magic, bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::iterator& bl) {
DECODE_START(1, bl);
::decode(magic, bl);
DECODE_FINISH(bl);
}
};
struct pg_begin {
spg_t pgid;
OSDSuperblock superblock;
pg_begin(spg_t pg, const OSDSuperblock& sb):
pgid(pg), superblock(sb) { }
pg_begin() { }
void encode(bufferlist& bl) const {
// If superblock doesn't include CEPH_FS_FEATURE_INCOMPAT_SHARDS then
// shard will be NO_SHARD for a replicated pool. This means
// that we allow the decode by struct_v 2.
ENCODE_START(3, 2, bl);
::encode(pgid.pgid, bl);
::encode(superblock, bl);
::encode(pgid.shard, bl);
ENCODE_FINISH(bl);
}
// NOTE: New super_ver prevents decode from ver 1
void decode(bufferlist::iterator& bl) {
DECODE_START(3, bl);
::decode(pgid.pgid, bl);
if (struct_v > 1) {
::decode(superblock, bl);
}
if (struct_v > 2) {
::decode(pgid.shard, bl);
} else {
pgid.shard = ghobject_t::NO_SHARD;
}
DECODE_FINISH(bl);
}
};
struct object_begin {
ghobject_t hoid;
// Duplicate what is in the OI_ATTR so we have it at the start
// of object processing.
object_info_t oi;
object_begin(const ghobject_t &hoid): hoid(hoid) { }
object_begin() { }
// If superblock doesn't include CEPH_FS_FEATURE_INCOMPAT_SHARDS then
// generation will be NO_GEN, shard_id will be NO_SHARD for a replicated
// pool. This means we will allow the decode by struct_v 1.
void encode(bufferlist& bl) const {
ENCODE_START(3, 1, bl);
::encode(hoid.hobj, bl);
::encode(hoid.generation, bl);
::encode(hoid.shard_id, bl);
::encode(oi, bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::iterator& bl) {
DECODE_START(3, bl);
::decode(hoid.hobj, bl);
if (struct_v > 1) {
::decode(hoid.generation, bl);
::decode(hoid.shard_id, bl);
} else {
hoid.generation = ghobject_t::NO_GEN;
hoid.shard_id = ghobject_t::NO_SHARD;
}
if (struct_v > 2) {
::decode(oi, bl);
}
DECODE_FINISH(bl);
}
};
struct data_section {
uint64_t offset;
uint64_t len;
bufferlist databl;
data_section(uint64_t offset, uint64_t len, bufferlist bl):
offset(offset), len(len), databl(bl) { }
data_section(): offset(0), len(0) { }
void encode(bufferlist& bl) const {
ENCODE_START(1, 1, bl);
::encode(offset, bl);
::encode(len, bl);
::encode(databl, bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::iterator& bl) {
DECODE_START(1, bl);
::decode(offset, bl);
::decode(len, bl);
::decode(databl, bl);
DECODE_FINISH(bl);
}
};
struct attr_section {
map<string,bufferptr> data;
attr_section(const map<string,bufferptr> &data) : data(data) { }
attr_section() { }
void encode(bufferlist& bl) const {
ENCODE_START(1, 1, bl);
::encode(data, bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::iterator& bl) {
DECODE_START(1, bl);
::decode(data, bl);
DECODE_FINISH(bl);
}
};
struct omap_hdr_section {
bufferlist hdr;
omap_hdr_section(bufferlist hdr) : hdr(hdr) { }
omap_hdr_section() { }
void encode(bufferlist& bl) const {
ENCODE_START(1, 1, bl);
::encode(hdr, bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::iterator& bl) {
DECODE_START(1, bl);
::decode(hdr, bl);
DECODE_FINISH(bl);
}
};
struct omap_section {
map<string, bufferlist> omap;
omap_section(const map<string, bufferlist> &omap) :
omap(omap) { }
omap_section() { }
void encode(bufferlist& bl) const {
ENCODE_START(1, 1, bl);
::encode(omap, bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::iterator& bl) {
DECODE_START(1, bl);
::decode(omap, bl);
DECODE_FINISH(bl);
}
};
struct metadata_section {
__u8 struct_ver;
epoch_t map_epoch;
pg_info_t info;
pg_log_t log;
map<epoch_t,pg_interval_t> past_intervals;
OSDMap osdmap;
metadata_section(__u8 struct_ver, epoch_t map_epoch, const pg_info_t &info,
const pg_log_t &log, map<epoch_t,pg_interval_t> &past_intervals)
: struct_ver(struct_ver),
map_epoch(map_epoch),
info(info),
log(log),
past_intervals(past_intervals) { }
metadata_section()
: struct_ver(0),
map_epoch(0) { }
void encode(bufferlist& bl) const {
ENCODE_START(3, 1, bl);
::encode(struct_ver, bl);
::encode(map_epoch, bl);
::encode(info, bl);
::encode(log, bl);
::encode(past_intervals, bl);
osdmap.encode(bl);
ENCODE_FINISH(bl);
}
void decode(bufferlist::iterator& bl) {
DECODE_START(3, bl);
::decode(struct_ver, bl);
::decode(map_epoch, bl);
::decode(info, bl);
::decode(log, bl);
if (struct_v > 1) {
::decode(past_intervals, bl);
} else {
cout << "NOTICE: Older export without past_intervals" << std::endl;
}
if (struct_v > 2) {
osdmap.decode(bl);
} else {
cout << "WARNING: Older export without OSDMap information" << std::endl;
}
DECODE_FINISH(bl);
}
};
struct action_on_object_t {
virtual ~action_on_object_t() {}
virtual int call(ObjectStore *store, coll_t coll, ghobject_t &ghobj, object_info_t &oi) = 0;
};
int _action_on_all_objects_in_pg(ObjectStore *store, coll_t coll, action_on_object_t &action, bool debug)
{
unsigned LIST_AT_A_TIME = 100;
ghobject_t next;
while (!next.is_max()) {
vector<ghobject_t> list;
int r = store->collection_list_partial(
coll,
next,
LIST_AT_A_TIME,
LIST_AT_A_TIME,
0,
&list,
&next);
if (r < 0) {
cerr << "Error listing collection: " << coll << ", "
<< cpp_strerror(r) << std::endl;
return r;
}
for (vector<ghobject_t>::iterator obj = list.begin();
obj != list.end();
++obj) {
bufferlist attr;
r = store->getattr(coll, *obj, OI_ATTR, attr);
if (r < 0) {
cerr << "Error getting attr on : " << make_pair(coll, *obj) << ", "
<< cpp_strerror(r) << std::endl;
return r;
}
object_info_t oi;
bufferlist::iterator bp = attr.begin();
try {
::decode(oi, bp);
} catch (...) {
r = -EINVAL;
cerr << "Error getting attr on : " << make_pair(coll, *obj) << ", "
<< cpp_strerror(r) << std::endl;
return r;
}
r = action.call(store, coll, *obj, oi);
if (r < 0)
return r;
}
}
return 0;
}
int action_on_all_objects_in_pg(ObjectStore *store, coll_t coll, action_on_object_t &action, bool debug)
{
int r = _action_on_all_objects_in_pg(store, coll, action, debug);
store->sync_and_flush();
return r;
}
int _action_on_all_objects(ObjectStore *store, action_on_object_t &action, bool debug)
{
unsigned scanned = 0;
int r = 0;
vector<coll_t> colls_to_check;
vector<coll_t> candidates;
r = store->list_collections(candidates);
if (r < 0) {
cerr << "Error listing collections: " << cpp_strerror(r) << std::endl;
return r;
}
for (vector<coll_t>::iterator i = candidates.begin();
i != candidates.end();
++i) {
spg_t pgid;
snapid_t snap;
if (i->is_pg(pgid, snap)) {
colls_to_check.push_back(*i);
}
}
if (debug)
cerr << colls_to_check.size() << " pgs to scan" << std::endl;
for (vector<coll_t>::iterator i = colls_to_check.begin();
i != colls_to_check.end();
++i, ++scanned) {
if (debug)
cerr << "Scanning " << *i << ", " << scanned << "/"
<< colls_to_check.size() << " completed" << std::endl;
r = _action_on_all_objects_in_pg(store, *i, action, debug);
if (r < 0)
return r;
}
return 0;
}
int action_on_all_objects(ObjectStore *store, action_on_object_t &action, bool debug)
{
int r = _action_on_all_objects(store, action, debug);
store->sync_and_flush();
return r;
}
struct pgid_object_list {
list<pair<coll_t, ghobject_t> > _objects;
void insert(coll_t coll, ghobject_t &ghobj) {
_objects.push_back(make_pair(coll, ghobj));
}
void dump(Formatter *f, bool human_readable) const {
if (!human_readable)
f->open_array_section("pgid_objects");
for (list<pair<coll_t, ghobject_t> >::const_iterator i = _objects.begin();
i != _objects.end();
++i) {
if (i != _objects.begin() && human_readable) {
f->flush(cout);
cout << std::endl;
}
f->open_array_section("pgid_object");
string pgid = i->first.c_str();
std::size_t pos = pgid.find("_");
if (pos == string::npos)
f->dump_string("pgid", pgid);
else
f->dump_string("pgid", pgid.substr(0, pos));
f->open_object_section("ghobject");
i->second.dump(f);
f->close_section();
f->close_section();
}
if (!human_readable)
f->close_section();
}
};
struct lookup_ghobject : public action_on_object_t {
pgid_object_list _objects;
const string _name;
lookup_ghobject(const string& name) : _name(name) { }
virtual int call(ObjectStore *store, coll_t coll, ghobject_t &ghobj, object_info_t &oi) {
if (_name.length() == 0 || ghobj.hobj.oid.name == _name)
_objects.insert(coll, ghobj);
return 0;
}
int size() const {
return _objects._objects.size();
}
pair<coll_t, ghobject_t> pop() {
pair<coll_t, ghobject_t> front = _objects._objects.front();
_objects._objects.pop_front();
return front;
}
void dump(Formatter *f, bool human_readable) const {
_objects.dump(f, human_readable);
}
};
hobject_t infos_oid = OSD::make_infos_oid();
hobject_t biginfo_oid, log_oid;
int file_fd = fd_none;
bool debug = false;
super_header sh;
uint64_t testalign;
template <typename T>
int write_section(sectiontype_t type, const T& obj, int fd) {
bufferlist blhdr, bl, blftr;
obj.encode(bl);
header hdr(type, bl.length());
hdr.encode(blhdr);
footer ft;
ft.encode(blftr);
int ret = blhdr.write_fd(fd);
if (ret) return ret;
ret = bl.write_fd(fd);
if (ret) return ret;
ret = blftr.write_fd(fd);
return ret;
}
// Convert non-printable characters to '\###'
static void cleanbin(string &str)
{
bool cleaned = false;
string clean;
for (string::iterator it = str.begin(); it != str.end(); ++it) {
if (!isprint(*it)) {
clean.push_back('\\');
clean.push_back('0' + ((*it >> 6) & 7));
clean.push_back('0' + ((*it >> 3) & 7));
clean.push_back('0' + (*it & 7));
cleaned = true;
} else {
clean.push_back(*it);
}
}
if (cleaned)
str = clean;
return;
}
int write_simple(sectiontype_t type, int fd)
{
bufferlist hbl;
header hdr(type, 0);
hdr.encode(hbl);
return hbl.write_fd(fd);
}
static int get_fd_data(int fd, bufferlist &bl)
{
uint64_t total = 0;
do {
ssize_t bytes = bl.read_fd(fd, max_read);
if (bytes < 0) {
cerr << "read_fd error " << cpp_strerror(-bytes) << std::endl;
return 1;
}
if (bytes == 0)
break;
total += bytes;
} while(true);
assert(bl.length() == total);
return 0;
}
static void invalid_filestore_path(string &path)
{
cerr << "Invalid filestore path specified: " << path << "\n";
exit(1);
}
int get_log(ObjectStore *fs, coll_t coll, spg_t pgid, const pg_info_t &info,
PGLog::IndexedLog &log, pg_missing_t &missing)
{
map<eversion_t, hobject_t> divergent_priors;
try {
ostringstream oss;
PGLog::read_log(fs, coll, log_oid, info, divergent_priors, log, missing, oss);
if (debug && oss.str().size())
cerr << oss.str() << std::endl;
}
catch (const buffer::error &e) {
cerr << "read_log threw exception error " << e.what() << std::endl;
return 1;
}
return 0;
}
//Based on RemoveWQ::_process()
void remove_coll(ObjectStore *store, const coll_t &coll)
{
spg_t pg;
coll.is_pg_prefix(pg);
OSDriver driver(
store,
coll_t(),
OSD::make_snapmapper_oid());
SnapMapper mapper(&driver, 0, 0, 0, pg.shard);
ghobject_t next;
int r = 0;
int64_t num = 0;
ObjectStore::Transaction *t = new ObjectStore::Transaction;
cout << "remove_coll " << coll << std::endl;
while (!next.is_max()) {
vector<ghobject_t> objects;
r = store->collection_list_partial(coll, next, 200, 300, 0,
&objects, &next);
if (r < 0)
goto out;
for (vector<ghobject_t>::iterator i = objects.begin();
i != objects.end();
++i, ++num) {
OSDriver::OSTransaction _t(driver.get_transaction(t));
cout << "remove " << *i << std::endl;
int r = mapper.remove_oid(i->hobj, &_t);
if (r != 0 && r != -ENOENT) {
assert(0);
}
t->remove(coll, *i);
if (num >= 30) {
store->apply_transaction(*t);
delete t;
t = new ObjectStore::Transaction;
num = 0;
}
}
}
t->remove_collection(coll);
store->apply_transaction(*t);
out:
delete t;
}
//Based on part of OSD::load_pgs()
int finish_remove_pgs(ObjectStore *store, uint64_t *next_removal_seq)
{
vector<coll_t> ls;
int r = store->list_collections(ls);
if (r < 0) {
cerr << "finish_remove_pgs: failed to list pgs: " << cpp_strerror(-r)
<< std::endl;
return r;
}
for (vector<coll_t>::iterator it = ls.begin();
it != ls.end();
++it) {
spg_t pgid;
if (it->is_temp(pgid)) {
cout << "finish_remove_pgs " << *it << " clearing temp" << std::endl;
OSD::recursive_remove_collection(store, *it);
continue;
}
uint64_t seq;
if (it->is_removal(&seq, &pgid)) {
if (seq >= *next_removal_seq)
*next_removal_seq = seq + 1;
cout << "finish_remove_pgs removing " << *it << ", seq is "
<< seq << " pgid is " << pgid << std::endl;
remove_coll(store, *it);
continue;
}
//cout << "finish_remove_pgs ignoring unrecognized " << *it << std::endl;
}
return 0;
}
int initiate_new_remove_pg(ObjectStore *store, spg_t r_pgid,
uint64_t *next_removal_seq)
{
ObjectStore::Transaction *rmt = new ObjectStore::Transaction;
if (store->collection_exists(coll_t(r_pgid))) {
coll_t to_remove = coll_t::make_removal_coll((*next_removal_seq)++, r_pgid);
cout << "collection rename " << coll_t(r_pgid)
<< " to " << to_remove
<< std::endl;
rmt->collection_rename(coll_t(r_pgid), to_remove);
} else {
delete rmt;
return ENOENT;
}
cout << "remove " << coll_t::META_COLL << " " << log_oid.oid << std::endl;
rmt->remove(coll_t::META_COLL, log_oid);
cout << "remove " << coll_t::META_COLL << " " << biginfo_oid.oid << std::endl;
rmt->remove(coll_t::META_COLL, biginfo_oid);
store->apply_transaction(*rmt);
return 0;
}
int header::get_header()
{
bufferlist ebl;
bufferlist::iterator ebliter = ebl.begin();
ssize_t bytes;
bytes = ebl.read_fd(file_fd, sh.header_size);
if ((size_t)bytes != sh.header_size) {
cerr << "Unexpected EOF" << std::endl;
return EFAULT;
}
decode(ebliter);
return 0;
}
int footer::get_footer()
{
bufferlist ebl;
bufferlist::iterator ebliter = ebl.begin();
ssize_t bytes;
bytes = ebl.read_fd(file_fd, sh.footer_size);
if ((size_t)bytes != sh.footer_size) {
cerr << "Unexpected EOF" << std::endl;
return EFAULT;
}
decode(ebliter);
if (magic != endmagic) {
cerr << "Bad footer magic" << std::endl;
return EFAULT;
}
return 0;
}
int write_info(ObjectStore::Transaction &t, epoch_t epoch, pg_info_t &info,
__u8 struct_ver, map<epoch_t,pg_interval_t> &past_intervals)
{
//Empty for this
interval_set<snapid_t> snap_collections; // obsolete
coll_t coll(info.pgid);
int ret = PG::_write_info(t, epoch,
info, coll,
past_intervals,
snap_collections,
infos_oid,
struct_ver,
true, true);
if (ret < 0) ret = -ret;
if (ret) cerr << "Failed to write info" << std::endl;
return ret;
}
void write_log(ObjectStore::Transaction &t, pg_log_t &log)
{
map<eversion_t, hobject_t> divergent_priors;
PGLog::write_log(t, log, log_oid, divergent_priors);
}
int write_pg(ObjectStore::Transaction &t, epoch_t epoch, pg_info_t &info,
pg_log_t &log, __u8 struct_ver, map<epoch_t,pg_interval_t> &past_intervals)
{
int ret = write_info(t, epoch, info, struct_ver, past_intervals);
if (ret) return ret;
write_log(t, log);
return 0;
}
const int OMAP_BATCH_SIZE = 25;
void get_omap_batch(ObjectMap::ObjectMapIterator &iter, map<string, bufferlist> &oset)
{
oset.clear();
for (int count = OMAP_BATCH_SIZE; count && iter->valid(); --count, iter->next()) {
oset.insert(pair<string, bufferlist>(iter->key(), iter->value()));
}
}
int export_file(ObjectStore *store, coll_t cid, ghobject_t &obj)
{
struct stat st;
mysize_t total;
footer ft;
int ret = store->stat(cid, obj, &st);
if (ret < 0)
return ret;
cerr << "Read " << obj << std::endl;
total = st.st_size;
if (debug)
cerr << "size=" << total << std::endl;
object_begin objb(obj);
{
bufferptr bp;
bufferlist bl;
ret = store->getattr(cid, obj, OI_ATTR, bp);
if (ret < 0) {
cerr << "getattr failure object_info " << ret << std::endl;
return ret;
}
bl.push_back(bp);
decode(objb.oi, bl);
if (debug)
cerr << "object_info: " << objb.oi << std::endl;
}
// XXX: Should we be checking for WHITEOUT or LOST in objb.oi.flags and skip?
ret = write_section(TYPE_OBJECT_BEGIN, objb, file_fd);
if (ret < 0)
return ret;
uint64_t offset = 0;
bufferlist rawdatabl;
while(total > 0) {
rawdatabl.clear();
mysize_t len = max_read;
if (len > total)
len = total;
ret = store->read(cid, obj, offset, len, rawdatabl);
if (ret < 0)
return ret;
if (ret == 0)
return -EINVAL;
data_section dblock(offset, len, rawdatabl);
if (debug)
cerr << "data section offset=" << offset << " len=" << len << std::endl;
total -= ret;
offset += ret;
ret = write_section(TYPE_DATA, dblock, file_fd);
if (ret) return ret;
}
//Handle attrs for this object
map<string,bufferptr> aset;
ret = store->getattrs(cid, obj, aset, false);
if (ret) return ret;
attr_section as(aset);
ret = write_section(TYPE_ATTRS, as, file_fd);
if (ret)
return ret;
if (debug) {
cerr << "attrs size " << aset.size() << std::endl;
}
//Handle omap information
bufferlist hdrbuf;
ret = store->omap_get_header(cid, obj, &hdrbuf, true);
if (ret < 0) {
cerr << "omap_get_header: " << cpp_strerror(-ret) << std::endl;
return ret;
}
omap_hdr_section ohs(hdrbuf);
ret = write_section(TYPE_OMAP_HDR, ohs, file_fd);
if (ret)
return ret;
ObjectMap::ObjectMapIterator iter = store->get_omap_iterator(cid, obj);
if (!iter) {
ret = -ENOENT;
cerr << "omap_get_iterator: " << cpp_strerror(-ret) << std::endl;
return ret;
}
iter->seek_to_first();
int mapcount = 0;
map<string, bufferlist> out;
while(iter->valid()) {
get_omap_batch(iter, out);
if (out.empty()) break;
mapcount += out.size();
omap_section oms(out);
ret = write_section(TYPE_OMAP, oms, file_fd);
if (ret)
return ret;
}
if (debug)
cerr << "omap map size " << mapcount << std::endl;
ret = write_simple(TYPE_OBJECT_END, file_fd);
if (ret)
return ret;
return 0;
}
int export_files(ObjectStore *store, coll_t coll)
{
ghobject_t next;
while (!next.is_max()) {
vector<ghobject_t> objects;
int r = store->collection_list_partial(coll, next, 200, 300, 0,
&objects, &next);
if (r < 0)
return r;
for (vector<ghobject_t>::iterator i = objects.begin();
i != objects.end();
++i) {
r = export_file(store, coll, *i);
if (r < 0)
return r;
}
}
return 0;
}
int get_osdmap(ObjectStore *store, epoch_t e, OSDMap &osdmap)
{
bufferlist bl;
bool found = store->read(
coll_t::META_COLL, OSD::get_osdmap_pobject_name(e), 0, 0, bl) >= 0;
if (!found) {
cerr << "Can't find OSDMap for pg epoch " << e << std::endl;
return ENOENT;
}
osdmap.decode(bl);
if (debug)
cerr << osdmap << std::endl;
return 0;
}
int add_osdmap(ObjectStore *store, metadata_section &ms)
{
return get_osdmap(store, ms.map_epoch, ms.osdmap);
}
//Write super_header with its fixed 16 byte length
void write_super()
{
bufferlist superbl;
super_header sh;
footer ft;
header hdr(TYPE_NONE, 0);
hdr.encode(superbl);
sh.magic = super_header::super_magic;
sh.version = super_header::super_ver;
sh.header_size = superbl.length();
superbl.clear();
ft.encode(superbl);
sh.footer_size = superbl.length();
superbl.clear();