summaryrefslogtreecommitdiff
path: root/sys/dev/softraid.c
blob: af5cfd1b5dc34f02df5928694348ededa820e74a (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
/* $OpenBSD: softraid.c,v 1.90 2008/01/05 07:33:37 brad Exp $ */
/*
 * Copyright (c) 2007 Marco Peereboom <marco@peereboom.us>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "bio.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/disk.h>
#include <sys/rwlock.h>
#include <sys/queue.h>
#include <sys/fcntl.h>
#include <sys/disklabel.h>
#include <sys/mount.h>
#include <sys/sensors.h>
#include <sys/stat.h>
#include <sys/conf.h>
#include <sys/uio.h>

#include <crypto/cryptodev.h>

#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <scsi/scsi_disk.h>

#include <dev/softraidvar.h>
#include <dev/rndvar.h>

/* #define SR_FANCY_STATS */

#ifdef SR_DEBUG
#define SR_FANCY_STATS
uint32_t	sr_debug = 0
		    /* | SR_D_CMD */
		    /* | SR_D_MISC */
		    /* | SR_D_INTR */
		    /* | SR_D_IOCTL */
		    /* | SR_D_CCB */
		    /* | SR_D_WU */
		    /* | SR_D_META */
		    /* | SR_D_DIS */
		    /* | SR_D_STATE */
		;
#endif

int		sr_match(struct device *, void *, void *);
void		sr_attach(struct device *, struct device *, void *);
int		sr_detach(struct device *, int);
int		sr_activate(struct device *, enum devact);

struct cfattach softraid_ca = {
	sizeof(struct sr_softc), sr_match, sr_attach, sr_detach,
	sr_activate
};

struct cfdriver softraid_cd = {
	NULL, "softraid", DV_DULL
};

int			sr_scsi_cmd(struct scsi_xfer *);
void			sr_minphys(struct buf *bp);
void			sr_copy_internal_data(struct scsi_xfer *,
			    void *, size_t);
int			sr_scsi_ioctl(struct scsi_link *, u_long,
			    caddr_t, int, struct proc *);
int			sr_ioctl(struct device *, u_long, caddr_t);
int			sr_ioctl_inq(struct sr_softc *, struct bioc_inq *);
int			sr_ioctl_vol(struct sr_softc *, struct bioc_vol *);
int			sr_ioctl_disk(struct sr_softc *, struct bioc_disk *);
int			sr_ioctl_setstate(struct sr_softc *,
			    struct bioc_setstate *);
int			sr_ioctl_createraid(struct sr_softc *,
			    struct bioc_createraid *, int);
int			sr_open_chunks(struct sr_softc *,
			    struct sr_chunk_head *, dev_t *, int);
int			sr_read_meta(struct sr_discipline *);
int			sr_create_chunk_meta(struct sr_softc *,
			    struct sr_chunk_head *);
void			sr_unwind_chunks(struct sr_softc *,
			    struct sr_chunk_head *);
void			sr_free_discipline(struct sr_discipline *);
void			sr_shutdown_discipline(struct sr_discipline *);

/* utility functions */
void			sr_shutdown(void *);
void			sr_get_uuid(struct sr_uuid *);
void			sr_print_uuid(struct sr_uuid *, int);
u_int32_t		sr_checksum(char *, u_int32_t *, u_int32_t);
int			sr_clear_metadata(struct sr_discipline *);
int			sr_save_metadata(struct sr_discipline *, u_int32_t);
void			sr_save_metadata_callback(void *, void *);
int			sr_boot_assembly(struct sr_softc *);
int			sr_already_assembled(struct sr_discipline *);
int			sr_validate_metadata(struct sr_softc *, dev_t,
			    struct sr_metadata *);

/* don't include these on RAMDISK */
#ifndef SMALL_KERNEL
void			sr_refresh_sensors(void *);
int			sr_create_sensors(struct sr_discipline *);
void			sr_delete_sensors(struct sr_discipline *);
#endif

#ifdef SR_DEBUG
void			sr_print_metadata(struct sr_metadata *);
#else
#define			sr_print_metadata(m)
#endif

struct scsi_adapter sr_switch = {
	sr_scsi_cmd, sr_minphys, NULL, NULL, sr_scsi_ioctl
};

struct scsi_device sr_dev = {
	NULL, NULL, NULL, NULL
};

int
sr_match(struct device *parent, void *match, void *aux)
{
	return (1);
}

void
sr_attach(struct device *parent, struct device *self, void *aux)
{
	struct sr_softc		*sc = (void *)self;

	DNPRINTF(SR_D_MISC, "\n%s: sr_attach", DEVNAME(sc));

	rw_init(&sc->sc_lock, "sr_lock");

	if (bio_register(&sc->sc_dev, sr_ioctl) != 0)
		printf("%s: controller registration failed", DEVNAME(sc));
	else
		sc->sc_ioctl = sr_ioctl;

	printf("\n");

	sr_boot_assembly(sc);
}

int
sr_detach(struct device *self, int flags)
{
	return (0);
}

int
sr_activate(struct device *self, enum devact act)
{
	return (1);
}

void
sr_minphys(struct buf *bp)
{
	DNPRINTF(SR_D_MISC, "sr_minphys: %d\n", bp->b_bcount);

	/* XXX currently using SR_MAXFER = MAXPHYS */
	if (bp->b_bcount > SR_MAXFER)
		bp->b_bcount = SR_MAXFER;
	minphys(bp);
}

void
sr_copy_internal_data(struct scsi_xfer *xs, void *v, size_t size)
{
	size_t			copy_cnt;

	DNPRINTF(SR_D_MISC, "sr_copy_internal_data xs: %p size: %d\n",
	    xs, size);

	if (xs->datalen) {
		copy_cnt = MIN(size, xs->datalen);
		bcopy(v, xs->data, copy_cnt);
	}
}

int
sr_alloc_ccb(struct sr_discipline *sd)
{
	struct sr_ccb		*ccb;
	int			i;

	if (!sd)
		return (1);

	DNPRINTF(SR_D_CCB, "%s: sr_alloc_ccb\n", DEVNAME(sd->sd_sc));

	if (sd->sd_ccb)
		return (1);

	sd->sd_ccb = malloc(sizeof(struct sr_ccb) *
	    sd->sd_max_wu * sd->sd_max_ccb_per_wu, M_DEVBUF, M_WAITOK | M_ZERO);
	TAILQ_INIT(&sd->sd_ccb_freeq);
	for (i = 0; i < sd->sd_max_wu * sd->sd_max_ccb_per_wu; i++) {
		ccb = &sd->sd_ccb[i];
		ccb->ccb_dis = sd;
		sr_put_ccb(ccb);
	}

	DNPRINTF(SR_D_CCB, "%s: sr_alloc_ccb ccb: %d\n",
	    DEVNAME(sd->sd_sc), sd->sd_max_wu * sd->sd_max_ccb_per_wu);

	return (0);
}

void
sr_free_ccb(struct sr_discipline *sd)
{
	struct sr_ccb		*ccb;

	if (!sd)
		return;

	DNPRINTF(SR_D_CCB, "%s: sr_free_ccb %p\n", DEVNAME(sd->sd_sc), sd);

	while ((ccb = TAILQ_FIRST(&sd->sd_ccb_freeq)) != NULL)
		TAILQ_REMOVE(&sd->sd_ccb_freeq, ccb, ccb_link);

	if (sd->sd_ccb)
		free(sd->sd_ccb, M_DEVBUF);
}

struct sr_ccb *
sr_get_ccb(struct sr_discipline *sd)
{
	struct sr_ccb		*ccb;
	int			s;

	s = splbio();

	ccb = TAILQ_FIRST(&sd->sd_ccb_freeq);
	if (ccb) {
		TAILQ_REMOVE(&sd->sd_ccb_freeq, ccb, ccb_link);
		ccb->ccb_state = SR_CCB_INPROGRESS;
	}

	splx(s);

	DNPRINTF(SR_D_CCB, "%s: sr_get_ccb: %p\n", DEVNAME(sd->sd_sc),
	    ccb);

	return (ccb);
}

void
sr_put_ccb(struct sr_ccb *ccb)
{
	struct sr_discipline	*sd = ccb->ccb_dis;
	int			s;

	DNPRINTF(SR_D_CCB, "%s: sr_put_ccb: %p\n", DEVNAME(sd->sd_sc),
	    ccb);

	s = splbio();

	ccb->ccb_wu = NULL;
	ccb->ccb_state = SR_CCB_FREE;
	ccb->ccb_target = -1;

	TAILQ_INSERT_TAIL(&sd->sd_ccb_freeq, ccb, ccb_link);

	splx(s);
}

int
sr_alloc_wu(struct sr_discipline *sd)
{
	struct sr_workunit	*wu;
	int			i, no_wu;

	if (!sd)
		return (1);

	DNPRINTF(SR_D_WU, "%s: sr_alloc_wu %p %d\n", DEVNAME(sd->sd_sc),
	    sd, sd->sd_max_wu);

	if (sd->sd_wu)
		return (1);

	no_wu = sd->sd_max_wu;
	sd->sd_wu_pending = no_wu;

	sd->sd_wu = malloc(sizeof(struct sr_workunit) * no_wu,
	    M_DEVBUF, M_WAITOK | M_ZERO);
	TAILQ_INIT(&sd->sd_wu_freeq);
	TAILQ_INIT(&sd->sd_wu_pendq);
	TAILQ_INIT(&sd->sd_wu_defq);
	for (i = 0; i < no_wu; i++) {
		wu = &sd->sd_wu[i];
		wu->swu_dis = sd;
		sr_put_wu(wu);
	}

	return (0);
}

void
sr_free_wu(struct sr_discipline *sd)
{
	struct sr_workunit	*wu;

	if (!sd)
		return;

	DNPRINTF(SR_D_WU, "%s: sr_free_wu %p\n", DEVNAME(sd->sd_sc), sd);

	while ((wu = TAILQ_FIRST(&sd->sd_wu_freeq)) != NULL)
		TAILQ_REMOVE(&sd->sd_wu_freeq, wu, swu_link);
	while ((wu = TAILQ_FIRST(&sd->sd_wu_pendq)) != NULL)
		TAILQ_REMOVE(&sd->sd_wu_pendq, wu, swu_link);
	while ((wu = TAILQ_FIRST(&sd->sd_wu_defq)) != NULL)
		TAILQ_REMOVE(&sd->sd_wu_defq, wu, swu_link);

	if (sd->sd_wu)
		free(sd->sd_wu, M_DEVBUF);
}

void
sr_put_wu(struct sr_workunit *wu)
{
	struct sr_discipline	*sd = wu->swu_dis;
	struct sr_ccb		*ccb;

	int			s;

	DNPRINTF(SR_D_WU, "%s: sr_put_wu: %p\n", DEVNAME(sd->sd_sc), wu);

	s = splbio();

	wu->swu_xs = NULL;
	wu->swu_state = SR_WU_FREE;
	wu->swu_ios_complete = 0;
	wu->swu_ios_failed = 0;
	wu->swu_ios_succeeded = 0;
	wu->swu_io_count = 0;
	wu->swu_blk_start = 0;
	wu->swu_blk_end = 0;
	wu->swu_collider = NULL;
	wu->swu_fake = 0;

	while ((ccb = TAILQ_FIRST(&wu->swu_ccb)) != NULL) {
		TAILQ_REMOVE(&wu->swu_ccb, ccb, ccb_link);
		sr_put_ccb(ccb);
	}
	TAILQ_INIT(&wu->swu_ccb);

	TAILQ_INSERT_TAIL(&sd->sd_wu_freeq, wu, swu_link);
	sd->sd_wu_pending--;

	splx(s);
}

struct sr_workunit *
sr_get_wu(struct sr_discipline *sd)
{
	struct sr_workunit	*wu;
	int			s;

	s = splbio();

	wu = TAILQ_FIRST(&sd->sd_wu_freeq);
	if (wu) {
		TAILQ_REMOVE(&sd->sd_wu_freeq, wu, swu_link);
		wu->swu_state = SR_WU_INPROGRESS;
	}
	sd->sd_wu_pending++;

	splx(s);

	DNPRINTF(SR_D_WU, "%s: sr_get_wu: %p\n", DEVNAME(sd->sd_sc), wu);

	return (wu);
}

int
sr_scsi_cmd(struct scsi_xfer *xs)
{
	int			s;
	struct scsi_link	*link = xs->sc_link;
	struct sr_softc		*sc = link->adapter_softc;
	struct sr_workunit	*wu;
	struct sr_discipline	*sd;

	DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd: scsibus%d xs: %p "
	    "flags: %#x\n", DEVNAME(sc), link->scsibus, xs, xs->flags);

	sd = sc->sc_dis[link->scsibus];
	if (sd == NULL) {
		s = splhigh();
		sd = sc->sc_attach_dis;
		splx(s);

		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd: attaching %p\n",
		    DEVNAME(sc), sd);
		if (sd == NULL) {
			wu = NULL;
			printf("%s: sr_scsi_cmd NULL discipline\n",
			    DEVNAME(sc));
			goto stuffup;
		}
	}

	if ((wu = sr_get_wu(sd)) == NULL) {
		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd no wu\n", DEVNAME(sc));
		return (TRY_AGAIN_LATER);
	}

	xs->error = XS_NOERROR;
	wu->swu_xs = xs;

	switch (xs->cmd->opcode) {
	case READ_COMMAND:
	case READ_BIG:
	case WRITE_COMMAND:
	case WRITE_BIG:
		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd: READ/WRITE %02x\n",
		    DEVNAME(sc), xs->cmd->opcode);
		if (sd->sd_scsi_rw(wu))
			goto stuffup;
		break;

	case SYNCHRONIZE_CACHE:
		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd: SYNCHRONIZE_CACHE\n",
		    DEVNAME(sc));
		if (sd->sd_scsi_sync(wu))
			goto stuffup;
		goto complete;

	case TEST_UNIT_READY:
		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd: TEST_UNIT_READY\n",
		    DEVNAME(sc));
		if (sd->sd_scsi_tur(wu))
			goto stuffup;
		goto complete;

	case START_STOP:
		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd: START_STOP\n",
		    DEVNAME(sc));
		if (sd->sd_scsi_start_stop(wu))
			goto stuffup;
		goto complete;

	case INQUIRY:
		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd: INQUIRY\n",
		    DEVNAME(sc));
		if (sd->sd_scsi_inquiry(wu))
			goto stuffup;
		goto complete;

	case READ_CAPACITY:
		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd READ CAPACITY\n",
		    DEVNAME(sc));
		if (sd->sd_scsi_read_cap(wu))
			goto stuffup;
		goto complete;

	case REQUEST_SENSE:
		DNPRINTF(SR_D_CMD, "%s: sr_scsi_cmd REQUEST SENSE\n",
		    DEVNAME(sc));
		if (sd->sd_scsi_req_sense(wu))
			goto stuffup;
		goto complete;

	default:
		DNPRINTF(SR_D_CMD, "%s: unsupported scsi command %x\n",
		    DEVNAME(sc), xs->cmd->opcode);
		/* XXX might need to add generic function to handle others */
		goto stuffup;
	}

	return (SUCCESSFULLY_QUEUED);
