summaryrefslogtreecommitdiffstats
path: root/configs/fedora/generic/arm/CONFIG_POWER_RESET_SYSCON
diff options
context:
space:
mode:
Diffstat (limited to 'configs/fedora/generic/arm/CONFIG_POWER_RESET_SYSCON')
-rw-r--r--configs/fedora/generic/arm/CONFIG_POWER_RESET_SYSCON1
1 files changed, 1 insertions, 0 deletions
diff --git a/configs/fedora/generic/arm/CONFIG_POWER_RESET_SYSCON b/configs/fedora/generic/arm/CONFIG_POWER_RESET_SYSCON
new file mode 100644
index 000000000..d0db15703
--- /dev/null
+++ b/configs/fedora/generic/arm/CONFIG_POWER_RESET_SYSCON
@@ -0,0 +1 @@
+CONFIG_POWER_RESET_SYSCON=y
f='#n177'>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
/*
 * mdadm - manage Linux "md" devices aka RAID arrays.
 *
 * Copyright (C) 2001-2009 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>

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


	if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
		fprintf(stderr, Name ": cannot get array info for %s\n", devname);
		return 1;
	}

	st = super_by_fd(fd);
	if (!st) {
		fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version);
		return 1;
	}

	if (info.array.level != -1) {
		fprintf(stderr, Name ": can only add devices to linear arrays\n");
		return 1;
	}

	nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
	if (nfd < 0) {
		fprintf(stderr, Name ": cannot open %s\n", newdev);
		return 1;
	}
	fstat(nfd, &stb);
	if ((stb.st_mode & S_IFMT) != S_IFBLK) {
		fprintf(stderr, Name ": %s is not a block device!\n", newdev);
		close(nfd);
		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;

		disk.number = d;
		if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
			fprintf(stderr, Name ": cannot get device detail for device %d\n",
				d);
			return 1;
		}
		dv = map_dev(disk.major, disk.minor, 1);
		if (!dv) {
			fprintf(stderr, Name ": cannot find device file for device %d\n",
				d);
			return 1;
		}
		fd2 = dev_open(dv, O_RDWR);
		if (!fd2) {
			fprintf(stderr, Name ": cannot open device file %s\n", dv);
			return 1;
		}
		st->ss->free_super(st);

		if (st->ss->load_super(st, fd2, NULL)) {
			fprintf(stderr, Name ": cannot find super block on %s\n", dv);
			close(fd2);
			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)) {
		fprintf(stderr, Name ": Cannot store new superblock on %s\n",
			newdev);
		close(nfd);
		return 1;
	}
	close(nfd);

	if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
		fprintf(stderr, Name ": 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) {
		fprintf(stderr, Name ": 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) {
			fprintf(stderr, Name ": cannot get device detail for device %d\n",
				d);
			return 1;
		}
		dv = map_dev(disk.major, disk.minor, 1);
		if (!dv) {
			fprintf(stderr, Name ": cannot find device file for device %d\n",
				d);
			return 1;
		}
		fd2 = dev_open(dv, O_RDWR);
		if (fd2 < 0) {
			fprintf(stderr, Name ": cannot open device file %s\n", dv);
			return 1;
		}
		if (st->ss->load_super(st, fd2, NULL)) {
			fprintf(stderr, Name ": 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)) {
			fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
			close(fd2);
			return 1;
		}
		close(fd2);
	}

	return 0;
}

int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force)
{
	/*
	 * 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;
	int major = BITMAP_MAJOR_HI;
	int vers = md_get_version(fd);
	unsigned long long bitmapsize, array_size;

	if (vers < 9003) {
		major = BITMAP_MAJOR_HOSTENDIAN;
#ifdef __BIG_ENDIAN
		fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
			"  between different architectured.  Consider upgrading the Linux kernel.\n");
#endif
	}

	if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
		if (errno == ENOMEM)
			fprintf(stderr, Name ": Memory allocation failure.\n");
		else
			fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
		return 1;
	}
	if (bmf.pathname[0]) {
		if (strcmp(file,"none")==0) {
			if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
				fprintf(stderr, Name ": failed to remove bitmap %s\n",
					bmf.pathname);
				return 1;
			}
			return 0;
		}
		fprintf(stderr, Name ": %s already has a bitmap (%s)\n",
			devname, bmf.pathname);
		return 1;
	}
	if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
		fprintf(stderr, Name ": cannot get array status for %s\n", devname);
		return 1;
	}
	if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
		if (strcmp(file, "none")==0) {
			array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
			if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
				fprintf(stderr, Name ": failed to remove internal bitmap.\n");
				return 1;
			}
			return 0;
		}
		fprintf(stderr, Name ": Internal bitmap already present on %s\n",
			devname);
		return 1;
	}
	if (array.level <= 0) {
		fprintf(stderr, Name ": 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) {
		fprintf(stderr, Name ": 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);
	if (!st) {
		fprintf(stderr, Name ": Cannot understand version %d.%d\n",
			array.major_version, array.minor_version);
		return 1;
	}
	if (strcmp(file, "none") == 0) {
		fprintf(stderr, Name ": no bitmap found on %s\n", devname);
		return 1;
	} else if (strcmp(file, "internal") == 0) {
		int d;
		if (st->ss->add_internal_bitmap == NULL) {
			fprintf(stderr, Name ": Internal bitmaps not supported "
				"with %s metadata\n", st->ss->name);
			return 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,
						    &chunk, delay, write_behind,
						    bitmapsize, 0, major)
						)
						st->ss->write_bitmap(st, fd2);
					else {
						fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
						close(fd2);
						return 1;
					}
				}
				close(fd2);
			}
		}
		array.state |= (1<<MD_SB_BITMAP_PRESENT);
		if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
			fprintf(stderr, Name ": 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 &&
			    st->ss->load_super(st, fd2, NULL) == 0) {
				close(fd2);
				st->ss->uuid_from_super(st, uuid);
				break;
			}
			close(fd2);
		}
		if (d == max_devs) {
			fprintf(stderr, Name ": cannot find UUID for array!\n");
			return 1;
		}
		if (CreateBitmap(file, force, (char*)uuid, chunk,
				 delay, write_behind, bitmapsize, major)) {
			return 1;
		}
		bitmap_fd = open(file, O_RDWR);
		if (bitmap_fd < 0) {
			fprintf(stderr, Name ": weird: %s cannot be opened\n",
				file);
			return 1;
		}
		if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
			fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
				devname, strerror(errno));
			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;

int 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 child_grow(int afd, struct mdinfo *sra, unsigned long blocks,
		      int *fds, unsigned long long *offsets,
		      int disks, int chunk, int level, int layout, int data,
		      int dests, int *destfd, unsigned long long *destoffsets);
static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks,
			int *fds, unsigned long long *offsets,
			int disks, int chunk, int level, int layout, int data,
			int dests, int *destfd, unsigned long long *destoffsets);
static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks,
			   int *fds, unsigned long long *offsets,
			   unsigned long long start,
			   int disks, int chunk, int level, int layout, int data,
			   int dests, int *destfd, unsigned long long *destoffsets);

int freeze_array(struct mdinfo *sra)
{
	/* Try to freeze resync on this array.
	 * Return -1 if the array is busy,
	 * return 0 if this kernel doesn't support 'frozen'
	 * return 1 if it worked.
	 */
	char buf[20];
	if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
		return 0;
	if (strcmp(buf, "idle\n") != 0 &&
	    strcmp(buf, "frozen\n") != 0)
		return -1;
	if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
		return 0;
	return 1;
}

