summaryrefslogtreecommitdiffstats
path: root/src/librbd/operation/SnapshotProtectRequest.cc
blob: f3b9e7e0b76f3eb0eafe154ba209cd9da15a078f (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/operation/SnapshotProtectRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "librbd/ImageCtx.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::SnapshotProtectRequest: "

namespace librbd {
namespace operation {

namespace {

template <typename I>
std::ostream& operator<<(std::ostream& os,
                         const typename SnapshotProtectRequest<I>::State& state) {
  switch(state) {
  case SnapshotProtectRequest<I>::STATE_PROTECT_SNAP:
    os << "PROTECT_SNAP";
    break;
  }
  return os;
}

} // anonymous namespace

template <typename I>
SnapshotProtectRequest<I>::SnapshotProtectRequest(I &image_ctx,
                                                  Context *on_finish,
						  const cls::rbd::SnapshotNamespace &snap_namespace,
						  const std::string &snap_name)
  : Request<I>(image_ctx, on_finish), m_snap_namespace(snap_namespace),
    m_snap_name(snap_name), m_state(STATE_PROTECT_SNAP) {
}

template <typename I>
void SnapshotProtectRequest<I>::send_op() {
  send_protect_snap();
}

template <typename I>
bool SnapshotProtectRequest<I>::should_complete(int r) {
  I &image_ctx = this->m_image_ctx;
  CephContext *cct = image_ctx.cct;
  ldout(cct, 5) << this << " " << __func__ << ": state=" << m_state << ", "
                << "r=" << r << dendl;
  if (r < 0) {
    if (r == -EBUSY) {
      ldout(cct, 1) << "snapshot is already protected" << dendl;
    } else {
      lderr(cct) << "encountered error: " << cpp_strerror(r) << dendl;
    }
  }
  return true;
}

template <typename I>
void SnapshotProtectRequest<I>::send_protect_snap() {
  I &image_ctx = this->m_image_ctx;
  ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock));

  CephContext *cct = image_ctx.cct;
  ldout(cct, 5) << this << " " << __func__ << dendl;

  int r = verify_and_send_protect_snap();
  if (r < 0) {
    this->async_complete(r);
    return;
  }
}

template <typename I>
int SnapshotProtectRequest<I>::verify_and_send_protect_snap() {
  I &image_ctx = this->m_image_ctx;
  std::shared_lock image_locker{image_ctx.image_lock};

  CephContext *cct = image_ctx.cct;
  if ((image_ctx.features & RBD_FEATURE_LAYERING) == 0) {
    lderr(cct) << "image must support layering" << dendl;
    return -ENOSYS;
  }

  uint64_t snap_id = image_ctx.get_snap_id(m_snap_namespace, m_snap_name);
  if (snap_id == CEPH_NOSNAP) {
    return -ENOENT;
  }

  bool is_protected;
  int r = image_ctx.is_snap_protected(snap_id, &is_protected);
  if (r < 0) {
    return r;
  }

  if (is_protected) {
    return -EBUSY;
  }

  librados::ObjectWriteOperation op;
  cls_client::set_protection_status(&op, snap_id,
                                    RBD_PROTECTION_STATUS_PROTECTED);

  librados::AioCompletion *rados_completion =
    this->create_callback_completion();
  r = image_ctx.md_ctx.aio_operate(image_ctx.header_oid, rados_completion,
                                     &op);
  ceph_assert(r == 0);
  rados_completion->release();
  return 0;
}

} // namespace operation
} // namespace librbd

template class librbd::operation::SnapshotProtectRequest<librbd::ImageCtx>;