]> git.proxmox.com Git - ceph.git/blob - ceph/src/osdc/Journaler.cc
update sources to v12.2.3
[ceph.git] / ceph / src / osdc / Journaler.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15 #include "common/perf_counters.h"
16 #include "common/dout.h"
17 #include "include/Context.h"
18 #include "msg/Messenger.h"
19 #include "osdc/Journaler.h"
20 #include "common/errno.h"
21 #include "include/assert.h"
22 #include "common/Finisher.h"
23
24 #define dout_subsys ceph_subsys_journaler
25 #undef dout_prefix
26 #define dout_prefix *_dout << objecter->messenger->get_myname() \
27 << ".journaler." << name << (readonly ? "(ro) ":"(rw) ")
28
29 using std::chrono::seconds;
30
31
32 class Journaler::C_DelayFlush : public Context {
33 Journaler *journaler;
34 public:
35 C_DelayFlush(Journaler *j) : journaler(j) {}
36 void finish(int r) override {
37 journaler->_do_delayed_flush();
38 }
39 };
40
41 void Journaler::set_readonly()
42 {
43 lock_guard l(lock);
44
45 ldout(cct, 1) << "set_readonly" << dendl;
46 readonly = true;
47 }
48
49 void Journaler::set_writeable()
50 {
51 lock_guard l(lock);
52
53 ldout(cct, 1) << "set_writeable" << dendl;
54 readonly = false;
55 }
56
57 void Journaler::create(file_layout_t *l, stream_format_t const sf)
58 {
59 lock_guard lk(lock);
60
61 assert(!readonly);
62 state = STATE_ACTIVE;
63
64 stream_format = sf;
65 journal_stream.set_format(sf);
66 _set_layout(l);
67
68 prezeroing_pos = prezero_pos = write_pos = flush_pos =
69 safe_pos = read_pos = requested_pos = received_pos =
70 expire_pos = trimming_pos = trimmed_pos =
71 next_safe_pos = layout.get_period();
72
73 ldout(cct, 1) << "created blank journal at inode 0x" << std::hex << ino
74 << std::dec << ", format=" << stream_format << dendl;
75 }
76
77 void Journaler::set_layout(file_layout_t const *l)
78 {
79 lock_guard lk(lock);
80 _set_layout(l);
81 }
82
83 void Journaler::_set_layout(file_layout_t const *l)
84 {
85 layout = *l;
86
87 if (layout.pool_id != pg_pool) {
88 // user can reset pool id through cephfs-journal-tool
89 lderr(cct) << "may got older pool id from header layout" << dendl;
90 ceph_abort();
91 }
92 last_written.layout = layout;
93 last_committed.layout = layout;
94
95 // prefetch intelligently.
96 // (watch out, this is big if you use big objects or weird striping)
97 uint64_t periods = cct->_conf->journaler_prefetch_periods;
98 if (periods < 2)
99 periods = 2; // we need at least 2 periods to make progress.
100 fetch_len = layout.get_period() * periods;
101 }
102
103
104 /***************** HEADER *******************/
105
106 ostream& operator<<(ostream &out, const Journaler::Header &h)
107 {
108 return out << "loghead(trim " << h.trimmed_pos
109 << ", expire " << h.expire_pos
110 << ", write " << h.write_pos
111 << ", stream_format " << (int)(h.stream_format)
112 << ")";
113 }
114
115 class Journaler::C_ReadHead : public Context {
116 Journaler *ls;
117 public:
118 bufferlist bl;
119 explicit C_ReadHead(Journaler *l) : ls(l) {}
120 void finish(int r) override {
121 ls->_finish_read_head(r, bl);
122 }
123 };
124
125 class Journaler::C_RereadHead : public Context {
126 Journaler *ls;
127 Context *onfinish;
128 public:
129 bufferlist bl;
130 C_RereadHead(Journaler *l, Context *onfinish_) : ls (l),
131 onfinish(onfinish_) {}
132 void finish(int r) override {
133 ls->_finish_reread_head(r, bl, onfinish);
134 }
135 };
136
137 class Journaler::C_ProbeEnd : public Context {
138 Journaler *ls;
139 public:
140 uint64_t end;
141 explicit C_ProbeEnd(Journaler *l) : ls(l), end(-1) {}
142 void finish(int r) override {
143 ls->_finish_probe_end(r, end);
144 }
145 };
146
147 class Journaler::C_ReProbe : public Context {
148 Journaler *ls;
149 C_OnFinisher *onfinish;
150 public:
151 uint64_t end;
152 C_ReProbe(Journaler *l, C_OnFinisher *onfinish_) :
153 ls(l), onfinish(onfinish_), end(0) {}
154 void finish(int r) override {
155 ls->_finish_reprobe(r, end, onfinish);
156 }
157 };
158
159 void Journaler::recover(Context *onread)
160 {
161 lock_guard l(lock);
162 if (is_stopping()) {
163 onread->complete(-EAGAIN);
164 return;
165 }
166
167 ldout(cct, 1) << "recover start" << dendl;
168 assert(state != STATE_ACTIVE);
169 assert(readonly);
170
171 if (onread)
172 waitfor_recover.push_back(wrap_finisher(onread));
173
174 if (state != STATE_UNDEF) {
175 ldout(cct, 1) << "recover - already recovering" << dendl;
176 return;
177 }
178
179 ldout(cct, 1) << "read_head" << dendl;
180 state = STATE_READHEAD;
181 C_ReadHead *fin = new C_ReadHead(this);
182 _read_head(fin, &fin->bl);
183 }
184
185 void Journaler::_read_head(Context *on_finish, bufferlist *bl)
186 {
187 // lock is locked
188 assert(state == STATE_READHEAD || state == STATE_REREADHEAD);
189
190 object_t oid = file_object_t(ino, 0);
191 object_locator_t oloc(pg_pool);
192 objecter->read_full(oid, oloc, CEPH_NOSNAP, bl, 0, wrap_finisher(on_finish));
193 }
194
195 void Journaler::reread_head(Context *onfinish)
196 {
197 lock_guard l(lock);
198 _reread_head(wrap_finisher(onfinish));
199 }
200
201 /**
202 * Re-read the head from disk, and set the write_pos, expire_pos, trimmed_pos
203 * from the on-disk header. This switches the state to STATE_REREADHEAD for
204 * the duration, and you shouldn't start a re-read while other operations are
205 * in-flight, nor start other operations while a re-read is in progress.
206 * Also, don't call this until the Journaler has finished its recovery and has
207 * gone STATE_ACTIVE!
208 */
209 void Journaler::_reread_head(Context *onfinish)
210 {
211 ldout(cct, 10) << "reread_head" << dendl;
212 assert(state == STATE_ACTIVE);
213
214 state = STATE_REREADHEAD;
215 C_RereadHead *fin = new C_RereadHead(this, onfinish);
216 _read_head(fin, &fin->bl);
217 }
218
219 void Journaler::_finish_reread_head(int r, bufferlist& bl, Context *finish)
220 {
221 lock_guard l(lock);
222 if (is_stopping()) {
223 finish->complete(-EAGAIN);
224 return;
225 }
226
227 //read on-disk header into
228 assert(bl.length() || r < 0 );
229
230 // unpack header
231 if (r == 0) {
232 Header h;
233 bufferlist::iterator p = bl.begin();
234 try {
235 ::decode(h, p);
236 } catch (const buffer::error &e) {
237 finish->complete(-EINVAL);
238 return;
239 }
240 prezeroing_pos = prezero_pos = write_pos = flush_pos = safe_pos = next_safe_pos
241 = h.write_pos;
242 expire_pos = h.expire_pos;
243 trimmed_pos = trimming_pos = h.trimmed_pos;
244 init_headers(h);
245 state = STATE_ACTIVE;
246 }
247
248 finish->complete(r);
249 }
250
251 void Journaler::_finish_read_head(int r, bufferlist& bl)
252 {
253 lock_guard l(lock);
254 if (is_stopping())
255 return;
256
257 assert(state == STATE_READHEAD);
258
259 if (r!=0) {
260 ldout(cct, 0) << "error getting journal off disk" << dendl;
261 list<Context*> ls;
262 ls.swap(waitfor_recover);
263 finish_contexts(cct, ls, r);
264 return;
265 }
266
267 if (bl.length() == 0) {
268 ldout(cct, 1) << "_finish_read_head r=" << r
269 << " read 0 bytes, assuming empty log" << dendl;
270 state = STATE_ACTIVE;
271 list<Context*> ls;
272 ls.swap(waitfor_recover);
273 finish_contexts(cct, ls, 0);
274 return;
275 }
276
277 // unpack header
278 bool corrupt = false;
279 Header h;
280 bufferlist::iterator p = bl.begin();
281 try {
282 ::decode(h, p);
283
284 if (h.magic != magic) {
285 ldout(cct, 0) << "on disk magic '" << h.magic << "' != my magic '"
286 << magic << "'" << dendl;
287 corrupt = true;
288 } else if (h.write_pos < h.expire_pos || h.expire_pos < h.trimmed_pos) {
289 ldout(cct, 0) << "Corrupt header (bad offsets): " << h << dendl;
290 corrupt = true;
291 }
292 } catch (const buffer::error &e) {
293 corrupt = true;
294 }
295
296 if (corrupt) {
297 list<Context*> ls;
298 ls.swap(waitfor_recover);
299 finish_contexts(cct, ls, -EINVAL);
300 return;
301 }
302
303 prezeroing_pos = prezero_pos = write_pos = flush_pos = safe_pos = next_safe_pos
304 = h.write_pos;
305 read_pos = requested_pos = received_pos = expire_pos = h.expire_pos;
306 trimmed_pos = trimming_pos = h.trimmed_pos;
307
308 init_headers(h);
309 _set_layout(&h.layout);
310 stream_format = h.stream_format;
311 journal_stream.set_format(h.stream_format);
312
313 ldout(cct, 1) << "_finish_read_head " << h
314 << ". probing for end of log (from " << write_pos << ")..."
315 << dendl;
316 C_ProbeEnd *fin = new C_ProbeEnd(this);
317 state = STATE_PROBING;
318 _probe(fin, &fin->end);
319 }
320
321 void Journaler::_probe(Context *finish, uint64_t *end)
322 {
323 // lock is locked
324 ldout(cct, 1) << "probing for end of the log" << dendl;
325 assert(state == STATE_PROBING || state == STATE_REPROBING);
326 // probe the log
327 filer.probe(ino, &layout, CEPH_NOSNAP,
328 write_pos, end, true, 0, wrap_finisher(finish));
329 }
330
331 void Journaler::_reprobe(C_OnFinisher *finish)
332 {
333 ldout(cct, 10) << "reprobe" << dendl;
334 assert(state == STATE_ACTIVE);
335
336 state = STATE_REPROBING;
337 C_ReProbe *fin = new C_ReProbe(this, finish);
338 _probe(fin, &fin->end);
339 }
340
341
342 void Journaler::_finish_reprobe(int r, uint64_t new_end,
343 C_OnFinisher *onfinish)
344 {
345 lock_guard l(lock);
346 if (is_stopping()) {
347 onfinish->complete(-EAGAIN);
348 return;
349 }
350
351 assert(new_end >= write_pos || r < 0);
352 ldout(cct, 1) << "_finish_reprobe new_end = " << new_end
353 << " (header had " << write_pos << ")."
354 << dendl;
355 prezeroing_pos = prezero_pos = write_pos = flush_pos = safe_pos = next_safe_pos = new_end;
356 state = STATE_ACTIVE;
357 onfinish->complete(r);
358 }
359
360 void Journaler::_finish_probe_end(int r, uint64_t end)
361 {
362 lock_guard l(lock);
363 if (is_stopping())
364 return;
365
366 assert(state == STATE_PROBING);
367 if (r < 0) { // error in probing
368 goto out;
369 }
370 if (((int64_t)end) == -1) {
371 end = write_pos;
372 ldout(cct, 1) << "_finish_probe_end write_pos = " << end << " (header had "
373 << write_pos << "). log was empty. recovered." << dendl;
374 ceph_abort(); // hrm.
375 } else {
376 assert(end >= write_pos);
377 ldout(cct, 1) << "_finish_probe_end write_pos = " << end
378 << " (header had " << write_pos << "). recovered."
379 << dendl;
380 }
381
382 state = STATE_ACTIVE;
383
384 prezeroing_pos = prezero_pos = write_pos = flush_pos = safe_pos = next_safe_pos = end;
385
386 out:
387 // done.
388 list<Context*> ls;
389 ls.swap(waitfor_recover);
390 finish_contexts(cct, ls, r);
391 }
392
393 class Journaler::C_RereadHeadProbe : public Context
394 {
395 Journaler *ls;
396 C_OnFinisher *final_finish;
397 public:
398 C_RereadHeadProbe(Journaler *l, C_OnFinisher *finish) :
399 ls(l), final_finish(finish) {}
400 void finish(int r) override {
401 ls->_finish_reread_head_and_probe(r, final_finish);
402 }
403 };
404
405 void Journaler::reread_head_and_probe(Context *onfinish)
406 {
407 lock_guard l(lock);
408
409 assert(state == STATE_ACTIVE);
410 _reread_head(new C_RereadHeadProbe(this, wrap_finisher(onfinish)));
411 }
412
413 void Journaler::_finish_reread_head_and_probe(int r, C_OnFinisher *onfinish)
414 {
415 // Expect to be called back from finish_reread_head, which already takes lock
416 // lock is locked
417 if (is_stopping()) {
418 onfinish->complete(-EAGAIN);
419 return;
420 }
421
422 assert(!r); //if we get an error, we're boned
423 _reprobe(onfinish);
424 }
425
426
427 // WRITING
428
429 class Journaler::C_WriteHead : public Context {
430 public:
431 Journaler *ls;
432 Header h;
433 C_OnFinisher *oncommit;
434 C_WriteHead(Journaler *l, Header& h_, C_OnFinisher *c) : ls(l), h(h_),
435 oncommit(c) {}
436 void finish(int r) override {
437 ls->_finish_write_head(r, h, oncommit);
438 }
439 };
440
441 void Journaler::write_head(Context *oncommit)
442 {
443 lock_guard l(lock);
444 _write_head(oncommit);
445 }
446
447
448 void Journaler::_write_head(Context *oncommit)
449 {
450 assert(!readonly);
451 assert(state == STATE_ACTIVE);
452 last_written.trimmed_pos = trimmed_pos;
453 last_written.expire_pos = expire_pos;
454 last_written.unused_field = expire_pos;
455 last_written.write_pos = safe_pos;
456 last_written.stream_format = stream_format;
457 ldout(cct, 10) << "write_head " << last_written << dendl;
458
459 // Avoid persisting bad pointers in case of bugs
460 assert(last_written.write_pos >= last_written.expire_pos);
461 assert(last_written.expire_pos >= last_written.trimmed_pos);
462
463 last_wrote_head = ceph::real_clock::now();
464
465 bufferlist bl;
466 ::encode(last_written, bl);
467 SnapContext snapc;
468
469 object_t oid = file_object_t(ino, 0);
470 object_locator_t oloc(pg_pool);
471 objecter->write_full(oid, oloc, snapc, bl, ceph::real_clock::now(), 0,
472 wrap_finisher(new C_WriteHead(
473 this, last_written,
474 wrap_finisher(oncommit))),
475 0, 0, write_iohint);
476 }
477
478 void Journaler::_finish_write_head(int r, Header &wrote,
479 C_OnFinisher *oncommit)
480 {
481 lock_guard l(lock);
482
483 if (r < 0) {
484 lderr(cct) << "_finish_write_head got " << cpp_strerror(r) << dendl;
485 handle_write_error(r);
486 return;
487 }
488 assert(!readonly);
489 ldout(cct, 10) << "_finish_write_head " << wrote << dendl;
490 last_committed = wrote;
491 if (oncommit) {
492 oncommit->complete(r);
493 }
494
495 _trim(); // trim?
496 }
497
498
499 /***************** WRITING *******************/
500
501 class Journaler::C_Flush : public Context {
502 Journaler *ls;
503 uint64_t start;
504 ceph::real_time stamp;
505 public:
506 C_Flush(Journaler *l, int64_t s, ceph::real_time st)
507 : ls(l), start(s), stamp(st) {}
508 void finish(int r) override {
509 ls->_finish_flush(r, start, stamp);
510 }
511 };
512
513 void Journaler::_finish_flush(int r, uint64_t start, ceph::real_time stamp)
514 {
515 lock_guard l(lock);
516 assert(!readonly);
517
518 if (r < 0) {
519 lderr(cct) << "_finish_flush got " << cpp_strerror(r) << dendl;
520 handle_write_error(r);
521 return;
522 }
523
524 assert(start < flush_pos);
525
526 // calc latency?
527 if (logger) {
528 ceph::timespan lat = ceph::real_clock::now() - stamp;
529 logger->tinc(logger_key_lat, lat);
530 }
531
532 // adjust safe_pos
533 auto it = pending_safe.find(start);
534 assert(it != pending_safe.end());
535 pending_safe.erase(it);
536 if (pending_safe.empty())
537 safe_pos = next_safe_pos;
538 else
539 safe_pos = pending_safe.begin()->second;
540
541 ldout(cct, 10) << "_finish_flush safe from " << start
542 << ", pending_safe " << pending_safe
543 << ", (prezeroing/prezero)/write/flush/safe positions now "
544 << "(" << prezeroing_pos << "/" << prezero_pos << ")/"
545 << write_pos << "/" << flush_pos << "/" << safe_pos
546 << dendl;
547
548 // kick waiters <= safe_pos
549 if (!waitfor_safe.empty()) {
550 list<Context*> ls;
551 while (!waitfor_safe.empty()) {
552 auto it = waitfor_safe.begin();
553 if (it->first > safe_pos)
554 break;
555 ls.splice(ls.end(), it->second);
556 waitfor_safe.erase(it);
557 }
558 finish_contexts(cct, ls);
559 }
560 }
561
562
563
564 uint64_t Journaler::append_entry(bufferlist& bl)
565 {
566 unique_lock l(lock);
567
568 assert(!readonly);
569 uint32_t s = bl.length();
570
571 // append
572 size_t delta = bl.length() + journal_stream.get_envelope_size();
573 // write_buf space is nearly full
574 if (!write_buf_throttle.get_or_fail(delta)) {
575 l.unlock();
576 ldout(cct, 10) << "write_buf_throttle wait, delta " << delta << dendl;
577 write_buf_throttle.get(delta);
578 l.lock();
579 }
580 ldout(cct, 20) << "write_buf_throttle get, delta " << delta << dendl;
581 size_t wrote = journal_stream.write(bl, &write_buf, write_pos);
582 ldout(cct, 10) << "append_entry len " << s << " to " << write_pos << "~"
583 << wrote << dendl;
584 write_pos += wrote;
585
586 // flush previous object?
587 uint64_t su = get_layout_period();
588 assert(su > 0);
589 uint64_t write_off = write_pos % su;
590 uint64_t write_obj = write_pos / su;
591 uint64_t flush_obj = flush_pos / su;
592 if (write_obj != flush_obj) {
593 ldout(cct, 10) << " flushing completed object(s) (su " << su << " wro "
594 << write_obj << " flo " << flush_obj << ")" << dendl;
595 _do_flush(write_buf.length() - write_off);
596
597 // if _do_flush() skips flushing some data, it does do a best effort to
598 // update next_safe_pos.
599 if (write_buf.length() > 0 &&
600 write_buf.length() <= wrote) { // the unflushed data are within this entry
601 // set next_safe_pos to end of previous entry
602 next_safe_pos = write_pos - wrote;
603 }
604 }
605
606 return write_pos;
607 }
608
609
610 void Journaler::_do_flush(unsigned amount)
611 {
612 if (is_stopping())
613 return;
614 if (write_pos == flush_pos)
615 return;
616 assert(write_pos > flush_pos);
617 assert(!readonly);
618
619 // flush
620 uint64_t len = write_pos - flush_pos;
621 assert(len == write_buf.length());
622 if (amount && amount < len)
623 len = amount;
624
625 // zero at least two full periods ahead. this ensures
626 // that the next object will not exist.
627 uint64_t period = get_layout_period();
628 if (flush_pos + len + 2*period > prezero_pos) {
629 _issue_prezero();
630
631 int64_t newlen = prezero_pos - flush_pos - period;
632 if (newlen <= 0) {
633 ldout(cct, 10) << "_do_flush wanted to do " << flush_pos << "~" << len
634 << " already too close to prezero_pos " << prezero_pos
635 << ", zeroing first" << dendl;
636 waiting_for_zero = true;
637 return;
638 }
639 if (static_cast<uint64_t>(newlen) < len) {
640 ldout(cct, 10) << "_do_flush wanted to do " << flush_pos << "~" << len
641 << " but hit prezero_pos " << prezero_pos
642 << ", will do " << flush_pos << "~" << newlen << dendl;
643 len = newlen;
644 } else {
645 waiting_for_zero = false;
646 }
647 } else {
648 waiting_for_zero = false;
649 }
650 ldout(cct, 10) << "_do_flush flushing " << flush_pos << "~" << len << dendl;
651
652 // submit write for anything pending
653 // flush _start_ pos to _finish_flush
654 ceph::real_time now = ceph::real_clock::now();
655 SnapContext snapc;
656
657 Context *onsafe = new C_Flush(this, flush_pos, now); // on COMMIT
658 pending_safe[flush_pos] = next_safe_pos;
659
660 bufferlist write_bl;
661
662 // adjust pointers
663 if (len == write_buf.length()) {
664 write_bl.swap(write_buf);
665 next_safe_pos = write_pos;
666 } else {
667 write_buf.splice(0, len, &write_bl);
668 // Keys of waitfor_safe map are journal entry boundaries.
669 // Try finding a journal entry that we are actually flushing
670 // and set next_safe_pos to end of it. This is best effort.
671 // The one we found may not be the lastest flushing entry.
672 auto p = waitfor_safe.lower_bound(flush_pos + len);
673 if (p != waitfor_safe.end()) {
674 if (p->first > flush_pos + len && p != waitfor_safe.begin())
675 --p;
676 if (p->first <= flush_pos + len && p->first > next_safe_pos)
677 next_safe_pos = p->first;
678 }
679 }
680
681 filer.write(ino, &layout, snapc,
682 flush_pos, len, write_bl, ceph::real_clock::now(),
683 0,
684 wrap_finisher(onsafe), write_iohint);
685
686 flush_pos += len;
687 assert(write_buf.length() == write_pos - flush_pos);
688 write_buf_throttle.put(len);
689 ldout(cct, 20) << "write_buf_throttle put, len " << len << dendl;
690
691 ldout(cct, 10)
692 << "_do_flush (prezeroing/prezero)/write/flush/safe pointers now at "
693 << "(" << prezeroing_pos << "/" << prezero_pos << ")/" << write_pos
694 << "/" << flush_pos << "/" << safe_pos << dendl;
695
696 _issue_prezero();
697 }
698
699
700 void Journaler::wait_for_flush(Context *onsafe)
701 {
702 lock_guard l(lock);
703 if (is_stopping()) {
704 onsafe->complete(-EAGAIN);
705 return;
706 }
707 _wait_for_flush(onsafe);
708 }
709
710 void Journaler::_wait_for_flush(Context *onsafe)
711 {
712 assert(!readonly);
713
714 // all flushed and safe?
715 if (write_pos == safe_pos) {
716 assert(write_buf.length() == 0);
717 ldout(cct, 10)
718 << "flush nothing to flush, (prezeroing/prezero)/write/flush/safe "
719 "pointers at " << "(" << prezeroing_pos << "/" << prezero_pos << ")/"
720 << write_pos << "/" << flush_pos << "/" << safe_pos << dendl;
721 if (onsafe) {
722 finisher->queue(onsafe, 0);
723 }
724 return;
725 }
726
727 // queue waiter
728 if (onsafe) {
729 waitfor_safe[write_pos].push_back(wrap_finisher(onsafe));
730 }
731 }
732
733 void Journaler::flush(Context *onsafe)
734 {
735 lock_guard l(lock);
736 if (is_stopping()) {
737 onsafe->complete(-EAGAIN);
738 return;
739 }
740 _flush(wrap_finisher(onsafe));
741 }
742
743 void Journaler::_flush(C_OnFinisher *onsafe)
744 {
745 assert(!readonly);
746
747 if (write_pos == flush_pos) {
748 assert(write_buf.length() == 0);
749 ldout(cct, 10) << "flush nothing to flush, (prezeroing/prezero)/write/"
750 "flush/safe pointers at " << "(" << prezeroing_pos << "/" << prezero_pos
751 << ")/" << write_pos << "/" << flush_pos << "/" << safe_pos
752 << dendl;
753 if (onsafe) {
754 onsafe->complete(0);
755 }
756 } else {
757 _do_flush();
758 _wait_for_flush(onsafe);
759 }
760
761 // write head?
762 if (_write_head_needed()) {
763 _write_head();
764 }
765 }
766
767 bool Journaler::_write_head_needed()
768 {
769 return last_wrote_head + seconds(cct->_conf->journaler_write_head_interval)
770 < ceph::real_clock::now();
771 }
772
773
774 /*************** prezeroing ******************/
775
776 struct C_Journaler_Prezero : public Context {
777 Journaler *journaler;
778 uint64_t from, len;
779 C_Journaler_Prezero(Journaler *j, uint64_t f, uint64_t l)
780 : journaler(j), from(f), len(l) {}
781 void finish(int r) override {
782 journaler->_finish_prezero(r, from, len);
783 }
784 };
785
786 void Journaler::_issue_prezero()
787 {
788 assert(prezeroing_pos >= flush_pos);
789
790 // we need to zero at least two periods, minimum, to ensure that we
791 // have a full empty object/period in front of us.
792 uint64_t num_periods = MAX(2, cct->_conf->journaler_prezero_periods);
793
794 /*
795 * issue zero requests based on write_pos, even though the invariant
796 * is that we zero ahead of flush_pos.
797 */
798 uint64_t period = get_layout_period();
799 uint64_t to = write_pos + period * num_periods + period - 1;
800 to -= to % period;
801
802 if (prezeroing_pos >= to) {
803 ldout(cct, 20) << "_issue_prezero target " << to << " <= prezeroing_pos "
804 << prezeroing_pos << dendl;
805 return;
806 }
807
808 while (prezeroing_pos < to) {
809 uint64_t len;
810 if (prezeroing_pos % period == 0) {
811 len = period;
812 ldout(cct, 10) << "_issue_prezero removing " << prezeroing_pos << "~"
813 << period << " (full period)" << dendl;
814 } else {
815 len = period - (prezeroing_pos % period);
816 ldout(cct, 10) << "_issue_prezero zeroing " << prezeroing_pos << "~"
817 << len << " (partial period)" << dendl;
818 }
819 SnapContext snapc;
820 Context *c = wrap_finisher(new C_Journaler_Prezero(this, prezeroing_pos,
821 len));
822 filer.zero(ino, &layout, snapc, prezeroing_pos, len,
823 ceph::real_clock::now(), 0, c);
824 prezeroing_pos += len;
825 }
826 }
827
828 // Lock cycle because we get called out of objecter callback (holding
829 // objecter read lock), but there are also cases where we take the journaler
830 // lock before calling into objecter to do I/O.
831 void Journaler::_finish_prezero(int r, uint64_t start, uint64_t len)
832 {
833 lock_guard l(lock);
834
835 ldout(cct, 10) << "_prezeroed to " << start << "~" << len
836 << ", prezeroing/prezero was " << prezeroing_pos << "/"
837 << prezero_pos << ", pending " << pending_zero
838 << dendl;
839 if (r < 0 && r != -ENOENT) {
840 lderr(cct) << "_prezeroed got " << cpp_strerror(r) << dendl;
841 handle_write_error(r);
842 return;
843 }
844
845 assert(r == 0 || r == -ENOENT);
846
847 if (start == prezero_pos) {
848 prezero_pos += len;
849 while (!pending_zero.empty() &&
850 pending_zero.begin().get_start() == prezero_pos) {
851 interval_set<uint64_t>::iterator b(pending_zero.begin());
852 prezero_pos += b.get_len();
853 pending_zero.erase(b);
854 }
855
856 if (waiting_for_zero) {
857 _do_flush();
858 }
859 } else {
860 pending_zero.insert(start, len);
861 }
862 ldout(cct, 10) << "_prezeroed prezeroing/prezero now " << prezeroing_pos
863 << "/" << prezero_pos
864 << ", pending " << pending_zero
865 << dendl;
866 }
867
868
869
870 /***************** READING *******************/
871
872
873 class Journaler::C_Read : public Context {
874 Journaler *ls;
875 uint64_t offset;
876 uint64_t length;
877 public:
878 bufferlist bl;
879 C_Read(Journaler *j, uint64_t o, uint64_t l) : ls(j), offset(o), length(l) {}
880 void finish(int r) override {
881 ls->_finish_read(r, offset, length, bl);
882 }
883 };
884
885 class Journaler::C_RetryRead : public Context {
886 Journaler *ls;
887 public:
888 explicit C_RetryRead(Journaler *l) : ls(l) {}
889
890 void finish(int r) override {
891 // Should only be called from waitfor_safe i.e. already inside lock
892 // (ls->lock is locked
893 ls->_prefetch();
894 }
895 };
896
897 void Journaler::_finish_read(int r, uint64_t offset, uint64_t length,
898 bufferlist& bl)
899 {
900 lock_guard l(lock);
901
902 if (r < 0) {
903 ldout(cct, 0) << "_finish_read got error " << r << dendl;
904 error = r;
905 } else {
906 ldout(cct, 10) << "_finish_read got " << offset << "~" << bl.length()
907 << dendl;
908 if (bl.length() < length) {
909 ldout(cct, 0) << "_finish_read got less than expected (" << length << ")"
910 << dendl;
911 error = -EINVAL;
912 }
913 }
914
915 if (error) {
916 if (on_readable) {
917 C_OnFinisher *f = on_readable;
918 on_readable = 0;
919 f->complete(error);
920 }
921 return;
922 }
923
924 prefetch_buf[offset].swap(bl);
925
926 try {
927 _assimilate_prefetch();
928 } catch (const buffer::error &err) {
929 lderr(cct) << "_decode error from assimilate_prefetch" << dendl;
930 error = -EINVAL;
931 if (on_readable) {
932 C_OnFinisher *f = on_readable;
933 on_readable = 0;
934 f->complete(error);
935 }
936 return;
937 }
938 _prefetch();
939 }
940
941 void Journaler::_assimilate_prefetch()
942 {
943 bool was_readable = readable;
944
945 bool got_any = false;
946 while (!prefetch_buf.empty()) {
947 map<uint64_t,bufferlist>::iterator p = prefetch_buf.begin();
948 if (p->first != received_pos) {
949 uint64_t gap = p->first - received_pos;
950 ldout(cct, 10) << "_assimilate_prefetch gap of " << gap
951 << " from received_pos " << received_pos
952 << " to first prefetched buffer " << p->first << dendl;
953 break;
954 }
955
956 ldout(cct, 10) << "_assimilate_prefetch " << p->first << "~"
957 << p->second.length() << dendl;
958 received_pos += p->second.length();
959 read_buf.claim_append(p->second);
960 assert(received_pos <= requested_pos);
961 prefetch_buf.erase(p);
962 got_any = true;
963 }
964
965 if (got_any) {
966 ldout(cct, 10) << "_assimilate_prefetch read_buf now " << read_pos << "~"
967 << read_buf.length() << ", read pointers " << read_pos
968 << "/" << received_pos << "/" << requested_pos
969 << dendl;
970
971 // Update readability (this will also hit any decode errors resulting
972 // from bad data)
973 readable = _is_readable();
974 }
975
976 if ((got_any && !was_readable && readable) || read_pos == write_pos) {
977 // readable!
978 ldout(cct, 10) << "_finish_read now readable (or at journal end) readable="
979 << readable << " read_pos=" << read_pos << " write_pos="
980 << write_pos << dendl;
981 if (on_readable) {
982 C_OnFinisher *f = on_readable;
983 on_readable = 0;
984 f->complete(0);
985 }
986 }
987 }
988
989 void Journaler::_issue_read(uint64_t len)
990 {
991 // stuck at safe_pos? (this is needed if we are reading the tail of
992 // a journal we are also writing to)
993 assert(requested_pos <= safe_pos);
994 if (requested_pos == safe_pos) {
995 ldout(cct, 10) << "_issue_read requested_pos = safe_pos = " << safe_pos
996 << ", waiting" << dendl;
997 assert(write_pos > requested_pos);
998 if (pending_safe.empty()) {
999 _flush(NULL);
1000 }
1001
1002 // Make sure keys of waitfor_safe map are journal entry boundaries.
1003 // The key we used here is either next_safe_pos or old value of
1004 // next_safe_pos. next_safe_pos is always set to journal entry
1005 // boundary.
1006 auto p = pending_safe.rbegin();
1007 if (p != pending_safe.rend())
1008 waitfor_safe[p->second].push_back(new C_RetryRead(this));
1009 else
1010 waitfor_safe[next_safe_pos].push_back(new C_RetryRead(this));
1011 return;
1012 }
1013
1014 // don't read too much
1015 if (requested_pos + len > safe_pos) {
1016 len = safe_pos - requested_pos;
1017 ldout(cct, 10) << "_issue_read reading only up to safe_pos " << safe_pos
1018 << dendl;
1019 }
1020
1021 // go.
1022 ldout(cct, 10) << "_issue_read reading " << requested_pos << "~" << len
1023 << ", read pointers " << read_pos << "/" << received_pos
1024 << "/" << (requested_pos+len) << dendl;
1025
1026 // step by period (object). _don't_ do a single big filer.read()
1027 // here because it will wait for all object reads to complete before
1028 // giving us back any data. this way we can process whatever bits
1029 // come in that are contiguous.
1030 uint64_t period = get_layout_period();
1031 while (len > 0) {
1032 uint64_t e = requested_pos + period;
1033 e -= e % period;
1034 uint64_t l = e - requested_pos;
1035 if (l > len)
1036 l = len;
1037 C_Read *c = new C_Read(this, requested_pos, l);
1038 filer.read(ino, &layout, CEPH_NOSNAP, requested_pos, l, &c->bl, 0,
1039 wrap_finisher(c), CEPH_OSD_OP_FLAG_FADVISE_DONTNEED);
1040 requested_pos += l;
1041 len -= l;
1042 }
1043 }
1044
1045 void Journaler::_prefetch()
1046 {
1047 if (is_stopping())
1048 return;
1049
1050 ldout(cct, 10) << "_prefetch" << dendl;
1051 // prefetch
1052 uint64_t pf;
1053 if (temp_fetch_len) {
1054 ldout(cct, 10) << "_prefetch temp_fetch_len " << temp_fetch_len << dendl;
1055 pf = temp_fetch_len;
1056 temp_fetch_len = 0;
1057 } else {
1058 pf = fetch_len;
1059 }
1060
1061 uint64_t raw_target = read_pos + pf;
1062
1063 // read full log segments, so increase if necessary
1064 uint64_t period = get_layout_period();
1065 uint64_t remainder = raw_target % period;
1066 uint64_t adjustment = remainder ? period - remainder : 0;
1067 uint64_t target = raw_target + adjustment;
1068
1069 // don't read past the log tail
1070 if (target > write_pos)
1071 target = write_pos;
1072
1073 if (requested_pos < target) {
1074 uint64_t len = target - requested_pos;
1075 ldout(cct, 10) << "_prefetch " << pf << " requested_pos " << requested_pos
1076 << " < target " << target << " (" << raw_target
1077 << "), prefetching " << len << dendl;
1078
1079 if (pending_safe.empty() && write_pos > safe_pos) {
1080 // If we are reading and writing the journal, then we may need
1081 // to issue a flush if one isn't already in progress.
1082 // Avoid doing a flush every time so that if we do write/read/write/read
1083 // we don't end up flushing after every write.
1084 ldout(cct, 10) << "_prefetch: requested_pos=" << requested_pos
1085 << ", read_pos=" << read_pos
1086 << ", write_pos=" << write_pos
1087 << ", safe_pos=" << safe_pos << dendl;
1088 _do_flush();
1089 }
1090
1091 _issue_read(len);
1092 }
1093 }
1094
1095
1096 /*
1097 * _is_readable() - return true if next entry is ready.
1098 */
1099 bool Journaler::_is_readable()
1100 {
1101 // anything to read?
1102 if (read_pos == write_pos)
1103 return false;
1104
1105 // Check if the retrieve bytestream has enough for an entry
1106 uint64_t need;
1107 if (journal_stream.readable(read_buf, &need)) {
1108 return true;
1109 }
1110
1111 ldout (cct, 10) << "_is_readable read_buf.length() == " << read_buf.length()
1112 << ", but need " << need << " for next entry; fetch_len is "
1113 << fetch_len << dendl;
1114
1115 // partial fragment at the end?
1116 if (received_pos == write_pos) {
1117 ldout(cct, 10) << "is_readable() detected partial entry at tail, "
1118 "adjusting write_pos to " << read_pos << dendl;
1119
1120 // adjust write_pos
1121 prezeroing_pos = prezero_pos = write_pos = flush_pos = safe_pos = next_safe_pos = read_pos;
1122 assert(write_buf.length() == 0);
1123 assert(waitfor_safe.empty());
1124
1125 // reset read state
1126 requested_pos = received_pos = read_pos;
1127 read_buf.clear();
1128
1129 // FIXME: truncate on disk?
1130
1131 return false;
1132 }
1133
1134 if (need > fetch_len) {
1135 temp_fetch_len = need;
1136 ldout(cct, 10) << "_is_readable noting temp_fetch_len " << temp_fetch_len
1137 << dendl;
1138 }
1139
1140 ldout(cct, 10) << "_is_readable: not readable, returning false" << dendl;
1141 return false;
1142 }
1143
1144 /*
1145 * is_readable() - kickstart prefetch, too
1146 */
1147 bool Journaler::is_readable()
1148 {
1149 lock_guard l(lock);
1150
1151 if (error != 0) {
1152 return false;
1153 }
1154
1155 bool r = readable;
1156 _prefetch();
1157 return r;
1158 }
1159
1160 class Journaler::C_EraseFinish : public Context {
1161 Journaler *journaler;
1162 C_OnFinisher *completion;
1163 public:
1164 C_EraseFinish(Journaler *j, C_OnFinisher *c) : journaler(j), completion(c) {}
1165 void finish(int r) override {
1166 journaler->_finish_erase(r, completion);
1167 }
1168 };
1169
1170 /**
1171 * Entirely erase the journal, including header. For use when you
1172 * have already made a copy of the journal somewhere else.
1173 */
1174 void Journaler::erase(Context *completion)
1175 {
1176 lock_guard l(lock);
1177
1178 // Async delete the journal data
1179 uint64_t first = trimmed_pos / get_layout_period();
1180 uint64_t num = (write_pos - trimmed_pos) / get_layout_period() + 2;
1181 filer.purge_range(ino, &layout, SnapContext(), first, num,
1182 ceph::real_clock::now(), 0,
1183 wrap_finisher(new C_EraseFinish(
1184 this, wrap_finisher(completion))));
1185
1186 // We will not start the operation to delete the header until
1187 // _finish_erase has seen the data deletion succeed: otherwise if
1188 // there was an error deleting data we might prematurely delete the
1189 // header thereby lose our reference to the data.
1190 }
1191
1192 void Journaler::_finish_erase(int data_result, C_OnFinisher *completion)
1193 {
1194 lock_guard l(lock);
1195 if (is_stopping()) {
1196 completion->complete(-EAGAIN);
1197 return;
1198 }
1199
1200 if (data_result == 0) {
1201 // Async delete the journal header
1202 filer.purge_range(ino, &layout, SnapContext(), 0, 1,
1203 ceph::real_clock::now(),
1204 0, wrap_finisher(completion));
1205 } else {
1206 lderr(cct) << "Failed to delete journal " << ino << " data: "
1207 << cpp_strerror(data_result) << dendl;
1208 completion->complete(data_result);
1209 }
1210 }
1211
1212 /* try_read_entry(bl)
1213 * read entry into bl if it's ready.
1214 * otherwise, do nothing.
1215 */
1216 bool Journaler::try_read_entry(bufferlist& bl)
1217 {
1218 lock_guard l(lock);
1219
1220 if (!readable) {
1221 ldout(cct, 10) << "try_read_entry at " << read_pos << " not readable"
1222 << dendl;
1223 return false;
1224 }
1225
1226 uint64_t start_ptr;
1227 size_t consumed;
1228 try {
1229 consumed = journal_stream.read(read_buf, &bl, &start_ptr);
1230 if (stream_format >= JOURNAL_FORMAT_RESILIENT) {
1231 assert(start_ptr == read_pos);
1232 }
1233 } catch (const buffer::error &e) {
1234 lderr(cct) << __func__ << ": decode error from journal_stream" << dendl;
1235 error = -EINVAL;
1236 return false;
1237 }
1238
1239 ldout(cct, 10) << "try_read_entry at " << read_pos << " read "
1240 << read_pos << "~" << consumed << " (have "
1241 << read_buf.length() << ")" << dendl;
1242
1243 read_pos += consumed;
1244 try {
1245 // We were readable, we might not be any more
1246 readable = _is_readable();
1247 } catch (const buffer::error &e) {
1248 lderr(cct) << __func__ << ": decode error from _is_readable" << dendl;
1249 error = -EINVAL;
1250 return false;
1251 }
1252
1253 // prefetch?
1254 _prefetch();
1255 return true;
1256 }
1257
1258 void Journaler::wait_for_readable(Context *onreadable)
1259 {
1260 lock_guard l(lock);
1261 if (is_stopping()) {
1262 finisher->queue(onreadable, -EAGAIN);
1263 return;
1264 }
1265
1266 assert(on_readable == 0);
1267 if (!readable) {
1268 ldout(cct, 10) << "wait_for_readable at " << read_pos << " onreadable "
1269 << onreadable << dendl;
1270 on_readable = wrap_finisher(onreadable);
1271 } else {
1272 // race with OSD reply
1273 finisher->queue(onreadable, 0);
1274 }
1275 }
1276
1277 bool Journaler::have_waiter() const
1278 {
1279 return on_readable != nullptr;
1280 }
1281
1282
1283
1284
1285 /***************** TRIMMING *******************/
1286
1287
1288 class Journaler::C_Trim : public Context {
1289 Journaler *ls;
1290 uint64_t to;
1291 public:
1292 C_Trim(Journaler *l, int64_t t) : ls(l), to(t) {}
1293 void finish(int r) override {
1294 ls->_finish_trim(r, to);
1295 }
1296 };
1297
1298 void Journaler::trim()
1299 {
1300 lock_guard l(lock);
1301 _trim();
1302 }
1303
1304 void Journaler::_trim()
1305 {
1306 if (is_stopping())
1307 return;
1308
1309 assert(!readonly);
1310 uint64_t period = get_layout_period();
1311 uint64_t trim_to = last_committed.expire_pos;
1312 trim_to -= trim_to % period;
1313 ldout(cct, 10) << "trim last_commited head was " << last_committed
1314 << ", can trim to " << trim_to
1315 << dendl;
1316 if (trim_to == 0 || trim_to == trimming_pos) {
1317 ldout(cct, 10) << "trim already trimmed/trimming to "
1318 << trimmed_pos << "/" << trimming_pos << dendl;
1319 return;
1320 }
1321
1322 if (trimming_pos > trimmed_pos) {
1323 ldout(cct, 10) << "trim already trimming atm, try again later. "
1324 "trimmed/trimming is " << trimmed_pos << "/" << trimming_pos << dendl;
1325 return;
1326 }
1327
1328 // trim
1329 assert(trim_to <= write_pos);
1330 assert(trim_to <= expire_pos);
1331 assert(trim_to > trimming_pos);
1332 ldout(cct, 10) << "trim trimming to " << trim_to
1333 << ", trimmed/trimming/expire are "
1334 << trimmed_pos << "/" << trimming_pos << "/" << expire_pos
1335 << dendl;
1336
1337 // delete range of objects
1338 uint64_t first = trimming_pos / period;
1339 uint64_t num = (trim_to - trimming_pos) / period;
1340 SnapContext snapc;
1341 filer.purge_range(ino, &layout, snapc, first, num,
1342 ceph::real_clock::now(), 0,
1343 wrap_finisher(new C_Trim(this, trim_to)));
1344 trimming_pos = trim_to;
1345 }
1346
1347 void Journaler::_finish_trim(int r, uint64_t to)
1348 {
1349 lock_guard l(lock);
1350
1351 assert(!readonly);
1352 ldout(cct, 10) << "_finish_trim trimmed_pos was " << trimmed_pos
1353 << ", trimmed/trimming/expire now "
1354 << to << "/" << trimming_pos << "/" << expire_pos
1355 << dendl;
1356 if (r < 0 && r != -ENOENT) {
1357 lderr(cct) << "_finish_trim got " << cpp_strerror(r) << dendl;
1358 handle_write_error(r);
1359 return;
1360 }
1361
1362 assert(r >= 0 || r == -ENOENT);
1363
1364 assert(to <= trimming_pos);
1365 assert(to > trimmed_pos);
1366 trimmed_pos = to;
1367 }
1368
1369 void Journaler::handle_write_error(int r)
1370 {
1371 // lock is locked
1372
1373 lderr(cct) << "handle_write_error " << cpp_strerror(r) << dendl;
1374 if (on_write_error) {
1375 on_write_error->complete(r);
1376 on_write_error = NULL;
1377 called_write_error = true;
1378 } else if (called_write_error) {
1379 /* We don't call error handler more than once, subsequent errors
1380 * are dropped -- this is okay as long as the error handler does
1381 * something dramatic like respawn */
1382 lderr(cct) << __func__ << ": multiple write errors, handler already called"
1383 << dendl;
1384 } else {
1385 assert(0 == "unhandled write error");
1386 }
1387 }
1388
1389
1390 /**
1391 * Test whether the 'read_buf' byte stream has enough data to read
1392 * an entry
1393 *
1394 * sets 'next_envelope_size' to the number of bytes needed to advance (enough
1395 * to get the next header if header was unavailable, or enough to get the whole
1396 * next entry if the header was available but the body wasn't).
1397 */
1398 bool JournalStream::readable(bufferlist &read_buf, uint64_t *need) const
1399 {
1400 assert(need != NULL);
1401
1402 uint32_t entry_size = 0;
1403 uint64_t entry_sentinel = 0;
1404 bufferlist::iterator p = read_buf.begin();
1405
1406 // Do we have enough data to decode an entry prefix?
1407 if (format >= JOURNAL_FORMAT_RESILIENT) {
1408 *need = sizeof(entry_size) + sizeof(entry_sentinel);
1409 } else {
1410 *need = sizeof(entry_size);
1411 }
1412 if (read_buf.length() >= *need) {
1413 if (format >= JOURNAL_FORMAT_RESILIENT) {
1414 ::decode(entry_sentinel, p);
1415 if (entry_sentinel != sentinel) {
1416 throw buffer::malformed_input("Invalid sentinel");
1417 }
1418 }
1419
1420 ::decode(entry_size, p);
1421 } else {
1422 return false;
1423 }
1424
1425 // Do we have enough data to decode an entry prefix, payload and suffix?
1426 if (format >= JOURNAL_FORMAT_RESILIENT) {
1427 *need = JOURNAL_ENVELOPE_RESILIENT + entry_size;
1428 } else {
1429 *need = JOURNAL_ENVELOPE_LEGACY + entry_size;
1430 }
1431 if (read_buf.length() >= *need) {
1432 return true; // No more bytes needed
1433 }
1434
1435 return false;
1436 }
1437
1438
1439 /**
1440 * Consume one entry from a journal byte stream 'from', splicing a
1441 * serialized LogEvent blob into 'entry'.
1442 *
1443 * 'entry' must be non null and point to an empty bufferlist.
1444 *
1445 * 'from' must contain sufficient valid data (i.e. readable is true).
1446 *
1447 * 'start_ptr' will be set to the entry's start pointer, if the collection
1448 * format provides it. It may not be null.
1449 *
1450 * @returns The number of bytes consumed from the `from` byte stream. Note
1451 * that this is not equal to the length of `entry`, which contains
1452 * the inner serialized LogEvent and not the envelope.
1453 */
1454 size_t JournalStream::read(bufferlist &from, bufferlist *entry,
1455 uint64_t *start_ptr)
1456 {
1457 assert(start_ptr != NULL);
1458 assert(entry != NULL);
1459 assert(entry->length() == 0);
1460
1461 uint32_t entry_size = 0;
1462
1463 // Consume envelope prefix: entry_size and entry_sentinel
1464 bufferlist::iterator from_ptr = from.begin();
1465 if (format >= JOURNAL_FORMAT_RESILIENT) {
1466 uint64_t entry_sentinel = 0;
1467 ::decode(entry_sentinel, from_ptr);
1468 // Assertion instead of clean check because of precondition of this
1469 // fn is that readable() already passed
1470 assert(entry_sentinel == sentinel);
1471 }
1472 ::decode(entry_size, from_ptr);
1473
1474 // Read out the payload
1475 from_ptr.copy(entry_size, *entry);
1476
1477 // Consume the envelope suffix (start_ptr)
1478 if (format >= JOURNAL_FORMAT_RESILIENT) {
1479 ::decode(*start_ptr, from_ptr);
1480 } else {
1481 *start_ptr = 0;
1482 }
1483
1484 // Trim the input buffer to discard the bytes we have consumed
1485 from.splice(0, from_ptr.get_off());
1486
1487 return from_ptr.get_off();
1488 }
1489
1490
1491 /**
1492 * Append one entry
1493 */
1494 size_t JournalStream::write(bufferlist &entry, bufferlist *to,
1495 uint64_t const &start_ptr)
1496 {
1497 assert(to != NULL);
1498
1499 uint32_t const entry_size = entry.length();
1500 if (format >= JOURNAL_FORMAT_RESILIENT) {
1501 ::encode(sentinel, *to);
1502 }
1503 ::encode(entry_size, *to);
1504 to->claim_append(entry);
1505 if (format >= JOURNAL_FORMAT_RESILIENT) {
1506 ::encode(start_ptr, *to);
1507 }
1508
1509 if (format >= JOURNAL_FORMAT_RESILIENT) {
1510 return JOURNAL_ENVELOPE_RESILIENT + entry_size;
1511 } else {
1512 return JOURNAL_ENVELOPE_LEGACY + entry_size;
1513 }
1514 }
1515
1516 /**
1517 * set write error callback
1518 *
1519 * Set a callback/context to trigger if we get a write error from
1520 * the objecter. This may be from an explicit request (e.g., flush)
1521 * or something async the journaler did on its own (e.g., journal
1522 * header update).
1523 *
1524 * It is only used once; if the caller continues to use the
1525 * Journaler and wants to hear about errors, it needs to reset the
1526 * error_handler.
1527 *
1528 * @param c callback/context to trigger on error
1529 */
1530 void Journaler::set_write_error_handler(Context *c) {
1531 lock_guard l(lock);
1532 assert(!on_write_error);
1533 on_write_error = wrap_finisher(c);
1534 called_write_error = false;
1535 }
1536
1537
1538 /**
1539 * Wrap a context in a C_OnFinisher, if it is non-NULL
1540 *
1541 * Utility function to avoid lots of error-prone and verbose
1542 * NULL checking on contexts passed in.
1543 */
1544 C_OnFinisher *Journaler::wrap_finisher(Context *c)
1545 {
1546 if (c != NULL) {
1547 return new C_OnFinisher(c, finisher);
1548 } else {
1549 return NULL;
1550 }
1551 }
1552
1553 void Journaler::shutdown()
1554 {
1555 lock_guard l(lock);
1556
1557 ldout(cct, 1) << __func__ << dendl;
1558
1559 state = STATE_STOPPING;
1560 readable = false;
1561
1562 // Kick out anyone reading from journal
1563 error = -EAGAIN;
1564 if (on_readable) {
1565 C_OnFinisher *f = on_readable;
1566 on_readable = 0;
1567 f->complete(-EAGAIN);
1568 }
1569
1570 list<Context*> ls;
1571 ls.swap(waitfor_recover);
1572 finish_contexts(cct, ls, -ESHUTDOWN);
1573
1574 std::map<uint64_t, std::list<Context*> >::iterator i;
1575 for (i = waitfor_safe.begin(); i != waitfor_safe.end(); ++i) {
1576 finish_contexts(cct, i->second, -EAGAIN);
1577 }
1578 waitfor_safe.clear();
1579 }
1580