stuffup:
	if (sd->sd_scsi_sense.error_code) {
		xs->error = XS_SENSE;
		bcopy(&sd->sd_scsi_sense, &xs->sense, sizeof(xs->sense));
		bzero(&sd->sd_scsi_sense, sizeof(sd->sd_scsi_sense));
	} else {
		xs->error = XS_DRIVER_STUFFUP;
		xs->flags |= ITSDONE;
	}
complete:
	s = splbio();
	scsi_done(xs);
	splx(s);
	if (wu)
		sr_put_wu(wu);
	return (COMPLETE);
}
int
sr_scsi_ioctl(struct scsi_link *link, u_long cmd, caddr_t addr, int flag,
    struct proc *p)
{
	DNPRINTF(SR_D_IOCTL, "%s: sr_scsi_ioctl cmd: %#x\n",
	    DEVNAME((struct sr_softc *)link->adapter_softc), cmd);

	return (sr_ioctl(link->adapter_softc, cmd, addr));
}

int
sr_ioctl(struct device *dev, u_long cmd, caddr_t addr)
{
	struct sr_softc		*sc = (struct sr_softc *)dev;
	int			rv = 0;

	DNPRINTF(SR_D_IOCTL, "%s: sr_ioctl ", DEVNAME(sc));

	rw_enter_write(&sc->sc_lock);

	switch (cmd) {
	case BIOCINQ:
		DNPRINTF(SR_D_IOCTL, "inq\n");
		rv = sr_ioctl_inq(sc, (struct bioc_inq *)addr);
		break;

	case BIOCVOL:
		DNPRINTF(SR_D_IOCTL, "vol\n");
		rv = sr_ioctl_vol(sc, (struct bioc_vol *)addr);
		break;

	case BIOCDISK:
		DNPRINTF(SR_D_IOCTL, "disk\n");
		rv = sr_ioctl_disk(sc, (struct bioc_disk *)addr);
		break;

	case BIOCALARM:
		DNPRINTF(SR_D_IOCTL, "alarm\n");
		/*rv = sr_ioctl_alarm(sc, (struct bioc_alarm *)addr); */
		break;

	case BIOCBLINK:
		DNPRINTF(SR_D_IOCTL, "blink\n");
		/*rv = sr_ioctl_blink(sc, (struct bioc_blink *)addr); */
		break;

	case BIOCSETSTATE:
		DNPRINTF(SR_D_IOCTL, "setstate\n");
		rv = sr_ioctl_setstate(sc, (struct bioc_setstate *)addr);
		break;

	case BIOCCREATERAID:
		DNPRINTF(SR_D_IOCTL, "createraid\n");
		rv = sr_ioctl_createraid(sc, (struct bioc_createraid *)addr, 1);
		break;

	default:
		DNPRINTF(SR_D_IOCTL, "invalid ioctl\n");
		rv = ENOTTY;
	}

	rw_exit_write(&sc->sc_lock);

	return (rv);
}

int
sr_ioctl_inq(struct sr_softc *sc, struct bioc_inq *bi)
{
	int			i, vol, disk;

	for (i = 0, vol = 0, disk = 0; i < SR_MAXSCSIBUS; i++)
		/* XXX this will not work when we stagger disciplines */
		if (sc->sc_dis[i]) {
			vol++;
			disk += sc->sc_dis[i]->sd_vol.sv_meta.svm_no_chunk;
		}

	strlcpy(bi->bi_dev, sc->sc_dev.dv_xname, sizeof(bi->bi_dev));
	bi->bi_novol = vol;
	bi->bi_nodisk = disk;

	return (0);
}

int
sr_ioctl_vol(struct sr_softc *sc, struct bioc_vol *bv)
{
	int			i, vol, rv = EINVAL;
	struct sr_volume	*sv;

	for (i = 0, vol = -1; i < SR_MAXSCSIBUS; i++) {
		/* XXX this will not work when we stagger disciplines */
		if (sc->sc_dis[i])
			vol++;
		if (vol != bv->bv_volid)
			continue;

		sv = &sc->sc_dis[i]->sd_vol;
		bv->bv_status = sv->sv_meta.svm_status;
		bv->bv_size = sv->sv_meta.svm_size;
		bv->bv_level = sv->sv_meta.svm_level;
		bv->bv_nodisk = sv->sv_meta.svm_no_chunk;
		strlcpy(bv->bv_dev, sv->sv_meta.svm_devname,
		    sizeof(bv->bv_dev));
		strlcpy(bv->bv_vendor, sv->sv_meta.svm_vendor,
		    sizeof(bv->bv_vendor));
		rv = 0;
		break;
	}

	return (rv);
}

int
sr_ioctl_disk(struct sr_softc *sc, struct bioc_disk *bd)
{
	int			i, vol, rv = EINVAL, id;
	struct sr_chunk		*src;

	for (i = 0, vol = -1; i < SR_MAXSCSIBUS; i++) {
		/* XXX this will not work when we stagger disciplines */
		if (sc->sc_dis[i])
			vol++;
		if (vol != bd->bd_volid)
			continue;

		id = bd->bd_diskid;
		if (id >= sc->sc_dis[i]->sd_vol.sv_meta.svm_no_chunk)
			break;

		src = sc->sc_dis[i]->sd_vol.sv_chunks[id];
		bd->bd_status = src->src_meta.scm_status;
		bd->bd_size = src->src_meta.scm_size;
		bd->bd_channel = vol;
		bd->bd_target = id;
		strlcpy(bd->bd_vendor, src->src_meta.scm_devname,
		    sizeof(bd->bd_vendor));
		rv = 0;
		break;
	}

	return (rv);
}

int
sr_ioctl_setstate(struct sr_softc *sc, struct bioc_setstate *bs)
{
	int			rv = EINVAL;

#ifdef SR_UNIT_TEST
	int			i, vol, state;
	struct sr_discipline	*sd;

	for (i = 0, vol = -1; i < SR_MAXSCSIBUS; i++) {
		/* XXX this will not work when we stagger disciplines */
		if (sc->sc_dis[i])
			vol++;
		if (vol != bs->bs_channel)
			continue;

		sd = sc->sc_dis[vol];
		if (bs->bs_target >= sd->sd_vol.sv_meta.svm_no_chunk)
			goto done;

		switch (bs->bs_status) {
		case BIOC_SSONLINE:
			state = BIOC_SDONLINE;
			break;
		case BIOC_SSOFFLINE:
			state = BIOC_SDOFFLINE;
			break;
		case BIOC_SSHOTSPARE:
			state = BIOC_SDHOTSPARE;
			break;
		case BIOC_SSREBUILD:
			state = BIOC_SDREBUILD;
			break;
		default:
			printf("invalid state %d\n", bs->bs_status);
			goto done;
		}

		printf("status change for %u:%u -> %u %u\n",
		    bs->bs_channel, bs->bs_target, bs->bs_status, state);

		sd->sd_set_chunk_state(sd, bs->bs_target, bs->bs_status);

		rv = 0;

		break;
	}

done:
#endif
	return (rv);
}