void unfreeze_array(struct mdinfo *sra, int frozen)
{
	/* If 'frozen' is 1, unfreeze the array */
	if (frozen > 0)
		sysfs_set_str(sra, NULL, "sync_action", "idle");
}

void wait_reshape(struct mdinfo *sra)
{
	int fd = sysfs_get_fd(sra, NULL, "sync_action");
	char action[20];

	do {
		fd_set rfds;
		FD_ZERO(&rfds);
		FD_SET(fd, &rfds);
		select(fd+1, NULL, NULL, &rfds, NULL);
		
		if (sysfs_fd_get_str(fd, action, 20) < 0) {
			close(fd);
			return;
		}
	} while  (strncmp(action, "reshape", 7) == 0);
}
			
		
int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
		 long long size,
		 int level, char *layout_str, int chunksize, int raid_disks)
{
	/* 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, orig;
	char *c;
	int rv = 0;
	struct supertype *st;

	int nchunk, ochunk;
	int nlayout, olayout;
	int ndisks, odisks;
	int ndata, odata;
	int orig_level = UnSet;
	char alt_layout[40];
	int *fdlist;
	unsigned long long *offsets;
	int d, i;
	int nrdisks;
	int err;
	int frozen;
	unsigned long a,b, blocks, stripes;
	int cache;
	unsigned long long array_size;
	int changed = 0;
	int done;

	struct mdinfo *sra;
	struct mdinfo *sd;

	if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
		fprintf(stderr, Name ": %s is not an active md array - aborting\n",
			devname);
		return 1;
	}

	if (size >= 0 &&
	    (chunksize || level!= UnSet || layout_str || raid_disks)) {
		fprintf(stderr, Name ": 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 (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
	    get_linux_version() < 2006032 &&
	    !check_env("MDADM_FORCE_FEWER")) {
		fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
			"       Please use a newer kernel\n");
		return 1;
	}
	sra = sysfs_read(fd, 0, GET_LEVEL);
	if (sra)
		frozen = freeze_array(sra);
	else {
		fprintf(stderr, Name ": failed to read sysfs parameters for %s\n",
			devname);
		return 1;
	}
	if (frozen < 0) {
		fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
			" be reshaped\n", devname);
		return 1;
	}

	/* ========= set size =============== */
	if (size >= 0 && (size == 0 || size != array.size)) {
		array.size = size;
		if (array.size != size) {
			/* got truncated to 32bit, write to
			 * component_size instead
			 */
			if (sra)
				rv = sysfs_set_num(sra, NULL,
						   "component_size", size);
			else
				rv = -1;
		} else
			rv = ioctl(fd, SET_ARRAY_INFO, &array);
		if (rv != 0) {
			int err = errno;
			fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
				devname, strerror(err));
			if (err == EBUSY && 
			    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
				fprintf(stderr, "       Bitmap must be removed before size can be changed\n");
			rv = 1;
			goto release;
		}
		ioctl(fd, GET_ARRAY_INFO, &array);
		size = get_component_size(fd)/2;
		if (size == 0)
			size = array.size;
		if (!quiet)
			fprintf(stderr, Name ": component size of %s has been set to %lluK\n",
				devname, size);
		changed = 1;
	} else {
		size = get_component_size(fd)/2;
		if (size == 0)
			size = array.size;
	}

	/* ======= set level =========== */
	if (level != UnSet && level != array.level) {
		/* Trying to change the level.
		 * We might need to change layout first and schedule a
		 * level change for later.
		 * Level changes that can happen immediately are:
		 * 0->4,5,6  1->5  4->5,6  5->1,6
		 * Level changes that need a layout change first are:
		 * 6->5,4,0 : need a -6 layout, or parity-last
		 * 5->4,0   : need parity-last
		 */
		if ((array.level == 6 || array.level == 5) &&
		    (level == 5 || level == 4 || level == 0)) {
			/* Don't change level yet, but choose intermediate
			 * layout
			 */
			if (level == 5) {
				if (layout_str == NULL)
					switch (array.layout) {
					case ALGORITHM_LEFT_ASYMMETRIC:
					case ALGORITHM_LEFT_ASYMMETRIC_6:
					case ALGORITHM_ROTATING_N_RESTART:
						layout_str = "left-asymmetric-6";
						break;
					case ALGORITHM_LEFT_SYMMETRIC:
					case ALGORITHM_LEFT_SYMMETRIC_6:
					case ALGORITHM_ROTATING_N_CONTINUE:
						layout_str = "left-symmetric-6";
						break;
					case ALGORITHM_RIGHT_ASYMMETRIC:
					case ALGORITHM_RIGHT_ASYMMETRIC_6:
					case ALGORITHM_ROTATING_ZERO_RESTART:
						layout_str = "right-asymmetric-6";
						break;
					case ALGORITHM_RIGHT_SYMMETRIC:
					case ALGORITHM_RIGHT_SYMMETRIC_6:
						layout_str = "right-symmetric-6";
						break;
					case ALGORITHM_PARITY_0:
					case ALGORITHM_PARITY_0_6:
						layout_str = "parity-first-6";
						break;
					case ALGORITHM_PARITY_N:
						layout_str = "parity-last";
						break;
					default:
						fprintf(stderr, Name ": %s: cannot"
							"convert layout to RAID5 equivalent\n",
							devname);
						rv = 1;
						goto release;
					}
				else {
					int l = map_name(r5layout, layout_str);
					if (l == UnSet) {
						fprintf(stderr, Name ": %s: layout '%s' not recognised\n",
							devname, layout_str);
						rv = 1;
						goto release;
					}
					if (l != ALGORITHM_PARITY_N) {
						/* need the -6 version */
						char *ls = map_num(r5layout, l);
						strcat(strcpy(alt_layout, ls),
						       "-6");
						layout_str = alt_layout;
					}
				}
				if (raid_disks)
					/* The final raid6->raid5 conversion
					 * will reduce the number of disks,
					 * so now we need to aim higher
					 */
					raid_disks++;
			} else
				layout_str = "parity-last";
		} else {
			c = map_num(pers, level);
			if (c == NULL) {
				rv = 1;/* not possible */
				goto release;
			}
			err = sysfs_set_str(sra, NULL, "level", c);
			if (err) {
				err = errno;
				fprintf(stderr, Name ": %s: could not set level to %s\n",
					devname, c);
				if (err == EBUSY && 
				    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
					fprintf(stderr, "       Bitmap must be removed before level can be changed\n");
				rv = 1;
				goto release;
			}
			orig = array;
			orig_level = orig.level;
			ioctl(fd, GET_ARRAY_INFO, &array);
			if (layout_str == NULL &&
			    orig.level == 5 && level == 6 &&
			    array.layout != orig.layout)
				layout_str = map_num(r5layout, orig.layout);
			if (!quiet)
				fprintf(stderr, Name " level of %s changed to %s\n",
					devname, c);
			changed = 1;
		}
	}

	/* ========= set shape (chunk_size / layout / ndisks)  ============== */
	/* Check if layout change is a no-op */
	if (layout_str) switch(array.level) {
	case 5:
		if (array.layout == map_name(r5layout, layout_str))
			layout_str = NULL;
		break;
	case 6:
		if (layout_str == NULL &&
		    ((chunksize && chunksize * 1024 != array.chunk_size) ||
		     (raid_disks && raid_disks != array.raid_disks)) &&
		    array.layout >= 16) {
			fprintf(stderr, Name
				": %s has a non-standard layout.  If you wish to preserve this\n"
				"      during the reshape, please specify --layout=preserve\n"
				"      If you want to change it, specify a layout or use --layout=normalise\n",
				devname);
			rv = 1;
			goto release;
		}
		if (strcmp(layout_str, "normalise") == 0 ||
		    strcmp(layout_str, "normalize") == 0) {
			char *hyphen;
			strcpy(alt_layout, map_num(r6layout, array.layout));
			hyphen = strrchr(alt_layout, '-');
			if (hyphen && strcmp(hyphen, "-6") == 0) {
				*hyphen = 0;
				layout_str = alt_layout;
			}
		}

		if (array.layout == map_name(r6layout, layout_str))
			layout_str = NULL;
		if (layout_str && strcmp(layout_str, "preserve") == 0)
			layout_str = NULL;
		break;
	}
	if (layout_str == NULL
	    && (chunksize == 0 || chunksize*1024 == array.chunk_size)
	    && (raid_disks == 0 || raid_disks == array.raid_disks)) {
		rv = 0;
		if (level != UnSet && level != array.level) {
			/* Looks like this level change doesn't need
			 * a reshape after all.
			 */
			c = map_num(pers, level);
			if (c) {
				rv = sysfs_set_str(sra, NULL, "level", c);
				if (rv) {
					int err = errno;
					fprintf(stderr, Name ": %s: could not set level to %s\n",
						devname, c);
					if (err == EBUSY && 
					    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
						fprintf(stderr, "       Bitmap must be removed before level can be changed\n");
				}
			}
		} else if (!changed && !quiet)
			fprintf(stderr, Name ": %s: no change requested\n",
				devname);
		goto release;
	}

	c = map_num(pers, array.level);
	if (c == NULL) c = "-unknown-";
	switch(array.level) {
	default: /* raid0, linear, multipath cannot be reconfigured */
		fprintf(stderr, Name ": %s array %s cannot be reshaped.\n",
			c, devname);
		rv = 1;
		break;

	case LEVEL_FAULTY: /* only 'layout' change is permitted */

		if (chunksize  || raid_disks) {
			fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n",
				devname);
			rv = 1;
			break;
		}
		if (layout_str == NULL)
			break; /* nothing to do.... */

		array.layout = parse_layout_faulty(layout_str);
		if (array.layout < 0) {
			int rv;
			fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n",
				devname, layout_str);
			rv = 1;
			break;
		}
		if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
			fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
				devname, strerror(errno));
			rv = 1;
		} else if (!quiet)
			printf("layout for %s set to %d\n", devname, array.layout);
		break;

	case 1: /* only raid_disks can each be changed. */

		if (chunksize || layout_str != NULL) {
			fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n",
				devname);
			rv = 1;
			break;
		}
		if (raid_disks > 0) {
			array.raid_disks = raid_disks;
			if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
				fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n",
					devname, strerror(errno));
				rv = 1;
			}
		}
		break;

	case 4:
	case 5:
	case 6:

		/*
		 * layout/chunksize/raid_disks can be changed
		 * though the kernel may not support it all.
		 */
		st = super_by_fd(fd);

		/*
		 * 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.
		 *
		 * 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.
		 *
		 *    We backup data by writing it to one spare, or to a
		 *    file which was given on command line.
		 *
		 *    [FOLLOWING IS OLD AND PARTLY WRONG]
		 *    So: we enumerate the devices in the array and
		 *    make sure we can open all of them.
		 *    Then we freeze the early part of the array and
		 *    backup to the various spares.
		 *    Then we request changes and start the reshape.
		 *    Monitor progress until it has passed the danger zone.
		 *    and finally invalidate the copied data and unfreeze the
		 *    start of the array.
		 *
		 * In each case, we first make sure that storage is available
		 * for the required backup.
		 * Then we:
		 *   -  request the shape change.
		 *   -  for to handle backup etc.
		 */
		nchunk = ochunk = array.chunk_size;
		nlayout = olayout = array.layout;
		ndisks = odisks = array.raid_disks;

		if (chunksize) {
			nchunk = chunksize * 1024;
			if (size % chunksize) {
				fprintf(stderr, Name ": component size %lluK is not"
					" a multiple of chunksize %dK\n",
					size, chunksize);
				break;
			}
		}
		if (layout_str != NULL)
			switch(array.level) {
			case 4: /* ignore layout */
				break;
			case 5:
				nlayout = map_name(r5layout, layout_str);
				if (nlayout == UnSet) {
					fprintf(stderr, Name ": layout %s not understood for raid5.\n",
						layout_str);
					rv = 1;
					goto release;
				}
				break;

			case 6:
				nlayout = map_name(r6layout, layout_str);
				if (nlayout == UnSet) {
					fprintf(stderr, Name ": layout %s not understood for raid6.\n",
						layout_str);
					rv = 1;
					goto release;
				}
				break;
			}
		if (raid_disks) ndisks = raid_disks;

		odata = odisks-1;
		ndata = ndisks-1;
		if (array.level == 6) {
			odata--; /* number of data disks */
			ndata--;
		}

		if (odata == ndata &&
		    get_linux_version() < 2006032) {
			fprintf(stderr, Name ": in-place reshape is not safe before 2.6.32, sorry.\n");
			break;
		}

		/* Check that we can hold all the data */
		get_dev_size(fd, NULL, &array_size);
		if (ndata * size < (array_size/1024)) {
			fprintf(stderr, Name ": 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, ndata * size);
			rv = 1;
			break;
		}

		/* 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 */
		while (a != b) {
			if (a < b)
				b -= a;
			if (b < a)
				a -= b;
		}
		/* LCM == product / GCD */
		blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;

		sysfs_free(sra);
		sra = sysfs_read(fd, 0,
				 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
				 GET_CACHE);

		if (!sra) {
			fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
				devname);
			rv = 1;
			break;
		}

		if (ndata == odata) {
			/* 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
			fprintf(stderr, Name ": Need to backup %luK of critical "
				"section..\n", blocks/2);

		if (blocks >= sra->component_size/2) {
			fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n",
				devname);
			rv = 1;
			break;
		}
		nrdisks = array.nr_disks + sra->array.spare_disks;
		/* Now we need to open all these devices so we can read/write.
		 */
		fdlist = malloc((1+nrdisks) * sizeof(int));
		offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
		if (!fdlist || !offsets) {
			fprintf(stderr, Name ": malloc failed: grow aborted\n");
			rv = 1;
			break;
		}
		for (d=0; d <= nrdisks; d++)
			fdlist[d] = -1;
		d = array.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) {
					fprintf(stderr, Name ": %s: cannot open component %s\n",
						devname, dn?dn:"-unknown-");
					rv = 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) {
					fprintf(stderr, Name ": %s: cannot open component %s\n",
						devname, dn?dn:"-unknown");
					rv = 1;
					goto release;
				}
				d++;
			}
		}
		if (backup_file == NULL) {
			if (ndata <= odata) {
				fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n",
					devname);
				rv = 1;
				break;
			} else if (sra->array.spare_disks == 0) {
				fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
					"backup-file to backup critical section\n",
					devname);
				rv = 1;
				break;
			}
			if (d == array.raid_disks) {
				fprintf(stderr, Name ": %s: No spare device for backup\n",
					devname);
				rv = 1;
				break;
			}
		} else {
			/* need to check backup file is large enough */
			char buf[512];
			fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
				     S_IRUSR | S_IWUSR);
			offsets[d] = 8 * 512;
			if (fdlist[d] < 0) {
				fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
					devname, backup_file, strerror(errno));
				rv = 1;
				break;
			}
			memset(buf, 0, 512);
			for (i=0; i < blocks + 1 ; i++) {
				if (write(fdlist[d], buf, 512) != 512) {
					fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
						devname, backup_file, strerror(errno));
					rv = 1;
					break;
				}
			}
			if (fsync(fdlist[d]) != 0) {
				fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
					devname, backup_file, strerror(errno));
				rv = 1;
				break;
			}
			d++;
		}

		/* lastly, check that the internal stripe cache is
		 * large enough, or it won't work.
		 */
		
		cache = (nchunk < ochunk) ? ochunk : nchunk;
		cache = cache * 4 / 4096;
		if (cache < blocks / 8 / odisks + 16)
			/* Make it big enough to hold 'blocks' */
			cache = blocks / 8 / odisks + 16;
		if (sra->cache_size < cache)
			sysfs_set_num(sra, NULL, "stripe_cache_size",
				      cache+1);
		/* Right, everything seems fine. Let's kick things off.
		 * If only changing raid_disks, use ioctl, else use
		 * sysfs.
		 */
		if (ochunk == nchunk && olayout == nlayout) {
			array.raid_disks = ndisks;
			if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
				int err = errno;
				rv = 1;
				fprintf(stderr, Name ": Cannot set device shape for %s: %s\n",
					devname, strerror(errno));
				if (ndisks < odisks &&
				    get_linux_version() < 2006030)
					fprintf(stderr, Name ": linux 2.6.30 or later required\n");
				if (err == EBUSY && 
				    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
					fprintf(stderr, "       Bitmap must be removed before shape can be changed\n");

				break;
			}
		} else {
			/* set them all just in case some old 'new_*' value
			 * persists from some earlier problem
			 */
			int err = err; /* only used if rv==1, and always set if
					* rv==1, so initialisation not needed,
					* despite gcc warning
					*/
			if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0)
				rv = 1, err = errno;
			if (!rv && sysfs_set_num(sra, NULL, "layout", nlayout) < 0)
				rv = 1, err = errno;
			if (!rv && sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0)
				rv = 1, err = errno;
			if (rv) {
				fprintf(stderr, Name ": Cannot set device shape for %s\n",
					devname);
				if (get_linux_version() < 2006030)
					fprintf(stderr, Name ": linux 2.6.30 or later required\n");
				if (err == EBUSY && 
				    (array.state & (1<<MD_SB_BITMAP_PRESENT)))
					fprintf(stderr, "       Bitmap must be removed before shape can be changed\n");
				break;
			}
		}

		if (ndisks == 2 && odisks == 2) {
			/* No reshape is needed in this trivial case */
			rv = 0;
			break;
		}

		/* 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) {
			fprintf(stderr, Name ": %s: Cannot find a superblock\n",
				devname);
			rv = 1;
			break;
		}

		memset(&bsb, 0, 512);
		memcpy(bsb.magic, "md_backup_data-1", 16);
		st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
		bsb.mtime = __cpu_to_le64(time(0));
		bsb.devstart2 = blocks;
		stripes = blocks / (ochunk/512) / odata;
		/* 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(fork()) {
		case 0:
			close(fd);
			if (check_env("MDADM_GROW_VERIFY"))
				fd = open(devname, O_RDONLY | O_DIRECT);
			else
				fd = -1;
			mlockall(MCL_FUTURE);

			if (odata < ndata)
				done = child_grow(fd, sra, stripes,
						  fdlist, offsets,
						  odisks, ochunk, array.level, olayout, odata,
						  d - odisks, fdlist+odisks, offsets+odisks);
			else if (odata > ndata)
				done = child_shrink(fd, sra, stripes,
						    fdlist, offsets,
						    odisks, ochunk, array.level, olayout, odata,
						    d - odisks, fdlist+odisks, offsets+odisks);
			else
				done = child_same_size(fd, sra, stripes,
						       fdlist, offsets,
						       0,
						       odisks, ochunk, array.level, olayout, odata,
						       d - odisks, fdlist+odisks, offsets+odisks);
			if (backup_file && done)
				unlink(backup_file);
			if (level != UnSet && level != array.level) {
				/* We need to wait for the reshape to finish
				 * (which will have happened unless odata < ndata)
				 * and then set the level
				 */

				c = map_num(pers, level);
				if (c == NULL)
					exit(0);/* not possible */

				if (odata < ndata)
					wait_reshape(sra);
				err = sysfs_set_str(sra, NULL, "level", c);
				if (err)
					fprintf(stderr, Name ": %s: could not set level to %s\n",
						devname, c);
			}
			exit(0);
		case -1:
			fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
				strerror(errno));
			rv = 1;
			break;
		default:
			/* The child will take care of unfreezing the array */
			frozen = 0;
			break;
		}
		break;

	}

 release:
	if (rv && orig_level != UnSet && sra) {
		c = map_num(pers, orig_level);
		if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
			fprintf(stderr, Name ": aborting level change\n");
	}
	if (sra)
		unfreeze_array(sra, frozen);
	return rv;
}

