]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rados/rados.cc
update sources to v12.2.4
[ceph.git] / ceph / src / tools / rados / rados.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) 2004-2006 Sage Weil <sage@newdream.net>
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 "include/types.h"
16
17 #include "include/rados/librados.hpp"
18 #include "include/rados/rados_types.hpp"
19 #include "include/radosstriper/libradosstriper.hpp"
20 using namespace libradosstriper;
21
22 #include "common/config.h"
23 #include "common/ceph_argparse.h"
24 #include "global/global_init.h"
25 #include "common/Cond.h"
26 #include "common/debug.h"
27 #include "common/errno.h"
28 #include "common/Formatter.h"
29 #include "common/obj_bencher.h"
30 #include "common/TextTable.h"
31 #include "include/stringify.h"
32 #include "mds/inode_backtrace.h"
33 #include "auth/Crypto.h"
34 #include <iostream>
35 #include <fstream>
36
37 #include <stdlib.h>
38 #include <time.h>
39 #include <sstream>
40 #include <errno.h>
41 #include <dirent.h>
42 #include <stdexcept>
43 #include <climits>
44 #include <locale>
45 #include <memory>
46
47 #include "cls/lock/cls_lock_client.h"
48 #include "include/compat.h"
49 #include "include/util.h"
50 #include "common/hobject.h"
51
52 #include "PoolDump.h"
53 #include "RadosImport.h"
54
55 using namespace librados;
56
57 // two steps seem to be necessary to do this right
58 #define STR(x) _STR(x)
59 #define _STR(x) #x
60
61 void usage(ostream& out)
62 {
63 out << \
64 "usage: rados [options] [commands]\n"
65 "POOL COMMANDS\n"
66 " lspools list pools\n"
67 " mkpool <pool-name> [123[ 4]] create pool <pool-name>'\n"
68 " [with auid 123[and using crush rule 4]]\n"
69 " cppool <pool-name> <dest-pool> copy content of a pool\n"
70 " rmpool <pool-name> [<pool-name> --yes-i-really-really-mean-it]\n"
71 " remove pool <pool-name>'\n"
72 " purge <pool-name> --yes-i-really-really-mean-it\n"
73 " remove all objects from pool <pool-name> without removing it\n"
74 " df show per-pool and total usage\n"
75 " ls list objects in pool\n\n"
76 " chown 123 change the pool owner to auid 123\n"
77 "\n"
78 "POOL SNAP COMMANDS\n"
79 " lssnap list snaps\n"
80 " mksnap <snap-name> create snap <snap-name>\n"
81 " rmsnap <snap-name> remove snap <snap-name>\n"
82 "\n"
83 "OBJECT COMMANDS\n"
84 " get <obj-name> [outfile] fetch object\n"
85 " put <obj-name> [infile] [--offset offset]\n"
86 " write object with start offset (default:0)\n"
87 " append <obj-name> [infile] append object\n"
88 " truncate <obj-name> length truncate object\n"
89 " create <obj-name> create object\n"
90 " rm <obj-name> ...[--force-full] [force no matter full or not]remove object(s)\n"
91 " cp <obj-name> [target-obj] copy object\n"
92 " listxattr <obj-name>\n"
93 " getxattr <obj-name> attr\n"
94 " setxattr <obj-name> attr val\n"
95 " rmxattr <obj-name> attr\n"
96 " stat <obj-name> stat the named object\n"
97 " mapext <obj-name>\n"
98 " rollback <obj-name> <snap-name> roll back object to snap <snap-name>\n"
99 "\n"
100 " listsnaps <obj-name> list the snapshots of this object\n"
101 " bench <seconds> write|seq|rand [-t concurrent_operations] [--no-cleanup] [--run-name run_name] [--no-hints]\n"
102 " default is 16 concurrent IOs and 4 MB ops\n"
103 " default is to clean up after write benchmark\n"
104 " default run-name is 'benchmark_last_metadata'\n"
105 " cleanup [--run-name run_name] [--prefix prefix]\n"
106 " clean up a previous benchmark operation\n"
107 " default run-name is 'benchmark_last_metadata'\n"
108 " load-gen [options] generate load on the cluster\n"
109 " listomapkeys <obj-name> list the keys in the object map\n"
110 " listomapvals <obj-name> list the keys and vals in the object map \n"
111 " getomapval <obj-name> <key> [file] show the value for the specified key\n"
112 " in the object's object map\n"
113 " setomapval <obj-name> <key> <val>\n"
114 " rmomapkey <obj-name> <key>\n"
115 " getomapheader <obj-name> [file]\n"
116 " setomapheader <obj-name> <val>\n"
117 " tmap-to-omap <obj-name> convert tmap keys/values to omap\n"
118 " watch <obj-name> add watcher on this object\n"
119 " notify <obj-name> <message> notify watcher of this object with message\n"
120 " listwatchers <obj-name> list the watchers of this object\n"
121 " set-alloc-hint <obj-name> <expected-object-size> <expected-write-size>\n"
122 " set allocation hint for an object\n"
123 "\n"
124 "IMPORT AND EXPORT\n"
125 " export [filename]\n"
126 " Serialize pool contents to a file or standard out.\n"
127 " import [--dry-run] [--no-overwrite] < filename | - >\n"
128 " Load pool contents from a file or standard in\n"
129 "\n"
130 "ADVISORY LOCKS\n"
131 " lock list <obj-name>\n"
132 " List all advisory locks on an object\n"
133 " lock get <obj-name> <lock-name>\n"
134 " Try to acquire a lock\n"
135 " lock break <obj-name> <lock-name> <locker-name>\n"
136 " Try to break a lock acquired by another client\n"
137 " lock info <obj-name> <lock-name>\n"
138 " Show lock information\n"
139 " options:\n"
140 " --lock-tag Lock tag, all locks operation should use\n"
141 " the same tag\n"
142 " --lock-cookie Locker cookie\n"
143 " --lock-description Description of lock\n"
144 " --lock-duration Lock duration (in seconds)\n"
145 " --lock-type Lock type (shared, exclusive)\n"
146 "\n"
147 "SCRUB AND REPAIR:\n"
148 " list-inconsistent-pg <pool> list inconsistent PGs in given pool\n"
149 " list-inconsistent-obj <pgid> list inconsistent objects in given pg\n"
150 " list-inconsistent-snapset <pgid> list inconsistent snapsets in the given pg\n"
151 "\n"
152 "CACHE POOLS: (for testing/development only)\n"
153 " cache-flush <obj-name> flush cache pool object (blocking)\n"
154 " cache-try-flush <obj-name> flush cache pool object (non-blocking)\n"
155 " cache-evict <obj-name> evict cache pool object\n"
156 " cache-flush-evict-all flush+evict all objects\n"
157 " cache-try-flush-evict-all try-flush+evict all objects\n"
158 "\n"
159 "GLOBAL OPTIONS:\n"
160 " --object_locator object_locator\n"
161 " set object_locator for operation\n"
162 " -p pool\n"
163 " --pool=pool\n"
164 " select given pool by name\n"
165 " --target-pool=pool\n"
166 " select target pool by name\n"
167 " -b op_size\n"
168 " set the block size for put/get ops and for write benchmarking\n"
169 " -o object_size\n"
170 " set the object size for put/get ops and for write benchmarking\n"
171 " --max-objects\n"
172 " set the max number of objects for write benchmarking\n"
173 " -s name\n"
174 " --snap name\n"
175 " select given snap name for (read) IO\n"
176 " -i infile\n"
177 " --create\n"
178 " create the pool or directory that was specified\n"
179 " -N namespace\n"
180 " --namespace=namespace\n"
181 " specify the namespace to use for the object\n"
182 " --all\n"
183 " Use with ls to list objects in all namespaces\n"
184 " Put in CEPH_ARGS environment variable to make this the default\n"
185 " --default\n"
186 " Use with ls to list objects in default namespace\n"
187 " Takes precedence over --all in case --all is in environment\n"
188 " --target-locator\n"
189 " Use with cp to specify the locator of the new object\n"
190 " --target-nspace\n"
191 " Use with cp to specify the namespace of the new object\n"
192 " --striper\n"
193 " Use radostriper interface rather than pure rados\n"
194 " Available for stat, get, put, truncate, rm, ls and \n"
195 " all xattr related operations\n"
196 "\n"
197 "BENCH OPTIONS:\n"
198 " -t N\n"
199 " --concurrent-ios=N\n"
200 " Set number of concurrent I/O operations\n"
201 " --show-time\n"
202 " prefix output with date/time\n"
203 " --no-verify\n"
204 " do not verify contents of read objects\n"
205 " --write-object\n"
206 " write contents to the objects\n"
207 " --write-omap\n"
208 " write contents to the omap\n"
209 " --write-xattr\n"
210 " write contents to the extended attributes\n"
211 "\n"
212 "LOAD GEN OPTIONS:\n"
213 " --num-objects total number of objects\n"
214 " --min-object-size min object size\n"
215 " --max-object-size max object size\n"
216 " --min-op-len min io size of operations\n"
217 " --max-op-len max io size of operations\n"
218 " --max-ops max number of operations\n"
219 " --max-backlog max backlog size\n"
220 " --read-percent percent of operations that are read\n"
221 " --target-throughput target throughput (in bytes)\n"
222 " --run-length total time (in seconds)\n"
223 "CACHE POOLS OPTIONS:\n"
224 " --with-clones include clones when doing flush or evict\n"
225 "OMAP OPTIONS:\n"
226 " --omap-key-file file read the omap key from a file\n";
227 }
228
229 unsigned default_op_size = 1 << 22;
230
231 static void usage_exit()
232 {
233 usage(cerr);
234 exit(1);
235 }
236
237
238 template <typename I, typename T>
239 static int rados_sistrtoll(I &i, T *val) {
240 std::string err;
241 *val = strict_sistrtoll(i->second.c_str(), &err);
242 if (err != "") {
243 cerr << "Invalid value for " << i->first << ": " << err << std::endl;
244 return -EINVAL;
245 } else {
246 return 0;
247 }
248 }
249
250
251 static int dump_data(std::string const &filename, bufferlist const &data)
252 {
253 int fd;
254 if (filename == "-") {
255 fd = STDOUT_FILENO;
256 } else {
257 fd = TEMP_FAILURE_RETRY(::open(filename.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644));
258 if (fd < 0) {
259 int err = errno;
260 cerr << "failed to open file: " << cpp_strerror(err) << std::endl;
261 return -err;
262 }
263 }
264
265 int r = data.write_fd(fd);
266
267 if (fd != 1) {
268 VOID_TEMP_FAILURE_RETRY(::close(fd));
269 }
270
271 return r;
272 }
273
274
275 static int do_get(IoCtx& io_ctx, RadosStriper& striper,
276 const char *objname, const char *outfile, unsigned op_size,
277 bool use_striper)
278 {
279 string oid(objname);
280
281 int fd;
282 if (strcmp(outfile, "-") == 0) {
283 fd = STDOUT_FILENO;
284 } else {
285 fd = TEMP_FAILURE_RETRY(::open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0644));
286 if (fd < 0) {
287 int err = errno;
288 cerr << "failed to open file: " << cpp_strerror(err) << std::endl;
289 return -err;
290 }
291 }
292
293 uint64_t offset = 0;
294 int ret;
295 while (true) {
296 bufferlist outdata;
297 if (use_striper) {
298 ret = striper.read(oid, &outdata, op_size, offset);
299 } else {
300 ret = io_ctx.read(oid, outdata, op_size, offset);
301 }
302 if (ret <= 0) {
303 goto out;
304 }
305 ret = outdata.write_fd(fd);
306 if (ret < 0) {
307 cerr << "error writing to file: " << cpp_strerror(ret) << std::endl;
308 goto out;
309 }
310 if (outdata.length() < op_size)
311 break;
312 offset += outdata.length();
313 }
314 ret = 0;
315
316 out:
317 if (fd != 1)
318 VOID_TEMP_FAILURE_RETRY(::close(fd));
319 return ret;
320 }
321
322 static int do_copy(IoCtx& io_ctx, const char *objname,
323 IoCtx& target_ctx, const char *target_obj)
324 {
325 __le32 src_fadvise_flags = LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL | LIBRADOS_OP_FLAG_FADVISE_NOCACHE;
326 __le32 dest_fadvise_flags = LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL | LIBRADOS_OP_FLAG_FADVISE_DONTNEED;
327 ObjectWriteOperation op;
328 op.copy_from2(objname, io_ctx, 0, src_fadvise_flags);
329 op.set_op_flags2(dest_fadvise_flags);
330
331 return target_ctx.operate(target_obj, &op);
332 }
333
334 static int do_copy_pool(Rados& rados, const char *src_pool, const char *target_pool)
335 {
336 IoCtx src_ctx, target_ctx;
337 int ret = rados.ioctx_create(src_pool, src_ctx);
338 if (ret < 0) {
339 cerr << "cannot open source pool: " << src_pool << std::endl;
340 return ret;
341 }
342 ret = rados.ioctx_create(target_pool, target_ctx);
343 if (ret < 0) {
344 cerr << "cannot open target pool: " << target_pool << std::endl;
345 return ret;
346 }
347 src_ctx.set_namespace(all_nspaces);
348 librados::NObjectIterator i = src_ctx.nobjects_begin();
349 librados::NObjectIterator i_end = src_ctx.nobjects_end();
350 for (; i != i_end; ++i) {
351 string nspace = i->get_nspace();
352 string oid = i->get_oid();
353 string locator = i->get_locator();
354
355 string target_name = (nspace.size() ? nspace + "/" : "") + oid;
356 string src_name = target_name;
357 if (locator.size())
358 src_name += "(@" + locator + ")";
359 cout << src_pool << ":" << src_name << " => "
360 << target_pool << ":" << target_name << std::endl;
361
362 src_ctx.locator_set_key(locator);
363 src_ctx.set_namespace(nspace);
364 target_ctx.set_namespace(nspace);
365 ret = do_copy(src_ctx, oid.c_str(), target_ctx, oid.c_str());
366 if (ret < 0) {
367 cerr << "error copying object: " << cpp_strerror(errno) << std::endl;
368 return ret;
369 }
370 }
371
372 return 0;
373 }
374
375 static int do_put(IoCtx& io_ctx, RadosStriper& striper,
376 const char *objname, const char *infile, int op_size,
377 uint64_t obj_offset, bool use_striper)
378 {
379 string oid(objname);
380 bool stdio = (strcmp(infile, "-") == 0);
381 int ret = 0;
382 int fd = STDIN_FILENO;
383 if (!stdio)
384 fd = open(infile, O_RDONLY);
385 if (fd < 0) {
386 cerr << "error reading input file " << infile << ": " << cpp_strerror(errno) << std::endl;
387 return 1;
388 }
389 int count = op_size;
390 uint64_t offset = obj_offset;
391 while (count != 0) {
392 bufferlist indata;
393 count = indata.read_fd(fd, op_size);
394 if (count < 0) {
395 ret = -errno;
396 cerr << "error reading input file " << infile << ": " << cpp_strerror(ret) << std::endl;
397 goto out;
398 }
399
400 if (count == 0) {
401 if (offset == obj_offset) { // in case we have to create an empty object & if obj_offset > 0 do a hole
402 if (use_striper) {
403 ret = striper.write_full(oid, indata); // indata is empty
404 } else {
405 ret = io_ctx.write_full(oid, indata); // indata is empty
406 }
407 if (ret < 0) {
408 goto out;
409 }
410 if (offset) {
411 if (use_striper) {
412 ret = striper.trunc(oid, offset); // before truncate, object must be existed.
413 } else {
414 ret = io_ctx.trunc(oid, offset); // before truncate, object must be existed.
415 }
416
417 if (ret < 0) {
418 goto out;
419 }
420 }
421 }
422 continue;
423 }
424 if (use_striper) {
425 if (offset == 0)
426 ret = striper.write_full(oid, indata);
427 else
428 ret = striper.write(oid, indata, count, offset);
429 } else {
430 if (offset == 0)
431 ret = io_ctx.write_full(oid, indata);
432 else
433 ret = io_ctx.write(oid, indata, count, offset);
434 }
435
436 if (ret < 0) {
437 goto out;
438 }
439 offset += count;
440 }
441 ret = 0;
442 out:
443 if (fd != STDOUT_FILENO)
444 VOID_TEMP_FAILURE_RETRY(close(fd));
445 return ret;
446 }
447
448 static int do_append(IoCtx& io_ctx, RadosStriper& striper,
449 const char *objname, const char *infile, int op_size,
450 bool use_striper)
451 {
452 string oid(objname);
453 bool stdio = (strcmp(infile, "-") == 0);
454 int ret = 0;
455 int fd = STDIN_FILENO;
456 if (!stdio)
457 fd = open(infile, O_RDONLY);
458 if (fd < 0) {
459 cerr << "error reading input file " << infile << ": " << cpp_strerror(errno) << std::endl;
460 return 1;
461 }
462 int count = op_size;
463 while (count != 0) {
464 bufferlist indata;
465 count = indata.read_fd(fd, op_size);
466 if (count < 0) {
467 ret = -errno;
468 cerr << "error reading input file " << infile << ": " << cpp_strerror(ret) << std::endl;
469 goto out;
470 }
471 if (use_striper) {
472 ret = striper.append(oid, indata, count);
473 } else {
474 ret = io_ctx.append(oid, indata, count);
475 }
476
477 if (ret < 0) {
478 goto out;
479 }
480 }
481 ret = 0;
482 out:
483 if (fd != STDOUT_FILENO)
484 VOID_TEMP_FAILURE_RETRY(close(fd));
485 return ret;
486 }
487
488 class RadosWatchCtx : public librados::WatchCtx2 {
489 IoCtx& ioctx;
490 string name;
491 public:
492 RadosWatchCtx(IoCtx& io, const char *imgname) : ioctx(io), name(imgname) {}
493 ~RadosWatchCtx() override {}
494 void handle_notify(uint64_t notify_id,
495 uint64_t cookie,
496 uint64_t notifier_id,
497 bufferlist& bl) override {
498 cout << "NOTIFY"
499 << " cookie " << cookie
500 << " notify_id " << notify_id
501 << " from " << notifier_id
502 << std::endl;
503 bl.hexdump(cout);
504 ioctx.notify_ack(name, notify_id, cookie, bl);
505 }
506 void handle_error(uint64_t cookie, int err) override {
507 cout << "ERROR"
508 << " cookie " << cookie
509 << " err " << cpp_strerror(err)
510 << std::endl;
511 }
512 };
513
514 static const char alphanum_table[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
515
516 int gen_rand_alphanumeric(char *dest, int size) /* size should be the required string size + 1 */
517 {
518 int ret = get_random_bytes(dest, size);
519 if (ret < 0) {
520 cerr << "cannot get random bytes: " << cpp_strerror(ret) << std::endl;
521 return -1;
522 }
523
524 int i;
525 for (i=0; i<size - 1; i++) {
526 int pos = (unsigned)dest[i];
527 dest[i] = alphanum_table[pos & 63];
528 }
529 dest[i] = '\0';
530
531 return 0;
532 }
533
534 struct obj_info {
535 string name;
536 size_t len;
537 };
538
539 class LoadGen {
540 size_t total_sent;
541 size_t total_completed;
542
543 IoCtx io_ctx;
544 Rados *rados;
545
546 map<int, obj_info> objs;
547
548 utime_t start_time;
549
550 bool going_down;
551
552 public:
553 int read_percent;
554 int num_objs;
555 size_t min_obj_len;
556 uint64_t max_obj_len;
557 size_t min_op_len;
558 size_t max_op_len;
559 size_t max_ops;
560 size_t max_backlog;
561 size_t target_throughput;
562 int run_length;
563
564 enum {
565 OP_READ,
566 OP_WRITE,
567 };
568
569 struct LoadGenOp {
570 int id;
571 int type;
572 string oid;
573 size_t off;
574 size_t len;
575 bufferlist bl;
576 LoadGen *lg;
577 librados::AioCompletion *completion;
578
579 LoadGenOp() : id(0), type(0), off(0), len(0), lg(NULL), completion(NULL) {}
580 explicit LoadGenOp(LoadGen *_lg) : id(0), type(0), off(0), len(0), lg(_lg), completion(NULL) {}
581 };
582
583 int max_op;
584
585 map<int, LoadGenOp *> pending_ops;
586
587 void gen_op(LoadGenOp *op);
588 uint64_t gen_next_op();
589 void run_op(LoadGenOp *op);
590
591 uint64_t cur_sent_rate() {
592 return total_sent / time_passed();
593 }
594
595 uint64_t cur_completed_rate() {
596 return total_completed / time_passed();
597 }
598
599 uint64_t total_expected() {
600 return target_throughput * time_passed();
601 }
602
603 float time_passed() {
604 utime_t now = ceph_clock_now();
605 now -= start_time;
606 uint64_t ns = now.nsec();
607 float total = (float) ns / 1000000000.0;
608 total += now.sec();
609 return total;
610 }
611
612 Mutex lock;
613 Cond cond;
614
615 explicit LoadGen(Rados *_rados) : rados(_rados), going_down(false), lock("LoadGen") {
616 read_percent = 80;
617 min_obj_len = 1024;
618 max_obj_len = 5ull * 1024ull * 1024ull * 1024ull;
619 min_op_len = 1024;
620 target_throughput = 5 * 1024 * 1024; // B/sec
621 max_op_len = 2 * 1024 * 1024;
622 max_ops = 16;
623 max_backlog = target_throughput * 2;
624 run_length = 60;
625
626 total_sent = 0;
627 total_completed = 0;
628 num_objs = 200;
629 max_op = 0;
630 }
631 int bootstrap(const char *pool);
632 int run();
633 void cleanup();
634
635 void io_cb(completion_t c, LoadGenOp *op) {
636 Mutex::Locker l(lock);
637
638 total_completed += op->len;
639
640 double rate = (double)cur_completed_rate() / (1024 * 1024);
641 std::streamsize original_precision = cout.precision();
642 cout.precision(3);
643 cout << "op " << op->id << " completed, throughput=" << rate << "MB/sec" << std::endl;
644 cout.precision(original_precision);
645
646 map<int, LoadGenOp *>::iterator iter = pending_ops.find(op->id);
647 if (iter != pending_ops.end())
648 pending_ops.erase(iter);
649
650 if (!going_down)
651 op->completion->release();
652
653 delete op;
654
655 cond.Signal();
656 }
657 };
658
659 static void _load_gen_cb(completion_t c, void *param)
660 {
661 LoadGen::LoadGenOp *op = (LoadGen::LoadGenOp *)param;
662 op->lg->io_cb(c, op);
663 }
664
665 int LoadGen::bootstrap(const char *pool)
666 {
667 char buf[128];
668 int i;
669
670 if (!pool) {
671 cerr << "ERROR: pool name was not specified" << std::endl;
672 return -EINVAL;
673 }
674
675 int ret = rados->ioctx_create(pool, io_ctx);
676 if (ret < 0) {
677 cerr << "error opening pool " << pool << ": " << cpp_strerror(ret) << std::endl;
678 return ret;
679 }
680
681 int buf_len = 1;
682 bufferptr p = buffer::create(buf_len);
683 bufferlist bl;
684 memset(p.c_str(), 0, buf_len);
685 bl.push_back(p);
686
687 list<librados::AioCompletion *> completions;
688 for (i = 0; i < num_objs; i++) {
689 obj_info info;
690 gen_rand_alphanumeric(buf, 16);
691 info.name = "obj-";
692 info.name.append(buf);
693 info.len = get_random(min_obj_len, max_obj_len);
694
695 // throttle...
696 while (completions.size() > max_ops) {
697 AioCompletion *c = completions.front();
698 c->wait_for_complete();
699 ret = c->get_return_value();
700 c->release();
701 completions.pop_front();
702 if (ret < 0) {
703 cerr << "aio_write failed" << std::endl;
704 return ret;
705 }
706 }
707
708 librados::AioCompletion *c = rados->aio_create_completion(NULL, NULL, NULL);
709 completions.push_back(c);
710 // generate object
711 ret = io_ctx.aio_write(info.name, c, bl, buf_len, info.len - buf_len);
712 if (ret < 0) {
713 cerr << "couldn't write obj: " << info.name << " ret=" << ret << std::endl;
714 return ret;
715 }
716 objs[i] = info;
717 }
718
719 list<librados::AioCompletion *>::iterator iter;
720 for (iter = completions.begin(); iter != completions.end(); ++iter) {
721 AioCompletion *c = *iter;
722 c->wait_for_complete();
723 ret = c->get_return_value();
724 c->release();
725 if (ret < 0) { // yes, we leak.
726 cerr << "aio_write failed" << std::endl;
727 return ret;
728 }
729 }
730 return 0;
731 }
732
733 void LoadGen::run_op(LoadGenOp *op)
734 {
735 op->completion = rados->aio_create_completion(op, _load_gen_cb, NULL);
736
737 switch (op->type) {
738 case OP_READ:
739 io_ctx.aio_read(op->oid, op->completion, &op->bl, op->len, op->off);
740 break;
741 case OP_WRITE:
742 bufferptr p = buffer::create(op->len);
743 memset(p.c_str(), 0, op->len);
744 op->bl.push_back(p);
745
746 io_ctx.aio_write(op->oid, op->completion, op->bl, op->len, op->off);
747 break;
748 }
749
750 total_sent += op->len;
751 }
752
753 void LoadGen::gen_op(LoadGenOp *op)
754 {
755 int i = get_random(0, objs.size() - 1);
756 obj_info& info = objs[i];
757 op->oid = info.name;
758
759 size_t len = get_random(min_op_len, max_op_len);
760 if (len > info.len)
761 len = info.len;
762 size_t off = get_random(0, info.len);
763
764 if (off + len > info.len)
765 off = info.len - len;
766
767 op->off = off;
768 op->len = len;
769
770 i = get_random(1, 100);
771 if (i > read_percent)
772 op->type = OP_WRITE;
773 else
774 op->type = OP_READ;
775
776 cout << (op->type == OP_READ ? "READ" : "WRITE") << " : oid=" << op->oid << " off=" << op->off << " len=" << op->len << std::endl;
777 }
778
779 uint64_t LoadGen::gen_next_op()
780 {
781 lock.Lock();
782
783 LoadGenOp *op = new LoadGenOp(this);
784 gen_op(op);
785 op->id = max_op++;
786 pending_ops[op->id] = op;
787
788 lock.Unlock();
789
790 run_op(op);
791
792 return op->len;
793 }
794
795 int LoadGen::run()
796 {
797 start_time = ceph_clock_now();
798 utime_t end_time = start_time;
799 end_time += run_length;
800 utime_t stamp_time = start_time;
801 uint32_t total_sec = 0;
802
803 while (1) {
804 lock.Lock();
805 utime_t one_second(1, 0);
806 cond.WaitInterval(lock, one_second);
807 lock.Unlock();
808 utime_t now = ceph_clock_now();
809
810 if (now > end_time)
811 break;
812
813 uint64_t expected = total_expected();
814 lock.Lock();
815 uint64_t sent = total_sent;
816 uint64_t completed = total_completed;
817 lock.Unlock();
818
819 if (now - stamp_time >= utime_t(1, 0)) {
820 double rate = (double)cur_completed_rate() / (1024 * 1024);
821 ++total_sec;
822 std::streamsize original_precision = cout.precision();
823 cout.precision(3);
824 cout << setw(5) << total_sec << ": throughput=" << rate << "MB/sec" << " pending data=" << sent - completed << std::endl;
825 cout.precision(original_precision);
826 stamp_time = now;
827 }
828
829 while (sent < expected &&
830 sent - completed < max_backlog &&
831 pending_ops.size() < max_ops) {
832 sent += gen_next_op();
833 }
834 }
835
836 // get a reference to all pending requests
837 vector<librados::AioCompletion *> completions;
838 lock.Lock();
839 going_down = true;
840 map<int, LoadGenOp *>::iterator iter;
841 for (iter = pending_ops.begin(); iter != pending_ops.end(); ++iter) {
842 LoadGenOp *op = iter->second;
843 completions.push_back(op->completion);
844 }
845 lock.Unlock();
846
847 cout << "waiting for all operations to complete" << std::endl;
848
849 // now wait on all the pending requests
850 for (vector<librados::AioCompletion *>::iterator citer = completions.begin(); citer != completions.end(); ++citer) {
851 librados::AioCompletion *c = *citer;
852 c->wait_for_complete();
853 c->release();
854 }
855
856 return 0;
857 }
858
859 void LoadGen::cleanup()
860 {
861 cout << "cleaning up objects" << std::endl;
862 map<int, obj_info>::iterator iter;
863 for (iter = objs.begin(); iter != objs.end(); ++iter) {
864 obj_info& info = iter->second;
865 int ret = io_ctx.remove(info.name);
866 if (ret < 0)
867 cerr << "couldn't remove obj: " << info.name << " ret=" << ret << std::endl;
868 }
869 }
870
871 enum OpWriteDest {
872 OP_WRITE_DEST_OBJ = 2 << 0,
873 OP_WRITE_DEST_OMAP = 2 << 1,
874 OP_WRITE_DEST_XATTR = 2 << 2,
875 };
876
877 class RadosBencher : public ObjBencher {
878 librados::AioCompletion **completions;
879 librados::Rados& rados;
880 librados::IoCtx& io_ctx;
881 librados::NObjectIterator oi;
882 bool iterator_valid;
883 OpWriteDest write_destination;
884
885 protected:
886 int completions_init(int concurrentios) override {
887 completions = new librados::AioCompletion *[concurrentios];
888 return 0;
889 }
890 void completions_done() override {
891 delete[] completions;
892 completions = NULL;
893 }
894 int create_completion(int slot, void (*cb)(void *, void*), void *arg) override {
895 completions[slot] = rados.aio_create_completion((void *) arg, 0, cb);
896
897 if (!completions[slot])
898 return -EINVAL;
899
900 return 0;
901 }
902 void release_completion(int slot) override {
903 completions[slot]->release();
904 completions[slot] = 0;
905 }
906
907 int aio_read(const std::string& oid, int slot, bufferlist *pbl, size_t len,
908 size_t offset) override {
909 return io_ctx.aio_read(oid, completions[slot], pbl, len, 0);
910 }
911
912 int aio_write(const std::string& oid, int slot, bufferlist& bl, size_t len,
913 size_t offset) override {
914 librados::ObjectWriteOperation op;
915
916 if (write_destination & OP_WRITE_DEST_OBJ) {
917 if (data.hints)
918 op.set_alloc_hint2(data.object_size, data.op_size,
919 ALLOC_HINT_FLAG_SEQUENTIAL_WRITE |
920 ALLOC_HINT_FLAG_SEQUENTIAL_READ |
921 ALLOC_HINT_FLAG_APPEND_ONLY |
922 ALLOC_HINT_FLAG_IMMUTABLE);
923 op.write(offset, bl);
924 }
925
926 if (write_destination & OP_WRITE_DEST_OMAP) {
927 std::map<std::string, librados::bufferlist> omap;
928 omap[string("bench-omap-key-") + stringify(offset)] = bl;
929 op.omap_set(omap);
930 }
931
932 if (write_destination & OP_WRITE_DEST_XATTR) {
933 char key[80];
934 snprintf(key, sizeof(key), "bench-xattr-key-%d", (int)offset);
935 op.setxattr(key, bl);
936 }
937
938 return io_ctx.aio_operate(oid, completions[slot], &op);
939 }
940
941 int aio_remove(const std::string& oid, int slot) override {
942 return io_ctx.aio_remove(oid, completions[slot]);
943 }
944
945 int sync_read(const std::string& oid, bufferlist& bl, size_t len) override {
946 return io_ctx.read(oid, bl, len, 0);
947 }
948 int sync_write(const std::string& oid, bufferlist& bl, size_t len) override {
949 return io_ctx.write_full(oid, bl);
950 }
951
952 int sync_remove(const std::string& oid) override {
953 return io_ctx.remove(oid);
954 }
955
956 bool completion_is_done(int slot) override {
957 return completions[slot]->is_safe();
958 }
959
960 int completion_wait(int slot) override {
961 return completions[slot]->wait_for_safe_and_cb();
962 }
963 int completion_ret(int slot) override {
964 return completions[slot]->get_return_value();
965 }
966
967 bool get_objects(std::list<Object>* objects, int num) override {
968 int count = 0;
969
970 if (!iterator_valid) {
971 oi = io_ctx.nobjects_begin();
972 iterator_valid = true;
973 }
974
975 librados::NObjectIterator ei = io_ctx.nobjects_end();
976
977 if (oi == ei) {
978 iterator_valid = false;
979 return false;
980 }
981
982 objects->clear();
983 for ( ; oi != ei && count < num; ++oi) {
984 Object obj(oi->get_oid(), oi->get_nspace());
985 objects->push_back(obj);
986 ++count;
987 }
988
989 return true;
990 }
991
992 void set_namespace( const std::string& ns) override {
993 io_ctx.set_namespace(ns);
994 }
995
996 public:
997 RadosBencher(CephContext *cct_, librados::Rados& _r, librados::IoCtx& _i)
998 : ObjBencher(cct_), completions(NULL), rados(_r), io_ctx(_i), iterator_valid(false), write_destination(OP_WRITE_DEST_OBJ) {}
999 ~RadosBencher() override { }
1000
1001 void set_write_destination(OpWriteDest dest) {
1002 write_destination = dest;
1003 }
1004 };
1005
1006 static int do_lock_cmd(std::vector<const char*> &nargs,
1007 const std::map < std::string, std::string > &opts,
1008 IoCtx *ioctx,
1009 Formatter *formatter)
1010 {
1011 if (nargs.size() < 3)
1012 usage_exit();
1013
1014 string cmd(nargs[1]);
1015 string oid(nargs[2]);
1016
1017 string lock_tag;
1018 string lock_cookie;
1019 string lock_description;
1020 int lock_duration = 0;
1021 ClsLockType lock_type = LOCK_EXCLUSIVE;
1022
1023 map<string, string>::const_iterator i;
1024 i = opts.find("lock-tag");
1025 if (i != opts.end()) {
1026 lock_tag = i->second;
1027 }
1028 i = opts.find("lock-cookie");
1029 if (i != opts.end()) {
1030 lock_cookie = i->second;
1031 }
1032 i = opts.find("lock-description");
1033 if (i != opts.end()) {
1034 lock_description = i->second;
1035 }
1036 i = opts.find("lock-duration");
1037 if (i != opts.end()) {
1038 if (rados_sistrtoll(i, &lock_duration)) {
1039 return -EINVAL;
1040 }
1041 }
1042 i = opts.find("lock-type");
1043 if (i != opts.end()) {
1044 const string& type_str = i->second;
1045 if (type_str.compare("exclusive") == 0) {
1046 lock_type = LOCK_EXCLUSIVE;
1047 } else if (type_str.compare("shared") == 0) {
1048 lock_type = LOCK_SHARED;
1049 } else {
1050 cerr << "unknown lock type was specified, aborting" << std::endl;
1051 return -EINVAL;
1052 }
1053 }
1054
1055 if (cmd.compare("list") == 0) {
1056 list<string> locks;
1057 int ret = rados::cls::lock::list_locks(ioctx, oid, &locks);
1058 if (ret < 0) {
1059 cerr << "ERROR: rados_list_locks(): " << cpp_strerror(ret) << std::endl;
1060 return ret;
1061 }
1062
1063 formatter->open_object_section("object");
1064 formatter->dump_string("objname", oid);
1065 formatter->open_array_section("locks");
1066 list<string>::iterator iter;
1067 for (iter = locks.begin(); iter != locks.end(); ++iter) {
1068 formatter->open_object_section("lock");
1069 formatter->dump_string("name", *iter);
1070 formatter->close_section();
1071 }
1072 formatter->close_section();
1073 formatter->close_section();
1074 formatter->flush(cout);
1075 return 0;
1076 }
1077
1078 if (nargs.size() < 4)
1079 usage_exit();
1080
1081 string lock_name(nargs[3]);
1082
1083 if (cmd.compare("info") == 0) {
1084 map<rados::cls::lock::locker_id_t, rados::cls::lock::locker_info_t> lockers;
1085 ClsLockType type = LOCK_NONE;
1086 string tag;
1087 int ret = rados::cls::lock::get_lock_info(ioctx, oid, lock_name, &lockers, &type, &tag);
1088 if (ret < 0) {
1089 cerr << "ERROR: rados_lock_get_lock_info(): " << cpp_strerror(ret) << std::endl;
1090 return ret;
1091 }
1092
1093 formatter->open_object_section("lock");
1094 formatter->dump_string("name", lock_name);
1095 formatter->dump_string("type", cls_lock_type_str(type));
1096 formatter->dump_string("tag", tag);
1097 formatter->open_array_section("lockers");
1098 map<rados::cls::lock::locker_id_t, rados::cls::lock::locker_info_t>::iterator iter;
1099 for (iter = lockers.begin(); iter != lockers.end(); ++iter) {
1100 const rados::cls::lock::locker_id_t& id = iter->first;
1101 const rados::cls::lock::locker_info_t& info = iter->second;
1102 formatter->open_object_section("locker");
1103 formatter->dump_stream("name") << id.locker;
1104 formatter->dump_string("cookie", id.cookie);
1105 formatter->dump_string("description", info.description);
1106 formatter->dump_stream("expiration") << info.expiration;
1107 formatter->dump_stream("addr") << info.addr;
1108 formatter->close_section();
1109 }
1110 formatter->close_section();
1111 formatter->close_section();
1112 formatter->flush(cout);
1113
1114 return ret;
1115 } else if (cmd.compare("get") == 0) {
1116 rados::cls::lock::Lock l(lock_name);
1117 l.set_cookie(lock_cookie);
1118 l.set_tag(lock_tag);
1119 l.set_duration(utime_t(lock_duration, 0));
1120 l.set_description(lock_description);
1121 int ret;
1122 switch (lock_type) {
1123 case LOCK_SHARED:
1124 ret = l.lock_shared(ioctx, oid);
1125 break;
1126 default:
1127 ret = l.lock_exclusive(ioctx, oid);
1128 }
1129 if (ret < 0) {
1130 cerr << "ERROR: failed locking: " << cpp_strerror(ret) << std::endl;
1131 return ret;
1132 }
1133
1134 return ret;
1135 }
1136
1137 if (nargs.size() < 5)
1138 usage_exit();
1139
1140 if (cmd.compare("break") == 0) {
1141 string locker(nargs[4]);
1142 rados::cls::lock::Lock l(lock_name);
1143 l.set_cookie(lock_cookie);
1144 l.set_tag(lock_tag);
1145 entity_name_t name;
1146 if (!name.parse(locker)) {
1147 cerr << "ERROR: failed to parse locker name (" << locker << ")" << std::endl;
1148 return -EINVAL;
1149 }
1150 int ret = l.break_lock(ioctx, oid, name);
1151 if (ret < 0) {
1152 cerr << "ERROR: failed breaking lock: " << cpp_strerror(ret) << std::endl;
1153 return ret;
1154 }
1155 } else {
1156 usage_exit();
1157 }
1158
1159 return 0;
1160 }
1161
1162 static int do_cache_flush(IoCtx& io_ctx, string oid)
1163 {
1164 ObjectReadOperation op;
1165 op.cache_flush();
1166 librados::AioCompletion *completion =
1167 librados::Rados::aio_create_completion();
1168 io_ctx.aio_operate(oid.c_str(), completion, &op,
1169 librados::OPERATION_IGNORE_CACHE |
1170 librados::OPERATION_IGNORE_OVERLAY,
1171 NULL);
1172 completion->wait_for_safe();
1173 int r = completion->get_return_value();
1174 completion->release();
1175 return r;
1176 }
1177
1178 static int do_cache_try_flush(IoCtx& io_ctx, string oid)
1179 {
1180 ObjectReadOperation op;
1181 op.cache_try_flush();
1182 librados::AioCompletion *completion =
1183 librados::Rados::aio_create_completion();
1184 io_ctx.aio_operate(oid.c_str(), completion, &op,
1185 librados::OPERATION_IGNORE_CACHE |
1186 librados::OPERATION_IGNORE_OVERLAY |
1187 librados::OPERATION_SKIPRWLOCKS,
1188 NULL);
1189 completion->wait_for_safe();
1190 int r = completion->get_return_value();
1191 completion->release();
1192 return r;
1193 }
1194
1195 static int do_cache_evict(IoCtx& io_ctx, string oid)
1196 {
1197 ObjectReadOperation op;
1198 op.cache_evict();
1199 librados::AioCompletion *completion =
1200 librados::Rados::aio_create_completion();
1201 io_ctx.aio_operate(oid.c_str(), completion, &op,
1202 librados::OPERATION_IGNORE_CACHE |
1203 librados::OPERATION_IGNORE_OVERLAY |
1204 librados::OPERATION_SKIPRWLOCKS,
1205 NULL);
1206 completion->wait_for_safe();
1207 int r = completion->get_return_value();
1208 completion->release();
1209 return r;
1210 }
1211
1212 static int do_cache_flush_evict_all(IoCtx& io_ctx, bool blocking)
1213 {
1214 int errors = 0;
1215 io_ctx.set_namespace(all_nspaces);
1216 try {
1217 librados::NObjectIterator i = io_ctx.nobjects_begin();
1218 librados::NObjectIterator i_end = io_ctx.nobjects_end();
1219 for (; i != i_end; ++i) {
1220 int r;
1221 cout << i->get_nspace() << "\t" << i->get_oid() << "\t" << i->get_locator() << std::endl;
1222 if (i->get_locator().size()) {
1223 io_ctx.locator_set_key(i->get_locator());
1224 } else {
1225 io_ctx.locator_set_key(string());
1226 }
1227 io_ctx.set_namespace(i->get_nspace());
1228 snap_set_t ls;
1229 io_ctx.snap_set_read(LIBRADOS_SNAP_DIR);
1230 r = io_ctx.list_snaps(i->get_oid(), &ls);
1231 if (r < 0) {
1232 cerr << "error listing snap shots " << i->get_nspace() << "/" << i->get_oid() << ": "
1233 << cpp_strerror(r) << std::endl;
1234 ++errors;
1235 continue;
1236 }
1237 std::vector<clone_info_t>::iterator ci = ls.clones.begin();
1238 // no snapshots
1239 if (ci == ls.clones.end()) {
1240 io_ctx.snap_set_read(CEPH_NOSNAP);
1241 if (blocking)
1242 r = do_cache_flush(io_ctx, i->get_oid());
1243 else
1244 r = do_cache_try_flush(io_ctx, i->get_oid());
1245 if (r < 0) {
1246 cerr << "failed to flush " << i->get_nspace() << "/" << i->get_oid() << ": "
1247 << cpp_strerror(r) << std::endl;
1248 ++errors;
1249 continue;
1250 }
1251 r = do_cache_evict(io_ctx, i->get_oid());
1252 if (r < 0) {
1253 cerr << "failed to evict " << i->get_nspace() << "/" << i->get_oid() << ": "
1254 << cpp_strerror(r) << std::endl;
1255 ++errors;
1256 continue;
1257 }
1258 } else {
1259 // has snapshots
1260 for (std::vector<clone_info_t>::iterator ci = ls.clones.begin();
1261 ci != ls.clones.end(); ++ci) {
1262 io_ctx.snap_set_read(ci->cloneid);
1263 if (blocking)
1264 r = do_cache_flush(io_ctx, i->get_oid());
1265 else
1266 r = do_cache_try_flush(io_ctx, i->get_oid());
1267 if (r < 0) {
1268 cerr << "failed to flush " << i->get_nspace() << "/" << i->get_oid() << ": "
1269 << cpp_strerror(r) << std::endl;
1270 ++errors;
1271 break;
1272 }
1273 r = do_cache_evict(io_ctx, i->get_oid());
1274 if (r < 0) {
1275 cerr << "failed to evict " << i->get_nspace() << "/" << i->get_oid() << ": "
1276 << cpp_strerror(r) << std::endl;
1277 ++errors;
1278 break;
1279 }
1280 }
1281 }
1282 }
1283 }
1284 catch (const std::runtime_error& e) {
1285 cerr << e.what() << std::endl;
1286 return -1;
1287 }
1288 return errors ? -1 : 0;
1289 }
1290
1291 static int do_get_inconsistent_pg_cmd(const std::vector<const char*> &nargs,
1292 Rados& rados,
1293 Formatter& formatter)
1294 {
1295 if (nargs.size() < 2) {
1296 usage_exit();
1297 }
1298 int64_t pool_id = rados.pool_lookup(nargs[1]);
1299 if (pool_id < 0) {
1300 cerr << "pool \"" << nargs[1] << "\" not found" << std::endl;
1301 return (int)pool_id;
1302 }
1303 std::vector<PlacementGroup> pgs;
1304 int ret = rados.get_inconsistent_pgs(pool_id, &pgs);
1305 if (ret) {
1306 return ret;
1307 }
1308 formatter.open_array_section("pgs");
1309 for (auto& pg : pgs) {
1310 formatter.dump_stream("pg") << pg;
1311 }
1312 formatter.close_section();
1313 formatter.flush(cout);
1314 cout << std::endl;
1315 return 0;
1316 }
1317
1318 static void dump_errors(const err_t &err, Formatter &f, const char *name)
1319 {
1320 f.open_array_section(name);
1321 if (err.has_shard_missing())
1322 f.dump_string("error", "missing");
1323 if (err.has_stat_error())
1324 f.dump_string("error", "stat_error");
1325 if (err.has_read_error())
1326 f.dump_string("error", "read_error");
1327 if (err.has_data_digest_mismatch_oi())
1328 f.dump_string("error", "data_digest_mismatch_oi");
1329 if (err.has_omap_digest_mismatch_oi())
1330 f.dump_string("error", "omap_digest_mismatch_oi");
1331 if (err.has_size_mismatch_oi())
1332 f.dump_string("error", "size_mismatch_oi");
1333 if (err.has_ec_hash_error())
1334 f.dump_string("error", "ec_hash_error");
1335 if (err.has_ec_size_error())
1336 f.dump_string("error", "ec_size_error");
1337 if (err.has_oi_attr_missing())
1338 f.dump_string("error", "oi_attr_missing");
1339 if (err.has_oi_attr_corrupted())
1340 f.dump_string("error", "oi_attr_corrupted");
1341 if (err.has_obj_size_oi_mismatch())
1342 f.dump_string("error", "obj_size_oi_mismatch");
1343 if (err.has_ss_attr_missing())
1344 f.dump_string("error", "ss_attr_missing");
1345 if (err.has_ss_attr_corrupted())
1346 f.dump_string("error", "ss_attr_corrupted");
1347 f.close_section();
1348 }
1349
1350 static void dump_shard(const shard_info_t& shard,
1351 const inconsistent_obj_t& inc,
1352 Formatter &f)
1353 {
1354 dump_errors(shard, f, "errors");
1355
1356 if (shard.has_shard_missing())
1357 return;
1358
1359 if (!shard.has_stat_error())
1360 f.dump_unsigned("size", shard.size);
1361 if (shard.omap_digest_present) {
1362 f.dump_format("omap_digest", "0x%08x", shard.omap_digest);
1363 }
1364 if (shard.data_digest_present) {
1365 f.dump_format("data_digest", "0x%08x", shard.data_digest);
1366 }
1367
1368 if (!shard.has_oi_attr_missing() && !shard.has_oi_attr_corrupted() &&
1369 inc.has_object_info_inconsistency()) {
1370 object_info_t oi;
1371 bufferlist bl;
1372 map<std::string, ceph::bufferlist>::iterator k = (const_cast<shard_info_t&>(shard)).attrs.find(OI_ATTR);
1373 assert(k != shard.attrs.end()); // Can't be missing
1374 bufferlist::iterator bliter = k->second.begin();
1375 ::decode(oi, bliter); // Can't be corrupted
1376 f.dump_stream("object_info") << oi;
1377 }
1378 if (!shard.has_ss_attr_missing() && !shard.has_ss_attr_corrupted() &&
1379 inc.has_snapset_inconsistency()) {
1380 SnapSet ss;
1381 bufferlist bl;
1382 map<std::string, ceph::bufferlist>::iterator k = (const_cast<shard_info_t&>(shard)).attrs.find(SS_ATTR);
1383 assert(k != shard.attrs.end()); // Can't be missing
1384 bufferlist::iterator bliter = k->second.begin();
1385 decode(ss, bliter); // Can't be corrupted
1386 f.dump_stream("snapset") << ss;
1387 }
1388 if (inc.has_attr_name_mismatch() || inc.has_attr_value_mismatch()
1389 || inc.union_shards.has_oi_attr_missing()
1390 || inc.union_shards.has_oi_attr_corrupted()
1391 || inc.union_shards.has_ss_attr_missing()
1392 || inc.union_shards.has_ss_attr_corrupted()) {
1393 f.open_array_section("attrs");
1394 for (auto kv : shard.attrs) {
1395 f.open_object_section("attr");
1396 f.dump_string("name", kv.first);
1397 bool b64;
1398 f.dump_string("value", cleanbin(kv.second, b64));
1399 f.dump_bool("Base64", b64);
1400 f.close_section();
1401 }
1402 f.close_section();
1403 }
1404 }
1405
1406 static void dump_obj_errors(const obj_err_t &err, Formatter &f)
1407 {
1408 f.open_array_section("errors");
1409 if (err.has_object_info_inconsistency())
1410 f.dump_string("error", "object_info_inconsistency");
1411 if (err.has_data_digest_mismatch())
1412 f.dump_string("error", "data_digest_mismatch");
1413 if (err.has_omap_digest_mismatch())
1414 f.dump_string("error", "omap_digest_mismatch");
1415 if (err.has_size_mismatch())
1416 f.dump_string("error", "size_mismatch");
1417 if (err.has_attr_value_mismatch())
1418 f.dump_string("error", "attr_value_mismatch");
1419 if (err.has_attr_name_mismatch())
1420 f.dump_string("error", "attr_name_mismatch");
1421 if (err.has_snapset_inconsistency())
1422 f.dump_string("error", "snapset_inconsistency");
1423 f.close_section();
1424 }
1425
1426 static void dump_object_id(const object_id_t& object,
1427 Formatter &f)
1428 {
1429 f.dump_string("name", object.name);
1430 f.dump_string("nspace", object.nspace);
1431 f.dump_string("locator", object.locator);
1432 switch (object.snap) {
1433 case CEPH_NOSNAP:
1434 f.dump_string("snap", "head");
1435 break;
1436 case CEPH_SNAPDIR:
1437 f.dump_string("snap", "snapdir");
1438 break;
1439 default:
1440 f.dump_unsigned("snap", object.snap);
1441 break;
1442 }
1443 }
1444
1445 static void dump_inconsistent(const inconsistent_obj_t& inc,
1446 Formatter &f)
1447 {
1448 f.open_object_section("object");
1449 dump_object_id(inc.object, f);
1450 f.dump_unsigned("version", inc.version);
1451 f.close_section();
1452
1453 dump_obj_errors(inc, f);
1454 dump_errors(inc.union_shards, f, "union_shard_errors");
1455 for (const auto& shard_info : inc.shards) {
1456 shard_info_t shard = const_cast<shard_info_t&>(shard_info.second);
1457 if (shard.selected_oi) {
1458 object_info_t oi;
1459 bufferlist bl;
1460 auto k = shard.attrs.find(OI_ATTR);
1461 assert(k != shard.attrs.end()); // Can't be missing
1462 bufferlist::iterator bliter = k->second.begin();
1463 ::decode(oi, bliter); // Can't be corrupted
1464 f.dump_stream("selected_object_info") << oi;
1465 break;
1466 }
1467 }
1468 f.open_array_section("shards");
1469 for (const auto& shard_info : inc.shards) {
1470 f.open_object_section("shard");
1471 auto& osd_shard = shard_info.first;
1472 f.dump_int("osd", osd_shard.osd);
1473 f.dump_bool("primary", shard_info.second.primary);
1474 auto shard = osd_shard.shard;
1475 if (shard != shard_id_t::NO_SHARD)
1476 f.dump_unsigned("shard", shard);
1477 dump_shard(shard_info.second, inc, f);
1478 f.close_section();
1479 }
1480 f.close_section();
1481 }
1482
1483 static void dump_inconsistent(const inconsistent_snapset_t& inc,
1484 Formatter &f)
1485 {
1486 dump_object_id(inc.object, f);
1487
1488 f.open_array_section("errors");
1489 if (inc.ss_attr_missing())
1490 f.dump_string("error", "ss_attr_missing");
1491 if (inc.ss_attr_corrupted())
1492 f.dump_string("error", "ss_attr_corrupted");
1493 if (inc.oi_attr_missing())
1494 f.dump_string("error", "oi_attr_missing");
1495 if (inc.oi_attr_corrupted())
1496 f.dump_string("error", "oi_attr_corrupted");
1497 if (inc.snapset_mismatch())
1498 f.dump_string("error", "snapset_mismatch");
1499 if (inc.head_mismatch())
1500 f.dump_string("error", "head_mismatch");
1501 if (inc.headless())
1502 f.dump_string("error", "headless");
1503 if (inc.size_mismatch())
1504 f.dump_string("error", "size_mismatch");
1505 if (inc.extra_clones())
1506 f.dump_string("error", "extra_clones");
1507 if (inc.clone_missing())
1508 f.dump_string("error", "clone_missing");
1509 f.close_section();
1510
1511 if (inc.extra_clones()) {
1512 f.open_array_section("extra clones");
1513 for (auto snap : inc.clones) {
1514 f.dump_unsigned("snap", snap);
1515 }
1516 f.close_section();
1517 }
1518
1519 if (inc.clone_missing()) {
1520 f.open_array_section("missing");
1521 for (auto snap : inc.missing) {
1522 f.dump_unsigned("snap", snap);
1523 }
1524 f.close_section();
1525 }
1526 }
1527
1528 // dispatch the call by type
1529 static int do_get_inconsistent(Rados& rados,
1530 const PlacementGroup& pg,
1531 const librados::object_id_t &start,
1532 unsigned max_return,
1533 AioCompletion *c,
1534 std::vector<inconsistent_obj_t>* objs,
1535 uint32_t* interval)
1536 {
1537 return rados.get_inconsistent_objects(pg, start, max_return, c,
1538 objs, interval);
1539 }
1540
1541 static int do_get_inconsistent(Rados& rados,
1542 const PlacementGroup& pg,
1543 const librados::object_id_t &start,
1544 unsigned max_return,
1545 AioCompletion *c,
1546 std::vector<inconsistent_snapset_t>* snapsets,
1547 uint32_t* interval)
1548 {
1549 return rados.get_inconsistent_snapsets(pg, start, max_return, c,
1550 snapsets, interval);
1551 }
1552
1553 template <typename T>
1554 static int do_get_inconsistent_cmd(const std::vector<const char*> &nargs,
1555 Rados& rados,
1556 Formatter& formatter)
1557 {
1558 if (nargs.size() < 2) {
1559 usage_exit();
1560 }
1561 PlacementGroup pg;
1562 int ret = 0;
1563 ret = pg.parse(nargs[1]);
1564 if (!ret) {
1565 cerr << "bad pg: " << nargs[1] << std::endl;
1566 return ret;
1567 }
1568 uint32_t interval = 0, first_interval = 0;
1569 const unsigned max_item_num = 32;
1570 bool opened = false;
1571 for (librados::object_id_t start;;) {
1572 std::vector<T> items;
1573 auto completion = librados::Rados::aio_create_completion();
1574 ret = do_get_inconsistent(rados, pg, start, max_item_num, completion,
1575 &items, &interval);
1576 completion->wait_for_safe();
1577 ret = completion->get_return_value();
1578 completion->release();
1579 if (ret < 0) {
1580 if (ret == -EAGAIN)
1581 cerr << "interval#" << interval << " expired." << std::endl;
1582 else if (ret == -ENOENT)
1583 cerr << "No scrub information available for pg " << pg << std::endl;
1584 else
1585 cerr << "Unknown error " << cpp_strerror(ret) << std::endl;
1586 break;
1587 }
1588 // It must be the same interval every time. EAGAIN would
1589 // occur if interval changes.
1590 assert(start.name.empty() || first_interval == interval);
1591 if (start.name.empty()) {
1592 first_interval = interval;
1593 formatter.open_object_section("info");
1594 formatter.dump_int("epoch", interval);
1595 formatter.open_array_section("inconsistents");
1596 opened = true;
1597 }
1598 for (auto& inc : items) {
1599 formatter.open_object_section("inconsistent");
1600 dump_inconsistent(inc, formatter);
1601 formatter.close_section();
1602 }
1603 if (items.size() < max_item_num) {
1604 formatter.close_section();
1605 break;
1606 }
1607 if (!items.empty()) {
1608 start = items.back().object;
1609 }
1610 items.clear();
1611 }
1612 if (opened) {
1613 formatter.close_section();
1614 formatter.flush(cout);
1615 }
1616 return ret;
1617 }
1618
1619 /**********************************************
1620
1621 **********************************************/
1622 static int rados_tool_common(const std::map < std::string, std::string > &opts,
1623 std::vector<const char*> &nargs)
1624 {
1625 int ret;
1626 bool create_pool = false;
1627 const char *pool_name = NULL;
1628 const char *target_pool_name = NULL;
1629 string oloc, target_oloc, nspace, target_nspace;
1630 int concurrent_ios = 16;
1631 unsigned op_size = default_op_size;
1632 unsigned object_size = 0;
1633 unsigned max_objects = 0;
1634 uint64_t obj_offset = 0;
1635 bool block_size_specified = false;
1636 int bench_write_dest = 0;
1637 bool cleanup = true;
1638 bool hints = true; // for rados bench
1639 bool no_verify = false;
1640 bool use_striper = false;
1641 bool with_clones = false;
1642 const char *snapname = NULL;
1643 snap_t snapid = CEPH_NOSNAP;
1644 std::map<std::string, std::string>::const_iterator i;
1645
1646 uint64_t min_obj_len = 0;
1647 uint64_t max_obj_len = 0;
1648 uint64_t min_op_len = 0;
1649 uint64_t max_op_len = 0;
1650 uint64_t max_ops = 0;
1651 uint64_t max_backlog = 0;
1652 uint64_t target_throughput = 0;
1653 int64_t read_percent = -1;
1654 uint64_t num_objs = 0;
1655 int run_length = 0;
1656
1657 bool show_time = false;
1658 bool wildcard = false;
1659
1660 std::string run_name;
1661 std::string prefix;
1662 bool forcefull = false;
1663 Formatter *formatter = NULL;
1664 bool pretty_format = false;
1665 const char *output = NULL;
1666 bool omap_key_valid = false;
1667 std::string omap_key;
1668 std::string omap_key_pretty;
1669
1670 Rados rados;
1671 IoCtx io_ctx;
1672 RadosStriper striper;
1673
1674 i = opts.find("create");
1675 if (i != opts.end()) {
1676 create_pool = true;
1677 }
1678 i = opts.find("pool");
1679 if (i != opts.end()) {
1680 pool_name = i->second.c_str();
1681 }
1682 i = opts.find("target_pool");
1683 if (i != opts.end()) {
1684 target_pool_name = i->second.c_str();
1685 }
1686 i = opts.find("object_locator");
1687 if (i != opts.end()) {
1688 oloc = i->second;
1689 }
1690 i = opts.find("target_locator");
1691 if (i != opts.end()) {
1692 target_oloc = i->second;
1693 }
1694 i = opts.find("target_nspace");
1695 if (i != opts.end()) {
1696 target_nspace = i->second;
1697 }
1698 i = opts.find("concurrent-ios");
1699 if (i != opts.end()) {
1700 if (rados_sistrtoll(i, &concurrent_ios)) {
1701 return -EINVAL;
1702 }
1703 }
1704 i = opts.find("run-name");
1705 if (i != opts.end()) {
1706 run_name = i->second;
1707 }
1708
1709 i = opts.find("force-full");
1710 if (i != opts.end()) {
1711 forcefull = true;
1712 }
1713 i = opts.find("prefix");
1714 if (i != opts.end()) {
1715 prefix = i->second;
1716 }
1717 i = opts.find("block-size");
1718 if (i != opts.end()) {
1719 if (rados_sistrtoll(i, &op_size)) {
1720 return -EINVAL;
1721 }
1722 block_size_specified = true;
1723 }
1724 i = opts.find("object-size");
1725 if (i != opts.end()) {
1726 if (rados_sistrtoll(i, &object_size)) {
1727 return -EINVAL;
1728 }
1729 block_size_specified = true;
1730 }
1731 i = opts.find("max-objects");
1732 if (i != opts.end()) {
1733 if (rados_sistrtoll(i, &max_objects)) {
1734 return -EINVAL;
1735 }
1736 }
1737 i = opts.find("offset");
1738 if (i != opts.end()) {
1739 if (rados_sistrtoll(i, &obj_offset)) {
1740 return -EINVAL;
1741 }
1742 }
1743 i = opts.find("snap");
1744 if (i != opts.end()) {
1745 snapname = i->second.c_str();
1746 }
1747 i = opts.find("snapid");
1748 if (i != opts.end()) {
1749 if (rados_sistrtoll(i, &snapid)) {
1750 return -EINVAL;
1751 }
1752 }
1753 i = opts.find("min-object-size");
1754 if (i != opts.end()) {
1755 if (rados_sistrtoll(i, &min_obj_len)) {
1756 return -EINVAL;
1757 }
1758 }
1759 i = opts.find("max-object-size");
1760 if (i != opts.end()) {
1761 if (rados_sistrtoll(i, &max_obj_len)) {
1762 return -EINVAL;
1763 }
1764 }
1765 i = opts.find("min-op-len");
1766 if (i != opts.end()) {
1767 if (rados_sistrtoll(i, &min_op_len)) {
1768 return -EINVAL;
1769 }
1770 }
1771 i = opts.find("max-op-len");
1772 if (i != opts.end()) {
1773 if (rados_sistrtoll(i, &max_op_len)) {
1774 return -EINVAL;
1775 }
1776 }
1777 i = opts.find("max-ops");
1778 if (i != opts.end()) {
1779 if (rados_sistrtoll(i, &max_ops)) {
1780 return -EINVAL;
1781 }
1782 }
1783 i = opts.find("max-backlog");
1784 if (i != opts.end()) {
1785 if (rados_sistrtoll(i, &max_backlog)) {
1786 return -EINVAL;
1787 }
1788 }
1789 i = opts.find("target-throughput");
1790 if (i != opts.end()) {
1791 if (rados_sistrtoll(i, &target_throughput)) {
1792 return -EINVAL;
1793 }
1794 }
1795 i = opts.find("read-percent");
1796 if (i != opts.end()) {
1797 if (rados_sistrtoll(i, &read_percent)) {
1798 return -EINVAL;
1799 }
1800 }
1801 i = opts.find("num-objects");
1802 if (i != opts.end()) {
1803 if (rados_sistrtoll(i, &num_objs)) {
1804 return -EINVAL;
1805 }
1806 }
1807 i = opts.find("run-length");
1808 if (i != opts.end()) {
1809 if (rados_sistrtoll(i, &run_length)) {
1810 return -EINVAL;
1811 }
1812 }
1813 i = opts.find("show-time");
1814 if (i != opts.end()) {
1815 show_time = true;
1816 }
1817 i = opts.find("no-cleanup");
1818 if (i != opts.end()) {
1819 cleanup = false;
1820 }
1821 i = opts.find("no-hints");
1822 if (i != opts.end()) {
1823 hints = false;
1824 }
1825 i = opts.find("pretty-format");
1826 if (i != opts.end()) {
1827 pretty_format = true;
1828 }
1829 i = opts.find("format");
1830 if (i != opts.end()) {
1831 const char *format = i->second.c_str();
1832 formatter = Formatter::create(format);
1833 if (!formatter) {
1834 cerr << "unrecognized format: " << format << std::endl;
1835 return -EINVAL;
1836 }
1837 }
1838 i = opts.find("namespace");
1839 if (i != opts.end()) {
1840 nspace = i->second;
1841 }
1842 i = opts.find("no-verify");
1843 if (i != opts.end()) {
1844 no_verify = true;
1845 }
1846 i = opts.find("output");
1847 if (i != opts.end()) {
1848 output = i->second.c_str();
1849 }
1850 i = opts.find("write-dest-obj");
1851 if (i != opts.end()) {
1852 bench_write_dest |= static_cast<int>(OP_WRITE_DEST_OBJ);
1853 }
1854 i = opts.find("write-dest-omap");
1855 if (i != opts.end()) {
1856 bench_write_dest |= static_cast<int>(OP_WRITE_DEST_OMAP);
1857 }
1858 i = opts.find("write-dest-xattr");
1859 if (i != opts.end()) {
1860 bench_write_dest |= static_cast<int>(OP_WRITE_DEST_XATTR);
1861 }
1862 i = opts.find("with-clones");
1863 if (i != opts.end()) {
1864 with_clones = true;
1865 }
1866 i = opts.find("omap-key-file");
1867 if (i != opts.end()) {
1868 string err;
1869 bufferlist indata;
1870 ret = indata.read_file(i->second.c_str(), &err);
1871 if (ret < 0) {
1872 cerr << err << std::endl;
1873 return 1;
1874 }
1875
1876 omap_key_valid = true;
1877 omap_key = std::string(indata.c_str(), indata.length());
1878 omap_key_pretty = omap_key;
1879 if (std::find_if_not(omap_key.begin(), omap_key.end(),
1880 (int (*)(int))isprint) != omap_key.end()) {
1881 omap_key_pretty = "(binary key)";
1882 }
1883 }
1884
1885 // open rados
1886 ret = rados.init_with_context(g_ceph_context);
1887 if (ret < 0) {
1888 cerr << "couldn't initialize rados: " << cpp_strerror(ret) << std::endl;
1889 goto out;
1890 }
1891
1892 ret = rados.connect();
1893 if (ret) {
1894 cerr << "couldn't connect to cluster: " << cpp_strerror(ret) << std::endl;
1895 ret = -1;
1896 goto out;
1897 }
1898
1899 if (create_pool && !pool_name) {
1900 cerr << "--create-pool requested but pool_name was not specified!" << std::endl;
1901 usage_exit();
1902 }
1903
1904 if (create_pool) {
1905 ret = rados.pool_create(pool_name, 0, 0);
1906 if (ret < 0) {
1907 cerr << "error creating pool " << pool_name << ": "
1908 << cpp_strerror(ret) << std::endl;
1909 goto out;
1910 }
1911 }
1912
1913 // open io context.
1914 if (pool_name) {
1915 ret = rados.ioctx_create(pool_name, io_ctx);
1916 if (ret < 0) {
1917 cerr << "error opening pool " << pool_name << ": "
1918 << cpp_strerror(ret) << std::endl;
1919 goto out;
1920 }
1921
1922 // align op_size
1923 {
1924 bool requires;
1925 ret = io_ctx.pool_requires_alignment2(&requires);
1926 if (ret < 0) {
1927 cerr << "error checking pool alignment requirement"
1928 << cpp_strerror(ret) << std::endl;
1929 goto out;
1930 }
1931
1932 if (requires) {
1933 uint64_t align = 0;
1934 ret = io_ctx.pool_required_alignment2(&align);
1935 if (ret < 0) {
1936 cerr << "error getting pool alignment"
1937 << cpp_strerror(ret) << std::endl;
1938 goto out;
1939 }
1940
1941 const uint64_t prev_op_size = op_size;
1942 op_size = uint64_t((op_size + align - 1) / align) * align;
1943 // Warn: if user specified and it was rounded
1944 if (prev_op_size != default_op_size && prev_op_size != op_size)
1945 cerr << "INFO: op_size has been rounded to " << op_size << std::endl;
1946 }
1947 }
1948
1949 // create striper interface
1950 if (opts.find("striper") != opts.end()) {
1951 ret = RadosStriper::striper_create(io_ctx, &striper);
1952 if (0 != ret) {
1953 cerr << "error opening pool " << pool_name << " with striper interface: "
1954 << cpp_strerror(ret) << std::endl;
1955 goto out;
1956 }
1957 use_striper = true;
1958 }
1959 }
1960
1961 // snapname?
1962 if (snapname) {
1963 if (!pool_name) {
1964 cerr << "pool name must be specified with --snap" << std::endl;
1965 ret = -1;
1966 goto out;
1967 }
1968 ret = io_ctx.snap_lookup(snapname, &snapid);
1969 if (ret < 0) {
1970 cerr << "error looking up snap '" << snapname << "': " << cpp_strerror(ret) << std::endl;
1971 goto out;
1972 }
1973 }
1974 if (oloc.size()) {
1975 if (!pool_name) {
1976 cerr << "pool name must be specified with --object_locator" << std::endl;
1977 ret = -1;
1978 goto out;
1979 }
1980 io_ctx.locator_set_key(oloc);
1981 }
1982 // Use namespace from command line if specified
1983 if (opts.find("namespace") != opts.end()) {
1984 if (!pool_name) {
1985 cerr << "pool name must be specified with --namespace" << std::endl;
1986 ret = -1;
1987 goto out;
1988 }
1989 io_ctx.set_namespace(nspace);
1990 // Use wildcard if --all specified and --default NOT specified
1991 } else if (opts.find("all") != opts.end() && opts.find("default") == opts.end()) {
1992 // Only the ls should ever set namespace to special value
1993 wildcard = true;
1994 }
1995 if (snapid != CEPH_NOSNAP) {
1996 if (!pool_name) {
1997 cerr << "pool name must be specified with --snapid" << std::endl;
1998 ret = -1;
1999 goto out;
2000 }
2001 string name;
2002 ret = io_ctx.snap_get_name(snapid, &name);
2003 if (ret < 0) {
2004 cerr << "snapid " << snapid << " doesn't exist in pool "
2005 << io_ctx.get_pool_name() << std::endl;
2006 goto out;
2007 }
2008 io_ctx.snap_set_read(snapid);
2009 cout << "selected snap " << snapid << " '" << name << "'" << std::endl;
2010 }
2011
2012 assert(!nargs.empty());
2013
2014 // list pools?
2015 if (strcmp(nargs[0], "lspools") == 0) {
2016 list<string> vec;
2017 ret = rados.pool_list(vec);
2018 if (ret < 0) {
2019 cerr << "error listing pools: " << cpp_strerror(ret) << std::endl;
2020 goto out;
2021 }
2022 for (list<string>::iterator i = vec.begin(); i != vec.end(); ++i)
2023 cout << *i << std::endl;
2024 }
2025 else if (strcmp(nargs[0], "df") == 0) {
2026 // pools
2027 list<string> vec;
2028
2029 if (!pool_name) {
2030 ret = rados.pool_list(vec);
2031 if (ret < 0) {
2032 cerr << "error listing pools: " << cpp_strerror(ret) << std::endl;
2033 goto out;
2034 }
2035 } else {
2036 vec.push_back(pool_name);
2037 }
2038
2039 map<string,librados::pool_stat_t> stats;
2040 ret = rados.get_pool_stats(vec, stats);
2041 if (ret < 0) {
2042 cerr << "error fetching pool stats: " << cpp_strerror(ret) << std::endl;
2043 goto out;
2044 }
2045
2046 TextTable tab;
2047
2048 if (!formatter) {
2049 tab.define_column("POOL_NAME", TextTable::LEFT, TextTable::LEFT);
2050 tab.define_column("USED", TextTable::LEFT, TextTable::RIGHT);
2051 tab.define_column("OBJECTS", TextTable::LEFT, TextTable::RIGHT);
2052 tab.define_column("CLONES", TextTable::LEFT, TextTable::RIGHT);
2053 tab.define_column("COPIES", TextTable::LEFT, TextTable::RIGHT);
2054 tab.define_column("MISSING_ON_PRIMARY", TextTable::LEFT, TextTable::RIGHT);
2055 tab.define_column("UNFOUND", TextTable::LEFT, TextTable::RIGHT);
2056 tab.define_column("DEGRADED", TextTable::LEFT, TextTable::RIGHT);
2057 tab.define_column("RD_OPS", TextTable::LEFT, TextTable::RIGHT);
2058 tab.define_column("RD", TextTable::LEFT, TextTable::RIGHT);
2059 tab.define_column("WR_OPS", TextTable::LEFT, TextTable::RIGHT);
2060 tab.define_column("WR", TextTable::LEFT, TextTable::RIGHT);
2061 } else {
2062 formatter->open_object_section("stats");
2063 formatter->open_array_section("pools");
2064 }
2065 for (map<string,librados::pool_stat_t>::iterator i = stats.begin();
2066 i != stats.end();
2067 ++i) {
2068 const char *pool_name = i->first.c_str();
2069 librados::pool_stat_t& s = i->second;
2070 if (!formatter) {
2071 tab << pool_name
2072 << si_t(s.num_bytes)
2073 << s.num_objects
2074 << s.num_object_clones
2075 << s.num_object_copies
2076 << s.num_objects_missing_on_primary
2077 << s.num_objects_unfound
2078 << s.num_objects_degraded
2079 << s.num_rd
2080 << si_t(s.num_rd_kb << 10)
2081 << s.num_wr
2082 << si_t(s.num_wr_kb << 10)
2083 << TextTable::endrow;
2084 } else {
2085 formatter->open_object_section("pool");
2086 int64_t pool_id = rados.pool_lookup(pool_name);
2087 formatter->dump_string("name", pool_name);
2088 if (pool_id >= 0)
2089 formatter->dump_int("id", pool_id);
2090 else
2091 cerr << "ERROR: lookup_pg_pool_name for name=" << pool_name
2092 << " returned " << pool_id << std::endl;
2093 formatter->dump_int("size_bytes",s.num_bytes);
2094 formatter->dump_int("size_kb", s.num_kb);
2095 formatter->dump_int("num_objects", s.num_objects);
2096 formatter->dump_int("num_object_clones", s.num_object_clones);
2097 formatter->dump_int("num_object_copies", s.num_object_copies);
2098 formatter->dump_int("num_objects_missing_on_primary", s.num_objects_missing_on_primary);
2099 formatter->dump_int("num_objects_unfound", s.num_objects_unfound);
2100 formatter->dump_int("num_objects_degraded", s.num_objects_degraded);
2101 formatter->dump_int("read_ops", s.num_rd);
2102 formatter->dump_int("read_bytes", s.num_rd_kb * 1024ull);
2103 formatter->dump_int("write_ops", s.num_wr);
2104 formatter->dump_int("write_bytes", s.num_wr_kb * 1024ull);
2105 formatter->close_section();
2106 }
2107 }
2108
2109 if (!formatter) {
2110 cout << tab;
2111 }
2112
2113 // total
2114 cluster_stat_t tstats;
2115 ret = rados.cluster_stat(tstats);
2116 if (ret < 0) {
2117 cerr << "error getting total cluster usage: " << cpp_strerror(ret) << std::endl;
2118 goto out;
2119 }
2120 if (!formatter) {
2121 cout << std::endl;
2122 cout << "total_objects " << tstats.num_objects
2123 << std::endl;
2124 cout << "total_used " << si_t(tstats.kb_used << 10)
2125 << std::endl;
2126 cout << "total_avail " << si_t(tstats.kb_avail << 10)
2127 << std::endl;
2128 cout << "total_space " << si_t(tstats.kb << 10)
2129 << std::endl;
2130 } else {
2131 formatter->close_section();
2132 formatter->dump_int("total_objects", tstats.num_objects);
2133 formatter->dump_int("total_used", tstats.kb_used);
2134 formatter->dump_int("total_avail", tstats.kb_avail);
2135 formatter->dump_int("total_space", tstats.kb);
2136 formatter->close_section();
2137 formatter->flush(cout);
2138 }
2139 }
2140
2141 else if (strcmp(nargs[0], "ls") == 0) {
2142 if (!pool_name) {
2143 cerr << "pool name was not specified" << std::endl;
2144 ret = -1;
2145 goto out;
2146 }
2147
2148 if (wildcard)
2149 io_ctx.set_namespace(all_nspaces);
2150 bool use_stdout = (nargs.size() < 2) || (strcmp(nargs[1], "-") == 0);
2151 ostream *outstream;
2152 if(use_stdout)
2153 outstream = &cout;
2154 else
2155 outstream = new ofstream(nargs[1]);
2156
2157 {
2158 if (formatter)
2159 formatter->open_array_section("objects");
2160 try {
2161 librados::NObjectIterator i = io_ctx.nobjects_begin();
2162 librados::NObjectIterator i_end = io_ctx.nobjects_end();
2163 for (; i != i_end; ++i) {
2164 if (use_striper) {
2165 // in case of --striper option, we only list striped
2166 // objects, so we only display the first object of
2167 // each, without its suffix '.000...000'
2168 size_t l = i->get_oid().length();
2169 if (l <= 17 ||
2170 (0 != i->get_oid().compare(l-17, 17,".0000000000000000"))) continue;
2171 }
2172 if (!formatter) {
2173 // Only include namespace in output when wildcard specified
2174 if (wildcard)
2175 *outstream << i->get_nspace() << "\t";
2176 if (use_striper) {
2177 *outstream << i->get_oid().substr(0, i->get_oid().length()-17);
2178 } else {
2179 *outstream << i->get_oid();
2180 }
2181 if (i->get_locator().size())
2182 *outstream << "\t" << i->get_locator();
2183 *outstream << std::endl;
2184 } else {
2185 formatter->open_object_section("object");
2186 formatter->dump_string("namespace", i->get_nspace());
2187 if (use_striper) {
2188 formatter->dump_string("name", i->get_oid().substr(0, i->get_oid().length()-17));
2189 } else {
2190 formatter->dump_string("name", i->get_oid());
2191 }
2192 if (i->get_locator().size())
2193 formatter->dump_string("locator", i->get_locator());
2194 formatter->close_section(); //object
2195 }
2196 }
2197 }
2198 catch (const std::runtime_error& e) {
2199 cerr << e.what() << std::endl;
2200 ret = -1;
2201 goto out;
2202 }
2203 }
2204 if (formatter) {
2205 formatter->close_section(); //objects
2206 formatter->flush(*outstream);
2207 if (pretty_format)
2208 *outstream << std::endl;
2209 formatter->flush(*outstream);
2210 }
2211 if (!stdout)
2212 delete outstream;
2213 }
2214 else if (strcmp(nargs[0], "chown") == 0) {
2215 if (!pool_name || nargs.size() < 2)
2216 usage_exit();
2217
2218 char* endptr = NULL;
2219 uint64_t new_auid = strtol(nargs[1], &endptr, 10);
2220 if (*endptr) {
2221 cerr << "Invalid value for new-auid: '" << nargs[1] << "'" << std::endl;
2222 ret = -1;
2223 goto out;
2224 }
2225 ret = io_ctx.set_auid(new_auid);
2226 if (ret < 0) {
2227 cerr << "error changing auid on pool " << io_ctx.get_pool_name() << ':'
2228 << cpp_strerror(ret) << std::endl;
2229 } else cerr << "changed auid on pool " << io_ctx.get_pool_name()
2230 << " to " << new_auid << std::endl;
2231 }
2232 else if (strcmp(nargs[0], "mapext") == 0) {
2233 if (!pool_name || nargs.size() < 2)
2234 usage_exit();
2235 string oid(nargs[1]);
2236 std::map<uint64_t,uint64_t> m;
2237 ret = io_ctx.mapext(oid, 0, -1, m);
2238 if (ret < 0) {
2239 cerr << "mapext error on " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl;
2240 goto out;
2241 }
2242 std::map<uint64_t,uint64_t>::iterator iter;
2243 for (iter = m.begin(); iter != m.end(); ++iter) {
2244 cout << hex << iter->first << "\t" << iter->second << dec << std::endl;
2245 }
2246 }
2247 else if (strcmp(nargs[0], "stat") == 0) {
2248 if (!pool_name || nargs.size() < 2)
2249 usage_exit();
2250 string oid(nargs[1]);
2251 uint64_t size;
2252 time_t mtime;
2253 if (use_striper) {
2254 ret = striper.stat(oid, &size, &mtime);
2255 } else {
2256 ret = io_ctx.stat(oid, &size, &mtime);
2257 }
2258 if (ret < 0) {
2259 cerr << " error stat-ing " << pool_name << "/" << oid << ": "
2260 << cpp_strerror(ret) << std::endl;
2261 goto out;
2262 } else {
2263 utime_t t(mtime, 0);
2264 cout << pool_name << "/" << oid
2265 << " mtime " << t << ", size " << size << std::endl;
2266 }
2267 }
2268 else if (strcmp(nargs[0], "get") == 0) {
2269 if (!pool_name || nargs.size() < 3)
2270 usage_exit();
2271 ret = do_get(io_ctx, striper, nargs[1], nargs[2], op_size, use_striper);
2272 if (ret < 0) {
2273 cerr << "error getting " << pool_name << "/" << nargs[1] << ": " << cpp_strerror(ret) << std::endl;
2274 goto out;
2275 }
2276 }
2277 else if (strcmp(nargs[0], "put") == 0) {
2278 if (!pool_name || nargs.size() < 3)
2279 usage_exit();
2280 ret = do_put(io_ctx, striper, nargs[1], nargs[2], op_size, obj_offset, use_striper);
2281 if (ret < 0) {
2282 cerr << "error putting " << pool_name << "/" << nargs[1] << ": " << cpp_strerror(ret) << std::endl;
2283 goto out;
2284 }
2285 }
2286 else if (strcmp(nargs[0], "append") == 0) {
2287 if (!pool_name || nargs.size() < 3)
2288 usage_exit();
2289 ret = do_append(io_ctx, striper, nargs[1], nargs[2], op_size, use_striper);
2290 if (ret < 0) {
2291 cerr << "error appending " << pool_name << "/" << nargs[1] << ": " << cpp_strerror(ret) << std::endl;
2292 goto out;
2293 }
2294 }
2295 else if (strcmp(nargs[0], "truncate") == 0) {
2296 if (!pool_name || nargs.size() < 3)
2297 usage_exit();
2298
2299 string oid(nargs[1]);
2300 char* endptr = NULL;
2301 long size = strtoll(nargs[2], &endptr, 10);
2302 if (*endptr) {
2303 cerr << "Invalid value for size: '" << nargs[2] << "'" << std::endl;
2304 ret = -EINVAL;
2305 goto out;
2306 }
2307 if (size < 0) {
2308 cerr << "error, cannot truncate to negative value" << std::endl;
2309 usage_exit();
2310 }
2311 if (use_striper) {
2312 ret = striper.trunc(oid, size);
2313 } else {
2314 ret = io_ctx.trunc(oid, size);
2315 }
2316 if (ret < 0) {
2317 cerr << "error truncating oid "
2318 << oid << " to " << size << ": "
2319 << cpp_strerror(ret) << std::endl;
2320 } else {
2321 ret = 0;
2322 }
2323 }
2324 else if (strcmp(nargs[0], "setxattr") == 0) {
2325 if (!pool_name || nargs.size() < 3 || nargs.size() > 4)
2326 usage_exit();
2327
2328 string oid(nargs[1]);
2329 string attr_name(nargs[2]);
2330 bufferlist bl;
2331 if (nargs.size() == 4) {
2332 string attr_val(nargs[3]);
2333 bl.append(attr_val.c_str(), attr_val.length());
2334 } else {
2335 do {
2336 ret = bl.read_fd(STDIN_FILENO, 1024); // from stdin
2337 if (ret < 0)
2338 goto out;
2339 } while (ret > 0);
2340 }
2341
2342 if (use_striper) {
2343 ret = striper.setxattr(oid, attr_name.c_str(), bl);
2344 } else {
2345 ret = io_ctx.setxattr(oid, attr_name.c_str(), bl);
2346 }
2347 if (ret < 0) {
2348 cerr << "error setting xattr " << pool_name << "/" << oid << "/" << attr_name << ": " << cpp_strerror(ret) << std::endl;
2349 goto out;
2350 }
2351 else
2352 ret = 0;
2353 }
2354 else if (strcmp(nargs[0], "getxattr") == 0) {
2355 if (!pool_name || nargs.size() < 3)
2356 usage_exit();
2357
2358 string oid(nargs[1]);
2359 string attr_name(nargs[2]);
2360
2361 bufferlist bl;
2362 if (use_striper) {
2363 ret = striper.getxattr(oid, attr_name.c_str(), bl);
2364 } else {
2365 ret = io_ctx.getxattr(oid, attr_name.c_str(), bl);
2366 }
2367 if (ret < 0) {
2368 cerr << "error getting xattr " << pool_name << "/" << oid << "/" << attr_name << ": " << cpp_strerror(ret) << std::endl;
2369 goto out;
2370 }
2371 else
2372 ret = 0;
2373 string s(bl.c_str(), bl.length());
2374 cout << s;
2375 } else if (strcmp(nargs[0], "rmxattr") == 0) {
2376 if (!pool_name || nargs.size() < 3)
2377 usage_exit();
2378
2379 string oid(nargs[1]);
2380 string attr_name(nargs[2]);
2381
2382 if (use_striper) {
2383 ret = striper.rmxattr(oid, attr_name.c_str());
2384 } else {
2385 ret = io_ctx.rmxattr(oid, attr_name.c_str());
2386 }
2387 if (ret < 0) {
2388 cerr << "error removing xattr " << pool_name << "/" << oid << "/" << attr_name << ": " << cpp_strerror(ret) << std::endl;
2389 goto out;
2390 }
2391 } else if (strcmp(nargs[0], "listxattr") == 0) {
2392 if (!pool_name || nargs.size() < 2)
2393 usage_exit();
2394
2395 string oid(nargs[1]);
2396 map<std::string, bufferlist> attrset;
2397 bufferlist bl;
2398 if (use_striper) {
2399 ret = striper.getxattrs(oid, attrset);
2400 } else {
2401 ret = io_ctx.getxattrs(oid, attrset);
2402 }
2403 if (ret < 0) {
2404 cerr << "error getting xattr set " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl;
2405 goto out;
2406 }
2407
2408 for (map<std::string, bufferlist>::iterator iter = attrset.begin();
2409 iter != attrset.end(); ++iter) {
2410 cout << iter->first << std::endl;
2411 }
2412 } else if (strcmp(nargs[0], "getomapheader") == 0) {
2413 if (!pool_name || nargs.size() < 2)
2414 usage_exit();
2415
2416 string oid(nargs[1]);
2417 string outfile;
2418 if (nargs.size() >= 3) {
2419 outfile = nargs[2];
2420 }
2421
2422 bufferlist header;
2423 ret = io_ctx.omap_get_header(oid, &header);
2424 if (ret < 0) {
2425 cerr << "error getting omap header " << pool_name << "/" << oid
2426 << ": " << cpp_strerror(ret) << std::endl;
2427 goto out;
2428 } else {
2429 if (!outfile.empty()) {
2430 cerr << "Writing to " << outfile << std::endl;
2431 dump_data(outfile, header);
2432 } else {
2433 cout << "header (" << header.length() << " bytes) :\n";
2434 header.hexdump(cout);
2435 cout << std::endl;
2436 }
2437 ret = 0;
2438 }
2439 } else if (strcmp(nargs[0], "setomapheader") == 0) {
2440 if (!pool_name || nargs.size() < 3)
2441 usage_exit();
2442
2443 string oid(nargs[1]);
2444 string val(nargs[2]);
2445
2446 bufferlist bl;
2447 bl.append(val);
2448
2449 ret = io_ctx.omap_set_header(oid, bl);
2450 if (ret < 0) {
2451 cerr << "error setting omap value " << pool_name << "/" << oid
2452 << ": " << cpp_strerror(ret) << std::endl;
2453 goto out;
2454 } else {
2455 ret = 0;
2456 }
2457 } else if (strcmp(nargs[0], "setomapval") == 0) {
2458 uint32_t min_args = (omap_key_valid ? 2 : 3);
2459 if (!pool_name || nargs.size() < min_args || nargs.size() > min_args + 1) {
2460 usage_exit();
2461 }
2462
2463 string oid(nargs[1]);
2464 if (!omap_key_valid) {
2465 omap_key = nargs[2];
2466 omap_key_pretty = omap_key;
2467 }
2468
2469 bufferlist bl;
2470 if (nargs.size() > min_args) {
2471 string val(nargs[min_args]);
2472 bl.append(val);
2473 } else {
2474 do {
2475 ret = bl.read_fd(STDIN_FILENO, 1024); // from stdin
2476 if (ret < 0) {
2477 goto out;
2478 }
2479 } while (ret > 0);
2480 }
2481
2482 map<string, bufferlist> values;
2483 values[omap_key] = bl;
2484
2485 ret = io_ctx.omap_set(oid, values);
2486 if (ret < 0) {
2487 cerr << "error setting omap value " << pool_name << "/" << oid << "/"
2488 << omap_key_pretty << ": " << cpp_strerror(ret) << std::endl;
2489 goto out;
2490 } else {
2491 ret = 0;
2492 }
2493 } else if (strcmp(nargs[0], "getomapval") == 0) {
2494 uint32_t min_args = (omap_key_valid ? 2 : 3);
2495 if (!pool_name || nargs.size() < min_args || nargs.size() > min_args + 1) {
2496 usage_exit();
2497 }
2498
2499 string oid(nargs[1]);
2500 if (!omap_key_valid) {
2501 omap_key = nargs[2];
2502 omap_key_pretty = omap_key;
2503 }
2504
2505 set<string> keys;
2506 keys.insert(omap_key);
2507
2508 std::string outfile;
2509 if (nargs.size() > min_args) {
2510 outfile = nargs[min_args];
2511 }
2512
2513 map<string, bufferlist> values;
2514 ret = io_ctx.omap_get_vals_by_keys(oid, keys, &values);
2515 if (ret < 0) {
2516 cerr << "error getting omap value " << pool_name << "/" << oid << "/"
2517 << omap_key_pretty << ": " << cpp_strerror(ret) << std::endl;
2518 goto out;
2519 } else {
2520 ret = 0;
2521 }
2522
2523 if (values.size() && values.begin()->first == omap_key) {
2524 if (!outfile.empty()) {
2525 cerr << "Writing to " << outfile << std::endl;
2526 dump_data(outfile, values.begin()->second);
2527 } else {
2528 cout << "value (" << values.begin()->second.length() << " bytes) :\n";
2529 values.begin()->second.hexdump(cout);
2530 cout << std::endl;
2531 }
2532 ret = 0;
2533 } else {
2534 cout << "No such key: " << pool_name << "/" << oid << "/"
2535 << omap_key_pretty << std::endl;
2536 ret = -1;
2537 goto out;
2538 }
2539 } else if (strcmp(nargs[0], "rmomapkey") == 0) {
2540 uint32_t num_args = (omap_key_valid ? 2 : 3);
2541 if (!pool_name || nargs.size() != num_args) {
2542 usage_exit();
2543 }
2544
2545 string oid(nargs[1]);
2546 if (!omap_key_valid) {
2547 omap_key = nargs[2];
2548 omap_key_pretty = omap_key;
2549 }
2550 set<string> keys;
2551 keys.insert(omap_key);
2552
2553 ret = io_ctx.omap_rm_keys(oid, keys);
2554 if (ret < 0) {
2555 cerr << "error removing omap key " << pool_name << "/" << oid << "/"
2556 << omap_key_pretty << ": " << cpp_strerror(ret) << std::endl;
2557 goto out;
2558 } else {
2559 ret = 0;
2560 }
2561 } else if (strcmp(nargs[0], "listomapvals") == 0) {
2562 if (!pool_name || nargs.size() < 2)
2563 usage_exit();
2564
2565 string oid(nargs[1]);
2566 string last_read = "";
2567 int MAX_READ = 512;
2568 do {
2569 map<string, bufferlist> values;
2570 ret = io_ctx.omap_get_vals(oid, last_read, MAX_READ, &values);
2571 if (ret < 0) {
2572 cerr << "error getting omap keys " << pool_name << "/" << oid << ": "
2573 << cpp_strerror(ret) << std::endl;
2574 return 1;
2575 }
2576 ret = values.size();
2577 for (map<string, bufferlist>::const_iterator it = values.begin();
2578 it != values.end(); ++it) {
2579 last_read = it->first;
2580 // dump key in hex if it contains nonprintable characters
2581 if (std::count_if(it->first.begin(), it->first.end(),
2582 (int (*)(int))isprint) < (int)it->first.length()) {
2583 cout << "key (" << it->first.length() << " bytes):\n";
2584 bufferlist keybl;
2585 keybl.append(it->first);
2586 keybl.hexdump(cout);
2587 } else {
2588 cout << it->first;
2589 }
2590 cout << std::endl;
2591 cout << "value (" << it->second.length() << " bytes) :\n";
2592 it->second.hexdump(cout);
2593 cout << std::endl;
2594 }
2595 } while (ret == MAX_READ);
2596 ret = 0;
2597 }
2598 else if (strcmp(nargs[0], "cp") == 0) {
2599 if (!pool_name)
2600 usage_exit();
2601
2602 if (nargs.size() < 2 || nargs.size() > 3)
2603 usage_exit();
2604
2605 const char *target = target_pool_name;
2606 if (!target)
2607 target = pool_name;
2608
2609 const char *target_obj;
2610 if (nargs.size() < 3) {
2611 if (strcmp(target, pool_name) == 0) {
2612 cerr << "cannot copy object into itself" << std::endl;
2613 ret = -1;
2614 goto out;
2615 }
2616 target_obj = nargs[1];
2617 } else {
2618 target_obj = nargs[2];
2619 }
2620
2621 // open io context.
2622 IoCtx target_ctx;
2623 ret = rados.ioctx_create(target, target_ctx);
2624 if (ret < 0) {
2625 cerr << "error opening target pool " << target << ": "
2626 << cpp_strerror(ret) << std::endl;
2627 goto out;
2628 }
2629 if (target_oloc.size()) {
2630 target_ctx.locator_set_key(target_oloc);
2631 }
2632 if (target_nspace.size()) {
2633 target_ctx.set_namespace(target_nspace);
2634 }
2635
2636 ret = do_copy(io_ctx, nargs[1], target_ctx, target_obj);
2637 if (ret < 0) {
2638 cerr << "error copying " << pool_name << "/" << nargs[1] << " => " << target << "/" << target_obj << ": " << cpp_strerror(ret) << std::endl;
2639 goto out;
2640 }
2641 } else if (strcmp(nargs[0], "rm") == 0) {
2642 if (!pool_name || nargs.size() < 2)
2643 usage_exit();
2644 vector<const char *>::iterator iter = nargs.begin();
2645 ++iter;
2646 for (; iter != nargs.end(); ++iter) {
2647 const string & oid = *iter;
2648 if (use_striper) {
2649 if (forcefull) {
2650 ret = striper.remove(oid, CEPH_OSD_FLAG_FULL_FORCE);
2651 } else {
2652 ret = striper.remove(oid);
2653 }
2654 } else {
2655 if (forcefull) {
2656 ret = io_ctx.remove(oid, CEPH_OSD_FLAG_FULL_FORCE);
2657 } else {
2658 ret = io_ctx.remove(oid);
2659 }
2660 }
2661 if (ret < 0) {
2662 string name = (nspace.size() ? nspace + "/" : "" ) + oid;
2663 cerr << "error removing " << pool_name << ">" << name << ": " << cpp_strerror(ret) << std::endl;
2664 goto out;
2665 }
2666 }
2667 }
2668 else if (strcmp(nargs[0], "create") == 0) {
2669 if (!pool_name || nargs.size() < 2)
2670 usage_exit();
2671 string oid(nargs[1]);
2672 ret = io_ctx.create(oid, true);
2673 if (ret < 0) {
2674 cerr << "error creating " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl;
2675 goto out;
2676 }
2677 }
2678
2679 else if (strcmp(nargs[0], "tmap") == 0) {
2680 if (nargs.size() < 3)
2681 usage_exit();
2682 if (strcmp(nargs[1], "dump") == 0) {
2683 bufferlist outdata;
2684 string oid(nargs[2]);
2685 ret = io_ctx.read(oid, outdata, 0, 0);
2686 if (ret < 0) {
2687 cerr << "error reading " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl;
2688 goto out;
2689 }
2690 bufferlist::iterator p = outdata.begin();
2691 bufferlist header;
2692 map<string, bufferlist> kv;
2693 try {
2694 ::decode(header, p);
2695 ::decode(kv, p);
2696 }
2697 catch (buffer::error& e) {
2698 cerr << "error decoding tmap " << pool_name << "/" << oid << std::endl;
2699 ret = -EINVAL;
2700 goto out;
2701 }
2702 cout << "header (" << header.length() << " bytes):\n";
2703 header.hexdump(cout);
2704 cout << "\n";
2705 cout << kv.size() << " keys\n";
2706 for (map<string,bufferlist>::iterator q = kv.begin(); q != kv.end(); ++q) {
2707 cout << "key '" << q->first << "' (" << q->second.length() << " bytes):\n";
2708 q->second.hexdump(cout);
2709 cout << "\n";
2710 }
2711 }
2712 else if (strcmp(nargs[1], "set") == 0 ||
2713 strcmp(nargs[1], "create") == 0) {
2714 if (nargs.size() < 5)
2715 usage_exit();
2716 string oid(nargs[2]);
2717 string k(nargs[3]);
2718 string v(nargs[4]);
2719 bufferlist bl;
2720 char c = (strcmp(nargs[1], "set") == 0) ? CEPH_OSD_TMAP_SET : CEPH_OSD_TMAP_CREATE;
2721 ::encode(c, bl);
2722 ::encode(k, bl);
2723 ::encode(v, bl);
2724 ret = io_ctx.tmap_update(oid, bl);
2725 }
2726 }
2727
2728 else if (strcmp(nargs[0], "tmap-to-omap") == 0) {
2729 if (!pool_name || nargs.size() < 2)
2730 usage_exit();
2731 string oid(nargs[1]);
2732
2733 bufferlist bl;
2734 int r = io_ctx.tmap_get(oid, bl);
2735 if (r < 0) {
2736 ret = r;
2737 cerr << "error reading tmap " << pool_name << "/" << oid
2738 << ": " << cpp_strerror(ret) << std::endl;
2739 goto out;
2740 }
2741 bufferlist hdr;
2742 map<string, bufferlist> kv;
2743 bufferlist::iterator p = bl.begin();
2744 try {
2745 ::decode(hdr, p);
2746 ::decode(kv, p);
2747 }
2748 catch (buffer::error& e) {
2749 cerr << "error decoding tmap " << pool_name << "/" << oid << std::endl;
2750 ret = -EINVAL;
2751 goto out;
2752 }
2753 if (!p.end()) {
2754 cerr << "error decoding tmap (stray trailing data) in " << pool_name << "/" << oid << std::endl;
2755 ret = -EINVAL;
2756 goto out;
2757 }
2758 librados::ObjectWriteOperation wr;
2759 wr.omap_set_header(hdr);
2760 wr.omap_set(kv);
2761 wr.truncate(0); // delete the old tmap data
2762 r = io_ctx.operate(oid, &wr);
2763 if (r < 0) {
2764 ret = r;
2765 cerr << "error writing tmap data as omap on " << pool_name << "/" << oid
2766 << ": " << cpp_strerror(ret) << std::endl;
2767 goto out;
2768 }
2769 ret = 0;
2770 }
2771
2772 else if (strcmp(nargs[0], "mkpool") == 0) {
2773 int auid = 0;
2774 __u8 crush_rule = 0;
2775 if (nargs.size() < 2)
2776 usage_exit();
2777 if (nargs.size() > 2) {
2778 char* endptr = NULL;
2779 auid = strtol(nargs[2], &endptr, 10);
2780 if (*endptr) {
2781 cerr << "Invalid value for auid: '" << nargs[2] << "'" << std::endl;
2782 ret = -EINVAL;
2783 goto out;
2784 }
2785 cerr << "setting auid:" << auid << std::endl;
2786 if (nargs.size() > 3) {
2787 crush_rule = (__u8)strtol(nargs[3], &endptr, 10);
2788 if (*endptr) {
2789 cerr << "Invalid value for crush-rule: '" << nargs[3] << "'" << std::endl;
2790 ret = -EINVAL;
2791 goto out;
2792 }
2793 cerr << "using crush rule " << (int)crush_rule << std::endl;
2794 }
2795 }
2796 ret = rados.pool_create(nargs[1], auid, crush_rule);
2797 if (ret < 0) {
2798 cerr << "error creating pool " << nargs[1] << ": "
2799 << cpp_strerror(ret) << std::endl;
2800 goto out;
2801 }
2802 cout << "successfully created pool " << nargs[1] << std::endl;
2803 }
2804 else if (strcmp(nargs[0], "cppool") == 0) {
2805 bool force = nargs.size() == 4 && !strcmp(nargs[3], "--yes-i-really-mean-it");
2806 if (nargs.size() != 3 && !(nargs.size() == 4 && force))
2807 usage_exit();
2808 const char *src_pool = nargs[1];
2809 const char *target_pool = nargs[2];
2810
2811 if (strcmp(src_pool, target_pool) == 0) {
2812 cerr << "cannot copy pool into itself" << std::endl;
2813 ret = -1;
2814 goto out;
2815 }
2816
2817 cerr << "WARNING: pool copy does not preserve user_version, which some "
2818 << " apps may rely on." << std::endl;
2819
2820 if (rados.get_pool_is_selfmanaged_snaps_mode(src_pool)) {
2821 cerr << "WARNING: pool " << src_pool << " has selfmanaged snaps, which are not preserved\n"
2822 << " by the cppool operation. This will break any snapshot user."
2823 << std::endl;
2824 if (!force) {
2825 cerr << " If you insist on making a broken copy, you can pass\n"
2826 << " --yes-i-really-mean-it to proceed anyway."
2827 << std::endl;
2828 exit(1);
2829 }
2830 }
2831
2832 ret = do_copy_pool(rados, src_pool, target_pool);
2833 if (ret < 0) {
2834 cerr << "error copying pool " << src_pool << " => " << target_pool << ": "
2835 << cpp_strerror(ret) << std::endl;
2836 goto out;
2837 }
2838 cout << "successfully copied pool " << nargs[1] << std::endl;
2839 }
2840 else if (strcmp(nargs[0], "rmpool") == 0) {
2841 if (nargs.size() < 2)
2842 usage_exit();
2843 if (nargs.size() < 4 ||
2844 strcmp(nargs[1], nargs[2]) != 0 ||
2845 strcmp(nargs[3], "--yes-i-really-really-mean-it") != 0) {
2846 cerr << "WARNING:\n"
2847 << " This will PERMANENTLY DESTROY an entire pool of objects with no way back.\n"
2848 << " To confirm, pass the pool to remove twice, followed by\n"
2849 << " --yes-i-really-really-mean-it" << std::endl;
2850 ret = -1;
2851 goto out;
2852 }
2853 ret = rados.pool_delete(nargs[1]);
2854 if (ret >= 0) {
2855 cout << "successfully deleted pool " << nargs[1] << std::endl;
2856 } else { //error
2857 cerr << "pool " << nargs[1] << " could not be removed" << std::endl;
2858 cerr << "Check your monitor configuration - `mon allow pool delete` is set to false by default,"
2859 << " change it to true to allow deletion of pools" << std::endl;
2860 }
2861 }
2862 else if (strcmp(nargs[0], "purge") == 0) {
2863 if (nargs.size() < 2)
2864 usage_exit();
2865 if (nargs.size() < 3 ||
2866 strcmp(nargs[2], "--yes-i-really-really-mean-it") != 0) {
2867 cerr << "WARNING:\n"
2868 << " This will PERMANENTLY DESTROY all objects from a pool with no way back.\n"
2869 << " To confirm, follow pool with --yes-i-really-really-mean-it" << std::endl;
2870 ret = -1;
2871 goto out;
2872 }
2873 ret = rados.ioctx_create(nargs[1], io_ctx);
2874 if (ret < 0) {
2875 cerr << "error pool " << nargs[1] << ": "
2876 << cpp_strerror(ret) << std::endl;
2877 goto out;
2878 }
2879 io_ctx.set_namespace(all_nspaces);
2880 io_ctx.set_osdmap_full_try();
2881 RadosBencher bencher(g_ceph_context, rados, io_ctx);
2882 ret = bencher.clean_up_slow("", concurrent_ios);
2883 if (ret >= 0) {
2884 cout << "successfully purged pool " << nargs[1] << std::endl;
2885 } else { //error
2886 cerr << "pool " << nargs[1] << " could not be purged" << std::endl;
2887 cerr << "Check your monitor configuration - `mon allow pool delete` is set to false by default,"
2888 << " change it to true to allow deletion of pools" << std::endl;
2889 }
2890 }
2891 else if (strcmp(nargs[0], "lssnap") == 0) {
2892 if (!pool_name || nargs.size() != 1)
2893 usage_exit();
2894
2895 vector<snap_t> snaps;
2896 io_ctx.snap_list(&snaps);
2897 for (vector<snap_t>::iterator i = snaps.begin();
2898 i != snaps.end();
2899 ++i) {
2900 string s;
2901 time_t t;
2902 if (io_ctx.snap_get_name(*i, &s) < 0)
2903 continue;
2904 if (io_ctx.snap_get_stamp(*i, &t) < 0)
2905 continue;
2906 struct tm bdt;
2907 localtime_r(&t, &bdt);
2908 cout << *i << "\t" << s << "\t";
2909
2910 std::ios_base::fmtflags original_flags = cout.flags();
2911 cout.setf(std::ios::right);
2912 cout.fill('0');
2913 cout << std::setw(4) << (bdt.tm_year+1900)
2914 << '.' << std::setw(2) << (bdt.tm_mon+1)
2915 << '.' << std::setw(2) << bdt.tm_mday
2916 << ' '
2917 << std::setw(2) << bdt.tm_hour
2918 << ':' << std::setw(2) << bdt.tm_min
2919 << ':' << std::setw(2) << bdt.tm_sec
2920 << std::endl;
2921 cout.flags(original_flags);
2922 }
2923 cout << snaps.size() << " snaps" << std::endl;
2924 }
2925
2926 else if (strcmp(nargs[0], "mksnap") == 0) {
2927 if (!pool_name || nargs.size() < 2)
2928 usage_exit();
2929
2930 ret = io_ctx.snap_create(nargs[1]);
2931 if (ret < 0) {
2932 cerr << "error creating pool " << pool_name << " snapshot " << nargs[1]
2933 << ": " << cpp_strerror(ret) << std::endl;
2934 goto out;
2935 }
2936 cout << "created pool " << pool_name << " snap " << nargs[1] << std::endl;
2937 }
2938
2939 else if (strcmp(nargs[0], "rmsnap") == 0) {
2940 if (!pool_name || nargs.size() < 2)
2941 usage_exit();
2942
2943 ret = io_ctx.snap_remove(nargs[1]);
2944 if (ret < 0) {
2945 cerr << "error removing pool " << pool_name << " snapshot " << nargs[1]
2946 << ": " << cpp_strerror(ret) << std::endl;
2947 goto out;
2948 }
2949 cout << "removed pool " << pool_name << " snap " << nargs[1] << std::endl;
2950 }
2951
2952 else if (strcmp(nargs[0], "rollback") == 0) {
2953 if (!pool_name || nargs.size() < 3)
2954 usage_exit();
2955
2956 ret = io_ctx.snap_rollback(nargs[1], nargs[2]);
2957 if (ret < 0) {
2958 cerr << "error rolling back pool " << pool_name << " to snapshot " << nargs[1]
2959 << cpp_strerror(ret) << std::endl;
2960 goto out;
2961 }
2962 cout << "rolled back pool " << pool_name
2963 << " to snapshot " << nargs[2] << std::endl;
2964 }
2965 else if (strcmp(nargs[0], "bench") == 0) {
2966 if (!pool_name || nargs.size() < 3)
2967 usage_exit();
2968 char* endptr = NULL;
2969 int seconds = strtol(nargs[1], &endptr, 10);
2970 if (*endptr) {
2971 cerr << "Invalid value for seconds: '" << nargs[1] << "'" << std::endl;
2972 ret = -EINVAL;
2973 goto out;
2974 }
2975 int operation = 0;
2976 if (strcmp(nargs[2], "write") == 0)
2977 operation = OP_WRITE;
2978 else if (strcmp(nargs[2], "seq") == 0)
2979 operation = OP_SEQ_READ;
2980 else if (strcmp(nargs[2], "rand") == 0)
2981 operation = OP_RAND_READ;
2982 else
2983 usage_exit();
2984 if (operation != OP_WRITE) {
2985 if (block_size_specified) {
2986 cerr << "-b|--block_size option can be used only with 'write' bench test"
2987 << std::endl;
2988 ret = -EINVAL;
2989 goto out;
2990 }
2991 if (bench_write_dest != 0) {
2992 cerr << "--write-object, --write-omap and --write-xattr options can "
2993 "only be used with the 'write' bench test"
2994 << std::endl;
2995 ret = -EINVAL;
2996 goto out;
2997 }
2998 }
2999 else if (bench_write_dest == 0) {
3000 bench_write_dest = OP_WRITE_DEST_OBJ;
3001 }
3002
3003 if (!formatter && output) {
3004 cerr << "-o|--output option can only be used with '--format' option"
3005 << std::endl;
3006 ret = -EINVAL;
3007 goto out;
3008 }
3009 RadosBencher bencher(g_ceph_context, rados, io_ctx);
3010 bencher.set_show_time(show_time);
3011 bencher.set_write_destination(static_cast<OpWriteDest>(bench_write_dest));
3012
3013 ostream *outstream = NULL;
3014 if (formatter) {
3015 bencher.set_formatter(formatter);
3016 if (output)
3017 outstream = new ofstream(output);
3018 else
3019 outstream = &cout;
3020 bencher.set_outstream(*outstream);
3021 }
3022 if (!object_size)
3023 object_size = op_size;
3024 else if (object_size < op_size)
3025 op_size = object_size;
3026 cout << "hints = " << (int)hints << std::endl;
3027 ret = bencher.aio_bench(operation, seconds,
3028 concurrent_ios, op_size, object_size,
3029 max_objects, cleanup, hints, run_name, no_verify);
3030 if (ret != 0)
3031 cerr << "error during benchmark: " << cpp_strerror(ret) << std::endl;
3032 if (formatter && output)
3033 delete outstream;
3034 }
3035 else if (strcmp(nargs[0], "cleanup") == 0) {
3036 if (!pool_name)
3037 usage_exit();
3038 if (wildcard)
3039 io_ctx.set_namespace(all_nspaces);
3040 RadosBencher bencher(g_ceph_context, rados, io_ctx);
3041 ret = bencher.clean_up(prefix, concurrent_ios, run_name);
3042 if (ret != 0)
3043 cerr << "error during cleanup: " << cpp_strerror(ret) << std::endl;
3044 }
3045 else if (strcmp(nargs[0], "watch") == 0) {
3046 if (!pool_name || nargs.size() < 2)
3047 usage_exit();
3048 string oid(nargs[1]);
3049 RadosWatchCtx ctx(io_ctx, oid.c_str());
3050 uint64_t cookie;
3051 ret = io_ctx.watch2(oid, &cookie, &ctx);
3052 if (ret != 0)
3053 cerr << "error calling watch: " << cpp_strerror(ret) << std::endl;
3054 else {
3055 cout << "press enter to exit..." << std::endl;
3056 getchar();
3057 io_ctx.unwatch2(cookie);
3058 rados.watch_flush();
3059 }
3060 }
3061 else if (strcmp(nargs[0], "notify") == 0) {
3062 if (!pool_name || nargs.size() < 3)
3063 usage_exit();
3064 string oid(nargs[1]);
3065 string msg(nargs[2]);
3066 bufferlist bl, replybl;
3067 ::encode(msg, bl);
3068 ret = io_ctx.notify2(oid, bl, 10000, &replybl);
3069 if (ret != 0)
3070 cerr << "error calling notify: " << cpp_strerror(ret) << std::endl;
3071 if (replybl.length()) {
3072 map<pair<uint64_t,uint64_t>,bufferlist> rm;
3073 set<pair<uint64_t,uint64_t> > missed;
3074 bufferlist::iterator p = replybl.begin();
3075 ::decode(rm, p);
3076 ::decode(missed, p);
3077 for (map<pair<uint64_t,uint64_t>,bufferlist>::iterator p = rm.begin();
3078 p != rm.end();
3079 ++p) {
3080 cout << "reply client." << p->first.first
3081 << " cookie " << p->first.second
3082 << " : " << p->second.length() << " bytes" << std::endl;
3083 if (p->second.length())
3084 p->second.hexdump(cout);
3085 }
3086 for (multiset<pair<uint64_t,uint64_t> >::iterator p = missed.begin();
3087 p != missed.end(); ++p) {
3088 cout << "timeout client." << p->first
3089 << " cookie " << p->second << std::endl;
3090 }
3091 }
3092 } else if (strcmp(nargs[0], "set-alloc-hint") == 0) {
3093 if (!pool_name || nargs.size() < 4)
3094 usage_exit();
3095 string err;
3096 string oid(nargs[1]);
3097 uint64_t expected_object_size = strict_strtoll(nargs[2], 10, &err);
3098 if (!err.empty()) {
3099 cerr << "couldn't parse expected_object_size: " << err << std::endl;
3100 usage_exit();
3101 }
3102 uint64_t expected_write_size = strict_strtoll(nargs[3], 10, &err);
3103 if (!err.empty()) {
3104 cerr << "couldn't parse expected_write_size: " << err << std::endl;
3105 usage_exit();
3106 }
3107 ret = io_ctx.set_alloc_hint(oid, expected_object_size, expected_write_size);
3108 if (ret < 0) {
3109 cerr << "error setting alloc-hint " << pool_name << "/" << oid << ": "
3110 << cpp_strerror(ret) << std::endl;
3111 goto out;
3112 }
3113 } else if (strcmp(nargs[0], "load-gen") == 0) {
3114 if (!pool_name) {
3115 cerr << "error: must specify pool" << std::endl;
3116 usage_exit();
3117 }
3118 LoadGen lg(&rados);
3119 if (min_obj_len)
3120 lg.min_obj_len = min_obj_len;
3121 if (max_obj_len)
3122 lg.max_obj_len = max_obj_len;
3123 if (min_op_len)
3124 lg.min_op_len = min_op_len;
3125 if (max_op_len)
3126 lg.max_op_len = max_op_len;
3127 if (max_ops)
3128 lg.max_ops = max_ops;
3129 if (max_backlog)
3130 lg.max_backlog = max_backlog;
3131 if (target_throughput)
3132 lg.target_throughput = target_throughput << 20;
3133 if (read_percent >= 0)
3134 lg.read_percent = read_percent;
3135 if (num_objs)
3136 lg.num_objs = num_objs;
3137 if (run_length)
3138 lg.run_length = run_length;
3139
3140 cout << "run length " << run_length << " seconds" << std::endl;
3141 cout << "preparing " << lg.num_objs << " objects" << std::endl;
3142 ret = lg.bootstrap(pool_name);
3143 if (ret < 0) {
3144 cerr << "load-gen bootstrap failed" << std::endl;
3145 exit(1);
3146 }
3147 cout << "load-gen will run " << lg.run_length << " seconds" << std::endl;
3148 lg.run();
3149 lg.cleanup();
3150 } else if (strcmp(nargs[0], "listomapkeys") == 0) {
3151 if (!pool_name || nargs.size() < 2)
3152 usage_exit();
3153
3154 set<string> out_keys;
3155 ret = io_ctx.omap_get_keys(nargs[1], "", LONG_MAX, &out_keys);
3156 if (ret < 0) {
3157 cerr << "error getting omap key set " << pool_name << "/"
3158 << nargs[1] << ": " << cpp_strerror(ret) << std::endl;
3159 goto out;
3160 }
3161
3162 for (set<string>::iterator iter = out_keys.begin();
3163 iter != out_keys.end(); ++iter) {
3164 cout << *iter << std::endl;
3165 }
3166 } else if (strcmp(nargs[0], "lock") == 0) {
3167 if (!pool_name)
3168 usage_exit();
3169
3170 if (!formatter) {
3171 formatter = new JSONFormatter(pretty_format);
3172 }
3173 ret = do_lock_cmd(nargs, opts, &io_ctx, formatter);
3174 } else if (strcmp(nargs[0], "listwatchers") == 0) {
3175 if (!pool_name || nargs.size() < 2)
3176 usage_exit();
3177
3178 string oid(nargs[1]);
3179 std::list<obj_watch_t> lw;
3180
3181 ret = io_ctx.list_watchers(oid, &lw);
3182 if (ret < 0) {
3183 cerr << "error listing watchers " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl;
3184 goto out;
3185 }
3186 else
3187 ret = 0;
3188
3189 for (std::list<obj_watch_t>::iterator i = lw.begin(); i != lw.end(); ++i) {
3190 cout << "watcher=" << i->addr << " client." << i->watcher_id << " cookie=" << i->cookie << std::endl;
3191 }
3192 } else if (strcmp(nargs[0], "listsnaps") == 0) {
3193 if (!pool_name || nargs.size() < 2)
3194 usage_exit();
3195
3196 string oid(nargs[1]);
3197 snap_set_t ls;
3198
3199 io_ctx.snap_set_read(LIBRADOS_SNAP_DIR);
3200 ret = io_ctx.list_snaps(oid, &ls);
3201 if (ret < 0) {
3202 cerr << "error listing snap shots " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl;
3203 goto out;
3204 }
3205 else
3206 ret = 0;
3207
3208 map<snap_t,string> snamemap;
3209 if (formatter || pretty_format) {
3210 vector<snap_t> snaps;
3211 io_ctx.snap_list(&snaps);
3212 for (vector<snap_t>::iterator i = snaps.begin();
3213 i != snaps.end(); ++i) {
3214 string s;
3215 if (io_ctx.snap_get_name(*i, &s) < 0)
3216 continue;
3217 snamemap.insert(pair<snap_t,string>(*i, s));
3218 }
3219 }
3220
3221 if (formatter) {
3222 formatter->open_object_section("object");
3223 formatter->dump_string("name", oid);
3224 formatter->open_array_section("clones");
3225 } else {
3226 cout << oid << ":" << std::endl;
3227 cout << "cloneid snaps size overlap" << std::endl;
3228 }
3229
3230 for (std::vector<clone_info_t>::iterator ci = ls.clones.begin();
3231 ci != ls.clones.end(); ++ci) {
3232
3233 if (formatter) formatter->open_object_section("clone");
3234
3235 if (ci->cloneid == librados::SNAP_HEAD) {
3236 if (formatter)
3237 formatter->dump_string("id", "head");
3238 else
3239 cout << "head";
3240 } else {
3241 if (formatter)
3242 formatter->dump_unsigned("id", ci->cloneid);
3243 else
3244 cout << ci->cloneid;
3245 }
3246
3247 if (formatter)
3248 formatter->open_array_section("snapshots");
3249 else
3250 cout << "\t";
3251
3252 if (!formatter && ci->snaps.empty()) {
3253 cout << "-";
3254 }
3255 for (std::vector<snap_t>::const_iterator snapindex = ci->snaps.begin();
3256 snapindex != ci->snaps.end(); ++snapindex) {
3257
3258 map<snap_t,string>::iterator si;
3259
3260 if (formatter || pretty_format) si = snamemap.find(*snapindex);
3261
3262 if (formatter) {
3263 formatter->open_object_section("snapshot");
3264 formatter->dump_unsigned("id", *snapindex);
3265 if (si != snamemap.end())
3266 formatter->dump_string("name", si->second);
3267 formatter->close_section(); //snapshot
3268 } else {
3269 if (snapindex != ci->snaps.begin()) cout << ",";
3270 if (!pretty_format || (si == snamemap.end()))
3271 cout << *snapindex;
3272 else
3273 cout << si->second << "(" << *snapindex << ")";
3274 }
3275 }
3276
3277 if (formatter) {
3278 formatter->close_section(); //Snapshots
3279 formatter->dump_unsigned("size", ci->size);
3280 } else {
3281 cout << "\t" << ci->size;
3282 }
3283
3284 if (ci->cloneid != librados::SNAP_HEAD) {
3285 if (formatter)
3286 formatter->open_array_section("overlaps");
3287 else
3288 cout << "\t[";
3289
3290 for (std::vector< std::pair<uint64_t,uint64_t> >::iterator ovi = ci->overlap.begin();
3291 ovi != ci->overlap.end(); ++ovi) {
3292 if (formatter) {
3293 formatter->open_object_section("section");
3294 formatter->dump_unsigned("start", ovi->first);
3295 formatter->dump_unsigned("length", ovi->second);
3296 formatter->close_section(); //section
3297 } else {
3298 if (ovi != ci->overlap.begin()) cout << ",";
3299 cout << ovi->first << "~" << ovi->second;
3300 }
3301 }
3302 if (formatter)
3303 formatter->close_section(); //overlaps
3304 else
3305 cout << "]" << std::endl;
3306 }
3307 if (formatter) formatter->close_section(); //clone
3308 }
3309 if (formatter) {
3310 formatter->close_section(); //clones
3311 formatter->close_section(); //object
3312 formatter->flush(cout);
3313 } else {
3314 cout << std::endl;
3315 }
3316 } else if (strcmp(nargs[0], "list-inconsistent-pg") == 0) {
3317 if (!formatter) {
3318 formatter = new JSONFormatter(pretty_format);
3319 }
3320 ret = do_get_inconsistent_pg_cmd(nargs, rados, *formatter);
3321 } else if (strcmp(nargs[0], "list-inconsistent-obj") == 0) {
3322 if (!formatter) {
3323 formatter = new JSONFormatter(pretty_format);
3324 }
3325 ret = do_get_inconsistent_cmd<inconsistent_obj_t>(nargs, rados, *formatter);
3326 } else if (strcmp(nargs[0], "list-inconsistent-snapset") == 0) {
3327 if (!formatter) {
3328 formatter = new JSONFormatter(pretty_format);
3329 }
3330 ret = do_get_inconsistent_cmd<inconsistent_snapset_t>(nargs, rados, *formatter);
3331 } else if (strcmp(nargs[0], "cache-flush") == 0) {
3332 if (!pool_name || nargs.size() < 2)
3333 usage_exit();
3334 string oid(nargs[1]);
3335 if (with_clones) {
3336 snap_set_t ls;
3337 io_ctx.snap_set_read(LIBRADOS_SNAP_DIR);
3338 ret = io_ctx.list_snaps(oid, &ls);
3339 if (ret < 0) {
3340 cerr << "error listing snapshots " << pool_name << "/" << oid << ": "
3341 << cpp_strerror(ret) << std::endl;
3342 goto out;
3343 }
3344 for (std::vector<clone_info_t>::iterator ci = ls.clones.begin();
3345 ci != ls.clones.end(); ++ci) {
3346 if (snapid != CEPH_NOSNAP && ci->cloneid > snapid)
3347 break;
3348 io_ctx.snap_set_read(ci->cloneid);
3349 ret = do_cache_flush(io_ctx, oid);
3350 if (ret < 0) {
3351 cerr << "error from cache-flush " << oid << ": "
3352 << cpp_strerror(ret) << std::endl;
3353 goto out;
3354 }
3355 }
3356 } else {
3357 ret = do_cache_flush(io_ctx, oid);
3358 if (ret < 0) {
3359 cerr << "error from cache-flush " << oid << ": "
3360 << cpp_strerror(ret) << std::endl;
3361 goto out;
3362 }
3363 }
3364 } else if (strcmp(nargs[0], "cache-try-flush") == 0) {
3365 if (!pool_name || nargs.size() < 2)
3366 usage_exit();
3367 string oid(nargs[1]);
3368 if (with_clones) {
3369 snap_set_t ls;
3370 io_ctx.snap_set_read(LIBRADOS_SNAP_DIR);
3371 ret = io_ctx.list_snaps(oid, &ls);
3372 if (ret < 0) {
3373 cerr << "error listing snapshots " << pool_name << "/" << oid << ": "
3374 << cpp_strerror(ret) << std::endl;
3375 goto out;
3376 }
3377 for (std::vector<clone_info_t>::iterator ci = ls.clones.begin();
3378 ci != ls.clones.end(); ++ci) {
3379 if (snapid != CEPH_NOSNAP && ci->cloneid > snapid)
3380 break;
3381 io_ctx.snap_set_read(ci->cloneid);
3382 ret = do_cache_try_flush(io_ctx, oid);
3383 if (ret < 0) {
3384 cerr << "error from cache-flush " << oid << ": "
3385 << cpp_strerror(ret) << std::endl;
3386 goto out;
3387 }
3388 }
3389 } else {
3390 ret = do_cache_try_flush(io_ctx, oid);
3391 if (ret < 0) {
3392 cerr << "error from cache-flush " << oid << ": "
3393 << cpp_strerror(ret) << std::endl;
3394 goto out;
3395 }
3396 }
3397 } else if (strcmp(nargs[0], "cache-evict") == 0) {
3398 if (!pool_name || nargs.size() < 2)
3399 usage_exit();
3400 string oid(nargs[1]);
3401 if (with_clones) {
3402 snap_set_t ls;
3403 io_ctx.snap_set_read(LIBRADOS_SNAP_DIR);
3404 ret = io_ctx.list_snaps(oid, &ls);
3405 if (ret < 0) {
3406 cerr << "error listing snapshots " << pool_name << "/" << oid << ": "
3407 << cpp_strerror(ret) << std::endl;
3408 goto out;
3409 }
3410 for (std::vector<clone_info_t>::iterator ci = ls.clones.begin();
3411 ci != ls.clones.end(); ++ci) {
3412 if (snapid != CEPH_NOSNAP && ci->cloneid > snapid)
3413 break;
3414 io_ctx.snap_set_read(ci->cloneid);
3415 ret = do_cache_evict(io_ctx, oid);
3416 if (ret < 0) {
3417 cerr << "error from cache-flush " << oid << ": "
3418 << cpp_strerror(ret) << std::endl;
3419 goto out;
3420 }
3421 }
3422 } else {
3423 ret = do_cache_evict(io_ctx, oid);
3424 if (ret < 0) {
3425 cerr << "error from cache-flush " << oid << ": "
3426 << cpp_strerror(ret) << std::endl;
3427 goto out;
3428 }
3429 }
3430 } else if (strcmp(nargs[0], "cache-flush-evict-all") == 0) {
3431 if (!pool_name)
3432 usage_exit();
3433 ret = do_cache_flush_evict_all(io_ctx, true);
3434 if (ret < 0) {
3435 cerr << "error from cache-flush-evict-all: "
3436 << cpp_strerror(ret) << std::endl;
3437 goto out;
3438 }
3439 } else if (strcmp(nargs[0], "cache-try-flush-evict-all") == 0) {
3440 if (!pool_name)
3441 usage_exit();
3442 ret = do_cache_flush_evict_all(io_ctx, false);
3443 if (ret < 0) {
3444 cerr << "error from cache-try-flush-evict-all: "
3445 << cpp_strerror(ret) << std::endl;
3446 goto out;
3447 }
3448 } else if (strcmp(nargs[0], "set-redirect") == 0) {
3449 if (!pool_name)
3450 usage_exit();
3451
3452 const char *target = target_pool_name;
3453 if (!target)
3454 target = pool_name;
3455
3456 const char *target_obj;
3457 if (nargs.size() < 3) {
3458 if (strcmp(target, pool_name) == 0) {
3459 cerr << "cannot copy object into itself" << std::endl;
3460 ret = -1;
3461 goto out;
3462 }
3463 target_obj = nargs[1];
3464 } else {
3465 target_obj = nargs[2];
3466 }
3467
3468 IoCtx target_ctx;
3469 ret = rados.ioctx_create(target, target_ctx);
3470 if (target_oloc.size()) {
3471 target_ctx.locator_set_key(target_oloc);
3472 }
3473 if (target_nspace.size()) {
3474 target_ctx.set_namespace(target_nspace);
3475 }
3476
3477 ObjectWriteOperation op;
3478 op.set_redirect(target_obj, target_ctx, 0);
3479 ret = io_ctx.operate(nargs[1], &op);
3480 if (ret < 0) {
3481 cerr << "error set-redirect " << pool_name << "/" << nargs[1] << " => " << target << "/" << target_obj << ": " << cpp_strerror(ret) << std::endl;
3482 goto out;
3483 }
3484 } else if (strcmp(nargs[0], "export") == 0) {
3485 // export [filename]
3486 if (!pool_name || nargs.size() > 2) {
3487 usage_exit();
3488 }
3489
3490 int file_fd;
3491 if (nargs.size() < 2 || std::string(nargs[1]) == "-") {
3492 file_fd = STDOUT_FILENO;
3493 } else {
3494 file_fd = open(nargs[1], O_WRONLY|O_CREAT|O_TRUNC, 0666);
3495 if (file_fd < 0) {
3496 cerr << "Error opening '" << nargs[1] << "': "
3497 << cpp_strerror(file_fd) << std::endl;
3498 ret = file_fd;
3499 goto out;
3500 }
3501 }
3502
3503 ret = PoolDump(file_fd).dump(&io_ctx);
3504
3505 if (file_fd != STDIN_FILENO) {
3506 VOID_TEMP_FAILURE_RETRY(::close(file_fd));
3507 }
3508
3509 if (ret < 0) {
3510 cerr << "error from export: "
3511 << cpp_strerror(ret) << std::endl;
3512 goto out;
3513 }
3514 } else if (strcmp(nargs[0], "import") == 0) {
3515 // import [--no-overwrite] [--dry-run] <filename | - >
3516 if (!pool_name || nargs.size() > 4 || nargs.size() < 2) {
3517 usage_exit();
3518 }
3519
3520 // Last arg is the filename
3521 std::string const filename = nargs[nargs.size() - 1];
3522
3523 // All other args may be flags
3524 bool dry_run = false;
3525 bool no_overwrite = false;
3526 for (unsigned i = 1; i < nargs.size() - 1; ++i) {
3527 std::string arg(nargs[i]);
3528
3529 if (arg == std::string("--no-overwrite")) {
3530 no_overwrite = true;
3531 } else if (arg == std::string("--dry-run")) {
3532 dry_run = true;
3533 } else {
3534 std::cerr << "Invalid argument '" << arg << "'" << std::endl;
3535 ret = -EINVAL;
3536 goto out;
3537 }
3538 }
3539
3540 int file_fd;
3541 if (filename == "-") {
3542 file_fd = STDIN_FILENO;
3543 } else {
3544 file_fd = open(filename.c_str(), O_RDONLY);
3545 if (file_fd < 0) {
3546 cerr << "Error opening '" << filename << "': "
3547 << cpp_strerror(file_fd) << std::endl;
3548 ret = file_fd;
3549 goto out;
3550 }
3551 }
3552
3553 ret = RadosImport(file_fd, 0, dry_run).import(io_ctx, no_overwrite);
3554
3555 if (file_fd != STDIN_FILENO) {
3556 VOID_TEMP_FAILURE_RETRY(::close(file_fd));
3557 }
3558
3559 if (ret < 0) {
3560 cerr << "error from import: "
3561 << cpp_strerror(ret) << std::endl;
3562 goto out;
3563 }
3564 } else {
3565 cerr << "unrecognized command " << nargs[0] << "; -h or --help for usage" << std::endl;
3566 ret = -EINVAL;
3567 goto out;
3568 }
3569
3570 if (ret < 0)
3571 cerr << "error " << (-ret) << ": " << cpp_strerror(ret) << std::endl;
3572
3573 out:
3574 delete formatter;
3575 return (ret < 0) ? 1 : 0;
3576 }
3577
3578 int main(int argc, const char **argv)
3579 {
3580 vector<const char*> args;
3581 argv_to_vec(argc, argv, args);
3582 env_to_vec(args);
3583
3584 std::map < std::string, std::string > opts;
3585 std::string val;
3586
3587 // Necessary to support usage of -f for formatting,
3588 // since global_init will remove the -f using ceph
3589 // argparse procedures.
3590 for (auto j = args.begin(); j != args.end(); ++j) {
3591 if (strcmp(*j, "--") == 0) {
3592 break;
3593 } else if ((j+1) == args.end()) {
3594 // This can't be a formatting call (no format arg)
3595 break;
3596 } else if (strcmp(*j, "-f") == 0) {
3597 val = *(j+1);
3598 unique_ptr<Formatter> formatter(Formatter::create(val.c_str()));
3599
3600 if (formatter) {
3601 j = args.erase(j);
3602 opts["format"] = val;
3603
3604 j = args.erase(j);
3605 break;
3606 }
3607 }
3608 }
3609
3610 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
3611 CODE_ENVIRONMENT_UTILITY, 0);
3612 common_init_finish(g_ceph_context);
3613
3614 std::vector<const char*>::iterator i;
3615 for (i = args.begin(); i != args.end(); ) {
3616 if (ceph_argparse_double_dash(args, i)) {
3617 break;
3618 } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
3619 usage(cout);
3620 exit(0);
3621 } else if (ceph_argparse_flag(args, i, "-f", "--force", (char*)NULL)) {
3622 opts["force"] = "true";
3623 } else if (ceph_argparse_flag(args, i, "--force-full", (char*)NULL)) {
3624 opts["force-full"] = "true";
3625 } else if (ceph_argparse_flag(args, i, "-d", "--delete-after", (char*)NULL)) {
3626 opts["delete-after"] = "true";
3627 } else if (ceph_argparse_flag(args, i, "-C", "--create", "--create-pool",
3628 (char*)NULL)) {
3629 opts["create"] = "true";
3630 } else if (ceph_argparse_flag(args, i, "--pretty-format", (char*)NULL)) {
3631 opts["pretty-format"] = "true";
3632 } else if (ceph_argparse_flag(args, i, "--show-time", (char*)NULL)) {
3633 opts["show-time"] = "true";
3634 } else if (ceph_argparse_flag(args, i, "--no-cleanup", (char*)NULL)) {
3635 opts["no-cleanup"] = "true";
3636 } else if (ceph_argparse_flag(args, i, "--no-hints", (char*)NULL)) {
3637 opts["no-hints"] = "true";
3638 } else if (ceph_argparse_flag(args, i, "--no-verify", (char*)NULL)) {
3639 opts["no-verify"] = "true";
3640 } else if (ceph_argparse_witharg(args, i, &val, "--run-name", (char*)NULL)) {
3641 opts["run-name"] = val;
3642 } else if (ceph_argparse_witharg(args, i, &val, "--prefix", (char*)NULL)) {
3643 opts["prefix"] = val;
3644 } else if (ceph_argparse_witharg(args, i, &val, "-p", "--pool", (char*)NULL)) {
3645 opts["pool"] = val;
3646 } else if (ceph_argparse_witharg(args, i, &val, "--target-pool", (char*)NULL)) {
3647 opts["target_pool"] = val;
3648 } else if (ceph_argparse_witharg(args, i, &val, "--object-locator" , (char *)NULL)) {
3649 opts["object_locator"] = val;
3650 } else if (ceph_argparse_witharg(args, i, &val, "--target-locator" , (char *)NULL)) {
3651 opts["target_locator"] = val;
3652 } else if (ceph_argparse_witharg(args, i, &val, "--target-nspace" , (char *)NULL)) {
3653 opts["target_nspace"] = val;
3654 } else if (ceph_argparse_flag(args, i, "--striper" , (char *)NULL)) {
3655 opts["striper"] = "true";
3656 } else if (ceph_argparse_witharg(args, i, &val, "-t", "--concurrent-ios", (char*)NULL)) {
3657 opts["concurrent-ios"] = val;
3658 } else if (ceph_argparse_witharg(args, i, &val, "--block-size", (char*)NULL)) {
3659 opts["block-size"] = val;
3660 } else if (ceph_argparse_witharg(args, i, &val, "-b", (char*)NULL)) {
3661 opts["block-size"] = val;
3662 } else if (ceph_argparse_witharg(args, i, &val, "--object-size", (char*)NULL)) {
3663 opts["object-size"] = val;
3664 } else if (ceph_argparse_witharg(args, i, &val, "--max-objects", (char*)NULL)) {
3665 opts["max-objects"] = val;
3666 } else if (ceph_argparse_witharg(args, i, &val, "--offset", (char*)NULL)) {
3667 opts["offset"] = val;
3668 } else if (ceph_argparse_witharg(args, i, &val, "-o", (char*)NULL)) {
3669 opts["object-size"] = val;
3670 } else if (ceph_argparse_witharg(args, i, &val, "-s", "--snap", (char*)NULL)) {
3671 opts["snap"] = val;
3672 } else if (ceph_argparse_witharg(args, i, &val, "-S", "--snapid", (char*)NULL)) {
3673 opts["snapid"] = val;
3674 } else if (ceph_argparse_witharg(args, i, &val, "--min-object-size", (char*)NULL)) {
3675 opts["min-object-size"] = val;
3676 } else if (ceph_argparse_witharg(args, i, &val, "--max-object-size", (char*)NULL)) {
3677 opts["max-object-size"] = val;
3678 } else if (ceph_argparse_witharg(args, i, &val, "--min-op-len", (char*)NULL)) {
3679 opts["min-op-len"] = val;
3680 } else if (ceph_argparse_witharg(args, i, &val, "--max-op-len", (char*)NULL)) {
3681 opts["max-op-len"] = val;
3682 } else if (ceph_argparse_witharg(args, i, &val, "--max-ops", (char*)NULL)) {
3683 opts["max-ops"] = val;
3684 } else if (ceph_argparse_witharg(args, i, &val, "--max-backlog", (char*)NULL)) {
3685 opts["max-backlog"] = val;
3686 } else if (ceph_argparse_witharg(args, i, &val, "--target-throughput", (char*)NULL)) {
3687 opts["target-throughput"] = val;
3688 } else if (ceph_argparse_witharg(args, i, &val, "--read-percent", (char*)NULL)) {
3689 opts["read-percent"] = val;
3690 } else if (ceph_argparse_witharg(args, i, &val, "--num-objects", (char*)NULL)) {
3691 opts["num-objects"] = val;
3692 } else if (ceph_argparse_witharg(args, i, &val, "--run-length", (char*)NULL)) {
3693 opts["run-length"] = val;
3694 } else if (ceph_argparse_witharg(args, i, &val, "--workers", (char*)NULL)) {
3695 opts["workers"] = val;
3696 } else if (ceph_argparse_witharg(args, i, &val, "--format", (char*)NULL)) {
3697 opts["format"] = val;
3698 } else if (ceph_argparse_witharg(args, i, &val, "--lock-tag", (char*)NULL)) {
3699 opts["lock-tag"] = val;
3700 } else if (ceph_argparse_witharg(args, i, &val, "--lock-cookie", (char*)NULL)) {
3701 opts["lock-cookie"] = val;
3702 } else if (ceph_argparse_witharg(args, i, &val, "--lock-description", (char*)NULL)) {
3703 opts["lock-description"] = val;
3704 } else if (ceph_argparse_witharg(args, i, &val, "--lock-duration", (char*)NULL)) {
3705 opts["lock-duration"] = val;
3706 } else if (ceph_argparse_witharg(args, i, &val, "--lock-type", (char*)NULL)) {
3707 opts["lock-type"] = val;
3708 } else if (ceph_argparse_witharg(args, i, &val, "-N", "--namespace", (char*)NULL)) {
3709 opts["namespace"] = val;
3710 } else if (ceph_argparse_flag(args, i, "--all", (char*)NULL)) {
3711 opts["all"] = "true";
3712 } else if (ceph_argparse_flag(args, i, "--default", (char*)NULL)) {
3713 opts["default"] = "true";
3714 } else if (ceph_argparse_witharg(args, i, &val, "-o", "--output", (char*)NULL)) {
3715 opts["output"] = val;
3716 } else if (ceph_argparse_flag(args, i, "--write-omap", (char*)NULL)) {
3717 opts["write-dest-omap"] = "true";
3718 } else if (ceph_argparse_flag(args, i, "--write-object", (char*)NULL)) {
3719 opts["write-dest-obj"] = "true";
3720 } else if (ceph_argparse_flag(args, i, "--write-xattr", (char*)NULL)) {
3721 opts["write-dest-xattr"] = "true";
3722 } else if (ceph_argparse_flag(args, i, "--with-clones", (char*)NULL)) {
3723 opts["with-clones"] = "true";
3724 } else if (ceph_argparse_witharg(args, i, &val, "--omap-key-file", (char*)NULL)) {
3725 opts["omap-key-file"] = val;
3726 } else {
3727 if (val[0] == '-')
3728 usage_exit();
3729 ++i;
3730 }
3731 }
3732
3733 if (args.empty()) {
3734 cerr << "rados: you must give an action. Try --help" << std::endl;
3735 return 1;
3736 }
3737
3738 return rados_tool_common(opts, args);
3739 }