forked from ceph/ceph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal.cc
3221 lines (2746 loc) · 93.4 KB
/
internal.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/int_types.h"
#include <errno.h>
#include <limits.h>
#include "include/types.h"
#include "common/ceph_context.h"
#include "common/dout.h"
#include "common/errno.h"
#include "common/ContextCompletion.h"
#include "common/Throttle.h"
#include "common/WorkQueue.h"
#include "common/event_socket.h"
#include "cls/lock/cls_lock_client.h"
#include "include/stringify.h"
#include "cls/rbd/cls_rbd.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/AioCompletion.h"
#include "librbd/AioImageRequest.h"
#include "librbd/AioImageRequestWQ.h"
#include "librbd/AioObjectRequest.h"
#include "librbd/CopyupRequest.h"
#include "librbd/DiffIterate.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/ImageWatcher.h"
#include "librbd/internal.h"
#include "librbd/Journal.h"
#include "librbd/ObjectMap.h"
#include "librbd/parent_types.h"
#include "librbd/Utils.h"
#include "librbd/operation/FlattenRequest.h"
#include "librbd/operation/RebuildObjectMapRequest.h"
#include "librbd/operation/RenameRequest.h"
#include "librbd/operation/ResizeRequest.h"
#include "librbd/operation/SnapshotCreateRequest.h"
#include "librbd/operation/SnapshotProtectRequest.h"
#include "librbd/operation/SnapshotRemoveRequest.h"
#include "librbd/operation/SnapshotRenameRequest.h"
#include "librbd/operation/SnapshotRollbackRequest.h"
#include "librbd/operation/SnapshotUnprotectRequest.h"
#include "librbd/operation/TrimRequest.h"
#include "include/util.h"
#include <boost/bind.hpp>
#include <boost/scope_exit.hpp>
#include <boost/variant.hpp>
#include "include/assert.h"
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd: "
#define rbd_howmany(x, y) (((x) + (y) - 1) / (y))
using std::map;
using std::pair;
using std::set;
using std::string;
using std::vector;
// list binds to list() here, so std::list is explicitly used below
using ceph::bufferlist;
using librados::snap_t;
using librados::IoCtx;
using librados::Rados;
namespace librbd {
namespace {
int remove_object_map(ImageCtx *ictx) {
assert(ictx->snap_lock.is_locked());
CephContext *cct = ictx->cct;
int r;
for (std::map<snap_t, SnapInfo>::iterator it = ictx->snap_info.begin();
it != ictx->snap_info.end(); ++it) {
std::string oid(ObjectMap::object_map_name(ictx->id, it->first));
r = ictx->md_ctx.remove(oid);
if (r < 0 && r != -ENOENT) {
lderr(cct) << "failed to remove object map " << oid << ": "
<< cpp_strerror(r) << dendl;
return r;
}
}
r = ictx->md_ctx.remove(ObjectMap::object_map_name(ictx->id, CEPH_NOSNAP));
if (r < 0 && r != -ENOENT) {
lderr(cct) << "failed to remove object map: " << cpp_strerror(r) << dendl;
return r;
}
return 0;
}
int create_object_map(ImageCtx *ictx) {
assert(ictx->snap_lock.is_locked());
CephContext *cct = ictx->cct;
int r;
std::vector<uint64_t> snap_ids;
snap_ids.push_back(CEPH_NOSNAP);
for (std::map<snap_t, SnapInfo>::iterator it = ictx->snap_info.begin();
it != ictx->snap_info.end(); ++it) {
snap_ids.push_back(it->first);
}
for (std::vector<uint64_t>::iterator it = snap_ids.begin();
it != snap_ids.end(); ++it) {
librados::ObjectWriteOperation op;
std::string oid(ObjectMap::object_map_name(ictx->id, *it));
uint64_t snap_size = ictx->get_image_size(*it);
cls_client::object_map_resize(&op, Striper::get_num_objects(ictx->layout, snap_size),
OBJECT_NONEXISTENT);
r = ictx->md_ctx.operate(oid, &op);
if (r < 0) {
lderr(cct) << "failed to create object map " << oid << ": "
<< cpp_strerror(r) << dendl;
return r;
}
}
return 0;
}
int update_all_flags(ImageCtx *ictx, uint64_t flags, uint64_t mask) {
assert(ictx->snap_lock.is_locked());
CephContext *cct = ictx->cct;
std::vector<uint64_t> snap_ids;
snap_ids.push_back(CEPH_NOSNAP);
for (std::map<snap_t, SnapInfo>::iterator it = ictx->snap_info.begin();
it != ictx->snap_info.end(); ++it) {
snap_ids.push_back(it->first);
}
for (size_t i=0; i<snap_ids.size(); ++i) {
librados::ObjectWriteOperation op;
cls_client::set_flags(&op, snap_ids[i], flags, mask);
int r = ictx->md_ctx.operate(ictx->header_oid, &op);
if (r < 0) {
lderr(cct) << "failed to update image flags: " << cpp_strerror(r)
<< dendl;
return r;
}
}
return 0;
}
int prepare_image_update(ImageCtx *ictx) {
assert(ictx->owner_lock.is_locked() && !ictx->owner_lock.is_wlocked());
if (ictx->image_watcher == NULL) {
return -EROFS;
}
// need to upgrade to a write lock
int r = 0;
bool trying_lock = false;
C_SaferCond ctx;
ictx->owner_lock.put_read();
{
RWLock::WLocker owner_locker(ictx->owner_lock);
if (ictx->exclusive_lock != nullptr &&
!ictx->exclusive_lock->is_lock_owner()) {
ictx->exclusive_lock->try_lock(&ctx);
trying_lock = true;
}
}
if (trying_lock) {
r = ctx.wait();
}
ictx->owner_lock.get_read();
return r;
}
int invoke_async_request(ImageCtx *ictx, const std::string& request_type,
bool permit_snapshot,
const boost::function<void(Context*)>& local_request,
const boost::function<int()>& remote_request) {
int r;
do {
C_SaferCond ctx;
{
RWLock::RLocker l(ictx->owner_lock);
{
RWLock::RLocker snap_locker(ictx->snap_lock);
if (ictx->read_only ||
(!permit_snapshot && ictx->snap_id != CEPH_NOSNAP)) {
return -EROFS;
}
}
while (ictx->exclusive_lock != nullptr) {
r = prepare_image_update(ictx);
if (r < 0) {
return -EROFS;
} else if (ictx->exclusive_lock->is_lock_owner()) {
break;
}
r = remote_request();
if (r != -ETIMEDOUT && r != -ERESTART) {
return r;
}
ldout(ictx->cct, 5) << request_type << " timed out notifying lock owner"
<< dendl;
}
local_request(&ctx);
}
r = ctx.wait();
if (r == -ERESTART) {
ldout(ictx->cct, 5) << request_type << " interrupted: restarting"
<< dendl;
}
} while (r == -ERESTART);
return r;
}
int validate_pool(IoCtx &io_ctx, CephContext *cct) {
if (!cct->_conf->rbd_validate_pool) {
return 0;
}
int r = io_ctx.stat(RBD_DIRECTORY, NULL, NULL);
if (r == 0) {
return 0;
} else if (r < 0 && r != -ENOENT) {
lderr(cct) << "failed to stat RBD directory: " << cpp_strerror(r) << dendl;
return r;
}
// allocate a self-managed snapshot id if this a new pool to force
// self-managed snapshot mode
uint64_t snap_id;
r = io_ctx.selfmanaged_snap_create(&snap_id);
if (r == -EINVAL) {
lderr(cct) << "pool not configured for self-managed RBD snapshot support"
<< dendl;
return r;
} else if (r < 0) {
lderr(cct) << "failed to allocate self-managed snapshot: "
<< cpp_strerror(r) << dendl;
return r;
}
r = io_ctx.selfmanaged_snap_remove(snap_id);
if (r < 0) {
lderr(cct) << "failed to release self-managed snapshot " << snap_id
<< ": " << cpp_strerror(r) << dendl;
}
return 0;
}
} // anonymous namespace
int detect_format(IoCtx &io_ctx, const string &name,
bool *old_format, uint64_t *size)
{
CephContext *cct = (CephContext *)io_ctx.cct();
if (old_format)
*old_format = true;
int r = io_ctx.stat(util::old_header_name(name), size, NULL);
if (r == -ENOENT) {
if (old_format)
*old_format = false;
r = io_ctx.stat(util::id_obj_name(name), size, NULL);
if (r < 0)
return r;
} else if (r < 0) {
return r;
}
ldout(cct, 20) << "detect format of " << name << " : "
<< (old_format ? (*old_format ? "old" : "new") :
"don't care") << dendl;
return 0;
}
bool has_parent(int64_t parent_pool_id, uint64_t off, uint64_t overlap)
{
return (parent_pool_id != -1 && off <= overlap);
}
void init_rbd_header(struct rbd_obj_header_ondisk& ondisk,
uint64_t size, int order, uint64_t bid)
{
uint32_t hi = bid >> 32;
uint32_t lo = bid & 0xFFFFFFFF;
uint32_t extra = rand() % 0xFFFFFFFF;
memset(&ondisk, 0, sizeof(ondisk));
memcpy(&ondisk.text, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT));
memcpy(&ondisk.signature, RBD_HEADER_SIGNATURE,
sizeof(RBD_HEADER_SIGNATURE));
memcpy(&ondisk.version, RBD_HEADER_VERSION, sizeof(RBD_HEADER_VERSION));
snprintf(ondisk.block_name, sizeof(ondisk.block_name), "rb.%x.%x.%x",
hi, lo, extra);
ondisk.image_size = size;
ondisk.options.order = order;
ondisk.options.crypt_type = RBD_CRYPT_NONE;
ondisk.options.comp_type = RBD_COMP_NONE;
ondisk.snap_seq = 0;
ondisk.snap_count = 0;
ondisk.reserved = 0;
ondisk.snap_names_len = 0;
}
void image_info(ImageCtx *ictx, image_info_t& info, size_t infosize)
{
int obj_order = ictx->order;
ictx->snap_lock.get_read();
info.size = ictx->get_image_size(ictx->snap_id);
ictx->snap_lock.put_read();
info.obj_size = 1ULL << obj_order;
info.num_objs = Striper::get_num_objects(ictx->layout, info.size);
info.order = obj_order;
memcpy(&info.block_name_prefix, ictx->object_prefix.c_str(),
min((size_t)RBD_MAX_BLOCK_NAME_SIZE,
ictx->object_prefix.length() + 1));
// clear deprecated fields
info.parent_pool = -1L;
info.parent_name[0] = '\0';
}
uint64_t oid_to_object_no(const string& oid, const string& object_prefix)
{
istringstream iss(oid);
// skip object prefix and separator
iss.ignore(object_prefix.length() + 1);
uint64_t num;
iss >> std::hex >> num;
return num;
}
void trim_image(ImageCtx *ictx, uint64_t newsize, ProgressContext& prog_ctx)
{
assert(ictx->owner_lock.is_locked());
assert(ictx->exclusive_lock == nullptr ||
ictx->exclusive_lock->is_lock_owner());
C_SaferCond ctx;
ictx->snap_lock.get_read();
operation::TrimRequest<> *req = new operation::TrimRequest<>(
*ictx, &ctx, ictx->size, newsize, prog_ctx);
ictx->snap_lock.put_read();
req->send();
int r = ctx.wait();
if (r < 0) {
lderr(ictx->cct) << "warning: failed to remove some object(s): "
<< cpp_strerror(r) << dendl;
}
}
int read_header_bl(IoCtx& io_ctx, const string& header_oid,
bufferlist& header, uint64_t *ver)
{
int r;
uint64_t off = 0;
#define READ_SIZE 4096
do {
bufferlist bl;
r = io_ctx.read(header_oid, bl, READ_SIZE, off);
if (r < 0)
return r;
header.claim_append(bl);
off += r;
} while (r == READ_SIZE);
if (memcmp(RBD_HEADER_TEXT, header.c_str(), sizeof(RBD_HEADER_TEXT))) {
CephContext *cct = (CephContext *)io_ctx.cct();
lderr(cct) << "unrecognized header format" << dendl;
return -ENXIO;
}
if (ver)
*ver = io_ctx.get_last_version();
return 0;
}
int notify_change(IoCtx& io_ctx, const string& oid, ImageCtx *ictx)
{
if (ictx) {
ictx->state->handle_update_notification();
}
ImageWatcher::notify_header_update(io_ctx, oid);
return 0;
}
int read_header(IoCtx& io_ctx, const string& header_oid,
struct rbd_obj_header_ondisk *header, uint64_t *ver)
{
bufferlist header_bl;
int r = read_header_bl(io_ctx, header_oid, header_bl, ver);
if (r < 0)
return r;
if (header_bl.length() < (int)sizeof(*header))
return -EIO;
memcpy(header, header_bl.c_str(), sizeof(*header));
return 0;
}
int tmap_set(IoCtx& io_ctx, const string& imgname)
{
bufferlist cmdbl, emptybl;
__u8 c = CEPH_OSD_TMAP_SET;
::encode(c, cmdbl);
::encode(imgname, cmdbl);
::encode(emptybl, cmdbl);
return io_ctx.tmap_update(RBD_DIRECTORY, cmdbl);
}
int tmap_rm(IoCtx& io_ctx, const string& imgname)
{
bufferlist cmdbl;
__u8 c = CEPH_OSD_TMAP_RM;
::encode(c, cmdbl);
::encode(imgname, cmdbl);
return io_ctx.tmap_update(RBD_DIRECTORY, cmdbl);
}
typedef boost::variant<std::string,uint64_t> image_option_value_t;
typedef std::map<int,image_option_value_t> image_options_t;
typedef std::shared_ptr<image_options_t> image_options_ref;
enum image_option_type_t {
STR,
UINT64,
};
const std::map<int, image_option_type_t> IMAGE_OPTIONS_TYPE_MAPPING = {
{RBD_IMAGE_OPTION_FORMAT, UINT64},
{RBD_IMAGE_OPTION_FEATURES, UINT64},
{RBD_IMAGE_OPTION_ORDER, UINT64},
{RBD_IMAGE_OPTION_STRIPE_UNIT, UINT64},
{RBD_IMAGE_OPTION_STRIPE_COUNT, UINT64},
{RBD_IMAGE_OPTION_JOURNAL_ORDER, UINT64},
{RBD_IMAGE_OPTION_JOURNAL_SPLAY_WIDTH, UINT64},
{RBD_IMAGE_OPTION_JOURNAL_POOL, STR},
};
std::string image_option_name(int optname) {
switch (optname) {
case RBD_IMAGE_OPTION_FORMAT:
return "format";
case RBD_IMAGE_OPTION_FEATURES:
return "features";
case RBD_IMAGE_OPTION_ORDER:
return "order";
case RBD_IMAGE_OPTION_STRIPE_UNIT:
return "stripe_unit";
case RBD_IMAGE_OPTION_STRIPE_COUNT:
return "stripe_count";
case RBD_IMAGE_OPTION_JOURNAL_ORDER:
return "journal_order";
case RBD_IMAGE_OPTION_JOURNAL_SPLAY_WIDTH:
return "journal_splay_width";
case RBD_IMAGE_OPTION_JOURNAL_POOL:
return "journal_pool";
default:
return "unknown (" + stringify(optname) + ")";
}
}
std::ostream &operator<<(std::ostream &os, rbd_image_options_t &opts) {
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
os << "[";
for (image_options_t::const_iterator i = (*opts_)->begin();
i != (*opts_)->end(); i++) {
os << (i == (*opts_)->begin() ? "" : ", ") << image_option_name(i->first)
<< "=" << i->second;
}
os << "]";
return os;
}
std::ostream &operator<<(std::ostream &os, ImageOptions &opts) {
os << "[";
const char *delimiter = "";
for (auto &i : IMAGE_OPTIONS_TYPE_MAPPING) {
if (i.second == STR) {
std::string val;
if (opts.get(i.first, &val) == 0) {
os << delimiter << image_option_name(i.first) << "=" << val;
delimiter = ", ";
}
} else if (i.second == UINT64) {
uint64_t val;
if (opts.get(i.first, &val) == 0) {
os << delimiter << image_option_name(i.first) << "=" << val;
delimiter = ", ";
}
}
}
os << "]";
return os;
}
void image_options_create(rbd_image_options_t* opts)
{
image_options_ref* opts_ = new image_options_ref(new image_options_t());
*opts = static_cast<rbd_image_options_t>(opts_);
}
void image_options_create_ref(rbd_image_options_t* opts,
rbd_image_options_t orig)
{
image_options_ref* orig_ = static_cast<image_options_ref*>(orig);
image_options_ref* opts_ = new image_options_ref(*orig_);
*opts = static_cast<rbd_image_options_t>(opts_);
}
void image_options_destroy(rbd_image_options_t opts)
{
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
delete opts_;
}
int image_options_set(rbd_image_options_t opts, int optname,
const std::string& optval)
{
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
std::map<int, image_option_type_t>::const_iterator i =
IMAGE_OPTIONS_TYPE_MAPPING.find(optname);
if (i == IMAGE_OPTIONS_TYPE_MAPPING.end() || i->second != STR) {
return -EINVAL;
}
(*opts_->get())[optname] = optval;
return 0;
}
int image_options_set(rbd_image_options_t opts, int optname, uint64_t optval)
{
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
std::map<int, image_option_type_t>::const_iterator i =
IMAGE_OPTIONS_TYPE_MAPPING.find(optname);
if (i == IMAGE_OPTIONS_TYPE_MAPPING.end() || i->second != UINT64) {
return -EINVAL;
}
(*opts_->get())[optname] = optval;
return 0;
}
int image_options_get(rbd_image_options_t opts, int optname,
std::string* optval)
{
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
std::map<int, image_option_type_t>::const_iterator i =
IMAGE_OPTIONS_TYPE_MAPPING.find(optname);
if (i == IMAGE_OPTIONS_TYPE_MAPPING.end() || i->second != STR) {
return -EINVAL;
}
image_options_t::const_iterator j = (*opts_)->find(optname);
if (j == (*opts_)->end()) {
return -ENOENT;
}
*optval = boost::get<std::string>(j->second);
return 0;
}
int image_options_get(rbd_image_options_t opts, int optname, uint64_t* optval)
{
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
std::map<int, image_option_type_t>::const_iterator i =
IMAGE_OPTIONS_TYPE_MAPPING.find(optname);
if (i == IMAGE_OPTIONS_TYPE_MAPPING.end() || i->second != UINT64) {
return -EINVAL;
}
image_options_t::const_iterator j = (*opts_)->find(optname);
if (j == (*opts_)->end()) {
return -ENOENT;
}
*optval = boost::get<uint64_t>(j->second);
return 0;
}
int image_options_unset(rbd_image_options_t opts, int optname)
{
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
std::map<int, image_option_type_t>::const_iterator i =
IMAGE_OPTIONS_TYPE_MAPPING.find(optname);
if (i == IMAGE_OPTIONS_TYPE_MAPPING.end()) {
assert((*opts_)->find(optname) == (*opts_)->end());
return -EINVAL;
}
image_options_t::const_iterator j = (*opts_)->find(optname);
if (j == (*opts_)->end()) {
return -ENOENT;
}
(*opts_)->erase(j);
return 0;
}
void image_options_clear(rbd_image_options_t opts)
{
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
(*opts_)->clear();
}
bool image_options_is_empty(rbd_image_options_t opts)
{
image_options_ref* opts_ = static_cast<image_options_ref*>(opts);
return (*opts_)->empty();
}
int list(IoCtx& io_ctx, vector<string>& names)
{
CephContext *cct = (CephContext *)io_ctx.cct();
ldout(cct, 20) << "list " << &io_ctx << dendl;
bufferlist bl;
int r = io_ctx.read(RBD_DIRECTORY, bl, 0, 0);
if (r < 0)
return r;
// old format images are in a tmap
if (bl.length()) {
bufferlist::iterator p = bl.begin();
bufferlist header;
map<string,bufferlist> m;
::decode(header, p);
::decode(m, p);
for (map<string,bufferlist>::iterator q = m.begin(); q != m.end(); ++q) {
names.push_back(q->first);
}
}
// new format images are accessed by class methods
int max_read = 1024;
string last_read = "";
do {
map<string, string> images;
cls_client::dir_list(&io_ctx, RBD_DIRECTORY,
last_read, max_read, &images);
for (map<string, string>::const_iterator it = images.begin();
it != images.end(); ++it) {
names.push_back(it->first);
}
if (!images.empty()) {
last_read = images.rbegin()->first;
}
r = images.size();
} while (r == max_read);
return 0;
}
int list_children(ImageCtx *ictx, set<pair<string, string> >& names)
{
CephContext *cct = ictx->cct;
ldout(cct, 20) << "children list " << ictx->name << dendl;
int r = ictx->state->refresh_if_required();
if (r < 0)
return r;
// no children for non-layered or old format image
if (!ictx->test_features(RBD_FEATURE_LAYERING))
return 0;
parent_spec parent_spec(ictx->md_ctx.get_id(), ictx->id, ictx->snap_id);
names.clear();
// search all pools for children depending on this snapshot
Rados rados(ictx->md_ctx);
std::list<std::pair<int64_t, string> > pools;
rados.pool_list2(pools);
for (std::list<std::pair<int64_t, string> >::const_iterator it =
pools.begin(); it != pools.end(); ++it) {
int64_t base_tier;
r = rados.pool_get_base_tier(it->first, &base_tier);
if (r == -ENOENT) {
ldout(cct, 1) << "pool " << it->second << " no longer exists" << dendl;
continue;
} else if (r < 0) {
lderr(cct) << "Error retrieving base tier for pool " << it->second
<< dendl;
return r;
}
if (it->first != base_tier) {
// pool is a cache; skip it
continue;
}
IoCtx ioctx;
r = rados.ioctx_create2(it->first, ioctx);
if (r == -ENOENT) {
ldout(cct, 1) << "pool " << it->second << " no longer exists" << dendl;
continue;
} else if (r < 0) {
lderr(cct) << "Error accessing child image pool " << it->second
<< dendl;
return r;
}
set<string> image_ids;
r = cls_client::get_children(&ioctx, RBD_CHILDREN, parent_spec,
image_ids);
if (r < 0 && r != -ENOENT) {
lderr(cct) << "Error reading list of children from pool " << it->second
<< dendl;
return r;
}
for (set<string>::const_iterator id_it = image_ids.begin();
id_it != image_ids.end(); ++id_it) {
string name;
r = cls_client::dir_get_name(&ioctx, RBD_DIRECTORY,
*id_it, &name);
if (r < 0) {
lderr(cct) << "Error looking up name for image id " << *id_it
<< " in pool " << it->second << dendl;
return r;
}
names.insert(make_pair(it->second, name));
}
}
return 0;
}
int snap_create(ImageCtx *ictx, const char *snap_name)
{
ldout(ictx->cct, 20) << "snap_create " << ictx << " " << snap_name << dendl;
if (ictx->read_only) {
return -EROFS;
}
int r = ictx->state->refresh_if_required();
if (r < 0)
return r;
{
RWLock::RLocker l(ictx->snap_lock);
if (ictx->get_snap_id(snap_name) != CEPH_NOSNAP) {
return -EEXIST;
}
}
r = invoke_async_request(ictx, "snap_create", true,
boost::bind(&snap_create_helper, ictx, _1,
snap_name),
boost::bind(&ImageWatcher::notify_snap_create,
ictx->image_watcher, snap_name));
if (r < 0 && r != -EEXIST) {
return r;
}
ictx->perfcounter->inc(l_librbd_snap_create);
notify_change(ictx->md_ctx, ictx->header_oid, ictx);
return 0;
}
void snap_create_helper(ImageCtx* ictx, Context* ctx, const char* snap_name) {
assert(ictx->owner_lock.is_locked());
assert(ictx->exclusive_lock == nullptr ||
ictx->exclusive_lock->is_lock_owner());
ldout(ictx->cct, 20) << "snap_create_helper " << ictx << " " << snap_name
<< dendl;
operation::SnapshotCreateRequest<> *req =
new operation::SnapshotCreateRequest<>(*ictx, ctx, snap_name);
req->send();
}
int snap_remove(ImageCtx *ictx, const char *snap_name)
{
ldout(ictx->cct, 20) << "snap_remove " << ictx << " " << snap_name << dendl;
if (ictx->read_only)
return -EROFS;
int r = ictx->state->refresh_if_required();
if (r < 0)
return r;
bool proxy_op = false;
{
RWLock::RLocker snap_locker(ictx->snap_lock);
if (ictx->get_snap_id(snap_name) == CEPH_NOSNAP) {
return -ENOENT;
}
proxy_op = ((ictx->features & RBD_FEATURE_FAST_DIFF) != 0 ||
(ictx->features & RBD_FEATURE_JOURNALING) != 0);
}
if (proxy_op) {
r = invoke_async_request(ictx, "snap_remove", true,
boost::bind(&snap_remove_helper, ictx, _1,
snap_name),
boost::bind(&ImageWatcher::notify_snap_remove,
ictx->image_watcher, snap_name));
if (r < 0 && r != -ENOENT) {
return r;
}
} else {
RWLock::RLocker owner_lock(ictx->owner_lock);
C_SaferCond cond_ctx;
snap_remove_helper(ictx, &cond_ctx, snap_name);
r = cond_ctx.wait();
if (r < 0) {
return r;
}
}
notify_change(ictx->md_ctx, ictx->header_oid, ictx);
ictx->perfcounter->inc(l_librbd_snap_remove);
return 0;
}
void snap_remove_helper(ImageCtx *ictx, Context *ctx, const char *snap_name)
{
assert(ictx->owner_lock.is_locked());
{
if ((ictx->features & RBD_FEATURE_FAST_DIFF) != 0) {
assert(ictx->exclusive_lock == nullptr ||
ictx->exclusive_lock->is_lock_owner());
}
}
ldout(ictx->cct, 20) << "snap_remove_helper " << ictx << " " << snap_name
<< dendl;
uint64_t snap_id;
{
RWLock::RLocker snap_locker(ictx->snap_lock);
snap_id = ictx->get_snap_id(snap_name);
if (snap_id == CEPH_NOSNAP) {
lderr(ictx->cct) << "No such snapshot found." << dendl;
ctx->complete(-ENOENT);
return;
}
bool is_protected;
int r = ictx->is_snap_protected(snap_id, &is_protected);
if (r < 0) {
ctx->complete(r);
return;
} else if (is_protected) {
lderr(ictx->cct) << "snapshot is protected" << dendl;
ctx->complete(-EBUSY);
return;
}
}
operation::SnapshotRemoveRequest<> *req =
new operation::SnapshotRemoveRequest<>(*ictx, ctx, snap_name, snap_id);
req->send();
}
int snap_rename(ImageCtx *ictx, const char *srcname, const char *dstname)
{
ldout(ictx->cct, 20) << "snap_rename " << ictx << " from " << srcname << " to " << dstname << dendl;
snapid_t snap_id;
if (ictx->read_only) {
return -EROFS;
}
int r = ictx->state->refresh_if_required();
if (r < 0)
return r;
{
RWLock::RLocker l(ictx->snap_lock);
snap_id = ictx->get_snap_id(srcname);
if (snap_id == CEPH_NOSNAP) {
return -ENOENT;
}
if (ictx->get_snap_id(dstname) != CEPH_NOSNAP) {
return -EEXIST;
}
}
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
r = invoke_async_request(ictx, "snap_rename", true,
boost::bind(&snap_rename_helper, ictx, _1,
snap_id, dstname),
boost::bind(&ImageWatcher::notify_snap_rename,
ictx->image_watcher, snap_id,
dstname));
if (r < 0 && r != -EEXIST) {
return r;
}
} else {
RWLock::RLocker owner_lock(ictx->owner_lock);
C_SaferCond cond_ctx;
snap_rename_helper(ictx, &cond_ctx, snap_id, dstname);
r = cond_ctx.wait();
if (r < 0) {
return r;
}
}
ictx->perfcounter->inc(l_librbd_snap_rename);
notify_change(ictx->md_ctx, ictx->header_oid, ictx);
return 0;
}
void snap_rename_helper(ImageCtx* ictx, Context* ctx,
const uint64_t src_snap_id, const char* dst_name) {
assert(ictx->owner_lock.is_locked());
if ((ictx->features & RBD_FEATURE_JOURNALING) != 0) {
assert(ictx->exclusive_lock == nullptr ||
ictx->exclusive_lock->is_lock_owner());
}
ldout(ictx->cct, 20) << __func__ << " " << ictx << " from "
<< src_snap_id << " to " << dst_name << dendl;
operation::SnapshotRenameRequest<> *req =
new operation::SnapshotRenameRequest<>(*ictx, ctx, src_snap_id, dst_name);
req->send();
}
int snap_protect(ImageCtx *ictx, const char *snap_name)
{
ldout(ictx->cct, 20) << "snap_protect " << ictx << " " << snap_name
<< dendl;
if (ictx->read_only) {
return -EROFS;
}
int r = ictx->state->refresh_if_required();
if (r < 0) {
return r;
}
{
RWLock::RLocker snap_locker(ictx->snap_lock);
bool is_protected;
r = ictx->is_snap_protected(ictx->get_snap_id(snap_name), &is_protected);
if (r < 0) {
return r;
}
if (is_protected) {
return -EBUSY;
}
}
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
r = invoke_async_request(ictx, "snap_protect", true,
boost::bind(&snap_protect_helper, ictx, _1,
snap_name),
boost::bind(&ImageWatcher::notify_snap_protect,
ictx->image_watcher, snap_name));