forked from ceph/ceph
/
ceph_objectstore_tool.cc
3353 lines (2985 loc) · 93.2 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 <boost/optional.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/FileJournal.h"
#include "os/filestore/FileStore.h"
#ifdef HAVE_LIBFUSE
#include "os/FuseStore.h"
#endif
#include "osd/PGLog.h"
#include "osd/OSD.h"
#include "osd/PG.h"
#include "json_spirit/json_spirit_value.h"
#include "json_spirit/json_spirit_reader.h"
#include "rebuild_mondb.h"
#include "ceph_objectstore_tool.h"
#include "include/compat.h"
namespace po = boost::program_options;
using namespace std;
#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
const ssize_t max_read = 1024 * 1024;
const int fd_none = INT_MIN;
bool outistty;
bool dry_run = false;
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(
coll,
next,
ghobject_t::get_max(),
true,
LIST_AT_A_TIME,
&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) {
if (obj->is_pgmeta())
continue;
object_info_t oi;
if (coll != coll_t::meta()) {
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;
continue;
}
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;
continue;
}
}
r = action.call(store, coll, *obj, oi);
if (r < 0)
return r;
}
}
return 0;
}
int action_on_all_objects_in_pg(ObjectStore *store, string pgidstr, action_on_object_t &action, bool debug)
{
spg_t pgid;
// Scan collections in case this is an ec pool but no shard specified
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;
}
pgid.parse(pgidstr.c_str());
for (vector<coll_t>::iterator i = candidates.begin();
i != candidates.end();
++i) {
spg_t cand_pgid;
if (!i->is_pg(&cand_pgid))
continue;
// If an exact match or treat no shard as any shard
if (cand_pgid == pgid ||
(pgid.is_no_shard() && pgid.pgid == cand_pgid.pgid)) {
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)
break;
}
return r;
}
int action_on_all_objects_in_exact_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);
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) {
if (i->is_pg()) {
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);
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) {
f->open_array_section("pgid_object");
spg_t pgid;
bool is_pg = i->first.is_pg(&pgid);
if (is_pg)
f->dump_string("pgid", stringify(pgid));
if (!is_pg || !human_readable)
f->dump_string("coll", i->first.to_str());
f->open_object_section("ghobject");
i->second.dump(f);
f->close_section();
f->close_section();
if (human_readable) {
f->flush(cout);
cout << std::endl;
}
}
if (!human_readable) {
f->close_section();
f->flush(cout);
cout << std::endl;
}
}
};
struct lookup_ghobject : public action_on_object_t {
pgid_object_list _objects;
const string _name;
const boost::optional<std::string> _namespace;
bool _need_snapset;
lookup_ghobject(const string& name, const boost::optional<std::string>& nspace, bool need_snapset = false) : _name(name),
_namespace(nspace), _need_snapset(need_snapset) { }
virtual int call(ObjectStore *store, coll_t coll, ghobject_t &ghobj, object_info_t &oi) {
if (_need_snapset && !ghobj.hobj.has_snapset())
return 0;
if ((_name.length() == 0 || ghobj.hobj.oid.name == _name) &&
(!_namespace || ghobj.hobj.nspace == _namespace))
_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);
}
};
ghobject_t infos_oid = OSD::make_infos_oid();
ghobject_t log_oid;
ghobject_t biginfo_oid;
int file_fd = fd_none;
bool debug = false;
super_header sh;
uint64_t testalign;
// 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;
}
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 bytes;
}
if (bytes == 0)
break;
total += bytes;
} while(true);
assert(bl.length() == total);
return 0;
}
void myexit(int ret)
{
if (g_ceph_context)
g_ceph_context->put();
exit(ret);
}
int get_log(ObjectStore *fs, __u8 struct_ver,
coll_t coll, spg_t pgid, const pg_info_t &info,
PGLog::IndexedLog &log, pg_missing_t &missing)
{
try {
ostringstream oss;
assert(struct_ver > 0);
PGLog::read_log_and_missing(fs, coll,
struct_ver >= 8 ? coll : coll_t::meta(),
struct_ver >= 8 ? pgid.make_pgmeta_oid() : log_oid,
info, log, missing, oss);
if (debug && oss.str().size())
cerr << oss.str() << std::endl;
}
catch (const buffer::error &e) {
cerr << "read_log_and_missing threw exception error " << e.what() << std::endl;
return -EFAULT;
}
return 0;
}
void dump_log(Formatter *formatter, ostream &out, pg_log_t &log,
pg_missing_t &missing)
{
formatter->open_object_section("op_log");
formatter->open_object_section("pg_log_t");
log.dump(formatter);
formatter->close_section();
formatter->flush(out);
formatter->open_object_section("pg_missing_t");
missing.dump(formatter);
formatter->close_section();
formatter->flush(out);
formatter->open_object_section("map");
formatter->close_section();
formatter->close_section();
formatter->flush(out);
}
//Based on part of OSD::load_pgs()
int finish_remove_pgs(ObjectStore *store)
{
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) ||
(it->is_pg(&pgid) && PG::_has_removal_flag(store, pgid))) {
cout << "finish_remove_pgs " << *it << " removing " << pgid << std::endl;
OSD::recursive_remove_collection(store, pgid, *it);
continue;
}
//cout << "finish_remove_pgs ignoring unrecognized " << *it << std::endl;
}
return 0;
}
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
int mark_pg_for_removal(ObjectStore *fs, spg_t pgid, ObjectStore::Transaction *t)
{
pg_info_t info(pgid);
coll_t coll(pgid);
ghobject_t pgmeta_oid(info.pgid.make_pgmeta_oid());
bufferlist bl;
epoch_t map_epoch = 0;
int r = PG::peek_map_epoch(fs, pgid, &map_epoch, &bl);
if (r < 0)
cerr << __func__ << " warning: peek_map_epoch reported error" << std::endl;
map<epoch_t,pg_interval_t> past_intervals;
__u8 struct_v;
r = PG::read_info(fs, pgid, coll, bl, info, past_intervals, struct_v);
if (r < 0) {
cerr << __func__ << " error on read_info " << cpp_strerror(r) << std::endl;
return r;
}
assert(struct_v >= 8);
// new omap key
cout << "setting '_remove' omap key" << std::endl;
map<string,bufferlist> values;
::encode((char)1, values["_remove"]);
t->omap_setkeys(coll, pgmeta_oid, values);
return 0;
}
#pragma GCC diagnostic pop
#pragma GCC diagnostic warning "-Wpragmas"
int initiate_new_remove_pg(ObjectStore *store, spg_t r_pgid,
ObjectStore::Sequencer &osr)
{
if (!dry_run)
finish_remove_pgs(store);
if (!store->collection_exists(coll_t(r_pgid)))
return -ENOENT;
cout << " marking collection for removal" << std::endl;
if (dry_run)
return 0;
ObjectStore::Transaction rmt;
int r = mark_pg_for_removal(store, r_pgid, &rmt);
if (r < 0) {
return r;
}
store->apply_transaction(&osr, std::move(rmt));
finish_remove_pgs(store);
return r;
}
int write_info(ObjectStore::Transaction &t, epoch_t epoch, pg_info_t &info,
map<epoch_t,pg_interval_t> &past_intervals)
{
//Empty for this
coll_t coll(info.pgid);
ghobject_t pgmeta_oid(info.pgid.make_pgmeta_oid());
map<string,bufferlist> km;
int ret = PG::_prepare_write_info(
&km, epoch,
info, coll,
past_intervals,
pgmeta_oid,
true, true);
if (ret) cerr << "Failed to write info" << std::endl;
t.omap_setkeys(coll, pgmeta_oid, km);
return ret;
}
typedef map<eversion_t, hobject_t> divergent_priors_t;
int write_pg(ObjectStore::Transaction &t, epoch_t epoch, pg_info_t &info,
pg_log_t &log, map<epoch_t,pg_interval_t> &past_intervals,
divergent_priors_t &divergent,
pg_missing_t &missing)
{
int ret = write_info(t, epoch, info, past_intervals);
if (ret)
return ret;
coll_t coll(info.pgid);
map<string,bufferlist> km;
if (!divergent.empty()) {
assert(missing.get_items().empty());
PGLog::write_log_and_missing_wo_missing(
t, &km, log, coll, info.pgid.make_pgmeta_oid(), divergent, true);
} else {
pg_missing_tracker_t tmissing(missing);
PGLog::write_log_and_missing(
t, &km, log, coll, info.pgid.make_pgmeta_oid(), tmissing, true);
}
t.omap_setkeys(coll, info.pgid.make_pgmeta_oid(), km);
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 ObjectStoreTool::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);
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 ObjectStoreTool::export_files(ObjectStore *store, coll_t coll)
{
ghobject_t next;
while (!next.is_max()) {
vector<ghobject_t> objects;
int r = store->collection_list(coll, next, ghobject_t::get_max(), true, 300,
&objects, &next);
if (r < 0)
return r;
for (vector<ghobject_t>::iterator i = objects.begin();
i != objects.end();
++i) {
assert(!i->hobj.is_meta());
if (i->is_pgmeta() || i->hobj.is_temp()) {
continue;
}
r = export_file(store, coll, *i);
if (r < 0)
return r;
}
}
return 0;
}
int set_inc_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl, bool force,
ObjectStore::Sequencer &osr) {
OSDMap::Incremental inc;
bufferlist::iterator it = bl.begin();
inc.decode(it);
if (e == 0) {
e = inc.epoch;
} else if (e != inc.epoch) {
cerr << "incremental.epoch mismatch: "
<< inc.epoch << " != " << e << std::endl;
if (force) {
cerr << "But will continue anyway." << std::endl;
} else {
return -EINVAL;
}
}
const ghobject_t inc_oid = OSD::get_inc_osdmap_pobject_name(e);
if (!store->exists(coll_t::meta(), inc_oid)) {
cerr << "inc-osdmap (" << inc_oid << ") does not exist." << std::endl;
if (!force) {
return -ENOENT;
}
cout << "Creating a new epoch." << std::endl;
}
if (dry_run)
return 0;
ObjectStore::Transaction t;
t.write(coll_t::meta(), inc_oid, 0, bl.length(), bl);
t.truncate(coll_t::meta(), inc_oid, bl.length());
int ret = store->apply_transaction(&osr, std::move(t));
if (ret) {
cerr << "Failed to set inc-osdmap (" << inc_oid << "): " << ret << std::endl;
} else {
cout << "Wrote inc-osdmap." << inc.epoch << std::endl;
}
return ret;
}
int get_inc_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl)
{
if (store->read(coll_t::meta(),
OSD::get_inc_osdmap_pobject_name(e),
0, 0, bl) < 0) {
return -ENOENT;
}
return 0;
}
int set_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl, bool force,
ObjectStore::Sequencer &osr) {
OSDMap osdmap;
osdmap.decode(bl);
if (e == 0) {
e = osdmap.get_epoch();
} else if (e != osdmap.get_epoch()) {
cerr << "osdmap.epoch mismatch: "
<< e << " != " << osdmap.get_epoch() << std::endl;
if (force) {
cerr << "But will continue anyway." << std::endl;
} else {
return -EINVAL;
}
}
const ghobject_t full_oid = OSD::get_osdmap_pobject_name(e);
if (!store->exists(coll_t::meta(), full_oid)) {
cerr << "osdmap (" << full_oid << ") does not exist." << std::endl;
if (!force) {
return -ENOENT;
}
cout << "Creating a new epoch." << std::endl;
}
if (dry_run)
return 0;
ObjectStore::Transaction t;
t.write(coll_t::meta(), full_oid, 0, bl.length(), bl);
t.truncate(coll_t::meta(), full_oid, bl.length());
int ret = store->apply_transaction(&osr, std::move(t));
if (ret) {
cerr << "Failed to set osdmap (" << full_oid << "): " << ret << std::endl;
} else {
cout << "Wrote osdmap." << osdmap.get_epoch() << std::endl;
}
return ret;
}
int get_osdmap(ObjectStore *store, epoch_t e, OSDMap &osdmap, bufferlist& bl)
{
bool found = store->read(
coll_t::meta(), 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, ms.osdmap_bl);
}
int ObjectStoreTool::do_export(ObjectStore *fs, coll_t coll, spg_t pgid,
pg_info_t &info, epoch_t map_epoch, __u8 struct_ver,
const OSDSuperblock& superblock,
map<epoch_t,pg_interval_t> &past_intervals)
{
PGLog::IndexedLog log;
pg_missing_t missing;
cerr << "Exporting " << pgid << std::endl;
int ret = get_log(fs, struct_ver, coll, pgid, info, log, missing);
if (ret > 0)
return ret;
if (debug) {
Formatter *formatter = Formatter::create("json-pretty");
assert(formatter);
dump_log(formatter, cerr, log, missing);
delete formatter;
}
write_super();
pg_begin pgb(pgid, superblock);
// Special case: If replicated pg don't require the importing OSD to have shard feature
if (pgid.is_no_shard()) {
pgb.superblock.compat_features.incompat.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS);
}
ret = write_section(TYPE_PG_BEGIN, pgb, file_fd);
if (ret)
return ret;
// The metadata_section is now before files, so import can detect
// errors and abort without wasting time.
metadata_section ms(
struct_ver,
map_epoch,
info,
log,
past_intervals,
missing);
ret = add_osdmap(fs, ms);
if (ret)
return ret;
ret = write_section(TYPE_PG_METADATA, ms, file_fd);
if (ret)
return ret;
ret = export_files(fs, coll);
if (ret) {
cerr << "export_files error " << ret << std::endl;
return ret;
}
ret = write_simple(TYPE_PG_END, file_fd);
if (ret)
return ret;
return 0;
}
int get_data(ObjectStore *store, coll_t coll, ghobject_t hoid,
ObjectStore::Transaction *t, bufferlist &bl)
{
bufferlist::iterator ebliter = bl.begin();
data_section ds;
ds.decode(ebliter);
if (debug)
cerr << "\tdata: offset " << ds.offset << " len " << ds.len << std::endl;
t->write(coll, hoid, ds.offset, ds.len, ds.databl);
return 0;
}
int get_attrs(ObjectStore *store, coll_t coll, ghobject_t hoid,
ObjectStore::Transaction *t, bufferlist &bl,
OSDriver &driver, SnapMapper &snap_mapper)
{
bufferlist::iterator ebliter = bl.begin();
attr_section as;
as.decode(ebliter);
if (debug)
cerr << "\tattrs: len " << as.data.size() << std::endl;
t->setattrs(coll, hoid, as.data);
// This could have been handled in the caller if we didn't need to
// support exports that didn't include object_info_t in object_begin.
if (hoid.hobj.snap < CEPH_MAXSNAP && hoid.generation == ghobject_t::NO_GEN) {
map<string,bufferlist>::iterator mi = as.data.find(OI_ATTR);
if (mi != as.data.end()) {
object_info_t oi(mi->second);
if (debug)
cerr << "object_info " << oi << std::endl;
OSDriver::OSTransaction _t(driver.get_transaction(t));
set<snapid_t> oi_snaps(oi.snaps.begin(), oi.snaps.end());
snap_mapper.add_oid(hoid.hobj, oi_snaps, &_t);
}
}
return 0;
}
int get_omap_hdr(ObjectStore *store, coll_t coll, ghobject_t hoid,
ObjectStore::Transaction *t, bufferlist &bl)
{
bufferlist::iterator ebliter = bl.begin();
omap_hdr_section oh;
oh.decode(ebliter);
if (debug)
cerr << "\tomap header: " << string(oh.hdr.c_str(), oh.hdr.length())
<< std::endl;
t->omap_setheader(coll, hoid, oh.hdr);
return 0;
}
int get_omap(ObjectStore *store, coll_t coll, ghobject_t hoid,
ObjectStore::Transaction *t, bufferlist &bl)
{
bufferlist::iterator ebliter = bl.begin();
omap_section os;
os.decode(ebliter);
if (debug)
cerr << "\tomap: size " << os.omap.size() << std::endl;
t->omap_setkeys(coll, hoid, os.omap);
return 0;
}
int ObjectStoreTool::get_object(ObjectStore *store, coll_t coll,
bufferlist &bl, OSDMap &curmap,
bool *skipped_objects,
ObjectStore::Sequencer &osr)
{
ObjectStore::Transaction tran;
ObjectStore::Transaction *t = &tran;
bufferlist::iterator ebliter = bl.begin();
object_begin ob;
ob.decode(ebliter);
OSDriver driver(
store,
coll_t(),
OSD::make_snapmapper_oid());
spg_t pg;
coll.is_pg_prefix(&pg);
SnapMapper mapper(&driver, 0, 0, 0, pg.shard);
if (ob.hoid.hobj.is_temp()) {
cerr << "ERROR: Export contains temporary object '" << ob.hoid << "'" << std::endl;
return -EFAULT;
}
assert(g_ceph_context);
if (ob.hoid.hobj.nspace != g_ceph_context->_conf->osd_hit_set_namespace) {
object_t oid = ob.hoid.hobj.oid;
object_locator_t loc(ob.hoid.hobj);
pg_t raw_pgid = curmap.object_locator_to_pg(oid, loc);
pg_t pgid = curmap.raw_pg_to_pg(raw_pgid);
spg_t coll_pgid;
if (coll.is_pg(&coll_pgid) == false) {
cerr << "INTERNAL ERROR: Bad collection during import" << std::endl;
return -EFAULT;
}
if (coll_pgid.shard != ob.hoid.shard_id) {
cerr << "INTERNAL ERROR: Importing shard " << coll_pgid.shard
<< " but object shard is " << ob.hoid.shard_id << std::endl;
return -EFAULT;
}
if (coll_pgid.pgid != pgid) {
cerr << "Skipping object '" << ob.hoid << "' which belongs in pg " << pgid << std::endl;
*skipped_objects = true;
skip_object(bl);
return 0;
}
}
if (!dry_run)
t->touch(coll, ob.hoid);
cout << "Write " << ob.hoid << std::endl;
bufferlist ebl;
bool done = false;
while(!done) {
sectiontype_t type;
int ret = read_section(&type, &ebl);
if (ret)
return ret;
//cout << "\tdo_object: Section type " << hex << type << dec << std::endl;
//cout << "\t\tsection size " << ebl.length() << std::endl;
if (type >= END_OF_TYPES) {
cout << "Skipping unknown object section type" << std::endl;
continue;
}
switch(type) {
case TYPE_DATA:
if (dry_run) break;
ret = get_data(store, coll, ob.hoid, t, ebl);
if (ret) return ret;
break;
case TYPE_ATTRS:
if (dry_run) break;
ret = get_attrs(store, coll, ob.hoid, t, ebl, driver, mapper);
if (ret) return ret;
break;
case TYPE_OMAP_HDR:
if (dry_run) break;
ret = get_omap_hdr(store, coll, ob.hoid, t, ebl);
if (ret) return ret;
break;
case TYPE_OMAP:
if (dry_run) break;
ret = get_omap(store, coll, ob.hoid, t, ebl);
if (ret) return ret;
break;
case TYPE_OBJECT_END:
done = true;
break;
default:
cerr << "Unknown section type " << type << std::endl;
return -EFAULT;
}
}