summaryrefslogtreecommitdiff
path: root/Grow.c
blob: 0e20b240df61cf211e3fd3263cfc3de2c87e0f78 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
/*
 * mdadm - manage Linux "md" devices aka RAID arrays.
 *
 * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
 *
 *
 *    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: <neilb@suse.de>
 */
#include	"mdadm.h"
#include	"dlink.h"
#include	<sys/mman.h>
#include	<stdint.h>

#if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
#error no endian defined
#endif
#include	"md_u.h"
#include	"md_p.h"

#ifndef offsetof
#define offsetof(t,f) ((size_t)&(((t*)0)->f))
#endif

int restore_backup(struct supertype *st,
		   struct mdinfo *content,
		   int working_disks,
		   int next_spare,
		   char *backup_file,
		   int verbose)
{
	int i;
	int *fdlist;
	struct mdinfo *dev;
	int err;
	int disk_count = next_spare + working_disks;

	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 (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;

	struct stat stb;
	int nfd, fd2;
	int d, nd;
	struct supertype *st = NULL;
	char *subarray = NULL;

	if (ioctl(fd, GET_ARRAY_INFO, &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;
	}
	fstat(nfd, &stb);
	if ((stb.st_mode & S_IFMT) != S_IFBLK) {
		pr_err("%s is not a block device!\n", newdev);
		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 (ioctl(fd, GET_DISK_INFO, &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(stb.st_rdev);
	info.disk.minor = minor(stb.st_rdev);
	info.disk.raid_disk = d;
	info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
	st->ss->update_super(st, &info, "linear-grow-new", newdev,
			     0, 0, NULL);

	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 (ioctl(fd, GET_ARRAY_INFO, &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 (ioctl(fd, GET_DISK_INFO, &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);
			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;

		st->ss->update_super(st, &info, "linear-grow-update", dv,
				     0, 0, NULL);

		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;
	int vers = md_get_version(fd);
	unsigned long long bitmapsize, array_size;

	if (vers < 9003) {
		major = BITMAP_MAJOR_HOSTENDIAN;
		pr_err("Warning - bitmaps created on this kernel"
			" are not portable\n"
			"  between different architectures.  Consider upgrading"
			" the Linux kernel.\n");
	}

	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 (strcmp(s->bitmap_file,"none")==0) {
			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 (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
		pr_err("cannot get array status for %s\n", devname);
		return 1;
	}
	if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
		if (strcmp(s->bitmap_file, "none")==0) {
			array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
			if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
				pr_err("failed to remove internal bitmap.\n");
				return 1;
			}
			return 0;
		}
		pr_err("Internal bitmap already present on %s\n",
			devname);
		return 1;
	}

	if (strcmp(s->bitmap_file, "none") == 0) {
		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 = (array.layout&255)*((array.layout>>8)&255);
		bitmapsize = bitmapsize * array.raid_disks / ncopies;
	}

	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;
	}
	if (strcmp(s->bitmap_file, "internal") == 0) {
		int rv;
		int d;
		int offset_setable = 0;
		struct mdinfo *mdi;
		if (st->ss->add_internal_bitmap == NULL) {
			pr_err("Internal bitmaps not supported "
				"with %s metadata\n", st->ss->name);
			return 1;
		}
		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;
			disk.number = d;
			if (ioctl(fd, GET_DISK_INFO, &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) {
				int fd2 = dev_open(dv, O_RDWR);
				if (fd2 < 0)
					continue;
				if (st->ss->load_super(st, fd2, NULL)==0) {
					if (st->ss->add_internal_bitmap(
						    st,
						    &s->bitmap_chunk, c->delay, s->write_behind,
						    bitmapsize, offset_setable,
						    major)
						)
						st->ss->write_bitmap(st, fd2);
					else {
						pr_err("failed to create internal bitmap"
						       " - chunksize problem.\n");
						close(fd2);
						return 1;
					}
				}
				close(fd2);
			}
		}
		if (offset_setable) {
			st->ss->getinfo_super(st, mdi, NULL);
			sysfs_init(mdi, fd, NULL);
			rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
						  mdi->bitmap_offset);
		} else {
			array.state |= (1<<MD_SB_BITMAP_PRESENT);
			rv = ioctl(fd, SET_ARRAY_INFO, &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 (ioctl(fd, GET_DISK_INFO, &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));
			return 1;
		}
	}

	return 0;
}

/*
 * 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;
		if (e->percent >= 0) {
			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[20];

		if (!sra)
			return -1;
		/* Need to clear any 'read-auto' status */
		if (sysfs_get_str(sra, NULL, "array_state", buf, 20) > 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);

		if (sra)
			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[20];

	if (fd < 0)
		return;

	while (sysfs_fd_get_str(fd, action, 20) > 0 &&
	       strncmp(action, "reshape", 7) == 0)
		sysfs_wait(fd, NULL);
	close(fd);
}

static int reshape_super(struct supertype *st, unsigned long long size,
			 int level, int layout, int chunksize, int raid_disks,
			 int delta_disks, char *backup_file, char *dev,
			 int direction, int verbose)
{
	/* 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, size, level, layout, chunksize,
				     raid_disks, delta_disks, backup_file, dev,
				     direction, verbose);
}

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[50];
	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)
{
	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;
	if (!already_running)
		sysfs_set_num(sra, NULL, "sync_min", sync_max_to_set);
	err = err ?: sysfs_set_num(sra, NULL, "sync_max", sync_max_to_set);
	if (!already_running)
		err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");

	return err;
}

void abort_reshape(struct mdinfo *sra)
{
	sysfs_set_str(sra, NULL, "sync_action", "idle");
	sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
	sysfs_set_num(sra, NULL, "suspend_hi", 0);
	sysfs_set_num(sra, NULL, "suspend_lo", 0);
	sysfs_set_num(sra, NULL, "sync_min", 0);
	sysfs_set_str(sra, NULL, "sync_max", "max");
}

int remove_disks_for_takeover(struct supertype *st,
			      struct mdinfo *sra,
			      int layout)
{
	int nr_of_copies;
	struct mdinfo *remaining;
	int slot;

	if (sra->array.level == 10)
		nr_of_copies = layout & 0xff;
	else if (sra->array.level == 1)
		nr_of_copies = sra->array.raid_disks;
	else
		return 1;

	remaining = sra->devs;
	sra->devs = NULL;
	/* for each 'copy', select one device and remove from the list. */
	for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) {
		struct mdinfo **diskp;
		int found = 0;

		/* Find a working device to keep */
		for (diskp =  &remaining; *diskp ; diskp = &(*diskp)->next) {
			struct mdinfo *disk = *diskp;

			if (disk->disk.raid_disk < slot)
				continue;
			if (disk->disk.raid_disk >= slot + nr_of_copies)
				continue;
			if (disk->disk.state & (1<<MD_DISK_REMOVED))
				continue;
			if (disk->disk.state & (1<<MD_DISK_FAULTY))
				continue;
			if (!(disk->disk.state & (1<<MD_DISK_SYNC)))
				continue;

			/* We have found a good disk to use! */
			*diskp = disk->next;
			disk->next = sra->devs;
			sra->devs = disk;
			found = 1;
			break;
		}
		if (!found)
			break;
	}

	if (slot < sra->array.raid_disks) {
		/* didn't find all slots */
		struct mdinfo **e;
		e = &remaining;
		while (*e)
			e = &(*e)->next;
		*e = sra->devs;
		sra->devs = remaining;
		return 1;
	}

	/* Remove all 'remaining' devices from the array */
	while (remaining) {
		struct mdinfo *sd = remaining;
		remaining = sd->next;

		sysfs_set_str(sra, sd, "state", "faulty");
		sysfs_set_str(sra, sd, "slot", "none");
		/* for external metadata disks should be removed in mdmon */
		if (!st->ss->external)
			sysfs_set_str(sra, sd, "state", "remove");
		sd->disk.state |= (1<<MD_DISK_REMOVED);
		sd->disk.state &= ~(1<<MD_DISK_SYNC);
		sd->next = sra->devs;
		sra->devs = sd;
	}
	return 0;
}

void reshape_free_fdlist(int *fdlist,
			 unsigned long long *offsets,
			 int size)
{
	int i;

	for (i = 0; i < size; i++)
		if (fdlist[i] >= 0)
			close(fdlist[i]);

	free(fdlist);
	free(offsets);
}

int reshape_prepare_fdlist(char *devname,
			   struct mdinfo *sra,
			   int raid_disks,
			   int nrdisks,
			   unsigned long blocks,
			   char *backup_file,
			   int *fdlist,
			   unsigned long long *offsets)
{
	int d = 0;
	struct mdinfo *sd;

	enable_fds(nrdisks);
	for (d = 0; d <= nrdisks; d++)
		fdlist[d] = -1;
	d = raid_disks;
	for (sd = sra->devs; sd; sd = sd->next) {
		if (sd->disk.state & (1<<MD_DISK_FAULTY))
			continue;
		if (sd->disk.state & (1<<MD_DISK_SYNC)) {
			char *dn = map_dev(sd->disk.major,
					   sd->disk.minor, 1);
			fdlist[sd->disk.raid_disk]
				= dev_open(dn, O_RDONLY);
			offsets[sd->disk.raid_disk] = sd->data_offset*512;
			if (fdlist[sd->disk.raid_disk] < 0) {
				pr_err("%s: cannot open component %s\n",
				       devname, dn ? dn : "-unknown-");
				d = -1;
				goto release;
			}
		} else if (backup_file == NULL) {
			/* spare */
			char *dn = map_dev(sd->disk.major,
					   sd->disk.minor, 1);
				fdlist[d] = dev_open(dn, O_RDWR);
				offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512;
				if (fdlist[d] < 0) {
					pr_err("%s: cannot open component %s\n",
						devname, dn ? dn : "-unknown-");
					d = -1;
					goto release;
				}
				d++;
			}
		}
release:
	return d;
}

int reshape_open_backup_file(char *backup_file,
			     int fd,
			     char *devname,
			     long blocks,
			     int *fdlist,
			     unsigned long long *offsets,
			     int restart)
{
	/* Return 1 on success, 0 on any form of failure */
	/* need to check backup file is large enough */
	char buf[512];
	struct stat stb;
	unsigned int dev;
	int i;

	*fdlist = open(backup_file, O_RDWR|O_CREAT|(restart ? O_TRUNC : O_EXCL),
		       S_IRUSR | S_IWUSR);
	*offsets = 8 * 512;
	if (*fdlist < 0) {
		pr_err("%s: cannot create backup file %s: %s\n",
			devname, backup_file, strerror(errno));
		return 0;
	}
	/* Guard against backup file being on array device.
	 * If array is partitioned or if LVM etc is in the
	 * way this will not notice, but it is better than
	 * nothing.
	 */
	fstat(*fdlist, &stb);
	dev = stb.st_dev;
	fstat(fd, &stb);
	if (stb.st_rdev == dev) {
		pr_err("backup file must NOT be"
			" on the array being reshaped.\n");
		close(*fdlist);
		return 0;
	}

	memset(buf, 0, 512);
	for (i=0; i < blocks + 8 ; i++) {
		if (write(*fdlist, buf, 512) != 512) {
			pr_err("%s: cannot create"
				" backup file %s: %s\n",
				devname, backup_file, strerror(errno));
			return 0;
		}
	}
	if (fsync(*fdlist) != 0) {
		pr_err("%s: cannot create backup file %s: %s\n",
			devname, backup_file, strerror(errno));
		return 0;
	}

	return 1;
}

unsigned long GCD(unsigned long a, unsigned long b)
{
	while (a != b) {
		if (a < b)
			b -= a;
		if (b < a)
			a -= b;
	}
	return a;
}

unsigned long compute_backup_blocks(int nchunk, int ochunk,
				    unsigned int ndata, unsigned int odata)
{
	unsigned long a, b, blocks;
	/* So how much do we need to backup.
	 * We need an amount of data which is both a whole number of
	 * old stripes and a whole number of new stripes.
	 * So LCM for (chunksize*datadisks).
	 */
	a = (ochunk/512) * odata;
	b = (nchunk/512) * ndata;
	/* Find GCD */
	a = GCD(a, b);
	/* LCM == product / GCD */
	blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;

	return blocks;
}

char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
{
	/* Based on the current array state in info->array and
	 * the changes in info->new_* etc, determine:
	 *  - whether the change is possible
	 *  - Intermediate level/raid_disks/layout
	 *  - whether a restriping reshape is needed
	 *  - number of sectors in minimum change unit.  This
	 *    will cover a whole number of stripes in 'before' and
	 *    'after'.
	 *
	 * Return message if the change should be rejected
	 *        NULL if the change can be achieved
	 *
	 * This can be called as part of starting a reshape, or
	 * when assembling an array that is undergoing reshape.
	 */
	int near, far, offset, copies;
	int new_disks;
	int old_chunk, new_chunk;
	/* delta_parity records change in number of devices
	 * caused by level change
	 */
	int delta_parity = 0;

	memset(re, 0, sizeof(*re));

	/* If a new level not explicitly given, we assume no-change */
	if (info->new_level == UnSet)
		info->new_level = info->array.level;

	if (info->new_chunk)
		switch (info->new_level) {
		case 0:
		case 4:
		case 5:
		case 6:
		case 10:
			/* chunk size is meaningful, must divide component_size
			 * evenly
			 */
			if (info->component_size % (info->new_chunk/512)) {
				unsigned long long shrink = info->component_size;
				shrink &= ~(unsigned long long)(info->new_chunk/512-1);
				pr_err("New chunk size (%dK) does not evenly divide device size (%lluk)\n",
				       info->new_chunk/1024, info->component_size/2);
				pr_err("After shrinking any filesystem, \"mdadm --grow %s --size %llu\"\n",
				       devname, shrink/2);
				pr_err("will shrink the array so the given chunk size would work.\n");
				return "";
			}
			break;
		default:
			return "chunk size not meaningful for this level";
		}
	else
		info->new_chunk = info->array.chunk_size;

	switch (info->array.level) {
	default:
		return "Cannot understand this RAID level";
	case 1:
		/* RAID1 can convert to RAID1 with different disks, or
		 * raid5 with 2 disks, or
		 * raid0 with 1 disk
		 */
		if (info->new_level > 1 &&
		    (info->component_size & 7))
			return "Cannot convert RAID1 of this size - "
				"reduce size to multiple of 4K first.";
		if (info->new_level == 0) {
			if (info->delta_disks != UnSet &&
			    info->delta_disks != 0)
				return "Cannot change number of disks "
					"with RAID1->RAID0 conversion";
			re->level = 0;
			re->before.data_disks = 1;
			re->after.data_disks = 1;
			return NULL;
		}
		if (info->new_level == 1) {
			if (info->delta_disks == UnSet)
				/* Don't know what to do */
				return "no change requested for Growing RAID1";
			re->level = 1;
			return NULL;
		}
		if (info->array.raid_disks == 2 &&
		    info->new_level == 5) {

			re->level = 5;
			re->before.data_disks = 1;
			if (info->delta_disks != UnSet &&
			    info->delta_disks != 0)
				re->after.data_disks = 1 + info->delta_disks;
			else
				re->after.data_disks = 1;
			if (re->after.data_disks < 1)
				return "Number of disks too small for RAID5";

			re->before.layout = ALGORITHM_LEFT_SYMMETRIC;
			info->array.chunk_size = 65536;
			break;
		}
		/* Could do some multi-stage conversions, but leave that to
		 * later.
		 */
		return "Impossibly level change request for RAID1";

	case 10:
		/* RAID10 can be converted from near mode to
		 * RAID0 by removing some devices.
		 * It can also be reshaped if the kernel supports
		 * new_data_offset.
		 */
		switch (info->new_level) {
		case 0:
			if ((info->array.layout & ~0xff) != 0x100)
				return "Cannot Grow RAID10 with far/offset layout";
			/* number of devices must be multiple of number of copies */
			if (info->array.raid_disks % (info->array.layout & 0xff))
				return "RAID10 layout too complex for Grow operation";

			new_disks = (info->array.raid_disks
				     / (info->array.layout & 0xff));
			if (info->delta_disks == UnSet)
				info->delta_disks = (new_disks
						     - info->array.raid_disks);

			if (info->delta_disks != new_disks - info->array.raid_disks)
				return "New number of raid-devices impossible for RAID10";
			if (info->new_chunk &&
			    info->new_chunk != info->array.chunk_size)
				return "Cannot change chunk-size with RAID10 Grow";

			/* looks good */
			re->level = 0;
			re->before.data_disks = new_disks;
			re->after.data_disks = re->before.data_disks;
			return NULL;

		case 10:
			near = info->array.layout & 0xff;
			far = (info->array.layout >> 8) & 0xff;
			offset = info->array.layout & 0x10000;
			if (far > 1 && !offset)
				return "Cannot reshape RAID10 in far-mode";
			copies = near * far;

			old_chunk = info->array.chunk_size * far;

			if (info->new_layout == UnSet)
				info->new_layout = info->array.layout;
			else {
				near = info->new_layout & 0xff;
				far = (info->new_layout >> 8) & 0xff;
				offset = info->new_layout & 0x10000;
				if (far > 1 && !offset)
					return "Cannot reshape RAID10 to far-mode";
				if (near * far != copies)
					return "Cannot change number of copies"
						" when reshaping RAID10";
			}
			if (info->delta_disks == UnSet)
				info->delta_disks = 0;
			new_disks = (info->array.raid_disks +
				     info->delta_disks);

			new_chunk = info->new_chunk * far;

			re->level = 10;
			re->before.layout = info->array.layout;
			re->before.data_disks = info->array.raid_disks;
			re->after.layout = info->new_layout;
			re->after.data_disks = new_disks;
			/* For RAID10 we don't do backup but do allow reshape,
			 * so set backup_blocks to INVALID_SECTORS rather than
			 * zero.
			 * And there is no need to synchronise stripes on both
			 * 'old' and  'new'.  So the important
			 * number is the minimum data_offset difference
			 * which is the larger of (offset copies * chunk).
			 */
			re->backup_blocks = INVALID_SECTORS;
			re->min_offset_change = max(old_chunk, new_chunk) / 512;
			if (new_disks < re->before.data_disks &&
			    info->space_after < re->min_offset_change)
				/* Reduce component size by one chunk */
				re->new_size = (info->component_size -
						re->min_offset_change);
			else
				re->new_size = info->component_size;
			re->new_size = re->new_size * new_disks / copies;
			return NULL;

		default:
			return "RAID10 can only be changed to RAID0";
		}
	case 0:
		/* RAID0 can be converted to RAID10, or to RAID456 */
		if (info->new_level == 10) {
			if (info->new_layout == UnSet && info->delta_disks == UnSet) {
				/* Assume near=2 layout */
				info->new_layout = 0x102;
				info->delta_disks = info->array.raid_disks;
			}
			if (info->new_layout == UnSet) {
				int copies = 1 + (info->delta_disks
						  / info->array.raid_disks);
				if (info->array.raid_disks * (copies-1)
				    != info->delta_disks)
					return "Impossible number of devices"
						" for RAID0->RAID10";
				info->new_layout = 0x100 + copies;
			}
			if (info->delta_disks == UnSet) {
				int copies = info->new_layout & 0xff;
				if (info->new_layout != 0x100 + copies)
					return "New layout impossible"
						" for RAID0->RAID10";;
				info->delta_disks = (copies - 1) *
					info->array.raid_disks;
			}
			if (info->new_chunk &&
			    info->new_chunk != info->array.chunk_size)
				return "Cannot change chunk-size with RAID0->RAID10";
			/* looks good */
			re->level = 10;
			re->before.data_disks = (info->array.raid_disks +
						 info->delta_disks);
			re->after.data_disks = re->before.data_disks;
			re->before.layout = info->new_layout;
			return NULL;
		}

		/* RAID0 can also covert to RAID0/4/5/6 by first converting to
		 * a raid4 style layout of the final level.
		 */
		switch (info->new_level) {
		case 4:
			delta_parity = 1;
		case 0:
			re->level = 4;
			re->before.layout = 0;
			break;
		case 5:
			delta_parity = 1;
			re->level = 5;
			re->before.layout = ALGORITHM_PARITY_N;
			if (info->new_layout == UnSet)
				info->new_layout = map_name(r5layout, "default");
			break;
		case 6:
			delta_parity = 2;
			re->level = 6;
			re->before.layout = ALGORITHM_PARITY_N;
			if (info->new_layout == UnSet)
				info->new_layout = map_name(r6layout, "default");
			break;
		default:
			return "Impossible level change requested";
		}
		re->before.data_disks = info->array.raid_disks;
		/* determining 'after' layout happens outside this 'switch' */
		break;

	case 4:
		info->array.layout = ALGORITHM_PARITY_N;
	case 5:
		switch (info->new_level) {
		case 0:
			delta_parity = -1;
		case 4:
			re->level = info->array.level;
			re->before.data_disks = info->array.raid_disks - 1;
			re->before.layout = info->array.layout;
			break;
		case 5:
			re->level = 5;
			re->before.data_disks = info->array.raid_disks - 1;
			re->before.layout = info->array.layout;
			break;
		case 6:
			delta_parity = 1;
			re->level = 6;
			re->before.data_disks = info->array.raid_disks - 1;
			switch (info->array.layout) {
			case ALGORITHM_LEFT_ASYMMETRIC:
				re->before.layout = ALGORITHM_LEFT_ASYMMETRIC_6;
				break;
			case ALGORITHM_RIGHT_ASYMMETRIC:
				re->before.layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
				break;
			case ALGORITHM_LEFT_SYMMETRIC:
				re->before.layout = ALGORITHM_LEFT_SYMMETRIC_6;
				break;
			case ALGORITHM_RIGHT_SYMMETRIC:
				re->before.layout = ALGORITHM_RIGHT_SYMMETRIC_6;
				break;
			case ALGORITHM_PARITY_0:
				re->before.layout = ALGORITHM_PARITY_0_6;
				break;
			case ALGORITHM_PARITY_N:
				re->before.layout = ALGORITHM_PARITY_N_6;
				break;
			default:
				return "Cannot convert an array with this layout";
			}
			break;
		case 1:
			if (info->array.raid_disks != 2)
				return "Can only convert a 2-device array to RAID1";
			if (info->delta_disks != UnSet &&
			    info->delta_disks != 0)
				return "Cannot set raid_disk when "
					"converting RAID5->RAID1";
			re->level = 1;
			info->new_chunk = 0;
			return NULL;
		default:
			return "Impossible level change requested";
		}
		break;
	case 6:
		switch (info->new_level) {
		case 4:
		case 5:
			delta_parity = -1;
		case 6:
			re->level = 6;
			re->before.data_disks = info->array.raid_disks - 2;
			re->before.layout = info->array.layout;
			break;
		default:
			return "Impossible level change requested";
		}
		break;
	}

	/* If we reached here then it looks like a re-stripe is
	 * happening.  We have determined the intermediate level
	 * and initial raid_disks/layout and stored these in 're'.
	 *
	 * We need to deduce the final layout that can be atomically
	 * converted to the end state.
	 */
	switch (info->new_level) {
	case 0:
		/* We can only get to RAID0 from RAID4 or RAID5
		 * with appropriate layout and one extra device
		 */
		if (re->level != 4 && re->level != 5)
			return "Cannot covert to RAID0 from this level";

		switch (re->level) {
		case 4:
			re->before.layout = 0;
			re->after.layout = 0;
			break;
		case 5:
			re->after.layout = ALGORITHM_PARITY_N;
			break;
		}
		break;

	case 4:
		/* We can only get to RAID4 from RAID5 */
		if (re->level != 4 && re->level != 5)
			return "Cannot convert to RAID4 from this level";

		switch (re->level) {
		case 4:
			re->before.layout = 0;
			re->after.layout = 0;
			break;
		case 5:
			re->after.layout = ALGORITHM_PARITY_N;
			break;
		}
		break;

	case 5:
		/* We get to RAID5 from RAID5 or RAID6 */
		if (re->level != 5 && re->level != 6)
			return "Cannot convert to RAID5 from this level";

		switch (re->level) {
		case 5:
			if (info->new_layout == UnSet)
				re->after.layout = re->before.layout;
			else
				re->after.layout = info->new_layout;
			break;
		case 6:
			if (info->new_layout == UnSet)
				info->new_layout = re->before.layout;

			/* after.layout needs to be raid6 version of new_layout */
			if (info->new_layout == ALGORITHM_PARITY_N)
				re->after.layout = ALGORITHM_PARITY_N;
			else {
				char layout[40];
				char *ls = map_num(r5layout, info->new_layout);
				int l;
				if (ls) {
					/* Current RAID6 layout has a RAID5
					 * equivalent - good
					 */
					strcat(strcpy(layout, ls), "-6");
					l = map_name(r6layout, layout);
					if (l == UnSet)
						return "Cannot find RAID6 layout"
							" to convert to";
				} else {
					/* Current RAID6 has no equivalent.
					 * If it is already a '-6' layout we
					 * can leave it unchanged, else we must
					 * fail
					 */
					ls = map_num(r6layout, info->new_layout);
					if (!ls ||
					    strcmp(ls+strlen(ls)-2, "-6") != 0)
						return "Please specify new layout";
					l = info->new_layout;
				}
				re->after.layout = l;
			}
		}
		break;

	case 6:
		/* We must already be at level 6 */
		if (re->level != 6)
			return "Impossible level change";
		if (info->new_layout == UnSet)
			re->after.layout = info->array.layout;
		else
			re->after.layout = info->new_layout;
		break;
	default:
		return "Impossible level change requested";
	}
	if (info->delta_disks == UnSet)
		info->delta_disks = delta_parity;

	re->after.data_disks = (re->before.data_disks
				+ info->delta_disks
				- delta_parity);
	switch (re->level) {
	case 6: re->parity = 2;
		break;
	case 4:
	case 5: re->parity = 1;
		break;
	default: re->parity = 0;
		break;
	}
	/* So we have a restripe operation, we need to calculate the number
	 * of blocks per reshape operation.
	 */
	re->new_size = info->component_size * re->before.data_disks;
	if (info->new_chunk == 0)
		info->new_chunk = info->array.chunk_size;
	if (re->after.data_disks == re->before.data_disks &&
	    re->after.layout == re->before.layout &&
	    info->new_chunk == info->array.chunk_size) {
		/* Nothing to change, can change level immediately. */
		re->level = info->new_level;
		re->backup_blocks = 0;
		return NULL;
	}
	if (re->after.data_disks == 1 && re->before.data_disks == 1) {
		/* chunk and layout changes make no difference */
		re->level = info->new_level;
		re->backup_blocks = 0;
		return NULL;
	}

	if (re->after.data_disks == re->before.data_disks &&
	    get_linux_version() < 2006032)
		return "in-place reshape is not safe before 2.6.32 - sorry.";

	if (re->after.data_disks < re->before.data_disks &&
	    get_linux_version() < 2006030)
		return "reshape to fewer devices is not supported before 2.6.30 - sorry.";

	re->backup_blocks = compute_backup_blocks(
		info->new_chunk, info->array.chunk_size,
		re->after.data_disks,
		re->before.data_disks);
	re->min_offset_change = re->backup_blocks / re->before.data_disks;

	re->new_size = info->component_size * re->after.data_disks;
	return NULL;
}

static int set_array_size(struct supertype *st, struct mdinfo *sra,
			  char *text_version)
{
	struct mdinfo *info;
	char *subarray;
	int ret_val = -1;

	if ((st == NULL) || (sra == NULL))
		return ret_val;

	if (text_version == NULL)
		text_version = sra->text_version;
	subarray = strchr(text_version+1, '/')+1;
	info = st->ss->container_content(st, subarray);
	if (info) {
		unsigned long long current_size = 0;
		unsigned long long new_size =
			info->custom_array_size/2;

		if (sysfs_get_ll(sra, NULL, "array_size", &current_size) == 0 &&
		    new_size > current_size) {
			if (sysfs_set_num(sra, NULL, "array_size", new_size)
					< 0)
				dprintf("Error: Cannot set array size");
			else {
				ret_val = 0;
				dprintf("Array size changed");
			}
			dprintf(" from %llu to %llu.\n",
				current_size, new_size);
		}
		sysfs_free(info);
	} else
		dprintf("Error: set_array_size(): info pointer in NULL\n");

	return ret_val;
}

static int reshape_array(char *container, int fd, char *devname,
			 struct supertype *st, struct mdinfo *info,
			 int force, struct mddev_dev *devlist,
			 unsigned long long data_offset,
			 char *backup_file, int verbose, int forked,
			 int restart, int freeze_reshape);
static int reshape_container(char *container, char *devname,
			     int mdfd,
			     struct supertype *st,
			     struct mdinfo *info,
			     int force,
			     char *backup_file,
			     int verbose, int restart, int freeze_reshape);

int Grow_reshape(char *devname, int fd,
		 struct mddev_dev *devlist,
		 unsigned long long data_offset,
		 struct context *c, struct shape *s)
{
	/* Make some changes in the shape of an array.
	 * The kernel must support the change.
	 *
	 * There are three different changes.  Each can trigger
	 * a resync or recovery so we freeze that until we have
	 * requested everything (if kernel supports freezing - 2.6.30).
	 * The steps are:
	 *  - change size (i.e. component_size)
	 *  - change level
	 *  - change layout/chunksize/ndisks
	 *
	 * The last can require a reshape.  It is different on different
	 * levels so we need to check the level before actioning it.
	 * Some times the level change needs to be requested after the
	 * reshape (e.g. raid6->raid5, raid5->raid0)
	 *
	 */
	struct mdu_array_info_s array;
	int rv = 0;
	struct supertype *st;
	char *subarray = NULL;

	int frozen;
	int changed = 0;
	char *container = NULL;
	int cfd = -1;

	struct mddev_dev *dv;
	int added_disks;

	struct mdinfo info;
	struct mdinfo *sra;

	if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
		pr_err("%s is not an active md array - aborting\n",
			devname);
		return 1;
	}
	if (data_offset != INVALID_SECTORS && array.level != 10
	    && (array.level < 4 || array.level > 6)) {
		pr_err("--grow --data-offset not yet supported\n");
		return 1;
	}

	if (s->size > 0 &&
	    (s->chunk || s->level!= UnSet || s->layout_str || s->raiddisks)) {
		pr_err("cannot change component size at the same time "
			"as other changes.\n"
			"   Change size first, then check data is intact before "
			"making other changes.\n");
		return 1;
	}

	if (s->raiddisks && s->raiddisks < array.raid_disks && array.level > 1 &&
	    get_linux_version() < 2006032 &&
	    !check_env("MDADM_FORCE_FEWER")) {
		pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
			"       Please use a newer kernel\n");
		return 1;
	}

	st = super_by_fd(fd, &subarray);
	if (!st) {
		pr_err("Unable to determine metadata format for %s\n", devname);
		return 1;
	}
	if (s->raiddisks > st->max_devs) {
		pr_err("Cannot increase raid-disks on this array"
			" beyond %d\n", st->max_devs);
		return 1;
	}

	/* in the external case we need to check that the requested reshape is
	 * supported, and perform an initial check that the container holds the
	 * pre-requisite spare devices (mdmon owns final validation)
	 */
	if (st->ss->external) {
		int rv;

		if (subarray) {
			container = st->container_devnm;
			cfd = open_dev_excl(st->container_devnm);
		} else {
			container = st->devnm;
			close(fd);
			cfd = open_dev_excl(st->devnm);
			fd = cfd;
		}
		if (cfd < 0) {
			pr_err("Unable to open container for %s\n",
				devname);
			free(subarray);
			return 1;
		}

		rv = st->ss->load_container(st, cfd, NULL);

		if (rv) {
			pr_err("Cannot read superblock for %s\n",
				devname);
			free(subarray);
			return 1;
		}

		/* check if operation is supported for metadata handler */
		if (st->ss->container_content) {
			struct mdinfo *cc = NULL;
			struct mdinfo *content = NULL;

			cc = st->ss->container_content(st, subarray);
			for (content = cc; content ; content = content->next) {
				int allow_reshape = 1;

				/* check if reshape is allowed based on metadata
				 * indications stored in content.array.status
				 */
				if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
					allow_reshape = 0;
				if (content->array.state
				    & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))
					allow_reshape = 0;
				if (!allow_reshape) {
					pr_err("cannot reshape arrays in"
					       " container with unsupported"
					       " metadata: %s(%s)\n",
					       devname, container);
					sysfs_free(cc);
					free(subarray);
					return 1;
				}
			}
			sysfs_free(cc);
		}
		if (mdmon_running(container))
			st->update_tail = &st->updates;
	}

	added_disks = 0;
	for (dv = devlist; dv; dv = dv->next)
		added_disks++;
	if (s->raiddisks > array.raid_disks &&
	    array.spare_disks +added_disks < (s->raiddisks - array.raid_disks) &&
	    !c->force) {
		pr_err("Need %d spare%s to avoid degraded array,"
		       " and only have %d.\n"
		       "       Use --force to over-ride this check.\n",
		       s->raiddisks - array.raid_disks,
		       s->raiddisks - array.raid_disks == 1 ? "" : "s",
		       array.spare_disks + added_disks);
		return 1;
	}

	sra = sysfs_read(fd, NULL, GET_LEVEL | GET_DISKS | GET_DEVS
			 | GET_STATE | GET_VERSION);
	if (sra) {
		if (st->ss->external && subarray == NULL) {
			array.level = LEVEL_CONTAINER;
			sra->array.level = LEVEL_CONTAINER;
		}
	} else {
		pr_err("failed to read sysfs parameters for %s\n",
			devname);
		return 1;
	}
	frozen = freeze(st);
	if (frozen < -1) {
		/* freeze() already spewed the reason */
		sysfs_free(sra);
		return 1;
	} else if (frozen < 0) {
		pr_err("%s is performing resync/recovery and cannot"
			" be reshaped\n", devname);
		sysfs_free(sra);
		return 1;
	}

	/* ========= set size =============== */
	if (s->size > 0 && (s->size == MAX_SIZE || s->size != (unsigned)array.size)) {
		unsigned long long orig_size = get_component_size(fd)/2;
		unsigned long long min_csize;
		struct mdinfo *mdi;
		int raid0_takeover = 0;

		if (orig_size == 0)
			orig_size = (unsigned) array.size;

		if (orig_size == 0) {
			pr_err("Cannot set device size in this type of array.\n");
			rv = 1;
			goto release;
		}

		if (reshape_super(st, s->size, UnSet, UnSet, 0, 0, UnSet, NULL,
				  devname, APPLY_METADATA_CHANGES, c->verbose > 0)) {
			rv = 1;
			goto release;
		}
		sync_metadata(st);
		if (st->ss->external) {
			/* metadata can have size limitation
			 * update size value according to metadata information
			 */
			struct mdinfo *sizeinfo =
				st->ss->container_content(st, subarray);
			if (sizeinfo) {
				unsigned long long new_size =
					sizeinfo->custom_array_size/2;
				int data_disks = get_data_disks(
						sizeinfo->array.level,
						sizeinfo->array.layout,
						sizeinfo->array.raid_disks);
				new_size /= data_disks;
				dprintf("Metadata size correction from %llu to "
					"%llu (%llu)\n", orig_size, new_size,
					new_size * data_disks);
				s->size = new_size;
				sysfs_free(sizeinfo);
			}
		}

		/* Update the size of each member device in case
		 * they have been resized.  This will never reduce
		 * below the current used-size.  The "size" attribute
		 * understands '0' to mean 'max'.
		 */
		min_csize = 0;
		rv = 0;
		for (mdi = sra->devs; mdi; mdi = mdi->next) {
			if (sysfs_set_num(sra, mdi, "size",
					  s->size == MAX_SIZE ? 0 : s->size) < 0) {
				/* Probably kernel refusing to let us
				 * reduce the size - not an error.
				 */
				break;
			}
			if (array.not_persistent == 0 &&
			    array.major_version == 0 &&
			    get_linux_version() < 3001000) {
				/* Dangerous to allow size to exceed 2TB */
				unsigned long long csize;
				if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) {
					if (csize >= 2ULL*1024*1024*1024)
						csize = 2ULL*1024*1024*1024;
					if ((min_csize == 0 || (min_csize
								> csize)))
						min_csize = csize;
				}
			}
		}
		if (rv) {
			pr_err("Cannot set size on "
				"array members.\n");
			goto size_change_error;
		}
		if (min_csize && s->size > min_csize) {
			pr_err("Cannot safely make this array "
				"use more than 2TB per device on this kernel.\n");
			rv = 1;
			goto size_change_error;
		}
		if (min_csize && s->size == MAX_SIZE) {
			/* Don't let the kernel choose a size - it will get
			 * it wrong
			 */
			pr_err("Limited v0.90 array to "
			       "2TB per device\n");
			s->size = min_csize;
		}
		if (st->ss->external) {
			if (sra->array.level == 0) {
				rv = sysfs_set_str(sra, NULL, "level",
						   "raid5");
				if (!rv) {
					raid0_takeover = 1;
					/* get array parametes after takeover
					 * to chane one parameter at time only
					 */
					rv = ioctl(fd, GET_ARRAY_INFO, &array);
				}
			}
			/* make sure mdmon is
			 * aware of the new level */
			if (!mdmon_running(st->container_devnm))
				start_mdmon(st->container_devnm);
			ping_monitor(container);
			if (mdmon_running(st->container_devnm) &&
					st->update_tail == NULL)
				st->update_tail = &st->updates;
		}

		if (s->size == MAX_SIZE)
			s->size = 0;
		array.size = s->size;
		if ((unsigned)array.size != s->size) {
			/* got truncated to 32bit, write to
			 * component_size instead
			 */
			if (sra)
				rv = sysfs_set_num(sra, NULL,
						   "component_size", s->size);
			else
				rv = -1;
		} else {
			rv = ioctl(fd, SET_ARRAY_INFO, &array);

			/* manage array size when it is managed externally
			 */
			if ((rv == 0) && st->ss->external)
				rv = set_array_size(st, sra, sra->text_version);
		}

		if (raid0_takeover) {
			/* do not recync non-existing parity,
			 * we will drop it anyway
			 */
			sysfs_set_str(sra, NULL, "sync_action", "frozen");
			/* go back to raid0, drop parity disk
			 */
			sysfs_set_str(sra, NULL, "level", "raid0");
			ioctl(fd, GET_ARRAY_INFO, &array);
		}

size_change_error:
		if (rv != 0) {
			int err = errno;

			/* restore metadata */
			if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
					  UnSet, NULL, devname,
					  ROLLBACK_METADATA_CHANGES,
					  c->verbose) == 0)
				sync_metadata(st);
			pr_err("Cannot set device size for %s: %s\n",
				devname, strerror(err));
			if (err == EBUSY &&
			    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
				cont_err("Bitmap must be removed before size can be changed\n");
			rv = 1;
			goto release;
		}
		if (s->assume_clean) {
			/* This will fail on kernels older than 3.0 unless
			 * a backport has been arranged.
			 */
			if (sra == NULL ||
			    sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
				pr_err("--assume-clean not supported with --grow on this kernel\n");
		}
		ioctl(fd, GET_ARRAY_INFO, &array);
		s->size = get_component_size(fd)/2;
		if (s->size == 0)
			s->size = array.size;
		if (c->verbose >= 0) {
			if (s->size == orig_size)
				pr_err("component size of %s "
					"unchanged at %lluK\n",
					devname, s->size);
			else
				pr_err("component size of %s "
					"has been set to %lluK\n",
					devname, s->size);
		}
		changed = 1;
	} else if (array.level != LEVEL_CONTAINER) {
		s->size = get_component_size(fd)/2;
		if (s->size == 0)
			s->size = array.size;
	}

	/* See if there is anything else to do */
	if ((s->level == UnSet || s->level == array.level) &&
	    (s->layout_str == NULL) &&
	    (s->chunk == 0 || s->chunk == array.chunk_size) &&
	    data_offset == INVALID_SECTORS &&
	    (s->raiddisks == 0 || s->raiddisks == array.raid_disks)) {
		/* Nothing more to do */
		if (!changed && c->verbose >= 0)
			pr_err("%s: no change requested\n",
				devname);
		goto release;
	}

	/* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
	 * current implementation assumes that following conditions must be met:
	 * - RAID10:
	 *	- far_copies == 1
	 *	- near_copies == 2
	 */
	if ((s->level == 0 && array.level == 10 && sra &&
	     array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
	    (s->level == 0 && array.level == 1 && sra)) {
		int err;
		err = remove_disks_for_takeover(st, sra, array.layout);
		if (err) {
			dprintf(Name": Array cannot be reshaped\n");
			if (cfd > -1)
				close(cfd);
			rv = 1;
			goto release;
		}
		/* Make sure mdmon has seen the device removal
		 * and updated metadata before we continue with
		 * level change
		 */
		if (container)
			ping_monitor(container);
	}

	memset(&info, 0, sizeof(info));
	info.array = array;
	sysfs_init(&info, fd, NULL);
	strcpy(info.text_version, sra->text_version);
	info.component_size = s->size*2;
	info.new_level = s->level;
	info.new_chunk = s->chunk * 1024;
	if (info.array.level == LEVEL_CONTAINER) {
		info.delta_disks = UnSet;
		info.array.raid_disks = s->raiddisks;
	} else if (s->raiddisks)
		info.delta_disks = s->raiddisks - info.array.raid_disks;
	else
		info.delta_disks = UnSet;
	if (s->layout_str == NULL) {
		info.new_layout = UnSet;
		if (info.array.level == 6 &&
		    (info.new_level == 6 || info.new_level == UnSet) &&
		    info.array.layout >= 16) {
			pr_err("%s has a non-standard layout.  If you"
			       " wish to preserve this\n", devname);
			cont_err("during the reshape, please specify"
				 " --layout=preserve\n");
			cont_err("If you want to change it, specify a"
				 " layout or use --layout=normalise\n");
			rv = 1;
			goto release;
		}
	} else if (strcmp(s->layout_str, "normalise") == 0 ||
		   strcmp(s->layout_str, "normalize") == 0) {
		/* If we have a -6 RAID6 layout, remove the '-6'. */
		info.new_layout = UnSet;
		if (info.array.level == 6 && info.new_level == UnSet) {
			char l[40], *h;
			strcpy(l, map_num(r6layout, info.array.layout));
			h = strrchr(l, '-');
			if (h && strcmp(h, "-6") == 0) {
				*h = 0;
				info.new_layout = map_name(r6layout, l);
			}
		} else {
			pr_err("%s is only meaningful when reshaping"
			       " a RAID6 array.\n", s->layout_str);
			rv = 1;
			goto release;
		}
	} else if (strcmp(s->layout_str, "preserve") == 0) {
		/* This means that a non-standard RAID6 layout
		 * is OK.
		 * In particular:
		 * - When reshape a RAID6 (e.g. adding a device)
		 *   which is in a non-standard layout, it is OK
		 *   to preserve that layout.
		 * - When converting a RAID5 to RAID6, leave it in
		 *   the XXX-6 layout, don't re-layout.
		 */
		if (info.array.level == 6 && info.new_level == UnSet)
			info.new_layout = info.array.layout;
		else if (info.array.level == 5 && info.new_level == 6) {
			char l[40];
			strcpy(l, map_num(r5layout, info.array.layout));
			strcat(l, "-6");
			info.new_layout = map_name(r6layout, l);
		} else {
			pr_err("%s in only meaningful when reshaping"
			       " to RAID6\n", s->layout_str);
			rv = 1;
			goto release;
		}
	} else {
		int l = info.new_level;
		if (l == UnSet)
			l = info.array.level;
		switch (l) {
		case 5:
			info.new_layout = map_name(r5layout, s->layout_str);
			break;
		case 6:
			info.new_layout = map_name(r6layout, s->layout_str);
			break;
		case 10:
			info.new_layout = parse_layout_10(s->layout_str);
			break;
		case LEVEL_FAULTY:
			info.new_layout = parse_layout_faulty(s->layout_str);
			break;
		default:
			pr_err("layout not meaningful"
				" with this level\n");
			rv = 1;
			goto release;
		}
		if (info.new_layout == UnSet) {
			pr_err("layout %s not understood"
				" for this level\n",
				s->layout_str);
			rv = 1;
			goto release;
		}
	}

	if (array.level == LEVEL_FAULTY) {
		if (s->level != UnSet && s->level != array.level) {
			pr_err("cannot change level of Faulty device\n");
			rv =1 ;
		}
		if (s->chunk) {
			pr_err("cannot set chunksize of Faulty device\n");
			rv =1 ;
		}
		if (s->raiddisks && s->raiddisks != 1) {
			pr_err("cannot set raid_disks of Faulty device\n");
			rv =1 ;
		}
		if (s->layout_str) {
			if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
				dprintf("Cannot get array information.\n");
				goto release;
			}
			array.layout = info.new_layout;
			if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
				pr_err("failed to set new layout\n");
				rv = 1;
			} else if (c->verbose >= 0)
				printf("layout for %s set to %d\n",
				       devname, array.layout);
		}
	} else if (array.level == LEVEL_CONTAINER) {
		/* This change is to be applied to every array in the
		 * container.  This is only needed when the metadata imposes
		 * restraints of the various arrays in the container.
		 * Currently we only know that IMSM requires all arrays
		 * to have the same number of devices so changing the
		 * number of devices (On-Line Capacity Expansion) must be
		 * performed at the level of the container
		 */
		rv = reshape_container(container, devname, -1, st, &info,
				       c->force, c->backup_file, c->verbose, 0, 0);
		frozen = 0;
	} else {
		/* get spare devices from external metadata
		 */
		if (st->ss->external) {
			struct mdinfo *info2;

			info2 = st->ss->container_content(st, subarray);
			if (info2) {
				info.array.spare_disks =
					info2->array.spare_disks;
				sysfs_free(info2);
			}
		}

		/* Impose these changes on a single array.  First
		 * check that the metadata is OK with the change. */

		if (reshape_super(st, 0, info.new_level,
				  info.new_layout, info.new_chunk,
				  info.array.raid_disks, info.delta_disks,
				  c->backup_file, devname, APPLY_METADATA_CHANGES,
				  c->verbose)) {
			rv = 1;
			goto release;
		}
		sync_metadata(st);
		rv = reshape_array(container, fd, devname, st, &info, c->force,
				   devlist, data_offset, c->backup_file, c->verbose,
				   0, 0, 0);
		frozen = 0;
	}