int
sr_ioctl_createraid(struct sr_softc *sc, struct bioc_createraid *bc, int user)
{
	dev_t			*dt;
	int			i, s, no_chunk, rv = EINVAL, vol;
	int			no_meta, updatemeta = 0;
	int64_t			vol_size;
	struct sr_chunk_head	*cl;
	struct sr_discipline	*sd = NULL;
	struct sr_chunk		*ch_entry;
	struct device		*dev, *dev2;
	struct scsibus_attach_args saa;

	DNPRINTF(SR_D_IOCTL, "%s: sr_ioctl_createraid(%d)\n",
	    DEVNAME(sc), user);

	/* user input */
	if (bc->bc_dev_list_len > BIOC_CRMAXLEN)
		goto unwind;

	dt = malloc(bc->bc_dev_list_len, M_DEVBUF, M_WAITOK | M_ZERO);
	if (user)
		copyin(bc->bc_dev_list, dt, bc->bc_dev_list_len);
	else
		bcopy(bc->bc_dev_list, dt, bc->bc_dev_list_len);

	sd = malloc(sizeof(struct sr_discipline), M_DEVBUF, M_WAITOK | M_ZERO);
	sd->sd_sc = sc;

	no_chunk = bc->bc_dev_list_len / sizeof(dev_t);
	cl = &sd->sd_vol.sv_chunk_list;
	SLIST_INIT(cl);
	if (sr_open_chunks(sc, cl, dt, no_chunk))
		goto unwind;

	/* in memory copy of metadata */
	sd->sd_meta = malloc(SR_META_SIZE * 512 , M_DEVBUF, M_WAITOK | M_ZERO);

	/* we have a valid list now create an array index */
	sd->sd_vol.sv_chunks = malloc(sizeof(struct sr_chunk *) * no_chunk,
	    M_DEVBUF, M_WAITOK | M_ZERO);

	/* force the raid volume by clearing metadata region */
	if (bc->bc_flags & BIOC_SCFORCE) {
		/* make sure disk isn't up and running */
		if (sr_read_meta(sd))
			if (sr_already_assembled(sd)) {
				printf("%s: disk ", DEVNAME(sc));
				sr_print_uuid(&sd->sd_meta->ssd_uuid, 0);
				printf(" is currently in use; can't force "
				    "create\n");
				goto unwind;
			}

		/* zero out pointers and metadata again to create disk */
		bzero(sd->sd_vol.sv_chunks,
		    sizeof(struct sr_chunk *) * no_chunk);
		bzero(sd->sd_meta, SR_META_SIZE  * 512);

		if (sr_clear_metadata(sd)) {
			printf("%s: failed to clear metadata\n");
			goto unwind;
		}
	}

	if ((no_meta = sr_read_meta(sd)) == 0) {
		/* no metadata available */
		switch (bc->bc_level) {
		case 1:
			if (no_chunk < 2)
				goto unwind;
			strlcpy(sd->sd_name, "RAID 1", sizeof(sd->sd_name));
			break;
#if 0
		case 'C':
			if (no_chunk != 1)
				goto unwind;
			strlcpy(sd->sd_name, "CRYPTO", sizeof(sd->sd_name));
			break;
#endif
		default:
			goto unwind;
		}

		/* fill out chunk array */
		i = 0;
		SLIST_FOREACH(ch_entry, cl, src_link)
			sd->sd_vol.sv_chunks[i++] = ch_entry;

		/* fill out all chunk metadata */
		sr_create_chunk_meta(sc, cl);

		/* fill out all volume metadata */
		ch_entry = SLIST_FIRST(cl);
		vol_size = ch_entry->src_meta.scm_coerced_size;
		DNPRINTF(SR_D_IOCTL,
		    "%s: sr_ioctl_createraid: vol_size: %lld\n",
		    DEVNAME(sc), vol_size);
		sd->sd_vol.sv_meta.svm_no_chunk = no_chunk;
		sd->sd_vol.sv_meta.svm_size = vol_size;
		sd->sd_vol.sv_meta.svm_status = BIOC_SVONLINE;
		sd->sd_vol.sv_meta.svm_level = bc->bc_level;
		strlcpy(sd->sd_vol.sv_meta.svm_vendor, "OPENBSD",
		    sizeof(sd->sd_vol.sv_meta.svm_vendor));
		snprintf(sd->sd_vol.sv_meta.svm_product,
		    sizeof(sd->sd_vol.sv_meta.svm_product), "SR %s",
		    sd->sd_name);
		snprintf(sd->sd_vol.sv_meta.svm_revision,
		    sizeof(sd->sd_vol.sv_meta.svm_revision), "%03d",
		    SR_META_VERSION);

		sd->sd_meta_flags = bc->bc_flags & BIOC_SCNOAUTOASSEMBLE;
		updatemeta = 1;
	} else if (no_meta == no_chunk) {
		if (user == 0 && sd->sd_meta_flags & BIOC_SCNOAUTOASSEMBLE) {
			DNPRINTF(SR_D_META, "%s: disk not auto assembled from "
			    "metadata\n", DEVNAME(sc));
			goto unwind;
		}
		if (sr_already_assembled(sd)) {
			printf("%s: disk ", DEVNAME(sc));
			sr_print_uuid(&sd->sd_meta->ssd_uuid, 0);
			printf(" already assembled\n");
			goto unwind;
		}
		DNPRINTF(SR_D_META, "%s: disk assembled from metadata\n",
		    DEVNAME(sc));
		updatemeta = 0;
	} else {
		if (sr_already_assembled(sd)) {
			printf("%s: disk ", DEVNAME(sc));
			sr_print_uuid(&sd->sd_meta->ssd_uuid, 0);
			printf(" already assembled; will not partial "
			    "assemble it\n");
			goto unwind;
		}
		printf("%s: not yet partial bringup\n", DEVNAME(sc));
		goto unwind;
	}

	/* XXX metadata SHALL be fully filled in at this point */

	switch (bc->bc_level) {
	case 1:
		/* fill out discipline members */
		sd->sd_type = SR_MD_RAID1;
		sd->sd_max_ccb_per_wu = no_chunk;
		sd->sd_max_wu = SR_RAID1_NOWU;

		/* setup discipline pointers */
		sd->sd_alloc_resources = sr_raid1_alloc_resources;
		sd->sd_free_resources = sr_raid1_free_resources;
		sd->sd_scsi_inquiry = sr_raid_inquiry;
		sd->sd_scsi_read_cap = sr_raid_read_cap;
		sd->sd_scsi_tur = sr_raid_tur;
		sd->sd_scsi_req_sense = sr_raid_request_sense;
		sd->sd_scsi_start_stop = sr_raid_start_stop;
		sd->sd_scsi_sync = sr_raid_sync;
		sd->sd_scsi_rw = sr_raid1_rw;
		sd->sd_set_chunk_state = sr_raid_set_chunk_state;
		sd->sd_set_vol_state = sr_raid_set_vol_state;
		break;
#ifdef CRYPTO
	case 'C':
		/* fill out discipline members */
		sd->sd_type = SR_MD_RAIDCRYPTO;
		sd->sd_max_ccb_per_wu = no_chunk;
		sd->sd_max_wu = SR_RAIDCRYPTO_NOWU;

		/* setup discipline pointers */
		sd->sd_alloc_resources = sr_crypto_alloc_resources;
		sd->sd_free_resources = sr_crypto_free_resources;
		sd->sd_scsi_inquiry = sr_raid_inquiry;
		sd->sd_scsi_read_cap = sr_raid_read_cap;
		sd->sd_scsi_tur = sr_raid_tur;
		sd->sd_scsi_req_sense = sr_raid_request_sense;
		sd->sd_scsi_start_stop = sr_raid_start_stop;
		sd->sd_scsi_sync = sr_raid_sync;
		sd->sd_scsi_rw = sr_crypto_rw;
		sd->sd_set_chunk_state = sr_raid_set_chunk_state;
		sd->sd_set_vol_state = sr_raid_set_vol_state;
		break;
#endif
	default:
		printf("default %d\n", bc->bc_level);
		goto unwind;
	}

	/* allocate all resources */
	if ((rv = sd->sd_alloc_resources(sd)))
		goto unwind;

	/* setup scsi midlayer */
	sd->sd_link.openings = sd->sd_max_wu;
	sd->sd_link.device = &sr_dev;
	sd->sd_link.device_softc = sc;
	sd->sd_link.adapter_softc = sc;
	sd->sd_link.adapter = &sr_switch;
	sd->sd_link.adapter_target = SR_MAX_LD;
	sd->sd_link.adapter_buswidth = 1;
	bzero(&saa, sizeof(saa));
	saa.saa_sc_link = &sd->sd_link;

	/* we passed all checks return ENXIO if volume can't be created */
	rv = ENXIO;

	/* clear sense data */
	bzero(&sd->sd_scsi_sense, sizeof(sd->sd_scsi_sense));

	/* use temporary discipline pointer */
	s = splhigh();
	sc->sc_attach_dis = sd;
	splx(s);
	dev2 = config_found(&sc->sc_dev, &saa, scsiprint);
	s = splhigh();
	sc->sc_attach_dis = NULL;
	splx(s);
	TAILQ_FOREACH(dev, &alldevs, dv_list)
		if (dev->dv_parent == dev2)
			break;
	if (dev == NULL)
		goto unwind;

	DNPRINTF(SR_D_IOCTL, "%s: sr device added: %s on scsibus%d\n",
	    DEVNAME(sc), dev->dv_xname, sd->sd_link.scsibus);

	sc->sc_dis[sd->sd_link.scsibus] = sd;
	for (i = 0, vol = -1; i <= sd->sd_link.scsibus; i++)
		if (sc->sc_dis[i])
			vol++;

	rv = 0;
	if (updatemeta) {
		/* fill out remaining volume metadata */
		sd->sd_vol.sv_meta.svm_volid = vol;
		strlcpy(sd->sd_vol.sv_meta.svm_devname, dev->dv_xname,
		    sizeof(sd->sd_vol.sv_meta.svm_devname));

	}

	/* save metadata to disk */
	rv = sr_save_metadata(sd, SR_VOL_DIRTY);

#ifndef SMALL_KERNEL
	if (sr_create_sensors(sd))
		printf("%s: unable to create sensor for %s\n", DEVNAME(sc),
		    dev->dv_xname);
	else
		sd->sd_vol.sv_sensor_valid = 1;
#endif /* SMALL_KERNEL */

	sd->sd_scsibus_dev = dev2;
	sd->sd_shutdownhook = shutdownhook_establish(sr_shutdown, sd);

	return (rv);

unwind:
	sr_shutdown_discipline(sd);

	return (rv);
}

