1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "librbd/operation/RenameRequest.h"
5 #include "common/dout.h"
6 #include "common/errno.h"
7 #include "include/rados/librados.hpp"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/internal.h"
10 #include "librbd/Utils.h"
12 #define dout_subsys ceph_subsys_rbd
14 #define dout_prefix *_dout << "librbd::operation::RenameRequest: "
22 std::ostream& operator<<(std::ostream& os,
23 const typename RenameRequest<I>::State& state) {
25 case RenameRequest<I>::STATE_READ_SOURCE_HEADER:
26 os << "READ_SOURCE_HEADER";
28 case RenameRequest<I>::STATE_WRITE_DEST_HEADER:
29 os << "WRITE_DEST_HEADER";
31 case RenameRequest<I>::STATE_UPDATE_DIRECTORY:
32 os << "UPDATE_DIRECTORY";
34 case RenameRequest<I>::STATE_REMOVE_SOURCE_HEADER:
35 os << "REMOVE_SOURCE_HEADER";
38 os << "UNKNOWN (" << static_cast<uint32_t>(state) << ")";
44 } // anonymous namespace
47 RenameRequest<I>::RenameRequest(I &image_ctx, Context *on_finish,
48 const std::string &dest_name)
49 : Request<I>(image_ctx, on_finish), m_dest_name(dest_name),
50 m_source_oid(image_ctx.old_format ? util::old_header_name(image_ctx.name) :
51 util::id_obj_name(image_ctx.name)),
52 m_dest_oid(image_ctx.old_format ? util::old_header_name(dest_name) :
53 util::id_obj_name(dest_name)) {
57 void RenameRequest<I>::send_op() {
58 send_read_source_header();
62 bool RenameRequest<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 r = filter_state_return_code(r);
70 ldout(cct, 1) << "image already exists" << dendl;
72 lderr(cct) << "encountered error: " << cpp_strerror(r) << dendl;
77 if (m_state == STATE_REMOVE_SOURCE_HEADER) {
82 RWLock::RLocker owner_lock(image_ctx.owner_lock);
84 case STATE_READ_SOURCE_HEADER:
85 send_write_destination_header();
87 case STATE_WRITE_DEST_HEADER:
88 send_update_directory();
90 case STATE_UPDATE_DIRECTORY:
91 send_remove_source_header();
100 template <typename I>
101 int RenameRequest<I>::filter_state_return_code(int r) {
102 I &image_ctx = this->m_image_ctx;
103 CephContext *cct = image_ctx.cct;
105 if (m_state == STATE_REMOVE_SOURCE_HEADER && r < 0) {
107 lderr(cct) << "warning: couldn't remove old source object ("
108 << m_source_oid << ")" << dendl;
115 template <typename I>
116 void RenameRequest<I>::send_read_source_header() {
117 I &image_ctx = this->m_image_ctx;
118 CephContext *cct = image_ctx.cct;
119 ldout(cct, 5) << this << " " << __func__ << dendl;
120 m_state = STATE_READ_SOURCE_HEADER;
122 librados::ObjectReadOperation op;
123 op.read(0, 0, NULL, NULL);
125 // TODO: old code read omap values but there are no omap values on the
126 // old format header nor the new format id object
127 librados::AioCompletion *rados_completion = this->create_callback_completion();
128 int r = image_ctx.md_ctx.aio_operate(m_source_oid, rados_completion, &op,
131 rados_completion->release();
134 template <typename I>
135 void RenameRequest<I>::send_write_destination_header() {
136 I &image_ctx = this->m_image_ctx;
137 CephContext *cct = image_ctx.cct;
138 ldout(cct, 5) << this << " " << __func__ << dendl;
139 m_state = STATE_WRITE_DEST_HEADER;
141 librados::ObjectWriteOperation op;
143 op.write_full(m_header_bl);
145 librados::AioCompletion *rados_completion = this->create_callback_completion();
146 int r = image_ctx.md_ctx.aio_operate(m_dest_oid, rados_completion, &op);
148 rados_completion->release();
151 template <typename I>
152 void RenameRequest<I>::send_update_directory() {
153 I &image_ctx = this->m_image_ctx;
154 CephContext *cct = image_ctx.cct;
155 ldout(cct, 5) << this << " " << __func__ << dendl;
156 m_state = STATE_UPDATE_DIRECTORY;
158 librados::ObjectWriteOperation op;
159 if (image_ctx.old_format) {
162 ::encode(static_cast<__u8>(CEPH_OSD_TMAP_SET), cmd_bl);
163 ::encode(m_dest_name, cmd_bl);
164 ::encode(empty_bl, cmd_bl);
165 ::encode(static_cast<__u8>(CEPH_OSD_TMAP_RM), cmd_bl);
166 ::encode(image_ctx.name, cmd_bl);
167 op.tmap_update(cmd_bl);
169 cls_client::dir_rename_image(&op, image_ctx.name, m_dest_name,
173 librados::AioCompletion *rados_completion = this->create_callback_completion();
174 int r = image_ctx.md_ctx.aio_operate(RBD_DIRECTORY, rados_completion, &op);
176 rados_completion->release();
179 template <typename I>
180 void RenameRequest<I>::send_remove_source_header() {
181 I &image_ctx = this->m_image_ctx;
182 CephContext *cct = image_ctx.cct;
183 ldout(cct, 5) << this << " " << __func__ << dendl;
184 m_state = STATE_REMOVE_SOURCE_HEADER;
186 librados::ObjectWriteOperation op;
189 librados::AioCompletion *rados_completion = this->create_callback_completion();
190 int r = image_ctx.md_ctx.aio_operate(m_source_oid, rados_completion, &op);
192 rados_completion->release();
195 template <typename I>
196 void RenameRequest<I>::apply() {
197 I &image_ctx = this->m_image_ctx;
198 image_ctx.set_image_name(m_dest_name);
201 } // namespace operation
202 } // namespace librbd
204 template class librbd::operation::RenameRequest<librbd::ImageCtx>;