/*
 * 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.
 * 
 */

/* FIXME return status is never checked */
int grow_backup(struct mdinfo *sra,
		unsigned long long offset, /* per device */
		unsigned long stripes, /* per device */
		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 new_degraded;
	//printf("offset %llu\n", offset);
	if (level >= 4)
		odata--;
	if (level == 6)
		odata--;
	sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * (chunk/512)) * odata);
	/* Check that array hasn't become degraded, else we might backup the wrong data */
	sysfs_get_ll(sra, NULL, "degraded", &new_degraded);
	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));

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

	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 */
int wait_backup(struct mdinfo *sra,
		unsigned long long offset, /* per device */
		unsigned long long blocks, /* per device */
		unsigned long long blocks2, /* per device - hack */
		int dests, int *destfd, unsigned long long *destoffsets,
		int part)
{
	/* Wait for resync to pass the section that was backed up
	 * then erase the backup and allow IO
	 */
	int fd = sysfs_get_fd(sra, NULL, "sync_completed");
	unsigned long long completed;
	int i;
	int rv;

	if (fd < 0)
		return -1;
	sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2);
	if (offset == 0)
		sysfs_set_str(sra, NULL, "sync_action", "reshape");
	do {
		char action[20];
		fd_set rfds;
		FD_ZERO(&rfds);
		FD_SET(fd, &rfds);
		select(fd+1, NULL, NULL, &rfds, NULL);
		if (sysfs_fd_get_ll(fd, &completed) < 0) {
			close(fd);
			return -1;
		}
		if (sysfs_get_str(sra, NULL, "sync_action",
				  action, 20) > 0 &&
		    strncmp(action, "reshape", 7) != 0)
			break;
	} while (completed < offset + blocks);
	close(fd);

	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 (lseek64(destfd[i], destoffsets[i]-4096, 0) !=
		    destoffsets[i]-4096)
			rv = 1;
		rv = rv ?: write(destfd[i], &bsb, 512);
		fsync(destfd[i]);
	}
	return rv;
}

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