int
sr_open_chunks(struct sr_softc *sc, struct sr_chunk_head *cl, dev_t *dt,
    int no_chunk)
{
	struct sr_chunk		*ch_entry, *ch_prev = NULL;
	struct disklabel	label;
	struct bdevsw		*bdsw;
	char			*name;
	int			maj, unit, part, i, error;
	daddr64_t		size;
	dev_t			dev;

	DNPRINTF(SR_D_IOCTL, "%s: sr_open_chunks(%d)\n", DEVNAME(sc), no_chunk);

	/* fill out chunk list */
	for (i = 0; i < no_chunk; i++) {
		ch_entry = malloc(sizeof(struct sr_chunk), M_DEVBUF,
		    M_WAITOK | M_ZERO);
		/* keep disks in user supplied order */
		if (ch_prev)
			SLIST_INSERT_AFTER(ch_prev, ch_entry, src_link);
		else
			SLIST_INSERT_HEAD(cl, ch_entry, src_link);
		ch_prev = ch_entry;

		dev = dt[i];
		maj = major(dev);
		part = DISKPART(dev);
		unit = DISKUNIT(dev);
		bdsw = &bdevsw[maj];

		name = findblkname(maj);
		if (name == NULL)
			goto unwind;

		snprintf(ch_entry->src_devname, sizeof(ch_entry->src_devname),
		    "%s%d%c", name, unit, part + 'a');
		name = ch_entry->src_devname;

		/* open device */
		error = bdsw->d_open(dev, FREAD | FWRITE , S_IFBLK, curproc);

		/* get disklabel */
		error = bdsw->d_ioctl(dev, DIOCGDINFO, (void *)&label,
		    0, NULL);
		if (error) {
			printf("%s: %s can't obtain disklabel\n",
			    DEVNAME(sc), name);
			bdsw->d_close(dev, FWRITE, S_IFBLK, curproc);
			goto unwind;
		}

		/* make sure the partition is of the right type */
		if (label.d_partitions[part].p_fstype != FS_RAID) {
			printf("%s: %s partition not of type RAID (%d)\n",
			    DEVNAME(sc), name,
			    label.d_partitions[part].p_fstype);
			bdsw->d_close(dev, FWRITE, S_IFBLK, curproc);
			goto unwind;
		}

		/* get partition size */
		ch_entry->src_size = size = DL_GETPSIZE(&label.d_partitions[part]) -
		    SR_META_SIZE - SR_META_OFFSET;
		if (size <= 0) {
			printf("%s: %s partition too small\n",
			    DEVNAME(sc), name);
			bdsw->d_close(dev, FWRITE, S_IFBLK, curproc);
			goto unwind;
		}


		ch_entry->src_dev_mm = dev; /* major/minor */

		DNPRINTF(SR_D_IOCTL, "%s: found %s size %d\n", DEVNAME(sc),
		    name, size);
	}

	return (0);
unwind:
	printf("%s: invalid device: %s\n", DEVNAME(sc), name ? name : "nodev");
	return (1);
}

int
sr_read_meta(struct sr_discipline *sd)
{
	struct sr_softc		*sc = sd->sd_sc;
	struct sr_chunk_head	*cl = &sd->sd_vol.sv_chunk_list;
	struct sr_metadata	*sm = sd->sd_meta, *m;
	struct sr_chunk		*ch_entry;
	struct buf		b;
	struct sr_vol_meta	*mv;
	struct sr_chunk_meta	*mc;
	size_t			sz = SR_META_SIZE * 512;
	int			no_chunk = 0;
	u_int32_t		volid, ondisk = 0, cid;

	DNPRINTF(SR_D_META, "%s: sr_read_meta\n", DEVNAME(sc));

	m = malloc(sz , M_DEVBUF, M_WAITOK | M_ZERO);

	SLIST_FOREACH(ch_entry, cl, src_link) {
		bzero(&b, sizeof(b));

		b.b_flags = B_READ;
		b.b_blkno = SR_META_OFFSET;
		b.b_bcount = sz;
		b.b_bufsize = sz;
		b.b_resid = sz;
		b.b_data = (void *)m;
		b.b_error = 0;
		b.b_proc = curproc;
		b.b_dev = ch_entry->src_dev_mm;
		b.b_vp = NULL;
		b.b_iodone = NULL;
		LIST_INIT(&b.b_dep);
		bdevsw_lookup(b.b_dev)->d_strategy(&b);
		biowait(&b);

		/* XXX mark chunk offline and restart metadata write */
		if (b.b_flags & B_ERROR) {
			printf("%s: %s i/o error on block %d while reading "
			    "metadata %d\n", DEVNAME(sc),
			    ch_entry->src_devname, b.b_blkno, b.b_error);
			continue;
		}

		if (m->ssd_magic != SR_MAGIC)
			continue;

		/* validate metadata */
		if (sr_validate_metadata(sc, ch_entry->src_dev_mm, m)) {
			printf("%s: invalid metadata\n", DEVNAME(sc));
			no_chunk = -1;
			goto bad;
		}

		mv = (struct sr_vol_meta *)(m + 1);
		mc = (struct sr_chunk_meta *)(mv + 1);

		/* we asssume that the first chunk has the initial metadata */
		if (no_chunk++ == 0) {
			bcopy(m, sm, sz);
			bcopy(m, sd->sd_meta, sizeof(*sd->sd_meta));
			bcopy(mv, &sd->sd_vol.sv_meta,
			    sizeof(sd->sd_vol.sv_meta));

			volid = m->ssd_vd_volid;
			sd->sd_meta_flags = sm->ssd_flags;
		}

		if (bcmp(&sm->ssd_uuid, &sd->sd_vol.sv_meta.svm_uuid,
		    sizeof(struct sr_uuid))) {
			printf("%s: %s invalid chunk uuid ",
			    DEVNAME(sc), ch_entry->src_devname);
			sr_print_uuid(&sm->ssd_uuid, 0);
			printf(", expected ");
			sr_print_uuid(&sd->sd_vol.sv_meta.svm_uuid, 1);
			no_chunk = -1;
			goto bad;
		}

		/* we have meta data on disk */
		ch_entry->src_meta_ondisk = 1;

		/* make sure we are part of this vd */
		if (volid != m->ssd_vd_volid) {
			printf("%s: %s invalid volume id %d, expected %d\n",
			    DEVNAME(sc), ch_entry->src_devname,
			    volid, m->ssd_vd_volid);
			no_chunk = -1;
			goto bad;
		}

		if (m->ssd_chunk_id > m->ssd_chunk_no) {
			printf("%s: %s chunk id out of range %d, expected "
			    "lower than %d\n", DEVNAME(sc),
			    ch_entry->src_devname,
			    m->ssd_chunk_id, m->ssd_chunk_no);
			no_chunk = -1;
			goto bad;
		}

		if (sd->sd_vol.sv_chunks[m->ssd_chunk_id]) {
			printf("%s: %s chunk id %d already in use\n",
			    DEVNAME(sc), ch_entry->src_devname,
			    m->ssd_chunk_id);
			no_chunk = -1;
			goto bad;
		}

		sd->sd_vol.sv_chunks[m->ssd_chunk_id] = ch_entry;
		bcopy(mc + m->ssd_chunk_id, &ch_entry->src_meta,
		    sizeof(ch_entry->src_meta));

		if (ondisk == 0) {
			ondisk = m->ssd_ondisk;
			cid = m->ssd_chunk_id;
		}

		if (m->ssd_ondisk != ondisk) {
			printf("%s: %s chunk id %d contains stale metadata\n",
			    DEVNAME(sc), ch_entry->src_devname,
			    m->ssd_ondisk < ondisk ? m->ssd_chunk_id : cid);
			no_chunk = -1;
			goto bad;
		}
	}

	if (no_chunk != m->ssd_chunk_no) {
		DNPRINTF(SR_D_META, "%s: not enough chunks supplied\n",
		    DEVNAME(sc));
		no_chunk = -1;
		goto bad;
	}

	DNPRINTF(SR_D_META, "%s: sr_read_meta: found %d elements\n",
	    DEVNAME(sc), no_chunk);

	sr_print_metadata(m);

bad:
	/* return nr of chunks that contain metadata */
	free(m, M_DEVBUF);
	return (no_chunk);
}

int
sr_create_chunk_meta(struct sr_softc *sc, struct sr_chunk_head *cl)
{
	struct sr_chunk		*ch_entry;
	struct sr_uuid		uuid;
	int			rv = 1, cid = 0;
	char			*name;
	u_int64_t		max_chunk_sz = 0, min_chunk_sz;

	DNPRINTF(SR_D_IOCTL, "%s: sr_create_chunk_meta\n", DEVNAME(sc));

	sr_get_uuid(&uuid);

	/* fill out stuff and get largest chunk size while looping */
	SLIST_FOREACH(ch_entry, cl, src_link) {
		name = ch_entry->src_devname;
		ch_entry->src_meta.scm_size = ch_entry->src_size;
		ch_entry->src_meta.scm_chunk_id = cid++;
		ch_entry->src_meta.scm_status = BIOC_SDONLINE;
		strlcpy(ch_entry->src_meta.scm_devname, name,
		    sizeof(ch_entry->src_meta.scm_devname));
		bcopy(&uuid,  &ch_entry->src_meta.scm_uuid,
		    sizeof(ch_entry->src_meta.scm_uuid));

		if (ch_entry->src_meta.scm_size > max_chunk_sz)
			max_chunk_sz = ch_entry->src_meta.scm_size;
	}

	/* get smallest chunk size */
	min_chunk_sz = max_chunk_sz;
	SLIST_FOREACH(ch_entry, cl, src_link)
		if (ch_entry->src_meta.scm_size < min_chunk_sz)
			min_chunk_sz = ch_entry->src_meta.scm_size;

	/* equalize all sizes */
	SLIST_FOREACH(ch_entry, cl, src_link)
		ch_entry->src_meta.scm_coerced_size = min_chunk_sz;

	/* whine if chunks are not the same size */
	if (min_chunk_sz != max_chunk_sz)
		printf("%s: chunk sizes are not equal; up to %llu blocks "
		    "wasted per chunk\n",
		    DEVNAME(sc), max_chunk_sz - min_chunk_sz);

	rv = 0;

	return (rv);
}

void
sr_unwind_chunks(struct sr_softc *sc, struct sr_chunk_head *cl)
{
	struct sr_chunk		*ch_entry, *ch_next;
	dev_t			dev;

	DNPRINTF(SR_D_IOCTL, "%s: sr_unwind_chunks\n", DEVNAME(sc));

	if (!cl)
		return;

	for (ch_entry = SLIST_FIRST(cl);
	    ch_entry != SLIST_END(cl); ch_entry = ch_next) {
		ch_next = SLIST_NEXT(ch_entry, src_link);

		dev = ch_entry->src_dev_mm;

		if (dev != NODEV)
			bdevsw_lookup(dev)->d_close(dev, FWRITE, S_IFBLK,
			    curproc);

		free(ch_entry, M_DEVBUF);
	}
	SLIST_INIT(cl);
}

