forked from fghaas/ceph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrbd.cc
2193 lines (1861 loc) · 57.4 KB
/
librbd.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
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2011 New Dream Network
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "common/Cond.h"
#include "common/dout.h"
#include "common/errno.h"
#include "common/snap_types.h"
#include "include/rbd/librbd.hpp"
#include <errno.h>
#include <inttypes.h>
#define DOUT_SUBSYS rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd: "
namespace librbd {
using ceph::bufferlist;
using librados::snap_t;
using librados::IoCtx;
using librados::Rados;
// raw callbacks
void rados_cb(rados_completion_t cb, void *arg);
void rados_buffered_cb(rados_completion_t cb, void *arg);
void rados_aio_sparse_read_cb(rados_completion_t cb, void *arg);
class WatchCtx;
struct SnapInfo {
snap_t id;
uint64_t size;
SnapInfo(snap_t _id, uint64_t _size) : id(_id), size(_size) {};
};
struct AioCompletion;
struct AioBlockCompletion {
CephContext *cct;
struct AioCompletion *completion;
uint64_t ofs;
size_t len;
char *buf;
map<uint64_t,uint64_t> m;
bufferlist data_bl;
AioBlockCompletion(CephContext *cct_, AioCompletion *aio_completion,
uint64_t _ofs, size_t _len, char *_buf)
: cct(cct_), completion(aio_completion),
ofs(_ofs), len(_len), buf(_buf) {}
void complete(ssize_t r);
};
struct ImageCtx;
struct AioBufferedCompletion {
ImageCtx *ictx;
AioBlockCompletion *block_completion;
uint64_t len;
list<AioBufferedCompletion*>::iterator pos;
AioBufferedCompletion(ImageCtx *i, AioBlockCompletion *bc, uint64_t l)
: ictx(i), block_completion(bc), len(l) {}
};
struct ImageCtx {
CephContext *cct;
struct rbd_obj_header_ondisk header;
::SnapContext snapc;
vector<snap_t> snaps;
std::map<std::string, struct SnapInfo> snaps_by_name;
uint64_t snapid;
std::string name;
std::string snapname;
IoCtx data_ctx, md_ctx;
WatchCtx *wctx;
bool needs_refresh;
Mutex refresh_lock;
Mutex lock; // protects access to snapshot and header information
list<AioBufferedCompletion*> tx_queue;
list<AioBufferedCompletion*>::iterator tx_next; // next uncompleted item
uint64_t tx_unsafe_bytes, tx_pending_bytes, tx_window;
int tx_rval;
ImageCtx(std::string imgname, IoCtx& p)
: cct((CephContext*)p.cct()), snapid(CEPH_NOSNAP),
name(imgname),
needs_refresh(true),
refresh_lock("librbd::ImageCtx::refresh_lock"),
lock("librbd::ImageCtx::lock"),
tx_next(tx_queue.end()),
tx_unsafe_bytes(0), tx_pending_bytes(0), tx_window(0), tx_rval(0)
{
md_ctx.dup(p);
data_ctx.dup(p);
}
~ImageCtx() {
assert(tx_queue.empty());
}
int snap_set(std::string snap_name)
{
std::map<std::string, struct SnapInfo>::iterator it = snaps_by_name.find(snap_name);
if (it != snaps_by_name.end()) {
snapname = snap_name;
snapid = it->second.id;
return 0;
}
snap_unset();
return -ENOENT;
}
void snap_unset()
{
snapid = CEPH_NOSNAP;
snapname = "";
}
snap_t get_snapid(std::string snap_name) const
{
std::map<std::string, struct SnapInfo>::const_iterator it = snaps_by_name.find(snap_name);
if (it != snaps_by_name.end())
return it->second.id;
return CEPH_NOSNAP;
}
int get_snap_size(std::string snap_name, uint64_t *size) const
{
std::map<std::string, struct SnapInfo>::const_iterator it = snaps_by_name.find(snap_name);
if (it != snaps_by_name.end()) {
*size = it->second.size;
return 0;
}
return -ENOENT;
}
void add_snap(std::string snap_name, snap_t id, uint64_t size)
{
snapc.snaps.push_back(id);
snaps.push_back(id);
struct SnapInfo info(id, size);
snaps_by_name.insert(std::pair<std::string, struct SnapInfo>(snap_name, info));
}
const string md_oid() const
{
return name + RBD_SUFFIX;
}
uint64_t get_image_size() const
{
if (snapname.length() == 0) {
return header.image_size;
} else {
map<std::string,SnapInfo>::const_iterator p = snaps_by_name.find(snapname);
assert(p != snaps_by_name.end());
return p->second.size;
}
}
librados::AioCompletion *get_buffered_tx_completion(uint64_t len, AioBlockCompletion *abc) {
assert(lock.is_locked());
if (tx_window > 0) {
tx_pending_bytes += len;
AioBufferedCompletion *bc = new AioBufferedCompletion(this, abc, len);
tx_queue.push_back(bc);
bc->pos = tx_queue.end();
bc->pos--;
if (tx_next == tx_queue.end())
tx_next = bc->pos;
ldout(cct, 20) << "get_buffered_tx " << bc << dendl;
return Rados::aio_create_completion(bc, NULL, rados_buffered_cb);
} else {
return Rados::aio_create_completion(abc, NULL, rados_cb);
}
}
void finish_buffered_tx(AioBufferedCompletion *bc, int rval) {
ldout(cct, 20) << "finish_buffered_tx " << bc << dendl;
assert(lock.is_locked());
if (bc->pos == tx_next)
tx_next++;
if (bc->block_completion) {
bc->block_completion->complete(0);
delete bc->block_completion;
tx_pending_bytes -= bc->len;
tx_queue.erase(bc->pos);
} else {
tx_unsafe_bytes -= bc->len;
tx_queue.erase(bc->pos);
do_buffered_tx_completions();
}
if (rval < 0)
tx_rval = rval; // user will see this on next flush().
}
void do_buffered_tx_completions() {
assert(lock.is_locked());
ldout(cct, 20) << "do_buffered_tx_completions unsafe " << tx_unsafe_bytes
<< " tx_pending " << tx_pending_bytes
<< " window " << tx_window
<< dendl;
while (tx_unsafe_bytes < tx_window && tx_next != tx_queue.end()) {
AioBufferedCompletion *bc = *tx_next;
tx_unsafe_bytes += bc->len;
tx_pending_bytes -= bc->len;
ldout(cct, 20) << "do_buffered_tx_completion " << bc << dendl;
bc->block_completion->complete(0);
delete bc->block_completion;
bc->block_completion = NULL;
tx_next++;
}
}
};
class WatchCtx : public librados::WatchCtx {
ImageCtx *ictx;
bool valid;
Mutex lock;
public:
uint64_t cookie;
WatchCtx(ImageCtx *ctx) : ictx(ctx),
valid(true),
lock("librbd::WatchCtx") {}
virtual ~WatchCtx() {}
void invalidate();
virtual void notify(uint8_t opcode, uint64_t ver, bufferlist& bl);
};
struct AioCompletion {
Mutex lock;
Cond cond;
bool done;
ssize_t rval;
callback_t complete_cb;
void *complete_arg;
rbd_completion_t rbd_comp;
int pending_count;
int ref;
bool released;
AioCompletion() : lock("AioCompletion::lock", true),
done(false), rval(0), complete_cb(NULL), complete_arg(NULL),
rbd_comp(NULL), pending_count(1), ref(1), released(false) {
}
~AioCompletion() {
}
int wait_for_complete() {
lock.Lock();
while (!done)
cond.Wait(lock);
lock.Unlock();
return 0;
}
void add_block_completion(AioBlockCompletion *aio_completion) {
lock.Lock();
pending_count++;
lock.Unlock();
get();
}
void finish_adding_completions() {
lock.Lock();
assert(pending_count);
int count = --pending_count;
if (!count) {
complete();
}
lock.Unlock();
}
void complete() {
assert(lock.is_locked());
if (complete_cb) {
complete_cb(rbd_comp, complete_arg);
}
done = true;
cond.Signal();
}
void set_complete_cb(void *cb_arg, callback_t cb) {
complete_cb = cb;
complete_arg = cb_arg;
}
void complete_block(AioBlockCompletion *block_completion, ssize_t r);
ssize_t get_return_value() {
lock.Lock();
ssize_t r = rval;
lock.Unlock();
return r;
}
void get() {
lock.Lock();
assert(ref > 0);
ref++;
lock.Unlock();
}
void release() {
lock.Lock();
assert(!released);
released = true;
put_unlock();
}
void put() {
lock.Lock();
put_unlock();
}
void put_unlock() {
assert(ref > 0);
int n = --ref;
lock.Unlock();
if (!n)
delete this;
}
};
int snap_set(ImageCtx *ictx, const char *snap_name);
int list(IoCtx& io_ctx, std::vector<string>& names);
int create(IoCtx& io_ctx, const char *imgname, uint64_t size, int *order);
int rename(IoCtx& io_ctx, const char *srcname, const char *dstname);
int info(ImageCtx *ictx, image_info_t& info, size_t image_size);
int remove(IoCtx& io_ctx, const char *imgname, ProgressContext& prog_ctx);
int resize(ImageCtx *ictx, uint64_t size, ProgressContext& prog_ctx);
int resize_helper(ImageCtx *ictx, uint64_t size, ProgressContext& prog_ctx);
int snap_create(ImageCtx *ictx, const char *snap_name);
int snap_list(ImageCtx *ictx, std::vector<snap_info_t>& snaps);
int snap_rollback(ImageCtx *ictx, const char *snap_name, ProgressContext& prog_ctx);
int snap_remove(ImageCtx *ictx, const char *snap_name);
int add_snap(ImageCtx *ictx, const char *snap_name);
int rm_snap(ImageCtx *ictx, const char *snap_name);
int ictx_check(ImageCtx *ictx);
int ictx_refresh(ImageCtx *ictx, const char *snap_name);
int copy(ImageCtx& srci, IoCtx& dest_md_ctx, const char *destname);
int open_image(IoCtx& io_ctx, ImageCtx *ictx, const char *name, const char *snap_name);
void close_image(ImageCtx *ictx);
void trim_image(IoCtx& io_ctx, const rbd_obj_header_ondisk &header, uint64_t newsize,
ProgressContext& prog_ctx);
int read_rbd_info(IoCtx& io_ctx, const string& info_oid, struct rbd_info *info);
int touch_rbd_info(IoCtx& io_ctx, const string& info_oid);
int rbd_assign_bid(IoCtx& io_ctx, const string& info_oid, uint64_t *id);
int read_header_bl(IoCtx& io_ctx, const string& md_oid, bufferlist& header, uint64_t *ver);
int notify_change(IoCtx& io_ctx, const string& oid, uint64_t *pver, ImageCtx *ictx);
int read_header(IoCtx& io_ctx, const string& md_oid, struct rbd_obj_header_ondisk *header, uint64_t *ver);
int write_header(IoCtx& io_ctx, const string& md_oid, bufferlist& header);
int tmap_set(IoCtx& io_ctx, const string& imgname);
int tmap_rm(IoCtx& io_ctx, const string& imgname);
int rollback_image(ImageCtx *ictx, uint64_t snapid, ProgressContext& prog_ctx);
void image_info(const ImageCtx& ictx, image_info_t& info, size_t info_size);
string get_block_oid(const rbd_obj_header_ondisk &header, uint64_t num);
uint64_t get_max_block(uint64_t size, int obj_order);
uint64_t get_max_block(const rbd_obj_header_ondisk &header);
uint64_t get_block_size(const rbd_obj_header_ondisk &header);
uint64_t get_block_num(const rbd_obj_header_ondisk &header, uint64_t ofs);
uint64_t get_block_ofs(const rbd_obj_header_ondisk &header, uint64_t ofs);
int check_io(ImageCtx *ictx, uint64_t off, uint64_t len);
int init_rbd_info(struct rbd_info *info);
void init_rbd_header(struct rbd_obj_header_ondisk& ondisk,
uint64_t size, int *order, uint64_t bid);
int64_t read_iterate(ImageCtx *ictx, uint64_t off, size_t len,
int (*cb)(uint64_t, size_t, const char *, void *),
void *arg);
ssize_t read(ImageCtx *ictx, uint64_t off, size_t len, char *buf);
ssize_t write(ImageCtx *ictx, uint64_t off, size_t len, const char *buf);
int aio_write(ImageCtx *ictx, uint64_t off, size_t len, const char *buf,
AioCompletion *c);
int aio_read(ImageCtx *ictx, uint64_t off, size_t len,
char *buf, AioCompletion *c);
int flush(ImageCtx *ictx);
ssize_t handle_sparse_read(CephContext *cct,
bufferlist data_bl,
uint64_t block_ofs,
const map<uint64_t, uint64_t> &data_map,
uint64_t buf_ofs,
size_t buf_len,
int (*cb)(uint64_t, size_t, const char *, void *),
void *arg);
AioCompletion *aio_create_completion() {
AioCompletion *c= new AioCompletion();
return c;
}
AioCompletion *aio_create_completion(void *cb_arg, callback_t cb_complete) {
AioCompletion *c = new AioCompletion();
c->set_complete_cb(cb_arg, cb_complete);
return c;
}
void WatchCtx::invalidate()
{
Mutex::Locker l(lock);
valid = false;
}
void WatchCtx::notify(uint8_t opcode, uint64_t ver, bufferlist& bl)
{
Mutex::Locker l(lock);
ldout(ictx->cct, 1) << " got notification opcode=" << (int)opcode << " ver=" << ver << " cookie=" << cookie << dendl;
if (valid) {
Mutex::Locker lictx(ictx->refresh_lock);
ictx->needs_refresh = true;
}
}
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;
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", hi, lo);
if (!*order)
*order = RBD_DEFAULT_OBJ_ORDER;
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(const ImageCtx& ictx, image_info_t& info, size_t infosize)
{
int obj_order = ictx.header.options.order;
info.size = ictx.get_image_size();
info.obj_size = 1 << obj_order;
info.num_objs = ictx.get_image_size() >> obj_order;
info.order = obj_order;
memcpy(&info.block_name_prefix, &ictx.header.block_name, RBD_MAX_BLOCK_NAME_SIZE);
info.parent_pool = -1;
bzero(&info.parent_name, RBD_MAX_IMAGE_NAME_SIZE);
}
string get_block_oid(const rbd_obj_header_ondisk &header, uint64_t num)
{
char o[RBD_MAX_BLOCK_NAME_SIZE];
snprintf(o, RBD_MAX_BLOCK_NAME_SIZE,
"%s.%012" PRIx64, header.block_name, num);
return o;
}
uint64_t get_max_block(uint64_t size, int obj_order)
{
uint64_t block_size = 1 << obj_order;
uint64_t numseg = (size + block_size - 1) >> obj_order;
return numseg;
}
uint64_t get_max_block(const rbd_obj_header_ondisk &header)
{
return get_max_block(header.image_size, header.options.order);
}
uint64_t get_block_ofs(const rbd_obj_header_ondisk &header, uint64_t ofs)
{
int obj_order = header.options.order;
uint64_t block_size = 1 << obj_order;
return ofs & (block_size - 1);
}
uint64_t get_block_size(const rbd_obj_header_ondisk &header)
{
return 1 << header.options.order;
}
uint64_t get_block_num(const rbd_obj_header_ondisk &header, uint64_t ofs)
{
int obj_order = header.options.order;
uint64_t num = ofs >> obj_order;
return num;
}
int init_rbd_info(struct rbd_info *info)
{
memset(info, 0, sizeof(*info));
return 0;
}
void trim_image(IoCtx& io_ctx, const rbd_obj_header_ondisk &header, uint64_t newsize,
ProgressContext& prog_ctx)
{
CephContext *cct = (CephContext *)io_ctx.cct();
uint64_t bsize = get_block_size(header);
uint64_t numseg = get_max_block(header);
uint64_t start = get_block_num(header, newsize);
ldout(cct, 2) << "trimming image data from " << numseg << " to " << start << " objects..." << dendl;
for (uint64_t i=start; i<numseg; i++) {
string oid = get_block_oid(header, i);
io_ctx.remove(oid);
prog_ctx.update_progress(i * bsize, (numseg - start) * bsize);
}
}
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 touch_rbd_info(IoCtx& io_ctx, const string& info_oid)
{
bufferlist bl;
int r = io_ctx.write(info_oid, bl, 0, 0);
if (r < 0)
return r;
return 0;
}
int rbd_assign_bid(IoCtx& io_ctx, const string& info_oid, uint64_t *id)
{
bufferlist bl, out;
*id = 0;
int r = touch_rbd_info(io_ctx, info_oid);
if (r < 0)
return r;
r = io_ctx.exec(info_oid, "rbd", "assign_bid", bl, out);
if (r < 0)
return r;
bufferlist::iterator iter = out.begin();
::decode(*id, iter);
return 0;
}
int read_header_bl(IoCtx& io_ctx, const string& md_oid, bufferlist& header, uint64_t *ver)
{
int r;
uint64_t off = 0;
#define READ_SIZE 4096
do {
bufferlist bl;
r = io_ctx.read(md_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) {
assert(ictx->lock.is_locked());
ictx->refresh_lock.Lock();
ictx->needs_refresh = true;
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& md_oid, struct rbd_obj_header_ondisk *header, uint64_t *ver)
{
bufferlist header_bl;
int r = read_header_bl(io_ctx, md_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& md_oid, bufferlist& header)
{
bufferlist bl;
int r = io_ctx.write(md_oid, header, header.length(), 0);
notify_change(io_ctx, md_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 snapid, ProgressContext& prog_ctx)
{
assert(ictx->lock.is_locked());
uint64_t numseg = get_max_block(ictx->header);
uint64_t bsize = get_block_size(ictx->header);
for (uint64_t i = 0; i < numseg; i++) {
int r;
string oid = get_block_oid(ictx->header, i);
r = ictx->data_ctx.selfmanaged_snap_rollback(oid, snapid);
ldout(ictx->cct, 10) << "selfmanaged_snap_rollback on " << oid << " to " << snapid << " returned " << r << dendl;
prog_ctx.update_progress(i * bsize, numseg * bsize);
if (r < 0 && r != -ENOENT)
return r;
}
return 0;
}
int list(IoCtx& io_ctx, std::vector<std::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;
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);
return 0;
}
int snap_create(ImageCtx *ictx, const char *snap_name)
{
ldout(ictx->cct, 20) << "snap_create " << ictx << " " << snap_name << dendl;
int r = ictx_check(ictx);
if (r < 0)
return r;
Mutex::Locker l(ictx->lock);
r = add_snap(ictx, snap_name);
if (r < 0)
return r;
notify_change(ictx->md_ctx, ictx->md_oid(), NULL, ictx);
return 0;
}
int snap_remove(ImageCtx *ictx, const char *snap_name)
{
ldout(ictx->cct, 20) << "snap_remove " << ictx << " " << snap_name << dendl;
int r = ictx_check(ictx);
if (r < 0)
return r;
Mutex::Locker l(ictx->lock);
snap_t snapid = ictx->get_snapid(snap_name);
if (snapid == CEPH_NOSNAP)
return -ENOENT;
r = rm_snap(ictx, snap_name);
if (r < 0)
return r;
r = ictx->data_ctx.selfmanaged_snap_remove(snapid);
if (r < 0)
return r;
notify_change(ictx->md_ctx, ictx->md_oid(), NULL, ictx);
return 0;
}
int create(IoCtx& io_ctx, const char *imgname, uint64_t size, int *order)
{
CephContext *cct = (CephContext *)io_ctx.cct();
ldout(cct, 20) << "create " << &io_ctx << " name = " << imgname << " size = " << size << dendl;
string md_oid = imgname;
md_oid += RBD_SUFFIX;
// make sure it doesn't already exist
int r = io_ctx.stat(md_oid, NULL, NULL);
if (r == 0) {
lderr(cct) << "rbd image header " << md_oid << " already exists" << dendl;
return -EEXIST;
}
uint64_t bid;
string dir_info = RBD_INFO;
r = rbd_assign_bid(io_ctx, dir_info, &bid);
if (r < 0) {
lderr(cct) << "failed to assign a block name for image" << dendl;
return r;
}
struct rbd_obj_header_ondisk header;
init_rbd_header(header, size, order, bid);
bufferlist bl;
bl.append((const char *)&header, sizeof(header));
ldout(cct, 2) << "adding rbd image to directory..." << dendl;
r = tmap_set(io_ctx, imgname);
if (r < 0) {
lderr(cct) << "error adding img to directory: " << cpp_strerror(-r)<< dendl;
return r;
}
ldout(cct, 2) << "creating rbd image..." << dendl;
r = io_ctx.write(md_oid, bl, bl.length(), 0);
if (r < 0) {
lderr(cct) << "error writing header: " << cpp_strerror(-r) << dendl;
return r;
}
ldout(cct, 2) << "done." << dendl;
return 0;
}
int rename(IoCtx& io_ctx, const char *srcname, const char *dstname)
{
CephContext *cct = (CephContext *)io_ctx.cct();
ldout(cct, 20) << "rename " << &io_ctx << " " << srcname << " -> " << dstname << dendl;
string md_oid = srcname;
md_oid += RBD_SUFFIX;
string dst_md_oid = dstname;
dst_md_oid += RBD_SUFFIX;
string dstname_str = dstname;
string imgname_str = srcname;
uint64_t ver;
bufferlist header;
int r = read_header_bl(io_ctx, md_oid, header, &ver);
if (r < 0) {
lderr(cct) << "error reading header: " << md_oid << ": " << cpp_strerror(-r) << dendl;
return r;
}
r = io_ctx.stat(dst_md_oid, NULL, NULL);
if (r == 0) {
lderr(cct) << "rbd image header " << dst_md_oid << " already exists" << dendl;
return -EEXIST;
}
r = write_header(io_ctx, dst_md_oid, header);
if (r < 0) {
lderr(cct) << "error writing header: " << dst_md_oid << ": " << cpp_strerror(-r) << dendl;
return r;
}
r = tmap_set(io_ctx, dstname_str);
if (r < 0) {
io_ctx.remove(dst_md_oid);
lderr(cct) << "can't add " << dst_md_oid << " to directory" << dendl;
return r;
}
r = tmap_rm(io_ctx, imgname_str);
if (r < 0)
lderr(cct) << "warning: couldn't remove old entry from directory (" << imgname_str << ")" << dendl;
r = io_ctx.remove(md_oid);
if (r < 0 && r != -ENOENT)
lderr(cct) << "warning: couldn't remove old metadata" << dendl;
notify_change(io_ctx, md_oid, NULL, NULL);
return 0;
}
int info(ImageCtx *ictx, image_info_t& info, size_t infosize)
{
ldout(ictx->cct, 20) << "info " << ictx << dendl;
int r = ictx_check(ictx);
if (r < 0)
return r;
Mutex::Locker l(ictx->lock);
image_info(*ictx, info, infosize);
return 0;
}
bool has_snaps(IoCtx& io_ctx, const std::string& md_oid)
{
CephContext *cct((CephContext *)io_ctx.cct());
ldout(cct, 20) << "has_snaps " << &io_ctx << " " << md_oid << dendl;
bufferlist bl, bl2;
int r = io_ctx.exec(md_oid, "rbd", "snap_list", bl, bl2);
if (r < 0) {
lderr(cct) << "Error listing snapshots: " << cpp_strerror(-r) << dendl;
return true;
}
uint32_t num_snaps;
uint64_t snap_seq;
bufferlist::iterator iter = bl2.begin();
::decode(snap_seq, iter);
::decode(num_snaps, iter);
return num_snaps > 0;
}
int remove(IoCtx& io_ctx, const char *imgname, ProgressContext& prog_ctx)
{
CephContext *cct((CephContext *)io_ctx.cct());
ldout(cct, 20) << "remove " << &io_ctx << " " << imgname << dendl;
string md_oid = imgname;
md_oid += RBD_SUFFIX;
struct rbd_obj_header_ondisk header;
int r = read_header(io_ctx, md_oid, &header, NULL);
if (r < 0) {
ldout(cct, 2) << "error reading header: " << cpp_strerror(-r) << dendl;
}
if (r >= 0) {
if (has_snaps(io_ctx, md_oid)) {
lderr(cct) << "image has snapshots - not removing" << dendl;
return -EBUSY;
}
trim_image(io_ctx, header, 0, prog_ctx);
ldout(cct, 2) << "removing header..." << dendl;
io_ctx.remove(md_oid);
}
ldout(cct, 2) << "removing rbd image from directory..." << dendl;
r = tmap_rm(io_ctx, imgname);
if (r < 0) {
lderr(cct) << "error removing img from directory: " << cpp_strerror(-r) << dendl;
return r;
}
ldout(cct, 2) << "done." << dendl;
return 0;
}
int resize_helper(ImageCtx *ictx, uint64_t size, ProgressContext& prog_ctx)
{
CephContext *cct = ictx->cct;
if (size == ictx->header.image_size) {
ldout(cct, 2) << "no change in size (" << size << " -> " << ictx->header.image_size << ")" << dendl;
return 0;
}
if (size > ictx->header.image_size) {
ldout(cct, 2) << "expanding image " << size << " -> " << ictx->header.image_size << " objects" << dendl;
ictx->header.image_size = size;
} else {
ldout(cct, 2) << "shrinking image " << size << " -> " << ictx->header.image_size << " objects" << dendl;
trim_image(ictx->data_ctx, ictx->header, size, prog_ctx);
ictx->header.image_size = size;
}
// rewrite header
bufferlist bl;
bl.append((const char *)&(ictx->header), sizeof(ictx->header));
int r = ictx->md_ctx.write(ictx->md_oid(), bl, bl.length(), 0);
if (r == -ERANGE)
lderr(cct) << "operation might have conflicted with another client!" << dendl;
if (r < 0) {
lderr(cct) << "error writing header: " << cpp_strerror(-r) << dendl;
return r;
} else {
notify_change(ictx->md_ctx, ictx->md_oid(), NULL, ictx);
}
return 0;
}
int resize(ImageCtx *ictx, uint64_t size, ProgressContext& prog_ctx)
{
CephContext *cct = ictx->cct;
ldout(cct, 20) << "resize " << ictx << " " << ictx->header.image_size << " -> " << size << dendl;
int r = ictx_check(ictx);
if (r < 0)
return r;
Mutex::Locker l(ictx->lock);
resize_helper(ictx, size, prog_ctx);
ldout(cct, 2) << "done." << dendl;
return 0;
}
int snap_list(ImageCtx *ictx, std::vector<snap_info_t>& snaps)
{
ldout(ictx->cct, 20) << "snap_list " << ictx << dendl;
int r = ictx_check(ictx);
if (r < 0)
return r;
bufferlist bl, bl2;
Mutex::Locker l(ictx->lock);
for (std::map<std::string, struct SnapInfo>::iterator it = ictx->snaps_by_name.begin();
it != ictx->snaps_by_name.end(); ++it) {
snap_info_t info;
info.name = it->first;
info.id = it->second.id;
info.size = it->second.size;
snaps.push_back(info);
}
return 0;
}
int add_snap(ImageCtx *ictx, const char *snap_name)
{
assert(ictx->lock.is_locked());
bufferlist bl, bl2;
uint64_t snap_id;
int r = ictx->md_ctx.selfmanaged_snap_create(&snap_id);
if (r < 0) {
lderr(ictx->cct) << "failed to create snap id: " << cpp_strerror(-r) << dendl;
return r;
}
::encode(snap_name, bl);
::encode(snap_id, bl);
r = ictx->md_ctx.exec(ictx->md_oid(), "rbd", "snap_add", bl, bl2);
if (r < 0) {
lderr(ictx->cct) << "rbd.snap_add execution failed failed: " << cpp_strerror(-r) << dendl;