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

#include "librbd/image/SetFlagsRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/ImageCtx.h"
#include "librbd/Utils.h"
#include "include/ceph_assert.h"

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

namespace librbd {
namespace image {

using util::create_context_callback;
using util::create_rados_callback;

template <typename I>
SetFlagsRequest<I>::SetFlagsRequest(I *image_ctx, uint64_t flags,
				    uint64_t mask, Context *on_finish)
  : m_image_ctx(image_ctx), m_flags(flags), m_mask(mask),
    m_on_finish(on_finish) {
}

template <typename I>
void SetFlagsRequest<I>::send() {
  send_set_flags();
}

template <typename I>
void SetFlagsRequest<I>::send_set_flags() {
  CephContext *cct = m_image_ctx->cct;
  ldout(cct, 20) << __func__ << dendl;

  std::unique_lock image_locker{m_image_ctx->image_lock};
  std::vector<uint64_t> snap_ids;
  snap_ids.push_back(CEPH_NOSNAP);
  for (auto it : m_image_ctx->snap_info) {
    snap_ids.push_back(it.first);
  }

  Context *ctx = create_context_callback<
    SetFlagsRequest<I>, &SetFlagsRequest<I>::handle_set_flags>(this);
  C_Gather *gather_ctx = new C_Gather(cct, ctx);

  for (auto snap_id : snap_ids) {
    librados::ObjectWriteOperation op;
    cls_client::set_flags(&op, snap_id, m_flags, m_mask);

    librados::AioCompletion *comp =
      create_rados_callback(gather_ctx->new_sub());
    int r = m_image_ctx->md_ctx.aio_operate(m_image_ctx->header_oid, comp, &op);
    ceph_assert(r == 0);
    comp->release();
  }
  gather_ctx->activate();
}

template <typename I>
Context *SetFlagsRequest<I>::handle_set_flags(int *result) {
  CephContext *cct = m_image_ctx->cct;
  ldout(cct, 20) << __func__ << ": r=" << *result << dendl;

  if (*result < 0) {
    lderr(cct) << "set_flags failed: " << cpp_strerror(*result)
	       << dendl;
  }
  return m_on_finish;
}

} // namespace image
} // namespace librbd

template class librbd::image::SetFlagsRequest<librbd::ImageCtx>;