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"
50 namespace po
= boost::program_options
;
53 CompatSet
get_test_compat_set() {
54 CompatSet::FeatureSet ceph_osd_feature_compat
;
55 CompatSet::FeatureSet ceph_osd_feature_ro_compat
;
56 CompatSet::FeatureSet ceph_osd_feature_incompat
;
57 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_BASE
);
58 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_PGINFO
);
59 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_OLOC
);
60 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_LEC
);
61 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_CATEGORIES
);
62 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_HOBJECTPOOL
);
63 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_BIGINFO
);
64 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBINFO
);
65 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBLOG
);
67 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_SNAPMAPPER
);
68 ceph_osd_feature_incompat
.insert(CEPH_OSD_FEATURE_INCOMPAT_SHARDS
);
70 return CompatSet(ceph_osd_feature_compat
, ceph_osd_feature_ro_compat
,
71 ceph_osd_feature_incompat
);
75 const ssize_t max_read
= 1024 * 1024;
76 const int fd_none
= INT_MIN
;
80 struct action_on_object_t
{
81 virtual ~action_on_object_t() {}
82 virtual void call(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, object_info_t
&oi
) = 0;
85 int _action_on_all_objects_in_pg(ObjectStore
*store
, coll_t coll
, action_on_object_t
&action
, bool debug
)
87 auto ch
= store
->open_collection(coll
);
89 unsigned LIST_AT_A_TIME
= 100;
91 while (!next
.is_max()) {
92 vector
<ghobject_t
> list
;
93 int r
= store
->collection_list(ch
,
95 ghobject_t::get_max(),
100 cerr
<< "Error listing collection: " << coll
<< ", "
101 << cpp_strerror(r
) << std::endl
;
104 for (vector
<ghobject_t
>::iterator obj
= list
.begin();
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
;
140 r
= store
->list_collections(candidates
);
142 cerr
<< "Error listing collections: " << cpp_strerror(r
) << std::endl
;
145 pgid
.parse(pgidstr
.c_str());
146 for (vector
<coll_t
>::iterator i
= candidates
.begin();
147 i
!= candidates
.end();
150 if (i
->is_meta() && pgidstr
== "meta") {
151 colls_to_check
.push_back(*i
);
154 if (!i
->is_pg(&cand_pgid
))
157 // If an exact match or treat no shard as any shard
158 if (cand_pgid
== pgid
||
159 (pgid
.is_no_shard() && pgid
.pgid
== cand_pgid
.pgid
)) {
160 colls_to_check
.push_back(*i
);
165 cerr
<< colls_to_check
.size() << " pgs to scan" << std::endl
;
166 for (vector
<coll_t
>::iterator i
= colls_to_check
.begin();
167 i
!= colls_to_check
.end();
170 cerr
<< "Scanning " << *i
<< ", " << scanned
<< "/"
171 << colls_to_check
.size() << " completed" << std::endl
;
172 r
= _action_on_all_objects_in_pg(store
, *i
, action
, debug
);
179 int action_on_all_objects_in_exact_pg(ObjectStore
*store
, coll_t coll
, action_on_object_t
&action
, bool debug
)
181 int r
= _action_on_all_objects_in_pg(store
, coll
, action
, debug
);
185 int _action_on_all_objects(ObjectStore
*store
, action_on_object_t
&action
, bool debug
)
187 unsigned scanned
= 0;
189 vector
<coll_t
> colls_to_check
;
190 vector
<coll_t
> candidates
;
191 r
= store
->list_collections(candidates
);
193 cerr
<< "Error listing collections: " << cpp_strerror(r
) << std::endl
;
196 for (vector
<coll_t
>::iterator i
= candidates
.begin();
197 i
!= candidates
.end();
200 colls_to_check
.push_back(*i
);
205 cerr
<< colls_to_check
.size() << " pgs to scan" << std::endl
;
206 for (vector
<coll_t
>::iterator i
= colls_to_check
.begin();
207 i
!= colls_to_check
.end();
210 cerr
<< "Scanning " << *i
<< ", " << scanned
<< "/"
211 << colls_to_check
.size() << " completed" << std::endl
;
212 r
= _action_on_all_objects_in_pg(store
, *i
, action
, debug
);
219 int action_on_all_objects(ObjectStore
*store
, action_on_object_t
&action
, bool debug
)
221 int r
= _action_on_all_objects(store
, action
, debug
);
225 struct pgid_object_list
{
226 list
<pair
<coll_t
, ghobject_t
> > _objects
;
228 void insert(coll_t coll
, ghobject_t
&ghobj
) {
229 _objects
.push_back(make_pair(coll
, ghobj
));
232 void dump(Formatter
*f
, bool human_readable
) const {
234 f
->open_array_section("pgid_objects");
235 for (list
<pair
<coll_t
, ghobject_t
> >::const_iterator i
= _objects
.begin();
238 f
->open_array_section("pgid_object");
240 bool is_pg
= i
->first
.is_pg(&pgid
);
242 f
->dump_string("pgid", stringify(pgid
));
243 if (!is_pg
|| !human_readable
)
244 f
->dump_string("coll", i
->first
.to_str());
245 f
->open_object_section("ghobject");
249 if (human_readable
) {
254 if (!human_readable
) {
262 struct lookup_ghobject
: public action_on_object_t
{
263 pgid_object_list _objects
;
265 const boost::optional
<std::string
> _namespace
;
268 lookup_ghobject(const string
& name
, const boost::optional
<std::string
>& nspace
, bool need_snapset
= false) : _name(name
),
269 _namespace(nspace
), _need_snapset(need_snapset
) { }
271 void call(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, object_info_t
&oi
) override
{
272 if (_need_snapset
&& !ghobj
.hobj
.has_snapset())
274 if ((_name
.length() == 0 || ghobj
.hobj
.oid
.name
== _name
) &&
275 (!_namespace
|| ghobj
.hobj
.nspace
== _namespace
))
276 _objects
.insert(coll
, ghobj
);
281 return _objects
._objects
.size();
284 pair
<coll_t
, ghobject_t
> pop() {
285 pair
<coll_t
, ghobject_t
> front
= _objects
._objects
.front();
286 _objects
._objects
.pop_front();
290 void dump(Formatter
*f
, bool human_readable
) const {
291 _objects
.dump(f
, human_readable
);
295 struct lookup_slow_ghobject
: public action_on_object_t
{
308 lookup_slow_ghobject(const string
& name
, double _threshold
) :
309 _name(name
), threshold(_threshold
) { }
311 void call(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, object_info_t
&oi
) override
{
312 ObjectMap::ObjectMapIterator iter
;
313 auto start1
= mono_clock::now();
314 ceph::signedspan first_seek_time
= start1
- start1
;
315 ceph::signedspan last_seek_time
= first_seek_time
;
316 ceph::signedspan total_time
= first_seek_time
;
318 auto ch
= store
->open_collection(coll
);
319 iter
= store
->get_omap_iterator(ch
, ghobj
);
321 cerr
<< "omap_get_iterator: " << cpp_strerror(ENOENT
)
326 auto start
= mono_clock::now();
327 iter
->seek_to_first();
328 first_seek_time
= mono_clock::now() - start
;
330 while(iter
->valid()) {
331 start
= mono_clock::now();
333 last_seek_time
= mono_clock::now() - start
;
337 if (coll
!= last_coll
) {
338 cerr
<< ">>> inspecting coll" << coll
<< std::endl
;
342 total_time
= mono_clock::now() - start1
;
343 if ( total_time
>= make_timespan(threshold
)) {
344 _objects
.emplace_back(coll
, ghobj
,
345 first_seek_time
, last_seek_time
, total_time
,
346 url_escape(iter
->tail_key()));
347 cerr
<< ">>>>> found obj " << ghobj
348 << " first_seek_time "
349 << std::chrono::duration_cast
<std::chrono::seconds
>(first_seek_time
).count()
350 << " last_seek_time "
351 << std::chrono::duration_cast
<std::chrono::seconds
>(last_seek_time
).count()
353 << std::chrono::duration_cast
<std::chrono::seconds
>(total_time
).count()
354 << " tail key: " << url_escape(iter
->tail_key())
361 return _objects
.size();
364 void dump(Formatter
*f
, bool human_readable
) const {
366 f
->open_array_section("objects");
367 for (auto i
= _objects
.begin();
370 f
->open_array_section("object");
373 ceph::signedspan first_seek_time
;
374 ceph::signedspan last_seek_time
;
375 ceph::signedspan total_time
;
377 std::tie(coll
, ghobj
, first_seek_time
, last_seek_time
, total_time
, tail_key
) = *i
;
380 bool is_pg
= coll
.is_pg(&pgid
);
382 f
->dump_string("pgid", stringify(pgid
));
383 if (!is_pg
|| !human_readable
)
384 f
->dump_string("coll", coll
.to_str());
385 f
->dump_object("ghobject", ghobj
);
386 f
->open_object_section("times");
387 f
->dump_int("first_seek_time",
388 std::chrono::duration_cast
<std::chrono::seconds
>(first_seek_time
).count());
389 f
->dump_int("last_seek_time",
390 std::chrono::duration_cast
<std::chrono::seconds
>
391 (last_seek_time
).count());
392 f
->dump_int("total_time",
393 std::chrono::duration_cast
<std::chrono::seconds
>(total_time
).count());
394 f
->dump_string("tail_key", tail_key
);
398 if (human_readable
) {
403 if (!human_readable
) {
411 int file_fd
= fd_none
;
414 bool no_superblock
= false;
418 static int get_fd_data(int fd
, bufferlist
&bl
)
422 ssize_t bytes
= bl
.read_fd(fd
, max_read
);
424 cerr
<< "read_fd error " << cpp_strerror(bytes
) << std::endl
;
434 ceph_assert(bl
.length() == total
);
438 int get_log(ObjectStore
*fs
, __u8 struct_ver
,
439 spg_t pgid
, const pg_info_t
&info
,
440 PGLog::IndexedLog
&log
, pg_missing_t
&missing
)
443 auto ch
= fs
->open_collection(coll_t(pgid
));
448 ceph_assert(struct_ver
> 0);
449 PGLog::read_log_and_missing(
451 pgid
.make_pgmeta_oid(),
454 g_ceph_context
->_conf
->osd_ignore_stale_divergent_priors
);
455 if (debug
&& oss
.str().size())
456 cerr
<< oss
.str() << std::endl
;
458 catch (const buffer::error
&e
) {
459 cerr
<< "read_log_and_missing threw exception error " << e
.what() << std::endl
;
465 void dump_log(Formatter
*formatter
, ostream
&out
, pg_log_t
&log
,
466 pg_missing_t
&missing
)
468 formatter
->open_object_section("op_log");
469 formatter
->open_object_section("pg_log_t");
471 formatter
->close_section();
472 formatter
->flush(out
);
473 formatter
->open_object_section("pg_missing_t");
474 missing
.dump(formatter
);
475 formatter
->close_section();
476 formatter
->close_section();
477 formatter
->flush(out
);
480 //Based on part of OSD::load_pgs()
481 int finish_remove_pgs(ObjectStore
*store
)
484 int r
= store
->list_collections(ls
);
486 cerr
<< "finish_remove_pgs: failed to list pgs: " << cpp_strerror(r
)
491 for (vector
<coll_t
>::iterator it
= ls
.begin();
496 if (it
->is_temp(&pgid
) ||
497 (it
->is_pg(&pgid
) && PG::_has_removal_flag(store
, pgid
))) {
498 cout
<< "finish_remove_pgs " << *it
<< " removing " << pgid
<< std::endl
;
499 OSD::recursive_remove_collection(g_ceph_context
, store
, pgid
, *it
);
503 //cout << "finish_remove_pgs ignoring unrecognized " << *it << std::endl;
508 #pragma GCC diagnostic ignored "-Wpragmas"
509 #pragma GCC diagnostic push
510 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
512 int mark_pg_for_removal(ObjectStore
*fs
, spg_t pgid
, ObjectStore::Transaction
*t
)
514 pg_info_t
info(pgid
);
516 ghobject_t
pgmeta_oid(info
.pgid
.make_pgmeta_oid());
518 epoch_t map_epoch
= 0;
519 int r
= PG::peek_map_epoch(fs
, pgid
, &map_epoch
);
521 cerr
<< __func__
<< " warning: peek_map_epoch reported error" << std::endl
;
522 PastIntervals past_intervals
;
524 r
= PG::read_info(fs
, pgid
, coll
, info
, past_intervals
, struct_v
);
526 cerr
<< __func__
<< " error on read_info " << cpp_strerror(r
) << std::endl
;
529 ceph_assert(struct_v
>= 8);
531 cout
<< "setting '_remove' omap key" << std::endl
;
532 map
<string
,bufferlist
> values
;
533 encode((char)1, values
["_remove"]);
534 t
->omap_setkeys(coll
, pgmeta_oid
, values
);
538 #pragma GCC diagnostic pop
539 #pragma GCC diagnostic warning "-Wpragmas"
541 template<typename Func
>
542 void wait_until_done(ObjectStore::Transaction
* txn
, Func
&& func
)
544 bool finished
= false;
545 std::condition_variable cond
;
547 txn
->register_on_complete(make_lambda_context([&](int) {
548 std::unique_lock lock
{m
};
553 std::unique_lock lock
{m
};
554 cond
.wait(lock
, [&] {return finished
;});
557 int initiate_new_remove_pg(ObjectStore
*store
, spg_t r_pgid
)
560 finish_remove_pgs(store
);
561 if (!store
->collection_exists(coll_t(r_pgid
)))
564 cout
<< " marking collection for removal" << std::endl
;
567 ObjectStore::Transaction rmt
;
568 int r
= mark_pg_for_removal(store
, r_pgid
, &rmt
);
572 ObjectStore::CollectionHandle ch
= store
->open_collection(coll_t(r_pgid
));
573 store
->queue_transaction(ch
, std::move(rmt
));
574 finish_remove_pgs(store
);
578 int write_info(ObjectStore::Transaction
&t
, epoch_t epoch
, pg_info_t
&info
,
579 PastIntervals
&past_intervals
)
582 coll_t
coll(info
.pgid
);
583 ghobject_t
pgmeta_oid(info
.pgid
.make_pgmeta_oid());
584 map
<string
,bufferlist
> km
;
585 string key_to_remove
;
586 pg_info_t last_written_info
;
587 int ret
= prepare_info_keymap(
595 if (ret
) cerr
<< "Failed to write info" << std::endl
;
596 t
.omap_setkeys(coll
, pgmeta_oid
, km
);
597 if (!key_to_remove
.empty()) {
598 t
.omap_rmkey(coll
, pgmeta_oid
, key_to_remove
);
603 typedef map
<eversion_t
, hobject_t
> divergent_priors_t
;
605 int write_pg(ObjectStore::Transaction
&t
, epoch_t epoch
, pg_info_t
&info
,
606 pg_log_t
&log
, PastIntervals
&past_intervals
,
607 divergent_priors_t
&divergent
,
608 pg_missing_t
&missing
)
610 cout
<< __func__
<< " epoch " << epoch
<< " info " << info
<< std::endl
;
611 int ret
= write_info(t
, epoch
, info
, past_intervals
);
615 coll_t
coll(info
.pgid
);
616 map
<string
,bufferlist
> km
;
617 const bool require_rollback
= !info
.pgid
.is_no_shard();
618 if (!divergent
.empty()) {
619 ceph_assert(missing
.get_items().empty());
620 PGLog::write_log_and_missing_wo_missing(
621 t
, &km
, log
, coll
, info
.pgid
.make_pgmeta_oid(), divergent
,
624 pg_missing_tracker_t
tmissing(missing
);
625 bool rebuilt_missing_set_with_deletes
= missing
.may_include_deletes
;
626 PGLog::write_log_and_missing(
627 t
, &km
, log
, coll
, info
.pgid
.make_pgmeta_oid(), tmissing
,
629 &rebuilt_missing_set_with_deletes
);
631 t
.omap_setkeys(coll
, info
.pgid
.make_pgmeta_oid(), km
);
635 int do_trim_pg_log(ObjectStore
*store
, const coll_t
&coll
,
636 pg_info_t
&info
, const spg_t
&pgid
,
638 PastIntervals
&past_intervals
)
640 ghobject_t oid
= pgid
.make_pgmeta_oid();
642 auto ch
= store
->open_collection(coll
);
643 int r
= store
->stat(ch
, oid
, &st
);
645 ceph_assert(st
.st_size
== 0);
647 cerr
<< "Log bounds are: " << "(" << info
.log_tail
<< ","
648 << info
.last_update
<< "]" << std::endl
;
650 uint64_t max_entries
= g_ceph_context
->_conf
->osd_max_pg_log_entries
;
651 if (info
.last_update
.version
- info
.log_tail
.version
<= max_entries
) {
652 cerr
<< "Log not larger than osd_max_pg_log_entries " << max_entries
<< std::endl
;
656 ceph_assert(info
.last_update
.version
> max_entries
);
657 version_t trim_to
= info
.last_update
.version
- max_entries
;
658 size_t trim_at_once
= g_ceph_context
->_conf
->osd_pg_log_trim_max
;
663 // gather keys so we can delete them in a batch without
664 // affecting the iterator
665 set
<string
> keys_to_trim
;
667 ObjectMap::ObjectMapIterator p
= store
->get_omap_iterator(ch
, oid
);
670 for (p
->seek_to_first(); p
->valid(); p
->next()) {
671 if (p
->key()[0] == '_')
673 if (p
->key() == "can_rollback_to")
675 if (p
->key() == "divergent_priors")
677 if (p
->key() == "rollback_info_trimmed_to")
679 if (p
->key() == "may_include_deletes_in_missing")
681 if (p
->key().substr(0, 7) == string("missing"))
683 if (p
->key().substr(0, 4) == string("dup_"))
686 bufferlist bl
= p
->value();
687 auto bp
= bl
.cbegin();
690 e
.decode_with_checksum(bp
);
691 } catch (const buffer::error
&e
) {
692 cerr
<< "Error reading pg log entry: " << e
.what() << std::endl
;
695 cerr
<< "read entry " << e
<< std::endl
;
697 if (e
.version
.version
> trim_to
) {
701 keys_to_trim
.insert(p
->key());
702 new_tail
= e
.version
;
703 if (keys_to_trim
.size() >= trim_at_once
)
709 } // deconstruct ObjectMapIterator
712 if (!dry_run
&& !keys_to_trim
.empty()) {
713 cout
<< "Removing keys " << *keys_to_trim
.begin() << " - " << *keys_to_trim
.rbegin() << std::endl
;
714 ObjectStore::Transaction t
;
715 t
.omap_rmkeys(coll
, oid
, keys_to_trim
);
716 store
->queue_transaction(ch
, std::move(t
));
721 // update pg info with new tail
722 if (!dry_run
&& new_tail
!= eversion_t()) {
723 info
.log_tail
= new_tail
;
724 ObjectStore::Transaction t
;
725 int ret
= write_info(t
, map_epoch
, info
, past_intervals
);
728 store
->queue_transaction(ch
, std::move(t
));
732 // compact the db since we just removed a bunch of data
733 cerr
<< "Finished trimming, now compacting..." << std::endl
;
739 int do_trim_pg_log_dups(ObjectStore
*store
, const coll_t
&coll
,
740 pg_info_t
&info
, const spg_t
&pgid
,
742 PastIntervals
&past_intervals
)
744 ghobject_t oid
= pgid
.make_pgmeta_oid();
746 auto ch
= store
->open_collection(coll
);
747 int r
= store
->stat(ch
, oid
, &st
);
749 ceph_assert(st
.st_size
== 0);
751 const size_t max_dup_entries
= g_ceph_context
->_conf
->osd_pg_log_dups_tracked
;
752 ceph_assert(max_dup_entries
> 0);
753 const size_t max_chunk_size
= g_ceph_context
->_conf
->osd_pg_log_trim_max
;
754 ceph_assert(max_chunk_size
> 0);
756 cout
<< "max_dup_entries=" << max_dup_entries
757 << " max_chunk_size=" << max_chunk_size
<< std::endl
;
759 cout
<< "Dry run enabled, so when many chunks are needed,"
760 << " the trimming will never stop!" << std::endl
;
763 set
<string
> keys_to_keep
;
764 size_t num_removed
= 0;
766 set
<string
> keys_to_trim
;
768 ObjectMap::ObjectMapIterator p
= store
->get_omap_iterator(ch
, oid
);
771 for (p
->seek_to_first(); p
->valid(); p
->next()) {
772 if (p
->key()[0] == '_')
774 if (p
->key() == "can_rollback_to")
776 if (p
->key() == "divergent_priors")
778 if (p
->key() == "rollback_info_trimmed_to")
780 if (p
->key() == "may_include_deletes_in_missing")
782 if (p
->key().substr(0, 7) == string("missing"))
784 if (p
->key().substr(0, 4) != string("dup_"))
786 keys_to_keep
.insert(p
->key());
787 if (keys_to_keep
.size() > max_dup_entries
) {
788 auto oldest_to_keep
= keys_to_keep
.begin();
789 keys_to_trim
.emplace(*oldest_to_keep
);
790 keys_to_keep
.erase(oldest_to_keep
);
792 if (keys_to_trim
.size() >= max_chunk_size
) {
796 } // deconstruct ObjectMapIterator
798 num_removed
= keys_to_trim
.size();
799 if (!dry_run
&& !keys_to_trim
.empty()) {
800 cout
<< "Removing keys " << *keys_to_trim
.begin() << " - " << *keys_to_trim
.rbegin() << std::endl
;
801 ObjectStore::Transaction t
;
802 t
.omap_rmkeys(coll
, oid
, keys_to_trim
);
803 store
->queue_transaction(ch
, std::move(t
));
806 } while (num_removed
== max_chunk_size
);
808 // compact the db since we just removed a bunch of data
809 cerr
<< "Finished trimming, now compacting..." << std::endl
;
815 const int OMAP_BATCH_SIZE
= 25;
816 void get_omap_batch(ObjectMap::ObjectMapIterator
&iter
, map
<string
, bufferlist
> &oset
)
819 for (int count
= OMAP_BATCH_SIZE
; count
&& iter
->valid(); --count
, iter
->next()) {
820 oset
.insert(pair
<string
, bufferlist
>(iter
->key(), iter
->value()));
824 int ObjectStoreTool::export_file(ObjectStore
*store
, coll_t cid
, ghobject_t
&obj
)
830 auto ch
= store
->open_collection(cid
);
831 int ret
= store
->stat(ch
, obj
, &st
);
835 cerr
<< "Read " << obj
<< std::endl
;
839 cerr
<< "size=" << total
<< std::endl
;
841 object_begin
objb(obj
);
846 ret
= store
->getattr(ch
, obj
, OI_ATTR
, bp
);
848 cerr
<< "getattr failure object_info " << ret
<< std::endl
;
854 cerr
<< "object_info: " << objb
.oi
<< std::endl
;
857 // NOTE: we include whiteouts, lost, etc.
859 ret
= write_section(TYPE_OBJECT_BEGIN
, objb
, file_fd
);
864 bufferlist rawdatabl
;
867 mysize_t len
= max_read
;
871 ret
= store
->read(ch
, obj
, offset
, len
, rawdatabl
);
877 data_section
dblock(offset
, len
, rawdatabl
);
879 cerr
<< "data section offset=" << offset
<< " len=" << len
<< std::endl
;
884 ret
= write_section(TYPE_DATA
, dblock
, file_fd
);
888 //Handle attrs for this object
889 map
<string
,bufferptr
,less
<>> aset
;
890 ret
= store
->getattrs(ch
, obj
, aset
);
892 attr_section
as(aset
);
893 ret
= write_section(TYPE_ATTRS
, as
, file_fd
);
898 cerr
<< "attrs size " << aset
.size() << std::endl
;
901 //Handle omap information
903 ret
= store
->omap_get_header(ch
, obj
, &hdrbuf
, true);
905 cerr
<< "omap_get_header: " << cpp_strerror(ret
) << std::endl
;
909 omap_hdr_section
ohs(hdrbuf
);
910 ret
= write_section(TYPE_OMAP_HDR
, ohs
, file_fd
);
914 ObjectMap::ObjectMapIterator iter
= store
->get_omap_iterator(ch
, obj
);
917 cerr
<< "omap_get_iterator: " << cpp_strerror(ret
) << std::endl
;
920 iter
->seek_to_first();
922 map
<string
, bufferlist
> out
;
923 while(iter
->valid()) {
924 get_omap_batch(iter
, out
);
926 if (out
.empty()) break;
928 mapcount
+= out
.size();
929 omap_section
oms(out
);
930 ret
= write_section(TYPE_OMAP
, oms
, file_fd
);
935 cerr
<< "omap map size " << mapcount
<< std::endl
;
937 ret
= write_simple(TYPE_OBJECT_END
, file_fd
);
944 int ObjectStoreTool::export_files(ObjectStore
*store
, coll_t coll
)
947 auto ch
= store
->open_collection(coll
);
948 while (!next
.is_max()) {
949 vector
<ghobject_t
> objects
;
950 int r
= store
->collection_list(ch
, next
, ghobject_t::get_max(), 300,
954 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
957 ceph_assert(!i
->hobj
.is_meta());
958 if (i
->is_pgmeta() || i
->hobj
.is_temp() || !i
->is_no_gen()) {
961 r
= export_file(store
, coll
, *i
);
969 int set_inc_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
, bool force
) {
970 OSDMap::Incremental inc
;
971 auto it
= bl
.cbegin();
975 } else if (e
!= inc
.epoch
) {
976 cerr
<< "incremental.epoch mismatch: "
977 << inc
.epoch
<< " != " << e
<< std::endl
;
979 cerr
<< "But will continue anyway." << std::endl
;
984 auto ch
= store
->open_collection(coll_t::meta());
985 const ghobject_t inc_oid
= OSD::get_inc_osdmap_pobject_name(e
);
986 if (!store
->exists(ch
, inc_oid
)) {
987 cerr
<< "inc-osdmap (" << inc_oid
<< ") does not exist." << std::endl
;
991 cout
<< "Creating a new epoch." << std::endl
;
995 ObjectStore::Transaction t
;
996 t
.write(coll_t::meta(), inc_oid
, 0, bl
.length(), bl
);
997 t
.truncate(coll_t::meta(), inc_oid
, bl
.length());
998 store
->queue_transaction(ch
, std::move(t
));
1002 int get_inc_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
)
1004 auto ch
= store
->open_collection(coll_t::meta());
1006 OSD::get_inc_osdmap_pobject_name(e
),
1013 int set_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
, bool force
) {
1017 e
= osdmap
.get_epoch();
1018 } else if (e
!= osdmap
.get_epoch()) {
1019 cerr
<< "osdmap.epoch mismatch: "
1020 << e
<< " != " << osdmap
.get_epoch() << std::endl
;
1022 cerr
<< "But will continue anyway." << std::endl
;
1027 auto ch
= store
->open_collection(coll_t::meta());
1028 const ghobject_t full_oid
= OSD::get_osdmap_pobject_name(e
);
1029 if (!store
->exists(ch
, full_oid
)) {
1030 cerr
<< "osdmap (" << full_oid
<< ") does not exist." << std::endl
;
1034 cout
<< "Creating a new epoch." << std::endl
;
1038 ObjectStore::Transaction t
;
1039 t
.write(coll_t::meta(), full_oid
, 0, bl
.length(), bl
);
1040 t
.truncate(coll_t::meta(), full_oid
, bl
.length());
1041 store
->queue_transaction(ch
, std::move(t
));
1045 int get_osdmap(ObjectStore
*store
, epoch_t e
, OSDMap
&osdmap
, bufferlist
& bl
)
1047 ObjectStore::CollectionHandle ch
= store
->open_collection(coll_t::meta());
1048 bool found
= store
->read(
1049 ch
, OSD::get_osdmap_pobject_name(e
), 0, 0, bl
) >= 0;
1051 cerr
<< "Can't find OSDMap for pg epoch " << e
<< std::endl
;
1056 cerr
<< osdmap
<< std::endl
;
1060 int get_pg_num_history(ObjectStore
*store
, pool_pg_num_history_t
*h
)
1062 ObjectStore::CollectionHandle ch
= store
->open_collection(coll_t::meta());
1064 auto pghist
= OSD::make_pg_num_history_oid();
1065 int r
= store
->read(ch
, pghist
, 0, 0, bl
, 0);
1066 if (r
>= 0 && bl
.length() > 0) {
1067 auto p
= bl
.cbegin();
1070 cout
<< __func__
<< " pg_num_history " << *h
<< std::endl
;
1074 int add_osdmap(ObjectStore
*store
, metadata_section
&ms
)
1076 return get_osdmap(store
, ms
.map_epoch
, ms
.osdmap
, ms
.osdmap_bl
);
1079 int ObjectStoreTool::do_export(ObjectStore
*fs
, coll_t coll
, spg_t pgid
,
1080 pg_info_t
&info
, epoch_t map_epoch
, __u8 struct_ver
,
1081 const OSDSuperblock
& superblock
,
1082 PastIntervals
&past_intervals
)
1084 PGLog::IndexedLog log
;
1085 pg_missing_t missing
;
1087 cerr
<< "Exporting " << pgid
<< " info " << info
<< std::endl
;
1089 int ret
= get_log(fs
, struct_ver
, pgid
, info
, log
, missing
);
1094 Formatter
*formatter
= Formatter::create("json-pretty");
1095 ceph_assert(formatter
);
1096 dump_log(formatter
, cerr
, log
, missing
);
1101 pg_begin
pgb(pgid
, superblock
);
1102 // Special case: If replicated pg don't require the importing OSD to have shard feature
1103 if (pgid
.is_no_shard()) {
1104 pgb
.superblock
.compat_features
.incompat
.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS
);
1106 ret
= write_section(TYPE_PG_BEGIN
, pgb
, file_fd
);
1110 // The metadata_section is now before files, so import can detect
1111 // errors and abort without wasting time.
1112 metadata_section
ms(
1119 ret
= add_osdmap(fs
, ms
);
1122 ret
= write_section(TYPE_PG_METADATA
, ms
, file_fd
);
1126 ret
= export_files(fs
, coll
);
1128 cerr
<< "export_files error " << ret
<< std::endl
;
1132 ret
= write_simple(TYPE_PG_END
, file_fd
);
1139 int dump_data(Formatter
*formatter
, bufferlist
&bl
)
1141 auto ebliter
= bl
.cbegin();
1145 formatter
->open_object_section("data_block");
1146 formatter
->dump_unsigned("offset", ds
.offset
);
1147 formatter
->dump_unsigned("len", ds
.len
);
1148 // XXX: Add option to dump data like od -cx ?
1149 formatter
->close_section();
1150 formatter
->flush(cout
);
1154 int get_data(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1155 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1157 auto ebliter
= bl
.cbegin();
1162 cerr
<< "\tdata: offset " << ds
.offset
<< " len " << ds
.len
<< std::endl
;
1163 t
->write(coll
, hoid
, ds
.offset
, ds
.len
, ds
.databl
);
1168 Formatter
*formatter
, ghobject_t hoid
,
1171 auto ebliter
= bl
.cbegin();
1175 // This could have been handled in the caller if we didn't need to
1176 // support exports that didn't include object_info_t in object_begin.
1177 if (hoid
.generation
== ghobject_t::NO_GEN
&&
1178 hoid
.hobj
.is_head()) {
1179 map
<string
,bufferlist
>::iterator mi
= as
.data
.find(SS_ATTR
);
1180 if (mi
!= as
.data
.end()) {
1182 auto p
= mi
->second
.cbegin();
1184 formatter
->open_object_section("snapset");
1185 snapset
.dump(formatter
);
1186 formatter
->close_section();
1188 formatter
->open_object_section("snapset");
1189 formatter
->dump_string("error", "missing SS_ATTR");
1190 formatter
->close_section();
1194 formatter
->open_object_section("attrs");
1195 formatter
->open_array_section("user");
1196 for (auto kv
: as
.data
) {
1197 // Skip system attributes
1198 if (('_' != kv
.first
.at(0)) || kv
.first
.size() == 1)
1200 formatter
->open_object_section("user_attr");
1201 formatter
->dump_string("name", kv
.first
.substr(1));
1203 formatter
->dump_string("value", cleanbin(kv
.second
, b64
));
1204 formatter
->dump_bool("Base64", b64
);
1205 formatter
->close_section();
1207 formatter
->close_section();
1208 formatter
->open_array_section("system");
1209 for (auto kv
: as
.data
) {
1210 // Skip user attributes
1211 if (('_' == kv
.first
.at(0)) && kv
.first
.size() != 1)
1213 formatter
->open_object_section("sys_attr");
1214 formatter
->dump_string("name", kv
.first
);
1215 formatter
->close_section();
1217 formatter
->close_section();
1218 formatter
->close_section();
1219 formatter
->flush(cout
);
1225 ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1226 ObjectStore::Transaction
*t
, bufferlist
&bl
,
1227 OSDriver
&driver
, SnapMapper
&snap_mapper
)
1229 auto ebliter
= bl
.cbegin();
1233 auto ch
= store
->open_collection(coll
);
1235 cerr
<< "\tattrs: len " << as
.data
.size() << std::endl
;
1236 t
->setattrs(coll
, hoid
, as
.data
);
1238 // This could have been handled in the caller if we didn't need to
1239 // support exports that didn't include object_info_t in object_begin.
1240 if (hoid
.generation
== ghobject_t::NO_GEN
&&
1241 hoid
.hobj
.is_head()) {
1242 map
<string
,bufferlist
>::iterator mi
= as
.data
.find(SS_ATTR
);
1243 if (mi
!= as
.data
.end()) {
1245 auto p
= mi
->second
.cbegin();
1247 cout
<< "snapset " << snapset
<< std::endl
;
1248 for (auto& p
: snapset
.clone_snaps
) {
1249 ghobject_t clone
= hoid
;
1250 clone
.hobj
.snap
= p
.first
;
1251 set
<snapid_t
> snaps(p
.second
.begin(), p
.second
.end());
1252 if (!store
->exists(ch
, clone
)) {
1253 // no clone, skip. this is probably a cache pool. this works
1254 // because we use a separate transaction per object and clones
1255 // come before head in the archive.
1257 cerr
<< "\tskipping missing " << clone
<< " (snaps "
1258 << snaps
<< ")" << std::endl
;
1262 cerr
<< "\tsetting " << clone
.hobj
<< " snaps " << snaps
1264 OSDriver::OSTransaction
_t(driver
.get_transaction(t
));
1265 ceph_assert(!snaps
.empty());
1266 snap_mapper
.add_oid(clone
.hobj
, snaps
, &_t
);
1269 cerr
<< "missing SS_ATTR on " << hoid
<< std::endl
;
1275 int dump_omap_hdr(Formatter
*formatter
, bufferlist
&bl
)
1277 auto ebliter
= bl
.cbegin();
1278 omap_hdr_section oh
;
1281 formatter
->open_object_section("omap_header");
1282 formatter
->dump_string("value", string(oh
.hdr
.c_str(), oh
.hdr
.length()));
1283 formatter
->close_section();
1284 formatter
->flush(cout
);
1288 int get_omap_hdr(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1289 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1291 auto ebliter
= bl
.cbegin();
1292 omap_hdr_section oh
;
1296 cerr
<< "\tomap header: " << string(oh
.hdr
.c_str(), oh
.hdr
.length())
1298 t
->omap_setheader(coll
, hoid
, oh
.hdr
);
1302 int dump_omap(Formatter
*formatter
, bufferlist
&bl
)
1304 auto ebliter
= bl
.cbegin();
1308 formatter
->open_object_section("omaps");
1309 formatter
->dump_unsigned("count", os
.omap
.size());
1310 formatter
->open_array_section("data");
1311 for (auto o
: os
.omap
) {
1312 formatter
->open_object_section("omap");
1313 formatter
->dump_string("name", o
.first
);
1315 formatter
->dump_string("value", cleanbin(o
.second
, b64
));
1316 formatter
->dump_bool("Base64", b64
);
1317 formatter
->close_section();
1319 formatter
->close_section();
1320 formatter
->close_section();
1321 formatter
->flush(cout
);
1325 int get_omap(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1326 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1328 auto ebliter
= bl
.cbegin();
1333 cerr
<< "\tomap: size " << os
.omap
.size() << std::endl
;
1334 t
->omap_setkeys(coll
, hoid
, os
.omap
);
1338 int ObjectStoreTool::dump_object(Formatter
*formatter
,
1341 auto ebliter
= bl
.cbegin();
1345 if (ob
.hoid
.hobj
.is_temp()) {
1346 cerr
<< "ERROR: Export contains temporary object '" << ob
.hoid
<< "'" << std::endl
;
1350 formatter
->open_object_section("object");
1351 formatter
->open_object_section("oid");
1352 ob
.hoid
.dump(formatter
);
1353 formatter
->close_section();
1354 formatter
->open_object_section("object_info");
1355 ob
.oi
.dump(formatter
);
1356 formatter
->close_section();
1362 int ret
= read_section(&type
, &ebl
);
1366 //cout << "\tdo_object: Section type " << hex << type << dec << std::endl;
1367 //cout << "\t\tsection size " << ebl.length() << std::endl;
1368 if (type
>= END_OF_TYPES
) {
1369 cout
<< "Skipping unknown object section type" << std::endl
;
1375 ret
= dump_data(formatter
, ebl
);
1376 if (ret
) return ret
;
1380 ret
= dump_attrs(formatter
, ob
.hoid
, ebl
);
1381 if (ret
) return ret
;
1385 ret
= dump_omap_hdr(formatter
, ebl
);
1386 if (ret
) return ret
;
1390 ret
= dump_omap(formatter
, ebl
);
1391 if (ret
) return ret
;
1393 case TYPE_OBJECT_END
:
1397 cerr
<< "Unknown section type " << type
<< std::endl
;
1401 formatter
->close_section();
1405 int ObjectStoreTool::get_object(ObjectStore
*store
,
1409 bufferlist
&bl
, OSDMap
&origmap
,
1410 bool *skipped_objects
)
1412 ObjectStore::Transaction tran
;
1413 ObjectStore::Transaction
*t
= &tran
;
1414 auto ebliter
= bl
.cbegin();
1418 if (ob
.hoid
.hobj
.is_temp()) {
1419 cerr
<< "ERROR: Export contains temporary object '" << ob
.hoid
<< "'" << std::endl
;
1422 ceph_assert(g_ceph_context
);
1424 auto ch
= store
->open_collection(coll
);
1425 if (ob
.hoid
.hobj
.nspace
!= g_ceph_context
->_conf
->osd_hit_set_namespace
) {
1426 object_t oid
= ob
.hoid
.hobj
.oid
;
1427 object_locator_t
loc(ob
.hoid
.hobj
);
1428 pg_t raw_pgid
= origmap
.object_locator_to_pg(oid
, loc
);
1429 pg_t pgid
= origmap
.raw_pg_to_pg(raw_pgid
);
1432 if (coll
.is_pg(&coll_pgid
) == false) {
1433 cerr
<< "INTERNAL ERROR: Bad collection during import" << std::endl
;
1436 if (coll_pgid
.shard
!= ob
.hoid
.shard_id
) {
1437 cerr
<< "INTERNAL ERROR: Importing shard " << coll_pgid
.shard
1438 << " but object shard is " << ob
.hoid
.shard_id
<< std::endl
;
1442 if (coll_pgid
.pgid
!= pgid
) {
1443 cerr
<< "Skipping object '" << ob
.hoid
<< "' which belongs in pg " << pgid
<< std::endl
;
1444 *skipped_objects
= true;
1451 t
->touch(coll
, ob
.hoid
);
1453 cout
<< "Write " << ob
.hoid
<< std::endl
;
1459 int ret
= read_section(&type
, &ebl
);
1463 //cout << "\tdo_object: Section type " << hex << type << dec << std::endl;
1464 //cout << "\t\tsection size " << ebl.length() << std::endl;
1465 if (type
>= END_OF_TYPES
) {
1466 cout
<< "Skipping unknown object section type" << std::endl
;
1472 ret
= get_data(store
, coll
, ob
.hoid
, t
, ebl
);
1473 if (ret
) return ret
;
1477 ret
= get_attrs(store
, coll
, ob
.hoid
, t
, ebl
, driver
, mapper
);
1478 if (ret
) return ret
;
1482 ret
= get_omap_hdr(store
, coll
, ob
.hoid
, t
, ebl
);
1483 if (ret
) return ret
;
1487 ret
= get_omap(store
, coll
, ob
.hoid
, t
, ebl
);
1488 if (ret
) return ret
;
1490 case TYPE_OBJECT_END
:
1494 cerr
<< "Unknown section type " << type
<< std::endl
;
1499 wait_until_done(t
, [&] {
1500 store
->queue_transaction(ch
, std::move(*t
));
1507 int dump_pg_metadata(Formatter
*formatter
, bufferlist
&bl
, metadata_section
&ms
)
1509 auto ebliter
= bl
.cbegin();
1512 formatter
->open_object_section("metadata_section");
1514 formatter
->dump_unsigned("pg_disk_version", (int)ms
.struct_ver
);
1515 formatter
->dump_unsigned("map_epoch", ms
.map_epoch
);
1517 formatter
->open_object_section("OSDMap");
1518 ms
.osdmap
.dump(formatter
);
1519 formatter
->close_section();
1520 formatter
->flush(cout
);
1523 formatter
->open_object_section("info");
1524 ms
.info
.dump(formatter
);
1525 formatter
->close_section();
1526 formatter
->flush(cout
);
1528 formatter
->open_object_section("log");
1529 ms
.log
.dump(formatter
);
1530 formatter
->close_section();
1531 formatter
->flush(cout
);
1533 formatter
->open_object_section("pg_missing_t");
1534 ms
.missing
.dump(formatter
);
1535 formatter
->close_section();
1537 // XXX: ms.past_intervals?
1539 formatter
->close_section();
1540 formatter
->flush(cout
);
1542 if (ms
.osdmap
.get_epoch() != 0 && ms
.map_epoch
!= ms
.osdmap
.get_epoch()) {
1543 cerr
<< "FATAL: Invalid OSDMap epoch in export data" << std::endl
;
1550 int get_pg_metadata(ObjectStore
*store
, bufferlist
&bl
, metadata_section
&ms
,
1551 const OSDSuperblock
& sb
, spg_t pgid
)
1553 auto ebliter
= bl
.cbegin();
1555 spg_t old_pgid
= ms
.info
.pgid
;
1556 ms
.info
.pgid
= pgid
;
1559 cout
<< "export pgid " << old_pgid
<< std::endl
;
1560 cout
<< "struct_v " << (int)ms
.struct_ver
<< std::endl
;
1561 cout
<< "map epoch " << ms
.map_epoch
<< std::endl
;
1564 Formatter
*formatter
= new JSONFormatter(true);
1565 formatter
->open_object_section("stuff");
1567 formatter
->open_object_section("importing OSDMap");
1568 ms
.osdmap
.dump(formatter
);
1569 formatter
->close_section();
1570 formatter
->flush(cout
);
1573 cout
<< "osd current epoch " << sb
.current_epoch
<< std::endl
;
1575 formatter
->open_object_section("info");
1576 ms
.info
.dump(formatter
);
1577 formatter
->close_section();
1578 formatter
->flush(cout
);
1581 formatter
->open_object_section("log");
1582 ms
.log
.dump(formatter
);
1583 formatter
->close_section();
1584 formatter
->flush(cout
);
1587 formatter
->close_section();
1588 formatter
->flush(cout
);
1593 if (ms
.osdmap
.get_epoch() != 0 && ms
.map_epoch
!= ms
.osdmap
.get_epoch()) {
1594 cerr
<< "FATAL: Invalid OSDMap epoch in export data" << std::endl
;
1598 if (ms
.map_epoch
> sb
.current_epoch
) {
1599 cerr
<< "ERROR: Export PG's map_epoch " << ms
.map_epoch
<< " > OSD's epoch " << sb
.current_epoch
<< std::endl
;
1600 cerr
<< "The OSD you are using is older than the exported PG" << std::endl
;
1601 cerr
<< "Either use another OSD or join selected OSD to cluster to update it first" << std::endl
;
1605 // Old exports didn't include OSDMap
1606 if (ms
.osdmap
.get_epoch() == 0) {
1607 cerr
<< "WARNING: No OSDMap in old export, this is an ancient export."
1608 " Not supported." << std::endl
;
1612 if (ms
.osdmap
.get_epoch() < sb
.oldest_map
) {
1613 cerr
<< "PG export's map " << ms
.osdmap
.get_epoch()
1614 << " is older than OSD's oldest_map " << sb
.oldest_map
<< std::endl
;
1616 cerr
<< " pass --force to proceed anyway (with incomplete PastIntervals)"
1622 cerr
<< "Import pgid " << ms
.info
.pgid
<< std::endl
;
1623 cerr
<< "Previous past_intervals " << ms
.past_intervals
<< std::endl
;
1624 cerr
<< "history.same_interval_since "
1625 << ms
.info
.history
.same_interval_since
<< std::endl
;
1631 // out: pg_log_t that only has entries that apply to import_pgid using curmap
1632 // reject: Entries rejected from "in" are in the reject.log. Other fields not set.
1633 void filter_divergent_priors(spg_t import_pgid
, const OSDMap
&curmap
,
1634 const string
&hit_set_namespace
, const divergent_priors_t
&in
,
1635 divergent_priors_t
&out
, divergent_priors_t
&reject
)
1640 for (divergent_priors_t::const_iterator i
= in
.begin();
1641 i
!= in
.end(); ++i
) {
1643 // Reject divergent priors for temporary objects
1644 if (i
->second
.is_temp()) {
1649 if (i
->second
.nspace
!= hit_set_namespace
) {
1650 object_t oid
= i
->second
.oid
;
1651 object_locator_t
loc(i
->second
);
1652 pg_t raw_pgid
= curmap
.object_locator_to_pg(oid
, loc
);
1653 pg_t pgid
= curmap
.raw_pg_to_pg(raw_pgid
);
1655 if (import_pgid
.pgid
== pgid
) {
1666 int ObjectStoreTool::dump_export(Formatter
*formatter
)
1670 PGLog::IndexedLog log
;
1671 //bool skipped_objects = false;
1673 int ret
= read_super();
1677 if (sh
.magic
!= super_header::super_magic
) {
1678 cerr
<< "Invalid magic number" << std::endl
;
1682 if (sh
.version
> super_header::super_ver
) {
1683 cerr
<< "Can't handle export format version=" << sh
.version
<< std::endl
;
1687 formatter
->open_object_section("Export");
1689 //First section must be TYPE_PG_BEGIN
1691 ret
= read_section(&type
, &ebl
);
1694 if (type
== TYPE_POOL_BEGIN
) {
1695 cerr
<< "Dump of pool exports not supported" << std::endl
;
1697 } else if (type
!= TYPE_PG_BEGIN
) {
1698 cerr
<< "Invalid first section type " << std::to_string(type
) << std::endl
;
1702 auto ebliter
= ebl
.cbegin();
1704 pgb
.decode(ebliter
);
1705 spg_t pgid
= pgb
.pgid
;
1707 formatter
->dump_string("pgid", stringify(pgid
));
1708 formatter
->dump_string("cluster_fsid", stringify(pgb
.superblock
.cluster_fsid
));
1709 formatter
->dump_string("features", stringify(pgb
.superblock
.compat_features
));
1712 bool found_metadata
= false;
1713 metadata_section ms
;
1714 bool objects_started
= false;
1716 ret
= read_section(&type
, &ebl
);
1721 cerr
<< "dump_export: Section type " << std::to_string(type
) << std::endl
;
1723 if (type
>= END_OF_TYPES
) {
1724 cerr
<< "Skipping unknown section type" << std::endl
;
1728 case TYPE_OBJECT_BEGIN
:
1729 if (!objects_started
) {
1730 formatter
->open_array_section("objects");
1731 objects_started
= true;
1733 ret
= dump_object(formatter
, ebl
);
1734 if (ret
) return ret
;
1736 case TYPE_PG_METADATA
:
1737 if (objects_started
)
1738 cerr
<< "WARNING: metadata_section out of order" << std::endl
;
1739 ret
= dump_pg_metadata(formatter
, ebl
, ms
);
1740 if (ret
) return ret
;
1741 found_metadata
= true;
1744 if (objects_started
) {
1745 formatter
->close_section();
1750 cerr
<< "Unknown section type " << std::to_string(type
) << std::endl
;
1755 if (!found_metadata
) {
1756 cerr
<< "Missing metadata section" << std::endl
;
1760 formatter
->close_section();
1761 formatter
->flush(cout
);
1766 int ObjectStoreTool::do_import(ObjectStore
*store
, OSDSuperblock
& sb
,
1767 bool force
, std::string pgidstr
)
1771 PGLog::IndexedLog log
;
1772 bool skipped_objects
= false;
1775 finish_remove_pgs(store
);
1777 int ret
= read_super();
1781 if (sh
.magic
!= super_header::super_magic
) {
1782 cerr
<< "Invalid magic number" << std::endl
;
1786 if (sh
.version
> super_header::super_ver
) {
1787 cerr
<< "Can't handle export format version=" << sh
.version
<< std::endl
;
1791 //First section must be TYPE_PG_BEGIN
1793 ret
= read_section(&type
, &ebl
);
1796 if (type
== TYPE_POOL_BEGIN
) {
1797 cerr
<< "Pool exports cannot be imported into a PG" << std::endl
;
1799 } else if (type
!= TYPE_PG_BEGIN
) {
1800 cerr
<< "Invalid first section type " << std::to_string(type
) << std::endl
;
1804 auto ebliter
= ebl
.cbegin();
1806 pgb
.decode(ebliter
);
1807 spg_t pgid
= pgb
.pgid
;
1809 if (pgidstr
.length()) {
1812 bool ok
= user_pgid
.parse(pgidstr
.c_str());
1813 // This succeeded in main() already
1815 if (pgid
!= user_pgid
) {
1816 cerr
<< "specified pgid " << user_pgid
1817 << " does not match actual pgid " << pgid
<< std::endl
;
1822 if (!pgb
.superblock
.cluster_fsid
.is_zero()
1823 && pgb
.superblock
.cluster_fsid
!= sb
.cluster_fsid
) {
1824 cerr
<< "Export came from different cluster with fsid "
1825 << pgb
.superblock
.cluster_fsid
<< std::endl
;
1830 cerr
<< "Exported features: " << pgb
.superblock
.compat_features
<< std::endl
;
1833 // Special case: Old export has SHARDS incompat feature on replicated pg, removqqe it
1834 if (pgid
.is_no_shard())
1835 pgb
.superblock
.compat_features
.incompat
.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS
);
1837 if (sb
.compat_features
.compare(pgb
.superblock
.compat_features
) == -1) {
1838 CompatSet unsupported
= sb
.compat_features
.unsupported(pgb
.superblock
.compat_features
);
1840 cerr
<< "Export has incompatible features set " << unsupported
<< std::endl
;
1842 // Let them import if they specify the --force option
1844 return 11; // Positive return means exit status
1847 // we need the latest OSDMap to check for collisions
1850 ret
= get_osdmap(store
, sb
.current_epoch
, curmap
, bl
);
1852 cerr
<< "Can't find latest local OSDMap " << sb
.current_epoch
<< std::endl
;
1855 if (!curmap
.have_pg_pool(pgid
.pgid
.m_pool
)) {
1856 cerr
<< "Pool " << pgid
.pgid
.m_pool
<< " no longer exists" << std::endl
;
1857 // Special exit code for this error, used by test code
1858 return 10; // Positive return means exit status
1861 pool_pg_num_history_t pg_num_history
;
1862 get_pg_num_history(store
, &pg_num_history
);
1864 ghobject_t pgmeta_oid
= pgid
.make_pgmeta_oid();
1866 // Check for PG already present.
1868 if (store
->collection_exists(coll
)) {
1869 cerr
<< "pgid " << pgid
<< " already exists" << std::endl
;
1873 ObjectStore::CollectionHandle ch
;
1878 OSD::make_snapmapper_oid());
1879 SnapMapper
mapper(g_ceph_context
, &driver
, 0, 0, 0, pgid
.shard
);
1881 cout
<< "Importing pgid " << pgid
;
1885 bool found_metadata
= false;
1886 metadata_section ms
;
1888 ret
= read_section(&type
, &ebl
);
1893 cout
<< __func__
<< ": Section type " << std::to_string(type
) << std::endl
;
1895 if (type
>= END_OF_TYPES
) {
1896 cout
<< "Skipping unknown section type" << std::endl
;
1900 case TYPE_OBJECT_BEGIN
:
1901 ceph_assert(found_metadata
);
1902 ret
= get_object(store
, driver
, mapper
, coll
, ebl
, ms
.osdmap
,
1904 if (ret
) return ret
;
1906 case TYPE_PG_METADATA
:
1907 ret
= get_pg_metadata(store
, ebl
, ms
, sb
, pgid
);
1908 if (ret
) return ret
;
1909 found_metadata
= true;
1911 if (pgid
!= ms
.info
.pgid
) {
1912 cerr
<< "specified pgid " << pgid
<< " does not match import file pgid "
1913 << ms
.info
.pgid
<< std::endl
;
1917 // make sure there are no conflicting splits or merges
1918 if (ms
.osdmap
.have_pg_pool(pgid
.pgid
.pool())) {
1919 auto p
= pg_num_history
.pg_nums
.find(pgid
.pgid
.m_pool
);
1920 if (p
!= pg_num_history
.pg_nums
.end() &&
1921 !p
->second
.empty()) {
1922 unsigned start_pg_num
= ms
.osdmap
.get_pg_num(pgid
.pgid
.pool());
1923 unsigned pg_num
= start_pg_num
;
1924 for (auto q
= p
->second
.lower_bound(ms
.map_epoch
);
1925 q
!= p
->second
.end();
1927 unsigned new_pg_num
= q
->second
;
1928 cout
<< "pool " << pgid
.pgid
.pool() << " pg_num " << pg_num
1929 << " -> " << new_pg_num
<< std::endl
;
1931 // check for merge target
1933 if (pgid
.is_merge_source(pg_num
, new_pg_num
, &target
)) {
1934 // FIXME: this checks assumes the OSD's PG is at the OSD's
1935 // map epoch; it could be, say, at *our* epoch, pre-merge.
1936 coll_t
coll(target
);
1937 if (store
->collection_exists(coll
)) {
1938 cerr
<< "pgid " << pgid
<< " merges to target " << target
1939 << " which already exists" << std::endl
;
1944 // check for split children
1945 set
<spg_t
> children
;
1946 if (pgid
.is_split(start_pg_num
, new_pg_num
, &children
)) {
1947 cerr
<< " children are " << children
<< std::endl
;
1948 for (auto child
: children
) {
1950 if (store
->collection_exists(coll
)) {
1951 cerr
<< "pgid " << pgid
<< " splits to " << children
1952 << " and " << child
<< " exists" << std::endl
;
1957 pg_num
= new_pg_num
;
1961 cout
<< "pool " << pgid
.pgid
.pool() << " doesn't existing, not checking"
1962 << " for splits or mergers" << std::endl
;
1966 ObjectStore::Transaction t
;
1967 ch
= store
->create_new_collection(coll
);
1968 create_pg_collection(
1970 pgid
.get_split_bits(ms
.osdmap
.get_pg_pool(pgid
.pool())->get_pg_num()));
1971 init_pg_ondisk(t
, pgid
, NULL
);
1973 // mark this coll for removal until we're done
1974 map
<string
,bufferlist
> values
;
1975 encode((char)1, values
["_remove"]);
1976 t
.omap_setkeys(coll
, pgid
.make_pgmeta_oid(), values
);
1978 store
->queue_transaction(ch
, std::move(t
));
1983 ceph_assert(found_metadata
);
1987 cerr
<< "Unknown section type " << std::to_string(type
) << std::endl
;
1992 if (!found_metadata
) {
1993 cerr
<< "Missing metadata section" << std::endl
;
1997 ObjectStore::Transaction t
;
1999 pg_log_t newlog
, reject
;
2000 pg_log_t::filter_log(pgid
, ms
.osdmap
, g_ceph_context
->_conf
->osd_hit_set_namespace
,
2001 ms
.log
, newlog
, reject
);
2003 for (list
<pg_log_entry_t
>::iterator i
= newlog
.log
.begin();
2004 i
!= newlog
.log
.end(); ++i
)
2005 cerr
<< "Keeping log entry " << *i
<< std::endl
;
2006 for (list
<pg_log_entry_t
>::iterator i
= reject
.log
.begin();
2007 i
!= reject
.log
.end(); ++i
)
2008 cerr
<< "Skipping log entry " << *i
<< std::endl
;
2011 divergent_priors_t newdp
, rejectdp
;
2012 filter_divergent_priors(pgid
, ms
.osdmap
, g_ceph_context
->_conf
->osd_hit_set_namespace
,
2013 ms
.divergent_priors
, newdp
, rejectdp
);
2014 ms
.divergent_priors
= newdp
;
2016 for (divergent_priors_t::iterator i
= newdp
.begin();
2017 i
!= newdp
.end(); ++i
)
2018 cerr
<< "Keeping divergent_prior " << *i
<< std::endl
;
2019 for (divergent_priors_t::iterator i
= rejectdp
.begin();
2020 i
!= rejectdp
.end(); ++i
)
2021 cerr
<< "Skipping divergent_prior " << *i
<< std::endl
;
2024 ms
.missing
.filter_objects([&](const hobject_t
&obj
) {
2025 if (obj
.nspace
== g_ceph_context
->_conf
->osd_hit_set_namespace
)
2027 ceph_assert(!obj
.is_temp());
2028 object_t oid
= obj
.oid
;
2029 object_locator_t
loc(obj
);
2030 pg_t raw_pgid
= ms
.osdmap
.object_locator_to_pg(oid
, loc
);
2031 pg_t _pgid
= ms
.osdmap
.raw_pg_to_pg(raw_pgid
);
2033 return pgid
.pgid
!= _pgid
;
2038 pg_missing_t missing
;
2039 Formatter
*formatter
= Formatter::create("json-pretty");
2040 dump_log(formatter
, cerr
, newlog
, ms
.missing
);
2044 // Just like a split invalidate stats since the object count is changed
2045 if (skipped_objects
)
2046 ms
.info
.stats
.stats_invalid
= true;
2054 ms
.divergent_priors
,
2056 if (ret
) return ret
;
2059 // done, clear removal flag
2061 cerr
<< "done, clearing removal flag" << std::endl
;
2064 t
.omap_rmkey(coll
, pgid
.make_pgmeta_oid(), "_remove");
2065 wait_until_done(&t
, [&] {
2066 store
->queue_transaction(ch
, std::move(t
));
2067 // make sure we flush onreadable items before mapper/driver are destroyed.
2074 int do_list(ObjectStore
*store
, string pgidstr
, string object
, boost::optional
<std::string
> nspace
,
2075 Formatter
*formatter
, bool debug
, bool human_readable
, bool head
)
2078 lookup_ghobject
lookup(object
, nspace
, head
);
2079 if (pgidstr
.length() > 0) {
2080 r
= action_on_all_objects_in_pg(store
, pgidstr
, lookup
, debug
);
2082 r
= action_on_all_objects(store
, lookup
, debug
);
2086 lookup
.dump(formatter
, human_readable
);
2087 formatter
->flush(cout
);
2091 int do_list_slow(ObjectStore
*store
, string pgidstr
, string object
,
2092 double threshold
, Formatter
*formatter
, bool debug
, bool human_readable
)
2095 lookup_slow_ghobject
lookup(object
, threshold
);
2096 if (pgidstr
.length() > 0) {
2097 r
= action_on_all_objects_in_pg(store
, pgidstr
, lookup
, debug
);
2099 r
= action_on_all_objects(store
, lookup
, debug
);
2103 lookup
.dump(formatter
, human_readable
);
2104 formatter
->flush(cout
);
2108 int do_meta(ObjectStore
*store
, string object
, Formatter
*formatter
, bool debug
, bool human_readable
)
2111 boost::optional
<std::string
> nspace
; // Not specified
2112 lookup_ghobject
lookup(object
, nspace
);
2113 r
= action_on_all_objects_in_exact_pg(store
, coll_t::meta(), lookup
, debug
);
2116 lookup
.dump(formatter
, human_readable
);
2117 formatter
->flush(cout
);
2127 int remove_object(coll_t coll
, ghobject_t
&ghobj
,
2129 MapCacher::Transaction
<std::string
, bufferlist
> *_t
,
2130 ObjectStore::Transaction
*t
,
2133 if (type
== BOTH
|| type
== SNAPMAP
) {
2134 int r
= mapper
.remove_oid(ghobj
.hobj
, _t
);
2135 if (r
< 0 && r
!= -ENOENT
) {
2136 cerr
<< "remove_oid returned " << cpp_strerror(r
) << std::endl
;
2141 if (type
== BOTH
|| type
== NOSNAPMAP
) {
2142 t
->remove(coll
, ghobj
);
2147 int get_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, SnapSet
&ss
, bool silent
);
2149 int do_remove_object(ObjectStore
*store
, coll_t coll
,
2150 ghobject_t
&ghobj
, bool all
, bool force
, enum rmtype type
)
2152 auto ch
= store
->open_collection(coll
);
2154 coll
.is_pg_prefix(&pg
);
2158 OSD::make_snapmapper_oid());
2159 SnapMapper
mapper(g_ceph_context
, &driver
, 0, 0, 0, pg
.shard
);
2162 int r
= store
->stat(ch
, ghobj
, &st
);
2164 cerr
<< "remove: " << cpp_strerror(r
) << std::endl
;
2169 if (ghobj
.hobj
.has_snapset()) {
2170 r
= get_snapset(store
, coll
, ghobj
, ss
, false);
2172 cerr
<< "Can't get snapset error " << cpp_strerror(r
) << std::endl
;
2173 // If --force and bad snapset let them remove the head
2174 if (!(force
&& !all
))
2177 // cout << "snapset " << ss << std::endl;
2178 if (!ss
.clone_snaps
.empty() && !all
) {
2180 cout
<< "WARNING: only removing "
2181 << (ghobj
.hobj
.is_head() ? "head" : "snapdir")
2182 << " with clones present" << std::endl
;
2183 ss
.clone_snaps
.clear();
2185 cerr
<< "Clones are present, use removeall to delete everything"
2192 ObjectStore::Transaction t
;
2193 OSDriver::OSTransaction
_t(driver
.get_transaction(&t
));
2195 ghobject_t snapobj
= ghobj
;
2196 for (auto& p
: ss
.clone_snaps
) {
2197 snapobj
.hobj
.snap
= p
.first
;
2198 cout
<< "remove clone " << snapobj
<< std::endl
;
2200 r
= remove_object(coll
, snapobj
, mapper
, &_t
, &t
, type
);
2206 cout
<< "remove " << ghobj
<< std::endl
;
2209 r
= remove_object(coll
, ghobj
, mapper
, &_t
, &t
, type
);
2215 wait_until_done(&t
, [&] {
2216 store
->queue_transaction(ch
, std::move(t
));
2223 int do_list_attrs(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2225 auto ch
= store
->open_collection(coll
);
2226 map
<string
,bufferptr
,less
<>> aset
;
2227 int r
= store
->getattrs(ch
, ghobj
, aset
);
2229 cerr
<< "getattrs: " << cpp_strerror(r
) << std::endl
;
2233 for (map
<string
,bufferptr
>::iterator i
= aset
.begin();i
!= aset
.end(); ++i
) {
2234 string
key(i
->first
);
2236 key
= cleanbin(key
);
2237 cout
<< key
<< std::endl
;
2242 int do_list_omap(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2244 auto ch
= store
->open_collection(coll
);
2245 ObjectMap::ObjectMapIterator iter
= store
->get_omap_iterator(ch
, ghobj
);
2247 cerr
<< "omap_get_iterator: " << cpp_strerror(ENOENT
) << std::endl
;
2250 iter
->seek_to_first();
2251 map
<string
, bufferlist
> oset
;
2252 while(iter
->valid()) {
2253 get_omap_batch(iter
, oset
);
2255 for (map
<string
,bufferlist
>::iterator i
= oset
.begin();i
!= oset
.end(); ++i
) {
2256 string
key(i
->first
);
2258 key
= cleanbin(key
);
2259 cout
<< key
<< std::endl
;
2265 int do_get_bytes(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, int fd
)
2267 auto ch
= store
->open_collection(coll
);
2271 int ret
= store
->stat(ch
, ghobj
, &st
);
2273 cerr
<< "get-bytes: " << cpp_strerror(ret
) << std::endl
;
2279 cerr
<< "size=" << total
<< std::endl
;
2281 uint64_t offset
= 0;
2282 bufferlist rawdatabl
;
2285 mysize_t len
= max_read
;
2289 ret
= store
->read(ch
, ghobj
, offset
, len
, rawdatabl
);
2296 cerr
<< "data section offset=" << offset
<< " len=" << len
<< std::endl
;
2301 ret
= write(fd
, rawdatabl
.c_str(), ret
);
2311 int do_set_bytes(ObjectStore
*store
, coll_t coll
,
2312 ghobject_t
&ghobj
, int fd
)
2314 ObjectStore::Transaction tran
;
2315 ObjectStore::Transaction
*t
= &tran
;
2318 cerr
<< "Write " << ghobj
<< std::endl
;
2321 t
->touch(coll
, ghobj
);
2322 t
->truncate(coll
, ghobj
, 0);
2325 uint64_t offset
= 0;
2326 bufferlist rawdatabl
;
2329 ssize_t bytes
= rawdatabl
.read_fd(fd
, max_read
);
2331 cerr
<< "read_fd error " << cpp_strerror(bytes
) << std::endl
;
2339 cerr
<< "\tdata: offset " << offset
<< " bytes " << bytes
<< std::endl
;
2341 t
->write(coll
, ghobj
, offset
, bytes
, rawdatabl
);
2344 // XXX: Should we queue_transaction() every once in a while for very large files
2347 auto ch
= store
->open_collection(coll
);
2349 store
->queue_transaction(ch
, std::move(*t
));
2353 int do_get_attr(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, string key
)
2355 auto ch
= store
->open_collection(coll
);
2358 int r
= store
->getattr(ch
, ghobj
, key
.c_str(), bp
);
2360 cerr
<< "getattr: " << cpp_strerror(r
) << std::endl
;
2364 string
value(bp
.c_str(), bp
.length());
2366 value
= cleanbin(value
);
2367 value
.push_back('\n');
2374 int do_set_attr(ObjectStore
*store
, coll_t coll
,
2375 ghobject_t
&ghobj
, string key
, int fd
)
2377 ObjectStore::Transaction tran
;
2378 ObjectStore::Transaction
*t
= &tran
;
2382 cerr
<< "Setattr " << ghobj
<< std::endl
;
2384 int ret
= get_fd_data(fd
, bl
);
2391 t
->touch(coll
, ghobj
);
2393 t
->setattr(coll
, ghobj
, key
, bl
);
2395 auto ch
= store
->open_collection(coll
);
2396 store
->queue_transaction(ch
, std::move(*t
));
2400 int do_rm_attr(ObjectStore
*store
, coll_t coll
,
2401 ghobject_t
&ghobj
, string key
)
2403 ObjectStore::Transaction tran
;
2404 ObjectStore::Transaction
*t
= &tran
;
2407 cerr
<< "Rmattr " << ghobj
<< std::endl
;
2412 t
->rmattr(coll
, ghobj
, key
);
2414 auto ch
= store
->open_collection(coll
);
2415 store
->queue_transaction(ch
, std::move(*t
));
2419 int do_get_omap(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, string key
)
2421 auto ch
= store
->open_collection(coll
);
2423 map
<string
, bufferlist
> out
;
2427 int r
= store
->omap_get_values(ch
, ghobj
, keys
, &out
);
2429 cerr
<< "omap_get_values: " << cpp_strerror(r
) << std::endl
;
2434 cerr
<< "Key not found" << std::endl
;
2438 ceph_assert(out
.size() == 1);
2440 bufferlist bl
= out
.begin()->second
;
2441 string
value(bl
.c_str(), bl
.length());
2443 value
= cleanbin(value
);
2444 value
.push_back('\n');
2451 int do_set_omap(ObjectStore
*store
, coll_t coll
,
2452 ghobject_t
&ghobj
, string key
, int fd
)
2454 ObjectStore::Transaction tran
;
2455 ObjectStore::Transaction
*t
= &tran
;
2456 map
<string
, bufferlist
> attrset
;
2460 cerr
<< "Set_omap " << ghobj
<< std::endl
;
2462 int ret
= get_fd_data(fd
, valbl
);
2466 attrset
.insert(pair
<string
, bufferlist
>(key
, valbl
));
2471 t
->touch(coll
, ghobj
);
2473 t
->omap_setkeys(coll
, ghobj
, attrset
);
2475 auto ch
= store
->open_collection(coll
);
2476 store
->queue_transaction(ch
, std::move(*t
));
2480 int do_rm_omap(ObjectStore
*store
, coll_t coll
,
2481 ghobject_t
&ghobj
, string key
)
2483 ObjectStore::Transaction tran
;
2484 ObjectStore::Transaction
*t
= &tran
;
2487 cerr
<< "Rm_omap " << ghobj
<< std::endl
;
2492 t
->omap_rmkey(coll
, ghobj
, key
);
2494 auto ch
= store
->open_collection(coll
);
2495 store
->queue_transaction(ch
, std::move(*t
));
2499 int do_get_omaphdr(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2501 auto ch
= store
->open_collection(coll
);
2504 int r
= store
->omap_get_header(ch
, ghobj
, &hdrbl
, true);
2506 cerr
<< "omap_get_header: " << cpp_strerror(r
) << std::endl
;
2510 string
header(hdrbl
.c_str(), hdrbl
.length());
2512 header
= cleanbin(header
);
2513 header
.push_back('\n');
2520 int do_set_omaphdr(ObjectStore
*store
, coll_t coll
,
2521 ghobject_t
&ghobj
, int fd
)
2523 ObjectStore::Transaction tran
;
2524 ObjectStore::Transaction
*t
= &tran
;
2528 cerr
<< "Omap_setheader " << ghobj
<< std::endl
;
2530 int ret
= get_fd_data(fd
, hdrbl
);
2537 t
->touch(coll
, ghobj
);
2539 t
->omap_setheader(coll
, ghobj
, hdrbl
);
2541 auto ch
= store
->open_collection(coll
);
2542 store
->queue_transaction(ch
, std::move(*t
));
2546 struct do_fix_lost
: public action_on_object_t
{
2547 void call(ObjectStore
*store
, coll_t coll
,
2548 ghobject_t
&ghobj
, object_info_t
&oi
) override
{
2550 cout
<< coll
<< "/" << ghobj
<< " is lost";
2556 oi
.clear_flag(object_info_t::FLAG_LOST
);
2558 encode(oi
, bl
, -1); /* fixme: using full features */
2559 ObjectStore::Transaction t
;
2560 t
.setattr(coll
, ghobj
, OI_ATTR
, bl
);
2561 auto ch
= store
->open_collection(coll
);
2562 store
->queue_transaction(ch
, std::move(t
));
2568 int get_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, SnapSet
&ss
, bool silent
= false)
2570 auto ch
= store
->open_collection(coll
);
2572 int r
= store
->getattr(ch
, ghobj
, SS_ATTR
, attr
);
2575 cerr
<< "Error getting snapset on : " << make_pair(coll
, ghobj
) << ", "
2576 << cpp_strerror(r
) << std::endl
;
2579 auto bp
= attr
.cbegin();
2584 cerr
<< "Error decoding snapset on : " << make_pair(coll
, ghobj
) << ", "
2585 << cpp_strerror(r
) << std::endl
;
2591 int print_obj_info(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, Formatter
* formatter
)
2593 auto ch
= store
->open_collection(coll
);
2595 formatter
->open_object_section("obj");
2596 formatter
->open_object_section("id");
2597 ghobj
.dump(formatter
);
2598 formatter
->close_section();
2601 int gr
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2604 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2605 << cpp_strerror(r
) << std::endl
;
2608 auto bp
= attr
.cbegin();
2611 formatter
->open_object_section("info");
2613 formatter
->close_section();
2616 cerr
<< "Error decoding attr on : " << make_pair(coll
, ghobj
) << ", "
2617 << cpp_strerror(r
) << std::endl
;
2621 int sr
= store
->stat(ch
, ghobj
, &st
, true);
2624 cerr
<< "Error stat on : " << make_pair(coll
, ghobj
) << ", "
2625 << cpp_strerror(r
) << std::endl
;
2627 formatter
->open_object_section("stat");
2628 formatter
->dump_int("size", st
.st_size
);
2629 formatter
->dump_int("blksize", st
.st_blksize
);
2630 formatter
->dump_int("blocks", st
.st_blocks
);
2631 formatter
->dump_int("nlink", st
.st_nlink
);
2632 formatter
->close_section();
2635 if (ghobj
.hobj
.has_snapset()) {
2637 int snr
= get_snapset(store
, coll
, ghobj
, ss
);
2641 formatter
->open_object_section("SnapSet");
2643 formatter
->close_section();
2647 gr
= store
->getattr(ch
, ghobj
, ECUtil::get_hinfo_key(), hattr
);
2649 ECUtil::HashInfo hinfo
;
2650 auto hp
= hattr
.cbegin();
2653 formatter
->open_object_section("hinfo");
2654 hinfo
.dump(formatter
);
2655 formatter
->close_section();
2658 cerr
<< "Error decoding hinfo on : " << make_pair(coll
, ghobj
) << ", "
2659 << cpp_strerror(r
) << std::endl
;
2662 gr
= store
->dump_onode(ch
, ghobj
, "onode", formatter
);
2664 formatter
->close_section();
2665 formatter
->flush(cout
);
2670 int corrupt_info(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, Formatter
* formatter
)
2672 auto ch
= store
->open_collection(coll
);
2674 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2676 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2677 << cpp_strerror(r
) << std::endl
;
2681 auto bp
= attr
.cbegin();
2686 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2687 << cpp_strerror(r
) << std::endl
;
2692 oi
.alloc_hint_flags
+= 0xff;
2693 ObjectStore::Transaction t
;
2694 encode(oi
, attr
, -1); /* fixme: using full features */
2695 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2696 auto ch
= store
->open_collection(coll
);
2697 r
= store
->queue_transaction(ch
, std::move(t
));
2699 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2700 << cpp_strerror(r
) << std::endl
;
2708 ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, uint64_t setsize
, Formatter
* formatter
,
2711 auto ch
= store
->open_collection(coll
);
2712 if (ghobj
.hobj
.is_snapdir()) {
2713 cerr
<< "Can't set the size of a snapdir" << std::endl
;
2717 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2719 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2720 << cpp_strerror(r
) << std::endl
;
2724 auto bp
= attr
.cbegin();
2729 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2730 << cpp_strerror(r
) << std::endl
;
2734 r
= store
->stat(ch
, ghobj
, &st
, true);
2736 cerr
<< "Error stat on : " << make_pair(coll
, ghobj
) << ", "
2737 << cpp_strerror(r
) << std::endl
;
2739 ghobject_t
head(ghobj
);
2741 bool found_head
= true;
2742 map
<snapid_t
, uint64_t>::iterator csi
;
2743 bool is_snap
= ghobj
.hobj
.is_snap();
2745 head
.hobj
= head
.hobj
.get_head();
2746 r
= get_snapset(store
, coll
, head
, ss
, true);
2747 if (r
< 0 && r
!= -ENOENT
) {
2748 // Requested get_snapset() silent, so if not -ENOENT show error
2749 cerr
<< "Error getting snapset on : " << make_pair(coll
, head
) << ", "
2750 << cpp_strerror(r
) << std::endl
;
2754 head
.hobj
= head
.hobj
.get_snapdir();
2755 r
= get_snapset(store
, coll
, head
, ss
);
2762 csi
= ss
.clone_size
.find(ghobj
.hobj
.snap
);
2763 if (csi
== ss
.clone_size
.end()) {
2764 cerr
<< "SnapSet is missing clone_size for snap " << ghobj
.hobj
.snap
<< std::endl
;
2768 if ((uint64_t)st
.st_size
== setsize
&& oi
.size
== setsize
2769 && (!is_snap
|| csi
->second
== setsize
)) {
2770 cout
<< "Size of object is already " << setsize
<< std::endl
;
2773 cout
<< "Setting size to " << setsize
<< ", stat size " << st
.st_size
2774 << ", obj info size " << oi
.size
;
2776 cout
<< ", " << (found_head
? "head" : "snapdir")
2777 << " clone_size " << csi
->second
;
2778 csi
->second
= setsize
;
2784 ObjectStore::Transaction t
;
2785 // Only modify object info if we want to corrupt it
2786 if (!corrupt
&& (uint64_t)st
.st_size
!= setsize
) {
2787 t
.truncate(coll
, ghobj
, setsize
);
2788 // Changing objectstore size will invalidate data_digest, so clear it.
2789 oi
.clear_data_digest();
2791 encode(oi
, attr
, -1); /* fixme: using full features */
2792 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2794 bufferlist snapattr
;
2796 encode(ss
, snapattr
);
2797 t
.setattr(coll
, head
, SS_ATTR
, snapattr
);
2799 auto ch
= store
->open_collection(coll
);
2800 r
= store
->queue_transaction(ch
, std::move(t
));
2802 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2803 << cpp_strerror(r
) << std::endl
;
2810 int clear_data_digest(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
) {
2811 auto ch
= store
->open_collection(coll
);
2813 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2815 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2816 << cpp_strerror(r
) << std::endl
;
2820 auto bp
= attr
.cbegin();
2825 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2826 << cpp_strerror(r
) << std::endl
;
2831 oi
.clear_data_digest();
2832 encode(oi
, attr
, -1); /* fixme: using full features */
2833 ObjectStore::Transaction t
;
2834 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2835 auto ch
= store
->open_collection(coll
);
2836 r
= store
->queue_transaction(ch
, std::move(t
));
2838 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2839 << cpp_strerror(r
) << std::endl
;
2846 int clear_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
,
2850 int ret
= get_snapset(store
, coll
, ghobj
, ss
);
2854 // Use "corrupt" to clear entire SnapSet
2855 // Use "seq" to just corrupt SnapSet.seq
2856 if (arg
== "corrupt" || arg
== "seq")
2858 // Use "snaps" to just clear SnapSet.clone_snaps
2859 if (arg
== "corrupt" || arg
== "snaps")
2860 ss
.clone_snaps
.clear();
2861 // By default just clear clone, clone_overlap and clone_size
2862 if (arg
== "corrupt")
2864 if (arg
== "" || arg
== "clones")
2866 if (arg
== "" || arg
== "clone_overlap")
2867 ss
.clone_overlap
.clear();
2868 if (arg
== "" || arg
== "clone_size")
2869 ss
.clone_size
.clear();
2870 // Break all clone sizes by adding 1
2871 if (arg
== "size") {
2872 for (map
<snapid_t
, uint64_t>::iterator i
= ss
.clone_size
.begin();
2873 i
!= ss
.clone_size
.end(); ++i
)
2880 ObjectStore::Transaction t
;
2881 t
.setattr(coll
, ghobj
, SS_ATTR
, bl
);
2882 auto ch
= store
->open_collection(coll
);
2883 int r
= store
->queue_transaction(ch
, std::move(t
));
2885 cerr
<< "Error setting snapset on : " << make_pair(coll
, ghobj
) << ", "
2886 << cpp_strerror(r
) << std::endl
;
2893 vector
<snapid_t
>::iterator
find(vector
<snapid_t
> &v
, snapid_t clid
)
2895 return std::find(v
.begin(), v
.end(), clid
);
2898 map
<snapid_t
, interval_set
<uint64_t> >::iterator
2899 find(map
<snapid_t
, interval_set
<uint64_t> > &m
, snapid_t clid
)
2901 return m
.find(clid
);
2904 map
<snapid_t
, uint64_t>::iterator
find(map
<snapid_t
, uint64_t> &m
,
2907 return m
.find(clid
);
2911 int remove_from(T
&mv
, string name
, snapid_t cloneid
, bool force
)
2913 typename
T::iterator i
= find(mv
, cloneid
);
2914 if (i
!= mv
.end()) {
2917 cerr
<< "Clone " << cloneid
<< " doesn't exist in " << name
;
2919 cerr
<< " (ignored)" << std::endl
;
2929 ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, snapid_t cloneid
, bool force
)
2931 // XXX: Don't allow this if in a cache tier or former cache tier
2932 // bool allow_incomplete_clones() const {
2933 // return cache_mode != CACHEMODE_NONE || has_flag(FLAG_INCOMPLETE_CLONES);
2936 int ret
= get_snapset(store
, coll
, ghobj
, snapset
);
2940 // Derived from trim_object()
2942 vector
<snapid_t
>::iterator p
;
2943 for (p
= snapset
.clones
.begin(); p
!= snapset
.clones
.end(); ++p
)
2946 if (p
== snapset
.clones
.end()) {
2947 cerr
<< "Clone " << cloneid
<< " not present";
2950 if (p
!= snapset
.clones
.begin()) {
2951 // not the oldest... merge overlap into next older clone
2952 vector
<snapid_t
>::iterator n
= p
- 1;
2953 hobject_t prev_coid
= ghobj
.hobj
;
2954 prev_coid
.snap
= *n
;
2955 //bool adjust_prev_bytes = is_present_clone(prev_coid);
2957 //if (adjust_prev_bytes)
2958 // ctx->delta_stats.num_bytes -= snapset.get_clone_bytes(*n);
2960 snapset
.clone_overlap
[*n
].intersection_of(
2961 snapset
.clone_overlap
[*p
]);
2963 //if (adjust_prev_bytes)
2964 // ctx->delta_stats.num_bytes += snapset.get_clone_bytes(*n);
2967 ret
= remove_from(snapset
.clones
, "clones", cloneid
, force
);
2968 if (ret
) return ret
;
2969 ret
= remove_from(snapset
.clone_overlap
, "clone_overlap", cloneid
, force
);
2970 if (ret
) return ret
;
2971 ret
= remove_from(snapset
.clone_size
, "clone_size", cloneid
, force
);
2972 if (ret
) return ret
;
2978 encode(snapset
, bl
);
2979 ObjectStore::Transaction t
;
2980 t
.setattr(coll
, ghobj
, SS_ATTR
, bl
);
2981 auto ch
= store
->open_collection(coll
);
2982 int r
= store
->queue_transaction(ch
, std::move(t
));
2984 cerr
<< "Error setting snapset on : " << make_pair(coll
, ghobj
) << ", "
2985 << cpp_strerror(r
) << std::endl
;
2988 cout
<< "Removal of clone " << cloneid
<< " complete" << std::endl
;
2989 cout
<< "Use pg repair after OSD restarted to correct stat information" << std::endl
;
2993 int dup(string srcpath
, ObjectStore
*src
, string dstpath
, ObjectStore
*dst
)
2995 cout
<< "dup from " << src
->get_type() << ": " << srcpath
<< "\n"
2996 << " to " << dst
->get_type() << ": " << dstpath
2999 vector
<coll_t
> collections
;
3004 cerr
<< "failed to mount src: " << cpp_strerror(r
) << std::endl
;
3009 cerr
<< "failed to mount dst: " << cpp_strerror(r
) << std::endl
;
3013 if (src
->get_fsid() != dst
->get_fsid()) {
3014 cerr
<< "src fsid " << src
->get_fsid() << " != dest " << dst
->get_fsid()
3018 cout
<< "fsid " << src
->get_fsid() << std::endl
;
3020 // make sure dst is empty
3021 r
= dst
->list_collections(collections
);
3023 cerr
<< "error listing collections on dst: " << cpp_strerror(r
) << std::endl
;
3026 if (!collections
.empty()) {
3027 cerr
<< "destination store is not empty" << std::endl
;
3031 r
= src
->list_collections(collections
);
3033 cerr
<< "error listing collections on src: " << cpp_strerror(r
) << std::endl
;
3037 num
= collections
.size();
3038 cout
<< num
<< " collections" << std::endl
;
3040 for (auto cid
: collections
) {
3041 cout
<< i
++ << "/" << num
<< " " << cid
<< std::endl
;
3042 auto ch
= src
->open_collection(cid
);
3043 auto dch
= dst
->create_new_collection(cid
);
3045 ObjectStore::Transaction t
;
3046 int bits
= src
->collection_bits(ch
);
3048 if (src
->get_type() == "filestore" && cid
.is_meta()) {
3051 cerr
<< "cannot get bit count for collection " << cid
<< ": "
3052 << cpp_strerror(bits
) << std::endl
;
3056 t
.create_collection(cid
, bits
);
3057 dst
->queue_transaction(dch
, std::move(t
));
3062 uint64_t bytes
= 0, keys
= 0;
3064 vector
<ghobject_t
> ls
;
3065 r
= src
->collection_list(ch
, pos
, ghobject_t::get_max(), 1000, &ls
, &pos
);
3067 cerr
<< "collection_list on " << cid
<< " from " << pos
<< " got: "
3068 << cpp_strerror(r
) << std::endl
;
3075 for (auto& oid
: ls
) {
3076 //cout << " " << cid << " " << oid << std::endl;
3078 cout
<< " " << std::setw(16) << n
<< " objects, "
3079 << std::setw(16) << bytes
<< " bytes, "
3080 << std::setw(16) << keys
<< " keys"
3081 << std::setw(1) << "\r" << std::flush
;
3085 ObjectStore::Transaction t
;
3088 map
<string
,bufferptr
,less
<>> attrs
;
3089 src
->getattrs(ch
, oid
, attrs
);
3090 if (!attrs
.empty()) {
3091 t
.setattrs(cid
, oid
, attrs
);
3095 src
->read(ch
, oid
, 0, 0, bl
);
3097 t
.write(cid
, oid
, 0, bl
.length(), bl
);
3098 bytes
+= bl
.length();
3102 map
<string
,bufferlist
> omap
;
3103 src
->omap_get(ch
, oid
, &header
, &omap
);
3104 if (header
.length()) {
3105 t
.omap_setheader(cid
, oid
, header
);
3108 if (!omap
.empty()) {
3109 keys
+= omap
.size();
3110 t
.omap_setkeys(cid
, oid
, omap
);
3113 dst
->queue_transaction(dch
, std::move(t
));
3116 cout
<< " " << std::setw(16) << n
<< " objects, "
3117 << std::setw(16) << bytes
<< " bytes, "
3118 << std::setw(16) << keys
<< " keys"
3119 << std::setw(1) << std::endl
;
3123 cout
<< "keyring" << std::endl
;
3126 string s
= srcpath
+ "/keyring";
3128 r
= bl
.read_file(s
.c_str(), &err
);
3130 cerr
<< "failed to copy " << s
<< ": " << err
<< std::endl
;
3132 string d
= dstpath
+ "/keyring";
3133 bl
.write_file(d
.c_str(), 0600);
3138 cout
<< "duping osd metadata" << std::endl
;
3140 for (auto k
: {"magic", "whoami", "ceph_fsid", "fsid"}) {
3142 src
->read_meta(k
, &val
);
3143 dst
->write_meta(k
, val
);
3147 dst
->write_meta("ready", "ready");
3149 cout
<< "done." << std::endl
;
3158 void usage(po::options_description
&desc
)
3161 cerr
<< desc
<< std::endl
;
3163 cerr
<< "Positional syntax:" << std::endl
;
3165 cerr
<< "ceph-objectstore-tool ... <object> (get|set)-bytes [file]" << std::endl
;
3166 cerr
<< "ceph-objectstore-tool ... <object> set-(attr|omap) <key> [file]" << std::endl
;
3167 cerr
<< "ceph-objectstore-tool ... <object> (get|rm)-(attr|omap) <key>" << std::endl
;
3168 cerr
<< "ceph-objectstore-tool ... <object> get-omaphdr" << std::endl
;
3169 cerr
<< "ceph-objectstore-tool ... <object> set-omaphdr [file]" << std::endl
;
3170 cerr
<< "ceph-objectstore-tool ... <object> list-attrs" << std::endl
;
3171 cerr
<< "ceph-objectstore-tool ... <object> list-omap" << std::endl
;
3172 cerr
<< "ceph-objectstore-tool ... <object> remove|removeall" << std::endl
;
3173 cerr
<< "ceph-objectstore-tool ... <object> dump" << std::endl
;
3174 cerr
<< "ceph-objectstore-tool ... <object> set-size" << std::endl
;
3175 cerr
<< "ceph-objectstore-tool ... <object> clear-data-digest" << std::endl
;
3176 cerr
<< "ceph-objectstore-tool ... <object> remove-clone-metadata <cloneid>" << std::endl
;
3178 cerr
<< "<object> can be a JSON object description as displayed" << std::endl
;
3179 cerr
<< "by --op list." << std::endl
;
3180 cerr
<< "<object> can be an object name which will be looked up in all" << std::endl
;
3181 cerr
<< "the OSD's PGs." << std::endl
;
3182 cerr
<< "<object> can be the empty string ('') which with a provided pgid " << std::endl
;
3183 cerr
<< "specifies the pgmeta object" << std::endl
;
3185 cerr
<< "The optional [file] argument will read stdin or write stdout" << std::endl
;
3186 cerr
<< "if not specified or if '-' specified." << std::endl
;
3189 bool ends_with(const string
& check
, const string
& ending
)
3191 return check
.size() >= ending
.size() && check
.rfind(ending
) == (check
.size() - ending
.size());
3194 // Based on FileStore::dump_journal(), set-up enough to only dump
3195 int mydump_journal(Formatter
*f
, string journalpath
, bool m_journal_dio
)
3199 if (!journalpath
.length())
3202 FileJournal
*journal
= new FileJournal(g_ceph_context
, uuid_d(), NULL
, NULL
,
3203 journalpath
.c_str(), m_journal_dio
);
3204 r
= journal
->_fdump(*f
, false);
3209 int apply_layout_settings(ObjectStore
*os
, const OSDSuperblock
&superblock
,
3210 const string
&pool_name
, const spg_t
&pgid
, bool dry_run
,
3215 FileStore
*fs
= dynamic_cast<FileStore
*>(os
);
3217 cerr
<< "Nothing to do for non-filestore backend" << std::endl
;
3218 return 0; // making this return success makes testing easier
3223 r
= get_osdmap(os
, superblock
.current_epoch
, curmap
, bl
);
3225 cerr
<< "Can't find local OSDMap: " << cpp_strerror(r
) << std::endl
;
3229 int64_t poolid
= -1;
3230 if (pool_name
.length()) {
3231 poolid
= curmap
.lookup_pg_pool_name(pool_name
);
3233 cerr
<< "Couldn't find pool " << pool_name
<< ": " << cpp_strerror(poolid
)
3239 vector
<coll_t
> collections
, filtered_colls
;
3240 r
= os
->list_collections(collections
);
3242 cerr
<< "Error listing collections: " << cpp_strerror(r
) << std::endl
;
3246 for (auto const &coll
: collections
) {
3248 if (coll
.is_pg(&coll_pgid
) &&
3249 ((poolid
>= 0 && coll_pgid
.pool() == (uint64_t)poolid
) ||
3250 coll_pgid
== pgid
)) {
3251 filtered_colls
.push_back(coll
);
3255 size_t done
= 0, total
= filtered_colls
.size();
3256 for (auto const &coll
: filtered_colls
) {
3258 cerr
<< "Would apply layout settings to " << coll
<< std::endl
;
3260 cerr
<< "Finished " << done
<< "/" << total
<< " collections" << "\r";
3261 r
= fs
->apply_layout_settings(coll
, target_level
);
3263 cerr
<< "Error applying layout settings to " << coll
<< std::endl
;
3270 cerr
<< "Finished " << total
<< "/" << total
<< " collections" << "\r" << std::endl
;
3274 int main(int argc
, char **argv
)
3276 string dpath
, jpath
, pgidstr
, op
, file
, mountpoint
, mon_store_path
, object
;
3277 string target_data_path
, fsid
;
3278 string objcmd
, arg1
, arg2
, type
, format
, argnspace
, pool
, rmtypestr
;
3279 boost::optional
<std::string
> nspace
;
3282 unsigned slow_threshold
= 16;
3284 bool human_readable
;
3285 Formatter
*formatter
;
3288 po::options_description
desc("Allowed options");
3290 ("help", "produce help message")
3291 ("type", po::value
<string
>(&type
),
3292 "Arg is one of [bluestore (default), filestore, memstore]")
3293 ("data-path", po::value
<string
>(&dpath
),
3294 "path to object store, mandatory")
3295 ("journal-path", po::value
<string
>(&jpath
),
3296 "path to journal, use if tool can't find it")
3297 ("pgid", po::value
<string
>(&pgidstr
),
3298 "PG id, mandatory for info, log, remove, export, export-remove, mark-complete, trim-pg-log, trim-pg-log-dups and mandatory for apply-layout-settings if --pool is not specified")
3299 ("pool", po::value
<string
>(&pool
),
3300 "Pool name, mandatory for apply-layout-settings if --pgid is not specified")
3301 ("op", po::value
<string
>(&op
),
3302 "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, "
3303 "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, trim-pg-log-dups statfs]")
3304 ("epoch", po::value
<unsigned>(&epoch
),
3305 "epoch# for get-osdmap and get-inc-osdmap, the current epoch in use if not specified")
3306 ("file", po::value
<string
>(&file
),
3307 "path of file to export, export-remove, import, get-osdmap, set-osdmap, get-inc-osdmap or set-inc-osdmap")
3308 ("mon-store-path", po::value
<string
>(&mon_store_path
),
3309 "path of monstore to update-mon-db")
3310 ("fsid", po::value
<string
>(&fsid
),
3311 "fsid for new store created by mkfs")
3312 ("target-data-path", po::value
<string
>(&target_data_path
),
3313 "path of target object store (for --op dup)")
3314 ("mountpoint", po::value
<string
>(&mountpoint
),
3316 ("format", po::value
<string
>(&format
)->default_value("json-pretty"),
3317 "Output format which may be json, json-pretty, xml, xml-pretty")
3318 ("debug", "Enable diagnostic output to stderr")
3319 ("no-mon-config", "Do not contact mons for config")
3320 ("no-superblock", "Do not read superblock")
3321 ("force", "Ignore some types of errors and proceed with operation - USE WITH CAUTION: CORRUPTION POSSIBLE NOW OR IN THE FUTURE")
3322 ("skip-journal-replay", "Disable journal replay")
3323 ("skip-mount-omap", "Disable mounting of omap")
3324 ("head", "Find head/snapdir when searching for objects by name")
3325 ("dry-run", "Don't modify the objectstore")
3326 ("tty", "Treat stdout as a tty (no binary data)")
3327 ("namespace", po::value
<string
>(&argnspace
), "Specify namespace when searching for objects")
3328 ("rmtype", po::value
<string
>(&rmtypestr
), "Specify corrupting object removal 'snapmap' or 'nosnapmap' - TESTING USE ONLY")
3329 ("slow-omap-threshold", po::value
<unsigned>(&slow_threshold
),
3330 "Threshold (in seconds) to consider omap listing slow (for op=list-slow-omap)")
3333 po::options_description
positional("Positional options");
3334 positional
.add_options()
3335 ("object", po::value
<string
>(&object
), "'' for pgmeta_oid, object name or ghobject in json")
3336 ("objcmd", po::value
<string
>(&objcmd
), "command [(get|set)-bytes, (get|set|rm)-(attr|omap), (get|set)-omaphdr, list-attrs, list-omap, remove]")
3337 ("arg1", po::value
<string
>(&arg1
), "arg1 based on cmd")
3338 ("arg2", po::value
<string
>(&arg2
), "arg2 based on cmd")
3341 po::options_description all
;
3342 all
.add(desc
).add(positional
);
3344 po::positional_options_description pd
;
3345 pd
.add("object", 1).add("objcmd", 1).add("arg1", 1).add("arg2", 1);
3347 vector
<string
> ceph_option_strings
;
3349 po::variables_map vm
;
3351 po::parsed_options parsed
=
3352 po::command_line_parser(argc
, argv
).options(all
).allow_unregistered().positional(pd
).run();
3353 po::store( parsed
, vm
);
3355 ceph_option_strings
= po::collect_unrecognized(parsed
.options
,
3356 po::include_positional
);
3357 } catch(po::error
&e
) {
3358 std::cerr
<< e
.what() << std::endl
;
3362 if (vm
.count("help")) {
3367 // Compatibility with previous option name
3368 if (op
== "dump-import")
3371 debug
= (vm
.count("debug") > 0);
3373 force
= (vm
.count("force") > 0);
3375 no_superblock
= (vm
.count("no-superblock") > 0);
3377 if (vm
.count("namespace"))
3380 dry_run
= (vm
.count("dry-run") > 0);
3381 tty
= (vm
.count("tty") > 0);
3383 osflagbits_t flags
= 0;
3384 if (dry_run
|| vm
.count("skip-journal-replay"))
3385 flags
|= SKIP_JOURNAL_REPLAY
;
3386 if (vm
.count("skip-mount-omap"))
3387 flags
|= SKIP_MOUNT_OMAP
;
3388 if (op
== "update-mon-db")
3389 flags
|= SKIP_JOURNAL_REPLAY
;
3391 head
= (vm
.count("head") > 0);
3393 // infer osd id so we can authenticate
3395 snprintf(fn
, sizeof(fn
), "%s/whoami", dpath
.c_str());
3396 int fd
= ::open(fn
, O_RDONLY
);
3400 string
s(bl
.c_str(), bl
.length());
3401 int whoami
= atoi(s
.c_str());
3403 // identify ourselves as this osd so we can auth and fetch our configs
3404 tmp
.push_back("-n");
3405 tmp
.push_back(string("osd.") + stringify(whoami
));
3406 // populate osd_data so that the default keyring location works
3407 tmp
.push_back("--osd-data");
3408 tmp
.push_back(dpath
);
3409 tmp
.insert(tmp
.end(), ceph_option_strings
.begin(),
3410 ceph_option_strings
.end());
3411 tmp
.swap(ceph_option_strings
);
3414 vector
<const char *> ceph_options
;
3415 ceph_options
.reserve(ceph_options
.size() + ceph_option_strings
.size());
3416 for (vector
<string
>::iterator i
= ceph_option_strings
.begin();
3417 i
!= ceph_option_strings
.end();
3419 ceph_options
.push_back(i
->c_str());
3422 snprintf(fn
, sizeof(fn
), "%s/type", dpath
.c_str());
3423 fd
= ::open(fn
, O_RDONLY
);
3428 string dp_type
= string(bl
.c_str(), bl
.length() - 1); // drop \n
3429 if (vm
.count("type") && dp_type
!= "" && type
!= dp_type
)
3430 cerr
<< "WARNING: Ignoring type \"" << type
<< "\" - found data-path type \""
3431 << dp_type
<< "\"" << std::endl
;
3433 //cout << "object store type is " << type << std::endl;
3438 if (!vm
.count("type") && type
== "") {
3441 if (!vm
.count("data-path") &&
3442 op
!= "dump-export" &&
3443 !(op
== "dump-journal" && type
== "filestore")) {
3444 cerr
<< "Must provide --data-path" << std::endl
;
3448 if (type
== "filestore" && !vm
.count("journal-path")) {
3449 jpath
= dpath
+ "/journal";
3451 if (!vm
.count("op") && !vm
.count("object")) {
3452 cerr
<< "Must provide --op or object command..." << std::endl
;
3456 if (op
!= "list" && op
!= "apply-layout-settings" &&
3457 vm
.count("op") && vm
.count("object")) {
3458 cerr
<< "Can't specify both --op and object command syntax" << std::endl
;
3462 if (op
== "apply-layout-settings" && !(vm
.count("pool") ^ vm
.count("pgid"))) {
3463 cerr
<< "apply-layout-settings requires either --pool or --pgid"
3468 if (op
!= "list" && op
!= "apply-layout-settings" && vm
.count("object") && !vm
.count("objcmd")) {
3469 cerr
<< "Invalid syntax, missing command" << std::endl
;
3473 if (op
== "fuse" && mountpoint
.length() == 0) {
3474 cerr
<< "Missing fuse mountpoint" << std::endl
;
3478 outistty
= isatty(STDOUT_FILENO
) || tty
;
3481 if ((op
== "export" || op
== "export-remove" || op
== "get-osdmap" || op
== "get-inc-osdmap") && !dry_run
) {
3482 if (!vm
.count("file") || file
== "-") {
3484 cerr
<< "stdout is a tty and no --file filename specified" << std::endl
;
3487 file_fd
= STDOUT_FILENO
;
3489 file_fd
= open(file
.c_str(), O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
3491 } else if (op
== "import" || op
== "dump-export" || op
== "set-osdmap" || op
== "set-inc-osdmap") {
3492 if (!vm
.count("file") || file
== "-") {
3493 if (isatty(STDIN_FILENO
)) {
3494 cerr
<< "stdin is a tty and no --file filename specified" << std::endl
;
3497 file_fd
= STDIN_FILENO
;
3499 file_fd
= open(file
.c_str(), O_RDONLY
);
3503 ObjectStoreTool tool
= ObjectStoreTool(file_fd
, dry_run
);
3505 if (vm
.count("file") && file_fd
== fd_none
&& !dry_run
) {
3506 cerr
<< "--file option only applies to import, dump-export, export, export-remove, "
3507 << "get-osdmap, set-osdmap, get-inc-osdmap or set-inc-osdmap" << std::endl
;
3511 if (file_fd
!= fd_none
&& file_fd
< 0) {
3512 string err
= string("file: ") + file
;
3513 perror(err
.c_str());
3517 if (vm
.count("no-mon-config") > 0) {
3518 init_flags
|= CINIT_FLAG_NO_MON_CONFIG
;
3521 auto cct
= global_init(
3523 CEPH_ENTITY_TYPE_OSD
,
3524 CODE_ENVIRONMENT_UTILITY_NODOUT
,
3526 common_init_finish(g_ceph_context
);
3528 g_conf().set_val_or_die("log_to_stderr", "true");
3529 g_conf().set_val_or_die("err_to_stderr", "true");
3531 g_conf().apply_changes(nullptr);
3533 // Special list handling. Treating pretty_format as human readable,
3534 // with one object per line and not an enclosing array.
3535 human_readable
= ends_with(format
, "-pretty");
3536 if ((op
== "list" || op
== "meta-list") && human_readable
) {
3537 // Remove -pretty from end of format which we know is there
3538 format
= format
.substr(0, format
.size() - strlen("-pretty"));
3541 formatter
= Formatter::create(format
);
3542 if (formatter
== NULL
) {
3543 cerr
<< "unrecognized format: " << format
<< std::endl
;
3547 // Special handling for filestore journal, so we can dump it without mounting
3548 if (op
== "dump-journal" && type
== "filestore") {
3549 int ret
= mydump_journal(formatter
, jpath
, g_conf()->journal_dio
);
3551 cerr
<< "journal-path: " << jpath
<< ": "
3552 << cpp_strerror(ret
) << std::endl
;
3555 formatter
->flush(cout
);
3559 if (op
== "dump-export") {
3560 int ret
= tool
.dump_export(formatter
);
3562 cerr
<< "dump-export: "
3563 << cpp_strerror(ret
) << std::endl
;
3569 //Verify that data-path really exists
3571 if (::stat(dpath
.c_str(), &st
) == -1) {
3572 string err
= string("data-path: ") + dpath
;
3573 perror(err
.c_str());
3577 if (pgidstr
.length() && pgidstr
!= "meta" && !pgid
.parse(pgidstr
.c_str())) {
3578 cerr
<< "Invalid pgid '" << pgidstr
<< "' specified" << std::endl
;
3582 //Verify that the journal-path really exists
3583 if (type
== "filestore") {
3584 if (::stat(jpath
.c_str(), &st
) == -1) {
3585 string err
= string("journal-path: ") + jpath
;
3586 perror(err
.c_str());
3589 if (S_ISDIR(st
.st_mode
)) {
3590 cerr
<< "journal-path: " << jpath
<< ": "
3591 << cpp_strerror(EISDIR
) << std::endl
;
3596 std::unique_ptr
<ObjectStore
> fs
= ObjectStore::create(g_ceph_context
, type
, dpath
, jpath
, flags
);
3598 cerr
<< "Unable to create store of type " << type
<< std::endl
;
3602 if (op
== "fsck" || op
== "fsck-deep") {
3603 int r
= fs
->fsck(op
== "fsck-deep");
3605 cerr
<< "fsck failed: " << cpp_strerror(r
) << std::endl
;
3609 cerr
<< "fsck status: " << r
<< " remaining error(s) and warning(s)" << std::endl
;
3612 cout
<< "fsck success" << std::endl
;
3615 if (op
== "repair" || op
== "repair-deep") {
3616 int r
= fs
->repair(op
== "repair-deep");
3618 cerr
<< "repair failed: " << cpp_strerror(r
) << std::endl
;
3622 cerr
<< "repair status: " << r
<< " remaining error(s) and warning(s)" << std::endl
;
3625 cout
<< "repair success" << std::endl
;
3629 if (fsid
.length()) {
3631 bool r
= f
.parse(fsid
.c_str());
3633 cerr
<< "failed to parse uuid '" << fsid
<< "'" << std::endl
;
3640 cerr
<< "mkfs failed: " << cpp_strerror(r
) << std::endl
;
3648 snprintf(fn
, sizeof(fn
), "%s/type", target_data_path
.c_str());
3649 int fd
= ::open(fn
, O_RDONLY
);
3651 cerr
<< "Unable to open " << target_data_path
<< "/type" << std::endl
;
3657 target_type
= string(bl
.c_str(), bl
.length() - 1); // drop \n
3660 unique_ptr
<ObjectStore
> targetfs
= ObjectStore::create(
3661 g_ceph_context
, target_type
,
3662 target_data_path
, "", 0);
3664 cerr
<< "Unable to open store of type " << target_type
<< std::endl
;
3667 int r
= dup(dpath
, fs
.get(), target_data_path
, targetfs
.get());
3669 cerr
<< "dup failed: " << cpp_strerror(r
) << std::endl
;
3675 int ret
= fs
->mount();
3677 if (ret
== -EBUSY
) {
3678 cerr
<< "OSD has the store locked" << std::endl
;
3680 cerr
<< "Mount failed with '" << cpp_strerror(ret
) << "'" << std::endl
;
3687 FuseStore
fuse(fs
.get(), mountpoint
);
3688 cout
<< "mounting fuse at " << mountpoint
<< " ..." << std::endl
;
3689 int r
= fuse
.main();
3692 cerr
<< "failed to mount fuse: " << cpp_strerror(r
) << std::endl
;
3696 cerr
<< "fuse support not enabled" << std::endl
;
3702 vector
<coll_t
>::iterator it
;
3703 CompatSet supported
;
3705 #ifdef INTERNAL_TEST
3706 supported
= get_test_compat_set();
3708 supported
= OSD::get_osd_compat_set();
3712 auto ch
= fs
->open_collection(coll_t::meta());
3713 std::unique_ptr
<OSDSuperblock
> superblock
;
3714 if (!no_superblock
) {
3715 superblock
.reset(new OSDSuperblock
);
3716 bufferlist::const_iterator p
;
3717 ret
= fs
->read(ch
, OSD_SUPERBLOCK_GOBJECT
, 0, 0, bl
);
3719 cerr
<< "Failure to read OSD superblock: " << cpp_strerror(ret
) << std::endl
;
3724 decode(*superblock
, p
);
3727 cerr
<< "Cluster fsid=" << superblock
->cluster_fsid
<< std::endl
;
3731 cerr
<< "Supported features: " << supported
<< std::endl
;
3732 cerr
<< "On-disk features: " << superblock
->compat_features
<< std::endl
;
3734 if (supported
.compare(superblock
->compat_features
) == -1) {
3735 CompatSet unsupported
= supported
.unsupported(superblock
->compat_features
);
3736 cerr
<< "On-disk OSD incompatible features set "
3737 << unsupported
<< std::endl
;
3743 if (op
== "apply-layout-settings") {
3744 int target_level
= 0;
3745 // Single positional argument with apply-layout-settings
3746 // for target_level.
3747 if (vm
.count("object") && isdigit(object
[0])) {
3748 target_level
= atoi(object
.c_str());
3749 // This requires --arg1 to be specified since
3750 // this is the third positional argument and normally
3751 // used with object operations.
3752 } else if (vm
.count("arg1") && isdigit(arg1
[0])) {
3753 target_level
= atoi(arg1
.c_str());
3755 ceph_assert(superblock
!= nullptr);
3756 ret
= apply_layout_settings(fs
.get(), *superblock
, pool
, pgid
, dry_run
, target_level
);
3760 if (op
!= "list" && vm
.count("object")) {
3761 // Special case: Create pgmeta_oid if empty string specified
3762 // This can't conflict with any actual object names.
3764 ghobj
= pgid
.make_pgmeta_oid();
3766 json_spirit::Value v
;
3768 if (!json_spirit::read(object
, v
) ||
3769 (v
.type() != json_spirit::array_type
&& v
.type() != json_spirit::obj_type
)) {
3770 // Special: Need head/snapdir so set even if user didn't specify
3771 if (vm
.count("objcmd") && (objcmd
== "remove-clone-metadata"))
3773 lookup_ghobject
lookup(object
, nspace
, head
);
3774 if (pgidstr
== "meta")
3775 ret
= action_on_all_objects_in_exact_pg(fs
.get(), coll_t::meta(), lookup
, debug
);
3776 else if (pgidstr
.length())
3777 ret
= action_on_all_objects_in_exact_pg(fs
.get(), coll_t(pgid
), lookup
, debug
);
3779 ret
= action_on_all_objects(fs
.get(), lookup
, debug
);
3781 throw std::runtime_error("Internal error");
3783 if (lookup
.size() != 1) {
3785 if (lookup
.size() == 0)
3786 ss
<< "No object id '" << object
<< "' found or invalid JSON specified";
3788 ss
<< "Found " << lookup
.size() << " objects with id '" << object
3789 << "', please use a JSON spec from --op list instead";
3790 throw std::runtime_error(ss
.str());
3792 pair
<coll_t
, ghobject_t
> found
= lookup
.pop();
3793 pgidstr
= found
.first
.to_str();
3794 pgid
.parse(pgidstr
.c_str());
3795 ghobj
= found
.second
;
3799 if (pgidstr
.length() == 0 && v
.type() != json_spirit::array_type
) {
3800 ss
<< "Without --pgid the object '" << object
3801 << "' must be a JSON array";
3802 throw std::runtime_error(ss
.str());
3804 if (v
.type() == json_spirit::array_type
) {
3805 json_spirit::Array array
= v
.get_array();
3806 if (array
.size() != 2) {
3807 ss
<< "Object '" << object
3808 << "' must be a JSON array with 2 elements";
3809 throw std::runtime_error(ss
.str());
3811 vector
<json_spirit::Value
>::iterator i
= array
.begin();
3812 ceph_assert(i
!= array
.end());
3813 if (i
->type() != json_spirit::str_type
) {
3814 ss
<< "Object '" << object
3815 << "' must be a JSON array with the first element a string";
3816 throw std::runtime_error(ss
.str());
3818 string object_pgidstr
= i
->get_str();
3819 if (object_pgidstr
!= "meta") {
3821 object_pgid
.parse(object_pgidstr
.c_str());
3822 if (pgidstr
.length() > 0) {
3823 if (object_pgid
!= pgid
) {
3824 ss
<< "object '" << object
3825 << "' has a pgid different from the --pgid="
3826 << pgidstr
<< " option";
3827 throw std::runtime_error(ss
.str());
3830 pgidstr
= object_pgidstr
;
3834 pgidstr
= object_pgidstr
;
3841 } catch (std::runtime_error
& e
) {
3842 ss
<< "Decode object JSON error: " << e
.what();
3843 throw std::runtime_error(ss
.str());
3845 if (pgidstr
!= "meta" && (uint64_t)pgid
.pgid
.m_pool
!= (uint64_t)ghobj
.hobj
.pool
) {
3846 cerr
<< "Object pool and pgid pool don't match" << std::endl
;
3850 if (pgidstr
!= "meta") {
3851 auto ch
= fs
->open_collection(coll_t(pgid
));
3852 if (!ghobj
.match(fs
->collection_bits(ch
), pgid
.ps())) {
3854 ss
<< "object " << ghobj
<< " not contained by pg " << pgid
;
3855 throw std::runtime_error(ss
.str());
3859 } catch (std::runtime_error
& e
) {
3860 cerr
<< e
.what() << std::endl
;
3867 // The ops which require --pgid option are checked here and
3868 // mentioned in the usage for --pgid.
3869 if ((op
== "info" || op
== "log" || op
== "remove" || op
== "export"
3870 || op
== "export-remove" || op
== "mark-complete"
3871 || op
== "reset-last-complete"
3872 || op
== "trim-pg-log"
3873 || op
== "trim-pg-log-dups") &&
3874 pgidstr
.length() == 0) {
3875 cerr
<< "Must provide pgid" << std::endl
;
3881 if (op
== "import") {
3882 ceph_assert(superblock
!= nullptr);
3884 ret
= tool
.do_import(fs
.get(), *superblock
, force
, pgidstr
);
3886 catch (const buffer::error
&e
) {
3887 cerr
<< "do_import threw exception error " << e
.what() << std::endl
;
3890 if (ret
== -EFAULT
) {
3891 cerr
<< "Corrupt input for import" << std::endl
;
3894 cout
<< "Import successful" << std::endl
;
3896 } else if (op
== "dump-journal-mount") {
3897 // Undocumented feature to dump journal with mounted fs
3898 // This doesn't support the format option, but it uses the
3899 // ObjectStore::dump_journal() and mounts to get replay to run.
3900 ret
= fs
->dump_journal(cout
);
3902 if (ret
== -EOPNOTSUPP
) {
3903 cerr
<< "Object store type \"" << type
<< "\" doesn't support journal dump" << std::endl
;
3905 cerr
<< "Journal dump failed with error " << cpp_strerror(ret
) << std::endl
;
3909 } else if (op
== "get-osdmap") {
3913 ceph_assert(superblock
!= nullptr);
3914 epoch
= superblock
->current_epoch
;
3916 ret
= get_osdmap(fs
.get(), epoch
, osdmap
, bl
);
3918 cerr
<< "Failed to get osdmap#" << epoch
<< ": "
3919 << cpp_strerror(ret
) << std::endl
;
3922 ret
= bl
.write_fd(file_fd
);
3924 cerr
<< "Failed to write to " << file
<< ": " << cpp_strerror(ret
) << std::endl
;
3926 cout
<< "osdmap#" << epoch
<< " exported." << std::endl
;
3929 } else if (op
== "set-osdmap") {
3931 ret
= get_fd_data(file_fd
, bl
);
3933 cerr
<< "Failed to read osdmap " << cpp_strerror(ret
) << std::endl
;
3935 ret
= set_osdmap(fs
.get(), epoch
, bl
, force
);
3938 } else if (op
== "get-inc-osdmap") {
3941 ceph_assert(superblock
!= nullptr);
3942 epoch
= superblock
->current_epoch
;
3944 ret
= get_inc_osdmap(fs
.get(), epoch
, bl
);
3946 cerr
<< "Failed to get incremental osdmap# " << epoch
<< ": "
3947 << cpp_strerror(ret
) << std::endl
;
3950 ret
= bl
.write_fd(file_fd
);
3952 cerr
<< "Failed to write to " << file
<< ": " << cpp_strerror(ret
) << std::endl
;
3954 cout
<< "inc-osdmap#" << epoch
<< " exported." << std::endl
;
3957 } else if (op
== "set-inc-osdmap") {
3959 ret
= get_fd_data(file_fd
, bl
);
3961 cerr
<< "Failed to read incremental osdmap " << cpp_strerror(ret
) << std::endl
;
3964 ret
= set_inc_osdmap(fs
.get(), epoch
, bl
, force
);
3967 } else if (op
== "update-mon-db") {
3968 if (!vm
.count("mon-store-path")) {
3969 cerr
<< "Please specify the path to monitor db to update" << std::endl
;
3972 ceph_assert(superblock
!= nullptr);
3973 ret
= update_mon_db(*fs
, *superblock
, dpath
+ "/keyring", mon_store_path
);
3978 if (op
== "remove") {
3979 if (!force
&& !dry_run
) {
3980 cerr
<< "Please use export-remove or you must use --force option" << std::endl
;
3984 ret
= initiate_new_remove_pg(fs
.get(), pgid
);
3986 cerr
<< "PG '" << pgid
<< "' not found" << std::endl
;
3989 cout
<< "Remove successful" << std::endl
;
3993 if (op
== "fix-lost") {
3994 boost::scoped_ptr
<action_on_object_t
> action
;
3995 action
.reset(new do_fix_lost());
3996 if (pgidstr
.length())
3997 ret
= action_on_all_objects_in_exact_pg(fs
.get(), coll_t(pgid
), *action
, debug
);
3999 ret
= action_on_all_objects(fs
.get(), *action
, debug
);
4004 ret
= do_list(fs
.get(), pgidstr
, object
, nspace
, formatter
, debug
,
4005 human_readable
, head
);
4007 cerr
<< "do_list failed: " << cpp_strerror(ret
) << std::endl
;
4011 if (op
== "list-slow-omap") {
4012 ret
= do_list_slow(fs
.get(), pgidstr
, object
, slow_threshold
, formatter
, debug
,
4015 cerr
<< "do_list failed: " << cpp_strerror(ret
) << std::endl
;
4020 if (op
== "dump-super") {
4021 ceph_assert(superblock
!= nullptr);
4022 formatter
->open_object_section("superblock");
4023 superblock
->dump(formatter
);
4024 formatter
->close_section();
4025 formatter
->flush(cout
);
4030 if (op
== "statfs") {
4031 store_statfs_t statsbuf
;
4032 ret
= fs
->statfs(&statsbuf
);
4034 cerr
<< "error from statfs: " << cpp_strerror(ret
) << std::endl
;
4037 formatter
->open_object_section("statfs");
4038 statsbuf
.dump(formatter
);
4039 formatter
->close_section();
4040 formatter
->flush(cout
);
4045 if (op
== "meta-list") {
4046 ret
= do_meta(fs
.get(), object
, formatter
, debug
, human_readable
);
4048 cerr
<< "do_meta failed: " << cpp_strerror(ret
) << std::endl
;
4053 ret
= fs
->list_collections(ls
);
4055 cerr
<< "failed to list pgs: " << cpp_strerror(ret
) << std::endl
;
4059 if (debug
&& op
== "list-pgs")
4060 cout
<< "Performing list-pgs operation" << std::endl
;
4063 for (it
= ls
.begin(); it
!= ls
.end(); ++it
) {
4066 if (pgidstr
== "meta") {
4067 if (it
->to_str() == "meta")
4073 if (!it
->is_pg(&tmppgid
)) {
4077 if (it
->is_temp(&tmppgid
)) {
4081 if (op
!= "list-pgs" && tmppgid
!= pgid
) {
4085 if (op
!= "list-pgs") {
4090 cout
<< tmppgid
<< std::endl
;
4093 if (op
== "list-pgs") {
4098 // If not an object command nor any of the ops handled below, then output this usage
4099 // before complaining about a bad pgid
4100 if (!vm
.count("objcmd") && op
!= "export" && op
!= "export-remove" && op
!= "info" && op
!= "log" && op
!= "mark-complete" && op
!= "trim-pg-log" && op
!= "trim-pg-log-dups") {
4101 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, "
4102 "get-osdmap, set-osdmap, get-inc-osdmap, set-inc-osdmap, mark-complete, reset-last-complete, dump-export, trim-pg-log, trim-pg-log-dups statfs)"
4109 // The following code for export, info, log require omap or !skip-mount-omap
4110 if (it
!= ls
.end()) {
4114 if (vm
.count("objcmd")) {
4116 if (objcmd
== "remove" || objcmd
== "removeall") {
4117 bool all
= (objcmd
== "removeall");
4118 enum rmtype type
= BOTH
;
4119 if (rmtypestr
== "nosnapmap")
4121 else if (rmtypestr
== "snapmap")
4123 ret
= do_remove_object(fs
.get(), coll
, ghobj
, all
, force
, type
);
4125 } else if (objcmd
== "list-attrs") {
4126 ret
= do_list_attrs(fs
.get(), coll
, ghobj
);
4128 } else if (objcmd
== "list-omap") {
4129 ret
= do_list_omap(fs
.get(), coll
, ghobj
);
4131 } else if (objcmd
== "get-bytes" || objcmd
== "set-bytes") {
4132 if (objcmd
== "get-bytes") {
4134 if (vm
.count("arg1") == 0 || arg1
== "-") {
4137 fd
= open(arg1
.c_str(), O_WRONLY
|O_TRUNC
|O_CREAT
|O_EXCL
|O_LARGEFILE
, 0666);
4139 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4144 ret
= do_get_bytes(fs
.get(), coll
, ghobj
, fd
);
4145 if (fd
!= STDOUT_FILENO
)
4149 if (vm
.count("arg1") == 0 || arg1
== "-") {
4150 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4151 if (isatty(STDIN_FILENO
)) {
4152 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4158 fd
= open(arg1
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4160 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4165 ret
= do_set_bytes(fs
.get(), coll
, ghobj
, fd
);
4166 if (fd
!= STDIN_FILENO
)
4170 } else if (objcmd
== "get-attr") {
4171 if (vm
.count("arg1") == 0) {
4176 ret
= do_get_attr(fs
.get(), coll
, ghobj
, arg1
);
4178 } else if (objcmd
== "set-attr") {
4179 if (vm
.count("arg1") == 0) {
4185 if (vm
.count("arg2") == 0 || arg2
== "-") {
4186 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4187 if (isatty(STDIN_FILENO
)) {
4188 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4194 fd
= open(arg2
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4196 cerr
<< "open " << arg2
<< " " << cpp_strerror(errno
) << std::endl
;
4201 ret
= do_set_attr(fs
.get(), coll
, ghobj
, arg1
, fd
);
4202 if (fd
!= STDIN_FILENO
)
4205 } else if (objcmd
== "rm-attr") {
4206 if (vm
.count("arg1") == 0) {
4211 ret
= do_rm_attr(fs
.get(), coll
, ghobj
, arg1
);
4213 } else if (objcmd
== "get-omap") {
4214 if (vm
.count("arg1") == 0) {
4219 ret
= do_get_omap(fs
.get(), coll
, ghobj
, arg1
);
4221 } else if (objcmd
== "set-omap") {
4222 if (vm
.count("arg1") == 0) {
4228 if (vm
.count("arg2") == 0 || arg2
== "-") {
4229 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4230 if (isatty(STDIN_FILENO
)) {
4231 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4237 fd
= open(arg2
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4239 cerr
<< "open " << arg2
<< " " << cpp_strerror(errno
) << std::endl
;
4244 ret
= do_set_omap(fs
.get(), coll
, ghobj
, arg1
, fd
);
4245 if (fd
!= STDIN_FILENO
)
4248 } else if (objcmd
== "rm-omap") {
4249 if (vm
.count("arg1") == 0) {
4254 ret
= do_rm_omap(fs
.get(), coll
, ghobj
, arg1
);
4256 } else if (objcmd
== "get-omaphdr") {
4257 if (vm
.count("arg1")) {
4262 ret
= do_get_omaphdr(fs
.get(), coll
, ghobj
);
4264 } else if (objcmd
== "set-omaphdr") {
4266 if (vm
.count("arg2")) {
4272 if (vm
.count("arg1") == 0 || arg1
== "-") {
4273 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4274 if (isatty(STDIN_FILENO
)) {
4275 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4281 fd
= open(arg1
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4283 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4288 ret
= do_set_omaphdr(fs
.get(), coll
, ghobj
, fd
);
4289 if (fd
!= STDIN_FILENO
)
4292 } else if (objcmd
== "dump") {
4293 // There should not be any other arguments
4294 if (vm
.count("arg1") || vm
.count("arg2")) {
4299 ret
= print_obj_info(fs
.get(), coll
, ghobj
, formatter
);
4301 } else if (objcmd
== "corrupt-info") { // Undocumented testing feature
4302 // There should not be any other arguments
4303 if (vm
.count("arg1") || vm
.count("arg2")) {
4308 ret
= corrupt_info(fs
.get(), coll
, ghobj
, formatter
);
4310 } else if (objcmd
== "set-size" || objcmd
== "corrupt-size") {
4311 // Undocumented testing feature
4312 bool corrupt
= (objcmd
== "corrupt-size");
4314 if (vm
.count("arg1") == 0 || vm
.count("arg2")) {
4319 if (arg1
.length() == 0 || !isdigit(arg1
.c_str()[0])) {
4320 cerr
<< "Invalid size '" << arg1
<< "' specified" << std::endl
;
4324 uint64_t size
= atoll(arg1
.c_str());
4325 ret
= set_size(fs
.get(), coll
, ghobj
, size
, formatter
, corrupt
);
4327 } else if (objcmd
== "clear-data-digest") {
4328 ret
= clear_data_digest(fs
.get(), coll
, ghobj
);
4330 } else if (objcmd
== "clear-snapset") {
4331 // UNDOCUMENTED: For testing zap SnapSet
4332 // IGNORE extra args since not in usage anyway
4333 if (!ghobj
.hobj
.has_snapset()) {
4334 cerr
<< "'" << objcmd
<< "' requires a head or snapdir object" << std::endl
;
4338 ret
= clear_snapset(fs
.get(), coll
, ghobj
, arg1
);
4340 } else if (objcmd
== "remove-clone-metadata") {
4342 if (vm
.count("arg1") == 0 || vm
.count("arg2")) {
4347 if (!ghobj
.hobj
.has_snapset()) {
4348 cerr
<< "'" << objcmd
<< "' requires a head or snapdir object" << std::endl
;
4352 if (arg1
.length() == 0 || !isdigit(arg1
.c_str()[0])) {
4353 cerr
<< "Invalid cloneid '" << arg1
<< "' specified" << std::endl
;
4357 snapid_t cloneid
= atoi(arg1
.c_str());
4358 ret
= remove_clone(fs
.get(), coll
, ghobj
, cloneid
, force
);
4361 cerr
<< "Unknown object command '" << objcmd
<< "'" << std::endl
;
4368 ret
= PG::peek_map_epoch(fs
.get(), pgid
, &map_epoch
);
4370 cerr
<< "peek_map_epoch reports error" << std::endl
;
4372 cerr
<< "map_epoch " << map_epoch
<< std::endl
;
4374 pg_info_t
info(pgid
);
4375 PastIntervals past_intervals
;
4377 ret
= PG::read_info(fs
.get(), pgid
, coll
, info
, past_intervals
, struct_ver
);
4379 cerr
<< "read_info error " << cpp_strerror(ret
) << std::endl
;
4382 if (struct_ver
< PG::get_compat_struct_v()) {
4383 cerr
<< "PG is too old to upgrade, use older Ceph version" << std::endl
;
4388 cerr
<< "struct_v " << (int)struct_ver
<< std::endl
;
4390 if (op
== "export" || op
== "export-remove") {
4391 ceph_assert(superblock
!= nullptr);
4392 ret
= tool
.do_export(fs
.get(), coll
, pgid
, info
, map_epoch
, struct_ver
, *superblock
, past_intervals
);
4394 cerr
<< "Export successful" << std::endl
;
4395 if (op
== "export-remove") {
4396 ret
= initiate_new_remove_pg(fs
.get(), pgid
);
4397 // Export succeeded, so pgid is there
4398 ceph_assert(ret
== 0);
4399 cerr
<< "Remove successful" << std::endl
;
4402 } else if (op
== "info") {
4403 formatter
->open_object_section("info");
4404 info
.dump(formatter
);
4405 formatter
->close_section();
4406 formatter
->flush(cout
);
4408 } else if (op
== "log") {
4409 PGLog::IndexedLog log
;
4410 pg_missing_t missing
;
4411 ret
= get_log(fs
.get(), struct_ver
, pgid
, info
, log
, missing
);
4415 dump_log(formatter
, cout
, log
, missing
);
4416 } else if (op
== "mark-complete") {
4417 ObjectStore::Transaction tran
;
4418 ObjectStore::Transaction
*t
= &tran
;
4420 if (struct_ver
< PG::get_compat_struct_v()) {
4421 cerr
<< "Can't mark-complete, version mismatch " << (int)struct_ver
4422 << " (pg) < compat " << (int)PG::get_compat_struct_v() << " (tool)"
4428 cout
<< "Marking complete " << std::endl
;
4430 ceph_assert(superblock
!= nullptr);
4431 info
.last_update
= eversion_t(superblock
->current_epoch
, info
.last_update
.version
+ 1);
4432 info
.last_backfill
= hobject_t::get_max();
4433 info
.last_epoch_started
= superblock
->current_epoch
;
4434 info
.history
.last_epoch_started
= superblock
->current_epoch
;
4435 info
.history
.last_epoch_clean
= superblock
->current_epoch
;
4436 past_intervals
.clear();
4439 ret
= write_info(*t
, map_epoch
, info
, past_intervals
);
4442 auto ch
= fs
->open_collection(coll_t(pgid
));
4443 fs
->queue_transaction(ch
, std::move(*t
));
4445 cout
<< "Marking complete succeeded" << std::endl
;
4446 } else if (op
== "trim-pg-log") {
4447 ret
= do_trim_pg_log(fs
.get(), coll
, info
, pgid
,
4448 map_epoch
, past_intervals
);
4450 cerr
<< "Error trimming pg log: " << cpp_strerror(ret
) << std::endl
;
4453 cout
<< "Finished trimming pg log" << std::endl
;
4455 } else if (op
== "trim-pg-log-dups") {
4456 ret
= do_trim_pg_log_dups(fs
.get(), coll
, info
, pgid
,
4457 map_epoch
, past_intervals
);
4459 cerr
<< "Error trimming pg log dups: " << cpp_strerror(ret
) << std::endl
;
4462 cout
<< "Finished trimming pg log dups" << std::endl
;
4464 } else if (op
== "reset-last-complete") {
4466 std::cerr
<< "WARNING: reset-last-complete is extremely dangerous and almost "
4467 << "certain to lead to permanent data loss unless you know exactly "
4468 << "what you are doing. Pass --force to proceed anyway."
4473 ObjectStore::Transaction tran
;
4474 ObjectStore::Transaction
*t
= &tran
;
4476 if (struct_ver
< PG::get_compat_struct_v()) {
4477 cerr
<< "Can't reset-last-complete, version mismatch " << (int)struct_ver
4478 << " (pg) < compat " << (int)PG::get_compat_struct_v() << " (tool)"
4484 cout
<< "Reseting last_complete " << std::endl
;
4486 info
.last_complete
= info
.last_update
;
4489 ret
= write_info(*t
, map_epoch
, info
, past_intervals
);
4492 fs
->queue_transaction(ch
, std::move(*t
));
4494 cout
<< "Reseting last_complete succeeded" << std::endl
;
4497 ceph_assert(!"Should have already checked for valid --op");
4500 cerr
<< "PG '" << pgid
<< "' not found" << std::endl
;
4507 Formatter
* f
= Formatter::create("json-pretty", "json-pretty", "json-pretty");
4508 cct
->get_perfcounters_collection()->dump_formatted(f
, false);
4509 ostr
<< "ceph-objectstore-tool ";
4512 cout
<< ostr
.str() << std::endl
;
4515 int r
= fs
->umount();
4517 cerr
<< "umount failed: " << cpp_strerror(r
) << std::endl
;
4518 // If no previous error, then use umount() error
4524 // Export output can go to stdout, so put this message on stderr
4526 cerr
<< "dry-run: Nothing changed" << std::endl
;
4528 cout
<< "dry-run: Nothing changed" << std::endl
;