release:
	sysfs_free(sra);
	if (frozen > 0)
		unfreeze(st);
	return rv;
}

/* verify_reshape_position()
 *	Function checks if reshape position in metadata is not farther
 *	than position in md.
 * Return value:
 *	 0 : not valid sysfs entry
 *		it can be caused by not started reshape, it should be started
 *		by reshape array or raid0 array is before takeover
 *	-1 :	error, reshape position is obviously wrong
 *	 1 :	success, reshape progress correct or updated
*/
static int verify_reshape_position(struct mdinfo *info, int level)
{
	int ret_val = 0;
	char buf[40];
	int rv;

	/* read sync_max, failure can mean raid0 array */
	rv = sysfs_get_str(info, NULL, "sync_max", buf, 40);

	if (rv > 0) {
		char *ep;
		unsigned long long position = strtoull(buf, &ep, 0);

		dprintf(Name": Read sync_max sysfs entry is: %s\n", buf);
		if (!(ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))) {
			position *= get_data_disks(level,
						   info->new_layout,
						   info->array.raid_disks);
			if (info->reshape_progress < position) {
				dprintf("Corrected reshape progress (%llu) to "
					"md position (%llu)\n",
					info->reshape_progress, position);
				info->reshape_progress = position;
				ret_val = 1;
			} else if (info->reshape_progress > position) {
				pr_err("Fatal error: array "
				       "reshape was not properly frozen "
				       "(expected reshape position is %llu, "
				       "but reshape progress is %llu.\n",
				       position, info->reshape_progress);
				ret_val = -1;
			} else {
				dprintf("Reshape position in md and metadata "
					"are the same;");
				ret_val = 1;
			}
		}
	} else if (rv == 0) {
		/* for valid sysfs entry, 0-length content
		 * should be indicated as error
		 */
		ret_val = -1;
	}

	return ret_val;
}

