1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "librbd/image/SetSnapRequest.h"
5 #include "common/dout.h"
6 #include "common/errno.h"
7 #include "librbd/ExclusiveLock.h"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/ObjectMap.h"
10 #include "librbd/Utils.h"
11 #include "librbd/image/RefreshParentRequest.h"
12 #include "librbd/io/ImageRequestWQ.h"
14 #define dout_subsys ceph_subsys_rbd
16 #define dout_prefix *_dout << "librbd::image::SetSnapRequest: "
21 using util::create_context_callback;
24 SetSnapRequest<I>::SetSnapRequest(I &image_ctx, const cls::rbd::SnapshotNamespace& snap_namespace,
25 const std::string &snap_name,
27 : m_image_ctx(image_ctx), m_snap_namespace(snap_namespace),
28 m_snap_name(snap_name), m_on_finish(on_finish),
29 m_snap_id(CEPH_NOSNAP), m_exclusive_lock(nullptr), m_object_map(nullptr),
30 m_refresh_parent(nullptr), m_writes_blocked(false) {
34 SetSnapRequest<I>::~SetSnapRequest() {
35 assert(!m_writes_blocked);
36 delete m_refresh_parent;
38 delete m_exclusive_lock;
42 void SetSnapRequest<I>::send() {
43 if (m_snap_name.empty()) {
44 send_init_exclusive_lock();
51 void SetSnapRequest<I>::send_init_exclusive_lock() {
53 RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
54 if (m_image_ctx.exclusive_lock != nullptr) {
55 assert(m_image_ctx.snap_id == CEPH_NOSNAP);
61 if (m_image_ctx.read_only ||
62 !m_image_ctx.test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
64 if (send_refresh_parent(&r) != nullptr) {
70 CephContext *cct = m_image_ctx.cct;
71 ldout(cct, 10) << __func__ << dendl;
73 m_exclusive_lock = ExclusiveLock<I>::create(m_image_ctx);
75 using klass = SetSnapRequest<I>;
76 Context *ctx = create_context_callback<
77 klass, &klass::handle_init_exclusive_lock>(this);
79 RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
80 m_exclusive_lock->init(m_image_ctx.features, ctx);
84 Context *SetSnapRequest<I>::handle_init_exclusive_lock(int *result) {
85 CephContext *cct = m_image_ctx.cct;
86 ldout(cct, 10) << __func__ << ": r=" << *result << dendl;
89 lderr(cct) << "failed to initialize exclusive lock: "
90 << cpp_strerror(*result) << dendl;
94 return send_refresh_parent(result);
98 void SetSnapRequest<I>::send_block_writes() {
99 CephContext *cct = m_image_ctx.cct;
100 ldout(cct, 10) << __func__ << dendl;
102 m_writes_blocked = true;
104 using klass = SetSnapRequest<I>;
105 Context *ctx = create_context_callback<
106 klass, &klass::handle_block_writes>(this);
108 RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
109 m_image_ctx.io_work_queue->block_writes(ctx);
112 template <typename I>
113 Context *SetSnapRequest<I>::handle_block_writes(int *result) {
114 CephContext *cct = m_image_ctx.cct;
115 ldout(cct, 10) << __func__ << ": r=" << *result << dendl;
118 lderr(cct) << "failed to block writes: " << cpp_strerror(*result)
125 RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
126 m_snap_id = m_image_ctx.get_snap_id(m_snap_namespace, m_snap_name);
127 if (m_snap_id == CEPH_NOSNAP) {
128 ldout(cct, 5) << "failed to locate snapshot '" << m_snap_name << "'"
137 return send_shut_down_exclusive_lock(result);
140 template <typename I>
141 Context *SetSnapRequest<I>::send_shut_down_exclusive_lock(int *result) {
143 RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
144 m_exclusive_lock = m_image_ctx.exclusive_lock;
147 if (m_exclusive_lock == nullptr) {
148 return send_refresh_parent(result);
151 CephContext *cct = m_image_ctx.cct;
152 ldout(cct, 10) << __func__ << dendl;
154 using klass = SetSnapRequest<I>;
155 Context *ctx = create_context_callback<
156 klass, &klass::handle_shut_down_exclusive_lock>(this);
157 m_exclusive_lock->shut_down(ctx);
161 template <typename I>
162 Context *SetSnapRequest<I>::handle_shut_down_exclusive_lock(int *result) {
163 CephContext *cct = m_image_ctx.cct;
164 ldout(cct, 10) << __func__ << ": r=" << *result << dendl;
167 lderr(cct) << "failed to shut down exclusive lock: "
168 << cpp_strerror(*result) << dendl;
173 return send_refresh_parent(result);
176 template <typename I>
177 Context *SetSnapRequest<I>::send_refresh_parent(int *result) {
178 CephContext *cct = m_image_ctx.cct;
180 ParentInfo parent_md;
183 RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
184 RWLock::RLocker parent_locker(m_image_ctx.parent_lock);
186 const ParentInfo *parent_info = m_image_ctx.get_parent_info(m_snap_id);
187 if (parent_info == nullptr) {
189 lderr(cct) << "failed to retrieve snapshot parent info" << dendl;
194 parent_md = *parent_info;
195 refresh_parent = RefreshParentRequest<I>::is_refresh_required(m_image_ctx,
199 if (!refresh_parent) {
200 if (m_snap_id == CEPH_NOSNAP) {
201 // object map is loaded when exclusive lock is acquired
206 // load snapshot object map
207 return send_open_object_map(result);
211 ldout(cct, 10) << __func__ << dendl;
213 using klass = SetSnapRequest<I>;
214 Context *ctx = create_context_callback<
215 klass, &klass::handle_refresh_parent>(this);
216 m_refresh_parent = RefreshParentRequest<I>::create(m_image_ctx, parent_md,
218 m_refresh_parent->send();
222 template <typename I>
223 Context *SetSnapRequest<I>::handle_refresh_parent(int *result) {
224 CephContext *cct = m_image_ctx.cct;
225 ldout(cct, 10) << __func__ << ": r=" << *result << dendl;
228 lderr(cct) << "failed to refresh snapshot parent: " << cpp_strerror(*result)
234 if (m_snap_id == CEPH_NOSNAP) {
235 // object map is loaded when exclusive lock is acquired
242 return send_finalize_refresh_parent(result);
244 // load snapshot object map
245 return send_open_object_map(result);
249 template <typename I>
250 Context *SetSnapRequest<I>::send_open_object_map(int *result) {
251 if (!m_image_ctx.test_features(RBD_FEATURE_OBJECT_MAP)) {
258 return send_finalize_refresh_parent(result);
261 CephContext *cct = m_image_ctx.cct;
262 ldout(cct, 10) << __func__ << dendl;
264 using klass = SetSnapRequest<I>;
265 Context *ctx = create_context_callback<
266 klass, &klass::handle_open_object_map>(this);
267 m_object_map = ObjectMap<I>::create(m_image_ctx, m_snap_id);
268 m_object_map->open(ctx);
272 template <typename I>
273 Context *SetSnapRequest<I>::handle_open_object_map(int *result) {
274 CephContext *cct = m_image_ctx.cct;
275 ldout(cct, 10) << __func__ << ": r=" << *result << dendl;
278 lderr(cct) << "failed to open object map: " << cpp_strerror(*result)
281 m_object_map = nullptr;
290 return send_finalize_refresh_parent(result);
293 template <typename I>
294 Context *SetSnapRequest<I>::send_finalize_refresh_parent(int *result) {
295 if (m_refresh_parent == nullptr) {
300 CephContext *cct = m_image_ctx.cct;
301 ldout(cct, 10) << this << " " << __func__ << dendl;
303 using klass = SetSnapRequest<I>;
304 Context *ctx = create_context_callback<
305 klass, &klass::handle_finalize_refresh_parent>(this);
306 m_refresh_parent->finalize(ctx);
310 template <typename I>
311 Context *SetSnapRequest<I>::handle_finalize_refresh_parent(int *result) {
312 CephContext *cct = m_image_ctx.cct;
313 ldout(cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
316 lderr(cct) << "failed to close parent image: " << cpp_strerror(*result)
323 template <typename I>
324 int SetSnapRequest<I>::apply() {
325 CephContext *cct = m_image_ctx.cct;
326 ldout(cct, 10) << __func__ << dendl;
328 RWLock::WLocker owner_locker(m_image_ctx.owner_lock);
329 RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
330 RWLock::WLocker parent_locker(m_image_ctx.parent_lock);
331 if (m_snap_id != CEPH_NOSNAP) {
332 assert(m_image_ctx.exclusive_lock == nullptr);
333 int r = m_image_ctx.snap_set(m_snap_namespace, m_snap_name);
338 std::swap(m_image_ctx.exclusive_lock, m_exclusive_lock);
339 m_image_ctx.snap_unset();
342 if (m_refresh_parent != nullptr) {
343 m_refresh_parent->apply();
346 std::swap(m_object_map, m_image_ctx.object_map);
350 template <typename I>
351 void SetSnapRequest<I>::finalize() {
352 if (m_writes_blocked) {
353 m_image_ctx.io_work_queue->unblock_writes();
354 m_writes_blocked = false;
358 template <typename I>
359 void SetSnapRequest<I>::send_complete() {
361 m_on_finish->complete(0);
366 } // namespace librbd
368 template class librbd::image::SetSnapRequest<librbd::ImageCtx>;