]> git.proxmox.com Git - ceph.git/blame - ceph/src/librbd/api/Group.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / librbd / api / Group.cc
CommitLineData
11fdf7f2
TL
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#include "common/errno.h"
5
6#include "librbd/ExclusiveLock.h"
7#include "librbd/api/Group.h"
8#include "librbd/ImageCtx.h"
9#include "librbd/ImageState.h"
10#include "librbd/ImageWatcher.h"
11#include "librbd/Operations.h"
12#include "librbd/Utils.h"
13#include "librbd/io/AioCompletion.h"
14
15#define dout_subsys ceph_subsys_rbd
16#undef dout_prefix
17#define dout_prefix *_dout << "librbd::api::Group: " << __func__ << ": "
18
19using std::map;
20using std::pair;
21using std::set;
22using std::string;
23using std::vector;
24// list binds to list() here, so std::list is explicitly used below
25
26using ceph::bufferlist;
27using librados::snap_t;
28using librados::IoCtx;
29using librados::Rados;
30
31
32namespace librbd {
33namespace api {
34
35namespace {
36
37template <typename I>
38snap_t get_group_snap_id(I* ictx,
39 const cls::rbd::SnapshotNamespace& in_snap_namespace) {
40 ceph_assert(ictx->snap_lock.is_locked());
41 auto it = ictx->snap_ids.lower_bound({in_snap_namespace, ""});
42 if (it != ictx->snap_ids.end() && it->first.first == in_snap_namespace) {
43 return it->second;
44 }
45 return CEPH_NOSNAP;
46}
47
48string generate_uuid(librados::IoCtx& io_ctx)
49{
50 Rados rados(io_ctx);
51 uint64_t bid = rados.get_instance_id();
52
53 uint32_t extra = rand() % 0xFFFFFFFF;
54 ostringstream bid_ss;
55 bid_ss << std::hex << bid << std::hex << extra;
56 return bid_ss.str();
57}
58
59int group_snap_list(librados::IoCtx& group_ioctx, const char *group_name,
60 std::vector<cls::rbd::GroupSnapshot> *cls_snaps)
61{
62 CephContext *cct = (CephContext *)group_ioctx.cct();
63
64 string group_id;
65 vector<string> ind_snap_names;
66
67 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY,
68 group_name, &group_id);
69 if (r < 0) {
70 lderr(cct) << "error reading group id object: "
71 << cpp_strerror(r)
72 << dendl;
73 return r;
74 }
75 string group_header_oid = util::group_header_name(group_id);
76
77 const int max_read = 1024;
78 cls::rbd::GroupSnapshot snap_last;
79
80 for (;;) {
81 vector<cls::rbd::GroupSnapshot> snaps_page;
82
83 r = cls_client::group_snap_list(&group_ioctx, group_header_oid,
84 snap_last, max_read, &snaps_page);
85
86 if (r < 0) {
87 lderr(cct) << "error reading snap list from group: "
88 << cpp_strerror(-r) << dendl;
89 return r;
90 }
91 cls_snaps->insert(cls_snaps->end(), snaps_page.begin(), snaps_page.end());
92 if (snaps_page.size() < max_read) {
93 break;
94 }
95 snap_last = *snaps_page.rbegin();
96 }
97
98 return 0;
99}
100
101std::string calc_ind_image_snap_name(uint64_t pool_id,
102 const std::string &group_id,
103 const std::string &snap_id)
104{
105 std::stringstream ind_snap_name_stream;
106 ind_snap_name_stream << ".group." << std::hex << pool_id << "_"
107 << group_id << "_" << snap_id;
108 return ind_snap_name_stream.str();
109}
110
111int group_image_list(librados::IoCtx& group_ioctx, const char *group_name,
112 std::vector<cls::rbd::GroupImageStatus> *image_ids)
113{
114 CephContext *cct = (CephContext *)group_ioctx.cct();
115
116 string group_id;
117
118 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY,
119 group_name, &group_id);
120 if (r < 0) {
121 lderr(cct) << "error reading group id object: "
122 << cpp_strerror(r)
123 << dendl;
124 return r;
125 }
126 string group_header_oid = util::group_header_name(group_id);
127
128 ldout(cct, 20) << "listing images in group name "
129 << group_name << " group id " << group_header_oid << dendl;
130 image_ids->clear();
131
132 const int max_read = 1024;
133 cls::rbd::GroupImageSpec start_last;
134 do {
135 std::vector<cls::rbd::GroupImageStatus> image_ids_page;
136
137 r = cls_client::group_image_list(&group_ioctx, group_header_oid,
138 start_last, max_read, &image_ids_page);
139
140 if (r < 0) {
141 lderr(cct) << "error reading image list from group: "
142 << cpp_strerror(-r) << dendl;
143 return r;
144 }
145 image_ids->insert(image_ids->end(),
146 image_ids_page.begin(), image_ids_page.end());
147
148 if (image_ids_page.size() > 0)
149 start_last = image_ids_page.rbegin()->spec;
150
151 r = image_ids_page.size();
152 } while (r == max_read);
153
154 return 0;
155}
156
157int group_image_remove(librados::IoCtx& group_ioctx, string group_id,
158 librados::IoCtx& image_ioctx, string image_id)
159{
160 CephContext *cct = (CephContext *)group_ioctx.cct();
161
162 string group_header_oid = util::group_header_name(group_id);
163
164 string image_header_oid = util::header_name(image_id);
165
166 ldout(cct, 20) << "removing image " << image_id
167 << " image id " << image_header_oid << dendl;
168
169 cls::rbd::GroupSpec group_spec(group_id, group_ioctx.get_id());
170
171 cls::rbd::GroupImageStatus incomplete_st(image_id, image_ioctx.get_id(),
172 cls::rbd::GROUP_IMAGE_LINK_STATE_INCOMPLETE);
173
174 cls::rbd::GroupImageSpec spec(image_id, image_ioctx.get_id());
175
176 int r = cls_client::group_image_set(&group_ioctx, group_header_oid,
177 incomplete_st);
178
179 if (r < 0) {
180 lderr(cct) << "couldn't put image into removing state: "
181 << cpp_strerror(-r) << dendl;
182 return r;
183 }
184
185 r = cls_client::image_group_remove(&image_ioctx, image_header_oid,
186 group_spec);
187 if ((r < 0) && (r != -ENOENT)) {
188 lderr(cct) << "couldn't remove group reference from image"
189 << cpp_strerror(-r) << dendl;
190 return r;
191 } else if (r >= 0) {
192 ImageWatcher<>::notify_header_update(image_ioctx, image_header_oid);
193 }
194
195 r = cls_client::group_image_remove(&group_ioctx, group_header_oid, spec);
196 if (r < 0) {
197 lderr(cct) << "couldn't remove image from group"
198 << cpp_strerror(-r) << dendl;
199 return r;
200 }
201
202 return 0;
203}
204
205int group_snap_remove_by_record(librados::IoCtx& group_ioctx,
206 const cls::rbd::GroupSnapshot& group_snap,
207 const std::string& group_id,
208 const std::string& group_header_oid) {
209
210 CephContext *cct = (CephContext *)group_ioctx.cct();
211 std::vector<C_SaferCond*> on_finishes;
212 int r, ret_code;
213
214 std::vector<librbd::ImageCtx*> ictxs;
215
216 cls::rbd::GroupSnapshotNamespace ne{group_ioctx.get_id(), group_id,
217 group_snap.id};
218
219 ldout(cct, 20) << "Removing snapshots" << dendl;
220 int snap_count = group_snap.snaps.size();
221
222 for (int i = 0; i < snap_count; ++i) {
223 librbd::IoCtx image_io_ctx;
224 r = util::create_ioctx(group_ioctx, "image", group_snap.snaps[i].pool, {},
225 &image_io_ctx);
226 if (r < 0) {
227 return r;
228 }
229
230 librbd::ImageCtx* image_ctx = new ImageCtx("", group_snap.snaps[i].image_id,
231 nullptr, image_io_ctx, false);
232
233 C_SaferCond* on_finish = new C_SaferCond;
234
235 image_ctx->state->open(0, on_finish);
236
237 ictxs.push_back(image_ctx);
238 on_finishes.push_back(on_finish);
239 }
240
241 ret_code = 0;
242 for (int i = 0; i < snap_count; ++i) {
243 r = on_finishes[i]->wait();
244 delete on_finishes[i];
245 if (r < 0) {
246 delete ictxs[i];
247 ictxs[i] = nullptr;
248 ret_code = r;
249 }
250 }
251 if (ret_code != 0) {
252 goto finish;
253 }
254
255 ldout(cct, 20) << "Opened participating images. " <<
256 "Deleting snapshots themselves." << dendl;
257
258 for (int i = 0; i < snap_count; ++i) {
259 ImageCtx *ictx = ictxs[i];
260 on_finishes[i] = new C_SaferCond;
261
262 std::string snap_name;
263 ictx->snap_lock.get_read();
264 snap_t snap_id = get_group_snap_id(ictx, ne);
265 r = ictx->get_snap_name(snap_id, &snap_name);
266 ictx->snap_lock.put_read();
267
268 if (r >= 0) {
269 ldout(cct, 20) << "removing individual snapshot from image " << ictx->name
270 << dendl;
271 ictx->operations->snap_remove(ne, snap_name, on_finishes[i]);
272 } else {
273 // We are ok to ignore missing image snapshots. The snapshot could have
274 // been inconsistent in the first place.
275 on_finishes[i]->complete(0);
276 }
277 }
278
279 for (int i = 0; i < snap_count; ++i) {
280 r = on_finishes[i]->wait();
281 delete on_finishes[i];
282 if (r < 0 && r != -ENOENT) {
283 // if previous attempts to remove this snapshot failed then the image's
284 // snapshot may not exist
285 lderr(cct) << "Failed deleting image snapshot. Ret code: " << r << dendl;
286 ret_code = r;
287 }
288 }
289
290 if (ret_code != 0) {
291 goto finish;
292 }
293
294 ldout(cct, 20) << "Removed images snapshots removing snapshot record."
295 << dendl;
296
297 r = cls_client::group_snap_remove(&group_ioctx, group_header_oid,
298 group_snap.id);
299 if (r < 0) {
300 ret_code = r;
301 goto finish;
302 }
303
304finish:
305 for (int i = 0; i < snap_count; ++i) {
306 if (ictxs[i] != nullptr) {
307 ictxs[i]->state->close();
308 }
309 }
310 return ret_code;
311}
312
313int group_snap_rollback_by_record(librados::IoCtx& group_ioctx,
314 const cls::rbd::GroupSnapshot& group_snap,
315 const std::string& group_id,
316 const std::string& group_header_oid,
317 ProgressContext& pctx) {
318 CephContext *cct = (CephContext *)group_ioctx.cct();
319 std::vector<C_SaferCond*> on_finishes;
320 int r, ret_code;
321
322 std::vector<librbd::ImageCtx*> ictxs;
323
324 cls::rbd::GroupSnapshotNamespace ne{group_ioctx.get_id(), group_id,
325 group_snap.id};
326
327 ldout(cct, 20) << "Rolling back snapshots" << dendl;
328 int snap_count = group_snap.snaps.size();
329
330 for (int i = 0; i < snap_count; ++i) {
331 librados::IoCtx image_io_ctx;
332 r = util::create_ioctx(group_ioctx, "image", group_snap.snaps[i].pool, {},
333 &image_io_ctx);
334 if (r < 0) {
335 return r;
336 }
337
338 librbd::ImageCtx* image_ctx = new ImageCtx("", group_snap.snaps[i].image_id,
339 nullptr, image_io_ctx, false);
340
341 C_SaferCond* on_finish = new C_SaferCond;
342
343 image_ctx->state->open(0, on_finish);
344
345 ictxs.push_back(image_ctx);
346 on_finishes.push_back(on_finish);
347 }
348
349 ret_code = 0;
350 for (int i = 0; i < snap_count; ++i) {
351 r = on_finishes[i]->wait();
352 delete on_finishes[i];
353 if (r < 0) {
354 delete ictxs[i];
355 ictxs[i] = nullptr;
356 ret_code = r;
357 }
358 }
359 if (ret_code != 0) {
360 goto finish;
361 }
362
363 ldout(cct, 20) << "Requesting exclusive locks for images" << dendl;
364 for (auto ictx: ictxs) {
365 RWLock::RLocker owner_lock(ictx->owner_lock);
366 if (ictx->exclusive_lock != nullptr) {
367 ictx->exclusive_lock->block_requests(-EBUSY);
368 }
369 }
370 for (int i = 0; i < snap_count; ++i) {
371 ImageCtx *ictx = ictxs[i];
372 RWLock::RLocker owner_lock(ictx->owner_lock);
373
374 on_finishes[i] = new C_SaferCond;
375 if (ictx->exclusive_lock != nullptr) {
376 ictx->exclusive_lock->acquire_lock(on_finishes[i]);
377 }
378 }
379
380 ret_code = 0;
381 for (int i = 0; i < snap_count; ++i) {
382 r = 0;
383 ImageCtx *ictx = ictxs[i];
384 if (ictx->exclusive_lock != nullptr) {
385 r = on_finishes[i]->wait();
386 }
387 delete on_finishes[i];
388 if (r < 0) {
389 ret_code = r;
390 }
391 }
392 if (ret_code != 0) {
393 goto finish;
394 }
395
396 for (int i = 0; i < snap_count; ++i) {
397 ImageCtx *ictx = ictxs[i];
398 on_finishes[i] = new C_SaferCond;
399
400 RWLock::RLocker owner_locker(ictx->owner_lock);
401 std::string snap_name;
402 ictx->snap_lock.get_read();
403 snap_t snap_id = get_group_snap_id(ictx, ne);
404 r = ictx->get_snap_name(snap_id, &snap_name);
405 ictx->snap_lock.put_read();
406
407 if (r >= 0) {
408 ldout(cct, 20) << "rolling back to individual snapshot for image " << ictx->name
409 << dendl;
410 ictx->operations->execute_snap_rollback(ne, snap_name, pctx, on_finishes[i]);
411 } else {
412 on_finishes[i]->complete(r);
413 }
414 }
415
416 for (int i = 0; i < snap_count; ++i) {
417 r = on_finishes[i]->wait();
418 delete on_finishes[i];
419 if (r < 0 && r != -ENOENT) {
420 lderr(cct) << "Failed rolling back group to snapshot. Ret code: " << r << dendl;
421 ret_code = r;
422 }
423 }
424
425finish:
426 for (int i = 0; i < snap_count; ++i) {
427 if (ictxs[i] != nullptr) {
428 ictxs[i]->state->close();
429 }
430 }
431 return ret_code;
432}
433
434} // anonymous namespace
435
436template <typename I>
437int Group<I>::image_remove_by_id(librados::IoCtx& group_ioctx,
438 const char *group_name,
439 librados::IoCtx& image_ioctx,
440 const char *image_id)
441{
442 CephContext *cct = (CephContext *)group_ioctx.cct();
443 ldout(cct, 20) << "io_ctx=" << &group_ioctx
444 << " group name " << group_name << " image "
445 << &image_ioctx << " id " << image_id << dendl;
446
447 string group_id;
448
449 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY, group_name,
450 &group_id);
451 if (r < 0) {
452 lderr(cct) << "error reading group id object: "
453 << cpp_strerror(r)
454 << dendl;
455 return r;
456 }
457
458 ldout(cct, 20) << "removing image from group name " << group_name
459 << " group id " << group_id << dendl;
460
461 return group_image_remove(group_ioctx, group_id, image_ioctx, string(image_id));
462}
463
464template <typename I>
465int Group<I>::create(librados::IoCtx& io_ctx, const char *group_name)
466{
467 CephContext *cct = (CephContext *)io_ctx.cct();
468
469 string id = generate_uuid(io_ctx);
470
471 ldout(cct, 2) << "adding group to directory..." << dendl;
472
473 int r = cls_client::group_dir_add(&io_ctx, RBD_GROUP_DIRECTORY, group_name,
474 id);
475 if (r < 0) {
476 lderr(cct) << "error adding group to directory: "
477 << cpp_strerror(r)
478 << dendl;
479 return r;
480 }
481 string header_oid = util::group_header_name(id);
482
483 r = io_ctx.create(header_oid, true);
484 if (r < 0) {
485 lderr(cct) << "error creating group header: " << cpp_strerror(r) << dendl;
486 goto err_remove_from_dir;
487 }
488
489 return 0;
490
491err_remove_from_dir:
492 int remove_r = cls_client::group_dir_remove(&io_ctx, RBD_GROUP_DIRECTORY,
493 group_name, id);
494 if (remove_r < 0) {
495 lderr(cct) << "error cleaning up group from rbd_directory "
496 << "object after creation failed: " << cpp_strerror(remove_r)
497 << dendl;
498 }
499
500 return r;
501}
502
503template <typename I>
504int Group<I>::remove(librados::IoCtx& io_ctx, const char *group_name)
505{
506 CephContext *cct((CephContext *)io_ctx.cct());
507 ldout(cct, 20) << "group_remove " << &io_ctx << " " << group_name << dendl;
508
509 std::string group_id;
510 int r = cls_client::dir_get_id(&io_ctx, RBD_GROUP_DIRECTORY,
511 std::string(group_name), &group_id);
512 if (r < 0 && r != -ENOENT) {
513 lderr(cct) << "error getting id of group" << dendl;
514 return r;
515 }
516 string group_header_oid = util::group_header_name(group_id);
517
518 std::vector<cls::rbd::GroupSnapshot> snaps;
519 r = group_snap_list(io_ctx, group_name, &snaps);
520 if (r < 0 && r != -ENOENT) {
521 lderr(cct) << "error listing group snapshots" << dendl;
522 return r;
523 }
524
525 for (auto &snap : snaps) {
526 r = group_snap_remove_by_record(io_ctx, snap, group_id, group_header_oid);
527 if (r < 0) {
528 return r;
529 }
530 }
531
532 std::vector<cls::rbd::GroupImageStatus> images;
533 r = group_image_list(io_ctx, group_name, &images);
534 if (r < 0 && r != -ENOENT) {
535 lderr(cct) << "error listing group images" << dendl;
536 return r;
537 }
538
539 for (auto image : images) {
540 IoCtx image_ioctx;
541 r = util::create_ioctx(io_ctx, "image", image.spec.pool_id, {},
542 &image_ioctx);
543 if (r < 0) {
544 return r;
545 }
546
547 r = group_image_remove(io_ctx, group_id, image_ioctx, image.spec.image_id);
548 if (r < 0 && r != -ENOENT) {
549 lderr(cct) << "error removing image from a group" << dendl;
550 return r;
551 }
552 }
553
554 string header_oid = util::group_header_name(group_id);
555
556 r = io_ctx.remove(header_oid);
557 if (r < 0 && r != -ENOENT) {
558 lderr(cct) << "error removing header: " << cpp_strerror(-r) << dendl;
559 return r;
560 }
561
562 r = cls_client::group_dir_remove(&io_ctx, RBD_GROUP_DIRECTORY,
563 group_name, group_id);
564 if (r < 0 && r != -ENOENT) {
565 lderr(cct) << "error removing group from directory" << dendl;
566 return r;
567 }
568
569 return 0;
570}
571
572template <typename I>
573int Group<I>::list(IoCtx& io_ctx, vector<string> *names)
574{
575 CephContext *cct = (CephContext *)io_ctx.cct();
576 ldout(cct, 20) << "io_ctx=" << &io_ctx << dendl;
577
578 int max_read = 1024;
579 string last_read = "";
580 int r;
581 do {
582 map<string, string> groups;
583 r = cls_client::group_dir_list(&io_ctx, RBD_GROUP_DIRECTORY, last_read,
584 max_read, &groups);
585 if (r == -ENOENT) {
586 return 0; // Ignore missing rbd group directory. It means we don't have any groups yet.
587 }
588 if (r < 0) {
589 if (r != -ENOENT) {
590 lderr(cct) << "error listing group in directory: "
591 << cpp_strerror(r) << dendl;
592 } else {
593 r = 0;
594 }
595 return r;
596 }
597 for (pair<string, string> group : groups) {
598 names->push_back(group.first);
599 }
600 if (!groups.empty()) {
601 last_read = groups.rbegin()->first;
602 }
603 r = groups.size();
604 } while (r == max_read);
605
606 return 0;
607}
608
609template <typename I>
610int Group<I>::image_add(librados::IoCtx& group_ioctx, const char *group_name,
611 librados::IoCtx& image_ioctx, const char *image_name)
612{
613 CephContext *cct = (CephContext *)group_ioctx.cct();
614 ldout(cct, 20) << "io_ctx=" << &group_ioctx
615 << " group name " << group_name << " image "
616 << &image_ioctx << " name " << image_name << dendl;
617
618 if (group_ioctx.get_namespace() != image_ioctx.get_namespace()) {
619 lderr(cct) << "group and image cannot be in different namespaces" << dendl;
620 return -EINVAL;
621 }
622
623 string group_id;
624
625 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY, group_name,
626 &group_id);
627 if (r < 0) {
628 lderr(cct) << "error reading group id object: "
629 << cpp_strerror(r)
630 << dendl;
631 return r;
632 }
633 string group_header_oid = util::group_header_name(group_id);
634
635
636 ldout(cct, 20) << "adding image to group name " << group_name
637 << " group id " << group_header_oid << dendl;
638
639 string image_id;
640
641 r = cls_client::dir_get_id(&image_ioctx, RBD_DIRECTORY, image_name,
642 &image_id);
643 if (r < 0) {
644 lderr(cct) << "error reading image id object: "
645 << cpp_strerror(-r) << dendl;
646 return r;
647 }
648
649 string image_header_oid = util::header_name(image_id);
650
651 ldout(cct, 20) << "adding image " << image_name
652 << " image id " << image_header_oid << dendl;
653
654 cls::rbd::GroupImageStatus incomplete_st(
655 image_id, image_ioctx.get_id(),
656 cls::rbd::GROUP_IMAGE_LINK_STATE_INCOMPLETE);
657 cls::rbd::GroupImageStatus attached_st(
658 image_id, image_ioctx.get_id(), cls::rbd::GROUP_IMAGE_LINK_STATE_ATTACHED);
659
660 r = cls_client::group_image_set(&group_ioctx, group_header_oid,
661 incomplete_st);
662
663 cls::rbd::GroupSpec group_spec(group_id, group_ioctx.get_id());
664
665 if (r < 0) {
666 lderr(cct) << "error adding image reference to group: "
667 << cpp_strerror(-r) << dendl;
668 return r;
669 }
670
671 r = cls_client::image_group_add(&image_ioctx, image_header_oid, group_spec);
672 if (r < 0) {
673 lderr(cct) << "error adding group reference to image: "
674 << cpp_strerror(-r) << dendl;
675 cls::rbd::GroupImageSpec spec(image_id, image_ioctx.get_id());
676 cls_client::group_image_remove(&group_ioctx, group_header_oid, spec);
677 // Ignore errors in the clean up procedure.
678 return r;
679 }
680 ImageWatcher<>::notify_header_update(image_ioctx, image_header_oid);
681
682 r = cls_client::group_image_set(&group_ioctx, group_header_oid,
683 attached_st);
684
685 return r;
686}
687
688template <typename I>
689int Group<I>::image_remove(librados::IoCtx& group_ioctx, const char *group_name,
690 librados::IoCtx& image_ioctx, const char *image_name)
691{
692 CephContext *cct = (CephContext *)group_ioctx.cct();
693 ldout(cct, 20) << "io_ctx=" << &group_ioctx
694 << " group name " << group_name << " image "
695 << &image_ioctx << " name " << image_name << dendl;
696
697 if (group_ioctx.get_namespace() != image_ioctx.get_namespace()) {
698 lderr(cct) << "group and image cannot be in different namespaces" << dendl;
699 return -EINVAL;
700 }
701
702 string group_id;
703
704 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY, group_name,
705 &group_id);
706 if (r < 0) {
707 lderr(cct) << "error reading group id object: "
708 << cpp_strerror(r)
709 << dendl;
710 return r;
711 }
712
713 ldout(cct, 20) << "removing image from group name " << group_name
714 << " group id " << group_id << dendl;
715
716 string image_id;
717 r = cls_client::dir_get_id(&image_ioctx, RBD_DIRECTORY, image_name,
718 &image_id);
719 if (r < 0) {
720 lderr(cct) << "error reading image id object: "
721 << cpp_strerror(-r) << dendl;
722 return r;
723 }
724
725 r = group_image_remove(group_ioctx, group_id, image_ioctx, image_id);
726
727 return r;
728}
729
730template <typename I>
731int Group<I>::image_list(librados::IoCtx& group_ioctx,
732 const char *group_name,
733 std::vector<group_image_info_t>* images)
734{
735 CephContext *cct = (CephContext *)group_ioctx.cct();
736 ldout(cct, 20) << "io_ctx=" << &group_ioctx
737 << " group name " << group_name << dendl;
738
739 std::vector<cls::rbd::GroupImageStatus> image_ids;
740
741 group_image_list(group_ioctx, group_name, &image_ids);
742
743 for (auto image_id : image_ids) {
744 IoCtx ioctx;
745 int r = util::create_ioctx(group_ioctx, "image", image_id.spec.pool_id, {},
746 &ioctx);
747 if (r < 0) {
748 return r;
749 }
750
751 std::string image_name;
752 r = cls_client::dir_get_name(&ioctx, RBD_DIRECTORY,
753 image_id.spec.image_id, &image_name);
754 if (r < 0) {
755 return r;
756 }
757
758 images->push_back(
759 group_image_info_t {
760 image_name,
761 ioctx.get_id(),
762 static_cast<group_image_state_t>(image_id.state)});
763 }
764
765 return 0;
766}
767
768template <typename I>
769int Group<I>::rename(librados::IoCtx& io_ctx, const char *src_name,
770 const char *dest_name)
771{
772 CephContext *cct((CephContext *)io_ctx.cct());
773 ldout(cct, 20) << "group_rename " << &io_ctx << " " << src_name
774 << " -> " << dest_name << dendl;
775
776 std::string group_id;
777 int r = cls_client::dir_get_id(&io_ctx, RBD_GROUP_DIRECTORY,
778 std::string(src_name), &group_id);
779 if (r < 0) {
780 if (r != -ENOENT)
781 lderr(cct) << "error getting id of group" << dendl;
782 return r;
783 }
784
785 r = cls_client::group_dir_rename(&io_ctx, RBD_GROUP_DIRECTORY,
786 src_name, dest_name, group_id);
787 if (r < 0 && r != -ENOENT) {
788 lderr(cct) << "error renaming group from directory" << dendl;
789 return r;
790 }
791
792 return 0;
793}
794
795
796template <typename I>
797int Group<I>::image_get_group(I *ictx, group_info_t *group_info)
798{
799 int r = ictx->state->refresh_if_required();
800 if (r < 0)
801 return r;
802
803 if (RBD_GROUP_INVALID_POOL != ictx->group_spec.pool_id) {
804 IoCtx ioctx;
805 r = util::create_ioctx(ictx->md_ctx, "group", ictx->group_spec.pool_id, {},
806 &ioctx);
807 if (r < 0) {
808 return r;
809 }
810
811 std::string group_name;
812 r = cls_client::dir_get_name(&ioctx, RBD_GROUP_DIRECTORY,
813 ictx->group_spec.group_id, &group_name);
814 if (r < 0)
815 return r;
816 group_info->pool = ioctx.get_id();
817 group_info->name = group_name;
818 } else {
819 group_info->pool = RBD_GROUP_INVALID_POOL;
820 group_info->name = "";
821 }
822
823 return 0;
824}
825
826template <typename I>
827int Group<I>::snap_create(librados::IoCtx& group_ioctx,
828 const char *group_name, const char *snap_name)
829{
830 CephContext *cct = (CephContext *)group_ioctx.cct();
831
832 string group_id;
833 cls::rbd::GroupSnapshot group_snap;
834 vector<cls::rbd::ImageSnapshotSpec> image_snaps;
835 std::string ind_snap_name;
836
837 std::vector<librbd::ImageCtx*> ictxs;
838 std::vector<C_SaferCond*> on_finishes;
839
840 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY,
841 group_name, &group_id);
842 if (r < 0) {
843 lderr(cct) << "error reading group id object: "
844 << cpp_strerror(r)
845 << dendl;
846 return r;
847 }
848
849 std::vector<cls::rbd::GroupImageStatus> images;
850 r = group_image_list(group_ioctx, group_name, &images);
851 if (r < 0) {
852 return r;
853 }
854 int image_count = images.size();
855
856 ldout(cct, 20) << "Found " << image_count << " images in group" << dendl;
857
858 image_snaps = vector<cls::rbd::ImageSnapshotSpec>(image_count,
859 cls::rbd::ImageSnapshotSpec());
860
861 for (int i = 0; i < image_count; ++i) {
862 image_snaps[i].pool = images[i].spec.pool_id;
863 image_snaps[i].image_id = images[i].spec.image_id;
864 }
865
866 string group_header_oid = util::group_header_name(group_id);
867
868 group_snap.id = generate_uuid(group_ioctx);
869 group_snap.name = string(snap_name);
870 group_snap.state = cls::rbd::GROUP_SNAPSHOT_STATE_INCOMPLETE;
871 group_snap.snaps = image_snaps;
872
873 cls::rbd::GroupSnapshotNamespace ne{group_ioctx.get_id(), group_id,
874 group_snap.id};
875
876 r = cls_client::group_snap_set(&group_ioctx, group_header_oid, group_snap);
877 if (r == -EEXIST) {
878 lderr(cct) << "snapshot with this name already exists: "
879 << cpp_strerror(r)
880 << dendl;
881 }
882 int ret_code = 0;
883 if (r < 0) {
884 ret_code = r;
885 goto finish;
886 }
887
888 for (auto image: images) {
889 librbd::IoCtx image_io_ctx;
890 r = util::create_ioctx(group_ioctx, "image", image.spec.pool_id, {},
891 &image_io_ctx);
892 if (r < 0) {
893 ret_code = r;
894 goto finish;
895 }
896
897 ldout(cct, 20) << "Opening image with id " << image.spec.image_id << dendl;
898
899 librbd::ImageCtx* image_ctx = new ImageCtx("", image.spec.image_id.c_str(),
900 nullptr, image_io_ctx, false);
901
902 C_SaferCond* on_finish = new C_SaferCond;
903
904 image_ctx->state->open(0, on_finish);
905
906 ictxs.push_back(image_ctx);
907 on_finishes.push_back(on_finish);
908 }
909 ldout(cct, 20) << "Issued open request waiting for the completion" << dendl;
910 ret_code = 0;
911 for (int i = 0; i < image_count; ++i) {
912
913 ldout(cct, 20) << "Waiting for completion on on_finish: " <<
914 on_finishes[i] << dendl;
915
916 r = on_finishes[i]->wait();
917 delete on_finishes[i];
918 if (r < 0) {
919 delete ictxs[i];
920 ictxs[i] = nullptr;
921 ret_code = r;
922 }
923 }
924 if (ret_code != 0) {
925 goto remove_record;
926 }
927 ldout(cct, 20) << "Requesting exclusive locks for images" << dendl;
928
929 for (auto ictx: ictxs) {
930 RWLock::RLocker owner_lock(ictx->owner_lock);
931 if (ictx->exclusive_lock != nullptr) {
932 ictx->exclusive_lock->block_requests(-EBUSY);
933 }
934 }
935 for (int i = 0; i < image_count; ++i) {
936 ImageCtx *ictx = ictxs[i];
937 RWLock::RLocker owner_lock(ictx->owner_lock);
938
939 on_finishes[i] = new C_SaferCond;
940 if (ictx->exclusive_lock != nullptr) {
941 ictx->exclusive_lock->acquire_lock(on_finishes[i]);
942 }
943 }
944
945 ret_code = 0;
946 for (int i = 0; i < image_count; ++i) {
947 r = 0;
948 ImageCtx *ictx = ictxs[i];
949 if (ictx->exclusive_lock != nullptr) {
950 r = on_finishes[i]->wait();
951 }
952 delete on_finishes[i];
953 if (r < 0) {
954 ret_code = r;
955 }
956 }
957 if (ret_code != 0) {
958 goto remove_record;
959 }
960
961 ind_snap_name = calc_ind_image_snap_name(group_ioctx.get_id(), group_id,
962 group_snap.id);
963
964 for (int i = 0; i < image_count; ++i) {
965 ImageCtx *ictx = ictxs[i];
966
967 C_SaferCond* on_finish = new C_SaferCond;
968
969 ictx->operations->snap_create(ne, ind_snap_name.c_str(), on_finish);
970
971 on_finishes[i] = on_finish;
972 }
973
974 ret_code = 0;
975 for (int i = 0; i < image_count; ++i) {
976 r = on_finishes[i]->wait();
977 delete on_finishes[i];
978 if (r < 0) {
979 ret_code = r;
980 } else {
981 ImageCtx *ictx = ictxs[i];
982 ictx->snap_lock.get_read();
983 snap_t snap_id = get_group_snap_id(ictx, ne);
984 ictx->snap_lock.put_read();
985 if (snap_id == CEPH_NOSNAP) {
986 ldout(cct, 20) << "Couldn't find created snapshot with namespace: "
987 << ne << dendl;
988 ret_code = -ENOENT;
989 } else {
990 image_snaps[i].snap_id = snapid_t(snap_id);
991 image_snaps[i].pool = ictx->md_ctx.get_id();
992 image_snaps[i].image_id = ictx->id;
993 }
994 }
995 }
996 if (ret_code != 0) {
997 goto remove_image_snaps;
998 }
999
1000 group_snap.snaps = image_snaps;
1001 group_snap.state = cls::rbd::GROUP_SNAPSHOT_STATE_COMPLETE;
1002
1003 r = cls_client::group_snap_set(&group_ioctx, group_header_oid, group_snap);
1004 if (r < 0) {
1005 ret_code = r;
1006 goto remove_image_snaps;
1007 }
1008
1009 goto finish;
1010
1011remove_image_snaps:
1012
1013 for (int i = 0; i < image_count; ++i) {
1014 ImageCtx *ictx = ictxs[i];
1015 ldout(cct, 20) << "Removing individual snapshot with name: " <<
1016 ind_snap_name << dendl;
1017
1018 on_finishes[i] = new C_SaferCond;
1019 std::string snap_name;
1020 ictx->snap_lock.get_read();
1021 snap_t snap_id = get_group_snap_id(ictx, ne);
1022 r = ictx->get_snap_name(snap_id, &snap_name);
1023 ictx->snap_lock.put_read();
1024 if (r >= 0) {
1025 ictx->operations->snap_remove(ne, snap_name.c_str(), on_finishes[i]);
1026 } else {
1027 // Ignore missing image snapshots. The whole snapshot could have been
1028 // inconsistent.
1029 on_finishes[i]->complete(0);
1030 }
1031 }
1032
1033 for (int i = 0, n = on_finishes.size(); i < n; ++i) {
1034 r = on_finishes[i]->wait();
1035 delete on_finishes[i];
1036 if (r < 0 && r != -ENOENT) { // if previous attempts to remove this snapshot failed then the image's snapshot may not exist
1037 lderr(cct) << "Failed cleaning up image snapshot. Ret code: " << r << dendl;
1038 // just report error, but don't abort the process
1039 }
1040 }
1041
1042remove_record:
1043 r = cls_client::group_snap_remove(&group_ioctx, group_header_oid,
1044 group_snap.id);
1045 if (r < 0) {
1046 lderr(cct) << "error while cleaning up group snapshot" << dendl;
1047 // we ignore return value in clean up
1048 }
1049
1050finish:
1051 for (int i = 0, n = ictxs.size(); i < n; ++i) {
1052 if (ictxs[i] != nullptr) {
1053 ictxs[i]->state->close();
1054 }
1055 }
1056 return ret_code;
1057}
1058
1059template <typename I>
1060int Group<I>::snap_remove(librados::IoCtx& group_ioctx, const char *group_name,
1061 const char *snap_name)
1062{
1063 CephContext *cct = (CephContext *)group_ioctx.cct();
1064
1065 string group_id;
1066 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY,
1067 group_name, &group_id);
1068 if (r < 0) {
1069 lderr(cct) << "error reading group id object: "
1070 << cpp_strerror(r)
1071 << dendl;
1072 return r;
1073 }
1074
1075 std::vector<cls::rbd::GroupSnapshot> snaps;
1076 r = group_snap_list(group_ioctx, group_name, &snaps);
1077 if (r < 0) {
1078 return r;
1079 }
1080
1081 cls::rbd::GroupSnapshot *group_snap = nullptr;
1082 for (auto &snap : snaps) {
1083 if (snap.name == string(snap_name)) {
1084 group_snap = &snap;
1085 break;
1086 }
1087 }
1088 if (group_snap == nullptr) {
1089 return -ENOENT;
1090 }
1091
1092 string group_header_oid = util::group_header_name(group_id);
1093 r = group_snap_remove_by_record(group_ioctx, *group_snap, group_id,
1094 group_header_oid);
1095 return r;
1096}
1097
1098template <typename I>
1099int Group<I>::snap_rename(librados::IoCtx& group_ioctx, const char *group_name,
1100 const char *old_snap_name,
1101 const char *new_snap_name) {
1102 CephContext *cct = (CephContext *)group_ioctx.cct();
1103 if (0 == strcmp(old_snap_name, new_snap_name))
1104 return -EEXIST;
1105
1106 std::string group_id;
1107 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY,
1108 group_name, &group_id);
1109 if (r == -ENOENT) {
1110 return r;
1111 } else if (r < 0) {
1112 lderr(cct) << "error reading group id object: " << cpp_strerror(r) << dendl;
1113 return r;
1114 }
1115
1116 std::vector<cls::rbd::GroupSnapshot> group_snaps;
1117 r = group_snap_list(group_ioctx, group_name, &group_snaps);
1118 if (r < 0) {
1119 return r;
1120 }
1121
1122 cls::rbd::GroupSnapshot group_snap;
1123 for (auto &snap : group_snaps) {
1124 if (snap.name == old_snap_name) {
1125 group_snap = snap;
1126 break;
1127 }
1128 }
1129
1130 if (group_snap.id.empty()) {
1131 return -ENOENT;
1132 }
1133
1134 std::string group_header_oid = util::group_header_name(group_id);
1135 group_snap.name = new_snap_name;
1136 r = cls_client::group_snap_set(&group_ioctx, group_header_oid, group_snap);
1137 if (r < 0) {
1138 return r;
1139 }
1140
1141 return 0;
1142}
1143
1144template <typename I>
1145int Group<I>::snap_list(librados::IoCtx& group_ioctx, const char *group_name,
1146 std::vector<group_snap_info_t> *snaps)
1147{
1148 std::vector<cls::rbd::GroupSnapshot> cls_snaps;
1149
1150 int r = group_snap_list(group_ioctx, group_name, &cls_snaps);
1151 if (r < 0) {
1152 return r;
1153 }
1154
1155 for (auto snap : cls_snaps) {
1156 snaps->push_back(
1157 group_snap_info_t {
1158 snap.name,
1159 static_cast<group_snap_state_t>(snap.state)});
1160
1161 }
1162 return 0;
1163}
1164
1165template <typename I>
1166int Group<I>::snap_rollback(librados::IoCtx& group_ioctx,
1167 const char *group_name, const char *snap_name,
1168 ProgressContext& pctx)
1169{
1170 CephContext *cct = (CephContext *)group_ioctx.cct();
1171
1172 string group_id;
1173 int r = cls_client::dir_get_id(&group_ioctx, RBD_GROUP_DIRECTORY,
1174 group_name, &group_id);
1175 if (r < 0) {
1176 lderr(cct) << "error reading group id object: "
1177 << cpp_strerror(r) << dendl;
1178 return r;
1179 }
1180
1181 std::vector<cls::rbd::GroupSnapshot> snaps;
1182 r = group_snap_list(group_ioctx, group_name, &snaps);
1183 if (r < 0) {
1184 return r;
1185 }
1186
1187 cls::rbd::GroupSnapshot *group_snap = nullptr;
1188 for (auto &snap : snaps) {
1189 if (snap.name == string(snap_name)) {
1190 group_snap = &snap;
1191 break;
1192 }
1193 }
1194 if (group_snap == nullptr) {
1195 return -ENOENT;
1196 }
1197
1198 string group_header_oid = util::group_header_name(group_id);
1199 r = group_snap_rollback_by_record(group_ioctx, *group_snap, group_id,
1200 group_header_oid, pctx);
1201 return r;
1202}
1203
1204} // namespace api
1205} // namespace librbd
1206
1207template class librbd::api::Group<librbd::ImageCtx>;