1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2015 Red Hat
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "include/rados/librados.hpp"
#include "common/errno.h"
#include "PoolDump.h"
using namespace librados;
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rados
/**
* Export RADOS objects from a live cluster
* to a serialized format via a file descriptor.
*
* @returns 0 on success, else error code
*/
int PoolDump::dump(IoCtx *io_ctx)
{
ceph_assert(io_ctx != NULL);
int r = 0;
write_super();
r = write_simple(TYPE_POOL_BEGIN, file_fd);
if (r != 0) {
return r;
}
io_ctx->set_namespace(all_nspaces);
librados::NObjectIterator i = io_ctx->nobjects_begin();
librados::NObjectIterator i_end = io_ctx->nobjects_end();
for (; i != i_end; ++i) {
const std::string oid = i->get_oid();
dout(10) << "OID '" << oid << "'" << dendl;
// Compose OBJECT_BEGIN
// ====================
object_begin obj_begin;
obj_begin.hoid.hobj.oid = i->get_oid();
obj_begin.hoid.hobj.nspace = i->get_nspace();
obj_begin.hoid.hobj.set_key(i->get_locator());
// Only output head, RadosImport only wants that
obj_begin.hoid.hobj.snap = CEPH_NOSNAP;
// Skip setting object_begin.oi, RadosImport doesn't care
r = write_section(TYPE_OBJECT_BEGIN, obj_begin, file_fd);
if (r != 0) {
return r;
}
// Compose TYPE_DATA chunks
// ========================
const uint32_t op_size = 4096 * 1024;
uint64_t offset = 0;
io_ctx->set_namespace(i->get_nspace());
io_ctx->locator_set_key(i->get_locator());
while (true) {
bufferlist outdata;
r = io_ctx->read(oid, outdata, op_size, offset);
if (r <= 0) {
// Error or no data
break;
}
r = write_section(TYPE_DATA,
data_section(offset, outdata.length(), outdata), file_fd);
if (r != 0) {
// Output stream error
return r;
}
if (outdata.length() < op_size) {
// No more data
break;
}
offset += outdata.length();
}
// Compose TYPE_ATTRS chunk
// ========================
std::map<std::string, bufferlist> raw_xattrs;
std::map<std::string, bufferlist> xattrs;
r = io_ctx->getxattrs(oid, raw_xattrs);
if (r < 0) {
cerr << "error getting xattr set " << oid << ": " << cpp_strerror(r)
<< std::endl;
return r;
}
// Prepend "_" to mimic how user keys are represented in a pg export
for (std::map<std::string, bufferlist>::iterator i = raw_xattrs.begin();
i != raw_xattrs.end(); ++i) {
std::pair< std::string, bufferlist> item(std::string("_") + std::string(i->first.c_str()), i->second);
xattrs.insert(item);
}
r = write_section(TYPE_ATTRS, attr_section(xattrs), file_fd);
if (r != 0) {
return r;
}
// Compose TYPE_OMAP_HDR section
// =============================
bufferlist omap_header;
r = io_ctx->omap_get_header(oid, &omap_header);
if (r < 0) {
cerr << "error getting omap header " << oid
<< ": " << cpp_strerror(r) << std::endl;
return r;
}
r = write_section(TYPE_OMAP_HDR, omap_hdr_section(omap_header), file_fd);
if (r != 0) {
return r;
}
// Compose TYPE_OMAP
int MAX_READ = 512;
string last_read = "";
do {
map<string, bufferlist> values;
r = io_ctx->omap_get_vals(oid, last_read, MAX_READ, &values);
if (r < 0) {
cerr << "error getting omap keys " << oid << ": "
<< cpp_strerror(r) << std::endl;
return r;
}
if (values.size()) {
last_read = values.rbegin()->first;
} else {
break;
}
r = write_section(TYPE_OMAP, omap_section(values), file_fd);
if (r != 0) {
return r;
}
r = values.size();
} while (r == MAX_READ);
// Close object
// =============
r = write_simple(TYPE_OBJECT_END, file_fd);
if (r != 0) {
return r;
}
}
r = write_simple(TYPE_POOL_END, file_fd);
#if defined(__linux__)
if (file_fd != STDOUT_FILENO)
posix_fadvise(file_fd, 0, 0, POSIX_FADV_DONTNEED);
#endif
return r;
}
|