static unsigned long long choose_offset(unsigned long long lo,
					unsigned long long hi,
					unsigned long long min,
					unsigned long long max)
{
	/* Choose a new offset between hi and lo.
	 * It must be between min and max, but
	 * we would prefer something near the middle of hi/lo, and also
	 * prefer to be aligned to a big power of 2.
	 *
	 * So we start with the middle, then for each bit,
	 * starting at '1' and increasing, if it is set, we either
	 * add it or subtract it if possible, preferring the option
	 * which is furthest from the boundary.
	 *
	 * We stop once we get a 1MB alignment. As units are in sectors,
	 * 1MB = 2*1024 sectors.
	 */
	unsigned long long choice = (lo + hi) / 2;
	unsigned long long bit = 1;

	for (bit = 1; bit < 2*1024; bit = bit << 1) {
		unsigned long long bigger, smaller;
		if (! (bit & choice))
			continue;
		bigger = choice + bit;
		smaller = choice - bit;
		if (bigger > max && smaller < min)
			break;
		if (bigger > max)
			choice = smaller;
		else if (smaller < min)
			choice = bigger;
		else if (hi - bigger > smaller - lo)
			choice = bigger;
		else
			choice = smaller;
	}
	return choice;
}

static int set_new_data_offset(struct mdinfo *sra, struct supertype *st,
			       char *devname, int delta_disks,
			       unsigned long long data_offset,
			       unsigned long long min,
			       int can_fallback)
{
	struct mdinfo *sd;
	int dir = 0;
	int err = 0;
	unsigned long long before, after;

	/* Need to find min space before and after so same is used
	 * on all devices
	 */
	before = UINT64_MAX;
	after = UINT64_MAX;
	for (sd = sra->devs; sd; sd = sd->next) {
		char *dn;
		int dfd;
		int rv;
		struct supertype *st2;
		struct mdinfo info2;

		if (sd->disk.state & (1<<MD_DISK_FAULTY))
			continue;
		dn = map_dev(sd->disk.major, sd->disk.minor, 0);
		dfd = dev_open(dn, O_RDONLY);
		if (dfd < 0) {
			pr_err("%s: cannot open component %s\n",
				devname, dn ? dn : "-unknown-");
			goto release;
		}
		st2 = dup_super(st);
		rv = st2->ss->load_super(st2,dfd, NULL);
		close(dfd);
		if (rv) {
			free(st2);
			pr_err("%s: cannot get superblock from %s\n",
				devname, dn);
			goto release;
		}
		st2->ss->getinfo_super(st2, &info2, NULL);
		st2->ss->free_super(st2);
		free(st2);
		if (info2.space_before == 0 &&
		    info2.space_after == 0) {
			/* Metadata doesn't support data_offset changes */
			return 1;
		}
		if (before > info2.space_before)
			before = info2.space_before;
		if (after > info2.space_after)
			after = info2.space_after;

		if (data_offset != INVALID_SECTORS) {
			if (dir == 0) {
				if (info2.data_offset == data_offset) {
					pr_err("%s: already has that data_offset\n",
					       dn);
					goto release;
				}
				if (data_offset < info2.data_offset)
					dir = -1;
				else
					dir = 1;
			} else if ((data_offset <= info2.data_offset && dir == 1) ||
				   (data_offset >= info2.data_offset && dir == -1)) {
				pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
					dn);
				goto release;
			}
		}
	}
	if (before == UINT64_MAX)
		/* impossible really, there must be no devices */
		return 1;

	for (sd = sra->devs; sd; sd = sd->next) {
		char *dn = map_dev(sd->disk.major, sd->disk.minor, 0);
		unsigned long long new_data_offset;

		if (sd->disk.state & (1<<MD_DISK_FAULTY))
			continue;
		if (delta_disks < 0) {
			/* Don't need any space as array is shrinking
			 * just move data_offset up by min
			 */
			if (data_offset == INVALID_SECTORS)
				new_data_offset = sd->data_offset + min;
			else {
				if (data_offset < sd->data_offset + min) {
					pr_err("--data-offset too small for %s\n",
						dn);
					goto release;
				}
				new_data_offset = data_offset;
			}
		} else if (delta_disks > 0) {
			/* need space before */
			if (before < min) {
				if (can_fallback)
					goto fallback;
				pr_err("Insufficient head-space for reshape on %s\n",
					dn);
				goto release;
			}
			if (data_offset == INVALID_SECTORS)
				new_data_offset = sd->data_offset - min;
			else {
				if (data_offset > sd->data_offset - min) {
					pr_err("--data-offset too large for %s\n",
						dn);
					goto release;
				}
				new_data_offset = data_offset;
			}
		} else {
			if (dir == 0) {
				/* can move up or down.  If 'data_offset'
				 * was set we would have already decided,
				 * so just choose direction with most space.
				 */
				if (before > after)
					dir = -1;
				else
					dir = 1;
			}
			sysfs_set_str(sra, NULL, "reshape_direction",
				      dir == 1 ? "backwards" : "forwards");
			if (dir > 0) {
				/* Increase data offset */
				if (after < min) {
					if (can_fallback)
						goto fallback;
					pr_err("Insufficient tail-space for reshape on %s\n",
						dn);
					goto release;
				}
				if (data_offset != INVALID_SECTORS &&
				    data_offset < sd->data_offset + min) {
					pr_err("--data-offset too small on %s\n",
						dn);
					goto release;
				}
				if (data_offset != INVALID_SECTORS)
					new_data_offset = data_offset;
				else
					new_data_offset = choose_offset(sd->data_offset,
									sd->data_offset + after,
									sd->data_offset + min,
									sd->data_offset + after);
			} else {
				/* Decrease data offset */
				if (before < min) {
					if (can_fallback)
						goto fallback;
					pr_err("insufficient head-room on %s\n",
						dn);
					goto release;
				}
				if (data_offset != INVALID_SECTORS &&
				    data_offset < sd->data_offset - min) {
					pr_err("--data-offset too small on %s\n",
						dn);
					goto release;
				}
				if (data_offset != INVALID_SECTORS)
					new_data_offset = data_offset;
				else
					new_data_offset = choose_offset(sd->data_offset - before,
									sd->data_offset,
									sd->data_offset - before,
									sd->data_offset - min);
			}
		}
		err = sysfs_set_num(sra, sd, "new_offset", new_data_offset);
		if (err < 0 && errno == E2BIG) {
			/* try again after increasing data size to max */
			err = sysfs_set_num(sra, sd, "size", 0);
			if (err < 0 && errno == EINVAL &&
			    !(sd->disk.state & (1<<MD_DISK_SYNC))) {
				/* some kernels have a bug where you cannot
				 * use '0' on spare devices. */
				sysfs_set_num(sra, sd, "size",
					      (sra->component_size + after)/2);
			}
			err = sysfs_set_num(sra, sd, "new_offset",
					    new_data_offset);
		}
		if (err < 0) {
			if (errno == E2BIG && data_offset != INVALID_SECTORS) {
				pr_err("data-offset is too big for %s\n",
				       dn);
				goto release;
			}
			if (sd == sra->devs &&
			    (errno == ENOENT || errno == E2BIG))
				/* Early kernel, no 'new_offset' file,
				 * or kernel doesn't like us.
				 * For RAID5/6 this is not fatal
				 */
				return 1;
			pr_err("Cannot set new_offset for %s\n",
				dn);
			break;
		}
	}
	return err;
