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
|
/*
* 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) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Directory operations for High Sierra filesystem
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/vfs.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/mode.h>
#include <sys/dnlc.h>
#include <sys/cmn_err.h>
#include <sys/fbuf.h>
#include <sys/kmem.h>
#include <sys/policy.h>
#include <sys/sunddi.h>
#include <vm/hat.h>
#include <vm/as.h>
#include <vm/pvn.h>
#include <vm/seg.h>
#include <vm/seg_map.h>
#include <vm/seg_kmem.h>
#include <vm/page.h>
#include <sys/fs/hsfs_spec.h>
#include <sys/fs/hsfs_isospec.h>
#include <sys/fs/hsfs_node.h>
#include <sys/fs/hsfs_impl.h>
#include <sys/fs/hsfs_susp.h>
#include <sys/fs/hsfs_rrip.h>
#include <sys/sysinfo.h>
#include <sys/sysmacros.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include <fs/fs_subr.h>
/*
* This macro expects a name that ends in '.' and returns TRUE if the
* name is not "." or ".."
*/
#define CAN_TRUNCATE_DOT(name, namelen) \
(namelen > 1 && (namelen > 2 || name[0] != '.'))
enum dirblock_result { FOUND_ENTRY, WENT_PAST, HIT_END };
/*
* These values determine whether we will try to read a file or dir;
* they may be patched via /etc/system to allow users to read
* record-oriented files.
*/
int ide_prohibited = IDE_PROHIBITED;
int hde_prohibited = HDE_PROHIBITED;
/*
* This variable determines if the HSFS code will use the
* directory name lookup cache. The default is for the cache to be used.
*/
static int hsfs_use_dnlc = 1;
/*
* This variable determines whether strict ISO-9660 directory ordering
* is to be assumed. If false (which it is by default), then when
* searching a directory of an ISO-9660 disk, we do not expect the
* entries to be sorted (as the spec requires), and so cannot terminate
* the search early. Unfortunately, some vendors are producing
* non-compliant disks. This variable exists to revert to the old
* behavior in case someone relies on this. This option is expected to be
* removed at some point in the future.
*
* Use "set hsfs:strict_iso9660_ordering = 1" in /etc/system to override.
*/
static int strict_iso9660_ordering = 0;
/*
* This tunable allows us to ignore inode numbers from rrip-1.12.
* In this case, we fall back to our default inode algorithm.
*/
int use_rrip_inodes = 1;
static void hs_hsnode_cache_reclaim(void *unused);
static void hs_addfreeb(struct hsfs *fsp, struct hsnode *hp);
static enum dirblock_result process_dirblock(struct fbuf *fbp, uint_t *offset,
uint_t last_offset, char *nm, int nmlen, struct hsfs *fsp,
struct hsnode *dhp, struct vnode *dvp, struct vnode **vpp,
int *error);
static int strip_trailing(struct hsfs *fsp, char *nm, int len);
static int hs_namelen(struct hsfs *fsp, char *nm, int len);
static int uppercase_cp(char *from, char *to, int size);
static void hs_log_bogus_joliet_warning(void);
static int hs_iso_copy(char *from, char *to, int size);
static int32_t hs_ucs2_2_utf8(uint16_t c_16, uint8_t *s_8);
static int hs_utf8_trunc(uint8_t *str, int len);
/*
* hs_access
* Return 0 if the desired access may be granted.
* Otherwise return error code.
*/
int
hs_access(struct vnode *vp, mode_t m, struct cred *cred)
{
struct hsnode *hp;
int shift = 0;
/*
* Write access cannot be granted for a read-only medium
*/
if ((m & VWRITE) && !IS_DEVVP(vp))
return (EROFS);
hp = VTOH(vp);
/*
* XXX - For now, use volume protections.
* Also, always grant EXEC access for directories
* if READ access is granted.
*/
if ((vp->v_type == VDIR) && (m & VEXEC)) {
m &= ~VEXEC;
m |= VREAD;
}
if (crgetuid(cred) != hp->hs_dirent.uid) {
shift += 3;
if (!groupmember((uid_t)hp->hs_dirent.gid, cred))
shift += 3;
}
return (secpolicy_vnode_access2(cred, vp, hp->hs_dirent.uid,
hp->hs_dirent.mode << shift, m));
}
#if ((HS_HASHSIZE & (HS_HASHSIZE - 1)) == 0)
#define HS_HASH(l) ((uint_t)(l) & (HS_HASHSIZE - 1))
#else
#define HS_HASH(l) ((uint_t)(l) % HS_HASHSIZE)
#endif
#define HS_HPASH(hp) HS_HASH((hp)->hs_nodeid)
/*
* The tunable nhsnode is now a threshold for a dynamically allocated
* pool of hsnodes, not the size of a statically allocated table.
* When the number of hsnodes for a particular file system exceeds
* nhsnode, the allocate and free logic will try to reduce the number
* of allocated nodes by returning unreferenced nodes to the kmem_cache
* instead of putting them on the file system's private free list.
*/
int nhsnode = HS_HSNODESPACE / sizeof (struct hsnode);
struct kmem_cache *hsnode_cache; /* free hsnode cache */
/*
* Initialize the cache of free hsnodes.
*/
void
hs_init_hsnode_cache(void)
{
/*
* A kmem_cache is used for the hsnodes
* No constructor because hsnodes are initialised by bzeroing.
*/
hsnode_cache = kmem_cache_create("hsfs_hsnode_cache",
sizeof (struct hsnode), 0, NULL,
NULL, hs_hsnode_cache_reclaim, NULL, NULL, 0);
}
/*
* Destroy the cache of free hsnodes.
*/
void
hs_fini_hsnode_cache(void)
{
kmem_cache_destroy(hsnode_cache);
}
/*
* System is short on memory, free up as much as possible
*/
/*ARGSUSED*/
static void
hs_hsnode_cache_reclaim(void *unused)
{
struct hsfs *fsp;
struct hsnode *hp;
/*
* For each vfs in the hs_mounttab list
*/
mutex_enter(&hs_mounttab_lock);
for (fsp = hs_mounttab; fsp != NULL; fsp = fsp->hsfs_next) {
/*
* Purge the dnlc of all hsfs entries
*/
(void) dnlc_purge_vfsp(fsp->hsfs_vfs, 0);
/*
* For each entry in the free chain
*/
rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
mutex_enter(&fsp->hsfs_free_lock);
for (hp = fsp->hsfs_free_f; hp != NULL; hp = fsp->hsfs_free_f) {
/*
* Remove from chain
*/
fsp->hsfs_free_f = hp->hs_freef;
if (fsp->hsfs_free_f != NULL) {
fsp->hsfs_free_f->hs_freeb = NULL;
} else {
fsp->hsfs_free_b = NULL;
}
/*
* Free the node. Force it to be fully freed
* by setting the 3rd arg (nopage) to 1.
*/
hs_freenode(HTOV(hp), fsp, 1);
}
mutex_exit(&fsp->hsfs_free_lock);
rw_exit(&fsp->hsfs_hash_lock);
}
mutex_exit(&hs_mounttab_lock);
}
/*
* Add an hsnode to the end of the free list.
*/
static void
hs_addfreeb(struct hsfs *fsp, struct hsnode *hp)
{
struct hsnode *ep;
vn_invalid(HTOV(hp));
mutex_enter(&fsp->hsfs_free_lock);
ep = fsp->hsfs_free_b;
fsp->hsfs_free_b = hp; /* hp is the last entry in free list */
hp->hs_freef = NULL;
hp->hs_freeb = ep; /* point at previous last entry */
if (ep == NULL)
fsp->hsfs_free_f = hp; /* hp is only entry in free list */
else
ep->hs_freef = hp; /* point previous last entry at hp */
mutex_exit(&fsp->hsfs_free_lock);
}
/*
* Get an hsnode from the front of the free list.
* Must be called with write hsfs_hash_lock held.
*/
static struct hsnode *
hs_getfree(struct hsfs *fsp)
{
struct hsnode *hp, **tp;
ASSERT(RW_WRITE_HELD(&fsp->hsfs_hash_lock));
/*
* If the number of currently-allocated hsnodes is less than
* the hsnode count threshold (nhsnode), or if there are no
* nodes on the file system's local free list (which acts as a
* cache), call kmem_cache_alloc to get a new hsnode from
* kernel memory.
*/
mutex_enter(&fsp->hsfs_free_lock);
if ((fsp->hsfs_nohsnode < nhsnode) || (fsp->hsfs_free_f == NULL)) {
mutex_exit(&fsp->hsfs_free_lock);
hp = kmem_cache_alloc(hsnode_cache, KM_SLEEP);
fsp->hsfs_nohsnode++;
bzero((caddr_t)hp, sizeof (*hp));
hp->hs_vnode = vn_alloc(KM_SLEEP);
return (hp);
}
hp = fsp->hsfs_free_f;
/* hp cannot be NULL, since we already checked this above */
fsp->hsfs_free_f = hp->hs_freef;
if (fsp->hsfs_free_f != NULL)
fsp->hsfs_free_f->hs_freeb = NULL;
else
fsp->hsfs_free_b = NULL;
mutex_exit(&fsp->hsfs_free_lock);
for (tp = &fsp->hsfs_hash[HS_HPASH(hp)]; *tp != NULL;
tp = &(*tp)->hs_hash) {
if (*tp == hp) {
struct vnode *vp;
vp = HTOV(hp);
/*
* file is no longer referenced, destroy all old pages
*/
if (vn_has_cached_data(vp))
/*
* pvn_vplist_dirty will abort all old pages
*/
(void) pvn_vplist_dirty(vp, (u_offset_t)0,
hsfs_putapage, B_INVAL,
(struct cred *)NULL);
*tp = hp->hs_hash;
break;
}
}
if (hp->hs_dirent.sym_link != (char *)NULL) {
kmem_free(hp->hs_dirent.sym_link,
(size_t)(hp->hs_dirent.ext_size + 1));
}
mutex_destroy(&hp->hs_contents_lock);
{
vnode_t *vp;
vp = hp->hs_vnode;
bzero((caddr_t)hp, sizeof (*hp));
hp->hs_vnode = vp;
vn_reinit(vp);
}
return (hp);
}
/*
* Remove an hsnode from the free list.
*/
static void
hs_remfree(struct hsfs *fsp, struct hsnode *hp)
{
mutex_enter(&fsp->hsfs_free_lock);
if (hp->hs_freef != NULL)
hp->hs_freef->hs_freeb = hp->hs_freeb;
else
fsp->hsfs_free_b = hp->hs_freeb;
if (hp->hs_freeb != NULL)
hp->hs_freeb->hs_freef = hp->hs_freef;
else
fsp->hsfs_free_f = hp->hs_freef;
mutex_exit(&fsp->hsfs_free_lock);
}
/*
* Look for hsnode in hash list.
* If the inode number is != HS_DUMMY_INO (16), then only the inode
* number is used for the check.
* If the inode number is == HS_DUMMY_INO, we additionally always
* check the directory offset for the file to avoid caching the
* meta data for all zero sized to the first zero sized file that
* was touched.
*
* If found, reactivate it if inactive.
*
* Must be entered with hsfs_hash_lock held.
*/
struct vnode *
hs_findhash(ino64_t nodeid, uint_t lbn, uint_t off, struct vfs *vfsp)
{
struct hsnode *tp;
struct hsfs *fsp;
fsp = VFS_TO_HSFS(vfsp);
ASSERT(RW_LOCK_HELD(&fsp->hsfs_hash_lock));
for (tp = fsp->hsfs_hash[HS_HASH(nodeid)]; tp != NULL;
tp = tp->hs_hash) {
if (tp->hs_nodeid == nodeid) {
struct vnode *vp;
if (nodeid == HS_DUMMY_INO) {
/*
* If this is the dummy inode number, look for
* matching dir_lbn and dir_off.
*/
for (; tp != NULL; tp = tp->hs_hash) {
if (tp->hs_nodeid == nodeid &&
tp->hs_dir_lbn == lbn &&
tp->hs_dir_off == off)
break;
}
if (tp == NULL)
return (NULL);
}
mutex_enter(&tp->hs_contents_lock);
vp = HTOV(tp);
VN_HOLD(vp);
if ((tp->hs_flags & HREF) == 0) {
tp->hs_flags |= HREF;
/*
* reactivating a free hsnode:
* remove from free list
*/
hs_remfree(fsp, tp);
}
mutex_exit(&tp->hs_contents_lock);
return (vp);
}
}
return (NULL);
}
static void
hs_addhash(struct hsfs *fsp, struct hsnode *hp)
{
ulong_t hashno;
ASSERT(RW_WRITE_HELD(&fsp->hsfs_hash_lock));
hashno = HS_HPASH(hp);
hp->hs_hash = fsp->hsfs_hash[hashno];
fsp->hsfs_hash[hashno] = hp;
}
/*
* Destroy all old pages and free the hsnodes
* Return 1 if busy (a hsnode is still referenced).
*/
int
hs_synchash(struct vfs *vfsp)
{
struct hsfs *fsp;
int i;
struct hsnode *hp, *nhp;
int busy = 0;
struct vnode *vp, *rvp;
fsp = VFS_TO_HSFS(vfsp);
rvp = fsp->hsfs_rootvp;
/* make sure no one can come in */
rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
for (i = 0; i < HS_HASHSIZE; i++) {
for (hp = fsp->hsfs_hash[i]; hp != NULL; hp = hp->hs_hash) {
vp = HTOV(hp);
if ((hp->hs_flags & HREF) && (vp != rvp ||
(vp == rvp && vp->v_count > 1))) {
busy = 1;
continue;
}
if (vn_has_cached_data(vp))
(void) pvn_vplist_dirty(vp, (u_offset_t)0,
hsfs_putapage, B_INVAL,
(struct cred *)NULL);
}
}
if (busy) {
rw_exit(&fsp->hsfs_hash_lock);
return (1);
}
/* now free the hsnodes */
for (i = 0; i < HS_HASHSIZE; i++) {
for (hp = fsp->hsfs_hash[i]; hp != NULL; hp = nhp) {
nhp = hp->hs_hash;
/*
* We know there are no pages associated with
* all the hsnodes (they've all been released
* above). So remove from free list and
* free the entry with nopage set.
*/
vp = HTOV(hp);
if (vp != rvp) {
hs_remfree(fsp, hp);
hs_freenode(vp, fsp, 1);
}
}
}
ASSERT(fsp->hsfs_nohsnode == 1);
rw_exit(&fsp->hsfs_hash_lock);
/* release the root hsnode, this should free the final hsnode */
VN_RELE(rvp);
return (0);
}
/*
* hs_makenode
*
* Construct an hsnode.
* Caller specifies the directory entry, the block number and offset
* of the directory entry, and the vfs pointer.
* note: off is the sector offset, not lbn offset
* if NULL is returned implies file system hsnode table full
*/
struct vnode *
hs_makenode(
struct hs_direntry *dp,
uint_t lbn,
uint_t off,
struct vfs *vfsp)
{
struct hsnode *hp;
struct vnode *vp;
struct hs_volume *hvp;
struct vnode *newvp;
struct hsfs *fsp;
ino64_t nodeid;
fsp = VFS_TO_HSFS(vfsp);
/*
* Construct the data that allows us to re-read the meta data without
* knowing the name of the file: in the case of a directory
* entry, this should point to the canonical dirent, the "."
* directory entry for the directory. This dirent is pointed
* to by all directory entries for that dir (including the ".")
* entry itself.
* In the case of a file, simply point to the dirent for that
* file (there are hard links in Rock Ridge, so we need to use
* different data to contruct the node id).
*/
if (dp->type == VDIR) {
lbn = dp->ext_lbn;
off = 0;
}
/*
* Normalize lbn and off before creating a nodeid
* and before storing them in a hs_node structure
*/
hvp = &fsp->hsfs_vol;
lbn += off >> hvp->lbn_shift;
off &= hvp->lbn_maxoffset;
/*
* If the media carries rrip-v1.12 or newer, and we trust the inodes
* from the rrip data (use_rrip_inodes != 0), use that data. If the
* media has been created by a recent mkisofs version, we may trust
* all numbers in the starting extent number; otherwise, we cannot
* do this for zero sized files and symlinks, because if we did we'd
* end up mapping all of them to the same node.
* We use HS_DUMMY_INO in this case and make sure that we will not
* map all files to the same meta data.
*/
if (dp->inode != 0 && use_rrip_inodes) {
nodeid = dp->inode;
} else if ((dp->ext_size == 0 || dp->sym_link != (char *)NULL) &&
(fsp->hsfs_flags & HSFSMNT_INODE) == 0) {
nodeid = HS_DUMMY_INO;
} else {
nodeid = dp->ext_lbn;
}
/* look for hsnode in cache first */
rw_enter(&fsp->hsfs_hash_lock, RW_READER);
if ((vp = hs_findhash(nodeid, lbn, off, vfsp)) == NULL) {
/*
* Not in cache. However, someone else may have come
* to the same conclusion and just put one in. Upgrade
* our lock to a write lock and look again.
*/
rw_exit(&fsp->hsfs_hash_lock);
rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
if ((vp = hs_findhash(nodeid, lbn, off, vfsp)) == NULL) {
/*
* Now we are really sure that the hsnode is not
* in the cache. Get one off freelist or else
* allocate one. Either way get a bzeroed hsnode.
*/
hp = hs_getfree(fsp);
bcopy((caddr_t)dp, (caddr_t)&hp->hs_dirent,
sizeof (*dp));
/*
* We've just copied this pointer into hs_dirent,
* and don't want 2 references to same symlink.
*/
dp->sym_link = (char *)NULL;
/*
* No need to hold any lock because hsnode is not
* yet in the hash chain.
*/
mutex_init(&hp->hs_contents_lock, NULL, MUTEX_DEFAULT,
NULL);
hp->hs_dir_lbn = lbn;
hp->hs_dir_off = off;
hp->hs_nodeid = nodeid;
hp->hs_seq = 0;
hp->hs_prev_offset = 0;
hp->hs_num_contig = 0;
hp->hs_ra_bytes = 0;
hp->hs_flags = HREF;
if (off > HS_SECTOR_SIZE)
cmn_err(CE_WARN, "hs_makenode: bad offset");
vp = HTOV(hp);
vp->v_vfsp = vfsp;
vp->v_type = dp->type;
vp->v_rdev = dp->r_dev;
vn_setops(vp, hsfs_vnodeops);
vp->v_data = (caddr_t)hp;
vn_exists(vp);
/*
* if it's a device, call specvp
*/
if (IS_DEVVP(vp)) {
rw_exit(&fsp->hsfs_hash_lock);
newvp = specvp(vp, vp->v_rdev, vp->v_type,
CRED());
if (newvp == NULL)
cmn_err(CE_NOTE,
"hs_makenode: specvp failed");
VN_RELE(vp);
return (newvp);
}
hs_addhash(fsp, hp);
}
}
if (dp->sym_link != (char *)NULL) {
kmem_free(dp->sym_link, (size_t)(dp->ext_size + 1));
dp->sym_link = (char *)NULL;
}
rw_exit(&fsp->hsfs_hash_lock);
return (vp);
}
/*
* hs_freenode
*
* Deactivate an hsnode.
* Leave it on the hash list but put it on the free list.
* If the vnode does not have any pages, release the hsnode to the
* kmem_cache using kmem_cache_free, else put in back of the free list.
*
* This function can be called with the hsfs_free_lock held, but only
* when the code is guaranteed to go through the path where the
* node is freed entirely, and not the path where the node could go back
* on the free list (and where the free lock would need to be acquired).
*/
void
hs_freenode(vnode_t *vp, struct hsfs *fsp, int nopage)
{
struct hsnode **tp;
struct hsnode *hp = VTOH(vp);
ASSERT(RW_LOCK_HELD(&fsp->hsfs_hash_lock));
if (nopage || (fsp->hsfs_nohsnode >= nhsnode)) {
/* remove this node from the hash list, if it's there */
for (tp = &fsp->hsfs_hash[HS_HPASH(hp)]; *tp != NULL;
tp = &(*tp)->hs_hash) {
if (*tp == hp) {
*tp = hp->hs_hash;
break;
}
}
if (hp->hs_dirent.sym_link != (char *)NULL) {
kmem_free(hp->hs_dirent.sym_link,
(size_t)(hp->hs_dirent.ext_size + 1));
hp->hs_dirent.sym_link = NULL;
}
if (vn_has_cached_data(vp)) {
/* clean all old pages */
(void) pvn_vplist_dirty(vp, (u_offset_t)0,
hsfs_putapage, B_INVAL, (struct cred *)NULL);
/* XXX - can we remove pages by fiat like this??? */
vp->v_pages = NULL;
}
mutex_destroy(&hp->hs_contents_lock);
vn_invalid(vp);
vn_free(vp);
kmem_cache_free(hsnode_cache, hp);
fsp->hsfs_nohsnode--;
return;
}
hs_addfreeb(fsp, hp); /* add to back of free list */
}
/*
* hs_remakenode
*
* Reconstruct a vnode given the location of its directory entry.
* Caller specifies the the block number and offset
* of the directory entry, and the vfs pointer.
* Returns an error code or 0.
*/
int
hs_remakenode(uint_t lbn, uint_t off, struct vfs *vfsp,
struct vnode **vpp)
{
struct buf *secbp;
struct hsfs *fsp;
uint_t secno;
uchar_t *dirp;
struct hs_direntry hd;
int error;
/* Convert to sector and offset */
fsp = VFS_TO_HSFS(vfsp);
if (off > HS_SECTOR_SIZE) {
cmn_err(CE_WARN, "hs_remakenode: bad offset");
error = EINVAL;
goto end;
}
secno = LBN_TO_SEC(lbn, vfsp);
secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "hs_remakenode: bread: error=(%d)", error);
goto end;
}
dirp = (uchar_t *)secbp->b_un.b_addr;
error = hs_parsedir(fsp, &dirp[off], &hd, (char *)NULL, (int *)NULL,
HS_SECTOR_SIZE - off);
if (!error) {
*vpp = hs_makenode(&hd, lbn, off, vfsp);
if (*vpp == NULL)
error = ENFILE;
}
end:
brelse(secbp);
return (error);
}
/*
* hs_dirlook
*
* Look for a given name in a given directory.
* If found, construct an hsnode for it.
*/
int
hs_dirlook(
struct vnode *dvp,
char *name,
int namlen, /* length of 'name' */
struct vnode **vpp,
struct cred *cred)
{
struct hsnode *dhp;
struct hsfs *fsp;
int error = 0;
uint_t offset; /* real offset in directory */
uint_t last_offset; /* last index in directory */
char *cmpname; /* case-folded name */
int cmpname_size; /* how much memory we allocate for it */
int cmpnamelen;
int adhoc_search; /* did we start at begin of dir? */
int end;
uint_t hsoffset;
struct fbuf *fbp;
int bytes_wanted;
int dirsiz;
int is_rrip;
if (dvp->v_type != VDIR)
return (ENOTDIR);
if (error = hs_access(dvp, (mode_t)VEXEC, cred))
return (error);
if (hsfs_use_dnlc && (*vpp = dnlc_lookup(dvp, name)))
return (0);
dhp = VTOH(dvp);
fsp = VFS_TO_HSFS(dvp->v_vfsp);
is_rrip = IS_RRIP_IMPLEMENTED(fsp);
/*
* name == "^A" is illegal for ISO-9660 and Joliet as '..' is '\1' on
* disk. It is no problem for Rock Ridge as RR uses '.' and '..'.
* XXX It could be OK for Joliet also (because namelen == 1 is
* XXX impossible for UCS-2) but then we need a better compare algorith.
*/
if (!is_rrip && *name == '\1' && namlen == 1)
return (EINVAL);
cmpname_size = (int)(fsp->hsfs_namemax + 1);
cmpname = kmem_alloc((size_t)cmpname_size, KM_SLEEP);
if (namlen >= cmpname_size)
namlen = cmpname_size - 1;
/*
* For the purposes of comparing the name against dir entries,
* fold it to upper case.
*/
if (is_rrip) {
(void) strlcpy(cmpname, name, cmpname_size);
cmpnamelen = namlen;
} else {
/*
* If we don't consider a trailing dot as part of the filename,
* remove it from the specified name
*/
if ((fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
name[namlen-1] == '.' &&
CAN_TRUNCATE_DOT(name, namlen))
name[--namlen] = '\0';
if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2 ||
fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
cmpnamelen = hs_iso_copy(name, cmpname, namlen);
} else {
cmpnamelen = hs_uppercase_copy(name, cmpname, namlen);
}
}
/* make sure dirent is filled up with all info */
if (dhp->hs_dirent.ext_size == 0)
hs_filldirent(dvp, &dhp->hs_dirent);
/*
* No lock is needed - hs_offset is used as starting
* point for searching the directory.
*/
offset = dhp->hs_offset;
hsoffset = offset;
adhoc_search = (offset != 0);
end = dhp->hs_dirent.ext_size;
dirsiz = end;
tryagain:
while (offset < end) {
bytes_wanted = MIN(MAXBSIZE, dirsiz - (offset & MAXBMASK));
error = fbread(dvp, (offset_t)(offset & MAXBMASK),
(unsigned int)bytes_wanted, S_READ, &fbp);
if (error)
goto done;
last_offset = (offset & MAXBMASK) + fbp->fb_count;
switch (process_dirblock(fbp, &offset, last_offset,
cmpname, cmpnamelen, fsp, dhp, dvp, vpp, &error)) {
case FOUND_ENTRY:
/* found an entry, either correct or not */
goto done;
case WENT_PAST:
/*
* If we get here we know we didn't find it on the
* first pass. If adhoc_search, then we started a
* bit into the dir, and need to wrap around and
* search the first entries. If not, then we started
* at the beginning and didn't find it.
*/
if (adhoc_search) {
offset = 0;
end = hsoffset;
adhoc_search = 0;
goto tryagain;
}
error = ENOENT;
goto done;
case HIT_END:
goto tryagain;
}
}
/*
* End of all dir blocks, didn't find entry.
*/
if (adhoc_search) {
offset = 0;
end = hsoffset;
adhoc_search = 0;
goto tryagain;
}
error = ENOENT;
done:
/*
* If we found the entry, add it to the DNLC
* If the entry is a device file (assuming we support Rock Ridge),
* we enter the device vnode to the cache since that is what
* is in *vpp.
* That is ok since the CD-ROM is read-only, so (dvp,name) will
* always point to the same device.
*/
if (hsfs_use_dnlc && !error)
dnlc_enter(dvp, name, *vpp);
kmem_free(cmpname, (size_t)cmpname_size);
return (error);
}
/*
* hs_parsedir
*
* Parse a Directory Record into an hs_direntry structure.
* High Sierra and ISO directory are almost the same
* except the flag and date
*/
int
hs_parsedir(
struct hsfs *fsp,
uchar_t *dirp,
struct hs_direntry *hdp,
char *dnp,
int *dnlen,
int last_offset) /* last offset in dirp */
{
char *on_disk_name;
int on_disk_namelen;
int on_disk_dirlen;
uchar_t flags;
int namelen;
int error;
int name_change_flag = 0; /* set if name was gotten in SUA */
hdp->ext_lbn = HDE_EXT_LBN(dirp);
hdp->ext_size = HDE_EXT_SIZE(dirp);
hdp->xar_len = HDE_XAR_LEN(dirp);
hdp->intlf_sz = HDE_INTRLV_SIZE(dirp);
hdp->intlf_sk = HDE_INTRLV_SKIP(dirp);
hdp->sym_link = (char *)NULL;
if (fsp->hsfs_vol_type == HS_VOL_TYPE_HS) {
flags = HDE_FLAGS(dirp);
hs_parse_dirdate(HDE_cdate(dirp), &hdp->cdate);
hs_parse_dirdate(HDE_cdate(dirp), &hdp->adate);
hs_parse_dirdate(HDE_cdate(dirp), &hdp->mdate);
if ((flags & hde_prohibited) == 0) {
/*
* Skip files with the associated bit set.
*/
if (flags & HDE_ASSOCIATED)
return (EAGAIN);
hdp->type = VREG;
hdp->mode = HFREG;
hdp->nlink = 1;
} else if ((flags & hde_prohibited) == HDE_DIRECTORY) {
hdp->type = VDIR;
hdp->mode = HFDIR;
hdp->nlink = 2;
} else {
hs_log_bogus_disk_warning(fsp,
HSFS_ERR_UNSUP_TYPE, flags);
return (EINVAL);
}
hdp->uid = fsp -> hsfs_vol.vol_uid;
hdp->gid = fsp -> hsfs_vol.vol_gid;
hdp->mode = hdp-> mode | (fsp -> hsfs_vol.vol_prot & 0777);
} else if ((fsp->hsfs_vol_type == HS_VOL_TYPE_ISO) ||
(fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) ||
(fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET)) {
flags = IDE_FLAGS(dirp);
hs_parse_dirdate(IDE_cdate(dirp), &hdp->cdate);
hs_parse_dirdate(IDE_cdate(dirp), &hdp->adate);
hs_parse_dirdate(IDE_cdate(dirp), &hdp->mdate);
if ((flags & ide_prohibited) == 0) {
/*
* Skip files with the associated bit set.
*/
if (flags & IDE_ASSOCIATED)
return (EAGAIN);
hdp->type = VREG;
hdp->mode = HFREG;
hdp->nlink = 1;
} else if ((flags & ide_prohibited) == IDE_DIRECTORY) {
hdp->type = VDIR;
hdp->mode = HFDIR;
hdp->nlink = 2;
} else {
hs_log_bogus_disk_warning(fsp,
HSFS_ERR_UNSUP_TYPE, flags);
return (EINVAL);
}
hdp->uid = fsp -> hsfs_vol.vol_uid;
hdp->gid = fsp -> hsfs_vol.vol_gid;
hdp->mode = hdp-> mode | (fsp -> hsfs_vol.vol_prot & 0777);
hdp->inode = 0; /* initialize with 0, then check rrip */
/*
* Having this all filled in, let's see if we have any
* SUA susp to look at.
*/
if (IS_SUSP_IMPLEMENTED(fsp)) {
error = parse_sua((uchar_t *)dnp, dnlen,
&name_change_flag, dirp, last_offset,
hdp, fsp, NULL, 0);
if (error) {
if (hdp->sym_link) {
kmem_free(hdp->sym_link,
(size_t)(hdp->ext_size + 1));
hdp->sym_link = (char *)NULL;
}
return (error);
}
}
}
hdp->xar_prot = (HDE_PROTECTION & flags) != 0;
#if dontskip
if (hdp->xar_len > 0) {
cmn_err(CE_NOTE, "hsfs: extended attributes not supported");
return (EINVAL);
}
#endif
/* check interleaf size and skip factor */
/* must both be zero or non-zero */
if (hdp->intlf_sz + hdp->intlf_sk) {
if ((hdp->intlf_sz == 0) || (hdp->intlf_sk == 0)) {
cmn_err(CE_NOTE,
"hsfs: interleaf size or skip factor error");
return (EINVAL);
}
if (hdp->ext_size == 0) {
cmn_err(CE_NOTE,
"hsfs: interleaving specified on zero length file");
return (EINVAL);
}
}
if (HDE_VOL_SET(dirp) != 1) {
if (fsp->hsfs_vol.vol_set_size != 1 &&
fsp->hsfs_vol.vol_set_size != HDE_VOL_SET(dirp)) {
cmn_err(CE_NOTE, "hsfs: multivolume file?");
return (EINVAL);
}
}
/*
* If the name changed, then the NM field for RRIP was hit and
* we should not copy the name again, just return.
*/
if (NAME_HAS_CHANGED(name_change_flag))
return (0);
/*
* Fall back to the ISO name. Note that as in process_dirblock,
* the on-disk filename length must be validated against ISO
* limits - which, in case of RR present but no RR name found,
* are NOT identical to fsp->hsfs_namemax on this filesystem.
*/
on_disk_name = (char *)HDE_name(dirp);
on_disk_namelen = (int)HDE_NAME_LEN(dirp);
on_disk_dirlen = (int)HDE_DIR_LEN(dirp);
if (on_disk_dirlen < HDE_ROOT_DIR_REC_SIZE ||
((on_disk_dirlen > last_offset) ||
((HDE_FDESIZE + on_disk_namelen) > on_disk_dirlen))) {
hs_log_bogus_disk_warning(fsp,
HSFS_ERR_BAD_DIR_ENTRY, 0);
return (EINVAL);
}
if (on_disk_namelen > fsp->hsfs_namelen &&
hs_namelen(fsp, on_disk_name, on_disk_namelen) >
fsp->hsfs_namelen) {
hs_log_bogus_disk_warning(fsp,
fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET ?
HSFS_ERR_BAD_JOLIET_FILE_LEN :
HSFS_ERR_BAD_FILE_LEN, 0);
}
if (on_disk_namelen > ISO_NAMELEN_V2_MAX)
on_disk_namelen = fsp->hsfs_namemax; /* Paranoia */
if (dnp != NULL) {
if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
namelen = hs_jnamecopy(on_disk_name, dnp,
on_disk_namelen, fsp->hsfs_namemax,
fsp->hsfs_flags);
/*
* A negative return value means that the file name
* has been truncated to fsp->hsfs_namemax.
*/
if (namelen < 0) {
namelen = -namelen;
hs_log_bogus_disk_warning(fsp,
HSFS_ERR_TRUNC_JOLIET_FILE_LEN, 0);
}
} else {
/*
* HS_VOL_TYPE_ISO && HS_VOL_TYPE_ISO_V2
*/
namelen = hs_namecopy(on_disk_name, dnp,
on_disk_namelen, fsp->hsfs_flags);
}
if (namelen == 0)
return (EINVAL);
if ((fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
dnp[ namelen-1 ] == '.' && CAN_TRUNCATE_DOT(dnp, namelen))
dnp[ --namelen ] = '\0';
} else
namelen = on_disk_namelen;
if (dnlen != NULL)
*dnlen = namelen;
return (0);
}
/*
* hs_namecopy
*
* Parse a file/directory name into UNIX form.
* Delete trailing blanks, upper-to-lower case, add NULL terminator.
* Returns the (possibly new) length.
*
* Called from hsfs_readdir() via hs_parsedir()
*/
int
hs_namecopy(char *from, char *to, int size, ulong_t flags)
{
uint_t i;
uchar_t c;
int lastspace;
int maplc;
int trailspace;
int version;
/* special handling for '.' and '..' */
if (size == 1) {
if (*from == '\0') {
*to++ = '.';
*to = '\0';
return (1);
} else if (*from == '\1') {
*to++ = '.';
*to++ = '.';
*to = '\0';
return (2);
}
}
maplc = (flags & HSFSMNT_NOMAPLCASE) == 0;
trailspace = (flags & HSFSMNT_NOTRAILSPACE) == 0;
version = (flags & HSFSMNT_NOVERSION) == 0;
for (i = 0, lastspace = -1; i < size; i++) {
c = from[i];
if (c == ';' && version)
break;
if (c <= ' ' && !trailspace) {
if (lastspace == -1)
lastspace = i;
} else
lastspace = -1;
if (maplc && (c >= 'A') && (c <= 'Z'))
c += 'a' - 'A';
to[i] = c;
}
if (lastspace != -1)
i = lastspace;
to[i] = '\0';
return (i);
}
/*
* hs_jnamecopy
*
* This is the Joliet variant of hs_namecopy()
*
* Parse a UCS-2 Joliet file/directory name into UNIX form.
* Add NULL terminator.
* Returns the new length.
*
* Called from hsfs_readdir() via hs_parsedir()
*/
int
hs_jnamecopy(char *from, char *to, int size, int maxsize, ulong_t flags)
{
uint_t i;
uint_t len;
uint16_t c;
int amt;
int version;
/* special handling for '.' and '..' */
if (size == 1) {
if (*from == '\0') {
*to++ = '.';
*to = '\0';
return (1);
} else if (*from == '\1') {
*to++ = '.';
*to++ = '.';
*to = '\0';
return (2);
}
}
version = (flags & HSFSMNT_NOVERSION) == 0;
for (i = 0, len = 0; i < size; i++) {
c = (from[i++] & 0xFF) << 8;
c |= from[i] & 0xFF;
if (c == ';' && version)
break;
if (len > (maxsize-3)) {
if (c < 0x80)
amt = 1;
else if (c < 0x800)
amt = 2;
else
amt = 3;
if ((len+amt) > maxsize) {
to[len] = '\0';
return (-len);
}
}
amt = hs_ucs2_2_utf8(c, (uint8_t *)&to[len]);
if (amt == 0) {
hs_log_bogus_joliet_warning(); /* should never happen */
return (0);
}
len += amt;
}
to[len] = '\0';
return (len);
}
/*
* map a filename to upper case;
* return 1 if found lowercase character
*
* Called from process_dirblock()
* via hsfs_lookup() -> hs_dirlook() -> process_dirblock()
* to create an intermedia name from on disk file names for
* comparing names.
*/
static int
uppercase_cp(char *from, char *to, int size)
{
uint_t i;
uchar_t c;
uchar_t had_lc = 0;
for (i = 0; i < size; i++) {
c = *from++;
if ((c >= 'a') && (c <= 'z')) {
c -= ('a' - 'A');
had_lc = 1;
}
*to++ = c;
}
return (had_lc);
}
/*
* This is the Joliet variant of uppercase_cp()
*
* map a UCS-2 filename to UTF-8;
* return new length
*
* Called from process_dirblock()
* via hsfs_lookup() -> hs_dirlook() -> process_dirblock()
* to create an intermedia name from on disk file names for
* comparing names.
*/
int
hs_joliet_cp(char *from, char *to, int size)
{
uint_t i;
uint16_t c;
int len = 0;
int amt;
/* special handling for '\0' and '\1' */
if (size == 1) {
*to = *from;
return (1);
}
for (i = 0; i < size; i += 2) {
c = (*from++ & 0xFF) << 8;
c |= *from++ & 0xFF;
amt = hs_ucs2_2_utf8(c, (uint8_t *)to);
if (amt == 0) {
hs_log_bogus_joliet_warning(); /* should never happen */
return (0);
}
to += amt;
len += amt;
}
return (len);
}
static void
hs_log_bogus_joliet_warning(void)
{
static int warned = 0;
if (warned)
return;
warned = 1;
cmn_err(CE_CONT, "hsfs: Warning: "
"file name contains bad UCS-2 chacarter\n");
}
/*
* hs_uppercase_copy
*
* Convert a UNIX-style name into its HSFS equivalent
* replacing '.' and '..' with '\0' and '\1'.
* Map to upper case.
* Returns the (possibly new) length.
*
* Called from hs_dirlook() and rrip_namecopy()
* to create an intermediate name from the callers name from hsfs_lookup()
* XXX Is the call from rrip_namecopy() OK?
*/
int
hs_uppercase_copy(char *from, char *to, int size)
{
uint_t i;
uchar_t c;
/* special handling for '.' and '..' */
if (size == 1 && *from == '.') {
*to = '\0';
return (1);
} else if (size == 2 && *from == '.' && *(from+1) == '.') {
*to = '\1';
return (1);
}
for (i = 0; i < size; i++) {
c = *from++;
if ((c >= 'a') && (c <= 'z'))
c = c - 'a' + 'A';
*to++ = c;
}
return (size);
}
/*
* hs_iso_copy
*
* This is the Joliet/ISO-9660:1999 variant of hs_uppercase_copy()
*
* Convert a UTF-8 UNIX-style name into its UTF-8 Joliet/ISO equivalent
* replacing '.' and '..' with '\0' and '\1'.
* Returns the (possibly new) length.
*
* Called from hs_dirlook()
* to create an intermediate name from the callers name from hsfs_lookup()
*/
static int
hs_iso_copy(char *from, char *to, int size)
{
uint_t i;
uchar_t c;
/* special handling for '.' and '..' */
if (size == 1 && *from == '.') {
*to = '\0';
return (1);
} else if (size == 2 && *from == '.' && *(from+1) == '.') {
*to = '\1';
return (1);
}
for (i = 0; i < size; i++) {
c = *from++;
*to++ = c;
}
return (size);
}
void
hs_filldirent(struct vnode *vp, struct hs_direntry *hdp)
{
struct buf *secbp;
uint_t secno;
offset_t secoff;
struct hsfs *fsp;
uchar_t *secp;
int error;
if (vp->v_type != VDIR) {
cmn_err(CE_WARN, "hsfs_filldirent: vp (0x%p) not a directory",
(void *)vp);
return;
}
fsp = VFS_TO_HSFS(vp ->v_vfsp);
secno = LBN_TO_SEC(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp);
secoff = LBN_TO_BYTE(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp) &
MAXHSOFFSET;
secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "hs_filldirent: bread: error=(%d)", error);
goto end;
}
secp = (uchar_t *)secbp->b_un.b_addr;
/* quick check */
if (hdp->ext_lbn != HDE_EXT_LBN(&secp[secoff])) {
cmn_err(CE_NOTE, "hsfs_filldirent: dirent not match");
/* keep on going */
}
(void) hs_parsedir(fsp, &secp[secoff], hdp, (char *)NULL,
(int *)NULL, HS_SECTOR_SIZE - secoff);
end:
brelse(secbp);
}
/*
* Look through a directory block for a matching entry.
* Note: this routine does an fbrelse() on the buffer passed in.
*/
static enum dirblock_result
process_dirblock(
struct fbuf *fbp, /* buffer containing dirblk */
uint_t *offset, /* lower index */
uint_t last_offset, /* upper index */
char *nm, /* upcase nm to compare against */
int nmlen, /* length of name */
struct hsfs *fsp,
struct hsnode *dhp,
struct vnode *dvp,
struct vnode **vpp,
int *error) /* return value: errno */
{
uchar_t *blkp = (uchar_t *)fbp->fb_addr; /* dir block */
char *dname; /* name in directory entry */
int dnamelen; /* length of name */
struct hs_direntry hd;
int hdlen;
uchar_t *dirp; /* the directory entry */
int res;
int parsedir_res;
int is_rrip;
size_t rrip_name_size;
int rr_namelen = 0;
char *rrip_name_str = NULL;
char *rrip_tmp_name = NULL;
enum dirblock_result err = 0;
int did_fbrelse = 0;
char uppercase_name[JOLIET_NAMELEN_MAX*3 + 1]; /* 331 */
#define PD_return(retval) \
{ err = retval; goto do_ret; } /* return after cleanup */
#define rel_offset(offset) \
((offset) & MAXBOFFSET) /* index into cur blk */
#define RESTORE_NM(tmp, orig) \
if (is_rrip && *(tmp) != '\0') \
(void) strcpy((orig), (tmp))
is_rrip = IS_RRIP_IMPLEMENTED(fsp);
if (is_rrip) {
rrip_name_size = RRIP_FILE_NAMELEN + 1;
rrip_name_str = kmem_alloc(rrip_name_size, KM_SLEEP);
rrip_tmp_name = kmem_alloc(rrip_name_size, KM_SLEEP);
rrip_name_str[0] = '\0';
rrip_tmp_name[0] = '\0';
}
while (*offset < last_offset) {
/*
* Directory Entries cannot span sectors.
*
* Unused bytes at the end of each sector are zeroed
* according to ISO9660, but we cannot rely on this
* since both media failures and maliciously corrupted
* media may return arbitrary values.
* We therefore have to check for consistency:
* The size of a directory entry must be at least
* 34 bytes (the size of the directory entry metadata),
* or zero (indicating the end-of-sector condition).
* For a non-zero directory entry size of less than
* 34 Bytes, log a warning.
* In any case, skip the rest of this sector and
* continue with the next.
*/
hdlen = (int)((uchar_t)
HDE_DIR_LEN(&blkp[rel_offset(*offset)]));
if (hdlen < HDE_ROOT_DIR_REC_SIZE ||
*offset + hdlen > last_offset) {
/*
* Advance to the next sector boundary
*/
*offset = roundup(*offset + 1, HS_SECTOR_SIZE);
if (hdlen)
hs_log_bogus_disk_warning(fsp,
HSFS_ERR_TRAILING_JUNK, 0);
continue;
}
bzero(&hd, sizeof (hd));
/*
* Check the filename length in the ISO record for
* plausibility and reset it to a safe value, in case
* the name length byte is out of range. Since the ISO
* name will be used as fallback if the rockridge name
* is invalid/nonexistant, we must make sure not to
* blow the bounds and initialize dnamelen to a sensible
* value within the limits of ISO9660.
* In addition to that, the ISO filename is part of the
* directory entry. If the filename length is too large
* to fit, the record is invalid and we'll advance to
* the next.
*/
dirp = &blkp[rel_offset(*offset)];
dname = (char *)HDE_name(dirp);
dnamelen = (int)((uchar_t)HDE_NAME_LEN(dirp));
/*
* If the directory entry extends beyond the end of the
* block, it must be invalid. Skip it.
*/
if (dnamelen > hdlen - HDE_FDESIZE) {
hs_log_bogus_disk_warning(fsp,
HSFS_ERR_BAD_DIR_ENTRY, 0);
goto skip_rec;
} else if (dnamelen > fsp->hsfs_namelen &&
hs_namelen(fsp, dname, dnamelen) > fsp->hsfs_namelen) {
hs_log_bogus_disk_warning(fsp,
fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET ?
HSFS_ERR_BAD_JOLIET_FILE_LEN :
HSFS_ERR_BAD_FILE_LEN, 0);
}
if (dnamelen > ISO_NAMELEN_V2_MAX)
dnamelen = fsp->hsfs_namemax; /* Paranoia */
/*
* If the rock ridge is implemented, then we copy the name
* from the SUA area to rrip_name_str. If no Alternate
* name is found, then use the uppercase NM in the
* rrip_name_str char array.
*/
if (is_rrip) {
rrip_name_str[0] = '\0';
rr_namelen = rrip_namecopy(nm, &rrip_name_str[0],
&rrip_tmp_name[0], dirp, last_offset - *offset,
fsp, &hd);
if (hd.sym_link) {
kmem_free(hd.sym_link,
(size_t)(hd.ext_size+1));
hd.sym_link = (char *)NULL;
}
if (rr_namelen != -1) {
dname = (char *)&rrip_name_str[0];
dnamelen = rr_namelen;
}
}
if (!is_rrip || rr_namelen == -1) {
/* use iso name instead */
int i = -1;
/*
* make sure that we get rid of ';' in the dname of
* an iso direntry, as we should have no knowledge
* of file versions.
*
* XXX This is done the wrong way: it does not take
* XXX care of the fact that the version string is
* XXX a decimal number in the range 1 to 32767.
*/
if ((fsp->hsfs_flags & HSFSMNT_NOVERSION) == 0) {
if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
for (i = dnamelen - 1; i > 0; i -= 2) {
if (dname[i] == ';' &&
dname[i-1] == '\0') {
--i;
break;
}
}
} else {
for (i = dnamelen - 1; i > 0; i--) {
if (dname[i] == ';')
break;
}
}
}
if (i > 0) {
dnamelen = i;
} else if (fsp->hsfs_vol_type != HS_VOL_TYPE_ISO_V2 &&
fsp->hsfs_vol_type != HS_VOL_TYPE_JOLIET) {
dnamelen = strip_trailing(fsp, dname, dnamelen);
}
ASSERT(dnamelen < sizeof (uppercase_name));
if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) {
(void) strncpy(uppercase_name, dname, dnamelen);
} else if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
dnamelen = hs_joliet_cp(dname, uppercase_name,
dnamelen);
} else if (uppercase_cp(dname, uppercase_name,
dnamelen)) {
hs_log_bogus_disk_warning(fsp,
HSFS_ERR_LOWER_CASE_NM, 0);
}
dname = uppercase_name;
if (!is_rrip &&
(fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
dname[dnamelen - 1] == '.' &&
CAN_TRUNCATE_DOT(dname, dnamelen))
dname[--dnamelen] = '\0';
}
/*
* Quickly screen for a non-matching entry, but not for RRIP.
* This test doesn't work for lowercase vs. uppercase names.
*/
/* if we saw a lower case name we can't do this test either */
if (strict_iso9660_ordering && !is_rrip &&
!HSFS_HAVE_LOWER_CASE(fsp) && *nm < *dname) {
RESTORE_NM(rrip_tmp_name, nm);
PD_return(WENT_PAST)
}
if (*nm != *dname || nmlen != dnamelen)
goto skip_rec;
if ((res = bcmp(dname, nm, nmlen)) == 0) {
/* name matches */
parsedir_res = hs_parsedir(fsp, dirp, &hd,
(char *)NULL, (int *)NULL,
last_offset - *offset);
if (!parsedir_res) {
uint_t lbn; /* logical block number */
lbn = dhp->hs_dirent.ext_lbn +
dhp->hs_dirent.xar_len;
/*
* Need to do an fbrelse() on the buffer,
* as hs_makenode() may try to acquire
* hs_hashlock, which may not be required
* while a page is locked.
*/
fbrelse(fbp, S_READ);
did_fbrelse = 1;
*vpp = hs_makenode(&hd, lbn, *offset,
dvp->v_vfsp);
if (*vpp == NULL) {
*error = ENFILE;
RESTORE_NM(rrip_tmp_name, nm);
PD_return(FOUND_ENTRY)
}
dhp->hs_offset = *offset;
RESTORE_NM(rrip_tmp_name, nm);
PD_return(FOUND_ENTRY)
} else if (parsedir_res != EAGAIN) {
/* improper dir entry */
*error = parsedir_res;
RESTORE_NM(rrip_tmp_name, nm);
PD_return(FOUND_ENTRY)
}
} else if (strict_iso9660_ordering && !is_rrip &&
!HSFS_HAVE_LOWER_CASE(fsp) && res < 0) {
/* name < dir entry */
RESTORE_NM(rrip_tmp_name, nm);
PD_return(WENT_PAST)
}
/*
* name > dir entry,
* look at next one.
*/
skip_rec:
*offset += hdlen;
RESTORE_NM(rrip_tmp_name, nm);
}
PD_return(HIT_END)
do_ret:
if (rrip_name_str)
kmem_free(rrip_name_str, rrip_name_size);
if (rrip_tmp_name)
kmem_free(rrip_tmp_name, rrip_name_size);
if (!did_fbrelse)
fbrelse(fbp, S_READ);
return (err);
#undef PD_return
#undef RESTORE_NM
}
/*
* Strip trailing nulls or spaces from the name;
* return adjusted length. If we find such junk,
* log a non-conformant disk message.
*/
static int
strip_trailing(struct hsfs *fsp, char *nm, int len)
{
char *c;
int trailing_junk = 0;
for (c = nm + len - 1; c > nm; c--) {
if (*c == ' ' || *c == '\0')
trailing_junk = 1;
else
break;
}
if (trailing_junk)
hs_log_bogus_disk_warning(fsp, HSFS_ERR_TRAILING_JUNK, 0);
return ((int)(c - nm + 1));
}
static int
hs_namelen(struct hsfs *fsp, char *nm, int len)
{
char *p = nm + len;
if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) {
return (len);
} else if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
uint16_t c;
while (--p > &nm[1]) {
c = *p;
c |= *--p * 256;
if (c == ';')
return (p - nm);
if (c < '0' || c > '9') {
p++;
return (p - nm);
}
}
} else {
char c;
while (--p > nm) {
c = *p;
if (c == ';')
return (p - nm);
if (c < '0' || c > '9') {
p++;
return (p - nm);
}
}
}
return (len);
}
/*
* Take a UCS-2 character and convert
* it into a utf8 character.
* A 0 will be returned if the conversion fails
*
* See http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
*
* The code has been taken from udfs/udf_subr.c
*/
static uint8_t hs_first_byte_mark[7] =
{ 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
static int32_t
hs_ucs2_2_utf8(uint16_t c_16, uint8_t *s_8)
{
int32_t nc;
uint32_t c_32;
uint32_t byte_mask = 0xBF;
uint32_t byte_mark = 0x80;
/*
* Convert the 16-bit character to a 32-bit character
*/
c_32 = c_16;
/*
* By here the 16-bit character is converted
* to a 32-bit wide character
*/
if (c_32 < 0x80) {
nc = 1;
} else if (c_32 < 0x800) {
nc = 2;
} else if (c_32 < 0x10000) {
nc = 3;
} else if (c_32 < 0x200000) {
nc = 4;
} else if (c_32 < 0x4000000) {
nc = 5;
} else if (c_32 <= 0x7FFFFFFF) { /* avoid signed overflow */
nc = 6;
} else {
nc = 0;
}
s_8 += nc;
switch (nc) {
case 6 :
*(--s_8) = (c_32 | byte_mark) & byte_mask;
c_32 >>= 6;
/* FALLTHROUGH */
case 5 :
*(--s_8) = (c_32 | byte_mark) & byte_mask;
c_32 >>= 6;
/* FALLTHROUGH */
case 4 :
*(--s_8) = (c_32 | byte_mark) & byte_mask;
c_32 >>= 6;
/* FALLTHROUGH */
case 3 :
*(--s_8) = (c_32 | byte_mark) & byte_mask;
c_32 >>= 6;
/* FALLTHROUGH */
case 2 :
*(--s_8) = (c_32 | byte_mark) & byte_mask;
c_32 >>= 6;
/* FALLTHROUGH */
case 1 :
*(--s_8) = c_32 | hs_first_byte_mark[nc];
}
return (nc);
}
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Rock Ridge extensions to the System Use Sharing protocol
* for the High Sierra filesystem
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/signal.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/buf.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <sys/stat.h>
#include <sys/mode.h>
#include <sys/mkdev.h>
#include <sys/ddi.h>
#include <vm/page.h>
#include <sys/fs/hsfs_spec.h>
#include <sys/fs/hsfs_isospec.h>
#include <sys/fs/hsfs_node.h>
#include <sys/fs/hsfs_impl.h>
#include <sys/fs/hsfs_susp.h>
#include <sys/fs/hsfs_rrip.h>
#include <sys/statvfs.h>
#include <sys/mount.h>
#include <sys/swap.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include "fs/fs_subr.h"
#include <sys/cmn_err.h>
static void form_time(int, uchar_t *, struct timeval *);
static void name_parse(int, uchar_t *, size_t, uchar_t *, int *,
ulong_t *, int);
/*
* Signature table for RRIP
*/
ext_signature_t rrip_signature_table[ ] = {
RRIP_CL, rrip_child_link,
RRIP_NM, rrip_name,
RRIP_PL, rrip_parent_link,
RRIP_PN, rrip_dev_nodes,
RRIP_PX, rrip_file_attr,
RRIP_RE, rrip_reloc_dir,
RRIP_RR, rrip_rock_ridge,
RRIP_SL, rrip_sym_link,
RRIP_TF, rrip_file_time,
(char *)NULL, NULL
};
/*
* rrip_dev_nodes()
*
* sig_handler() for RRIP signature "PN"
*
* This function parses out the major and minor numbers from the "PN
* " SUF.
*/
uchar_t *
rrip_dev_nodes(sig_args_t *sig_args_p)
{
uchar_t *pn_ptr = sig_args_p->SUF_ptr;
major_t major_dev = (major_t)RRIP_MAJOR(pn_ptr);
minor_t minor_dev = (minor_t)RRIP_MINOR(pn_ptr);
sig_args_p->hdp->r_dev = makedevice(major_dev, minor_dev);
return (pn_ptr + SUF_LEN(pn_ptr));
}
/*
* rrip_file_attr()
*
* sig_handler() for RRIP signature "PX"
*
* This function parses out the file attributes of a file from the "PX"
* SUF. The attributes is finds are : st_mode, st_nlink, st_uid,
* and st_gid.
*/
uchar_t *
rrip_file_attr(sig_args_t *sig_args_p)
{
uchar_t *px_ptr = sig_args_p->SUF_ptr;
struct hs_direntry *hdp = sig_args_p->hdp;
hdp->mode = RRIP_MODE(px_ptr);
hdp->nlink = RRIP_NLINK(px_ptr);
hdp->uid = RRIP_UID(px_ptr);
hdp->gid = RRIP_GID(px_ptr);
if (SUF_LEN(px_ptr) >= RRIP_PX_SIZE)
hdp->inode = (ino64_t)RRIP_INO(px_ptr);
else
hdp->inode = 0;
hdp->type = IFTOVT(hdp->mode);
return (px_ptr + SUF_LEN(px_ptr));
}
/*
* rrip_file_time()
*
* support function for rrip_file_time()
*
* This function decides whether to parse the times in a long time form
* (17 bytes) or a short time form (7 bytes). These time formats are
* defined in the ISO 9660 specification.
*/
static void
form_time(int time_length, uchar_t *file_time, struct timeval *tvp)
{
if (time_length == ISO_DATE_LEN)
hs_parse_longdate(file_time, tvp);
else
hs_parse_dirdate(file_time, tvp);
}
/*
* rrip_file_time()
*
* sig_handler() for RRIP signature RRIP_TF
*
* This function parses out the file time attributes of a file from the
* "TI" SUF. The times it parses are : st_mtime, st_atime and st_ctime.
*
* The function form_time is a support function only used in this
* function.
*/
uchar_t *
rrip_file_time(sig_args_t *sig_args_p)
{
uchar_t *tf_ptr = sig_args_p->SUF_ptr;
if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_ACCESS_BIT)) {
form_time(RRIP_TF_TIME_LENGTH(tf_ptr),
RRIP_tf_access(tf_ptr),
&sig_args_p->hdp->adate);
}
if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_MODIFY_BIT)) {
form_time(RRIP_TF_TIME_LENGTH(tf_ptr), RRIP_tf_modify(tf_ptr),
&sig_args_p->hdp->mdate);
}
if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_ATTRIBUTES_BIT)) {
form_time(RRIP_TF_TIME_LENGTH(tf_ptr),
RRIP_tf_attributes(tf_ptr),
&sig_args_p->hdp->cdate);
}
return (tf_ptr + SUF_LEN(tf_ptr));
}
/*
* name_parse()
*
* This is a generic fuction used for sym links and filenames. The
* flags passed to it effect the way the name/component field is parsed.
*
* The return value will be the NAME_CONTINUE or NAME_CHANGE value.
*
*/
static void
name_parse(
int rrip_flags, /* component/name flag */
uchar_t *SUA_string, /* string from SUA */
size_t SUA_string_len, /* length of SUA string */
uchar_t *dst, /* string to copy to */
int *dst_lenp, /* ptr to cur. str len */
ulong_t *name_flags_p, /* internal name flags */
int dst_size) /* limit dest string to */
/* this value */
{
size_t off;
size_t len;
if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_ROOT))
dst[0] = 0;
if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_CURRENT)) {
SUA_string = (uchar_t *)".";
SUA_string_len = 1;
}
if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_PARENT)) {
SUA_string = (uchar_t *)"..";
SUA_string_len = 2;
}
/*
* XXX
* For now, ignore the following flags and return.
* have to figure out how to get host name in kernel.
* Unsure if this even should be done.
*/
if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_VOLROOT) ||
IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_HOST)) {
cmn_err(CE_NOTE,
"VOLUME ROOT and NAME_HOST currently unsupported\n");
return;
}
/*
* strlcat() has two nice properties:
* - the size of the output buffer includes the trailing '\0'
* - we pass "total size" not "remaining size"
* It'd be the ideal candidate for this codeblock - make it:
*
* strlcat(dst, SUA_string,
* MIN(dstsize, strlen(dst) + SUA_string_len + 1));
*
* Unfortunately, strlcat() cannot deal with input strings
* that are not NULL-terminated - like SUA_string can be in
* our case here. So we can't use it :(
* Now strncat() doesn't work either - because it doesn't deal
* with strings for which the 'potential NULL-termination' isn't
* accessible - strncat(dst, NULL, 0) crashes although it copies
* nothing in any case. If the SUA ends on a mapping boundary,
* then telling strncat() to copy no more than the remaining bytes
* in the buffer is of no avail if there's no NULL byte in them.
*
* Hence - binary copy. What are all these str* funcs for ??
*/
dst_size--; /* trailing '\0' */
off = strlen((char *)dst);
len = MIN(dst_size - off, SUA_string_len);
bcopy((char *)SUA_string, (char *)(dst + off), len);
dst[off + len] = '\0';
*dst_lenp = strlen((char *)dst);
if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_CONTINUE))
SET_NAME_BIT(*(name_flags_p), RRIP_NAME_CONTINUE);
else
SET_NAME_BIT(*(name_flags_p), RRIP_NAME_CHANGE);
}
/*
* rrip_name()
*
* sig_handler() for RRIP signature RRIP_NM
*
* This function handles the name of the current file. It is case
* sensitive to whatever was put into the field and does NO
* translation. It will take whatever characters were in the field.
*
* Because the flags effect the way the name is parsed the same way
* that the sym_link component parsing is done, we will use the same
* function to do the actual parsing.
*/
uchar_t *
rrip_name(sig_args_t *sig_args_p)
{
uchar_t *nm_ptr = sig_args_p->SUF_ptr;
if ((sig_args_p->name_p == (uchar_t *)NULL) ||
(sig_args_p->name_len_p == (int *)NULL))
goto end;
/*
* If we have a "." or ".." directory, we should not look for
* an alternate name
*/
if (HDE_NAME_LEN(sig_args_p->dirp) == 1) {
if (*((char *)HDE_name(sig_args_p->dirp)) == '\0') {
(void) strcpy((char *)sig_args_p->name_p, ".");
*sig_args_p->name_len_p = 1;
goto end;
} else if (*((char *)HDE_name(sig_args_p->dirp)) == '\1') {
(void) strcpy((char *)sig_args_p->name_p, "..");
*sig_args_p->name_len_p = 2;
goto end;
}
}
name_parse((int)RRIP_NAME_FLAGS(nm_ptr), RRIP_name(nm_ptr),
(size_t)RRIP_NAME_LEN(nm_ptr), sig_args_p->name_p,
sig_args_p->name_len_p, &(sig_args_p->name_flags),
MAXNAMELEN);
end:
return (nm_ptr + SUF_LEN(nm_ptr));
}
/*
* rrip_sym_link()
*
* sig_handler() for RRIP signature RRIP_SL
*
* creates a symlink buffer to simulate sym_links.
*/
uchar_t *
rrip_sym_link(sig_args_t *sig_args_p)
{
uchar_t *sl_ptr = sig_args_p->SUF_ptr;
uchar_t *comp_ptr;
char *tmp_sym_link;
struct hs_direntry *hdp = sig_args_p->hdp;
int sym_link_len;
char *sym_link;
if (hdp->type != VLNK)
goto end;
/*
* If the sym link has already been created, don't recreate it
*/
if (IS_NAME_BIT_SET(sig_args_p->name_flags, RRIP_SYM_LINK_COMPLETE))
goto end;
sym_link = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
/*
* If there is an original string put it into sym_link[], otherwise
* initialize sym_link[] to the empty string.
*/
if (hdp->sym_link != (char *)NULL) {
sym_link_len = (int)strlen(strcpy(sym_link, hdp->sym_link));
} else {
sym_link[0] = '\0';
sym_link_len = 0;
}
/* for all components */
for (comp_ptr = RRIP_sl_comp(sl_ptr);
comp_ptr < (sl_ptr + SUF_LEN(sl_ptr));
comp_ptr += RRIP_COMP_LEN(comp_ptr)) {
name_parse((int)RRIP_COMP_FLAGS(comp_ptr),
RRIP_comp(comp_ptr),
(size_t)RRIP_COMP_NAME_LEN(comp_ptr), (uchar_t *)sym_link,
&sym_link_len, &(sig_args_p->name_flags),
MAXPATHLEN);
/*
* If the component is continued don't put a '/' in
* the pathname, but do NULL terminate it.
*/
if (IS_NAME_BIT_SET(RRIP_COMP_FLAGS(comp_ptr),
RRIP_NAME_CONTINUE)) {
sym_link[sym_link_len] = '\0';
} else {
sym_link[sym_link_len] = '/';
sym_link[sym_link_len + 1] = '\0';
/* add 1 to sym_link_len for '/' */
sym_link_len++;
}
}
/*
* If we reached the end of the symbolic link, take out the
* last slash, but don't change ROOT "/" to an empty string.
*/
if (!IS_NAME_BIT_SET(RRIP_SL_FLAGS(sl_ptr), RRIP_NAME_CONTINUE) &&
sym_link_len > 1 && sym_link[sym_link_len - 1] == '/')
sym_link[--sym_link_len] = '\0';
/*
* if no memory has been allocated, get some, otherwise, append
* to the current allocation
*/
tmp_sym_link = kmem_alloc(SYM_LINK_LEN(sym_link), KM_SLEEP);
(void) strcpy(tmp_sym_link, sym_link);
if (hdp->sym_link != (char *)NULL)
kmem_free(hdp->sym_link, (size_t)(hdp->ext_size + 1));
hdp->sym_link = (char *)&tmp_sym_link[0];
/* the size of a symlink is its length */
hdp->ext_size = (uint_t)strlen(tmp_sym_link);
if (!IS_NAME_BIT_SET(RRIP_SL_FLAGS(sl_ptr), RRIP_NAME_CONTINUE)) {
/* reached the end of the symbolic link */
SET_NAME_BIT(sig_args_p->name_flags, RRIP_SYM_LINK_COMPLETE);
}
kmem_free(sym_link, MAXPATHLEN + 1);
end:
return (sl_ptr + SUF_LEN(sl_ptr));
}
/*
* rrip_namecopy()
*
* This function will copy the rrip name to the "to" buffer, if it
* exists.
*
* XXX - We should speed this up by implementing the search in
* parse_sua(). It works right now, so I don't want to mess with it.
*/
int
rrip_namecopy(
char *from, /* name to copy */
char *to, /* string to copy "from" to */
char *tmp_name, /* temp storage for original name */
uchar_t *dirp, /* directory entry pointer */
uint_t last_offset, /* last index into current dir block */
struct hsfs *fsp, /* filesystem pointer */
struct hs_direntry *hdp) /* directory entry pointer to put */
/* all that good info in */
{
int size = 0;
int change_flag = 0;
int ret_val;
if ((to == (char *)NULL) ||
(from == (char *)NULL) ||
(dirp == (uchar_t *)NULL)) {
return (0);
}
/* special handling for '.' and '..' */
if (HDE_NAME_LEN(dirp) == 1) {
if (*((char *)HDE_name(dirp)) == '\0') {
(void) strcpy(to, ".");
return (1);
} else if (*((char *)HDE_name(dirp)) == '\1') {
(void) strcpy(to, "..");
return (2);
}
}
ret_val = parse_sua((uchar_t *)to, &size, &change_flag,
dirp, last_offset, hdp, fsp, NULL, 0);
if (IS_NAME_BIT_SET(change_flag, RRIP_NAME_CHANGE) && !ret_val)
return (size);
/*
* Well, the name was not found
*
* make rripname an upper case "nm" (to), so that
* we can compare it the current HDE_DIR_NAME()
* without nuking the original "nm", for future case
* sensitive name comparing
*/
(void) strcpy(tmp_name, from); /* keep original */
size = hs_uppercase_copy(tmp_name, from, (int)strlen(from));
return (-1);
}
/*
* rrip_reloc_dir()
*
* This function is fairly bogus. All it does is cause a failure of
* the hs_parsedir, so that no vnode will be made for it and
* essentially, the directory will no longer be seen. This is part
* of the directory hierarchy mess, where the relocated directory will
* be hidden as far as ISO 9660 is concerned. When we hit the child
* link "CL" SUF, then we will read the new directory.
*/
uchar_t *
rrip_reloc_dir(sig_args_t *sig_args_p)
{
uchar_t *re_ptr = sig_args_p->SUF_ptr;
sig_args_p->flags = RELOC_DIR;
return (re_ptr + SUF_LEN(re_ptr));
}
/*
* rrip_child_link()
*
* This is the child link part of the directory hierarchy stuff and
* this does not really do much anyway. All it does is read the
* directory entry that the child link SUF contains. All should be
* fine then.
*/
uchar_t *
rrip_child_link(sig_args_t *sig_args_p)
{
uchar_t *cl_ptr = sig_args_p->SUF_ptr;
sig_args_p->hdp->ext_lbn = RRIP_CHILD_LBN(cl_ptr);
hs_filldirent(sig_args_p->fsp->hsfs_rootvp, sig_args_p->hdp);
sig_args_p->flags = 0;
return (cl_ptr + SUF_LEN(cl_ptr));
}
/*
* rrip_parent_link()
*
* This is the parent link part of the directory hierarchy stuff and
* this does not really do much anyway. All it does is read the
* directory entry that the parent link SUF contains. All should be
* fine then.
*/
uchar_t *
rrip_parent_link(sig_args_t *sig_args_p)
{
uchar_t *pl_ptr = sig_args_p->SUF_ptr;
sig_args_p->hdp->ext_lbn = RRIP_PARENT_LBN(pl_ptr);
hs_filldirent(sig_args_p->fsp->hsfs_rootvp, sig_args_p->hdp);
sig_args_p->flags = 0;
return (pl_ptr + SUF_LEN(pl_ptr));
}
/*
* rrip_rock_ridge()
*
* This function is supposed to aid in speed of the filesystem.
*
* XXX - It is only here as a place holder so far.
*/
uchar_t *
rrip_rock_ridge(sig_args_t *sig_args_p)
{
uchar_t *rr_ptr = sig_args_p->SUF_ptr;
return (rr_ptr + SUF_LEN(rr_ptr));
}
/*
* 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
*/
/*
* Miscellaneous support subroutines for High Sierra filesystem
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/cmn_err.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/user.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/proc.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/uio.h>
#include <vm/hat.h>
#include <vm/as.h>
#include <vm/seg.h>
#include <vm/page.h>
#include <vm/pvn.h>
#include <vm/seg_map.h>
#include <sys/swap.h>
#include <vm/seg_kmem.h>
#include <sys/fs/hsfs_spec.h>
#include <sys/fs/hsfs_node.h>
#include <sys/fs/hsfs_impl.h>
#define THE_EPOCH 1970
#define END_OF_TIME 2099
extern int hsfs_lostpage;
#ifdef __STDC__
static time_t hs_date_to_gmtime(int year, int mon, int day, int gmtoff);
#else
static time_t hs_date_to_gmtime();
#endif
/*
* Table used in logging non-fatal errors which should be recorded
* once per mount. Indexed by HSFS_ERR values (defined in hsfs_node.h).
*/
struct hsfs_error {
char *hdr_text; /* msg prefix: general error type */
/* must contain %s for mnt pt */
char *err_text; /* specific error message */
uchar_t multiple; /* > 1 such error per fs possible? */
uchar_t n_printf_args; /* if err_text printf-like, # addtl args */
} hsfs_error[] = {
/* HSFS_ERR_TRAILING_JUNK */
"hsfs: Warning: the file system mounted on %s "
"does not conform to the ISO-9660 specification:",
"trailing blanks or null characters in file or directory name.\n",
1, 0,
/* HSFS_ERR_LOWER_CASE_NM */
"hsfs: Warning: the file system mounted on %s "
"does not conform to the ISO-9660 specification:",
"lower case characters in file or directory name.\n",
1, 0,
/* HSFS_ERR_BAD_ROOT_DIR */
"hsfs: Warning: the file system mounted on %s "
"does not conform to the ISO-9660 specification:",
"invalid root directory.\n",
0, 0,
/* HSFS_ERR_UNSUP_TYPE */
"hsfs: Warning: the file system mounted on %s "
"contains a file or directory with an unsupported type:",
" 0x%x.\n",
1, 1,
/* HSFS_ERR_BAD_FILE_LEN */
"hsfs: Warning: file system mounted on %s "
"does not conform to the ISO-9660 specification:",
"file name length greater than max allowed\n",
1, 0,
/* HSFS_ERR_BAD_JOLIET_FILE_LEN */
"hsfs: Warning: file system mounted on %s "
"does not conform to the Joliet specification:",
"file name length greater than max allowed\n",
1, 0,
/* HSFS_ERR_TRUNC_JOLIET_FILE_LEN */
"hsfs: Warning: file system mounted on %s "
"does not conform to the Joliet specification:",
"file name length greater than MAXNAMELEN (truncated)\n",
1, 0,
/* HSFS_ERR_BAD_DIR_ENTRY */
"hsfs: Warning: file system mounted on %s "
"has inconsistent data:",
"invalid directory or file name length (ignored)\n",
1, 0,
/* HSFS_ERR_NEG_SUA_LEN */
"hsfs: Warning: file system mounted on %s "
"has inconsistent Rock Ridge data:",
"negative SUA len\n",
1, 0,
/* HSFS_ERR_BAD_SUA_LEN */
"hsfs: Warning: file system mounted on %s "
"has inconsistent Rock Ridge data:",
"SUA len too big\n",
1, 0,
};
/*
* Local datatype for defining tables of (Offset, Name) pairs for
* kstats.
*/
typedef struct {
offset_t index;
char *name;
} hsfs_ksindex_t;
static const hsfs_ksindex_t hsfs_kstats[] = {
{ 0, "mountpoint" },
{ 1, "pages_lost" },
{ 2, "physical_read_pages" },
{ 3, "cache_read_pages" },
{ 4, "readahead_pages" },
{ 5, "coalesced_pages" },
{ 6, "total_pages_requested" },
{-1, NULL }
};
/*
* hs_parse_dirdate
*
* Parse the short 'directory-format' date into a Unix timeval.
* This is the date format used in Directory Entries.
*
* If the date is not representable, make something up.
*/
void
hs_parse_dirdate(dp, tvp)
uchar_t *dp;
struct timeval *tvp;
{
int year, month, day, hour, minute, sec, gmtoff;
year = HDE_DATE_YEAR(dp);
month = HDE_DATE_MONTH(dp);
day = HDE_DATE_DAY(dp);
hour = HDE_DATE_HOUR(dp);
minute = HDE_DATE_MIN(dp);
sec = HDE_DATE_SEC(dp);
gmtoff = HDE_DATE_GMTOFF(dp);
tvp->tv_usec = 0;
if (year < THE_EPOCH) {
tvp->tv_sec = 0;
} else {
tvp->tv_sec = hs_date_to_gmtime(year, month, day, gmtoff);
if (tvp->tv_sec != -1) {
tvp->tv_sec += ((hour * 60) + minute) * 60 + sec;
}
}
return;
}
/*
* hs_parse_longdate
*
* Parse the long 'user-oriented' date into a Unix timeval.
* This is the date format used in the Volume Descriptor.
*
* If the date is not representable, make something up.
*/
void
hs_parse_longdate(dp, tvp)
uchar_t *dp;
struct timeval *tvp;
{
int year, month, day, hour, minute, sec, gmtoff;
year = HSV_DATE_YEAR(dp);
month = HSV_DATE_MONTH(dp);
day = HSV_DATE_DAY(dp);
hour = HSV_DATE_HOUR(dp);
minute = HSV_DATE_MIN(dp);
sec = HSV_DATE_SEC(dp);
gmtoff = HSV_DATE_GMTOFF(dp);
tvp->tv_usec = 0;
if (year < THE_EPOCH) {
tvp->tv_sec = 0;
} else {
tvp->tv_sec = hs_date_to_gmtime(year, month, day, gmtoff);
if (tvp->tv_sec != -1) {
tvp->tv_sec += ((hour * 60) + minute) * 60 + sec;
tvp->tv_usec = HSV_DATE_HSEC(dp) * 10000;
}
}
}
/* cumulative number of seconds per month, non-leap and leap-year versions */
static time_t cum_sec[] = {
0x0, 0x28de80, 0x4dc880, 0x76a700, 0x9e3400, 0xc71280,
0xee9f80, 0x1177e00, 0x1405c80, 0x167e980, 0x190c800, 0x1b85500
};
static time_t cum_sec_leap[] = {
0x0, 0x28de80, 0x4f1a00, 0x77f880, 0x9f8580, 0xc86400,
0xeff100, 0x118cf80, 0x141ae00, 0x1693b00, 0x1921980, 0x1b9a680
};
#define SEC_PER_DAY 0x15180
#define SEC_PER_YEAR 0x1e13380
/*
* hs_date_to_gmtime
*
* Convert year(1970-2099)/month(1-12)/day(1-31) to seconds-since-1970/1/1.
*
* Returns -1 if the date is out of range.
*/
static time_t
hs_date_to_gmtime(year, mon, day, gmtoff)
int year;
int mon;
int day;
int gmtoff;
{
time_t sum;
time_t *cp;
int y;
if ((year < THE_EPOCH) || (year > END_OF_TIME) ||
(mon < 1) || (mon > 12) ||
(day < 1) || (day > 31))
return (-1);
/*
* Figure seconds until this year and correct for leap years.
* Note: 2000 is a leap year but not 2100.
*/
y = year - THE_EPOCH;
sum = y * SEC_PER_YEAR;
sum += ((y + 1) / 4) * SEC_PER_DAY;
/*
* Point to the correct table for this year and
* add in seconds until this month.
*/
cp = ((y + 2) % 4) ? cum_sec : cum_sec_leap;
sum += cp[mon - 1];
/*
* Add in seconds until 0:00 of this day.
* (days-per-month validation is not done here)
*/
sum += (day - 1) * SEC_PER_DAY;
sum -= (gmtoff * 15 * 60);
return (sum);
}
/*
* Indicate whether the directory is valid.
*/
int
hsfs_valid_dir(hd)
struct hs_direntry *hd;
{
/*
* check to see if this directory is not marked as a directory.
* check to see if data length is zero.
*/
if (hd->ext_size == 0)
return (0);
if (hd->type != VDIR)
return (0);
return (1);
}
/*
* If we haven't complained about this error type yet, do.
*/
void
hs_log_bogus_disk_warning(fsp, errtype, data)
struct hsfs *fsp;
int errtype;
uint_t data;
{
if (fsp->hsfs_err_flags & (1 << errtype))
return; /* already complained */
cmn_err(CE_NOTE, hsfs_error[errtype].hdr_text,
fsp->hsfs_fsmnt);
switch (hsfs_error[errtype].n_printf_args) {
case 0:
cmn_err(CE_CONT, hsfs_error[errtype].err_text);
break;
case 1:
cmn_err(CE_CONT, hsfs_error[errtype].err_text, data);
break;
default:
/* don't currently handle more than 1 arg */
cmn_err(CE_CONT, "unknown problem; internal error.\n");
}
cmn_err(CE_CONT,
"Due to this error, the file system may not be correctly interpreted.\n");
if (hsfs_error[errtype].multiple)
cmn_err(CE_CONT,
"Other such errors in this file system will be silently ignored.\n\n");
else
cmn_err(CE_CONT, "\n");
fsp->hsfs_err_flags |= (1 << errtype);
}
/*
* Callback from kstat framework. Grab a snapshot of the current hsfs
* counters and populate the kstats.
*/
static int
hsfs_kstats_update(kstat_t *ksp, int flag)
{
struct hsfs *fsp;
kstat_named_t *knp;
uint64_t pages_lost;
uint64_t physical_read_bytes;
uint64_t cache_read_pages;
uint64_t readahead_bytes;
uint64_t coalesced_bytes;
uint64_t total_pages_requested;
if (flag != KSTAT_READ)
return (EACCES);
fsp = ksp->ks_private;
knp = ksp->ks_data;
mutex_enter(&(fsp->hqueue->strategy_lock));
mutex_enter(&(fsp->hqueue->hsfs_queue_lock));
cache_read_pages = fsp->cache_read_pages;
pages_lost = hsfs_lostpage;
physical_read_bytes = fsp->physical_read_bytes;
readahead_bytes = fsp->readahead_bytes;
coalesced_bytes = fsp->coalesced_bytes;
total_pages_requested = fsp->total_pages_requested;
mutex_exit(&(fsp->hqueue->strategy_lock));
mutex_exit(&(fsp->hqueue->hsfs_queue_lock));
knp++;
(knp++)->value.ui64 = pages_lost;
(knp++)->value.ui64 = howmany(physical_read_bytes, PAGESIZE);
(knp++)->value.ui64 = cache_read_pages;
(knp++)->value.ui64 = howmany(readahead_bytes, PAGESIZE);
(knp++)->value.ui64 = howmany(coalesced_bytes, PAGESIZE);
(knp++)->value.ui64 = total_pages_requested;
return (0);
}
/*
* Initialize hsfs kstats, which are all name value pairs with
* values being various counters.
*/
static kstat_t *
hsfs_setup_named_kstats(struct hsfs *fsp, int fsid, char *name,
const hsfs_ksindex_t *ksip, int (*update)(kstat_t *, int))
{
kstat_t *ksp;
kstat_named_t *knp;
char *np;
char *mntpt = fsp->hsfs_fsmnt;
size_t size;
size = (sizeof (hsfs_kstats)) / (sizeof (hsfs_ksindex_t));
ksp = kstat_create("hsfs_fs", fsid, name, "hsfs",
KSTAT_TYPE_NAMED, size-1, KSTAT_FLAG_VIRTUAL);
if (ksp == NULL)
return (NULL);
ksp->ks_data = kmem_alloc(sizeof (kstat_named_t) * size, KM_SLEEP);
ksp->ks_private = fsp;
ksp->ks_update = update;
ksp->ks_data_size += strlen(mntpt) + 1;
knp = ksp->ks_data;
kstat_named_init(knp, ksip->name, KSTAT_DATA_STRING);
kstat_named_setstr(knp, mntpt);
knp++;
ksip++;
for (; (np = ksip->name) != NULL; ++knp, ++ksip) {
kstat_named_init(knp, np, KSTAT_DATA_UINT64);
}
kstat_install(ksp);
return (ksp);
}
void
hsfs_init_kstats(struct hsfs *fsp, int fsid)
{
fsp->hsfs_kstats = hsfs_setup_named_kstats(fsp, fsid, "hsfs_read_stats",
hsfs_kstats, hsfs_kstats_update);
}
void
hsfs_fini_kstats(struct hsfs *fsp)
{
void *data;
if (fsp->hsfs_kstats != NULL) {
data = fsp->hsfs_kstats->ks_data;
kstat_delete(fsp->hsfs_kstats);
kmem_free(data, sizeof (kstat_named_t) *
(sizeof (hsfs_kstats)) / (sizeof (hsfs_ksindex_t)));
}
fsp->hsfs_kstats = NULL;
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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
*/
/*
* ISO 9660 System Use Sharing Protocol extension filesystem specifications
* Copyright (c) 1991,2000,2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/signal.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/buf.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <vm/page.h>
#include <sys/fs/hsfs_spec.h>
#include <sys/fs/hsfs_isospec.h>
#include <sys/fs/hsfs_node.h>
#include <sys/fs/hsfs_impl.h>
#include <sys/fs/hsfs_susp.h>
#include <sys/fs/hsfs_rrip.h>
#include <sys/statvfs.h>
#include <sys/mount.h>
#include <sys/swap.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include "fs/fs_subr.h"
#include <sys/cmn_err.h>
/*
* Common Signatures for all SUSP
*/
ext_signature_t susp_signature_table[ ] = {
SUSP_SP, share_protocol, /* must be first in table */
SUSP_CE, share_continue, /* must be second in table */
SUSP_PD, share_padding,
SUSP_ER, share_ext_ref,
SUSP_ST, share_stop,
(char *)NULL, NULL
};
/*
* These are global pointers referring to the above table, so the
* positions must not change as marked on the right above.
*/
ext_signature_t *susp_sp = &susp_signature_table[0];
ext_signature_t *susp_ce = &susp_signature_table[1];
/*
* ext name version implemented signature table
*
* the SUSP must be the first entry in the table.
* the RRIP must be the second entry in the table. We need to be able
* to check the RRIP bit being set, so we must know it's position.
* RRIP_BIT is set to 2 in rrip.h
*/
extension_name_t extension_name_table[] = {
"SUSP_1991A", SUSP_VERSION, susp_signature_table, /* #1 */
RRIP_ER_EXT_ID, RRIP_EXT_VERSION, rrip_signature_table,
(char *)NULL, 0, (ext_signature_t *)NULL
};
extension_name_t *susp_ext_name = extension_name_table;
/*
* share_protocol()
*
* sig_handler() for SUSP signature "SP"
*
* This function looks for the "SP" signature field, which means that
* the SUSP is supported on the current CD-ROM. It looks for the word
* 0xBEEF in the signature. If that exists, the SUSP is implemented.
* The function will then set the implemented bit in the "SUSP" entry
* of the extention_name_table[]. If the bytes don't match, then we
* return a big fat NULL and treat this as an ISO 9660 CD-ROM.
*/
uchar_t *
share_protocol(sig_args_t *sig_args_p)
{
uchar_t *sp_ptr = sig_args_p->SUF_ptr;
/* Let's check the check bytes */
if (!CHECK_BYTES_OK(sp_ptr))
return ((uchar_t *)NULL);
/*
* Ah, we have the go ahead, so let's set the implemented bit
* of the SUSP in the extension_name_table[]
*/
if (SUSP_VERSION < SUF_VER(sp_ptr)) {
cmn_err(CE_NOTE,
"System Use Sharing Protocol ver. %d:not supported\n",
(int)SUF_VER(sp_ptr));
return ((uchar_t *)NULL);
}
SET_SUSP_BIT(sig_args_p->fsp);
sig_args_p->fsp->hsfs_sua_off = SP_SUA_OFFSET(sp_ptr);
return (sp_ptr + SUF_LEN(sp_ptr));
}
/*
* share_ext_ref()
*
* sig_handler() for SUSP signature "ER"
*
* This function looks for the "ER" signature field, which lists an
* extension that is implemented on the current CD-ROM. The function
* will then search through the extention_name_table[], looking for the
* extension reference in this SUF.
*
* If the correct extension reference is found, and the version number
* in the "ER" SUF is less than or equal to the version specified in
* the extension_name_table, the implemented bit will be set to 1.
*
* If the version number in the "ER" field is greater than that in the
* extension_name_table or no reference can be matched, the reference
* will be skipped the function will return the next field.
*/
uchar_t *
share_ext_ref(sig_args_t *sig_args_p)
{
uchar_t *er_ptr = sig_args_p->SUF_ptr;
extension_name_t *extnp;
int index;
/*
* Find appropriate extension and signature table
*/
for (extnp = extension_name_table, index = 0;
extnp->extension_name != (char *)NULL;
extnp++, index++) {
if (strncmp(extnp->extension_name,
(char *)ER_ext_id(er_ptr),
ER_ID_LEN(er_ptr)) == 0) {
SET_IMPL_BIT(sig_args_p->fsp, index);
}
}
return (er_ptr + SUF_LEN(er_ptr));
}
/*
* share_continue()
*
* sig_handler() for SUSP signature "CE"
*
* This function looks for the "CE" signature field. This means that
* the SUA is continued in another block on the CD-ROM. Because it is
* not a requirement that this "CE" field come at the end of the SUA,
* this function will only set up a structure containing the
* information needed to read the next SUA, somewhere on the disk.
*
* The end of the SUA is signaled by 2 NULL bytes, where the next
* signature would have been.
*
* This one will be tough to implement.
*/
uchar_t *
share_continue(sig_args_t *sig_args_p)
{
uchar_t *ce_ptr = sig_args_p->SUF_ptr;
sig_args_p->cont_info_p->cont_lbn = CE_BLK_LOC(ce_ptr);
sig_args_p->cont_info_p->cont_offset = CE_OFFSET(ce_ptr);
sig_args_p->cont_info_p->cont_len = CE_CONT_LEN(ce_ptr);
return (ce_ptr + SUF_LEN(ce_ptr));
}
/*
* share_padding()
*
* sig_handler() for SUSP signature "PD"
*
* All this function is needed for is bypassing a certain number of
* bytes. So, we just advance past this field and we're set.
*/
uchar_t *
share_padding(sig_args_t *sig_args_p)
{
uchar_t *pd_ptr = sig_args_p->SUF_ptr;
return (pd_ptr + SUF_LEN(pd_ptr));
}
/*
* share_stop()
*
* sig_handler() for SUSP signature "ST"
*
* All this is used for is signaling the end of an SUA.
* It fills the flag variable with the
*/
uchar_t *
share_stop(sig_args_t *sig_args_p)
{
uchar_t *st_ptr = sig_args_p->SUF_ptr;
sig_args_p->flags = END_OF_SUA; /* stop parsing the SUA NOW!!!! */
return (st_ptr + SUF_LEN(st_ptr));
}
/*
* 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
*/
/*
* System Use Sharing protocol subroutines for High Sierra filesystem
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/signal.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/buf.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <vm/page.h>
#include <sys/fs/hsfs_spec.h>
#include <sys/fs/hsfs_isospec.h>
#include <sys/fs/hsfs_node.h>
#include <sys/fs/hsfs_impl.h>
#include <sys/fs/hsfs_susp.h>
#include <sys/fs/hsfs_rrip.h>
#include <sys/statvfs.h>
#include <sys/mount.h>
#include <sys/swap.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include "fs/fs_subr.h"
#include <sys/cmn_err.h>
/* static declarations */
static void free_cont_area(uchar_t *);
static int get_cont_area(struct hsfs *, uchar_t **, cont_info_t *);
static int parse_signatures(sig_args_t *, int, uchar_t *, int);
/*
* parse_sua()
*
* This is the main SUSP routine, that gets all the SUA areas and
* continuations. It calls parse_signatures() to actually interpret
* the signature fields.
*
* XXX - need to implement signature searching to speed things up and
* which is needed for the api, which isn't done yet.
*/
int
parse_sua(
uchar_t *name_p, /* location to copy name */
int *name_len_p, /* location to put name len */
int *name_change_p, /* flags to signal name chg */
uchar_t *dirp, /* pointer to ISO dir entry */
uint_t last_offset, /* last ind. in cur. dirblock */
struct hs_direntry *hdp, /* loc to store dir info */
struct hsfs *fsp, /* filesystem pointer */
uchar_t *search_sig, /* signature to search for */
int search_num) /* n^th sig to search for */
{
uchar_t *SUA_p = IDE_sys_use_area(dirp);
int SUA_len = IDE_SUA_LEN(dirp);
uchar_t *tmp_SUA_p = (SUA_p + fsp->hsfs_sua_off);
int tmp_SUA_len = (SUA_len - fsp->hsfs_sua_off);
short ret_val = -1;
uchar_t *cont_p = (uchar_t *)NULL;
sig_args_t sig_args;
cont_info_t cont_info;
/*
* If there is no SUA, just return, no error
*/
if (SUA_len == 0)
return (0);
/*
* Underflow on the length field means there's a mismatch
* between sizes of SUA and ISO directory entry. This entry
* is corrupted, return an appropriate error.
*/
if (SUA_len < 0) {
hs_log_bogus_disk_warning(fsp, HSFS_ERR_NEG_SUA_LEN, 0);
return (SUA_EINVAL);
}
if ((tmp_SUA_p + tmp_SUA_len) > (dirp + last_offset)) {
hs_log_bogus_disk_warning(fsp, HSFS_ERR_BAD_SUA_LEN, 0);
return (SUA_EINVAL);
}
/*
* Make sure that the continuation lenth is zero, as that is
* the way to tell if we must grab another continuation area.
*/
bzero((char *)&cont_info, sizeof (cont_info));
sig_args.dirp = dirp;
sig_args.name_p = name_p;
sig_args.name_len_p = name_len_p;
sig_args.SUF_ptr = tmp_SUA_p;
sig_args.hdp = hdp;
sig_args.fsp = fsp;
sig_args.cont_info_p = &cont_info;
sig_args.flags = 0;
sig_args.name_flags = 0;
/*
* Get ready to put in a new name. If no "NM" is found, then
* hs_namecopy will come to the rescue. Make sure you don't
* have NULL names, also.
*/
if (name_p)
*(name_p) = '\0';
if (name_len_p)
*(name_len_p) = 0;
while (ret_val == -1) {
switch (parse_signatures(&sig_args, tmp_SUA_len, search_sig,
search_num)) {
case END_OF_SUA :
if (cont_info.cont_len) {
if (get_cont_area(fsp, &cont_p, &cont_info)) {
ret_val = 1;
goto clean_up;
}
sig_args.SUF_ptr =
cont_p + cont_info.cont_offset;
tmp_SUA_len = cont_info.cont_len;
cont_info.cont_len = 0;
continue;
}
sig_args.flags = 0; /* reset */
ret_val = 0; /* keep going */
break;
case SUA_NULL_POINTER:
ret_val = SUA_NULL_POINTER;
goto clean_up;
case SUA_ENOMEM:
ret_val = SUA_ENOMEM;
goto clean_up;
case SUA_EINVAL:
ret_val = SUA_EINVAL;
goto clean_up;
case RELOC_DIR:
ret_val = RELOC_DIR;
goto clean_up;
}
}
if (ret_val != 0)
goto clean_up;
if (IS_NAME_BIT_SET(sig_args.name_flags, RRIP_NAME_CHANGE))
SET_NAME_BIT(*(name_change_p), RRIP_NAME_CHANGE);
clean_up:
free_cont_area(cont_p);
return (ret_val);
}
/*
* parse_signatures()
*
* Find the correct handling function for the signature string that is
* passed to this function.
*
* signature searching:
*
* The two arguments of search_sig and search_num are for finding the
* search_num^th occurance of the signature search_sig. This will come
* in handy with searching for the "NM" field and is part of the api
* for rrip (which really can be used for any extension).
*/
/*ARGSUSED*/
static int
parse_signatures(
sig_args_t *sig_args_p,
int SUA_len,
uchar_t *search_sig, /* possible signature to search for */
int search_num) /* n^th occurance of search_sig to */
/* search for */
{
uchar_t *sig_string = sig_args_p->SUF_ptr;
extension_name_t *extnp;
ext_signature_t *ext_sigp;
int impl_bit_num = 0;
int SUA_rem = SUA_len; /* SUA length */
/* remaining to be parsed */
/* This should never happen ... just so we don't panic, literally */
if (sig_string == (uchar_t *)NULL)
return (SUA_NULL_POINTER);
if (SUA_len < 0)
return (SUA_EINVAL);
/*
* Until the end of SUA, search for the signatures
* (check for end of SUA (2 consecutive NULL bytes)) or the
* remaining length of the SUA is <= 3. The minimum signature
* field is 4.
*/
while ((SUA_rem >= SUF_MIN_LEN) && (*sig_string != '\0') &&
(*(sig_string + 1) != '\0')) {
/*
* Find appropriate extension and signature table
*/
for (extnp = extension_name_table, impl_bit_num = 0;
extnp->extension_name != (char *)NULL;
extnp++, impl_bit_num++) {
/*
* look at an extension only if it is implemented
* on the CD-ROM
*/
if (!IS_IMPL_BIT_SET(sig_args_p->fsp, impl_bit_num))
continue;
/*
* Find the appropriate signature
*/
for (ext_sigp = extnp->signature_table;
ext_sigp->ext_signature != (char *)NULL;
ext_sigp++) {
if (strncmp((char *)sig_string,
ext_sigp->ext_signature,
SUF_SIG_LEN) == 0) {
SUA_rem -= SUF_LEN(sig_string);
if (SUA_rem < 0)
return (END_OF_SUA);
/*
* The SUA_len parameter specifies the
* length of the SUA that the kernel
* expects. There is also a length
* encoded in the SUA data. If they
* do not agree, bail out.
*/
if (SUA_len < SUF_LEN(sig_string)) {
cmn_err(CE_NOTE,
"parse_signatures: SUA length too big: "
"expected=%d, found=%d",
SUA_len,
SUF_LEN(sig_string));
return (SUA_EINVAL);
}
sig_args_p->SUF_ptr = sig_string;
sig_string =
(ext_sigp->sig_handler)(sig_args_p);
switch (sig_args_p->flags) {
case END_OF_SUA :
return (END_OF_SUA);
case SUA_ENOMEM :
return (SUA_ENOMEM);
case SUA_EINVAL :
return (SUA_EINVAL);
case RELOC_DIR :
return (RELOC_DIR);
default :
#if NAME_SEARCH
case NAME_CONTINUE :
/* nothing for now */
case NAME_CHANGE :
/* nothing for now */
#endif
break;
}
/* reset to be zero */
sig_args_p->flags = 0;
goto next_signature;
}
/* off to the next signature .... */
} /* for ext_sigp */
} /* for extnp (extension parsing) */
/*
* Opps, did not find this signature. We must
* advance on the the next signature in the SUA
* and pray to persumedly omniscient, omnipresent,
* almighty transcendental being(s) that the next
* record is in the susp format, or we get hosed.
*/
if (SUA_rem < SUF_MIN_LEN)
return (END_OF_SUA);
SUA_rem -= SUF_LEN(sig_string);
sig_string += SUF_LEN(sig_string);
next_signature:
/*
* Failsafe
*/
if (SUA_rem < SUF_MIN_LEN ||
sig_string == NULL || SUF_LEN(sig_string) <= 0) {
return (END_OF_SUA);
}
} /* while */
return (END_OF_SUA);
}
/*
* hs_fill_root_dirent()
*
*
* This function reads the root directory extent to get to the SUA of
* the "." entry of the root directory. It the checks to see if the
* susp is implemented.
*/
void
hs_check_root_dirent(struct vnode *vp, struct hs_direntry *hdp)
{
struct buf *secbp;
uchar_t *root_ptr;
uchar_t *secp;
uint_t secno;
offset_t secoff;
sig_args_t sig_args;
struct hsfs *fsp;
int error;
if (vp->v_type != VDIR) {
cmn_err(CE_NOTE,
"hs_check_root_dirent: vp (0x%p) not a directory",
(void *)vp);
return;
}
bzero((caddr_t)&sig_args, sizeof (sig_args));
fsp = VFS_TO_HSFS(vp->v_vfsp);
secno = LBN_TO_SEC(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp);
secoff = LBN_TO_BYTE(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp) &
MAXHSOFFSET;
secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE,
"hs_check_root_dirent: bread: error=(%d)", error);
goto end;
}
secp = (uchar_t *)secbp->b_un.b_addr;
root_ptr = &secp[secoff];
/* quick check */
if (hdp->ext_lbn != HDE_EXT_LBN(root_ptr)) {
cmn_err(CE_NOTE, "hs_check_root_dirent: dirent not match\n");
/* keep on going */
}
/*
* Here, we know that the "." entry is the first in the sector
* just read (ISO 9660). Let's now check for the sharing
* protocol and set call the susp sig_handler() if we should.
* Then we run through the hs_parsedir() function to catch all
* the other possibilities of SUSP fields and continuations.
*
* If there is no SUA area, just return, and assume ISO.
*
* If the SUA area length is invalid (negative, due to a mismatch
* between dirent size and SUA size), return and hope for the best.
*/
if (IDE_SUA_LEN(root_ptr) <= 0)
goto end;
if (strncmp(SUSP_SP, (char *)IDE_sys_use_area(root_ptr),
SUF_SIG_LEN) == 0) {
/*
* We have a match of the sharing signature, so let's
* call the sig_handler to do what is necessary. We can
* ignore the return value, as implemented bits are set.
*/
sig_args.SUF_ptr = IDE_sys_use_area(root_ptr);
sig_args.fsp = fsp;
if ((susp_sp->sig_handler)(&sig_args) == (uchar_t *)NULL) {
goto end;
}
} else {
goto end;
}
/*
* If the "ER" signature in the root directory is past any non SU
* signature, the Rock Ridge signatures will be ignored. This happens
* e.g. for filesystems created by mkisofs. In this case,
* IS_RRIP_IMPLEMENTED(fsp) will return 0 when the "ER" signature is
* parsed. Unfortunately, the results of this run will be cached for
* the root vnode. The solution is to run hs_parsedir() a second time
* for the root directory.
*/
if (hs_parsedir(fsp, root_ptr, hdp, (char *)NULL, (int *)NULL,
HS_SECTOR_SIZE - secoff) == 0) {
(void) hs_parsedir(fsp, root_ptr, hdp, (char *)NULL,
(int *)NULL, HS_SECTOR_SIZE - secoff);
}
/*
* If we did not get at least 1 extension, let's assume ISO and
* NULL out the implementation bits.
*/
if (fsp->hsfs_ext_impl <= 1L)
fsp->hsfs_ext_impl = 0L;
end:
brelse(secbp);
}
/*
* get_cont_area()
*
* This function allocates a memory block, if necessary, and reads the
* continuation area into the allocated space.
*
* Return value : 0 if the read and allocation went OK.
* 1 if there was an error.
*/
static int
get_cont_area(struct hsfs *fsp, uchar_t **buf_pp, cont_info_t *cont_info_p)
{
struct buf *secbp;
int error;
uint_t secno;
/*
* Guard against invalid continuation area records.
* Both cont_offset and cont_len must be no longer than
* HS_SECTOR_SIZE. If they are, return an error.
*/
if (cont_info_p->cont_offset > HS_SECTOR_SIZE ||
cont_info_p->cont_len > HS_SECTOR_SIZE) {
cmn_err(CE_NOTE, "get_cont_area: invalid offset/length");
return (1);
}
if (*buf_pp == (uchar_t *)NULL)
*buf_pp = kmem_alloc((size_t)HS_SECTOR_SIZE, KM_SLEEP);
secno = (uint_t)LBN_TO_SEC(cont_info_p->cont_lbn, fsp->hsfs_vfs);
secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "get_cont_area: bread: error=(%d)", error);
brelse(secbp);
return (1);
}
/*
* This continuation area does not extend into the next sector
* so just copy the data to the buffer.
*/
if ((cont_info_p->cont_offset + cont_info_p->cont_len) <=
HS_SECTOR_SIZE) {
bcopy(secbp->b_un.b_addr, (char *)*buf_pp, HS_SECTOR_SIZE);
}
/*
* This continuation area extends into the next sector so we
* need to do some dancing:
*
* - zero the return buffer so nothing random is returned
* - copy the partial data to the *beginning* of the return buffer
* - release the first sector's buffer
* - read the next sector
* - copy the remainder of the data to the return buffer
*/
else {
uint_t partial_size;
bzero((char *)*buf_pp, HS_SECTOR_SIZE);
partial_size = HS_SECTOR_SIZE - cont_info_p->cont_offset;
bcopy(&secbp->b_un.b_addr[cont_info_p->cont_offset],
(char *)*buf_pp, partial_size);
cont_info_p->cont_offset = 0;
brelse(secbp);
secbp = bread(fsp->hsfs_devvp->v_rdev, (secno + 1) * 4,
HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "get_cont_area: bread(2): error=(%d)",
error);
brelse(secbp);
return (1);
}
bcopy(secbp->b_un.b_addr, (char *)&(*buf_pp)[partial_size],
cont_info_p->cont_len - partial_size);
}
brelse(secbp);
return (0);
}
/*
* free_cont_area
*
* simple function to just free up memory, if it exists
*
*/
static void
free_cont_area(uchar_t *cont_p)
{
if (cont_p)
(void) kmem_free((caddr_t)cont_p, (size_t)HS_SECTOR_SIZE);
cont_p = (uchar_t *)NULL;
}
/*
* 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) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011 Bayard G. Bell. All rights reserved.
* Copyright 2013 Joyent, Inc. All rights reserved.
* Copyright (c) 2017 by Delphix. All rights reserved.
*/
/*
* VFS operations for High Sierra filesystem
*/
#include <sys/types.h>
#include <sys/isa_defs.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/signal.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/buf.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <sys/policy.h>
#include <vm/page.h>
#include <sys/fs/snode.h>
#include <sys/fs/hsfs_spec.h>
#include <sys/fs/hsfs_isospec.h>
#include <sys/fs/hsfs_node.h>
#include <sys/fs/hsfs_impl.h>
#include <sys/fs/hsfs_susp.h>
#include <sys/fs/hsfs_rrip.h>
#include <sys/statvfs.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/swap.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include "fs/fs_subr.h"
#include <sys/cmn_err.h>
#include <sys/bootconf.h>
#include <sys/sdt.h>
/*
* These are needed for the CDROMREADOFFSET Code
*/
#include <sys/cdio.h>
#include <sys/sunddi.h>
#define HSFS_CLKSET
#include <sys/modctl.h>
/*
* Options for mount.
*/
#define HOPT_GLOBAL MNTOPT_GLOBAL
#define HOPT_NOGLOBAL MNTOPT_NOGLOBAL
#define HOPT_MAPLCASE "maplcase"
#define HOPT_NOMAPLCASE "nomaplcase"
#define HOPT_NOTRAILDOT "notraildot"
#define HOPT_TRAILDOT "traildot"
#define HOPT_NRR "nrr"
#define HOPT_RR "rr"
#define HOPT_JOLIET "joliet"
#define HOPT_NOJOLIET "nojoliet"
#define HOPT_JOLIETLONG "jolietlong"
#define HOPT_VERS2 "vers2"
#define HOPT_NOVERS2 "novers2"
#define HOPT_RO MNTOPT_RO
static char *global_cancel[] = { HOPT_NOGLOBAL, NULL };
static char *noglobal_cancel[] = { HOPT_GLOBAL, NULL };
static char *mapl_cancel[] = { HOPT_NOMAPLCASE, NULL };
static char *nomapl_cancel[] = { HOPT_MAPLCASE, NULL };
static char *ro_cancel[] = { MNTOPT_RW, NULL };
static char *rr_cancel[] = { HOPT_NRR, NULL };
static char *nrr_cancel[] = { HOPT_RR, NULL };
static char *joliet_cancel[] = { HOPT_NOJOLIET, NULL };
static char *nojoliet_cancel[] = { HOPT_JOLIET, NULL };
static char *vers2_cancel[] = { HOPT_NOVERS2, NULL };
static char *novers2_cancel[] = { HOPT_VERS2, NULL };
static char *trail_cancel[] = { HOPT_NOTRAILDOT, NULL };
static char *notrail_cancel[] = { HOPT_TRAILDOT, NULL };
static mntopt_t hsfs_options[] = {
{ HOPT_GLOBAL, global_cancel, NULL, 0, NULL },
{ HOPT_NOGLOBAL, noglobal_cancel, NULL, MO_DEFAULT, NULL },
{ HOPT_MAPLCASE, mapl_cancel, NULL, MO_DEFAULT, NULL },
{ HOPT_NOMAPLCASE, nomapl_cancel, NULL, 0, NULL },
{ HOPT_RO, ro_cancel, NULL, MO_DEFAULT, NULL },
{ HOPT_RR, rr_cancel, NULL, MO_DEFAULT, NULL },
{ HOPT_NRR, nrr_cancel, NULL, 0, NULL },
{ HOPT_JOLIET, joliet_cancel, NULL, 0, NULL },
{ HOPT_NOJOLIET, nojoliet_cancel, NULL, 0, NULL },
{ HOPT_JOLIETLONG, NULL, NULL, 0, NULL },
{ HOPT_VERS2, vers2_cancel, NULL, 0, NULL },
{ HOPT_NOVERS2, novers2_cancel, NULL, 0, NULL },
{ HOPT_TRAILDOT, trail_cancel, NULL, MO_DEFAULT, NULL },
{ HOPT_NOTRAILDOT, notrail_cancel, NULL, 0, NULL },
{ "sector", NULL, "0", MO_HASVALUE, NULL},
};
static mntopts_t hsfs_proto_opttbl = {
sizeof (hsfs_options) / sizeof (mntopt_t),
hsfs_options
};
/*
* Indicates whether to enable the I/O scheduling and readahead logic
* 1 - Enable, 0 - Do not Enable.
* Debugging purposes.
*/
int do_schedio = 1;
static int hsfsfstype;
static int hsfsinit(int, char *);
static vfsdef_t vfw = {
VFSDEF_VERSION,
"hsfs",
hsfsinit,
/* We don't suppport remounting */
VSW_HASPROTO|VSW_STATS|VSW_CANLOFI|VSW_MOUNTDEV,
&hsfs_proto_opttbl
};
static struct modlfs modlfs = {
&mod_fsops, "filesystem for HSFS", &vfw
};
static struct modlinkage modlinkage = {
MODREV_1, (void *)&modlfs, NULL
};
extern void hsched_init_caches(void);
extern void hsched_fini_caches(void);
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_fini(void)
{
int error;
error = mod_remove(&modlinkage);
DTRACE_PROBE1(mod_remove, int, error);
if (error)
return (error);
mutex_destroy(&hs_mounttab_lock);
/*
* Tear down the operations vectors
*/
(void) vfs_freevfsops_by_type(hsfsfstype);
vn_freevnodeops(hsfs_vnodeops);
hs_fini_hsnode_cache();
hsched_fini_caches();
return (0);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
#define BDEVFLAG(dev) ((devopsp[getmajor(dev)])->devo_cb_ops->cb_flag)
kmutex_t hs_mounttab_lock;
struct hsfs *hs_mounttab = NULL;
/* default mode, uid, gid */
mode_t hsfs_default_mode = 0555;
uid_t hsfs_default_uid = 0;
gid_t hsfs_default_gid = 3;
extern void hsched_init(struct hsfs *fsp, int fsid,
struct modlinkage *modlinkage);
extern void hsched_fini(struct hsfs_queue *hqueue);
extern void hsfs_init_kstats(struct hsfs *fsp, int fsid);
extern void hsfs_fini_kstats(struct hsfs *fsp);
static int hsfs_mount(struct vfs *vfsp, struct vnode *mvp,
struct mounta *uap, struct cred *cr);
static int hsfs_unmount(struct vfs *vfsp, int, struct cred *cr);
static int hsfs_root(struct vfs *vfsp, struct vnode **vpp);
static int hsfs_statvfs(struct vfs *vfsp, struct statvfs64 *sbp);
static int hsfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp);
static int hsfs_mountroot(struct vfs *, enum whymountroot);
static int hs_mountfs(struct vfs *vfsp, dev_t dev, char *path,
mode_t mode, int flags, struct cred *cr, int isroot);
static int hs_getrootvp(struct vfs *vfsp, struct hsfs *fsp, size_t pathsize);
static int hs_findhsvol(struct hsfs *fsp, struct vnode *vp,
struct hs_volume *hvp);
static int hs_parsehsvol(struct hsfs *fsp, uchar_t *volp,
struct hs_volume *hvp);
static int hs_findisovol(struct hsfs *fsp, struct vnode *vp,
struct hs_volume *hvp,
struct hs_volume *svp,
struct hs_volume *jvp);
static int hs_joliet_level(uchar_t *volp);
static int hs_parseisovol(struct hsfs *fsp, uchar_t *volp,
struct hs_volume *hvp);
static void hs_copylabel(struct hs_volume *, unsigned char *, int);
static int hs_getmdev(struct vfs *, char *fspec, int flags, dev_t *pdev,
mode_t *mode, cred_t *cr);
static int hs_findvoldesc(dev_t rdev, int desc_sec);
static int
hsfsinit(int fstype, char *name)
{
static const fs_operation_def_t hsfs_vfsops_template[] = {
VFSNAME_MOUNT, { .vfs_mount = hsfs_mount },
VFSNAME_UNMOUNT, { .vfs_unmount = hsfs_unmount },
VFSNAME_ROOT, { .vfs_root = hsfs_root },
VFSNAME_STATVFS, { .vfs_statvfs = hsfs_statvfs },
VFSNAME_VGET, { .vfs_vget = hsfs_vget },
VFSNAME_MOUNTROOT, { .vfs_mountroot = hsfs_mountroot },
NULL, NULL
};
int error;
error = vfs_setfsops(fstype, hsfs_vfsops_template, NULL);
if (error != 0) {
cmn_err(CE_WARN, "hsfsinit: bad vfs ops template");
return (error);
}
error = vn_make_ops(name, hsfs_vnodeops_template, &hsfs_vnodeops);
if (error != 0) {
(void) vfs_freevfsops_by_type(fstype);
cmn_err(CE_WARN, "hsfsinit: bad vnode ops template");
return (error);
}
hsfsfstype = fstype;
mutex_init(&hs_mounttab_lock, NULL, MUTEX_DEFAULT, NULL);
hs_init_hsnode_cache();
hsched_init_caches();
return (0);
}
/*ARGSUSED*/
static int
hsfs_mount(struct vfs *vfsp, struct vnode *mvp,
struct mounta *uap, struct cred *cr)
{
int vnode_busy;
dev_t dev;
struct pathname dpn;
int error;
mode_t mode;
int flags; /* this will hold the mount specific data */
if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
return (error);
if (mvp->v_type != VDIR)
return (ENOTDIR);
/* mount option must be read only, else mount will be rejected */
if (!(uap->flags & MS_RDONLY))
return (EROFS);
/*
* We already told the framework that we don't support remounting.
*/
ASSERT(!(uap->flags & MS_REMOUNT));
mutex_enter(&mvp->v_lock);
vnode_busy = (mvp->v_count != 1) || (mvp->v_flag & VROOT);
mutex_exit(&mvp->v_lock);
if ((uap->flags & MS_OVERLAY) == 0 && vnode_busy) {
return (EBUSY);
}
/*
* Check for the options that actually affect things
* at our level.
*/
flags = 0;
if (vfs_optionisset(vfsp, HOPT_NOMAPLCASE, NULL))
flags |= HSFSMNT_NOMAPLCASE;
if (vfs_optionisset(vfsp, HOPT_NOTRAILDOT, NULL))
flags |= HSFSMNT_NOTRAILDOT;
if (vfs_optionisset(vfsp, HOPT_NRR, NULL))
flags |= HSFSMNT_NORRIP;
if (vfs_optionisset(vfsp, HOPT_NOJOLIET, NULL))
flags |= HSFSMNT_NOJOLIET;
if (vfs_optionisset(vfsp, HOPT_JOLIETLONG, NULL))
flags |= HSFSMNT_JOLIETLONG;
if (vfs_optionisset(vfsp, HOPT_NOVERS2, NULL))
flags |= HSFSMNT_NOVERS2;
error = pn_get(uap->dir, (uap->flags & MS_SYSSPACE) ?
UIO_SYSSPACE : UIO_USERSPACE, &dpn);
if (error)
return (error);
error = hs_getmdev(vfsp, uap->spec, uap->flags, &dev, &mode, cr);
if (error != 0) {
pn_free(&dpn);
return (error);
}
/*
* If the device is a tape, return error
*/
if ((BDEVFLAG(dev) & D_TAPE) == D_TAPE) {
pn_free(&dpn);
return (ENOTBLK);
}
/*
* Mount the filesystem.
*/
error = hs_mountfs(vfsp, dev, dpn.pn_path, mode, flags, cr, 0);
pn_free(&dpn);
return (error);
}
/*ARGSUSED*/
static int
hsfs_unmount(
struct vfs *vfsp,
int flag,
struct cred *cr)
{
struct hsfs **tspp;
struct hsfs *fsp;
if (secpolicy_fs_unmount(cr, vfsp) != 0)
return (EPERM);
/*
* forced unmount is not supported by this file system
* and thus, ENOTSUP is being returned.
*/
if (flag & MS_FORCE)
return (ENOTSUP);
fsp = VFS_TO_HSFS(vfsp);
if (fsp->hsfs_rootvp->v_count != 1)
return (EBUSY);
/* destroy all old pages and hsnodes for this vfs */
if (hs_synchash(vfsp))
return (EBUSY);
mutex_enter(&hs_mounttab_lock);
for (tspp = &hs_mounttab; *tspp != NULL; tspp = &(*tspp)->hsfs_next) {
if (*tspp == fsp)
break;
}
if (*tspp == NULL) {
mutex_exit(&hs_mounttab_lock);
panic("hsfs_unmount: vfs not mounted?");
/*NOTREACHED*/
}
*tspp = fsp->hsfs_next;
mutex_exit(&hs_mounttab_lock);
hsfs_fini_kstats(fsp);
(void) VOP_CLOSE(fsp->hsfs_devvp, FREAD, 1, (offset_t)0, cr, NULL);
VN_RELE(fsp->hsfs_devvp);
/* free path table space */
if (fsp->hsfs_ptbl != NULL)
kmem_free(fsp->hsfs_ptbl, (size_t)fsp->hsfs_vol.ptbl_len);
/* free path table index table */
if (fsp->hsfs_ptbl_idx != NULL)
kmem_free(fsp->hsfs_ptbl_idx, (size_t)
(fsp->hsfs_ptbl_idx_size * sizeof (struct ptable_idx)));
/* free "mounted on" pathame */
if (fsp->hsfs_fsmnt != NULL)
kmem_free(fsp->hsfs_fsmnt, strlen(fsp->hsfs_fsmnt) + 1);
hsched_fini(fsp->hqueue);
kmem_free(fsp->hqueue, sizeof (struct hsfs_queue));
mutex_destroy(&fsp->hsfs_free_lock);
rw_destroy(&fsp->hsfs_hash_lock);
kmem_free(fsp, sizeof (*fsp));
return (0);
}
/*ARGSUSED*/
static int
hsfs_root(struct vfs *vfsp, struct vnode **vpp)
{
*vpp = (VFS_TO_HSFS(vfsp))->hsfs_rootvp;
VN_HOLD(*vpp);
return (0);
}
/*ARGSUSED*/
static int
hsfs_statvfs(struct vfs *vfsp, struct statvfs64 *sbp)
{
struct hsfs *fsp;
dev32_t d32;
fsp = VFS_TO_HSFS(vfsp);
if (fsp->hsfs_magic != HSFS_MAGIC)
return (EINVAL);
bzero(sbp, sizeof (*sbp));
sbp->f_bsize = vfsp->vfs_bsize;
sbp->f_frsize = sbp->f_bsize; /* no fragment, same as block size */
sbp->f_blocks = (fsblkcnt64_t)fsp->hsfs_vol.vol_size;
sbp->f_bfree = (fsblkcnt64_t)0;
sbp->f_bavail = (fsblkcnt64_t)0;
sbp->f_files = (fsfilcnt64_t)-1;
sbp->f_ffree = (fsfilcnt64_t)0;
sbp->f_favail = (fsfilcnt64_t)0;
(void) cmpldev(&d32, vfsp->vfs_dev);
sbp->f_fsid = d32;
(void) strcpy(sbp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
sbp->f_namemax = fsp->hsfs_namemax;
(void) strcpy(sbp->f_fstr, fsp->hsfs_vol.vol_id);
return (0);
}
/*
* Previously nodeid was declared as uint32_t. This has been changed
* to conform better with the ISO9660 standard. The standard states that
* a LBN can be a 32 bit number, as the MAKE_NODEID macro shifts this
* LBN 11 places left (LBN_TO_BYTE) and then shifts the result 5 right
* (divide by 32) we are left with the potential of an overflow if
* confined to a 32 bit value.
*/
static int
hsfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
{
struct hsfid *fid;
struct hsfs *fsp;
ino64_t nodeid;
int error;
fsp = (struct hsfs *)VFS_TO_HSFS(vfsp);
fid = (struct hsfid *)fidp;
/*
* Look for vnode on hashlist.
* If found, it's now active and the refcnt was incremented.
*/
rw_enter(&fsp->hsfs_hash_lock, RW_READER);
nodeid = fid->hf_ino;
if ((*vpp = hs_findhash(nodeid, fid->hf_dir_lbn,
(uint_t)fid->hf_dir_off, vfsp)) == NULL) {
/*
* Not in cache, so we need to remake it.
* hs_remakenode() will read the directory entry
* and then check again to see if anyone else has
* put it in the cache.
*/
rw_exit(&fsp->hsfs_hash_lock);
error = hs_remakenode(fid->hf_dir_lbn, (uint_t)fid->hf_dir_off,
vfsp, vpp);
return (error);
}
rw_exit(&fsp->hsfs_hash_lock);
return (0);
}
#define CHECKSUM_SIZE (64 * 1024)
/*
* Compute a CD-ROM fsid by checksumming the first 64K of data on the CD
* We use the 'fsp' argument to determine the location of the root
* directory entry, and we start reading from there.
*/
static int
compute_cdrom_id(struct hsfs *fsp, vnode_t *devvp)
{
uint_t secno;
struct hs_volume *hsvp = &fsp->hsfs_vol;
struct buf *bp;
int error;
int fsid;
secno = hsvp->root_dir.ext_lbn >> hsvp->lbn_secshift;
bp = bread(devvp->v_rdev, secno * 4, CHECKSUM_SIZE);
error = geterror(bp);
/*
* An error on read or a partial read means we asked
* for a nonexistant/corrupted piece of the device
* (including past-the-end of the media). Don't
* try to use the checksumming method then.
*/
if (!error && bp->b_bcount == CHECKSUM_SIZE) {
int *ibuf = (int *)bp->b_un.b_addr;
int i;
fsid = 0;
for (i = 0; i < CHECKSUM_SIZE / sizeof (int); i++)
fsid ^= ibuf[ i ];
} else {
/*
* Fallback - use creation date
*/
fsid = hsvp->cre_date.tv_sec;
}
brelse(bp);
return (fsid);
}
/*ARGSUSED*/
static int
hs_mountfs(
struct vfs *vfsp,
dev_t dev,
char *path,
mode_t mode,
int mount_flags,
struct cred *cr,
int isroot)
{
struct vnode *devvp;
struct hsfs *tsp;
struct hsfs *fsp = NULL;
struct vattr vap;
struct hsnode *hp;
int error;
struct timeval tv;
int fsid;
int use_rrip;
int use_vers2;
int use_joliet;
int has_rrip = 0;
int has_vers2 = 0;
int has_joliet = 0;
int force_rrip_off;
int force_vers2_off;
int force_joliet_off;
size_t pathbufsz = strlen(path) + 1;
int redo_rootvp;
struct hs_volume *svp = NULL; /* Supplemental VD for ISO-9660:1999 */
struct hs_volume *jvp = NULL; /* Joliet VD */
/*
* The rules for which extension will be used are:
* 1. No specific mount options given:
* - use rrip if available
* - use ISO9660:1999 if available
* - use joliet if available.
* 2. rrip/ISO9660:1999/joliet explicitly disabled via mount option:
* - use next "lower" extension
* 3. joliet/ISO9660:1999/rrip explicitly requested via mount option:
* - disable rrip support even if available
* - disable IOS9660:1999 support even if available
*
* We need to adjust these flags as we discover the extensions
* present. See below. These are just the starting values.
*/
use_rrip = (mount_flags & HSFSMNT_NORRIP) == 0;
use_vers2 = (mount_flags & HSFSMNT_NOVERS2) == 0;
use_joliet = (mount_flags & HSFSMNT_NOJOLIET) == 0;
/*
* Open the device
*/
devvp = makespecvp(dev, VBLK);
ASSERT(devvp != 0);
/*
* Open the target device (file) for read only.
*/
if (error = VOP_OPEN(&devvp, FREAD, cr, NULL)) {
VN_RELE(devvp);
return (error);
}
/*
* Refuse to go any further if this
* device is being used for swapping
*/
if (IS_SWAPVP(common_specvp(devvp))) {
error = EBUSY;
goto cleanup;
}
vap.va_mask = AT_SIZE;
if ((error = VOP_GETATTR(devvp, &vap, ATTR_COMM, cr, NULL)) != 0) {
cmn_err(CE_NOTE, "Cannot get attributes of the CD-ROM driver");
goto cleanup;
}
/*
* Make sure we have a nonzero size partition.
* The current version of the SD driver will *not* fail the open
* of such a partition so we have to check for it here.
*/
if (vap.va_size == 0) {
error = ENXIO;
goto cleanup;
}
/*
* Init a new hsfs structure.
*/
fsp = kmem_zalloc(sizeof (*fsp), KM_SLEEP);
svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
jvp = kmem_zalloc(sizeof (*jvp), KM_SLEEP);
/* hardwire perms, uid, gid */
fsp->hsfs_vol.vol_uid = hsfs_default_uid;
fsp->hsfs_vol.vol_gid = hsfs_default_gid;
fsp->hsfs_vol.vol_prot = hsfs_default_mode;
svp->vol_uid = hsfs_default_uid;
svp->vol_gid = hsfs_default_gid;
svp->vol_prot = hsfs_default_mode;
jvp->vol_uid = hsfs_default_uid;
jvp->vol_gid = hsfs_default_gid;
jvp->vol_prot = hsfs_default_mode;
/*
* Look for a Standard File Structure Volume Descriptor,
* of which there must be at least one.
* If found, check for volume size consistency.
*
* If svp->lbn_size is != 0, we did find a ISO-9660:1999 SVD
* If jvp->lbn_size is != 0, we did find a Joliet SVD.
*/
fsp->hsfs_namemax = ISO_FILE_NAMELEN;
fsp->hsfs_namelen = ISO_FILE_NAMELEN;
error = hs_findisovol(fsp, devvp, &fsp->hsfs_vol, svp, jvp);
if (error == EINVAL) /* no iso 9660 - try high sierra ... */
error = hs_findhsvol(fsp, devvp, &fsp->hsfs_vol);
if (error)
goto cleanup;
DTRACE_PROBE4(findvol,
struct hsfs *, fsp,
struct hs_volume *, &fsp->hsfs_vol,
struct hs_volume *, svp,
struct hs_volume *, jvp);
/*
* Generate a file system ID from the CD-ROM,
* and check it for uniqueness.
*
* What we are aiming for is some chance of integrity
* across disk change. That is, if a client has an fhandle,
* it will be valid as long as the same disk is mounted.
*/
fsid = compute_cdrom_id(fsp, devvp);
mutex_enter(&hs_mounttab_lock);
if (fsid == 0 || fsid == -1) {
uniqtime(&tv);
fsid = tv.tv_sec;
} else /* make sure that the fsid is unique */
for (tsp = hs_mounttab; tsp != NULL; tsp = tsp->hsfs_next) {
if (fsid == tsp->hsfs_vfs->vfs_fsid.val[0]) {
uniqtime(&tv);
fsid = tv.tv_sec;
break;
}
}
fsp->hsfs_next = hs_mounttab;
hs_mounttab = fsp;
fsp->hsfs_devvp = devvp;
fsp->hsfs_vfs = vfsp;
fsp->hsfs_fsmnt = kmem_alloc(pathbufsz, KM_SLEEP);
(void) strlcpy(fsp->hsfs_fsmnt, path, pathbufsz);
mutex_init(&fsp->hsfs_free_lock, NULL, MUTEX_DEFAULT, NULL);
rw_init(&fsp->hsfs_hash_lock, NULL, RW_DEFAULT, NULL);
vfsp->vfs_data = (caddr_t)fsp;
vfsp->vfs_dev = dev;
vfsp->vfs_fstype = hsfsfstype;
vfsp->vfs_bsize = fsp->hsfs_vol.lbn_size; /* %% */
vfsp->vfs_fsid.val[0] = fsid;
vfsp->vfs_fsid.val[1] = hsfsfstype;
if (!hs_getrootvp(vfsp, fsp, pathbufsz)) {
DTRACE_PROBE1(rootvp__failed, struct hsfs *, fsp);
error = EINVAL;
goto cleanup;
}
DTRACE_PROBE1(rootvp, struct hsfs *, fsp);
/*
* Attempt to discover a RR extension.
*/
if (use_rrip) {
hp = VTOH(fsp->hsfs_rootvp);
hs_check_root_dirent(fsp->hsfs_rootvp, &(hp->hs_dirent));
}
has_rrip = IS_RRIP_IMPLEMENTED(fsp);
has_vers2 = (svp->lbn_size != 0);
has_joliet = (jvp->lbn_size != 0);
DTRACE_PROBE4(voltype__suggested, struct hsfs *, fsp,
int, use_rrip, int, use_vers2, int, use_joliet);
DTRACE_PROBE4(voltype__actual, struct hsfs *, fsp,
int, has_rrip, int, has_vers2, int, has_joliet);
DTRACE_PROBE4(findvol,
struct hsfs *, fsp,
struct hs_volume *, &fsp->hsfs_vol,
struct hs_volume *, svp,
struct hs_volume *, jvp);
force_rrip_off = !use_rrip ||
(vfs_optionisset(vfsp, HOPT_JOLIET, NULL) && has_joliet) ||
(vfs_optionisset(vfsp, HOPT_VERS2, NULL) && has_vers2);
force_vers2_off = !use_vers2 ||
(vfs_optionisset(vfsp, HOPT_JOLIET, NULL) && has_joliet);
force_joliet_off = !use_joliet;
DTRACE_PROBE4(voltype__force_off, struct hsfs *, fsp,
int, force_rrip_off, int, force_vers2_off, int, force_joliet_off);
/*
* At the moment, we have references of all three possible
* extensions (RR, ISO9660:1999/v2 and Joliet) if present.
*
* The "active" volume descriptor is RRIP (or ISO9660:1988).
* We now switch to the user-requested one.
*/
redo_rootvp = 0;
if (force_rrip_off || !has_rrip) {
if (has_vers2 && !force_vers2_off) {
VN_RELE(fsp->hsfs_rootvp);
bcopy(svp, &fsp->hsfs_vol, sizeof (struct hs_volume));
fsp->hsfs_vol_type = HS_VOL_TYPE_ISO_V2;
vfsp->vfs_bsize = fsp->hsfs_vol.lbn_size;
redo_rootvp = 1;
has_joliet = 0;
} else if (has_joliet && !force_joliet_off) {
VN_RELE(fsp->hsfs_rootvp);
bcopy(jvp, &fsp->hsfs_vol, sizeof (struct hs_volume));
fsp->hsfs_vol_type = HS_VOL_TYPE_JOLIET;
vfsp->vfs_bsize = fsp->hsfs_vol.lbn_size;
redo_rootvp = 1;
has_vers2 = 0;
}
}
if (redo_rootvp) {
/*
* Make sure not to use Rock Ridge.
*/
UNSET_IMPL_BIT(fsp, RRIP_BIT);
UNSET_SUSP_BIT(fsp);
has_rrip = 0;
if (!hs_getrootvp(vfsp, fsp, pathbufsz)) {
DTRACE_PROBE1(rootvp__failed, struct hsfs *, fsp);
error = EINVAL;
goto cleanup;
}
DTRACE_PROBE1(rootvp, struct hsfs *, fsp);
}
if (IS_RRIP_IMPLEMENTED(fsp)) {
has_vers2 = 0;
has_joliet = 0;
}
if (force_vers2_off)
has_vers2 = 0;
if (force_joliet_off)
has_joliet = 0;
DTRACE_PROBE4(voltype__taken, struct hsfs *, fsp,
int, has_rrip, int, has_vers2, int, has_joliet);
/*
* mark root node as VROOT
*/
fsp->hsfs_rootvp->v_flag |= VROOT;
/* Here we take care of some special case stuff for mountroot */
if (isroot) {
fsp->hsfs_rootvp->v_rdev = devvp->v_rdev;
rootvp = fsp->hsfs_rootvp;
}
if (IS_RRIP_IMPLEMENTED(fsp)) {
/*
* if RRIP, don't copy NOMAPLCASE or NOTRAILDOT to hsfs_flags
*/
mount_flags &= ~(HSFSMNT_NOMAPLCASE | HSFSMNT_NOTRAILDOT);
fsp->hsfs_namemax = RRIP_FILE_NAMELEN;
fsp->hsfs_namelen = RRIP_FILE_NAMELEN;
ASSERT(vfs_optionisset(vfsp, HOPT_RR, NULL));
vfs_clearmntopt(vfsp, HOPT_VERS2);
vfs_clearmntopt(vfsp, HOPT_JOLIET);
} else switch (fsp->hsfs_vol_type) {
case HS_VOL_TYPE_HS:
case HS_VOL_TYPE_ISO:
default:
/*
* if iso v1, don't allow trailing spaces in iso file names
*/
mount_flags |= HSFSMNT_NOTRAILSPACE;
fsp->hsfs_namemax = ISO_NAMELEN_V2_MAX;
fsp->hsfs_namelen = ISO_FILE_NAMELEN;
vfs_clearmntopt(vfsp, HOPT_RR);
vfs_clearmntopt(vfsp, HOPT_VERS2);
vfs_clearmntopt(vfsp, HOPT_JOLIET);
break;
case HS_VOL_TYPE_ISO_V2:
/*
* if iso v2, don't copy NOTRAILDOT to hsfs_flags
*/
mount_flags &= ~HSFSMNT_NOTRAILDOT;
mount_flags |= HSFSMNT_NOMAPLCASE | HSFSMNT_NOVERSION;
fsp->hsfs_namemax = ISO_NAMELEN_V2_MAX;
fsp->hsfs_namelen = ISO_NAMELEN_V2;
vfs_setmntopt(vfsp, HOPT_VERS2, NULL, 0);
vfs_clearmntopt(vfsp, HOPT_RR);
vfs_clearmntopt(vfsp, HOPT_JOLIET);
break;
case HS_VOL_TYPE_JOLIET:
/*
* if Joliet, don't copy NOMAPLCASE or NOTRAILDOT to hsfs_flags
*/
mount_flags &= ~(HSFSMNT_NOMAPLCASE | HSFSMNT_NOTRAILDOT);
mount_flags |= HSFSMNT_NOMAPLCASE;
if (mount_flags & HSFSMNT_JOLIETLONG)
fsp->hsfs_namemax = JOLIET_NAMELEN_MAX*3; /* UTF-8 */
else
fsp->hsfs_namemax = MAXNAMELEN-1;
fsp->hsfs_namelen = JOLIET_NAMELEN*2;
vfs_setmntopt(vfsp, HOPT_JOLIET, NULL, 0);
vfs_clearmntopt(vfsp, HOPT_RR);
vfs_clearmntopt(vfsp, HOPT_VERS2);
break;
}
/*
* Add the HSFSMNT_INODE pseudo mount flag to the current mount flags.
*/
fsp->hsfs_flags = mount_flags | (fsp->hsfs_flags & HSFSMNT_INODE);
/*
* Setup I/O Scheduling structures
*/
if (do_schedio) {
fsp->hqueue = kmem_alloc(sizeof (struct hsfs_queue), KM_SLEEP);
hsched_init(fsp, fsid, &modlinkage);
}
/*
* Setup kstats
*/
hsfs_init_kstats(fsp, fsid);
DTRACE_PROBE1(mount__done, struct hsfs *, fsp);
/*
* set the magic word
*/
fsp->hsfs_magic = HSFS_MAGIC;
mutex_exit(&hs_mounttab_lock);
kmem_free(svp, sizeof (*svp));
kmem_free(jvp, sizeof (*jvp));
return (0);
cleanup:
(void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, cr, NULL);
VN_RELE(devvp);
if (fsp)
kmem_free(fsp, sizeof (*fsp));
if (svp)
kmem_free(svp, sizeof (*svp));
if (jvp)
kmem_free(jvp, sizeof (*jvp));
return (error);
}
/*
* Get the rootvp associated with fsp->hsfs_vol
*/
static int
hs_getrootvp(
struct vfs *vfsp,
struct hsfs *fsp,
size_t pathsize)
{
struct hsnode *hp;
ASSERT(pathsize == strlen(fsp->hsfs_fsmnt) + 1);
/*
* If the root directory does not appear to be
* valid, use what it points to as "." instead.
* Some Defense Mapping Agency disks are non-conformant
* in this way.
*/
if (!hsfs_valid_dir(&fsp->hsfs_vol.root_dir)) {
hs_log_bogus_disk_warning(fsp, HSFS_ERR_BAD_ROOT_DIR, 0);
if (hs_remakenode(fsp->hsfs_vol.root_dir.ext_lbn,
(uint_t)0, vfsp, &fsp->hsfs_rootvp)) {
hs_mounttab = hs_mounttab->hsfs_next;
mutex_destroy(&fsp->hsfs_free_lock);
rw_destroy(&fsp->hsfs_hash_lock);
kmem_free(fsp->hsfs_fsmnt, pathsize);
mutex_exit(&hs_mounttab_lock);
return (0);
}
} else {
fsp->hsfs_rootvp = hs_makenode(&fsp->hsfs_vol.root_dir,
fsp->hsfs_vol.root_dir.ext_lbn, 0, vfsp);
}
/* XXX - ignore the path table for now */
fsp->hsfs_ptbl = NULL;
hp = VTOH(fsp->hsfs_rootvp);
hp->hs_ptbl_idx = NULL;
return (1);
}
/*
* hs_findhsvol()
*
* Locate the Standard File Structure Volume Descriptor and
* parse it into an hs_volume structure.
*
* XXX - May someday want to look for Coded Character Set FSVD, too.
*/
static int
hs_findhsvol(struct hsfs *fsp, struct vnode *vp, struct hs_volume *hvp)
{
struct buf *secbp;
int i;
int n;
uchar_t *volp;
int error;
uint_t secno;
secno = hs_findvoldesc(vp->v_rdev, HS_VOLDESC_SEC);
secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "hs_findhsvol: bread: error=(%d)", error);
brelse(secbp);
return (error);
}
volp = (uchar_t *)secbp->b_un.b_addr;
/*
* To avoid that we read the whole medium in case that someone prepares
* a malicious "fs image", we read at most 32 blocks.
*/
for (n = 0; n < 32 &&
HSV_DESC_TYPE(volp) != VD_EOV; n++) {
for (i = 0; i < HSV_ID_STRLEN; i++)
if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i])
goto cantfind;
if (HSV_STD_VER(volp) != HSV_ID_VER)
goto cantfind;
switch (HSV_DESC_TYPE(volp)) {
case VD_SFS:
/* Standard File Structure */
fsp->hsfs_vol_type = HS_VOL_TYPE_HS;
error = hs_parsehsvol(fsp, volp, hvp);
brelse(secbp);
return (error);
case VD_CCFS:
/* Coded Character File Structure */
case VD_BOOT:
case VD_UNSPEC:
case VD_EOV:
break;
}
brelse(secbp);
++secno;
secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "hs_findhsvol: bread: error=(%d)",
error);
brelse(secbp);
return (error);
}
volp = (uchar_t *)secbp->b_un.b_addr;
}
cantfind:
brelse(secbp);
return (EINVAL);
}
/*
* hs_parsehsvol
*
* Parse the Standard File Structure Volume Descriptor into
* an hs_volume structure. We can't just bcopy it into the
* structure because of byte-ordering problems.
*
*/
static int
hs_parsehsvol(struct hsfs *fsp, uchar_t *volp, struct hs_volume *hvp)
{
hvp->vol_size = HSV_VOL_SIZE(volp);
hvp->lbn_size = HSV_BLK_SIZE(volp);
if (hvp->lbn_size == 0) {
cmn_err(CE_NOTE, "hs_parsehsvol: logical block size in the "
"SFSVD is zero");
return (EINVAL);
}
hvp->lbn_shift = ffs((long)hvp->lbn_size) - 1;
hvp->lbn_secshift =
ffs((long)howmany(HS_SECTOR_SIZE, (int)hvp->lbn_size)) - 1;
hvp->lbn_maxoffset = hvp->lbn_size - 1;
hs_parse_longdate(HSV_cre_date(volp), &hvp->cre_date);
hs_parse_longdate(HSV_mod_date(volp), &hvp->mod_date);
hvp->file_struct_ver = HSV_FILE_STRUCT_VER(volp);
hvp->ptbl_len = HSV_PTBL_SIZE(volp);
hvp->vol_set_size = (ushort_t)HSV_SET_SIZE(volp);
hvp->vol_set_seq = (ushort_t)HSV_SET_SEQ(volp);
#if defined(_LITTLE_ENDIAN)
hvp->ptbl_lbn = HSV_PTBL_MAN_LS(volp);
#else
hvp->ptbl_lbn = HSV_PTBL_MAN_MS(volp);
#endif
hs_copylabel(hvp, HSV_VOL_ID(volp), 0);
/*
* Make sure that lbn_size is a power of two and otherwise valid.
*/
if (hvp->lbn_size & ~(1 << hvp->lbn_shift)) {
cmn_err(CE_NOTE,
"hsfs: %d-byte logical block size not supported",
hvp->lbn_size);
return (EINVAL);
}
return (hs_parsedir(fsp, HSV_ROOT_DIR(volp), &hvp->root_dir,
(char *)NULL, (int *)NULL, HDE_ROOT_DIR_REC_SIZE));
}
/*
* hs_findisovol()
*
* Locate the Primary Volume Descriptor
* parse it into an hs_volume structure.
*
* XXX - Partition not yet done
*
* Except for fsp->hsfs_vol_type, no fsp member may be modified.
* fsp->hsfs_vol is modified indirectly via the *hvp argument.
*/
static int
hs_findisovol(struct hsfs *fsp, struct vnode *vp,
struct hs_volume *hvp,
struct hs_volume *svp,
struct hs_volume *jvp)
{
struct buf *secbp;
int i;
int n;
uchar_t *volp;
int error;
uint_t secno;
int foundpvd = 0;
int foundsvd = 0;
int foundjvd = 0;
int pvd_sum = 0;
secno = hs_findvoldesc(vp->v_rdev, ISO_VOLDESC_SEC);
secbp = bread(vp->v_rdev, secno * 4, ISO_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)", error);
brelse(secbp);
return (error);
}
volp = (uchar_t *)secbp->b_un.b_addr;
/*
* To avoid that we read the whole medium in case that someone prepares
* a malicious "fs image", we read at most 32 blocks.
*/
for (n = 0; n < 32 && ISO_DESC_TYPE(volp) != ISO_VD_EOV; n++) {
for (i = 0; i < ISO_ID_STRLEN; i++)
if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
goto cantfind;
switch (ISO_DESC_TYPE(volp)) {
case ISO_VD_PVD:
/* Standard File Structure */
if (ISO_STD_VER(volp) != ISO_ID_VER)
goto cantfind;
if (foundpvd != 1) {
fsp->hsfs_vol_type = HS_VOL_TYPE_ISO;
if (error = hs_parseisovol(fsp, volp, hvp)) {
brelse(secbp);
return (error);
}
foundpvd = 1;
for (i = 0; i < ISO_SECTOR_SIZE; i++)
pvd_sum += volp[i];
}
break;
case ISO_VD_SVD:
/* Supplementary Volume Descriptor */
if (ISO_STD_VER(volp) == ISO_ID_VER2 &&
foundsvd != 1) {
fsp->hsfs_vol_type = HS_VOL_TYPE_ISO;
if (error = hs_parseisovol(fsp, volp, svp)) {
brelse(secbp);
return (error);
}
foundsvd = 1;
}
if (hs_joliet_level(volp) >= 1 && foundjvd != 1) {
fsp->hsfs_vol_type = HS_VOL_TYPE_ISO;
if (error = hs_parseisovol(fsp, volp, jvp)) {
brelse(secbp);
return (error);
}
foundjvd = 1;
}
break;
case ISO_VD_BOOT:
break;
case ISO_VD_VPD:
/* currently cannot handle partition */
break;
case VD_EOV:
break;
}
brelse(secbp);
++secno;
secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)",
error);
brelse(secbp);
return (error);
}
volp = (uchar_t *)secbp->b_un.b_addr;
}
for (n = 0; n < 16; n++) {
brelse(secbp);
++secno;
secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
error = geterror(secbp);
if (error != 0) {
cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)",
error);
brelse(secbp);
return (error);
}
/*
* Check for the signature from mkisofs that grants that
* the current filesystem allows to use the extent lbn as
* inode number even in pure ISO9660 mode.
*/
volp = (uchar_t *)secbp->b_un.b_addr;
if (strncmp((char *)volp, "MKI ", 4) == 0) {
int sum;
sum = volp[2045];
sum *= 256;
sum += volp[2046];
sum *= 256;
sum += volp[2047];
if (sum == pvd_sum)
fsp->hsfs_flags |= HSFSMNT_INODE;
break;
}
}
if (foundpvd) {
brelse(secbp);
return (0);
}
cantfind:
brelse(secbp);
return (EINVAL);
}
/*
* Return 0 if no Joliet is found
* else return Joliet Level 1..3
*/
static int
hs_joliet_level(uchar_t *volp)
{
if (ISO_std_ver(volp)[0] == ISO_ID_VER &&
ISO_svd_esc(volp)[0] == '%' &&
ISO_svd_esc(volp)[1] == '/') {
switch (ISO_svd_esc(volp)[2]) {
case '@':
return (1);
case 'C':
return (2);
case 'E':
return (3);
}
}
return (0);
}
/*
* hs_parseisovol
*
* Parse the Primary Volume Descriptor into an hs_volume structure.
*
*/
static int
hs_parseisovol(struct hsfs *fsp, uchar_t *volp, struct hs_volume *hvp)
{
hvp->vol_size = ISO_VOL_SIZE(volp);
hvp->lbn_size = ISO_BLK_SIZE(volp);
if (hvp->lbn_size == 0) {
cmn_err(CE_NOTE, "hs_parseisovol: logical block size in the "
"PVD is zero");
return (EINVAL);
}
hvp->lbn_shift = ffs((long)hvp->lbn_size) - 1;
hvp->lbn_secshift =
ffs((long)howmany(ISO_SECTOR_SIZE, (int)hvp->lbn_size)) - 1;
hvp->lbn_maxoffset = hvp->lbn_size - 1;
hs_parse_longdate(ISO_cre_date(volp), &hvp->cre_date);
hs_parse_longdate(ISO_mod_date(volp), &hvp->mod_date);
hvp->file_struct_ver = ISO_FILE_STRUCT_VER(volp);
hvp->ptbl_len = ISO_PTBL_SIZE(volp);
hvp->vol_set_size = (ushort_t)ISO_SET_SIZE(volp);
hvp->vol_set_seq = (ushort_t)ISO_SET_SEQ(volp);
#if defined(_LITTLE_ENDIAN)
hvp->ptbl_lbn = ISO_PTBL_MAN_LS(volp);
#else
hvp->ptbl_lbn = ISO_PTBL_MAN_MS(volp);
#endif
hs_copylabel(hvp, ISO_VOL_ID(volp), hs_joliet_level(volp) >= 1);
/*
* Make sure that lbn_size is a power of two and otherwise valid.
*/
if (hvp->lbn_size & ~(1 << hvp->lbn_shift)) {
cmn_err(CE_NOTE,
"hsfs: %d-byte logical block size not supported",
hvp->lbn_size);
return (EINVAL);
}
return (hs_parsedir(fsp, ISO_ROOT_DIR(volp), &hvp->root_dir,
(char *)NULL, (int *)NULL, IDE_ROOT_DIR_REC_SIZE));
}
/*
* Common code for mount and umount.
* Check that the user's argument is a reasonable
* thing on which to mount, and return the device number if so.
*/
static int
hs_getmdev(struct vfs *vfsp, char *fspec, int flags, dev_t *pdev, mode_t *mode,
cred_t *cr)
{
int error;
struct vnode *svp = NULL;
struct vnode *lvp = NULL;
struct vnode *bvp;
struct vattr vap;
dev_t dev;
enum uio_seg fromspace = (flags & MS_SYSSPACE) ?
UIO_SYSSPACE : UIO_USERSPACE;
/*
* Look up the device/file to be mounted.
*/
error = lookupname(fspec, fromspace, FOLLOW, NULLVPP, &svp);
if (error) {
if (error == ENOENT)
error = ENODEV;
goto out;
}
error = vfs_get_lofi(vfsp, &lvp);
if (error > 0) {
if (error == ENOENT)
error = ENODEV;
goto out;
} else if (error == 0) {
bvp = lvp;
} else {
bvp = svp;
if (bvp->v_type != VBLK) {
error = ENOTBLK;
goto out;
}
if ((error = secpolicy_spec_open(cr, bvp, FREAD)) != 0)
goto out;
}
/*
* Can we read from the device/file ?
*/
if ((error = VOP_ACCESS(svp, VREAD, 0, cr, NULL)) != 0)
goto out;
vap.va_mask = AT_MODE; /* get protection mode */
(void) VOP_GETATTR(bvp, &vap, 0, CRED(), NULL);
*mode = vap.va_mode;
dev = *pdev = bvp->v_rdev;
error = EBUSY;
/*
* Ensure that this device isn't already mounted,
* unless this is a REMOUNT request or we are told to suppress
* mount checks.
*/
if ((flags & MS_NOCHECK) == 0) {
if (vfs_devmounting(dev, vfsp))
goto out;
if (vfs_devismounted(dev) && !(flags & MS_REMOUNT))
goto out;
}
if (getmajor(*pdev) >= devcnt) {
error = ENXIO;
goto out;
}
error = 0;
out:
if (svp != NULL)
VN_RELE(svp);
if (lvp != NULL)
VN_RELE(lvp);
return (error);
}
static void
hs_copylabel(struct hs_volume *hvp, unsigned char *label, int isjoliet)
{
char lbuf[64]; /* hs_joliet_cp() creates 48 bytes at most */
if (isjoliet) {
/*
* hs_joliet_cp() will output 16..48 bytes.
* We need to clear 'lbuf' to avoid junk chars past byte 15.
*/
bzero(lbuf, sizeof (lbuf));
(void) hs_joliet_cp((char *)label, lbuf, 32);
label = (unsigned char *)lbuf;
}
/* cdrom volid is at most 32 bytes */
bcopy(label, hvp->vol_id, 32);
hvp->vol_id[31] = '\0';
}
/*
* Mount root file system.
* "why" is ROOT_INIT on initial call, ROOT_REMOUNT if called to
* remount the root file system, and ROOT_UNMOUNT if called to
* unmount the root (e.g., as part of a system shutdown).
*
* XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
* operation, goes along with auto-configuration. A mechanism should be
* provided by which machine-INdependent code in the kernel can say "get me the
* right root file system" and "get me the right initial swap area", and have
* that done in what may well be a machine-dependent fashion.
* Unfortunately, it is also file-system-type dependent (NFS gets it via
* bootparams calls, UFS gets it from various and sundry machine-dependent
* mechanisms, as SPECFS does for swap).
*/
static int
hsfs_mountroot(struct vfs *vfsp, enum whymountroot why)
{
int error;
struct hsfs *fsp;
struct hs_volume *fvolp;
static int hsfsrootdone = 0;
dev_t rootdev;
mode_t mode = 0;
if (why == ROOT_INIT) {
if (hsfsrootdone++)
return (EBUSY);
rootdev = getrootdev();
if (rootdev == (dev_t)NODEV)
return (ENODEV);
vfsp->vfs_dev = rootdev;
vfsp->vfs_flag |= VFS_RDONLY;
} else if (why == ROOT_REMOUNT) {
cmn_err(CE_NOTE, "hsfs_mountroot: ROOT_REMOUNT");
return (0);
} else if (why == ROOT_UNMOUNT) {
return (0);
}
error = vfs_lock(vfsp);
if (error) {
cmn_err(CE_NOTE, "hsfs_mountroot: couldn't get vfs_lock");
return (error);
}
error = hs_mountfs(vfsp, rootdev, "/", mode, 1, CRED(), 1);
/*
* XXX - assumes root device is not indirect, because we don't set
* rootvp. Is rootvp used for anything? If so, make another arg
* to mountfs.
*/
if (error) {
vfs_unlock(vfsp);
if (rootvp) {
VN_RELE(rootvp);
rootvp = (struct vnode *)0;
}
return (error);
}
if (why == ROOT_INIT)
vfs_add((struct vnode *)0, vfsp,
(vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
vfs_unlock(vfsp);
fsp = VFS_TO_HSFS(vfsp);
fvolp = &fsp->hsfs_vol;
#ifdef HSFS_CLKSET
if (fvolp->cre_date.tv_sec == 0) {
cmn_err(CE_NOTE, "hsfs_mountroot: cre_date.tv_sec == 0");
if (fvolp->mod_date.tv_sec == 0) {
cmn_err(CE_NOTE,
"hsfs_mountroot: mod_date.tv_sec == 0");
cmn_err(CE_NOTE, "hsfs_mountroot: clkset(-1L)");
clkset(-1L);
} else {
clkset(fvolp->mod_date.tv_sec);
}
} else {
clkset(fvolp->mod_date.tv_sec);
}
#else /* HSFS_CLKSET */
clkset(-1L);
#endif /* HSFS_CLKSET */
return (0);
}
/*
* hs_findvoldesc()
*
* Return the sector where the volume descriptor lives. This is
* a fixed value for "normal" cd-rom's, but can change for
* multisession cd's.
*
* desc_sec is the same for high-sierra and iso 9660 formats, why
* there are two different #defines used in the code for this is
* beyond me. These are standards, cast in concrete, right?
* To be general, however, this function supports passing in different
* values.
*/
static int
hs_findvoldesc(dev_t rdev, int desc_sec)
{
int secno;
int error;
int rval; /* ignored */
#ifdef CDROMREADOFFSET
/*
* Issue the Read Offset ioctl directly to the
* device. Ignore any errors and set starting
* secno to the default, otherwise add the
* VOLDESC sector number to the offset.
*/
error = cdev_ioctl(rdev, CDROMREADOFFSET, (intptr_t)&secno,
FNATIVE|FKIOCTL|FREAD, CRED(), &rval);
if (error) {
secno = desc_sec;
} else {
secno += desc_sec;
}
#else
secno = desc_sec;
#endif
return (secno);
}
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2017 by Delphix. All rights reserved.
*/
/*
* Vnode operations for the High Sierra filesystem
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/resource.h>
#include <sys/signal.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/buf.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/mode.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/flock.h>
#include <sys/kmem.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <sys/errno.h>
#include <sys/mman.h>
#include <sys/pathname.h>
#include <sys/debug.h>
#include <sys/vmsystm.h>
#include <sys/cmn_err.h>
#include <sys/fbuf.h>
#include <sys/dirent.h>
#include <sys/errno.h>
#include <sys/dkio.h>
#include <sys/cmn_err.h>
#include <sys/atomic.h>
#include <vm/hat.h>
#include <vm/page.h>
#include <vm/pvn.h>
#include <vm/as.h>
#include <vm/seg.h>
#include <vm/seg_map.h>
#include <vm/seg_kmem.h>
#include <vm/seg_vn.h>
#include <vm/rm.h>
#include <vm/page.h>
#include <sys/swap.h>
#include <sys/avl.h>
#include <sys/sunldi.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sdt.h>
/*
* For struct modlinkage
*/
#include <sys/modctl.h>
#include <sys/fs/hsfs_spec.h>
#include <sys/fs/hsfs_node.h>
#include <sys/fs/hsfs_impl.h>
#include <sys/fs/hsfs_susp.h>
#include <sys/fs/hsfs_rrip.h>
#include <fs/fs_subr.h>
/* # of contiguous requests to detect sequential access pattern */
static int seq_contig_requests = 2;
/*
* This is the max number os taskq threads that will be created
* if required. Since we are using a Dynamic TaskQ by default only
* one thread is created initially.
*
* NOTE: In the usual hsfs use case this per fs instance number
* of taskq threads should not place any undue load on a system.
* Even on an unusual system with say 100 CDROM drives, 800 threads
* will not be created unless all the drives are loaded and all
* of them are saturated with I/O at the same time! If there is at
* all a complaint of system load due to such an unusual case it
* should be easy enough to change to one per-machine Dynamic TaskQ
* for all hsfs mounts with a nthreads of say 32.
*/
static int hsfs_taskq_nthreads = 8; /* # of taskq threads per fs */
/* Min count of adjacent bufs that will avoid buf coalescing */
static int hsched_coalesce_min = 2;
/*
* Kmem caches for heavily used small allocations. Using these kmem
* caches provides a factor of 3 reduction in system time and greatly
* aids overall throughput esp. on SPARC.
*/
struct kmem_cache *hio_cache;
struct kmem_cache *hio_info_cache;
/*
* This tunable allows us to ignore inode numbers from rrip-1.12.
* In this case, we fall back to our default inode algorithm.
*/
extern int use_rrip_inodes;
static int hsched_deadline_compare(const void *x1, const void *x2);
static int hsched_offset_compare(const void *x1, const void *x2);
static void hsched_enqueue_io(struct hsfs *fsp, struct hio *hsio, int ra);
int hsched_invoke_strategy(struct hsfs *fsp);
/* ARGSUSED */
static int
hsfs_fsync(vnode_t *cp, int syncflag, cred_t *cred, caller_context_t *ct)
{
return (0);
}
/*ARGSUSED*/
static int
hsfs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
struct caller_context *ct)
{
caddr_t base;
offset_t diff;
int error;
struct hsnode *hp;
uint_t filesize;
hp = VTOH(vp);
/*
* if vp is of type VDIR, make sure dirent
* is filled up with all info (because of ptbl)
*/
if (vp->v_type == VDIR) {
if (hp->hs_dirent.ext_size == 0)
hs_filldirent(vp, &hp->hs_dirent);
}
filesize = hp->hs_dirent.ext_size;
/* Sanity checks. */
if (uiop->uio_resid == 0 || /* No data wanted. */
uiop->uio_loffset > HS_MAXFILEOFF || /* Offset too big. */
uiop->uio_loffset >= filesize) /* Past EOF. */
return (0);
do {
/*
* We want to ask for only the "right" amount of data.
* In this case that means:-
*
* We can't get data from beyond our EOF. If asked,
* we will give a short read.
*
* segmap_getmapflt returns buffers of MAXBSIZE bytes.
* These buffers are always MAXBSIZE aligned.
* If our starting offset is not MAXBSIZE aligned,
* we can only ask for less than MAXBSIZE bytes.
*
* If our requested offset and length are such that
* they belong in different MAXBSIZE aligned slots
* then we'll be making more than one call on
* segmap_getmapflt.
*
* This diagram shows the variables we use and their
* relationships.
*
* |<-----MAXBSIZE----->|
* +--------------------------...+
* |.....mapon->|<--n-->|....*...|EOF
* +--------------------------...+
* uio_loffset->|
* uio_resid....|<---------->|
* diff.........|<-------------->|
*
* So, in this case our offset is not aligned
* and our request takes us outside of the
* MAXBSIZE window. We will break this up into
* two segmap_getmapflt calls.
*/
size_t nbytes;
offset_t mapon;
size_t n;
uint_t flags;
mapon = uiop->uio_loffset & MAXBOFFSET;
diff = filesize - uiop->uio_loffset;
nbytes = (size_t)MIN(MAXBSIZE - mapon, uiop->uio_resid);
n = MIN(diff, nbytes);
if (n <= 0) {
/* EOF or request satisfied. */
return (0);
}
base = segmap_getmapflt(segkmap, vp,
(u_offset_t)uiop->uio_loffset, n, 1, S_READ);
error = uiomove(base + mapon, n, UIO_READ, uiop);
if (error == 0) {
/*
* if read a whole block, or read to eof,
* won't need this buffer again soon.
*/
if (n + mapon == MAXBSIZE ||
uiop->uio_loffset == filesize)
flags = SM_DONTNEED;
else
flags = 0;
error = segmap_release(segkmap, base, flags);
} else
(void) segmap_release(segkmap, base, 0);
} while (error == 0 && uiop->uio_resid > 0);
return (error);
}
/*ARGSUSED2*/
static int
hsfs_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cred,
caller_context_t *ct)
{
struct hsnode *hp;
struct vfs *vfsp;
struct hsfs *fsp;
hp = VTOH(vp);
fsp = VFS_TO_HSFS(vp->v_vfsp);
vfsp = vp->v_vfsp;
if ((hp->hs_dirent.ext_size == 0) && (vp->v_type == VDIR)) {
hs_filldirent(vp, &hp->hs_dirent);
}
vap->va_type = IFTOVT(hp->hs_dirent.mode);
vap->va_mode = hp->hs_dirent.mode;
vap->va_uid = hp->hs_dirent.uid;
vap->va_gid = hp->hs_dirent.gid;
vap->va_fsid = vfsp->vfs_dev;
vap->va_nodeid = (ino64_t)hp->hs_nodeid;
vap->va_nlink = hp->hs_dirent.nlink;
vap->va_size = (offset_t)hp->hs_dirent.ext_size;
vap->va_atime.tv_sec = hp->hs_dirent.adate.tv_sec;
vap->va_atime.tv_nsec = hp->hs_dirent.adate.tv_usec*1000;
vap->va_mtime.tv_sec = hp->hs_dirent.mdate.tv_sec;
vap->va_mtime.tv_nsec = hp->hs_dirent.mdate.tv_usec*1000;
vap->va_ctime.tv_sec = hp->hs_dirent.cdate.tv_sec;
vap->va_ctime.tv_nsec = hp->hs_dirent.cdate.tv_usec*1000;
if (vp->v_type == VCHR || vp->v_type == VBLK)
vap->va_rdev = hp->hs_dirent.r_dev;
else
vap->va_rdev = 0;
vap->va_blksize = vfsp->vfs_bsize;
/* no. of blocks = no. of data blocks + no. of xar blocks */
vap->va_nblocks = (fsblkcnt64_t)howmany(vap->va_size + (u_longlong_t)
(hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift), DEV_BSIZE);
vap->va_seq = hp->hs_seq;
return (0);
}
/*ARGSUSED*/
static int
hsfs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred,
caller_context_t *ct)
{
struct hsnode *hp;
if (vp->v_type != VLNK)
return (EINVAL);
hp = VTOH(vp);
if (hp->hs_dirent.sym_link == (char *)NULL)
return (ENOENT);
return (uiomove(hp->hs_dirent.sym_link,
(size_t)MIN(hp->hs_dirent.ext_size,
uiop->uio_resid), UIO_READ, uiop));
}
/*ARGSUSED*/
static void
hsfs_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
{
struct hsnode *hp;
struct hsfs *fsp;
int nopage;
hp = VTOH(vp);
fsp = VFS_TO_HSFS(vp->v_vfsp);
/*
* Note: acquiring and holding v_lock for quite a while
* here serializes on the vnode; this is unfortunate, but
* likely not to overly impact performance, as the underlying
* device (CDROM drive) is quite slow.
*/
rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
mutex_enter(&hp->hs_contents_lock);
mutex_enter(&vp->v_lock);
if (vp->v_count < 1) {
panic("hsfs_inactive: v_count < 1");
/*NOTREACHED*/
}
VN_RELE_LOCKED(vp);
if (vp->v_count > 0 || (hp->hs_flags & HREF) == 0) {
mutex_exit(&vp->v_lock);
mutex_exit(&hp->hs_contents_lock);
rw_exit(&fsp->hsfs_hash_lock);
return;
}
if (vp->v_count == 0) {
/*
* Free the hsnode.
* If there are no pages associated with the
* hsnode, give it back to the kmem_cache,
* else put at the end of this file system's
* internal free list.
*/
nopage = !vn_has_cached_data(vp);
hp->hs_flags = 0;
/*
* exit these locks now, since hs_freenode may
* kmem_free the hsnode and embedded vnode
*/
mutex_exit(&vp->v_lock);
mutex_exit(&hp->hs_contents_lock);
hs_freenode(vp, fsp, nopage);
} else {
mutex_exit(&vp->v_lock);
mutex_exit(&hp->hs_contents_lock);
}
rw_exit(&fsp->hsfs_hash_lock);
}
/*ARGSUSED*/
static int
hsfs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
caller_context_t *ct, int *direntflags, pathname_t *realpnp)
{
int error;
int namelen = (int)strlen(nm);
if (*nm == '\0') {
VN_HOLD(dvp);
*vpp = dvp;
return (0);
}
/*
* If we're looking for ourself, life is simple.
*/
if (namelen == 1 && *nm == '.') {
if (error = hs_access(dvp, (mode_t)VEXEC, cred))
return (error);
VN_HOLD(dvp);
*vpp = dvp;
return (0);
}
return (hs_dirlook(dvp, nm, namelen, vpp, cred));
}
/*ARGSUSED*/
static int
hsfs_readdir(struct vnode *vp, struct uio *uiop, struct cred *cred, int *eofp,
caller_context_t *ct, int flags)
{
struct hsnode *dhp;
struct hsfs *fsp;
struct hs_direntry hd;
struct dirent64 *nd;
int error;
uint_t offset; /* real offset in directory */
uint_t dirsiz; /* real size of directory */
uchar_t *blkp;
int hdlen; /* length of hs directory entry */
long ndlen; /* length of dirent entry */
int bytes_wanted;
size_t bufsize; /* size of dirent buffer */
char *outbuf; /* ptr to dirent buffer */
char *dname;
int dnamelen;
size_t dname_size;
struct fbuf *fbp;
uint_t last_offset; /* last index into current dir block */
ino64_t dirino; /* temporary storage before storing in dirent */
off_t diroff;
dhp = VTOH(vp);
fsp = VFS_TO_HSFS(vp->v_vfsp);
if (dhp->hs_dirent.ext_size == 0)
hs_filldirent(vp, &dhp->hs_dirent);
dirsiz = dhp->hs_dirent.ext_size;
if (uiop->uio_loffset >= dirsiz) { /* at or beyond EOF */
if (eofp)
*eofp = 1;
return (0);
}
ASSERT(uiop->uio_loffset <= HS_MAXFILEOFF);
offset = uiop->uio_loffset;
dname_size = fsp->hsfs_namemax + 1; /* 1 for the ending NUL */
dname = kmem_alloc(dname_size, KM_SLEEP);
bufsize = uiop->uio_resid + sizeof (struct dirent64);
outbuf = kmem_alloc(bufsize, KM_SLEEP);
nd = (struct dirent64 *)outbuf;
while (offset < dirsiz) {
bytes_wanted = MIN(MAXBSIZE, dirsiz - (offset & MAXBMASK));
error = fbread(vp, (offset_t)(offset & MAXBMASK),
(unsigned int)bytes_wanted, S_READ, &fbp);
if (error)
goto done;
blkp = (uchar_t *)fbp->fb_addr;
last_offset = (offset & MAXBMASK) + fbp->fb_count;
#define rel_offset(offset) ((offset) & MAXBOFFSET) /* index into blkp */
while (offset < last_offset) {
/*
* Very similar validation code is found in
* process_dirblock(), hsfs_node.c.
* For an explanation, see there.
* It may make sense for the future to
* "consolidate" the code in hs_parsedir(),
* process_dirblock() and hsfs_readdir() into
* a single utility function.
*/
hdlen = (int)((uchar_t)
HDE_DIR_LEN(&blkp[rel_offset(offset)]));
if (hdlen < HDE_ROOT_DIR_REC_SIZE ||
offset + hdlen > last_offset) {
/*
* advance to next sector boundary
*/
offset = roundup(offset + 1, HS_SECTOR_SIZE);
if (hdlen)
hs_log_bogus_disk_warning(fsp,
HSFS_ERR_TRAILING_JUNK, 0);
continue;
}
bzero(&hd, sizeof (hd));
/*
* Just ignore invalid directory entries.
* XXX - maybe hs_parsedir() will detect EXISTENCE bit
*/
if (!hs_parsedir(fsp, &blkp[rel_offset(offset)],
&hd, dname, &dnamelen, last_offset - offset)) {
/*
* Determine if there is enough room
*/
ndlen = (long)DIRENT64_RECLEN((dnamelen));
if ((ndlen + ((char *)nd - outbuf)) >
uiop->uio_resid) {
fbrelse(fbp, S_READ);
goto done; /* output buffer full */
}
diroff = offset + hdlen;
/*
* If the media carries rrip-v1.12 or newer,
* and we trust the inodes from the rrip data
* (use_rrip_inodes != 0), use that data. If the
* media has been created by a recent mkisofs
* version, we may trust all numbers in the
* starting extent number; otherwise, we cannot
* do this for zero sized files and symlinks,
* because if we did we'd end up mapping all of
* them to the same node. We use HS_DUMMY_INO
* in this case and make sure that we will not
* map all files to the same meta data.
*/
if (hd.inode != 0 && use_rrip_inodes) {
dirino = hd.inode;
} else if ((hd.ext_size == 0 ||
hd.sym_link != (char *)NULL) &&
(fsp->hsfs_flags & HSFSMNT_INODE) == 0) {
dirino = HS_DUMMY_INO;
} else {
dirino = hd.ext_lbn;
}
/* strncpy(9f) will zero uninitialized bytes */
ASSERT(strlen(dname) + 1 <=
DIRENT64_NAMELEN(ndlen));
(void) strncpy(nd->d_name, dname,
DIRENT64_NAMELEN(ndlen));
nd->d_reclen = (ushort_t)ndlen;
nd->d_off = (offset_t)diroff;
nd->d_ino = dirino;
nd = (struct dirent64 *)((char *)nd + ndlen);
/*
* free up space allocated for symlink
*/
if (hd.sym_link != (char *)NULL) {
kmem_free(hd.sym_link,
(size_t)(hd.ext_size+1));
hd.sym_link = (char *)NULL;
}
}
offset += hdlen;
}
fbrelse(fbp, S_READ);
}
/*
* Got here for one of the following reasons:
* 1) outbuf is full (error == 0)
* 2) end of directory reached (error == 0)
* 3) error reading directory sector (error != 0)
* 4) directory entry crosses sector boundary (error == 0)
*
* If any directory entries have been copied, don't report
* case 4. Instead, return the valid directory entries.
*
* If no entries have been copied, report the error.
* If case 4, this will be indistiguishable from EOF.
*/
done:
ndlen = ((char *)nd - outbuf);
if (ndlen != 0) {
error = uiomove(outbuf, (size_t)ndlen, UIO_READ, uiop);
uiop->uio_loffset = offset;
}
kmem_free(dname, dname_size);
kmem_free(outbuf, bufsize);
if (eofp && error == 0)
*eofp = (uiop->uio_loffset >= dirsiz);
return (error);
}
/*ARGSUSED2*/
static int
hsfs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
{
struct hsnode *hp;
struct hsfid *fid;
if (fidp->fid_len < (sizeof (*fid) - sizeof (fid->hf_len))) {
fidp->fid_len = sizeof (*fid) - sizeof (fid->hf_len);
return (ENOSPC);
}
fid = (struct hsfid *)fidp;
fid->hf_len = sizeof (*fid) - sizeof (fid->hf_len);
hp = VTOH(vp);
mutex_enter(&hp->hs_contents_lock);
fid->hf_dir_lbn = hp->hs_dir_lbn;
fid->hf_dir_off = (ushort_t)hp->hs_dir_off;
fid->hf_ino = hp->hs_nodeid;
mutex_exit(&hp->hs_contents_lock);
return (0);
}
/*ARGSUSED*/
static int
hsfs_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct)
{
return (0);
}
/*ARGSUSED*/
static int
hsfs_close(struct vnode *vp, int flag, int count, offset_t offset,
struct cred *cred, caller_context_t *ct)
{
(void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
cleanshares(vp, ttoproc(curthread)->p_pid);
return (0);
}
/*ARGSUSED2*/
static int
hsfs_access(struct vnode *vp, int mode, int flags, cred_t *cred,
caller_context_t *ct)
{
return (hs_access(vp, (mode_t)mode, cred));
}
/*
* the seek time of a CD-ROM is very slow, and data transfer
* rate is even worse (max. 150K per sec). The design
* decision is to reduce access to cd-rom as much as possible,
* and to transfer a sizable block (read-ahead) of data at a time.
* UFS style of read ahead one block at a time is not appropriate,
* and is not supported
*/
/*
* KLUSTSIZE should be a multiple of PAGESIZE and <= MAXPHYS.
*/
#define KLUSTSIZE (56 * 1024)
/* we don't support read ahead */
int hsfs_lostpage; /* no. of times we lost original page */
/*
* Used to prevent biodone() from releasing buf resources that
* we didn't allocate in quite the usual way.
*/
/*ARGSUSED*/
int
hsfs_iodone(struct buf *bp)
{
sema_v(&bp->b_io);
return (0);
}
/*
* The taskq thread that invokes the scheduling function to ensure
* that all readaheads are complete and cleans up the associated
* memory and releases the page lock.
*/
void
hsfs_ra_task(void *arg)
{
struct hio_info *info = arg;
uint_t count;
struct buf *wbuf;
ASSERT(info->pp != NULL);
for (count = 0; count < info->bufsused; count++) {
wbuf = &(info->bufs[count]);
DTRACE_PROBE1(hsfs_io_wait_ra, struct buf *, wbuf);
while (sema_tryp(&(info->sema[count])) == 0) {
if (hsched_invoke_strategy(info->fsp)) {
sema_p(&(info->sema[count]));
break;
}
}
sema_destroy(&(info->sema[count]));
DTRACE_PROBE1(hsfs_io_done_ra, struct buf *, wbuf);
biofini(&(info->bufs[count]));
}
for (count = 0; count < info->bufsused; count++) {
if (info->vas[count] != NULL) {
ppmapout(info->vas[count]);
}
}
kmem_free(info->vas, info->bufcnt * sizeof (caddr_t));
kmem_free(info->bufs, info->bufcnt * sizeof (struct buf));
kmem_free(info->sema, info->bufcnt * sizeof (ksema_t));
pvn_read_done(info->pp, 0);
kmem_cache_free(hio_info_cache, info);
}
/*
* Submit asynchronous readahead requests to the I/O scheduler
* depending on the number of pages to read ahead. These requests
* are asynchronous to the calling thread but I/O requests issued
* subsequently by other threads with higher LBNs must wait for
* these readaheads to complete since we have a single ordered
* I/O pipeline. Thus these readaheads are semi-asynchronous.
* A TaskQ handles waiting for the readaheads to complete.
*
* This function is mostly a copy of hsfs_getapage but somewhat
* simpler. A readahead request is aborted if page allocation
* fails.
*/
/*ARGSUSED*/
static int
hsfs_getpage_ra(struct vnode *vp, u_offset_t off, struct seg *seg,
caddr_t addr, struct hsnode *hp, struct hsfs *fsp, int xarsiz,
offset_t bof, int chunk_lbn_count, int chunk_data_bytes)
{
struct buf *bufs;
caddr_t *vas;
caddr_t va;
struct page *pp, *searchp, *lastp;
struct vnode *devvp;
ulong_t byte_offset;
size_t io_len_tmp;
uint_t io_off, io_len;
uint_t xlen;
uint_t filsiz;
uint_t secsize;
uint_t bufcnt;
uint_t bufsused;
uint_t count;
uint_t io_end;
uint_t which_chunk_lbn;
uint_t offset_lbn;
uint_t offset_extra;
offset_t offset_bytes;
uint_t remaining_bytes;
uint_t extension;
int remainder; /* must be signed */
diskaddr_t driver_block;
u_offset_t io_off_tmp;
ksema_t *fio_done;
struct hio_info *info;
size_t len;
ASSERT(fsp->hqueue != NULL);
if (addr >= seg->s_base + seg->s_size) {
return (-1);
}
devvp = fsp->hsfs_devvp;
secsize = fsp->hsfs_vol.lbn_size; /* bytes per logical block */
/* file data size */
filsiz = hp->hs_dirent.ext_size;
if (off >= filsiz)
return (0);
extension = 0;
pp = NULL;
extension += hp->hs_ra_bytes;
/*
* Some CD writers (e.g. Kodak Photo CD writers)
* create CDs in TAO mode and reserve tracks that
* are not completely written. Some sectors remain
* unreadable for this reason and give I/O errors.
* Also, there's no point in reading sectors
* we'll never look at. So, if we're asked to go
* beyond the end of a file, truncate to the length
* of that file.
*
* Additionally, this behaviour is required by section
* 6.4.5 of ISO 9660:1988(E).
*/
len = MIN(extension ? extension : PAGESIZE, filsiz - off);
/* A little paranoia */
if (len <= 0)
return (-1);
/*
* After all that, make sure we're asking for things in units
* that bdev_strategy() will understand (see bug 4202551).
*/
len = roundup(len, DEV_BSIZE);
pp = pvn_read_kluster(vp, off, seg, addr, &io_off_tmp,
&io_len_tmp, off, len, 1);
if (pp == NULL) {
hp->hs_num_contig = 0;
hp->hs_ra_bytes = 0;
hp->hs_prev_offset = 0;
return (-1);
}
io_off = (uint_t)io_off_tmp;
io_len = (uint_t)io_len_tmp;
/* check for truncation */
/*
* xxx Clean up and return EIO instead?
* xxx Ought to go to u_offset_t for everything, but we
* xxx call lots of things that want uint_t arguments.
*/
ASSERT(io_off == io_off_tmp);
/*
* get enough buffers for worst-case scenario
* (i.e., no coalescing possible).
*/
bufcnt = (len + secsize - 1) / secsize;
bufs = kmem_alloc(bufcnt * sizeof (struct buf), KM_SLEEP);
vas = kmem_alloc(bufcnt * sizeof (caddr_t), KM_SLEEP);
/*
* Allocate a array of semaphores since we are doing I/O
* scheduling.
*/
fio_done = kmem_alloc(bufcnt * sizeof (ksema_t), KM_SLEEP);
/*
* If our filesize is not an integer multiple of PAGESIZE,
* we zero that part of the last page that's between EOF and
* the PAGESIZE boundary.
*/
xlen = io_len & PAGEOFFSET;
if (xlen != 0)
pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
DTRACE_PROBE2(hsfs_readahead, struct vnode *, vp, uint_t, io_len);
va = NULL;
lastp = NULL;
searchp = pp;
io_end = io_off + io_len;
for (count = 0, byte_offset = io_off;
byte_offset < io_end;
count++) {
ASSERT(count < bufcnt);
bioinit(&bufs[count]);
bufs[count].b_edev = devvp->v_rdev;
bufs[count].b_dev = cmpdev(devvp->v_rdev);
bufs[count].b_flags = B_NOCACHE|B_BUSY|B_READ;
bufs[count].b_iodone = hsfs_iodone;
bufs[count].b_vp = vp;
bufs[count].b_file = vp;
/* Compute disk address for interleaving. */
/* considered without skips */
which_chunk_lbn = byte_offset / chunk_data_bytes;
/* factor in skips */
offset_lbn = which_chunk_lbn * chunk_lbn_count;
/* convert to physical byte offset for lbn */
offset_bytes = LBN_TO_BYTE(offset_lbn, vp->v_vfsp);
/* don't forget offset into lbn */
offset_extra = byte_offset % chunk_data_bytes;
/* get virtual block number for driver */
driver_block = lbtodb(bof + xarsiz
+ offset_bytes + offset_extra);
if (lastp != searchp) {
/* this branch taken first time through loop */
va = vas[count] = ppmapin(searchp, PROT_WRITE,
(caddr_t)-1);
/* ppmapin() guarantees not to return NULL */
} else {
vas[count] = NULL;
}
bufs[count].b_un.b_addr = va + byte_offset % PAGESIZE;
bufs[count].b_offset =
(offset_t)(byte_offset - io_off + off);
/*
* We specifically use the b_lblkno member here
* as even in the 32 bit world driver_block can
* get very large in line with the ISO9660 spec.
*/
bufs[count].b_lblkno = driver_block;
remaining_bytes = ((which_chunk_lbn + 1) * chunk_data_bytes)
- byte_offset;
/*
* remaining_bytes can't be zero, as we derived
* which_chunk_lbn directly from byte_offset.
*/
if ((remaining_bytes + byte_offset) < (off + len)) {
/* coalesce-read the rest of the chunk */
bufs[count].b_bcount = remaining_bytes;
} else {
/* get the final bits */
bufs[count].b_bcount = off + len - byte_offset;
}
remainder = PAGESIZE - (byte_offset % PAGESIZE);
if (bufs[count].b_bcount > remainder) {
bufs[count].b_bcount = remainder;
}
bufs[count].b_bufsize = bufs[count].b_bcount;
if (((offset_t)byte_offset + bufs[count].b_bcount) >
HS_MAXFILEOFF) {
break;
}
byte_offset += bufs[count].b_bcount;
/*
* We are scheduling I/O so we need to enqueue
* requests rather than calling bdev_strategy
* here. A later invocation of the scheduling
* function will take care of doing the actual
* I/O as it selects requests from the queue as
* per the scheduling logic.
*/
struct hio *hsio = kmem_cache_alloc(hio_cache,
KM_SLEEP);
sema_init(&fio_done[count], 0, NULL,
SEMA_DEFAULT, NULL);
hsio->bp = &bufs[count];
hsio->sema = &fio_done[count];
hsio->io_lblkno = bufs[count].b_lblkno;
hsio->nblocks = howmany(hsio->bp->b_bcount,
DEV_BSIZE);
/* used for deadline */
hsio->io_timestamp = drv_hztousec(ddi_get_lbolt());
/* for I/O coalescing */
hsio->contig_chain = NULL;
hsched_enqueue_io(fsp, hsio, 1);
lwp_stat_update(LWP_STAT_INBLK, 1);
lastp = searchp;
if ((remainder - bufs[count].b_bcount) < 1) {
searchp = searchp->p_next;
}
}
bufsused = count;
info = kmem_cache_alloc(hio_info_cache, KM_SLEEP);
info->bufs = bufs;
info->vas = vas;
info->sema = fio_done;
info->bufsused = bufsused;
info->bufcnt = bufcnt;
info->fsp = fsp;
info->pp = pp;
(void) taskq_dispatch(fsp->hqueue->ra_task,
hsfs_ra_task, info, KM_SLEEP);
/*
* The I/O locked pages are unlocked in our taskq thread.
*/
return (0);
}
/*
* Each file may have a different interleaving on disk. This makes
* things somewhat interesting. The gist is that there are some
* number of contiguous data sectors, followed by some other number
* of contiguous skip sectors. The sum of those two sets of sectors
* defines the interleave size. Unfortunately, it means that we generally
* can't simply read N sectors starting at a given offset to satisfy
* any given request.
*
* What we do is get the relevant memory pages via pvn_read_kluster(),
* then stride through the interleaves, setting up a buf for each
* sector that needs to be brought in. Instead of kmem_alloc'ing
* space for the sectors, though, we just point at the appropriate
* spot in the relevant page for each of them. This saves us a bunch
* of copying.
*
* NOTICE: The code below in hsfs_getapage is mostly same as the code
* in hsfs_getpage_ra above (with some omissions). If you are
* making any change to this function, please also look at
* hsfs_getpage_ra.
*/
/*ARGSUSED*/
static int
hsfs_getapage(struct vnode *vp, u_offset_t off, size_t len, uint_t *protp,
struct page *pl[], size_t plsz, struct seg *seg, caddr_t addr,
enum seg_rw rw, struct cred *cred)
{
struct hsnode *hp;
struct hsfs *fsp;
int err;
struct buf *bufs;
caddr_t *vas;
caddr_t va;
struct page *pp, *searchp, *lastp;
page_t *pagefound;
offset_t bof;
struct vnode *devvp;
ulong_t byte_offset;
size_t io_len_tmp;
uint_t io_off, io_len;
uint_t xlen;
uint_t filsiz;
uint_t secsize;
uint_t bufcnt;
uint_t bufsused;
uint_t count;
uint_t io_end;
uint_t which_chunk_lbn;
uint_t offset_lbn;
uint_t offset_extra;
offset_t offset_bytes;
uint_t remaining_bytes;
uint_t extension;
int remainder; /* must be signed */
int chunk_lbn_count;
int chunk_data_bytes;
int xarsiz;
diskaddr_t driver_block;
u_offset_t io_off_tmp;
ksema_t *fio_done;
int calcdone;
/*
* We don't support asynchronous operation at the moment, so
* just pretend we did it. If the pages are ever actually
* needed, they'll get brought in then.
*/
if (pl == NULL)
return (0);
hp = VTOH(vp);
fsp = VFS_TO_HSFS(vp->v_vfsp);
devvp = fsp->hsfs_devvp;
secsize = fsp->hsfs_vol.lbn_size; /* bytes per logical block */
/* file data size */
filsiz = hp->hs_dirent.ext_size;
/* disk addr for start of file */
bof = LBN_TO_BYTE((offset_t)hp->hs_dirent.ext_lbn, vp->v_vfsp);
/* xarsiz byte must be skipped for data */
xarsiz = hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift;
/* how many logical blocks in an interleave (data+skip) */
chunk_lbn_count = hp->hs_dirent.intlf_sz + hp->hs_dirent.intlf_sk;
if (chunk_lbn_count == 0) {
chunk_lbn_count = 1;
}
/*
* Convert interleaving size into bytes. The zero case
* (no interleaving) optimization is handled as a side-
* effect of the read-ahead logic.
*/
if (hp->hs_dirent.intlf_sz == 0) {
chunk_data_bytes = LBN_TO_BYTE(1, vp->v_vfsp);
/*
* Optimization: If our pagesize is a multiple of LBN
* bytes, we can avoid breaking up a page into individual
* lbn-sized requests.
*/
if (PAGESIZE % chunk_data_bytes == 0) {
chunk_lbn_count = BYTE_TO_LBN(PAGESIZE, vp->v_vfsp);
chunk_data_bytes = PAGESIZE;
}
} else {
chunk_data_bytes =
LBN_TO_BYTE(hp->hs_dirent.intlf_sz, vp->v_vfsp);
}
reread:
err = 0;
pagefound = 0;
calcdone = 0;
/*
* Do some read-ahead. This mostly saves us a bit of
* system cpu time more than anything else when doing
* sequential reads. At some point, could do the
* read-ahead asynchronously which might gain us something
* on wall time, but it seems unlikely....
*
* We do the easy case here, which is to read through
* the end of the chunk, minus whatever's at the end that
* won't exactly fill a page.
*/
if (hp->hs_ra_bytes > 0 && chunk_data_bytes != PAGESIZE) {
which_chunk_lbn = (off + len) / chunk_data_bytes;
extension = ((which_chunk_lbn + 1) * chunk_data_bytes) - off;
extension -= (extension % PAGESIZE);
} else {
extension = roundup(len, PAGESIZE);
}
atomic_inc_64(&fsp->total_pages_requested);
pp = NULL;
again:
/* search for page in buffer */
if ((pagefound = page_exists(vp, off)) == 0) {
/*
* Need to really do disk IO to get the page.
*/
if (!calcdone) {
extension += hp->hs_ra_bytes;
/*
* Some cd writers don't write sectors that aren't
* used. Also, there's no point in reading sectors
* we'll never look at. So, if we're asked to go
* beyond the end of a file, truncate to the length
* of that file.
*
* Additionally, this behaviour is required by section
* 6.4.5 of ISO 9660:1988(E).
*/
len = MIN(extension ? extension : PAGESIZE,
filsiz - off);
/* A little paranoia. */
ASSERT(len > 0);
/*
* After all that, make sure we're asking for things
* in units that bdev_strategy() will understand
* (see bug 4202551).
*/
len = roundup(len, DEV_BSIZE);
calcdone = 1;
}
pp = pvn_read_kluster(vp, off, seg, addr, &io_off_tmp,
&io_len_tmp, off, len, 0);
if (pp == NULL) {
/*
* Pressure on memory, roll back readahead
*/
hp->hs_num_contig = 0;
hp->hs_ra_bytes = 0;
hp->hs_prev_offset = 0;
goto again;
}
io_off = (uint_t)io_off_tmp;
io_len = (uint_t)io_len_tmp;
/* check for truncation */
/*
* xxx Clean up and return EIO instead?
* xxx Ought to go to u_offset_t for everything, but we
* xxx call lots of things that want uint_t arguments.
*/
ASSERT(io_off == io_off_tmp);
/*
* get enough buffers for worst-case scenario
* (i.e., no coalescing possible).
*/
bufcnt = (len + secsize - 1) / secsize;
bufs = kmem_zalloc(bufcnt * sizeof (struct buf), KM_SLEEP);
vas = kmem_alloc(bufcnt * sizeof (caddr_t), KM_SLEEP);
/*
* Allocate a array of semaphores if we are doing I/O
* scheduling.
*/
if (fsp->hqueue != NULL)
fio_done = kmem_alloc(bufcnt * sizeof (ksema_t),
KM_SLEEP);
for (count = 0; count < bufcnt; count++) {
bioinit(&bufs[count]);
bufs[count].b_edev = devvp->v_rdev;
bufs[count].b_dev = cmpdev(devvp->v_rdev);
bufs[count].b_flags = B_NOCACHE|B_BUSY|B_READ;
bufs[count].b_iodone = hsfs_iodone;
bufs[count].b_vp = vp;
bufs[count].b_file = vp;
}
/*
* If our filesize is not an integer multiple of PAGESIZE,
* we zero that part of the last page that's between EOF and
* the PAGESIZE boundary.
*/
xlen = io_len & PAGEOFFSET;
if (xlen != 0)
pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
va = NULL;
lastp = NULL;
searchp = pp;
io_end = io_off + io_len;
for (count = 0, byte_offset = io_off;
byte_offset < io_end; count++) {
ASSERT(count < bufcnt);
/* Compute disk address for interleaving. */
/* considered without skips */
which_chunk_lbn = byte_offset / chunk_data_bytes;
/* factor in skips */
offset_lbn = which_chunk_lbn * chunk_lbn_count;
/* convert to physical byte offset for lbn */
offset_bytes = LBN_TO_BYTE(offset_lbn, vp->v_vfsp);
/* don't forget offset into lbn */
offset_extra = byte_offset % chunk_data_bytes;
/* get virtual block number for driver */
driver_block =
lbtodb(bof + xarsiz + offset_bytes + offset_extra);
if (lastp != searchp) {
/* this branch taken first time through loop */
va = vas[count] =
ppmapin(searchp, PROT_WRITE, (caddr_t)-1);
/* ppmapin() guarantees not to return NULL */
} else {
vas[count] = NULL;
}
bufs[count].b_un.b_addr = va + byte_offset % PAGESIZE;
bufs[count].b_offset =
(offset_t)(byte_offset - io_off + off);
/*
* We specifically use the b_lblkno member here
* as even in the 32 bit world driver_block can
* get very large in line with the ISO9660 spec.
*/
bufs[count].b_lblkno = driver_block;
remaining_bytes =
((which_chunk_lbn + 1) * chunk_data_bytes)
- byte_offset;
/*
* remaining_bytes can't be zero, as we derived
* which_chunk_lbn directly from byte_offset.
*/
if ((remaining_bytes + byte_offset) < (off + len)) {
/* coalesce-read the rest of the chunk */
bufs[count].b_bcount = remaining_bytes;
} else {
/* get the final bits */
bufs[count].b_bcount = off + len - byte_offset;
}
/*
* It would be nice to do multiple pages'
* worth at once here when the opportunity
* arises, as that has been shown to improve
* our wall time. However, to do that
* requires that we use the pageio subsystem,
* which doesn't mix well with what we're
* already using here. We can't use pageio
* all the time, because that subsystem
* assumes that a page is stored in N
* contiguous blocks on the device.
* Interleaving violates that assumption.
*
* Update: This is now not so big a problem
* because of the I/O scheduler sitting below
* that can re-order and coalesce I/O requests.
*/
remainder = PAGESIZE - (byte_offset % PAGESIZE);
if (bufs[count].b_bcount > remainder) {
bufs[count].b_bcount = remainder;
}
bufs[count].b_bufsize = bufs[count].b_bcount;
if (((offset_t)byte_offset + bufs[count].b_bcount) >
HS_MAXFILEOFF) {
break;
}
byte_offset += bufs[count].b_bcount;
if (fsp->hqueue == NULL) {
(void) bdev_strategy(&bufs[count]);
} else {
/*
* We are scheduling I/O so we need to enqueue
* requests rather than calling bdev_strategy
* here. A later invocation of the scheduling
* function will take care of doing the actual
* I/O as it selects requests from the queue as
* per the scheduling logic.
*/
struct hio *hsio = kmem_cache_alloc(hio_cache,
KM_SLEEP);
sema_init(&fio_done[count], 0, NULL,
SEMA_DEFAULT, NULL);
hsio->bp = &bufs[count];
hsio->sema = &fio_done[count];
hsio->io_lblkno = bufs[count].b_lblkno;
hsio->nblocks = howmany(hsio->bp->b_bcount,
DEV_BSIZE);
/* used for deadline */
hsio->io_timestamp =
drv_hztousec(ddi_get_lbolt());
/* for I/O coalescing */
hsio->contig_chain = NULL;
hsched_enqueue_io(fsp, hsio, 0);
}
lwp_stat_update(LWP_STAT_INBLK, 1);
lastp = searchp;
if ((remainder - bufs[count].b_bcount) < 1) {
searchp = searchp->p_next;
}
}
bufsused = count;
/* Now wait for everything to come in */
if (fsp->hqueue == NULL) {
for (count = 0; count < bufsused; count++) {
if (err == 0) {
err = biowait(&bufs[count]);
} else
(void) biowait(&bufs[count]);
}
} else {
for (count = 0; count < bufsused; count++) {
struct buf *wbuf;
/*
* Invoke scheduling function till our buf
* is processed. In doing this it might
* process bufs enqueued by other threads
* which is good.
*/
wbuf = &bufs[count];
DTRACE_PROBE1(hsfs_io_wait, struct buf *, wbuf);
while (sema_tryp(&fio_done[count]) == 0) {
/*
* hsched_invoke_strategy will return 1
* if the I/O queue is empty. This means
* that there is another thread who has
* issued our buf and is waiting. So we
* just block instead of spinning.
*/
if (hsched_invoke_strategy(fsp)) {
sema_p(&fio_done[count]);
break;
}
}
sema_destroy(&fio_done[count]);
DTRACE_PROBE1(hsfs_io_done, struct buf *, wbuf);
if (err == 0) {
err = geterror(wbuf);
}
}
kmem_free(fio_done, bufcnt * sizeof (ksema_t));
}
/* Don't leak resources */
for (count = 0; count < bufcnt; count++) {
biofini(&bufs[count]);
if (count < bufsused && vas[count] != NULL) {
ppmapout(vas[count]);
}
}
kmem_free(vas, bufcnt * sizeof (caddr_t));
kmem_free(bufs, bufcnt * sizeof (struct buf));
}
if (err) {
pvn_read_done(pp, B_ERROR);
return (err);
}
/*
* Lock the requested page, and the one after it if possible.
* Don't bother if our caller hasn't given us a place to stash
* the page pointers, since otherwise we'd lock pages that would
* never get unlocked.
*/
if (pagefound) {
int index;
ulong_t soff;
/*
* Make sure it's in memory before we say it's here.
*/
if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
hsfs_lostpage++;
goto reread;
}
pl[0] = pp;
index = 1;
atomic_inc_64(&fsp->cache_read_pages);
/*
* Try to lock the next page, if it exists, without
* blocking.
*/
plsz -= PAGESIZE;
/* LINTED (plsz is unsigned) */
for (soff = off + PAGESIZE; plsz > 0;
soff += PAGESIZE, plsz -= PAGESIZE) {
pp = page_lookup_nowait(vp, (u_offset_t)soff,
SE_SHARED);
if (pp == NULL)
break;
pl[index++] = pp;
}
pl[index] = NULL;
/*
* Schedule a semi-asynchronous readahead if we are
* accessing the last cached page for the current
* file.
*
* Doing this here means that readaheads will be
* issued only if cache-hits occur. This is an advantage
* since cache-hits would mean that readahead is giving
* the desired benefit. If cache-hits do not occur there
* is no point in reading ahead of time - the system
* is loaded anyway.
*/
if (fsp->hqueue != NULL &&
hp->hs_prev_offset - off == PAGESIZE &&
hp->hs_prev_offset < filsiz &&
hp->hs_ra_bytes > 0 &&
!page_exists(vp, hp->hs_prev_offset)) {
(void) hsfs_getpage_ra(vp, hp->hs_prev_offset, seg,
addr + PAGESIZE, hp, fsp, xarsiz, bof,
chunk_lbn_count, chunk_data_bytes);
}
return (0);
}
if (pp != NULL) {
pvn_plist_init(pp, pl, plsz, off, io_len, rw);
}
return (err);
}
/*ARGSUSED*/
static int
hsfs_getpage(struct vnode *vp, offset_t off, size_t len, uint_t *protp,
struct page *pl[], size_t plsz, struct seg *seg, caddr_t addr,
enum seg_rw rw, struct cred *cred, caller_context_t *ct)
{
uint_t filsiz;
struct hsfs *fsp;
struct hsnode *hp;
fsp = VFS_TO_HSFS(vp->v_vfsp);
hp = VTOH(vp);
/* does not support write */
if (rw == S_WRITE) {
return (EROFS);
}
if (vp->v_flag & VNOMAP) {
return (ENOSYS);
}
ASSERT(off <= HS_MAXFILEOFF);
/*
* Determine file data size for EOF check.
*/
filsiz = hp->hs_dirent.ext_size;
if ((off + len) > (offset_t)(filsiz + PAGEOFFSET) && seg != segkmap)
return (EFAULT); /* beyond EOF */
/*
* Async Read-ahead computation.
* This attempts to detect sequential access pattern and
* enables reading extra pages ahead of time.
*/
if (fsp->hqueue != NULL) {
/*
* This check for sequential access also takes into
* account segmap weirdness when reading in chunks
* less than the segmap size of 8K.
*/
if (hp->hs_prev_offset == off || (off <
hp->hs_prev_offset && off + MAX(len, PAGESIZE)
>= hp->hs_prev_offset)) {
if (hp->hs_num_contig <
(seq_contig_requests - 1)) {
hp->hs_num_contig++;
} else {
/*
* We increase readahead quantum till
* a predefined max. max_readahead_bytes
* is a multiple of PAGESIZE.
*/
if (hp->hs_ra_bytes <
fsp->hqueue->max_ra_bytes) {
hp->hs_ra_bytes += PAGESIZE;
}
}
} else {
/*
* Not contiguous so reduce read ahead counters.
*/
if (hp->hs_ra_bytes > 0)
hp->hs_ra_bytes -= PAGESIZE;
if (hp->hs_ra_bytes <= 0) {
hp->hs_ra_bytes = 0;
if (hp->hs_num_contig > 0)
hp->hs_num_contig--;
}
}
/*
* Length must be rounded up to page boundary.
* since we read in units of pages.
*/
hp->hs_prev_offset = off + roundup(len, PAGESIZE);
DTRACE_PROBE1(hsfs_compute_ra, struct hsnode *, hp);
}
if (protp != NULL)
*protp = PROT_ALL;
return (pvn_getpages(hsfs_getapage, vp, off, len, protp, pl, plsz,
seg, addr, rw, cred));
}
/*
* This function should never be called. We need to have it to pass
* it as an argument to other functions.
*/
/*ARGSUSED*/
int
hsfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
int flags, cred_t *cr)
{
/* should never happen - just destroy it */
cmn_err(CE_NOTE, "hsfs_putapage: dirty HSFS page");
pvn_write_done(pp, B_ERROR | B_WRITE | B_INVAL | B_FORCE | flags);
return (0);
}
/*
* The only flags we support are B_INVAL, B_FREE and B_DONTNEED.
* B_INVAL is set by:
*
* 1) the MC_SYNC command of memcntl(2) to support the MS_INVALIDATE flag.
* 2) the MC_ADVISE command of memcntl(2) with the MADV_DONTNEED advice
* which translates to an MC_SYNC with the MS_INVALIDATE flag.
*
* The B_FREE (as well as the B_DONTNEED) flag is set when the
* MADV_SEQUENTIAL advice has been used. VOP_PUTPAGE is invoked
* from SEGVN to release pages behind a pagefault.
*/
/*ARGSUSED*/
static int
hsfs_putpage(struct vnode *vp, offset_t off, size_t len, int flags,
struct cred *cr, caller_context_t *ct)
{
int error = 0;
if (vp->v_count == 0) {
panic("hsfs_putpage: bad v_count");
/*NOTREACHED*/
}
if (vp->v_flag & VNOMAP)
return (ENOSYS);
ASSERT(off <= HS_MAXFILEOFF);
if (!vn_has_cached_data(vp)) /* no pages mapped */
return (0);
if (len == 0) { /* from 'off' to EOF */
error = pvn_vplist_dirty(vp, off, hsfs_putapage, flags, cr);
} else {
offset_t end_off = off + len;
offset_t file_size = VTOH(vp)->hs_dirent.ext_size;
offset_t io_off;
file_size = (file_size + PAGESIZE - 1) & PAGEMASK;
if (end_off > file_size)
end_off = file_size;
for (io_off = off; io_off < end_off; io_off += PAGESIZE) {
page_t *pp;
/*
* We insist on getting the page only if we are
* about to invalidate, free or write it and
* the B_ASYNC flag is not set.
*/
if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
pp = page_lookup(vp, io_off,
(flags & (B_INVAL | B_FREE)) ?
SE_EXCL : SE_SHARED);
} else {
pp = page_lookup_nowait(vp, io_off,
(flags & B_FREE) ? SE_EXCL : SE_SHARED);
}
if (pp == NULL)
continue;
/*
* Normally pvn_getdirty() should return 0, which
* impies that it has done the job for us.
* The shouldn't-happen scenario is when it returns 1.
* This means that the page has been modified and
* needs to be put back.
* Since we can't write on a CD, we fake a failed
* I/O and force pvn_write_done() to destroy the page.
*/
if (pvn_getdirty(pp, flags) == 1) {
cmn_err(CE_NOTE,
"hsfs_putpage: dirty HSFS page");
pvn_write_done(pp, flags |
B_ERROR | B_WRITE | B_INVAL | B_FORCE);
}
}
}
return (error);
}
/*ARGSUSED*/
static int
hsfs_map(struct vnode *vp, offset_t off, struct as *as, caddr_t *addrp,
size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, struct cred *cred,
caller_context_t *ct)
{
struct segvn_crargs vn_a;
int error;
/* VFS_RECORD(vp->v_vfsp, VS_MAP, VS_CALL); */
if (vp->v_flag & VNOMAP)
return (ENOSYS);
if ((prot & PROT_WRITE) && (flags & MAP_SHARED))
return (ENOSYS);
if (off > HS_MAXFILEOFF || off < 0 ||
(off + len) < 0 || (off + len) > HS_MAXFILEOFF)
return (ENXIO);
if (vp->v_type != VREG) {
return (ENODEV);
}
/*
* If file is being locked, disallow mapping.
*/
if (vn_has_mandatory_locks(vp, VTOH(vp)->hs_dirent.mode))
return (EAGAIN);
as_rangelock(as);
error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
if (error != 0) {
as_rangeunlock(as);
return (error);
}
vn_a.vp = vp;
vn_a.offset = off;
vn_a.type = flags & MAP_TYPE;
vn_a.prot = prot;
vn_a.maxprot = maxprot;
vn_a.flags = flags & ~MAP_TYPE;
vn_a.cred = cred;
vn_a.amp = NULL;
vn_a.szc = 0;
vn_a.lgrp_mem_policy_flags = 0;
error = as_map(as, *addrp, len, segvn_create, &vn_a);
as_rangeunlock(as);
return (error);
}
/* ARGSUSED */
static int
hsfs_addmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, struct cred *cr,
caller_context_t *ct)
{
struct hsnode *hp;
if (vp->v_flag & VNOMAP)
return (ENOSYS);
hp = VTOH(vp);
mutex_enter(&hp->hs_contents_lock);
hp->hs_mapcnt += btopr(len);
mutex_exit(&hp->hs_contents_lock);
return (0);
}
/*ARGSUSED*/
static int
hsfs_delmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
size_t len, uint_t prot, uint_t maxprot, uint_t flags, struct cred *cr,
caller_context_t *ct)
{
struct hsnode *hp;
if (vp->v_flag & VNOMAP)
return (ENOSYS);
hp = VTOH(vp);
mutex_enter(&hp->hs_contents_lock);
hp->hs_mapcnt -= btopr(len); /* Count released mappings */
ASSERT(hp->hs_mapcnt >= 0);
mutex_exit(&hp->hs_contents_lock);
return (0);
}
/* ARGSUSED */
static int
hsfs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
caller_context_t *ct)
{
return (*noffp < 0 ? EINVAL : 0);
}
/* ARGSUSED */
static int
hsfs_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
caller_context_t *ct)
{
struct hsnode *hp = VTOH(vp);
/*
* If the file is being mapped, disallow fs_frlock.
* We are not holding the hs_contents_lock while checking
* hs_mapcnt because the current locking strategy drops all
* locks before calling fs_frlock.
* So, hs_mapcnt could change before we enter fs_frlock making
* it meaningless to have held hs_contents_lock in the first place.
*/
if (hp->hs_mapcnt > 0 && MANDLOCK(vp, hp->hs_dirent.mode))
return (EAGAIN);
return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
}
static int
hsched_deadline_compare(const void *x1, const void *x2)
{
const struct hio *h1 = x1;
const struct hio *h2 = x2;
if (h1->io_timestamp < h2->io_timestamp)
return (-1);
if (h1->io_timestamp > h2->io_timestamp)
return (1);
if (h1->io_lblkno < h2->io_lblkno)
return (-1);
if (h1->io_lblkno > h2->io_lblkno)
return (1);
if (h1 < h2)
return (-1);
if (h1 > h2)
return (1);
return (0);
}
static int
hsched_offset_compare(const void *x1, const void *x2)
{
const struct hio *h1 = x1;
const struct hio *h2 = x2;
if (h1->io_lblkno < h2->io_lblkno)
return (-1);
if (h1->io_lblkno > h2->io_lblkno)
return (1);
if (h1 < h2)
return (-1);
if (h1 > h2)
return (1);
return (0);
}
void
hsched_init_caches(void)
{
hio_cache = kmem_cache_create("hsfs_hio_cache",
sizeof (struct hio), 0, NULL,
NULL, NULL, NULL, NULL, 0);
hio_info_cache = kmem_cache_create("hsfs_hio_info_cache",
sizeof (struct hio_info), 0, NULL,
NULL, NULL, NULL, NULL, 0);
}
void
hsched_fini_caches(void)
{
kmem_cache_destroy(hio_cache);
kmem_cache_destroy(hio_info_cache);
}
/*
* Initialize I/O scheduling structures. This is called via hsfs_mount
*/
void
hsched_init(struct hsfs *fsp, int fsid, struct modlinkage *modlinkage)
{
struct hsfs_queue *hqueue = fsp->hqueue;
struct vnode *vp = fsp->hsfs_devvp;
/* TaskQ name of the form: hsched_task_ + stringof(int) */
char namebuf[23];
int error, err;
struct dk_cinfo info;
ldi_handle_t lh;
ldi_ident_t li;
/*
* Default maxtransfer = 16k chunk
*/
hqueue->dev_maxtransfer = 16384;
/*
* Try to fetch the maximum device transfer size. This is used to
* ensure that a coalesced block does not exceed the maxtransfer.
*/
err = ldi_ident_from_mod(modlinkage, &li);
if (err) {
cmn_err(CE_NOTE, "hsched_init: Querying device failed");
cmn_err(CE_NOTE, "hsched_init: ldi_ident_from_mod err=%d\n",
err);
goto set_ra;
}
err = ldi_open_by_dev(&(vp->v_rdev), OTYP_CHR, FREAD, CRED(), &lh, li);
ldi_ident_release(li);
if (err) {
cmn_err(CE_NOTE, "hsched_init: Querying device failed");
cmn_err(CE_NOTE, "hsched_init: ldi_open err=%d\n", err);
goto set_ra;
}
error = ldi_ioctl(lh, DKIOCINFO, (intptr_t)&info, FKIOCTL,
CRED(), &err);
err = ldi_close(lh, FREAD, CRED());
if (err) {
cmn_err(CE_NOTE, "hsched_init: Querying device failed");
cmn_err(CE_NOTE, "hsched_init: ldi_close err=%d\n", err);
}
if (error == 0) {
hqueue->dev_maxtransfer = ldbtob(info.dki_maxtransfer);
}
set_ra:
/*
* Max size of data to read ahead for sequential access pattern.
* Conservative to avoid letting the underlying CD drive to spin
* down, in case the application is reading slowly.
* We read ahead upto a max of 4 pages.
*/
hqueue->max_ra_bytes = PAGESIZE * 8;
mutex_init(&(hqueue->hsfs_queue_lock), NULL, MUTEX_DEFAULT, NULL);
mutex_init(&(hqueue->strategy_lock), NULL, MUTEX_DEFAULT, NULL);
avl_create(&(hqueue->read_tree), hsched_offset_compare,
sizeof (struct hio), offsetof(struct hio, io_offset_node));
avl_create(&(hqueue->deadline_tree), hsched_deadline_compare,
sizeof (struct hio), offsetof(struct hio, io_deadline_node));
(void) snprintf(namebuf, sizeof (namebuf), "hsched_task_%d", fsid);
hqueue->ra_task = taskq_create(namebuf, hsfs_taskq_nthreads,
minclsyspri + 2, 1, 104857600 / PAGESIZE, TASKQ_DYNAMIC);
hqueue->next = NULL;
hqueue->nbuf = kmem_zalloc(sizeof (struct buf), KM_SLEEP);
}
void
hsched_fini(struct hsfs_queue *hqueue)
{
if (hqueue != NULL) {
/*
* Remove the sentinel if there was one.
*/
if (hqueue->next != NULL) {
avl_remove(&hqueue->read_tree, hqueue->next);
kmem_cache_free(hio_cache, hqueue->next);
}
avl_destroy(&(hqueue->read_tree));
avl_destroy(&(hqueue->deadline_tree));
mutex_destroy(&(hqueue->hsfs_queue_lock));
mutex_destroy(&(hqueue->strategy_lock));
/*
* If there are any existing readahead threads running
* taskq_destroy will wait for them to finish.
*/
taskq_destroy(hqueue->ra_task);
kmem_free(hqueue->nbuf, sizeof (struct buf));
}
}
/*
* Determine if two I/O requests are adjacent to each other so
* that they can coalesced.
*/
#define IS_ADJACENT(io, nio) \
(((io)->io_lblkno + (io)->nblocks == (nio)->io_lblkno) && \
(io)->bp->b_edev == (nio)->bp->b_edev)
/*
* This performs the actual I/O scheduling logic. We use the Circular
* Look algorithm here. Sort the I/O requests in ascending order of
* logical block number and process them starting with the lowest
* numbered block and progressing towards higher block numbers in the
* queue. Once there are no more higher numbered blocks, start again
* with the lowest one. This is good for CD/DVD as you keep moving
* the head in one direction along the outward spiral track and avoid
* too many seeks as much as possible. The re-ordering also allows
* us to coalesce adjacent requests into one larger request.
* This is thus essentially a 1-way Elevator with front merging.
*
* In addition each read request here has a deadline and will be
* processed out of turn if the deadline (500ms) expires.
*
* This function is necessarily serialized via hqueue->strategy_lock.
* This function sits just below hsfs_getapage and processes all read
* requests orginating from that function.
*/
int
hsched_invoke_strategy(struct hsfs *fsp)
{
struct hsfs_queue *hqueue;
struct buf *nbuf;
struct hio *fio, *nio, *tio, *prev, *last;
size_t bsize, soffset, offset, data;
int bioret, bufcount;
struct vnode *fvp;
ksema_t *io_done;
caddr_t iodata;
hqueue = fsp->hqueue;
mutex_enter(&hqueue->strategy_lock);
mutex_enter(&hqueue->hsfs_queue_lock);
/*
* Check for Deadline expiration first
*/
fio = avl_first(&hqueue->deadline_tree);
/*
* Paranoid check for empty I/O queue. Both deadline
* and read trees contain same data sorted in different
* ways. So empty deadline tree = empty read tree.
*/
if (fio == NULL) {
/*
* Remove the sentinel if there was one.
*/
if (hqueue->next != NULL) {
avl_remove(&hqueue->read_tree, hqueue->next);
kmem_cache_free(hio_cache, hqueue->next);
hqueue->next = NULL;
}
mutex_exit(&hqueue->hsfs_queue_lock);
mutex_exit(&hqueue->strategy_lock);
return (1);
}
if (drv_hztousec(ddi_get_lbolt()) - fio->io_timestamp
< HSFS_READ_DEADLINE) {
/*
* Apply standard scheduling logic. This uses the
* C-LOOK approach. Process I/O requests in ascending
* order of logical block address till no subsequent
* higher numbered block request remains. Then start
* again from the lowest numbered block in the queue.
*
* We do this cheaply here by means of a sentinel.
* The last processed I/O structure from the previous
* invocation of this func, is left dangling in the
* read_tree so that we can easily scan to the next
* higher numbered request and remove the sentinel.
*/
fio = NULL;
if (hqueue->next != NULL) {
fio = AVL_NEXT(&hqueue->read_tree, hqueue->next);
avl_remove(&hqueue->read_tree, hqueue->next);
kmem_cache_free(hio_cache, hqueue->next);
hqueue->next = NULL;
}
if (fio == NULL) {
fio = avl_first(&hqueue->read_tree);
}
} else if (hqueue->next != NULL) {
DTRACE_PROBE1(hsfs_deadline_expiry, struct hio *, fio);
avl_remove(&hqueue->read_tree, hqueue->next);
kmem_cache_free(hio_cache, hqueue->next);
hqueue->next = NULL;
}
/*
* In addition we try to coalesce contiguous
* requests into one bigger request.
*/
bufcount = 1;
bsize = ldbtob(fio->nblocks);
fvp = fio->bp->b_file;
nio = AVL_NEXT(&hqueue->read_tree, fio);
tio = fio;
while (nio != NULL && IS_ADJACENT(tio, nio) &&
bsize < hqueue->dev_maxtransfer) {
avl_remove(&hqueue->deadline_tree, tio);
avl_remove(&hqueue->read_tree, tio);
tio->contig_chain = nio;
bsize += ldbtob(nio->nblocks);
prev = tio;
tio = nio;
/*
* This check is required to detect the case where
* we are merging adjacent buffers belonging to
* different files. fvp is used to set the b_file
* parameter in the coalesced buf. b_file is used
* by DTrace so we do not want DTrace to accrue
* requests to two different files to any one file.
*/
if (fvp && tio->bp->b_file != fvp) {
fvp = NULL;
}
nio = AVL_NEXT(&hqueue->read_tree, nio);
bufcount++;
}
/*
* tio is not removed from the read_tree as it serves as a sentinel
* to cheaply allow us to scan to the next higher numbered I/O
* request.
*/
hqueue->next = tio;
avl_remove(&hqueue->deadline_tree, tio);
mutex_exit(&hqueue->hsfs_queue_lock);
DTRACE_PROBE3(hsfs_io_dequeued, struct hio *, fio, int, bufcount,
size_t, bsize);
/*
* The benefit of coalescing occurs if the the savings in I/O outweighs
* the cost of doing the additional work below.
* It was observed that coalescing 2 buffers results in diminishing
* returns, so we do coalescing if we have >2 adjacent bufs.
*/
if (bufcount > hsched_coalesce_min) {
/*
* We have coalesced blocks. First allocate mem and buf for
* the entire coalesced chunk.
* Since we are guaranteed single-threaded here we pre-allocate
* one buf at mount time and that is re-used every time. This
* is a synthesized buf structure that uses kmem_alloced chunk.
* Not quite a normal buf attached to pages.
*/
fsp->coalesced_bytes += bsize;
nbuf = hqueue->nbuf;
bioinit(nbuf);
nbuf->b_edev = fio->bp->b_edev;
nbuf->b_dev = fio->bp->b_dev;
nbuf->b_flags = fio->bp->b_flags;
nbuf->b_iodone = fio->bp->b_iodone;
iodata = kmem_alloc(bsize, KM_SLEEP);
nbuf->b_un.b_addr = iodata;
nbuf->b_lblkno = fio->bp->b_lblkno;
nbuf->b_vp = fvp;
nbuf->b_file = fvp;
nbuf->b_bcount = bsize;
nbuf->b_bufsize = bsize;
DTRACE_PROBE3(hsfs_coalesced_io_start, struct hio *, fio, int,
bufcount, size_t, bsize);
/*
* Perform I/O for the coalesced block.
*/
(void) bdev_strategy(nbuf);
/*
* Duplicate the last IO node to leave the sentinel alone.
* The sentinel is freed in the next invocation of this
* function.
*/
prev->contig_chain = kmem_cache_alloc(hio_cache, KM_SLEEP);
prev->contig_chain->bp = tio->bp;
prev->contig_chain->sema = tio->sema;
tio = prev->contig_chain;
tio->contig_chain = NULL;
soffset = ldbtob(fio->bp->b_lblkno);
nio = fio;
bioret = biowait(nbuf);
data = bsize - nbuf->b_resid;
biofini(nbuf);
mutex_exit(&hqueue->strategy_lock);
/*
* We use the b_resid parameter to detect how much
* data was succesfully transferred. We will signal
* a success to all the fully retrieved actual bufs
* before coalescing, rest is signaled as error,
* if any.
*/
tio = nio;
DTRACE_PROBE3(hsfs_coalesced_io_done, struct hio *, nio,
int, bioret, size_t, data);
/*
* Copy data and signal success to all the bufs
* which can be fully satisfied from b_resid.
*/
while (nio != NULL && data >= nio->bp->b_bcount) {
offset = ldbtob(nio->bp->b_lblkno) - soffset;
bcopy(iodata + offset, nio->bp->b_un.b_addr,
nio->bp->b_bcount);
data -= nio->bp->b_bcount;
bioerror(nio->bp, 0);
biodone(nio->bp);
sema_v(nio->sema);
tio = nio;
nio = nio->contig_chain;
kmem_cache_free(hio_cache, tio);
}
/*
* Signal error to all the leftover bufs (if any)
* after b_resid data is exhausted.
*/
while (nio != NULL) {
nio->bp->b_resid = nio->bp->b_bcount - data;
bzero(nio->bp->b_un.b_addr + data, nio->bp->b_resid);
bioerror(nio->bp, bioret);
biodone(nio->bp);
sema_v(nio->sema);
tio = nio;
nio = nio->contig_chain;
kmem_cache_free(hio_cache, tio);
data = 0;
}
kmem_free(iodata, bsize);
} else {
nbuf = tio->bp;
io_done = tio->sema;
nio = fio;
last = tio;
while (nio != NULL) {
(void) bdev_strategy(nio->bp);
nio = nio->contig_chain;
}
nio = fio;
mutex_exit(&hqueue->strategy_lock);
while (nio != NULL) {
if (nio == last) {
(void) biowait(nbuf);
sema_v(io_done);
break;
/* sentinel last not freed. See above. */
} else {
(void) biowait(nio->bp);
sema_v(nio->sema);
}
tio = nio;
nio = nio->contig_chain;
kmem_cache_free(hio_cache, tio);
}
}
return (0);
}
/*
* Insert an I/O request in the I/O scheduler's pipeline
* Using AVL tree makes it easy to reorder the I/O request
* based on logical block number.
*/
static void
hsched_enqueue_io(struct hsfs *fsp, struct hio *hsio, int ra)
{
struct hsfs_queue *hqueue = fsp->hqueue;
mutex_enter(&hqueue->hsfs_queue_lock);
fsp->physical_read_bytes += hsio->bp->b_bcount;
if (ra)
fsp->readahead_bytes += hsio->bp->b_bcount;
avl_add(&hqueue->deadline_tree, hsio);
avl_add(&hqueue->read_tree, hsio);
DTRACE_PROBE3(hsfs_io_enqueued, struct hio *, hsio,
struct hsfs_queue *, hqueue, int, ra);
mutex_exit(&hqueue->hsfs_queue_lock);
}
/* ARGSUSED */
static int
hsfs_pathconf(struct vnode *vp, int cmd, ulong_t *valp, struct cred *cr,
caller_context_t *ct)
{
struct hsfs *fsp;
int error = 0;
switch (cmd) {
case _PC_NAME_MAX:
fsp = VFS_TO_HSFS(vp->v_vfsp);
*valp = fsp->hsfs_namemax;
break;
case _PC_FILESIZEBITS:
*valp = 33; /* Without multi extent support: 4 GB - 2k */
break;
case _PC_TIMESTAMP_RESOLUTION:
/*
* HSFS keeps, at best, 1/100 second timestamp resolution.
*/
*valp = 10000000L;
break;
default:
error = fs_pathconf(vp, cmd, valp, cr, ct);
break;
}
return (error);
}
const fs_operation_def_t hsfs_vnodeops_template[] = {
VOPNAME_OPEN, { .vop_open = hsfs_open },
VOPNAME_CLOSE, { .vop_close = hsfs_close },
VOPNAME_READ, { .vop_read = hsfs_read },
VOPNAME_GETATTR, { .vop_getattr = hsfs_getattr },
VOPNAME_ACCESS, { .vop_access = hsfs_access },
VOPNAME_LOOKUP, { .vop_lookup = hsfs_lookup },
VOPNAME_READDIR, { .vop_readdir = hsfs_readdir },
VOPNAME_READLINK, { .vop_readlink = hsfs_readlink },
VOPNAME_FSYNC, { .vop_fsync = hsfs_fsync },
VOPNAME_INACTIVE, { .vop_inactive = hsfs_inactive },
VOPNAME_FID, { .vop_fid = hsfs_fid },
VOPNAME_SEEK, { .vop_seek = hsfs_seek },
VOPNAME_FRLOCK, { .vop_frlock = hsfs_frlock },
VOPNAME_GETPAGE, { .vop_getpage = hsfs_getpage },
VOPNAME_PUTPAGE, { .vop_putpage = hsfs_putpage },
VOPNAME_MAP, { .vop_map = hsfs_map },
VOPNAME_ADDMAP, { .vop_addmap = hsfs_addmap },
VOPNAME_DELMAP, { .vop_delmap = hsfs_delmap },
VOPNAME_PATHCONF, { .vop_pathconf = hsfs_pathconf },
NULL, NULL
};
struct vnodeops *hsfs_vnodeops;
|