]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/operation/SnapshotRenameRequest.h" | |
5 | #include "common/dout.h" | |
6 | #include "common/errno.h" | |
7 | #include "librbd/ExclusiveLock.h" | |
8 | #include "librbd/ImageCtx.h" | |
9 | ||
10 | #define dout_subsys ceph_subsys_rbd | |
11 | #undef dout_prefix | |
12 | #define dout_prefix *_dout << "librbd::SnapshotRenameRequest: " | |
13 | ||
14 | namespace librbd { | |
15 | namespace operation { | |
16 | ||
17 | namespace { | |
18 | ||
19 | template <typename I> | |
20 | std::ostream& operator<<(std::ostream& os, | |
21 | const typename SnapshotRenameRequest<I>::State& state) { | |
22 | switch(state) { | |
23 | case SnapshotRenameRequest<I>::STATE_RENAME_SNAP: | |
24 | os << "RENAME_SNAP"; | |
25 | break; | |
26 | } | |
27 | return os; | |
28 | } | |
29 | ||
30 | } // anonymous namespace | |
31 | ||
32 | template <typename I> | |
33 | SnapshotRenameRequest<I>::SnapshotRenameRequest(I &image_ctx, | |
34 | Context *on_finish, | |
35 | uint64_t snap_id, | |
36 | const std::string &snap_name) | |
11fdf7f2 TL |
37 | : Request<I>(image_ctx, on_finish), m_snap_id(snap_id), |
38 | m_snap_name(snap_name), m_state(STATE_RENAME_SNAP) { | |
7c673cae FG |
39 | } |
40 | ||
41 | template <typename I> | |
42 | journal::Event SnapshotRenameRequest<I>::create_event(uint64_t op_tid) const { | |
43 | I &image_ctx = this->m_image_ctx; | |
11fdf7f2 | 44 | ceph_assert(image_ctx.snap_lock.is_locked()); |
7c673cae FG |
45 | |
46 | std::string src_snap_name; | |
47 | auto snap_info_it = image_ctx.snap_info.find(m_snap_id); | |
48 | if (snap_info_it != image_ctx.snap_info.end()) { | |
49 | src_snap_name = snap_info_it->second.name; | |
50 | } | |
51 | ||
52 | return journal::SnapRenameEvent(op_tid, m_snap_id, src_snap_name, | |
53 | m_snap_name); | |
54 | } | |
55 | ||
56 | template <typename I> | |
57 | void SnapshotRenameRequest<I>::send_op() { | |
58 | send_rename_snap(); | |
59 | } | |
60 | ||
61 | template <typename I> | |
62 | bool SnapshotRenameRequest<I>::should_complete(int r) { | |
63 | I &image_ctx = this->m_image_ctx; | |
64 | CephContext *cct = image_ctx.cct; | |
65 | ldout(cct, 5) << this << " " << __func__ << ": state=" << m_state << ", " | |
66 | << "r=" << r << dendl; | |
67 | if (r < 0) { | |
68 | if (r == -EEXIST) { | |
69 | ldout(cct, 1) << "snapshot already exists" << dendl; | |
70 | } else { | |
71 | lderr(cct) << "encountered error: " << cpp_strerror(r) << dendl; | |
72 | } | |
73 | } | |
74 | return true; | |
75 | } | |
76 | ||
77 | template <typename I> | |
78 | void SnapshotRenameRequest<I>::send_rename_snap() { | |
79 | I &image_ctx = this->m_image_ctx; | |
11fdf7f2 | 80 | ceph_assert(image_ctx.owner_lock.is_locked()); |
7c673cae FG |
81 | RWLock::RLocker md_locker(image_ctx.md_lock); |
82 | RWLock::RLocker snap_locker(image_ctx.snap_lock); | |
83 | ||
84 | CephContext *cct = image_ctx.cct; | |
85 | ldout(cct, 5) << this << " " << __func__ << dendl; | |
86 | ||
7c673cae FG |
87 | librados::ObjectWriteOperation op; |
88 | if (image_ctx.old_format) { | |
89 | cls_client::old_snapshot_rename(&op, m_snap_id, m_snap_name); | |
90 | } else { | |
91 | cls_client::snapshot_rename(&op, m_snap_id, m_snap_name); | |
92 | } | |
93 | ||
94 | librados::AioCompletion *rados_completion = this->create_callback_completion(); | |
95 | int r = image_ctx.md_ctx.aio_operate(image_ctx.header_oid, | |
96 | rados_completion, &op); | |
11fdf7f2 | 97 | ceph_assert(r == 0); |
7c673cae FG |
98 | rados_completion->release(); |
99 | } | |
100 | ||
101 | } // namespace operation | |
102 | } // namespace librbd | |
103 | ||
104 | template class librbd::operation::SnapshotRenameRequest<librbd::ImageCtx>; |