]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/librbd/test_librbd.cc
update sources to v12.2.3
[ceph.git] / ceph / src / test / librbd / test_librbd.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2011 New Dream Network
7 *
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.
12 *
13 */
14
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"
22
23 #include "gtest/gtest.h"
24
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <poll.h>
31 #include <time.h>
32 #include <unistd.h>
33 #include <algorithm>
34 #include <chrono>
35 #include <condition_variable>
36 #include <iostream>
37 #include <sstream>
38 #include <list>
39 #include <set>
40 #include <thread>
41 #include <vector>
42
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"
48
49 #include <boost/assign/list_of.hpp>
50 #include <boost/scope_exit.hpp>
51
52 #ifdef HAVE_EVENTFD
53 #include <sys/eventfd.h>
54 #endif
55
56 using namespace std;
57
58 using std::chrono::seconds;
59
60 #define ASSERT_PASSED(x, args...) \
61 do { \
62 bool passed = false; \
63 x(args, &passed); \
64 ASSERT_TRUE(passed); \
65 } while(0)
66
67 void register_test_librbd() {
68 }
69
70 static int get_features(bool *old_format, uint64_t *features)
71 {
72 const char *c = getenv("RBD_FEATURES");
73 if (c) {
74 stringstream ss;
75 ss << c;
76 ss >> *features;
77 if (ss.fail())
78 return -EINVAL;
79 *old_format = false;
80 cout << "using new format!" << std::endl;
81 } else {
82 *old_format = true;
83 *features = 0;
84 cout << "using old format" << std::endl;
85 }
86
87 return 0;
88 }
89
90 static int create_image_full(rados_ioctx_t ioctx, const char *name,
91 uint64_t size, int *order, int old_format,
92 uint64_t features)
93 {
94 if (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");
98 if (r < 0) {
99 return r;
100 }
101 return rbd_create(ioctx, name, size, order);
102 } else if ((features & RBD_FEATURE_STRIPINGV2) != 0) {
103 uint64_t stripe_unit = IMAGE_STRIPE_UNIT;
104 if (*order) {
105 // use a conservative stripe_unit for non default order
106 stripe_unit = (1ull << (*order-1));
107 }
108
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);
114 } else {
115 return rbd_create2(ioctx, name, size, features, order);
116 }
117 }
118
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)
123 {
124 uint64_t stripe_unit, stripe_count;
125
126 int r;
127 r = rbd_get_stripe_unit(p_image, &stripe_unit);
128 if (r != 0) {
129 return r;
130 }
131
132 r = rbd_get_stripe_count(p_image, &stripe_count);
133 if (r != 0) {
134 return r;
135 }
136
137 return rbd_clone2(p_ioctx, p_name, p_snap_name, c_ioctx,
138 c_name, features, c_order, stripe_unit, stripe_count);
139 }
140
141
142 static int create_image(rados_ioctx_t ioctx, const char *name,
143 uint64_t size, int *order)
144 {
145 bool old_format;
146 uint64_t features;
147
148 int r = get_features(&old_format, &features);
149 if (r < 0)
150 return r;
151 return create_image_full(ioctx, name, size, order, old_format, features);
152 }
153
154 static int create_image_pp(librbd::RBD &rbd,
155 librados::IoCtx &ioctx,
156 const char *name,
157 uint64_t size, int *order) {
158 bool old_format;
159 uint64_t features;
160 int r = get_features(&old_format, &features);
161 if (r < 0)
162 return r;
163 if (old_format) {
164 librados::Rados rados(ioctx);
165 int r = rados.conf_set("rbd_default_format", "1");
166 if (r < 0) {
167 return r;
168 }
169 return rbd.create(ioctx, name, size, order);
170 } else {
171 return rbd.create2(ioctx, name, size, features, order);
172 }
173 }
174
175 class TestLibRBD : public ::testing::Test {
176 public:
177
178 TestLibRBD() : m_pool_number() {
179 }
180
181 static void SetUpTestCase() {
182 static bool seeded = false;
183 if (!seeded) {
184 seeded = true;
185 int seed = getpid();
186 cout << "seed " << seed << std::endl;
187 srand(seed);
188 }
189
190 _pool_names.clear();
191 _unique_pool_names.clear();
192 _image_number = 0;
193 ASSERT_EQ("", connect_cluster(&_cluster));
194 ASSERT_EQ("", connect_cluster_pp(_rados));
195
196 create_optional_data_pool();
197 }
198
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()));
206 }
207 if (!_pool_names.empty()) {
208 ASSERT_EQ(0, destroy_one_pool_pp(_pool_names[0], _rados));
209 }
210 }
211
212 void SetUp() override {
213 ASSERT_NE("", m_pool_name = create_pool());
214 }
215
216 bool is_skip_partial_discard_enabled() {
217 std::string value;
218 EXPECT_EQ(0, _rados.conf_get("rbd_skip_partial_discard", value));
219 return value == "true";
220 }
221
222 void validate_object_map(rbd_image_t image, bool *passed) {
223 uint64_t flags;
224 ASSERT_EQ(0, rbd_get_flags(image, &flags));
225 *passed = ((flags & RBD_FLAG_OBJECT_MAP_INVALID) == 0);
226 }
227
228 void validate_object_map(librbd::Image &image, bool *passed) {
229 uint64_t flags;
230 ASSERT_EQ(0, image.get_flags(&flags));
231 *passed = ((flags & RBD_FLAG_OBJECT_MAP_INVALID) == 0);
232 }
233
234 std::string get_temp_image_name() {
235 ++_image_number;
236 return "image" + stringify(_image_number);
237 }
238
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());
245 if (created) {
246 _unique_pool_names.push_back(data_pool);
247 }
248 }
249 }
250
251 std::string create_pool(bool unique = false) {
252 librados::Rados rados;
253 std::string pool_name;
254 if (unique) {
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];
260 } else {
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);
264 }
265 ++m_pool_number;
266 return pool_name;
267 }
268
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;
274
275 std::string m_pool_name;
276 uint32_t m_pool_number;
277
278 };
279
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;
285
286 TEST_F(TestLibRBD, CreateAndStat)
287 {
288 rados_ioctx_t ioctx;
289 ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
290
291 rbd_image_info_t info;
292 rbd_image_t image;
293 int order = 0;
294 std::string name = get_temp_image_name();
295 uint64_t size = 2 << 20;
296
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));
304
305 rados_ioctx_destroy(ioctx);
306 }
307
308 TEST_F(TestLibRBD, CreateWithSameDataPool)
309 {
310 REQUIRE_FORMAT_V2();
311
312 rados_ioctx_t ioctx;
313 ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
314
315 rbd_image_t image;
316 std::string name = get_temp_image_name();
317 uint64_t size = 2 << 20;
318
319 bool old_format;
320 uint64_t features;
321 ASSERT_EQ(0, get_features(&old_format, &features));
322 ASSERT_FALSE(old_format);
323
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;
329
330 ASSERT_EQ(0, rbd_image_options_set_uint64(image_options,
331 RBD_IMAGE_OPTION_FEATURES,
332 features));
333 ASSERT_EQ(0, rbd_image_options_set_string(image_options,
334 RBD_IMAGE_OPTION_DATA_POOL,
335 m_pool_name.c_str()));
336
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));
339
340 ASSERT_EQ(0, rbd_close(image));
341
342 rados_ioctx_destroy(ioctx);
343 }
344
345 TEST_F(TestLibRBD, CreateAndStatPP)
346 {
347 librados::IoCtx ioctx;
348 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
349
350 {
351 librbd::RBD rbd;
352 librbd::image_info_t info;
353 librbd::Image image;
354 int order = 0;
355 std::string name = get_temp_image_name();
356 uint64_t size = 2 << 20;
357
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);
363 }
364
365 ioctx.close();
366 }
367
368 TEST_F(TestLibRBD, GetId)
369 {
370 rados_ioctx_t ioctx;
371 ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
372
373 rbd_image_t image;
374 int order = 0;
375 std::string name = get_temp_image_name();
376
377 ASSERT_EQ(0, create_image(ioctx, name.c_str(), 0, &order));
378 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
379
380 char id[4096];
381 if (!is_feature_enabled(0)) {
382 // V1 image
383 ASSERT_EQ(-EINVAL, rbd_get_id(image, id, sizeof(id)));
384 } else {
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));
388 }
389
390 ASSERT_EQ(0, rbd_close(image));
391 rados_ioctx_destroy(ioctx);
392 }
393
394 TEST_F(TestLibRBD, GetIdPP)
395 {
396 librados::IoCtx ioctx;
397 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
398
399 librbd::RBD rbd;
400 librbd::Image image;
401 int order = 0;
402 std::string name = get_temp_image_name();
403
404 std::string id;
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)) {
408 // V1 image
409 ASSERT_EQ(-EINVAL, image.get_id(&id));
410 } else {
411 ASSERT_EQ(0, image.get_id(&id));
412 ASSERT_LT(0U, id.size());
413 }
414 }
415
416 TEST_F(TestLibRBD, GetBlockNamePrefix)
417 {
418 rados_ioctx_t ioctx;
419 ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
420
421 rbd_image_t image;
422 int order = 0;
423 std::string name = get_temp_image_name();
424
425 ASSERT_EQ(0, create_image(ioctx, name.c_str(), 0, &order));
426 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
427
428 char prefix[4096];
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));
432
433 ASSERT_EQ(0, rbd_close(image));
434 rados_ioctx_destroy(ioctx);
435 }
436
437 TEST_F(TestLibRBD, GetBlockNamePrefixPP)
438 {
439 librados::IoCtx ioctx;
440 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
441
442 librbd::RBD rbd;
443 librbd::Image image;
444 int order = 0;
445 std::string name = get_temp_image_name();
446
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());
450 }
451
452 TEST_F(TestLibRBD, TestGetCreateTimestamp)
453 {
454 REQUIRE_FORMAT_V2();
455
456 rados_ioctx_t ioctx;
457 ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
458
459 rbd_image_t image;
460 int order = 0;
461 std::string name = get_temp_image_name();
462
463 ASSERT_EQ(0, create_image(ioctx, name.c_str(), 0, &order));
464 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
465
466 struct timespec timestamp;
467 ASSERT_EQ(0, rbd_get_create_timestamp(image, &timestamp));
468 ASSERT_LT(0, timestamp.tv_sec);
469
470 ASSERT_EQ(0, rbd_close(image));
471
472 rados_ioctx_destroy(ioctx);
473 }
474
475 TEST_F(TestLibRBD, GetCreateTimestampPP)
476 {
477 REQUIRE_FORMAT_V2();
478
479 librados::IoCtx ioctx;
480 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
481
482 librbd::RBD rbd;
483 librbd::Image image;
484 int order = 0;
485 std::string name = get_temp_image_name();
486
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));
489
490 struct timespec timestamp;
491 ASSERT_EQ(0, image.get_create_timestamp(&timestamp));
492 ASSERT_LT(0, timestamp.tv_sec);
493 }
494
495 TEST_F(TestLibRBD, OpenAio)
496 {
497 rados_ioctx_t ioctx;
498 ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
499
500 rbd_image_info_t info;
501 rbd_image_t image;
502 int order = 0;
503 std::string name = get_temp_image_name();
504 uint64_t size = 2 << 20;
505
506 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
507
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);
515
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);
520
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);
528
529 rados_ioctx_destroy(ioctx);
530 }
531
532 TEST_F(TestLibRBD, OpenAioFail)
533 {
534 rados_ioctx_t ioctx;
535 ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
536
537 std::string name = get_temp_image_name();
538 rbd_image_t image;
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);
546
547 rados_ioctx_destroy(ioctx);
548 }
549
550 TEST_F(TestLibRBD, OpenAioPP)
551 {
552 librados::IoCtx ioctx;
553 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
554
555 librbd::RBD rbd;
556 librbd::image_info_t info;
557 librbd::Image image;
558 int order = 0;
559 std::string name = get_temp_image_name();
560 uint64_t size = 2 << 20;
561
562 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
563
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();
571
572 ASSERT_EQ(0, image.stat(info, sizeof(info)));
573 ASSERT_EQ(info.size, size);
574 ASSERT_EQ(info.order, order);
575
576 // reopen
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();
583
584 // close
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();
592
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();
597
598 ioctx.close();
599 }
600
601 TEST_F(TestLibRBD, OpenAioFailPP)
602 {
603 librados::IoCtx ioctx;
604 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
605
606 {
607 librbd::RBD rbd;
608 librbd::Image image;
609 std::string name = get_temp_image_name();
610
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();
618 }
619
620 ioctx.close();
621 }
622
623 TEST_F(TestLibRBD, ResizeAndStat)
624 {
625 rados_ioctx_t ioctx;
626 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
627
628 rbd_image_info_t info;
629 rbd_image_t image;
630 int order = 0;
631 std::string name = get_temp_image_name();
632 uint64_t size = 2 << 20;
633
634 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
635 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
636
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);
640
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);
644
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);
650
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);
654
655 ASSERT_PASSED(validate_object_map, image);
656 ASSERT_EQ(0, rbd_close(image));
657
658 rados_ioctx_destroy(ioctx);
659 }
660
661 TEST_F(TestLibRBD, ResizeAndStatPP)
662 {
663 librados::IoCtx ioctx;
664 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
665
666 {
667 librbd::RBD rbd;
668 librbd::image_info_t info;
669 librbd::Image image;
670 int order = 0;
671 std::string name = get_temp_image_name();
672 uint64_t size = 2 << 20;
673
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));
676
677 ASSERT_EQ(0, image.resize(size * 4));
678 ASSERT_EQ(0, image.stat(info, sizeof(info)));
679 ASSERT_EQ(info.size, size * 4);
680
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);
685 }
686
687 ioctx.close();
688 }
689
690 TEST_F(TestLibRBD, UpdateWatchAndResize)
691 {
692 rados_ioctx_t ioctx;
693 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
694
695 rbd_image_t image;
696 int order = 0;
697 std::string name = get_temp_image_name();
698 uint64_t size = 2 << 20;
699 struct Watcher {
700 rbd_image_t &m_image;
701 mutex m_lock;
702 condition_variable m_cond;
703 size_t m_size = 0;
704 static void cb(void *arg) {
705 Watcher *watcher = static_cast<Watcher *>(arg);
706 watcher->handle_notify();
707 }
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);
713 m_size = info.size;
714 m_cond.notify_one();
715 }
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),
719 [size, this] {
720 return this->m_size == size;}));
721 }
722 } watcher(image);
723 uint64_t handle;
724
725 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
726 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
727
728 ASSERT_EQ(0, rbd_update_watch(image, &handle, Watcher::cb, &watcher));
729
730 ASSERT_EQ(0, rbd_resize(image, size * 4));
731 watcher.wait_for_size(size * 4);
732
733 ASSERT_EQ(0, rbd_resize(image, size / 2));
734 watcher.wait_for_size(size / 2);
735
736 ASSERT_EQ(0, rbd_update_unwatch(image, handle));
737
738 ASSERT_EQ(0, rbd_close(image));
739 rados_ioctx_destroy(ioctx);
740 }
741
742 TEST_F(TestLibRBD, UpdateWatchAndResizePP)
743 {
744 librados::IoCtx ioctx;
745 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
746
747 {
748 librbd::RBD rbd;
749 librbd::Image image;
750 int order = 0;
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) {
755 }
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);
760 m_size = info.size;
761 m_cond.notify_one();
762 }
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),
766 [size, this] {
767 return this->m_size == size;}));
768 }
769 librbd::Image &m_image;
770 mutex m_lock;
771 condition_variable m_cond;
772 size_t m_size = 0;
773 } watcher(image);
774 uint64_t handle;
775
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));
778
779 ASSERT_EQ(0, image.update_watch(&watcher, &handle));
780
781 ASSERT_EQ(0, image.resize(size * 4));
782 watcher.wait_for_size(size * 4);
783
784 ASSERT_EQ(0, image.resize(size / 2));
785 watcher.wait_for_size(size / 2);
786
787 ASSERT_EQ(0, image.update_unwatch(handle));
788 }
789
790 ioctx.close();
791 }
792
793 int test_ls(rados_ioctx_t io_ctx, size_t num_expected, ...)
794 {
795 int num_images, i;
796 char *names, *cur_name;
797 va_list ap;
798 size_t max_size = 1024;
799
800 names = (char *) malloc(sizeof(char) * 1024);
801 int len = rbd_list(io_ctx, names, &max_size);
802
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;
808 num_images++;
809 }
810 free(names);
811
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);
821 } else {
822 ADD_FAILURE() << "Unable to find image " << expected;
823 va_end(ap);
824 return -ENOENT;
825 }
826 }
827 va_end(ap);
828
829 if (!image_names.empty()) {
830 ADD_FAILURE() << "Unexpected images discovered";
831 return -EINVAL;
832 }
833 return num_images;
834 }
835
836 TEST_F(TestLibRBD, TestCreateLsDelete)
837 {
838 rados_ioctx_t ioctx;
839 rados_ioctx_create(_cluster, create_pool(true).c_str(), &ioctx);
840
841 int order = 0;
842 std::string name = get_temp_image_name();
843 std::string name2 = get_temp_image_name();
844 uint64_t size = 2 << 20;
845
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()));
852
853 ASSERT_EQ(-ENOENT, rbd_remove(ioctx, name.c_str()));
854
855 rados_ioctx_destroy(ioctx);
856 }
857
858 int test_ls_pp(librbd::RBD& rbd, librados::IoCtx& io_ctx, size_t num_expected, ...)
859 {
860 int r;
861 size_t i;
862 va_list ap;
863 vector<string> names;
864 r = rbd.list(io_ctx, names);
865 if (r == -ENOENT)
866 r = 0;
867 EXPECT_TRUE(r >= 0);
868 cout << "num images is: " << names.size() << std::endl
869 << "expected: " << num_expected << std::endl;
870 int num = names.size();
871
872 for (i = 0; i < names.size(); i++) {
873 cout << "image: " << names[i] << std::endl;
874 }
875
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;
883 va_end(ap);
884 return -ENOENT;
885 }
886 names.erase(listed_name);
887 }
888 va_end(ap);
889
890 if (!names.empty()) {
891 ADD_FAILURE() << "Unexpected images discovered";
892 return -EINVAL;
893 }
894 return num;
895 }
896
897 TEST_F(TestLibRBD, TestCreateLsDeletePP)
898 {
899 librados::IoCtx ioctx;
900 ASSERT_EQ(0, _rados.ioctx_create(create_pool(true).c_str(), ioctx));
901
902 {
903 librbd::RBD rbd;
904 librbd::Image image;
905 int order = 0;
906 std::string name = get_temp_image_name();
907 std::string name2 = get_temp_image_name();
908 uint64_t size = 2 << 20;
909
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()));
916 }
917
918 ioctx.close();
919 }
920
921
922 static int print_progress_percent(uint64_t offset, uint64_t src_size,
923 void *data)
924 {
925 float percent = ((float)offset * 100) / src_size;
926 printf("%3.2f%% done\n", percent);
927 return 0;
928 }
929
930 TEST_F(TestLibRBD, TestCopy)
931 {
932 rados_ioctx_t ioctx;
933 rados_ioctx_create(_cluster, create_pool(true).c_str(), &ioctx);
934
935 rbd_image_t image;
936 rbd_image_t image2;
937 rbd_image_t image3;
938 int order = 0;
939 std::string name = get_temp_image_name();
940 std::string name2 = get_temp_image_name();
941 std::string name3 = get_temp_image_name();
942
943 uint64_t size = 2 << 20;
944
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()));
948
949 size_t sum_key_len = 0;
950 size_t sum_value_len = 0;
951 std::string key;
952 std::string val;
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()));
957
958 sum_key_len += (key.size() + 1);
959 sum_value_len += (val.size() + 1);
960 }
961
962 char keys[1024];
963 char vals[1024];
964 size_t keys_len = sizeof(keys);
965 size_t vals_len = sizeof(vals);
966
967 char value[1024];
968 size_t value_len = sizeof(value);
969
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,
974 &vals_len));
975 ASSERT_EQ(keys_len, sum_key_len);
976 ASSERT_EQ(vals_len, sum_value_len);
977
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);
983
984 value_len = sizeof(value);
985 }
986
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()));
990
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,
995 &vals_len));
996 ASSERT_EQ(keys_len, sum_key_len);
997 ASSERT_EQ(vals_len, sum_value_len);
998
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);
1004
1005 value_len = sizeof(value);
1006 }
1007
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);
1012 }
1013
1014 class PrintProgress : public librbd::ProgressContext
1015 {
1016 public:
1017 int update_progress(uint64_t offset, uint64_t src_size) override
1018 {
1019 float percent = ((float)offset * 100) / src_size;
1020 printf("%3.2f%% done\n", percent);
1021 return 0;
1022 }
1023 };
1024
1025 TEST_F(TestLibRBD, TestCopyPP)
1026 {
1027 librados::IoCtx ioctx;
1028 ASSERT_EQ(0, _rados.ioctx_create(create_pool(true).c_str(), ioctx));
1029
1030 {
1031 librbd::RBD rbd;
1032 librbd::Image image;
1033 librbd::Image image2;
1034 librbd::Image image3;
1035 int order = 0;
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;
1040 PrintProgress pp;
1041
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));
1044
1045 std::string key;
1046 std::string val;
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));
1051 }
1052
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));
1057
1058 map<string, bufferlist> pairs;
1059 std::string value;
1060 ASSERT_EQ(0, image2.metadata_list("", 70, &pairs));
1061 ASSERT_EQ(70U, pairs.size());
1062
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());
1068 }
1069
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(),
1072 name3.c_str()));
1073 ASSERT_EQ(0, rbd.open(ioctx, image3, name3.c_str(), NULL));
1074
1075 pairs.clear();
1076 ASSERT_EQ(0, image3.metadata_list("", 70, &pairs));
1077 ASSERT_EQ(70U, pairs.size());
1078
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());
1084 }
1085 }
1086
1087 ioctx.close();
1088 }
1089
1090 int test_ls_snaps(rbd_image_t image, int num_expected, ...)
1091 {
1092 int num_snaps, i, j, max_size = 10;
1093 va_list ap;
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);
1097
1098 for (i = 0; i < num_snaps; i++) {
1099 printf("snap: %s\n", snaps[i].name);
1100 }
1101
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);
1106 bool found = false;
1107 for (j = 0; j < num_snaps; j++) {
1108 if (snaps[j].name == NULL)
1109 continue;
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;
1115 found = true;
1116 break;
1117 }
1118 }
1119 EXPECT_TRUE(found);
1120 }
1121 va_end(ap);
1122
1123 for (i = 0; i < num_snaps; i++) {
1124 EXPECT_EQ((const char *)0, snaps[i].name);
1125 }
1126
1127 return num_snaps;
1128 }
1129
1130 TEST_F(TestLibRBD, TestCreateLsDeleteSnap)
1131 {
1132 rados_ioctx_t ioctx;
1133 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
1134
1135 rbd_image_t image;
1136 int order = 0;
1137 std::string name = get_temp_image_name();
1138 uint64_t size = 2 << 20;
1139 uint64_t size2 = 4 << 20;
1140
1141 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
1142 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
1143
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));
1153
1154 ASSERT_EQ(0, rbd_close(image));
1155
1156 rados_ioctx_destroy(ioctx);
1157 }
1158
1159 int test_get_snapshot_timestamp(rbd_image_t image, uint64_t snap_id)
1160 {
1161 struct timespec timestamp;
1162 EXPECT_EQ(0, rbd_snap_get_timestamp(image, snap_id, &timestamp));
1163 EXPECT_LT(0, timestamp.tv_sec);
1164 return 0;
1165 }
1166
1167 TEST_F(TestLibRBD, TestGetSnapShotTimeStamp)
1168 {
1169 REQUIRE_FORMAT_V2();
1170
1171 rados_ioctx_t ioctx;
1172 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
1173
1174 rbd_image_t image;
1175 int order = 0;
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];
1180
1181 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
1182 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
1183
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);
1189
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);
1197
1198 ASSERT_EQ(0, rbd_close(image));
1199
1200 rados_ioctx_destroy(ioctx);
1201 }
1202
1203
1204 int test_ls_snaps(librbd::Image& image, size_t num_expected, ...)
1205 {
1206 int r;
1207 size_t i, j;
1208 va_list ap;
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;
1214
1215 for (i = 0; i < snaps.size(); i++) {
1216 cout << "snap: " << snaps[i].name << std::endl;
1217 }
1218
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);
1223 int found = 0;
1224 for (j = 0; j < snaps.size(); j++) {
1225 if (snaps[j].name == "")
1226 continue;
1227 if (strcmp(snaps[j].name.c_str(), expected) == 0) {
1228 cout << "found " << snaps[j].name << " with size " << snaps[j].size
1229 << std::endl;
1230 EXPECT_EQ(expected_size, snaps[j].size);
1231 snaps[j].name = "";
1232 found = 1;
1233 break;
1234 }
1235 }
1236 EXPECT_TRUE(found);
1237 }
1238 va_end(ap);
1239
1240 for (i = 0; i < snaps.size(); i++) {
1241 EXPECT_EQ("", snaps[i].name);
1242 }
1243
1244 return snaps.size();
1245 }
1246
1247 TEST_F(TestLibRBD, TestCreateLsDeleteSnapPP)
1248 {
1249 librados::IoCtx ioctx;
1250 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
1251
1252 {
1253 librbd::RBD rbd;
1254 librbd::Image image;
1255 int order = 0;
1256 std::string name = get_temp_image_name();
1257 uint64_t size = 2 << 20;
1258 uint64_t size2 = 4 << 20;
1259
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));
1262
1263 bool exists;
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));
1285 }
1286
1287 ioctx.close();
1288 }
1289
1290 TEST_F(TestLibRBD, TestCreateLsRenameSnapPP)
1291 {
1292 librados::IoCtx ioctx;
1293 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
1294
1295 {
1296 librbd::RBD rbd;
1297 librbd::Image image;
1298 int order = 0;
1299 std::string name = get_temp_image_name();
1300 uint64_t size = 2 << 20;
1301 uint64_t size2 = 4 << 20;
1302
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));
1305
1306 bool exists;
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));
1335 }
1336
1337 ioctx.close();
1338 }
1339
1340 void simple_write_cb(rbd_completion_t cb, void *arg)
1341 {
1342 printf("write completion cb called!\n");
1343 }
1344
1345 void simple_read_cb(rbd_completion_t cb, void *arg)
1346 {
1347 printf("read completion cb called!\n");
1348 }
1349
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)
1352 {
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");
1358 if (iohint)
1359 rbd_aio_write2(image, off, len, test_data, comp, iohint);
1360 else
1361 rbd_aio_write(image, off, len, test_data, comp);
1362
1363 struct pollfd pfd;
1364 pfd.fd = fd;
1365 pfd.events = POLLIN;
1366
1367 ASSERT_EQ(1, poll(&pfd, 1, -1));
1368 ASSERT_TRUE(pfd.revents & POLLIN);
1369
1370 rbd_completion_t comps[1];
1371 ASSERT_EQ(1, rbd_poll_io_events(image, comps, 1));
1372 uint64_t count;
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);
1379 ASSERT_EQ(0, r);
1380 printf("finished write\n");
1381 rbd_aio_release(comps[0]);
1382 *passed = true;
1383 }
1384
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)
1386 {
1387 rbd_completion_t comp;
1388 rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp);
1389 printf("created completion\n");
1390 if (iohint)
1391 rbd_aio_write2(image, off, len, test_data, comp, iohint);
1392 else
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);
1398 ASSERT_EQ(0, r);
1399 printf("finished write\n");
1400 rbd_aio_release(comp);
1401 *passed = true;
1402 }
1403
1404 void write_test_data(rbd_image_t image, const char *test_data, uint64_t off, size_t len, uint32_t iohint, bool *passed)
1405 {
1406 ssize_t written;
1407 if (iohint)
1408 written = rbd_write2(image, off, len, test_data, iohint);
1409 else
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));
1413 *passed = true;
1414 }
1415
1416 void aio_discard_test_data(rbd_image_t image, uint64_t off, uint64_t len, bool *passed)
1417 {
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);
1423 ASSERT_EQ(0, r);
1424 printf("aio discard: %d~%d = %d\n", (int)off, (int)len, (int)r);
1425 rbd_aio_release(comp);
1426 *passed = true;
1427 }
1428
1429 void discard_test_data(rbd_image_t image, uint64_t off, size_t len, bool *passed)
1430 {
1431 ssize_t written;
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));
1435 *passed = true;
1436 }
1437
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)
1440 {
1441 rbd_completion_t comp;
1442 char *result = (char *)malloc(len + 1);
1443
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");
1448 if (iohint)
1449 rbd_aio_read2(image, off, len, result, comp, iohint);
1450 else
1451 rbd_aio_read(image, off, len, result, comp);
1452
1453 struct pollfd pfd;
1454 pfd.fd = fd;
1455 pfd.events = POLLIN;
1456
1457 ASSERT_EQ(1, poll(&pfd, 1, -1));
1458 ASSERT_TRUE(pfd.revents & POLLIN);
1459
1460 rbd_completion_t comps[1];
1461 ASSERT_EQ(1, rbd_poll_io_events(image, comps, 1));
1462 uint64_t count;
1463 ASSERT_EQ(static_cast<ssize_t>(sizeof(count)),
1464 read(fd, &count, sizeof(count)));
1465
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));
1474 }
1475 free(result);
1476 *passed = true;
1477 }
1478
1479 void aio_read_test_data(rbd_image_t image, const char *expected, uint64_t off, size_t len, uint32_t iohint, bool *passed)
1480 {
1481 rbd_completion_t comp;
1482 char *result = (char *)malloc(len + 1);
1483
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");
1487 if (iohint)
1488 rbd_aio_read2(image, off, len, result, comp, iohint);
1489 else
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));
1500 }
1501 free(result);
1502 *passed = true;
1503 }
1504
1505 void read_test_data(rbd_image_t image, const char *expected, uint64_t off, size_t len, uint32_t iohint, bool *passed)
1506 {
1507 ssize_t read;
1508 char *result = (char *)malloc(len + 1);
1509
1510 ASSERT_NE(static_cast<char *>(NULL), result);
1511 if (iohint)
1512 read = rbd_read2(image, off, len, result, iohint);
1513 else
1514 read = rbd_read(image, off, len, result);
1515 printf("read: %d\n", (int) read);
1516 ASSERT_EQ(len, static_cast<size_t>(read));
1517 result[len] = '\0';
1518 if (memcmp(result, expected, len)) {
1519 printf("read: %s\nexpected: %s\n", result, expected);
1520 ASSERT_EQ(0, memcmp(result, expected, len));
1521 }
1522 free(result);
1523 *passed = true;
1524 }
1525
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)
1528 {
1529 rbd_completion_t comp;
1530 rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp);
1531 printf("created completion\n");
1532 int r;
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);
1539 *passed = true;
1540 return;
1541 }
1542
1543 rbd_aio_wait_for_complete(comp);
1544 r = rbd_aio_get_return_value(comp);
1545 printf("return value is: %d\n", r);
1546 ASSERT_EQ(0, r);
1547 printf("finished writesame\n");
1548 rbd_aio_release(comp);
1549
1550 //verify data
1551 printf("to verify the data\n");
1552 ssize_t read;
1553 char *result = (char *)malloc(data_len+ 1);
1554 ASSERT_NE(static_cast<char *>(NULL), result);
1555 uint64_t left = len;
1556 while (left > 0) {
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));
1564 }
1565 off += data_len;
1566 left -= data_len;
1567 }
1568 ASSERT_EQ(0U, left);
1569 free(result);
1570 printf("verified\n");
1571
1572 *passed = true;
1573 }
1574
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)
1577 {
1578 ssize_t written;
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");
1583 *passed = true;
1584 return;
1585 }
1586 ASSERT_EQ(len, static_cast<size_t>(written));
1587 printf("wrote: %d\n", (int) written);
1588
1589 //verify data
1590 printf("to verify the data\n");
1591 ssize_t read;
1592 char *result = (char *)malloc(data_len+ 1);
1593 ASSERT_NE(static_cast<char *>(NULL), result);
1594 uint64_t left = len;
1595 while (left > 0) {
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));
1603 }
1604 off += data_len;
1605 left -= data_len;
1606 }
1607 ASSERT_EQ(0U, left);
1608 free(result);
1609 printf("verified\n");
1610
1611 *passed = true;
1612 }
1613
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)
1617 {
1618 rbd_completion_t comp;
1619 rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp);
1620 printf("created completion\n");
1621
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);
1628 ASSERT_EQ(0, r);
1629 printf("finished aio compare and write\n");
1630 rbd_aio_release(comp);
1631 *passed = true;
1632 }
1633
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)
1637 {
1638 printf("start compare and write\n");
1639 ssize_t written;
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));
1643 *passed = true;
1644 }
1645
1646
1647 TEST_F(TestLibRBD, TestIO)
1648 {
1649 rados_ioctx_t ioctx;
1650 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
1651
1652 bool skip_discard = is_skip_partial_discard_enabled();
1653
1654 rbd_image_t image;
1655 int order = 0;
1656 std::string name = get_temp_image_name();
1657 uint64_t size = 2 << 20;
1658
1659 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
1660 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
1661
1662 char test_data[TEST_IO_SIZE + 1];
1663 char zero_data[TEST_IO_SIZE + 1];
1664 char mismatch_data[TEST_IO_SIZE + 1];
1665 int i;
1666 uint64_t mismatch_offset;
1667
1668 for (i = 0; i < TEST_IO_SIZE; ++i) {
1669 test_data[i] = (char) (rand() % (126 - 33) + 33);
1670 }
1671 test_data[TEST_IO_SIZE] = '\0';
1672 memset(zero_data, 0, sizeof(zero_data));
1673 memset(mismatch_data, 9, sizeof(mismatch_data));
1674
1675 for (i = 0; i < 5; ++i)
1676 ASSERT_PASSED(write_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
1677
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);
1680
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);
1683
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);
1686
1687 for (i = 0; i < 5; ++i)
1688 ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
1689
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);
1692
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);
1696
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);
1704
1705 for (i = 0; i < 15; ++i) {
1706 if (i % 3 == 2) {
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);
1712 } else {
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);
1715 }
1716 }
1717 for (i = 0; i < 15; ++i) {
1718 if (i % 3 == 2) {
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);
1724 } else {
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);
1727 }
1728 }
1729
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));
1740
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);
1746
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);
1752
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);
1761
1762 ASSERT_PASSED(validate_object_map, image);
1763 ASSERT_EQ(0, rbd_close(image));
1764
1765 rados_ioctx_destroy(ioctx);
1766 }
1767
1768 TEST_F(TestLibRBD, TestIOWithIOHint)
1769 {
1770 rados_ioctx_t ioctx;
1771 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
1772
1773 bool skip_discard = is_skip_partial_discard_enabled();
1774
1775 rbd_image_t image;
1776 int order = 0;
1777 std::string name = get_temp_image_name();
1778 uint64_t size = 2 << 20;
1779
1780 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
1781 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
1782
1783 char test_data[TEST_IO_SIZE + 1];
1784 char zero_data[TEST_IO_SIZE + 1];
1785 char mismatch_data[TEST_IO_SIZE + 1];
1786 int i;
1787 uint64_t mismatch_offset;
1788
1789 for (i = 0; i < TEST_IO_SIZE; ++i) {
1790 test_data[i] = (char) (rand() % (126 - 33) + 33);
1791 }
1792 test_data[TEST_IO_SIZE] = '\0';
1793 memset(zero_data, 0, sizeof(zero_data));
1794 memset(mismatch_data, 9, sizeof(mismatch_data));
1795
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);
1799
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);
1803
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);
1807
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);
1811
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);
1815
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);
1819
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);
1823
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);
1835
1836 for (i = 0; i < 15; ++i) {
1837 if (i % 3 == 2) {
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);
1847 } else {
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);
1852 }
1853 }
1854 for (i = 0; i < 15; ++i) {
1855 if (i % 3 == 2) {
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);
1865 } else {
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);
1870 }
1871 }
1872
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));
1885
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);
1892
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);
1903
1904 ASSERT_PASSED(validate_object_map, image);
1905 ASSERT_EQ(0, rbd_close(image));
1906
1907 rados_ioctx_destroy(ioctx);
1908 }
1909
1910 TEST_F(TestLibRBD, TestDataPoolIO)
1911 {
1912 REQUIRE_FORMAT_V2();
1913
1914 rados_ioctx_t ioctx;
1915 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
1916
1917 std::string data_pool_name = create_pool(true);
1918
1919 bool skip_discard = is_skip_partial_discard_enabled();
1920
1921 rbd_image_t image;
1922 std::string name = get_temp_image_name();
1923 uint64_t size = 2 << 20;
1924
1925 bool old_format;
1926 uint64_t features;
1927 ASSERT_EQ(0, get_features(&old_format, &features));
1928 ASSERT_FALSE(old_format);
1929
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;
1935
1936 ASSERT_EQ(0, rbd_image_options_set_uint64(image_options,
1937 RBD_IMAGE_OPTION_FEATURES,
1938 features));
1939 ASSERT_EQ(0, rbd_image_options_set_string(image_options,
1940 RBD_IMAGE_OPTION_DATA_POOL,
1941 data_pool_name.c_str()));
1942
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));
1946
1947 char test_data[TEST_IO_SIZE + 1];
1948 char zero_data[TEST_IO_SIZE + 1];
1949 int i;
1950
1951 for (i = 0; i < TEST_IO_SIZE; ++i) {
1952 test_data[i] = (char) (rand() % (126 - 33) + 33);
1953 }
1954 test_data[TEST_IO_SIZE] = '\0';
1955 memset(zero_data, 0, sizeof(zero_data));
1956
1957 for (i = 0; i < 5; ++i)
1958 ASSERT_PASSED(write_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
1959
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);
1962
1963 for (i = 0; i < 5; ++i)
1964 ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
1965
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);
1968
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);
1972
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);
1980
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));
1991
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);
1997
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);
2003
2004 ASSERT_PASSED(validate_object_map, image);
2005 ASSERT_EQ(0, rbd_close(image));
2006
2007 rados_ioctx_destroy(ioctx);
2008 }
2009
2010 TEST_F(TestLibRBD, TestScatterGatherIO)
2011 {
2012 rados_ioctx_t ioctx;
2013 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
2014
2015 rbd_image_t image;
2016 int order = 0;
2017 std::string name = get_temp_image_name();
2018 uint64_t size = 20 << 20;
2019
2020 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
2021 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
2022
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)}
2026 };
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}
2032 };
2033
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),
2040 1<<order, comp));
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);
2044
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}
2050 };
2051
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),
2057 1<<order, comp));
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);
2062
2063 std::string linear_buffer(write_buffer.size(), '1');
2064 struct iovec linear_iovs[] = {
2065 {.iov_base = &linear_buffer[4], .iov_len = 4}
2066 };
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),
2070 1<<order, comp));
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);
2075
2076 ASSERT_PASSED(validate_object_map, image);
2077 ASSERT_EQ(0, rbd_close(image));
2078
2079 rados_ioctx_destroy(ioctx);
2080 }
2081
2082 TEST_F(TestLibRBD, TestEmptyDiscard)
2083 {
2084 rados_ioctx_t ioctx;
2085 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
2086
2087 rbd_image_t image;
2088 int order = 0;
2089 std::string name = get_temp_image_name();
2090 uint64_t size = 20 << 20;
2091
2092 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
2093 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
2094
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);
2098
2099 ASSERT_PASSED(validate_object_map, image);
2100 ASSERT_EQ(0, rbd_close(image));
2101
2102 rados_ioctx_destroy(ioctx);
2103 }
2104
2105
2106 void simple_write_cb_pp(librbd::completion_t cb, void *arg)
2107 {
2108 cout << "write completion cb called!" << std::endl;
2109 }
2110
2111 void simple_read_cb_pp(librbd::completion_t cb, void *arg)
2112 {
2113 cout << "read completion cb called!" << std::endl;
2114 }
2115
2116 void aio_write_test_data(librbd::Image& image, const char *test_data,
2117 off_t off, uint32_t iohint, bool *passed)
2118 {
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");
2123 if (iohint)
2124 image.aio_write2(off, strlen(test_data), bl, comp, iohint);
2125 else
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);
2131 ASSERT_EQ(0, r);
2132 printf("finished write\n");
2133 comp->release();
2134 *passed = true;
2135 }
2136
2137 void aio_discard_test_data(librbd::Image& image, off_t off, size_t len, bool *passed)
2138 {
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();
2143 ASSERT_EQ(0, r);
2144 comp->release();
2145 *passed = true;
2146 }
2147
2148 void write_test_data(librbd::Image& image, const char *test_data, off_t off, uint32_t iohint, bool *passed)
2149 {
2150 size_t written;
2151 size_t len = strlen(test_data);
2152 ceph::bufferlist bl;
2153 bl.append(test_data, len);
2154 if (iohint)
2155 written = image.write2(off, len, bl, iohint);
2156 else
2157 written = image.write(off, len, bl);
2158 printf("wrote: %u\n", (unsigned int) written);
2159 ASSERT_EQ(bl.length(), written);
2160 *passed = true;
2161 }
2162
2163 void discard_test_data(librbd::Image& image, off_t off, size_t len, bool *passed)
2164 {
2165 size_t written;
2166 written = image.discard(off, len);
2167 printf("discard: %u~%u\n", (unsigned)off, (unsigned)len);
2168 ASSERT_EQ(len, written);
2169 *passed = true;
2170 }
2171
2172 void aio_read_test_data(librbd::Image& image, const char *expected, off_t off, size_t expected_len, uint32_t iohint, bool *passed)
2173 {
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");
2177 if (iohint)
2178 image.aio_read2(off, expected_len, bl, comp, iohint);
2179 else
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");
2188 comp->release();
2189 *passed = true;
2190 }
2191
2192 void read_test_data(librbd::Image& image, const char *expected, off_t off, size_t expected_len, uint32_t iohint, bool *passed)
2193 {
2194 int read;
2195 size_t len = expected_len;
2196 ceph::bufferlist bl;
2197 if (iohint)
2198 read = image.read2(off, len, bl, iohint);
2199 else
2200 read = image.read(off, len, bl);
2201 ASSERT_TRUE(read >= 0);
2202 std::string bl_str(bl.c_str(), read);
2203
2204 printf("read: %u\n", (unsigned int) read);
2205 int result = memcmp(bl_str.c_str(), expected, expected_len);
2206 if (result != 0) {
2207 printf("read: %s\nexpected: %s\n", bl_str.c_str(), expected);
2208 ASSERT_EQ(0, result);
2209 }
2210 *passed = true;
2211 }
2212
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)
2215 {
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");
2220 int r;
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");
2226 comp->release();
2227 *passed = true;
2228 return;
2229 }
2230
2231 comp->wait_for_complete();
2232 r = comp->get_return_value();
2233 printf("return value is: %d\n", r);
2234 ASSERT_EQ(0, r);
2235 printf("finished writesame\n");
2236 comp->release();
2237
2238 //verify data
2239 printf("to verify the data\n");
2240 int read;
2241 uint64_t left = len;
2242 while (left > 0) {
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);
2248 if (result !=0 ) {
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);
2252 }
2253 off += data_len;
2254 left -= data_len;
2255 }
2256 ASSERT_EQ(0U, left);
2257 printf("verified\n");
2258
2259 *passed = true;
2260 }
2261
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,
2264 bool *passed)
2265 {
2266 ssize_t written;
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");
2273 *passed = true;
2274 return;
2275 }
2276 ASSERT_EQ(len, written);
2277 printf("wrote: %u\n", (unsigned int) written);
2278 *passed = true;
2279
2280 //verify data
2281 printf("to verify the data\n");
2282 int read;
2283 uint64_t left = len;
2284 while (left > 0) {
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);
2290 if (result !=0 ) {
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);
2294 }
2295 off += data_len;
2296 left -= data_len;
2297 }
2298 ASSERT_EQ(0U, left);
2299 printf("verified\n");
2300
2301 *passed = true;
2302 }
2303
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)
2307 {
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");
2314
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);
2321 ASSERT_EQ(0, r);
2322 printf("finished aio compare and write\n");
2323 comp->release();
2324 *passed = true;
2325 }
2326
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)
2329 {
2330 size_t written;
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));
2339 *passed = true;
2340 }
2341
2342 TEST_F(TestLibRBD, TestIOPP)
2343 {
2344 librados::IoCtx ioctx;
2345 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
2346
2347 bool skip_discard = is_skip_partial_discard_enabled();
2348
2349 {
2350 librbd::RBD rbd;
2351 librbd::Image image;
2352 int order = 0;
2353 std::string name = get_temp_image_name();
2354 uint64_t size = 2 << 20;
2355
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));
2358
2359 char test_data[TEST_IO_SIZE + 1];
2360 char zero_data[TEST_IO_SIZE + 1];
2361 int i;
2362 uint64_t mismatch_offset;
2363
2364 for (i = 0; i < TEST_IO_SIZE; ++i) {
2365 test_data[i] = (char) (rand() % (126 - 33) + 33);
2366 }
2367 test_data[TEST_IO_SIZE] = '\0';
2368 memset(zero_data, 0, sizeof(zero_data));
2369
2370 for (i = 0; i < 5; ++i)
2371 ASSERT_PASSED(write_test_data, image, test_data, strlen(test_data) * i, 0);
2372
2373 for (i = 5; i < 10; ++i)
2374 ASSERT_PASSED(aio_write_test_data, image, test_data, strlen(test_data) * i, 0);
2375
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);
2379
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,
2382 TEST_IO_SIZE, 0);
2383
2384 for (i = 0; i < 5; ++i)
2385 ASSERT_PASSED(read_test_data, image, test_data, strlen(test_data) * i, TEST_IO_SIZE, 0);
2386
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);
2389
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);
2393
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);
2401
2402 for (i = 0; i < 15; ++i) {
2403 if (i % 3 == 2) {
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);
2409 } else {
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);
2412 }
2413 }
2414 for (i = 0; i < 15; ++i) {
2415 if (i % 3 == 2) {
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);
2421 } else {
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);
2424 }
2425 }
2426
2427 ASSERT_PASSED(validate_object_map, image);
2428 }
2429
2430 ioctx.close();
2431 }
2432
2433 TEST_F(TestLibRBD, TestIOPPWithIOHint)
2434 {
2435 librados::IoCtx ioctx;
2436 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
2437
2438 {
2439 librbd::RBD rbd;
2440 librbd::Image image;
2441 int order = 0;
2442 std::string name = get_temp_image_name();
2443 uint64_t size = 2 << 20;
2444
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));
2447
2448 char test_data[TEST_IO_SIZE + 1];
2449 char zero_data[TEST_IO_SIZE + 1];
2450 test_data[TEST_IO_SIZE] = '\0';
2451 int i;
2452
2453 for (i = 0; i < TEST_IO_SIZE; ++i) {
2454 test_data[i] = (char) (rand() % (126 - 33) + 33);
2455 }
2456 memset(zero_data, 0, sizeof(zero_data));
2457
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);
2461
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);
2465
2466 ASSERT_PASSED(read_test_data, image, test_data, strlen(test_data),
2467 TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_RANDOM);
2468
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);
2472
2473 for (i = 0; i < 15; ++i) {
2474 if (i % 3 == 2) {
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);
2484 } else {
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);
2489 }
2490 }
2491 for (i = 0; i < 15; ++i) {
2492 if (i % 3 == 2) {
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);
2502 } else {
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);
2507 }
2508 }
2509
2510 ASSERT_PASSED(validate_object_map, image);
2511 }
2512
2513 ioctx.close();
2514 }
2515
2516
2517
2518 TEST_F(TestLibRBD, TestIOToSnapshot)
2519 {
2520 rados_ioctx_t ioctx;
2521 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
2522
2523 rbd_image_t image;
2524 int order = 0;
2525 std::string name = get_temp_image_name();
2526 uint64_t isize = 2 << 20;
2527
2528 ASSERT_EQ(0, create_image(ioctx, name.c_str(), isize, &order));
2529 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
2530
2531 int i, r;
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];
2535
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';
2540
2541 r = rbd_read(image, 0, TEST_IO_TO_SNAP_SIZE, orig_data);
2542 ASSERT_EQ(r, TEST_IO_TO_SNAP_SIZE);
2543
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);
2548
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));
2553
2554 ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
2555
2556 rbd_snap_set(image, "orig");
2557 ASSERT_PASSED(read_test_data, image, orig_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
2558
2559 rbd_snap_set(image, "written");
2560 ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
2561
2562 rbd_snap_set(image, "orig");
2563
2564 r = rbd_write(image, 0, TEST_IO_TO_SNAP_SIZE, test_data);
2565 printf("write to snapshot returned %d\n", r);
2566 ASSERT_LT(r, 0);
2567 cout << strerror(-r) << std::endl;
2568
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);
2572
2573 r = rbd_snap_rollback(image, "orig");
2574 ASSERT_EQ(r, -EROFS);
2575
2576 r = rbd_snap_set(image, NULL);
2577 ASSERT_EQ(r, 0);
2578 r = rbd_snap_rollback(image, "orig");
2579 ASSERT_EQ(r, 0);
2580
2581 ASSERT_PASSED(write_test_data, image, test_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
2582
2583 rbd_flush(image);
2584
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);
2590 ASSERT_LT(r, 0);
2591 cout << strerror(-r) << std::endl;
2592 ASSERT_EQ(0, rbd_close(image_at_snap));
2593
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));
2599
2600 ASSERT_PASSED(validate_object_map, image);
2601 ASSERT_EQ(0, rbd_close(image));
2602
2603 rados_ioctx_destroy(ioctx);
2604 }
2605
2606 TEST_F(TestLibRBD, TestClone)
2607 {
2608 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
2609
2610 rados_ioctx_t ioctx;
2611 rbd_image_info_t pinfo, cinfo;
2612 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
2613
2614 bool old_format;
2615 uint64_t features;
2616 rbd_image_t parent, child;
2617 int order = 0;
2618
2619 ASSERT_EQ(0, get_features(&old_format, &features));
2620 ASSERT_FALSE(old_format);
2621
2622 std::string parent_name = get_temp_image_name();
2623 std::string child_name = get_temp_image_name();
2624
2625 // make a parent to clone from
2626 ASSERT_EQ(0, create_image_full(ioctx, parent_name.c_str(), 4<<20, &order,
2627 false, features));
2628 ASSERT_EQ(0, rbd_open(ioctx, parent_name.c_str(), &parent, NULL));
2629 printf("made parent image \"parent\"\n");
2630
2631 char *data = (char *)"testdata";
2632 ASSERT_EQ((ssize_t)strlen(data), rbd_write(parent, 0, strlen(data), data));
2633
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));
2637
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");
2641
2642 // create 70 metadatas to verify we can clone all key/value pairs
2643 std::string key;
2644 std::string val;
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()));
2651
2652 sum_key_len += (key.size() + 1);
2653 sum_value_len += (val.size() + 1);
2654 }
2655
2656 char keys[1024];
2657 char vals[1024];
2658 size_t keys_len = sizeof(keys);
2659 size_t vals_len = sizeof(vals);
2660
2661 char value[1024];
2662 size_t value_len = sizeof(value);
2663
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"));
2669
2670 ASSERT_EQ(-EINVAL, clone_image(ioctx, parent, parent_name.c_str(), "parent_snap",
2671 ioctx, child_name.c_str(), features, &order));
2672
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");
2676
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");
2681
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");
2687
2688 // check read
2689 ASSERT_PASSED(read_test_data, child, data, 0, strlen(data), 0);
2690
2691 // check write
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);
2695
2696 // check attributes
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);
2700 uint64_t overlap;
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");
2706
2707 // check key/value pairs in child image
2708 ASSERT_EQ(0, rbd_metadata_list(child, "", 70, keys, &keys_len, vals,
2709 &vals_len));
2710 ASSERT_EQ(sum_key_len, keys_len);
2711 ASSERT_EQ(sum_value_len, vals_len);
2712
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);
2718
2719 value_len = sizeof(value);
2720 }
2721 printf("child image successfully cloned all image-meta pairs\n");
2722
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");
2735
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");
2748
2749 ASSERT_PASSED(validate_object_map, child);
2750 ASSERT_EQ(0, rbd_close(child));
2751
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");
2761
2762 ASSERT_EQ(0, rbd_close(parent));
2763 rados_ioctx_destroy(ioctx);
2764 }
2765
2766 TEST_F(TestLibRBD, TestClone2)
2767 {
2768 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
2769
2770 rados_ioctx_t ioctx;
2771 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
2772
2773 bool old_format;
2774 uint64_t features;
2775 rbd_image_t parent, child;
2776 int order = 0;
2777
2778 ASSERT_EQ(0, get_features(&old_format, &features));
2779 ASSERT_FALSE(old_format);
2780
2781 std::string parent_name = get_temp_image_name();
2782 std::string child_name = get_temp_image_name();
2783
2784 // make a parent to clone from
2785 ASSERT_EQ(0, create_image_full(ioctx, parent_name.c_str(), 4<<20, &order,
2786 false, features));
2787 ASSERT_EQ(0, rbd_open(ioctx, parent_name.c_str(), &parent, NULL));
2788 printf("made parent image \"parent\"\n");
2789
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));
2794
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));
2798
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");
2802
2803 // create 70 metadatas to verify we can clone all key/value pairs
2804 std::string key;
2805 std::string val;
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()));
2812
2813 sum_key_len += (key.size() + 1);
2814 sum_value_len += (val.size() + 1);
2815 }
2816
2817 char keys[1024];
2818 char vals[1024];
2819 size_t keys_len = sizeof(keys);
2820 size_t vals_len = sizeof(vals);
2821
2822 char value[1024];
2823 size_t value_len = sizeof(value);
2824
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"));
2830
2831 ASSERT_EQ(-EINVAL, clone_image(ioctx, parent, parent_name.c_str(), "parent_snap",
2832 ioctx, child_name.c_str(), features, &order));
2833
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");
2837
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");
2842
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");
2848
2849 // check key/value pairs in child image
2850 ASSERT_EQ(0, rbd_metadata_list(child, "", 70, keys, &keys_len, vals,
2851 &vals_len));
2852 ASSERT_EQ(sum_key_len, keys_len);
2853 ASSERT_EQ(sum_value_len, vals_len);
2854
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);
2860
2861 value_len = sizeof(value);
2862 }
2863 printf("child image successfully cloned all image-meta pairs\n");
2864
2865 // write something in
2866 ASSERT_EQ((ssize_t)strlen(childata), rbd_write(child, 20, strlen(childata), childata));
2867
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)));
2871
2872 // overlap
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)));
2876
2877 // all parent
2878 ASSERT_EQ((ssize_t)sizeof(test), rbd_read(child, 0, sizeof(test), test));
2879 ASSERT_EQ(0, memcmp(test, data, strlen(data)));
2880
2881 ASSERT_PASSED(validate_object_map, child);
2882 ASSERT_PASSED(validate_object_map, parent);
2883
2884 ASSERT_EQ(0, rbd_close(child));
2885 ASSERT_EQ(0, rbd_close(parent));
2886 rados_ioctx_destroy(ioctx);
2887 }
2888
2889 static void test_list_children(rbd_image_t image, ssize_t num_expected, ...)
2890 {
2891 va_list ap;
2892 va_start(ap, num_expected);
2893 size_t pools_len = 100;
2894 size_t children_len = 100;
2895 char *pools = NULL;
2896 char *children = NULL;
2897 ssize_t num_children;
2898
2899 do {
2900 free(pools);
2901 free(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);
2907
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 *);
2912 char *pool = pools;
2913 char *image = children;
2914 bool found = 0;
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);
2921 found = 1;
2922 break;
2923 }
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);
2929 }
2930 }
2931 ASSERT_TRUE(found);
2932 }
2933 va_end(ap);
2934
2935 if (pools)
2936 free(pools);
2937 if (children)
2938 free(children);
2939 }
2940
2941 TEST_F(TestLibRBD, ListChildren)
2942 {
2943 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
2944
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);
2949
2950 rados_ioctx_create(_cluster, pool_name1.c_str(), &ioctx1);
2951 rados_ioctx_create(_cluster, pool_name2.c_str(), &ioctx2);
2952
2953 bool old_format;
2954 uint64_t features;
2955 rbd_image_t parent;
2956 int order = 0;
2957
2958 ASSERT_EQ(0, get_features(&old_format, &features));
2959 ASSERT_FALSE(old_format);
2960
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();
2966
2967 // make a parent to clone from
2968 ASSERT_EQ(0, create_image_full(ioctx1, parent_name.c_str(), 4<<20, &order,
2969 false, features));
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"));
2975
2976 ASSERT_EQ(0, rbd_close(parent));
2977 ASSERT_EQ(0, rbd_open(ioctx1, parent_name.c_str(), &parent, "parent_snap"));
2978
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());
2982
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());
2987
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());
2993
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());
3000
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());
3006
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());
3011
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());
3015
3016 ASSERT_EQ(0, rbd_remove(ioctx1, child_name2.c_str()));
3017 test_list_children(parent, 0);
3018
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);
3025 }
3026
3027 TEST_F(TestLibRBD, ListChildrenTiered)
3028 {
3029 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
3030
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);
3036
3037 std::string cmdstr = "{\"prefix\": \"osd tier add\", \"pool\": \"" +
3038 pool_name1 + "\", \"tierpool\":\"" + pool_name3 + "\", \"force_nonempty\":\"\"}";
3039 char *cmd[1];
3040 cmd[0] = (char *)cmdstr.c_str();
3041 ASSERT_EQ(0, rados_mon_command(_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
3042
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));
3047
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));
3052
3053 EXPECT_EQ(0, rados_wait_for_latest_osdmap(_cluster));
3054
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();
3060
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);
3064
3065 bool old_format;
3066 uint64_t features;
3067 rbd_image_t parent;
3068 int order = 0;
3069
3070 ASSERT_EQ(0, get_features(&old_format, &features));
3071 ASSERT_FALSE(old_format);
3072
3073 // make a parent to clone from
3074 ASSERT_EQ(0, create_image_full(ioctx1, parent_name.c_str(), 4<<20, &order,
3075 false, features));
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"));
3081
3082 ASSERT_EQ(0, rbd_close(parent));
3083 ASSERT_EQ(0, rbd_open(ioctx1, parent_name.c_str(), &parent, "parent_snap"));
3084
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());
3088
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());
3093
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);
3100 ASSERT_GT(size, 0);
3101 free(buf);
3102 ASSERT_EQ(0, rbd_close(tier_image));
3103
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());
3109
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());
3116
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());
3122
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());
3127
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());
3131
3132 ASSERT_EQ(0, rbd_remove(ioctx1, child_name2.c_str()));
3133 test_list_children(parent, 0);
3134
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\": \"" +
3142 pool_name1 + "\"}";
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));
3149 }
3150
3151 TEST_F(TestLibRBD, LockingPP)
3152 {
3153 librados::IoCtx ioctx;
3154 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
3155
3156 {
3157 librbd::RBD rbd;
3158 librbd::Image image;
3159 int order = 0;
3160 std::string name = get_temp_image_name();
3161 uint64_t size = 2 << 20;
3162 std::string cookie1 = "foo";
3163 std::string cookie2 = "bar";
3164
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));
3167
3168 // no lockers initially
3169 std::list<librbd::locker_t> lockers;
3170 std::string tag;
3171 bool exclusive;
3172 ASSERT_EQ(0, image.list_lockers(&lockers, &exclusive, &tag));
3173 ASSERT_EQ(0u, lockers.size());
3174 ASSERT_EQ("", tag);
3175
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("", ""));
3184
3185 // list exclusive
3186 ASSERT_EQ(0, image.list_lockers(&lockers, &exclusive, &tag));
3187 ASSERT_TRUE(exclusive);
3188 ASSERT_EQ("", tag);
3189 ASSERT_EQ(1u, lockers.size());
3190 ASSERT_EQ(cookie1, lockers.front().cookie);
3191
3192 // unlock
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());
3199
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"));
3208
3209 // list shared
3210 ASSERT_EQ(0, image.list_lockers(&lockers, &exclusive, &tag));
3211 ASSERT_EQ(2u, lockers.size());
3212 }
3213
3214 ioctx.close();
3215 }
3216
3217 TEST_F(TestLibRBD, FlushAio)
3218 {
3219 rados_ioctx_t ioctx;
3220 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
3221
3222 rbd_image_t image;
3223 int order = 0;
3224 std::string name = get_temp_image_name();
3225 uint64_t size = 2 << 20;
3226 size_t num_aios = 256;
3227
3228 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
3229 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
3230
3231 char test_data[TEST_IO_SIZE + 1];
3232 size_t i;
3233 for (i = 0; i < TEST_IO_SIZE; ++i) {
3234 test_data[i] = (char) (rand() % (126 - 33) + 33);
3235 }
3236
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,
3242 write_comps[i]));
3243 }
3244
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);
3251
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]);
3255 }
3256
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);
3261 }
3262
3263 TEST_F(TestLibRBD, FlushAioPP)
3264 {
3265 librados::IoCtx ioctx;
3266 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
3267
3268 {
3269 librbd::RBD rbd;
3270 librbd::Image image;
3271 int order = 0;
3272 std::string name = get_temp_image_name();
3273 uint64_t size = 2 << 20;
3274 const size_t num_aios = 256;
3275
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));
3278
3279 char test_data[TEST_IO_SIZE + 1];
3280 size_t i;
3281 for (i = 0; i < TEST_IO_SIZE; ++i) {
3282 test_data[i] = (char) (rand() % (126 - 33) + 33);
3283 }
3284 test_data[TEST_IO_SIZE] = '\0';
3285
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],
3293 write_comps[i]));
3294 }
3295
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();
3302
3303 for (i = 0; i < num_aios; ++i) {
3304 librbd::RBD::AioCompletion *comp = write_comps[i];
3305 ASSERT_EQ(1, comp->is_complete());
3306 comp->release();
3307 }
3308 ASSERT_PASSED(validate_object_map, image);
3309 }
3310
3311 ioctx.close();
3312 }
3313
3314
3315 int iterate_cb(uint64_t off, size_t len, int exists, void *arg)
3316 {
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);
3320 return 0;
3321 }
3322
3323 static int iterate_error_cb(uint64_t off, size_t len, int exists, void *arg)
3324 {
3325 return -EINVAL;
3326 }
3327
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)
3331 {
3332 uint64_t size;
3333 image.size(&size);
3334 interval_set<uint64_t> exists_at_start = *exists;
3335
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;
3342 w.insert(off, len);
3343
3344 // the zeroed bit no longer exists...
3345 w.intersection_of(*exists);
3346 exists->subtract(w);
3347
3348 // the bits we discarded are no long written...
3349 interval_set<uint64_t> w2 = w;
3350 w2.intersection_of(*what);
3351 what->subtract(w2);
3352
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);
3357 what->union_of(w3);
3358
3359 } else {
3360 bufferlist bl;
3361 bl.append(buffer::create(len));
3362 bl.zero();
3363 ASSERT_EQ((int)len, image.write(off, len, bl));
3364 interval_set<uint64_t> w;
3365 w.insert(off, len);
3366 what->union_of(w);
3367 exists->union_of(w);
3368 }
3369 }
3370 }
3371
3372 interval_set<uint64_t> round_diff_interval(const interval_set<uint64_t>& diff,
3373 uint64_t object_size)
3374 {
3375 if (object_size == 0) {
3376 return diff;
3377 }
3378
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);
3389 }
3390 return rounded_diff;
3391 }
3392
3393 template <typename T>
3394 class DiffIterateTest : public TestLibRBD {
3395 public:
3396 static const uint8_t whole_object = T::whole_object;
3397 };
3398
3399 template <bool _whole_object>
3400 class DiffIterateParams {
3401 public:
3402 static const uint8_t whole_object = _whole_object;
3403 };
3404
3405 typedef ::testing::Types<DiffIterateParams<false>,
3406 DiffIterateParams<true> > DiffIterateTypes;
3407 TYPED_TEST_CASE(DiffIterateTest, DiffIterateTypes);
3408
3409 TYPED_TEST(DiffIterateTest, DiffIterate)
3410 {
3411 librados::IoCtx ioctx;
3412 ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
3413
3414 bool skip_discard = this->is_skip_partial_discard_enabled();
3415
3416 {
3417 librbd::RBD rbd;
3418 librbd::Image image;
3419 int order = 0;
3420 std::string name = this->get_temp_image_name();
3421 uint64_t size = 20 << 20;
3422
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));
3425
3426 uint64_t object_size = 0;
3427 if (this->whole_object) {
3428 object_size = 1 << order;
3429 }
3430
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);
3437
3438 two = round_diff_interval(two, object_size);
3439 cout << " wrote " << two << std::endl;
3440
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;
3449 l.subtract(i);
3450 cout << " ... two - (two*diff) = " << l << std::endl;
3451 }
3452 ASSERT_TRUE(two.subset_of(diff));
3453 }
3454 ioctx.close();
3455 }
3456
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)
3461 {
3462 if (object_size != 0) {
3463 offset -= offset % object_size;
3464 length = object_size;
3465 }
3466 }
3467 uint64_t offset;
3468 uint64_t length;
3469 bool exists;
3470 bool operator==(const diff_extent& o) const {
3471 return offset == o.offset && length == o.length && exists == o.exists;
3472 }
3473 };
3474
3475 ostream& operator<<(ostream & o, const diff_extent& e) {
3476 return o << '(' << e.offset << '~' << e.length << ' ' << (e.exists ? "true" : "false") << ')';
3477 }
3478
3479 int vector_iterate_cb(uint64_t off, size_t len, int exists, void *arg)
3480 {
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));
3484 return 0;
3485 }
3486
3487 TYPED_TEST(DiffIterateTest, DiffIterateDiscard)
3488 {
3489 librados::IoCtx ioctx;
3490 ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
3491
3492 librbd::RBD rbd;
3493 librbd::Image image;
3494 int order = 0;
3495 std::string name = this->get_temp_image_name();
3496 uint64_t size = 20 << 20;
3497
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));
3500
3501 uint64_t object_size = 0;
3502 if (this->whole_object) {
3503 object_size = 1 << order;
3504 }
3505 vector<diff_extent> extents;
3506 ceph::bufferlist bl;
3507
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());
3511
3512 char data[256];
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]);
3520
3521 int obj_ofs = 256;
3522 ASSERT_EQ(1 << order, image.discard(0, 1 << order));
3523
3524 extents.clear();
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());
3528
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"));
3536
3537 ASSERT_EQ(obj_ofs, image.discard(0, obj_ofs));
3538
3539 extents.clear();
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]);
3545
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"));
3550
3551 extents.clear();
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);
3557 }
3558
3559 TYPED_TEST(DiffIterateTest, DiffIterateStress)
3560 {
3561 librados::IoCtx ioctx;
3562 ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
3563
3564 bool skip_discard = this->is_skip_partial_discard_enabled();
3565
3566 librbd::RBD rbd;
3567 librbd::Image image;
3568 int order = 0;
3569 std::string name = this->get_temp_image_name();
3570 uint64_t size = 400 << 20;
3571
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));
3574
3575 uint64_t object_size = 0;
3576 if (this->whole_object) {
3577 object_size = 1 << order;
3578 }
3579
3580 interval_set<uint64_t> curexists;
3581 vector<interval_set<uint64_t> > wrote;
3582 vector<interval_set<uint64_t> > exists;
3583 vector<string> snap;
3584 int n = 20;
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()));
3591 wrote.push_back(w);
3592 exists.push_back(curexists);
3593 snap.push_back(s);
3594 }
3595
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;
3605
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;
3611
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,
3615 (void *)&actual));
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;
3621 l.subtract(i);
3622 cout << " ... diff - (actual*diff) = " << l << std::endl;
3623 }
3624 ASSERT_TRUE(diff.subset_of(actual));
3625 }
3626 }
3627 ASSERT_EQ(0, image.snap_set(NULL));
3628 ASSERT_EQ(0, image.snap_remove(snap[n-h-1].c_str()));
3629 }
3630
3631 ASSERT_PASSED(this->validate_object_map, image);
3632 }
3633
3634 TYPED_TEST(DiffIterateTest, DiffIterateRegression6926)
3635 {
3636 librados::IoCtx ioctx;
3637 ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
3638
3639 librbd::RBD rbd;
3640 librbd::Image image;
3641 int order = 0;
3642 std::string name = this->get_temp_image_name();
3643 uint64_t size = 20 << 20;
3644
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));
3647
3648 uint64_t object_size = 0;
3649 if (this->whole_object) {
3650 object_size = 1 << order;
3651 }
3652 vector<diff_extent> extents;
3653 ceph::bufferlist bl;
3654
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());
3658
3659 ASSERT_EQ(0, image.snap_create("snap1"));
3660 char data[256];
3661 memset(data, 1, sizeof(data));
3662 bl.append(data, 256);
3663 ASSERT_EQ(256, image.write(0, 256, bl));
3664
3665 extents.clear();
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]);
3670
3671 ASSERT_EQ(0, image.snap_set("snap1"));
3672 extents.clear();
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());
3676 }
3677
3678 TYPED_TEST(DiffIterateTest, DiffIterateIgnoreParent)
3679 {
3680 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
3681
3682 librados::IoCtx ioctx;
3683 ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
3684
3685 bool skip_discard = this->is_skip_partial_discard_enabled();
3686
3687 librbd::RBD rbd;
3688 librbd::Image image;
3689 std::string name = this->get_temp_image_name();
3690 uint64_t size = 20 << 20;
3691 int order = 0;
3692
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));
3695
3696 uint64_t object_size = 0;
3697 if (this->whole_object) {
3698 object_size = 1 << order;
3699 }
3700
3701 bufferlist bl;
3702 bl.append(buffer::create(size));
3703 bl.zero();
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"));
3709
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));
3714
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;
3720
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));
3727 }
3728 ASSERT_TRUE(two.subset_of(diff));
3729 }
3730
3731 TYPED_TEST(DiffIterateTest, DiffIterateCallbackError)
3732 {
3733 librados::IoCtx ioctx;
3734 ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
3735
3736 bool skip_discard = this->is_skip_partial_discard_enabled();
3737
3738 {
3739 librbd::RBD rbd;
3740 librbd::Image image;
3741 int order = 0;
3742 std::string name = this->get_temp_image_name();
3743 uint64_t size = 20 << 20;
3744
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));
3747
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;
3752
3753 interval_set<uint64_t> diff;
3754 ASSERT_EQ(-EINVAL, image.diff_iterate2(NULL, 0, size, true,
3755 this->whole_object,
3756 iterate_error_cb, NULL));
3757 }
3758 ioctx.close();
3759 }
3760
3761 TYPED_TEST(DiffIterateTest, DiffIterateParentDiscard)
3762 {
3763 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
3764
3765 librados::IoCtx ioctx;
3766 ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
3767
3768 bool skip_discard = this->is_skip_partial_discard_enabled();
3769
3770 librbd::RBD rbd;
3771 librbd::Image image;
3772 std::string name = this->get_temp_image_name();
3773 uint64_t size = 20 << 20;
3774 int order = 0;
3775
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));
3778
3779 uint64_t object_size = 0;
3780 if (this->whole_object) {
3781 object_size = 1 << order;
3782 }
3783
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"));
3788
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"));
3792 exists.clear();
3793 one.clear();
3794
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));
3799
3800 interval_set<uint64_t> two;
3801 scribble(image, 10, 102400, skip_discard, &exists, &two);
3802 two = round_diff_interval(two, object_size);
3803
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));
3808 }
3809
3810 TEST_F(TestLibRBD, ZeroLengthWrite)
3811 {
3812 rados_ioctx_t ioctx;
3813 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
3814
3815 rbd_image_t image;
3816 int order = 0;
3817 std::string name = get_temp_image_name();
3818 uint64_t size = 2 << 20;
3819
3820 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
3821 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
3822
3823 char read_data[1];
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]);
3827
3828 ASSERT_PASSED(validate_object_map, image);
3829 ASSERT_EQ(0, rbd_close(image));
3830
3831 rados_ioctx_destroy(ioctx);
3832 }
3833
3834
3835 TEST_F(TestLibRBD, ZeroLengthDiscard)
3836 {
3837 rados_ioctx_t ioctx;
3838 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
3839
3840 rbd_image_t image;
3841 int order = 0;
3842 std::string name = get_temp_image_name();
3843 uint64_t size = 2 << 20;
3844
3845 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
3846 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
3847
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)));
3854
3855 ASSERT_PASSED(validate_object_map, image);
3856 ASSERT_EQ(0, rbd_close(image));
3857
3858 rados_ioctx_destroy(ioctx);
3859 }
3860
3861 TEST_F(TestLibRBD, ZeroLengthRead)
3862 {
3863 rados_ioctx_t ioctx;
3864 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
3865
3866 rbd_image_t image;
3867 int order = 0;
3868 std::string name = get_temp_image_name();
3869 uint64_t size = 2 << 20;
3870
3871 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
3872 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
3873
3874 char read_data[1];
3875 ASSERT_EQ(0, rbd_read(image, 0, 0, read_data));
3876
3877 ASSERT_EQ(0, rbd_close(image));
3878
3879 rados_ioctx_destroy(ioctx);
3880 }
3881
3882 TEST_F(TestLibRBD, LargeCacheRead)
3883 {
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;
3888 return;
3889 }
3890
3891 rados_ioctx_t ioctx;
3892 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
3893
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;
3904
3905 rbd_image_t image;
3906 int order = 21;
3907 std::string name = get_temp_image_name();
3908 uint64_t size = 1 << order;
3909
3910 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
3911 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
3912
3913 std::string buffer(1 << order, '1');
3914
3915 ASSERT_EQ(static_cast<ssize_t>(buffer.size()),
3916 rbd_write(image, 0, buffer.size(), buffer.c_str()));
3917
3918 ASSERT_EQ(0, rbd_invalidate_cache(image));
3919
3920 ASSERT_EQ(static_cast<ssize_t>(buffer.size()),
3921 rbd_read(image, 0, buffer.size(), &buffer[0]));
3922
3923 ASSERT_EQ(0, rbd_close(image));
3924
3925 rados_ioctx_destroy(ioctx);
3926 }
3927
3928 TEST_F(TestLibRBD, TestPendingAio)
3929 {
3930 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
3931
3932 rados_ioctx_t ioctx;
3933 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
3934
3935 bool old_format;
3936 uint64_t features;
3937 rbd_image_t image;
3938 int order = 0;
3939
3940 ASSERT_EQ(0, get_features(&old_format, &features));
3941 ASSERT_FALSE(old_format);
3942
3943 std::string name = get_temp_image_name();
3944
3945 uint64_t size = 4 << 20;
3946 ASSERT_EQ(0, create_image_full(ioctx, name.c_str(), size, &order,
3947 false, features));
3948 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
3949
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);
3953 }
3954
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,
3961 comps[i]));
3962 }
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]);
3966 }
3967 ASSERT_EQ(0, rbd_invalidate_cache(image));
3968
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,
3973 comps[i]));
3974 }
3975
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]);
3981 }
3982
3983 rados_ioctx_destroy(ioctx);
3984 }
3985
3986 TEST_F(TestLibRBD, Flatten)
3987 {
3988 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
3989
3990 librados::IoCtx ioctx;
3991 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
3992
3993 librbd::RBD rbd;
3994 std::string parent_name = get_temp_image_name();
3995 uint64_t size = 2 << 20;
3996 int order = 0;
3997 ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(), size, &order));
3998
3999 librbd::Image parent_image;
4000 ASSERT_EQ(0, rbd.open(ioctx, parent_image, parent_name.c_str(), NULL));
4001
4002 bufferlist bl;
4003 bl.append(std::string(4096, '1'));
4004 ASSERT_EQ((ssize_t)bl.length(), parent_image.write(0, bl.length(), bl));
4005
4006 ASSERT_EQ(0, parent_image.snap_create("snap1"));
4007 ASSERT_EQ(0, parent_image.snap_protect("snap1"));
4008
4009 uint64_t features;
4010 ASSERT_EQ(0, parent_image.features(&features));
4011
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));
4015
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());
4019
4020 librbd::RBD::AioCompletion *read_comp =
4021 new librbd::RBD::AioCompletion(NULL, NULL);
4022 bufferlist read_bl;
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));
4028
4029 ASSERT_PASSED(validate_object_map, clone_image);
4030 }
4031
4032 TEST_F(TestLibRBD, SnapshotLimit)
4033 {
4034 rados_ioctx_t ioctx;
4035 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
4036
4037 rbd_image_t image;
4038 int order = 0;
4039 std::string name = get_temp_image_name();
4040 uint64_t size = 2 << 20;
4041 uint64_t limit;
4042
4043 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
4044 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
4045
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);
4051
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));
4058
4059 rados_ioctx_destroy(ioctx);
4060 }
4061
4062
4063 TEST_F(TestLibRBD, SnapshotLimitPP)
4064 {
4065 librados::IoCtx ioctx;
4066 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4067
4068 {
4069 librbd::RBD rbd;
4070 librbd::Image image;
4071 std::string name = get_temp_image_name();
4072 uint64_t size = 2 << 20;
4073 int order = 0;
4074 uint64_t limit;
4075
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));
4078
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);
4084
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"));
4090 }
4091
4092 ioctx.close();
4093 }
4094
4095 TEST_F(TestLibRBD, RebuildObjectMapViaLockOwner)
4096 {
4097 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_OBJECT_MAP);
4098
4099 librados::IoCtx ioctx;
4100 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4101
4102 librbd::RBD rbd;
4103 std::string name = get_temp_image_name();
4104 uint64_t size = 2 << 20;
4105 int order = 0;
4106 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4107
4108 std::string object_map_oid;
4109 {
4110 librbd::Image image;
4111 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
4112
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;
4116 }
4117
4118 // corrupt the object map
4119 bufferlist bl;
4120 bl.append("foo");
4121 ASSERT_EQ(0, ioctx.write(object_map_oid, bl, bl.length(), 0));
4122
4123 librbd::Image image1;
4124 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4125
4126 bool lock_owner;
4127 bl.clear();
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);
4131
4132 uint64_t flags;
4133 ASSERT_EQ(0, image1.get_flags(&flags));
4134 ASSERT_TRUE((flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0);
4135
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);
4140
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);
4145 }
4146
4147 TEST_F(TestLibRBD, RenameViaLockOwner)
4148 {
4149 REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
4150
4151 librados::IoCtx ioctx;
4152 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4153
4154 librbd::RBD rbd;
4155 std::string name = get_temp_image_name();
4156 uint64_t size = 2 << 20;
4157 int order = 0;
4158 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4159
4160 librbd::Image image1;
4161 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4162
4163 bufferlist bl;
4164 ASSERT_EQ(0, image1.write(0, 0, bl));
4165
4166 bool lock_owner;
4167 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4168 ASSERT_TRUE(lock_owner);
4169
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);
4174
4175 librbd::Image image2;
4176 ASSERT_EQ(0, rbd.open(ioctx, image2, new_name.c_str(), NULL));
4177 }
4178
4179 TEST_F(TestLibRBD, SnapCreateViaLockOwner)
4180 {
4181 REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
4182
4183 librados::IoCtx ioctx;
4184 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4185
4186 librbd::RBD rbd;
4187 std::string name = get_temp_image_name();
4188 uint64_t size = 2 << 20;
4189 int order = 0;
4190 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4191
4192 librbd::Image image1;
4193 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4194
4195 // switch to writeback cache
4196 ASSERT_EQ(0, image1.flush());
4197
4198 bufferlist bl;
4199 bl.append(std::string(4096, '1'));
4200 ASSERT_EQ((ssize_t)bl.length(), image1.write(0, bl.length(), bl));
4201
4202 bool lock_owner;
4203 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4204 ASSERT_TRUE(lock_owner);
4205
4206 librbd::Image image2;
4207 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
4208
4209 ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
4210 ASSERT_FALSE(lock_owner);
4211
4212 ASSERT_EQ(0, image2.snap_create("snap1"));
4213 bool exists;
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);
4218
4219 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4220 ASSERT_TRUE(lock_owner);
4221 }
4222
4223 TEST_F(TestLibRBD, SnapRemoveViaLockOwner)
4224 {
4225 REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
4226
4227 librados::IoCtx ioctx;
4228 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4229
4230 librbd::RBD rbd;
4231 std::string name = get_temp_image_name();
4232 uint64_t size = 2 << 20;
4233 int order = 0;
4234 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4235
4236 librbd::Image image1;
4237 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4238
4239 bufferlist bl;
4240 ASSERT_EQ(0, image1.write(0, 0, bl));
4241 ASSERT_EQ(0, image1.snap_create("snap1"));
4242
4243 bool lock_owner;
4244 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4245 ASSERT_TRUE(lock_owner);
4246
4247 librbd::Image image2;
4248 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
4249
4250 ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
4251 ASSERT_FALSE(lock_owner);
4252
4253 ASSERT_EQ(0, image2.snap_remove("snap1"));
4254 bool exists;
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);
4259
4260 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4261 ASSERT_TRUE(lock_owner);
4262 }
4263
4264 TEST_F(TestLibRBD, SnapRemove2)
4265 {
4266 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
4267
4268 librados::IoCtx ioctx;
4269 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4270
4271 librbd::RBD rbd;
4272 std::string name = get_temp_image_name();
4273 uint64_t size = 2 << 20;
4274 int order = 0;
4275 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4276
4277 librbd::Image image1;
4278 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4279
4280 bufferlist bl;
4281 ASSERT_EQ(0, image1.write(0, 0, bl));
4282 ASSERT_EQ(0, image1.snap_create("snap1"));
4283 bool exists;
4284 ASSERT_EQ(0, image1.snap_exists2("snap1", &exists));
4285 ASSERT_TRUE(exists);
4286 ASSERT_EQ(0, image1.snap_protect("snap1"));
4287 bool is_protected;
4288 ASSERT_EQ(0, image1.snap_is_protected("snap1", &is_protected));
4289 ASSERT_TRUE(is_protected);
4290
4291 uint64_t features;
4292 ASSERT_EQ(0, image1.features(&features));
4293
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));
4297
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);
4302
4303 ASSERT_EQ(-EBUSY, image1.snap_remove("snap1"));
4304 PrintProgress pp;
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);
4308 }
4309
4310 TEST_F(TestLibRBD, SnapRenameViaLockOwner)
4311 {
4312 REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
4313
4314 librados::IoCtx ioctx;
4315 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4316
4317 librbd::RBD rbd;
4318 std::string name = get_temp_image_name();
4319 uint64_t size = 2 << 20;
4320 int order = 0;
4321 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4322
4323 librbd::Image image1;
4324 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4325
4326 bufferlist bl;
4327 ASSERT_EQ(0, image1.write(0, 0, bl));
4328 ASSERT_EQ(0, image1.snap_create("snap1"));
4329
4330 bool lock_owner;
4331 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4332 ASSERT_TRUE(lock_owner);
4333
4334 librbd::Image image2;
4335 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
4336
4337 ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
4338 ASSERT_FALSE(lock_owner);
4339
4340 ASSERT_EQ(0, image2.snap_rename("snap1", "snap1-rename"));
4341 bool exists;
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);
4346
4347 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4348 ASSERT_TRUE(lock_owner);
4349 }
4350
4351 TEST_F(TestLibRBD, SnapProtectViaLockOwner)
4352 {
4353 REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
4354
4355 librados::IoCtx ioctx;
4356 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4357
4358 librbd::RBD rbd;
4359 std::string name = get_temp_image_name();
4360 uint64_t size = 2 << 20;
4361 int order = 0;
4362 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4363
4364 librbd::Image image1;
4365 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4366
4367 bufferlist bl;
4368 ASSERT_EQ(0, image1.write(0, 0, bl));
4369
4370 bool lock_owner;
4371 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4372 ASSERT_TRUE(lock_owner);
4373 ASSERT_EQ(0, image1.snap_create("snap1"));
4374
4375 librbd::Image image2;
4376 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
4377
4378 ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
4379 ASSERT_FALSE(lock_owner);
4380
4381 ASSERT_EQ(0, image2.snap_protect("snap1"));
4382 bool is_protected;
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);
4387
4388 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4389 ASSERT_TRUE(lock_owner);
4390 }
4391
4392 TEST_F(TestLibRBD, SnapUnprotectViaLockOwner)
4393 {
4394 REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
4395
4396 librados::IoCtx ioctx;
4397 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4398
4399 librbd::RBD rbd;
4400 std::string name = get_temp_image_name();
4401 uint64_t size = 2 << 20;
4402 int order = 0;
4403 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4404
4405 librbd::Image image1;
4406 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4407
4408 bufferlist bl;
4409 ASSERT_EQ(0, image1.write(0, 0, bl));
4410
4411 bool lock_owner;
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"));
4416 bool is_protected;
4417 ASSERT_EQ(0, image1.snap_is_protected("snap1", &is_protected));
4418 ASSERT_TRUE(is_protected);
4419
4420 librbd::Image image2;
4421 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
4422
4423 ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
4424 ASSERT_FALSE(lock_owner);
4425
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);
4431
4432 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4433 ASSERT_TRUE(lock_owner);
4434 }
4435
4436 TEST_F(TestLibRBD, FlattenViaLockOwner)
4437 {
4438 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
4439
4440 librados::IoCtx ioctx;
4441 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4442
4443 librbd::RBD rbd;
4444 std::string parent_name = get_temp_image_name();
4445 uint64_t size = 2 << 20;
4446 int order = 0;
4447 ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(), size, &order));
4448
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"));
4453
4454 uint64_t features;
4455 ASSERT_EQ(0, parent_image.features(&features));
4456
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));
4460
4461 librbd::Image image1;
4462 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4463
4464 bufferlist bl;
4465 ASSERT_EQ(0, image1.write(0, 0, bl));
4466
4467 bool lock_owner;
4468 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4469 ASSERT_TRUE(lock_owner);
4470
4471 librbd::Image image2;
4472 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
4473
4474 ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
4475 ASSERT_FALSE(lock_owner);
4476
4477 ASSERT_EQ(0, image2.flatten());
4478
4479 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4480 ASSERT_TRUE(lock_owner);
4481 ASSERT_PASSED(validate_object_map, image1);
4482 }
4483
4484 TEST_F(TestLibRBD, ResizeViaLockOwner)
4485 {
4486 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
4487
4488 librados::IoCtx ioctx;
4489 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4490
4491 librbd::RBD rbd;
4492 std::string name = get_temp_image_name();
4493 uint64_t size = 2 << 20;
4494 int order = 0;
4495 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4496
4497 librbd::Image image1;
4498 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4499
4500 bufferlist bl;
4501 ASSERT_EQ(0, image1.write(0, 0, bl));
4502
4503 bool lock_owner;
4504 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4505 ASSERT_TRUE(lock_owner);
4506
4507 librbd::Image image2;
4508 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
4509
4510 ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
4511 ASSERT_FALSE(lock_owner);
4512
4513 ASSERT_EQ(0, image2.resize(0));
4514
4515 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
4516 ASSERT_TRUE(lock_owner);
4517 ASSERT_PASSED(validate_object_map, image1);
4518 }
4519
4520 TEST_F(TestLibRBD, ObjectMapConsistentSnap)
4521 {
4522 REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
4523
4524 librados::IoCtx ioctx;
4525 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4526
4527 librbd::RBD rbd;
4528 std::string name = get_temp_image_name();
4529 uint64_t size = 1 << 20;
4530 int order = 12;
4531 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4532
4533 librbd::Image image1;
4534 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4535
4536 int num_snaps = 10;
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()));
4540 }
4541
4542
4543 thread writer([&image1](){
4544 librbd::image_info_t info;
4545 int r = image1.stat(info, sizeof(info));
4546 assert(r == 0);
4547 bufferlist bl;
4548 bl.append("foo");
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());
4552 }
4553 });
4554 writer.join();
4555
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);
4560 }
4561
4562 ASSERT_EQ(0, image1.snap_set(NULL));
4563 ASSERT_PASSED(validate_object_map, image1);
4564 }
4565
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);
4569 }
4570 }
4571
4572 TEST_F(TestLibRBD, Metadata)
4573 {
4574 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
4575
4576 rados_ioctx_t ioctx;
4577 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
4578
4579 std::string name = get_temp_image_name();
4580 uint64_t size = 2 << 20;
4581 int order = 0;
4582 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
4583
4584 rbd_image_t image;
4585 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
4586
4587 rbd_image_t image1;
4588 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image1, NULL));
4589
4590 char keys[1024];
4591 char vals[1024];
4592 size_t keys_len = sizeof(keys);
4593 size_t vals_len = sizeof(vals);
4594
4595 memset_rand(keys, keys_len);
4596 memset_rand(vals, vals_len);
4597
4598 ASSERT_EQ(0, rbd_metadata_list(image, "", 0, keys, &keys_len, vals,
4599 &vals_len));
4600 ASSERT_EQ(0U, keys_len);
4601 ASSERT_EQ(0U, vals_len);
4602
4603 char value[1024];
4604 size_t value_len = sizeof(value);
4605 memset_rand(value, value_len);
4606
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");
4611 value_len = 1;
4612 ASSERT_EQ(-ERANGE, rbd_metadata_get(image1, "key1", value, &value_len));
4613 ASSERT_EQ(value_len, strlen("value1") + 1);
4614
4615 ASSERT_EQ(-ERANGE, rbd_metadata_list(image1, "", 0, keys, &keys_len, vals,
4616 &vals_len));
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,
4622 &vals_len));
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");
4629
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,
4635 &vals_len));
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");
4640
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"));
4645
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"));
4650
4651 ASSERT_EQ(0, rbd_metadata_set(image1, "key1", "value1"));
4652 ASSERT_EQ(0, rbd_metadata_set(image1, "key3", "value3"));
4653
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,
4659 &vals_len));
4660 ASSERT_EQ(keys_len,
4661 strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") + 1);
4662 ASSERT_EQ(vals_len,
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");
4670
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,
4677 &vals_len));
4678 ASSERT_EQ(keys_len,
4679 strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") + 1);
4680 ASSERT_EQ(vals_len,
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");
4688
4689 // test metadata with cloning
4690 uint64_t features;
4691 ASSERT_EQ(0, rbd_get_features(image1, &features));
4692
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));
4696 rbd_image_t image2;
4697 ASSERT_EQ(0, rbd_open(ioctx, cname.c_str(), &image2, NULL));
4698 ASSERT_EQ(0, rbd_metadata_set(image2, "key4", "value4"));
4699
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,
4705 &vals_len));
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") +
4711 1, "key4");
4712 ASSERT_STREQ(vals + strlen("value1") + 1 + strlen("value2") + 1 +
4713 strlen("value3") + 1, "value4");
4714
4715 ASSERT_EQ(0, rbd_metadata_list(image1, "", 0, keys, &keys_len, vals,
4716 &vals_len));
4717 ASSERT_EQ(keys_len,
4718 strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") + 1);
4719 ASSERT_EQ(vals_len,
4720 strlen("value1") + 1 + strlen("value2") + 1 + strlen("value3") + 1);
4721 ASSERT_EQ(-ENOENT, rbd_metadata_get(image1, "key4", value, &value_len));
4722
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,
4729 &vals_len));
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");
4734
4735 ASSERT_EQ(-ERANGE, rbd_metadata_list(image2, "", 2, keys, &keys_len, vals,
4736 &vals_len));
4737 ASSERT_EQ(keys_len, strlen("key1") + 1 + strlen("key2") + 1);
4738 ASSERT_EQ(vals_len, strlen("value1") + 1 + strlen("value2") + 1);
4739
4740 ASSERT_EQ(-ERANGE, rbd_metadata_list(image2, "", 0, keys, &keys_len, vals,
4741 &vals_len));
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);
4746
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,
4753 &vals_len));
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");
4758
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);
4763 }
4764
4765 TEST_F(TestLibRBD, MetadataPP)
4766 {
4767 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
4768
4769 librados::IoCtx ioctx;
4770 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4771
4772 librbd::RBD rbd;
4773 string name = get_temp_image_name();
4774 uint64_t size = 2 << 20;
4775 int order = 0;
4776 uint64_t features;
4777 string value;
4778 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4779
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());
4785
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));
4794
4795 pairs.clear();
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));
4802
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"));
4807
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"));
4812
4813 pairs.clear();
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));
4821
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));
4828
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"));
4837 pairs.clear();
4838 ASSERT_EQ(0, image2.metadata_list("", 0, &pairs));
4839 ASSERT_EQ(4U, pairs.size());
4840 pairs.clear();
4841 ASSERT_EQ(0, image1.metadata_list("", 0, &pairs));
4842 ASSERT_EQ(3U, pairs.size());
4843 ASSERT_EQ(-ENOENT, image1.metadata_get("key4", &value));
4844 }
4845
4846 TEST_F(TestLibRBD, UpdateFeatures)
4847 {
4848 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
4849
4850 librados::IoCtx ioctx;
4851 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4852
4853 librbd::RBD rbd;
4854 std::string name = get_temp_image_name();
4855 uint64_t size = 1 << 20;
4856 int order = 0;
4857 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4858
4859 librbd::Image image;
4860 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
4861
4862 uint8_t old_format;
4863 ASSERT_EQ(0, image.old_format(&old_format));
4864 if (old_format) {
4865 ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_EXCLUSIVE_LOCK, true));
4866 return;
4867 }
4868
4869 uint64_t features;
4870 ASSERT_EQ(0, image.features(&features));
4871
4872 // must provide a single feature
4873 ASSERT_EQ(-EINVAL, image.update_features(0, true));
4874
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));
4882 }
4883
4884 ASSERT_EQ(0, image.features(&features));
4885 ASSERT_EQ(0U, features & disable_features);
4886
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));
4891
4892 ASSERT_EQ(0, image.features(&features));
4893 ASSERT_NE(0U, features & RBD_FEATURE_EXCLUSIVE_LOCK);
4894
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);
4900
4901 uint64_t expected_flags = RBD_FLAG_OBJECT_MAP_INVALID;
4902 uint64_t flags;
4903 ASSERT_EQ(0, image.get_flags(&flags));
4904 ASSERT_EQ(expected_flags, flags);
4905
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);
4909
4910 ASSERT_EQ(0, image.update_features(RBD_FEATURE_OBJECT_MAP |
4911 RBD_FEATURE_FAST_DIFF |
4912 RBD_FEATURE_JOURNALING, true));
4913
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);
4917
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);
4923
4924 expected_flags = RBD_FLAG_OBJECT_MAP_INVALID;
4925 ASSERT_EQ(0, image.get_flags(&flags));
4926 ASSERT_EQ(expected_flags, flags);
4927
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));
4931
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));
4935
4936 ASSERT_EQ(0, image.get_flags(&flags));
4937 ASSERT_EQ(0U, flags);
4938
4939 ASSERT_EQ(0, image.update_features(RBD_FEATURE_EXCLUSIVE_LOCK, false));
4940
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));
4944 }
4945 ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_DEEP_FLATTEN, true));
4946 }
4947
4948 TEST_F(TestLibRBD, RebuildObjectMap)
4949 {
4950 librados::IoCtx ioctx;
4951 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
4952
4953 librbd::RBD rbd;
4954 std::string name = get_temp_image_name();
4955 uint64_t size = 1 << 20;
4956 int order = 18;
4957 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
4958
4959 PrintProgress prog_ctx;
4960 std::string object_map_oid;
4961 bufferlist bl;
4962 bl.append("foo");
4963 {
4964 librbd::Image image;
4965 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
4966
4967 uint64_t features;
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));
4971 return;
4972 }
4973
4974 ASSERT_EQ((ssize_t)bl.length(), image.write(0, bl.length(), bl));
4975
4976 ASSERT_EQ(0, image.snap_create("snap1"));
4977 ASSERT_EQ((ssize_t)bl.length(), image.write(1<<order, bl.length(), bl));
4978
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;
4982 }
4983
4984 // corrupt the object map
4985 ASSERT_EQ(0, ioctx.write(object_map_oid, bl, bl.length(), 0));
4986
4987 librbd::Image image1;
4988 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
4989
4990 bool lock_owner;
4991 bl.clear();
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);
4995
4996 uint64_t flags;
4997 ASSERT_EQ(0, image1.get_flags(&flags));
4998 ASSERT_TRUE((flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0);
4999
5000 ASSERT_EQ(0, image1.rebuild_object_map(prog_ctx));
5001
5002 librbd::Image image2;
5003 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
5004
5005 bufferlist read_bl;
5006 ASSERT_EQ((ssize_t)bl.length(), image2.read(0, bl.length(), read_bl));
5007 ASSERT_TRUE(bl.contents_equal(read_bl));
5008
5009 read_bl.clear();
5010 ASSERT_EQ((ssize_t)bl.length(), image2.read(1<<order, bl.length(), read_bl));
5011 ASSERT_TRUE(bl.contents_equal(read_bl));
5012
5013 ASSERT_PASSED(validate_object_map, image1);
5014 ASSERT_PASSED(validate_object_map, image2);
5015 }
5016
5017 TEST_F(TestLibRBD, RebuildNewObjectMap)
5018 {
5019 REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
5020
5021 rados_ioctx_t ioctx;
5022 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
5023
5024 std::string name = get_temp_image_name();
5025 uint64_t size = 1 << 20;
5026 int order = 18;
5027 uint64_t features = RBD_FEATURE_EXCLUSIVE_LOCK;
5028 ASSERT_EQ(0, create_image_full(ioctx, name.c_str(), size, &order,
5029 false, features));
5030
5031 rbd_image_t image;
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));
5035
5036 ASSERT_PASSED(validate_object_map, image);
5037
5038 ASSERT_EQ(0, rbd_close(image));
5039 rados_ioctx_destroy(ioctx);
5040 }
5041
5042 TEST_F(TestLibRBD, CheckObjectMap)
5043 {
5044 REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
5045
5046 librados::IoCtx ioctx;
5047 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5048
5049 librbd::RBD rbd;
5050 std::string name = get_temp_image_name();
5051 uint64_t size = 1 << 20;
5052 int order = 18;
5053 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
5054
5055 PrintProgress prog_ctx;
5056 bufferlist bl1;
5057 bufferlist bl2;
5058 bl1.append("foo");
5059 {
5060 librbd::Image image;
5061 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
5062
5063 uint64_t features;
5064 ASSERT_EQ(0, image.features(&features));
5065
5066 ASSERT_EQ((ssize_t)bl1.length(), image.write(0, bl1.length(), bl1));
5067
5068 ASSERT_EQ(0, image.snap_create("snap1"));
5069 ASSERT_EQ((ssize_t)bl1.length(), image.write(1<<order, bl1.length(), bl1));
5070 }
5071
5072 librbd::Image image1;
5073 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
5074
5075 std::string image_id;
5076 ASSERT_EQ(0, get_image_id(image1, &image_id));
5077
5078 std::string object_map_oid = RBD_OBJECT_MAP_PREFIX + image_id;
5079
5080 ASSERT_LT(0, ioctx.read(object_map_oid, bl2, 1024, 0));
5081
5082 bool lock_owner;
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);
5086
5087 //reopen image to reread now corrupt object map from disk
5088 image1.close();
5089
5090 bl1.clear();
5091 ASSERT_LT(0, ioctx.read(object_map_oid, bl1, 1024, 0));
5092 ASSERT_FALSE(bl1.contents_equal(bl2));
5093
5094 ASSERT_EQ(0, ioctx.write_full(object_map_oid, bl2));
5095 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
5096
5097 uint64_t flags;
5098 ASSERT_EQ(0, image1.get_flags(&flags));
5099 ASSERT_TRUE((flags & RBD_FLAG_OBJECT_MAP_INVALID) == 0);
5100
5101 ASSERT_EQ(0, image1.check_object_map(prog_ctx));
5102
5103 ASSERT_EQ(0, image1.get_flags(&flags));
5104 ASSERT_TRUE((flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0);
5105 }
5106
5107 TEST_F(TestLibRBD, BlockingAIO)
5108 {
5109 librados::IoCtx ioctx;
5110 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5111
5112 bool skip_discard = is_skip_partial_discard_enabled();
5113
5114 librbd::RBD rbd;
5115 std::string name = get_temp_image_name();
5116 uint64_t size = 1 << 20;
5117 int order = 18;
5118 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
5119
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;
5127
5128 librbd::Image image;
5129 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
5130
5131 bufferlist bl;
5132 ASSERT_EQ(0, image.write(0, bl.length(), bl));
5133
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));
5138
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();
5145
5146 ASSERT_EQ(1, write_comp->is_complete());
5147 ASSERT_EQ(0, write_comp->get_return_value());
5148 write_comp->release();
5149
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();
5155
5156 librbd::RBD::AioCompletion *read_comp =
5157 new librbd::RBD::AioCompletion(NULL, NULL);
5158 bufferlist read_bl;
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();
5163
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));
5168 }
5169
5170 TEST_F(TestLibRBD, ExclusiveLockTransition)
5171 {
5172 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
5173
5174 librados::IoCtx ioctx;
5175 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5176
5177 librbd::RBD rbd;
5178 std::string name = get_temp_image_name();
5179
5180 uint64_t size = 1 << 18;
5181 int order = 12;
5182 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
5183
5184 librbd::Image image1;
5185 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
5186
5187 librbd::Image image2;
5188 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
5189
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,
5195 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));
5199 } else {
5200 ASSERT_EQ(0, image2.aio_write(object_no << order, bl.length(), bl, comp));
5201 }
5202 }
5203
5204 while (!comps.empty()) {
5205 librbd::RBD::AioCompletion *comp = comps.front();
5206 comps.pop_front();
5207 ASSERT_EQ(0, comp->wait_for_complete());
5208 ASSERT_EQ(1, comp->is_complete());
5209 comp->release();
5210 }
5211
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) {
5215 bufferlist read_bl;
5216 ASSERT_EQ((ssize_t)bl.length(), image3.read(object_no << order, bl.length(),
5217 read_bl));
5218 ASSERT_TRUE(bl.contents_equal(read_bl));
5219 }
5220
5221 ASSERT_PASSED(validate_object_map, image1);
5222 ASSERT_PASSED(validate_object_map, image2);
5223 ASSERT_PASSED(validate_object_map, image3);
5224 }
5225
5226 TEST_F(TestLibRBD, ExclusiveLockReadTransition)
5227 {
5228 REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
5229
5230 librados::IoCtx ioctx;
5231 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5232
5233 librbd::RBD rbd;
5234 std::string name = get_temp_image_name();
5235
5236 uint64_t size = 1 << 18;
5237 int order = 12;
5238 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
5239
5240 librbd::Image image1;
5241 ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
5242
5243 bool lock_owner;
5244 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
5245 ASSERT_FALSE(lock_owner);
5246
5247 // journaling should force read ops to acquire the lock
5248 bufferlist read_bl;
5249 ASSERT_EQ(0, image1.read(0, 0, read_bl));
5250
5251 ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
5252 ASSERT_TRUE(lock_owner);
5253
5254 librbd::Image image2;
5255 ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
5256
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,
5261 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));
5266 } else {
5267 ASSERT_EQ(0, image2.aio_read(object_no << order, 1 << order, read_bls.back(), comp));
5268 }
5269 }
5270
5271 while (!comps.empty()) {
5272 librbd::RBD::AioCompletion *comp = comps.front();
5273 comps.pop_front();
5274 ASSERT_EQ(0, comp->wait_for_complete());
5275 ASSERT_EQ(1, comp->is_complete());
5276 comp->release();
5277 }
5278 }
5279
5280 TEST_F(TestLibRBD, CacheMayCopyOnWrite) {
5281 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
5282
5283 librados::IoCtx ioctx;
5284 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5285
5286 librbd::RBD rbd;
5287 std::string name = get_temp_image_name();
5288
5289 uint64_t size = 1 << 18;
5290 int order = 12;
5291 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
5292
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"));
5297
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));
5301
5302 librbd::Image clone;
5303 ASSERT_EQ(0, rbd.open(ioctx, clone, clone_name.c_str(), NULL));
5304 ASSERT_EQ(0, clone.flush());
5305
5306 bufferlist expect_bl;
5307 expect_bl.append(std::string(1024, '\0'));
5308
5309 // test double read path
5310 bufferlist read_bl;
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));
5314
5315 bufferlist write_bl;
5316 write_bl.append(std::string(1024, '1'));
5317 ASSERT_EQ(1024, clone.write(offset, write_bl.length(), write_bl));
5318
5319 read_bl.clear();
5320 ASSERT_EQ(1024, clone.read(offset + 2048, 1024, read_bl));
5321 ASSERT_TRUE(expect_bl.contents_equal(read_bl));
5322
5323 // test read retry path
5324 offset = 1 << order;
5325 ASSERT_EQ(1024, clone.write(offset, write_bl.length(), write_bl));
5326
5327 read_bl.clear();
5328 ASSERT_EQ(1024, clone.read(offset + 2048, 1024, read_bl));
5329 ASSERT_TRUE(expect_bl.contents_equal(read_bl));
5330 }
5331
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;
5339
5340 librados::IoCtx ioctx;
5341 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5342
5343 librbd::RBD rbd;
5344 std::string name = get_temp_image_name();
5345 uint64_t size = 1 << 18;
5346 int order = 0;
5347 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
5348
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"));
5354
5355 librbd::RBD::AioCompletion *read_comp =
5356 new librbd::RBD::AioCompletion(NULL, NULL);
5357 bufferlist read_bl;
5358 image2.aio_read(0, 1024, read_bl, read_comp);
5359 ASSERT_EQ(0, read_comp->wait_for_complete());
5360 read_comp->release();
5361 }
5362
5363 TEST_F(TestLibRBD, TestImageOptions)
5364 {
5365 rados_ioctx_t ioctx;
5366 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
5367
5368 //make create image options
5369 uint64_t features = RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2 ;
5370 uint64_t order = 0;
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);
5375
5376 bool is_set;
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,
5379 &is_set));
5380 ASSERT_FALSE(is_set);
5381
5382 ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FORMAT,
5383 2));
5384 ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FEATURES,
5385 features));
5386 ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER,
5387 order));
5388 ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT,
5389 stripe_unit));
5390 ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_COUNT,
5391 stripe_count));
5392
5393 ASSERT_EQ(0, rbd_image_options_is_set(opts, RBD_IMAGE_OPTION_FORMAT,
5394 &is_set));
5395 ASSERT_TRUE(is_set);
5396
5397 std::string parent_name = get_temp_image_name();
5398
5399 // make parent
5400 ASSERT_EQ(0, rbd_create4(ioctx, parent_name.c_str(), 4<<20, opts));
5401
5402 // check order is returned in opts
5403 ASSERT_EQ(0, rbd_image_options_get_uint64(opts, RBD_IMAGE_OPTION_ORDER,
5404 &order));
5405 ASSERT_NE((uint64_t)0, order);
5406
5407 // write some data to parent
5408 rbd_image_t 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));
5413
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"));
5418
5419 // clone
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));
5424
5425 // copy
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));
5431
5432 ASSERT_EQ(0, rbd_close(parent));
5433
5434 rbd_image_options_destroy(opts);
5435
5436 rados_ioctx_destroy(ioctx);
5437 }
5438
5439 TEST_F(TestLibRBD, TestImageOptionsPP)
5440 {
5441 librados::IoCtx ioctx;
5442 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5443
5444 //make create image options
5445 uint64_t features = RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2 ;
5446 uint64_t order = 0;
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));
5455
5456 librbd::RBD rbd;
5457 std::string parent_name = get_temp_image_name();
5458
5459 // make parent
5460 ASSERT_EQ(0, rbd.create4(ioctx, parent_name.c_str(), 4<<20, opts));
5461
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);
5465
5466 // write some data to parent
5467 librbd::Image parent;
5468 ASSERT_EQ(0, rbd.open(ioctx, parent, parent_name.c_str(), NULL));
5469
5470 ssize_t len = 1024;
5471 bufferlist bl;
5472 bl.append(buffer::create(len));
5473 bl.zero();
5474 ASSERT_EQ(len, parent.write(0, len, bl));
5475 ASSERT_EQ(len, parent.write(len, len, bl));
5476
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"));
5481
5482 // clone
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));
5487
5488 // copy
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();
5492 PrintProgress pp;
5493 ASSERT_EQ(0, parent.copy_with_progress3(ioctx, copy2_name.c_str(), opts, pp));
5494
5495 ASSERT_EQ(0, parent.close());
5496 }
5497
5498 TEST_F(TestLibRBD, EventSocketPipe)
5499 {
5500 EventSocket event_sock;
5501 int pipe_fd[2]; // read and write fd
5502 char buf[32];
5503
5504 ASSERT_EQ(0, pipe(pipe_fd));
5505
5506 ASSERT_FALSE(event_sock.is_valid());
5507
5508 ASSERT_EQ(-EINVAL, event_sock.init(pipe_fd[1], EVENT_SOCKET_TYPE_NONE));
5509 ASSERT_FALSE(event_sock.is_valid());
5510
5511 ASSERT_EQ(-EINVAL, event_sock.init(pipe_fd[1], 44));
5512 ASSERT_FALSE(event_sock.is_valid());
5513
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());
5517 #endif
5518
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]);
5524
5525 close(pipe_fd[0]);
5526 close(pipe_fd[1]);
5527 }
5528
5529 TEST_F(TestLibRBD, EventSocketEventfd)
5530 {
5531 #ifdef HAVE_EVENTFD
5532 EventSocket event_sock;
5533 int event_fd;
5534 struct pollfd poll_fd;
5535 char buf[32];
5536
5537 event_fd = eventfd(0, EFD_NONBLOCK);
5538 ASSERT_NE(-1, event_fd);
5539
5540 ASSERT_FALSE(event_sock.is_valid());
5541
5542 ASSERT_EQ(-EINVAL, event_sock.init(event_fd, EVENT_SOCKET_TYPE_NONE));
5543 ASSERT_FALSE(event_sock.is_valid());
5544
5545 ASSERT_EQ(-EINVAL, event_sock.init(event_fd, 44));
5546 ASSERT_FALSE(event_sock.is_valid());
5547
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());
5551
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);
5556
5557 ASSERT_EQ(static_cast<ssize_t>(sizeof(uint64_t)), read(event_fd, buf, 32));
5558 ASSERT_EQ(1U, *reinterpret_cast<uint64_t *>(buf));
5559
5560 close(event_fd);
5561 #endif
5562 }
5563
5564 TEST_F(TestLibRBD, ImagePollIO)
5565 {
5566 #ifdef HAVE_EVENTFD
5567 rados_ioctx_t ioctx;
5568 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
5569
5570 rbd_image_t image;
5571 int order = 0;
5572 std::string name = get_temp_image_name();
5573 uint64_t size = 2 << 20;
5574 int fd = eventfd(0, EFD_NONBLOCK);
5575
5576 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
5577 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
5578
5579 ASSERT_EQ(0, rbd_set_image_notification(image, fd, EVENT_SOCKET_TYPE_EVENTFD));
5580
5581 char test_data[TEST_IO_SIZE + 1];
5582 char zero_data[TEST_IO_SIZE + 1];
5583 int i;
5584
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));
5589
5590 for (i = 0; i < 5; ++i)
5591 ASSERT_PASSED(write_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
5592
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);
5595
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);
5598
5599 ASSERT_EQ(0, rbd_close(image));
5600 rados_ioctx_destroy(ioctx);
5601 #endif
5602 }
5603
5604 namespace librbd {
5605
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);
5610 }
5611
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;
5616 return os;
5617 }
5618
5619 } // namespace librbd
5620
5621 TEST_F(TestLibRBD, Mirror) {
5622 librados::IoCtx ioctx;
5623 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5624
5625 librbd::RBD rbd;
5626
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);
5631
5632 std::string uuid1;
5633 ASSERT_EQ(-EINVAL, rbd.mirror_peer_add(ioctx, &uuid1, "cluster1", "client"));
5634
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);
5638
5639 ASSERT_EQ(0, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_IMAGE));
5640 ASSERT_EQ(0, rbd.mirror_mode_get(ioctx, &mirror_mode));
5641
5642 // Add some images to the pool
5643 int order = 0;
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,
5647 &order));
5648 bool old_format;
5649 uint64_t features;
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));
5661 }
5662
5663 ASSERT_EQ(RBD_MIRROR_MODE_IMAGE, mirror_mode);
5664
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);
5668
5669 std::string uuid2;
5670 std::string uuid3;
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"));
5675
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;
5680 };
5681 expected_peers = {
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);
5687
5688 ASSERT_EQ(0, rbd.mirror_peer_remove(ioctx, "uuid4"));
5689 ASSERT_EQ(0, rbd.mirror_peer_remove(ioctx, uuid2));
5690
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"));
5693
5694 ASSERT_EQ(-ENOENT, rbd.mirror_peer_set_cluster(ioctx, "uuid4",
5695 "new cluster"));
5696 ASSERT_EQ(0, rbd.mirror_peer_set_cluster(ioctx, uuid3, "new cluster"));
5697
5698 ASSERT_EQ(0, rbd.mirror_peer_list(ioctx, &peers));
5699 expected_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);
5704
5705 ASSERT_EQ(-EBUSY, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_DISABLED));
5706 }
5707
5708 TEST_F(TestLibRBD, FlushCacheWithCopyupOnExternalSnapshot) {
5709 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
5710
5711 librados::IoCtx ioctx;
5712 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5713
5714 librbd::RBD rbd;
5715 librbd::Image image;
5716 std::string name = get_temp_image_name();
5717
5718 uint64_t size = 1 << 18;
5719 int order = 0;
5720
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));
5723
5724 bufferlist bl;
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"));
5729
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));
5734
5735 librbd::Image image2;
5736 ASSERT_EQ(0, rbd.open(ioctx, image2, clone_name.c_str(), NULL));
5737
5738 // prepare CoW writeback that will be flushed on next op
5739 bl.clear();
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"));
5744
5745 librbd::RBD::AioCompletion *read_comp =
5746 new librbd::RBD::AioCompletion(NULL, NULL);
5747 bufferlist read_bl;
5748 image.aio_read(0, 1024, read_bl, read_comp);
5749 ASSERT_EQ(0, read_comp->wait_for_complete());
5750 read_comp->release();
5751 }
5752
5753 TEST_F(TestLibRBD, ExclusiveLock)
5754 {
5755 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
5756
5757 static char buf[10];
5758
5759 rados_ioctx_t ioctx;
5760 rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
5761
5762 std::string name = get_temp_image_name();
5763 uint64_t size = 2 << 20;
5764 int order = 0;
5765 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
5766
5767 rbd_image_t image1;
5768 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image1, NULL));
5769
5770 int lock_owner;
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);
5774
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,
5779 &max_lock_owners));
5780 ASSERT_EQ(1U, max_lock_owners);
5781
5782 max_lock_owners = 2;
5783 ASSERT_EQ(0, rbd_lock_get_owners(image1, &lock_mode, lock_owners,
5784 &max_lock_owners));
5785 ASSERT_EQ(RBD_LOCK_MODE_EXCLUSIVE, lock_mode);
5786 ASSERT_STRNE("", lock_owners[0]);
5787 ASSERT_EQ(1U, max_lock_owners);
5788
5789 rbd_image_t image2;
5790 ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image2, NULL));
5791
5792 ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2, &lock_owner));
5793 ASSERT_FALSE(lock_owner);
5794
5795 ASSERT_EQ(-EOPNOTSUPP, rbd_lock_break(image1, RBD_LOCK_MODE_SHARED, ""));
5796 ASSERT_EQ(-EBUSY, rbd_lock_break(image1, RBD_LOCK_MODE_EXCLUSIVE,
5797 "not the owner"));
5798
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);
5802
5803 ASSERT_EQ(-ENOENT, rbd_lock_break(image1, RBD_LOCK_MODE_EXCLUSIVE,
5804 lock_owners[0]));
5805 rbd_lock_get_owners_cleanup(lock_owners, max_lock_owners);
5806
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));
5809
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);
5813
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);
5817
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);
5821
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));
5824
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);
5828
5829 int owner_id = -1;
5830 mutex lock;
5831 const auto pingpong = [&,this](int m_id, rbd_image_t &m_image) {
5832 for (int i = 0; i < 10; i++) {
5833 {
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));
5838 int lock_owner;
5839 EXPECT_EQ(0, rbd_is_exclusive_lock_owner(m_image, &lock_owner));
5840 EXPECT_FALSE(lock_owner);
5841 owner_id = -1;
5842 std::cout << m_id << ": exclusive lock released" << std::endl;
5843 continue;
5844 }
5845 }
5846
5847 std::cout << m_id << ": acquiring exclusive lock" << std::endl;
5848 int r;
5849 do {
5850 r = rbd_lock_acquire(m_image, RBD_LOCK_MODE_EXCLUSIVE);
5851 if (r == -EROFS) {
5852 usleep(1000);
5853 }
5854 } while (r == -EROFS);
5855 EXPECT_EQ(0, r);
5856
5857 int lock_owner;
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;
5861 {
5862 lock_guard<mutex> locker(lock);
5863 owner_id = m_id;
5864 }
5865 usleep(rand() % 50000);
5866 }
5867
5868 lock_guard<mutex> locker(lock);
5869 if (owner_id == m_id) {
5870 EXPECT_EQ(0, rbd_lock_release(m_image));
5871 int lock_owner;
5872 EXPECT_EQ(0, rbd_is_exclusive_lock_owner(m_image, &lock_owner));
5873 EXPECT_FALSE(lock_owner);
5874 owner_id = -1;
5875 }
5876 };
5877 thread ping(bind(pingpong, 1, ref(image1)));
5878 thread pong(bind(pingpong, 2, ref(image2)));
5879
5880 ping.join();
5881 pong.join();
5882
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);
5886
5887 ASSERT_EQ(0, rbd_close(image2));
5888
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);
5892
5893 ASSERT_EQ(0, rbd_close(image1));
5894 rados_ioctx_destroy(ioctx);
5895 }
5896
5897 TEST_F(TestLibRBD, BreakLock)
5898 {
5899 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
5900
5901 static char buf[10];
5902
5903 rados_t blacklist_cluster;
5904 ASSERT_EQ("", connect_cluster(&blacklist_cluster));
5905
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(),
5909 &blacklist_ioctx));
5910
5911 std::string name = get_temp_image_name();
5912 uint64_t size = 2 << 20;
5913 int order = 0;
5914 ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
5915
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));
5919
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));
5922
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,
5927 &max_lock_owners));
5928 ASSERT_EQ(RBD_LOCK_MODE_EXCLUSIVE, lock_mode);
5929 ASSERT_STRNE("", lock_owners[0]);
5930 ASSERT_EQ(1U, max_lock_owners);
5931
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));
5935
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));
5938
5939 ASSERT_EQ(0, rbd_close(image));
5940 ASSERT_EQ(0, rbd_close(blacklist_image));
5941
5942 rbd_lock_get_owners_cleanup(lock_owners, max_lock_owners);
5943
5944 rados_ioctx_destroy(ioctx);
5945 rados_ioctx_destroy(blacklist_ioctx);
5946 rados_shutdown(blacklist_cluster);
5947 }
5948
5949 TEST_F(TestLibRBD, DiscardAfterWrite)
5950 {
5951 REQUIRE(!is_skip_partial_discard_enabled());
5952
5953 librados::IoCtx ioctx;
5954 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
5955
5956 librbd::RBD rbd;
5957 std::string name = get_temp_image_name();
5958 uint64_t size = 1 << 20;
5959 int order = 18;
5960 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
5961
5962 librbd::Image image;
5963 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
5964
5965 // enable writeback cache
5966 ASSERT_EQ(0, image.flush());
5967
5968 bufferlist bl;
5969 bl.append(std::string(256, '1'));
5970
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();
5976
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();
5982
5983 librbd::RBD::AioCompletion *read_comp =
5984 new librbd::RBD::AioCompletion(NULL, NULL);
5985 bufferlist read_bl;
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();
5991 }
5992
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()));
5999 };
6000
6001 std::list<std::pair<std::string, std::string> > feature_names_to_bitmask = {
6002 {"", orig_default_features},
6003 {"layering", "1"},
6004 {"layering, exclusive-lock", "5"},
6005 {"exclusive-lock,journaling", "68"},
6006 {"125", "125"}
6007 };
6008
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);
6014 }
6015 }
6016
6017 TEST_F(TestLibRBD, TestTrashMoveAndPurge) {
6018 librados::IoCtx ioctx;
6019 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
6020
6021 librbd::RBD rbd;
6022 std::string name = get_temp_image_name();
6023
6024 uint64_t size = 1 << 18;
6025 int order = 12;
6026 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
6027
6028 librbd::Image image;
6029 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
6030 uint8_t old_format;
6031 ASSERT_EQ(0, image.old_format(&old_format));
6032
6033 if (old_format) {
6034 ASSERT_EQ(-EOPNOTSUPP, rbd.trash_move(ioctx, name.c_str(), 0));
6035 image.close();
6036 return;
6037 }
6038 std::string image_id;
6039 ASSERT_EQ(0, image.get_id(&image_id));
6040 image.close();
6041
6042 ASSERT_EQ(0, rbd.trash_move(ioctx, name.c_str(), 0));
6043
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);
6048 }
6049
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);
6054
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);
6059
6060 entries.clear();
6061 PrintProgress pp;
6062 ASSERT_EQ(0, rbd.trash_remove_with_progress(ioctx, image_id.c_str(),
6063 false, pp));
6064 ASSERT_EQ(0, rbd.trash_list(ioctx, entries));
6065 ASSERT_TRUE(entries.empty());
6066 }
6067
6068 TEST_F(TestLibRBD, TestTrashMoveAndPurgeNonExpiredDelay) {
6069 librados::IoCtx ioctx;
6070 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
6071
6072 librbd::RBD rbd;
6073 std::string name = get_temp_image_name();
6074
6075 uint64_t size = 1 << 18;
6076 int order = 12;
6077 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
6078
6079 librbd::Image image;
6080 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
6081 uint8_t old_format;
6082 ASSERT_EQ(0, image.old_format(&old_format));
6083
6084 if (old_format) {
6085 ASSERT_EQ(-EOPNOTSUPP, rbd.trash_move(ioctx, name.c_str(), 0));
6086 image.close();
6087 return;
6088 }
6089 std::string image_id;
6090 ASSERT_EQ(0, image.get_id(&image_id));
6091 image.close();
6092
6093 ASSERT_EQ(0, rbd.trash_move(ioctx, name.c_str(), 100));
6094
6095 PrintProgress pp;
6096 ASSERT_EQ(-EPERM, rbd.trash_remove_with_progress(ioctx, image_id.c_str(),
6097 false, pp));
6098
6099 PrintProgress pp2;
6100 ASSERT_EQ(0, rbd.trash_remove_with_progress(ioctx, image_id.c_str(),
6101 true, pp2));
6102 }
6103
6104 TEST_F(TestLibRBD, TestTrashMoveAndRestore) {
6105 librados::IoCtx ioctx;
6106 ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
6107
6108 librbd::RBD rbd;
6109 std::string name = get_temp_image_name();
6110
6111 uint64_t size = 1 << 18;
6112 int order = 12;
6113 ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
6114
6115 librbd::Image image;
6116 ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
6117 uint8_t old_format;
6118 ASSERT_EQ(0, image.old_format(&old_format));
6119
6120 if (old_format) {
6121 ASSERT_EQ(-EOPNOTSUPP, rbd.trash_move(ioctx, name.c_str(), 0));
6122 image.close();
6123 return;
6124 }
6125 std::string image_id;
6126 ASSERT_EQ(0, image.get_id(&image_id));
6127 image.close();
6128
6129 ASSERT_EQ(0, rbd.trash_move(ioctx, name.c_str(), 10));
6130
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);
6135 }
6136
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);
6141
6142 images.clear();
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());
6146 bool found = false;
6147 for (const auto& image : images) {
6148 if (image == name) {
6149 found = true;
6150 break;
6151 }
6152 }
6153 ASSERT_TRUE(found);
6154 }
6155
6156 // poorman's assert()
6157 namespace ceph {
6158 void __ceph_assert_fail(const char *assertion, const char *file, int line,
6159 const char *func) {
6160 assert(false);
6161 }
6162 }