Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / operation / RenameRequest.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/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"
11
12 #define dout_subsys ceph_subsys_rbd
13 #undef dout_prefix
14 #define dout_prefix *_dout << "librbd::operation::RenameRequest: "
15
16 namespace librbd {
17 namespace operation {
18
19 namespace {
20
21 template <typename I>
22 std::ostream& operator<<(std::ostream& os,
23                          const typename RenameRequest<I>::State& state) {
24   switch(state) {
25   case RenameRequest<I>::STATE_READ_SOURCE_HEADER:
26     os << "READ_SOURCE_HEADER";
27     break;
28   case RenameRequest<I>::STATE_WRITE_DEST_HEADER:
29     os << "WRITE_DEST_HEADER";
30     break;
31   case RenameRequest<I>::STATE_UPDATE_DIRECTORY:
32     os << "UPDATE_DIRECTORY";
33     break;
34   case RenameRequest<I>::STATE_REMOVE_SOURCE_HEADER:
35     os << "REMOVE_SOURCE_HEADER";
36     break;
37   default:
38     os << "UNKNOWN (" << static_cast<uint32_t>(state) << ")";
39     break;
40   }
41   return os;
42 }
43
44 } // anonymous namespace
45
46 template <typename I>
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)) {
54 }
55
56 template <typename I>
57 void RenameRequest<I>::send_op() {
58   send_read_source_header();
59 }
60
61 template <typename I>
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);
68   if (r < 0) {
69     if (r == -EEXIST) {
70       ldout(cct, 1) << "image already exists" << dendl;
71     } else {
72       lderr(cct) << "encountered error: " << cpp_strerror(r) << dendl;
73     }
74     return true;
75   }
76
77   if (m_state == STATE_REMOVE_SOURCE_HEADER) {
78     apply();
79     return true;
80   }
81
82   RWLock::RLocker owner_lock(image_ctx.owner_lock);
83   switch (m_state) {
84   case STATE_READ_SOURCE_HEADER:
85     send_write_destination_header();
86     break;
87   case STATE_WRITE_DEST_HEADER:
88     send_update_directory();
89     break;
90   case STATE_UPDATE_DIRECTORY:
91     send_remove_source_header();
92     break;
93   default:
94     assert(false);
95     break;
96   }
97   return false;
98 }
99
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;
104
105   if (m_state == STATE_REMOVE_SOURCE_HEADER && r < 0) {
106     if (r != -ENOENT) {
107       lderr(cct) << "warning: couldn't remove old source object ("
108                  << m_source_oid << ")" << dendl;
109     }
110     return 0;
111   }
112   return r;
113 }
114
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;
121
122   librados::ObjectReadOperation op;
123   op.read(0, 0, NULL, NULL);
124
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,
129                                        &m_header_bl);
130   assert(r == 0);
131   rados_completion->release();
132 }
133
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;
140
141   librados::ObjectWriteOperation op;
142   op.create(true);
143   op.write_full(m_header_bl);
144
145   librados::AioCompletion *rados_completion = this->create_callback_completion();
146   int r = image_ctx.md_ctx.aio_operate(m_dest_oid, rados_completion, &op);
147   assert(r == 0);
148   rados_completion->release();
149 }
150
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;
157
158   librados::ObjectWriteOperation op;
159   if (image_ctx.old_format) {
160     bufferlist cmd_bl;
161     bufferlist empty_bl;
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);
168   } else {
169     cls_client::dir_rename_image(&op, image_ctx.name, m_dest_name,
170                                  image_ctx.id);
171   }
172
173   librados::AioCompletion *rados_completion = this->create_callback_completion();
174   int r = image_ctx.md_ctx.aio_operate(RBD_DIRECTORY, rados_completion, &op);
175   assert(r == 0);
176   rados_completion->release();
177 }
178
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;
185
186   librados::ObjectWriteOperation op;
187   op.remove();
188
189   librados::AioCompletion *rados_completion = this->create_callback_completion();
190   int r = image_ctx.md_ctx.aio_operate(m_source_oid, rados_completion, &op);
191   assert(r == 0);
192   rados_completion->release();
193 }
194
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);
199 }
200
201 } // namespace operation
202 } // namespace librbd
203
204 template class librbd::operation::RenameRequest<librbd::ImageCtx>;