void
sr_free_discipline(struct sr_discipline *sd)
{
#ifdef SR_DEBUG
	struct sr_softc		*sc = sd->sd_sc;
#endif
	if (!sd)
		return;

	DNPRINTF(SR_D_DIS, "%s: sr_free_discipline %s\n",
	    DEVNAME(sc), sd->sd_vol.sv_meta.svm_devname);

	if (sd->sd_free_resources)
		sd->sd_free_resources(sd);
	if (sd->sd_vol.sv_chunks)
		free(sd->sd_vol.sv_chunks, M_DEVBUF);
	free(sd, M_DEVBUF);
}

void
sr_shutdown_discipline(struct sr_discipline *sd)
{
	struct sr_softc		*sc = sd->sd_sc;
	int			s;

	if (!sd || !sc)
		return;

	DNPRINTF(SR_D_DIS, "%s: sr_shutdown_discipline %s\n",
	    DEVNAME(sc), sd->sd_vol.sv_meta.svm_devname);

	s = splbio();

	/* make sure there isn't a sync pending and yield */
	wakeup(sd);
	while (sd->sd_sync || sd->sd_must_flush)
		if (tsleep(&sd->sd_sync, MAXPRI, "sr_down", 60 * hz) ==
		    EWOULDBLOCK)
			break;

#ifndef SMALL_KERNEL
	sr_delete_sensors(sd);
#endif /* SMALL_KERNEL */

	if (sd->sd_scsibus_dev)
		config_detach(sd->sd_scsibus_dev, DETACH_FORCE);

	sr_unwind_chunks(sc, &sd->sd_vol.sv_chunk_list);

	if (sd)
		sr_free_discipline(sd);

	splx(s);
}

int
sr_raid_inquiry(struct sr_workunit *wu)
{
	struct sr_discipline	*sd = wu->swu_dis;
	struct scsi_xfer	*xs = wu->swu_xs;
	struct scsi_inquiry_data inq;

	DNPRINTF(SR_D_DIS, "%s: sr_raid_inquiry\n", DEVNAME(sd->sd_sc));

	bzero(&inq, sizeof(inq));
	inq.device = T_DIRECT;
	inq.dev_qual2 = 0;
	inq.version = 2;
	inq.response_format = 2;
	inq.additional_length = 32;
	strlcpy(inq.vendor, sd->sd_vol.sv_meta.svm_vendor,
	    sizeof(inq.vendor));
	strlcpy(inq.product, sd->sd_vol.sv_meta.svm_product,
	    sizeof(inq.product));
	strlcpy(inq.revision, sd->sd_vol.sv_meta.svm_revision,
	    sizeof(inq.revision));
	sr_copy_internal_data(xs, &inq, sizeof(inq));

	return (0);
}

int
sr_raid_read_cap(struct sr_workunit *wu)
{
	struct sr_discipline	*sd = wu->swu_dis;
	struct scsi_xfer	*xs = wu->swu_xs;
	struct scsi_read_cap_data rcd;

	DNPRINTF(SR_D_DIS, "%s: sr_raid_read_cap\n", DEVNAME(sd->sd_sc));

	bzero(&rcd, sizeof(rcd));
	_lto4b(sd->sd_vol.sv_meta.svm_size, rcd.addr);
	_lto4b(512, rcd.length);
	sr_copy_internal_data(xs, &rcd, sizeof(rcd));

	return (0);
}

int
sr_raid_tur(struct sr_workunit *wu)
{
	struct sr_discipline	*sd = wu->swu_dis;

	DNPRINTF(SR_D_DIS, "%s: sr_raid_tur\n", DEVNAME(sd->sd_sc));

	if (sd->sd_vol.sv_meta.svm_status == BIOC_SVOFFLINE) {
		sd->sd_scsi_sense.error_code = SSD_ERRCODE_CURRENT;
		sd->sd_scsi_sense.flags = SKEY_NOT_READY;
		sd->sd_scsi_sense.add_sense_code = 0x04;
		sd->sd_scsi_sense.add_sense_code_qual = 0x11;
		sd->sd_scsi_sense.extra_len = 4;
		return (1);
	} else if (sd->sd_vol.sv_meta.svm_status == BIOC_SVINVALID) {
		sd->sd_scsi_sense.error_code = SSD_ERRCODE_CURRENT;
		sd->sd_scsi_sense.flags = SKEY_HARDWARE_ERROR;
		sd->sd_scsi_sense.add_sense_code = 0x05;
		sd->sd_scsi_sense.add_sense_code_qual = 0x00;
		sd->sd_scsi_sense.extra_len = 4;
		return (1);
	}

	return (0);
}

int
sr_raid_request_sense(struct sr_workunit *wu)
{
	struct sr_discipline	*sd = wu->swu_dis;
	struct scsi_xfer	*xs = wu->swu_xs;

	DNPRINTF(SR_D_DIS, "%s: sr_raid_request_sense\n",
	    DEVNAME(sd->sd_sc));

	/* use latest sense data */
	bcopy(&sd->sd_scsi_sense, &xs->sense, sizeof(xs->sense));

	/* clear sense data */
	bzero(&sd->sd_scsi_sense, sizeof(sd->sd_scsi_sense));

	return (0);
}

int
sr_raid_start_stop(struct sr_workunit *wu)
{
	struct sr_discipline	*sd = wu->swu_dis;
	struct scsi_xfer	*xs = wu->swu_xs;
	struct scsi_start_stop	*ss = (struct scsi_start_stop *)xs->cmd;
	int			rv = 1;

	DNPRINTF(SR_D_DIS, "%s: sr_raid_start_stop\n",
	    DEVNAME(sd->sd_sc));

	if (!ss)
		return (rv);

	if (ss->byte2 == 0x00) {
		/* START */
		if (sd->sd_vol.sv_meta.svm_status == BIOC_SVOFFLINE) {
			/* bring volume online */
			/* XXX check to see if volume can be brought online */
			sd->sd_vol.sv_meta.svm_status = BIOC_SVONLINE;
		}
		rv = 0;
	} else /* XXX is this the check? if (byte == 0x01) */ {
		/* STOP */
		if (sd->sd_vol.sv_meta.svm_status == BIOC_SVONLINE) {
			/* bring volume offline */
			sd->sd_vol.sv_meta.svm_status = BIOC_SVOFFLINE;
		}
		rv = 0;
	}

	return (rv);
}

int
sr_raid_sync(struct sr_workunit *wu)
{
	struct sr_discipline	*sd = wu->swu_dis;
	int			s, rv = 0, ios;

	DNPRINTF(SR_D_DIS, "%s: sr_raid_sync\n", DEVNAME(sd->sd_sc));

	/* when doing a fake sync don't coun't the wu */
	ios = wu->swu_fake ? 0 : 1;

	s = splbio();
	sd->sd_sync = 1;

	while (sd->sd_wu_pending > ios)
		if (tsleep(sd, PRIBIO, "sr_sync", 15 * hz) == EWOULDBLOCK) {
			DNPRINTF(SR_D_DIS, "%s: sr_raid_sync timeout\n",
			    DEVNAME(sd->sd_sc));
			rv = 1;
			break;
		}

	sd->sd_sync = 0;
	splx(s);

	wakeup(&sd->sd_sync);

	return (rv);
}

void
sr_raid_startwu(struct sr_workunit *wu)
{
	struct sr_discipline	*sd = wu->swu_dis;
	struct sr_ccb		*ccb;

	splassert(IPL_BIO);

	if (wu->swu_state == SR_WU_RESTART)
		/*
		 * no need to put the wu on the pending queue since we
		 * are restarting the io
		 */
		 ;
	else
		/* move wu to pending queue */
		TAILQ_INSERT_TAIL(&sd->sd_wu_pendq, wu, swu_link);

	/* start all individual ios */
	TAILQ_FOREACH(ccb, &wu->swu_ccb, ccb_link) {
		bdevsw_lookup(ccb->ccb_buf.b_dev)->d_strategy(&ccb->ccb_buf);
	}
}

void
sr_raid_set_chunk_state(struct sr_discipline *sd, int c, int new_state)
{
	int			old_state, s;

	DNPRINTF(SR_D_STATE, "%s: %s: %s: sr_raid_set_chunk_state %d -> %d\n",
	    DEVNAME(sd->sd_sc), sd->sd_vol.sv_meta.svm_devname,
	    sd->sd_vol.sv_chunks[c]->src_meta.scm_devname, c, new_state);

	/* ok to go to splbio since this only happens in error path */
	s = splbio();
	old_state = sd->sd_vol.sv_chunks[c]->src_meta.scm_status;

	/* multiple IOs to the same chunk that fail will come through here */
	if (old_state == new_state)
		goto done;

	switch (old_state) {
	case BIOC_SDONLINE:
		switch (new_state) {
		case BIOC_SDOFFLINE:
			break;
		case BIOC_SDSCRUB:
			break;
		default:
			goto die;
		}
		break;

	case BIOC_SDOFFLINE:
		if (new_state == BIOC_SDREBUILD) {
			;
		} else
			goto die;
		break;

	case BIOC_SDSCRUB:
		if (new_state == BIOC_SDONLINE) {
			;
		} else
			goto die;
		break;

	case BIOC_SDREBUILD:
		if (new_state == BIOC_SDONLINE) {
			;
		} else
			goto die;
		break;

	case BIOC_SDHOTSPARE:
		if (new_state == BIOC_SDREBUILD) {
			;
		} else
			goto die;
		break;

	default:
die:
		splx(s); /* XXX */
		panic("%s: %s: %s: invalid chunk state transition "
		    "%d -> %d\n", DEVNAME(sd->sd_sc),
		    sd->sd_vol.sv_meta.svm_devname,
		    sd->sd_vol.sv_chunks[c]->src_meta.scm_devname,
		    old_state, new_state);
		/* NOTREACHED */
	}

	sd->sd_vol.sv_chunks[c]->src_meta.scm_status = new_state;
	sd->sd_set_vol_state(sd);

	sd->sd_must_flush = 1;
	workq_add_task(NULL, 0, sr_save_metadata_callback, sd, NULL);
done:
	splx(s);
}

