1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License version 2, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "include/int_types.h"
16 #include "include/rados/librados.h"
17 #include "include/rbd_types.h"
18 #include "include/rbd/librbd.h"
19 #include "include/rbd/librbd.hpp"
20 #include "include/event_type.h"
21 #include "include/err.h"
23 #include "gtest/gtest.h"
29 #include <sys/types.h>
35 #include <condition_variable>
43 #include "test/librados/test.h"
44 #include "test/librbd/test_support.h"
45 #include "common/event_socket.h"
46 #include "include/interval_set.h"
47 #include "include/stringify.h"
49 #include <boost/assign/list_of.hpp>
50 #include <boost/scope_exit.hpp>
53 #include <sys/eventfd.h>
58 using std::chrono::seconds
;
60 #define ASSERT_PASSED(x, args...) \
62 bool passed = false; \
64 ASSERT_TRUE(passed); \
67 void register_test_librbd() {
70 static int get_features(bool *old_format
, uint64_t *features
)
72 const char *c
= getenv("RBD_FEATURES");
80 cout
<< "using new format!" << std::endl
;
84 cout
<< "using old format" << std::endl
;
90 static int create_image_full(rados_ioctx_t ioctx
, const char *name
,
91 uint64_t size
, int *order
, int old_format
,
95 // ensure old-format tests actually use the old format
96 int r
= rados_conf_set(rados_ioctx_get_cluster(ioctx
),
97 "rbd_default_format", "1");
101 return rbd_create(ioctx
, name
, size
, order
);
102 } else if ((features
& RBD_FEATURE_STRIPINGV2
) != 0) {
103 uint64_t stripe_unit
= IMAGE_STRIPE_UNIT
;
105 // use a conservative stripe_unit for non default order
106 stripe_unit
= (1ull << (*order
-1));
109 printf("creating image with stripe unit: %" PRIu64
", "
110 "stripe count: %" PRIu64
"\n",
111 stripe_unit
, IMAGE_STRIPE_COUNT
);
112 return rbd_create3(ioctx
, name
, size
, features
, order
,
113 stripe_unit
, IMAGE_STRIPE_COUNT
);
115 return rbd_create2(ioctx
, name
, size
, features
, order
);
119 static int clone_image(rados_ioctx_t p_ioctx
,
120 rbd_image_t p_image
, const char *p_name
,
121 const char *p_snap_name
, rados_ioctx_t c_ioctx
,
122 const char *c_name
, uint64_t features
, int *c_order
)
124 uint64_t stripe_unit
, stripe_count
;
127 r
= rbd_get_stripe_unit(p_image
, &stripe_unit
);
132 r
= rbd_get_stripe_count(p_image
, &stripe_count
);
137 return rbd_clone2(p_ioctx
, p_name
, p_snap_name
, c_ioctx
,
138 c_name
, features
, c_order
, stripe_unit
, stripe_count
);
142 static int create_image(rados_ioctx_t ioctx
, const char *name
,
143 uint64_t size
, int *order
)
148 int r
= get_features(&old_format
, &features
);
151 return create_image_full(ioctx
, name
, size
, order
, old_format
, features
);
154 static int create_image_pp(librbd::RBD
&rbd
,
155 librados::IoCtx
&ioctx
,
157 uint64_t size
, int *order
) {
160 int r
= get_features(&old_format
, &features
);
164 librados::Rados
rados(ioctx
);
165 int r
= rados
.conf_set("rbd_default_format", "1");
169 return rbd
.create(ioctx
, name
, size
, order
);
171 return rbd
.create2(ioctx
, name
, size
, features
, order
);
175 class TestLibRBD
: public ::testing::Test
{
178 TestLibRBD() : m_pool_number() {
181 static void SetUpTestCase() {
182 static bool seeded
= false;
186 cout
<< "seed " << seed
<< std::endl
;
191 _unique_pool_names
.clear();
193 ASSERT_EQ("", connect_cluster(&_cluster
));
194 ASSERT_EQ("", connect_cluster_pp(_rados
));
196 create_optional_data_pool();
199 static void TearDownTestCase() {
200 rados_shutdown(_cluster
);
201 _rados
.wait_for_latest_osdmap();
202 _pool_names
.insert(_pool_names
.end(), _unique_pool_names
.begin(),
203 _unique_pool_names
.end());
204 for (size_t i
= 1; i
< _pool_names
.size(); ++i
) {
205 ASSERT_EQ(0, _rados
.pool_delete(_pool_names
[i
].c_str()));
207 if (!_pool_names
.empty()) {
208 ASSERT_EQ(0, destroy_one_pool_pp(_pool_names
[0], _rados
));
212 void SetUp() override
{
213 ASSERT_NE("", m_pool_name
= create_pool());
216 bool is_skip_partial_discard_enabled() {
218 EXPECT_EQ(0, _rados
.conf_get("rbd_skip_partial_discard", value
));
219 return value
== "true";
222 void validate_object_map(rbd_image_t image
, bool *passed
) {
224 ASSERT_EQ(0, rbd_get_flags(image
, &flags
));
225 *passed
= ((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) == 0);
228 void validate_object_map(librbd::Image
&image
, bool *passed
) {
230 ASSERT_EQ(0, image
.get_flags(&flags
));
231 *passed
= ((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) == 0);
234 std::string
get_temp_image_name() {
236 return "image" + stringify(_image_number
);
239 static void create_optional_data_pool() {
240 bool created
= false;
241 std::string data_pool
;
242 ASSERT_EQ(0, create_image_data_pool(_rados
, data_pool
, &created
));
243 if (!data_pool
.empty()) {
244 printf("using image data pool: %s\n", data_pool
.c_str());
246 _unique_pool_names
.push_back(data_pool
);
251 std::string
create_pool(bool unique
= false) {
252 librados::Rados rados
;
253 std::string pool_name
;
255 pool_name
= get_temp_pool_name("test-librbd-");
256 EXPECT_EQ("", create_one_pool_pp(pool_name
, rados
));
257 _unique_pool_names
.push_back(pool_name
);
258 } else if (m_pool_number
< _pool_names
.size()) {
259 pool_name
= _pool_names
[m_pool_number
];
261 pool_name
= get_temp_pool_name("test-librbd-");
262 EXPECT_EQ("", create_one_pool_pp(pool_name
, rados
));
263 _pool_names
.push_back(pool_name
);
269 static std::vector
<std::string
> _pool_names
;
270 static std::vector
<std::string
> _unique_pool_names
;
271 static rados_t _cluster
;
272 static librados::Rados _rados
;
273 static uint64_t _image_number
;
275 std::string m_pool_name
;
276 uint32_t m_pool_number
;
280 std::vector
<std::string
> TestLibRBD::_pool_names
;
281 std::vector
<std::string
> TestLibRBD::_unique_pool_names
;
282 rados_t
TestLibRBD::_cluster
;
283 librados::Rados
TestLibRBD::_rados
;
284 uint64_t TestLibRBD::_image_number
= 0;
286 TEST_F(TestLibRBD
, CreateAndStat
)
289 ASSERT_EQ(0, rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
));
291 rbd_image_info_t info
;
294 std::string name
= get_temp_image_name();
295 uint64_t size
= 2 << 20;
297 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
298 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
299 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
300 printf("image has size %llu and order %d\n", (unsigned long long) info
.size
, info
.order
);
301 ASSERT_EQ(info
.size
, size
);
302 ASSERT_EQ(info
.order
, order
);
303 ASSERT_EQ(0, rbd_close(image
));
305 rados_ioctx_destroy(ioctx
);
308 TEST_F(TestLibRBD
, CreateWithSameDataPool
)
313 ASSERT_EQ(0, rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
));
316 std::string name
= get_temp_image_name();
317 uint64_t size
= 2 << 20;
321 ASSERT_EQ(0, get_features(&old_format
, &features
));
322 ASSERT_FALSE(old_format
);
324 rbd_image_options_t image_options
;
325 rbd_image_options_create(&image_options
);
326 BOOST_SCOPE_EXIT( (&image_options
) ) {
327 rbd_image_options_destroy(image_options
);
328 } BOOST_SCOPE_EXIT_END
;
330 ASSERT_EQ(0, rbd_image_options_set_uint64(image_options
,
331 RBD_IMAGE_OPTION_FEATURES
,
333 ASSERT_EQ(0, rbd_image_options_set_string(image_options
,
334 RBD_IMAGE_OPTION_DATA_POOL
,
335 m_pool_name
.c_str()));
337 ASSERT_EQ(0, rbd_create4(ioctx
, name
.c_str(), size
, image_options
));
338 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
340 ASSERT_EQ(0, rbd_close(image
));
342 rados_ioctx_destroy(ioctx
);
345 TEST_F(TestLibRBD
, CreateAndStatPP
)
347 librados::IoCtx ioctx
;
348 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
352 librbd::image_info_t info
;
355 std::string name
= get_temp_image_name();
356 uint64_t size
= 2 << 20;
358 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
359 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
360 ASSERT_EQ(0, image
.stat(info
, sizeof(info
)));
361 ASSERT_EQ(info
.size
, size
);
362 ASSERT_EQ(info
.order
, order
);
368 TEST_F(TestLibRBD
, GetId
)
371 ASSERT_EQ(0, rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
));
375 std::string name
= get_temp_image_name();
377 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), 0, &order
));
378 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
381 if (!is_feature_enabled(0)) {
383 ASSERT_EQ(-EINVAL
, rbd_get_id(image
, id
, sizeof(id
)));
385 ASSERT_EQ(-ERANGE
, rbd_get_id(image
, id
, 0));
386 ASSERT_EQ(0, rbd_get_id(image
, id
, sizeof(id
)));
387 ASSERT_LT(0U, strlen(id
));
390 ASSERT_EQ(0, rbd_close(image
));
391 rados_ioctx_destroy(ioctx
);
394 TEST_F(TestLibRBD
, GetIdPP
)
396 librados::IoCtx ioctx
;
397 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
402 std::string name
= get_temp_image_name();
405 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), 0, &order
));
406 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
407 if (!is_feature_enabled(0)) {
409 ASSERT_EQ(-EINVAL
, image
.get_id(&id
));
411 ASSERT_EQ(0, image
.get_id(&id
));
412 ASSERT_LT(0U, id
.size());
416 TEST_F(TestLibRBD
, GetBlockNamePrefix
)
419 ASSERT_EQ(0, rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
));
423 std::string name
= get_temp_image_name();
425 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), 0, &order
));
426 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
429 ASSERT_EQ(-ERANGE
, rbd_get_block_name_prefix(image
, prefix
, 0));
430 ASSERT_EQ(0, rbd_get_block_name_prefix(image
, prefix
, sizeof(prefix
)));
431 ASSERT_LT(0U, strlen(prefix
));
433 ASSERT_EQ(0, rbd_close(image
));
434 rados_ioctx_destroy(ioctx
);
437 TEST_F(TestLibRBD
, GetBlockNamePrefixPP
)
439 librados::IoCtx ioctx
;
440 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
445 std::string name
= get_temp_image_name();
447 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), 0, &order
));
448 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
449 ASSERT_LT(0U, image
.get_block_name_prefix().size());
452 TEST_F(TestLibRBD
, TestGetCreateTimestamp
)
457 ASSERT_EQ(0, rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
));
461 std::string name
= get_temp_image_name();
463 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), 0, &order
));
464 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
466 struct timespec timestamp
;
467 ASSERT_EQ(0, rbd_get_create_timestamp(image
, ×tamp
));
468 ASSERT_LT(0, timestamp
.tv_sec
);
470 ASSERT_EQ(0, rbd_close(image
));
472 rados_ioctx_destroy(ioctx
);
475 TEST_F(TestLibRBD
, GetCreateTimestampPP
)
479 librados::IoCtx ioctx
;
480 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
485 std::string name
= get_temp_image_name();
487 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), 0, &order
));
488 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
490 struct timespec timestamp
;
491 ASSERT_EQ(0, image
.get_create_timestamp(×tamp
));
492 ASSERT_LT(0, timestamp
.tv_sec
);
495 TEST_F(TestLibRBD
, OpenAio
)
498 ASSERT_EQ(0, rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
));
500 rbd_image_info_t info
;
503 std::string name
= get_temp_image_name();
504 uint64_t size
= 2 << 20;
506 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
508 rbd_completion_t open_comp
;
509 ASSERT_EQ(0, rbd_aio_create_completion(NULL
, NULL
, &open_comp
));
510 ASSERT_EQ(0, rbd_aio_open(ioctx
, name
.c_str(), &image
, NULL
, open_comp
));
511 ASSERT_EQ(0, rbd_aio_wait_for_complete(open_comp
));
512 ASSERT_EQ(1, rbd_aio_is_complete(open_comp
));
513 ASSERT_EQ(0, rbd_aio_get_return_value(open_comp
));
514 rbd_aio_release(open_comp
);
516 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
517 printf("image has size %llu and order %d\n", (unsigned long long) info
.size
, info
.order
);
518 ASSERT_EQ(info
.size
, size
);
519 ASSERT_EQ(info
.order
, order
);
521 rbd_completion_t close_comp
;
522 ASSERT_EQ(0, rbd_aio_create_completion(NULL
, NULL
, &close_comp
));
523 ASSERT_EQ(0, rbd_aio_close(image
, close_comp
));
524 ASSERT_EQ(0, rbd_aio_wait_for_complete(close_comp
));
525 ASSERT_EQ(1, rbd_aio_is_complete(close_comp
));
526 ASSERT_EQ(0, rbd_aio_get_return_value(close_comp
));
527 rbd_aio_release(close_comp
);
529 rados_ioctx_destroy(ioctx
);
532 TEST_F(TestLibRBD
, OpenAioFail
)
535 ASSERT_EQ(0, rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
));
537 std::string name
= get_temp_image_name();
539 rbd_completion_t open_comp
;
540 ASSERT_EQ(0, rbd_aio_create_completion(NULL
, NULL
, &open_comp
));
541 ASSERT_EQ(0, rbd_aio_open(ioctx
, name
.c_str(), &image
, NULL
, open_comp
));
542 ASSERT_EQ(0, rbd_aio_wait_for_complete(open_comp
));
543 ASSERT_EQ(1, rbd_aio_is_complete(open_comp
));
544 ASSERT_EQ(-ENOENT
, rbd_aio_get_return_value(open_comp
));
545 rbd_aio_release(open_comp
);
547 rados_ioctx_destroy(ioctx
);
550 TEST_F(TestLibRBD
, OpenAioPP
)
552 librados::IoCtx ioctx
;
553 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
556 librbd::image_info_t info
;
559 std::string name
= get_temp_image_name();
560 uint64_t size
= 2 << 20;
562 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
564 librbd::RBD::AioCompletion
*open_comp
=
565 new librbd::RBD::AioCompletion(NULL
, NULL
);
566 ASSERT_EQ(0, rbd
.aio_open(ioctx
, image
, name
.c_str(), NULL
, open_comp
));
567 ASSERT_EQ(0, open_comp
->wait_for_complete());
568 ASSERT_EQ(1, open_comp
->is_complete());
569 ASSERT_EQ(0, open_comp
->get_return_value());
570 open_comp
->release();
572 ASSERT_EQ(0, image
.stat(info
, sizeof(info
)));
573 ASSERT_EQ(info
.size
, size
);
574 ASSERT_EQ(info
.order
, order
);
577 open_comp
= new librbd::RBD::AioCompletion(NULL
, NULL
);
578 ASSERT_EQ(0, rbd
.aio_open(ioctx
, image
, name
.c_str(), NULL
, open_comp
));
579 ASSERT_EQ(0, open_comp
->wait_for_complete());
580 ASSERT_EQ(1, open_comp
->is_complete());
581 ASSERT_EQ(0, open_comp
->get_return_value());
582 open_comp
->release();
585 librbd::RBD::AioCompletion
*close_comp
=
586 new librbd::RBD::AioCompletion(NULL
, NULL
);
587 ASSERT_EQ(0, image
.aio_close(close_comp
));
588 ASSERT_EQ(0, close_comp
->wait_for_complete());
589 ASSERT_EQ(1, close_comp
->is_complete());
590 ASSERT_EQ(0, close_comp
->get_return_value());
591 close_comp
->release();
593 // close closed image
594 close_comp
= new librbd::RBD::AioCompletion(NULL
, NULL
);
595 ASSERT_EQ(-EINVAL
, image
.aio_close(close_comp
));
596 close_comp
->release();
601 TEST_F(TestLibRBD
, OpenAioFailPP
)
603 librados::IoCtx ioctx
;
604 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
609 std::string name
= get_temp_image_name();
611 librbd::RBD::AioCompletion
*open_comp
=
612 new librbd::RBD::AioCompletion(NULL
, NULL
);
613 ASSERT_EQ(0, rbd
.aio_open(ioctx
, image
, name
.c_str(), NULL
, open_comp
));
614 ASSERT_EQ(0, open_comp
->wait_for_complete());
615 ASSERT_EQ(1, open_comp
->is_complete());
616 ASSERT_EQ(-ENOENT
, open_comp
->get_return_value());
617 open_comp
->release();
623 TEST_F(TestLibRBD
, ResizeAndStat
)
626 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
628 rbd_image_info_t info
;
631 std::string name
= get_temp_image_name();
632 uint64_t size
= 2 << 20;
634 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
635 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
637 ASSERT_EQ(0, rbd_resize(image
, size
* 4));
638 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
639 ASSERT_EQ(info
.size
, size
* 4);
641 ASSERT_EQ(0, rbd_resize(image
, size
/ 2));
642 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
643 ASSERT_EQ(info
.size
, size
/ 2);
645 // downsizing without allowing shrink should fail
646 // and image size should not change
647 ASSERT_EQ(-EINVAL
, rbd_resize2(image
, size
/ 4, false, NULL
, NULL
));
648 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
649 ASSERT_EQ(info
.size
, size
/ 2);
651 ASSERT_EQ(0, rbd_resize2(image
, size
/ 4, true, NULL
, NULL
));
652 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
653 ASSERT_EQ(info
.size
, size
/ 4);
655 ASSERT_PASSED(validate_object_map
, image
);
656 ASSERT_EQ(0, rbd_close(image
));
658 rados_ioctx_destroy(ioctx
);
661 TEST_F(TestLibRBD
, ResizeAndStatPP
)
663 librados::IoCtx ioctx
;
664 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
668 librbd::image_info_t info
;
671 std::string name
= get_temp_image_name();
672 uint64_t size
= 2 << 20;
674 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
675 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
677 ASSERT_EQ(0, image
.resize(size
* 4));
678 ASSERT_EQ(0, image
.stat(info
, sizeof(info
)));
679 ASSERT_EQ(info
.size
, size
* 4);
681 ASSERT_EQ(0, image
.resize(size
/ 2));
682 ASSERT_EQ(0, image
.stat(info
, sizeof(info
)));
683 ASSERT_EQ(info
.size
, size
/ 2);
684 ASSERT_PASSED(validate_object_map
, image
);
690 TEST_F(TestLibRBD
, UpdateWatchAndResize
)
693 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
697 std::string name
= get_temp_image_name();
698 uint64_t size
= 2 << 20;
700 rbd_image_t
&m_image
;
702 condition_variable m_cond
;
704 static void cb(void *arg
) {
705 Watcher
*watcher
= static_cast<Watcher
*>(arg
);
706 watcher
->handle_notify();
708 Watcher(rbd_image_t
&image
) : m_image(image
) {}
709 void handle_notify() {
710 rbd_image_info_t info
;
711 ASSERT_EQ(0, rbd_stat(m_image
, &info
, sizeof(info
)));
712 lock_guard
<mutex
> locker(m_lock
);
716 void wait_for_size(size_t size
) {
717 unique_lock
<mutex
> locker(m_lock
);
718 ASSERT_TRUE(m_cond
.wait_for(locker
, seconds(5),
720 return this->m_size
== size
;}));
725 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
726 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
728 ASSERT_EQ(0, rbd_update_watch(image
, &handle
, Watcher::cb
, &watcher
));
730 ASSERT_EQ(0, rbd_resize(image
, size
* 4));
731 watcher
.wait_for_size(size
* 4);
733 ASSERT_EQ(0, rbd_resize(image
, size
/ 2));
734 watcher
.wait_for_size(size
/ 2);
736 ASSERT_EQ(0, rbd_update_unwatch(image
, handle
));
738 ASSERT_EQ(0, rbd_close(image
));
739 rados_ioctx_destroy(ioctx
);
742 TEST_F(TestLibRBD
, UpdateWatchAndResizePP
)
744 librados::IoCtx ioctx
;
745 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
751 std::string name
= get_temp_image_name();
752 uint64_t size
= 2 << 20;
753 struct Watcher
: public librbd::UpdateWatchCtx
{
754 Watcher(librbd::Image
&image
) : m_image(image
) {
756 void handle_notify() override
{
757 librbd::image_info_t info
;
758 ASSERT_EQ(0, m_image
.stat(info
, sizeof(info
)));
759 lock_guard
<mutex
> locker(m_lock
);
763 void wait_for_size(size_t size
) {
764 unique_lock
<mutex
> locker(m_lock
);
765 ASSERT_TRUE(m_cond
.wait_for(locker
, seconds(5),
767 return this->m_size
== size
;}));
769 librbd::Image
&m_image
;
771 condition_variable m_cond
;
776 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
777 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
779 ASSERT_EQ(0, image
.update_watch(&watcher
, &handle
));
781 ASSERT_EQ(0, image
.resize(size
* 4));
782 watcher
.wait_for_size(size
* 4);
784 ASSERT_EQ(0, image
.resize(size
/ 2));
785 watcher
.wait_for_size(size
/ 2);
787 ASSERT_EQ(0, image
.update_unwatch(handle
));
793 int test_ls(rados_ioctx_t io_ctx
, size_t num_expected
, ...)
796 char *names
, *cur_name
;
798 size_t max_size
= 1024;
800 names
= (char *) malloc(sizeof(char) * 1024);
801 int len
= rbd_list(io_ctx
, names
, &max_size
);
803 std::set
<std::string
> image_names
;
804 for (i
= 0, num_images
= 0, cur_name
= names
; cur_name
< names
+ len
; i
++) {
805 printf("image: %s\n", cur_name
);
806 image_names
.insert(cur_name
);
807 cur_name
+= strlen(cur_name
) + 1;
812 va_start(ap
, num_expected
);
813 for (i
= num_expected
; i
> 0; i
--) {
814 char *expected
= va_arg(ap
, char *);
815 printf("expected = %s\n", expected
);
816 std::set
<std::string
>::iterator it
= image_names
.find(expected
);
817 if (it
!= image_names
.end()) {
818 printf("found %s\n", expected
);
819 image_names
.erase(it
);
820 printf("erased %s\n", expected
);
822 ADD_FAILURE() << "Unable to find image " << expected
;
829 if (!image_names
.empty()) {
830 ADD_FAILURE() << "Unexpected images discovered";
836 TEST_F(TestLibRBD
, TestCreateLsDelete
)
839 rados_ioctx_create(_cluster
, create_pool(true).c_str(), &ioctx
);
842 std::string name
= get_temp_image_name();
843 std::string name2
= get_temp_image_name();
844 uint64_t size
= 2 << 20;
846 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
847 ASSERT_EQ(1, test_ls(ioctx
, 1, name
.c_str()));
848 ASSERT_EQ(0, create_image(ioctx
, name2
.c_str(), size
, &order
));
849 ASSERT_EQ(2, test_ls(ioctx
, 2, name
.c_str(), name2
.c_str()));
850 ASSERT_EQ(0, rbd_remove(ioctx
, name
.c_str()));
851 ASSERT_EQ(1, test_ls(ioctx
, 1, name2
.c_str()));
853 ASSERT_EQ(-ENOENT
, rbd_remove(ioctx
, name
.c_str()));
855 rados_ioctx_destroy(ioctx
);
858 int test_ls_pp(librbd::RBD
& rbd
, librados::IoCtx
& io_ctx
, size_t num_expected
, ...)
863 vector
<string
> names
;
864 r
= rbd
.list(io_ctx
, names
);
868 cout
<< "num images is: " << names
.size() << std::endl
869 << "expected: " << num_expected
<< std::endl
;
870 int num
= names
.size();
872 for (i
= 0; i
< names
.size(); i
++) {
873 cout
<< "image: " << names
[i
] << std::endl
;
876 va_start(ap
, num_expected
);
877 for (i
= num_expected
; i
> 0; i
--) {
878 char *expected
= va_arg(ap
, char *);
879 cout
<< "expected = " << expected
<< std::endl
;
880 vector
<string
>::iterator listed_name
= find(names
.begin(), names
.end(), string(expected
));
881 if (listed_name
== names
.end()) {
882 ADD_FAILURE() << "Unable to find image " << expected
;
886 names
.erase(listed_name
);
890 if (!names
.empty()) {
891 ADD_FAILURE() << "Unexpected images discovered";
897 TEST_F(TestLibRBD
, TestCreateLsDeletePP
)
899 librados::IoCtx ioctx
;
900 ASSERT_EQ(0, _rados
.ioctx_create(create_pool(true).c_str(), ioctx
));
906 std::string name
= get_temp_image_name();
907 std::string name2
= get_temp_image_name();
908 uint64_t size
= 2 << 20;
910 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
911 ASSERT_EQ(1, test_ls_pp(rbd
, ioctx
, 1, name
.c_str()));
912 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name2
.c_str(), size
, &order
));
913 ASSERT_EQ(2, test_ls_pp(rbd
, ioctx
, 2, name
.c_str(), name2
.c_str()));
914 ASSERT_EQ(0, rbd
.remove(ioctx
, name
.c_str()));
915 ASSERT_EQ(1, test_ls_pp(rbd
, ioctx
, 1, name2
.c_str()));
922 static int print_progress_percent(uint64_t offset
, uint64_t src_size
,
925 float percent
= ((float)offset
* 100) / src_size
;
926 printf("%3.2f%% done\n", percent
);
930 TEST_F(TestLibRBD
, TestCopy
)
933 rados_ioctx_create(_cluster
, create_pool(true).c_str(), &ioctx
);
939 std::string name
= get_temp_image_name();
940 std::string name2
= get_temp_image_name();
941 std::string name3
= get_temp_image_name();
943 uint64_t size
= 2 << 20;
945 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
946 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
947 ASSERT_EQ(1, test_ls(ioctx
, 1, name
.c_str()));
949 size_t sum_key_len
= 0;
950 size_t sum_value_len
= 0;
953 for (int i
= 1; i
<= 70; i
++) {
954 key
= "key" + stringify(i
);
955 val
= "value" + stringify(i
);
956 ASSERT_EQ(0, rbd_metadata_set(image
, key
.c_str(), val
.c_str()));
958 sum_key_len
+= (key
.size() + 1);
959 sum_value_len
+= (val
.size() + 1);
964 size_t keys_len
= sizeof(keys
);
965 size_t vals_len
= sizeof(vals
);
968 size_t value_len
= sizeof(value
);
970 ASSERT_EQ(0, rbd_copy(image
, ioctx
, name2
.c_str()));
971 ASSERT_EQ(2, test_ls(ioctx
, 2, name
.c_str(), name2
.c_str()));
972 ASSERT_EQ(0, rbd_open(ioctx
, name2
.c_str(), &image2
, NULL
));
973 ASSERT_EQ(0, rbd_metadata_list(image2
, "", 70, keys
, &keys_len
, vals
,
975 ASSERT_EQ(keys_len
, sum_key_len
);
976 ASSERT_EQ(vals_len
, sum_value_len
);
978 for (int i
= 1; i
<= 70; i
++) {
979 key
= "key" + stringify(i
);
980 val
= "value" + stringify(i
);
981 ASSERT_EQ(0, rbd_metadata_get(image2
, key
.c_str(), value
, &value_len
));
982 ASSERT_STREQ(val
.c_str(), value
);
984 value_len
= sizeof(value
);
987 ASSERT_EQ(0, rbd_copy_with_progress(image
, ioctx
, name3
.c_str(),
988 print_progress_percent
, NULL
));
989 ASSERT_EQ(3, test_ls(ioctx
, 3, name
.c_str(), name2
.c_str(), name3
.c_str()));
991 keys_len
= sizeof(keys
);
992 vals_len
= sizeof(vals
);
993 ASSERT_EQ(0, rbd_open(ioctx
, name3
.c_str(), &image3
, NULL
));
994 ASSERT_EQ(0, rbd_metadata_list(image3
, "", 70, keys
, &keys_len
, vals
,
996 ASSERT_EQ(keys_len
, sum_key_len
);
997 ASSERT_EQ(vals_len
, sum_value_len
);
999 for (int i
= 1; i
<= 70; i
++) {
1000 key
= "key" + stringify(i
);
1001 val
= "value" + stringify(i
);
1002 ASSERT_EQ(0, rbd_metadata_get(image3
, key
.c_str(), value
, &value_len
));
1003 ASSERT_STREQ(val
.c_str(), value
);
1005 value_len
= sizeof(value
);
1008 ASSERT_EQ(0, rbd_close(image
));
1009 ASSERT_EQ(0, rbd_close(image2
));
1010 ASSERT_EQ(0, rbd_close(image3
));
1011 rados_ioctx_destroy(ioctx
);
1014 class PrintProgress
: public librbd::ProgressContext
1017 int update_progress(uint64_t offset
, uint64_t src_size
) override
1019 float percent
= ((float)offset
* 100) / src_size
;
1020 printf("%3.2f%% done\n", percent
);
1025 TEST_F(TestLibRBD
, TestCopyPP
)
1027 librados::IoCtx ioctx
;
1028 ASSERT_EQ(0, _rados
.ioctx_create(create_pool(true).c_str(), ioctx
));
1032 librbd::Image image
;
1033 librbd::Image image2
;
1034 librbd::Image image3
;
1036 std::string name
= get_temp_image_name();
1037 std::string name2
= get_temp_image_name();
1038 std::string name3
= get_temp_image_name();
1039 uint64_t size
= 2 << 20;
1042 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
1043 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
1047 for (int i
= 1; i
<= 70; i
++) {
1048 key
= "key" + stringify(i
);
1049 val
= "value" + stringify(i
);
1050 ASSERT_EQ(0, image
.metadata_set(key
, val
));
1053 ASSERT_EQ(1, test_ls_pp(rbd
, ioctx
, 1, name
.c_str()));
1054 ASSERT_EQ(0, image
.copy(ioctx
, name2
.c_str()));
1055 ASSERT_EQ(2, test_ls_pp(rbd
, ioctx
, 2, name
.c_str(), name2
.c_str()));
1056 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name2
.c_str(), NULL
));
1058 map
<string
, bufferlist
> pairs
;
1060 ASSERT_EQ(0, image2
.metadata_list("", 70, &pairs
));
1061 ASSERT_EQ(70U, pairs
.size());
1063 for (int i
= 1; i
<= 70; i
++) {
1064 key
= "key" + stringify(i
);
1065 val
= "value" + stringify(i
);
1066 ASSERT_EQ(0, image2
.metadata_get(key
.c_str(), &value
));
1067 ASSERT_STREQ(val
.c_str(), value
.c_str());
1070 ASSERT_EQ(0, image
.copy_with_progress(ioctx
, name3
.c_str(), pp
));
1071 ASSERT_EQ(3, test_ls_pp(rbd
, ioctx
, 3, name
.c_str(), name2
.c_str(),
1073 ASSERT_EQ(0, rbd
.open(ioctx
, image3
, name3
.c_str(), NULL
));
1076 ASSERT_EQ(0, image3
.metadata_list("", 70, &pairs
));
1077 ASSERT_EQ(70U, pairs
.size());
1079 for (int i
= 1; i
<= 70; i
++) {
1080 key
= "key" + stringify(i
);
1081 val
= "value" + stringify(i
);
1082 ASSERT_EQ(0, image3
.metadata_get(key
.c_str(), &value
));
1083 ASSERT_STREQ(val
.c_str(), value
.c_str());
1090 int test_ls_snaps(rbd_image_t image
, int num_expected
, ...)
1092 int num_snaps
, i
, j
, max_size
= 10;
1094 rbd_snap_info_t snaps
[max_size
];
1095 num_snaps
= rbd_snap_list(image
, snaps
, &max_size
);
1096 printf("num snaps is: %d\nexpected: %d\n", num_snaps
, num_expected
);
1098 for (i
= 0; i
< num_snaps
; i
++) {
1099 printf("snap: %s\n", snaps
[i
].name
);
1102 va_start(ap
, num_expected
);
1103 for (i
= num_expected
; i
> 0; i
--) {
1104 char *expected
= va_arg(ap
, char *);
1105 uint64_t expected_size
= va_arg(ap
, uint64_t);
1107 for (j
= 0; j
< num_snaps
; j
++) {
1108 if (snaps
[j
].name
== NULL
)
1110 if (strcmp(snaps
[j
].name
, expected
) == 0) {
1111 printf("found %s with size %llu\n", snaps
[j
].name
, (unsigned long long) snaps
[j
].size
);
1112 EXPECT_EQ(expected_size
, snaps
[j
].size
);
1113 free((void *) snaps
[j
].name
);
1114 snaps
[j
].name
= NULL
;
1123 for (i
= 0; i
< num_snaps
; i
++) {
1124 EXPECT_EQ((const char *)0, snaps
[i
].name
);
1130 TEST_F(TestLibRBD
, TestCreateLsDeleteSnap
)
1132 rados_ioctx_t ioctx
;
1133 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
1137 std::string name
= get_temp_image_name();
1138 uint64_t size
= 2 << 20;
1139 uint64_t size2
= 4 << 20;
1141 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
1142 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
1144 ASSERT_EQ(0, rbd_snap_create(image
, "snap1"));
1145 ASSERT_EQ(1, test_ls_snaps(image
, 1, "snap1", size
));
1146 ASSERT_EQ(0, rbd_resize(image
, size2
));
1147 ASSERT_EQ(0, rbd_snap_create(image
, "snap2"));
1148 ASSERT_EQ(2, test_ls_snaps(image
, 2, "snap1", size
, "snap2", size2
));
1149 ASSERT_EQ(0, rbd_snap_remove(image
, "snap1"));
1150 ASSERT_EQ(1, test_ls_snaps(image
, 1, "snap2", size2
));
1151 ASSERT_EQ(0, rbd_snap_remove(image
, "snap2"));
1152 ASSERT_EQ(0, test_ls_snaps(image
, 0));
1154 ASSERT_EQ(0, rbd_close(image
));
1156 rados_ioctx_destroy(ioctx
);
1159 int test_get_snapshot_timestamp(rbd_image_t image
, uint64_t snap_id
)
1161 struct timespec timestamp
;
1162 EXPECT_EQ(0, rbd_snap_get_timestamp(image
, snap_id
, ×tamp
));
1163 EXPECT_LT(0, timestamp
.tv_sec
);
1167 TEST_F(TestLibRBD
, TestGetSnapShotTimeStamp
)
1169 REQUIRE_FORMAT_V2();
1171 rados_ioctx_t ioctx
;
1172 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
1176 std::string name
= get_temp_image_name();
1177 uint64_t size
= 2 << 20;
1178 int num_snaps
, max_size
= 10;
1179 rbd_snap_info_t snaps
[max_size
];
1181 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
1182 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
1184 ASSERT_EQ(0, rbd_snap_create(image
, "snap1"));
1185 num_snaps
= rbd_snap_list(image
, snaps
, &max_size
);
1186 ASSERT_EQ(1, num_snaps
);
1187 ASSERT_EQ(0, test_get_snapshot_timestamp(image
, snaps
[0].id
));
1188 free((void *)snaps
[0].name
);
1190 ASSERT_EQ(0, rbd_snap_create(image
, "snap2"));
1191 num_snaps
= rbd_snap_list(image
, snaps
, &max_size
);
1192 ASSERT_EQ(2, num_snaps
);
1193 ASSERT_EQ(0, test_get_snapshot_timestamp(image
, snaps
[0].id
));
1194 ASSERT_EQ(0, test_get_snapshot_timestamp(image
, snaps
[1].id
));
1195 free((void *)snaps
[0].name
);
1196 free((void *)snaps
[1].name
);
1198 ASSERT_EQ(0, rbd_close(image
));
1200 rados_ioctx_destroy(ioctx
);
1204 int test_ls_snaps(librbd::Image
& image
, size_t num_expected
, ...)
1209 vector
<librbd::snap_info_t
> snaps
;
1210 r
= image
.snap_list(snaps
);
1211 EXPECT_TRUE(r
>= 0);
1212 cout
<< "num snaps is: " << snaps
.size() << std::endl
1213 << "expected: " << num_expected
<< std::endl
;
1215 for (i
= 0; i
< snaps
.size(); i
++) {
1216 cout
<< "snap: " << snaps
[i
].name
<< std::endl
;
1219 va_start(ap
, num_expected
);
1220 for (i
= num_expected
; i
> 0; i
--) {
1221 char *expected
= va_arg(ap
, char *);
1222 uint64_t expected_size
= va_arg(ap
, uint64_t);
1224 for (j
= 0; j
< snaps
.size(); j
++) {
1225 if (snaps
[j
].name
== "")
1227 if (strcmp(snaps
[j
].name
.c_str(), expected
) == 0) {
1228 cout
<< "found " << snaps
[j
].name
<< " with size " << snaps
[j
].size
1230 EXPECT_EQ(expected_size
, snaps
[j
].size
);
1240 for (i
= 0; i
< snaps
.size(); i
++) {
1241 EXPECT_EQ("", snaps
[i
].name
);
1244 return snaps
.size();
1247 TEST_F(TestLibRBD
, TestCreateLsDeleteSnapPP
)
1249 librados::IoCtx ioctx
;
1250 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
1254 librbd::Image image
;
1256 std::string name
= get_temp_image_name();
1257 uint64_t size
= 2 << 20;
1258 uint64_t size2
= 4 << 20;
1260 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
1261 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
1264 ASSERT_EQ(0, image
.snap_exists2("snap1", &exists
));
1265 ASSERT_FALSE(exists
);
1266 ASSERT_EQ(0, image
.snap_create("snap1"));
1267 ASSERT_EQ(0, image
.snap_exists2("snap1", &exists
));
1268 ASSERT_TRUE(exists
);
1269 ASSERT_EQ(1, test_ls_snaps(image
, 1, "snap1", size
));
1270 ASSERT_EQ(0, image
.resize(size2
));
1271 ASSERT_EQ(0, image
.snap_exists2("snap2", &exists
));
1272 ASSERT_FALSE(exists
);
1273 ASSERT_EQ(0, image
.snap_create("snap2"));
1274 ASSERT_EQ(0, image
.snap_exists2("snap2", &exists
));
1275 ASSERT_TRUE(exists
);
1276 ASSERT_EQ(2, test_ls_snaps(image
, 2, "snap1", size
, "snap2", size2
));
1277 ASSERT_EQ(0, image
.snap_remove("snap1"));
1278 ASSERT_EQ(0, image
.snap_exists2("snap1", &exists
));
1279 ASSERT_FALSE(exists
);
1280 ASSERT_EQ(1, test_ls_snaps(image
, 1, "snap2", size2
));
1281 ASSERT_EQ(0, image
.snap_remove("snap2"));
1282 ASSERT_EQ(0, image
.snap_exists2("snap2", &exists
));
1283 ASSERT_FALSE(exists
);
1284 ASSERT_EQ(0, test_ls_snaps(image
, 0));
1290 TEST_F(TestLibRBD
, TestCreateLsRenameSnapPP
)
1292 librados::IoCtx ioctx
;
1293 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
1297 librbd::Image image
;
1299 std::string name
= get_temp_image_name();
1300 uint64_t size
= 2 << 20;
1301 uint64_t size2
= 4 << 20;
1303 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
1304 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
1307 ASSERT_EQ(0, image
.snap_exists2("snap1", &exists
));
1308 ASSERT_FALSE(exists
);
1309 ASSERT_EQ(0, image
.snap_create("snap1"));
1310 ASSERT_EQ(0, image
.snap_exists2("snap1", &exists
));
1311 ASSERT_TRUE(exists
);
1312 ASSERT_EQ(1, test_ls_snaps(image
, 1, "snap1", size
));
1313 ASSERT_EQ(0, image
.resize(size2
));
1314 ASSERT_EQ(0, image
.snap_exists2("snap2", &exists
));
1315 ASSERT_FALSE(exists
);
1316 ASSERT_EQ(0, image
.snap_create("snap2"));
1317 ASSERT_EQ(0, image
.snap_exists2("snap2", &exists
));
1318 ASSERT_TRUE(exists
);
1319 ASSERT_EQ(2, test_ls_snaps(image
, 2, "snap1", size
, "snap2", size2
));
1320 ASSERT_EQ(0, image
.snap_rename("snap1","snap1-rename"));
1321 ASSERT_EQ(2, test_ls_snaps(image
, 2, "snap1-rename", size
, "snap2", size2
));
1322 ASSERT_EQ(0, image
.snap_exists2("snap1", &exists
));
1323 ASSERT_FALSE(exists
);
1324 ASSERT_EQ(0, image
.snap_exists2("snap1-rename", &exists
));
1325 ASSERT_TRUE(exists
);
1326 ASSERT_EQ(0, image
.snap_remove("snap1-rename"));
1327 ASSERT_EQ(0, image
.snap_rename("snap2","snap2-rename"));
1328 ASSERT_EQ(1, test_ls_snaps(image
, 1, "snap2-rename", size2
));
1329 ASSERT_EQ(0, image
.snap_exists2("snap2", &exists
));
1330 ASSERT_FALSE(exists
);
1331 ASSERT_EQ(0, image
.snap_exists2("snap2-rename", &exists
));
1332 ASSERT_TRUE(exists
);
1333 ASSERT_EQ(0, image
.snap_remove("snap2-rename"));
1334 ASSERT_EQ(0, test_ls_snaps(image
, 0));
1340 void simple_write_cb(rbd_completion_t cb
, void *arg
)
1342 printf("write completion cb called!\n");
1345 void simple_read_cb(rbd_completion_t cb
, void *arg
)
1347 printf("read completion cb called!\n");
1350 void aio_write_test_data_and_poll(rbd_image_t image
, int fd
, const char *test_data
,
1351 uint64_t off
, size_t len
, uint32_t iohint
, bool *passed
)
1353 rbd_completion_t comp
;
1354 uint64_t data
= 0x123;
1355 rbd_aio_create_completion((void*)&data
, (rbd_callback_t
) simple_write_cb
, &comp
);
1356 printf("created completion\n");
1357 printf("started write\n");
1359 rbd_aio_write2(image
, off
, len
, test_data
, comp
, iohint
);
1361 rbd_aio_write(image
, off
, len
, test_data
, comp
);
1365 pfd
.events
= POLLIN
;
1367 ASSERT_EQ(1, poll(&pfd
, 1, -1));
1368 ASSERT_TRUE(pfd
.revents
& POLLIN
);
1370 rbd_completion_t comps
[1];
1371 ASSERT_EQ(1, rbd_poll_io_events(image
, comps
, 1));
1373 ASSERT_EQ(static_cast<ssize_t
>(sizeof(count
)),
1374 read(fd
, &count
, sizeof(count
)));
1375 int r
= rbd_aio_get_return_value(comps
[0]);
1376 ASSERT_TRUE(rbd_aio_is_complete(comps
[0]));
1377 ASSERT_TRUE(*(uint64_t*)rbd_aio_get_arg(comps
[0]) == data
);
1378 printf("return value is: %d\n", r
);
1380 printf("finished write\n");
1381 rbd_aio_release(comps
[0]);
1385 void aio_write_test_data(rbd_image_t image
, const char *test_data
, uint64_t off
, size_t len
, uint32_t iohint
, bool *passed
)
1387 rbd_completion_t comp
;
1388 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_write_cb
, &comp
);
1389 printf("created completion\n");
1391 rbd_aio_write2(image
, off
, len
, test_data
, comp
, iohint
);
1393 rbd_aio_write(image
, off
, len
, test_data
, comp
);
1394 printf("started write\n");
1395 rbd_aio_wait_for_complete(comp
);
1396 int r
= rbd_aio_get_return_value(comp
);
1397 printf("return value is: %d\n", r
);
1399 printf("finished write\n");
1400 rbd_aio_release(comp
);
1404 void write_test_data(rbd_image_t image
, const char *test_data
, uint64_t off
, size_t len
, uint32_t iohint
, bool *passed
)
1408 written
= rbd_write2(image
, off
, len
, test_data
, iohint
);
1410 written
= rbd_write(image
, off
, len
, test_data
);
1411 printf("wrote: %d\n", (int) written
);
1412 ASSERT_EQ(len
, static_cast<size_t>(written
));
1416 void aio_discard_test_data(rbd_image_t image
, uint64_t off
, uint64_t len
, bool *passed
)
1418 rbd_completion_t comp
;
1419 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_write_cb
, &comp
);
1420 rbd_aio_discard(image
, off
, len
, comp
);
1421 rbd_aio_wait_for_complete(comp
);
1422 int r
= rbd_aio_get_return_value(comp
);
1424 printf("aio discard: %d~%d = %d\n", (int)off
, (int)len
, (int)r
);
1425 rbd_aio_release(comp
);
1429 void discard_test_data(rbd_image_t image
, uint64_t off
, size_t len
, bool *passed
)
1432 written
= rbd_discard(image
, off
, len
);
1433 printf("discard: %d~%d = %d\n", (int)off
, (int)len
, (int)written
);
1434 ASSERT_EQ(len
, static_cast<size_t>(written
));
1438 void aio_read_test_data_and_poll(rbd_image_t image
, int fd
, const char *expected
,
1439 uint64_t off
, size_t len
, uint32_t iohint
, bool *passed
)
1441 rbd_completion_t comp
;
1442 char *result
= (char *)malloc(len
+ 1);
1444 ASSERT_NE(static_cast<char *>(NULL
), result
);
1445 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1446 printf("created completion\n");
1447 printf("started read\n");
1449 rbd_aio_read2(image
, off
, len
, result
, comp
, iohint
);
1451 rbd_aio_read(image
, off
, len
, result
, comp
);
1455 pfd
.events
= POLLIN
;
1457 ASSERT_EQ(1, poll(&pfd
, 1, -1));
1458 ASSERT_TRUE(pfd
.revents
& POLLIN
);
1460 rbd_completion_t comps
[1];
1461 ASSERT_EQ(1, rbd_poll_io_events(image
, comps
, 1));
1463 ASSERT_EQ(static_cast<ssize_t
>(sizeof(count
)),
1464 read(fd
, &count
, sizeof(count
)));
1466 int r
= rbd_aio_get_return_value(comps
[0]);
1467 ASSERT_TRUE(rbd_aio_is_complete(comps
[0]));
1468 printf("return value is: %d\n", r
);
1469 ASSERT_EQ(len
, static_cast<size_t>(r
));
1470 rbd_aio_release(comps
[0]);
1471 if (memcmp(result
, expected
, len
)) {
1472 printf("read: %s\nexpected: %s\n", result
, expected
);
1473 ASSERT_EQ(0, memcmp(result
, expected
, len
));
1479 void aio_read_test_data(rbd_image_t image
, const char *expected
, uint64_t off
, size_t len
, uint32_t iohint
, bool *passed
)
1481 rbd_completion_t comp
;
1482 char *result
= (char *)malloc(len
+ 1);
1484 ASSERT_NE(static_cast<char *>(NULL
), result
);
1485 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1486 printf("created completion\n");
1488 rbd_aio_read2(image
, off
, len
, result
, comp
, iohint
);
1490 rbd_aio_read(image
, off
, len
, result
, comp
);
1491 printf("started read\n");
1492 rbd_aio_wait_for_complete(comp
);
1493 int r
= rbd_aio_get_return_value(comp
);
1494 printf("return value is: %d\n", r
);
1495 ASSERT_EQ(len
, static_cast<size_t>(r
));
1496 rbd_aio_release(comp
);
1497 if (memcmp(result
, expected
, len
)) {
1498 printf("read: %s\nexpected: %s\n", result
, expected
);
1499 ASSERT_EQ(0, memcmp(result
, expected
, len
));
1505 void read_test_data(rbd_image_t image
, const char *expected
, uint64_t off
, size_t len
, uint32_t iohint
, bool *passed
)
1508 char *result
= (char *)malloc(len
+ 1);
1510 ASSERT_NE(static_cast<char *>(NULL
), result
);
1512 read
= rbd_read2(image
, off
, len
, result
, iohint
);
1514 read
= rbd_read(image
, off
, len
, result
);
1515 printf("read: %d\n", (int) read
);
1516 ASSERT_EQ(len
, static_cast<size_t>(read
));
1518 if (memcmp(result
, expected
, len
)) {
1519 printf("read: %s\nexpected: %s\n", result
, expected
);
1520 ASSERT_EQ(0, memcmp(result
, expected
, len
));
1526 void aio_writesame_test_data(rbd_image_t image
, const char *test_data
, uint64_t off
, uint64_t len
,
1527 uint64_t data_len
, uint32_t iohint
, bool *passed
)
1529 rbd_completion_t comp
;
1530 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_write_cb
, &comp
);
1531 printf("created completion\n");
1533 r
= rbd_aio_writesame(image
, off
, len
, test_data
, data_len
, comp
, iohint
);
1534 printf("started writesame\n");
1535 if (len
% data_len
) {
1536 ASSERT_EQ(-EINVAL
, r
);
1537 printf("expected fail, finished writesame\n");
1538 rbd_aio_release(comp
);
1543 rbd_aio_wait_for_complete(comp
);
1544 r
= rbd_aio_get_return_value(comp
);
1545 printf("return value is: %d\n", r
);
1547 printf("finished writesame\n");
1548 rbd_aio_release(comp
);
1551 printf("to verify the data\n");
1553 char *result
= (char *)malloc(data_len
+ 1);
1554 ASSERT_NE(static_cast<char *>(NULL
), result
);
1555 uint64_t left
= len
;
1557 read
= rbd_read(image
, off
, data_len
, result
);
1558 ASSERT_EQ(data_len
, static_cast<size_t>(read
));
1559 result
[data_len
] = '\0';
1560 if (memcmp(result
, test_data
, data_len
)) {
1561 printf("read: %d ~ %d\n", (int) off
, (int) read
);
1562 printf("read: %s\nexpected: %s\n", result
, test_data
);
1563 ASSERT_EQ(0, memcmp(result
, test_data
, data_len
));
1568 ASSERT_EQ(0U, left
);
1570 printf("verified\n");
1575 void writesame_test_data(rbd_image_t image
, const char *test_data
, uint64_t off
, uint64_t len
,
1576 uint64_t data_len
, uint32_t iohint
, bool *passed
)
1579 written
= rbd_writesame(image
, off
, len
, test_data
, data_len
, iohint
);
1580 if (len
% data_len
) {
1581 ASSERT_EQ(-EINVAL
, written
);
1582 printf("expected fail, finished writesame\n");
1586 ASSERT_EQ(len
, static_cast<size_t>(written
));
1587 printf("wrote: %d\n", (int) written
);
1590 printf("to verify the data\n");
1592 char *result
= (char *)malloc(data_len
+ 1);
1593 ASSERT_NE(static_cast<char *>(NULL
), result
);
1594 uint64_t left
= len
;
1596 read
= rbd_read(image
, off
, data_len
, result
);
1597 ASSERT_EQ(data_len
, static_cast<size_t>(read
));
1598 result
[data_len
] = '\0';
1599 if (memcmp(result
, test_data
, data_len
)) {
1600 printf("read: %d ~ %d\n", (int) off
, (int) read
);
1601 printf("read: %s\nexpected: %s\n", result
, test_data
);
1602 ASSERT_EQ(0, memcmp(result
, test_data
, data_len
));
1607 ASSERT_EQ(0U, left
);
1609 printf("verified\n");
1614 void aio_compare_and_write_test_data(rbd_image_t image
, const char *cmp_data
,
1615 const char *test_data
, uint64_t off
,
1616 size_t len
, uint32_t iohint
, bool *passed
)
1618 rbd_completion_t comp
;
1619 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_write_cb
, &comp
);
1620 printf("created completion\n");
1622 uint64_t mismatch_offset
;
1623 rbd_aio_compare_and_write(image
, off
, len
, cmp_data
, test_data
, comp
, &mismatch_offset
, iohint
);
1624 printf("started aio compare and write\n");
1625 rbd_aio_wait_for_complete(comp
);
1626 int r
= rbd_aio_get_return_value(comp
);
1627 printf("return value is: %d\n", r
);
1629 printf("finished aio compare and write\n");
1630 rbd_aio_release(comp
);
1634 void compare_and_write_test_data(rbd_image_t image
, const char *cmp_data
,
1635 const char *test_data
, uint64_t off
, size_t len
,
1636 uint64_t *mismatch_off
, uint32_t iohint
, bool *passed
)
1638 printf("start compare and write\n");
1640 written
= rbd_compare_and_write(image
, off
, len
, cmp_data
, test_data
, mismatch_off
, iohint
);
1641 printf("compare and wrote: %d\n", (int) written
);
1642 ASSERT_EQ(len
, static_cast<size_t>(written
));
1647 TEST_F(TestLibRBD
, TestIO
)
1649 rados_ioctx_t ioctx
;
1650 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
1652 bool skip_discard
= is_skip_partial_discard_enabled();
1656 std::string name
= get_temp_image_name();
1657 uint64_t size
= 2 << 20;
1659 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
1660 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
1662 char test_data
[TEST_IO_SIZE
+ 1];
1663 char zero_data
[TEST_IO_SIZE
+ 1];
1664 char mismatch_data
[TEST_IO_SIZE
+ 1];
1666 uint64_t mismatch_offset
;
1668 for (i
= 0; i
< TEST_IO_SIZE
; ++i
) {
1669 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
1671 test_data
[TEST_IO_SIZE
] = '\0';
1672 memset(zero_data
, 0, sizeof(zero_data
));
1673 memset(mismatch_data
, 9, sizeof(mismatch_data
));
1675 for (i
= 0; i
< 5; ++i
)
1676 ASSERT_PASSED(write_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1678 for (i
= 5; i
< 10; ++i
)
1679 ASSERT_PASSED(aio_write_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1681 for (i
= 0; i
< 5; ++i
)
1682 ASSERT_PASSED(compare_and_write_test_data
, image
, test_data
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, &mismatch_offset
, 0);
1684 for (i
= 5; i
< 10; ++i
)
1685 ASSERT_PASSED(aio_compare_and_write_test_data
, image
, test_data
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1687 for (i
= 0; i
< 5; ++i
)
1688 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1690 for (i
= 5; i
< 10; ++i
)
1691 ASSERT_PASSED(aio_read_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1693 // discard 2nd, 4th sections.
1694 ASSERT_PASSED(discard_test_data
, image
, TEST_IO_SIZE
, TEST_IO_SIZE
);
1695 ASSERT_PASSED(aio_discard_test_data
, image
, TEST_IO_SIZE
*3, TEST_IO_SIZE
);
1697 ASSERT_PASSED(read_test_data
, image
, test_data
, 0, TEST_IO_SIZE
, 0);
1698 ASSERT_PASSED(read_test_data
, image
, skip_discard
? test_data
: zero_data
,
1699 TEST_IO_SIZE
, TEST_IO_SIZE
, 0);
1700 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
*2, TEST_IO_SIZE
, 0);
1701 ASSERT_PASSED(read_test_data
, image
, skip_discard
? test_data
: zero_data
,
1702 TEST_IO_SIZE
*3, TEST_IO_SIZE
, 0);
1703 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
*4, TEST_IO_SIZE
, 0);
1705 for (i
= 0; i
< 15; ++i
) {
1707 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, 0);
1708 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, 0);
1709 } else if (i
% 3 == 1) {
1710 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
1711 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
1713 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
1714 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
1717 for (i
= 0; i
< 15; ++i
) {
1719 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, 0);
1720 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, 0);
1721 } else if (i
% 3 == 1) {
1722 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
1723 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
1725 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
1726 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
1730 rbd_image_info_t info
;
1731 rbd_completion_t comp
;
1732 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
1733 // can't read or write starting past end
1734 ASSERT_EQ(-EINVAL
, rbd_write(image
, info
.size
, 1, test_data
));
1735 ASSERT_EQ(-EINVAL
, rbd_read(image
, info
.size
, 1, test_data
));
1736 // reading through end returns amount up to end
1737 ASSERT_EQ(10, rbd_read(image
, info
.size
- 10, 100, test_data
));
1738 // writing through end returns amount up to end
1739 ASSERT_EQ(10, rbd_write(image
, info
.size
- 10, 100, test_data
));
1741 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1742 ASSERT_EQ(0, rbd_aio_write(image
, info
.size
, 1, test_data
, comp
));
1743 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
1744 ASSERT_EQ(-EINVAL
, rbd_aio_get_return_value(comp
));
1745 rbd_aio_release(comp
);
1747 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1748 ASSERT_EQ(0, rbd_aio_read(image
, info
.size
, 1, test_data
, comp
));
1749 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
1750 ASSERT_EQ(-EINVAL
, rbd_aio_get_return_value(comp
));
1751 rbd_aio_release(comp
);
1753 ASSERT_PASSED(write_test_data
, image
, zero_data
, 0, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1754 ASSERT_EQ(-EILSEQ
, rbd_compare_and_write(image
, 0, TEST_IO_SIZE
, mismatch_data
, mismatch_data
, &mismatch_offset
, 0));
1755 ASSERT_EQ(0U, mismatch_offset
);
1756 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1757 ASSERT_EQ(0, rbd_aio_compare_and_write(image
, 0, TEST_IO_SIZE
, mismatch_data
, mismatch_data
, comp
, &mismatch_offset
, 0));
1758 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
1759 ASSERT_EQ(0U, mismatch_offset
);
1760 rbd_aio_release(comp
);
1762 ASSERT_PASSED(validate_object_map
, image
);
1763 ASSERT_EQ(0, rbd_close(image
));
1765 rados_ioctx_destroy(ioctx
);
1768 TEST_F(TestLibRBD
, TestIOWithIOHint
)
1770 rados_ioctx_t ioctx
;
1771 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
1773 bool skip_discard
= is_skip_partial_discard_enabled();
1777 std::string name
= get_temp_image_name();
1778 uint64_t size
= 2 << 20;
1780 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
1781 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
1783 char test_data
[TEST_IO_SIZE
+ 1];
1784 char zero_data
[TEST_IO_SIZE
+ 1];
1785 char mismatch_data
[TEST_IO_SIZE
+ 1];
1787 uint64_t mismatch_offset
;
1789 for (i
= 0; i
< TEST_IO_SIZE
; ++i
) {
1790 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
1792 test_data
[TEST_IO_SIZE
] = '\0';
1793 memset(zero_data
, 0, sizeof(zero_data
));
1794 memset(mismatch_data
, 9, sizeof(mismatch_data
));
1796 for (i
= 0; i
< 5; ++i
)
1797 ASSERT_PASSED(write_test_data
, image
, test_data
, TEST_IO_SIZE
* i
,
1798 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1800 for (i
= 5; i
< 10; ++i
)
1801 ASSERT_PASSED(aio_write_test_data
, image
, test_data
, TEST_IO_SIZE
* i
,
1802 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1804 for (i
= 0; i
< 5; ++i
)
1805 ASSERT_PASSED(compare_and_write_test_data
, image
, test_data
, test_data
,
1806 TEST_IO_SIZE
* i
, TEST_IO_SIZE
, &mismatch_offset
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1808 for (i
= 5; i
< 10; ++i
)
1809 ASSERT_PASSED(aio_compare_and_write_test_data
, image
, test_data
, test_data
,
1810 TEST_IO_SIZE
* i
, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1812 for (i
= 0; i
< 5; ++i
)
1813 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
,
1814 LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
);
1816 for (i
= 5; i
< 10; ++i
)
1817 ASSERT_PASSED(aio_read_test_data
, image
, test_data
, TEST_IO_SIZE
* i
,
1818 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
|LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1820 // discard 2nd, 4th sections.
1821 ASSERT_PASSED(discard_test_data
, image
, TEST_IO_SIZE
, TEST_IO_SIZE
);
1822 ASSERT_PASSED(aio_discard_test_data
, image
, TEST_IO_SIZE
*3, TEST_IO_SIZE
);
1824 ASSERT_PASSED(read_test_data
, image
, test_data
, 0, TEST_IO_SIZE
,
1825 LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
);
1826 ASSERT_PASSED(read_test_data
, image
, skip_discard
? test_data
: zero_data
,
1827 TEST_IO_SIZE
, TEST_IO_SIZE
,
1828 LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
);
1829 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
*2, TEST_IO_SIZE
,
1830 LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
);
1831 ASSERT_PASSED(read_test_data
, image
, skip_discard
? test_data
: zero_data
,
1832 TEST_IO_SIZE
*3, TEST_IO_SIZE
,
1833 LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
);
1834 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
*4, TEST_IO_SIZE
, 0);
1836 for (i
= 0; i
< 15; ++i
) {
1838 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
,
1839 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1840 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
,
1841 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1842 } else if (i
% 3 == 1) {
1843 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32,
1844 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1845 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32,
1846 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1848 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32,
1849 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1850 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32,
1851 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1854 for (i
= 0; i
< 15; ++i
) {
1856 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
,
1857 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1858 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
,
1859 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1860 } else if (i
% 3 == 1) {
1861 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32,
1862 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1863 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32,
1864 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1866 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32,
1867 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1868 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32,
1869 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
1873 rbd_image_info_t info
;
1874 rbd_completion_t comp
;
1875 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
1876 // can't read or write starting past end
1877 ASSERT_EQ(-EINVAL
, rbd_write(image
, info
.size
, 1, test_data
));
1878 ASSERT_EQ(-EINVAL
, rbd_read(image
, info
.size
, 1, test_data
));
1879 // reading through end returns amount up to end
1880 ASSERT_EQ(10, rbd_read2(image
, info
.size
- 10, 100, test_data
,
1881 LIBRADOS_OP_FLAG_FADVISE_NOCACHE
));
1882 // writing through end returns amount up to end
1883 ASSERT_EQ(10, rbd_write2(image
, info
.size
- 10, 100, test_data
,
1884 LIBRADOS_OP_FLAG_FADVISE_DONTNEED
));
1886 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1887 ASSERT_EQ(0, rbd_aio_read2(image
, info
.size
, 1, test_data
, comp
,
1888 LIBRADOS_OP_FLAG_FADVISE_DONTNEED
));
1889 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
1890 ASSERT_EQ(-EINVAL
, rbd_aio_get_return_value(comp
));
1891 rbd_aio_release(comp
);
1893 ASSERT_PASSED(write_test_data
, image
, zero_data
, 0, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
1894 ASSERT_EQ(-EILSEQ
, rbd_compare_and_write(image
, 0, TEST_IO_SIZE
, mismatch_data
, mismatch_data
,
1895 &mismatch_offset
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
));
1896 ASSERT_EQ(0U, mismatch_offset
);
1897 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1898 ASSERT_EQ(0, rbd_aio_compare_and_write(image
, 0, TEST_IO_SIZE
, mismatch_data
, mismatch_data
,
1899 comp
, &mismatch_offset
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
));
1900 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
1901 ASSERT_EQ(0U, mismatch_offset
);
1902 rbd_aio_release(comp
);
1904 ASSERT_PASSED(validate_object_map
, image
);
1905 ASSERT_EQ(0, rbd_close(image
));
1907 rados_ioctx_destroy(ioctx
);
1910 TEST_F(TestLibRBD
, TestDataPoolIO
)
1912 REQUIRE_FORMAT_V2();
1914 rados_ioctx_t ioctx
;
1915 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
1917 std::string data_pool_name
= create_pool(true);
1919 bool skip_discard
= is_skip_partial_discard_enabled();
1922 std::string name
= get_temp_image_name();
1923 uint64_t size
= 2 << 20;
1927 ASSERT_EQ(0, get_features(&old_format
, &features
));
1928 ASSERT_FALSE(old_format
);
1930 rbd_image_options_t image_options
;
1931 rbd_image_options_create(&image_options
);
1932 BOOST_SCOPE_EXIT( (&image_options
) ) {
1933 rbd_image_options_destroy(image_options
);
1934 } BOOST_SCOPE_EXIT_END
;
1936 ASSERT_EQ(0, rbd_image_options_set_uint64(image_options
,
1937 RBD_IMAGE_OPTION_FEATURES
,
1939 ASSERT_EQ(0, rbd_image_options_set_string(image_options
,
1940 RBD_IMAGE_OPTION_DATA_POOL
,
1941 data_pool_name
.c_str()));
1943 ASSERT_EQ(0, rbd_create4(ioctx
, name
.c_str(), size
, image_options
));
1944 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
1945 ASSERT_NE(-1, rbd_get_data_pool_id(image
));
1947 char test_data
[TEST_IO_SIZE
+ 1];
1948 char zero_data
[TEST_IO_SIZE
+ 1];
1951 for (i
= 0; i
< TEST_IO_SIZE
; ++i
) {
1952 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
1954 test_data
[TEST_IO_SIZE
] = '\0';
1955 memset(zero_data
, 0, sizeof(zero_data
));
1957 for (i
= 0; i
< 5; ++i
)
1958 ASSERT_PASSED(write_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1960 for (i
= 5; i
< 10; ++i
)
1961 ASSERT_PASSED(aio_write_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1963 for (i
= 0; i
< 5; ++i
)
1964 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1966 for (i
= 5; i
< 10; ++i
)
1967 ASSERT_PASSED(aio_read_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
1969 // discard 2nd, 4th sections.
1970 ASSERT_PASSED(discard_test_data
, image
, TEST_IO_SIZE
, TEST_IO_SIZE
);
1971 ASSERT_PASSED(aio_discard_test_data
, image
, TEST_IO_SIZE
*3, TEST_IO_SIZE
);
1973 ASSERT_PASSED(read_test_data
, image
, test_data
, 0, TEST_IO_SIZE
, 0);
1974 ASSERT_PASSED(read_test_data
, image
, skip_discard
? test_data
: zero_data
,
1975 TEST_IO_SIZE
, TEST_IO_SIZE
, 0);
1976 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
*2, TEST_IO_SIZE
, 0);
1977 ASSERT_PASSED(read_test_data
, image
, skip_discard
? test_data
: zero_data
,
1978 TEST_IO_SIZE
*3, TEST_IO_SIZE
, 0);
1979 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
*4, TEST_IO_SIZE
, 0);
1981 rbd_image_info_t info
;
1982 rbd_completion_t comp
;
1983 ASSERT_EQ(0, rbd_stat(image
, &info
, sizeof(info
)));
1984 // can't read or write starting past end
1985 ASSERT_EQ(-EINVAL
, rbd_write(image
, info
.size
, 1, test_data
));
1986 ASSERT_EQ(-EINVAL
, rbd_read(image
, info
.size
, 1, test_data
));
1987 // reading through end returns amount up to end
1988 ASSERT_EQ(10, rbd_read(image
, info
.size
- 10, 100, test_data
));
1989 // writing through end returns amount up to end
1990 ASSERT_EQ(10, rbd_write(image
, info
.size
- 10, 100, test_data
));
1992 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1993 ASSERT_EQ(0, rbd_aio_write(image
, info
.size
, 1, test_data
, comp
));
1994 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
1995 ASSERT_EQ(-EINVAL
, rbd_aio_get_return_value(comp
));
1996 rbd_aio_release(comp
);
1998 rbd_aio_create_completion(NULL
, (rbd_callback_t
) simple_read_cb
, &comp
);
1999 ASSERT_EQ(0, rbd_aio_read(image
, info
.size
, 1, test_data
, comp
));
2000 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
2001 ASSERT_EQ(-EINVAL
, rbd_aio_get_return_value(comp
));
2002 rbd_aio_release(comp
);
2004 ASSERT_PASSED(validate_object_map
, image
);
2005 ASSERT_EQ(0, rbd_close(image
));
2007 rados_ioctx_destroy(ioctx
);
2010 TEST_F(TestLibRBD
, TestScatterGatherIO
)
2012 rados_ioctx_t ioctx
;
2013 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
2017 std::string name
= get_temp_image_name();
2018 uint64_t size
= 20 << 20;
2020 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
2021 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
2023 std::string
write_buffer("This is a test");
2024 struct iovec bad_iovs
[] = {
2025 {.iov_base
= NULL
, .iov_len
= static_cast<size_t>(-1)}
2027 struct iovec write_iovs
[] = {
2028 {.iov_base
= &write_buffer
[0], .iov_len
= 5},
2029 {.iov_base
= &write_buffer
[5], .iov_len
= 3},
2030 {.iov_base
= &write_buffer
[8], .iov_len
= 2},
2031 {.iov_base
= &write_buffer
[10], .iov_len
= 4}
2034 rbd_completion_t comp
;
2035 rbd_aio_create_completion(NULL
, NULL
, &comp
);
2036 ASSERT_EQ(-EINVAL
, rbd_aio_writev(image
, write_iovs
, 0, 0, comp
));
2037 ASSERT_EQ(-EINVAL
, rbd_aio_writev(image
, bad_iovs
, 1, 0, comp
));
2038 ASSERT_EQ(0, rbd_aio_writev(image
, write_iovs
,
2039 sizeof(write_iovs
) / sizeof(struct iovec
),
2041 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
2042 ASSERT_EQ(0, rbd_aio_get_return_value(comp
));
2043 rbd_aio_release(comp
);
2045 std::string
read_buffer(write_buffer
.size(), '1');
2046 struct iovec read_iovs
[] = {
2047 {.iov_base
= &read_buffer
[0], .iov_len
= 4},
2048 {.iov_base
= &read_buffer
[8], .iov_len
= 4},
2049 {.iov_base
= &read_buffer
[12], .iov_len
= 2}
2052 rbd_aio_create_completion(NULL
, NULL
, &comp
);
2053 ASSERT_EQ(-EINVAL
, rbd_aio_readv(image
, read_iovs
, 0, 0, comp
));
2054 ASSERT_EQ(-EINVAL
, rbd_aio_readv(image
, bad_iovs
, 1, 0, comp
));
2055 ASSERT_EQ(0, rbd_aio_readv(image
, read_iovs
,
2056 sizeof(read_iovs
) / sizeof(struct iovec
),
2058 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
2059 ASSERT_EQ(10, rbd_aio_get_return_value(comp
));
2060 rbd_aio_release(comp
);
2061 ASSERT_EQ("This1111 is a ", read_buffer
);
2063 std::string
linear_buffer(write_buffer
.size(), '1');
2064 struct iovec linear_iovs
[] = {
2065 {.iov_base
= &linear_buffer
[4], .iov_len
= 4}
2067 rbd_aio_create_completion(NULL
, NULL
, &comp
);
2068 ASSERT_EQ(0, rbd_aio_readv(image
, linear_iovs
,
2069 sizeof(linear_iovs
) / sizeof(struct iovec
),
2071 ASSERT_EQ(0, rbd_aio_wait_for_complete(comp
));
2072 ASSERT_EQ(4, rbd_aio_get_return_value(comp
));
2073 rbd_aio_release(comp
);
2074 ASSERT_EQ("1111This111111", linear_buffer
);
2076 ASSERT_PASSED(validate_object_map
, image
);
2077 ASSERT_EQ(0, rbd_close(image
));
2079 rados_ioctx_destroy(ioctx
);
2082 TEST_F(TestLibRBD
, TestEmptyDiscard
)
2084 rados_ioctx_t ioctx
;
2085 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
2089 std::string name
= get_temp_image_name();
2090 uint64_t size
= 20 << 20;
2092 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
2093 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
2095 ASSERT_PASSED(aio_discard_test_data
, image
, 0, 1*1024*1024);
2096 ASSERT_PASSED(aio_discard_test_data
, image
, 0, 4*1024*1024);
2097 ASSERT_PASSED(aio_discard_test_data
, image
, 3*1024*1024, 1*1024*1024);
2099 ASSERT_PASSED(validate_object_map
, image
);
2100 ASSERT_EQ(0, rbd_close(image
));
2102 rados_ioctx_destroy(ioctx
);
2106 void simple_write_cb_pp(librbd::completion_t cb
, void *arg
)
2108 cout
<< "write completion cb called!" << std::endl
;
2111 void simple_read_cb_pp(librbd::completion_t cb
, void *arg
)
2113 cout
<< "read completion cb called!" << std::endl
;
2116 void aio_write_test_data(librbd::Image
& image
, const char *test_data
,
2117 off_t off
, uint32_t iohint
, bool *passed
)
2119 ceph::bufferlist bl
;
2120 bl
.append(test_data
, strlen(test_data
));
2121 librbd::RBD::AioCompletion
*comp
= new librbd::RBD::AioCompletion(NULL
, (librbd::callback_t
) simple_write_cb_pp
);
2122 printf("created completion\n");
2124 image
.aio_write2(off
, strlen(test_data
), bl
, comp
, iohint
);
2126 image
.aio_write(off
, strlen(test_data
), bl
, comp
);
2127 printf("started write\n");
2128 comp
->wait_for_complete();
2129 int r
= comp
->get_return_value();
2130 printf("return value is: %d\n", r
);
2132 printf("finished write\n");
2137 void aio_discard_test_data(librbd::Image
& image
, off_t off
, size_t len
, bool *passed
)
2139 librbd::RBD::AioCompletion
*comp
= new librbd::RBD::AioCompletion(NULL
, (librbd::callback_t
) simple_write_cb_pp
);
2140 image
.aio_discard(off
, len
, comp
);
2141 comp
->wait_for_complete();
2142 int r
= comp
->get_return_value();
2148 void write_test_data(librbd::Image
& image
, const char *test_data
, off_t off
, uint32_t iohint
, bool *passed
)
2151 size_t len
= strlen(test_data
);
2152 ceph::bufferlist bl
;
2153 bl
.append(test_data
, len
);
2155 written
= image
.write2(off
, len
, bl
, iohint
);
2157 written
= image
.write(off
, len
, bl
);
2158 printf("wrote: %u\n", (unsigned int) written
);
2159 ASSERT_EQ(bl
.length(), written
);
2163 void discard_test_data(librbd::Image
& image
, off_t off
, size_t len
, bool *passed
)
2166 written
= image
.discard(off
, len
);
2167 printf("discard: %u~%u\n", (unsigned)off
, (unsigned)len
);
2168 ASSERT_EQ(len
, written
);
2172 void aio_read_test_data(librbd::Image
& image
, const char *expected
, off_t off
, size_t expected_len
, uint32_t iohint
, bool *passed
)
2174 librbd::RBD::AioCompletion
*comp
= new librbd::RBD::AioCompletion(NULL
, (librbd::callback_t
) simple_read_cb_pp
);
2175 ceph::bufferlist bl
;
2176 printf("created completion\n");
2178 image
.aio_read2(off
, expected_len
, bl
, comp
, iohint
);
2180 image
.aio_read(off
, expected_len
, bl
, comp
);
2181 printf("started read\n");
2182 comp
->wait_for_complete();
2183 int r
= comp
->get_return_value();
2184 printf("return value is: %d\n", r
);
2185 ASSERT_EQ(TEST_IO_SIZE
, r
);
2186 ASSERT_EQ(0, memcmp(expected
, bl
.c_str(), TEST_IO_SIZE
));
2187 printf("finished read\n");
2192 void read_test_data(librbd::Image
& image
, const char *expected
, off_t off
, size_t expected_len
, uint32_t iohint
, bool *passed
)
2195 size_t len
= expected_len
;
2196 ceph::bufferlist bl
;
2198 read
= image
.read2(off
, len
, bl
, iohint
);
2200 read
= image
.read(off
, len
, bl
);
2201 ASSERT_TRUE(read
>= 0);
2202 std::string
bl_str(bl
.c_str(), read
);
2204 printf("read: %u\n", (unsigned int) read
);
2205 int result
= memcmp(bl_str
.c_str(), expected
, expected_len
);
2207 printf("read: %s\nexpected: %s\n", bl_str
.c_str(), expected
);
2208 ASSERT_EQ(0, result
);
2213 void aio_writesame_test_data(librbd::Image
& image
, const char *test_data
, off_t off
,
2214 size_t len
, size_t data_len
, uint32_t iohint
, bool *passed
)
2216 ceph::bufferlist bl
;
2217 bl
.append(test_data
, data_len
);
2218 librbd::RBD::AioCompletion
*comp
= new librbd::RBD::AioCompletion(NULL
, (librbd::callback_t
) simple_write_cb_pp
);
2219 printf("created completion\n");
2221 r
= image
.aio_writesame(off
, len
, bl
, comp
, iohint
);
2222 printf("started writesame\n");
2223 if (len
% data_len
) {
2224 ASSERT_EQ(-EINVAL
, r
);
2225 printf("expected fail, finished writesame\n");
2231 comp
->wait_for_complete();
2232 r
= comp
->get_return_value();
2233 printf("return value is: %d\n", r
);
2235 printf("finished writesame\n");
2239 printf("to verify the data\n");
2241 uint64_t left
= len
;
2243 ceph::bufferlist bl
;
2244 read
= image
.read(off
, data_len
, bl
);
2245 ASSERT_EQ(data_len
, static_cast<size_t>(read
));
2246 std::string
bl_str(bl
.c_str(), read
);
2247 int result
= memcmp(bl_str
.c_str(), test_data
, data_len
);
2249 printf("read: %u ~ %u\n", (unsigned int) off
, (unsigned int) read
);
2250 printf("read: %s\nexpected: %s\n", bl_str
.c_str(), test_data
);
2251 ASSERT_EQ(0, result
);
2256 ASSERT_EQ(0U, left
);
2257 printf("verified\n");
2262 void writesame_test_data(librbd::Image
& image
, const char *test_data
, off_t off
,
2263 ssize_t len
, size_t data_len
, uint32_t iohint
,
2267 ceph::bufferlist bl
;
2268 bl
.append(test_data
, data_len
);
2269 written
= image
.writesame(off
, len
, bl
, iohint
);
2270 if (len
% data_len
) {
2271 ASSERT_EQ(-EINVAL
, written
);
2272 printf("expected fail, finished writesame\n");
2276 ASSERT_EQ(len
, written
);
2277 printf("wrote: %u\n", (unsigned int) written
);
2281 printf("to verify the data\n");
2283 uint64_t left
= len
;
2285 ceph::bufferlist bl
;
2286 read
= image
.read(off
, data_len
, bl
);
2287 ASSERT_EQ(data_len
, static_cast<size_t>(read
));
2288 std::string
bl_str(bl
.c_str(), read
);
2289 int result
= memcmp(bl_str
.c_str(), test_data
, data_len
);
2291 printf("read: %u ~ %u\n", (unsigned int) off
, (unsigned int) read
);
2292 printf("read: %s\nexpected: %s\n", bl_str
.c_str(), test_data
);
2293 ASSERT_EQ(0, result
);
2298 ASSERT_EQ(0U, left
);
2299 printf("verified\n");
2304 void aio_compare_and_write_test_data(librbd::Image
& image
, const char *cmp_data
,
2305 const char *test_data
, off_t off
, ssize_t len
,
2306 uint32_t iohint
, bool *passed
)
2308 ceph::bufferlist cmp_bl
;
2309 cmp_bl
.append(cmp_data
, strlen(cmp_data
));
2310 ceph::bufferlist test_bl
;
2311 test_bl
.append(test_data
, strlen(test_data
));
2312 librbd::RBD::AioCompletion
*comp
= new librbd::RBD::AioCompletion(NULL
, (librbd::callback_t
) simple_write_cb_pp
);
2313 printf("created completion\n");
2315 uint64_t mismatch_offset
;
2316 image
.aio_compare_and_write(off
, len
, cmp_bl
, test_bl
, comp
, &mismatch_offset
, iohint
);
2317 printf("started aio compare and write\n");
2318 comp
->wait_for_complete();
2319 int r
= comp
->get_return_value();
2320 printf("return value is: %d\n", r
);
2322 printf("finished aio compare and write\n");
2327 void compare_and_write_test_data(librbd::Image
& image
, const char *cmp_data
, const char *test_data
,
2328 off_t off
, ssize_t len
, uint64_t *mismatch_off
, uint32_t iohint
, bool *passed
)
2331 ceph::bufferlist cmp_bl
;
2332 cmp_bl
.append(cmp_data
, strlen(cmp_data
));
2333 ceph::bufferlist test_bl
;
2334 test_bl
.append(test_data
, strlen(test_data
));
2335 printf("start compare and write\n");
2336 written
= image
.compare_and_write(off
, len
, cmp_bl
, test_bl
, mismatch_off
, iohint
);
2337 printf("compare and wrote: %d\n", (int) written
);
2338 ASSERT_EQ(len
, static_cast<ssize_t
>(written
));
2342 TEST_F(TestLibRBD
, TestIOPP
)
2344 librados::IoCtx ioctx
;
2345 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
2347 bool skip_discard
= is_skip_partial_discard_enabled();
2351 librbd::Image image
;
2353 std::string name
= get_temp_image_name();
2354 uint64_t size
= 2 << 20;
2356 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
2357 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
2359 char test_data
[TEST_IO_SIZE
+ 1];
2360 char zero_data
[TEST_IO_SIZE
+ 1];
2362 uint64_t mismatch_offset
;
2364 for (i
= 0; i
< TEST_IO_SIZE
; ++i
) {
2365 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
2367 test_data
[TEST_IO_SIZE
] = '\0';
2368 memset(zero_data
, 0, sizeof(zero_data
));
2370 for (i
= 0; i
< 5; ++i
)
2371 ASSERT_PASSED(write_test_data
, image
, test_data
, strlen(test_data
) * i
, 0);
2373 for (i
= 5; i
< 10; ++i
)
2374 ASSERT_PASSED(aio_write_test_data
, image
, test_data
, strlen(test_data
) * i
, 0);
2376 for (i
= 0; i
< 5; ++i
)
2377 ASSERT_PASSED(compare_and_write_test_data
, image
, test_data
, test_data
, TEST_IO_SIZE
* i
,
2378 TEST_IO_SIZE
, &mismatch_offset
, 0);
2380 for (i
= 5; i
< 10; ++i
)
2381 ASSERT_PASSED(aio_compare_and_write_test_data
, image
, test_data
, test_data
, TEST_IO_SIZE
* i
,
2384 for (i
= 0; i
< 5; ++i
)
2385 ASSERT_PASSED(read_test_data
, image
, test_data
, strlen(test_data
) * i
, TEST_IO_SIZE
, 0);
2387 for (i
= 5; i
< 10; ++i
)
2388 ASSERT_PASSED(aio_read_test_data
, image
, test_data
, strlen(test_data
) * i
, TEST_IO_SIZE
, 0);
2390 // discard 2nd, 4th sections.
2391 ASSERT_PASSED(discard_test_data
, image
, TEST_IO_SIZE
, TEST_IO_SIZE
);
2392 ASSERT_PASSED(aio_discard_test_data
, image
, TEST_IO_SIZE
*3, TEST_IO_SIZE
);
2394 ASSERT_PASSED(read_test_data
, image
, test_data
, 0, TEST_IO_SIZE
, 0);
2395 ASSERT_PASSED(read_test_data
, image
, skip_discard
? test_data
: zero_data
,
2396 TEST_IO_SIZE
, TEST_IO_SIZE
, 0);
2397 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
*2, TEST_IO_SIZE
, 0);
2398 ASSERT_PASSED(read_test_data
, image
, skip_discard
? test_data
: zero_data
,
2399 TEST_IO_SIZE
*3, TEST_IO_SIZE
, 0);
2400 ASSERT_PASSED(read_test_data
, image
, test_data
, TEST_IO_SIZE
*4, TEST_IO_SIZE
, 0);
2402 for (i
= 0; i
< 15; ++i
) {
2404 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, 0);
2405 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, 0);
2406 } else if (i
% 3 == 1) {
2407 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
2408 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
2410 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
2411 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
2414 for (i
= 0; i
< 15; ++i
) {
2416 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, 0);
2417 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, 0);
2418 } else if (i
% 3 == 1) {
2419 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
2420 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
+ i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
2422 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
2423 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, 0);
2427 ASSERT_PASSED(validate_object_map
, image
);
2433 TEST_F(TestLibRBD
, TestIOPPWithIOHint
)
2435 librados::IoCtx ioctx
;
2436 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
2440 librbd::Image image
;
2442 std::string name
= get_temp_image_name();
2443 uint64_t size
= 2 << 20;
2445 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
2446 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
2448 char test_data
[TEST_IO_SIZE
+ 1];
2449 char zero_data
[TEST_IO_SIZE
+ 1];
2450 test_data
[TEST_IO_SIZE
] = '\0';
2453 for (i
= 0; i
< TEST_IO_SIZE
; ++i
) {
2454 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
2456 memset(zero_data
, 0, sizeof(zero_data
));
2458 for (i
= 0; i
< 5; ++i
)
2459 ASSERT_PASSED(write_test_data
, image
, test_data
, strlen(test_data
) * i
,
2460 LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
2462 for (i
= 5; i
< 10; ++i
)
2463 ASSERT_PASSED(aio_write_test_data
, image
, test_data
, strlen(test_data
) * i
,
2464 LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
2466 ASSERT_PASSED(read_test_data
, image
, test_data
, strlen(test_data
),
2467 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_RANDOM
);
2469 for (i
= 5; i
< 10; ++i
)
2470 ASSERT_PASSED(aio_read_test_data
, image
, test_data
, strlen(test_data
) * i
,
2471 TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
|LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
2473 for (i
= 0; i
< 15; ++i
) {
2475 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
,
2476 TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
2477 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
,
2478 TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
2479 } else if (i
% 3 == 1) {
2480 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
+ i
,
2481 TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
2482 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
+ i
,
2483 TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
2485 ASSERT_PASSED(writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
,
2486 TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
2487 ASSERT_PASSED(writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
,
2488 TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
);
2491 for (i
= 0; i
< 15; ++i
) {
2493 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
,
2494 TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
2495 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
,
2496 TEST_IO_SIZE
* i
* 32 + i
, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
2497 } else if (i
% 3 == 1) {
2498 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
+ i
,
2499 TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
2500 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
+ i
,
2501 TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
2503 ASSERT_PASSED(aio_writesame_test_data
, image
, test_data
, TEST_IO_SIZE
* i
,
2504 TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
2505 ASSERT_PASSED(aio_writesame_test_data
, image
, zero_data
, TEST_IO_SIZE
* i
,
2506 TEST_IO_SIZE
* i
* 32, TEST_IO_SIZE
, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
);
2510 ASSERT_PASSED(validate_object_map
, image
);
2518 TEST_F(TestLibRBD
, TestIOToSnapshot
)
2520 rados_ioctx_t ioctx
;
2521 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
2525 std::string name
= get_temp_image_name();
2526 uint64_t isize
= 2 << 20;
2528 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), isize
, &order
));
2529 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
2532 rbd_image_t image_at_snap
;
2533 char orig_data
[TEST_IO_TO_SNAP_SIZE
+ 1];
2534 char test_data
[TEST_IO_TO_SNAP_SIZE
+ 1];
2536 for (i
= 0; i
< TEST_IO_TO_SNAP_SIZE
; ++i
)
2537 test_data
[i
] = (char) (i
+ 48);
2538 test_data
[TEST_IO_TO_SNAP_SIZE
] = '\0';
2539 orig_data
[TEST_IO_TO_SNAP_SIZE
] = '\0';
2541 r
= rbd_read(image
, 0, TEST_IO_TO_SNAP_SIZE
, orig_data
);
2542 ASSERT_EQ(r
, TEST_IO_TO_SNAP_SIZE
);
2544 ASSERT_EQ(0, test_ls_snaps(image
, 0));
2545 ASSERT_EQ(0, rbd_snap_create(image
, "orig"));
2546 ASSERT_EQ(1, test_ls_snaps(image
, 1, "orig", isize
));
2547 ASSERT_PASSED(read_test_data
, image
, orig_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2549 printf("write test data!\n");
2550 ASSERT_PASSED(write_test_data
, image
, test_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2551 ASSERT_EQ(0, rbd_snap_create(image
, "written"));
2552 ASSERT_EQ(2, test_ls_snaps(image
, 2, "orig", isize
, "written", isize
));
2554 ASSERT_PASSED(read_test_data
, image
, test_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2556 rbd_snap_set(image
, "orig");
2557 ASSERT_PASSED(read_test_data
, image
, orig_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2559 rbd_snap_set(image
, "written");
2560 ASSERT_PASSED(read_test_data
, image
, test_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2562 rbd_snap_set(image
, "orig");
2564 r
= rbd_write(image
, 0, TEST_IO_TO_SNAP_SIZE
, test_data
);
2565 printf("write to snapshot returned %d\n", r
);
2567 cout
<< strerror(-r
) << std::endl
;
2569 ASSERT_PASSED(read_test_data
, image
, orig_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2570 rbd_snap_set(image
, "written");
2571 ASSERT_PASSED(read_test_data
, image
, test_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2573 r
= rbd_snap_rollback(image
, "orig");
2574 ASSERT_EQ(r
, -EROFS
);
2576 r
= rbd_snap_set(image
, NULL
);
2578 r
= rbd_snap_rollback(image
, "orig");
2581 ASSERT_PASSED(write_test_data
, image
, test_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2585 printf("opening testimg@orig\n");
2586 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image_at_snap
, "orig"));
2587 ASSERT_PASSED(read_test_data
, image_at_snap
, orig_data
, 0, TEST_IO_TO_SNAP_SIZE
, 0);
2588 r
= rbd_write(image_at_snap
, 0, TEST_IO_TO_SNAP_SIZE
, test_data
);
2589 printf("write to snapshot returned %d\n", r
);
2591 cout
<< strerror(-r
) << std::endl
;
2592 ASSERT_EQ(0, rbd_close(image_at_snap
));
2594 ASSERT_EQ(2, test_ls_snaps(image
, 2, "orig", isize
, "written", isize
));
2595 ASSERT_EQ(0, rbd_snap_remove(image
, "written"));
2596 ASSERT_EQ(1, test_ls_snaps(image
, 1, "orig", isize
));
2597 ASSERT_EQ(0, rbd_snap_remove(image
, "orig"));
2598 ASSERT_EQ(0, test_ls_snaps(image
, 0));
2600 ASSERT_PASSED(validate_object_map
, image
);
2601 ASSERT_EQ(0, rbd_close(image
));
2603 rados_ioctx_destroy(ioctx
);
2606 TEST_F(TestLibRBD
, TestClone
)
2608 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
2610 rados_ioctx_t ioctx
;
2611 rbd_image_info_t pinfo
, cinfo
;
2612 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
2616 rbd_image_t parent
, child
;
2619 ASSERT_EQ(0, get_features(&old_format
, &features
));
2620 ASSERT_FALSE(old_format
);
2622 std::string parent_name
= get_temp_image_name();
2623 std::string child_name
= get_temp_image_name();
2625 // make a parent to clone from
2626 ASSERT_EQ(0, create_image_full(ioctx
, parent_name
.c_str(), 4<<20, &order
,
2628 ASSERT_EQ(0, rbd_open(ioctx
, parent_name
.c_str(), &parent
, NULL
));
2629 printf("made parent image \"parent\"\n");
2631 char *data
= (char *)"testdata";
2632 ASSERT_EQ((ssize_t
)strlen(data
), rbd_write(parent
, 0, strlen(data
), data
));
2634 // can't clone a non-snapshot, expect failure
2635 EXPECT_NE(0, clone_image(ioctx
, parent
, parent_name
.c_str(), NULL
, ioctx
,
2636 child_name
.c_str(), features
, &order
));
2638 // verify that there is no parent info on "parent"
2639 ASSERT_EQ(-ENOENT
, rbd_get_parent_info(parent
, NULL
, 0, NULL
, 0, NULL
, 0));
2640 printf("parent has no parent info\n");
2642 // create 70 metadatas to verify we can clone all key/value pairs
2645 size_t sum_key_len
= 0;
2646 size_t sum_value_len
= 0;
2647 for (int i
= 1; i
<= 70; i
++) {
2648 key
= "key" + stringify(i
);
2649 val
= "value" + stringify(i
);
2650 ASSERT_EQ(0, rbd_metadata_set(parent
, key
.c_str(), val
.c_str()));
2652 sum_key_len
+= (key
.size() + 1);
2653 sum_value_len
+= (val
.size() + 1);
2658 size_t keys_len
= sizeof(keys
);
2659 size_t vals_len
= sizeof(vals
);
2662 size_t value_len
= sizeof(value
);
2664 // create a snapshot, reopen as the parent we're interested in
2665 ASSERT_EQ(0, rbd_snap_create(parent
, "parent_snap"));
2666 printf("made snapshot \"parent@parent_snap\"\n");
2667 ASSERT_EQ(0, rbd_close(parent
));
2668 ASSERT_EQ(0, rbd_open(ioctx
, parent_name
.c_str(), &parent
, "parent_snap"));
2670 ASSERT_EQ(-EINVAL
, clone_image(ioctx
, parent
, parent_name
.c_str(), "parent_snap",
2671 ioctx
, child_name
.c_str(), features
, &order
));
2673 // unprotected image should fail unprotect
2674 ASSERT_EQ(-EINVAL
, rbd_snap_unprotect(parent
, "parent_snap"));
2675 printf("can't unprotect an unprotected snap\n");
2677 ASSERT_EQ(0, rbd_snap_protect(parent
, "parent_snap"));
2678 // protecting again should fail
2679 ASSERT_EQ(-EBUSY
, rbd_snap_protect(parent
, "parent_snap"));
2680 printf("can't protect a protected snap\n");
2682 // This clone and open should work
2683 ASSERT_EQ(0, clone_image(ioctx
, parent
, parent_name
.c_str(), "parent_snap",
2684 ioctx
, child_name
.c_str(), features
, &order
));
2685 ASSERT_EQ(0, rbd_open(ioctx
, child_name
.c_str(), &child
, NULL
));
2686 printf("made and opened clone \"child\"\n");
2689 ASSERT_PASSED(read_test_data
, child
, data
, 0, strlen(data
), 0);
2692 ASSERT_EQ((ssize_t
)strlen(data
), rbd_write(child
, 20, strlen(data
), data
));
2693 ASSERT_PASSED(read_test_data
, child
, data
, 20, strlen(data
), 0);
2694 ASSERT_PASSED(read_test_data
, child
, data
, 0, strlen(data
), 0);
2697 ASSERT_EQ(0, rbd_stat(parent
, &pinfo
, sizeof(pinfo
)));
2698 ASSERT_EQ(0, rbd_stat(child
, &cinfo
, sizeof(cinfo
)));
2699 EXPECT_EQ(cinfo
.size
, pinfo
.size
);
2701 rbd_get_overlap(child
, &overlap
);
2702 EXPECT_EQ(overlap
, pinfo
.size
);
2703 EXPECT_EQ(cinfo
.obj_size
, pinfo
.obj_size
);
2704 EXPECT_EQ(cinfo
.order
, pinfo
.order
);
2705 printf("sizes and overlaps are good between parent and child\n");
2707 // check key/value pairs in child image
2708 ASSERT_EQ(0, rbd_metadata_list(child
, "", 70, keys
, &keys_len
, vals
,
2710 ASSERT_EQ(sum_key_len
, keys_len
);
2711 ASSERT_EQ(sum_value_len
, vals_len
);
2713 for (int i
= 1; i
<= 70; i
++) {
2714 key
= "key" + stringify(i
);
2715 val
= "value" + stringify(i
);
2716 ASSERT_EQ(0, rbd_metadata_get(child
, key
.c_str(), value
, &value_len
));
2717 ASSERT_STREQ(val
.c_str(), value
);
2719 value_len
= sizeof(value
);
2721 printf("child image successfully cloned all image-meta pairs\n");
2723 // sizing down child results in changing overlap and size, not parent size
2724 ASSERT_EQ(0, rbd_resize(child
, 2UL<<20));
2725 ASSERT_EQ(0, rbd_stat(child
, &cinfo
, sizeof(cinfo
)));
2726 rbd_get_overlap(child
, &overlap
);
2727 ASSERT_EQ(overlap
, 2UL<<20);
2728 ASSERT_EQ(cinfo
.size
, 2UL<<20);
2729 ASSERT_EQ(0, rbd_resize(child
, 4UL<<20));
2730 ASSERT_EQ(0, rbd_stat(child
, &cinfo
, sizeof(cinfo
)));
2731 rbd_get_overlap(child
, &overlap
);
2732 ASSERT_EQ(overlap
, 2UL<<20);
2733 ASSERT_EQ(cinfo
.size
, 4UL<<20);
2734 printf("sized down clone, changed overlap\n");
2736 // sizing back up doesn't change that
2737 ASSERT_EQ(0, rbd_resize(child
, 5UL<<20));
2738 ASSERT_EQ(0, rbd_stat(child
, &cinfo
, sizeof(cinfo
)));
2739 rbd_get_overlap(child
, &overlap
);
2740 ASSERT_EQ(overlap
, 2UL<<20);
2741 ASSERT_EQ(cinfo
.size
, 5UL<<20);
2742 ASSERT_EQ(0, rbd_stat(parent
, &pinfo
, sizeof(pinfo
)));
2743 printf("parent info: size %lld obj_size %lld parent_pool %lld\n",
2744 (unsigned long long)pinfo
.size
, (unsigned long long)pinfo
.obj_size
,
2745 (unsigned long long)pinfo
.parent_pool
);
2746 ASSERT_EQ(pinfo
.size
, 4UL<<20);
2747 printf("sized up clone, changed size but not overlap or parent's size\n");
2749 ASSERT_PASSED(validate_object_map
, child
);
2750 ASSERT_EQ(0, rbd_close(child
));
2752 ASSERT_PASSED(validate_object_map
, parent
);
2753 ASSERT_EQ(-EBUSY
, rbd_snap_remove(parent
, "parent_snap"));
2754 printf("can't remove parent while child still exists\n");
2755 ASSERT_EQ(0, rbd_remove(ioctx
, child_name
.c_str()));
2756 ASSERT_EQ(-EBUSY
, rbd_snap_remove(parent
, "parent_snap"));
2757 printf("can't remove parent while still protected\n");
2758 ASSERT_EQ(0, rbd_snap_unprotect(parent
, "parent_snap"));
2759 ASSERT_EQ(0, rbd_snap_remove(parent
, "parent_snap"));
2760 printf("removed parent snap after unprotecting\n");
2762 ASSERT_EQ(0, rbd_close(parent
));
2763 rados_ioctx_destroy(ioctx
);
2766 TEST_F(TestLibRBD
, TestClone2
)
2768 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
2770 rados_ioctx_t ioctx
;
2771 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
2775 rbd_image_t parent
, child
;
2778 ASSERT_EQ(0, get_features(&old_format
, &features
));
2779 ASSERT_FALSE(old_format
);
2781 std::string parent_name
= get_temp_image_name();
2782 std::string child_name
= get_temp_image_name();
2784 // make a parent to clone from
2785 ASSERT_EQ(0, create_image_full(ioctx
, parent_name
.c_str(), 4<<20, &order
,
2787 ASSERT_EQ(0, rbd_open(ioctx
, parent_name
.c_str(), &parent
, NULL
));
2788 printf("made parent image \"parent\"\n");
2790 char *data
= (char *)"testdata";
2791 char *childata
= (char *)"childata";
2792 ASSERT_EQ((ssize_t
)strlen(data
), rbd_write(parent
, 0, strlen(data
), data
));
2793 ASSERT_EQ((ssize_t
)strlen(data
), rbd_write(parent
, 12, strlen(data
), data
));
2795 // can't clone a non-snapshot, expect failure
2796 EXPECT_NE(0, clone_image(ioctx
, parent
, parent_name
.c_str(), NULL
, ioctx
,
2797 child_name
.c_str(), features
, &order
));
2799 // verify that there is no parent info on "parent"
2800 ASSERT_EQ(-ENOENT
, rbd_get_parent_info(parent
, NULL
, 0, NULL
, 0, NULL
, 0));
2801 printf("parent has no parent info\n");
2803 // create 70 metadatas to verify we can clone all key/value pairs
2806 size_t sum_key_len
= 0;
2807 size_t sum_value_len
= 0;
2808 for (int i
= 1; i
<= 70; i
++) {
2809 key
= "key" + stringify(i
);
2810 val
= "value" + stringify(i
);
2811 ASSERT_EQ(0, rbd_metadata_set(parent
, key
.c_str(), val
.c_str()));
2813 sum_key_len
+= (key
.size() + 1);
2814 sum_value_len
+= (val
.size() + 1);
2819 size_t keys_len
= sizeof(keys
);
2820 size_t vals_len
= sizeof(vals
);
2823 size_t value_len
= sizeof(value
);
2825 // create a snapshot, reopen as the parent we're interested in
2826 ASSERT_EQ(0, rbd_snap_create(parent
, "parent_snap"));
2827 printf("made snapshot \"parent@parent_snap\"\n");
2828 ASSERT_EQ(0, rbd_close(parent
));
2829 ASSERT_EQ(0, rbd_open(ioctx
, parent_name
.c_str(), &parent
, "parent_snap"));
2831 ASSERT_EQ(-EINVAL
, clone_image(ioctx
, parent
, parent_name
.c_str(), "parent_snap",
2832 ioctx
, child_name
.c_str(), features
, &order
));
2834 // unprotected image should fail unprotect
2835 ASSERT_EQ(-EINVAL
, rbd_snap_unprotect(parent
, "parent_snap"));
2836 printf("can't unprotect an unprotected snap\n");
2838 ASSERT_EQ(0, rbd_snap_protect(parent
, "parent_snap"));
2839 // protecting again should fail
2840 ASSERT_EQ(-EBUSY
, rbd_snap_protect(parent
, "parent_snap"));
2841 printf("can't protect a protected snap\n");
2843 // This clone and open should work
2844 ASSERT_EQ(0, clone_image(ioctx
, parent
, parent_name
.c_str(), "parent_snap",
2845 ioctx
, child_name
.c_str(), features
, &order
));
2846 ASSERT_EQ(0, rbd_open(ioctx
, child_name
.c_str(), &child
, NULL
));
2847 printf("made and opened clone \"child\"\n");
2849 // check key/value pairs in child image
2850 ASSERT_EQ(0, rbd_metadata_list(child
, "", 70, keys
, &keys_len
, vals
,
2852 ASSERT_EQ(sum_key_len
, keys_len
);
2853 ASSERT_EQ(sum_value_len
, vals_len
);
2855 for (int i
= 1; i
<= 70; i
++) {
2856 key
= "key" + stringify(i
);
2857 val
= "value" + stringify(i
);
2858 ASSERT_EQ(0, rbd_metadata_get(child
, key
.c_str(), value
, &value_len
));
2859 ASSERT_STREQ(val
.c_str(), value
);
2861 value_len
= sizeof(value
);
2863 printf("child image successfully cloned all image-meta pairs\n");
2865 // write something in
2866 ASSERT_EQ((ssize_t
)strlen(childata
), rbd_write(child
, 20, strlen(childata
), childata
));
2868 char test
[strlen(data
) * 2];
2869 ASSERT_EQ((ssize_t
)strlen(data
), rbd_read(child
, 20, strlen(data
), test
));
2870 ASSERT_EQ(0, memcmp(test
, childata
, strlen(childata
)));
2873 ASSERT_EQ((ssize_t
)sizeof(test
), rbd_read(child
, 20 - strlen(data
), sizeof(test
), test
));
2874 ASSERT_EQ(0, memcmp(test
, data
, strlen(data
)));
2875 ASSERT_EQ(0, memcmp(test
+ strlen(data
), childata
, strlen(childata
)));
2878 ASSERT_EQ((ssize_t
)sizeof(test
), rbd_read(child
, 0, sizeof(test
), test
));
2879 ASSERT_EQ(0, memcmp(test
, data
, strlen(data
)));
2881 ASSERT_PASSED(validate_object_map
, child
);
2882 ASSERT_PASSED(validate_object_map
, parent
);
2884 ASSERT_EQ(0, rbd_close(child
));
2885 ASSERT_EQ(0, rbd_close(parent
));
2886 rados_ioctx_destroy(ioctx
);
2889 static void test_list_children(rbd_image_t image
, ssize_t num_expected
, ...)
2892 va_start(ap
, num_expected
);
2893 size_t pools_len
= 100;
2894 size_t children_len
= 100;
2896 char *children
= NULL
;
2897 ssize_t num_children
;
2902 pools
= (char *) malloc(pools_len
);
2903 children
= (char *) malloc(children_len
);
2904 num_children
= rbd_list_children(image
, pools
, &pools_len
,
2905 children
, &children_len
);
2906 } while (num_children
== -ERANGE
);
2908 ASSERT_EQ(num_expected
, num_children
);
2909 for (ssize_t i
= num_expected
; i
> 0; --i
) {
2910 char *expected_pool
= va_arg(ap
, char *);
2911 char *expected_image
= va_arg(ap
, char *);
2913 char *image
= children
;
2915 printf("\ntrying to find %s/%s\n", expected_pool
, expected_image
);
2916 for (ssize_t j
= 0; j
< num_children
; ++j
) {
2917 printf("checking %s/%s\n", pool
, image
);
2918 if (strcmp(expected_pool
, pool
) == 0 &&
2919 strcmp(expected_image
, image
) == 0) {
2920 printf("found child %s/%s\n\n", pool
, image
);
2924 pool
+= strlen(pool
) + 1;
2925 image
+= strlen(image
) + 1;
2926 if (j
== num_children
- 1) {
2927 ASSERT_EQ(pool
- pools
- 1, (ssize_t
) pools_len
);
2928 ASSERT_EQ(image
- children
- 1, (ssize_t
) children_len
);
2941 TEST_F(TestLibRBD
, ListChildren
)
2943 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
2945 rados_ioctx_t ioctx1
, ioctx2
;
2946 string pool_name1
= create_pool(true);
2947 string pool_name2
= create_pool(true);
2948 ASSERT_NE("", pool_name2
);
2950 rados_ioctx_create(_cluster
, pool_name1
.c_str(), &ioctx1
);
2951 rados_ioctx_create(_cluster
, pool_name2
.c_str(), &ioctx2
);
2958 ASSERT_EQ(0, get_features(&old_format
, &features
));
2959 ASSERT_FALSE(old_format
);
2961 std::string parent_name
= get_temp_image_name();
2962 std::string child_name1
= get_temp_image_name();
2963 std::string child_name2
= get_temp_image_name();
2964 std::string child_name3
= get_temp_image_name();
2965 std::string child_name4
= get_temp_image_name();
2967 // make a parent to clone from
2968 ASSERT_EQ(0, create_image_full(ioctx1
, parent_name
.c_str(), 4<<20, &order
,
2970 ASSERT_EQ(0, rbd_open(ioctx1
, parent_name
.c_str(), &parent
, NULL
));
2971 // create a snapshot, reopen as the parent we're interested in
2972 ASSERT_EQ(0, rbd_snap_create(parent
, "parent_snap"));
2973 ASSERT_EQ(0, rbd_snap_set(parent
, "parent_snap"));
2974 ASSERT_EQ(0, rbd_snap_protect(parent
, "parent_snap"));
2976 ASSERT_EQ(0, rbd_close(parent
));
2977 ASSERT_EQ(0, rbd_open(ioctx1
, parent_name
.c_str(), &parent
, "parent_snap"));
2979 ASSERT_EQ(0, clone_image(ioctx1
, parent
, parent_name
.c_str(), "parent_snap",
2980 ioctx2
, child_name1
.c_str(), features
, &order
));
2981 test_list_children(parent
, 1, pool_name2
.c_str(), child_name1
.c_str());
2983 ASSERT_EQ(0, clone_image(ioctx1
, parent
, parent_name
.c_str(), "parent_snap",
2984 ioctx1
, child_name2
.c_str(), features
, &order
));
2985 test_list_children(parent
, 2, pool_name2
.c_str(), child_name1
.c_str(),
2986 pool_name1
.c_str(), child_name2
.c_str());
2988 ASSERT_EQ(0, clone_image(ioctx1
, parent
, parent_name
.c_str(), "parent_snap",
2989 ioctx2
, child_name3
.c_str(), features
, &order
));
2990 test_list_children(parent
, 3, pool_name2
.c_str(), child_name1
.c_str(),
2991 pool_name1
.c_str(), child_name2
.c_str(),
2992 pool_name2
.c_str(), child_name3
.c_str());
2994 ASSERT_EQ(0, clone_image(ioctx1
, parent
, parent_name
.c_str(), "parent_snap",
2995 ioctx2
, child_name4
.c_str(), features
, &order
));
2996 test_list_children(parent
, 4, pool_name2
.c_str(), child_name1
.c_str(),
2997 pool_name1
.c_str(), child_name2
.c_str(),
2998 pool_name2
.c_str(), child_name3
.c_str(),
2999 pool_name2
.c_str(), child_name4
.c_str());
3001 ASSERT_EQ(0, rbd_remove(ioctx2
, child_name1
.c_str()));
3002 test_list_children(parent
, 3,
3003 pool_name1
.c_str(), child_name2
.c_str(),
3004 pool_name2
.c_str(), child_name3
.c_str(),
3005 pool_name2
.c_str(), child_name4
.c_str());
3007 ASSERT_EQ(0, rbd_remove(ioctx2
, child_name3
.c_str()));
3008 test_list_children(parent
, 2,
3009 pool_name1
.c_str(), child_name2
.c_str(),
3010 pool_name2
.c_str(), child_name4
.c_str());
3012 ASSERT_EQ(0, rbd_remove(ioctx2
, child_name4
.c_str()));
3013 test_list_children(parent
, 1,
3014 pool_name1
.c_str(), child_name2
.c_str());
3016 ASSERT_EQ(0, rbd_remove(ioctx1
, child_name2
.c_str()));
3017 test_list_children(parent
, 0);
3019 ASSERT_EQ(0, rbd_snap_unprotect(parent
, "parent_snap"));
3020 ASSERT_EQ(0, rbd_snap_remove(parent
, "parent_snap"));
3021 ASSERT_EQ(0, rbd_close(parent
));
3022 ASSERT_EQ(0, rbd_remove(ioctx1
, parent_name
.c_str()));
3023 rados_ioctx_destroy(ioctx1
);
3024 rados_ioctx_destroy(ioctx2
);
3027 TEST_F(TestLibRBD
, ListChildrenTiered
)
3029 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
3031 string pool_name1
= m_pool_name
;
3032 string pool_name2
= create_pool(true);
3033 string pool_name3
= create_pool(true);
3034 ASSERT_NE("", pool_name2
);
3035 ASSERT_NE("", pool_name3
);
3037 std::string cmdstr
= "{\"prefix\": \"osd tier add\", \"pool\": \"" +
3038 pool_name1
+ "\", \"tierpool\":\"" + pool_name3
+ "\", \"force_nonempty\":\"\"}";
3040 cmd
[0] = (char *)cmdstr
.c_str();
3041 ASSERT_EQ(0, rados_mon_command(_cluster
, (const char **)cmd
, 1, "", 0, NULL
, 0, NULL
, 0));
3043 cmdstr
= "{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" +
3044 pool_name3
+ "\", \"mode\":\"writeback\"}";
3045 cmd
[0] = (char *)cmdstr
.c_str();
3046 ASSERT_EQ(0, rados_mon_command(_cluster
, (const char **)cmd
, 1, "", 0, NULL
, 0, NULL
, 0));
3048 cmdstr
= "{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" +
3049 pool_name1
+ "\", \"overlaypool\":\"" + pool_name3
+ "\"}";
3050 cmd
[0] = (char *)cmdstr
.c_str();
3051 ASSERT_EQ(0, rados_mon_command(_cluster
, (const char **)cmd
, 1, "", 0, NULL
, 0, NULL
, 0));
3053 EXPECT_EQ(0, rados_wait_for_latest_osdmap(_cluster
));
3055 string parent_name
= get_temp_image_name();
3056 string child_name1
= get_temp_image_name();
3057 string child_name2
= get_temp_image_name();
3058 string child_name3
= get_temp_image_name();
3059 string child_name4
= get_temp_image_name();
3061 rados_ioctx_t ioctx1
, ioctx2
;
3062 rados_ioctx_create(_cluster
, pool_name1
.c_str(), &ioctx1
);
3063 rados_ioctx_create(_cluster
, pool_name2
.c_str(), &ioctx2
);
3070 ASSERT_EQ(0, get_features(&old_format
, &features
));
3071 ASSERT_FALSE(old_format
);
3073 // make a parent to clone from
3074 ASSERT_EQ(0, create_image_full(ioctx1
, parent_name
.c_str(), 4<<20, &order
,
3076 ASSERT_EQ(0, rbd_open(ioctx1
, parent_name
.c_str(), &parent
, NULL
));
3077 // create a snapshot, reopen as the parent we're interested in
3078 ASSERT_EQ(0, rbd_snap_create(parent
, "parent_snap"));
3079 ASSERT_EQ(0, rbd_snap_set(parent
, "parent_snap"));
3080 ASSERT_EQ(0, rbd_snap_protect(parent
, "parent_snap"));
3082 ASSERT_EQ(0, rbd_close(parent
));
3083 ASSERT_EQ(0, rbd_open(ioctx1
, parent_name
.c_str(), &parent
, "parent_snap"));
3085 ASSERT_EQ(0, clone_image(ioctx1
, parent
, parent_name
.c_str(), "parent_snap",
3086 ioctx2
, child_name1
.c_str(), features
, &order
));
3087 test_list_children(parent
, 1, pool_name2
.c_str(), child_name1
.c_str());
3089 ASSERT_EQ(0, clone_image(ioctx1
, parent
, parent_name
.c_str(), "parent_snap",
3090 ioctx1
, child_name2
.c_str(), features
, &order
));
3091 test_list_children(parent
, 2, pool_name2
.c_str(), child_name1
.c_str(),
3092 pool_name1
.c_str(), child_name2
.c_str());
3094 // read from the cache to populate it
3095 rbd_image_t tier_image
;
3096 ASSERT_EQ(0, rbd_open(ioctx1
, child_name2
.c_str(), &tier_image
, NULL
));
3097 size_t len
= 4 * 1024 * 1024;
3098 char* buf
= (char*)malloc(len
);
3099 ssize_t size
= rbd_read(tier_image
, 0, len
, buf
);
3102 ASSERT_EQ(0, rbd_close(tier_image
));
3104 ASSERT_EQ(0, clone_image(ioctx1
, parent
, parent_name
.c_str(), "parent_snap",
3105 ioctx2
, child_name3
.c_str(), features
, &order
));
3106 test_list_children(parent
, 3, pool_name2
.c_str(), child_name1
.c_str(),
3107 pool_name1
.c_str(), child_name2
.c_str(),
3108 pool_name2
.c_str(), child_name3
.c_str());
3110 ASSERT_EQ(0, clone_image(ioctx1
, parent
, parent_name
.c_str(), "parent_snap",
3111 ioctx2
, child_name4
.c_str(), features
, &order
));
3112 test_list_children(parent
, 4, pool_name2
.c_str(), child_name1
.c_str(),
3113 pool_name1
.c_str(), child_name2
.c_str(),
3114 pool_name2
.c_str(), child_name3
.c_str(),
3115 pool_name2
.c_str(), child_name4
.c_str());
3117 ASSERT_EQ(0, rbd_remove(ioctx2
, child_name1
.c_str()));
3118 test_list_children(parent
, 3,
3119 pool_name1
.c_str(), child_name2
.c_str(),
3120 pool_name2
.c_str(), child_name3
.c_str(),
3121 pool_name2
.c_str(), child_name4
.c_str());
3123 ASSERT_EQ(0, rbd_remove(ioctx2
, child_name3
.c_str()));
3124 test_list_children(parent
, 2,
3125 pool_name1
.c_str(), child_name2
.c_str(),
3126 pool_name2
.c_str(), child_name4
.c_str());
3128 ASSERT_EQ(0, rbd_remove(ioctx2
, child_name4
.c_str()));
3129 test_list_children(parent
, 1,
3130 pool_name1
.c_str(), child_name2
.c_str());
3132 ASSERT_EQ(0, rbd_remove(ioctx1
, child_name2
.c_str()));
3133 test_list_children(parent
, 0);
3135 ASSERT_EQ(0, rbd_snap_unprotect(parent
, "parent_snap"));
3136 ASSERT_EQ(0, rbd_snap_remove(parent
, "parent_snap"));
3137 ASSERT_EQ(0, rbd_close(parent
));
3138 ASSERT_EQ(0, rbd_remove(ioctx1
, parent_name
.c_str()));
3139 rados_ioctx_destroy(ioctx1
);
3140 rados_ioctx_destroy(ioctx2
);
3141 cmdstr
= "{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" +
3143 cmd
[0] = (char *)cmdstr
.c_str();
3144 ASSERT_EQ(0, rados_mon_command(_cluster
, (const char **)cmd
, 1, "", 0, NULL
, 0, NULL
, 0));
3145 cmdstr
= "{\"prefix\": \"osd tier remove\", \"pool\": \"" +
3146 pool_name1
+ "\", \"tierpool\":\"" + pool_name3
+ "\"}";
3147 cmd
[0] = (char *)cmdstr
.c_str();
3148 ASSERT_EQ(0, rados_mon_command(_cluster
, (const char **)cmd
, 1, "", 0, NULL
, 0, NULL
, 0));
3151 TEST_F(TestLibRBD
, LockingPP
)
3153 librados::IoCtx ioctx
;
3154 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
3158 librbd::Image image
;
3160 std::string name
= get_temp_image_name();
3161 uint64_t size
= 2 << 20;
3162 std::string cookie1
= "foo";
3163 std::string cookie2
= "bar";
3165 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3166 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3168 // no lockers initially
3169 std::list
<librbd::locker_t
> lockers
;
3172 ASSERT_EQ(0, image
.list_lockers(&lockers
, &exclusive
, &tag
));
3173 ASSERT_EQ(0u, lockers
.size());
3176 // exclusive lock is exclusive
3177 ASSERT_EQ(0, image
.lock_exclusive(cookie1
));
3178 ASSERT_EQ(-EEXIST
, image
.lock_exclusive(cookie1
));
3179 ASSERT_EQ(-EBUSY
, image
.lock_exclusive(""));
3180 ASSERT_EQ(-EEXIST
, image
.lock_shared(cookie1
, ""));
3181 ASSERT_EQ(-EBUSY
, image
.lock_shared(cookie1
, "test"));
3182 ASSERT_EQ(-EBUSY
, image
.lock_shared("", "test"));
3183 ASSERT_EQ(-EBUSY
, image
.lock_shared("", ""));
3186 ASSERT_EQ(0, image
.list_lockers(&lockers
, &exclusive
, &tag
));
3187 ASSERT_TRUE(exclusive
);
3189 ASSERT_EQ(1u, lockers
.size());
3190 ASSERT_EQ(cookie1
, lockers
.front().cookie
);
3193 ASSERT_EQ(-ENOENT
, image
.unlock(""));
3194 ASSERT_EQ(-ENOENT
, image
.unlock(cookie2
));
3195 ASSERT_EQ(0, image
.unlock(cookie1
));
3196 ASSERT_EQ(-ENOENT
, image
.unlock(cookie1
));
3197 ASSERT_EQ(0, image
.list_lockers(&lockers
, &exclusive
, &tag
));
3198 ASSERT_EQ(0u, lockers
.size());
3200 ASSERT_EQ(0, image
.lock_shared(cookie1
, ""));
3201 ASSERT_EQ(-EEXIST
, image
.lock_shared(cookie1
, ""));
3202 ASSERT_EQ(0, image
.lock_shared(cookie2
, ""));
3203 ASSERT_EQ(-EEXIST
, image
.lock_shared(cookie2
, ""));
3204 ASSERT_EQ(-EEXIST
, image
.lock_exclusive(cookie1
));
3205 ASSERT_EQ(-EEXIST
, image
.lock_exclusive(cookie2
));
3206 ASSERT_EQ(-EBUSY
, image
.lock_exclusive(""));
3207 ASSERT_EQ(-EBUSY
, image
.lock_exclusive("test"));
3210 ASSERT_EQ(0, image
.list_lockers(&lockers
, &exclusive
, &tag
));
3211 ASSERT_EQ(2u, lockers
.size());
3217 TEST_F(TestLibRBD
, FlushAio
)
3219 rados_ioctx_t ioctx
;
3220 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
3224 std::string name
= get_temp_image_name();
3225 uint64_t size
= 2 << 20;
3226 size_t num_aios
= 256;
3228 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
3229 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
3231 char test_data
[TEST_IO_SIZE
+ 1];
3233 for (i
= 0; i
< TEST_IO_SIZE
; ++i
) {
3234 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
3237 rbd_completion_t write_comps
[num_aios
];
3238 for (i
= 0; i
< num_aios
; ++i
) {
3239 ASSERT_EQ(0, rbd_aio_create_completion(NULL
, NULL
, &write_comps
[i
]));
3240 uint64_t offset
= rand() % (size
- TEST_IO_SIZE
);
3241 ASSERT_EQ(0, rbd_aio_write(image
, offset
, TEST_IO_SIZE
, test_data
,
3245 rbd_completion_t flush_comp
;
3246 ASSERT_EQ(0, rbd_aio_create_completion(NULL
, NULL
, &flush_comp
));
3247 ASSERT_EQ(0, rbd_aio_flush(image
, flush_comp
));
3248 ASSERT_EQ(0, rbd_aio_wait_for_complete(flush_comp
));
3249 ASSERT_EQ(1, rbd_aio_is_complete(flush_comp
));
3250 rbd_aio_release(flush_comp
);
3252 for (i
= 0; i
< num_aios
; ++i
) {
3253 ASSERT_EQ(1, rbd_aio_is_complete(write_comps
[i
]));
3254 rbd_aio_release(write_comps
[i
]);
3257 ASSERT_PASSED(validate_object_map
, image
);
3258 ASSERT_EQ(0, rbd_close(image
));
3259 ASSERT_EQ(0, rbd_remove(ioctx
, name
.c_str()));
3260 rados_ioctx_destroy(ioctx
);
3263 TEST_F(TestLibRBD
, FlushAioPP
)
3265 librados::IoCtx ioctx
;
3266 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
3270 librbd::Image image
;
3272 std::string name
= get_temp_image_name();
3273 uint64_t size
= 2 << 20;
3274 const size_t num_aios
= 256;
3276 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3277 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3279 char test_data
[TEST_IO_SIZE
+ 1];
3281 for (i
= 0; i
< TEST_IO_SIZE
; ++i
) {
3282 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
3284 test_data
[TEST_IO_SIZE
] = '\0';
3286 librbd::RBD::AioCompletion
*write_comps
[num_aios
];
3287 ceph::bufferlist bls
[num_aios
];
3288 for (i
= 0; i
< num_aios
; ++i
) {
3289 bls
[i
].append(test_data
, strlen(test_data
));
3290 write_comps
[i
] = new librbd::RBD::AioCompletion(NULL
, NULL
);
3291 uint64_t offset
= rand() % (size
- TEST_IO_SIZE
);
3292 ASSERT_EQ(0, image
.aio_write(offset
, TEST_IO_SIZE
, bls
[i
],
3296 librbd::RBD::AioCompletion
*flush_comp
=
3297 new librbd::RBD::AioCompletion(NULL
, NULL
);
3298 ASSERT_EQ(0, image
.aio_flush(flush_comp
));
3299 ASSERT_EQ(0, flush_comp
->wait_for_complete());
3300 ASSERT_EQ(1, flush_comp
->is_complete());
3301 flush_comp
->release();
3303 for (i
= 0; i
< num_aios
; ++i
) {
3304 librbd::RBD::AioCompletion
*comp
= write_comps
[i
];
3305 ASSERT_EQ(1, comp
->is_complete());
3308 ASSERT_PASSED(validate_object_map
, image
);
3315 int iterate_cb(uint64_t off
, size_t len
, int exists
, void *arg
)
3317 //cout << "iterate_cb " << off << "~" << len << std::endl;
3318 interval_set
<uint64_t> *diff
= static_cast<interval_set
<uint64_t> *>(arg
);
3319 diff
->insert(off
, len
);
3323 static int iterate_error_cb(uint64_t off
, size_t len
, int exists
, void *arg
)
3328 void scribble(librbd::Image
& image
, int n
, int max
, bool skip_discard
,
3329 interval_set
<uint64_t> *exists
,
3330 interval_set
<uint64_t> *what
)
3334 interval_set
<uint64_t> exists_at_start
= *exists
;
3336 for (int i
=0; i
<n
; i
++) {
3337 uint64_t off
= rand() % (size
- max
+ 1);
3338 uint64_t len
= 1 + rand() % max
;
3339 if (!skip_discard
&& rand() % 4 == 0) {
3340 ASSERT_EQ((int)len
, image
.discard(off
, len
));
3341 interval_set
<uint64_t> w
;
3344 // the zeroed bit no longer exists...
3345 w
.intersection_of(*exists
);
3346 exists
->subtract(w
);
3348 // the bits we discarded are no long written...
3349 interval_set
<uint64_t> w2
= w
;
3350 w2
.intersection_of(*what
);
3353 // except for the extents that existed at the start that we overwrote.
3354 interval_set
<uint64_t> w3
;
3355 w3
.insert(off
, len
);
3356 w3
.intersection_of(exists_at_start
);
3361 bl
.append(buffer::create(len
));
3363 ASSERT_EQ((int)len
, image
.write(off
, len
, bl
));
3364 interval_set
<uint64_t> w
;
3367 exists
->union_of(w
);
3372 interval_set
<uint64_t> round_diff_interval(const interval_set
<uint64_t>& diff
,
3373 uint64_t object_size
)
3375 if (object_size
== 0) {
3379 interval_set
<uint64_t> rounded_diff
;
3380 for (interval_set
<uint64_t>::const_iterator it
= diff
.begin();
3381 it
!= diff
.end(); ++it
) {
3382 uint64_t off
= it
.get_start();
3383 uint64_t len
= it
.get_len();
3384 off
-= off
% object_size
;
3385 len
+= (object_size
- (len
% object_size
));
3386 interval_set
<uint64_t> interval
;
3387 interval
.insert(off
, len
);
3388 rounded_diff
.union_of(interval
);
3390 return rounded_diff
;
3393 template <typename T
>
3394 class DiffIterateTest
: public TestLibRBD
{
3396 static const uint8_t whole_object
= T::whole_object
;
3399 template <bool _whole_object
>
3400 class DiffIterateParams
{
3402 static const uint8_t whole_object
= _whole_object
;
3405 typedef ::testing::Types
<DiffIterateParams
<false>,
3406 DiffIterateParams
<true> > DiffIterateTypes
;
3407 TYPED_TEST_CASE(DiffIterateTest
, DiffIterateTypes
);
3409 TYPED_TEST(DiffIterateTest
, DiffIterate
)
3411 librados::IoCtx ioctx
;
3412 ASSERT_EQ(0, this->_rados
.ioctx_create(this->m_pool_name
.c_str(), ioctx
));
3414 bool skip_discard
= this->is_skip_partial_discard_enabled();
3418 librbd::Image image
;
3420 std::string name
= this->get_temp_image_name();
3421 uint64_t size
= 20 << 20;
3423 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3424 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3426 uint64_t object_size
= 0;
3427 if (this->whole_object
) {
3428 object_size
= 1 << order
;
3431 interval_set
<uint64_t> exists
;
3432 interval_set
<uint64_t> one
, two
;
3433 scribble(image
, 10, 102400, skip_discard
, &exists
, &one
);
3434 cout
<< " wrote " << one
<< std::endl
;
3435 ASSERT_EQ(0, image
.snap_create("one"));
3436 scribble(image
, 10, 102400, skip_discard
, &exists
, &two
);
3438 two
= round_diff_interval(two
, object_size
);
3439 cout
<< " wrote " << two
<< std::endl
;
3441 interval_set
<uint64_t> diff
;
3442 ASSERT_EQ(0, image
.diff_iterate2("one", 0, size
, true, this->whole_object
,
3443 iterate_cb
, (void *)&diff
));
3444 cout
<< " diff was " << diff
<< std::endl
;
3445 if (!two
.subset_of(diff
)) {
3446 interval_set
<uint64_t> i
;
3447 i
.intersection_of(two
, diff
);
3448 interval_set
<uint64_t> l
= two
;
3450 cout
<< " ... two - (two*diff) = " << l
<< std::endl
;
3452 ASSERT_TRUE(two
.subset_of(diff
));
3457 struct diff_extent
{
3458 diff_extent(uint64_t _offset
, uint64_t _length
, bool _exists
,
3459 uint64_t object_size
) :
3460 offset(_offset
), length(_length
), exists(_exists
)
3462 if (object_size
!= 0) {
3463 offset
-= offset
% object_size
;
3464 length
= object_size
;
3470 bool operator==(const diff_extent
& o
) const {
3471 return offset
== o
.offset
&& length
== o
.length
&& exists
== o
.exists
;
3475 ostream
& operator<<(ostream
& o
, const diff_extent
& e
) {
3476 return o
<< '(' << e
.offset
<< '~' << e
.length
<< ' ' << (e
.exists
? "true" : "false") << ')';
3479 int vector_iterate_cb(uint64_t off
, size_t len
, int exists
, void *arg
)
3481 cout
<< "iterate_cb " << off
<< "~" << len
<< std::endl
;
3482 vector
<diff_extent
> *diff
= static_cast<vector
<diff_extent
> *>(arg
);
3483 diff
->push_back(diff_extent(off
, len
, exists
, 0));
3487 TYPED_TEST(DiffIterateTest
, DiffIterateDiscard
)
3489 librados::IoCtx ioctx
;
3490 ASSERT_EQ(0, this->_rados
.ioctx_create(this->m_pool_name
.c_str(), ioctx
));
3493 librbd::Image image
;
3495 std::string name
= this->get_temp_image_name();
3496 uint64_t size
= 20 << 20;
3498 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3499 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3501 uint64_t object_size
= 0;
3502 if (this->whole_object
) {
3503 object_size
= 1 << order
;
3505 vector
<diff_extent
> extents
;
3506 ceph::bufferlist bl
;
3508 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, true, this->whole_object
,
3509 vector_iterate_cb
, (void *) &extents
));
3510 ASSERT_EQ(0u, extents
.size());
3513 memset(data
, 1, sizeof(data
));
3514 bl
.append(data
, 256);
3515 ASSERT_EQ(256, image
.write(0, 256, bl
));
3516 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, true, this->whole_object
,
3517 vector_iterate_cb
, (void *) &extents
));
3518 ASSERT_EQ(1u, extents
.size());
3519 ASSERT_EQ(diff_extent(0, 256, true, object_size
), extents
[0]);
3522 ASSERT_EQ(1 << order
, image
.discard(0, 1 << order
));
3525 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, true, this->whole_object
,
3526 vector_iterate_cb
, (void *) &extents
));
3527 ASSERT_EQ(0u, extents
.size());
3529 ASSERT_EQ(0, image
.snap_create("snap1"));
3530 ASSERT_EQ(256, image
.write(0, 256, bl
));
3531 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, true, this->whole_object
,
3532 vector_iterate_cb
, (void *) &extents
));
3533 ASSERT_EQ(1u, extents
.size());
3534 ASSERT_EQ(diff_extent(0, 256, true, object_size
), extents
[0]);
3535 ASSERT_EQ(0, image
.snap_create("snap2"));
3537 ASSERT_EQ(obj_ofs
, image
.discard(0, obj_ofs
));
3540 ASSERT_EQ(0, image
.snap_set("snap2"));
3541 ASSERT_EQ(0, image
.diff_iterate2("snap1", 0, size
, true, this->whole_object
,
3542 vector_iterate_cb
, (void *) &extents
));
3543 ASSERT_EQ(1u, extents
.size());
3544 ASSERT_EQ(diff_extent(0, 256, true, object_size
), extents
[0]);
3546 ASSERT_EQ(0, image
.snap_set(NULL
));
3547 ASSERT_EQ(1 << order
, image
.discard(0, 1 << order
));
3548 ASSERT_EQ(0, image
.snap_create("snap3"));
3549 ASSERT_EQ(0, image
.snap_set("snap3"));
3552 ASSERT_EQ(0, image
.diff_iterate2("snap1", 0, size
, true, this->whole_object
,
3553 vector_iterate_cb
, (void *) &extents
));
3554 ASSERT_EQ(1u, extents
.size());
3555 ASSERT_EQ(diff_extent(0, 256, false, object_size
), extents
[0]);
3556 ASSERT_PASSED(this->validate_object_map
, image
);
3559 TYPED_TEST(DiffIterateTest
, DiffIterateStress
)
3561 librados::IoCtx ioctx
;
3562 ASSERT_EQ(0, this->_rados
.ioctx_create(this->m_pool_name
.c_str(), ioctx
));
3564 bool skip_discard
= this->is_skip_partial_discard_enabled();
3567 librbd::Image image
;
3569 std::string name
= this->get_temp_image_name();
3570 uint64_t size
= 400 << 20;
3572 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3573 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3575 uint64_t object_size
= 0;
3576 if (this->whole_object
) {
3577 object_size
= 1 << order
;
3580 interval_set
<uint64_t> curexists
;
3581 vector
<interval_set
<uint64_t> > wrote
;
3582 vector
<interval_set
<uint64_t> > exists
;
3583 vector
<string
> snap
;
3585 for (int i
=0; i
<n
; i
++) {
3586 interval_set
<uint64_t> w
;
3587 scribble(image
, 10, 8192000, skip_discard
, &curexists
, &w
);
3588 cout
<< " i=" << i
<< " exists " << curexists
<< " wrote " << w
<< std::endl
;
3589 string s
= "snap" + stringify(i
);
3590 ASSERT_EQ(0, image
.snap_create(s
.c_str()));
3592 exists
.push_back(curexists
);
3596 for (int h
=0; h
<n
-1; h
++) {
3597 for (int i
=0; i
<n
-h
-1; i
++) {
3598 for (int j
=(h
==0 ? i
+1 : n
-1); j
<n
; j
++) {
3599 interval_set
<uint64_t> diff
, actual
, uex
;
3600 for (int k
=i
+1; k
<=j
; k
++)
3601 diff
.union_of(wrote
[k
]);
3602 cout
<< "from " << i
<< " to "
3603 << (h
!= 0 ? string("HEAD") : stringify(j
)) << " diff "
3604 << round_diff_interval(diff
, object_size
) << std::endl
;
3606 // limit to extents that exists both at the beginning and at the end
3607 uex
.union_of(exists
[i
], exists
[j
]);
3608 diff
.intersection_of(uex
);
3609 diff
= round_diff_interval(diff
, object_size
);
3610 cout
<< " limited diff " << diff
<< std::endl
;
3612 ASSERT_EQ(0, image
.snap_set(h
==0 ? snap
[j
].c_str() : NULL
));
3613 ASSERT_EQ(0, image
.diff_iterate2(snap
[i
].c_str(), 0, size
, true,
3614 this->whole_object
, iterate_cb
,
3616 cout
<< " actual was " << actual
<< std::endl
;
3617 if (!diff
.subset_of(actual
)) {
3618 interval_set
<uint64_t> i
;
3619 i
.intersection_of(diff
, actual
);
3620 interval_set
<uint64_t> l
= diff
;
3622 cout
<< " ... diff - (actual*diff) = " << l
<< std::endl
;
3624 ASSERT_TRUE(diff
.subset_of(actual
));
3627 ASSERT_EQ(0, image
.snap_set(NULL
));
3628 ASSERT_EQ(0, image
.snap_remove(snap
[n
-h
-1].c_str()));
3631 ASSERT_PASSED(this->validate_object_map
, image
);
3634 TYPED_TEST(DiffIterateTest
, DiffIterateRegression6926
)
3636 librados::IoCtx ioctx
;
3637 ASSERT_EQ(0, this->_rados
.ioctx_create(this->m_pool_name
.c_str(), ioctx
));
3640 librbd::Image image
;
3642 std::string name
= this->get_temp_image_name();
3643 uint64_t size
= 20 << 20;
3645 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3646 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3648 uint64_t object_size
= 0;
3649 if (this->whole_object
) {
3650 object_size
= 1 << order
;
3652 vector
<diff_extent
> extents
;
3653 ceph::bufferlist bl
;
3655 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, true, this->whole_object
,
3656 vector_iterate_cb
, (void *) &extents
));
3657 ASSERT_EQ(0u, extents
.size());
3659 ASSERT_EQ(0, image
.snap_create("snap1"));
3661 memset(data
, 1, sizeof(data
));
3662 bl
.append(data
, 256);
3663 ASSERT_EQ(256, image
.write(0, 256, bl
));
3666 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, true, this->whole_object
,
3667 vector_iterate_cb
, (void *) &extents
));
3668 ASSERT_EQ(1u, extents
.size());
3669 ASSERT_EQ(diff_extent(0, 256, true, object_size
), extents
[0]);
3671 ASSERT_EQ(0, image
.snap_set("snap1"));
3673 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, true, this->whole_object
,
3674 vector_iterate_cb
, (void *) &extents
));
3675 ASSERT_EQ(static_cast<size_t>(0), extents
.size());
3678 TYPED_TEST(DiffIterateTest
, DiffIterateIgnoreParent
)
3680 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
3682 librados::IoCtx ioctx
;
3683 ASSERT_EQ(0, this->_rados
.ioctx_create(this->m_pool_name
.c_str(), ioctx
));
3685 bool skip_discard
= this->is_skip_partial_discard_enabled();
3688 librbd::Image image
;
3689 std::string name
= this->get_temp_image_name();
3690 uint64_t size
= 20 << 20;
3693 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3694 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3696 uint64_t object_size
= 0;
3697 if (this->whole_object
) {
3698 object_size
= 1 << order
;
3702 bl
.append(buffer::create(size
));
3704 interval_set
<uint64_t> one
;
3705 one
.insert(0, size
);
3706 ASSERT_EQ((int)size
, image
.write(0, size
, bl
));
3707 ASSERT_EQ(0, image
.snap_create("one"));
3708 ASSERT_EQ(0, image
.snap_protect("one"));
3710 std::string clone_name
= this->get_temp_image_name();
3711 ASSERT_EQ(0, rbd
.clone(ioctx
, name
.c_str(), "one", ioctx
, clone_name
.c_str(),
3712 RBD_FEATURE_LAYERING
, &order
));
3713 ASSERT_EQ(0, rbd
.open(ioctx
, image
, clone_name
.c_str(), NULL
));
3715 interval_set
<uint64_t> exists
;
3716 interval_set
<uint64_t> two
;
3717 scribble(image
, 10, 102400, skip_discard
, &exists
, &two
);
3718 two
= round_diff_interval(two
, object_size
);
3719 cout
<< " wrote " << two
<< " to clone" << std::endl
;
3721 interval_set
<uint64_t> diff
;
3722 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, false, this->whole_object
,
3723 iterate_cb
, (void *)&diff
));
3724 cout
<< " diff was " << diff
<< std::endl
;
3725 if (!this->whole_object
) {
3726 ASSERT_FALSE(one
.subset_of(diff
));
3728 ASSERT_TRUE(two
.subset_of(diff
));
3731 TYPED_TEST(DiffIterateTest
, DiffIterateCallbackError
)
3733 librados::IoCtx ioctx
;
3734 ASSERT_EQ(0, this->_rados
.ioctx_create(this->m_pool_name
.c_str(), ioctx
));
3736 bool skip_discard
= this->is_skip_partial_discard_enabled();
3740 librbd::Image image
;
3742 std::string name
= this->get_temp_image_name();
3743 uint64_t size
= 20 << 20;
3745 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3746 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3748 interval_set
<uint64_t> exists
;
3749 interval_set
<uint64_t> one
;
3750 scribble(image
, 10, 102400, skip_discard
, &exists
, &one
);
3751 cout
<< " wrote " << one
<< std::endl
;
3753 interval_set
<uint64_t> diff
;
3754 ASSERT_EQ(-EINVAL
, image
.diff_iterate2(NULL
, 0, size
, true,
3756 iterate_error_cb
, NULL
));
3761 TYPED_TEST(DiffIterateTest
, DiffIterateParentDiscard
)
3763 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
3765 librados::IoCtx ioctx
;
3766 ASSERT_EQ(0, this->_rados
.ioctx_create(this->m_pool_name
.c_str(), ioctx
));
3768 bool skip_discard
= this->is_skip_partial_discard_enabled();
3771 librbd::Image image
;
3772 std::string name
= this->get_temp_image_name();
3773 uint64_t size
= 20 << 20;
3776 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
3777 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
3779 uint64_t object_size
= 0;
3780 if (this->whole_object
) {
3781 object_size
= 1 << order
;
3784 interval_set
<uint64_t> exists
;
3785 interval_set
<uint64_t> one
;
3786 scribble(image
, 10, 102400, skip_discard
, &exists
, &one
);
3787 ASSERT_EQ(0, image
.snap_create("one"));
3789 ASSERT_EQ(1 << order
, image
.discard(0, 1 << order
));
3790 ASSERT_EQ(0, image
.snap_create("two"));
3791 ASSERT_EQ(0, image
.snap_protect("two"));
3795 std::string clone_name
= this->get_temp_image_name();
3796 ASSERT_EQ(0, rbd
.clone(ioctx
, name
.c_str(), "two", ioctx
,
3797 clone_name
.c_str(), RBD_FEATURE_LAYERING
, &order
));
3798 ASSERT_EQ(0, rbd
.open(ioctx
, image
, clone_name
.c_str(), NULL
));
3800 interval_set
<uint64_t> two
;
3801 scribble(image
, 10, 102400, skip_discard
, &exists
, &two
);
3802 two
= round_diff_interval(two
, object_size
);
3804 interval_set
<uint64_t> diff
;
3805 ASSERT_EQ(0, image
.diff_iterate2(NULL
, 0, size
, true, this->whole_object
,
3806 iterate_cb
, (void *)&diff
));
3807 ASSERT_TRUE(two
.subset_of(diff
));
3810 TEST_F(TestLibRBD
, ZeroLengthWrite
)
3812 rados_ioctx_t ioctx
;
3813 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
3817 std::string name
= get_temp_image_name();
3818 uint64_t size
= 2 << 20;
3820 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
3821 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
3824 ASSERT_EQ(0, rbd_write(image
, 0, 0, NULL
));
3825 ASSERT_EQ(1, rbd_read(image
, 0, 1, read_data
));
3826 ASSERT_EQ('\0', read_data
[0]);
3828 ASSERT_PASSED(validate_object_map
, image
);
3829 ASSERT_EQ(0, rbd_close(image
));
3831 rados_ioctx_destroy(ioctx
);
3835 TEST_F(TestLibRBD
, ZeroLengthDiscard
)
3837 rados_ioctx_t ioctx
;
3838 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
3842 std::string name
= get_temp_image_name();
3843 uint64_t size
= 2 << 20;
3845 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
3846 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
3848 const char data
[] = "blah";
3849 char read_data
[sizeof(data
)];
3850 ASSERT_EQ((int)strlen(data
), rbd_write(image
, 0, strlen(data
), data
));
3851 ASSERT_EQ(0, rbd_discard(image
, 0, 0));
3852 ASSERT_EQ((int)strlen(data
), rbd_read(image
, 0, strlen(data
), read_data
));
3853 ASSERT_EQ(0, memcmp(data
, read_data
, strlen(data
)));
3855 ASSERT_PASSED(validate_object_map
, image
);
3856 ASSERT_EQ(0, rbd_close(image
));
3858 rados_ioctx_destroy(ioctx
);
3861 TEST_F(TestLibRBD
, ZeroLengthRead
)
3863 rados_ioctx_t ioctx
;
3864 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
3868 std::string name
= get_temp_image_name();
3869 uint64_t size
= 2 << 20;
3871 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
3872 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
3875 ASSERT_EQ(0, rbd_read(image
, 0, 0, read_data
));
3877 ASSERT_EQ(0, rbd_close(image
));
3879 rados_ioctx_destroy(ioctx
);
3882 TEST_F(TestLibRBD
, LargeCacheRead
)
3884 std::string config_value
;
3885 ASSERT_EQ(0, _rados
.conf_get("rbd_cache", config_value
));
3886 if (config_value
== "false") {
3887 std::cout
<< "SKIPPING due to disabled cache" << std::endl
;
3891 rados_ioctx_t ioctx
;
3892 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
3894 uint32_t new_cache_size
= 1 << 20;
3895 std::string orig_cache_size
;
3896 ASSERT_EQ(0, _rados
.conf_get("rbd_cache_size", orig_cache_size
));
3897 ASSERT_EQ(0, _rados
.conf_set("rbd_cache_size",
3898 stringify(new_cache_size
).c_str()));
3899 ASSERT_EQ(0, _rados
.conf_get("rbd_cache_size", config_value
));
3900 ASSERT_EQ(stringify(new_cache_size
), config_value
);
3901 BOOST_SCOPE_EXIT( (orig_cache_size
) ) {
3902 ASSERT_EQ(0, _rados
.conf_set("rbd_cache_size", orig_cache_size
.c_str()));
3903 } BOOST_SCOPE_EXIT_END
;
3907 std::string name
= get_temp_image_name();
3908 uint64_t size
= 1 << order
;
3910 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
3911 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
3913 std::string
buffer(1 << order
, '1');
3915 ASSERT_EQ(static_cast<ssize_t
>(buffer
.size()),
3916 rbd_write(image
, 0, buffer
.size(), buffer
.c_str()));
3918 ASSERT_EQ(0, rbd_invalidate_cache(image
));
3920 ASSERT_EQ(static_cast<ssize_t
>(buffer
.size()),
3921 rbd_read(image
, 0, buffer
.size(), &buffer
[0]));
3923 ASSERT_EQ(0, rbd_close(image
));
3925 rados_ioctx_destroy(ioctx
);
3928 TEST_F(TestLibRBD
, TestPendingAio
)
3930 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
3932 rados_ioctx_t ioctx
;
3933 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
3940 ASSERT_EQ(0, get_features(&old_format
, &features
));
3941 ASSERT_FALSE(old_format
);
3943 std::string name
= get_temp_image_name();
3945 uint64_t size
= 4 << 20;
3946 ASSERT_EQ(0, create_image_full(ioctx
, name
.c_str(), size
, &order
,
3948 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
3950 char test_data
[TEST_IO_SIZE
];
3951 for (size_t i
= 0; i
< TEST_IO_SIZE
; ++i
) {
3952 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
3955 size_t num_aios
= 256;
3956 rbd_completion_t comps
[num_aios
];
3957 for (size_t i
= 0; i
< num_aios
; ++i
) {
3958 ASSERT_EQ(0, rbd_aio_create_completion(NULL
, NULL
, &comps
[i
]));
3959 uint64_t offset
= rand() % (size
- TEST_IO_SIZE
);
3960 ASSERT_EQ(0, rbd_aio_write(image
, offset
, TEST_IO_SIZE
, test_data
,
3963 for (size_t i
= 0; i
< num_aios
; ++i
) {
3964 ASSERT_EQ(0, rbd_aio_wait_for_complete(comps
[i
]));
3965 rbd_aio_release(comps
[i
]);
3967 ASSERT_EQ(0, rbd_invalidate_cache(image
));
3969 for (size_t i
= 0; i
< num_aios
; ++i
) {
3970 ASSERT_EQ(0, rbd_aio_create_completion(NULL
, NULL
, &comps
[i
]));
3971 uint64_t offset
= rand() % (size
- TEST_IO_SIZE
);
3972 ASSERT_LE(0, rbd_aio_read(image
, offset
, TEST_IO_SIZE
, test_data
,
3976 ASSERT_PASSED(validate_object_map
, image
);
3977 ASSERT_EQ(0, rbd_close(image
));
3978 for (size_t i
= 0; i
< num_aios
; ++i
) {
3979 ASSERT_EQ(1, rbd_aio_is_complete(comps
[i
]));
3980 rbd_aio_release(comps
[i
]);
3983 rados_ioctx_destroy(ioctx
);
3986 TEST_F(TestLibRBD
, Flatten
)
3988 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
3990 librados::IoCtx ioctx
;
3991 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
3994 std::string parent_name
= get_temp_image_name();
3995 uint64_t size
= 2 << 20;
3997 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, parent_name
.c_str(), size
, &order
));
3999 librbd::Image parent_image
;
4000 ASSERT_EQ(0, rbd
.open(ioctx
, parent_image
, parent_name
.c_str(), NULL
));
4003 bl
.append(std::string(4096, '1'));
4004 ASSERT_EQ((ssize_t
)bl
.length(), parent_image
.write(0, bl
.length(), bl
));
4006 ASSERT_EQ(0, parent_image
.snap_create("snap1"));
4007 ASSERT_EQ(0, parent_image
.snap_protect("snap1"));
4010 ASSERT_EQ(0, parent_image
.features(&features
));
4012 std::string clone_name
= get_temp_image_name();
4013 EXPECT_EQ(0, rbd
.clone(ioctx
, parent_name
.c_str(), "snap1", ioctx
,
4014 clone_name
.c_str(), features
, &order
));
4016 librbd::Image clone_image
;
4017 ASSERT_EQ(0, rbd
.open(ioctx
, clone_image
, clone_name
.c_str(), NULL
));
4018 ASSERT_EQ(0, clone_image
.flatten());
4020 librbd::RBD::AioCompletion
*read_comp
=
4021 new librbd::RBD::AioCompletion(NULL
, NULL
);
4023 clone_image
.aio_read(0, bl
.length(), read_bl
, read_comp
);
4024 ASSERT_EQ(0, read_comp
->wait_for_complete());
4025 ASSERT_EQ((ssize_t
)bl
.length(), read_comp
->get_return_value());
4026 read_comp
->release();
4027 ASSERT_TRUE(bl
.contents_equal(read_bl
));
4029 ASSERT_PASSED(validate_object_map
, clone_image
);
4032 TEST_F(TestLibRBD
, SnapshotLimit
)
4034 rados_ioctx_t ioctx
;
4035 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
4039 std::string name
= get_temp_image_name();
4040 uint64_t size
= 2 << 20;
4043 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
4044 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
4046 ASSERT_EQ(0, rbd_snap_get_limit(image
, &limit
));
4047 ASSERT_EQ(UINT64_MAX
, limit
);
4048 ASSERT_EQ(0, rbd_snap_set_limit(image
, 2));
4049 ASSERT_EQ(0, rbd_snap_get_limit(image
, &limit
));
4050 ASSERT_EQ(2U, limit
);
4052 ASSERT_EQ(0, rbd_snap_create(image
, "snap1"));
4053 ASSERT_EQ(0, rbd_snap_create(image
, "snap2"));
4054 ASSERT_EQ(-EDQUOT
, rbd_snap_create(image
, "snap3"));
4055 ASSERT_EQ(0, rbd_snap_set_limit(image
, UINT64_MAX
));
4056 ASSERT_EQ(0, rbd_snap_create(image
, "snap3"));
4057 ASSERT_EQ(0, rbd_close(image
));
4059 rados_ioctx_destroy(ioctx
);
4063 TEST_F(TestLibRBD
, SnapshotLimitPP
)
4065 librados::IoCtx ioctx
;
4066 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4070 librbd::Image image
;
4071 std::string name
= get_temp_image_name();
4072 uint64_t size
= 2 << 20;
4076 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4077 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
4079 ASSERT_EQ(0, image
.snap_get_limit(&limit
));
4080 ASSERT_EQ(UINT64_MAX
, limit
);
4081 ASSERT_EQ(0, image
.snap_set_limit(2));
4082 ASSERT_EQ(0, image
.snap_get_limit(&limit
));
4083 ASSERT_EQ(2U, limit
);
4085 ASSERT_EQ(0, image
.snap_create("snap1"));
4086 ASSERT_EQ(0, image
.snap_create("snap2"));
4087 ASSERT_EQ(-EDQUOT
, image
.snap_create("snap3"));
4088 ASSERT_EQ(0, image
.snap_set_limit(UINT64_MAX
));
4089 ASSERT_EQ(0, image
.snap_create("snap3"));
4095 TEST_F(TestLibRBD
, RebuildObjectMapViaLockOwner
)
4097 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
| RBD_FEATURE_OBJECT_MAP
);
4099 librados::IoCtx ioctx
;
4100 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4103 std::string name
= get_temp_image_name();
4104 uint64_t size
= 2 << 20;
4106 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4108 std::string object_map_oid
;
4110 librbd::Image image
;
4111 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
4113 std::string image_id
;
4114 ASSERT_EQ(0, get_image_id(image
, &image_id
));
4115 object_map_oid
= RBD_OBJECT_MAP_PREFIX
+ image_id
;
4118 // corrupt the object map
4121 ASSERT_EQ(0, ioctx
.write(object_map_oid
, bl
, bl
.length(), 0));
4123 librbd::Image image1
;
4124 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4128 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4129 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4130 ASSERT_TRUE(lock_owner
);
4133 ASSERT_EQ(0, image1
.get_flags(&flags
));
4134 ASSERT_TRUE((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) != 0);
4136 librbd::Image image2
;
4137 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
4138 ASSERT_EQ(0, image2
.is_exclusive_lock_owner(&lock_owner
));
4139 ASSERT_FALSE(lock_owner
);
4141 PrintProgress prog_ctx
;
4142 ASSERT_EQ(0, image2
.rebuild_object_map(prog_ctx
));
4143 ASSERT_PASSED(validate_object_map
, image1
);
4144 ASSERT_PASSED(validate_object_map
, image2
);
4147 TEST_F(TestLibRBD
, RenameViaLockOwner
)
4149 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
| RBD_FEATURE_EXCLUSIVE_LOCK
);
4151 librados::IoCtx ioctx
;
4152 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4155 std::string name
= get_temp_image_name();
4156 uint64_t size
= 2 << 20;
4158 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4160 librbd::Image image1
;
4161 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4164 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4167 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4168 ASSERT_TRUE(lock_owner
);
4170 std::string new_name
= get_temp_image_name();
4171 ASSERT_EQ(0, rbd
.rename(ioctx
, name
.c_str(), new_name
.c_str()));
4172 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4173 ASSERT_TRUE(lock_owner
);
4175 librbd::Image image2
;
4176 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, new_name
.c_str(), NULL
));
4179 TEST_F(TestLibRBD
, SnapCreateViaLockOwner
)
4181 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
| RBD_FEATURE_EXCLUSIVE_LOCK
);
4183 librados::IoCtx ioctx
;
4184 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4187 std::string name
= get_temp_image_name();
4188 uint64_t size
= 2 << 20;
4190 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4192 librbd::Image image1
;
4193 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4195 // switch to writeback cache
4196 ASSERT_EQ(0, image1
.flush());
4199 bl
.append(std::string(4096, '1'));
4200 ASSERT_EQ((ssize_t
)bl
.length(), image1
.write(0, bl
.length(), bl
));
4203 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4204 ASSERT_TRUE(lock_owner
);
4206 librbd::Image image2
;
4207 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
4209 ASSERT_EQ(0, image2
.is_exclusive_lock_owner(&lock_owner
));
4210 ASSERT_FALSE(lock_owner
);
4212 ASSERT_EQ(0, image2
.snap_create("snap1"));
4214 ASSERT_EQ(0, image1
.snap_exists2("snap1", &exists
));
4215 ASSERT_TRUE(exists
);
4216 ASSERT_EQ(0, image2
.snap_exists2("snap1", &exists
));
4217 ASSERT_TRUE(exists
);
4219 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4220 ASSERT_TRUE(lock_owner
);
4223 TEST_F(TestLibRBD
, SnapRemoveViaLockOwner
)
4225 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
| RBD_FEATURE_EXCLUSIVE_LOCK
);
4227 librados::IoCtx ioctx
;
4228 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4231 std::string name
= get_temp_image_name();
4232 uint64_t size
= 2 << 20;
4234 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4236 librbd::Image image1
;
4237 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4240 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4241 ASSERT_EQ(0, image1
.snap_create("snap1"));
4244 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4245 ASSERT_TRUE(lock_owner
);
4247 librbd::Image image2
;
4248 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
4250 ASSERT_EQ(0, image2
.is_exclusive_lock_owner(&lock_owner
));
4251 ASSERT_FALSE(lock_owner
);
4253 ASSERT_EQ(0, image2
.snap_remove("snap1"));
4255 ASSERT_EQ(0, image1
.snap_exists2("snap1", &exists
));
4256 ASSERT_FALSE(exists
);
4257 ASSERT_EQ(0, image2
.snap_exists2("snap1", &exists
));
4258 ASSERT_FALSE(exists
);
4260 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4261 ASSERT_TRUE(lock_owner
);
4264 TEST_F(TestLibRBD
, SnapRemove2
)
4266 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
4268 librados::IoCtx ioctx
;
4269 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4272 std::string name
= get_temp_image_name();
4273 uint64_t size
= 2 << 20;
4275 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4277 librbd::Image image1
;
4278 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4281 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4282 ASSERT_EQ(0, image1
.snap_create("snap1"));
4284 ASSERT_EQ(0, image1
.snap_exists2("snap1", &exists
));
4285 ASSERT_TRUE(exists
);
4286 ASSERT_EQ(0, image1
.snap_protect("snap1"));
4288 ASSERT_EQ(0, image1
.snap_is_protected("snap1", &is_protected
));
4289 ASSERT_TRUE(is_protected
);
4292 ASSERT_EQ(0, image1
.features(&features
));
4294 std::string child_name
= get_temp_image_name();
4295 EXPECT_EQ(0, rbd
.clone(ioctx
, name
.c_str(), "snap1", ioctx
,
4296 child_name
.c_str(), features
, &order
));
4298 ASSERT_EQ(0, image1
.snap_exists2("snap1", &exists
));
4299 ASSERT_TRUE(exists
);
4300 ASSERT_EQ(0, image1
.snap_is_protected("snap1", &is_protected
));
4301 ASSERT_TRUE(is_protected
);
4303 ASSERT_EQ(-EBUSY
, image1
.snap_remove("snap1"));
4305 ASSERT_EQ(0, image1
.snap_remove2("snap1", RBD_SNAP_REMOVE_FORCE
, pp
));
4306 ASSERT_EQ(0, image1
.snap_exists2("snap1", &exists
));
4307 ASSERT_FALSE(exists
);
4310 TEST_F(TestLibRBD
, SnapRenameViaLockOwner
)
4312 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
| RBD_FEATURE_EXCLUSIVE_LOCK
);
4314 librados::IoCtx ioctx
;
4315 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4318 std::string name
= get_temp_image_name();
4319 uint64_t size
= 2 << 20;
4321 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4323 librbd::Image image1
;
4324 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4327 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4328 ASSERT_EQ(0, image1
.snap_create("snap1"));
4331 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4332 ASSERT_TRUE(lock_owner
);
4334 librbd::Image image2
;
4335 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
4337 ASSERT_EQ(0, image2
.is_exclusive_lock_owner(&lock_owner
));
4338 ASSERT_FALSE(lock_owner
);
4340 ASSERT_EQ(0, image2
.snap_rename("snap1", "snap1-rename"));
4342 ASSERT_EQ(0, image1
.snap_exists2("snap1-rename", &exists
));
4343 ASSERT_TRUE(exists
);
4344 ASSERT_EQ(0, image2
.snap_exists2("snap1-rename", &exists
));
4345 ASSERT_TRUE(exists
);
4347 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4348 ASSERT_TRUE(lock_owner
);
4351 TEST_F(TestLibRBD
, SnapProtectViaLockOwner
)
4353 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
| RBD_FEATURE_EXCLUSIVE_LOCK
);
4355 librados::IoCtx ioctx
;
4356 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4359 std::string name
= get_temp_image_name();
4360 uint64_t size
= 2 << 20;
4362 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4364 librbd::Image image1
;
4365 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4368 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4371 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4372 ASSERT_TRUE(lock_owner
);
4373 ASSERT_EQ(0, image1
.snap_create("snap1"));
4375 librbd::Image image2
;
4376 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
4378 ASSERT_EQ(0, image2
.is_exclusive_lock_owner(&lock_owner
));
4379 ASSERT_FALSE(lock_owner
);
4381 ASSERT_EQ(0, image2
.snap_protect("snap1"));
4383 ASSERT_EQ(0, image2
.snap_is_protected("snap1", &is_protected
));
4384 ASSERT_TRUE(is_protected
);
4385 ASSERT_EQ(0, image1
.snap_is_protected("snap1", &is_protected
));
4386 ASSERT_TRUE(is_protected
);
4388 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4389 ASSERT_TRUE(lock_owner
);
4392 TEST_F(TestLibRBD
, SnapUnprotectViaLockOwner
)
4394 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
| RBD_FEATURE_EXCLUSIVE_LOCK
);
4396 librados::IoCtx ioctx
;
4397 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4400 std::string name
= get_temp_image_name();
4401 uint64_t size
= 2 << 20;
4403 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4405 librbd::Image image1
;
4406 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4409 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4412 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4413 ASSERT_TRUE(lock_owner
);
4414 ASSERT_EQ(0, image1
.snap_create("snap1"));
4415 ASSERT_EQ(0, image1
.snap_protect("snap1"));
4417 ASSERT_EQ(0, image1
.snap_is_protected("snap1", &is_protected
));
4418 ASSERT_TRUE(is_protected
);
4420 librbd::Image image2
;
4421 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
4423 ASSERT_EQ(0, image2
.is_exclusive_lock_owner(&lock_owner
));
4424 ASSERT_FALSE(lock_owner
);
4426 ASSERT_EQ(0, image2
.snap_unprotect("snap1"));
4427 ASSERT_EQ(0, image2
.snap_is_protected("snap1", &is_protected
));
4428 ASSERT_FALSE(is_protected
);
4429 ASSERT_EQ(0, image1
.snap_is_protected("snap1", &is_protected
));
4430 ASSERT_FALSE(is_protected
);
4432 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4433 ASSERT_TRUE(lock_owner
);
4436 TEST_F(TestLibRBD
, FlattenViaLockOwner
)
4438 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
4440 librados::IoCtx ioctx
;
4441 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4444 std::string parent_name
= get_temp_image_name();
4445 uint64_t size
= 2 << 20;
4447 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, parent_name
.c_str(), size
, &order
));
4449 librbd::Image parent_image
;
4450 ASSERT_EQ(0, rbd
.open(ioctx
, parent_image
, parent_name
.c_str(), NULL
));
4451 ASSERT_EQ(0, parent_image
.snap_create("snap1"));
4452 ASSERT_EQ(0, parent_image
.snap_protect("snap1"));
4455 ASSERT_EQ(0, parent_image
.features(&features
));
4457 std::string name
= get_temp_image_name();
4458 EXPECT_EQ(0, rbd
.clone(ioctx
, parent_name
.c_str(), "snap1", ioctx
,
4459 name
.c_str(), features
, &order
));
4461 librbd::Image image1
;
4462 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4465 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4468 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4469 ASSERT_TRUE(lock_owner
);
4471 librbd::Image image2
;
4472 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
4474 ASSERT_EQ(0, image2
.is_exclusive_lock_owner(&lock_owner
));
4475 ASSERT_FALSE(lock_owner
);
4477 ASSERT_EQ(0, image2
.flatten());
4479 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4480 ASSERT_TRUE(lock_owner
);
4481 ASSERT_PASSED(validate_object_map
, image1
);
4484 TEST_F(TestLibRBD
, ResizeViaLockOwner
)
4486 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
4488 librados::IoCtx ioctx
;
4489 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4492 std::string name
= get_temp_image_name();
4493 uint64_t size
= 2 << 20;
4495 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4497 librbd::Image image1
;
4498 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4501 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4504 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4505 ASSERT_TRUE(lock_owner
);
4507 librbd::Image image2
;
4508 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
4510 ASSERT_EQ(0, image2
.is_exclusive_lock_owner(&lock_owner
));
4511 ASSERT_FALSE(lock_owner
);
4513 ASSERT_EQ(0, image2
.resize(0));
4515 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4516 ASSERT_TRUE(lock_owner
);
4517 ASSERT_PASSED(validate_object_map
, image1
);
4520 TEST_F(TestLibRBD
, ObjectMapConsistentSnap
)
4522 REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP
);
4524 librados::IoCtx ioctx
;
4525 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4528 std::string name
= get_temp_image_name();
4529 uint64_t size
= 1 << 20;
4531 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4533 librbd::Image image1
;
4534 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4537 for (int i
= 0; i
< num_snaps
; ++i
) {
4538 std::string snap_name
= "snap" + stringify(i
);
4539 ASSERT_EQ(0, image1
.snap_create(snap_name
.c_str()));
4543 thread
writer([&image1
](){
4544 librbd::image_info_t info
;
4545 int r
= image1
.stat(info
, sizeof(info
));
4549 for (unsigned i
= 0; i
< info
.num_objs
; ++i
) {
4550 r
= image1
.write((1 << info
.order
) * i
, bl
.length(), bl
);
4551 assert(r
== (int) bl
.length());
4556 for (int i
= 0; i
< num_snaps
; ++i
) {
4557 std::string snap_name
= "snap" + stringify(i
);
4558 ASSERT_EQ(0, image1
.snap_set(snap_name
.c_str()));
4559 ASSERT_PASSED(validate_object_map
, image1
);
4562 ASSERT_EQ(0, image1
.snap_set(NULL
));
4563 ASSERT_PASSED(validate_object_map
, image1
);
4566 void memset_rand(char *buf
, size_t len
) {
4567 for (size_t i
= 0; i
< len
; ++i
) {
4568 buf
[i
] = (char) (rand() % (126 - 33) + 33);
4572 TEST_F(TestLibRBD
, Metadata
)
4574 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
4576 rados_ioctx_t ioctx
;
4577 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
4579 std::string name
= get_temp_image_name();
4580 uint64_t size
= 2 << 20;
4582 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
4585 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
4588 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image1
, NULL
));
4592 size_t keys_len
= sizeof(keys
);
4593 size_t vals_len
= sizeof(vals
);
4595 memset_rand(keys
, keys_len
);
4596 memset_rand(vals
, vals_len
);
4598 ASSERT_EQ(0, rbd_metadata_list(image
, "", 0, keys
, &keys_len
, vals
,
4600 ASSERT_EQ(0U, keys_len
);
4601 ASSERT_EQ(0U, vals_len
);
4604 size_t value_len
= sizeof(value
);
4605 memset_rand(value
, value_len
);
4607 ASSERT_EQ(0, rbd_metadata_set(image1
, "key1", "value1"));
4608 ASSERT_EQ(0, rbd_metadata_set(image1
, "key2", "value2"));
4609 ASSERT_EQ(0, rbd_metadata_get(image1
, "key1", value
, &value_len
));
4610 ASSERT_STREQ(value
, "value1");
4612 ASSERT_EQ(-ERANGE
, rbd_metadata_get(image1
, "key1", value
, &value_len
));
4613 ASSERT_EQ(value_len
, strlen("value1") + 1);
4615 ASSERT_EQ(-ERANGE
, rbd_metadata_list(image1
, "", 0, keys
, &keys_len
, vals
,
4617 keys_len
= sizeof(keys
);
4618 vals_len
= sizeof(vals
);
4619 memset_rand(keys
, keys_len
);
4620 memset_rand(vals
, vals_len
);
4621 ASSERT_EQ(0, rbd_metadata_list(image1
, "", 0, keys
, &keys_len
, vals
,
4623 ASSERT_EQ(keys_len
, strlen("key1") + 1 + strlen("key2") + 1);
4624 ASSERT_EQ(vals_len
, strlen("value1") + 1 + strlen("value2") + 1);
4625 ASSERT_STREQ(keys
, "key1");
4626 ASSERT_STREQ(keys
+ strlen(keys
) + 1, "key2");
4627 ASSERT_STREQ(vals
, "value1");
4628 ASSERT_STREQ(vals
+ strlen(vals
) + 1, "value2");
4630 ASSERT_EQ(0, rbd_metadata_remove(image1
, "key1"));
4631 ASSERT_EQ(-ENOENT
, rbd_metadata_remove(image1
, "key3"));
4632 value_len
= sizeof(value
);
4633 ASSERT_EQ(-ENOENT
, rbd_metadata_get(image1
, "key3", value
, &value_len
));
4634 ASSERT_EQ(0, rbd_metadata_list(image1
, "", 0, keys
, &keys_len
, vals
,
4636 ASSERT_EQ(keys_len
, strlen("key2") + 1);
4637 ASSERT_EQ(vals_len
, strlen("value2") + 1);
4638 ASSERT_STREQ(keys
, "key2");
4639 ASSERT_STREQ(vals
, "value2");
4641 // test config setting
4642 ASSERT_EQ(0, rbd_metadata_set(image1
, "conf_rbd_cache", "false"));
4643 ASSERT_EQ(-EINVAL
, rbd_metadata_set(image1
, "conf_rbd_cache", "INVALID_VAL"));
4644 ASSERT_EQ(0, rbd_metadata_remove(image1
, "conf_rbd_cache"));
4646 // test metadata with snapshot adding
4647 ASSERT_EQ(0, rbd_snap_create(image1
, "snap1"));
4648 ASSERT_EQ(0, rbd_snap_protect(image1
, "snap1"));
4649 ASSERT_EQ(0, rbd_snap_set(image1
, "snap1"));
4651 ASSERT_EQ(0, rbd_metadata_set(image1
, "key1", "value1"));
4652 ASSERT_EQ(0, rbd_metadata_set(image1
, "key3", "value3"));
4654 keys_len
= sizeof(keys
);
4655 vals_len
= sizeof(vals
);
4656 memset_rand(keys
, keys_len
);
4657 memset_rand(vals
, vals_len
);
4658 ASSERT_EQ(0, rbd_metadata_list(image1
, "", 0, keys
, &keys_len
, vals
,
4661 strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") + 1);
4663 strlen("value1") + 1 + strlen("value2") + 1 + strlen("value3") + 1);
4664 ASSERT_STREQ(keys
, "key1");
4665 ASSERT_STREQ(keys
+ strlen("key1") + 1, "key2");
4666 ASSERT_STREQ(keys
+ strlen("key1") + 1 + strlen("key2") + 1, "key3");
4667 ASSERT_STREQ(vals
, "value1");
4668 ASSERT_STREQ(vals
+ strlen("value1") + 1, "value2");
4669 ASSERT_STREQ(vals
+ strlen("value1") + 1 + strlen("value2") + 1, "value3");
4671 ASSERT_EQ(0, rbd_snap_set(image1
, NULL
));
4672 keys_len
= sizeof(keys
);
4673 vals_len
= sizeof(vals
);
4674 memset_rand(keys
, keys_len
);
4675 memset_rand(vals
, vals_len
);
4676 ASSERT_EQ(0, rbd_metadata_list(image1
, "", 0, keys
, &keys_len
, vals
,
4679 strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") + 1);
4681 strlen("value1") + 1 + strlen("value2") + 1 + strlen("value3") + 1);
4682 ASSERT_STREQ(keys
, "key1");
4683 ASSERT_STREQ(keys
+ strlen("key1") + 1, "key2");
4684 ASSERT_STREQ(keys
+ strlen("key1") + 1 + strlen("key2") + 1, "key3");
4685 ASSERT_STREQ(vals
, "value1");
4686 ASSERT_STREQ(vals
+ strlen("value1") + 1, "value2");
4687 ASSERT_STREQ(vals
+ strlen("value1") + 1 + strlen("value2") + 1, "value3");
4689 // test metadata with cloning
4691 ASSERT_EQ(0, rbd_get_features(image1
, &features
));
4693 string cname
= get_temp_image_name();
4694 EXPECT_EQ(0, rbd_clone(ioctx
, name
.c_str(), "snap1", ioctx
,
4695 cname
.c_str(), features
, &order
));
4697 ASSERT_EQ(0, rbd_open(ioctx
, cname
.c_str(), &image2
, NULL
));
4698 ASSERT_EQ(0, rbd_metadata_set(image2
, "key4", "value4"));
4700 keys_len
= sizeof(keys
);
4701 vals_len
= sizeof(vals
);
4702 memset_rand(keys
, keys_len
);
4703 memset_rand(vals
, vals_len
);
4704 ASSERT_EQ(0, rbd_metadata_list(image2
, "", 0, keys
, &keys_len
, vals
,
4706 ASSERT_EQ(keys_len
, strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") +
4707 1 + strlen("key4") + 1);
4708 ASSERT_EQ(vals_len
, strlen("value1") + 1 + strlen("value2") + 1 +
4709 strlen("value3") + 1 + strlen("value4") + 1);
4710 ASSERT_STREQ(keys
+ strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") +
4712 ASSERT_STREQ(vals
+ strlen("value1") + 1 + strlen("value2") + 1 +
4713 strlen("value3") + 1, "value4");
4715 ASSERT_EQ(0, rbd_metadata_list(image1
, "", 0, keys
, &keys_len
, vals
,
4718 strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") + 1);
4720 strlen("value1") + 1 + strlen("value2") + 1 + strlen("value3") + 1);
4721 ASSERT_EQ(-ENOENT
, rbd_metadata_get(image1
, "key4", value
, &value_len
));
4723 // test short buffer cases
4724 keys_len
= strlen("key1") + 1;
4725 vals_len
= strlen("value1") + 1;
4726 memset_rand(keys
, keys_len
);
4727 memset_rand(vals
, vals_len
);
4728 ASSERT_EQ(0, rbd_metadata_list(image2
, "", 1, keys
, &keys_len
, vals
,
4730 ASSERT_EQ(keys_len
, strlen("key1") + 1);
4731 ASSERT_EQ(vals_len
, strlen("value1") + 1);
4732 ASSERT_STREQ(keys
, "key1");
4733 ASSERT_STREQ(vals
, "value1");
4735 ASSERT_EQ(-ERANGE
, rbd_metadata_list(image2
, "", 2, keys
, &keys_len
, vals
,
4737 ASSERT_EQ(keys_len
, strlen("key1") + 1 + strlen("key2") + 1);
4738 ASSERT_EQ(vals_len
, strlen("value1") + 1 + strlen("value2") + 1);
4740 ASSERT_EQ(-ERANGE
, rbd_metadata_list(image2
, "", 0, keys
, &keys_len
, vals
,
4742 ASSERT_EQ(keys_len
, strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") +
4743 1 + strlen("key4") + 1);
4744 ASSERT_EQ(vals_len
, strlen("value1") + 1 + strlen("value2") + 1 +
4745 strlen("value3") + 1 + strlen("value4") + 1);
4747 // test `start` param
4748 keys_len
= sizeof(keys
);
4749 vals_len
= sizeof(vals
);
4750 memset_rand(keys
, keys_len
);
4751 memset_rand(vals
, vals_len
);
4752 ASSERT_EQ(0, rbd_metadata_list(image2
, "key2", 0, keys
, &keys_len
, vals
,
4754 ASSERT_EQ(keys_len
, strlen("key3") + 1 + strlen("key4") + 1);
4755 ASSERT_EQ(vals_len
, strlen("value3") + 1 + strlen("value4") + 1);
4756 ASSERT_STREQ(keys
, "key3");
4757 ASSERT_STREQ(vals
, "value3");
4759 ASSERT_EQ(0, rbd_close(image
));
4760 ASSERT_EQ(0, rbd_close(image1
));
4761 ASSERT_EQ(0, rbd_close(image2
));
4762 rados_ioctx_destroy(ioctx
);
4765 TEST_F(TestLibRBD
, MetadataPP
)
4767 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
4769 librados::IoCtx ioctx
;
4770 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4773 string name
= get_temp_image_name();
4774 uint64_t size
= 2 << 20;
4778 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4780 librbd::Image image1
;
4781 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4782 map
<string
, bufferlist
> pairs
;
4783 ASSERT_EQ(0, image1
.metadata_list("", 0, &pairs
));
4784 ASSERT_TRUE(pairs
.empty());
4786 ASSERT_EQ(0, image1
.metadata_set("key1", "value1"));
4787 ASSERT_EQ(0, image1
.metadata_set("key2", "value2"));
4788 ASSERT_EQ(0, image1
.metadata_get("key1", &value
));
4789 ASSERT_EQ(0, strcmp("value1", value
.c_str()));
4790 ASSERT_EQ(0, image1
.metadata_list("", 0, &pairs
));
4791 ASSERT_EQ(2U, pairs
.size());
4792 ASSERT_EQ(0, strncmp("value1", pairs
["key1"].c_str(), 6));
4793 ASSERT_EQ(0, strncmp("value2", pairs
["key2"].c_str(), 6));
4796 ASSERT_EQ(0, image1
.metadata_remove("key1"));
4797 ASSERT_EQ(-ENOENT
, image1
.metadata_remove("key3"));
4798 ASSERT_TRUE(image1
.metadata_get("key3", &value
) < 0);
4799 ASSERT_EQ(0, image1
.metadata_list("", 0, &pairs
));
4800 ASSERT_EQ(1U, pairs
.size());
4801 ASSERT_EQ(0, strncmp("value2", pairs
["key2"].c_str(), 6));
4803 // test config setting
4804 ASSERT_EQ(0, image1
.metadata_set("conf_rbd_cache", "false"));
4805 ASSERT_EQ(-EINVAL
, image1
.metadata_set("conf_rbd_cache", "INVALID_VALUE"));
4806 ASSERT_EQ(0, image1
.metadata_remove("conf_rbd_cache"));
4808 // test metadata with snapshot adding
4809 ASSERT_EQ(0, image1
.snap_create("snap1"));
4810 ASSERT_EQ(0, image1
.snap_protect("snap1"));
4811 ASSERT_EQ(0, image1
.snap_set("snap1"));
4814 ASSERT_EQ(0, image1
.metadata_set("key1", "value1"));
4815 ASSERT_EQ(0, image1
.metadata_set("key3", "value3"));
4816 ASSERT_EQ(0, image1
.metadata_list("", 0, &pairs
));
4817 ASSERT_EQ(3U, pairs
.size());
4818 ASSERT_EQ(0, strncmp("value1", pairs
["key1"].c_str(), 6));
4819 ASSERT_EQ(0, strncmp("value2", pairs
["key2"].c_str(), 6));
4820 ASSERT_EQ(0, strncmp("value3", pairs
["key3"].c_str(), 6));
4822 ASSERT_EQ(0, image1
.snap_set(NULL
));
4823 ASSERT_EQ(0, image1
.metadata_list("", 0, &pairs
));
4824 ASSERT_EQ(3U, pairs
.size());
4825 ASSERT_EQ(0, strncmp("value1", pairs
["key1"].c_str(), 6));
4826 ASSERT_EQ(0, strncmp("value2", pairs
["key2"].c_str(), 6));
4827 ASSERT_EQ(0, strncmp("value3", pairs
["key3"].c_str(), 6));
4829 // test metadata with cloning
4830 string cname
= get_temp_image_name();
4831 librbd::Image image2
;
4832 ASSERT_EQ(0, image1
.features(&features
));
4833 EXPECT_EQ(0, rbd
.clone(ioctx
, name
.c_str(), "snap1", ioctx
,
4834 cname
.c_str(), features
, &order
));
4835 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, cname
.c_str(), NULL
));
4836 ASSERT_EQ(0, image2
.metadata_set("key4", "value4"));
4838 ASSERT_EQ(0, image2
.metadata_list("", 0, &pairs
));
4839 ASSERT_EQ(4U, pairs
.size());
4841 ASSERT_EQ(0, image1
.metadata_list("", 0, &pairs
));
4842 ASSERT_EQ(3U, pairs
.size());
4843 ASSERT_EQ(-ENOENT
, image1
.metadata_get("key4", &value
));
4846 TEST_F(TestLibRBD
, UpdateFeatures
)
4848 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
4850 librados::IoCtx ioctx
;
4851 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4854 std::string name
= get_temp_image_name();
4855 uint64_t size
= 1 << 20;
4857 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4859 librbd::Image image
;
4860 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
4863 ASSERT_EQ(0, image
.old_format(&old_format
));
4865 ASSERT_EQ(-EINVAL
, image
.update_features(RBD_FEATURE_EXCLUSIVE_LOCK
, true));
4870 ASSERT_EQ(0, image
.features(&features
));
4872 // must provide a single feature
4873 ASSERT_EQ(-EINVAL
, image
.update_features(0, true));
4875 uint64_t disable_features
;
4876 disable_features
= features
& (RBD_FEATURE_EXCLUSIVE_LOCK
|
4877 RBD_FEATURE_OBJECT_MAP
|
4878 RBD_FEATURE_FAST_DIFF
|
4879 RBD_FEATURE_JOURNALING
);
4880 if (disable_features
!= 0) {
4881 ASSERT_EQ(0, image
.update_features(disable_features
, false));
4884 ASSERT_EQ(0, image
.features(&features
));
4885 ASSERT_EQ(0U, features
& disable_features
);
4887 // cannot enable object map nor journaling w/o exclusive lock
4888 ASSERT_EQ(-EINVAL
, image
.update_features(RBD_FEATURE_OBJECT_MAP
, true));
4889 ASSERT_EQ(-EINVAL
, image
.update_features(RBD_FEATURE_JOURNALING
, true));
4890 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_EXCLUSIVE_LOCK
, true));
4892 ASSERT_EQ(0, image
.features(&features
));
4893 ASSERT_NE(0U, features
& RBD_FEATURE_EXCLUSIVE_LOCK
);
4895 // cannot enable fast diff w/o object map
4896 ASSERT_EQ(-EINVAL
, image
.update_features(RBD_FEATURE_FAST_DIFF
, true));
4897 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_OBJECT_MAP
, true));
4898 ASSERT_EQ(0, image
.features(&features
));
4899 ASSERT_NE(0U, features
& RBD_FEATURE_OBJECT_MAP
);
4901 uint64_t expected_flags
= RBD_FLAG_OBJECT_MAP_INVALID
;
4903 ASSERT_EQ(0, image
.get_flags(&flags
));
4904 ASSERT_EQ(expected_flags
, flags
);
4906 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_OBJECT_MAP
, false));
4907 ASSERT_EQ(0, image
.features(&features
));
4908 ASSERT_EQ(0U, features
& RBD_FEATURE_OBJECT_MAP
);
4910 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_OBJECT_MAP
|
4911 RBD_FEATURE_FAST_DIFF
|
4912 RBD_FEATURE_JOURNALING
, true));
4914 expected_flags
= RBD_FLAG_OBJECT_MAP_INVALID
| RBD_FLAG_FAST_DIFF_INVALID
;
4915 ASSERT_EQ(0, image
.get_flags(&flags
));
4916 ASSERT_EQ(expected_flags
, flags
);
4918 // cannot disable object map w/ fast diff
4919 ASSERT_EQ(-EINVAL
, image
.update_features(RBD_FEATURE_OBJECT_MAP
, false));
4920 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_FAST_DIFF
, false));
4921 ASSERT_EQ(0, image
.features(&features
));
4922 ASSERT_EQ(0U, features
& RBD_FEATURE_FAST_DIFF
);
4924 expected_flags
= RBD_FLAG_OBJECT_MAP_INVALID
;
4925 ASSERT_EQ(0, image
.get_flags(&flags
));
4926 ASSERT_EQ(expected_flags
, flags
);
4928 // cannot disable exclusive lock w/ object map
4929 ASSERT_EQ(-EINVAL
, image
.update_features(RBD_FEATURE_EXCLUSIVE_LOCK
, false));
4930 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_OBJECT_MAP
, false));
4932 // cannot disable exclusive lock w/ journaling
4933 ASSERT_EQ(-EINVAL
, image
.update_features(RBD_FEATURE_EXCLUSIVE_LOCK
, false));
4934 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_JOURNALING
, false));
4936 ASSERT_EQ(0, image
.get_flags(&flags
));
4937 ASSERT_EQ(0U, flags
);
4939 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_EXCLUSIVE_LOCK
, false));
4941 ASSERT_EQ(0, image
.features(&features
));
4942 if ((features
& RBD_FEATURE_DEEP_FLATTEN
) != 0) {
4943 ASSERT_EQ(0, image
.update_features(RBD_FEATURE_DEEP_FLATTEN
, false));
4945 ASSERT_EQ(-EINVAL
, image
.update_features(RBD_FEATURE_DEEP_FLATTEN
, true));
4948 TEST_F(TestLibRBD
, RebuildObjectMap
)
4950 librados::IoCtx ioctx
;
4951 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
4954 std::string name
= get_temp_image_name();
4955 uint64_t size
= 1 << 20;
4957 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
4959 PrintProgress prog_ctx
;
4960 std::string object_map_oid
;
4964 librbd::Image image
;
4965 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
4968 ASSERT_EQ(0, image
.features(&features
));
4969 if ((features
& RBD_FEATURE_OBJECT_MAP
) == 0) {
4970 ASSERT_EQ(-EINVAL
, image
.rebuild_object_map(prog_ctx
));
4974 ASSERT_EQ((ssize_t
)bl
.length(), image
.write(0, bl
.length(), bl
));
4976 ASSERT_EQ(0, image
.snap_create("snap1"));
4977 ASSERT_EQ((ssize_t
)bl
.length(), image
.write(1<<order
, bl
.length(), bl
));
4979 std::string image_id
;
4980 ASSERT_EQ(0, get_image_id(image
, &image_id
));
4981 object_map_oid
= RBD_OBJECT_MAP_PREFIX
+ image_id
;
4984 // corrupt the object map
4985 ASSERT_EQ(0, ioctx
.write(object_map_oid
, bl
, bl
.length(), 0));
4987 librbd::Image image1
;
4988 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
4992 ASSERT_EQ(0, image1
.write(0, 0, bl
));
4993 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
4994 ASSERT_TRUE(lock_owner
);
4997 ASSERT_EQ(0, image1
.get_flags(&flags
));
4998 ASSERT_TRUE((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) != 0);
5000 ASSERT_EQ(0, image1
.rebuild_object_map(prog_ctx
));
5002 librbd::Image image2
;
5003 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
5006 ASSERT_EQ((ssize_t
)bl
.length(), image2
.read(0, bl
.length(), read_bl
));
5007 ASSERT_TRUE(bl
.contents_equal(read_bl
));
5010 ASSERT_EQ((ssize_t
)bl
.length(), image2
.read(1<<order
, bl
.length(), read_bl
));
5011 ASSERT_TRUE(bl
.contents_equal(read_bl
));
5013 ASSERT_PASSED(validate_object_map
, image1
);
5014 ASSERT_PASSED(validate_object_map
, image2
);
5017 TEST_F(TestLibRBD
, RebuildNewObjectMap
)
5019 REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP
);
5021 rados_ioctx_t ioctx
;
5022 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
5024 std::string name
= get_temp_image_name();
5025 uint64_t size
= 1 << 20;
5027 uint64_t features
= RBD_FEATURE_EXCLUSIVE_LOCK
;
5028 ASSERT_EQ(0, create_image_full(ioctx
, name
.c_str(), size
, &order
,
5032 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
5033 ASSERT_EQ(0, rbd_update_features(image
, RBD_FEATURE_OBJECT_MAP
, true));
5034 ASSERT_EQ(0, rbd_rebuild_object_map(image
, print_progress_percent
, NULL
));
5036 ASSERT_PASSED(validate_object_map
, image
);
5038 ASSERT_EQ(0, rbd_close(image
));
5039 rados_ioctx_destroy(ioctx
);
5042 TEST_F(TestLibRBD
, CheckObjectMap
)
5044 REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP
);
5046 librados::IoCtx ioctx
;
5047 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5050 std::string name
= get_temp_image_name();
5051 uint64_t size
= 1 << 20;
5053 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
5055 PrintProgress prog_ctx
;
5060 librbd::Image image
;
5061 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
5064 ASSERT_EQ(0, image
.features(&features
));
5066 ASSERT_EQ((ssize_t
)bl1
.length(), image
.write(0, bl1
.length(), bl1
));
5068 ASSERT_EQ(0, image
.snap_create("snap1"));
5069 ASSERT_EQ((ssize_t
)bl1
.length(), image
.write(1<<order
, bl1
.length(), bl1
));
5072 librbd::Image image1
;
5073 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
5075 std::string image_id
;
5076 ASSERT_EQ(0, get_image_id(image1
, &image_id
));
5078 std::string object_map_oid
= RBD_OBJECT_MAP_PREFIX
+ image_id
;
5080 ASSERT_LT(0, ioctx
.read(object_map_oid
, bl2
, 1024, 0));
5083 ASSERT_EQ((ssize_t
)bl1
.length(), image1
.write(3 * (1 << 18), bl1
.length(), bl1
));
5084 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
5085 ASSERT_TRUE(lock_owner
);
5087 //reopen image to reread now corrupt object map from disk
5091 ASSERT_LT(0, ioctx
.read(object_map_oid
, bl1
, 1024, 0));
5092 ASSERT_FALSE(bl1
.contents_equal(bl2
));
5094 ASSERT_EQ(0, ioctx
.write_full(object_map_oid
, bl2
));
5095 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
5098 ASSERT_EQ(0, image1
.get_flags(&flags
));
5099 ASSERT_TRUE((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) == 0);
5101 ASSERT_EQ(0, image1
.check_object_map(prog_ctx
));
5103 ASSERT_EQ(0, image1
.get_flags(&flags
));
5104 ASSERT_TRUE((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) != 0);
5107 TEST_F(TestLibRBD
, BlockingAIO
)
5109 librados::IoCtx ioctx
;
5110 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5112 bool skip_discard
= is_skip_partial_discard_enabled();
5115 std::string name
= get_temp_image_name();
5116 uint64_t size
= 1 << 20;
5118 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
5120 std::string non_blocking_aio
;
5121 ASSERT_EQ(0, _rados
.conf_get("rbd_non_blocking_aio", non_blocking_aio
));
5122 ASSERT_EQ(0, _rados
.conf_set("rbd_non_blocking_aio", "0"));
5123 BOOST_SCOPE_EXIT( (non_blocking_aio
) ) {
5124 ASSERT_EQ(0, _rados
.conf_set("rbd_non_blocking_aio",
5125 non_blocking_aio
.c_str()));
5126 } BOOST_SCOPE_EXIT_END
;
5128 librbd::Image image
;
5129 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
5132 ASSERT_EQ(0, image
.write(0, bl
.length(), bl
));
5134 bl
.append(std::string(256, '1'));
5135 librbd::RBD::AioCompletion
*write_comp
=
5136 new librbd::RBD::AioCompletion(NULL
, NULL
);
5137 ASSERT_EQ(0, image
.aio_write(0, bl
.length(), bl
, write_comp
));
5139 librbd::RBD::AioCompletion
*flush_comp
=
5140 new librbd::RBD::AioCompletion(NULL
, NULL
);
5141 ASSERT_EQ(0, image
.aio_flush(flush_comp
));
5142 ASSERT_EQ(0, flush_comp
->wait_for_complete());
5143 ASSERT_EQ(0, flush_comp
->get_return_value());
5144 flush_comp
->release();
5146 ASSERT_EQ(1, write_comp
->is_complete());
5147 ASSERT_EQ(0, write_comp
->get_return_value());
5148 write_comp
->release();
5150 librbd::RBD::AioCompletion
*discard_comp
=
5151 new librbd::RBD::AioCompletion(NULL
, NULL
);
5152 ASSERT_EQ(0, image
.aio_discard(128, 128, discard_comp
));
5153 ASSERT_EQ(0, discard_comp
->wait_for_complete());
5154 discard_comp
->release();
5156 librbd::RBD::AioCompletion
*read_comp
=
5157 new librbd::RBD::AioCompletion(NULL
, NULL
);
5159 image
.aio_read(0, bl
.length(), read_bl
, read_comp
);
5160 ASSERT_EQ(0, read_comp
->wait_for_complete());
5161 ASSERT_EQ((ssize_t
)bl
.length(), read_comp
->get_return_value());
5162 read_comp
->release();
5164 bufferlist expected_bl
;
5165 expected_bl
.append(std::string(128, '1'));
5166 expected_bl
.append(std::string(128, skip_discard
? '1' : '\0'));
5167 ASSERT_TRUE(expected_bl
.contents_equal(read_bl
));
5170 TEST_F(TestLibRBD
, ExclusiveLockTransition
)
5172 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
5174 librados::IoCtx ioctx
;
5175 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5178 std::string name
= get_temp_image_name();
5180 uint64_t size
= 1 << 18;
5182 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
5184 librbd::Image image1
;
5185 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
5187 librbd::Image image2
;
5188 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
5190 std::list
<librbd::RBD::AioCompletion
*> comps
;
5191 ceph::bufferlist bl
;
5192 bl
.append(std::string(1 << order
, '1'));
5193 for (size_t object_no
= 0; object_no
< (size
>> 12); ++object_no
) {
5194 librbd::RBD::AioCompletion
*comp
= new librbd::RBD::AioCompletion(NULL
,
5196 comps
.push_back(comp
);
5197 if (object_no
% 2 == 0) {
5198 ASSERT_EQ(0, image1
.aio_write(object_no
<< order
, bl
.length(), bl
, comp
));
5200 ASSERT_EQ(0, image2
.aio_write(object_no
<< order
, bl
.length(), bl
, comp
));
5204 while (!comps
.empty()) {
5205 librbd::RBD::AioCompletion
*comp
= comps
.front();
5207 ASSERT_EQ(0, comp
->wait_for_complete());
5208 ASSERT_EQ(1, comp
->is_complete());
5212 librbd::Image image3
;
5213 ASSERT_EQ(0, rbd
.open(ioctx
, image3
, name
.c_str(), NULL
));
5214 for (size_t object_no
= 0; object_no
< (size
>> 12); ++object_no
) {
5216 ASSERT_EQ((ssize_t
)bl
.length(), image3
.read(object_no
<< order
, bl
.length(),
5218 ASSERT_TRUE(bl
.contents_equal(read_bl
));
5221 ASSERT_PASSED(validate_object_map
, image1
);
5222 ASSERT_PASSED(validate_object_map
, image2
);
5223 ASSERT_PASSED(validate_object_map
, image3
);
5226 TEST_F(TestLibRBD
, ExclusiveLockReadTransition
)
5228 REQUIRE_FEATURE(RBD_FEATURE_JOURNALING
);
5230 librados::IoCtx ioctx
;
5231 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5234 std::string name
= get_temp_image_name();
5236 uint64_t size
= 1 << 18;
5238 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
5240 librbd::Image image1
;
5241 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
5244 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
5245 ASSERT_FALSE(lock_owner
);
5247 // journaling should force read ops to acquire the lock
5249 ASSERT_EQ(0, image1
.read(0, 0, read_bl
));
5251 ASSERT_EQ(0, image1
.is_exclusive_lock_owner(&lock_owner
));
5252 ASSERT_TRUE(lock_owner
);
5254 librbd::Image image2
;
5255 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
5257 std::list
<librbd::RBD::AioCompletion
*> comps
;
5258 std::list
<bufferlist
> read_bls
;
5259 for (size_t object_no
= 0; object_no
< (size
>> 12); ++object_no
) {
5260 librbd::RBD::AioCompletion
*comp
= new librbd::RBD::AioCompletion(NULL
,
5262 comps
.push_back(comp
);
5263 read_bls
.emplace_back();
5264 if (object_no
% 2 == 0) {
5265 ASSERT_EQ(0, image1
.aio_read(object_no
<< order
, 1 << order
, read_bls
.back(), comp
));
5267 ASSERT_EQ(0, image2
.aio_read(object_no
<< order
, 1 << order
, read_bls
.back(), comp
));
5271 while (!comps
.empty()) {
5272 librbd::RBD::AioCompletion
*comp
= comps
.front();
5274 ASSERT_EQ(0, comp
->wait_for_complete());
5275 ASSERT_EQ(1, comp
->is_complete());
5280 TEST_F(TestLibRBD
, CacheMayCopyOnWrite
) {
5281 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
5283 librados::IoCtx ioctx
;
5284 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5287 std::string name
= get_temp_image_name();
5289 uint64_t size
= 1 << 18;
5291 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
5293 librbd::Image image
;
5294 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
5295 ASSERT_EQ(0, image
.snap_create("one"));
5296 ASSERT_EQ(0, image
.snap_protect("one"));
5298 std::string clone_name
= this->get_temp_image_name();
5299 ASSERT_EQ(0, rbd
.clone(ioctx
, name
.c_str(), "one", ioctx
, clone_name
.c_str(),
5300 RBD_FEATURE_LAYERING
, &order
));
5302 librbd::Image clone
;
5303 ASSERT_EQ(0, rbd
.open(ioctx
, clone
, clone_name
.c_str(), NULL
));
5304 ASSERT_EQ(0, clone
.flush());
5306 bufferlist expect_bl
;
5307 expect_bl
.append(std::string(1024, '\0'));
5309 // test double read path
5311 uint64_t offset
= 0;
5312 ASSERT_EQ(1024, clone
.read(offset
+ 2048, 1024, read_bl
));
5313 ASSERT_TRUE(expect_bl
.contents_equal(read_bl
));
5315 bufferlist write_bl
;
5316 write_bl
.append(std::string(1024, '1'));
5317 ASSERT_EQ(1024, clone
.write(offset
, write_bl
.length(), write_bl
));
5320 ASSERT_EQ(1024, clone
.read(offset
+ 2048, 1024, read_bl
));
5321 ASSERT_TRUE(expect_bl
.contents_equal(read_bl
));
5323 // test read retry path
5324 offset
= 1 << order
;
5325 ASSERT_EQ(1024, clone
.write(offset
, write_bl
.length(), write_bl
));
5328 ASSERT_EQ(1024, clone
.read(offset
+ 2048, 1024, read_bl
));
5329 ASSERT_TRUE(expect_bl
.contents_equal(read_bl
));
5332 TEST_F(TestLibRBD
, FlushEmptyOpsOnExternalSnapshot
) {
5333 std::string cache_enabled
;
5334 ASSERT_EQ(0, _rados
.conf_get("rbd_cache", cache_enabled
));
5335 ASSERT_EQ(0, _rados
.conf_set("rbd_cache", "false"));
5336 BOOST_SCOPE_EXIT( (cache_enabled
) ) {
5337 ASSERT_EQ(0, _rados
.conf_set("rbd_cache", cache_enabled
.c_str()));
5338 } BOOST_SCOPE_EXIT_END
;
5340 librados::IoCtx ioctx
;
5341 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5344 std::string name
= get_temp_image_name();
5345 uint64_t size
= 1 << 18;
5347 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
5349 librbd::Image image1
;
5350 librbd::Image image2
;
5351 ASSERT_EQ(0, rbd
.open(ioctx
, image1
, name
.c_str(), NULL
));
5352 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, name
.c_str(), NULL
));
5353 ASSERT_EQ(0, image1
.snap_create("snap1"));
5355 librbd::RBD::AioCompletion
*read_comp
=
5356 new librbd::RBD::AioCompletion(NULL
, NULL
);
5358 image2
.aio_read(0, 1024, read_bl
, read_comp
);
5359 ASSERT_EQ(0, read_comp
->wait_for_complete());
5360 read_comp
->release();
5363 TEST_F(TestLibRBD
, TestImageOptions
)
5365 rados_ioctx_t ioctx
;
5366 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
5368 //make create image options
5369 uint64_t features
= RBD_FEATURE_LAYERING
| RBD_FEATURE_STRIPINGV2
;
5371 uint64_t stripe_unit
= IMAGE_STRIPE_UNIT
;
5372 uint64_t stripe_count
= IMAGE_STRIPE_COUNT
;
5373 rbd_image_options_t opts
;
5374 rbd_image_options_create(&opts
);
5377 ASSERT_EQ(-EINVAL
, rbd_image_options_is_set(opts
, 12345, &is_set
));
5378 ASSERT_EQ(0, rbd_image_options_is_set(opts
, RBD_IMAGE_OPTION_FORMAT
,
5380 ASSERT_FALSE(is_set
);
5382 ASSERT_EQ(0, rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_FORMAT
,
5384 ASSERT_EQ(0, rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_FEATURES
,
5386 ASSERT_EQ(0, rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_ORDER
,
5388 ASSERT_EQ(0, rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_STRIPE_UNIT
,
5390 ASSERT_EQ(0, rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_STRIPE_COUNT
,
5393 ASSERT_EQ(0, rbd_image_options_is_set(opts
, RBD_IMAGE_OPTION_FORMAT
,
5395 ASSERT_TRUE(is_set
);
5397 std::string parent_name
= get_temp_image_name();
5400 ASSERT_EQ(0, rbd_create4(ioctx
, parent_name
.c_str(), 4<<20, opts
));
5402 // check order is returned in opts
5403 ASSERT_EQ(0, rbd_image_options_get_uint64(opts
, RBD_IMAGE_OPTION_ORDER
,
5405 ASSERT_NE((uint64_t)0, order
);
5407 // write some data to parent
5409 ASSERT_EQ(0, rbd_open(ioctx
, parent_name
.c_str(), &parent
, NULL
));
5410 char *data
= (char *)"testdata";
5411 ASSERT_EQ((ssize_t
)strlen(data
), rbd_write(parent
, 0, strlen(data
), data
));
5412 ASSERT_EQ((ssize_t
)strlen(data
), rbd_write(parent
, 12, strlen(data
), data
));
5414 // create a snapshot, reopen as the parent we're interested in
5415 ASSERT_EQ(0, rbd_snap_create(parent
, "parent_snap"));
5416 ASSERT_EQ(0, rbd_close(parent
));
5417 ASSERT_EQ(0, rbd_open(ioctx
, parent_name
.c_str(), &parent
, "parent_snap"));
5420 std::string child_name
= get_temp_image_name();
5421 ASSERT_EQ(0, rbd_snap_protect(parent
, "parent_snap"));
5422 ASSERT_EQ(0, rbd_clone3(ioctx
, parent_name
.c_str(), "parent_snap", ioctx
,
5423 child_name
.c_str(), opts
));
5426 std::string copy1_name
= get_temp_image_name();
5427 ASSERT_EQ(0, rbd_copy3(parent
, ioctx
, copy1_name
.c_str(), opts
));
5428 std::string copy2_name
= get_temp_image_name();
5429 ASSERT_EQ(0, rbd_copy_with_progress3(parent
, ioctx
, copy2_name
.c_str(), opts
,
5430 print_progress_percent
, NULL
));
5432 ASSERT_EQ(0, rbd_close(parent
));
5434 rbd_image_options_destroy(opts
);
5436 rados_ioctx_destroy(ioctx
);
5439 TEST_F(TestLibRBD
, TestImageOptionsPP
)
5441 librados::IoCtx ioctx
;
5442 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5444 //make create image options
5445 uint64_t features
= RBD_FEATURE_LAYERING
| RBD_FEATURE_STRIPINGV2
;
5447 uint64_t stripe_unit
= IMAGE_STRIPE_UNIT
;
5448 uint64_t stripe_count
= IMAGE_STRIPE_COUNT
;
5449 librbd::ImageOptions opts
;
5450 ASSERT_EQ(0, opts
.set(RBD_IMAGE_OPTION_FORMAT
, static_cast<uint64_t>(2)));
5451 ASSERT_EQ(0, opts
.set(RBD_IMAGE_OPTION_FEATURES
, features
));
5452 ASSERT_EQ(0, opts
.set(RBD_IMAGE_OPTION_ORDER
, order
));
5453 ASSERT_EQ(0, opts
.set(RBD_IMAGE_OPTION_STRIPE_UNIT
, stripe_unit
));
5454 ASSERT_EQ(0, opts
.set(RBD_IMAGE_OPTION_STRIPE_COUNT
, stripe_count
));
5457 std::string parent_name
= get_temp_image_name();
5460 ASSERT_EQ(0, rbd
.create4(ioctx
, parent_name
.c_str(), 4<<20, opts
));
5462 // check order is returned in opts
5463 ASSERT_EQ(0, opts
.get(RBD_IMAGE_OPTION_ORDER
, &order
));
5464 ASSERT_NE((uint64_t)0, order
);
5466 // write some data to parent
5467 librbd::Image parent
;
5468 ASSERT_EQ(0, rbd
.open(ioctx
, parent
, parent_name
.c_str(), NULL
));
5472 bl
.append(buffer::create(len
));
5474 ASSERT_EQ(len
, parent
.write(0, len
, bl
));
5475 ASSERT_EQ(len
, parent
.write(len
, len
, bl
));
5477 // create a snapshot, reopen as the parent we're interested in
5478 ASSERT_EQ(0, parent
.snap_create("parent_snap"));
5479 ASSERT_EQ(0, parent
.close());
5480 ASSERT_EQ(0, rbd
.open(ioctx
, parent
, parent_name
.c_str(), "parent_snap"));
5483 std::string child_name
= get_temp_image_name();
5484 ASSERT_EQ(0, parent
.snap_protect("parent_snap"));
5485 ASSERT_EQ(0, rbd
.clone3(ioctx
, parent_name
.c_str(), "parent_snap", ioctx
,
5486 child_name
.c_str(), opts
));
5489 std::string copy1_name
= get_temp_image_name();
5490 ASSERT_EQ(0, parent
.copy3(ioctx
, copy1_name
.c_str(), opts
));
5491 std::string copy2_name
= get_temp_image_name();
5493 ASSERT_EQ(0, parent
.copy_with_progress3(ioctx
, copy2_name
.c_str(), opts
, pp
));
5495 ASSERT_EQ(0, parent
.close());
5498 TEST_F(TestLibRBD
, EventSocketPipe
)
5500 EventSocket event_sock
;
5501 int pipe_fd
[2]; // read and write fd
5504 ASSERT_EQ(0, pipe(pipe_fd
));
5506 ASSERT_FALSE(event_sock
.is_valid());
5508 ASSERT_EQ(-EINVAL
, event_sock
.init(pipe_fd
[1], EVENT_SOCKET_TYPE_NONE
));
5509 ASSERT_FALSE(event_sock
.is_valid());
5511 ASSERT_EQ(-EINVAL
, event_sock
.init(pipe_fd
[1], 44));
5512 ASSERT_FALSE(event_sock
.is_valid());
5514 #ifndef HAVE_EVENTFD
5515 ASSERT_EQ(-EINVAL
, event_sock
.init(pipe_fd
[1], EVENT_SOCKET_TYPE_EVENTFD
));
5516 ASSERT_FALSE(event_sock
.is_valid());
5519 ASSERT_EQ(0, event_sock
.init(pipe_fd
[1], EVENT_SOCKET_TYPE_PIPE
));
5520 ASSERT_TRUE(event_sock
.is_valid());
5521 ASSERT_EQ(0, event_sock
.notify());
5522 ASSERT_EQ(1, read(pipe_fd
[0], buf
, 32));
5523 ASSERT_EQ('i', buf
[0]);
5529 TEST_F(TestLibRBD
, EventSocketEventfd
)
5532 EventSocket event_sock
;
5534 struct pollfd poll_fd
;
5537 event_fd
= eventfd(0, EFD_NONBLOCK
);
5538 ASSERT_NE(-1, event_fd
);
5540 ASSERT_FALSE(event_sock
.is_valid());
5542 ASSERT_EQ(-EINVAL
, event_sock
.init(event_fd
, EVENT_SOCKET_TYPE_NONE
));
5543 ASSERT_FALSE(event_sock
.is_valid());
5545 ASSERT_EQ(-EINVAL
, event_sock
.init(event_fd
, 44));
5546 ASSERT_FALSE(event_sock
.is_valid());
5548 ASSERT_EQ(0, event_sock
.init(event_fd
, EVENT_SOCKET_TYPE_EVENTFD
));
5549 ASSERT_TRUE(event_sock
.is_valid());
5550 ASSERT_EQ(0, event_sock
.notify());
5552 poll_fd
.fd
= event_fd
;
5553 poll_fd
.events
= POLLIN
;
5554 ASSERT_EQ(1, poll(&poll_fd
, 1, -1));
5555 ASSERT_TRUE(poll_fd
.revents
& POLLIN
);
5557 ASSERT_EQ(static_cast<ssize_t
>(sizeof(uint64_t)), read(event_fd
, buf
, 32));
5558 ASSERT_EQ(1U, *reinterpret_cast<uint64_t *>(buf
));
5564 TEST_F(TestLibRBD
, ImagePollIO
)
5567 rados_ioctx_t ioctx
;
5568 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
5572 std::string name
= get_temp_image_name();
5573 uint64_t size
= 2 << 20;
5574 int fd
= eventfd(0, EFD_NONBLOCK
);
5576 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
5577 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
5579 ASSERT_EQ(0, rbd_set_image_notification(image
, fd
, EVENT_SOCKET_TYPE_EVENTFD
));
5581 char test_data
[TEST_IO_SIZE
+ 1];
5582 char zero_data
[TEST_IO_SIZE
+ 1];
5585 for (i
= 0; i
< TEST_IO_SIZE
; ++i
)
5586 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
5587 test_data
[TEST_IO_SIZE
] = '\0';
5588 memset(zero_data
, 0, sizeof(zero_data
));
5590 for (i
= 0; i
< 5; ++i
)
5591 ASSERT_PASSED(write_test_data
, image
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
5593 for (i
= 5; i
< 10; ++i
)
5594 ASSERT_PASSED(aio_write_test_data_and_poll
, image
, fd
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
5596 for (i
= 5; i
< 10; ++i
)
5597 ASSERT_PASSED(aio_read_test_data_and_poll
, image
, fd
, test_data
, TEST_IO_SIZE
* i
, TEST_IO_SIZE
, 0);
5599 ASSERT_EQ(0, rbd_close(image
));
5600 rados_ioctx_destroy(ioctx
);
5606 static bool operator==(const mirror_peer_t
&lhs
, const mirror_peer_t
&rhs
) {
5607 return (lhs
.uuid
== rhs
.uuid
&&
5608 lhs
.cluster_name
== rhs
.cluster_name
&&
5609 lhs
.client_name
== rhs
.client_name
);
5612 static std::ostream
& operator<<(std::ostream
&os
, const mirror_peer_t
&peer
) {
5613 os
<< "uuid=" << peer
.uuid
<< ", "
5614 << "cluster=" << peer
.cluster_name
<< ", "
5615 << "client=" << peer
.client_name
;
5619 } // namespace librbd
5621 TEST_F(TestLibRBD
, Mirror
) {
5622 librados::IoCtx ioctx
;
5623 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5627 std::vector
<librbd::mirror_peer_t
> expected_peers
;
5628 std::vector
<librbd::mirror_peer_t
> peers
;
5629 ASSERT_EQ(0, rbd
.mirror_peer_list(ioctx
, &peers
));
5630 ASSERT_EQ(expected_peers
, peers
);
5633 ASSERT_EQ(-EINVAL
, rbd
.mirror_peer_add(ioctx
, &uuid1
, "cluster1", "client"));
5635 rbd_mirror_mode_t mirror_mode
;
5636 ASSERT_EQ(0, rbd
.mirror_mode_get(ioctx
, &mirror_mode
));
5637 ASSERT_EQ(RBD_MIRROR_MODE_DISABLED
, mirror_mode
);
5639 ASSERT_EQ(0, rbd
.mirror_mode_set(ioctx
, RBD_MIRROR_MODE_IMAGE
));
5640 ASSERT_EQ(0, rbd
.mirror_mode_get(ioctx
, &mirror_mode
));
5642 // Add some images to the pool
5644 std::string parent_name
= get_temp_image_name();
5645 std::string child_name
= get_temp_image_name();
5646 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, parent_name
.c_str(), 2 << 20,
5650 ASSERT_EQ(0, get_features(&old_format
, &features
));
5651 if ((features
& RBD_FEATURE_LAYERING
) != 0) {
5652 librbd::Image parent
;
5653 ASSERT_EQ(0, rbd
.open(ioctx
, parent
, parent_name
.c_str(), NULL
));
5654 ASSERT_EQ(0, parent
.snap_create("parent_snap"));
5655 ASSERT_EQ(0, parent
.close());
5656 ASSERT_EQ(0, rbd
.open(ioctx
, parent
, parent_name
.c_str(), "parent_snap"));
5657 ASSERT_EQ(0, parent
.snap_protect("parent_snap"));
5658 ASSERT_EQ(0, parent
.close());
5659 ASSERT_EQ(0, rbd
.clone(ioctx
, parent_name
.c_str(), "parent_snap", ioctx
,
5660 child_name
.c_str(), features
, &order
));
5663 ASSERT_EQ(RBD_MIRROR_MODE_IMAGE
, mirror_mode
);
5665 ASSERT_EQ(0, rbd
.mirror_mode_set(ioctx
, RBD_MIRROR_MODE_POOL
));
5666 ASSERT_EQ(0, rbd
.mirror_mode_get(ioctx
, &mirror_mode
));
5667 ASSERT_EQ(RBD_MIRROR_MODE_POOL
, mirror_mode
);
5671 ASSERT_EQ(0, rbd
.mirror_peer_add(ioctx
, &uuid1
, "cluster1", "client"));
5672 ASSERT_EQ(0, rbd
.mirror_peer_add(ioctx
, &uuid2
, "cluster2", "admin"));
5673 ASSERT_EQ(-EEXIST
, rbd
.mirror_peer_add(ioctx
, &uuid3
, "cluster1", "foo"));
5674 ASSERT_EQ(0, rbd
.mirror_peer_add(ioctx
, &uuid3
, "cluster3", "admin"));
5676 ASSERT_EQ(0, rbd
.mirror_peer_list(ioctx
, &peers
));
5677 auto sort_peers
= [](const librbd::mirror_peer_t
&lhs
,
5678 const librbd::mirror_peer_t
&rhs
) {
5679 return lhs
.uuid
< rhs
.uuid
;
5682 {uuid1
, "cluster1", "client"},
5683 {uuid2
, "cluster2", "admin"},
5684 {uuid3
, "cluster3", "admin"}};
5685 std::sort(expected_peers
.begin(), expected_peers
.end(), sort_peers
);
5686 ASSERT_EQ(expected_peers
, peers
);
5688 ASSERT_EQ(0, rbd
.mirror_peer_remove(ioctx
, "uuid4"));
5689 ASSERT_EQ(0, rbd
.mirror_peer_remove(ioctx
, uuid2
));
5691 ASSERT_EQ(-ENOENT
, rbd
.mirror_peer_set_client(ioctx
, "uuid4", "new client"));
5692 ASSERT_EQ(0, rbd
.mirror_peer_set_client(ioctx
, uuid1
, "new client"));
5694 ASSERT_EQ(-ENOENT
, rbd
.mirror_peer_set_cluster(ioctx
, "uuid4",
5696 ASSERT_EQ(0, rbd
.mirror_peer_set_cluster(ioctx
, uuid3
, "new cluster"));
5698 ASSERT_EQ(0, rbd
.mirror_peer_list(ioctx
, &peers
));
5700 {uuid1
, "cluster1", "new client"},
5701 {uuid3
, "new cluster", "admin"}};
5702 std::sort(expected_peers
.begin(), expected_peers
.end(), sort_peers
);
5703 ASSERT_EQ(expected_peers
, peers
);
5705 ASSERT_EQ(-EBUSY
, rbd
.mirror_mode_set(ioctx
, RBD_MIRROR_MODE_DISABLED
));
5708 TEST_F(TestLibRBD
, FlushCacheWithCopyupOnExternalSnapshot
) {
5709 REQUIRE_FEATURE(RBD_FEATURE_LAYERING
);
5711 librados::IoCtx ioctx
;
5712 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5715 librbd::Image image
;
5716 std::string name
= get_temp_image_name();
5718 uint64_t size
= 1 << 18;
5721 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
5722 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
5725 bl
.append(std::string(size
, '1'));
5726 ASSERT_EQ((int)size
, image
.write(0, size
, bl
));
5727 ASSERT_EQ(0, image
.snap_create("one"));
5728 ASSERT_EQ(0, image
.snap_protect("one"));
5730 std::string clone_name
= this->get_temp_image_name();
5731 ASSERT_EQ(0, rbd
.clone(ioctx
, name
.c_str(), "one", ioctx
, clone_name
.c_str(),
5732 RBD_FEATURE_LAYERING
, &order
));
5733 ASSERT_EQ(0, rbd
.open(ioctx
, image
, clone_name
.c_str(), NULL
));
5735 librbd::Image image2
;
5736 ASSERT_EQ(0, rbd
.open(ioctx
, image2
, clone_name
.c_str(), NULL
));
5738 // prepare CoW writeback that will be flushed on next op
5740 bl
.append(std::string(1, '1'));
5741 ASSERT_EQ(0, image
.flush());
5742 ASSERT_EQ(1, image
.write(0, 1, bl
));
5743 ASSERT_EQ(0, image2
.snap_create("snap1"));
5745 librbd::RBD::AioCompletion
*read_comp
=
5746 new librbd::RBD::AioCompletion(NULL
, NULL
);
5748 image
.aio_read(0, 1024, read_bl
, read_comp
);
5749 ASSERT_EQ(0, read_comp
->wait_for_complete());
5750 read_comp
->release();
5753 TEST_F(TestLibRBD
, ExclusiveLock
)
5755 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
5757 static char buf
[10];
5759 rados_ioctx_t ioctx
;
5760 rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
);
5762 std::string name
= get_temp_image_name();
5763 uint64_t size
= 2 << 20;
5765 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
5768 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image1
, NULL
));
5771 ASSERT_EQ(0, rbd_lock_acquire(image1
, RBD_LOCK_MODE_EXCLUSIVE
));
5772 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1
, &lock_owner
));
5773 ASSERT_TRUE(lock_owner
);
5775 rbd_lock_mode_t lock_mode
;
5776 char *lock_owners
[1];
5777 size_t max_lock_owners
= 0;
5778 ASSERT_EQ(-ERANGE
, rbd_lock_get_owners(image1
, &lock_mode
, lock_owners
,
5780 ASSERT_EQ(1U, max_lock_owners
);
5782 max_lock_owners
= 2;
5783 ASSERT_EQ(0, rbd_lock_get_owners(image1
, &lock_mode
, lock_owners
,
5785 ASSERT_EQ(RBD_LOCK_MODE_EXCLUSIVE
, lock_mode
);
5786 ASSERT_STRNE("", lock_owners
[0]);
5787 ASSERT_EQ(1U, max_lock_owners
);
5790 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image2
, NULL
));
5792 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2
, &lock_owner
));
5793 ASSERT_FALSE(lock_owner
);
5795 ASSERT_EQ(-EOPNOTSUPP
, rbd_lock_break(image1
, RBD_LOCK_MODE_SHARED
, ""));
5796 ASSERT_EQ(-EBUSY
, rbd_lock_break(image1
, RBD_LOCK_MODE_EXCLUSIVE
,
5799 ASSERT_EQ(0, rbd_lock_release(image1
));
5800 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1
, &lock_owner
));
5801 ASSERT_FALSE(lock_owner
);
5803 ASSERT_EQ(-ENOENT
, rbd_lock_break(image1
, RBD_LOCK_MODE_EXCLUSIVE
,
5805 rbd_lock_get_owners_cleanup(lock_owners
, max_lock_owners
);
5807 ASSERT_EQ(-EROFS
, rbd_write(image1
, 0, sizeof(buf
), buf
));
5808 ASSERT_EQ((ssize_t
)sizeof(buf
), rbd_write(image2
, 0, sizeof(buf
), buf
));
5810 ASSERT_EQ(0, rbd_lock_acquire(image2
, RBD_LOCK_MODE_EXCLUSIVE
));
5811 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2
, &lock_owner
));
5812 ASSERT_TRUE(lock_owner
);
5814 ASSERT_EQ(0, rbd_lock_release(image2
));
5815 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2
, &lock_owner
));
5816 ASSERT_FALSE(lock_owner
);
5818 ASSERT_EQ(0, rbd_lock_acquire(image1
, RBD_LOCK_MODE_EXCLUSIVE
));
5819 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1
, &lock_owner
));
5820 ASSERT_TRUE(lock_owner
);
5822 ASSERT_EQ((ssize_t
)sizeof(buf
), rbd_write(image1
, 0, sizeof(buf
), buf
));
5823 ASSERT_EQ(-EROFS
, rbd_write(image2
, 0, sizeof(buf
), buf
));
5825 ASSERT_EQ(0, rbd_lock_release(image1
));
5826 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1
, &lock_owner
));
5827 ASSERT_FALSE(lock_owner
);
5831 const auto pingpong
= [&,this](int m_id
, rbd_image_t
&m_image
) {
5832 for (int i
= 0; i
< 10; i
++) {
5834 lock_guard
<mutex
> locker(lock
);
5835 if (owner_id
== m_id
) {
5836 std::cout
<< m_id
<< ": releasing exclusive lock" << std::endl
;
5837 EXPECT_EQ(0, rbd_lock_release(m_image
));
5839 EXPECT_EQ(0, rbd_is_exclusive_lock_owner(m_image
, &lock_owner
));
5840 EXPECT_FALSE(lock_owner
);
5842 std::cout
<< m_id
<< ": exclusive lock released" << std::endl
;
5847 std::cout
<< m_id
<< ": acquiring exclusive lock" << std::endl
;
5850 r
= rbd_lock_acquire(m_image
, RBD_LOCK_MODE_EXCLUSIVE
);
5854 } while (r
== -EROFS
);
5858 EXPECT_EQ(0, rbd_is_exclusive_lock_owner(m_image
, &lock_owner
));
5859 EXPECT_TRUE(lock_owner
);
5860 std::cout
<< m_id
<< ": exclusive lock acquired" << std::endl
;
5862 lock_guard
<mutex
> locker(lock
);
5865 usleep(rand() % 50000);
5868 lock_guard
<mutex
> locker(lock
);
5869 if (owner_id
== m_id
) {
5870 EXPECT_EQ(0, rbd_lock_release(m_image
));
5872 EXPECT_EQ(0, rbd_is_exclusive_lock_owner(m_image
, &lock_owner
));
5873 EXPECT_FALSE(lock_owner
);
5877 thread
ping(bind(pingpong
, 1, ref(image1
)));
5878 thread
pong(bind(pingpong
, 2, ref(image2
)));
5883 ASSERT_EQ(0, rbd_lock_acquire(image2
, RBD_LOCK_MODE_EXCLUSIVE
));
5884 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2
, &lock_owner
));
5885 ASSERT_TRUE(lock_owner
);
5887 ASSERT_EQ(0, rbd_close(image2
));
5889 ASSERT_EQ(0, rbd_lock_acquire(image1
, RBD_LOCK_MODE_EXCLUSIVE
));
5890 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1
, &lock_owner
));
5891 ASSERT_TRUE(lock_owner
);
5893 ASSERT_EQ(0, rbd_close(image1
));
5894 rados_ioctx_destroy(ioctx
);
5897 TEST_F(TestLibRBD
, BreakLock
)
5899 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK
);
5901 static char buf
[10];
5903 rados_t blacklist_cluster
;
5904 ASSERT_EQ("", connect_cluster(&blacklist_cluster
));
5906 rados_ioctx_t ioctx
, blacklist_ioctx
;
5907 ASSERT_EQ(0, rados_ioctx_create(_cluster
, m_pool_name
.c_str(), &ioctx
));
5908 ASSERT_EQ(0, rados_ioctx_create(blacklist_cluster
, m_pool_name
.c_str(),
5911 std::string name
= get_temp_image_name();
5912 uint64_t size
= 2 << 20;
5914 ASSERT_EQ(0, create_image(ioctx
, name
.c_str(), size
, &order
));
5916 rbd_image_t image
, blacklist_image
;
5917 ASSERT_EQ(0, rbd_open(ioctx
, name
.c_str(), &image
, NULL
));
5918 ASSERT_EQ(0, rbd_open(blacklist_ioctx
, name
.c_str(), &blacklist_image
, NULL
));
5920 ASSERT_EQ(0, rbd_metadata_set(image
, "rbd_blacklist_on_break_lock", "true"));
5921 ASSERT_EQ(0, rbd_lock_acquire(blacklist_image
, RBD_LOCK_MODE_EXCLUSIVE
));
5923 rbd_lock_mode_t lock_mode
;
5924 char *lock_owners
[1];
5925 size_t max_lock_owners
= 1;
5926 ASSERT_EQ(0, rbd_lock_get_owners(image
, &lock_mode
, lock_owners
,
5928 ASSERT_EQ(RBD_LOCK_MODE_EXCLUSIVE
, lock_mode
);
5929 ASSERT_STRNE("", lock_owners
[0]);
5930 ASSERT_EQ(1U, max_lock_owners
);
5932 ASSERT_EQ(0, rbd_lock_break(image
, RBD_LOCK_MODE_EXCLUSIVE
, lock_owners
[0]));
5933 ASSERT_EQ(0, rbd_lock_acquire(image
, RBD_LOCK_MODE_EXCLUSIVE
));
5934 EXPECT_EQ(0, rados_wait_for_latest_osdmap(blacklist_cluster
));
5936 ASSERT_EQ((ssize_t
)sizeof(buf
), rbd_write(image
, 0, sizeof(buf
), buf
));
5937 ASSERT_EQ(-EBLACKLISTED
, rbd_write(blacklist_image
, 0, sizeof(buf
), buf
));
5939 ASSERT_EQ(0, rbd_close(image
));
5940 ASSERT_EQ(0, rbd_close(blacklist_image
));
5942 rbd_lock_get_owners_cleanup(lock_owners
, max_lock_owners
);
5944 rados_ioctx_destroy(ioctx
);
5945 rados_ioctx_destroy(blacklist_ioctx
);
5946 rados_shutdown(blacklist_cluster
);
5949 TEST_F(TestLibRBD
, DiscardAfterWrite
)
5951 REQUIRE(!is_skip_partial_discard_enabled());
5953 librados::IoCtx ioctx
;
5954 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
5957 std::string name
= get_temp_image_name();
5958 uint64_t size
= 1 << 20;
5960 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
5962 librbd::Image image
;
5963 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), NULL
));
5965 // enable writeback cache
5966 ASSERT_EQ(0, image
.flush());
5969 bl
.append(std::string(256, '1'));
5971 librbd::RBD::AioCompletion
*write_comp
=
5972 new librbd::RBD::AioCompletion(NULL
, NULL
);
5973 ASSERT_EQ(0, image
.aio_write(0, bl
.length(), bl
, write_comp
));
5974 ASSERT_EQ(0, write_comp
->wait_for_complete());
5975 write_comp
->release();
5977 librbd::RBD::AioCompletion
*discard_comp
=
5978 new librbd::RBD::AioCompletion(NULL
, NULL
);
5979 ASSERT_EQ(0, image
.aio_discard(0, 256, discard_comp
));
5980 ASSERT_EQ(0, discard_comp
->wait_for_complete());
5981 discard_comp
->release();
5983 librbd::RBD::AioCompletion
*read_comp
=
5984 new librbd::RBD::AioCompletion(NULL
, NULL
);
5986 image
.aio_read(0, bl
.length(), read_bl
, read_comp
);
5987 ASSERT_EQ(0, read_comp
->wait_for_complete());
5988 ASSERT_EQ(bl
.length(), read_comp
->get_return_value());
5989 ASSERT_TRUE(read_bl
.is_zero());
5990 read_comp
->release();
5993 TEST_F(TestLibRBD
, DefaultFeatures
) {
5994 std::string orig_default_features
;
5995 ASSERT_EQ(0, _rados
.conf_get("rbd_default_features", orig_default_features
));
5996 BOOST_SCOPE_EXIT_ALL(orig_default_features
) {
5997 ASSERT_EQ(0, _rados
.conf_set("rbd_default_features",
5998 orig_default_features
.c_str()));
6001 std::list
<std::pair
<std::string
, std::string
> > feature_names_to_bitmask
= {
6002 {"", orig_default_features
},
6004 {"layering, exclusive-lock", "5"},
6005 {"exclusive-lock,journaling", "68"},
6009 for (auto &pair
: feature_names_to_bitmask
) {
6010 ASSERT_EQ(0, _rados
.conf_set("rbd_default_features", pair
.first
.c_str()));
6011 std::string features
;
6012 ASSERT_EQ(0, _rados
.conf_get("rbd_default_features", features
));
6013 ASSERT_EQ(pair
.second
, features
);
6017 TEST_F(TestLibRBD
, TestTrashMoveAndPurge
) {
6018 librados::IoCtx ioctx
;
6019 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
6022 std::string name
= get_temp_image_name();
6024 uint64_t size
= 1 << 18;
6026 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
6028 librbd::Image image
;
6029 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), nullptr));
6031 ASSERT_EQ(0, image
.old_format(&old_format
));
6034 ASSERT_EQ(-EOPNOTSUPP
, rbd
.trash_move(ioctx
, name
.c_str(), 0));
6038 std::string image_id
;
6039 ASSERT_EQ(0, image
.get_id(&image_id
));
6042 ASSERT_EQ(0, rbd
.trash_move(ioctx
, name
.c_str(), 0));
6044 std::vector
<std::string
> images
;
6045 ASSERT_EQ(0, rbd
.list(ioctx
, images
));
6046 for (const auto& image
: images
) {
6047 ASSERT_TRUE(image
!= name
);
6050 librbd::trash_image_info_t info
;
6051 ASSERT_EQ(-ENOENT
, rbd
.trash_get(ioctx
, "dummy image id", &info
));
6052 ASSERT_EQ(0, rbd
.trash_get(ioctx
, image_id
.c_str(), &info
));
6053 ASSERT_EQ(image_id
, info
.id
);
6055 std::vector
<librbd::trash_image_info_t
> entries
;
6056 ASSERT_EQ(0, rbd
.trash_list(ioctx
, entries
));
6057 ASSERT_FALSE(entries
.empty());
6058 ASSERT_EQ(entries
.begin()->id
, image_id
);
6062 ASSERT_EQ(0, rbd
.trash_remove_with_progress(ioctx
, image_id
.c_str(),
6064 ASSERT_EQ(0, rbd
.trash_list(ioctx
, entries
));
6065 ASSERT_TRUE(entries
.empty());
6068 TEST_F(TestLibRBD
, TestTrashMoveAndPurgeNonExpiredDelay
) {
6069 librados::IoCtx ioctx
;
6070 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
6073 std::string name
= get_temp_image_name();
6075 uint64_t size
= 1 << 18;
6077 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
6079 librbd::Image image
;
6080 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), nullptr));
6082 ASSERT_EQ(0, image
.old_format(&old_format
));
6085 ASSERT_EQ(-EOPNOTSUPP
, rbd
.trash_move(ioctx
, name
.c_str(), 0));
6089 std::string image_id
;
6090 ASSERT_EQ(0, image
.get_id(&image_id
));
6093 ASSERT_EQ(0, rbd
.trash_move(ioctx
, name
.c_str(), 100));
6096 ASSERT_EQ(-EPERM
, rbd
.trash_remove_with_progress(ioctx
, image_id
.c_str(),
6100 ASSERT_EQ(0, rbd
.trash_remove_with_progress(ioctx
, image_id
.c_str(),
6104 TEST_F(TestLibRBD
, TestTrashMoveAndRestore
) {
6105 librados::IoCtx ioctx
;
6106 ASSERT_EQ(0, _rados
.ioctx_create(m_pool_name
.c_str(), ioctx
));
6109 std::string name
= get_temp_image_name();
6111 uint64_t size
= 1 << 18;
6113 ASSERT_EQ(0, create_image_pp(rbd
, ioctx
, name
.c_str(), size
, &order
));
6115 librbd::Image image
;
6116 ASSERT_EQ(0, rbd
.open(ioctx
, image
, name
.c_str(), nullptr));
6118 ASSERT_EQ(0, image
.old_format(&old_format
));
6121 ASSERT_EQ(-EOPNOTSUPP
, rbd
.trash_move(ioctx
, name
.c_str(), 0));
6125 std::string image_id
;
6126 ASSERT_EQ(0, image
.get_id(&image_id
));
6129 ASSERT_EQ(0, rbd
.trash_move(ioctx
, name
.c_str(), 10));
6131 std::vector
<std::string
> images
;
6132 ASSERT_EQ(0, rbd
.list(ioctx
, images
));
6133 for (const auto& image
: images
) {
6134 ASSERT_TRUE(image
!= name
);
6137 std::vector
<librbd::trash_image_info_t
> entries
;
6138 ASSERT_EQ(0, rbd
.trash_list(ioctx
, entries
));
6139 ASSERT_FALSE(entries
.empty());
6140 ASSERT_EQ(entries
.begin()->id
, image_id
);
6143 ASSERT_EQ(0, rbd
.trash_restore(ioctx
, image_id
.c_str(), ""));
6144 ASSERT_EQ(0, rbd
.list(ioctx
, images
));
6145 ASSERT_FALSE(images
.empty());
6147 for (const auto& image
: images
) {
6148 if (image
== name
) {
6156 // poorman's assert()
6158 void __ceph_assert_fail(const char *assertion
, const char *file
, int line
,