1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
|
/*
* 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) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/signal.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/session.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/stropts.h>
#include <sys/poll.h>
#include <sys/systm.h>
#include <sys/cpuvar.h>
#include <sys/uio.h>
#include <sys/cmn_err.h>
#include <sys/priocntl.h>
#include <sys/procset.h>
#include <sys/vmem.h>
#include <sys/bitmap.h>
#include <sys/kmem.h>
#include <sys/siginfo.h>
#include <sys/vtrace.h>
#include <sys/callb.h>
#include <sys/debug.h>
#include <sys/modctl.h>
#include <sys/vmsystm.h>
#include <vm/page.h>
#include <sys/atomic.h>
#include <sys/suntpi.h>
#include <sys/strlog.h>
#include <sys/promif.h>
#include <sys/project.h>
#include <sys/vm.h>
#include <sys/taskq.h>
#include <sys/sunddi.h>
#include <sys/sunldi_impl.h>
#include <sys/strsun.h>
#include <sys/isa_defs.h>
#include <sys/multidata.h>
#include <sys/pattr.h>
#include <sys/strft.h>
#include <sys/fs/snode.h>
#include <sys/zone.h>
#include <sys/open.h>
#include <sys/sunldi.h>
#include <sys/sad.h>
#include <sys/netstack.h>
#define O_SAMESTR(q) (((q)->q_next) && \
(((q)->q_flag & QREADR) == ((q)->q_next->q_flag & QREADR)))
/*
* WARNING:
* The variables and routines in this file are private, belonging
* to the STREAMS subsystem. These should not be used by modules
* or drivers. Compatibility will not be guaranteed.
*/
/*
* Id value used to distinguish between different multiplexor links.
*/
static int32_t lnk_id = 0;
#define STREAMS_LOPRI MINCLSYSPRI
static pri_t streams_lopri = STREAMS_LOPRI;
#define STRSTAT(x) (str_statistics.x.value.ui64++)
typedef struct str_stat {
kstat_named_t sqenables;
kstat_named_t stenables;
kstat_named_t syncqservice;
kstat_named_t freebs;
kstat_named_t qwr_outer;
kstat_named_t rservice;
kstat_named_t strwaits;
kstat_named_t taskqfails;
kstat_named_t bufcalls;
kstat_named_t qhelps;
kstat_named_t qremoved;
kstat_named_t sqremoved;
kstat_named_t bcwaits;
kstat_named_t sqtoomany;
} str_stat_t;
static str_stat_t str_statistics = {
{ "sqenables", KSTAT_DATA_UINT64 },
{ "stenables", KSTAT_DATA_UINT64 },
{ "syncqservice", KSTAT_DATA_UINT64 },
{ "freebs", KSTAT_DATA_UINT64 },
{ "qwr_outer", KSTAT_DATA_UINT64 },
{ "rservice", KSTAT_DATA_UINT64 },
{ "strwaits", KSTAT_DATA_UINT64 },
{ "taskqfails", KSTAT_DATA_UINT64 },
{ "bufcalls", KSTAT_DATA_UINT64 },
{ "qhelps", KSTAT_DATA_UINT64 },
{ "qremoved", KSTAT_DATA_UINT64 },
{ "sqremoved", KSTAT_DATA_UINT64 },
{ "bcwaits", KSTAT_DATA_UINT64 },
{ "sqtoomany", KSTAT_DATA_UINT64 },
};
static kstat_t *str_kstat;
/*
* qrunflag was used previously to control background scheduling of queues. It
* is not used anymore, but kept here in case some module still wants to access
* it via qready() and setqsched macros.
*/
char qrunflag; /* Unused */
/*
* Most of the streams scheduling is done via task queues. Task queues may fail
* for non-sleep dispatches, so there are two backup threads servicing failed
* requests for queues and syncqs. Both of these threads also service failed
* dispatches freebs requests. Queues are put in the list specified by `qhead'
* and `qtail' pointers, syncqs use `sqhead' and `sqtail' pointers and freebs
* requests are put into `freebs_list' which has no tail pointer. All three
* lists are protected by a single `service_queue' lock and use
* `services_to_run' condition variable for signaling background threads. Use of
* a single lock should not be a problem because it is only used under heavy
* loads when task queues start to fail and at that time it may be a good idea
* to throttle scheduling requests.
*
* NOTE: queues and syncqs should be scheduled by two separate threads because
* queue servicing may be blocked waiting for a syncq which may be also
* scheduled for background execution. This may create a deadlock when only one
* thread is used for both.
*/
static taskq_t *streams_taskq; /* Used for most STREAMS scheduling */
static kmutex_t service_queue; /* protects all of servicing vars */
static kcondvar_t services_to_run; /* wake up background service thread */
static kcondvar_t syncqs_to_run; /* wake up background service thread */
/*
* List of queues scheduled for background processing due to lack of resources
* in the task queues. Protected by service_queue lock;
*/
static struct queue *qhead;
static struct queue *qtail;
/*
* Same list for syncqs
*/
static syncq_t *sqhead;
static syncq_t *sqtail;
static mblk_t *freebs_list; /* list of buffers to free */
/*
* Backup threads for servicing queues and syncqs
*/
kthread_t *streams_qbkgrnd_thread;
kthread_t *streams_sqbkgrnd_thread;
/*
* Bufcalls related variables.
*/
struct bclist strbcalls; /* list of waiting bufcalls */
kmutex_t strbcall_lock; /* protects bufcall list (strbcalls) */
kcondvar_t strbcall_cv; /* Signaling when a bufcall is added */
kmutex_t bcall_monitor; /* sleep/wakeup style monitor */
kcondvar_t bcall_cv; /* wait 'till executing bufcall completes */
kthread_t *bc_bkgrnd_thread; /* Thread to service bufcall requests */
kmutex_t strresources; /* protects global resources */
kmutex_t muxifier; /* single-threads multiplexor creation */
static void *str_stack_init(netstackid_t stackid, netstack_t *ns);
static void str_stack_shutdown(netstackid_t stackid, void *arg);
static void str_stack_fini(netstackid_t stackid, void *arg);
extern void time_to_wait(clock_t *, clock_t);
/*
* run_queues is no longer used, but is kept in case some 3rd party
* module/driver decides to use it.
*/
int run_queues = 0;
/*
* sq_max_size is the depth of the syncq (in number of messages) before
* qfill_syncq() starts QFULL'ing destination queues. As its primary
* consumer - IP is no longer D_MTPERMOD, but there may be other
* modules/drivers depend on this syncq flow control, we prefer to
* choose a large number as the default value. For potential
* performance gain, this value is tunable in /etc/system.
*/
int sq_max_size = 10000;
/*
* The number of ciputctrl structures per syncq and stream we create when
* needed.
*/
int n_ciputctrl;
int max_n_ciputctrl = 16;
/*
* If n_ciputctrl is < min_n_ciputctrl don't even create ciputctrl_cache.
*/
int min_n_ciputctrl = 2;
/*
* Per-driver/module syncqs
* ========================
*
* For drivers/modules that use PERMOD or outer syncqs we keep a list of
* perdm structures, new entries being added (and new syncqs allocated) when
* setq() encounters a module/driver with a streamtab that it hasn't seen
* before.
* The reason for this mechanism is that some modules and drivers share a
* common streamtab and it is necessary for those modules and drivers to also
* share a common PERMOD syncq.
*
* perdm_list --> dm_str == streamtab_1
* dm_sq == syncq_1
* dm_ref
* dm_next --> dm_str == streamtab_2
* dm_sq == syncq_2
* dm_ref
* dm_next --> ... NULL
*
* The dm_ref field is incremented for each new driver/module that takes
* a reference to the perdm structure and hence shares the syncq.
* References are held in the fmodsw_impl_t structure for each STREAMS module
* or the dev_impl array (indexed by device major number) for each driver.
*
* perdm_list -> [dm_ref == 1] -> [dm_ref == 2] -> [dm_ref == 1] -> NULL
* ^ ^ ^ ^
* | ______________/ | |
* | / | |
* dev_impl: ...|x|y|... module A module B
*
* When a module/driver is unloaded the reference count is decremented and,
* when it falls to zero, the perdm structure is removed from the list and
* the syncq is freed (see rele_dm()).
*/
perdm_t *perdm_list = NULL;
static krwlock_t perdm_rwlock;
cdevsw_impl_t *devimpl;
extern struct qinit strdata;
extern struct qinit stwdata;
static void runservice(queue_t *);
static void streams_bufcall_service(void);
static void streams_qbkgrnd_service(void);
static void streams_sqbkgrnd_service(void);
static syncq_t *new_syncq(void);
static void free_syncq(syncq_t *);
static void outer_insert(syncq_t *, syncq_t *);
static void outer_remove(syncq_t *, syncq_t *);
static void write_now(syncq_t *);
static void clr_qfull(queue_t *);
static void runbufcalls(void);
static void sqenable(syncq_t *);
static void sqfill_events(syncq_t *, queue_t *, mblk_t *, void (*)());
static void wait_q_syncq(queue_t *);
static void backenable_insertedq(queue_t *);
static void queue_service(queue_t *);
static void stream_service(stdata_t *);
static void syncq_service(syncq_t *);
static void qwriter_outer_service(syncq_t *);
static void mblk_free(mblk_t *);
#ifdef DEBUG
static int qprocsareon(queue_t *);
#endif
static void set_nfsrv_ptr(queue_t *, queue_t *, queue_t *, queue_t *);
static void reset_nfsrv_ptr(queue_t *, queue_t *);
void set_qfull(queue_t *);
static void sq_run_events(syncq_t *);
static int propagate_syncq(queue_t *);
static void blocksq(syncq_t *, ushort_t, int);
static void unblocksq(syncq_t *, ushort_t, int);
static int dropsq(syncq_t *, uint16_t);
static void emptysq(syncq_t *);
static sqlist_t *sqlist_alloc(struct stdata *, int);
static void sqlist_free(sqlist_t *);
static sqlist_t *sqlist_build(queue_t *, struct stdata *, boolean_t);
static void sqlist_insert(sqlist_t *, syncq_t *);
static void sqlist_insertall(sqlist_t *, queue_t *);
static void strsetuio(stdata_t *);
struct kmem_cache *stream_head_cache;
struct kmem_cache *queue_cache;
struct kmem_cache *syncq_cache;
struct kmem_cache *qband_cache;
struct kmem_cache *linkinfo_cache;
struct kmem_cache *ciputctrl_cache = NULL;
static linkinfo_t *linkinfo_list;
/* Global esballoc throttling queue */
static esb_queue_t system_esbq;
/*
* esballoc tunable parameters.
*/
int esbq_max_qlen = 0x16; /* throttled queue length */
clock_t esbq_timeout = 0x8; /* timeout to process esb queue */
/*
* Routines to handle esballoc queueing.
*/
static void esballoc_process_queue(esb_queue_t *);
static void esballoc_enqueue_mblk(mblk_t *);
static void esballoc_timer(void *);
static void esballoc_set_timer(esb_queue_t *, clock_t);
static void esballoc_mblk_free(mblk_t *);
/*
* Qinit structure and Module_info structures
* for passthru read and write queues
*/
static void pass_wput(queue_t *, mblk_t *);
static queue_t *link_addpassthru(stdata_t *);
static void link_rempassthru(queue_t *);
struct module_info passthru_info = {
0,
"passthru",
0,
INFPSZ,
STRHIGH,
STRLOW
};
struct qinit passthru_rinit = {
(int (*)())putnext,
NULL,
NULL,
NULL,
NULL,
&passthru_info,
NULL
};
struct qinit passthru_winit = {
(int (*)()) pass_wput,
NULL,
NULL,
NULL,
NULL,
&passthru_info,
NULL
};
/*
* Special form of assertion: verify that X implies Y i.e. when X is true Y
* should also be true.
*/
#define IMPLY(X, Y) ASSERT(!(X) || (Y))
/*
* Logical equivalence. Verify that both X and Y are either TRUE or FALSE.
*/
#define EQUIV(X, Y) { IMPLY(X, Y); IMPLY(Y, X); }
/*
* Verify correctness of list head/tail pointers.
*/
#define LISTCHECK(head, tail, link) { \
EQUIV(head, tail); \
IMPLY(tail != NULL, tail->link == NULL); \
}
/*
* Enqueue a list element `el' in the end of a list denoted by `head' and `tail'
* using a `link' field.
*/
#define ENQUEUE(el, head, tail, link) { \
ASSERT(el->link == NULL); \
LISTCHECK(head, tail, link); \
if (head == NULL) \
head = el; \
else \
tail->link = el; \
tail = el; \
}
/*
* Dequeue the first element of the list denoted by `head' and `tail' pointers
* using a `link' field and put result into `el'.
*/
#define DQ(el, head, tail, link) { \
LISTCHECK(head, tail, link); \
el = head; \
if (head != NULL) { \
head = head->link; \
if (head == NULL) \
tail = NULL; \
el->link = NULL; \
} \
}
/*
* Remove `el' from the list using `chase' and `curr' pointers and return result
* in `succeed'.
*/
#define RMQ(el, head, tail, link, chase, curr, succeed) { \
LISTCHECK(head, tail, link); \
chase = NULL; \
succeed = 0; \
for (curr = head; (curr != el) && (curr != NULL); curr = curr->link) \
chase = curr; \
if (curr != NULL) { \
succeed = 1; \
ASSERT(curr == el); \
if (chase != NULL) \
chase->link = curr->link; \
else \
head = curr->link; \
curr->link = NULL; \
if (curr == tail) \
tail = chase; \
} \
LISTCHECK(head, tail, link); \
}
/* Handling of delayed messages on the inner syncq. */
/*
* DEBUG versions should use function versions (to simplify tracing) and
* non-DEBUG kernels should use macro versions.
*/
/*
* Put a queue on the syncq list of queues.
* Assumes SQLOCK held.
*/
#define SQPUT_Q(sq, qp) \
{ \
ASSERT(MUTEX_HELD(SQLOCK(sq))); \
if (!(qp->q_sqflags & Q_SQQUEUED)) { \
/* The queue should not be linked anywhere */ \
ASSERT((qp->q_sqprev == NULL) && (qp->q_sqnext == NULL)); \
/* Head and tail may only be NULL simultaneously */ \
EQUIV(sq->sq_head, sq->sq_tail); \
/* Queue may be only enqueued on its syncq */ \
ASSERT(sq == qp->q_syncq); \
/* Check the correctness of SQ_MESSAGES flag */ \
EQUIV(sq->sq_head, (sq->sq_flags & SQ_MESSAGES)); \
/* Sanity check first/last elements of the list */ \
IMPLY(sq->sq_head != NULL, sq->sq_head->q_sqprev == NULL);\
IMPLY(sq->sq_tail != NULL, sq->sq_tail->q_sqnext == NULL);\
/* \
* Sanity check of priority field: empty queue should \
* have zero priority \
* and nqueues equal to zero. \
*/ \
IMPLY(sq->sq_head == NULL, sq->sq_pri == 0); \
/* Sanity check of sq_nqueues field */ \
EQUIV(sq->sq_head, sq->sq_nqueues); \
if (sq->sq_head == NULL) { \
sq->sq_head = sq->sq_tail = qp; \
sq->sq_flags |= SQ_MESSAGES; \
} else if (qp->q_spri == 0) { \
qp->q_sqprev = sq->sq_tail; \
sq->sq_tail->q_sqnext = qp; \
sq->sq_tail = qp; \
} else { \
/* \
* Put this queue in priority order: higher \
* priority gets closer to the head. \
*/ \
queue_t **qpp = &sq->sq_tail; \
queue_t *qnext = NULL; \
\
while (*qpp != NULL && qp->q_spri > (*qpp)->q_spri) { \
qnext = *qpp; \
qpp = &(*qpp)->q_sqprev; \
} \
qp->q_sqnext = qnext; \
qp->q_sqprev = *qpp; \
if (*qpp != NULL) { \
(*qpp)->q_sqnext = qp; \
} else { \
sq->sq_head = qp; \
sq->sq_pri = sq->sq_head->q_spri; \
} \
*qpp = qp; \
} \
qp->q_sqflags |= Q_SQQUEUED; \
qp->q_sqtstamp = lbolt; \
sq->sq_nqueues++; \
} \
}
/*
* Remove a queue from the syncq list
* Assumes SQLOCK held.
*/
#define SQRM_Q(sq, qp) \
{ \
ASSERT(MUTEX_HELD(SQLOCK(sq))); \
ASSERT(qp->q_sqflags & Q_SQQUEUED); \
ASSERT(sq->sq_head != NULL && sq->sq_tail != NULL); \
ASSERT((sq->sq_flags & SQ_MESSAGES) != 0); \
/* Check that the queue is actually in the list */ \
ASSERT(qp->q_sqnext != NULL || sq->sq_tail == qp); \
ASSERT(qp->q_sqprev != NULL || sq->sq_head == qp); \
ASSERT(sq->sq_nqueues != 0); \
if (qp->q_sqprev == NULL) { \
/* First queue on list, make head q_sqnext */ \
sq->sq_head = qp->q_sqnext; \
} else { \
/* Make prev->next == next */ \
qp->q_sqprev->q_sqnext = qp->q_sqnext; \
} \
if (qp->q_sqnext == NULL) { \
/* Last queue on list, make tail sqprev */ \
sq->sq_tail = qp->q_sqprev; \
} else { \
/* Make next->prev == prev */ \
qp->q_sqnext->q_sqprev = qp->q_sqprev; \
} \
/* clear out references on this queue */ \
qp->q_sqprev = qp->q_sqnext = NULL; \
qp->q_sqflags &= ~Q_SQQUEUED; \
/* If there is nothing queued, clear SQ_MESSAGES */ \
if (sq->sq_head != NULL) { \
sq->sq_pri = sq->sq_head->q_spri; \
} else { \
sq->sq_flags &= ~SQ_MESSAGES; \
sq->sq_pri = 0; \
} \
sq->sq_nqueues--; \
ASSERT(sq->sq_head != NULL || sq->sq_evhead != NULL || \
(sq->sq_flags & SQ_QUEUED) == 0); \
}
/* Hide the definition from the header file. */
#ifdef SQPUT_MP
#undef SQPUT_MP
#endif
/*
* Put a message on the queue syncq.
* Assumes QLOCK held.
*/
#define SQPUT_MP(qp, mp) \
{ \
ASSERT(MUTEX_HELD(QLOCK(qp))); \
ASSERT(qp->q_sqhead == NULL || \
(qp->q_sqtail != NULL && \
qp->q_sqtail->b_next == NULL)); \
qp->q_syncqmsgs++; \
ASSERT(qp->q_syncqmsgs != 0); /* Wraparound */ \
if (qp->q_sqhead == NULL) { \
qp->q_sqhead = qp->q_sqtail = mp; \
} else { \
qp->q_sqtail->b_next = mp; \
qp->q_sqtail = mp; \
} \
ASSERT(qp->q_syncqmsgs > 0); \
set_qfull(qp); \
}
#define SQ_PUTCOUNT_SETFAST_LOCKED(sq) { \
ASSERT(MUTEX_HELD(SQLOCK(sq))); \
if ((sq)->sq_ciputctrl != NULL) { \
int i; \
int nlocks = (sq)->sq_nciputctrl; \
ciputctrl_t *cip = (sq)->sq_ciputctrl; \
ASSERT((sq)->sq_type & SQ_CIPUT); \
for (i = 0; i <= nlocks; i++) { \
ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \
cip[i].ciputctrl_count |= SQ_FASTPUT; \
} \
} \
}
#define SQ_PUTCOUNT_CLRFAST_LOCKED(sq) { \
ASSERT(MUTEX_HELD(SQLOCK(sq))); \
if ((sq)->sq_ciputctrl != NULL) { \
int i; \
int nlocks = (sq)->sq_nciputctrl; \
ciputctrl_t *cip = (sq)->sq_ciputctrl; \
ASSERT((sq)->sq_type & SQ_CIPUT); \
for (i = 0; i <= nlocks; i++) { \
ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \
cip[i].ciputctrl_count &= ~SQ_FASTPUT; \
} \
} \
}
/*
* Run service procedures for all queues in the stream head.
*/
#define STR_SERVICE(stp, q) { \
ASSERT(MUTEX_HELD(&stp->sd_qlock)); \
while (stp->sd_qhead != NULL) { \
DQ(q, stp->sd_qhead, stp->sd_qtail, q_link); \
ASSERT(stp->sd_nqueues > 0); \
stp->sd_nqueues--; \
ASSERT(!(q->q_flag & QINSERVICE)); \
mutex_exit(&stp->sd_qlock); \
queue_service(q); \
mutex_enter(&stp->sd_qlock); \
} \
ASSERT(stp->sd_nqueues == 0); \
ASSERT((stp->sd_qhead == NULL) && (stp->sd_qtail == NULL)); \
}
/*
* Constructor/destructor routines for the stream head cache
*/
/* ARGSUSED */
static int
stream_head_constructor(void *buf, void *cdrarg, int kmflags)
{
stdata_t *stp = buf;
mutex_init(&stp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&stp->sd_reflock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&stp->sd_qlock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&stp->sd_monitor, NULL, CV_DEFAULT, NULL);
cv_init(&stp->sd_iocmonitor, NULL, CV_DEFAULT, NULL);
cv_init(&stp->sd_refmonitor, NULL, CV_DEFAULT, NULL);
cv_init(&stp->sd_qcv, NULL, CV_DEFAULT, NULL);
cv_init(&stp->sd_zcopy_wait, NULL, CV_DEFAULT, NULL);
stp->sd_wrq = NULL;
return (0);
}
/* ARGSUSED */
static void
stream_head_destructor(void *buf, void *cdrarg)
{
stdata_t *stp = buf;
mutex_destroy(&stp->sd_lock);
mutex_destroy(&stp->sd_reflock);
mutex_destroy(&stp->sd_qlock);
cv_destroy(&stp->sd_monitor);
cv_destroy(&stp->sd_iocmonitor);
cv_destroy(&stp->sd_refmonitor);
cv_destroy(&stp->sd_qcv);
cv_destroy(&stp->sd_zcopy_wait);
}
/*
* Constructor/destructor routines for the queue cache
*/
/* ARGSUSED */
static int
queue_constructor(void *buf, void *cdrarg, int kmflags)
{
queinfo_t *qip = buf;
queue_t *qp = &qip->qu_rqueue;
queue_t *wqp = &qip->qu_wqueue;
syncq_t *sq = &qip->qu_syncq;
qp->q_first = NULL;
qp->q_link = NULL;
qp->q_count = 0;
qp->q_mblkcnt = 0;
qp->q_sqhead = NULL;
qp->q_sqtail = NULL;
qp->q_sqnext = NULL;
qp->q_sqprev = NULL;
qp->q_sqflags = 0;
qp->q_rwcnt = 0;
qp->q_spri = 0;
mutex_init(QLOCK(qp), NULL, MUTEX_DEFAULT, NULL);
cv_init(&qp->q_wait, NULL, CV_DEFAULT, NULL);
wqp->q_first = NULL;
wqp->q_link = NULL;
wqp->q_count = 0;
wqp->q_mblkcnt = 0;
wqp->q_sqhead = NULL;
wqp->q_sqtail = NULL;
wqp->q_sqnext = NULL;
wqp->q_sqprev = NULL;
wqp->q_sqflags = 0;
wqp->q_rwcnt = 0;
wqp->q_spri = 0;
mutex_init(QLOCK(wqp), NULL, MUTEX_DEFAULT, NULL);
cv_init(&wqp->q_wait, NULL, CV_DEFAULT, NULL);
sq->sq_head = NULL;
sq->sq_tail = NULL;
sq->sq_evhead = NULL;
sq->sq_evtail = NULL;
sq->sq_callbpend = NULL;
sq->sq_outer = NULL;
sq->sq_onext = NULL;
sq->sq_oprev = NULL;
sq->sq_next = NULL;
sq->sq_svcflags = 0;
sq->sq_servcount = 0;
sq->sq_needexcl = 0;
sq->sq_nqueues = 0;
sq->sq_pri = 0;
mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL);
cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL);
return (0);
}
/* ARGSUSED */
static void
queue_destructor(void *buf, void *cdrarg)
{
queinfo_t *qip = buf;
queue_t *qp = &qip->qu_rqueue;
queue_t *wqp = &qip->qu_wqueue;
syncq_t *sq = &qip->qu_syncq;
ASSERT(qp->q_sqhead == NULL);
ASSERT(wqp->q_sqhead == NULL);
ASSERT(qp->q_sqnext == NULL);
ASSERT(wqp->q_sqnext == NULL);
ASSERT(qp->q_rwcnt == 0);
ASSERT(wqp->q_rwcnt == 0);
mutex_destroy(&qp->q_lock);
cv_destroy(&qp->q_wait);
mutex_destroy(&wqp->q_lock);
cv_destroy(&wqp->q_wait);
mutex_destroy(&sq->sq_lock);
cv_destroy(&sq->sq_wait);
cv_destroy(&sq->sq_exitwait);
}
/*
* Constructor/destructor routines for the syncq cache
*/
/* ARGSUSED */
static int
syncq_constructor(void *buf, void *cdrarg, int kmflags)
{
syncq_t *sq = buf;
bzero(buf, sizeof (syncq_t));
mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL);
cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL);
return (0);
}
/* ARGSUSED */
static void
syncq_destructor(void *buf, void *cdrarg)
{
syncq_t *sq = buf;
ASSERT(sq->sq_head == NULL);
ASSERT(sq->sq_tail == NULL);
ASSERT(sq->sq_evhead == NULL);
ASSERT(sq->sq_evtail == NULL);
ASSERT(sq->sq_callbpend == NULL);
ASSERT(sq->sq_callbflags == 0);
ASSERT(sq->sq_outer == NULL);
ASSERT(sq->sq_onext == NULL);
ASSERT(sq->sq_oprev == NULL);
ASSERT(sq->sq_next == NULL);
ASSERT(sq->sq_needexcl == 0);
ASSERT(sq->sq_svcflags == 0);
ASSERT(sq->sq_servcount == 0);
ASSERT(sq->sq_nqueues == 0);
ASSERT(sq->sq_pri == 0);
ASSERT(sq->sq_count == 0);
ASSERT(sq->sq_rmqcount == 0);
ASSERT(sq->sq_cancelid == 0);
ASSERT(sq->sq_ciputctrl == NULL);
ASSERT(sq->sq_nciputctrl == 0);
ASSERT(sq->sq_type == 0);
ASSERT(sq->sq_flags == 0);
mutex_destroy(&sq->sq_lock);
cv_destroy(&sq->sq_wait);
cv_destroy(&sq->sq_exitwait);
}
/* ARGSUSED */
static int
ciputctrl_constructor(void *buf, void *cdrarg, int kmflags)
{
ciputctrl_t *cip = buf;
int i;
for (i = 0; i < n_ciputctrl; i++) {
cip[i].ciputctrl_count = SQ_FASTPUT;
mutex_init(&cip[i].ciputctrl_lock, NULL, MUTEX_DEFAULT, NULL);
}
return (0);
}
/* ARGSUSED */
static void
ciputctrl_destructor(void *buf, void *cdrarg)
{
ciputctrl_t *cip = buf;
int i;
for (i = 0; i < n_ciputctrl; i++) {
ASSERT(cip[i].ciputctrl_count & SQ_FASTPUT);
mutex_destroy(&cip[i].ciputctrl_lock);
}
}
/*
* Init routine run from main at boot time.
*/
void
strinit(void)
{
int ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus);
stream_head_cache = kmem_cache_create("stream_head_cache",
sizeof (stdata_t), 0,
stream_head_constructor, stream_head_destructor, NULL,
NULL, NULL, 0);
queue_cache = kmem_cache_create("queue_cache", sizeof (queinfo_t), 0,
queue_constructor, queue_destructor, NULL, NULL, NULL, 0);
syncq_cache = kmem_cache_create("syncq_cache", sizeof (syncq_t), 0,
syncq_constructor, syncq_destructor, NULL, NULL, NULL, 0);
qband_cache = kmem_cache_create("qband_cache",
sizeof (qband_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
linkinfo_cache = kmem_cache_create("linkinfo_cache",
sizeof (linkinfo_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
n_ciputctrl = ncpus;
n_ciputctrl = 1 << highbit(n_ciputctrl - 1);
ASSERT(n_ciputctrl >= 1);
n_ciputctrl = MIN(n_ciputctrl, max_n_ciputctrl);
if (n_ciputctrl >= min_n_ciputctrl) {
ciputctrl_cache = kmem_cache_create("ciputctrl_cache",
sizeof (ciputctrl_t) * n_ciputctrl,
sizeof (ciputctrl_t), ciputctrl_constructor,
ciputctrl_destructor, NULL, NULL, NULL, 0);
}
streams_taskq = system_taskq;
if (streams_taskq == NULL)
panic("strinit: no memory for streams taskq!");
bc_bkgrnd_thread = thread_create(NULL, 0,
streams_bufcall_service, NULL, 0, &p0, TS_RUN, streams_lopri);
streams_qbkgrnd_thread = thread_create(NULL, 0,
streams_qbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri);
streams_sqbkgrnd_thread = thread_create(NULL, 0,
streams_sqbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri);
/*
* Create STREAMS kstats.
*/
str_kstat = kstat_create("streams", 0, "strstat",
"net", KSTAT_TYPE_NAMED,
sizeof (str_statistics) / sizeof (kstat_named_t),
KSTAT_FLAG_VIRTUAL);
if (str_kstat != NULL) {
str_kstat->ks_data = &str_statistics;
kstat_install(str_kstat);
}
/*
* TPI support routine initialisation.
*/
tpi_init();
/*
* Handle to have autopush and persistent link information per
* zone.
* Note: uses shutdown hook instead of destroy hook so that the
* persistent links can be torn down before the destroy hooks
* in the TCP/IP stack are called.
*/
netstack_register(NS_STR, str_stack_init, str_stack_shutdown,
str_stack_fini);
}
void
str_sendsig(vnode_t *vp, int event, uchar_t band, int error)
{
struct stdata *stp;
ASSERT(vp->v_stream);
stp = vp->v_stream;
/* Have to hold sd_lock to prevent siglist from changing */
mutex_enter(&stp->sd_lock);
if (stp->sd_sigflags & event)
strsendsig(stp->sd_siglist, event, band, error);
mutex_exit(&stp->sd_lock);
}
/*
* Send the "sevent" set of signals to a process.
* This might send more than one signal if the process is registered
* for multiple events. The caller should pass in an sevent that only
* includes the events for which the process has registered.
*/
static void
dosendsig(proc_t *proc, int events, int sevent, k_siginfo_t *info,
uchar_t band, int error)
{
ASSERT(MUTEX_HELD(&proc->p_lock));
info->si_band = 0;
info->si_errno = 0;
if (sevent & S_ERROR) {
sevent &= ~S_ERROR;
info->si_code = POLL_ERR;
info->si_errno = error;
TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
"strsendsig:proc %p info %p", proc, info);
sigaddq(proc, NULL, info, KM_NOSLEEP);
info->si_errno = 0;
}
if (sevent & S_HANGUP) {
sevent &= ~S_HANGUP;
info->si_code = POLL_HUP;
TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
"strsendsig:proc %p info %p", proc, info);
sigaddq(proc, NULL, info, KM_NOSLEEP);
}
if (sevent & S_HIPRI) {
sevent &= ~S_HIPRI;
info->si_code = POLL_PRI;
TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
"strsendsig:proc %p info %p", proc, info);
sigaddq(proc, NULL, info, KM_NOSLEEP);
}
if (sevent & S_RDBAND) {
sevent &= ~S_RDBAND;
if (events & S_BANDURG)
sigtoproc(proc, NULL, SIGURG);
else
sigtoproc(proc, NULL, SIGPOLL);
}
if (sevent & S_WRBAND) {
sevent &= ~S_WRBAND;
sigtoproc(proc, NULL, SIGPOLL);
}
if (sevent & S_INPUT) {
sevent &= ~S_INPUT;
info->si_code = POLL_IN;
info->si_band = band;
TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
"strsendsig:proc %p info %p", proc, info);
sigaddq(proc, NULL, info, KM_NOSLEEP);
info->si_band = 0;
}
if (sevent & S_OUTPUT) {
sevent &= ~S_OUTPUT;
info->si_code = POLL_OUT;
info->si_band = band;
TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
"strsendsig:proc %p info %p", proc, info);
sigaddq(proc, NULL, info, KM_NOSLEEP);
info->si_band = 0;
}
if (sevent & S_MSG) {
sevent &= ~S_MSG;
info->si_code = POLL_MSG;
info->si_band = band;
TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
"strsendsig:proc %p info %p", proc, info);
sigaddq(proc, NULL, info, KM_NOSLEEP);
info->si_band = 0;
}
if (sevent & S_RDNORM) {
sevent &= ~S_RDNORM;
sigtoproc(proc, NULL, SIGPOLL);
}
if (sevent != 0) {
panic("strsendsig: unknown event(s) %x", sevent);
}
}
/*
* Send SIGPOLL/SIGURG signal to all processes and process groups
* registered on the given signal list that want a signal for at
* least one of the specified events.
*
* Must be called with exclusive access to siglist (caller holding sd_lock).
*
* strioctl(I_SETSIG/I_ESETSIG) will only change siglist when holding
* sd_lock and the ioctl code maintains a PID_HOLD on the pid structure
* while it is in the siglist.
*
* For performance reasons (MP scalability) the code drops pidlock
* when sending signals to a single process.
* When sending to a process group the code holds
* pidlock to prevent the membership in the process group from changing
* while walking the p_pglink list.
*/
void
strsendsig(strsig_t *siglist, int event, uchar_t band, int error)
{
strsig_t *ssp;
k_siginfo_t info;
struct pid *pidp;
proc_t *proc;
info.si_signo = SIGPOLL;
info.si_errno = 0;
for (ssp = siglist; ssp; ssp = ssp->ss_next) {
int sevent;
sevent = ssp->ss_events & event;
if (sevent == 0)
continue;
if ((pidp = ssp->ss_pidp) == NULL) {
/* pid was released but still on event list */
continue;
}
if (ssp->ss_pid > 0) {
/*
* XXX This unfortunately still generates
* a signal when a fd is closed but
* the proc is active.
*/
ASSERT(ssp->ss_pid == pidp->pid_id);
mutex_enter(&pidlock);
proc = prfind_zone(pidp->pid_id, ALL_ZONES);
if (proc == NULL) {
mutex_exit(&pidlock);
continue;
}
mutex_enter(&proc->p_lock);
mutex_exit(&pidlock);
dosendsig(proc, ssp->ss_events, sevent, &info,
band, error);
mutex_exit(&proc->p_lock);
} else {
/*
* Send to process group. Hold pidlock across
* calls to dosendsig().
*/
pid_t pgrp = -ssp->ss_pid;
mutex_enter(&pidlock);
proc = pgfind_zone(pgrp, ALL_ZONES);
while (proc != NULL) {
mutex_enter(&proc->p_lock);
dosendsig(proc, ssp->ss_events, sevent,
&info, band, error);
mutex_exit(&proc->p_lock);
proc = proc->p_pglink;
}
mutex_exit(&pidlock);
}
}
}
/*
* Attach a stream device or module.
* qp is a read queue; the new queue goes in so its next
* read ptr is the argument, and the write queue corresponding
* to the argument points to this queue. Return 0 on success,
* or a non-zero errno on failure.
*/
int
qattach(queue_t *qp, dev_t *devp, int oflag, cred_t *crp, fmodsw_impl_t *fp,
boolean_t is_insert)
{
major_t major;
cdevsw_impl_t *dp;
struct streamtab *str;
queue_t *rq;
queue_t *wrq;
uint32_t qflag;
uint32_t sqtype;
perdm_t *dmp;
int error;
int sflag;
rq = allocq();
wrq = _WR(rq);
STREAM(rq) = STREAM(wrq) = STREAM(qp);
if (fp != NULL) {
str = fp->f_str;
qflag = fp->f_qflag;
sqtype = fp->f_sqtype;
dmp = fp->f_dmp;
IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL);
sflag = MODOPEN;
/*
* stash away a pointer to the module structure so we can
* unref it in qdetach.
*/
rq->q_fp = fp;
} else {
ASSERT(!is_insert);
major = getmajor(*devp);
dp = &devimpl[major];
str = dp->d_str;
ASSERT(str == STREAMSTAB(major));
qflag = dp->d_qflag;
ASSERT(qflag & QISDRV);
sqtype = dp->d_sqtype;
/* create perdm_t if needed */
if (NEED_DM(dp->d_dmp, qflag))
dp->d_dmp = hold_dm(str, qflag, sqtype);
dmp = dp->d_dmp;
sflag = 0;
}
TRACE_2(TR_FAC_STREAMS_FR, TR_QATTACH_FLAGS,
"qattach:qflag == %X(%X)", qflag, *devp);
/* setq might sleep in allocator - avoid holding locks. */
setq(rq, str->st_rdinit, str->st_wrinit, dmp, qflag, sqtype, B_FALSE);
/*
* Before calling the module's open routine, set up the q_next
* pointer for inserting a module in the middle of a stream.
*
* Note that we can always set _QINSERTING and set up q_next
* pointer for both inserting and pushing a module. Then there
* is no need for the is_insert parameter. In insertq(), called
* by qprocson(), assume that q_next of the new module always points
* to the correct queue and use it for insertion. Everything should
* work out fine. But in the first release of _I_INSERT, we
* distinguish between inserting and pushing to make sure that
* pushing a module follows the same code path as before.
*/
if (is_insert) {
rq->q_flag |= _QINSERTING;
rq->q_next = qp;
}
/*
* If there is an outer perimeter get exclusive access during
* the open procedure. Bump up the reference count on the queue.
*/
entersq(rq->q_syncq, SQ_OPENCLOSE);
error = (*rq->q_qinfo->qi_qopen)(rq, devp, oflag, sflag, crp);
if (error != 0)
goto failed;
leavesq(rq->q_syncq, SQ_OPENCLOSE);
ASSERT(qprocsareon(rq));
return (0);
failed:
rq->q_flag &= ~_QINSERTING;
if (backq(wrq) != NULL && backq(wrq)->q_next == wrq)
qprocsoff(rq);
leavesq(rq->q_syncq, SQ_OPENCLOSE);
rq->q_next = wrq->q_next = NULL;
qdetach(rq, 0, 0, crp, B_FALSE);
return (error);
}
/*
* Handle second open of stream. For modules, set the
* last argument to MODOPEN and do not pass any open flags.
* Ignore dummydev since this is not the first open.
*/
int
qreopen(queue_t *qp, dev_t *devp, int flag, cred_t *crp)
{
int error;
dev_t dummydev;
queue_t *wqp = _WR(qp);
ASSERT(qp->q_flag & QREADR);
entersq(qp->q_syncq, SQ_OPENCLOSE);
dummydev = *devp;
if (error = ((*qp->q_qinfo->qi_qopen)(qp, &dummydev,
(wqp->q_next ? 0 : flag), (wqp->q_next ? MODOPEN : 0), crp))) {
leavesq(qp->q_syncq, SQ_OPENCLOSE);
mutex_enter(&STREAM(qp)->sd_lock);
qp->q_stream->sd_flag |= STREOPENFAIL;
mutex_exit(&STREAM(qp)->sd_lock);
return (error);
}
leavesq(qp->q_syncq, SQ_OPENCLOSE);
/*
* successful open should have done qprocson()
*/
ASSERT(qprocsareon(_RD(qp)));
return (0);
}
/*
* Detach a stream module or device.
* If clmode == 1 then the module or driver was opened and its
* close routine must be called. If clmode == 0, the module
* or driver was never opened or the open failed, and so its close
* should not be called.
*/
void
qdetach(queue_t *qp, int clmode, int flag, cred_t *crp, boolean_t is_remove)
{
queue_t *wqp = _WR(qp);
ASSERT(STREAM(qp)->sd_flag & (STRCLOSE|STWOPEN|STRPLUMB));
if (STREAM_NEEDSERVICE(STREAM(qp)))
stream_runservice(STREAM(qp));
if (clmode) {
/*
* Make sure that all the messages on the write side syncq are
* processed and nothing is left. Since we are closing, no new
* messages may appear there.
*/
wait_q_syncq(wqp);
entersq(qp->q_syncq, SQ_OPENCLOSE);
if (is_remove) {
mutex_enter(QLOCK(qp));
qp->q_flag |= _QREMOVING;
mutex_exit(QLOCK(qp));
}
(*qp->q_qinfo->qi_qclose)(qp, flag, crp);
/*
* Check that qprocsoff() was actually called.
*/
ASSERT((qp->q_flag & QWCLOSE) && (wqp->q_flag & QWCLOSE));
leavesq(qp->q_syncq, SQ_OPENCLOSE);
} else {
disable_svc(qp);
}
/*
* Allow any threads blocked in entersq to proceed and discover
* the QWCLOSE is set.
* Note: This assumes that all users of entersq check QWCLOSE.
* Currently runservice is the only entersq that can happen
* after removeq has finished.
* Removeq will have discarded all messages destined to the closing
* pair of queues from the syncq.
* NOTE: Calling a function inside an assert is unconventional.
* However, it does not cause any problem since flush_syncq() does
* not change any state except when it returns non-zero i.e.
* when the assert will trigger.
*/
ASSERT(flush_syncq(qp->q_syncq, qp) == 0);
ASSERT(flush_syncq(wqp->q_syncq, wqp) == 0);
ASSERT((qp->q_flag & QPERMOD) ||
((qp->q_syncq->sq_head == NULL) &&
(wqp->q_syncq->sq_head == NULL)));
/* release any fmodsw_impl_t structure held on behalf of the queue */
ASSERT(qp->q_fp != NULL || qp->q_flag & QISDRV);
if (qp->q_fp != NULL)
fmodsw_rele(qp->q_fp);
/* freeq removes us from the outer perimeter if any */
freeq(qp);
}
/* Prevent service procedures from being called */
void
disable_svc(queue_t *qp)
{
queue_t *wqp = _WR(qp);
ASSERT(qp->q_flag & QREADR);
mutex_enter(QLOCK(qp));
qp->q_flag |= QWCLOSE;
mutex_exit(QLOCK(qp));
mutex_enter(QLOCK(wqp));
wqp->q_flag |= QWCLOSE;
mutex_exit(QLOCK(wqp));
}
/* Allow service procedures to be called again */
void
enable_svc(queue_t *qp)
{
queue_t *wqp = _WR(qp);
ASSERT(qp->q_flag & QREADR);
mutex_enter(QLOCK(qp));
qp->q_flag &= ~QWCLOSE;
mutex_exit(QLOCK(qp));
mutex_enter(QLOCK(wqp));
wqp->q_flag &= ~QWCLOSE;
mutex_exit(QLOCK(wqp));
}
/*
* Remove queue from qhead/qtail if it is enabled.
* Only reset QENAB if the queue was removed from the runlist.
* A queue goes through 3 stages:
* It is on the service list and QENAB is set.
* It is removed from the service list but QENAB is still set.
* QENAB gets changed to QINSERVICE.
* QINSERVICE is reset (when the service procedure is done)
* Thus we can not reset QENAB unless we actually removed it from the service
* queue.
*/
void
remove_runlist(queue_t *qp)
{
if (qp->q_flag & QENAB && qhead != NULL) {
queue_t *q_chase;
queue_t *q_curr;
int removed;
mutex_enter(&service_queue);
RMQ(qp, qhead, qtail, q_link, q_chase, q_curr, removed);
mutex_exit(&service_queue);
if (removed) {
STRSTAT(qremoved);
qp->q_flag &= ~QENAB;
}
}
}
/*
* Wait for any pending service processing to complete.
* The removal of queues from the runlist is not atomic with the
* clearing of the QENABLED flag and setting the INSERVICE flag.
* consequently it is possible for remove_runlist in strclose
* to not find the queue on the runlist but for it to be QENABLED
* and not yet INSERVICE -> hence wait_svc needs to check QENABLED
* as well as INSERVICE.
*/
void
wait_svc(queue_t *qp)
{
queue_t *wqp = _WR(qp);
ASSERT(qp->q_flag & QREADR);
/*
* Try to remove queues from qhead/qtail list.
*/
if (qhead != NULL) {
remove_runlist(qp);
remove_runlist(wqp);
}
/*
* Wait till the syncqs associated with the queue disappear from the
* background processing list.
* This only needs to be done for non-PERMOD perimeters since
* for PERMOD perimeters the syncq may be shared and will only be freed
* when the last module/driver is unloaded.
* If for PERMOD perimeters queue was on the syncq list, removeq()
* should call propagate_syncq() or drain_syncq() for it. Both of these
* functions remove the queue from its syncq list, so sqthread will not
* try to access the queue.
*/
if (!(qp->q_flag & QPERMOD)) {
syncq_t *rsq = qp->q_syncq;
syncq_t *wsq = wqp->q_syncq;
/*
* Disable rsq and wsq and wait for any background processing of
* syncq to complete.
*/
wait_sq_svc(rsq);
if (wsq != rsq)
wait_sq_svc(wsq);
}
mutex_enter(QLOCK(qp));
while (qp->q_flag & (QINSERVICE|QENAB))
cv_wait(&qp->q_wait, QLOCK(qp));
mutex_exit(QLOCK(qp));
mutex_enter(QLOCK(wqp));
while (wqp->q_flag & (QINSERVICE|QENAB))
cv_wait(&wqp->q_wait, QLOCK(wqp));
mutex_exit(QLOCK(wqp));
}
/*
* Put ioctl data from userland buffer `arg' into the mblk chain `bp'.
* `flag' must always contain either K_TO_K or U_TO_K; STR_NOSIG may
* also be set, and is passed through to allocb_cred_wait().
*
* Returns errno on failure, zero on success.
*/
int
putiocd(mblk_t *bp, char *arg, int flag, cred_t *cr)
{
mblk_t *tmp;
ssize_t count;
int error = 0;
ASSERT((flag & (U_TO_K | K_TO_K)) == U_TO_K ||
(flag & (U_TO_K | K_TO_K)) == K_TO_K);
if (bp->b_datap->db_type == M_IOCTL) {
count = ((struct iocblk *)bp->b_rptr)->ioc_count;
} else {
ASSERT(bp->b_datap->db_type == M_COPYIN);
count = ((struct copyreq *)bp->b_rptr)->cq_size;
}
/*
* strdoioctl validates ioc_count, so if this assert fails it
* cannot be due to user error.
*/
ASSERT(count >= 0);
if ((tmp = allocb_cred_wait(count, (flag & STR_NOSIG), &error, cr,
curproc->p_pid)) == NULL) {
return (error);
}
error = strcopyin(arg, tmp->b_wptr, count, flag & (U_TO_K|K_TO_K));
if (error != 0) {
freeb(tmp);
return (error);
}
DB_CPID(tmp) = curproc->p_pid;
tmp->b_wptr += count;
bp->b_cont = tmp;
return (0);
}
/*
* Copy ioctl data to user-land. Return non-zero errno on failure,
* 0 for success.
*/
int
getiocd(mblk_t *bp, char *arg, int copymode)
{
ssize_t count;
size_t n;
int error;
if (bp->b_datap->db_type == M_IOCACK)
count = ((struct iocblk *)bp->b_rptr)->ioc_count;
else {
ASSERT(bp->b_datap->db_type == M_COPYOUT);
count = ((struct copyreq *)bp->b_rptr)->cq_size;
}
ASSERT(count >= 0);
for (bp = bp->b_cont; bp && count;
count -= n, bp = bp->b_cont, arg += n) {
n = MIN(count, bp->b_wptr - bp->b_rptr);
error = strcopyout(bp->b_rptr, arg, n, copymode);
if (error)
return (error);
}
ASSERT(count == 0);
return (0);
}
/*
* Allocate a linkinfo entry given the write queue of the
* bottom module of the top stream and the write queue of the
* stream head of the bottom stream.
*/
linkinfo_t *
alloclink(queue_t *qup, queue_t *qdown, file_t *fpdown)
{
linkinfo_t *linkp;
linkp = kmem_cache_alloc(linkinfo_cache, KM_SLEEP);
linkp->li_lblk.l_qtop = qup;
linkp->li_lblk.l_qbot = qdown;
linkp->li_fpdown = fpdown;
mutex_enter(&strresources);
linkp->li_next = linkinfo_list;
linkp->li_prev = NULL;
if (linkp->li_next)
linkp->li_next->li_prev = linkp;
linkinfo_list = linkp;
linkp->li_lblk.l_index = ++lnk_id;
ASSERT(lnk_id != 0); /* this should never wrap in practice */
mutex_exit(&strresources);
return (linkp);
}
/*
* Free a linkinfo entry.
*/
void
lbfree(linkinfo_t *linkp)
{
mutex_enter(&strresources);
if (linkp->li_next)
linkp->li_next->li_prev = linkp->li_prev;
if (linkp->li_prev)
linkp->li_prev->li_next = linkp->li_next;
else
linkinfo_list = linkp->li_next;
mutex_exit(&strresources);
kmem_cache_free(linkinfo_cache, linkp);
}
/*
* Check for a potential linking cycle.
* Return 1 if a link will result in a cycle,
* and 0 otherwise.
*/
int
linkcycle(stdata_t *upstp, stdata_t *lostp, str_stack_t *ss)
{
struct mux_node *np;
struct mux_edge *ep;
int i;
major_t lomaj;
major_t upmaj;
/*
* if the lower stream is a pipe/FIFO, return, since link
* cycles can not happen on pipes/FIFOs
*/
if (lostp->sd_vnode->v_type == VFIFO)
return (0);
for (i = 0; i < ss->ss_devcnt; i++) {
np = &ss->ss_mux_nodes[i];
MUX_CLEAR(np);
}
lomaj = getmajor(lostp->sd_vnode->v_rdev);
upmaj = getmajor(upstp->sd_vnode->v_rdev);
np = &ss->ss_mux_nodes[lomaj];
for (;;) {
if (!MUX_DIDVISIT(np)) {
if (np->mn_imaj == upmaj)
return (1);
if (np->mn_outp == NULL) {
MUX_VISIT(np);
if (np->mn_originp == NULL)
return (0);
np = np->mn_originp;
continue;
}
MUX_VISIT(np);
np->mn_startp = np->mn_outp;
} else {
if (np->mn_startp == NULL) {
if (np->mn_originp == NULL)
return (0);
else {
np = np->mn_originp;
continue;
}
}
/*
* If ep->me_nodep is a FIFO (me_nodep == NULL),
* ignore the edge and move on. ep->me_nodep gets
* set to NULL in mux_addedge() if it is a FIFO.
*
*/
ep = np->mn_startp;
np->mn_startp = ep->me_nextp;
if (ep->me_nodep == NULL)
continue;
ep->me_nodep->mn_originp = np;
np = ep->me_nodep;
}
}
}
/*
* Find linkinfo entry corresponding to the parameters.
*/
linkinfo_t *
findlinks(stdata_t *stp, int index, int type, str_stack_t *ss)
{
linkinfo_t *linkp;
struct mux_edge *mep;
struct mux_node *mnp;
queue_t *qup;
mutex_enter(&strresources);
if ((type & LINKTYPEMASK) == LINKNORMAL) {
qup = getendq(stp->sd_wrq);
for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) {
if ((qup == linkp->li_lblk.l_qtop) &&
(!index || (index == linkp->li_lblk.l_index))) {
mutex_exit(&strresources);
return (linkp);
}
}
} else {
ASSERT((type & LINKTYPEMASK) == LINKPERSIST);
mnp = &ss->ss_mux_nodes[getmajor(stp->sd_vnode->v_rdev)];
mep = mnp->mn_outp;
while (mep) {
if ((index == 0) || (index == mep->me_muxid))
break;
mep = mep->me_nextp;
}
if (!mep) {
mutex_exit(&strresources);
return (NULL);
}
for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) {
if ((!linkp->li_lblk.l_qtop) &&
(mep->me_muxid == linkp->li_lblk.l_index)) {
mutex_exit(&strresources);
return (linkp);
}
}
}
mutex_exit(&strresources);
return (NULL);
}
/*
* Given a queue ptr, follow the chain of q_next pointers until you reach the
* last queue on the chain and return it.
*/
queue_t *
getendq(queue_t *q)
{
ASSERT(q != NULL);
while (_SAMESTR(q))
q = q->q_next;
return (q);
}
/*
* Wait for the syncq count to drop to zero.
* sq could be either outer or inner.
*/
static void
wait_syncq(syncq_t *sq)
{
uint16_t count;
mutex_enter(SQLOCK(sq));
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
while (count != 0) {
sq->sq_flags |= SQ_WANTWAKEUP;
SQ_PUTLOCKS_EXIT(sq);
cv_wait(&sq->sq_wait, SQLOCK(sq));
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
}
SQ_PUTLOCKS_EXIT(sq);
mutex_exit(SQLOCK(sq));
}
/*
* Wait while there are any messages for the queue in its syncq.
*/
static void
wait_q_syncq(queue_t *q)
{
if ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) {
syncq_t *sq = q->q_syncq;
mutex_enter(SQLOCK(sq));
while ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) {
sq->sq_flags |= SQ_WANTWAKEUP;
cv_wait(&sq->sq_wait, SQLOCK(sq));
}
mutex_exit(SQLOCK(sq));
}
}
int
mlink_file(vnode_t *vp, int cmd, struct file *fpdown, cred_t *crp, int *rvalp,
int lhlink)
{
struct stdata *stp;
struct strioctl strioc;
struct linkinfo *linkp;
struct stdata *stpdown;
struct streamtab *str;
queue_t *passq;
syncq_t *passyncq;
queue_t *rq;
cdevsw_impl_t *dp;
uint32_t qflag;
uint32_t sqtype;
perdm_t *dmp;
int error = 0;
netstack_t *ns;
str_stack_t *ss;
stp = vp->v_stream;
TRACE_1(TR_FAC_STREAMS_FR,
TR_I_LINK, "I_LINK/I_PLINK:stp %p", stp);
/*
* Test for invalid upper stream
*/
if (stp->sd_flag & STRHUP) {
return (ENXIO);
}
if (vp->v_type == VFIFO) {
return (EINVAL);
}
if (stp->sd_strtab == NULL) {
return (EINVAL);
}
if (!stp->sd_strtab->st_muxwinit) {
return (EINVAL);
}
if (fpdown == NULL) {
return (EBADF);
}
ns = netstack_find_by_cred(crp);
ASSERT(ns != NULL);
ss = ns->netstack_str;
ASSERT(ss != NULL);
if (getmajor(stp->sd_vnode->v_rdev) >= ss->ss_devcnt) {
netstack_rele(ss->ss_netstack);
return (EINVAL);
}
mutex_enter(&muxifier);
if (stp->sd_flag & STPLEX) {
mutex_exit(&muxifier);
netstack_rele(ss->ss_netstack);
return (ENXIO);
}
/*
* Test for invalid lower stream.
* The check for the v_type != VFIFO and having a major
* number not >= devcnt is done to avoid problems with
* adding mux_node entry past the end of mux_nodes[].
* For FIFO's we don't add an entry so this isn't a
* problem.
*/
if (((stpdown = fpdown->f_vnode->v_stream) == NULL) ||
(stpdown == stp) || (stpdown->sd_flag &
(STPLEX|STRHUP|STRDERR|STWRERR|IOCWAIT|STRPLUMB)) ||
((stpdown->sd_vnode->v_type != VFIFO) &&
(getmajor(stpdown->sd_vnode->v_rdev) >= ss->ss_devcnt)) ||
linkcycle(stp, stpdown, ss)) {
mutex_exit(&muxifier);
netstack_rele(ss->ss_netstack);
return (EINVAL);
}
TRACE_1(TR_FAC_STREAMS_FR,
TR_STPDOWN, "stpdown:%p", stpdown);
rq = getendq(stp->sd_wrq);
if (cmd == I_PLINK)
rq = NULL;
linkp = alloclink(rq, stpdown->sd_wrq, fpdown);
strioc.ic_cmd = cmd;
strioc.ic_timout = INFTIM;
strioc.ic_len = sizeof (struct linkblk);
strioc.ic_dp = (char *)&linkp->li_lblk;
/*
* STRPLUMB protects plumbing changes and should be set before
* link_addpassthru()/link_rempassthru() are called, so it is set here
* and cleared in the end of mlink when passthru queue is removed.
* Setting of STRPLUMB prevents reopens of the stream while passthru
* queue is in-place (it is not a proper module and doesn't have open
* entry point).
*
* STPLEX prevents any threads from entering the stream from above. It
* can't be set before the call to link_addpassthru() because putnext
* from below may cause stream head I/O routines to be called and these
* routines assert that STPLEX is not set. After link_addpassthru()
* nothing may come from below since the pass queue syncq is blocked.
* Note also that STPLEX should be cleared before the call to
* link_rempassthru() since when messages start flowing to the stream
* head (e.g. because of message propagation from the pass queue) stream
* head I/O routines may be called with STPLEX flag set.
*
* When STPLEX is set, nothing may come into the stream from above and
* it is safe to do a setq which will change stream head. So, the
* correct sequence of actions is:
*
* 1) Set STRPLUMB
* 2) Call link_addpassthru()
* 3) Set STPLEX
* 4) Call setq and update the stream state
* 5) Clear STPLEX
* 6) Call link_rempassthru()
* 7) Clear STRPLUMB
*
* The same sequence applies to munlink() code.
*/
mutex_enter(&stpdown->sd_lock);
stpdown->sd_flag |= STRPLUMB;
mutex_exit(&stpdown->sd_lock);
/*
* Add passthru queue below lower mux. This will block
* syncqs of lower muxs read queue during I_LINK/I_UNLINK.
*/
passq = link_addpassthru(stpdown);
mutex_enter(&stpdown->sd_lock);
stpdown->sd_flag |= STPLEX;
mutex_exit(&stpdown->sd_lock);
rq = _RD(stpdown->sd_wrq);
/*
* There may be messages in the streamhead's syncq due to messages
* that arrived before link_addpassthru() was done. To avoid
* background processing of the syncq happening simultaneous with
* setq processing, we disable the streamhead syncq and wait until
* existing background thread finishes working on it.
*/
wait_sq_svc(rq->q_syncq);
passyncq = passq->q_syncq;
if (!(passyncq->sq_flags & SQ_BLOCKED))
blocksq(passyncq, SQ_BLOCKED, 0);
ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq));
rq->q_ptr = _WR(rq)->q_ptr = NULL;
/* setq might sleep in allocator - avoid holding locks. */
/* Note: we are holding muxifier here. */
str = stp->sd_strtab;
dp = &devimpl[getmajor(vp->v_rdev)];
ASSERT(dp->d_str == str);
qflag = dp->d_qflag;
sqtype = dp->d_sqtype;
/* create perdm_t if needed */
if (NEED_DM(dp->d_dmp, qflag))
dp->d_dmp = hold_dm(str, qflag, sqtype);
dmp = dp->d_dmp;
setq(rq, str->st_muxrinit, str->st_muxwinit, dmp, qflag, sqtype,
B_TRUE);
/*
* XXX Remove any "odd" messages from the queue.
* Keep only M_DATA, M_PROTO, M_PCPROTO.
*/
error = strdoioctl(stp, &strioc, FNATIVE,
K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp);
if (error != 0) {
lbfree(linkp);
if (!(passyncq->sq_flags & SQ_BLOCKED))
blocksq(passyncq, SQ_BLOCKED, 0);
/*
* Restore the stream head queue and then remove
* the passq. Turn off STPLEX before we turn on
* the stream by removing the passq.
*/
rq->q_ptr = _WR(rq)->q_ptr = stpdown;
setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO,
B_TRUE);
mutex_enter(&stpdown->sd_lock);
stpdown->sd_flag &= ~STPLEX;
mutex_exit(&stpdown->sd_lock);
link_rempassthru(passq);
mutex_enter(&stpdown->sd_lock);
stpdown->sd_flag &= ~STRPLUMB;
/* Wakeup anyone waiting for STRPLUMB to clear. */
cv_broadcast(&stpdown->sd_monitor);
mutex_exit(&stpdown->sd_lock);
mutex_exit(&muxifier);
netstack_rele(ss->ss_netstack);
return (error);
}
mutex_enter(&fpdown->f_tlock);
fpdown->f_count++;
mutex_exit(&fpdown->f_tlock);
/*
* if we've made it here the linkage is all set up so we should also
* set up the layered driver linkages
*/
ASSERT((cmd == I_LINK) || (cmd == I_PLINK));
if (cmd == I_LINK) {
ldi_mlink_fp(stp, fpdown, lhlink, LINKNORMAL);
} else {
ldi_mlink_fp(stp, fpdown, lhlink, LINKPERSIST);
}
link_rempassthru(passq);
mux_addedge(stp, stpdown, linkp->li_lblk.l_index, ss);
/*
* Mark the upper stream as having dependent links
* so that strclose can clean it up.
*/
if (cmd == I_LINK) {
mutex_enter(&stp->sd_lock);
stp->sd_flag |= STRHASLINKS;
mutex_exit(&stp->sd_lock);
}
/*
* Wake up any other processes that may have been
* waiting on the lower stream. These will all
* error out.
*/
mutex_enter(&stpdown->sd_lock);
/* The passthru module is removed so we may release STRPLUMB */
stpdown->sd_flag &= ~STRPLUMB;
cv_broadcast(&rq->q_wait);
cv_broadcast(&_WR(rq)->q_wait);
cv_broadcast(&stpdown->sd_monitor);
mutex_exit(&stpdown->sd_lock);
mutex_exit(&muxifier);
*rvalp = linkp->li_lblk.l_index;
netstack_rele(ss->ss_netstack);
return (0);
}
int
mlink(vnode_t *vp, int cmd, int arg, cred_t *crp, int *rvalp, int lhlink)
{
int ret;
struct file *fpdown;
fpdown = getf(arg);
ret = mlink_file(vp, cmd, fpdown, crp, rvalp, lhlink);
if (fpdown != NULL)
releasef(arg);
return (ret);
}
/*
* Unlink a multiplexor link. Stp is the controlling stream for the
* link, and linkp points to the link's entry in the linkinfo list.
* The muxifier lock must be held on entry and is dropped on exit.
*
* NOTE : Currently it is assumed that mux would process all the messages
* sitting on it's queue before ACKing the UNLINK. It is the responsibility
* of the mux to handle all the messages that arrive before UNLINK.
* If the mux has to send down messages on its lower stream before
* ACKing I_UNLINK, then it *should* know to handle messages even
* after the UNLINK is acked (actually it should be able to handle till we
* re-block the read side of the pass queue here). If the mux does not
* open up the lower stream, any messages that arrive during UNLINK
* will be put in the stream head. In the case of lower stream opening
* up, some messages might land in the stream head depending on when
* the message arrived and when the read side of the pass queue was
* re-blocked.
*/
int
munlink(stdata_t *stp, linkinfo_t *linkp, int flag, cred_t *crp, int *rvalp,
str_stack_t *ss)
{
struct strioctl strioc;
struct stdata *stpdown;
queue_t *rq, *wrq;
queue_t *passq;
syncq_t *passyncq;
int error = 0;
file_t *fpdown;
ASSERT(MUTEX_HELD(&muxifier));
stpdown = linkp->li_fpdown->f_vnode->v_stream;
/*
* See the comment in mlink() concerning STRPLUMB/STPLEX flags.
*/
mutex_enter(&stpdown->sd_lock);
stpdown->sd_flag |= STRPLUMB;
mutex_exit(&stpdown->sd_lock);
/*
* Add passthru queue below lower mux. This will block
* syncqs of lower muxs read queue during I_LINK/I_UNLINK.
*/
passq = link_addpassthru(stpdown);
if ((flag & LINKTYPEMASK) == LINKNORMAL)
strioc.ic_cmd = I_UNLINK;
else
strioc.ic_cmd = I_PUNLINK;
strioc.ic_timout = INFTIM;
strioc.ic_len = sizeof (struct linkblk);
strioc.ic_dp = (char *)&linkp->li_lblk;
error = strdoioctl(stp, &strioc, FNATIVE,
K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp);
/*
* If there was an error and this is not called via strclose,
* return to the user. Otherwise, pretend there was no error
* and close the link.
*/
if (error) {
if (flag & LINKCLOSE) {
cmn_err(CE_WARN, "KERNEL: munlink: could not perform "
"unlink ioctl, closing anyway (%d)\n", error);
} else {
link_rempassthru(passq);
mutex_enter(&stpdown->sd_lock);
stpdown->sd_flag &= ~STRPLUMB;
cv_broadcast(&stpdown->sd_monitor);
mutex_exit(&stpdown->sd_lock);
mutex_exit(&muxifier);
return (error);
}
}
mux_rmvedge(stp, linkp->li_lblk.l_index, ss);
fpdown = linkp->li_fpdown;
lbfree(linkp);
/*
* We go ahead and drop muxifier here--it's a nasty global lock that
* can slow others down. It's okay to since attempts to mlink() this
* stream will be stopped because STPLEX is still set in the stdata
* structure, and munlink() is stopped because mux_rmvedge() and
* lbfree() have removed it from mux_nodes[] and linkinfo_list,
* respectively. Note that we defer the closef() of fpdown until
* after we drop muxifier since strclose() can call munlinkall().
*/
mutex_exit(&muxifier);
wrq = stpdown->sd_wrq;
rq = _RD(wrq);
/*
* Get rid of outstanding service procedure runs, before we make
* it a stream head, since a stream head doesn't have any service
* procedure.
*/
disable_svc(rq);
wait_svc(rq);
/*
* Since we don't disable the syncq for QPERMOD, we wait for whatever
* is queued up to be finished. mux should take care that nothing is
* send down to this queue. We should do it now as we're going to block
* passyncq if it was unblocked.
*/
if (wrq->q_flag & QPERMOD) {
syncq_t *sq = wrq->q_syncq;
mutex_enter(SQLOCK(sq));
while (wrq->q_sqflags & Q_SQQUEUED) {
sq->sq_flags |= SQ_WANTWAKEUP;
cv_wait(&sq->sq_wait, SQLOCK(sq));
}
mutex_exit(SQLOCK(sq));
}
passyncq = passq->q_syncq;
if (!(passyncq->sq_flags & SQ_BLOCKED)) {
syncq_t *sq, *outer;
/*
* Messages could be flowing from underneath. We will
* block the read side of the passq. This would be
* sufficient for QPAIR and QPERQ muxes to ensure
* that no data is flowing up into this queue
* and hence no thread active in this instance of
* lower mux. But for QPERMOD and QMTOUTPERIM there
* could be messages on the inner and outer/inner
* syncqs respectively. We will wait for them to drain.
* Because passq is blocked messages end up in the syncq
* And qfill_syncq could possibly end up setting QFULL
* which will access the rq->q_flag. Hence, we have to
* acquire the QLOCK in setq.
*
* XXX Messages can also flow from top into this
* queue though the unlink is over (Ex. some instance
* in putnext() called from top that has still not
* accessed this queue. And also putq(lowerq) ?).
* Solution : How about blocking the l_qtop queue ?
* Do we really care about such pure D_MP muxes ?
*/
blocksq(passyncq, SQ_BLOCKED, 0);
sq = rq->q_syncq;
if ((outer = sq->sq_outer) != NULL) {
/*
* We have to just wait for the outer sq_count
* drop to zero. As this does not prevent new
* messages to enter the outer perimeter, this
* is subject to starvation.
*
* NOTE :Because of blocksq above, messages could
* be in the inner syncq only because of some
* thread holding the outer perimeter exclusively.
* Hence it would be sufficient to wait for the
* exclusive holder of the outer perimeter to drain
* the inner and outer syncqs. But we will not depend
* on this feature and hence check the inner syncqs
* separately.
*/
wait_syncq(outer);
}
/*
* There could be messages destined for
* this queue. Let the exclusive holder
* drain it.
*/
wait_syncq(sq);
ASSERT((rq->q_flag & QPERMOD) ||
((rq->q_syncq->sq_head == NULL) &&
(_WR(rq)->q_syncq->sq_head == NULL)));
}
/*
* We haven't taken care of QPERMOD case yet. QPERMOD is a special
* case as we don't disable its syncq or remove it off the syncq
* service list.
*/
if (rq->q_flag & QPERMOD) {
syncq_t *sq = rq->q_syncq;
mutex_enter(SQLOCK(sq));
while (rq->q_sqflags & Q_SQQUEUED) {
sq->sq_flags |= SQ_WANTWAKEUP;
cv_wait(&sq->sq_wait, SQLOCK(sq));
}
mutex_exit(SQLOCK(sq));
}
/*
* flush_syncq changes states only when there are some messages to
* free, i.e. when it returns non-zero value to return.
*/
ASSERT(flush_syncq(rq->q_syncq, rq) == 0);
ASSERT(flush_syncq(wrq->q_syncq, wrq) == 0);
/*
* Nobody else should know about this queue now.
* If the mux did not process the messages before
* acking the I_UNLINK, free them now.
*/
flushq(rq, FLUSHALL);
flushq(_WR(rq), FLUSHALL);
/*
* Convert the mux lower queue into a stream head queue.
* Turn off STPLEX before we turn on the stream by removing the passq.
*/
rq->q_ptr = wrq->q_ptr = stpdown;
setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_TRUE);
ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq));
enable_svc(rq);
/*
* Now it is a proper stream, so STPLEX is cleared. But STRPLUMB still
* needs to be set to prevent reopen() of the stream - such reopen may
* try to call non-existent pass queue open routine and panic.
*/
mutex_enter(&stpdown->sd_lock);
stpdown->sd_flag &= ~STPLEX;
mutex_exit(&stpdown->sd_lock);
ASSERT(((flag & LINKTYPEMASK) == LINKNORMAL) ||
((flag & LINKTYPEMASK) == LINKPERSIST));
/* clean up the layered driver linkages */
if ((flag & LINKTYPEMASK) == LINKNORMAL) {
ldi_munlink_fp(stp, fpdown, LINKNORMAL);
} else {
ldi_munlink_fp(stp, fpdown, LINKPERSIST);
}
link_rempassthru(passq);
/*
* Now all plumbing changes are finished and STRPLUMB is no
* longer needed.
*/
mutex_enter(&stpdown->sd_lock);
stpdown->sd_flag &= ~STRPLUMB;
cv_broadcast(&stpdown->sd_monitor);
mutex_exit(&stpdown->sd_lock);
(void) closef(fpdown);
return (0);
}
/*
* Unlink all multiplexor links for which stp is the controlling stream.
* Return 0, or a non-zero errno on failure.
*/
int
munlinkall(stdata_t *stp, int flag, cred_t *crp, int *rvalp, str_stack_t *ss)
{
linkinfo_t *linkp;
int error = 0;
mutex_enter(&muxifier);
while (linkp = findlinks(stp, 0, flag, ss)) {
/*
* munlink() releases the muxifier lock.
*/
if (error = munlink(stp, linkp, flag, crp, rvalp, ss))
return (error);
mutex_enter(&muxifier);
}
mutex_exit(&muxifier);
return (0);
}
/*
* A multiplexor link has been made. Add an
* edge to the directed graph.
*/
void
mux_addedge(stdata_t *upstp, stdata_t *lostp, int muxid, str_stack_t *ss)
{
struct mux_node *np;
struct mux_edge *ep;
major_t upmaj;
major_t lomaj;
upmaj = getmajor(upstp->sd_vnode->v_rdev);
lomaj = getmajor(lostp->sd_vnode->v_rdev);
np = &ss->ss_mux_nodes[upmaj];
if (np->mn_outp) {
ep = np->mn_outp;
while (ep->me_nextp)
ep = ep->me_nextp;
ep->me_nextp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP);
ep = ep->me_nextp;
} else {
np->mn_outp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP);
ep = np->mn_outp;
}
ep->me_nextp = NULL;
ep->me_muxid = muxid;
/*
* Save the dev_t for the purposes of str_stack_shutdown.
* str_stack_shutdown assumes that the device allows reopen, since
* this dev_t is the one after any cloning by xx_open().
* Would prefer finding the dev_t from before any cloning,
* but specfs doesn't retain that.
*/
ep->me_dev = upstp->sd_vnode->v_rdev;
if (lostp->sd_vnode->v_type == VFIFO)
ep->me_nodep = NULL;
else
ep->me_nodep = &ss->ss_mux_nodes[lomaj];
}
/*
* A multiplexor link has been removed. Remove the
* edge in the directed graph.
*/
void
mux_rmvedge(stdata_t *upstp, int muxid, str_stack_t *ss)
{
struct mux_node *np;
struct mux_edge *ep;
struct mux_edge *pep = NULL;
major_t upmaj;
upmaj = getmajor(upstp->sd_vnode->v_rdev);
np = &ss->ss_mux_nodes[upmaj];
ASSERT(np->mn_outp != NULL);
ep = np->mn_outp;
while (ep) {
if (ep->me_muxid == muxid) {
if (pep)
pep->me_nextp = ep->me_nextp;
else
np->mn_outp = ep->me_nextp;
kmem_free(ep, sizeof (struct mux_edge));
return;
}
pep = ep;
ep = ep->me_nextp;
}
ASSERT(0); /* should not reach here */
}
/*
* Translate the device flags (from conf.h) to the corresponding
* qflag and sq_flag (type) values.
*/
int
devflg_to_qflag(struct streamtab *stp, uint32_t devflag, uint32_t *qflagp,
uint32_t *sqtypep)
{
uint32_t qflag = 0;
uint32_t sqtype = 0;
if (devflag & _D_OLD)
goto bad;
/* Inner perimeter presence and scope */
switch (devflag & D_MTINNER_MASK) {
case D_MP:
qflag |= QMTSAFE;
sqtype |= SQ_CI;
break;
case D_MTPERQ|D_MP:
qflag |= QPERQ;
break;
case D_MTQPAIR|D_MP:
qflag |= QPAIR;
break;
case D_MTPERMOD|D_MP:
qflag |= QPERMOD;
break;
default:
goto bad;
}
/* Outer perimeter */
if (devflag & D_MTOUTPERIM) {
switch (devflag & D_MTINNER_MASK) {
case D_MP:
case D_MTPERQ|D_MP:
case D_MTQPAIR|D_MP:
break;
default:
goto bad;
}
qflag |= QMTOUTPERIM;
}
/* Inner perimeter modifiers */
if (devflag & D_MTINNER_MOD) {
switch (devflag & D_MTINNER_MASK) {
case D_MP:
goto bad;
default:
break;
}
if (devflag & D_MTPUTSHARED)
sqtype |= SQ_CIPUT;
if (devflag & _D_MTOCSHARED) {
/*
* The code in putnext assumes that it has the
* highest concurrency by not checking sq_count.
* Thus _D_MTOCSHARED can only be supported when
* D_MTPUTSHARED is set.
*/
if (!(devflag & D_MTPUTSHARED))
goto bad;
sqtype |= SQ_CIOC;
}
if (devflag & _D_MTCBSHARED) {
/*
* The code in putnext assumes that it has the
* highest concurrency by not checking sq_count.
* Thus _D_MTCBSHARED can only be supported when
* D_MTPUTSHARED is set.
*/
if (!(devflag & D_MTPUTSHARED))
goto bad;
sqtype |= SQ_CICB;
}
if (devflag & _D_MTSVCSHARED) {
/*
* The code in putnext assumes that it has the
* highest concurrency by not checking sq_count.
* Thus _D_MTSVCSHARED can only be supported when
* D_MTPUTSHARED is set. Also _D_MTSVCSHARED is
* supported only for QPERMOD.
*/
if (!(devflag & D_MTPUTSHARED) || !(qflag & QPERMOD))
goto bad;
sqtype |= SQ_CISVC;
}
}
/* Default outer perimeter concurrency */
sqtype |= SQ_CO;
/* Outer perimeter modifiers */
if (devflag & D_MTOCEXCL) {
if (!(devflag & D_MTOUTPERIM)) {
/* No outer perimeter */
goto bad;
}
sqtype &= ~SQ_COOC;
}
/* Synchronous Streams extended qinit structure */
if (devflag & D_SYNCSTR)
qflag |= QSYNCSTR;
/*
* Private flag used by a transport module to indicate
* to sockfs that it supports direct-access mode without
* having to go through STREAMS.
*/
if (devflag & _D_DIRECT) {
/* Reject unless the module is fully-MT (no perimeter) */
if ((qflag & QMT_TYPEMASK) != QMTSAFE)
goto bad;
qflag |= _QDIRECT;
}
*qflagp = qflag;
*sqtypep = sqtype;
return (0);
bad:
cmn_err(CE_WARN,
"stropen: bad MT flags (0x%x) in driver '%s'",
(int)(qflag & D_MTSAFETY_MASK),
stp->st_rdinit->qi_minfo->mi_idname);
return (EINVAL);
}
/*
* Set the interface values for a pair of queues (qinit structure,
* packet sizes, water marks).
* setq assumes that the caller does not have a claim (entersq or claimq)
* on the queue.
*/
void
setq(queue_t *rq, struct qinit *rinit, struct qinit *winit,
perdm_t *dmp, uint32_t qflag, uint32_t sqtype, boolean_t lock_needed)
{
queue_t *wq;
syncq_t *sq, *outer;
ASSERT(rq->q_flag & QREADR);
ASSERT((qflag & QMT_TYPEMASK) != 0);
IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL);
wq = _WR(rq);
rq->q_qinfo = rinit;
rq->q_hiwat = rinit->qi_minfo->mi_hiwat;
rq->q_lowat = rinit->qi_minfo->mi_lowat;
rq->q_minpsz = rinit->qi_minfo->mi_minpsz;
rq->q_maxpsz = rinit->qi_minfo->mi_maxpsz;
wq->q_qinfo = winit;
wq->q_hiwat = winit->qi_minfo->mi_hiwat;
wq->q_lowat = winit->qi_minfo->mi_lowat;
wq->q_minpsz = winit->qi_minfo->mi_minpsz;
wq->q_maxpsz = winit->qi_minfo->mi_maxpsz;
/* Remove old syncqs */
sq = rq->q_syncq;
outer = sq->sq_outer;
if (outer != NULL) {
ASSERT(wq->q_syncq->sq_outer == outer);
outer_remove(outer, rq->q_syncq);
if (wq->q_syncq != rq->q_syncq)
outer_remove(outer, wq->q_syncq);
}
ASSERT(sq->sq_outer == NULL);
ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
if (sq != SQ(rq)) {
if (!(rq->q_flag & QPERMOD))
free_syncq(sq);
if (wq->q_syncq == rq->q_syncq)
wq->q_syncq = NULL;
rq->q_syncq = NULL;
}
if (wq->q_syncq != NULL && wq->q_syncq != sq &&
wq->q_syncq != SQ(rq)) {
free_syncq(wq->q_syncq);
wq->q_syncq = NULL;
}
ASSERT(rq->q_syncq == NULL || (rq->q_syncq->sq_head == NULL &&
rq->q_syncq->sq_tail == NULL));
ASSERT(wq->q_syncq == NULL || (wq->q_syncq->sq_head == NULL &&
wq->q_syncq->sq_tail == NULL));
if (!(rq->q_flag & QPERMOD) &&
rq->q_syncq != NULL && rq->q_syncq->sq_ciputctrl != NULL) {
ASSERT(rq->q_syncq->sq_nciputctrl == n_ciputctrl - 1);
SUMCHECK_CIPUTCTRL_COUNTS(rq->q_syncq->sq_ciputctrl,
rq->q_syncq->sq_nciputctrl, 0);
ASSERT(ciputctrl_cache != NULL);
kmem_cache_free(ciputctrl_cache, rq->q_syncq->sq_ciputctrl);
rq->q_syncq->sq_ciputctrl = NULL;
rq->q_syncq->sq_nciputctrl = 0;
}
if (!(wq->q_flag & QPERMOD) &&
wq->q_syncq != NULL && wq->q_syncq->sq_ciputctrl != NULL) {
ASSERT(wq->q_syncq->sq_nciputctrl == n_ciputctrl - 1);
SUMCHECK_CIPUTCTRL_COUNTS(wq->q_syncq->sq_ciputctrl,
wq->q_syncq->sq_nciputctrl, 0);
ASSERT(ciputctrl_cache != NULL);
kmem_cache_free(ciputctrl_cache, wq->q_syncq->sq_ciputctrl);
wq->q_syncq->sq_ciputctrl = NULL;
wq->q_syncq->sq_nciputctrl = 0;
}
sq = SQ(rq);
ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL);
ASSERT(sq->sq_outer == NULL);
ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
/*
* Create syncqs based on qflag and sqtype. Set the SQ_TYPES_IN_FLAGS
* bits in sq_flag based on the sqtype.
*/
ASSERT((sq->sq_flags & ~SQ_TYPES_IN_FLAGS) == 0);
rq->q_syncq = wq->q_syncq = sq;
sq->sq_type = sqtype;
sq->sq_flags = (sqtype & SQ_TYPES_IN_FLAGS);
/*
* We are making sq_svcflags zero,
* resetting SQ_DISABLED in case it was set by
* wait_svc() in the munlink path.
*
*/
ASSERT((sq->sq_svcflags & SQ_SERVICE) == 0);
sq->sq_svcflags = 0;
/*
* We need to acquire the lock here for the mlink and munlink case,
* where canputnext, backenable, etc can access the q_flag.
*/
if (lock_needed) {
mutex_enter(QLOCK(rq));
rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
mutex_exit(QLOCK(rq));
mutex_enter(QLOCK(wq));
wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
mutex_exit(QLOCK(wq));
} else {
rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
}
if (qflag & QPERQ) {
/* Allocate a separate syncq for the write side */
sq = new_syncq();
sq->sq_type = rq->q_syncq->sq_type;
sq->sq_flags = rq->q_syncq->sq_flags;
ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
sq->sq_oprev == NULL);
wq->q_syncq = sq;
}
if (qflag & QPERMOD) {
sq = dmp->dm_sq;
/*
* Assert that we do have an inner perimeter syncq and that it
* does not have an outer perimeter associated with it.
*/
ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
sq->sq_oprev == NULL);
rq->q_syncq = wq->q_syncq = sq;
}
if (qflag & QMTOUTPERIM) {
outer = dmp->dm_sq;
ASSERT(outer->sq_outer == NULL);
outer_insert(outer, rq->q_syncq);
if (wq->q_syncq != rq->q_syncq)
outer_insert(outer, wq->q_syncq);
}
ASSERT((rq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) ==
(rq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS));
ASSERT((wq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) ==
(wq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS));
ASSERT((rq->q_flag & QMT_TYPEMASK) == (qflag & QMT_TYPEMASK));
/*
* Initialize struio() types.
*/
rq->q_struiot =
(rq->q_flag & QSYNCSTR) ? rinit->qi_struiot : STRUIOT_NONE;
wq->q_struiot =
(wq->q_flag & QSYNCSTR) ? winit->qi_struiot : STRUIOT_NONE;
}
perdm_t *
hold_dm(struct streamtab *str, uint32_t qflag, uint32_t sqtype)
{
syncq_t *sq;
perdm_t **pp;
perdm_t *p;
perdm_t *dmp;
ASSERT(str != NULL);
ASSERT(qflag & (QPERMOD | QMTOUTPERIM));
rw_enter(&perdm_rwlock, RW_READER);
for (p = perdm_list; p != NULL; p = p->dm_next) {
if (p->dm_str == str) { /* found one */
atomic_add_32(&(p->dm_ref), 1);
rw_exit(&perdm_rwlock);
return (p);
}
}
rw_exit(&perdm_rwlock);
sq = new_syncq();
if (qflag & QPERMOD) {
sq->sq_type = sqtype | SQ_PERMOD;
sq->sq_flags = sqtype & SQ_TYPES_IN_FLAGS;
} else {
ASSERT(qflag & QMTOUTPERIM);
sq->sq_onext = sq->sq_oprev = sq;
}
dmp = kmem_alloc(sizeof (perdm_t), KM_SLEEP);
dmp->dm_sq = sq;
dmp->dm_str = str;
dmp->dm_ref = 1;
dmp->dm_next = NULL;
rw_enter(&perdm_rwlock, RW_WRITER);
for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next)) {
if (p->dm_str == str) { /* already present */
p->dm_ref++;
rw_exit(&perdm_rwlock);
free_syncq(sq);
kmem_free(dmp, sizeof (perdm_t));
return (p);
}
}
*pp = dmp;
rw_exit(&perdm_rwlock);
return (dmp);
}
void
rele_dm(perdm_t *dmp)
{
perdm_t **pp;
perdm_t *p;
rw_enter(&perdm_rwlock, RW_WRITER);
ASSERT(dmp->dm_ref > 0);
if (--dmp->dm_ref > 0) {
rw_exit(&perdm_rwlock);
return;
}
for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next))
if (p == dmp)
break;
ASSERT(p == dmp);
*pp = p->dm_next;
rw_exit(&perdm_rwlock);
/*
* Wait for any background processing that relies on the
* syncq to complete before it is freed.
*/
wait_sq_svc(p->dm_sq);
free_syncq(p->dm_sq);
kmem_free(p, sizeof (perdm_t));
}
/*
* Make a protocol message given control and data buffers.
* n.b., this can block; be careful of what locks you hold when calling it.
*
* If sd_maxblk is less than *iosize this routine can fail part way through
* (due to an allocation failure). In this case on return *iosize will contain
* the amount that was consumed. Otherwise *iosize will not be modified
* i.e. it will contain the amount that was consumed.
*/
int
strmakemsg(
struct strbuf *mctl,
ssize_t *iosize,
struct uio *uiop,
stdata_t *stp,
int32_t flag,
mblk_t **mpp)
{
mblk_t *mpctl = NULL;
mblk_t *mpdata = NULL;
int error;
ASSERT(uiop != NULL);
*mpp = NULL;
/* Create control part, if any */
if ((mctl != NULL) && (mctl->len >= 0)) {
error = strmakectl(mctl, flag, uiop->uio_fmode, &mpctl);
if (error)
return (error);
}
/* Create data part, if any */
if (*iosize >= 0) {
error = strmakedata(iosize, uiop, stp, flag, &mpdata);
if (error) {
freemsg(mpctl);
return (error);
}
}
if (mpctl != NULL) {
if (mpdata != NULL)
linkb(mpctl, mpdata);
*mpp = mpctl;
} else {
*mpp = mpdata;
}
return (0);
}
/*
* Make the control part of a protocol message given a control buffer.
* n.b., this can block; be careful of what locks you hold when calling it.
*/
int
strmakectl(
struct strbuf *mctl,
int32_t flag,
int32_t fflag,
mblk_t **mpp)
{
mblk_t *bp = NULL;
unsigned char msgtype;
int error = 0;
cred_t *cr = CRED();
/* We do not support interrupt threads using the stream head to send */
ASSERT(cr != NULL);
*mpp = NULL;
/*
* Create control part of message, if any.
*/
if ((mctl != NULL) && (mctl->len >= 0)) {
caddr_t base;
int ctlcount;
int allocsz;
if (flag & RS_HIPRI)
msgtype = M_PCPROTO;
else
msgtype = M_PROTO;
ctlcount = mctl->len;
base = mctl->buf;
/*
* Give modules a better chance to reuse M_PROTO/M_PCPROTO
* blocks by increasing the size to something more usable.
*/
allocsz = MAX(ctlcount, 64);
/*
* Range checking has already been done; simply try
* to allocate a message block for the ctl part.
*/
while ((bp = allocb_cred(allocsz, cr,
curproc->p_pid)) == NULL) {
if (fflag & (FNDELAY|FNONBLOCK))
return (EAGAIN);
if (error = strwaitbuf(allocsz, BPRI_MED))
return (error);
}
bp->b_datap->db_type = msgtype;
if (copyin(base, bp->b_wptr, ctlcount)) {
freeb(bp);
return (EFAULT);
}
bp->b_wptr += ctlcount;
}
*mpp = bp;
return (0);
}
/*
* Make a protocol message given data buffers.
* n.b., this can block; be careful of what locks you hold when calling it.
*
* If sd_maxblk is less than *iosize this routine can fail part way through
* (due to an allocation failure). In this case on return *iosize will contain
* the amount that was consumed. Otherwise *iosize will not be modified
* i.e. it will contain the amount that was consumed.
*/
int
strmakedata(
ssize_t *iosize,
struct uio *uiop,
stdata_t *stp,
int32_t flag,
mblk_t **mpp)
{
mblk_t *mp = NULL;
mblk_t *bp;
int wroff = (int)stp->sd_wroff;
int tail_len = (int)stp->sd_tail;
int extra = wroff + tail_len;
int error = 0;
ssize_t maxblk;
ssize_t count = *iosize;
cred_t *cr;
*mpp = NULL;
if (count < 0)
return (0);
/* We do not support interrupt threads using the stream head to send */
cr = CRED();
ASSERT(cr != NULL);
maxblk = stp->sd_maxblk;
if (maxblk == INFPSZ)
maxblk = count;
/*
* Create data part of message, if any.
*/
do {
ssize_t size;
dblk_t *dp;
ASSERT(uiop);
size = MIN(count, maxblk);
while ((bp = allocb_cred(size + extra, cr,
curproc->p_pid)) == NULL) {
error = EAGAIN;
if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
(error = strwaitbuf(size + extra, BPRI_MED)) != 0) {
if (count == *iosize) {
freemsg(mp);
return (error);
} else {
*iosize -= count;
*mpp = mp;
return (0);
}
}
}
dp = bp->b_datap;
dp->db_cpid = curproc->p_pid;
ASSERT(wroff <= dp->db_lim - bp->b_wptr);
bp->b_wptr = bp->b_rptr = bp->b_rptr + wroff;
if (flag & STRUIO_POSTPONE) {
/*
* Setup the stream uio portion of the
* dblk for subsequent use by struioget().
*/
dp->db_struioflag = STRUIO_SPEC;
dp->db_cksumstart = 0;
dp->db_cksumstuff = 0;
dp->db_cksumend = size;
*(long long *)dp->db_struioun.data = 0ll;
bp->b_wptr += size;
} else {
if (stp->sd_copyflag & STRCOPYCACHED)
uiop->uio_extflg |= UIO_COPY_CACHED;
if (size != 0) {
error = uiomove(bp->b_wptr, size, UIO_WRITE,
uiop);
if (error != 0) {
freeb(bp);
freemsg(mp);
return (error);
}
}
bp->b_wptr += size;
if (stp->sd_wputdatafunc != NULL) {
mblk_t *newbp;
newbp = (stp->sd_wputdatafunc)(stp->sd_vnode,
bp, NULL, NULL, NULL, NULL);
if (newbp == NULL) {
freeb(bp);
freemsg(mp);
return (ECOMM);
}
bp = newbp;
}
}
count -= size;
if (mp == NULL)
mp = bp;
else
linkb(mp, bp);
} while (count > 0);
*mpp = mp;
return (0);
}
/*
* Wait for a buffer to become available. Return non-zero errno
* if not able to wait, 0 if buffer is probably there.
*/
int
strwaitbuf(size_t size, int pri)
{
bufcall_id_t id;
mutex_enter(&bcall_monitor);
if ((id = bufcall(size, pri, (void (*)(void *))cv_broadcast,
&ttoproc(curthread)->p_flag_cv)) == 0) {
mutex_exit(&bcall_monitor);
return (ENOSR);
}
if (!cv_wait_sig(&(ttoproc(curthread)->p_flag_cv), &bcall_monitor)) {
unbufcall(id);
mutex_exit(&bcall_monitor);
return (EINTR);
}
unbufcall(id);
mutex_exit(&bcall_monitor);
return (0);
}
/*
* This function waits for a read or write event to happen on a stream.
* fmode can specify FNDELAY and/or FNONBLOCK.
* The timeout is in ms with -1 meaning infinite.
* The flag values work as follows:
* READWAIT Check for read side errors, send M_READ
* GETWAIT Check for read side errors, no M_READ
* WRITEWAIT Check for write side errors.
* NOINTR Do not return error if nonblocking or timeout.
* STR_NOERROR Ignore all errors except STPLEX.
* STR_NOSIG Ignore/hold signals during the duration of the call.
* STR_PEEK Pass through the strgeterr().
*/
int
strwaitq(stdata_t *stp, int flag, ssize_t count, int fmode, clock_t timout,
int *done)
{
int slpflg, errs;
int error;
kcondvar_t *sleepon;
mblk_t *mp;
ssize_t *rd_count;
clock_t rval;
ASSERT(MUTEX_HELD(&stp->sd_lock));
if ((flag & READWAIT) || (flag & GETWAIT)) {
slpflg = RSLEEP;
sleepon = &_RD(stp->sd_wrq)->q_wait;
errs = STRDERR|STPLEX;
} else {
slpflg = WSLEEP;
sleepon = &stp->sd_wrq->q_wait;
errs = STWRERR|STRHUP|STPLEX;
}
if (flag & STR_NOERROR)
errs = STPLEX;
if (stp->sd_wakeq & slpflg) {
/*
* A strwakeq() is pending, no need to sleep.
*/
stp->sd_wakeq &= ~slpflg;
*done = 0;
return (0);
}
if (stp->sd_flag & errs) {
/*
* Check for errors before going to sleep since the
* caller might not have checked this while holding
* sd_lock.
*/
error = strgeterr(stp, errs, (flag & STR_PEEK));
if (error != 0) {
*done = 1;
return (error);
}
}
/*
* If any module downstream has requested read notification
* by setting SNDMREAD flag using M_SETOPTS, send a message
* down stream.
*/
if ((flag & READWAIT) && (stp->sd_flag & SNDMREAD)) {
mutex_exit(&stp->sd_lock);
if (!(mp = allocb_wait(sizeof (ssize_t), BPRI_MED,
(flag & STR_NOSIG), &error))) {
mutex_enter(&stp->sd_lock);
*done = 1;
return (error);
}
mp->b_datap->db_type = M_READ;
rd_count = (ssize_t *)mp->b_wptr;
*rd_count = count;
mp->b_wptr += sizeof (ssize_t);
/*
* Send the number of bytes requested by the
* read as the argument to M_READ.
*/
stream_willservice(stp);
putnext(stp->sd_wrq, mp);
stream_runservice(stp);
mutex_enter(&stp->sd_lock);
/*
* If any data arrived due to inline processing
* of putnext(), don't sleep.
*/
if (_RD(stp->sd_wrq)->q_first != NULL) {
*done = 0;
return (0);
}
}
if (fmode & (FNDELAY|FNONBLOCK)) {
if (!(flag & NOINTR))
error = EAGAIN;
else
error = 0;
*done = 1;
return (error);
}
stp->sd_flag |= slpflg;
TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAIT2,
"strwaitq sleeps (2):%p, %X, %lX, %X, %p",
stp, flag, count, fmode, done);
rval = str_cv_wait(sleepon, &stp->sd_lock, timout, flag & STR_NOSIG);
if (rval > 0) {
/* EMPTY */
TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAKE2,
"strwaitq awakes(2):%X, %X, %X, %X, %X",
stp, flag, count, fmode, done);
} else if (rval == 0) {
TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_INTR2,
"strwaitq interrupt #2:%p, %X, %lX, %X, %p",
stp, flag, count, fmode, done);
stp->sd_flag &= ~slpflg;
cv_broadcast(sleepon);
if (!(flag & NOINTR))
error = EINTR;
else
error = 0;
*done = 1;
return (error);
} else {
/* timeout */
TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_TIME,
"strwaitq timeout:%p, %X, %lX, %X, %p",
stp, flag, count, fmode, done);
*done = 1;
if (!(flag & NOINTR))
return (ETIME);
else
return (0);
}
/*
* If the caller implements delayed errors (i.e. queued after data)
* we can not check for errors here since data as well as an
* error might have arrived at the stream head. We return to
* have the caller check the read queue before checking for errors.
*/
if ((stp->sd_flag & errs) && !(flag & STR_DELAYERR)) {
error = strgeterr(stp, errs, (flag & STR_PEEK));
if (error != 0) {
*done = 1;
return (error);
}
}
*done = 0;
return (0);
}
/*
* Perform job control discipline access checks.
* Return 0 for success and the errno for failure.
*/
#define cantsend(p, t, sig) \
(sigismember(&(p)->p_ignore, sig) || signal_is_blocked((t), sig))
int
straccess(struct stdata *stp, enum jcaccess mode)
{
extern kcondvar_t lbolt_cv; /* XXX: should be in a header file */
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
sess_t *sp;
ASSERT(mutex_owned(&stp->sd_lock));
if (stp->sd_sidp == NULL || stp->sd_vnode->v_type == VFIFO)
return (0);
mutex_enter(&p->p_lock); /* protects p_pgidp */
for (;;) {
mutex_enter(&p->p_splock); /* protects p->p_sessp */
sp = p->p_sessp;
mutex_enter(&sp->s_lock); /* protects sp->* */
/*
* If this is not the calling process's controlling terminal
* or if the calling process is already in the foreground
* then allow access.
*/
if (sp->s_dev != stp->sd_vnode->v_rdev ||
p->p_pgidp == stp->sd_pgidp) {
mutex_exit(&sp->s_lock);
mutex_exit(&p->p_splock);
mutex_exit(&p->p_lock);
return (0);
}
/*
* Check to see if controlling terminal has been deallocated.
*/
if (sp->s_vp == NULL) {
if (!cantsend(p, t, SIGHUP))
sigtoproc(p, t, SIGHUP);
mutex_exit(&sp->s_lock);
mutex_exit(&p->p_splock);
mutex_exit(&p->p_lock);
return (EIO);
}
mutex_exit(&sp->s_lock);
mutex_exit(&p->p_splock);
if (mode == JCGETP) {
mutex_exit(&p->p_lock);
return (0);
}
if (mode == JCREAD) {
if (p->p_detached || cantsend(p, t, SIGTTIN)) {
mutex_exit(&p->p_lock);
return (EIO);
}
mutex_exit(&p->p_lock);
mutex_exit(&stp->sd_lock);
pgsignal(p->p_pgidp, SIGTTIN);
mutex_enter(&stp->sd_lock);
mutex_enter(&p->p_lock);
} else { /* mode == JCWRITE or JCSETP */
if ((mode == JCWRITE && !(stp->sd_flag & STRTOSTOP)) ||
cantsend(p, t, SIGTTOU)) {
mutex_exit(&p->p_lock);
return (0);
}
if (p->p_detached) {
mutex_exit(&p->p_lock);
return (EIO);
}
mutex_exit(&p->p_lock);
mutex_exit(&stp->sd_lock);
pgsignal(p->p_pgidp, SIGTTOU);
mutex_enter(&stp->sd_lock);
mutex_enter(&p->p_lock);
}
/*
* We call cv_wait_sig_swap() to cause the appropriate
* action for the jobcontrol signal to take place.
* If the signal is being caught, we will take the
* EINTR error return. Otherwise, the default action
* of causing the process to stop will take place.
* In this case, we rely on the periodic cv_broadcast() on
* &lbolt_cv to wake us up to loop around and test again.
* We can't get here if the signal is ignored or
* if the current thread is blocking the signal.
*/
mutex_exit(&stp->sd_lock);
if (!cv_wait_sig_swap(&lbolt_cv, &p->p_lock)) {
mutex_exit(&p->p_lock);
mutex_enter(&stp->sd_lock);
return (EINTR);
}
mutex_exit(&p->p_lock);
mutex_enter(&stp->sd_lock);
mutex_enter(&p->p_lock);
}
}
/*
* Return size of message of block type (bp->b_datap->db_type)
*/
size_t
xmsgsize(mblk_t *bp)
{
unsigned char type;
size_t count = 0;
type = bp->b_datap->db_type;
for (; bp; bp = bp->b_cont) {
if (type != bp->b_datap->db_type)
break;
ASSERT(bp->b_wptr >= bp->b_rptr);
count += bp->b_wptr - bp->b_rptr;
}
return (count);
}
/*
* Allocate a stream head.
*/
struct stdata *
shalloc(queue_t *qp)
{
stdata_t *stp;
stp = kmem_cache_alloc(stream_head_cache, KM_SLEEP);
stp->sd_wrq = _WR(qp);
stp->sd_strtab = NULL;
stp->sd_iocid = 0;
stp->sd_mate = NULL;
stp->sd_freezer = NULL;
stp->sd_refcnt = 0;
stp->sd_wakeq = 0;
stp->sd_anchor = 0;
stp->sd_struiowrq = NULL;
stp->sd_struiordq = NULL;
stp->sd_struiodnak = 0;
stp->sd_struionak = NULL;
stp->sd_t_audit_data = NULL;
stp->sd_rput_opt = 0;
stp->sd_wput_opt = 0;
stp->sd_read_opt = 0;
stp->sd_rprotofunc = strrput_proto;
stp->sd_rmiscfunc = strrput_misc;
stp->sd_rderrfunc = stp->sd_wrerrfunc = NULL;
stp->sd_rputdatafunc = stp->sd_wputdatafunc = NULL;
stp->sd_ciputctrl = NULL;
stp->sd_nciputctrl = 0;
stp->sd_qhead = NULL;
stp->sd_qtail = NULL;
stp->sd_servid = NULL;
stp->sd_nqueues = 0;
stp->sd_svcflags = 0;
stp->sd_copyflag = 0;
return (stp);
}
/*
* Free a stream head.
*/
void
shfree(stdata_t *stp)
{
ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
stp->sd_wrq = NULL;
mutex_enter(&stp->sd_qlock);
while (stp->sd_svcflags & STRS_SCHEDULED) {
STRSTAT(strwaits);
cv_wait(&stp->sd_qcv, &stp->sd_qlock);
}
mutex_exit(&stp->sd_qlock);
if (stp->sd_ciputctrl != NULL) {
ASSERT(stp->sd_nciputctrl == n_ciputctrl - 1);
SUMCHECK_CIPUTCTRL_COUNTS(stp->sd_ciputctrl,
stp->sd_nciputctrl, 0);
ASSERT(ciputctrl_cache != NULL);
kmem_cache_free(ciputctrl_cache, stp->sd_ciputctrl);
stp->sd_ciputctrl = NULL;
stp->sd_nciputctrl = 0;
}
ASSERT(stp->sd_qhead == NULL);
ASSERT(stp->sd_qtail == NULL);
ASSERT(stp->sd_nqueues == 0);
kmem_cache_free(stream_head_cache, stp);
}
/*
* Allocate a pair of queues and a syncq for the pair
*/
queue_t *
allocq(void)
{
queinfo_t *qip;
queue_t *qp, *wqp;
syncq_t *sq;
qip = kmem_cache_alloc(queue_cache, KM_SLEEP);
qp = &qip->qu_rqueue;
wqp = &qip->qu_wqueue;
sq = &qip->qu_syncq;
qp->q_last = NULL;
qp->q_next = NULL;
qp->q_ptr = NULL;
qp->q_flag = QUSE | QREADR;
qp->q_bandp = NULL;
qp->q_stream = NULL;
qp->q_syncq = sq;
qp->q_nband = 0;
qp->q_nfsrv = NULL;
qp->q_draining = 0;
qp->q_syncqmsgs = 0;
qp->q_spri = 0;
qp->q_qtstamp = 0;
qp->q_sqtstamp = 0;
qp->q_fp = NULL;
wqp->q_last = NULL;
wqp->q_next = NULL;
wqp->q_ptr = NULL;
wqp->q_flag = QUSE;
wqp->q_bandp = NULL;
wqp->q_stream = NULL;
wqp->q_syncq = sq;
wqp->q_nband = 0;
wqp->q_nfsrv = NULL;
wqp->q_draining = 0;
wqp->q_syncqmsgs = 0;
wqp->q_qtstamp = 0;
wqp->q_sqtstamp = 0;
wqp->q_spri = 0;
sq->sq_count = 0;
sq->sq_rmqcount = 0;
sq->sq_flags = 0;
sq->sq_type = 0;
sq->sq_callbflags = 0;
sq->sq_cancelid = 0;
sq->sq_ciputctrl = NULL;
sq->sq_nciputctrl = 0;
sq->sq_needexcl = 0;
sq->sq_svcflags = 0;
return (qp);
}
/*
* Free a pair of queues and the "attached" syncq.
* Discard any messages left on the syncq(s), remove the syncq(s) from the
* outer perimeter, and free the syncq(s) if they are not the "attached" syncq.
*/
void
freeq(queue_t *qp)
{
qband_t *qbp, *nqbp;
syncq_t *sq, *outer;
queue_t *wqp = _WR(qp);
ASSERT(qp->q_flag & QREADR);
/*
* If a previously dispatched taskq job is scheduled to run
* sync_service() or a service routine is scheduled for the
* queues about to be freed, wait here until all service is
* done on the queue and all associated queues and syncqs.
*/
wait_svc(qp);
(void) flush_syncq(qp->q_syncq, qp);
(void) flush_syncq(wqp->q_syncq, wqp);
ASSERT(qp->q_syncqmsgs == 0 && wqp->q_syncqmsgs == 0);
/*
* Flush the queues before q_next is set to NULL This is needed
* in order to backenable any downstream queue before we go away.
* Note: we are already removed from the stream so that the
* backenabling will not cause any messages to be delivered to our
* put procedures.
*/
flushq(qp, FLUSHALL);
flushq(wqp, FLUSHALL);
/* Tidy up - removeq only does a half-remove from stream */
qp->q_next = wqp->q_next = NULL;
ASSERT(!(qp->q_flag & QENAB));
ASSERT(!(wqp->q_flag & QENAB));
outer = qp->q_syncq->sq_outer;
if (outer != NULL) {
outer_remove(outer, qp->q_syncq);
if (wqp->q_syncq != qp->q_syncq)
outer_remove(outer, wqp->q_syncq);
}
/*
* Free any syncqs that are outside what allocq returned.
*/
if (qp->q_syncq != SQ(qp) && !(qp->q_flag & QPERMOD))
free_syncq(qp->q_syncq);
if (qp->q_syncq != wqp->q_syncq && wqp->q_syncq != SQ(qp))
free_syncq(wqp->q_syncq);
ASSERT((qp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0);
ASSERT((wqp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0);
ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
ASSERT(MUTEX_NOT_HELD(QLOCK(wqp)));
sq = SQ(qp);
ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL);
ASSERT(sq->sq_outer == NULL);
ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
ASSERT(sq->sq_callbpend == NULL);
ASSERT(sq->sq_needexcl == 0);
if (sq->sq_ciputctrl != NULL) {
ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1);
SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl,
sq->sq_nciputctrl, 0);
ASSERT(ciputctrl_cache != NULL);
kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl);
sq->sq_ciputctrl = NULL;
sq->sq_nciputctrl = 0;
}
ASSERT(qp->q_first == NULL && wqp->q_first == NULL);
ASSERT(qp->q_count == 0 && wqp->q_count == 0);
ASSERT(qp->q_mblkcnt == 0 && wqp->q_mblkcnt == 0);
qp->q_flag &= ~QUSE;
wqp->q_flag &= ~QUSE;
/* NOTE: Uncomment the assert below once bugid 1159635 is fixed. */
/* ASSERT((qp->q_flag & QWANTW) == 0 && (wqp->q_flag & QWANTW) == 0); */
qbp = qp->q_bandp;
while (qbp) {
nqbp = qbp->qb_next;
freeband(qbp);
qbp = nqbp;
}
qbp = wqp->q_bandp;
while (qbp) {
nqbp = qbp->qb_next;
freeband(qbp);
qbp = nqbp;
}
kmem_cache_free(queue_cache, qp);
}
/*
* Allocate a qband structure.
*/
qband_t *
allocband(void)
{
qband_t *qbp;
qbp = kmem_cache_alloc(qband_cache, KM_NOSLEEP);
if (qbp == NULL)
return (NULL);
qbp->qb_next = NULL;
qbp->qb_count = 0;
qbp->qb_mblkcnt = 0;
qbp->qb_first = NULL;
qbp->qb_last = NULL;
qbp->qb_flag = 0;
return (qbp);
}
/*
* Free a qband structure.
*/
void
freeband(qband_t *qbp)
{
kmem_cache_free(qband_cache, qbp);
}
/*
* Just like putnextctl(9F), except that allocb_wait() is used.
*
* Consolidation Private, and of course only callable from the stream head or
* routines that may block.
*/
int
putnextctl_wait(queue_t *q, int type)
{
mblk_t *bp;
int error;
if ((datamsg(type) && (type != M_DELAY)) ||
(bp = allocb_wait(0, BPRI_HI, 0, &error)) == NULL)
return (0);
bp->b_datap->db_type = (unsigned char)type;
putnext(q, bp);
return (1);
}
/*
* Run any possible bufcalls.
*/
void
runbufcalls(void)
{
strbufcall_t *bcp;
mutex_enter(&bcall_monitor);
mutex_enter(&strbcall_lock);
if (strbcalls.bc_head) {
size_t count;
int nevent;
/*
* count how many events are on the list
* now so we can check to avoid looping
* in low memory situations
*/
nevent = 0;
for (bcp = strbcalls.bc_head; bcp; bcp = bcp->bc_next)
nevent++;
/*
* get estimate of available memory from kmem_avail().
* awake all bufcall functions waiting for
* memory whose request could be satisfied
* by 'count' memory and let 'em fight for it.
*/
count = kmem_avail();
while ((bcp = strbcalls.bc_head) != NULL && nevent) {
STRSTAT(bufcalls);
--nevent;
if (bcp->bc_size <= count) {
bcp->bc_executor = curthread;
mutex_exit(&strbcall_lock);
(*bcp->bc_func)(bcp->bc_arg);
mutex_enter(&strbcall_lock);
bcp->bc_executor = NULL;
cv_broadcast(&bcall_cv);
strbcalls.bc_head = bcp->bc_next;
kmem_free(bcp, sizeof (strbufcall_t));
} else {
/*
* too big, try again later - note
* that nevent was decremented above
* so we won't retry this one on this
* iteration of the loop
*/
if (bcp->bc_next != NULL) {
strbcalls.bc_head = bcp->bc_next;
bcp->bc_next = NULL;
strbcalls.bc_tail->bc_next = bcp;
strbcalls.bc_tail = bcp;
}
}
}
if (strbcalls.bc_head == NULL)
strbcalls.bc_tail = NULL;
}
mutex_exit(&strbcall_lock);
mutex_exit(&bcall_monitor);
}
/*
* Actually run queue's service routine.
*/
static void
runservice(queue_t *q)
{
qband_t *qbp;
ASSERT(q->q_qinfo->qi_srvp);
again:
entersq(q->q_syncq, SQ_SVC);
TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_START,
"runservice starts:%p", q);
if (!(q->q_flag & QWCLOSE))
(*q->q_qinfo->qi_srvp)(q);
TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_END,
"runservice ends:(%p)", q);
leavesq(q->q_syncq, SQ_SVC);
mutex_enter(QLOCK(q));
if (q->q_flag & QENAB) {
q->q_flag &= ~QENAB;
mutex_exit(QLOCK(q));
goto again;
}
q->q_flag &= ~QINSERVICE;
q->q_flag &= ~QBACK;
for (qbp = q->q_bandp; qbp; qbp = qbp->qb_next)
qbp->qb_flag &= ~QB_BACK;
/*
* Wakeup thread waiting for the service procedure
* to be run (strclose and qdetach).
*/
cv_broadcast(&q->q_wait);
mutex_exit(QLOCK(q));
}
/*
* Background processing of bufcalls.
*/
void
streams_bufcall_service(void)
{
callb_cpr_t cprinfo;
CALLB_CPR_INIT(&cprinfo, &strbcall_lock, callb_generic_cpr,
"streams_bufcall_service");
mutex_enter(&strbcall_lock);
for (;;) {
if (strbcalls.bc_head != NULL && kmem_avail() > 0) {
mutex_exit(&strbcall_lock);
runbufcalls();
mutex_enter(&strbcall_lock);
}
if (strbcalls.bc_head != NULL) {
clock_t wt, tick;
STRSTAT(bcwaits);
/* Wait for memory to become available */
CALLB_CPR_SAFE_BEGIN(&cprinfo);
tick = SEC_TO_TICK(60);
time_to_wait(&wt, tick);
(void) cv_timedwait(&memavail_cv, &strbcall_lock, wt);
CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock);
}
/* Wait for new work to arrive */
if (strbcalls.bc_head == NULL) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
cv_wait(&strbcall_cv, &strbcall_lock);
CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock);
}
}
}
/*
* Background processing of streams background tasks which failed
* taskq_dispatch.
*/
static void
streams_qbkgrnd_service(void)
{
callb_cpr_t cprinfo;
queue_t *q;
CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr,
"streams_bkgrnd_service");
mutex_enter(&service_queue);
for (;;) {
/*
* Wait for work to arrive.
*/
while ((freebs_list == NULL) && (qhead == NULL)) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
cv_wait(&services_to_run, &service_queue);
CALLB_CPR_SAFE_END(&cprinfo, &service_queue);
}
/*
* Handle all pending freebs requests to free memory.
*/
while (freebs_list != NULL) {
mblk_t *mp = freebs_list;
freebs_list = mp->b_next;
mutex_exit(&service_queue);
mblk_free(mp);
mutex_enter(&service_queue);
}
/*
* Run pending queues.
*/
while (qhead != NULL) {
DQ(q, qhead, qtail, q_link);
ASSERT(q != NULL);
mutex_exit(&service_queue);
queue_service(q);
mutex_enter(&service_queue);
}
ASSERT(qhead == NULL && qtail == NULL);
}
}
/*
* Background processing of streams background tasks which failed
* taskq_dispatch.
*/
static void
streams_sqbkgrnd_service(void)
{
callb_cpr_t cprinfo;
syncq_t *sq;
CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr,
"streams_sqbkgrnd_service");
mutex_enter(&service_queue);
for (;;) {
/*
* Wait for work to arrive.
*/
while (sqhead == NULL) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
cv_wait(&syncqs_to_run, &service_queue);
CALLB_CPR_SAFE_END(&cprinfo, &service_queue);
}
/*
* Run pending syncqs.
*/
while (sqhead != NULL) {
DQ(sq, sqhead, sqtail, sq_next);
ASSERT(sq != NULL);
ASSERT(sq->sq_svcflags & SQ_BGTHREAD);
mutex_exit(&service_queue);
syncq_service(sq);
mutex_enter(&service_queue);
}
}
}
/*
* Disable the syncq and wait for background syncq processing to complete.
* If the syncq is placed on the sqhead/sqtail queue, try to remove it from the
* list.
*/
void
wait_sq_svc(syncq_t *sq)
{
mutex_enter(SQLOCK(sq));
sq->sq_svcflags |= SQ_DISABLED;
if (sq->sq_svcflags & SQ_BGTHREAD) {
syncq_t *sq_chase;
syncq_t *sq_curr;
int removed;
ASSERT(sq->sq_servcount == 1);
mutex_enter(&service_queue);
RMQ(sq, sqhead, sqtail, sq_next, sq_chase, sq_curr, removed);
mutex_exit(&service_queue);
if (removed) {
sq->sq_svcflags &= ~SQ_BGTHREAD;
sq->sq_servcount = 0;
STRSTAT(sqremoved);
goto done;
}
}
while (sq->sq_servcount != 0) {
sq->sq_flags |= SQ_WANTWAKEUP;
cv_wait(&sq->sq_wait, SQLOCK(sq));
}
done:
mutex_exit(SQLOCK(sq));
}
/*
* Put a syncq on the list of syncq's to be serviced by the sqthread.
* Add the argument to the end of the sqhead list and set the flag
* indicating this syncq has been enabled. If it has already been
* enabled, don't do anything.
* This routine assumes that SQLOCK is held.
* NOTE that the lock order is to have the SQLOCK first,
* so if the service_syncq lock is held, we need to release it
* before acquiring the SQLOCK (mostly relevant for the background
* thread, and this seems to be common among the STREAMS global locks).
* Note that the sq_svcflags are protected by the SQLOCK.
*/
void
sqenable(syncq_t *sq)
{
/*
* This is probably not important except for where I believe it
* is being called. At that point, it should be held (and it
* is a pain to release it just for this routine, so don't do
* it).
*/
ASSERT(MUTEX_HELD(SQLOCK(sq)));
IMPLY(sq->sq_servcount == 0, sq->sq_next == NULL);
IMPLY(sq->sq_next != NULL, sq->sq_svcflags & SQ_BGTHREAD);
/*
* Do not put on list if background thread is scheduled or
* syncq is disabled.
*/
if (sq->sq_svcflags & (SQ_DISABLED | SQ_BGTHREAD))
return;
/*
* Check whether we should enable sq at all.
* Non PERMOD syncqs may be drained by at most one thread.
* PERMOD syncqs may be drained by several threads but we limit the
* total amount to the lesser of
* Number of queues on the squeue and
* Number of CPUs.
*/
if (sq->sq_servcount != 0) {
if (((sq->sq_type & SQ_PERMOD) == 0) ||
(sq->sq_servcount >= MIN(sq->sq_nqueues, ncpus_online))) {
STRSTAT(sqtoomany);
return;
}
}
sq->sq_tstamp = lbolt;
STRSTAT(sqenables);
/* Attempt a taskq dispatch */
sq->sq_servid = (void *)taskq_dispatch(streams_taskq,
(task_func_t *)syncq_service, sq, TQ_NOSLEEP | TQ_NOQUEUE);
if (sq->sq_servid != NULL) {
sq->sq_servcount++;
return;
}
/*
* This taskq dispatch failed, but a previous one may have succeeded.
* Don't try to schedule on the background thread whilst there is
* outstanding taskq processing.
*/
if (sq->sq_servcount != 0)
return;
/*
* System is low on resources and can't perform a non-sleeping
* dispatch. Schedule the syncq for a background thread and mark the
* syncq to avoid any further taskq dispatch attempts.
*/
mutex_enter(&service_queue);
STRSTAT(taskqfails);
ENQUEUE(sq, sqhead, sqtail, sq_next);
sq->sq_svcflags |= SQ_BGTHREAD;
sq->sq_servcount = 1;
cv_signal(&syncqs_to_run);
mutex_exit(&service_queue);
}
/*
* Note: fifo_close() depends on the mblk_t on the queue being freed
* asynchronously. The asynchronous freeing of messages breaks the
* recursive call chain of fifo_close() while there are I_SENDFD type of
* messages referring to other file pointers on the queue. Then when
* closing pipes it can avoid stack overflow in case of daisy-chained
* pipes, and also avoid deadlock in case of fifonode_t pairs (which
* share the same fifolock_t).
*/
void
freebs_enqueue(mblk_t *mp, dblk_t *dbp)
{
esb_queue_t *eqp = &system_esbq;
ASSERT(dbp->db_mblk == mp);
/*
* Check data sanity. The dblock should have non-empty free function.
* It is better to panic here then later when the dblock is freed
* asynchronously when the context is lost.
*/
if (dbp->db_frtnp->free_func == NULL) {
panic("freebs_enqueue: dblock %p has a NULL free callback",
(void *)dbp);
}
mutex_enter(&eqp->eq_lock);
/* queue the new mblk on the esballoc queue */
if (eqp->eq_head == NULL) {
eqp->eq_head = eqp->eq_tail = mp;
} else {
eqp->eq_tail->b_next = mp;
eqp->eq_tail = mp;
}
eqp->eq_len++;
/* If we're the first thread to reach the threshold, process */
if (eqp->eq_len >= esbq_max_qlen &&
!(eqp->eq_flags & ESBQ_PROCESSING))
esballoc_process_queue(eqp);
esballoc_set_timer(eqp, esbq_timeout);
mutex_exit(&eqp->eq_lock);
}
static void
esballoc_process_queue(esb_queue_t *eqp)
{
mblk_t *mp;
ASSERT(MUTEX_HELD(&eqp->eq_lock));
eqp->eq_flags |= ESBQ_PROCESSING;
do {
/*
* Detach the message chain for processing.
*/
mp = eqp->eq_head;
eqp->eq_tail->b_next = NULL;
eqp->eq_head = eqp->eq_tail = NULL;
eqp->eq_len = 0;
mutex_exit(&eqp->eq_lock);
/*
* Process the message chain.
*/
esballoc_enqueue_mblk(mp);
mutex_enter(&eqp->eq_lock);
} while ((eqp->eq_len >= esbq_max_qlen) && (eqp->eq_len > 0));
eqp->eq_flags &= ~ESBQ_PROCESSING;
}
/*
* taskq callback routine to free esballoced mblk's
*/
static void
esballoc_mblk_free(mblk_t *mp)
{
mblk_t *nextmp;
for (; mp != NULL; mp = nextmp) {
nextmp = mp->b_next;
mp->b_next = NULL;
mblk_free(mp);
}
}
static void
esballoc_enqueue_mblk(mblk_t *mp)
{
if (taskq_dispatch(system_taskq, (task_func_t *)esballoc_mblk_free, mp,
TQ_NOSLEEP) == NULL) {
mblk_t *first_mp = mp;
/*
* System is low on resources and can't perform a non-sleeping
* dispatch. Schedule for a background thread.
*/
mutex_enter(&service_queue);
STRSTAT(taskqfails);
while (mp->b_next != NULL)
mp = mp->b_next;
mp->b_next = freebs_list;
freebs_list = first_mp;
cv_signal(&services_to_run);
mutex_exit(&service_queue);
}
}
static void
esballoc_timer(void *arg)
{
esb_queue_t *eqp = arg;
mutex_enter(&eqp->eq_lock);
eqp->eq_flags &= ~ESBQ_TIMER;
if (!(eqp->eq_flags & ESBQ_PROCESSING) &&
eqp->eq_len > 0)
esballoc_process_queue(eqp);
esballoc_set_timer(eqp, esbq_timeout);
mutex_exit(&eqp->eq_lock);
}
static void
esballoc_set_timer(esb_queue_t *eqp, clock_t eq_timeout)
{
ASSERT(MUTEX_HELD(&eqp->eq_lock));
if (eqp->eq_len > 0 && !(eqp->eq_flags & ESBQ_TIMER)) {
(void) timeout(esballoc_timer, eqp, eq_timeout);
eqp->eq_flags |= ESBQ_TIMER;
}
}
void
esballoc_queue_init(void)
{
system_esbq.eq_len = 0;
system_esbq.eq_head = system_esbq.eq_tail = NULL;
system_esbq.eq_flags = 0;
}
/*
* Set the QBACK or QB_BACK flag in the given queue for
* the given priority band.
*/
void
setqback(queue_t *q, unsigned char pri)
{
int i;
qband_t *qbp;
qband_t **qbpp;
ASSERT(MUTEX_HELD(QLOCK(q)));
if (pri != 0) {
if (pri > q->q_nband) {
qbpp = &q->q_bandp;
while (*qbpp)
qbpp = &(*qbpp)->qb_next;
while (pri > q->q_nband) {
if ((*qbpp = allocband()) == NULL) {
cmn_err(CE_WARN,
"setqback: can't allocate qband\n");
return;
}
(*qbpp)->qb_hiwat = q->q_hiwat;
(*qbpp)->qb_lowat = q->q_lowat;
q->q_nband++;
qbpp = &(*qbpp)->qb_next;
}
}
qbp = q->q_bandp;
i = pri;
while (--i)
qbp = qbp->qb_next;
qbp->qb_flag |= QB_BACK;
} else {
q->q_flag |= QBACK;
}
}
int
strcopyin(void *from, void *to, size_t len, int copyflag)
{
if (copyflag & U_TO_K) {
ASSERT((copyflag & K_TO_K) == 0);
if (copyin(from, to, len))
return (EFAULT);
} else {
ASSERT(copyflag & K_TO_K);
bcopy(from, to, len);
}
return (0);
}
int
strcopyout(void *from, void *to, size_t len, int copyflag)
{
if (copyflag & U_TO_K) {
if (copyout(from, to, len))
return (EFAULT);
} else {
ASSERT(copyflag & K_TO_K);
bcopy(from, to, len);
}
return (0);
}
/*
* strsignal_nolock() posts a signal to the process(es) at the stream head.
* It assumes that the stream head lock is already held, whereas strsignal()
* acquires the lock first. This routine was created because a few callers
* release the stream head lock before calling only to re-acquire it after
* it returns.
*/
void
strsignal_nolock(stdata_t *stp, int sig, uchar_t band)
{
ASSERT(MUTEX_HELD(&stp->sd_lock));
switch (sig) {
case SIGPOLL:
if (stp->sd_sigflags & S_MSG)
strsendsig(stp->sd_siglist, S_MSG, band, 0);
break;
default:
if (stp->sd_pgidp)
pgsignal(stp->sd_pgidp, sig);
break;
}
}
void
strsignal(stdata_t *stp, int sig, int32_t band)
{
TRACE_3(TR_FAC_STREAMS_FR, TR_SENDSIG,
"strsignal:%p, %X, %X", stp, sig, band);
mutex_enter(&stp->sd_lock);
switch (sig) {
case SIGPOLL:
if (stp->sd_sigflags & S_MSG)
strsendsig(stp->sd_siglist, S_MSG, (uchar_t)band, 0);
break;
default:
if (stp->sd_pgidp) {
pgsignal(stp->sd_pgidp, sig);
}
break;
}
mutex_exit(&stp->sd_lock);
}
void
strhup(stdata_t *stp)
{
ASSERT(mutex_owned(&stp->sd_lock));
pollwakeup(&stp->sd_pollist, POLLHUP);
if (stp->sd_sigflags & S_HANGUP)
strsendsig(stp->sd_siglist, S_HANGUP, 0, 0);
}
/*
* Backenable the first queue upstream from `q' with a service procedure.
*/
void
backenable(queue_t *q, uchar_t pri)
{
queue_t *nq;
/*
* Our presence might not prevent other modules in our own
* stream from popping/pushing since the caller of getq might not
* have a claim on the queue (some drivers do a getq on somebody
* else's queue - they know that the queue itself is not going away
* but the framework has to guarantee q_next in that stream).
*/
claimstr(q);
/* Find nearest back queue with service proc */
for (nq = backq(q); nq && !nq->q_qinfo->qi_srvp; nq = backq(nq)) {
ASSERT(STRMATED(q->q_stream) || STREAM(q) == STREAM(nq));
}
if (nq) {
kthread_t *freezer;
/*
* backenable can be called either with no locks held
* or with the stream frozen (the latter occurs when a module
* calls rmvq with the stream frozen). If the stream is frozen
* by the caller the caller will hold all qlocks in the stream.
* Note that a frozen stream doesn't freeze a mated stream,
* so we explicitly check for that.
*/
freezer = STREAM(q)->sd_freezer;
if (freezer != curthread || STREAM(q) != STREAM(nq)) {
mutex_enter(QLOCK(nq));
}
#ifdef DEBUG
else {
ASSERT(frozenstr(q));
ASSERT(MUTEX_HELD(QLOCK(q)));
ASSERT(MUTEX_HELD(QLOCK(nq)));
}
#endif
setqback(nq, pri);
qenable_locked(nq);
if (freezer != curthread || STREAM(q) != STREAM(nq))
mutex_exit(QLOCK(nq));
}
releasestr(q);
}
/*
* Return the appropriate errno when one of flags_to_check is set
* in sd_flags. Uses the exported error routines if they are set.
* Will return 0 if non error is set (or if the exported error routines
* do not return an error).
*
* If there is both a read and write error to check, we prefer the read error.
* Also, give preference to recorded errno's over the error functions.
* The flags that are handled are:
* STPLEX return EINVAL
* STRDERR return sd_rerror (and clear if STRDERRNONPERSIST)
* STWRERR return sd_werror (and clear if STWRERRNONPERSIST)
* STRHUP return sd_werror
*
* If the caller indicates that the operation is a peek, a nonpersistent error
* is not cleared.
*/
int
strgeterr(stdata_t *stp, int32_t flags_to_check, int ispeek)
{
int32_t sd_flag = stp->sd_flag & flags_to_check;
int error = 0;
ASSERT(MUTEX_HELD(&stp->sd_lock));
ASSERT((flags_to_check & ~(STRDERR|STWRERR|STRHUP|STPLEX)) == 0);
if (sd_flag & STPLEX)
error = EINVAL;
else if (sd_flag & STRDERR) {
error = stp->sd_rerror;
if ((stp->sd_flag & STRDERRNONPERSIST) && !ispeek) {
/*
* Read errors are non-persistent i.e. discarded once
* returned to a non-peeking caller,
*/
stp->sd_rerror = 0;
stp->sd_flag &= ~STRDERR;
}
if (error == 0 && stp->sd_rderrfunc != NULL) {
int clearerr = 0;
error = (*stp->sd_rderrfunc)(stp->sd_vnode, ispeek,
&clearerr);
if (clearerr) {
stp->sd_flag &= ~STRDERR;
stp->sd_rderrfunc = NULL;
}
}
} else if (sd_flag & STWRERR) {
error = stp->sd_werror;
if ((stp->sd_flag & STWRERRNONPERSIST) && !ispeek) {
/*
* Write errors are non-persistent i.e. discarded once
* returned to a non-peeking caller,
*/
stp->sd_werror = 0;
stp->sd_flag &= ~STWRERR;
}
if (error == 0 && stp->sd_wrerrfunc != NULL) {
int clearerr = 0;
error = (*stp->sd_wrerrfunc)(stp->sd_vnode, ispeek,
&clearerr);
if (clearerr) {
stp->sd_flag &= ~STWRERR;
stp->sd_wrerrfunc = NULL;
}
}
} else if (sd_flag & STRHUP) {
/* sd_werror set when STRHUP */
error = stp->sd_werror;
}
return (error);
}
/*
* Single-thread open/close/push/pop
* for twisted streams also
*/
int
strstartplumb(stdata_t *stp, int flag, int cmd)
{
int waited = 1;
int error = 0;
if (STRMATED(stp)) {
struct stdata *stmatep = stp->sd_mate;
STRLOCKMATES(stp);
while (waited) {
waited = 0;
while (stmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
if ((cmd == I_POP) &&
(flag & (FNDELAY|FNONBLOCK))) {
STRUNLOCKMATES(stp);
return (EAGAIN);
}
waited = 1;
mutex_exit(&stp->sd_lock);
if (!cv_wait_sig(&stmatep->sd_monitor,
&stmatep->sd_lock)) {
mutex_exit(&stmatep->sd_lock);
return (EINTR);
}
mutex_exit(&stmatep->sd_lock);
STRLOCKMATES(stp);
}
while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
if ((cmd == I_POP) &&
(flag & (FNDELAY|FNONBLOCK))) {
STRUNLOCKMATES(stp);
return (EAGAIN);
}
waited = 1;
mutex_exit(&stmatep->sd_lock);
if (!cv_wait_sig(&stp->sd_monitor,
&stp->sd_lock)) {
mutex_exit(&stp->sd_lock);
return (EINTR);
}
mutex_exit(&stp->sd_lock);
STRLOCKMATES(stp);
}
if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
error = strgeterr(stp,
STRDERR|STWRERR|STRHUP|STPLEX, 0);
if (error != 0) {
STRUNLOCKMATES(stp);
return (error);
}
}
}
stp->sd_flag |= STRPLUMB;
STRUNLOCKMATES(stp);
} else {
mutex_enter(&stp->sd_lock);
while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
if (((cmd == I_POP) || (cmd == _I_REMOVE)) &&
(flag & (FNDELAY|FNONBLOCK))) {
mutex_exit(&stp->sd_lock);
return (EAGAIN);
}
if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) {
mutex_exit(&stp->sd_lock);
return (EINTR);
}
if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
error = strgeterr(stp,
STRDERR|STWRERR|STRHUP|STPLEX, 0);
if (error != 0) {
mutex_exit(&stp->sd_lock);
return (error);
}
}
}
stp->sd_flag |= STRPLUMB;
mutex_exit(&stp->sd_lock);
}
return (0);
}
/*
* Complete the plumbing operation associated with stream `stp'.
*/
void
strendplumb(stdata_t *stp)
{
ASSERT(MUTEX_HELD(&stp->sd_lock));
ASSERT(stp->sd_flag & STRPLUMB);
stp->sd_flag &= ~STRPLUMB;
cv_broadcast(&stp->sd_monitor);
}
/*
* This describes how the STREAMS framework handles synchronization
* during open/push and close/pop.
* The key interfaces for open and close are qprocson and qprocsoff,
* respectively. While the close case in general is harder both open
* have close have significant similarities.
*
* During close the STREAMS framework has to both ensure that there
* are no stale references to the queue pair (and syncq) that
* are being closed and also provide the guarantees that are documented
* in qprocsoff(9F).
* If there are stale references to the queue that is closing it can
* result in kernel memory corruption or kernel panics.
*
* Note that is it up to the module/driver to ensure that it itself
* does not have any stale references to the closing queues once its close
* routine returns. This includes:
* - Cancelling any timeout/bufcall/qtimeout/qbufcall callback routines
* associated with the queues. For timeout and bufcall callbacks the
* module/driver also has to ensure (or wait for) any callbacks that
* are in progress.
* - If the module/driver is using esballoc it has to ensure that any
* esballoc free functions do not refer to a queue that has closed.
* (Note that in general the close routine can not wait for the esballoc'ed
* messages to be freed since that can cause a deadlock.)
* - Cancelling any interrupts that refer to the closing queues and
* also ensuring that there are no interrupts in progress that will
* refer to the closing queues once the close routine returns.
* - For multiplexors removing any driver global state that refers to
* the closing queue and also ensuring that there are no threads in
* the multiplexor that has picked up a queue pointer but not yet
* finished using it.
*
* In addition, a driver/module can only reference the q_next pointer
* in its open, close, put, or service procedures or in a
* qtimeout/qbufcall callback procedure executing "on" the correct
* stream. Thus it can not reference the q_next pointer in an interrupt
* routine or a timeout, bufcall or esballoc callback routine. Likewise
* it can not reference q_next of a different queue e.g. in a mux that
* passes messages from one queues put/service procedure to another queue.
* In all the cases when the driver/module can not access the q_next
* field it must use the *next* versions e.g. canputnext instead of
* canput(q->q_next) and putnextctl instead of putctl(q->q_next, ...).
*
*
* Assuming that the driver/module conforms to the above constraints
* the STREAMS framework has to avoid stale references to q_next for all
* the framework internal cases which include (but are not limited to):
* - Threads in canput/canputnext/backenable and elsewhere that are
* walking q_next.
* - Messages on a syncq that have a reference to the queue through b_queue.
* - Messages on an outer perimeter (syncq) that have a reference to the
* queue through b_queue.
* - Threads that use q_nfsrv (e.g. canput) to find a queue.
* Note that only canput and bcanput use q_nfsrv without any locking.
*
* The STREAMS framework providing the qprocsoff(9F) guarantees means that
* after qprocsoff returns, the framework has to ensure that no threads can
* enter the put or service routines for the closing read or write-side queue.
* In addition to preventing "direct" entry into the put procedures
* the framework also has to prevent messages being drained from
* the syncq or the outer perimeter.
* XXX Note that currently qdetach does relies on D_MTOCEXCL as the only
* mechanism to prevent qwriter(PERIM_OUTER) from running after
* qprocsoff has returned.
* Note that if a module/driver uses put(9F) on one of its own queues
* it is up to the module/driver to ensure that the put() doesn't
* get called when the queue is closing.
*
*
* The framework aspects of the above "contract" is implemented by
* qprocsoff, removeq, and strlock:
* - qprocsoff (disable_svc) sets QWCLOSE to prevent runservice from
* entering the service procedures.
* - strlock acquires the sd_lock and sd_reflock to prevent putnext,
* canputnext, backenable etc from dereferencing the q_next that will
* soon change.
* - strlock waits for sd_refcnt to be zero to wait for e.g. any canputnext
* or other q_next walker that uses claimstr/releasestr to finish.
* - optionally for every syncq in the stream strlock acquires all the
* sq_lock's and waits for all sq_counts to drop to a value that indicates
* that no thread executes in the put or service procedures and that no
* thread is draining into the module/driver. This ensures that no
* open, close, put, service, or qtimeout/qbufcall callback procedure is
* currently executing hence no such thread can end up with the old stale
* q_next value and no canput/backenable can have the old stale
* q_nfsrv/q_next.
* - qdetach (wait_svc) makes sure that any scheduled or running threads
* have either finished or observed the QWCLOSE flag and gone away.
*/
/*
* Get all the locks necessary to change q_next.
*
* Wait for sd_refcnt to reach 0 and, if sqlist is present, wait for the
* sq_count of each syncq in the list to drop to sq_rmqcount, indicating that
* the only threads inside the syncq are threads currently calling removeq().
* Since threads calling removeq() are in the process of removing their queues
* from the stream, we do not need to worry about them accessing a stale q_next
* pointer and thus we do not need to wait for them to exit (in fact, waiting
* for them can cause deadlock).
*
* This routine is subject to starvation since it does not set any flag to
* prevent threads from entering a module in the stream (i.e. sq_count can
* increase on some syncq while it is waiting on some other syncq).
*
* Assumes that only one thread attempts to call strlock for a given
* stream. If this is not the case the two threads would deadlock.
* This assumption is guaranteed since strlock is only called by insertq
* and removeq and streams plumbing changes are single-threaded for
* a given stream using the STWOPEN, STRCLOSE, and STRPLUMB flags.
*
* For pipes, it is not difficult to atomically designate a pair of streams
* to be mated. Once mated atomically by the framework the twisted pair remain
* configured that way until dismantled atomically by the framework.
* When plumbing takes place on a twisted stream it is necessary to ensure that
* this operation is done exclusively on the twisted stream since two such
* operations, each initiated on different ends of the pipe will deadlock
* waiting for each other to complete.
*
* On entry, no locks should be held.
* The locks acquired and held by strlock depends on a few factors.
* - If sqlist is non-NULL all the syncq locks in the sqlist will be acquired
* and held on exit and all sq_count are at an acceptable level.
* - In all cases, sd_lock and sd_reflock are acquired and held on exit with
* sd_refcnt being zero.
*/
static void
strlock(struct stdata *stp, sqlist_t *sqlist)
{
syncql_t *sql, *sql2;
retry:
/*
* Wait for any claimstr to go away.
*/
if (STRMATED(stp)) {
struct stdata *stp1, *stp2;
STRLOCKMATES(stp);
/*
* Note that the selection of locking order is not
* important, just that they are always acquired in
* the same order. To assure this, we choose this
* order based on the value of the pointer, and since
* the pointer will not change for the life of this
* pair, we will always grab the locks in the same
* order (and hence, prevent deadlocks).
*/
if (&(stp->sd_lock) > &((stp->sd_mate)->sd_lock)) {
stp1 = stp;
stp2 = stp->sd_mate;
} else {
stp2 = stp;
stp1 = stp->sd_mate;
}
mutex_enter(&stp1->sd_reflock);
if (stp1->sd_refcnt > 0) {
STRUNLOCKMATES(stp);
cv_wait(&stp1->sd_refmonitor, &stp1->sd_reflock);
mutex_exit(&stp1->sd_reflock);
goto retry;
}
mutex_enter(&stp2->sd_reflock);
if (stp2->sd_refcnt > 0) {
STRUNLOCKMATES(stp);
mutex_exit(&stp1->sd_reflock);
cv_wait(&stp2->sd_refmonitor, &stp2->sd_reflock);
mutex_exit(&stp2->sd_reflock);
goto retry;
}
STREAM_PUTLOCKS_ENTER(stp1);
STREAM_PUTLOCKS_ENTER(stp2);
} else {
mutex_enter(&stp->sd_lock);
mutex_enter(&stp->sd_reflock);
while (stp->sd_refcnt > 0) {
mutex_exit(&stp->sd_lock);
cv_wait(&stp->sd_refmonitor, &stp->sd_reflock);
if (mutex_tryenter(&stp->sd_lock) == 0) {
mutex_exit(&stp->sd_reflock);
mutex_enter(&stp->sd_lock);
mutex_enter(&stp->sd_reflock);
}
}
STREAM_PUTLOCKS_ENTER(stp);
}
if (sqlist == NULL)
return;
for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) {
syncq_t *sq = sql->sql_sq;
uint16_t count;
mutex_enter(SQLOCK(sq));
count = sq->sq_count;
ASSERT(sq->sq_rmqcount <= count);
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
if (count == sq->sq_rmqcount)
continue;
/* Failed - drop all locks that we have acquired so far */
if (STRMATED(stp)) {
STREAM_PUTLOCKS_EXIT(stp);
STREAM_PUTLOCKS_EXIT(stp->sd_mate);
STRUNLOCKMATES(stp);
mutex_exit(&stp->sd_reflock);
mutex_exit(&stp->sd_mate->sd_reflock);
} else {
STREAM_PUTLOCKS_EXIT(stp);
mutex_exit(&stp->sd_lock);
mutex_exit(&stp->sd_reflock);
}
for (sql2 = sqlist->sqlist_head; sql2 != sql;
sql2 = sql2->sql_next) {
SQ_PUTLOCKS_EXIT(sql2->sql_sq);
mutex_exit(SQLOCK(sql2->sql_sq));
}
/*
* The wait loop below may starve when there are many threads
* claiming the syncq. This is especially a problem with permod
* syncqs (IP). To lessen the impact of the problem we increment
* sq_needexcl and clear fastbits so that putnexts will slow
* down and call sqenable instead of draining right away.
*/
sq->sq_needexcl++;
SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
while (count > sq->sq_rmqcount) {
sq->sq_flags |= SQ_WANTWAKEUP;
SQ_PUTLOCKS_EXIT(sq);
cv_wait(&sq->sq_wait, SQLOCK(sq));
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
}
sq->sq_needexcl--;
if (sq->sq_needexcl == 0)
SQ_PUTCOUNT_SETFAST_LOCKED(sq);
SQ_PUTLOCKS_EXIT(sq);
ASSERT(count == sq->sq_rmqcount);
mutex_exit(SQLOCK(sq));
goto retry;
}
}
/*
* Drop all the locks that strlock acquired.
*/
static void
strunlock(struct stdata *stp, sqlist_t *sqlist)
{
syncql_t *sql;
if (STRMATED(stp)) {
STREAM_PUTLOCKS_EXIT(stp);
STREAM_PUTLOCKS_EXIT(stp->sd_mate);
STRUNLOCKMATES(stp);
mutex_exit(&stp->sd_reflock);
mutex_exit(&stp->sd_mate->sd_reflock);
} else {
STREAM_PUTLOCKS_EXIT(stp);
mutex_exit(&stp->sd_lock);
mutex_exit(&stp->sd_reflock);
}
if (sqlist == NULL)
return;
for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) {
SQ_PUTLOCKS_EXIT(sql->sql_sq);
mutex_exit(SQLOCK(sql->sql_sq));
}
}
/*
* When the module has service procedure, we need check if the next
* module which has service procedure is in flow control to trigger
* the backenable.
*/
static void
backenable_insertedq(queue_t *q)
{
qband_t *qbp;
claimstr(q);
if (q->q_qinfo->qi_srvp != NULL && q->q_next != NULL) {
if (q->q_next->q_nfsrv->q_flag & QWANTW)
backenable(q, 0);
qbp = q->q_next->q_nfsrv->q_bandp;
for (; qbp != NULL; qbp = qbp->qb_next)
if ((qbp->qb_flag & QB_WANTW) && qbp->qb_first != NULL)
backenable(q, qbp->qb_first->b_band);
}
releasestr(q);
}
/*
* Given two read queues, insert a new single one after another.
*
* This routine acquires all the necessary locks in order to change
* q_next and related pointer using strlock().
* It depends on the stream head ensuring that there are no concurrent
* insertq or removeq on the same stream. The stream head ensures this
* using the flags STWOPEN, STRCLOSE, and STRPLUMB.
*
* Note that no syncq locks are held during the q_next change. This is
* applied to all streams since, unlike removeq, there is no problem of stale
* pointers when adding a module to the stream. Thus drivers/modules that do a
* canput(rq->q_next) would never get a closed/freed queue pointer even if we
* applied this optimization to all streams.
*/
void
insertq(struct stdata *stp, queue_t *new)
{
queue_t *after;
queue_t *wafter;
queue_t *wnew = _WR(new);
boolean_t have_fifo = B_FALSE;
if (new->q_flag & _QINSERTING) {
ASSERT(stp->sd_vnode->v_type != VFIFO);
after = new->q_next;
wafter = _WR(new->q_next);
} else {
after = _RD(stp->sd_wrq);
wafter = stp->sd_wrq;
}
TRACE_2(TR_FAC_STREAMS_FR, TR_INSERTQ,
"insertq:%p, %p", after, new);
ASSERT(after->q_flag & QREADR);
ASSERT(new->q_flag & QREADR);
strlock(stp, NULL);
/* Do we have a FIFO? */
if (wafter->q_next == after) {
have_fifo = B_TRUE;
wnew->q_next = new;
} else {
wnew->q_next = wafter->q_next;
}
new->q_next = after;
set_nfsrv_ptr(new, wnew, after, wafter);
/*
* set_nfsrv_ptr() needs to know if this is an insertion or not,
* so only reset this flag after calling it.
*/
new->q_flag &= ~_QINSERTING;
if (have_fifo) {
wafter->q_next = wnew;
} else {
if (wafter->q_next)
_OTHERQ(wafter->q_next)->q_next = new;
wafter->q_next = wnew;
}
set_qend(new);
/* The QEND flag might have to be updated for the upstream guy */
set_qend(after);
ASSERT(_SAMESTR(new) == O_SAMESTR(new));
ASSERT(_SAMESTR(wnew) == O_SAMESTR(wnew));
ASSERT(_SAMESTR(after) == O_SAMESTR(after));
ASSERT(_SAMESTR(wafter) == O_SAMESTR(wafter));
strsetuio(stp);
/*
* If this was a module insertion, bump the push count.
*/
if (!(new->q_flag & QISDRV))
stp->sd_pushcnt++;
strunlock(stp, NULL);
/* check if the write Q needs backenable */
backenable_insertedq(wnew);
/* check if the read Q needs backenable */
backenable_insertedq(new);
}
/*
* Given a read queue, unlink it from any neighbors.
*
* This routine acquires all the necessary locks in order to
* change q_next and related pointers and also guard against
* stale references (e.g. through q_next) to the queue that
* is being removed. It also plays part of the role in ensuring
* that the module's/driver's put procedure doesn't get called
* after qprocsoff returns.
*
* Removeq depends on the stream head ensuring that there are
* no concurrent insertq or removeq on the same stream. The
* stream head ensures this using the flags STWOPEN, STRCLOSE and
* STRPLUMB.
*
* The set of locks needed to remove the queue is different in
* different cases:
*
* Acquire sd_lock, sd_reflock, and all the syncq locks in the stream after
* waiting for the syncq reference count to drop to 0 indicating that no
* non-close threads are present anywhere in the stream. This ensures that any
* module/driver can reference q_next in its open, close, put, or service
* procedures.
*
* The sq_rmqcount counter tracks the number of threads inside removeq().
* strlock() ensures that there is either no threads executing inside perimeter
* or there is only a thread calling qprocsoff().
*
* strlock() compares the value of sq_count with the number of threads inside
* removeq() and waits until sq_count is equal to sq_rmqcount. We need to wakeup
* any threads waiting in strlock() when the sq_rmqcount increases.
*/
void
removeq(queue_t *qp)
{
queue_t *wqp = _WR(qp);
struct stdata *stp = STREAM(qp);
sqlist_t *sqlist = NULL;
boolean_t isdriver;
int moved;
syncq_t *sq = qp->q_syncq;
syncq_t *wsq = wqp->q_syncq;
ASSERT(stp);
TRACE_2(TR_FAC_STREAMS_FR, TR_REMOVEQ,
"removeq:%p %p", qp, wqp);
ASSERT(qp->q_flag&QREADR);
/*
* For queues using Synchronous streams, we must wait for all threads in
* rwnext() to drain out before proceeding.
*/
if (qp->q_flag & QSYNCSTR) {
/* First, we need wakeup any threads blocked in rwnext() */
mutex_enter(SQLOCK(sq));
if (sq->sq_flags & SQ_WANTWAKEUP) {
sq->sq_flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
mutex_exit(SQLOCK(sq));
if (wsq != sq) {
mutex_enter(SQLOCK(wsq));
if (wsq->sq_flags & SQ_WANTWAKEUP) {
wsq->sq_flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&wsq->sq_wait);
}
mutex_exit(SQLOCK(wsq));
}
mutex_enter(QLOCK(qp));
while (qp->q_rwcnt > 0) {
qp->q_flag |= QWANTRMQSYNC;
cv_wait(&qp->q_wait, QLOCK(qp));
}
mutex_exit(QLOCK(qp));
mutex_enter(QLOCK(wqp));
while (wqp->q_rwcnt > 0) {
wqp->q_flag |= QWANTRMQSYNC;
cv_wait(&wqp->q_wait, QLOCK(wqp));
}
mutex_exit(QLOCK(wqp));
}
mutex_enter(SQLOCK(sq));
sq->sq_rmqcount++;
if (sq->sq_flags & SQ_WANTWAKEUP) {
sq->sq_flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
mutex_exit(SQLOCK(sq));
isdriver = (qp->q_flag & QISDRV);
sqlist = sqlist_build(qp, stp, STRMATED(stp));
strlock(stp, sqlist);
reset_nfsrv_ptr(qp, wqp);
ASSERT(wqp->q_next == NULL || backq(qp)->q_next == qp);
ASSERT(qp->q_next == NULL || backq(wqp)->q_next == wqp);
/* Do we have a FIFO? */
if (wqp->q_next == qp) {
stp->sd_wrq->q_next = _RD(stp->sd_wrq);
} else {
if (wqp->q_next)
backq(qp)->q_next = qp->q_next;
if (qp->q_next)
backq(wqp)->q_next = wqp->q_next;
}
/* The QEND flag might have to be updated for the upstream guy */
if (qp->q_next)
set_qend(qp->q_next);
ASSERT(_SAMESTR(stp->sd_wrq) == O_SAMESTR(stp->sd_wrq));
ASSERT(_SAMESTR(_RD(stp->sd_wrq)) == O_SAMESTR(_RD(stp->sd_wrq)));
/*
* Move any messages destined for the put procedures to the next
* syncq in line. Otherwise free them.
*/
moved = 0;
/*
* Quick check to see whether there are any messages or events.
*/
if (qp->q_syncqmsgs != 0 || (qp->q_syncq->sq_flags & SQ_EVENTS))
moved += propagate_syncq(qp);
if (wqp->q_syncqmsgs != 0 ||
(wqp->q_syncq->sq_flags & SQ_EVENTS))
moved += propagate_syncq(wqp);
strsetuio(stp);
/*
* If this was a module removal, decrement the push count.
*/
if (!isdriver)
stp->sd_pushcnt--;
strunlock(stp, sqlist);
sqlist_free(sqlist);
/*
* Make sure any messages that were propagated are drained.
* Also clear any QFULL bit caused by messages that were propagated.
*/
if (qp->q_next != NULL) {
clr_qfull(qp);
/*
* For the driver calling qprocsoff, propagate_syncq
* frees all the messages instead of putting it in
* the stream head
*/
if (!isdriver && (moved > 0))
emptysq(qp->q_next->q_syncq);
}
if (wqp->q_next != NULL) {
clr_qfull(wqp);
/*
* We come here for any pop of a module except for the
* case of driver being removed. We don't call emptysq
* if we did not move any messages. This will avoid holding
* PERMOD syncq locks in emptysq
*/
if (moved > 0)
emptysq(wqp->q_next->q_syncq);
}
mutex_enter(SQLOCK(sq));
sq->sq_rmqcount--;
mutex_exit(SQLOCK(sq));
}
/*
* Prevent further entry by setting a flag (like SQ_FROZEN, SQ_BLOCKED or
* SQ_WRITER) on a syncq.
* If maxcnt is not -1 it assumes that caller has "maxcnt" claim(s) on the
* sync queue and waits until sq_count reaches maxcnt.
*
* If maxcnt is -1 there's no need to grab sq_putlocks since the caller
* does not care about putnext threads that are in the middle of calling put
* entry points.
*
* This routine is used for both inner and outer syncqs.
*/
static void
blocksq(syncq_t *sq, ushort_t flag, int maxcnt)
{
uint16_t count = 0;
mutex_enter(SQLOCK(sq));
/*
* Wait for SQ_FROZEN/SQ_BLOCKED to be reset.
* SQ_FROZEN will be set if there is a frozen stream that has a
* queue which also refers to this "shared" syncq.
* SQ_BLOCKED will be set if there is "off" queue which also
* refers to this "shared" syncq.
*/
if (maxcnt != -1) {
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
SUM_SQ_PUTCOUNTS(sq, count);
}
sq->sq_needexcl++;
ASSERT(sq->sq_needexcl != 0); /* wraparound */
while ((sq->sq_flags & flag) ||
(maxcnt != -1 && count > (unsigned)maxcnt)) {
sq->sq_flags |= SQ_WANTWAKEUP;
if (maxcnt != -1) {
SQ_PUTLOCKS_EXIT(sq);
}
cv_wait(&sq->sq_wait, SQLOCK(sq));
if (maxcnt != -1) {
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
}
}
sq->sq_needexcl--;
sq->sq_flags |= flag;
ASSERT(maxcnt == -1 || count == maxcnt);
if (maxcnt != -1) {
if (sq->sq_needexcl == 0) {
SQ_PUTCOUNT_SETFAST_LOCKED(sq);
}
SQ_PUTLOCKS_EXIT(sq);
} else if (sq->sq_needexcl == 0) {
SQ_PUTCOUNT_SETFAST(sq);
}
mutex_exit(SQLOCK(sq));
}
/*
* Reset a flag that was set with blocksq.
*
* Can not use this routine to reset SQ_WRITER.
*
* If "isouter" is set then the syncq is assumed to be an outer perimeter
* and drain_syncq is not called. Instead we rely on the qwriter_outer thread
* to handle the queued qwriter operations.
*
* No need to grab sq_putlocks here. See comment in strsubr.h that explains when
* sq_putlocks are used.
*/
static void
unblocksq(syncq_t *sq, uint16_t resetflag, int isouter)
{
uint16_t flags;
mutex_enter(SQLOCK(sq));
ASSERT(resetflag != SQ_WRITER);
ASSERT(sq->sq_flags & resetflag);
flags = sq->sq_flags & ~resetflag;
sq->sq_flags = flags;
if (flags & (SQ_QUEUED | SQ_WANTWAKEUP)) {
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
sq->sq_flags = flags;
if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
if (!isouter) {
/* drain_syncq drops SQLOCK */
drain_syncq(sq);
return;
}
}
}
mutex_exit(SQLOCK(sq));
}
/*
* Reset a flag that was set with blocksq.
* Does not drain the syncq. Use emptysq() for that.
* Returns 1 if SQ_QUEUED is set. Otherwise 0.
*
* No need to grab sq_putlocks here. See comment in strsubr.h that explains when
* sq_putlocks are used.
*/
static int
dropsq(syncq_t *sq, uint16_t resetflag)
{
uint16_t flags;
mutex_enter(SQLOCK(sq));
ASSERT(sq->sq_flags & resetflag);
flags = sq->sq_flags & ~resetflag;
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
sq->sq_flags = flags;
mutex_exit(SQLOCK(sq));
if (flags & SQ_QUEUED)
return (1);
return (0);
}
/*
* Empty all the messages on a syncq.
*
* No need to grab sq_putlocks here. See comment in strsubr.h that explains when
* sq_putlocks are used.
*/
static void
emptysq(syncq_t *sq)
{
uint16_t flags;
mutex_enter(SQLOCK(sq));
flags = sq->sq_flags;
if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
/*
* To prevent potential recursive invocation of drain_syncq we
* do not call drain_syncq if count is non-zero.
*/
if (sq->sq_count == 0) {
/* drain_syncq() drops SQLOCK */
drain_syncq(sq);
return;
} else
sqenable(sq);
}
mutex_exit(SQLOCK(sq));
}
/*
* Ordered insert while removing duplicates.
*/
static void
sqlist_insert(sqlist_t *sqlist, syncq_t *sqp)
{
syncql_t *sqlp, **prev_sqlpp, *new_sqlp;
prev_sqlpp = &sqlist->sqlist_head;
while ((sqlp = *prev_sqlpp) != NULL) {
if (sqlp->sql_sq >= sqp) {
if (sqlp->sql_sq == sqp) /* duplicate */
return;
break;
}
prev_sqlpp = &sqlp->sql_next;
}
new_sqlp = &sqlist->sqlist_array[sqlist->sqlist_index++];
ASSERT((char *)new_sqlp < (char *)sqlist + sqlist->sqlist_size);
new_sqlp->sql_next = sqlp;
new_sqlp->sql_sq = sqp;
*prev_sqlpp = new_sqlp;
}
/*
* Walk the write side queues until we hit either the driver
* or a twist in the stream (_SAMESTR will return false in both
* these cases) then turn around and walk the read side queues
* back up to the stream head.
*/
static void
sqlist_insertall(sqlist_t *sqlist, queue_t *q)
{
while (q != NULL) {
sqlist_insert(sqlist, q->q_syncq);
if (_SAMESTR(q))
q = q->q_next;
else if (!(q->q_flag & QREADR))
q = _RD(q);
else
q = NULL;
}
}
/*
* Allocate and build a list of all syncqs in a stream and the syncq(s)
* associated with the "q" parameter. The resulting list is sorted in a
* canonical order and is free of duplicates.
* Assumes the passed queue is a _RD(q).
*/
static sqlist_t *
sqlist_build(queue_t *q, struct stdata *stp, boolean_t do_twist)
{
sqlist_t *sqlist = sqlist_alloc(stp, KM_SLEEP);
/*
* start with the current queue/qpair
*/
ASSERT(q->q_flag & QREADR);
sqlist_insert(sqlist, q->q_syncq);
sqlist_insert(sqlist, _WR(q)->q_syncq);
sqlist_insertall(sqlist, stp->sd_wrq);
if (do_twist)
sqlist_insertall(sqlist, stp->sd_mate->sd_wrq);
return (sqlist);
}
static sqlist_t *
sqlist_alloc(struct stdata *stp, int kmflag)
{
size_t sqlist_size;
sqlist_t *sqlist;
/*
* Allocate 2 syncql_t's for each pushed module. Note that
* the sqlist_t structure already has 4 syncql_t's built in:
* 2 for the stream head, and 2 for the driver/other stream head.
*/
sqlist_size = 2 * sizeof (syncql_t) * stp->sd_pushcnt +
sizeof (sqlist_t);
if (STRMATED(stp))
sqlist_size += 2 * sizeof (syncql_t) * stp->sd_mate->sd_pushcnt;
sqlist = kmem_alloc(sqlist_size, kmflag);
sqlist->sqlist_head = NULL;
sqlist->sqlist_size = sqlist_size;
sqlist->sqlist_index = 0;
return (sqlist);
}
/*
* Free the list created by sqlist_alloc()
*/
static void
sqlist_free(sqlist_t *sqlist)
{
kmem_free(sqlist, sqlist->sqlist_size);
}
/*
* Prevent any new entries into any syncq in this stream.
* Used by freezestr.
*/
void
strblock(queue_t *q)
{
struct stdata *stp;
syncql_t *sql;
sqlist_t *sqlist;
q = _RD(q);
stp = STREAM(q);
ASSERT(stp != NULL);
/*
* Get a sorted list with all the duplicates removed containing
* all the syncqs referenced by this stream.
*/
sqlist = sqlist_build(q, stp, B_FALSE);
for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next)
blocksq(sql->sql_sq, SQ_FROZEN, -1);
sqlist_free(sqlist);
}
/*
* Release the block on new entries into this stream
*/
void
strunblock(queue_t *q)
{
struct stdata *stp;
syncql_t *sql;
sqlist_t *sqlist;
int drain_needed;
q = _RD(q);
/*
* Get a sorted list with all the duplicates removed containing
* all the syncqs referenced by this stream.
* Have to drop the SQ_FROZEN flag on all the syncqs before
* starting to drain them; otherwise the draining might
* cause a freezestr in some module on the stream (which
* would deadlock).
*/
stp = STREAM(q);
ASSERT(stp != NULL);
sqlist = sqlist_build(q, stp, B_FALSE);
drain_needed = 0;
for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next)
drain_needed += dropsq(sql->sql_sq, SQ_FROZEN);
if (drain_needed) {
for (sql = sqlist->sqlist_head; sql != NULL;
sql = sql->sql_next)
emptysq(sql->sql_sq);
}
sqlist_free(sqlist);
}
#ifdef DEBUG
static int
qprocsareon(queue_t *rq)
{
if (rq->q_next == NULL)
return (0);
return (_WR(rq->q_next)->q_next == _WR(rq));
}
int
qclaimed(queue_t *q)
{
uint_t count;
count = q->q_syncq->sq_count;
SUM_SQ_PUTCOUNTS(q->q_syncq, count);
return (count != 0);
}
/*
* Check if anyone has frozen this stream with freezestr
*/
int
frozenstr(queue_t *q)
{
return ((q->q_syncq->sq_flags & SQ_FROZEN) != 0);
}
#endif /* DEBUG */
/*
* Enter a queue.
* Obsoleted interface. Should not be used.
*/
void
enterq(queue_t *q)
{
entersq(q->q_syncq, SQ_CALLBACK);
}
void
leaveq(queue_t *q)
{
leavesq(q->q_syncq, SQ_CALLBACK);
}
/*
* Enter a perimeter. c_inner and c_outer specifies which concurrency bits
* to check.
* Wait if SQ_QUEUED is set to preserve ordering between messages and qwriter
* calls and the running of open, close and service procedures.
*
* If c_inner bit is set no need to grab sq_putlocks since we don't care
* if other threads have entered or are entering put entry point.
*
* If c_inner bit is set it might have been possible to use
* sq_putlocks/sq_putcounts instead of SQLOCK/sq_count (e.g. to optimize
* open/close path for IP) but since the count may need to be decremented in
* qwait() we wouldn't know which counter to decrement. Currently counter is
* selected by current cpu_seqid and current CPU can change at any moment. XXX
* in the future we might use curthread id bits to select the counter and this
* would stay constant across routine calls.
*/
void
entersq(syncq_t *sq, int entrypoint)
{
uint16_t count = 0;
uint16_t flags;
uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL;
uint16_t type;
uint_t c_inner = entrypoint & SQ_CI;
uint_t c_outer = entrypoint & SQ_CO;
/*
* Increment ref count to keep closes out of this queue.
*/
ASSERT(sq);
ASSERT(c_inner && c_outer);
mutex_enter(SQLOCK(sq));
flags = sq->sq_flags;
type = sq->sq_type;
if (!(type & c_inner)) {
/* Make sure all putcounts now use slowlock. */
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
SUM_SQ_PUTCOUNTS(sq, count);
sq->sq_needexcl++;
ASSERT(sq->sq_needexcl != 0); /* wraparound */
waitflags |= SQ_MESSAGES;
}
/*
* Wait until we can enter the inner perimeter.
* If we want exclusive access we wait until sq_count is 0.
* We have to do this before entering the outer perimeter in order
* to preserve put/close message ordering.
*/
while ((flags & waitflags) || (!(type & c_inner) && count != 0)) {
sq->sq_flags = flags | SQ_WANTWAKEUP;
if (!(type & c_inner)) {
SQ_PUTLOCKS_EXIT(sq);
}
cv_wait(&sq->sq_wait, SQLOCK(sq));
if (!(type & c_inner)) {
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
}
flags = sq->sq_flags;
}
if (!(type & c_inner)) {
ASSERT(sq->sq_needexcl > 0);
sq->sq_needexcl--;
if (sq->sq_needexcl == 0) {
SQ_PUTCOUNT_SETFAST_LOCKED(sq);
}
}
/* Check if we need to enter the outer perimeter */
if (!(type & c_outer)) {
/*
* We have to enter the outer perimeter exclusively before
* we can increment sq_count to avoid deadlock. This implies
* that we have to re-check sq_flags and sq_count.
*
* is it possible to have c_inner set when c_outer is not set?
*/
if (!(type & c_inner)) {
SQ_PUTLOCKS_EXIT(sq);
}
mutex_exit(SQLOCK(sq));
outer_enter(sq->sq_outer, SQ_GOAWAY);
mutex_enter(SQLOCK(sq));
flags = sq->sq_flags;
/*
* there should be no need to recheck sq_putcounts
* because outer_enter() has already waited for them to clear
* after setting SQ_WRITER.
*/
count = sq->sq_count;
#ifdef DEBUG
/*
* SUMCHECK_SQ_PUTCOUNTS should return the sum instead
* of doing an ASSERT internally. Others should do
* something like
* ASSERT(SUMCHECK_SQ_PUTCOUNTS(sq) == 0);
* without the need to #ifdef DEBUG it.
*/
SUMCHECK_SQ_PUTCOUNTS(sq, 0);
#endif
while ((flags & (SQ_EXCL|SQ_BLOCKED|SQ_FROZEN)) ||
(!(type & c_inner) && count != 0)) {
sq->sq_flags = flags | SQ_WANTWAKEUP;
cv_wait(&sq->sq_wait, SQLOCK(sq));
count = sq->sq_count;
flags = sq->sq_flags;
}
}
sq->sq_count++;
ASSERT(sq->sq_count != 0); /* Wraparound */
if (!(type & c_inner)) {
/* Exclusive entry */
ASSERT(sq->sq_count == 1);
sq->sq_flags |= SQ_EXCL;
if (type & c_outer) {
SQ_PUTLOCKS_EXIT(sq);
}
}
mutex_exit(SQLOCK(sq));
}
/*
* Leave a syncq. Announce to framework that closes may proceed.
* c_inner and c_outer specify which concurrency bits to check.
*
* Must never be called from driver or module put entry point.
*
* No need to grab sq_putlocks here. See comment in strsubr.h that explains when
* sq_putlocks are used.
*/
void
leavesq(syncq_t *sq, int entrypoint)
{
uint16_t flags;
uint16_t type;
uint_t c_outer = entrypoint & SQ_CO;
#ifdef DEBUG
uint_t c_inner = entrypoint & SQ_CI;
#endif
/*
* Decrement ref count, drain the syncq if possible, and wake up
* any waiting close.
*/
ASSERT(sq);
ASSERT(c_inner && c_outer);
mutex_enter(SQLOCK(sq));
flags = sq->sq_flags;
type = sq->sq_type;
if (flags & (SQ_QUEUED|SQ_WANTWAKEUP|SQ_WANTEXWAKEUP)) {
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
if (flags & SQ_WANTEXWAKEUP) {
flags &= ~SQ_WANTEXWAKEUP;
cv_broadcast(&sq->sq_exitwait);
}
if ((flags & SQ_QUEUED) && !(flags & SQ_STAYAWAY)) {
/*
* The syncq needs to be drained. "Exit" the syncq
* before calling drain_syncq.
*/
ASSERT(sq->sq_count != 0);
sq->sq_count--;
ASSERT((flags & SQ_EXCL) || (type & c_inner));
sq->sq_flags = flags & ~SQ_EXCL;
drain_syncq(sq);
ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
/* Check if we need to exit the outer perimeter */
/* XXX will this ever be true? */
if (!(type & c_outer))
outer_exit(sq->sq_outer);
return;
}
}
ASSERT(sq->sq_count != 0);
sq->sq_count--;
ASSERT((flags & SQ_EXCL) || (type & c_inner));
sq->sq_flags = flags & ~SQ_EXCL;
mutex_exit(SQLOCK(sq));
/* Check if we need to exit the outer perimeter */
if (!(sq->sq_type & c_outer))
outer_exit(sq->sq_outer);
}
/*
* Prevent q_next from changing in this stream by incrementing sq_count.
*
* No need to grab sq_putlocks here. See comment in strsubr.h that explains when
* sq_putlocks are used.
*/
void
claimq(queue_t *qp)
{
syncq_t *sq = qp->q_syncq;
mutex_enter(SQLOCK(sq));
sq->sq_count++;
ASSERT(sq->sq_count != 0); /* Wraparound */
mutex_exit(SQLOCK(sq));
}
/*
* Undo claimq.
*
* No need to grab sq_putlocks here. See comment in strsubr.h that explains when
* sq_putlocks are used.
*/
void
releaseq(queue_t *qp)
{
syncq_t *sq = qp->q_syncq;
uint16_t flags;
mutex_enter(SQLOCK(sq));
ASSERT(sq->sq_count > 0);
sq->sq_count--;
flags = sq->sq_flags;
if (flags & (SQ_WANTWAKEUP|SQ_QUEUED)) {
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
sq->sq_flags = flags;
if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
/*
* To prevent potential recursive invocation of
* drain_syncq we do not call drain_syncq if count is
* non-zero.
*/
if (sq->sq_count == 0) {
drain_syncq(sq);
return;
} else
sqenable(sq);
}
}
mutex_exit(SQLOCK(sq));
}
/*
* Prevent q_next from changing in this stream by incrementing sd_refcnt.
*/
void
claimstr(queue_t *qp)
{
struct stdata *stp = STREAM(qp);
mutex_enter(&stp->sd_reflock);
stp->sd_refcnt++;
ASSERT(stp->sd_refcnt != 0); /* Wraparound */
mutex_exit(&stp->sd_reflock);
}
/*
* Undo claimstr.
*/
void
releasestr(queue_t *qp)
{
struct stdata *stp = STREAM(qp);
mutex_enter(&stp->sd_reflock);
ASSERT(stp->sd_refcnt != 0);
if (--stp->sd_refcnt == 0)
cv_broadcast(&stp->sd_refmonitor);
mutex_exit(&stp->sd_reflock);
}
static syncq_t *
new_syncq(void)
{
return (kmem_cache_alloc(syncq_cache, KM_SLEEP));
}
static void
free_syncq(syncq_t *sq)
{
ASSERT(sq->sq_head == NULL);
ASSERT(sq->sq_outer == NULL);
ASSERT(sq->sq_callbpend == NULL);
ASSERT((sq->sq_onext == NULL && sq->sq_oprev == NULL) ||
(sq->sq_onext == sq && sq->sq_oprev == sq));
if (sq->sq_ciputctrl != NULL) {
ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1);
SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl,
sq->sq_nciputctrl, 0);
ASSERT(ciputctrl_cache != NULL);
kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl);
}
sq->sq_tail = NULL;
sq->sq_evhead = NULL;
sq->sq_evtail = NULL;
sq->sq_ciputctrl = NULL;
sq->sq_nciputctrl = 0;
sq->sq_count = 0;
sq->sq_rmqcount = 0;
sq->sq_callbflags = 0;
sq->sq_cancelid = 0;
sq->sq_next = NULL;
sq->sq_needexcl = 0;
sq->sq_svcflags = 0;
sq->sq_nqueues = 0;
sq->sq_pri = 0;
sq->sq_onext = NULL;
sq->sq_oprev = NULL;
sq->sq_flags = 0;
sq->sq_type = 0;
sq->sq_servcount = 0;
kmem_cache_free(syncq_cache, sq);
}
/* Outer perimeter code */
/*
* The outer syncq uses the fields and flags in the syncq slightly
* differently from the inner syncqs.
* sq_count Incremented when there are pending or running
* writers at the outer perimeter to prevent the set of
* inner syncqs that belong to the outer perimeter from
* changing.
* sq_head/tail List of deferred qwriter(OUTER) operations.
*
* SQ_BLOCKED Set to prevent traversing of sq_next,sq_prev while
* inner syncqs are added to or removed from the
* outer perimeter.
* SQ_QUEUED sq_head/tail has messages or events queued.
*
* SQ_WRITER A thread is currently traversing all the inner syncqs
* setting the SQ_WRITER flag.
*/
/*
* Get write access at the outer perimeter.
* Note that read access is done by entersq, putnext, and put by simply
* incrementing sq_count in the inner syncq.
*
* Waits until "flags" is no longer set in the outer to prevent multiple
* threads from having write access at the same time. SQ_WRITER has to be part
* of "flags".
*
* Increases sq_count on the outer syncq to keep away outer_insert/remove
* until the outer_exit is finished.
*
* outer_enter is vulnerable to starvation since it does not prevent new
* threads from entering the inner syncqs while it is waiting for sq_count to
* go to zero.
*/
void
outer_enter(syncq_t *outer, uint16_t flags)
{
syncq_t *sq;
int wait_needed;
uint16_t count;
ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
outer->sq_oprev != NULL);
ASSERT(flags & SQ_WRITER);
retry:
mutex_enter(SQLOCK(outer));
while (outer->sq_flags & flags) {
outer->sq_flags |= SQ_WANTWAKEUP;
cv_wait(&outer->sq_wait, SQLOCK(outer));
}
ASSERT(!(outer->sq_flags & SQ_WRITER));
outer->sq_flags |= SQ_WRITER;
outer->sq_count++;
ASSERT(outer->sq_count != 0); /* wraparound */
wait_needed = 0;
/*
* Set SQ_WRITER on all the inner syncqs while holding
* the SQLOCK on the outer syncq. This ensures that the changing
* of SQ_WRITER is atomic under the outer SQLOCK.
*/
for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
mutex_enter(SQLOCK(sq));
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
sq->sq_flags |= SQ_WRITER;
SUM_SQ_PUTCOUNTS(sq, count);
if (count != 0)
wait_needed = 1;
SQ_PUTLOCKS_EXIT(sq);
mutex_exit(SQLOCK(sq));
}
mutex_exit(SQLOCK(outer));
/*
* Get everybody out of the syncqs sequentially.
* Note that we don't actually need to acquire the PUTLOCKS, since
* we have already cleared the fastbit, and set QWRITER. By
* definition, the count can not increase since putnext will
* take the slowlock path (and the purpose of acquiring the
* putlocks was to make sure it didn't increase while we were
* waiting).
*
* Note that we still acquire the PUTLOCKS to be safe.
*/
if (wait_needed) {
for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
mutex_enter(SQLOCK(sq));
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
while (count != 0) {
sq->sq_flags |= SQ_WANTWAKEUP;
SQ_PUTLOCKS_EXIT(sq);
cv_wait(&sq->sq_wait, SQLOCK(sq));
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
}
SQ_PUTLOCKS_EXIT(sq);
mutex_exit(SQLOCK(sq));
}
/*
* Verify that none of the flags got set while we
* were waiting for the sq_counts to drop.
* If this happens we exit and retry entering the
* outer perimeter.
*/
mutex_enter(SQLOCK(outer));
if (outer->sq_flags & (flags & ~SQ_WRITER)) {
mutex_exit(SQLOCK(outer));
outer_exit(outer);
goto retry;
}
mutex_exit(SQLOCK(outer));
}
}
/*
* Drop the write access at the outer perimeter.
* Read access is dropped implicitly (by putnext, put, and leavesq) by
* decrementing sq_count.
*/
void
outer_exit(syncq_t *outer)
{
syncq_t *sq;
int drain_needed;
uint16_t flags;
ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
outer->sq_oprev != NULL);
ASSERT(MUTEX_NOT_HELD(SQLOCK(outer)));
/*
* Atomically (from the perspective of threads calling become_writer)
* drop the write access at the outer perimeter by holding
* SQLOCK(outer) across all the dropsq calls and the resetting of
* SQ_WRITER.
* This defines a locking order between the outer perimeter
* SQLOCK and the inner perimeter SQLOCKs.
*/
mutex_enter(SQLOCK(outer));
flags = outer->sq_flags;
ASSERT(outer->sq_flags & SQ_WRITER);
if (flags & SQ_QUEUED) {
write_now(outer);
flags = outer->sq_flags;
}
/*
* sq_onext is stable since sq_count has not yet been decreased.
* Reset the SQ_WRITER flags in all syncqs.
* After dropping SQ_WRITER on the outer syncq we empty all the
* inner syncqs.
*/
drain_needed = 0;
for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext)
drain_needed += dropsq(sq, SQ_WRITER);
ASSERT(!(outer->sq_flags & SQ_QUEUED));
flags &= ~SQ_WRITER;
if (drain_needed) {
outer->sq_flags = flags;
mutex_exit(SQLOCK(outer));
for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext)
emptysq(sq);
mutex_enter(SQLOCK(outer));
flags = outer->sq_flags;
}
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&outer->sq_wait);
}
outer->sq_flags = flags;
ASSERT(outer->sq_count > 0);
outer->sq_count--;
mutex_exit(SQLOCK(outer));
}
/*
* Add another syncq to an outer perimeter.
* Block out all other access to the outer perimeter while it is being
* changed using blocksq.
* Assumes that the caller has *not* done an outer_enter.
*
* Vulnerable to starvation in blocksq.
*/
static void
outer_insert(syncq_t *outer, syncq_t *sq)
{
ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
outer->sq_oprev != NULL);
ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
sq->sq_oprev == NULL); /* Can't be in an outer perimeter */
/* Get exclusive access to the outer perimeter list */
blocksq(outer, SQ_BLOCKED, 0);
ASSERT(outer->sq_flags & SQ_BLOCKED);
ASSERT(!(outer->sq_flags & SQ_WRITER));
mutex_enter(SQLOCK(sq));
sq->sq_outer = outer;
outer->sq_onext->sq_oprev = sq;
sq->sq_onext = outer->sq_onext;
outer->sq_onext = sq;
sq->sq_oprev = outer;
mutex_exit(SQLOCK(sq));
unblocksq(outer, SQ_BLOCKED, 1);
}
/*
* Remove a syncq from an outer perimeter.
* Block out all other access to the outer perimeter while it is being
* changed using blocksq.
* Assumes that the caller has *not* done an outer_enter.
*
* Vulnerable to starvation in blocksq.
*/
static void
outer_remove(syncq_t *outer, syncq_t *sq)
{
ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
outer->sq_oprev != NULL);
ASSERT(sq->sq_outer == outer);
/* Get exclusive access to the outer perimeter list */
blocksq(outer, SQ_BLOCKED, 0);
ASSERT(outer->sq_flags & SQ_BLOCKED);
ASSERT(!(outer->sq_flags & SQ_WRITER));
mutex_enter(SQLOCK(sq));
sq->sq_outer = NULL;
sq->sq_onext->sq_oprev = sq->sq_oprev;
sq->sq_oprev->sq_onext = sq->sq_onext;
sq->sq_oprev = sq->sq_onext = NULL;
mutex_exit(SQLOCK(sq));
unblocksq(outer, SQ_BLOCKED, 1);
}
/*
* Queue a deferred qwriter(OUTER) callback for this outer perimeter.
* If this is the first callback for this outer perimeter then add
* this outer perimeter to the list of outer perimeters that
* the qwriter_outer_thread will process.
*
* Increments sq_count in the outer syncq to prevent the membership
* of the outer perimeter (in terms of inner syncqs) to change while
* the callback is pending.
*/
static void
queue_writer(syncq_t *outer, void (*func)(), queue_t *q, mblk_t *mp)
{
ASSERT(MUTEX_HELD(SQLOCK(outer)));
mp->b_prev = (mblk_t *)func;
mp->b_queue = q;
mp->b_next = NULL;
outer->sq_count++; /* Decremented when dequeued */
ASSERT(outer->sq_count != 0); /* Wraparound */
if (outer->sq_evhead == NULL) {
/* First message. */
outer->sq_evhead = outer->sq_evtail = mp;
outer->sq_flags |= SQ_EVENTS;
mutex_exit(SQLOCK(outer));
STRSTAT(qwr_outer);
(void) taskq_dispatch(streams_taskq,
(task_func_t *)qwriter_outer_service, outer, TQ_SLEEP);
} else {
ASSERT(outer->sq_flags & SQ_EVENTS);
outer->sq_evtail->b_next = mp;
outer->sq_evtail = mp;
mutex_exit(SQLOCK(outer));
}
}
/*
* Try and upgrade to write access at the outer perimeter. If this can
* not be done without blocking then queue the callback to be done
* by the qwriter_outer_thread.
*
* This routine can only be called from put or service procedures plus
* asynchronous callback routines that have properly entered the queue (with
* entersq). Thus qwriter(OUTER) assumes the caller has one claim on the syncq
* associated with q.
*/
void
qwriter_outer(queue_t *q, mblk_t *mp, void (*func)())
{
syncq_t *osq, *sq, *outer;
int failed;
uint16_t flags;
osq = q->q_syncq;
outer = osq->sq_outer;
if (outer == NULL)
panic("qwriter(PERIM_OUTER): no outer perimeter");
ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
outer->sq_oprev != NULL);
mutex_enter(SQLOCK(outer));
flags = outer->sq_flags;
/*
* If some thread is traversing sq_next, or if we are blocked by
* outer_insert or outer_remove, or if the we already have queued
* callbacks, then queue this callback for later processing.
*
* Also queue the qwriter for an interrupt thread in order
* to reduce the time spent running at high IPL.
* to identify there are events.
*/
if ((flags & SQ_GOAWAY) || (curthread->t_pri >= kpreemptpri)) {
/*
* Queue the become_writer request.
* The queueing is atomic under SQLOCK(outer) in order
* to synchronize with outer_exit.
* queue_writer will drop the outer SQLOCK
*/
if (flags & SQ_BLOCKED) {
/* Must set SQ_WRITER on inner perimeter */
mutex_enter(SQLOCK(osq));
osq->sq_flags |= SQ_WRITER;
mutex_exit(SQLOCK(osq));
} else {
if (!(flags & SQ_WRITER)) {
/*
* The outer could have been SQ_BLOCKED thus
* SQ_WRITER might not be set on the inner.
*/
mutex_enter(SQLOCK(osq));
osq->sq_flags |= SQ_WRITER;
mutex_exit(SQLOCK(osq));
}
ASSERT(osq->sq_flags & SQ_WRITER);
}
queue_writer(outer, func, q, mp);
return;
}
/*
* We are half-way to exclusive access to the outer perimeter.
* Prevent any outer_enter, qwriter(OUTER), or outer_insert/remove
* while the inner syncqs are traversed.
*/
outer->sq_count++;
ASSERT(outer->sq_count != 0); /* wraparound */
flags |= SQ_WRITER;
/*
* Check if we can run the function immediately. Mark all
* syncqs with the writer flag to prevent new entries into
* put and service procedures.
*
* Set SQ_WRITER on all the inner syncqs while holding
* the SQLOCK on the outer syncq. This ensures that the changing
* of SQ_WRITER is atomic under the outer SQLOCK.
*/
failed = 0;
for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
uint16_t count;
uint_t maxcnt = (sq == osq) ? 1 : 0;
mutex_enter(SQLOCK(sq));
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
if (sq->sq_count > maxcnt)
failed = 1;
sq->sq_flags |= SQ_WRITER;
SQ_PUTLOCKS_EXIT(sq);
mutex_exit(SQLOCK(sq));
}
if (failed) {
/*
* Some other thread has a read claim on the outer perimeter.
* Queue the callback for deferred processing.
*
* queue_writer will set SQ_QUEUED before we drop SQ_WRITER
* so that other qwriter(OUTER) calls will queue their
* callbacks as well. queue_writer increments sq_count so we
* decrement to compensate for the our increment.
*
* Dropping SQ_WRITER enables the writer thread to work
* on this outer perimeter.
*/
outer->sq_flags = flags;
queue_writer(outer, func, q, mp);
/* queue_writer dropper the lock */
mutex_enter(SQLOCK(outer));
ASSERT(outer->sq_count > 0);
outer->sq_count--;
ASSERT(outer->sq_flags & SQ_WRITER);
flags = outer->sq_flags;
flags &= ~SQ_WRITER;
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&outer->sq_wait);
}
outer->sq_flags = flags;
mutex_exit(SQLOCK(outer));
return;
} else {
outer->sq_flags = flags;
mutex_exit(SQLOCK(outer));
}
/* Can run it immediately */
(*func)(q, mp);
outer_exit(outer);
}
/*
* Dequeue all writer callbacks from the outer perimeter and run them.
*/
static void
write_now(syncq_t *outer)
{
mblk_t *mp;
queue_t *q;
void (*func)();
ASSERT(MUTEX_HELD(SQLOCK(outer)));
ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
outer->sq_oprev != NULL);
while ((mp = outer->sq_evhead) != NULL) {
/*
* queues cannot be placed on the queuelist on the outer
* perimeter.
*/
ASSERT(!(outer->sq_flags & SQ_MESSAGES));
ASSERT((outer->sq_flags & SQ_EVENTS));
outer->sq_evhead = mp->b_next;
if (outer->sq_evhead == NULL) {
outer->sq_evtail = NULL;
outer->sq_flags &= ~SQ_EVENTS;
}
ASSERT(outer->sq_count != 0);
outer->sq_count--; /* Incremented when enqueued. */
mutex_exit(SQLOCK(outer));
/*
* Drop the message if the queue is closing.
* Make sure that the queue is "claimed" when the callback
* is run in order to satisfy various ASSERTs.
*/
q = mp->b_queue;
func = (void (*)())mp->b_prev;
ASSERT(func != NULL);
mp->b_next = mp->b_prev = NULL;
if (q->q_flag & QWCLOSE) {
freemsg(mp);
} else {
claimq(q);
(*func)(q, mp);
releaseq(q);
}
mutex_enter(SQLOCK(outer));
}
ASSERT(MUTEX_HELD(SQLOCK(outer)));
}
/*
* The list of messages on the inner syncq is effectively hashed
* by destination queue. These destination queues are doubly
* linked lists (hopefully) in priority order. Messages are then
* put on the queue referenced by the q_sqhead/q_sqtail elements.
* Additional messages are linked together by the b_next/b_prev
* elements in the mblk, with (similar to putq()) the first message
* having a NULL b_prev and the last message having a NULL b_next.
*
* Events, such as qwriter callbacks, are put onto a list in FIFO
* order referenced by sq_evhead, and sq_evtail. This is a singly
* linked list, and messages here MUST be processed in the order queued.
*/
/*
* Run the events on the syncq event list (sq_evhead).
* Assumes there is only one claim on the syncq, it is
* already exclusive (SQ_EXCL set), and the SQLOCK held.
* Messages here are processed in order, with the SQ_EXCL bit
* held all the way through till the last message is processed.
*/
void
sq_run_events(syncq_t *sq)
{
mblk_t *bp;
queue_t *qp;
uint16_t flags = sq->sq_flags;
void (*func)();
ASSERT(MUTEX_HELD(SQLOCK(sq)));
ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
sq->sq_oprev == NULL) ||
(sq->sq_outer != NULL && sq->sq_onext != NULL &&
sq->sq_oprev != NULL));
ASSERT(flags & SQ_EXCL);
ASSERT(sq->sq_count == 1);
/*
* We need to process all of the events on this list. It
* is possible that new events will be added while we are
* away processing a callback, so on every loop, we start
* back at the beginning of the list.
*/
/*
* We have to reaccess sq_evhead since there is a
* possibility of a new entry while we were running
* the callback.
*/
for (bp = sq->sq_evhead; bp != NULL; bp = sq->sq_evhead) {
ASSERT(bp->b_queue->q_syncq == sq);
ASSERT(sq->sq_flags & SQ_EVENTS);
qp = bp->b_queue;
func = (void (*)())bp->b_prev;
ASSERT(func != NULL);
/*
* Messages from the event queue must be taken off in
* FIFO order.
*/
ASSERT(sq->sq_evhead == bp);
sq->sq_evhead = bp->b_next;
if (bp->b_next == NULL) {
/* Deleting last */
ASSERT(sq->sq_evtail == bp);
sq->sq_evtail = NULL;
sq->sq_flags &= ~SQ_EVENTS;
}
bp->b_prev = bp->b_next = NULL;
ASSERT(bp->b_datap->db_ref != 0);
mutex_exit(SQLOCK(sq));
(*func)(qp, bp);
mutex_enter(SQLOCK(sq));
/*
* re-read the flags, since they could have changed.
*/
flags = sq->sq_flags;
ASSERT(flags & SQ_EXCL);
}
ASSERT(sq->sq_evhead == NULL && sq->sq_evtail == NULL);
ASSERT(!(sq->sq_flags & SQ_EVENTS));
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
if (flags & SQ_WANTEXWAKEUP) {
flags &= ~SQ_WANTEXWAKEUP;
cv_broadcast(&sq->sq_exitwait);
}
sq->sq_flags = flags;
}
/*
* Put messages on the event list.
* If we can go exclusive now, do so and process the event list, otherwise
* let the last claim service this list (or wake the sqthread).
* This procedure assumes SQLOCK is held. To run the event list, it
* must be called with no claims.
*/
static void
sqfill_events(syncq_t *sq, queue_t *q, mblk_t *mp, void (*func)())
{
uint16_t count;
ASSERT(MUTEX_HELD(SQLOCK(sq)));
ASSERT(func != NULL);
/*
* This is a callback. Add it to the list of callbacks
* and see about upgrading.
*/
mp->b_prev = (mblk_t *)func;
mp->b_queue = q;
mp->b_next = NULL;
if (sq->sq_evhead == NULL) {
sq->sq_evhead = sq->sq_evtail = mp;
sq->sq_flags |= SQ_EVENTS;
} else {
ASSERT(sq->sq_evtail != NULL);
ASSERT(sq->sq_evtail->b_next == NULL);
ASSERT(sq->sq_flags & SQ_EVENTS);
sq->sq_evtail->b_next = mp;
sq->sq_evtail = mp;
}
/*
* We have set SQ_EVENTS, so threads will have to
* unwind out of the perimeter, and new entries will
* not grab a putlock. But we still need to know
* how many threads have already made a claim to the
* syncq, so grab the putlocks, and sum the counts.
* If there are no claims on the syncq, we can upgrade
* to exclusive, and run the event list.
* NOTE: We hold the SQLOCK, so we can just grab the
* putlocks.
*/
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
/*
* We have no claim, so we need to check if there
* are no others, then we can upgrade.
*/
/*
* There are currently no claims on
* the syncq by this thread (at least on this entry). The thread who has
* the claim should drain syncq.
*/
if (count > 0) {
/*
* Can't upgrade - other threads inside.
*/
SQ_PUTLOCKS_EXIT(sq);
mutex_exit(SQLOCK(sq));
return;
}
/*
* Need to set SQ_EXCL and make a claim on the syncq.
*/
ASSERT((sq->sq_flags & SQ_EXCL) == 0);
sq->sq_flags |= SQ_EXCL;
ASSERT(sq->sq_count == 0);
sq->sq_count++;
SQ_PUTLOCKS_EXIT(sq);
/* Process the events list */
sq_run_events(sq);
/*
* Release our claim...
*/
sq->sq_count--;
/*
* And release SQ_EXCL.
* We don't need to acquire the putlocks to release
* SQ_EXCL, since we are exclusive, and hold the SQLOCK.
*/
sq->sq_flags &= ~SQ_EXCL;
/*
* sq_run_events should have released SQ_EXCL
*/
ASSERT(!(sq->sq_flags & SQ_EXCL));
/*
* If anything happened while we were running the
* events (or was there before), we need to process
* them now. We shouldn't be exclusive sine we
* released the perimeter above (plus, we asserted
* for it).
*/
if (!(sq->sq_flags & SQ_STAYAWAY) && (sq->sq_flags & SQ_QUEUED))
drain_syncq(sq);
else
mutex_exit(SQLOCK(sq));
}
/*
* Perform delayed processing. The caller has to make sure that it is safe
* to enter the syncq (e.g. by checking that none of the SQ_STAYAWAY bits are
* set).
*
* Assume that the caller has NO claims on the syncq. However, a claim
* on the syncq does not indicate that a thread is draining the syncq.
* There may be more claims on the syncq than there are threads draining
* (i.e. #_threads_draining <= sq_count)
*
* drain_syncq has to terminate when one of the SQ_STAYAWAY bits gets set
* in order to preserve qwriter(OUTER) ordering constraints.
*
* sq_putcount only needs to be checked when dispatching the queued
* writer call for CIPUT sync queue, but this is handled in sq_run_events.
*/
void
drain_syncq(syncq_t *sq)
{
queue_t *qp;
uint16_t count;
uint16_t type = sq->sq_type;
uint16_t flags = sq->sq_flags;
boolean_t bg_service = sq->sq_svcflags & SQ_SERVICE;
TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START,
"drain_syncq start:%p", sq);
ASSERT(MUTEX_HELD(SQLOCK(sq)));
ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
sq->sq_oprev == NULL) ||
(sq->sq_outer != NULL && sq->sq_onext != NULL &&
sq->sq_oprev != NULL));
/*
* Drop SQ_SERVICE flag.
*/
if (bg_service)
sq->sq_svcflags &= ~SQ_SERVICE;
/*
* If SQ_EXCL is set, someone else is processing this syncq - let him
* finish the job.
*/
if (flags & SQ_EXCL) {
if (bg_service) {
ASSERT(sq->sq_servcount != 0);
sq->sq_servcount--;
}
mutex_exit(SQLOCK(sq));
return;
}
/*
* This routine can be called by a background thread if
* it was scheduled by a hi-priority thread. SO, if there are
* NOT messages queued, return (remember, we have the SQLOCK,
* and it cannot change until we release it). Wakeup any waiters also.
*/
if (!(flags & SQ_QUEUED)) {
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
if (flags & SQ_WANTEXWAKEUP) {
flags &= ~SQ_WANTEXWAKEUP;
cv_broadcast(&sq->sq_exitwait);
}
sq->sq_flags = flags;
if (bg_service) {
ASSERT(sq->sq_servcount != 0);
sq->sq_servcount--;
}
mutex_exit(SQLOCK(sq));
return;
}
/*
* If this is not a concurrent put perimeter, we need to
* become exclusive to drain. Also, if not CIPUT, we would
* not have acquired a putlock, so we don't need to check
* the putcounts. If not entering with a claim, we test
* for sq_count == 0.
*/
type = sq->sq_type;
if (!(type & SQ_CIPUT)) {
if (sq->sq_count > 1) {
if (bg_service) {
ASSERT(sq->sq_servcount != 0);
sq->sq_servcount--;
}
mutex_exit(SQLOCK(sq));
return;
}
sq->sq_flags |= SQ_EXCL;
}
/*
* This is where we make a claim to the syncq.
* This can either be done by incrementing a putlock, or
* the sq_count. But since we already have the SQLOCK
* here, we just bump the sq_count.
*
* Note that after we make a claim, we need to let the code
* fall through to the end of this routine to clean itself
* up. A return in the while loop will put the syncq in a
* very bad state.
*/
sq->sq_count++;
ASSERT(sq->sq_count != 0); /* wraparound */
while ((flags = sq->sq_flags) & SQ_QUEUED) {
/*
* If we are told to stayaway or went exclusive,
* we are done.
*/
if (flags & (SQ_STAYAWAY)) {
break;
}
/*
* If there are events to run, do so.
* We have one claim to the syncq, so if there are
* more than one, other threads are running.
*/
if (sq->sq_evhead != NULL) {
ASSERT(sq->sq_flags & SQ_EVENTS);
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
if (count > 1) {
SQ_PUTLOCKS_EXIT(sq);
/* Can't upgrade - other threads inside */
break;
}
ASSERT((flags & SQ_EXCL) == 0);
sq->sq_flags = flags | SQ_EXCL;
SQ_PUTLOCKS_EXIT(sq);
/*
* we have the only claim, run the events,
* sq_run_events will clear the SQ_EXCL flag.
*/
sq_run_events(sq);
/*
* If this is a CIPUT perimeter, we need
* to drop the SQ_EXCL flag so we can properly
* continue draining the syncq.
*/
if (type & SQ_CIPUT) {
ASSERT(sq->sq_flags & SQ_EXCL);
sq->sq_flags &= ~SQ_EXCL;
}
/*
* And go back to the beginning just in case
* anything changed while we were away.
*/
ASSERT((sq->sq_flags & SQ_EXCL) || (type & SQ_CIPUT));
continue;
}
ASSERT(sq->sq_evhead == NULL);
ASSERT(!(sq->sq_flags & SQ_EVENTS));
/*
* Find the queue that is not draining.
*
* q_draining is protected by QLOCK which we do not hold.
* But if it was set, then a thread was draining, and if it gets
* cleared, then it was because the thread has successfully
* drained the syncq, or a GOAWAY state occurred. For the GOAWAY
* state to happen, a thread needs the SQLOCK which we hold, and
* if there was such a flag, we would have already seen it.
*/
for (qp = sq->sq_head;
qp != NULL && (qp->q_draining ||
(qp->q_sqflags & Q_SQDRAINING));
qp = qp->q_sqnext)
;
if (qp == NULL)
break;
/*
* We have a queue to work on, and we hold the
* SQLOCK and one claim, call qdrain_syncq.
* This means we need to release the SQLOCK and
* acquire the QLOCK (OK since we have a claim).
* Note that qdrain_syncq will actually dequeue
* this queue from the sq_head list when it is
* convinced all the work is done and release
* the QLOCK before returning.
*/
qp->q_sqflags |= Q_SQDRAINING;
mutex_exit(SQLOCK(sq));
mutex_enter(QLOCK(qp));
qdrain_syncq(sq, qp);
mutex_enter(SQLOCK(sq));
/* The queue is drained */
ASSERT(qp->q_sqflags & Q_SQDRAINING);
qp->q_sqflags &= ~Q_SQDRAINING;
/*
* NOTE: After this point qp should not be used since it may be
* closed.
*/
}
ASSERT(MUTEX_HELD(SQLOCK(sq)));
flags = sq->sq_flags;
/*
* sq->sq_head cannot change because we hold the
* sqlock. However, a thread CAN decide that it is no longer
* going to drain that queue. However, this should be due to
* a GOAWAY state, and we should see that here.
*
* This loop is not very efficient. One solution may be adding a second
* pointer to the "draining" queue, but it is difficult to do when
* queues are inserted in the middle due to priority ordering. Another
* possibility is to yank the queue out of the sq list and put it onto
* the "draining list" and then put it back if it can't be drained.
*/
ASSERT((sq->sq_head == NULL) || (flags & SQ_GOAWAY) ||
(type & SQ_CI) || sq->sq_head->q_draining);
/* Drop SQ_EXCL for non-CIPUT perimeters */
if (!(type & SQ_CIPUT))
flags &= ~SQ_EXCL;
ASSERT((flags & SQ_EXCL) == 0);
/* Wake up any waiters. */
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
if (flags & SQ_WANTEXWAKEUP) {
flags &= ~SQ_WANTEXWAKEUP;
cv_broadcast(&sq->sq_exitwait);
}
sq->sq_flags = flags;
ASSERT(sq->sq_count != 0);
/* Release our claim. */
sq->sq_count--;
if (bg_service) {
ASSERT(sq->sq_servcount != 0);
sq->sq_servcount--;
}
mutex_exit(SQLOCK(sq));
TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END,
"drain_syncq end:%p", sq);
}
/*
*
* qdrain_syncq can be called (currently) from only one of two places:
* drain_syncq
* putnext (or some variation of it).
* and eventually
* qwait(_sig)
*
* If called from drain_syncq, we found it in the list of queues needing
* service, so there is work to be done (or it wouldn't be in the list).
*
* If called from some putnext variation, it was because the
* perimeter is open, but messages are blocking a putnext and
* there is not a thread working on it. Now a thread could start
* working on it while we are getting ready to do so ourself, but
* the thread would set the q_draining flag, and we can spin out.
*
* As for qwait(_sig), I think I shall let it continue to call
* drain_syncq directly (after all, it will get here eventually).
*
* qdrain_syncq has to terminate when:
* - one of the SQ_STAYAWAY bits gets set to preserve qwriter(OUTER) ordering
* - SQ_EVENTS gets set to preserve qwriter(INNER) ordering
*
* ASSUMES:
* One claim
* QLOCK held
* SQLOCK not held
* Will release QLOCK before returning
*/
void
qdrain_syncq(syncq_t *sq, queue_t *q)
{
mblk_t *bp;
#ifdef DEBUG
uint16_t count;
#endif
TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START,
"drain_syncq start:%p", sq);
ASSERT(q->q_syncq == sq);
ASSERT(MUTEX_HELD(QLOCK(q)));
ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
/*
* For non-CIPUT perimeters, we should be called with the exclusive bit
* set already. For CIPUT perimeters, we will be doing a concurrent
* drain, so it better not be set.
*/
ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT)));
ASSERT(!((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL)));
ASSERT((sq->sq_type & SQ_CIPUT) || (sq->sq_flags & SQ_EXCL));
/*
* All outer pointers are set, or none of them are
*/
ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
sq->sq_oprev == NULL) ||
(sq->sq_outer != NULL && sq->sq_onext != NULL &&
sq->sq_oprev != NULL));
#ifdef DEBUG
count = sq->sq_count;
/*
* This is OK without the putlocks, because we have one
* claim either from the sq_count, or a putcount. We could
* get an erroneous value from other counts, but ours won't
* change, so one way or another, we will have at least a
* value of one.
*/
SUM_SQ_PUTCOUNTS(sq, count);
ASSERT(count >= 1);
#endif /* DEBUG */
/*
* The first thing to do is find out if a thread is already draining
* this queue. If so, we are done, just return.
*/
if (q->q_draining) {
mutex_exit(QLOCK(q));
return;
}
/*
* If the perimeter is exclusive, there is nothing we can do right now,
* go away. Note that there is nothing to prevent this case from
* changing right after this check, but the spin-out will catch it.
*/
/* Tell other threads that we are draining this queue */
q->q_draining = 1; /* Protected by QLOCK */
/*
* If there is nothing to do, clear QFULL as necessary. This caters for
* the case where an empty queue was enqueued onto the syncq.
*/
if (q->q_sqhead == NULL) {
ASSERT(q->q_syncqmsgs == 0);
mutex_exit(QLOCK(q));
clr_qfull(q);
mutex_enter(QLOCK(q));
}
/*
* Note that q_sqhead must be re-checked here in case another message
* was enqueued whilst QLOCK was dropped during the call to clr_qfull.
*/
for (bp = q->q_sqhead; bp != NULL; bp = q->q_sqhead) {
/*
* Because we can enter this routine just because a putnext is
* blocked, we need to spin out if the perimeter wants to go
* exclusive as well as just blocked. We need to spin out also
* if events are queued on the syncq.
* Don't check for SQ_EXCL, because non-CIPUT perimeters would
* set it, and it can't become exclusive while we hold a claim.
*/
if (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS)) {
break;
}
#ifdef DEBUG
/*
* Since we are in qdrain_syncq, we already know the queue,
* but for sanity, we want to check this against the qp that
* was passed in by bp->b_queue.
*/
ASSERT(bp->b_queue == q);
ASSERT(bp->b_queue->q_syncq == sq);
bp->b_queue = NULL;
/*
* We would have the following check in the DEBUG code:
*
* if (bp->b_prev != NULL) {
* ASSERT(bp->b_prev == (void (*)())q->q_qinfo->qi_putp);
* }
*
* This can't be done, however, since IP modifies qinfo
* structure at run-time (switching between IPv4 qinfo and IPv6
* qinfo), invalidating the check.
* So the assignment to func is left here, but the ASSERT itself
* is removed until the whole issue is resolved.
*/
#endif
ASSERT(q->q_sqhead == bp);
q->q_sqhead = bp->b_next;
bp->b_prev = bp->b_next = NULL;
ASSERT(q->q_syncqmsgs > 0);
mutex_exit(QLOCK(q));
ASSERT(bp->b_datap->db_ref != 0);
(void) (*q->q_qinfo->qi_putp)(q, bp);
mutex_enter(QLOCK(q));
/*
* q_syncqmsgs should only be decremented after executing the
* put procedure to avoid message re-ordering. This is due to an
* optimisation in putnext() which can call the put procedure
* directly if it sees q_syncqmsgs == 0 (despite Q_SQQUEUED
* being set).
*
* We also need to clear QFULL in the next service procedure
* queue if this is the last message destined for that queue.
*
* It would make better sense to have some sort of tunable for
* the low water mark, but these semantics are not yet defined.
* So, alas, we use a constant.
*/
if (--q->q_syncqmsgs == 0) {
mutex_exit(QLOCK(q));
clr_qfull(q);
mutex_enter(QLOCK(q));
}
/*
* Always clear SQ_EXCL when CIPUT in order to handle
* qwriter(INNER). The putp() can call qwriter and get exclusive
* access IFF this is the only claim. So, we need to test for
* this possibility, acquire the mutex and clear the bit.
*/
if ((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL)) {
mutex_enter(SQLOCK(sq));
sq->sq_flags &= ~SQ_EXCL;
mutex_exit(SQLOCK(sq));
}
}
/*
* We should either have no messages on this queue, or we were told to
* goaway by a waiter (which we will wake up at the end of this
* function).
*/
ASSERT((q->q_sqhead == NULL) ||
(sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS)));
ASSERT(MUTEX_HELD(QLOCK(q)));
ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
/* Remove the q from the syncq list if all the messages are drained. */
if (q->q_sqhead == NULL) {
ASSERT(q->q_syncqmsgs == 0);
mutex_enter(SQLOCK(sq));
if (q->q_sqflags & Q_SQQUEUED)
SQRM_Q(sq, q);
mutex_exit(SQLOCK(sq));
/*
* Since the queue is removed from the list, reset its priority.
*/
q->q_spri = 0;
}
/*
* Remember, the q_draining flag is used to let another thread know
* that there is a thread currently draining the messages for a queue.
* Since we are now done with this queue (even if there may be messages
* still there), we need to clear this flag so some thread will work on
* it if needed.
*/
ASSERT(q->q_draining);
q->q_draining = 0;
/* Called with a claim, so OK to drop all locks. */
mutex_exit(QLOCK(q));
TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END,
"drain_syncq end:%p", sq);
}
/* END OF QDRAIN_SYNCQ */
/*
* This is the mate to qdrain_syncq, except that it is putting the message onto
* the queue instead of draining. Since the message is destined for the queue
* that is selected, there is no need to identify the function because the
* message is intended for the put routine for the queue. For debug kernels,
* this routine will do it anyway just in case.
*
* After the message is enqueued on the syncq, it calls putnext_tail()
* which will schedule a background thread to actually process the message.
*
* Assumes that there is a claim on the syncq (sq->sq_count > 0) and
* SQLOCK(sq) and QLOCK(q) are not held.
*/
void
qfill_syncq(syncq_t *sq, queue_t *q, mblk_t *mp)
{
ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
ASSERT(MUTEX_NOT_HELD(QLOCK(q)));
ASSERT(sq->sq_count > 0);
ASSERT(q->q_syncq == sq);
ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
sq->sq_oprev == NULL) ||
(sq->sq_outer != NULL && sq->sq_onext != NULL &&
sq->sq_oprev != NULL));
mutex_enter(QLOCK(q));
#ifdef DEBUG
/*
* This is used for debug in the qfill_syncq/qdrain_syncq case
* to trace the queue that the message is intended for. Note
* that the original use was to identify the queue and function
* to call on the drain. In the new syncq, we have the context
* of the queue that we are draining, so call it's putproc and
* don't rely on the saved values. But for debug this is still
* useful information.
*/
mp->b_prev = (mblk_t *)q->q_qinfo->qi_putp;
mp->b_queue = q;
mp->b_next = NULL;
#endif
ASSERT(q->q_syncq == sq);
/*
* Enqueue the message on the list.
* SQPUT_MP() accesses q_syncqmsgs. We are already holding QLOCK to
* protect it. So it's ok to acquire SQLOCK after SQPUT_MP().
*/
SQPUT_MP(q, mp);
mutex_enter(SQLOCK(sq));
/*
* And queue on syncq for scheduling, if not already queued.
* Note that we need the SQLOCK for this, and for testing flags
* at the end to see if we will drain. So grab it now, and
* release it before we call qdrain_syncq or return.
*/
if (!(q->q_sqflags & Q_SQQUEUED)) {
q->q_spri = curthread->t_pri;
SQPUT_Q(sq, q);
}
#ifdef DEBUG
else {
/*
* All of these conditions MUST be true!
*/
ASSERT(sq->sq_tail != NULL);
if (sq->sq_tail == sq->sq_head) {
ASSERT((q->q_sqprev == NULL) &&
(q->q_sqnext == NULL));
} else {
ASSERT((q->q_sqprev != NULL) ||
(q->q_sqnext != NULL));
}
ASSERT(sq->sq_flags & SQ_QUEUED);
ASSERT(q->q_syncqmsgs != 0);
ASSERT(q->q_sqflags & Q_SQQUEUED);
}
#endif
mutex_exit(QLOCK(q));
/*
* SQLOCK is still held, so sq_count can be safely decremented.
*/
sq->sq_count--;
putnext_tail(sq, q, 0);
/* Should not reference sq or q after this point. */
}
/* End of qfill_syncq */
/*
* Remove all messages from a syncq (if qp is NULL) or remove all messages
* that would be put into qp by drain_syncq.
* Used when deleting the syncq (qp == NULL) or when detaching
* a queue (qp != NULL).
* Return non-zero if one or more messages were freed.
*
* No need to grab sq_putlocks here. See comment in strsubr.h that explains when
* sq_putlocks are used.
*
* NOTE: This function assumes that it is called from the close() context and
* that all the queues in the syncq are going away. For this reason it doesn't
* acquire QLOCK for modifying q_sqhead/q_sqtail fields. This assumption is
* currently valid, but it is useful to rethink this function to behave properly
* in other cases.
*/
int
flush_syncq(syncq_t *sq, queue_t *qp)
{
mblk_t *bp, *mp_head, *mp_next, *mp_prev;
queue_t *q;
int ret = 0;
mutex_enter(SQLOCK(sq));
/*
* Before we leave, we need to make sure there are no
* events listed for this queue. All events for this queue
* will just be freed.
*/
if (qp != NULL && sq->sq_evhead != NULL) {
ASSERT(sq->sq_flags & SQ_EVENTS);
mp_prev = NULL;
for (bp = sq->sq_evhead; bp != NULL; bp = mp_next) {
mp_next = bp->b_next;
if (bp->b_queue == qp) {
/* Delete this message */
if (mp_prev != NULL) {
mp_prev->b_next = mp_next;
/*
* Update sq_evtail if the last element
* is removed.
*/
if (bp == sq->sq_evtail) {
ASSERT(mp_next == NULL);
sq->sq_evtail = mp_prev;
}
} else
sq->sq_evhead = mp_next;
if (sq->sq_evhead == NULL)
sq->sq_flags &= ~SQ_EVENTS;
bp->b_prev = bp->b_next = NULL;
freemsg(bp);
ret++;
} else {
mp_prev = bp;
}
}
}
/*
* Walk sq_head and:
* - match qp if qp is set, remove it's messages
* - all if qp is not set
*/
q = sq->sq_head;
while (q != NULL) {
ASSERT(q->q_syncq == sq);
if ((qp == NULL) || (qp == q)) {
/*
* Yank the messages as a list off the queue
*/
mp_head = q->q_sqhead;
/*
* We do not have QLOCK(q) here (which is safe due to
* assumptions mentioned above). To obtain the lock we
* need to release SQLOCK which may allow lots of things
* to change upon us. This place requires more analysis.
*/
q->q_sqhead = q->q_sqtail = NULL;
ASSERT(mp_head->b_queue &&
mp_head->b_queue->q_syncq == sq);
/*
* Free each of the messages.
*/
for (bp = mp_head; bp != NULL; bp = mp_next) {
mp_next = bp->b_next;
bp->b_prev = bp->b_next = NULL;
freemsg(bp);
ret++;
}
/*
* Now remove the queue from the syncq.
*/
ASSERT(q->q_sqflags & Q_SQQUEUED);
SQRM_Q(sq, q);
q->q_spri = 0;
q->q_syncqmsgs = 0;
/*
* If qp was specified, we are done with it and are
* going to drop SQLOCK(sq) and return. We wakeup syncq
* waiters while we still have the SQLOCK.
*/
if ((qp != NULL) && (sq->sq_flags & SQ_WANTWAKEUP)) {
sq->sq_flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
/* Drop SQLOCK across clr_qfull */
mutex_exit(SQLOCK(sq));
/*
* We avoid doing the test that drain_syncq does and
* unconditionally clear qfull for every flushed
* message. Since flush_syncq is only called during
* close this should not be a problem.
*/
clr_qfull(q);
if (qp != NULL) {
return (ret);
} else {
mutex_enter(SQLOCK(sq));
/*
* The head was removed by SQRM_Q above.
* reread the new head and flush it.
*/
q = sq->sq_head;
}
} else {
q = q->q_sqnext;
}
ASSERT(MUTEX_HELD(SQLOCK(sq)));
}
if (sq->sq_flags & SQ_WANTWAKEUP) {
sq->sq_flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
mutex_exit(SQLOCK(sq));
return (ret);
}
/*
* Propagate all messages from a syncq to the next syncq that are associated
* with the specified queue. If the queue is attached to a driver or if the
* messages have been added due to a qwriter(PERIM_INNER), free the messages.
*
* Assumes that the stream is strlock()'ed. We don't come here if there
* are no messages to propagate.
*
* NOTE : If the queue is attached to a driver, all the messages are freed
* as there is no point in propagating the messages from the driver syncq
* to the closing stream head which will in turn get freed later.
*/
static int
propagate_syncq(queue_t *qp)
{
mblk_t *bp, *head, *tail, *prev, *next;
syncq_t *sq;
queue_t *nqp;
syncq_t *nsq;
boolean_t isdriver;
int moved = 0;
uint16_t flags;
pri_t priority = curthread->t_pri;
#ifdef DEBUG
void (*func)();
#endif
sq = qp->q_syncq;
ASSERT(MUTEX_HELD(SQLOCK(sq)));
/* debug macro */
SQ_PUTLOCKS_HELD(sq);
/*
* As entersq() does not increment the sq_count for
* the write side, check sq_count for non-QPERQ
* perimeters alone.
*/
ASSERT((qp->q_flag & QPERQ) || (sq->sq_count >= 1));
/*
* propagate_syncq() can be called because of either messages on the
* queue syncq or because on events on the queue syncq. Do actual
* message propagations if there are any messages.
*/
if (qp->q_syncqmsgs) {
isdriver = (qp->q_flag & QISDRV);
if (!isdriver) {
nqp = qp->q_next;
nsq = nqp->q_syncq;
ASSERT(MUTEX_HELD(SQLOCK(nsq)));
/* debug macro */
SQ_PUTLOCKS_HELD(nsq);
#ifdef DEBUG
func = (void (*)())nqp->q_qinfo->qi_putp;
#endif
}
SQRM_Q(sq, qp);
priority = MAX(qp->q_spri, priority);
qp->q_spri = 0;
head = qp->q_sqhead;
tail = qp->q_sqtail;
qp->q_sqhead = qp->q_sqtail = NULL;
qp->q_syncqmsgs = 0;
/*
* Walk the list of messages, and free them if this is a driver,
* otherwise reset the b_prev and b_queue value to the new putp.
* Afterward, we will just add the head to the end of the next
* syncq, and point the tail to the end of this one.
*/
for (bp = head; bp != NULL; bp = next) {
next = bp->b_next;
if (isdriver) {
bp->b_prev = bp->b_next = NULL;
freemsg(bp);
continue;
}
/* Change the q values for this message */
bp->b_queue = nqp;
#ifdef DEBUG
bp->b_prev = (mblk_t *)func;
#endif
moved++;
}
/*
* Attach list of messages to the end of the new queue (if there
* is a list of messages).
*/
if (!isdriver && head != NULL) {
ASSERT(tail != NULL);
if (nqp->q_sqhead == NULL) {
nqp->q_sqhead = head;
} else {
ASSERT(nqp->q_sqtail != NULL);
nqp->q_sqtail->b_next = head;
}
nqp->q_sqtail = tail;
/*
* When messages are moved from high priority queue to
* another queue, the destination queue priority is
* upgraded.
*/
if (priority > nqp->q_spri)
nqp->q_spri = priority;
SQPUT_Q(nsq, nqp);
nqp->q_syncqmsgs += moved;
ASSERT(nqp->q_syncqmsgs != 0);
}
}
/*
* Before we leave, we need to make sure there are no
* events listed for this queue. All events for this queue
* will just be freed.
*/
if (sq->sq_evhead != NULL) {
ASSERT(sq->sq_flags & SQ_EVENTS);
prev = NULL;
for (bp = sq->sq_evhead; bp != NULL; bp = next) {
next = bp->b_next;
if (bp->b_queue == qp) {
/* Delete this message */
if (prev != NULL) {
prev->b_next = next;
/*
* Update sq_evtail if the last element
* is removed.
*/
if (bp == sq->sq_evtail) {
ASSERT(next == NULL);
sq->sq_evtail = prev;
}
} else
sq->sq_evhead = next;
if (sq->sq_evhead == NULL)
sq->sq_flags &= ~SQ_EVENTS;
bp->b_prev = bp->b_next = NULL;
freemsg(bp);
} else {
prev = bp;
}
}
}
flags = sq->sq_flags;
/* Wake up any waiter before leaving. */
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
sq->sq_flags = flags;
return (moved);
}
/*
* Try and upgrade to exclusive access at the inner perimeter. If this can
* not be done without blocking then request will be queued on the syncq
* and drain_syncq will run it later.
*
* This routine can only be called from put or service procedures plus
* asynchronous callback routines that have properly entered the queue (with
* entersq). Thus qwriter_inner assumes the caller has one claim on the syncq
* associated with q.
*/
void
qwriter_inner(queue_t *q, mblk_t *mp, void (*func)())
{
syncq_t *sq = q->q_syncq;
uint16_t count;
mutex_enter(SQLOCK(sq));
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
ASSERT(count >= 1);
ASSERT(sq->sq_type & (SQ_CIPUT|SQ_CISVC));
if (count == 1) {
/*
* Can upgrade. This case also handles nested qwriter calls
* (when the qwriter callback function calls qwriter). In that
* case SQ_EXCL is already set.
*/
sq->sq_flags |= SQ_EXCL;
SQ_PUTLOCKS_EXIT(sq);
mutex_exit(SQLOCK(sq));
(*func)(q, mp);
/*
* Assumes that leavesq, putnext, and drain_syncq will reset
* SQ_EXCL for SQ_CIPUT/SQ_CISVC queues. We leave SQ_EXCL on
* until putnext, leavesq, or drain_syncq drops it.
* That way we handle nested qwriter(INNER) without dropping
* SQ_EXCL until the outermost qwriter callback routine is
* done.
*/
return;
}
SQ_PUTLOCKS_EXIT(sq);
sqfill_events(sq, q, mp, func);
}
/*
* Synchronous callback support functions
*/
/*
* Allocate a callback parameter structure.
* Assumes that caller initializes the flags and the id.
* Acquires SQLOCK(sq) if non-NULL is returned.
*/
callbparams_t *
callbparams_alloc(syncq_t *sq, void (*func)(void *), void *arg, int kmflags)
{
callbparams_t *cbp;
size_t size = sizeof (callbparams_t);
cbp = kmem_alloc(size, kmflags & ~KM_PANIC);
/*
* Only try tryhard allocation if the caller is ready to panic.
* Otherwise just fail.
*/
if (cbp == NULL) {
if (kmflags & KM_PANIC)
cbp = kmem_alloc_tryhard(sizeof (callbparams_t),
&size, kmflags);
else
return (NULL);
}
ASSERT(size >= sizeof (callbparams_t));
cbp->cbp_size = size;
cbp->cbp_sq = sq;
cbp->cbp_func = func;
cbp->cbp_arg = arg;
mutex_enter(SQLOCK(sq));
cbp->cbp_next = sq->sq_callbpend;
sq->sq_callbpend = cbp;
return (cbp);
}
void
callbparams_free(syncq_t *sq, callbparams_t *cbp)
{
callbparams_t **pp, *p;
ASSERT(MUTEX_HELD(SQLOCK(sq)));
for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) {
if (p == cbp) {
*pp = p->cbp_next;
kmem_free(p, p->cbp_size);
return;
}
}
(void) (STRLOG(0, 0, 0, SL_CONSOLE,
"callbparams_free: not found\n"));
}
void
callbparams_free_id(syncq_t *sq, callbparams_id_t id, int32_t flag)
{
callbparams_t **pp, *p;
ASSERT(MUTEX_HELD(SQLOCK(sq)));
for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) {
if (p->cbp_id == id && p->cbp_flags == flag) {
*pp = p->cbp_next;
kmem_free(p, p->cbp_size);
return;
}
}
(void) (STRLOG(0, 0, 0, SL_CONSOLE,
"callbparams_free_id: not found\n"));
}
/*
* Callback wrapper function used by once-only callbacks that can be
* cancelled (qtimeout and qbufcall)
* Contains inline version of entersq(sq, SQ_CALLBACK) that can be
* cancelled by the qun* functions.
*/
void
qcallbwrapper(void *arg)
{
callbparams_t *cbp = arg;
syncq_t *sq;
uint16_t count = 0;
uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL;
uint16_t type;
sq = cbp->cbp_sq;
mutex_enter(SQLOCK(sq));
type = sq->sq_type;
if (!(type & SQ_CICB)) {
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
SUM_SQ_PUTCOUNTS(sq, count);
sq->sq_needexcl++;
ASSERT(sq->sq_needexcl != 0); /* wraparound */
waitflags |= SQ_MESSAGES;
}
/* Can not handle exclusive entry at outer perimeter */
ASSERT(type & SQ_COCB);
while ((sq->sq_flags & waitflags) || (!(type & SQ_CICB) &&count != 0)) {
if ((sq->sq_callbflags & cbp->cbp_flags) &&
(sq->sq_cancelid == cbp->cbp_id)) {
/* timeout has been cancelled */
sq->sq_callbflags |= SQ_CALLB_BYPASSED;
callbparams_free(sq, cbp);
if (!(type & SQ_CICB)) {
ASSERT(sq->sq_needexcl > 0);
sq->sq_needexcl--;
if (sq->sq_needexcl == 0) {
SQ_PUTCOUNT_SETFAST_LOCKED(sq);
}
SQ_PUTLOCKS_EXIT(sq);
}
mutex_exit(SQLOCK(sq));
return;
}
sq->sq_flags |= SQ_WANTWAKEUP;
if (!(type & SQ_CICB)) {
SQ_PUTLOCKS_EXIT(sq);
}
cv_wait(&sq->sq_wait, SQLOCK(sq));
if (!(type & SQ_CICB)) {
count = sq->sq_count;
SQ_PUTLOCKS_ENTER(sq);
SUM_SQ_PUTCOUNTS(sq, count);
}
}
sq->sq_count++;
ASSERT(sq->sq_count != 0); /* Wraparound */
if (!(type & SQ_CICB)) {
ASSERT(count == 0);
sq->sq_flags |= SQ_EXCL;
ASSERT(sq->sq_needexcl > 0);
sq->sq_needexcl--;
if (sq->sq_needexcl == 0) {
SQ_PUTCOUNT_SETFAST_LOCKED(sq);
}
SQ_PUTLOCKS_EXIT(sq);
}
mutex_exit(SQLOCK(sq));
cbp->cbp_func(cbp->cbp_arg);
/*
* We drop the lock only for leavesq to re-acquire it.
* Possible optimization is inline of leavesq.
*/
mutex_enter(SQLOCK(sq));
callbparams_free(sq, cbp);
mutex_exit(SQLOCK(sq));
leavesq(sq, SQ_CALLBACK);
}
/*
* No need to grab sq_putlocks here. See comment in strsubr.h that
* explains when sq_putlocks are used.
*
* sq_count (or one of the sq_putcounts) has already been
* decremented by the caller, and if SQ_QUEUED, we need to call
* drain_syncq (the global syncq drain).
* If putnext_tail is called with the SQ_EXCL bit set, we are in
* one of two states, non-CIPUT perimeter, and we need to clear
* it, or we went exclusive in the put procedure. In any case,
* we want to clear the bit now, and it is probably easier to do
* this at the beginning of this function (remember, we hold
* the SQLOCK). Lastly, if there are other messages queued
* on the syncq (and not for our destination), enable the syncq
* for background work.
*/
/* ARGSUSED */
void
putnext_tail(syncq_t *sq, queue_t *qp, uint32_t passflags)
{
uint16_t flags = sq->sq_flags;
ASSERT(MUTEX_HELD(SQLOCK(sq)));
ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
/* Clear SQ_EXCL if set in passflags */
if (passflags & SQ_EXCL) {
flags &= ~SQ_EXCL;
}
if (flags & SQ_WANTWAKEUP) {
flags &= ~SQ_WANTWAKEUP;
cv_broadcast(&sq->sq_wait);
}
if (flags & SQ_WANTEXWAKEUP) {
flags &= ~SQ_WANTEXWAKEUP;
cv_broadcast(&sq->sq_exitwait);
}
sq->sq_flags = flags;
/*
* We have cleared SQ_EXCL if we were asked to, and started
* the wakeup process for waiters. If there are no writers
* then we need to drain the syncq if we were told to, or
* enable the background thread to do it.
*/
if (!(flags & (SQ_STAYAWAY|SQ_EXCL))) {
if ((passflags & SQ_QUEUED) ||
(sq->sq_svcflags & SQ_DISABLED)) {
/* drain_syncq will take care of events in the list */
drain_syncq(sq);
return;
} else if (flags & SQ_QUEUED) {
sqenable(sq);
}
}
/* Drop the SQLOCK on exit */
mutex_exit(SQLOCK(sq));
TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
"putnext_end:(%p, %p, %p) done", NULL, qp, sq);
}
void
set_qend(queue_t *q)
{
mutex_enter(QLOCK(q));
if (!O_SAMESTR(q))
q->q_flag |= QEND;
else
q->q_flag &= ~QEND;
mutex_exit(QLOCK(q));
q = _OTHERQ(q);
mutex_enter(QLOCK(q));
if (!O_SAMESTR(q))
q->q_flag |= QEND;
else
q->q_flag &= ~QEND;
mutex_exit(QLOCK(q));
}
/*
* Set QFULL in next service procedure queue (that cares) if not already
* set and if there are already more messages on the syncq than
* sq_max_size. If sq_max_size is 0, no flow control will be asserted on
* any syncq.
*
* The fq here is the next queue with a service procedure. This is where
* we would fail canputnext, so this is where we need to set QFULL.
* In the case when fq != q we need to take QLOCK(fq) to set QFULL flag.
*
* We already have QLOCK at this point. To avoid cross-locks with
* freezestr() which grabs all QLOCKs and with strlock() which grabs both
* SQLOCK and sd_reflock, we need to drop respective locks first.
*/
void
set_qfull(queue_t *q)
{
queue_t *fq = NULL;
ASSERT(MUTEX_HELD(QLOCK(q)));
if ((sq_max_size != 0) && (!(q->q_nfsrv->q_flag & QFULL)) &&
(q->q_syncqmsgs > sq_max_size)) {
if ((fq = q->q_nfsrv) == q) {
fq->q_flag |= QFULL;
} else {
mutex_exit(QLOCK(q));
mutex_enter(QLOCK(fq));
fq->q_flag |= QFULL;
mutex_exit(QLOCK(fq));
mutex_enter(QLOCK(q));
}
}
}
void
clr_qfull(queue_t *q)
{
queue_t *oq = q;
q = q->q_nfsrv;
/* Fast check if there is any work to do before getting the lock. */
if ((q->q_flag & (QFULL|QWANTW)) == 0) {
return;
}
/*
* Do not reset QFULL (and backenable) if the q_count is the reason
* for QFULL being set.
*/
mutex_enter(QLOCK(q));
/*
* If queue is empty i.e q_mblkcnt is zero, queue can not be full.
* Hence clear the QFULL.
* If both q_count and q_mblkcnt are less than the hiwat mark,
* clear the QFULL.
*/
if (q->q_mblkcnt == 0 || ((q->q_count < q->q_hiwat) &&
(q->q_mblkcnt < q->q_hiwat))) {
q->q_flag &= ~QFULL;
/*
* A little more confusing, how about this way:
* if someone wants to write,
* AND
* both counts are less than the lowat mark
* OR
* the lowat mark is zero
* THEN
* backenable
*/
if ((q->q_flag & QWANTW) &&
(((q->q_count < q->q_lowat) &&
(q->q_mblkcnt < q->q_lowat)) || q->q_lowat == 0)) {
q->q_flag &= ~QWANTW;
mutex_exit(QLOCK(q));
backenable(oq, 0);
} else
mutex_exit(QLOCK(q));
} else
mutex_exit(QLOCK(q));
}
/*
* Set the forward service procedure pointer.
*
* Called at insert-time to cache a queue's next forward service procedure in
* q_nfsrv; used by canput() and canputnext(). If the queue to be inserted
* has a service procedure then q_nfsrv points to itself. If the queue to be
* inserted does not have a service procedure, then q_nfsrv points to the next
* queue forward that has a service procedure. If the queue is at the logical
* end of the stream (driver for write side, stream head for the read side)
* and does not have a service procedure, then q_nfsrv also points to itself.
*/
void
set_nfsrv_ptr(
queue_t *rnew, /* read queue pointer to new module */
queue_t *wnew, /* write queue pointer to new module */
queue_t *prev_rq, /* read queue pointer to the module above */
queue_t *prev_wq) /* write queue pointer to the module above */
{
queue_t *qp;
if (prev_wq->q_next == NULL) {
/*
* Insert the driver, initialize the driver and stream head.
* In this case, prev_rq/prev_wq should be the stream head.
* _I_INSERT does not allow inserting a driver. Make sure
* that it is not an insertion.
*/
ASSERT(!(rnew->q_flag & _QINSERTING));
wnew->q_nfsrv = wnew;
if (rnew->q_qinfo->qi_srvp)
rnew->q_nfsrv = rnew;
else
rnew->q_nfsrv = prev_rq;
prev_rq->q_nfsrv = prev_rq;
prev_wq->q_nfsrv = prev_wq;
} else {
/*
* set up read side q_nfsrv pointer. This MUST be done
* before setting the write side, because the setting of
* the write side for a fifo may depend on it.
*
* Suppose we have a fifo that only has pipemod pushed.
* pipemod has no read or write service procedures, so
* nfsrv for both pipemod queues points to prev_rq (the
* stream read head). Now push bufmod (which has only a
* read service procedure). Doing the write side first,
* wnew->q_nfsrv is set to pipemod's writeq nfsrv, which
* is WRONG; the next queue forward from wnew with a
* service procedure will be rnew, not the stream read head.
* Since the downstream queue (which in the case of a fifo
* is the read queue rnew) can affect upstream queues, it
* needs to be done first. Setting up the read side first
* sets nfsrv for both pipemod queues to rnew and then
* when the write side is set up, wnew-q_nfsrv will also
* point to rnew.
*/
if (rnew->q_qinfo->qi_srvp) {
/*
* use _OTHERQ() because, if this is a pipe, next
* module may have been pushed from other end and
* q_next could be a read queue.
*/
qp = _OTHERQ(prev_wq->q_next);
while (qp && qp->q_nfsrv != qp) {
qp->q_nfsrv = rnew;
qp = backq(qp);
}
rnew->q_nfsrv = rnew;
} else
rnew->q_nfsrv = prev_rq->q_nfsrv;
/* set up write side q_nfsrv pointer */
if (wnew->q_qinfo->qi_srvp) {
wnew->q_nfsrv = wnew;
/*
* For insertion, need to update nfsrv of the modules
* above which do not have a service routine.
*/
if (rnew->q_flag & _QINSERTING) {
for (qp = prev_wq;
qp != NULL && qp->q_nfsrv != qp;
qp = backq(qp)) {
qp->q_nfsrv = wnew->q_nfsrv;
}
}
} else {
if (prev_wq->q_next == prev_rq)
/*
* Since prev_wq/prev_rq are the middle of a
* fifo, wnew/rnew will also be the middle of
* a fifo and wnew's nfsrv is same as rnew's.
*/
wnew->q_nfsrv = rnew->q_nfsrv;
else
wnew->q_nfsrv = prev_wq->q_next->q_nfsrv;
}
}
}
/*
* Reset the forward service procedure pointer; called at remove-time.
*/
void
reset_nfsrv_ptr(queue_t *rqp, queue_t *wqp)
{
queue_t *tmp_qp;
/* Reset the write side q_nfsrv pointer for _I_REMOVE */
if ((rqp->q_flag & _QREMOVING) && (wqp->q_qinfo->qi_srvp != NULL)) {
for (tmp_qp = backq(wqp);
tmp_qp != NULL && tmp_qp->q_nfsrv == wqp;
tmp_qp = backq(tmp_qp)) {
tmp_qp->q_nfsrv = wqp->q_nfsrv;
}
}
/* reset the read side q_nfsrv pointer */
if (rqp->q_qinfo->qi_srvp) {
if (wqp->q_next) { /* non-driver case */
tmp_qp = _OTHERQ(wqp->q_next);
while (tmp_qp && tmp_qp->q_nfsrv == rqp) {
/* Note that rqp->q_next cannot be NULL */
ASSERT(rqp->q_next != NULL);
tmp_qp->q_nfsrv = rqp->q_next->q_nfsrv;
tmp_qp = backq(tmp_qp);
}
}
}
}
/*
* This routine should be called after all stream geometry changes to update
* the stream head cached struio() rd/wr queue pointers. Note must be called
* with the streamlock()ed.
*
* Note: only enables Synchronous STREAMS for a side of a Stream which has
* an explicit synchronous barrier module queue. That is, a queue that
* has specified a struio() type.
*/
static void
strsetuio(stdata_t *stp)
{
queue_t *wrq;
if (stp->sd_flag & STPLEX) {
/*
* Not streamhead, but a mux, so no Synchronous STREAMS.
*/
stp->sd_struiowrq = NULL;
stp->sd_struiordq = NULL;
return;
}
/*
* Scan the write queue(s) while synchronous
* until we find a qinfo uio type specified.
*/
wrq = stp->sd_wrq->q_next;
while (wrq) {
if (wrq->q_struiot == STRUIOT_NONE) {
wrq = 0;
break;
}
if (wrq->q_struiot != STRUIOT_DONTCARE)
break;
if (! _SAMESTR(wrq)) {
wrq = 0;
break;
}
wrq = wrq->q_next;
}
stp->sd_struiowrq = wrq;
/*
* Scan the read queue(s) while synchronous
* until we find a qinfo uio type specified.
*/
wrq = stp->sd_wrq->q_next;
while (wrq) {
if (_RD(wrq)->q_struiot == STRUIOT_NONE) {
wrq = 0;
break;
}
if (_RD(wrq)->q_struiot != STRUIOT_DONTCARE)
break;
if (! _SAMESTR(wrq)) {
wrq = 0;
break;
}
wrq = wrq->q_next;
}
stp->sd_struiordq = wrq ? _RD(wrq) : 0;
}
/*
* pass_wput, unblocks the passthru queues, so that
* messages can arrive at muxs lower read queue, before
* I_LINK/I_UNLINK is acked/nacked.
*/
static void
pass_wput(queue_t *q, mblk_t *mp)
{
syncq_t *sq;
sq = _RD(q)->q_syncq;
if (sq->sq_flags & SQ_BLOCKED)
unblocksq(sq, SQ_BLOCKED, 0);
putnext(q, mp);
}
/*
* Set up queues for the link/unlink.
* Create a new queue and block it and then insert it
* below the stream head on the lower stream.
* This prevents any messages from arriving during the setq
* as well as while the mux is processing the LINK/I_UNLINK.
* The blocked passq is unblocked once the LINK/I_UNLINK has
* been acked or nacked or if a message is generated and sent
* down muxs write put procedure.
* See pass_wput().
*
* After the new queue is inserted, all messages coming from below are
* blocked. The call to strlock will ensure that all activity in the stream head
* read queue syncq is stopped (sq_count drops to zero).
*/
static queue_t *
link_addpassthru(stdata_t *stpdown)
{
queue_t *passq;
sqlist_t sqlist;
passq = allocq();
STREAM(passq) = STREAM(_WR(passq)) = stpdown;
/* setq might sleep in allocator - avoid holding locks. */
setq(passq, &passthru_rinit, &passthru_winit, NULL, QPERQ,
SQ_CI|SQ_CO, B_FALSE);
claimq(passq);
blocksq(passq->q_syncq, SQ_BLOCKED, 1);
insertq(STREAM(passq), passq);
/*
* Use strlock() to wait for the stream head sq_count to drop to zero
* since we are going to change q_ptr in the stream head. Note that
* insertq() doesn't wait for any syncq counts to drop to zero.
*/
sqlist.sqlist_head = NULL;
sqlist.sqlist_index = 0;
sqlist.sqlist_size = sizeof (sqlist_t);
sqlist_insert(&sqlist, _RD(stpdown->sd_wrq)->q_syncq);
strlock(stpdown, &sqlist);
strunlock(stpdown, &sqlist);
releaseq(passq);
return (passq);
}
/*
* Let messages flow up into the mux by removing
* the passq.
*/
static void
link_rempassthru(queue_t *passq)
{
claimq(passq);
removeq(passq);
releaseq(passq);
freeq(passq);
}
/*
* Wait for the condition variable pointed to by `cvp' to be signaled,
* or for `tim' milliseconds to elapse, whichever comes first. If `tim'
* is negative, then there is no time limit. If `nosigs' is non-zero,
* then the wait will be non-interruptible.
*
* Returns >0 if signaled, 0 if interrupted, or -1 upon timeout.
*/
clock_t
str_cv_wait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim, int nosigs)
{
clock_t ret, now, tick;
if (tim < 0) {
if (nosigs) {
cv_wait(cvp, mp);
ret = 1;
} else {
ret = cv_wait_sig(cvp, mp);
}
} else if (tim > 0) {
/*
* convert milliseconds to clock ticks
*/
tick = MSEC_TO_TICK_ROUNDUP(tim);
time_to_wait(&now, tick);
if (nosigs) {
ret = cv_timedwait(cvp, mp, now);
} else {
ret = cv_timedwait_sig(cvp, mp, now);
}
} else {
ret = -1;
}
return (ret);
}
/*
* Wait until the stream head can determine if it is at the mark but
* don't wait forever to prevent a race condition between the "mark" state
* in the stream head and any mark state in the caller/user of this routine.
*
* This is used by sockets and for a socket it would be incorrect
* to return a failure for SIOCATMARK when there is no data in the receive
* queue and the marked urgent data is traveling up the stream.
*
* This routine waits until the mark is known by waiting for one of these
* three events:
* The stream head read queue becoming non-empty (including an EOF).
* The STRATMARK flag being set (due to a MSGMARKNEXT message).
* The STRNOTATMARK flag being set (which indicates that the transport
* has sent a MSGNOTMARKNEXT message to indicate that it is not at
* the mark).
*
* The routine returns 1 if the stream is at the mark; 0 if it can
* be determined that the stream is not at the mark.
* If the wait times out and it can't determine
* whether or not the stream might be at the mark the routine will return -1.
*
* Note: This routine should only be used when a mark is pending i.e.,
* in the socket case the SIGURG has been posted.
* Note2: This can not wakeup just because synchronous streams indicate
* that data is available since it is not possible to use the synchronous
* streams interfaces to determine the b_flag value for the data queued below
* the stream head.
*/
int
strwaitmark(vnode_t *vp)
{
struct stdata *stp = vp->v_stream;
queue_t *rq = _RD(stp->sd_wrq);
int mark;
mutex_enter(&stp->sd_lock);
while (rq->q_first == NULL &&
!(stp->sd_flag & (STRATMARK|STRNOTATMARK|STREOF))) {
stp->sd_flag |= RSLEEP;
/* Wait for 100 milliseconds for any state change. */
if (str_cv_wait(&rq->q_wait, &stp->sd_lock, 100, 1) == -1) {
mutex_exit(&stp->sd_lock);
return (-1);
}
}
if (stp->sd_flag & STRATMARK)
mark = 1;
else if (rq->q_first != NULL && (rq->q_first->b_flag & MSGMARK))
mark = 1;
else
mark = 0;
mutex_exit(&stp->sd_lock);
return (mark);
}
/*
* Set a read side error. If persist is set change the socket error
* to persistent. If errfunc is set install the function as the exported
* error handler.
*/
void
strsetrerror(vnode_t *vp, int error, int persist, errfunc_t errfunc)
{
struct stdata *stp = vp->v_stream;
mutex_enter(&stp->sd_lock);
stp->sd_rerror = error;
if (error == 0 && errfunc == NULL)
stp->sd_flag &= ~STRDERR;
else
stp->sd_flag |= STRDERR;
if (persist) {
stp->sd_flag &= ~STRDERRNONPERSIST;
} else {
stp->sd_flag |= STRDERRNONPERSIST;
}
stp->sd_rderrfunc = errfunc;
if (error != 0 || errfunc != NULL) {
cv_broadcast(&_RD(stp->sd_wrq)->q_wait); /* readers */
cv_broadcast(&stp->sd_wrq->q_wait); /* writers */
cv_broadcast(&stp->sd_monitor); /* ioctllers */
mutex_exit(&stp->sd_lock);
pollwakeup(&stp->sd_pollist, POLLERR);
mutex_enter(&stp->sd_lock);
if (stp->sd_sigflags & S_ERROR)
strsendsig(stp->sd_siglist, S_ERROR, 0, error);
}
mutex_exit(&stp->sd_lock);
}
/*
* Set a write side error. If persist is set change the socket error
* to persistent.
*/
void
strsetwerror(vnode_t *vp, int error, int persist, errfunc_t errfunc)
{
struct stdata *stp = vp->v_stream;
mutex_enter(&stp->sd_lock);
stp->sd_werror = error;
if (error == 0 && errfunc == NULL)
stp->sd_flag &= ~STWRERR;
else
stp->sd_flag |= STWRERR;
if (persist) {
stp->sd_flag &= ~STWRERRNONPERSIST;
} else {
stp->sd_flag |= STWRERRNONPERSIST;
}
stp->sd_wrerrfunc = errfunc;
if (error != 0 || errfunc != NULL) {
cv_broadcast(&_RD(stp->sd_wrq)->q_wait); /* readers */
cv_broadcast(&stp->sd_wrq->q_wait); /* writers */
cv_broadcast(&stp->sd_monitor); /* ioctllers */
mutex_exit(&stp->sd_lock);
pollwakeup(&stp->sd_pollist, POLLERR);
mutex_enter(&stp->sd_lock);
if (stp->sd_sigflags & S_ERROR)
strsendsig(stp->sd_siglist, S_ERROR, 0, error);
}
mutex_exit(&stp->sd_lock);
}
/*
* Make the stream return 0 (EOF) when all data has been read.
* No effect on write side.
*/
void
strseteof(vnode_t *vp, int eof)
{
struct stdata *stp = vp->v_stream;
mutex_enter(&stp->sd_lock);
if (!eof) {
stp->sd_flag &= ~STREOF;
mutex_exit(&stp->sd_lock);
return;
}
stp->sd_flag |= STREOF;
if (stp->sd_flag & RSLEEP) {
stp->sd_flag &= ~RSLEEP;
cv_broadcast(&_RD(stp->sd_wrq)->q_wait);
}
mutex_exit(&stp->sd_lock);
pollwakeup(&stp->sd_pollist, POLLIN|POLLRDNORM);
mutex_enter(&stp->sd_lock);
if (stp->sd_sigflags & (S_INPUT|S_RDNORM))
strsendsig(stp->sd_siglist, S_INPUT|S_RDNORM, 0, 0);
mutex_exit(&stp->sd_lock);
}
void
strflushrq(vnode_t *vp, int flag)
{
struct stdata *stp = vp->v_stream;
mutex_enter(&stp->sd_lock);
flushq(_RD(stp->sd_wrq), flag);
mutex_exit(&stp->sd_lock);
}
void
strsetrputhooks(vnode_t *vp, uint_t flags,
msgfunc_t protofunc, msgfunc_t miscfunc)
{
struct stdata *stp = vp->v_stream;
mutex_enter(&stp->sd_lock);
if (protofunc == NULL)
stp->sd_rprotofunc = strrput_proto;
else
stp->sd_rprotofunc = protofunc;
if (miscfunc == NULL)
stp->sd_rmiscfunc = strrput_misc;
else
stp->sd_rmiscfunc = miscfunc;
if (flags & SH_CONSOL_DATA)
stp->sd_rput_opt |= SR_CONSOL_DATA;
else
stp->sd_rput_opt &= ~SR_CONSOL_DATA;
if (flags & SH_SIGALLDATA)
stp->sd_rput_opt |= SR_SIGALLDATA;
else
stp->sd_rput_opt &= ~SR_SIGALLDATA;
if (flags & SH_IGN_ZEROLEN)
stp->sd_rput_opt |= SR_IGN_ZEROLEN;
else
stp->sd_rput_opt &= ~SR_IGN_ZEROLEN;
mutex_exit(&stp->sd_lock);
}
void
strsetwputhooks(vnode_t *vp, uint_t flags, clock_t closetime)
{
struct stdata *stp = vp->v_stream;
mutex_enter(&stp->sd_lock);
stp->sd_closetime = closetime;
if (flags & SH_SIGPIPE)
stp->sd_wput_opt |= SW_SIGPIPE;
else
stp->sd_wput_opt &= ~SW_SIGPIPE;
if (flags & SH_RECHECK_ERR)
stp->sd_wput_opt |= SW_RECHECK_ERR;
else
stp->sd_wput_opt &= ~SW_RECHECK_ERR;
mutex_exit(&stp->sd_lock);
}
void
strsetrwputdatahooks(vnode_t *vp, msgfunc_t rdatafunc, msgfunc_t wdatafunc)
{
struct stdata *stp = vp->v_stream;
mutex_enter(&stp->sd_lock);
stp->sd_rputdatafunc = rdatafunc;
stp->sd_wputdatafunc = wdatafunc;
mutex_exit(&stp->sd_lock);
}
/* Used within framework when the queue is already locked */
void
qenable_locked(queue_t *q)
{
stdata_t *stp = STREAM(q);
ASSERT(MUTEX_HELD(QLOCK(q)));
if (!q->q_qinfo->qi_srvp)
return;
/*
* Do not place on run queue if already enabled or closing.
*/
if (q->q_flag & (QWCLOSE|QENAB))
return;
/*
* mark queue enabled and place on run list if it is not already being
* serviced. If it is serviced, the runservice() function will detect
* that QENAB is set and call service procedure before clearing
* QINSERVICE flag.
*/
q->q_flag |= QENAB;
if (q->q_flag & QINSERVICE)
return;
/* Record the time of qenable */
q->q_qtstamp = lbolt;
/*
* Put the queue in the stp list and schedule it for background
* processing if it is not already scheduled or if stream head does not
* intent to process it in the foreground later by setting
* STRS_WILLSERVICE flag.
*/
mutex_enter(&stp->sd_qlock);
/*
* If there are already something on the list, stp flags should show
* intention to drain it.
*/
IMPLY(STREAM_NEEDSERVICE(stp),
(stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED)));
ENQUEUE(q, stp->sd_qhead, stp->sd_qtail, q_link);
stp->sd_nqueues++;
/*
* If no one will drain this stream we are the first producer and
* need to schedule it for background thread.
*/
if (!(stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED))) {
/*
* No one will service this stream later, so we have to
* schedule it now.
*/
STRSTAT(stenables);
stp->sd_svcflags |= STRS_SCHEDULED;
stp->sd_servid = (void *)taskq_dispatch(streams_taskq,
(task_func_t *)stream_service, stp, TQ_NOSLEEP|TQ_NOQUEUE);
if (stp->sd_servid == NULL) {
/*
* Task queue failed so fail over to the backup
* servicing thread.
*/
STRSTAT(taskqfails);
/*
* It is safe to clear STRS_SCHEDULED flag because it
* was set by this thread above.
*/
stp->sd_svcflags &= ~STRS_SCHEDULED;
/*
* Failover scheduling is protected by service_queue
* lock.
*/
mutex_enter(&service_queue);
ASSERT((stp->sd_qhead == q) && (stp->sd_qtail == q));
ASSERT(q->q_link == NULL);
/*
* Append the queue to qhead/qtail list.
*/
if (qhead == NULL)
qhead = q;
else
qtail->q_link = q;
qtail = q;
/*
* Clear stp queue list.
*/
stp->sd_qhead = stp->sd_qtail = NULL;
stp->sd_nqueues = 0;
/*
* Wakeup background queue processing thread.
*/
cv_signal(&services_to_run);
mutex_exit(&service_queue);
}
}
mutex_exit(&stp->sd_qlock);
}
static void
queue_service(queue_t *q)
{
/*
* The queue in the list should have
* QENAB flag set and should not have
* QINSERVICE flag set. QINSERVICE is
* set when the queue is dequeued and
* qenable_locked doesn't enqueue a
* queue with QINSERVICE set.
*/
ASSERT(!(q->q_flag & QINSERVICE));
ASSERT((q->q_flag & QENAB));
mutex_enter(QLOCK(q));
q->q_flag &= ~QENAB;
q->q_flag |= QINSERVICE;
mutex_exit(QLOCK(q));
runservice(q);
}
static void
syncq_service(syncq_t *sq)
{
STRSTAT(syncqservice);
mutex_enter(SQLOCK(sq));
ASSERT(!(sq->sq_svcflags & SQ_SERVICE));
ASSERT(sq->sq_servcount != 0);
ASSERT(sq->sq_next == NULL);
/* if we came here from the background thread, clear the flag */
if (sq->sq_svcflags & SQ_BGTHREAD)
sq->sq_svcflags &= ~SQ_BGTHREAD;
/* let drain_syncq know that it's being called in the background */
sq->sq_svcflags |= SQ_SERVICE;
drain_syncq(sq);
}
static void
qwriter_outer_service(syncq_t *outer)
{
/*
* Note that SQ_WRITER is used on the outer perimeter
* to signal that a qwriter(OUTER) is either investigating
* running or that it is actually running a function.
*/
outer_enter(outer, SQ_BLOCKED|SQ_WRITER);
/*
* All inner syncq are empty and have SQ_WRITER set
* to block entering the outer perimeter.
*
* We do not need to explicitly call write_now since
* outer_exit does it for us.
*/
outer_exit(outer);
}
static void
mblk_free(mblk_t *mp)
{
dblk_t *dbp = mp->b_datap;
frtn_t *frp = dbp->db_frtnp;
mp->b_next = NULL;
if (dbp->db_fthdr != NULL)
str_ftfree(dbp);
ASSERT(dbp->db_fthdr == NULL);
frp->free_func(frp->free_arg);
ASSERT(dbp->db_mblk == mp);
if (dbp->db_credp != NULL) {
crfree(dbp->db_credp);
dbp->db_credp = NULL;
}
dbp->db_cpid = -1;
dbp->db_struioflag = 0;
dbp->db_struioun.cksum.flags = 0;
kmem_cache_free(dbp->db_cache, dbp);
}
/*
* Background processing of the stream queue list.
*/
static void
stream_service(stdata_t *stp)
{
queue_t *q;
mutex_enter(&stp->sd_qlock);
STR_SERVICE(stp, q);
stp->sd_svcflags &= ~STRS_SCHEDULED;
stp->sd_servid = NULL;
cv_signal(&stp->sd_qcv);
mutex_exit(&stp->sd_qlock);
}
/*
* Foreground processing of the stream queue list.
*/
void
stream_runservice(stdata_t *stp)
{
queue_t *q;
mutex_enter(&stp->sd_qlock);
STRSTAT(rservice);
/*
* We are going to drain this stream queue list, so qenable_locked will
* not schedule it until we finish.
*/
stp->sd_svcflags |= STRS_WILLSERVICE;
STR_SERVICE(stp, q);
stp->sd_svcflags &= ~STRS_WILLSERVICE;
mutex_exit(&stp->sd_qlock);
/*
* Help backup background thread to drain the qhead/qtail list.
*/
while (qhead != NULL) {
STRSTAT(qhelps);
mutex_enter(&service_queue);
DQ(q, qhead, qtail, q_link);
mutex_exit(&service_queue);
if (q != NULL)
queue_service(q);
}
}
void
stream_willservice(stdata_t *stp)
{
mutex_enter(&stp->sd_qlock);
stp->sd_svcflags |= STRS_WILLSERVICE;
mutex_exit(&stp->sd_qlock);
}
/*
* Replace the cred currently in the mblk with a different one.
* Also update db_cpid.
*/
void
mblk_setcred(mblk_t *mp, cred_t *cr, pid_t cpid)
{
dblk_t *dbp = mp->b_datap;
cred_t *ocr = dbp->db_credp;
ASSERT(cr != NULL);
if (cr != ocr) {
crhold(dbp->db_credp = cr);
if (ocr != NULL)
crfree(ocr);
}
/* Don't overwrite with NOPID */
if (cpid != NOPID)
dbp->db_cpid = cpid;
}
/*
* If the src message has a cred, then replace the cred currently in the mblk
* with it.
* Also update db_cpid.
*/
void
mblk_copycred(mblk_t *mp, const mblk_t *src)
{
dblk_t *dbp = mp->b_datap;
cred_t *cr, *ocr;
pid_t cpid;
cr = msg_getcred(src, &cpid);
if (cr == NULL)
return;
ocr = dbp->db_credp;
if (cr != ocr) {
crhold(dbp->db_credp = cr);
if (ocr != NULL)
crfree(ocr);
}
/* Don't overwrite with NOPID */
if (cpid != NOPID)
dbp->db_cpid = cpid;
}
int
hcksum_assoc(mblk_t *mp, multidata_t *mmd, pdesc_t *pd,
uint32_t start, uint32_t stuff, uint32_t end, uint32_t value,
uint32_t flags, int km_flags)
{
int rc = 0;
ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
if (mp->b_datap->db_type == M_DATA) {
/* Associate values for M_DATA type */
DB_CKSUMSTART(mp) = (intptr_t)start;
DB_CKSUMSTUFF(mp) = (intptr_t)stuff;
DB_CKSUMEND(mp) = (intptr_t)end;
DB_CKSUMFLAGS(mp) = flags;
DB_CKSUM16(mp) = (uint16_t)value;
} else {
pattrinfo_t pa_info;
ASSERT(mmd != NULL);
pa_info.type = PATTR_HCKSUM;
pa_info.len = sizeof (pattr_hcksum_t);
if (mmd_addpattr(mmd, pd, &pa_info, B_TRUE, km_flags) != NULL) {
pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf;
hck->hcksum_start_offset = start;
hck->hcksum_stuff_offset = stuff;
hck->hcksum_end_offset = end;
hck->hcksum_cksum_val.inet_cksum = (uint16_t)value;
hck->hcksum_flags = flags;
} else {
rc = -1;
}
}
return (rc);
}
void
hcksum_retrieve(mblk_t *mp, multidata_t *mmd, pdesc_t *pd,
uint32_t *start, uint32_t *stuff, uint32_t *end,
uint32_t *value, uint32_t *flags)
{
ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
if (mp->b_datap->db_type == M_DATA) {
if (flags != NULL) {
*flags = DB_CKSUMFLAGS(mp) & HCK_FLAGS;
if ((*flags & (HCK_PARTIALCKSUM |
HCK_FULLCKSUM)) != 0) {
if (value != NULL)
*value = (uint32_t)DB_CKSUM16(mp);
if ((*flags & HCK_PARTIALCKSUM) != 0) {
if (start != NULL)
*start =
(uint32_t)DB_CKSUMSTART(mp);
if (stuff != NULL)
*stuff =
(uint32_t)DB_CKSUMSTUFF(mp);
if (end != NULL)
*end =
(uint32_t)DB_CKSUMEND(mp);
}
}
}
} else {
pattrinfo_t hck_attr = {PATTR_HCKSUM};
ASSERT(mmd != NULL);
/* get hardware checksum attribute */
if (mmd_getpattr(mmd, pd, &hck_attr) != NULL) {
pattr_hcksum_t *hck = (pattr_hcksum_t *)hck_attr.buf;
ASSERT(hck_attr.len >= sizeof (pattr_hcksum_t));
if (flags != NULL)
*flags = hck->hcksum_flags;
if (start != NULL)
*start = hck->hcksum_start_offset;
if (stuff != NULL)
*stuff = hck->hcksum_stuff_offset;
if (end != NULL)
*end = hck->hcksum_end_offset;
if (value != NULL)
*value = (uint32_t)
hck->hcksum_cksum_val.inet_cksum;
}
}
}
void
lso_info_set(mblk_t *mp, uint32_t mss, uint32_t flags)
{
ASSERT(DB_TYPE(mp) == M_DATA);
/* Set the flags */
DB_LSOFLAGS(mp) |= flags;
DB_LSOMSS(mp) = mss;
}
void
lso_info_get(mblk_t *mp, uint32_t *mss, uint32_t *flags)
{
ASSERT(DB_TYPE(mp) == M_DATA);
if (flags != NULL) {
*flags = DB_CKSUMFLAGS(mp) & HW_LSO;
if ((*flags != 0) && (mss != NULL))
*mss = (uint32_t)DB_LSOMSS(mp);
}
}
/*
* Checksum buffer *bp for len bytes with psum partial checksum,
* or 0 if none, and return the 16 bit partial checksum.
*/
unsigned
bcksum(uchar_t *bp, int len, unsigned int psum)
{
int odd = len & 1;
extern unsigned int ip_ocsum();
if (((intptr_t)bp & 1) == 0 && !odd) {
/*
* Bp is 16 bit aligned and len is multiple of 16 bit word.
*/
return (ip_ocsum((ushort_t *)bp, len >> 1, psum));
}
if (((intptr_t)bp & 1) != 0) {
/*
* Bp isn't 16 bit aligned.
*/
unsigned int tsum;
#ifdef _LITTLE_ENDIAN
psum += *bp;
#else
psum += *bp << 8;
#endif
len--;
bp++;
tsum = ip_ocsum((ushort_t *)bp, len >> 1, 0);
psum += (tsum << 8) & 0xffff | (tsum >> 8);
if (len & 1) {
bp += len - 1;
#ifdef _LITTLE_ENDIAN
psum += *bp << 8;
#else
psum += *bp;
#endif
}
} else {
/*
* Bp is 16 bit aligned.
*/
psum = ip_ocsum((ushort_t *)bp, len >> 1, psum);
if (odd) {
bp += len - 1;
#ifdef _LITTLE_ENDIAN
psum += *bp;
#else
psum += *bp << 8;
#endif
}
}
/*
* Normalize psum to 16 bits before returning the new partial
* checksum. The max psum value before normalization is 0x3FDFE.
*/
return ((psum >> 16) + (psum & 0xFFFF));
}
boolean_t
is_vmloaned_mblk(mblk_t *mp, multidata_t *mmd, pdesc_t *pd)
{
boolean_t rc;
ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
if (DB_TYPE(mp) == M_DATA) {
rc = (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0);
} else {
pattrinfo_t zcopy_attr = {PATTR_ZCOPY};
ASSERT(mmd != NULL);
rc = (mmd_getpattr(mmd, pd, &zcopy_attr) != NULL);
}
return (rc);
}
void
freemsgchain(mblk_t *mp)
{
mblk_t *next;
while (mp != NULL) {
next = mp->b_next;
mp->b_next = NULL;
freemsg(mp);
mp = next;
}
}
mblk_t *
copymsgchain(mblk_t *mp)
{
mblk_t *nmp = NULL;
mblk_t **nmpp = &nmp;
for (; mp != NULL; mp = mp->b_next) {
if ((*nmpp = copymsg(mp)) == NULL) {
freemsgchain(nmp);
return (NULL);
}
nmpp = &((*nmpp)->b_next);
}
return (nmp);
}
/* NOTE: Do not add code after this point. */
#undef QLOCK
/*
* Replacement for QLOCK macro for those that can't use it.
*/
kmutex_t *
QLOCK(queue_t *q)
{
return (&(q)->q_lock);
}
/*
* Dummy runqueues/queuerun functions functions for backwards compatibility.
*/
#undef runqueues
void
runqueues(void)
{
}
#undef queuerun
void
queuerun(void)
{
}
/*
* Initialize the STR stack instance, which tracks autopush and persistent
* links.
*/
/* ARGSUSED */
static void *
str_stack_init(netstackid_t stackid, netstack_t *ns)
{
str_stack_t *ss;
int i;
ss = (str_stack_t *)kmem_zalloc(sizeof (*ss), KM_SLEEP);
ss->ss_netstack = ns;
/*
* set up autopush
*/
sad_initspace(ss);
/*
* set up mux_node structures.
*/
ss->ss_devcnt = devcnt; /* In case it should change before free */
ss->ss_mux_nodes = kmem_zalloc((sizeof (struct mux_node) *
ss->ss_devcnt), KM_SLEEP);
for (i = 0; i < ss->ss_devcnt; i++)
ss->ss_mux_nodes[i].mn_imaj = i;
return (ss);
}
/*
* Note: run at zone shutdown and not destroy so that the PLINKs are
* gone by the time other cleanup happens from the destroy callbacks.
*/
static void
str_stack_shutdown(netstackid_t stackid, void *arg)
{
str_stack_t *ss = (str_stack_t *)arg;
int i;
cred_t *cr;
cr = zone_get_kcred(netstackid_to_zoneid(stackid));
ASSERT(cr != NULL);
/* Undo all the I_PLINKs for this zone */
for (i = 0; i < ss->ss_devcnt; i++) {
struct mux_edge *ep;
ldi_handle_t lh;
ldi_ident_t li;
int ret;
int rval;
dev_t rdev;
ep = ss->ss_mux_nodes[i].mn_outp;
if (ep == NULL)
continue;
ret = ldi_ident_from_major((major_t)i, &li);
if (ret != 0) {
continue;
}
rdev = ep->me_dev;
ret = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD|FWRITE,
cr, &lh, li);
if (ret != 0) {
ldi_ident_release(li);
continue;
}
ret = ldi_ioctl(lh, I_PUNLINK, (intptr_t)MUXID_ALL, FKIOCTL,
cr, &rval);
if (ret) {
(void) ldi_close(lh, FREAD|FWRITE, cr);
ldi_ident_release(li);
continue;
}
(void) ldi_close(lh, FREAD|FWRITE, cr);
/* Close layered handles */
ldi_ident_release(li);
}
crfree(cr);
sad_freespace(ss);
kmem_free(ss->ss_mux_nodes, sizeof (struct mux_node) * ss->ss_devcnt);
ss->ss_mux_nodes = NULL;
}
/*
* Free the structure; str_stack_shutdown did the other cleanup work.
*/
/* ARGSUSED */
static void
str_stack_fini(netstackid_t stackid, void *arg)
{
str_stack_t *ss = (str_stack_t *)arg;
kmem_free(ss, sizeof (*ss));
}
|