void
sr_raid_set_vol_state(struct sr_discipline *sd)
{
	int			states[SR_MAX_STATES];
	int			new_state, i, s, nd;
	int			old_state = sd->sd_vol.sv_meta.svm_status;

	DNPRINTF(SR_D_STATE, "%s: %s: sr_raid_set_vol_state\n",
	    DEVNAME(sd->sd_sc), sd->sd_vol.sv_meta.svm_devname);

	nd = sd->sd_vol.sv_meta.svm_no_chunk;

	for (i = 0; i < SR_MAX_STATES; i++)
		states[i] = 0;

	for (i = 0; i < nd; i++) {
		s = sd->sd_vol.sv_chunks[i]->src_meta.scm_status;
		if (s > SR_MAX_STATES)
			panic("%s: %s: %s: invalid chunk state",
			    DEVNAME(sd->sd_sc),
			    sd->sd_vol.sv_meta.svm_devname,
			    sd->sd_vol.sv_chunks[i]->src_meta.scm_devname);
		states[s]++;
	}

	if (states[BIOC_SDONLINE] == nd)
		new_state = BIOC_SVONLINE;
	else if (states[BIOC_SDONLINE] == 0)
		new_state = BIOC_SVOFFLINE;
	else if (states[BIOC_SDSCRUB] != 0)
		new_state = BIOC_SVSCRUB;
	else if (states[BIOC_SDREBUILD] != 0)
		new_state = BIOC_SVREBUILD;
	else if (states[BIOC_SDOFFLINE] != 0)
		new_state = BIOC_SVDEGRADED;
	else {
		printf("old_state = %d, ", old_state);
		for (i = 0; i < nd; i++)
			printf("%d = %d, ", i,
			    sd->sd_vol.sv_chunks[i]->src_meta.scm_status);
		panic("invalid new_state");
	}

	DNPRINTF(SR_D_STATE, "%s: %s: sr_raid_set_vol_state %d -> %d\n",
	    DEVNAME(sd->sd_sc), sd->sd_vol.sv_meta.svm_devname,
	    old_state, new_state);

	switch (old_state) {
	case BIOC_SVONLINE:
		switch (new_state) {
		case BIOC_SVOFFLINE:
		case BIOC_SVDEGRADED:
			break;
		default:
			goto die;
		}
		break;

	case BIOC_SVOFFLINE:
		/* XXX this might be a little too much */
		goto die;

	case BIOC_SVSCRUB:
		switch (new_state) {
		case BIOC_SVONLINE:
		case BIOC_SVOFFLINE:
		case BIOC_SVDEGRADED:
		case BIOC_SVSCRUB: /* can go to same state */
			break;
		default:
			goto die;
		}
		break;

	case BIOC_SVBUILDING:
		switch (new_state) {
		case BIOC_SVONLINE:
		case BIOC_SVOFFLINE:
		case BIOC_SVBUILDING: /* can go to the same state */
			break;
		default:
			goto die;
		}
		break;

	case BIOC_SVREBUILD:
		switch (new_state) {
		case BIOC_SVONLINE:
		case BIOC_SVOFFLINE:
		case BIOC_SVREBUILD: /* can go to the same state */
			break;
		default:
			goto die;
		}
		break;

	case BIOC_SVDEGRADED:
		switch (new_state) {
		case BIOC_SVOFFLINE:
		case BIOC_SVREBUILD:
		case BIOC_SVDEGRADED: /* can go to the same state */
			break;
		default:
			goto die;
		}
		break;

	default:
die:
		panic("%s: %s: invalid volume state transition "
		    "%d -> %d\n", DEVNAME(sd->sd_sc),
		    sd->sd_vol.sv_meta.svm_devname,
		    old_state, new_state);
		/* NOTREACHED */
	}

	sd->sd_vol.sv_meta.svm_status = new_state;
}

u_int32_t
sr_checksum(char *s, u_int32_t *p, u_int32_t size)
{
	u_int32_t		chk = 0;
	int			i;

	DNPRINTF(SR_D_MISC, "%s: sr_checksum %p %d\n", s, p, size);

	if (size % sizeof(u_int32_t))
		return (0); /* 0 is failure */

	for (i = 0; i < size / sizeof(u_int32_t); i++)
		chk ^= p[i];

	return (chk);
}

void
sr_get_uuid(struct sr_uuid *uuid)
{
	arc4random_bytes(uuid->sui_id, sizeof(uuid->sui_id));
}

void
sr_print_uuid(struct sr_uuid *uuid, int cr)
{
	int			i;

	for (i = 0; i < SR_UUID_MAX; i++)
		printf("%x%s", uuid->sui_id[i],
		    i < SR_UUID_MAX - 1 ? ":" : "");

	if (cr)
		printf("\n");
}

int
sr_clear_metadata(struct sr_discipline *sd)
{
	struct sr_softc		*sc = sd->sd_sc;
	struct sr_chunk_head	*cl = &sd->sd_vol.sv_chunk_list;
	struct sr_chunk		*ch_entry;
	struct buf		b;
	size_t			sz = SR_META_SIZE * 512;
	void			*m;
	int			rv = 0;

	DNPRINTF(SR_D_META, "%s: sr_clear_metadata\n", DEVNAME(sc));

	m = malloc(sz , M_DEVBUF, M_WAITOK | M_ZERO);

	SLIST_FOREACH(ch_entry, cl, src_link) {
		bzero(&b, sizeof(b));

		b.b_flags = B_WRITE;
		b.b_blkno = SR_META_OFFSET;
		b.b_bcount = sz;
		b.b_bufsize = sz;
		b.b_resid = sz;
		b.b_data = (void *)m;
		b.b_error = 0;
		b.b_proc = curproc;
		b.b_dev = ch_entry->src_dev_mm;
		b.b_vp = NULL;
		b.b_iodone = NULL;
		LIST_INIT(&b.b_dep);
		bdevsw_lookup(b.b_dev)->d_strategy(&b);
		biowait(&b);

		if (b.b_flags & B_ERROR) {
			printf("%s: %s i/o error on block %d while clearing "
			    "metadata %d\n", DEVNAME(sc),
			    ch_entry->src_devname, b.b_blkno, b.b_error);
			rv++;
			continue;
		}
	}

	free(m, M_DEVBUF);
	return (rv);
}

int
sr_already_assembled(struct sr_discipline *sd)
{
	struct sr_softc		*sc = sd->sd_sc;
	int			i;

	for (i = 0; i < SR_MAXSCSIBUS; i++)
		if (sc->sc_dis[i])
			if (!bcmp(&sd->sd_meta->ssd_uuid,
			    &sc->sc_dis[i]->sd_meta->ssd_uuid,
			    sizeof(sd->sd_meta->ssd_uuid)))
				return (1);

	return (0);
}

void
sr_save_metadata_callback(void *arg1, void *arg2)
{
	struct sr_discipline	*sd = arg1;
	int			s;

	s = splbio();

	if (sr_save_metadata(arg1, SR_VOL_DIRTY))
		printf("%s: save metadata failed\n",
		    DEVNAME(sd->sd_sc));

	sd->sd_must_flush = 0;
	splx(s);
}

int
sr_save_metadata(struct sr_discipline *sd, u_int32_t flags)
{
	struct sr_softc		*sc = sd->sd_sc;
	struct sr_metadata	*sm = sd->sd_meta;
	struct sr_vol_meta	*sv = &sd->sd_vol.sv_meta, *im_sv;
	struct sr_chunk_meta	*im_sc;
	struct sr_chunk		*src;
	struct buf		b;
	struct sr_workunit	wu;
	int			i, rv = 1, ch = 0;
	size_t			sz = SR_META_SIZE * 512;

	DNPRINTF(SR_D_META, "%s: sr_save_metadata %s\n",
	    DEVNAME(sc), sd->sd_vol.sv_meta.svm_devname);

	if (!sm) {
		printf("%s: no in memory copy of metadata\n", DEVNAME(sc));
		goto bad;
	}

	im_sv = (struct sr_vol_meta *)(sm + 1);
	im_sc = (struct sr_chunk_meta *)(im_sv + 1);

	if (sizeof(struct sr_metadata) + sizeof(struct sr_vol_meta) +
	    (sizeof(struct sr_chunk_meta) * sd->sd_vol.sv_meta.svm_no_chunk) >
	    sz) {
		printf("%s: too much metadata; metadata NOT written\n",
		    DEVNAME(sc));
		goto bad;
	}

	if (sm->ssd_magic == 0) {
		/* initial metadata */
		sm->ssd_magic = SR_MAGIC;
		sm->ssd_version = SR_META_VERSION;
		sm->ssd_size = sizeof(struct sr_metadata);
		sm->ssd_ondisk = 0;
		sm->ssd_flags = sd->sd_meta_flags;
		/* get uuid from chunk 0 */
		bcopy(&sd->sd_vol.sv_chunks[0]->src_meta.scm_uuid,
		    &sm->ssd_uuid,
		    sizeof(struct sr_uuid));

		/* volume */
		bcopy(sv, im_sv, sizeof(struct sr_vol_meta));
		bcopy(&sm->ssd_uuid, &im_sv->svm_uuid,
		    sizeof(im_sv->svm_uuid));
		sm->ssd_vd_ver = SR_VOL_VERSION;
		sm->ssd_vd_size = sizeof(struct sr_vol_meta);

		/* chunk */
		for (i = 0; i < sd->sd_vol.sv_meta.svm_no_chunk; i++)
			bcopy(sd->sd_vol.sv_chunks[i], &im_sc[i],
			    sizeof(struct sr_chunk_meta));

		sm->ssd_chunk_ver = SR_CHUNK_VERSION;
		sm->ssd_chunk_size = sizeof(struct sr_chunk_meta);
		sm->ssd_chunk_no = sd->sd_vol.sv_meta.svm_no_chunk;

		/* optional */
		sm->ssd_opt_ver = SR_OPT_VERSION;
		sm->ssd_opt_size = 0; /* unused */
		sm->ssd_opt_no = 0; /* unused */
	}

	/* from here on out metadata is updated */
	sm->ssd_ondisk++;
	im_sv->svm_flags |= flags;
	sm->ssd_vd_chk = sr_checksum(DEVNAME(sc),
	    (u_int32_t *)im_sv, sm->ssd_vd_size);

	sm->ssd_chunk_chk = 0;
	for (ch = 0; ch < sm->ssd_chunk_no; ch++)
		sm->ssd_chunk_chk ^= sr_checksum(DEVNAME(sc),
		    (u_int32_t *)&im_sc[ch], sm->ssd_chunk_size);

	sr_print_metadata(sm);

	for (i = 0; i < sm->ssd_chunk_no; i++) {
		memset(&b, 0, sizeof(b));

		src = sd->sd_vol.sv_chunks[i];

		/* skip disks that are offline */
		if (src->src_meta.scm_status == BIOC_SDOFFLINE)
			continue;

		/* calculate metdata checksum and ids */
		sm->ssd_vd_volid = im_sv->svm_volid;
		sm->ssd_chunk_id = i;
		sm->ssd_checksum = sr_checksum(DEVNAME(sc),
		    (u_int32_t *)sm, sm->ssd_size);
		DNPRINTF(SR_D_META, "%s: sr_save_metadata %s: volid: %d "
		    "chunkid: %d checksum: 0x%x\n",
		    DEVNAME(sc), src->src_meta.scm_devname,
		    sm->ssd_vd_volid, sm->ssd_chunk_id,
		    sm->ssd_checksum);

		b.b_flags = B_WRITE;
		b.b_blkno = SR_META_OFFSET;
		b.b_bcount = sz;
		b.b_bufsize = sz;
		b.b_resid = sz;
		b.b_data = (void *)sm;
		b.b_error = 0;
		b.b_proc = curproc;
		b.b_dev = src->src_dev_mm;
		b.b_vp = NULL;
		b.b_iodone = NULL;
		LIST_INIT(&b.b_dep);
		bdevsw_lookup(b.b_dev)->d_strategy(&b);
		biowait(&b);

		/* make sure in memory copy is clean */
		sm->ssd_vd_volid = 0;
		sm->ssd_chunk_id = 0;
		sm->ssd_checksum = 0;

		/* XXX do something smart here */
		/* mark chunk offline and restart metadata write */
		if (b.b_flags & B_ERROR) {
			printf("%s: %s i/o error on block %d while writing "
			    "metadata %d\n", DEVNAME(sc),
			    src->src_meta.scm_devname, b.b_blkno, b.b_error);
			goto bad;
		}

		DNPRINTF(SR_D_META, "%s: sr_save_metadata written to %s\n",
		    DEVNAME(sc), src->src_meta.scm_devname);
	}

	bzero(&wu, sizeof(wu));
	wu.swu_fake = 1;
	wu.swu_dis = sd;
	sd->sd_scsi_sync(&wu);

	rv = 0;
bad:
	return (rv);
}