release:
	return -1;
fallback:
	/* Just use a backup file */
	return 1;
}

static int raid10_reshape(char *container, int fd, char *devname,
			  struct supertype *st, struct mdinfo *info,
			  struct reshape *reshape,
			  unsigned long long data_offset,
			  int force, int verbose)
{
	/* Changing raid_disks, layout, chunksize or possibly
	 * just data_offset for a RAID10.
	 * We must always change data_offset.  We change by at least
	 * ->min_offset_change which is the largest of the old and new
	 * chunk sizes.
	 * If raid_disks is increasing, then data_offset must decrease
	 * by at least this copy size.
	 * If raid_disks is unchanged, data_offset must increase or
	 * decrease by at least min_offset_change but preferably by much more.
	 * We choose half of the available space.
	 * If raid_disks is decreasing, data_offset must increase by
	 * at least min_offset_change.  To allow of this, component_size
	 * must be decreased by the same amount.
	 *
	 * So we calculate the required minimum and direction, possibly
	 * reduce the component_size, then iterate through the devices
	 * and set the new_data_offset.
	 * If that all works, we set chunk_size, layout, raid_disks, and start
	 * 'reshape'
	 */
	struct mdinfo *sra;
	unsigned long long min;
	int err = 0;

	sra = sysfs_read(fd, NULL,
			 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK
		);
	if (!sra) {
		pr_err("%s: Cannot get array details from sysfs\n",
			devname);
		goto release;
	}
	min = reshape->min_offset_change;

	if (info->delta_disks)
		sysfs_set_str(sra, NULL, "reshape_direction",
			      info->delta_disks < 0 ? "backwards" : "forwards");
	if (info->delta_disks < 0 &&
	    info->space_after < min) {
		int rv = sysfs_set_num(sra, NULL, "component_size",
				       (sra->component_size -
					min)/2);
		if (rv) {
			pr_err("cannot reduce component size\n");
			goto release;
		}
	}
	err = set_new_data_offset(sra, st, devname, info->delta_disks, data_offset,
				  min, 0);
	if (err == 1) {
		pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
		cont_err("supported on this kernel\n");
		err = -1;
	}
	if (err < 0)
		goto release;

	if (!err && sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
		err = errno;
	if (!err && sysfs_set_num(sra, NULL, "layout", reshape->after.layout) < 0)
		err = errno;
	if (!err && sysfs_set_num(sra, NULL, "raid_disks",
				  info->array.raid_disks + info->delta_disks) < 0)
		err = errno;
	if (!err && sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0)
		err = errno;
	if (err) {
		pr_err("Cannot set array shape for %s\n",
		       devname);
		if (err == EBUSY &&
		    (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
			cont_err("       Bitmap must be removed before"
				 " shape can be changed\n");
		goto release;
	}
	sysfs_free(sra);
	return 0;
release:
	sysfs_free(sra);
	return 1;
}

static void get_space_after(int fd, struct supertype *st, struct mdinfo *info)
{
	struct mdinfo *sra, *sd;
	/* Initialisation to silence compiler warning */
	unsigned long long min_space_before = 0, min_space_after = 0;
	int first = 1;

	sra = sysfs_read(fd, NULL, GET_DEVS);
	if (!sra)
		return;
	for (sd = sra->devs; sd; sd = sd->next) {
		char *dn;
		int dfd;
		struct supertype *st2;
		struct mdinfo info2;

		if (sd->disk.state & (1<<MD_DISK_FAULTY))
			continue;
		dn = map_dev(sd->disk.major, sd->disk.minor, 0);
		dfd = dev_open(dn, O_RDONLY);
		if (dfd < 0)
			break;
		st2 = dup_super(st);
		if (st2->ss->load_super(st2,dfd, NULL)) {
			close(dfd);
			free(st2);
			break;
		}
		close(dfd);
		st2->ss->getinfo_super(st2, &info2, NULL);
		st2->ss->free_super(st2);
		free(st2);
		if (first ||
		    min_space_before > info2.space_before)
			min_space_before = info2.space_before;
		if (first ||
		    min_space_after > info2.space_after)
			min_space_after = info2.space_after;
		first = 0;
	}
	if (sd == NULL && !first) {
		info->space_after = min_space_after;
		info->space_before = min_space_before;
	}
	sysfs_free(sra);
}

static void update_cache_size(char *container, struct mdinfo *sra,
			      struct mdinfo *info,
			      int disks, unsigned long long blocks)
{
	/* Check that the internal stripe cache is
	 * large enough, or it won't work.
	 * It must hold at least 4 stripes of the larger
	 * chunk size
	 */
	unsigned long cache;
	cache = max(info->array.chunk_size, info->new_chunk);
	cache *= 4; /* 4 stripes minimum */
	cache /= 512; /* convert to sectors */
	/* make sure there is room for 'blocks' with a bit to spare */
	if (cache < 16 + blocks / disks)
		cache = 16 + blocks / disks;
	cache /= (4096/512); /* Covert from sectors to pages */

	if (sra->cache_size < cache)
		subarray_set_num(container, sra, "stripe_cache_size",
				 cache+1);
}

static int impose_reshape(struct mdinfo *sra,
			  struct mdinfo *info,
			  struct supertype *st,
			  int fd,
			  int restart,
			  char *devname, char *container,
			  struct reshape *reshape)
{
	struct mdu_array_info_s array;

	sra->new_chunk = info->new_chunk;

	if (restart) {
		/* for external metadata checkpoint saved by mdmon can be lost
		 * or missed /due to e.g. crash/. Check if md is not during
		 * restart farther than metadata points to.
		 * If so, this means metadata information is obsolete.
		 */
		if (st->ss->external)
			verify_reshape_position(info, reshape->level);
		sra->reshape_progress = info->reshape_progress;
	} else {
		sra->reshape_progress = 0;
		if (reshape->after.data_disks < reshape->before.data_disks)
			/* start from the end of the new array */
			sra->reshape_progress = (sra->component_size
						 * reshape->after.data_disks);
	}

	ioctl(fd, GET_ARRAY_INFO, &array);
	if (info->array.chunk_size == info->new_chunk &&
	    reshape->before.layout == reshape->after.layout &&
	    st->ss->external == 0) {
		/* use SET_ARRAY_INFO but only if reshape hasn't started */
		array.raid_disks = reshape->after.data_disks + reshape->parity;
		if (!restart &&
		    ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
			int err = errno;

			pr_err("Cannot set device shape for %s: %s\n",
			       devname, strerror(errno));

			if (err == EBUSY &&
			    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
				cont_err("Bitmap must be removed before"
					 " shape can be changed\n");

			goto release;
		}
	} else if (!restart) {
		/* set them all just in case some old 'new_*' value
		 * persists from some earlier problem.
		 */
		int err = 0;
		if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
			err = errno;
		if (!err && sysfs_set_num(sra, NULL, "layout",
					  reshape->after.layout) < 0)
			err = errno;
		if (!err && subarray_set_num(container, sra, "raid_disks",
					     reshape->after.data_disks +
					     reshape->parity) < 0)
			err = errno;
		if (err) {
			pr_err("Cannot set device shape for %s\n",
				devname);

			if (err == EBUSY &&
			    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
				cont_err("Bitmap must be removed before"
					 " shape can be changed\n");
			goto release;
		}
	}
	return 0;
release:
	return -1;
}

static int impose_level(int fd, int level, char *devname, int verbose)
{
	char *c;
	struct mdu_array_info_s array;
	struct mdinfo info;
	sysfs_init(&info, fd, NULL);

	ioctl(fd, GET_ARRAY_INFO, &array);
	if (level == 0 &&
	    (array.level >= 4 && array.level <= 6)) {
		/* To convert to RAID0 we need to fail and
		 * remove any non-data devices. */
		int found = 0;
		int d;
		int data_disks = array.raid_disks - 1;
		if (array.level == 6)
			data_disks -= 1;
		if (array.level == 5 &&
		    array.layout != ALGORITHM_PARITY_N)
			return -1;
		if (array.level == 6 &&
		    array.layout != ALGORITHM_PARITY_N_6)
			return -1;
		sysfs_set_str(&info, NULL,"sync_action", "idle");
		/* First remove any spares so no recovery starts */
		for (d = 0, found = 0;
		     d < MAX_DISKS && found < array.nr_disks;
		     d++) {
				mdu_disk_info_t disk;
			disk.number = d;
			if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
				continue;
			if (disk.major == 0 && disk.minor == 0)
				continue;
			found++;
			if ((disk.state & (1 << MD_DISK_ACTIVE))
			    && disk.raid_disk < data_disks)
				/* keep this */
				continue;
			ioctl(fd, HOT_REMOVE_DISK,
			      makedev(disk.major, disk.minor));
		}
		/* Now fail anything left */
		ioctl(fd, GET_ARRAY_INFO, &array);
		for (d = 0, found = 0;
		     d < MAX_DISKS && found < array.nr_disks;
		     d++) {
			int cnt;
			mdu_disk_info_t disk;
			disk.number = d;
			if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
				continue;
			if (disk.major == 0 && disk.minor == 0)
				continue;
			found++;
			if ((disk.state & (1 << MD_DISK_ACTIVE))
			    && disk.raid_disk < data_disks)
				/* keep this */
				continue;
			ioctl(fd, SET_DISK_FAULTY,
			      makedev(disk.major, disk.minor));
			cnt = 5;
			while (ioctl(fd, HOT_REMOVE_DISK,
				     makedev(disk.major, disk.minor)) < 0
			       && errno == EBUSY
			       && cnt--) {
				usleep(10000);
			}
		}
	}
	c = map_num(pers, level);
	if (c) {
		int err = sysfs_set_str(&info, NULL, "level", c);
		if (err) {
			err = errno;
			pr_err("%s: could not set level to %s\n",
				devname, c);
			if (err == EBUSY &&
			    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
				cont_err("Bitmap must be removed"
					 " before level can be changed\n");
			return err;
		}
		if (verbose >= 0)
			pr_err("level of %s changed to %s\n",
				devname, c);
	}
	return 0;
}

static int reshape_array(char *container, int fd, char *devname,
			 struct supertype *st, struct mdinfo *info,
			 int force, struct mddev_dev *devlist,
			 unsigned long long data_offset,
			 char *backup_file, int verbose, int forked,
			 int restart, int freeze_reshape)
{
	struct reshape reshape;
	int spares_needed;
	char *msg;
	int orig_level = UnSet;
	int odisks;
	int delayed;

	struct mdu_array_info_s array;
	char *c;

	struct mddev_dev *dv;
	int added_disks;

	int *fdlist = NULL;
	unsigned long long *offsets = NULL;
	int d;
	int nrdisks;
	int err;
	unsigned long blocks;
	unsigned long long array_size;
	int done;
	struct mdinfo *sra = NULL;

	/* when reshaping a RAID0, the component_size might be zero.
	 * So try to fix that up.
	 */
	if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
		dprintf("Cannot get array information.\n");
		goto release;
	}
	if (array.level == 0 && info->component_size == 0) {
		get_dev_size(fd, NULL, &array_size);
		info->component_size = array_size / array.raid_disks;
	}

	if (array.level == 10)
		/* Need space_after info */
		get_space_after(fd, st, info);

	if (info->reshape_active) {
		int new_level = info->new_level;
		info->new_level = UnSet;
		if (info->delta_disks > 0)
			info->array.raid_disks -= info->delta_disks;
		msg = analyse_change(devname, info, &reshape);
		info->new_level = new_level;
		if (info->delta_disks > 0)
			info->array.raid_disks += info->delta_disks;
		if (!restart)
			/* Make sure the array isn't read-only */
			ioctl(fd, RESTART_ARRAY_RW, 0);
	} else
		msg = analyse_change(devname, info, &reshape);
	if (msg) {
		/* if msg == "", error has already been printed */
		if (msg[0])
			pr_err("%s\n", msg);
		goto release;
	}
	if (restart &&
	    (reshape.level != info->array.level ||
	     reshape.before.layout != info->array.layout ||
	     reshape.before.data_disks + reshape.parity
	     != info->array.raid_disks - max(0, info->delta_disks))) {
		pr_err("reshape info is not in native format -"
			" cannot continue.\n");
		goto release;
	}

	if (st->ss->external && restart && (info->reshape_progress == 0)) {
		/* When reshape is restarted from '0', very begin of array
		 * it is possible that for external metadata reshape and array
		 * configuration doesn't happen.
		 * Check if md has the same opinion, and reshape is restarted
		 * from 0. If so, this is regular reshape start after reshape
		 * switch in metadata to next array only.
		 */
		if ((verify_reshape_position(info, reshape.level) >= 0) &&
		    (info->reshape_progress == 0))
			restart = 0;
	}
	if (restart) {
		/* reshape already started. just skip to monitoring the reshape */
		if (reshape.backup_blocks == 0)
			return 0;
		if (restart & RESHAPE_NO_BACKUP)
			return 0;

		/* Need 'sra' down at 'started:' */
		sra = sysfs_read(fd, NULL,
				 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
				 GET_CACHE);
		if (!sra) {
			pr_err("%s: Cannot get array details from sysfs\n",
			       devname);
			goto release;
		}
		goto started;
	}
	/* The container is frozen but the array may not be.
	 * So freeze the array so spares don't get put to the wrong use
	 * FIXME there should probably be a cleaner separation between
	 * freeze_array and freeze_container.
	 */
	sysfs_freeze_array(info);
	/* Check we have enough spares to not be degraded */
	added_disks = 0;
	for (dv = devlist; dv ; dv=dv->next)
		added_disks++;
	spares_needed = max(reshape.before.data_disks,
			    reshape.after.data_disks)
		+ reshape.parity - array.raid_disks;

	if (!force &&
	    info->new_level > 1 && info->array.level > 1 &&
	    spares_needed > info->array.spare_disks + added_disks) {
		pr_err("Need %d spare%s to avoid degraded array,"
		       " and only have %d.\n"
		       "       Use --force to over-ride this check.\n",
		       spares_needed,
		       spares_needed == 1 ? "" : "s",
		       info->array.spare_disks + added_disks);
		goto release;
	}
	/* Check we have enough spares to not fail */
	spares_needed = max(reshape.before.data_disks,
			    reshape.after.data_disks)
		- array.raid_disks;
	if ((info->new_level > 1 || info->new_level == 0) &&
	    spares_needed > info->array.spare_disks +added_disks) {
		pr_err("Need %d spare%s to create working array,"
		       " and only have %d.\n",
		       spares_needed,
		       spares_needed == 1 ? "" : "s",
		       info->array.spare_disks + added_disks);
		goto release;
	}

	if (reshape.level != array.level) {
		int err = impose_level(fd, reshape.level, devname, verbose);
		if (err)
			goto release;
		info->new_layout = UnSet; /* after level change,
					   * layout is meaningless */
		orig_level = array.level;
		sysfs_freeze_array(info);

		if (reshape.level > 0 && st->ss->external) {
			/* make sure mdmon is aware of the new level */
			if (mdmon_running(container))
				flush_mdmon(container);

			if (!mdmon_running(container))
				start_mdmon(container);
			ping_monitor(container);
			if (mdmon_running(container) &&
			    st->update_tail == NULL)
				st->update_tail = &st->updates;
		}
	}
	/* ->reshape_super might have chosen some spares from the
	 * container that it wants to be part of the new array.
	 * We can collect them with ->container_content and give
	 * them to the kernel.
	 */
	if (st->ss->reshape_super && st->ss->container_content) {
		char *subarray = strchr(info->text_version+1, '/')+1;
		struct mdinfo *info2 =
			st->ss->container_content(st, subarray);
		struct mdinfo *d;

		if (info2) {
			sysfs_init(info2, fd, st->devnm);
			/* When increasing number of devices, we need to set
			 * new raid_disks before adding these, or they might
			 * be rejected.
			 */
			if (reshape.backup_blocks &&
			    reshape.after.data_disks > reshape.before.data_disks)
				subarray_set_num(container, info2, "raid_disks",
						 reshape.after.data_disks +
						 reshape.parity);
			for (d = info2->devs; d; d = d->next) {
				if (d->disk.state == 0 &&
				    d->disk.raid_disk >= 0) {
					/* This is a spare that wants to
					 * be part of the array.
					 */
					add_disk(fd, st, info2, d);
				}
			}
			sysfs_free(info2);
		}
	}
	/* We might have been given some devices to add to the
	 * array.  Now that the array has been changed to the right
	 * level and frozen, we can safely add them.
	 */
	if (devlist)
		Manage_subdevs(devname, fd, devlist, verbose,
			       0,NULL, 0);

	if (reshape.backup_blocks == 0 && data_offset != INVALID_SECTORS)
		reshape.backup_blocks = reshape.before.data_disks * info->array.chunk_size/512;
	if (reshape.backup_blocks == 0) {
		/* No restriping needed, but we might need to impose
		 * some more changes: layout, raid_disks, chunk_size
		 */
		/* read current array info */
		if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
			dprintf("Cannot get array information.\n");
			goto release;
		}
		/* compare current array info with new values and if
		 * it is different update them to new */
		if (info->new_layout != UnSet &&
		    info->new_layout != array.layout) {
			array.layout = info->new_layout;
			if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
				pr_err("failed to set new layout\n");
				goto release;
			} else if (verbose >= 0)
				printf("layout for %s set to %d\n",
				       devname, array.layout);
		}
		if (info->delta_disks != UnSet &&
		    info->delta_disks != 0 &&
		    array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
			array.raid_disks += info->delta_disks;
			if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
				pr_err("failed to set raid disks\n");
				goto release;
			} else if (verbose >= 0) {
				printf("raid_disks for %s set to %d\n",
				       devname, array.raid_disks);
			}
		}
		if (info->new_chunk != 0 &&
		    info->new_chunk != array.chunk_size) {
			if (sysfs_set_num(info, NULL,
					  "chunk_size", info->new_chunk) != 0) {
				pr_err("failed to set chunk size\n");
				goto release;
			} else if (verbose >= 0)
				printf("chunk size for %s set to %d\n",
				       devname, array.chunk_size);
		}
		unfreeze(st);
		return 0;
	}

	/*
	 * There are three possibilities.
	 * 1/ The array will shrink.
	 *    We need to ensure the reshape will pause before reaching
	 *    the 'critical section'.  We also need to fork and wait for
	 *    that to happen.  When it does we
	 *       suspend/backup/complete/unfreeze
	 *
	 * 2/ The array will not change size.
	 *    This requires that we keep a backup of a sliding window
	 *    so that we can restore data after a crash.  So we need
	 *    to fork and monitor progress.
	 *    In future we will allow the data_offset to change, so
	 *    a sliding backup becomes unnecessary.
	 *
	 * 3/ The array will grow. This is relatively easy.
	 *    However the kernel's restripe routines will cheerfully
	 *    overwrite some early data before it is safe.  So we
	 *    need to make a backup of the early parts of the array
	 *    and be ready to restore it if rebuild aborts very early.
	 *    For externally managed metadata, we still need a forked
	 *    child to monitor the reshape and suspend IO over the region
	 *    that is being reshaped.
	 *
	 *    We backup data by writing it to one spare, or to a
	 *    file which was given on command line.
	 *
	 * In each case, we first make sure that storage is available
	 * for the required backup.
	 * Then we:
	 *   -  request the shape change.
	 *   -  fork to handle backup etc.
	 */
	/* Check that we can hold all the data */
	get_dev_size(fd, NULL, &array_size);
	if (reshape.new_size < (array_size/512)) {
		pr_err("this change will reduce the size of the array.\n"
		       "       use --grow --array-size first to truncate array.\n"
		       "       e.g. mdadm --grow %s --array-size %llu\n",
		       devname, reshape.new_size/2);
		goto release;
	}

	if (array.level == 10) {
		/* Reshaping RAID10 does not require any data backup by
		 * user-space.  Instead it requires that the data_offset
		 * is changed to avoid the need for backup.
		 * So this is handled very separately
		 */
		if (restart)
			/* Nothing to do. */
			return 0;
		return raid10_reshape(container, fd, devname, st, info,
				      &reshape, data_offset,
				      force, verbose);
	}
	sra = sysfs_read(fd, NULL,
			 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
			 GET_CACHE);
	if (!sra) {
		pr_err("%s: Cannot get array details from sysfs\n",
			devname);
		goto release;
	}

	if (!backup_file)
		switch(set_new_data_offset(sra, st, devname,
					   reshape.after.data_disks - reshape.before.data_disks,
					   data_offset,
					   reshape.min_offset_change, 1)) {
	case -1:
		goto release;
	case 0:
		/* Updated data_offset, so it's easy now */
		update_cache_size(container, sra, info,
				  min(reshape.before.data_disks,
				      reshape.after.data_disks),
				  reshape.backup_blocks);

		/* Right, everything seems fine. Let's kick things off.
		 */
		sync_metadata(st);

		if (impose_reshape(sra, info, st, fd, restart,
				   devname, container, &reshape) < 0)
			goto release;
		if (sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0) {
			pr_err("Failed to initiate reshape!\n");
			goto release;
		}
		if (info->new_level == reshape.level)
			return 0;
		/* need to adjust level when reshape completes */
		switch(fork()) {
		case -1: /* ignore error, but don't wait */
			return 0;
		default: /* parent */
			return 0;
		case 0:
			map_fork();
			break;
		}
		wait_reshape(sra);
		impose_level(fd, info->new_level, devname, verbose);

		return 0;
	case 1: /* Couldn't set data_offset, try the old way */
		if (data_offset != INVALID_SECTORS) {
			pr_err("Cannot update data_offset on this array\n");
			goto release;
		}
		break;
	}

started:
	/* Decide how many blocks (sectors) for a reshape
	 * unit.  The number we have so far is just a minimum
	 */
	blocks = reshape.backup_blocks;
	if (reshape.before.data_disks ==
	    reshape.after.data_disks) {
		/* Make 'blocks' bigger for better throughput, but
		 * not so big that we reject it below.
		 * Try for 16 megabytes
		 */
		while (blocks * 32 < sra->component_size &&
		       blocks < 16*1024*2)
			blocks *= 2;
	} else
		pr_err("Need to backup %luK of critical "
			"section..\n", blocks/2);

	if (blocks >= sra->component_size/2) {
		pr_err("%s: Something wrong"
			" - reshape aborted\n",
			devname);
		goto release;
	}

	/* Now we need to open all these devices so we can read/write.
	 */
	nrdisks = max(reshape.before.data_disks,
		      reshape.after.data_disks) + reshape.parity
		+ sra->array.spare_disks;
	fdlist = xcalloc((1+nrdisks), sizeof(int));
	offsets = xcalloc((1+nrdisks), sizeof(offsets[0]));

	odisks = reshape.before.data_disks + reshape.parity;
	d = reshape_prepare_fdlist(devname, sra, odisks,
				   nrdisks, blocks, backup_file,
				   fdlist, offsets);
	if (d < 0) {
		goto release;
	}
	if ((st->ss->manage_reshape == NULL) ||
	    (st->ss->recover_backup == NULL)) {
		if (backup_file == NULL) {
			if (reshape.after.data_disks <=
			    reshape.before.data_disks) {
				pr_err("%s: Cannot grow - "
					"need backup-file\n", devname);
				goto release;
			} else if (sra->array.spare_disks == 0) {
				pr_err("%s: Cannot grow - "
					"need a spare or backup-file to backup "
					"critical section\n", devname);
				goto release;
			}
		} else {
			if (!reshape_open_backup_file(backup_file, fd, devname,
						      (signed)blocks,
						      fdlist+d, offsets+d,
						      restart)) {
				goto release;
			}
			d++;
		}
	}

	update_cache_size(container, sra, info,
			  min(reshape.before.data_disks, reshape.after.data_disks),
			  blocks);

	/* Right, everything seems fine. Let's kick things off.
	 * If only changing raid_disks, use ioctl, else use
	 * sysfs.
	 */
	sync_metadata(st);

	if (impose_reshape(sra, info, st, fd, restart,
			   devname, container, &reshape) < 0)
		goto release;

	err = start_reshape(sra, restart, reshape.before.data_disks,
			    reshape.after.data_disks);
	if (err) {
		pr_err("Cannot %s reshape for %s\n",
		       restart ? "continue" : "start",
		       devname);
		goto release;
	}
	if (restart)
		sysfs_set_str(sra, NULL, "array_state", "active");
	if (freeze_reshape) {
		free(fdlist);
		free(offsets);
		sysfs_free(sra);
		pr_err("Reshape has to be continued from"
			" location %llu when root filesystem has been mounted.\n",
			sra->reshape_progress);
		return 1;
	}

	/* Now we just need to kick off the reshape and watch, while
	 * handling backups of the data...
	 * This is all done by a forked background process.
	 */
	switch(forked ? 0 : fork()) {
	case -1:
		pr_err("Cannot run child to monitor reshape: %s\n",
			strerror(errno));
		abort_reshape(sra);
		goto release;
	default:
		free(fdlist);
		free(offsets);
		sysfs_free(sra);
		return 0;
	case 0:
		map_fork();
		break;
	}

	/* If another array on the same devices is busy, the
	 * reshape will wait for them.  This would mean that
	 * the first section that we suspend will stay suspended
	 * for a long time.  So check on that possibility
	 * by looking for "DELAYED" in /proc/mdstat, and if found,
	 * wait a while
	 */
	do {
		struct mdstat_ent *mds, *m;
		delayed = 0;
		mds = mdstat_read(0, 0);
		for (m = mds; m; m = m->next)
			if (strcmp(m->devnm, sra->sys_name) == 0) {
				if (m->resync &&
				    m->percent == RESYNC_DELAYED)
					delayed = 1;
				if (m->resync == 0)
					/* Haven't started the reshape thread
					 * yet, wait a bit
					 */
					delayed = 2;
				break;
			}
		free_mdstat(mds);
		if (delayed == 1 && get_linux_version() < 3007000) {
			pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
			       "       You might experience problems until other reshapes complete.\n");
			delayed = 0;
		}
		if (delayed)
			sleep(30 - (delayed-1) * 25);
	} while (delayed);

	close(fd);
	if (check_env("MDADM_GROW_VERIFY"))
		fd = open(devname, O_RDONLY | O_DIRECT);
	else
		fd = -1;
	mlockall(MCL_FUTURE);

	if (st->ss->external) {
		/* metadata handler takes it from here */
		done = st->ss->manage_reshape(
			fd, sra, &reshape, st, blocks,
			fdlist, offsets,
			d - odisks, fdlist+odisks,
			offsets+odisks);
	} else
		done = child_monitor(
			fd, sra, &reshape, st, blocks,
			fdlist, offsets,
			d - odisks, fdlist+odisks,
			offsets+odisks);

	free(fdlist);
	free(offsets);

	if (backup_file && done)
		unlink(backup_file);
	if (!done) {
		abort_reshape(sra);
		goto out;
	}

	if (!st->ss->external &&
	    !(reshape.before.data_disks != reshape.after.data_disks
	      && info->custom_array_size) &&
	    info->new_level == reshape.level &&
	    !forked) {
		/* no need to wait for the reshape to finish as
		 * there is nothing more to do.
		 */
		sysfs_free(sra);
		exit(0);
	}
	wait_reshape(sra);

	if (st->ss->external) {
		/* Re-load the metadata as much could have changed */
		int cfd = open_dev(st->container_devnm);
		if (cfd >= 0) {
			flush_mdmon(container);
			st->ss->free_super(st);
			st->ss->load_container(st, cfd, container);
			close(cfd);
		}
	}

	/* set new array size if required customer_array_size is used
	 * by this metadata.
	 */
	if (reshape.before.data_disks !=
	    reshape.after.data_disks &&
	    info->custom_array_size)
		set_array_size(st, info, info->text_version);

	if (info->new_level != reshape.level) {
		if (fd < 0)
			fd = open(devname, O_RDONLY);
		impose_level(fd, info->new_level, devname, verbose);
		close(fd);
		if (info->new_level == 0)
			st->update_tail = NULL;
	}
out:
	sysfs_free(sra);
	if (forked)
		return 0;
	unfreeze(st);
	exit(0);

release:
	free(fdlist);
	free(offsets);
	if (orig_level != UnSet && sra) {
		c = map_num(pers, orig_level);
		if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
			pr_err("aborting level change\n");
	}
	sysfs_free(sra);
	if (!forked)
		unfreeze(st);
	return 1;
}

/* mdfd handle is passed to be closed in child process (after fork).
 */
int reshape_container(char *container, char *devname,
		      int mdfd,
		      struct supertype *st,
		      struct mdinfo *info,
		      int force,
		      char *backup_file,
		      int verbose, int restart, int freeze_reshape)
{
	struct mdinfo *cc = NULL;
	int rv = restart;
	char last_devnm[32] = "";

	/* component_size is not meaningful for a container,
	 * so pass '0' meaning 'no change'
	 */
	if (!restart &&
	    reshape_super(st, 0, info->new_level,
			  info->new_layout, info->new_chunk,
			  info->array.raid_disks, info->delta_disks,
			  backup_file, devname, APPLY_METADATA_CHANGES,
			  verbose)) {
		unfreeze(st);
		return 1;
	}

	sync_metadata(st);

	/* ping monitor to be sure that update is on disk
	 */
	ping_monitor(container);

	switch (fork()) {
	case -1: /* error */
		perror("Cannot fork to complete reshape\n");
		unfreeze(st);
		return 1;
	default: /* parent */
		if (!freeze_reshape)
			printf(Name ": multi-array reshape continues"
			       " in background\n");
		return 0;
	case 0: /* child */
		map_fork();
		break;
	}

	/* close unused handle in child process
	 */
	if (mdfd > -1)
		close(mdfd);

	while(1) {
		/* For each member array with reshape_active,
		 * we need to perform the reshape.
		 * We pick the first array that needs reshaping and
		 * reshape it.  reshape_array() will re-read the metadata
		 * so the next time through a different array should be
		 * ready for reshape.
		 * It is possible that the 'different' array will not
		 * be assembled yet.  In that case we simple exit.
		 * When it is assembled, the mdadm which assembles it
		 * will take over the reshape.
		 */
		struct mdinfo *content;
		int fd;
		struct mdstat_ent *mdstat;
		char *adev;
		int devid;

		sysfs_free(cc);

		cc = st->ss->container_content(st, NULL);

		for (content = cc; content ; content = content->next) {
			char *subarray;
			if (!content->reshape_active)
				continue;

			subarray = strchr(content->text_version+1, '/')+1;
			mdstat = mdstat_by_subdev(subarray, container);
			if (!mdstat)
				continue;
			if (mdstat->active == 0) {
				pr_err("Skipping inactive array %s.\n",
				       mdstat->devnm);
				free_mdstat(mdstat);
				mdstat = NULL;
				continue;
			}
			break;
		}
		if (!content)
			break;

		devid = devnm2devid(mdstat->devnm);
		adev = map_dev(major(devid), minor(devid), 0);
		if (!adev)
			adev = content->text_version;

		fd = open_dev(mdstat->devnm);
		if (fd < 0) {
			printf(Name ": Device %s cannot be opened for reshape.",
			       adev);
			break;
		}

		if (strcmp(last_devnm, mdstat->devnm) == 0) {
			/* Do not allow for multiple reshape_array() calls for
			 * the same array.
			 * It can happen when reshape_array() returns without
			 * error, when reshape is not finished (wrong reshape
			 * starting/continuation conditions).  Mdmon doesn't
			 * switch to next array in container and reentry
			 * conditions for the same array occur.
			 * This is possibly interim until the behaviour of
			 * reshape_array is resolved().
			 */
			printf(Name ": Multiple reshape execution detected for "
			       "device  %s.", adev);
			close(fd);
			break;
		}
		strcpy(last_devnm, mdstat->devnm);

		sysfs_init(content, fd, mdstat->devnm);

		if (mdmon_running(container))
			flush_mdmon(container);

		rv = reshape_array(container, fd, adev, st,
				   content, force, NULL, 0ULL,
				   backup_file, verbose, 1, restart,
				   freeze_reshape);
		close(fd);

		if (freeze_reshape) {
			sysfs_free(cc);
			exit(0);
		}

		restart = 0;
		if (rv)
			break;

		if (mdmon_running(container))
			flush_mdmon(container);
	}
	if (!rv)
		unfreeze(st);
	sysfs_free(cc);
	exit(0);
}

/*
 * We run a child process in the background which performs the following
 * steps:
 *   - wait for resync to reach a certain point
 *   - suspend io to the following section
 *   - backup that section
 *   - allow resync to proceed further
 *   - resume io
 *   - discard the backup.
 *
 * When are combined in slightly different ways in the three cases.
 * Grow:
 *   - suspend/backup/allow/wait/resume/discard
 * Shrink:
 *   - allow/wait/suspend/backup/allow/wait/resume/discard
 * same-size:
 *   - wait/resume/discard/suspend/backup/allow
 *
 * suspend/backup/allow always come together
 * wait/resume/discard do too.
 * For the same-size case we have two backups to improve flow.
 *
 */

int progress_reshape(struct mdinfo *info, struct reshape *reshape,
		     unsigned long long backup_point,
		     unsigned long long wait_point,
		     unsigned long long *suspend_point,
		     unsigned long long *reshape_completed)
{
	/* This function is called repeatedly by the reshape manager.
	 * It determines how much progress can safely be made and allows
	 * that progress.
	 * - 'info' identifies the array and particularly records in
	 *    ->reshape_progress the metadata's knowledge of progress
	 *      This is a sector offset from the start of the array
	 *      of the next array block to be relocated.  This number
	 *      may increase from 0 or decrease from array_size, depending
	 *      on the type of reshape that is happening.
	 *    Note that in contrast, 'sync_completed' is a block count of the
	 *    reshape so far.  It gives the distance between the start point
	 *    (head or tail of device) and the next place that data will be
	 *    written.  It always increases.
	 * - 'reshape' is the structure created by analyse_change
	 * - 'backup_point' shows how much the metadata manager has backed-up
	 *   data.  For reshapes with increasing progress, it is the next address
	 *   to be backed up, previous addresses have been backed-up.  For
	 *   decreasing progress, it is the earliest address that has been
	 *   backed up - later address are also backed up.
	 *   So addresses between reshape_progress and backup_point are
	 *   backed up providing those are in the 'correct' order.
	 * - 'wait_point' is an array address.  When reshape_completed
	 *   passes this point, progress_reshape should return.  It might
	 *   return earlier if it determines that ->reshape_progress needs
	 *   to be updated or further backup is needed.
	 * - suspend_point is maintained by progress_reshape and the caller
	 *   should not touch it except to initialise to zero.
	 *   It is an array address and it only increases in 2.6.37 and earlier.
	 *   This makes it difficult to handle reducing reshapes with
	 *   external metadata.
	 *   However:  it is similar to backup_point in that it records the
	 *     other end of a suspended region from  reshape_progress.
	 *     it is moved to extend the region that is safe to backup and/or
	 *     reshape
	 * - reshape_completed is read from sysfs and returned.  The caller
	 *   should copy this into ->reshape_progress when it has reason to
	 *   believe that the metadata knows this, and any backup outside this
	 *   has been erased.
	 *
	 * Return value is:
	 *   1 if more data from backup_point - but only as far as suspend_point,
	 *     should be backed up
	 *   0 if things are progressing smoothly
	 *  -1 if the reshape is finished because it is all done,
	 *  -2 if the reshape is finished due to an error.
	 */

	int advancing = (reshape->after.data_disks
			 >= reshape->before.data_disks);
	unsigned long long need_backup; /* All data between start of array and
					 * here will at some point need to
					 * be backed up.
					 */
	unsigned long long read_offset, write_offset;
	unsigned long long write_range;
	unsigned long long max_progress, target, completed;
	unsigned long long array_size = (info->component_size
					 * reshape->before.data_disks);
	int fd;
	char buf[20];

	/* First, we unsuspend any region that is now known to be safe.
	 * If suspend_point is on the 'wrong' side of reshape_progress, then
	 * we don't have or need suspension at the moment.  This is true for
	 * native metadata when we don't need to back-up.
	 */
	if (advancing) {
		if (info->reshape_progress <= *suspend_point)
			sysfs_set_num(info, NULL, "suspend_lo",
				      info->reshape_progress);
	} else {
		/* Note: this won't work in 2.6.37 and before.
		 * Something somewhere should make sure we don't need it!
		 */
		if (info->reshape_progress >= *suspend_point)
			sysfs_set_num(info, NULL, "suspend_hi",
				      info->reshape_progress);
	}

	/* Now work out how far it is safe to progress.
	 * If the read_offset for ->reshape_progress is less than
	 * 'blocks' beyond the write_offset, we can only progress as far
	 * as a backup.
	 * Otherwise we can progress until the write_offset for the new location
	 * reaches (within 'blocks' of) the read_offset at the current location.
	 * However that region must be suspended unless we are using native
	 * metadata.
	 * If we need to suspend more, we limit it to 128M per device, which is
	 * rather arbitrary and should be some time-based calculation.
	 */
	read_offset = info->reshape_progress / reshape->before.data_disks;
	write_offset = info->reshape_progress / reshape->after.data_disks;
	write_range = info->new_chunk/512;
	if (reshape->before.data_disks == reshape->after.data_disks)
		need_backup = array_size;
	else
		need_backup = reshape->backup_blocks;
	if (advancing) {
		if (read_offset < write_offset + write_range)
			max_progress = backup_point;
		else
			max_progress =
				read_offset *
				reshape->after.data_disks;
	} else {
		if (read_offset > write_offset - write_range)
			/* Can only progress as far as has been backed up,
			 * which must be suspended */
			max_progress = backup_point;
		else if (info->reshape_progress <= need_backup)
			max_progress = backup_point;
		else {
			if (info->array.major_version >= 0)
				/* Can progress until backup is needed */
				max_progress = need_backup;
			else {
				/* Can progress until metadata update is required */
				max_progress =
					read_offset *
					reshape->after.data_disks;
				/* but data must be suspended */
				if (max_progress < *suspend_point)
					max_progress = *suspend_point;
			}
		}
	}

	/* We know it is safe to progress to 'max_progress' providing
	 * it is suspended or we are using native metadata.
	 * Consider extending suspend_point 128M per device if it
	 * is less than 64M per device beyond reshape_progress.
	 * But always do a multiple of 'blocks'
	 * FIXME this is too big - it takes to long to complete
	 * this much.
	 */
	target = 64*1024*2 * min(reshape->before.data_disks,
				 reshape->after.data_disks);
	target /= reshape->backup_blocks;
	if (target < 2)
		target = 2;
	target *= reshape->backup_blocks;

	/* For externally managed metadata we always need to suspend IO to
	 * the area being reshaped so we regularly push suspend_point forward.
	 * For native metadata we only need the suspend if we are going to do
	 * a backup.
	 */
	if (advancing) {
		if ((need_backup > info->reshape_progress
		     || info->array.major_version < 0) &&
		    *suspend_point < info->reshape_progress + target) {
			if (need_backup < *suspend_point + 2 * target)
				*suspend_point = need_backup;
			else if (*suspend_point + 2 * target < array_size)
				*suspend_point += 2 * target;
			else
				*suspend_point = array_size;
			sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
			if (max_progress > *suspend_point)
				max_progress = *suspend_point;
		}
	} else {
		if (info->array.major_version >= 0) {
			/* Only need to suspend when about to backup */
			if (info->reshape_progress < need_backup * 2 &&
			    *suspend_point > 0) {
				*suspend_point = 0;
				sysfs_set_num(info, NULL, "suspend_lo", 0);
				sysfs_set_num(info, NULL, "suspend_hi", need_backup);
			}
		} else {
			/* Need to suspend continually */
			if (info->reshape_progress < *suspend_point)
				*suspend_point = info->reshape_progress;
			if (*suspend_point + target < info->reshape_progress)
				/* No need to move suspend region yet */;
			else {
				if (*suspend_point >= 2 * target)
					*suspend_point -= 2 * target;
				else
					*suspend_point = 0;
				sysfs_set_num(info, NULL, "suspend_lo",
					      *suspend_point);
			}
			if (max_progress < *suspend_point)
				max_progress = *suspend_point;
		}
	}

	/* now set sync_max to allow that progress. sync_max, like
	 * sync_completed is a count of sectors written per device, so
	 * we find the difference between max_progress and the start point,
	 * and divide that by after.data_disks to get a sync_max
	 * number.
	 * At the same time we convert wait_point to a similar number
	 * for comparing against sync_completed.
	 */
	/* scale down max_progress to per_disk */
	max_progress /= reshape->after.data_disks;
	/* Round to chunk size as some kernels give an erroneously high number */
	max_progress /= info->new_chunk/512;
	max_progress *= info->new_chunk/512;
	/* And round to old chunk size as the kernel wants that */
	max_progress /= info->array.chunk_size/512;
	max_progress *= info->array.chunk_size/512;
	/* Limit progress to the whole device */
	if (max_progress > info->component_size)
		max_progress = info->component_size;
	wait_point /= reshape->after.data_disks;
	if (!advancing) {
		/* switch from 'device offset' to 'processed block count' */
		max_progress = info->component_size - max_progress;
		wait_point = info->component_size - wait_point;
	}

	sysfs_set_num(info, NULL, "sync_max", max_progress);

	/* Now wait.  If we have already reached the point that we were
	 * asked to wait to, don't wait at all, else wait for any change.
	 * We need to select on 'sync_completed' as that is the place that
	 * notifications happen, but we are really interested in
	 * 'reshape_position'
	 */
	fd = sysfs_get_fd(info, NULL, "sync_completed");
	if (fd < 0)
		goto check_progress;

	if (sysfs_fd_get_ll(fd, &completed) < 0)
		goto check_progress;

	while (completed < max_progress && completed < wait_point) {
		/* Check that sync_action is still 'reshape' to avoid
		 * waiting forever on a dead array
		 */
		char action[20];
		if (sysfs_get_str(info, NULL, "sync_action",
				  action, 20) <= 0 ||
		    strncmp(action, "reshape", 7) != 0)
			break;
		/* Some kernels reset 'sync_completed' to zero
		 * before setting 'sync_action' to 'idle'.
		 * So we need these extra tests.
		 */
		if (completed == 0 && advancing
		    && info->reshape_progress > 0)
			break;
		if (completed == 0 && !advancing
		    && info->reshape_progress < (info->component_size
						 * reshape->after.data_disks))
			break;
		sysfs_wait(fd, NULL);
		if (sysfs_fd_get_ll(fd, &completed) < 0)
			goto check_progress;
	}
	/* Some kernels reset 'sync_completed' to zero,
	 * we need to have real point we are in md
	 */
	if (completed == 0)
		completed = max_progress;

	/* some kernels can give an incorrectly high 'completed' number */
	completed /= (info->new_chunk/512);
	completed *= (info->new_chunk/512);
	/* Convert 'completed' back in to a 'progress' number */
	completed *= reshape->after.data_disks;
	if (!advancing) {
		completed = info->component_size * reshape->after.data_disks
			- completed;
	}
	*reshape_completed = completed;

	close(fd);

	/* We return the need_backup flag.  Caller will decide
	 * how much - a multiple of ->backup_blocks up to *suspend_point
	 */
	if (advancing)
		return need_backup > info->reshape_progress;
	else
		return need_backup >= info->reshape_progress;

check_progress:
	/* if we couldn't read a number from sync_completed, then
	 * either the reshape did complete, or it aborted.
	 * We can tell which by checking for 'none' in reshape_position.
	 * If it did abort, then it might immediately restart if it
	 * it was just a device failure that leaves us degraded but
	 * functioning.
	 */
	strcpy(buf, "hi");
	if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
	    || strncmp(buf, "none", 4) != 0) {
		/* The abort might only be temporary.  Wait up to 10
		 * seconds for fd to contain a valid number again.
		 */
		int wait = 10000;
		int rv = -2;
		while (fd >= 0 && rv < 0 && wait > 0) {
			if (sysfs_wait(fd, &wait) != 1)
				break;
			switch (sysfs_fd_get_ll(fd, &completed)) {
			case 0:
				/* all good again */
				rv = 1;
				break;
			case -2: /* read error - abort */
				wait = 0;
				break;
			}
		}
		if (fd >= 0)
			close(fd);
		return rv; /* abort */
	} else {
		/* Maybe racing with array shutdown - check state */
		if (fd >= 0)
			close(fd);
		if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
		    || strncmp(buf, "inactive", 8) == 0
		    || strncmp(buf, "clear",5) == 0)
			return -2; /* abort */
		return -1; /* complete */
	}
}

