-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathrbd.go
1282 lines (1087 loc) · 32.8 KB
/
rbd.go
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
package rbd
// #cgo LDFLAGS: -lrbd
// /* force XSI-complaint strerror_r() */
// #define _POSIX_C_SOURCE 200112L
// #undef _GNU_SOURCE
// #include <errno.h>
// #include <stdlib.h>
// #include <rados/librados.h>
// #include <rbd/librbd.h>
import "C"
import (
"bytes"
"errors"
"io"
"time"
"unsafe"
"github.com/ceph/go-ceph/internal/retry"
ts "github.com/ceph/go-ceph/internal/timespec"
"github.com/ceph/go-ceph/rados"
)
const (
// Image.Seek() constants:
// SeekSet is used with Seek to absolutely position the file.
SeekSet = int(C.SEEK_SET)
// SeekCur is used with Seek to position the file relatively to the current
// position.
SeekCur = int(C.SEEK_CUR)
// SeekEnd is used with Seek to position the file relatively to the end.
SeekEnd = int(C.SEEK_END)
)
// bits for Image.validate() and Snapshot.validate()
const (
imageNeedsName uint32 = 1 << iota
imageNeedsIOContext
imageIsOpen
imageIsNotOpen
snapshotNeedsName
// NoSnapshot indicates that no snapshot name is in use (see OpenImage)
NoSnapshot = ""
)
// Timespec is a public type for the internal C 'struct timespec'
type Timespec ts.Timespec
// ImageInfo represents the status information for an image.
type ImageInfo struct {
Size uint64
Obj_size uint64
Num_objs uint64
Order int
Block_name_prefix string
Parent_pool int64
Parent_name string
}
// SnapInfo represents the status information for a snapshot.
type SnapInfo struct {
Id uint64
Size uint64
Name string
}
// Locker provides info about a client that is locking an image.
type Locker struct {
Client string
Cookie string
Addr string
}
// Image is a handle for an RBD image.
type Image struct {
io.Reader
io.Writer
io.Seeker
io.ReaderAt
io.WriterAt
name string
offset int64
ioctx *rados.IOContext
image C.rbd_image_t
}
// TrashInfo contains information about trashed RBDs.
type TrashInfo struct {
Id string // Id string, required to remove / restore trashed RBDs.
Name string // Original name of trashed RBD.
DeletionTime time.Time // Date / time at which the RBD was moved to the trash.
DefermentEndTime time.Time // Date / time after which the trashed RBD may be permanently deleted.
}
//
func split(buf []byte) (values []string) {
tmp := bytes.Split(buf[:len(buf)-1], []byte{0})
for _, s := range tmp {
if len(s) > 0 {
go_s := C.GoString((*C.char)(unsafe.Pointer(&s[0])))
values = append(values, go_s)
}
}
return values
}
// cephIoctx returns a ceph rados_ioctx_t given a go-ceph rados IOContext.
func cephIoctx(radosIoctx *rados.IOContext) C.rados_ioctx_t {
return C.rados_ioctx_t(radosIoctx.Pointer())
}
// test if a bit is set in the given value
func hasBit(value, bit uint32) bool {
return (value & bit) == bit
}
// validate the attributes listed in the req bitmask, and return an error in
// case the attribute is not set
func (image *Image) validate(req uint32) error {
if hasBit(req, imageNeedsName) && image.name == "" {
return ErrNoName
} else if hasBit(req, imageNeedsIOContext) && image.ioctx == nil {
return ErrNoIOContext
} else if hasBit(req, imageIsOpen) && image.image == nil {
return ErrImageNotOpen
} else if hasBit(req, imageIsNotOpen) && image.image != nil {
return ErrImageIsOpen
}
return nil
}
// Version returns the major, minor, and patch level of the librbd library.
func Version() (int, int, int) {
var c_major, c_minor, c_patch C.int
C.rbd_version(&c_major, &c_minor, &c_patch)
return int(c_major), int(c_minor), int(c_patch)
}
// GetImage gets a reference to a previously created rbd image.
func GetImage(ioctx *rados.IOContext, name string) *Image {
return &Image{
ioctx: ioctx,
name: name,
}
}
// Create a new rbd image.
//
// Implements:
// int rbd_create(rados_ioctx_t io, const char *name, uint64_t size, int *order);
//
// Also implements (for backward compatibility):
// int rbd_create2(rados_ioctx_t io, const char *name, uint64_t size,
// uint64_t features, int *order);
// int rbd_create3(rados_ioctx_t io, const char *name, uint64_t size,
// uint64_t features, int *order,
// uint64_t stripe_unit, uint64_t stripe_count);
func Create(ioctx *rados.IOContext, name string, size uint64, order int,
args ...uint64) (image *Image, err error) {
var ret C.int
switch len(args) {
case 3:
return Create3(ioctx, name, size, args[0], order, args[1],
args[2])
case 1:
return Create2(ioctx, name, size, args[0], order)
case 0:
c_order := C.int(order)
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
ret = C.rbd_create(cephIoctx(ioctx),
c_name, C.uint64_t(size), &c_order)
default:
return nil, errors.New("Wrong number of argument")
}
if ret < 0 {
return nil, RBDError(ret)
}
return &Image{
ioctx: ioctx,
name: name,
}, nil
}
// Create2 creates a new rbd image using provided features.
//
// Implements:
// int rbd_create2(rados_ioctx_t io, const char *name, uint64_t size,
// uint64_t features, int *order);
func Create2(ioctx *rados.IOContext, name string, size uint64, features uint64,
order int) (image *Image, err error) {
var ret C.int
c_order := C.int(order)
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
ret = C.rbd_create2(cephIoctx(ioctx), c_name,
C.uint64_t(size), C.uint64_t(features), &c_order)
if ret < 0 {
return nil, RBDError(ret)
}
return &Image{
ioctx: ioctx,
name: name,
}, nil
}
// Create3 creates a new rbd image using provided features and stripe
// parameters.
//
// Implements:
// int rbd_create3(rados_ioctx_t io, const char *name, uint64_t size,
// uint64_t features, int *order,
// uint64_t stripe_unit, uint64_t stripe_count);
func Create3(ioctx *rados.IOContext, name string, size uint64, features uint64,
order int, stripe_unit uint64, stripe_count uint64) (image *Image, err error) {
var ret C.int
c_order := C.int(order)
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
ret = C.rbd_create3(cephIoctx(ioctx), c_name,
C.uint64_t(size), C.uint64_t(features), &c_order,
C.uint64_t(stripe_unit), C.uint64_t(stripe_count))
if ret < 0 {
return nil, RBDError(ret)
}
return &Image{
ioctx: ioctx,
name: name,
}, nil
}
// Clone a new rbd image from a snapshot.
//
// Implements:
// int rbd_clone(rados_ioctx_t p_ioctx, const char *p_name,
// const char *p_snapname, rados_ioctx_t c_ioctx,
// const char *c_name, uint64_t features, int *c_order);
func (image *Image) Clone(snapname string, c_ioctx *rados.IOContext, c_name string, features uint64, order int) (*Image, error) {
if err := image.validate(imageNeedsIOContext); err != nil {
return nil, err
}
c_order := C.int(order)
c_p_name := C.CString(image.name)
c_p_snapname := C.CString(snapname)
c_c_name := C.CString(c_name)
defer C.free(unsafe.Pointer(c_p_name))
defer C.free(unsafe.Pointer(c_p_snapname))
defer C.free(unsafe.Pointer(c_c_name))
ret := C.rbd_clone(cephIoctx(image.ioctx),
c_p_name, c_p_snapname,
cephIoctx(c_ioctx),
c_c_name, C.uint64_t(features), &c_order)
if ret < 0 {
return nil, RBDError(ret)
}
return &Image{
ioctx: c_ioctx,
name: c_name,
}, nil
}
// Remove the specified rbd image.
//
// Implements:
// int rbd_remove(rados_ioctx_t io, const char *name);
func (image *Image) Remove() error {
if err := image.validate(imageNeedsIOContext | imageNeedsName | imageIsNotOpen); err != nil {
return err
}
return RemoveImage(image.ioctx, image.name)
}
// Trash will move an image into the RBD trash, where it will be protected (i.e., salvageable) for
// at least the specified delay.
func (image *Image) Trash(delay time.Duration) error {
if err := image.validate(imageNeedsIOContext | imageNeedsName); err != nil {
return err
}
c_name := C.CString(image.name)
defer C.free(unsafe.Pointer(c_name))
return getError(C.rbd_trash_move(cephIoctx(image.ioctx), c_name,
C.uint64_t(delay.Seconds())))
}
// Rename an rbd image.
//
// Implements:
// int rbd_rename(rados_ioctx_t src_io_ctx, const char *srcname, const char *destname);
func (image *Image) Rename(destname string) error {
if err := image.validate(imageNeedsIOContext | imageNeedsName); err != nil {
return err
}
c_srcname := C.CString(image.name)
c_destname := C.CString(destname)
defer C.free(unsafe.Pointer(c_srcname))
defer C.free(unsafe.Pointer(c_destname))
err := RBDError(C.rbd_rename(cephIoctx(image.ioctx),
c_srcname, c_destname))
if err == 0 {
image.name = destname
return nil
}
return err
}
// Open the rbd image.
//
// Deprecated: The Open function was provided in earlier versions of the API
// and now exists to support older code. The use of OpenImage and
// OpenImageReadOnly is preferred.
func (image *Image) Open(args ...interface{}) error {
if err := image.validate(imageNeedsIOContext | imageNeedsName); err != nil {
return err
}
var (
snapName string
readOnly bool
)
for _, arg := range args {
switch t := arg.(type) {
case string:
snapName = t
case bool:
readOnly = t
default:
return errors.New("Unexpected argument")
}
}
var (
tmp *Image
err error
)
if readOnly {
tmp, err = OpenImageReadOnly(image.ioctx, image.name, snapName)
} else {
tmp, err = OpenImage(image.ioctx, image.name, snapName)
}
if err != nil {
return err
}
image.image = tmp.image
return nil
}
// Close an open rbd image.
//
// Implements:
// int rbd_close(rbd_image_t image);
func (image *Image) Close() error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
if ret := C.rbd_close(image.image); ret != 0 {
return RBDError(ret)
}
image.image = nil
return nil
}
// Resize an rbd image.
//
// Implements:
// int rbd_resize(rbd_image_t image, uint64_t size);
func (image *Image) Resize(size uint64) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
return getError(C.rbd_resize(image.image, C.uint64_t(size)))
}
// Stat an rbd image.
//
// Implements:
// int rbd_stat(rbd_image_t image, rbd_image_info_t *info, size_t infosize);
func (image *Image) Stat() (info *ImageInfo, err error) {
if err := image.validate(imageIsOpen); err != nil {
return nil, err
}
var c_stat C.rbd_image_info_t
if ret := C.rbd_stat(image.image, &c_stat, C.size_t(unsafe.Sizeof(info))); ret < 0 {
return info, RBDError(ret)
}
return &ImageInfo{
Size: uint64(c_stat.size),
Obj_size: uint64(c_stat.obj_size),
Num_objs: uint64(c_stat.num_objs),
Order: int(c_stat.order),
Block_name_prefix: C.GoString((*C.char)(&c_stat.block_name_prefix[0])),
Parent_pool: int64(c_stat.parent_pool),
Parent_name: C.GoString((*C.char)(&c_stat.parent_name[0]))}, nil
}
// IsOldFormat returns true if the rbd image uses the old format.
//
// Implements:
// int rbd_get_old_format(rbd_image_t image, uint8_t *old);
func (image *Image) IsOldFormat() (old_format bool, err error) {
if err := image.validate(imageIsOpen); err != nil {
return false, err
}
var c_old_format C.uint8_t
ret := C.rbd_get_old_format(image.image,
&c_old_format)
if ret < 0 {
return false, RBDError(ret)
}
return c_old_format != 0, nil
}
// GetSize returns the size of the rbd image.
//
// Implements:
// int rbd_size(rbd_image_t image, uint64_t *size);
func (image *Image) GetSize() (size uint64, err error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
if ret := C.rbd_get_size(image.image, (*C.uint64_t)(&size)); ret < 0 {
return 0, RBDError(ret)
}
return size, nil
}
// GetStripeUnit returns the stripe-unit value for the rbd image.
//
// Implements:
// int rbd_get_stripe_unit(rbd_image_t image, uint64_t *stripe_unit);
func (image *Image) GetStripeUnit() (stripe_unit uint64, err error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
if ret := C.rbd_get_stripe_unit(image.image, (*C.uint64_t)(&stripe_unit)); ret < 0 {
return 0, RBDError(ret)
}
return stripe_unit, nil
}
// GetStripeCount returns the stripe-count value for the rbd image.
//
// Implements:
// int rbd_get_stripe_count(rbd_image_t image, uint64_t *stripe_count);
func (image *Image) GetStripeCount() (stripe_count uint64, err error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
if ret := C.rbd_get_stripe_count(image.image, (*C.uint64_t)(&stripe_count)); ret < 0 {
return 0, RBDError(ret)
}
return stripe_count, nil
}
// GetOverlap returns the overlapping bytes between the rbd image and its
// parent.
//
// Implements:
// int rbd_get_overlap(rbd_image_t image, uint64_t *overlap);
func (image *Image) GetOverlap() (overlap uint64, err error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
if ret := C.rbd_get_overlap(image.image, (*C.uint64_t)(&overlap)); ret < 0 {
return overlap, RBDError(ret)
}
return overlap, nil
}
// Copy one rbd image to another.
//
// Implements:
// int rbd_copy(rbd_image_t image, rados_ioctx_t dest_io_ctx, const char *destname);
func (image *Image) Copy(ioctx *rados.IOContext, destname string) error {
if err := image.validate(imageIsOpen); err != nil {
return err
} else if ioctx == nil {
return ErrNoIOContext
} else if len(destname) == 0 {
return ErrNoName
}
c_destname := C.CString(destname)
defer C.free(unsafe.Pointer(c_destname))
return getError(C.rbd_copy(image.image,
cephIoctx(ioctx), c_destname))
}
// Copy2 copies one rbd image to another, using an image handle.
//
// Implements:
// int rbd_copy2(rbd_image_t src, rbd_image_t dest);
func (image *Image) Copy2(dest *Image) error {
if err := image.validate(imageIsOpen); err != nil {
return err
} else if err := dest.validate(imageIsOpen); err != nil {
return err
}
return getError(C.rbd_copy2(image.image, dest.image))
}
// Flatten removes snapshot references from the image.
//
// Implements:
// int rbd_flatten(rbd_image_t image);
func (image *Image) Flatten() error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
return getError(C.rbd_flatten(image.image))
}
// ListLockers returns a list of clients that have locks on the image.
//
// Impelemnts:
// ssize_t rbd_list_lockers(rbd_image_t image, int *exclusive,
// char *tag, size_t *tag_len,
// char *clients, size_t *clients_len,
// char *cookies, size_t *cookies_len,
// char *addrs, size_t *addrs_len);
func (image *Image) ListLockers() (tag string, lockers []Locker, err error) {
if err := image.validate(imageIsOpen); err != nil {
return "", nil, err
}
var c_exclusive C.int
var c_tag_len, c_clients_len, c_cookies_len, c_addrs_len C.size_t
var c_locker_cnt C.ssize_t
C.rbd_list_lockers(image.image, &c_exclusive,
nil, (*C.size_t)(&c_tag_len),
nil, (*C.size_t)(&c_clients_len),
nil, (*C.size_t)(&c_cookies_len),
nil, (*C.size_t)(&c_addrs_len))
// no locker held on rbd image when either c_clients_len,
// c_cookies_len or c_addrs_len is *0*, so just quickly returned
if int(c_clients_len) == 0 || int(c_cookies_len) == 0 ||
int(c_addrs_len) == 0 {
lockers = make([]Locker, 0)
return "", lockers, nil
}
tag_buf := make([]byte, c_tag_len)
clients_buf := make([]byte, c_clients_len)
cookies_buf := make([]byte, c_cookies_len)
addrs_buf := make([]byte, c_addrs_len)
c_locker_cnt = C.rbd_list_lockers(image.image, &c_exclusive,
(*C.char)(unsafe.Pointer(&tag_buf[0])), (*C.size_t)(&c_tag_len),
(*C.char)(unsafe.Pointer(&clients_buf[0])), (*C.size_t)(&c_clients_len),
(*C.char)(unsafe.Pointer(&cookies_buf[0])), (*C.size_t)(&c_cookies_len),
(*C.char)(unsafe.Pointer(&addrs_buf[0])), (*C.size_t)(&c_addrs_len))
// rbd_list_lockers returns negative value for errors
// and *0* means no locker held on rbd image.
// but *0* is unexpected here because first rbd_list_lockers already
// dealt with no locker case
if int(c_locker_cnt) <= 0 {
return "", nil, RBDError(c_locker_cnt)
}
clients := split(clients_buf)
cookies := split(cookies_buf)
addrs := split(addrs_buf)
lockers = make([]Locker, c_locker_cnt)
for i := 0; i < int(c_locker_cnt); i++ {
lockers[i] = Locker{Client: clients[i],
Cookie: cookies[i],
Addr: addrs[i]}
}
return string(tag_buf), lockers, nil
}
// LockExclusive acquires an exclusive lock on the rbd image.
//
// Implements:
// int rbd_lock_exclusive(rbd_image_t image, const char *cookie);
func (image *Image) LockExclusive(cookie string) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
c_cookie := C.CString(cookie)
defer C.free(unsafe.Pointer(c_cookie))
return getError(C.rbd_lock_exclusive(image.image, c_cookie))
}
// LockShared acquires a shared lock on the rbd image.
//
// Implements:
// int rbd_lock_shared(rbd_image_t image, const char *cookie, const char *tag);
func (image *Image) LockShared(cookie string, tag string) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
c_cookie := C.CString(cookie)
c_tag := C.CString(tag)
defer C.free(unsafe.Pointer(c_cookie))
defer C.free(unsafe.Pointer(c_tag))
return getError(C.rbd_lock_shared(image.image, c_cookie, c_tag))
}
// Unlock releases a lock on the image.
//
// Implements:
// int rbd_lock_shared(rbd_image_t image, const char *cookie, const char *tag);
func (image *Image) Unlock(cookie string) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
c_cookie := C.CString(cookie)
defer C.free(unsafe.Pointer(c_cookie))
return getError(C.rbd_unlock(image.image, c_cookie))
}
// BreakLock forces the release of a lock held by another client.
//
// Implements:
// int rbd_break_lock(rbd_image_t image, const char *client, const char *cookie);
func (image *Image) BreakLock(client string, cookie string) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
c_client := C.CString(client)
c_cookie := C.CString(cookie)
defer C.free(unsafe.Pointer(c_client))
defer C.free(unsafe.Pointer(c_cookie))
return getError(C.rbd_break_lock(image.image, c_client, c_cookie))
}
// ssize_t rbd_read(rbd_image_t image, uint64_t ofs, size_t len, char *buf);
// TODO: int64_t rbd_read_iterate(rbd_image_t image, uint64_t ofs, size_t len,
// int (*cb)(uint64_t, size_t, const char *, void *), void *arg);
// TODO: int rbd_read_iterate2(rbd_image_t image, uint64_t ofs, uint64_t len,
// int (*cb)(uint64_t, size_t, const char *, void *), void *arg);
// TODO: int rbd_diff_iterate(rbd_image_t image,
// const char *fromsnapname,
// uint64_t ofs, uint64_t len,
// int (*cb)(uint64_t, size_t, int, void *), void *arg);
func (image *Image) Read(data []byte) (int, error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
if len(data) == 0 {
return 0, nil
}
ret := int(C.rbd_read(
image.image,
(C.uint64_t)(image.offset),
(C.size_t)(len(data)),
(*C.char)(unsafe.Pointer(&data[0]))))
if ret < 0 {
return 0, RBDError(ret)
}
image.offset += int64(ret)
if ret < len(data) {
return ret, io.EOF
}
return ret, nil
}
// ssize_t rbd_write(rbd_image_t image, uint64_t ofs, size_t len, const char *buf);
func (image *Image) Write(data []byte) (n int, err error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
ret := int(C.rbd_write(image.image, C.uint64_t(image.offset),
C.size_t(len(data)), (*C.char)(unsafe.Pointer(&data[0]))))
if ret >= 0 {
image.offset += int64(ret)
}
if ret != len(data) {
err = RBDError(-C.EPERM)
}
return ret, err
}
// Seek updates the internal file position for the current image.
func (image *Image) Seek(offset int64, whence int) (int64, error) {
switch whence {
case SeekSet:
image.offset = offset
case SeekCur:
image.offset += offset
case SeekEnd:
stats, err := image.Stat()
if err != nil {
return 0, err
}
image.offset = int64(stats.Size) - offset
default:
return 0, errors.New("Wrong value for whence")
}
return image.offset, nil
}
// Discard the supplied range from the image. The supplied range will be read
// as zeros once Discard returns. The discarded range will no longer take up
// space.
//
// Implements:
// int rbd_discard(rbd_image_t image, uint64_t ofs, uint64_t len);
func (image *Image) Discard(ofs uint64, length uint64) (int, error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
ret := C.rbd_discard(image.image, C.uint64_t(ofs), C.uint64_t(length))
if ret < 0 {
return 0, RBDError(ret)
}
return int(ret), nil
}
// ReadAt copies data from the image into the supplied buffer.
func (image *Image) ReadAt(data []byte, off int64) (int, error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
if len(data) == 0 {
return 0, nil
}
ret := int(C.rbd_read(
image.image,
(C.uint64_t)(off),
(C.size_t)(len(data)),
(*C.char)(unsafe.Pointer(&data[0]))))
if ret < 0 {
return 0, RBDError(ret)
}
if ret < len(data) {
return ret, io.EOF
}
return ret, nil
}
// WriteAt copies data from the supplied buffer to the image.
func (image *Image) WriteAt(data []byte, off int64) (n int, err error) {
if err := image.validate(imageIsOpen); err != nil {
return 0, err
}
if len(data) == 0 {
return 0, nil
}
ret := int(C.rbd_write(image.image, C.uint64_t(off),
C.size_t(len(data)), (*C.char)(unsafe.Pointer(&data[0]))))
if ret != len(data) {
err = RBDError(-C.EPERM)
}
return ret, err
}
// Flush all cached writes to storage.
//
// Implements:
// int rbd_flush(rbd_image_t image);
func (image *Image) Flush() error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
return getError(C.rbd_flush(image.image))
}
// GetSnapshotNames returns more than just the names of snapshots
// associated with the rbd image.
//
// Implements:
// int rbd_snap_list(rbd_image_t image, rbd_snap_info_t *snaps, int *max_snaps);
func (image *Image) GetSnapshotNames() (snaps []SnapInfo, err error) {
if err := image.validate(imageIsOpen); err != nil {
return nil, err
}
var c_max_snaps C.int
ret := C.rbd_snap_list(image.image, nil, &c_max_snaps)
c_snaps := make([]C.rbd_snap_info_t, c_max_snaps)
snaps = make([]SnapInfo, c_max_snaps)
ret = C.rbd_snap_list(image.image,
&c_snaps[0], &c_max_snaps)
if ret < 0 {
return nil, RBDError(ret)
}
for i, s := range c_snaps {
snaps[i] = SnapInfo{Id: uint64(s.id),
Size: uint64(s.size),
Name: C.GoString(s.name)}
}
C.rbd_snap_list_end(&c_snaps[0])
return snaps[:len(snaps)-1], nil
}
// GetMetadata returns the metadata string associated with the given key.
//
// Implements:
// int rbd_metadata_get(rbd_image_t image, const char *key, char *value, size_t *vallen)
func (image *Image) GetMetadata(key string) (string, error) {
if err := image.validate(imageIsOpen); err != nil {
return "", err
}
c_key := C.CString(key)
defer C.free(unsafe.Pointer(c_key))
var (
buf []byte
err error
)
retry.WithSizes(4096, 262144, func(size int) retry.Hint {
csize := C.size_t(size)
buf = make([]byte, csize)
// rbd_metadata_get is a bit quirky and *does not* update the size
// value if the size passed in >= the needed size.
ret := C.rbd_metadata_get(
image.image, c_key, (*C.char)(unsafe.Pointer(&buf[0])), &csize)
err = getError(ret)
return retry.Size(int(csize)).If(err == errRange)
})
if err != nil {
return "", err
}
return C.GoString((*C.char)(unsafe.Pointer(&buf[0]))), nil
}
// SetMetadata updates the metadata string associated with the given key.
//
// Implements:
// int rbd_metadata_set(rbd_image_t image, const char *key, const char *value)
func (image *Image) SetMetadata(key string, value string) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
c_key := C.CString(key)
c_value := C.CString(value)
defer C.free(unsafe.Pointer(c_key))
defer C.free(unsafe.Pointer(c_value))
ret := C.rbd_metadata_set(image.image, c_key, c_value)
if ret < 0 {
return RBDError(ret)
}
return nil
}
// RemoveMetadata clears the metadata associated with the given key.
//
// Implements:
// int rbd_metadata_remove(rbd_image_t image, const char *key)
func (image *Image) RemoveMetadata(key string) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
c_key := C.CString(key)
defer C.free(unsafe.Pointer(c_key))
ret := C.rbd_metadata_remove(image.image, c_key)
if ret < 0 {
return RBDError(ret)
}
return nil
}
// GetId returns the internal image ID string.
//
// Implements:
// int rbd_get_id(rbd_image_t image, char *id, size_t id_len);
func (image *Image) GetId() (string, error) {
if err := image.validate(imageIsOpen); err != nil {
return "", err
}
var (
err error
buf []byte
)
retry.WithSizes(1, 8192, func(size int) retry.Hint {
buf = make([]byte, size)
ret := C.rbd_get_id(
image.image,
(*C.char)(unsafe.Pointer(&buf[0])),
C.size_t(size))
err = getErrorIfNegative(ret)
return retry.DoubleSize.If(err == errRange)
})
if err != nil {
return "", err
}
id := C.GoString((*C.char)(unsafe.Pointer(&buf[0])))
return id, nil
}
// GetTrashList returns a slice of TrashInfo structs, containing information about all RBD images
// currently residing in the trash.
func GetTrashList(ioctx *rados.IOContext) ([]TrashInfo, error) {
var (
err error
count C.size_t
entries []C.rbd_trash_image_info_t
)
retry.WithSizes(32, 1024, func(size int) retry.Hint {
count = C.size_t(size)
entries = make([]C.rbd_trash_image_info_t, count)
ret := C.rbd_trash_list(cephIoctx(ioctx), &entries[0], &count)
err = getErrorIfNegative(ret)
return retry.Size(int(count)).If(err == errRange)
})
if err != nil {
return nil, err
}
// Free rbd_trash_image_info_t pointers
defer C.rbd_trash_list_cleanup(&entries[0], count)
trashList := make([]TrashInfo, count)
for i, ti := range entries[:count] {
trashList[i] = TrashInfo{
Id: C.GoString(ti.id),
Name: C.GoString(ti.name),