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) 2013 Inktank
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.
15 #include <boost/program_options/variables_map.hpp>
16 #include <boost/program_options/parsers.hpp>
17 #include <boost/scoped_ptr.hpp>
18 #include <boost/optional.hpp>
22 #include "common/Formatter.h"
23 #include "common/errno.h"
24 #include "common/ceph_argparse.h"
25 #include "common/url_escape.h"
27 #include "global/global_init.h"
29 #include "os/ObjectStore.h"
30 #include "os/filestore/FileJournal.h"
31 #include "os/filestore/FileStore.h"
33 #include "os/FuseStore.h"
36 #include "osd/PGLog.h"
39 #include "osd/ECUtil.h"
41 #include "json_spirit/json_spirit_value.h"
42 #include "json_spirit/json_spirit_reader.h"
44 #include "rebuild_mondb.h"
45 #include "ceph_objectstore_tool.h"
46 #include "include/compat.h"
47 #include "include/util.h"
49 namespace po
= boost::program_options
;
52 CompatSet
get_test_compat_set() {
53 CompatSet::FeatureSet ceph_osd_feature_compat
;
54 CompatSet::FeatureSet ceph_osd_feature_ro_compat
;
55 CompatSet::FeatureSet ceph_osd_feature_incompat
;
56 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_BASE
);
57 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_PGINFO
);
58 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_OLOC
);
59 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_LEC
);
60 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_CATEGORIES
);
61 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_HOBJECTPOOL
);
62 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_BIGINFO
);
63 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBINFO
);
64 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBLOG
);
66 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_SNAPMAPPER
);
67 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_SHARDS
);
69 return CompatSet(ceph_osd_feature_compat
, ceph_osd_feature_ro_compat
,
70 ceph_osd_feature_incompat
);
74 const ssize_t max_read
= 1024 * 1024;
75 const int fd_none
= INT_MIN
;
79 struct action_on_object_t
{
80 virtual ~action_on_object_t() {}
81 virtual void call(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, object_info_t
&oi
) = 0;
84 int _action_on_all_objects_in_pg(ObjectStore
*store
, coll_t coll
, action_on_object_t
&action
, bool debug
)
86 auto ch
= store
->open_collection(coll
);
87 unsigned LIST_AT_A_TIME
= 100;
89 while (!next
.is_max()) {
90 vector
<ghobject_t
> list
;
91 int r
= store
->collection_list(ch
,
93 ghobject_t::get_max(),
98 cerr
<< "Error listing collection: " << coll
<< ", "
99 << cpp_strerror(r
) << std::endl
;
102 for (vector
<ghobject_t
>::iterator obj
= list
.begin();
105 if (obj
->is_pgmeta())
108 if (coll
!= coll_t::meta()) {
110 r
= store
->getattr(ch
, *obj
, OI_ATTR
, attr
);
112 cerr
<< "Error getting attr on : " << make_pair(coll
, *obj
) << ", "
113 << cpp_strerror(r
) << std::endl
;
115 auto bp
= attr
.cbegin();
120 cerr
<< "Error decoding attr on : " << make_pair(coll
, *obj
) << ", "
121 << cpp_strerror(r
) << std::endl
;
125 action
.call(store
, coll
, *obj
, oi
);
131 int action_on_all_objects_in_pg(ObjectStore
*store
, string pgidstr
, action_on_object_t
&action
, bool debug
)
134 // Scan collections in case this is an ec pool but no shard specified
135 unsigned scanned
= 0;
137 vector
<coll_t
> colls_to_check
;
138 vector
<coll_t
> candidates
;
139 r
= store
->list_collections(candidates
);
141 cerr
<< "Error listing collections: " << cpp_strerror(r
) << std::endl
;
144 pgid
.parse(pgidstr
.c_str());
145 for (vector
<coll_t
>::iterator i
= candidates
.begin();
146 i
!= candidates
.end();
149 if (!i
->is_pg(&cand_pgid
))
152 // If an exact match or treat no shard as any shard
153 if (cand_pgid
== pgid
||
154 (pgid
.is_no_shard() && pgid
.pgid
== cand_pgid
.pgid
)) {
155 colls_to_check
.push_back(*i
);
160 cerr
<< colls_to_check
.size() << " pgs to scan" << std::endl
;
161 for (vector
<coll_t
>::iterator i
= colls_to_check
.begin();
162 i
!= colls_to_check
.end();
165 cerr
<< "Scanning " << *i
<< ", " << scanned
<< "/"
166 << colls_to_check
.size() << " completed" << std::endl
;
167 r
= _action_on_all_objects_in_pg(store
, *i
, action
, debug
);
174 int action_on_all_objects_in_exact_pg(ObjectStore
*store
, coll_t coll
, action_on_object_t
&action
, bool debug
)
176 int r
= _action_on_all_objects_in_pg(store
, coll
, action
, debug
);
180 int _action_on_all_objects(ObjectStore
*store
, action_on_object_t
&action
, bool debug
)
182 unsigned scanned
= 0;
184 vector
<coll_t
> colls_to_check
;
185 vector
<coll_t
> candidates
;
186 r
= store
->list_collections(candidates
);
188 cerr
<< "Error listing collections: " << cpp_strerror(r
) << std::endl
;
191 for (vector
<coll_t
>::iterator i
= candidates
.begin();
192 i
!= candidates
.end();
195 colls_to_check
.push_back(*i
);
200 cerr
<< colls_to_check
.size() << " pgs to scan" << std::endl
;
201 for (vector
<coll_t
>::iterator i
= colls_to_check
.begin();
202 i
!= colls_to_check
.end();
205 cerr
<< "Scanning " << *i
<< ", " << scanned
<< "/"
206 << colls_to_check
.size() << " completed" << std::endl
;
207 r
= _action_on_all_objects_in_pg(store
, *i
, action
, debug
);
214 int action_on_all_objects(ObjectStore
*store
, action_on_object_t
&action
, bool debug
)
216 int r
= _action_on_all_objects(store
, action
, debug
);
220 struct pgid_object_list
{
221 list
<pair
<coll_t
, ghobject_t
> > _objects
;
223 void insert(coll_t coll
, ghobject_t
&ghobj
) {
224 _objects
.push_back(make_pair(coll
, ghobj
));
227 void dump(Formatter
*f
, bool human_readable
) const {
229 f
->open_array_section("pgid_objects");
230 for (list
<pair
<coll_t
, ghobject_t
> >::const_iterator i
= _objects
.begin();
233 f
->open_array_section("pgid_object");
235 bool is_pg
= i
->first
.is_pg(&pgid
);
237 f
->dump_string("pgid", stringify(pgid
));
238 if (!is_pg
|| !human_readable
)
239 f
->dump_string("coll", i
->first
.to_str());
240 f
->open_object_section("ghobject");
244 if (human_readable
) {
249 if (!human_readable
) {
257 struct lookup_ghobject
: public action_on_object_t
{
258 pgid_object_list _objects
;
260 const boost::optional
<std::string
> _namespace
;
263 lookup_ghobject(const string
& name
, const boost::optional
<std::string
>& nspace
, bool need_snapset
= false) : _name(name
),
264 _namespace(nspace
), _need_snapset(need_snapset
) { }
266 void call(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, object_info_t
&oi
) override
{
267 if (_need_snapset
&& !ghobj
.hobj
.has_snapset())
269 if ((_name
.length() == 0 || ghobj
.hobj
.oid
.name
== _name
) &&
270 (!_namespace
|| ghobj
.hobj
.nspace
== _namespace
))
271 _objects
.insert(coll
, ghobj
);
276 return _objects
._objects
.size();
279 pair
<coll_t
, ghobject_t
> pop() {
280 pair
<coll_t
, ghobject_t
> front
= _objects
._objects
.front();
281 _objects
._objects
.pop_front();
285 void dump(Formatter
*f
, bool human_readable
) const {
286 _objects
.dump(f
, human_readable
);
290 struct lookup_slow_ghobject
: public action_on_object_t
{
303 lookup_slow_ghobject(const string
& name
, double _threshold
) :
304 _name(name
), threshold(_threshold
) { }
306 void call(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, object_info_t
&oi
) override
{
307 ObjectMap::ObjectMapIterator iter
;
308 auto start1
= mono_clock::now();
309 ceph::signedspan first_seek_time
= start1
- start1
;
310 ceph::signedspan last_seek_time
= first_seek_time
;
311 ceph::signedspan total_time
= first_seek_time
;
313 auto ch
= store
->open_collection(coll
);
314 iter
= store
->get_omap_iterator(ch
, ghobj
);
316 cerr
<< "omap_get_iterator: " << cpp_strerror(ENOENT
)
321 auto start
= mono_clock::now();
322 iter
->seek_to_first();
323 first_seek_time
= mono_clock::now() - start
;
325 while(iter
->valid()) {
326 start
= mono_clock::now();
328 last_seek_time
= mono_clock::now() - start
;
332 if (coll
!= last_coll
) {
333 cerr
<< ">>> inspecting coll" << coll
<< std::endl
;
337 total_time
= mono_clock::now() - start1
;
338 if ( total_time
>= make_timespan(threshold
)) {
339 _objects
.emplace_back(coll
, ghobj
,
340 first_seek_time
, last_seek_time
, total_time
,
341 url_escape(iter
->tail_key()));
342 cerr
<< ">>>>> found obj " << ghobj
343 << " first_seek_time "
344 << std::chrono::duration_cast
<std::chrono::seconds
>(first_seek_time
).count()
345 << " last_seek_time "
346 << std::chrono::duration_cast
<std::chrono::seconds
>(last_seek_time
).count()
348 << std::chrono::duration_cast
<std::chrono::seconds
>(total_time
).count()
349 << " tail key: " << url_escape(iter
->tail_key())
356 return _objects
.size();
359 void dump(Formatter
*f
, bool human_readable
) const {
361 f
->open_array_section("objects");
362 for (auto i
= _objects
.begin();
365 f
->open_array_section("object");
368 ceph::signedspan first_seek_time
;
369 ceph::signedspan last_seek_time
;
370 ceph::signedspan total_time
;
372 std::tie(coll
, ghobj
, first_seek_time
, last_seek_time
, total_time
, tail_key
) = *i
;
375 bool is_pg
= coll
.is_pg(&pgid
);
377 f
->dump_string("pgid", stringify(pgid
));
378 if (!is_pg
|| !human_readable
)
379 f
->dump_string("coll", coll
.to_str());
380 f
->dump_object("ghobject", ghobj
);
381 f
->open_object_section("times");
382 f
->dump_int("first_seek_time",
383 std::chrono::duration_cast
<std::chrono::seconds
>(first_seek_time
).count());
384 f
->dump_int("last_seek_time",
385 std::chrono::duration_cast
<std::chrono::seconds
>
386 (last_seek_time
).count());
387 f
->dump_int("total_time",
388 std::chrono::duration_cast
<std::chrono::seconds
>(total_time
).count());
389 f
->dump_string("tail_key", tail_key
);
393 if (human_readable
) {
398 if (!human_readable
) {
406 int file_fd
= fd_none
;
409 bool no_superblock
= false;
413 static int get_fd_data(int fd
, bufferlist
&bl
)
417 ssize_t bytes
= bl
.read_fd(fd
, max_read
);
419 cerr
<< "read_fd error " << cpp_strerror(bytes
) << std::endl
;
429 ceph_assert(bl
.length() == total
);
433 int get_log(ObjectStore
*fs
, __u8 struct_ver
,
434 spg_t pgid
, const pg_info_t
&info
,
435 PGLog::IndexedLog
&log
, pg_missing_t
&missing
)
438 auto ch
= fs
->open_collection(coll_t(pgid
));
443 ceph_assert(struct_ver
> 0);
444 PGLog::read_log_and_missing(
446 pgid
.make_pgmeta_oid(),
449 g_ceph_context
->_conf
->osd_ignore_stale_divergent_priors
);
450 if (debug
&& oss
.str().size())
451 cerr
<< oss
.str() << std::endl
;
453 catch (const buffer::error
&e
) {
454 cerr
<< "read_log_and_missing threw exception error " << e
.what() << std::endl
;
460 void dump_log(Formatter
*formatter
, ostream
&out
, pg_log_t
&log
,
461 pg_missing_t
&missing
)
463 formatter
->open_object_section("op_log");
464 formatter
->open_object_section("pg_log_t");
466 formatter
->close_section();
467 formatter
->flush(out
);
468 formatter
->open_object_section("pg_missing_t");
469 missing
.dump(formatter
);
470 formatter
->close_section();
471 formatter
->close_section();
472 formatter
->flush(out
);
475 //Based on part of OSD::load_pgs()
476 int finish_remove_pgs(ObjectStore
*store
)
479 int r
= store
->list_collections(ls
);
481 cerr
<< "finish_remove_pgs: failed to list pgs: " << cpp_strerror(r
)
486 for (vector
<coll_t
>::iterator it
= ls
.begin();
491 if (it
->is_temp(&pgid
) ||
492 (it
->is_pg(&pgid
) && PG::_has_removal_flag(store
, pgid
))) {
493 cout
<< "finish_remove_pgs " << *it
<< " removing " << pgid
<< std::endl
;
494 OSD::recursive_remove_collection(g_ceph_context
, store
, pgid
, *it
);
498 //cout << "finish_remove_pgs ignoring unrecognized " << *it << std::endl;
503 #pragma GCC diagnostic ignored "-Wpragmas"
504 #pragma GCC diagnostic push
505 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
507 int mark_pg_for_removal(ObjectStore
*fs
, spg_t pgid
, ObjectStore::Transaction
*t
)
509 pg_info_t
info(pgid
);
511 ghobject_t
pgmeta_oid(info
.pgid
.make_pgmeta_oid());
513 epoch_t map_epoch
= 0;
514 int r
= PG::peek_map_epoch(fs
, pgid
, &map_epoch
);
516 cerr
<< __func__
<< " warning: peek_map_epoch reported error" << std::endl
;
517 PastIntervals past_intervals
;
519 r
= PG::read_info(fs
, pgid
, coll
, info
, past_intervals
, struct_v
);
521 cerr
<< __func__
<< " error on read_info " << cpp_strerror(r
) << std::endl
;
524 ceph_assert(struct_v
>= 8);
526 cout
<< "setting '_remove' omap key" << std::endl
;
527 map
<string
,bufferlist
> values
;
528 encode((char)1, values
["_remove"]);
529 t
->omap_setkeys(coll
, pgmeta_oid
, values
);
533 #pragma GCC diagnostic pop
534 #pragma GCC diagnostic warning "-Wpragmas"
536 template<typename Func
>
537 void wait_until_done(ObjectStore::Transaction
* txn
, Func
&& func
)
539 bool finished
= false;
540 std::condition_variable cond
;
542 txn
->register_on_complete(make_lambda_context([&](int) {
543 std::unique_lock lock
{m
};
548 std::unique_lock lock
{m
};
549 cond
.wait(lock
, [&] {return finished
;});
552 int initiate_new_remove_pg(ObjectStore
*store
, spg_t r_pgid
)
555 finish_remove_pgs(store
);
556 if (!store
->collection_exists(coll_t(r_pgid
)))
559 cout
<< " marking collection for removal" << std::endl
;
562 ObjectStore::Transaction rmt
;
563 int r
= mark_pg_for_removal(store
, r_pgid
, &rmt
);
567 ObjectStore::CollectionHandle ch
= store
->open_collection(coll_t(r_pgid
));
568 store
->queue_transaction(ch
, std::move(rmt
));
569 finish_remove_pgs(store
);
573 int write_info(ObjectStore::Transaction
&t
, epoch_t epoch
, pg_info_t
&info
,
574 PastIntervals
&past_intervals
)
577 coll_t
coll(info
.pgid
);
578 ghobject_t
pgmeta_oid(info
.pgid
.make_pgmeta_oid());
579 map
<string
,bufferlist
> km
;
580 string key_to_remove
;
581 pg_info_t last_written_info
;
582 int ret
= prepare_info_keymap(
590 if (ret
) cerr
<< "Failed to write info" << std::endl
;
591 t
.omap_setkeys(coll
, pgmeta_oid
, km
);
592 if (!key_to_remove
.empty()) {
593 t
.omap_rmkey(coll
, pgmeta_oid
, key_to_remove
);
598 typedef map
<eversion_t
, hobject_t
> divergent_priors_t
;
600 int write_pg(ObjectStore::Transaction
&t
, epoch_t epoch
, pg_info_t
&info
,
601 pg_log_t
&log
, PastIntervals
&past_intervals
,
602 divergent_priors_t
&divergent
,
603 pg_missing_t
&missing
)
605 cout
<< __func__
<< " epoch " << epoch
<< " info " << info
<< std::endl
;
606 int ret
= write_info(t
, epoch
, info
, past_intervals
);
609 coll_t
coll(info
.pgid
);
610 map
<string
,bufferlist
> km
;
612 if (!divergent
.empty()) {
613 ceph_assert(missing
.get_items().empty());
614 PGLog::write_log_and_missing_wo_missing(
615 t
, &km
, log
, coll
, info
.pgid
.make_pgmeta_oid(), divergent
, true);
617 pg_missing_tracker_t
tmissing(missing
);
618 bool rebuilt_missing_set_with_deletes
= missing
.may_include_deletes
;
619 PGLog::write_log_and_missing(
620 t
, &km
, log
, coll
, info
.pgid
.make_pgmeta_oid(), tmissing
, true,
621 &rebuilt_missing_set_with_deletes
);
623 t
.omap_setkeys(coll
, info
.pgid
.make_pgmeta_oid(), km
);
627 int do_trim_pg_log(ObjectStore
*store
, const coll_t
&coll
,
628 pg_info_t
&info
, const spg_t
&pgid
,
630 PastIntervals
&past_intervals
)
632 ghobject_t oid
= pgid
.make_pgmeta_oid();
634 auto ch
= store
->open_collection(coll
);
635 int r
= store
->stat(ch
, oid
, &st
);
637 ceph_assert(st
.st_size
== 0);
639 cerr
<< "Log bounds are: " << "(" << info
.log_tail
<< ","
640 << info
.last_update
<< "]" << std::endl
;
642 uint64_t max_entries
= g_ceph_context
->_conf
->osd_max_pg_log_entries
;
643 if (info
.last_update
.version
- info
.log_tail
.version
<= max_entries
) {
644 cerr
<< "Log not larger than osd_max_pg_log_entries " << max_entries
<< std::endl
;
648 ceph_assert(info
.last_update
.version
> max_entries
);
649 version_t trim_to
= info
.last_update
.version
- max_entries
;
650 size_t trim_at_once
= g_ceph_context
->_conf
->osd_pg_log_trim_max
;
655 // gather keys so we can delete them in a batch without
656 // affecting the iterator
657 set
<string
> keys_to_trim
;
659 ObjectMap::ObjectMapIterator p
= store
->get_omap_iterator(ch
, oid
);
662 for (p
->seek_to_first(); p
->valid(); p
->next()) {
663 if (p
->key()[0] == '_')
665 if (p
->key() == "can_rollback_to")
667 if (p
->key() == "divergent_priors")
669 if (p
->key() == "rollback_info_trimmed_to")
671 if (p
->key() == "may_include_deletes_in_missing")
673 if (p
->key().substr(0, 7) == string("missing"))
675 if (p
->key().substr(0, 4) == string("dup_"))
678 bufferlist bl
= p
->value();
679 auto bp
= bl
.cbegin();
682 e
.decode_with_checksum(bp
);
683 } catch (const buffer::error
&e
) {
684 cerr
<< "Error reading pg log entry: " << e
.what() << std::endl
;
687 cerr
<< "read entry " << e
<< std::endl
;
689 if (e
.version
.version
> trim_to
) {
693 keys_to_trim
.insert(p
->key());
694 new_tail
= e
.version
;
695 if (keys_to_trim
.size() >= trim_at_once
)
701 } // deconstruct ObjectMapIterator
704 if (!dry_run
&& !keys_to_trim
.empty()) {
705 cout
<< "Removing keys " << *keys_to_trim
.begin() << " - " << *keys_to_trim
.rbegin() << std::endl
;
706 ObjectStore::Transaction t
;
707 t
.omap_rmkeys(coll
, oid
, keys_to_trim
);
708 store
->queue_transaction(ch
, std::move(t
));
713 // update pg info with new tail
714 if (!dry_run
&& new_tail
!= eversion_t()) {
715 info
.log_tail
= new_tail
;
716 ObjectStore::Transaction t
;
717 int ret
= write_info(t
, map_epoch
, info
, past_intervals
);
720 store
->queue_transaction(ch
, std::move(t
));
724 // compact the db since we just removed a bunch of data
725 cerr
<< "Finished trimming, now compacting..." << std::endl
;
731 const int OMAP_BATCH_SIZE
= 25;
732 void get_omap_batch(ObjectMap::ObjectMapIterator
&iter
, map
<string
, bufferlist
> &oset
)
735 for (int count
= OMAP_BATCH_SIZE
; count
&& iter
->valid(); --count
, iter
->next()) {
736 oset
.insert(pair
<string
, bufferlist
>(iter
->key(), iter
->value()));
740 int ObjectStoreTool::export_file(ObjectStore
*store
, coll_t cid
, ghobject_t
&obj
)
746 auto ch
= store
->open_collection(cid
);
747 int ret
= store
->stat(ch
, obj
, &st
);
751 cerr
<< "Read " << obj
<< std::endl
;
755 cerr
<< "size=" << total
<< std::endl
;
757 object_begin
objb(obj
);
762 ret
= store
->getattr(ch
, obj
, OI_ATTR
, bp
);
764 cerr
<< "getattr failure object_info " << ret
<< std::endl
;
770 cerr
<< "object_info: " << objb
.oi
<< std::endl
;
773 // NOTE: we include whiteouts, lost, etc.
775 ret
= write_section(TYPE_OBJECT_BEGIN
, objb
, file_fd
);
780 bufferlist rawdatabl
;
783 mysize_t len
= max_read
;
787 ret
= store
->read(ch
, obj
, offset
, len
, rawdatabl
);
793 data_section
dblock(offset
, len
, rawdatabl
);
795 cerr
<< "data section offset=" << offset
<< " len=" << len
<< std::endl
;
800 ret
= write_section(TYPE_DATA
, dblock
, file_fd
);
804 //Handle attrs for this object
805 map
<string
,bufferptr
> aset
;
806 ret
= store
->getattrs(ch
, obj
, aset
);
808 attr_section
as(aset
);
809 ret
= write_section(TYPE_ATTRS
, as
, file_fd
);
814 cerr
<< "attrs size " << aset
.size() << std::endl
;
817 //Handle omap information
819 ret
= store
->omap_get_header(ch
, obj
, &hdrbuf
, true);
821 cerr
<< "omap_get_header: " << cpp_strerror(ret
) << std::endl
;
825 omap_hdr_section
ohs(hdrbuf
);
826 ret
= write_section(TYPE_OMAP_HDR
, ohs
, file_fd
);
830 ObjectMap::ObjectMapIterator iter
= store
->get_omap_iterator(ch
, obj
);
833 cerr
<< "omap_get_iterator: " << cpp_strerror(ret
) << std::endl
;
836 iter
->seek_to_first();
838 map
<string
, bufferlist
> out
;
839 while(iter
->valid()) {
840 get_omap_batch(iter
, out
);
842 if (out
.empty()) break;
844 mapcount
+= out
.size();
845 omap_section
oms(out
);
846 ret
= write_section(TYPE_OMAP
, oms
, file_fd
);
851 cerr
<< "omap map size " << mapcount
<< std::endl
;
853 ret
= write_simple(TYPE_OBJECT_END
, file_fd
);
860 int ObjectStoreTool::export_files(ObjectStore
*store
, coll_t coll
)
863 auto ch
= store
->open_collection(coll
);
864 while (!next
.is_max()) {
865 vector
<ghobject_t
> objects
;
866 int r
= store
->collection_list(ch
, next
, ghobject_t::get_max(), 300,
870 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
873 ceph_assert(!i
->hobj
.is_meta());
874 if (i
->is_pgmeta() || i
->hobj
.is_temp() || !i
->is_no_gen()) {
877 r
= export_file(store
, coll
, *i
);
885 int set_inc_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
, bool force
) {
886 OSDMap::Incremental inc
;
887 auto it
= bl
.cbegin();
891 } else if (e
!= inc
.epoch
) {
892 cerr
<< "incremental.epoch mismatch: "
893 << inc
.epoch
<< " != " << e
<< std::endl
;
895 cerr
<< "But will continue anyway." << std::endl
;
900 auto ch
= store
->open_collection(coll_t::meta());
901 const ghobject_t inc_oid
= OSD::get_inc_osdmap_pobject_name(e
);
902 if (!store
->exists(ch
, inc_oid
)) {
903 cerr
<< "inc-osdmap (" << inc_oid
<< ") does not exist." << std::endl
;
907 cout
<< "Creating a new epoch." << std::endl
;
911 ObjectStore::Transaction t
;
912 t
.write(coll_t::meta(), inc_oid
, 0, bl
.length(), bl
);
913 t
.truncate(coll_t::meta(), inc_oid
, bl
.length());
914 store
->queue_transaction(ch
, std::move(t
));
918 int get_inc_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
)
920 auto ch
= store
->open_collection(coll_t::meta());
922 OSD::get_inc_osdmap_pobject_name(e
),
929 int set_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
, bool force
) {
933 e
= osdmap
.get_epoch();
934 } else if (e
!= osdmap
.get_epoch()) {
935 cerr
<< "osdmap.epoch mismatch: "
936 << e
<< " != " << osdmap
.get_epoch() << std::endl
;
938 cerr
<< "But will continue anyway." << std::endl
;
943 auto ch
= store
->open_collection(coll_t::meta());
944 const ghobject_t full_oid
= OSD::get_osdmap_pobject_name(e
);
945 if (!store
->exists(ch
, full_oid
)) {
946 cerr
<< "osdmap (" << full_oid
<< ") does not exist." << std::endl
;
950 cout
<< "Creating a new epoch." << std::endl
;
954 ObjectStore::Transaction t
;
955 t
.write(coll_t::meta(), full_oid
, 0, bl
.length(), bl
);
956 t
.truncate(coll_t::meta(), full_oid
, bl
.length());
957 store
->queue_transaction(ch
, std::move(t
));
961 int get_osdmap(ObjectStore
*store
, epoch_t e
, OSDMap
&osdmap
, bufferlist
& bl
)
963 ObjectStore::CollectionHandle ch
= store
->open_collection(coll_t::meta());
964 bool found
= store
->read(
965 ch
, OSD::get_osdmap_pobject_name(e
), 0, 0, bl
) >= 0;
967 cerr
<< "Can't find OSDMap for pg epoch " << e
<< std::endl
;
972 cerr
<< osdmap
<< std::endl
;
976 int get_pg_num_history(ObjectStore
*store
, pool_pg_num_history_t
*h
)
978 ObjectStore::CollectionHandle ch
= store
->open_collection(coll_t::meta());
980 auto pghist
= OSD::make_pg_num_history_oid();
981 int r
= store
->read(ch
, pghist
, 0, 0, bl
, 0);
982 if (r
>= 0 && bl
.length() > 0) {
983 auto p
= bl
.cbegin();
986 cout
<< __func__
<< " pg_num_history " << *h
<< std::endl
;
990 int add_osdmap(ObjectStore
*store
, metadata_section
&ms
)
992 return get_osdmap(store
, ms
.map_epoch
, ms
.osdmap
, ms
.osdmap_bl
);
995 int ObjectStoreTool::do_export(ObjectStore
*fs
, coll_t coll
, spg_t pgid
,
996 pg_info_t
&info
, epoch_t map_epoch
, __u8 struct_ver
,
997 const OSDSuperblock
& superblock
,
998 PastIntervals
&past_intervals
)
1000 PGLog::IndexedLog log
;
1001 pg_missing_t missing
;
1003 cerr
<< "Exporting " << pgid
<< " info " << info
<< std::endl
;
1005 int ret
= get_log(fs
, struct_ver
, pgid
, info
, log
, missing
);
1010 Formatter
*formatter
= Formatter::create("json-pretty");
1011 ceph_assert(formatter
);
1012 dump_log(formatter
, cerr
, log
, missing
);
1017 pg_begin
pgb(pgid
, superblock
);
1018 // Special case: If replicated pg don't require the importing OSD to have shard feature
1019 if (pgid
.is_no_shard()) {
1020 pgb
.superblock
.compat_features
.incompat
.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS
);
1022 ret
= write_section(TYPE_PG_BEGIN
, pgb
, file_fd
);
1026 // The metadata_section is now before files, so import can detect
1027 // errors and abort without wasting time.
1028 metadata_section
ms(
1035 ret
= add_osdmap(fs
, ms
);
1038 ret
= write_section(TYPE_PG_METADATA
, ms
, file_fd
);
1042 ret
= export_files(fs
, coll
);
1044 cerr
<< "export_files error " << ret
<< std::endl
;
1048 ret
= write_simple(TYPE_PG_END
, file_fd
);
1055 int dump_data(Formatter
*formatter
, bufferlist
&bl
)
1057 auto ebliter
= bl
.cbegin();
1061 formatter
->open_object_section("data_block");
1062 formatter
->dump_unsigned("offset", ds
.offset
);
1063 formatter
->dump_unsigned("len", ds
.len
);
1064 // XXX: Add option to dump data like od -cx ?
1065 formatter
->close_section();
1066 formatter
->flush(cout
);
1070 int get_data(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1071 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1073 auto ebliter
= bl
.cbegin();
1078 cerr
<< "\tdata: offset " << ds
.offset
<< " len " << ds
.len
<< std::endl
;
1079 t
->write(coll
, hoid
, ds
.offset
, ds
.len
, ds
.databl
);
1084 Formatter
*formatter
, ghobject_t hoid
,
1087 auto ebliter
= bl
.cbegin();
1091 // This could have been handled in the caller if we didn't need to
1092 // support exports that didn't include object_info_t in object_begin.
1093 if (hoid
.generation
== ghobject_t::NO_GEN
&&
1094 hoid
.hobj
.is_head()) {
1095 map
<string
,bufferlist
>::iterator mi
= as
.data
.find(SS_ATTR
);
1096 if (mi
!= as
.data
.end()) {
1098 auto p
= mi
->second
.cbegin();
1100 formatter
->open_object_section("snapset");
1101 snapset
.dump(formatter
);
1102 formatter
->close_section();
1104 formatter
->open_object_section("snapset");
1105 formatter
->dump_string("error", "missing SS_ATTR");
1106 formatter
->close_section();
1110 formatter
->open_object_section("attrs");
1111 formatter
->open_array_section("user");
1112 for (auto kv
: as
.data
) {
1113 // Skip system attributes
1114 if (('_' != kv
.first
.at(0)) || kv
.first
.size() == 1)
1116 formatter
->open_object_section("user_attr");
1117 formatter
->dump_string("name", kv
.first
.substr(1));
1119 formatter
->dump_string("value", cleanbin(kv
.second
, b64
));
1120 formatter
->dump_bool("Base64", b64
);
1121 formatter
->close_section();
1123 formatter
->close_section();
1124 formatter
->open_array_section("system");
1125 for (auto kv
: as
.data
) {
1126 // Skip user attributes
1127 if (('_' == kv
.first
.at(0)) && kv
.first
.size() != 1)
1129 formatter
->open_object_section("sys_attr");
1130 formatter
->dump_string("name", kv
.first
);
1131 formatter
->close_section();
1133 formatter
->close_section();
1134 formatter
->close_section();
1135 formatter
->flush(cout
);
1141 ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1142 ObjectStore::Transaction
*t
, bufferlist
&bl
,
1143 OSDriver
&driver
, SnapMapper
&snap_mapper
)
1145 auto ebliter
= bl
.cbegin();
1149 auto ch
= store
->open_collection(coll
);
1151 cerr
<< "\tattrs: len " << as
.data
.size() << std::endl
;
1152 t
->setattrs(coll
, hoid
, as
.data
);
1154 // This could have been handled in the caller if we didn't need to
1155 // support exports that didn't include object_info_t in object_begin.
1156 if (hoid
.generation
== ghobject_t::NO_GEN
&&
1157 hoid
.hobj
.is_head()) {
1158 map
<string
,bufferlist
>::iterator mi
= as
.data
.find(SS_ATTR
);
1159 if (mi
!= as
.data
.end()) {
1161 auto p
= mi
->second
.cbegin();
1163 cout
<< "snapset " << snapset
<< std::endl
;
1164 for (auto& p
: snapset
.clone_snaps
) {
1165 ghobject_t clone
= hoid
;
1166 clone
.hobj
.snap
= p
.first
;
1167 set
<snapid_t
> snaps(p
.second
.begin(), p
.second
.end());
1168 if (!store
->exists(ch
, clone
)) {
1169 // no clone, skip. this is probably a cache pool. this works
1170 // because we use a separate transaction per object and clones
1171 // come before head in the archive.
1173 cerr
<< "\tskipping missing " << clone
<< " (snaps "
1174 << snaps
<< ")" << std::endl
;
1178 cerr
<< "\tsetting " << clone
.hobj
<< " snaps " << snaps
1180 OSDriver::OSTransaction
_t(driver
.get_transaction(t
));
1181 ceph_assert(!snaps
.empty());
1182 snap_mapper
.add_oid(clone
.hobj
, snaps
, &_t
);
1185 cerr
<< "missing SS_ATTR on " << hoid
<< std::endl
;
1191 int dump_omap_hdr(Formatter
*formatter
, bufferlist
&bl
)
1193 auto ebliter
= bl
.cbegin();
1194 omap_hdr_section oh
;
1197 formatter
->open_object_section("omap_header");
1198 formatter
->dump_string("value", string(oh
.hdr
.c_str(), oh
.hdr
.length()));
1199 formatter
->close_section();
1200 formatter
->flush(cout
);
1204 int get_omap_hdr(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1205 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1207 auto ebliter
= bl
.cbegin();
1208 omap_hdr_section oh
;
1212 cerr
<< "\tomap header: " << string(oh
.hdr
.c_str(), oh
.hdr
.length())
1214 t
->omap_setheader(coll
, hoid
, oh
.hdr
);
1218 int dump_omap(Formatter
*formatter
, bufferlist
&bl
)
1220 auto ebliter
= bl
.cbegin();
1224 formatter
->open_object_section("omaps");
1225 formatter
->dump_unsigned("count", os
.omap
.size());
1226 formatter
->open_array_section("data");
1227 for (auto o
: os
.omap
) {
1228 formatter
->open_object_section("omap");
1229 formatter
->dump_string("name", o
.first
);
1231 formatter
->dump_string("value", cleanbin(o
.second
, b64
));
1232 formatter
->dump_bool("Base64", b64
);
1233 formatter
->close_section();
1235 formatter
->close_section();
1236 formatter
->close_section();
1237 formatter
->flush(cout
);
1241 int get_omap(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1242 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1244 auto ebliter
= bl
.cbegin();
1249 cerr
<< "\tomap: size " << os
.omap
.size() << std::endl
;
1250 t
->omap_setkeys(coll
, hoid
, os
.omap
);
1254 int ObjectStoreTool::dump_object(Formatter
*formatter
,
1257 auto ebliter
= bl
.cbegin();
1261 if (ob
.hoid
.hobj
.is_temp()) {
1262 cerr
<< "ERROR: Export contains temporary object '" << ob
.hoid
<< "'" << std::endl
;
1266 formatter
->open_object_section("object");
1267 formatter
->open_object_section("oid");
1268 ob
.hoid
.dump(formatter
);
1269 formatter
->close_section();
1270 formatter
->open_object_section("object_info");
1271 ob
.oi
.dump(formatter
);
1272 formatter
->close_section();
1278 int ret
= read_section(&type
, &ebl
);
1282 //cout << "\tdo_object: Section type " << hex << type << dec << std::endl;
1283 //cout << "\t\tsection size " << ebl.length() << std::endl;
1284 if (type
>= END_OF_TYPES
) {
1285 cout
<< "Skipping unknown object section type" << std::endl
;
1291 ret
= dump_data(formatter
, ebl
);
1292 if (ret
) return ret
;
1296 ret
= dump_attrs(formatter
, ob
.hoid
, ebl
);
1297 if (ret
) return ret
;
1301 ret
= dump_omap_hdr(formatter
, ebl
);
1302 if (ret
) return ret
;
1306 ret
= dump_omap(formatter
, ebl
);
1307 if (ret
) return ret
;
1309 case TYPE_OBJECT_END
:
1313 cerr
<< "Unknown section type " << type
<< std::endl
;
1317 formatter
->close_section();
1321 int ObjectStoreTool::get_object(ObjectStore
*store
,
1325 bufferlist
&bl
, OSDMap
&origmap
,
1326 bool *skipped_objects
)
1328 ObjectStore::Transaction tran
;
1329 ObjectStore::Transaction
*t
= &tran
;
1330 auto ebliter
= bl
.cbegin();
1334 if (ob
.hoid
.hobj
.is_temp()) {
1335 cerr
<< "ERROR: Export contains temporary object '" << ob
.hoid
<< "'" << std::endl
;
1338 ceph_assert(g_ceph_context
);
1340 auto ch
= store
->open_collection(coll
);
1341 if (ob
.hoid
.hobj
.nspace
!= g_ceph_context
->_conf
->osd_hit_set_namespace
) {
1342 object_t oid
= ob
.hoid
.hobj
.oid
;
1343 object_locator_t
loc(ob
.hoid
.hobj
);
1344 pg_t raw_pgid
= origmap
.object_locator_to_pg(oid
, loc
);
1345 pg_t pgid
= origmap
.raw_pg_to_pg(raw_pgid
);
1348 if (coll
.is_pg(&coll_pgid
) == false) {
1349 cerr
<< "INTERNAL ERROR: Bad collection during import" << std::endl
;
1352 if (coll_pgid
.shard
!= ob
.hoid
.shard_id
) {
1353 cerr
<< "INTERNAL ERROR: Importing shard " << coll_pgid
.shard
1354 << " but object shard is " << ob
.hoid
.shard_id
<< std::endl
;
1358 if (coll_pgid
.pgid
!= pgid
) {
1359 cerr
<< "Skipping object '" << ob
.hoid
<< "' which belongs in pg " << pgid
<< std::endl
;
1360 *skipped_objects
= true;
1367 t
->touch(coll
, ob
.hoid
);
1369 cout
<< "Write " << ob
.hoid
<< std::endl
;
1375 int ret
= read_section(&type
, &ebl
);
1379 //cout << "\tdo_object: Section type " << hex << type << dec << std::endl;
1380 //cout << "\t\tsection size " << ebl.length() << std::endl;
1381 if (type
>= END_OF_TYPES
) {
1382 cout
<< "Skipping unknown object section type" << std::endl
;
1388 ret
= get_data(store
, coll
, ob
.hoid
, t
, ebl
);
1389 if (ret
) return ret
;
1393 ret
= get_attrs(store
, coll
, ob
.hoid
, t
, ebl
, driver
, mapper
);
1394 if (ret
) return ret
;
1398 ret
= get_omap_hdr(store
, coll
, ob
.hoid
, t
, ebl
);
1399 if (ret
) return ret
;
1403 ret
= get_omap(store
, coll
, ob
.hoid
, t
, ebl
);
1404 if (ret
) return ret
;
1406 case TYPE_OBJECT_END
:
1410 cerr
<< "Unknown section type " << type
<< std::endl
;
1415 wait_until_done(t
, [&] {
1416 store
->queue_transaction(ch
, std::move(*t
));
1423 int dump_pg_metadata(Formatter
*formatter
, bufferlist
&bl
, metadata_section
&ms
)
1425 auto ebliter
= bl
.cbegin();
1428 formatter
->open_object_section("metadata_section");
1430 formatter
->dump_unsigned("pg_disk_version", (int)ms
.struct_ver
);
1431 formatter
->dump_unsigned("map_epoch", ms
.map_epoch
);
1433 formatter
->open_object_section("OSDMap");
1434 ms
.osdmap
.dump(formatter
);
1435 formatter
->close_section();
1436 formatter
->flush(cout
);
1439 formatter
->open_object_section("info");
1440 ms
.info
.dump(formatter
);
1441 formatter
->close_section();
1442 formatter
->flush(cout
);
1444 formatter
->open_object_section("log");
1445 ms
.log
.dump(formatter
);
1446 formatter
->close_section();
1447 formatter
->flush(cout
);
1449 formatter
->open_object_section("pg_missing_t");
1450 ms
.missing
.dump(formatter
);
1451 formatter
->close_section();
1453 // XXX: ms.past_intervals?
1455 formatter
->close_section();
1456 formatter
->flush(cout
);
1458 if (ms
.osdmap
.get_epoch() != 0 && ms
.map_epoch
!= ms
.osdmap
.get_epoch()) {
1459 cerr
<< "FATAL: Invalid OSDMap epoch in export data" << std::endl
;
1466 int get_pg_metadata(ObjectStore
*store
, bufferlist
&bl
, metadata_section
&ms
,
1467 const OSDSuperblock
& sb
, spg_t pgid
)
1469 auto ebliter
= bl
.cbegin();
1471 spg_t old_pgid
= ms
.info
.pgid
;
1472 ms
.info
.pgid
= pgid
;
1475 cout
<< "export pgid " << old_pgid
<< std::endl
;
1476 cout
<< "struct_v " << (int)ms
.struct_ver
<< std::endl
;
1477 cout
<< "map epoch " << ms
.map_epoch
<< std::endl
;
1480 Formatter
*formatter
= new JSONFormatter(true);
1481 formatter
->open_object_section("stuff");
1483 formatter
->open_object_section("importing OSDMap");
1484 ms
.osdmap
.dump(formatter
);
1485 formatter
->close_section();
1486 formatter
->flush(cout
);
1489 cout
<< "osd current epoch " << sb
.current_epoch
<< std::endl
;
1491 formatter
->open_object_section("info");
1492 ms
.info
.dump(formatter
);
1493 formatter
->close_section();
1494 formatter
->flush(cout
);
1497 formatter
->open_object_section("log");
1498 ms
.log
.dump(formatter
);
1499 formatter
->close_section();
1500 formatter
->flush(cout
);
1503 formatter
->close_section();
1504 formatter
->flush(cout
);
1509 if (ms
.osdmap
.get_epoch() != 0 && ms
.map_epoch
!= ms
.osdmap
.get_epoch()) {
1510 cerr
<< "FATAL: Invalid OSDMap epoch in export data" << std::endl
;
1514 if (ms
.map_epoch
> sb
.current_epoch
) {
1515 cerr
<< "ERROR: Export PG's map_epoch " << ms
.map_epoch
<< " > OSD's epoch " << sb
.current_epoch
<< std::endl
;
1516 cerr
<< "The OSD you are using is older than the exported PG" << std::endl
;
1517 cerr
<< "Either use another OSD or join selected OSD to cluster to update it first" << std::endl
;
1521 // Old exports didn't include OSDMap
1522 if (ms
.osdmap
.get_epoch() == 0) {
1523 cerr
<< "WARNING: No OSDMap in old export, this is an ancient export."
1524 " Not supported." << std::endl
;
1528 if (ms
.osdmap
.get_epoch() < sb
.oldest_map
) {
1529 cerr
<< "PG export's map " << ms
.osdmap
.get_epoch()
1530 << " is older than OSD's oldest_map " << sb
.oldest_map
<< std::endl
;
1532 cerr
<< " pass --force to proceed anyway (with incomplete PastIntervals)"
1538 cerr
<< "Import pgid " << ms
.info
.pgid
<< std::endl
;
1539 cerr
<< "Previous past_intervals " << ms
.past_intervals
<< std::endl
;
1540 cerr
<< "history.same_interval_since "
1541 << ms
.info
.history
.same_interval_since
<< std::endl
;
1547 // out: pg_log_t that only has entries that apply to import_pgid using curmap
1548 // reject: Entries rejected from "in" are in the reject.log. Other fields not set.
1549 void filter_divergent_priors(spg_t import_pgid
, const OSDMap
&curmap
,
1550 const string
&hit_set_namespace
, const divergent_priors_t
&in
,
1551 divergent_priors_t
&out
, divergent_priors_t
&reject
)
1556 for (divergent_priors_t::const_iterator i
= in
.begin();
1557 i
!= in
.end(); ++i
) {
1559 // Reject divergent priors for temporary objects
1560 if (i
->second
.is_temp()) {
1565 if (i
->second
.nspace
!= hit_set_namespace
) {
1566 object_t oid
= i
->second
.oid
;
1567 object_locator_t
loc(i
->second
);
1568 pg_t raw_pgid
= curmap
.object_locator_to_pg(oid
, loc
);
1569 pg_t pgid
= curmap
.raw_pg_to_pg(raw_pgid
);
1571 if (import_pgid
.pgid
== pgid
) {
1582 int ObjectStoreTool::dump_export(Formatter
*formatter
)
1586 PGLog::IndexedLog log
;
1587 //bool skipped_objects = false;
1589 int ret
= read_super();
1593 if (sh
.magic
!= super_header::super_magic
) {
1594 cerr
<< "Invalid magic number" << std::endl
;
1598 if (sh
.version
> super_header::super_ver
) {
1599 cerr
<< "Can't handle export format version=" << sh
.version
<< std::endl
;
1603 formatter
->open_object_section("Export");
1605 //First section must be TYPE_PG_BEGIN
1607 ret
= read_section(&type
, &ebl
);
1610 if (type
== TYPE_POOL_BEGIN
) {
1611 cerr
<< "Dump of pool exports not supported" << std::endl
;
1613 } else if (type
!= TYPE_PG_BEGIN
) {
1614 cerr
<< "Invalid first section type " << std::to_string(type
) << std::endl
;
1618 auto ebliter
= ebl
.cbegin();
1620 pgb
.decode(ebliter
);
1621 spg_t pgid
= pgb
.pgid
;
1623 formatter
->dump_string("pgid", stringify(pgid
));
1624 formatter
->dump_string("cluster_fsid", stringify(pgb
.superblock
.cluster_fsid
));
1625 formatter
->dump_string("features", stringify(pgb
.superblock
.compat_features
));
1628 bool found_metadata
= false;
1629 metadata_section ms
;
1630 bool objects_started
= false;
1632 ret
= read_section(&type
, &ebl
);
1637 cerr
<< "dump_export: Section type " << std::to_string(type
) << std::endl
;
1639 if (type
>= END_OF_TYPES
) {
1640 cerr
<< "Skipping unknown section type" << std::endl
;
1644 case TYPE_OBJECT_BEGIN
:
1645 if (!objects_started
) {
1646 formatter
->open_array_section("objects");
1647 objects_started
= true;
1649 ret
= dump_object(formatter
, ebl
);
1650 if (ret
) return ret
;
1652 case TYPE_PG_METADATA
:
1653 if (objects_started
)
1654 cerr
<< "WARNING: metadata_section out of order" << std::endl
;
1655 ret
= dump_pg_metadata(formatter
, ebl
, ms
);
1656 if (ret
) return ret
;
1657 found_metadata
= true;
1660 if (objects_started
) {
1661 formatter
->close_section();
1666 cerr
<< "Unknown section type " << std::to_string(type
) << std::endl
;
1671 if (!found_metadata
) {
1672 cerr
<< "Missing metadata section" << std::endl
;
1676 formatter
->close_section();
1677 formatter
->flush(cout
);
1682 int ObjectStoreTool::do_import(ObjectStore
*store
, OSDSuperblock
& sb
,
1683 bool force
, std::string pgidstr
)
1687 PGLog::IndexedLog log
;
1688 bool skipped_objects
= false;
1691 finish_remove_pgs(store
);
1693 int ret
= read_super();
1697 if (sh
.magic
!= super_header::super_magic
) {
1698 cerr
<< "Invalid magic number" << std::endl
;
1702 if (sh
.version
> super_header::super_ver
) {
1703 cerr
<< "Can't handle export format version=" << sh
.version
<< std::endl
;
1707 //First section must be TYPE_PG_BEGIN
1709 ret
= read_section(&type
, &ebl
);
1712 if (type
== TYPE_POOL_BEGIN
) {
1713 cerr
<< "Pool exports cannot be imported into a PG" << std::endl
;
1715 } else if (type
!= TYPE_PG_BEGIN
) {
1716 cerr
<< "Invalid first section type " << std::to_string(type
) << std::endl
;
1720 auto ebliter
= ebl
.cbegin();
1722 pgb
.decode(ebliter
);
1723 spg_t pgid
= pgb
.pgid
;
1725 if (pgidstr
.length()) {
1728 bool ok
= user_pgid
.parse(pgidstr
.c_str());
1729 // This succeeded in main() already
1731 if (pgid
!= user_pgid
) {
1732 cerr
<< "specified pgid " << user_pgid
1733 << " does not match actual pgid " << pgid
<< std::endl
;
1738 if (!pgb
.superblock
.cluster_fsid
.is_zero()
1739 && pgb
.superblock
.cluster_fsid
!= sb
.cluster_fsid
) {
1740 cerr
<< "Export came from different cluster with fsid "
1741 << pgb
.superblock
.cluster_fsid
<< std::endl
;
1746 cerr
<< "Exported features: " << pgb
.superblock
.compat_features
<< std::endl
;
1749 // Special case: Old export has SHARDS incompat feature on replicated pg, removqqe it
1750 if (pgid
.is_no_shard())
1751 pgb
.superblock
.compat_features
.incompat
.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS
);
1753 if (sb
.compat_features
.compare(pgb
.superblock
.compat_features
) == -1) {
1754 CompatSet unsupported
= sb
.compat_features
.unsupported(pgb
.superblock
.compat_features
);
1756 cerr
<< "Export has incompatible features set " << unsupported
<< std::endl
;
1758 // Let them import if they specify the --force option
1760 return 11; // Positive return means exit status
1763 // we need the latest OSDMap to check for collisions
1766 ret
= get_osdmap(store
, sb
.current_epoch
, curmap
, bl
);
1768 cerr
<< "Can't find latest local OSDMap " << sb
.current_epoch
<< std::endl
;
1771 if (!curmap
.have_pg_pool(pgid
.pgid
.m_pool
)) {
1772 cerr
<< "Pool " << pgid
.pgid
.m_pool
<< " no longer exists" << std::endl
;
1773 // Special exit code for this error, used by test code
1774 return 10; // Positive return means exit status
1777 pool_pg_num_history_t pg_num_history
;
1778 get_pg_num_history(store
, &pg_num_history
);
1780 ghobject_t pgmeta_oid
= pgid
.make_pgmeta_oid();
1782 // Check for PG already present.
1784 if (store
->collection_exists(coll
)) {
1785 cerr
<< "pgid " << pgid
<< " already exists" << std::endl
;
1789 ObjectStore::CollectionHandle ch
;
1794 OSD::make_snapmapper_oid());
1795 SnapMapper
mapper(g_ceph_context
, &driver
, 0, 0, 0, pgid
.shard
);
1797 cout
<< "Importing pgid " << pgid
;
1801 bool found_metadata
= false;
1802 metadata_section ms
;
1804 ret
= read_section(&type
, &ebl
);
1809 cout
<< __func__
<< ": Section type " << std::to_string(type
) << std::endl
;
1811 if (type
>= END_OF_TYPES
) {
1812 cout
<< "Skipping unknown section type" << std::endl
;
1816 case TYPE_OBJECT_BEGIN
:
1817 ceph_assert(found_metadata
);
1818 ret
= get_object(store
, driver
, mapper
, coll
, ebl
, ms
.osdmap
,
1820 if (ret
) return ret
;
1822 case TYPE_PG_METADATA
:
1823 ret
= get_pg_metadata(store
, ebl
, ms
, sb
, pgid
);
1824 if (ret
) return ret
;
1825 found_metadata
= true;
1827 if (pgid
!= ms
.info
.pgid
) {
1828 cerr
<< "specified pgid " << pgid
<< " does not match import file pgid "
1829 << ms
.info
.pgid
<< std::endl
;
1833 // make sure there are no conflicting splits or merges
1834 if (ms
.osdmap
.have_pg_pool(pgid
.pgid
.pool())) {
1835 auto p
= pg_num_history
.pg_nums
.find(pgid
.pgid
.m_pool
);
1836 if (p
!= pg_num_history
.pg_nums
.end() &&
1837 !p
->second
.empty()) {
1838 unsigned start_pg_num
= ms
.osdmap
.get_pg_num(pgid
.pgid
.pool());
1839 unsigned pg_num
= start_pg_num
;
1840 for (auto q
= p
->second
.lower_bound(ms
.map_epoch
);
1841 q
!= p
->second
.end();
1843 unsigned new_pg_num
= q
->second
;
1844 cout
<< "pool " << pgid
.pgid
.pool() << " pg_num " << pg_num
1845 << " -> " << new_pg_num
<< std::endl
;
1847 // check for merge target
1849 if (pgid
.is_merge_source(pg_num
, new_pg_num
, &target
)) {
1850 // FIXME: this checks assumes the OSD's PG is at the OSD's
1851 // map epoch; it could be, say, at *our* epoch, pre-merge.
1852 coll_t
coll(target
);
1853 if (store
->collection_exists(coll
)) {
1854 cerr
<< "pgid " << pgid
<< " merges to target " << target
1855 << " which already exists" << std::endl
;
1860 // check for split children
1861 set
<spg_t
> children
;
1862 if (pgid
.is_split(start_pg_num
, new_pg_num
, &children
)) {
1863 cerr
<< " children are " << children
<< std::endl
;
1864 for (auto child
: children
) {
1866 if (store
->collection_exists(coll
)) {
1867 cerr
<< "pgid " << pgid
<< " splits to " << children
1868 << " and " << child
<< " exists" << std::endl
;
1873 pg_num
= new_pg_num
;
1877 cout
<< "pool " << pgid
.pgid
.pool() << " doesn't existing, not checking"
1878 << " for splits or mergers" << std::endl
;
1882 ObjectStore::Transaction t
;
1883 ch
= store
->create_new_collection(coll
);
1884 create_pg_collection(
1886 pgid
.get_split_bits(ms
.osdmap
.get_pg_pool(pgid
.pool())->get_pg_num()));
1887 init_pg_ondisk(t
, pgid
, NULL
);
1889 // mark this coll for removal until we're done
1890 map
<string
,bufferlist
> values
;
1891 encode((char)1, values
["_remove"]);
1892 t
.omap_setkeys(coll
, pgid
.make_pgmeta_oid(), values
);
1894 store
->queue_transaction(ch
, std::move(t
));
1899 ceph_assert(found_metadata
);
1903 cerr
<< "Unknown section type " << std::to_string(type
) << std::endl
;
1908 if (!found_metadata
) {
1909 cerr
<< "Missing metadata section" << std::endl
;
1913 ObjectStore::Transaction t
;
1915 pg_log_t newlog
, reject
;
1916 pg_log_t::filter_log(pgid
, ms
.osdmap
, g_ceph_context
->_conf
->osd_hit_set_namespace
,
1917 ms
.log
, newlog
, reject
);
1919 for (list
<pg_log_entry_t
>::iterator i
= newlog
.log
.begin();
1920 i
!= newlog
.log
.end(); ++i
)
1921 cerr
<< "Keeping log entry " << *i
<< std::endl
;
1922 for (list
<pg_log_entry_t
>::iterator i
= reject
.log
.begin();
1923 i
!= reject
.log
.end(); ++i
)
1924 cerr
<< "Skipping log entry " << *i
<< std::endl
;
1927 divergent_priors_t newdp
, rejectdp
;
1928 filter_divergent_priors(pgid
, ms
.osdmap
, g_ceph_context
->_conf
->osd_hit_set_namespace
,
1929 ms
.divergent_priors
, newdp
, rejectdp
);
1930 ms
.divergent_priors
= newdp
;
1932 for (divergent_priors_t::iterator i
= newdp
.begin();
1933 i
!= newdp
.end(); ++i
)
1934 cerr
<< "Keeping divergent_prior " << *i
<< std::endl
;
1935 for (divergent_priors_t::iterator i
= rejectdp
.begin();
1936 i
!= rejectdp
.end(); ++i
)
1937 cerr
<< "Skipping divergent_prior " << *i
<< std::endl
;
1940 ms
.missing
.filter_objects([&](const hobject_t
&obj
) {
1941 if (obj
.nspace
== g_ceph_context
->_conf
->osd_hit_set_namespace
)
1943 ceph_assert(!obj
.is_temp());
1944 object_t oid
= obj
.oid
;
1945 object_locator_t
loc(obj
);
1946 pg_t raw_pgid
= ms
.osdmap
.object_locator_to_pg(oid
, loc
);
1947 pg_t _pgid
= ms
.osdmap
.raw_pg_to_pg(raw_pgid
);
1949 return pgid
.pgid
!= _pgid
;
1954 pg_missing_t missing
;
1955 Formatter
*formatter
= Formatter::create("json-pretty");
1956 dump_log(formatter
, cerr
, newlog
, ms
.missing
);
1960 // Just like a split invalidate stats since the object count is changed
1961 if (skipped_objects
)
1962 ms
.info
.stats
.stats_invalid
= true;
1970 ms
.divergent_priors
,
1972 if (ret
) return ret
;
1975 // done, clear removal flag
1977 cerr
<< "done, clearing removal flag" << std::endl
;
1980 t
.omap_rmkey(coll
, pgid
.make_pgmeta_oid(), "_remove");
1981 wait_until_done(&t
, [&] {
1982 store
->queue_transaction(ch
, std::move(t
));
1983 // make sure we flush onreadable items before mapper/driver are destroyed.
1990 int do_list(ObjectStore
*store
, string pgidstr
, string object
, boost::optional
<std::string
> nspace
,
1991 Formatter
*formatter
, bool debug
, bool human_readable
, bool head
)
1994 lookup_ghobject
lookup(object
, nspace
, head
);
1995 if (pgidstr
.length() > 0) {
1996 r
= action_on_all_objects_in_pg(store
, pgidstr
, lookup
, debug
);
1998 r
= action_on_all_objects(store
, lookup
, debug
);
2002 lookup
.dump(formatter
, human_readable
);
2003 formatter
->flush(cout
);
2007 int do_list_slow(ObjectStore
*store
, string pgidstr
, string object
,
2008 double threshold
, Formatter
*formatter
, bool debug
, bool human_readable
)
2011 lookup_slow_ghobject
lookup(object
, threshold
);
2012 if (pgidstr
.length() > 0) {
2013 r
= action_on_all_objects_in_pg(store
, pgidstr
, lookup
, debug
);
2015 r
= action_on_all_objects(store
, lookup
, debug
);
2019 lookup
.dump(formatter
, human_readable
);
2020 formatter
->flush(cout
);
2024 int do_meta(ObjectStore
*store
, string object
, Formatter
*formatter
, bool debug
, bool human_readable
)
2027 boost::optional
<std::string
> nspace
; // Not specified
2028 lookup_ghobject
lookup(object
, nspace
);
2029 r
= action_on_all_objects_in_exact_pg(store
, coll_t::meta(), lookup
, debug
);
2032 lookup
.dump(formatter
, human_readable
);
2033 formatter
->flush(cout
);
2043 int remove_object(coll_t coll
, ghobject_t
&ghobj
,
2045 MapCacher::Transaction
<std::string
, bufferlist
> *_t
,
2046 ObjectStore::Transaction
*t
,
2049 if (type
== BOTH
|| type
== SNAPMAP
) {
2050 int r
= mapper
.remove_oid(ghobj
.hobj
, _t
);
2051 if (r
< 0 && r
!= -ENOENT
) {
2052 cerr
<< "remove_oid returned " << cpp_strerror(r
) << std::endl
;
2057 if (type
== BOTH
|| type
== NOSNAPMAP
) {
2058 t
->remove(coll
, ghobj
);
2063 int get_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, SnapSet
&ss
, bool silent
);
2065 int do_remove_object(ObjectStore
*store
, coll_t coll
,
2066 ghobject_t
&ghobj
, bool all
, bool force
, enum rmtype type
)
2068 auto ch
= store
->open_collection(coll
);
2070 coll
.is_pg_prefix(&pg
);
2074 OSD::make_snapmapper_oid());
2075 SnapMapper
mapper(g_ceph_context
, &driver
, 0, 0, 0, pg
.shard
);
2078 int r
= store
->stat(ch
, ghobj
, &st
);
2080 cerr
<< "remove: " << cpp_strerror(r
) << std::endl
;
2085 if (ghobj
.hobj
.has_snapset()) {
2086 r
= get_snapset(store
, coll
, ghobj
, ss
, false);
2088 cerr
<< "Can't get snapset error " << cpp_strerror(r
) << std::endl
;
2089 // If --force and bad snapset let them remove the head
2090 if (!(force
&& !all
))
2093 // cout << "snapset " << ss << std::endl;
2094 if (!ss
.clone_snaps
.empty() && !all
) {
2096 cout
<< "WARNING: only removing "
2097 << (ghobj
.hobj
.is_head() ? "head" : "snapdir")
2098 << " with clones present" << std::endl
;
2099 ss
.clone_snaps
.clear();
2101 cerr
<< "Clones are present, use removeall to delete everything"
2108 ObjectStore::Transaction t
;
2109 OSDriver::OSTransaction
_t(driver
.get_transaction(&t
));
2111 ghobject_t snapobj
= ghobj
;
2112 for (auto& p
: ss
.clone_snaps
) {
2113 snapobj
.hobj
.snap
= p
.first
;
2114 cout
<< "remove clone " << snapobj
<< std::endl
;
2116 r
= remove_object(coll
, snapobj
, mapper
, &_t
, &t
, type
);
2122 cout
<< "remove " << ghobj
<< std::endl
;
2125 r
= remove_object(coll
, ghobj
, mapper
, &_t
, &t
, type
);
2131 wait_until_done(&t
, [&] {
2132 store
->queue_transaction(ch
, std::move(t
));
2139 int do_list_attrs(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2141 auto ch
= store
->open_collection(coll
);
2142 map
<string
,bufferptr
> aset
;
2143 int r
= store
->getattrs(ch
, ghobj
, aset
);
2145 cerr
<< "getattrs: " << cpp_strerror(r
) << std::endl
;
2149 for (map
<string
,bufferptr
>::iterator i
= aset
.begin();i
!= aset
.end(); ++i
) {
2150 string
key(i
->first
);
2152 key
= cleanbin(key
);
2153 cout
<< key
<< std::endl
;
2158 int do_list_omap(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2160 auto ch
= store
->open_collection(coll
);
2161 ObjectMap::ObjectMapIterator iter
= store
->get_omap_iterator(ch
, ghobj
);
2163 cerr
<< "omap_get_iterator: " << cpp_strerror(ENOENT
) << std::endl
;
2166 iter
->seek_to_first();
2167 map
<string
, bufferlist
> oset
;
2168 while(iter
->valid()) {
2169 get_omap_batch(iter
, oset
);
2171 for (map
<string
,bufferlist
>::iterator i
= oset
.begin();i
!= oset
.end(); ++i
) {
2172 string
key(i
->first
);
2174 key
= cleanbin(key
);
2175 cout
<< key
<< std::endl
;
2181 int do_get_bytes(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, int fd
)
2183 auto ch
= store
->open_collection(coll
);
2187 int ret
= store
->stat(ch
, ghobj
, &st
);
2189 cerr
<< "get-bytes: " << cpp_strerror(ret
) << std::endl
;
2195 cerr
<< "size=" << total
<< std::endl
;
2197 uint64_t offset
= 0;
2198 bufferlist rawdatabl
;
2201 mysize_t len
= max_read
;
2205 ret
= store
->read(ch
, ghobj
, offset
, len
, rawdatabl
);
2212 cerr
<< "data section offset=" << offset
<< " len=" << len
<< std::endl
;
2217 ret
= write(fd
, rawdatabl
.c_str(), ret
);
2227 int do_set_bytes(ObjectStore
*store
, coll_t coll
,
2228 ghobject_t
&ghobj
, int fd
)
2230 ObjectStore::Transaction tran
;
2231 ObjectStore::Transaction
*t
= &tran
;
2234 cerr
<< "Write " << ghobj
<< std::endl
;
2237 t
->touch(coll
, ghobj
);
2238 t
->truncate(coll
, ghobj
, 0);
2241 uint64_t offset
= 0;
2242 bufferlist rawdatabl
;
2245 ssize_t bytes
= rawdatabl
.read_fd(fd
, max_read
);
2247 cerr
<< "read_fd error " << cpp_strerror(bytes
) << std::endl
;
2255 cerr
<< "\tdata: offset " << offset
<< " bytes " << bytes
<< std::endl
;
2257 t
->write(coll
, ghobj
, offset
, bytes
, rawdatabl
);
2260 // XXX: Should we queue_transaction() every once in a while for very large files
2263 auto ch
= store
->open_collection(coll
);
2265 store
->queue_transaction(ch
, std::move(*t
));
2269 int do_get_attr(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, string key
)
2271 auto ch
= store
->open_collection(coll
);
2274 int r
= store
->getattr(ch
, ghobj
, key
.c_str(), bp
);
2276 cerr
<< "getattr: " << cpp_strerror(r
) << std::endl
;
2280 string
value(bp
.c_str(), bp
.length());
2282 value
= cleanbin(value
);
2283 value
.push_back('\n');
2290 int do_set_attr(ObjectStore
*store
, coll_t coll
,
2291 ghobject_t
&ghobj
, string key
, int fd
)
2293 ObjectStore::Transaction tran
;
2294 ObjectStore::Transaction
*t
= &tran
;
2298 cerr
<< "Setattr " << ghobj
<< std::endl
;
2300 int ret
= get_fd_data(fd
, bl
);
2307 t
->touch(coll
, ghobj
);
2309 t
->setattr(coll
, ghobj
, key
, bl
);
2311 auto ch
= store
->open_collection(coll
);
2312 store
->queue_transaction(ch
, std::move(*t
));
2316 int do_rm_attr(ObjectStore
*store
, coll_t coll
,
2317 ghobject_t
&ghobj
, string key
)
2319 ObjectStore::Transaction tran
;
2320 ObjectStore::Transaction
*t
= &tran
;
2323 cerr
<< "Rmattr " << ghobj
<< std::endl
;
2328 t
->rmattr(coll
, ghobj
, key
);
2330 auto ch
= store
->open_collection(coll
);
2331 store
->queue_transaction(ch
, std::move(*t
));
2335 int do_get_omap(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, string key
)
2337 auto ch
= store
->open_collection(coll
);
2339 map
<string
, bufferlist
> out
;
2343 int r
= store
->omap_get_values(ch
, ghobj
, keys
, &out
);
2345 cerr
<< "omap_get_values: " << cpp_strerror(r
) << std::endl
;
2350 cerr
<< "Key not found" << std::endl
;
2354 ceph_assert(out
.size() == 1);
2356 bufferlist bl
= out
.begin()->second
;
2357 string
value(bl
.c_str(), bl
.length());
2359 value
= cleanbin(value
);
2360 value
.push_back('\n');
2367 int do_set_omap(ObjectStore
*store
, coll_t coll
,
2368 ghobject_t
&ghobj
, string key
, int fd
)
2370 ObjectStore::Transaction tran
;
2371 ObjectStore::Transaction
*t
= &tran
;
2372 map
<string
, bufferlist
> attrset
;
2376 cerr
<< "Set_omap " << ghobj
<< std::endl
;
2378 int ret
= get_fd_data(fd
, valbl
);
2382 attrset
.insert(pair
<string
, bufferlist
>(key
, valbl
));
2387 t
->touch(coll
, ghobj
);
2389 t
->omap_setkeys(coll
, ghobj
, attrset
);
2391 auto ch
= store
->open_collection(coll
);
2392 store
->queue_transaction(ch
, std::move(*t
));
2396 int do_rm_omap(ObjectStore
*store
, coll_t coll
,
2397 ghobject_t
&ghobj
, string key
)
2399 ObjectStore::Transaction tran
;
2400 ObjectStore::Transaction
*t
= &tran
;
2403 cerr
<< "Rm_omap " << ghobj
<< std::endl
;
2408 t
->omap_rmkey(coll
, ghobj
, key
);
2410 auto ch
= store
->open_collection(coll
);
2411 store
->queue_transaction(ch
, std::move(*t
));
2415 int do_get_omaphdr(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2417 auto ch
= store
->open_collection(coll
);
2420 int r
= store
->omap_get_header(ch
, ghobj
, &hdrbl
, true);
2422 cerr
<< "omap_get_header: " << cpp_strerror(r
) << std::endl
;
2426 string
header(hdrbl
.c_str(), hdrbl
.length());
2428 header
= cleanbin(header
);
2429 header
.push_back('\n');
2436 int do_set_omaphdr(ObjectStore
*store
, coll_t coll
,
2437 ghobject_t
&ghobj
, int fd
)
2439 ObjectStore::Transaction tran
;
2440 ObjectStore::Transaction
*t
= &tran
;
2444 cerr
<< "Omap_setheader " << ghobj
<< std::endl
;
2446 int ret
= get_fd_data(fd
, hdrbl
);
2453 t
->touch(coll
, ghobj
);
2455 t
->omap_setheader(coll
, ghobj
, hdrbl
);
2457 auto ch
= store
->open_collection(coll
);
2458 store
->queue_transaction(ch
, std::move(*t
));
2462 struct do_fix_lost
: public action_on_object_t
{
2463 void call(ObjectStore
*store
, coll_t coll
,
2464 ghobject_t
&ghobj
, object_info_t
&oi
) override
{
2466 cout
<< coll
<< "/" << ghobj
<< " is lost";
2472 oi
.clear_flag(object_info_t::FLAG_LOST
);
2474 encode(oi
, bl
, -1); /* fixme: using full features */
2475 ObjectStore::Transaction t
;
2476 t
.setattr(coll
, ghobj
, OI_ATTR
, bl
);
2477 auto ch
= store
->open_collection(coll
);
2478 store
->queue_transaction(ch
, std::move(t
));
2484 int get_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, SnapSet
&ss
, bool silent
= false)
2486 auto ch
= store
->open_collection(coll
);
2488 int r
= store
->getattr(ch
, ghobj
, SS_ATTR
, attr
);
2491 cerr
<< "Error getting snapset on : " << make_pair(coll
, ghobj
) << ", "
2492 << cpp_strerror(r
) << std::endl
;
2495 auto bp
= attr
.cbegin();
2500 cerr
<< "Error decoding snapset on : " << make_pair(coll
, ghobj
) << ", "
2501 << cpp_strerror(r
) << std::endl
;
2507 int print_obj_info(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, Formatter
* formatter
)
2509 auto ch
= store
->open_collection(coll
);
2511 formatter
->open_object_section("obj");
2512 formatter
->open_object_section("id");
2513 ghobj
.dump(formatter
);
2514 formatter
->close_section();
2517 int gr
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2520 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2521 << cpp_strerror(r
) << std::endl
;
2524 auto bp
= attr
.cbegin();
2527 formatter
->open_object_section("info");
2529 formatter
->close_section();
2532 cerr
<< "Error decoding attr on : " << make_pair(coll
, ghobj
) << ", "
2533 << cpp_strerror(r
) << std::endl
;
2537 int sr
= store
->stat(ch
, ghobj
, &st
, true);
2540 cerr
<< "Error stat on : " << make_pair(coll
, ghobj
) << ", "
2541 << cpp_strerror(r
) << std::endl
;
2543 formatter
->open_object_section("stat");
2544 formatter
->dump_int("size", st
.st_size
);
2545 formatter
->dump_int("blksize", st
.st_blksize
);
2546 formatter
->dump_int("blocks", st
.st_blocks
);
2547 formatter
->dump_int("nlink", st
.st_nlink
);
2548 formatter
->close_section();
2551 if (ghobj
.hobj
.has_snapset()) {
2553 int snr
= get_snapset(store
, coll
, ghobj
, ss
);
2557 formatter
->open_object_section("SnapSet");
2559 formatter
->close_section();
2563 gr
= store
->getattr(ch
, ghobj
, ECUtil::get_hinfo_key(), hattr
);
2565 ECUtil::HashInfo hinfo
;
2566 auto hp
= hattr
.cbegin();
2569 formatter
->open_object_section("hinfo");
2570 hinfo
.dump(formatter
);
2571 formatter
->close_section();
2574 cerr
<< "Error decoding hinfo on : " << make_pair(coll
, ghobj
) << ", "
2575 << cpp_strerror(r
) << std::endl
;
2578 gr
= store
->dump_onode(ch
, ghobj
, "onode", formatter
);
2580 formatter
->close_section();
2581 formatter
->flush(cout
);
2586 int corrupt_info(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, Formatter
* formatter
)
2588 auto ch
= store
->open_collection(coll
);
2590 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2592 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2593 << cpp_strerror(r
) << std::endl
;
2597 auto bp
= attr
.cbegin();
2602 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2603 << cpp_strerror(r
) << std::endl
;
2608 oi
.alloc_hint_flags
+= 0xff;
2609 ObjectStore::Transaction t
;
2610 encode(oi
, attr
, -1); /* fixme: using full features */
2611 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2612 auto ch
= store
->open_collection(coll
);
2613 r
= store
->queue_transaction(ch
, std::move(t
));
2615 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2616 << cpp_strerror(r
) << std::endl
;
2624 ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, uint64_t setsize
, Formatter
* formatter
,
2627 auto ch
= store
->open_collection(coll
);
2628 if (ghobj
.hobj
.is_snapdir()) {
2629 cerr
<< "Can't set the size of a snapdir" << std::endl
;
2633 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2635 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2636 << cpp_strerror(r
) << std::endl
;
2640 auto bp
= attr
.cbegin();
2645 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2646 << cpp_strerror(r
) << std::endl
;
2650 r
= store
->stat(ch
, ghobj
, &st
, true);
2652 cerr
<< "Error stat on : " << make_pair(coll
, ghobj
) << ", "
2653 << cpp_strerror(r
) << std::endl
;
2655 ghobject_t
head(ghobj
);
2657 bool found_head
= true;
2658 map
<snapid_t
, uint64_t>::iterator csi
;
2659 bool is_snap
= ghobj
.hobj
.is_snap();
2661 head
.hobj
= head
.hobj
.get_head();
2662 r
= get_snapset(store
, coll
, head
, ss
, true);
2663 if (r
< 0 && r
!= -ENOENT
) {
2664 // Requested get_snapset() silent, so if not -ENOENT show error
2665 cerr
<< "Error getting snapset on : " << make_pair(coll
, head
) << ", "
2666 << cpp_strerror(r
) << std::endl
;
2670 head
.hobj
= head
.hobj
.get_snapdir();
2671 r
= get_snapset(store
, coll
, head
, ss
);
2678 csi
= ss
.clone_size
.find(ghobj
.hobj
.snap
);
2679 if (csi
== ss
.clone_size
.end()) {
2680 cerr
<< "SnapSet is missing clone_size for snap " << ghobj
.hobj
.snap
<< std::endl
;
2684 if ((uint64_t)st
.st_size
== setsize
&& oi
.size
== setsize
2685 && (!is_snap
|| csi
->second
== setsize
)) {
2686 cout
<< "Size of object is already " << setsize
<< std::endl
;
2689 cout
<< "Setting size to " << setsize
<< ", stat size " << st
.st_size
2690 << ", obj info size " << oi
.size
;
2692 cout
<< ", " << (found_head
? "head" : "snapdir")
2693 << " clone_size " << csi
->second
;
2694 csi
->second
= setsize
;
2700 ObjectStore::Transaction t
;
2701 // Only modify object info if we want to corrupt it
2702 if (!corrupt
&& (uint64_t)st
.st_size
!= setsize
) {
2703 t
.truncate(coll
, ghobj
, setsize
);
2704 // Changing objectstore size will invalidate data_digest, so clear it.
2705 oi
.clear_data_digest();
2707 encode(oi
, attr
, -1); /* fixme: using full features */
2708 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2710 bufferlist snapattr
;
2712 encode(ss
, snapattr
);
2713 t
.setattr(coll
, head
, SS_ATTR
, snapattr
);
2715 auto ch
= store
->open_collection(coll
);
2716 r
= store
->queue_transaction(ch
, std::move(t
));
2718 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2719 << cpp_strerror(r
) << std::endl
;
2726 int clear_data_digest(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
) {
2727 auto ch
= store
->open_collection(coll
);
2729 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2731 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2732 << cpp_strerror(r
) << std::endl
;
2736 auto bp
= attr
.cbegin();
2741 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2742 << cpp_strerror(r
) << std::endl
;
2747 oi
.clear_data_digest();
2748 encode(oi
, attr
, -1); /* fixme: using full features */
2749 ObjectStore::Transaction t
;
2750 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2751 auto ch
= store
->open_collection(coll
);
2752 r
= store
->queue_transaction(ch
, std::move(t
));
2754 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2755 << cpp_strerror(r
) << std::endl
;
2762 int clear_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
,
2766 int ret
= get_snapset(store
, coll
, ghobj
, ss
);
2770 // Use "corrupt" to clear entire SnapSet
2771 // Use "seq" to just corrupt SnapSet.seq
2772 if (arg
== "corrupt" || arg
== "seq")
2774 // Use "snaps" to just clear SnapSet.clone_snaps
2775 if (arg
== "corrupt" || arg
== "snaps")
2776 ss
.clone_snaps
.clear();
2777 // By default just clear clone, clone_overlap and clone_size
2778 if (arg
== "corrupt")
2780 if (arg
== "" || arg
== "clones")
2782 if (arg
== "" || arg
== "clone_overlap")
2783 ss
.clone_overlap
.clear();
2784 if (arg
== "" || arg
== "clone_size")
2785 ss
.clone_size
.clear();
2786 // Break all clone sizes by adding 1
2787 if (arg
== "size") {
2788 for (map
<snapid_t
, uint64_t>::iterator i
= ss
.clone_size
.begin();
2789 i
!= ss
.clone_size
.end(); ++i
)
2796 ObjectStore::Transaction t
;
2797 t
.setattr(coll
, ghobj
, SS_ATTR
, bl
);
2798 auto ch
= store
->open_collection(coll
);
2799 int r
= store
->queue_transaction(ch
, std::move(t
));
2801 cerr
<< "Error setting snapset on : " << make_pair(coll
, ghobj
) << ", "
2802 << cpp_strerror(r
) << std::endl
;
2809 vector
<snapid_t
>::iterator
find(vector
<snapid_t
> &v
, snapid_t clid
)
2811 return std::find(v
.begin(), v
.end(), clid
);
2814 map
<snapid_t
, interval_set
<uint64_t> >::iterator
2815 find(map
<snapid_t
, interval_set
<uint64_t> > &m
, snapid_t clid
)
2817 return m
.find(clid
);
2820 map
<snapid_t
, uint64_t>::iterator
find(map
<snapid_t
, uint64_t> &m
,
2823 return m
.find(clid
);
2827 int remove_from(T
&mv
, string name
, snapid_t cloneid
, bool force
)
2829 typename
T::iterator i
= find(mv
, cloneid
);
2830 if (i
!= mv
.end()) {
2833 cerr
<< "Clone " << cloneid
<< " doesn't exist in " << name
;
2835 cerr
<< " (ignored)" << std::endl
;
2845 ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, snapid_t cloneid
, bool force
)
2847 // XXX: Don't allow this if in a cache tier or former cache tier
2848 // bool allow_incomplete_clones() const {
2849 // return cache_mode != CACHEMODE_NONE || has_flag(FLAG_INCOMPLETE_CLONES);
2852 int ret
= get_snapset(store
, coll
, ghobj
, snapset
);
2856 // Derived from trim_object()
2858 vector
<snapid_t
>::iterator p
;
2859 for (p
= snapset
.clones
.begin(); p
!= snapset
.clones
.end(); ++p
)
2862 if (p
== snapset
.clones
.end()) {
2863 cerr
<< "Clone " << cloneid
<< " not present";
2866 if (p
!= snapset
.clones
.begin()) {
2867 // not the oldest... merge overlap into next older clone
2868 vector
<snapid_t
>::iterator n
= p
- 1;
2869 hobject_t prev_coid
= ghobj
.hobj
;
2870 prev_coid
.snap
= *n
;
2871 //bool adjust_prev_bytes = is_present_clone(prev_coid);
2873 //if (adjust_prev_bytes)
2874 // ctx->delta_stats.num_bytes -= snapset.get_clone_bytes(*n);
2876 snapset
.clone_overlap
[*n
].intersection_of(
2877 snapset
.clone_overlap
[*p
]);
2879 //if (adjust_prev_bytes)
2880 // ctx->delta_stats.num_bytes += snapset.get_clone_bytes(*n);
2883 ret
= remove_from(snapset
.clones
, "clones", cloneid
, force
);
2884 if (ret
) return ret
;
2885 ret
= remove_from(snapset
.clone_overlap
, "clone_overlap", cloneid
, force
);
2886 if (ret
) return ret
;
2887 ret
= remove_from(snapset
.clone_size
, "clone_size", cloneid
, force
);
2888 if (ret
) return ret
;
2894 encode(snapset
, bl
);
2895 ObjectStore::Transaction t
;
2896 t
.setattr(coll
, ghobj
, SS_ATTR
, bl
);
2897 auto ch
= store
->open_collection(coll
);
2898 int r
= store
->queue_transaction(ch
, std::move(t
));
2900 cerr
<< "Error setting snapset on : " << make_pair(coll
, ghobj
) << ", "
2901 << cpp_strerror(r
) << std::endl
;
2904 cout
<< "Removal of clone " << cloneid
<< " complete" << std::endl
;
2905 cout
<< "Use pg repair after OSD restarted to correct stat information" << std::endl
;
2909 int dup(string srcpath
, ObjectStore
*src
, string dstpath
, ObjectStore
*dst
)
2911 cout
<< "dup from " << src
->get_type() << ": " << srcpath
<< "\n"
2912 << " to " << dst
->get_type() << ": " << dstpath
2915 vector
<coll_t
> collections
;
2920 cerr
<< "failed to mount src: " << cpp_strerror(r
) << std::endl
;
2925 cerr
<< "failed to mount dst: " << cpp_strerror(r
) << std::endl
;
2929 if (src
->get_fsid() != dst
->get_fsid()) {
2930 cerr
<< "src fsid " << src
->get_fsid() << " != dest " << dst
->get_fsid()
2934 cout
<< "fsid " << src
->get_fsid() << std::endl
;
2936 // make sure dst is empty
2937 r
= dst
->list_collections(collections
);
2939 cerr
<< "error listing collections on dst: " << cpp_strerror(r
) << std::endl
;
2942 if (!collections
.empty()) {
2943 cerr
<< "destination store is not empty" << std::endl
;
2947 r
= src
->list_collections(collections
);
2949 cerr
<< "error listing collections on src: " << cpp_strerror(r
) << std::endl
;
2953 num
= collections
.size();
2954 cout
<< num
<< " collections" << std::endl
;
2956 for (auto cid
: collections
) {
2957 cout
<< i
++ << "/" << num
<< " " << cid
<< std::endl
;
2958 auto ch
= src
->open_collection(cid
);
2959 auto dch
= dst
->create_new_collection(cid
);
2961 ObjectStore::Transaction t
;
2962 int bits
= src
->collection_bits(ch
);
2964 if (src
->get_type() == "filestore" && cid
.is_meta()) {
2967 cerr
<< "cannot get bit count for collection " << cid
<< ": "
2968 << cpp_strerror(bits
) << std::endl
;
2972 t
.create_collection(cid
, bits
);
2973 dst
->queue_transaction(dch
, std::move(t
));
2978 uint64_t bytes
= 0, keys
= 0;
2980 vector
<ghobject_t
> ls
;
2981 r
= src
->collection_list(ch
, pos
, ghobject_t::get_max(), 1000, &ls
, &pos
);
2983 cerr
<< "collection_list on " << cid
<< " from " << pos
<< " got: "
2984 << cpp_strerror(r
) << std::endl
;
2991 for (auto& oid
: ls
) {
2992 //cout << " " << cid << " " << oid << std::endl;
2994 cout
<< " " << std::setw(16) << n
<< " objects, "
2995 << std::setw(16) << bytes
<< " bytes, "
2996 << std::setw(16) << keys
<< " keys"
2997 << std::setw(1) << "\r" << std::flush
;
3001 ObjectStore::Transaction t
;
3004 map
<string
,bufferptr
> attrs
;
3005 src
->getattrs(ch
, oid
, attrs
);
3006 if (!attrs
.empty()) {
3007 t
.setattrs(cid
, oid
, attrs
);
3011 src
->read(ch
, oid
, 0, 0, bl
);
3013 t
.write(cid
, oid
, 0, bl
.length(), bl
);
3014 bytes
+= bl
.length();
3018 map
<string
,bufferlist
> omap
;
3019 src
->omap_get(ch
, oid
, &header
, &omap
);
3020 if (header
.length()) {
3021 t
.omap_setheader(cid
, oid
, header
);
3024 if (!omap
.empty()) {
3025 keys
+= omap
.size();
3026 t
.omap_setkeys(cid
, oid
, omap
);
3029 dst
->queue_transaction(dch
, std::move(t
));
3032 cout
<< " " << std::setw(16) << n
<< " objects, "
3033 << std::setw(16) << bytes
<< " bytes, "
3034 << std::setw(16) << keys
<< " keys"
3035 << std::setw(1) << std::endl
;
3039 cout
<< "keyring" << std::endl
;
3042 string s
= srcpath
+ "/keyring";
3044 r
= bl
.read_file(s
.c_str(), &err
);
3046 cerr
<< "failed to copy " << s
<< ": " << err
<< std::endl
;
3048 string d
= dstpath
+ "/keyring";
3049 bl
.write_file(d
.c_str(), 0600);
3054 cout
<< "duping osd metadata" << std::endl
;
3056 for (auto k
: {"magic", "whoami", "ceph_fsid", "fsid"}) {
3058 src
->read_meta(k
, &val
);
3059 dst
->write_meta(k
, val
);
3063 dst
->write_meta("ready", "ready");
3065 cout
<< "done." << std::endl
;
3074 void usage(po::options_description
&desc
)
3077 cerr
<< desc
<< std::endl
;
3079 cerr
<< "Positional syntax:" << std::endl
;
3081 cerr
<< "ceph-objectstore-tool ... <object> (get|set)-bytes [file]" << std::endl
;
3082 cerr
<< "ceph-objectstore-tool ... <object> set-(attr|omap) <key> [file]" << std::endl
;
3083 cerr
<< "ceph-objectstore-tool ... <object> (get|rm)-(attr|omap) <key>" << std::endl
;
3084 cerr
<< "ceph-objectstore-tool ... <object> get-omaphdr" << std::endl
;
3085 cerr
<< "ceph-objectstore-tool ... <object> set-omaphdr [file]" << std::endl
;
3086 cerr
<< "ceph-objectstore-tool ... <object> list-attrs" << std::endl
;
3087 cerr
<< "ceph-objectstore-tool ... <object> list-omap" << std::endl
;
3088 cerr
<< "ceph-objectstore-tool ... <object> remove|removeall" << std::endl
;
3089 cerr
<< "ceph-objectstore-tool ... <object> dump" << std::endl
;
3090 cerr
<< "ceph-objectstore-tool ... <object> set-size" << std::endl
;
3091 cerr
<< "ceph-objectstore-tool ... <object> clear-data-digest" << std::endl
;
3092 cerr
<< "ceph-objectstore-tool ... <object> remove-clone-metadata <cloneid>" << std::endl
;
3094 cerr
<< "<object> can be a JSON object description as displayed" << std::endl
;
3095 cerr
<< "by --op list." << std::endl
;
3096 cerr
<< "<object> can be an object name which will be looked up in all" << std::endl
;
3097 cerr
<< "the OSD's PGs." << std::endl
;
3098 cerr
<< "<object> can be the empty string ('') which with a provided pgid " << std::endl
;
3099 cerr
<< "specifies the pgmeta object" << std::endl
;
3101 cerr
<< "The optional [file] argument will read stdin or write stdout" << std::endl
;
3102 cerr
<< "if not specified or if '-' specified." << std::endl
;
3105 bool ends_with(const string
& check
, const string
& ending
)
3107 return check
.size() >= ending
.size() && check
.rfind(ending
) == (check
.size() - ending
.size());
3110 // Based on FileStore::dump_journal(), set-up enough to only dump
3111 int mydump_journal(Formatter
*f
, string journalpath
, bool m_journal_dio
)
3115 if (!journalpath
.length())
3118 FileJournal
*journal
= new FileJournal(g_ceph_context
, uuid_d(), NULL
, NULL
,
3119 journalpath
.c_str(), m_journal_dio
);
3120 r
= journal
->_fdump(*f
, false);
3125 int apply_layout_settings(ObjectStore
*os
, const OSDSuperblock
&superblock
,
3126 const string
&pool_name
, const spg_t
&pgid
, bool dry_run
,
3131 FileStore
*fs
= dynamic_cast<FileStore
*>(os
);
3133 cerr
<< "Nothing to do for non-filestore backend" << std::endl
;
3134 return 0; // making this return success makes testing easier
3139 r
= get_osdmap(os
, superblock
.current_epoch
, curmap
, bl
);
3141 cerr
<< "Can't find local OSDMap: " << cpp_strerror(r
) << std::endl
;
3145 int64_t poolid
= -1;
3146 if (pool_name
.length()) {
3147 poolid
= curmap
.lookup_pg_pool_name(pool_name
);
3149 cerr
<< "Couldn't find pool " << pool_name
<< ": " << cpp_strerror(poolid
)
3155 vector
<coll_t
> collections
, filtered_colls
;
3156 r
= os
->list_collections(collections
);
3158 cerr
<< "Error listing collections: " << cpp_strerror(r
) << std::endl
;
3162 for (auto const &coll
: collections
) {
3164 if (coll
.is_pg(&coll_pgid
) &&
3165 ((poolid
>= 0 && coll_pgid
.pool() == (uint64_t)poolid
) ||
3166 coll_pgid
== pgid
)) {
3167 filtered_colls
.push_back(coll
);
3171 size_t done
= 0, total
= filtered_colls
.size();
3172 for (auto const &coll
: filtered_colls
) {
3174 cerr
<< "Would apply layout settings to " << coll
<< std::endl
;
3176 cerr
<< "Finished " << done
<< "/" << total
<< " collections" << "\r";
3177 r
= fs
->apply_layout_settings(coll
, target_level
);
3179 cerr
<< "Error applying layout settings to " << coll
<< std::endl
;
3186 cerr
<< "Finished " << total
<< "/" << total
<< " collections" << "\r" << std::endl
;
3190 int main(int argc
, char **argv
)
3192 string dpath
, jpath
, pgidstr
, op
, file
, mountpoint
, mon_store_path
, object
;
3193 string target_data_path
, fsid
;
3194 string objcmd
, arg1
, arg2
, type
, format
, argnspace
, pool
, rmtypestr
;
3195 boost::optional
<std::string
> nspace
;
3198 unsigned slow_threshold
= 16;
3200 bool human_readable
;
3201 Formatter
*formatter
;
3204 po::options_description
desc("Allowed options");
3206 ("help", "produce help message")
3207 ("type", po::value
<string
>(&type
),
3208 "Arg is one of [bluestore (default), filestore, memstore]")
3209 ("data-path", po::value
<string
>(&dpath
),
3210 "path to object store, mandatory")
3211 ("journal-path", po::value
<string
>(&jpath
),
3212 "path to journal, use if tool can't find it")
3213 ("pgid", po::value
<string
>(&pgidstr
),
3214 "PG id, mandatory for info, log, remove, export, export-remove, mark-complete, trim-pg-log, and mandatory for apply-layout-settings if --pool is not specified")
3215 ("pool", po::value
<string
>(&pool
),
3216 "Pool name, mandatory for apply-layout-settings if --pgid is not specified")
3217 ("op", po::value
<string
>(&op
),
3218 "Arg is one of [info, log, remove, mkfs, fsck, repair, fuse, dup, export, export-remove, import, list, list-slow-omap, fix-lost, list-pgs, dump-journal, dump-super, meta-list, "
3219 "get-osdmap, set-osdmap, get-inc-osdmap, set-inc-osdmap, mark-complete, reset-last-complete, apply-layout-settings, update-mon-db, dump-export, trim-pg-log, statfs]")
3220 ("epoch", po::value
<unsigned>(&epoch
),
3221 "epoch# for get-osdmap and get-inc-osdmap, the current epoch in use if not specified")
3222 ("file", po::value
<string
>(&file
),
3223 "path of file to export, export-remove, import, get-osdmap, set-osdmap, get-inc-osdmap or set-inc-osdmap")
3224 ("mon-store-path", po::value
<string
>(&mon_store_path
),
3225 "path of monstore to update-mon-db")
3226 ("fsid", po::value
<string
>(&fsid
),
3227 "fsid for new store created by mkfs")
3228 ("target-data-path", po::value
<string
>(&target_data_path
),
3229 "path of target object store (for --op dup)")
3230 ("mountpoint", po::value
<string
>(&mountpoint
),
3232 ("format", po::value
<string
>(&format
)->default_value("json-pretty"),
3233 "Output format which may be json, json-pretty, xml, xml-pretty")
3234 ("debug", "Enable diagnostic output to stderr")
3235 ("no-mon-config", "Do not contact mons for config")
3236 ("no-superblock", "Do not read superblock")
3237 ("force", "Ignore some types of errors and proceed with operation - USE WITH CAUTION: CORRUPTION POSSIBLE NOW OR IN THE FUTURE")
3238 ("skip-journal-replay", "Disable journal replay")
3239 ("skip-mount-omap", "Disable mounting of omap")
3240 ("head", "Find head/snapdir when searching for objects by name")
3241 ("dry-run", "Don't modify the objectstore")
3242 ("tty", "Treat stdout as a tty (no binary data)")
3243 ("namespace", po::value
<string
>(&argnspace
), "Specify namespace when searching for objects")
3244 ("rmtype", po::value
<string
>(&rmtypestr
), "Specify corrupting object removal 'snapmap' or 'nosnapmap' - TESTING USE ONLY")
3245 ("slow-omap-threshold", po::value
<unsigned>(&slow_threshold
),
3246 "Threshold (in seconds) to consider omap listing slow (for op=list-slow-omap)")
3249 po::options_description
positional("Positional options");
3250 positional
.add_options()
3251 ("object", po::value
<string
>(&object
), "'' for pgmeta_oid, object name or ghobject in json")
3252 ("objcmd", po::value
<string
>(&objcmd
), "command [(get|set)-bytes, (get|set|rm)-(attr|omap), (get|set)-omaphdr, list-attrs, list-omap, remove]")
3253 ("arg1", po::value
<string
>(&arg1
), "arg1 based on cmd")
3254 ("arg2", po::value
<string
>(&arg2
), "arg2 based on cmd")
3257 po::options_description all
;
3258 all
.add(desc
).add(positional
);
3260 po::positional_options_description pd
;
3261 pd
.add("object", 1).add("objcmd", 1).add("arg1", 1).add("arg2", 1);
3263 vector
<string
> ceph_option_strings
;
3265 po::variables_map vm
;
3267 po::parsed_options parsed
=
3268 po::command_line_parser(argc
, argv
).options(all
).allow_unregistered().positional(pd
).run();
3269 po::store( parsed
, vm
);
3271 ceph_option_strings
= po::collect_unrecognized(parsed
.options
,
3272 po::include_positional
);
3273 } catch(po::error
&e
) {
3274 std::cerr
<< e
.what() << std::endl
;
3278 if (vm
.count("help")) {
3283 // Compatibility with previous option name
3284 if (op
== "dump-import")
3287 debug
= (vm
.count("debug") > 0);
3289 force
= (vm
.count("force") > 0);
3291 no_superblock
= (vm
.count("no-superblock") > 0);
3293 if (vm
.count("namespace"))
3296 dry_run
= (vm
.count("dry-run") > 0);
3297 tty
= (vm
.count("tty") > 0);
3299 osflagbits_t flags
= 0;
3300 if (dry_run
|| vm
.count("skip-journal-replay"))
3301 flags
|= SKIP_JOURNAL_REPLAY
;
3302 if (vm
.count("skip-mount-omap"))
3303 flags
|= SKIP_MOUNT_OMAP
;
3304 if (op
== "update-mon-db")
3305 flags
|= SKIP_JOURNAL_REPLAY
;
3307 head
= (vm
.count("head") > 0);
3309 // infer osd id so we can authenticate
3311 snprintf(fn
, sizeof(fn
), "%s/whoami", dpath
.c_str());
3312 int fd
= ::open(fn
, O_RDONLY
);
3316 string
s(bl
.c_str(), bl
.length());
3317 int whoami
= atoi(s
.c_str());
3319 // identify ourselves as this osd so we can auth and fetch our configs
3320 tmp
.push_back("-n");
3321 tmp
.push_back(string("osd.") + stringify(whoami
));
3322 // populate osd_data so that the default keyring location works
3323 tmp
.push_back("--osd-data");
3324 tmp
.push_back(dpath
);
3325 tmp
.insert(tmp
.end(), ceph_option_strings
.begin(),
3326 ceph_option_strings
.end());
3327 tmp
.swap(ceph_option_strings
);
3330 vector
<const char *> ceph_options
;
3331 ceph_options
.reserve(ceph_options
.size() + ceph_option_strings
.size());
3332 for (vector
<string
>::iterator i
= ceph_option_strings
.begin();
3333 i
!= ceph_option_strings
.end();
3335 ceph_options
.push_back(i
->c_str());
3338 snprintf(fn
, sizeof(fn
), "%s/type", dpath
.c_str());
3339 fd
= ::open(fn
, O_RDONLY
);
3344 string dp_type
= string(bl
.c_str(), bl
.length() - 1); // drop \n
3345 if (vm
.count("type") && dp_type
!= "" && type
!= dp_type
)
3346 cerr
<< "WARNING: Ignoring type \"" << type
<< "\" - found data-path type \""
3347 << dp_type
<< "\"" << std::endl
;
3349 //cout << "object store type is " << type << std::endl;
3354 if (!vm
.count("type") && type
== "") {
3357 if (!vm
.count("data-path") &&
3358 op
!= "dump-export" &&
3359 !(op
== "dump-journal" && type
== "filestore")) {
3360 cerr
<< "Must provide --data-path" << std::endl
;
3364 if (type
== "filestore" && !vm
.count("journal-path")) {
3365 jpath
= dpath
+ "/journal";
3367 if (!vm
.count("op") && !vm
.count("object")) {
3368 cerr
<< "Must provide --op or object command..." << std::endl
;
3372 if (op
!= "list" && op
!= "apply-layout-settings" &&
3373 vm
.count("op") && vm
.count("object")) {
3374 cerr
<< "Can't specify both --op and object command syntax" << std::endl
;
3378 if (op
== "apply-layout-settings" && !(vm
.count("pool") ^ vm
.count("pgid"))) {
3379 cerr
<< "apply-layout-settings requires either --pool or --pgid"
3384 if (op
!= "list" && op
!= "apply-layout-settings" && vm
.count("object") && !vm
.count("objcmd")) {
3385 cerr
<< "Invalid syntax, missing command" << std::endl
;
3389 if (op
== "fuse" && mountpoint
.length() == 0) {
3390 cerr
<< "Missing fuse mountpoint" << std::endl
;
3394 outistty
= isatty(STDOUT_FILENO
) || tty
;
3397 if ((op
== "export" || op
== "export-remove" || op
== "get-osdmap" || op
== "get-inc-osdmap") && !dry_run
) {
3398 if (!vm
.count("file") || file
== "-") {
3400 cerr
<< "stdout is a tty and no --file filename specified" << std::endl
;
3403 file_fd
= STDOUT_FILENO
;
3405 file_fd
= open(file
.c_str(), O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
3407 } else if (op
== "import" || op
== "dump-export" || op
== "set-osdmap" || op
== "set-inc-osdmap") {
3408 if (!vm
.count("file") || file
== "-") {
3409 if (isatty(STDIN_FILENO
)) {
3410 cerr
<< "stdin is a tty and no --file filename specified" << std::endl
;
3413 file_fd
= STDIN_FILENO
;
3415 file_fd
= open(file
.c_str(), O_RDONLY
);
3419 ObjectStoreTool tool
= ObjectStoreTool(file_fd
, dry_run
);
3421 if (vm
.count("file") && file_fd
== fd_none
&& !dry_run
) {
3422 cerr
<< "--file option only applies to import, dump-export, export, export-remove, "
3423 << "get-osdmap, set-osdmap, get-inc-osdmap or set-inc-osdmap" << std::endl
;
3427 if (file_fd
!= fd_none
&& file_fd
< 0) {
3428 string err
= string("file: ") + file
;
3429 perror(err
.c_str());
3433 if (vm
.count("no-mon-config") > 0) {
3434 init_flags
|= CINIT_FLAG_NO_MON_CONFIG
;
3437 auto cct
= global_init(
3439 CEPH_ENTITY_TYPE_OSD
,
3440 CODE_ENVIRONMENT_UTILITY_NODOUT
,
3442 common_init_finish(g_ceph_context
);
3444 g_conf().set_val_or_die("log_to_stderr", "true");
3445 g_conf().set_val_or_die("err_to_stderr", "true");
3447 g_conf().apply_changes(nullptr);
3449 // Special list handling. Treating pretty_format as human readable,
3450 // with one object per line and not an enclosing array.
3451 human_readable
= ends_with(format
, "-pretty");
3452 if ((op
== "list" || op
== "meta-list") && human_readable
) {
3453 // Remove -pretty from end of format which we know is there
3454 format
= format
.substr(0, format
.size() - strlen("-pretty"));
3457 formatter
= Formatter::create(format
);
3458 if (formatter
== NULL
) {
3459 cerr
<< "unrecognized format: " << format
<< std::endl
;
3463 // Special handling for filestore journal, so we can dump it without mounting
3464 if (op
== "dump-journal" && type
== "filestore") {
3465 int ret
= mydump_journal(formatter
, jpath
, g_conf()->journal_dio
);
3467 cerr
<< "journal-path: " << jpath
<< ": "
3468 << cpp_strerror(ret
) << std::endl
;
3471 formatter
->flush(cout
);
3475 if (op
== "dump-export") {
3476 int ret
= tool
.dump_export(formatter
);
3478 cerr
<< "dump-export: "
3479 << cpp_strerror(ret
) << std::endl
;
3485 //Verify that data-path really exists
3487 if (::stat(dpath
.c_str(), &st
) == -1) {
3488 string err
= string("data-path: ") + dpath
;
3489 perror(err
.c_str());
3493 if (pgidstr
.length() && !pgid
.parse(pgidstr
.c_str())) {
3494 cerr
<< "Invalid pgid '" << pgidstr
<< "' specified" << std::endl
;
3498 //Verify that the journal-path really exists
3499 if (type
== "filestore") {
3500 if (::stat(jpath
.c_str(), &st
) == -1) {
3501 string err
= string("journal-path: ") + jpath
;
3502 perror(err
.c_str());
3505 if (S_ISDIR(st
.st_mode
)) {
3506 cerr
<< "journal-path: " << jpath
<< ": "
3507 << cpp_strerror(EISDIR
) << std::endl
;
3512 ObjectStore
*fs
= ObjectStore::create(g_ceph_context
, type
, dpath
, jpath
, flags
);
3514 cerr
<< "Unable to create store of type " << type
<< std::endl
;
3518 if (op
== "fsck" || op
== "fsck-deep") {
3519 int r
= fs
->fsck(op
== "fsck-deep");
3521 cerr
<< "fsck failed: " << cpp_strerror(r
) << std::endl
;
3525 cerr
<< "fsck status: " << r
<< " remaining error(s) and warning(s)" << std::endl
;
3528 cout
<< "fsck success" << std::endl
;
3531 if (op
== "repair" || op
== "repair-deep") {
3532 int r
= fs
->repair(op
== "repair-deep");
3534 cerr
<< "repair failed: " << cpp_strerror(r
) << std::endl
;
3538 cerr
<< "repair status: " << r
<< " remaining error(s) and warning(s)" << std::endl
;
3541 cout
<< "repair success" << std::endl
;
3545 if (fsid
.length()) {
3547 bool r
= f
.parse(fsid
.c_str());
3549 cerr
<< "failed to parse uuid '" << fsid
<< "'" << std::endl
;
3556 cerr
<< "mkfs failed: " << cpp_strerror(r
) << std::endl
;
3564 snprintf(fn
, sizeof(fn
), "%s/type", target_data_path
.c_str());
3565 int fd
= ::open(fn
, O_RDONLY
);
3567 cerr
<< "Unable to open " << target_data_path
<< "/type" << std::endl
;
3573 target_type
= string(bl
.c_str(), bl
.length() - 1); // drop \n
3576 ObjectStore
*targetfs
= ObjectStore::create(
3577 g_ceph_context
, target_type
,
3578 target_data_path
, "", 0);
3579 if (targetfs
== NULL
) {
3580 cerr
<< "Unable to open store of type " << target_type
<< std::endl
;
3583 int r
= dup(dpath
, fs
, target_data_path
, targetfs
);
3585 cerr
<< "dup failed: " << cpp_strerror(r
) << std::endl
;
3591 int ret
= fs
->mount();
3593 if (ret
== -EBUSY
) {
3594 cerr
<< "OSD has the store locked" << std::endl
;
3596 cerr
<< "Mount failed with '" << cpp_strerror(ret
) << "'" << std::endl
;
3603 FuseStore
fuse(fs
, mountpoint
);
3604 cout
<< "mounting fuse at " << mountpoint
<< " ..." << std::endl
;
3605 int r
= fuse
.main();
3607 cerr
<< "failed to mount fuse: " << cpp_strerror(r
) << std::endl
;
3611 cerr
<< "fuse support not enabled" << std::endl
;
3617 vector
<coll_t
>::iterator it
;
3618 CompatSet supported
;
3620 #ifdef INTERNAL_TEST
3621 supported
= get_test_compat_set();
3623 supported
= OSD::get_osd_compat_set();
3627 auto ch
= fs
->open_collection(coll_t::meta());
3628 std::unique_ptr
<OSDSuperblock
> superblock
;
3629 if (!no_superblock
) {
3630 superblock
.reset(new OSDSuperblock
);
3631 bufferlist::const_iterator p
;
3632 ret
= fs
->read(ch
, OSD_SUPERBLOCK_GOBJECT
, 0, 0, bl
);
3634 cerr
<< "Failure to read OSD superblock: " << cpp_strerror(ret
) << std::endl
;
3639 decode(*superblock
, p
);
3642 cerr
<< "Cluster fsid=" << superblock
->cluster_fsid
<< std::endl
;
3646 cerr
<< "Supported features: " << supported
<< std::endl
;
3647 cerr
<< "On-disk features: " << superblock
->compat_features
<< std::endl
;
3649 if (supported
.compare(superblock
->compat_features
) == -1) {
3650 CompatSet unsupported
= supported
.unsupported(superblock
->compat_features
);
3651 cerr
<< "On-disk OSD incompatible features set "
3652 << unsupported
<< std::endl
;
3658 if (op
== "apply-layout-settings") {
3659 int target_level
= 0;
3660 // Single positional argument with apply-layout-settings
3661 // for target_level.
3662 if (vm
.count("object") && isdigit(object
[0])) {
3663 target_level
= atoi(object
.c_str());
3664 // This requires --arg1 to be specified since
3665 // this is the third positional argument and normally
3666 // used with object operations.
3667 } else if (vm
.count("arg1") && isdigit(arg1
[0])) {
3668 target_level
= atoi(arg1
.c_str());
3670 ceph_assert(superblock
!= nullptr);
3671 ret
= apply_layout_settings(fs
, *superblock
, pool
, pgid
, dry_run
, target_level
);
3675 if (op
!= "list" && vm
.count("object")) {
3676 // Special case: Create pgmeta_oid if empty string specified
3677 // This can't conflict with any actual object names.
3679 ghobj
= pgid
.make_pgmeta_oid();
3681 json_spirit::Value v
;
3683 if (!json_spirit::read(object
, v
) ||
3684 (v
.type() != json_spirit::array_type
&& v
.type() != json_spirit::obj_type
)) {
3685 // Special: Need head/snapdir so set even if user didn't specify
3686 if (vm
.count("objcmd") && (objcmd
== "remove-clone-metadata"))
3688 lookup_ghobject
lookup(object
, nspace
, head
);
3689 if (pgidstr
.length())
3690 ret
= action_on_all_objects_in_exact_pg(fs
, coll_t(pgid
), lookup
, debug
);
3692 ret
= action_on_all_objects(fs
, lookup
, debug
);
3694 throw std::runtime_error("Internal error");
3696 if (lookup
.size() != 1) {
3698 if (lookup
.size() == 0)
3699 ss
<< "No object id '" << object
<< "' found or invalid JSON specified";
3701 ss
<< "Found " << lookup
.size() << " objects with id '" << object
3702 << "', please use a JSON spec from --op list instead";
3703 throw std::runtime_error(ss
.str());
3705 pair
<coll_t
, ghobject_t
> found
= lookup
.pop();
3706 pgidstr
= found
.first
.to_str();
3707 pgid
.parse(pgidstr
.c_str());
3708 ghobj
= found
.second
;
3712 if (pgidstr
.length() == 0 && v
.type() != json_spirit::array_type
) {
3713 ss
<< "Without --pgid the object '" << object
3714 << "' must be a JSON array";
3715 throw std::runtime_error(ss
.str());
3717 if (v
.type() == json_spirit::array_type
) {
3718 json_spirit::Array array
= v
.get_array();
3719 if (array
.size() != 2) {
3720 ss
<< "Object '" << object
3721 << "' must be a JSON array with 2 elements";
3722 throw std::runtime_error(ss
.str());
3724 vector
<json_spirit::Value
>::iterator i
= array
.begin();
3725 ceph_assert(i
!= array
.end());
3726 if (i
->type() != json_spirit::str_type
) {
3727 ss
<< "Object '" << object
3728 << "' must be a JSON array with the first element a string";
3729 throw std::runtime_error(ss
.str());
3731 string object_pgidstr
= i
->get_str();
3732 if (object_pgidstr
!= "meta") {
3734 object_pgid
.parse(object_pgidstr
.c_str());
3735 if (pgidstr
.length() > 0) {
3736 if (object_pgid
!= pgid
) {
3737 ss
<< "object '" << object
3738 << "' has a pgid different from the --pgid="
3739 << pgidstr
<< " option";
3740 throw std::runtime_error(ss
.str());
3743 pgidstr
= object_pgidstr
;
3747 pgidstr
= object_pgidstr
;
3754 } catch (std::runtime_error
& e
) {
3755 ss
<< "Decode object JSON error: " << e
.what();
3756 throw std::runtime_error(ss
.str());
3758 if (pgidstr
!= "meta" && (uint64_t)pgid
.pgid
.m_pool
!= (uint64_t)ghobj
.hobj
.pool
) {
3759 cerr
<< "Object pool and pgid pool don't match" << std::endl
;
3763 if (pgidstr
!= "meta") {
3764 auto ch
= fs
->open_collection(coll_t(pgid
));
3765 if (!ghobj
.match(fs
->collection_bits(ch
), pgid
.ps())) {
3767 ss
<< "object " << ghobj
<< " not contained by pg " << pgid
;
3768 throw std::runtime_error(ss
.str());
3772 } catch (std::runtime_error
& e
) {
3773 cerr
<< e
.what() << std::endl
;
3780 // The ops which require --pgid option are checked here and
3781 // mentioned in the usage for --pgid.
3782 if ((op
== "info" || op
== "log" || op
== "remove" || op
== "export"
3783 || op
== "export-remove" || op
== "mark-complete"
3784 || op
== "reset-last-complete"
3785 || op
== "trim-pg-log") &&
3786 pgidstr
.length() == 0) {
3787 cerr
<< "Must provide pgid" << std::endl
;
3793 if (op
== "import") {
3794 ceph_assert(superblock
!= nullptr);
3796 ret
= tool
.do_import(fs
, *superblock
, force
, pgidstr
);
3798 catch (const buffer::error
&e
) {
3799 cerr
<< "do_import threw exception error " << e
.what() << std::endl
;
3802 if (ret
== -EFAULT
) {
3803 cerr
<< "Corrupt input for import" << std::endl
;
3806 cout
<< "Import successful" << std::endl
;
3808 } else if (op
== "dump-journal-mount") {
3809 // Undocumented feature to dump journal with mounted fs
3810 // This doesn't support the format option, but it uses the
3811 // ObjectStore::dump_journal() and mounts to get replay to run.
3812 ret
= fs
->dump_journal(cout
);
3814 if (ret
== -EOPNOTSUPP
) {
3815 cerr
<< "Object store type \"" << type
<< "\" doesn't support journal dump" << std::endl
;
3817 cerr
<< "Journal dump failed with error " << cpp_strerror(ret
) << std::endl
;
3821 } else if (op
== "get-osdmap") {
3825 ceph_assert(superblock
!= nullptr);
3826 epoch
= superblock
->current_epoch
;
3828 ret
= get_osdmap(fs
, epoch
, osdmap
, bl
);
3830 cerr
<< "Failed to get osdmap#" << epoch
<< ": "
3831 << cpp_strerror(ret
) << std::endl
;
3834 ret
= bl
.write_fd(file_fd
);
3836 cerr
<< "Failed to write to " << file
<< ": " << cpp_strerror(ret
) << std::endl
;
3838 cout
<< "osdmap#" << epoch
<< " exported." << std::endl
;
3841 } else if (op
== "set-osdmap") {
3843 ret
= get_fd_data(file_fd
, bl
);
3845 cerr
<< "Failed to read osdmap " << cpp_strerror(ret
) << std::endl
;
3847 ret
= set_osdmap(fs
, epoch
, bl
, force
);
3850 } else if (op
== "get-inc-osdmap") {
3853 ceph_assert(superblock
!= nullptr);
3854 epoch
= superblock
->current_epoch
;
3856 ret
= get_inc_osdmap(fs
, epoch
, bl
);
3858 cerr
<< "Failed to get incremental osdmap# " << epoch
<< ": "
3859 << cpp_strerror(ret
) << std::endl
;
3862 ret
= bl
.write_fd(file_fd
);
3864 cerr
<< "Failed to write to " << file
<< ": " << cpp_strerror(ret
) << std::endl
;
3866 cout
<< "inc-osdmap#" << epoch
<< " exported." << std::endl
;
3869 } else if (op
== "set-inc-osdmap") {
3871 ret
= get_fd_data(file_fd
, bl
);
3873 cerr
<< "Failed to read incremental osdmap " << cpp_strerror(ret
) << std::endl
;
3876 ret
= set_inc_osdmap(fs
, epoch
, bl
, force
);
3879 } else if (op
== "update-mon-db") {
3880 if (!vm
.count("mon-store-path")) {
3881 cerr
<< "Please specify the path to monitor db to update" << std::endl
;
3884 ceph_assert(superblock
!= nullptr);
3885 ret
= update_mon_db(*fs
, *superblock
, dpath
+ "/keyring", mon_store_path
);
3890 if (op
== "remove") {
3891 if (!force
&& !dry_run
) {
3892 cerr
<< "Please use export-remove or you must use --force option" << std::endl
;
3896 ret
= initiate_new_remove_pg(fs
, pgid
);
3898 cerr
<< "PG '" << pgid
<< "' not found" << std::endl
;
3901 cout
<< "Remove successful" << std::endl
;
3905 if (op
== "fix-lost") {
3906 boost::scoped_ptr
<action_on_object_t
> action
;
3907 action
.reset(new do_fix_lost());
3908 if (pgidstr
.length())
3909 ret
= action_on_all_objects_in_exact_pg(fs
, coll_t(pgid
), *action
, debug
);
3911 ret
= action_on_all_objects(fs
, *action
, debug
);
3916 ret
= do_list(fs
, pgidstr
, object
, nspace
, formatter
, debug
,
3917 human_readable
, head
);
3919 cerr
<< "do_list failed: " << cpp_strerror(ret
) << std::endl
;
3923 if (op
== "list-slow-omap") {
3924 ret
= do_list_slow(fs
, pgidstr
, object
, slow_threshold
, formatter
, debug
,
3927 cerr
<< "do_list failed: " << cpp_strerror(ret
) << std::endl
;
3932 if (op
== "dump-super") {
3933 ceph_assert(superblock
!= nullptr);
3934 formatter
->open_object_section("superblock");
3935 superblock
->dump(formatter
);
3936 formatter
->close_section();
3937 formatter
->flush(cout
);
3942 if (op
== "statfs") {
3943 store_statfs_t statsbuf
;
3944 ret
= fs
->statfs(&statsbuf
);
3946 cerr
<< "error from statfs: " << cpp_strerror(ret
) << std::endl
;
3949 formatter
->open_object_section("statfs");
3950 statsbuf
.dump(formatter
);
3951 formatter
->close_section();
3952 formatter
->flush(cout
);
3957 if (op
== "meta-list") {
3958 ret
= do_meta(fs
, object
, formatter
, debug
, human_readable
);
3960 cerr
<< "do_meta failed: " << cpp_strerror(ret
) << std::endl
;
3965 ret
= fs
->list_collections(ls
);
3967 cerr
<< "failed to list pgs: " << cpp_strerror(ret
) << std::endl
;
3971 if (debug
&& op
== "list-pgs")
3972 cout
<< "Performing list-pgs operation" << std::endl
;
3975 for (it
= ls
.begin(); it
!= ls
.end(); ++it
) {
3978 if (pgidstr
== "meta") {
3979 if (it
->to_str() == "meta")
3985 if (!it
->is_pg(&tmppgid
)) {
3989 if (it
->is_temp(&tmppgid
)) {
3993 if (op
!= "list-pgs" && tmppgid
!= pgid
) {
3997 if (op
!= "list-pgs") {
4002 cout
<< tmppgid
<< std::endl
;
4005 if (op
== "list-pgs") {
4010 // If not an object command nor any of the ops handled below, then output this usage
4011 // before complaining about a bad pgid
4012 if (!vm
.count("objcmd") && op
!= "export" && op
!= "export-remove" && op
!= "info" && op
!= "log" && op
!= "mark-complete" && op
!= "trim-pg-log") {
4013 cerr
<< "Must provide --op (info, log, remove, mkfs, fsck, repair, export, export-remove, import, list, fix-lost, list-pgs, dump-journal, dump-super, meta-list, "
4014 "get-osdmap, set-osdmap, get-inc-osdmap, set-inc-osdmap, mark-complete, reset-last-complete, dump-export, trim-pg-log, statfs)"
4021 // The following code for export, info, log require omap or !skip-mount-omap
4022 if (it
!= ls
.end()) {
4026 if (vm
.count("objcmd")) {
4028 if (objcmd
== "remove" || objcmd
== "removeall") {
4029 bool all
= (objcmd
== "removeall");
4030 enum rmtype type
= BOTH
;
4031 if (rmtypestr
== "nosnapmap")
4033 else if (rmtypestr
== "snapmap")
4035 ret
= do_remove_object(fs
, coll
, ghobj
, all
, force
, type
);
4037 } else if (objcmd
== "list-attrs") {
4038 ret
= do_list_attrs(fs
, coll
, ghobj
);
4040 } else if (objcmd
== "list-omap") {
4041 ret
= do_list_omap(fs
, coll
, ghobj
);
4043 } else if (objcmd
== "get-bytes" || objcmd
== "set-bytes") {
4044 if (objcmd
== "get-bytes") {
4046 if (vm
.count("arg1") == 0 || arg1
== "-") {
4049 fd
= open(arg1
.c_str(), O_WRONLY
|O_TRUNC
|O_CREAT
|O_EXCL
|O_LARGEFILE
, 0666);
4051 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4056 ret
= do_get_bytes(fs
, coll
, ghobj
, fd
);
4057 if (fd
!= STDOUT_FILENO
)
4061 if (vm
.count("arg1") == 0 || arg1
== "-") {
4062 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4063 if (isatty(STDIN_FILENO
)) {
4064 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4070 fd
= open(arg1
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4072 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4077 ret
= do_set_bytes(fs
, coll
, ghobj
, fd
);
4078 if (fd
!= STDIN_FILENO
)
4082 } else if (objcmd
== "get-attr") {
4083 if (vm
.count("arg1") == 0) {
4088 ret
= do_get_attr(fs
, coll
, ghobj
, arg1
);
4090 } else if (objcmd
== "set-attr") {
4091 if (vm
.count("arg1") == 0) {
4097 if (vm
.count("arg2") == 0 || arg2
== "-") {
4098 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4099 if (isatty(STDIN_FILENO
)) {
4100 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4106 fd
= open(arg2
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4108 cerr
<< "open " << arg2
<< " " << cpp_strerror(errno
) << std::endl
;
4113 ret
= do_set_attr(fs
, coll
, ghobj
, arg1
, fd
);
4114 if (fd
!= STDIN_FILENO
)
4117 } else if (objcmd
== "rm-attr") {
4118 if (vm
.count("arg1") == 0) {
4123 ret
= do_rm_attr(fs
, coll
, ghobj
, arg1
);
4125 } else if (objcmd
== "get-omap") {
4126 if (vm
.count("arg1") == 0) {
4131 ret
= do_get_omap(fs
, coll
, ghobj
, arg1
);
4133 } else if (objcmd
== "set-omap") {
4134 if (vm
.count("arg1") == 0) {
4140 if (vm
.count("arg2") == 0 || arg2
== "-") {
4141 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4142 if (isatty(STDIN_FILENO
)) {
4143 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4149 fd
= open(arg2
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4151 cerr
<< "open " << arg2
<< " " << cpp_strerror(errno
) << std::endl
;
4156 ret
= do_set_omap(fs
, coll
, ghobj
, arg1
, fd
);
4157 if (fd
!= STDIN_FILENO
)
4160 } else if (objcmd
== "rm-omap") {
4161 if (vm
.count("arg1") == 0) {
4166 ret
= do_rm_omap(fs
, coll
, ghobj
, arg1
);
4168 } else if (objcmd
== "get-omaphdr") {
4169 if (vm
.count("arg1")) {
4174 ret
= do_get_omaphdr(fs
, coll
, ghobj
);
4176 } else if (objcmd
== "set-omaphdr") {
4178 if (vm
.count("arg2")) {
4184 if (vm
.count("arg1") == 0 || arg1
== "-") {
4185 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4186 if (isatty(STDIN_FILENO
)) {
4187 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4193 fd
= open(arg1
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4195 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4200 ret
= do_set_omaphdr(fs
, coll
, ghobj
, fd
);
4201 if (fd
!= STDIN_FILENO
)
4204 } else if (objcmd
== "dump") {
4205 // There should not be any other arguments
4206 if (vm
.count("arg1") || vm
.count("arg2")) {
4211 ret
= print_obj_info(fs
, coll
, ghobj
, formatter
);
4213 } else if (objcmd
== "corrupt-info") { // Undocumented testing feature
4214 // There should not be any other arguments
4215 if (vm
.count("arg1") || vm
.count("arg2")) {
4220 ret
= corrupt_info(fs
, coll
, ghobj
, formatter
);
4222 } else if (objcmd
== "set-size" || objcmd
== "corrupt-size") {
4223 // Undocumented testing feature
4224 bool corrupt
= (objcmd
== "corrupt-size");
4226 if (vm
.count("arg1") == 0 || vm
.count("arg2")) {
4231 if (arg1
.length() == 0 || !isdigit(arg1
.c_str()[0])) {
4232 cerr
<< "Invalid size '" << arg1
<< "' specified" << std::endl
;
4236 uint64_t size
= atoll(arg1
.c_str());
4237 ret
= set_size(fs
, coll
, ghobj
, size
, formatter
, corrupt
);
4239 } else if (objcmd
== "clear-data-digest") {
4240 ret
= clear_data_digest(fs
, coll
, ghobj
);
4242 } else if (objcmd
== "clear-snapset") {
4243 // UNDOCUMENTED: For testing zap SnapSet
4244 // IGNORE extra args since not in usage anyway
4245 if (!ghobj
.hobj
.has_snapset()) {
4246 cerr
<< "'" << objcmd
<< "' requires a head or snapdir object" << std::endl
;
4250 ret
= clear_snapset(fs
, coll
, ghobj
, arg1
);
4252 } else if (objcmd
== "remove-clone-metadata") {
4254 if (vm
.count("arg1") == 0 || vm
.count("arg2")) {
4259 if (!ghobj
.hobj
.has_snapset()) {
4260 cerr
<< "'" << objcmd
<< "' requires a head or snapdir object" << std::endl
;
4264 if (arg1
.length() == 0 || !isdigit(arg1
.c_str()[0])) {
4265 cerr
<< "Invalid cloneid '" << arg1
<< "' specified" << std::endl
;
4269 snapid_t cloneid
= atoi(arg1
.c_str());
4270 ret
= remove_clone(fs
, coll
, ghobj
, cloneid
, force
);
4273 cerr
<< "Unknown object command '" << objcmd
<< "'" << std::endl
;
4280 ret
= PG::peek_map_epoch(fs
, pgid
, &map_epoch
);
4282 cerr
<< "peek_map_epoch reports error" << std::endl
;
4284 cerr
<< "map_epoch " << map_epoch
<< std::endl
;
4286 pg_info_t
info(pgid
);
4287 PastIntervals past_intervals
;
4289 ret
= PG::read_info(fs
, pgid
, coll
, info
, past_intervals
, struct_ver
);
4291 cerr
<< "read_info error " << cpp_strerror(ret
) << std::endl
;
4294 if (struct_ver
< PG::get_compat_struct_v()) {
4295 cerr
<< "PG is too old to upgrade, use older Ceph version" << std::endl
;
4300 cerr
<< "struct_v " << (int)struct_ver
<< std::endl
;
4302 if (op
== "export" || op
== "export-remove") {
4303 ceph_assert(superblock
!= nullptr);
4304 ret
= tool
.do_export(fs
, coll
, pgid
, info
, map_epoch
, struct_ver
, *superblock
, past_intervals
);
4306 cerr
<< "Export successful" << std::endl
;
4307 if (op
== "export-remove") {
4308 ret
= initiate_new_remove_pg(fs
, pgid
);
4309 // Export succeeded, so pgid is there
4310 ceph_assert(ret
== 0);
4311 cerr
<< "Remove successful" << std::endl
;
4314 } else if (op
== "info") {
4315 formatter
->open_object_section("info");
4316 info
.dump(formatter
);
4317 formatter
->close_section();
4318 formatter
->flush(cout
);
4320 } else if (op
== "log") {
4321 PGLog::IndexedLog log
;
4322 pg_missing_t missing
;
4323 ret
= get_log(fs
, struct_ver
, pgid
, info
, log
, missing
);
4327 dump_log(formatter
, cout
, log
, missing
);
4328 } else if (op
== "mark-complete") {
4329 ObjectStore::Transaction tran
;
4330 ObjectStore::Transaction
*t
= &tran
;
4332 if (struct_ver
< PG::get_compat_struct_v()) {
4333 cerr
<< "Can't mark-complete, version mismatch " << (int)struct_ver
4334 << " (pg) < compat " << (int)PG::get_compat_struct_v() << " (tool)"
4340 cout
<< "Marking complete " << std::endl
;
4342 ceph_assert(superblock
!= nullptr);
4343 info
.last_update
= eversion_t(superblock
->current_epoch
, info
.last_update
.version
+ 1);
4344 info
.last_backfill
= hobject_t::get_max();
4345 info
.last_epoch_started
= superblock
->current_epoch
;
4346 info
.history
.last_epoch_started
= superblock
->current_epoch
;
4347 info
.history
.last_epoch_clean
= superblock
->current_epoch
;
4348 past_intervals
.clear();
4351 ret
= write_info(*t
, map_epoch
, info
, past_intervals
);
4354 auto ch
= fs
->open_collection(coll_t(pgid
));
4355 fs
->queue_transaction(ch
, std::move(*t
));
4357 cout
<< "Marking complete succeeded" << std::endl
;
4358 } else if (op
== "trim-pg-log") {
4359 ret
= do_trim_pg_log(fs
, coll
, info
, pgid
,
4360 map_epoch
, past_intervals
);
4362 cerr
<< "Error trimming pg log: " << cpp_strerror(ret
) << std::endl
;
4365 cout
<< "Finished trimming pg log" << std::endl
;
4367 } else if (op
== "reset-last-complete") {
4369 std::cerr
<< "WARNING: reset-last-complete is extremely dangerous and almost "
4370 << "certain to lead to permanent data loss unless you know exactly "
4371 << "what you are doing. Pass --force to proceed anyway."
4376 ObjectStore::Transaction tran
;
4377 ObjectStore::Transaction
*t
= &tran
;
4379 if (struct_ver
< PG::get_compat_struct_v()) {
4380 cerr
<< "Can't reset-last-complete, version mismatch " << (int)struct_ver
4381 << " (pg) < compat " << (int)PG::get_compat_struct_v() << " (tool)"
4387 cout
<< "Reseting last_complete " << std::endl
;
4389 info
.last_complete
= info
.last_update
;
4392 ret
= write_info(*t
, map_epoch
, info
, past_intervals
);
4395 fs
->queue_transaction(ch
, std::move(*t
));
4397 cout
<< "Reseting last_complete succeeded" << std::endl
;
4400 ceph_assert(!"Should have already checked for valid --op");
4403 cerr
<< "PG '" << pgid
<< "' not found" << std::endl
;
4410 Formatter
* f
= Formatter::create("json-pretty", "json-pretty", "json-pretty");
4411 cct
->get_perfcounters_collection()->dump_formatted(f
, false);
4412 ostr
<< "ceph-objectstore-tool ";
4415 cout
<< ostr
.str() << std::endl
;
4418 int r
= fs
->umount();
4420 cerr
<< "umount failed: " << cpp_strerror(r
) << std::endl
;
4421 // If no previous error, then use umount() error
4427 // Export output can go to stdout, so put this message on stderr
4429 cerr
<< "dry-run: Nothing changed" << std::endl
;
4431 cout
<< "dry-run: Nothing changed" << std::endl
;