]>
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 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2004-2012 Sage Weil <sage@newdream.net> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <limits.h> | |
16 | ||
17 | #include "IoCtxImpl.h" | |
18 | ||
11fdf7f2 | 19 | #include "librados/librados_c.h" |
7c673cae FG |
20 | #include "librados/AioCompletionImpl.h" |
21 | #include "librados/PoolAsyncCompletionImpl.h" | |
22 | #include "librados/RadosClient.h" | |
11fdf7f2 | 23 | #include "include/ceph_assert.h" |
7c673cae FG |
24 | #include "common/valgrind.h" |
25 | #include "common/EventTrace.h" | |
26 | ||
27 | #define dout_subsys ceph_subsys_rados | |
28 | #undef dout_prefix | |
29 | #define dout_prefix *_dout << "librados: " | |
30 | ||
20effc67 TL |
31 | using std::string; |
32 | using std::map; | |
33 | using std::unique_lock; | |
34 | using std::vector; | |
35 | ||
f67539c2 TL |
36 | namespace bs = boost::system; |
37 | namespace ca = ceph::async; | |
38 | namespace cb = ceph::buffer; | |
39 | ||
7c673cae FG |
40 | namespace librados { |
41 | namespace { | |
42 | ||
f67539c2 | 43 | struct CB_notify_Finish { |
7c673cae FG |
44 | CephContext *cct; |
45 | Context *ctx; | |
46 | Objecter *objecter; | |
47 | Objecter::LingerOp *linger_op; | |
7c673cae FG |
48 | bufferlist *preply_bl; |
49 | char **preply_buf; | |
50 | size_t *preply_buf_len; | |
51 | ||
f67539c2 TL |
52 | CB_notify_Finish(CephContext *_cct, Context *_ctx, Objecter *_objecter, |
53 | Objecter::LingerOp *_linger_op, bufferlist *_preply_bl, | |
54 | char **_preply_buf, size_t *_preply_buf_len) | |
7c673cae FG |
55 | : cct(_cct), ctx(_ctx), objecter(_objecter), linger_op(_linger_op), |
56 | preply_bl(_preply_bl), preply_buf(_preply_buf), | |
f67539c2 | 57 | preply_buf_len(_preply_buf_len) {} |
7c673cae | 58 | |
f67539c2 TL |
59 | |
60 | // move-only | |
61 | CB_notify_Finish(const CB_notify_Finish&) = delete; | |
62 | CB_notify_Finish& operator =(const CB_notify_Finish&) = delete; | |
63 | CB_notify_Finish(CB_notify_Finish&&) = default; | |
64 | CB_notify_Finish& operator =(CB_notify_Finish&&) = default; | |
65 | ||
66 | void operator()(bs::error_code ec, bufferlist&& reply_bl) { | |
7c673cae | 67 | ldout(cct, 10) << __func__ << " completed notify (linger op " |
f67539c2 | 68 | << linger_op << "), ec = " << ec << dendl; |
7c673cae FG |
69 | |
70 | // pass result back to user | |
71 | // NOTE: we do this regardless of what error code we return | |
72 | if (preply_buf) { | |
73 | if (reply_bl.length()) { | |
74 | *preply_buf = (char*)malloc(reply_bl.length()); | |
75 | memcpy(*preply_buf, reply_bl.c_str(), reply_bl.length()); | |
76 | } else { | |
77 | *preply_buf = NULL; | |
78 | } | |
79 | } | |
80 | if (preply_buf_len) | |
81 | *preply_buf_len = reply_bl.length(); | |
82 | if (preply_bl) | |
f67539c2 | 83 | *preply_bl = std::move(reply_bl); |
7c673cae | 84 | |
f67539c2 | 85 | ctx->complete(ceph::from_error_code(ec)); |
7c673cae FG |
86 | } |
87 | }; | |
88 | ||
f67539c2 | 89 | struct CB_aio_linger_cancel { |
7c673cae FG |
90 | Objecter *objecter; |
91 | Objecter::LingerOp *linger_op; | |
92 | ||
f67539c2 | 93 | CB_aio_linger_cancel(Objecter *_objecter, Objecter::LingerOp *_linger_op) |
7c673cae FG |
94 | : objecter(_objecter), linger_op(_linger_op) |
95 | { | |
96 | } | |
97 | ||
f67539c2 | 98 | void operator()() { |
7c673cae FG |
99 | objecter->linger_cancel(linger_op); |
100 | } | |
101 | }; | |
102 | ||
103 | struct C_aio_linger_Complete : public Context { | |
104 | AioCompletionImpl *c; | |
105 | Objecter::LingerOp *linger_op; | |
106 | bool cancel; | |
107 | ||
108 | C_aio_linger_Complete(AioCompletionImpl *_c, Objecter::LingerOp *_linger_op, bool _cancel) | |
109 | : c(_c), linger_op(_linger_op), cancel(_cancel) | |
110 | { | |
111 | c->get(); | |
112 | } | |
113 | ||
114 | void finish(int r) override { | |
115 | if (cancel || r < 0) | |
f67539c2 TL |
116 | boost::asio::defer(c->io->client->finish_strand, |
117 | CB_aio_linger_cancel(c->io->objecter, | |
118 | linger_op)); | |
7c673cae | 119 | |
9f95a23c | 120 | c->lock.lock(); |
7c673cae FG |
121 | c->rval = r; |
122 | c->complete = true; | |
9f95a23c | 123 | c->cond.notify_all(); |
7c673cae FG |
124 | |
125 | if (c->callback_complete || | |
126 | c->callback_safe) { | |
f67539c2 | 127 | boost::asio::defer(c->io->client->finish_strand, CB_AioComplete(c)); |
7c673cae FG |
128 | } |
129 | c->put_unlock(); | |
130 | } | |
131 | }; | |
132 | ||
133 | struct C_aio_notify_Complete : public C_aio_linger_Complete { | |
9f95a23c | 134 | ceph::mutex lock = ceph::make_mutex("C_aio_notify_Complete::lock"); |
7c673cae FG |
135 | bool acked = false; |
136 | bool finished = false; | |
137 | int ret_val = 0; | |
138 | ||
139 | C_aio_notify_Complete(AioCompletionImpl *_c, Objecter::LingerOp *_linger_op) | |
9f95a23c | 140 | : C_aio_linger_Complete(_c, _linger_op, false) { |
7c673cae FG |
141 | } |
142 | ||
143 | void handle_ack(int r) { | |
144 | // invoked by C_aio_notify_Ack | |
9f95a23c | 145 | lock.lock(); |
7c673cae FG |
146 | acked = true; |
147 | complete_unlock(r); | |
148 | } | |
149 | ||
150 | void complete(int r) override { | |
28e407b8 | 151 | // invoked by C_notify_Finish |
9f95a23c | 152 | lock.lock(); |
7c673cae FG |
153 | finished = true; |
154 | complete_unlock(r); | |
155 | } | |
156 | ||
157 | void complete_unlock(int r) { | |
158 | if (ret_val == 0 && r < 0) { | |
159 | ret_val = r; | |
160 | } | |
161 | ||
162 | if (acked && finished) { | |
9f95a23c | 163 | lock.unlock(); |
7c673cae FG |
164 | cancel = true; |
165 | C_aio_linger_Complete::complete(ret_val); | |
166 | } else { | |
9f95a23c | 167 | lock.unlock(); |
7c673cae FG |
168 | } |
169 | } | |
170 | }; | |
171 | ||
172 | struct C_aio_notify_Ack : public Context { | |
173 | CephContext *cct; | |
7c673cae FG |
174 | C_aio_notify_Complete *oncomplete; |
175 | ||
f67539c2 | 176 | C_aio_notify_Ack(CephContext *_cct, |
7c673cae | 177 | C_aio_notify_Complete *_oncomplete) |
f67539c2 | 178 | : cct(_cct), oncomplete(_oncomplete) |
7c673cae FG |
179 | { |
180 | } | |
181 | ||
182 | void finish(int r) override | |
183 | { | |
184 | ldout(cct, 10) << __func__ << " linger op " << oncomplete->linger_op << " " | |
185 | << "acked (" << r << ")" << dendl; | |
186 | oncomplete->handle_ack(r); | |
7c673cae FG |
187 | } |
188 | }; | |
189 | ||
190 | struct C_aio_selfmanaged_snap_op_Complete : public Context { | |
191 | librados::RadosClient *client; | |
192 | librados::AioCompletionImpl *c; | |
193 | ||
194 | C_aio_selfmanaged_snap_op_Complete(librados::RadosClient *client, | |
195 | librados::AioCompletionImpl *c) | |
196 | : client(client), c(c) { | |
197 | c->get(); | |
198 | } | |
199 | ||
200 | void finish(int r) override { | |
9f95a23c | 201 | c->lock.lock(); |
7c673cae FG |
202 | c->rval = r; |
203 | c->complete = true; | |
9f95a23c | 204 | c->cond.notify_all(); |
7c673cae FG |
205 | |
206 | if (c->callback_complete || c->callback_safe) { | |
f67539c2 | 207 | boost::asio::defer(client->finish_strand, librados::CB_AioComplete(c)); |
7c673cae FG |
208 | } |
209 | c->put_unlock(); | |
210 | } | |
211 | }; | |
212 | ||
213 | struct C_aio_selfmanaged_snap_create_Complete : public C_aio_selfmanaged_snap_op_Complete { | |
214 | snapid_t snapid; | |
215 | uint64_t *dest_snapid; | |
216 | ||
217 | C_aio_selfmanaged_snap_create_Complete(librados::RadosClient *client, | |
218 | librados::AioCompletionImpl *c, | |
219 | uint64_t *dest_snapid) | |
220 | : C_aio_selfmanaged_snap_op_Complete(client, c), | |
221 | dest_snapid(dest_snapid) { | |
222 | } | |
223 | ||
224 | void finish(int r) override { | |
225 | if (r >= 0) { | |
226 | *dest_snapid = snapid; | |
227 | } | |
228 | C_aio_selfmanaged_snap_op_Complete::finish(r); | |
229 | } | |
230 | }; | |
231 | ||
232 | } // anonymous namespace | |
233 | } // namespace librados | |
234 | ||
9f95a23c | 235 | librados::IoCtxImpl::IoCtxImpl() = default; |
7c673cae FG |
236 | |
237 | librados::IoCtxImpl::IoCtxImpl(RadosClient *c, Objecter *objecter, | |
238 | int64_t poolid, snapid_t s) | |
9f95a23c | 239 | : client(c), poolid(poolid), snap_seq(s), |
7c673cae | 240 | notify_timeout(c->cct->_conf->client_notify_timeout), |
9f95a23c | 241 | oloc(poolid), |
7c673cae FG |
242 | aio_write_seq(0), objecter(objecter) |
243 | { | |
244 | } | |
245 | ||
246 | void librados::IoCtxImpl::set_snap_read(snapid_t s) | |
247 | { | |
248 | if (!s) | |
249 | s = CEPH_NOSNAP; | |
250 | ldout(client->cct, 10) << "set snap read " << snap_seq << " -> " << s << dendl; | |
251 | snap_seq = s; | |
252 | } | |
253 | ||
254 | int librados::IoCtxImpl::set_snap_write_context(snapid_t seq, vector<snapid_t>& snaps) | |
255 | { | |
256 | ::SnapContext n; | |
257 | ldout(client->cct, 10) << "set snap write context: seq = " << seq | |
258 | << " and snaps = " << snaps << dendl; | |
259 | n.seq = seq; | |
260 | n.snaps = snaps; | |
261 | if (!n.is_valid()) | |
262 | return -EINVAL; | |
263 | snapc = n; | |
264 | return 0; | |
265 | } | |
266 | ||
267 | int librados::IoCtxImpl::get_object_hash_position( | |
268 | const std::string& oid, uint32_t *hash_position) | |
269 | { | |
270 | int64_t r = objecter->get_object_hash_position(poolid, oid, oloc.nspace); | |
271 | if (r < 0) | |
272 | return r; | |
273 | *hash_position = (uint32_t)r; | |
274 | return 0; | |
275 | } | |
276 | ||
277 | int librados::IoCtxImpl::get_object_pg_hash_position( | |
278 | const std::string& oid, uint32_t *pg_hash_position) | |
279 | { | |
280 | int64_t r = objecter->get_object_pg_hash_position(poolid, oid, oloc.nspace); | |
281 | if (r < 0) | |
282 | return r; | |
283 | *pg_hash_position = (uint32_t)r; | |
284 | return 0; | |
285 | } | |
286 | ||
287 | void librados::IoCtxImpl::queue_aio_write(AioCompletionImpl *c) | |
288 | { | |
289 | get(); | |
9f95a23c | 290 | std::scoped_lock l{aio_write_list_lock}; |
11fdf7f2 | 291 | ceph_assert(c->io == this); |
7c673cae FG |
292 | c->aio_write_seq = ++aio_write_seq; |
293 | ldout(client->cct, 20) << "queue_aio_write " << this << " completion " << c | |
294 | << " write_seq " << aio_write_seq << dendl; | |
295 | aio_write_list.push_back(&c->aio_write_list_item); | |
7c673cae FG |
296 | } |
297 | ||
298 | void librados::IoCtxImpl::complete_aio_write(AioCompletionImpl *c) | |
299 | { | |
300 | ldout(client->cct, 20) << "complete_aio_write " << c << dendl; | |
9f95a23c | 301 | aio_write_list_lock.lock(); |
11fdf7f2 | 302 | ceph_assert(c->io == this); |
7c673cae FG |
303 | c->aio_write_list_item.remove_myself(); |
304 | ||
305 | map<ceph_tid_t, std::list<AioCompletionImpl*> >::iterator waiters = aio_write_waiters.begin(); | |
306 | while (waiters != aio_write_waiters.end()) { | |
307 | if (!aio_write_list.empty() && | |
308 | aio_write_list.front()->aio_write_seq <= waiters->first) { | |
309 | ldout(client->cct, 20) << " next outstanding write is " << aio_write_list.front()->aio_write_seq | |
310 | << " <= waiter " << waiters->first | |
311 | << ", stopping" << dendl; | |
312 | break; | |
313 | } | |
314 | ldout(client->cct, 20) << " waking waiters on seq " << waiters->first << dendl; | |
315 | for (std::list<AioCompletionImpl*>::iterator it = waiters->second.begin(); | |
316 | it != waiters->second.end(); ++it) { | |
f67539c2 | 317 | boost::asio::defer(client->finish_strand, CB_AioCompleteAndSafe(*it)); |
7c673cae FG |
318 | (*it)->put(); |
319 | } | |
320 | aio_write_waiters.erase(waiters++); | |
321 | } | |
322 | ||
9f95a23c TL |
323 | aio_write_cond.notify_all(); |
324 | aio_write_list_lock.unlock(); | |
7c673cae FG |
325 | put(); |
326 | } | |
327 | ||
328 | void librados::IoCtxImpl::flush_aio_writes_async(AioCompletionImpl *c) | |
329 | { | |
330 | ldout(client->cct, 20) << "flush_aio_writes_async " << this | |
331 | << " completion " << c << dendl; | |
11fdf7f2 | 332 | std::lock_guard l(aio_write_list_lock); |
7c673cae FG |
333 | ceph_tid_t seq = aio_write_seq; |
334 | if (aio_write_list.empty()) { | |
335 | ldout(client->cct, 20) << "flush_aio_writes_async no writes. (tid " | |
336 | << seq << ")" << dendl; | |
f67539c2 | 337 | boost::asio::defer(client->finish_strand, CB_AioCompleteAndSafe(c)); |
7c673cae FG |
338 | } else { |
339 | ldout(client->cct, 20) << "flush_aio_writes_async " << aio_write_list.size() | |
340 | << " writes in flight; waiting on tid " << seq << dendl; | |
341 | c->get(); | |
342 | aio_write_waiters[seq].push_back(c); | |
343 | } | |
344 | } | |
345 | ||
346 | void librados::IoCtxImpl::flush_aio_writes() | |
347 | { | |
348 | ldout(client->cct, 20) << "flush_aio_writes" << dendl; | |
9f95a23c TL |
349 | std::unique_lock l{aio_write_list_lock}; |
350 | aio_write_cond.wait(l, [seq=aio_write_seq, this] { | |
351 | return (aio_write_list.empty() || | |
352 | aio_write_list.front()->aio_write_seq > seq); | |
353 | }); | |
7c673cae FG |
354 | } |
355 | ||
356 | string librados::IoCtxImpl::get_cached_pool_name() | |
357 | { | |
358 | std::string pn; | |
359 | client->pool_get_name(get_id(), &pn); | |
360 | return pn; | |
361 | } | |
362 | ||
363 | // SNAPS | |
364 | ||
365 | int librados::IoCtxImpl::snap_create(const char *snapName) | |
366 | { | |
367 | int reply; | |
368 | string sName(snapName); | |
369 | ||
9f95a23c TL |
370 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::snap_create::mylock"); |
371 | ceph::condition_variable cond; | |
7c673cae | 372 | bool done; |
9f95a23c | 373 | Context *onfinish = new C_SafeCond(mylock, cond, &done, &reply); |
f67539c2 | 374 | objecter->create_pool_snap(poolid, sName, onfinish); |
7c673cae | 375 | |
f67539c2 TL |
376 | std::unique_lock l{mylock}; |
377 | cond.wait(l, [&done] { return done; }); | |
7c673cae FG |
378 | return reply; |
379 | } | |
380 | ||
381 | int librados::IoCtxImpl::selfmanaged_snap_create(uint64_t *psnapid) | |
382 | { | |
383 | int reply; | |
384 | ||
9f95a23c TL |
385 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::selfmanaged_snap_create::mylock"); |
386 | ceph::condition_variable cond; | |
7c673cae | 387 | bool done; |
9f95a23c | 388 | Context *onfinish = new C_SafeCond(mylock, cond, &done, &reply); |
7c673cae | 389 | snapid_t snapid; |
f67539c2 | 390 | objecter->allocate_selfmanaged_snap(poolid, &snapid, onfinish); |
7c673cae | 391 | |
f67539c2 TL |
392 | { |
393 | std::unique_lock l{mylock}; | |
394 | cond.wait(l, [&done] { return done; }); | |
7c673cae | 395 | } |
f67539c2 TL |
396 | if (reply == 0) |
397 | *psnapid = snapid; | |
7c673cae FG |
398 | return reply; |
399 | } | |
400 | ||
401 | void librados::IoCtxImpl::aio_selfmanaged_snap_create(uint64_t *snapid, | |
402 | AioCompletionImpl *c) | |
403 | { | |
404 | C_aio_selfmanaged_snap_create_Complete *onfinish = | |
405 | new C_aio_selfmanaged_snap_create_Complete(client, c, snapid); | |
f67539c2 TL |
406 | objecter->allocate_selfmanaged_snap(poolid, &onfinish->snapid, |
407 | onfinish); | |
7c673cae FG |
408 | } |
409 | ||
410 | int librados::IoCtxImpl::snap_remove(const char *snapName) | |
411 | { | |
412 | int reply; | |
413 | string sName(snapName); | |
414 | ||
9f95a23c TL |
415 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::snap_remove::mylock"); |
416 | ceph::condition_variable cond; | |
7c673cae | 417 | bool done; |
9f95a23c | 418 | Context *onfinish = new C_SafeCond(mylock, cond, &done, &reply); |
f67539c2 TL |
419 | objecter->delete_pool_snap(poolid, sName, onfinish); |
420 | unique_lock l{mylock}; | |
421 | cond.wait(l, [&done] { return done; }); | |
7c673cae FG |
422 | return reply; |
423 | } | |
424 | ||
425 | int librados::IoCtxImpl::selfmanaged_snap_rollback_object(const object_t& oid, | |
426 | ::SnapContext& snapc, | |
427 | uint64_t snapid) | |
428 | { | |
429 | int reply; | |
430 | ||
9f95a23c TL |
431 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::snap_rollback::mylock"); |
432 | ceph::condition_variable cond; | |
7c673cae | 433 | bool done; |
9f95a23c | 434 | Context *onack = new C_SafeCond(mylock, cond, &done, &reply); |
7c673cae FG |
435 | |
436 | ::ObjectOperation op; | |
437 | prepare_assert_ops(&op); | |
438 | op.rollback(snapid); | |
439 | objecter->mutate(oid, oloc, | |
f67539c2 TL |
440 | op, snapc, ceph::real_clock::now(), |
441 | extra_op_flags, | |
7c673cae FG |
442 | onack, NULL); |
443 | ||
9f95a23c TL |
444 | std::unique_lock l{mylock}; |
445 | cond.wait(l, [&done] { return done; }); | |
7c673cae FG |
446 | return reply; |
447 | } | |
448 | ||
449 | int librados::IoCtxImpl::rollback(const object_t& oid, const char *snapName) | |
450 | { | |
451 | snapid_t snap; | |
452 | ||
453 | int r = objecter->pool_snap_by_name(poolid, snapName, &snap); | |
454 | if (r < 0) { | |
455 | return r; | |
456 | } | |
457 | ||
458 | return selfmanaged_snap_rollback_object(oid, snapc, snap); | |
459 | } | |
460 | ||
461 | int librados::IoCtxImpl::selfmanaged_snap_remove(uint64_t snapid) | |
462 | { | |
463 | int reply; | |
464 | ||
9f95a23c TL |
465 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::selfmanaged_snap_remove::mylock"); |
466 | ceph::condition_variable cond; | |
7c673cae FG |
467 | bool done; |
468 | objecter->delete_selfmanaged_snap(poolid, snapid_t(snapid), | |
9f95a23c | 469 | new C_SafeCond(mylock, cond, &done, &reply)); |
7c673cae | 470 | |
9f95a23c TL |
471 | std::unique_lock l{mylock}; |
472 | cond.wait(l, [&done] { return done; }); | |
7c673cae FG |
473 | return (int)reply; |
474 | } | |
475 | ||
476 | void librados::IoCtxImpl::aio_selfmanaged_snap_remove(uint64_t snapid, | |
477 | AioCompletionImpl *c) | |
478 | { | |
479 | Context *onfinish = new C_aio_selfmanaged_snap_op_Complete(client, c); | |
480 | objecter->delete_selfmanaged_snap(poolid, snapid, onfinish); | |
481 | } | |
482 | ||
7c673cae FG |
483 | int librados::IoCtxImpl::snap_list(vector<uint64_t> *snaps) |
484 | { | |
485 | return objecter->pool_snap_list(poolid, snaps); | |
486 | } | |
487 | ||
488 | int librados::IoCtxImpl::snap_lookup(const char *name, uint64_t *snapid) | |
489 | { | |
490 | return objecter->pool_snap_by_name(poolid, name, (snapid_t *)snapid); | |
491 | } | |
492 | ||
493 | int librados::IoCtxImpl::snap_get_name(uint64_t snapid, std::string *s) | |
494 | { | |
495 | pool_snap_info_t info; | |
496 | int ret = objecter->pool_snap_get_info(poolid, snapid, &info); | |
497 | if (ret < 0) { | |
498 | return ret; | |
499 | } | |
500 | *s = info.name.c_str(); | |
501 | return 0; | |
502 | } | |
503 | ||
504 | int librados::IoCtxImpl::snap_get_stamp(uint64_t snapid, time_t *t) | |
505 | { | |
506 | pool_snap_info_t info; | |
507 | int ret = objecter->pool_snap_get_info(poolid, snapid, &info); | |
508 | if (ret < 0) { | |
509 | return ret; | |
510 | } | |
511 | *t = info.stamp.sec(); | |
512 | return 0; | |
513 | } | |
514 | ||
515 | ||
516 | // IO | |
517 | ||
518 | int librados::IoCtxImpl::nlist(Objecter::NListContext *context, int max_entries) | |
519 | { | |
7c673cae FG |
520 | bool done; |
521 | int r = 0; | |
9f95a23c TL |
522 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::nlist::mylock"); |
523 | ceph::condition_variable cond; | |
7c673cae FG |
524 | |
525 | if (context->at_end()) | |
526 | return 0; | |
527 | ||
528 | context->max_entries = max_entries; | |
529 | context->nspace = oloc.nspace; | |
530 | ||
9f95a23c | 531 | objecter->list_nobjects(context, new C_SafeCond(mylock, cond, &done, &r)); |
7c673cae | 532 | |
9f95a23c TL |
533 | std::unique_lock l{mylock}; |
534 | cond.wait(l, [&done] { return done; }); | |
7c673cae FG |
535 | return r; |
536 | } | |
537 | ||
538 | uint32_t librados::IoCtxImpl::nlist_seek(Objecter::NListContext *context, | |
539 | uint32_t pos) | |
540 | { | |
541 | context->list.clear(); | |
542 | return objecter->list_nobjects_seek(context, pos); | |
543 | } | |
544 | ||
545 | uint32_t librados::IoCtxImpl::nlist_seek(Objecter::NListContext *context, | |
546 | const rados_object_list_cursor& cursor) | |
547 | { | |
548 | context->list.clear(); | |
549 | return objecter->list_nobjects_seek(context, *(const hobject_t *)cursor); | |
550 | } | |
551 | ||
552 | rados_object_list_cursor librados::IoCtxImpl::nlist_get_cursor(Objecter::NListContext *context) | |
553 | { | |
554 | hobject_t *c = new hobject_t; | |
555 | ||
556 | objecter->list_nobjects_get_cursor(context, c); | |
557 | return (rados_object_list_cursor)c; | |
558 | } | |
559 | ||
560 | int librados::IoCtxImpl::create(const object_t& oid, bool exclusive) | |
561 | { | |
562 | ::ObjectOperation op; | |
563 | prepare_assert_ops(&op); | |
564 | op.create(exclusive); | |
565 | return operate(oid, &op, NULL); | |
566 | } | |
567 | ||
568 | /* | |
569 | * add any version assert operations that are appropriate given the | |
570 | * stat in the IoCtx, either the target version assert or any src | |
571 | * object asserts. these affect a single ioctx operation, so clear | |
572 | * the ioctx state when we're doing. | |
573 | * | |
574 | * return a pointer to the ObjectOperation if we added any events; | |
575 | * this is convenient for passing the extra_ops argument into Objecter | |
576 | * methods. | |
577 | */ | |
578 | ::ObjectOperation *librados::IoCtxImpl::prepare_assert_ops(::ObjectOperation *op) | |
579 | { | |
580 | ::ObjectOperation *pop = NULL; | |
581 | if (assert_ver) { | |
582 | op->assert_version(assert_ver); | |
583 | assert_ver = 0; | |
584 | pop = op; | |
585 | } | |
586 | return pop; | |
587 | } | |
588 | ||
589 | int librados::IoCtxImpl::write(const object_t& oid, bufferlist& bl, | |
590 | size_t len, uint64_t off) | |
591 | { | |
592 | if (len > UINT_MAX/2) | |
593 | return -E2BIG; | |
594 | ::ObjectOperation op; | |
595 | prepare_assert_ops(&op); | |
596 | bufferlist mybl; | |
597 | mybl.substr_of(bl, 0, len); | |
598 | op.write(off, mybl); | |
599 | return operate(oid, &op, NULL); | |
600 | } | |
601 | ||
602 | int librados::IoCtxImpl::append(const object_t& oid, bufferlist& bl, size_t len) | |
603 | { | |
604 | if (len > UINT_MAX/2) | |
605 | return -E2BIG; | |
606 | ::ObjectOperation op; | |
607 | prepare_assert_ops(&op); | |
608 | bufferlist mybl; | |
609 | mybl.substr_of(bl, 0, len); | |
610 | op.append(mybl); | |
611 | return operate(oid, &op, NULL); | |
612 | } | |
613 | ||
614 | int librados::IoCtxImpl::write_full(const object_t& oid, bufferlist& bl) | |
615 | { | |
616 | if (bl.length() > UINT_MAX/2) | |
617 | return -E2BIG; | |
618 | ::ObjectOperation op; | |
619 | prepare_assert_ops(&op); | |
620 | op.write_full(bl); | |
621 | return operate(oid, &op, NULL); | |
622 | } | |
623 | ||
624 | int librados::IoCtxImpl::writesame(const object_t& oid, bufferlist& bl, | |
625 | size_t write_len, uint64_t off) | |
626 | { | |
627 | if ((bl.length() > UINT_MAX/2) || (write_len > UINT_MAX/2)) | |
628 | return -E2BIG; | |
629 | if ((bl.length() == 0) || (write_len % bl.length())) | |
630 | return -EINVAL; | |
631 | ::ObjectOperation op; | |
632 | prepare_assert_ops(&op); | |
633 | bufferlist mybl; | |
634 | mybl.substr_of(bl, 0, bl.length()); | |
635 | op.writesame(off, write_len, mybl); | |
636 | return operate(oid, &op, NULL); | |
637 | } | |
638 | ||
639 | int librados::IoCtxImpl::operate(const object_t& oid, ::ObjectOperation *o, | |
640 | ceph::real_time *pmtime, int flags) | |
641 | { | |
642 | ceph::real_time ut = (pmtime ? *pmtime : | |
643 | ceph::real_clock::now()); | |
644 | ||
645 | /* can't write to a snapshot */ | |
646 | if (snap_seq != CEPH_NOSNAP) | |
647 | return -EROFS; | |
648 | ||
649 | if (!o->size()) | |
650 | return 0; | |
651 | ||
9f95a23c TL |
652 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::operate::mylock"); |
653 | ceph::condition_variable cond; | |
7c673cae FG |
654 | bool done; |
655 | int r; | |
656 | version_t ver; | |
657 | ||
9f95a23c | 658 | Context *oncommit = new C_SafeCond(mylock, cond, &done, &r); |
7c673cae FG |
659 | |
660 | int op = o->ops[0].op.op; | |
661 | ldout(client->cct, 10) << ceph_osd_op_name(op) << " oid=" << oid | |
662 | << " nspace=" << oloc.nspace << dendl; | |
f67539c2 TL |
663 | Objecter::Op *objecter_op = objecter->prepare_mutate_op( |
664 | oid, oloc, | |
665 | *o, snapc, ut, | |
666 | flags | extra_op_flags, | |
667 | oncommit, &ver); | |
7c673cae FG |
668 | objecter->op_submit(objecter_op); |
669 | ||
9f95a23c TL |
670 | { |
671 | std::unique_lock l{mylock}; | |
672 | cond.wait(l, [&done] { return done;}); | |
673 | } | |
7c673cae FG |
674 | ldout(client->cct, 10) << "Objecter returned from " |
675 | << ceph_osd_op_name(op) << " r=" << r << dendl; | |
676 | ||
677 | set_sync_op_version(ver); | |
678 | ||
679 | return r; | |
680 | } | |
681 | ||
682 | int librados::IoCtxImpl::operate_read(const object_t& oid, | |
683 | ::ObjectOperation *o, | |
684 | bufferlist *pbl, | |
685 | int flags) | |
686 | { | |
687 | if (!o->size()) | |
688 | return 0; | |
689 | ||
9f95a23c TL |
690 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::operate_read::mylock"); |
691 | ceph::condition_variable cond; | |
7c673cae FG |
692 | bool done; |
693 | int r; | |
694 | version_t ver; | |
695 | ||
9f95a23c | 696 | Context *onack = new C_SafeCond(mylock, cond, &done, &r); |
7c673cae FG |
697 | |
698 | int op = o->ops[0].op.op; | |
699 | ldout(client->cct, 10) << ceph_osd_op_name(op) << " oid=" << oid << " nspace=" << oloc.nspace << dendl; | |
f67539c2 TL |
700 | Objecter::Op *objecter_op = objecter->prepare_read_op( |
701 | oid, oloc, | |
702 | *o, snap_seq, pbl, | |
703 | flags | extra_op_flags, | |
704 | onack, &ver); | |
7c673cae FG |
705 | objecter->op_submit(objecter_op); |
706 | ||
9f95a23c TL |
707 | { |
708 | std::unique_lock l{mylock}; | |
709 | cond.wait(l, [&done] { return done; }); | |
710 | } | |
7c673cae FG |
711 | ldout(client->cct, 10) << "Objecter returned from " |
712 | << ceph_osd_op_name(op) << " r=" << r << dendl; | |
713 | ||
714 | set_sync_op_version(ver); | |
715 | ||
716 | return r; | |
717 | } | |
718 | ||
719 | int librados::IoCtxImpl::aio_operate_read(const object_t &oid, | |
720 | ::ObjectOperation *o, | |
721 | AioCompletionImpl *c, | |
722 | int flags, | |
723 | bufferlist *pbl, | |
31f18b77 | 724 | const blkin_trace_info *trace_info) |
7c673cae | 725 | { |
11fdf7f2 | 726 | FUNCTRACE(client->cct); |
7c673cae FG |
727 | Context *oncomplete = new C_aio_Complete(c); |
728 | ||
9f95a23c | 729 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
730 | ((C_aio_Complete *) oncomplete)->oid = oid; |
731 | #endif | |
732 | c->is_read = true; | |
733 | c->io = this; | |
734 | ||
735 | ZTracer::Trace trace; | |
31f18b77 FG |
736 | if (trace_info) { |
737 | ZTracer::Trace parent_trace("", nullptr, trace_info); | |
738 | trace.init("rados operate read", &objecter->trace_endpoint, &parent_trace); | |
739 | } | |
7c673cae FG |
740 | |
741 | trace.event("init root span"); | |
f67539c2 TL |
742 | Objecter::Op *objecter_op = objecter->prepare_read_op( |
743 | oid, oloc, | |
744 | *o, snap_seq, pbl, flags | extra_op_flags, | |
745 | oncomplete, &c->objver, nullptr, 0, &trace); | |
7c673cae FG |
746 | objecter->op_submit(objecter_op, &c->tid); |
747 | trace.event("rados operate read submitted"); | |
748 | ||
749 | return 0; | |
750 | } | |
751 | ||
752 | int librados::IoCtxImpl::aio_operate(const object_t& oid, | |
753 | ::ObjectOperation *o, AioCompletionImpl *c, | |
754 | const SnapContext& snap_context, int flags, | |
31f18b77 | 755 | const blkin_trace_info *trace_info) |
7c673cae | 756 | { |
11fdf7f2 | 757 | FUNCTRACE(client->cct); |
7c673cae FG |
758 | OID_EVENT_TRACE(oid.name.c_str(), "RADOS_WRITE_OP_BEGIN"); |
759 | auto ut = ceph::real_clock::now(); | |
760 | /* can't write to a snapshot */ | |
761 | if (snap_seq != CEPH_NOSNAP) | |
762 | return -EROFS; | |
763 | ||
764 | Context *oncomplete = new C_aio_Complete(c); | |
9f95a23c | 765 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
766 | ((C_aio_Complete *) oncomplete)->oid = oid; |
767 | #endif | |
768 | ||
769 | c->io = this; | |
770 | queue_aio_write(c); | |
771 | ||
772 | ZTracer::Trace trace; | |
31f18b77 FG |
773 | if (trace_info) { |
774 | ZTracer::Trace parent_trace("", nullptr, trace_info); | |
775 | trace.init("rados operate", &objecter->trace_endpoint, &parent_trace); | |
776 | } | |
7c673cae FG |
777 | |
778 | trace.event("init root span"); | |
779 | Objecter::Op *op = objecter->prepare_mutate_op( | |
f67539c2 | 780 | oid, oloc, *o, snap_context, ut, flags | extra_op_flags, |
7c673cae FG |
781 | oncomplete, &c->objver, osd_reqid_t(), &trace); |
782 | objecter->op_submit(op, &c->tid); | |
783 | trace.event("rados operate op submitted"); | |
784 | ||
785 | return 0; | |
786 | } | |
787 | ||
788 | int librados::IoCtxImpl::aio_read(const object_t oid, AioCompletionImpl *c, | |
789 | bufferlist *pbl, size_t len, uint64_t off, | |
790 | uint64_t snapid, const blkin_trace_info *info) | |
791 | { | |
11fdf7f2 | 792 | FUNCTRACE(client->cct); |
7c673cae FG |
793 | if (len > (size_t) INT_MAX) |
794 | return -EDOM; | |
795 | ||
796 | OID_EVENT_TRACE(oid.name.c_str(), "RADOS_READ_OP_BEGIN"); | |
797 | Context *oncomplete = new C_aio_Complete(c); | |
798 | ||
9f95a23c | 799 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
800 | ((C_aio_Complete *) oncomplete)->oid = oid; |
801 | #endif | |
802 | c->is_read = true; | |
803 | c->io = this; | |
804 | c->blp = pbl; | |
805 | ||
806 | ZTracer::Trace trace; | |
807 | if (info) | |
808 | trace.init("rados read", &objecter->trace_endpoint, info); | |
809 | ||
810 | Objecter::Op *o = objecter->prepare_read_op( | |
811 | oid, oloc, | |
f67539c2 | 812 | off, len, snapid, pbl, extra_op_flags, |
7c673cae FG |
813 | oncomplete, &c->objver, nullptr, 0, &trace); |
814 | objecter->op_submit(o, &c->tid); | |
815 | return 0; | |
816 | } | |
817 | ||
818 | int librados::IoCtxImpl::aio_read(const object_t oid, AioCompletionImpl *c, | |
819 | char *buf, size_t len, uint64_t off, | |
820 | uint64_t snapid, const blkin_trace_info *info) | |
821 | { | |
11fdf7f2 | 822 | FUNCTRACE(client->cct); |
7c673cae FG |
823 | if (len > (size_t) INT_MAX) |
824 | return -EDOM; | |
825 | ||
826 | OID_EVENT_TRACE(oid.name.c_str(), "RADOS_READ_OP_BEGIN"); | |
827 | Context *oncomplete = new C_aio_Complete(c); | |
828 | ||
9f95a23c | 829 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
830 | ((C_aio_Complete *) oncomplete)->oid = oid; |
831 | #endif | |
832 | c->is_read = true; | |
833 | c->io = this; | |
834 | c->bl.clear(); | |
835 | c->bl.push_back(buffer::create_static(len, buf)); | |
836 | c->blp = &c->bl; | |
837 | c->out_buf = buf; | |
838 | ||
839 | ZTracer::Trace trace; | |
840 | if (info) | |
841 | trace.init("rados read", &objecter->trace_endpoint, info); | |
842 | ||
843 | Objecter::Op *o = objecter->prepare_read_op( | |
844 | oid, oloc, | |
f67539c2 | 845 | off, len, snapid, &c->bl, extra_op_flags, |
7c673cae FG |
846 | oncomplete, &c->objver, nullptr, 0, &trace); |
847 | objecter->op_submit(o, &c->tid); | |
848 | return 0; | |
849 | } | |
850 | ||
851 | class C_ObjectOperation : public Context { | |
852 | public: | |
853 | ::ObjectOperation m_ops; | |
854 | explicit C_ObjectOperation(Context *c) : m_ctx(c) {} | |
855 | void finish(int r) override { | |
856 | m_ctx->complete(r); | |
857 | } | |
858 | private: | |
859 | Context *m_ctx; | |
860 | }; | |
861 | ||
862 | int librados::IoCtxImpl::aio_sparse_read(const object_t oid, | |
863 | AioCompletionImpl *c, | |
864 | std::map<uint64_t,uint64_t> *m, | |
865 | bufferlist *data_bl, size_t len, | |
866 | uint64_t off, uint64_t snapid) | |
867 | { | |
11fdf7f2 | 868 | FUNCTRACE(client->cct); |
7c673cae FG |
869 | if (len > (size_t) INT_MAX) |
870 | return -EDOM; | |
871 | ||
872 | Context *nested = new C_aio_Complete(c); | |
873 | C_ObjectOperation *onack = new C_ObjectOperation(nested); | |
874 | ||
9f95a23c | 875 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
876 | ((C_aio_Complete *) nested)->oid = oid; |
877 | #endif | |
878 | c->is_read = true; | |
879 | c->io = this; | |
880 | ||
881 | onack->m_ops.sparse_read(off, len, m, data_bl, NULL); | |
882 | ||
883 | Objecter::Op *o = objecter->prepare_read_op( | |
884 | oid, oloc, | |
f67539c2 | 885 | onack->m_ops, snapid, NULL, extra_op_flags, |
7c673cae FG |
886 | onack, &c->objver); |
887 | objecter->op_submit(o, &c->tid); | |
888 | return 0; | |
889 | } | |
890 | ||
891 | int librados::IoCtxImpl::aio_cmpext(const object_t& oid, | |
892 | AioCompletionImpl *c, | |
893 | uint64_t off, | |
894 | bufferlist& cmp_bl) | |
895 | { | |
896 | if (cmp_bl.length() > UINT_MAX/2) | |
897 | return -E2BIG; | |
898 | ||
899 | Context *onack = new C_aio_Complete(c); | |
900 | ||
901 | c->is_read = true; | |
902 | c->io = this; | |
903 | ||
904 | Objecter::Op *o = objecter->prepare_cmpext_op( | |
f67539c2 | 905 | oid, oloc, off, cmp_bl, snap_seq, extra_op_flags, |
7c673cae FG |
906 | onack, &c->objver); |
907 | objecter->op_submit(o, &c->tid); | |
908 | ||
909 | return 0; | |
910 | } | |
911 | ||
912 | /* use m_ops.cmpext() + prepare_read_op() for non-bufferlist C API */ | |
913 | int librados::IoCtxImpl::aio_cmpext(const object_t& oid, | |
914 | AioCompletionImpl *c, | |
915 | const char *cmp_buf, | |
916 | size_t cmp_len, | |
917 | uint64_t off) | |
918 | { | |
919 | if (cmp_len > UINT_MAX/2) | |
920 | return -E2BIG; | |
921 | ||
922 | bufferlist cmp_bl; | |
923 | cmp_bl.append(cmp_buf, cmp_len); | |
924 | ||
925 | Context *nested = new C_aio_Complete(c); | |
926 | C_ObjectOperation *onack = new C_ObjectOperation(nested); | |
927 | ||
928 | c->is_read = true; | |
929 | c->io = this; | |
930 | ||
931 | onack->m_ops.cmpext(off, cmp_len, cmp_buf, NULL); | |
932 | ||
933 | Objecter::Op *o = objecter->prepare_read_op( | |
f67539c2 | 934 | oid, oloc, onack->m_ops, snap_seq, NULL, extra_op_flags, onack, &c->objver); |
7c673cae FG |
935 | objecter->op_submit(o, &c->tid); |
936 | return 0; | |
937 | } | |
938 | ||
939 | int librados::IoCtxImpl::aio_write(const object_t &oid, AioCompletionImpl *c, | |
940 | const bufferlist& bl, size_t len, | |
941 | uint64_t off, const blkin_trace_info *info) | |
942 | { | |
11fdf7f2 | 943 | FUNCTRACE(client->cct); |
7c673cae FG |
944 | auto ut = ceph::real_clock::now(); |
945 | ldout(client->cct, 20) << "aio_write " << oid << " " << off << "~" << len << " snapc=" << snapc << " snap_seq=" << snap_seq << dendl; | |
946 | OID_EVENT_TRACE(oid.name.c_str(), "RADOS_WRITE_OP_BEGIN"); | |
947 | ||
948 | if (len > UINT_MAX/2) | |
949 | return -E2BIG; | |
950 | /* can't write to a snapshot */ | |
951 | if (snap_seq != CEPH_NOSNAP) | |
952 | return -EROFS; | |
953 | ||
954 | Context *oncomplete = new C_aio_Complete(c); | |
955 | ||
9f95a23c | 956 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
957 | ((C_aio_Complete *) oncomplete)->oid = oid; |
958 | #endif | |
959 | ZTracer::Trace trace; | |
960 | if (info) | |
961 | trace.init("rados write", &objecter->trace_endpoint, info); | |
962 | ||
963 | c->io = this; | |
964 | queue_aio_write(c); | |
965 | ||
966 | Objecter::Op *o = objecter->prepare_write_op( | |
967 | oid, oloc, | |
f67539c2 | 968 | off, len, snapc, bl, ut, extra_op_flags, |
7c673cae FG |
969 | oncomplete, &c->objver, nullptr, 0, &trace); |
970 | objecter->op_submit(o, &c->tid); | |
971 | ||
972 | return 0; | |
973 | } | |
974 | ||
975 | int librados::IoCtxImpl::aio_append(const object_t &oid, AioCompletionImpl *c, | |
976 | const bufferlist& bl, size_t len) | |
977 | { | |
11fdf7f2 | 978 | FUNCTRACE(client->cct); |
7c673cae FG |
979 | auto ut = ceph::real_clock::now(); |
980 | ||
981 | if (len > UINT_MAX/2) | |
982 | return -E2BIG; | |
983 | /* can't write to a snapshot */ | |
984 | if (snap_seq != CEPH_NOSNAP) | |
985 | return -EROFS; | |
986 | ||
987 | Context *oncomplete = new C_aio_Complete(c); | |
9f95a23c | 988 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
989 | ((C_aio_Complete *) oncomplete)->oid = oid; |
990 | #endif | |
991 | ||
992 | c->io = this; | |
993 | queue_aio_write(c); | |
994 | ||
995 | Objecter::Op *o = objecter->prepare_append_op( | |
996 | oid, oloc, | |
f67539c2 | 997 | len, snapc, bl, ut, extra_op_flags, |
7c673cae FG |
998 | oncomplete, &c->objver); |
999 | objecter->op_submit(o, &c->tid); | |
1000 | ||
1001 | return 0; | |
1002 | } | |
1003 | ||
1004 | int librados::IoCtxImpl::aio_write_full(const object_t &oid, | |
1005 | AioCompletionImpl *c, | |
1006 | const bufferlist& bl) | |
1007 | { | |
11fdf7f2 | 1008 | FUNCTRACE(client->cct); |
7c673cae FG |
1009 | auto ut = ceph::real_clock::now(); |
1010 | ||
1011 | if (bl.length() > UINT_MAX/2) | |
1012 | return -E2BIG; | |
1013 | /* can't write to a snapshot */ | |
1014 | if (snap_seq != CEPH_NOSNAP) | |
1015 | return -EROFS; | |
1016 | ||
1017 | Context *oncomplete = new C_aio_Complete(c); | |
9f95a23c | 1018 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
1019 | ((C_aio_Complete *) oncomplete)->oid = oid; |
1020 | #endif | |
1021 | ||
1022 | c->io = this; | |
1023 | queue_aio_write(c); | |
1024 | ||
1025 | Objecter::Op *o = objecter->prepare_write_full_op( | |
1026 | oid, oloc, | |
f67539c2 | 1027 | snapc, bl, ut, extra_op_flags, |
7c673cae FG |
1028 | oncomplete, &c->objver); |
1029 | objecter->op_submit(o, &c->tid); | |
1030 | ||
1031 | return 0; | |
1032 | } | |
1033 | ||
1034 | int librados::IoCtxImpl::aio_writesame(const object_t &oid, | |
1035 | AioCompletionImpl *c, | |
1036 | const bufferlist& bl, | |
1037 | size_t write_len, | |
1038 | uint64_t off) | |
1039 | { | |
11fdf7f2 | 1040 | FUNCTRACE(client->cct); |
7c673cae FG |
1041 | auto ut = ceph::real_clock::now(); |
1042 | ||
1043 | if ((bl.length() > UINT_MAX/2) || (write_len > UINT_MAX/2)) | |
1044 | return -E2BIG; | |
1045 | if ((bl.length() == 0) || (write_len % bl.length())) | |
1046 | return -EINVAL; | |
1047 | /* can't write to a snapshot */ | |
1048 | if (snap_seq != CEPH_NOSNAP) | |
1049 | return -EROFS; | |
1050 | ||
1051 | Context *oncomplete = new C_aio_Complete(c); | |
1052 | ||
9f95a23c | 1053 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
1054 | ((C_aio_Complete *) oncomplete)->oid = oid; |
1055 | #endif | |
1056 | c->io = this; | |
1057 | queue_aio_write(c); | |
1058 | ||
1059 | Objecter::Op *o = objecter->prepare_writesame_op( | |
1060 | oid, oloc, | |
1061 | write_len, off, | |
f67539c2 | 1062 | snapc, bl, ut, extra_op_flags, |
7c673cae FG |
1063 | oncomplete, &c->objver); |
1064 | objecter->op_submit(o, &c->tid); | |
1065 | ||
1066 | return 0; | |
1067 | } | |
1068 | ||
1069 | int librados::IoCtxImpl::aio_remove(const object_t &oid, AioCompletionImpl *c, int flags) | |
1070 | { | |
11fdf7f2 | 1071 | FUNCTRACE(client->cct); |
7c673cae FG |
1072 | auto ut = ceph::real_clock::now(); |
1073 | ||
1074 | /* can't write to a snapshot */ | |
1075 | if (snap_seq != CEPH_NOSNAP) | |
1076 | return -EROFS; | |
1077 | ||
1078 | Context *oncomplete = new C_aio_Complete(c); | |
1079 | ||
9f95a23c | 1080 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
1081 | ((C_aio_Complete *) oncomplete)->oid = oid; |
1082 | #endif | |
1083 | c->io = this; | |
1084 | queue_aio_write(c); | |
1085 | ||
1086 | Objecter::Op *o = objecter->prepare_remove_op( | |
1087 | oid, oloc, | |
f67539c2 | 1088 | snapc, ut, flags | extra_op_flags, |
7c673cae FG |
1089 | oncomplete, &c->objver); |
1090 | objecter->op_submit(o, &c->tid); | |
1091 | ||
1092 | return 0; | |
1093 | } | |
1094 | ||
1095 | ||
1096 | int librados::IoCtxImpl::aio_stat(const object_t& oid, AioCompletionImpl *c, | |
1097 | uint64_t *psize, time_t *pmtime) | |
1098 | { | |
1099 | C_aio_stat_Ack *onack = new C_aio_stat_Ack(c, pmtime); | |
1100 | c->is_read = true; | |
1101 | c->io = this; | |
1102 | Objecter::Op *o = objecter->prepare_stat_op( | |
1103 | oid, oloc, | |
f67539c2 | 1104 | snap_seq, psize, &onack->mtime, extra_op_flags, |
7c673cae FG |
1105 | onack, &c->objver); |
1106 | objecter->op_submit(o, &c->tid); | |
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | int librados::IoCtxImpl::aio_stat2(const object_t& oid, AioCompletionImpl *c, | |
1111 | uint64_t *psize, struct timespec *pts) | |
1112 | { | |
1113 | C_aio_stat2_Ack *onack = new C_aio_stat2_Ack(c, pts); | |
1114 | c->is_read = true; | |
1115 | c->io = this; | |
1116 | Objecter::Op *o = objecter->prepare_stat_op( | |
1117 | oid, oloc, | |
f67539c2 | 1118 | snap_seq, psize, &onack->mtime, extra_op_flags, |
7c673cae FG |
1119 | onack, &c->objver); |
1120 | objecter->op_submit(o, &c->tid); | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | int librados::IoCtxImpl::aio_getxattr(const object_t& oid, AioCompletionImpl *c, | |
1125 | const char *name, bufferlist& bl) | |
1126 | { | |
1127 | ::ObjectOperation rd; | |
1128 | prepare_assert_ops(&rd); | |
1129 | rd.getxattr(name, &bl, NULL); | |
1130 | int r = aio_operate_read(oid, &rd, c, 0, &bl); | |
1131 | return r; | |
1132 | } | |
1133 | ||
1134 | int librados::IoCtxImpl::aio_rmxattr(const object_t& oid, AioCompletionImpl *c, | |
1135 | const char *name) | |
1136 | { | |
1137 | ::ObjectOperation op; | |
1138 | prepare_assert_ops(&op); | |
1139 | op.rmxattr(name); | |
1140 | return aio_operate(oid, &op, c, snapc, 0); | |
1141 | } | |
1142 | ||
1143 | int librados::IoCtxImpl::aio_setxattr(const object_t& oid, AioCompletionImpl *c, | |
1144 | const char *name, bufferlist& bl) | |
1145 | { | |
1146 | ::ObjectOperation op; | |
1147 | prepare_assert_ops(&op); | |
1148 | op.setxattr(name, bl); | |
1149 | return aio_operate(oid, &op, c, snapc, 0); | |
1150 | } | |
1151 | ||
1152 | namespace { | |
1153 | struct AioGetxattrsData { | |
1154 | AioGetxattrsData(librados::AioCompletionImpl *c, map<string, bufferlist>* attrset, | |
1155 | librados::RadosClient *_client) : | |
1156 | user_completion(c), user_attrset(attrset), client(_client) {} | |
f67539c2 | 1157 | struct librados::CB_AioCompleteAndSafe user_completion; |
7c673cae FG |
1158 | map<string, bufferlist> result_attrset; |
1159 | map<std::string, bufferlist>* user_attrset; | |
1160 | librados::RadosClient *client; | |
1161 | }; | |
1162 | } | |
1163 | ||
1164 | static void aio_getxattrs_complete(rados_completion_t c, void *arg) { | |
1165 | AioGetxattrsData *cdata = reinterpret_cast<AioGetxattrsData*>(arg); | |
1166 | int rc = rados_aio_get_return_value(c); | |
1167 | cdata->user_attrset->clear(); | |
1168 | if (rc >= 0) { | |
1169 | for (map<string,bufferlist>::iterator p = cdata->result_attrset.begin(); | |
1170 | p != cdata->result_attrset.end(); | |
1171 | ++p) { | |
1172 | ldout(cdata->client->cct, 10) << "IoCtxImpl::getxattrs: xattr=" << p->first << dendl; | |
1173 | (*cdata->user_attrset)[p->first] = p->second; | |
1174 | } | |
1175 | } | |
f67539c2 | 1176 | cdata->user_completion(rc); |
7c673cae FG |
1177 | ((librados::AioCompletionImpl*)c)->put(); |
1178 | delete cdata; | |
1179 | } | |
1180 | ||
1181 | int librados::IoCtxImpl::aio_getxattrs(const object_t& oid, AioCompletionImpl *c, | |
1182 | map<std::string, bufferlist>& attrset) | |
1183 | { | |
1184 | AioGetxattrsData *cdata = new AioGetxattrsData(c, &attrset, client); | |
1185 | ::ObjectOperation rd; | |
1186 | prepare_assert_ops(&rd); | |
1187 | rd.getxattrs(&cdata->result_attrset, NULL); | |
1188 | librados::AioCompletionImpl *comp = new librados::AioCompletionImpl; | |
1189 | comp->set_complete_callback(cdata, aio_getxattrs_complete); | |
1190 | return aio_operate_read(oid, &rd, comp, 0, NULL); | |
1191 | } | |
1192 | ||
1193 | int librados::IoCtxImpl::aio_cancel(AioCompletionImpl *c) | |
1194 | { | |
1195 | return objecter->op_cancel(c->tid, -ECANCELED); | |
1196 | } | |
1197 | ||
1198 | ||
1199 | int librados::IoCtxImpl::hit_set_list(uint32_t hash, AioCompletionImpl *c, | |
1200 | std::list< std::pair<time_t, time_t> > *pls) | |
1201 | { | |
1202 | Context *oncomplete = new C_aio_Complete(c); | |
1203 | c->is_read = true; | |
1204 | c->io = this; | |
1205 | ||
1206 | ::ObjectOperation rd; | |
1207 | rd.hit_set_ls(pls, NULL); | |
1208 | object_locator_t oloc(poolid); | |
1209 | Objecter::Op *o = objecter->prepare_pg_read_op( | |
f67539c2 | 1210 | hash, oloc, rd, NULL, extra_op_flags, oncomplete, NULL, NULL); |
7c673cae FG |
1211 | objecter->op_submit(o, &c->tid); |
1212 | return 0; | |
1213 | } | |
1214 | ||
1215 | int librados::IoCtxImpl::hit_set_get(uint32_t hash, AioCompletionImpl *c, | |
1216 | time_t stamp, | |
1217 | bufferlist *pbl) | |
1218 | { | |
1219 | Context *oncomplete = new C_aio_Complete(c); | |
1220 | c->is_read = true; | |
1221 | c->io = this; | |
1222 | ||
1223 | ::ObjectOperation rd; | |
1224 | rd.hit_set_get(ceph::real_clock::from_time_t(stamp), pbl, 0); | |
1225 | object_locator_t oloc(poolid); | |
1226 | Objecter::Op *o = objecter->prepare_pg_read_op( | |
f67539c2 | 1227 | hash, oloc, rd, NULL, extra_op_flags, oncomplete, NULL, NULL); |
7c673cae FG |
1228 | objecter->op_submit(o, &c->tid); |
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | int librados::IoCtxImpl::remove(const object_t& oid) | |
1233 | { | |
1234 | ::ObjectOperation op; | |
1235 | prepare_assert_ops(&op); | |
1236 | op.remove(); | |
94b18763 | 1237 | return operate(oid, &op, nullptr, librados::OPERATION_FULL_FORCE); |
7c673cae FG |
1238 | } |
1239 | ||
1240 | int librados::IoCtxImpl::remove(const object_t& oid, int flags) | |
1241 | { | |
1242 | ::ObjectOperation op; | |
1243 | prepare_assert_ops(&op); | |
1244 | op.remove(); | |
1245 | return operate(oid, &op, NULL, flags); | |
1246 | } | |
1247 | ||
1248 | int librados::IoCtxImpl::trunc(const object_t& oid, uint64_t size) | |
1249 | { | |
1250 | ::ObjectOperation op; | |
1251 | prepare_assert_ops(&op); | |
1252 | op.truncate(size); | |
1253 | return operate(oid, &op, NULL); | |
1254 | } | |
1255 | ||
1256 | int librados::IoCtxImpl::get_inconsistent_objects(const pg_t& pg, | |
1257 | const librados::object_id_t& start_after, | |
1258 | uint64_t max_to_get, | |
1259 | AioCompletionImpl *c, | |
1260 | std::vector<inconsistent_obj_t>* objects, | |
1261 | uint32_t* interval) | |
1262 | { | |
1263 | Context *oncomplete = new C_aio_Complete(c); | |
1264 | c->is_read = true; | |
1265 | c->io = this; | |
1266 | ||
1267 | ::ObjectOperation op; | |
11fdf7f2 | 1268 | op.scrub_ls(start_after, max_to_get, objects, interval, &c->rval); |
7c673cae FG |
1269 | object_locator_t oloc{poolid, pg.ps()}; |
1270 | Objecter::Op *o = objecter->prepare_pg_read_op( | |
f67539c2 | 1271 | oloc.hash, oloc, op, nullptr, CEPH_OSD_FLAG_PGOP | extra_op_flags, oncomplete, |
7c673cae FG |
1272 | nullptr, nullptr); |
1273 | objecter->op_submit(o, &c->tid); | |
1274 | return 0; | |
1275 | } | |
1276 | ||
1277 | int librados::IoCtxImpl::get_inconsistent_snapsets(const pg_t& pg, | |
1278 | const librados::object_id_t& start_after, | |
1279 | uint64_t max_to_get, | |
1280 | AioCompletionImpl *c, | |
1281 | std::vector<inconsistent_snapset_t>* snapsets, | |
1282 | uint32_t* interval) | |
1283 | { | |
1284 | Context *oncomplete = new C_aio_Complete(c); | |
1285 | c->is_read = true; | |
1286 | c->io = this; | |
1287 | ||
1288 | ::ObjectOperation op; | |
11fdf7f2 | 1289 | op.scrub_ls(start_after, max_to_get, snapsets, interval, &c->rval); |
7c673cae FG |
1290 | object_locator_t oloc{poolid, pg.ps()}; |
1291 | Objecter::Op *o = objecter->prepare_pg_read_op( | |
f67539c2 | 1292 | oloc.hash, oloc, op, nullptr, CEPH_OSD_FLAG_PGOP | extra_op_flags, oncomplete, |
7c673cae FG |
1293 | nullptr, nullptr); |
1294 | objecter->op_submit(o, &c->tid); | |
1295 | return 0; | |
1296 | } | |
1297 | ||
1298 | int librados::IoCtxImpl::tmap_update(const object_t& oid, bufferlist& cmdbl) | |
1299 | { | |
1300 | ::ObjectOperation wr; | |
1301 | prepare_assert_ops(&wr); | |
1302 | wr.tmap_update(cmdbl); | |
1303 | return operate(oid, &wr, NULL); | |
1304 | } | |
1305 | ||
7c673cae FG |
1306 | int librados::IoCtxImpl::exec(const object_t& oid, |
1307 | const char *cls, const char *method, | |
1308 | bufferlist& inbl, bufferlist& outbl) | |
1309 | { | |
1310 | ::ObjectOperation rd; | |
1311 | prepare_assert_ops(&rd); | |
1312 | rd.call(cls, method, inbl); | |
1313 | return operate_read(oid, &rd, &outbl); | |
1314 | } | |
1315 | ||
1316 | int librados::IoCtxImpl::aio_exec(const object_t& oid, AioCompletionImpl *c, | |
1317 | const char *cls, const char *method, | |
1318 | bufferlist& inbl, bufferlist *outbl) | |
1319 | { | |
11fdf7f2 | 1320 | FUNCTRACE(client->cct); |
7c673cae FG |
1321 | Context *oncomplete = new C_aio_Complete(c); |
1322 | ||
9f95a23c | 1323 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
1324 | ((C_aio_Complete *) oncomplete)->oid = oid; |
1325 | #endif | |
1326 | c->is_read = true; | |
1327 | c->io = this; | |
1328 | ||
1329 | ::ObjectOperation rd; | |
1330 | prepare_assert_ops(&rd); | |
1331 | rd.call(cls, method, inbl); | |
1332 | Objecter::Op *o = objecter->prepare_read_op( | |
f67539c2 | 1333 | oid, oloc, rd, snap_seq, outbl, extra_op_flags, oncomplete, &c->objver); |
7c673cae FG |
1334 | objecter->op_submit(o, &c->tid); |
1335 | return 0; | |
1336 | } | |
1337 | ||
1338 | int librados::IoCtxImpl::aio_exec(const object_t& oid, AioCompletionImpl *c, | |
1339 | const char *cls, const char *method, | |
1340 | bufferlist& inbl, char *buf, size_t out_len) | |
1341 | { | |
11fdf7f2 | 1342 | FUNCTRACE(client->cct); |
7c673cae FG |
1343 | Context *oncomplete = new C_aio_Complete(c); |
1344 | ||
9f95a23c | 1345 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
1346 | ((C_aio_Complete *) oncomplete)->oid = oid; |
1347 | #endif | |
1348 | c->is_read = true; | |
1349 | c->io = this; | |
1350 | c->bl.clear(); | |
1351 | c->bl.push_back(buffer::create_static(out_len, buf)); | |
1352 | c->blp = &c->bl; | |
1353 | c->out_buf = buf; | |
1354 | ||
1355 | ::ObjectOperation rd; | |
1356 | prepare_assert_ops(&rd); | |
1357 | rd.call(cls, method, inbl); | |
1358 | Objecter::Op *o = objecter->prepare_read_op( | |
f67539c2 | 1359 | oid, oloc, rd, snap_seq, &c->bl, extra_op_flags, oncomplete, &c->objver); |
7c673cae FG |
1360 | objecter->op_submit(o, &c->tid); |
1361 | return 0; | |
1362 | } | |
1363 | ||
1364 | int librados::IoCtxImpl::read(const object_t& oid, | |
1365 | bufferlist& bl, size_t len, uint64_t off) | |
1366 | { | |
1367 | if (len > (size_t) INT_MAX) | |
1368 | return -EDOM; | |
1369 | OID_EVENT_TRACE(oid.name.c_str(), "RADOS_READ_OP_BEGIN"); | |
1370 | ||
1371 | ::ObjectOperation rd; | |
1372 | prepare_assert_ops(&rd); | |
1373 | rd.read(off, len, &bl, NULL, NULL); | |
1374 | int r = operate_read(oid, &rd, &bl); | |
1375 | if (r < 0) | |
1376 | return r; | |
1377 | ||
1378 | if (bl.length() < len) { | |
1379 | ldout(client->cct, 10) << "Returned length " << bl.length() | |
1380 | << " less than original length "<< len << dendl; | |
1381 | } | |
1382 | ||
1383 | return bl.length(); | |
1384 | } | |
1385 | ||
1386 | int librados::IoCtxImpl::cmpext(const object_t& oid, uint64_t off, | |
1387 | bufferlist& cmp_bl) | |
1388 | { | |
1389 | if (cmp_bl.length() > UINT_MAX/2) | |
1390 | return -E2BIG; | |
1391 | ||
1392 | ::ObjectOperation op; | |
1393 | prepare_assert_ops(&op); | |
1394 | op.cmpext(off, cmp_bl, NULL); | |
1395 | return operate_read(oid, &op, NULL); | |
1396 | } | |
1397 | ||
1398 | int librados::IoCtxImpl::mapext(const object_t& oid, | |
1399 | uint64_t off, size_t len, | |
1400 | std::map<uint64_t,uint64_t>& m) | |
1401 | { | |
1402 | bufferlist bl; | |
1403 | ||
9f95a23c TL |
1404 | ceph::mutex mylock = ceph::make_mutex("IoCtxImpl::read::mylock"); |
1405 | ceph::condition_variable cond; | |
7c673cae FG |
1406 | bool done; |
1407 | int r; | |
9f95a23c | 1408 | Context *onack = new C_SafeCond(mylock, cond, &done, &r); |
7c673cae FG |
1409 | |
1410 | objecter->mapext(oid, oloc, | |
f67539c2 | 1411 | off, len, snap_seq, &bl, extra_op_flags, |
7c673cae FG |
1412 | onack); |
1413 | ||
9f95a23c TL |
1414 | { |
1415 | unique_lock l{mylock}; | |
1416 | cond.wait(l, [&done] { return done;}); | |
1417 | } | |
7c673cae FG |
1418 | ldout(client->cct, 10) << "Objecter returned from read r=" << r << dendl; |
1419 | ||
1420 | if (r < 0) | |
1421 | return r; | |
1422 | ||
11fdf7f2 TL |
1423 | auto iter = bl.cbegin(); |
1424 | decode(m, iter); | |
7c673cae FG |
1425 | |
1426 | return m.size(); | |
1427 | } | |
1428 | ||
1429 | int librados::IoCtxImpl::sparse_read(const object_t& oid, | |
1430 | std::map<uint64_t,uint64_t>& m, | |
1431 | bufferlist& data_bl, size_t len, | |
1432 | uint64_t off) | |
1433 | { | |
1434 | if (len > (size_t) INT_MAX) | |
1435 | return -EDOM; | |
1436 | ||
1437 | ::ObjectOperation rd; | |
1438 | prepare_assert_ops(&rd); | |
1439 | rd.sparse_read(off, len, &m, &data_bl, NULL); | |
1440 | ||
1441 | int r = operate_read(oid, &rd, NULL); | |
1442 | if (r < 0) | |
1443 | return r; | |
1444 | ||
1445 | return m.size(); | |
1446 | } | |
1447 | ||
1448 | int librados::IoCtxImpl::checksum(const object_t& oid, uint8_t type, | |
1449 | const bufferlist &init_value, size_t len, | |
1450 | uint64_t off, size_t chunk_size, | |
1451 | bufferlist *pbl) | |
1452 | { | |
1453 | if (len > (size_t) INT_MAX) { | |
1454 | return -EDOM; | |
1455 | } | |
1456 | ||
1457 | ::ObjectOperation rd; | |
1458 | prepare_assert_ops(&rd); | |
1459 | rd.checksum(type, init_value, off, len, chunk_size, pbl, nullptr, nullptr); | |
1460 | ||
1461 | int r = operate_read(oid, &rd, nullptr); | |
1462 | if (r < 0) { | |
1463 | return r; | |
1464 | } | |
1465 | ||
1466 | return 0; | |
1467 | } | |
1468 | ||
1469 | int librados::IoCtxImpl::stat(const object_t& oid, uint64_t *psize, time_t *pmtime) | |
1470 | { | |
1471 | uint64_t size; | |
1472 | real_time mtime; | |
1473 | ||
1474 | if (!psize) | |
1475 | psize = &size; | |
1476 | ||
1477 | ::ObjectOperation rd; | |
1478 | prepare_assert_ops(&rd); | |
f67539c2 | 1479 | rd.stat(psize, &mtime, nullptr); |
7c673cae FG |
1480 | int r = operate_read(oid, &rd, NULL); |
1481 | ||
1482 | if (r >= 0 && pmtime) { | |
1483 | *pmtime = real_clock::to_time_t(mtime); | |
1484 | } | |
1485 | ||
1486 | return r; | |
1487 | } | |
1488 | ||
1489 | int librados::IoCtxImpl::stat2(const object_t& oid, uint64_t *psize, struct timespec *pts) | |
1490 | { | |
1491 | uint64_t size; | |
1492 | ceph::real_time mtime; | |
1493 | ||
1494 | if (!psize) | |
1495 | psize = &size; | |
1496 | ||
1497 | ::ObjectOperation rd; | |
1498 | prepare_assert_ops(&rd); | |
f67539c2 | 1499 | rd.stat(psize, &mtime, nullptr); |
7c673cae FG |
1500 | int r = operate_read(oid, &rd, NULL); |
1501 | if (r < 0) { | |
1502 | return r; | |
1503 | } | |
1504 | ||
1505 | if (pts) { | |
1506 | *pts = ceph::real_clock::to_timespec(mtime); | |
1507 | } | |
1508 | ||
1509 | return 0; | |
1510 | } | |
1511 | ||
1512 | int librados::IoCtxImpl::getxattr(const object_t& oid, | |
1513 | const char *name, bufferlist& bl) | |
1514 | { | |
1515 | ::ObjectOperation rd; | |
1516 | prepare_assert_ops(&rd); | |
1517 | rd.getxattr(name, &bl, NULL); | |
1518 | int r = operate_read(oid, &rd, &bl); | |
1519 | if (r < 0) | |
1520 | return r; | |
1521 | ||
1522 | return bl.length(); | |
1523 | } | |
1524 | ||
1525 | int librados::IoCtxImpl::rmxattr(const object_t& oid, const char *name) | |
1526 | { | |
1527 | ::ObjectOperation op; | |
1528 | prepare_assert_ops(&op); | |
1529 | op.rmxattr(name); | |
1530 | return operate(oid, &op, NULL); | |
1531 | } | |
1532 | ||
1533 | int librados::IoCtxImpl::setxattr(const object_t& oid, | |
1534 | const char *name, bufferlist& bl) | |
1535 | { | |
1536 | ::ObjectOperation op; | |
1537 | prepare_assert_ops(&op); | |
1538 | op.setxattr(name, bl); | |
1539 | return operate(oid, &op, NULL); | |
1540 | } | |
1541 | ||
1542 | int librados::IoCtxImpl::getxattrs(const object_t& oid, | |
1543 | map<std::string, bufferlist>& attrset) | |
1544 | { | |
1545 | map<string, bufferlist> aset; | |
1546 | ||
1547 | ::ObjectOperation rd; | |
1548 | prepare_assert_ops(&rd); | |
1549 | rd.getxattrs(&aset, NULL); | |
1550 | int r = operate_read(oid, &rd, NULL); | |
1551 | ||
1552 | attrset.clear(); | |
1553 | if (r >= 0) { | |
1554 | for (map<string,bufferlist>::iterator p = aset.begin(); p != aset.end(); ++p) { | |
1555 | ldout(client->cct, 10) << "IoCtxImpl::getxattrs: xattr=" << p->first << dendl; | |
1556 | attrset[p->first.c_str()] = p->second; | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | return r; | |
1561 | } | |
1562 | ||
1563 | void librados::IoCtxImpl::set_sync_op_version(version_t ver) | |
1564 | { | |
1565 | ANNOTATE_BENIGN_RACE_SIZED(&last_objver, sizeof(last_objver), | |
1566 | "IoCtxImpl last_objver"); | |
1567 | last_objver = ver; | |
1568 | } | |
1569 | ||
f67539c2 TL |
1570 | namespace librados { |
1571 | void intrusive_ptr_add_ref(IoCtxImpl *p) { p->get(); } | |
1572 | void intrusive_ptr_release(IoCtxImpl *p) { p->put(); } | |
1573 | } | |
1574 | ||
1575 | struct WatchInfo { | |
1576 | boost::intrusive_ptr<librados::IoCtxImpl> ioctx; | |
7c673cae FG |
1577 | object_t oid; |
1578 | librados::WatchCtx *ctx; | |
1579 | librados::WatchCtx2 *ctx2; | |
7c673cae FG |
1580 | |
1581 | WatchInfo(librados::IoCtxImpl *io, object_t o, | |
f67539c2 TL |
1582 | librados::WatchCtx *c, librados::WatchCtx2 *c2) |
1583 | : ioctx(io), oid(o), ctx(c), ctx2(c2) {} | |
7c673cae FG |
1584 | |
1585 | void handle_notify(uint64_t notify_id, | |
1586 | uint64_t cookie, | |
1587 | uint64_t notifier_id, | |
f67539c2 | 1588 | bufferlist& bl) { |
7c673cae FG |
1589 | ldout(ioctx->client->cct, 10) << __func__ << " " << notify_id |
1590 | << " cookie " << cookie | |
1591 | << " notifier_id " << notifier_id | |
1592 | << " len " << bl.length() | |
1593 | << dendl; | |
1594 | ||
1595 | if (ctx2) | |
1596 | ctx2->handle_notify(notify_id, cookie, notifier_id, bl); | |
1597 | if (ctx) { | |
1598 | ctx->notify(0, 0, bl); | |
1599 | ||
1600 | // send ACK back to OSD if using legacy protocol | |
1601 | bufferlist empty; | |
1602 | ioctx->notify_ack(oid, notify_id, cookie, empty); | |
1603 | } | |
1604 | } | |
f67539c2 | 1605 | void handle_error(uint64_t cookie, int err) { |
7c673cae FG |
1606 | ldout(ioctx->client->cct, 10) << __func__ << " cookie " << cookie |
1607 | << " err " << err | |
1608 | << dendl; | |
1609 | if (ctx2) | |
1610 | ctx2->handle_error(cookie, err); | |
1611 | } | |
f67539c2 TL |
1612 | |
1613 | void operator()(bs::error_code ec, | |
1614 | uint64_t notify_id, | |
1615 | uint64_t cookie, | |
1616 | uint64_t notifier_id, | |
1617 | bufferlist&& bl) { | |
1618 | if (ec) { | |
1619 | handle_error(cookie, ceph::from_error_code(ec)); | |
1620 | } else { | |
1621 | handle_notify(notify_id, cookie, notifier_id, bl); | |
1622 | } | |
1623 | } | |
1624 | }; | |
1625 | ||
1626 | // internal WatchInfo that owns the context memory | |
1627 | struct InternalWatchInfo : public WatchInfo { | |
1628 | std::unique_ptr<librados::WatchCtx> ctx; | |
1629 | std::unique_ptr<librados::WatchCtx2> ctx2; | |
1630 | ||
1631 | InternalWatchInfo(librados::IoCtxImpl *io, object_t o, | |
1632 | librados::WatchCtx *c, librados::WatchCtx2 *c2) | |
1633 | : WatchInfo(io, o, c, c2), ctx(c), ctx2(c2) {} | |
7c673cae FG |
1634 | }; |
1635 | ||
1636 | int librados::IoCtxImpl::watch(const object_t& oid, uint64_t *handle, | |
1637 | librados::WatchCtx *ctx, | |
1638 | librados::WatchCtx2 *ctx2, | |
1639 | bool internal) | |
1640 | { | |
1641 | return watch(oid, handle, ctx, ctx2, 0, internal); | |
1642 | } | |
1643 | ||
1644 | int librados::IoCtxImpl::watch(const object_t& oid, uint64_t *handle, | |
1645 | librados::WatchCtx *ctx, | |
1646 | librados::WatchCtx2 *ctx2, | |
1647 | uint32_t timeout, | |
1648 | bool internal) | |
1649 | { | |
1650 | ::ObjectOperation wr; | |
1651 | version_t objver; | |
1652 | C_SaferCond onfinish; | |
1653 | ||
20effc67 TL |
1654 | Objecter::LingerOp *linger_op = objecter->linger_register(oid, oloc, |
1655 | extra_op_flags); | |
7c673cae | 1656 | *handle = linger_op->get_cookie(); |
f67539c2 TL |
1657 | if (internal) { |
1658 | linger_op->handle = InternalWatchInfo(this, oid, ctx, ctx2); | |
1659 | } else { | |
1660 | linger_op->handle = WatchInfo(this, oid, ctx, ctx2); | |
1661 | } | |
7c673cae FG |
1662 | prepare_assert_ops(&wr); |
1663 | wr.watch(*handle, CEPH_OSD_WATCH_OP_WATCH, timeout); | |
1664 | bufferlist bl; | |
1665 | objecter->linger_watch(linger_op, wr, | |
1666 | snapc, ceph::real_clock::now(), bl, | |
1667 | &onfinish, | |
1668 | &objver); | |
1669 | ||
1670 | int r = onfinish.wait(); | |
1671 | ||
1672 | set_sync_op_version(objver); | |
1673 | ||
1674 | if (r < 0) { | |
1675 | objecter->linger_cancel(linger_op); | |
1676 | *handle = 0; | |
1677 | } | |
1678 | ||
1679 | return r; | |
1680 | } | |
1681 | ||
1682 | int librados::IoCtxImpl::aio_watch(const object_t& oid, | |
1683 | AioCompletionImpl *c, | |
1684 | uint64_t *handle, | |
1685 | librados::WatchCtx *ctx, | |
1686 | librados::WatchCtx2 *ctx2, | |
1687 | bool internal) { | |
1688 | return aio_watch(oid, c, handle, ctx, ctx2, 0, internal); | |
1689 | } | |
1690 | ||
1691 | int librados::IoCtxImpl::aio_watch(const object_t& oid, | |
1692 | AioCompletionImpl *c, | |
1693 | uint64_t *handle, | |
1694 | librados::WatchCtx *ctx, | |
1695 | librados::WatchCtx2 *ctx2, | |
1696 | uint32_t timeout, | |
1697 | bool internal) | |
1698 | { | |
20effc67 TL |
1699 | Objecter::LingerOp *linger_op = objecter->linger_register(oid, oloc, |
1700 | extra_op_flags); | |
7c673cae FG |
1701 | c->io = this; |
1702 | Context *oncomplete = new C_aio_linger_Complete(c, linger_op, false); | |
1703 | ||
1704 | ::ObjectOperation wr; | |
1705 | *handle = linger_op->get_cookie(); | |
f67539c2 TL |
1706 | if (internal) { |
1707 | linger_op->handle = InternalWatchInfo(this, oid, ctx, ctx2); | |
1708 | } else { | |
1709 | linger_op->handle = WatchInfo(this, oid, ctx, ctx2); | |
1710 | } | |
7c673cae FG |
1711 | |
1712 | prepare_assert_ops(&wr); | |
1713 | wr.watch(*handle, CEPH_OSD_WATCH_OP_WATCH, timeout); | |
1714 | bufferlist bl; | |
1715 | objecter->linger_watch(linger_op, wr, | |
1716 | snapc, ceph::real_clock::now(), bl, | |
1717 | oncomplete, &c->objver); | |
1718 | ||
1719 | return 0; | |
1720 | } | |
1721 | ||
1722 | ||
1723 | int librados::IoCtxImpl::notify_ack( | |
1724 | const object_t& oid, | |
1725 | uint64_t notify_id, | |
1726 | uint64_t cookie, | |
1727 | bufferlist& bl) | |
1728 | { | |
1729 | ::ObjectOperation rd; | |
1730 | prepare_assert_ops(&rd); | |
1731 | rd.notify_ack(notify_id, cookie, bl); | |
f67539c2 | 1732 | objecter->read(oid, oloc, rd, snap_seq, (bufferlist*)NULL, extra_op_flags, 0, 0); |
7c673cae FG |
1733 | return 0; |
1734 | } | |
1735 | ||
1736 | int librados::IoCtxImpl::watch_check(uint64_t cookie) | |
1737 | { | |
f67539c2 TL |
1738 | auto linger_op = reinterpret_cast<Objecter::LingerOp*>(cookie); |
1739 | auto r = objecter->linger_check(linger_op); | |
1740 | if (r) | |
1741 | return 1 + std::chrono::duration_cast< | |
1742 | std::chrono::milliseconds>(*r).count(); | |
1743 | else | |
1744 | return ceph::from_error_code(r.error()); | |
7c673cae FG |
1745 | } |
1746 | ||
1747 | int librados::IoCtxImpl::unwatch(uint64_t cookie) | |
1748 | { | |
1749 | Objecter::LingerOp *linger_op = reinterpret_cast<Objecter::LingerOp*>(cookie); | |
1750 | C_SaferCond onfinish; | |
1751 | version_t ver = 0; | |
1752 | ||
1753 | ::ObjectOperation wr; | |
1754 | prepare_assert_ops(&wr); | |
1755 | wr.watch(cookie, CEPH_OSD_WATCH_OP_UNWATCH); | |
1756 | objecter->mutate(linger_op->target.base_oid, oloc, wr, | |
f67539c2 | 1757 | snapc, ceph::real_clock::now(), extra_op_flags, |
7c673cae FG |
1758 | &onfinish, &ver); |
1759 | objecter->linger_cancel(linger_op); | |
1760 | ||
1761 | int r = onfinish.wait(); | |
1762 | set_sync_op_version(ver); | |
1763 | return r; | |
1764 | } | |
1765 | ||
1766 | int librados::IoCtxImpl::aio_unwatch(uint64_t cookie, AioCompletionImpl *c) | |
1767 | { | |
1768 | c->io = this; | |
1769 | Objecter::LingerOp *linger_op = reinterpret_cast<Objecter::LingerOp*>(cookie); | |
1770 | Context *oncomplete = new C_aio_linger_Complete(c, linger_op, true); | |
1771 | ||
1772 | ::ObjectOperation wr; | |
1773 | prepare_assert_ops(&wr); | |
1774 | wr.watch(cookie, CEPH_OSD_WATCH_OP_UNWATCH); | |
1775 | objecter->mutate(linger_op->target.base_oid, oloc, wr, | |
f67539c2 | 1776 | snapc, ceph::real_clock::now(), extra_op_flags, |
7c673cae FG |
1777 | oncomplete, &c->objver); |
1778 | return 0; | |
1779 | } | |
1780 | ||
1781 | int librados::IoCtxImpl::notify(const object_t& oid, bufferlist& bl, | |
1782 | uint64_t timeout_ms, | |
1783 | bufferlist *preply_bl, | |
1784 | char **preply_buf, size_t *preply_buf_len) | |
1785 | { | |
20effc67 TL |
1786 | Objecter::LingerOp *linger_op = objecter->linger_register(oid, oloc, |
1787 | extra_op_flags); | |
7c673cae FG |
1788 | |
1789 | C_SaferCond notify_finish_cond; | |
f67539c2 TL |
1790 | linger_op->on_notify_finish = |
1791 | Objecter::LingerOp::OpComp::create( | |
1792 | objecter->service.get_executor(), | |
1793 | CB_notify_Finish(client->cct, ¬ify_finish_cond, | |
1794 | objecter, linger_op, preply_bl, | |
1795 | preply_buf, preply_buf_len)); | |
7c673cae FG |
1796 | uint32_t timeout = notify_timeout; |
1797 | if (timeout_ms) | |
1798 | timeout = timeout_ms / 1000; | |
1799 | ||
1800 | // Construct RADOS op | |
1801 | ::ObjectOperation rd; | |
1802 | prepare_assert_ops(&rd); | |
1803 | bufferlist inbl; | |
1804 | rd.notify(linger_op->get_cookie(), 1, timeout, bl, &inbl); | |
1805 | ||
1806 | // Issue RADOS op | |
1807 | C_SaferCond onack; | |
1808 | version_t objver; | |
1809 | objecter->linger_notify(linger_op, | |
1810 | rd, snap_seq, inbl, NULL, | |
1811 | &onack, &objver); | |
1812 | ||
1813 | ldout(client->cct, 10) << __func__ << " issued linger op " << linger_op << dendl; | |
1814 | int r = onack.wait(); | |
1815 | ldout(client->cct, 10) << __func__ << " linger op " << linger_op | |
1816 | << " acked (" << r << ")" << dendl; | |
1817 | ||
1818 | if (r == 0) { | |
1819 | ldout(client->cct, 10) << __func__ << " waiting for watch_notify finish " | |
1820 | << linger_op << dendl; | |
1821 | r = notify_finish_cond.wait(); | |
1822 | ||
1823 | } else { | |
1824 | ldout(client->cct, 10) << __func__ << " failed to initiate notify, r = " | |
1825 | << r << dendl; | |
28e407b8 | 1826 | notify_finish_cond.wait(); |
7c673cae FG |
1827 | } |
1828 | ||
1829 | objecter->linger_cancel(linger_op); | |
1830 | ||
1831 | set_sync_op_version(objver); | |
1832 | return r; | |
1833 | } | |
1834 | ||
1835 | int librados::IoCtxImpl::aio_notify(const object_t& oid, AioCompletionImpl *c, | |
1836 | bufferlist& bl, uint64_t timeout_ms, | |
1837 | bufferlist *preply_bl, char **preply_buf, | |
1838 | size_t *preply_buf_len) | |
1839 | { | |
20effc67 TL |
1840 | Objecter::LingerOp *linger_op = objecter->linger_register(oid, oloc, |
1841 | extra_op_flags); | |
7c673cae FG |
1842 | |
1843 | c->io = this; | |
1844 | ||
1845 | C_aio_notify_Complete *oncomplete = new C_aio_notify_Complete(c, linger_op); | |
f67539c2 TL |
1846 | linger_op->on_notify_finish = |
1847 | Objecter::LingerOp::OpComp::create( | |
1848 | objecter->service.get_executor(), | |
1849 | CB_notify_Finish(client->cct, oncomplete, | |
1850 | objecter, linger_op, | |
1851 | preply_bl, preply_buf, | |
1852 | preply_buf_len)); | |
1853 | Context *onack = new C_aio_notify_Ack(client->cct, oncomplete); | |
7c673cae FG |
1854 | |
1855 | uint32_t timeout = notify_timeout; | |
1856 | if (timeout_ms) | |
1857 | timeout = timeout_ms / 1000; | |
1858 | ||
1859 | // Construct RADOS op | |
1860 | ::ObjectOperation rd; | |
1861 | prepare_assert_ops(&rd); | |
1862 | bufferlist inbl; | |
1863 | rd.notify(linger_op->get_cookie(), 1, timeout, bl, &inbl); | |
1864 | ||
1865 | // Issue RADOS op | |
1866 | objecter->linger_notify(linger_op, | |
1867 | rd, snap_seq, inbl, NULL, | |
1868 | onack, &c->objver); | |
1869 | return 0; | |
1870 | } | |
1871 | ||
1872 | int librados::IoCtxImpl::set_alloc_hint(const object_t& oid, | |
1873 | uint64_t expected_object_size, | |
1874 | uint64_t expected_write_size, | |
1875 | uint32_t flags) | |
1876 | { | |
1877 | ::ObjectOperation wr; | |
1878 | prepare_assert_ops(&wr); | |
1879 | wr.set_alloc_hint(expected_object_size, expected_write_size, flags); | |
1880 | return operate(oid, &wr, NULL); | |
1881 | } | |
1882 | ||
1883 | version_t librados::IoCtxImpl::last_version() | |
1884 | { | |
1885 | return last_objver; | |
1886 | } | |
1887 | ||
1888 | void librados::IoCtxImpl::set_assert_version(uint64_t ver) | |
1889 | { | |
1890 | assert_ver = ver; | |
1891 | } | |
1892 | ||
1893 | void librados::IoCtxImpl::set_notify_timeout(uint32_t timeout) | |
1894 | { | |
1895 | notify_timeout = timeout; | |
1896 | } | |
1897 | ||
1898 | int librados::IoCtxImpl::cache_pin(const object_t& oid) | |
1899 | { | |
1900 | ::ObjectOperation wr; | |
1901 | prepare_assert_ops(&wr); | |
1902 | wr.cache_pin(); | |
1903 | return operate(oid, &wr, NULL); | |
1904 | } | |
1905 | ||
1906 | int librados::IoCtxImpl::cache_unpin(const object_t& oid) | |
1907 | { | |
1908 | ::ObjectOperation wr; | |
1909 | prepare_assert_ops(&wr); | |
1910 | wr.cache_unpin(); | |
1911 | return operate(oid, &wr, NULL); | |
1912 | } | |
1913 | ||
1914 | ||
1915 | ///////////////////////////// C_aio_stat_Ack //////////////////////////// | |
1916 | ||
1917 | librados::IoCtxImpl::C_aio_stat_Ack::C_aio_stat_Ack(AioCompletionImpl *_c, | |
1918 | time_t *pm) | |
1919 | : c(_c), pmtime(pm) | |
1920 | { | |
11fdf7f2 | 1921 | ceph_assert(!c->io); |
7c673cae FG |
1922 | c->get(); |
1923 | } | |
1924 | ||
1925 | void librados::IoCtxImpl::C_aio_stat_Ack::finish(int r) | |
1926 | { | |
9f95a23c | 1927 | c->lock.lock(); |
7c673cae FG |
1928 | c->rval = r; |
1929 | c->complete = true; | |
9f95a23c | 1930 | c->cond.notify_all(); |
7c673cae FG |
1931 | |
1932 | if (r >= 0 && pmtime) { | |
1933 | *pmtime = real_clock::to_time_t(mtime); | |
1934 | } | |
1935 | ||
1936 | if (c->callback_complete) { | |
f67539c2 | 1937 | boost::asio::defer(c->io->client->finish_strand, CB_AioComplete(c)); |
7c673cae FG |
1938 | } |
1939 | ||
1940 | c->put_unlock(); | |
1941 | } | |
1942 | ||
1943 | ///////////////////////////// C_aio_stat2_Ack //////////////////////////// | |
1944 | ||
1945 | librados::IoCtxImpl::C_aio_stat2_Ack::C_aio_stat2_Ack(AioCompletionImpl *_c, | |
1946 | struct timespec *pt) | |
1947 | : c(_c), pts(pt) | |
1948 | { | |
11fdf7f2 | 1949 | ceph_assert(!c->io); |
7c673cae FG |
1950 | c->get(); |
1951 | } | |
1952 | ||
1953 | void librados::IoCtxImpl::C_aio_stat2_Ack::finish(int r) | |
1954 | { | |
9f95a23c | 1955 | c->lock.lock(); |
7c673cae FG |
1956 | c->rval = r; |
1957 | c->complete = true; | |
9f95a23c | 1958 | c->cond.notify_all(); |
7c673cae FG |
1959 | |
1960 | if (r >= 0 && pts) { | |
1961 | *pts = real_clock::to_timespec(mtime); | |
1962 | } | |
1963 | ||
1964 | if (c->callback_complete) { | |
f67539c2 | 1965 | boost::asio::defer(c->io->client->finish_strand, CB_AioComplete(c)); |
7c673cae FG |
1966 | } |
1967 | ||
1968 | c->put_unlock(); | |
1969 | } | |
1970 | ||
1971 | //////////////////////////// C_aio_Complete //////////////////////////////// | |
1972 | ||
1973 | librados::IoCtxImpl::C_aio_Complete::C_aio_Complete(AioCompletionImpl *_c) | |
1974 | : c(_c) | |
1975 | { | |
1976 | c->get(); | |
1977 | } | |
1978 | ||
1979 | void librados::IoCtxImpl::C_aio_Complete::finish(int r) | |
1980 | { | |
9f95a23c | 1981 | c->lock.lock(); |
11fdf7f2 TL |
1982 | // Leave an existing rval unless r != 0 |
1983 | if (r) | |
1984 | c->rval = r; // This clears the error set in C_ObjectOperation_scrub_ls::finish() | |
7c673cae | 1985 | c->complete = true; |
9f95a23c | 1986 | c->cond.notify_all(); |
7c673cae FG |
1987 | |
1988 | if (r == 0 && c->blp && c->blp->length() > 0) { | |
1adf2230 AA |
1989 | if (c->out_buf && !c->blp->is_contiguous()) { |
1990 | c->rval = -ERANGE; | |
1991 | } else { | |
11fdf7f2 | 1992 | if (c->out_buf && !c->blp->is_provided_buffer(c->out_buf)) |
9f95a23c | 1993 | c->blp->begin().copy(c->blp->length(), c->out_buf); |
11fdf7f2 | 1994 | |
1adf2230 AA |
1995 | c->rval = c->blp->length(); |
1996 | } | |
7c673cae FG |
1997 | } |
1998 | ||
1999 | if (c->callback_complete || | |
2000 | c->callback_safe) { | |
f67539c2 | 2001 | boost::asio::defer(c->io->client->finish_strand, CB_AioComplete(c)); |
7c673cae FG |
2002 | } |
2003 | ||
2004 | if (c->aio_write_seq) { | |
2005 | c->io->complete_aio_write(c); | |
2006 | } | |
2007 | ||
9f95a23c | 2008 | #if defined(WITH_EVENTTRACE) |
7c673cae FG |
2009 | OID_EVENT_TRACE(oid.name.c_str(), "RADOS_OP_COMPLETE"); |
2010 | #endif | |
2011 | c->put_unlock(); | |
2012 | } | |
2013 | ||
2014 | void librados::IoCtxImpl::object_list_slice( | |
2015 | const hobject_t start, | |
2016 | const hobject_t finish, | |
2017 | const size_t n, | |
2018 | const size_t m, | |
2019 | hobject_t *split_start, | |
2020 | hobject_t *split_finish) | |
2021 | { | |
2022 | if (start.is_max()) { | |
2023 | *split_start = hobject_t::get_max(); | |
2024 | *split_finish = hobject_t::get_max(); | |
2025 | return; | |
2026 | } | |
2027 | ||
2028 | uint64_t start_hash = hobject_t::_reverse_bits(start.get_hash()); | |
2029 | uint64_t finish_hash = | |
2030 | finish.is_max() ? 0x100000000 : | |
2031 | hobject_t::_reverse_bits(finish.get_hash()); | |
2032 | ||
2033 | uint64_t diff = finish_hash - start_hash; | |
2034 | uint64_t rev_start = start_hash + (diff * n / m); | |
2035 | uint64_t rev_finish = start_hash + (diff * (n + 1) / m); | |
2036 | if (n == 0) { | |
2037 | *split_start = start; | |
2038 | } else { | |
2039 | *split_start = hobject_t( | |
2040 | object_t(), string(), CEPH_NOSNAP, | |
2041 | hobject_t::_reverse_bits(rev_start), poolid, string()); | |
2042 | } | |
2043 | ||
2044 | if (n == m - 1) | |
2045 | *split_finish = finish; | |
2046 | else if (rev_finish >= 0x100000000) | |
2047 | *split_finish = hobject_t::get_max(); | |
2048 | else | |
2049 | *split_finish = hobject_t( | |
2050 | object_t(), string(), CEPH_NOSNAP, | |
2051 | hobject_t::_reverse_bits(rev_finish), poolid, string()); | |
2052 | } | |
2053 | ||
c07f9fc5 FG |
2054 | int librados::IoCtxImpl::application_enable(const std::string& app_name, |
2055 | bool force) | |
2056 | { | |
2057 | auto c = new PoolAsyncCompletionImpl(); | |
2058 | application_enable_async(app_name, force, c); | |
2059 | ||
2060 | int r = c->wait(); | |
11fdf7f2 | 2061 | ceph_assert(r == 0); |
c07f9fc5 FG |
2062 | |
2063 | r = c->get_return_value(); | |
2064 | c->release(); | |
f67539c2 | 2065 | c->put(); |
c07f9fc5 FG |
2066 | if (r < 0) { |
2067 | return r; | |
2068 | } | |
2069 | ||
2070 | return client->wait_for_latest_osdmap(); | |
2071 | } | |
2072 | ||
2073 | void librados::IoCtxImpl::application_enable_async(const std::string& app_name, | |
2074 | bool force, | |
2075 | PoolAsyncCompletionImpl *c) | |
2076 | { | |
2077 | // pre-Luminous clusters will return -EINVAL and application won't be | |
2078 | // preserved until Luminous is configured as minimim version. | |
2079 | if (!client->get_required_monitor_features().contains_all( | |
2080 | ceph::features::mon::FEATURE_LUMINOUS)) { | |
f67539c2 TL |
2081 | boost::asio::defer(client->finish_strand, |
2082 | [cb = CB_PoolAsync_Safe(c)]() mutable { | |
2083 | cb(-EOPNOTSUPP); | |
2084 | }); | |
c07f9fc5 FG |
2085 | return; |
2086 | } | |
2087 | ||
2088 | std::stringstream cmd; | |
2089 | cmd << "{" | |
2090 | << "\"prefix\": \"osd pool application enable\"," | |
2091 | << "\"pool\": \"" << get_cached_pool_name() << "\"," | |
2092 | << "\"app\": \"" << app_name << "\""; | |
2093 | if (force) { | |
11fdf7f2 | 2094 | cmd << ",\"yes_i_really_mean_it\": true"; |
c07f9fc5 FG |
2095 | } |
2096 | cmd << "}"; | |
2097 | ||
2098 | std::vector<std::string> cmds; | |
2099 | cmds.push_back(cmd.str()); | |
2100 | bufferlist inbl; | |
2101 | client->mon_command_async(cmds, inbl, nullptr, nullptr, | |
f67539c2 | 2102 | make_lambda_context(CB_PoolAsync_Safe(c))); |
c07f9fc5 FG |
2103 | } |
2104 | ||
2105 | int librados::IoCtxImpl::application_list(std::set<std::string> *app_names) | |
2106 | { | |
2107 | int r = 0; | |
2108 | app_names->clear(); | |
2109 | objecter->with_osdmap([&](const OSDMap& o) { | |
2110 | auto pg_pool = o.get_pg_pool(poolid); | |
2111 | if (pg_pool == nullptr) { | |
2112 | r = -ENOENT; | |
2113 | return; | |
2114 | } | |
2115 | ||
2116 | for (auto &pair : pg_pool->application_metadata) { | |
2117 | app_names->insert(pair.first); | |
2118 | } | |
2119 | }); | |
2120 | return r; | |
2121 | } | |
2122 | ||
2123 | int librados::IoCtxImpl::application_metadata_get(const std::string& app_name, | |
2124 | const std::string &key, | |
2125 | std::string* value) | |
2126 | { | |
2127 | int r = 0; | |
2128 | objecter->with_osdmap([&](const OSDMap& o) { | |
2129 | auto pg_pool = o.get_pg_pool(poolid); | |
2130 | if (pg_pool == nullptr) { | |
2131 | r = -ENOENT; | |
2132 | return; | |
2133 | } | |
2134 | ||
2135 | auto app_it = pg_pool->application_metadata.find(app_name); | |
2136 | if (app_it == pg_pool->application_metadata.end()) { | |
2137 | r = -ENOENT; | |
2138 | return; | |
2139 | } | |
2140 | ||
2141 | auto it = app_it->second.find(key); | |
2142 | if (it == app_it->second.end()) { | |
2143 | r = -ENOENT; | |
2144 | return; | |
2145 | } | |
2146 | ||
2147 | *value = it->second; | |
2148 | }); | |
2149 | return r; | |
2150 | } | |
2151 | ||
2152 | int librados::IoCtxImpl::application_metadata_set(const std::string& app_name, | |
2153 | const std::string &key, | |
2154 | const std::string& value) | |
2155 | { | |
2156 | std::stringstream cmd; | |
2157 | cmd << "{" | |
2158 | << "\"prefix\":\"osd pool application set\"," | |
2159 | << "\"pool\":\"" << get_cached_pool_name() << "\"," | |
2160 | << "\"app\":\"" << app_name << "\"," | |
2161 | << "\"key\":\"" << key << "\"," | |
2162 | << "\"value\":\"" << value << "\"" | |
2163 | << "}"; | |
2164 | ||
2165 | std::vector<std::string> cmds; | |
2166 | cmds.push_back(cmd.str()); | |
2167 | bufferlist inbl; | |
2168 | int r = client->mon_command(cmds, inbl, nullptr, nullptr); | |
2169 | if (r < 0) { | |
2170 | return r; | |
2171 | } | |
2172 | ||
2173 | // ensure we have the latest osd map epoch before proceeding | |
2174 | return client->wait_for_latest_osdmap(); | |
2175 | } | |
2176 | ||
2177 | int librados::IoCtxImpl::application_metadata_remove(const std::string& app_name, | |
2178 | const std::string &key) | |
2179 | { | |
2180 | std::stringstream cmd; | |
2181 | cmd << "{" | |
2182 | << "\"prefix\":\"osd pool application rm\"," | |
2183 | << "\"pool\":\"" << get_cached_pool_name() << "\"," | |
2184 | << "\"app\":\"" << app_name << "\"," | |
2185 | << "\"key\":\"" << key << "\"" | |
2186 | << "}"; | |
2187 | ||
2188 | std::vector<std::string> cmds; | |
2189 | cmds.push_back(cmd.str()); | |
2190 | bufferlist inbl; | |
2191 | int r = client->mon_command(cmds, inbl, nullptr, nullptr); | |
2192 | if (r < 0) { | |
2193 | return r; | |
2194 | } | |
2195 | ||
2196 | // ensure we have the latest osd map epoch before proceeding | |
2197 | return client->wait_for_latest_osdmap(); | |
2198 | } | |
2199 | ||
2200 | int librados::IoCtxImpl::application_metadata_list(const std::string& app_name, | |
2201 | std::map<std::string, std::string> *values) | |
2202 | { | |
2203 | int r = 0; | |
2204 | values->clear(); | |
2205 | objecter->with_osdmap([&](const OSDMap& o) { | |
2206 | auto pg_pool = o.get_pg_pool(poolid); | |
2207 | if (pg_pool == nullptr) { | |
2208 | r = -ENOENT; | |
2209 | return; | |
2210 | } | |
2211 | ||
2212 | auto it = pg_pool->application_metadata.find(app_name); | |
2213 | if (it == pg_pool->application_metadata.end()) { | |
2214 | r = -ENOENT; | |
2215 | return; | |
2216 | } | |
2217 | ||
2218 | *values = it->second; | |
2219 | }); | |
2220 | return r; | |
2221 | } | |
2222 |