]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/ceph_objectstore_tool.cc
update sources to v12.1.3
[ceph.git] / ceph / src / tools / ceph_objectstore_tool.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2013 Inktank
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#include <boost/program_options/variables_map.hpp>
16#include <boost/program_options/parsers.hpp>
17#include <boost/scoped_ptr.hpp>
18#include <boost/optional.hpp>
19
20#include <stdlib.h>
21
22#include "common/Formatter.h"
23#include "common/errno.h"
24#include "common/ceph_argparse.h"
25
26#include "global/global_init.h"
27
28#include "os/ObjectStore.h"
29#include "os/filestore/FileJournal.h"
30#include "os/filestore/FileStore.h"
31#ifdef HAVE_LIBFUSE
32#include "os/FuseStore.h"
33#endif
34
35#include "osd/PGLog.h"
36#include "osd/OSD.h"
37#include "osd/PG.h"
38
39#include "json_spirit/json_spirit_value.h"
40#include "json_spirit/json_spirit_reader.h"
41
42#include "rebuild_mondb.h"
43#include "ceph_objectstore_tool.h"
44#include "include/compat.h"
45#include "include/util.h"
46
47namespace po = boost::program_options;
48using namespace std;
49
50#ifdef INTERNAL_TEST
51CompatSet get_test_compat_set() {
52 CompatSet::FeatureSet ceph_osd_feature_compat;
53 CompatSet::FeatureSet ceph_osd_feature_ro_compat;
54 CompatSet::FeatureSet ceph_osd_feature_incompat;
55 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_BASE);
56 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_PGINFO);
57 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_OLOC);
58 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEC);
59 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_CATEGORIES);
60 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_HOBJECTPOOL);
61 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_BIGINFO);
62 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBINFO);
63 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBLOG);
64#ifdef INTERNAL_TEST2
65 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_SNAPMAPPER);
66 ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_SHARDS);
67#endif
68 return CompatSet(ceph_osd_feature_compat, ceph_osd_feature_ro_compat,
69 ceph_osd_feature_incompat);
70}
71#endif
72
73const ssize_t max_read = 1024 * 1024;
74const int fd_none = INT_MIN;
75bool outistty;
76bool dry_run = false;
77
78struct action_on_object_t {
79 virtual ~action_on_object_t() {}
80 virtual int call(ObjectStore *store, coll_t coll, ghobject_t &ghobj, object_info_t &oi) = 0;
81};
82
83int _action_on_all_objects_in_pg(ObjectStore *store, coll_t coll, action_on_object_t &action, bool debug)
84{
85 unsigned LIST_AT_A_TIME = 100;
86 ghobject_t next;
87 while (!next.is_max()) {
88 vector<ghobject_t> list;
89 int r = store->collection_list(
90 coll,
91 next,
92 ghobject_t::get_max(),
93 LIST_AT_A_TIME,
94 &list,
95 &next);
96 if (r < 0) {
97 cerr << "Error listing collection: " << coll << ", "
98 << cpp_strerror(r) << std::endl;
99 return r;
100 }
101 for (vector<ghobject_t>::iterator obj = list.begin();
102 obj != list.end();
103 ++obj) {
104 if (obj->is_pgmeta())
105 continue;
106 object_info_t oi;
107 if (coll != coll_t::meta()) {
108 bufferlist attr;
109 r = store->getattr(coll, *obj, OI_ATTR, attr);
110 if (r < 0) {
111 cerr << "Error getting attr on : " << make_pair(coll, *obj) << ", "
112 << cpp_strerror(r) << std::endl;
113 continue;
114 }
115 bufferlist::iterator bp = attr.begin();
116 try {
117 ::decode(oi, bp);
118 } catch (...) {
119 r = -EINVAL;
120 cerr << "Error getting attr on : " << make_pair(coll, *obj) << ", "
121 << cpp_strerror(r) << std::endl;
122 continue;
123 }
124 }
125 r = action.call(store, coll, *obj, oi);
126 if (r < 0)
127 return r;
128 }
129 }
130 return 0;
131}
132
133int action_on_all_objects_in_pg(ObjectStore *store, string pgidstr, action_on_object_t &action, bool debug)
134{
135 spg_t pgid;
136 // Scan collections in case this is an ec pool but no shard specified
137 unsigned scanned = 0;
138 int r = 0;
139 vector<coll_t> colls_to_check;
140 vector<coll_t> candidates;
141 r = store->list_collections(candidates);
142 if (r < 0) {
143 cerr << "Error listing collections: " << cpp_strerror(r) << std::endl;
144 return r;
145 }
146 pgid.parse(pgidstr.c_str());
147 for (vector<coll_t>::iterator i = candidates.begin();
148 i != candidates.end();
149 ++i) {
150 spg_t cand_pgid;
151 if (!i->is_pg(&cand_pgid))
152 continue;
153
154 // If an exact match or treat no shard as any shard
155 if (cand_pgid == pgid ||
156 (pgid.is_no_shard() && pgid.pgid == cand_pgid.pgid)) {
157 colls_to_check.push_back(*i);
158 }
159 }
160
161 if (debug)
162 cerr << colls_to_check.size() << " pgs to scan" << std::endl;
163 for (vector<coll_t>::iterator i = colls_to_check.begin();
164 i != colls_to_check.end();
165 ++i, ++scanned) {
166 if (debug)
167 cerr << "Scanning " << *i << ", " << scanned << "/"
168 << colls_to_check.size() << " completed" << std::endl;
169 r = _action_on_all_objects_in_pg(store, *i, action, debug);
170 if (r < 0)
171 break;
172 }
173 return r;
174}
175
176int action_on_all_objects_in_exact_pg(ObjectStore *store, coll_t coll, action_on_object_t &action, bool debug)
177{
178 int r = _action_on_all_objects_in_pg(store, coll, action, debug);
179 return r;
180}
181
182int _action_on_all_objects(ObjectStore *store, action_on_object_t &action, bool debug)
183{
184 unsigned scanned = 0;
185 int r = 0;
186 vector<coll_t> colls_to_check;
187 vector<coll_t> candidates;
188 r = store->list_collections(candidates);
189 if (r < 0) {
190 cerr << "Error listing collections: " << cpp_strerror(r) << std::endl;
191 return r;
192 }
193 for (vector<coll_t>::iterator i = candidates.begin();
194 i != candidates.end();
195 ++i) {
196 if (i->is_pg()) {
197 colls_to_check.push_back(*i);
198 }
199 }
200
201 if (debug)
202 cerr << colls_to_check.size() << " pgs to scan" << std::endl;
203 for (vector<coll_t>::iterator i = colls_to_check.begin();
204 i != colls_to_check.end();
205 ++i, ++scanned) {
206 if (debug)
207 cerr << "Scanning " << *i << ", " << scanned << "/"
208 << colls_to_check.size() << " completed" << std::endl;
209 r = _action_on_all_objects_in_pg(store, *i, action, debug);
210 if (r < 0)
211 return r;
212 }
213 return 0;
214}
215
216int action_on_all_objects(ObjectStore *store, action_on_object_t &action, bool debug)
217{
218 int r = _action_on_all_objects(store, action, debug);
219 return r;
220}
221
222struct pgid_object_list {
223 list<pair<coll_t, ghobject_t> > _objects;
224
225 void insert(coll_t coll, ghobject_t &ghobj) {
226 _objects.push_back(make_pair(coll, ghobj));
227 }
228
229 void dump(Formatter *f, bool human_readable) const {
230 if (!human_readable)
231 f->open_array_section("pgid_objects");
232 for (list<pair<coll_t, ghobject_t> >::const_iterator i = _objects.begin();
233 i != _objects.end();
234 ++i) {
235 f->open_array_section("pgid_object");
236 spg_t pgid;
237 bool is_pg = i->first.is_pg(&pgid);
238 if (is_pg)
239 f->dump_string("pgid", stringify(pgid));
240 if (!is_pg || !human_readable)
241 f->dump_string("coll", i->first.to_str());
242 f->open_object_section("ghobject");
243 i->second.dump(f);
244 f->close_section();
245 f->close_section();
246 if (human_readable) {
247 f->flush(cout);
248 cout << std::endl;
249 }
250 }
251 if (!human_readable) {
252 f->close_section();
253 f->flush(cout);
254 cout << std::endl;
255 }
256 }
257};
258
259struct lookup_ghobject : public action_on_object_t {
260 pgid_object_list _objects;
261 const string _name;
262 const boost::optional<std::string> _namespace;
263 bool _need_snapset;
264
265 lookup_ghobject(const string& name, const boost::optional<std::string>& nspace, bool need_snapset = false) : _name(name),
266 _namespace(nspace), _need_snapset(need_snapset) { }
267
268 int call(ObjectStore *store, coll_t coll, ghobject_t &ghobj, object_info_t &oi) override {
269 if (_need_snapset && !ghobj.hobj.has_snapset())
270 return 0;
271 if ((_name.length() == 0 || ghobj.hobj.oid.name == _name) &&
272 (!_namespace || ghobj.hobj.nspace == _namespace))
273 _objects.insert(coll, ghobj);
274 return 0;
275 }
276
277 int size() const {
278 return _objects._objects.size();
279 }
280
281 pair<coll_t, ghobject_t> pop() {
282 pair<coll_t, ghobject_t> front = _objects._objects.front();
283 _objects._objects.pop_front();
284 return front;
285 }
286
287 void dump(Formatter *f, bool human_readable) const {
288 _objects.dump(f, human_readable);
289 }
290};
291
292ghobject_t infos_oid = OSD::make_infos_oid();
293ghobject_t log_oid;
294ghobject_t biginfo_oid;
295
296int file_fd = fd_none;
297bool debug = false;
298super_header sh;
299uint64_t testalign;
300
301static int get_fd_data(int fd, bufferlist &bl)
302{
303 uint64_t total = 0;
304 do {
305 ssize_t bytes = bl.read_fd(fd, max_read);
306 if (bytes < 0) {
307 cerr << "read_fd error " << cpp_strerror(bytes) << std::endl;
308 return bytes;
309 }
310
311 if (bytes == 0)
312 break;
313
314 total += bytes;
315 } while(true);
316
317 assert(bl.length() == total);
318 return 0;
319}
320
321int get_log(ObjectStore *fs, __u8 struct_ver,
322 coll_t coll, spg_t pgid, const pg_info_t &info,
323 PGLog::IndexedLog &log, pg_missing_t &missing)
324{
325 try {
326 ostringstream oss;
327 assert(struct_ver > 0);
328 PGLog::read_log_and_missing(fs, coll,
329 struct_ver >= 8 ? coll : coll_t::meta(),
330 struct_ver >= 8 ? pgid.make_pgmeta_oid() : log_oid,
d2e6a577
FG
331 info, log, missing,
332 struct_ver < 9,
333 oss,
7c673cae
FG
334 g_ceph_context->_conf->osd_ignore_stale_divergent_priors);
335 if (debug && oss.str().size())
336 cerr << oss.str() << std::endl;
337 }
338 catch (const buffer::error &e) {
339 cerr << "read_log_and_missing threw exception error " << e.what() << std::endl;
340 return -EFAULT;
341 }
342 return 0;
343}
344
345void dump_log(Formatter *formatter, ostream &out, pg_log_t &log,
346 pg_missing_t &missing)
347{
348 formatter->open_object_section("op_log");
349 formatter->open_object_section("pg_log_t");
350 log.dump(formatter);
351 formatter->close_section();
352 formatter->flush(out);
353 formatter->open_object_section("pg_missing_t");
354 missing.dump(formatter);
355 formatter->close_section();
356 formatter->flush(out);
357 formatter->open_object_section("map");
358 formatter->close_section();
359 formatter->close_section();
360 formatter->flush(out);
361}
362
363//Based on part of OSD::load_pgs()
364int finish_remove_pgs(ObjectStore *store)
365{
366 vector<coll_t> ls;
367 int r = store->list_collections(ls);
368 if (r < 0) {
369 cerr << "finish_remove_pgs: failed to list pgs: " << cpp_strerror(r)
370 << std::endl;
371 return r;
372 }
373
374 for (vector<coll_t>::iterator it = ls.begin();
375 it != ls.end();
376 ++it) {
377 spg_t pgid;
378
379 if (it->is_temp(&pgid) ||
380 (it->is_pg(&pgid) && PG::_has_removal_flag(store, pgid))) {
381 cout << "finish_remove_pgs " << *it << " removing " << pgid << std::endl;
382 OSD::recursive_remove_collection(g_ceph_context, store, pgid, *it);
383 continue;
384 }
385
386 //cout << "finish_remove_pgs ignoring unrecognized " << *it << std::endl;
387 }
388 return 0;
389}
390
391#pragma GCC diagnostic ignored "-Wpragmas"
392#pragma GCC diagnostic push
393#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
394
395int mark_pg_for_removal(ObjectStore *fs, spg_t pgid, ObjectStore::Transaction *t)
396{
397 pg_info_t info(pgid);
398 coll_t coll(pgid);
399 ghobject_t pgmeta_oid(info.pgid.make_pgmeta_oid());
400
401 bufferlist bl;
402 epoch_t map_epoch = 0;
403 int r = PG::peek_map_epoch(fs, pgid, &map_epoch, &bl);
404 if (r < 0)
405 cerr << __func__ << " warning: peek_map_epoch reported error" << std::endl;
406 PastIntervals past_intervals;
407 __u8 struct_v;
408 r = PG::read_info(fs, pgid, coll, bl, info, past_intervals, struct_v);
409 if (r < 0) {
410 cerr << __func__ << " error on read_info " << cpp_strerror(r) << std::endl;
411 return r;
412 }
413 assert(struct_v >= 8);
414 // new omap key
415 cout << "setting '_remove' omap key" << std::endl;
416 map<string,bufferlist> values;
417 ::encode((char)1, values["_remove"]);
418 t->omap_setkeys(coll, pgmeta_oid, values);
419 return 0;
420}
421
422#pragma GCC diagnostic pop
423#pragma GCC diagnostic warning "-Wpragmas"
424
425int initiate_new_remove_pg(ObjectStore *store, spg_t r_pgid,
426 ObjectStore::Sequencer &osr)
427{
428 if (!dry_run)
429 finish_remove_pgs(store);
430 if (!store->collection_exists(coll_t(r_pgid)))
431 return -ENOENT;
432
433 cout << " marking collection for removal" << std::endl;
434 if (dry_run)
435 return 0;
436 ObjectStore::Transaction rmt;
437 int r = mark_pg_for_removal(store, r_pgid, &rmt);
438 if (r < 0) {
439 return r;
440 }
441 store->apply_transaction(&osr, std::move(rmt));
442 finish_remove_pgs(store);
443 return r;
444}
445
446int write_info(ObjectStore::Transaction &t, epoch_t epoch, pg_info_t &info,
447 PastIntervals &past_intervals)
448{
449 //Empty for this
450 coll_t coll(info.pgid);
451 ghobject_t pgmeta_oid(info.pgid.make_pgmeta_oid());
452 map<string,bufferlist> km;
453 pg_info_t last_written_info;
454 int ret = PG::_prepare_write_info(
455 g_ceph_context,
456 &km, epoch,
457 info,
458 last_written_info,
459 past_intervals,
460 true, true, false);
461 if (ret) cerr << "Failed to write info" << std::endl;
462 t.omap_setkeys(coll, pgmeta_oid, km);
463 return ret;
464}
465
466typedef map<eversion_t, hobject_t> divergent_priors_t;
467
468int write_pg(ObjectStore::Transaction &t, epoch_t epoch, pg_info_t &info,
469 pg_log_t &log, PastIntervals &past_intervals,
470 divergent_priors_t &divergent,
471 pg_missing_t &missing)
472{
473 int ret = write_info(t, epoch, info, past_intervals);
474 if (ret)
475 return ret;
476 coll_t coll(info.pgid);
477 map<string,bufferlist> km;
478
479 if (!divergent.empty()) {
480 assert(missing.get_items().empty());
481 PGLog::write_log_and_missing_wo_missing(
c07f9fc5 482 t, &km, log, coll, info.pgid.make_pgmeta_oid(), divergent, true, true);
7c673cae
FG
483 } else {
484 pg_missing_tracker_t tmissing(missing);
c07f9fc5 485 bool rebuilt_missing_set_with_deletes = missing.may_include_deletes;
7c673cae 486 PGLog::write_log_and_missing(
c07f9fc5
FG
487 t, &km, log, coll, info.pgid.make_pgmeta_oid(), tmissing, true, true,
488 &rebuilt_missing_set_with_deletes);
7c673cae
FG
489 }
490 t.omap_setkeys(coll, info.pgid.make_pgmeta_oid(), km);
491 return 0;
492}
493
494const int OMAP_BATCH_SIZE = 25;
495void get_omap_batch(ObjectMap::ObjectMapIterator &iter, map<string, bufferlist> &oset)
496{
497 oset.clear();
498 for (int count = OMAP_BATCH_SIZE; count && iter->valid(); --count, iter->next()) {
499 oset.insert(pair<string, bufferlist>(iter->key(), iter->value()));
500 }
501}
502
503int ObjectStoreTool::export_file(ObjectStore *store, coll_t cid, ghobject_t &obj)
504{
505 struct stat st;
506 mysize_t total;
507 footer ft;
508
509 int ret = store->stat(cid, obj, &st);
510 if (ret < 0)
511 return ret;
512
513 cerr << "Read " << obj << std::endl;
514
515 total = st.st_size;
516 if (debug)
517 cerr << "size=" << total << std::endl;
518
519 object_begin objb(obj);
520
521 {
522 bufferptr bp;
523 bufferlist bl;
524 ret = store->getattr(cid, obj, OI_ATTR, bp);
525 if (ret < 0) {
526 cerr << "getattr failure object_info " << ret << std::endl;
527 return ret;
528 }
529 bl.push_back(bp);
530 decode(objb.oi, bl);
531 if (debug)
532 cerr << "object_info: " << objb.oi << std::endl;
533 }
534
535 // NOTE: we include whiteouts, lost, etc.
536
537 ret = write_section(TYPE_OBJECT_BEGIN, objb, file_fd);
538 if (ret < 0)
539 return ret;
540
541 uint64_t offset = 0;
542 bufferlist rawdatabl;
543 while(total > 0) {
544 rawdatabl.clear();
545 mysize_t len = max_read;
546 if (len > total)
547 len = total;
548
549 ret = store->read(cid, obj, offset, len, rawdatabl);
550 if (ret < 0)
551 return ret;
552 if (ret == 0)
553 return -EINVAL;
554
555 data_section dblock(offset, len, rawdatabl);
556 if (debug)
557 cerr << "data section offset=" << offset << " len=" << len << std::endl;
558
559 total -= ret;
560 offset += ret;
561
562 ret = write_section(TYPE_DATA, dblock, file_fd);
563 if (ret) return ret;
564 }
565
566 //Handle attrs for this object
567 map<string,bufferptr> aset;
568 ret = store->getattrs(cid, obj, aset);
569 if (ret) return ret;
570 attr_section as(aset);
571 ret = write_section(TYPE_ATTRS, as, file_fd);
572 if (ret)
573 return ret;
574
575 if (debug) {
576 cerr << "attrs size " << aset.size() << std::endl;
577 }
578
579 //Handle omap information
580 bufferlist hdrbuf;
581 ret = store->omap_get_header(cid, obj, &hdrbuf, true);
582 if (ret < 0) {
583 cerr << "omap_get_header: " << cpp_strerror(ret) << std::endl;
584 return ret;
585 }
586
587 omap_hdr_section ohs(hdrbuf);
588 ret = write_section(TYPE_OMAP_HDR, ohs, file_fd);
589 if (ret)
590 return ret;
591
592 ObjectMap::ObjectMapIterator iter = store->get_omap_iterator(cid, obj);
593 if (!iter) {
594 ret = -ENOENT;
595 cerr << "omap_get_iterator: " << cpp_strerror(ret) << std::endl;
596 return ret;
597 }
598 iter->seek_to_first();
599 int mapcount = 0;
600 map<string, bufferlist> out;
601 while(iter->valid()) {
602 get_omap_batch(iter, out);
603
604 if (out.empty()) break;
605
606 mapcount += out.size();
607 omap_section oms(out);
608 ret = write_section(TYPE_OMAP, oms, file_fd);
609 if (ret)
610 return ret;
611 }
612 if (debug)
613 cerr << "omap map size " << mapcount << std::endl;
614
615 ret = write_simple(TYPE_OBJECT_END, file_fd);
616 if (ret)
617 return ret;
618
619 return 0;
620}
621
622int ObjectStoreTool::export_files(ObjectStore *store, coll_t coll)
623{
624 ghobject_t next;
625
626 while (!next.is_max()) {
627 vector<ghobject_t> objects;
628 int r = store->collection_list(coll, next, ghobject_t::get_max(), 300,
629 &objects, &next);
630 if (r < 0)
631 return r;
632 for (vector<ghobject_t>::iterator i = objects.begin();
633 i != objects.end();
634 ++i) {
635 assert(!i->hobj.is_meta());
636 if (i->is_pgmeta() || i->hobj.is_temp()) {
637 continue;
638 }
639 r = export_file(store, coll, *i);
640 if (r < 0)
641 return r;
642 }
643 }
644 return 0;
645}
646
647int set_inc_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl, bool force,
648 ObjectStore::Sequencer &osr) {
649 OSDMap::Incremental inc;
650 bufferlist::iterator it = bl.begin();
651 inc.decode(it);
652 if (e == 0) {
653 e = inc.epoch;
654 } else if (e != inc.epoch) {
655 cerr << "incremental.epoch mismatch: "
656 << inc.epoch << " != " << e << std::endl;
657 if (force) {
658 cerr << "But will continue anyway." << std::endl;
659 } else {
660 return -EINVAL;
661 }
662 }
663 const ghobject_t inc_oid = OSD::get_inc_osdmap_pobject_name(e);
664 if (!store->exists(coll_t::meta(), inc_oid)) {
665 cerr << "inc-osdmap (" << inc_oid << ") does not exist." << std::endl;
666 if (!force) {
667 return -ENOENT;
668 }
669 cout << "Creating a new epoch." << std::endl;
670 }
671 if (dry_run)
672 return 0;
673 ObjectStore::Transaction t;
674 t.write(coll_t::meta(), inc_oid, 0, bl.length(), bl);
675 t.truncate(coll_t::meta(), inc_oid, bl.length());
676 int ret = store->apply_transaction(&osr, std::move(t));
677 if (ret) {
678 cerr << "Failed to set inc-osdmap (" << inc_oid << "): " << ret << std::endl;
679 } else {
680 cout << "Wrote inc-osdmap." << inc.epoch << std::endl;
681 }
682 return ret;
683}
684
685int get_inc_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl)
686{
687 if (store->read(coll_t::meta(),
688 OSD::get_inc_osdmap_pobject_name(e),
689 0, 0, bl) < 0) {
690 return -ENOENT;
691 }
692 return 0;
693}
694
695int set_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl, bool force,
696 ObjectStore::Sequencer &osr) {
697 OSDMap osdmap;
698 osdmap.decode(bl);
699 if (e == 0) {
700 e = osdmap.get_epoch();
701 } else if (e != osdmap.get_epoch()) {
702 cerr << "osdmap.epoch mismatch: "
703 << e << " != " << osdmap.get_epoch() << std::endl;
704 if (force) {
705 cerr << "But will continue anyway." << std::endl;
706 } else {
707 return -EINVAL;
708 }
709 }
710 const ghobject_t full_oid = OSD::get_osdmap_pobject_name(e);
711 if (!store->exists(coll_t::meta(), full_oid)) {
712 cerr << "osdmap (" << full_oid << ") does not exist." << std::endl;
713 if (!force) {
714 return -ENOENT;
715 }
716 cout << "Creating a new epoch." << std::endl;
717 }
718 if (dry_run)
719 return 0;
720 ObjectStore::Transaction t;
721 t.write(coll_t::meta(), full_oid, 0, bl.length(), bl);
722 t.truncate(coll_t::meta(), full_oid, bl.length());
723 int ret = store->apply_transaction(&osr, std::move(t));
724 if (ret) {
725 cerr << "Failed to set osdmap (" << full_oid << "): " << ret << std::endl;
726 } else {
727 cout << "Wrote osdmap." << osdmap.get_epoch() << std::endl;
728 }
729 return ret;
730}
731
732int get_osdmap(ObjectStore *store, epoch_t e, OSDMap &osdmap, bufferlist& bl)
733{
734 bool found = store->read(
735 coll_t::meta(), OSD::get_osdmap_pobject_name(e), 0, 0, bl) >= 0;
736 if (!found) {
737 cerr << "Can't find OSDMap for pg epoch " << e << std::endl;
738 return -ENOENT;
739 }
740 osdmap.decode(bl);
741 if (debug)
742 cerr << osdmap << std::endl;
743 return 0;
744}
745
746int add_osdmap(ObjectStore *store, metadata_section &ms)
747{
748 return get_osdmap(store, ms.map_epoch, ms.osdmap, ms.osdmap_bl);
749}
750
751int ObjectStoreTool::do_export(ObjectStore *fs, coll_t coll, spg_t pgid,
752 pg_info_t &info, epoch_t map_epoch, __u8 struct_ver,
753 const OSDSuperblock& superblock,
754 PastIntervals &past_intervals)
755{
756 PGLog::IndexedLog log;
757 pg_missing_t missing;
758
759 cerr << "Exporting " << pgid << std::endl;
760
761 int ret = get_log(fs, struct_ver, coll, pgid, info, log, missing);
762 if (ret > 0)
763 return ret;
764
765 if (debug) {
766 Formatter *formatter = Formatter::create("json-pretty");
767 assert(formatter);
768 dump_log(formatter, cerr, log, missing);
769 delete formatter;
770 }
771 write_super();
772
773 pg_begin pgb(pgid, superblock);
774 // Special case: If replicated pg don't require the importing OSD to have shard feature
775 if (pgid.is_no_shard()) {
776 pgb.superblock.compat_features.incompat.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS);
777 }
778 ret = write_section(TYPE_PG_BEGIN, pgb, file_fd);
779 if (ret)
780 return ret;
781
782 // The metadata_section is now before files, so import can detect
783 // errors and abort without wasting time.
784 metadata_section ms(
785 struct_ver,
786 map_epoch,
787 info,
788 log,
789 past_intervals,
790 missing);
791 ret = add_osdmap(fs, ms);
792 if (ret)
793 return ret;
794 ret = write_section(TYPE_PG_METADATA, ms, file_fd);
795 if (ret)
796 return ret;
797
798 ret = export_files(fs, coll);
799 if (ret) {
800 cerr << "export_files error " << ret << std::endl;
801 return ret;
802 }
803
804 ret = write_simple(TYPE_PG_END, file_fd);
805 if (ret)
806 return ret;
807
808 return 0;
809}
810
811int get_data(ObjectStore *store, coll_t coll, ghobject_t hoid,
812 ObjectStore::Transaction *t, bufferlist &bl)
813{
814 bufferlist::iterator ebliter = bl.begin();
815 data_section ds;
816 ds.decode(ebliter);
817
818 if (debug)
819 cerr << "\tdata: offset " << ds.offset << " len " << ds.len << std::endl;
820 t->write(coll, hoid, ds.offset, ds.len, ds.databl);
821 return 0;
822}
823
824int get_attrs(
825 ObjectStore *store, coll_t coll, ghobject_t hoid,
826 ObjectStore::Transaction *t, bufferlist &bl,
224ce89b 827 OSDriver &driver, SnapMapper &snap_mapper)
7c673cae
FG
828{
829 bufferlist::iterator ebliter = bl.begin();
830 attr_section as;
831 as.decode(ebliter);
832
833 if (debug)
834 cerr << "\tattrs: len " << as.data.size() << std::endl;
835 t->setattrs(coll, hoid, as.data);
836
837 // This could have been handled in the caller if we didn't need to
838 // support exports that didn't include object_info_t in object_begin.
839 if (hoid.generation == ghobject_t::NO_GEN) {
840 if (hoid.hobj.snap < CEPH_MAXSNAP) {
841 map<string,bufferlist>::iterator mi = as.data.find(OI_ATTR);
842 if (mi != as.data.end()) {
843 object_info_t oi(mi->second);
844
845 if (debug)
846 cerr << "object_info " << oi << std::endl;
847
848 OSDriver::OSTransaction _t(driver.get_transaction(t));
849 set<snapid_t> oi_snaps(oi.legacy_snaps.begin(), oi.legacy_snaps.end());
850 if (!oi_snaps.empty()) {
851 if (debug)
852 cerr << "\tsetting legacy snaps " << oi_snaps << std::endl;
853 snap_mapper.add_oid(hoid.hobj, oi_snaps, &_t);
854 }
855 }
856 } else {
224ce89b 857 if (hoid.hobj.is_head()) {
7c673cae
FG
858 map<string,bufferlist>::iterator mi = as.data.find(SS_ATTR);
859 if (mi != as.data.end()) {
860 SnapSet snapset;
861 auto p = mi->second.begin();
862 snapset.decode(p);
863 cout << "snapset " << snapset << std::endl;
864 if (!snapset.is_legacy()) {
865 for (auto& p : snapset.clone_snaps) {
31f18b77
FG
866 ghobject_t clone = hoid;
867 clone.hobj.snap = p.first;
7c673cae 868 set<snapid_t> snaps(p.second.begin(), p.second.end());
31f18b77
FG
869 if (!store->exists(coll, clone)) {
870 // no clone, skip. this is probably a cache pool. this works
871 // because we use a separate transaction per object and clones
872 // come before head in the archive.
873 if (debug)
874 cerr << "\tskipping missing " << clone << " (snaps "
875 << snaps << ")" << std::endl;
876 continue;
877 }
7c673cae 878 if (debug)
31f18b77
FG
879 cerr << "\tsetting " << clone.hobj << " snaps " << snaps
880 << std::endl;
7c673cae
FG
881 OSDriver::OSTransaction _t(driver.get_transaction(t));
882 assert(!snaps.empty());
31f18b77 883 snap_mapper.add_oid(clone.hobj, snaps, &_t);
7c673cae
FG
884 }
885 }
886 } else {
887 cerr << "missing SS_ATTR on " << hoid << std::endl;
888 }
889 }
890 }
891 }
892
893 return 0;
894}
895
896int get_omap_hdr(ObjectStore *store, coll_t coll, ghobject_t hoid,
897 ObjectStore::Transaction *t, bufferlist &bl)
898{
899 bufferlist::iterator ebliter = bl.begin();
900 omap_hdr_section oh;
901 oh.decode(ebliter);
902
903 if (debug)
904 cerr << "\tomap header: " << string(oh.hdr.c_str(), oh.hdr.length())
905 << std::endl;
906 t->omap_setheader(coll, hoid, oh.hdr);
907 return 0;
908}
909
910int get_omap(ObjectStore *store, coll_t coll, ghobject_t hoid,
911 ObjectStore::Transaction *t, bufferlist &bl)
912{
913 bufferlist::iterator ebliter = bl.begin();
914 omap_section os;
915 os.decode(ebliter);
916
917 if (debug)
918 cerr << "\tomap: size " << os.omap.size() << std::endl;
919 t->omap_setkeys(coll, hoid, os.omap);
920 return 0;
921}
922
923int ObjectStoreTool::get_object(ObjectStore *store, coll_t coll,
924 bufferlist &bl, OSDMap &curmap,
925 bool *skipped_objects,
224ce89b 926 ObjectStore::Sequencer &osr)
7c673cae
FG
927{
928 ObjectStore::Transaction tran;
929 ObjectStore::Transaction *t = &tran;
930 bufferlist::iterator ebliter = bl.begin();
931 object_begin ob;
932 ob.decode(ebliter);
933 OSDriver driver(
934 store,
935 coll_t(),
936 OSD::make_snapmapper_oid());
937 spg_t pg;
938 coll.is_pg_prefix(&pg);
939 SnapMapper mapper(g_ceph_context, &driver, 0, 0, 0, pg.shard);
940
941 if (ob.hoid.hobj.is_temp()) {
942 cerr << "ERROR: Export contains temporary object '" << ob.hoid << "'" << std::endl;
943 return -EFAULT;
944 }
945 assert(g_ceph_context);
946 if (ob.hoid.hobj.nspace != g_ceph_context->_conf->osd_hit_set_namespace) {
947 object_t oid = ob.hoid.hobj.oid;
948 object_locator_t loc(ob.hoid.hobj);
949 pg_t raw_pgid = curmap.object_locator_to_pg(oid, loc);
950 pg_t pgid = curmap.raw_pg_to_pg(raw_pgid);
951
952 spg_t coll_pgid;
953 if (coll.is_pg(&coll_pgid) == false) {
954 cerr << "INTERNAL ERROR: Bad collection during import" << std::endl;
955 return -EFAULT;
956 }
957 if (coll_pgid.shard != ob.hoid.shard_id) {
958 cerr << "INTERNAL ERROR: Importing shard " << coll_pgid.shard
959 << " but object shard is " << ob.hoid.shard_id << std::endl;
960 return -EFAULT;
961 }
962
963 if (coll_pgid.pgid != pgid) {
964 cerr << "Skipping object '" << ob.hoid << "' which belongs in pg " << pgid << std::endl;
965 *skipped_objects = true;
966 skip_object(bl);
967 return 0;
968 }
969 }
970
971 if (!dry_run)
972 t->touch(coll, ob.hoid);
973
974 cout << "Write " << ob.hoid << std::endl;
975
7c673cae
FG
976 bufferlist ebl;
977 bool done = false;
978 while(!done) {
979 sectiontype_t type;
980 int ret = read_section(&type, &ebl);
981 if (ret)
982 return ret;
983
984 //cout << "\tdo_object: Section type " << hex << type << dec << std::endl;
985 //cout << "\t\tsection size " << ebl.length() << std::endl;
986 if (type >= END_OF_TYPES) {
987 cout << "Skipping unknown object section type" << std::endl;
988 continue;
989 }
990 switch(type) {
991 case TYPE_DATA:
992 if (dry_run) break;
993 ret = get_data(store, coll, ob.hoid, t, ebl);
994 if (ret) return ret;
995 break;
996 case TYPE_ATTRS:
997 if (dry_run) break;
224ce89b 998 ret = get_attrs(store, coll, ob.hoid, t, ebl, driver, mapper);
7c673cae
FG
999 if (ret) return ret;
1000 break;
1001 case TYPE_OMAP_HDR:
1002 if (dry_run) break;
1003 ret = get_omap_hdr(store, coll, ob.hoid, t, ebl);
1004 if (ret) return ret;
1005 break;
1006 case TYPE_OMAP:
1007 if (dry_run) break;
1008 ret = get_omap(store, coll, ob.hoid, t, ebl);
1009 if (ret) return ret;
1010 break;
1011 case TYPE_OBJECT_END:
1012 done = true;
1013 break;
1014 default:
1015 cerr << "Unknown section type " << type << std::endl;
1016 return -EFAULT;
1017 }
1018 }
1019 if (!dry_run)
1020 store->apply_transaction(&osr, std::move(*t));
1021 return 0;
1022}
1023
1024int get_pg_metadata(ObjectStore *store, bufferlist &bl, metadata_section &ms,
1025 const OSDSuperblock& sb, OSDMap& curmap, spg_t pgid)
1026{
1027 bufferlist::iterator ebliter = bl.begin();
1028 ms.decode(ebliter);
1029 spg_t old_pgid = ms.info.pgid;
1030 ms.info.pgid = pgid;
1031
1032#if DIAGNOSTIC
1033 Formatter *formatter = new JSONFormatter(true);
1034 cout << "export pgid " << old_pgid << std::endl;
1035 cout << "struct_v " << (int)ms.struct_ver << std::endl;
1036 cout << "map epoch " << ms.map_epoch << std::endl;
1037
1038 formatter->open_object_section("importing OSDMap");
1039 ms.osdmap.dump(formatter);
1040 formatter->close_section();
1041 formatter->flush(cout);
1042 cout << std::endl;
1043
1044 cout << "osd current epoch " << sb.current_epoch << std::endl;
1045 formatter->open_object_section("current OSDMap");
1046 curmap.dump(formatter);
1047 formatter->close_section();
1048 formatter->flush(cout);
1049 cout << std::endl;
1050
1051 formatter->open_object_section("info");
1052 ms.info.dump(formatter);
1053 formatter->close_section();
1054 formatter->flush(cout);
1055 cout << std::endl;
1056
1057 formatter->open_object_section("log");
1058 ms.log.dump(formatter);
1059 formatter->close_section();
1060 formatter->flush(cout);
1061 cout << std::endl;
1062
1063 formatter->flush(cout);
1064 cout << std::endl;
1065#endif
1066
1067 if (ms.osdmap.get_epoch() != 0 && ms.map_epoch != ms.osdmap.get_epoch()) {
1068 cerr << "FATAL: Invalid OSDMap epoch in export data" << std::endl;
1069 return -EFAULT;
1070 }
1071
1072 if (ms.map_epoch > sb.current_epoch) {
1073 cerr << "ERROR: Export PG's map_epoch " << ms.map_epoch << " > OSD's epoch " << sb.current_epoch << std::endl;
1074 cerr << "The OSD you are using is older than the exported PG" << std::endl;
1075 cerr << "Either use another OSD or join selected OSD to cluster to update it first" << std::endl;
1076 return -EINVAL;
1077 }
1078
1079 // Pool verified to exist for call to get_pg_num().
1080 unsigned new_pg_num = curmap.get_pg_num(pgid.pgid.pool());
1081
1082 if (pgid.pgid.ps() >= new_pg_num) {
1083 cerr << "Illegal pgid, the seed is larger than current pg_num" << std::endl;
1084 return -EINVAL;
1085 }
1086
1087 // Old exports didn't include OSDMap, see if we have a copy locally
1088 if (ms.osdmap.get_epoch() == 0) {
1089 OSDMap findmap;
1090 bufferlist findmap_bl;
1091 int ret = get_osdmap(store, ms.map_epoch, findmap, findmap_bl);
1092 if (ret == 0) {
1093 ms.osdmap.deepish_copy_from(findmap);
1094 } else {
1095 cerr << "WARNING: No OSDMap in old export,"
1096 " some objects may be ignored due to a split" << std::endl;
1097 }
1098 }
1099
1100 // Make sure old_pg_num is 0 in the unusual case that OSDMap not in export
1101 // nor can we find a local copy.
1102 unsigned old_pg_num = 0;
1103 if (ms.osdmap.get_epoch() != 0)
1104 old_pg_num = ms.osdmap.get_pg_num(pgid.pgid.pool());
1105
1106 if (debug) {
1107 cerr << "old_pg_num " << old_pg_num << std::endl;
1108 cerr << "new_pg_num " << new_pg_num << std::endl;
1109 cerr << ms.osdmap << std::endl;
1110 cerr << curmap << std::endl;
1111 }
1112
1113 // If we have managed to have a good OSDMap we can do these checks
1114 if (old_pg_num) {
1115 if (old_pgid.pgid.ps() >= old_pg_num) {
1116 cerr << "FATAL: pgid invalid for original map epoch" << std::endl;
1117 return -EFAULT;
1118 }
1119 if (pgid.pgid.ps() >= old_pg_num) {
1120 cout << "NOTICE: Post split pgid specified" << std::endl;
1121 } else {
1122 spg_t parent(pgid);
1123 if (parent.is_split(old_pg_num, new_pg_num, NULL)) {
1124 cerr << "WARNING: Split occurred, some objects may be ignored" << std::endl;
1125 }
1126 }
1127 }
1128
1129 if (debug) {
1130 cerr << "Import pgid " << ms.info.pgid << std::endl;
1131 cerr << "Clearing past_intervals " << ms.past_intervals << std::endl;
1132 cerr << "Zero same_interval_since " << ms.info.history.same_interval_since << std::endl;
1133 }
1134
1135 // Let osd recompute past_intervals and same_interval_since
1136 ms.past_intervals.clear();
1137 ms.info.history.same_interval_since = 0;
1138
1139 if (debug)
1140 cerr << "Changing pg epoch " << ms.map_epoch << " to " << sb.current_epoch << std::endl;
1141
1142 ms.map_epoch = sb.current_epoch;
1143
1144 return 0;
1145}
1146
1147// out: pg_log_t that only has entries that apply to import_pgid using curmap
1148// reject: Entries rejected from "in" are in the reject.log. Other fields not set.
1149void filter_divergent_priors(spg_t import_pgid, const OSDMap &curmap,
1150 const string &hit_set_namespace, const divergent_priors_t &in,
1151 divergent_priors_t &out, divergent_priors_t &reject)
1152{
1153 out.clear();
1154 reject.clear();
1155
1156 for (divergent_priors_t::const_iterator i = in.begin();
1157 i != in.end(); ++i) {
1158
1159 // Reject divergent priors for temporary objects
1160 if (i->second.is_temp()) {
1161 reject.insert(*i);
1162 continue;
1163 }
1164
1165 if (i->second.nspace != hit_set_namespace) {
1166 object_t oid = i->second.oid;
1167 object_locator_t loc(i->second);
1168 pg_t raw_pgid = curmap.object_locator_to_pg(oid, loc);
1169 pg_t pgid = curmap.raw_pg_to_pg(raw_pgid);
1170
1171 if (import_pgid.pgid == pgid) {
1172 out.insert(*i);
1173 } else {
1174 reject.insert(*i);
1175 }
1176 } else {
1177 out.insert(*i);
1178 }
1179 }
1180}
1181
1182int ObjectStoreTool::do_import(ObjectStore *store, OSDSuperblock& sb,
1183 bool force, std::string pgidstr,
1184 ObjectStore::Sequencer &osr)
1185{
1186 bufferlist ebl;
1187 pg_info_t info;
1188 PGLog::IndexedLog log;
1189 bool skipped_objects = false;
1190
1191 if (!dry_run)
1192 finish_remove_pgs(store);
1193
1194 int ret = read_super();
1195 if (ret)
1196 return ret;
1197
1198 if (sh.magic != super_header::super_magic) {
1199 cerr << "Invalid magic number" << std::endl;
1200 return -EFAULT;
1201 }
1202
1203 if (sh.version > super_header::super_ver) {
1204 cerr << "Can't handle export format version=" << sh.version << std::endl;
1205 return -EINVAL;
1206 }
1207
1208 //First section must be TYPE_PG_BEGIN
1209 sectiontype_t type;
1210 ret = read_section(&type, &ebl);
1211 if (ret)
1212 return ret;
1213 if (type == TYPE_POOL_BEGIN) {
1214 cerr << "Pool exports cannot be imported into a PG" << std::endl;
1215 return -EINVAL;
1216 } else if (type != TYPE_PG_BEGIN) {
1217 cerr << "Invalid first section type " << type << std::endl;
1218 return -EFAULT;
1219 }
1220
1221 bufferlist::iterator ebliter = ebl.begin();
1222 pg_begin pgb;
1223 pgb.decode(ebliter);
1224 spg_t pgid = pgb.pgid;
1225 spg_t orig_pgid = pgid;
1226
1227 if (pgidstr.length()) {
1228 spg_t user_pgid;
1229
1230 bool ok = user_pgid.parse(pgidstr.c_str());
1231 // This succeeded in main() already
1232 assert(ok);
1233 if (pgid != user_pgid) {
1234 if (pgid.pool() != user_pgid.pool()) {
1235 cerr << "Can't specify a different pgid pool, must be " << pgid.pool() << std::endl;
1236 return -EINVAL;
1237 }
1238 if (pgid.is_no_shard() && !user_pgid.is_no_shard()) {
1239 cerr << "Can't specify a sharded pgid with a non-sharded export" << std::endl;
1240 return -EINVAL;
1241 }
1242 // Get shard from export information if not specified
1243 if (!pgid.is_no_shard() && user_pgid.is_no_shard()) {
1244 user_pgid.shard = pgid.shard;
1245 }
1246 if (pgid.shard != user_pgid.shard) {
1247 cerr << "Can't specify a different shard, must be " << pgid.shard << std::endl;
1248 return -EINVAL;
1249 }
1250 pgid = user_pgid;
1251 }
1252 }
1253
1254 if (!pgb.superblock.cluster_fsid.is_zero()
1255 && pgb.superblock.cluster_fsid != sb.cluster_fsid) {
1256 cerr << "Export came from different cluster with fsid "
1257 << pgb.superblock.cluster_fsid << std::endl;
1258 return -EINVAL;
1259 }
1260
1261 if (debug) {
1262 cerr << "Exported features: " << pgb.superblock.compat_features << std::endl;
1263 }
1264
1265 // Special case: Old export has SHARDS incompat feature on replicated pg, remove it
1266 if (pgid.is_no_shard())
1267 pgb.superblock.compat_features.incompat.remove(CEPH_OSD_FEATURE_INCOMPAT_SHARDS);
1268
1269 if (sb.compat_features.compare(pgb.superblock.compat_features) == -1) {
1270 CompatSet unsupported = sb.compat_features.unsupported(pgb.superblock.compat_features);
1271
1272 cerr << "Export has incompatible features set " << unsupported << std::endl;
1273
1274 // Let them import if they specify the --force option
1275 if (!force)
1276 return 11; // Positive return means exit status
1277 }
1278
1279 // Don't import if pool no longer exists
1280 OSDMap curmap;
1281 bufferlist bl;
1282 ret = get_osdmap(store, sb.current_epoch, curmap, bl);
1283 if (ret) {
1284 cerr << "Can't find local OSDMap" << std::endl;
1285 return ret;
1286 }
1287 if (!curmap.have_pg_pool(pgid.pgid.m_pool)) {
1288 cerr << "Pool " << pgid.pgid.m_pool << " no longer exists" << std::endl;
1289 // Special exit code for this error, used by test code
1290 return 10; // Positive return means exit status
1291 }
1292
1293 ghobject_t pgmeta_oid = pgid.make_pgmeta_oid();
1294 log_oid = OSD::make_pg_log_oid(pgid);
1295 biginfo_oid = OSD::make_pg_biginfo_oid(pgid);
1296
1297 //Check for PG already present.
1298 coll_t coll(pgid);
1299 if (store->collection_exists(coll)) {
1300 cerr << "pgid " << pgid << " already exists" << std::endl;
1301 return -EEXIST;
1302 }
1303
1304 if (!dry_run) {
1305 ObjectStore::Transaction t;
1306 PG::_create(t, pgid,
1307 pgid.get_split_bits(curmap.get_pg_pool(pgid.pool())->get_pg_num()));
1308 PG::_init(t, pgid, NULL);
1309
1310 // mark this coll for removal until we're done
1311 map<string,bufferlist> values;
1312 ::encode((char)1, values["_remove"]);
1313 t.omap_setkeys(coll, pgid.make_pgmeta_oid(), values);
1314
1315 store->apply_transaction(&osr, std::move(t));
1316 }
1317
1318 cout << "Importing pgid " << pgid;
1319 if (orig_pgid != pgid) {
1320 cout << " exported as " << orig_pgid;
1321 }
1322 cout << std::endl;
1323
1324 bool done = false;
1325 bool found_metadata = false;
1326 metadata_section ms;
7c673cae
FG
1327 while(!done) {
1328 ret = read_section(&type, &ebl);
1329 if (ret)
1330 return ret;
1331
1332 //cout << "do_import: Section type " << hex << type << dec << std::endl;
1333 if (type >= END_OF_TYPES) {
1334 cout << "Skipping unknown section type" << std::endl;
1335 continue;
1336 }
1337 switch(type) {
1338 case TYPE_OBJECT_BEGIN:
224ce89b 1339 ret = get_object(store, coll, ebl, curmap, &skipped_objects, osr);
7c673cae
FG
1340 if (ret) return ret;
1341 break;
1342 case TYPE_PG_METADATA:
1343 ret = get_pg_metadata(store, ebl, ms, sb, curmap, pgid);
1344 if (ret) return ret;
1345 found_metadata = true;
1346 break;
1347 case TYPE_PG_END:
1348 done = true;
1349 break;
1350 default:
1351 cerr << "Unknown section type " << type << std::endl;
1352 return -EFAULT;
1353 }
1354 }
1355
1356 if (!found_metadata) {
1357 cerr << "Missing metadata section" << std::endl;
1358 return -EFAULT;
1359 }
1360
1361 ObjectStore::Transaction t;
1362 if (!dry_run) {
1363 pg_log_t newlog, reject;
1364 pg_log_t::filter_log(pgid, curmap, g_ceph_context->_conf->osd_hit_set_namespace,
1365 ms.log, newlog, reject);
1366 if (debug) {
1367 for (list<pg_log_entry_t>::iterator i = newlog.log.begin();
1368 i != newlog.log.end(); ++i)
1369 cerr << "Keeping log entry " << *i << std::endl;
1370 for (list<pg_log_entry_t>::iterator i = reject.log.begin();
1371 i != reject.log.end(); ++i)
1372 cerr << "Skipping log entry " << *i << std::endl;
1373 }
1374
1375 divergent_priors_t newdp, rejectdp;
1376 filter_divergent_priors(pgid, curmap, g_ceph_context->_conf->osd_hit_set_namespace,
1377 ms.divergent_priors, newdp, rejectdp);
1378 ms.divergent_priors = newdp;
1379 if (debug) {
1380 for (divergent_priors_t::iterator i = newdp.begin();
1381 i != newdp.end(); ++i)
1382 cerr << "Keeping divergent_prior " << *i << std::endl;
1383 for (divergent_priors_t::iterator i = rejectdp.begin();
1384 i != rejectdp.end(); ++i)
1385 cerr << "Skipping divergent_prior " << *i << std::endl;
1386 }
1387
1388 ms.missing.filter_objects([&](const hobject_t &obj) {
1389 if (obj.nspace == g_ceph_context->_conf->osd_hit_set_namespace)
1390 return false;
1391 assert(!obj.is_temp());
1392 object_t oid = obj.oid;
1393 object_locator_t loc(obj);
1394 pg_t raw_pgid = curmap.object_locator_to_pg(oid, loc);
1395 pg_t _pgid = curmap.raw_pg_to_pg(raw_pgid);
1396
1397 return pgid.pgid != _pgid;
1398 });
1399
1400
1401 if (debug) {
1402 pg_missing_t missing;
1403 Formatter *formatter = Formatter::create("json-pretty");
1404 dump_log(formatter, cerr, newlog, ms.missing);
1405 delete formatter;
1406 }
1407
1408 // Just like a split invalidate stats since the object count is changed
1409 if (skipped_objects)
1410 ms.info.stats.stats_invalid = true;
1411
1412 ret = write_pg(
1413 t,
1414 ms.map_epoch,
1415 ms.info,
1416 newlog,
1417 ms.past_intervals,
1418 ms.divergent_priors,
1419 ms.missing);
1420 if (ret) return ret;
1421 }
1422
1423 // done, clear removal flag
1424 if (debug)
1425 cerr << "done, clearing removal flag" << std::endl;
1426
1427 if (!dry_run) {
1428 set<string> remove;
1429 remove.insert("_remove");
1430 t.omap_rmkeys(coll, pgid.make_pgmeta_oid(), remove);
1431 store->apply_transaction(&osr, std::move(t));
1432 }
1433
1434 return 0;
1435}
1436
1437int do_list(ObjectStore *store, string pgidstr, string object, boost::optional<std::string> nspace,
1438 Formatter *formatter, bool debug, bool human_readable, bool head)
1439{
1440 int r;
1441 lookup_ghobject lookup(object, nspace, head);
1442 if (pgidstr.length() > 0) {
1443 r = action_on_all_objects_in_pg(store, pgidstr, lookup, debug);
1444 } else {
1445 r = action_on_all_objects(store, lookup, debug);
1446 }
1447 if (r)
1448 return r;
1449 lookup.dump(formatter, human_readable);
1450 formatter->flush(cout);
1451 return 0;
1452}
1453
1454int do_meta(ObjectStore *store, string object, Formatter *formatter, bool debug, bool human_readable)
1455{
1456 int r;
1457 boost::optional<std::string> nspace; // Not specified
1458 lookup_ghobject lookup(object, nspace);
1459 r = action_on_all_objects_in_exact_pg(store, coll_t::meta(), lookup, debug);
1460 if (r)
1461 return r;
1462 lookup.dump(formatter, human_readable);
1463 formatter->flush(cout);
1464 return 0;
1465}
1466
1467int remove_object(coll_t coll, ghobject_t &ghobj,
1468 SnapMapper &mapper,
1469 MapCacher::Transaction<std::string, bufferlist> *_t,
1470 ObjectStore::Transaction *t)
1471{
1472 int r = mapper.remove_oid(ghobj.hobj, _t);
1473 if (r < 0 && r != -ENOENT) {
1474 cerr << "remove_oid returned " << cpp_strerror(r) << std::endl;
1475 return r;
1476 }
1477
1478 t->remove(coll, ghobj);
1479 return 0;
1480}
1481
1482int get_snapset(ObjectStore *store, coll_t coll, ghobject_t &ghobj, SnapSet &ss, bool silent);
1483
1484int do_remove_object(ObjectStore *store, coll_t coll,
1485 ghobject_t &ghobj, bool all, bool force,
1486 ObjectStore::Sequencer &osr)
1487{
1488 spg_t pg;
1489 coll.is_pg_prefix(&pg);
1490 OSDriver driver(
1491 store,
1492 coll_t(),
1493 OSD::make_snapmapper_oid());
1494 SnapMapper mapper(g_ceph_context, &driver, 0, 0, 0, pg.shard);
1495 struct stat st;
1496
1497 int r = store->stat(coll, ghobj, &st);
1498 if (r < 0) {
1499 cerr << "remove: " << cpp_strerror(r) << std::endl;
1500 return r;
1501 }
1502
1503 SnapSet ss;
1504 if (ghobj.hobj.has_snapset()) {
1505 r = get_snapset(store, coll, ghobj, ss, false);
1506 if (r < 0) {
1507 cerr << "Can't get snapset error " << cpp_strerror(r) << std::endl;
1508 return r;
1509 }
1510 if (!ss.snaps.empty() && !all) {
1511 if (force) {
1512 cout << "WARNING: only removing "
1513 << (ghobj.hobj.is_head() ? "head" : "snapdir")
1514 << " with snapshots present" << std::endl;
1515 ss.snaps.clear();
1516 } else {
1517 cerr << "Snapshots are present, use removeall to delete everything" << std::endl;
1518 return -EINVAL;
1519 }
1520 }
1521 }
1522
1523 ObjectStore::Transaction t;
1524 OSDriver::OSTransaction _t(driver.get_transaction(&t));
1525
1526 cout << "remove " << ghobj << std::endl;
1527
1528 if (!dry_run) {
1529 r = remove_object(coll, ghobj, mapper, &_t, &t);
1530 if (r < 0)
1531 return r;
1532 }
1533
1534 ghobject_t snapobj = ghobj;
1535 for (vector<snapid_t>::iterator i = ss.snaps.begin() ;
1536 i != ss.snaps.end() ; ++i) {
1537 snapobj.hobj.snap = *i;
1538 cout << "remove " << snapobj << std::endl;
1539 if (!dry_run) {
1540 r = remove_object(coll, snapobj, mapper, &_t, &t);
1541 if (r < 0)
1542 return r;
1543 }
1544 }
1545
1546 if (!dry_run)
1547 store->apply_transaction(&osr, std::move(t));
1548
1549 return 0;
1550}
1551
1552int do_list_attrs(ObjectStore *store, coll_t coll, ghobject_t &ghobj)
1553{
1554 map<string,bufferptr> aset;
1555 int r = store->getattrs(coll, ghobj, aset);
1556 if (r < 0) {
1557 cerr << "getattrs: " << cpp_strerror(r) << std::endl;
1558 return r;
1559 }
1560
1561 for (map<string,bufferptr>::iterator i = aset.begin();i != aset.end(); ++i) {
1562 string key(i->first);
1563 if (outistty)
1564 key = cleanbin(key);
1565 cout << key << std::endl;
1566 }
1567 return 0;
1568}
1569
1570int do_list_omap(ObjectStore *store, coll_t coll, ghobject_t &ghobj)
1571{
1572 ObjectMap::ObjectMapIterator iter = store->get_omap_iterator(coll, ghobj);
1573 if (!iter) {
1574 cerr << "omap_get_iterator: " << cpp_strerror(ENOENT) << std::endl;
1575 return -ENOENT;
1576 }
1577 iter->seek_to_first();
1578 map<string, bufferlist> oset;
1579 while(iter->valid()) {
1580 get_omap_batch(iter, oset);
1581
1582 for (map<string,bufferlist>::iterator i = oset.begin();i != oset.end(); ++i) {
1583 string key(i->first);
1584 if (outistty)
1585 key = cleanbin(key);
1586 cout << key << std::endl;
1587 }
1588 }
1589 return 0;
1590}
1591
1592int do_get_bytes(ObjectStore *store, coll_t coll, ghobject_t &ghobj, int fd)
1593{
1594 struct stat st;
1595 mysize_t total;
1596
1597 int ret = store->stat(coll, ghobj, &st);
1598 if (ret < 0) {
1599 cerr << "get-bytes: " << cpp_strerror(ret) << std::endl;
1600 return ret;
1601 }
1602
1603 total = st.st_size;
1604 if (debug)
1605 cerr << "size=" << total << std::endl;
1606
1607 uint64_t offset = 0;
1608 bufferlist rawdatabl;
1609 while(total > 0) {
1610 rawdatabl.clear();
1611 mysize_t len = max_read;
1612 if (len > total)
1613 len = total;
1614
1615 ret = store->read(coll, ghobj, offset, len, rawdatabl);
1616 if (ret < 0)
1617 return ret;
1618 if (ret == 0)
1619 return -EINVAL;
1620
1621 if (debug)
1622 cerr << "data section offset=" << offset << " len=" << len << std::endl;
1623
1624 total -= ret;
1625 offset += ret;
1626
1627 ret = write(fd, rawdatabl.c_str(), ret);
1628 if (ret == -1) {
1629 perror("write");
1630 return -errno;
1631 }
1632 }
1633
1634 return 0;
1635}
1636
1637int do_set_bytes(ObjectStore *store, coll_t coll,
1638 ghobject_t &ghobj, int fd,
1639 ObjectStore::Sequencer &osr)
1640{
1641 ObjectStore::Transaction tran;
1642 ObjectStore::Transaction *t = &tran;
1643
1644 if (debug)
1645 cerr << "Write " << ghobj << std::endl;
1646
1647 if (!dry_run) {
1648 t->touch(coll, ghobj);
1649 t->truncate(coll, ghobj, 0);
1650 }
1651
1652 uint64_t offset = 0;
1653 bufferlist rawdatabl;
1654 do {
1655 rawdatabl.clear();
1656 ssize_t bytes = rawdatabl.read_fd(fd, max_read);
1657 if (bytes < 0) {
1658 cerr << "read_fd error " << cpp_strerror(bytes) << std::endl;
1659 return bytes;
1660 }
1661
1662 if (bytes == 0)
1663 break;
1664
1665 if (debug)
1666 cerr << "\tdata: offset " << offset << " bytes " << bytes << std::endl;
1667 if (!dry_run)
1668 t->write(coll, ghobj, offset, bytes, rawdatabl);
1669
1670 offset += bytes;
1671 // XXX: Should we apply_transaction() every once in a while for very large files
1672 } while(true);
1673
1674 if (!dry_run)
1675 store->apply_transaction(&osr, std::move(*t));
1676 return 0;
1677}
1678
1679int do_get_attr(ObjectStore *store, coll_t coll, ghobject_t &ghobj, string key)
1680{
1681 bufferptr bp;
1682
1683 int r = store->getattr(coll, ghobj, key.c_str(), bp);
1684 if (r < 0) {
1685 cerr << "getattr: " << cpp_strerror(r) << std::endl;
1686 return r;
1687 }
1688
1689 string value(bp.c_str(), bp.length());
1690 if (outistty) {
1691 value = cleanbin(value);
1692 value.push_back('\n');
1693 }
1694 cout << value;
1695
1696 return 0;
1697}
1698
1699int do_set_attr(ObjectStore *store, coll_t coll,
1700 ghobject_t &ghobj, string key, int fd,
1701 ObjectStore::Sequencer &osr)
1702{
1703 ObjectStore::Transaction tran;
1704 ObjectStore::Transaction *t = &tran;
1705 bufferlist bl;
1706
1707 if (debug)
1708 cerr << "Setattr " << ghobj << std::endl;
1709
1710 int ret = get_fd_data(fd, bl);
1711 if (ret < 0)
1712 return ret;
1713
1714 if (dry_run)
1715 return 0;
1716
1717 t->touch(coll, ghobj);
1718
1719 t->setattr(coll, ghobj, key, bl);
1720
1721 store->apply_transaction(&osr, std::move(*t));
1722 return 0;
1723}
1724
1725int do_rm_attr(ObjectStore *store, coll_t coll,
1726 ghobject_t &ghobj, string key,
1727 ObjectStore::Sequencer &osr)
1728{
1729 ObjectStore::Transaction tran;
1730 ObjectStore::Transaction *t = &tran;
1731
1732 if (debug)
1733 cerr << "Rmattr " << ghobj << std::endl;
1734
1735 if (dry_run)
1736 return 0;
1737
1738 t->rmattr(coll, ghobj, key);
1739
1740 store->apply_transaction(&osr, std::move(*t));
1741 return 0;
1742}
1743
1744int do_get_omap(ObjectStore *store, coll_t coll, ghobject_t &ghobj, string key)
1745{
1746 set<string> keys;
1747 map<string, bufferlist> out;
1748
1749 keys.insert(key);
1750
1751 int r = store->omap_get_values(coll, ghobj, keys, &out);
1752 if (r < 0) {
1753 cerr << "omap_get_values: " << cpp_strerror(r) << std::endl;
1754 return r;
1755 }
1756
1757 if (out.empty()) {
1758 cerr << "Key not found" << std::endl;
1759 return -ENOENT;
1760 }
1761
1762 assert(out.size() == 1);
1763
1764 bufferlist bl = out.begin()->second;
1765 string value(bl.c_str(), bl.length());
1766 if (outistty) {
1767 value = cleanbin(value);
1768 value.push_back('\n');
1769 }
1770 cout << value;
1771
1772 return 0;
1773}
1774
1775int do_set_omap(ObjectStore *store, coll_t coll,
1776 ghobject_t &ghobj, string key, int fd,
1777 ObjectStore::Sequencer &osr)
1778{
1779 ObjectStore::Transaction tran;
1780 ObjectStore::Transaction *t = &tran;
1781 map<string, bufferlist> attrset;
1782 bufferlist valbl;
1783
1784 if (debug)
1785 cerr << "Set_omap " << ghobj << std::endl;
1786
1787 int ret = get_fd_data(fd, valbl);
1788 if (ret < 0)
1789 return ret;
1790
1791 attrset.insert(pair<string, bufferlist>(key, valbl));
1792
1793 if (dry_run)
1794 return 0;
1795
1796 t->touch(coll, ghobj);
1797
1798 t->omap_setkeys(coll, ghobj, attrset);
1799
1800 store->apply_transaction(&osr, std::move(*t));
1801 return 0;
1802}
1803
1804int do_rm_omap(ObjectStore *store, coll_t coll,
1805 ghobject_t &ghobj, string key,
1806 ObjectStore::Sequencer &osr)
1807{
1808 ObjectStore::Transaction tran;
1809 ObjectStore::Transaction *t = &tran;
1810 set<string> keys;
1811
1812 keys.insert(key);
1813
1814 if (debug)
1815 cerr << "Rm_omap " << ghobj << std::endl;
1816
1817 if (dry_run)
1818 return 0;
1819
1820 t->omap_rmkeys(coll, ghobj, keys);
1821
1822 store->apply_transaction(&osr, std::move(*t));
1823 return 0;
1824}
1825
1826int do_get_omaphdr(ObjectStore *store, coll_t coll, ghobject_t &ghobj)
1827{
1828 bufferlist hdrbl;
1829
1830 int r = store->omap_get_header(coll, ghobj, &hdrbl, true);
1831 if (r < 0) {
1832 cerr << "omap_get_header: " << cpp_strerror(r) << std::endl;
1833 return r;
1834 }
1835
1836 string header(hdrbl.c_str(), hdrbl.length());
1837 if (outistty) {
1838 header = cleanbin(header);
1839 header.push_back('\n');
1840 }
1841 cout << header;
1842
1843 return 0;
1844}
1845
1846int do_set_omaphdr(ObjectStore *store, coll_t coll,
1847 ghobject_t &ghobj, int fd,
1848 ObjectStore::Sequencer &osr)
1849{
1850 ObjectStore::Transaction tran;
1851 ObjectStore::Transaction *t = &tran;
1852 bufferlist hdrbl;
1853
1854 if (debug)
1855 cerr << "Omap_setheader " << ghobj << std::endl;
1856
1857 int ret = get_fd_data(fd, hdrbl);
1858 if (ret)
1859 return ret;
1860
1861 if (dry_run)
1862 return 0;
1863
1864 t->touch(coll, ghobj);
1865
1866 t->omap_setheader(coll, ghobj, hdrbl);
1867
1868 store->apply_transaction(&osr, std::move(*t));
1869 return 0;
1870}
1871
1872struct do_fix_lost : public action_on_object_t {
1873 ObjectStore::Sequencer *osr;
1874
1875 explicit do_fix_lost(ObjectStore::Sequencer *_osr) : osr(_osr) {}
1876
1877 int call(ObjectStore *store, coll_t coll,
1878 ghobject_t &ghobj, object_info_t &oi) override {
1879 if (oi.is_lost()) {
1880 cout << coll << "/" << ghobj << " is lost";
1881 if (!dry_run)
1882 cout << ", fixing";
1883 cout << std::endl;
1884 if (dry_run)
1885 return 0;
1886 oi.clear_flag(object_info_t::FLAG_LOST);
1887 bufferlist bl;
1888 ::encode(oi, bl, -1); /* fixme: using full features */
1889 ObjectStore::Transaction t;
1890 t.setattr(coll, ghobj, OI_ATTR, bl);
1891 int r = store->apply_transaction(osr, std::move(t));
1892 if (r < 0) {
1893 cerr << "Error getting fixing attr on : " << make_pair(coll, ghobj)
1894 << ", "
1895 << cpp_strerror(r) << std::endl;
1896 return r;
1897 }
1898 }
1899 return 0;
1900 }
1901};
1902
1903int get_snapset(ObjectStore *store, coll_t coll, ghobject_t &ghobj, SnapSet &ss, bool silent = false)
1904{
1905 bufferlist attr;
1906 int r = store->getattr(coll, ghobj, SS_ATTR, attr);
1907 if (r < 0) {
1908 if (!silent)
1909 cerr << "Error getting snapset on : " << make_pair(coll, ghobj) << ", "
1910 << cpp_strerror(r) << std::endl;
1911 return r;
1912 }
1913 bufferlist::iterator bp = attr.begin();
1914 try {
1915 ::decode(ss, bp);
1916 } catch (...) {
1917 r = -EINVAL;
1918 cerr << "Error decoding snapset on : " << make_pair(coll, ghobj) << ", "
1919 << cpp_strerror(r) << std::endl;
1920 return r;
1921 }
1922 return 0;
1923}
1924
1925int print_obj_info(ObjectStore *store, coll_t coll, ghobject_t &ghobj, Formatter* formatter)
1926{
1927 int r = 0;
1928 formatter->open_object_section("obj");
1929 formatter->open_object_section("id");
1930 ghobj.dump(formatter);
1931 formatter->close_section();
1932
1933 bufferlist attr;
1934 int gr = store->getattr(coll, ghobj, OI_ATTR, attr);
1935 if (gr < 0) {
1936 r = gr;
1937 cerr << "Error getting attr on : " << make_pair(coll, ghobj) << ", "
1938 << cpp_strerror(r) << std::endl;
1939 } else {
1940 object_info_t oi;
1941 bufferlist::iterator bp = attr.begin();
1942 try {
1943 ::decode(oi, bp);
1944 formatter->open_object_section("info");
1945 oi.dump(formatter);
1946 formatter->close_section();
1947 } catch (...) {
1948 r = -EINVAL;
1949 cerr << "Error decoding attr on : " << make_pair(coll, ghobj) << ", "
1950 << cpp_strerror(r) << std::endl;
1951 }
1952 }
1953 struct stat st;
1954 int sr = store->stat(coll, ghobj, &st, true);
1955 if (sr < 0) {
1956 r = sr;
1957 cerr << "Error stat on : " << make_pair(coll, ghobj) << ", "
1958 << cpp_strerror(r) << std::endl;
1959 } else {
1960 formatter->open_object_section("stat");
1961 formatter->dump_int("size", st.st_size);
1962 formatter->dump_int("blksize", st.st_blksize);
1963 formatter->dump_int("blocks", st.st_blocks);
1964 formatter->dump_int("nlink", st.st_nlink);
1965 formatter->close_section();
1966 }
1967
1968 if (ghobj.hobj.has_snapset()) {
1969 SnapSet ss;
1970 int snr = get_snapset(store, coll, ghobj, ss);
1971 if (snr < 0) {
1972 r = snr;
1973 } else {
1974 formatter->open_object_section("SnapSet");
1975 ss.dump(formatter);
1976 formatter->close_section();
1977 }
1978 }
1979 formatter->close_section();
1980 formatter->flush(cout);
1981 cout << std::endl;
1982 return r;
1983}
1984
1985int set_size(ObjectStore *store, coll_t coll, ghobject_t &ghobj, uint64_t setsize, Formatter* formatter,
1986 ObjectStore::Sequencer &osr)
1987{
1988 if (ghobj.hobj.is_snapdir()) {
1989 cerr << "Can't set the size of a snapdir" << std::endl;
1990 return -EINVAL;
1991 }
1992 bufferlist attr;
1993 int r = store->getattr(coll, ghobj, OI_ATTR, attr);
1994 if (r < 0) {
1995 cerr << "Error getting attr on : " << make_pair(coll, ghobj) << ", "
1996 << cpp_strerror(r) << std::endl;
1997 return r;
1998 }
1999 object_info_t oi;
2000 bufferlist::iterator bp = attr.begin();
2001 try {
2002 ::decode(oi, bp);
2003 } catch (...) {
2004 r = -EINVAL;
2005 cerr << "Error getting attr on : " << make_pair(coll, ghobj) << ", "
2006 << cpp_strerror(r) << std::endl;
2007 return r;
2008 }
2009 struct stat st;
2010 r = store->stat(coll, ghobj, &st, true);
2011 if (r < 0) {
2012 cerr << "Error stat on : " << make_pair(coll, ghobj) << ", "
2013 << cpp_strerror(r) << std::endl;
2014 }
2015 ghobject_t head(ghobj);
2016 SnapSet ss;
2017 bool found_head = true;
2018 map<snapid_t, uint64_t>::iterator csi;
2019 bool is_snap = ghobj.hobj.is_snap();
2020 if (is_snap) {
2021 head.hobj = head.hobj.get_head();
2022 r = get_snapset(store, coll, head, ss, true);
2023 if (r < 0 && r != -ENOENT) {
2024 // Requested get_snapset() silent, so if not -ENOENT show error
2025 cerr << "Error getting snapset on : " << make_pair(coll, head) << ", "
2026 << cpp_strerror(r) << std::endl;
2027 return r;
2028 }
2029 if (r == -ENOENT) {
2030 head.hobj = head.hobj.get_snapdir();
2031 r = get_snapset(store, coll, head, ss);
2032 if (r < 0)
2033 return r;
2034 found_head = false;
2035 } else {
2036 found_head = true;
2037 }
2038 csi = ss.clone_size.find(ghobj.hobj.snap);
2039 if (csi == ss.clone_size.end()) {
2040 cerr << "SnapSet is missing clone_size for snap " << ghobj.hobj.snap << std::endl;
2041 return -EINVAL;
2042 }
2043 }
2044 if ((uint64_t)st.st_size == setsize && oi.size == setsize
2045 && (!is_snap || csi->second == setsize)) {
2046 cout << "Size of object is already " << setsize << std::endl;
2047 return 0;
2048 }
2049 cout << "Setting size to " << setsize << ", stat size " << st.st_size
2050 << ", obj info size " << oi.size;
2051 if (is_snap) {
2052 cout << ", " << (found_head ? "head" : "snapdir")
2053 << " clone_size " << csi->second;
2054 csi->second = setsize;
2055 }
2056 cout << std::endl;
2057 if (!dry_run) {
2058 attr.clear();
2059 oi.size = setsize;
2060 ::encode(oi, attr, -1); /* fixme: using full features */
2061 ObjectStore::Transaction t;
2062 t.setattr(coll, ghobj, OI_ATTR, attr);
2063 t.truncate(coll, ghobj, setsize);
2064 if (is_snap) {
2065 bufferlist snapattr;
2066 snapattr.clear();
2067 ::encode(ss, snapattr);
2068 t.setattr(coll, head, SS_ATTR, snapattr);
2069 }
2070 r = store->apply_transaction(&osr, std::move(t));
2071 if (r < 0) {
2072 cerr << "Error writing object info: " << make_pair(coll, ghobj) << ", "
2073 << cpp_strerror(r) << std::endl;
2074 return r;
2075 }
2076 }
2077 return 0;
2078}
2079
2080int clear_snapset(ObjectStore *store, coll_t coll, ghobject_t &ghobj,
2081 string arg, ObjectStore::Sequencer &osr)
2082{
2083 SnapSet ss;
2084 int ret = get_snapset(store, coll, ghobj, ss);
2085 if (ret < 0)
2086 return ret;
2087
2088 // Use "head" to set head_exists incorrectly
2089 if (arg == "corrupt" || arg == "head")
2090 ss.head_exists = !ghobj.hobj.is_head();
2091 else if (ss.head_exists != ghobj.hobj.is_head()) {
2092 cerr << "Correcting head_exists, set to "
2093 << (ghobj.hobj.is_head() ? "true" : "false") << std::endl;
2094 ss.head_exists = ghobj.hobj.is_head();
2095 }
2096 // Use "corrupt" to clear entire SnapSet
2097 // Use "seq" to just corrupt SnapSet.seq
2098 if (arg == "corrupt" || arg == "seq")
2099 ss.seq = 0;
2100 // Use "snaps" to just clear SnapSet.snaps
2101 if (arg == "corrupt" || arg == "snaps")
2102 ss.snaps.clear();
2103 // By default just clear clone, clone_overlap and clone_size
2104 if (arg == "corrupt")
2105 arg = "";
2106 if (arg == "" || arg == "clones")
2107 ss.clones.clear();
2108 if (arg == "" || arg == "clone_overlap")
2109 ss.clone_overlap.clear();
2110 if (arg == "" || arg == "clone_size")
2111 ss.clone_size.clear();
2112 // Break all clone sizes by adding 1
2113 if (arg == "size") {
2114 for (map<snapid_t, uint64_t>::iterator i = ss.clone_size.begin();
2115 i != ss.clone_size.end(); ++i)
2116 ++(i->second);
2117 }
2118
2119 if (!dry_run) {
2120 bufferlist bl;
2121 ::encode(ss, bl);
2122 ObjectStore::Transaction t;
2123 t.setattr(coll, ghobj, SS_ATTR, bl);
2124 int r = store->apply_transaction(&osr, std::move(t));
2125 if (r < 0) {
2126 cerr << "Error setting snapset on : " << make_pair(coll, ghobj) << ", "
2127 << cpp_strerror(r) << std::endl;
2128 return r;
2129 }
2130 }
2131 return 0;
2132}
2133
2134vector<snapid_t>::iterator find(vector<snapid_t> &v, snapid_t clid)
2135{
2136 return std::find(v.begin(), v.end(), clid);
2137}
2138
2139map<snapid_t, interval_set<uint64_t> >::iterator
2140find(map<snapid_t, interval_set<uint64_t> > &m, snapid_t clid)
2141{
2142 return m.find(clid);
2143}
2144
2145map<snapid_t, uint64_t>::iterator find(map<snapid_t, uint64_t> &m,
2146 snapid_t clid)
2147{
2148 return m.find(clid);
2149}
2150
2151template<class T>
2152int remove_from(T &mv, string name, snapid_t cloneid, bool force)
2153{
2154 typename T::iterator i = find(mv, cloneid);
2155 if (i != mv.end()) {
2156 mv.erase(i);
2157 } else {
2158 cerr << "Clone " << cloneid << " doesn't exist in " << name;
2159 if (force) {
2160 cerr << " (ignored)" << std::endl;
2161 return 0;
2162 }
2163 cerr << std::endl;
2164 return -EINVAL;
2165 }
2166 return 0;
2167}
2168
2169int remove_clone(ObjectStore *store, coll_t coll, ghobject_t &ghobj, snapid_t cloneid, bool force,
2170 ObjectStore::Sequencer &osr)
2171{
2172 // XXX: Don't allow this if in a cache tier or former cache tier
2173 // bool allow_incomplete_clones() const {
2174 // return cache_mode != CACHEMODE_NONE || has_flag(FLAG_INCOMPLETE_CLONES);
2175
2176 SnapSet snapset;
2177 int ret = get_snapset(store, coll, ghobj, snapset);
2178 if (ret < 0)
2179 return ret;
2180
2181 // Derived from trim_object()
2182 // ...from snapset
2183 vector<snapid_t>::iterator p;
2184 for (p = snapset.clones.begin(); p != snapset.clones.end(); ++p)
2185 if (*p == cloneid)
2186 break;
2187 if (p == snapset.clones.end()) {
2188 cerr << "Clone " << cloneid << " not present";
2189 return -ENOENT;
2190 }
2191 if (p != snapset.clones.begin()) {
2192 // not the oldest... merge overlap into next older clone
2193 vector<snapid_t>::iterator n = p - 1;
2194 hobject_t prev_coid = ghobj.hobj;
2195 prev_coid.snap = *n;
2196 //bool adjust_prev_bytes = is_present_clone(prev_coid);
2197
2198 //if (adjust_prev_bytes)
2199 // ctx->delta_stats.num_bytes -= snapset.get_clone_bytes(*n);
2200
2201 snapset.clone_overlap[*n].intersection_of(
2202 snapset.clone_overlap[*p]);
2203
2204 //if (adjust_prev_bytes)
2205 // ctx->delta_stats.num_bytes += snapset.get_clone_bytes(*n);
2206 }
2207
2208 ret = remove_from(snapset.clones, "clones", cloneid, force);
2209 if (ret) return ret;
2210 ret = remove_from(snapset.clone_overlap, "clone_overlap", cloneid, force);
2211 if (ret) return ret;
2212 ret = remove_from(snapset.clone_size, "clone_size", cloneid, force);
2213 if (ret) return ret;
2214
2215 if (dry_run)
2216 return 0;
2217
2218 bufferlist bl;
2219 ::encode(snapset, bl);
2220 ObjectStore::Transaction t;
2221 t.setattr(coll, ghobj, SS_ATTR, bl);
2222 int r = store->apply_transaction(&osr, std::move(t));
2223 if (r < 0) {
2224 cerr << "Error setting snapset on : " << make_pair(coll, ghobj) << ", "
2225 << cpp_strerror(r) << std::endl;
2226 return r;
2227 }
2228 cout << "Removal of clone " << cloneid << " complete" << std::endl;
2229 cout << "Use pg repair after OSD restarted to correct stat information" << std::endl;
2230 return 0;
2231}
2232
2233int dup(string srcpath, ObjectStore *src, string dstpath, ObjectStore *dst)
2234{
2235 cout << "dup from " << src->get_type() << ": " << srcpath << "\n"
2236 << " to " << dst->get_type() << ": " << dstpath
2237 << std::endl;
2238 ObjectStore::Sequencer osr("dup");
2239 int num, i;
2240 vector<coll_t> collections;
2241 int r;
2242
2243 r = src->mount();
2244 if (r < 0) {
2245 cerr << "failed to mount src: " << cpp_strerror(r) << std::endl;
2246 return r;
2247 }
2248 r = dst->mount();
2249 if (r < 0) {
2250 cerr << "failed to mount dst: " << cpp_strerror(r) << std::endl;
2251 goto out_src;
2252 }
2253
2254 if (src->get_fsid() != dst->get_fsid()) {
2255 cerr << "src fsid " << src->get_fsid() << " != dest " << dst->get_fsid()
2256 << std::endl;
2257 goto out;
2258 }
2259 cout << "fsid " << src->get_fsid() << std::endl;
2260
2261 // make sure dst is empty
2262 r = dst->list_collections(collections);
2263 if (r < 0) {
2264 cerr << "error listing collections on dst: " << cpp_strerror(r) << std::endl;
2265 goto out;
2266 }
2267 if (!collections.empty()) {
2268 cerr << "destination store is not empty" << std::endl;
2269 goto out;
2270 }
2271
2272 r = src->list_collections(collections);
2273 if (r < 0) {
2274 cerr << "error listing collections on src: " << cpp_strerror(r) << std::endl;
2275 goto out;
2276 }
2277
2278 num = collections.size();
2279 cout << num << " collections" << std::endl;
2280 i = 1;
2281 for (auto cid : collections) {
2282 cout << i++ << "/" << num << " " << cid << std::endl;
2283 {
2284 ObjectStore::Transaction t;
2285 int bits = src->collection_bits(cid);
2286 if (bits < 0) {
2287 cerr << "cannot get bit count for collection " << cid << ": "
2288 << cpp_strerror(bits) << std::endl;
2289 goto out;
2290 }
2291 t.create_collection(cid, bits);
2292 dst->apply_transaction(&osr, std::move(t));
2293 }
2294
2295 ghobject_t pos;
2296 uint64_t n = 0;
2297 uint64_t bytes = 0, keys = 0;
2298 while (true) {
2299 vector<ghobject_t> ls;
2300 r = src->collection_list(cid, pos, ghobject_t::get_max(), 1000, &ls, &pos);
2301 if (r < 0) {
2302 cerr << "collection_list on " << cid << " from " << pos << " got: "
2303 << cpp_strerror(r) << std::endl;
2304 goto out;
2305 }
2306 if (ls.empty()) {
2307 break;
2308 }
2309
2310 for (auto& oid : ls) {
2311 //cout << " " << cid << " " << oid << std::endl;
2312 if (n % 100 == 0) {
2313 cout << " " << std::setw(16) << n << " objects, "
2314 << std::setw(16) << bytes << " bytes, "
2315 << std::setw(16) << keys << " keys"
2316 << std::setw(1) << "\r" << std::flush;
2317 }
2318 n++;
2319
2320 ObjectStore::Transaction t;
2321 t.touch(cid, oid);
2322
2323 map<string,bufferptr> attrs;
2324 src->getattrs(cid, oid, attrs);
2325 if (!attrs.empty()) {
2326 t.setattrs(cid, oid, attrs);
2327 }
2328
2329 bufferlist bl;
2330 src->read(cid, oid, 0, 0, bl);
2331 if (bl.length()) {
2332 t.write(cid, oid, 0, bl.length(), bl);
2333 bytes += bl.length();
2334 }
2335
2336 bufferlist header;
2337 map<string,bufferlist> omap;
2338 src->omap_get(cid, oid, &header, &omap);
2339 if (header.length()) {
2340 t.omap_setheader(cid, oid, header);
2341 ++keys;
2342 }
2343 if (!omap.empty()) {
2344 keys += omap.size();
2345 t.omap_setkeys(cid, oid, omap);
2346 }
2347
2348 dst->apply_transaction(&osr, std::move(t));
2349 }
2350 }
2351 cout << " " << std::setw(16) << n << " objects, "
2352 << std::setw(16) << bytes << " bytes, "
2353 << std::setw(16) << keys << " keys"
2354 << std::setw(1) << std::endl;
2355 }
2356
2357 // keyring
2358 cout << "keyring" << std::endl;
2359 {
2360 bufferlist bl;
2361 string s = srcpath + "/keyring";
2362 string err;
2363 r = bl.read_file(s.c_str(), &err);
2364 if (r < 0) {
2365 cerr << "failed to copy " << s << ": " << err << std::endl;
2366 } else {
2367 string d = dstpath + "/keyring";
2368 bl.write_file(d.c_str(), 0600);
2369 }
2370 }
2371
2372 // osd metadata
2373 cout << "duping osd metadata" << std::endl;
2374 {
2375 for (auto k : {"magic", "whoami", "ceph_fsid", "fsid"}) {
2376 string val;
2377 src->read_meta(k, &val);
2378 dst->write_meta(k, val);
2379 }
2380 }
2381
2382 dst->write_meta("ready", "ready");
2383
2384 cout << "done." << std::endl;
2385 r = 0;
2386 out:
2387 dst->umount();
2388 out_src:
2389 src->umount();
2390 return r;
2391}
2392
2393void usage(po::options_description &desc)
2394{
2395 cerr << std::endl;
2396 cerr << desc << std::endl;
2397 cerr << std::endl;
2398 cerr << "Positional syntax:" << std::endl;
2399 cerr << std::endl;
2400 cerr << "ceph-objectstore-tool ... <object> (get|set)-bytes [file]" << std::endl;
2401 cerr << "ceph-objectstore-tool ... <object> set-(attr|omap) <key> [file]" << std::endl;
2402 cerr << "ceph-objectstore-tool ... <object> (get|rm)-(attr|omap) <key>" << std::endl;
2403 cerr << "ceph-objectstore-tool ... <object> get-omaphdr" << std::endl;
2404 cerr << "ceph-objectstore-tool ... <object> set-omaphdr [file]" << std::endl;
2405 cerr << "ceph-objectstore-tool ... <object> list-attrs" << std::endl;
2406 cerr << "ceph-objectstore-tool ... <object> list-omap" << std::endl;
2407 cerr << "ceph-objectstore-tool ... <object> remove|removeall" << std::endl;
2408 cerr << "ceph-objectstore-tool ... <object> dump" << std::endl;
2409 cerr << "ceph-objectstore-tool ... <object> set-size" << std::endl;
2410 cerr << "ceph-objectstore-tool ... <object> remove-clone-metadata <cloneid>" << std::endl;
2411 cerr << std::endl;
2412 cerr << "<object> can be a JSON object description as displayed" << std::endl;
2413 cerr << "by --op list." << std::endl;
2414 cerr << "<object> can be an object name which will be looked up in all" << std::endl;
2415 cerr << "the OSD's PGs." << std::endl;
2416 cerr << "<object> can be the empty string ('') which with a provided pgid " << std::endl;
2417 cerr << "specifies the pgmeta object" << std::endl;
2418 cerr << std::endl;
2419 cerr << "The optional [file] argument will read stdin or write stdout" << std::endl;
2420 cerr << "if not specified or if '-' specified." << std::endl;
2421}
2422
2423bool ends_with(const string& check, const string& ending)
2424{
2425 return check.size() >= ending.size() && check.rfind(ending) == (check.size() - ending.size());
2426}
2427
2428// Based on FileStore::dump_journal(), set-up enough to only dump
2429int mydump_journal(Formatter *f, string journalpath, bool m_journal_dio)
2430{
2431 int r;
2432
2433 if (!journalpath.length())
2434 return -EINVAL;
2435
2436 FileJournal *journal = new FileJournal(g_ceph_context, uuid_d(), NULL, NULL,
2437 journalpath.c_str(), m_journal_dio);
2438 r = journal->_fdump(*f, false);
2439 delete journal;
2440 return r;
2441}
2442
2443int apply_layout_settings(ObjectStore *os, const OSDSuperblock &superblock,
2444 const string &pool_name, const spg_t &pgid, bool dry_run)
2445{
2446 int r = 0;
2447
2448 FileStore *fs = dynamic_cast<FileStore*>(os);
2449 if (!fs) {
2450 cerr << "Nothing to do for non-filestore backend" << std::endl;
2451 return 0; // making this return success makes testing easier
2452 }
2453
2454 OSDMap curmap;
2455 bufferlist bl;
2456 r = get_osdmap(os, superblock.current_epoch, curmap, bl);
2457 if (r) {
2458 cerr << "Can't find local OSDMap: " << cpp_strerror(r) << std::endl;
2459 return r;
2460 }
2461
2462 int64_t poolid = -1;
2463 if (pool_name.length()) {
2464 poolid = curmap.lookup_pg_pool_name(pool_name);
2465 if (poolid < 0) {
2466 cerr << "Couldn't find pool " << pool_name << ": " << cpp_strerror(poolid)
2467 << std::endl;
2468 return poolid;
2469 }
2470 }
2471
2472 vector<coll_t> collections, filtered_colls;
2473 r = os->list_collections(collections);
2474 if (r < 0) {
2475 cerr << "Error listing collections: " << cpp_strerror(r) << std::endl;
2476 return r;
2477 }
2478
2479 for (auto const &coll : collections) {
2480 spg_t coll_pgid;
2481 if (coll.is_pg(&coll_pgid) &&
2482 ((poolid >= 0 && coll_pgid.pool() == (uint64_t)poolid) ||
2483 coll_pgid == pgid)) {
2484 filtered_colls.push_back(coll);
2485 }
2486 }
2487
2488 size_t done = 0, total = filtered_colls.size();
2489 for (auto const &coll : filtered_colls) {
2490 if (dry_run) {
2491 cerr << "Would apply layout settings to " << coll << std::endl;
2492 } else {
2493 cerr << "Finished " << done << "/" << total << " collections" << "\r";
2494 r = fs->apply_layout_settings(coll);
2495 if (r < 0) {
2496 cerr << "Error applying layout settings to " << coll << std::endl;
2497 return r;
2498 }
2499 }
2500 ++done;
2501 }
2502
2503 cerr << "Finished " << total << "/" << total << " collections" << "\r" << std::endl;
2504 return r;
2505}
2506
2507int main(int argc, char **argv)
2508{
2509 string dpath, jpath, pgidstr, op, file, mountpoint, mon_store_path, object;
2510 string target_data_path, fsid;
2511 string objcmd, arg1, arg2, type, format, argnspace, pool;
2512 boost::optional<std::string> nspace;
2513 spg_t pgid;
2514 unsigned epoch = 0;
2515 ghobject_t ghobj;
2516 bool human_readable;
2517 bool force;
2518 Formatter *formatter;
2519 bool head;
2520
2521 po::options_description desc("Allowed options");
2522 desc.add_options()
2523 ("help", "produce help message")
2524 ("type", po::value<string>(&type),
2525 "Arg is one of [bluestore, filestore (default), memstore]")
2526 ("data-path", po::value<string>(&dpath),
2527 "path to object store, mandatory")
2528 ("journal-path", po::value<string>(&jpath),
2529 "path to journal, use if tool can't find it")
2530 ("pgid", po::value<string>(&pgidstr),
2531 "PG id, mandatory for info, log, remove, export, rm-past-intervals, mark-complete, and mandatory for apply-layout-settings if --pool is not specified")
2532 ("pool", po::value<string>(&pool),
2533 "Pool name, mandatory for apply-layout-settings if --pgid is not specified")
2534 ("op", po::value<string>(&op),
2535 "Arg is one of [info, log, remove, mkfs, fsck, fuse, dup, export, import, list, fix-lost, list-pgs, rm-past-intervals, dump-journal, dump-super, meta-list, "
2536 "get-osdmap, set-osdmap, get-inc-osdmap, set-inc-osdmap, mark-complete, apply-layout-settings, update-mon-db]")
2537 ("epoch", po::value<unsigned>(&epoch),
2538 "epoch# for get-osdmap and get-inc-osdmap, the current epoch in use if not specified")
2539 ("file", po::value<string>(&file),
2540 "path of file to export, import, get-osdmap, set-osdmap, get-inc-osdmap or set-inc-osdmap")
2541 ("mon-store-path", po::value<string>(&mon_store_path),
2542 "path of monstore to update-mon-db")
2543 ("fsid", po::value<string>(&fsid),
2544 "fsid for new store created by mkfs")
2545 ("target-data-path", po::value<string>(&target_data_path),
2546 "path of target object store (for --op dup)")
2547 ("mountpoint", po::value<string>(&mountpoint),
2548 "fuse mountpoint")
2549 ("format", po::value<string>(&format)->default_value("json-pretty"),
2550 "Output format which may be json, json-pretty, xml, xml-pretty")
2551 ("debug", "Enable diagnostic output to stderr")
2552 ("force", "Ignore some types of errors and proceed with operation - USE WITH CAUTION: CORRUPTION POSSIBLE NOW OR IN THE FUTURE")
2553 ("skip-journal-replay", "Disable journal replay")
2554 ("skip-mount-omap", "Disable mounting of omap")
2555 ("head", "Find head/snapdir when searching for objects by name")
2556 ("dry-run", "Don't modify the objectstore")
2557 ("namespace", po::value<string>(&argnspace), "Specify namespace when searching for objects")
2558 ;
2559
2560 po::options_description positional("Positional options");
2561 positional.add_options()
2562 ("object", po::value<string>(&object), "'' for pgmeta_oid, object name or ghobject in json")
2563 ("objcmd", po::value<string>(&objcmd), "command [(get|set)-bytes, (get|set|rm)-(attr|omap), (get|set)-omaphdr, list-attrs, list-omap, remove]")
2564 ("arg1", po::value<string>(&arg1), "arg1 based on cmd")
2565 ("arg2", po::value<string>(&arg2), "arg2 based on cmd")
2566 ("test-align", po::value<uint64_t>(&testalign)->default_value(0), "hidden align option for testing")
2567 ;
2568
2569 po::options_description all("All options");
2570 all.add(desc).add(positional);
2571
2572 po::positional_options_description pd;
2573 pd.add("object", 1).add("objcmd", 1).add("arg1", 1).add("arg2", 1);
2574
2575 vector<string> ceph_option_strings;
2576 po::variables_map vm;
2577 try {
2578 po::parsed_options parsed =
2579 po::command_line_parser(argc, argv).options(all).allow_unregistered().positional(pd).run();
2580 po::store( parsed, vm);
2581 po::notify(vm);
2582 ceph_option_strings = po::collect_unrecognized(parsed.options,
2583 po::include_positional);
2584 } catch(po::error &e) {
2585 std::cerr << e.what() << std::endl;
2586 return 1;
2587 }
2588
2589 if (vm.count("help")) {
2590 usage(all);
2591 return 1;
2592 }
2593
2594 if (!vm.count("debug")) {
2595 debug = false;
2596 } else {
2597 debug = true;
2598 }
2599
2600 if (!vm.count("force")) {
2601 force = false;
2602 } else {
2603 force = true;
2604 }
2605
2606 if (vm.count("namespace"))
2607 nspace = argnspace;
2608
2609 if (vm.count("dry-run"))
2610 dry_run = true;
2611 osflagbits_t flags = 0;
2612 if (dry_run || vm.count("skip-journal-replay"))
2613 flags |= SKIP_JOURNAL_REPLAY;
2614 if (vm.count("skip-mount-omap"))
2615 flags |= SKIP_MOUNT_OMAP;
2616 if (op == "update-mon-db")
2617 flags |= SKIP_JOURNAL_REPLAY;
2618 head = (vm.count("head") > 0);
2619
2620 vector<const char *> ceph_options;
2621 env_to_vec(ceph_options);
2622 ceph_options.reserve(ceph_options.size() + ceph_option_strings.size());
2623 for (vector<string>::iterator i = ceph_option_strings.begin();
2624 i != ceph_option_strings.end();
2625 ++i) {
2626 ceph_options.push_back(i->c_str());
2627 }
2628
2629 char fn[PATH_MAX];
2630 snprintf(fn, sizeof(fn), "%s/type", dpath.c_str());
2631 int fd = ::open(fn, O_RDONLY);
2632 if (fd >= 0) {
2633 bufferlist bl;
2634 bl.read_fd(fd, 64);
2635 if (bl.length()) {
2636 string dp_type = string(bl.c_str(), bl.length() - 1); // drop \n
2637 if (vm.count("type") && dp_type != "" && type != dp_type)
2638 cerr << "WARNING: Ignoring type \"" << type << "\" - found data-path type \""
2639 << dp_type << "\"" << std::endl;
2640 type = dp_type;
2641 //cout << "object store type is " << type << std::endl;
2642 }
2643 ::close(fd);
2644 }
2645 if (!vm.count("type") && type == "") {
2646 type = "filestore";
2647 }
2648 if (!vm.count("data-path") &&
2649 !(op == "dump-journal" && type == "filestore")) {
2650 cerr << "Must provide --data-path" << std::endl;
2651 usage(desc);
2652 return 1;
2653 }
2654 if (type == "filestore" && !vm.count("journal-path")) {
2655 jpath = dpath + "/journal";
2656 }
2657 if (!vm.count("op") && !vm.count("object")) {
2658 cerr << "Must provide --op or object command..." << std::endl;
2659 usage(desc);
2660 return 1;
2661 }
2662 if (op != "list" &&
2663 vm.count("op") && vm.count("object")) {
2664 cerr << "Can't specify both --op and object command syntax" << std::endl;
2665 usage(desc);
2666 return 1;
2667 }
2668 if (op == "apply-layout-settings" && !(vm.count("pool") ^ vm.count("pgid"))) {
2669 cerr << "apply-layout-settings requires either --pool or --pgid"
2670 << std::endl;
2671 usage(desc);
2672 return 1;
2673 }
2674 if (op != "list" && vm.count("object") && !vm.count("objcmd")) {
2675 cerr << "Invalid syntax, missing command" << std::endl;
2676 usage(desc);
2677 return 1;
2678 }
2679 if (op == "fuse" && mountpoint.length() == 0) {
2680 cerr << "Missing fuse mountpoint" << std::endl;
2681 usage(desc);
2682 return 1;
2683 }
2684 outistty = isatty(STDOUT_FILENO);
2685
2686 file_fd = fd_none;
2687 if ((op == "export" || op == "get-osdmap" || op == "get-inc-osdmap") && !dry_run) {
2688 if (!vm.count("file") || file == "-") {
2689 if (outistty) {
2690 cerr << "stdout is a tty and no --file filename specified" << std::endl;
2691 return 1;
2692 }
2693 file_fd = STDOUT_FILENO;
2694 } else {
2695 file_fd = open(file.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
2696 }
2697 } else if (op == "import" || op == "set-osdmap" || op == "set-inc-osdmap") {
2698 if (!vm.count("file") || file == "-") {
2699 if (isatty(STDIN_FILENO)) {
2700 cerr << "stdin is a tty and no --file filename specified" << std::endl;
2701 return 1;
2702 }
2703 file_fd = STDIN_FILENO;
2704 } else {
2705 file_fd = open(file.c_str(), O_RDONLY);
2706 }
2707 }
2708
2709 ObjectStoreTool tool = ObjectStoreTool(file_fd, dry_run);
2710
2711 if (vm.count("file") && file_fd == fd_none && !dry_run) {
2712 cerr << "--file option only applies to import, export, "
2713 << "get-osdmap, set-osdmap, get-inc-osdmap or set-inc-osdmap" << std::endl;
2714 return 1;
2715 }
2716
2717 if (file_fd != fd_none && file_fd < 0) {
2718 string err = string("file: ") + file;
2719 perror(err.c_str());
2720 return 1;
2721 }
2722
2723 auto cct = global_init(
2724 NULL, ceph_options, CEPH_ENTITY_TYPE_OSD,
2725 CODE_ENVIRONMENT_UTILITY_NODOUT, 0);
2726 //CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
2727 common_init_finish(g_ceph_context);
2728 g_conf = g_ceph_context->_conf;
2729 if (debug) {
2730 g_conf->set_val_or_die("log_to_stderr", "true");
2731 g_conf->set_val_or_die("err_to_stderr", "true");
2732 }
2733 g_conf->apply_changes(NULL);
2734
2735 // Special list handling. Treating pretty_format as human readable,
2736 // with one object per line and not an enclosing array.
2737 human_readable = ends_with(format, "-pretty");
2738 if ((op == "list" || op == "meta-list") && human_readable) {
2739 // Remove -pretty from end of format which we know is there
2740 format = format.substr(0, format.size() - strlen("-pretty"));
2741 }
2742
2743 formatter = Formatter::create(format);
2744 if (formatter == NULL) {
2745 cerr << "unrecognized format: " << format << std::endl;
2746 return 1;
2747 }
2748
2749 // Special handling for filestore journal, so we can dump it without mounting
2750 if (op == "dump-journal" && type == "filestore") {
2751 int ret = mydump_journal(formatter, jpath, g_conf->journal_dio);
2752 if (ret < 0) {
2753 cerr << "journal-path: " << jpath << ": "
2754 << cpp_strerror(ret) << std::endl;
2755 return 1;
2756 }
2757 formatter->flush(cout);
2758 return 0;
2759 }
2760
2761 //Verify that data-path really exists
2762 struct stat st;
2763 if (::stat(dpath.c_str(), &st) == -1) {
2764 string err = string("data-path: ") + dpath;
2765 perror(err.c_str());
2766 return 1;
2767 }
2768
2769 if (pgidstr.length() && !pgid.parse(pgidstr.c_str())) {
2770 cerr << "Invalid pgid '" << pgidstr << "' specified" << std::endl;
2771 return 1;
2772 }
2773
2774 ObjectStore *fs = ObjectStore::create(g_ceph_context, type, dpath, jpath, flags);
2775 if (fs == NULL) {
2776 cerr << "Unable to create store of type " << type << std::endl;
2777 return 1;
2778 }
2779
2780 if (op == "fsck" || op == "fsck-deep") {
2781 int r = fs->fsck(op == "fsck-deep");
2782 if (r < 0) {
2783 cerr << "fsck failed: " << cpp_strerror(r) << std::endl;
2784 return 1;
2785 }
2786 if (r > 0) {
2787 cerr << "fsck found " << r << " errors" << std::endl;
2788 return 1;
2789 }
2790 cout << "fsck found no errors" << std::endl;
2791 return 0;
2792 }
2793 if (op == "mkfs") {
2794 if (fsid.length()) {
2795 uuid_d f;
2796 bool r = f.parse(fsid.c_str());
2797 if (!r) {
2798 cerr << "failed to parse uuid '" << fsid << "'" << std::endl;
2799 return 1;
2800 }
2801 fs->set_fsid(f);
2802 }
2803 int r = fs->mkfs();
2804 if (r < 0) {
2805 cerr << "fsck failed: " << cpp_strerror(r) << std::endl;
2806 return 1;
2807 }
2808 return 0;
2809 }
2810 if (op == "dup") {
2811 string target_type;
2812 char fn[PATH_MAX];
2813 snprintf(fn, sizeof(fn), "%s/type", target_data_path.c_str());
2814 int fd = ::open(fn, O_RDONLY);
2815 if (fd < 0) {
2816 cerr << "Unable to open " << target_data_path << "/type" << std::endl;
2817 exit(1);
2818 }
2819 bufferlist bl;
2820 bl.read_fd(fd, 64);
2821 if (bl.length()) {
2822 target_type = string(bl.c_str(), bl.length() - 1); // drop \n
2823 }
2824 ::close(fd);
2825 ObjectStore *targetfs = ObjectStore::create(
2826 g_ceph_context, target_type,
2827 target_data_path, "", 0);
2828 if (targetfs == NULL) {
2829 cerr << "Unable to open store of type " << target_type << std::endl;
2830 return 1;
2831 }
2832 int r = dup(dpath, fs, target_data_path, targetfs);
2833 if (r < 0) {
2834 cerr << "dup failed: " << cpp_strerror(r) << std::endl;
2835 return 1;
2836 }
2837 return 0;
2838 }
2839
2840 ObjectStore::Sequencer *osr = new ObjectStore::Sequencer(__func__);
2841 int ret = fs->mount();
2842 if (ret < 0) {
2843 if (ret == -EBUSY) {
2844 cerr << "OSD has the store locked" << std::endl;
2845 } else {
2846 cerr << "Mount failed with '" << cpp_strerror(ret) << "'" << std::endl;
2847 }
2848 return 1;
2849 }
2850
2851 if (op == "fuse") {
2852#ifdef HAVE_LIBFUSE
2853 FuseStore fuse(fs, mountpoint);
2854 cout << "mounting fuse at " << mountpoint << " ..." << std::endl;
2855 int r = fuse.main();
2856 if (r < 0) {
2857 cerr << "failed to mount fuse: " << cpp_strerror(r) << std::endl;
2858 return 1;
2859 }
2860#else
2861 cerr << "fuse support not enabled" << std::endl;
2862#endif
2863 return 0;
2864 }
2865
2866 vector<coll_t> ls;
2867 vector<coll_t>::iterator it;
2868 CompatSet supported;
2869
2870#ifdef INTERNAL_TEST
2871 supported = get_test_compat_set();
2872#else
2873 supported = OSD::get_osd_compat_set();
2874#endif
2875
2876 bufferlist bl;
2877 OSDSuperblock superblock;
2878 bufferlist::iterator p;
2879 ret = fs->read(coll_t::meta(), OSD_SUPERBLOCK_GOBJECT, 0, 0, bl);
2880 if (ret < 0) {
2881 cerr << "Failure to read OSD superblock: " << cpp_strerror(ret) << std::endl;
2882 goto out;
2883 }
2884
2885 p = bl.begin();
2886 ::decode(superblock, p);
2887
2888 if (debug) {
2889 cerr << "Cluster fsid=" << superblock.cluster_fsid << std::endl;
2890 }
2891
2892 if (debug) {
2893 cerr << "Supported features: " << supported << std::endl;
2894 cerr << "On-disk features: " << superblock.compat_features << std::endl;
2895 }
2896 if (supported.compare(superblock.compat_features) == -1) {
2897 CompatSet unsupported = supported.unsupported(superblock.compat_features);
2898 cerr << "On-disk OSD incompatible features set "
2899 << unsupported << std::endl;
2900 ret = -EINVAL;
2901 goto out;
2902 }
2903
2904 if (op == "apply-layout-settings") {
2905 ret = apply_layout_settings(fs, superblock, pool, pgid, dry_run);
2906 goto out;
2907 }
2908
2909 if (op != "list" && vm.count("object")) {
2910 // Special case: Create pgmeta_oid if empty string specified
2911 // This can't conflict with any actual object names.
2912 if (object == "") {
2913 ghobj = pgid.make_pgmeta_oid();
2914 } else {
2915 json_spirit::Value v;
2916 try {
2917 if (!json_spirit::read(object, v) ||
2918 (v.type() != json_spirit::array_type && v.type() != json_spirit::obj_type)) {
2919 // Special: Need head/snapdir so set even if user didn't specify
2920 if (vm.count("objcmd") && (objcmd == "remove-clone-metadata"))
2921 head = true;
2922 lookup_ghobject lookup(object, nspace, head);
2923 if (action_on_all_objects(fs, lookup, debug)) {
2924 throw std::runtime_error("Internal error");
2925 } else {
2926 if (lookup.size() != 1) {
2927 stringstream ss;
2928 if (lookup.size() == 0)
2929 ss << "No object id '" << object << "' found or invalid JSON specified";
2930 else
2931 ss << "Found " << lookup.size() << " objects with id '" << object
2932 << "', please use a JSON spec from --op list instead";
2933 throw std::runtime_error(ss.str());
2934 }
2935 pair<coll_t, ghobject_t> found = lookup.pop();
2936 pgidstr = found.first.to_str();
2937 pgid.parse(pgidstr.c_str());
2938 ghobj = found.second;
2939 }
2940 } else {
2941 stringstream ss;
2942 if (pgidstr.length() == 0 && v.type() != json_spirit::array_type) {
2943 ss << "Without --pgid the object '" << object
2944 << "' must be a JSON array";
2945 throw std::runtime_error(ss.str());
2946 }
2947 if (v.type() == json_spirit::array_type) {
2948 json_spirit::Array array = v.get_array();
2949 if (array.size() != 2) {
2950 ss << "Object '" << object
2951 << "' must be a JSON array with 2 elements";
2952 throw std::runtime_error(ss.str());
2953 }
2954 vector<json_spirit::Value>::iterator i = array.begin();
2955 assert(i != array.end());
2956 if (i->type() != json_spirit::str_type) {
2957 ss << "Object '" << object
2958 << "' must be a JSON array with the first element a string";
2959 throw std::runtime_error(ss.str());
2960 }
2961 string object_pgidstr = i->get_str();
2962 if (object_pgidstr != "meta") {
2963 spg_t object_pgid;
2964 object_pgid.parse(object_pgidstr.c_str());
2965 if (pgidstr.length() > 0) {
2966 if (object_pgid != pgid) {
2967 ss << "object '" << object
2968 << "' has a pgid different from the --pgid="
2969 << pgidstr << " option";
2970 throw std::runtime_error(ss.str());
2971 }
2972 } else {
2973 pgidstr = object_pgidstr;
2974 pgid = object_pgid;
2975 }
2976 } else {
2977 pgidstr = object_pgidstr;
2978 }
2979 ++i;
2980 v = *i;
2981 }
2982 try {
2983 ghobj.decode(v);
2984 } catch (std::runtime_error& e) {
2985 ss << "Decode object JSON error: " << e.what();
2986 throw std::runtime_error(ss.str());
2987 }
2988 if (pgidstr != "meta" && (uint64_t)pgid.pgid.m_pool != (uint64_t)ghobj.hobj.pool) {
2989 cerr << "Object pool and pgid pool don't match" << std::endl;
2990 ret = 1;
2991 goto out;
2992 }
2993 }
2994 } catch (std::runtime_error& e) {
2995 cerr << e.what() << std::endl;
2996 ret = 1;
2997 goto out;
2998 }
2999 }
3000 }
3001
3002 // The ops which require --pgid option are checked here and
3003 // mentioned in the usage for --pgid.
3004 if ((op == "info" || op == "log" || op == "remove" || op == "export"
3005 || op == "rm-past-intervals" || op == "mark-complete") &&
3006 pgidstr.length() == 0) {
3007 cerr << "Must provide pgid" << std::endl;
3008 usage(desc);
3009 ret = 1;
3010 goto out;
3011 }
3012
3013 if (op == "import") {
3014
3015 try {
3016 ret = tool.do_import(fs, superblock, force, pgidstr, *osr);
3017 }
3018 catch (const buffer::error &e) {
3019 cerr << "do_import threw exception error " << e.what() << std::endl;
3020 ret = -EFAULT;
3021 }
3022 if (ret == -EFAULT) {
3023 cerr << "Corrupt input for import" << std::endl;
3024 }
3025 if (ret == 0)
3026 cout << "Import successful" << std::endl;
3027 goto out;
3028 } else if (op == "dump-journal-mount") {
3029 // Undocumented feature to dump journal with mounted fs
3030 // This doesn't support the format option, but it uses the
3031 // ObjectStore::dump_journal() and mounts to get replay to run.
3032 ret = fs->dump_journal(cout);
3033 if (ret) {
3034 if (ret == -EOPNOTSUPP) {
3035 cerr << "Object store type \"" << type << "\" doesn't support journal dump" << std::endl;
3036 } else {
3037 cerr << "Journal dump failed with error " << cpp_strerror(ret) << std::endl;
3038 }
3039 }
3040 goto out;
3041 } else if (op == "get-osdmap") {
3042 bufferlist bl;
3043 OSDMap osdmap;
3044 if (epoch == 0) {
3045 epoch = superblock.current_epoch;
3046 }
3047 ret = get_osdmap(fs, epoch, osdmap, bl);
3048 if (ret) {
3049 cerr << "Failed to get osdmap#" << epoch << ": "
3050 << cpp_strerror(ret) << std::endl;
3051 goto out;
3052 }
3053 ret = bl.write_fd(file_fd);
3054 if (ret) {
3055 cerr << "Failed to write to " << file << ": " << cpp_strerror(ret) << std::endl;
3056 } else {
3057 cout << "osdmap#" << epoch << " exported." << std::endl;
3058 }
3059 goto out;
3060 } else if (op == "set-osdmap") {
3061 bufferlist bl;
3062 ret = get_fd_data(file_fd, bl);
3063 if (ret < 0) {
3064 cerr << "Failed to read osdmap " << cpp_strerror(ret) << std::endl;
3065 } else {
3066 ret = set_osdmap(fs, epoch, bl, force, *osr);
3067 }
3068 goto out;
3069 } else if (op == "get-inc-osdmap") {
3070 bufferlist bl;
3071 if (epoch == 0) {
3072 epoch = superblock.current_epoch;
3073 }
3074 ret = get_inc_osdmap(fs, epoch, bl);
3075 if (ret < 0) {
3076 cerr << "Failed to get incremental osdmap# " << epoch << ": "
3077 << cpp_strerror(ret) << std::endl;
3078 goto out;
3079 }
3080 ret = bl.write_fd(file_fd);
3081 if (ret) {
3082 cerr << "Failed to write to " << file << ": " << cpp_strerror(ret) << std::endl;
3083 } else {
3084 cout << "inc-osdmap#" << epoch << " exported." << std::endl;
3085 }
3086 goto out;
3087 } else if (op == "set-inc-osdmap") {
3088 bufferlist bl;
3089 ret = get_fd_data(file_fd, bl);
3090 if (ret < 0) {
3091 cerr << "Failed to read incremental osdmap " << cpp_strerror(ret) << std::endl;
3092 goto out;
3093 } else {
3094 ret = set_inc_osdmap(fs, epoch, bl, force, *osr);
3095 }
3096 goto out;
3097 } else if (op == "update-mon-db") {
3098 if (!vm.count("mon-store-path")) {
3099 cerr << "Please specify the path to monitor db to update" << std::endl;
3100 ret = -EINVAL;
3101 } else {
3102 ret = update_mon_db(*fs, superblock, dpath + "/keyring", mon_store_path);
3103 }
3104 goto out;
3105 }
3106
3107 log_oid = OSD::make_pg_log_oid(pgid);
3108 biginfo_oid = OSD::make_pg_biginfo_oid(pgid);
3109
3110 if (op == "remove") {
3111 ret = initiate_new_remove_pg(fs, pgid, *osr);
3112 if (ret < 0) {
3113 cerr << "PG '" << pgid << "' not found" << std::endl;
3114 goto out;
3115 }
3116 cout << "Remove successful" << std::endl;
3117 goto out;
3118 }
3119
3120 if (op == "fix-lost") {
3121 boost::scoped_ptr<action_on_object_t> action;
3122 action.reset(new do_fix_lost(osr));
3123 if (pgidstr.length())
3124 ret = action_on_all_objects_in_exact_pg(fs, coll_t(pgid), *action, debug);
3125 else
3126 ret = action_on_all_objects(fs, *action, debug);
3127 goto out;
3128 }
3129
3130 if (op == "list") {
3131 ret = do_list(fs, pgidstr, object, nspace, formatter, debug,
3132 human_readable, head);
3133 if (ret < 0) {
3134 cerr << "do_list failed: " << cpp_strerror(ret) << std::endl;
3135 }
3136 goto out;
3137 }
3138
3139 if (op == "dump-super") {
3140 formatter->open_object_section("superblock");
3141 superblock.dump(formatter);
3142 formatter->close_section();
3143 formatter->flush(cout);
3144 cout << std::endl;
3145 goto out;
3146 }
3147
3148 if (op == "meta-list") {
3149 ret = do_meta(fs, object, formatter, debug, human_readable);
3150 if (ret < 0) {
3151 cerr << "do_meta failed: " << cpp_strerror(ret) << std::endl;
3152 }
3153 goto out;
3154 }
3155
3156 ret = fs->list_collections(ls);
3157 if (ret < 0) {
3158 cerr << "failed to list pgs: " << cpp_strerror(ret) << std::endl;
3159 goto out;
3160 }
3161
3162 if (debug && op == "list-pgs")
3163 cout << "Performing list-pgs operation" << std::endl;
3164
3165 // Find pg
3166 for (it = ls.begin(); it != ls.end(); ++it) {
3167 spg_t tmppgid;
3168
3169 if (pgidstr == "meta") {
3170 if (it->to_str() == "meta")
3171 break;
3172 else
3173 continue;
3174 }
3175
3176 if (!it->is_pg(&tmppgid)) {
3177 continue;
3178 }
3179
3180 if (it->is_temp(&tmppgid)) {
3181 continue;
3182 }
3183
3184 if (op != "list-pgs" && tmppgid != pgid) {
3185 continue;
3186 }
3187
3188 if (op != "list-pgs") {
3189 //Found!
3190 break;
3191 }
3192
3193 cout << tmppgid << std::endl;
3194 }
3195
3196 if (op == "list-pgs") {
3197 ret = 0;
3198 goto out;
3199 }
3200
3201 // If not an object command nor any of the ops handled below, then output this usage
3202 // before complaining about a bad pgid
3203 if (!vm.count("objcmd") && op != "export" && op != "info" && op != "log" && op != "rm-past-intervals" && op != "mark-complete") {
3204 cerr << "Must provide --op (info, log, remove, mkfs, fsck, export, import, list, fix-lost, list-pgs, rm-past-intervals, dump-journal, dump-super, meta-list, "
3205 "get-osdmap, set-osdmap, get-inc-osdmap, set-inc-osdmap, mark-complete)"
3206 << std::endl;
3207 usage(desc);
3208 ret = 1;
3209 goto out;
3210 }
3211 epoch_t map_epoch;
3212// The following code for export, info, log require omap or !skip-mount-omap
3213 if (it != ls.end()) {
3214
3215 coll_t coll = *it;
3216
3217 if (vm.count("objcmd")) {
3218 ret = 0;
3219 if (objcmd == "remove" || objcmd == "removeall") {
3220 bool all = (objcmd == "removeall");
3221 ret = do_remove_object(fs, coll, ghobj, all, force, *osr);
3222 goto out;
3223 } else if (objcmd == "list-attrs") {
3224 ret = do_list_attrs(fs, coll, ghobj);
3225 goto out;
3226 } else if (objcmd == "list-omap") {
3227 ret = do_list_omap(fs, coll, ghobj);
3228 goto out;
3229 } else if (objcmd == "get-bytes" || objcmd == "set-bytes") {
3230 if (objcmd == "get-bytes") {
3231 int fd;
3232 if (vm.count("arg1") == 0 || arg1 == "-") {
3233 fd = STDOUT_FILENO;
3234 } else {
3235 fd = open(arg1.c_str(), O_WRONLY|O_TRUNC|O_CREAT|O_EXCL|O_LARGEFILE, 0666);
3236 if (fd == -1) {
3237 cerr << "open " << arg1 << " " << cpp_strerror(errno) << std::endl;
3238 ret = 1;
3239 goto out;
3240 }
3241 }
3242 ret = do_get_bytes(fs, coll, ghobj, fd);
3243 if (fd != STDOUT_FILENO)
3244 close(fd);
3245 } else {
3246 int fd;
3247 if (vm.count("arg1") == 0 || arg1 == "-") {
3248 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
3249 if (isatty(STDIN_FILENO)) {
3250 cerr << "stdin is a tty and no file specified" << std::endl;
3251 ret = 1;
3252 goto out;
3253 }
3254 fd = STDIN_FILENO;
3255 } else {
3256 fd = open(arg1.c_str(), O_RDONLY|O_LARGEFILE, 0666);
3257 if (fd == -1) {
3258 cerr << "open " << arg1 << " " << cpp_strerror(errno) << std::endl;
3259 ret = 1;
3260 goto out;
3261 }
3262 }
3263 ret = do_set_bytes(fs, coll, ghobj, fd, *osr);
3264 if (fd != STDIN_FILENO)
3265 close(fd);
3266 }
3267 goto out;
3268 } else if (objcmd == "get-attr") {
3269 if (vm.count("arg1") == 0) {
3270 usage(desc);
3271 ret = 1;
3272 goto out;
3273 }
3274 ret = do_get_attr(fs, coll, ghobj, arg1);
3275 goto out;
3276 } else if (objcmd == "set-attr") {
3277 if (vm.count("arg1") == 0) {
3278 usage(desc);
3279 ret = 1;
3280 }
3281
3282 int fd;
3283 if (vm.count("arg2") == 0 || arg2 == "-") {
3284 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
3285 if (isatty(STDIN_FILENO)) {
3286 cerr << "stdin is a tty and no file specified" << std::endl;
3287 ret = 1;
3288 goto out;
3289 }
3290 fd = STDIN_FILENO;
3291 } else {
3292 fd = open(arg2.c_str(), O_RDONLY|O_LARGEFILE, 0666);
3293 if (fd == -1) {
3294 cerr << "open " << arg2 << " " << cpp_strerror(errno) << std::endl;
3295 ret = 1;
3296 goto out;
3297 }
3298 }
3299 ret = do_set_attr(fs, coll, ghobj, arg1, fd, *osr);
3300 if (fd != STDIN_FILENO)
3301 close(fd);
3302 goto out;
3303 } else if (objcmd == "rm-attr") {
3304 if (vm.count("arg1") == 0) {
3305 usage(desc);
3306 ret = 1;
3307 goto out;
3308 }
3309 ret = do_rm_attr(fs, coll, ghobj, arg1, *osr);
3310 goto out;
3311 } else if (objcmd == "get-omap") {
3312 if (vm.count("arg1") == 0) {
3313 usage(desc);
3314 ret = 1;
3315 goto out;
3316 }
3317 ret = do_get_omap(fs, coll, ghobj, arg1);
3318 goto out;
3319 } else if (objcmd == "set-omap") {
3320 if (vm.count("arg1") == 0) {
3321 usage(desc);
3322 ret = 1;
3323 goto out;
3324 }
3325 int fd;
3326 if (vm.count("arg2") == 0 || arg2 == "-") {
3327 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
3328 if (isatty(STDIN_FILENO)) {
3329 cerr << "stdin is a tty and no file specified" << std::endl;
3330 ret = 1;
3331 goto out;
3332 }
3333 fd = STDIN_FILENO;
3334 } else {
3335 fd = open(arg2.c_str(), O_RDONLY|O_LARGEFILE, 0666);
3336 if (fd == -1) {
3337 cerr << "open " << arg2 << " " << cpp_strerror(errno) << std::endl;
3338 ret = 1;
3339 goto out;
3340 }
3341 }
3342 ret = do_set_omap(fs, coll, ghobj, arg1, fd, *osr);
3343 if (fd != STDIN_FILENO)
3344 close(fd);
3345 goto out;
3346 } else if (objcmd == "rm-omap") {
3347 if (vm.count("arg1") == 0) {
3348 usage(desc);
3349 ret = 1;
3350 goto out;
3351 }
3352 ret = do_rm_omap(fs, coll, ghobj, arg1, *osr);
3353 goto out;
3354 } else if (objcmd == "get-omaphdr") {
3355 if (vm.count("arg1")) {
3356 usage(desc);
3357 ret = 1;
3358 goto out;
3359 }
3360 ret = do_get_omaphdr(fs, coll, ghobj);
3361 goto out;
3362 } else if (objcmd == "set-omaphdr") {
3363 // Extra arg
3364 if (vm.count("arg2")) {
3365 usage(desc);
3366 ret = 1;
3367 goto out;
3368 }
3369 int fd;
3370 if (vm.count("arg1") == 0 || arg1 == "-") {
3371 // Since read_fd() doesn't handle ^D from a tty stdin, don't allow it.
3372 if (isatty(STDIN_FILENO)) {
3373 cerr << "stdin is a tty and no file specified" << std::endl;
3374 ret = 1;
3375 goto out;
3376 }
3377 fd = STDIN_FILENO;
3378 } else {
3379 fd = open(arg1.c_str(), O_RDONLY|O_LARGEFILE, 0666);
3380 if (fd == -1) {
3381 cerr << "open " << arg1 << " " << cpp_strerror(errno) << std::endl;
3382 ret = 1;
3383 goto out;
3384 }
3385 }
3386 ret = do_set_omaphdr(fs, coll, ghobj, fd, *osr);
3387 if (fd != STDIN_FILENO)
3388 close(fd);
3389 goto out;
3390 } else if (objcmd == "dump") {
3391 // There should not be any other arguments
3392 if (vm.count("arg1") || vm.count("arg2")) {
3393 usage(desc);
3394 ret = 1;
3395 goto out;
3396 }
3397 ret = print_obj_info(fs, coll, ghobj, formatter);
3398 goto out;
3399 } else if (objcmd == "set-size") {
3400 // Extra arg
3401 if (vm.count("arg1") == 0 || vm.count("arg2")) {
3402 usage(desc);
3403 ret = 1;
3404 goto out;
3405 }
3406 if (arg1.length() == 0 || !isdigit(arg1.c_str()[0])) {
3407 cerr << "Invalid size '" << arg1 << "' specified" << std::endl;
3408 ret = 1;
3409 goto out;
3410 }
3411 uint64_t size = atoll(arg1.c_str());
3412 ret = set_size(fs, coll, ghobj, size, formatter, *osr);
3413 goto out;
3414 } else if (objcmd == "clear-snapset") {
3415 // UNDOCUMENTED: For testing zap SnapSet
3416 // IGNORE extra args since not in usage anyway
3417 if (!ghobj.hobj.has_snapset()) {
3418 cerr << "'" << objcmd << "' requires a head or snapdir object" << std::endl;
3419 ret = 1;
3420 goto out;
3421 }
3422 ret = clear_snapset(fs, coll, ghobj, arg1, *osr);
3423 goto out;
3424 } else if (objcmd == "remove-clone-metadata") {
3425 // Extra arg
3426 if (vm.count("arg1") == 0 || vm.count("arg2")) {
3427 usage(desc);
3428 ret = 1;
3429 goto out;
3430 }
3431 if (!ghobj.hobj.has_snapset()) {
3432 cerr << "'" << objcmd << "' requires a head or snapdir object" << std::endl;
3433 ret = 1;
3434 goto out;
3435 }
3436 if (arg1.length() == 0 || !isdigit(arg1.c_str()[0])) {
3437 cerr << "Invalid cloneid '" << arg1 << "' specified" << std::endl;
3438 ret = 1;
3439 goto out;
3440 }
3441 snapid_t cloneid = atoi(arg1.c_str());
3442 ret = remove_clone(fs, coll, ghobj, cloneid, force, *osr);
3443 goto out;
3444 }
3445 cerr << "Unknown object command '" << objcmd << "'" << std::endl;
3446 usage(desc);
3447 ret = 1;
3448 goto out;
3449 }
3450
3451 bufferlist bl;
3452 map_epoch = 0;
3453 ret = PG::peek_map_epoch(fs, pgid, &map_epoch, &bl);
3454 if (ret < 0)
3455 cerr << "peek_map_epoch reports error" << std::endl;
3456 if (debug)
3457 cerr << "map_epoch " << map_epoch << std::endl;
3458
3459 pg_info_t info(pgid);
3460 PastIntervals past_intervals;
3461 __u8 struct_ver;
3462 ret = PG::read_info(fs, pgid, coll, bl, info, past_intervals,
3463 struct_ver);
3464 if (ret < 0) {
3465 cerr << "read_info error " << cpp_strerror(ret) << std::endl;
3466 goto out;
3467 }
3468 if (struct_ver < PG::compat_struct_v) {
3469 cerr << "PG is too old to upgrade, use older Ceph version" << std::endl;
3470 ret = -EFAULT;
3471 goto out;
3472 }
3473 if (debug)
3474 cerr << "struct_v " << (int)struct_ver << std::endl;
3475
3476 if (op == "export") {
3477 ret = tool.do_export(fs, coll, pgid, info, map_epoch, struct_ver, superblock, past_intervals);
3478 if (ret == 0)
3479 cerr << "Export successful" << std::endl;
3480 } else if (op == "info") {
3481 formatter->open_object_section("info");
3482 info.dump(formatter);
3483 formatter->close_section();
3484 formatter->flush(cout);
3485 cout << std::endl;
3486 } else if (op == "log") {
3487 PGLog::IndexedLog log;
3488 pg_missing_t missing;
3489 ret = get_log(fs, struct_ver, coll, pgid, info, log, missing);
3490 if (ret < 0)
3491 goto out;
3492
3493 dump_log(formatter, cout, log, missing);
3494 } else if (op == "rm-past-intervals") {
3495 ObjectStore::Transaction tran;
3496 ObjectStore::Transaction *t = &tran;
3497
3498 if (struct_ver < PG::compat_struct_v) {
3499 cerr << "Can't remove past-intervals, version mismatch " << (int)struct_ver
3500 << " (pg) < compat " << (int)PG::compat_struct_v << " (tool)"
3501 << std::endl;
3502 ret = -EFAULT;
3503 goto out;
3504 }
3505
3506 cout << "Remove past-intervals " << past_intervals << std::endl;
3507
3508 past_intervals.clear();
3509 if (dry_run) {
3510 ret = 0;
3511 goto out;
3512 }
3513 ret = write_info(*t, map_epoch, info, past_intervals);
3514
3515 if (ret == 0) {
3516 fs->apply_transaction(osr, std::move(*t));
3517 cout << "Removal succeeded" << std::endl;
3518 }
3519 } else if (op == "mark-complete") {
3520 ObjectStore::Transaction tran;
3521 ObjectStore::Transaction *t = &tran;
3522
3523 if (struct_ver < PG::compat_struct_v) {
3524 cerr << "Can't mark-complete, version mismatch " << (int)struct_ver
3525 << " (pg) < compat " << (int)PG::compat_struct_v << " (tool)"
3526 << std::endl;
3527 ret = 1;
3528 goto out;
3529 }
3530
3531 cout << "Marking complete " << std::endl;
3532
3533 info.last_update = eversion_t(superblock.current_epoch, info.last_update.version + 1);
3534 info.last_backfill = hobject_t::get_max();
3535 info.last_epoch_started = superblock.current_epoch;
3536 info.history.last_epoch_started = superblock.current_epoch;
3537 info.history.last_epoch_clean = superblock.current_epoch;
3538 past_intervals.clear();
3539
3540 if (!dry_run) {
3541 ret = write_info(*t, map_epoch, info, past_intervals);
3542 if (ret != 0)
3543 goto out;
3544 fs->apply_transaction(osr, std::move(*t));
3545 }
3546 cout << "Marking complete succeeded" << std::endl;
3547 } else {
3548 assert(!"Should have already checked for valid --op");
3549 }
3550 } else {
3551 cerr << "PG '" << pgid << "' not found" << std::endl;
3552 ret = -ENOENT;
3553 }
3554
3555out:
3556 int r = fs->umount();
3557 delete osr;
3558 if (r < 0) {
3559 cerr << "umount failed: " << cpp_strerror(r) << std::endl;
3560 // If no previous error, then use umount() error
3561 if (ret == 0)
3562 ret = r;
3563 }
3564
3565 if (dry_run) {
3566 // Export output can go to stdout, so put this message on stderr
3567 if (op == "export")
3568 cerr << "dry-run: Nothing changed" << std::endl;
3569 else
3570 cout << "dry-run: Nothing changed" << std::endl;
3571 }
3572
3573 if (ret < 0)
3574 ret = 1;
3575 return ret;
3576}