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) 2014 john spray <john.spray@inktank.com>
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.
17 #include "common/ceph_argparse.h"
18 #include "common/errno.h"
19 #include "osdc/Journaler.h"
20 #include "mds/mdstypes.h"
21 #include "mds/LogEvent.h"
22 #include "mds/InoTable.h"
24 #include "mds/events/ENoOp.h"
25 #include "mds/events/EUpdate.h"
27 #include "JournalScanner.h"
28 #include "EventOutput.h"
32 #include "JournalTool.h"
35 #define dout_context g_ceph_context
36 #define dout_subsys ceph_subsys_mds
38 #define dout_prefix *_dout << __func__ << ": "
42 void JournalTool::usage()
44 std::cout
<< "Usage: \n"
45 << " cephfs-journal-tool [options] journal <command>\n"
48 << " import <path> [--force]\n"
50 << " reset [--force]\n"
51 << " cephfs-journal-tool [options] header <get|set <field> <value>\n"
52 << " <field>: [trimmed_pos|expire_pos|write_pos|pool_id]"
53 << " cephfs-journal-tool [options] event <effect> <selector> <output> [special options]\n"
55 << " --range=<start>..<end>\n"
56 << " --path=<substring>\n"
57 << " --inode=<integer>\n"
58 << " --type=<UPDATE|OPEN|SESSION...><\n"
59 << " --frag=<ino>.<frag> [--dname=<dentry string>]\n"
60 << " --client=<session id integer>\n"
61 << " <effect>: [get|recover_dentries|splice]\n"
62 << " <output>: [summary|list|binary|json] [--path <path>]\n"
64 << "General options:\n"
65 << " --rank=filesystem:mds-rank Journal rank (required if multiple\n"
66 << " file systems, default is rank 0 on\n"
67 << " the only filesystem otherwise.\n"
69 << "Special options\n"
70 << " --alternate-pool <name> Alternative metadata pool to target\n"
71 << " when using recover_dentries.\n";
73 generic_client_usage();
78 * Handle arguments and hand off to journal/header/event mode
80 int JournalTool::main(std::vector
<const char*> &argv
)
84 dout(10) << "JournalTool::main " << dendl
;
92 std::vector
<const char*>::iterator arg
= argv
.begin();
95 if(!ceph_argparse_witharg(argv
, arg
, &rank_str
, "--rank", (char*)NULL
)) {
96 // Default: act on rank 0. Will give the user an error if they
97 // try invoking this way when they have more than one filesystem.
101 r
= role_selector
.parse(*fsmap
, rank_str
);
103 derr
<< "Couldn't determine MDS rank." << dendl
;
108 if (arg
== argv
.end()) {
109 derr
<< "Missing mode [journal|header|event]" << dendl
;
112 mode
= std::string(*arg
);
113 arg
= argv
.erase(arg
);
117 r
= rados
.init_with_context(g_ceph_context
);
119 derr
<< "RADOS unavailable, cannot scan filesystem journal" << dendl
;
123 dout(4) << "JournalTool: connecting to RADOS..." << dendl
;
126 derr
<< "couldn't connect to cluster: " << cpp_strerror(r
) << dendl
;
130 auto fs
= fsmap
->get_filesystem(role_selector
.get_ns());
131 assert(fs
!= nullptr);
132 int64_t const pool_id
= fs
->mds_map
.get_metadata_pool();
133 dout(4) << "JournalTool: resolving pool " << pool_id
<< dendl
;
134 std::string pool_name
;
135 r
= rados
.pool_reverse_lookup(pool_id
, &pool_name
);
137 derr
<< "Pool " << pool_id
<< " named in MDS map not found in RADOS!" << dendl
;
141 dout(4) << "JournalTool: creating IoCtx.." << dendl
;
142 r
= rados
.ioctx_create(pool_name
.c_str(), input
);
148 for (auto role
: role_selector
.get_roles()) {
150 dout(4) << "Executing for rank " << rank
<< dendl
;
151 if (mode
== std::string("journal")) {
152 r
= main_journal(argv
);
153 } else if (mode
== std::string("header")) {
154 r
= main_header(argv
);
155 } else if (mode
== std::string("event")) {
156 r
= main_event(argv
);
158 derr
<< "Bad command '" << mode
<< "'" << dendl
;
173 * Handle arguments for 'journal' mode
175 * This is for operations that act on the journal as a whole.
177 int JournalTool::main_journal(std::vector
<const char*> &argv
)
179 std::string command
= argv
[0];
180 if (command
== "inspect") {
181 return journal_inspect();
182 } else if (command
== "export" || command
== "import") {
184 if (argv
.size() >= 2) {
185 std::string
const path
= argv
[1];
186 if (argv
.size() == 3) {
187 if (std::string(argv
[2]) == "--force") {
190 std::cerr
<< "Unknown argument " << argv
[1] << std::endl
;
194 return journal_export(path
, command
== "import", force
);
196 derr
<< "Missing path" << dendl
;
199 } else if (command
== "reset") {
201 if (argv
.size() == 2) {
202 if (std::string(argv
[1]) == "--force") {
205 std::cerr
<< "Unknown argument " << argv
[1] << std::endl
;
209 } else if (argv
.size() > 2) {
210 std::cerr
<< "Too many arguments!" << std::endl
;
214 return journal_reset(force
);
216 derr
<< "Bad journal command '" << command
<< "'" << dendl
;
223 * Parse arguments and execute for 'header' mode
225 * This is for operations that act on the header only.
227 int JournalTool::main_header(std::vector
<const char*> &argv
)
229 JournalFilter filter
;
230 JournalScanner
js(input
, rank
, filter
);
231 int r
= js
.scan(false);
233 std::cerr
<< "Unable to scan journal" << std::endl
;
237 if (!js
.header_present
) {
238 std::cerr
<< "Header object not found!" << std::endl
;
240 } else if (!js
.header_valid
&& js
.header
== NULL
) {
241 // Can't do a read or a single-field write without a copy of the original
242 derr
<< "Header could not be read!" << dendl
;
245 assert(js
.header
!= NULL
);
248 if (argv
.size() == 0) {
249 derr
<< "Invalid header command, must be [get|set]" << dendl
;
252 std::vector
<const char *>::iterator arg
= argv
.begin();
253 std::string
const command
= *arg
;
254 arg
= argv
.erase(arg
);
256 if (command
== std::string("get")) {
257 // Write JSON journal dump to stdout
258 JSONFormatter
jf(true);
259 js
.header
->dump(&jf
);
261 std::cout
<< std::endl
;
262 } else if (command
== std::string("set")) {
263 // Need two more args <key> <val>
264 if (argv
.size() != 2) {
265 derr
<< "'set' requires two arguments <trimmed_pos|expire_pos|write_pos> <value>" << dendl
;
269 std::string
const field_name
= *arg
;
270 arg
= argv
.erase(arg
);
272 std::string
const value_str
= *arg
;
273 arg
= argv
.erase(arg
);
274 assert(argv
.empty());
276 std::string parse_err
;
277 uint64_t new_val
= strict_strtoll(value_str
.c_str(), 0, &parse_err
);
278 if (!parse_err
.empty()) {
279 derr
<< "Invalid value '" << value_str
<< "': " << parse_err
<< dendl
;
283 uint64_t *field
= NULL
;
284 if (field_name
== "trimmed_pos") {
285 field
= &(js
.header
->trimmed_pos
);
286 } else if (field_name
== "expire_pos") {
287 field
= &(js
.header
->expire_pos
);
288 } else if (field_name
== "write_pos") {
289 field
= &(js
.header
->write_pos
);
290 } else if (field_name
== "pool_id") {
291 field
= (uint64_t*)(&(js
.header
->layout
.pool_id
));
293 derr
<< "Invalid field '" << field_name
<< "'" << dendl
;
297 std::cout
<< "Updating " << field_name
<< std::hex
<< " 0x" << *field
<< " -> 0x" << new_val
<< std::dec
<< std::endl
;
300 dout(4) << "Writing object..." << dendl
;
301 bufferlist header_bl
;
302 ::encode(*(js
.header
), header_bl
);
303 output
.write_full(js
.obj_name(0), header_bl
);
304 dout(4) << "Write complete." << dendl
;
305 std::cout
<< "Successfully updated header." << std::endl
;
307 derr
<< "Bad header command '" << command
<< "'" << dendl
;
316 * Parse arguments and execute for 'event' mode
318 * This is for operations that act on LogEvents within the log
320 int JournalTool::main_event(std::vector
<const char*> &argv
)
324 std::vector
<const char*>::iterator arg
= argv
.begin();
326 std::string command
= *(arg
++);
327 if (command
!= "get" && command
!= "splice" && command
!= "recover_dentries") {
328 derr
<< "Unknown argument '" << command
<< "'" << dendl
;
333 if (arg
== argv
.end()) {
334 derr
<< "Incomplete command line" << dendl
;
339 // Parse filter options
340 // ====================
341 JournalFilter filter
;
342 r
= filter
.parse_args(argv
, arg
);
347 // Parse output options
348 // ====================
349 if (arg
== argv
.end()) {
350 derr
<< "Missing output command" << dendl
;
353 std::string output_style
= *(arg
++);
354 if (output_style
!= "binary" && output_style
!= "json" &&
355 output_style
!= "summary" && output_style
!= "list") {
356 derr
<< "Unknown argument: '" << output_style
<< "'" << dendl
;
361 std::string output_path
= "dump";
362 while(arg
!= argv
.end()) {
364 if (ceph_argparse_witharg(argv
, arg
, &arg_str
, "--path", (char*)NULL
)) {
365 output_path
= arg_str
;
366 } else if (ceph_argparse_witharg(argv
, arg
, &arg_str
, "--alternate-pool",
368 dout(1) << "Using alternate pool " << arg_str
<< dendl
;
369 int r
= rados
.ioctx_create(arg_str
.c_str(), output
);
373 derr
<< "Unknown argument: '" << *arg
<< "'" << dendl
;
381 JournalScanner
js(input
, rank
, filter
);
382 if (command
== "get") {
385 derr
<< "Failed to scan journal (" << cpp_strerror(r
) << ")" << dendl
;
388 } else if (command
== "recover_dentries") {
391 derr
<< "Failed to scan journal (" << cpp_strerror(r
) << ")" << dendl
;
395 bool dry_run
= false;
396 if (arg
!= argv
.end() && ceph_argparse_flag(argv
, arg
, "--dry_run", (char*)NULL
)) {
401 * Iterate over log entries, attempting to scavenge from each one
403 std::set
<inodeno_t
> consumed_inos
;
404 for (JournalScanner::EventMap::iterator i
= js
.events
.begin();
405 i
!= js
.events
.end(); ++i
) {
406 LogEvent
*le
= i
->second
.log_event
;
407 EMetaBlob
const *mb
= le
->get_metablob();
409 int scav_r
= recover_dentries(*mb
, dry_run
, &consumed_inos
);
411 dout(1) << "Error processing event 0x" << std::hex
<< i
->first
<< std::dec
412 << ": " << cpp_strerror(scav_r
) << ", continuing..." << dendl
;
416 // Our goal is to read all we can, so don't stop on errors, but
417 // do record them for possible later output
418 js
.errors
.insert(std::make_pair(i
->first
,
419 JournalScanner::EventError(scav_r
, cpp_strerror(r
))));
425 * Update InoTable to reflect any inode numbers consumed during scavenge
427 dout(4) << "consumed " << consumed_inos
.size() << " inodes" << dendl
;
428 if (consumed_inos
.size() && !dry_run
) {
429 int consume_r
= consume_inos(consumed_inos
);
431 dout(1) << "Error updating InoTable for " << consumed_inos
.size()
432 << " consume inos: " << cpp_strerror(consume_r
) << dendl
;
439 // Remove consumed dentries from lost+found.
440 if (other_pool
&& !dry_run
) {
441 std::set
<std::string
> found
;
443 for (auto i
: consumed_inos
) {
446 snprintf(s
, sizeof(s
), "%llx_head", (unsigned long long) i
);
447 dout(20) << "removing " << s
<< dendl
;
448 found
.insert(std::string(s
));
452 frag_oid
= InodeStore::get_object_name(CEPH_INO_LOST_AND_FOUND
,
454 output
.omap_rm_keys(frag_oid
.name
, found
);
456 } else if (command
== "splice") {
459 derr
<< "Failed to scan journal (" << cpp_strerror(r
) << ")" << dendl
;
464 if (filter
.get_range(start
, end
)) {
465 // Special case for range filter: erase a numeric range in the log
466 uint64_t range
= end
- start
;
467 int r
= erase_region(js
, start
, range
);
469 derr
<< "Failed to erase region 0x" << std::hex
<< start
<< "~0x" << range
<< std::dec
470 << ": " << cpp_strerror(r
) << dendl
;
474 // General case: erase a collection of individual entries in the log
475 for (JournalScanner::EventMap::iterator i
= js
.events
.begin(); i
!= js
.events
.end(); ++i
) {
476 dout(4) << "Erasing offset 0x" << std::hex
<< i
->first
<< std::dec
<< dendl
;
478 int r
= erase_region(js
, i
->first
, i
->second
.raw_size
);
480 derr
<< "Failed to erase event 0x" << std::hex
<< i
->first
<< std::dec
481 << ": " << cpp_strerror(r
) << dendl
;
489 derr
<< "Unknown argument '" << command
<< "'" << dendl
;
496 EventOutput
output(js
, output_path
);
497 int output_result
= 0;
498 if (output_style
== "binary") {
499 output_result
= output
.binary();
500 } else if (output_style
== "json") {
501 output_result
= output
.json();
502 } else if (output_style
== "summary") {
504 } else if (output_style
== "list") {
507 std::cerr
<< "Bad output command '" << output_style
<< "'" << std::endl
;
511 if (output_result
!= 0) {
512 std::cerr
<< "Error writing output: " << cpp_strerror(output_result
) << std::endl
;
515 return output_result
;
519 * Provide the user with information about the condition of the journal,
520 * especially indicating what range of log events is available and where
521 * any gaps or corruptions in the journal are.
523 int JournalTool::journal_inspect()
527 JournalFilter filter
;
528 JournalScanner
js(input
, rank
, filter
);
531 std::cerr
<< "Failed to scan journal (" << cpp_strerror(r
) << ")" << std::endl
;
535 js
.report(std::cout
);
542 * Attempt to export a binary dump of the journal.
544 * This is allowed to fail if the header is malformed or there are
545 * objects inaccessible, in which case the user would have to fall
546 * back to manually listing RADOS objects and extracting them, which
547 * they can do with the ``rados`` CLI.
549 int JournalTool::journal_export(std::string
const &path
, bool import
, bool force
)
552 JournalScanner
js(input
, rank
);
556 * If doing an export, first check that the header is valid and
557 * no objects are missing before trying to dump
561 derr
<< "Unable to scan journal, assuming badly damaged" << dendl
;
564 if (!js
.is_readable()) {
565 derr
<< "Journal not readable, attempt object-by-object dump with `rados`" << dendl
;
571 * Assuming we can cleanly read the journal data, dump it out to a file
575 r
= dumper
.init(mds_role_t(role_selector
.get_ns(), rank
));
577 derr
<< "dumper::init failed: " << cpp_strerror(r
) << dendl
;
581 r
= dumper
.undump(path
.c_str(), force
);
583 r
= dumper
.dump(path
.c_str());
592 * Truncate journal and insert EResetJournal
594 int JournalTool::journal_reset(bool hard
)
600 derr
<< "resetter::init failed: " << cpp_strerror(r
) << dendl
;
605 r
= resetter
.reset_hard(mds_role_t(role_selector
.get_ns(), rank
));
607 r
= resetter
.reset(mds_role_t(role_selector
.get_ns(), rank
));
615 * Selective offline replay which only reads out dentries and writes
616 * them to the backing store iff their version is > what is currently
617 * in the backing store.
619 * In order to write dentries to the backing store, we may create the
620 * required enclosing dirfrag objects.
622 * Test this by running scavenge on an unflushed journal, then nuking
623 * it offline, then starting an MDS and seeing that the dentries are
626 * @param metablob an EMetaBlob retrieved from the journal
627 * @param dry_run if true, do no writes to RADOS
628 * @param consumed_inos output, populated with any inos inserted
629 * @returns 0 on success, else negative error code
631 int JournalTool::recover_dentries(
632 EMetaBlob
const &metablob
,
634 std::set
<inodeno_t
> *consumed_inos
)
636 assert(consumed_inos
!= NULL
);
640 // Replay fullbits (dentry+inode)
641 for (list
<dirfrag_t
>::const_iterator lp
= metablob
.lump_order
.begin();
642 lp
!= metablob
.lump_order
.end(); ++lp
)
644 dirfrag_t
const &frag
= *lp
;
645 EMetaBlob::dirlump
const &lump
= metablob
.lump_map
.find(frag
)->second
;
647 object_t frag_oid
= InodeStore::get_object_name(frag
.ino
, frag
.frag
, "");
649 dout(4) << "inspecting lump " << frag_oid
.name
<< dendl
;
652 // We will record old fnode version for use in hard link handling
653 // If we don't read an old fnode, take version as zero and write in
654 // all hardlinks we find.
655 version_t old_fnode_version
= 0;
657 // Update fnode in omap header of dirfrag object
658 bool write_fnode
= false;
659 bufferlist old_fnode_bl
;
660 r
= input
.omap_get_header(frag_oid
.name
, &old_fnode_bl
);
662 // Creating dirfrag from scratch
663 dout(4) << "failed to read OMAP header from directory fragment "
664 << frag_oid
.name
<< " " << cpp_strerror(r
) << dendl
;
666 // Note: creating the dirfrag *without* a backtrace, relying on
667 // MDS to regenerate backtraces on read or in FSCK
669 // Conditionally update existing omap header
671 bufferlist::iterator old_fnode_iter
= old_fnode_bl
.begin();
673 old_fnode
.decode(old_fnode_iter
);
674 dout(4) << "frag " << frag_oid
.name
<< " fnode old v" <<
675 old_fnode
.version
<< " vs new v" << lump
.fnode
.version
<< dendl
;
676 old_fnode_version
= old_fnode
.version
;
677 write_fnode
= old_fnode_version
< lump
.fnode
.version
;
678 } catch (const buffer::error
&err
) {
679 dout(1) << "frag " << frag_oid
.name
680 << " is corrupt, overwriting" << dendl
;
685 dout(4) << "failed to read OMAP header from directory fragment "
686 << frag_oid
.name
<< " " << cpp_strerror(r
) << dendl
;
690 if ((other_pool
|| write_fnode
) && !dry_run
) {
691 dout(4) << "writing fnode to omap header" << dendl
;
693 lump
.fnode
.encode(fnode_bl
);
694 if (!other_pool
|| frag
.ino
>= MDS_INO_SYSTEM_BASE
) {
695 r
= output
.omap_set_header(frag_oid
.name
, fnode_bl
);
698 derr
<< "Failed to write fnode for frag object "
699 << frag_oid
.name
<< dendl
;
704 std::set
<std::string
> read_keys
;
706 // Compose list of potentially-existing dentries we would like to fetch
707 list
<ceph::shared_ptr
<EMetaBlob::fullbit
> > const &fb_list
=
709 for (list
<ceph::shared_ptr
<EMetaBlob::fullbit
> >::const_iterator fbi
=
710 fb_list
.begin(); fbi
!= fb_list
.end(); ++fbi
) {
711 EMetaBlob::fullbit
const &fb
= *(*fbi
);
713 // Get a key like "foobar_head"
715 dentry_key_t
dn_key(fb
.dnlast
, fb
.dn
.c_str());
717 read_keys
.insert(key
);
720 list
<EMetaBlob::remotebit
> const &rb_list
=
722 for (list
<EMetaBlob::remotebit
>::const_iterator rbi
=
723 rb_list
.begin(); rbi
!= rb_list
.end(); ++rbi
) {
724 EMetaBlob::remotebit
const &rb
= *rbi
;
726 // Get a key like "foobar_head"
728 dentry_key_t
dn_key(rb
.dnlast
, rb
.dn
.c_str());
730 read_keys
.insert(key
);
733 list
<EMetaBlob::nullbit
> const &nb_list
= lump
.get_dnull();
734 for (auto& nb
: nb_list
) {
735 // Get a key like "foobar_head"
737 dentry_key_t
dn_key(nb
.dnlast
, nb
.dn
.c_str());
739 read_keys
.insert(key
);
742 // Perform bulk read of existing dentries
743 std::map
<std::string
, bufferlist
> read_vals
;
744 r
= input
.omap_get_vals_by_keys(frag_oid
.name
, read_keys
, &read_vals
);
745 if (r
== -ENOENT
&& other_pool
) {
746 r
= output
.omap_get_vals_by_keys(frag_oid
.name
, read_keys
, &read_vals
);
749 derr
<< "unexpected error reading fragment object "
750 << frag_oid
.name
<< ": " << cpp_strerror(r
) << dendl
;
754 // Compose list of dentries we will write back
755 std::map
<std::string
, bufferlist
> write_vals
;
756 for (list
<ceph::shared_ptr
<EMetaBlob::fullbit
> >::const_iterator fbi
=
757 fb_list
.begin(); fbi
!= fb_list
.end(); ++fbi
) {
758 EMetaBlob::fullbit
const &fb
= *(*fbi
);
760 // Get a key like "foobar_head"
762 dentry_key_t
dn_key(fb
.dnlast
, fb
.dn
.c_str());
765 dout(4) << "inspecting fullbit " << frag_oid
.name
<< "/" << fb
.dn
767 bool write_dentry
= false;
768 if (read_vals
.find(key
) == read_vals
.end()) {
769 dout(4) << "dentry did not already exist, will create" << dendl
;
772 dout(4) << "dentry " << key
<< " existed already" << dendl
;
773 dout(4) << "dentry exists, checking versions..." << dendl
;
774 bufferlist
&old_dentry
= read_vals
[key
];
775 // Decode dentry+inode
776 bufferlist::iterator q
= old_dentry
.begin();
779 ::decode(dnfirst
, q
);
781 ::decode(dentry_type
, q
);
783 if (dentry_type
== 'L') {
784 // leave write_dentry false, we have no version to
785 // compare with in a hardlink, so it's not safe to
786 // squash over it with what's in this fullbit
787 dout(10) << "Existing remote inode in slot to be (maybe) written "
788 << "by a full inode from the journal dn '" << fb
.dn
.c_str()
789 << "' with lump fnode version " << lump
.fnode
.version
790 << "vs existing fnode version " << old_fnode_version
<< dendl
;
791 write_dentry
= old_fnode_version
< lump
.fnode
.version
;
792 } else if (dentry_type
== 'I') {
793 // Read out inode version to compare with backing store
795 inode
.decode_bare(q
);
796 dout(4) << "decoded embedded inode version "
797 << inode
.inode
.version
<< " vs fullbit version "
798 << fb
.inode
.version
<< dendl
;
799 if (inode
.inode
.version
< fb
.inode
.version
) {
803 dout(4) << "corrupt dentry in backing store, overwriting from "
809 if ((other_pool
|| write_dentry
) && !dry_run
) {
810 dout(4) << "writing I dentry " << key
<< " into frag "
811 << frag_oid
.name
<< dendl
;
813 // Compose: Dentry format is dnfirst, [I|L], InodeStore(bare=true)
814 bufferlist dentry_bl
;
815 ::encode(fb
.dnfirst
, dentry_bl
);
816 ::encode('I', dentry_bl
);
817 encode_fullbit_as_inode(fb
, true, &dentry_bl
);
819 // Record for writing to RADOS
820 write_vals
[key
] = dentry_bl
;
821 consumed_inos
->insert(fb
.inode
.ino
);
825 for (list
<EMetaBlob::remotebit
>::const_iterator rbi
=
826 rb_list
.begin(); rbi
!= rb_list
.end(); ++rbi
) {
827 EMetaBlob::remotebit
const &rb
= *rbi
;
829 // Get a key like "foobar_head"
831 dentry_key_t
dn_key(rb
.dnlast
, rb
.dn
.c_str());
834 dout(4) << "inspecting remotebit " << frag_oid
.name
<< "/" << rb
.dn
836 bool write_dentry
= false;
837 if (read_vals
.find(key
) == read_vals
.end()) {
838 dout(4) << "dentry did not already exist, will create" << dendl
;
841 dout(4) << "dentry " << key
<< " existed already" << dendl
;
842 dout(4) << "dentry exists, checking versions..." << dendl
;
843 bufferlist
&old_dentry
= read_vals
[key
];
844 // Decode dentry+inode
845 bufferlist::iterator q
= old_dentry
.begin();
848 ::decode(dnfirst
, q
);
850 ::decode(dentry_type
, q
);
852 if (dentry_type
== 'L') {
853 dout(10) << "Existing hardlink inode in slot to be (maybe) written "
854 << "by a remote inode from the journal dn '" << rb
.dn
.c_str()
855 << "' with lump fnode version " << lump
.fnode
.version
856 << "vs existing fnode version " << old_fnode_version
<< dendl
;
857 write_dentry
= old_fnode_version
< lump
.fnode
.version
;
858 } else if (dentry_type
== 'I') {
859 dout(10) << "Existing full inode in slot to be (maybe) written "
860 << "by a remote inode from the journal dn '" << rb
.dn
.c_str()
861 << "' with lump fnode version " << lump
.fnode
.version
862 << "vs existing fnode version " << old_fnode_version
<< dendl
;
863 write_dentry
= old_fnode_version
< lump
.fnode
.version
;
865 dout(4) << "corrupt dentry in backing store, overwriting from "
871 if ((other_pool
|| write_dentry
) && !dry_run
) {
872 dout(4) << "writing L dentry " << key
<< " into frag "
873 << frag_oid
.name
<< dendl
;
875 // Compose: Dentry format is dnfirst, [I|L], InodeStore(bare=true)
876 bufferlist dentry_bl
;
877 ::encode(rb
.dnfirst
, dentry_bl
);
878 ::encode('L', dentry_bl
);
879 ::encode(rb
.ino
, dentry_bl
);
880 ::encode(rb
.d_type
, dentry_bl
);
882 // Record for writing to RADOS
883 write_vals
[key
] = dentry_bl
;
884 consumed_inos
->insert(rb
.ino
);
888 std::set
<std::string
> null_vals
;
889 for (auto& nb
: nb_list
) {
891 dentry_key_t
dn_key(nb
.dnlast
, nb
.dn
.c_str());
894 dout(4) << "inspecting nullbit " << frag_oid
.name
<< "/" << nb
.dn
897 auto it
= read_vals
.find(key
);
898 if (it
!= read_vals
.end()) {
899 dout(4) << "dentry exists, will remove" << dendl
;
901 bufferlist::iterator q
= it
->second
.begin();
903 ::decode(dnfirst
, q
);
905 ::decode(dentry_type
, q
);
907 bool remove_dentry
= false;
908 if (dentry_type
== 'L') {
909 dout(10) << "Existing hardlink inode in slot to be (maybe) removed "
910 << "by null journal dn '" << nb
.dn
.c_str()
911 << "' with lump fnode version " << lump
.fnode
.version
912 << "vs existing fnode version " << old_fnode_version
<< dendl
;
913 remove_dentry
= old_fnode_version
< lump
.fnode
.version
;
914 } else if (dentry_type
== 'I') {
915 dout(10) << "Existing full inode in slot to be (maybe) removed "
916 << "by null journal dn '" << nb
.dn
.c_str()
917 << "' with lump fnode version " << lump
.fnode
.version
918 << "vs existing fnode version " << old_fnode_version
<< dendl
;
919 remove_dentry
= old_fnode_version
< lump
.fnode
.version
;
921 dout(4) << "corrupt dentry in backing store, will remove" << dendl
;
922 remove_dentry
= true;
926 null_vals
.insert(key
);
930 // Write back any new/changed dentries
931 if (!write_vals
.empty()) {
932 r
= output
.omap_set(frag_oid
.name
, write_vals
);
934 derr
<< "error writing dentries to " << frag_oid
.name
935 << ": " << cpp_strerror(r
) << dendl
;
940 // remove any null dentries
941 if (!null_vals
.empty()) {
942 r
= output
.omap_rm_keys(frag_oid
.name
, null_vals
);
944 derr
<< "error removing dentries from " << frag_oid
.name
945 << ": " << cpp_strerror(r
) << dendl
;
951 /* Now that we've looked at the dirlumps, we finally pay attention to
952 * the roots (i.e. inodes without ancestry). This is necessary in order
953 * to pick up dirstat updates on ROOT_INO. dirstat updates are functionally
954 * important because clients use them to infer completeness
957 for (list
<ceph::shared_ptr
<EMetaBlob::fullbit
> >::const_iterator p
=
958 metablob
.roots
.begin(); p
!= metablob
.roots
.end(); ++p
) {
959 EMetaBlob::fullbit
const &fb
= *(*p
);
960 inodeno_t ino
= fb
.inode
.ino
;
961 dout(4) << "updating root 0x" << std::hex
<< ino
<< std::dec
<< dendl
;
963 object_t root_oid
= InodeStore::get_object_name(ino
, frag_t(), ".inode");
964 dout(4) << "object id " << root_oid
.name
<< dendl
;
966 bool write_root_ino
= false;
967 bufferlist old_root_ino_bl
;
968 r
= input
.read(root_oid
.name
, old_root_ino_bl
, (1<<22), 0);
970 dout(4) << "root does not exist, will create" << dendl
;
971 write_root_ino
= true;
974 InodeStore old_inode
;
975 dout(4) << "root exists, will modify (" << old_root_ino_bl
.length()
977 bufferlist::iterator inode_bl_iter
= old_root_ino_bl
.begin();
979 ::decode(magic
, inode_bl_iter
);
980 if (magic
== CEPH_FS_ONDISK_MAGIC
) {
981 dout(4) << "magic ok" << dendl
;
982 old_inode
.decode(inode_bl_iter
);
984 if (old_inode
.inode
.version
< fb
.inode
.version
) {
985 write_root_ino
= true;
988 dout(4) << "magic bad: '" << magic
<< "'" << dendl
;
989 write_root_ino
= true;
992 derr
<< "error reading root inode object " << root_oid
.name
993 << ": " << cpp_strerror(r
) << dendl
;
997 if (write_root_ino
&& !dry_run
) {
998 dout(4) << "writing root ino " << root_oid
.name
999 << " version " << fb
.inode
.version
<< dendl
;
1001 // Compose: root ino format is magic,InodeStore(bare=false)
1002 bufferlist new_root_ino_bl
;
1003 ::encode(std::string(CEPH_FS_ONDISK_MAGIC
), new_root_ino_bl
);
1004 encode_fullbit_as_inode(fb
, false, &new_root_ino_bl
);
1007 r
= output
.write_full(root_oid
.name
, new_root_ino_bl
);
1009 derr
<< "error writing inode object " << root_oid
.name
1010 << ": " << cpp_strerror(r
) << dendl
;
1021 * Erase a region of the log by overwriting it with ENoOp
1024 int JournalTool::erase_region(JournalScanner
const &js
, uint64_t const pos
, uint64_t const length
)
1026 // To erase this region, we use our preamble, the encoding overhead
1027 // of an ENoOp, and our trailing start ptr. Calculate how much padding
1028 // is needed inside the ENoOp to make up the difference.
1031 enoop
.encode_with_header(tmp
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
1033 dout(4) << "erase_region " << pos
<< " len=" << length
<< dendl
;
1035 // FIXME: get the preamble/postamble length via JournalStream
1036 int32_t padding
= length
- tmp
.length() - sizeof(uint32_t) - sizeof(uint64_t) - sizeof(uint64_t);
1037 dout(4) << "erase_region padding=0x" << std::hex
<< padding
<< std::dec
<< dendl
;
1040 derr
<< "Erase region " << length
<< " too short" << dendl
;
1044 // Serialize an ENoOp with the correct amount of padding
1045 enoop
= ENoOp(padding
);
1047 enoop
.encode_with_header(entry
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
1048 JournalStream
stream(JOURNAL_FORMAT_RESILIENT
);
1050 // Serialize region of log stream
1051 bufferlist log_data
;
1052 stream
.write(entry
, &log_data
, pos
);
1054 dout(4) << "erase_region data length " << log_data
.length() << dendl
;
1055 assert(log_data
.length() == length
);
1057 // Write log stream region to RADOS
1058 // FIXME: get object size somewhere common to scan_events
1059 uint32_t object_size
= g_conf
->mds_log_segment_size
;
1060 if (object_size
== 0) {
1061 // Default layout object size
1062 object_size
= file_layout_t::get_default().object_size
;
1065 uint64_t write_offset
= pos
;
1066 uint64_t obj_offset
= (pos
/ object_size
);
1068 while(log_data
.length()) {
1069 std::string
const oid
= js
.obj_name(obj_offset
);
1070 uint32_t offset_in_obj
= write_offset
% object_size
;
1071 uint32_t write_len
= min(log_data
.length(), object_size
- offset_in_obj
);
1073 r
= output
.write(oid
, log_data
, write_len
, offset_in_obj
);
1077 dout(4) << "Wrote " << write_len
<< " bytes to " << oid
<< dendl
;
1081 log_data
.splice(0, write_len
);
1082 write_offset
+= write_len
;
1090 * Given an EMetaBlob::fullbit containing an inode, write out
1091 * the encoded inode in the format used by InodeStore (i.e. the
1092 * backing store format)
1094 * This is a distant cousin of EMetaBlob::fullbit::update_inode, but for use
1095 * on an offline InodeStore instance. It's way simpler, because we are just
1096 * uncritically hauling the data between structs.
1098 * @param fb a fullbit extracted from a journal entry
1099 * @param bare if true, leave out [EN|DE]CODE_START decoration
1100 * @param out_bl output, write serialized inode to this bufferlist
1102 void JournalTool::encode_fullbit_as_inode(
1103 const EMetaBlob::fullbit
&fb
,
1107 assert(out_bl
!= NULL
);
1109 // Compose InodeStore
1110 InodeStore new_inode
;
1111 new_inode
.inode
= fb
.inode
;
1112 new_inode
.xattrs
= fb
.xattrs
;
1113 new_inode
.dirfragtree
= fb
.dirfragtree
;
1114 new_inode
.snap_blob
= fb
.snapbl
;
1115 new_inode
.symlink
= mempool::mds_co::string(boost::string_view(fb
.symlink
));
1116 new_inode
.old_inodes
= fb
.old_inodes
;
1118 // Serialize InodeStore
1120 new_inode
.encode_bare(*out_bl
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
1122 new_inode
.encode(*out_bl
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
1127 * Given a list of inode numbers known to be in use by
1128 * inodes in the backing store, ensure that none of these
1129 * numbers are listed as free in the InoTables in the
1132 * Used after injecting inodes into the backing store, to
1133 * ensure that the same inode numbers are not subsequently
1134 * used for new files during ordinary operation.
1136 * @param inos list of inode numbers to be removed from
1137 * free lists in InoTables
1138 * @returns 0 on success, else negative error code
1140 int JournalTool::consume_inos(const std::set
<inodeno_t
> &inos
)
1144 // InoTable is a per-MDS structure, so iterate over assigned ranks
1145 auto fs
= fsmap
->get_filesystem(role_selector
.get_ns());
1146 std::set
<mds_rank_t
> in_ranks
;
1147 fs
->mds_map
.get_mds_set(in_ranks
);
1149 for (std::set
<mds_rank_t
>::iterator rank_i
= in_ranks
.begin();
1150 rank_i
!= in_ranks
.end(); ++rank_i
)
1152 // Compose object name
1153 std::ostringstream oss
;
1154 oss
<< "mds" << *rank_i
<< "_inotable";
1155 object_t inotable_oid
= object_t(oss
.str());
1158 bufferlist inotable_bl
;
1159 int read_r
= input
.read(inotable_oid
.name
, inotable_bl
, (1<<22), 0);
1161 // Things are really bad if we can't read inotable. Beyond our powers.
1162 derr
<< "unable to read inotable '" << inotable_oid
.name
<< "': "
1163 << cpp_strerror(read_r
) << dendl
;
1168 // Deserialize InoTable
1169 version_t inotable_ver
;
1170 bufferlist::iterator q
= inotable_bl
.begin();
1171 ::decode(inotable_ver
, q
);
1172 InoTable
ino_table(NULL
);
1173 ino_table
.decode(q
);
1175 // Update InoTable in memory
1176 bool inotable_modified
= false;
1177 for (std::set
<inodeno_t
>::iterator i
= inos
.begin();
1178 i
!= inos
.end(); ++i
)
1180 const inodeno_t ino
= *i
;
1181 if (ino_table
.force_consume(ino
)) {
1182 dout(4) << "Used ino 0x" << std::hex
<< ino
<< std::dec
1183 << " requires inotable update" << dendl
;
1184 inotable_modified
= true;
1188 // Serialize and write InoTable
1189 if (inotable_modified
) {
1191 dout(4) << "writing modified inotable version " << inotable_ver
<< dendl
;
1192 bufferlist inotable_new_bl
;
1193 ::encode(inotable_ver
, inotable_new_bl
);
1194 ino_table
.encode_state(inotable_new_bl
);
1195 int write_r
= output
.write_full(inotable_oid
.name
, inotable_new_bl
);
1197 derr
<< "error writing modified inotable " << inotable_oid
.name
1198 << ": " << cpp_strerror(write_r
) << dendl
;