1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "cls/rbd/cls_rbd_types.h"
5 #include "librbd/operation/SnapshotCreateRequest.h"
6 #include "common/dout.h"
7 #include "common/errno.h"
8 #include "librbd/ExclusiveLock.h"
9 #include "librbd/ImageCtx.h"
10 #include "librbd/ObjectMap.h"
11 #include "librbd/Utils.h"
12 #include "librbd/io/ImageRequestWQ.h"
14 #define dout_subsys ceph_subsys_rbd
16 #define dout_prefix *_dout << "librbd::SnapshotCreateRequest: "
21 using util::create_async_context_callback;
22 using util::create_context_callback;
23 using util::create_rados_callback;
26 SnapshotCreateRequest<I>::SnapshotCreateRequest(I &image_ctx,
28 const cls::rbd::SnapshotNamespace &snap_namespace,
29 const std::string &snap_name,
30 uint64_t journal_op_tid,
32 : Request<I>(image_ctx, on_finish, journal_op_tid),
33 m_snap_namespace(snap_namespace), m_snap_name(snap_name),
34 m_skip_object_map(skip_object_map), m_ret_val(0), m_snap_id(CEPH_NOSNAP) {
38 void SnapshotCreateRequest<I>::send_op() {
39 send_suspend_requests();
43 void SnapshotCreateRequest<I>::send_suspend_requests() {
44 I &image_ctx = this->m_image_ctx;
45 CephContext *cct = image_ctx.cct;
46 ldout(cct, 5) << this << " " << __func__ << dendl;
48 // TODO suspend (shrink) resize to ensure consistent RBD mirror
53 Context *SnapshotCreateRequest<I>::handle_suspend_requests(int *result) {
54 I &image_ctx = this->m_image_ctx;
55 CephContext *cct = image_ctx.cct;
56 ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl;
64 void SnapshotCreateRequest<I>::send_suspend_aio() {
65 I &image_ctx = this->m_image_ctx;
66 assert(image_ctx.owner_lock.is_locked());
68 CephContext *cct = image_ctx.cct;
69 ldout(cct, 5) << this << " " << __func__ << dendl;
71 image_ctx.io_work_queue->block_writes(create_context_callback<
72 SnapshotCreateRequest<I>,
73 &SnapshotCreateRequest<I>::handle_suspend_aio>(this));
77 Context *SnapshotCreateRequest<I>::handle_suspend_aio(int *result) {
78 I &image_ctx = this->m_image_ctx;
79 CephContext *cct = image_ctx.cct;
80 ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl;
83 lderr(cct) << "failed to block writes: " << cpp_strerror(*result) << dendl;
84 image_ctx.io_work_queue->unblock_writes();
85 return this->create_context_finisher(*result);
88 send_append_op_event();
93 void SnapshotCreateRequest<I>::send_append_op_event() {
94 I &image_ctx = this->m_image_ctx;
95 if (!this->template append_op_event<
96 SnapshotCreateRequest<I>,
97 &SnapshotCreateRequest<I>::handle_append_op_event>(this)) {
98 send_allocate_snap_id();
102 CephContext *cct = image_ctx.cct;
103 ldout(cct, 5) << this << " " << __func__ << dendl;
106 template <typename I>
107 Context *SnapshotCreateRequest<I>::handle_append_op_event(int *result) {
108 I &image_ctx = this->m_image_ctx;
109 CephContext *cct = image_ctx.cct;
110 ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl;
113 image_ctx.io_work_queue->unblock_writes();
114 lderr(cct) << "failed to commit journal entry: " << cpp_strerror(*result)
116 return this->create_context_finisher(*result);
119 send_allocate_snap_id();
123 template <typename I>
124 void SnapshotCreateRequest<I>::send_allocate_snap_id() {
125 I &image_ctx = this->m_image_ctx;
126 CephContext *cct = image_ctx.cct;
127 ldout(cct, 5) << this << " " << __func__ << dendl;
129 librados::AioCompletion *rados_completion = create_rados_callback<
130 SnapshotCreateRequest<I>,
131 &SnapshotCreateRequest<I>::handle_allocate_snap_id>(this);
132 image_ctx.data_ctx.aio_selfmanaged_snap_create(&m_snap_id, rados_completion);
133 rados_completion->release();
136 template <typename I>
137 Context *SnapshotCreateRequest<I>::handle_allocate_snap_id(int *result) {
138 I &image_ctx = this->m_image_ctx;
139 CephContext *cct = image_ctx.cct;
140 ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << ", "
141 << "snap_id=" << m_snap_id << dendl;
145 image_ctx.io_work_queue->unblock_writes();
146 lderr(cct) << "failed to allocate snapshot id: " << cpp_strerror(*result)
148 return this->create_context_finisher(*result);
155 template <typename I>
156 void SnapshotCreateRequest<I>::send_create_snap() {
157 I &image_ctx = this->m_image_ctx;
158 CephContext *cct = image_ctx.cct;
159 ldout(cct, 5) << this << " " << __func__ << dendl;
161 RWLock::RLocker owner_locker(image_ctx.owner_lock);
162 RWLock::RLocker snap_locker(image_ctx.snap_lock);
163 RWLock::RLocker parent_locker(image_ctx.parent_lock);
165 // should have been canceled prior to releasing lock
166 assert(image_ctx.exclusive_lock == nullptr ||
167 image_ctx.exclusive_lock->is_lock_owner());
169 // save current size / parent info for creating snapshot record in ImageCtx
170 m_size = image_ctx.size;
171 m_parent_info = image_ctx.parent_md;
173 librados::ObjectWriteOperation op;
174 if (image_ctx.old_format) {
175 cls_client::old_snapshot_add(&op, m_snap_id, m_snap_name);
177 cls_client::snapshot_add(&op, m_snap_id, m_snap_name, m_snap_namespace);
180 librados::AioCompletion *rados_completion = create_rados_callback<
181 SnapshotCreateRequest<I>,
182 &SnapshotCreateRequest<I>::handle_create_snap>(this);
183 int r = image_ctx.md_ctx.aio_operate(image_ctx.header_oid,
184 rados_completion, &op);
186 rados_completion->release();
189 template <typename I>
190 Context *SnapshotCreateRequest<I>::handle_create_snap(int *result) {
191 I &image_ctx = this->m_image_ctx;
192 CephContext *cct = image_ctx.cct;
193 ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl;
195 if (*result == -ESTALE) {
196 send_allocate_snap_id();
198 } else if (*result < 0) {
200 send_release_snap_id();
204 return send_create_object_map();
207 template <typename I>
208 Context *SnapshotCreateRequest<I>::send_create_object_map() {
209 I &image_ctx = this->m_image_ctx;
211 update_snap_context();
213 image_ctx.snap_lock.get_read();
214 if (image_ctx.object_map == nullptr || m_skip_object_map) {
215 image_ctx.snap_lock.put_read();
217 image_ctx.io_work_queue->unblock_writes();
218 return this->create_context_finisher(0);
221 CephContext *cct = image_ctx.cct;
222 ldout(cct, 5) << this << " " << __func__ << dendl;
225 RWLock::RLocker object_map_lock(image_ctx.object_map_lock);
226 image_ctx.object_map->snapshot_add(
227 m_snap_id, create_context_callback<
228 SnapshotCreateRequest<I>,
229 &SnapshotCreateRequest<I>::handle_create_object_map>(this));
231 image_ctx.snap_lock.put_read();
235 template <typename I>
236 Context *SnapshotCreateRequest<I>::handle_create_object_map(int *result) {
237 I &image_ctx = this->m_image_ctx;
238 CephContext *cct = image_ctx.cct;
239 ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl;
241 assert(*result == 0);
243 image_ctx.io_work_queue->unblock_writes();
244 return this->create_context_finisher(0);
247 template <typename I>
248 void SnapshotCreateRequest<I>::send_release_snap_id() {
249 I &image_ctx = this->m_image_ctx;
250 CephContext *cct = image_ctx.cct;
251 ldout(cct, 5) << this << " " << __func__ << dendl;
253 assert(m_snap_id != CEPH_NOSNAP);
255 librados::AioCompletion *rados_completion = create_rados_callback<
256 SnapshotCreateRequest<I>,
257 &SnapshotCreateRequest<I>::handle_release_snap_id>(this);
258 image_ctx.data_ctx.aio_selfmanaged_snap_remove(m_snap_id, rados_completion);
259 rados_completion->release();
262 template <typename I>
263 Context *SnapshotCreateRequest<I>::handle_release_snap_id(int *result) {
264 I &image_ctx = this->m_image_ctx;
265 CephContext *cct = image_ctx.cct;
266 ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl;
268 assert(m_ret_val < 0);
271 image_ctx.io_work_queue->unblock_writes();
272 return this->create_context_finisher(m_ret_val);
275 template <typename I>
276 void SnapshotCreateRequest<I>::update_snap_context() {
277 I &image_ctx = this->m_image_ctx;
279 RWLock::RLocker owner_locker(image_ctx.owner_lock);
280 RWLock::WLocker snap_locker(image_ctx.snap_lock);
281 if (image_ctx.old_format) {
285 if (image_ctx.get_snap_info(m_snap_id) != NULL) {
289 CephContext *cct = image_ctx.cct;
290 ldout(cct, 5) << this << " " << __func__ << dendl;
292 // should have been canceled prior to releasing lock
293 assert(image_ctx.exclusive_lock == nullptr ||
294 image_ctx.exclusive_lock->is_lock_owner());
296 // immediately add a reference to the new snapshot
297 utime_t snap_time = ceph_clock_now();
298 image_ctx.add_snap(m_snap_namespace, m_snap_name, m_snap_id, m_size,
299 m_parent_info, RBD_PROTECTION_STATUS_UNPROTECTED,
302 // immediately start using the new snap context if we
303 // own the exclusive lock
304 std::vector<snapid_t> snaps;
305 snaps.push_back(m_snap_id);
306 snaps.insert(snaps.end(), image_ctx.snapc.snaps.begin(),
307 image_ctx.snapc.snaps.end());
309 image_ctx.snapc.seq = m_snap_id;
310 image_ctx.snapc.snaps.swap(snaps);
311 image_ctx.data_ctx.selfmanaged_snap_set_write_ctx(
312 image_ctx.snapc.seq, image_ctx.snaps);
315 } // namespace operation
316 } // namespace librbd
318 template class librbd::operation::SnapshotCreateRequest<librbd::ImageCtx>;