forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm-raid.c
4093 lines (3518 loc) · 117 KB
/
dm-raid.c
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
/*
* Copyright (C) 2010-2011 Neil Brown
* Copyright (C) 2010-2017 Red Hat, Inc. All rights reserved.
*
* This file is released under the GPL.
*/
#include <linux/slab.h>
#include <linux/module.h>
#include "md.h"
#include "raid1.h"
#include "raid5.h"
#include "raid10.h"
#include "md-bitmap.h"
#include <linux/device-mapper.h>
#define DM_MSG_PREFIX "raid"
#define MAX_RAID_DEVICES 253 /* md-raid kernel limit */
/*
* Minimum sectors of free reshape space per raid device
*/
#define MIN_FREE_RESHAPE_SPACE to_sector(4*4096)
/*
* Minimum journal space 4 MiB in sectors.
*/
#define MIN_RAID456_JOURNAL_SPACE (4*2048)
/* Global list of all raid sets */
static LIST_HEAD(raid_sets);
static bool devices_handle_discard_safely = false;
/*
* The following flags are used by dm-raid.c to set up the array state.
* They must be cleared before md_run is called.
*/
#define FirstUse 10 /* rdev flag */
struct raid_dev {
/*
* Two DM devices, one to hold metadata and one to hold the
* actual data/parity. The reason for this is to not confuse
* ti->len and give more flexibility in altering size and
* characteristics.
*
* While it is possible for this device to be associated
* with a different physical device than the data_dev, it
* is intended for it to be the same.
* |--------- Physical Device ---------|
* |- meta_dev -|------ data_dev ------|
*/
struct dm_dev *meta_dev;
struct dm_dev *data_dev;
struct md_rdev rdev;
};
/*
* Bits for establishing rs->ctr_flags
*
* 1 = no flag value
* 2 = flag with value
*/
#define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */
#define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */
#define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */
#define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */
#define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */
#define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */
#define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */
/* New for v1.9.0 */
#define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid1/4/5/6/10! */
#define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
#define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */
/* New for v1.10.0 */
#define __CTR_FLAG_JOURNAL_DEV 15 /* 2 */ /* Only with raid4/5/6 (journal device)! */
/* New for v1.11.1 */
#define __CTR_FLAG_JOURNAL_MODE 16 /* 2 */ /* Only with raid4/5/6 (journal mode)! */
/*
* Flags for rs->ctr_flags field.
*/
#define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC)
#define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC)
#define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD)
#define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP)
#define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE)
#define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE)
#define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND)
#define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY)
#define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE)
#define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE)
#define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES)
#define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT)
#define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS)
#define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET)
#define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS)
#define CTR_FLAG_JOURNAL_DEV (1 << __CTR_FLAG_JOURNAL_DEV)
#define CTR_FLAG_JOURNAL_MODE (1 << __CTR_FLAG_JOURNAL_MODE)
/*
* Definitions of various constructor flags to
* be used in checks of valid / invalid flags
* per raid level.
*/
/* Define all any sync flags */
#define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
/* Define flags for options without argument (e.g. 'nosync') */
#define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \
CTR_FLAG_RAID10_USE_NEAR_SETS)
/* Define flags for options with one argument (e.g. 'delta_disks +2') */
#define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
CTR_FLAG_WRITE_MOSTLY | \
CTR_FLAG_DAEMON_SLEEP | \
CTR_FLAG_MIN_RECOVERY_RATE | \
CTR_FLAG_MAX_RECOVERY_RATE | \
CTR_FLAG_MAX_WRITE_BEHIND | \
CTR_FLAG_STRIPE_CACHE | \
CTR_FLAG_REGION_SIZE | \
CTR_FLAG_RAID10_COPIES | \
CTR_FLAG_RAID10_FORMAT | \
CTR_FLAG_DELTA_DISKS | \
CTR_FLAG_DATA_OFFSET)
/* Valid options definitions per raid level... */
/* "raid0" does only accept data offset */
#define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET)
/* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
#define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
CTR_FLAG_REBUILD | \
CTR_FLAG_WRITE_MOSTLY | \
CTR_FLAG_DAEMON_SLEEP | \
CTR_FLAG_MIN_RECOVERY_RATE | \
CTR_FLAG_MAX_RECOVERY_RATE | \
CTR_FLAG_MAX_WRITE_BEHIND | \
CTR_FLAG_REGION_SIZE | \
CTR_FLAG_DELTA_DISKS | \
CTR_FLAG_DATA_OFFSET)
/* "raid10" does not accept any raid1 or stripe cache options */
#define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
CTR_FLAG_REBUILD | \
CTR_FLAG_DAEMON_SLEEP | \
CTR_FLAG_MIN_RECOVERY_RATE | \
CTR_FLAG_MAX_RECOVERY_RATE | \
CTR_FLAG_REGION_SIZE | \
CTR_FLAG_RAID10_COPIES | \
CTR_FLAG_RAID10_FORMAT | \
CTR_FLAG_DELTA_DISKS | \
CTR_FLAG_DATA_OFFSET | \
CTR_FLAG_RAID10_USE_NEAR_SETS)
/*
* "raid4/5/6" do not accept any raid1 or raid10 specific options
*
* "raid6" does not accept "nosync", because it is not guaranteed
* that both parity and q-syndrome are being written properly with
* any writes
*/
#define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
CTR_FLAG_REBUILD | \
CTR_FLAG_DAEMON_SLEEP | \
CTR_FLAG_MIN_RECOVERY_RATE | \
CTR_FLAG_MAX_RECOVERY_RATE | \
CTR_FLAG_STRIPE_CACHE | \
CTR_FLAG_REGION_SIZE | \
CTR_FLAG_DELTA_DISKS | \
CTR_FLAG_DATA_OFFSET | \
CTR_FLAG_JOURNAL_DEV | \
CTR_FLAG_JOURNAL_MODE)
#define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \
CTR_FLAG_REBUILD | \
CTR_FLAG_DAEMON_SLEEP | \
CTR_FLAG_MIN_RECOVERY_RATE | \
CTR_FLAG_MAX_RECOVERY_RATE | \
CTR_FLAG_STRIPE_CACHE | \
CTR_FLAG_REGION_SIZE | \
CTR_FLAG_DELTA_DISKS | \
CTR_FLAG_DATA_OFFSET | \
CTR_FLAG_JOURNAL_DEV | \
CTR_FLAG_JOURNAL_MODE)
/* ...valid options definitions per raid level */
/*
* Flags for rs->runtime_flags field
* (RT_FLAG prefix meaning "runtime flag")
*
* These are all internal and used to define runtime state,
* e.g. to prevent another resume from preresume processing
* the raid set all over again.
*/
#define RT_FLAG_RS_PRERESUMED 0
#define RT_FLAG_RS_RESUMED 1
#define RT_FLAG_RS_BITMAP_LOADED 2
#define RT_FLAG_UPDATE_SBS 3
#define RT_FLAG_RESHAPE_RS 4
#define RT_FLAG_RS_SUSPENDED 5
#define RT_FLAG_RS_IN_SYNC 6
#define RT_FLAG_RS_RESYNCING 7
/* Array elements of 64 bit needed for rebuild/failed disk bits */
#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
/*
* raid set level, layout and chunk sectors backup/restore
*/
struct rs_layout {
int new_level;
int new_layout;
int new_chunk_sectors;
};
struct raid_set {
struct dm_target *ti;
struct list_head list;
uint32_t stripe_cache_entries;
unsigned long ctr_flags;
unsigned long runtime_flags;
uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
int raid_disks;
int delta_disks;
int data_offset;
int raid10_copies;
int requested_bitmap_chunk_sectors;
struct mddev md;
struct raid_type *raid_type;
struct dm_target_callbacks callbacks;
/* Optional raid4/5/6 journal device */
struct journal_dev {
struct dm_dev *dev;
struct md_rdev rdev;
int mode;
} journal_dev;
struct raid_dev dev[0];
};
static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)
{
struct mddev *mddev = &rs->md;
l->new_level = mddev->new_level;
l->new_layout = mddev->new_layout;
l->new_chunk_sectors = mddev->new_chunk_sectors;
}
static void rs_config_restore(struct raid_set *rs, struct rs_layout *l)
{
struct mddev *mddev = &rs->md;
mddev->new_level = l->new_level;
mddev->new_layout = l->new_layout;
mddev->new_chunk_sectors = l->new_chunk_sectors;
}
/* Find any raid_set in active slot for @rs on global list */
static struct raid_set *rs_find_active(struct raid_set *rs)
{
struct raid_set *r;
struct mapped_device *md = dm_table_get_md(rs->ti->table);
list_for_each_entry(r, &raid_sets, list)
if (r != rs && dm_table_get_md(r->ti->table) == md)
return r;
return NULL;
}
/* raid10 algorithms (i.e. formats) */
#define ALGORITHM_RAID10_DEFAULT 0
#define ALGORITHM_RAID10_NEAR 1
#define ALGORITHM_RAID10_OFFSET 2
#define ALGORITHM_RAID10_FAR 3
/* Supported raid types and properties. */
static struct raid_type {
const char *name; /* RAID algorithm. */
const char *descr; /* Descriptor text for logging. */
const unsigned int parity_devs; /* # of parity devices. */
const unsigned int minimal_devs;/* minimal # of devices in set. */
const unsigned int level; /* RAID level. */
const unsigned int algorithm; /* RAID algorithm. */
} raid_types[] = {
{"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */},
{"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */},
{"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR},
{"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET},
{"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR},
{"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT},
{"raid4", "raid4 (dedicated first parity disk)", 1, 2, 5, ALGORITHM_PARITY_0}, /* raid4 layout = raid5_0 */
{"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N},
{"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
{"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
{"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
{"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
{"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
{"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
{"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE},
{"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6},
{"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6},
{"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6},
{"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6},
{"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6}
};
/* True, if @v is in inclusive range [@min, @max] */
static bool __within_range(long v, long min, long max)
{
return v >= min && v <= max;
}
/* All table line arguments are defined here */
static struct arg_name_flag {
const unsigned long flag;
const char *name;
} __arg_name_flags[] = {
{ CTR_FLAG_SYNC, "sync"},
{ CTR_FLAG_NOSYNC, "nosync"},
{ CTR_FLAG_REBUILD, "rebuild"},
{ CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
{ CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
{ CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
{ CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
{ CTR_FLAG_WRITE_MOSTLY, "write_mostly"},
{ CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
{ CTR_FLAG_REGION_SIZE, "region_size"},
{ CTR_FLAG_RAID10_COPIES, "raid10_copies"},
{ CTR_FLAG_RAID10_FORMAT, "raid10_format"},
{ CTR_FLAG_DATA_OFFSET, "data_offset"},
{ CTR_FLAG_DELTA_DISKS, "delta_disks"},
{ CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
{ CTR_FLAG_JOURNAL_DEV, "journal_dev" },
{ CTR_FLAG_JOURNAL_MODE, "journal_mode" },
};
/* Return argument name string for given @flag */
static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
{
if (hweight32(flag) == 1) {
struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
while (anf-- > __arg_name_flags)
if (flag & anf->flag)
return anf->name;
} else
DMERR("%s called with more than one flag!", __func__);
return NULL;
}
/* Define correlation of raid456 journal cache modes and dm-raid target line parameters */
static struct {
const int mode;
const char *param;
} _raid456_journal_mode[] = {
{ R5C_JOURNAL_MODE_WRITE_THROUGH , "writethrough" },
{ R5C_JOURNAL_MODE_WRITE_BACK , "writeback" }
};
/* Return MD raid4/5/6 journal mode for dm @journal_mode one */
static int dm_raid_journal_mode_to_md(const char *mode)
{
int m = ARRAY_SIZE(_raid456_journal_mode);
while (m--)
if (!strcasecmp(mode, _raid456_journal_mode[m].param))
return _raid456_journal_mode[m].mode;
return -EINVAL;
}
/* Return dm-raid raid4/5/6 journal mode string for @mode */
static const char *md_journal_mode_to_dm_raid(const int mode)
{
int m = ARRAY_SIZE(_raid456_journal_mode);
while (m--)
if (mode == _raid456_journal_mode[m].mode)
return _raid456_journal_mode[m].param;
return "unknown";
}
/*
* Bool helpers to test for various raid levels of a raid set.
* It's level as reported by the superblock rather than
* the requested raid_type passed to the constructor.
*/
/* Return true, if raid set in @rs is raid0 */
static bool rs_is_raid0(struct raid_set *rs)
{
return !rs->md.level;
}
/* Return true, if raid set in @rs is raid1 */
static bool rs_is_raid1(struct raid_set *rs)
{
return rs->md.level == 1;
}
/* Return true, if raid set in @rs is raid10 */
static bool rs_is_raid10(struct raid_set *rs)
{
return rs->md.level == 10;
}
/* Return true, if raid set in @rs is level 6 */
static bool rs_is_raid6(struct raid_set *rs)
{
return rs->md.level == 6;
}
/* Return true, if raid set in @rs is level 4, 5 or 6 */
static bool rs_is_raid456(struct raid_set *rs)
{
return __within_range(rs->md.level, 4, 6);
}
/* Return true, if raid set in @rs is reshapable */
static bool __is_raid10_far(int layout);
static bool rs_is_reshapable(struct raid_set *rs)
{
return rs_is_raid456(rs) ||
(rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
}
/* Return true, if raid set in @rs is recovering */
static bool rs_is_recovering(struct raid_set *rs)
{
return rs->md.recovery_cp < rs->md.dev_sectors;
}
/* Return true, if raid set in @rs is reshaping */
static bool rs_is_reshaping(struct raid_set *rs)
{
return rs->md.reshape_position != MaxSector;
}
/*
* bool helpers to test for various raid levels of a raid type @rt
*/
/* Return true, if raid type in @rt is raid0 */
static bool rt_is_raid0(struct raid_type *rt)
{
return !rt->level;
}
/* Return true, if raid type in @rt is raid1 */
static bool rt_is_raid1(struct raid_type *rt)
{
return rt->level == 1;
}
/* Return true, if raid type in @rt is raid10 */
static bool rt_is_raid10(struct raid_type *rt)
{
return rt->level == 10;
}
/* Return true, if raid type in @rt is raid4/5 */
static bool rt_is_raid45(struct raid_type *rt)
{
return __within_range(rt->level, 4, 5);
}
/* Return true, if raid type in @rt is raid6 */
static bool rt_is_raid6(struct raid_type *rt)
{
return rt->level == 6;
}
/* Return true, if raid type in @rt is raid4/5/6 */
static bool rt_is_raid456(struct raid_type *rt)
{
return __within_range(rt->level, 4, 6);
}
/* END: raid level bools */
/* Return valid ctr flags for the raid level of @rs */
static unsigned long __valid_flags(struct raid_set *rs)
{
if (rt_is_raid0(rs->raid_type))
return RAID0_VALID_FLAGS;
else if (rt_is_raid1(rs->raid_type))
return RAID1_VALID_FLAGS;
else if (rt_is_raid10(rs->raid_type))
return RAID10_VALID_FLAGS;
else if (rt_is_raid45(rs->raid_type))
return RAID45_VALID_FLAGS;
else if (rt_is_raid6(rs->raid_type))
return RAID6_VALID_FLAGS;
return 0;
}
/*
* Check for valid flags set on @rs
*
* Has to be called after parsing of the ctr flags!
*/
static int rs_check_for_valid_flags(struct raid_set *rs)
{
if (rs->ctr_flags & ~__valid_flags(rs)) {
rs->ti->error = "Invalid flags combination";
return -EINVAL;
}
return 0;
}
/* MD raid10 bit definitions and helpers */
#define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
#define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
#define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
#define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
/* Return md raid10 near copies for @layout */
static unsigned int __raid10_near_copies(int layout)
{
return layout & 0xFF;
}
/* Return md raid10 far copies for @layout */
static unsigned int __raid10_far_copies(int layout)
{
return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
}
/* Return true if md raid10 offset for @layout */
static bool __is_raid10_offset(int layout)
{
return !!(layout & RAID10_OFFSET);
}
/* Return true if md raid10 near for @layout */
static bool __is_raid10_near(int layout)
{
return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
}
/* Return true if md raid10 far for @layout */
static bool __is_raid10_far(int layout)
{
return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
}
/* Return md raid10 layout string for @layout */
static const char *raid10_md_layout_to_format(int layout)
{
/*
* Bit 16 stands for "offset"
* (i.e. adjacent stripes hold copies)
*
* Refer to MD's raid10.c for details
*/
if (__is_raid10_offset(layout))
return "offset";
if (__raid10_near_copies(layout) > 1)
return "near";
if (__raid10_far_copies(layout) > 1)
return "far";
return "unknown";
}
/* Return md raid10 algorithm for @name */
static const int raid10_name_to_format(const char *name)
{
if (!strcasecmp(name, "near"))
return ALGORITHM_RAID10_NEAR;
else if (!strcasecmp(name, "offset"))
return ALGORITHM_RAID10_OFFSET;
else if (!strcasecmp(name, "far"))
return ALGORITHM_RAID10_FAR;
return -EINVAL;
}
/* Return md raid10 copies for @layout */
static unsigned int raid10_md_layout_to_copies(int layout)
{
return max(__raid10_near_copies(layout), __raid10_far_copies(layout));
}
/* Return md raid10 format id for @format string */
static int raid10_format_to_md_layout(struct raid_set *rs,
unsigned int algorithm,
unsigned int copies)
{
unsigned int n = 1, f = 1, r = 0;
/*
* MD resilienece flaw:
*
* enabling use_far_sets for far/offset formats causes copies
* to be colocated on the same devs together with their origins!
*
* -> disable it for now in the definition above
*/
if (algorithm == ALGORITHM_RAID10_DEFAULT ||
algorithm == ALGORITHM_RAID10_NEAR)
n = copies;
else if (algorithm == ALGORITHM_RAID10_OFFSET) {
f = copies;
r = RAID10_OFFSET;
if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
r |= RAID10_USE_FAR_SETS;
} else if (algorithm == ALGORITHM_RAID10_FAR) {
f = copies;
r = !RAID10_OFFSET;
if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
r |= RAID10_USE_FAR_SETS;
} else
return -EINVAL;
return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
}
/* END: MD raid10 bit definitions and helpers */
/* Check for any of the raid10 algorithms */
static bool __got_raid10(struct raid_type *rtp, const int layout)
{
if (rtp->level == 10) {
switch (rtp->algorithm) {
case ALGORITHM_RAID10_DEFAULT:
case ALGORITHM_RAID10_NEAR:
return __is_raid10_near(layout);
case ALGORITHM_RAID10_OFFSET:
return __is_raid10_offset(layout);
case ALGORITHM_RAID10_FAR:
return __is_raid10_far(layout);
default:
break;
}
}
return false;
}
/* Return raid_type for @name */
static struct raid_type *get_raid_type(const char *name)
{
struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
while (rtp-- > raid_types)
if (!strcasecmp(rtp->name, name))
return rtp;
return NULL;
}
/* Return raid_type for @name based derived from @level and @layout */
static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
{
struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
while (rtp-- > raid_types) {
/* RAID10 special checks based on @layout flags/properties */
if (rtp->level == level &&
(__got_raid10(rtp, layout) || rtp->algorithm == layout))
return rtp;
}
return NULL;
}
/* Adjust rdev sectors */
static void rs_set_rdev_sectors(struct raid_set *rs)
{
struct mddev *mddev = &rs->md;
struct md_rdev *rdev;
/*
* raid10 sets rdev->sector to the device size, which
* is unintended in case of out-of-place reshaping
*/
rdev_for_each(rdev, mddev)
if (!test_bit(Journal, &rdev->flags))
rdev->sectors = mddev->dev_sectors;
}
/*
* Change bdev capacity of @rs in case of a disk add/remove reshape
*/
static void rs_set_capacity(struct raid_set *rs)
{
struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));
set_capacity(gendisk, rs->md.array_sectors);
revalidate_disk(gendisk);
}
/*
* Set the mddev properties in @rs to the current
* ones retrieved from the freshest superblock
*/
static void rs_set_cur(struct raid_set *rs)
{
struct mddev *mddev = &rs->md;
mddev->new_level = mddev->level;
mddev->new_layout = mddev->layout;
mddev->new_chunk_sectors = mddev->chunk_sectors;
}
/*
* Set the mddev properties in @rs to the new
* ones requested by the ctr
*/
static void rs_set_new(struct raid_set *rs)
{
struct mddev *mddev = &rs->md;
mddev->level = mddev->new_level;
mddev->layout = mddev->new_layout;
mddev->chunk_sectors = mddev->new_chunk_sectors;
mddev->raid_disks = rs->raid_disks;
mddev->delta_disks = 0;
}
static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
unsigned int raid_devs)
{
unsigned int i;
struct raid_set *rs;
if (raid_devs <= raid_type->parity_devs) {
ti->error = "Insufficient number of devices";
return ERR_PTR(-EINVAL);
}
rs = kzalloc(struct_size(rs, dev, raid_devs), GFP_KERNEL);
if (!rs) {
ti->error = "Cannot allocate raid context";
return ERR_PTR(-ENOMEM);
}
mddev_init(&rs->md);
INIT_LIST_HEAD(&rs->list);
rs->raid_disks = raid_devs;
rs->delta_disks = 0;
rs->ti = ti;
rs->raid_type = raid_type;
rs->stripe_cache_entries = 256;
rs->md.raid_disks = raid_devs;
rs->md.level = raid_type->level;
rs->md.new_level = rs->md.level;
rs->md.layout = raid_type->algorithm;
rs->md.new_layout = rs->md.layout;
rs->md.delta_disks = 0;
rs->md.recovery_cp = MaxSector;
for (i = 0; i < raid_devs; i++)
md_rdev_init(&rs->dev[i].rdev);
/* Add @rs to global list. */
list_add(&rs->list, &raid_sets);
/*
* Remaining items to be initialized by further RAID params:
* rs->md.persistent
* rs->md.external
* rs->md.chunk_sectors
* rs->md.new_chunk_sectors
* rs->md.dev_sectors
*/
return rs;
}
/* Free all @rs allocations and remove it from global list. */
static void raid_set_free(struct raid_set *rs)
{
int i;
if (rs->journal_dev.dev) {
md_rdev_clear(&rs->journal_dev.rdev);
dm_put_device(rs->ti, rs->journal_dev.dev);
}
for (i = 0; i < rs->raid_disks; i++) {
if (rs->dev[i].meta_dev)
dm_put_device(rs->ti, rs->dev[i].meta_dev);
md_rdev_clear(&rs->dev[i].rdev);
if (rs->dev[i].data_dev)
dm_put_device(rs->ti, rs->dev[i].data_dev);
}
list_del(&rs->list);
kfree(rs);
}
/*
* For every device we have two words
* <meta_dev>: meta device name or '-' if missing
* <data_dev>: data device name or '-' if missing
*
* The following are permitted:
* - -
* - <data_dev>
* <meta_dev> <data_dev>
*
* The following is not allowed:
* <meta_dev> -
*
* This code parses those words. If there is a failure,
* the caller must use raid_set_free() to unwind the operations.
*/
static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
{
int i;
int rebuild = 0;
int metadata_available = 0;
int r = 0;
const char *arg;
/* Put off the number of raid devices argument to get to dev pairs */
arg = dm_shift_arg(as);
if (!arg)
return -EINVAL;
for (i = 0; i < rs->raid_disks; i++) {
rs->dev[i].rdev.raid_disk = i;
rs->dev[i].meta_dev = NULL;
rs->dev[i].data_dev = NULL;
/*
* There are no offsets initially.
* Out of place reshape will set them accordingly.
*/
rs->dev[i].rdev.data_offset = 0;
rs->dev[i].rdev.new_data_offset = 0;
rs->dev[i].rdev.mddev = &rs->md;
arg = dm_shift_arg(as);
if (!arg)
return -EINVAL;
if (strcmp(arg, "-")) {
r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
&rs->dev[i].meta_dev);
if (r) {
rs->ti->error = "RAID metadata device lookup failure";
return r;
}
rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
if (!rs->dev[i].rdev.sb_page) {
rs->ti->error = "Failed to allocate superblock page";
return -ENOMEM;
}
}
arg = dm_shift_arg(as);
if (!arg)
return -EINVAL;
if (!strcmp(arg, "-")) {
if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
(!rs->dev[i].rdev.recovery_offset)) {
rs->ti->error = "Drive designated for rebuild not specified";
return -EINVAL;
}
if (rs->dev[i].meta_dev) {
rs->ti->error = "No data device supplied with metadata device";
return -EINVAL;
}
continue;
}
r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
&rs->dev[i].data_dev);
if (r) {
rs->ti->error = "RAID device lookup failure";
return r;
}
if (rs->dev[i].meta_dev) {
metadata_available = 1;
rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
}
rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
rebuild++;
}
if (rs->journal_dev.dev)
list_add_tail(&rs->journal_dev.rdev.same_set, &rs->md.disks);
if (metadata_available) {
rs->md.external = 0;
rs->md.persistent = 1;
rs->md.major_version = 2;
} else if (rebuild && !rs->md.recovery_cp) {
/*
* Without metadata, we will not be able to tell if the array
* is in-sync or not - we must assume it is not. Therefore,
* it is impossible to rebuild a drive.
*
* Even if there is metadata, the on-disk information may
* indicate that the array is not in-sync and it will then
* fail at that time.
*
* User could specify 'nosync' option if desperate.
*/
rs->ti->error = "Unable to rebuild drive while array is not in-sync";
return -EINVAL;
}
return 0;
}
/*
* validate_region_size
* @rs
* @region_size: region size in sectors. If 0, pick a size (4MiB default).
*
* Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
* Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
*
* Returns: 0 on success, -EINVAL on failure.
*/
static int validate_region_size(struct raid_set *rs, unsigned long region_size)
{
unsigned long min_region_size = rs->ti->len / (1 << 21);
if (rs_is_raid0(rs))
return 0;
if (!region_size) {
/*
* Choose a reasonable default. All figures in sectors.
*/
if (min_region_size > (1 << 13)) {
/* If not a power of 2, make it the next power of 2 */
region_size = roundup_pow_of_two(min_region_size);
DMINFO("Choosing default region size of %lu sectors",
region_size);
} else {
DMINFO("Choosing default region size of 4MiB");
region_size = 1 << 13; /* sectors */
}
} else {
/*
* Validate user-supplied value.
*/
if (region_size > rs->ti->len) {
rs->ti->error = "Supplied region size is too large";
return -EINVAL;
}
if (region_size < min_region_size) {
DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
region_size, min_region_size);
rs->ti->error = "Supplied region size is too small";
return -EINVAL;
}
if (!is_power_of_2(region_size)) {
rs->ti->error = "Region size is not a power of 2";
return -EINVAL;
}
if (region_size < rs->md.chunk_sectors) {
rs->ti->error = "Region size is smaller than the chunk size";
return -EINVAL;