/* FIXME return status is never checked */
static int grow_backup(struct mdinfo *sra,
		unsigned long long offset, /* per device */
		unsigned long stripes, /* per device, in old chunks */
		int *sources, unsigned long long *offsets,
		int disks, int chunk, int level, int layout,
		int dests, int *destfd, unsigned long long *destoffsets,
		int part, int *degraded,
		char *buf)
{
	/* Backup 'blocks' sectors at 'offset' on each device of the array,
	 * to storage 'destfd' (offset 'destoffsets'), after first
	 * suspending IO.  Then allow resync to continue
	 * over the suspended section.
	 * Use part 'part' of the backup-super-block.
	 */
	int odata = disks;
	int rv = 0;
	int i;
	unsigned long long ll;
	int new_degraded;
	//printf("offset %llu\n", offset);
	if (level >= 4)
		odata--;
	if (level == 6)
		odata--;

	/* Check that array hasn't become degraded, else we might backup the wrong data */
	if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
		return -1; /* FIXME this error is ignored */
	new_degraded = (int)ll;
	if (new_degraded != *degraded) {
		/* check each device to ensure it is still working */
		struct mdinfo *sd;
		for (sd = sra->devs ; sd ; sd = sd->next) {
			if (sd->disk.state & (1<<MD_DISK_FAULTY))
				continue;
			if (sd->disk.state & (1<<MD_DISK_SYNC)) {
				char sbuf[20];
				if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
				    strstr(sbuf, "faulty") ||
				    strstr(sbuf, "in_sync") == NULL) {
					/* this device is dead */
					sd->disk.state = (1<<MD_DISK_FAULTY);
					if (sd->disk.raid_disk >= 0 &&
					    sources[sd->disk.raid_disk] >= 0) {
						close(sources[sd->disk.raid_disk]);
						sources[sd->disk.raid_disk] = -1;
					}
				}
			}
		}
		*degraded = new_degraded;
	}
	if (part) {
		bsb.arraystart2 = __cpu_to_le64(offset * odata);
		bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
	} else {
		bsb.arraystart = __cpu_to_le64(offset * odata);
		bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
	}
	if (part)
		bsb.magic[15] = '2';
	for (i = 0; i < dests; i++)
		if (part)
			lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
		else
			lseek64(destfd[i], destoffsets[i], 0);

	rv = save_stripes(sources, offsets,
			  disks, chunk, level, layout,
			  dests, destfd,
			  offset*512*odata, stripes * chunk * odata,
			  buf);

	if (rv)
		return rv;
	bsb.mtime = __cpu_to_le64(time(0));
	for (i = 0; i < dests; i++) {
		bsb.devstart = __cpu_to_le64(destoffsets[i]/512);

		bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
		if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
			bsb.sb_csum2 = bsb_csum((char*)&bsb,
						((char*)&bsb.sb_csum2)-((char*)&bsb));

		rv = -1;
		if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
		    != destoffsets[i] - 4096)
			break;
		if (write(destfd[i], &bsb, 512) != 512)
			break;
		if (destoffsets[i] > 4096) {
			if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
			    destoffsets[i]+stripes*chunk*odata)
				break;
			if (write(destfd[i], &bsb, 512) != 512)
				break;
		}
		fsync(destfd[i]);
		rv = 0;
	}

	return rv;
}

