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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011 Bayard G. Bell. All rights reserved.
*/
/*
* Md - is the meta-disk driver. It sits below the UFS file system
* but above the 'real' disk drivers, xy, id, sd etc.
*
* To the UFS software, md looks like a normal driver, since it has
* the normal kinds of entries in the bdevsw and cdevsw arrays. So
* UFS accesses md in the usual ways. In particular, the strategy
* routine, mdstrategy(), gets called by fbiwrite(), ufs_getapage(),
* and ufs_writelbn().
*
* Md maintains an array of minor devices (meta-partitions). Each
* meta partition stands for a matrix of real partitions, in rows
* which are not necessarily of equal length. Md maintains a table,
* with one entry for each meta-partition, which lists the rows and
* columns of actual partitions, and the job of the strategy routine
* is to translate from the meta-partition device and block numbers
* known to UFS into the actual partitions' device and block numbers.
*
* See below, in mdstrategy(), mdreal(), and mddone() for details of
* this translation.
*/
/*
* Driver for Virtual Disk.
*/
#include <sys/user.h>
#include <sys/sysmacros.h>
#include <sys/conf.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/file.h>
#include <sys/open.h>
#include <sys/dkio.h>
#include <sys/vtoc.h>
#include <sys/cmn_err.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/debug.h>
#include <sys/utsname.h>
#include <sys/lvm/mdvar.h>
#include <sys/lvm/md_names.h>
#include <sys/lvm/md_mddb.h>
#include <sys/lvm/md_sp.h>
#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/cladm.h>
#include <sys/priv_names.h>
#include <sys/modhash.h>
int md_init_debug = 0; /* module binding debug */
/*
* Tunable to turn off the failfast behavior.
*/
int md_ff_disable = 0;
/*
* dynamically allocated list of non FF driver names - needs to
* be freed when md is detached.
*/
char **non_ff_drivers = NULL;
md_krwlock_t md_unit_array_rw; /* protects all unit arrays */
md_krwlock_t nm_lock; /* protects all the name spaces */
md_resync_t md_cpr_resync;
extern char svm_bootpath[];
#define SVM_PSEUDO_STR "/pseudo/md@0:"
#define VERSION_LENGTH 6
#define VERSION "1.0"
/*
* Keep track of possible 'orphan' entries in the name space
*/
int *md_nm_snarfed = NULL;
/*
* Global tunable giving the percentage of free space left in replica during
* conversion of non-devid style replica to devid style replica.
*/
int md_conv_perc = MDDB_DEVID_CONV_PERC;
#ifdef DEBUG
/* debug code to verify framework exclusion guarantees */
int md_in;
kmutex_t md_in_mx; /* used to md global stuff */
#define IN_INIT 0x01
#define IN_FINI 0x02
#define IN_ATTACH 0x04
#define IN_DETACH 0x08
#define IN_OPEN 0x10
#define MD_SET_IN(x) { \
mutex_enter(&md_in_mx); \
if (md_in) \
debug_enter("MD_SET_IN exclusion lost"); \
if (md_in & x) \
debug_enter("MD_SET_IN already set"); \
md_in |= x; \
mutex_exit(&md_in_mx); \
}
#define MD_CLR_IN(x) { \
mutex_enter(&md_in_mx); \
if (md_in & ~(x)) \
debug_enter("MD_CLR_IN exclusion lost"); \
if (!(md_in & x)) \
debug_enter("MD_CLR_IN already clr"); \
md_in &= ~x; \
mutex_exit(&md_in_mx); \
}
#else /* DEBUG */
#define MD_SET_IN(x)
#define MD_CLR_IN(x)
#endif /* DEBUG */
hrtime_t savetime1, savetime2;
/*
* list things protected by md_mx even if they aren't
* used in this file.
*/
kmutex_t md_mx; /* used to md global stuff */
kcondvar_t md_cv; /* md_status events */
int md_status = 0; /* global status for the meta-driver */
int md_num_daemons = 0;
int md_ioctl_cnt = 0;
int md_mtioctl_cnt = 0; /* multithreaded ioctl cnt */
uint_t md_mdelay = 10; /* variable so can be patched */
int (*mdv_strategy_tstpnt)(buf_t *, int, void*);
major_t md_major, md_major_targ;
unit_t md_nunits = MD_MAXUNITS;
set_t md_nsets = MD_MAXSETS;
int md_nmedh = 0;
char *md_med_trans_lst = NULL;
md_set_t md_set[MD_MAXSETS];
md_set_io_t md_set_io[MD_MAXSETS];
md_krwlock_t hsp_rwlp; /* protects hot_spare_interface */
md_krwlock_t ni_rwlp; /* protects notify_interface */
md_ops_t **md_ops = NULL;
ddi_modhandle_t *md_mods = NULL;
md_ops_t *md_opslist;
clock_t md_hz;
md_event_queue_t *md_event_queue = NULL;
int md_in_upgrade;
int md_keep_repl_state;
int md_devid_destroy;
/* for sending messages thru a door to userland */
door_handle_t mdmn_door_handle = NULL;
int mdmn_door_did = -1;
dev_info_t *md_devinfo = NULL;
md_mn_nodeid_t md_mn_mynode_id = ~0u; /* My node id (for multi-node sets) */
static uint_t md_ocnt[OTYPCNT];
static int mdinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int mdattach(dev_info_t *, ddi_attach_cmd_t);
static int mddetach(dev_info_t *, ddi_detach_cmd_t);
static int mdopen(dev_t *, int, int, cred_t *);
static int mdclose(dev_t, int, int, cred_t *);
static int mddump(dev_t, caddr_t, daddr_t, int);
static int mdread(dev_t, struct uio *, cred_t *);
static int mdwrite(dev_t, struct uio *, cred_t *);
static int mdaread(dev_t, struct aio_req *, cred_t *);
static int mdawrite(dev_t, struct aio_req *, cred_t *);
static int mdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int mdprop_op(dev_t, dev_info_t *,
ddi_prop_op_t, int, char *, caddr_t, int *);
static struct cb_ops md_cb_ops = {
mdopen, /* open */
mdclose, /* close */
mdstrategy, /* strategy */
/* print routine -- none yet */
(int(*)(dev_t, char *))nulldev,
mddump, /* dump */
mdread, /* read */
mdwrite, /* write */
mdioctl, /* ioctl */
/* devmap */
(int(*)(dev_t, devmap_cookie_t, offset_t, size_t, size_t *,
uint_t))nodev,
/* mmap */
(int(*)(dev_t, off_t, int))nodev,
/* segmap */
(int(*)(dev_t, off_t, struct as *, caddr_t *, off_t, unsigned,
unsigned, unsigned, cred_t *))nodev,
nochpoll, /* poll */
mdprop_op, /* prop_op */
0, /* streamtab */
(D_64BIT|D_MP|D_NEW), /* driver compatibility flag */
CB_REV, /* cb_ops version */
mdaread, /* aread */
mdawrite, /* awrite */
};
static struct dev_ops md_devops = {
DEVO_REV, /* dev_ops version */
0, /* device reference count */
mdinfo, /* info routine */
nulldev, /* identify routine */
nulldev, /* probe - not defined */
mdattach, /* attach routine */
mddetach, /* detach routine */
nodev, /* reset - not defined */
&md_cb_ops, /* driver operations */
NULL, /* bus operations */
nodev, /* power management */
ddi_quiesce_not_needed, /* quiesce */
};
/*
* loadable module wrapper
*/
#include <sys/modctl.h>
static struct modldrv modldrv = {
&mod_driverops, /* type of module -- a pseudodriver */
"Solaris Volume Manager base module", /* name of the module */
&md_devops, /* driver ops */
};
static struct modlinkage modlinkage = {
MODREV_1,
(void *)&modldrv,
NULL
};
/* md_medd.c */
extern void med_init(void);
extern void med_fini(void);
extern void md_devid_cleanup(set_t, uint_t);
/* md_names.c */
extern struct nm_next_hdr *get_first_record(set_t, int, int);
int md_maxphys = 0; /* maximum io size in bytes */
#define MD_MAXBCOUNT (1024 * 1024)
unsigned md_maxbcount = 0; /* maximum physio size in bytes */
/*
* Some md ioctls trigger io framework device tree operations. An
* example is md ioctls that call md_resolve_bydevid(): which uses the
* io framework to resolve a devid. Such operations result in acquiring
* io framework locks (like ndi_devi_enter() of "/") while holding
* driver locks (like md_unit_writerlock()).
*
* The prop_op(9E) entry point is called from the devinfo driver with
* an active ndi_devi_enter of "/". To avoid deadlock, md's prop_op
* implementation must avoid taking a lock that is held per above md
* ioctl description: i.e. mdprop_op(9E) can't call md_unit_readerlock()
* without risking deadlock.
*
* To service "size" requests without risking deadlock, we maintain a
* "mnum->nblocks" sizemap (protected by a short-term global mutex).
*/
static kmutex_t md_nblocks_mutex;
static mod_hash_t *md_nblocksmap; /* mnum -> nblocks */
int md_nblocksmap_size = 512;
/*
* Maintain "mnum->nblocks" sizemap for mdprop_op use:
*
* Create: any code that establishes a unit's un_total_blocks needs the
* following type of call to establish nblocks for mdprop_op():
* md_nblocks_set(mnum, un->c.un_total_blocks);"
* NOTE: locate via cscope md_create_minor_node/md_create_unit_incore
* ...or "MD_UNIT..*="
*
* Change: any code that changes a unit's un_total_blocks needs the
* following type of call to sync nblocks for mdprop_op():
* md_nblocks_set(mnum, un->c.un_total_blocks);"
* NOTE: locate via cscope for "un_total_blocks[ \t]*="
*
* Destroy: any code that deletes a unit needs the following type of call
* to sync nblocks for mdprop_op():
* md_nblocks_set(mnum, -1ULL);
* NOTE: locate via cscope md_remove_minor_node/md_destroy_unit_incore
* ...or "MD_UNIT..*="
*/
void
md_nblocks_set(minor_t mnum, uint64_t nblocks)
{
mutex_enter(&md_nblocks_mutex);
if (nblocks == -1ULL)
(void) mod_hash_destroy(md_nblocksmap,
(mod_hash_key_t)(intptr_t)mnum);
else
(void) mod_hash_replace(md_nblocksmap,
(mod_hash_key_t)(intptr_t)mnum,
(mod_hash_val_t)(intptr_t)nblocks);
mutex_exit(&md_nblocks_mutex);
}
/* get the size of a mnum from "mnum->nblocks" sizemap */
uint64_t
md_nblocks_get(minor_t mnum)
{
mod_hash_val_t hv;
mutex_enter(&md_nblocks_mutex);
if (mod_hash_find(md_nblocksmap,
(mod_hash_key_t)(intptr_t)mnum, &hv) == 0) {
mutex_exit(&md_nblocks_mutex);
return ((uint64_t)(intptr_t)hv);
}
mutex_exit(&md_nblocks_mutex);
return (0);
}
/* allocate/free dynamic space associated with driver globals */
void
md_global_alloc_free(int alloc)
{
set_t s;
if (alloc) {
/* initialize driver global locks */
cv_init(&md_cv, NULL, CV_DEFAULT, NULL);
mutex_init(&md_mx, NULL, MUTEX_DEFAULT, NULL);
rw_init(&md_unit_array_rw.lock, NULL, RW_DEFAULT, NULL);
rw_init(&nm_lock.lock, NULL, RW_DEFAULT, NULL);
rw_init(&ni_rwlp.lock, NULL, RW_DRIVER, NULL);
rw_init(&hsp_rwlp.lock, NULL, RW_DRIVER, NULL);
mutex_init(&md_cpr_resync.md_resync_mutex, NULL,
MUTEX_DEFAULT, NULL);
mutex_init(&md_nblocks_mutex, NULL, MUTEX_DEFAULT, NULL);
/* initialize per set driver global locks */
for (s = 0; s < MD_MAXSETS; s++) {
/* initialize per set driver globals locks */
mutex_init(&md_set[s].s_dbmx,
NULL, MUTEX_DEFAULT, NULL);
mutex_init(&md_set_io[s].md_io_mx,
NULL, MUTEX_DEFAULT, NULL);
cv_init(&md_set_io[s].md_io_cv,
NULL, CV_DEFAULT, NULL);
}
} else {
/* destroy per set driver global locks */
for (s = 0; s < MD_MAXSETS; s++) {
cv_destroy(&md_set_io[s].md_io_cv);
mutex_destroy(&md_set_io[s].md_io_mx);
mutex_destroy(&md_set[s].s_dbmx);
}
/* destroy driver global locks */
mutex_destroy(&md_nblocks_mutex);
mutex_destroy(&md_cpr_resync.md_resync_mutex);
rw_destroy(&hsp_rwlp.lock);
rw_destroy(&ni_rwlp.lock);
rw_destroy(&nm_lock.lock);
rw_destroy(&md_unit_array_rw.lock);
mutex_destroy(&md_mx);
cv_destroy(&md_cv);
}
}
int
_init(void)
{
set_t s;
int err;
MD_SET_IN(IN_INIT);
/* allocate dynamic space associated with driver globals */
md_global_alloc_free(1);
/* initialize driver globals */
md_major = ddi_name_to_major("md");
md_hz = drv_usectohz(NUM_USEC_IN_SEC);
/* initialize tunable globals */
if (md_maxphys == 0) /* maximum io size in bytes */
md_maxphys = maxphys;
if (md_maxbcount == 0) /* maximum physio size in bytes */
md_maxbcount = MD_MAXBCOUNT;
/* initialize per set driver globals */
for (s = 0; s < MD_MAXSETS; s++)
md_set_io[s].io_state = MD_SET_ACTIVE;
/*
* NOTE: the framework does not currently guarantee exclusion
* between _init and attach after calling mod_install.
*/
MD_CLR_IN(IN_INIT);
if ((err = mod_install(&modlinkage))) {
MD_SET_IN(IN_INIT);
md_global_alloc_free(0); /* free dynamic space */
MD_CLR_IN(IN_INIT);
}
return (err);
}
int
_fini(void)
{
int err;
/*
* NOTE: the framework currently does not guarantee exclusion
* with attach until after mod_remove returns 0.
*/
if ((err = mod_remove(&modlinkage)))
return (err);
MD_SET_IN(IN_FINI);
md_global_alloc_free(0); /* free dynamic space */
MD_CLR_IN(IN_FINI);
return (err);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/* ARGSUSED */
static int
mdattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int len;
unit_t i;
size_t sz;
char ver[VERSION_LENGTH];
char **maj_str_array;
char *str, *str2;
MD_SET_IN(IN_ATTACH);
md_in_upgrade = 0;
md_keep_repl_state = 0;
md_devid_destroy = 0;
if (cmd != DDI_ATTACH) {
MD_CLR_IN(IN_ATTACH);
return (DDI_FAILURE);
}
if (md_devinfo != NULL) {
MD_CLR_IN(IN_ATTACH);
return (DDI_FAILURE);
}
mddb_init();
if (md_start_daemons(TRUE)) {
MD_CLR_IN(IN_ATTACH);
mddb_unload(); /* undo mddb_init() allocations */
return (DDI_FAILURE);
}
/* clear the halted state */
md_clr_status(MD_GBL_HALTED);
/* see if the diagnostic switch is on */
if (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "md_init_debug", 0))
md_init_debug++;
/* see if the failfast disable switch is on */
if (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "md_ff_disable", 0))
md_ff_disable++;
/* try and get the md_nmedh property */
md_nmedh = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "md_nmedh", MED_DEF_HOSTS);
if ((md_nmedh <= 0) || (md_nmedh > MED_MAX_HOSTS))
md_nmedh = MED_DEF_HOSTS;
/* try and get the md_med_trans_lst property */
len = 0;
if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN,
0, "md_med_trans_lst", NULL, &len) != DDI_PROP_SUCCESS ||
len == 0) {
md_med_trans_lst = md_strdup("tcp");
} else {
md_med_trans_lst = kmem_zalloc((size_t)len, KM_SLEEP);
if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
0, "md_med_trans_lst", md_med_trans_lst, &len) !=
DDI_PROP_SUCCESS) {
kmem_free(md_med_trans_lst, (size_t)len);
md_med_trans_lst = md_strdup("tcp");
}
}
/*
* Must initialize the internal data structures before the
* any possible calls to 'goto attach_failure' as _fini
* routine references them.
*/
med_init();
md_ops = (md_ops_t **)kmem_zalloc(
sizeof (md_ops_t *) * MD_NOPS, KM_SLEEP);
md_mods = (ddi_modhandle_t *)kmem_zalloc(
sizeof (ddi_modhandle_t) * MD_NOPS, KM_SLEEP);
/* try and get the md_xlate property */
/* Should we only do this if upgrade? */
len = sizeof (char) * 5;
if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
0, "md_xlate_ver", ver, &len) == DDI_PROP_SUCCESS) {
if (strcmp(ver, VERSION) == 0) {
len = 0;
if (ddi_prop_op(DDI_DEV_T_ANY, dip,
PROP_LEN_AND_VAL_ALLOC, 0, "md_xlate",
(caddr_t)&md_tuple_table, &len) !=
DDI_PROP_SUCCESS) {
if (md_init_debug)
cmn_err(CE_WARN,
"md_xlate ddi_prop_op failed");
goto attach_failure;
} else {
md_tuple_length =
len/(2 * ((int)sizeof (dev32_t)));
md_in_upgrade = 1;
}
/* Get target's name to major table */
if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY,
dip, DDI_PROP_DONTPASS,
"md_targ_nm_table", &maj_str_array,
&md_majortab_len) != DDI_PROP_SUCCESS) {
md_majortab_len = 0;
if (md_init_debug)
cmn_err(CE_WARN, "md_targ_nm_table "
"ddi_prop_lookup_string_array "
"failed");
goto attach_failure;
}
md_major_tuple_table =
(struct md_xlate_major_table *)
kmem_zalloc(md_majortab_len *
sizeof (struct md_xlate_major_table), KM_SLEEP);
for (i = 0; i < md_majortab_len; i++) {
/* Getting major name */
str = strchr(maj_str_array[i], ' ');
if (str == NULL)
continue;
*str = '\0';
md_major_tuple_table[i].drv_name =
md_strdup(maj_str_array[i]);
/* Simplified atoi to get major number */
str2 = str + 1;
md_major_tuple_table[i].targ_maj = 0;
while ((*str2 >= '0') && (*str2 <= '9')) {
md_major_tuple_table[i].targ_maj *= 10;
md_major_tuple_table[i].targ_maj +=
*str2++ - '0';
}
*str = ' ';
}
ddi_prop_free((void *)maj_str_array);
} else {
if (md_init_debug)
cmn_err(CE_WARN, "md_xlate_ver is incorrect");
goto attach_failure;
}
}
/*
* Check for properties:
* md_keep_repl_state and md_devid_destroy
* and set globals if these exist.
*/
md_keep_repl_state = ddi_getprop(DDI_DEV_T_ANY, dip,
0, "md_keep_repl_state", 0);
md_devid_destroy = ddi_getprop(DDI_DEV_T_ANY, dip,
0, "md_devid_destroy", 0);
if (MD_UPGRADE)
md_major_targ = md_targ_name_to_major("md");
else
md_major_targ = 0;
/* allocate admin device node */
if (ddi_create_priv_minor_node(dip, "admin", S_IFCHR,
MD_ADM_MINOR, DDI_PSEUDO, 0, NULL, PRIV_SYS_CONFIG, 0640))
goto attach_failure;
if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
DDI_KERNEL_IOCTL, NULL, 0) != DDI_SUCCESS)
goto attach_failure;
if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
"ddi-abrwrite-supported", 1) != DDI_SUCCESS)
goto attach_failure;
/* these could have been cleared by a detach */
md_nunits = MD_MAXUNITS;
md_nsets = MD_MAXSETS;
sz = sizeof (void *) * MD_MAXUNITS;
if (md_set[0].s_un == NULL)
md_set[0].s_un = kmem_zalloc(sz, KM_SLEEP);
if (md_set[0].s_ui == NULL)
md_set[0].s_ui = kmem_zalloc(sz, KM_SLEEP);
md_devinfo = dip;
/*
* Only allocate device node for root mirror metadevice.
* Don't pre-allocate unnecessary device nodes (thus slowing down a
* boot when we attach).
* We can't read the mddbs in attach. The mddbs will be read
* by metainit during the boot process when it is doing the
* auto-take processing and any other minor nodes will be
* allocated at that point.
*
* There are two scenarios to be aware of here:
* 1) when we are booting from a mirrored root we need the root
* metadevice to exist very early (during vfs_mountroot processing)
* 2) we need all of the nodes to be created so that any mnttab entries
* will succeed (handled by metainit reading the mddb during boot).
*/
if (strncmp(SVM_PSEUDO_STR, svm_bootpath, sizeof (SVM_PSEUDO_STR) - 1)
== 0) {
char *p;
int mnum = 0;
/*
* The svm_bootpath string looks something like
* /pseudo/md@0:0,150,blk where 150 is the minor number
* in this example so we need to set the pointer p onto
* the first digit of the minor number and convert it
* from ascii.
*/
for (p = svm_bootpath + sizeof (SVM_PSEUDO_STR) + 1;
*p >= '0' && *p <= '9'; p++) {
mnum *= 10;
mnum += *p - '0';
}
if (md_create_minor_node(0, mnum)) {
kmem_free(md_set[0].s_un, sz);
kmem_free(md_set[0].s_ui, sz);
goto attach_failure;
}
}
/* create the hash to store the meta device sizes */
md_nblocksmap = mod_hash_create_idhash("md_nblocksmap",
md_nblocksmap_size, mod_hash_null_valdtor);
MD_CLR_IN(IN_ATTACH);
return (DDI_SUCCESS);
attach_failure:
/*
* Use our own detach routine to toss any stuff we allocated above.
* NOTE: detach will call md_halt to free the mddb_init allocations.
*/
MD_CLR_IN(IN_ATTACH);
if (mddetach(dip, DDI_DETACH) != DDI_SUCCESS)
cmn_err(CE_WARN, "detach from attach failed");
return (DDI_FAILURE);
}
/* ARGSUSED */
static int
mddetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
extern int check_active_locators();
set_t s;
size_t sz;
int len;
MD_SET_IN(IN_DETACH);
/* check command */
if (cmd != DDI_DETACH) {
MD_CLR_IN(IN_DETACH);
return (DDI_FAILURE);
}
/*
* if we have not already halted yet we have no active config
* then automatically initiate a halt so we can detach.
*/
if (!(md_get_status() & MD_GBL_HALTED)) {
if (check_active_locators() == 0) {
/*
* NOTE: a successful md_halt will have done the
* mddb_unload to free allocations done in mddb_init
*/
if (md_halt(MD_NO_GBL_LOCKS_HELD)) {
cmn_err(CE_NOTE, "md:detach: "
"Could not halt Solaris Volume Manager");
MD_CLR_IN(IN_DETACH);
return (DDI_FAILURE);
}
}
/* fail detach if we have not halted */
if (!(md_get_status() & MD_GBL_HALTED)) {
MD_CLR_IN(IN_DETACH);
return (DDI_FAILURE);
}
}
/* must be in halted state, this will be cleared on next attach */
ASSERT(md_get_status() & MD_GBL_HALTED);
/* cleanup attach allocations and initializations */
md_major_targ = 0;
sz = sizeof (void *) * md_nunits;
for (s = 0; s < md_nsets; s++) {
if (md_set[s].s_un != NULL) {
kmem_free(md_set[s].s_un, sz);
md_set[s].s_un = NULL;
}
if (md_set[s].s_ui != NULL) {
kmem_free(md_set[s].s_ui, sz);
md_set[s].s_ui = NULL;
}
}
md_nunits = 0;
md_nsets = 0;
md_nmedh = 0;
if (non_ff_drivers != NULL) {
int i;
for (i = 0; non_ff_drivers[i] != NULL; i++)
kmem_free(non_ff_drivers[i],
strlen(non_ff_drivers[i]) + 1);
/* free i+1 entries because there is a null entry at list end */
kmem_free(non_ff_drivers, (i + 1) * sizeof (char *));
non_ff_drivers = NULL;
}
if (md_med_trans_lst != NULL) {
kmem_free(md_med_trans_lst, strlen(md_med_trans_lst) + 1);
md_med_trans_lst = NULL;
}
if (md_mods != NULL) {
kmem_free(md_mods, sizeof (ddi_modhandle_t) * MD_NOPS);
md_mods = NULL;
}
if (md_ops != NULL) {
kmem_free(md_ops, sizeof (md_ops_t *) * MD_NOPS);
md_ops = NULL;
}
if (MD_UPGRADE) {
len = md_tuple_length * (2 * ((int)sizeof (dev32_t)));
md_in_upgrade = 0;
md_xlate_free(len);
md_majortab_free();
}
/*
* Undo what we did in mdattach, freeing resources
* and removing things we installed. The system
* framework guarantees we are not active with this devinfo
* node in any other entry points at this time.
*/
ddi_prop_remove_all(dip);
ddi_remove_minor_node(dip, NULL);
med_fini();
mod_hash_destroy_idhash(md_nblocksmap);
md_devinfo = NULL;
MD_CLR_IN(IN_DETACH);
return (DDI_SUCCESS);
}
/*
* Given the device number return the devinfo pointer
* given to md via md_attach
*/
/*ARGSUSED*/
static int
mdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
int error = DDI_FAILURE;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
if (md_devinfo) {
*result = (void *)md_devinfo;
error = DDI_SUCCESS;
}
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)0;
error = DDI_SUCCESS;
break;
}
return (error);
}
/*
* property operation routine. return the number of blocks for the partition
* in question or forward the request to the property facilities.
*/
static int
mdprop_op(
dev_t dev, /* device number associated with device */
dev_info_t *dip, /* device info struct for this device */
ddi_prop_op_t prop_op, /* property operator */
int mod_flags, /* property flags */
char *name, /* name of property */
caddr_t valuep, /* where to put property value */
int *lengthp) /* put length of property here */
{
return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
name, valuep, lengthp, md_nblocks_get(getminor(dev))));
}
static void
snarf_user_data(set_t setno)
{
mddb_recid_t recid;
mddb_recstatus_t status;
recid = mddb_makerecid(setno, 0);
while ((recid = mddb_getnextrec(recid, MDDB_USER, 0)) > 0) {
if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
continue;
status = mddb_getrecstatus(recid);
if (status == MDDB_STALE)
continue;
if (status == MDDB_NODATA) {
mddb_setrecprivate(recid, MD_PRV_PENDDEL);
continue;
}
ASSERT(status == MDDB_OK);
mddb_setrecprivate(recid, MD_PRV_GOTIT);
}
}
static void
md_print_block_usage(mddb_set_t *s, uint_t blks)
{
uint_t ib;
int li;
mddb_mb_ic_t *mbip;
uint_t max_blk_needed;
mddb_lb_t *lbp;
mddb_sidelocator_t *slp;
int drv_index;
md_splitname sn;
char *name;
char *suffix;
size_t prefixlen;
size_t suffixlen;
int alloc_sz;
max_blk_needed = s->s_totalblkcnt - s->s_freeblkcnt + blks;
cmn_err(CE_WARN, "Blocks in Metadevice State Database: %d\n"
" Additional Blocks Needed: %d\n\n"
" Increase size of following replicas for\n"
" device relocatability by deleting listed\n"
" replica and re-adding replica with\n"
" increased size (see metadb(1M)):\n"
" Replica Increase By",
s->s_totalblkcnt, (blks - s->s_freeblkcnt));
lbp = s->s_lbp;
for (li = 0; li < lbp->lb_loccnt; li++) {
if (lbp->lb_locators[li].l_flags & MDDB_F_DELETED)
continue;
ib = 0;
for (mbip = s->s_mbiarray[li]; mbip != NULL;
mbip = mbip->mbi_next) {
ib += (uint_t)mbip->mbi_mddb_mb.mb_blkcnt;
}
if (ib == 0)
continue;
if (ib < max_blk_needed) {
slp = &lbp->lb_sidelocators[s->s_sideno][li];
drv_index = slp->l_drvnm_index;
mddb_locatorblock2splitname(s->s_lnp, li, s->s_sideno,
&sn);
prefixlen = SPN_PREFIX(&sn).pre_len;
suffixlen = SPN_SUFFIX(&sn).suf_len;
alloc_sz = (int)(prefixlen + suffixlen + 2);
name = (char *)kmem_alloc(alloc_sz, KM_SLEEP);
(void) strncpy(name, SPN_PREFIX(&sn).pre_data,
prefixlen);
name[prefixlen] = '/';
suffix = name + (prefixlen + 1);
(void) strncpy(suffix, SPN_SUFFIX(&sn).suf_data,
suffixlen);
name[prefixlen + suffixlen + 1] = '\0';
cmn_err(CE_WARN,
" %s (%s:%d:%d) %d blocks",
name, lbp->lb_drvnm[drv_index].dn_data,
slp->l_mnum, lbp->lb_locators[li].l_blkno,
(max_blk_needed - ib));
kmem_free(name, alloc_sz);
}
}
}
/*
* md_create_minor_node:
* Create the minor device for the given set and un_self_id.
*
* Input:
* setno - set number
* mnum - selfID of unit
*
* Output:
* None.
*
* Returns 0 for success, 1 for failure.
*
* Side-effects:
* None.
*/
int
md_create_minor_node(set_t setno, minor_t mnum)
{
char name[20];
/* Check for valid arguments */
if (setno >= MD_MAXSETS || MD_MIN2UNIT(mnum) >= MD_MAXUNITS)
return (1);
(void) snprintf(name, 20, "%u,%u,blk",
(unsigned)setno, (unsigned)MD_MIN2UNIT(mnum));
if (ddi_create_minor_node(md_devinfo, name, S_IFBLK,
MD_MKMIN(setno, mnum), DDI_PSEUDO, 0))
return (1);
(void) snprintf(name, 20, "%u,%u,raw",
(unsigned)setno, (unsigned)MD_MIN2UNIT(mnum));
if (ddi_create_minor_node(md_devinfo, name, S_IFCHR,
MD_MKMIN(setno, mnum), DDI_PSEUDO, 0))
return (1);
return (0);
}
/*
* For a given key check if it is an orphaned record.
* The following conditions are used to determine an orphan.
* 1. The device associated with that key is not a metadevice.
* 2. If DEVID_STYLE then the physical device does not have a device Id
* associated with it.
*
* If a key does not have an entry in the devid namespace it could be
* a device that does not support device ids. Hence the record is not
* deleted.
*/
static int
md_verify_orphaned_record(set_t setno, mdkey_t key)
{
md_dev64_t odev; /* orphaned dev */
mddb_set_t *s;
side_t side = 0;
struct nm_next_hdr *did_nh = NULL;
s = (mddb_set_t *)md_set[setno].s_db;
if ((did_nh = get_first_record(setno, 1, (NM_DEVID | NM_NOTSHARED)))
== NULL)
return (0);
/*
* If devid style is set then get the dev_t using MD_NOTRUST_DEVT
*/
if (s->s_lbp->lb_flags & MDDB_DEVID_STYLE) {
odev = md_getdevnum(setno, side, key, MD_NOTRUST_DEVT);
if ((odev == NODEV64) || (md_getmajor(odev) == md_major))
return (0);
if (lookup_entry(did_nh, setno, side, key, odev, NM_DEVID) ==
NULL)
return (1);
}
return (0);
}
int
md_snarf_db_set(set_t setno, md_error_t *ep)
{
int err = 0;
int i;
mddb_recid_t recid;
mddb_type_t drvrid;
mddb_recstatus_t status;
md_ops_t *ops;
uint_t privat;
mddb_set_t *s;
uint_t cvt_blks;
struct nm_next_hdr *nh;
mdkey_t key = MD_KEYWILD;
side_t side = 0;
int size;
int devid_flag;
int retval;
uint_t un;
int un_next_set = 0;
md_haltsnarf_enter(setno);
mutex_enter(&md_mx);
if (md_set[setno].s_status & MD_SET_SNARFED) {
mutex_exit(&md_mx);
md_haltsnarf_exit(setno);
return (0);
}
mutex_exit(&md_mx);
if (! (md_get_status() & MD_GBL_DAEMONS_LIVE)) {
if (md_start_daemons(TRUE)) {
if (ep != NULL)
(void) mdsyserror(ep, ENXIO);
err = -1;
goto out;
}
}
/*
* Load the devid name space if it exists
*/
(void) md_load_namespace(setno, NULL, NM_DEVID);
if (!md_load_namespace(setno, ep, 0L)) {
/*
* Unload the devid namespace
*/
(void) md_unload_namespace(setno, NM_DEVID);
err = -1;
goto out;
}
/*
* If replica is in non-devid state, convert if:
* - not in probe during upgrade (md_keep_repl_state = 0)
* - enough space available in replica
* - local set
* - not a multi-node diskset
* - clustering is not present (for non-local set)
*/
s = (mddb_set_t *)md_set[setno].s_db;
devid_flag = 0;
if (!(s->s_lbp->lb_flags & MDDB_DEVID_STYLE) && !md_keep_repl_state)
devid_flag = 1;
if (cluster_bootflags & CLUSTER_CONFIGURED)
if (setno != MD_LOCAL_SET)
devid_flag = 0;
if (MD_MNSET_SETNO(setno))
devid_flag = 0;
if ((md_devid_destroy == 1) && (md_keep_repl_state == 1))
devid_flag = 0;
/*
* if we weren't devid style before and md_keep_repl_state=1
* we need to stay non-devid
*/
if ((md_keep_repl_state == 1) &&
((s->s_lbp->lb_flags & MDDB_DEVID_STYLE) == 0))
devid_flag = 0;
if (devid_flag) {
/*
* Determine number of free blocks needed to convert
* entire replica to device id format - locator blocks
* and namespace.
*/
cvt_blks = 0;
if (mddb_lb_did_convert(s, 0, &cvt_blks) != 0) {
if (ep != NULL)
(void) mdsyserror(ep, EIO);
err = -1;
goto out;
}
cvt_blks += md_nm_did_chkspace(setno);
/* add MDDB_DEVID_CONV_PERC% */
if ((md_conv_perc > 0) && (md_conv_perc <= 100)) {
cvt_blks = cvt_blks * (100 + md_conv_perc) / 100;
}
if (cvt_blks <= s->s_freeblkcnt) {
if (mddb_lb_did_convert(s, 1, &cvt_blks) != 0) {
if (ep != NULL)
(void) mdsyserror(ep, EIO);
err = -1;
goto out;
}
} else {
/*
* Print message that replica can't be converted for
* lack of space. No failure - just continue to
* run without device ids.
*/
cmn_err(CE_WARN,
"Unable to add Solaris Volume Manager device "
"relocation data.\n"
" To use device relocation feature:\n"
" - Increase size of listed replicas\n"
" - Reboot");
md_print_block_usage(s, cvt_blks);
cmn_err(CE_WARN,
"Loading set without device relocation data.\n"
" Solaris Volume Manager disk movement "
"not tracked in local set.");
}
}
/*
* go through and load any modules referenced in
* data base
*/
recid = mddb_makerecid(setno, 0);
while ((recid = mddb_getnextrec(recid, MDDB_ALL, 0)) > 0) {
status = mddb_getrecstatus(recid);
if (status == MDDB_STALE) {
if (! (md_get_setstatus(setno) & MD_SET_STALE)) {
md_set_setstatus(setno, MD_SET_STALE);
cmn_err(CE_WARN,
"md: state database is stale");
}
} else if (status == MDDB_NODATA) {
mddb_setrecprivate(recid, MD_PRV_PENDDEL);
continue;
}
drvrid = mddb_getrectype1(recid);
if (drvrid < MDDB_FIRST_MODID)
continue;
if (md_loadsubmod(setno, md_getshared_name(setno, drvrid),
drvrid) < 0) {
cmn_err(CE_NOTE, "md: could not load misc/%s",
md_getshared_name(setno, drvrid));
}
}
if (recid < 0)
goto out;
snarf_user_data(setno);
/*
* Initialize the md_nm_snarfed array
* this array is indexed by the key and
* is set by md_getdevnum during the snarf time
*/
if ((nh = get_first_record(setno, 0, NM_NOTSHARED)) != NULL) {
size = (int)((((struct nm_rec_hdr *)nh->nmn_record)->
r_next_key) * (sizeof (int)));
md_nm_snarfed = (int *)kmem_zalloc(size, KM_SLEEP);
}
/*
* go through and snarf until nothing gets added
*/
do {
i = 0;
for (ops = md_opslist; ops != NULL; ops = ops->md_next) {
if (ops->md_snarf != NULL) {
retval = ops->md_snarf(MD_SNARF_DOIT, setno);
if (retval == -1) {
err = -1;
/* Don't know the failed unit */
(void) mdmderror(ep, MDE_RR_ALLOC_ERROR,
0);
(void) md_halt_set(setno, MD_HALT_ALL);
(void) mddb_unload_set(setno);
md_haltsnarf_exit(setno);
return (err);
} else {
i += retval;
}
}
}
} while (i);
/*
* Set the first available slot and availability
*/
md_set[setno].s_un_avail = 0;
for (un = 0; un < MD_MAXUNITS; un++) {
if (md_set[setno].s_un[un] != NULL) {
continue;
} else {
if (!un_next_set) {
md_set[setno].s_un_next = un;
un_next_set = 1;
}
md_set[setno].s_un_avail++;
}
}
md_set_setstatus(setno, MD_SET_SNARFED);
recid = mddb_makerecid(setno, 0);
while ((recid = mddb_getnextrec(recid, MDDB_ALL, 0)) > 0) {
privat = mddb_getrecprivate(recid);
if (privat & MD_PRV_COMMIT) {
if (mddb_commitrec(recid)) {
if (!(md_get_setstatus(setno) & MD_SET_STALE)) {
md_set_setstatus(setno, MD_SET_STALE);
cmn_err(CE_WARN,
"md: state database is stale");
}
}
mddb_setrecprivate(recid, MD_PRV_GOTIT);
}
}
/* Deletes must happen after all the commits */
recid = mddb_makerecid(setno, 0);
while ((recid = mddb_getnextrec(recid, MDDB_ALL, 0)) > 0) {
privat = mddb_getrecprivate(recid);
if (privat & MD_PRV_DELETE) {
if (mddb_deleterec(recid)) {
if (!(md_get_setstatus(setno) & MD_SET_STALE)) {
md_set_setstatus(setno, MD_SET_STALE);
cmn_err(CE_WARN,
"md: state database is stale");
}
mddb_setrecprivate(recid, MD_PRV_GOTIT);
}
recid = mddb_makerecid(setno, 0);
}
}
/*
* go through and clean up records until nothing gets cleaned up.
*/
do {
i = 0;
for (ops = md_opslist; ops != NULL; ops = ops->md_next)
if (ops->md_snarf != NULL)
i += ops->md_snarf(MD_SNARF_CLEANUP, setno);
} while (i);
if (md_nm_snarfed != NULL &&
!(md_get_setstatus(setno) & MD_SET_STALE)) {
/*
* go thru and cleanup the namespace and the device id
* name space
*/
for (key = 1;
key < ((struct nm_rec_hdr *)nh->nmn_record)->r_next_key;
key++) {
/*
* Is the entry an 'orphan'?
*/
if (lookup_entry(nh, setno, side, key, NODEV64, 0L) !=
NULL) {
/*
* If the value is not set then apparently
* it is not part of the current configuration,
* remove it this can happen when system panic
* between the primary name space update and
* the device id name space update
*/
if (md_nm_snarfed[key] == 0) {
if (md_verify_orphaned_record(setno,
key) == 1)
(void) remove_entry(nh,
side, key, 0L);
}
}
}
}
if (md_nm_snarfed != NULL) {
/*
* Done and free the memory
*/
kmem_free(md_nm_snarfed, size);
md_nm_snarfed = NULL;
}
if (s->s_lbp->lb_flags & MDDB_DEVID_STYLE &&
!(md_get_setstatus(setno) & MD_SET_STALE)) {
/*
* if the destroy flag has been set and
* the MD_SET_DIDCLUP bit is not set in
* the set's status field, cleanup the
* entire device id namespace
*/
if (md_devid_destroy &&
!(md_get_setstatus(setno) & MD_SET_DIDCLUP)) {
(void) md_devid_cleanup(setno, 1);
md_set_setstatus(setno, MD_SET_DIDCLUP);
} else
(void) md_devid_cleanup(setno, 0);
}
/*
* clear single threading on snarf, return success or error
*/
out:
md_haltsnarf_exit(setno);
return (err);
}
void
get_minfo(struct dk_minfo *info, minor_t mnum)
{
md_unit_t *un;
mdi_unit_t *ui;
info->dki_capacity = 0;
info->dki_lbsize = 0;
info->dki_media_type = 0;
if ((ui = MDI_UNIT(mnum)) == NULL) {
return;
}
un = (md_unit_t *)md_unit_readerlock(ui);
info->dki_capacity = un->c.un_total_blocks;
md_unit_readerexit(ui);
info->dki_lbsize = DEV_BSIZE;
info->dki_media_type = DK_UNKNOWN;
}
void
get_info(struct dk_cinfo *info, minor_t mnum)
{
/*
* Controller Information
*/
info->dki_ctype = DKC_MD;
info->dki_cnum = ddi_get_instance(ddi_get_parent(md_devinfo));
(void) strcpy(info->dki_cname,
ddi_get_name(ddi_get_parent(md_devinfo)));
/*
* Unit Information
*/
info->dki_unit = mnum;
info->dki_slave = 0;
(void) strcpy(info->dki_dname, ddi_driver_name(md_devinfo));
info->dki_flags = 0;
info->dki_partition = 0;
info->dki_maxtransfer = (ushort_t)(md_maxphys / DEV_BSIZE);
/*
* We can't get from here to there yet
*/
info->dki_addr = 0;
info->dki_space = 0;
info->dki_prio = 0;
info->dki_vec = 0;
}
/*
* open admin device
*/
static int
mdadminopen(
int flag,
int otyp)
{
int err = 0;
/* single thread */
mutex_enter(&md_mx);
/* check type and flags */
if ((otyp != OTYP_CHR) && (otyp != OTYP_LYR)) {
err = EINVAL;
goto out;
}
if (((flag & FEXCL) && (md_status & MD_GBL_OPEN)) ||
(md_status & MD_GBL_EXCL)) {
err = EBUSY;
goto out;
}
/* count and flag open */
md_ocnt[otyp]++;
md_status |= MD_GBL_OPEN;
if (flag & FEXCL)
md_status |= MD_GBL_EXCL;
/* unlock return success */
out:
mutex_exit(&md_mx);
return (err);
}
/*
* open entry point
*/
static int
mdopen(
dev_t *dev,
int flag,
int otyp,
cred_t *cred_p)
{
minor_t mnum = getminor(*dev);
unit_t unit = MD_MIN2UNIT(mnum);
set_t setno = MD_MIN2SET(mnum);
mdi_unit_t *ui = NULL;
int err = 0;
md_parent_t parent;
/* dispatch admin device opens */
if (mnum == MD_ADM_MINOR)
return (mdadminopen(flag, otyp));
/* lock, check status */
rw_enter(&md_unit_array_rw.lock, RW_READER);
tryagain:
if (md_get_status() & MD_GBL_HALTED) {
err = ENODEV;
goto out;
}
/* check minor */
if ((setno >= md_nsets) || (unit >= md_nunits)) {
err = ENXIO;
goto out;
}
/* make sure we're snarfed */
if ((md_get_setstatus(MD_LOCAL_SET) & MD_SET_SNARFED) == 0) {
if (md_snarf_db_set(MD_LOCAL_SET, NULL) != 0) {
err = ENODEV;
goto out;
}
}
if ((md_get_setstatus(setno) & MD_SET_SNARFED) == 0) {
err = ENODEV;
goto out;
}
/* check unit */
if ((ui = MDI_UNIT(mnum)) == NULL) {
err = ENXIO;
goto out;
}
/*
* The softpart open routine may do an I/O during the open, in
* which case the open routine will set the OPENINPROGRESS flag
* and drop all locks during the I/O. If this thread sees
* the OPENINPROGRESS flag set, if should wait until the flag
* is reset before calling the driver's open routine. It must
* also revalidate the world after it grabs the unit_array lock
* since the set may have been released or the metadevice cleared
* during the sleep.
*/
if (MD_MNSET_SETNO(setno)) {
mutex_enter(&ui->ui_mx);
if (ui->ui_lock & MD_UL_OPENINPROGRESS) {
rw_exit(&md_unit_array_rw.lock);
cv_wait(&ui->ui_cv, &ui->ui_mx);
rw_enter(&md_unit_array_rw.lock, RW_READER);
mutex_exit(&ui->ui_mx);
goto tryagain;
}
mutex_exit(&ui->ui_mx);
}
/* Test if device is openable */
if ((ui->ui_tstate & MD_NOTOPENABLE) != 0) {
err = ENXIO;
goto out;
}
/* don't allow opens w/WRITE flag if stale */
if ((flag & FWRITE) && (md_get_setstatus(setno) & MD_SET_STALE)) {
err = EROFS;
goto out;
}
/* don't allow writes to subdevices */
parent = md_get_parent(md_expldev(*dev));
if ((flag & FWRITE) && MD_HAS_PARENT(parent)) {
err = EROFS;
goto out;
}
/* open underlying driver */
if (md_ops[ui->ui_opsindex]->md_open != NULL) {
if ((err = (*md_ops[ui->ui_opsindex]->md_open)
(dev, flag, otyp, cred_p, 0)) != 0)
goto out;
}
/* or do it ourselves */
else {
/* single thread */
(void) md_unit_openclose_enter(ui);
err = md_unit_incopen(mnum, flag, otyp);
md_unit_openclose_exit(ui);
if (err != 0)
goto out;
}
/* unlock, return status */
out:
rw_exit(&md_unit_array_rw.lock);
return (err);
}
/*
* close admin device
*/
static int
mdadminclose(
int otyp)
{
int i;
int err = 0;
/* single thread */
mutex_enter(&md_mx);
/* check type and flags */
if ((otyp < 0) || (otyp >= OTYPCNT)) {
err = EINVAL;
goto out;
} else if (md_ocnt[otyp] == 0) {
err = ENXIO;
goto out;
}
/* count and flag closed */
if (otyp == OTYP_LYR)
md_ocnt[otyp]--;
else
md_ocnt[otyp] = 0;
md_status &= ~MD_GBL_OPEN;
for (i = 0; (i < OTYPCNT); ++i)
if (md_ocnt[i] != 0)
md_status |= MD_GBL_OPEN;
if (! (md_status & MD_GBL_OPEN))
md_status &= ~MD_GBL_EXCL;
/* unlock return success */
out:
mutex_exit(&md_mx);
return (err);
}
/*
* close entry point
*/
static int
mdclose(
dev_t dev,
int flag,
int otyp,
cred_t *cred_p)
{
minor_t mnum = getminor(dev);
set_t setno = MD_MIN2SET(mnum);
unit_t unit = MD_MIN2UNIT(mnum);
mdi_unit_t *ui = NULL;
int err = 0;
/* dispatch admin device closes */
if (mnum == MD_ADM_MINOR)
return (mdadminclose(otyp));
/* check minor */
if ((setno >= md_nsets) || (unit >= md_nunits) ||
((ui = MDI_UNIT(mnum)) == NULL)) {
err = ENXIO;
goto out;
}
/* close underlying driver */
if (md_ops[ui->ui_opsindex]->md_close != NULL) {
if ((err = (*md_ops[ui->ui_opsindex]->md_close)
(dev, flag, otyp, cred_p, 0)) != 0)
goto out;
}
/* or do it ourselves */
else {
/* single thread */
(void) md_unit_openclose_enter(ui);
err = md_unit_decopen(mnum, otyp);
md_unit_openclose_exit(ui);
if (err != 0)
goto out;
}
/* return success */
out:
return (err);
}
/*
* This routine performs raw read operations. It is called from the
* device switch at normal priority.
*
* The main catch is that the *uio struct which is passed to us may
* specify a read which spans two buffers, which would be contiguous
* on a single partition, but not on a striped partition. This will
* be handled by mdstrategy.
*/
/*ARGSUSED*/
static int
mdread(dev_t dev, struct uio *uio, cred_t *credp)
{
minor_t mnum;
mdi_unit_t *ui;
int error;
if (((mnum = getminor(dev)) == MD_ADM_MINOR) ||
(MD_MIN2SET(mnum) >= md_nsets) ||
(MD_MIN2UNIT(mnum) >= md_nunits) ||
((ui = MDI_UNIT(mnum)) == NULL))
return (ENXIO);
if (md_ops[ui->ui_opsindex]->md_read != NULL)
return ((*md_ops[ui->ui_opsindex]->md_read)
(dev, uio, credp));
if ((error = md_chk_uio(uio)) != 0)
return (error);
return (physio(mdstrategy, NULL, dev, B_READ, md_minphys, uio));
}
/*
* This routine performs async raw read operations. It is called from the
* device switch at normal priority.
*
* The main catch is that the *aio struct which is passed to us may
* specify a read which spans two buffers, which would be contiguous
* on a single partition, but not on a striped partition. This will
* be handled by mdstrategy.
*/
/*ARGSUSED*/
static int
mdaread(dev_t dev, struct aio_req *aio, cred_t *credp)
{
minor_t mnum;
mdi_unit_t *ui;
int error;
if (((mnum = getminor(dev)) == MD_ADM_MINOR) ||
(MD_MIN2SET(mnum) >= md_nsets) ||
(MD_MIN2UNIT(mnum) >= md_nunits) ||
((ui = MDI_UNIT(mnum)) == NULL))
return (ENXIO);
if (md_ops[ui->ui_opsindex]->md_aread != NULL)
return ((*md_ops[ui->ui_opsindex]->md_aread)
(dev, aio, credp));
if ((error = md_chk_uio(aio->aio_uio)) != 0)
return (error);
return (aphysio(mdstrategy, anocancel, dev, B_READ, md_minphys, aio));
}
/*
* This routine performs raw write operations. It is called from the
* device switch at normal priority.
*
* The main catch is that the *uio struct which is passed to us may
* specify a write which spans two buffers, which would be contiguous
* on a single partition, but not on a striped partition. This is
* handled by mdstrategy.
*
*/
/*ARGSUSED*/
static int
mdwrite(dev_t dev, struct uio *uio, cred_t *credp)
{
minor_t mnum;
mdi_unit_t *ui;
int error;
if (((mnum = getminor(dev)) == MD_ADM_MINOR) ||
(MD_MIN2SET(mnum) >= md_nsets) ||
(MD_MIN2UNIT(mnum) >= md_nunits) ||
((ui = MDI_UNIT(mnum)) == NULL))
return (ENXIO);
if (md_ops[ui->ui_opsindex]->md_write != NULL)
return ((*md_ops[ui->ui_opsindex]->md_write)
(dev, uio, credp));
if ((error = md_chk_uio(uio)) != 0)
return (error);
return (physio(mdstrategy, NULL, dev, B_WRITE, md_minphys, uio));
}
/*
* This routine performs async raw write operations. It is called from the
* device switch at normal priority.
*
* The main catch is that the *aio struct which is passed to us may
* specify a write which spans two buffers, which would be contiguous
* on a single partition, but not on a striped partition. This is
* handled by mdstrategy.
*
*/
/*ARGSUSED*/
static int
mdawrite(dev_t dev, struct aio_req *aio, cred_t *credp)
{
minor_t mnum;
mdi_unit_t *ui;
int error;
if (((mnum = getminor(dev)) == MD_ADM_MINOR) ||
(MD_MIN2SET(mnum) >= md_nsets) ||
(MD_MIN2UNIT(mnum) >= md_nunits) ||
((ui = MDI_UNIT(mnum)) == NULL))
return (ENXIO);
if (md_ops[ui->ui_opsindex]->md_awrite != NULL)
return ((*md_ops[ui->ui_opsindex]->md_awrite)
(dev, aio, credp));
if ((error = md_chk_uio(aio->aio_uio)) != 0)
return (error);
return (aphysio(mdstrategy, anocancel, dev, B_WRITE, md_minphys, aio));
}
int
mdstrategy(struct buf *bp)
{
minor_t mnum;
mdi_unit_t *ui;
ASSERT((bp->b_flags & B_DONE) == 0);
if (panicstr)
md_clr_status(MD_GBL_DAEMONS_LIVE);
if (((mnum = getminor(bp->b_edev)) == MD_ADM_MINOR) ||
(MD_MIN2SET(mnum) >= md_nsets) ||
(MD_MIN2UNIT(mnum) >= md_nunits) ||
((ui = MDI_UNIT(mnum)) == NULL)) {
bp->b_flags |= B_ERROR;
bp->b_error = ENXIO;
bp->b_resid = bp->b_bcount;
biodone(bp);
return (0);
}
bp->b_flags &= ~(B_ERROR | B_DONE);
if (md_ops[ui->ui_opsindex]->md_strategy != NULL) {
(*md_ops[ui->ui_opsindex]->md_strategy) (bp, 0, NULL);
} else {
(void) errdone(ui, bp, ENXIO);
}
return (0);
}
/*
* Return true if the ioctl is allowed to be multithreaded.
* All the ioctls with MN are sent only from the message handlers through
* rpc.mdcommd, which (via it's own locking mechanism) takes care that not two
* ioctl for the same metadevice are issued at the same time.
* So we are safe here.
* The other ioctls do not mess with any metadevice structures and therefor
* are harmless too, if called multiple times at the same time.
*/
static boolean_t
is_mt_ioctl(int cmd) {
switch (cmd) {
case MD_IOCGUNIQMSGID:
case MD_IOCGVERSION:
case MD_IOCISOPEN:
case MD_MN_SET_MM_OWNER:
case MD_MN_SET_STATE:
case MD_MN_SUSPEND_WRITES:
case MD_MN_ALLOCATE_HOTSPARE:
case MD_MN_SET_SETFLAGS:
case MD_MN_GET_SETFLAGS:
case MD_MN_MDDB_OPTRECFIX:
case MD_MN_MDDB_PARSE:
case MD_MN_MDDB_BLOCK:
case MD_MN_DB_USERREQ:
case MD_IOC_SPSTATUS:
case MD_MN_COMMD_ERR:
case MD_MN_SET_COMMD_RUNNING:
case MD_MN_RESYNC:
case MD_MN_SETSYNC:
case MD_MN_POKE_HOTSPARES:
case MD_MN_RR_DIRTY:
case MD_MN_RR_CLEAN:
case MD_MN_IOC_SPUPDATEWM:
return (1);
default:
return (0);
}
}
/*
* This routine implements the ioctl calls for the Virtual Disk System.
* It is called from the device switch at normal priority.
*/
/* ARGSUSED */
static int
mdioctl(dev_t dev, int cmd, intptr_t data, int mode, cred_t *cred_p,
int *rval_p)
{
minor_t mnum = getminor(dev);
mdi_unit_t *ui;
IOLOCK lock;
int err;
/*
* For multinode disksets number of ioctls are allowed to be
* multithreaded.
* A fundamental assumption made in this implementation is that
* ioctls either do not interact with other md structures or the
* ioctl to the admin device can only occur if the metadevice
* device is open. i.e. avoid a race between metaclear and the
* progress of a multithreaded ioctl.
*/
if (!is_mt_ioctl(cmd) && md_ioctl_lock_enter() == EINTR) {
return (EINTR);
}
/*
* initialize lock tracker
*/
IOLOCK_INIT(&lock);
/* Flag to indicate that MD_GBL_IOCTL_LOCK is not acquired */
if (is_mt_ioctl(cmd)) {
/* increment the md_mtioctl_cnt */
mutex_enter(&md_mx);
md_mtioctl_cnt++;
mutex_exit(&md_mx);
lock.l_flags |= MD_MT_IOCTL;
}
/*
* this has been added to prevent notification from re-snarfing
* so metaunload will work. It may interfere with other modules
* halt process.
*/
if (md_get_status() & (MD_GBL_HALTED | MD_GBL_DAEMONS_DIE))
return (IOLOCK_RETURN(ENXIO, &lock));
/*
* admin device ioctls
*/
if (mnum == MD_ADM_MINOR) {
err = md_admin_ioctl(md_expldev(dev), cmd, (void *) data,
mode, &lock);
}
/*
* metadevice ioctls
*/
else if ((MD_MIN2SET(mnum) >= md_nsets) ||
(MD_MIN2UNIT(mnum) >= md_nunits) ||
(md_set[MD_MIN2SET(mnum)].s_ui == NULL) ||
((ui = MDI_UNIT(mnum)) == NULL)) {
err = ENXIO;
} else if (md_ops[ui->ui_opsindex]->md_ioctl == NULL) {
err = ENOTTY;
} else {
err = (*md_ops[ui->ui_opsindex]->md_ioctl)
(dev, cmd, (void *) data, mode, &lock);
}
/*
* drop any locks we grabbed
*/
return (IOLOCK_RETURN_IOCTLEND(err, &lock));
}
static int
mddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
{
minor_t mnum;
set_t setno;
mdi_unit_t *ui;
if ((mnum = getminor(dev)) == MD_ADM_MINOR)
return (ENXIO);
setno = MD_MIN2SET(mnum);
if ((setno >= md_nsets) || (MD_MIN2UNIT(mnum) >= md_nunits) ||
((ui = MDI_UNIT(mnum)) == NULL))
return (ENXIO);
if ((md_get_setstatus(setno) & MD_SET_SNARFED) == 0)
return (ENXIO);
if (md_ops[ui->ui_opsindex]->md_dump != NULL)
return ((*md_ops[ui->ui_opsindex]->md_dump)
(dev, addr, blkno, nblk));
return (ENXIO);
}
/*
* Metadevice unit number dispatcher
* When this routine is called it will scan the
* incore unit array and return the avail slot
* hence the unit number to the caller
*
* Return -1 if there is nothing available
*/
unit_t
md_get_nextunit(set_t setno)
{
unit_t un, start;
/*
* If nothing available
*/
if (md_set[setno].s_un_avail == 0) {
return (MD_UNITBAD);
}
mutex_enter(&md_mx);
start = un = md_set[setno].s_un_next;
/* LINTED: E_CONSTANT_CONDITION */
while (1) {
if (md_set[setno].s_un[un] == NULL) {
/*
* Advance the starting index for the next
* md_get_nextunit call
*/
if (un == MD_MAXUNITS - 1) {
md_set[setno].s_un_next = 0;
} else {
md_set[setno].s_un_next = un + 1;
}
break;
}
un = ((un == MD_MAXUNITS - 1) ? 0 : un + 1);
if (un == start) {
un = MD_UNITBAD;
break;
}
}
mutex_exit(&md_mx);
return (un);
}
|