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