Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / object_map / InvalidateRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "librbd/object_map/InvalidateRequest.h"
5 #include "common/dout.h"
6 #include "librbd/ExclusiveLock.h"
7 #include "librbd/ImageCtx.h"
8
9 #define dout_subsys ceph_subsys_rbd
10 #undef dout_prefix
11 #define dout_prefix *_dout << "librbd::object_map::InvalidateRequest: "
12
13 namespace librbd {
14 namespace object_map {
15
16 template <typename I>
17 InvalidateRequest<I>* InvalidateRequest<I>::create(I &image_ctx,
18                                                    uint64_t snap_id, bool force,
19                                                    Context *on_finish) {
20   return new InvalidateRequest<I>(image_ctx, snap_id, force, on_finish);
21 }
22
23 template <typename I>
24 void InvalidateRequest<I>::send() {
25   I &image_ctx = this->m_image_ctx;
26   assert(image_ctx.owner_lock.is_locked());
27   assert(image_ctx.snap_lock.is_wlocked());
28
29   uint64_t snap_flags;
30   int r = image_ctx.get_flags(m_snap_id, &snap_flags);
31   if (r < 0 || ((snap_flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0)) {
32     this->async_complete(r);
33     return;
34   }
35
36   CephContext *cct = image_ctx.cct;
37   lderr(cct) << this << " invalidating object map in-memory" << dendl;
38
39   // update in-memory flags
40   uint64_t flags = RBD_FLAG_OBJECT_MAP_INVALID;
41   if ((image_ctx.features & RBD_FEATURE_FAST_DIFF) != 0) {
42     flags |= RBD_FLAG_FAST_DIFF_INVALID;
43   }
44
45   r = image_ctx.update_flags(m_snap_id, flags, true);
46   if (r < 0) {
47     this->async_complete(r);
48     return;
49   }
50
51   // do not update on-disk flags if not image owner
52   if (image_ctx.image_watcher == nullptr ||
53       (!m_force && m_snap_id == CEPH_NOSNAP &&
54        image_ctx.exclusive_lock != nullptr &&
55        !image_ctx.exclusive_lock->is_lock_owner())) {
56     this->async_complete(0);
57     return;
58   }
59
60   lderr(cct) << this << " invalidating object map on-disk" << dendl;
61   librados::ObjectWriteOperation op;
62   cls_client::set_flags(&op, m_snap_id, flags, flags);
63
64   librados::AioCompletion *rados_completion =
65     this->create_callback_completion();
66   r = image_ctx.md_ctx.aio_operate(image_ctx.header_oid, rados_completion,
67                                      &op);
68   assert(r == 0);
69   rados_completion->release();
70 }
71
72 template <typename I>
73 bool InvalidateRequest<I>::should_complete(int r) {
74   I &image_ctx = this->m_image_ctx;
75   CephContext *cct = image_ctx.cct;
76   lderr(cct) << this << " " << __func__ << ": r=" << r << dendl;
77   return true;
78 }
79
80 } // namespace object_map
81 } // namespace librbd
82
83 template class librbd::object_map::InvalidateRequest<librbd::ImageCtx>;