/
internal.cc
3220 lines (2762 loc) · 89.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 "common/ceph_context.h"
#include "common/dout.h"
#include "common/errno.h"
#include "common/Throttle.h"
#include "cls/lock/cls_lock_client.h"
#include "include/stringify.h"
#include "cls/rbd/cls_rbd.h"
#include "librbd/AioCompletion.h"
#include "librbd/AioRequest.h"
#include "librbd/ImageCtx.h"
#include "librbd/internal.h"
#include "librbd/parent_types.h"
#include "include/util.h"
#include "librados/snap_set_diff.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 {
const string id_obj_name(const string &name)
{
return RBD_ID_PREFIX + name;
}
const string header_name(const string &image_id)
{
return RBD_HEADER_PREFIX + image_id;
}
const string old_header_name(const string &image_name)
{
return image_name + RBD_SUFFIX;
}
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(old_header_name(name), size, NULL);
if (r < 0) {
if (old_format)
*old_format = false;
r = io_ctx.stat(id_obj_name(name), size, NULL);
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->md_lock.get_read();
ictx->snap_lock.get_read();
info.size = ictx->get_image_size(ictx->snap_id);
ictx->snap_lock.put_read();
ictx->md_lock.put_read();
info.obj_size = 1ULL << obj_order;
info.num_objs = rbd_howmany(info.size, ictx->get_object_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;
}
int init_rbd_info(struct rbd_info *info)
{
memset(info, 0, sizeof(*info));
return 0;
}
void trim_image(ImageCtx *ictx, uint64_t newsize, ProgressContext& prog_ctx)
{
CephContext *cct = (CephContext *)ictx->data_ctx.cct();
uint64_t size = ictx->get_current_size();
uint64_t period = ictx->get_stripe_period();
uint64_t num_period = ((newsize + period - 1) / period);
uint64_t delete_off = MIN(num_period * period, size);
// first object we can delete free and clear
uint64_t delete_start = num_period * ictx->get_stripe_count();
uint64_t num_objects = ictx->get_num_objects();
uint64_t object_size = ictx->get_object_size();
ldout(cct, 10) << "trim_image " << size << " -> " << newsize
<< " periods " << num_period
<< " discard to offset " << delete_off
<< " delete objects " << delete_start
<< " to " << (num_objects-1)
<< dendl;
SimpleThrottle throttle(cct->_conf->rbd_concurrent_management_ops, true);
if (delete_start < num_objects) {
ldout(cct, 2) << "trim_image objects " << delete_start << " to "
<< (num_objects - 1) << dendl;
for (uint64_t i = delete_start; i < num_objects; ++i) {
string oid = ictx->get_object_name(i);
Context *req_comp = new C_SimpleThrottle(&throttle);
librados::AioCompletion *rados_completion =
librados::Rados::aio_create_completion(req_comp, NULL, rados_ctx_cb);
ictx->data_ctx.aio_remove(oid, rados_completion);
rados_completion->release();
prog_ctx.update_progress((i - delete_start) * object_size,
(num_objects - delete_start) * object_size);
}
}
// discard the weird boundary, if any
if (delete_off > newsize) {
vector<ObjectExtent> extents;
Striper::file_to_extents(ictx->cct, ictx->format_string, &ictx->layout,
newsize, delete_off - newsize, 0, extents);
for (vector<ObjectExtent>::iterator p = extents.begin();
p != extents.end(); ++p) {
ldout(ictx->cct, 20) << " ex " << *p << dendl;
Context *req_comp = new C_SimpleThrottle(&throttle);
librados::AioCompletion *rados_completion =
librados::Rados::aio_create_completion(req_comp, NULL, rados_ctx_cb);
if (p->offset == 0) {
ictx->data_ctx.aio_remove(p->oid.name, rados_completion);
} else {
librados::ObjectWriteOperation op;
op.truncate(p->offset);
ictx->data_ctx.aio_operate(p->oid.name, rados_completion, &op);
}
rados_completion->release();
}
}
int r = throttle.wait_for_ret();
if (r < 0) {
lderr(cct) << "warning: failed to remove some object(s): "
<< cpp_strerror(r) << dendl;
}
}
int read_rbd_info(IoCtx& io_ctx, const string& info_oid,
struct rbd_info *info)
{
int r;
bufferlist bl;
r = io_ctx.read(info_oid, bl, sizeof(*info), 0);
if (r < 0)
return r;
if (r == 0) {
return init_rbd_info(info);
}
if (r < (int)sizeof(*info))
return -EIO;
memcpy(info, bl.c_str(), r);
return 0;
}
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, uint64_t *pver,
ImageCtx *ictx)
{
uint64_t ver;
if (ictx) {
ictx->refresh_lock.Lock();
ldout(ictx->cct, 20) << "notify_change refresh_seq = " << ictx->refresh_seq
<< " last_refresh = " << ictx->last_refresh << dendl;
++ictx->refresh_seq;
ictx->refresh_lock.Unlock();
}
if (pver)
ver = *pver;
else
ver = io_ctx.get_last_version();
bufferlist bl;
io_ctx.notify(oid, ver, bl);
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 write_header(IoCtx& io_ctx, const string& header_oid, bufferlist& header)
{
bufferlist bl;
int r = io_ctx.write(header_oid, header, header.length(), 0);
notify_change(io_ctx, header_oid, NULL, NULL);
return r;
}
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);
}
int rollback_image(ImageCtx *ictx, uint64_t snap_id,
ProgressContext& prog_ctx)
{
uint64_t numseg = ictx->get_num_objects();
uint64_t bsize = ictx->get_object_size();
int r;
CephContext *cct = ictx->cct;
SimpleThrottle throttle(cct->_conf->rbd_concurrent_management_ops, true);
for (uint64_t i = 0; i < numseg; i++) {
string oid = ictx->get_object_name(i);
Context *req_comp = new C_SimpleThrottle(&throttle);
librados::AioCompletion *rados_completion =
librados::Rados::aio_create_completion(req_comp, NULL, rados_ctx_cb);
librados::ObjectWriteOperation op;
op.selfmanaged_snap_rollback(snap_id);
ictx->data_ctx.aio_operate(oid, rados_completion, &op);
ldout(cct, 10) << "scheduling selfmanaged_snap_rollback on "
<< oid << " to " << snap_id << dendl;
rados_completion->release();
prog_ctx.update_progress(i * bsize, numseg * bsize);
}
r = throttle.wait_for_ret();
if (r < 0) {
ldout(cct, 10) << "failed to rollback at least one object: "
<< cpp_strerror(r) << dendl;
return r;
}
return 0;
}
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_check(ictx);
if (r < 0)
return r;
// no children for non-layered or old format image
if ((ictx->features & RBD_FEATURE_LAYERING) == 0)
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<string> pools;
rados.pool_list(pools);
for (std::list<string>::const_iterator it = pools.begin();
it != pools.end(); ++it) {
IoCtx ioctx;
r = rados.ioctx_create(it->c_str(), ioctx);
if (r == -ENOENT) {
ldout(cct, 1) << "pool " << *it << " no longer exists" << dendl;
continue;
} else if (r < 0) {
lderr(cct) << "Error accessing child image pool " << *it << dendl;
return r;
}
set<string> image_ids;
int 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
<< 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 << dendl;
return r;
}
names.insert(make_pair(*it, 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_check(ictx);
if (r < 0)
return r;
RWLock::RLocker l(ictx->md_lock);
do {
r = add_snap(ictx, snap_name);
} while (r == -ESTALE);
if (r < 0)
return r;
notify_change(ictx->md_ctx, ictx->header_oid, NULL, ictx);
ictx->perfcounter->inc(l_librbd_snap_create);
return 0;
}
static int scan_for_parents(ImageCtx *ictx, parent_spec &pspec,
snapid_t oursnap_id)
{
if (pspec.pool_id != -1) {
map<string, SnapInfo>::iterator it;
for (it = ictx->snaps_by_name.begin();
it != ictx->snaps_by_name.end(); ++it) {
// skip our snap id (if checking base image, CEPH_NOSNAP won't match)
if (it->second.id == oursnap_id)
continue;
if (it->second.parent.spec == pspec)
break;
}
if (it == ictx->snaps_by_name.end())
return -ENOENT;
}
return 0;
}
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_check(ictx);
if (r < 0)
return r;
RWLock::RLocker l(ictx->md_lock);
snap_t snap_id;
{
// block for purposes of auto-destruction of l2 on early return
RWLock::RLocker l2(ictx->snap_lock);
snap_id = ictx->get_snap_id(snap_name);
if (snap_id == CEPH_NOSNAP)
return -ENOENT;
parent_spec our_pspec;
RWLock::RLocker l3(ictx->parent_lock);
r = ictx->get_parent_spec(snap_id, &our_pspec);
if (r < 0) {
lderr(ictx->cct) << "snap_remove: can't get parent spec" << dendl;
return r;
}
if (ictx->parent_md.spec != our_pspec &&
(scan_for_parents(ictx, our_pspec, snap_id) == -ENOENT)) {
r = cls_client::remove_child(&ictx->md_ctx, RBD_CHILDREN,
our_pspec, ictx->id);
if (r < 0)
return r;
}
}
r = rm_snap(ictx, snap_name);
if (r < 0)
return r;
r = ictx->data_ctx.selfmanaged_snap_remove(snap_id);
if (r < 0)
return r;
notify_change(ictx->md_ctx, ictx->header_oid, NULL, ictx);
ictx->perfcounter->inc(l_librbd_snap_remove);
return 0;
}
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_check(ictx);
if (r < 0)
return r;
RWLock::RLocker l(ictx->md_lock);
RWLock::RLocker l2(ictx->snap_lock);
uint64_t features;
ictx->get_features(ictx->snap_id, &features);
if ((features & RBD_FEATURE_LAYERING) == 0) {
lderr(ictx->cct) << "snap_protect: image must support layering"
<< dendl;
return -ENOSYS;
}
snap_t snap_id = ictx->get_snap_id(snap_name);
if (snap_id == CEPH_NOSNAP)
return -ENOENT;
bool is_protected;
r = ictx->is_snap_protected(snap_name, &is_protected);
if (r < 0)
return r;
if (is_protected)
return -EBUSY;
r = cls_client::set_protection_status(&ictx->md_ctx,
ictx->header_oid,
snap_id,
RBD_PROTECTION_STATUS_PROTECTED);
if (r < 0)
return r;
notify_change(ictx->md_ctx, ictx->header_oid, NULL, ictx);
return 0;
}
int snap_unprotect(ImageCtx *ictx, const char *snap_name)
{
ldout(ictx->cct, 20) << "snap_unprotect " << ictx << " " << snap_name
<< dendl;
if (ictx->read_only)
return -EROFS;
int r = ictx_check(ictx);
if (r < 0)
return r;
RWLock::RLocker l(ictx->md_lock);
RWLock::RLocker l2(ictx->snap_lock);
uint64_t features;
ictx->get_features(ictx->snap_id, &features);
if ((features & RBD_FEATURE_LAYERING) == 0) {
lderr(ictx->cct) << "snap_unprotect: image must support layering"
<< dendl;
return -ENOSYS;
}
snap_t snap_id = ictx->get_snap_id(snap_name);
if (snap_id == CEPH_NOSNAP)
return -ENOENT;
bool is_unprotected;
r = ictx->is_snap_unprotected(snap_name, &is_unprotected);
if (r < 0)
return r;
if (is_unprotected) {
lderr(ictx->cct) << "snap_unprotect: snapshot is already unprotected"
<< dendl;
return -EINVAL;
}
r = cls_client::set_protection_status(&ictx->md_ctx,
ictx->header_oid,
snap_id,
RBD_PROTECTION_STATUS_UNPROTECTING);
if (r < 0)
return r;
notify_change(ictx->md_ctx, ictx->header_oid, NULL, ictx);
parent_spec pspec(ictx->md_ctx.get_id(), ictx->id, snap_id);
// search all pools for children depending on this snapshot
Rados rados(ictx->md_ctx);
// protect against pools being renamed/deleted
bool retry_pool_check;
do {
retry_pool_check = false;
std::list<std::string> pools;
rados.pool_list(pools);
for (std::list<std::string>::const_iterator it = pools.begin(); it != pools.end(); ++it) {
IoCtx pool_ioctx;
r = rados.ioctx_create(it->c_str(), pool_ioctx);
if (r == -ENOENT) {
ldout(ictx->cct, 1) << "pool " << *it << " no longer exists" << dendl;
retry_pool_check = true;
break;
} else if (r < 0) {
lderr(ictx->cct) << "snap_unprotect: can't create ioctx for pool "
<< *it << dendl;
goto reprotect_and_return_err;
}
std::set<std::string> children;
r = cls_client::get_children(&pool_ioctx, RBD_CHILDREN, pspec, children);
// key should not exist for this parent if there is no entry
if (((r < 0) && (r != -ENOENT))) {
lderr(ictx->cct) << "can't get children for pool " << *it << dendl;
goto reprotect_and_return_err;
}
// if we found a child, can't unprotect
if (r == 0) {
lderr(ictx->cct) << "snap_unprotect: can't unprotect; at least "
<< children.size() << " child(ren) in pool "
<< it->c_str() << dendl;
r = -EBUSY;
goto reprotect_and_return_err;
}
pool_ioctx.close(); // last one out will self-destruct
}
} while(retry_pool_check);
// didn't find any child in any pool, go ahead with unprotect
r = cls_client::set_protection_status(&ictx->md_ctx,
ictx->header_oid,
snap_id,
RBD_PROTECTION_STATUS_UNPROTECTED);
notify_change(ictx->md_ctx, ictx->header_oid, NULL, ictx);
return 0;
reprotect_and_return_err:
int proterr = cls_client::set_protection_status(&ictx->md_ctx,
ictx->header_oid,
snap_id,
RBD_PROTECTION_STATUS_PROTECTED);
if (proterr < 0) {
lderr(ictx->cct) << "snap_unprotect: can't reprotect image" << dendl;
}
notify_change(ictx->md_ctx, ictx->header_oid, NULL, ictx);
return r;
}
int snap_is_protected(ImageCtx *ictx, const char *snap_name,
bool *is_protected)
{
ldout(ictx->cct, 20) << "snap_is_protected " << ictx << " " << snap_name
<< dendl;
int r = ictx_check(ictx);
if (r < 0)
return r;
RWLock::RLocker l(ictx->snap_lock);
bool is_unprotected;
r = ictx->is_snap_unprotected(snap_name, &is_unprotected);
// consider both PROTECTED or UNPROTECTING to be 'protected',
// since in either state they can't be deleted
*is_protected = !is_unprotected;
return r;
}
int create_v1(IoCtx& io_ctx, const char *imgname, uint64_t bid,
uint64_t size, int order)
{
CephContext *cct = (CephContext *)io_ctx.cct();
ldout(cct, 2) << "adding rbd image to directory..." << dendl;
int r = tmap_set(io_ctx, imgname);
if (r < 0) {
lderr(cct) << "error adding image to directory: " << cpp_strerror(r)
<< dendl;
return r;
}
ldout(cct, 2) << "creating rbd image..." << dendl;
struct rbd_obj_header_ondisk header;
init_rbd_header(header, size, order, bid);
bufferlist bl;
bl.append((const char *)&header, sizeof(header));
string header_oid = old_header_name(imgname);
r = io_ctx.write(header_oid, bl, bl.length(), 0);
if (r < 0) {
lderr(cct) << "Error writing image header: " << cpp_strerror(r)
<< dendl;
int remove_r = tmap_rm(io_ctx, imgname);
if (remove_r < 0) {
lderr(cct) << "Could not remove image from directory after "
<< "header creation failed: "
<< cpp_strerror(r) << dendl;
}
return r;
}
ldout(cct, 2) << "done." << dendl;
return 0;
}
int create_v2(IoCtx& io_ctx, const char *imgname, uint64_t bid, uint64_t size,
int order, uint64_t features, uint64_t stripe_unit,
uint64_t stripe_count)
{
ostringstream bid_ss;
uint32_t extra;
string id, id_obj, header_oid;
int remove_r;
ostringstream oss;
CephContext *cct = (CephContext *)io_ctx.cct();
id_obj = id_obj_name(imgname);
int r = io_ctx.create(id_obj, true);
if (r < 0) {
lderr(cct) << "error creating rbd id object: " << cpp_strerror(r)
<< dendl;
return r;
}
extra = rand() % 0xFFFFFFFF;
bid_ss << std::hex << bid << std::hex << extra;
id = bid_ss.str();
r = cls_client::set_id(&io_ctx, id_obj, id);
if (r < 0) {
lderr(cct) << "error setting image id: " << cpp_strerror(r) << dendl;
goto err_remove_id;
}
ldout(cct, 2) << "adding rbd image to directory..." << dendl;
r = cls_client::dir_add_image(&io_ctx, RBD_DIRECTORY, imgname, id);
if (r < 0) {
lderr(cct) << "error adding image to directory: " << cpp_strerror(r)
<< dendl;
goto err_remove_id;
}
oss << RBD_DATA_PREFIX << id;
header_oid = header_name(id);
r = cls_client::create_image(&io_ctx, header_oid, size, order,
features, oss.str());
if (r < 0) {
lderr(cct) << "error writing header: " << cpp_strerror(r) << dendl;
goto err_remove_from_dir;
}
if ((stripe_unit || stripe_count) &&
(stripe_count != 1 || stripe_unit != (1ull << order))) {
r = cls_client::set_stripe_unit_count(&io_ctx, header_oid,
stripe_unit, stripe_count);
if (r < 0) {
lderr(cct) << "error setting striping parameters: "
<< cpp_strerror(r) << dendl;
goto err_remove_header;
}
}
ldout(cct, 2) << "done." << dendl;
return 0;
err_remove_header:
remove_r = io_ctx.remove(header_oid);
if (remove_r < 0) {
lderr(cct) << "error cleaning up image header after creation failed: "
<< dendl;
}
err_remove_from_dir:
remove_r = cls_client::dir_remove_image(&io_ctx, RBD_DIRECTORY,
imgname, id);
if (remove_r < 0) {
lderr(cct) << "error cleaning up image from rbd_directory object "
<< "after creation failed: " << cpp_strerror(remove_r)
<< dendl;
}
err_remove_id:
remove_r = io_ctx.remove(id_obj);
if (remove_r < 0) {
lderr(cct) << "error cleaning up id object after creation failed: "
<< cpp_strerror(remove_r) << dendl;
}
return r;
}
int create(librados::IoCtx& io_ctx, const char *imgname, uint64_t size,
int *order)
{
CephContext *cct = (CephContext *)io_ctx.cct();
bool old_format = cct->_conf->rbd_default_format == 1;
uint64_t features = old_format ? 0 : cct->_conf->rbd_default_features;
return create(io_ctx, imgname, size, old_format, features, order, 0, 0);
}
int create(IoCtx& io_ctx, const char *imgname, uint64_t size,
bool old_format, uint64_t features, int *order,
uint64_t stripe_unit, uint64_t stripe_count)
{
if (!order)
return -EINVAL;
CephContext *cct = (CephContext *)io_ctx.cct();
ldout(cct, 20) << "create " << &io_ctx << " name = " << imgname
<< " size = " << size << " old_format = " << old_format
<< " features = " << features << " order = " << *order
<< " stripe_unit = " << stripe_unit
<< " stripe_count = " << stripe_count
<< dendl;
if (features & ~RBD_FEATURES_ALL) {
lderr(cct) << "librbd does not support requested features." << dendl;
return -ENOSYS;
}
// make sure it doesn't already exist, in either format
int r = detect_format(io_ctx, imgname, NULL, NULL);
if (r != -ENOENT) {
if (r) {
lderr(cct) << "Could not tell if " << imgname << " already exists" << dendl;
return r;
}
lderr(cct) << "rbd image " << imgname << " already exists" << dendl;
return -EEXIST;
}
if (!*order)
*order = cct->_conf->rbd_default_order;
if (!*order)
*order = RBD_DEFAULT_OBJ_ORDER;
if (*order && (*order > 64 || *order < 12)) {
lderr(cct) << "order must be in the range [12, 64]" << dendl;
return -EDOM;
}
Rados rados(io_ctx);
uint64_t bid = rados.get_instance_id();
// if striping is enabled, use possibly custom defaults
if (!old_format && (features & RBD_FEATURE_STRIPINGV2) &&
!stripe_unit && !stripe_count) {
stripe_unit = cct->_conf->rbd_default_stripe_unit;
stripe_count = cct->_conf->rbd_default_stripe_count;
}
// normalize for default striping
if (stripe_unit == (1ull << *order) && stripe_count == 1) {
stripe_unit = 0;
stripe_count = 0;
}
if ((stripe_unit || stripe_count) &&
(features & RBD_FEATURE_STRIPINGV2) == 0) {
lderr(cct) << "STRIPINGV2 and format 2 or later required for non-default striping" << dendl;
return -EINVAL;
}
if ((stripe_unit && !stripe_count) ||
(!stripe_unit && stripe_count))
return -EINVAL;
if (old_format) {
if (stripe_unit && stripe_unit != (1ull << *order))
return -EINVAL;
if (stripe_count && stripe_count != 1)
return -EINVAL;
return create_v1(io_ctx, imgname, bid, size, *order);
} else {
return create_v2(io_ctx, imgname, bid, size, *order, features,
stripe_unit, stripe_count);
}
}
/*
* Parent may be in different pool, hence different IoCtx
*/
int clone(IoCtx& p_ioctx, const char *p_name, const char *p_snap_name,
IoCtx& c_ioctx, const char *c_name,
uint64_t features, int *c_order,
uint64_t stripe_unit, int stripe_count)
{
CephContext *cct = (CephContext *)p_ioctx.cct();
ldout(cct, 20) << "clone " << &p_ioctx << " name " << p_name << " snap "
<< p_snap_name << "to child " << &c_ioctx << " name "
<< c_name << " features = " << features << " order = "
<< *c_order
<< " stripe_unit = " << stripe_unit
<< " stripe_count = " << stripe_count
<< dendl;
if (features & ~RBD_FEATURES_ALL) {
lderr(cct) << "librbd does not support requested features" << dendl;
return -ENOSYS;
}
// make sure child doesn't already exist, in either format
int r = detect_format(c_ioctx, c_name, NULL, NULL);
if (r != -ENOENT) {
lderr(cct) << "rbd image " << c_name << " already exists" << dendl;
return -EEXIST;
}
if (p_snap_name == NULL) {
lderr(cct) << "image to be cloned must be a snapshot" << dendl;
return -EINVAL;
}
bool snap_protected;
int order;
uint64_t size;
uint64_t p_features;
int remove_r;
librbd::NoOpProgressContext no_op;
ImageCtx *c_imctx = NULL;
// make sure parent snapshot exists
ImageCtx *p_imctx = new ImageCtx(p_name, "", p_snap_name, p_ioctx, true);
r = open_image(p_imctx);
if (r < 0) {
lderr(cct) << "error opening parent image: "
<< cpp_strerror(-r) << dendl;
return r;
}
parent_spec pspec(p_ioctx.get_id(), p_imctx->id,
p_imctx->snap_id);
if (p_imctx->old_format) {
lderr(cct) << "parent image must be in new format" << dendl;
r = -EINVAL;
goto err_close_parent;
}
p_imctx->md_lock.get_read();
p_imctx->snap_lock.get_read();
p_imctx->get_features(p_imctx->snap_id, &p_features);
size = p_imctx->get_image_size(p_imctx->snap_id);
p_imctx->is_snap_protected(p_imctx->snap_name, &snap_protected);
p_imctx->snap_lock.put_read();
p_imctx->md_lock.put_read();
if ((p_features & RBD_FEATURE_LAYERING) != RBD_FEATURE_LAYERING) {
lderr(cct) << "parent image must support layering" << dendl;