/* in 2.6.30, the value reported by sync_completed can be
 * less that it should be by one stripe.
 * This only happens when reshape hits sync_max and pauses.
 * So allow wait_backup to either extent sync_max further
 * than strictly necessary, or return before the
 * sync has got quite as far as we would really like.
 * This is what 'blocks2' is for.
 * The various caller give appropriate values so that
 * every works.
 */
/* FIXME return value is often ignored */
static int forget_backup(int dests, int *destfd,
			 unsigned long long *destoffsets,
			 int part)
{
	/*
	 * Erase backup 'part' (which is 0 or 1)
	 */
	int i;
	int rv;

	if (part) {
		bsb.arraystart2 = __cpu_to_le64(0);
		bsb.length2 = __cpu_to_le64(0);
	} else {
		bsb.arraystart = __cpu_to_le64(0);
		bsb.length = __cpu_to_le64(0);
	}
	bsb.mtime = __cpu_to_le64(time(0));
	rv = 0;
	for (i = 0; i < dests; i++) {
		bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
		bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
		if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
			bsb.sb_csum2 = bsb_csum((char*)&bsb,
						((char*)&bsb.sb_csum2)-((char*)&bsb));
		if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
		    destoffsets[i]-4096)
			rv = -1;
		if (rv == 0 &&
		    write(destfd[i], &bsb, 512) != 512)
			rv = -1;
		fsync(destfd[i]);
	}
	return rv;
}