int
sr_boot_assembly(struct sr_softc *sc)
{
	struct device		*dv;
	struct buf		*bp;
	struct bdevsw		*bdsw;
	struct disklabel	label;
	struct sr_metadata	*sm;
	struct sr_metadata_list_head mlh;
	struct sr_metadata_list *mle, *mle2;
	struct sr_vol_meta	*vm;
	struct bioc_createraid	bc;
	dev_t			dev, devr, *dt = NULL;
	int			error, majdev, i, no_dev, rv = 0;
	size_t			sz = SR_META_SIZE * 512;

	DNPRINTF(SR_D_META, "%s: sr_boot_assembly\n", DEVNAME(sc));

	SLIST_INIT(&mlh);
	bp = geteblk(sz);
	if (!bp)
		return (ENOMEM);

	TAILQ_FOREACH(dv, &alldevs, dv_list) {
		if (dv->dv_class != DV_DISK)
			continue;

		majdev = findblkmajor(dv);
		if (majdev == -1)
			continue;

		bp->b_dev = dev = MAKEDISKDEV(majdev, dv->dv_unit, RAW_PART);
		bdsw = &bdevsw[majdev];

		/* XXX is there  a better way of excluding some devices? */
		if (!strncmp(dv->dv_xname, "fd", 2) ||
		    !strncmp(dv->dv_xname, "cd", 2) ||
		    !strncmp(dv->dv_xname, "rx", 2))
			continue;
		/*
		 * The devices are being opened with S_IFCHR instead of
		 * S_IFBLK so that the SCSI mid-layer does not whine when
		 * media is not inserted in certain devices like zip drives
		 * and such.
		 */

		/* open device */
		error = (*bdsw->d_open)(dev, FREAD, S_IFCHR, curproc);
		if (error) {
			DNPRINTF(SR_D_META, "%s: sr_boot_assembly open failed"
			    "\n", DEVNAME(sc));
			continue;
		}

		/* get disklabel */
		error = (*bdsw->d_ioctl)(dev, DIOCGDINFO, (void *)&label,
		    FREAD, curproc);
		if (error) {
			DNPRINTF(SR_D_META, "%s: sr_boot_assembly ioctl "
			    "failed\n", DEVNAME(sc));
			error = (*bdsw->d_close)(dev, FREAD, S_IFCHR, curproc);
			continue;
		}

		/* we are done, close device */
		error = (*bdsw->d_close)(dev, FREAD, S_IFCHR, curproc);
		if (error) {
			DNPRINTF(SR_D_META, "%s: sr_boot_assembly close "
			    "failed\n", DEVNAME(sc));
			continue;
		}

		/* are we a softraid partition? */
		for (i = 0; i < MAXPARTITIONS; i++) {
			if (label.d_partitions[i].p_fstype != FS_RAID)
				continue;

			/* open device */
			bp->b_dev = devr = MAKEDISKDEV(majdev, dv->dv_unit, i);
			error = (*bdsw->d_open)(devr, FREAD, S_IFCHR, curproc);
			if (error) {
				DNPRINTF(SR_D_META, "%s: sr_boot_assembly "
				    "open failed, partition %d\n",
				    DEVNAME(sc), i);
				continue;
			}
			/* read metadat */
			bp->b_flags = B_BUSY | B_READ;
			bp->b_blkno = SR_META_OFFSET;
			bp->b_cylinder = 0;
			bp->b_bcount = sz;
			bp->b_bufsize = sz;
			bp->b_resid = sz;
			(*bdsw->d_strategy)(bp);
			if ((error = biowait(bp))) {
				DNPRINTF(SR_D_META, "%s: sr_boot_assembly "
				    "strategy failed, partition %d\n",
				    DEVNAME(sc));
				error = (*bdsw->d_close)(devr, FREAD, S_IFCHR,
				    curproc);
				continue;
			}

			sm = (struct sr_metadata *)bp->b_data;
			if (!sr_validate_metadata(sc, devr, sm)) {
				/* we got one; save it off */
				mle = malloc(sizeof(*mle), M_DEVBUF,
				    M_WAITOK | M_ZERO);
				mle->sml_metadata = malloc(sz, M_DEVBUF,
				    M_WAITOK | M_ZERO);
				bcopy(sm, mle->sml_metadata, sz);
				mle->sml_mm = devr;
				SLIST_INSERT_HEAD(&mlh, mle, sml_link);
			}

			/* we are done, close device */
			error = (*bdsw->d_close)(devr, FREAD, S_IFCHR,
			    curproc);
			if (error) {
				DNPRINTF(SR_D_META, "%s: sr_boot_assembly "
				    "close failed\n", DEVNAME(sc));
				continue;
			}
		}
	}

	/*
	 * XXX poor mans hack that doesn't keep disks in order and does not
	 * roam disks correctly.  replace this with something smarter that
	 * orders disks by volid, chunkid and uuid.
	 */
	dt = malloc(BIOC_CRMAXLEN, M_DEVBUF, M_WAITOK);
	SLIST_FOREACH(mle, &mlh, sml_link) {
		/* chunk used already? */
		if (mle->sml_used)
			continue;

		no_dev = 0;
		bzero(dt, BIOC_CRMAXLEN);
		SLIST_FOREACH(mle2, &mlh, sml_link) {
			/* chunk used already? */
			if (mle2->sml_used)
				continue;

			/* are we the same volume? */
			if (mle->sml_metadata->ssd_vd_volid !=
			    mle2->sml_metadata->ssd_vd_volid)
				continue;

			/* same uuid? */
			if (bcmp(&mle->sml_metadata->ssd_uuid,
			    &mle2->sml_metadata->ssd_uuid,
			    sizeof(mle->sml_metadata->ssd_uuid)))
				continue;

			/* sanity */
			if (dt[mle2->sml_metadata->ssd_chunk_id]) {
				printf("%s: chunk id already in use; can not "
				    "assemble volume\n", DEVNAME(sc));
				goto unwind;
			}
			dt[mle2->sml_metadata->ssd_chunk_id] = mle2->sml_mm;
			no_dev++;
			mle2->sml_used = 1;
		}
		if (mle->sml_metadata->ssd_chunk_no != no_dev) {
			printf("%s: not assembling partial disk that used to "
			    "be volume %d\n", DEVNAME(sc),
			    mle->sml_metadata->ssd_vd_volid);
			continue;
		}

		bzero(&bc, sizeof(bc));
		vm = (struct sr_vol_meta *)(mle->sml_metadata + 1);
		bc.bc_level = vm->svm_level;
		bc.bc_dev_list_len = no_dev * sizeof(dev_t);
		bc.bc_dev_list = dt;
		bc.bc_flags = BIOC_SCDEVT;
		sr_ioctl_createraid(sc, &bc, 0);
		rv++;
	}

unwind:
	if (dt)
		free(dt, M_DEVBUF);

	for (mle = SLIST_FIRST(&mlh); mle != SLIST_END(&mlh); mle = mle2) {
		mle2 = SLIST_NEXT(mle, sml_link);

		free(mle->sml_metadata, M_DEVBUF);
		free(mle, M_DEVBUF);
	}
	SLIST_INIT(&mlh);

	return (rv);
}

int
sr_validate_metadata(struct sr_softc *sc, dev_t dev, struct sr_metadata *sm)
{
	struct sr_vol_meta	*mv;
	struct sr_chunk_meta	*mc;
	char			*name, devname[32];
	int			maj, part, unit;
	u_int32_t		chk;

	DNPRINTF(SR_D_META, "%s: sr_validate_metadata(0x%x)\n",
	    DEVNAME(sc), dev);

	bzero(devname, sizeof(devname));

	if (sm->ssd_magic != SR_MAGIC)
		goto bad;

	maj = major(dev);
	part = DISKPART(dev);
	unit = DISKUNIT(dev);

	name = findblkname(maj);
	if (name == NULL)
		goto bad;

	snprintf(devname, sizeof(devname),
	    "%s%d%c", name, unit, part + 'a');
	name = devname;

	/* validate metadata */
	if (sm->ssd_version != SR_META_VERSION) {
		printf("%s: %s can not read metadata version %d, "
		    "expected %d\n", DEVNAME(sc),
		    devname, sm->ssd_version,
		    SR_META_VERSION);
		goto bad;
	}
	if (sm->ssd_size != sizeof(struct sr_metadata)) {
		printf("%s: %s invalid metadata size %d, "
		    "expected %d\n", DEVNAME(sc),
		    devname, sm->ssd_size,
		    sizeof(struct sr_metadata));
		goto bad;
	}
	chk = sr_checksum(DEVNAME(sc), (u_int32_t *)sm, sm->ssd_size);
	/*
	 * since the checksum value is part of the checksum a good
	 * result equals 0
	 */
	if (chk != 0) {
		printf("%s: %s invalid metadata checksum 0x%x, "
		    "expected 0x%x\n", DEVNAME(sc),
		    devname, sm->ssd_checksum, chk);
		goto bad;
	}

	/* validate volume metadata */
	if (sm->ssd_vd_ver != SR_VOL_VERSION) {
		printf("%s: %s can not read volume metadata version "
		    "%d, expected %d\n", DEVNAME(sc),
		    devname, sm->ssd_vd_ver,
		    SR_VOL_VERSION);
		goto bad;
	}
	if (sm->ssd_vd_size != sizeof(struct sr_vol_meta)) {
		printf("%s: %s invalid volume metadata size %d, "
		    "expected %d\n", DEVNAME(sc),
		    devname, sm->ssd_vd_size,
		    sizeof(struct sr_vol_meta));
		goto bad;
	}
	mv = (struct sr_vol_meta *)(sm + 1);
	chk = sr_checksum(DEVNAME(sc), (u_int32_t *)mv, sm->ssd_vd_size);
	if (chk != sm->ssd_vd_chk) {
		printf("%s: %s invalid volume metadata checksum 0x%x, "
		    "expected 0x%x\n", DEVNAME(sc),
		    devname, sm->ssd_vd_chk, chk);
		goto bad;
	}

	/* validate chunk metadata */
	if (sm->ssd_chunk_ver != SR_CHUNK_VERSION) {
		printf("%s: %s can not read chunk metadata version "
		    "%d, expected %d\n", DEVNAME(sc),
		    devname, sm->ssd_chunk_ver,
		    SR_CHUNK_VERSION);
		goto bad;
	}
	if (sm->ssd_chunk_size != sizeof(struct sr_chunk_meta)) {
		printf("%s: %s invalid chunk metadata size %d, "
		    "expected %d\n", DEVNAME(sc),
		    devname, sm->ssd_chunk_size,
		    sizeof(struct sr_chunk_meta));
		goto bad;
	}

	mc = (struct sr_chunk_meta *)(mv + 1);
	/* checksum is calculated over ALL chunks */
	chk = sr_checksum(DEVNAME(sc), (u_int32_t *)(mc),
	    sm->ssd_chunk_size * sm->ssd_chunk_no);

	if (chk != sm->ssd_chunk_chk) {
		printf("%s: %s invalid chunk metadata checksum 0x%x, "
		    "expected 0x%x\n", DEVNAME(sc),
		    devname, sm->ssd_chunk_chk, chk);
		goto bad;
	}

	/* warn if disk changed order */
	if (strncmp(mc[sm->ssd_chunk_id].scm_devname, name,
	    sizeof(mc[sm->ssd_chunk_id].scm_devname)))
		printf("%s: roaming device %s -> %s\n", DEVNAME(sc),
		    mc[sm->ssd_chunk_id].scm_devname, name);

	/* we have meta data on disk */
	DNPRINTF(SR_D_META, "%s: sr_validate_metadata valid metadata %s\n",
	    DEVNAME(sc), devname);

	return (0);
bad:
	DNPRINTF(SR_D_META, "%s: sr_validate_metadata invalid metadata %s\n",
	    DEVNAME(sc), devname);

	return (1);
}

