]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/librados_test_stub/LibradosTestStub.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / librados_test_stub / LibradosTestStub.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 "test/librados_test_stub/LibradosTestStub.h"
5 #include "include/rados/librados.hpp"
6 #include "include/stringify.h"
7 #include "common/ceph_argparse.h"
8 #include "common/ceph_context.h"
9 #include "common/common_init.h"
10 #include "common/config.h"
11 #include "common/debug.h"
12 #include "common/snap_types.h"
13 #include "librados/AioCompletionImpl.h"
14 #include "log/Log.h"
15 #include "test/librados_test_stub/TestClassHandler.h"
16 #include "test/librados_test_stub/TestIoCtxImpl.h"
17 #include "test/librados_test_stub/TestRadosClient.h"
18 #include "test/librados_test_stub/TestMemCluster.h"
19 #include "test/librados_test_stub/TestMemRadosClient.h"
20 #include "objclass/objclass.h"
21 #include "osd/osd_types.h"
22 #include <arpa/inet.h>
23 #include <boost/shared_ptr.hpp>
24 #include <deque>
25 #include <functional>
26 #include <list>
27 #include <vector>
28 #include "include/ceph_assert.h"
29 #include "include/compat.h"
30
31 #define dout_context g_ceph_context
32 #define dout_subsys ceph_subsys_rados
33
34 using namespace std;
35
36 namespace librados {
37
38 MockTestMemIoCtxImpl &get_mock_io_ctx(IoCtx &ioctx) {
39 MockTestMemIoCtxImpl **mock =
40 reinterpret_cast<MockTestMemIoCtxImpl **>(&ioctx);
41 return **mock;
42 }
43
44 } // namespace librados
45
46 namespace librados_test_stub {
47
48 TestClusterRef &cluster() {
49 static TestClusterRef s_cluster;
50 return s_cluster;
51 }
52
53 void set_cluster(TestClusterRef cluster_ref) {
54 cluster() = cluster_ref;
55 }
56
57 TestClusterRef get_cluster() {
58 auto &cluster_ref = cluster();
59 if (cluster_ref.get() == nullptr) {
60 cluster_ref.reset(new librados::TestMemCluster());
61 }
62 return cluster_ref;
63 }
64
65 librados::TestClassHandler *get_class_handler() {
66 static boost::shared_ptr<librados::TestClassHandler> s_class_handler;
67 if (!s_class_handler) {
68 s_class_handler.reset(new librados::TestClassHandler());
69 s_class_handler->open_all_classes();
70 }
71 return s_class_handler.get();
72 }
73
74 } // namespace librados_test_stub
75
76 namespace {
77
78 void do_out_buffer(bufferlist& outbl, char **outbuf, size_t *outbuflen) {
79 if (outbuf) {
80 if (outbl.length() > 0) {
81 *outbuf = (char *)malloc(outbl.length());
82 memcpy(*outbuf, outbl.c_str(), outbl.length());
83 } else {
84 *outbuf = NULL;
85 }
86 }
87 if (outbuflen) {
88 *outbuflen = outbl.length();
89 }
90 }
91
92 void do_out_buffer(string& outbl, char **outbuf, size_t *outbuflen) {
93 if (outbuf) {
94 if (outbl.length() > 0) {
95 *outbuf = (char *)malloc(outbl.length());
96 memcpy(*outbuf, outbl.c_str(), outbl.length());
97 } else {
98 *outbuf = NULL;
99 }
100 }
101 if (outbuflen) {
102 *outbuflen = outbl.length();
103 }
104 }
105
106 librados::TestRadosClient *create_rados_client() {
107 CephInitParameters iparams(CEPH_ENTITY_TYPE_CLIENT);
108 CephContext *cct = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0);
109 cct->_conf.parse_env(cct->get_module_type());
110 cct->_conf.apply_changes(nullptr);
111 cct->_log->start();
112
113 auto rados_client =
114 librados_test_stub::get_cluster()->create_rados_client(cct);
115 cct->put();
116 return rados_client;
117 }
118
119 } // anonymous namespace
120
121 extern "C" int rados_aio_create_completion2(void *cb_arg,
122 rados_callback_t cb_complete,
123 rados_completion_t *pc)
124 {
125 librados::AioCompletionImpl *c = new librados::AioCompletionImpl;
126 if (cb_complete) {
127 c->set_complete_callback(cb_arg, cb_complete);
128 }
129 *pc = c;
130 return 0;
131 }
132
133 extern "C" int rados_aio_get_return_value(rados_completion_t c) {
134 return reinterpret_cast<librados::AioCompletionImpl*>(c)->get_return_value();
135 }
136
137 extern "C" rados_config_t rados_cct(rados_t cluster)
138 {
139 librados::TestRadosClient *client =
140 reinterpret_cast<librados::TestRadosClient*>(cluster);
141 return reinterpret_cast<rados_config_t>(client->cct());
142 }
143
144 extern "C" int rados_conf_set(rados_t cluster, const char *option,
145 const char *value) {
146 librados::TestRadosClient *impl =
147 reinterpret_cast<librados::TestRadosClient*>(cluster);
148 CephContext *cct = impl->cct();
149 return cct->_conf.set_val(option, value);
150 }
151
152 extern "C" int rados_conf_parse_env(rados_t cluster, const char *var) {
153 librados::TestRadosClient *client =
154 reinterpret_cast<librados::TestRadosClient*>(cluster);
155 auto& conf = client->cct()->_conf;
156 conf.parse_env(client->cct()->get_module_type(), var);
157 conf.apply_changes(NULL);
158 return 0;
159 }
160
161 extern "C" int rados_conf_read_file(rados_t cluster, const char *path) {
162 librados::TestRadosClient *client =
163 reinterpret_cast<librados::TestRadosClient*>(cluster);
164 auto& conf = client->cct()->_conf;
165 int ret = conf.parse_config_files(path, NULL, 0);
166 if (ret == 0) {
167 conf.parse_env(client->cct()->get_module_type());
168 conf.apply_changes(NULL);
169 conf.complain_about_parse_error(client->cct());
170 } else if (ret == -ENOENT) {
171 // ignore missing client config
172 return 0;
173 }
174 return ret;
175 }
176
177 extern "C" int rados_connect(rados_t cluster) {
178 librados::TestRadosClient *client =
179 reinterpret_cast<librados::TestRadosClient*>(cluster);
180 return client->connect();
181 }
182
183 extern "C" int rados_create(rados_t *cluster, const char * const id) {
184 *cluster = create_rados_client();
185 return 0;
186 }
187
188 extern "C" int rados_create_with_context(rados_t *cluster,
189 rados_config_t cct_) {
190 auto cct = reinterpret_cast<CephContext*>(cct_);
191 *cluster = librados_test_stub::get_cluster()->create_rados_client(cct);
192 return 0;
193 }
194
195 extern "C" rados_config_t rados_ioctx_cct(rados_ioctx_t ioctx)
196 {
197 librados::TestIoCtxImpl *ctx =
198 reinterpret_cast<librados::TestIoCtxImpl*>(ioctx);
199 return reinterpret_cast<rados_config_t>(ctx->get_rados_client()->cct());
200 }
201
202 extern "C" int rados_ioctx_create(rados_t cluster, const char *pool_name,
203 rados_ioctx_t *ioctx) {
204 librados::TestRadosClient *client =
205 reinterpret_cast<librados::TestRadosClient*>(cluster);
206
207 int64_t pool_id = client->pool_lookup(pool_name);
208 if (pool_id < 0) {
209 return static_cast<int>(pool_id);
210 }
211
212 *ioctx = reinterpret_cast<rados_ioctx_t>(
213 client->create_ioctx(pool_id, pool_name));
214 return 0;
215 }
216
217 extern "C" int rados_ioctx_create2(rados_t cluster, int64_t pool_id,
218 rados_ioctx_t *ioctx)
219 {
220 librados::TestRadosClient *client =
221 reinterpret_cast<librados::TestRadosClient*>(cluster);
222
223 std::list<std::pair<int64_t, std::string> > pools;
224 int r = client->pool_list(pools);
225 if (r < 0) {
226 return r;
227 }
228
229 for (std::list<std::pair<int64_t, std::string> >::iterator it =
230 pools.begin(); it != pools.end(); ++it) {
231 if (it->first == pool_id) {
232 *ioctx = reinterpret_cast<rados_ioctx_t>(
233 client->create_ioctx(pool_id, it->second));
234 return 0;
235 }
236 }
237 return -ENOENT;
238 }
239
240 extern "C" void rados_ioctx_destroy(rados_ioctx_t io) {
241 librados::TestIoCtxImpl *ctx =
242 reinterpret_cast<librados::TestIoCtxImpl*>(io);
243 ctx->put();
244 }
245
246 extern "C" rados_t rados_ioctx_get_cluster(rados_ioctx_t io) {
247 librados::TestIoCtxImpl *ctx =
248 reinterpret_cast<librados::TestIoCtxImpl*>(io);
249 return reinterpret_cast<rados_t>(ctx->get_rados_client());
250 }
251
252 extern "C" int rados_mon_command(rados_t cluster, const char **cmd,
253 size_t cmdlen, const char *inbuf,
254 size_t inbuflen, char **outbuf,
255 size_t *outbuflen, char **outs,
256 size_t *outslen) {
257 librados::TestRadosClient *client =
258 reinterpret_cast<librados::TestRadosClient*>(cluster);
259
260 vector<string> cmdvec;
261 for (size_t i = 0; i < cmdlen; i++) {
262 cmdvec.push_back(cmd[i]);
263 }
264
265 bufferlist inbl;
266 inbl.append(inbuf, inbuflen);
267
268 bufferlist outbl;
269 string outstring;
270 int ret = client->mon_command(cmdvec, inbl, &outbl, &outstring);
271
272 do_out_buffer(outbl, outbuf, outbuflen);
273 do_out_buffer(outstring, outs, outslen);
274 return ret;
275 }
276
277 extern "C" int rados_nobjects_list_open(rados_ioctx_t io,
278 rados_list_ctx_t *ctx) {
279 librados::TestIoCtxImpl *io_ctx =
280 reinterpret_cast<librados::TestIoCtxImpl*>(io);
281 librados::TestRadosClient *client = io_ctx->get_rados_client();
282
283 std::list<librados::TestRadosClient::Object> *list =
284 new std::list<librados::TestRadosClient::Object>();
285
286 client->object_list(io_ctx->get_id(), list);
287 list->push_front(librados::TestRadosClient::Object());
288 *ctx = reinterpret_cast<rados_list_ctx_t>(list);
289 return 0;
290 }
291
292 extern "C" int rados_nobjects_list_next(rados_list_ctx_t ctx,
293 const char **entry,
294 const char **key,
295 const char **nspace) {
296 std::list<librados::TestRadosClient::Object> *list =
297 reinterpret_cast<std::list<librados::TestRadosClient::Object> *>(ctx);
298 if (!list->empty()) {
299 list->pop_front();
300 }
301 if (list->empty()) {
302 return -ENOENT;
303 }
304
305 librados::TestRadosClient::Object &obj = list->front();
306 if (entry != NULL) {
307 *entry = obj.oid.c_str();
308 }
309 if (key != NULL) {
310 *key = obj.locator.c_str();
311 }
312 if (nspace != NULL) {
313 *nspace = obj.nspace.c_str();
314 }
315 return 0;
316 }
317
318 extern "C" void rados_nobjects_list_close(rados_list_ctx_t ctx) {
319 std::list<librados::TestRadosClient::Object> *list =
320 reinterpret_cast<std::list<librados::TestRadosClient::Object> *>(ctx);
321 delete list;
322 }
323
324 extern "C" int rados_pool_create(rados_t cluster, const char *pool_name) {
325 librados::TestRadosClient *client =
326 reinterpret_cast<librados::TestRadosClient*>(cluster);
327 return client->pool_create(pool_name);
328 }
329
330 extern "C" int rados_pool_delete(rados_t cluster, const char *pool_name) {
331 librados::TestRadosClient *client =
332 reinterpret_cast<librados::TestRadosClient*>(cluster);
333 return client->pool_delete(pool_name);
334 }
335
336 extern "C" void rados_shutdown(rados_t cluster) {
337 librados::TestRadosClient *client =
338 reinterpret_cast<librados::TestRadosClient*>(cluster);
339 client->put();
340 }
341
342 extern "C" int rados_wait_for_latest_osdmap(rados_t cluster) {
343 librados::TestRadosClient *client =
344 reinterpret_cast<librados::TestRadosClient*>(cluster);
345 return client->wait_for_latest_osdmap();
346 }
347
348 using namespace std::placeholders;
349
350 namespace librados {
351
352 AioCompletion::~AioCompletion()
353 {
354 auto c = reinterpret_cast<AioCompletionImpl *>(pc);
355 c->release();
356 }
357
358 void AioCompletion::release() {
359 delete this;
360 }
361
362 IoCtx::IoCtx() : io_ctx_impl(NULL) {
363 }
364
365 IoCtx::~IoCtx() {
366 close();
367 }
368
369 IoCtx::IoCtx(const IoCtx& rhs) {
370 io_ctx_impl = rhs.io_ctx_impl;
371 if (io_ctx_impl) {
372 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
373 ctx->get();
374 }
375 }
376
377 IoCtx::IoCtx(IoCtx&& rhs) noexcept : io_ctx_impl(std::exchange(rhs.io_ctx_impl, nullptr))
378 {
379 }
380
381 IoCtx& IoCtx::operator=(const IoCtx& rhs) {
382 if (io_ctx_impl) {
383 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
384 ctx->put();
385 }
386
387 io_ctx_impl = rhs.io_ctx_impl;
388 if (io_ctx_impl) {
389 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
390 ctx->get();
391 }
392 return *this;
393 }
394
395 librados::IoCtx& librados::IoCtx::operator=(IoCtx&& rhs) noexcept
396 {
397 if (io_ctx_impl) {
398 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
399 ctx->put();
400 }
401
402 io_ctx_impl = std::exchange(rhs.io_ctx_impl, nullptr);
403 return *this;
404 }
405
406 int IoCtx::aio_flush() {
407 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
408 ctx->aio_flush();
409 return 0;
410 }
411
412 int IoCtx::aio_flush_async(AioCompletion *c) {
413 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
414 ctx->aio_flush_async(c->pc);
415 return 0;
416 }
417
418 int IoCtx::aio_notify(const std::string& oid, AioCompletion *c, bufferlist& bl,
419 uint64_t timeout_ms, bufferlist *pbl) {
420 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
421 ctx->aio_notify(oid, c->pc, bl, timeout_ms, pbl);
422 return 0;
423 }
424
425 int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
426 ObjectReadOperation *op, bufferlist *pbl) {
427 return aio_operate(oid, c, op, 0, pbl);
428 }
429
430 int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
431 ObjectReadOperation *op, int flags,
432 bufferlist *pbl) {
433 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
434 TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
435 return ctx->aio_operate_read(oid, *ops, c->pc, flags, pbl,
436 ctx->get_snap_read(), nullptr);
437 }
438
439 int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
440 ObjectReadOperation *op, int flags,
441 bufferlist *pbl, const blkin_trace_info *trace_info) {
442 return aio_operate(oid, c, op, flags, pbl);
443 }
444
445 int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
446 ObjectWriteOperation *op) {
447 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
448 TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
449 return ctx->aio_operate(oid, *ops, c->pc, NULL, 0);
450 }
451
452 int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
453 ObjectWriteOperation *op, snap_t seq,
454 std::vector<snap_t>& snaps, int flags,
455 const blkin_trace_info *trace_info) {
456 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
457 TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
458
459 std::vector<snapid_t> snv;
460 snv.resize(snaps.size());
461 for (size_t i = 0; i < snaps.size(); ++i)
462 snv[i] = snaps[i];
463 SnapContext snapc(seq, snv);
464
465 return ctx->aio_operate(oid, *ops, c->pc, &snapc, flags);
466 }
467
468 int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
469 ObjectWriteOperation *op, snap_t seq,
470 std::vector<snap_t>& snaps) {
471 return aio_operate(oid, c, op, seq, snaps, 0, nullptr);
472 }
473
474 int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
475 ObjectWriteOperation *op, snap_t seq,
476 std::vector<snap_t>& snaps,
477 const blkin_trace_info *trace_info) {
478 return aio_operate(oid, c, op, seq, snaps, 0, trace_info);
479 }
480
481 int IoCtx::aio_remove(const std::string& oid, AioCompletion *c) {
482 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
483 return ctx->aio_remove(oid, c->pc);
484 }
485
486 int IoCtx::aio_remove(const std::string& oid, AioCompletion *c, int flags) {
487 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
488 return ctx->aio_remove(oid, c->pc, flags);
489 }
490
491 int IoCtx::aio_watch(const std::string& o, AioCompletion *c, uint64_t *handle,
492 librados::WatchCtx2 *watch_ctx) {
493 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
494 return ctx->aio_watch(o, c->pc, handle, watch_ctx);
495 }
496
497 int IoCtx::aio_unwatch(uint64_t handle, AioCompletion *c) {
498 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
499 return ctx->aio_unwatch(handle, c->pc);
500 }
501
502 config_t IoCtx::cct() {
503 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
504 return reinterpret_cast<config_t>(ctx->get_rados_client()->cct());
505 }
506
507 void IoCtx::close() {
508 if (io_ctx_impl) {
509 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
510 ctx->put();
511 }
512 io_ctx_impl = NULL;
513 }
514
515 int IoCtx::create(const std::string& oid, bool exclusive) {
516 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
517 return ctx->execute_operation(
518 oid, std::bind(&TestIoCtxImpl::create, _1, _2, exclusive,
519 ctx->get_snap_context()));
520 }
521
522 void IoCtx::dup(const IoCtx& rhs) {
523 close();
524 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(rhs.io_ctx_impl);
525 io_ctx_impl = reinterpret_cast<IoCtxImpl*>(ctx->clone());
526 }
527
528 int IoCtx::exec(const std::string& oid, const char *cls, const char *method,
529 bufferlist& inbl, bufferlist& outbl) {
530 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
531 return ctx->execute_operation(
532 oid, std::bind(&TestIoCtxImpl::exec, _1, _2,
533 librados_test_stub::get_class_handler(), cls,
534 method, inbl, &outbl, ctx->get_snap_read(),
535 ctx->get_snap_context()));
536 }
537
538 void IoCtx::from_rados_ioctx_t(rados_ioctx_t p, IoCtx &io) {
539 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(p);
540 ctx->get();
541
542 io.close();
543 io.io_ctx_impl = reinterpret_cast<IoCtxImpl*>(ctx);
544 }
545
546 uint64_t IoCtx::get_instance_id() const {
547 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
548 return ctx->get_instance_id();
549 }
550
551 int64_t IoCtx::get_id() {
552 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
553 return ctx->get_id();
554 }
555
556 uint64_t IoCtx::get_last_version() {
557 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
558 return ctx->get_last_version();
559 }
560
561 std::string IoCtx::get_pool_name() {
562 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
563 return ctx->get_pool_name();
564 }
565
566 int IoCtx::list_snaps(const std::string& o, snap_set_t *out_snaps) {
567 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
568 return ctx->execute_operation(
569 o, std::bind(&TestIoCtxImpl::list_snaps, _1, _2, out_snaps));
570 }
571
572 int IoCtx::list_watchers(const std::string& o,
573 std::list<obj_watch_t> *out_watchers) {
574 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
575 return ctx->execute_operation(
576 o, std::bind(&TestIoCtxImpl::list_watchers, _1, _2, out_watchers));
577 }
578
579 int IoCtx::notify(const std::string& o, uint64_t ver, bufferlist& bl) {
580 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
581 return ctx->notify(o, bl, 0, NULL);
582 }
583
584 int IoCtx::notify2(const std::string& o, bufferlist& bl,
585 uint64_t timeout_ms, bufferlist *pbl) {
586 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
587 return ctx->notify(o, bl, timeout_ms, pbl);
588 }
589
590 void IoCtx::notify_ack(const std::string& o, uint64_t notify_id,
591 uint64_t handle, bufferlist& bl) {
592 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
593 ctx->notify_ack(o, notify_id, handle, bl);
594 }
595
596 int IoCtx::omap_get_vals(const std::string& oid,
597 const std::string& start_after,
598 uint64_t max_return,
599 std::map<std::string, bufferlist> *out_vals) {
600 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
601 return ctx->execute_operation(
602 oid, std::bind(&TestIoCtxImpl::omap_get_vals, _1, _2, start_after, "",
603 max_return, out_vals));
604 }
605
606 int IoCtx::operate(const std::string& oid, ObjectWriteOperation *op) {
607 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
608 TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
609 return ctx->operate(oid, *ops);
610 }
611
612 int IoCtx::operate(const std::string& oid, ObjectReadOperation *op,
613 bufferlist *pbl) {
614 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
615 TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
616 return ctx->operate_read(oid, *ops, pbl);
617 }
618
619 int IoCtx::read(const std::string& oid, bufferlist& bl, size_t len,
620 uint64_t off) {
621 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
622 return ctx->execute_operation(
623 oid, std::bind(&TestIoCtxImpl::read, _1, _2, len, off, &bl,
624 ctx->get_snap_read(), nullptr));
625 }
626
627 int IoCtx::remove(const std::string& oid) {
628 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
629 return ctx->execute_operation(
630 oid, std::bind(&TestIoCtxImpl::remove, _1, _2, ctx->get_snap_context()));
631 }
632
633 int IoCtx::selfmanaged_snap_create(uint64_t *snapid) {
634 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
635 return ctx->selfmanaged_snap_create(snapid);
636 }
637
638 void IoCtx::aio_selfmanaged_snap_create(uint64_t *snapid, AioCompletion* c) {
639 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
640 return ctx->aio_selfmanaged_snap_create(snapid, c->pc);
641 }
642
643 int IoCtx::selfmanaged_snap_remove(uint64_t snapid) {
644 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
645 return ctx->selfmanaged_snap_remove(snapid);
646 }
647
648 void IoCtx::aio_selfmanaged_snap_remove(uint64_t snapid, AioCompletion* c) {
649 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
650 ctx->aio_selfmanaged_snap_remove(snapid, c->pc);
651 }
652
653 int IoCtx::selfmanaged_snap_rollback(const std::string& oid,
654 uint64_t snapid) {
655 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
656 return ctx->selfmanaged_snap_rollback(oid, snapid);
657 }
658
659 int IoCtx::selfmanaged_snap_set_write_ctx(snap_t seq,
660 std::vector<snap_t>& snaps) {
661 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
662 return ctx->selfmanaged_snap_set_write_ctx(seq, snaps);
663 }
664
665 void IoCtx::snap_set_read(snap_t seq) {
666 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
667 ctx->set_snap_read(seq);
668 }
669
670 int IoCtx::sparse_read(const std::string& oid, std::map<uint64_t,uint64_t>& m,
671 bufferlist& bl, size_t len, uint64_t off) {
672 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
673 return ctx->execute_operation(
674 oid, std::bind(&TestIoCtxImpl::sparse_read, _1, _2, off, len, &m, &bl,
675 ctx->get_snap_read()));
676 }
677
678 int IoCtx::stat(const std::string& oid, uint64_t *psize, time_t *pmtime) {
679 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
680 return ctx->execute_operation(
681 oid, std::bind(&TestIoCtxImpl::stat, _1, _2, psize, pmtime));
682 }
683
684 int IoCtx::tmap_update(const std::string& oid, bufferlist& cmdbl) {
685 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
686 return ctx->execute_operation(
687 oid, std::bind(&TestIoCtxImpl::tmap_update, _1, _2, cmdbl));
688 }
689
690 int IoCtx::trunc(const std::string& oid, uint64_t off) {
691 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
692 return ctx->execute_operation(
693 oid, std::bind(&TestIoCtxImpl::truncate, _1, _2, off,
694 ctx->get_snap_context()));
695 }
696
697 int IoCtx::unwatch2(uint64_t handle) {
698 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
699 return ctx->unwatch(handle);
700 }
701
702 int IoCtx::unwatch(const std::string& o, uint64_t handle) {
703 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
704 return ctx->unwatch(handle);
705 }
706
707 int IoCtx::watch(const std::string& o, uint64_t ver, uint64_t *handle,
708 librados::WatchCtx *wctx) {
709 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
710 return ctx->watch(o, handle, wctx, NULL);
711 }
712
713 int IoCtx::watch2(const std::string& o, uint64_t *handle,
714 librados::WatchCtx2 *wctx) {
715 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
716 return ctx->watch(o, handle, NULL, wctx);
717 }
718
719 int IoCtx::write(const std::string& oid, bufferlist& bl, size_t len,
720 uint64_t off) {
721 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
722 return ctx->execute_operation(
723 oid, std::bind(&TestIoCtxImpl::write, _1, _2, bl, len, off,
724 ctx->get_snap_context()));
725 }
726
727 int IoCtx::write_full(const std::string& oid, bufferlist& bl) {
728 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
729 return ctx->execute_operation(
730 oid, std::bind(&TestIoCtxImpl::write_full, _1, _2, bl,
731 ctx->get_snap_context()));
732 }
733
734 int IoCtx::writesame(const std::string& oid, bufferlist& bl, size_t len,
735 uint64_t off) {
736 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
737 return ctx->execute_operation(
738 oid, std::bind(&TestIoCtxImpl::writesame, _1, _2, bl, len, off,
739 ctx->get_snap_context()));
740 }
741
742 int IoCtx::cmpext(const std::string& oid, uint64_t off, bufferlist& cmp_bl) {
743 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
744 return ctx->execute_operation(
745 oid, std::bind(&TestIoCtxImpl::cmpext, _1, _2, off, cmp_bl,
746 ctx->get_snap_read()));
747 }
748
749 int IoCtx::application_enable(const std::string& app_name, bool force) {
750 return 0;
751 }
752
753 int IoCtx::application_enable_async(const std::string& app_name,
754 bool force, PoolAsyncCompletion *c) {
755 return -EOPNOTSUPP;
756 }
757
758 int IoCtx::application_list(std::set<std::string> *app_names) {
759 return -EOPNOTSUPP;
760 }
761
762 int IoCtx::application_metadata_get(const std::string& app_name,
763 const std::string &key,
764 std::string *value) {
765 return -EOPNOTSUPP;
766 }
767
768 int IoCtx::application_metadata_set(const std::string& app_name,
769 const std::string &key,
770 const std::string& value) {
771 return -EOPNOTSUPP;
772 }
773
774 int IoCtx::application_metadata_remove(const std::string& app_name,
775 const std::string &key) {
776 return -EOPNOTSUPP;
777 }
778
779 int IoCtx::application_metadata_list(const std::string& app_name,
780 std::map<std::string, std::string> *values) {
781 return -EOPNOTSUPP;
782 }
783
784 void IoCtx::set_namespace(const std::string& nspace) {
785 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
786 ctx->set_namespace(nspace);
787 }
788
789 std::string IoCtx::get_namespace() const {
790 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
791 return ctx->get_namespace();
792 }
793
794 void IoCtx::set_pool_full_try() {
795 }
796
797 bool IoCtx::get_pool_full_try() {
798 return false;
799 }
800
801 static int save_operation_result(int result, int *pval) {
802 if (pval != NULL) {
803 *pval = result;
804 }
805 return result;
806 }
807
808 ObjectOperation::ObjectOperation() {
809 TestObjectOperationImpl *o = new TestObjectOperationImpl();
810 o->get();
811 impl = reinterpret_cast<ObjectOperationImpl*>(o);
812 }
813
814 ObjectOperation::~ObjectOperation() {
815 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
816 if (o) {
817 o->put();
818 o = NULL;
819 }
820 }
821
822 void ObjectOperation::assert_exists() {
823 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
824 o->ops.push_back(std::bind(&TestIoCtxImpl::assert_exists, _1, _2, _4));
825 }
826
827 void ObjectOperation::assert_version(uint64_t ver) {
828 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
829 o->ops.push_back(std::bind(&TestIoCtxImpl::assert_version, _1, _2, ver));
830 }
831
832 void ObjectOperation::exec(const char *cls, const char *method,
833 bufferlist& inbl) {
834 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
835 o->ops.push_back(std::bind(&TestIoCtxImpl::exec, _1, _2,
836 librados_test_stub::get_class_handler(), cls,
837 method, inbl, _3, _4, _5));
838 }
839
840 void ObjectOperation::set_op_flags2(int flags) {
841 }
842
843 size_t ObjectOperation::size() {
844 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
845 return o->ops.size();
846 }
847
848 void ObjectOperation::cmpext(uint64_t off, const bufferlist& cmp_bl,
849 int *prval) {
850 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
851 ObjectOperationTestImpl op = std::bind(&TestIoCtxImpl::cmpext, _1, _2, off,
852 cmp_bl, _4);
853 if (prval != NULL) {
854 op = std::bind(save_operation_result,
855 std::bind(op, _1, _2, _3, _4, _5, _6), prval);
856 }
857 o->ops.push_back(op);
858 }
859
860 void ObjectReadOperation::list_snaps(snap_set_t *out_snaps, int *prval) {
861 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
862
863 ObjectOperationTestImpl op = std::bind(&TestIoCtxImpl::list_snaps, _1, _2,
864 out_snaps);
865 if (prval != NULL) {
866 op = std::bind(save_operation_result,
867 std::bind(op, _1, _2, _3, _4, _5, _6), prval);
868 }
869 o->ops.push_back(op);
870 }
871
872 void ObjectReadOperation::list_watchers(std::list<obj_watch_t> *out_watchers,
873 int *prval) {
874 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
875
876 ObjectOperationTestImpl op = std::bind(&TestIoCtxImpl::list_watchers, _1,
877 _2, out_watchers);
878 if (prval != NULL) {
879 op = std::bind(save_operation_result,
880 std::bind(op, _1, _2, _3, _4, _5, _6), prval);
881 }
882 o->ops.push_back(op);
883 }
884
885 void ObjectReadOperation::read(size_t off, uint64_t len, bufferlist *pbl,
886 int *prval) {
887 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
888
889 ObjectOperationTestImpl op;
890 if (pbl != NULL) {
891 op = std::bind(&TestIoCtxImpl::read, _1, _2, len, off, pbl, _4, nullptr);
892 } else {
893 op = std::bind(&TestIoCtxImpl::read, _1, _2, len, off, _3, _4, nullptr);
894 }
895
896 if (prval != NULL) {
897 op = std::bind(save_operation_result,
898 std::bind(op, _1, _2, _3, _4, _5, _6), prval);
899 }
900 o->ops.push_back(op);
901 }
902
903 void ObjectReadOperation::sparse_read(uint64_t off, uint64_t len,
904 std::map<uint64_t,uint64_t> *m,
905 bufferlist *pbl, int *prval) {
906 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
907
908 ObjectOperationTestImpl op;
909 if (pbl != NULL) {
910 op = std::bind(&TestIoCtxImpl::sparse_read, _1, _2, off, len, m, pbl, _4);
911 } else {
912 op = std::bind(&TestIoCtxImpl::sparse_read, _1, _2, off, len, m, _3, _4);
913 }
914
915 if (prval != NULL) {
916 op = std::bind(save_operation_result,
917 std::bind(op, _1, _2, _3, _4, _5, _6), prval);
918 }
919 o->ops.push_back(op);
920 }
921
922 void ObjectReadOperation::stat(uint64_t *psize, time_t *pmtime, int *prval) {
923 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
924
925 ObjectOperationTestImpl op = std::bind(&TestIoCtxImpl::stat, _1, _2,
926 psize, pmtime);
927
928 if (prval != NULL) {
929 op = std::bind(save_operation_result,
930 std::bind(op, _1, _2, _3, _4, _5, _6), prval);
931 }
932 o->ops.push_back(op);
933 }
934
935 void ObjectWriteOperation::append(const bufferlist &bl) {
936 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
937 o->ops.push_back(std::bind(&TestIoCtxImpl::append, _1, _2, bl, _5));
938 }
939
940 void ObjectWriteOperation::create(bool exclusive) {
941 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
942 o->ops.push_back(std::bind(&TestIoCtxImpl::create, _1, _2, exclusive, _5));
943 }
944
945 void ObjectWriteOperation::omap_set(const std::map<std::string, bufferlist> &map) {
946 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
947 o->ops.push_back(std::bind(&TestIoCtxImpl::omap_set, _1, _2, boost::ref(map)));
948 }
949
950 void ObjectWriteOperation::remove() {
951 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
952 o->ops.push_back(std::bind(&TestIoCtxImpl::remove, _1, _2, _5));
953 }
954
955 void ObjectWriteOperation::selfmanaged_snap_rollback(uint64_t snapid) {
956 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
957 o->ops.push_back(std::bind(&TestIoCtxImpl::selfmanaged_snap_rollback,
958 _1, _2, snapid));
959 }
960
961 void ObjectWriteOperation::set_alloc_hint(uint64_t expected_object_size,
962 uint64_t expected_write_size) {
963 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
964 o->ops.push_back(std::bind(&TestIoCtxImpl::set_alloc_hint, _1, _2,
965 expected_object_size, expected_write_size, 0,
966 _5));
967 }
968
969 void ObjectWriteOperation::set_alloc_hint2(uint64_t expected_object_size,
970 uint64_t expected_write_size,
971 uint32_t flags) {
972 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
973 o->ops.push_back(std::bind(&TestIoCtxImpl::set_alloc_hint, _1, _2,
974 expected_object_size, expected_write_size, flags,
975 _5));
976 }
977
978 void ObjectWriteOperation::tmap_update(const bufferlist& cmdbl) {
979 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
980 o->ops.push_back(std::bind(&TestIoCtxImpl::tmap_update, _1, _2,
981 cmdbl));
982 }
983
984 void ObjectWriteOperation::truncate(uint64_t off) {
985 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
986 o->ops.push_back(std::bind(&TestIoCtxImpl::truncate, _1, _2, off, _5));
987 }
988
989 void ObjectWriteOperation::write(uint64_t off, const bufferlist& bl) {
990 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
991 o->ops.push_back(std::bind(&TestIoCtxImpl::write, _1, _2, bl, bl.length(),
992 off, _5));
993 }
994
995 void ObjectWriteOperation::write_full(const bufferlist& bl) {
996 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
997 o->ops.push_back(std::bind(&TestIoCtxImpl::write_full, _1, _2, bl, _5));
998 }
999
1000 void ObjectWriteOperation::writesame(uint64_t off, uint64_t len,
1001 const bufferlist& bl) {
1002 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
1003 o->ops.push_back(std::bind(&TestIoCtxImpl::writesame, _1, _2, bl, len,
1004 off, _5));
1005 }
1006
1007 void ObjectWriteOperation::zero(uint64_t off, uint64_t len) {
1008 TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
1009 o->ops.push_back(std::bind(&TestIoCtxImpl::zero, _1, _2, off, len, _5));
1010 }
1011
1012 Rados::Rados() : client(NULL) {
1013 }
1014
1015 Rados::Rados(IoCtx& ioctx) {
1016 TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(ioctx.io_ctx_impl);
1017 TestRadosClient *impl = ctx->get_rados_client();
1018 impl->get();
1019
1020 client = reinterpret_cast<RadosClient*>(impl);
1021 ceph_assert(client != NULL);
1022 }
1023
1024 Rados::~Rados() {
1025 shutdown();
1026 }
1027
1028 void Rados::from_rados_t(rados_t p, Rados &rados) {
1029 if (rados.client != nullptr) {
1030 reinterpret_cast<TestRadosClient*>(rados.client)->put();
1031 rados.client = nullptr;
1032 }
1033
1034 auto impl = reinterpret_cast<TestRadosClient*>(p);
1035 if (impl) {
1036 impl->get();
1037 rados.client = reinterpret_cast<RadosClient*>(impl);
1038 }
1039 }
1040
1041 AioCompletion *Rados::aio_create_completion(void *cb_arg,
1042 callback_t cb_complete) {
1043 AioCompletionImpl *c;
1044 int r = rados_aio_create_completion2(cb_arg, cb_complete,
1045 reinterpret_cast<void**>(&c));
1046 ceph_assert(r == 0);
1047 return new AioCompletion(c);
1048 }
1049
1050 int Rados::aio_watch_flush(AioCompletion* c) {
1051 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1052 return impl->aio_watch_flush(c->pc);
1053 }
1054
1055 int Rados::blocklist_add(const std::string& client_address,
1056 uint32_t expire_seconds) {
1057 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1058 return impl->blocklist_add(client_address, expire_seconds);
1059 }
1060
1061 config_t Rados::cct() {
1062 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1063 return reinterpret_cast<config_t>(impl->cct());
1064 }
1065
1066 int Rados::cluster_fsid(std::string* fsid) {
1067 *fsid = "00000000-1111-2222-3333-444444444444";
1068 return 0;
1069 }
1070
1071 int Rados::conf_set(const char *option, const char *value) {
1072 return rados_conf_set(reinterpret_cast<rados_t>(client), option, value);
1073 }
1074
1075 int Rados::conf_get(const char *option, std::string &val) {
1076 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1077 CephContext *cct = impl->cct();
1078
1079 char *str = NULL;
1080 int ret = cct->_conf.get_val(option, &str, -1);
1081 if (ret != 0) {
1082 free(str);
1083 return ret;
1084 }
1085
1086 val = str;
1087 free(str);
1088 return 0;
1089 }
1090
1091 int Rados::conf_parse_env(const char *env) const {
1092 return rados_conf_parse_env(reinterpret_cast<rados_t>(client), env);
1093 }
1094
1095 int Rados::conf_read_file(const char * const path) const {
1096 return rados_conf_read_file(reinterpret_cast<rados_t>(client), path);
1097 }
1098
1099 int Rados::connect() {
1100 return rados_connect(reinterpret_cast<rados_t>(client));
1101 }
1102
1103 uint64_t Rados::get_instance_id() {
1104 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1105 return impl->get_instance_id();
1106 }
1107
1108 int Rados::get_min_compatible_osd(int8_t* require_osd_release) {
1109 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1110 return impl->get_min_compatible_osd(require_osd_release);
1111 }
1112
1113 int Rados::get_min_compatible_client(int8_t* min_compat_client,
1114 int8_t* require_min_compat_client) {
1115 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1116 return impl->get_min_compatible_client(min_compat_client,
1117 require_min_compat_client);
1118 }
1119
1120 int Rados::init(const char * const id) {
1121 return rados_create(reinterpret_cast<rados_t *>(&client), id);
1122 }
1123
1124 int Rados::init_with_context(config_t cct_) {
1125 return rados_create_with_context(reinterpret_cast<rados_t *>(&client), cct_);
1126 }
1127
1128 int Rados::ioctx_create(const char *name, IoCtx &io) {
1129 rados_ioctx_t p;
1130 int ret = rados_ioctx_create(reinterpret_cast<rados_t>(client), name, &p);
1131 if (ret) {
1132 return ret;
1133 }
1134
1135 io.close();
1136 io.io_ctx_impl = reinterpret_cast<IoCtxImpl*>(p);
1137 return 0;
1138 }
1139
1140 int Rados::ioctx_create2(int64_t pool_id, IoCtx &io)
1141 {
1142 rados_ioctx_t p;
1143 int ret = rados_ioctx_create2(reinterpret_cast<rados_t>(client), pool_id, &p);
1144 if (ret) {
1145 return ret;
1146 }
1147
1148 io.close();
1149 io.io_ctx_impl = reinterpret_cast<IoCtxImpl*>(p);
1150 return 0;
1151 }
1152
1153 int Rados::mon_command(std::string cmd, const bufferlist& inbl,
1154 bufferlist *outbl, std::string *outs) {
1155 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1156
1157 std::vector<std::string> cmds;
1158 cmds.push_back(cmd);
1159 return impl->mon_command(cmds, inbl, outbl, outs);
1160 }
1161
1162 int Rados::service_daemon_register(const std::string& service,
1163 const std::string& name,
1164 const std::map<std::string,std::string>& metadata) {
1165 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1166 return impl->service_daemon_register(service, name, metadata);
1167 }
1168
1169 int Rados::service_daemon_update_status(std::map<std::string,std::string>&& status) {
1170 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1171 return impl->service_daemon_update_status(std::move(status));
1172 }
1173
1174 int Rados::pool_create(const char *name) {
1175 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1176 return impl->pool_create(name);
1177 }
1178
1179 int Rados::pool_delete(const char *name) {
1180 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1181 return impl->pool_delete(name);
1182 }
1183
1184 int Rados::pool_get_base_tier(int64_t pool, int64_t* base_tier) {
1185 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1186 return impl->pool_get_base_tier(pool, base_tier);
1187 }
1188
1189 int Rados::pool_list(std::list<std::string>& v) {
1190 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1191 std::list<std::pair<int64_t, std::string> > pools;
1192 int r = impl->pool_list(pools);
1193 if (r < 0) {
1194 return r;
1195 }
1196
1197 v.clear();
1198 for (std::list<std::pair<int64_t, std::string> >::iterator it = pools.begin();
1199 it != pools.end(); ++it) {
1200 v.push_back(it->second);
1201 }
1202 return 0;
1203 }
1204
1205 int Rados::pool_list2(std::list<std::pair<int64_t, std::string> >& v)
1206 {
1207 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1208 return impl->pool_list(v);
1209 }
1210
1211 int64_t Rados::pool_lookup(const char *name) {
1212 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1213 return impl->pool_lookup(name);
1214 }
1215
1216 int Rados::pool_reverse_lookup(int64_t id, std::string *name) {
1217 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1218 return impl->pool_reverse_lookup(id, name);
1219 }
1220
1221 void Rados::shutdown() {
1222 if (client == NULL) {
1223 return;
1224 }
1225 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1226 impl->put();
1227 client = NULL;
1228 }
1229
1230 void Rados::test_blocklist_self(bool set) {
1231 }
1232
1233 int Rados::wait_for_latest_osdmap() {
1234 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1235 return impl->wait_for_latest_osdmap();
1236 }
1237
1238 int Rados::watch_flush() {
1239 TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
1240 return impl->watch_flush();
1241 }
1242
1243 WatchCtx::~WatchCtx() {
1244 }
1245
1246 WatchCtx2::~WatchCtx2() {
1247 }
1248
1249 } // namespace librados
1250
1251 int cls_cxx_create(cls_method_context_t hctx, bool exclusive) {
1252 librados::TestClassHandler::MethodContext *ctx =
1253 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1254 return ctx->io_ctx_impl->create(ctx->oid, exclusive, ctx->snapc);
1255 }
1256
1257 int cls_cxx_remove(cls_method_context_t hctx) {
1258 librados::TestClassHandler::MethodContext *ctx =
1259 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1260 return ctx->io_ctx_impl->remove(ctx->oid, ctx->io_ctx_impl->get_snap_context());
1261 }
1262
1263 int cls_get_request_origin(cls_method_context_t hctx, entity_inst_t *origin) {
1264 librados::TestClassHandler::MethodContext *ctx =
1265 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1266
1267 librados::TestRadosClient *rados_client =
1268 ctx->io_ctx_impl->get_rados_client();
1269
1270 struct sockaddr_in sin;
1271 memset(&sin, 0, sizeof(sin));
1272 sin.sin_family = AF_INET;
1273 sin.sin_port = 0;
1274 inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
1275
1276 entity_addr_t entity_addr(entity_addr_t::TYPE_DEFAULT,
1277 rados_client->get_nonce());
1278 entity_addr.in4_addr() = sin;
1279
1280 *origin = entity_inst_t(
1281 entity_name_t::CLIENT(rados_client->get_instance_id()),
1282 entity_addr);
1283 return 0;
1284 }
1285
1286 int cls_cxx_getxattr(cls_method_context_t hctx, const char *name,
1287 bufferlist *outbl) {
1288 std::map<string, bufferlist> attrs;
1289 int r = cls_cxx_getxattrs(hctx, &attrs);
1290 if (r < 0) {
1291 return r;
1292 }
1293
1294 std::map<string, bufferlist>::iterator it = attrs.find(name);
1295 if (it == attrs.end()) {
1296 return -ENODATA;
1297 }
1298 *outbl = it->second;
1299 return 0;
1300 }
1301
1302 int cls_cxx_getxattrs(cls_method_context_t hctx, std::map<string, bufferlist> *attrset) {
1303 librados::TestClassHandler::MethodContext *ctx =
1304 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1305 return ctx->io_ctx_impl->xattr_get(ctx->oid, attrset);
1306 }
1307
1308 int cls_cxx_map_get_keys(cls_method_context_t hctx, const string &start_obj,
1309 uint64_t max_to_get, std::set<string> *keys, bool *more) {
1310 librados::TestClassHandler::MethodContext *ctx =
1311 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1312
1313 keys->clear();
1314 std::map<string, bufferlist> vals;
1315 int r = ctx->io_ctx_impl->omap_get_vals2(ctx->oid, start_obj, "", max_to_get,
1316 &vals, more);
1317 if (r < 0) {
1318 return r;
1319 }
1320
1321 for (std::map<string, bufferlist>::iterator it = vals.begin();
1322 it != vals.end(); ++it) {
1323 keys->insert(it->first);
1324 }
1325 return keys->size();
1326 }
1327
1328 int cls_cxx_map_get_val(cls_method_context_t hctx, const string &key,
1329 bufferlist *outbl) {
1330 librados::TestClassHandler::MethodContext *ctx =
1331 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1332
1333 std::map<string, bufferlist> vals;
1334 int r = ctx->io_ctx_impl->omap_get_vals(ctx->oid, "", key, 1024, &vals);
1335 if (r < 0) {
1336 return r;
1337 }
1338
1339 std::map<string, bufferlist>::iterator it = vals.find(key);
1340 if (it == vals.end()) {
1341 return -ENOENT;
1342 }
1343
1344 *outbl = it->second;
1345 return 0;
1346 }
1347
1348 int cls_cxx_map_get_vals(cls_method_context_t hctx, const string &start_obj,
1349 const string &filter_prefix, uint64_t max_to_get,
1350 std::map<string, bufferlist> *vals, bool *more) {
1351 librados::TestClassHandler::MethodContext *ctx =
1352 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1353 int r = ctx->io_ctx_impl->omap_get_vals2(ctx->oid, start_obj, filter_prefix,
1354 max_to_get, vals, more);
1355 if (r < 0) {
1356 return r;
1357 }
1358 return vals->size();
1359 }
1360
1361 int cls_cxx_map_remove_key(cls_method_context_t hctx, const string &key) {
1362 std::set<std::string> keys;
1363 keys.insert(key);
1364
1365 librados::TestClassHandler::MethodContext *ctx =
1366 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1367 return ctx->io_ctx_impl->omap_rm_keys(ctx->oid, keys);
1368 }
1369
1370 int cls_cxx_map_set_val(cls_method_context_t hctx, const string &key,
1371 bufferlist *inbl) {
1372 std::map<std::string, bufferlist> m;
1373 m[key] = *inbl;
1374 return cls_cxx_map_set_vals(hctx, &m);
1375 }
1376
1377 int cls_cxx_map_set_vals(cls_method_context_t hctx,
1378 const std::map<string, bufferlist> *map) {
1379 librados::TestClassHandler::MethodContext *ctx =
1380 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1381 return ctx->io_ctx_impl->omap_set(ctx->oid, *map);
1382 }
1383
1384 int cls_cxx_read(cls_method_context_t hctx, int ofs, int len,
1385 bufferlist *outbl) {
1386 return cls_cxx_read2(hctx, ofs, len, outbl, 0);
1387 }
1388
1389 int cls_cxx_read2(cls_method_context_t hctx, int ofs, int len,
1390 bufferlist *outbl, uint32_t op_flags) {
1391 librados::TestClassHandler::MethodContext *ctx =
1392 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1393 return ctx->io_ctx_impl->read(
1394 ctx->oid, len, ofs, outbl, ctx->snap_id, nullptr);
1395 }
1396
1397 int cls_cxx_setxattr(cls_method_context_t hctx, const char *name,
1398 bufferlist *inbl) {
1399 librados::TestClassHandler::MethodContext *ctx =
1400 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1401 return ctx->io_ctx_impl->xattr_set(ctx->oid, name, *inbl);
1402 }
1403
1404 int cls_cxx_stat(cls_method_context_t hctx, uint64_t *size, time_t *mtime) {
1405 librados::TestClassHandler::MethodContext *ctx =
1406 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1407 return ctx->io_ctx_impl->stat(ctx->oid, size, mtime);
1408 }
1409
1410 int cls_cxx_write(cls_method_context_t hctx, int ofs, int len,
1411 bufferlist *inbl) {
1412 return cls_cxx_write2(hctx, ofs, len, inbl, 0);
1413 }
1414
1415 int cls_cxx_write2(cls_method_context_t hctx, int ofs, int len,
1416 bufferlist *inbl, uint32_t op_flags) {
1417 librados::TestClassHandler::MethodContext *ctx =
1418 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1419 return ctx->io_ctx_impl->write(ctx->oid, *inbl, len, ofs, ctx->snapc);
1420 }
1421
1422 int cls_cxx_write_full(cls_method_context_t hctx, bufferlist *inbl) {
1423 librados::TestClassHandler::MethodContext *ctx =
1424 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1425 return ctx->io_ctx_impl->write_full(ctx->oid, *inbl, ctx->snapc);
1426 }
1427
1428 int cls_cxx_replace(cls_method_context_t hctx, int ofs, int len,
1429 bufferlist *inbl) {
1430 librados::TestClassHandler::MethodContext *ctx =
1431 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1432 int r = ctx->io_ctx_impl->truncate(ctx->oid, 0, ctx->snapc);
1433 if (r < 0) {
1434 return r;
1435 }
1436 return ctx->io_ctx_impl->write(ctx->oid, *inbl, len, ofs, ctx->snapc);
1437 }
1438
1439 int cls_cxx_truncate(cls_method_context_t hctx, int ofs) {
1440 librados::TestClassHandler::MethodContext *ctx =
1441 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1442 return ctx->io_ctx_impl->truncate(ctx->oid, ofs, ctx->snapc);
1443 }
1444
1445 int cls_cxx_write_zero(cls_method_context_t hctx, int ofs, int len) {
1446 librados::TestClassHandler::MethodContext *ctx =
1447 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1448 return ctx->io_ctx_impl->zero(ctx->oid, len, ofs, ctx->snapc);
1449 }
1450
1451 int cls_cxx_list_watchers(cls_method_context_t hctx,
1452 obj_list_watch_response_t *watchers) {
1453 librados::TestClassHandler::MethodContext *ctx =
1454 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1455
1456 std::list<obj_watch_t> obj_watchers;
1457 int r = ctx->io_ctx_impl->list_watchers(ctx->oid, &obj_watchers);
1458 if (r < 0) {
1459 return r;
1460 }
1461
1462 for (auto &w : obj_watchers) {
1463 watch_item_t watcher;
1464 watcher.name = entity_name_t::CLIENT(w.watcher_id);
1465 watcher.cookie = w.cookie;
1466 watcher.timeout_seconds = w.timeout_seconds;
1467 watcher.addr.parse(w.addr);
1468 watchers->entries.push_back(watcher);
1469 }
1470
1471 return 0;
1472 }
1473
1474 uint64_t cls_get_features(cls_method_context_t hctx) {
1475 return CEPH_FEATURES_SUPPORTED_DEFAULT;
1476 }
1477
1478 uint64_t cls_get_client_features(cls_method_context_t hctx) {
1479 return CEPH_FEATURES_SUPPORTED_DEFAULT;
1480 }
1481
1482 int cls_get_snapset_seq(cls_method_context_t hctx, uint64_t *snap_seq) {
1483 librados::TestClassHandler::MethodContext *ctx =
1484 reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
1485 librados::snap_set_t snapset;
1486 int r = ctx->io_ctx_impl->list_snaps(ctx->oid, &snapset);
1487 if (r < 0) {
1488 return r;
1489 }
1490
1491 *snap_seq = snapset.seq;
1492 return 0;
1493 }
1494
1495 int cls_log(int level, const char *format, ...) {
1496 int size = 256;
1497 va_list ap;
1498 while (1) {
1499 char buf[size];
1500 va_start(ap, format);
1501 int n = vsnprintf(buf, size, format, ap);
1502 va_end(ap);
1503 if ((n > -1 && n < size) || size > 8196) {
1504 dout(ceph::dout::need_dynamic(level)) << buf << dendl;
1505 return n;
1506 }
1507 size *= 2;
1508 }
1509 return 0;
1510 }
1511
1512 int cls_register(const char *name, cls_handle_t *handle) {
1513 librados::TestClassHandler *cls = librados_test_stub::get_class_handler();
1514 return cls->create(name, handle);
1515 }
1516
1517 int cls_register_cxx_method(cls_handle_t hclass, const char *method,
1518 int flags,
1519 cls_method_cxx_call_t class_call,
1520 cls_method_handle_t *handle) {
1521 librados::TestClassHandler *cls = librados_test_stub::get_class_handler();
1522 return cls->create_method(hclass, method, class_call, handle);
1523 }
1524
1525 int cls_register_cxx_filter(cls_handle_t hclass,
1526 const std::string &filter_name,
1527 cls_cxx_filter_factory_t fn,
1528 cls_filter_handle_t *)
1529 {
1530 librados::TestClassHandler *cls = librados_test_stub::get_class_handler();
1531 return cls->create_filter(hclass, filter_name, fn);
1532 }
1533
1534 ceph_release_t cls_get_required_osd_release(cls_handle_t hclass) {
1535 return ceph_release_t::nautilus;
1536 }
1537
1538 ceph_release_t cls_get_min_compatible_client(cls_handle_t hclass) {
1539 return ceph_release_t::nautilus;
1540 }
1541
1542 // stubs to silence TestClassHandler::open_class()
1543 PGLSFilter::~PGLSFilter()
1544 {}
1545
1546 int cls_gen_rand_base64(char *, int) {
1547 return -ENOTSUP;
1548 }
1549
1550 int cls_cxx_chunk_write_and_set(cls_method_handle_t, int,
1551 int, bufferlist *,
1552 uint32_t, bufferlist *, int) {
1553 return -ENOTSUP;
1554 }
1555
1556 int cls_cxx_map_read_header(cls_method_handle_t, bufferlist *) {
1557 return -ENOTSUP;
1558 }
1559
1560 uint64_t cls_get_osd_min_alloc_size(cls_method_context_t hctx) {
1561 return 0;
1562 }
1563
1564 uint64_t cls_get_pool_stripe_width(cls_method_context_t hctx) {
1565 return 0;
1566 }