static char *abuf, *bbuf;
static int 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 (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 (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 = malloc(abuflen);
			bbuf = malloc(abuflen);
		}

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

static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes,
		      int *fds, unsigned long long *offsets,
		      int disks, int chunk, int level, int layout, int data,
		      int dests, int *destfd, unsigned long long *destoffsets)
{
	char *buf;
	int degraded = 0;

	if (posix_memalign((void**)&buf, 4096, disks * chunk))
		/* Don't start the 'reshape' */
		return 0;
	sysfs_set_num(sra, NULL, "suspend_hi", 0);
	sysfs_set_num(sra, NULL, "suspend_lo", 0);
	grow_backup(sra, 0, stripes,
		    fds, offsets, disks, chunk, level, layout,
		    dests, destfd, destoffsets,
		    0, &degraded, buf);
	validate(afd, destfd[0], destoffsets[0]);
	wait_backup(sra, 0, stripes * (chunk / 512), stripes * (chunk / 512),
		    dests, destfd, destoffsets,
		    0);
	sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data);
	free(buf);
	/* FIXME this should probably be numeric */
	sysfs_set_str(sra, NULL, "sync_max", "max");
	return 1;
}

static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes,
			int *fds, unsigned long long *offsets,
			int disks, int chunk, int level, int layout, int data,
			int dests, int *destfd, unsigned long long *destoffsets)
{
	char *buf;
	unsigned long long start;
	int rv;
	int degraded = 0;

	if (posix_memalign((void**)&buf, 4096, disks * chunk))
		return 0;
	start = sra->component_size - stripes * (chunk/512);
	sysfs_set_num(sra, NULL, "sync_max", start);
	sysfs_set_str(sra, NULL, "sync_action", "reshape");
	sysfs_set_num(sra, NULL, "suspend_lo", 0);
	sysfs_set_num(sra, NULL, "suspend_hi", 0);
	rv = wait_backup(sra, 0, start - stripes * (chunk/512), stripes * (chunk/512),
			 dests, destfd, destoffsets, 0);
	if (rv < 0)
		return 0;
	grow_backup(sra, 0, stripes,
		    fds, offsets,
		    disks, chunk, level, layout,
		    dests, destfd, destoffsets,
		    0, &degraded, buf);
	validate(afd, destfd[0], destoffsets[0]);
	wait_backup(sra, start, stripes*(chunk/512), 0,
		    dests, destfd, destoffsets, 0);
	sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data);
	free(buf);
	/* FIXME this should probably be numeric */
	sysfs_set_str(sra, NULL, "sync_max", "max");
	return 1;
}

