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 const int OMAP_BATCH_SIZE
= 25;
740 void get_omap_batch(ObjectMap::ObjectMapIterator
&iter
, map
<string
, bufferlist
> &oset
)
743 for (int count
= OMAP_BATCH_SIZE
; count
&& iter
->valid(); --count
, iter
->next()) {
744 oset
.insert(pair
<string
, bufferlist
>(iter
->key(), iter
->value()));
748 int ObjectStoreTool::export_file(ObjectStore
*store
, coll_t cid
, ghobject_t
&obj
)
754 auto ch
= store
->open_collection(cid
);
755 int ret
= store
->stat(ch
, obj
, &st
);
759 cerr
<< "Read " << obj
<< std::endl
;
763 cerr
<< "size=" << total
<< std::endl
;
765 object_begin
objb(obj
);
770 ret
= store
->getattr(ch
, obj
, OI_ATTR
, bp
);
772 cerr
<< "getattr failure object_info " << ret
<< std::endl
;
778 cerr
<< "object_info: " << objb
.oi
<< std::endl
;
781 // NOTE: we include whiteouts, lost, etc.
783 ret
= write_section(TYPE_OBJECT_BEGIN
, objb
, file_fd
);
788 bufferlist rawdatabl
;
791 mysize_t len
= max_read
;
795 ret
= store
->read(ch
, obj
, offset
, len
, rawdatabl
);
801 data_section
dblock(offset
, len
, rawdatabl
);
803 cerr
<< "data section offset=" << offset
<< " len=" << len
<< std::endl
;
808 ret
= write_section(TYPE_DATA
, dblock
, file_fd
);
812 //Handle attrs for this object
813 map
<string
,bufferptr
,less
<>> aset
;
814 ret
= store
->getattrs(ch
, obj
, aset
);
816 attr_section
as(aset
);
817 ret
= write_section(TYPE_ATTRS
, as
, file_fd
);
822 cerr
<< "attrs size " << aset
.size() << std::endl
;
825 //Handle omap information
827 ret
= store
->omap_get_header(ch
, obj
, &hdrbuf
, true);
829 cerr
<< "omap_get_header: " << cpp_strerror(ret
) << std::endl
;
833 omap_hdr_section
ohs(hdrbuf
);
834 ret
= write_section(TYPE_OMAP_HDR
, ohs
, file_fd
);
838 ObjectMap::ObjectMapIterator iter
= store
->get_omap_iterator(ch
, obj
);
841 cerr
<< "omap_get_iterator: " << cpp_strerror(ret
) << std::endl
;
844 iter
->seek_to_first();
846 map
<string
, bufferlist
> out
;
847 while(iter
->valid()) {
848 get_omap_batch(iter
, out
);
850 if (out
.empty()) break;
852 mapcount
+= out
.size();
853 omap_section
oms(out
);
854 ret
= write_section(TYPE_OMAP
, oms
, file_fd
);
859 cerr
<< "omap map size " << mapcount
<< std::endl
;
861 ret
= write_simple(TYPE_OBJECT_END
, file_fd
);
868 int ObjectStoreTool::export_files(ObjectStore
*store
, coll_t coll
)
871 auto ch
= store
->open_collection(coll
);
872 while (!next
.is_max()) {
873 vector
<ghobject_t
> objects
;
874 int r
= store
->collection_list(ch
, next
, ghobject_t::get_max(), 300,
878 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
881 ceph_assert(!i
->hobj
.is_meta());
882 if (i
->is_pgmeta() || i
->hobj
.is_temp() || !i
->is_no_gen()) {
885 r
= export_file(store
, coll
, *i
);
893 int set_inc_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
, bool force
) {
894 OSDMap::Incremental inc
;
895 auto it
= bl
.cbegin();
899 } else if (e
!= inc
.epoch
) {
900 cerr
<< "incremental.epoch mismatch: "
901 << inc
.epoch
<< " != " << e
<< std::endl
;
903 cerr
<< "But will continue anyway." << std::endl
;
908 auto ch
= store
->open_collection(coll_t::meta());
909 const ghobject_t inc_oid
= OSD::get_inc_osdmap_pobject_name(e
);
910 if (!store
->exists(ch
, inc_oid
)) {
911 cerr
<< "inc-osdmap (" << inc_oid
<< ") does not exist." << std::endl
;
915 cout
<< "Creating a new epoch." << std::endl
;
919 ObjectStore::Transaction t
;
920 t
.write(coll_t::meta(), inc_oid
, 0, bl
.length(), bl
);
921 t
.truncate(coll_t::meta(), inc_oid
, bl
.length());
922 store
->queue_transaction(ch
, std::move(t
));
926 int get_inc_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
)
928 auto ch
= store
->open_collection(coll_t::meta());
930 OSD::get_inc_osdmap_pobject_name(e
),
937 int set_osdmap(ObjectStore
*store
, epoch_t e
, bufferlist
& bl
, bool force
) {
941 e
= osdmap
.get_epoch();
942 } else if (e
!= osdmap
.get_epoch()) {
943 cerr
<< "osdmap.epoch mismatch: "
944 << e
<< " != " << osdmap
.get_epoch() << std::endl
;
946 cerr
<< "But will continue anyway." << std::endl
;
951 auto ch
= store
->open_collection(coll_t::meta());
952 const ghobject_t full_oid
= OSD::get_osdmap_pobject_name(e
);
953 if (!store
->exists(ch
, full_oid
)) {
954 cerr
<< "osdmap (" << full_oid
<< ") does not exist." << std::endl
;
958 cout
<< "Creating a new epoch." << std::endl
;
962 ObjectStore::Transaction t
;
963 t
.write(coll_t::meta(), full_oid
, 0, bl
.length(), bl
);
964 t
.truncate(coll_t::meta(), full_oid
, bl
.length());
965 store
->queue_transaction(ch
, std::move(t
));
969 int get_osdmap(ObjectStore
*store
, epoch_t e
, OSDMap
&osdmap
, bufferlist
& bl
)
971 ObjectStore::CollectionHandle ch
= store
->open_collection(coll_t::meta());
972 bool found
= store
->read(
973 ch
, OSD::get_osdmap_pobject_name(e
), 0, 0, bl
) >= 0;
975 cerr
<< "Can't find OSDMap for pg epoch " << e
<< std::endl
;
980 cerr
<< osdmap
<< std::endl
;
984 int get_pg_num_history(ObjectStore
*store
, pool_pg_num_history_t
*h
)
986 ObjectStore::CollectionHandle ch
= store
->open_collection(coll_t::meta());
988 auto pghist
= OSD::make_pg_num_history_oid();
989 int r
= store
->read(ch
, pghist
, 0, 0, bl
, 0);
990 if (r
>= 0 && bl
.length() > 0) {
991 auto p
= bl
.cbegin();
994 cout
<< __func__
<< " pg_num_history " << *h
<< std::endl
;
998 int add_osdmap(ObjectStore
*store
, metadata_section
&ms
)
1000 return get_osdmap(store
, ms
.map_epoch
, ms
.osdmap
, ms
.osdmap_bl
);
1003 int ObjectStoreTool::do_export(ObjectStore
*fs
, coll_t coll
, spg_t pgid
,
1004 pg_info_t
&info
, epoch_t map_epoch
, __u8 struct_ver
,
1005 const OSDSuperblock
& superblock
,
1006 PastIntervals
&past_intervals
)
1008 PGLog::IndexedLog log
;
1009 pg_missing_t missing
;
1011 cerr
<< "Exporting " << pgid
<< " info " << info
<< std::endl
;
1013 int ret
= get_log(fs
, struct_ver
, pgid
, info
, log
, missing
);
1018 Formatter
*formatter
= Formatter::create("json-pretty");
1019 ceph_assert(formatter
);
1020 dump_log(formatter
, cerr
, log
, missing
);
1025 pg_begin
pgb(pgid
, superblock
);
1026 // Special case: If replicated pg don't require the importing OSD to have shard feature
1027 if (pgid
.is_no_shard()) {
1028 pgb
.superblock
.compat_features
.incompat
.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS
);
1030 ret
= write_section(TYPE_PG_BEGIN
, pgb
, file_fd
);
1034 // The metadata_section is now before files, so import can detect
1035 // errors and abort without wasting time.
1036 metadata_section
ms(
1043 ret
= add_osdmap(fs
, ms
);
1046 ret
= write_section(TYPE_PG_METADATA
, ms
, file_fd
);
1050 ret
= export_files(fs
, coll
);
1052 cerr
<< "export_files error " << ret
<< std::endl
;
1056 ret
= write_simple(TYPE_PG_END
, file_fd
);
1063 int dump_data(Formatter
*formatter
, bufferlist
&bl
)
1065 auto ebliter
= bl
.cbegin();
1069 formatter
->open_object_section("data_block");
1070 formatter
->dump_unsigned("offset", ds
.offset
);
1071 formatter
->dump_unsigned("len", ds
.len
);
1072 // XXX: Add option to dump data like od -cx ?
1073 formatter
->close_section();
1074 formatter
->flush(cout
);
1078 int get_data(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1079 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1081 auto ebliter
= bl
.cbegin();
1086 cerr
<< "\tdata: offset " << ds
.offset
<< " len " << ds
.len
<< std::endl
;
1087 t
->write(coll
, hoid
, ds
.offset
, ds
.len
, ds
.databl
);
1092 Formatter
*formatter
, ghobject_t hoid
,
1095 auto ebliter
= bl
.cbegin();
1099 // This could have been handled in the caller if we didn't need to
1100 // support exports that didn't include object_info_t in object_begin.
1101 if (hoid
.generation
== ghobject_t::NO_GEN
&&
1102 hoid
.hobj
.is_head()) {
1103 map
<string
,bufferlist
>::iterator mi
= as
.data
.find(SS_ATTR
);
1104 if (mi
!= as
.data
.end()) {
1106 auto p
= mi
->second
.cbegin();
1108 formatter
->open_object_section("snapset");
1109 snapset
.dump(formatter
);
1110 formatter
->close_section();
1112 formatter
->open_object_section("snapset");
1113 formatter
->dump_string("error", "missing SS_ATTR");
1114 formatter
->close_section();
1118 formatter
->open_object_section("attrs");
1119 formatter
->open_array_section("user");
1120 for (auto kv
: as
.data
) {
1121 // Skip system attributes
1122 if (('_' != kv
.first
.at(0)) || kv
.first
.size() == 1)
1124 formatter
->open_object_section("user_attr");
1125 formatter
->dump_string("name", kv
.first
.substr(1));
1127 formatter
->dump_string("value", cleanbin(kv
.second
, b64
));
1128 formatter
->dump_bool("Base64", b64
);
1129 formatter
->close_section();
1131 formatter
->close_section();
1132 formatter
->open_array_section("system");
1133 for (auto kv
: as
.data
) {
1134 // Skip user attributes
1135 if (('_' == kv
.first
.at(0)) && kv
.first
.size() != 1)
1137 formatter
->open_object_section("sys_attr");
1138 formatter
->dump_string("name", kv
.first
);
1139 formatter
->close_section();
1141 formatter
->close_section();
1142 formatter
->close_section();
1143 formatter
->flush(cout
);
1149 ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1150 ObjectStore::Transaction
*t
, bufferlist
&bl
,
1151 OSDriver
&driver
, SnapMapper
&snap_mapper
)
1153 auto ebliter
= bl
.cbegin();
1157 auto ch
= store
->open_collection(coll
);
1159 cerr
<< "\tattrs: len " << as
.data
.size() << std::endl
;
1160 t
->setattrs(coll
, hoid
, as
.data
);
1162 // This could have been handled in the caller if we didn't need to
1163 // support exports that didn't include object_info_t in object_begin.
1164 if (hoid
.generation
== ghobject_t::NO_GEN
&&
1165 hoid
.hobj
.is_head()) {
1166 map
<string
,bufferlist
>::iterator mi
= as
.data
.find(SS_ATTR
);
1167 if (mi
!= as
.data
.end()) {
1169 auto p
= mi
->second
.cbegin();
1171 cout
<< "snapset " << snapset
<< std::endl
;
1172 for (auto& p
: snapset
.clone_snaps
) {
1173 ghobject_t clone
= hoid
;
1174 clone
.hobj
.snap
= p
.first
;
1175 set
<snapid_t
> snaps(p
.second
.begin(), p
.second
.end());
1176 if (!store
->exists(ch
, clone
)) {
1177 // no clone, skip. this is probably a cache pool. this works
1178 // because we use a separate transaction per object and clones
1179 // come before head in the archive.
1181 cerr
<< "\tskipping missing " << clone
<< " (snaps "
1182 << snaps
<< ")" << std::endl
;
1186 cerr
<< "\tsetting " << clone
.hobj
<< " snaps " << snaps
1188 OSDriver::OSTransaction
_t(driver
.get_transaction(t
));
1189 ceph_assert(!snaps
.empty());
1190 snap_mapper
.add_oid(clone
.hobj
, snaps
, &_t
);
1193 cerr
<< "missing SS_ATTR on " << hoid
<< std::endl
;
1199 int dump_omap_hdr(Formatter
*formatter
, bufferlist
&bl
)
1201 auto ebliter
= bl
.cbegin();
1202 omap_hdr_section oh
;
1205 formatter
->open_object_section("omap_header");
1206 formatter
->dump_string("value", string(oh
.hdr
.c_str(), oh
.hdr
.length()));
1207 formatter
->close_section();
1208 formatter
->flush(cout
);
1212 int get_omap_hdr(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1213 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1215 auto ebliter
= bl
.cbegin();
1216 omap_hdr_section oh
;
1220 cerr
<< "\tomap header: " << string(oh
.hdr
.c_str(), oh
.hdr
.length())
1222 t
->omap_setheader(coll
, hoid
, oh
.hdr
);
1226 int dump_omap(Formatter
*formatter
, bufferlist
&bl
)
1228 auto ebliter
= bl
.cbegin();
1232 formatter
->open_object_section("omaps");
1233 formatter
->dump_unsigned("count", os
.omap
.size());
1234 formatter
->open_array_section("data");
1235 for (auto o
: os
.omap
) {
1236 formatter
->open_object_section("omap");
1237 formatter
->dump_string("name", o
.first
);
1239 formatter
->dump_string("value", cleanbin(o
.second
, b64
));
1240 formatter
->dump_bool("Base64", b64
);
1241 formatter
->close_section();
1243 formatter
->close_section();
1244 formatter
->close_section();
1245 formatter
->flush(cout
);
1249 int get_omap(ObjectStore
*store
, coll_t coll
, ghobject_t hoid
,
1250 ObjectStore::Transaction
*t
, bufferlist
&bl
)
1252 auto ebliter
= bl
.cbegin();
1257 cerr
<< "\tomap: size " << os
.omap
.size() << std::endl
;
1258 t
->omap_setkeys(coll
, hoid
, os
.omap
);
1262 int ObjectStoreTool::dump_object(Formatter
*formatter
,
1265 auto ebliter
= bl
.cbegin();
1269 if (ob
.hoid
.hobj
.is_temp()) {
1270 cerr
<< "ERROR: Export contains temporary object '" << ob
.hoid
<< "'" << std::endl
;
1274 formatter
->open_object_section("object");
1275 formatter
->open_object_section("oid");
1276 ob
.hoid
.dump(formatter
);
1277 formatter
->close_section();
1278 formatter
->open_object_section("object_info");
1279 ob
.oi
.dump(formatter
);
1280 formatter
->close_section();
1286 int ret
= read_section(&type
, &ebl
);
1290 //cout << "\tdo_object: Section type " << hex << type << dec << std::endl;
1291 //cout << "\t\tsection size " << ebl.length() << std::endl;
1292 if (type
>= END_OF_TYPES
) {
1293 cout
<< "Skipping unknown object section type" << std::endl
;
1299 ret
= dump_data(formatter
, ebl
);
1300 if (ret
) return ret
;
1304 ret
= dump_attrs(formatter
, ob
.hoid
, ebl
);
1305 if (ret
) return ret
;
1309 ret
= dump_omap_hdr(formatter
, ebl
);
1310 if (ret
) return ret
;
1314 ret
= dump_omap(formatter
, ebl
);
1315 if (ret
) return ret
;
1317 case TYPE_OBJECT_END
:
1321 cerr
<< "Unknown section type " << type
<< std::endl
;
1325 formatter
->close_section();
1329 int ObjectStoreTool::get_object(ObjectStore
*store
,
1333 bufferlist
&bl
, OSDMap
&origmap
,
1334 bool *skipped_objects
)
1336 ObjectStore::Transaction tran
;
1337 ObjectStore::Transaction
*t
= &tran
;
1338 auto ebliter
= bl
.cbegin();
1342 if (ob
.hoid
.hobj
.is_temp()) {
1343 cerr
<< "ERROR: Export contains temporary object '" << ob
.hoid
<< "'" << std::endl
;
1346 ceph_assert(g_ceph_context
);
1348 auto ch
= store
->open_collection(coll
);
1349 if (ob
.hoid
.hobj
.nspace
!= g_ceph_context
->_conf
->osd_hit_set_namespace
) {
1350 object_t oid
= ob
.hoid
.hobj
.oid
;
1351 object_locator_t
loc(ob
.hoid
.hobj
);
1352 pg_t raw_pgid
= origmap
.object_locator_to_pg(oid
, loc
);
1353 pg_t pgid
= origmap
.raw_pg_to_pg(raw_pgid
);
1356 if (coll
.is_pg(&coll_pgid
) == false) {
1357 cerr
<< "INTERNAL ERROR: Bad collection during import" << std::endl
;
1360 if (coll_pgid
.shard
!= ob
.hoid
.shard_id
) {
1361 cerr
<< "INTERNAL ERROR: Importing shard " << coll_pgid
.shard
1362 << " but object shard is " << ob
.hoid
.shard_id
<< std::endl
;
1366 if (coll_pgid
.pgid
!= pgid
) {
1367 cerr
<< "Skipping object '" << ob
.hoid
<< "' which belongs in pg " << pgid
<< std::endl
;
1368 *skipped_objects
= true;
1375 t
->touch(coll
, ob
.hoid
);
1377 cout
<< "Write " << ob
.hoid
<< std::endl
;
1383 int ret
= read_section(&type
, &ebl
);
1387 //cout << "\tdo_object: Section type " << hex << type << dec << std::endl;
1388 //cout << "\t\tsection size " << ebl.length() << std::endl;
1389 if (type
>= END_OF_TYPES
) {
1390 cout
<< "Skipping unknown object section type" << std::endl
;
1396 ret
= get_data(store
, coll
, ob
.hoid
, t
, ebl
);
1397 if (ret
) return ret
;
1401 ret
= get_attrs(store
, coll
, ob
.hoid
, t
, ebl
, driver
, mapper
);
1402 if (ret
) return ret
;
1406 ret
= get_omap_hdr(store
, coll
, ob
.hoid
, t
, ebl
);
1407 if (ret
) return ret
;
1411 ret
= get_omap(store
, coll
, ob
.hoid
, t
, ebl
);
1412 if (ret
) return ret
;
1414 case TYPE_OBJECT_END
:
1418 cerr
<< "Unknown section type " << type
<< std::endl
;
1423 wait_until_done(t
, [&] {
1424 store
->queue_transaction(ch
, std::move(*t
));
1431 int dump_pg_metadata(Formatter
*formatter
, bufferlist
&bl
, metadata_section
&ms
)
1433 auto ebliter
= bl
.cbegin();
1436 formatter
->open_object_section("metadata_section");
1438 formatter
->dump_unsigned("pg_disk_version", (int)ms
.struct_ver
);
1439 formatter
->dump_unsigned("map_epoch", ms
.map_epoch
);
1441 formatter
->open_object_section("OSDMap");
1442 ms
.osdmap
.dump(formatter
);
1443 formatter
->close_section();
1444 formatter
->flush(cout
);
1447 formatter
->open_object_section("info");
1448 ms
.info
.dump(formatter
);
1449 formatter
->close_section();
1450 formatter
->flush(cout
);
1452 formatter
->open_object_section("log");
1453 ms
.log
.dump(formatter
);
1454 formatter
->close_section();
1455 formatter
->flush(cout
);
1457 formatter
->open_object_section("pg_missing_t");
1458 ms
.missing
.dump(formatter
);
1459 formatter
->close_section();
1461 // XXX: ms.past_intervals?
1463 formatter
->close_section();
1464 formatter
->flush(cout
);
1466 if (ms
.osdmap
.get_epoch() != 0 && ms
.map_epoch
!= ms
.osdmap
.get_epoch()) {
1467 cerr
<< "FATAL: Invalid OSDMap epoch in export data" << std::endl
;
1474 int get_pg_metadata(ObjectStore
*store
, bufferlist
&bl
, metadata_section
&ms
,
1475 const OSDSuperblock
& sb
, spg_t pgid
)
1477 auto ebliter
= bl
.cbegin();
1479 spg_t old_pgid
= ms
.info
.pgid
;
1480 ms
.info
.pgid
= pgid
;
1483 cout
<< "export pgid " << old_pgid
<< std::endl
;
1484 cout
<< "struct_v " << (int)ms
.struct_ver
<< std::endl
;
1485 cout
<< "map epoch " << ms
.map_epoch
<< std::endl
;
1488 Formatter
*formatter
= new JSONFormatter(true);
1489 formatter
->open_object_section("stuff");
1491 formatter
->open_object_section("importing OSDMap");
1492 ms
.osdmap
.dump(formatter
);
1493 formatter
->close_section();
1494 formatter
->flush(cout
);
1497 cout
<< "osd current epoch " << sb
.current_epoch
<< std::endl
;
1499 formatter
->open_object_section("info");
1500 ms
.info
.dump(formatter
);
1501 formatter
->close_section();
1502 formatter
->flush(cout
);
1505 formatter
->open_object_section("log");
1506 ms
.log
.dump(formatter
);
1507 formatter
->close_section();
1508 formatter
->flush(cout
);
1511 formatter
->close_section();
1512 formatter
->flush(cout
);
1517 if (ms
.osdmap
.get_epoch() != 0 && ms
.map_epoch
!= ms
.osdmap
.get_epoch()) {
1518 cerr
<< "FATAL: Invalid OSDMap epoch in export data" << std::endl
;
1522 if (ms
.map_epoch
> sb
.current_epoch
) {
1523 cerr
<< "ERROR: Export PG's map_epoch " << ms
.map_epoch
<< " > OSD's epoch " << sb
.current_epoch
<< std::endl
;
1524 cerr
<< "The OSD you are using is older than the exported PG" << std::endl
;
1525 cerr
<< "Either use another OSD or join selected OSD to cluster to update it first" << std::endl
;
1529 // Old exports didn't include OSDMap
1530 if (ms
.osdmap
.get_epoch() == 0) {
1531 cerr
<< "WARNING: No OSDMap in old export, this is an ancient export."
1532 " Not supported." << std::endl
;
1536 if (ms
.osdmap
.get_epoch() < sb
.oldest_map
) {
1537 cerr
<< "PG export's map " << ms
.osdmap
.get_epoch()
1538 << " is older than OSD's oldest_map " << sb
.oldest_map
<< std::endl
;
1540 cerr
<< " pass --force to proceed anyway (with incomplete PastIntervals)"
1546 cerr
<< "Import pgid " << ms
.info
.pgid
<< std::endl
;
1547 cerr
<< "Previous past_intervals " << ms
.past_intervals
<< std::endl
;
1548 cerr
<< "history.same_interval_since "
1549 << ms
.info
.history
.same_interval_since
<< std::endl
;
1555 // out: pg_log_t that only has entries that apply to import_pgid using curmap
1556 // reject: Entries rejected from "in" are in the reject.log. Other fields not set.
1557 void filter_divergent_priors(spg_t import_pgid
, const OSDMap
&curmap
,
1558 const string
&hit_set_namespace
, const divergent_priors_t
&in
,
1559 divergent_priors_t
&out
, divergent_priors_t
&reject
)
1564 for (divergent_priors_t::const_iterator i
= in
.begin();
1565 i
!= in
.end(); ++i
) {
1567 // Reject divergent priors for temporary objects
1568 if (i
->second
.is_temp()) {
1573 if (i
->second
.nspace
!= hit_set_namespace
) {
1574 object_t oid
= i
->second
.oid
;
1575 object_locator_t
loc(i
->second
);
1576 pg_t raw_pgid
= curmap
.object_locator_to_pg(oid
, loc
);
1577 pg_t pgid
= curmap
.raw_pg_to_pg(raw_pgid
);
1579 if (import_pgid
.pgid
== pgid
) {
1590 int ObjectStoreTool::dump_export(Formatter
*formatter
)
1594 PGLog::IndexedLog log
;
1595 //bool skipped_objects = false;
1597 int ret
= read_super();
1601 if (sh
.magic
!= super_header::super_magic
) {
1602 cerr
<< "Invalid magic number" << std::endl
;
1606 if (sh
.version
> super_header::super_ver
) {
1607 cerr
<< "Can't handle export format version=" << sh
.version
<< std::endl
;
1611 formatter
->open_object_section("Export");
1613 //First section must be TYPE_PG_BEGIN
1615 ret
= read_section(&type
, &ebl
);
1618 if (type
== TYPE_POOL_BEGIN
) {
1619 cerr
<< "Dump of pool exports not supported" << std::endl
;
1621 } else if (type
!= TYPE_PG_BEGIN
) {
1622 cerr
<< "Invalid first section type " << std::to_string(type
) << std::endl
;
1626 auto ebliter
= ebl
.cbegin();
1628 pgb
.decode(ebliter
);
1629 spg_t pgid
= pgb
.pgid
;
1631 formatter
->dump_string("pgid", stringify(pgid
));
1632 formatter
->dump_string("cluster_fsid", stringify(pgb
.superblock
.cluster_fsid
));
1633 formatter
->dump_string("features", stringify(pgb
.superblock
.compat_features
));
1636 bool found_metadata
= false;
1637 metadata_section ms
;
1638 bool objects_started
= false;
1640 ret
= read_section(&type
, &ebl
);
1645 cerr
<< "dump_export: Section type " << std::to_string(type
) << std::endl
;
1647 if (type
>= END_OF_TYPES
) {
1648 cerr
<< "Skipping unknown section type" << std::endl
;
1652 case TYPE_OBJECT_BEGIN
:
1653 if (!objects_started
) {
1654 formatter
->open_array_section("objects");
1655 objects_started
= true;
1657 ret
= dump_object(formatter
, ebl
);
1658 if (ret
) return ret
;
1660 case TYPE_PG_METADATA
:
1661 if (objects_started
)
1662 cerr
<< "WARNING: metadata_section out of order" << std::endl
;
1663 ret
= dump_pg_metadata(formatter
, ebl
, ms
);
1664 if (ret
) return ret
;
1665 found_metadata
= true;
1668 if (objects_started
) {
1669 formatter
->close_section();
1674 cerr
<< "Unknown section type " << std::to_string(type
) << std::endl
;
1679 if (!found_metadata
) {
1680 cerr
<< "Missing metadata section" << std::endl
;
1684 formatter
->close_section();
1685 formatter
->flush(cout
);
1690 int ObjectStoreTool::do_import(ObjectStore
*store
, OSDSuperblock
& sb
,
1691 bool force
, std::string pgidstr
)
1695 PGLog::IndexedLog log
;
1696 bool skipped_objects
= false;
1699 finish_remove_pgs(store
);
1701 int ret
= read_super();
1705 if (sh
.magic
!= super_header::super_magic
) {
1706 cerr
<< "Invalid magic number" << std::endl
;
1710 if (sh
.version
> super_header::super_ver
) {
1711 cerr
<< "Can't handle export format version=" << sh
.version
<< std::endl
;
1715 //First section must be TYPE_PG_BEGIN
1717 ret
= read_section(&type
, &ebl
);
1720 if (type
== TYPE_POOL_BEGIN
) {
1721 cerr
<< "Pool exports cannot be imported into a PG" << std::endl
;
1723 } else if (type
!= TYPE_PG_BEGIN
) {
1724 cerr
<< "Invalid first section type " << std::to_string(type
) << std::endl
;
1728 auto ebliter
= ebl
.cbegin();
1730 pgb
.decode(ebliter
);
1731 spg_t pgid
= pgb
.pgid
;
1733 if (pgidstr
.length()) {
1736 bool ok
= user_pgid
.parse(pgidstr
.c_str());
1737 // This succeeded in main() already
1739 if (pgid
!= user_pgid
) {
1740 cerr
<< "specified pgid " << user_pgid
1741 << " does not match actual pgid " << pgid
<< std::endl
;
1746 if (!pgb
.superblock
.cluster_fsid
.is_zero()
1747 && pgb
.superblock
.cluster_fsid
!= sb
.cluster_fsid
) {
1748 cerr
<< "Export came from different cluster with fsid "
1749 << pgb
.superblock
.cluster_fsid
<< std::endl
;
1754 cerr
<< "Exported features: " << pgb
.superblock
.compat_features
<< std::endl
;
1757 // Special case: Old export has SHARDS incompat feature on replicated pg, removqqe it
1758 if (pgid
.is_no_shard())
1759 pgb
.superblock
.compat_features
.incompat
.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS
);
1761 if (sb
.compat_features
.compare(pgb
.superblock
.compat_features
) == -1) {
1762 CompatSet unsupported
= sb
.compat_features
.unsupported(pgb
.superblock
.compat_features
);
1764 cerr
<< "Export has incompatible features set " << unsupported
<< std::endl
;
1766 // Let them import if they specify the --force option
1768 return 11; // Positive return means exit status
1771 // we need the latest OSDMap to check for collisions
1774 ret
= get_osdmap(store
, sb
.current_epoch
, curmap
, bl
);
1776 cerr
<< "Can't find latest local OSDMap " << sb
.current_epoch
<< std::endl
;
1779 if (!curmap
.have_pg_pool(pgid
.pgid
.m_pool
)) {
1780 cerr
<< "Pool " << pgid
.pgid
.m_pool
<< " no longer exists" << std::endl
;
1781 // Special exit code for this error, used by test code
1782 return 10; // Positive return means exit status
1785 pool_pg_num_history_t pg_num_history
;
1786 get_pg_num_history(store
, &pg_num_history
);
1788 ghobject_t pgmeta_oid
= pgid
.make_pgmeta_oid();
1790 // Check for PG already present.
1792 if (store
->collection_exists(coll
)) {
1793 cerr
<< "pgid " << pgid
<< " already exists" << std::endl
;
1797 ObjectStore::CollectionHandle ch
;
1802 OSD::make_snapmapper_oid());
1803 SnapMapper
mapper(g_ceph_context
, &driver
, 0, 0, 0, pgid
.shard
);
1805 cout
<< "Importing pgid " << pgid
;
1809 bool found_metadata
= false;
1810 metadata_section ms
;
1812 ret
= read_section(&type
, &ebl
);
1817 cout
<< __func__
<< ": Section type " << std::to_string(type
) << std::endl
;
1819 if (type
>= END_OF_TYPES
) {
1820 cout
<< "Skipping unknown section type" << std::endl
;
1824 case TYPE_OBJECT_BEGIN
:
1825 ceph_assert(found_metadata
);
1826 ret
= get_object(store
, driver
, mapper
, coll
, ebl
, ms
.osdmap
,
1828 if (ret
) return ret
;
1830 case TYPE_PG_METADATA
:
1831 ret
= get_pg_metadata(store
, ebl
, ms
, sb
, pgid
);
1832 if (ret
) return ret
;
1833 found_metadata
= true;
1835 if (pgid
!= ms
.info
.pgid
) {
1836 cerr
<< "specified pgid " << pgid
<< " does not match import file pgid "
1837 << ms
.info
.pgid
<< std::endl
;
1841 // make sure there are no conflicting splits or merges
1842 if (ms
.osdmap
.have_pg_pool(pgid
.pgid
.pool())) {
1843 auto p
= pg_num_history
.pg_nums
.find(pgid
.pgid
.m_pool
);
1844 if (p
!= pg_num_history
.pg_nums
.end() &&
1845 !p
->second
.empty()) {
1846 unsigned start_pg_num
= ms
.osdmap
.get_pg_num(pgid
.pgid
.pool());
1847 unsigned pg_num
= start_pg_num
;
1848 for (auto q
= p
->second
.lower_bound(ms
.map_epoch
);
1849 q
!= p
->second
.end();
1851 unsigned new_pg_num
= q
->second
;
1852 cout
<< "pool " << pgid
.pgid
.pool() << " pg_num " << pg_num
1853 << " -> " << new_pg_num
<< std::endl
;
1855 // check for merge target
1857 if (pgid
.is_merge_source(pg_num
, new_pg_num
, &target
)) {
1858 // FIXME: this checks assumes the OSD's PG is at the OSD's
1859 // map epoch; it could be, say, at *our* epoch, pre-merge.
1860 coll_t
coll(target
);
1861 if (store
->collection_exists(coll
)) {
1862 cerr
<< "pgid " << pgid
<< " merges to target " << target
1863 << " which already exists" << std::endl
;
1868 // check for split children
1869 set
<spg_t
> children
;
1870 if (pgid
.is_split(start_pg_num
, new_pg_num
, &children
)) {
1871 cerr
<< " children are " << children
<< std::endl
;
1872 for (auto child
: children
) {
1874 if (store
->collection_exists(coll
)) {
1875 cerr
<< "pgid " << pgid
<< " splits to " << children
1876 << " and " << child
<< " exists" << std::endl
;
1881 pg_num
= new_pg_num
;
1885 cout
<< "pool " << pgid
.pgid
.pool() << " doesn't existing, not checking"
1886 << " for splits or mergers" << std::endl
;
1890 ObjectStore::Transaction t
;
1891 ch
= store
->create_new_collection(coll
);
1892 create_pg_collection(
1894 pgid
.get_split_bits(ms
.osdmap
.get_pg_pool(pgid
.pool())->get_pg_num()));
1895 init_pg_ondisk(t
, pgid
, NULL
);
1897 // mark this coll for removal until we're done
1898 map
<string
,bufferlist
> values
;
1899 encode((char)1, values
["_remove"]);
1900 t
.omap_setkeys(coll
, pgid
.make_pgmeta_oid(), values
);
1902 store
->queue_transaction(ch
, std::move(t
));
1907 ceph_assert(found_metadata
);
1911 cerr
<< "Unknown section type " << std::to_string(type
) << std::endl
;
1916 if (!found_metadata
) {
1917 cerr
<< "Missing metadata section" << std::endl
;
1921 ObjectStore::Transaction t
;
1923 pg_log_t newlog
, reject
;
1924 pg_log_t::filter_log(pgid
, ms
.osdmap
, g_ceph_context
->_conf
->osd_hit_set_namespace
,
1925 ms
.log
, newlog
, reject
);
1927 for (list
<pg_log_entry_t
>::iterator i
= newlog
.log
.begin();
1928 i
!= newlog
.log
.end(); ++i
)
1929 cerr
<< "Keeping log entry " << *i
<< std::endl
;
1930 for (list
<pg_log_entry_t
>::iterator i
= reject
.log
.begin();
1931 i
!= reject
.log
.end(); ++i
)
1932 cerr
<< "Skipping log entry " << *i
<< std::endl
;
1935 divergent_priors_t newdp
, rejectdp
;
1936 filter_divergent_priors(pgid
, ms
.osdmap
, g_ceph_context
->_conf
->osd_hit_set_namespace
,
1937 ms
.divergent_priors
, newdp
, rejectdp
);
1938 ms
.divergent_priors
= newdp
;
1940 for (divergent_priors_t::iterator i
= newdp
.begin();
1941 i
!= newdp
.end(); ++i
)
1942 cerr
<< "Keeping divergent_prior " << *i
<< std::endl
;
1943 for (divergent_priors_t::iterator i
= rejectdp
.begin();
1944 i
!= rejectdp
.end(); ++i
)
1945 cerr
<< "Skipping divergent_prior " << *i
<< std::endl
;
1948 ms
.missing
.filter_objects([&](const hobject_t
&obj
) {
1949 if (obj
.nspace
== g_ceph_context
->_conf
->osd_hit_set_namespace
)
1951 ceph_assert(!obj
.is_temp());
1952 object_t oid
= obj
.oid
;
1953 object_locator_t
loc(obj
);
1954 pg_t raw_pgid
= ms
.osdmap
.object_locator_to_pg(oid
, loc
);
1955 pg_t _pgid
= ms
.osdmap
.raw_pg_to_pg(raw_pgid
);
1957 return pgid
.pgid
!= _pgid
;
1962 pg_missing_t missing
;
1963 Formatter
*formatter
= Formatter::create("json-pretty");
1964 dump_log(formatter
, cerr
, newlog
, ms
.missing
);
1968 // Just like a split invalidate stats since the object count is changed
1969 if (skipped_objects
)
1970 ms
.info
.stats
.stats_invalid
= true;
1978 ms
.divergent_priors
,
1980 if (ret
) return ret
;
1983 // done, clear removal flag
1985 cerr
<< "done, clearing removal flag" << std::endl
;
1988 t
.omap_rmkey(coll
, pgid
.make_pgmeta_oid(), "_remove");
1989 wait_until_done(&t
, [&] {
1990 store
->queue_transaction(ch
, std::move(t
));
1991 // make sure we flush onreadable items before mapper/driver are destroyed.
1998 int do_list(ObjectStore
*store
, string pgidstr
, string object
, boost::optional
<std::string
> nspace
,
1999 Formatter
*formatter
, bool debug
, bool human_readable
, bool head
)
2002 lookup_ghobject
lookup(object
, nspace
, head
);
2003 if (pgidstr
.length() > 0) {
2004 r
= action_on_all_objects_in_pg(store
, pgidstr
, lookup
, debug
);
2006 r
= action_on_all_objects(store
, lookup
, debug
);
2010 lookup
.dump(formatter
, human_readable
);
2011 formatter
->flush(cout
);
2015 int do_list_slow(ObjectStore
*store
, string pgidstr
, string object
,
2016 double threshold
, Formatter
*formatter
, bool debug
, bool human_readable
)
2019 lookup_slow_ghobject
lookup(object
, threshold
);
2020 if (pgidstr
.length() > 0) {
2021 r
= action_on_all_objects_in_pg(store
, pgidstr
, lookup
, debug
);
2023 r
= action_on_all_objects(store
, lookup
, debug
);
2027 lookup
.dump(formatter
, human_readable
);
2028 formatter
->flush(cout
);
2032 int do_meta(ObjectStore
*store
, string object
, Formatter
*formatter
, bool debug
, bool human_readable
)
2035 boost::optional
<std::string
> nspace
; // Not specified
2036 lookup_ghobject
lookup(object
, nspace
);
2037 r
= action_on_all_objects_in_exact_pg(store
, coll_t::meta(), lookup
, debug
);
2040 lookup
.dump(formatter
, human_readable
);
2041 formatter
->flush(cout
);
2051 int remove_object(coll_t coll
, ghobject_t
&ghobj
,
2053 MapCacher::Transaction
<std::string
, bufferlist
> *_t
,
2054 ObjectStore::Transaction
*t
,
2057 if (type
== BOTH
|| type
== SNAPMAP
) {
2058 int r
= mapper
.remove_oid(ghobj
.hobj
, _t
);
2059 if (r
< 0 && r
!= -ENOENT
) {
2060 cerr
<< "remove_oid returned " << cpp_strerror(r
) << std::endl
;
2065 if (type
== BOTH
|| type
== NOSNAPMAP
) {
2066 t
->remove(coll
, ghobj
);
2071 int get_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, SnapSet
&ss
, bool silent
);
2073 int do_remove_object(ObjectStore
*store
, coll_t coll
,
2074 ghobject_t
&ghobj
, bool all
, bool force
, enum rmtype type
)
2076 auto ch
= store
->open_collection(coll
);
2078 coll
.is_pg_prefix(&pg
);
2082 OSD::make_snapmapper_oid());
2083 SnapMapper
mapper(g_ceph_context
, &driver
, 0, 0, 0, pg
.shard
);
2086 int r
= store
->stat(ch
, ghobj
, &st
);
2088 cerr
<< "remove: " << cpp_strerror(r
) << std::endl
;
2093 if (ghobj
.hobj
.has_snapset()) {
2094 r
= get_snapset(store
, coll
, ghobj
, ss
, false);
2096 cerr
<< "Can't get snapset error " << cpp_strerror(r
) << std::endl
;
2097 // If --force and bad snapset let them remove the head
2098 if (!(force
&& !all
))
2101 // cout << "snapset " << ss << std::endl;
2102 if (!ss
.clone_snaps
.empty() && !all
) {
2104 cout
<< "WARNING: only removing "
2105 << (ghobj
.hobj
.is_head() ? "head" : "snapdir")
2106 << " with clones present" << std::endl
;
2107 ss
.clone_snaps
.clear();
2109 cerr
<< "Clones are present, use removeall to delete everything"
2116 ObjectStore::Transaction t
;
2117 OSDriver::OSTransaction
_t(driver
.get_transaction(&t
));
2119 ghobject_t snapobj
= ghobj
;
2120 for (auto& p
: ss
.clone_snaps
) {
2121 snapobj
.hobj
.snap
= p
.first
;
2122 cout
<< "remove clone " << snapobj
<< std::endl
;
2124 r
= remove_object(coll
, snapobj
, mapper
, &_t
, &t
, type
);
2130 cout
<< "remove " << ghobj
<< std::endl
;
2133 r
= remove_object(coll
, ghobj
, mapper
, &_t
, &t
, type
);
2139 wait_until_done(&t
, [&] {
2140 store
->queue_transaction(ch
, std::move(t
));
2147 int do_list_attrs(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2149 auto ch
= store
->open_collection(coll
);
2150 map
<string
,bufferptr
,less
<>> aset
;
2151 int r
= store
->getattrs(ch
, ghobj
, aset
);
2153 cerr
<< "getattrs: " << cpp_strerror(r
) << std::endl
;
2157 for (map
<string
,bufferptr
>::iterator i
= aset
.begin();i
!= aset
.end(); ++i
) {
2158 string
key(i
->first
);
2160 key
= cleanbin(key
);
2161 cout
<< key
<< std::endl
;
2166 int do_list_omap(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2168 auto ch
= store
->open_collection(coll
);
2169 ObjectMap::ObjectMapIterator iter
= store
->get_omap_iterator(ch
, ghobj
);
2171 cerr
<< "omap_get_iterator: " << cpp_strerror(ENOENT
) << std::endl
;
2174 iter
->seek_to_first();
2175 map
<string
, bufferlist
> oset
;
2176 while(iter
->valid()) {
2177 get_omap_batch(iter
, oset
);
2179 for (map
<string
,bufferlist
>::iterator i
= oset
.begin();i
!= oset
.end(); ++i
) {
2180 string
key(i
->first
);
2182 key
= cleanbin(key
);
2183 cout
<< key
<< std::endl
;
2189 int do_get_bytes(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, int fd
)
2191 auto ch
= store
->open_collection(coll
);
2195 int ret
= store
->stat(ch
, ghobj
, &st
);
2197 cerr
<< "get-bytes: " << cpp_strerror(ret
) << std::endl
;
2203 cerr
<< "size=" << total
<< std::endl
;
2205 uint64_t offset
= 0;
2206 bufferlist rawdatabl
;
2209 mysize_t len
= max_read
;
2213 ret
= store
->read(ch
, ghobj
, offset
, len
, rawdatabl
);
2220 cerr
<< "data section offset=" << offset
<< " len=" << len
<< std::endl
;
2225 ret
= write(fd
, rawdatabl
.c_str(), ret
);
2235 int do_set_bytes(ObjectStore
*store
, coll_t coll
,
2236 ghobject_t
&ghobj
, int fd
)
2238 ObjectStore::Transaction tran
;
2239 ObjectStore::Transaction
*t
= &tran
;
2242 cerr
<< "Write " << ghobj
<< std::endl
;
2245 t
->touch(coll
, ghobj
);
2246 t
->truncate(coll
, ghobj
, 0);
2249 uint64_t offset
= 0;
2250 bufferlist rawdatabl
;
2253 ssize_t bytes
= rawdatabl
.read_fd(fd
, max_read
);
2255 cerr
<< "read_fd error " << cpp_strerror(bytes
) << std::endl
;
2263 cerr
<< "\tdata: offset " << offset
<< " bytes " << bytes
<< std::endl
;
2265 t
->write(coll
, ghobj
, offset
, bytes
, rawdatabl
);
2268 // XXX: Should we queue_transaction() every once in a while for very large files
2271 auto ch
= store
->open_collection(coll
);
2273 store
->queue_transaction(ch
, std::move(*t
));
2277 int do_get_attr(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, string key
)
2279 auto ch
= store
->open_collection(coll
);
2282 int r
= store
->getattr(ch
, ghobj
, key
.c_str(), bp
);
2284 cerr
<< "getattr: " << cpp_strerror(r
) << std::endl
;
2288 string
value(bp
.c_str(), bp
.length());
2290 value
= cleanbin(value
);
2291 value
.push_back('\n');
2298 int do_set_attr(ObjectStore
*store
, coll_t coll
,
2299 ghobject_t
&ghobj
, string key
, int fd
)
2301 ObjectStore::Transaction tran
;
2302 ObjectStore::Transaction
*t
= &tran
;
2306 cerr
<< "Setattr " << ghobj
<< std::endl
;
2308 int ret
= get_fd_data(fd
, bl
);
2315 t
->touch(coll
, ghobj
);
2317 t
->setattr(coll
, ghobj
, key
, bl
);
2319 auto ch
= store
->open_collection(coll
);
2320 store
->queue_transaction(ch
, std::move(*t
));
2324 int do_rm_attr(ObjectStore
*store
, coll_t coll
,
2325 ghobject_t
&ghobj
, string key
)
2327 ObjectStore::Transaction tran
;
2328 ObjectStore::Transaction
*t
= &tran
;
2331 cerr
<< "Rmattr " << ghobj
<< std::endl
;
2336 t
->rmattr(coll
, ghobj
, key
);
2338 auto ch
= store
->open_collection(coll
);
2339 store
->queue_transaction(ch
, std::move(*t
));
2343 int do_get_omap(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, string key
)
2345 auto ch
= store
->open_collection(coll
);
2347 map
<string
, bufferlist
> out
;
2351 int r
= store
->omap_get_values(ch
, ghobj
, keys
, &out
);
2353 cerr
<< "omap_get_values: " << cpp_strerror(r
) << std::endl
;
2358 cerr
<< "Key not found" << std::endl
;
2362 ceph_assert(out
.size() == 1);
2364 bufferlist bl
= out
.begin()->second
;
2365 string
value(bl
.c_str(), bl
.length());
2367 value
= cleanbin(value
);
2368 value
.push_back('\n');
2375 int do_set_omap(ObjectStore
*store
, coll_t coll
,
2376 ghobject_t
&ghobj
, string key
, int fd
)
2378 ObjectStore::Transaction tran
;
2379 ObjectStore::Transaction
*t
= &tran
;
2380 map
<string
, bufferlist
> attrset
;
2384 cerr
<< "Set_omap " << ghobj
<< std::endl
;
2386 int ret
= get_fd_data(fd
, valbl
);
2390 attrset
.insert(pair
<string
, bufferlist
>(key
, valbl
));
2395 t
->touch(coll
, ghobj
);
2397 t
->omap_setkeys(coll
, ghobj
, attrset
);
2399 auto ch
= store
->open_collection(coll
);
2400 store
->queue_transaction(ch
, std::move(*t
));
2404 int do_rm_omap(ObjectStore
*store
, coll_t coll
,
2405 ghobject_t
&ghobj
, string key
)
2407 ObjectStore::Transaction tran
;
2408 ObjectStore::Transaction
*t
= &tran
;
2411 cerr
<< "Rm_omap " << ghobj
<< std::endl
;
2416 t
->omap_rmkey(coll
, ghobj
, key
);
2418 auto ch
= store
->open_collection(coll
);
2419 store
->queue_transaction(ch
, std::move(*t
));
2423 int do_get_omaphdr(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
)
2425 auto ch
= store
->open_collection(coll
);
2428 int r
= store
->omap_get_header(ch
, ghobj
, &hdrbl
, true);
2430 cerr
<< "omap_get_header: " << cpp_strerror(r
) << std::endl
;
2434 string
header(hdrbl
.c_str(), hdrbl
.length());
2436 header
= cleanbin(header
);
2437 header
.push_back('\n');
2444 int do_set_omaphdr(ObjectStore
*store
, coll_t coll
,
2445 ghobject_t
&ghobj
, int fd
)
2447 ObjectStore::Transaction tran
;
2448 ObjectStore::Transaction
*t
= &tran
;
2452 cerr
<< "Omap_setheader " << ghobj
<< std::endl
;
2454 int ret
= get_fd_data(fd
, hdrbl
);
2461 t
->touch(coll
, ghobj
);
2463 t
->omap_setheader(coll
, ghobj
, hdrbl
);
2465 auto ch
= store
->open_collection(coll
);
2466 store
->queue_transaction(ch
, std::move(*t
));
2470 struct do_fix_lost
: public action_on_object_t
{
2471 void call(ObjectStore
*store
, coll_t coll
,
2472 ghobject_t
&ghobj
, object_info_t
&oi
) override
{
2474 cout
<< coll
<< "/" << ghobj
<< " is lost";
2480 oi
.clear_flag(object_info_t::FLAG_LOST
);
2482 encode(oi
, bl
, -1); /* fixme: using full features */
2483 ObjectStore::Transaction t
;
2484 t
.setattr(coll
, ghobj
, OI_ATTR
, bl
);
2485 auto ch
= store
->open_collection(coll
);
2486 store
->queue_transaction(ch
, std::move(t
));
2492 int get_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, SnapSet
&ss
, bool silent
= false)
2494 auto ch
= store
->open_collection(coll
);
2496 int r
= store
->getattr(ch
, ghobj
, SS_ATTR
, attr
);
2499 cerr
<< "Error getting snapset on : " << make_pair(coll
, ghobj
) << ", "
2500 << cpp_strerror(r
) << std::endl
;
2503 auto bp
= attr
.cbegin();
2508 cerr
<< "Error decoding snapset on : " << make_pair(coll
, ghobj
) << ", "
2509 << cpp_strerror(r
) << std::endl
;
2515 int print_obj_info(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, Formatter
* formatter
)
2517 auto ch
= store
->open_collection(coll
);
2519 formatter
->open_object_section("obj");
2520 formatter
->open_object_section("id");
2521 ghobj
.dump(formatter
);
2522 formatter
->close_section();
2525 int gr
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2528 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2529 << cpp_strerror(r
) << std::endl
;
2532 auto bp
= attr
.cbegin();
2535 formatter
->open_object_section("info");
2537 formatter
->close_section();
2540 cerr
<< "Error decoding attr on : " << make_pair(coll
, ghobj
) << ", "
2541 << cpp_strerror(r
) << std::endl
;
2545 int sr
= store
->stat(ch
, ghobj
, &st
, true);
2548 cerr
<< "Error stat on : " << make_pair(coll
, ghobj
) << ", "
2549 << cpp_strerror(r
) << std::endl
;
2551 formatter
->open_object_section("stat");
2552 formatter
->dump_int("size", st
.st_size
);
2553 formatter
->dump_int("blksize", st
.st_blksize
);
2554 formatter
->dump_int("blocks", st
.st_blocks
);
2555 formatter
->dump_int("nlink", st
.st_nlink
);
2556 formatter
->close_section();
2559 if (ghobj
.hobj
.has_snapset()) {
2561 int snr
= get_snapset(store
, coll
, ghobj
, ss
);
2565 formatter
->open_object_section("SnapSet");
2567 formatter
->close_section();
2571 gr
= store
->getattr(ch
, ghobj
, ECUtil::get_hinfo_key(), hattr
);
2573 ECUtil::HashInfo hinfo
;
2574 auto hp
= hattr
.cbegin();
2577 formatter
->open_object_section("hinfo");
2578 hinfo
.dump(formatter
);
2579 formatter
->close_section();
2582 cerr
<< "Error decoding hinfo on : " << make_pair(coll
, ghobj
) << ", "
2583 << cpp_strerror(r
) << std::endl
;
2586 gr
= store
->dump_onode(ch
, ghobj
, "onode", formatter
);
2588 formatter
->close_section();
2589 formatter
->flush(cout
);
2594 int corrupt_info(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, Formatter
* formatter
)
2596 auto ch
= store
->open_collection(coll
);
2598 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2600 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2601 << cpp_strerror(r
) << std::endl
;
2605 auto bp
= attr
.cbegin();
2610 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2611 << cpp_strerror(r
) << std::endl
;
2616 oi
.alloc_hint_flags
+= 0xff;
2617 ObjectStore::Transaction t
;
2618 encode(oi
, attr
, -1); /* fixme: using full features */
2619 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2620 auto ch
= store
->open_collection(coll
);
2621 r
= store
->queue_transaction(ch
, std::move(t
));
2623 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2624 << cpp_strerror(r
) << std::endl
;
2632 ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, uint64_t setsize
, Formatter
* formatter
,
2635 auto ch
= store
->open_collection(coll
);
2636 if (ghobj
.hobj
.is_snapdir()) {
2637 cerr
<< "Can't set the size of a snapdir" << std::endl
;
2641 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2643 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2644 << cpp_strerror(r
) << std::endl
;
2648 auto bp
= attr
.cbegin();
2653 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2654 << cpp_strerror(r
) << std::endl
;
2658 r
= store
->stat(ch
, ghobj
, &st
, true);
2660 cerr
<< "Error stat on : " << make_pair(coll
, ghobj
) << ", "
2661 << cpp_strerror(r
) << std::endl
;
2663 ghobject_t
head(ghobj
);
2665 bool found_head
= true;
2666 map
<snapid_t
, uint64_t>::iterator csi
;
2667 bool is_snap
= ghobj
.hobj
.is_snap();
2669 head
.hobj
= head
.hobj
.get_head();
2670 r
= get_snapset(store
, coll
, head
, ss
, true);
2671 if (r
< 0 && r
!= -ENOENT
) {
2672 // Requested get_snapset() silent, so if not -ENOENT show error
2673 cerr
<< "Error getting snapset on : " << make_pair(coll
, head
) << ", "
2674 << cpp_strerror(r
) << std::endl
;
2678 head
.hobj
= head
.hobj
.get_snapdir();
2679 r
= get_snapset(store
, coll
, head
, ss
);
2686 csi
= ss
.clone_size
.find(ghobj
.hobj
.snap
);
2687 if (csi
== ss
.clone_size
.end()) {
2688 cerr
<< "SnapSet is missing clone_size for snap " << ghobj
.hobj
.snap
<< std::endl
;
2692 if ((uint64_t)st
.st_size
== setsize
&& oi
.size
== setsize
2693 && (!is_snap
|| csi
->second
== setsize
)) {
2694 cout
<< "Size of object is already " << setsize
<< std::endl
;
2697 cout
<< "Setting size to " << setsize
<< ", stat size " << st
.st_size
2698 << ", obj info size " << oi
.size
;
2700 cout
<< ", " << (found_head
? "head" : "snapdir")
2701 << " clone_size " << csi
->second
;
2702 csi
->second
= setsize
;
2708 ObjectStore::Transaction t
;
2709 // Only modify object info if we want to corrupt it
2710 if (!corrupt
&& (uint64_t)st
.st_size
!= setsize
) {
2711 t
.truncate(coll
, ghobj
, setsize
);
2712 // Changing objectstore size will invalidate data_digest, so clear it.
2713 oi
.clear_data_digest();
2715 encode(oi
, attr
, -1); /* fixme: using full features */
2716 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2718 bufferlist snapattr
;
2720 encode(ss
, snapattr
);
2721 t
.setattr(coll
, head
, SS_ATTR
, snapattr
);
2723 auto ch
= store
->open_collection(coll
);
2724 r
= store
->queue_transaction(ch
, std::move(t
));
2726 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2727 << cpp_strerror(r
) << std::endl
;
2734 int clear_data_digest(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
) {
2735 auto ch
= store
->open_collection(coll
);
2737 int r
= store
->getattr(ch
, ghobj
, OI_ATTR
, attr
);
2739 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2740 << cpp_strerror(r
) << std::endl
;
2744 auto bp
= attr
.cbegin();
2749 cerr
<< "Error getting attr on : " << make_pair(coll
, ghobj
) << ", "
2750 << cpp_strerror(r
) << std::endl
;
2755 oi
.clear_data_digest();
2756 encode(oi
, attr
, -1); /* fixme: using full features */
2757 ObjectStore::Transaction t
;
2758 t
.setattr(coll
, ghobj
, OI_ATTR
, attr
);
2759 auto ch
= store
->open_collection(coll
);
2760 r
= store
->queue_transaction(ch
, std::move(t
));
2762 cerr
<< "Error writing object info: " << make_pair(coll
, ghobj
) << ", "
2763 << cpp_strerror(r
) << std::endl
;
2770 int clear_snapset(ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
,
2774 int ret
= get_snapset(store
, coll
, ghobj
, ss
);
2778 // Use "corrupt" to clear entire SnapSet
2779 // Use "seq" to just corrupt SnapSet.seq
2780 if (arg
== "corrupt" || arg
== "seq")
2782 // Use "snaps" to just clear SnapSet.clone_snaps
2783 if (arg
== "corrupt" || arg
== "snaps")
2784 ss
.clone_snaps
.clear();
2785 // By default just clear clone, clone_overlap and clone_size
2786 if (arg
== "corrupt")
2788 if (arg
== "" || arg
== "clones")
2790 if (arg
== "" || arg
== "clone_overlap")
2791 ss
.clone_overlap
.clear();
2792 if (arg
== "" || arg
== "clone_size")
2793 ss
.clone_size
.clear();
2794 // Break all clone sizes by adding 1
2795 if (arg
== "size") {
2796 for (map
<snapid_t
, uint64_t>::iterator i
= ss
.clone_size
.begin();
2797 i
!= ss
.clone_size
.end(); ++i
)
2804 ObjectStore::Transaction t
;
2805 t
.setattr(coll
, ghobj
, SS_ATTR
, bl
);
2806 auto ch
= store
->open_collection(coll
);
2807 int r
= store
->queue_transaction(ch
, std::move(t
));
2809 cerr
<< "Error setting snapset on : " << make_pair(coll
, ghobj
) << ", "
2810 << cpp_strerror(r
) << std::endl
;
2817 vector
<snapid_t
>::iterator
find(vector
<snapid_t
> &v
, snapid_t clid
)
2819 return std::find(v
.begin(), v
.end(), clid
);
2822 map
<snapid_t
, interval_set
<uint64_t> >::iterator
2823 find(map
<snapid_t
, interval_set
<uint64_t> > &m
, snapid_t clid
)
2825 return m
.find(clid
);
2828 map
<snapid_t
, uint64_t>::iterator
find(map
<snapid_t
, uint64_t> &m
,
2831 return m
.find(clid
);
2835 int remove_from(T
&mv
, string name
, snapid_t cloneid
, bool force
)
2837 typename
T::iterator i
= find(mv
, cloneid
);
2838 if (i
!= mv
.end()) {
2841 cerr
<< "Clone " << cloneid
<< " doesn't exist in " << name
;
2843 cerr
<< " (ignored)" << std::endl
;
2853 ObjectStore
*store
, coll_t coll
, ghobject_t
&ghobj
, snapid_t cloneid
, bool force
)
2855 // XXX: Don't allow this if in a cache tier or former cache tier
2856 // bool allow_incomplete_clones() const {
2857 // return cache_mode != CACHEMODE_NONE || has_flag(FLAG_INCOMPLETE_CLONES);
2860 int ret
= get_snapset(store
, coll
, ghobj
, snapset
);
2864 // Derived from trim_object()
2866 vector
<snapid_t
>::iterator p
;
2867 for (p
= snapset
.clones
.begin(); p
!= snapset
.clones
.end(); ++p
)
2870 if (p
== snapset
.clones
.end()) {
2871 cerr
<< "Clone " << cloneid
<< " not present";
2874 if (p
!= snapset
.clones
.begin()) {
2875 // not the oldest... merge overlap into next older clone
2876 vector
<snapid_t
>::iterator n
= p
- 1;
2877 hobject_t prev_coid
= ghobj
.hobj
;
2878 prev_coid
.snap
= *n
;
2879 //bool adjust_prev_bytes = is_present_clone(prev_coid);
2881 //if (adjust_prev_bytes)
2882 // ctx->delta_stats.num_bytes -= snapset.get_clone_bytes(*n);
2884 snapset
.clone_overlap
[*n
].intersection_of(
2885 snapset
.clone_overlap
[*p
]);
2887 //if (adjust_prev_bytes)
2888 // ctx->delta_stats.num_bytes += snapset.get_clone_bytes(*n);
2891 ret
= remove_from(snapset
.clones
, "clones", cloneid
, force
);
2892 if (ret
) return ret
;
2893 ret
= remove_from(snapset
.clone_overlap
, "clone_overlap", cloneid
, force
);
2894 if (ret
) return ret
;
2895 ret
= remove_from(snapset
.clone_size
, "clone_size", cloneid
, force
);
2896 if (ret
) return ret
;
2902 encode(snapset
, bl
);
2903 ObjectStore::Transaction t
;
2904 t
.setattr(coll
, ghobj
, SS_ATTR
, bl
);
2905 auto ch
= store
->open_collection(coll
);
2906 int r
= store
->queue_transaction(ch
, std::move(t
));
2908 cerr
<< "Error setting snapset on : " << make_pair(coll
, ghobj
) << ", "
2909 << cpp_strerror(r
) << std::endl
;
2912 cout
<< "Removal of clone " << cloneid
<< " complete" << std::endl
;
2913 cout
<< "Use pg repair after OSD restarted to correct stat information" << std::endl
;
2917 int dup(string srcpath
, ObjectStore
*src
, string dstpath
, ObjectStore
*dst
)
2919 cout
<< "dup from " << src
->get_type() << ": " << srcpath
<< "\n"
2920 << " to " << dst
->get_type() << ": " << dstpath
2923 vector
<coll_t
> collections
;
2928 cerr
<< "failed to mount src: " << cpp_strerror(r
) << std::endl
;
2933 cerr
<< "failed to mount dst: " << cpp_strerror(r
) << std::endl
;
2937 if (src
->get_fsid() != dst
->get_fsid()) {
2938 cerr
<< "src fsid " << src
->get_fsid() << " != dest " << dst
->get_fsid()
2942 cout
<< "fsid " << src
->get_fsid() << std::endl
;
2944 // make sure dst is empty
2945 r
= dst
->list_collections(collections
);
2947 cerr
<< "error listing collections on dst: " << cpp_strerror(r
) << std::endl
;
2950 if (!collections
.empty()) {
2951 cerr
<< "destination store is not empty" << std::endl
;
2955 r
= src
->list_collections(collections
);
2957 cerr
<< "error listing collections on src: " << cpp_strerror(r
) << std::endl
;
2961 num
= collections
.size();
2962 cout
<< num
<< " collections" << std::endl
;
2964 for (auto cid
: collections
) {
2965 cout
<< i
++ << "/" << num
<< " " << cid
<< std::endl
;
2966 auto ch
= src
->open_collection(cid
);
2967 auto dch
= dst
->create_new_collection(cid
);
2969 ObjectStore::Transaction t
;
2970 int bits
= src
->collection_bits(ch
);
2972 if (src
->get_type() == "filestore" && cid
.is_meta()) {
2975 cerr
<< "cannot get bit count for collection " << cid
<< ": "
2976 << cpp_strerror(bits
) << std::endl
;
2980 t
.create_collection(cid
, bits
);
2981 dst
->queue_transaction(dch
, std::move(t
));
2986 uint64_t bytes
= 0, keys
= 0;
2988 vector
<ghobject_t
> ls
;
2989 r
= src
->collection_list(ch
, pos
, ghobject_t::get_max(), 1000, &ls
, &pos
);
2991 cerr
<< "collection_list on " << cid
<< " from " << pos
<< " got: "
2992 << cpp_strerror(r
) << std::endl
;
2999 for (auto& oid
: ls
) {
3000 //cout << " " << cid << " " << oid << std::endl;
3002 cout
<< " " << std::setw(16) << n
<< " objects, "
3003 << std::setw(16) << bytes
<< " bytes, "
3004 << std::setw(16) << keys
<< " keys"
3005 << std::setw(1) << "\r" << std::flush
;
3009 ObjectStore::Transaction t
;
3012 map
<string
,bufferptr
,less
<>> attrs
;
3013 src
->getattrs(ch
, oid
, attrs
);
3014 if (!attrs
.empty()) {
3015 t
.setattrs(cid
, oid
, attrs
);
3019 src
->read(ch
, oid
, 0, 0, bl
);
3021 t
.write(cid
, oid
, 0, bl
.length(), bl
);
3022 bytes
+= bl
.length();
3026 map
<string
,bufferlist
> omap
;
3027 src
->omap_get(ch
, oid
, &header
, &omap
);
3028 if (header
.length()) {
3029 t
.omap_setheader(cid
, oid
, header
);
3032 if (!omap
.empty()) {
3033 keys
+= omap
.size();
3034 t
.omap_setkeys(cid
, oid
, omap
);
3037 dst
->queue_transaction(dch
, std::move(t
));
3040 cout
<< " " << std::setw(16) << n
<< " objects, "
3041 << std::setw(16) << bytes
<< " bytes, "
3042 << std::setw(16) << keys
<< " keys"
3043 << std::setw(1) << std::endl
;
3047 cout
<< "keyring" << std::endl
;
3050 string s
= srcpath
+ "/keyring";
3052 r
= bl
.read_file(s
.c_str(), &err
);
3054 cerr
<< "failed to copy " << s
<< ": " << err
<< std::endl
;
3056 string d
= dstpath
+ "/keyring";
3057 bl
.write_file(d
.c_str(), 0600);
3062 cout
<< "duping osd metadata" << std::endl
;
3064 for (auto k
: {"magic", "whoami", "ceph_fsid", "fsid"}) {
3066 src
->read_meta(k
, &val
);
3067 dst
->write_meta(k
, val
);
3071 dst
->write_meta("ready", "ready");
3073 cout
<< "done." << std::endl
;
3082 void usage(po::options_description
&desc
)
3085 cerr
<< desc
<< std::endl
;
3087 cerr
<< "Positional syntax:" << std::endl
;
3089 cerr
<< "ceph-objectstore-tool ... <object> (get|set)-bytes [file]" << std::endl
;
3090 cerr
<< "ceph-objectstore-tool ... <object> set-(attr|omap) <key> [file]" << std::endl
;
3091 cerr
<< "ceph-objectstore-tool ... <object> (get|rm)-(attr|omap) <key>" << std::endl
;
3092 cerr
<< "ceph-objectstore-tool ... <object> get-omaphdr" << std::endl
;
3093 cerr
<< "ceph-objectstore-tool ... <object> set-omaphdr [file]" << std::endl
;
3094 cerr
<< "ceph-objectstore-tool ... <object> list-attrs" << std::endl
;
3095 cerr
<< "ceph-objectstore-tool ... <object> list-omap" << std::endl
;
3096 cerr
<< "ceph-objectstore-tool ... <object> remove|removeall" << std::endl
;
3097 cerr
<< "ceph-objectstore-tool ... <object> dump" << std::endl
;
3098 cerr
<< "ceph-objectstore-tool ... <object> set-size" << std::endl
;
3099 cerr
<< "ceph-objectstore-tool ... <object> clear-data-digest" << std::endl
;
3100 cerr
<< "ceph-objectstore-tool ... <object> remove-clone-metadata <cloneid>" << std::endl
;
3102 cerr
<< "<object> can be a JSON object description as displayed" << std::endl
;
3103 cerr
<< "by --op list." << std::endl
;
3104 cerr
<< "<object> can be an object name which will be looked up in all" << std::endl
;
3105 cerr
<< "the OSD's PGs." << std::endl
;
3106 cerr
<< "<object> can be the empty string ('') which with a provided pgid " << std::endl
;
3107 cerr
<< "specifies the pgmeta object" << std::endl
;
3109 cerr
<< "The optional [file] argument will read stdin or write stdout" << std::endl
;
3110 cerr
<< "if not specified or if '-' specified." << std::endl
;
3113 bool ends_with(const string
& check
, const string
& ending
)
3115 return check
.size() >= ending
.size() && check
.rfind(ending
) == (check
.size() - ending
.size());
3118 // Based on FileStore::dump_journal(), set-up enough to only dump
3119 int mydump_journal(Formatter
*f
, string journalpath
, bool m_journal_dio
)
3123 if (!journalpath
.length())
3126 FileJournal
*journal
= new FileJournal(g_ceph_context
, uuid_d(), NULL
, NULL
,
3127 journalpath
.c_str(), m_journal_dio
);
3128 r
= journal
->_fdump(*f
, false);
3133 int apply_layout_settings(ObjectStore
*os
, const OSDSuperblock
&superblock
,
3134 const string
&pool_name
, const spg_t
&pgid
, bool dry_run
,
3139 FileStore
*fs
= dynamic_cast<FileStore
*>(os
);
3141 cerr
<< "Nothing to do for non-filestore backend" << std::endl
;
3142 return 0; // making this return success makes testing easier
3147 r
= get_osdmap(os
, superblock
.current_epoch
, curmap
, bl
);
3149 cerr
<< "Can't find local OSDMap: " << cpp_strerror(r
) << std::endl
;
3153 int64_t poolid
= -1;
3154 if (pool_name
.length()) {
3155 poolid
= curmap
.lookup_pg_pool_name(pool_name
);
3157 cerr
<< "Couldn't find pool " << pool_name
<< ": " << cpp_strerror(poolid
)
3163 vector
<coll_t
> collections
, filtered_colls
;
3164 r
= os
->list_collections(collections
);
3166 cerr
<< "Error listing collections: " << cpp_strerror(r
) << std::endl
;
3170 for (auto const &coll
: collections
) {
3172 if (coll
.is_pg(&coll_pgid
) &&
3173 ((poolid
>= 0 && coll_pgid
.pool() == (uint64_t)poolid
) ||
3174 coll_pgid
== pgid
)) {
3175 filtered_colls
.push_back(coll
);
3179 size_t done
= 0, total
= filtered_colls
.size();
3180 for (auto const &coll
: filtered_colls
) {
3182 cerr
<< "Would apply layout settings to " << coll
<< std::endl
;
3184 cerr
<< "Finished " << done
<< "/" << total
<< " collections" << "\r";
3185 r
= fs
->apply_layout_settings(coll
, target_level
);
3187 cerr
<< "Error applying layout settings to " << coll
<< std::endl
;
3194 cerr
<< "Finished " << total
<< "/" << total
<< " collections" << "\r" << std::endl
;
3198 int main(int argc
, char **argv
)
3200 string dpath
, jpath
, pgidstr
, op
, file
, mountpoint
, mon_store_path
, object
;
3201 string target_data_path
, fsid
;
3202 string objcmd
, arg1
, arg2
, type
, format
, argnspace
, pool
, rmtypestr
;
3203 boost::optional
<std::string
> nspace
;
3206 unsigned slow_threshold
= 16;
3208 bool human_readable
;
3209 Formatter
*formatter
;
3212 po::options_description
desc("Allowed options");
3214 ("help", "produce help message")
3215 ("type", po::value
<string
>(&type
),
3216 "Arg is one of [bluestore (default), filestore, memstore]")
3217 ("data-path", po::value
<string
>(&dpath
),
3218 "path to object store, mandatory")
3219 ("journal-path", po::value
<string
>(&jpath
),
3220 "path to journal, use if tool can't find it")
3221 ("pgid", po::value
<string
>(&pgidstr
),
3222 "PG id, mandatory for info, log, remove, export, export-remove, mark-complete, trim-pg-log, and mandatory for apply-layout-settings if --pool is not specified")
3223 ("pool", po::value
<string
>(&pool
),
3224 "Pool name, mandatory for apply-layout-settings if --pgid is not specified")
3225 ("op", po::value
<string
>(&op
),
3226 "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, "
3227 "get-osdmap, set-osdmap, get-inc-osdmap, set-inc-osdmap, mark-complete, reset-last-complete, apply-layout-settings, update-mon-db, dump-export, trim-pg-log, statfs]")
3228 ("epoch", po::value
<unsigned>(&epoch
),
3229 "epoch# for get-osdmap and get-inc-osdmap, the current epoch in use if not specified")
3230 ("file", po::value
<string
>(&file
),
3231 "path of file to export, export-remove, import, get-osdmap, set-osdmap, get-inc-osdmap or set-inc-osdmap")
3232 ("mon-store-path", po::value
<string
>(&mon_store_path
),
3233 "path of monstore to update-mon-db")
3234 ("fsid", po::value
<string
>(&fsid
),
3235 "fsid for new store created by mkfs")
3236 ("target-data-path", po::value
<string
>(&target_data_path
),
3237 "path of target object store (for --op dup)")
3238 ("mountpoint", po::value
<string
>(&mountpoint
),
3240 ("format", po::value
<string
>(&format
)->default_value("json-pretty"),
3241 "Output format which may be json, json-pretty, xml, xml-pretty")
3242 ("debug", "Enable diagnostic output to stderr")
3243 ("no-mon-config", "Do not contact mons for config")
3244 ("no-superblock", "Do not read superblock")
3245 ("force", "Ignore some types of errors and proceed with operation - USE WITH CAUTION: CORRUPTION POSSIBLE NOW OR IN THE FUTURE")
3246 ("skip-journal-replay", "Disable journal replay")
3247 ("skip-mount-omap", "Disable mounting of omap")
3248 ("head", "Find head/snapdir when searching for objects by name")
3249 ("dry-run", "Don't modify the objectstore")
3250 ("tty", "Treat stdout as a tty (no binary data)")
3251 ("namespace", po::value
<string
>(&argnspace
), "Specify namespace when searching for objects")
3252 ("rmtype", po::value
<string
>(&rmtypestr
), "Specify corrupting object removal 'snapmap' or 'nosnapmap' - TESTING USE ONLY")
3253 ("slow-omap-threshold", po::value
<unsigned>(&slow_threshold
),
3254 "Threshold (in seconds) to consider omap listing slow (for op=list-slow-omap)")
3257 po::options_description
positional("Positional options");
3258 positional
.add_options()
3259 ("object", po::value
<string
>(&object
), "'' for pgmeta_oid, object name or ghobject in json")
3260 ("objcmd", po::value
<string
>(&objcmd
), "command [(get|set)-bytes, (get|set|rm)-(attr|omap), (get|set)-omaphdr, list-attrs, list-omap, remove]")
3261 ("arg1", po::value
<string
>(&arg1
), "arg1 based on cmd")
3262 ("arg2", po::value
<string
>(&arg2
), "arg2 based on cmd")
3265 po::options_description all
;
3266 all
.add(desc
).add(positional
);
3268 po::positional_options_description pd
;
3269 pd
.add("object", 1).add("objcmd", 1).add("arg1", 1).add("arg2", 1);
3271 vector
<string
> ceph_option_strings
;
3273 po::variables_map vm
;
3275 po::parsed_options parsed
=
3276 po::command_line_parser(argc
, argv
).options(all
).allow_unregistered().positional(pd
).run();
3277 po::store( parsed
, vm
);
3279 ceph_option_strings
= po::collect_unrecognized(parsed
.options
,
3280 po::include_positional
);
3281 } catch(po::error
&e
) {
3282 std::cerr
<< e
.what() << std::endl
;
3286 if (vm
.count("help")) {
3291 // Compatibility with previous option name
3292 if (op
== "dump-import")
3295 debug
= (vm
.count("debug") > 0);
3297 force
= (vm
.count("force") > 0);
3299 no_superblock
= (vm
.count("no-superblock") > 0);
3301 if (vm
.count("namespace"))
3304 dry_run
= (vm
.count("dry-run") > 0);
3305 tty
= (vm
.count("tty") > 0);
3307 osflagbits_t flags
= 0;
3308 if (dry_run
|| vm
.count("skip-journal-replay"))
3309 flags
|= SKIP_JOURNAL_REPLAY
;
3310 if (vm
.count("skip-mount-omap"))
3311 flags
|= SKIP_MOUNT_OMAP
;
3312 if (op
== "update-mon-db")
3313 flags
|= SKIP_JOURNAL_REPLAY
;
3315 head
= (vm
.count("head") > 0);
3317 // infer osd id so we can authenticate
3319 snprintf(fn
, sizeof(fn
), "%s/whoami", dpath
.c_str());
3320 int fd
= ::open(fn
, O_RDONLY
);
3324 string
s(bl
.c_str(), bl
.length());
3325 int whoami
= atoi(s
.c_str());
3327 // identify ourselves as this osd so we can auth and fetch our configs
3328 tmp
.push_back("-n");
3329 tmp
.push_back(string("osd.") + stringify(whoami
));
3330 // populate osd_data so that the default keyring location works
3331 tmp
.push_back("--osd-data");
3332 tmp
.push_back(dpath
);
3333 tmp
.insert(tmp
.end(), ceph_option_strings
.begin(),
3334 ceph_option_strings
.end());
3335 tmp
.swap(ceph_option_strings
);
3338 vector
<const char *> ceph_options
;
3339 ceph_options
.reserve(ceph_options
.size() + ceph_option_strings
.size());
3340 for (vector
<string
>::iterator i
= ceph_option_strings
.begin();
3341 i
!= ceph_option_strings
.end();
3343 ceph_options
.push_back(i
->c_str());
3346 snprintf(fn
, sizeof(fn
), "%s/type", dpath
.c_str());
3347 fd
= ::open(fn
, O_RDONLY
);
3352 string dp_type
= string(bl
.c_str(), bl
.length() - 1); // drop \n
3353 if (vm
.count("type") && dp_type
!= "" && type
!= dp_type
)
3354 cerr
<< "WARNING: Ignoring type \"" << type
<< "\" - found data-path type \""
3355 << dp_type
<< "\"" << std::endl
;
3357 //cout << "object store type is " << type << std::endl;
3362 if (!vm
.count("type") && type
== "") {
3365 if (!vm
.count("data-path") &&
3366 op
!= "dump-export" &&
3367 !(op
== "dump-journal" && type
== "filestore")) {
3368 cerr
<< "Must provide --data-path" << std::endl
;
3372 if (type
== "filestore" && !vm
.count("journal-path")) {
3373 jpath
= dpath
+ "/journal";
3375 if (!vm
.count("op") && !vm
.count("object")) {
3376 cerr
<< "Must provide --op or object command..." << std::endl
;
3380 if (op
!= "list" && op
!= "apply-layout-settings" &&
3381 vm
.count("op") && vm
.count("object")) {
3382 cerr
<< "Can't specify both --op and object command syntax" << std::endl
;
3386 if (op
== "apply-layout-settings" && !(vm
.count("pool") ^ vm
.count("pgid"))) {
3387 cerr
<< "apply-layout-settings requires either --pool or --pgid"
3392 if (op
!= "list" && op
!= "apply-layout-settings" && vm
.count("object") && !vm
.count("objcmd")) {
3393 cerr
<< "Invalid syntax, missing command" << std::endl
;
3397 if (op
== "fuse" && mountpoint
.length() == 0) {
3398 cerr
<< "Missing fuse mountpoint" << std::endl
;
3402 outistty
= isatty(STDOUT_FILENO
) || tty
;
3405 if ((op
== "export" || op
== "export-remove" || op
== "get-osdmap" || op
== "get-inc-osdmap") && !dry_run
) {
3406 if (!vm
.count("file") || file
== "-") {
3408 cerr
<< "stdout is a tty and no --file filename specified" << std::endl
;
3411 file_fd
= STDOUT_FILENO
;
3413 file_fd
= open(file
.c_str(), O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
3415 } else if (op
== "import" || op
== "dump-export" || op
== "set-osdmap" || op
== "set-inc-osdmap") {
3416 if (!vm
.count("file") || file
== "-") {
3417 if (isatty(STDIN_FILENO
)) {
3418 cerr
<< "stdin is a tty and no --file filename specified" << std::endl
;
3421 file_fd
= STDIN_FILENO
;
3423 file_fd
= open(file
.c_str(), O_RDONLY
);
3427 ObjectStoreTool tool
= ObjectStoreTool(file_fd
, dry_run
);
3429 if (vm
.count("file") && file_fd
== fd_none
&& !dry_run
) {
3430 cerr
<< "--file option only applies to import, dump-export, export, export-remove, "
3431 << "get-osdmap, set-osdmap, get-inc-osdmap or set-inc-osdmap" << std::endl
;
3435 if (file_fd
!= fd_none
&& file_fd
< 0) {
3436 string err
= string("file: ") + file
;
3437 perror(err
.c_str());
3441 if (vm
.count("no-mon-config") > 0) {
3442 init_flags
|= CINIT_FLAG_NO_MON_CONFIG
;
3445 auto cct
= global_init(
3447 CEPH_ENTITY_TYPE_OSD
,
3448 CODE_ENVIRONMENT_UTILITY_NODOUT
,
3450 common_init_finish(g_ceph_context
);
3452 g_conf().set_val_or_die("log_to_stderr", "true");
3453 g_conf().set_val_or_die("err_to_stderr", "true");
3455 g_conf().apply_changes(nullptr);
3457 // Special list handling. Treating pretty_format as human readable,
3458 // with one object per line and not an enclosing array.
3459 human_readable
= ends_with(format
, "-pretty");
3460 if ((op
== "list" || op
== "meta-list") && human_readable
) {
3461 // Remove -pretty from end of format which we know is there
3462 format
= format
.substr(0, format
.size() - strlen("-pretty"));
3465 formatter
= Formatter::create(format
);
3466 if (formatter
== NULL
) {
3467 cerr
<< "unrecognized format: " << format
<< std::endl
;
3471 // Special handling for filestore journal, so we can dump it without mounting
3472 if (op
== "dump-journal" && type
== "filestore") {
3473 int ret
= mydump_journal(formatter
, jpath
, g_conf()->journal_dio
);
3475 cerr
<< "journal-path: " << jpath
<< ": "
3476 << cpp_strerror(ret
) << std::endl
;
3479 formatter
->flush(cout
);
3483 if (op
== "dump-export") {
3484 int ret
= tool
.dump_export(formatter
);
3486 cerr
<< "dump-export: "
3487 << cpp_strerror(ret
) << std::endl
;
3493 //Verify that data-path really exists
3495 if (::stat(dpath
.c_str(), &st
) == -1) {
3496 string err
= string("data-path: ") + dpath
;
3497 perror(err
.c_str());
3501 if (pgidstr
.length() && pgidstr
!= "meta" && !pgid
.parse(pgidstr
.c_str())) {
3502 cerr
<< "Invalid pgid '" << pgidstr
<< "' specified" << std::endl
;
3506 //Verify that the journal-path really exists
3507 if (type
== "filestore") {
3508 if (::stat(jpath
.c_str(), &st
) == -1) {
3509 string err
= string("journal-path: ") + jpath
;
3510 perror(err
.c_str());
3513 if (S_ISDIR(st
.st_mode
)) {
3514 cerr
<< "journal-path: " << jpath
<< ": "
3515 << cpp_strerror(EISDIR
) << std::endl
;
3520 std::unique_ptr
<ObjectStore
> fs
= ObjectStore::create(g_ceph_context
, type
, dpath
, jpath
, flags
);
3522 cerr
<< "Unable to create store of type " << type
<< std::endl
;
3526 if (op
== "fsck" || op
== "fsck-deep") {
3527 int r
= fs
->fsck(op
== "fsck-deep");
3529 cerr
<< "fsck failed: " << cpp_strerror(r
) << std::endl
;
3533 cerr
<< "fsck status: " << r
<< " remaining error(s) and warning(s)" << std::endl
;
3536 cout
<< "fsck success" << std::endl
;
3539 if (op
== "repair" || op
== "repair-deep") {
3540 int r
= fs
->repair(op
== "repair-deep");
3542 cerr
<< "repair failed: " << cpp_strerror(r
) << std::endl
;
3546 cerr
<< "repair status: " << r
<< " remaining error(s) and warning(s)" << std::endl
;
3549 cout
<< "repair success" << std::endl
;
3553 if (fsid
.length()) {
3555 bool r
= f
.parse(fsid
.c_str());
3557 cerr
<< "failed to parse uuid '" << fsid
<< "'" << std::endl
;
3564 cerr
<< "mkfs failed: " << cpp_strerror(r
) << std::endl
;
3572 snprintf(fn
, sizeof(fn
), "%s/type", target_data_path
.c_str());
3573 int fd
= ::open(fn
, O_RDONLY
);
3575 cerr
<< "Unable to open " << target_data_path
<< "/type" << std::endl
;
3581 target_type
= string(bl
.c_str(), bl
.length() - 1); // drop \n
3584 unique_ptr
<ObjectStore
> targetfs
= ObjectStore::create(
3585 g_ceph_context
, target_type
,
3586 target_data_path
, "", 0);
3588 cerr
<< "Unable to open store of type " << target_type
<< std::endl
;
3591 int r
= dup(dpath
, fs
.get(), target_data_path
, targetfs
.get());
3593 cerr
<< "dup failed: " << cpp_strerror(r
) << std::endl
;
3599 int ret
= fs
->mount();
3601 if (ret
== -EBUSY
) {
3602 cerr
<< "OSD has the store locked" << std::endl
;
3604 cerr
<< "Mount failed with '" << cpp_strerror(ret
) << "'" << std::endl
;
3611 FuseStore
fuse(fs
.get(), mountpoint
);
3612 cout
<< "mounting fuse at " << mountpoint
<< " ..." << std::endl
;
3613 int r
= fuse
.main();
3616 cerr
<< "failed to mount fuse: " << cpp_strerror(r
) << std::endl
;
3620 cerr
<< "fuse support not enabled" << std::endl
;
3626 vector
<coll_t
>::iterator it
;
3627 CompatSet supported
;
3629 #ifdef INTERNAL_TEST
3630 supported
= get_test_compat_set();
3632 supported
= OSD::get_osd_compat_set();
3636 auto ch
= fs
->open_collection(coll_t::meta());
3637 std::unique_ptr
<OSDSuperblock
> superblock
;
3638 if (!no_superblock
) {
3639 superblock
.reset(new OSDSuperblock
);
3640 bufferlist::const_iterator p
;
3641 ret
= fs
->read(ch
, OSD_SUPERBLOCK_GOBJECT
, 0, 0, bl
);
3643 cerr
<< "Failure to read OSD superblock: " << cpp_strerror(ret
) << std::endl
;
3648 decode(*superblock
, p
);
3651 cerr
<< "Cluster fsid=" << superblock
->cluster_fsid
<< std::endl
;
3655 cerr
<< "Supported features: " << supported
<< std::endl
;
3656 cerr
<< "On-disk features: " << superblock
->compat_features
<< std::endl
;
3658 if (supported
.compare(superblock
->compat_features
) == -1) {
3659 CompatSet unsupported
= supported
.unsupported(superblock
->compat_features
);
3660 cerr
<< "On-disk OSD incompatible features set "
3661 << unsupported
<< std::endl
;
3667 if (op
== "apply-layout-settings") {
3668 int target_level
= 0;
3669 // Single positional argument with apply-layout-settings
3670 // for target_level.
3671 if (vm
.count("object") && isdigit(object
[0])) {
3672 target_level
= atoi(object
.c_str());
3673 // This requires --arg1 to be specified since
3674 // this is the third positional argument and normally
3675 // used with object operations.
3676 } else if (vm
.count("arg1") && isdigit(arg1
[0])) {
3677 target_level
= atoi(arg1
.c_str());
3679 ceph_assert(superblock
!= nullptr);
3680 ret
= apply_layout_settings(fs
.get(), *superblock
, pool
, pgid
, dry_run
, target_level
);
3684 if (op
!= "list" && vm
.count("object")) {
3685 // Special case: Create pgmeta_oid if empty string specified
3686 // This can't conflict with any actual object names.
3688 ghobj
= pgid
.make_pgmeta_oid();
3690 json_spirit::Value v
;
3692 if (!json_spirit::read(object
, v
) ||
3693 (v
.type() != json_spirit::array_type
&& v
.type() != json_spirit::obj_type
)) {
3694 // Special: Need head/snapdir so set even if user didn't specify
3695 if (vm
.count("objcmd") && (objcmd
== "remove-clone-metadata"))
3697 lookup_ghobject
lookup(object
, nspace
, head
);
3698 if (pgidstr
== "meta")
3699 ret
= action_on_all_objects_in_exact_pg(fs
.get(), coll_t::meta(), lookup
, debug
);
3700 else if (pgidstr
.length())
3701 ret
= action_on_all_objects_in_exact_pg(fs
.get(), coll_t(pgid
), lookup
, debug
);
3703 ret
= action_on_all_objects(fs
.get(), lookup
, debug
);
3705 throw std::runtime_error("Internal error");
3707 if (lookup
.size() != 1) {
3709 if (lookup
.size() == 0)
3710 ss
<< "No object id '" << object
<< "' found or invalid JSON specified";
3712 ss
<< "Found " << lookup
.size() << " objects with id '" << object
3713 << "', please use a JSON spec from --op list instead";
3714 throw std::runtime_error(ss
.str());
3716 pair
<coll_t
, ghobject_t
> found
= lookup
.pop();
3717 pgidstr
= found
.first
.to_str();
3718 pgid
.parse(pgidstr
.c_str());
3719 ghobj
= found
.second
;
3723 if (pgidstr
.length() == 0 && v
.type() != json_spirit::array_type
) {
3724 ss
<< "Without --pgid the object '" << object
3725 << "' must be a JSON array";
3726 throw std::runtime_error(ss
.str());
3728 if (v
.type() == json_spirit::array_type
) {
3729 json_spirit::Array array
= v
.get_array();
3730 if (array
.size() != 2) {
3731 ss
<< "Object '" << object
3732 << "' must be a JSON array with 2 elements";
3733 throw std::runtime_error(ss
.str());
3735 vector
<json_spirit::Value
>::iterator i
= array
.begin();
3736 ceph_assert(i
!= array
.end());
3737 if (i
->type() != json_spirit::str_type
) {
3738 ss
<< "Object '" << object
3739 << "' must be a JSON array with the first element a string";
3740 throw std::runtime_error(ss
.str());
3742 string object_pgidstr
= i
->get_str();
3743 if (object_pgidstr
!= "meta") {
3745 object_pgid
.parse(object_pgidstr
.c_str());
3746 if (pgidstr
.length() > 0) {
3747 if (object_pgid
!= pgid
) {
3748 ss
<< "object '" << object
3749 << "' has a pgid different from the --pgid="
3750 << pgidstr
<< " option";
3751 throw std::runtime_error(ss
.str());
3754 pgidstr
= object_pgidstr
;
3758 pgidstr
= object_pgidstr
;
3765 } catch (std::runtime_error
& e
) {
3766 ss
<< "Decode object JSON error: " << e
.what();
3767 throw std::runtime_error(ss
.str());
3769 if (pgidstr
!= "meta" && (uint64_t)pgid
.pgid
.m_pool
!= (uint64_t)ghobj
.hobj
.pool
) {
3770 cerr
<< "Object pool and pgid pool don't match" << std::endl
;
3774 if (pgidstr
!= "meta") {
3775 auto ch
= fs
->open_collection(coll_t(pgid
));
3776 if (!ghobj
.match(fs
->collection_bits(ch
), pgid
.ps())) {
3778 ss
<< "object " << ghobj
<< " not contained by pg " << pgid
;
3779 throw std::runtime_error(ss
.str());
3783 } catch (std::runtime_error
& e
) {
3784 cerr
<< e
.what() << std::endl
;
3791 // The ops which require --pgid option are checked here and
3792 // mentioned in the usage for --pgid.
3793 if ((op
== "info" || op
== "log" || op
== "remove" || op
== "export"
3794 || op
== "export-remove" || op
== "mark-complete"
3795 || op
== "reset-last-complete"
3796 || op
== "trim-pg-log") &&
3797 pgidstr
.length() == 0) {
3798 cerr
<< "Must provide pgid" << std::endl
;
3804 if (op
== "import") {
3805 ceph_assert(superblock
!= nullptr);
3807 ret
= tool
.do_import(fs
.get(), *superblock
, force
, pgidstr
);
3809 catch (const buffer::error
&e
) {
3810 cerr
<< "do_import threw exception error " << e
.what() << std::endl
;
3813 if (ret
== -EFAULT
) {
3814 cerr
<< "Corrupt input for import" << std::endl
;
3817 cout
<< "Import successful" << std::endl
;
3819 } else if (op
== "dump-journal-mount") {
3820 // Undocumented feature to dump journal with mounted fs
3821 // This doesn't support the format option, but it uses the
3822 // ObjectStore::dump_journal() and mounts to get replay to run.
3823 ret
= fs
->dump_journal(cout
);
3825 if (ret
== -EOPNOTSUPP
) {
3826 cerr
<< "Object store type \"" << type
<< "\" doesn't support journal dump" << std::endl
;
3828 cerr
<< "Journal dump failed with error " << cpp_strerror(ret
) << std::endl
;
3832 } else if (op
== "get-osdmap") {
3836 ceph_assert(superblock
!= nullptr);
3837 epoch
= superblock
->current_epoch
;
3839 ret
= get_osdmap(fs
.get(), epoch
, osdmap
, bl
);
3841 cerr
<< "Failed to get osdmap#" << epoch
<< ": "
3842 << cpp_strerror(ret
) << std::endl
;
3845 ret
= bl
.write_fd(file_fd
);
3847 cerr
<< "Failed to write to " << file
<< ": " << cpp_strerror(ret
) << std::endl
;
3849 cout
<< "osdmap#" << epoch
<< " exported." << std::endl
;
3852 } else if (op
== "set-osdmap") {
3854 ret
= get_fd_data(file_fd
, bl
);
3856 cerr
<< "Failed to read osdmap " << cpp_strerror(ret
) << std::endl
;
3858 ret
= set_osdmap(fs
.get(), epoch
, bl
, force
);
3861 } else if (op
== "get-inc-osdmap") {
3864 ceph_assert(superblock
!= nullptr);
3865 epoch
= superblock
->current_epoch
;
3867 ret
= get_inc_osdmap(fs
.get(), epoch
, bl
);
3869 cerr
<< "Failed to get incremental osdmap# " << epoch
<< ": "
3870 << cpp_strerror(ret
) << std::endl
;
3873 ret
= bl
.write_fd(file_fd
);
3875 cerr
<< "Failed to write to " << file
<< ": " << cpp_strerror(ret
) << std::endl
;
3877 cout
<< "inc-osdmap#" << epoch
<< " exported." << std::endl
;
3880 } else if (op
== "set-inc-osdmap") {
3882 ret
= get_fd_data(file_fd
, bl
);
3884 cerr
<< "Failed to read incremental osdmap " << cpp_strerror(ret
) << std::endl
;
3887 ret
= set_inc_osdmap(fs
.get(), epoch
, bl
, force
);
3890 } else if (op
== "update-mon-db") {
3891 if (!vm
.count("mon-store-path")) {
3892 cerr
<< "Please specify the path to monitor db to update" << std::endl
;
3895 ceph_assert(superblock
!= nullptr);
3896 ret
= update_mon_db(*fs
, *superblock
, dpath
+ "/keyring", mon_store_path
);
3901 if (op
== "remove") {
3902 if (!force
&& !dry_run
) {
3903 cerr
<< "Please use export-remove or you must use --force option" << std::endl
;
3907 ret
= initiate_new_remove_pg(fs
.get(), pgid
);
3909 cerr
<< "PG '" << pgid
<< "' not found" << std::endl
;
3912 cout
<< "Remove successful" << std::endl
;
3916 if (op
== "fix-lost") {
3917 boost::scoped_ptr
<action_on_object_t
> action
;
3918 action
.reset(new do_fix_lost());
3919 if (pgidstr
.length())
3920 ret
= action_on_all_objects_in_exact_pg(fs
.get(), coll_t(pgid
), *action
, debug
);
3922 ret
= action_on_all_objects(fs
.get(), *action
, debug
);
3927 ret
= do_list(fs
.get(), pgidstr
, object
, nspace
, formatter
, debug
,
3928 human_readable
, head
);
3930 cerr
<< "do_list failed: " << cpp_strerror(ret
) << std::endl
;
3934 if (op
== "list-slow-omap") {
3935 ret
= do_list_slow(fs
.get(), pgidstr
, object
, slow_threshold
, formatter
, debug
,
3938 cerr
<< "do_list failed: " << cpp_strerror(ret
) << std::endl
;
3943 if (op
== "dump-super") {
3944 ceph_assert(superblock
!= nullptr);
3945 formatter
->open_object_section("superblock");
3946 superblock
->dump(formatter
);
3947 formatter
->close_section();
3948 formatter
->flush(cout
);
3953 if (op
== "statfs") {
3954 store_statfs_t statsbuf
;
3955 ret
= fs
->statfs(&statsbuf
);
3957 cerr
<< "error from statfs: " << cpp_strerror(ret
) << std::endl
;
3960 formatter
->open_object_section("statfs");
3961 statsbuf
.dump(formatter
);
3962 formatter
->close_section();
3963 formatter
->flush(cout
);
3968 if (op
== "meta-list") {
3969 ret
= do_meta(fs
.get(), object
, formatter
, debug
, human_readable
);
3971 cerr
<< "do_meta failed: " << cpp_strerror(ret
) << std::endl
;
3976 ret
= fs
->list_collections(ls
);
3978 cerr
<< "failed to list pgs: " << cpp_strerror(ret
) << std::endl
;
3982 if (debug
&& op
== "list-pgs")
3983 cout
<< "Performing list-pgs operation" << std::endl
;
3986 for (it
= ls
.begin(); it
!= ls
.end(); ++it
) {
3989 if (pgidstr
== "meta") {
3990 if (it
->to_str() == "meta")
3996 if (!it
->is_pg(&tmppgid
)) {
4000 if (it
->is_temp(&tmppgid
)) {
4004 if (op
!= "list-pgs" && tmppgid
!= pgid
) {
4008 if (op
!= "list-pgs") {
4013 cout
<< tmppgid
<< std::endl
;
4016 if (op
== "list-pgs") {
4021 // If not an object command nor any of the ops handled below, then output this usage
4022 // before complaining about a bad pgid
4023 if (!vm
.count("objcmd") && op
!= "export" && op
!= "export-remove" && op
!= "info" && op
!= "log" && op
!= "mark-complete" && op
!= "trim-pg-log") {
4024 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, "
4025 "get-osdmap, set-osdmap, get-inc-osdmap, set-inc-osdmap, mark-complete, reset-last-complete, dump-export, trim-pg-log, statfs)"
4032 // The following code for export, info, log require omap or !skip-mount-omap
4033 if (it
!= ls
.end()) {
4037 if (vm
.count("objcmd")) {
4039 if (objcmd
== "remove" || objcmd
== "removeall") {
4040 bool all
= (objcmd
== "removeall");
4041 enum rmtype type
= BOTH
;
4042 if (rmtypestr
== "nosnapmap")
4044 else if (rmtypestr
== "snapmap")
4046 ret
= do_remove_object(fs
.get(), coll
, ghobj
, all
, force
, type
);
4048 } else if (objcmd
== "list-attrs") {
4049 ret
= do_list_attrs(fs
.get(), coll
, ghobj
);
4051 } else if (objcmd
== "list-omap") {
4052 ret
= do_list_omap(fs
.get(), coll
, ghobj
);
4054 } else if (objcmd
== "get-bytes" || objcmd
== "set-bytes") {
4055 if (objcmd
== "get-bytes") {
4057 if (vm
.count("arg1") == 0 || arg1
== "-") {
4060 fd
= open(arg1
.c_str(), O_WRONLY
|O_TRUNC
|O_CREAT
|O_EXCL
|O_LARGEFILE
, 0666);
4062 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4067 ret
= do_get_bytes(fs
.get(), coll
, ghobj
, fd
);
4068 if (fd
!= STDOUT_FILENO
)
4072 if (vm
.count("arg1") == 0 || arg1
== "-") {
4073 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4074 if (isatty(STDIN_FILENO
)) {
4075 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4081 fd
= open(arg1
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4083 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4088 ret
= do_set_bytes(fs
.get(), coll
, ghobj
, fd
);
4089 if (fd
!= STDIN_FILENO
)
4093 } else if (objcmd
== "get-attr") {
4094 if (vm
.count("arg1") == 0) {
4099 ret
= do_get_attr(fs
.get(), coll
, ghobj
, arg1
);
4101 } else if (objcmd
== "set-attr") {
4102 if (vm
.count("arg1") == 0) {
4108 if (vm
.count("arg2") == 0 || arg2
== "-") {
4109 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4110 if (isatty(STDIN_FILENO
)) {
4111 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4117 fd
= open(arg2
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4119 cerr
<< "open " << arg2
<< " " << cpp_strerror(errno
) << std::endl
;
4124 ret
= do_set_attr(fs
.get(), coll
, ghobj
, arg1
, fd
);
4125 if (fd
!= STDIN_FILENO
)
4128 } else if (objcmd
== "rm-attr") {
4129 if (vm
.count("arg1") == 0) {
4134 ret
= do_rm_attr(fs
.get(), coll
, ghobj
, arg1
);
4136 } else if (objcmd
== "get-omap") {
4137 if (vm
.count("arg1") == 0) {
4142 ret
= do_get_omap(fs
.get(), coll
, ghobj
, arg1
);
4144 } else if (objcmd
== "set-omap") {
4145 if (vm
.count("arg1") == 0) {
4151 if (vm
.count("arg2") == 0 || arg2
== "-") {
4152 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4153 if (isatty(STDIN_FILENO
)) {
4154 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4160 fd
= open(arg2
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4162 cerr
<< "open " << arg2
<< " " << cpp_strerror(errno
) << std::endl
;
4167 ret
= do_set_omap(fs
.get(), coll
, ghobj
, arg1
, fd
);
4168 if (fd
!= STDIN_FILENO
)
4171 } else if (objcmd
== "rm-omap") {
4172 if (vm
.count("arg1") == 0) {
4177 ret
= do_rm_omap(fs
.get(), coll
, ghobj
, arg1
);
4179 } else if (objcmd
== "get-omaphdr") {
4180 if (vm
.count("arg1")) {
4185 ret
= do_get_omaphdr(fs
.get(), coll
, ghobj
);
4187 } else if (objcmd
== "set-omaphdr") {
4189 if (vm
.count("arg2")) {
4195 if (vm
.count("arg1") == 0 || arg1
== "-") {
4196 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
4197 if (isatty(STDIN_FILENO
)) {
4198 cerr
<< "stdin is a tty and no file specified" << std::endl
;
4204 fd
= open(arg1
.c_str(), O_RDONLY
|O_LARGEFILE
, 0666);
4206 cerr
<< "open " << arg1
<< " " << cpp_strerror(errno
) << std::endl
;
4211 ret
= do_set_omaphdr(fs
.get(), coll
, ghobj
, fd
);
4212 if (fd
!= STDIN_FILENO
)
4215 } else if (objcmd
== "dump") {
4216 // There should not be any other arguments
4217 if (vm
.count("arg1") || vm
.count("arg2")) {
4222 ret
= print_obj_info(fs
.get(), coll
, ghobj
, formatter
);
4224 } else if (objcmd
== "corrupt-info") { // Undocumented testing feature
4225 // There should not be any other arguments
4226 if (vm
.count("arg1") || vm
.count("arg2")) {
4231 ret
= corrupt_info(fs
.get(), coll
, ghobj
, formatter
);
4233 } else if (objcmd
== "set-size" || objcmd
== "corrupt-size") {
4234 // Undocumented testing feature
4235 bool corrupt
= (objcmd
== "corrupt-size");
4237 if (vm
.count("arg1") == 0 || vm
.count("arg2")) {
4242 if (arg1
.length() == 0 || !isdigit(arg1
.c_str()[0])) {
4243 cerr
<< "Invalid size '" << arg1
<< "' specified" << std::endl
;
4247 uint64_t size
= atoll(arg1
.c_str());
4248 ret
= set_size(fs
.get(), coll
, ghobj
, size
, formatter
, corrupt
);
4250 } else if (objcmd
== "clear-data-digest") {
4251 ret
= clear_data_digest(fs
.get(), coll
, ghobj
);
4253 } else if (objcmd
== "clear-snapset") {
4254 // UNDOCUMENTED: For testing zap SnapSet
4255 // IGNORE extra args since not in usage anyway
4256 if (!ghobj
.hobj
.has_snapset()) {
4257 cerr
<< "'" << objcmd
<< "' requires a head or snapdir object" << std::endl
;
4261 ret
= clear_snapset(fs
.get(), coll
, ghobj
, arg1
);
4263 } else if (objcmd
== "remove-clone-metadata") {
4265 if (vm
.count("arg1") == 0 || vm
.count("arg2")) {
4270 if (!ghobj
.hobj
.has_snapset()) {
4271 cerr
<< "'" << objcmd
<< "' requires a head or snapdir object" << std::endl
;
4275 if (arg1
.length() == 0 || !isdigit(arg1
.c_str()[0])) {
4276 cerr
<< "Invalid cloneid '" << arg1
<< "' specified" << std::endl
;
4280 snapid_t cloneid
= atoi(arg1
.c_str());
4281 ret
= remove_clone(fs
.get(), coll
, ghobj
, cloneid
, force
);
4284 cerr
<< "Unknown object command '" << objcmd
<< "'" << std::endl
;
4291 ret
= PG::peek_map_epoch(fs
.get(), pgid
, &map_epoch
);
4293 cerr
<< "peek_map_epoch reports error" << std::endl
;
4295 cerr
<< "map_epoch " << map_epoch
<< std::endl
;
4297 pg_info_t
info(pgid
);
4298 PastIntervals past_intervals
;
4300 ret
= PG::read_info(fs
.get(), pgid
, coll
, info
, past_intervals
, struct_ver
);
4302 cerr
<< "read_info error " << cpp_strerror(ret
) << std::endl
;
4305 if (struct_ver
< PG::get_compat_struct_v()) {
4306 cerr
<< "PG is too old to upgrade, use older Ceph version" << std::endl
;
4311 cerr
<< "struct_v " << (int)struct_ver
<< std::endl
;
4313 if (op
== "export" || op
== "export-remove") {
4314 ceph_assert(superblock
!= nullptr);
4315 ret
= tool
.do_export(fs
.get(), coll
, pgid
, info
, map_epoch
, struct_ver
, *superblock
, past_intervals
);
4317 cerr
<< "Export successful" << std::endl
;
4318 if (op
== "export-remove") {
4319 ret
= initiate_new_remove_pg(fs
.get(), pgid
);
4320 // Export succeeded, so pgid is there
4321 ceph_assert(ret
== 0);
4322 cerr
<< "Remove successful" << std::endl
;
4325 } else if (op
== "info") {
4326 formatter
->open_object_section("info");
4327 info
.dump(formatter
);
4328 formatter
->close_section();
4329 formatter
->flush(cout
);
4331 } else if (op
== "log") {
4332 PGLog::IndexedLog log
;
4333 pg_missing_t missing
;
4334 ret
= get_log(fs
.get(), struct_ver
, pgid
, info
, log
, missing
);
4338 dump_log(formatter
, cout
, log
, missing
);
4339 } else if (op
== "mark-complete") {
4340 ObjectStore::Transaction tran
;
4341 ObjectStore::Transaction
*t
= &tran
;
4343 if (struct_ver
< PG::get_compat_struct_v()) {
4344 cerr
<< "Can't mark-complete, version mismatch " << (int)struct_ver
4345 << " (pg) < compat " << (int)PG::get_compat_struct_v() << " (tool)"
4351 cout
<< "Marking complete " << std::endl
;
4353 ceph_assert(superblock
!= nullptr);
4354 info
.last_update
= eversion_t(superblock
->current_epoch
, info
.last_update
.version
+ 1);
4355 info
.last_backfill
= hobject_t::get_max();
4356 info
.last_epoch_started
= superblock
->current_epoch
;
4357 info
.history
.last_epoch_started
= superblock
->current_epoch
;
4358 info
.history
.last_epoch_clean
= superblock
->current_epoch
;
4359 past_intervals
.clear();
4362 ret
= write_info(*t
, map_epoch
, info
, past_intervals
);
4365 auto ch
= fs
->open_collection(coll_t(pgid
));
4366 fs
->queue_transaction(ch
, std::move(*t
));
4368 cout
<< "Marking complete succeeded" << std::endl
;
4369 } else if (op
== "trim-pg-log") {
4370 ret
= do_trim_pg_log(fs
.get(), coll
, info
, pgid
,
4371 map_epoch
, past_intervals
);
4373 cerr
<< "Error trimming pg log: " << cpp_strerror(ret
) << std::endl
;
4376 cout
<< "Finished trimming pg log" << std::endl
;
4378 } else if (op
== "reset-last-complete") {
4380 std::cerr
<< "WARNING: reset-last-complete is extremely dangerous and almost "
4381 << "certain to lead to permanent data loss unless you know exactly "
4382 << "what you are doing. Pass --force to proceed anyway."
4387 ObjectStore::Transaction tran
;
4388 ObjectStore::Transaction
*t
= &tran
;
4390 if (struct_ver
< PG::get_compat_struct_v()) {
4391 cerr
<< "Can't reset-last-complete, version mismatch " << (int)struct_ver
4392 << " (pg) < compat " << (int)PG::get_compat_struct_v() << " (tool)"
4398 cout
<< "Reseting last_complete " << std::endl
;
4400 info
.last_complete
= info
.last_update
;
4403 ret
= write_info(*t
, map_epoch
, info
, past_intervals
);
4406 fs
->queue_transaction(ch
, std::move(*t
));
4408 cout
<< "Reseting last_complete succeeded" << std::endl
;
4411 ceph_assert(!"Should have already checked for valid --op");
4414 cerr
<< "PG '" << pgid
<< "' not found" << std::endl
;
4421 Formatter
* f
= Formatter::create("json-pretty", "json-pretty", "json-pretty");
4422 cct
->get_perfcounters_collection()->dump_formatted(f
, false);
4423 ostr
<< "ceph-objectstore-tool ";
4426 cout
<< ostr
.str() << std::endl
;
4429 int r
= fs
->umount();
4431 cerr
<< "umount failed: " << cpp_strerror(r
) << std::endl
;
4432 // If no previous error, then use umount() error
4438 // Export output can go to stdout, so put this message on stderr
4440 cerr
<< "dry-run: Nothing changed" << std::endl
;
4442 cout
<< "dry-run: Nothing changed" << std::endl
;