forked from md-raid-utilities/mdadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grow.c
5297 lines (4896 loc) · 146 KB
/
Grow.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
/*
* mdadm - manage Linux "md" devices aka RAID arrays.
*
* Copyright (C) 2001-2013 Neil Brown <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Neil Brown
* Email: <[email protected]>
*/
#include "mdadm.h"
#include "dlink.h"
#include <sys/mman.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/wait.h>
#if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
#error no endian defined
#endif
#include "md_u.h"
#include "md_p.h"
int restore_backup(struct supertype *st,
struct mdinfo *content,
int working_disks,
int next_spare,
char **backup_filep,
int verbose)
{
int i;
int *fdlist;
struct mdinfo *dev;
int err;
int disk_count = next_spare + working_disks;
char *backup_file = *backup_filep;
dprintf("Called restore_backup()\n");
fdlist = xmalloc(sizeof(int) * disk_count);
enable_fds(next_spare);
for (i = 0; i < next_spare; i++)
fdlist[i] = -1;
for (dev = content->devs; dev; dev = dev->next) {
char buf[22];
int fd;
sprintf(buf, "%d:%d", dev->disk.major, dev->disk.minor);
fd = dev_open(buf, O_RDWR);
if (dev->disk.raid_disk >= 0)
fdlist[dev->disk.raid_disk] = fd;
else
fdlist[next_spare++] = fd;
}
if (!backup_file) {
backup_file = locate_backup(content->sys_name);
*backup_filep = backup_file;
}
if (st->ss->external && st->ss->recover_backup)
err = st->ss->recover_backup(st, content);
else
err = Grow_restart(st, content, fdlist, next_spare,
backup_file, verbose > 0);
while (next_spare > 0) {
next_spare--;
if (fdlist[next_spare] >= 0)
close(fdlist[next_spare]);
}
free(fdlist);
if (err) {
pr_err("Failed to restore critical section for reshape - sorry.\n");
if (!backup_file)
pr_err("Possibly you need to specify a --backup-file\n");
return 1;
}
dprintf("restore_backup() returns status OK.\n");
return 0;
}
int Grow_Add_device(char *devname, int fd, char *newdev)
{
/* Add a device to an active array.
* Currently, just extend a linear array.
* This requires writing a new superblock on the
* new device, calling the kernel to add the device,
* and if that succeeds, update the superblock on
* all other devices.
* This means that we need to *find* all other devices.
*/
struct mdinfo info;
dev_t rdev;
int nfd, fd2;
int d, nd;
struct supertype *st = NULL;
char *subarray = NULL;
if (md_get_array_info(fd, &info.array) < 0) {
pr_err("cannot get array info for %s\n", devname);
return 1;
}
if (info.array.level != -1) {
pr_err("can only add devices to linear arrays\n");
return 1;
}
st = super_by_fd(fd, &subarray);
if (!st) {
pr_err("cannot handle arrays with superblock version %d\n",
info.array.major_version);
return 1;
}
if (subarray) {
pr_err("Cannot grow linear sub-arrays yet\n");
free(subarray);
free(st);
return 1;
}
nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
if (nfd < 0) {
pr_err("cannot open %s\n", newdev);
free(st);
return 1;
}
if (!fstat_is_blkdev(nfd, newdev, &rdev)) {
close(nfd);
free(st);
return 1;
}
/* now check out all the devices and make sure we can read the
* superblock */
for (d=0 ; d < info.array.raid_disks ; d++) {
mdu_disk_info_t disk;
char *dv;
st->ss->free_super(st);
disk.number = d;
if (md_get_disk_info(fd, &disk) < 0) {
pr_err("cannot get device detail for device %d\n", d);
close(nfd);
free(st);
return 1;
}
dv = map_dev(disk.major, disk.minor, 1);
if (!dv) {
pr_err("cannot find device file for device %d\n", d);
close(nfd);
free(st);
return 1;
}
fd2 = dev_open(dv, O_RDWR);
if (fd2 < 0) {
pr_err("cannot open device file %s\n", dv);
close(nfd);
free(st);
return 1;
}
if (st->ss->load_super(st, fd2, NULL)) {
pr_err("cannot find super block on %s\n", dv);
close(nfd);
close(fd2);
free(st);
return 1;
}
close(fd2);
}
/* Ok, looks good. Lets update the superblock and write it out to
* newdev.
*/
info.disk.number = d;
info.disk.major = major(rdev);
info.disk.minor = minor(rdev);
info.disk.raid_disk = d;
info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
if (st->ss->update_super(st, &info, UOPT_SPEC_LINEAR_GROW_NEW, newdev,
0, 0, NULL) != 0) {
pr_err("Preparing new metadata failed on %s\n", newdev);
close(nfd);
return 1;
}
if (st->ss->store_super(st, nfd)) {
pr_err("Cannot store new superblock on %s\n", newdev);
close(nfd);
return 1;
}
close(nfd);
if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
pr_err("Cannot add new disk to this array\n");
return 1;
}
/* Well, that seems to have worked.
* Now go through and update all superblocks
*/
if (md_get_array_info(fd, &info.array) < 0) {
pr_err("cannot get array info for %s\n", devname);
return 1;
}
nd = d;
for (d=0 ; d < info.array.raid_disks ; d++) {
mdu_disk_info_t disk;
char *dv;
disk.number = d;
if (md_get_disk_info(fd, &disk) < 0) {
pr_err("cannot get device detail for device %d\n", d);
return 1;
}
dv = map_dev(disk.major, disk.minor, 1);
if (!dv) {
pr_err("cannot find device file for device %d\n", d);
return 1;
}
fd2 = dev_open(dv, O_RDWR);
if (fd2 < 0) {
pr_err("cannot open device file %s\n", dv);
return 1;
}
if (st->ss->load_super(st, fd2, NULL)) {
pr_err("cannot find super block on %s\n", dv);
close(fd);
close(fd2);
return 1;
}
info.array.raid_disks = nd+1;
info.array.nr_disks = nd+1;
info.array.active_disks = nd+1;
info.array.working_disks = nd+1;
if (st->ss->update_super(st, &info, UOPT_SPEC_LINEAR_GROW_UPDATE, dv,
0, 0, NULL) != 0) {
pr_err("Updating metadata failed on %s\n", dv);
close(fd2);
return 1;
}
if (st->ss->store_super(st, fd2)) {
pr_err("Cannot store new superblock on %s\n", dv);
close(fd2);
return 1;
}
close(fd2);
}
return 0;
}
int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
{
/*
* First check that array doesn't have a bitmap
* Then create the bitmap
* Then add it
*
* For internal bitmaps, we need to check the version,
* find all the active devices, and write the bitmap block
* to all devices
*/
mdu_bitmap_file_t bmf;
mdu_array_info_t array;
struct supertype *st;
char *subarray = NULL;
int major = BITMAP_MAJOR_HI;
unsigned long long bitmapsize, array_size;
struct mdinfo *mdi;
/*
* We only ever get called if s->bitmap_file is != NULL, so this check
* is just here to quiet down static code checkers.
*/
if (!s->bitmap_file)
return 1;
if (strcmp(s->bitmap_file, "clustered") == 0)
major = BITMAP_MAJOR_CLUSTERED;
if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
if (errno == ENOMEM)
pr_err("Memory allocation failure.\n");
else
pr_err("bitmaps not supported by this kernel.\n");
return 1;
}
if (bmf.pathname[0]) {
if (str_is_none(s->bitmap_file) == true) {
if (ioctl(fd, SET_BITMAP_FILE, -1) != 0) {
pr_err("failed to remove bitmap %s\n",
bmf.pathname);
return 1;
}
return 0;
}
pr_err("%s already has a bitmap (%s)\n", devname, bmf.pathname);
return 1;
}
if (md_get_array_info(fd, &array) != 0) {
pr_err("cannot get array status for %s\n", devname);
return 1;
}
if (array.state & (1 << MD_SB_BITMAP_PRESENT)) {
if (str_is_none(s->bitmap_file) == true) {
array.state &= ~(1 << MD_SB_BITMAP_PRESENT);
if (md_set_array_info(fd, &array) != 0) {
if (array.state & (1 << MD_SB_CLUSTERED))
pr_err("failed to remove clustered bitmap.\n");
else
pr_err("failed to remove internal bitmap.\n");
return 1;
}
return 0;
}
pr_err("bitmap already present on %s\n", devname);
return 1;
}
if (str_is_none(s->bitmap_file) == true) {
pr_err("no bitmap found on %s\n", devname);
return 1;
}
if (array.level <= 0) {
pr_err("Bitmaps not meaningful with level %s\n",
map_num(pers, array.level)?:"of this array");
return 1;
}
bitmapsize = array.size;
bitmapsize <<= 1;
if (get_dev_size(fd, NULL, &array_size) &&
array_size > (0x7fffffffULL << 9)) {
/* Array is big enough that we cannot trust array.size
* try other approaches
*/
bitmapsize = get_component_size(fd);
}
if (bitmapsize == 0) {
pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
return 1;
}
if (array.level == 10) {
int ncopies;
ncopies = (array.layout & 255) * ((array.layout >> 8) & 255);
bitmapsize = bitmapsize * array.raid_disks / ncopies;
if (strcmp(s->bitmap_file, "clustered") == 0 &&
!is_near_layout_10(array.layout)) {
pr_err("only near layout is supported with clustered raid10\n");
return 1;
}
}
st = super_by_fd(fd, &subarray);
if (!st) {
pr_err("Cannot understand version %d.%d\n",
array.major_version, array.minor_version);
return 1;
}
if (subarray) {
pr_err("Cannot add bitmaps to sub-arrays yet\n");
free(subarray);
free(st);
return 1;
}
mdi = sysfs_read(fd, NULL, GET_CONSISTENCY_POLICY);
if (mdi) {
if (mdi->consistency_policy == CONSISTENCY_POLICY_PPL) {
pr_err("Cannot add bitmap to array with PPL\n");
free(mdi);
free(st);
return 1;
}
free(mdi);
}
if (strcmp(s->bitmap_file, "internal") == 0 ||
strcmp(s->bitmap_file, "clustered") == 0) {
int rv;
int d;
int offset_setable = 0;
if (st->ss->add_internal_bitmap == NULL) {
pr_err("Internal bitmaps not supported with %s metadata\n", st->ss->name);
return 1;
}
st->nodes = c->nodes;
st->cluster_name = c->homecluster;
mdi = sysfs_read(fd, NULL, GET_BITMAP_LOCATION);
if (mdi)
offset_setable = 1;
for (d = 0; d < st->max_devs; d++) {
mdu_disk_info_t disk;
char *dv;
int fd2;
disk.number = d;
if (md_get_disk_info(fd, &disk) < 0)
continue;
if (disk.major == 0 && disk.minor == 0)
continue;
if ((disk.state & (1 << MD_DISK_SYNC)) == 0)
continue;
dv = map_dev(disk.major, disk.minor, 1);
if (!dv)
continue;
if ((disk.state & (1 << MD_DISK_WRITEMOSTLY)) &&
(strcmp(s->bitmap_file, "clustered") == 0)) {
pr_err("%s disks marked write-mostly are not supported with clustered bitmap\n",devname);
free(mdi);
return 1;
}
fd2 = dev_open(dv, O_RDWR);
if (fd2 < 0)
continue;
rv = st->ss->load_super(st, fd2, NULL);
if (!rv) {
rv = st->ss->add_internal_bitmap(
st, &s->bitmap_chunk, c->delay,
s->write_behind, bitmapsize,
offset_setable, major);
if (!rv) {
st->ss->write_bitmap(st, fd2,
NodeNumUpdate);
} else {
pr_err("failed to create internal bitmap - chunksize problem.\n");
}
} else {
pr_err("failed to load super-block.\n");
}
close(fd2);
if (rv) {
free(mdi);
return 1;
}
}
if (offset_setable) {
st->ss->getinfo_super(st, mdi, NULL);
if (sysfs_init(mdi, fd, NULL)) {
pr_err("failed to initialize sysfs.\n");
free(mdi);
}
rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
mdi->bitmap_offset);
free(mdi);
} else {
if (strcmp(s->bitmap_file, "clustered") == 0)
array.state |= (1 << MD_SB_CLUSTERED);
array.state |= (1 << MD_SB_BITMAP_PRESENT);
rv = md_set_array_info(fd, &array);
}
if (rv < 0) {
if (errno == EBUSY)
pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
pr_err("failed to set internal bitmap.\n");
return 1;
}
} else {
int uuid[4];
int bitmap_fd;
int d;
int max_devs = st->max_devs;
/* try to load a superblock */
for (d = 0; d < max_devs; d++) {
mdu_disk_info_t disk;
char *dv;
int fd2;
disk.number = d;
if (md_get_disk_info(fd, &disk) < 0)
continue;
if ((disk.major==0 && disk.minor == 0) ||
(disk.state & (1 << MD_DISK_REMOVED)))
continue;
dv = map_dev(disk.major, disk.minor, 1);
if (!dv)
continue;
fd2 = dev_open(dv, O_RDONLY);
if (fd2 >= 0) {
if (st->ss->load_super(st, fd2, NULL) == 0) {
close(fd2);
st->ss->uuid_from_super(st, uuid);
break;
}
close(fd2);
}
}
if (d == max_devs) {
pr_err("cannot find UUID for array!\n");
return 1;
}
if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid,
s->bitmap_chunk, c->delay, s->write_behind,
bitmapsize, major)) {
return 1;
}
bitmap_fd = open(s->bitmap_file, O_RDWR);
if (bitmap_fd < 0) {
pr_err("weird: %s cannot be opened\n", s->bitmap_file);
return 1;
}
if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
int err = errno;
if (errno == EBUSY)
pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
pr_err("Cannot set bitmap file for %s: %s\n",
devname, strerror(err));
close_fd(&bitmap_fd);
return 1;
}
close_fd(&bitmap_fd);
}
return 0;
}
int Grow_consistency_policy(char *devname, int fd, struct context *c, struct shape *s)
{
struct supertype *st;
struct mdinfo *sra;
struct mdinfo *sd;
char *subarray = NULL;
int ret = 0;
char container_dev[PATH_MAX];
char buf[SYSFS_MAX_BUF_SIZE];
if (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
s->consistency_policy != CONSISTENCY_POLICY_PPL) {
pr_err("Operation not supported for consistency policy %s\n",
map_num_s(consistency_policies, s->consistency_policy));
return 1;
}
st = super_by_fd(fd, &subarray);
if (!st)
return 1;
sra = sysfs_read(fd, NULL, GET_CONSISTENCY_POLICY|GET_LEVEL|
GET_DEVS|GET_STATE);
if (!sra) {
ret = 1;
goto free_st;
}
if (s->consistency_policy == CONSISTENCY_POLICY_PPL &&
!st->ss->write_init_ppl) {
pr_err("%s metadata does not support PPL\n", st->ss->name);
ret = 1;
goto free_info;
}
if (sra->array.level != 5) {
pr_err("Operation not supported for array level %d\n",
sra->array.level);
ret = 1;
goto free_info;
}
if (sra->consistency_policy == (unsigned)s->consistency_policy) {
pr_err("Consistency policy is already %s\n",
map_num_s(consistency_policies, s->consistency_policy));
ret = 1;
goto free_info;
} else if (sra->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
sra->consistency_policy != CONSISTENCY_POLICY_PPL) {
pr_err("Current consistency policy is %s, cannot change to %s\n",
map_num_s(consistency_policies, sra->consistency_policy),
map_num_s(consistency_policies, s->consistency_policy));
ret = 1;
goto free_info;
}
if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
if (sysfs_get_str(sra, NULL, "sync_action", buf, sizeof(buf)) <= 0) {
ret = 1;
goto free_info;
} else if (strcmp(buf, "reshape\n") == 0) {
pr_err("PPL cannot be enabled when reshape is in progress\n");
ret = 1;
goto free_info;
}
}
if (subarray) {
enum update_opt update;
if (s->consistency_policy == CONSISTENCY_POLICY_PPL)
update = UOPT_PPL;
else
update = UOPT_NO_PPL;
sprintf(container_dev, "/dev/%s", st->container_devnm);
ret = Update_subarray(container_dev, subarray, update, NULL,
c->verbose);
if (ret)
goto free_info;
}
if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
struct mdinfo info;
if (subarray) {
struct mdinfo *mdi;
int cfd;
cfd = open(container_dev, O_RDWR|O_EXCL);
if (cfd < 0) {
pr_err("Failed to open %s\n", container_dev);
ret = 1;
goto free_info;
}
ret = st->ss->load_container(st, cfd, st->container_devnm);
close(cfd);
if (ret) {
pr_err("Cannot read superblock for %s\n",
container_dev);
goto free_info;
}
mdi = st->ss->container_content(st, subarray);
info = *mdi;
free(mdi);
}
for (sd = sra->devs; sd; sd = sd->next) {
int dfd;
char *devpath;
devpath = map_dev(sd->disk.major, sd->disk.minor, 0);
dfd = dev_open(devpath, O_RDWR);
if (dfd < 0) {
pr_err("Failed to open %s\n", devpath);
ret = 1;
goto free_info;
}
if (!subarray) {
ret = st->ss->load_super(st, dfd, NULL);
if (ret) {
pr_err("Failed to load super-block.\n");
close(dfd);
goto free_info;
}
ret = st->ss->update_super(st, sra, UOPT_PPL,
devname,
c->verbose, 0, NULL);
if (ret) {
close(dfd);
st->ss->free_super(st);
goto free_info;
}
st->ss->getinfo_super(st, &info, NULL);
}
ret |= sysfs_set_num(sra, sd, "ppl_sector",
info.ppl_sector);
ret |= sysfs_set_num(sra, sd, "ppl_size",
info.ppl_size);
if (ret) {
pr_err("Failed to set PPL attributes for %s\n",
sd->sys_name);
close(dfd);
st->ss->free_super(st);
goto free_info;
}
ret = st->ss->write_init_ppl(st, &info, dfd);
if (ret)
pr_err("Failed to write PPL\n");
close(dfd);
if (!subarray)
st->ss->free_super(st);
if (ret)
goto free_info;
}
}
ret = sysfs_set_str(sra, NULL, "consistency_policy",
map_num_s(consistency_policies,
s->consistency_policy));
if (ret)
pr_err("Failed to change array consistency policy\n");
free_info:
sysfs_free(sra);
free_st:
free(st);
free(subarray);
return ret;
}
/*
* When reshaping an array we might need to backup some data.
* This is written to all spares with a 'super_block' describing it.
* The superblock goes 4K from the end of the used space on the
* device.
* It if written after the backup is complete.
* It has the following structure.
*/
static struct mdp_backup_super {
char magic[16]; /* md_backup_data-1 or -2 */
__u8 set_uuid[16];
__u64 mtime;
/* start/sizes in 512byte sectors */
__u64 devstart; /* address on backup device/file of data */
__u64 arraystart;
__u64 length;
__u32 sb_csum; /* csum of preceeding bytes. */
__u32 pad1;
__u64 devstart2; /* offset in to data of second section */
__u64 arraystart2;
__u64 length2;
__u32 sb_csum2; /* csum of preceeding bytes. */
__u8 pad[512-68-32];
} __attribute__((aligned(512))) bsb, bsb2;
static __u32 bsb_csum(char *buf, int len)
{
int i;
int csum = 0;
for (i = 0; i < len; i++)
csum = (csum<<3) + buf[0];
return __cpu_to_le32(csum);
}
static int check_idle(struct supertype *st)
{
/* Check that all member arrays for this container, or the
* container of this array, are idle
*/
char *container = (st->container_devnm[0]
? st->container_devnm : st->devnm);
struct mdstat_ent *ent, *e;
int is_idle = 1;
ent = mdstat_read(0, 0);
for (e = ent ; e; e = e->next) {
if (!is_container_member(e, container))
continue;
/* frozen array is not idle*/
if (e->percent >= 0 || e->metadata_version[9] == '-') {
is_idle = 0;
break;
}
}
free_mdstat(ent);
return is_idle;
}
static int freeze_container(struct supertype *st)
{
char *container = (st->container_devnm[0]
? st->container_devnm : st->devnm);
if (!check_idle(st))
return -1;
if (block_monitor(container, 1)) {
pr_err("failed to freeze container\n");
return -2;
}
return 1;
}
static void unfreeze_container(struct supertype *st)
{
char *container = (st->container_devnm[0]
? st->container_devnm : st->devnm);
unblock_monitor(container, 1);
}
static int freeze(struct supertype *st)
{
/* Try to freeze resync/rebuild on this array/container.
* Return -1 if the array is busy,
* return -2 container cannot be frozen,
* return 0 if this kernel doesn't support 'frozen'
* return 1 if it worked.
*/
if (st->ss->external)
return freeze_container(st);
else {
struct mdinfo *sra = sysfs_read(-1, st->devnm, GET_VERSION);
int err;
char buf[SYSFS_MAX_BUF_SIZE];
if (!sra)
return -1;
/* Need to clear any 'read-auto' status */
if (sysfs_get_str(sra, NULL, "array_state", buf, sizeof(buf)) > 0 &&
strncmp(buf, "read-auto", 9) == 0)
sysfs_set_str(sra, NULL, "array_state", "clean");
err = sysfs_freeze_array(sra);
sysfs_free(sra);
return err;
}
}
static void unfreeze(struct supertype *st)
{
if (st->ss->external)
return unfreeze_container(st);
else {
struct mdinfo *sra = sysfs_read(-1, st->devnm, GET_VERSION);
char buf[SYSFS_MAX_BUF_SIZE];
if (sra &&
sysfs_get_str(sra, NULL, "sync_action", buf, sizeof(buf)) > 0 &&
strcmp(buf, "frozen\n") == 0)
sysfs_set_str(sra, NULL, "sync_action", "idle");
sysfs_free(sra);
}
}
static void wait_reshape(struct mdinfo *sra)
{
int fd = sysfs_get_fd(sra, NULL, "sync_action");
char action[SYSFS_MAX_BUF_SIZE];
if (fd < 0)
return;
while (sysfs_fd_get_str(fd, action, sizeof(action)) > 0 &&
strncmp(action, "reshape", 7) == 0)
sysfs_wait(fd, NULL);
close(fd);
}
static int reshape_super(struct supertype *st, struct shape *shape, struct context *c)
{
/* nothing extra to check in the native case */
if (!st->ss->external)
return 0;
if (!st->ss->reshape_super || !st->ss->manage_reshape) {
pr_err("%s metadata does not support reshape\n",
st->ss->name);
return 1;
}
return st->ss->reshape_super(st, shape, c);
}
/**
* reshape_super_size() - Reshape array, size only.
*
* @st: supertype.
* @devname: device name.
* @size: component size.
* @dir metadata changes direction
* Returns: 0 on success, 1 otherwise.
*
* This function is solely used to change size of the volume.
* Setting size is not valid for container.
* Size is only change that can be rolled back, thus the @dir param.
*/
static int reshape_super_size(struct supertype *st, char *devname,
unsigned long long size, change_dir_t direction,
struct context *c)
{
struct shape shape = {0};
shape.level = UnSet;
shape.layout = UnSet;
shape.delta_disks = UnSet;
shape.dev = devname;
shape.size = size;
shape.direction = direction;
return reshape_super(st, &shape, c);
}
/**
* reshape_super_non_size() - Reshape array, non size changes.
*
* @st: supertype.
* @devname: device name.
* @info: superblock info.
* Returns: 0 on success, 1 otherwise.
*
* This function is used for any external array changes but size.
* It handles both volumes and containers.
* For changes other than size, rollback is not possible.
*/
static int reshape_super_non_size(struct supertype *st, char *devname,
struct mdinfo *info, struct context *c)
{
struct shape shape = {0};
/* Size already set to zero, not updating size */
shape.level = info->new_level;
shape.layout = info->new_layout;
shape.chunk = info->new_chunk;
shape.raiddisks = info->array.raid_disks;
shape.delta_disks = info->delta_disks;
shape.dev = devname;
/* Rollback not possible for non size changes */
shape.direction = APPLY_METADATA_CHANGES;
return reshape_super(st, &shape, c);
}
static void sync_metadata(struct supertype *st)
{
if (st->ss->external) {
if (st->update_tail) {
flush_metadata_updates(st);
st->update_tail = &st->updates;
} else
st->ss->sync_metadata(st);
}
}
static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n)
{
/* when dealing with external metadata subarrays we need to be
* prepared to handle EAGAIN. The kernel may need to wait for
* mdmon to mark the array active so the kernel can handle
* allocations/writeback when preparing the reshape action
* (md_allow_write()). We temporarily disable safe_mode_delay
* to close a race with the array_state going clean before the
* next write to raid_disks / stripe_cache_size
*/
char safe[SYSFS_MAX_BUF_SIZE];
int rc;
/* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
if (!container ||
(strcmp(name, "raid_disks") != 0 &&
strcmp(name, "stripe_cache_size") != 0))
return sysfs_set_num(sra, NULL, name, n);
rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe));
if (rc <= 0)
return -1;
sysfs_set_num(sra, NULL, "safe_mode_delay", 0);
rc = sysfs_set_num(sra, NULL, name, n);
if (rc < 0 && errno == EAGAIN) {
ping_monitor(container);
/* if we get EAGAIN here then the monitor is not active
* so stop trying
*/
rc = sysfs_set_num(sra, NULL, name, n);
}
sysfs_set_str(sra, NULL, "safe_mode_delay", safe);
return rc;
}
int start_reshape(struct mdinfo *sra, int already_running,
int before_data_disks, int data_disks, struct supertype *st)
{
int err;
unsigned long long sync_max_to_set;
sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
err = sysfs_set_num(sra, NULL, "suspend_hi", sra->reshape_progress);
err = err ?: sysfs_set_num(sra, NULL, "suspend_lo",
sra->reshape_progress);
if (before_data_disks <= data_disks)
sync_max_to_set = sra->reshape_progress / data_disks;
else
sync_max_to_set = (sra->component_size * data_disks
- sra->reshape_progress) / data_disks;