static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes,
			   int *fds, unsigned long long *offsets,
			   unsigned long long start,
			   int disks, int chunk, int level, int layout, int data,
			   int dests, int *destfd, unsigned long long *destoffsets)
{
	unsigned long long size;
	unsigned long tailstripes = stripes;
	int part;
	char *buf;
	unsigned long long speed;
	int degraded = 0;


	if (posix_memalign((void**)&buf, 4096, disks * chunk))
		return 0;

	sysfs_set_num(sra, NULL, "suspend_lo", 0);
	sysfs_set_num(sra, NULL, "suspend_hi", 0);

	sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
	sysfs_set_num(sra, NULL, "sync_speed_min", 200000);

	grow_backup(sra, start, stripes,
		    fds, offsets,
		    disks, chunk, level, layout,
		    dests, destfd, destoffsets,
		    0, &degraded, buf);
	grow_backup(sra, (start + stripes) * (chunk/512), stripes,
		    fds, offsets,
		    disks, chunk, level, layout,
		    dests, destfd, destoffsets,
		    1, &degraded, buf);
	validate(afd, destfd[0], destoffsets[0]);
	part = 0;
	start += stripes * 2; /* where to read next */
	size = sra->component_size / (chunk/512);
	while (start < size) {
		if (wait_backup(sra, (start-stripes*2)*(chunk/512),
				stripes*(chunk/512), 0,
				dests, destfd, destoffsets,
				part) < 0)
			return 0;
		sysfs_set_num(sra, NULL, "suspend_lo", start*(chunk/512) * data);
		if (start + stripes > size)
			tailstripes = (size - start);

		grow_backup(sra, start*(chunk/512), tailstripes,
			    fds, offsets,
			    disks, chunk, level, layout,
			    dests, destfd, destoffsets,
			    part, &degraded, buf);
		start += stripes;
		part = 1 - part;
		validate(afd, destfd[0], destoffsets[0]);
	}
	if (wait_backup(sra, (start-stripes*2) * (chunk/512), stripes * (chunk/512), 0,
			dests, destfd, destoffsets,
			part) < 0)
		return 0;
	sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*(chunk/512)) * data);
	wait_backup(sra, (start-stripes) * (chunk/512), tailstripes * (chunk/512), 0,
		    dests, destfd, destoffsets,
		    1-part);
	sysfs_set_num(sra, NULL, "suspend_lo", (size*(chunk/512)) * data);
	sysfs_set_num(sra, NULL, "sync_speed_min", speed);
	free(buf);
	return 1;
}

