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
|
// -*- 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) 2015 Red Hat
*
* 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 "common/errno.h"
#include "common/ceph_argparse.h"
#include <fstream>
#include "include/util.h"
#include "mds/CInode.h"
#include "cls/cephfs/cls_cephfs_client.h"
#include "DataScan.h"
#define dout_subsys ceph_subsys_mds
#undef dout_prefix
#define dout_prefix *_dout << "datascan." << __func__ << ": "
void DataScan::usage()
{
std::cout << "Usage: \n"
<< " cephfs-data-scan init [--force-init]\n"
<< " cephfs-data-scan scan_extents [--force-pool] <data pool name>\n"
<< " cephfs-data-scan scan_inodes [--force-pool] [--force-corrupt] <data pool name>\n"
<< "\n"
<< " --force-corrupt: overrite apparently corrupt structures\n"
<< " --force-init: write root inodes even if they exist\n"
<< " --force-pool: use data pool even if it is not in MDSMap\n"
<< std::endl;
generic_client_usage();
}
bool DataScan::parse_kwarg(
const std::vector<const char*> &args,
std::vector<const char *>::const_iterator &i,
int *r)
{
if (i + 1 == args.end()) {
return false;
}
const std::string arg(*i);
const std::string val(*(++i));
if (arg == std::string("--output-dir")) {
if (driver != NULL) {
derr << "Unexpected --output-dir: output already selected!" << dendl;
*r = -EINVAL;
return false;
}
driver = new LocalFileDriver(val, data_io);
return true;
} else if (arg == std::string("-n")) {
std::string err;
n = strict_strtoll(val.c_str(), 10, &err);
if (!err.empty()) {
std::cerr << "Invalid worker number '" << val << "'" << std::endl;
*r = -EINVAL;
return false;
}
return true;
} else if (arg == std::string("-m")) {
std::string err;
m = strict_strtoll(val.c_str(), 10, &err);
if (!err.empty()) {
std::cerr << "Invalid worker count '" << val << "'" << std::endl;
*r = -EINVAL;
return false;
}
return true;
} else {
return false;
}
}
bool DataScan::parse_arg(
const std::vector<const char*> &args,
std::vector<const char *>::const_iterator &i)
{
const std::string arg(*i);
if (arg == "--force-pool") {
force_pool = true;
return true;
} else if (arg == "--force-corrupt") {
force_corrupt = true;
return true;
} else if (arg == "--force-init") {
force_init = true;
return true;
} else {
return false;
}
}
int DataScan::main(const std::vector<const char*> &args)
{
// Parse args
// ==========
if (args.size() < 1) {
usage();
return -EINVAL;
}
// Common RADOS init: open metadata pool
// =====================================
librados::Rados rados;
int r = rados.init_with_context(g_ceph_context);
if (r < 0) {
derr << "RADOS unavailable" << dendl;
return r;
}
std::string const &command = args[0];
std::string data_pool_name;
// Consume any known --key val or --flag arguments
for (std::vector<const char *>::const_iterator i = args.begin() + 1;
i != args.end(); ++i) {
if (parse_kwarg(args, i, &r)) {
// Skip the kwarg value field
++i;
continue;
} else if (r) {
return r;
}
if (parse_arg(args, i)) {
continue;
}
if (i + 1 == args.end() &&
(command == "scan_inodes" || command == "scan_extents")) {
data_pool_name = *i;
continue;
}
// Fall through: unhandled
std::cerr << "Unknown argument '" << *i << "'" << std::endl;
return -EINVAL;
}
// Default to output to metadata pool
if (driver == NULL) {
driver = new MetadataDriver();
driver->set_force_corrupt(force_corrupt);
driver->set_force_init(force_init);
}
dout(4) << "connecting to RADOS..." << dendl;
rados.connect();
r = driver->init(rados, mdsmap);
if (r < 0) {
return r;
}
// Initialize data_io for those commands that need it
if (command == "scan_inodes"
|| command == "scan_extents") {
if (data_pool_name.empty()) {
std::cerr << "Data pool not specified" << std::endl;
usage();
return -EINVAL;
}
data_pool_id = rados.pool_lookup(data_pool_name.c_str());
if (data_pool_id < 0) {
std::cerr << "Data pool '" << data_pool_name << "' not found!" << std::endl;
return -ENOENT;
} else {
dout(4) << "data pool '" << data_pool_name
<< "' has ID " << data_pool_id << dendl;
}
if (!mdsmap->is_data_pool(data_pool_id)) {
std::cerr << "Warning: pool '" << data_pool_name << "' is not a "
"CephFS data pool!" << std::endl;
if (!force_pool) {
std::cerr << "Use --force-pool to continue" << std::endl;
return -EINVAL;
}
}
dout(4) << "opening data pool '" << data_pool_name << "'" << dendl;
r = rados.ioctx_create(data_pool_name.c_str(), data_io);
if (r != 0) {
return r;
}
}
// Finally, dispatch command
if (command == "scan_inodes") {
return scan_inodes();
} else if (command == "scan_extents") {
return scan_extents();
} else if (command == "init") {
return driver->init_roots(mdsmap->get_first_data_pool());
} else {
std::cerr << "Unknown command '" << command << "'" << std::endl;
return -EINVAL;
}
}
int MetadataDriver::inject_unlinked_inode(
inodeno_t inono, int mode, int64_t data_pool_id)
{
const object_t oid = InodeStore::get_object_name(inono, frag_t(), ".inode");
// Skip if exists
bool already_exists = false;
int r = root_exists(inono, &already_exists);
if (r) {
return r;
}
if (already_exists && !force_init) {
std::cerr << "Inode 0x" << std::hex << inono << std::dec << " already"
" exists, skipping create. Use --force-init to overwrite"
" the existing object." << std::endl;
return 0;
}
// Compose
InodeStore inode;
inode.inode.ino = inono;
inode.inode.version = 1;
inode.inode.xattr_version = 1;
inode.inode.mode = 0500 | mode;
// Fake size to 1, so that the directory doesn't appear to be empty
// (we won't actually give the *correct* size here though)
inode.inode.size = 1;
inode.inode.dirstat.nfiles = 1;
inode.inode.ctime =
inode.inode.mtime = ceph_clock_now(g_ceph_context);
inode.inode.nlink = 1;
inode.inode.truncate_size = -1ull;
inode.inode.truncate_seq = 1;
inode.inode.uid = g_conf->mds_root_ino_uid;
inode.inode.gid = g_conf->mds_root_ino_gid;
// Force layout to default: should we let users override this so that
// they don't have to mount the filesystem to correct it?
inode.inode.layout = g_default_file_layout;
inode.inode.layout.fl_pg_pool = data_pool_id;
// Assume that we will get our stats wrong, and that we may
// be ignoring dirfrags that exist
inode.damage_flags |= (DAMAGE_STATS | DAMAGE_RSTATS | DAMAGE_FRAGTREE);
// Serialize
bufferlist inode_bl;
::encode(std::string(CEPH_FS_ONDISK_MAGIC), inode_bl);
inode.encode(inode_bl);
// Write
r = metadata_io.write_full(oid.name, inode_bl);
if (r != 0) {
derr << "Error writing '" << oid.name << "': " << cpp_strerror(r) << dendl;
return r;
}
return r;
}
int MetadataDriver::root_exists(inodeno_t ino, bool *result)
{
object_t oid = InodeStore::get_object_name(ino, frag_t(), ".inode");
uint64_t size;
time_t mtime;
int r = metadata_io.stat(oid.name, &size, &mtime);
if (r == -ENOENT) {
*result = false;
return 0;
} else if (r < 0) {
return r;
}
*result = true;
return 0;
}
int MetadataDriver::init_roots(int64_t data_pool_id)
{
int r = 0;
r = inject_unlinked_inode(MDS_INO_ROOT, S_IFDIR|0755, data_pool_id);
if (r != 0) {
return r;
}
r = inject_unlinked_inode(MDS_INO_MDSDIR(0), S_IFDIR, data_pool_id);
if (r != 0) {
return r;
}
return 0;
}
int MetadataDriver::check_roots(bool *result)
{
int r;
r = root_exists(MDS_INO_ROOT, result);
if (r != 0) {
return r;
}
if (!*result) {
return 0;
}
r = root_exists(MDS_INO_MDSDIR(0), result);
if (r != 0) {
return r;
}
if (!*result) {
return 0;
}
return 0;
}
/**
* Stages:
*
* SERIAL init
* 0. Create root inodes if don't exist
* PARALLEL scan_extents
* 1. Size and mtime recovery: scan ALL objects, and update 0th
* objects with max size and max mtime seen.
* PARALLEL scan_inodes
* 2. Inode recovery: scan ONLY 0th objects, and inject metadata
* into dirfrag OMAPs, creating blank dirfrags as needed. No stats
* or rstats at this stage. Inodes without backtraces go into
* lost+found
* TODO: SERIAL "recover stats"
* 3. Dirfrag statistics: depth first traverse into metadata tree,
* rebuilding dir sizes.
* TODO PARALLEL "clean up"
* 4. Cleanup; go over all 0th objects (and dirfrags if we tagged
* anything onto them) and remove any of the xattrs that we
* used for accumulating.
*/
int parse_oid(const std::string &oid, uint64_t *inode_no, uint64_t *obj_id)
{
if (oid.find(".") == std::string::npos || oid.find(".") == oid.size() - 1) {
return -EINVAL;
}
std::string err;
std::string inode_str = oid.substr(0, oid.find("."));
*inode_no = strict_strtoll(inode_str.c_str(), 16, &err);
if (!err.empty()) {
return -EINVAL;
}
std::string pos_string = oid.substr(oid.find(".") + 1);
*obj_id = strict_strtoll(pos_string.c_str(), 16, &err);
if (!err.empty()) {
return -EINVAL;
}
return 0;
}
// Pending sharded pgls & add in progress mechanism for that
#undef SHARDEDPGLS
int DataScan::scan_extents()
{
#ifdef SHARDED_PGLS
float progress = 0.0;
librados::NObjectIterator i = data_io.nobjects_begin(n, m);
#else
librados::NObjectIterator i = data_io.nobjects_begin();
#endif
librados::NObjectIterator i_end = data_io.nobjects_end();
int r = 0;
for (; i != i_end; ++i) {
const std::string oid = i->get_oid();
#ifdef SHARDED_PGLS
if (i.get_progress() != progress) {
if (int(i.get_progress() * 100) / 5 != int(progress * 100) / 5) {
std::cerr << percentify(i.get_progress()) << "%" << std::endl;
}
progress = i.get_progress();
}
#endif
// Read size
uint64_t size;
time_t mtime;
r = data_io.stat(oid, &size, &mtime);
if (r != 0) {
dout(4) << "Cannot stat '" << oid << "': skipping" << dendl;
continue;
}
// I need to keep track of
// * The highest object ID seen
// * The size of the highest object ID seen
// * The largest object seen
//
// Given those things, I can later infer the object chunking
// size, the offset of the last object (chunk size * highest ID seen)
// and the actual size (offset of last object + size of highest ID seen)
//
// This logic doesn't take account of striping.
uint64_t inode_no = 0;
uint64_t obj_id = 0;
r = parse_oid(oid, &inode_no, &obj_id);
if (r != 0) {
dout(4) << "Bad object name '" << oid << "' skipping" << dendl;
continue;
}
int r = ClsCephFSClient::accumulate_inode_metadata(
data_io,
inode_no,
obj_id,
size,
mtime);
if (r < 0) {
derr << "Failed to accumulate metadata data from '"
<< oid << "': " << cpp_strerror(r) << dendl;
continue;
}
}
return 0;
}
int DataScan::scan_inodes()
{
#ifdef SHARDED_PGLS
float progress = 0.0;
librados::NObjectIterator i = data_io.nobjects_begin(n, m);
#else
librados::NObjectIterator i = data_io.nobjects_begin();
#endif
librados::NObjectIterator i_end = data_io.nobjects_end();
bool roots_present;
int r = driver->check_roots(&roots_present);
if (r != 0) {
derr << "Unexpected error checking roots: '"
<< cpp_strerror(r) << "'" << dendl;
return r;
}
if (!roots_present) {
std::cerr << "Some or all system inodes are absent. Run 'init' from "
"one node before running 'scan_inodes'" << std::endl;
return -EIO;
}
for (; i != i_end; ++i) {
const std::string oid = i->get_oid();
#ifdef SHARDED_PGLS
if (i.get_progress() != progress) {
if (int(i.get_progress() * 100) / 5 != int(progress * 100) / 5) {
std::cerr << percentify(i.get_progress()) << "%" << std::endl;
}
progress = i.get_progress();
}
#endif
uint64_t obj_name_ino = 0;
uint64_t obj_name_offset = 0;
r = parse_oid(oid, &obj_name_ino, &obj_name_offset);
if (r != 0) {
dout(4) << "Bad object name '" << oid << "', skipping" << dendl;
continue;
}
// We are only interested in 0th objects during this phase: we touched
// the other objects during scan_extents
if (obj_name_offset != 0) {
continue;
}
AccumulateResult accum_res;
inode_backtrace_t backtrace;
int r = ClsCephFSClient::fetch_inode_accumulate_result(
data_io, oid, &backtrace, &accum_res);
if (r < 0) {
dout(4) << "Unexpected error loading accumulated metadata from '"
<< oid << "': " << cpp_strerror(r) << dendl;
continue;
}
const time_t file_mtime = accum_res.max_mtime;
uint64_t file_size = 0;
uint32_t chunk_size = g_default_file_layout.fl_object_size;
bool have_backtrace = !(backtrace.ancestors.empty());
// Calculate file_size, guess chunk_size
if (accum_res.ceiling_obj_index > 0) {
// When there are multiple objects, the largest object probably
// indicates the chunk size. But not necessarily, because files
// can be sparse. Only make this assumption if size seen
// is a power of two, as chunk sizes typically are.
if ((accum_res.max_obj_size & (accum_res.max_obj_size - 1)) == 0) {
chunk_size = accum_res.max_obj_size;
}
file_size = chunk_size * accum_res.ceiling_obj_index
+ accum_res.ceiling_obj_size;
} else {
file_size = accum_res.ceiling_obj_size;
}
ceph_file_layout guessed_layout;
guessed_layout = g_default_file_layout;
guessed_layout.fl_object_size = chunk_size;
guessed_layout.fl_stripe_unit = chunk_size;
guessed_layout.fl_pg_pool = data_pool_id;
// Santity checking backtrace ino against object name
if (have_backtrace && backtrace.ino != obj_name_ino) {
dout(4) << "Backtrace ino 0x" << std::hex << backtrace.ino
<< " doesn't match object name ino 0x" << obj_name_ino
<< std::dec << dendl;
have_backtrace = false;
}
// Inject inode to the metadata pool
if (have_backtrace) {
inode_backpointer_t root_bp = *(backtrace.ancestors.rbegin());
if (MDS_INO_IS_MDSDIR(root_bp.dirino)) {
/* Special case for strays: even if we have a good backtrace,
* don't put it in the stray dir, because while that would technically
* give it linkage it would still be invisible to the user */
r = driver->inject_lost_and_found(
obj_name_ino, file_size, file_mtime, guessed_layout);
if (r < 0) {
dout(4) << "Error injecting 0x" << std::hex << backtrace.ino
<< std::dec << " into lost+found: " << cpp_strerror(r) << dendl;
if (r == -EINVAL) {
dout(4) << "Use --force-corrupt to overwrite structures that "
"appear to be corrupt" << dendl;
}
}
} else {
/* Happy case: we will inject a named dentry for this inode */
r = driver->inject_with_backtrace(
backtrace, file_size, file_mtime, guessed_layout);
if (r < 0) {
dout(4) << "Error injecting 0x" << std::hex << backtrace.ino
<< std::dec << " with backtrace: " << cpp_strerror(r) << dendl;
if (r == -EINVAL) {
dout(4) << "Use --force-corrupt to overwrite structures that "
"appear to be corrupt" << dendl;
}
}
}
} else {
/* Backtrace-less case: we will inject a lost+found dentry */
r = driver->inject_lost_and_found(
obj_name_ino, file_size, file_mtime, guessed_layout);
if (r < 0) {
dout(4) << "Error injecting 0x" << std::hex << obj_name_ino
<< std::dec << " into lost+found: " << cpp_strerror(r) << dendl;
if (r == -EINVAL) {
dout(4) << "Use --force-corrupt to overwrite structures that "
"appear to be corrupt" << dendl;
}
}
}
}
return 0;
}
int MetadataDriver::read_fnode(
inodeno_t ino, frag_t frag, fnode_t *fnode,
uint64_t *last_version)
{
assert(fnode != NULL);
object_t frag_oid = InodeStore::get_object_name(ino, frag, "");
bufferlist fnode_bl;
int r = metadata_io.omap_get_header(frag_oid.name, &fnode_bl);
*last_version = metadata_io.get_last_version();
if (r < 0) {
return r;
}
bufferlist::iterator old_fnode_iter = fnode_bl.begin();
try {
(*fnode).decode(old_fnode_iter);
} catch (const buffer::error &err) {
return -EINVAL;
}
return 0;
}
int MetadataDriver::read_dentry(inodeno_t parent_ino, frag_t frag,
const std::string &dname, InodeStore *inode)
{
assert(inode != NULL);
std::string key;
dentry_key_t dn_key(CEPH_NOSNAP, dname.c_str());
dn_key.encode(key);
std::set<std::string> keys;
keys.insert(key);
std::map<std::string, bufferlist> vals;
object_t frag_oid = InodeStore::get_object_name(parent_ino, frag, "");
int r = metadata_io.omap_get_vals_by_keys(frag_oid.name, keys, &vals);
dout(20) << "oid=" << frag_oid.name
<< " dname=" << dname
<< " frag=" << frag
<< ", r=" << r << dendl;
if (r < 0) {
return r;
}
if (vals.find(key) == vals.end()) {
dout(20) << key << " not found in result" << dendl;
return -ENOENT;
}
try {
bufferlist::iterator q = vals[key].begin();
snapid_t dnfirst;
::decode(dnfirst, q);
char dentry_type;
::decode(dentry_type, q);
if (dentry_type == 'I') {
inode->decode_bare(q);
return 0;
} else {
dout(20) << "dentry type '" << dentry_type << "': cannot"
"read an inode out of that" << dendl;
return -EINVAL;
}
} catch (const buffer::error &err) {
dout(20) << "encoding error in dentry 0x" << std::hex << parent_ino
<< std::dec << "/" << dname << dendl;
return -EINVAL;
}
return 0;
}
int MetadataDriver::inject_lost_and_found(inodeno_t ino, uint64_t file_size,
time_t file_mtime, const ceph_file_layout &layout)
{
// Create lost+found if doesn't exist
bool created = false;
int r = find_or_create_dirfrag(CEPH_INO_ROOT, frag_t(), &created);
if (r < 0) {
return r;
}
InodeStore lf_ino;
r = read_dentry(CEPH_INO_ROOT, frag_t(), "lost+found", &lf_ino);
if (r == -ENOENT || r == -EINVAL) {
if (r == -EINVAL && !force_corrupt) {
return r;
}
// Inject dentry
lf_ino.inode.mode = 0755 | S_IFDIR;
// Set nfiles to something non-zero, to fool any other code
// that tries to ignore 'empty' directories. This won't be
// accurate, but it should avoid functional issues.
lf_ino.inode.dirstat.nfiles = 1;
lf_ino.inode.size = 1;
lf_ino.inode.nlink = 1;
lf_ino.inode.ino = CEPH_INO_LOST_AND_FOUND;
lf_ino.inode.version = 1;
lf_ino.inode.backtrace_version = 1;
lf_ino.inode.uid = g_conf->mds_root_ino_uid;
lf_ino.inode.gid = g_conf->mds_root_ino_gid;
r = inject_linkage(CEPH_INO_ROOT, "lost+found", frag_t(), lf_ino);
if (r < 0) {
return r;
}
} else {
if (!(lf_ino.inode.mode & S_IFDIR)) {
derr << "lost+found exists but is not a directory!" << dendl;
// In this case we error out, and the user should do something about
// this problem.
return -EINVAL;
}
}
r = find_or_create_dirfrag(CEPH_INO_LOST_AND_FOUND, frag_t(), &created);
if (r < 0) {
return r;
}
InodeStore recovered_ino;
recovered_ino.inode.mode = 0500 | S_IFREG;
recovered_ino.inode.size = file_size;
recovered_ino.inode.max_size_ever = file_size;
recovered_ino.inode.mtime.tv.tv_sec = file_mtime;
recovered_ino.inode.atime.tv.tv_sec = file_mtime;
recovered_ino.inode.ctime.tv.tv_sec = file_mtime;
recovered_ino.inode.layout = layout;
recovered_ino.inode.truncate_seq = 1;
recovered_ino.inode.truncate_size = -1ull;
recovered_ino.inode.inline_data.version = CEPH_INLINE_NONE;
recovered_ino.inode.nlink = 1;
recovered_ino.inode.ino = ino;
recovered_ino.inode.version = 1;
recovered_ino.inode.backtrace_version = 1;
recovered_ino.inode.uid = g_conf->mds_root_ino_uid;
recovered_ino.inode.gid = g_conf->mds_root_ino_gid;
const std::string dname = lost_found_dname(ino);
// Write dentry into lost+found dirfrag
return inject_linkage(lf_ino.inode.ino, dname, frag_t(), recovered_ino);
}
int MetadataDriver::get_frag_of(
inodeno_t dirino,
const std::string &target_dname,
frag_t *result_ft)
{
object_t root_frag_oid = InodeStore::get_object_name(dirino, frag_t(), "");
dout(20) << "dirino=" << dirino << " target_dname=" << target_dname << dendl;
// Find and load fragtree if existing dirfrag
// ==========================================
bool have_backtrace = false;
bufferlist parent_bl;
int r = metadata_io.getxattr(root_frag_oid.name, "parent", parent_bl);
if (r == -ENODATA) {
dout(10) << "No backtrace on '" << root_frag_oid << "'" << dendl;
} else if (r < 0) {
dout(4) << "Unexpected error on '" << root_frag_oid << "': "
<< cpp_strerror(r) << dendl;
return r;
}
// Deserialize backtrace
inode_backtrace_t backtrace;
if (parent_bl.length()) {
try {
bufferlist::iterator q = parent_bl.begin();
backtrace.decode(q);
have_backtrace = true;
} catch (buffer::error &e) {
dout(4) << "Corrupt backtrace on '" << root_frag_oid << "': " << e << dendl;
}
}
if (!(have_backtrace && backtrace.ancestors.size())) {
// Can't work out fragtree without a backtrace
dout(4) << "No backtrace on '" << root_frag_oid
<< "': cannot determine fragtree" << dendl;
return -ENOENT;
}
// The parentage of dirino
const inode_backpointer_t &bp = *(backtrace.ancestors.begin());
// The inode of dirino's parent
const inodeno_t parent_ino = bp.dirino;
// The dname of dirino in its parent.
const std::string &parent_dname = bp.dname;
dout(20) << "got backtrace parent " << parent_ino << "/"
<< parent_dname << dendl;
// The primary dentry for dirino
InodeStore existing_dentry;
// See if we can find ourselves in dirfrag zero of the parent: this
// is a fast path that avoids needing to go further up the tree
// if the parent isn't fragmented (worst case we would have to
// go all the way to the root)
r = read_dentry(parent_ino, frag_t(), parent_dname, &existing_dentry);
if (r >= 0) {
// Great, fast path: return the fragtree from here
if (existing_dentry.inode.ino != dirino) {
dout(4) << "Unexpected inode in dentry! 0x" << std::hex
<< existing_dentry.inode.ino
<< " vs expected 0x" << dirino << std::dec << dendl;
return -ENOENT;
}
dout(20) << "fast path, fragtree is "
<< existing_dentry.dirfragtree << dendl;
*result_ft = existing_dentry.pick_dirfrag(target_dname);
dout(20) << "frag is " << *result_ft << dendl;
return 0;
} else if (r != -ENOENT) {
// Dentry not present in 0th frag, must read parent's fragtree
frag_t parent_frag;
r = get_frag_of(parent_ino, parent_dname, &parent_frag);
if (r == 0) {
// We have the parent fragtree, so try again to load our dentry
r = read_dentry(parent_ino, parent_frag, parent_dname, &existing_dentry);
if (r >= 0) {
// Got it!
*result_ft = existing_dentry.pick_dirfrag(target_dname);
dout(20) << "resolved via parent, frag is " << *result_ft << dendl;
return 0;
} else {
if (r == -EINVAL || r == -ENOENT) {
return -ENOENT; // dentry missing or corrupt, so frag is missing
} else {
return r;
}
}
} else {
// Couldn't resolve parent fragtree, so can't find ours.
return r;
}
} else if (r == -EINVAL) {
// Unreadable dentry, can't know the fragtree.
return -ENOENT;
} else {
// Unexpected error, raise it
return r;
}
}
int MetadataDriver::inject_with_backtrace(
const inode_backtrace_t &backtrace, uint64_t file_size, time_t file_mtime,
const ceph_file_layout &layout)
{
// On dirfrags
// ===========
// In order to insert something into a directory, we first (ideally)
// need to know the fragtree for the directory. Sometimes we can't
// get that, in which case we just go ahead and insert it into
// fragment zero for a good chance of that being the right thing
// anyway (most moderate-sized dirs aren't fragmented!)
// On ancestry
// ===========
// My immediate ancestry should be correct, so if we can find that
// directory's dirfrag then go inject it there. This works well
// in the case that this inode's dentry was somehow lost and we
// are recreating it, because the rest of the hierarchy
// will probably still exist.
//
// It's more of a "better than nothing" approach when rebuilding
// a whole tree, as backtraces will in general not be up to date
// beyond the first parent, if anything in the trace was ever
// moved after the file was created.
// On inode numbers
// ================
// The backtrace tells us inodes for each of the parents. If we are
// creating those parent dirfrags, then there is a risk that somehow
// the inode indicated here was also used for data (not a dirfrag) at
// some stage. That would be a zany situation, and we don't check
// for it here, because to do so would require extra IOs for everything
// we inject, and anyway wouldn't guarantee that the inode number
// wasn't in use in some dentry elsewhere in the metadata tree that
// just happened not to have any data objects.
// On multiple workers touching the same traces
// ============================================
// When creating linkage for a directory, *only* create it if we are
// also creating the object. That way, we might not manage to get the
// *right* linkage for a directory, but at least we won't multiply link
// it. We assume that if a root dirfrag exists for a directory, then
// it is linked somewhere (i.e. that the metadata pool is not already
// inconsistent).
//
// Making sure *that* is true is someone else's job! Probably someone
// who is not going to run in parallel, so that they can self-consistently
// look at versions and move things around as they go.
// Note this isn't 100% safe: if we die immediately after creating dirfrag
// object, next run will fail to create linkage for the dirfrag object
// and leave it orphaned.
inodeno_t ino = backtrace.ino;
dout(10) << " inode: 0x" << std::hex << ino << std::dec << dendl;
for (std::vector<inode_backpointer_t>::const_iterator i = backtrace.ancestors.begin();
i != backtrace.ancestors.end(); ++i) {
const inode_backpointer_t &backptr = *i;
dout(10) << " backptr: 0x" << std::hex << backptr.dirino << std::dec
<< "/" << backptr.dname << dendl;
// Examine root dirfrag for parent
const inodeno_t parent_ino = backptr.dirino;
const std::string dname = backptr.dname;
frag_t fragment;
int r = get_frag_of(parent_ino, dname, &fragment);
if (r == -ENOENT) {
// Don't know fragment, fall back to assuming root
dout(20) << "don't know fragment for 0x" << std::hex <<
parent_ino << std::dec << "/" << dname << ", will insert to root"
<< dendl;
}
// Find or create dirfrag
// ======================
bool created_dirfrag;
r = find_or_create_dirfrag(parent_ino, fragment, &created_dirfrag);
if (r < 0) {
return r;
}
// Check if dentry already exists
// ==============================
InodeStore existing_dentry;
r = read_dentry(parent_ino, fragment, dname, &existing_dentry);
bool write_dentry = false;
if (r == -ENOENT || r == -EINVAL) {
if (r == -EINVAL && !force_corrupt) {
return r;
}
// Missing or corrupt dentry
write_dentry = true;
} else if (r < 0) {
derr << "Unexpected error reading dentry 0x" << std::hex
<< parent_ino << std::dec << "/"
<< dname << ": " << cpp_strerror(r) << dendl;
break;
} else {
// Dentry already present, does it link to me?
if (existing_dentry.inode.ino == ino) {
dout(20) << "Dentry 0x" << std::hex
<< parent_ino << std::dec << "/"
<< dname << " already exists and points to me" << dendl;
} else {
derr << "Dentry 0x" << std::hex
<< parent_ino << std::dec << "/"
<< dname << " already exists but points to 0x"
<< std::hex << existing_dentry.inode.ino << std::dec << dendl;
// Fall back to lost+found!
return inject_lost_and_found(backtrace.ino, file_size, file_mtime,
layout);
}
}
// Inject linkage
// ==============
if (write_dentry) {
InodeStore dentry;
if (i == backtrace.ancestors.begin()) {
// This is the linkage for a file
dentry.inode.mode = 0500 | S_IFREG;
dout(10) << "Linking inode 0x" << std::hex << ino
<< " at 0x" << parent_ino << "/" << dname << std::dec
<< " with size=" << file_size << " bytes" << dendl;
// The file size and mtime we learned by scanning globally
dentry.inode.size = file_size;
dentry.inode.max_size_ever = file_size;
dentry.inode.mtime.tv.tv_sec = file_mtime;
dentry.inode.atime.tv.tv_sec = file_mtime;
dentry.inode.ctime.tv.tv_sec = file_mtime;
dentry.inode.layout = layout;
dentry.inode.truncate_seq = 1;
dentry.inode.truncate_size = -1ull;
dentry.inode.inline_data.version = CEPH_INLINE_NONE;
} else {
// This is the linkage for a directory
dentry.inode.mode = 0755 | S_IFDIR;
// Set nfiles to something non-zero, to fool any other code
// that tries to ignore 'empty' directories. This won't be
// accurate, but it should avoid functional issues.
dentry.inode.dirstat.nfiles = 1;
dentry.inode.size = 1;
}
dentry.inode.nlink = 1;
dentry.inode.ino = ino;
dentry.inode.uid = g_conf->mds_root_ino_uid;
dentry.inode.gid = g_conf->mds_root_ino_gid;
dentry.inode.version = 1;
dentry.inode.backtrace_version = 1;
r = inject_linkage(parent_ino, dname, fragment, dentry);
if (r < 0) {
return r;
}
}
if (!created_dirfrag) {
// If the parent dirfrag already existed, then stop traversing the
// backtrace: assume that the other ancestors already exist too. This
// is an assumption rather than a truth, but it's a convenient way
// to avoid the risk of creating multiply-linked directories while
// injecting data. If there are in fact missing ancestors, this
// should be fixed up using a separate tool scanning the metadata
// pool.
break;
} else {
// Proceed up the backtrace, creating parents
ino = parent_ino;
}
}
return 0;
}
int MetadataDriver::find_or_create_dirfrag(
inodeno_t ino,
frag_t fragment,
bool *created)
{
assert(created != NULL);
fnode_t existing_fnode;
*created = false;
uint64_t read_version = 0;
int r = read_fnode(ino, fragment, &existing_fnode, &read_version);
dout(10) << "read_version = " << read_version << dendl;
if (r == -ENOENT || r == -EINVAL) {
if (r == -EINVAL && !force_corrupt) {
return r;
}
// Missing or corrupt fnode, create afresh
bufferlist fnode_bl;
fnode_t blank_fnode;
blank_fnode.version = 1;
blank_fnode.damage_flags |= (DAMAGE_RSTATS | DAMAGE_RSTATS);
blank_fnode.encode(fnode_bl);
librados::ObjectWriteOperation op;
if (read_version) {
assert(r == -EINVAL);
// Case A: We must assert that the version isn't changed since we saw the object
// was unreadable, to avoid the possibility of two data-scan processes
// both creating the frag.
op.assert_version(read_version);
} else {
assert(r == -ENOENT);
// Case B: The object didn't exist in read_fnode, so while creating it we must
// use an exclusive create to correctly populate *creating with
// whether we created it ourselves or someone beat us to it.
op.create(true);
}
object_t frag_oid = InodeStore::get_object_name(ino, fragment, "");
op.omap_set_header(fnode_bl);
r = metadata_io.operate(frag_oid.name, &op);
if (r == -EOVERFLOW) {
// Someone else wrote it (see case A above)
dout(10) << "Dirfrag creation race: 0x" << std::hex
<< ino << " " << fragment << std::dec << dendl;
*created = false;
return 0;
} else if (r < 0) {
// We were unable to create or write it, error out
derr << "Failed to create dirfrag 0x" << std::hex
<< ino << std::dec << ": " << cpp_strerror(r) << dendl;
return r;
} else {
// Success: the dirfrag object now exists with a value header
dout(10) << "Created dirfrag: 0x" << std::hex
<< ino << std::dec << dendl;
*created = true;
}
} else if (r < 0) {
derr << "Unexpected error reading dirfrag 0x" << std::hex
<< ino << std::dec << " : " << cpp_strerror(r) << dendl;
return r;
} else {
dout(20) << "Dirfrag already exists: 0x" << std::hex
<< ino << " " << fragment << std::dec << dendl;
}
return 0;
}
int MetadataDriver::inject_linkage(
inodeno_t dir_ino, const std::string &dname,
const frag_t fragment, const InodeStore &inode)
{
// We have no information about snapshots, so everything goes
// in as CEPH_NOSNAP
snapid_t snap = CEPH_NOSNAP;
object_t frag_oid = InodeStore::get_object_name(dir_ino, fragment, "");
std::string key;
dentry_key_t dn_key(snap, dname.c_str());
dn_key.encode(key);
bufferlist dentry_bl;
::encode(snap, dentry_bl);
::encode('I', dentry_bl);
inode.encode_bare(dentry_bl);
// Write out
std::map<std::string, bufferlist> vals;
vals[key] = dentry_bl;
int r = metadata_io.omap_set(frag_oid.name, vals);
if (r != 0) {
derr << "Error writing dentry 0x" << std::hex
<< dir_ino << std::dec << "/"
<< dname << ": " << cpp_strerror(r) << dendl;
return r;
} else {
dout(20) << "Injected dentry 0x" << std::hex
<< dir_ino << "/" << dname << " pointing to 0x"
<< inode.inode.ino << std::dec << dendl;
return 0;
}
}
int MetadataDriver::init(librados::Rados &rados, const MDSMap *mdsmap)
{
int const metadata_pool_id = mdsmap->get_metadata_pool();
dout(4) << "resolving metadata pool " << metadata_pool_id << dendl;
std::string metadata_pool_name;
int r = rados.pool_reverse_lookup(metadata_pool_id, &metadata_pool_name);
if (r < 0) {
derr << "Pool " << metadata_pool_id
<< " identified in MDS map not found in RADOS!" << dendl;
return r;
}
dout(4) << "found metadata pool '" << metadata_pool_name << "'" << dendl;
return rados.ioctx_create(metadata_pool_name.c_str(), metadata_io);
}
int LocalFileDriver::init(librados::Rados &rados, const MDSMap *mdsmap)
{
return 0;
}
int LocalFileDriver::inject_data(
const std::string &file_path,
uint64_t size,
uint32_t chunk_size,
inodeno_t ino)
{
// Scrape the file contents out of the data pool and into the
// local filesystem
std::fstream f;
f.open(file_path.c_str(), std::fstream::out | std::fstream::binary);
for (uint64_t offset = 0; offset < size; offset += chunk_size) {
bufferlist bl;
char buf[32];
snprintf(buf, sizeof(buf),
"%llx.%08llx",
(unsigned long long)ino,
(unsigned long long)(offset / chunk_size));
std::string oid(buf);
int r = data_io.read(oid, bl, chunk_size, 0);
if (r <= 0 && r != -ENOENT) {
derr << "error reading data object '" << oid << "': "
<< cpp_strerror(r) << dendl;
f.close();
return r;
} else if (r >=0) {
f.seekp(offset);
bl.write_stream(f);
}
}
f.close();
return 0;
}
int LocalFileDriver::inject_with_backtrace(
const inode_backtrace_t &bt,
uint64_t size,
time_t mtime,
const ceph_file_layout &layout)
{
std::string path_builder = path;
// Iterate through backtrace creating directory parents
std::vector<inode_backpointer_t>::const_reverse_iterator i;
for (i = bt.ancestors.rbegin();
i != bt.ancestors.rend(); ++i) {
const inode_backpointer_t &backptr = *i;
path_builder += "/";
path_builder += backptr.dname;
// Last entry is the filename itself
bool is_file = (i + 1 == bt.ancestors.rend());
if (is_file) {
// FIXME: inject_data won't cope with interesting (i.e. striped)
// layouts (need a librados-compatible Filer to read these)
inject_data(path_builder, size, layout.fl_object_size, bt.ino);
} else {
int r = mkdir(path_builder.c_str(), 0755);
if (r != 0 && r != -EPERM) {
derr << "error creating directory: '" << path_builder << "': "
<< cpp_strerror(r) << dendl;
return r;
}
}
}
return 0;
}
int LocalFileDriver::inject_lost_and_found(
inodeno_t ino,
uint64_t size,
time_t mtime,
const ceph_file_layout &layout)
{
std::string lf_path = path + "/lost+found";
int r = mkdir(lf_path.c_str(), 0755);
if (r != 0 && r != -EPERM) {
derr << "error creating directory: '" << lf_path << "': "
<< cpp_strerror(r) << dendl;
return r;
}
std::string file_path = lf_path + "/" + lost_found_dname(ino);
return inject_data(file_path, size, layout.fl_object_size, ino);
}
int LocalFileDriver::init_roots(int64_t data_pool_id)
{
// Ensure that the path exists and is a directory
bool exists;
int r = check_roots(&exists);
if (r != 0) {
return r;
}
if (exists) {
return 0;
} else {
return ::mkdir(path.c_str(), 0755);
}
}
int LocalFileDriver::check_roots(bool *result)
{
// Check if the path exists and is a directory
DIR *d = ::opendir(path.c_str());
if (d == NULL) {
*result = false;
} else {
int r = closedir(d);
if (r != 0) {
// Weird, but maybe possible with e.g. stale FD on NFS mount?
*result = false;
} else {
*result = true;
}
}
return 0;
}
|