-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlvol.c
More file actions
2740 lines (2282 loc) · 69.7 KB
/
Copy pathlvol.c
File metadata and controls
2740 lines (2282 loc) · 69.7 KB
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
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (C) 2017 Intel Corporation.
* All rights reserved.
* Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*/
#include "spdk_internal/lvolstore.h"
#include "spdk/log.h"
#include "spdk/string.h"
#include "spdk/thread.h"
#include "spdk/blob_bdev.h"
#include "spdk/tree.h"
#include "spdk/util.h"
/* Default blob channel opts for lvol */
#define SPDK_LVOL_BLOB_OPTS_CHANNEL_OPS 512
/**
* Maximum number of user-defined attributes for volume snapshot
* creation operations.
*/
#define SPDK_LVOL_MAX_SNAPSHOT_ATTRS 32
#define LVOL_NAME "name"
#define LVOL_UUID "uuid"
SPDK_LOG_REGISTER_COMPONENT(lvol)
struct spdk_lvs_degraded_lvol_set {
struct spdk_lvol_store *lvol_store;
const void *esnap_id;
uint32_t id_len;
TAILQ_HEAD(degraded_lvols, spdk_lvol) lvols;
RB_ENTRY(spdk_lvs_degraded_lvol_set) node;
};
static TAILQ_HEAD(, spdk_lvol_store) g_lvol_stores = TAILQ_HEAD_INITIALIZER(g_lvol_stores);
static pthread_mutex_t g_lvol_stores_mutex = PTHREAD_MUTEX_INITIALIZER;
static inline int lvs_opts_copy(const struct spdk_lvs_opts *src, struct spdk_lvs_opts *dst);
static int lvs_esnap_bs_dev_create(void *bs_ctx, void *blob_ctx, struct spdk_blob *blob,
const void *esnap_id, uint32_t id_len,
struct spdk_bs_dev **_bs_dev);
static void lvs_degraded_lvol_set_add(struct spdk_lvs_degraded_lvol_set *degraded_set,
struct spdk_lvol *lvol);
static void lvs_degraded_lvol_set_remove(struct spdk_lvs_degraded_lvol_set *degraded_set,
struct spdk_lvol *lvol);
struct xattr_value_ext_arg {
struct spdk_blob_xattr_opts lvol_xattrs;
struct spdk_lvol *lvol;
struct spdk_xattr_descriptor *user_xattrs;
uint32_t user_xattrs_count;
};
struct spdk_lvol_snapshot_req {
spdk_blob_op_with_id_complete orig_cb_fn;
void *orig_cb_arg;
struct spdk_lvol *parent;
};
static int
add_lvs_to_list(struct spdk_lvol_store *lvs)
{
struct spdk_lvol_store *tmp;
bool name_conflict = false;
pthread_mutex_lock(&g_lvol_stores_mutex);
TAILQ_FOREACH(tmp, &g_lvol_stores, link) {
if (!strncmp(lvs->name, tmp->name, SPDK_LVS_NAME_MAX)) {
name_conflict = true;
break;
}
}
if (!name_conflict) {
lvs->on_list = true;
TAILQ_INSERT_TAIL(&g_lvol_stores, lvs, link);
}
pthread_mutex_unlock(&g_lvol_stores_mutex);
return name_conflict ? -1 : 0;
}
static struct spdk_lvol_store *
lvs_alloc(void)
{
struct spdk_lvol_store *lvs;
lvs = calloc(1, sizeof(*lvs));
if (lvs == NULL) {
return NULL;
}
TAILQ_INIT(&lvs->lvols);
TAILQ_INIT(&lvs->pending_lvols);
TAILQ_INIT(&lvs->retry_open_lvols);
lvs->load_esnaps = false;
RB_INIT(&lvs->degraded_lvol_sets_tree);
lvs->thread = spdk_get_thread();
return lvs;
}
static void
lvs_free(struct spdk_lvol_store *lvs)
{
pthread_mutex_lock(&g_lvol_stores_mutex);
if (lvs->on_list) {
TAILQ_REMOVE(&g_lvol_stores, lvs, link);
}
pthread_mutex_unlock(&g_lvol_stores_mutex);
assert(RB_EMPTY(&lvs->degraded_lvol_sets_tree));
free(lvs);
}
static struct spdk_lvol *
lvol_alloc(struct spdk_lvol_store *lvs, const char *name, struct spdk_uuid *user_uuid,
bool thin_provision,
enum lvol_clear_method clear_method)
{
struct spdk_lvol *lvol;
lvol = calloc(1, sizeof(*lvol));
if (lvol == NULL) {
return NULL;
}
lvol->lvol_store = lvs;
lvol->clear_method = (enum blob_clear_method)clear_method;
snprintf(lvol->name, sizeof(lvol->name), "%s", name);
if (user_uuid) {
spdk_uuid_copy(&lvol->uuid, user_uuid);
} else {
spdk_uuid_generate(&lvol->uuid);
}
spdk_uuid_fmt_lower(lvol->uuid_str, sizeof(lvol->uuid_str), &lvol->uuid);
spdk_uuid_fmt_lower(lvol->unique_id, sizeof(lvol->uuid_str), &lvol->uuid);
TAILQ_INSERT_TAIL(&lvs->pending_lvols, lvol, link);
return lvol;
}
static void
lvol_free(struct spdk_lvol *lvol)
{
free(lvol);
}
static void
lvol_open_cb(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
{
struct spdk_lvol_with_handle_req *req = cb_arg;
struct spdk_lvol *lvol = req->lvol;
if (lvolerrno != 0) {
SPDK_INFOLOG(lvol, "Failed to open lvol %s\n", lvol->unique_id);
goto end;
}
lvol->ref_count++;
lvol->blob = blob;
end:
req->cb_fn(req->cb_arg, lvol, lvolerrno);
free(req);
}
void
spdk_lvol_open(struct spdk_lvol *lvol, spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg)
{
struct spdk_lvol_with_handle_req *req;
struct spdk_blob_open_opts opts;
assert(cb_fn != NULL);
if (lvol == NULL) {
SPDK_ERRLOG("lvol does not exist\n");
cb_fn(cb_arg, NULL, -ENODEV);
return;
}
if (lvol->action_in_progress == true) {
SPDK_ERRLOG("Cannot open lvol - operations on lvol pending\n");
cb_fn(cb_arg, lvol, -EBUSY);
return;
}
if (lvol->ref_count > 0) {
lvol->ref_count++;
cb_fn(cb_arg, lvol, 0);
return;
}
req = calloc(1, sizeof(*req));
if (req == NULL) {
SPDK_ERRLOG("Cannot alloc memory for request structure\n");
cb_fn(cb_arg, NULL, -ENOMEM);
return;
}
req->cb_fn = cb_fn;
req->cb_arg = cb_arg;
req->lvol = lvol;
spdk_blob_open_opts_init(&opts, sizeof(opts));
opts.clear_method = lvol->clear_method;
spdk_bs_open_blob_ext(lvol->lvol_store->blobstore, lvol->blob_id, &opts, lvol_open_cb, req);
}
static void
bs_unload_with_error_cb(void *cb_arg, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = (struct spdk_lvs_with_handle_req *)cb_arg;
req->cb_fn(req->cb_arg, NULL, req->lvserrno);
free(req);
}
static void
load_next_lvol(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
struct spdk_blob_store *bs = lvs->blobstore;
struct spdk_lvol *lvol, *tmp;
spdk_blob_id blob_id;
const char *attr;
size_t value_len;
int rc;
if (lvolerrno == -ENOENT) {
/* Finished iterating */
if (req->lvserrno == 0) {
lvs->load_esnaps = true;
req->cb_fn(req->cb_arg, lvs, req->lvserrno);
free(req);
} else {
TAILQ_FOREACH_SAFE(lvol, &lvs->lvols, link, tmp) {
TAILQ_REMOVE(&lvs->lvols, lvol, link);
lvol_free(lvol);
}
lvs_free(lvs);
spdk_bs_unload(bs, bs_unload_with_error_cb, req);
}
return;
} else if (lvolerrno < 0) {
SPDK_ERRLOG("Failed to fetch blobs list\n");
req->lvserrno = lvolerrno;
goto invalid;
}
blob_id = spdk_blob_get_id(blob);
if (blob_id == lvs->super_blob_id) {
SPDK_INFOLOG(lvol, "found superblob %"PRIu64"\n", (uint64_t)blob_id);
spdk_bs_iter_next(bs, blob, load_next_lvol, req);
return;
}
lvol = calloc(1, sizeof(*lvol));
if (!lvol) {
SPDK_ERRLOG("Cannot alloc memory for lvol base pointer\n");
req->lvserrno = -ENOMEM;
goto invalid;
}
/*
* Do not store a reference to blob now because spdk_bs_iter_next() will close it.
* Storing blob_id for future lookups is fine.
*/
lvol->blob_id = blob_id;
lvol->lvol_store = lvs;
rc = spdk_blob_get_xattr_value(blob, LVOL_UUID, (const void **)&attr, &value_len);
if (rc != 0 || value_len != SPDK_UUID_STRING_LEN || attr[SPDK_UUID_STRING_LEN - 1] != '\0' ||
spdk_uuid_parse(&lvol->uuid, attr) != 0) {
SPDK_INFOLOG(lvol, "Missing or corrupt lvol uuid\n");
spdk_uuid_set_null(&lvol->uuid);
}
spdk_uuid_fmt_lower(lvol->uuid_str, sizeof(lvol->uuid_str), &lvol->uuid);
rc = spdk_blob_get_xattr_value(blob, "name", (const void **)&attr, &value_len);
if (rc != 0 || value_len > SPDK_LVOL_NAME_MAX) {
SPDK_ERRLOG("Cannot assign lvol name\n");
lvol_free(lvol);
req->lvserrno = -EINVAL;
goto invalid;
}
snprintf(lvol->name, sizeof(lvol->name), "%s", attr);
/*
* unique_id becomes bdev.name which is why we use lvol name instead
* of randomly generated uuid.
*/
if (snprintf(lvol->unique_id, sizeof(lvol->unique_id), "%s", lvol->name) < 0) {
/*
* we don't care if name is longer than unique_id because
* with mayastor that does not happen
*/
abort();
}
TAILQ_INSERT_TAIL(&lvs->lvols, lvol, link);
lvs->lvol_count++;
SPDK_INFOLOG(lvol, "added lvol %s (%s)\n", lvol->unique_id, lvol->uuid_str);
invalid:
spdk_bs_iter_next(bs, blob, load_next_lvol, req);
}
static void
close_super_cb(void *cb_arg, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = (struct spdk_lvs_with_handle_req *)cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
struct spdk_blob_store *bs = lvs->blobstore;
if (lvolerrno != 0) {
SPDK_INFOLOG(lvol, "Could not close super blob\n");
lvs_free(lvs);
req->lvserrno = -ENODEV;
spdk_bs_unload(bs, bs_unload_with_error_cb, req);
return;
}
/* Start loading lvols */
spdk_bs_iter_first(lvs->blobstore, load_next_lvol, req);
}
static void
close_super_blob_with_error_cb(void *cb_arg, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = (struct spdk_lvs_with_handle_req *)cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
struct spdk_blob_store *bs = lvs->blobstore;
lvs_free(lvs);
spdk_bs_unload(bs, bs_unload_with_error_cb, req);
}
static void
lvs_read_uuid(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = (struct spdk_lvs_with_handle_req *)cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
struct spdk_blob_store *bs = lvs->blobstore;
const char *attr;
size_t value_len;
int rc;
if (lvolerrno != 0) {
SPDK_INFOLOG(lvol, "Could not open super blob\n");
lvs_free(lvs);
req->lvserrno = -ENODEV;
spdk_bs_unload(bs, bs_unload_with_error_cb, req);
return;
}
rc = spdk_blob_get_xattr_value(blob, LVOL_UUID, (const void **)&attr, &value_len);
if (rc != 0 || value_len != SPDK_UUID_STRING_LEN || attr[SPDK_UUID_STRING_LEN - 1] != '\0') {
SPDK_INFOLOG(lvol, "degraded_set or incorrect UUID\n");
req->lvserrno = -EINVAL;
spdk_blob_close(blob, close_super_blob_with_error_cb, req);
return;
}
if (spdk_uuid_parse(&lvs->uuid, attr)) {
SPDK_INFOLOG(lvol, "incorrect UUID '%s'\n", attr);
req->lvserrno = -EINVAL;
spdk_blob_close(blob, close_super_blob_with_error_cb, req);
return;
}
rc = spdk_blob_get_xattr_value(blob, "name", (const void **)&attr, &value_len);
if (rc != 0 || value_len > SPDK_LVS_NAME_MAX) {
SPDK_INFOLOG(lvol, "degraded_set or invalid name\n");
req->lvserrno = -EINVAL;
spdk_blob_close(blob, close_super_blob_with_error_cb, req);
return;
}
snprintf(lvs->name, sizeof(lvs->name), "%s", attr);
rc = add_lvs_to_list(lvs);
if (rc) {
SPDK_INFOLOG(lvol, "lvolstore with name %s already exists\n", lvs->name);
req->lvserrno = -EEXIST;
spdk_blob_close(blob, close_super_blob_with_error_cb, req);
return;
}
lvs->super_blob_id = spdk_blob_get_id(blob);
spdk_blob_close(blob, close_super_cb, req);
}
static void
lvs_open_super(void *cb_arg, spdk_blob_id blobid, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = (struct spdk_lvs_with_handle_req *)cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
struct spdk_blob_store *bs = lvs->blobstore;
if (lvolerrno != 0) {
SPDK_INFOLOG(lvol, "Super blob not found\n");
lvs_free(lvs);
req->lvserrno = -ENODEV;
spdk_bs_unload(bs, bs_unload_with_error_cb, req);
return;
}
spdk_bs_open_blob(bs, blobid, lvs_read_uuid, req);
}
static void
lvs_load_cb(void *cb_arg, struct spdk_blob_store *bs, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = (struct spdk_lvs_with_handle_req *)cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
if (lvolerrno != 0) {
req->cb_fn(req->cb_arg, NULL, lvolerrno);
lvs_free(lvs);
free(req);
return;
}
lvs->blobstore = bs;
lvs->bs_dev = req->bs_dev;
spdk_bs_get_super(bs, lvs_open_super, req);
}
static void
lvs_bs_opts_init(struct spdk_bs_opts *opts)
{
spdk_bs_opts_init(opts, sizeof(*opts));
opts->max_channel_ops = SPDK_LVOL_BLOB_OPTS_CHANNEL_OPS;
}
static void
lvs_load(struct spdk_bs_dev *bs_dev, const struct spdk_lvs_opts *_lvs_opts,
spdk_lvs_op_with_handle_complete cb_fn, void *cb_arg)
{
struct spdk_lvs_with_handle_req *req;
struct spdk_bs_opts bs_opts = {};
struct spdk_lvs_opts lvs_opts;
assert(cb_fn != NULL);
if (bs_dev == NULL) {
SPDK_ERRLOG("Blobstore device does not exist\n");
cb_fn(cb_arg, NULL, -ENODEV);
return;
}
spdk_lvs_opts_init(&lvs_opts);
if (_lvs_opts != NULL) {
if (lvs_opts_copy(_lvs_opts, &lvs_opts) != 0) {
SPDK_ERRLOG("Invalid options\n");
cb_fn(cb_arg, NULL, -EINVAL);
return;
}
}
req = calloc(1, sizeof(*req));
if (req == NULL) {
SPDK_ERRLOG("Cannot alloc memory for request structure\n");
cb_fn(cb_arg, NULL, -ENOMEM);
return;
}
req->lvol_store = lvs_alloc();
if (req->lvol_store == NULL) {
SPDK_ERRLOG("Cannot alloc memory for lvol store\n");
free(req);
cb_fn(cb_arg, NULL, -ENOMEM);
return;
}
req->cb_fn = cb_fn;
req->cb_arg = cb_arg;
req->bs_dev = bs_dev;
lvs_bs_opts_init(&bs_opts);
snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "LVOLSTORE");
if (lvs_opts.esnap_bs_dev_create != NULL) {
req->lvol_store->esnap_bs_dev_create = lvs_opts.esnap_bs_dev_create;
bs_opts.esnap_bs_dev_create = lvs_esnap_bs_dev_create;
bs_opts.esnap_ctx = req->lvol_store;
}
spdk_bs_load(bs_dev, &bs_opts, lvs_load_cb, req);
}
void
spdk_lvs_load(struct spdk_bs_dev *bs_dev, spdk_lvs_op_with_handle_complete cb_fn, void *cb_arg)
{
lvs_load(bs_dev, NULL, cb_fn, cb_arg);
}
void
spdk_lvs_load_ext(struct spdk_bs_dev *bs_dev, const struct spdk_lvs_opts *opts,
spdk_lvs_op_with_handle_complete cb_fn, void *cb_arg)
{
lvs_load(bs_dev, opts, cb_fn, cb_arg);
}
static void
remove_bs_on_error_cb(void *cb_arg, int bserrno)
{
}
static void
exit_error_lvs_req(struct spdk_lvs_with_handle_req *req, struct spdk_lvol_store *lvs, int lvolerrno)
{
req->cb_fn(req->cb_arg, NULL, lvolerrno);
spdk_bs_destroy(lvs->blobstore, remove_bs_on_error_cb, NULL);
lvs_free(lvs);
free(req);
}
static void
super_create_close_cb(void *cb_arg, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
if (lvolerrno < 0) {
SPDK_ERRLOG("Lvol store init failed: could not close super blob\n");
exit_error_lvs_req(req, lvs, lvolerrno);
return;
}
req->cb_fn(req->cb_arg, lvs, lvolerrno);
free(req);
}
static void
super_blob_set_cb(void *cb_arg, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
struct spdk_blob *blob = lvs->super_blob;
if (lvolerrno < 0) {
SPDK_ERRLOG("Lvol store init failed: could not set uuid for super blob\n");
exit_error_lvs_req(req, lvs, lvolerrno);
return;
}
spdk_blob_close(blob, super_create_close_cb, req);
}
static void
super_blob_init_cb(void *cb_arg, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
struct spdk_blob *blob = lvs->super_blob;
char uuid[SPDK_UUID_STRING_LEN];
if (lvolerrno < 0) {
SPDK_ERRLOG("Lvol store init failed: could not set super blob\n");
exit_error_lvs_req(req, lvs, lvolerrno);
return;
}
spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvs->uuid);
spdk_blob_set_xattr(blob, LVOL_UUID, uuid, sizeof(uuid));
spdk_blob_set_xattr(blob, "name", lvs->name, strnlen(lvs->name, SPDK_LVS_NAME_MAX) + 1);
spdk_blob_sync_md(blob, super_blob_set_cb, req);
}
static void
super_blob_create_open_cb(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
if (lvolerrno < 0) {
SPDK_ERRLOG("Lvol store init failed: could not open super blob\n");
exit_error_lvs_req(req, lvs, lvolerrno);
return;
}
lvs->super_blob = blob;
lvs->super_blob_id = spdk_blob_get_id(blob);
spdk_bs_set_super(lvs->blobstore, lvs->super_blob_id, super_blob_init_cb, req);
}
static void
super_blob_create_cb(void *cb_arg, spdk_blob_id blobid, int lvolerrno)
{
struct spdk_lvs_with_handle_req *req = cb_arg;
struct spdk_lvol_store *lvs = req->lvol_store;
struct spdk_blob_store *bs;
if (lvolerrno < 0) {
SPDK_ERRLOG("Lvol store init failed: could not create super blob\n");
exit_error_lvs_req(req, lvs, lvolerrno);
return;
}
bs = req->lvol_store->blobstore;
spdk_bs_open_blob(bs, blobid, super_blob_create_open_cb, req);
}
static void
lvs_init_cb(void *cb_arg, struct spdk_blob_store *bs, int lvserrno)
{
struct spdk_lvs_with_handle_req *lvs_req = cb_arg;
struct spdk_lvol_store *lvs = lvs_req->lvol_store;
if (lvserrno != 0) {
assert(bs == NULL);
lvs_req->cb_fn(lvs_req->cb_arg, NULL, lvserrno);
SPDK_ERRLOG("Lvol store init failed: could not initialize blobstore\n");
lvs_free(lvs);
free(lvs_req);
return;
}
assert(bs != NULL);
lvs->blobstore = bs;
SPDK_INFOLOG(lvol, "Lvol store initialized\n");
/* create super blob */
spdk_bs_create_blob(lvs->blobstore, super_blob_create_cb, lvs_req);
}
void
spdk_lvs_opts_init(struct spdk_lvs_opts *o)
{
memset(o, 0, sizeof(*o));
o->cluster_sz = SPDK_LVS_OPTS_CLUSTER_SZ;
o->clear_method = LVS_CLEAR_WITH_UNMAP;
o->num_md_pages_per_cluster_ratio = 100;
o->uuid = NULL;
o->opts_size = sizeof(*o);
}
static inline int
lvs_opts_copy(const struct spdk_lvs_opts *src, struct spdk_lvs_opts *dst)
{
if (src->opts_size == 0) {
SPDK_ERRLOG("opts_size should not be zero value\n");
return -1;
}
#define FIELD_OK(field) \
offsetof(struct spdk_lvs_opts, field) + sizeof(src->field) <= src->opts_size
#define SET_FIELD(field) \
if (FIELD_OK(field)) { \
dst->field = src->field; \
} \
SET_FIELD(cluster_sz);
SET_FIELD(clear_method);
if (FIELD_OK(name)) {
memcpy(&dst->name, &src->name, sizeof(dst->name));
}
SET_FIELD(num_md_pages_per_cluster_ratio);
SET_FIELD(opts_size);
SET_FIELD(esnap_bs_dev_create);
SET_FIELD(md_page_size);
SET_FIELD(uuid);
dst->opts_size = src->opts_size;
/* You should not remove this statement, but need to update the assert statement
* if you add a new field, and also add a corresponding SET_FIELD statement */
SPDK_STATIC_ASSERT(sizeof(struct spdk_lvs_opts) == 100, "Incorrect size");
#undef FIELD_OK
#undef SET_FIELD
return 0;
}
static void
setup_lvs_opts(struct spdk_bs_opts *bs_opts, struct spdk_lvs_opts *o, uint32_t total_clusters,
void *esnap_ctx)
{
assert(o != NULL);
lvs_bs_opts_init(bs_opts);
bs_opts->cluster_sz = o->cluster_sz;
bs_opts->clear_method = (enum bs_clear_method)o->clear_method;
bs_opts->num_md_pages = (o->num_md_pages_per_cluster_ratio * total_clusters) / 100;
bs_opts->md_page_size = o->md_page_size;
bs_opts->esnap_bs_dev_create = o->esnap_bs_dev_create;
bs_opts->esnap_ctx = esnap_ctx;
snprintf(bs_opts->bstype.bstype, sizeof(bs_opts->bstype.bstype), "LVOLSTORE");
}
int
spdk_lvs_init(struct spdk_bs_dev *bs_dev, struct spdk_lvs_opts *o,
spdk_lvs_op_with_handle_complete cb_fn, void *cb_arg)
{
struct spdk_lvol_store *lvs;
struct spdk_lvs_with_handle_req *lvs_req;
struct spdk_bs_opts opts = {};
struct spdk_lvs_opts lvs_opts;
uint32_t total_clusters;
int rc, len;
if (bs_dev == NULL) {
SPDK_ERRLOG("Blobstore device does not exist\n");
return -ENODEV;
}
if (o == NULL) {
SPDK_ERRLOG("spdk_lvs_opts not specified\n");
return -EINVAL;
}
spdk_lvs_opts_init(&lvs_opts);
if (lvs_opts_copy(o, &lvs_opts) != 0) {
SPDK_ERRLOG("spdk_lvs_opts invalid\n");
return -EINVAL;
}
if (lvs_opts.cluster_sz < bs_dev->blocklen || (lvs_opts.cluster_sz % bs_dev->blocklen) != 0) {
SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than blocklen %" PRIu32
"Or not an integral multiple\n", lvs_opts.cluster_sz, bs_dev->blocklen);
return -EINVAL;
}
total_clusters = bs_dev->blockcnt / (lvs_opts.cluster_sz / bs_dev->blocklen);
lvs = lvs_alloc();
if (!lvs) {
SPDK_ERRLOG("Cannot alloc memory for lvol store base pointer\n");
return -ENOMEM;
}
setup_lvs_opts(&opts, o, total_clusters, lvs);
len = strnlen(lvs_opts.name, SPDK_LVS_NAME_MAX);
if (len == 0 || len == SPDK_LVS_NAME_MAX) {
SPDK_ERRLOG("Name must be between 1 and %d characters\n", SPDK_LVS_NAME_MAX - 1);
lvs_free(lvs);
return -EINVAL;
}
if (!o->uuid) {
spdk_uuid_generate(&lvs->uuid);
} else if (spdk_uuid_parse(&lvs->uuid, o->uuid) != 0) {
lvs_free(lvs);
SPDK_ERRLOG("Invalid lvs uuid provided\n");
return -EINVAL;
}
snprintf(lvs->name, sizeof(lvs->name), "%s", o->name);
rc = add_lvs_to_list(lvs);
if (rc) {
SPDK_ERRLOG("lvolstore with name %s already exists\n", lvs->name);
lvs_free(lvs);
return -EEXIST;
}
lvs_req = calloc(1, sizeof(*lvs_req));
if (!lvs_req) {
lvs_free(lvs);
SPDK_ERRLOG("Cannot alloc memory for lvol store request pointer\n");
return -ENOMEM;
}
assert(cb_fn != NULL);
lvs_req->cb_fn = cb_fn;
lvs_req->cb_arg = cb_arg;
lvs_req->lvol_store = lvs;
lvs->bs_dev = bs_dev;
SPDK_INFOLOG(lvol, "Initializing lvol store\n");
spdk_bs_init(bs_dev, &opts, lvs_init_cb, lvs_req);
return 0;
}
static void
lvs_rename_cb(void *cb_arg, int lvolerrno)
{
struct spdk_lvs_req *req = cb_arg;
if (lvolerrno != 0) {
req->lvserrno = lvolerrno;
}
if (req->lvserrno != 0) {
SPDK_ERRLOG("Lvol store rename operation failed\n");
/* Lvs renaming failed, so we should 'clear' new_name.
* Otherwise it could cause a failure on the next attempt to change the name to 'new_name' */
snprintf(req->lvol_store->new_name,
sizeof(req->lvol_store->new_name),
"%s", req->lvol_store->name);
} else {
/* Update lvs name with new_name */
snprintf(req->lvol_store->name,
sizeof(req->lvol_store->name),
"%s", req->lvol_store->new_name);
}
req->cb_fn(req->cb_arg, req->lvserrno);
free(req);
}
static void
lvs_rename_sync_cb(void *cb_arg, int lvolerrno)
{
struct spdk_lvs_req *req = cb_arg;
struct spdk_blob *blob = req->lvol_store->super_blob;
if (lvolerrno < 0) {
req->lvserrno = lvolerrno;
}
spdk_blob_close(blob, lvs_rename_cb, req);
}
static void
lvs_rename_open_cb(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
{
struct spdk_lvs_req *req = cb_arg;
int rc;
if (lvolerrno < 0) {
lvs_rename_cb(cb_arg, lvolerrno);
return;
}
rc = spdk_blob_set_xattr(blob, "name", req->lvol_store->new_name,
strlen(req->lvol_store->new_name) + 1);
if (rc < 0) {
req->lvserrno = rc;
lvs_rename_sync_cb(req, rc);
return;
}
req->lvol_store->super_blob = blob;
spdk_blob_sync_md(blob, lvs_rename_sync_cb, req);
}
void
spdk_lvs_rename(struct spdk_lvol_store *lvs, const char *new_name,
spdk_lvs_op_complete cb_fn, void *cb_arg)
{
struct spdk_lvs_req *req;
struct spdk_lvol_store *tmp;
/* Check if new name is current lvs name.
* If so, return success immediately */
if (strncmp(lvs->name, new_name, SPDK_LVS_NAME_MAX) == 0) {
cb_fn(cb_arg, 0);
return;
}
/* Check if new or new_name is already used in other lvs */
pthread_mutex_lock(&g_lvol_stores_mutex);
TAILQ_FOREACH(tmp, &g_lvol_stores, link) {
if (!strncmp(new_name, tmp->name, SPDK_LVS_NAME_MAX) ||
!strncmp(new_name, tmp->new_name, SPDK_LVS_NAME_MAX)) {
pthread_mutex_unlock(&g_lvol_stores_mutex);
cb_fn(cb_arg, -EEXIST);
return;
}
}
pthread_mutex_unlock(&g_lvol_stores_mutex);
req = calloc(1, sizeof(*req));
if (!req) {
SPDK_ERRLOG("Cannot alloc memory for lvol request pointer\n");
cb_fn(cb_arg, -ENOMEM);
return;
}
snprintf(lvs->new_name, sizeof(lvs->new_name), "%s", new_name);
req->lvol_store = lvs;
req->cb_fn = cb_fn;
req->cb_arg = cb_arg;
spdk_bs_open_blob(lvs->blobstore, lvs->super_blob_id, lvs_rename_open_cb, req);
}
static void
_lvs_unload_cb(void *cb_arg, int lvserrno)
{
struct spdk_lvs_req *lvs_req = cb_arg;
SPDK_INFOLOG(lvol, "Lvol store unloaded\n");
assert(lvs_req->cb_fn != NULL);
lvs_req->cb_fn(lvs_req->cb_arg, lvserrno);
free(lvs_req);
}
int
spdk_lvs_unload(struct spdk_lvol_store *lvs, spdk_lvs_op_complete cb_fn,
void *cb_arg)
{
struct spdk_lvs_req *lvs_req;
struct spdk_lvol *lvol, *tmp;
if (lvs == NULL) {
SPDK_ERRLOG("Lvol store is NULL\n");
return -ENODEV;
}
TAILQ_FOREACH_SAFE(lvol, &lvs->lvols, link, tmp) {
if (lvol->action_in_progress == true) {
SPDK_ERRLOG("Cannot unload lvol store - operations on lvols pending\n");
cb_fn(cb_arg, -EBUSY);
return -EBUSY;
} else if (lvol->ref_count != 0) {
SPDK_ERRLOG("Lvols still open on lvol store\n");
cb_fn(cb_arg, -EBUSY);
return -EBUSY;
}
}
TAILQ_FOREACH_SAFE(lvol, &lvs->lvols, link, tmp) {
spdk_lvs_esnap_missing_remove(lvol);
TAILQ_REMOVE(&lvs->lvols, lvol, link);
lvol_free(lvol);
}
lvs_req = calloc(1, sizeof(*lvs_req));
if (!lvs_req) {
SPDK_ERRLOG("Cannot alloc memory for lvol store request pointer\n");
return -ENOMEM;
}
lvs_req->cb_fn = cb_fn;
lvs_req->cb_arg = cb_arg;
SPDK_INFOLOG(lvol, "Unloading lvol store\n");
spdk_bs_unload(lvs->blobstore, _lvs_unload_cb, lvs_req);
lvs_free(lvs);
return 0;
}
static void
_lvs_destroy_cb(void *cb_arg, int lvserrno)
{
struct spdk_lvs_destroy_req *lvs_req = cb_arg;
SPDK_INFOLOG(lvol, "Lvol store destroyed\n");
assert(lvs_req->cb_fn != NULL);
lvs_req->cb_fn(lvs_req->cb_arg, lvserrno);
free(lvs_req);
}
static void
_lvs_destroy_super_cb(void *cb_arg, int bserrno)
{
struct spdk_lvs_destroy_req *lvs_req = cb_arg;
struct spdk_lvol_store *lvs = lvs_req->lvs;
assert(lvs != NULL);
SPDK_INFOLOG(lvol, "Destroying lvol store\n");
spdk_bs_destroy(lvs->blobstore, _lvs_destroy_cb, lvs_req);
lvs_free(lvs);
}
int
spdk_lvs_destroy(struct spdk_lvol_store *lvs, spdk_lvs_op_complete cb_fn,
void *cb_arg)
{
struct spdk_lvs_destroy_req *lvs_req;
struct spdk_lvol *iter_lvol, *tmp;
if (lvs == NULL) {
SPDK_ERRLOG("Lvol store is NULL\n");
return -ENODEV;
}
TAILQ_FOREACH_SAFE(iter_lvol, &lvs->lvols, link, tmp) {
if (iter_lvol->action_in_progress == true) {
SPDK_ERRLOG("Cannot destroy lvol store - operations on lvols pending\n");
cb_fn(cb_arg, -EBUSY);
return -EBUSY;
} else if (iter_lvol->ref_count != 0) {
SPDK_ERRLOG("Lvols still open on lvol store\n");
cb_fn(cb_arg, -EBUSY);
return -EBUSY;
}
}
TAILQ_FOREACH_SAFE(iter_lvol, &lvs->lvols, link, tmp) {