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

#include "librbd/object_map/InvalidateRequest.h"
#include "common/dout.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"

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

namespace librbd {
namespace object_map {

template <typename I>
InvalidateRequest<I>* InvalidateRequest<I>::create(I &image_ctx,
                                                   uint64_t snap_id, bool force,
                                                   Context *on_finish) {
  return new InvalidateRequest<I>(image_ctx, snap_id, force, on_finish);
}

template <typename I>
void InvalidateRequest<I>::send() {
  I &image_ctx = this->m_image_ctx;
  ceph_assert(image_ctx.owner_lock.is_locked());
  ceph_assert(image_ctx.snap_lock.is_wlocked());

  uint64_t snap_flags;
  int r = image_ctx.get_flags(m_snap_id, &snap_flags);
  if (r < 0 || ((snap_flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0)) {
    this->async_complete(r);
    return;
  }

  CephContext *cct = image_ctx.cct;
  lderr(cct) << this << " invalidating object map in-memory" << dendl;

  // update in-memory flags
  uint64_t flags = RBD_FLAG_OBJECT_MAP_INVALID;
  if ((image_ctx.features & RBD_FEATURE_FAST_DIFF) != 0) {
    flags |= RBD_FLAG_FAST_DIFF_INVALID;
  }

  r = image_ctx.update_flags(m_snap_id, flags, true);
  if (r < 0) {
    this->async_complete(r);
    return;
  }

  // do not update on-disk flags if not image owner
  if (image_ctx.image_watcher == nullptr ||
      (!m_force && m_snap_id == CEPH_NOSNAP &&
       image_ctx.exclusive_lock != nullptr &&
       !image_ctx.exclusive_lock->is_lock_owner())) {
    this->async_complete(-EROFS);
    return;
  }

  lderr(cct) << this << " invalidating object map on-disk" << dendl;
  librados::ObjectWriteOperation op;
  cls_client::set_flags(&op, m_snap_id, flags, flags);

  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();
}

template <typename I>
bool InvalidateRequest<I>::should_complete(int r) {
  I &image_ctx = this->m_image_ctx;
  CephContext *cct = image_ctx.cct;
  lderr(cct) << this << " " << __func__ << ": r=" << r << dendl;
  return true;
}

} // namespace object_map
} // namespace librbd

template class librbd::object_map::InvalidateRequest<librbd::ImageCtx>;