/*
 * 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;

	if (info->new_level != info->array.level)
		return 1; /* Cannot handle level changes (they are instantaneous) */

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

		/* 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) {
				fprintf(stderr, Name ": 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);
			st->ss->free_super(st);

			if (lseek64(fd,
				    (dinfo.data_offset + dinfo.component_size - 8) <<9,
				    0) < 0) {
				fprintf(stderr, Name ": 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)
				fprintf(stderr, Name ": 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)
				fprintf(stderr, Name ": No backup metadata on %s\n", devname);
			continue;
		}
		if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
			if (verbose)
				fprintf(stderr, Name ": 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)
				fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
			continue; /* Bad second checksum */
		}
		if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
			if (verbose)
				fprintf(stderr, Name ": 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 > __le64_to_cpu(bsb.mtime) + 2*60*60 ||
		    info->array.utime < __le64_to_cpu(bsb.mtime) - 10*60) {
			if (check_env("MDADM_GROW_ALLOW_OLD")) {
				fprintf(stderr, Name ": accepting backup with timestamp %lu "
					"for array with timestamp %lu\n",
					(unsigned long)__le64_to_cpu(bsb.mtime),
					(unsigned long)info->array.utime);
			} else {
				if (verbose)
					fprintf(stderr, Name ": too-old timestamp on "
						"backup-metadata on %s\n", devname);
				continue; /* time stamp is too bad */
			}
		}

		if (bsb.magic[15] == '1') {
		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)
					fprintf(stderr, Name ": 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 (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)
				fprintf(stderr, Name ": 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, 4096) != 4096)
			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 = malloc(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);
			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)) {
			/* didn't succeed, so giveup */
			if (verbose)
				fprintf(stderr, Name ": Error restoring backup from %s\n",
					devname);
			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)) {
			/* didn't succeed, so giveup */
			if (verbose)
				fprintf(stderr, Name ": Error restoring second backup from %s\n",
					devname);
			return 1;
		}


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

		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);
			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)
		fprintf(stderr, Name ": Failed to find backup of critical section\n");
	return 1;
}

