1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2015 Red Hat
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "include/rados/librados.hpp"
16 #include "common/errno.h"
20 using namespace librados;
22 #define dout_context g_ceph_context
23 #define dout_subsys ceph_subsys_rados
26 * Export RADOS objects from a live cluster
27 * to a serialized format via a file descriptor.
29 * @returns 0 on success, else error code
31 int PoolDump::dump(IoCtx *io_ctx)
33 assert(io_ctx != NULL);
38 r = write_simple(TYPE_POOL_BEGIN, file_fd);
43 io_ctx->set_namespace(all_nspaces);
44 librados::NObjectIterator i = io_ctx->nobjects_begin();
46 librados::NObjectIterator i_end = io_ctx->nobjects_end();
47 for (; i != i_end; ++i) {
48 const std::string oid = i->get_oid();
49 dout(10) << "OID '" << oid << "'" << dendl;
51 // Compose OBJECT_BEGIN
52 // ====================
53 object_begin obj_begin;
54 obj_begin.hoid.hobj.oid = i->get_oid();
55 obj_begin.hoid.hobj.nspace = i->get_nspace();
56 obj_begin.hoid.hobj.set_key(i->get_locator());
58 // Only output head, RadosImport only wants that
59 obj_begin.hoid.hobj.snap = CEPH_NOSNAP;
61 // Skip setting object_begin.oi, RadosImport doesn't care
63 r = write_section(TYPE_OBJECT_BEGIN, obj_begin, file_fd);
68 // Compose TYPE_DATA chunks
69 // ========================
70 const uint32_t op_size = 4096 * 1024;
72 io_ctx->set_namespace(i->get_nspace());
75 r = io_ctx->read(oid, outdata, op_size, offset);
81 r = write_section(TYPE_DATA,
82 data_section(offset, outdata.length(), outdata), file_fd);
84 // Output stream error
88 if (outdata.length() < op_size) {
93 offset += outdata.length();
96 // Compose TYPE_ATTRS chunk
97 // ========================
98 std::map<std::string, bufferlist> raw_xattrs;
99 std::map<std::string, bufferlist> xattrs;
100 r = io_ctx->getxattrs(oid, raw_xattrs);
102 cerr << "error getting xattr set " << oid << ": " << cpp_strerror(r)
106 // Prepend "_" to mimic how user keys are represented in a pg export
107 for (std::map<std::string, bufferlist>::iterator i = raw_xattrs.begin();
108 i != raw_xattrs.end(); ++i) {
109 std::pair< std::string, bufferlist> item(std::string("_") + std::string(i->first.c_str()), i->second);
112 r = write_section(TYPE_ATTRS, attr_section(xattrs), file_fd);
117 // Compose TYPE_OMAP_HDR section
118 // =============================
119 bufferlist omap_header;
120 r = io_ctx->omap_get_header(oid, &omap_header);
122 cerr << "error getting omap header " << oid
123 << ": " << cpp_strerror(r) << std::endl;
126 r = write_section(TYPE_OMAP_HDR, omap_hdr_section(omap_header), file_fd);
133 string last_read = "";
135 map<string, bufferlist> values;
136 r = io_ctx->omap_get_vals(oid, last_read, MAX_READ, &values);
138 cerr << "error getting omap keys " << oid << ": "
139 << cpp_strerror(r) << std::endl;
143 last_read = values.rbegin()->first;
148 r = write_section(TYPE_OMAP, omap_section(values), file_fd);
153 } while (r == MAX_READ);
158 r = write_simple(TYPE_OBJECT_END, file_fd);
164 r = write_simple(TYPE_POOL_END, file_fd);
165 #if defined(__linux__)
166 if (file_fd != STDOUT_FILENO)
167 posix_fadvise(file_fd, 0, 0, POSIX_FADV_DONTNEED);