1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "boost/algorithm/string.hpp"
7 #include "common/debug.h"
8 #include "common/errno.h"
9 #include "common/perf_counters.h"
10 #include "BlockDevice.h"
11 #include "Allocator.h"
12 #include "include/assert.h"
14 #define dout_context cct
15 #define dout_subsys ceph_subsys_bluefs
17 #define dout_prefix *_dout << "bluefs "
19 MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::File
, bluefs_file
, bluefs
);
20 MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::Dir
, bluefs_dir
, bluefs
);
21 MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::FileWriter
, bluefs_file_writer
, bluefs
);
22 MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::FileReaderBuffer
,
23 bluefs_file_reader_buffer
, bluefs
);
24 MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::FileReader
, bluefs_file_reader
, bluefs
);
25 MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::FileLock
, bluefs_file_lock
, bluefs
);
28 BlueFS::BlueFS(CephContext
* cct
)
33 block_total(MAX_BDEV
, 0)
54 void BlueFS::_init_logger()
56 PerfCountersBuilder
b(cct
, "bluefs",
57 l_bluefs_first
, l_bluefs_last
);
58 b
.add_u64_counter(l_bluefs_gift_bytes
, "gift_bytes",
59 "Bytes gifted from BlueStore");
60 b
.add_u64_counter(l_bluefs_reclaim_bytes
, "reclaim_bytes",
61 "Bytes reclaimed by BlueStore");
62 b
.add_u64(l_bluefs_db_total_bytes
, "db_total_bytes",
63 "Total bytes (main db device)",
64 "b", PerfCountersBuilder::PRIO_USEFUL
);
65 b
.add_u64(l_bluefs_db_used_bytes
, "db_used_bytes",
66 "Used bytes (main db device)",
67 "u", PerfCountersBuilder::PRIO_USEFUL
);
68 b
.add_u64(l_bluefs_wal_total_bytes
, "wal_total_bytes",
69 "Total bytes (wal device)",
70 "walb", PerfCountersBuilder::PRIO_USEFUL
);
71 b
.add_u64(l_bluefs_wal_used_bytes
, "wal_used_bytes",
72 "Used bytes (wal device)",
73 "walu", PerfCountersBuilder::PRIO_USEFUL
);
74 b
.add_u64(l_bluefs_slow_total_bytes
, "slow_total_bytes",
75 "Total bytes (slow device)",
76 "slob", PerfCountersBuilder::PRIO_USEFUL
);
77 b
.add_u64(l_bluefs_slow_used_bytes
, "slow_used_bytes",
78 "Used bytes (slow device)",
79 "slou", PerfCountersBuilder::PRIO_USEFUL
);
80 b
.add_u64(l_bluefs_num_files
, "num_files", "File count",
81 "f", PerfCountersBuilder::PRIO_USEFUL
);
82 b
.add_u64(l_bluefs_log_bytes
, "log_bytes", "Size of the metadata log",
83 "jlen", PerfCountersBuilder::PRIO_INTERESTING
);
84 b
.add_u64_counter(l_bluefs_log_compactions
, "log_compactions",
85 "Compactions of the metadata log");
86 b
.add_u64_counter(l_bluefs_logged_bytes
, "logged_bytes",
87 "Bytes written to the metadata log", "j",
88 PerfCountersBuilder::PRIO_CRITICAL
);
89 b
.add_u64_counter(l_bluefs_files_written_wal
, "files_written_wal",
90 "Files written to WAL");
91 b
.add_u64_counter(l_bluefs_files_written_sst
, "files_written_sst",
92 "Files written to SSTs");
93 b
.add_u64_counter(l_bluefs_bytes_written_wal
, "bytes_written_wal",
94 "Bytes written to WAL", "wal",
95 PerfCountersBuilder::PRIO_CRITICAL
);
96 b
.add_u64_counter(l_bluefs_bytes_written_sst
, "bytes_written_sst",
97 "Bytes written to SSTs", "sst",
98 PerfCountersBuilder::PRIO_CRITICAL
);
99 logger
= b
.create_perf_counters();
100 cct
->get_perfcounters_collection()->add(logger
);
103 void BlueFS::_shutdown_logger()
105 cct
->get_perfcounters_collection()->remove(logger
);
109 void BlueFS::_update_logger_stats()
111 // we must be holding the lock
112 logger
->set(l_bluefs_num_files
, file_map
.size());
113 logger
->set(l_bluefs_log_bytes
, log_writer
->file
->fnode
.size
);
115 if (alloc
[BDEV_WAL
]) {
116 logger
->set(l_bluefs_wal_total_bytes
, block_total
[BDEV_WAL
]);
117 logger
->set(l_bluefs_wal_used_bytes
,
118 block_total
[BDEV_WAL
] - alloc
[BDEV_WAL
]->get_free());
120 if (alloc
[BDEV_DB
]) {
121 logger
->set(l_bluefs_db_total_bytes
, block_total
[BDEV_DB
]);
122 logger
->set(l_bluefs_db_used_bytes
,
123 block_total
[BDEV_DB
] - alloc
[BDEV_DB
]->get_free());
125 if (alloc
[BDEV_SLOW
]) {
126 logger
->set(l_bluefs_slow_total_bytes
, block_total
[BDEV_SLOW
]);
127 logger
->set(l_bluefs_slow_used_bytes
,
128 block_total
[BDEV_SLOW
] - alloc
[BDEV_SLOW
]->get_free());
132 int BlueFS::add_block_device(unsigned id
, const string
& path
)
134 dout(10) << __func__
<< " bdev " << id
<< " path " << path
<< dendl
;
135 assert(id
< bdev
.size());
136 assert(bdev
[id
] == NULL
);
137 BlockDevice
*b
= BlockDevice::create(cct
, path
, NULL
, NULL
);
138 int r
= b
->open(path
);
143 dout(1) << __func__
<< " bdev " << id
<< " path " << path
144 << " size " << pretty_si_t(b
->get_size()) << "B" << dendl
;
146 ioc
[id
] = new IOContext(cct
, NULL
);
150 bool BlueFS::bdev_support_label(unsigned id
)
152 assert(id
< bdev
.size());
154 return bdev
[id
]->supported_bdev_label();
157 uint64_t BlueFS::get_block_device_size(unsigned id
)
159 if (id
< bdev
.size() && bdev
[id
])
160 return bdev
[id
]->get_size();
164 void BlueFS::add_block_extent(unsigned id
, uint64_t offset
, uint64_t length
)
166 std::unique_lock
<std::mutex
> l(lock
);
167 dout(1) << __func__
<< " bdev " << id
168 << " 0x" << std::hex
<< offset
<< "~" << length
<< std::dec
170 assert(id
< bdev
.size());
172 assert(bdev
[id
]->get_size() >= offset
+ length
);
173 block_all
[id
].insert(offset
, length
);
174 block_total
[id
] += length
;
176 if (id
< alloc
.size() && alloc
[id
]) {
177 log_t
.op_alloc_add(id
, offset
, length
);
178 int r
= _flush_and_sync_log(l
);
180 alloc
[id
]->init_add_free(offset
, length
);
184 logger
->inc(l_bluefs_gift_bytes
, length
);
185 dout(10) << __func__
<< " done" << dendl
;
188 int BlueFS::reclaim_blocks(unsigned id
, uint64_t want
,
189 AllocExtentVector
*extents
)
191 std::unique_lock
<std::mutex
> l(lock
);
192 dout(1) << __func__
<< " bdev " << id
193 << " want 0x" << std::hex
<< want
<< std::dec
<< dendl
;
194 assert(id
< alloc
.size());
196 int r
= alloc
[id
]->reserve(want
);
197 assert(r
== 0); // caller shouldn't ask for more than they can get
198 int64_t got
= alloc
[id
]->allocate(want
, cct
->_conf
->bluefs_alloc_size
, 0,
200 if (got
< (int64_t)want
) {
201 alloc
[id
]->unreserve(want
- MAX(0, got
));
204 derr
<< __func__
<< " failed to allocate space to return to bluestore"
210 for (auto& p
: *extents
) {
211 block_all
[id
].erase(p
.offset
, p
.length
);
212 block_total
[id
] -= p
.length
;
213 log_t
.op_alloc_rm(id
, p
.offset
, p
.length
);
217 r
= _flush_and_sync_log(l
);
221 logger
->inc(l_bluefs_reclaim_bytes
, got
);
222 dout(1) << __func__
<< " bdev " << id
<< " want 0x" << std::hex
<< want
223 << " got " << *extents
<< dendl
;
227 uint64_t BlueFS::get_fs_usage()
229 std::lock_guard
<std::mutex
> l(lock
);
230 uint64_t total_bytes
= 0;
231 for (auto& p
: file_map
) {
232 total_bytes
+= p
.second
->fnode
.get_allocated();
237 uint64_t BlueFS::get_total(unsigned id
)
239 std::lock_guard
<std::mutex
> l(lock
);
240 assert(id
< block_all
.size());
241 return block_total
[id
];
244 uint64_t BlueFS::get_free(unsigned id
)
246 std::lock_guard
<std::mutex
> l(lock
);
247 assert(id
< alloc
.size());
248 return alloc
[id
]->get_free();
251 void BlueFS::dump_perf_counters(Formatter
*f
)
253 f
->open_object_section("bluefs_perf_counters");
254 logger
->dump_formatted(f
,0);
258 void BlueFS::dump_block_extents(ostream
& out
)
260 for (unsigned i
= 0; i
< MAX_BDEV
; ++i
) {
264 out
<< i
<< " : size 0x" << std::hex
<< bdev
[i
]->get_size()
265 << " : own 0x" << block_all
[i
] << std::dec
<< "\n";
269 void BlueFS::get_usage(vector
<pair
<uint64_t,uint64_t>> *usage
)
271 std::lock_guard
<std::mutex
> l(lock
);
272 usage
->resize(bdev
.size());
273 for (unsigned id
= 0; id
< bdev
.size(); ++id
) {
275 (*usage
)[id
] = make_pair(0, 0);
278 (*usage
)[id
].first
= alloc
[id
]->get_free();
279 (*usage
)[id
].second
= block_total
[id
];
281 (block_total
[id
] - (*usage
)[id
].first
) * 100 / block_total
[id
];
282 dout(10) << __func__
<< " bdev " << id
283 << " free " << (*usage
)[id
].first
284 << " (" << pretty_si_t((*usage
)[id
].first
) << "B)"
285 << " / " << (*usage
)[id
].second
286 << " (" << pretty_si_t((*usage
)[id
].second
) << "B)"
287 << ", used " << used
<< "%"
292 int BlueFS::get_block_extents(unsigned id
, interval_set
<uint64_t> *extents
)
294 std::lock_guard
<std::mutex
> l(lock
);
295 dout(10) << __func__
<< " bdev " << id
<< dendl
;
296 if (id
>= block_all
.size())
298 *extents
= block_all
[id
];
302 int BlueFS::mkfs(uuid_d osd_uuid
)
304 std::unique_lock
<std::mutex
> l(lock
);
306 << " osd_uuid " << osd_uuid
313 super
.block_size
= bdev
[BDEV_DB
]->get_block_size();
314 super
.osd_uuid
= osd_uuid
;
315 super
.uuid
.generate_random();
316 dout(1) << __func__
<< " uuid " << super
.uuid
<< dendl
;
319 FileRef log_file
= new File
;
320 log_file
->fnode
.ino
= 1;
321 log_file
->fnode
.prefer_bdev
= BDEV_WAL
;
323 log_file
->fnode
.prefer_bdev
,
324 cct
->_conf
->bluefs_max_log_runway
,
327 log_writer
= _create_writer(log_file
);
331 for (unsigned bdev
= 0; bdev
< MAX_BDEV
; ++bdev
) {
332 interval_set
<uint64_t>& p
= block_all
[bdev
];
335 for (interval_set
<uint64_t>::iterator q
= p
.begin(); q
!= p
.end(); ++q
) {
336 dout(20) << __func__
<< " op_alloc_add " << bdev
<< " 0x"
337 << std::hex
<< q
.get_start() << "~" << q
.get_len() << std::dec
339 log_t
.op_alloc_add(bdev
, q
.get_start(), q
.get_len());
342 _flush_and_sync_log(l
);
345 super
.log_fnode
= log_file
->fnode
;
350 super
= bluefs_super_t();
351 _close_writer(log_writer
);
358 dout(10) << __func__
<< " success" << dendl
;
362 void BlueFS::_init_alloc()
364 dout(20) << __func__
<< dendl
;
365 alloc
.resize(MAX_BDEV
);
366 pending_release
.resize(MAX_BDEV
);
367 for (unsigned id
= 0; id
< bdev
.size(); ++id
) {
371 assert(bdev
[id
]->get_size());
372 alloc
[id
] = Allocator::create(cct
, cct
->_conf
->bluefs_allocator
,
373 bdev
[id
]->get_size(),
374 cct
->_conf
->bluefs_alloc_size
);
375 interval_set
<uint64_t>& p
= block_all
[id
];
376 for (interval_set
<uint64_t>::iterator q
= p
.begin(); q
!= p
.end(); ++q
) {
377 alloc
[id
]->init_add_free(q
.get_start(), q
.get_len());
382 void BlueFS::_stop_alloc()
384 dout(20) << __func__
<< dendl
;
385 for (auto p
: alloc
) {
396 dout(1) << __func__
<< dendl
;
398 int r
= _open_super();
400 derr
<< __func__
<< " failed to open super: " << cpp_strerror(r
) << dendl
;
405 block_all
.resize(MAX_BDEV
);
407 block_total
.resize(MAX_BDEV
, 0);
412 derr
<< __func__
<< " failed to replay log: " << cpp_strerror(r
) << dendl
;
418 for (auto& p
: file_map
) {
419 dout(30) << __func__
<< " noting alloc for " << p
.second
->fnode
<< dendl
;
420 for (auto& q
: p
.second
->fnode
.extents
) {
421 alloc
[q
.bdev
]->init_rm_free(q
.offset
, q
.length
);
425 // set up the log for future writes
426 log_writer
= _create_writer(_get_file(1));
427 assert(log_writer
->file
->fnode
.ino
== 1);
428 log_writer
->pos
= log_writer
->file
->fnode
.size
;
429 dout(10) << __func__
<< " log write pos set to 0x"
430 << std::hex
<< log_writer
->pos
<< std::dec
437 super
= bluefs_super_t();
441 void BlueFS::umount()
443 dout(1) << __func__
<< dendl
;
447 _close_writer(log_writer
);
453 super
= bluefs_super_t();
458 void BlueFS::collect_metadata(map
<string
,string
> *pm
)
461 bdev
[BDEV_DB
]->collect_metadata("bluefs_db_", pm
);
463 bdev
[BDEV_WAL
]->collect_metadata("bluefs_wal_", pm
);
465 bdev
[BDEV_SLOW
]->collect_metadata("bluefs_slow_", pm
);
470 std::lock_guard
<std::mutex
> l(lock
);
471 dout(1) << __func__
<< dendl
;
472 // hrm, i think we check everything on mount...
476 int BlueFS::_write_super()
481 uint32_t crc
= bl
.crc32c(-1);
483 dout(10) << __func__
<< " super block length(encoded): " << bl
.length() << dendl
;
484 dout(10) << __func__
<< " superblock " << super
.version
<< dendl
;
485 dout(10) << __func__
<< " log_fnode " << super
.log_fnode
<< dendl
;
486 assert(bl
.length() <= get_super_length());
487 bl
.append_zero(get_super_length() - bl
.length());
489 bdev
[BDEV_DB
]->write(get_super_offset(), bl
, false);
490 dout(20) << __func__
<< " v " << super
.version
491 << " crc 0x" << std::hex
<< crc
492 << " offset 0x" << get_super_offset() << std::dec
497 int BlueFS::_open_super()
499 dout(10) << __func__
<< dendl
;
502 uint32_t expected_crc
, crc
;
505 // always the second block
506 r
= bdev
[BDEV_DB
]->read(get_super_offset(), get_super_length(),
507 &bl
, ioc
[BDEV_DB
], false);
511 bufferlist::iterator p
= bl
.begin();
515 t
.substr_of(bl
, 0, p
.get_off());
518 ::decode(expected_crc
, p
);
519 if (crc
!= expected_crc
) {
520 derr
<< __func__
<< " bad crc on superblock, expected 0x"
521 << std::hex
<< expected_crc
<< " != actual 0x" << crc
<< std::dec
525 dout(10) << __func__
<< " superblock " << super
.version
<< dendl
;
526 dout(10) << __func__
<< " log_fnode " << super
.log_fnode
<< dendl
;
530 int BlueFS::_replay(bool noop
)
532 dout(10) << __func__
<< (noop
? " NO-OP" : "") << dendl
;
533 ino_last
= 1; // by the log
540 log_file
= _get_file(1);
542 log_file
->fnode
= super
.log_fnode
;
543 dout(10) << __func__
<< " log_fnode " << super
.log_fnode
<< dendl
;
545 FileReader
*log_reader
= new FileReader(
546 log_file
, cct
->_conf
->bluefs_max_prefetch
,
550 assert((log_reader
->buf
.pos
& ~super
.block_mask()) == 0);
551 uint64_t pos
= log_reader
->buf
.pos
;
552 uint64_t read_pos
= pos
;
555 int r
= _read(log_reader
, &log_reader
->buf
, read_pos
, super
.block_size
,
557 assert(r
== (int)super
.block_size
);
564 bufferlist::iterator p
= bl
.begin();
572 if (len
+ 6 > bl
.length()) {
573 more
= ROUND_UP_TO(len
+ 6 - bl
.length(), super
.block_size
);
576 if (uuid
!= super
.uuid
) {
577 dout(10) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
578 << ": stop: uuid " << uuid
<< " != super.uuid " << super
.uuid
582 if (seq
!= log_seq
+ 1) {
583 dout(10) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
584 << ": stop: seq " << seq
<< " != expected " << log_seq
+ 1
589 dout(20) << __func__
<< " need 0x" << std::hex
<< more
<< std::dec
590 << " more bytes" << dendl
;
592 int r
= _read(log_reader
, &log_reader
->buf
, read_pos
, more
, &t
, NULL
);
594 dout(10) << __func__
<< " 0x" << std::hex
<< pos
595 << ": stop: len is 0x" << bl
.length() + more
<< std::dec
596 << ", which is past eof" << dendl
;
599 assert(r
== (int)more
);
603 bluefs_transaction_t t
;
605 bufferlist::iterator p
= bl
.begin();
608 catch (buffer::error
& e
) {
609 dout(10) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
610 << ": stop: failed to decode: " << e
.what()
615 assert(seq
== t
.seq
);
616 dout(10) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
617 << ": " << t
<< dendl
;
619 bufferlist::iterator p
= t
.op_bl
.begin();
625 case bluefs_transaction_t::OP_INIT
:
626 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
627 << ": op_init" << dendl
;
631 case bluefs_transaction_t::OP_JUMP
:
635 ::decode(next_seq
, p
);
637 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
638 << ": op_jump seq " << next_seq
639 << " offset 0x" << std::hex
<< offset
<< std::dec
<< dendl
;
640 assert(next_seq
>= log_seq
);
641 log_seq
= next_seq
- 1; // we will increment it below
642 uint64_t skip
= offset
- read_pos
;
645 int r
= _read(log_reader
, &log_reader
->buf
, read_pos
, skip
, &junk
,
647 if (r
!= (int)skip
) {
648 dout(10) << __func__
<< " 0x" << std::hex
<< read_pos
649 << ": stop: failed to skip to " << offset
650 << std::dec
<< dendl
;
651 assert(0 == "problem with op_jump");
657 case bluefs_transaction_t::OP_JUMP_SEQ
:
660 ::decode(next_seq
, p
);
661 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
662 << ": op_jump_seq " << next_seq
<< dendl
;
663 assert(next_seq
>= log_seq
);
664 log_seq
= next_seq
- 1; // we will increment it below
668 case bluefs_transaction_t::OP_ALLOC_ADD
:
671 uint64_t offset
, length
;
675 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
676 << ": op_alloc_add " << " " << (int)id
677 << ":0x" << std::hex
<< offset
<< "~" << length
<< std::dec
680 block_all
[id
].insert(offset
, length
);
681 block_total
[id
] += length
;
682 alloc
[id
]->init_add_free(offset
, length
);
687 case bluefs_transaction_t::OP_ALLOC_RM
:
690 uint64_t offset
, length
;
694 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
695 << ": op_alloc_rm " << " " << (int)id
696 << ":0x" << std::hex
<< offset
<< "~" << length
<< std::dec
699 block_all
[id
].erase(offset
, length
);
700 block_total
[id
] -= length
;
701 alloc
[id
]->init_rm_free(offset
, length
);
706 case bluefs_transaction_t::OP_DIR_LINK
:
708 string dirname
, filename
;
710 ::decode(dirname
, p
);
711 ::decode(filename
, p
);
713 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
714 << ": op_dir_link " << " " << dirname
<< "/" << filename
718 FileRef file
= _get_file(ino
);
719 assert(file
->fnode
.ino
);
720 map
<string
,DirRef
>::iterator q
= dir_map
.find(dirname
);
721 assert(q
!= dir_map
.end());
722 map
<string
,FileRef
>::iterator r
= q
->second
->file_map
.find(filename
);
723 assert(r
== q
->second
->file_map
.end());
724 q
->second
->file_map
[filename
] = file
;
730 case bluefs_transaction_t::OP_DIR_UNLINK
:
732 string dirname
, filename
;
733 ::decode(dirname
, p
);
734 ::decode(filename
, p
);
735 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
736 << ": op_dir_unlink " << " " << dirname
<< "/" << filename
739 map
<string
,DirRef
>::iterator q
= dir_map
.find(dirname
);
740 assert(q
!= dir_map
.end());
741 map
<string
,FileRef
>::iterator r
= q
->second
->file_map
.find(filename
);
742 assert(r
!= q
->second
->file_map
.end());
743 assert(r
->second
->refs
> 0);
745 q
->second
->file_map
.erase(r
);
750 case bluefs_transaction_t::OP_DIR_CREATE
:
753 ::decode(dirname
, p
);
754 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
755 << ": op_dir_create " << dirname
<< dendl
;
757 map
<string
,DirRef
>::iterator q
= dir_map
.find(dirname
);
758 assert(q
== dir_map
.end());
759 dir_map
[dirname
] = new Dir
;
764 case bluefs_transaction_t::OP_DIR_REMOVE
:
767 ::decode(dirname
, p
);
768 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
769 << ": op_dir_remove " << dirname
<< dendl
;
771 map
<string
,DirRef
>::iterator q
= dir_map
.find(dirname
);
772 assert(q
!= dir_map
.end());
773 assert(q
->second
->file_map
.empty());
779 case bluefs_transaction_t::OP_FILE_UPDATE
:
781 bluefs_fnode_t fnode
;
783 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
784 << ": op_file_update " << " " << fnode
<< dendl
;
786 FileRef f
= _get_file(fnode
.ino
);
788 if (fnode
.ino
> ino_last
) {
789 ino_last
= fnode
.ino
;
795 case bluefs_transaction_t::OP_FILE_REMOVE
:
799 dout(20) << __func__
<< " 0x" << std::hex
<< pos
<< std::dec
800 << ": op_file_remove " << ino
<< dendl
;
802 auto p
= file_map
.find(ino
);
803 assert(p
!= file_map
.end());
810 derr
<< __func__
<< " 0x" << std::hex
<< pos
<< std::dec
811 << ": stop: unrecognized op " << (int)op
<< dendl
;
818 // we successfully replayed the transaction; bump the seq and log size
820 log_file
->fnode
.size
= log_reader
->buf
.pos
;
823 dout(10) << __func__
<< " log file size was 0x"
824 << std::hex
<< log_file
->fnode
.size
<< std::dec
<< dendl
;
828 // verify file link counts are all >0
829 for (auto& p
: file_map
) {
830 if (p
.second
->refs
== 0 &&
831 p
.second
->fnode
.ino
> 1) {
832 derr
<< __func__
<< " file with link count 0: " << p
.second
->fnode
839 dout(10) << __func__
<< " done" << dendl
;
843 BlueFS::FileRef
BlueFS::_get_file(uint64_t ino
)
845 auto p
= file_map
.find(ino
);
846 if (p
== file_map
.end()) {
847 FileRef f
= new File
;
849 dout(30) << __func__
<< " ino " << ino
<< " = " << f
850 << " (new)" << dendl
;
853 dout(30) << __func__
<< " ino " << ino
<< " = " << p
->second
<< dendl
;
858 void BlueFS::_drop_link(FileRef file
)
860 dout(20) << __func__
<< " had refs " << file
->refs
861 << " on " << file
->fnode
<< dendl
;
862 assert(file
->refs
> 0);
864 if (file
->refs
== 0) {
865 dout(20) << __func__
<< " destroying " << file
->fnode
<< dendl
;
866 assert(file
->num_reading
.load() == 0);
867 log_t
.op_file_remove(file
->fnode
.ino
);
868 for (auto& r
: file
->fnode
.extents
) {
869 pending_release
[r
.bdev
].insert(r
.offset
, r
.length
);
871 file_map
.erase(file
->fnode
.ino
);
872 file
->deleted
= true;
874 if (file
->dirty_seq
) {
875 assert(file
->dirty_seq
> log_seq_stable
);
876 assert(dirty_files
.count(file
->dirty_seq
));
877 auto it
= dirty_files
[file
->dirty_seq
].iterator_to(*file
);
878 dirty_files
[file
->dirty_seq
].erase(it
);
884 int BlueFS::_read_random(
885 FileReader
*h
, ///< [in] read from here
886 uint64_t off
, ///< [in] offset
887 size_t len
, ///< [in] this many bytes
888 char *out
) ///< [out] optional: or copy it here
890 dout(10) << __func__
<< " h " << h
891 << " 0x" << std::hex
<< off
<< "~" << len
<< std::dec
892 << " from " << h
->file
->fnode
<< dendl
;
894 ++h
->file
->num_reading
;
896 if (!h
->ignore_eof
&&
897 off
+ len
> h
->file
->fnode
.size
) {
898 if (off
> h
->file
->fnode
.size
)
901 len
= h
->file
->fnode
.size
- off
;
902 dout(20) << __func__
<< " reaching (or past) eof, len clipped to 0x"
903 << std::hex
<< len
<< std::dec
<< dendl
;
909 auto p
= h
->file
->fnode
.seek(off
, &x_off
);
910 uint64_t l
= MIN(p
->length
- x_off
, len
);
911 dout(20) << __func__
<< " read buffered 0x"
912 << std::hex
<< x_off
<< "~" << l
<< std::dec
913 << " of " << *p
<< dendl
;
914 int r
= bdev
[p
->bdev
]->read_random(p
->offset
+ x_off
, l
, out
,
915 cct
->_conf
->bluefs_buffered_io
);
923 dout(20) << __func__
<< " got " << ret
<< dendl
;
924 --h
->file
->num_reading
;
929 FileReader
*h
, ///< [in] read from here
930 FileReaderBuffer
*buf
, ///< [in] reader state
931 uint64_t off
, ///< [in] offset
932 size_t len
, ///< [in] this many bytes
933 bufferlist
*outbl
, ///< [out] optional: reference the result here
934 char *out
) ///< [out] optional: or copy it here
936 dout(10) << __func__
<< " h " << h
937 << " 0x" << std::hex
<< off
<< "~" << len
<< std::dec
938 << " from " << h
->file
->fnode
<< dendl
;
940 ++h
->file
->num_reading
;
942 if (!h
->ignore_eof
&&
943 off
+ len
> h
->file
->fnode
.size
) {
944 if (off
> h
->file
->fnode
.size
)
947 len
= h
->file
->fnode
.size
- off
;
948 dout(20) << __func__
<< " reaching (or past) eof, len clipped to 0x"
949 << std::hex
<< len
<< std::dec
<< dendl
;
957 if (off
< buf
->bl_off
|| off
>= buf
->get_buf_end()) {
959 buf
->bl_off
= off
& super
.block_mask();
961 auto p
= h
->file
->fnode
.seek(buf
->bl_off
, &x_off
);
962 uint64_t want
= ROUND_UP_TO(len
+ (off
& ~super
.block_mask()),
964 want
= MAX(want
, buf
->max_prefetch
);
965 uint64_t l
= MIN(p
->length
- x_off
, want
);
966 uint64_t eof_offset
= ROUND_UP_TO(h
->file
->fnode
.size
, super
.block_size
);
967 if (!h
->ignore_eof
&&
968 buf
->bl_off
+ l
> eof_offset
) {
969 l
= eof_offset
- buf
->bl_off
;
971 dout(20) << __func__
<< " fetching 0x"
972 << std::hex
<< x_off
<< "~" << l
<< std::dec
973 << " of " << *p
<< dendl
;
974 int r
= bdev
[p
->bdev
]->read(p
->offset
+ x_off
, l
, &buf
->bl
, ioc
[p
->bdev
],
975 cct
->_conf
->bluefs_buffered_io
);
978 left
= buf
->get_buf_remaining(off
);
979 dout(20) << __func__
<< " left 0x" << std::hex
<< left
980 << " len 0x" << len
<< std::dec
<< dendl
;
982 int r
= MIN(len
, left
);
985 t
.substr_of(buf
->bl
, off
- buf
->bl_off
, r
);
986 outbl
->claim_append(t
);
989 // NOTE: h->bl is normally a contiguous buffer so c_str() is free.
990 memcpy(out
, buf
->bl
.c_str() + off
- buf
->bl_off
, r
);
994 dout(30) << __func__
<< " result chunk (0x"
995 << std::hex
<< r
<< std::dec
<< " bytes):\n";
997 t
.substr_of(buf
->bl
, off
- buf
->bl_off
, r
);
1007 dout(20) << __func__
<< " got " << ret
<< dendl
;
1008 assert(!outbl
|| (int)outbl
->length() == ret
);
1009 --h
->file
->num_reading
;
1013 void BlueFS::_invalidate_cache(FileRef f
, uint64_t offset
, uint64_t length
)
1015 dout(10) << __func__
<< " file " << f
->fnode
1016 << " 0x" << std::hex
<< offset
<< "~" << length
<< std::dec
1018 if (offset
& ~super
.block_mask()) {
1019 offset
&= super
.block_mask();
1020 length
= ROUND_UP_TO(length
, super
.block_size
);
1023 auto p
= f
->fnode
.seek(offset
, &x_off
);
1024 while (length
> 0 && p
!= f
->fnode
.extents
.end()) {
1025 uint64_t x_len
= MIN(p
->length
- x_off
, length
);
1026 bdev
[p
->bdev
]->invalidate_cache(p
->offset
+ x_off
, x_len
);
1027 dout(20) << __func__
<< " 0x" << std::hex
<< x_off
<< "~" << x_len
1028 << std:: dec
<< " of " << *p
<< dendl
;
1034 uint64_t BlueFS::_estimate_log_size()
1036 int avg_dir_size
= 40; // fixme
1037 int avg_file_size
= 12;
1038 uint64_t size
= 4096 * 2;
1039 size
+= file_map
.size() * (1 + sizeof(bluefs_fnode_t
));
1040 for (auto& p
: block_all
)
1041 size
+= p
.num_intervals() * (1 + 1 + sizeof(uint64_t) * 2);
1042 size
+= dir_map
.size() + (1 + avg_dir_size
);
1043 size
+= file_map
.size() * (1 + avg_dir_size
+ avg_file_size
);
1044 return ROUND_UP_TO(size
, super
.block_size
);
1047 void BlueFS::compact_log()
1049 std::unique_lock
<std::mutex
> l(lock
);
1050 if (cct
->_conf
->bluefs_compact_log_sync
) {
1051 _compact_log_sync();
1053 _compact_log_async(l
);
1057 bool BlueFS::_should_compact_log()
1059 uint64_t current
= log_writer
->file
->fnode
.size
;
1060 uint64_t expected
= _estimate_log_size();
1061 float ratio
= (float)current
/ (float)expected
;
1062 dout(10) << __func__
<< " current 0x" << std::hex
<< current
1063 << " expected " << expected
<< std::dec
1064 << " ratio " << ratio
1065 << (new_log
? " (async compaction in progress)" : "")
1068 current
< cct
->_conf
->bluefs_log_compact_min_size
||
1069 ratio
< cct
->_conf
->bluefs_log_compact_min_ratio
) {
1075 void BlueFS::_compact_log_dump_metadata(bluefs_transaction_t
*t
)
1078 t
->uuid
= super
.uuid
;
1079 dout(20) << __func__
<< " op_init" << dendl
;
1082 for (unsigned bdev
= 0; bdev
< MAX_BDEV
; ++bdev
) {
1083 interval_set
<uint64_t>& p
= block_all
[bdev
];
1084 for (interval_set
<uint64_t>::iterator q
= p
.begin(); q
!= p
.end(); ++q
) {
1085 dout(20) << __func__
<< " op_alloc_add " << bdev
<< " 0x"
1086 << std::hex
<< q
.get_start() << "~" << q
.get_len() << std::dec
1088 t
->op_alloc_add(bdev
, q
.get_start(), q
.get_len());
1091 for (auto& p
: file_map
) {
1094 dout(20) << __func__
<< " op_file_update " << p
.second
->fnode
<< dendl
;
1095 assert(p
.first
> 1);
1096 t
->op_file_update(p
.second
->fnode
);
1098 for (auto& p
: dir_map
) {
1099 dout(20) << __func__
<< " op_dir_create " << p
.first
<< dendl
;
1100 t
->op_dir_create(p
.first
);
1101 for (auto& q
: p
.second
->file_map
) {
1102 dout(20) << __func__
<< " op_dir_link " << p
.first
<< "/" << q
.first
1103 << " to " << q
.second
->fnode
.ino
<< dendl
;
1104 t
->op_dir_link(p
.first
, q
.first
, q
.second
->fnode
.ino
);
1109 void BlueFS::_compact_log_sync()
1111 dout(10) << __func__
<< dendl
;
1112 File
*log_file
= log_writer
->file
.get();
1114 // clear out log (be careful who calls us!!!)
1117 bluefs_transaction_t t
;
1118 _compact_log_dump_metadata(&t
);
1120 dout(20) << __func__
<< " op_jump_seq " << log_seq
<< dendl
;
1121 t
.op_jump_seq(log_seq
);
1127 uint64_t need
= bl
.length() + cct
->_conf
->bluefs_max_log_runway
;
1128 dout(20) << __func__
<< " need " << need
<< dendl
;
1130 mempool::bluefs::vector
<bluefs_extent_t
> old_extents
;
1131 uint64_t old_allocated
= 0;
1132 log_file
->fnode
.swap_extents(old_extents
, old_allocated
);
1133 while (log_file
->fnode
.get_allocated() < need
) {
1134 int r
= _allocate(log_file
->fnode
.prefer_bdev
,
1135 need
- log_file
->fnode
.get_allocated(),
1140 _close_writer(log_writer
);
1142 log_file
->fnode
.size
= bl
.length();
1143 log_writer
= _create_writer(log_file
);
1144 log_writer
->append(bl
);
1145 int r
= _flush(log_writer
, true);
1147 wait_for_aio(log_writer
);
1149 list
<aio_t
> completed_ios
;
1150 _claim_completed_aios(log_writer
, &completed_ios
);
1152 completed_ios
.clear();
1154 dout(10) << __func__
<< " writing super" << dendl
;
1155 super
.log_fnode
= log_file
->fnode
;
1160 dout(10) << __func__
<< " release old log extents " << old_extents
<< dendl
;
1161 for (auto& r
: old_extents
) {
1162 pending_release
[r
.bdev
].insert(r
.offset
, r
.length
);
1165 logger
->inc(l_bluefs_log_compactions
);
1169 * 1. Allocate a new extent to continue the log, and then log an event
1170 * that jumps the log write position to the new extent. At this point, the
1171 * old extent(s) won't be written to, and reflect everything to compact.
1172 * New events will be written to the new region that we'll keep.
1174 * 2. While still holding the lock, encode a bufferlist that dumps all of the
1175 * in-memory fnodes and names. This will become the new beginning of the
1176 * log. The last event will jump to the log continuation extent from #1.
1178 * 3. Queue a write to a new extent for the new beginnging of the log.
1180 * 4. Drop lock and wait
1182 * 5. Retake the lock.
1184 * 6. Update the log_fnode to splice in the new beginning.
1186 * 7. Write the new superblock.
1188 * 8. Release the old log space. Clean up.
1190 void BlueFS::_compact_log_async(std::unique_lock
<std::mutex
>& l
)
1192 dout(10) << __func__
<< dendl
;
1193 File
*log_file
= log_writer
->file
.get();
1195 assert(!new_log_writer
);
1197 // create a new log [writer] so that we know compaction is in progress
1198 // (see _should_compact_log)
1200 new_log
->fnode
.ino
= 0; // so that _flush_range won't try to log the fnode
1202 // 0. wait for any racing flushes to complete. (We do not want to block
1203 // in _flush_sync_log with jump_to set or else a racing thread might flush
1204 // our entries and our jump_to update won't be correct.)
1205 while (log_flushing
) {
1206 dout(10) << __func__
<< " log is currently flushing, waiting" << dendl
;
1210 // 1. allocate new log space and jump to it.
1211 old_log_jump_to
= log_file
->fnode
.get_allocated();
1212 uint64_t need
= old_log_jump_to
+ cct
->_conf
->bluefs_max_log_runway
;
1213 dout(10) << __func__
<< " old_log_jump_to 0x" << std::hex
<< old_log_jump_to
1214 << " need 0x" << need
<< std::dec
<< dendl
;
1215 while (log_file
->fnode
.get_allocated() < need
) {
1216 int r
= _allocate(log_file
->fnode
.prefer_bdev
,
1217 cct
->_conf
->bluefs_max_log_runway
,
1221 dout(10) << __func__
<< " log extents " << log_file
->fnode
.extents
<< dendl
;
1223 // update the log file change and log a jump to the offset where we want to
1224 // write the new entries
1225 log_t
.op_file_update(log_file
->fnode
);
1226 log_t
.op_jump(log_seq
, old_log_jump_to
);
1228 flush_bdev(); // FIXME?
1230 _flush_and_sync_log(l
, 0, old_log_jump_to
);
1232 // 2. prepare compacted log
1233 bluefs_transaction_t t
;
1234 //avoid record two times in log_t and _compact_log_dump_metadata.
1236 _compact_log_dump_metadata(&t
);
1238 // conservative estimate for final encoded size
1239 new_log_jump_to
= ROUND_UP_TO(t
.op_bl
.length() + super
.block_size
* 2,
1240 cct
->_conf
->bluefs_alloc_size
);
1241 t
.op_jump(log_seq
, new_log_jump_to
);
1247 dout(10) << __func__
<< " new_log_jump_to 0x" << std::hex
<< new_log_jump_to
1248 << std::dec
<< dendl
;
1251 int r
= _allocate(BlueFS::BDEV_DB
, new_log_jump_to
,
1254 new_log_writer
= _create_writer(new_log
);
1255 new_log_writer
->append(bl
);
1258 r
= _flush(new_log_writer
, true);
1263 dout(10) << __func__
<< " waiting for compacted log to sync" << dendl
;
1264 wait_for_aio(new_log_writer
);
1266 list
<aio_t
> completed_ios
;
1267 _claim_completed_aios(new_log_writer
, &completed_ios
);
1269 completed_ios
.clear();
1274 // 6. update our log fnode
1275 // discard first old_log_jump_to extents
1276 dout(10) << __func__
<< " remove 0x" << std::hex
<< old_log_jump_to
<< std::dec
1277 << " of " << log_file
->fnode
.extents
<< dendl
;
1278 uint64_t discarded
= 0;
1279 mempool::bluefs::vector
<bluefs_extent_t
> old_extents
;
1280 while (discarded
< old_log_jump_to
) {
1281 assert(!log_file
->fnode
.extents
.empty());
1282 bluefs_extent_t
& e
= log_file
->fnode
.extents
.front();
1283 bluefs_extent_t temp
= e
;
1284 if (discarded
+ e
.length
<= old_log_jump_to
) {
1285 dout(10) << __func__
<< " remove old log extent " << e
<< dendl
;
1286 discarded
+= e
.length
;
1287 log_file
->fnode
.pop_front_extent();
1289 dout(10) << __func__
<< " remove front of old log extent " << e
<< dendl
;
1290 uint64_t drop
= old_log_jump_to
- discarded
;
1295 dout(10) << __func__
<< " kept " << e
<< " removed " << temp
<< dendl
;
1297 old_extents
.push_back(temp
);
1299 auto from
= log_file
->fnode
.extents
.begin();
1300 auto to
= log_file
->fnode
.extents
.end();
1301 while (from
!= to
) {
1302 new_log
->fnode
.append_extent(*from
);
1306 // clear the extents from old log file, they are added to new log
1307 log_file
->fnode
.clear_extents();
1308 // swap the log files. New log file is the log file now.
1309 new_log
->fnode
.swap_extents(log_file
->fnode
);
1311 log_writer
->pos
= log_writer
->file
->fnode
.size
=
1312 log_writer
->pos
- old_log_jump_to
+ new_log_jump_to
;
1314 // 7. write the super block to reflect the changes
1315 dout(10) << __func__
<< " writing super" << dendl
;
1316 super
.log_fnode
= log_file
->fnode
;
1324 // 8. release old space
1325 dout(10) << __func__
<< " release old log extents " << old_extents
<< dendl
;
1326 for (auto& r
: old_extents
) {
1327 pending_release
[r
.bdev
].insert(r
.offset
, r
.length
);
1330 // delete the new log, remove from the dirty files list
1331 _close_writer(new_log_writer
);
1332 if (new_log
->dirty_seq
) {
1333 assert(dirty_files
.count(new_log
->dirty_seq
));
1334 auto it
= dirty_files
[new_log
->dirty_seq
].iterator_to(*new_log
);
1335 dirty_files
[new_log
->dirty_seq
].erase(it
);
1337 new_log_writer
= nullptr;
1339 log_cond
.notify_all();
1341 dout(10) << __func__
<< " log extents " << log_file
->fnode
.extents
<< dendl
;
1342 logger
->inc(l_bluefs_log_compactions
);
1345 void BlueFS::_pad_bl(bufferlist
& bl
)
1347 uint64_t partial
= bl
.length() % super
.block_size
;
1349 dout(10) << __func__
<< " padding with 0x" << std::hex
1350 << super
.block_size
- partial
<< " zeros" << std::dec
<< dendl
;
1351 bl
.append_zero(super
.block_size
- partial
);
1355 void BlueFS::flush_log()
1357 std::unique_lock
<std::mutex
> l(lock
);
1359 _flush_and_sync_log(l
);
1362 int BlueFS::_flush_and_sync_log(std::unique_lock
<std::mutex
>& l
,
1366 while (log_flushing
) {
1367 dout(10) << __func__
<< " want_seq " << want_seq
1368 << " log is currently flushing, waiting" << dendl
;
1372 if (want_seq
&& want_seq
<= log_seq_stable
) {
1373 dout(10) << __func__
<< " want_seq " << want_seq
<< " <= log_seq_stable "
1374 << log_seq_stable
<< ", done" << dendl
;
1378 if (log_t
.empty() && dirty_files
.empty()) {
1379 dout(10) << __func__
<< " want_seq " << want_seq
1380 << " " << log_t
<< " not dirty, dirty_files empty, no-op" << dendl
;
1385 uint64_t seq
= log_t
.seq
= ++log_seq
;
1386 assert(want_seq
== 0 || want_seq
<= seq
);
1387 log_t
.uuid
= super
.uuid
;
1390 auto lsi
= dirty_files
.find(seq
);
1391 if (lsi
!= dirty_files
.end()) {
1392 dout(20) << __func__
<< " " << lsi
->second
.size() << " dirty_files" << dendl
;
1393 for (auto &f
: lsi
->second
) {
1394 dout(20) << __func__
<< " op_file_update " << f
.fnode
<< dendl
;
1395 log_t
.op_file_update(f
.fnode
);
1399 dout(10) << __func__
<< " " << log_t
<< dendl
;
1400 assert(!log_t
.empty());
1402 // allocate some more space (before we run out)?
1403 int64_t runway
= log_writer
->file
->fnode
.get_allocated() -
1404 log_writer
->get_effective_write_pos();
1405 if (runway
< (int64_t)cct
->_conf
->bluefs_min_log_runway
) {
1406 dout(10) << __func__
<< " allocating more log runway (0x"
1407 << std::hex
<< runway
<< std::dec
<< " remaining)" << dendl
;
1408 while (new_log_writer
) {
1409 dout(10) << __func__
<< " waiting for async compaction" << dendl
;
1412 int r
= _allocate(log_writer
->file
->fnode
.prefer_bdev
,
1413 cct
->_conf
->bluefs_max_log_runway
,
1414 &log_writer
->file
->fnode
);
1416 log_t
.op_file_update(log_writer
->file
->fnode
);
1420 ::encode(log_t
, bl
);
1422 // pad to block boundary
1424 logger
->inc(l_bluefs_logged_bytes
, bl
.length());
1426 log_writer
->append(bl
);
1429 log_t
.seq
= 0; // just so debug output is less confusing
1430 log_flushing
= true;
1432 int r
= _flush(log_writer
, true);
1436 dout(10) << __func__
<< " jumping log offset from 0x" << std::hex
1437 << log_writer
->pos
<< " -> 0x" << jump_to
<< std::dec
<< dendl
;
1438 log_writer
->pos
= jump_to
;
1439 log_writer
->file
->fnode
.size
= jump_to
;
1442 _flush_bdev_safely(log_writer
);
1444 log_flushing
= false;
1445 log_cond
.notify_all();
1447 // clean dirty files
1448 if (seq
> log_seq_stable
) {
1449 log_seq_stable
= seq
;
1450 dout(20) << __func__
<< " log_seq_stable " << log_seq_stable
<< dendl
;
1452 auto p
= dirty_files
.begin();
1453 while (p
!= dirty_files
.end()) {
1454 if (p
->first
> log_seq_stable
) {
1455 dout(20) << __func__
<< " done cleaning up dirty files" << dendl
;
1459 auto l
= p
->second
.begin();
1460 while (l
!= p
->second
.end()) {
1462 assert(file
->dirty_seq
> 0);
1463 assert(file
->dirty_seq
<= log_seq_stable
);
1464 dout(20) << __func__
<< " cleaned file " << file
->fnode
<< dendl
;
1465 file
->dirty_seq
= 0;
1466 p
->second
.erase(l
++);
1469 assert(p
->second
.empty());
1470 dirty_files
.erase(p
++);
1473 dout(20) << __func__
<< " log_seq_stable " << log_seq_stable
1474 << " already >= out seq " << seq
1475 << ", we lost a race against another log flush, done" << dendl
;
1477 _update_logger_stats();
1482 int BlueFS::_flush_range(FileWriter
*h
, uint64_t offset
, uint64_t length
)
1484 dout(10) << __func__
<< " " << h
<< " pos 0x" << std::hex
<< h
->pos
1485 << " 0x" << offset
<< "~" << length
<< std::dec
1486 << " to " << h
->file
->fnode
<< dendl
;
1487 assert(!h
->file
->deleted
);
1488 assert(h
->file
->num_readers
.load() == 0);
1490 h
->buffer_appender
.flush();
1493 if (h
->file
->fnode
.ino
== 1)
1496 buffered
= cct
->_conf
->bluefs_buffered_io
;
1498 if (offset
+ length
<= h
->pos
)
1500 if (offset
< h
->pos
) {
1501 length
-= h
->pos
- offset
;
1503 dout(10) << " still need 0x"
1504 << std::hex
<< offset
<< "~" << length
<< std::dec
1507 assert(offset
<= h
->file
->fnode
.size
);
1509 uint64_t allocated
= h
->file
->fnode
.get_allocated();
1511 // do not bother to dirty the file if we are overwriting
1512 // previously allocated extents.
1513 bool must_dirty
= false;
1514 if (allocated
< offset
+ length
) {
1515 // we should never run out of log space here; see the min runway check
1516 // in _flush_and_sync_log.
1517 assert(h
->file
->fnode
.ino
!= 1);
1518 int r
= _allocate(h
->file
->fnode
.prefer_bdev
,
1519 offset
+ length
- allocated
,
1522 derr
<< __func__
<< " allocated: 0x" << std::hex
<< allocated
1523 << " offset: 0x" << offset
<< " length: 0x" << length
<< std::dec
1525 assert(0 == "bluefs enospc");
1528 if (cct
->_conf
->bluefs_preextend_wal_files
&&
1529 h
->writer_type
== WRITER_WAL
) {
1530 // NOTE: this *requires* that rocksdb also has log recycling
1531 // enabled and is therefore doing robust CRCs on the log
1532 // records. otherwise, we will fail to reply the rocksdb log
1533 // properly due to garbage on the device.
1534 h
->file
->fnode
.size
= h
->file
->fnode
.get_allocated();
1535 dout(10) << __func__
<< " extending WAL size to 0x" << std::hex
1536 << h
->file
->fnode
.size
<< std::dec
<< " to include allocated"
1541 if (h
->file
->fnode
.size
< offset
+ length
) {
1542 h
->file
->fnode
.size
= offset
+ length
;
1543 if (h
->file
->fnode
.ino
> 1) {
1544 // we do not need to dirty the log file (or it's compacting
1545 // replacement) when the file size changes because replay is
1546 // smart enough to discover it on its own.
1551 h
->file
->fnode
.mtime
= ceph_clock_now();
1552 assert(h
->file
->fnode
.ino
>= 1);
1553 if (h
->file
->dirty_seq
== 0) {
1554 h
->file
->dirty_seq
= log_seq
+ 1;
1555 dirty_files
[h
->file
->dirty_seq
].push_back(*h
->file
);
1556 dout(20) << __func__
<< " dirty_seq = " << log_seq
+ 1
1557 << " (was clean)" << dendl
;
1559 if (h
->file
->dirty_seq
!= log_seq
+ 1) {
1560 // need re-dirty, erase from list first
1561 assert(dirty_files
.count(h
->file
->dirty_seq
));
1562 auto it
= dirty_files
[h
->file
->dirty_seq
].iterator_to(*h
->file
);
1563 dirty_files
[h
->file
->dirty_seq
].erase(it
);
1564 h
->file
->dirty_seq
= log_seq
+ 1;
1565 dirty_files
[h
->file
->dirty_seq
].push_back(*h
->file
);
1566 dout(20) << __func__
<< " dirty_seq = " << log_seq
+ 1
1567 << " (was " << h
->file
->dirty_seq
<< ")" << dendl
;
1569 dout(20) << __func__
<< " dirty_seq = " << log_seq
+ 1
1570 << " (unchanged, do nothing) " << dendl
;
1574 dout(20) << __func__
<< " file now " << h
->file
->fnode
<< dendl
;
1577 auto p
= h
->file
->fnode
.seek(offset
, &x_off
);
1578 assert(p
!= h
->file
->fnode
.extents
.end());
1579 dout(20) << __func__
<< " in " << *p
<< " x_off 0x"
1580 << std::hex
<< x_off
<< std::dec
<< dendl
;
1582 unsigned partial
= x_off
& ~super
.block_mask();
1585 dout(20) << __func__
<< " using partial tail 0x"
1586 << std::hex
<< partial
<< std::dec
<< dendl
;
1587 assert(h
->tail_block
.length() == partial
);
1588 bl
.claim_append_piecewise(h
->tail_block
);
1592 dout(20) << __func__
<< " waiting for previous aio to complete" << dendl
;
1593 for (auto p
: h
->iocv
) {
1599 if (length
== partial
+ h
->buffer
.length()) {
1600 bl
.claim_append_piecewise(h
->buffer
);
1603 h
->buffer
.splice(0, length
, &t
);
1604 bl
.claim_append_piecewise(t
);
1605 t
.substr_of(h
->buffer
, length
, h
->buffer
.length() - length
);
1607 dout(20) << " leaving 0x" << std::hex
<< h
->buffer
.length() << std::dec
1608 << " unflushed" << dendl
;
1610 assert(bl
.length() == length
);
1612 switch (h
->writer_type
) {
1614 logger
->inc(l_bluefs_bytes_written_wal
, length
);
1617 logger
->inc(l_bluefs_bytes_written_sst
, length
);
1621 dout(30) << "dump:\n";
1625 h
->pos
= offset
+ length
;
1626 h
->tail_block
.clear();
1629 while (length
> 0) {
1630 uint64_t x_len
= MIN(p
->length
- x_off
, length
);
1632 t
.substr_of(bl
, bloff
, x_len
);
1633 unsigned tail
= x_len
& ~super
.block_mask();
1635 size_t zlen
= super
.block_size
- tail
;
1636 dout(20) << __func__
<< " caching tail of 0x"
1638 << " and padding block with 0x" << zlen
1639 << std::dec
<< dendl
;
1640 h
->tail_block
.substr_of(bl
, bl
.length() - tail
, tail
);
1641 if (h
->file
->fnode
.ino
> 1) {
1642 // we are using the page_aligned_appender, and can safely use
1643 // the tail of the raw buffer.
1644 const bufferptr
&last
= t
.back();
1645 if (last
.unused_tail_length() < zlen
) {
1646 derr
<< " wtf, last is " << last
<< " from " << t
<< dendl
;
1647 assert(last
.unused_tail_length() >= zlen
);
1650 z
.set_offset(last
.offset() + last
.length());
1653 t
.append(z
, 0, zlen
);
1655 t
.append_zero(zlen
);
1658 if (cct
->_conf
->bluefs_sync_write
) {
1659 bdev
[p
->bdev
]->write(p
->offset
+ x_off
, t
, buffered
);
1661 bdev
[p
->bdev
]->aio_write(p
->offset
+ x_off
, t
, h
->iocv
[p
->bdev
], buffered
);
1668 for (unsigned i
= 0; i
< MAX_BDEV
; ++i
) {
1671 if (h
->iocv
[i
]->has_pending_aios()) {
1672 bdev
[i
]->aio_submit(h
->iocv
[i
]);
1676 dout(20) << __func__
<< " h " << h
<< " pos now 0x"
1677 << std::hex
<< h
->pos
<< std::dec
<< dendl
;
1681 // we need to retire old completed aios so they don't stick around in
1682 // memory indefinitely (along with their bufferlist refs).
1683 void BlueFS::_claim_completed_aios(FileWriter
*h
, list
<aio_t
> *ls
)
1685 for (auto p
: h
->iocv
) {
1687 ls
->splice(ls
->end(), p
->running_aios
);
1690 dout(10) << __func__
<< " got " << ls
->size() << " aios" << dendl
;
1693 void BlueFS::wait_for_aio(FileWriter
*h
)
1695 // NOTE: this is safe to call without a lock, as long as our reference is
1697 dout(10) << __func__
<< " " << h
<< dendl
;
1698 utime_t start
= ceph_clock_now();
1699 for (auto p
: h
->iocv
) {
1704 utime_t end
= ceph_clock_now();
1705 utime_t dur
= end
- start
;
1706 dout(10) << __func__
<< " " << h
<< " done in " << dur
<< dendl
;
1709 int BlueFS::_flush(FileWriter
*h
, bool force
)
1711 h
->buffer_appender
.flush();
1712 uint64_t length
= h
->buffer
.length();
1713 uint64_t offset
= h
->pos
;
1715 length
< cct
->_conf
->bluefs_min_flush_size
) {
1716 dout(10) << __func__
<< " " << h
<< " ignoring, length " << length
1717 << " < min_flush_size " << cct
->_conf
->bluefs_min_flush_size
1722 dout(10) << __func__
<< " " << h
<< " no dirty data on "
1723 << h
->file
->fnode
<< dendl
;
1726 dout(10) << __func__
<< " " << h
<< " 0x"
1727 << std::hex
<< offset
<< "~" << length
<< std::dec
1728 << " to " << h
->file
->fnode
<< dendl
;
1729 assert(h
->pos
<= h
->file
->fnode
.size
);
1730 return _flush_range(h
, offset
, length
);
1733 int BlueFS::_truncate(FileWriter
*h
, uint64_t offset
)
1735 dout(10) << __func__
<< " 0x" << std::hex
<< offset
<< std::dec
1736 << " file " << h
->file
->fnode
<< dendl
;
1737 if (h
->file
->deleted
) {
1738 dout(10) << __func__
<< " deleted, no-op" << dendl
;
1742 // we never truncate internal log files
1743 assert(h
->file
->fnode
.ino
> 1);
1745 h
->buffer_appender
.flush();
1747 // truncate off unflushed data?
1748 if (h
->pos
< offset
&&
1749 h
->pos
+ h
->buffer
.length() > offset
) {
1751 dout(20) << __func__
<< " tossing out last " << offset
- h
->pos
1752 << " unflushed bytes" << dendl
;
1753 t
.substr_of(h
->buffer
, 0, offset
- h
->pos
);
1755 assert(0 == "actually this shouldn't happen");
1757 if (h
->buffer
.length()) {
1758 int r
= _flush(h
, true);
1762 if (offset
== h
->file
->fnode
.size
) {
1765 if (offset
> h
->file
->fnode
.size
) {
1766 assert(0 == "truncate up not supported");
1768 assert(h
->file
->fnode
.size
>= offset
);
1769 h
->file
->fnode
.size
= offset
;
1770 log_t
.op_file_update(h
->file
->fnode
);
1774 int BlueFS::_fsync(FileWriter
*h
, std::unique_lock
<std::mutex
>& l
)
1776 dout(10) << __func__
<< " " << h
<< " " << h
->file
->fnode
<< dendl
;
1777 int r
= _flush(h
, true);
1780 uint64_t old_dirty_seq
= h
->file
->dirty_seq
;
1782 _flush_bdev_safely(h
);
1784 if (old_dirty_seq
) {
1785 uint64_t s
= log_seq
;
1786 dout(20) << __func__
<< " file metadata was dirty (" << old_dirty_seq
1787 << ") on " << h
->file
->fnode
<< ", flushing log" << dendl
;
1788 _flush_and_sync_log(l
, old_dirty_seq
);
1789 assert(h
->file
->dirty_seq
== 0 || // cleaned
1790 h
->file
->dirty_seq
> s
); // or redirtied by someone else
1795 void BlueFS::_flush_bdev_safely(FileWriter
*h
)
1797 if (!cct
->_conf
->bluefs_sync_write
) {
1798 list
<aio_t
> completed_ios
;
1799 _claim_completed_aios(h
, &completed_ios
);
1802 completed_ios
.clear();
1812 void BlueFS::flush_bdev()
1814 // NOTE: this is safe to call without a lock.
1815 dout(20) << __func__
<< dendl
;
1816 for (auto p
: bdev
) {
1822 int BlueFS::_allocate(uint8_t id
, uint64_t len
,
1823 bluefs_fnode_t
* node
)
1825 dout(10) << __func__
<< " len 0x" << std::hex
<< len
<< std::dec
1826 << " from " << (int)id
<< dendl
;
1827 assert(id
< alloc
.size());
1828 uint64_t min_alloc_size
= cct
->_conf
->bluefs_alloc_size
;
1830 uint64_t left
= ROUND_UP_TO(len
, min_alloc_size
);
1832 int64_t alloc_len
= 0;
1833 AllocExtentVector extents
;
1836 r
= alloc
[id
]->reserve(left
);
1841 if (!node
->extents
.empty() && node
->extents
.back().bdev
== id
) {
1842 hint
= node
->extents
.back().end();
1844 extents
.reserve(4); // 4 should be (more than) enough for most allocations
1845 alloc_len
= alloc
[id
]->allocate(left
, min_alloc_size
, hint
, &extents
);
1847 if (r
< 0 || (alloc_len
< (int64_t)left
)) {
1849 alloc
[id
]->unreserve(left
- alloc_len
);
1850 for (auto& p
: extents
) {
1851 alloc
[id
]->release(p
.offset
, p
.length
);
1854 if (id
!= BDEV_SLOW
) {
1856 dout(1) << __func__
<< " failed to allocate 0x" << std::hex
<< left
1857 << " on bdev " << (int)id
1858 << ", free 0x" << alloc
[id
]->get_free()
1859 << "; fallback to bdev " << (int)id
+ 1
1860 << std::dec
<< dendl
;
1862 return _allocate(id
+ 1, len
, node
);
1865 derr
<< __func__
<< " failed to allocate 0x" << std::hex
<< left
1866 << " on bdev " << (int)id
1867 << ", free 0x" << alloc
[id
]->get_free() << std::dec
<< dendl
;
1869 derr
<< __func__
<< " failed to allocate 0x" << std::hex
<< left
1870 << " on bdev " << (int)id
<< ", dne" << std::dec
<< dendl
;
1876 for (auto& p
: extents
) {
1877 node
->append_extent(bluefs_extent_t(id
, p
.offset
, p
.length
));
1883 int BlueFS::_preallocate(FileRef f
, uint64_t off
, uint64_t len
)
1885 dout(10) << __func__
<< " file " << f
->fnode
<< " 0x"
1886 << std::hex
<< off
<< "~" << len
<< std::dec
<< dendl
;
1888 dout(10) << __func__
<< " deleted, no-op" << dendl
;
1891 assert(f
->fnode
.ino
> 1);
1892 uint64_t allocated
= f
->fnode
.get_allocated();
1893 if (off
+ len
> allocated
) {
1894 uint64_t want
= off
+ len
- allocated
;
1895 int r
= _allocate(f
->fnode
.prefer_bdev
, want
, &f
->fnode
);
1898 log_t
.op_file_update(f
->fnode
);
1903 void BlueFS::sync_metadata()
1905 std::unique_lock
<std::mutex
> l(lock
);
1906 if (log_t
.empty()) {
1907 dout(10) << __func__
<< " - no pending log events" << dendl
;
1910 dout(10) << __func__
<< dendl
;
1911 utime_t start
= ceph_clock_now();
1912 vector
<interval_set
<uint64_t>> to_release(pending_release
.size());
1913 to_release
.swap(pending_release
);
1914 flush_bdev(); // FIXME?
1915 _flush_and_sync_log(l
);
1916 for (unsigned i
= 0; i
< to_release
.size(); ++i
) {
1917 for (auto p
= to_release
[i
].begin(); p
!= to_release
[i
].end(); ++p
) {
1918 alloc
[i
]->release(p
.get_start(), p
.get_len());
1922 if (_should_compact_log()) {
1923 if (cct
->_conf
->bluefs_compact_log_sync
) {
1924 _compact_log_sync();
1926 _compact_log_async(l
);
1930 utime_t end
= ceph_clock_now();
1931 utime_t dur
= end
- start
;
1932 dout(10) << __func__
<< " done in " << dur
<< dendl
;
1935 int BlueFS::open_for_write(
1936 const string
& dirname
,
1937 const string
& filename
,
1941 std::lock_guard
<std::mutex
> l(lock
);
1942 dout(10) << __func__
<< " " << dirname
<< "/" << filename
<< dendl
;
1943 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
1945 if (p
== dir_map
.end()) {
1946 // implicitly create the dir
1947 dout(20) << __func__
<< " dir " << dirname
1948 << " does not exist" << dendl
;
1955 bool create
= false;
1956 map
<string
,FileRef
>::iterator q
= dir
->file_map
.find(filename
);
1957 if (q
== dir
->file_map
.end()) {
1959 dout(20) << __func__
<< " dir " << dirname
<< " (" << dir
1960 << ") file " << filename
1961 << " does not exist" << dendl
;
1965 file
->fnode
.ino
= ++ino_last
;
1966 file_map
[ino_last
] = file
;
1967 dir
->file_map
[filename
] = file
;
1971 // overwrite existing file?
1974 dout(20) << __func__
<< " dir " << dirname
<< " (" << dir
1975 << ") file " << filename
1976 << " already exists, overwrite in place" << dendl
;
1978 dout(20) << __func__
<< " dir " << dirname
<< " (" << dir
1979 << ") file " << filename
1980 << " already exists, truncate + overwrite" << dendl
;
1981 file
->fnode
.size
= 0;
1982 for (auto& p
: file
->fnode
.extents
) {
1983 pending_release
[p
.bdev
].insert(p
.offset
, p
.length
);
1986 file
->fnode
.clear_extents();
1989 assert(file
->fnode
.ino
> 1);
1991 file
->fnode
.mtime
= ceph_clock_now();
1992 file
->fnode
.prefer_bdev
= BlueFS::BDEV_DB
;
1993 if (dirname
.length() > 5) {
1994 // the "db.slow" and "db.wal" directory names are hard-coded at
1995 // match up with bluestore. the slow device is always the second
1996 // one (when a dedicated block.db device is present and used at
1997 // bdev 0). the wal device is always last.
1998 if (boost::algorithm::ends_with(dirname
, ".slow")) {
1999 file
->fnode
.prefer_bdev
= BlueFS::BDEV_SLOW
;
2000 } else if (boost::algorithm::ends_with(dirname
, ".wal")) {
2001 file
->fnode
.prefer_bdev
= BlueFS::BDEV_WAL
;
2004 dout(20) << __func__
<< " mapping " << dirname
<< "/" << filename
2005 << " to bdev " << (int)file
->fnode
.prefer_bdev
<< dendl
;
2007 log_t
.op_file_update(file
->fnode
);
2009 log_t
.op_dir_link(dirname
, filename
, file
->fnode
.ino
);
2011 *h
= _create_writer(file
);
2013 if (boost::algorithm::ends_with(filename
, ".log")) {
2014 (*h
)->writer_type
= BlueFS::WRITER_WAL
;
2015 if (logger
&& !overwrite
) {
2016 logger
->inc(l_bluefs_files_written_wal
);
2018 } else if (boost::algorithm::ends_with(filename
, ".sst")) {
2019 (*h
)->writer_type
= BlueFS::WRITER_SST
;
2021 logger
->inc(l_bluefs_files_written_sst
);
2025 dout(10) << __func__
<< " h " << *h
<< " on " << file
->fnode
<< dendl
;
2029 BlueFS::FileWriter
*BlueFS::_create_writer(FileRef f
)
2031 FileWriter
*w
= new FileWriter(f
);
2032 for (unsigned i
= 0; i
< MAX_BDEV
; ++i
) {
2034 w
->iocv
[i
] = new IOContext(cct
, NULL
);
2042 void BlueFS::_close_writer(FileWriter
*h
)
2044 dout(10) << __func__
<< " " << h
<< " type " << h
->writer_type
<< dendl
;
2045 for (unsigned i
=0; i
<MAX_BDEV
; ++i
) {
2048 h
->iocv
[i
]->aio_wait();
2049 bdev
[i
]->queue_reap_ioc(h
->iocv
[i
]);
2055 int BlueFS::open_for_read(
2056 const string
& dirname
,
2057 const string
& filename
,
2061 std::lock_guard
<std::mutex
> l(lock
);
2062 dout(10) << __func__
<< " " << dirname
<< "/" << filename
2063 << (random
? " (random)":" (sequential)") << dendl
;
2064 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
2065 if (p
== dir_map
.end()) {
2066 dout(20) << __func__
<< " dir " << dirname
<< " not found" << dendl
;
2069 DirRef dir
= p
->second
;
2071 map
<string
,FileRef
>::iterator q
= dir
->file_map
.find(filename
);
2072 if (q
== dir
->file_map
.end()) {
2073 dout(20) << __func__
<< " dir " << dirname
<< " (" << dir
2074 << ") file " << filename
2075 << " not found" << dendl
;
2078 File
*file
= q
->second
.get();
2080 *h
= new FileReader(file
, random
? 4096 : cct
->_conf
->bluefs_max_prefetch
,
2082 dout(10) << __func__
<< " h " << *h
<< " on " << file
->fnode
<< dendl
;
2087 const string
& old_dirname
, const string
& old_filename
,
2088 const string
& new_dirname
, const string
& new_filename
)
2090 std::lock_guard
<std::mutex
> l(lock
);
2091 dout(10) << __func__
<< " " << old_dirname
<< "/" << old_filename
2092 << " -> " << new_dirname
<< "/" << new_filename
<< dendl
;
2093 map
<string
,DirRef
>::iterator p
= dir_map
.find(old_dirname
);
2094 if (p
== dir_map
.end()) {
2095 dout(20) << __func__
<< " dir " << old_dirname
<< " not found" << dendl
;
2098 DirRef old_dir
= p
->second
;
2099 map
<string
,FileRef
>::iterator q
= old_dir
->file_map
.find(old_filename
);
2100 if (q
== old_dir
->file_map
.end()) {
2101 dout(20) << __func__
<< " dir " << old_dirname
<< " (" << old_dir
2102 << ") file " << old_filename
2103 << " not found" << dendl
;
2106 FileRef file
= q
->second
;
2108 p
= dir_map
.find(new_dirname
);
2109 if (p
== dir_map
.end()) {
2110 dout(20) << __func__
<< " dir " << new_dirname
<< " not found" << dendl
;
2113 DirRef new_dir
= p
->second
;
2114 q
= new_dir
->file_map
.find(new_filename
);
2115 if (q
!= new_dir
->file_map
.end()) {
2116 dout(20) << __func__
<< " dir " << new_dirname
<< " (" << old_dir
2117 << ") file " << new_filename
2118 << " already exists, unlinking" << dendl
;
2119 assert(q
->second
!= file
);
2120 log_t
.op_dir_unlink(new_dirname
, new_filename
);
2121 _drop_link(q
->second
);
2124 dout(10) << __func__
<< " " << new_dirname
<< "/" << new_filename
<< " "
2125 << " " << file
->fnode
<< dendl
;
2127 new_dir
->file_map
[new_filename
] = file
;
2128 old_dir
->file_map
.erase(old_filename
);
2130 log_t
.op_dir_link(new_dirname
, new_filename
, file
->fnode
.ino
);
2131 log_t
.op_dir_unlink(old_dirname
, old_filename
);
2135 int BlueFS::mkdir(const string
& dirname
)
2137 std::lock_guard
<std::mutex
> l(lock
);
2138 dout(10) << __func__
<< " " << dirname
<< dendl
;
2139 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
2140 if (p
!= dir_map
.end()) {
2141 dout(20) << __func__
<< " dir " << dirname
<< " exists" << dendl
;
2144 dir_map
[dirname
] = new Dir
;
2145 log_t
.op_dir_create(dirname
);
2149 int BlueFS::rmdir(const string
& dirname
)
2151 std::lock_guard
<std::mutex
> l(lock
);
2152 dout(10) << __func__
<< " " << dirname
<< dendl
;
2153 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
2154 if (p
== dir_map
.end()) {
2155 dout(20) << __func__
<< " dir " << dirname
<< " does not exist" << dendl
;
2158 DirRef dir
= p
->second
;
2159 if (!dir
->file_map
.empty()) {
2160 dout(20) << __func__
<< " dir " << dirname
<< " not empty" << dendl
;
2163 dir_map
.erase(dirname
);
2164 log_t
.op_dir_remove(dirname
);
2168 bool BlueFS::dir_exists(const string
& dirname
)
2170 std::lock_guard
<std::mutex
> l(lock
);
2171 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
2172 bool exists
= p
!= dir_map
.end();
2173 dout(10) << __func__
<< " " << dirname
<< " = " << (int)exists
<< dendl
;
2177 int BlueFS::stat(const string
& dirname
, const string
& filename
,
2178 uint64_t *size
, utime_t
*mtime
)
2180 std::lock_guard
<std::mutex
> l(lock
);
2181 dout(10) << __func__
<< " " << dirname
<< "/" << filename
<< dendl
;
2182 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
2183 if (p
== dir_map
.end()) {
2184 dout(20) << __func__
<< " dir " << dirname
<< " not found" << dendl
;
2187 DirRef dir
= p
->second
;
2188 map
<string
,FileRef
>::iterator q
= dir
->file_map
.find(filename
);
2189 if (q
== dir
->file_map
.end()) {
2190 dout(20) << __func__
<< " dir " << dirname
<< " (" << dir
2191 << ") file " << filename
2192 << " not found" << dendl
;
2195 File
*file
= q
->second
.get();
2196 dout(10) << __func__
<< " " << dirname
<< "/" << filename
2197 << " " << file
->fnode
<< dendl
;
2199 *size
= file
->fnode
.size
;
2201 *mtime
= file
->fnode
.mtime
;
2205 int BlueFS::lock_file(const string
& dirname
, const string
& filename
,
2208 std::lock_guard
<std::mutex
> l(lock
);
2209 dout(10) << __func__
<< " " << dirname
<< "/" << filename
<< dendl
;
2210 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
2211 if (p
== dir_map
.end()) {
2212 dout(20) << __func__
<< " dir " << dirname
<< " not found" << dendl
;
2215 DirRef dir
= p
->second
;
2216 map
<string
,FileRef
>::iterator q
= dir
->file_map
.find(filename
);
2218 if (q
== dir
->file_map
.end()) {
2219 dout(20) << __func__
<< " dir " << dirname
<< " (" << dir
2220 << ") file " << filename
2221 << " not found, creating" << dendl
;
2223 file
->fnode
.ino
= ++ino_last
;
2224 file
->fnode
.mtime
= ceph_clock_now();
2225 file_map
[ino_last
] = file
;
2226 dir
->file_map
[filename
] = file
;
2228 log_t
.op_file_update(file
->fnode
);
2229 log_t
.op_dir_link(dirname
, filename
, file
->fnode
.ino
);
2231 file
= q
->second
.get();
2233 dout(10) << __func__
<< " already locked" << dendl
;
2237 file
->locked
= true;
2238 *plock
= new FileLock(file
);
2239 dout(10) << __func__
<< " locked " << file
->fnode
2240 << " with " << *plock
<< dendl
;
2244 int BlueFS::unlock_file(FileLock
*fl
)
2246 std::lock_guard
<std::mutex
> l(lock
);
2247 dout(10) << __func__
<< " " << fl
<< " on " << fl
->file
->fnode
<< dendl
;
2248 assert(fl
->file
->locked
);
2249 fl
->file
->locked
= false;
2254 int BlueFS::readdir(const string
& dirname
, vector
<string
> *ls
)
2256 std::lock_guard
<std::mutex
> l(lock
);
2257 dout(10) << __func__
<< " " << dirname
<< dendl
;
2258 if (dirname
.empty()) {
2260 ls
->reserve(dir_map
.size() + 2);
2261 for (auto& q
: dir_map
) {
2262 ls
->push_back(q
.first
);
2265 // list files in dir
2266 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
2267 if (p
== dir_map
.end()) {
2268 dout(20) << __func__
<< " dir " << dirname
<< " not found" << dendl
;
2271 DirRef dir
= p
->second
;
2272 ls
->reserve(dir
->file_map
.size() + 2);
2273 for (auto& q
: dir
->file_map
) {
2274 ls
->push_back(q
.first
);
2278 ls
->push_back("..");
2282 int BlueFS::unlink(const string
& dirname
, const string
& filename
)
2284 std::lock_guard
<std::mutex
> l(lock
);
2285 dout(10) << __func__
<< " " << dirname
<< "/" << filename
<< dendl
;
2286 map
<string
,DirRef
>::iterator p
= dir_map
.find(dirname
);
2287 if (p
== dir_map
.end()) {
2288 dout(20) << __func__
<< " dir " << dirname
<< " not found" << dendl
;
2291 DirRef dir
= p
->second
;
2292 map
<string
,FileRef
>::iterator q
= dir
->file_map
.find(filename
);
2293 if (q
== dir
->file_map
.end()) {
2294 dout(20) << __func__
<< " file " << dirname
<< "/" << filename
2295 << " not found" << dendl
;
2298 FileRef file
= q
->second
;
2300 dout(20) << __func__
<< " file " << dirname
<< "/" << filename
2301 << " is locked" << dendl
;
2304 dir
->file_map
.erase(filename
);
2305 log_t
.op_dir_unlink(dirname
, filename
);
2310 bool BlueFS::wal_is_rotational()
2312 if (bdev
[BDEV_WAL
]) {
2313 return bdev
[BDEV_WAL
]->is_rotational();
2314 } else if (bdev
[BDEV_DB
]) {
2315 return bdev
[BDEV_DB
]->is_rotational();
2317 return bdev
[BDEV_SLOW
]->is_rotational();