]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/rbd/action/Export.cc
update sources to 12.2.7
[ceph.git] / ceph / src / tools / rbd / action / Export.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#include "include/compat.h"
5#include "tools/rbd/ArgumentTypes.h"
6#include "tools/rbd/Shell.h"
7#include "tools/rbd/Utils.h"
8#include "include/Context.h"
9#include "common/errno.h"
10#include "common/Throttle.h"
11#include "include/encoding.h"
12#include <iostream>
13#include <fcntl.h>
14#include <stdlib.h>
15#include <boost/program_options.hpp>
16#include <boost/scope_exit.hpp>
17
18namespace rbd {
19namespace action {
20namespace export_full {
21
22struct ExportDiffContext {
23 librbd::Image *image;
24 int fd;
25 int export_format;
26 uint64_t totalsize;
27 utils::ProgressContext pc;
28 OrderedThrottle throttle;
29
30 ExportDiffContext(librbd::Image *i, int f, uint64_t t, int max_ops,
31 bool no_progress, int eformat) :
32 image(i), fd(f), export_format(eformat), totalsize(t), pc("Exporting image", no_progress),
33 throttle(max_ops, true) {
34 }
35};
36
37class C_ExportDiff : public Context {
38public:
39 C_ExportDiff(ExportDiffContext *edc, uint64_t offset, uint64_t length,
40 bool exists, int export_format)
41 : m_export_diff_context(edc), m_offset(offset), m_length(length),
42 m_exists(exists), m_export_format(export_format) {
43 }
44
45 int send() {
46 if (m_export_diff_context->throttle.pending_error()) {
47 return m_export_diff_context->throttle.wait_for_ret();
48 }
49
50 C_OrderedThrottle *ctx = m_export_diff_context->throttle.start_op(this);
51 if (m_exists) {
52 librbd::RBD::AioCompletion *aio_completion =
53 new librbd::RBD::AioCompletion(ctx, &utils::aio_context_callback);
54
55 int op_flags = LIBRADOS_OP_FLAG_FADVISE_NOCACHE;
56 int r = m_export_diff_context->image->aio_read2(
57 m_offset, m_length, m_read_data, aio_completion, op_flags);
58 if (r < 0) {
59 aio_completion->release();
60 ctx->complete(r);
61 }
62 } else {
63 ctx->complete(0);
64 }
65 return 0;
66 }
67
68 static int export_diff_cb(uint64_t offset, size_t length, int exists,
69 void *arg) {
70 ExportDiffContext *edc = reinterpret_cast<ExportDiffContext *>(arg);
71
72 C_ExportDiff *context = new C_ExportDiff(edc, offset, length, exists, edc->export_format);
73 return context->send();
74 }
75
76protected:
77 void finish(int r) override {
78 if (r >= 0) {
79 if (m_exists) {
80 m_exists = !m_read_data.is_zero();
81 }
82 r = write_extent(m_export_diff_context, m_offset, m_length, m_exists, m_export_format);
83 if (r == 0 && m_exists) {
84 r = m_read_data.write_fd(m_export_diff_context->fd);
85 }
86 }
87 m_export_diff_context->throttle.end_op(r);
88 }
89
90private:
91 ExportDiffContext *m_export_diff_context;
92 uint64_t m_offset;
93 uint64_t m_length;
94 bool m_exists;
95 int m_export_format;
96 bufferlist m_read_data;
97
98 static int write_extent(ExportDiffContext *edc, uint64_t offset,
99 uint64_t length, bool exists, int export_format) {
100 // extent
101 bufferlist bl;
102 __u8 tag = exists ? RBD_DIFF_WRITE : RBD_DIFF_ZERO;
103 uint64_t len = 0;
104 ::encode(tag, bl);
105 if (export_format == 2) {
106 if (tag == RBD_DIFF_WRITE)
107 len = 8 + 8 + length;
108 else
109 len = 8 + 8;
110 ::encode(len, bl);
111 }
112 ::encode(offset, bl);
113 ::encode(length, bl);
114 int r = bl.write_fd(edc->fd);
115
116 edc->pc.update_progress(offset, edc->totalsize);
117 return r;
118 }
119};
120
121
122int do_export_diff_fd(librbd::Image& image, const char *fromsnapname,
123 const char *endsnapname, bool whole_object,
124 int fd, bool no_progress, int export_format)
125{
126 int r;
127 librbd::image_info_t info;
128
129 r = image.stat(info, sizeof(info));
130 if (r < 0)
131 return r;
132
133 {
134 // header
135 bufferlist bl;
136 if (export_format == 1)
137 bl.append(utils::RBD_DIFF_BANNER);
138 else
139 bl.append(utils::RBD_DIFF_BANNER_V2);
140
141 __u8 tag;
142 uint64_t len = 0;
143 if (fromsnapname) {
144 tag = RBD_DIFF_FROM_SNAP;
145 ::encode(tag, bl);
146 std::string from(fromsnapname);
147 if (export_format == 2) {
148 len = from.length() + 4;
149 ::encode(len, bl);
150 }
151 ::encode(from, bl);
152 }
153
154 if (endsnapname) {
155 tag = RBD_DIFF_TO_SNAP;
156 ::encode(tag, bl);
157 std::string to(endsnapname);
158 if (export_format == 2) {
28e407b8
AA
159 len = to.length() + 4;
160 ::encode(len, bl);
7c673cae
FG
161 }
162 ::encode(to, bl);
163 }
164
28e407b8
AA
165 if (endsnapname && export_format == 2) {
166 tag = RBD_SNAP_PROTECTION_STATUS;
167 encode(tag, bl);
168 bool is_protected = false;
169 r = image.snap_is_protected(endsnapname, &is_protected);
170 if (r < 0) {
171 return r;
172 }
173 len = 8;
174 encode(len, bl);
175 encode(is_protected, bl);
176 }
177
7c673cae
FG
178 tag = RBD_DIFF_IMAGE_SIZE;
179 ::encode(tag, bl);
180 uint64_t endsize = info.size;
181 if (export_format == 2) {
182 len = 8;
183 ::encode(len, bl);
184 }
185 ::encode(endsize, bl);
186
187 r = bl.write_fd(fd);
188 if (r < 0) {
189 return r;
190 }
191 }
192 ExportDiffContext edc(&image, fd, info.size,
181888fb
FG
193 g_conf->get_val<int64_t>("rbd_concurrent_management_ops"),
194 no_progress, export_format);
7c673cae
FG
195 r = image.diff_iterate2(fromsnapname, 0, info.size, true, whole_object,
196 &C_ExportDiff::export_diff_cb, (void *)&edc);
197 if (r < 0) {
198 goto out;
199 }
200
201 r = edc.throttle.wait_for_ret();
202 if (r < 0) {
203 goto out;
204 }
205
206 {
207 __u8 tag = RBD_DIFF_END;
208 bufferlist bl;
209 ::encode(tag, bl);
210 r = bl.write_fd(fd);
211 }
212
213out:
214 if (r < 0)
215 edc.pc.fail();
216 else
217 edc.pc.finish();
218
219 return r;
220}
221
222int do_export_diff(librbd::Image& image, const char *fromsnapname,
223 const char *endsnapname, bool whole_object,
224 const char *path, bool no_progress)
225{
226 int r;
227 int fd;
228
229 if (strcmp(path, "-") == 0)
230 fd = STDOUT_FILENO;
231 else
232 fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
233 if (fd < 0)
234 return -errno;
235
236 r = do_export_diff_fd(image, fromsnapname, endsnapname, whole_object, fd, no_progress, 1);
237
238 if (fd != 1)
239 close(fd);
240 if (r < 0 && fd != 1) {
241 remove(path);
242 }
243
244 return r;
245}
246
247
248namespace at = argument_types;
249namespace po = boost::program_options;
250
251void get_arguments_diff(po::options_description *positional,
252 po::options_description *options) {
253 at::add_image_or_snap_spec_options(positional, options,
254 at::ARGUMENT_MODIFIER_SOURCE);
255 at::add_path_options(positional, options,
256 "export file (or '-' for stdout)");
257 options->add_options()
258 (at::FROM_SNAPSHOT_NAME.c_str(), po::value<std::string>(),
259 "snapshot starting point")
260 (at::WHOLE_OBJECT.c_str(), po::bool_switch(), "compare whole object");
261 at::add_no_progress_option(options);
262}
263
264int execute_diff(const po::variables_map &vm) {
265 size_t arg_index = 0;
266 std::string pool_name;
267 std::string image_name;
268 std::string snap_name;
269 int r = utils::get_pool_image_snapshot_names(
270 vm, at::ARGUMENT_MODIFIER_SOURCE, &arg_index, &pool_name, &image_name,
271 &snap_name, utils::SNAPSHOT_PRESENCE_PERMITTED,
272 utils::SPEC_VALIDATION_NONE);
273 if (r < 0) {
274 return r;
275 }
276
277 std::string path;
278 r = utils::get_path(vm, utils::get_positional_argument(vm, 1), &path);
279 if (r < 0) {
280 return r;
281 }
282
283 std::string from_snap_name;
284 if (vm.count(at::FROM_SNAPSHOT_NAME)) {
285 from_snap_name = vm[at::FROM_SNAPSHOT_NAME].as<std::string>();
286 }
287
288 librados::Rados rados;
289 librados::IoCtx io_ctx;
290 librbd::Image image;
291 r = utils::init_and_open_image(pool_name, image_name, "", snap_name, true,
292 &rados, &io_ctx, &image);
293 if (r < 0) {
294 return r;
295 }
296
297 r = do_export_diff(image,
298 from_snap_name.empty() ? nullptr : from_snap_name.c_str(),
299 snap_name.empty() ? nullptr : snap_name.c_str(),
300 vm[at::WHOLE_OBJECT].as<bool>(), path.c_str(),
301 vm[at::NO_PROGRESS].as<bool>());
302 if (r < 0) {
303 std::cerr << "rbd: export-diff error: " << cpp_strerror(r) << std::endl;
304 return r;
305 }
306 return 0;
307}
308
309Shell::SwitchArguments switched_arguments({at::WHOLE_OBJECT});
310Shell::Action action_diff(
311 {"export-diff"}, {}, "Export incremental diff to file.", "",
312 &get_arguments_diff, &execute_diff);
313
314class C_Export : public Context
315{
316public:
317 C_Export(SimpleThrottle &simple_throttle, librbd::Image &image,
318 uint64_t fd_offset, uint64_t offset, uint64_t length, int fd)
319 : m_aio_completion(
320 new librbd::RBD::AioCompletion(this, &utils::aio_context_callback)),
321 m_throttle(simple_throttle), m_image(image), m_dest_offset(fd_offset),
322 m_offset(offset), m_length(length), m_fd(fd)
323 {
324 }
325
326 void send()
327 {
328 m_throttle.start_op();
329
330 int op_flags = LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL |
331 LIBRADOS_OP_FLAG_FADVISE_NOCACHE;
332 int r = m_image.aio_read2(m_offset, m_length, m_bufferlist,
333 m_aio_completion, op_flags);
334 if (r < 0) {
335 cerr << "rbd: error requesting read from source image" << std::endl;
336 m_aio_completion->release();
337 m_throttle.end_op(r);
338 }
339 }
340
341 void finish(int r) override
342 {
343 BOOST_SCOPE_EXIT((&m_throttle) (&r))
344 {
345 m_throttle.end_op(r);
346 } BOOST_SCOPE_EXIT_END
347
348 if (r < 0) {
349 cerr << "rbd: error reading from source image at offset "
350 << m_offset << ": " << cpp_strerror(r) << std::endl;
351 return;
352 }
353
354 assert(m_bufferlist.length() == static_cast<size_t>(r));
355 if (m_fd != STDOUT_FILENO) {
356 if (m_bufferlist.is_zero()) {
357 return;
358 }
359
360 uint64_t chkret = lseek64(m_fd, m_dest_offset, SEEK_SET);
361 if (chkret != m_dest_offset) {
362 cerr << "rbd: error seeking destination image to offset "
363 << m_dest_offset << std::endl;
364 r = -errno;
365 return;
366 }
367 }
368
369 r = m_bufferlist.write_fd(m_fd);
370 if (r < 0) {
371 cerr << "rbd: error writing to destination image at offset "
372 << m_dest_offset << std::endl;
373 }
374 }
375
376private:
377 librbd::RBD::AioCompletion *m_aio_completion;
378 SimpleThrottle &m_throttle;
379 librbd::Image &m_image;
380 bufferlist m_bufferlist;
381 uint64_t m_dest_offset;
382 uint64_t m_offset;
383 uint64_t m_length;
384 int m_fd;
385};
386
28e407b8
AA
387const uint32_t MAX_KEYS = 64;
388
7c673cae
FG
389static int do_export_v2(librbd::Image& image, librbd::image_info_t &info, int fd,
390 uint64_t period, int max_concurrent_ops, utils::ProgressContext &pc)
391{
392 int r = 0;
393 // header
394 bufferlist bl;
395 bl.append(utils::RBD_IMAGE_BANNER_V2);
396
397 __u8 tag;
398 uint64_t length;
399 // encode order
400 tag = RBD_EXPORT_IMAGE_ORDER;
401 length = 8;
402 ::encode(tag, bl);
403 ::encode(length, bl);
404 ::encode(uint64_t(info.order), bl);
405
406 // encode features
407 tag = RBD_EXPORT_IMAGE_FEATURES;
408 uint64_t features;
409 image.features(&features);
410 length = 8;
411 ::encode(tag, bl);
412 ::encode(length, bl);
413 ::encode(features, bl);
414
415 // encode stripe_unit and stripe_count
416 tag = RBD_EXPORT_IMAGE_STRIPE_UNIT;
417 uint64_t stripe_unit;
418 stripe_unit = image.get_stripe_unit();
419 length = 8;
420 ::encode(tag, bl);
421 ::encode(length, bl);
422 ::encode(stripe_unit, bl);
423
424 tag = RBD_EXPORT_IMAGE_STRIPE_COUNT;
425 uint64_t stripe_count;
426 stripe_count = image.get_stripe_count();
427 length = 8;
428 ::encode(tag, bl);
429 ::encode(length, bl);
430 ::encode(stripe_count, bl);
431
28e407b8
AA
432 //retrieve metadata of image
433 std::map<std::string, string> imagemetas;
434 std::string last_key;
435 bool more_results = true;
436 while (more_results) {
437 std::map<std::string, bufferlist> pairs;
438 r = image.metadata_list(last_key, MAX_KEYS, &pairs);
439 if (r < 0) {
440 std::cerr << "failed to retrieve metadata of image : " << cpp_strerror(r)
441 << std::endl;
442 return r;
443 }
444
445 if (!pairs.empty()) {
446 last_key = pairs.rbegin()->first;
447
448 for (auto kv : pairs) {
449 std::string key = kv.first;
450 std::string val(kv.second.c_str(), kv.second.length());
451 imagemetas[key] = val;
452 }
453 }
454 more_results = (pairs.size() == MAX_KEYS);
455 }
456
457 //encode imageMeta key and value
458 for (std::map<std::string, string>::iterator it = imagemetas.begin();
459 it != imagemetas.end(); ++it) {
460 string key = it->first;
461 string value = it->second;
462
463 tag = RBD_EXPORT_IMAGE_META;
464 length = key.length() + value.length() + 4 * 2;
465 ::encode(tag, bl);
466 ::encode(length, bl);
467 ::encode(key, bl);
468 ::encode(value, bl);
469 }
470
7c673cae
FG
471 // encode end tag
472 tag = RBD_EXPORT_IMAGE_END;
473 ::encode(tag, bl);
474
475 // write bl to fd.
476 r = bl.write_fd(fd);
477 if (r < 0) {
478 return r;
479 }
480
481 // header for snapshots
482 bl.clear();
483 bl.append(utils::RBD_IMAGE_DIFFS_BANNER_V2);
484
485 std::vector<librbd::snap_info_t> snaps;
486 r = image.snap_list(snaps);
487 if (r < 0) {
488 return r;
489 }
490
491 uint64_t diff_num = snaps.size() + 1;
492 ::encode(diff_num, bl);
493
494 r = bl.write_fd(fd);
495 if (r < 0) {
496 return r;
497 }
498
499 const char *last_snap = NULL;
500 for (size_t i = 0; i < snaps.size(); ++i) {
501 utils::snap_set(image, snaps[i].name.c_str());
502 r = do_export_diff_fd(image, last_snap, snaps[i].name.c_str(), false, fd, true, 2);
503 if (r < 0) {
504 return r;
505 }
506 pc.update_progress(i, snaps.size() + 1);
507 last_snap = snaps[i].name.c_str();
508 }
509 utils::snap_set(image, std::string(""));
510 r = do_export_diff_fd(image, last_snap, nullptr, false, fd, true, 2);
511 if (r < 0) {
512 return r;
513 }
514 pc.update_progress(snaps.size() + 1, snaps.size() + 1);
515 return r;
516}
517
518static int do_export_v1(librbd::Image& image, librbd::image_info_t &info, int fd,
519 uint64_t period, int max_concurrent_ops, utils::ProgressContext &pc)
520{
521 int r = 0;
522 size_t file_size = 0;
523 SimpleThrottle throttle(max_concurrent_ops, false);
524 for (uint64_t offset = 0; offset < info.size; offset += period) {
525 if (throttle.pending_error()) {
526 break;
527 }
528
529 uint64_t length = min(period, info.size - offset);
530 C_Export *ctx = new C_Export(throttle, image, file_size + offset, offset, length, fd);
531 ctx->send();
532
533 pc.update_progress(offset, info.size);
534 }
535
536 file_size += info.size;
537 r = throttle.wait_for_ret();
538 if (fd != 1) {
539 if (r >= 0) {
540 r = ftruncate(fd, file_size);
541 if (r < 0)
542 return r;
543
544 uint64_t chkret = lseek64(fd, file_size, SEEK_SET);
545 if (chkret != file_size)
546 r = errno;
547 }
548 }
549 return r;
550}
551
552static int do_export(librbd::Image& image, const char *path, bool no_progress, int export_format)
553{
554 librbd::image_info_t info;
555 int64_t r = image.stat(info, sizeof(info));
556 if (r < 0)
557 return r;
558
559 int fd;
560 int max_concurrent_ops;
561 bool to_stdout = (strcmp(path, "-") == 0);
562 if (to_stdout) {
563 fd = STDOUT_FILENO;
564 max_concurrent_ops = 1;
565 } else {
181888fb 566 max_concurrent_ops = g_conf->get_val<int64_t>("rbd_concurrent_management_ops");
7c673cae
FG
567 fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
568 if (fd < 0) {
569 return -errno;
570 }
571#ifdef HAVE_POSIX_FADVISE
572 posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
573#endif
574 }
575
576 utils::ProgressContext pc("Exporting image", no_progress);
577 uint64_t period = image.get_stripe_count() * (1ull << info.order);
578
579 if (export_format == 1)
580 r = do_export_v1(image, info, fd, period, max_concurrent_ops, pc);
581 else
582 r = do_export_v2(image, info, fd, period, max_concurrent_ops, pc);
583
584 if (r < 0)
585 pc.fail();
586 else
587 pc.finish();
588 if (!to_stdout)
589 close(fd);
590 return r;
591}
592
593void get_arguments(po::options_description *positional,
594 po::options_description *options) {
595 at::add_image_or_snap_spec_options(positional, options,
596 at::ARGUMENT_MODIFIER_SOURCE);
597 at::add_path_options(positional, options,
598 "export file (or '-' for stdout)");
599 at::add_no_progress_option(options);
600 at::add_export_format_option(options);
601}
602
603int execute(const po::variables_map &vm) {
604 size_t arg_index = 0;
605 std::string pool_name;
606 std::string image_name;
607 std::string snap_name;
608 int r = utils::get_pool_image_snapshot_names(
609 vm, at::ARGUMENT_MODIFIER_SOURCE, &arg_index, &pool_name, &image_name,
610 &snap_name, utils::SNAPSHOT_PRESENCE_PERMITTED,
611 utils::SPEC_VALIDATION_NONE);
612 if (r < 0) {
613 return r;
614 }
615
616 std::string path;
617 r = utils::get_path(vm, utils::get_positional_argument(vm, 1), &path);
618 if (r < 0) {
619 return r;
620 }
621
622 librados::Rados rados;
623 librados::IoCtx io_ctx;
624 librbd::Image image;
625 r = utils::init_and_open_image(pool_name, image_name, "", snap_name, true,
626 &rados, &io_ctx, &image);
627 if (r < 0) {
628 return r;
629 }
630
631 int format = 1;
632 if (vm.count("export-format"))
633 format = vm["export-format"].as<uint64_t>();
634
635 r = do_export(image, path.c_str(), vm[at::NO_PROGRESS].as<bool>(), format);
636 if (r < 0) {
637 std::cerr << "rbd: export error: " << cpp_strerror(r) << std::endl;
638 return r;
639 }
640 return 0;
641}
642
643Shell::Action action(
644 {"export"}, {}, "Export image to file.", "", &get_arguments, &execute);
645
646} // namespace export_full
647} // namespace action
648} // namespace rbd