-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
FileStore.cc
6398 lines (5653 loc) · 178 KB
/
FileStore.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
* Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
*
* 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 "include/compat.h"
#include "include/int_types.h"
#include "boost/tuple/tuple.hpp"
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>
#include <errno.h>
#include <dirent.h>
#include <sys/ioctl.h>
#if defined(__linux__)
#include <linux/fs.h>
#include <linux/falloc.h>
#endif
#include <iostream>
#include <map>
#include "include/linux_fiemap.h"
#include "common/xattr.h"
#include "chain_xattr.h"
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/mount.h>
#endif
#include <fstream>
#include <sstream>
#include "FileStore.h"
#include "GenericFileStoreBackend.h"
#include "BtrfsFileStoreBackend.h"
#include "XfsFileStoreBackend.h"
#include "ZFSFileStoreBackend.h"
#include "common/BackTrace.h"
#include "include/types.h"
#include "FileJournal.h"
#include "osd/osd_types.h"
#include "include/color.h"
#include "include/buffer.h"
#include "common/Timer.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/run_cmd.h"
#include "common/safe_io.h"
#include "common/perf_counters.h"
#include "common/sync_filesystem.h"
#include "common/fd.h"
#include "HashIndex.h"
#include "DBObjectMap.h"
#include "kv/KeyValueDB.h"
#include "common/ceph_crypto.h"
using ceph::crypto::SHA1;
#include "include/ceph_assert.h"
#include "common/config.h"
#include "common/blkdev.h"
#ifdef WITH_LTTNG
#define TRACEPOINT_DEFINE
#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
#include "tracing/objectstore.h"
#undef TRACEPOINT_PROBE_DYNAMIC_LINKAGE
#undef TRACEPOINT_DEFINE
#else
#define tracepoint(...)
#endif
#define dout_context cct
#define dout_subsys ceph_subsys_filestore
#undef dout_prefix
#define dout_prefix *_dout << "filestore(" << basedir << ") "
#define COMMIT_SNAP_ITEM "snap_%llu"
#define CLUSTER_SNAP_ITEM "clustersnap_%s"
#define REPLAY_GUARD_XATTR "user.cephos.seq"
#define GLOBAL_REPLAY_GUARD_XATTR "user.cephos.gseq"
// XATTR_SPILL_OUT_NAME as a xattr is used to maintain that indicates whether
// xattrs spill over into DBObjectMap, if XATTR_SPILL_OUT_NAME exists in file
// xattrs and the value is "no", it indicates no xattrs in DBObjectMap
#define XATTR_SPILL_OUT_NAME "user.cephos.spill_out"
#define XATTR_NO_SPILL_OUT "0"
#define XATTR_SPILL_OUT "1"
#define __FUNC__ __func__ << "(" << __LINE__ << ")"
//Initial features in new superblock.
static CompatSet get_fs_initial_compat_set() {
CompatSet::FeatureSet ceph_osd_feature_compat;
CompatSet::FeatureSet ceph_osd_feature_ro_compat;
CompatSet::FeatureSet ceph_osd_feature_incompat;
return CompatSet(ceph_osd_feature_compat, ceph_osd_feature_ro_compat,
ceph_osd_feature_incompat);
}
//Features are added here that this FileStore supports.
static CompatSet get_fs_supported_compat_set() {
CompatSet compat = get_fs_initial_compat_set();
//Any features here can be set in code, but not in initial superblock
compat.incompat.insert(CEPH_FS_FEATURE_INCOMPAT_SHARDS);
return compat;
}
int FileStore::validate_hobject_key(const hobject_t &obj) const
{
unsigned len = LFNIndex::get_max_escaped_name_len(obj);
return len > m_filestore_max_xattr_value_size ? -ENAMETOOLONG : 0;
}
int FileStore::get_block_device_fsid(CephContext* cct, const string& path,
uuid_d *fsid)
{
// make sure we don't try to use aio or direct_io (and get annoying
// error messages from failing to do so); performance implications
// should be irrelevant for this use
FileJournal j(cct, *fsid, 0, 0, path.c_str(), false, false);
return j.peek_fsid(*fsid);
}
void FileStore::FSPerfTracker::update_from_perfcounters(
PerfCounters &logger)
{
os_commit_latency_ns.consume_next(
logger.get_tavg_ns(
l_filestore_journal_latency));
os_apply_latency_ns.consume_next(
logger.get_tavg_ns(
l_filestore_apply_latency));
}
ostream& operator<<(ostream& out, const FileStore::OpSequencer& s)
{
return out << "osr(" << s.cid << ")";
}
int FileStore::get_cdir(const coll_t& cid, char *s, int len)
{
const string &cid_str(cid.to_str());
return snprintf(s, len, "%s/current/%s", basedir.c_str(), cid_str.c_str());
}
int FileStore::get_index(const coll_t& cid, Index *index)
{
int r = index_manager.get_index(cid, basedir, index);
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
int FileStore::init_index(const coll_t& cid)
{
char path[PATH_MAX];
get_cdir(cid, path, sizeof(path));
int r = index_manager.init_index(cid, path, target_version);
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
int FileStore::lfn_find(const ghobject_t& oid, const Index& index, IndexedPath *path)
{
IndexedPath path2;
if (!path)
path = &path2;
int r, exist;
ceph_assert(index.index);
r = (index.index)->lookup(oid, path, &exist);
if (r < 0) {
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
if (!exist)
return -ENOENT;
return 0;
}
int FileStore::lfn_truncate(const coll_t& cid, const ghobject_t& oid, off_t length)
{
FDRef fd;
int r = lfn_open(cid, oid, false, &fd);
if (r < 0)
return r;
r = ::ftruncate(**fd, length);
if (r < 0)
r = -errno;
if (r >= 0 && m_filestore_sloppy_crc) {
int rc = backend->_crc_update_truncate(**fd, length);
ceph_assert(rc >= 0);
}
lfn_close(fd);
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
int FileStore::lfn_stat(const coll_t& cid, const ghobject_t& oid, struct stat *buf)
{
IndexedPath path;
Index index;
int r = get_index(cid, &index);
if (r < 0)
return r;
ceph_assert(index.index);
RWLock::RLocker l((index.index)->access_lock);
r = lfn_find(oid, index, &path);
if (r < 0)
return r;
r = ::stat(path->path(), buf);
if (r < 0)
r = -errno;
return r;
}
int FileStore::lfn_open(const coll_t& cid,
const ghobject_t& oid,
bool create,
FDRef *outfd,
Index *index)
{
ceph_assert(outfd);
int r = 0;
bool need_lock = true;
int flags = O_RDWR;
if (create)
flags |= O_CREAT;
if (cct->_conf->filestore_odsync_write) {
flags |= O_DSYNC;
}
Index index2;
if (!index) {
index = &index2;
}
if (!((*index).index)) {
r = get_index(cid, index);
if (r < 0) {
dout(10) << __FUNC__ << ": could not get index r = " << r << dendl;
return r;
}
} else {
need_lock = false;
}
int fd, exist;
ceph_assert((*index).index);
if (need_lock) {
((*index).index)->access_lock.get_write();
}
if (!replaying) {
*outfd = fdcache.lookup(oid);
if (*outfd) {
if (need_lock) {
((*index).index)->access_lock.put_write();
}
return 0;
}
}
IndexedPath path2;
IndexedPath *path = &path2;
r = (*index)->lookup(oid, path, &exist);
if (r < 0) {
derr << "could not find " << oid << " in index: "
<< cpp_strerror(-r) << dendl;
goto fail;
}
r = ::open((*path)->path(), flags|O_CLOEXEC, 0644);
if (r < 0) {
r = -errno;
dout(10) << "error opening file " << (*path)->path() << " with flags="
<< flags << ": " << cpp_strerror(-r) << dendl;
goto fail;
}
fd = r;
if (create && (!exist)) {
r = (*index)->created(oid, (*path)->path());
if (r < 0) {
VOID_TEMP_FAILURE_RETRY(::close(fd));
derr << "error creating " << oid << " (" << (*path)->path()
<< ") in index: " << cpp_strerror(-r) << dendl;
goto fail;
}
r = chain_fsetxattr<true, true>(
fd, XATTR_SPILL_OUT_NAME,
XATTR_NO_SPILL_OUT, sizeof(XATTR_NO_SPILL_OUT));
if (r < 0) {
VOID_TEMP_FAILURE_RETRY(::close(fd));
derr << "error setting spillout xattr for oid " << oid << " (" << (*path)->path()
<< "):" << cpp_strerror(-r) << dendl;
goto fail;
}
}
if (!replaying) {
bool existed;
*outfd = fdcache.add(oid, fd, &existed);
if (existed) {
TEMP_FAILURE_RETRY(::close(fd));
}
} else {
*outfd = std::make_shared<FDCache::FD>(fd);
}
if (need_lock) {
((*index).index)->access_lock.put_write();
}
return 0;
fail:
if (need_lock) {
((*index).index)->access_lock.put_write();
}
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
void FileStore::lfn_close(FDRef fd)
{
}
int FileStore::lfn_link(const coll_t& c, const coll_t& newcid, const ghobject_t& o, const ghobject_t& newoid)
{
Index index_new, index_old;
IndexedPath path_new, path_old;
int exist;
int r;
bool index_same = false;
if (c < newcid) {
r = get_index(newcid, &index_new);
if (r < 0)
return r;
r = get_index(c, &index_old);
if (r < 0)
return r;
} else if (c == newcid) {
r = get_index(c, &index_old);
if (r < 0)
return r;
index_new = index_old;
index_same = true;
} else {
r = get_index(c, &index_old);
if (r < 0)
return r;
r = get_index(newcid, &index_new);
if (r < 0)
return r;
}
ceph_assert(index_old.index);
ceph_assert(index_new.index);
if (!index_same) {
RWLock::RLocker l1((index_old.index)->access_lock);
r = index_old->lookup(o, &path_old, &exist);
if (r < 0) {
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
if (!exist)
return -ENOENT;
RWLock::WLocker l2((index_new.index)->access_lock);
r = index_new->lookup(newoid, &path_new, &exist);
if (r < 0) {
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
if (exist)
return -EEXIST;
dout(25) << __FUNC__ << ": path_old: " << path_old << dendl;
dout(25) << __FUNC__ << ": path_new: " << path_new << dendl;
r = ::link(path_old->path(), path_new->path());
if (r < 0)
return -errno;
r = index_new->created(newoid, path_new->path());
if (r < 0) {
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
} else {
RWLock::WLocker l1((index_old.index)->access_lock);
r = index_old->lookup(o, &path_old, &exist);
if (r < 0) {
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
if (!exist)
return -ENOENT;
r = index_new->lookup(newoid, &path_new, &exist);
if (r < 0) {
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
if (exist)
return -EEXIST;
dout(25) << __FUNC__ << ": path_old: " << path_old << dendl;
dout(25) << __FUNC__ << ": path_new: " << path_new << dendl;
r = ::link(path_old->path(), path_new->path());
if (r < 0)
return -errno;
// make sure old fd for unlinked/overwritten file is gone
fdcache.clear(newoid);
r = index_new->created(newoid, path_new->path());
if (r < 0) {
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
}
return 0;
}
int FileStore::lfn_unlink(const coll_t& cid, const ghobject_t& o,
const SequencerPosition &spos,
bool force_clear_omap)
{
Index index;
int r = get_index(cid, &index);
if (r < 0) {
dout(25) << __FUNC__ << ": get_index failed " << cpp_strerror(r) << dendl;
return r;
}
ceph_assert(index.index);
RWLock::WLocker l((index.index)->access_lock);
{
IndexedPath path;
int hardlink;
r = index->lookup(o, &path, &hardlink);
if (r < 0) {
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
if (!force_clear_omap) {
if (hardlink == 0 || hardlink == 1) {
force_clear_omap = true;
}
}
if (force_clear_omap) {
dout(20) << __FUNC__ << ": clearing omap on " << o
<< " in cid " << cid << dendl;
r = object_map->clear(o, &spos);
if (r < 0 && r != -ENOENT) {
dout(25) << __FUNC__ << ": omap clear failed " << cpp_strerror(r) << dendl;
ceph_assert(!m_filestore_fail_eio || r != -EIO);
return r;
}
if (cct->_conf->filestore_debug_inject_read_err) {
debug_obj_on_delete(o);
}
if (!m_disable_wbthrottle) {
wbthrottle.clear_object(o); // should be only non-cache ref
}
fdcache.clear(o);
} else {
/* Ensure that replay of this op doesn't result in the object_map
* going away.
*/
if (!backend->can_checkpoint())
object_map->sync(&o, &spos);
}
if (hardlink == 0) {
if (!m_disable_wbthrottle) {
wbthrottle.clear_object(o); // should be only non-cache ref
}
return 0;
}
}
r = index->unlink(o);
if (r < 0) {
dout(25) << __FUNC__ << ": index unlink failed " << cpp_strerror(r) << dendl;
return r;
}
return 0;
}
FileStore::FileStore(CephContext* cct, const std::string &base,
const std::string &jdev, osflagbits_t flags,
const char *name, bool do_update) :
JournalingObjectStore(cct, base),
internal_name(name),
basedir(base), journalpath(jdev),
generic_flags(flags),
blk_size(0),
fsid_fd(-1), op_fd(-1),
basedir_fd(-1), current_fd(-1),
backend(nullptr),
index_manager(cct, do_update),
lock("FileStore::lock"),
force_sync(false),
sync_entry_timeo_lock("FileStore::sync_entry_timeo_lock"),
timer(cct, sync_entry_timeo_lock),
stop(false), sync_thread(this),
coll_lock("FileStore::coll_lock"),
fdcache(cct),
wbthrottle(cct),
next_osr_id(0),
m_disable_wbthrottle(cct->_conf->filestore_odsync_write ||
!cct->_conf->filestore_wbthrottle_enable),
throttle_ops(cct, "filestore_ops", cct->_conf->filestore_caller_concurrency),
throttle_bytes(cct, "filestore_bytes", cct->_conf->filestore_caller_concurrency),
m_ondisk_finisher_num(cct->_conf->filestore_ondisk_finisher_threads),
m_apply_finisher_num(cct->_conf->filestore_apply_finisher_threads),
op_tp(cct, "FileStore::op_tp", "tp_fstore_op", cct->_conf->filestore_op_threads, "filestore_op_threads"),
op_wq(this, cct->_conf->filestore_op_thread_timeout,
cct->_conf->filestore_op_thread_suicide_timeout, &op_tp),
logger(nullptr),
trace_endpoint("0.0.0.0", 0, "FileStore"),
read_error_lock("FileStore::read_error_lock"),
m_filestore_commit_timeout(cct->_conf->filestore_commit_timeout),
m_filestore_journal_parallel(cct->_conf->filestore_journal_parallel ),
m_filestore_journal_trailing(cct->_conf->filestore_journal_trailing),
m_filestore_journal_writeahead(cct->_conf->filestore_journal_writeahead),
m_filestore_fiemap_threshold(cct->_conf->filestore_fiemap_threshold),
m_filestore_max_sync_interval(cct->_conf->filestore_max_sync_interval),
m_filestore_min_sync_interval(cct->_conf->filestore_min_sync_interval),
m_filestore_fail_eio(cct->_conf->filestore_fail_eio),
m_filestore_fadvise(cct->_conf->filestore_fadvise),
do_update(do_update),
m_journal_dio(cct->_conf->journal_dio),
m_journal_aio(cct->_conf->journal_aio),
m_journal_force_aio(cct->_conf->journal_force_aio),
m_osd_rollback_to_cluster_snap(cct->_conf->osd_rollback_to_cluster_snap),
m_osd_use_stale_snap(cct->_conf->osd_use_stale_snap),
m_filestore_do_dump(false),
m_filestore_dump_fmt(true),
m_filestore_sloppy_crc(cct->_conf->filestore_sloppy_crc),
m_filestore_sloppy_crc_block_size(cct->_conf->filestore_sloppy_crc_block_size),
m_filestore_max_alloc_hint_size(cct->_conf->filestore_max_alloc_hint_size),
m_fs_type(0),
m_filestore_max_inline_xattr_size(0),
m_filestore_max_inline_xattrs(0),
m_filestore_max_xattr_value_size(0)
{
m_filestore_kill_at = cct->_conf->filestore_kill_at;
for (int i = 0; i < m_ondisk_finisher_num; ++i) {
ostringstream oss;
oss << "filestore-ondisk-" << i;
Finisher *f = new Finisher(cct, oss.str(), "fn_odsk_fstore");
ondisk_finishers.push_back(f);
}
for (int i = 0; i < m_apply_finisher_num; ++i) {
ostringstream oss;
oss << "filestore-apply-" << i;
Finisher *f = new Finisher(cct, oss.str(), "fn_appl_fstore");
apply_finishers.push_back(f);
}
ostringstream oss;
oss << basedir << "/current";
current_fn = oss.str();
ostringstream sss;
sss << basedir << "/current/commit_op_seq";
current_op_seq_fn = sss.str();
ostringstream omss;
if (cct->_conf->filestore_omap_backend_path != "") {
omap_dir = cct->_conf->filestore_omap_backend_path;
} else {
omss << basedir << "/current/omap";
omap_dir = omss.str();
}
// initialize logger
PerfCountersBuilder plb(cct, internal_name, l_filestore_first, l_filestore_last);
plb.add_u64(l_filestore_journal_queue_ops, "journal_queue_ops", "Operations in journal queue");
plb.add_u64(l_filestore_journal_ops, "journal_ops", "Active journal entries to be applied");
plb.add_u64(l_filestore_journal_queue_bytes, "journal_queue_bytes", "Size of journal queue");
plb.add_u64(l_filestore_journal_bytes, "journal_bytes", "Active journal operation size to be applied");
plb.add_time_avg(l_filestore_journal_latency, "journal_latency", "Average journal queue completing latency",
NULL, PerfCountersBuilder::PRIO_USEFUL);
plb.add_u64_counter(l_filestore_journal_wr, "journal_wr", "Journal write IOs");
plb.add_u64_avg(l_filestore_journal_wr_bytes, "journal_wr_bytes", "Journal data written");
plb.add_u64(l_filestore_op_queue_max_ops, "op_queue_max_ops", "Max operations in writing to FS queue");
plb.add_u64(l_filestore_op_queue_ops, "op_queue_ops", "Operations in writing to FS queue");
plb.add_u64_counter(l_filestore_ops, "ops", "Operations written to store");
plb.add_u64(l_filestore_op_queue_max_bytes, "op_queue_max_bytes", "Max data in writing to FS queue");
plb.add_u64(l_filestore_op_queue_bytes, "op_queue_bytes", "Size of writing to FS queue");
plb.add_u64_counter(l_filestore_bytes, "bytes", "Data written to store");
plb.add_time_avg(l_filestore_apply_latency, "apply_latency", "Apply latency");
plb.add_u64(l_filestore_committing, "committing", "Is currently committing");
plb.add_u64_counter(l_filestore_commitcycle, "commitcycle", "Commit cycles");
plb.add_time_avg(l_filestore_commitcycle_interval, "commitcycle_interval", "Average interval between commits");
plb.add_time_avg(l_filestore_commitcycle_latency, "commitcycle_latency", "Average latency of commit");
plb.add_u64_counter(l_filestore_journal_full, "journal_full", "Journal writes while full");
plb.add_time_avg(l_filestore_queue_transaction_latency_avg, "queue_transaction_latency_avg",
"Store operation queue latency", NULL, PerfCountersBuilder::PRIO_USEFUL);
plb.add_time(l_filestore_sync_pause_max_lat, "sync_pause_max_latency", "Max latency of op_wq pause before syncfs");
logger = plb.create_perf_counters();
cct->get_perfcounters_collection()->add(logger);
cct->_conf.add_observer(this);
superblock.compat_features = get_fs_initial_compat_set();
}
FileStore::~FileStore()
{
for (vector<Finisher*>::iterator it = ondisk_finishers.begin(); it != ondisk_finishers.end(); ++it) {
delete *it;
*it = nullptr;
}
for (vector<Finisher*>::iterator it = apply_finishers.begin(); it != apply_finishers.end(); ++it) {
delete *it;
*it = nullptr;
}
cct->_conf.remove_observer(this);
cct->get_perfcounters_collection()->remove(logger);
if (journal)
journal->logger = nullptr;
delete logger;
logger = nullptr;
if (m_filestore_do_dump) {
dump_stop();
}
}
static void get_attrname(const char *name, char *buf, int len)
{
snprintf(buf, len, "user.ceph.%s", name);
}
bool parse_attrname(char **name)
{
if (strncmp(*name, "user.ceph.", 10) == 0) {
*name += 10;
return true;
}
return false;
}
void FileStore::collect_metadata(map<string,string> *pm)
{
char partition_path[PATH_MAX];
char dev_node[PATH_MAX];
(*pm)["filestore_backend"] = backend->get_name();
ostringstream ss;
ss << "0x" << std::hex << m_fs_type << std::dec;
(*pm)["filestore_f_type"] = ss.str();
if (cct->_conf->filestore_collect_device_partition_information) {
int rc = 0;
BlkDev blkdev(fsid_fd);
if (rc = blkdev.partition(partition_path, PATH_MAX); rc) {
(*pm)["backend_filestore_partition_path"] = "unknown";
} else {
(*pm)["backend_filestore_partition_path"] = string(partition_path);
}
if (rc = blkdev.wholedisk(dev_node, PATH_MAX); rc) {
(*pm)["backend_filestore_dev_node"] = "unknown";
} else {
(*pm)["backend_filestore_dev_node"] = string(dev_node);
}
if (rc == 0 && vdo_fd >= 0) {
(*pm)["vdo"] = "true";
(*pm)["vdo_physical_size"] =
stringify(4096 * get_vdo_stat(vdo_fd, "physical_blocks"));
}
if (journal) {
journal->collect_metadata(pm);
}
}
}
int FileStore::get_devices(set<string> *ls)
{
char dev_node[PATH_MAX];
BlkDev blkdev(fsid_fd);
if (int rc = blkdev.wholedisk(dev_node, PATH_MAX); rc) {
return rc;
}
ls->insert(dev_node);
if (strncmp(dev_node, "dm-", 3) == 0) {
get_dm_parents(dev_node, ls);
}
if (journal) {
journal->get_devices(ls);
}
return 0;
}
int FileStore::statfs(struct store_statfs_t *buf0)
{
struct statfs buf;
buf0->reset();
if (::statfs(basedir.c_str(), &buf) < 0) {
int r = -errno;
ceph_assert(!m_filestore_fail_eio || r != -EIO);
ceph_assert(r != -ENOENT);
return r;
}
uint64_t bfree = buf.f_bavail * buf.f_bsize;
// assume all of leveldb/rocksdb is omap.
{
map<string,uint64_t> kv_usage;
buf0->omap_allocated += object_map->get_db()->get_estimated_size(kv_usage);
}
uint64_t thin_total, thin_avail;
if (get_vdo_utilization(vdo_fd, &thin_total, &thin_avail)) {
buf0->total = thin_total;
bfree = std::min(bfree, thin_avail);
buf0->allocated = thin_total - thin_avail;
buf0->data_stored = bfree;
} else {
buf0->total = buf.f_blocks * buf.f_bsize;
buf0->allocated = bfree;
buf0->data_stored = bfree;
}
buf0->available = bfree;
// FIXME: we don't know how to populate buf->internal_metadata; XFS doesn't
// tell us what its internal overhead is.
// Adjust for writes pending in the journal
if (journal) {
uint64_t estimate = journal->get_journal_size_estimate();
buf0->internally_reserved = estimate;
if (buf0->available > estimate)
buf0->available -= estimate;
else
buf0->available = 0;
}
return 0;
}
int FileStore::pool_statfs(uint64_t pool_id, struct store_statfs_t *buf)
{
return -ENOTSUP;
}
void FileStore::new_journal()
{
if (journalpath.length()) {
dout(10) << "open_journal at " << journalpath << dendl;
journal = new FileJournal(cct, fsid, &finisher, &sync_cond,
journalpath.c_str(),
m_journal_dio, m_journal_aio,
m_journal_force_aio);
if (journal)
journal->logger = logger;
}
return;
}
int FileStore::dump_journal(ostream& out)
{
int r;
if (!journalpath.length())
return -EINVAL;
FileJournal *journal = new FileJournal(cct, fsid, &finisher, &sync_cond, journalpath.c_str(), m_journal_dio);
r = journal->dump(out);
delete journal;
journal = nullptr;
return r;
}
FileStoreBackend *FileStoreBackend::create(unsigned long f_type, FileStore *fs)
{
switch (f_type) {
#if defined(__linux__)
case BTRFS_SUPER_MAGIC:
return new BtrfsFileStoreBackend(fs);
# ifdef HAVE_LIBXFS
case XFS_SUPER_MAGIC:
return new XfsFileStoreBackend(fs);
# endif
#endif
#ifdef HAVE_LIBZFS
case ZFS_SUPER_MAGIC:
return new ZFSFileStoreBackend(fs);
#endif
default:
return new GenericFileStoreBackend(fs);
}
}
void FileStore::create_backend(unsigned long f_type)
{
m_fs_type = f_type;
ceph_assert(!backend);
backend = FileStoreBackend::create(f_type, this);
dout(0) << "backend " << backend->get_name()
<< " (magic 0x" << std::hex << f_type << std::dec << ")"
<< dendl;
switch (f_type) {
#if defined(__linux__)
case BTRFS_SUPER_MAGIC:
if (!m_disable_wbthrottle){
wbthrottle.set_fs(WBThrottle::BTRFS);
}
break;
case XFS_SUPER_MAGIC:
// wbthrottle is constructed with fs(WBThrottle::XFS)
break;
#endif
}
set_xattr_limits_via_conf();
}
int FileStore::mkfs()
{
int ret = 0;
char fsid_fn[PATH_MAX];
char fsid_str[40];
uuid_d old_fsid;
uuid_d old_omap_fsid;
dout(1) << "mkfs in " << basedir << dendl;
basedir_fd = ::open(basedir.c_str(), O_RDONLY|O_CLOEXEC);
if (basedir_fd < 0) {
ret = -errno;
derr << __FUNC__ << ": failed to open base dir " << basedir << ": " << cpp_strerror(ret) << dendl;
return ret;
}
// open+lock fsid
snprintf(fsid_fn, sizeof(fsid_fn), "%s/fsid", basedir.c_str());
fsid_fd = ::open(fsid_fn, O_RDWR|O_CREAT|O_CLOEXEC, 0644);
if (fsid_fd < 0) {
ret = -errno;
derr << __FUNC__ << ": failed to open " << fsid_fn << ": " << cpp_strerror(ret) << dendl;
goto close_basedir_fd;
}
if (lock_fsid() < 0) {
ret = -EBUSY;
goto close_fsid_fd;
}
if (read_fsid(fsid_fd, &old_fsid) < 0 || old_fsid.is_zero()) {
if (fsid.is_zero()) {
fsid.generate_random();
dout(1) << __FUNC__ << ": generated fsid " << fsid << dendl;
} else {
dout(1) << __FUNC__ << ": using provided fsid " << fsid << dendl;
}
fsid.print(fsid_str);
strcat(fsid_str, "\n");
ret = ::ftruncate(fsid_fd, 0);
if (ret < 0) {
ret = -errno;
derr << __FUNC__ << ": failed to truncate fsid: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
ret = safe_write(fsid_fd, fsid_str, strlen(fsid_str));
if (ret < 0) {
derr << __FUNC__ << ": failed to write fsid: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
if (::fsync(fsid_fd) < 0) {
ret = -errno;
derr << __FUNC__ << ": close failed: can't write fsid: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
dout(10) << __FUNC__ << ": fsid is " << fsid << dendl;
} else {
if (!fsid.is_zero() && fsid != old_fsid) {
derr << __FUNC__ << ": on-disk fsid " << old_fsid << " != provided " << fsid << dendl;
ret = -EINVAL;
goto close_fsid_fd;
}
fsid = old_fsid;
dout(1) << __FUNC__ << ": fsid is already set to " << fsid << dendl;
}
// version stamp
ret = write_version_stamp();
if (ret < 0) {
derr << __FUNC__ << ": write_version_stamp() failed: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
// superblock
superblock.omap_backend = cct->_conf->filestore_omap_backend;
ret = write_superblock();
if (ret < 0) {
derr << __FUNC__ << ": write_superblock() failed: "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
struct statfs basefs;
ret = ::fstatfs(basedir_fd, &basefs);
if (ret < 0) {
ret = -errno;
derr << __FUNC__ << ": cannot fstatfs basedir "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
#if defined(__linux__)
if (basefs.f_type == BTRFS_SUPER_MAGIC &&
!g_ceph_context->check_experimental_feature_enabled("btrfs")) {
derr << __FUNC__ << ": deprecated btrfs support is not enabled" << dendl;
goto close_fsid_fd;
}
#endif
create_backend(basefs.f_type);
ret = backend->create_current();
if (ret < 0) {
derr << __FUNC__ << ": failed to create current/ " << cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
// write initial op_seq
{
uint64_t initial_seq = 0;
int fd = read_op_seq(&initial_seq);
if (fd < 0) {
ret = fd;
derr << __FUNC__ << ": failed to create " << current_op_seq_fn << ": "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
if (initial_seq == 0) {
ret = write_op_seq(fd, 1);
if (ret < 0) {
VOID_TEMP_FAILURE_RETRY(::close(fd));
derr << __FUNC__ << ": failed to write to " << current_op_seq_fn << ": "
<< cpp_strerror(ret) << dendl;
goto close_fsid_fd;
}
if (backend->can_checkpoint()) {
// create snap_1 too
current_fd = ::open(current_fn.c_str(), O_RDONLY|O_CLOEXEC);
ceph_assert(current_fd >= 0);
char s[NAME_MAX];
snprintf(s, sizeof(s), COMMIT_SNAP_ITEM, 1ull);
ret = backend->create_checkpoint(s, nullptr);