1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/time.h>
#include <sys/vnode.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/file.h>
#include <sys/filio.h>
#include <sys/uio.h>
#include <sys/buf.h>
#include <sys/mman.h>
#include <sys/tiuser.h>
#include <sys/pathname.h>
#include <sys/dirent.h>
#include <sys/conf.h>
#include <sys/debug.h>
#include <sys/vmsystm.h>
#include <sys/fcntl.h>
#include <sys/flock.h>
#include <sys/swap.h>
#include <sys/errno.h>
#include <sys/sysmacros.h>
#include <sys/disp.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/vtrace.h>
#include <sys/mount.h>
#include <sys/bootconf.h>
#include <sys/dnlc.h>
#include <sys/stat.h>
#include <sys/acl.h>
#include <sys/policy.h>
#include <rpc/types.h>
#include <vm/hat.h>
#include <vm/as.h>
#include <vm/page.h>
#include <vm/pvn.h>
#include <vm/seg.h>
#include <vm/seg_map.h>
#include <vm/seg_vn.h>
#include <vm/rm.h>
#include <sys/fs/cachefs_fs.h>
#include <sys/fs/cachefs_dir.h>
#include <sys/fs/cachefs_dlog.h>
#include <sys/fs/cachefs_ioctl.h>
#include <sys/fs/cachefs_log.h>
#include <fs/fs_subr.h>
int cachefs_dnlc; /* use dnlc, debugging */
static void cachefs_attr_setup(vattr_t *srcp, vattr_t *targp, cnode_t *cp,
cred_t *cr);
static void cachefs_creategid(cnode_t *dcp, cnode_t *newcp, vattr_t *vap,
cred_t *cr);
static void cachefs_createacl(cnode_t *dcp, cnode_t *newcp);
static int cachefs_getaclfromcache(cnode_t *cp, vsecattr_t *vsec);
static int cachefs_getacldirvp(cnode_t *cp);
static void cachefs_acl2perm(cnode_t *cp, vsecattr_t *vsec);
static int cachefs_access_local(void *cp, int mode, cred_t *cr);
static int cachefs_acl_access(struct cnode *cp, int mode, cred_t *cr);
static int cachefs_push_connected(vnode_t *vp, struct buf *bp, size_t iolen,
u_offset_t iooff, cred_t *cr);
static int cachefs_push_front(vnode_t *vp, struct buf *bp, size_t iolen,
u_offset_t iooff, cred_t *cr);
static int cachefs_setattr_connected(vnode_t *vp, vattr_t *vap, int flags,
cred_t *cr, caller_context_t *ct);
static int cachefs_setattr_disconnected(vnode_t *vp, vattr_t *vap,
int flags, cred_t *cr, caller_context_t *ct);
static int cachefs_access_connected(struct vnode *vp, int mode,
int flags, cred_t *cr);
static int cachefs_lookup_back(vnode_t *dvp, char *nm, vnode_t **vpp,
cred_t *cr);
static int cachefs_symlink_connected(vnode_t *dvp, char *lnm, vattr_t *tva,
char *tnm, cred_t *cr);
static int cachefs_symlink_disconnected(vnode_t *dvp, char *lnm,
vattr_t *tva, char *tnm, cred_t *cr);
static int cachefs_link_connected(vnode_t *tdvp, vnode_t *fvp, char *tnm,
cred_t *cr);
static int cachefs_link_disconnected(vnode_t *tdvp, vnode_t *fvp,
char *tnm, cred_t *cr);
static int cachefs_mkdir_connected(vnode_t *dvp, char *nm, vattr_t *vap,
vnode_t **vpp, cred_t *cr);
static int cachefs_mkdir_disconnected(vnode_t *dvp, char *nm, vattr_t *vap,
vnode_t **vpp, cred_t *cr);
static int cachefs_stickyrmchk(struct cnode *dcp, struct cnode *cp, cred_t *cr);
static int cachefs_rmdir_connected(vnode_t *dvp, char *nm,
vnode_t *cdir, cred_t *cr, vnode_t *vp);
static int cachefs_rmdir_disconnected(vnode_t *dvp, char *nm,
vnode_t *cdir, cred_t *cr, vnode_t *vp);
static char *cachefs_newname(void);
static int cachefs_remove_dolink(vnode_t *dvp, vnode_t *vp, char *nm,
cred_t *cr);
static int cachefs_rename_connected(vnode_t *odvp, char *onm,
vnode_t *ndvp, char *nnm, cred_t *cr, vnode_t *delvp);
static int cachefs_rename_disconnected(vnode_t *odvp, char *onm,
vnode_t *ndvp, char *nnm, cred_t *cr, vnode_t *delvp);
static int cachefs_readdir_connected(vnode_t *vp, uio_t *uiop, cred_t *cr,
int *eofp);
static int cachefs_readdir_disconnected(vnode_t *vp, uio_t *uiop,
cred_t *cr, int *eofp);
static int cachefs_readback_translate(cnode_t *cp, uio_t *uiop,
cred_t *cr, int *eofp);
static int cachefs_setattr_common(vnode_t *vp, vattr_t *vap, int flags,
cred_t *cr, caller_context_t *ct);
static int cachefs_open(struct vnode **, int, cred_t *,
caller_context_t *);
static int cachefs_close(struct vnode *, int, int, offset_t,
cred_t *, caller_context_t *);
static int cachefs_read(struct vnode *, struct uio *, int, cred_t *,
caller_context_t *);
static int cachefs_write(struct vnode *, struct uio *, int, cred_t *,
caller_context_t *);
static int cachefs_ioctl(struct vnode *, int, intptr_t, int, cred_t *,
int *, caller_context_t *);
static int cachefs_getattr(struct vnode *, struct vattr *, int,
cred_t *, caller_context_t *);
static int cachefs_setattr(struct vnode *, struct vattr *,
int, cred_t *, caller_context_t *);
static int cachefs_access(struct vnode *, int, int, cred_t *,
caller_context_t *);
static int cachefs_lookup(struct vnode *, char *, struct vnode **,
struct pathname *, int, struct vnode *, cred_t *,
caller_context_t *, int *, pathname_t *);
static int cachefs_create(struct vnode *, char *, struct vattr *,
enum vcexcl, int, struct vnode **, cred_t *, int,
caller_context_t *, vsecattr_t *);
static int cachefs_create_connected(vnode_t *dvp, char *nm,
vattr_t *vap, enum vcexcl exclusive, int mode,
vnode_t **vpp, cred_t *cr);
static int cachefs_create_disconnected(vnode_t *dvp, char *nm,
vattr_t *vap, enum vcexcl exclusive, int mode,
vnode_t **vpp, cred_t *cr);
static int cachefs_remove(struct vnode *, char *, cred_t *,
caller_context_t *, int);
static int cachefs_link(struct vnode *, struct vnode *, char *,
cred_t *, caller_context_t *, int);
static int cachefs_rename(struct vnode *, char *, struct vnode *,
char *, cred_t *, caller_context_t *, int);
static int cachefs_mkdir(struct vnode *, char *, struct
vattr *, struct vnode **, cred_t *, caller_context_t *,
int, vsecattr_t *);
static int cachefs_rmdir(struct vnode *, char *, struct vnode *,
cred_t *, caller_context_t *, int);
static int cachefs_readdir(struct vnode *, struct uio *,
cred_t *, int *, caller_context_t *, int);
static int cachefs_symlink(struct vnode *, char *, struct vattr *,
char *, cred_t *, caller_context_t *, int);
static int cachefs_readlink(struct vnode *, struct uio *, cred_t *,
caller_context_t *);
static int cachefs_readlink_connected(vnode_t *vp, uio_t *uiop, cred_t *cr);
static int cachefs_readlink_disconnected(vnode_t *vp, uio_t *uiop);
static int cachefs_fsync(struct vnode *, int, cred_t *,
caller_context_t *);
static void cachefs_inactive(struct vnode *, cred_t *, caller_context_t *);
static int cachefs_fid(struct vnode *, struct fid *, caller_context_t *);
static int cachefs_rwlock(struct vnode *, int, caller_context_t *);
static void cachefs_rwunlock(struct vnode *, int, caller_context_t *);
static int cachefs_seek(struct vnode *, offset_t, offset_t *,
caller_context_t *);
static int cachefs_frlock(struct vnode *, int, struct flock64 *,
int, offset_t, struct flk_callback *, cred_t *,
caller_context_t *);
static int cachefs_space(struct vnode *, int, struct flock64 *, int,
offset_t, cred_t *, caller_context_t *);
static int cachefs_realvp(struct vnode *, struct vnode **,
caller_context_t *);
static int cachefs_getpage(struct vnode *, offset_t, size_t, uint_t *,
struct page *[], size_t, struct seg *, caddr_t,
enum seg_rw, cred_t *, caller_context_t *);
static int cachefs_getapage(struct vnode *, u_offset_t, size_t, uint_t *,
struct page *[], size_t, struct seg *, caddr_t,
enum seg_rw, cred_t *);
static int cachefs_getapage_back(struct vnode *, u_offset_t, size_t,
uint_t *, struct page *[], size_t, struct seg *, caddr_t,
enum seg_rw, cred_t *);
static int cachefs_putpage(struct vnode *, offset_t, size_t, int,
cred_t *, caller_context_t *);
static int cachefs_map(struct vnode *, offset_t, struct as *,
caddr_t *, size_t, uchar_t, uchar_t, uint_t, cred_t *,
caller_context_t *);
static int cachefs_addmap(struct vnode *, offset_t, struct as *,
caddr_t, size_t, uchar_t, uchar_t, uint_t, cred_t *,
caller_context_t *);
static int cachefs_delmap(struct vnode *, offset_t, struct as *,
caddr_t, size_t, uint_t, uint_t, uint_t, cred_t *,
caller_context_t *);
static int cachefs_setsecattr(vnode_t *vp, vsecattr_t *vsec,
int flag, cred_t *cr, caller_context_t *);
static int cachefs_getsecattr(vnode_t *vp, vsecattr_t *vsec,
int flag, cred_t *cr, caller_context_t *);
static int cachefs_shrlock(vnode_t *, int, struct shrlock *, int,
cred_t *, caller_context_t *);
static int cachefs_getsecattr_connected(vnode_t *vp, vsecattr_t *vsec, int flag,
cred_t *cr);
static int cachefs_getsecattr_disconnected(vnode_t *vp, vsecattr_t *vsec,
int flag, cred_t *cr);
static int cachefs_dump(struct vnode *, caddr_t, offset_t, offset_t,
caller_context_t *);
static int cachefs_pageio(struct vnode *, page_t *,
u_offset_t, size_t, int, cred_t *, caller_context_t *);
static int cachefs_writepage(struct vnode *vp, caddr_t base,
int tcount, struct uio *uiop);
static int cachefs_pathconf(vnode_t *, int, ulong_t *, cred_t *,
caller_context_t *);
static int cachefs_read_backfs_nfsv4(vnode_t *vp, uio_t *uiop, int ioflag,
cred_t *cr, caller_context_t *ct);
static int cachefs_write_backfs_nfsv4(vnode_t *vp, uio_t *uiop, int ioflag,
cred_t *cr, caller_context_t *ct);
static int cachefs_getattr_backfs_nfsv4(vnode_t *vp, vattr_t *vap,
int flags, cred_t *cr, caller_context_t *ct);
static int cachefs_remove_backfs_nfsv4(vnode_t *dvp, char *nm, cred_t *cr,
vnode_t *vp);
static int cachefs_getpage_backfs_nfsv4(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, cred_t *cr);
static int cachefs_putpage_backfs_nfsv4(vnode_t *vp, offset_t off,
size_t len, int flags, cred_t *cr);
static int cachefs_map_backfs_nfsv4(struct vnode *vp, offset_t off,
struct as *as, caddr_t *addrp, size_t len, uchar_t prot,
uchar_t maxprot, uint_t flags, cred_t *cr);
static int cachefs_space_backfs_nfsv4(struct vnode *vp, int cmd,
struct flock64 *bfp, int flag, offset_t offset,
cred_t *cr, caller_context_t *ct);
struct vnodeops *cachefs_vnodeops;
static const fs_operation_def_t cachefs_vnodeops_template[] = {
VOPNAME_OPEN, { .vop_open = cachefs_open },
VOPNAME_CLOSE, { .vop_close = cachefs_close },
VOPNAME_READ, { .vop_read = cachefs_read },
VOPNAME_WRITE, { .vop_write = cachefs_write },
VOPNAME_IOCTL, { .vop_ioctl = cachefs_ioctl },
VOPNAME_GETATTR, { .vop_getattr = cachefs_getattr },
VOPNAME_SETATTR, { .vop_setattr = cachefs_setattr },
VOPNAME_ACCESS, { .vop_access = cachefs_access },
VOPNAME_LOOKUP, { .vop_lookup = cachefs_lookup },
VOPNAME_CREATE, { .vop_create = cachefs_create },
VOPNAME_REMOVE, { .vop_remove = cachefs_remove },
VOPNAME_LINK, { .vop_link = cachefs_link },
VOPNAME_RENAME, { .vop_rename = cachefs_rename },
VOPNAME_MKDIR, { .vop_mkdir = cachefs_mkdir },
VOPNAME_RMDIR, { .vop_rmdir = cachefs_rmdir },
VOPNAME_READDIR, { .vop_readdir = cachefs_readdir },
VOPNAME_SYMLINK, { .vop_symlink = cachefs_symlink },
VOPNAME_READLINK, { .vop_readlink = cachefs_readlink },
VOPNAME_FSYNC, { .vop_fsync = cachefs_fsync },
VOPNAME_INACTIVE, { .vop_inactive = cachefs_inactive },
VOPNAME_FID, { .vop_fid = cachefs_fid },
VOPNAME_RWLOCK, { .vop_rwlock = cachefs_rwlock },
VOPNAME_RWUNLOCK, { .vop_rwunlock = cachefs_rwunlock },
VOPNAME_SEEK, { .vop_seek = cachefs_seek },
VOPNAME_FRLOCK, { .vop_frlock = cachefs_frlock },
VOPNAME_SPACE, { .vop_space = cachefs_space },
VOPNAME_REALVP, { .vop_realvp = cachefs_realvp },
VOPNAME_GETPAGE, { .vop_getpage = cachefs_getpage },
VOPNAME_PUTPAGE, { .vop_putpage = cachefs_putpage },
VOPNAME_MAP, { .vop_map = cachefs_map },
VOPNAME_ADDMAP, { .vop_addmap = cachefs_addmap },
VOPNAME_DELMAP, { .vop_delmap = cachefs_delmap },
VOPNAME_DUMP, { .vop_dump = cachefs_dump },
VOPNAME_PATHCONF, { .vop_pathconf = cachefs_pathconf },
VOPNAME_PAGEIO, { .vop_pageio = cachefs_pageio },
VOPNAME_SETSECATTR, { .vop_setsecattr = cachefs_setsecattr },
VOPNAME_GETSECATTR, { .vop_getsecattr = cachefs_getsecattr },
VOPNAME_SHRLOCK, { .vop_shrlock = cachefs_shrlock },
NULL, NULL
};
/* forward declarations of statics */
static void cachefs_modified(cnode_t *cp);
static int cachefs_modified_alloc(cnode_t *cp);
int
cachefs_init_vnops(char *name)
{
return (vn_make_ops(name,
cachefs_vnodeops_template, &cachefs_vnodeops));
}
struct vnodeops *
cachefs_getvnodeops(void)
{
return (cachefs_vnodeops);
}
static int
cachefs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
{
int error = 0;
cnode_t *cp = VTOC(*vpp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int held = 0;
int type;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_open: ENTER vpp %p flag %x\n",
(void *)vpp, flag);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if ((flag & FWRITE) &&
((*vpp)->v_type == VDIR || (*vpp)->v_type == VLNK)) {
error = EISDIR;
goto out;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the open operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
goto out;
held = 1;
mutex_enter(&cp->c_statelock);
/* grab creds if we do not have any yet */
if (cp->c_cred == NULL) {
crhold(cr);
cp->c_cred = cr;
}
cp->c_flags |= CN_NEEDOPEN;
/* if we are disconnected */
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
/* if we cannot write to the file system */
if ((flag & FWRITE) && CFS_ISFS_WRITE_AROUND(fscp)) {
mutex_exit(&cp->c_statelock);
connected = 1;
continue;
}
/*
* Allow read only requests to continue
*/
if ((flag & (FWRITE|FREAD)) == FREAD) {
/* track the flag for opening the backvp */
cp->c_rdcnt++;
mutex_exit(&cp->c_statelock);
error = 0;
break;
}
/*
* check credentials - if this procs
* credentials don't match the creds in the
* cnode disallow writing while disconnected.
*/
if (crcmp(cp->c_cred, CRED()) != 0 &&
secpolicy_vnode_access2(CRED(), *vpp,
cp->c_attr.va_uid, 0, VWRITE) != 0) {
mutex_exit(&cp->c_statelock);
connected = 1;
continue;
}
/* to get here, we know that the WRITE flag is on */
cp->c_wrcnt++;
if (flag & FREAD)
cp->c_rdcnt++;
}
/* else if we are connected */
else {
/* if cannot use the cached copy of the file */
if ((flag & FWRITE) && CFS_ISFS_WRITE_AROUND(fscp) &&
((cp->c_flags & CN_NOCACHE) == 0))
cachefs_nocache(cp);
/* pass open to the back file */
if (cp->c_backvp) {
cp->c_flags &= ~CN_NEEDOPEN;
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_open (nfsv4): cnode %p, "
"backvp %p\n", cp, cp->c_backvp));
error = VOP_OPEN(&cp->c_backvp, flag, cr, ct);
if (CFS_TIMEOUT(fscp, error)) {
mutex_exit(&cp->c_statelock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
continue;
} else if (error) {
mutex_exit(&cp->c_statelock);
break;
}
} else {
/* backvp will be VOP_OPEN'd later */
if (flag & FREAD)
cp->c_rdcnt++;
if (flag & FWRITE)
cp->c_wrcnt++;
}
/*
* Now perform a consistency check on the file.
* If strict consistency then force a check to
* the backfs even if the timeout has not expired
* for close-to-open consistency.
*/
type = 0;
if (fscp->fs_consttype == CFS_FS_CONST_STRICT)
type = C_BACK_CHECK;
error = CFSOP_CHECK_COBJECT(fscp, cp, type, cr);
if (CFS_TIMEOUT(fscp, error)) {
mutex_exit(&cp->c_statelock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
continue;
}
}
mutex_exit(&cp->c_statelock);
break;
}
if (held)
cachefs_cd_release(fscp);
out:
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_open: EXIT vpp %p error %d\n",
(void *)vpp, error);
#endif
return (error);
}
/* ARGSUSED */
static int
cachefs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
caller_context_t *ct)
{
int error = 0;
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int held = 0;
int connected = 0;
int close_cnt = 1;
cachefscache_t *cachep;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_close: ENTER vp %p\n", (void *)vp);
#endif
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the close operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/*
* File could have been passed in or inherited from the global zone, so
* we don't want to flat out reject the request; we'll just leave things
* the way they are and let the backfs (NFS) deal with it.
*/
/* get rid of any local locks */
if (CFS_ISFS_LLOCK(fscp)) {
(void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
}
/* clean up if this is the daemon closing down */
if ((fscp->fs_cddaemonid == ttoproc(curthread)->p_pid) &&
((ttoproc(curthread)->p_pid) != 0) &&
(vp == fscp->fs_rootvp) &&
(count == 1)) {
mutex_enter(&fscp->fs_cdlock);
fscp->fs_cddaemonid = 0;
if (fscp->fs_dlogfile)
fscp->fs_cdconnected = CFS_CD_DISCONNECTED;
else
fscp->fs_cdconnected = CFS_CD_CONNECTED;
cv_broadcast(&fscp->fs_cdwaitcv);
mutex_exit(&fscp->fs_cdlock);
if (fscp->fs_flags & CFS_FS_ROOTFS) {
cachep = fscp->fs_cache;
mutex_enter(&cachep->c_contentslock);
ASSERT(cachep->c_rootdaemonid != 0);
cachep->c_rootdaemonid = 0;
mutex_exit(&cachep->c_contentslock);
}
return (0);
}
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
goto out;
held = 1;
connected = 0;
/* if not the last close */
if (count > 1) {
if (fscp->fs_cdconnected != CFS_CD_CONNECTED)
goto out;
mutex_enter(&cp->c_statelock);
if (cp->c_backvp) {
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_close (nfsv4): cnode %p, "
"backvp %p\n", cp, cp->c_backvp));
error = VOP_CLOSE(cp->c_backvp, flag, count,
offset, cr, ct);
if (CFS_TIMEOUT(fscp, error)) {
mutex_exit(&cp->c_statelock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
continue;
}
}
mutex_exit(&cp->c_statelock);
goto out;
}
/*
* If the file is an unlinked file, then flush the lookup
* cache so that inactive will be called if this is
* the last reference. It will invalidate all of the
* cached pages, without writing them out. Writing them
* out is not required because they will be written to a
* file which will be immediately removed.
*/
if (cp->c_unldvp != NULL) {
dnlc_purge_vp(vp);
mutex_enter(&cp->c_statelock);
error = cp->c_error;
cp->c_error = 0;
mutex_exit(&cp->c_statelock);
/* always call VOP_CLOSE() for back fs vnode */
}
/* force dirty data to stable storage */
else if ((vp->v_type == VREG) && (flag & FWRITE) &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
/* clean the cachefs pages synchronously */
error = cachefs_putpage_common(vp, (offset_t)0,
0, 0, cr);
if (CFS_TIMEOUT(fscp, error)) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
continue;
} else {
connected = 1;
continue;
}
}
/* if no space left in cache, wait until connected */
if ((error == ENOSPC) &&
(fscp->fs_cdconnected != CFS_CD_CONNECTED)) {
connected = 1;
continue;
}
/* clear the cnode error if putpage worked */
if ((error == 0) && cp->c_error) {
mutex_enter(&cp->c_statelock);
cp->c_error = 0;
mutex_exit(&cp->c_statelock);
}
/* if any other important error */
if (cp->c_error) {
/* get rid of the pages */
(void) cachefs_putpage_common(vp,
(offset_t)0, 0, B_INVAL | B_FORCE, cr);
dnlc_purge_vp(vp);
}
}
mutex_enter(&cp->c_statelock);
if (cp->c_backvp &&
(fscp->fs_cdconnected == CFS_CD_CONNECTED)) {
error = VOP_CLOSE(cp->c_backvp, flag, close_cnt,
offset, cr, ct);
if (CFS_TIMEOUT(fscp, error)) {
mutex_exit(&cp->c_statelock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
/* don't decrement the vnode counts again */
close_cnt = 0;
continue;
}
}
mutex_exit(&cp->c_statelock);
break;
}
mutex_enter(&cp->c_statelock);
if (!error)
error = cp->c_error;
cp->c_error = 0;
mutex_exit(&cp->c_statelock);
out:
if (held)
cachefs_cd_release(fscp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_close: EXIT vp %p\n", (void *)vp);
#endif
return (error);
}
/*ARGSUSED*/
static int
cachefs_read(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
caller_context_t *ct)
{
struct cnode *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
register u_offset_t off;
register int mapoff;
register caddr_t base;
int n;
offset_t diff;
uint_t flags = 0;
int error = 0;
#if 0
if (vp->v_flag & VNOCACHE)
flags = SM_INVAL;
#endif
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
if (vp->v_type != VREG)
return (EISDIR);
ASSERT(RW_READ_HELD(&cp->c_rwlock));
if (uiop->uio_resid == 0)
return (0);
if (uiop->uio_loffset < (offset_t)0)
return (EINVAL);
/*
* Call backfilesystem to read if NFSv4, the cachefs code
* does the read from the back filesystem asynchronously
* which is not supported by pass-through functionality.
*/
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_read_backfs_nfsv4(vp, uiop, ioflag, cr, ct);
goto out;
}
if (MANDLOCK(vp, cp->c_attr.va_mode)) {
error = chklock(vp, FREAD, (offset_t)uiop->uio_loffset,
uiop->uio_resid, uiop->uio_fmode, ct);
if (error)
return (error);
}
/*
* Sit in a loop and transfer (uiomove) the data in up to
* MAXBSIZE chunks. Each chunk is mapped into the kernel's
* address space as needed and then released.
*/
do {
/*
* off Offset of current MAXBSIZE chunk
* mapoff Offset within the current chunk
* n Number of bytes to move from this chunk
* base kernel address of mapped in chunk
*/
off = uiop->uio_loffset & (offset_t)MAXBMASK;
mapoff = uiop->uio_loffset & MAXBOFFSET;
n = MAXBSIZE - mapoff;
if (n > uiop->uio_resid)
n = (uint_t)uiop->uio_resid;
/* perform consistency check */
error = cachefs_cd_access(fscp, 0, 0);
if (error)
break;
mutex_enter(&cp->c_statelock);
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
diff = cp->c_size - uiop->uio_loffset;
mutex_exit(&cp->c_statelock);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
cachefs_cd_timedout(fscp);
error = 0;
continue;
}
cachefs_cd_release(fscp);
if (error)
break;
if (diff <= (offset_t)0)
break;
if (diff < (offset_t)n)
n = diff;
base = segmap_getmapflt(segkmap, vp, off, (uint_t)n, 1, S_READ);
error = segmap_fault(kas.a_hat, segkmap, base, n,
F_SOFTLOCK, S_READ);
if (error) {
(void) segmap_release(segkmap, base, 0);
if (FC_CODE(error) == FC_OBJERR)
error = FC_ERRNO(error);
else
error = EIO;
break;
}
error = uiomove(base+mapoff, n, UIO_READ, uiop);
(void) segmap_fault(kas.a_hat, segkmap, base, n,
F_SOFTUNLOCK, S_READ);
if (error == 0) {
/*
* if we read a whole page(s), or to eof,
* we won't need this page(s) again soon.
*/
if (n + mapoff == MAXBSIZE ||
uiop->uio_loffset == cp->c_size)
flags |= SM_DONTNEED;
}
(void) segmap_release(segkmap, base, flags);
} while (error == 0 && uiop->uio_resid > 0);
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_read: EXIT error %d resid %ld\n", error,
uiop->uio_resid);
#endif
return (error);
}
/*
* cachefs_read_backfs_nfsv4
*
* Call NFSv4 back filesystem to handle the read (cachefs
* pass-through support for NFSv4).
*/
static int
cachefs_read_backfs_nfsv4(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
vnode_t *backvp;
int error;
/*
* For NFSv4 pass-through to work, only connected operation
* is supported, the cnode backvp must exist, and cachefs
* optional (eg., disconnectable) flags are turned off. Assert
* these conditions for the read operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* Call backfs vnode op after extracting backvp */
mutex_enter(&cp->c_statelock);
backvp = cp->c_backvp;
mutex_exit(&cp->c_statelock);
CFS_DPRINT_BACKFS_NFSV4(fscp, ("cachefs_read_backfs_nfsv4: cnode %p, "
"backvp %p\n", cp, backvp));
(void) VOP_RWLOCK(backvp, V_WRITELOCK_FALSE, ct);
error = VOP_READ(backvp, uiop, ioflag, cr, ct);
VOP_RWUNLOCK(backvp, V_WRITELOCK_FALSE, ct);
/* Increment cache miss counter */
fscp->fs_stats.st_misses++;
return (error);
}
/*ARGSUSED*/
static int
cachefs_write(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
caller_context_t *ct)
{
struct cnode *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error = 0;
u_offset_t off;
caddr_t base;
uint_t bsize;
uint_t flags;
int n, on;
rlim64_t limit = uiop->uio_llimit;
ssize_t resid;
offset_t offset;
offset_t remainder;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf(
"cachefs_write: ENTER vp %p offset %llu count %ld cflags %x\n",
(void *)vp, uiop->uio_loffset, uiop->uio_resid,
cp->c_flags);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (vp->v_type != VREG) {
error = EISDIR;
goto out;
}
ASSERT(RW_WRITE_HELD(&cp->c_rwlock));
if (uiop->uio_resid == 0) {
goto out;
}
/* Call backfilesystem to write if NFSv4 */
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_write_backfs_nfsv4(vp, uiop, ioflag, cr, ct);
goto out2;
}
if (MANDLOCK(vp, cp->c_attr.va_mode)) {
error = chklock(vp, FWRITE, (offset_t)uiop->uio_loffset,
uiop->uio_resid, uiop->uio_fmode, ct);
if (error)
goto out;
}
if (ioflag & FAPPEND) {
for (;;) {
/* do consistency check to get correct file size */
error = cachefs_cd_access(fscp, 0, 1);
if (error)
goto out;
mutex_enter(&cp->c_statelock);
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
uiop->uio_loffset = cp->c_size;
mutex_exit(&cp->c_statelock);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
cachefs_cd_timedout(fscp);
continue;
}
cachefs_cd_release(fscp);
if (error)
goto out;
break;
}
}
if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
limit = MAXOFFSET_T;
if (uiop->uio_loffset >= limit) {
proc_t *p = ttoproc(curthread);
mutex_enter(&p->p_lock);
(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
p, RCA_UNSAFE_SIGINFO);
mutex_exit(&p->p_lock);
error = EFBIG;
goto out;
}
if (uiop->uio_loffset > fscp->fs_offmax) {
error = EFBIG;
goto out;
}
if (limit > fscp->fs_offmax)
limit = fscp->fs_offmax;
if (uiop->uio_loffset < (offset_t)0) {
error = EINVAL;
goto out;
}
offset = uiop->uio_loffset + uiop->uio_resid;
/*
* Check to make sure that the process will not exceed
* its limit on file size. It is okay to write up to
* the limit, but not beyond. Thus, the write which
* reaches the limit will be short and the next write
* will return an error.
*/
remainder = 0;
if (offset > limit) {
remainder = (int)(offset - (u_offset_t)limit);
uiop->uio_resid = limit - uiop->uio_loffset;
if (uiop->uio_resid <= 0) {
proc_t *p = ttoproc(curthread);
uiop->uio_resid += remainder;
mutex_enter(&p->p_lock);
(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
p->p_rctls, p, RCA_UNSAFE_SIGINFO);
mutex_exit(&p->p_lock);
error = EFBIG;
goto out;
}
}
resid = uiop->uio_resid;
offset = uiop->uio_loffset;
bsize = vp->v_vfsp->vfs_bsize;
/* loop around and do the write in MAXBSIZE chunks */
do {
/* mapping offset */
off = uiop->uio_loffset & (offset_t)MAXBMASK;
on = uiop->uio_loffset & MAXBOFFSET; /* Rel. offset */
n = MAXBSIZE - on;
if (n > uiop->uio_resid)
n = (int)uiop->uio_resid;
/*
* Touch the page and fault it in if it is not in
* core before segmap_getmapflt can lock it. This
* is to avoid the deadlock if the buffer is mapped
* to the same file through mmap which we want to
* write to.
*/
uio_prefaultpages((long)n, uiop);
base = segmap_getmap(segkmap, vp, off);
error = cachefs_writepage(vp, (base + on), n, uiop);
if (error == 0) {
flags = 0;
/*
* Have written a whole block.Start an
* asynchronous write and mark the buffer to
* indicate that it won't be needed again
* soon.
*/
if (n + on == bsize) {
flags = SM_WRITE |SM_ASYNC |SM_DONTNEED;
}
#if 0
/* XXX need to understand this */
if ((ioflag & (FSYNC|FDSYNC)) ||
(cp->c_backvp && vn_has_flocks(cp->c_backvp))) {
flags &= ~SM_ASYNC;
flags |= SM_WRITE;
}
#else
if (ioflag & (FSYNC|FDSYNC)) {
flags &= ~SM_ASYNC;
flags |= SM_WRITE;
}
#endif
error = segmap_release(segkmap, base, flags);
} else {
(void) segmap_release(segkmap, base, 0);
}
} while (error == 0 && uiop->uio_resid > 0);
out:
if (error == EINTR && (ioflag & (FSYNC|FDSYNC))) {
uiop->uio_resid = resid;
uiop->uio_loffset = offset;
} else
uiop->uio_resid += remainder;
out2:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_write: EXIT error %d\n", error);
#endif
return (error);
}
/*
* cachefs_write_backfs_nfsv4
*
* Call NFSv4 back filesystem to handle the write (cachefs
* pass-through support for NFSv4).
*/
static int
cachefs_write_backfs_nfsv4(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
vnode_t *backvp;
int error;
/*
* For NFSv4 pass-through to work, only connected operation
* is supported, the cnode backvp must exist, and cachefs
* optional (eg., disconnectable) flags are turned off. Assert
* these conditions for the read operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* Call backfs vnode op after extracting the backvp */
mutex_enter(&cp->c_statelock);
backvp = cp->c_backvp;
mutex_exit(&cp->c_statelock);
CFS_DPRINT_BACKFS_NFSV4(fscp, ("cachefs_write_backfs_nfsv4: cnode %p, "
"backvp %p\n", cp, backvp));
(void) VOP_RWLOCK(backvp, V_WRITELOCK_TRUE, ct);
error = VOP_WRITE(backvp, uiop, ioflag, cr, ct);
VOP_RWUNLOCK(backvp, V_WRITELOCK_TRUE, ct);
return (error);
}
/*
* see if we've charged ourselves for frontfile data at
* the given offset. If not, allocate a block for it now.
*/
static int
cachefs_charge_page(struct cnode *cp, u_offset_t offset)
{
u_offset_t blockoff;
int error;
int inc;
ASSERT(MUTEX_HELD(&cp->c_statelock));
/*LINTED*/
ASSERT(PAGESIZE <= MAXBSIZE);
error = 0;
blockoff = offset & (offset_t)MAXBMASK;
/* get the front file if necessary so allocblocks works */
if ((cp->c_frontvp == NULL) &&
((cp->c_flags & CN_NOCACHE) == 0)) {
(void) cachefs_getfrontfile(cp);
}
if (cp->c_flags & CN_NOCACHE)
return (1);
if (cachefs_check_allocmap(cp, blockoff))
return (0);
for (inc = PAGESIZE; inc < MAXBSIZE; inc += PAGESIZE)
if (cachefs_check_allocmap(cp, blockoff+inc))
return (0);
error = cachefs_allocblocks(C_TO_FSCACHE(cp)->fs_cache, 1,
cp->c_metadata.md_rltype);
if (error == 0) {
cp->c_metadata.md_frontblks++;
cp->c_flags |= CN_UPDATED;
}
return (error);
}
/*
* Called only by cachefs_write to write 1 page or less of data.
* base - base address kernel addr space
* tcount - Total bytes to move - < MAXBSIZE
*/
static int
cachefs_writepage(vnode_t *vp, caddr_t base, int tcount, uio_t *uiop)
{
struct cnode *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
register int n;
register u_offset_t offset;
int error = 0, terror;
extern struct as kas;
u_offset_t lastpage_off;
int pagecreate = 0;
int newpage;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf(
"cachefs_writepage: ENTER vp %p offset %llu len %ld\\\n",
(void *)vp, uiop->uio_loffset, uiop->uio_resid);
#endif
/*
* Move bytes in PAGESIZE chunks. We must avoid spanning pages in
* uiomove() because page faults may cause the cache to be invalidated
* out from under us.
*/
do {
offset = uiop->uio_loffset;
lastpage_off = (cp->c_size - 1) & (offset_t)PAGEMASK;
/*
* If not connected then need to make sure we have space
* to perform the write. We could make this check
* a little tighter by only doing it if we are growing the file.
*/
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
error = cachefs_allocblocks(fscp->fs_cache, 1,
cp->c_metadata.md_rltype);
if (error)
break;
cachefs_freeblocks(fscp->fs_cache, 1,
cp->c_metadata.md_rltype);
}
/*
* n is the number of bytes required to satisfy the request
* or the number of bytes to fill out the page.
*/
n = (int)(PAGESIZE - ((uintptr_t)base & PAGEOFFSET));
if (n > tcount)
n = tcount;
/*
* The number of bytes of data in the last page can not
* be accurately be determined while page is being
* uiomove'd to and the size of the file being updated.
* Thus, inform threads which need to know accurately
* how much data is in the last page of the file. They
* will not do the i/o immediately, but will arrange for
* the i/o to happen later when this modify operation
* will have finished.
*
* in similar NFS code, this is done right before the
* uiomove(), which is best. but here in cachefs, we
* have two uiomove()s, so we must do it here.
*/
ASSERT(!(cp->c_flags & CN_CMODINPROG));
mutex_enter(&cp->c_statelock);
cp->c_flags |= CN_CMODINPROG;
cp->c_modaddr = (offset & (offset_t)MAXBMASK);
mutex_exit(&cp->c_statelock);
/*
* Check to see if we can skip reading in the page
* and just allocate the memory. We can do this
* if we are going to rewrite the entire mapping
* or if we are going to write to or beyond the current
* end of file from the beginning of the mapping.
*/
if ((offset > (lastpage_off + PAGEOFFSET)) ||
((cp->c_size == 0) && (offset < PAGESIZE)) ||
((uintptr_t)base & PAGEOFFSET) == 0 && (n == PAGESIZE ||
((offset + n) >= cp->c_size))) {
pagecreate = 1;
/*
* segmap_pagecreate() returns 1 if it calls
* page_create_va() to allocate any pages.
*/
newpage = segmap_pagecreate(segkmap,
(caddr_t)((uintptr_t)base & (uintptr_t)PAGEMASK),
PAGESIZE, 0);
/* do not zero page if we are overwriting all of it */
if (!((((uintptr_t)base & PAGEOFFSET) == 0) &&
(n == PAGESIZE))) {
(void) kzero((void *)
((uintptr_t)base & (uintptr_t)PAGEMASK),
PAGESIZE);
}
error = uiomove(base, n, UIO_WRITE, uiop);
/*
* Unlock the page allocated by page_create_va()
* in segmap_pagecreate()
*/
if (newpage)
segmap_pageunlock(segkmap,
(caddr_t)((uintptr_t)base &
(uintptr_t)PAGEMASK),
PAGESIZE, S_WRITE);
} else {
/*
* KLUDGE ! Use segmap_fault instead of faulting and
* using as_fault() to avoid a recursive readers lock
* on kas.
*/
error = segmap_fault(kas.a_hat, segkmap, (caddr_t)
((uintptr_t)base & (uintptr_t)PAGEMASK),
PAGESIZE, F_SOFTLOCK, S_WRITE);
if (error) {
if (FC_CODE(error) == FC_OBJERR)
error = FC_ERRNO(error);
else
error = EIO;
break;
}
error = uiomove(base, n, UIO_WRITE, uiop);
(void) segmap_fault(kas.a_hat, segkmap, (caddr_t)
((uintptr_t)base & (uintptr_t)PAGEMASK),
PAGESIZE, F_SOFTUNLOCK, S_WRITE);
}
n = (int)(uiop->uio_loffset - offset); /* n = # bytes written */
base += n;
tcount -= n;
/* get access to the file system */
if ((terror = cachefs_cd_access(fscp, 0, 1)) != 0) {
error = terror;
break;
}
/*
* cp->c_attr.va_size is the maximum number of
* bytes known to be in the file.
* Make sure it is at least as high as the
* last byte we just wrote into the buffer.
*/
mutex_enter(&cp->c_statelock);
if (cp->c_size < uiop->uio_loffset) {
cp->c_size = uiop->uio_loffset;
}
if (cp->c_size != cp->c_attr.va_size) {
cp->c_attr.va_size = cp->c_size;
cp->c_flags |= CN_UPDATED;
}
/* c_size is now correct, so we can clear modinprog */
cp->c_flags &= ~CN_CMODINPROG;
if (error == 0) {
cp->c_flags |= CDIRTY;
if (pagecreate && (cp->c_flags & CN_NOCACHE) == 0) {
/*
* if we're not in NOCACHE mode
* (i.e., single-writer), we update the
* allocmap here rather than waiting until
* cachefspush is called. This prevents
* getpage from clustering up pages from
* the backfile and stomping over the changes
* we make here.
*/
if (cachefs_charge_page(cp, offset) == 0) {
cachefs_update_allocmap(cp,
offset & (offset_t)PAGEMASK,
(size_t)PAGESIZE);
}
/* else we ran out of space */
else {
/* nocache file if connected */
if (fscp->fs_cdconnected ==
CFS_CD_CONNECTED)
cachefs_nocache(cp);
/*
* If disconnected then cannot
* nocache the file. Let it have
* the space.
*/
else {
cp->c_metadata.md_frontblks++;
cp->c_flags |= CN_UPDATED;
cachefs_update_allocmap(cp,
offset & (offset_t)PAGEMASK,
(size_t)PAGESIZE);
}
}
}
}
mutex_exit(&cp->c_statelock);
cachefs_cd_release(fscp);
} while (tcount > 0 && error == 0);
if (cp->c_flags & CN_CMODINPROG) {
/* XXX assert error != 0? FC_ERRNO() makes this more risky. */
mutex_enter(&cp->c_statelock);
cp->c_flags &= ~CN_CMODINPROG;
mutex_exit(&cp->c_statelock);
}
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_writepage: EXIT error %d\n", error);
#endif
return (error);
}
/*
* Pushes out pages to the back and/or front file system.
*/
static int
cachefs_push(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
int flags, cred_t *cr)
{
struct cnode *cp = VTOC(vp);
struct buf *bp;
int error;
fscache_t *fscp = C_TO_FSCACHE(cp);
u_offset_t iooff;
size_t iolen;
u_offset_t lbn;
u_offset_t lbn_off;
uint_t bsize;
ASSERT((flags & B_ASYNC) == 0);
ASSERT(!vn_is_readonly(vp));
ASSERT(pp != NULL);
ASSERT(cr != NULL);
bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
lbn = pp->p_offset / bsize;
lbn_off = lbn * bsize;
/*
* Find a kluster that fits in one block, or in
* one page if pages are bigger than blocks. If
* there is less file space allocated than a whole
* page, we'll shorten the i/o request below.
*/
pp = pvn_write_kluster(vp, pp, &iooff, &iolen, lbn_off,
roundup(bsize, PAGESIZE), flags);
/*
* The CN_CMODINPROG flag makes sure that we use a correct
* value of c_size, below. CN_CMODINPROG is set in
* cachefs_writepage(). When CN_CMODINPROG is set it
* indicates that a uiomove() is in progress and the c_size
* has not been made consistent with the new size of the
* file. When the uiomove() completes the c_size is updated
* and the CN_CMODINPROG flag is cleared.
*
* The CN_CMODINPROG flag makes sure that cachefs_push_front
* and cachefs_push_connected see a consistent value of
* c_size. Without this handshaking, it is possible that
* these routines will pick up the old value of c_size before
* the uiomove() in cachefs_writepage() completes. This will
* result in the vn_rdwr() being too small, and data loss.
*
* More precisely, there is a window between the time the
* uiomove() completes and the time the c_size is updated. If
* a VOP_PUTPAGE() operation intervenes in this window, the
* page will be picked up, because it is dirty; it will be
* unlocked, unless it was pagecreate'd. When the page is
* picked up as dirty, the dirty bit is reset
* (pvn_getdirty()). In cachefs_push_connected(), c_size is
* checked. This will still be the old size. Therefore, the
* page will not be written out to the correct length, and the
* page will be clean, so the data may disappear.
*/
if (cp->c_flags & CN_CMODINPROG) {
mutex_enter(&cp->c_statelock);
if ((cp->c_flags & CN_CMODINPROG) &&
cp->c_modaddr + MAXBSIZE > iooff &&
cp->c_modaddr < iooff + iolen) {
page_t *plist;
/*
* A write is in progress for this region of
* the file. If we did not detect
* CN_CMODINPROG here then this path through
* cachefs_push_connected() would eventually
* do the vn_rdwr() and may not write out all
* of the data in the pages. We end up losing
* data. So we decide to set the modified bit
* on each page in the page list and mark the
* cnode with CDIRTY. This push will be
* restarted at some later time.
*/
plist = pp;
while (plist != NULL) {
pp = plist;
page_sub(&plist, pp);
hat_setmod(pp);
page_io_unlock(pp);
page_unlock(pp);
}
cp->c_flags |= CDIRTY;
mutex_exit(&cp->c_statelock);
if (offp)
*offp = iooff;
if (lenp)
*lenp = iolen;
return (0);
}
mutex_exit(&cp->c_statelock);
}
/*
* Set the pages up for pageout.
*/
bp = pageio_setup(pp, iolen, CTOV(cp), B_WRITE | flags);
if (bp == NULL) {
/*
* currently, there is no way for pageio_setup() to
* return NULL, since it uses its own scheme for
* kmem_alloc()ing that shouldn't return NULL, and
* since pageio_setup() itself dereferences the thing
* it's about to return. still, we need to be ready
* in case this ever does start happening.
*/
error = ENOMEM;
goto writedone;
}
/*
* pageio_setup should have set b_addr to 0. This
* is correct since we want to do I/O on a page
* boundary. bp_mapin will use this addr to calculate
* an offset, and then set b_addr to the kernel virtual
* address it allocated for us.
*/
bp->b_edev = 0;
bp->b_dev = 0;
bp->b_lblkno = (diskaddr_t)lbtodb(iooff);
bp_mapin(bp);
iolen = cp->c_size - ldbtob(bp->b_blkno);
if (iolen > bp->b_bcount)
iolen = bp->b_bcount;
/* if connected */
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
/* write to the back file first */
error = cachefs_push_connected(vp, bp, iolen, iooff, cr);
/* write to the front file if allowed */
if ((error == 0) && CFS_ISFS_NONSHARED(fscp) &&
((cp->c_flags & CN_NOCACHE) == 0)) {
/* try to write to the front file */
(void) cachefs_push_front(vp, bp, iolen, iooff, cr);
}
}
/* else if disconnected */
else {
/* try to write to the front file */
error = cachefs_push_front(vp, bp, iolen, iooff, cr);
}
bp_mapout(bp);
pageio_done(bp);
writedone:
pvn_write_done(pp, ((error) ? B_ERROR : 0) | B_WRITE | flags);
if (offp)
*offp = iooff;
if (lenp)
*lenp = iolen;
/* XXX ask bob mastors how to fix this someday */
mutex_enter(&cp->c_statelock);
if (error) {
if (error == ENOSPC) {
if ((fscp->fs_cdconnected == CFS_CD_CONNECTED) ||
CFS_ISFS_SOFT(fscp)) {
CFSOP_INVALIDATE_COBJECT(fscp, cp, cr);
cp->c_error = error;
}
} else if ((CFS_TIMEOUT(fscp, error) == 0) &&
(error != EINTR)) {
CFSOP_INVALIDATE_COBJECT(fscp, cp, cr);
cp->c_error = error;
}
} else if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
CFSOP_MODIFY_COBJECT(fscp, cp, cr);
}
mutex_exit(&cp->c_statelock);
return (error);
}
/*
* Pushes out pages to the back file system.
*/
static int
cachefs_push_connected(vnode_t *vp, struct buf *bp, size_t iolen,
u_offset_t iooff, cred_t *cr)
{
struct cnode *cp = VTOC(vp);
int error = 0;
int mode = 0;
fscache_t *fscp = C_TO_FSCACHE(cp);
ssize_t resid;
vnode_t *backvp;
/* get the back file if necessary */
mutex_enter(&cp->c_statelock);
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error) {
mutex_exit(&cp->c_statelock);
goto out;
}
}
backvp = cp->c_backvp;
VN_HOLD(backvp);
mutex_exit(&cp->c_statelock);
if (CFS_ISFS_NONSHARED(fscp) && CFS_ISFS_SNR(fscp))
mode = FSYNC;
/* write to the back file */
error = bp->b_error = vn_rdwr(UIO_WRITE, backvp, bp->b_un.b_addr,
iolen, iooff, UIO_SYSSPACE, mode,
RLIM64_INFINITY, cr, &resid);
if (error) {
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS | CFSDEBUG_BACK)
printf("cachefspush: error %d cr %p\n",
error, (void *)cr);
#endif
bp->b_flags |= B_ERROR;
}
VN_RELE(backvp);
out:
return (error);
}
/*
* Pushes out pages to the front file system.
* Called for both connected and disconnected states.
*/
static int
cachefs_push_front(vnode_t *vp, struct buf *bp, size_t iolen,
u_offset_t iooff, cred_t *cr)
{
struct cnode *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error = 0;
ssize_t resid;
u_offset_t popoff;
off_t commit = 0;
uint_t seq;
enum cachefs_rl_type type;
vnode_t *frontvp = NULL;
mutex_enter(&cp->c_statelock);
if (!CFS_ISFS_NONSHARED(fscp)) {
error = ETIMEDOUT;
goto out;
}
/* get the front file if necessary */
if ((cp->c_frontvp == NULL) &&
((cp->c_flags & CN_NOCACHE) == 0)) {
(void) cachefs_getfrontfile(cp);
}
if (cp->c_flags & CN_NOCACHE) {
error = ETIMEDOUT;
goto out;
}
/* if disconnected, needs to be populated and have good attributes */
if ((fscp->fs_cdconnected != CFS_CD_CONNECTED) &&
(((cp->c_metadata.md_flags & MD_POPULATED) == 0) ||
(cp->c_metadata.md_flags & MD_NEEDATTRS))) {
error = ETIMEDOUT;
goto out;
}
for (popoff = iooff; popoff < (iooff + iolen); popoff += MAXBSIZE) {
if (cachefs_charge_page(cp, popoff)) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
cachefs_nocache(cp);
goto out;
} else {
error = ENOSPC;
goto out;
}
}
}
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
/* log the first putpage to a file */
if ((cp->c_metadata.md_flags & MD_PUTPAGE) == 0) {
/* uses open's creds if we have them */
if (cp->c_cred)
cr = cp->c_cred;
if ((cp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
error = ENOSPC;
goto out;
}
cp->c_metadata.md_flags |= MD_MAPPING;
}
commit = cachefs_dlog_modify(fscp, cp, cr, &seq);
if (commit == 0) {
/* out of space */
error = ENOSPC;
goto out;
}
cp->c_metadata.md_seq = seq;
type = cp->c_metadata.md_rltype;
cachefs_modified(cp);
cp->c_metadata.md_flags |= MD_PUTPAGE;
cp->c_metadata.md_flags &= ~MD_PUSHDONE;
cp->c_flags |= CN_UPDATED;
}
/* subsequent putpages just get a new sequence number */
else {
/* but only if it matters */
if (cp->c_metadata.md_seq != fscp->fs_dlogseq) {
seq = cachefs_dlog_seqnext(fscp);
if (seq == 0) {
error = ENOSPC;
goto out;
}
cp->c_metadata.md_seq = seq;
cp->c_flags |= CN_UPDATED;
/* XXX maybe should do write_metadata here */
}
}
}
frontvp = cp->c_frontvp;
VN_HOLD(frontvp);
mutex_exit(&cp->c_statelock);
error = bp->b_error = vn_rdwr(UIO_WRITE, frontvp,
bp->b_un.b_addr, iolen, iooff, UIO_SYSSPACE, 0,
RLIM64_INFINITY, kcred, &resid);
mutex_enter(&cp->c_statelock);
VN_RELE(frontvp);
frontvp = NULL;
if (error) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
cachefs_nocache(cp);
error = 0;
goto out;
} else {
goto out;
}
}
(void) cachefs_update_allocmap(cp, iooff, iolen);
cp->c_flags |= (CN_UPDATED | CN_NEED_FRONT_SYNC |
CN_POPULATION_PENDING);
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
gethrestime(&cp->c_metadata.md_localmtime);
cp->c_metadata.md_flags |= MD_LOCALMTIME;
}
out:
if (commit) {
/* commit the log record */
ASSERT(fscp->fs_cdconnected == CFS_CD_DISCONNECTED);
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX fix on panic */
}
}
if (error && commit) {
cp->c_metadata.md_flags &= ~MD_PUTPAGE;
cachefs_rlent_moveto(fscp->fs_cache, type,
cp->c_metadata.md_rlno, cp->c_metadata.md_frontblks);
cp->c_metadata.md_rltype = type;
cp->c_flags |= CN_UPDATED;
}
mutex_exit(&cp->c_statelock);
return (error);
}
/*ARGSUSED*/
static int
cachefs_dump(struct vnode *vp, caddr_t foo1, offset_t foo2, offset_t foo3,
caller_context_t *ct)
{
return (ENOSYS); /* should we panic if we get here? */
}
/*ARGSUSED*/
static int
cachefs_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, cred_t *cred,
int *rvalp, caller_context_t *ct)
{
int error;
struct cnode *cp = VTOC(vp);
struct fscache *fscp = C_TO_FSCACHE(cp);
struct cachefscache *cachep;
extern kmutex_t cachefs_cachelock;
extern cachefscache_t *cachefs_cachelist;
cachefsio_pack_t *packp;
STRUCT_DECL(cachefsio_dcmd, dcmd);
int inlen, outlen; /* LP64: generic int for struct in/out len */
void *dinp, *doutp;
int (*dcmd_routine)(vnode_t *, void *, void *);
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions which ensure
* that only a subset of the ioctls are "truly supported"
* for NFSv4 (these are CFSDCMD_DAEMONID and CFSDCMD_GETSTATS.
* The packing operations are meaningless since there is
* no caching for NFSv4, and the called functions silently
* return if the backfilesystem is NFSv4. The daemon
* commands except for those above are essentially used
* for disconnectable operation support (including log
* rolling), so in each called function, we assert that
* NFSv4 is not in use. The _FIO* calls (except _FIOCOD)
* are from "cfsfstype" which is not a documented
* command. However, the command is visible in
* /usr/lib/fs/cachefs so the commands are simply let
* through (don't seem to impact pass-through functionality).
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
switch (cmd) {
case CACHEFSIO_PACK:
packp = cachefs_kmem_alloc(sizeof (cachefsio_pack_t), KM_SLEEP);
error = xcopyin((void *)arg, packp, sizeof (cachefsio_pack_t));
if (!error)
error = cachefs_pack(vp, packp->p_name, cred);
cachefs_kmem_free(packp, sizeof (cachefsio_pack_t));
break;
case CACHEFSIO_UNPACK:
packp = cachefs_kmem_alloc(sizeof (cachefsio_pack_t), KM_SLEEP);
error = xcopyin((void *)arg, packp, sizeof (cachefsio_pack_t));
if (!error)
error = cachefs_unpack(vp, packp->p_name, cred);
cachefs_kmem_free(packp, sizeof (cachefsio_pack_t));
break;
case CACHEFSIO_PACKINFO:
packp = cachefs_kmem_alloc(sizeof (cachefsio_pack_t), KM_SLEEP);
error = xcopyin((void *)arg, packp, sizeof (cachefsio_pack_t));
if (!error)
error = cachefs_packinfo(vp, packp->p_name,
&packp->p_status, cred);
if (!error)
error = xcopyout(packp, (void *)arg,
sizeof (cachefsio_pack_t));
cachefs_kmem_free(packp, sizeof (cachefsio_pack_t));
break;
case CACHEFSIO_UNPACKALL:
error = cachefs_unpackall(vp);
break;
case CACHEFSIO_DCMD:
/*
* This is a private interface between the cachefsd and
* this file system.
*/
/* must be root to use these commands */
if (secpolicy_fs_config(cred, vp->v_vfsp) != 0)
return (EPERM);
/* get the command packet */
STRUCT_INIT(dcmd, flag & DATAMODEL_MASK);
error = xcopyin((void *)arg, STRUCT_BUF(dcmd),
SIZEOF_STRUCT(cachefsio_dcmd, DATAMODEL_NATIVE));
if (error)
return (error);
/* copy in the data for the operation */
dinp = NULL;
if ((inlen = STRUCT_FGET(dcmd, d_slen)) > 0) {
dinp = cachefs_kmem_alloc(inlen, KM_SLEEP);
error = xcopyin(STRUCT_FGETP(dcmd, d_sdata), dinp,
inlen);
if (error)
return (error);
}
/* allocate space for the result */
doutp = NULL;
if ((outlen = STRUCT_FGET(dcmd, d_rlen)) > 0)
doutp = cachefs_kmem_alloc(outlen, KM_SLEEP);
/*
* Assert NFSv4 only allows the daemonid and getstats
* daemon requests
*/
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0 ||
STRUCT_FGET(dcmd, d_cmd) == CFSDCMD_DAEMONID ||
STRUCT_FGET(dcmd, d_cmd) == CFSDCMD_GETSTATS);
/* get the routine to execute */
dcmd_routine = NULL;
switch (STRUCT_FGET(dcmd, d_cmd)) {
case CFSDCMD_DAEMONID:
dcmd_routine = cachefs_io_daemonid;
break;
case CFSDCMD_STATEGET:
dcmd_routine = cachefs_io_stateget;
break;
case CFSDCMD_STATESET:
dcmd_routine = cachefs_io_stateset;
break;
case CFSDCMD_XWAIT:
dcmd_routine = cachefs_io_xwait;
break;
case CFSDCMD_EXISTS:
dcmd_routine = cachefs_io_exists;
break;
case CFSDCMD_LOSTFOUND:
dcmd_routine = cachefs_io_lostfound;
break;
case CFSDCMD_GETINFO:
dcmd_routine = cachefs_io_getinfo;
break;
case CFSDCMD_CIDTOFID:
dcmd_routine = cachefs_io_cidtofid;
break;
case CFSDCMD_GETATTRFID:
dcmd_routine = cachefs_io_getattrfid;
break;
case CFSDCMD_GETATTRNAME:
dcmd_routine = cachefs_io_getattrname;
break;
case CFSDCMD_GETSTATS:
dcmd_routine = cachefs_io_getstats;
break;
case CFSDCMD_ROOTFID:
dcmd_routine = cachefs_io_rootfid;
break;
case CFSDCMD_CREATE:
dcmd_routine = cachefs_io_create;
break;
case CFSDCMD_REMOVE:
dcmd_routine = cachefs_io_remove;
break;
case CFSDCMD_LINK:
dcmd_routine = cachefs_io_link;
break;
case CFSDCMD_RENAME:
dcmd_routine = cachefs_io_rename;
break;
case CFSDCMD_MKDIR:
dcmd_routine = cachefs_io_mkdir;
break;
case CFSDCMD_RMDIR:
dcmd_routine = cachefs_io_rmdir;
break;
case CFSDCMD_SYMLINK:
dcmd_routine = cachefs_io_symlink;
break;
case CFSDCMD_SETATTR:
dcmd_routine = cachefs_io_setattr;
break;
case CFSDCMD_SETSECATTR:
dcmd_routine = cachefs_io_setsecattr;
break;
case CFSDCMD_PUSHBACK:
dcmd_routine = cachefs_io_pushback;
break;
default:
error = ENOTTY;
break;
}
/* execute the routine */
if (dcmd_routine)
error = (*dcmd_routine)(vp, dinp, doutp);
/* copy out the result */
if ((error == 0) && doutp)
error = xcopyout(doutp, STRUCT_FGETP(dcmd, d_rdata),
outlen);
/* free allocated memory */
if (dinp)
cachefs_kmem_free(dinp, inlen);
if (doutp)
cachefs_kmem_free(doutp, outlen);
break;
case _FIOCOD:
if (secpolicy_fs_config(cred, vp->v_vfsp) != 0) {
error = EPERM;
break;
}
error = EBUSY;
if (arg) {
/* non-zero arg means do all filesystems */
mutex_enter(&cachefs_cachelock);
for (cachep = cachefs_cachelist; cachep != NULL;
cachep = cachep->c_next) {
mutex_enter(&cachep->c_fslistlock);
for (fscp = cachep->c_fslist;
fscp != NULL;
fscp = fscp->fs_next) {
if (CFS_ISFS_CODCONST(fscp)) {
gethrestime(&fscp->fs_cod_time);
error = 0;
}
}
mutex_exit(&cachep->c_fslistlock);
}
mutex_exit(&cachefs_cachelock);
} else {
if (CFS_ISFS_CODCONST(fscp)) {
gethrestime(&fscp->fs_cod_time);
error = 0;
}
}
break;
case _FIOSTOPCACHE:
error = cachefs_stop_cache(cp);
break;
default:
error = ENOTTY;
break;
}
/* return the result */
return (error);
}
ino64_t
cachefs_fileno_conflict(fscache_t *fscp, ino64_t old)
{
ino64_t new;
ASSERT(MUTEX_HELD(&fscp->fs_fslock));
for (;;) {
fscp->fs_info.fi_localfileno++;
if (fscp->fs_info.fi_localfileno == 0)
fscp->fs_info.fi_localfileno = 3;
fscp->fs_flags |= CFS_FS_DIRTYINFO;
new = fscp->fs_info.fi_localfileno;
if (! cachefs_fileno_inuse(fscp, new))
break;
}
cachefs_inum_register(fscp, old, new);
cachefs_inum_register(fscp, new, 0);
return (new);
}
/*ARGSUSED*/
static int
cachefs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
caller_context_t *ct)
{
struct cnode *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error = 0;
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_getattr: ENTER vp %p\n", (void *)vp);
#endif
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
/* Call backfilesystem getattr if NFSv4 */
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_getattr_backfs_nfsv4(vp, vap, flags, cr, ct);
goto out;
}
/*
* If it has been specified that the return value will
* just be used as a hint, and we are only being asked
* for size, fsid or rdevid, then return the client's
* notion of these values without checking to make sure
* that the attribute cache is up to date.
* The whole point is to avoid an over the wire GETATTR
* call.
*/
if (flags & ATTR_HINT) {
if (vap->va_mask ==
(vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
if (vap->va_mask | AT_SIZE)
vap->va_size = cp->c_size;
/*
* Return the FSID of the cachefs filesystem,
* not the back filesystem
*/
if (vap->va_mask | AT_FSID)
vap->va_fsid = vp->v_vfsp->vfs_dev;
if (vap->va_mask | AT_RDEV)
vap->va_rdev = cp->c_attr.va_rdev;
return (0);
}
}
/*
* Only need to flush pages if asking for the mtime
* and if there any dirty pages.
*/
if (vap->va_mask & AT_MTIME) {
/*EMPTY*/
#if 0
/*
* XXX bob: stolen from nfs code, need to do something similar
*/
rp = VTOR(vp);
if ((rp->r_flags & RDIRTY) || rp->r_iocnt > 0)
(void) nfs3_putpage(vp, (offset_t)0, 0, 0, cr);
#endif
}
for (;;) {
/* get (or renew) access to the file system */
if (held) {
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
goto out;
held = 1;
/*
* If it has been specified that the return value will
* just be used as a hint, and we are only being asked
* for size, fsid or rdevid, then return the client's
* notion of these values without checking to make sure
* that the attribute cache is up to date.
* The whole point is to avoid an over the wire GETATTR
* call.
*/
if (flags & ATTR_HINT) {
if (vap->va_mask ==
(vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
if (vap->va_mask | AT_SIZE)
vap->va_size = cp->c_size;
/*
* Return the FSID of the cachefs filesystem,
* not the back filesystem
*/
if (vap->va_mask | AT_FSID)
vap->va_fsid = vp->v_vfsp->vfs_dev;
if (vap->va_mask | AT_RDEV)
vap->va_rdev = cp->c_attr.va_rdev;
goto out;
}
}
mutex_enter(&cp->c_statelock);
if ((cp->c_metadata.md_flags & MD_NEEDATTRS) &&
(fscp->fs_cdconnected != CFS_CD_CONNECTED)) {
mutex_exit(&cp->c_statelock);
connected = 1;
continue;
}
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
if (CFS_TIMEOUT(fscp, error)) {
mutex_exit(&cp->c_statelock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
continue;
}
if (error) {
mutex_exit(&cp->c_statelock);
break;
}
/* check for fileno conflict */
if ((fscp->fs_inum_size > 0) &&
((cp->c_metadata.md_flags & MD_LOCALFILENO) == 0)) {
ino64_t fakenum;
mutex_exit(&cp->c_statelock);
mutex_enter(&fscp->fs_fslock);
fakenum = cachefs_inum_real2fake(fscp,
cp->c_attr.va_nodeid);
if (fakenum == 0) {
fakenum = cachefs_fileno_conflict(fscp,
cp->c_attr.va_nodeid);
}
mutex_exit(&fscp->fs_fslock);
mutex_enter(&cp->c_statelock);
cp->c_metadata.md_flags |= MD_LOCALFILENO;
cp->c_metadata.md_localfileno = fakenum;
cp->c_flags |= CN_UPDATED;
}
/* copy out the attributes */
*vap = cp->c_attr;
/*
* return the FSID of the cachefs filesystem,
* not the back filesystem
*/
vap->va_fsid = vp->v_vfsp->vfs_dev;
/* return our idea of the size */
if (cp->c_size > vap->va_size)
vap->va_size = cp->c_size;
/* overwrite with our version of fileno and timestamps */
vap->va_nodeid = cp->c_metadata.md_localfileno;
vap->va_mtime = cp->c_metadata.md_localmtime;
vap->va_ctime = cp->c_metadata.md_localctime;
mutex_exit(&cp->c_statelock);
break;
}
out:
if (held)
cachefs_cd_release(fscp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_getattr: EXIT error = %d\n", error);
#endif
return (error);
}
/*
* cachefs_getattr_backfs_nfsv4
*
* Call NFSv4 back filesystem to handle the getattr (cachefs
* pass-through support for NFSv4).
*/
static int
cachefs_getattr_backfs_nfsv4(vnode_t *vp, vattr_t *vap,
int flags, cred_t *cr, caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
vnode_t *backvp;
int error;
/*
* For NFSv4 pass-through to work, only connected operation
* is supported, the cnode backvp must exist, and cachefs
* optional (eg., disconnectable) flags are turned off. Assert
* these conditions for the getattr operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* Call backfs vnode op after extracting backvp */
mutex_enter(&cp->c_statelock);
backvp = cp->c_backvp;
mutex_exit(&cp->c_statelock);
CFS_DPRINT_BACKFS_NFSV4(fscp, ("cachefs_getattr_backfs_nfsv4: cnode %p,"
" backvp %p\n", cp, backvp));
error = VOP_GETATTR(backvp, vap, flags, cr, ct);
/* Update attributes */
cp->c_attr = *vap;
/*
* return the FSID of the cachefs filesystem,
* not the back filesystem
*/
vap->va_fsid = vp->v_vfsp->vfs_dev;
return (error);
}
/*ARGSUSED4*/
static int
cachefs_setattr(
vnode_t *vp,
vattr_t *vap,
int flags,
cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error;
int connected;
int held = 0;
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the setattr operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
connected = 0;
for (;;) {
/* drop hold on file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
/* acquire access to the file system */
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
/* perform the setattr */
error = cachefs_setattr_common(vp, vap, flags, cr, ct);
if (error) {
/* if connected */
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
}
/* else must be disconnected */
else {
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
}
break;
}
if (held) {
cachefs_cd_release(fscp);
}
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
return (error);
}
static int
cachefs_setattr_common(
vnode_t *vp,
vattr_t *vap,
int flags,
cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
cachefscache_t *cachep = fscp->fs_cache;
uint_t mask = vap->va_mask;
int error = 0;
uint_t bcnt;
/* Cannot set these attributes. */
if (mask & AT_NOSET)
return (EINVAL);
/*
* Truncate file. Must have write permission and not be a directory.
*/
if (mask & AT_SIZE) {
if (vp->v_type == VDIR) {
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_TRUNCATE))
cachefs_log_truncate(cachep, EISDIR,
fscp->fs_cfsvfsp,
&cp->c_metadata.md_cookie,
cp->c_id.cid_fileno,
crgetuid(cr), vap->va_size);
return (EISDIR);
}
}
/*
* Gotta deal with one special case here, where we're setting the
* size of the file. First, we zero out part of the page after the
* new size of the file. Then we toss (not write) all pages after
* page in which the new offset occurs. Note that the NULL passed
* in instead of a putapage() fn parameter is correct, since
* no dirty pages will be found (B_TRUNC | B_INVAL).
*/
rw_enter(&cp->c_rwlock, RW_WRITER);
/* sync dirty pages */
if (!CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_putpage_common(vp, (offset_t)0, 0, 0, cr);
if (error == EINTR)
goto out;
}
error = 0;
/* if connected */
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_setattr_connected(vp, vap, flags, cr, ct);
}
/* else must be disconnected */
else {
error = cachefs_setattr_disconnected(vp, vap, flags, cr, ct);
}
if (error)
goto out;
/*
* If the file size has been changed then
* toss whole pages beyond the end of the file and zero
* the portion of the last page that is beyond the end of the file.
*/
if (mask & AT_SIZE && !CFS_ISFS_BACKFS_NFSV4(fscp)) {
bcnt = (uint_t)(cp->c_size & PAGEOFFSET);
if (bcnt)
pvn_vpzero(vp, cp->c_size, PAGESIZE - bcnt);
(void) pvn_vplist_dirty(vp, cp->c_size, cachefs_push,
B_TRUNC | B_INVAL, cr);
}
out:
rw_exit(&cp->c_rwlock);
if ((mask & AT_SIZE) &&
(CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_TRUNCATE)))
cachefs_log_truncate(cachep, error, fscp->fs_cfsvfsp,
&cp->c_metadata.md_cookie, cp->c_id.cid_fileno,
crgetuid(cr), vap->va_size);
return (error);
}
static int
cachefs_setattr_connected(
vnode_t *vp,
vattr_t *vap,
int flags,
cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
uint_t mask = vap->va_mask;
int error = 0;
int setsize;
mutex_enter(&cp->c_statelock);
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error)
goto out;
}
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
if (error)
goto out;
CFS_DPRINT_BACKFS_NFSV4(fscp, ("cachefs_setattr (nfsv4): cnode %p, "
"backvp %p\n", cp, cp->c_backvp));
error = VOP_SETATTR(cp->c_backvp, vap, flags, cr, ct);
if (error) {
goto out;
}
/* if the size of the file is being changed */
if (mask & AT_SIZE) {
cp->c_size = vap->va_size;
error = 0;
setsize = 0;
/* see if okay to try to set the file size */
if (((cp->c_flags & CN_NOCACHE) == 0) &&
CFS_ISFS_NONSHARED(fscp)) {
/* okay to set size if file is populated */
if (cp->c_metadata.md_flags & MD_POPULATED)
setsize = 1;
/*
* Okay to set size if front file exists and setting
* file size to zero.
*/
if ((cp->c_metadata.md_flags & MD_FILE) &&
(vap->va_size == 0))
setsize = 1;
}
/* if okay to try to set the file size */
if (setsize) {
error = 0;
if (cp->c_frontvp == NULL)
error = cachefs_getfrontfile(cp);
if (error == 0)
error = cachefs_frontfile_size(cp, cp->c_size);
} else if (cp->c_metadata.md_flags & MD_FILE) {
/* make sure file gets nocached */
error = EEXIST;
}
/* if we have to nocache the file */
if (error) {
if ((cp->c_flags & CN_NOCACHE) == 0 &&
!CFS_ISFS_BACKFS_NFSV4(fscp))
cachefs_nocache(cp);
error = 0;
}
}
cp->c_flags |= CN_UPDATED;
/* XXX bob: given what modify_cobject does this seems unnecessary */
cp->c_attr.va_mask = AT_ALL;
error = VOP_GETATTR(cp->c_backvp, &cp->c_attr, 0, cr, ct);
if (error)
goto out;
cp->c_attr.va_size = MAX(cp->c_attr.va_size, cp->c_size);
cp->c_size = cp->c_attr.va_size;
CFSOP_MODIFY_COBJECT(fscp, cp, cr);
out:
mutex_exit(&cp->c_statelock);
return (error);
}
/*
* perform the setattr on the local file system
*/
/*ARGSUSED4*/
static int
cachefs_setattr_disconnected(
vnode_t *vp,
vattr_t *vap,
int flags,
cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int mask;
int error;
int newfile;
off_t commit = 0;
if (CFS_ISFS_WRITE_AROUND(fscp))
return (ETIMEDOUT);
/* if we do not have good attributes */
if (cp->c_metadata.md_flags & MD_NEEDATTRS)
return (ETIMEDOUT);
/* primary concern is to keep this routine as much like ufs_setattr */
mutex_enter(&cp->c_statelock);
error = secpolicy_vnode_setattr(cr, vp, vap, &cp->c_attr, flags,
cachefs_access_local, cp);
if (error)
goto out;
mask = vap->va_mask;
/* if changing the size of the file */
if (mask & AT_SIZE) {
if (vp->v_type == VDIR) {
error = EISDIR;
goto out;
}
if (vp->v_type == VFIFO) {
error = 0;
goto out;
}
if ((vp->v_type != VREG) &&
!((vp->v_type == VLNK) && (vap->va_size == 0))) {
error = EINVAL;
goto out;
}
if (vap->va_size > fscp->fs_offmax) {
error = EFBIG;
goto out;
}
/* if the file is not populated and we are not truncating it */
if (((cp->c_metadata.md_flags & MD_POPULATED) == 0) &&
(vap->va_size != 0)) {
error = ETIMEDOUT;
goto out;
}
if ((cp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
error = ENOSPC;
goto out;
}
cp->c_metadata.md_flags |= MD_MAPPING;
}
/* log the operation */
commit = cachefs_dlog_setattr(fscp, vap, flags, cp, cr);
if (commit == 0) {
error = ENOSPC;
goto out;
}
cp->c_flags &= ~CN_NOCACHE;
/* special case truncating fast sym links */
if ((vp->v_type == VLNK) &&
(cp->c_metadata.md_flags & MD_FASTSYMLNK)) {
/* XXX how can we get here */
/* XXX should update mtime */
cp->c_size = 0;
error = 0;
goto out;
}
/* get the front file, this may create one */
newfile = (cp->c_metadata.md_flags & MD_FILE) ? 0 : 1;
if (cp->c_frontvp == NULL) {
error = cachefs_getfrontfile(cp);
if (error)
goto out;
}
ASSERT(cp->c_frontvp);
if (newfile && (cp->c_flags & CN_UPDATED)) {
/* allocate space for the metadata */
ASSERT((cp->c_flags & CN_ALLOC_PENDING) == 0);
ASSERT((cp->c_filegrp->fg_flags & CFS_FG_ALLOC_ATTR)
== 0);
error = filegrp_write_metadata(cp->c_filegrp,
&cp->c_id, &cp->c_metadata);
if (error)
goto out;
}
/* change the size of the front file */
error = cachefs_frontfile_size(cp, vap->va_size);
if (error)
goto out;
cp->c_attr.va_size = cp->c_size = vap->va_size;
gethrestime(&cp->c_metadata.md_localmtime);
cp->c_metadata.md_flags |= MD_POPULATED | MD_LOCALMTIME;
cachefs_modified(cp);
cp->c_flags |= CN_UPDATED;
}
if (mask & AT_MODE) {
/* mark as modified */
if (cachefs_modified_alloc(cp)) {
error = ENOSPC;
goto out;
}
if ((cp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
error = ENOSPC;
goto out;
}
cp->c_metadata.md_flags |= MD_MAPPING;
}
/* log the operation if not already logged */
if (commit == 0) {
commit = cachefs_dlog_setattr(fscp, vap, flags, cp, cr);
if (commit == 0) {
error = ENOSPC;
goto out;
}
}
cp->c_attr.va_mode &= S_IFMT;
cp->c_attr.va_mode |= vap->va_mode & ~S_IFMT;
gethrestime(&cp->c_metadata.md_localctime);
cp->c_metadata.md_flags |= MD_LOCALCTIME;
cp->c_flags |= CN_UPDATED;
}
if (mask & (AT_UID|AT_GID)) {
/* mark as modified */
if (cachefs_modified_alloc(cp)) {
error = ENOSPC;
goto out;
}
if ((cp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
error = ENOSPC;
goto out;
}
cp->c_metadata.md_flags |= MD_MAPPING;
}
/* log the operation if not already logged */
if (commit == 0) {
commit = cachefs_dlog_setattr(fscp, vap, flags, cp, cr);
if (commit == 0) {
error = ENOSPC;
goto out;
}
}
if (mask & AT_UID)
cp->c_attr.va_uid = vap->va_uid;
if (mask & AT_GID)
cp->c_attr.va_gid = vap->va_gid;
gethrestime(&cp->c_metadata.md_localctime);
cp->c_metadata.md_flags |= MD_LOCALCTIME;
cp->c_flags |= CN_UPDATED;
}
if (mask & (AT_MTIME|AT_ATIME)) {
/* mark as modified */
if (cachefs_modified_alloc(cp)) {
error = ENOSPC;
goto out;
}
if ((cp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
error = ENOSPC;
goto out;
}
cp->c_metadata.md_flags |= MD_MAPPING;
}
/* log the operation if not already logged */
if (commit == 0) {
commit = cachefs_dlog_setattr(fscp, vap, flags, cp, cr);
if (commit == 0) {
error = ENOSPC;
goto out;
}
}
if (mask & AT_MTIME) {
cp->c_metadata.md_localmtime = vap->va_mtime;
cp->c_metadata.md_flags |= MD_LOCALMTIME;
}
if (mask & AT_ATIME)
cp->c_attr.va_atime = vap->va_atime;
gethrestime(&cp->c_metadata.md_localctime);
cp->c_metadata.md_flags |= MD_LOCALCTIME;
cp->c_flags |= CN_UPDATED;
}
out:
mutex_exit(&cp->c_statelock);
/* commit the log entry */
if (commit) {
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX bob: fix on panic */
}
}
return (error);
}
/* ARGSUSED */
static int
cachefs_access(vnode_t *vp, int mode, int flags, cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error;
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_access: ENTER vp %p\n", (void *)vp);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the access operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
break;
held = 1;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_access_connected(vp, mode, flags,
cr);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
mutex_enter(&cp->c_statelock);
error = cachefs_access_local(cp, mode, cr);
mutex_exit(&cp->c_statelock);
if (CFS_TIMEOUT(fscp, error)) {
if (cachefs_cd_access_miss(fscp)) {
mutex_enter(&cp->c_statelock);
if (cp->c_backvp == NULL) {
(void) cachefs_getbackvp(fscp,
cp);
}
mutex_exit(&cp->c_statelock);
error = cachefs_access_connected(vp,
mode, flags, cr);
if (!CFS_TIMEOUT(fscp, error))
break;
delay(5*hz);
connected = 0;
continue;
}
connected = 1;
continue;
}
}
break;
}
if (held)
cachefs_cd_release(fscp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_access: EXIT error = %d\n", error);
#endif
return (error);
}
static int
cachefs_access_connected(struct vnode *vp, int mode, int flags, cred_t *cr)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error = 0;
mutex_enter(&cp->c_statelock);
/* Make sure the cnode attrs are valid first. */
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
if (error)
goto out;
/* see if can do a local file system check */
if ((fscp->fs_info.fi_mntflags & CFS_ACCESS_BACKFS) == 0 &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_access_local(cp, mode, cr);
goto out;
}
/* else do a remote file system check */
else {
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error)
goto out;
}
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_access (nfsv4): cnode %p, backvp %p\n",
cp, cp->c_backvp));
error = VOP_ACCESS(cp->c_backvp, mode, flags, cr, NULL);
/*
* even though we don't `need' the ACL to do access
* via the backvp, we should cache it here to make our
* behavior more reasonable if we go disconnected.
*/
if (((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0) &&
(cachefs_vtype_aclok(vp)) &&
((cp->c_flags & CN_NOCACHE) == 0) &&
(!CFS_ISFS_BACKFS_NFSV4(fscp)) &&
((cp->c_metadata.md_flags & MD_ACL) == 0))
(void) cachefs_cacheacl(cp, NULL);
}
out:
/*
* If NFS returned ESTALE, mark this cnode as stale, so that
* the vn_open retry will read the file anew from backfs
*/
if (error == ESTALE)
cachefs_cnode_stale(cp);
mutex_exit(&cp->c_statelock);
return (error);
}
/*
* CFS has a fastsymlink scheme. If the size of the link is < C_FSL_SIZE, then
* the link is placed in the metadata itself (no front file is allocated).
*/
/*ARGSUSED*/
static int
cachefs_readlink(vnode_t *vp, uio_t *uiop, cred_t *cr, caller_context_t *ct)
{
int error = 0;
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
cachefscache_t *cachep = fscp->fs_cache;
int held = 0;
int connected = 0;
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
if (vp->v_type != VLNK)
return (EINVAL);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the readlink operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
break;
held = 1;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
/*
* since readlink_connected will call stuffsymlink
* on success, have to serialize access
*/
if (!rw_tryenter(&cp->c_rwlock, RW_WRITER)) {
cachefs_cd_release(fscp);
rw_enter(&cp->c_rwlock, RW_WRITER);
error = cachefs_cd_access(fscp, connected, 0);
if (error) {
held = 0;
rw_exit(&cp->c_rwlock);
break;
}
}
error = cachefs_readlink_connected(vp, uiop, cr);
rw_exit(&cp->c_rwlock);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_readlink_disconnected(vp, uiop);
if (CFS_TIMEOUT(fscp, error)) {
if (cachefs_cd_access_miss(fscp)) {
/* as above */
if (!rw_tryenter(&cp->c_rwlock,
RW_WRITER)) {
cachefs_cd_release(fscp);
rw_enter(&cp->c_rwlock,
RW_WRITER);
error = cachefs_cd_access(fscp,
connected, 0);
if (error) {
held = 0;
rw_exit(&cp->c_rwlock);
break;
}
}
error = cachefs_readlink_connected(vp,
uiop, cr);
rw_exit(&cp->c_rwlock);
if (!CFS_TIMEOUT(fscp, error))
break;
delay(5*hz);
connected = 0;
continue;
}
connected = 1;
continue;
}
}
break;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_READLINK))
cachefs_log_readlink(cachep, error, fscp->fs_cfsvfsp,
&cp->c_metadata.md_cookie, cp->c_id.cid_fileno,
crgetuid(cr), cp->c_size);
if (held)
cachefs_cd_release(fscp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
/*
* The over the wire error for attempting to readlink something
* other than a symbolic link is ENXIO. However, we need to
* return EINVAL instead of ENXIO, so we map it here.
*/
return (error == ENXIO ? EINVAL : error);
}
static int
cachefs_readlink_connected(vnode_t *vp, uio_t *uiop, cred_t *cr)
{
int error;
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
caddr_t buf;
int buflen;
int readcache = 0;
mutex_enter(&cp->c_statelock);
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
if (error)
goto out;
/* if the sym link is cached as a fast sym link */
if (cp->c_metadata.md_flags & MD_FASTSYMLNK) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
error = uiomove(cp->c_metadata.md_allocinfo,
MIN(cp->c_size, uiop->uio_resid), UIO_READ, uiop);
#ifdef CFSDEBUG
readcache = 1;
goto out;
#else /* CFSDEBUG */
/* XXX KLUDGE! correct for insidious 0-len symlink */
if (cp->c_size != 0) {
readcache = 1;
goto out;
}
#endif /* CFSDEBUG */
}
/* if the sym link is cached in a front file */
if (cp->c_metadata.md_flags & MD_POPULATED) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
ASSERT(cp->c_metadata.md_flags & MD_FILE);
if (cp->c_frontvp == NULL) {
(void) cachefs_getfrontfile(cp);
}
if (cp->c_metadata.md_flags & MD_POPULATED) {
/* read symlink data from frontfile */
uiop->uio_offset = 0;
(void) VOP_RWLOCK(cp->c_frontvp,
V_WRITELOCK_FALSE, NULL);
error = VOP_READ(cp->c_frontvp, uiop, 0, kcred, NULL);
VOP_RWUNLOCK(cp->c_frontvp, V_WRITELOCK_FALSE, NULL);
/* XXX KLUDGE! correct for insidious 0-len symlink */
if (cp->c_size != 0) {
readcache = 1;
goto out;
}
}
}
/* get the sym link contents from the back fs */
error = cachefs_readlink_back(cp, cr, &buf, &buflen);
if (error)
goto out;
/* copy the contents out to the user */
error = uiomove(buf, MIN(buflen, uiop->uio_resid), UIO_READ, uiop);
/*
* try to cache the sym link, note that its a noop if NOCACHE is set
* or if NFSv4 pass-through is enabled.
*/
if (cachefs_stuffsymlink(cp, buf, buflen)) {
cachefs_nocache(cp);
}
cachefs_kmem_free(buf, MAXPATHLEN);
out:
mutex_exit(&cp->c_statelock);
if (error == 0) {
if (readcache)
fscp->fs_stats.st_hits++;
else
fscp->fs_stats.st_misses++;
}
return (error);
}
static int
cachefs_readlink_disconnected(vnode_t *vp, uio_t *uiop)
{
int error;
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int readcache = 0;
mutex_enter(&cp->c_statelock);
/* if the sym link is cached as a fast sym link */
if (cp->c_metadata.md_flags & MD_FASTSYMLNK) {
error = uiomove(cp->c_metadata.md_allocinfo,
MIN(cp->c_size, uiop->uio_resid), UIO_READ, uiop);
readcache = 1;
goto out;
}
/* if the sym link is cached in a front file */
if (cp->c_metadata.md_flags & MD_POPULATED) {
ASSERT(cp->c_metadata.md_flags & MD_FILE);
if (cp->c_frontvp == NULL) {
(void) cachefs_getfrontfile(cp);
}
if (cp->c_metadata.md_flags & MD_POPULATED) {
/* read symlink data from frontfile */
uiop->uio_offset = 0;
(void) VOP_RWLOCK(cp->c_frontvp,
V_WRITELOCK_FALSE, NULL);
error = VOP_READ(cp->c_frontvp, uiop, 0, kcred, NULL);
VOP_RWUNLOCK(cp->c_frontvp, V_WRITELOCK_FALSE, NULL);
readcache = 1;
goto out;
}
}
error = ETIMEDOUT;
out:
mutex_exit(&cp->c_statelock);
if (error == 0) {
if (readcache)
fscp->fs_stats.st_hits++;
else
fscp->fs_stats.st_misses++;
}
return (error);
}
/*ARGSUSED*/
static int
cachefs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
int error = 0;
fscache_t *fscp = C_TO_FSCACHE(cp);
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_fsync: ENTER vp %p\n", (void *)vp);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (fscp->fs_backvfsp && fscp->fs_backvfsp->vfs_flag & VFS_RDONLY)
goto out;
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the fsync operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
connected = 0;
/* if a regular file, write out the pages */
if ((vp->v_type == VREG) && vn_has_cached_data(vp) &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_putpage_common(vp, (offset_t)0,
0, 0, cr);
if (CFS_TIMEOUT(fscp, error)) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
continue;
} else {
connected = 1;
continue;
}
}
/* if no space left in cache, wait until connected */
if ((error == ENOSPC) &&
(fscp->fs_cdconnected != CFS_CD_CONNECTED)) {
connected = 1;
continue;
}
/* clear the cnode error if putpage worked */
if ((error == 0) && cp->c_error) {
mutex_enter(&cp->c_statelock);
cp->c_error = 0;
mutex_exit(&cp->c_statelock);
}
if (error)
break;
}
/* if connected, sync the backvp */
if ((fscp->fs_cdconnected == CFS_CD_CONNECTED) &&
cp->c_backvp) {
mutex_enter(&cp->c_statelock);
if (cp->c_backvp) {
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_fsync (nfsv4): cnode %p, "
"backvp %p\n", cp, cp->c_backvp));
error = VOP_FSYNC(cp->c_backvp, syncflag, cr,
ct);
if (CFS_TIMEOUT(fscp, error)) {
mutex_exit(&cp->c_statelock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
continue;
} else if (error && (error != EINTR))
cp->c_error = error;
}
mutex_exit(&cp->c_statelock);
}
/* sync the metadata and the front file to the front fs */
if (!CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_sync_metadata(cp);
if (error &&
(fscp->fs_cdconnected == CFS_CD_CONNECTED))
error = 0;
}
break;
}
if (error == 0)
error = cp->c_error;
if (held)
cachefs_cd_release(fscp);
out:
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_fsync: EXIT vp %p\n", (void *)vp);
#endif
return (error);
}
/*
* Called from cachefs_inactive(), to make sure all the data goes out to disk.
*/
int
cachefs_sync_metadata(cnode_t *cp)
{
int error = 0;
struct filegrp *fgp;
struct vattr va;
fscache_t *fscp = C_TO_FSCACHE(cp);
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("c_sync_metadata: ENTER cp %p cflag %x\n",
(void *)cp, cp->c_flags);
#endif
mutex_enter(&cp->c_statelock);
if ((cp->c_flags & CN_UPDATED) == 0)
goto out;
if (cp->c_flags & (CN_STALE | CN_DESTROY))
goto out;
fgp = cp->c_filegrp;
if ((fgp->fg_flags & CFS_FG_WRITE) == 0)
goto out;
if (CFS_ISFS_BACKFS_NFSV4(fscp))
goto out;
if (fgp->fg_flags & CFS_FG_ALLOC_ATTR) {
mutex_exit(&cp->c_statelock);
error = filegrp_allocattr(fgp);
mutex_enter(&cp->c_statelock);
if (error) {
error = 0;
goto out;
}
}
if (cp->c_flags & CN_ALLOC_PENDING) {
error = filegrp_create_metadata(fgp, &cp->c_metadata,
&cp->c_id);
if (error)
goto out;
cp->c_flags &= ~CN_ALLOC_PENDING;
}
if (cp->c_flags & CN_NEED_FRONT_SYNC) {
if (cp->c_frontvp != NULL) {
error = VOP_FSYNC(cp->c_frontvp, FSYNC, kcred, NULL);
if (error) {
cp->c_metadata.md_timestamp.tv_sec = 0;
} else {
va.va_mask = AT_MTIME;
error = VOP_GETATTR(cp->c_frontvp, &va, 0,
kcred, NULL);
if (error)
goto out;
cp->c_metadata.md_timestamp = va.va_mtime;
cp->c_flags &=
~(CN_NEED_FRONT_SYNC |
CN_POPULATION_PENDING);
}
} else {
cp->c_flags &=
~(CN_NEED_FRONT_SYNC | CN_POPULATION_PENDING);
}
}
/*
* XXX tony: How can CN_ALLOC_PENDING still be set??
* XXX tony: How can CN_UPDATED not be set?????
*/
if ((cp->c_flags & CN_ALLOC_PENDING) == 0 &&
(cp->c_flags & CN_UPDATED)) {
error = filegrp_write_metadata(fgp, &cp->c_id,
&cp->c_metadata);
if (error)
goto out;
}
out:
if (error) {
/* XXX modified files? */
if (cp->c_metadata.md_rlno) {
cachefs_removefrontfile(&cp->c_metadata,
&cp->c_id, fgp);
cachefs_rlent_moveto(C_TO_FSCACHE(cp)->fs_cache,
CACHEFS_RL_FREE, cp->c_metadata.md_rlno, 0);
cp->c_metadata.md_rlno = 0;
cp->c_metadata.md_rltype = CACHEFS_RL_NONE;
if (cp->c_frontvp) {
VN_RELE(cp->c_frontvp);
cp->c_frontvp = NULL;
}
}
if ((cp->c_flags & CN_ALLOC_PENDING) == 0)
(void) filegrp_destroy_metadata(fgp, &cp->c_id);
cp->c_flags |= CN_ALLOC_PENDING;
cachefs_nocache(cp);
}
/*
* we clear the updated bit even on errors because a retry
* will probably fail also.
*/
cp->c_flags &= ~CN_UPDATED;
mutex_exit(&cp->c_statelock);
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("c_sync_metadata: EXIT cp %p cflag %x\n",
(void *)cp, cp->c_flags);
#endif
return (error);
}
/*
* This is the vop entry point for inactivating a vnode.
* It just queues the request for the async thread which
* calls cachefs_inactive.
* Because of the dnlc, it is not safe to grab most locks here.
*/
/*ARGSUSED*/
static void
cachefs_inactive(struct vnode *vp, cred_t *cr, caller_context_t *ct)
{
cnode_t *cp;
struct cachefs_req *rp;
fscache_t *fscp;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_inactive: ENTER vp %p\n", (void *)vp);
#endif
cp = VTOC(vp);
fscp = C_TO_FSCACHE(cp);
ASSERT((cp->c_flags & CN_IDLE) == 0);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the inactive operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* vn_rele() set the v_count == 1 */
cp->c_ipending = 1;
rp = kmem_cache_alloc(cachefs_req_cache, KM_SLEEP);
rp->cfs_cmd = CFS_IDLE;
rp->cfs_cr = cr;
crhold(rp->cfs_cr);
rp->cfs_req_u.cu_idle.ci_vp = vp;
cachefs_addqueue(rp, &(C_TO_FSCACHE(cp)->fs_workq));
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_inactive: EXIT vp %p\n", (void *)vp);
#endif
}
/* ARGSUSED */
static int
cachefs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp,
struct pathname *pnp, int flags, vnode_t *rdir, cred_t *cr,
caller_context_t *ct, int *direntflags, pathname_t *realpnp)
{
int error = 0;
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_lookup: ENTER dvp %p nm %s\n", (void *)dvp, nm);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the lookup operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(dcp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
break;
held = 1;
error = cachefs_lookup_common(dvp, nm, vpp, pnp,
flags, rdir, cr);
if (CFS_TIMEOUT(fscp, error)) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
} else {
if (cachefs_cd_access_miss(fscp)) {
rw_enter(&dcp->c_rwlock, RW_READER);
error = cachefs_lookup_back(dvp, nm,
vpp, cr);
rw_exit(&dcp->c_rwlock);
if (!CFS_TIMEOUT(fscp, error))
break;
delay(5*hz);
connected = 0;
continue;
}
connected = 1;
continue;
}
}
break;
}
if (held)
cachefs_cd_release(fscp);
if (error == 0 && IS_DEVVP(*vpp)) {
struct vnode *newvp;
newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
VN_RELE(*vpp);
if (newvp == NULL) {
error = ENOSYS;
} else {
*vpp = newvp;
}
}
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_lookup: EXIT error = %d\n", error);
#endif
return (error);
}
/* ARGSUSED */
int
cachefs_lookup_common(vnode_t *dvp, char *nm, vnode_t **vpp,
struct pathname *pnp, int flags, vnode_t *rdir, cred_t *cr)
{
int error = 0;
cnode_t *cp, *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
struct fid cookie;
u_offset_t d_offset;
struct cachefs_req *rp;
cfs_cid_t cid, dircid;
uint_t flag;
uint_t uncached = 0;
*vpp = NULL;
/*
* If lookup is for "", just return dvp. Don't need
* to send it over the wire, look it up in the dnlc,
* or perform any access checks.
*/
if (*nm == '\0') {
VN_HOLD(dvp);
*vpp = dvp;
return (0);
}
/* can't do lookups in non-directories */
if (dvp->v_type != VDIR)
return (ENOTDIR);
/* perform access check, also does consistency check if connected */
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_access_connected(dvp, VEXEC, 0, cr);
} else {
mutex_enter(&dcp->c_statelock);
error = cachefs_access_local(dcp, VEXEC, cr);
mutex_exit(&dcp->c_statelock);
}
if (error)
return (error);
/*
* If lookup is for ".", just return dvp. Don't need
* to send it over the wire or look it up in the dnlc,
* just need to check access.
*/
if (strcmp(nm, ".") == 0) {
VN_HOLD(dvp);
*vpp = dvp;
return (0);
}
/* check the dnlc */
*vpp = (vnode_t *)dnlc_lookup(dvp, nm);
if (*vpp)
return (0);
/* read lock the dir before starting the search */
rw_enter(&dcp->c_rwlock, RW_READER);
mutex_enter(&dcp->c_statelock);
dircid = dcp->c_id;
dcp->c_usage++;
/* if front file is not usable, lookup on the back fs */
if ((dcp->c_flags & (CN_NOCACHE | CN_ASYNC_POPULATE)) ||
CFS_ISFS_BACKFS_NFSV4(fscp) ||
((dcp->c_filegrp->fg_flags & CFS_FG_READ) == 0)) {
mutex_exit(&dcp->c_statelock);
if (fscp->fs_cdconnected == CFS_CD_CONNECTED)
error = cachefs_lookup_back(dvp, nm, vpp, cr);
else
error = ETIMEDOUT;
goto out;
}
/* if the front file is not populated, try to populate it */
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
error = ETIMEDOUT;
mutex_exit(&dcp->c_statelock);
goto out;
}
if (cachefs_async_okay()) {
/* cannot populate if cache is not writable */
ASSERT((dcp->c_flags &
(CN_ASYNC_POPULATE | CN_NOCACHE)) == 0);
dcp->c_flags |= CN_ASYNC_POPULATE;
rp = kmem_cache_alloc(cachefs_req_cache, KM_SLEEP);
rp->cfs_cmd = CFS_POPULATE;
rp->cfs_req_u.cu_populate.cpop_vp = dvp;
rp->cfs_cr = cr;
crhold(cr);
VN_HOLD(dvp);
cachefs_addqueue(rp, &fscp->fs_workq);
} else if (fscp->fs_info.fi_mntflags & CFS_NOACL) {
error = cachefs_dir_fill(dcp, cr);
if (error != 0) {
mutex_exit(&dcp->c_statelock);
goto out;
}
}
/* no populate if too many asyncs and we have to cache ACLs */
mutex_exit(&dcp->c_statelock);
if (fscp->fs_cdconnected == CFS_CD_CONNECTED)
error = cachefs_lookup_back(dvp, nm, vpp, cr);
else
error = ETIMEDOUT;
goto out;
}
/* by now we have a valid cached front file that we can search */
ASSERT((dcp->c_flags & CN_ASYNC_POPULATE) == 0);
error = cachefs_dir_look(dcp, nm, &cookie, &flag,
&d_offset, &cid);
mutex_exit(&dcp->c_statelock);
if (error) {
/* if the entry does not have the fid, go get it */
if (error == EINVAL) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED)
error = cachefs_lookup_back(dvp, nm, vpp, cr);
else
error = ETIMEDOUT;
}
/* errors other than does not exist */
else if (error != ENOENT) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED)
error = cachefs_lookup_back(dvp, nm, vpp, cr);
else
error = ETIMEDOUT;
}
goto out;
}
/*
* Else we found the entry in the cached directory.
* Make a cnode for it.
*/
error = cachefs_cnode_make(&cid, fscp, &cookie, NULL, NULL,
cr, 0, &cp);
if (error == ESTALE) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
mutex_enter(&dcp->c_statelock);
cachefs_nocache(dcp);
mutex_exit(&dcp->c_statelock);
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_lookup_back(dvp, nm, vpp, cr);
uncached = 1;
} else
error = ETIMEDOUT;
} else if (error == 0) {
*vpp = CTOV(cp);
}
out:
if (error == 0) {
/* put the entry in the dnlc */
if (cachefs_dnlc)
dnlc_enter(dvp, nm, *vpp);
/* save the cid of the parent so can find the name */
cp = VTOC(*vpp);
if (bcmp(&cp->c_metadata.md_parent, &dircid,
sizeof (cfs_cid_t)) != 0) {
mutex_enter(&cp->c_statelock);
cp->c_metadata.md_parent = dircid;
cp->c_flags |= CN_UPDATED;
mutex_exit(&cp->c_statelock);
}
}
rw_exit(&dcp->c_rwlock);
if (uncached && dcp->c_metadata.md_flags & MD_PACKED)
(void) cachefs_pack_common(dvp, cr);
return (error);
}
/*
* Called from cachefs_lookup_common when the back file system needs to be
* examined to perform the lookup.
*/
static int
cachefs_lookup_back(vnode_t *dvp, char *nm, vnode_t **vpp,
cred_t *cr)
{
int error = 0;
cnode_t *cp, *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
vnode_t *backvp = NULL;
struct vattr va;
struct fid cookie;
cfs_cid_t cid;
uint32_t valid_fid;
mutex_enter(&dcp->c_statelock);
/* do a lookup on the back FS to get the back vnode */
if (dcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, dcp);
if (error)
goto out;
}
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_lookup (nfsv4): dcp %p, dbackvp %p, name %s\n",
dcp, dcp->c_backvp, nm));
error = VOP_LOOKUP(dcp->c_backvp, nm, &backvp, (struct pathname *)NULL,
0, (vnode_t *)NULL, cr, NULL, NULL, NULL);
if (error)
goto out;
if (IS_DEVVP(backvp)) {
struct vnode *devvp = backvp;
if (VOP_REALVP(devvp, &backvp, NULL) == 0) {
VN_HOLD(backvp);
VN_RELE(devvp);
}
}
/* get the fid and attrs from the back fs */
valid_fid = (CFS_ISFS_BACKFS_NFSV4(fscp) ? FALSE : TRUE);
error = cachefs_getcookie(backvp, &cookie, &va, cr, valid_fid);
if (error)
goto out;
cid.cid_fileno = va.va_nodeid;
cid.cid_flags = 0;
#if 0
/* XXX bob: this is probably no longer necessary */
/* if the directory entry was incomplete, we can complete it now */
if ((dcp->c_metadata.md_flags & MD_POPULATED) &&
((dcp->c_flags & CN_ASYNC_POPULATE) == 0) &&
(dcp->c_filegrp->fg_flags & CFS_FG_WRITE)) {
cachefs_dir_modentry(dcp, d_offset, &cookie, &cid);
}
#endif
out:
mutex_exit(&dcp->c_statelock);
/* create the cnode */
if (error == 0) {
error = cachefs_cnode_make(&cid, fscp,
(valid_fid ? &cookie : NULL),
&va, backvp, cr, 0, &cp);
if (error == 0) {
*vpp = CTOV(cp);
}
}
if (backvp)
VN_RELE(backvp);
return (error);
}
/*ARGSUSED7*/
static int
cachefs_create(vnode_t *dvp, char *nm, vattr_t *vap,
vcexcl_t exclusive, int mode, vnode_t **vpp, cred_t *cr, int flag,
caller_context_t *ct, vsecattr_t *vsecp)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
cachefscache_t *cachep = fscp->fs_cache;
int error;
int connected = 0;
int held = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_create: ENTER dvp %p, nm %s\n",
(void *)dvp, nm);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the create operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(dcp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
/*
* if we are connected, perform the remote portion of the
* create.
*/
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_create_connected(dvp, nm, vap,
exclusive, mode, vpp, cr);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
} else if (error) {
break;
}
}
/* else we must be disconnected */
else {
error = cachefs_create_disconnected(dvp, nm, vap,
exclusive, mode, vpp, cr);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
} else if (error) {
break;
}
}
break;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_CREATE)) {
fid_t *fidp = NULL;
ino64_t fileno = 0;
cnode_t *cp = NULL;
if (error == 0)
cp = VTOC(*vpp);
if (cp != NULL) {
fidp = &cp->c_metadata.md_cookie;
fileno = cp->c_id.cid_fileno;
}
cachefs_log_create(cachep, error, fscp->fs_cfsvfsp,
fidp, fileno, crgetuid(cr));
}
if (held)
cachefs_cd_release(fscp);
if (error == 0 && CFS_ISFS_NONSHARED(fscp))
(void) cachefs_pack(dvp, nm, cr);
if (error == 0 && IS_DEVVP(*vpp)) {
struct vnode *spcvp;
spcvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
VN_RELE(*vpp);
if (spcvp == NULL) {
error = ENOSYS;
} else {
*vpp = spcvp;
}
}
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_create: EXIT error %d\n", error);
#endif
return (error);
}
static int
cachefs_create_connected(vnode_t *dvp, char *nm, vattr_t *vap,
enum vcexcl exclusive, int mode, vnode_t **vpp, cred_t *cr)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error;
vnode_t *tvp = NULL;
vnode_t *devvp;
fid_t cookie;
vattr_t va;
cnode_t *ncp;
cfs_cid_t cid;
vnode_t *vp;
uint32_t valid_fid;
/* special case if file already exists */
error = cachefs_lookup_common(dvp, nm, &vp, NULL, 0, NULL, cr);
if (CFS_TIMEOUT(fscp, error))
return (error);
if (error == 0) {
if (exclusive == EXCL)
error = EEXIST;
else if (vp->v_type == VDIR && (mode & VWRITE))
error = EISDIR;
else if ((error =
cachefs_access_connected(vp, mode, 0, cr)) == 0) {
if ((vap->va_mask & AT_SIZE) && (vp->v_type == VREG)) {
vap->va_mask = AT_SIZE;
error = cachefs_setattr_common(vp, vap, 0,
cr, NULL);
}
}
if (error) {
VN_RELE(vp);
} else
*vpp = vp;
return (error);
}
rw_enter(&dcp->c_rwlock, RW_WRITER);
mutex_enter(&dcp->c_statelock);
/* consistency check the directory */
error = CFSOP_CHECK_COBJECT(fscp, dcp, 0, cr);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
/* get the backvp if necessary */
if (dcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, dcp);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
}
/* create the file on the back fs */
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_create (nfsv4): dcp %p, dbackvp %p,"
"name %s\n", dcp, dcp->c_backvp, nm));
error = VOP_CREATE(dcp->c_backvp, nm, vap, exclusive, mode,
&devvp, cr, 0, NULL, NULL);
mutex_exit(&dcp->c_statelock);
if (error)
goto out;
if (VOP_REALVP(devvp, &tvp, NULL) == 0) {
VN_HOLD(tvp);
VN_RELE(devvp);
} else {
tvp = devvp;
}
/* get the fid and attrs from the back fs */
valid_fid = (CFS_ISFS_BACKFS_NFSV4(fscp) ? FALSE : TRUE);
error = cachefs_getcookie(tvp, &cookie, &va, cr, valid_fid);
if (error)
goto out;
/* make the cnode */
cid.cid_fileno = va.va_nodeid;
cid.cid_flags = 0;
error = cachefs_cnode_make(&cid, fscp, (valid_fid ? &cookie : NULL),
&va, tvp, cr, 0, &ncp);
if (error)
goto out;
*vpp = CTOV(ncp);
/* enter it in the parent directory */
mutex_enter(&dcp->c_statelock);
if (CFS_ISFS_NONSHARED(fscp) &&
(dcp->c_metadata.md_flags & MD_POPULATED)) {
/* see if entry already exists */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
error = cachefs_dir_look(dcp, nm, NULL, NULL, NULL, NULL);
if (error == ENOENT) {
/* entry, does not exist, add the new file */
error = cachefs_dir_enter(dcp, nm, &ncp->c_cookie,
&ncp->c_id, SM_ASYNC);
if (error) {
cachefs_nocache(dcp);
error = 0;
}
/* XXX should this be done elsewhere, too? */
dnlc_enter(dvp, nm, *vpp);
} else {
/* entry exists or some other problem */
cachefs_nocache(dcp);
error = 0;
}
}
CFSOP_MODIFY_COBJECT(fscp, dcp, cr);
mutex_exit(&dcp->c_statelock);
out:
rw_exit(&dcp->c_rwlock);
if (tvp)
VN_RELE(tvp);
return (error);
}
static int
cachefs_create_disconnected(vnode_t *dvp, char *nm, vattr_t *vap,
enum vcexcl exclusive, int mode, vnode_t **vpp, cred_t *cr)
{
cnode_t *dcp = VTOC(dvp);
cnode_t *cp;
cnode_t *ncp = NULL;
vnode_t *vp;
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error = 0;
struct vattr va;
timestruc_t current_time;
off_t commit = 0;
fid_t cookie;
cfs_cid_t cid;
rw_enter(&dcp->c_rwlock, RW_WRITER);
mutex_enter(&dcp->c_statelock);
/* give up if the directory is not populated */
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
mutex_exit(&dcp->c_statelock);
rw_exit(&dcp->c_rwlock);
return (ETIMEDOUT);
}
/* special case if file already exists */
error = cachefs_dir_look(dcp, nm, &cookie, NULL, NULL, &cid);
if (error == EINVAL) {
mutex_exit(&dcp->c_statelock);
rw_exit(&dcp->c_rwlock);
return (ETIMEDOUT);
}
if (error == 0) {
mutex_exit(&dcp->c_statelock);
rw_exit(&dcp->c_rwlock);
error = cachefs_cnode_make(&cid, fscp, &cookie, NULL, NULL,
cr, 0, &cp);
if (error) {
return (error);
}
vp = CTOV(cp);
if (cp->c_metadata.md_flags & MD_NEEDATTRS)
error = ETIMEDOUT;
else if (exclusive == EXCL)
error = EEXIST;
else if (vp->v_type == VDIR && (mode & VWRITE))
error = EISDIR;
else {
mutex_enter(&cp->c_statelock);
error = cachefs_access_local(cp, mode, cr);
mutex_exit(&cp->c_statelock);
if (!error) {
if ((vap->va_mask & AT_SIZE) &&
(vp->v_type == VREG)) {
vap->va_mask = AT_SIZE;
error = cachefs_setattr_common(vp,
vap, 0, cr, NULL);
}
}
}
if (error) {
VN_RELE(vp);
} else
*vpp = vp;
return (error);
}
/* give up if cannot modify the cache */
if (CFS_ISFS_WRITE_AROUND(fscp)) {
mutex_exit(&dcp->c_statelock);
error = ETIMEDOUT;
goto out;
}
/* check access */
if (error = cachefs_access_local(dcp, VWRITE, cr)) {
mutex_exit(&dcp->c_statelock);
goto out;
}
/* mark dir as modified */
cachefs_modified(dcp);
mutex_exit(&dcp->c_statelock);
/* must be privileged to set sticky bit */
if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr) != 0)
vap->va_mode &= ~VSVTX;
/* make up a reasonable set of attributes */
cachefs_attr_setup(vap, &va, dcp, cr);
/* create the cnode */
error = cachefs_cnode_create(fscp, &va, 0, &ncp);
if (error)
goto out;
mutex_enter(&ncp->c_statelock);
/* get the front file now instead of later */
if (vap->va_type == VREG) {
error = cachefs_getfrontfile(ncp);
if (error) {
mutex_exit(&ncp->c_statelock);
goto out;
}
ASSERT(ncp->c_frontvp != NULL);
ASSERT((ncp->c_flags & CN_ALLOC_PENDING) == 0);
ncp->c_metadata.md_flags |= MD_POPULATED;
} else {
ASSERT(ncp->c_flags & CN_ALLOC_PENDING);
if (ncp->c_filegrp->fg_flags & CFS_FG_ALLOC_ATTR) {
(void) filegrp_allocattr(ncp->c_filegrp);
}
error = filegrp_create_metadata(ncp->c_filegrp,
&ncp->c_metadata, &ncp->c_id);
if (error) {
mutex_exit(&ncp->c_statelock);
goto out;
}
ncp->c_flags &= ~CN_ALLOC_PENDING;
}
mutex_enter(&dcp->c_statelock);
cachefs_creategid(dcp, ncp, vap, cr);
cachefs_createacl(dcp, ncp);
mutex_exit(&dcp->c_statelock);
/* set times on the file */
gethrestime(¤t_time);
ncp->c_metadata.md_vattr.va_atime = current_time;
ncp->c_metadata.md_localctime = current_time;
ncp->c_metadata.md_localmtime = current_time;
ncp->c_metadata.md_flags |= MD_LOCALMTIME | MD_LOCALCTIME;
/* reserve space for the daemon cid mapping */
error = cachefs_dlog_cidmap(fscp);
if (error) {
mutex_exit(&ncp->c_statelock);
goto out;
}
ncp->c_metadata.md_flags |= MD_MAPPING;
/* mark the new file as modified */
if (cachefs_modified_alloc(ncp)) {
mutex_exit(&ncp->c_statelock);
error = ENOSPC;
goto out;
}
ncp->c_flags |= CN_UPDATED;
/*
* write the metadata now rather than waiting until
* inactive so that if there's no space we can let
* the caller know.
*/
ASSERT((ncp->c_flags & CN_ALLOC_PENDING) == 0);
ASSERT((ncp->c_filegrp->fg_flags & CFS_FG_ALLOC_ATTR) == 0);
error = filegrp_write_metadata(ncp->c_filegrp,
&ncp->c_id, &ncp->c_metadata);
if (error) {
mutex_exit(&ncp->c_statelock);
goto out;
}
/* log the operation */
commit = cachefs_dlog_create(fscp, dcp, nm, vap, exclusive,
mode, ncp, 0, cr);
if (commit == 0) {
mutex_exit(&ncp->c_statelock);
error = ENOSPC;
goto out;
}
mutex_exit(&ncp->c_statelock);
mutex_enter(&dcp->c_statelock);
/* update parent dir times */
dcp->c_metadata.md_localmtime = current_time;
dcp->c_metadata.md_flags |= MD_LOCALMTIME;
dcp->c_flags |= CN_UPDATED;
/* enter new file name in the parent directory */
if (dcp->c_metadata.md_flags & MD_POPULATED) {
error = cachefs_dir_enter(dcp, nm, &ncp->c_cookie,
&ncp->c_id, 0);
if (error) {
cachefs_nocache(dcp);
mutex_exit(&dcp->c_statelock);
error = ETIMEDOUT;
goto out;
}
dnlc_enter(dvp, nm, CTOV(ncp));
} else {
mutex_exit(&dcp->c_statelock);
error = ETIMEDOUT;
goto out;
}
mutex_exit(&dcp->c_statelock);
out:
rw_exit(&dcp->c_rwlock);
if (commit) {
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX bob: fix on panic */
}
}
if (error) {
/* destroy the cnode we created */
if (ncp) {
mutex_enter(&ncp->c_statelock);
ncp->c_flags |= CN_DESTROY;
mutex_exit(&ncp->c_statelock);
VN_RELE(CTOV(ncp));
}
} else {
*vpp = CTOV(ncp);
}
return (error);
}
/*ARGSUSED*/
static int
cachefs_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct,
int flags)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
cachefscache_t *cachep = fscp->fs_cache;
int error = 0;
int held = 0;
int connected = 0;
size_t namlen;
vnode_t *vp = NULL;
int vfslock = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_remove: ENTER dvp %p name %s\n",
(void *)dvp, nm);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (fscp->fs_cache->c_flags & (CACHE_NOFILL | CACHE_NOCACHE))
ASSERT(dcp->c_flags & CN_NOCACHE);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the remove operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(dcp);
for (;;) {
if (vfslock) {
vn_vfsunlock(vp);
vfslock = 0;
}
if (vp) {
VN_RELE(vp);
vp = NULL;
}
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
/* if disconnected, do some extra error checking */
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
/* check permissions */
mutex_enter(&dcp->c_statelock);
error = cachefs_access_local(dcp, (VEXEC|VWRITE), cr);
mutex_exit(&dcp->c_statelock);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
if (error)
break;
namlen = strlen(nm);
if (namlen == 0) {
error = EINVAL;
break;
}
/* cannot remove . and .. */
if (nm[0] == '.') {
if (namlen == 1) {
error = EINVAL;
break;
} else if (namlen == 2 && nm[1] == '.') {
error = EEXIST;
break;
}
}
}
/* get the cnode of the file to delete */
error = cachefs_lookup_common(dvp, nm, &vp, NULL, 0, NULL, cr);
if (error) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_REMOVE)) {
struct fid foo;
bzero(&foo, sizeof (foo));
cachefs_log_remove(cachep, error,
fscp->fs_cfsvfsp, &foo, 0, crgetuid(cr));
}
break;
}
if (vp->v_type == VDIR) {
/* must be privileged to remove dirs with unlink() */
if ((error = secpolicy_fs_linkdir(cr, vp->v_vfsp)) != 0)
break;
/* see ufs_dirremove for why this is done, mount race */
if (vn_vfswlock(vp)) {
error = EBUSY;
break;
}
vfslock = 1;
if (vn_mountedvfs(vp) != NULL) {
error = EBUSY;
break;
}
}
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_remove_connected(dvp, nm, cr, vp);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_remove_disconnected(dvp, nm, cr,
vp);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
#if 0
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_REMOVE))
cachefs_log_remove(cachep, error, fscp->fs_cfsvfsp,
&cp->c_metadata.md_cookie, cp->c_id.cid_fileno,
crgetuid(cr));
#endif
if (held)
cachefs_cd_release(fscp);
if (vfslock)
vn_vfsunlock(vp);
if (vp)
VN_RELE(vp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_remove: EXIT dvp %p\n", (void *)dvp);
#endif
return (error);
}
int
cachefs_remove_connected(vnode_t *dvp, char *nm, cred_t *cr, vnode_t *vp)
{
cnode_t *dcp = VTOC(dvp);
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error = 0;
/*
* Acquire the rwlock (WRITER) on the directory to prevent other
* activity on the directory.
*/
rw_enter(&dcp->c_rwlock, RW_WRITER);
/* purge dnlc of this entry so can get accurate vnode count */
dnlc_purge_vp(vp);
/*
* If the cnode is active, make a link to the file
* so operations on the file will continue.
*/
if ((vp->v_type != VDIR) &&
!((vp->v_count == 1) || ((vp->v_count == 2) && cp->c_ipending))) {
error = cachefs_remove_dolink(dvp, vp, nm, cr);
if (error)
goto out;
}
/* else call backfs NFSv4 handler if NFSv4 */
else if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_remove_backfs_nfsv4(dvp, nm, cr, vp);
goto out;
}
/* else drop the backvp so nfs does not do rename */
else if (cp->c_backvp) {
mutex_enter(&cp->c_statelock);
if (cp->c_backvp) {
VN_RELE(cp->c_backvp);
cp->c_backvp = NULL;
}
mutex_exit(&cp->c_statelock);
}
mutex_enter(&dcp->c_statelock);
/* get the backvp */
if (dcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, dcp);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
}
/* check directory consistency */
error = CFSOP_CHECK_COBJECT(fscp, dcp, 0, cr);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
/* perform the remove on the back fs */
error = VOP_REMOVE(dcp->c_backvp, nm, cr, NULL, 0);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
/* the dir has been modified */
CFSOP_MODIFY_COBJECT(fscp, dcp, cr);
/* remove the entry from the populated directory */
if (CFS_ISFS_NONSHARED(fscp) &&
(dcp->c_metadata.md_flags & MD_POPULATED)) {
error = cachefs_dir_rmentry(dcp, nm);
if (error) {
cachefs_nocache(dcp);
error = 0;
}
}
mutex_exit(&dcp->c_statelock);
/* fix up the file we deleted */
mutex_enter(&cp->c_statelock);
if (cp->c_attr.va_nlink == 1)
cp->c_flags |= CN_DESTROY;
else
cp->c_flags |= CN_UPDATED;
cp->c_attr.va_nlink--;
CFSOP_MODIFY_COBJECT(fscp, cp, cr);
mutex_exit(&cp->c_statelock);
out:
rw_exit(&dcp->c_rwlock);
return (error);
}
/*
* cachefs_remove_backfs_nfsv4
*
* Call NFSv4 back filesystem to handle the remove (cachefs
* pass-through support for NFSv4).
*/
int
cachefs_remove_backfs_nfsv4(vnode_t *dvp, char *nm, cred_t *cr, vnode_t *vp)
{
cnode_t *dcp = VTOC(dvp);
cnode_t *cp = VTOC(vp);
vnode_t *dbackvp;
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error = 0;
/*
* For NFSv4 pass-through to work, only connected operation
* is supported, the cnode backvp must exist, and cachefs
* optional (eg., disconnectable) flags are turned off. Assert
* these conditions for the getattr operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* Should hold the directory readwrite lock to update directory */
ASSERT(RW_WRITE_HELD(&dcp->c_rwlock));
/*
* Update attributes for directory. Note that
* CFSOP_CHECK_COBJECT asserts for c_statelock being
* held, so grab it before calling the routine.
*/
mutex_enter(&dcp->c_statelock);
error = CFSOP_CHECK_COBJECT(fscp, dcp, 0, cr);
mutex_exit(&dcp->c_statelock);
if (error)
goto out;
/*
* Update attributes for cp. Note that CFSOP_CHECK_COBJECT
* asserts for c_statelock being held, so grab it before
* calling the routine.
*/
mutex_enter(&cp->c_statelock);
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
if (error) {
mutex_exit(&cp->c_statelock);
goto out;
}
/*
* Drop the backvp so nfs if the link count is 1 so that
* nfs does not do rename. Ensure that we will destroy the cnode
* since this cnode no longer contains the backvp. Note that we
* maintain lock on this cnode to prevent change till the remove
* completes, otherwise other operations will encounter an ESTALE
* if they try to use the cnode with CN_DESTROY set (see
* cachefs_get_backvp()), or change the state of the cnode
* while we're removing it.
*/
if (cp->c_attr.va_nlink == 1) {
/*
* The unldvp information is created for the case
* when there is more than one reference on the
* vnode when a remove operation is called. If the
* remove itself was holding a reference to the
* vnode, then a subsequent remove will remove the
* backvp, so we need to get rid of the unldvp
* before removing the backvp. An alternate would
* be to simply ignore the remove and let the
* inactivation routine do the deletion of the
* unldvp.
*/
if (cp->c_unldvp) {
VN_RELE(cp->c_unldvp);
cachefs_kmem_free(cp->c_unlname, MAXNAMELEN);
crfree(cp->c_unlcred);
cp->c_unldvp = NULL;
cp->c_unlcred = NULL;
}
cp->c_flags |= CN_DESTROY;
cp->c_attr.va_nlink = 0;
VN_RELE(cp->c_backvp);
cp->c_backvp = NULL;
}
/* perform the remove on back fs after extracting directory backvp */
mutex_enter(&dcp->c_statelock);
dbackvp = dcp->c_backvp;
mutex_exit(&dcp->c_statelock);
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_remove (nfsv4): dcp %p, dbackvp %p, name %s\n",
dcp, dbackvp, nm));
error = VOP_REMOVE(dbackvp, nm, cr, NULL, 0);
if (error) {
mutex_exit(&cp->c_statelock);
goto out;
}
/* fix up the file we deleted, if not destroying the cnode */
if ((cp->c_flags & CN_DESTROY) == 0) {
cp->c_attr.va_nlink--;
cp->c_flags |= CN_UPDATED;
}
mutex_exit(&cp->c_statelock);
out:
return (error);
}
int
cachefs_remove_disconnected(vnode_t *dvp, char *nm, cred_t *cr,
vnode_t *vp)
{
cnode_t *dcp = VTOC(dvp);
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error = 0;
off_t commit = 0;
timestruc_t current_time;
if (CFS_ISFS_WRITE_AROUND(fscp))
return (ETIMEDOUT);
if (cp->c_metadata.md_flags & MD_NEEDATTRS)
return (ETIMEDOUT);
/*
* Acquire the rwlock (WRITER) on the directory to prevent other
* activity on the directory.
*/
rw_enter(&dcp->c_rwlock, RW_WRITER);
/* dir must be populated */
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
error = ETIMEDOUT;
goto out;
}
mutex_enter(&dcp->c_statelock);
mutex_enter(&cp->c_statelock);
error = cachefs_stickyrmchk(dcp, cp, cr);
mutex_exit(&cp->c_statelock);
mutex_exit(&dcp->c_statelock);
if (error)
goto out;
/* purge dnlc of this entry so can get accurate vnode count */
dnlc_purge_vp(vp);
/*
* If the cnode is active, make a link to the file
* so operations on the file will continue.
*/
if ((vp->v_type != VDIR) &&
!((vp->v_count == 1) || ((vp->v_count == 2) && cp->c_ipending))) {
error = cachefs_remove_dolink(dvp, vp, nm, cr);
if (error)
goto out;
}
if (cp->c_attr.va_nlink > 1) {
mutex_enter(&cp->c_statelock);
if (cachefs_modified_alloc(cp)) {
mutex_exit(&cp->c_statelock);
error = ENOSPC;
goto out;
}
if ((cp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
mutex_exit(&cp->c_statelock);
error = ENOSPC;
goto out;
}
cp->c_metadata.md_flags |= MD_MAPPING;
cp->c_flags |= CN_UPDATED;
}
mutex_exit(&cp->c_statelock);
}
/* log the remove */
commit = cachefs_dlog_remove(fscp, dcp, nm, cp, cr);
if (commit == 0) {
error = ENOSPC;
goto out;
}
/* remove the file from the dir */
mutex_enter(&dcp->c_statelock);
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
mutex_exit(&dcp->c_statelock);
error = ETIMEDOUT;
goto out;
}
cachefs_modified(dcp);
error = cachefs_dir_rmentry(dcp, nm);
if (error) {
mutex_exit(&dcp->c_statelock);
if (error == ENOTDIR)
error = ETIMEDOUT;
goto out;
}
/* update parent dir times */
gethrestime(¤t_time);
dcp->c_metadata.md_localctime = current_time;
dcp->c_metadata.md_localmtime = current_time;
dcp->c_metadata.md_flags |= MD_LOCALCTIME | MD_LOCALMTIME;
dcp->c_flags |= CN_UPDATED;
mutex_exit(&dcp->c_statelock);
/* adjust file we are deleting */
mutex_enter(&cp->c_statelock);
cp->c_attr.va_nlink--;
cp->c_metadata.md_localctime = current_time;
cp->c_metadata.md_flags |= MD_LOCALCTIME;
if (cp->c_attr.va_nlink == 0) {
cp->c_flags |= CN_DESTROY;
} else {
cp->c_flags |= CN_UPDATED;
}
mutex_exit(&cp->c_statelock);
out:
if (commit) {
/* commit the log entry */
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX bob: fix on panic */
}
}
rw_exit(&dcp->c_rwlock);
return (error);
}
/*ARGSUSED*/
static int
cachefs_link(vnode_t *tdvp, vnode_t *fvp, char *tnm, cred_t *cr,
caller_context_t *ct, int flags)
{
fscache_t *fscp = VFS_TO_FSCACHE(tdvp->v_vfsp);
cnode_t *tdcp = VTOC(tdvp);
struct vnode *realvp;
int error = 0;
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_link: ENTER fvp %p tdvp %p tnm %s\n",
(void *)fvp, (void *)tdvp, tnm);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (fscp->fs_cache->c_flags & (CACHE_NOFILL | CACHE_NOCACHE))
ASSERT(tdcp->c_flags & CN_NOCACHE);
if (VOP_REALVP(fvp, &realvp, ct) == 0) {
fvp = realvp;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the link operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(tdcp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
rw_exit(&tdcp->c_rwlock);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
rw_enter(&tdcp->c_rwlock, RW_WRITER);
held = 1;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_link_connected(tdvp, fvp, tnm, cr);
if (CFS_TIMEOUT(fscp, error)) {
rw_exit(&tdcp->c_rwlock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_link_disconnected(tdvp, fvp, tnm,
cr);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
if (held) {
rw_exit(&tdcp->c_rwlock);
cachefs_cd_release(fscp);
}
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_link: EXIT fvp %p tdvp %p tnm %s\n",
(void *)fvp, (void *)tdvp, tnm);
#endif
return (error);
}
static int
cachefs_link_connected(vnode_t *tdvp, vnode_t *fvp, char *tnm, cred_t *cr)
{
cnode_t *tdcp = VTOC(tdvp);
cnode_t *fcp = VTOC(fvp);
fscache_t *fscp = VFS_TO_FSCACHE(tdvp->v_vfsp);
int error = 0;
vnode_t *backvp = NULL;
if (tdcp != fcp) {
mutex_enter(&fcp->c_statelock);
if (fcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, fcp);
if (error) {
mutex_exit(&fcp->c_statelock);
goto out;
}
}
error = CFSOP_CHECK_COBJECT(fscp, fcp, 0, cr);
if (error) {
mutex_exit(&fcp->c_statelock);
goto out;
}
backvp = fcp->c_backvp;
VN_HOLD(backvp);
mutex_exit(&fcp->c_statelock);
}
mutex_enter(&tdcp->c_statelock);
/* get backvp of target directory */
if (tdcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, tdcp);
if (error) {
mutex_exit(&tdcp->c_statelock);
goto out;
}
}
/* consistency check target directory */
error = CFSOP_CHECK_COBJECT(fscp, tdcp, 0, cr);
if (error) {
mutex_exit(&tdcp->c_statelock);
goto out;
}
if (backvp == NULL) {
backvp = tdcp->c_backvp;
VN_HOLD(backvp);
}
/* perform the link on the back fs */
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_link (nfsv4): tdcp %p, tdbackvp %p, "
"name %s\n", tdcp, tdcp->c_backvp, tnm));
error = VOP_LINK(tdcp->c_backvp, backvp, tnm, cr, NULL, 0);
if (error) {
mutex_exit(&tdcp->c_statelock);
goto out;
}
CFSOP_MODIFY_COBJECT(fscp, tdcp, cr);
/* if the dir is populated, add the new link */
if (CFS_ISFS_NONSHARED(fscp) &&
(tdcp->c_metadata.md_flags & MD_POPULATED)) {
error = cachefs_dir_enter(tdcp, tnm, &fcp->c_cookie,
&fcp->c_id, SM_ASYNC);
if (error) {
cachefs_nocache(tdcp);
error = 0;
}
}
mutex_exit(&tdcp->c_statelock);
/* get the new link count on the file */
mutex_enter(&fcp->c_statelock);
fcp->c_flags |= CN_UPDATED;
CFSOP_MODIFY_COBJECT(fscp, fcp, cr);
if (fcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, fcp);
if (error) {
mutex_exit(&fcp->c_statelock);
goto out;
}
}
/* XXX bob: given what modify_cobject does this seems unnecessary */
fcp->c_attr.va_mask = AT_ALL;
error = VOP_GETATTR(fcp->c_backvp, &fcp->c_attr, 0, cr, NULL);
mutex_exit(&fcp->c_statelock);
out:
if (backvp)
VN_RELE(backvp);
return (error);
}
static int
cachefs_link_disconnected(vnode_t *tdvp, vnode_t *fvp, char *tnm,
cred_t *cr)
{
cnode_t *tdcp = VTOC(tdvp);
cnode_t *fcp = VTOC(fvp);
fscache_t *fscp = VFS_TO_FSCACHE(tdvp->v_vfsp);
int error = 0;
timestruc_t current_time;
off_t commit = 0;
if (fvp->v_type == VDIR && secpolicy_fs_linkdir(cr, fvp->v_vfsp) != 0 ||
fcp->c_attr.va_uid != crgetuid(cr) && secpolicy_basic_link(cr) != 0)
return (EPERM);
if (CFS_ISFS_WRITE_AROUND(fscp))
return (ETIMEDOUT);
if (fcp->c_metadata.md_flags & MD_NEEDATTRS)
return (ETIMEDOUT);
mutex_enter(&tdcp->c_statelock);
/* check permissions */
if (error = cachefs_access_local(tdcp, (VEXEC|VWRITE), cr)) {
mutex_exit(&tdcp->c_statelock);
goto out;
}
/* the directory front file must be populated */
if ((tdcp->c_metadata.md_flags & MD_POPULATED) == 0) {
error = ETIMEDOUT;
mutex_exit(&tdcp->c_statelock);
goto out;
}
/* make sure tnm does not already exist in the directory */
error = cachefs_dir_look(tdcp, tnm, NULL, NULL, NULL, NULL);
if (error == ENOTDIR) {
error = ETIMEDOUT;
mutex_exit(&tdcp->c_statelock);
goto out;
}
if (error != ENOENT) {
error = EEXIST;
mutex_exit(&tdcp->c_statelock);
goto out;
}
mutex_enter(&fcp->c_statelock);
/* create a mapping for the file if necessary */
if ((fcp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
mutex_exit(&fcp->c_statelock);
mutex_exit(&tdcp->c_statelock);
error = ENOSPC;
goto out;
}
fcp->c_metadata.md_flags |= MD_MAPPING;
fcp->c_flags |= CN_UPDATED;
}
/* mark file as modified */
if (cachefs_modified_alloc(fcp)) {
mutex_exit(&fcp->c_statelock);
mutex_exit(&tdcp->c_statelock);
error = ENOSPC;
goto out;
}
mutex_exit(&fcp->c_statelock);
/* log the operation */
commit = cachefs_dlog_link(fscp, tdcp, tnm, fcp, cr);
if (commit == 0) {
mutex_exit(&tdcp->c_statelock);
error = ENOSPC;
goto out;
}
gethrestime(¤t_time);
/* make the new link */
cachefs_modified(tdcp);
error = cachefs_dir_enter(tdcp, tnm, &fcp->c_cookie,
&fcp->c_id, SM_ASYNC);
if (error) {
error = 0;
mutex_exit(&tdcp->c_statelock);
goto out;
}
/* Update mtime/ctime of parent dir */
tdcp->c_metadata.md_localmtime = current_time;
tdcp->c_metadata.md_localctime = current_time;
tdcp->c_metadata.md_flags |= MD_LOCALCTIME | MD_LOCALMTIME;
tdcp->c_flags |= CN_UPDATED;
mutex_exit(&tdcp->c_statelock);
/* update the file we linked to */
mutex_enter(&fcp->c_statelock);
fcp->c_attr.va_nlink++;
fcp->c_metadata.md_localctime = current_time;
fcp->c_metadata.md_flags |= MD_LOCALCTIME;
fcp->c_flags |= CN_UPDATED;
mutex_exit(&fcp->c_statelock);
out:
if (commit) {
/* commit the log entry */
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX bob: fix on panic */
}
}
return (error);
}
/*
* Serialize all renames in CFS, to avoid deadlocks - We have to hold two
* cnodes atomically.
*/
kmutex_t cachefs_rename_lock;
/*ARGSUSED*/
static int
cachefs_rename(vnode_t *odvp, char *onm, vnode_t *ndvp,
char *nnm, cred_t *cr, caller_context_t *ct, int flags)
{
fscache_t *fscp = C_TO_FSCACHE(VTOC(odvp));
cachefscache_t *cachep = fscp->fs_cache;
int error = 0;
int held = 0;
int connected = 0;
vnode_t *delvp = NULL;
vnode_t *tvp = NULL;
int vfslock = 0;
struct vnode *realvp;
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
if (VOP_REALVP(ndvp, &realvp, ct) == 0)
ndvp = realvp;
/*
* if the fs NOFILL or NOCACHE flags are on, then the old and new
* directory cnodes better indicate NOCACHE mode as well.
*/
ASSERT(
(fscp->fs_cache->c_flags & (CACHE_NOFILL | CACHE_NOCACHE)) == 0 ||
((VTOC(odvp)->c_flags & CN_NOCACHE) &&
(VTOC(ndvp)->c_flags & CN_NOCACHE)));
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the rename operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(VTOC(odvp));
CFS_BACKFS_NFSV4_ASSERT_CNODE(VTOC(ndvp));
for (;;) {
if (vfslock) {
vn_vfsunlock(delvp);
vfslock = 0;
}
if (delvp) {
VN_RELE(delvp);
delvp = NULL;
}
/* get (or renew) access to the file system */
if (held) {
/* Won't loop for NFSv4 connected support */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
/* sanity check */
if ((odvp->v_type != VDIR) || (ndvp->v_type != VDIR)) {
error = EINVAL;
break;
}
/* cannot rename from or to . or .. */
if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0) {
error = EINVAL;
break;
}
if (odvp != ndvp) {
/*
* if moving a directory, its notion
* of ".." will change
*/
error = cachefs_lookup_common(odvp, onm, &tvp,
NULL, 0, NULL, cr);
if (error == 0) {
ASSERT(tvp != NULL);
if (tvp->v_type == VDIR) {
cnode_t *cp = VTOC(tvp);
dnlc_remove(tvp, "..");
mutex_enter(&cp->c_statelock);
CFSOP_MODIFY_COBJECT(fscp, cp, cr);
mutex_exit(&cp->c_statelock);
}
} else {
tvp = NULL;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
}
/* get the cnode if file being deleted */
error = cachefs_lookup_common(ndvp, nnm, &delvp, NULL, 0,
NULL, cr);
if (error) {
delvp = NULL;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
if (error != ENOENT)
break;
}
if (delvp && delvp->v_type == VDIR) {
/* see ufs_dirremove for why this is done, mount race */
if (vn_vfswlock(delvp)) {
error = EBUSY;
break;
}
vfslock = 1;
if (vn_mountedvfs(delvp) != NULL) {
error = EBUSY;
break;
}
}
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_rename_connected(odvp, onm,
ndvp, nnm, cr, delvp);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_rename_disconnected(odvp, onm,
ndvp, nnm, cr, delvp);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_RENAME)) {
struct fid gone;
bzero(&gone, sizeof (gone));
gone.fid_len = MAXFIDSZ;
if (delvp != NULL)
(void) VOP_FID(delvp, &gone, ct);
cachefs_log_rename(cachep, error, fscp->fs_cfsvfsp,
&gone, 0, (delvp != NULL), crgetuid(cr));
}
if (held)
cachefs_cd_release(fscp);
if (vfslock)
vn_vfsunlock(delvp);
if (delvp)
VN_RELE(delvp);
if (tvp)
VN_RELE(tvp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
return (error);
}
static int
cachefs_rename_connected(vnode_t *odvp, char *onm, vnode_t *ndvp,
char *nnm, cred_t *cr, vnode_t *delvp)
{
cnode_t *odcp = VTOC(odvp);
cnode_t *ndcp = VTOC(ndvp);
vnode_t *revp = NULL;
cnode_t *recp;
cnode_t *delcp;
fscache_t *fscp = C_TO_FSCACHE(odcp);
int error = 0;
struct fid cookie;
struct fid *cookiep;
cfs_cid_t cid;
int gotdirent;
/* find the file we are renaming */
error = cachefs_lookup_common(odvp, onm, &revp, NULL, 0, NULL, cr);
if (error)
return (error);
recp = VTOC(revp);
/*
* To avoid deadlock, we acquire this global rename lock before
* we try to get the locks for the source and target directories.
*/
mutex_enter(&cachefs_rename_lock);
rw_enter(&odcp->c_rwlock, RW_WRITER);
if (odcp != ndcp) {
rw_enter(&ndcp->c_rwlock, RW_WRITER);
}
mutex_exit(&cachefs_rename_lock);
ASSERT((odcp->c_flags & CN_ASYNC_POP_WORKING) == 0);
ASSERT((ndcp->c_flags & CN_ASYNC_POP_WORKING) == 0);
mutex_enter(&odcp->c_statelock);
if (odcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, odcp);
if (error) {
mutex_exit(&odcp->c_statelock);
goto out;
}
}
error = CFSOP_CHECK_COBJECT(fscp, odcp, 0, cr);
if (error) {
mutex_exit(&odcp->c_statelock);
goto out;
}
mutex_exit(&odcp->c_statelock);
if (odcp != ndcp) {
mutex_enter(&ndcp->c_statelock);
if (ndcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, ndcp);
if (error) {
mutex_exit(&ndcp->c_statelock);
goto out;
}
}
error = CFSOP_CHECK_COBJECT(fscp, ndcp, 0, cr);
if (error) {
mutex_exit(&ndcp->c_statelock);
goto out;
}
mutex_exit(&ndcp->c_statelock);
}
/* if a file is being deleted because of this rename */
if (delvp) {
/* if src and dest file are same */
if (delvp == revp) {
error = 0;
goto out;
}
/*
* If the cnode is active, make a link to the file
* so operations on the file will continue.
*/
dnlc_purge_vp(delvp);
delcp = VTOC(delvp);
if ((delvp->v_type != VDIR) &&
!((delvp->v_count == 1) ||
((delvp->v_count == 2) && delcp->c_ipending))) {
error = cachefs_remove_dolink(ndvp, delvp, nnm, cr);
if (error)
goto out;
}
}
/* do the rename on the back fs */
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_rename (nfsv4): odcp %p, odbackvp %p, "
" ndcp %p, ndbackvp %p, onm %s, nnm %s\n",
odcp, odcp->c_backvp, ndcp, ndcp->c_backvp, onm, nnm));
error = VOP_RENAME(odcp->c_backvp, onm, ndcp->c_backvp, nnm, cr, NULL,
0);
if (error)
goto out;
/* purge mappings to file in the old directory */
dnlc_purge_vp(odvp);
/* purge mappings in the new dir if we deleted a file */
if (delvp && (odvp != ndvp))
dnlc_purge_vp(ndvp);
/* update the file we just deleted */
if (delvp) {
mutex_enter(&delcp->c_statelock);
if (delcp->c_attr.va_nlink == 1) {
delcp->c_flags |= CN_DESTROY;
} else {
delcp->c_flags |= CN_UPDATED;
}
delcp->c_attr.va_nlink--;
CFSOP_MODIFY_COBJECT(fscp, delcp, cr);
mutex_exit(&delcp->c_statelock);
}
/* find the entry in the old directory */
mutex_enter(&odcp->c_statelock);
gotdirent = 0;
cookiep = NULL;
if (CFS_ISFS_NONSHARED(fscp) &&
(odcp->c_metadata.md_flags & MD_POPULATED)) {
error = cachefs_dir_look(odcp, onm, &cookie,
NULL, NULL, &cid);
if (error == 0 || error == EINVAL) {
gotdirent = 1;
if (error == 0)
cookiep = &cookie;
} else {
cachefs_inval_object(odcp);
}
}
error = 0;
/* remove the directory entry from the old directory */
if (gotdirent) {
error = cachefs_dir_rmentry(odcp, onm);
if (error) {
cachefs_nocache(odcp);
error = 0;
}
}
CFSOP_MODIFY_COBJECT(fscp, odcp, cr);
mutex_exit(&odcp->c_statelock);
/* install the directory entry in the new directory */
mutex_enter(&ndcp->c_statelock);
if (CFS_ISFS_NONSHARED(fscp) &&
(ndcp->c_metadata.md_flags & MD_POPULATED)) {
error = 1;
if (gotdirent) {
ASSERT(cid.cid_fileno != 0);
error = 0;
if (delvp) {
error = cachefs_dir_rmentry(ndcp, nnm);
}
if (error == 0) {
error = cachefs_dir_enter(ndcp, nnm, cookiep,
&cid, SM_ASYNC);
}
}
if (error) {
cachefs_nocache(ndcp);
error = 0;
}
}
if (odcp != ndcp)
CFSOP_MODIFY_COBJECT(fscp, ndcp, cr);
mutex_exit(&ndcp->c_statelock);
/* ctime of renamed file has changed */
mutex_enter(&recp->c_statelock);
CFSOP_MODIFY_COBJECT(fscp, recp, cr);
mutex_exit(&recp->c_statelock);
out:
if (odcp != ndcp)
rw_exit(&ndcp->c_rwlock);
rw_exit(&odcp->c_rwlock);
VN_RELE(revp);
return (error);
}
static int
cachefs_rename_disconnected(vnode_t *odvp, char *onm, vnode_t *ndvp,
char *nnm, cred_t *cr, vnode_t *delvp)
{
cnode_t *odcp = VTOC(odvp);
cnode_t *ndcp = VTOC(ndvp);
cnode_t *delcp = NULL;
vnode_t *revp = NULL;
cnode_t *recp;
fscache_t *fscp = C_TO_FSCACHE(odcp);
int error = 0;
struct fid cookie;
struct fid *cookiep;
cfs_cid_t cid;
off_t commit = 0;
timestruc_t current_time;
if (CFS_ISFS_WRITE_AROUND(fscp))
return (ETIMEDOUT);
/* find the file we are renaming */
error = cachefs_lookup_common(odvp, onm, &revp, NULL, 0, NULL, cr);
if (error)
return (error);
recp = VTOC(revp);
/*
* To avoid deadlock, we acquire this global rename lock before
* we try to get the locks for the source and target directories.
*/
mutex_enter(&cachefs_rename_lock);
rw_enter(&odcp->c_rwlock, RW_WRITER);
if (odcp != ndcp) {
rw_enter(&ndcp->c_rwlock, RW_WRITER);
}
mutex_exit(&cachefs_rename_lock);
if (recp->c_metadata.md_flags & MD_NEEDATTRS) {
error = ETIMEDOUT;
goto out;
}
if ((recp->c_metadata.md_flags & MD_MAPPING) == 0) {
mutex_enter(&recp->c_statelock);
if ((recp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
mutex_exit(&recp->c_statelock);
error = ENOSPC;
goto out;
}
recp->c_metadata.md_flags |= MD_MAPPING;
recp->c_flags |= CN_UPDATED;
}
mutex_exit(&recp->c_statelock);
}
/* check permissions */
/* XXX clean up this mutex junk sometime */
mutex_enter(&odcp->c_statelock);
error = cachefs_access_local(odcp, (VEXEC|VWRITE), cr);
mutex_exit(&odcp->c_statelock);
if (error != 0)
goto out;
mutex_enter(&ndcp->c_statelock);
error = cachefs_access_local(ndcp, (VEXEC|VWRITE), cr);
mutex_exit(&ndcp->c_statelock);
if (error != 0)
goto out;
mutex_enter(&odcp->c_statelock);
error = cachefs_stickyrmchk(odcp, recp, cr);
mutex_exit(&odcp->c_statelock);
if (error != 0)
goto out;
/* dirs must be populated */
if (((odcp->c_metadata.md_flags & MD_POPULATED) == 0) ||
((ndcp->c_metadata.md_flags & MD_POPULATED) == 0)) {
error = ETIMEDOUT;
goto out;
}
/* for now do not allow moving dirs because could cause cycles */
if ((((revp->v_type == VDIR) && (odvp != ndvp))) ||
(revp == odvp)) {
error = ETIMEDOUT;
goto out;
}
/* if a file is being deleted because of this rename */
if (delvp) {
delcp = VTOC(delvp);
/* if src and dest file are the same */
if (delvp == revp) {
error = 0;
goto out;
}
if (delcp->c_metadata.md_flags & MD_NEEDATTRS) {
error = ETIMEDOUT;
goto out;
}
/* if there are hard links to this file */
if (delcp->c_attr.va_nlink > 1) {
mutex_enter(&delcp->c_statelock);
if (cachefs_modified_alloc(delcp)) {
mutex_exit(&delcp->c_statelock);
error = ENOSPC;
goto out;
}
if ((delcp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
mutex_exit(&delcp->c_statelock);
error = ENOSPC;
goto out;
}
delcp->c_metadata.md_flags |= MD_MAPPING;
delcp->c_flags |= CN_UPDATED;
}
mutex_exit(&delcp->c_statelock);
}
/* make sure we can delete file */
mutex_enter(&ndcp->c_statelock);
error = cachefs_stickyrmchk(ndcp, delcp, cr);
mutex_exit(&ndcp->c_statelock);
if (error != 0)
goto out;
/*
* If the cnode is active, make a link to the file
* so operations on the file will continue.
*/
dnlc_purge_vp(delvp);
if ((delvp->v_type != VDIR) &&
!((delvp->v_count == 1) ||
((delvp->v_count == 2) && delcp->c_ipending))) {
error = cachefs_remove_dolink(ndvp, delvp, nnm, cr);
if (error)
goto out;
}
}
/* purge mappings to file in the old directory */
dnlc_purge_vp(odvp);
/* purge mappings in the new dir if we deleted a file */
if (delvp && (odvp != ndvp))
dnlc_purge_vp(ndvp);
/* find the entry in the old directory */
mutex_enter(&odcp->c_statelock);
if ((odcp->c_metadata.md_flags & MD_POPULATED) == 0) {
mutex_exit(&odcp->c_statelock);
error = ETIMEDOUT;
goto out;
}
cookiep = NULL;
error = cachefs_dir_look(odcp, onm, &cookie, NULL, NULL, &cid);
if (error == 0 || error == EINVAL) {
if (error == 0)
cookiep = &cookie;
} else {
mutex_exit(&odcp->c_statelock);
if (error == ENOTDIR)
error = ETIMEDOUT;
goto out;
}
error = 0;
/* write the log entry */
commit = cachefs_dlog_rename(fscp, odcp, onm, ndcp, nnm, cr,
recp, delcp);
if (commit == 0) {
mutex_exit(&odcp->c_statelock);
error = ENOSPC;
goto out;
}
/* remove the directory entry from the old directory */
cachefs_modified(odcp);
error = cachefs_dir_rmentry(odcp, onm);
if (error) {
mutex_exit(&odcp->c_statelock);
if (error == ENOTDIR)
error = ETIMEDOUT;
goto out;
}
mutex_exit(&odcp->c_statelock);
/* install the directory entry in the new directory */
mutex_enter(&ndcp->c_statelock);
error = ENOTDIR;
if (ndcp->c_metadata.md_flags & MD_POPULATED) {
ASSERT(cid.cid_fileno != 0);
cachefs_modified(ndcp);
error = 0;
if (delvp) {
error = cachefs_dir_rmentry(ndcp, nnm);
}
if (error == 0) {
error = cachefs_dir_enter(ndcp, nnm, cookiep,
&cid, SM_ASYNC);
}
}
if (error) {
cachefs_nocache(ndcp);
mutex_exit(&ndcp->c_statelock);
mutex_enter(&odcp->c_statelock);
cachefs_nocache(odcp);
mutex_exit(&odcp->c_statelock);
if (error == ENOTDIR)
error = ETIMEDOUT;
goto out;
}
mutex_exit(&ndcp->c_statelock);
gethrestime(¤t_time);
/* update the file we just deleted */
if (delvp) {
mutex_enter(&delcp->c_statelock);
delcp->c_attr.va_nlink--;
delcp->c_metadata.md_localctime = current_time;
delcp->c_metadata.md_flags |= MD_LOCALCTIME;
if (delcp->c_attr.va_nlink == 0) {
delcp->c_flags |= CN_DESTROY;
} else {
delcp->c_flags |= CN_UPDATED;
}
mutex_exit(&delcp->c_statelock);
}
/* update the file we renamed */
mutex_enter(&recp->c_statelock);
recp->c_metadata.md_localctime = current_time;
recp->c_metadata.md_flags |= MD_LOCALCTIME;
recp->c_flags |= CN_UPDATED;
mutex_exit(&recp->c_statelock);
/* update the source directory */
mutex_enter(&odcp->c_statelock);
odcp->c_metadata.md_localctime = current_time;
odcp->c_metadata.md_localmtime = current_time;
odcp->c_metadata.md_flags |= MD_LOCALCTIME | MD_LOCALMTIME;
odcp->c_flags |= CN_UPDATED;
mutex_exit(&odcp->c_statelock);
/* update the destination directory */
if (odcp != ndcp) {
mutex_enter(&ndcp->c_statelock);
ndcp->c_metadata.md_localctime = current_time;
ndcp->c_metadata.md_localmtime = current_time;
ndcp->c_metadata.md_flags |= MD_LOCALCTIME | MD_LOCALMTIME;
ndcp->c_flags |= CN_UPDATED;
mutex_exit(&ndcp->c_statelock);
}
out:
if (commit) {
/* commit the log entry */
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX bob: fix on panic */
}
}
if (odcp != ndcp)
rw_exit(&ndcp->c_rwlock);
rw_exit(&odcp->c_rwlock);
VN_RELE(revp);
return (error);
}
/*ARGSUSED*/
static int
cachefs_mkdir(vnode_t *dvp, char *nm, vattr_t *vap, vnode_t **vpp,
cred_t *cr, caller_context_t *ct, int flags, vsecattr_t *vsecp)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
cachefscache_t *cachep = fscp->fs_cache;
int error = 0;
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_mkdir: ENTER dvp %p\n", (void *)dvp);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (fscp->fs_cache->c_flags & (CACHE_NOFILL | CACHE_NOCACHE))
ASSERT(dcp->c_flags & CN_NOCACHE);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the mkdir operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(dcp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
rw_enter(&dcp->c_rwlock, RW_WRITER);
held = 1;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_mkdir_connected(dvp, nm, vap,
vpp, cr);
if (CFS_TIMEOUT(fscp, error)) {
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_mkdir_disconnected(dvp, nm, vap,
vpp, cr);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_MKDIR)) {
fid_t *fidp = NULL;
ino64_t fileno = 0;
cnode_t *cp = NULL;
if (error == 0)
cp = VTOC(*vpp);
if (cp != NULL) {
fidp = &cp->c_metadata.md_cookie;
fileno = cp->c_id.cid_fileno;
}
cachefs_log_mkdir(cachep, error, fscp->fs_cfsvfsp,
fidp, fileno, crgetuid(cr));
}
if (held) {
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
}
if (error == 0 && CFS_ISFS_NONSHARED(fscp))
(void) cachefs_pack(dvp, nm, cr);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_mkdir: EXIT error = %d\n", error);
#endif
return (error);
}
static int
cachefs_mkdir_connected(vnode_t *dvp, char *nm, vattr_t *vap,
vnode_t **vpp, cred_t *cr)
{
cnode_t *newcp = NULL, *dcp = VTOC(dvp);
struct vnode *vp = NULL;
int error = 0;
fscache_t *fscp = C_TO_FSCACHE(dcp);
struct fid cookie;
struct vattr attr;
cfs_cid_t cid, dircid;
uint32_t valid_fid;
if (fscp->fs_cache->c_flags & (CACHE_NOFILL | CACHE_NOCACHE))
ASSERT(dcp->c_flags & CN_NOCACHE);
mutex_enter(&dcp->c_statelock);
/* get backvp of dir */
if (dcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, dcp);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
}
/* consistency check the directory */
error = CFSOP_CHECK_COBJECT(fscp, dcp, 0, cr);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
dircid = dcp->c_id;
/* make the dir on the back fs */
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_mkdir (nfsv4): dcp %p, dbackvp %p, "
"name %s\n", dcp, dcp->c_backvp, nm));
error = VOP_MKDIR(dcp->c_backvp, nm, vap, &vp, cr, NULL, 0, NULL);
mutex_exit(&dcp->c_statelock);
if (error) {
goto out;
}
/* get the cookie and make the cnode */
attr.va_mask = AT_ALL;
valid_fid = (CFS_ISFS_BACKFS_NFSV4(fscp) ? FALSE : TRUE);
error = cachefs_getcookie(vp, &cookie, &attr, cr, valid_fid);
if (error) {
goto out;
}
cid.cid_flags = 0;
cid.cid_fileno = attr.va_nodeid;
error = cachefs_cnode_make(&cid, fscp, (valid_fid ? &cookie : NULL),
&attr, vp, cr, 0, &newcp);
if (error) {
goto out;
}
ASSERT(CTOV(newcp)->v_type == VDIR);
*vpp = CTOV(newcp);
/* if the dir is populated, add the new entry */
mutex_enter(&dcp->c_statelock);
if (CFS_ISFS_NONSHARED(fscp) &&
(dcp->c_metadata.md_flags & MD_POPULATED)) {
error = cachefs_dir_enter(dcp, nm, &cookie, &newcp->c_id,
SM_ASYNC);
if (error) {
cachefs_nocache(dcp);
error = 0;
}
}
dcp->c_attr.va_nlink++;
dcp->c_flags |= CN_UPDATED;
CFSOP_MODIFY_COBJECT(fscp, dcp, cr);
mutex_exit(&dcp->c_statelock);
/* XXX bob: should we do a filldir here? or just add . and .. */
/* maybe should kick off an async filldir so caller does not wait */
/* put the entry in the dnlc */
if (cachefs_dnlc)
dnlc_enter(dvp, nm, *vpp);
/* save the fileno of the parent so can find the name */
if (bcmp(&newcp->c_metadata.md_parent, &dircid,
sizeof (cfs_cid_t)) != 0) {
mutex_enter(&newcp->c_statelock);
newcp->c_metadata.md_parent = dircid;
newcp->c_flags |= CN_UPDATED;
mutex_exit(&newcp->c_statelock);
}
out:
if (vp)
VN_RELE(vp);
return (error);
}
static int
cachefs_mkdir_disconnected(vnode_t *dvp, char *nm, vattr_t *vap,
vnode_t **vpp, cred_t *cr)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error;
cnode_t *newcp = NULL;
struct vattr va;
timestruc_t current_time;
off_t commit = 0;
char *s;
int namlen;
/* don't allow '/' characters in pathname component */
for (s = nm, namlen = 0; *s; s++, namlen++)
if (*s == '/')
return (EACCES);
if (namlen == 0)
return (EINVAL);
if (CFS_ISFS_WRITE_AROUND(fscp))
return (ETIMEDOUT);
mutex_enter(&dcp->c_statelock);
/* check permissions */
if (error = cachefs_access_local(dcp, (VEXEC|VWRITE), cr)) {
mutex_exit(&dcp->c_statelock);
goto out;
}
/* the directory front file must be populated */
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
error = ETIMEDOUT;
mutex_exit(&dcp->c_statelock);
goto out;
}
/* make sure nm does not already exist in the directory */
error = cachefs_dir_look(dcp, nm, NULL, NULL, NULL, NULL);
if (error == ENOTDIR) {
error = ETIMEDOUT;
mutex_exit(&dcp->c_statelock);
goto out;
}
if (error != ENOENT) {
error = EEXIST;
mutex_exit(&dcp->c_statelock);
goto out;
}
/* make up a reasonable set of attributes */
cachefs_attr_setup(vap, &va, dcp, cr);
va.va_type = VDIR;
va.va_mode |= S_IFDIR;
va.va_nlink = 2;
mutex_exit(&dcp->c_statelock);
/* create the cnode */
error = cachefs_cnode_create(fscp, &va, 0, &newcp);
if (error)
goto out;
mutex_enter(&newcp->c_statelock);
error = cachefs_dlog_cidmap(fscp);
if (error) {
mutex_exit(&newcp->c_statelock);
goto out;
}
cachefs_creategid(dcp, newcp, vap, cr);
mutex_enter(&dcp->c_statelock);
cachefs_createacl(dcp, newcp);
mutex_exit(&dcp->c_statelock);
gethrestime(¤t_time);
newcp->c_metadata.md_vattr.va_atime = current_time;
newcp->c_metadata.md_localctime = current_time;
newcp->c_metadata.md_localmtime = current_time;
newcp->c_metadata.md_flags |= MD_MAPPING | MD_LOCALMTIME |
MD_LOCALCTIME;
newcp->c_flags |= CN_UPDATED;
/* make a front file for the new directory, add . and .. */
error = cachefs_dir_new(dcp, newcp);
if (error) {
mutex_exit(&newcp->c_statelock);
goto out;
}
cachefs_modified(newcp);
/*
* write the metadata now rather than waiting until
* inactive so that if there's no space we can let
* the caller know.
*/
ASSERT(newcp->c_frontvp);
ASSERT((newcp->c_filegrp->fg_flags & CFS_FG_ALLOC_ATTR) == 0);
ASSERT((newcp->c_flags & CN_ALLOC_PENDING) == 0);
error = filegrp_write_metadata(newcp->c_filegrp,
&newcp->c_id, &newcp->c_metadata);
if (error) {
mutex_exit(&newcp->c_statelock);
goto out;
}
mutex_exit(&newcp->c_statelock);
/* log the operation */
commit = cachefs_dlog_mkdir(fscp, dcp, newcp, nm, &va, cr);
if (commit == 0) {
error = ENOSPC;
goto out;
}
mutex_enter(&dcp->c_statelock);
/* make sure directory is still populated */
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
mutex_exit(&dcp->c_statelock);
error = ETIMEDOUT;
goto out;
}
cachefs_modified(dcp);
/* enter the new file in the directory */
error = cachefs_dir_enter(dcp, nm, &newcp->c_metadata.md_cookie,
&newcp->c_id, SM_ASYNC);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
/* update parent dir times */
dcp->c_metadata.md_localctime = current_time;
dcp->c_metadata.md_localmtime = current_time;
dcp->c_metadata.md_flags |= MD_LOCALCTIME | MD_LOCALMTIME;
dcp->c_attr.va_nlink++;
dcp->c_flags |= CN_UPDATED;
mutex_exit(&dcp->c_statelock);
out:
if (commit) {
/* commit the log entry */
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX bob: fix on panic */
}
}
if (error) {
if (newcp) {
mutex_enter(&newcp->c_statelock);
newcp->c_flags |= CN_DESTROY;
mutex_exit(&newcp->c_statelock);
VN_RELE(CTOV(newcp));
}
} else {
*vpp = CTOV(newcp);
}
return (error);
}
/*ARGSUSED*/
static int
cachefs_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
caller_context_t *ct, int flags)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
cachefscache_t *cachep = fscp->fs_cache;
int error = 0;
int held = 0;
int connected = 0;
size_t namlen;
vnode_t *vp = NULL;
int vfslock = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_rmdir: ENTER vp %p\n", (void *)dvp);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (fscp->fs_cache->c_flags & (CACHE_NOFILL | CACHE_NOCACHE))
ASSERT(dcp->c_flags & CN_NOCACHE);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the rmdir operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(dcp);
for (;;) {
if (vfslock) {
vn_vfsunlock(vp);
vfslock = 0;
}
if (vp) {
VN_RELE(vp);
vp = NULL;
}
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
/* if disconnected, do some extra error checking */
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
/* check permissions */
mutex_enter(&dcp->c_statelock);
error = cachefs_access_local(dcp, (VEXEC|VWRITE), cr);
mutex_exit(&dcp->c_statelock);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
if (error)
break;
namlen = strlen(nm);
if (namlen == 0) {
error = EINVAL;
break;
}
/* cannot remove . and .. */
if (nm[0] == '.') {
if (namlen == 1) {
error = EINVAL;
break;
} else if (namlen == 2 && nm[1] == '.') {
error = EEXIST;
break;
}
}
}
/* get the cnode of the dir to remove */
error = cachefs_lookup_common(dvp, nm, &vp, NULL, 0, NULL, cr);
if (error) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
/* must be a dir */
if (vp->v_type != VDIR) {
error = ENOTDIR;
break;
}
/* must not be current dir */
if (VOP_CMP(vp, cdir, ct)) {
error = EINVAL;
break;
}
/* see ufs_dirremove for why this is done, mount race */
if (vn_vfswlock(vp)) {
error = EBUSY;
break;
}
vfslock = 1;
if (vn_mountedvfs(vp) != NULL) {
error = EBUSY;
break;
}
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_rmdir_connected(dvp, nm, cdir,
cr, vp);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_rmdir_disconnected(dvp, nm, cdir,
cr, vp);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_RMDIR)) {
ino64_t fileno = 0;
fid_t *fidp = NULL;
cnode_t *cp = NULL;
if (vp)
cp = VTOC(vp);
if (cp != NULL) {
fidp = &cp->c_metadata.md_cookie;
fileno = cp->c_id.cid_fileno;
}
cachefs_log_rmdir(cachep, error, fscp->fs_cfsvfsp,
fidp, fileno, crgetuid(cr));
}
if (held) {
cachefs_cd_release(fscp);
}
if (vfslock)
vn_vfsunlock(vp);
if (vp)
VN_RELE(vp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_rmdir: EXIT error = %d\n", error);
#endif
return (error);
}
static int
cachefs_rmdir_connected(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
vnode_t *vp)
{
cnode_t *dcp = VTOC(dvp);
cnode_t *cp = VTOC(vp);
int error = 0;
fscache_t *fscp = C_TO_FSCACHE(dcp);
rw_enter(&dcp->c_rwlock, RW_WRITER);
mutex_enter(&dcp->c_statelock);
mutex_enter(&cp->c_statelock);
if (dcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, dcp);
if (error) {
goto out;
}
}
error = CFSOP_CHECK_COBJECT(fscp, dcp, 0, cr);
if (error)
goto out;
/* rmdir on the back fs */
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_rmdir (nfsv4): dcp %p, dbackvp %p, "
"name %s\n", dcp, dcp->c_backvp, nm));
error = VOP_RMDIR(dcp->c_backvp, nm, cdir, cr, NULL, 0);
if (error)
goto out;
/* if the dir is populated, remove the entry from it */
if (CFS_ISFS_NONSHARED(fscp) &&
(dcp->c_metadata.md_flags & MD_POPULATED)) {
error = cachefs_dir_rmentry(dcp, nm);
if (error) {
cachefs_nocache(dcp);
error = 0;
}
}
/*
* *if* the (hard) link count goes to 0, then we set the CDESTROY
* flag on the cnode. The cached object will then be destroyed
* at inactive time where the chickens come home to roost :-)
* The link cnt for directories is bumped down by 2 'cause the "."
* entry has to be elided too ! The link cnt for the parent goes down
* by 1 (because of "..").
*/
cp->c_attr.va_nlink -= 2;
dcp->c_attr.va_nlink--;
if (cp->c_attr.va_nlink == 0) {
cp->c_flags |= CN_DESTROY;
} else {
cp->c_flags |= CN_UPDATED;
}
dcp->c_flags |= CN_UPDATED;
dnlc_purge_vp(vp);
CFSOP_MODIFY_COBJECT(fscp, dcp, cr);
out:
mutex_exit(&cp->c_statelock);
mutex_exit(&dcp->c_statelock);
rw_exit(&dcp->c_rwlock);
return (error);
}
static int
/*ARGSUSED*/
cachefs_rmdir_disconnected(vnode_t *dvp, char *nm, vnode_t *cdir,
cred_t *cr, vnode_t *vp)
{
cnode_t *dcp = VTOC(dvp);
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error = 0;
off_t commit = 0;
timestruc_t current_time;
if (CFS_ISFS_WRITE_AROUND(fscp))
return (ETIMEDOUT);
rw_enter(&dcp->c_rwlock, RW_WRITER);
mutex_enter(&dcp->c_statelock);
mutex_enter(&cp->c_statelock);
/* both directories must be populated */
if (((dcp->c_metadata.md_flags & MD_POPULATED) == 0) ||
((cp->c_metadata.md_flags & MD_POPULATED) == 0)) {
error = ETIMEDOUT;
goto out;
}
/* if sticky bit set on the dir, more access checks to perform */
if (error = cachefs_stickyrmchk(dcp, cp, cr)) {
goto out;
}
/* make sure dir is empty */
if (cp->c_attr.va_nlink > 2) {
error = cachefs_dir_empty(cp);
if (error) {
if (error == ENOTDIR)
error = ETIMEDOUT;
goto out;
}
cachefs_modified(cp);
}
cachefs_modified(dcp);
/* log the operation */
commit = cachefs_dlog_rmdir(fscp, dcp, nm, cp, cr);
if (commit == 0) {
error = ENOSPC;
goto out;
}
/* remove name from parent dir */
error = cachefs_dir_rmentry(dcp, nm);
if (error == ENOTDIR) {
error = ETIMEDOUT;
goto out;
}
if (error)
goto out;
gethrestime(¤t_time);
/* update deleted dir values */
cp->c_attr.va_nlink -= 2;
if (cp->c_attr.va_nlink == 0)
cp->c_flags |= CN_DESTROY;
else {
cp->c_metadata.md_localctime = current_time;
cp->c_metadata.md_flags |= MD_LOCALCTIME;
cp->c_flags |= CN_UPDATED;
}
/* update parent values */
dcp->c_metadata.md_localctime = current_time;
dcp->c_metadata.md_localmtime = current_time;
dcp->c_metadata.md_flags |= MD_LOCALCTIME | MD_LOCALMTIME;
dcp->c_attr.va_nlink--;
dcp->c_flags |= CN_UPDATED;
out:
mutex_exit(&cp->c_statelock);
mutex_exit(&dcp->c_statelock);
rw_exit(&dcp->c_rwlock);
if (commit) {
/* commit the log entry */
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX bob: fix on panic */
}
dnlc_purge_vp(vp);
}
return (error);
}
/*ARGSUSED*/
static int
cachefs_symlink(vnode_t *dvp, char *lnm, vattr_t *tva,
char *tnm, cred_t *cr, caller_context_t *ct, int flags)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
cachefscache_t *cachep = fscp->fs_cache;
int error = 0;
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_symlink: ENTER dvp %p lnm %s tnm %s\n",
(void *)dvp, lnm, tnm);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (fscp->fs_cache->c_flags & CACHE_NOCACHE)
ASSERT(dcp->c_flags & CN_NOCACHE);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the symlink operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(dcp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
rw_enter(&dcp->c_rwlock, RW_WRITER);
held = 1;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_symlink_connected(dvp, lnm, tva,
tnm, cr);
if (CFS_TIMEOUT(fscp, error)) {
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_symlink_disconnected(dvp, lnm, tva,
tnm, cr);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_SYMLINK))
cachefs_log_symlink(cachep, error, fscp->fs_cfsvfsp,
&dcp->c_metadata.md_cookie, dcp->c_id.cid_fileno,
crgetuid(cr), (uint_t)strlen(tnm));
if (held) {
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
}
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_symlink: EXIT error = %d\n", error);
#endif
return (error);
}
static int
cachefs_symlink_connected(vnode_t *dvp, char *lnm, vattr_t *tva,
char *tnm, cred_t *cr)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error = 0;
vnode_t *backvp = NULL;
cnode_t *newcp = NULL;
struct vattr va;
struct fid cookie;
cfs_cid_t cid;
uint32_t valid_fid;
mutex_enter(&dcp->c_statelock);
if (dcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, dcp);
if (error) {
cachefs_nocache(dcp);
mutex_exit(&dcp->c_statelock);
goto out;
}
}
error = CFSOP_CHECK_COBJECT(fscp, dcp, 0, cr);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_symlink (nfsv4): dcp %p, dbackvp %p, "
"lnm %s, tnm %s\n", dcp, dcp->c_backvp, lnm, tnm));
error = VOP_SYMLINK(dcp->c_backvp, lnm, tva, tnm, cr, NULL, 0);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
if ((dcp->c_filegrp->fg_flags & CFS_FG_WRITE) == 0 &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
cachefs_nocache(dcp);
mutex_exit(&dcp->c_statelock);
goto out;
}
CFSOP_MODIFY_COBJECT(fscp, dcp, cr);
/* lookup the symlink we just created and get its fid and attrs */
(void) VOP_LOOKUP(dcp->c_backvp, lnm, &backvp, NULL, 0, NULL, cr,
NULL, NULL, NULL);
if (backvp == NULL) {
if (CFS_ISFS_BACKFS_NFSV4(fscp) == 0)
cachefs_nocache(dcp);
mutex_exit(&dcp->c_statelock);
goto out;
}
valid_fid = (CFS_ISFS_BACKFS_NFSV4(fscp) ? FALSE : TRUE);
error = cachefs_getcookie(backvp, &cookie, &va, cr, valid_fid);
if (error) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
error = 0;
cachefs_nocache(dcp);
mutex_exit(&dcp->c_statelock);
goto out;
}
cid.cid_fileno = va.va_nodeid;
cid.cid_flags = 0;
/* if the dir is cached, add the symlink to it */
if (CFS_ISFS_NONSHARED(fscp) &&
(dcp->c_metadata.md_flags & MD_POPULATED)) {
error = cachefs_dir_enter(dcp, lnm, &cookie, &cid, SM_ASYNC);
if (error) {
cachefs_nocache(dcp);
error = 0;
}
}
mutex_exit(&dcp->c_statelock);
/* make the cnode for the sym link */
error = cachefs_cnode_make(&cid, fscp, (valid_fid ? &cookie : NULL),
&va, backvp, cr, 0, &newcp);
if (error) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_nocache(dcp);
error = 0;
goto out;
}
/* try to cache the symlink contents */
rw_enter(&newcp->c_rwlock, RW_WRITER);
mutex_enter(&newcp->c_statelock);
/*
* try to cache the sym link, note that its a noop if NOCACHE
* or NFSv4 is set
*/
error = cachefs_stuffsymlink(newcp, tnm, (int)newcp->c_size);
if (error) {
cachefs_nocache(newcp);
error = 0;
}
mutex_exit(&newcp->c_statelock);
rw_exit(&newcp->c_rwlock);
out:
if (backvp)
VN_RELE(backvp);
if (newcp)
VN_RELE(CTOV(newcp));
return (error);
}
static int
cachefs_symlink_disconnected(vnode_t *dvp, char *lnm, vattr_t *tva,
char *tnm, cred_t *cr)
{
cnode_t *dcp = VTOC(dvp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
int error;
cnode_t *newcp = NULL;
struct vattr va;
timestruc_t current_time;
off_t commit = 0;
if (CFS_ISFS_WRITE_AROUND(fscp))
return (ETIMEDOUT);
mutex_enter(&dcp->c_statelock);
/* check permissions */
if (error = cachefs_access_local(dcp, (VEXEC|VWRITE), cr)) {
mutex_exit(&dcp->c_statelock);
goto out;
}
/* the directory front file must be populated */
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
error = ETIMEDOUT;
mutex_exit(&dcp->c_statelock);
goto out;
}
/* make sure lnm does not already exist in the directory */
error = cachefs_dir_look(dcp, lnm, NULL, NULL, NULL, NULL);
if (error == ENOTDIR) {
error = ETIMEDOUT;
mutex_exit(&dcp->c_statelock);
goto out;
}
if (error != ENOENT) {
error = EEXIST;
mutex_exit(&dcp->c_statelock);
goto out;
}
/* make up a reasonable set of attributes */
cachefs_attr_setup(tva, &va, dcp, cr);
va.va_type = VLNK;
va.va_mode |= S_IFLNK;
va.va_size = strlen(tnm);
mutex_exit(&dcp->c_statelock);
/* create the cnode */
error = cachefs_cnode_create(fscp, &va, 0, &newcp);
if (error)
goto out;
rw_enter(&newcp->c_rwlock, RW_WRITER);
mutex_enter(&newcp->c_statelock);
error = cachefs_dlog_cidmap(fscp);
if (error) {
mutex_exit(&newcp->c_statelock);
rw_exit(&newcp->c_rwlock);
error = ENOSPC;
goto out;
}
cachefs_creategid(dcp, newcp, tva, cr);
mutex_enter(&dcp->c_statelock);
cachefs_createacl(dcp, newcp);
mutex_exit(&dcp->c_statelock);
gethrestime(¤t_time);
newcp->c_metadata.md_vattr.va_atime = current_time;
newcp->c_metadata.md_localctime = current_time;
newcp->c_metadata.md_localmtime = current_time;
newcp->c_metadata.md_flags |= MD_MAPPING | MD_LOCALMTIME |
MD_LOCALCTIME;
newcp->c_flags |= CN_UPDATED;
/* log the operation */
commit = cachefs_dlog_symlink(fscp, dcp, newcp, lnm, tva, tnm, cr);
if (commit == 0) {
mutex_exit(&newcp->c_statelock);
rw_exit(&newcp->c_rwlock);
error = ENOSPC;
goto out;
}
/* store the symlink contents */
error = cachefs_stuffsymlink(newcp, tnm, (int)newcp->c_size);
if (error) {
mutex_exit(&newcp->c_statelock);
rw_exit(&newcp->c_rwlock);
goto out;
}
if (cachefs_modified_alloc(newcp)) {
mutex_exit(&newcp->c_statelock);
rw_exit(&newcp->c_rwlock);
error = ENOSPC;
goto out;
}
/*
* write the metadata now rather than waiting until
* inactive so that if there's no space we can let
* the caller know.
*/
if (newcp->c_flags & CN_ALLOC_PENDING) {
if (newcp->c_filegrp->fg_flags & CFS_FG_ALLOC_ATTR) {
(void) filegrp_allocattr(newcp->c_filegrp);
}
error = filegrp_create_metadata(newcp->c_filegrp,
&newcp->c_metadata, &newcp->c_id);
if (error) {
mutex_exit(&newcp->c_statelock);
rw_exit(&newcp->c_rwlock);
goto out;
}
newcp->c_flags &= ~CN_ALLOC_PENDING;
}
error = filegrp_write_metadata(newcp->c_filegrp,
&newcp->c_id, &newcp->c_metadata);
if (error) {
mutex_exit(&newcp->c_statelock);
rw_exit(&newcp->c_rwlock);
goto out;
}
mutex_exit(&newcp->c_statelock);
rw_exit(&newcp->c_rwlock);
mutex_enter(&dcp->c_statelock);
/* enter the new file in the directory */
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
error = ETIMEDOUT;
mutex_exit(&dcp->c_statelock);
goto out;
}
cachefs_modified(dcp);
error = cachefs_dir_enter(dcp, lnm, &newcp->c_metadata.md_cookie,
&newcp->c_id, SM_ASYNC);
if (error) {
mutex_exit(&dcp->c_statelock);
goto out;
}
/* update parent dir times */
dcp->c_metadata.md_localctime = current_time;
dcp->c_metadata.md_localmtime = current_time;
dcp->c_metadata.md_flags |= MD_LOCALMTIME | MD_LOCALCTIME;
dcp->c_flags |= CN_UPDATED;
mutex_exit(&dcp->c_statelock);
out:
if (commit) {
/* commit the log entry */
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX bob: fix on panic */
}
}
if (error) {
if (newcp) {
mutex_enter(&newcp->c_statelock);
newcp->c_flags |= CN_DESTROY;
mutex_exit(&newcp->c_statelock);
}
}
if (newcp) {
VN_RELE(CTOV(newcp));
}
return (error);
}
/*ARGSUSED*/
static int
cachefs_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
caller_context_t *ct, int flags)
{
cnode_t *dcp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(dcp);
cachefscache_t *cachep = fscp->fs_cache;
int error = 0;
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_readdir: ENTER vp %p\n", (void *)vp);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the readdir operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(dcp);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
break;
rw_enter(&dcp->c_rwlock, RW_READER);
held = 1;
/* quit if link count of zero (posix) */
if (dcp->c_attr.va_nlink == 0) {
if (eofp)
*eofp = 1;
error = 0;
break;
}
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_readdir_connected(vp, uiop, cr,
eofp);
if (CFS_TIMEOUT(fscp, error)) {
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_readdir_disconnected(vp, uiop, cr,
eofp);
if (CFS_TIMEOUT(fscp, error)) {
if (cachefs_cd_access_miss(fscp)) {
error = cachefs_readdir_connected(vp,
uiop, cr, eofp);
if (!CFS_TIMEOUT(fscp, error))
break;
delay(5*hz);
connected = 0;
continue;
}
connected = 1;
continue;
}
}
break;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_READDIR))
cachefs_log_readdir(cachep, error, fscp->fs_cfsvfsp,
&dcp->c_metadata.md_cookie, dcp->c_id.cid_fileno,
crgetuid(cr), uiop->uio_loffset, *eofp);
if (held) {
rw_exit(&dcp->c_rwlock);
cachefs_cd_release(fscp);
}
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_readdir: EXIT error = %d\n", error);
#endif
return (error);
}
static int
cachefs_readdir_connected(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp)
{
cnode_t *dcp = VTOC(vp);
int error;
fscache_t *fscp = C_TO_FSCACHE(dcp);
struct cachefs_req *rp;
mutex_enter(&dcp->c_statelock);
/* check directory consistency */
error = CFSOP_CHECK_COBJECT(fscp, dcp, 0, cr);
if (error)
goto out;
dcp->c_usage++;
/* if dir was modified, toss old contents */
if (dcp->c_metadata.md_flags & MD_INVALREADDIR) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_inval_object(dcp);
}
error = 0;
if (((dcp->c_metadata.md_flags & MD_POPULATED) == 0) &&
((dcp->c_flags & (CN_ASYNC_POPULATE | CN_NOCACHE)) == 0) &&
!CFS_ISFS_BACKFS_NFSV4(fscp) &&
(fscp->fs_cdconnected == CFS_CD_CONNECTED)) {
if (cachefs_async_okay()) {
/*
* Set up asynchronous request to fill this
* directory.
*/
dcp->c_flags |= CN_ASYNC_POPULATE;
rp = kmem_cache_alloc(cachefs_req_cache, KM_SLEEP);
rp->cfs_cmd = CFS_POPULATE;
rp->cfs_req_u.cu_populate.cpop_vp = vp;
rp->cfs_cr = cr;
crhold(cr);
VN_HOLD(vp);
cachefs_addqueue(rp, &fscp->fs_workq);
} else {
error = cachefs_dir_fill(dcp, cr);
if (error != 0)
cachefs_nocache(dcp);
}
}
/* if front file is populated */
if (((dcp->c_flags & (CN_NOCACHE | CN_ASYNC_POPULATE)) == 0) &&
!CFS_ISFS_BACKFS_NFSV4(fscp) &&
(dcp->c_metadata.md_flags & MD_POPULATED)) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
error = cachefs_dir_read(dcp, uiop, eofp);
if (error == 0)
fscp->fs_stats.st_hits++;
}
/* if front file could not be used */
if ((error != 0) ||
CFS_ISFS_BACKFS_NFSV4(fscp) ||
(dcp->c_flags & (CN_NOCACHE | CN_ASYNC_POPULATE)) ||
((dcp->c_metadata.md_flags & MD_POPULATED) == 0)) {
if (error && !(dcp->c_flags & CN_NOCACHE) &&
!CFS_ISFS_BACKFS_NFSV4(fscp))
cachefs_nocache(dcp);
/* get the back vp */
if (dcp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, dcp);
if (error)
goto out;
}
if (fscp->fs_inum_size > 0) {
error = cachefs_readback_translate(dcp, uiop, cr, eofp);
} else {
/* do the dir read from the back fs */
(void) VOP_RWLOCK(dcp->c_backvp,
V_WRITELOCK_FALSE, NULL);
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_readdir (nfsv4): "
"dcp %p, dbackvp %p\n", dcp, dcp->c_backvp));
error = VOP_READDIR(dcp->c_backvp, uiop, cr, eofp,
NULL, 0);
VOP_RWUNLOCK(dcp->c_backvp, V_WRITELOCK_FALSE, NULL);
}
if (error == 0)
fscp->fs_stats.st_misses++;
}
out:
mutex_exit(&dcp->c_statelock);
return (error);
}
static int
cachefs_readback_translate(cnode_t *cp, uio_t *uiop, cred_t *cr, int *eofp)
{
int error = 0;
fscache_t *fscp = C_TO_FSCACHE(cp);
caddr_t buffy = NULL;
int buffysize = MAXBSIZE;
caddr_t chrp, end;
ino64_t newinum;
struct dirent64 *de;
uio_t uioin;
iovec_t iov;
ASSERT(cp->c_backvp != NULL);
ASSERT(fscp->fs_inum_size > 0);
if (uiop->uio_resid < buffysize)
buffysize = (int)uiop->uio_resid;
buffy = cachefs_kmem_alloc(buffysize, KM_SLEEP);
iov.iov_base = buffy;
iov.iov_len = buffysize;
uioin.uio_iov = &iov;
uioin.uio_iovcnt = 1;
uioin.uio_segflg = UIO_SYSSPACE;
uioin.uio_fmode = 0;
uioin.uio_extflg = UIO_COPY_CACHED;
uioin.uio_loffset = uiop->uio_loffset;
uioin.uio_resid = buffysize;
(void) VOP_RWLOCK(cp->c_backvp, V_WRITELOCK_FALSE, NULL);
error = VOP_READDIR(cp->c_backvp, &uioin, cr, eofp, NULL, 0);
VOP_RWUNLOCK(cp->c_backvp, V_WRITELOCK_FALSE, NULL);
if (error != 0)
goto out;
end = buffy + buffysize - uioin.uio_resid;
mutex_exit(&cp->c_statelock);
mutex_enter(&fscp->fs_fslock);
for (chrp = buffy; chrp < end; chrp += de->d_reclen) {
de = (dirent64_t *)chrp;
newinum = cachefs_inum_real2fake(fscp, de->d_ino);
if (newinum == 0)
newinum = cachefs_fileno_conflict(fscp, de->d_ino);
de->d_ino = newinum;
}
mutex_exit(&fscp->fs_fslock);
mutex_enter(&cp->c_statelock);
error = uiomove(buffy, end - buffy, UIO_READ, uiop);
uiop->uio_loffset = uioin.uio_loffset;
out:
if (buffy != NULL)
cachefs_kmem_free(buffy, buffysize);
return (error);
}
static int
/*ARGSUSED*/
cachefs_readdir_disconnected(vnode_t *vp, uio_t *uiop, cred_t *cr,
int *eofp)
{
cnode_t *dcp = VTOC(vp);
int error;
mutex_enter(&dcp->c_statelock);
if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
error = ETIMEDOUT;
} else {
error = cachefs_dir_read(dcp, uiop, eofp);
if (error == ENOTDIR)
error = ETIMEDOUT;
}
mutex_exit(&dcp->c_statelock);
return (error);
}
/*ARGSUSED*/
static int
cachefs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
{
int error = 0;
struct cnode *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions, then bail
* as NFSv4 doesn't support VOP_FID.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
return (ENOTSUP);
}
mutex_enter(&cp->c_statelock);
if (fidp->fid_len < cp->c_metadata.md_cookie.fid_len) {
fidp->fid_len = cp->c_metadata.md_cookie.fid_len;
error = ENOSPC;
} else {
bcopy(cp->c_metadata.md_cookie.fid_data, fidp->fid_data,
cp->c_metadata.md_cookie.fid_len);
fidp->fid_len = cp->c_metadata.md_cookie.fid_len;
}
mutex_exit(&cp->c_statelock);
return (error);
}
/* ARGSUSED2 */
static int
cachefs_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
{
cnode_t *cp = VTOC(vp);
/*
* XXX - This is ifdef'ed out for now. The problem -
* getdents() acquires the read version of rwlock, then we come
* into cachefs_readdir() and that wants to acquire the write version
* of this lock (if its going to populate the directory). This is
* a problem, this can be solved by introducing another lock in the
* cnode.
*/
/* XXX */
if (vp->v_type != VREG)
return (-1);
if (write_lock)
rw_enter(&cp->c_rwlock, RW_WRITER);
else
rw_enter(&cp->c_rwlock, RW_READER);
return (write_lock);
}
/* ARGSUSED */
static void
cachefs_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
{
cnode_t *cp = VTOC(vp);
if (vp->v_type != VREG)
return;
rw_exit(&cp->c_rwlock);
}
/* ARGSUSED */
static int
cachefs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
caller_context_t *ct)
{
return (0);
}
static int cachefs_lostpage = 0;
/*
* Return all the pages from [off..off+len] in file
*/
/*ARGSUSED*/
static int
cachefs_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, cred_t *cr, caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
int error;
fscache_t *fscp = C_TO_FSCACHE(cp);
cachefscache_t *cachep = fscp->fs_cache;
int held = 0;
int connected = 0;
#ifdef CFSDEBUG
u_offset_t offx = (u_offset_t)off;
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_getpage: ENTER vp %p off %lld len %lu rw %d\n",
(void *)vp, offx, len, rw);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (vp->v_flag & VNOMAP) {
error = ENOSYS;
goto out;
}
/* Call backfilesystem if NFSv4 */
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_getpage_backfs_nfsv4(vp, off, len, protp, pl,
plsz, seg, addr, rw, cr);
goto out;
}
/* XXX sam: make this do an async populate? */
if (pl == NULL) {
error = 0;
goto out;
}
if (protp != NULL)
*protp = PROT_ALL;
for (;;) {
/* get (or renew) access to the file system */
if (held) {
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
break;
held = 1;
/*
* If we are getting called as a side effect of a
* cachefs_write()
* operation the local file size might not be extended yet.
* In this case we want to be able to return pages of zeroes.
*/
if ((u_offset_t)off + len >
((cp->c_size + PAGEOFFSET) & (offset_t)PAGEMASK)) {
if (seg != segkmap) {
error = EFAULT;
break;
}
}
error = pvn_getpages(cachefs_getapage, vp, (u_offset_t)off,
len, protp, pl, plsz, seg, addr, rw, cr);
if (error == 0)
break;
if (((cp->c_flags & CN_NOCACHE) && (error == ENOSPC)) ||
error == EAGAIN) {
connected = 0;
continue;
}
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
if (CFS_TIMEOUT(fscp, error)) {
if (cachefs_cd_access_miss(fscp)) {
error = pvn_getpages(
cachefs_getapage_back, vp,
(u_offset_t)off, len, protp, pl,
plsz, seg, addr, rw, cr);
if (!CFS_TIMEOUT(fscp, error) &&
(error != EAGAIN))
break;
delay(5*hz);
connected = 0;
continue;
}
connected = 1;
continue;
}
}
break;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_GETPAGE))
cachefs_log_getpage(cachep, error, vp->v_vfsp,
&cp->c_metadata.md_cookie, cp->c_id.cid_fileno,
crgetuid(cr), off, len);
if (held) {
cachefs_cd_release(fscp);
}
out:
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_getpage: EXIT vp %p error %d\n",
(void *)vp, error);
#endif
return (error);
}
/*
* cachefs_getpage_backfs_nfsv4
*
* Call NFSv4 back filesystem to handle the getpage (cachefs
* pass-through support for NFSv4).
*/
static int
cachefs_getpage_backfs_nfsv4(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,
cred_t *cr)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
vnode_t *backvp;
int error;
/*
* For NFSv4 pass-through to work, only connected operation is
* supported, the cnode backvp must exist, and cachefs optional
* (eg., disconnectable) flags are turned off. Assert these
* conditions for the getpage operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* Call backfs vnode op after extracting backvp */
mutex_enter(&cp->c_statelock);
backvp = cp->c_backvp;
mutex_exit(&cp->c_statelock);
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_getpage_backfs_nfsv4: cnode %p, backvp %p\n",
cp, backvp));
error = VOP_GETPAGE(backvp, off, len, protp, pl, plsz, seg,
addr, rw, cr, NULL);
return (error);
}
/*
* Called from pvn_getpages to get a particular page.
*/
/*ARGSUSED*/
static int
cachefs_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, cred_t *cr)
{
cnode_t *cp = VTOC(vp);
page_t **ppp, *pp = NULL;
fscache_t *fscp = C_TO_FSCACHE(cp);
cachefscache_t *cachep = fscp->fs_cache;
int error = 0;
struct page **ourpl;
struct page *ourstackpl[17]; /* see ASSERT() below for 17 */
int index = 0;
int downgrade;
int have_statelock = 0;
u_offset_t popoff;
size_t popsize = 0;
/*LINTED*/
ASSERT(((DEF_POP_SIZE / PAGESIZE) + 1) <= 17);
if (fscp->fs_info.fi_popsize > DEF_POP_SIZE)
ourpl = cachefs_kmem_alloc(sizeof (struct page *) *
((fscp->fs_info.fi_popsize / PAGESIZE) + 1), KM_SLEEP);
else
ourpl = ourstackpl;
ourpl[0] = NULL;
off = off & (offset_t)PAGEMASK;
again:
/*
* Look for the page
*/
if (page_exists(vp, off) == 0) {
/*
* Need to do work to get the page.
* Grab our lock because we are going to
* modify the state of the cnode.
*/
if (! have_statelock) {
mutex_enter(&cp->c_statelock);
have_statelock = 1;
}
/*
* If we're in NOCACHE mode, we will need a backvp
*/
if (cp->c_flags & CN_NOCACHE) {
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
error = ETIMEDOUT;
goto out;
}
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error)
goto out;
}
error = VOP_GETPAGE(cp->c_backvp, off,
PAGESIZE, protp, ourpl, PAGESIZE, seg,
addr, S_READ, cr, NULL);
/*
* backfs returns EFAULT when we are trying for a
* page beyond EOF but cachefs has the knowledge that
* it is not beyond EOF be cause cp->c_size is
* greater then the offset requested.
*/
if (error == EFAULT) {
error = 0;
pp = page_create_va(vp, off, PAGESIZE,
PG_EXCL | PG_WAIT, seg, addr);
if (pp == NULL)
goto again;
pagezero(pp, 0, PAGESIZE);
pvn_plist_init(pp, pl, plsz, off, PAGESIZE, rw);
goto out;
}
if (error)
goto out;
goto getpages;
}
/*
* We need a front file. If we can't get it,
* put the cnode in NOCACHE mode and try again.
*/
if (cp->c_frontvp == NULL) {
error = cachefs_getfrontfile(cp);
if (error) {
cachefs_nocache(cp);
error = EAGAIN;
goto out;
}
}
/*
* Check if the front file needs population.
* If population is necessary, make sure we have a
* backvp as well. We will get the page from the backvp.
* bug 4152459-
* But if the file system is in disconnected mode
* and the file is a local file then do not check the
* allocmap.
*/
if (((fscp->fs_cdconnected == CFS_CD_CONNECTED) ||
((cp->c_metadata.md_flags & MD_LOCALFILENO) == 0)) &&
(cachefs_check_allocmap(cp, off) == 0)) {
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
error = ETIMEDOUT;
goto out;
}
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error)
goto out;
}
if (cp->c_filegrp->fg_flags & CFS_FG_WRITE) {
cachefs_cluster_allocmap(off, &popoff,
&popsize,
fscp->fs_info.fi_popsize, cp);
if (popsize != 0) {
error = cachefs_populate(cp,
popoff, popsize,
cp->c_frontvp, cp->c_backvp,
cp->c_size, cr);
if (error) {
cachefs_nocache(cp);
error = EAGAIN;
goto out;
} else {
cp->c_flags |=
CN_UPDATED |
CN_NEED_FRONT_SYNC |
CN_POPULATION_PENDING;
}
popsize = popsize - (off - popoff);
} else {
popsize = PAGESIZE;
}
}
/* else XXX assert CN_NOCACHE? */
error = VOP_GETPAGE(cp->c_backvp, (offset_t)off,
PAGESIZE, protp, ourpl, popsize,
seg, addr, S_READ, cr, NULL);
if (error)
goto out;
fscp->fs_stats.st_misses++;
} else {
if (cp->c_flags & CN_POPULATION_PENDING) {
error = VOP_FSYNC(cp->c_frontvp, FSYNC, cr,
NULL);
cp->c_flags &= ~CN_POPULATION_PENDING;
if (error) {
cachefs_nocache(cp);
error = EAGAIN;
goto out;
}
}
/*
* File was populated so we get the page from the
* frontvp
*/
error = VOP_GETPAGE(cp->c_frontvp, (offset_t)off,
PAGESIZE, protp, ourpl, PAGESIZE, seg, addr,
rw, cr, NULL);
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_GPFRONT))
cachefs_log_gpfront(cachep, error,
fscp->fs_cfsvfsp,
&cp->c_metadata.md_cookie, cp->c_fileno,
crgetuid(cr), off, PAGESIZE);
if (error) {
cachefs_nocache(cp);
error = EAGAIN;
goto out;
}
fscp->fs_stats.st_hits++;
}
getpages:
ASSERT(have_statelock);
if (have_statelock) {
mutex_exit(&cp->c_statelock);
have_statelock = 0;
}
downgrade = 0;
for (ppp = ourpl; *ppp; ppp++) {
if ((*ppp)->p_offset < off) {
index++;
page_unlock(*ppp);
continue;
}
if (PAGE_SHARED(*ppp)) {
if (page_tryupgrade(*ppp) == 0) {
for (ppp = &ourpl[index]; *ppp; ppp++)
page_unlock(*ppp);
error = EAGAIN;
goto out;
}
downgrade = 1;
}
ASSERT(PAGE_EXCL(*ppp));
(void) hat_pageunload((*ppp), HAT_FORCE_PGUNLOAD);
page_rename(*ppp, vp, (*ppp)->p_offset);
}
pl[0] = ourpl[index];
pl[1] = NULL;
if (downgrade) {
page_downgrade(ourpl[index]);
}
/* Unlock the rest of the pages from the cluster */
for (ppp = &ourpl[index+1]; *ppp; ppp++)
page_unlock(*ppp);
} else {
ASSERT(! have_statelock);
if (have_statelock) {
mutex_exit(&cp->c_statelock);
have_statelock = 0;
}
/* XXX SE_SHARED probably isn't what we *always* want */
if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
cachefs_lostpage++;
goto again;
}
pl[0] = pp;
pl[1] = NULL;
/* XXX increment st_hits? i don't think so, but... */
}
out:
if (have_statelock) {
mutex_exit(&cp->c_statelock);
have_statelock = 0;
}
if (fscp->fs_info.fi_popsize > DEF_POP_SIZE)
cachefs_kmem_free(ourpl, sizeof (struct page *) *
((fscp->fs_info.fi_popsize / PAGESIZE) + 1));
return (error);
}
/* gets a page but only from the back fs */
/*ARGSUSED*/
static int
cachefs_getapage_back(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, cred_t *cr)
{
cnode_t *cp = VTOC(vp);
page_t **ppp, *pp = NULL;
fscache_t *fscp = C_TO_FSCACHE(cp);
int error = 0;
struct page *ourpl[17];
int index = 0;
int have_statelock = 0;
int downgrade;
/*
* Grab the cnode statelock so the cnode state won't change
* while we're in here.
*/
ourpl[0] = NULL;
off = off & (offset_t)PAGEMASK;
again:
if (page_exists(vp, off) == 0) {
if (! have_statelock) {
mutex_enter(&cp->c_statelock);
have_statelock = 1;
}
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error)
goto out;
}
error = VOP_GETPAGE(cp->c_backvp, (offset_t)off,
PAGESIZE, protp, ourpl, PAGESIZE, seg,
addr, S_READ, cr, NULL);
if (error)
goto out;
if (have_statelock) {
mutex_exit(&cp->c_statelock);
have_statelock = 0;
}
downgrade = 0;
for (ppp = ourpl; *ppp; ppp++) {
if ((*ppp)->p_offset < off) {
index++;
page_unlock(*ppp);
continue;
}
if (PAGE_SHARED(*ppp)) {
if (page_tryupgrade(*ppp) == 0) {
for (ppp = &ourpl[index]; *ppp; ppp++)
page_unlock(*ppp);
error = EAGAIN;
goto out;
}
downgrade = 1;
}
ASSERT(PAGE_EXCL(*ppp));
(void) hat_pageunload((*ppp), HAT_FORCE_PGUNLOAD);
page_rename(*ppp, vp, (*ppp)->p_offset);
}
pl[0] = ourpl[index];
pl[1] = NULL;
if (downgrade) {
page_downgrade(ourpl[index]);
}
/* Unlock the rest of the pages from the cluster */
for (ppp = &ourpl[index+1]; *ppp; ppp++)
page_unlock(*ppp);
} else {
ASSERT(! have_statelock);
if (have_statelock) {
mutex_exit(&cp->c_statelock);
have_statelock = 0;
}
if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
cachefs_lostpage++;
goto again;
}
pl[0] = pp;
pl[1] = NULL;
}
out:
if (have_statelock) {
mutex_exit(&cp->c_statelock);
have_statelock = 0;
}
return (error);
}
/*ARGSUSED*/
static int
cachefs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
int error = 0;
fscache_t *fscp = C_TO_FSCACHE(cp);
int held = 0;
int connected = 0;
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
/* Call backfilesytem if NFSv4 */
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_putpage_backfs_nfsv4(vp, off, len, flags, cr);
goto out;
}
for (;;) {
/* get (or renew) access to the file system */
if (held) {
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
error = cachefs_putpage_common(vp, off, len, flags, cr);
if (error == 0)
break;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
if (NOMEMWAIT()) {
error = 0;
goto out;
}
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
break;
}
out:
if (held) {
cachefs_cd_release(fscp);
}
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
return (error);
}
/*
* cachefs_putpage_backfs_nfsv4
*
* Call NFSv4 back filesystem to handle the putpage (cachefs
* pass-through support for NFSv4).
*/
static int
cachefs_putpage_backfs_nfsv4(vnode_t *vp, offset_t off, size_t len, int flags,
cred_t *cr)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
vnode_t *backvp;
int error;
/*
* For NFSv4 pass-through to work, only connected operation is
* supported, the cnode backvp must exist, and cachefs optional
* (eg., disconnectable) flags are turned off. Assert these
* conditions for the putpage operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* Call backfs vnode op after extracting backvp */
mutex_enter(&cp->c_statelock);
backvp = cp->c_backvp;
mutex_exit(&cp->c_statelock);
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_putpage_backfs_nfsv4: cnode %p, backvp %p\n",
cp, backvp));
error = VOP_PUTPAGE(backvp, off, len, flags, cr, NULL);
return (error);
}
/*
* Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
* If len == 0, do from off to EOF.
*
* The normal cases should be len == 0 & off == 0 (entire vp list),
* len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
* (from pageout).
*/
/*ARGSUSED*/
int
cachefs_putpage_common(struct vnode *vp, offset_t off, size_t len,
int flags, cred_t *cr)
{
struct cnode *cp = VTOC(vp);
struct page *pp;
size_t io_len;
u_offset_t eoff, io_off;
int error = 0;
fscache_t *fscp = C_TO_FSCACHE(cp);
cachefscache_t *cachep = fscp->fs_cache;
if (len == 0 && (flags & B_INVAL) == 0 && vn_is_readonly(vp)) {
return (0);
}
if (!vn_has_cached_data(vp) || (off >= cp->c_size &&
(flags & B_INVAL) == 0))
return (0);
/*
* Should never have cached data for the cachefs vnode
* if NFSv4 is in use.
*/
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
/*
* If this is an async putpage let a thread handle it.
*/
if (flags & B_ASYNC) {
struct cachefs_req *rp;
int tflags = (flags & ~(B_ASYNC|B_DONTNEED));
if (ttoproc(curthread) == proc_pageout) {
/*
* If this is the page daemon we
* do the push synchronously (Dangerous!) and hope
* we can free enough to keep running...
*/
flags &= ~B_ASYNC;
goto again;
}
if (! cachefs_async_okay()) {
/*
* this is somewhat like NFS's behavior. keep
* the system from thrashing. we've seen
* cases where async queues get out of
* control, especially if
* madvise(MADV_SEQUENTIAL) is done on a large
* mmap()ed file that is read sequentially.
*/
flags &= ~B_ASYNC;
goto again;
}
/*
* if no flags other than B_ASYNC were set,
* we coalesce putpage requests into a single one for the
* whole file (len = off = 0). If such a request is
* already queued, we're done.
*
* If there are other flags set (e.g., B_INVAL), we don't
* attempt to coalesce and we use the specified length and
* offset.
*/
rp = kmem_cache_alloc(cachefs_req_cache, KM_SLEEP);
mutex_enter(&cp->c_iomutex);
if ((cp->c_ioflags & CIO_PUTPAGES) == 0 || tflags != 0) {
rp->cfs_cmd = CFS_PUTPAGE;
rp->cfs_req_u.cu_putpage.cp_vp = vp;
if (tflags == 0) {
off = len = 0;
cp->c_ioflags |= CIO_PUTPAGES;
}
rp->cfs_req_u.cu_putpage.cp_off = off;
rp->cfs_req_u.cu_putpage.cp_len = (uint_t)len;
rp->cfs_req_u.cu_putpage.cp_flags = flags & ~B_ASYNC;
rp->cfs_cr = cr;
crhold(rp->cfs_cr);
VN_HOLD(vp);
cp->c_nio++;
cachefs_addqueue(rp, &(C_TO_FSCACHE(cp)->fs_workq));
} else {
kmem_cache_free(cachefs_req_cache, rp);
}
mutex_exit(&cp->c_iomutex);
return (0);
}
again:
if (len == 0) {
/*
* Search the entire vp list for pages >= off
*/
error = pvn_vplist_dirty(vp, off, cachefs_push, flags, cr);
} else {
/*
* Do a range from [off...off + len] looking for pages
* to deal with.
*/
eoff = (u_offset_t)off + len;
for (io_off = off; io_off < eoff && io_off < cp->c_size;
io_off += io_len) {
/*
* If we are not invalidating, synchronously
* freeing or writing pages use the routine
* page_lookup_nowait() to prevent reclaiming
* them from the free list.
*/
if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
pp = page_lookup(vp, io_off,
(flags & (B_INVAL | B_FREE)) ?
SE_EXCL : SE_SHARED);
} else {
/* XXX this looks like dead code */
pp = page_lookup_nowait(vp, io_off,
(flags & B_FREE) ? SE_EXCL : SE_SHARED);
}
if (pp == NULL || pvn_getdirty(pp, flags) == 0)
io_len = PAGESIZE;
else {
error = cachefs_push(vp, pp, &io_off,
&io_len, flags, cr);
if (error != 0)
break;
/*
* "io_off" and "io_len" are returned as
* the range of pages we actually wrote.
* This allows us to skip ahead more quickly
* since several pages may've been dealt
* with by this iteration of the loop.
*/
}
}
}
if (error == 0 && off == 0 && (len == 0 || len >= cp->c_size)) {
cp->c_flags &= ~CDIRTY;
}
if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_PUTPAGE))
cachefs_log_putpage(cachep, error, fscp->fs_cfsvfsp,
&cp->c_metadata.md_cookie, cp->c_id.cid_fileno,
crgetuid(cr), off, len);
return (error);
}
/*ARGSUSED*/
static int
cachefs_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, cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
struct segvn_crargs vn_a;
int error;
int held = 0;
int writing;
int connected = 0;
#ifdef CFSDEBUG
u_offset_t offx = (u_offset_t)off;
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_map: ENTER vp %p off %lld len %lu flags %d\n",
(void *)vp, offx, len, flags);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (vp->v_flag & VNOMAP) {
error = ENOSYS;
goto out;
}
if (off < 0 || (offset_t)(off + len) < 0) {
error = ENXIO;
goto out;
}
if (vp->v_type != VREG) {
error = ENODEV;
goto out;
}
/*
* Check to see if the vnode is currently marked as not cachable.
* If so, we have to refuse the map request as this violates the
* don't cache attribute.
*/
if (vp->v_flag & VNOCACHE)
return (EAGAIN);
#ifdef OBSOLETE
/*
* If file is being locked, disallow mapping.
*/
if (vn_has_flocks(vp)) {
error = EAGAIN;
goto out;
}
#endif
/* call backfilesystem if NFSv4 */
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_map_backfs_nfsv4(vp, off, as, addrp, len, prot,
maxprot, flags, cr);
goto out;
}
writing = (prot & PROT_WRITE && ((flags & MAP_PRIVATE) == 0));
for (;;) {
/* get (or renew) access to the file system */
if (held) {
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, writing);
if (error)
break;
held = 1;
if (writing) {
mutex_enter(&cp->c_statelock);
if (CFS_ISFS_WRITE_AROUND(fscp)) {
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
connected = 1;
continue;
} else {
cachefs_nocache(cp);
}
}
/*
* CN_MAPWRITE is for an optimization in cachefs_delmap.
* If CN_MAPWRITE is not set then cachefs_delmap does
* not need to try to push out any pages.
* This bit gets cleared when the cnode goes inactive.
*/
cp->c_flags |= CN_MAPWRITE;
mutex_exit(&cp->c_statelock);
}
break;
}
if (held) {
cachefs_cd_release(fscp);
}
as_rangelock(as);
error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
if (error != 0) {
as_rangeunlock(as);
goto out;
}
/*
* package up all the data passed in into a segvn_args struct and
* call as_map with segvn_create function to create a new segment
* in the address space.
*/
vn_a.vp = vp;
vn_a.offset = off;
vn_a.type = flags & MAP_TYPE;
vn_a.prot = (uchar_t)prot;
vn_a.maxprot = (uchar_t)maxprot;
vn_a.cred = cr;
vn_a.amp = NULL;
vn_a.flags = flags & ~MAP_TYPE;
vn_a.szc = 0;
vn_a.lgrp_mem_policy_flags = 0;
error = as_map(as, *addrp, len, segvn_create, &vn_a);
as_rangeunlock(as);
out:
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_map: EXIT vp %p error %d\n", (void *)vp, error);
#endif
return (error);
}
/*
* cachefs_map_backfs_nfsv4
*
* Call NFSv4 back filesystem to handle the map (cachefs
* pass-through support for NFSv4).
*/
static int
cachefs_map_backfs_nfsv4(struct vnode *vp, offset_t off, struct as *as,
caddr_t *addrp, size_t len, uchar_t prot,
uchar_t maxprot, uint_t flags, cred_t *cr)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
vnode_t *backvp;
int error;
/*
* For NFSv4 pass-through to work, only connected operation is
* supported, the cnode backvp must exist, and cachefs optional
* (eg., disconnectable) flags are turned off. Assert these
* conditions for the map operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* Call backfs vnode op after extracting backvp */
mutex_enter(&cp->c_statelock);
backvp = cp->c_backvp;
mutex_exit(&cp->c_statelock);
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_map_backfs_nfsv4: cnode %p, backvp %p\n",
cp, backvp));
error = VOP_MAP(backvp, off, as, addrp, len, prot, maxprot, flags, cr,
NULL);
return (error);
}
/*ARGSUSED*/
static int
cachefs_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,
cred_t *cr, caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
if (vp->v_flag & VNOMAP)
return (ENOSYS);
/*
* Check this is not an NFSv4 filesystem, as the mapping
* is not done on the cachefs filesystem if NFSv4 is in
* use.
*/
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
mutex_enter(&cp->c_statelock);
cp->c_mapcnt += btopr(len);
mutex_exit(&cp->c_statelock);
return (0);
}
/*ARGSUSED*/
static int
cachefs_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,
cred_t *cr, caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error;
int connected = 0;
int held = 0;
/*
* The file may be passed in to (or inherited into) the zone, so we
* need to let this operation go through since it happens as part of
* exiting.
*/
if (vp->v_flag & VNOMAP)
return (ENOSYS);
/*
* Check this is not an NFSv4 filesystem, as the mapping
* is not done on the cachefs filesystem if NFSv4 is in
* use.
*/
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
mutex_enter(&cp->c_statelock);
cp->c_mapcnt -= btopr(len);
ASSERT(cp->c_mapcnt >= 0);
mutex_exit(&cp->c_statelock);
if (cp->c_mapcnt || !vn_has_cached_data(vp) ||
((cp->c_flags & CN_MAPWRITE) == 0))
return (0);
for (;;) {
/* get (or renew) access to the file system */
if (held) {
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
connected = 0;
error = cachefs_putpage_common(vp, (offset_t)0,
(uint_t)0, 0, cr);
if (CFS_TIMEOUT(fscp, error)) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
continue;
} else {
connected = 1;
continue;
}
}
/* if no space left in cache, wait until connected */
if ((error == ENOSPC) &&
(fscp->fs_cdconnected != CFS_CD_CONNECTED)) {
connected = 1;
continue;
}
mutex_enter(&cp->c_statelock);
if (!error)
error = cp->c_error;
cp->c_error = 0;
mutex_exit(&cp->c_statelock);
break;
}
if (held)
cachefs_cd_release(fscp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
return (error);
}
/* ARGSUSED */
static int
cachefs_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 cnode *cp = VTOC(vp);
int error;
struct fscache *fscp = C_TO_FSCACHE(cp);
vnode_t *backvp;
int held = 0;
int connected = 0;
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
if ((cmd != F_GETLK) && (cmd != F_SETLK) && (cmd != F_SETLKW))
return (EINVAL);
/* Disallow locking of files that are currently mapped */
if (((cmd == F_SETLK) || (cmd == F_SETLKW)) && (cp->c_mapcnt > 0)) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
return (EAGAIN);
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the frlock operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* XXX bob: nfs does a bunch more checks than we do */
if (CFS_ISFS_LLOCK(fscp)) {
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
}
for (;;) {
/* get (or renew) access to the file system */
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
break;
held = 1;
/* if not connected, quit or wait */
if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
connected = 1;
continue;
}
/* nocache the file */
if ((cp->c_flags & CN_NOCACHE) == 0 &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
mutex_enter(&cp->c_statelock);
cachefs_nocache(cp);
mutex_exit(&cp->c_statelock);
}
/*
* XXX bob: probably should do a consistency check
* Pass arguments unchanged if NFSv4 is the backfs.
*/
if (bfp->l_whence == 2 && CFS_ISFS_BACKFS_NFSV4(fscp) == 0) {
bfp->l_start += cp->c_size;
bfp->l_whence = 0;
}
/* get the back vp */
mutex_enter(&cp->c_statelock);
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error) {
mutex_exit(&cp->c_statelock);
break;
}
}
backvp = cp->c_backvp;
VN_HOLD(backvp);
mutex_exit(&cp->c_statelock);
/*
* make sure we can flush currently dirty pages before
* allowing the lock
*/
if (bfp->l_type != F_UNLCK && cmd != F_GETLK &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_putpage(
vp, (offset_t)0, 0, B_INVAL, cr, ct);
if (error) {
error = ENOLCK;
VN_RELE(backvp);
break;
}
}
/* do lock on the back file */
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_frlock (nfsv4): cp %p, backvp %p\n",
cp, backvp));
error = VOP_FRLOCK(backvp, cmd, bfp, flag, offset, NULL, cr,
ct);
VN_RELE(backvp);
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
break;
}
if (held) {
cachefs_cd_release(fscp);
}
/*
* If we are setting a lock mark the vnode VNOCACHE so the page
* cache does not give inconsistent results on locked files shared
* between clients. The VNOCACHE flag is never turned off as long
* as the vnode is active because it is hard to figure out when the
* last lock is gone.
* XXX - what if some already has the vnode mapped in?
* XXX bob: see nfs3_frlock, do not allow locking if vnode mapped in.
*/
if ((error == 0) && (bfp->l_type != F_UNLCK) && (cmd != F_GETLK) &&
!CFS_ISFS_BACKFS_NFSV4(fscp))
vp->v_flag |= VNOCACHE;
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
return (error);
}
/*
* Free storage space associated with the specified vnode. The portion
* to be freed is specified by bfp->l_start and bfp->l_len (already
* normalized to a "whence" of 0).
*
* This is an experimental facility whose continued existence is not
* guaranteed. Currently, we only support the special case
* of l_len == 0, meaning free to end of file.
*/
/* ARGSUSED */
static int
cachefs_space(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
offset_t offset, cred_t *cr, caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error;
ASSERT(vp->v_type == VREG);
if (getzoneid() != GLOBAL_ZONEID)
return (EPERM);
if (cmd != F_FREESP)
return (EINVAL);
/* call backfilesystem if NFSv4 */
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_space_backfs_nfsv4(vp, cmd, bfp, flag,
offset, cr, ct);
goto out;
}
if ((error = convoff(vp, bfp, 0, offset)) == 0) {
ASSERT(bfp->l_start >= 0);
if (bfp->l_len == 0) {
struct vattr va;
va.va_size = bfp->l_start;
va.va_mask = AT_SIZE;
error = cachefs_setattr(vp, &va, 0, cr, ct);
} else
error = EINVAL;
}
out:
return (error);
}
/*
* cachefs_space_backfs_nfsv4
*
* Call NFSv4 back filesystem to handle the space (cachefs
* pass-through support for NFSv4).
*/
static int
cachefs_space_backfs_nfsv4(struct vnode *vp, int cmd, struct flock64 *bfp,
int flag, offset_t offset, cred_t *cr, caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
vnode_t *backvp;
int error;
/*
* For NFSv4 pass-through to work, only connected operation is
* supported, the cnode backvp must exist, and cachefs optional
* (eg., disconnectable) flags are turned off. Assert these
* conditions for the space operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
/* Call backfs vnode op after extracting backvp */
mutex_enter(&cp->c_statelock);
backvp = cp->c_backvp;
mutex_exit(&cp->c_statelock);
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_space_backfs_nfsv4: cnode %p, backvp %p\n",
cp, backvp));
error = VOP_SPACE(backvp, cmd, bfp, flag, offset, cr, ct);
return (error);
}
/*ARGSUSED*/
static int
cachefs_realvp(struct vnode *vp, struct vnode **vpp, caller_context_t *ct)
{
return (EINVAL);
}
/*ARGSUSED*/
static int
cachefs_pageio(struct vnode *vp, page_t *pp, u_offset_t io_off, size_t io_len,
int flags, cred_t *cr, caller_context_t *ct)
{
return (ENOSYS);
}
static int
cachefs_setsecattr_connected(cnode_t *cp,
vsecattr_t *vsec, int flag, cred_t *cr)
{
fscache_t *fscp = C_TO_FSCACHE(cp);
int error = 0;
ASSERT(RW_WRITE_HELD(&cp->c_rwlock));
ASSERT((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0);
mutex_enter(&cp->c_statelock);
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error) {
cachefs_nocache(cp);
goto out;
}
}
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
if (error)
goto out;
/* only owner can set acl */
if (cp->c_metadata.md_vattr.va_uid != crgetuid(cr)) {
error = EINVAL;
goto out;
}
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_setsecattr (nfsv4): cp %p, backvp %p",
cp, cp->c_backvp));
error = VOP_SETSECATTR(cp->c_backvp, vsec, flag, cr, NULL);
if (error) {
goto out;
}
if ((cp->c_filegrp->fg_flags & CFS_FG_WRITE) == 0 &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
cachefs_nocache(cp);
goto out;
}
CFSOP_MODIFY_COBJECT(fscp, cp, cr);
/* acl may have changed permissions -- handle this. */
if (!CFS_ISFS_BACKFS_NFSV4(fscp))
cachefs_acl2perm(cp, vsec);
if ((cp->c_flags & CN_NOCACHE) == 0 &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_cacheacl(cp, vsec);
if (error != 0) {
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_setacl: cacheacl: error %d\n",
error);
#endif /* CFSDEBUG */
error = 0;
cachefs_nocache(cp);
}
}
out:
mutex_exit(&cp->c_statelock);
return (error);
}
static int
cachefs_setsecattr_disconnected(cnode_t *cp,
vsecattr_t *vsec, int flag, cred_t *cr)
{
fscache_t *fscp = C_TO_FSCACHE(cp);
mode_t failmode = cp->c_metadata.md_vattr.va_mode;
off_t commit = 0;
int error = 0;
ASSERT((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0);
if (CFS_ISFS_WRITE_AROUND(fscp))
return (ETIMEDOUT);
mutex_enter(&cp->c_statelock);
/* only owner can set acl */
if (cp->c_metadata.md_vattr.va_uid != crgetuid(cr)) {
error = EINVAL;
goto out;
}
if (cp->c_metadata.md_flags & MD_NEEDATTRS) {
error = ETIMEDOUT;
goto out;
}
/* XXX do i need this? is this right? */
if (cp->c_flags & CN_ALLOC_PENDING) {
if (cp->c_filegrp->fg_flags & CFS_FG_ALLOC_ATTR) {
(void) filegrp_allocattr(cp->c_filegrp);
}
error = filegrp_create_metadata(cp->c_filegrp,
&cp->c_metadata, &cp->c_id);
if (error) {
goto out;
}
cp->c_flags &= ~CN_ALLOC_PENDING;
}
/* XXX is this right? */
if ((cp->c_metadata.md_flags & MD_MAPPING) == 0) {
error = cachefs_dlog_cidmap(fscp);
if (error) {
error = ENOSPC;
goto out;
}
cp->c_metadata.md_flags |= MD_MAPPING;
cp->c_flags |= CN_UPDATED;
}
commit = cachefs_dlog_setsecattr(fscp, vsec, flag, cp, cr);
if (commit == 0)
goto out;
/* fix modes in metadata */
cachefs_acl2perm(cp, vsec);
if ((cp->c_flags & CN_NOCACHE) == 0) {
error = cachefs_cacheacl(cp, vsec);
if (error != 0) {
goto out;
}
}
/* XXX is this right? */
if (cachefs_modified_alloc(cp)) {
error = ENOSPC;
goto out;
}
out:
if (error != 0)
cp->c_metadata.md_vattr.va_mode = failmode;
mutex_exit(&cp->c_statelock);
if (commit) {
if (cachefs_dlog_commit(fscp, commit, error)) {
/*EMPTY*/
/* XXX fix on panic? */
}
}
return (error);
}
/*ARGSUSED*/
static int
cachefs_setsecattr(vnode_t *vp, vsecattr_t *vsec, int flag, cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int connected = 0;
int held = 0;
int error = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_setsecattr: ENTER vp %p\n", (void *)vp);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
if (fscp->fs_info.fi_mntflags & CFS_NOACL) {
error = ENOSYS;
goto out;
}
if (! cachefs_vtype_aclok(vp)) {
error = EINVAL;
goto out;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the setsecattr operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
for (;;) {
/* drop hold on file system */
if (held) {
/* Won't loop with NFSv4 connected operation */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
/* acquire access to the file system */
error = cachefs_cd_access(fscp, connected, 1);
if (error)
break;
held = 1;
/* perform the setattr */
if (fscp->fs_cdconnected == CFS_CD_CONNECTED)
error = cachefs_setsecattr_connected(cp,
vsec, flag, cr);
else
error = cachefs_setsecattr_disconnected(cp,
vsec, flag, cr);
if (error) {
/* if connected */
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
}
/* else must be disconnected */
else {
if (CFS_TIMEOUT(fscp, error)) {
connected = 1;
continue;
}
}
}
break;
}
if (held) {
cachefs_cd_release(fscp);
}
return (error);
out:
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_setsecattr: EXIT error = %d\n", error);
#endif
return (error);
}
/*
* call this BEFORE calling cachefs_cacheacl(), as the latter will
* sanitize the acl.
*/
static void
cachefs_acl2perm(cnode_t *cp, vsecattr_t *vsec)
{
aclent_t *aclp;
int i;
for (i = 0; i < vsec->vsa_aclcnt; i++) {
aclp = ((aclent_t *)vsec->vsa_aclentp) + i;
switch (aclp->a_type) {
case USER_OBJ:
cp->c_metadata.md_vattr.va_mode &= (~0700);
cp->c_metadata.md_vattr.va_mode |= (aclp->a_perm << 6);
break;
case GROUP_OBJ:
cp->c_metadata.md_vattr.va_mode &= (~070);
cp->c_metadata.md_vattr.va_mode |= (aclp->a_perm << 3);
break;
case OTHER_OBJ:
cp->c_metadata.md_vattr.va_mode &= (~07);
cp->c_metadata.md_vattr.va_mode |= (aclp->a_perm);
break;
case CLASS_OBJ:
cp->c_metadata.md_aclclass = aclp->a_perm;
break;
}
}
cp->c_flags |= CN_UPDATED;
}
static int
cachefs_getsecattr(vnode_t *vp, vsecattr_t *vsec, int flag, cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int held = 0, connected = 0;
int error = 0;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_getsecattr: ENTER vp %p\n", (void *)vp);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the getsecattr operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
if (fscp->fs_info.fi_mntflags & CFS_NOACL) {
error = fs_fab_acl(vp, vsec, flag, cr, ct);
goto out;
}
for (;;) {
if (held) {
/* Won't loop with NFSv4 connected behavior */
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
cachefs_cd_release(fscp);
held = 0;
}
error = cachefs_cd_access(fscp, connected, 0);
if (error)
break;
held = 1;
if (fscp->fs_cdconnected == CFS_CD_CONNECTED) {
error = cachefs_getsecattr_connected(vp, vsec, flag,
cr);
if (CFS_TIMEOUT(fscp, error)) {
cachefs_cd_release(fscp);
held = 0;
cachefs_cd_timedout(fscp);
connected = 0;
continue;
}
} else {
error = cachefs_getsecattr_disconnected(vp, vsec, flag,
cr);
if (CFS_TIMEOUT(fscp, error)) {
if (cachefs_cd_access_miss(fscp)) {
error = cachefs_getsecattr_connected(vp,
vsec, flag, cr);
if (!CFS_TIMEOUT(fscp, error))
break;
delay(5*hz);
connected = 0;
continue;
}
connected = 1;
continue;
}
}
break;
}
out:
if (held)
cachefs_cd_release(fscp);
#ifdef CFS_CD_DEBUG
ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_getsecattr: EXIT error = %d\n", error);
#endif
return (error);
}
static int
cachefs_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
caller_context_t *ct)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int error = 0;
vnode_t *backvp;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_shrlock: ENTER vp %p\n", (void *)vp);
#endif
if (getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
goto out;
}
/*
* Cachefs only provides pass-through support for NFSv4,
* and all vnode operations are passed through to the
* back file system. For NFSv4 pass-through to work, only
* connected operation is supported, the cnode backvp must
* exist, and cachefs optional (eg., disconnectable) flags
* are turned off. Assert these conditions to ensure that
* the backfilesystem is called for the shrlock operation.
*/
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(cp);
mutex_enter(&cp->c_statelock);
if (cp->c_backvp == NULL)
error = cachefs_getbackvp(fscp, cp);
backvp = cp->c_backvp;
mutex_exit(&cp->c_statelock);
ASSERT((error != 0) || (backvp != NULL));
if (error == 0) {
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_shrlock (nfsv4): cp %p, backvp %p",
cp, backvp));
error = VOP_SHRLOCK(backvp, cmd, shr, flag, cr, ct);
}
out:
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_shrlock: EXIT error = %d\n", error);
#endif
return (error);
}
static int
cachefs_getsecattr_connected(vnode_t *vp, vsecattr_t *vsec, int flag,
cred_t *cr)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int hit = 0;
int error = 0;
mutex_enter(&cp->c_statelock);
error = CFSOP_CHECK_COBJECT(fscp, cp, 0, cr);
if (error)
goto out;
/* read from the cache if we can */
if ((cp->c_metadata.md_flags & MD_ACL) &&
((cp->c_flags & CN_NOCACHE) == 0) &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
ASSERT((cp->c_flags & CN_NOCACHE) == 0);
error = cachefs_getaclfromcache(cp, vsec);
if (error) {
cachefs_nocache(cp);
ASSERT((cp->c_metadata.md_flags & MD_ACL) == 0);
error = 0;
} else {
hit = 1;
goto out;
}
}
ASSERT(error == 0);
if (cp->c_backvp == NULL)
error = cachefs_getbackvp(fscp, cp);
if (error)
goto out;
CFS_DPRINT_BACKFS_NFSV4(fscp,
("cachefs_getsecattr (nfsv4): cp %p, backvp %p",
cp, cp->c_backvp));
error = VOP_GETSECATTR(cp->c_backvp, vsec, flag, cr, NULL);
if (error)
goto out;
if (((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0) &&
(cachefs_vtype_aclok(vp)) &&
((cp->c_flags & CN_NOCACHE) == 0) &&
!CFS_ISFS_BACKFS_NFSV4(fscp)) {
error = cachefs_cacheacl(cp, vsec);
if (error) {
error = 0;
cachefs_nocache(cp);
}
}
out:
if (error == 0) {
if (hit)
fscp->fs_stats.st_hits++;
else
fscp->fs_stats.st_misses++;
}
mutex_exit(&cp->c_statelock);
return (error);
}
static int
/*ARGSUSED*/
cachefs_getsecattr_disconnected(vnode_t *vp, vsecattr_t *vsec, int flag,
cred_t *cr)
{
cnode_t *cp = VTOC(vp);
fscache_t *fscp = C_TO_FSCACHE(cp);
int hit = 0;
int error = 0;
mutex_enter(&cp->c_statelock);
/* read from the cache if we can */
if (((cp->c_flags & CN_NOCACHE) == 0) &&
(cp->c_metadata.md_flags & MD_ACL)) {
error = cachefs_getaclfromcache(cp, vsec);
if (error) {
cachefs_nocache(cp);
ASSERT((cp->c_metadata.md_flags & MD_ACL) == 0);
error = 0;
} else {
hit = 1;
goto out;
}
}
error = ETIMEDOUT;
out:
if (error == 0) {
if (hit)
fscp->fs_stats.st_hits++;
else
fscp->fs_stats.st_misses++;
}
mutex_exit(&cp->c_statelock);
return (error);
}
/*
* cachefs_cacheacl() -- cache an ACL, which we do by applying it to
* the frontfile if possible; otherwise, the adjunct directory.
*
* inputs:
* cp - the cnode, with its statelock already held
* vsecp - a pointer to a vsecattr_t you'd like us to cache as-is,
* or NULL if you want us to do the VOP_GETSECATTR(backvp).
*
* returns:
* 0 - all is well
* nonzero - errno
*/
int
cachefs_cacheacl(cnode_t *cp, vsecattr_t *vsecp)
{
fscache_t *fscp = C_TO_FSCACHE(cp);
vsecattr_t vsec;
aclent_t *aclp;
int gotvsec = 0;
int error = 0;
vnode_t *vp = NULL;
void *aclkeep = NULL;
int i;
ASSERT(MUTEX_HELD(&cp->c_statelock));
ASSERT((cp->c_flags & CN_NOCACHE) == 0);
ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
ASSERT((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0);
ASSERT(cachefs_vtype_aclok(CTOV(cp)));
if (fscp->fs_info.fi_mntflags & CFS_NOACL) {
error = ENOSYS;
goto out;
}
if (vsecp == NULL) {
if (cp->c_backvp == NULL)
error = cachefs_getbackvp(fscp, cp);
if (error != 0)
goto out;
vsecp = &vsec;
bzero(&vsec, sizeof (vsec));
vsecp->vsa_mask =
VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT;
error = VOP_GETSECATTR(cp->c_backvp, vsecp, 0, kcred, NULL);
if (error != 0) {
goto out;
}
gotvsec = 1;
} else if (vsecp->vsa_mask & VSA_ACL) {
aclkeep = vsecp->vsa_aclentp;
vsecp->vsa_aclentp = cachefs_kmem_alloc(vsecp->vsa_aclcnt *
sizeof (aclent_t), KM_SLEEP);
bcopy(aclkeep, vsecp->vsa_aclentp, vsecp->vsa_aclcnt *
sizeof (aclent_t));
} else if ((vsecp->vsa_mask & (VSA_ACL | VSA_DFACL)) == 0) {
/* unless there's real data, we can cache nothing. */
return (0);
}
/*
* prevent the ACL from chmoding our frontfile, and
* snarf the class info
*/
if ((vsecp->vsa_mask & (VSA_ACL | VSA_ACLCNT)) ==
(VSA_ACL | VSA_ACLCNT)) {
for (i = 0; i < vsecp->vsa_aclcnt; i++) {
aclp = ((aclent_t *)vsecp->vsa_aclentp) + i;
switch (aclp->a_type) {
case CLASS_OBJ:
cp->c_metadata.md_aclclass =
aclp->a_perm;
/*FALLTHROUGH*/
case USER_OBJ:
case GROUP_OBJ:
case OTHER_OBJ:
aclp->a_perm = 06;
}
}
}
/*
* if the frontfile exists, then we always do the work. but,
* if there's no frontfile, and the ACL isn't a `real' ACL,
* then we don't want to do the work. otherwise, an `ls -l'
* will create tons of emtpy frontfiles.
*/
if (((cp->c_metadata.md_flags & MD_FILE) == 0) &&
((vsecp->vsa_aclcnt + vsecp->vsa_dfaclcnt)
<= MIN_ACL_ENTRIES)) {
cp->c_metadata.md_flags |= MD_ACL;
cp->c_flags |= CN_UPDATED;
goto out;
}
/*
* if we have a default ACL, then we need a
* real live directory in the frontfs that we
* can apply the ACL to. if not, then we just
* use the frontfile. we get the frontfile
* regardless -- that way, we know the
* directory for the frontfile exists.
*/
if (vsecp->vsa_dfaclcnt > 0) {
if (cp->c_acldirvp == NULL)
error = cachefs_getacldirvp(cp);
if (error != 0)
goto out;
vp = cp->c_acldirvp;
} else {
if (cp->c_frontvp == NULL)
error = cachefs_getfrontfile(cp);
if (error != 0)
goto out;
vp = cp->c_frontvp;
}
ASSERT(vp != NULL);
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
error = VOP_SETSECATTR(vp, vsecp, 0, kcred, NULL);
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
if (error != 0) {
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_cacheacl: setsecattr: error %d\n",
error);
#endif /* CFSDEBUG */
/*
* If there was an error, we don't want to call
* cachefs_nocache(); so, set error to 0.
* We will call cachefs_purgeacl(), in order to
* clean such things as adjunct ACL directories.
*/
cachefs_purgeacl(cp);
error = 0;
goto out;
}
if (vp == cp->c_frontvp)
cp->c_flags |= CN_NEED_FRONT_SYNC;
cp->c_metadata.md_flags |= MD_ACL;
cp->c_flags |= CN_UPDATED;
out:
if ((error) && (fscp->fs_cdconnected == CFS_CD_CONNECTED))
cachefs_nocache(cp);
if (gotvsec) {
if (vsec.vsa_aclcnt)
kmem_free(vsec.vsa_aclentp,
vsec.vsa_aclcnt * sizeof (aclent_t));
if (vsec.vsa_dfaclcnt)
kmem_free(vsec.vsa_dfaclentp,
vsec.vsa_dfaclcnt * sizeof (aclent_t));
} else if (aclkeep != NULL) {
cachefs_kmem_free(vsecp->vsa_aclentp,
vsecp->vsa_aclcnt * sizeof (aclent_t));
vsecp->vsa_aclentp = aclkeep;
}
return (error);
}
void
cachefs_purgeacl(cnode_t *cp)
{
ASSERT(MUTEX_HELD(&cp->c_statelock));
ASSERT(!CFS_ISFS_BACKFS_NFSV4(C_TO_FSCACHE(cp)));
if (cp->c_acldirvp != NULL) {
VN_RELE(cp->c_acldirvp);
cp->c_acldirvp = NULL;
}
if (cp->c_metadata.md_flags & MD_ACLDIR) {
char name[CFS_FRONTFILE_NAME_SIZE + 2];
ASSERT(cp->c_filegrp->fg_dirvp != NULL);
make_ascii_name(&cp->c_id, name);
(void) strcat(name, ".d");
(void) VOP_RMDIR(cp->c_filegrp->fg_dirvp, name,
cp->c_filegrp->fg_dirvp, kcred, NULL, 0);
}
cp->c_metadata.md_flags &= ~(MD_ACL | MD_ACLDIR);
cp->c_flags |= CN_UPDATED;
}
static int
cachefs_getacldirvp(cnode_t *cp)
{
char name[CFS_FRONTFILE_NAME_SIZE + 2];
int error = 0;
ASSERT(MUTEX_HELD(&cp->c_statelock));
ASSERT(cp->c_acldirvp == NULL);
if (cp->c_frontvp == NULL)
error = cachefs_getfrontfile(cp);
if (error != 0)
goto out;
ASSERT(cp->c_filegrp->fg_dirvp != NULL);
make_ascii_name(&cp->c_id, name);
(void) strcat(name, ".d");
error = VOP_LOOKUP(cp->c_filegrp->fg_dirvp,
name, &cp->c_acldirvp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
if ((error != 0) && (error != ENOENT))
goto out;
if (error != 0) {
vattr_t va;
va.va_mode = S_IFDIR | 0777;
va.va_uid = 0;
va.va_gid = 0;
va.va_type = VDIR;
va.va_mask = AT_TYPE | AT_MODE |
AT_UID | AT_GID;
error =
VOP_MKDIR(cp->c_filegrp->fg_dirvp,
name, &va, &cp->c_acldirvp, kcred, NULL, 0, NULL);
if (error != 0)
goto out;
}
ASSERT(cp->c_acldirvp != NULL);
cp->c_metadata.md_flags |= MD_ACLDIR;
cp->c_flags |= CN_UPDATED;
out:
if (error != 0)
cp->c_acldirvp = NULL;
return (error);
}
static int
cachefs_getaclfromcache(cnode_t *cp, vsecattr_t *vsec)
{
aclent_t *aclp;
int error = 0;
vnode_t *vp = NULL;
int i;
ASSERT(cp->c_metadata.md_flags & MD_ACL);
ASSERT(MUTEX_HELD(&cp->c_statelock));
ASSERT(vsec->vsa_aclentp == NULL);
if (cp->c_metadata.md_flags & MD_ACLDIR) {
if (cp->c_acldirvp == NULL)
error = cachefs_getacldirvp(cp);
if (error != 0)
goto out;
vp = cp->c_acldirvp;
} else if (cp->c_metadata.md_flags & MD_FILE) {
if (cp->c_frontvp == NULL)
error = cachefs_getfrontfile(cp);
if (error != 0)
goto out;
vp = cp->c_frontvp;
} else {
/*
* if we get here, then we know that MD_ACL is on,
* meaning an ACL was successfully cached. we also
* know that neither MD_ACLDIR nor MD_FILE are on, so
* this has to be an entry without a `real' ACL.
* thus, we forge whatever is necessary.
*/
if (vsec->vsa_mask & VSA_ACLCNT)
vsec->vsa_aclcnt = MIN_ACL_ENTRIES;
if (vsec->vsa_mask & VSA_ACL) {
vsec->vsa_aclentp =
kmem_zalloc(MIN_ACL_ENTRIES *
sizeof (aclent_t), KM_SLEEP);
aclp = (aclent_t *)vsec->vsa_aclentp;
aclp->a_type = USER_OBJ;
++aclp;
aclp->a_type = GROUP_OBJ;
++aclp;
aclp->a_type = OTHER_OBJ;
++aclp;
aclp->a_type = CLASS_OBJ;
ksort((caddr_t)vsec->vsa_aclentp, MIN_ACL_ENTRIES,
sizeof (aclent_t), cmp2acls);
}
ASSERT(vp == NULL);
}
if (vp != NULL) {
if ((error = VOP_GETSECATTR(vp, vsec, 0, kcred, NULL)) != 0) {
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_getaclfromcache: error %d\n",
error);
#endif /* CFSDEBUG */
goto out;
}
}
if (vsec->vsa_aclentp != NULL) {
for (i = 0; i < vsec->vsa_aclcnt; i++) {
aclp = ((aclent_t *)vsec->vsa_aclentp) + i;
switch (aclp->a_type) {
case USER_OBJ:
aclp->a_id = cp->c_metadata.md_vattr.va_uid;
aclp->a_perm =
cp->c_metadata.md_vattr.va_mode & 0700;
aclp->a_perm >>= 6;
break;
case GROUP_OBJ:
aclp->a_id = cp->c_metadata.md_vattr.va_gid;
aclp->a_perm =
cp->c_metadata.md_vattr.va_mode & 070;
aclp->a_perm >>= 3;
break;
case OTHER_OBJ:
aclp->a_perm =
cp->c_metadata.md_vattr.va_mode & 07;
break;
case CLASS_OBJ:
aclp->a_perm =
cp->c_metadata.md_aclclass;
break;
}
}
}
out:
if (error != 0)
cachefs_nocache(cp);
return (error);
}
/*
* Fills in targp with attribute information from srcp, cp
* and if necessary the system.
*/
static void
cachefs_attr_setup(vattr_t *srcp, vattr_t *targp, cnode_t *cp, cred_t *cr)
{
time_t now;
ASSERT((srcp->va_mask & (AT_TYPE | AT_MODE)) == (AT_TYPE | AT_MODE));
/*
* Add code to fill in the va struct. We use the fields from
* the srcp struct if they are populated, otherwise we guess
*/
targp->va_mask = 0; /* initialize all fields */
targp->va_mode = srcp->va_mode;
targp->va_type = srcp->va_type;
targp->va_nlink = 1;
targp->va_nodeid = 0;
if (srcp->va_mask & AT_UID)
targp->va_uid = srcp->va_uid;
else
targp->va_uid = crgetuid(cr);
if (srcp->va_mask & AT_GID)
targp->va_gid = srcp->va_gid;
else
targp->va_gid = crgetgid(cr);
if (srcp->va_mask & AT_FSID)
targp->va_fsid = srcp->va_fsid;
else
targp->va_fsid = 0; /* initialize all fields */
now = gethrestime_sec();
if (srcp->va_mask & AT_ATIME)
targp->va_atime = srcp->va_atime;
else
targp->va_atime.tv_sec = now;
if (srcp->va_mask & AT_MTIME)
targp->va_mtime = srcp->va_mtime;
else
targp->va_mtime.tv_sec = now;
if (srcp->va_mask & AT_CTIME)
targp->va_ctime = srcp->va_ctime;
else
targp->va_ctime.tv_sec = now;
if (srcp->va_mask & AT_SIZE)
targp->va_size = srcp->va_size;
else
targp->va_size = 0;
/*
* the remaing fields are set by the fs and not changable.
* we populate these entries useing the parent directory
* values. It's a small hack, but should work.
*/
targp->va_blksize = cp->c_metadata.md_vattr.va_blksize;
targp->va_rdev = cp->c_metadata.md_vattr.va_rdev;
targp->va_nblocks = cp->c_metadata.md_vattr.va_nblocks;
targp->va_seq = 0; /* Never keep the sequence number */
}
/*
* set the gid for a newly created file. The algorithm is as follows:
*
* 1) If the gid is set in the attribute list, then use it if
* the caller is privileged, belongs to the target group, or
* the group is the same as the parent directory.
*
* 2) If the parent directory's set-gid bit is clear, then use
* the process gid
*
* 3) Otherwise, use the gid of the parent directory.
*
* Note: newcp->c_attr.va_{mode,type} must already be set before calling
* this routine.
*/
static void
cachefs_creategid(cnode_t *dcp, cnode_t *newcp, vattr_t *vap, cred_t *cr)
{
if ((vap->va_mask & AT_GID) &&
((vap->va_gid == dcp->c_attr.va_gid) ||
groupmember(vap->va_gid, cr) ||
secpolicy_vnode_create_gid(cr) != 0)) {
newcp->c_attr.va_gid = vap->va_gid;
} else {
if (dcp->c_attr.va_mode & S_ISGID)
newcp->c_attr.va_gid = dcp->c_attr.va_gid;
else
newcp->c_attr.va_gid = crgetgid(cr);
}
/*
* if we're creating a directory, and the parent directory has the
* set-GID bit set, set it on the new directory.
* Otherwise, if the user is neither privileged nor a member of the
* file's new group, clear the file's set-GID bit.
*/
if (dcp->c_attr.va_mode & S_ISGID && newcp->c_attr.va_type == VDIR) {
newcp->c_attr.va_mode |= S_ISGID;
} else if ((newcp->c_attr.va_mode & S_ISGID) &&
secpolicy_vnode_setids_setgids(cr, newcp->c_attr.va_gid) != 0)
newcp->c_attr.va_mode &= ~S_ISGID;
}
/*
* create an acl for the newly created file. should be called right
* after cachefs_creategid.
*/
static void
cachefs_createacl(cnode_t *dcp, cnode_t *newcp)
{
fscache_t *fscp = C_TO_FSCACHE(dcp);
vsecattr_t vsec;
int gotvsec = 0;
int error = 0; /* placeholder */
aclent_t *aclp;
o_mode_t *classp = NULL;
o_mode_t gunion = 0;
int i;
if ((fscp->fs_info.fi_mntflags & CFS_NOACL) ||
(! cachefs_vtype_aclok(CTOV(newcp))))
return;
ASSERT(dcp->c_metadata.md_flags & MD_ACL);
ASSERT(MUTEX_HELD(&dcp->c_statelock));
ASSERT(MUTEX_HELD(&newcp->c_statelock));
/*
* XXX should probably not do VSA_ACL and VSA_ACLCNT, but that
* would hit code paths that isn't hit anywhere else.
*/
bzero(&vsec, sizeof (vsec));
vsec.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT;
error = cachefs_getaclfromcache(dcp, &vsec);
if (error != 0)
goto out;
gotvsec = 1;
if ((vsec.vsa_dfaclcnt > 0) && (vsec.vsa_dfaclentp != NULL)) {
if ((vsec.vsa_aclcnt > 0) && (vsec.vsa_aclentp != NULL))
kmem_free(vsec.vsa_aclentp,
vsec.vsa_aclcnt * sizeof (aclent_t));
vsec.vsa_aclcnt = vsec.vsa_dfaclcnt;
vsec.vsa_aclentp = vsec.vsa_dfaclentp;
vsec.vsa_dfaclcnt = 0;
vsec.vsa_dfaclentp = NULL;
if (newcp->c_attr.va_type == VDIR) {
vsec.vsa_dfaclentp = kmem_alloc(vsec.vsa_aclcnt *
sizeof (aclent_t), KM_SLEEP);
vsec.vsa_dfaclcnt = vsec.vsa_aclcnt;
bcopy(vsec.vsa_aclentp, vsec.vsa_dfaclentp,
vsec.vsa_aclcnt * sizeof (aclent_t));
}
/*
* this function should be called pretty much after
* the rest of the file creation stuff is done. so,
* uid, gid, etc. should be `right'. we'll go with
* that, rather than trying to determine whether to
* get stuff from cr or va.
*/
for (i = 0; i < vsec.vsa_aclcnt; i++) {
aclp = ((aclent_t *)vsec.vsa_aclentp) + i;
switch (aclp->a_type) {
case DEF_USER_OBJ:
aclp->a_type = USER_OBJ;
aclp->a_id = newcp->c_metadata.md_vattr.va_uid;
aclp->a_perm =
newcp->c_metadata.md_vattr.va_mode;
aclp->a_perm &= 0700;
aclp->a_perm >>= 6;
break;
case DEF_GROUP_OBJ:
aclp->a_type = GROUP_OBJ;
aclp->a_id = newcp->c_metadata.md_vattr.va_gid;
aclp->a_perm =
newcp->c_metadata.md_vattr.va_mode;
aclp->a_perm &= 070;
aclp->a_perm >>= 3;
gunion |= aclp->a_perm;
break;
case DEF_OTHER_OBJ:
aclp->a_type = OTHER_OBJ;
aclp->a_perm =
newcp->c_metadata.md_vattr.va_mode & 07;
break;
case DEF_CLASS_OBJ:
aclp->a_type = CLASS_OBJ;
classp = &(aclp->a_perm);
break;
case DEF_USER:
aclp->a_type = USER;
gunion |= aclp->a_perm;
break;
case DEF_GROUP:
aclp->a_type = GROUP;
gunion |= aclp->a_perm;
break;
}
}
/* XXX is this the POSIX thing to do? */
if (classp != NULL)
*classp &= gunion;
/*
* we don't need to log this; rather, we clear the
* MD_ACL bit when we reconnect.
*/
error = cachefs_cacheacl(newcp, &vsec);
if (error != 0)
goto out;
}
newcp->c_metadata.md_aclclass = 07; /* XXX check posix */
newcp->c_metadata.md_flags |= MD_ACL;
newcp->c_flags |= CN_UPDATED;
out:
if (gotvsec) {
if ((vsec.vsa_aclcnt > 0) && (vsec.vsa_aclentp != NULL))
kmem_free(vsec.vsa_aclentp,
vsec.vsa_aclcnt * sizeof (aclent_t));
if ((vsec.vsa_dfaclcnt > 0) && (vsec.vsa_dfaclentp != NULL))
kmem_free(vsec.vsa_dfaclentp,
vsec.vsa_dfaclcnt * sizeof (aclent_t));
}
}
/*
* this is translated from the UFS code for access checking.
*/
static int
cachefs_access_local(void *vcp, int mode, cred_t *cr)
{
cnode_t *cp = vcp;
fscache_t *fscp = C_TO_FSCACHE(cp);
int shift = 0;
ASSERT(MUTEX_HELD(&cp->c_statelock));
if (mode & VWRITE) {
/*
* Disallow write attempts on read-only
* file systems, unless the file is special.
*/
struct vnode *vp = CTOV(cp);
if (vn_is_readonly(vp)) {
if (!IS_DEVVP(vp)) {
return (EROFS);
}
}
}
/*
* if we need to do ACLs, do it. this works whether anyone
* has explicitly made an ACL or not.
*/
if (((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0) &&
(cachefs_vtype_aclok(CTOV(cp))))
return (cachefs_acl_access(cp, mode, cr));
if (crgetuid(cr) != cp->c_attr.va_uid) {
shift += 3;
if (!groupmember(cp->c_attr.va_gid, cr))
shift += 3;
}
return (secpolicy_vnode_access2(cr, CTOV(cp), cp->c_attr.va_uid,
cp->c_attr.va_mode << shift, mode));
}
/*
* This is transcribed from ufs_acl_access(). If that changes, then
* this should, too.
*
* Check the cnode's ACL's to see if this mode of access is
* allowed; return 0 if allowed, EACCES if not.
*
* We follow the procedure defined in Sec. 3.3.5, ACL Access
* Check Algorithm, of the POSIX 1003.6 Draft Standard.
*/
#define ACL_MODE_CHECK(M, PERM, C, I) \
secpolicy_vnode_access2(C, CTOV(I), owner, (PERM), (M))
static int
cachefs_acl_access(struct cnode *cp, int mode, cred_t *cr)
{
int error = 0;
fscache_t *fscp = C_TO_FSCACHE(cp);
int mask = ~0;
int ismask = 0;
int gperm = 0;
int ngroup = 0;
vsecattr_t vsec;
int gotvsec = 0;
aclent_t *aclp;
uid_t owner = cp->c_attr.va_uid;
int i;
ASSERT(MUTEX_HELD(&cp->c_statelock));
ASSERT((fscp->fs_info.fi_mntflags & CFS_NOACL) == 0);
/*
* strictly speaking, we shouldn't set VSA_DFACL and DFACLCNT,
* but then i believe we'd be the only thing exercising those
* code paths -- probably a bad thing.
*/
bzero(&vsec, sizeof (vsec));
vsec.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT;
/* XXX KLUDGE! correct insidious 0-class problem */
if (cp->c_metadata.md_aclclass == 0 &&
fscp->fs_cdconnected == CFS_CD_CONNECTED)
cachefs_purgeacl(cp);
again:
if (cp->c_metadata.md_flags & MD_ACL) {
error = cachefs_getaclfromcache(cp, &vsec);
if (error != 0) {
#ifdef CFSDEBUG
if (error != ETIMEDOUT)
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_acl_access():"
"error %d from getaclfromcache()\n",
error);
#endif /* CFSDEBUG */
if ((cp->c_metadata.md_flags & MD_ACL) == 0) {
goto again;
} else {
goto out;
}
}
} else {
if (cp->c_backvp == NULL) {
if (fscp->fs_cdconnected == CFS_CD_CONNECTED)
error = cachefs_getbackvp(fscp, cp);
else
error = ETIMEDOUT;
}
if (error == 0)
error = VOP_GETSECATTR(cp->c_backvp, &vsec, 0, cr,
NULL);
if (error != 0) {
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("cachefs_acl_access():"
"error %d from getsecattr(backvp)\n",
error);
#endif /* CFSDEBUG */
goto out;
}
if ((cp->c_flags & CN_NOCACHE) == 0 &&
!CFS_ISFS_BACKFS_NFSV4(fscp))
(void) cachefs_cacheacl(cp, &vsec);
}
gotvsec = 1;
ASSERT(error == 0);
for (i = 0; i < vsec.vsa_aclcnt; i++) {
aclp = ((aclent_t *)vsec.vsa_aclentp) + i;
switch (aclp->a_type) {
case USER_OBJ:
/*
* this might look cleaner in the 2nd loop
* below, but we do it here as an
* optimization.
*/
owner = aclp->a_id;
if (crgetuid(cr) == owner) {
error = ACL_MODE_CHECK(mode, aclp->a_perm << 6,
cr, cp);
goto out;
}
break;
case CLASS_OBJ:
mask = aclp->a_perm;
ismask = 1;
break;
}
}
ASSERT(error == 0);
for (i = 0; i < vsec.vsa_aclcnt; i++) {
aclp = ((aclent_t *)vsec.vsa_aclentp) + i;
switch (aclp->a_type) {
case USER:
if (crgetuid(cr) == aclp->a_id) {
error = ACL_MODE_CHECK(mode,
(aclp->a_perm & mask) << 6, cr, cp);
goto out;
}
break;
case GROUP_OBJ:
if (groupmember(aclp->a_id, cr)) {
++ngroup;
gperm |= aclp->a_perm;
if (! ismask) {
error = ACL_MODE_CHECK(mode,
aclp->a_perm << 6,
cr, cp);
goto out;
}
}
break;
case GROUP:
if (groupmember(aclp->a_id, cr)) {
++ngroup;
gperm |= aclp->a_perm;
}
break;
case OTHER_OBJ:
if (ngroup == 0) {
error = ACL_MODE_CHECK(mode, aclp->a_perm << 6,
cr, cp);
goto out;
}
break;
default:
break;
}
}
ASSERT(ngroup > 0);
error = ACL_MODE_CHECK(mode, (gperm & mask) << 6, cr, cp);
out:
if (gotvsec) {
if (vsec.vsa_aclcnt && vsec.vsa_aclentp)
kmem_free(vsec.vsa_aclentp,
vsec.vsa_aclcnt * sizeof (aclent_t));
if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp)
kmem_free(vsec.vsa_dfaclentp,
vsec.vsa_dfaclcnt * sizeof (aclent_t));
}
return (error);
}
/*
* see if permissions allow for removal of the given file from
* the given directory.
*/
static int
cachefs_stickyrmchk(struct cnode *dcp, struct cnode *cp, cred_t *cr)
{
uid_t uid;
/*
* If the containing directory is sticky, the user must:
* - own the directory, or
* - own the file, or
* - be able to write the file (if it's a plain file), or
* - be sufficiently privileged.
*/
if ((dcp->c_attr.va_mode & S_ISVTX) &&
((uid = crgetuid(cr)) != dcp->c_attr.va_uid) &&
(uid != cp->c_attr.va_uid) &&
(cp->c_attr.va_type != VREG ||
cachefs_access_local(cp, VWRITE, cr) != 0))
return (secpolicy_vnode_remove(cr));
return (0);
}
/*
* Returns a new name, may even be unique.
* Stolen from nfs code.
* Since now we will use renaming to .cfs* in place of .nfs*
* for CacheFS. Both NFS and CacheFS will rename opened files.
*/
static char cachefs_prefix[] = ".cfs";
kmutex_t cachefs_newnum_lock;
static char *
cachefs_newname(void)
{
static uint_t newnum = 0;
char *news;
char *s, *p;
uint_t id;
mutex_enter(&cachefs_newnum_lock);
if (newnum == 0) {
newnum = gethrestime_sec() & 0xfffff;
newnum |= 0x10000;
}
id = newnum++;
mutex_exit(&cachefs_newnum_lock);
news = cachefs_kmem_alloc(MAXNAMELEN, KM_SLEEP);
s = news;
p = cachefs_prefix;
while (*p != '\0')
*s++ = *p++;
while (id != 0) {
*s++ = "0123456789ABCDEF"[id & 0x0f];
id >>= 4;
}
*s = '\0';
return (news);
}
/*
* Called to rename the specified file to a temporary file so
* operations to the file after remove work.
* Must call this routine with the dir c_rwlock held as a writer.
*/
static int
/*ARGSUSED*/
cachefs_remove_dolink(vnode_t *dvp, vnode_t *vp, char *nm, cred_t *cr)
{
cnode_t *cp = VTOC(vp);
char *tmpname;
fscache_t *fscp = C_TO_FSCACHE(cp);
int error;
ASSERT(RW_WRITE_HELD(&(VTOC(dvp)->c_rwlock)));
/* get the new name for the file */
tmpname = cachefs_newname();
/* do the link */
if (fscp->fs_cdconnected == CFS_CD_CONNECTED)
error = cachefs_link_connected(dvp, vp, tmpname, cr);
else
error = cachefs_link_disconnected(dvp, vp, tmpname, cr);
if (error) {
cachefs_kmem_free(tmpname, MAXNAMELEN);
return (error);
}
mutex_enter(&cp->c_statelock);
if (cp->c_unldvp) {
VN_RELE(cp->c_unldvp);
cachefs_kmem_free(cp->c_unlname, MAXNAMELEN);
crfree(cp->c_unlcred);
}
VN_HOLD(dvp);
cp->c_unldvp = dvp;
crhold(cr);
cp->c_unlcred = cr;
cp->c_unlname = tmpname;
/* drop the backvp so NFS does not also do a rename */
mutex_exit(&cp->c_statelock);
return (0);
}
/*
* Marks the cnode as modified.
*/
static void
cachefs_modified(cnode_t *cp)
{
fscache_t *fscp = C_TO_FSCACHE(cp);
struct vattr va;
int error;
ASSERT(MUTEX_HELD(&cp->c_statelock));
ASSERT(cp->c_metadata.md_rlno);
/* if not on the modify list */
if (cp->c_metadata.md_rltype != CACHEFS_RL_MODIFIED) {
/* put on modified list, also marks the file as modified */
cachefs_rlent_moveto(fscp->fs_cache, CACHEFS_RL_MODIFIED,
cp->c_metadata.md_rlno, cp->c_metadata.md_frontblks);
cp->c_metadata.md_rltype = CACHEFS_RL_MODIFIED;
cp->c_flags |= CN_UPDATED;
/* if a modified regular file that is not local */
if (((cp->c_id.cid_flags & CFS_CID_LOCAL) == 0) &&
(cp->c_metadata.md_flags & MD_FILE) &&
(cp->c_attr.va_type == VREG)) {
if (cp->c_frontvp == NULL)
(void) cachefs_getfrontfile(cp);
if (cp->c_frontvp) {
/* identify file so fsck knows it is modified */
va.va_mode = 0766;
va.va_mask = AT_MODE;
error = VOP_SETATTR(cp->c_frontvp,
&va, 0, kcred, NULL);
if (error) {
cmn_err(CE_WARN,
"Cannot change ff mode.\n");
}
}
}
}
}
/*
* Marks the cnode as modified.
* Allocates a rl slot for the cnode if necessary.
* Returns 0 for success, !0 if cannot get an rl slot.
*/
static int
cachefs_modified_alloc(cnode_t *cp)
{
fscache_t *fscp = C_TO_FSCACHE(cp);
filegrp_t *fgp = cp->c_filegrp;
int error;
rl_entry_t rl_ent;
ASSERT(MUTEX_HELD(&cp->c_statelock));
/* get the rl slot if needed */
if (cp->c_metadata.md_rlno == 0) {
/* get a metadata slot if we do not have one yet */
if (cp->c_flags & CN_ALLOC_PENDING) {
if (cp->c_filegrp->fg_flags & CFS_FG_ALLOC_ATTR) {
(void) filegrp_allocattr(cp->c_filegrp);
}
error = filegrp_create_metadata(cp->c_filegrp,
&cp->c_metadata, &cp->c_id);
if (error)
return (error);
cp->c_flags &= ~CN_ALLOC_PENDING;
}
/* get a free rl entry */
rl_ent.rl_fileno = cp->c_id.cid_fileno;
rl_ent.rl_local = (cp->c_id.cid_flags & CFS_CID_LOCAL) ? 1 : 0;
rl_ent.rl_fsid = fscp->fs_cfsid;
rl_ent.rl_attrc = 0;
error = cachefs_rl_alloc(fscp->fs_cache, &rl_ent,
&cp->c_metadata.md_rlno);
if (error)
return (error);
cp->c_metadata.md_rltype = CACHEFS_RL_NONE;
/* hold the filegrp so the attrcache file is not gc */
error = filegrp_ffhold(fgp);
if (error) {
cachefs_rlent_moveto(fscp->fs_cache,
CACHEFS_RL_FREE, cp->c_metadata.md_rlno, 0);
cp->c_metadata.md_rlno = 0;
return (error);
}
}
cachefs_modified(cp);
return (0);
}
int
cachefs_vtype_aclok(vnode_t *vp)
{
vtype_t *vtp, oktypes[] = {VREG, VDIR, VFIFO, VNON};
if (vp->v_type == VNON)
return (0);
for (vtp = oktypes; *vtp != VNON; vtp++)
if (vp->v_type == *vtp)
break;
return (*vtp != VNON);
}
static int
cachefs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
caller_context_t *ct)
{
int error = 0;
fscache_t *fscp = C_TO_FSCACHE(VTOC(vp));
/* Assert cachefs compatibility if NFSv4 is in use */
CFS_BACKFS_NFSV4_ASSERT_FSCACHE(fscp);
CFS_BACKFS_NFSV4_ASSERT_CNODE(VTOC(vp));
if (cmd == _PC_FILESIZEBITS) {
u_offset_t maxsize = fscp->fs_offmax;
(*valp) = 0;
while (maxsize != 0) {
maxsize >>= 1;
(*valp)++;
}
(*valp)++;
} else
error = fs_pathconf(vp, cmd, valp, cr, ct);
return (error);
}
|