void
sr_shutdown(void *arg)
{
	struct sr_discipline	*sd = arg;
#ifdef SR_DEBUG
	struct sr_softc		*sc = sd->sd_sc;
#endif
	DNPRINTF(SR_D_DIS, "%s: sr_shutdown %s\n",
	    DEVNAME(sc), sd->sd_vol.sv_meta.svm_devname);

	sr_save_metadata(sd, 0);

	sr_shutdown_discipline(sd);
}

#ifndef SMALL_KERNEL
int
sr_create_sensors(struct sr_discipline *sd)
{
	struct sr_softc		*sc = sd->sd_sc;
	int			rv = 1;

	DNPRINTF(SR_D_STATE, "%s: %s: sr_create_sensors\n",
	    DEVNAME(sc), sd->sd_vol.sv_meta.svm_devname);

	strlcpy(sd->sd_vol.sv_sensordev.xname, DEVNAME(sc),
	    sizeof(sd->sd_vol.sv_sensordev.xname));

	sd->sd_vol.sv_sensor.type = SENSOR_DRIVE;
	sd->sd_vol.sv_sensor.status = SENSOR_S_UNKNOWN;
	strlcpy(sd->sd_vol.sv_sensor.desc, sd->sd_vol.sv_meta.svm_devname,
	    sizeof(sd->sd_vol.sv_sensor.desc));

	sensor_attach(&sd->sd_vol.sv_sensordev, &sd->sd_vol.sv_sensor);

	if (sc->sc_sensors_running == 0) {
		if (sensor_task_register(sc, sr_refresh_sensors, 10) == NULL)
			goto bad;
		sc->sc_sensors_running = 1;
	}
	sensordev_install(&sd->sd_vol.sv_sensordev);

	rv = 0;
bad:
	return (rv);
}

void
sr_delete_sensors(struct sr_discipline *sd)
{
#ifdef SR_DEBUG
	struct sr_softc		*sc = sd->sd_sc;
#endif
	DNPRINTF(SR_D_STATE, "%s: %s: sr_delete_sensors\n",
	    DEVNAME(sc), sd->sd_vol.sv_meta.svm_devname);

	if (sd->sd_vol.sv_sensor_valid)
		sensordev_deinstall(&sd->sd_vol.sv_sensordev);
}

void
sr_refresh_sensors(void *arg)
{
	struct sr_softc		*sc = arg;
	int			i, vol;
	struct sr_volume	*sv;

	DNPRINTF(SR_D_STATE, "%s: sr_refresh_sensors\n", DEVNAME(sc));

	for (i = 0, vol = -1; i < SR_MAXSCSIBUS; i++) {
		/* XXX this will not work when we stagger disciplines */
		if (!sc->sc_dis[i])
			continue;

		sv = &sc->sc_dis[i]->sd_vol;

		switch(sv->sv_meta.svm_status) {
		case BIOC_SVOFFLINE:
			sv->sv_sensor.value = SENSOR_DRIVE_FAIL;
			sv->sv_sensor.status = SENSOR_S_CRIT;
			break;

		case BIOC_SVDEGRADED:
			sv->sv_sensor.value = SENSOR_DRIVE_PFAIL;
			sv->sv_sensor.status = SENSOR_S_WARN;
			break;

		case BIOC_SVSCRUB:
		case BIOC_SVONLINE:
			sv->sv_sensor.value = SENSOR_DRIVE_ONLINE;
			sv->sv_sensor.status = SENSOR_S_OK;
			break;

		default:
			sv->sv_sensor.value = 0; /* unknown */
			sv->sv_sensor.status = SENSOR_S_UNKNOWN;
		}
	}
}
#endif /* SMALL_KERNEL */

#ifdef SR_FANCY_STATS
void				sr_print_stats(void);

void
sr_print_stats(void)
{
	struct sr_softc		*sc;
	struct sr_discipline	*sd;
	int			i, vol;

	for (i = 0; i < softraid_cd.cd_ndevs; i++)
		if (softraid_cd.cd_devs[i]) {
			sc = softraid_cd.cd_devs[i];
			/* we'll only have one softc */
			break;
		}

	if (!sc) {
		printf("no softraid softc found\n");
		return;
	}

	for (i = 0, vol = -1; i < SR_MAXSCSIBUS; i++) {
		/* XXX this will not work when we stagger disciplines */
		if (!sc->sc_dis[i])
			continue;

		sd = sc->sc_dis[i];
		printf("%s: ios pending: %d  collisions %llu\n",
		    sd->sd_vol.sv_meta.svm_devname,
		    sd->sd_wu_pending,
		    sd->sd_wu_collisions);
	}
}
#endif /* SR_FANCY_STATS */

#ifdef SR_DEBUG
void
sr_print_metadata(struct sr_metadata *sm)
{
	struct sr_vol_meta	*im_sv;
	struct sr_chunk_meta	*im_sc;
	int			ch;

	im_sv = (struct sr_vol_meta *)(sm + 1);
	im_sc = (struct sr_chunk_meta *)(im_sv + 1);

	DNPRINTF(SR_D_META, "\tmeta magic 0x%llx\n", sm->ssd_magic);
	DNPRINTF(SR_D_META, "\tmeta version %d\n", sm->ssd_version);
	DNPRINTF(SR_D_META, "\tmeta checksum 0x%x\n", sm->ssd_checksum);
	DNPRINTF(SR_D_META, "\tmeta size %d\n", sm->ssd_size);
	DNPRINTF(SR_D_META, "\tmeta on disk version %u\n", sm->ssd_ondisk);
	DNPRINTF(SR_D_META, "\tmeta uuid ");
	sr_print_uuid(&sm->ssd_uuid, 1);
	DNPRINTF(SR_D_META, "\tvd version %d\n", sm->ssd_vd_ver);
	DNPRINTF(SR_D_META, "\tvd size %lu\n", sm->ssd_vd_size);
	DNPRINTF(SR_D_META, "\tvd id %u\n", sm->ssd_vd_volid);
	DNPRINTF(SR_D_META, "\tvd checksum 0x%x\n", sm->ssd_vd_chk);
	DNPRINTF(SR_D_META, "\tchunk version %d\n", sm->ssd_chunk_ver);
	DNPRINTF(SR_D_META, "\tchunks %d\n", sm->ssd_chunk_no);
	DNPRINTF(SR_D_META, "\tchunk size %u\n", sm->ssd_chunk_size);
	DNPRINTF(SR_D_META, "\tchunk id %u\n", sm->ssd_chunk_id);
	DNPRINTF(SR_D_META, "\tchunk checksum 0x%x\n", sm->ssd_chunk_chk);

	DNPRINTF(SR_D_META, "\t\tvol id %d\n", im_sv->svm_volid);
	DNPRINTF(SR_D_META, "\t\tvol status %d\n", im_sv->svm_status);
	DNPRINTF(SR_D_META, "\t\tvol flags 0x%x\n", im_sv->svm_flags);
	DNPRINTF(SR_D_META, "\t\tvol level %d\n", im_sv->svm_level);
	DNPRINTF(SR_D_META, "\t\tvol size %lld\n", im_sv->svm_size);
	DNPRINTF(SR_D_META, "\t\tvol name %s\n", im_sv->svm_devname);
	DNPRINTF(SR_D_META, "\t\tvol vendor %s\n", im_sv->svm_vendor);
	DNPRINTF(SR_D_META, "\t\tvol prod %s\n", im_sv->svm_product);
	DNPRINTF(SR_D_META, "\t\tvol rev %s\n", im_sv->svm_revision);
	DNPRINTF(SR_D_META, "\t\tvol no chunks %d\n", im_sv->svm_no_chunk);
	DNPRINTF(SR_D_META, "\t\tvol uuid ");
	sr_print_uuid(& im_sv->svm_uuid, 1);

	for (ch = 0; ch < im_sv->svm_no_chunk; ch++) {
		DNPRINTF(SR_D_META, "\t\t\tchunk vol id %d\n",
		    im_sc[ch].scm_volid);
		DNPRINTF(SR_D_META, "\t\t\tchunk id %d\n",
		    im_sc[ch].scm_chunk_id);
		DNPRINTF(SR_D_META, "\t\t\tchunk status %d\n",
		    im_sc[ch].scm_status);
		DNPRINTF(SR_D_META, "\t\t\tchunk name %s\n",
		    im_sc[ch].scm_devname);
		DNPRINTF(SR_D_META, "\t\t\tchunk size %lld\n",
		    im_sc[ch].scm_size);
		DNPRINTF(SR_D_META, "\t\t\tchunk coerced size %lld\n",
		    im_sc[ch].scm_coerced_size);
		DNPRINTF(SR_D_META, "\t\t\tchunk uuid ");
		sr_print_uuid(&im_sc[ch].scm_uuid, 1);
	}
}
#endif /* SR_DEBUG */