Skip to content

Commit

Permalink
rbd: import with option --export-format fails to protect snapshot
Browse files Browse the repository at this point in the history
Fixes: http://tracker.ceph.com/issues/23038

Signed-off-by: songweibin <song.weibin@zte.com.cn>
  • Loading branch information
Songweibin committed Mar 1, 2018
1 parent a2a6f60 commit 4376c7a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
14 changes: 13 additions & 1 deletion doc/dev/rbd-export.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,21 @@ End


Diffs records
~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~

Record the all snapshots and the HEAD in this section.

Snap Protection status
----------------------

Record the snapshot's protection status if `--export-format=2`.
- u8: 'p'
- le64: length of appending data (8)
- u8: snap protection status (0 for false, 1 for true)

Others
------

- le64: number of diffs
- Diffs ...

Expand Down
2 changes: 2 additions & 0 deletions src/tools/rbd/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ static const std::string RBD_DIFF_BANNER_V2 ("rbd diff v2\n");
#define RBD_DIFF_ZERO 'z'
#define RBD_DIFF_END 'e'

#define RBD_SNAP_PROTECTION_STATUS 'p'

#define RBD_EXPORT_IMAGE_ORDER 'O'
#define RBD_EXPORT_IMAGE_FEATURES 'T'
#define RBD_EXPORT_IMAGE_STRIPE_UNIT 'U'
Expand Down
17 changes: 15 additions & 2 deletions src/tools/rbd/action/Export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,25 @@ int do_export_diff_fd(librbd::Image& image, const char *fromsnapname,
encode(tag, bl);
std::string to(endsnapname);
if (export_format == 2) {
len = to.length() + 4;
encode(len, bl);
len = to.length() + 4;
encode(len, bl);
}
encode(to, bl);
}

if (endsnapname && export_format == 2) {
tag = RBD_SNAP_PROTECTION_STATUS;
encode(tag, bl);
bool is_protected = false;
r = image.snap_is_protected(endsnapname, &is_protected);
if (r < 0) {
return r;
}
len = 8;
encode(len, bl);
encode(is_protected, bl);
}

tag = RBD_DIFF_IMAGE_SIZE;
encode(tag, bl);
uint64_t endsize = info.size;
Expand Down
20 changes: 20 additions & 0 deletions src/tools/rbd/action/Import.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ static int do_image_snap_to(ImportDiffContext *idiffctx, std::string *tosnap)
return 0;
}

static int get_snap_protection_status(ImportDiffContext *idiffctx, bool *is_protected)
{
int r;
char buf[sizeof(__u8)];
r = safe_read_exact(idiffctx->fd, buf, sizeof(buf));
if (r < 0) {
return r;
}

*is_protected = (buf[0] != 0);
idiffctx->update_progress();

return 0;
}

static int do_image_resize(ImportDiffContext *idiffctx)
{
int r;
Expand Down Expand Up @@ -371,6 +386,7 @@ int do_import_diff_fd(librados::Rados &rados, librbd::Image &image, int fd,

// begin image import
std::string tosnap;
bool is_protected = false;
ImportDiffContext idiffctx(&image, fd, size, no_progress);
while (r == 0) {
__u8 tag;
Expand All @@ -385,6 +401,8 @@ int do_import_diff_fd(librados::Rados &rados, librbd::Image &image, int fd,
r = do_image_snap_from(&idiffctx);
} else if (tag == RBD_DIFF_TO_SNAP) {
r = do_image_snap_to(&idiffctx, &tosnap);
} else if (tag == RBD_SNAP_PROTECTION_STATUS) {
r = get_snap_protection_status(&idiffctx, &is_protected);
} else if (tag == RBD_DIFF_IMAGE_SIZE) {
r = do_image_resize(&idiffctx);
} else if (tag == RBD_DIFF_WRITE || tag == RBD_DIFF_ZERO) {
Expand All @@ -400,6 +418,8 @@ int do_import_diff_fd(librados::Rados &rados, librbd::Image &image, int fd,
r = (r < 0) ? r : temp_r; // preserve original error
if (r == 0 && tosnap.length()) {
idiffctx.image->snap_create(tosnap.c_str());
if (is_protected)
idiffctx.image->snap_protect(tosnap.c_str());
}

idiffctx.finish(r);
Expand Down

0 comments on commit 4376c7a

Please sign in to comment.