int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
		  char *backup_file)
{
	/* Array is assembled and ready to be started, but
	 * monitoring is probably required.
	 * So:
	 *   - start read-only
	 *   - set upper bound for resync
	 *   - initialise the 'suspend' boundaries
	 *   - switch to read-write
	 *   - fork and continue monitoring
	 */
	int err;
	int backup_list[1];
	unsigned long long backup_offsets[1];
	int odisks, ndisks, ochunk, nchunk,odata,ndata;
	unsigned long a,b,blocks,stripes;
	int backup_fd;
	int *fds;
	unsigned long long *offsets;
	int d;
	struct mdinfo *sra, *sd;
	int rv;
	int cache;
	int done = 0;

	sra = sysfs_read(-1, devname2devnum(info->sys_name),
			 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
			 GET_CACHE);
	if (!sra)
		return 1;

	err = sysfs_set_str(info, NULL, "array_state", "readonly");
	if (err)
		return err;

	/* make sure reshape doesn't progress until we are ready */
	sysfs_set_str(info, NULL, "sync_max", "0");
	sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */
	
	/* ndisks is not growing, so raid_disks is old and +delta is new */
	odisks = info->array.raid_disks;
	ndisks = odisks + info->delta_disks;
	odata = odisks - 1;
	ndata = ndisks - 1;
	if (info->array.level == 6) {
		odata--;
		ndata--;
	}
	ochunk = info->array.chunk_size;
	nchunk = info->new_chunk;

	a = (ochunk/512) * odata;
	b = (nchunk/512) * ndata;
	/* Find GCD */
	while (a != b) {
		if (a < b)
			b -= a;
		if (b < a)
			a -= b;
	}
	/* LCM == product / GCD */
	blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;

	if (ndata == odata)
		while (blocks * 32 < sra->component_size &&
		       blocks < 16*1024*2)
			blocks *= 2;
	stripes = blocks / (info->array.chunk_size/512) / odata;

	/* check that the internal stripe cache is
	 * large enough, or it won't work.
	 */
	cache = (nchunk < ochunk) ? ochunk : nchunk;
	cache = cache * 4 / 4096;
	if (cache < blocks / 8 / odisks + 16)
		/* Make it big enough to hold 'blocks' */
		cache = blocks / 8 / odisks + 16;
	if (sra->cache_size < cache)
		sysfs_set_num(sra, NULL, "stripe_cache_size",
			      cache+1);

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

	backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
	backup_list[0] = backup_fd;
	backup_offsets[0] = 8 * 512;
	fds = malloc(odisks * sizeof(fds[0]));
	offsets = malloc(odisks * sizeof(offsets[0]));
	for (d=0; d<odisks; d++)
		fds[d] = -1;

	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);
			fds[sd->disk.raid_disk]
				= dev_open(dn, O_RDONLY);
			offsets[sd->disk.raid_disk] = sd->data_offset*512;
			if (fds[sd->disk.raid_disk] < 0) {
				fprintf(stderr, Name ": %s: cannot open component %s\n",
					info->sys_name, dn?dn:"-unknown-");
				rv = 1;
				goto release;
			}
			free(dn);
		}
	}

	switch(fork()) {
	case 0:
		close(mdfd);
		mlockall(MCL_FUTURE);
		if (info->delta_disks < 0)
			done = child_shrink(-1, info, stripes,
					    fds, offsets,
					    info->array.raid_disks,
					    info->array.chunk_size,
					    info->array.level, info->array.layout,
					    odata,
					    1, backup_list, backup_offsets);
		else if (info->delta_disks == 0) {
			/* The 'start' is a per-device stripe number.
			 * reshape_progress is a per-array sector number.
			 * So divide by ndata * chunk_size
			 */
			unsigned long long start = info->reshape_progress / ndata;
			start /= (info->array.chunk_size/512);
			done = child_same_size(-1, info, stripes,
					       fds, offsets,
					       start,
					       info->array.raid_disks,
					       info->array.chunk_size,
					       info->array.level, info->array.layout,
					       odata,
					       1, backup_list, backup_offsets);
		}
		if (backup_file && done)
			unlink(backup_file);
		/* FIXME should I intuit a level change */
		exit(0);
	case -1:
		fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n",
			strerror(errno));
		return 1;
	default:
		break;
	}
release:
	return 0;
}