]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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/MigrateRequest.h" | |
5 | #include "common/dout.h" | |
6 | #include "common/errno.h" | |
7 | #include "librbd/AsyncObjectThrottle.h" | |
8 | #include "librbd/ExclusiveLock.h" | |
9 | #include "librbd/ImageCtx.h" | |
10 | #include "librbd/Utils.h" | |
11 | #include "librbd/deep_copy/ObjectCopyRequest.h" | |
12 | #include "librbd/io/AsyncOperation.h" | |
13 | #include "librbd/io/ImageRequestWQ.h" | |
14 | #include "librbd/io/ObjectRequest.h" | |
15 | #include "osdc/Striper.h" | |
16 | #include <boost/lambda/bind.hpp> | |
17 | #include <boost/lambda/construct.hpp> | |
18 | ||
19 | #define dout_subsys ceph_subsys_rbd | |
20 | #undef dout_prefix | |
21 | #define dout_prefix *_dout << "librbd::MigrateRequest: " << this << " " \ | |
22 | << __func__ << ": " | |
23 | ||
24 | namespace librbd { | |
25 | namespace operation { | |
26 | ||
27 | using util::create_context_callback; | |
28 | using util::create_async_context_callback; | |
29 | ||
30 | namespace { | |
31 | ||
32 | template <typename I> | |
33 | class C_MigrateObject : public C_AsyncObjectThrottle<I> { | |
34 | public: | |
35 | C_MigrateObject(AsyncObjectThrottle<I> &throttle, I *image_ctx, | |
36 | ::SnapContext snapc, uint64_t object_no) | |
37 | : C_AsyncObjectThrottle<I>(throttle, *image_ctx), m_snapc(snapc), | |
38 | m_object_no(object_no) { | |
39 | } | |
40 | ||
41 | int send() override { | |
42 | I &image_ctx = this->m_image_ctx; | |
9f95a23c | 43 | ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock)); |
11fdf7f2 TL |
44 | CephContext *cct = image_ctx.cct; |
45 | ||
46 | if (image_ctx.exclusive_lock != nullptr && | |
47 | !image_ctx.exclusive_lock->is_lock_owner()) { | |
48 | ldout(cct, 1) << "lost exclusive lock during migrate" << dendl; | |
49 | return -ERESTART; | |
50 | } | |
51 | ||
52 | start_async_op(); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | private: | |
11fdf7f2 TL |
57 | ::SnapContext m_snapc; |
58 | uint64_t m_object_no; | |
59 | ||
60 | io::AsyncOperation *m_async_op = nullptr; | |
61 | ||
62 | void start_async_op() { | |
63 | I &image_ctx = this->m_image_ctx; | |
9f95a23c | 64 | ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock)); |
11fdf7f2 TL |
65 | CephContext *cct = image_ctx.cct; |
66 | ldout(cct, 10) << dendl; | |
67 | ||
68 | ceph_assert(m_async_op == nullptr); | |
69 | m_async_op = new io::AsyncOperation(); | |
70 | m_async_op->start_op(image_ctx); | |
71 | ||
72 | if (!image_ctx.io_work_queue->writes_blocked()) { | |
73 | migrate_object(); | |
74 | return; | |
75 | } | |
76 | ||
77 | auto ctx = create_async_context_callback( | |
78 | image_ctx, create_context_callback< | |
79 | C_MigrateObject<I>, &C_MigrateObject<I>::handle_start_async_op>(this)); | |
80 | m_async_op->finish_op(); | |
81 | delete m_async_op; | |
82 | m_async_op = nullptr; | |
83 | image_ctx.io_work_queue->wait_on_writes_unblocked(ctx); | |
84 | } | |
85 | ||
86 | void handle_start_async_op(int r) { | |
87 | I &image_ctx = this->m_image_ctx; | |
88 | CephContext *cct = image_ctx.cct; | |
89 | ldout(cct, 10) << "r=" << r << dendl; | |
90 | ||
91 | if (r < 0) { | |
92 | lderr(cct) << "failed to start async op: " << cpp_strerror(r) << dendl; | |
93 | this->complete(r); | |
94 | return; | |
95 | } | |
96 | ||
9f95a23c | 97 | std::shared_lock owner_locker{image_ctx.owner_lock}; |
11fdf7f2 TL |
98 | start_async_op(); |
99 | } | |
100 | ||
101 | bool is_within_overlap_bounds() { | |
102 | I &image_ctx = this->m_image_ctx; | |
9f95a23c | 103 | std::shared_lock image_locker{image_ctx.image_lock}; |
11fdf7f2 TL |
104 | |
105 | auto overlap = std::min(image_ctx.size, image_ctx.migration_info.overlap); | |
106 | return overlap > 0 && | |
107 | Striper::get_num_objects(image_ctx.layout, overlap) > m_object_no; | |
108 | } | |
109 | ||
110 | void migrate_object() { | |
111 | I &image_ctx = this->m_image_ctx; | |
9f95a23c | 112 | ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock)); |
11fdf7f2 TL |
113 | CephContext *cct = image_ctx.cct; |
114 | ||
115 | auto ctx = create_context_callback< | |
116 | C_MigrateObject<I>, &C_MigrateObject<I>::handle_migrate_object>(this); | |
117 | ||
118 | if (is_within_overlap_bounds()) { | |
119 | bufferlist bl; | |
9f95a23c | 120 | auto req = new io::ObjectWriteRequest<I>(&image_ctx, m_object_no, 0, |
11fdf7f2 TL |
121 | std::move(bl), m_snapc, 0, {}, |
122 | ctx); | |
123 | ||
124 | ldout(cct, 20) << "copyup object req " << req << ", object_no " | |
125 | << m_object_no << dendl; | |
126 | ||
127 | req->send(); | |
128 | } else { | |
129 | ceph_assert(image_ctx.parent != nullptr); | |
130 | ||
131 | auto req = deep_copy::ObjectCopyRequest<I>::create( | |
9f95a23c | 132 | image_ctx.parent, &image_ctx, 0, 0, image_ctx.migration_info.snap_map, |
1911f103 | 133 | m_object_no, image_ctx.migration_info.flatten, nullptr, ctx); |
11fdf7f2 TL |
134 | |
135 | ldout(cct, 20) << "deep copy object req " << req << ", object_no " | |
136 | << m_object_no << dendl; | |
137 | req->send(); | |
138 | } | |
139 | } | |
140 | ||
141 | void handle_migrate_object(int r) { | |
142 | CephContext *cct = this->m_image_ctx.cct; | |
143 | ldout(cct, 10) << "r=" << r << dendl; | |
144 | ||
145 | if (r == -ENOENT) { | |
146 | r = 0; | |
147 | } | |
148 | ||
149 | m_async_op->finish_op(); | |
150 | delete m_async_op; | |
151 | this->complete(r); | |
152 | } | |
153 | }; | |
154 | ||
155 | } // anonymous namespace | |
156 | ||
157 | template <typename I> | |
158 | void MigrateRequest<I>::send_op() { | |
159 | I &image_ctx = this->m_image_ctx; | |
9f95a23c | 160 | ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock)); |
11fdf7f2 TL |
161 | CephContext *cct = image_ctx.cct; |
162 | ldout(cct, 10) << dendl; | |
163 | ||
164 | migrate_objects(); | |
165 | } | |
166 | ||
167 | template <typename I> | |
168 | bool MigrateRequest<I>::should_complete(int r) { | |
169 | I &image_ctx = this->m_image_ctx; | |
170 | CephContext *cct = image_ctx.cct; | |
171 | ldout(cct, 10) << "r=" << r << dendl; | |
172 | ||
173 | if (r < 0) { | |
174 | lderr(cct) << "encountered error: " << cpp_strerror(r) << dendl; | |
175 | } | |
176 | ||
177 | return true; | |
178 | } | |
179 | ||
180 | template <typename I> | |
181 | void MigrateRequest<I>::migrate_objects() { | |
182 | I &image_ctx = this->m_image_ctx; | |
183 | CephContext *cct = image_ctx.cct; | |
9f95a23c | 184 | ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock)); |
11fdf7f2 TL |
185 | |
186 | uint64_t overlap_objects = get_num_overlap_objects(); | |
187 | ||
188 | ldout(cct, 10) << "from 0 to " << overlap_objects << dendl; | |
189 | ||
190 | auto ctx = create_context_callback< | |
191 | MigrateRequest<I>, &MigrateRequest<I>::handle_migrate_objects>(this); | |
192 | ||
193 | typename AsyncObjectThrottle<I>::ContextFactory context_factory( | |
194 | boost::lambda::bind(boost::lambda::new_ptr<C_MigrateObject<I> >(), | |
195 | boost::lambda::_1, &image_ctx, image_ctx.snapc, boost::lambda::_2)); | |
196 | AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>( | |
197 | this, image_ctx, context_factory, ctx, &m_prog_ctx, 0, overlap_objects); | |
198 | throttle->start_ops( | |
199 | image_ctx.config.template get_val<uint64_t>("rbd_concurrent_management_ops")); | |
200 | } | |
201 | ||
202 | template <typename I> | |
203 | void MigrateRequest<I>::handle_migrate_objects(int r) { | |
204 | I &image_ctx = this->m_image_ctx; | |
205 | CephContext *cct = image_ctx.cct; | |
206 | ldout(cct, 5) << "r=" << r << dendl; | |
207 | ||
208 | if (r < 0) { | |
209 | lderr(cct) << "failed to migrate objects: " << cpp_strerror(r) << dendl; | |
210 | } | |
211 | ||
212 | this->complete(r); | |
213 | } | |
214 | ||
215 | template <typename I> | |
216 | uint64_t MigrateRequest<I>::get_num_overlap_objects() { | |
217 | I &image_ctx = this->m_image_ctx; | |
218 | CephContext *cct = image_ctx.cct; | |
219 | ldout(cct, 10) << dendl; | |
220 | ||
9f95a23c | 221 | std::shared_lock image_locker{image_ctx.image_lock}; |
11fdf7f2 TL |
222 | |
223 | auto overlap = image_ctx.migration_info.overlap; | |
224 | ||
225 | return overlap > 0 ? | |
226 | Striper::get_num_objects(image_ctx.layout, overlap) : 0; | |
227 | } | |
228 | ||
229 | } // namespace operation | |
230 | } // namespace librbd | |
231 | ||
232 | template class librbd::operation::MigrateRequest<librbd::ImageCtx>; |