static void fail(char *msg)
{
	int rv;
	rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
	rv |= (write(2, "\n", 1) != 1);
	exit(rv ? 1 : 2);
}

static char *abuf, *bbuf;
static unsigned long long abuflen;
static void validate(int afd, int bfd, unsigned long long offset)
{
	/* check that the data in the backup against the array.
	 * This is only used for regression testing and should not
	 * be used while the array is active
	 */
	if (afd < 0)
		return;
	lseek64(bfd, offset - 4096, 0);
	if (read(bfd, &bsb2, 512) != 512)
		fail("cannot read bsb");
	if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
				     ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
		fail("first csum bad");
	if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
		fail("magic is bad");
	if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
	    bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
				      ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
		fail("second csum bad");

	if (__le64_to_cpu(bsb2.devstart)*512 != offset)
		fail("devstart is wrong");

	if (bsb2.length) {
		unsigned long long len = __le64_to_cpu(bsb2.length)*512;

		if (abuflen < len) {
			free(abuf);
			free(bbuf);
			abuflen = len;
			if (posix_memalign((void**)&abuf, 4096, abuflen) ||
			    posix_memalign((void**)&bbuf, 4096, abuflen)) {
				abuflen = 0;
				/* just stop validating on mem-alloc failure */
				return;
			}
		}

		lseek64(bfd, offset, 0);
		if ((unsigned long long)read(bfd, bbuf, len) != len) {
			//printf("len %llu\n", len);
			fail("read first backup failed");
		}
		lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
		if ((unsigned long long)read(afd, abuf, len) != len)
			fail("read first from array failed");
		if (memcmp(bbuf, abuf, len) != 0) {
#if 0
			int i;
			printf("offset=%llu len=%llu\n",
			       (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
			for (i=0; i<len; i++)
				if (bbuf[i] != abuf[i]) {
					printf("first diff byte %d\n", i);
					break;
				}
#endif
			fail("data1 compare failed");
		}
	}
	if (bsb2.length2) {
		unsigned long long len = __le64_to_cpu(bsb2.length2)*512;

		if (abuflen < len) {
			free(abuf);
			free(bbuf);
			abuflen = len;
			abuf = xmalloc(abuflen);
			bbuf = xmalloc(abuflen);
		}

		lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
		if ((unsigned long long)read(bfd, bbuf, len) != len)
			fail("read second backup failed");
		lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
		if ((unsigned long long)read(afd, abuf, len) != len)
			fail("read second from array failed");
		if (memcmp(bbuf, abuf, len) != 0)
			fail("data2 compare failed");
	}
}

int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
		  struct supertype *st, unsigned long blocks,
		  int *fds, unsigned long long *offsets,
		  int dests, int *destfd, unsigned long long *destoffsets)
{
	/* Monitor a reshape where backup is being performed using
	 * 'native' mechanism - either to a backup file, or
	 * to some space in a spare.
	 */
	char *buf;
	int degraded = -1;
	unsigned long long speed;
	unsigned long long suspend_point, array_size;
	unsigned long long backup_point, wait_point;
	unsigned long long reshape_completed;
	int done = 0;
	int increasing = reshape->after.data_disks >= reshape->before.data_disks;
	int part = 0; /* The next part of the backup area to fill.  It may already
		       * be full, so we need to check */
	int level = reshape->level;
	int layout = reshape->before.layout;
	int data = reshape->before.data_disks;
	int disks = reshape->before.data_disks + reshape->parity;
	int chunk = sra->array.chunk_size;
	struct mdinfo *sd;
	unsigned long stripes;
	int uuid[4];

	/* set up the backup-super-block.  This requires the
	 * uuid from the array.
	 */
	/* Find a superblock */
	for (sd = sra->devs; sd; sd = sd->next) {
		char *dn;
		int devfd;
		int ok;
		if (sd->disk.state & (1<<MD_DISK_FAULTY))
			continue;
		dn = map_dev(sd->disk.major, sd->disk.minor, 1);
		devfd = dev_open(dn, O_RDONLY);
		if (devfd < 0)
			continue;
		ok = st->ss->load_super(st, devfd, NULL);
		close(devfd);
		if (ok == 0)
			break;
	}
	if (!sd) {
		pr_err("Cannot find a superblock\n");
		return 0;
	}

	memset(&bsb, 0, 512);
	memcpy(bsb.magic, "md_backup_data-1", 16);
	st->ss->uuid_from_super(st, uuid);
	memcpy(bsb.set_uuid, uuid, 16);
	bsb.mtime = __cpu_to_le64(time(0));
	bsb.devstart2 = blocks;

	stripes = blocks / (sra->array.chunk_size/512) /
		reshape->before.data_disks;

	if (posix_memalign((void**)&buf, 4096, disks * chunk))
		/* Don't start the 'reshape' */
		return 0;
	if (reshape->before.data_disks == reshape->after.data_disks) {
		sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
		sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
	}

	if (increasing) {
		array_size = sra->component_size * reshape->after.data_disks;
		backup_point = sra->reshape_progress;
		suspend_point = 0;
	} else {
		array_size = sra->component_size * reshape->before.data_disks;
		backup_point = reshape->backup_blocks;
		suspend_point = array_size;
	}

	while (!done) {
		int rv;

		/* Want to return as soon the oldest backup slot can
		 * be released as that allows us to start backing up
		 * some more, providing suspend_point has been
		 * advanced, which it should have.
		 */
		if (increasing) {
			wait_point = array_size;
			if (part == 0 && __le64_to_cpu(bsb.length) > 0)
				wait_point = (__le64_to_cpu(bsb.arraystart) +
					      __le64_to_cpu(bsb.length));
			if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
				wait_point = (__le64_to_cpu(bsb.arraystart2) +
					      __le64_to_cpu(bsb.length2));
		} else {
			wait_point = 0;
			if (part == 0 && __le64_to_cpu(bsb.length) > 0)
				wait_point = __le64_to_cpu(bsb.arraystart);
			if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
				wait_point = __le64_to_cpu(bsb.arraystart2);
		}

		rv = progress_reshape(sra, reshape,
				      backup_point, wait_point,
				      &suspend_point, &reshape_completed);
		/* external metadata would need to ping_monitor here */
		sra->reshape_progress = reshape_completed;

		/* Clear any backup region that is before 'here' */
		if (increasing) {
			if (__le64_to_cpu(bsb.length) > 0 &&
			    reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
						  __le64_to_cpu(bsb.length)))
				forget_backup(dests, destfd,
					      destoffsets, 0);
			if (__le64_to_cpu(bsb.length2) > 0 &&
			    reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
						  __le64_to_cpu(bsb.length2)))
				forget_backup(dests, destfd,
					      destoffsets, 1);
		} else {
			if (__le64_to_cpu(bsb.length) > 0 &&
			    reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
				forget_backup(dests, destfd,
					      destoffsets, 0);
			if (__le64_to_cpu(bsb.length2) > 0 &&
			    reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
				forget_backup(dests, destfd,
					      destoffsets, 1);
		}

		if (rv < 0) {
			if (rv == -1)
				done = 1;
			break;
		}
		if (rv == 0 && increasing && !st->ss->external) {
			/* No longer need to monitor this reshape */
			done = 1;
			break;
		}

		while (rv) {
			unsigned long long offset;
			unsigned long actual_stripes;
			/* Need to backup some data.
			 * If 'part' is not used and the desired
			 * backup size is suspended, do a backup,
			 * then consider the next part.
			 */
			/* Check that 'part' is unused */
			if (part == 0 && __le64_to_cpu(bsb.length) != 0)
				break;
			if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
				break;

			offset = backup_point / data;
			actual_stripes = stripes;
			if (increasing) {
				if (offset + actual_stripes * (chunk/512) >
				    sra->component_size)
					actual_stripes = ((sra->component_size - offset)
							  / (chunk/512));
				if (offset + actual_stripes * (chunk/512) >
				    suspend_point/data)
					break;
			} else {
				if (offset < actual_stripes * (chunk/512))
					actual_stripes = offset / (chunk/512);
				offset -= actual_stripes * (chunk/512);
				if (offset < suspend_point/data)
					break;
			}
			if (actual_stripes == 0)
				break;
			grow_backup(sra, offset, actual_stripes,
				    fds, offsets,
				    disks, chunk, level, layout,
				    dests, destfd, destoffsets,
				    part, &degraded, buf);
			validate(afd, destfd[0], destoffsets[0]);
			/* record where 'part' is up to */
			part = !part;
			if (increasing)
				backup_point += actual_stripes * (chunk/512) * data;
			else
				backup_point -= actual_stripes * (chunk/512) * data;
		}
	}

	/* FIXME maybe call progress_reshape one more time instead */
	abort_reshape(sra); /* remove any remaining suspension */
	if (reshape->before.data_disks == reshape->after.data_disks)
		sysfs_set_num(sra, NULL, "sync_speed_min", speed);
	free(buf);
	return done;
}

/*
 * If any spare contains md_back_data-1 which is recent wrt mtime,
 * write that data into the array and update the super blocks with
 * the new reshape_progress
 */
int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
		 char *backup_file, int verbose)
{
	int i, j;
	int old_disks;
	unsigned long long *offsets;
	unsigned long long  nstripe, ostripe;
	int ndata, odata;

	odata = info->array.raid_disks - info->delta_disks - 1;
	if (info->array.level == 6) odata--; /* number of data disks */
	ndata = info->array.raid_disks - 1;
	if (info->new_level == 6) ndata--;

	old_disks = info->array.raid_disks - info->delta_disks;

	if (info->delta_disks <= 0)
		/* Didn't grow, so the backup file must have
		 * been used
		 */
		old_disks = cnt;
	for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
		struct mdinfo dinfo;
		int fd;
		int bsbsize;
		char *devname, namebuf[20];
		unsigned long long lo, hi;

		/* This was a spare and may have some saved data on it.
		 * Load the superblock, find and load the
		 * backup_super_block.
		 * If either fail, go on to next device.
		 * If the backup contains no new info, just return
		 * else restore data and update all superblocks
		 */
		if (i == old_disks-1) {
			fd = open(backup_file, O_RDONLY);
			if (fd<0) {
				pr_err("backup file %s inaccessible: %s\n",
					backup_file, strerror(errno));
				continue;
			}
			devname = backup_file;
		} else {
			fd = fdlist[i];
			if (fd < 0)
				continue;
			if (st->ss->load_super(st, fd, NULL))
				continue;

			st->ss->getinfo_super(st, &dinfo, NULL);
			st->ss->free_super(st);

			if (lseek64(fd,
				    (dinfo.data_offset + dinfo.component_size - 8) <<9,
				    0) < 0) {
				pr_err("Cannot seek on device %d\n", i);
				continue; /* Cannot seek */
			}
			sprintf(namebuf, "device-%d", i);
			devname = namebuf;
		}
		if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
			if (verbose)
				pr_err("Cannot read from %s\n", devname);
			continue; /* Cannot read */
		}
		if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
		    memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
			if (verbose)
				pr_err("No backup metadata on %s\n", devname);
			continue;
		}
		if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
			if (verbose)
				pr_err("Bad backup-metadata checksum on %s\n", devname);
			continue; /* bad checksum */
		}
		if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
		    bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
			if (verbose)
				pr_err("Bad backup-metadata checksum2 on %s\n", devname);
			continue; /* Bad second checksum */
		}
		if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
			if (verbose)
				pr_err("Wrong uuid on backup-metadata on %s\n", devname);
			continue; /* Wrong uuid */
		}

		/* array utime and backup-mtime should be updated at much the same time, but it seems that
		 * sometimes they aren't... So allow considerable flexability in matching, and allow
		 * this test to be overridden by an environment variable.
		 */
		if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
		    info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
			if (check_env("MDADM_GROW_ALLOW_OLD")) {
				pr_err("accepting backup with timestamp %lu "
					"for array with timestamp %lu\n",
					(unsigned long)__le64_to_cpu(bsb.mtime),
					(unsigned long)info->array.utime);
			} else {
				pr_err("too-old timestamp on backup-metadata on %s\n", devname);
				pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
				continue; /* time stamp is too bad */
			}
		}

		if (bsb.magic[15] == '1') {
			if (bsb.length == 0)
				continue;
			if (info->delta_disks >= 0) {
				/* reshape_progress is increasing */
				if (__le64_to_cpu(bsb.arraystart)
				    + __le64_to_cpu(bsb.length)
				    < info->reshape_progress) {
				nonew:
					if (verbose)
						pr_err("backup-metadata found on %s but is not needed\n", devname);
					continue; /* No new data here */
				}
			} else {
				/* reshape_progress is decreasing */
				if (__le64_to_cpu(bsb.arraystart) >=
				    info->reshape_progress)
					goto nonew; /* No new data here */
			}
		} else {
			if (bsb.length == 0 && bsb.length2 == 0)
				continue;
			if (info->delta_disks >= 0) {
				/* reshape_progress is increasing */
				if ((__le64_to_cpu(bsb.arraystart)
				     + __le64_to_cpu(bsb.length)
				     < info->reshape_progress)
				    &&
				    (__le64_to_cpu(bsb.arraystart2)
				     + __le64_to_cpu(bsb.length2)
				     < info->reshape_progress))
					goto nonew; /* No new data here */
			} else {
				/* reshape_progress is decreasing */
				if (__le64_to_cpu(bsb.arraystart) >=
				    info->reshape_progress &&
				    __le64_to_cpu(bsb.arraystart2) >=
				    info->reshape_progress)
					goto nonew; /* No new data here */
			}
		}
		if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
		second_fail:
			if (verbose)
				pr_err("Failed to verify secondary backup-metadata block on %s\n",
				       devname);
			continue; /* Cannot seek */
		}
		/* There should be a duplicate backup superblock 4k before here */
		if (lseek64(fd, -4096, 1) < 0 ||
		    read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
			goto second_fail; /* Cannot find leading superblock */
		if (bsb.magic[15] == '1')
			bsbsize = offsetof(struct mdp_backup_super, pad1);
		else
			bsbsize = offsetof(struct mdp_backup_super, pad);
		if (memcmp(&bsb2, &bsb, bsbsize) != 0)
			goto second_fail; /* Cannot find leading superblock */

		/* Now need the data offsets for all devices. */
		offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks);
		for(j=0; j<info->array.raid_disks; j++) {
			if (fdlist[j] < 0)
				continue;
			if (st->ss->load_super(st, fdlist[j], NULL))
				/* FIXME should be this be an error */
				continue;
			st->ss->getinfo_super(st, &dinfo, NULL);
			st->ss->free_super(st);
			offsets[j] = dinfo.data_offset * 512;
		}
		printf(Name ": restoring critical section\n");

		if (restore_stripes(fdlist, offsets,
				    info->array.raid_disks,
				    info->new_chunk,
				    info->new_level,
				    info->new_layout,
				    fd, __le64_to_cpu(bsb.devstart)*512,
				    __le64_to_cpu(bsb.arraystart)*512,
				    __le64_to_cpu(bsb.length)*512, NULL)) {
			/* didn't succeed, so giveup */
			if (verbose)
				pr_err("Error restoring backup from %s\n",
					devname);
			free(offsets);
			return 1;
		}

		if (bsb.magic[15] == '2' &&
		    restore_stripes(fdlist, offsets,
				    info->array.raid_disks,
				    info->new_chunk,
				    info->new_level,
				    info->new_layout,
				    fd, __le64_to_cpu(bsb.devstart)*512 +
				    __le64_to_cpu(bsb.devstart2)*512,
				    __le64_to_cpu(bsb.arraystart2)*512,
				    __le64_to_cpu(bsb.length2)*512, NULL)) {
			/* didn't succeed, so giveup */
			if (verbose)
				pr_err("Error restoring second backup from %s\n",
					devname);
			free(offsets);
			return 1;
		}

		free(offsets);

		/* Ok, so the data is restored. Let's update those superblocks. */

		lo = hi = 0;
		if (bsb.length) {
			lo = __le64_to_cpu(bsb.arraystart);
			hi = lo + __le64_to_cpu(bsb.length);
		}
		if (bsb.magic[15] == '2' && bsb.length2) {
			unsigned long long lo1, hi1;
			lo1 = __le64_to_cpu(bsb.arraystart2);
			hi1 = lo1 + __le64_to_cpu(bsb.length2);
			if (lo == hi) {
				lo = lo1;
				hi = hi1;
			} else if (lo < lo1)
				hi = hi1;
			else
				lo = lo1;
		}
		if (lo < hi &&
		    (info->reshape_progress < lo ||
		     info->reshape_progress > hi))
			/* backup does not affect reshape_progress*/ ;
		else if (info->delta_disks >= 0) {
			info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
				__le64_to_cpu(bsb.length);
			if (bsb.magic[15] == '2') {
				unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
					__le64_to_cpu(bsb.length2);
				if (p2 > info->reshape_progress)
					info->reshape_progress = p2;
			}
		} else {
			info->reshape_progress = __le64_to_cpu(bsb.arraystart);
			if (bsb.magic[15] == '2') {
				unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
				if (p2 < info->reshape_progress)
					info->reshape_progress = p2;
			}
		}
		for (j=0; j<info->array.raid_disks; j++) {
			if (fdlist[j] < 0)
				continue;
			if (st->ss->load_super(st, fdlist[j], NULL))
				continue;
			st->ss->getinfo_super(st, &dinfo, NULL);
			dinfo.reshape_progress = info->reshape_progress;
			st->ss->update_super(st, &dinfo,
					     "_reshape_progress",
					     NULL,0, 0, NULL);
			st->ss->store_super(st, fdlist[j]);
			st->ss->free_super(st);
		}
		return 0;
	}
	/* Didn't find any backup data, try to see if any
	 * was needed.
	 */
	if (info->delta_disks < 0) {
		/* When shrinking, the critical section is at the end.
		 * So see if we are before the critical section.
		 */
		unsigned long long first_block;
		nstripe = ostripe = 0;
		first_block = 0;
		while (ostripe >= nstripe) {
			ostripe += info->array.chunk_size / 512;
			first_block = ostripe * odata;
			nstripe = first_block / ndata / (info->new_chunk/512) *
				(info->new_chunk/512);
		}

		if (info->reshape_progress >= first_block)
			return 0;
	}
	if (info->delta_disks > 0) {
		/* See if we are beyond the critical section. */
		unsigned long long last_block;
		nstripe = ostripe = 0;
		last_block = 0;
		while (nstripe >= ostripe) {
			nstripe += info->new_chunk / 512;
			last_block = nstripe * ndata;
			ostripe = last_block / odata / (info->array.chunk_size/512) *
				(info->array.chunk_size/512);
		}

		if (info->reshape_progress >= last_block)
			return 0;
	}
	/* needed to recover critical section! */
	if (verbose)
		pr_err("Failed to find backup of critical section\n");
	return 1;
}

