1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
16 #include "common/debug.h"
17 #include "common/errno.h"
18 #include "common/safe_io.h"
19 #include "FileJournal.h"
20 #include "include/color.h"
21 #include "common/perf_counters.h"
22 #include "FileStore.h"
24 #include "include/compat.h"
31 #include <sys/types.h>
33 #include <sys/mount.h>
35 #include "common/blkdev.h"
36 #if defined(__linux__)
37 #include "common/linux_version.h"
40 #if defined(__FreeBSD__)
41 #define O_DSYNC O_SYNC
44 #define dout_context cct
45 #define dout_subsys ceph_subsys_journal
47 #define dout_prefix *_dout << "journal "
49 const static int64_t ONE_MEG(1 << 20);
50 const static int CEPH_DIRECTIO_ALIGNMENT(4096);
53 int FileJournal::_open(bool forwrite
, bool create
)
60 flags
|= O_DIRECT
| O_DSYNC
;
68 if (TEMP_FAILURE_RETRY(::close(fd
))) {
70 derr
<< "FileJournal::_open: error closing old fd: "
71 << cpp_strerror(err
) << dendl
;
74 fd
= TEMP_FAILURE_RETRY(::open(fn
.c_str(), flags
, 0644));
77 dout(2) << "FileJournal::_open unable to open journal "
78 << fn
<< ": " << cpp_strerror(err
) << dendl
;
83 ret
= ::fstat(fd
, &st
);
86 derr
<< "FileJournal::_open: unable to fstat journal: " << cpp_strerror(ret
) << dendl
;
91 if (S_ISBLK(st
.st_mode
)) {
92 ret
= _open_block_device();
93 } else if (S_ISREG(st
.st_mode
)) {
94 if (aio
&& !force_aio
) {
95 derr
<< "FileJournal::_open: disabling aio for non-block journal. Use "
96 << "journal_force_aio to force use of aio anyway" << dendl
;
99 ret
= _open_file(st
.st_size
, st
.st_blksize
, create
);
101 derr
<< "FileJournal::_open: wrong journal file type: " << st
.st_mode
112 ret
= io_setup(128, &aio_ctx
);
115 // Contrary to naive expectations -EAGIAN means ...
117 derr
<< "FileJournal::_open: user's limit of aio events exceeded. "
118 << "Try increasing /proc/sys/fs/aio-max-nr" << dendl
;
121 derr
<< "FileJournal::_open: unable to setup io_context " << cpp_strerror(-ret
) << dendl
;
129 /* We really want max_size to be a multiple of block_size. */
130 max_size
-= max_size
% block_size
;
132 dout(1) << "_open " << fn
<< " fd " << fd
134 << " bytes, block size " << block_size
135 << " bytes, directio = " << directio
141 VOID_TEMP_FAILURE_RETRY(::close(fd
));
146 int FileJournal::_open_block_device()
149 int ret
= get_block_device_size(fd
, &bdev_sz
);
151 dout(0) << __func__
<< ": failed to read block device size." << dendl
;
155 /* Check for bdev_sz too small */
156 if (bdev_sz
< ONE_MEG
) {
157 dout(0) << __func__
<< ": your block device must be at least "
158 << ONE_MEG
<< " bytes to be used for a Ceph journal." << dendl
;
162 dout(10) << __func__
<< ": ignoring osd journal size. "
163 << "We'll use the entire block device (size: " << bdev_sz
<< ")"
167 block_size
= cct
->_conf
->journal_block_size
;
169 if (cct
->_conf
->journal_discard
) {
170 discard
= block_device_support_discard(fn
.c_str());
171 dout(10) << fn
<< " support discard: " << (int)discard
<< dendl
;
177 int FileJournal::_open_file(int64_t oldsize
, blksize_t blksize
,
181 int64_t conf_journal_sz(cct
->_conf
->osd_journal_size
);
182 conf_journal_sz
<<= 20;
184 if ((cct
->_conf
->osd_journal_size
== 0) && (oldsize
< ONE_MEG
)) {
185 derr
<< "I'm sorry, I don't know how large of a journal to create."
186 << "Please specify a block device to use as the journal OR "
187 << "set osd_journal_size in your ceph.conf" << dendl
;
191 if (create
&& (oldsize
< conf_journal_sz
)) {
192 uint64_t newsize(conf_journal_sz
);
193 dout(10) << __func__
<< " _open extending to " << newsize
<< " bytes" << dendl
;
194 ret
= ::ftruncate(fd
, newsize
);
197 derr
<< "FileJournal::_open_file : unable to extend journal to "
198 << newsize
<< " bytes: " << cpp_strerror(err
) << dendl
;
201 #ifdef HAVE_POSIX_FALLOCATE
202 ret
= ::posix_fallocate(fd
, 0, newsize
);
204 derr
<< "FileJournal::_open_file : unable to preallocation journal to "
205 << newsize
<< " bytes: " << cpp_strerror(ret
) << dendl
;
209 #elif defined(__APPLE__)
211 store
.fst_flags
= F_ALLOCATECONTIG
;
212 store
.fst_posmode
= F_PEOFPOSMODE
;
213 store
.fst_offset
= 0;
214 store
.fst_length
= newsize
;
216 ret
= ::fcntl(fd
, F_PREALLOCATE
, &store
);
219 derr
<< "FileJournal::_open_file : unable to preallocation journal to "
220 << newsize
<< " bytes: " << cpp_strerror(ret
) << dendl
;
225 # error "Journal pre-allocation not supported on platform."
231 block_size
= cct
->_conf
->journal_block_size
;
233 if (create
&& cct
->_conf
->journal_zero_on_create
) {
234 derr
<< "FileJournal::_open_file : zeroing journal" << dendl
;
235 uint64_t write_size
= 1 << 20;
237 ret
= ::posix_memalign((void **)&buf
, block_size
, write_size
);
241 memset(static_cast<void*>(buf
), 0, write_size
);
243 for (; (i
+ write_size
) <= (uint64_t)max_size
; i
+= write_size
) {
244 ret
= ::pwrite(fd
, static_cast<void*>(buf
), write_size
, i
);
250 if (i
< (uint64_t)max_size
) {
251 ret
= ::pwrite(fd
, static_cast<void*>(buf
), max_size
- i
, i
);
261 dout(10) << "_open journal is not a block device, NOT checking disk "
262 << "write cache on '" << fn
<< "'" << dendl
;
267 // This can not be used on an active journal
268 int FileJournal::check()
273 ret
= _open(false, false);
277 ret
= read_header(&header
);
281 if (header
.fsid
!= fsid
) {
282 derr
<< "check: ondisk fsid " << header
.fsid
<< " doesn't match expected " << fsid
283 << ", invalid (someone else's?) journal" << dendl
;
288 dout(1) << "check: header looks ok" << dendl
;
297 int FileJournal::create()
300 int64_t needed_space
;
303 dout(2) << "create " << fn
<< " fsid " << fsid
<< dendl
;
305 ret
= _open(true, true);
309 // write empty header
311 header
.flags
= header_t::FLAG_CRC
; // enable crcs on any new journal.
313 header
.max_size
= max_size
;
314 header
.block_size
= block_size
;
315 if (cct
->_conf
->journal_block_align
|| directio
)
316 header
.alignment
= block_size
;
318 header
.alignment
= 16; // at least stay word aligned on 64bit machines...
320 header
.start
= get_top();
321 header
.start_seq
= 0;
323 print_header(header
);
325 // static zeroed buffer for alignment padding
327 zero_buf
= new char[header
.alignment
];
328 memset(zero_buf
, 0, header
.alignment
);
330 bp
= prepare_header();
331 if (TEMP_FAILURE_RETRY(::pwrite(fd
, bp
.c_str(), bp
.length(), 0)) < 0) {
333 derr
<< "FileJournal::create : create write header error "
334 << cpp_strerror(ret
) << dendl
;
338 // zero first little bit, too.
339 ret
= posix_memalign(&buf
, block_size
, block_size
);
342 derr
<< "FileJournal::create: failed to allocate " << block_size
343 << " bytes of memory: " << cpp_strerror(ret
) << dendl
;
346 memset(buf
, 0, block_size
);
347 if (TEMP_FAILURE_RETRY(::pwrite(fd
, buf
, block_size
, get_top())) < 0) {
349 derr
<< "FileJournal::create: error zeroing first " << block_size
350 << " bytes " << cpp_strerror(ret
) << dendl
;
354 needed_space
= ((int64_t)cct
->_conf
->osd_max_write_size
) << 20;
355 needed_space
+= (2 * sizeof(entry_header_t
)) + get_top();
356 if (header
.max_size
- header
.start
< needed_space
) {
357 derr
<< "FileJournal::create: OSD journal is not large enough to hold "
358 << "osd_max_write_size bytes!" << dendl
;
363 dout(2) << "create done" << dendl
;
370 if (TEMP_FAILURE_RETRY(::close(fd
)) < 0) {
372 derr
<< "FileJournal::create: error closing fd: " << cpp_strerror(ret
)
380 // This can not be used on an active journal
381 int FileJournal::peek_fsid(uuid_d
& fsid
)
384 int r
= _open(false, false);
387 r
= read_header(&header
);
396 int FileJournal::open(uint64_t fs_op_seq
)
398 dout(2) << "open " << fn
<< " fsid " << fsid
<< " fs_op_seq " << fs_op_seq
<< dendl
;
400 uint64_t next_seq
= fs_op_seq
+ 1;
402 int err
= _open(false);
406 // assume writeable, unless...
408 write_pos
= get_top();
411 err
= read_header(&header
);
415 // static zeroed buffer for alignment padding
417 zero_buf
= new char[header
.alignment
];
418 memset(zero_buf
, 0, header
.alignment
);
420 dout(10) << "open header.fsid = " << header
.fsid
421 //<< " vs expected fsid = " << fsid
423 if (header
.fsid
!= fsid
) {
424 derr
<< "FileJournal::open: ondisk fsid " << header
.fsid
<< " doesn't match expected " << fsid
425 << ", invalid (someone else's?) journal" << dendl
;
428 if (header
.max_size
> max_size
) {
429 dout(2) << "open journal size " << header
.max_size
<< " > current " << max_size
<< dendl
;
432 if (header
.block_size
!= block_size
) {
433 dout(2) << "open journal block size " << header
.block_size
<< " != current " << block_size
<< dendl
;
436 if (header
.max_size
% header
.block_size
) {
437 dout(2) << "open journal max size " << header
.max_size
438 << " not a multiple of block size " << header
.block_size
<< dendl
;
441 if (header
.alignment
!= block_size
&& directio
) {
442 dout(0) << "open journal alignment " << header
.alignment
<< " does not match block size "
443 << block_size
<< " (required for direct_io journal mode)" << dendl
;
446 if ((header
.alignment
% CEPH_DIRECTIO_ALIGNMENT
) && directio
) {
447 dout(0) << "open journal alignment " << header
.alignment
448 << " is not multiple of minimum directio alignment "
449 << CEPH_DIRECTIO_ALIGNMENT
<< " (required for direct_io journal mode)"
454 // looks like a valid header.
455 write_pos
= 0; // not writeable yet
457 journaled_seq
= header
.committed_up_to
;
460 read_pos
= header
.start
;
461 uint64_t seq
= header
.start_seq
;
463 // last_committed_seq is 1 before the start of the journal or
464 // 0 if the start is 0
465 last_committed_seq
= seq
> 0 ? seq
- 1 : seq
;
466 if (last_committed_seq
< fs_op_seq
) {
467 dout(2) << "open advancing committed_seq " << last_committed_seq
468 << " to fs op_seq " << fs_op_seq
<< dendl
;
469 last_committed_seq
= fs_op_seq
;
474 off64_t old_pos
= read_pos
;
475 if (!read_entry(bl
, seq
)) {
476 dout(10) << "open reached end of journal." << dendl
;
479 if (seq
> next_seq
) {
480 dout(10) << "open entry " << seq
<< " len " << bl
.length() << " > next_seq " << next_seq
481 << ", ignoring journal contents"
484 last_committed_seq
= 0;
487 if (seq
== next_seq
) {
488 dout(10) << "open reached seq " << seq
<< dendl
;
492 seq
++; // next event should follow.
498 void FileJournal::_close(int fd
) const
500 VOID_TEMP_FAILURE_RETRY(::close(fd
));
503 void FileJournal::close()
505 dout(1) << "close " << fn
<< dendl
;
507 // stop writer thread
511 assert(writeq_empty());
512 assert(!must_write_header
);
519 int FileJournal::dump(ostream
& out
)
521 return _dump(out
, false);
524 int FileJournal::simple_dump(ostream
& out
)
526 return _dump(out
, true);
529 int FileJournal::_dump(ostream
& out
, bool simple
)
531 JSONFormatter
f(true);
532 int ret
= _fdump(f
, simple
);
537 int FileJournal::_fdump(Formatter
&f
, bool simple
)
539 dout(10) << "_fdump" << dendl
;
542 int err
= _open(false, false);
546 err
= read_header(&header
);
552 off64_t next_pos
= header
.start
;
554 f
.open_object_section("journal");
556 f
.open_object_section("header");
557 f
.dump_unsigned("flags", header
.flags
);
560 f
.dump_string("fsid", os
.str());
561 f
.dump_unsigned("block_size", header
.block_size
);
562 f
.dump_unsigned("alignment", header
.alignment
);
563 f
.dump_int("max_size", header
.max_size
);
564 f
.dump_int("start", header
.start
);
565 f
.dump_unsigned("committed_up_to", header
.committed_up_to
);
566 f
.dump_unsigned("start_seq", header
.start_seq
);
569 f
.open_array_section("entries");
570 uint64_t seq
= header
.start_seq
;
573 off64_t pos
= next_pos
;
576 dout(2) << "_dump -- not readable" << dendl
;
581 read_entry_result result
= do_read_entry(
587 if (result
!= SUCCESS
) {
588 if (seq
< header
.committed_up_to
) {
589 dout(2) << "Unable to read past sequence " << seq
590 << " but header indicates the journal has committed up through "
591 << header
.committed_up_to
<< ", journal is corrupt" << dendl
;
594 dout(25) << ss
.str() << dendl
;
595 dout(25) << "No further valid entries found, journal is most likely valid"
600 f
.open_object_section("entry");
601 f
.dump_unsigned("offset", pos
);
602 f
.dump_unsigned("seq", seq
);
604 f
.dump_unsigned("bl.length", bl
.length());
606 f
.open_array_section("transactions");
607 bufferlist::iterator p
= bl
.begin();
610 ObjectStore::Transaction
t(p
);
611 f
.open_object_section("transaction");
612 f
.dump_unsigned("trans_num", trans_num
);
624 dout(10) << "dump finish" << dendl
;
631 void FileJournal::start_writer()
635 write_thread
.create("journal_write");
638 write_finish_thread
.create("journal_wrt_fin");
642 void FileJournal::stop_writer()
644 // Do nothing if writer already stopped or never started
648 Mutex::Locker
l(write_lock
);
649 Mutex::Locker
p(writeq_lock
);
651 writeq_cond
.Signal();
652 // Doesn't hurt to signal commit_cond in case thread is waiting there
653 // and caller didn't use committed_thru() first.
654 commit_cond
.Signal();
658 // write journal header now so that we have less to replay on remount
663 // stop aio completeion thread *after* writer thread has stopped
664 // and has submitted all of its io
665 if (aio
&& !aio_stop
) {
669 write_finish_cond
.Signal();
671 write_finish_thread
.join();
678 void FileJournal::print_header(const header_t
&header
) const
680 dout(10) << "header: block_size " << header
.block_size
681 << " alignment " << header
.alignment
682 << " max_size " << header
.max_size
684 dout(10) << "header: start " << header
.start
<< dendl
;
685 dout(10) << " write_pos " << write_pos
<< dendl
;
688 int FileJournal::read_header(header_t
*hdr
) const
690 dout(10) << "read_header" << dendl
;
693 buffer::ptr bp
= buffer::create_page_aligned(block_size
);
694 char* bpdata
= bp
.c_str();
695 int r
= ::pread(fd
, bpdata
, bp
.length(), 0);
699 dout(0) << "read_header got " << cpp_strerror(err
) << dendl
;
703 // don't use bp.zero() here, because it also invalidates
704 // crc cache (which is not yet populated anyway)
705 if (bp
.length() != (size_t)r
) {
706 // r will be always less or equal than bp.length
708 memset(bpdata
, 0, bp
.length() - r
);
711 bl
.push_back(std::move(bp
));
714 bufferlist::iterator p
= bl
.begin();
717 catch (buffer::error
& e
) {
718 derr
<< "read_header error decoding journal header" << dendl
;
724 * Unfortunately we weren't initializing the flags field for new
725 * journals! Aie. This is safe(ish) now that we have only one
726 * flag. Probably around when we add the next flag we need to
727 * remove this or else this (eventually old) code will clobber newer
730 if (hdr
->flags
> 3) {
731 derr
<< "read_header appears to have gibberish flags; assuming 0" << dendl
;
740 bufferptr
FileJournal::prepare_header()
744 Mutex::Locker
l(finisher_lock
);
745 header
.committed_up_to
= journaled_seq
;
747 ::encode(header
, bl
);
748 bufferptr bp
= buffer::create_page_aligned(get_top());
749 // don't use bp.zero() here, because it also invalidates
750 // crc cache (which is not yet populated anyway)
751 char* data
= bp
.c_str();
752 memcpy(data
, bl
.c_str(), bl
.length());
754 memset(data
, 0, bp
.length()-bl
.length());
758 void FileJournal::write_header_sync()
760 Mutex::Locker
locker(write_lock
);
761 must_write_header
= true;
764 dout(20) << __func__
<< " finish" << dendl
;
767 int FileJournal::check_for_full(uint64_t seq
, off64_t pos
, off64_t size
)
770 if (full_state
!= FULL_NOTFULL
)
773 // take 1 byte off so that we only get pos == header.start on EMPTY, never on FULL.
775 if (pos
>= header
.start
)
776 room
= (header
.max_size
- pos
) + (header
.start
- get_top()) - 1;
778 room
= header
.start
- pos
- 1;
779 dout(10) << "room " << room
<< " max_size " << max_size
<< " pos " << pos
<< " header.start " << header
.start
780 << " top " << get_top() << dendl
;
783 if (room
>= (header
.max_size
>> 1) &&
784 room
- size
< (header
.max_size
>> 1)) {
785 dout(10) << " passing half full mark, triggering commit" << dendl
;
786 do_sync_cond
->SloppySignal(); // initiate a real commit so we can trim
791 dout(10) << "check_for_full at " << pos
<< " : " << size
<< " < " << room
<< dendl
;
792 if (pos
+ size
> header
.max_size
)
793 must_write_header
= true;
798 dout(1) << "check_for_full at " << pos
<< " : JOURNAL FULL "
799 << pos
<< " >= " << room
800 << " (max_size " << header
.max_size
<< " start " << header
.start
<< ")"
803 off64_t max
= header
.max_size
- get_top();
805 dout(0) << "JOURNAL TOO SMALL: continuing, but slow: item " << size
<< " > journal " << max
<< " (usable)" << dendl
;
810 int FileJournal::prepare_multi_write(bufferlist
& bl
, uint64_t& orig_ops
, uint64_t& orig_bytes
)
812 // gather queued writes
813 off64_t queue_pos
= write_pos
;
815 int eleft
= cct
->_conf
->journal_max_write_entries
;
816 unsigned bmax
= cct
->_conf
->journal_max_write_bytes
;
818 if (full_state
!= FULL_NOTFULL
)
821 while (!writeq_empty()) {
822 list
<write_item
> items
;
823 batch_pop_write(items
);
824 list
<write_item
>::iterator it
= items
.begin();
825 while (it
!= items
.end()) {
826 uint64_t bytes
= it
->bl
.length();
827 int r
= prepare_single_write(*it
, bl
, queue_pos
, orig_ops
, orig_bytes
);
828 if (r
== 0) { // prepare ok, delete it
832 Mutex::Locker
locker(aio_lock
);
833 assert(aio_write_queue_ops
> 0);
834 aio_write_queue_ops
--;
835 assert(aio_write_queue_bytes
>= bytes
);
836 aio_write_queue_bytes
-= bytes
;
843 // the journal maybe full, insert the left item to writeq
844 batch_unpop_write(items
);
846 goto out
; // commit what we have
849 logger
->inc(l_filestore_journal_full
);
852 dout(20) << "prepare_multi_write full on first entry, need to wait" << dendl
;
854 dout(20) << "prepare_multi_write full on first entry, restarting journal" << dendl
;
856 // throw out what we have so far
857 full_state
= FULL_FULL
;
858 while (!writeq_empty()) {
859 complete_write(1, peek_write().orig_len
);
862 print_header(header
);
865 return -ENOSPC
; // hrm, full on first op
869 dout(20) << "prepare_multi_write hit max events per write "
870 << cct
->_conf
->journal_max_write_entries
<< dendl
;
871 batch_unpop_write(items
);
876 if (bl
.length() >= bmax
) {
877 dout(20) << "prepare_multi_write hit max write size "
878 << cct
->_conf
->journal_max_write_bytes
<< dendl
;
879 batch_unpop_write(items
);
887 dout(20) << "prepare_multi_write queue_pos now " << queue_pos
<< dendl
;
888 assert((write_pos
+ bl
.length() == queue_pos
) ||
889 (write_pos
+ bl
.length() - header
.max_size
+ get_top() == queue_pos
));
894 void FileJournal::queue_write_fin(uint64_t seq, Context *fin)
896 writing_seq.push_back(seq);
897 if (!waiting_for_notfull.empty()) {
898 // make sure previously unjournaled stuff waiting for UNFULL triggers
899 // _before_ newly journaled stuff does
900 dout(10) << "queue_write_fin will defer seq " << seq << " callback " << fin
901 << " until after UNFULL" << dendl;
902 C_Gather *g = new C_Gather(writeq.front().fin);
903 writing_fin.push_back(g->new_sub());
904 waiting_for_notfull.push_back(g->new_sub());
906 writing_fin.push_back(writeq.front().fin);
907 dout(20) << "queue_write_fin seq " << seq << " callback " << fin << dendl;
912 void FileJournal::queue_completions_thru(uint64_t seq
)
914 assert(finisher_lock
.is_locked());
915 utime_t now
= ceph_clock_now();
916 list
<completion_item
> items
;
917 batch_pop_completions(items
);
918 list
<completion_item
>::iterator it
= items
.begin();
919 while (it
!= items
.end()) {
920 completion_item
& next
= *it
;
925 dout(10) << "queue_completions_thru seq " << seq
926 << " queueing seq " << next
.seq
927 << " " << next
.finish
928 << " lat " << lat
<< dendl
;
930 logger
->tinc(l_filestore_journal_latency
, lat
);
933 finisher
->queue(next
.finish
);
934 if (next
.tracked_op
) {
935 next
.tracked_op
->mark_event("journaled_completion_queued");
936 next
.tracked_op
->journal_trace
.event("queued completion");
937 next
.tracked_op
->journal_trace
.keyval("completed through", seq
);
941 batch_unpop_completions(items
);
942 finisher_cond
.Signal();
946 int FileJournal::prepare_single_write(write_item
&next_write
, bufferlist
& bl
, off64_t
& queue_pos
, uint64_t& orig_ops
, uint64_t& orig_bytes
)
948 uint64_t seq
= next_write
.seq
;
949 bufferlist
&ebl
= next_write
.bl
;
950 off64_t size
= ebl
.length();
952 int r
= check_for_full(seq
, queue_pos
, size
);
954 return r
; // ENOSPC or EAGAIN
956 uint32_t orig_len
= next_write
.orig_len
;
957 orig_bytes
+= orig_len
;
960 // add to write buffer
961 dout(15) << "prepare_single_write " << orig_ops
<< " will write " << queue_pos
<< " : seq " << seq
962 << " len " << orig_len
<< " -> " << size
<< dendl
;
964 unsigned seq_offset
= offsetof(entry_header_t
, seq
);
965 unsigned magic1_offset
= offsetof(entry_header_t
, magic1
);
966 unsigned magic2_offset
= offsetof(entry_header_t
, magic2
);
968 bufferptr headerptr
= ebl
.buffers().front();
970 uint64_t _queue_pos
= queue_pos
;
971 uint64_t magic2
= entry_header_t::make_magic(seq
, orig_len
, header
.get_fsid64());
972 headerptr
.copy_in(seq_offset
, sizeof(uint64_t), (char *)&_seq
);
973 headerptr
.copy_in(magic1_offset
, sizeof(uint64_t), (char *)&_queue_pos
);
974 headerptr
.copy_in(magic2_offset
, sizeof(uint64_t), (char *)&magic2
);
976 bufferptr footerptr
= ebl
.buffers().back();
977 unsigned post_offset
= footerptr
.length() - sizeof(entry_header_t
);
978 footerptr
.copy_in(post_offset
+ seq_offset
, sizeof(uint64_t), (char *)&_seq
);
979 footerptr
.copy_in(post_offset
+ magic1_offset
, sizeof(uint64_t), (char *)&_queue_pos
);
980 footerptr
.copy_in(post_offset
+ magic2_offset
, sizeof(uint64_t), (char *)&magic2
);
982 bl
.claim_append(ebl
);
983 if (next_write
.tracked_op
) {
984 next_write
.tracked_op
->mark_event("write_thread_in_journal_buffer");
985 next_write
.tracked_op
->journal_trace
.event("prepare_single_write");
988 journalq
.push_back(pair
<uint64_t,off64_t
>(seq
, queue_pos
));
992 if (queue_pos
>= header
.max_size
)
993 queue_pos
= queue_pos
+ get_top() - header
.max_size
;
998 void FileJournal::check_align(off64_t pos
, bufferlist
& bl
)
1000 // make sure list segments are page aligned
1001 if (directio
&& !bl
.is_aligned_size_and_memory(block_size
, CEPH_DIRECTIO_ALIGNMENT
)) {
1002 assert((bl
.length() & (CEPH_DIRECTIO_ALIGNMENT
- 1)) == 0);
1003 assert((pos
& (CEPH_DIRECTIO_ALIGNMENT
- 1)) == 0);
1004 assert(0 == "bl was not aligned");
1008 int FileJournal::write_bl(off64_t
& pos
, bufferlist
& bl
)
1012 off64_t spos
= ::lseek64(fd
, pos
, SEEK_SET
);
1015 derr
<< "FileJournal::write_bl : lseek64 failed " << cpp_strerror(ret
) << dendl
;
1018 ret
= bl
.write_fd(fd
);
1020 derr
<< "FileJournal::write_bl : write_fd failed: " << cpp_strerror(ret
) << dendl
;
1024 if (pos
== header
.max_size
)
1029 void FileJournal::do_write(bufferlist
& bl
)
1032 if (bl
.length() == 0 && !must_write_header
)
1036 if (cct
->_conf
->journal_write_header_frequency
&&
1037 (((++journaled_since_start
) %
1038 cct
->_conf
->journal_write_header_frequency
) == 0)) {
1039 must_write_header
= true;
1042 if (must_write_header
) {
1043 must_write_header
= false;
1044 hbp
= prepare_header();
1047 dout(15) << "do_write writing " << write_pos
<< "~" << bl
.length()
1048 << (hbp
.length() ? " + header":"")
1051 utime_t from
= ceph_clock_now();
1054 off64_t pos
= write_pos
;
1057 write_pos
+= bl
.length();
1058 if (write_pos
>= header
.max_size
)
1059 write_pos
= write_pos
- header
.max_size
+ get_top();
1061 write_lock
.Unlock();
1065 if (pos
+ bl
.length() > header
.max_size
) {
1066 bufferlist first
, second
;
1067 split
= header
.max_size
- pos
;
1068 first
.substr_of(bl
, 0, split
);
1069 second
.substr_of(bl
, split
, bl
.length() - split
);
1070 assert(first
.length() + second
.length() == bl
.length());
1071 dout(10) << "do_write wrapping, first bit at " << pos
<< " len " << first
.length()
1072 << " second bit len " << second
.length() << " (orig len " << bl
.length() << ")" << dendl
;
1074 //Save pos to write first piece second
1075 off64_t first_pos
= pos
;
1080 // be sneaky: include the header in the second fragment
1081 second
.push_front(hbp
);
1082 pos
= 0; // we included the header
1084 // Write the second portion first possible with the header, so
1085 // do_read_entry() won't even get a valid entry_header_t if there
1086 // is a crash between the two writes.
1088 if (write_bl(pos
, second
)) {
1089 derr
<< "FileJournal::do_write: write_bl(pos=" << orig_pos
1090 << ") failed" << dendl
;
1091 check_align(pos
, second
);
1094 orig_pos
= first_pos
;
1095 if (write_bl(first_pos
, first
)) {
1096 derr
<< "FileJournal::do_write: write_bl(pos=" << orig_pos
1097 << ") failed" << dendl
;
1098 check_align(first_pos
, first
);
1101 assert(first_pos
== get_top());
1105 if (TEMP_FAILURE_RETRY(::pwrite(fd
, hbp
.c_str(), hbp
.length(), 0)) < 0) {
1107 derr
<< "FileJournal::do_write: pwrite(fd=" << fd
1108 << ", hbp.length=" << hbp
.length() << ") failed :"
1109 << cpp_strerror(err
) << dendl
;
1114 if (write_bl(pos
, bl
)) {
1115 derr
<< "FileJournal::do_write: write_bl(pos=" << pos
1116 << ") failed" << dendl
;
1117 check_align(pos
, bl
);
1123 dout(20) << "do_write fsync" << dendl
;
1126 * We'd really love to have a fsync_range or fdatasync_range and do a:
1129 * ::fsync_range(fd, header.max_size - split, split)l
1130 * ::fsync_range(fd, get_top(), bl.length() - split);
1132 * ::fsync_range(fd, write_pos, bl.length())
1134 * NetBSD and AIX apparently have it, and adding it to Linux wouldn't be
1135 * too hard given all the underlying infrastructure already exist.
1137 * NOTE: using sync_file_range here would not be safe as it does not
1138 * flush disk caches or commits any sort of metadata.
1141 #if defined(DARWIN) || defined(__FreeBSD__)
1144 ret
= ::fdatasync(fd
);
1147 derr
<< __func__
<< " fsync/fdatasync failed: " << cpp_strerror(errno
) << dendl
;
1150 #ifdef HAVE_POSIX_FADVISE
1151 if (cct
->_conf
->filestore_fadvise
)
1152 posix_fadvise(fd
, 0, 0, POSIX_FADV_DONTNEED
);
1156 utime_t lat
= ceph_clock_now() - from
;
1157 dout(20) << "do_write latency " << lat
<< dendl
;
1161 assert(write_pos
== pos
);
1162 assert(write_pos
% header
.alignment
== 0);
1165 Mutex::Locker
locker(finisher_lock
);
1166 journaled_seq
= writing_seq
;
1169 // only if we haven't filled up recently!
1170 if (full_state
!= FULL_NOTFULL
) {
1171 dout(10) << "do_write NOT queueing finisher seq " << journaled_seq
1172 << ", full_commit_seq|full_restart_seq" << dendl
;
1174 if (plug_journal_completions
) {
1175 dout(20) << "do_write NOT queueing finishers through seq " << journaled_seq
1176 << " due to completion plug" << dendl
;
1178 dout(20) << "do_write queueing finishers through seq " << journaled_seq
<< dendl
;
1179 queue_completions_thru(journaled_seq
);
1185 void FileJournal::flush()
1187 dout(10) << "waiting for completions to empty" << dendl
;
1189 Mutex::Locker
l(finisher_lock
);
1190 while (!completions_empty())
1191 finisher_cond
.Wait(finisher_lock
);
1193 dout(10) << "flush waiting for finisher" << dendl
;
1194 finisher
->wait_for_empty();
1195 dout(10) << "flush done" << dendl
;
1199 void FileJournal::write_thread_entry()
1201 dout(10) << "write_thread_entry start" << dendl
;
1204 Mutex::Locker
locker(writeq_lock
);
1205 if (writeq
.empty() && !must_write_header
) {
1208 dout(20) << "write_thread_entry going to sleep" << dendl
;
1209 writeq_cond
.Wait(writeq_lock
);
1210 dout(20) << "write_thread_entry woke up" << dendl
;
1217 Mutex::Locker
locker(aio_lock
);
1218 // should we back off to limit aios in flight? try to do this
1219 // adaptively so that we submit larger aios once we have lots of
1222 // NOTE: our condition here is based on aio_num (protected by
1223 // aio_lock) and throttle_bytes (part of the write queue). when
1224 // we sleep, we *only* wait for aio_num to change, and do not
1225 // wake when more data is queued. this is not strictly correct,
1226 // but should be fine given that we will have plenty of aios in
1227 // flight if we hit this limit to ensure we keep the device
1229 while (aio_num
> 0) {
1230 int exp
= MIN(aio_num
* 2, 24);
1231 long unsigned min_new
= 1ull << exp
;
1232 uint64_t cur
= aio_write_queue_bytes
;
1233 dout(20) << "write_thread_entry aio throttle: aio num " << aio_num
<< " bytes " << aio_bytes
1234 << " ... exp " << exp
<< " min_new " << min_new
1235 << " ... pending " << cur
<< dendl
;
1238 dout(20) << "write_thread_entry deferring until more aios complete: "
1239 << aio_num
<< " aios with " << aio_bytes
<< " bytes needs " << min_new
1240 << " bytes to start a new aio (currently " << cur
<< " pending)" << dendl
;
1241 aio_cond
.Wait(aio_lock
);
1242 dout(20) << "write_thread_entry woke up" << dendl
;
1247 Mutex::Locker
locker(write_lock
);
1248 uint64_t orig_ops
= 0;
1249 uint64_t orig_bytes
= 0;
1252 int r
= prepare_multi_write(bl
, orig_ops
, orig_bytes
);
1253 // Don't care about journal full if stoppping, so drop queue and
1254 // possibly let header get written and loop above to notice stop
1257 dout(20) << "write_thread_entry full and stopping, throw out queue and finish up" << dendl
;
1258 while (!writeq_empty()) {
1259 complete_write(1, peek_write().orig_len
);
1262 print_header(header
);
1265 dout(20) << "write_thread_entry full, going to sleep (waiting for commit)" << dendl
;
1266 commit_cond
.Wait(write_lock
);
1267 dout(20) << "write_thread_entry woke up" << dendl
;
1274 logger
->inc(l_filestore_journal_wr
);
1275 logger
->inc(l_filestore_journal_wr_bytes
, bl
.length());
1286 complete_write(orig_ops
, orig_bytes
);
1289 dout(10) << "write_thread_entry finish" << dendl
;
1293 void FileJournal::do_aio_write(bufferlist
& bl
)
1296 if (cct
->_conf
->journal_write_header_frequency
&&
1297 (((++journaled_since_start
) %
1298 cct
->_conf
->journal_write_header_frequency
) == 0)) {
1299 must_write_header
= true;
1303 if (bl
.length() == 0 && !must_write_header
)
1307 if (must_write_header
) {
1308 must_write_header
= false;
1309 hbp
= prepare_header();
1313 off64_t pos
= write_pos
;
1315 dout(15) << "do_aio_write writing " << pos
<< "~" << bl
.length()
1316 << (hbp
.length() ? " + header":"")
1321 if (pos
+ bl
.length() > header
.max_size
) {
1322 bufferlist first
, second
;
1323 split
= header
.max_size
- pos
;
1324 first
.substr_of(bl
, 0, split
);
1325 second
.substr_of(bl
, split
, bl
.length() - split
);
1326 assert(first
.length() + second
.length() == bl
.length());
1327 dout(10) << "do_aio_write wrapping, first bit at " << pos
<< "~" << first
.length() << dendl
;
1329 if (write_aio_bl(pos
, first
, 0)) {
1330 derr
<< "FileJournal::do_aio_write: write_aio_bl(pos=" << pos
1331 << ") failed" << dendl
;
1334 assert(pos
== header
.max_size
);
1336 // be sneaky: include the header in the second fragment
1337 second
.push_front(hbp
);
1338 pos
= 0; // we included the header
1340 pos
= get_top(); // no header, start after that
1341 if (write_aio_bl(pos
, second
, writing_seq
)) {
1342 derr
<< "FileJournal::do_aio_write: write_aio_bl(pos=" << pos
1343 << ") failed" << dendl
;
1352 if (write_aio_bl(pos
, hbl
, 0)) {
1353 derr
<< "FileJournal::do_aio_write: write_aio_bl(header) failed" << dendl
;
1358 if (write_aio_bl(pos
, bl
, writing_seq
)) {
1359 derr
<< "FileJournal::do_aio_write: write_aio_bl(pos=" << pos
1360 << ") failed" << dendl
;
1366 if (write_pos
== header
.max_size
)
1367 write_pos
= get_top();
1368 assert(write_pos
% header
.alignment
== 0);
1372 * write a buffer using aio
1374 * @param seq seq to trigger when this aio completes. if 0, do not update any state
1377 int FileJournal::write_aio_bl(off64_t
& pos
, bufferlist
& bl
, uint64_t seq
)
1379 dout(20) << "write_aio_bl " << pos
<< "~" << bl
.length() << " seq " << seq
<< dendl
;
1381 while (bl
.length() > 0) {
1382 int max
= MIN(bl
.get_num_buffers(), IOV_MAX
-1);
1383 iovec
*iov
= new iovec
[max
];
1386 for (std::list
<buffer::ptr
>::const_iterator p
= bl
.buffers().begin();
1389 assert(p
!= bl
.buffers().end());
1390 iov
[n
].iov_base
= (void *)p
->c_str();
1391 iov
[n
].iov_len
= p
->length();
1396 bl
.splice(0, len
, &tbl
); // move bytes from bl -> tbl
1398 // lock only aio_queue, current aio, aio_num, aio_bytes, which may be
1399 // modified in check_aio_completion
1401 aio_queue
.push_back(aio_info(tbl
, pos
, bl
.length() > 0 ? 0 : seq
));
1402 aio_info
& aio
= aio_queue
.back();
1405 io_prep_pwritev(&aio
.iocb
, fd
, aio
.iov
, n
, pos
);
1407 dout(20) << "write_aio_bl .. " << aio
.off
<< "~" << aio
.len
1408 << " in " << n
<< dendl
;
1411 aio_bytes
+= aio
.len
;
1413 // need to save current aio len to update write_pos later because current
1414 // aio could be ereased from aio_queue once it is done
1415 uint64_t cur_len
= aio
.len
;
1416 // unlock aio_lock because following io_submit might take time to return
1419 iocb
*piocb
= &aio
.iocb
;
1421 // 2^16 * 125us = ~8 seconds, so max sleep is ~16 seconds
1425 int r
= io_submit(aio_ctx
, 1, &piocb
);
1426 dout(20) << "write_aio_bl io_submit return value: " << r
<< dendl
;
1428 derr
<< "io_submit to " << aio
.off
<< "~" << cur_len
1429 << " got " << cpp_strerror(r
) << dendl
;
1430 if (r
== -EAGAIN
&& attempts
-- > 0) {
1435 check_align(pos
, tbl
);
1436 assert(0 == "io_submit got unexpected error");
1444 write_finish_cond
.Signal();
1450 void FileJournal::write_finish_thread_entry()
1453 dout(10) << "write_finish_thread_entry enter" << dendl
;
1456 Mutex::Locker
locker(aio_lock
);
1457 if (aio_queue
.empty()) {
1460 dout(20) << "write_finish_thread_entry sleeping" << dendl
;
1461 write_finish_cond
.Wait(aio_lock
);
1466 dout(20) << "write_finish_thread_entry waiting for aio(s)" << dendl
;
1468 int r
= io_getevents(aio_ctx
, 1, 16, event
, NULL
);
1471 dout(0) << "io_getevents got " << cpp_strerror(r
) << dendl
;
1474 derr
<< "io_getevents got " << cpp_strerror(r
) << dendl
;
1475 assert(0 == "got unexpected error from io_getevents");
1479 Mutex::Locker
locker(aio_lock
);
1480 for (int i
=0; i
<r
; i
++) {
1481 aio_info
*ai
= (aio_info
*)event
[i
].obj
;
1482 if (event
[i
].res
!= ai
->len
) {
1483 derr
<< "aio to " << ai
->off
<< "~" << ai
->len
1484 << " returned: " << (int)event
[i
].res
<< dendl
;
1485 assert(0 == "unexpected aio error");
1487 dout(10) << "write_finish_thread_entry aio " << ai
->off
1488 << "~" << ai
->len
<< " done" << dendl
;
1491 check_aio_completion();
1494 dout(10) << "write_finish_thread_entry exit" << dendl
;
1500 * check aio_wait for completed aio, and update state appropriately.
1502 void FileJournal::check_aio_completion()
1504 assert(aio_lock
.is_locked());
1505 dout(20) << "check_aio_completion" << dendl
;
1507 bool completed_something
= false, signal
= false;
1508 uint64_t new_journaled_seq
= 0;
1510 list
<aio_info
>::iterator p
= aio_queue
.begin();
1511 while (p
!= aio_queue
.end() && p
->done
) {
1512 dout(20) << "check_aio_completion completed seq " << p
->seq
<< " "
1513 << p
->off
<< "~" << p
->len
<< dendl
;
1515 new_journaled_seq
= p
->seq
;
1516 completed_something
= true;
1519 aio_bytes
-= p
->len
;
1520 aio_queue
.erase(p
++);
1524 if (completed_something
) {
1526 // only if we haven't filled up recently!
1527 Mutex::Locker
locker(finisher_lock
);
1528 journaled_seq
= new_journaled_seq
;
1529 if (full_state
!= FULL_NOTFULL
) {
1530 dout(10) << "check_aio_completion NOT queueing finisher seq " << journaled_seq
1531 << ", full_commit_seq|full_restart_seq" << dendl
;
1533 if (plug_journal_completions
) {
1534 dout(20) << "check_aio_completion NOT queueing finishers through seq " << journaled_seq
1535 << " due to completion plug" << dendl
;
1537 dout(20) << "check_aio_completion queueing finishers through seq " << journaled_seq
<< dendl
;
1538 queue_completions_thru(journaled_seq
);
1543 // maybe write queue was waiting for aio count to drop?
1549 int FileJournal::prepare_entry(vector
<ObjectStore::Transaction
>& tls
, bufferlist
* tbl
) {
1550 dout(10) << "prepare_entry " << tls
<< dendl
;
1551 int data_len
= cct
->_conf
->journal_align_min_size
- 1;
1552 int data_align
= -1; // -1 indicates that we don't care about the alignment
1554 for (vector
<ObjectStore::Transaction
>::iterator p
= tls
.begin();
1555 p
!= tls
.end(); ++p
) {
1556 if ((int)(*p
).get_data_length() > data_len
) {
1557 data_len
= (*p
).get_data_length();
1558 data_align
= ((*p
).get_data_alignment() - bl
.length()) & ~CEPH_PAGE_MASK
;
1562 if (tbl
->length()) {
1563 bl
.claim_append(*tbl
);
1565 // add it this entry
1567 unsigned head_size
= sizeof(entry_header_t
);
1568 off64_t base_size
= 2*head_size
+ bl
.length();
1569 memset(&h
, 0, sizeof(h
));
1570 if (data_align
>= 0)
1571 h
.pre_pad
= ((unsigned int)data_align
- (unsigned int)head_size
) & ~CEPH_PAGE_MASK
;
1572 off64_t size
= ROUND_UP_TO(base_size
+ h
.pre_pad
, header
.alignment
);
1573 unsigned post_pad
= size
- base_size
- h
.pre_pad
;
1574 h
.len
= bl
.length();
1575 h
.post_pad
= post_pad
;
1576 h
.crc32c
= bl
.crc32c(0);
1577 dout(10) << " len " << bl
.length() << " -> " << size
1578 << " (head " << head_size
<< " pre_pad " << h
.pre_pad
1579 << " bl " << bl
.length() << " post_pad " << post_pad
<< " tail " << head_size
<< ")"
1580 << " (bl alignment " << data_align
<< ")"
1584 ebl
.append((const char*)&h
, sizeof(h
));
1586 ebl
.push_back(buffer::create_static(h
.pre_pad
, zero_buf
));
1589 ebl
.claim_append(bl
, buffer::list::CLAIM_ALLOW_NONSHAREABLE
); // potential zero-copy
1591 ebl
.push_back(buffer::create_static(h
.post_pad
, zero_buf
));
1594 ebl
.append((const char*)&h
, sizeof(h
));
1596 ebl
.rebuild_aligned(CEPH_DIRECTIO_ALIGNMENT
);
1601 void FileJournal::submit_entry(uint64_t seq
, bufferlist
& e
, uint32_t orig_len
,
1602 Context
*oncommit
, TrackedOpRef osd_op
)
1605 dout(5) << "submit_entry seq " << seq
1606 << " len " << e
.length()
1607 << " (" << oncommit
<< ")" << dendl
;
1608 assert(e
.length() > 0);
1609 assert(e
.length() < header
.max_size
);
1612 osd_op
->mark_event("commit_queued_for_journal_write");
1614 logger
->inc(l_filestore_journal_queue_bytes
, orig_len
);
1615 logger
->inc(l_filestore_journal_queue_ops
, 1);
1618 throttle
.register_throttle_seq(seq
, e
.length());
1620 logger
->inc(l_filestore_journal_ops
, 1);
1621 logger
->inc(l_filestore_journal_bytes
, e
.length());
1625 osd_op
->mark_event("commit_queued_for_journal_write");
1626 if (osd_op
->store_trace
) {
1627 osd_op
->journal_trace
.init("journal", &trace_endpoint
, &osd_op
->store_trace
);
1628 osd_op
->journal_trace
.event("submit_entry");
1629 osd_op
->journal_trace
.keyval("seq", seq
);
1633 Mutex::Locker
l1(writeq_lock
);
1635 Mutex::Locker
l2(aio_lock
);
1637 Mutex::Locker
l3(completions_lock
);
1640 aio_write_queue_ops
++;
1641 aio_write_queue_bytes
+= e
.length();
1645 completions
.push_back(
1647 seq
, oncommit
, ceph_clock_now(), osd_op
));
1649 writeq_cond
.Signal();
1650 writeq
.push_back(write_item(seq
, e
, orig_len
, osd_op
));
1652 osd_op
->journal_trace
.keyval("queue depth", writeq
.size());
1656 bool FileJournal::writeq_empty()
1658 Mutex::Locker
locker(writeq_lock
);
1659 return writeq
.empty();
1662 FileJournal::write_item
&FileJournal::peek_write()
1664 assert(write_lock
.is_locked());
1665 Mutex::Locker
locker(writeq_lock
);
1666 return writeq
.front();
1669 void FileJournal::pop_write()
1671 assert(write_lock
.is_locked());
1672 Mutex::Locker
locker(writeq_lock
);
1674 logger
->dec(l_filestore_journal_queue_bytes
, writeq
.front().orig_len
);
1675 logger
->dec(l_filestore_journal_queue_ops
, 1);
1680 void FileJournal::batch_pop_write(list
<write_item
> &items
)
1682 assert(write_lock
.is_locked());
1684 Mutex::Locker
locker(writeq_lock
);
1687 for (auto &&i
: items
) {
1689 logger
->dec(l_filestore_journal_queue_bytes
, i
.orig_len
);
1690 logger
->dec(l_filestore_journal_queue_ops
, 1);
1695 void FileJournal::batch_unpop_write(list
<write_item
> &items
)
1697 assert(write_lock
.is_locked());
1698 for (auto &&i
: items
) {
1700 logger
->inc(l_filestore_journal_queue_bytes
, i
.orig_len
);
1701 logger
->inc(l_filestore_journal_queue_ops
, 1);
1704 Mutex::Locker
locker(writeq_lock
);
1705 writeq
.splice(writeq
.begin(), items
);
1708 void FileJournal::commit_start(uint64_t seq
)
1710 dout(10) << "commit_start" << dendl
;
1713 switch (full_state
) {
1718 if (seq
>= journaled_seq
) {
1719 dout(1) << " FULL_FULL -> FULL_WAIT. commit_start on seq "
1720 << seq
<< " > journaled_seq " << journaled_seq
1721 << ", moving to FULL_WAIT."
1723 full_state
= FULL_WAIT
;
1725 dout(1) << "FULL_FULL commit_start on seq "
1726 << seq
<< " < journaled_seq " << journaled_seq
1727 << ", remaining in FULL_FULL"
1733 dout(1) << " FULL_WAIT -> FULL_NOTFULL. journal now active, setting completion plug." << dendl
;
1734 full_state
= FULL_NOTFULL
;
1735 plug_journal_completions
= true;
1741 *send discard command to joural block deivce
1743 void FileJournal::do_discard(int64_t offset
, int64_t end
)
1745 dout(10) << __func__
<< "trim(" << offset
<< ", " << end
<< dendl
;
1747 offset
= ROUND_UP_TO(offset
, block_size
);
1750 end
= ROUND_UP_TO(end
- block_size
, block_size
);
1751 assert(end
>= offset
);
1753 if (block_device_discard(fd
, offset
, end
- offset
) < 0)
1754 dout(1) << __func__
<< "ioctl(BLKDISCARD) error:" << cpp_strerror(errno
) << dendl
;
1757 void FileJournal::committed_thru(uint64_t seq
)
1759 Mutex::Locker
locker(write_lock
);
1761 auto released
= throttle
.flush(seq
);
1763 logger
->dec(l_filestore_journal_ops
, released
.first
);
1764 logger
->dec(l_filestore_journal_bytes
, released
.second
);
1767 if (seq
< last_committed_seq
) {
1768 dout(5) << "committed_thru " << seq
<< " < last_committed_seq " << last_committed_seq
<< dendl
;
1769 assert(seq
>= last_committed_seq
);
1772 if (seq
== last_committed_seq
) {
1773 dout(5) << "committed_thru " << seq
<< " == last_committed_seq " << last_committed_seq
<< dendl
;
1777 dout(5) << "committed_thru " << seq
<< " (last_committed_seq " << last_committed_seq
<< ")" << dendl
;
1778 last_committed_seq
= seq
;
1782 Mutex::Locker
locker(finisher_lock
);
1783 queue_completions_thru(seq
);
1784 if (plug_journal_completions
&& seq
>= header
.start_seq
) {
1785 dout(10) << " removing completion plug, queuing completions thru journaled_seq " << journaled_seq
<< dendl
;
1786 plug_journal_completions
= false;
1787 queue_completions_thru(journaled_seq
);
1791 // adjust start pointer
1792 while (!journalq
.empty() && journalq
.front().first
<= seq
) {
1793 journalq
.pop_front();
1796 int64_t old_start
= header
.start
;
1797 if (!journalq
.empty()) {
1798 header
.start
= journalq
.front().second
;
1799 header
.start_seq
= journalq
.front().first
;
1801 header
.start
= write_pos
;
1802 header
.start_seq
= seq
+ 1;
1806 dout(10) << __func__
<< " will trim (" << old_start
<< ", " << header
.start
<< ")" << dendl
;
1807 if (old_start
< header
.start
)
1808 do_discard(old_start
, header
.start
- 1);
1810 do_discard(old_start
, header
.max_size
- 1);
1811 do_discard(get_top(), header
.start
- 1);
1815 must_write_header
= true;
1816 print_header(header
);
1818 // committed but unjournaled items
1819 while (!writeq_empty() && peek_write().seq
<= seq
) {
1820 dout(15) << " dropping committed but unwritten seq " << peek_write().seq
1821 << " len " << peek_write().bl
.length()
1823 complete_write(1, peek_write().orig_len
);
1827 commit_cond
.Signal();
1829 dout(10) << "committed_thru done" << dendl
;
1833 void FileJournal::complete_write(uint64_t ops
, uint64_t bytes
)
1835 dout(5) << __func__
<< " finished " << ops
<< " ops and "
1836 << bytes
<< " bytes" << dendl
;
1839 int FileJournal::make_writeable()
1841 dout(10) << __func__
<< dendl
;
1842 int r
= set_throttle_params();
1851 write_pos
= read_pos
;
1853 write_pos
= get_top();
1856 must_write_header
= true;
1862 int FileJournal::set_throttle_params()
1865 bool valid
= throttle
.set_params(
1866 cct
->_conf
->journal_throttle_low_threshhold
,
1867 cct
->_conf
->journal_throttle_high_threshhold
,
1868 cct
->_conf
->filestore_expected_throughput_bytes
,
1869 cct
->_conf
->journal_throttle_high_multiple
,
1870 cct
->_conf
->journal_throttle_max_multiple
,
1871 header
.max_size
- get_top(),
1875 derr
<< "tried to set invalid params: "
1879 return valid
? 0 : -EINVAL
;
1882 const char** FileJournal::get_tracked_conf_keys() const
1884 static const char *KEYS
[] = {
1885 "journal_throttle_low_threshhold",
1886 "journal_throttle_high_threshhold",
1887 "journal_throttle_high_multiple",
1888 "journal_throttle_max_multiple",
1889 "filestore_expected_throughput_bytes",
1894 void FileJournal::wrap_read_bl(
1902 while (pos
>= header
.max_size
)
1903 pos
= pos
+ get_top() - header
.max_size
;
1906 if (pos
+ olen
> header
.max_size
)
1907 len
= header
.max_size
- pos
; // partial
1911 int64_t actual
= ::lseek64(fd
, pos
, SEEK_SET
);
1912 assert(actual
== pos
);
1914 bufferptr bp
= buffer::create(len
);
1915 int r
= safe_read_exact(fd
, bp
.c_str(), len
);
1917 derr
<< "FileJournal::wrap_read_bl: safe_read_exact " << pos
<< "~" << len
<< " returned "
1921 bl
->push_back(std::move(bp
));
1925 if (pos
>= header
.max_size
)
1926 pos
= pos
+ get_top() - header
.max_size
;
1931 bool FileJournal::read_entry(
1938 uint64_t seq
= next_seq
;
1941 dout(2) << "read_entry -- not readable" << dendl
;
1945 off64_t pos
= read_pos
;
1946 off64_t next_pos
= pos
;
1948 read_entry_result result
= do_read_entry(
1954 if (result
== SUCCESS
) {
1955 journalq
.push_back( pair
<uint64_t,off64_t
>(seq
, pos
));
1956 uint64_t amount_to_take
=
1959 (header
.max_size
- pos
) + (next_pos
- get_top());
1960 throttle
.take(amount_to_take
);
1961 throttle
.register_throttle_seq(next_seq
, amount_to_take
);
1963 logger
->inc(l_filestore_journal_ops
, 1);
1964 logger
->inc(l_filestore_journal_bytes
, amount_to_take
);
1966 if (next_seq
> seq
) {
1969 read_pos
= next_pos
;
1971 if (seq
> journaled_seq
)
1972 journaled_seq
= seq
;
1977 if (seq
&& seq
< header
.committed_up_to
) {
1978 derr
<< "Unable to read past sequence " << seq
1979 << " but header indicates the journal has committed up through "
1980 << header
.committed_up_to
<< ", journal is corrupt" << dendl
;
1981 if (cct
->_conf
->journal_ignore_corruption
) {
1990 dout(25) << ss
.str() << dendl
;
1991 dout(2) << "No further valid entries found, journal is most likely valid"
1996 FileJournal::read_entry_result
FileJournal::do_read_entry(
2002 entry_header_t
*_h
) const
2004 off64_t cur_pos
= init_pos
;
2013 wrap_read_bl(cur_pos
, sizeof(*h
), &hbl
, &_next_pos
);
2014 h
= reinterpret_cast<entry_header_t
*>(hbl
.c_str());
2016 if (!h
->check_magic(cur_pos
, header
.get_fsid64())) {
2017 dout(25) << "read_entry " << init_pos
2018 << " : bad header magic, end of journal" << dendl
;
2020 *ss
<< "bad header magic";
2022 *next_pos
= init_pos
+ (4<<10); // check 4k ahead
2023 return MAYBE_CORRUPT
;
2025 cur_pos
= _next_pos
;
2029 cur_pos
+= h
->pre_pad
;
2032 wrap_read_bl(cur_pos
, h
->len
, bl
, &cur_pos
);
2035 cur_pos
+= h
->post_pad
;
2040 wrap_read_bl(cur_pos
, sizeof(*f
), &fbl
, &cur_pos
);
2041 f
= reinterpret_cast<entry_header_t
*>(fbl
.c_str());
2042 if (memcmp(f
, h
, sizeof(*f
))) {
2044 *ss
<< "bad footer magic, partial entry";
2046 *next_pos
= cur_pos
;
2047 return MAYBE_CORRUPT
;
2050 if ((header
.flags
& header_t::FLAG_CRC
) || // if explicitly enabled (new journal)
2051 h
->crc32c
!= 0) { // newer entry in old journal
2052 uint32_t actual_crc
= bl
->crc32c(0);
2053 if (actual_crc
!= h
->crc32c
) {
2055 *ss
<< "header crc (" << h
->crc32c
2056 << ") doesn't match body crc (" << actual_crc
<< ")";
2058 *next_pos
= cur_pos
;
2059 return MAYBE_CORRUPT
;
2064 dout(2) << "read_entry " << init_pos
<< " : seq " << h
->seq
2065 << " " << h
->len
<< " bytes"
2074 *next_pos
= cur_pos
;
2079 assert(cur_pos
% header
.alignment
== 0);
2083 void FileJournal::reserve_throttle_and_backoff(uint64_t count
)
2085 throttle
.get(count
);
2088 void FileJournal::get_header(
2089 uint64_t wanted_seq
,
2093 off64_t pos
= header
.start
;
2094 off64_t next_pos
= pos
;
2097 dout(2) << __func__
<< dendl
;
2101 read_entry_result result
= do_read_entry(
2108 if (result
== FAILURE
|| result
== MAYBE_CORRUPT
)
2110 if (seq
== wanted_seq
) {
2116 ceph_abort(); // not reachable
2119 void FileJournal::corrupt(
2123 dout(2) << __func__
<< dendl
;
2124 if (corrupt_at
>= header
.max_size
)
2125 corrupt_at
= corrupt_at
+ get_top() - header
.max_size
;
2127 int64_t actual
= ::lseek64(fd
, corrupt_at
, SEEK_SET
);
2128 assert(actual
== corrupt_at
);
2131 int r
= safe_read_exact(fd
, buf
, 1);
2134 actual
= ::lseek64(wfd
, corrupt_at
, SEEK_SET
);
2135 assert(actual
== corrupt_at
);
2138 r
= safe_write(wfd
, buf
, 1);
2142 void FileJournal::corrupt_payload(
2146 dout(2) << __func__
<< dendl
;
2149 get_header(seq
, &pos
, &h
);
2150 off64_t corrupt_at
=
2151 pos
+ sizeof(entry_header_t
) + h
.pre_pad
;
2152 corrupt(wfd
, corrupt_at
);
2156 void FileJournal::corrupt_footer_magic(
2160 dout(2) << __func__
<< dendl
;
2163 get_header(seq
, &pos
, &h
);
2164 off64_t corrupt_at
=
2165 pos
+ sizeof(entry_header_t
) + h
.pre_pad
+
2166 h
.len
+ h
.post_pad
+
2167 (reinterpret_cast<char*>(&h
.magic2
) - reinterpret_cast<char*>(&h
));
2168 corrupt(wfd
, corrupt_at
);
2172 void FileJournal::corrupt_header_magic(
2176 dout(2) << __func__
<< dendl
;
2179 get_header(seq
, &pos
, &h
);
2180 off64_t corrupt_at
=
2182 (reinterpret_cast<char*>(&h
.magic2
) - reinterpret_cast<char*>(&h
));
2183 corrupt(wfd
, corrupt_at
);
2186 off64_t
FileJournal::get_journal_size_estimate()
2188 off64_t size
, start
= header
.start
;
2189 if (write_pos
< start
) {
2190 size
= (max_size
- start
) + write_pos
;
2192 size
= write_pos
- start
;
2194 dout(20) << __func__
<< " journal size=" << size
<< dendl
;