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.
20 #include "include/buffer.h"
21 #include "include/encoding.h"
23 #include "osd/osd_types.h"
24 #include "osd/OSDMap.h"
26 typedef uint8_t sectiontype_t;
27 typedef uint32_t mymagic_t;
28 typedef int64_t mysize_t;
43 END_OF_TYPES, //Keep at the end
46 const uint16_t shortmagic = 0xffce; //goes into stream as "ceff"
47 //endmagic goes into stream as "ceff ffec"
48 const mymagic_t endmagic = (0xecff << 16) | shortmagic;
50 //The first FIXED_LENGTH bytes are a fixed
51 //portion of the export output. This includes the overall
52 //version number, and size of header and footer.
53 //THIS STRUCTURE CAN ONLY BE APPENDED TO. If it needs to expand,
54 //the version can be bumped and then anything
55 //can be added to the export format.
57 static const uint32_t super_magic = (shortmagic << 16) | shortmagic;
58 // ver = 1, Initial version
59 // ver = 2, Add OSDSuperblock to pg_begin
60 static const uint32_t super_ver = 2;
61 static const uint32_t FIXED_LENGTH = 16;
67 super_header() : magic(0), version(0), header_size(0), footer_size(0) { }
69 void encode(bufferlist& bl) const {
71 ::encode(version, bl);
72 ::encode(header_size, bl);
73 ::encode(footer_size, bl);
75 void decode(bufferlist::iterator& bl) {
77 ::decode(version, bl);
78 ::decode(header_size, bl);
79 ::decode(footer_size, bl);
86 header(sectiontype_t type, mysize_t size) :
87 type(type), size(size) { }
88 header(): type(0), size(0) { }
90 void encode(bufferlist& bl) const {
91 uint32_t debug_type = (type << 24) | (type << 16) | shortmagic;
92 ENCODE_START(1, 1, bl);
93 ::encode(debug_type, bl);
97 void decode(bufferlist::iterator& bl) {
100 ::decode(debug_type, bl);
101 type = debug_type >> 24;
109 footer() : magic(endmagic) { }
111 void encode(bufferlist& bl) const {
112 ENCODE_START(1, 1, bl);
116 void decode(bufferlist::iterator& bl) {
125 OSDSuperblock superblock;
127 pg_begin(spg_t pg, const OSDSuperblock& sb):
128 pgid(pg), superblock(sb) { }
131 void encode(bufferlist& bl) const {
132 // If superblock doesn't include CEPH_FS_FEATURE_INCOMPAT_SHARDS then
133 // shard will be NO_SHARD for a replicated pool. This means
134 // that we allow the decode by struct_v 2.
135 ENCODE_START(3, 2, bl);
136 ::encode(pgid.pgid, bl);
137 ::encode(superblock, bl);
138 ::encode(pgid.shard, bl);
141 // NOTE: New super_ver prevents decode from ver 1
142 void decode(bufferlist::iterator& bl) {
144 ::decode(pgid.pgid, bl);
146 ::decode(superblock, bl);
149 ::decode(pgid.shard, bl);
151 pgid.shard = shard_id_t::NO_SHARD;
157 struct object_begin {
160 // Duplicate what is in the OI_ATTR so we have it at the start
161 // of object processing.
164 explicit object_begin(const ghobject_t &hoid): hoid(hoid) { }
167 // If superblock doesn't include CEPH_FS_FEATURE_INCOMPAT_SHARDS then
168 // generation will be NO_GEN, shard_id will be NO_SHARD for a replicated
169 // pool. This means we will allow the decode by struct_v 1.
170 void encode(bufferlist& bl) const {
171 ENCODE_START(3, 1, bl);
172 ::encode(hoid.hobj, bl);
173 ::encode(hoid.generation, bl);
174 ::encode(hoid.shard_id, bl);
175 ::encode(oi, bl, -1); /* FIXME: we always encode with full features */
178 void decode(bufferlist::iterator& bl) {
180 ::decode(hoid.hobj, bl);
182 ::decode(hoid.generation, bl);
183 ::decode(hoid.shard_id, bl);
185 hoid.generation = ghobject_t::NO_GEN;
186 hoid.shard_id = shard_id_t::NO_SHARD;
195 struct data_section {
199 data_section(uint64_t offset, uint64_t len, bufferlist bl):
200 offset(offset), len(len), databl(bl) { }
201 data_section(): offset(0), len(0) { }
203 void encode(bufferlist& bl) const {
204 ENCODE_START(1, 1, bl);
205 ::encode(offset, bl);
207 ::encode(databl, bl);
210 void decode(bufferlist::iterator& bl) {
212 ::decode(offset, bl);
214 ::decode(databl, bl);
219 struct attr_section {
220 map<string,bufferlist> data;
221 explicit attr_section(const map<string,bufferlist> &data) : data(data) { }
222 explicit attr_section(map<string, bufferptr> &data_)
224 for (std::map<std::string, bufferptr>::iterator i = data_.begin();
225 i != data_.end(); ++i) {
227 bl.push_front(i->second);
234 void encode(bufferlist& bl) const {
235 ENCODE_START(1, 1, bl);
239 void decode(bufferlist::iterator& bl) {
246 struct omap_hdr_section {
248 explicit omap_hdr_section(bufferlist hdr) : hdr(hdr) { }
249 omap_hdr_section() { }
251 void encode(bufferlist& bl) const {
252 ENCODE_START(1, 1, bl);
256 void decode(bufferlist::iterator& bl) {
263 struct omap_section {
264 map<string, bufferlist> omap;
265 explicit omap_section(const map<string, bufferlist> &omap) :
269 void encode(bufferlist& bl) const {
270 ENCODE_START(1, 1, bl);
274 void decode(bufferlist::iterator& bl) {
281 struct metadata_section {
282 // struct_ver is the on-disk version of original pg
283 __u8 struct_ver; // for reference
287 PastIntervals past_intervals;
289 bufferlist osdmap_bl; // Used in lieu of encoding osdmap due to crc checking
290 map<eversion_t, hobject_t> divergent_priors;
291 pg_missing_t missing;
296 const pg_info_t &info,
298 const PastIntervals &past_intervals,
299 const pg_missing_t &missing)
300 : struct_ver(struct_ver),
301 map_epoch(map_epoch),
304 past_intervals(past_intervals),
310 void encode(bufferlist& bl) const {
311 ENCODE_START(6, 6, bl);
312 ::encode(struct_ver, bl);
313 ::encode(map_epoch, bl);
316 ::encode(past_intervals, bl);
317 // Equivalent to osdmap.encode(bl, features); but
318 // preserving exact layout for CRC checking.
319 bl.append(osdmap_bl);
320 ::encode(divergent_priors, bl);
321 ::encode(missing, bl);
324 void decode(bufferlist::iterator& bl) {
326 ::decode(struct_ver, bl);
327 ::decode(map_epoch, bl);
331 ::decode(past_intervals, bl);
332 } else if (struct_v > 1) {
333 past_intervals.decode_classic(bl);
335 cout << "NOTICE: Older export without past_intervals" << std::endl;
340 cout << "WARNING: Older export without OSDMap information" << std::endl;
343 ::decode(divergent_priors, bl);
346 ::decode(missing, bl);
353 * Superclass for classes that will need to handle a serialized RADOS
354 * dump. Requires that the serialized dump be opened with a known FD.
364 RadosDump(int file_fd_, bool dry_run_)
365 : file_fd(file_fd_), dry_run(dry_run_)
369 int get_header(header *h);
370 int get_footer(footer *f);
371 int read_section(sectiontype_t *type, bufferlist *bl);
372 int skip_object(bufferlist &bl);
375 // Define this in .h because it's templated
376 template <typename T>
377 int write_section(sectiontype_t type, const T& obj, int fd) {
380 bufferlist blhdr, bl, blftr;
382 header hdr(type, bl.length());
387 int ret = blhdr.write_fd(fd);
389 ret = bl.write_fd(fd);
391 ret = blftr.write_fd(fd);
395 int write_simple(sectiontype_t type, int fd)
403 return hbl.write_fd(fd);