int Grow_continue_command(char *devname, int fd,
			  char *backup_file, int verbose)
{
	int ret_val = 0;
	struct supertype *st = NULL;
	struct mdinfo *content = NULL;
	struct mdinfo array;
	char *subarray = NULL;
	struct mdinfo *cc = NULL;
	struct mdstat_ent *mdstat = NULL;
	int cfd = -1;
	int fd2 = -1;

	dprintf("Grow continue from command line called for %s\n",
		devname);

	st = super_by_fd(fd, &subarray);
	if (!st || !st->ss) {
		pr_err("Unable to determine metadata format for %s\n",
		       devname);
		return 1;
	}
	dprintf("Grow continue is run for ");
	if (st->ss->external == 0) {
		int d;
		dprintf("native array (%s)\n", devname);
		if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
			pr_err("%s is not an active md array -"
				" aborting\n", devname);
			ret_val = 1;
			goto Grow_continue_command_exit;
		}
		content = &array;
		/* Need to load a superblock.
		 * FIXME we should really get what we need from
		 * sysfs
		 */
		for (d = 0; d < MAX_DISKS; d++) {
			mdu_disk_info_t disk;
			char *dv;
			int err;
			disk.number = d;
			if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
				continue;
			if (disk.major == 0 && disk.minor == 0)
				continue;
			if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
				continue;
			dv = map_dev(disk.major, disk.minor, 1);
			if (!dv)
				continue;
			fd2 = dev_open(dv, O_RDONLY);
			if (fd2 < 0)
				continue;
			err = st->ss->load_super(st, fd2, NULL);
			close(fd2);
			if (err)
				continue;
			break;
		}
		if (d == MAX_DISKS) {
			pr_err("Unable to load metadata for %s\n",
			       devname);
			ret_val = 1;
			goto Grow_continue_command_exit;
		}
		st->ss->getinfo_super(st, content, NULL);
	} else {
		char *container;

		if (subarray) {
			dprintf("subarray (%s)\n", subarray);
			container = st->container_devnm;
			cfd = open_dev_excl(st->container_devnm);
		} else {
			container = st->devnm;
			close(fd);
			cfd = open_dev_excl(st->devnm);
			dprintf("container (%s)\n", container);
			fd = cfd;
		}
		if (cfd < 0) {
			pr_err("Unable to open container "
				"for %s\n", devname);
			ret_val = 1;
			goto Grow_continue_command_exit;
		}

		/* find in container array under reshape
		 */
		ret_val = st->ss->load_container(st, cfd, NULL);
		if (ret_val) {
			pr_err("Cannot read superblock for %s\n",
			       devname);
			ret_val = 1;
			goto Grow_continue_command_exit;
		}

		cc = st->ss->container_content(st, subarray);
		for (content = cc; content ; content = content->next) {
			char *array;
			int allow_reshape = 1;

			if (content->reshape_active == 0)
				continue;
			/* The decision about array or container wide
			 * reshape is taken in Grow_continue based
			 * content->reshape_active state, therefore we
			 * need to check_reshape based on
			 * reshape_active and subarray name
			 */
			if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
				allow_reshape = 0;
			if (content->reshape_active == CONTAINER_RESHAPE &&
			    (content->array.state
			     & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))
				allow_reshape = 0;

			if (!allow_reshape) {
				pr_err("cannot continue reshape of an array"
				       " in container with unsupported"
				       " metadata: %s(%s)\n",
				       devname, container);
				ret_val = 1;
				goto Grow_continue_command_exit;
			}

			array = strchr(content->text_version+1, '/')+1;
			mdstat = mdstat_by_subdev(array, container);
			if (!mdstat)
				continue;
			if (mdstat->active == 0) {
				pr_err("Skipping inactive array %s.\n",
				       mdstat->devnm);
				free_mdstat(mdstat);
				mdstat = NULL;
				continue;
			}
			break;
		}
		if (!content) {
			pr_err("Unable to determine reshaped "
			       "array for %s\n", devname);
			ret_val = 1;
			goto Grow_continue_command_exit;
		}
		fd2 = open_dev(mdstat->devnm);
		if (fd2 < 0) {
			pr_err("cannot open (%s)\n", mdstat->devnm);
			ret_val = 1;
			goto Grow_continue_command_exit;
		}

		sysfs_init(content, fd2, mdstat->devnm);

		/* start mdmon in case it is not running
		 */
		if (!mdmon_running(container))
			start_mdmon(container);
		ping_monitor(container);

		if (mdmon_running(container))
			st->update_tail = &st->updates;
		else {
			pr_err("No mdmon found. "
				"Grow cannot continue.\n");
			ret_val = 1;
			goto Grow_continue_command_exit;
		}
	}

	/* verify that array under reshape is started from
	 * correct position
	 */
	if (verify_reshape_position(content, content->array.level) < 0) {
		ret_val = 1;
		goto Grow_continue_command_exit;
	}

	/* continue reshape
	 */
	ret_val = Grow_continue(fd, st, content, backup_file, 0);

Grow_continue_command_exit:
	if (fd2 > -1)
		close(fd2);
	if (cfd > -1)
		close(cfd);
	st->ss->free_super(st);
	free_mdstat(mdstat);
	sysfs_free(cc);
	free(subarray);

	return ret_val;
}

int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
		  char *backup_file, int freeze_reshape)
{
	int ret_val = 2;

	if (!info->reshape_active)
		return ret_val;

	if (st->ss->external) {
		int cfd = open_dev(st->container_devnm);

		if (cfd < 0)
			return 1;

		st->ss->load_container(st, cfd, st->container_devnm);
		close(cfd);
		ret_val = reshape_container(st->container_devnm, NULL, mdfd,
					    st, info, 0, backup_file,
					    0,
					    1 | info->reshape_active,
					    freeze_reshape);
	} else
		ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
					NULL, 0ULL, backup_file, 0, 0,
					1 | info->reshape_active,
					freeze_reshape);

	return ret_val;
}