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
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2017 by Delphix. All rights reserved.
*/
/*
* The idea behind composition-based stacked filesystems is to add a
* vnode to the stack of vnodes for each mount. These vnodes have their
* own set of mount options and filesystem-specific functions, so they
* can modify data or operations before they are passed along. Such a
* filesystem must maintain a mapping from the underlying vnodes to its
* interposing vnodes.
*
* In lofs, this mapping is implemented by a hashtable. Each bucket
* contains a count of the number of nodes currently contained, the
* chain of vnodes, and a lock to protect the list of vnodes. The
* hashtable dynamically grows if the number of vnodes in the table as a
* whole exceeds the size of the table left-shifted by
* lo_resize_threshold. In order to minimize lock contention, there is
* no global lock protecting the hashtable, hence obtaining the
* per-bucket locks consists of a dance to make sure we've actually
* locked the correct bucket. Acquiring a bucket lock doesn't involve
* locking the hashtable itself, so we refrain from freeing old
* hashtables, and store them in a linked list of retired hashtables;
* the list is freed when the filesystem is unmounted.
*/
#include <sys/param.h>
#include <sys/kmem.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/cmn_err.h>
#include <sys/systm.h>
#include <sys/t_lock.h>
#include <sys/debug.h>
#include <sys/atomic.h>
#include <sys/fs/lofs_node.h>
#include <sys/fs/lofs_info.h>
/*
* Due to the hashing algorithm, the size of the hash table needs to be a
* power of 2.
*/
#define LOFS_DEFAULT_HTSIZE (1 << 6)
#define ltablehash(vp, tblsz) ((((intptr_t)(vp))>>10) & ((tblsz)-1))
/*
* The following macros can only be safely used when the desired bucket
* is already locked.
*/
/*
* The lock in the hashtable associated with the given vnode.
*/
#define TABLE_LOCK(vp, li) \
(&(li)->li_hashtable[ltablehash((vp), (li)->li_htsize)].lh_lock)
/*
* The bucket in the hashtable that the given vnode hashes to.
*/
#define TABLE_BUCKET(vp, li) \
((li)->li_hashtable[ltablehash((vp), (li)->li_htsize)].lh_chain)
/*
* Number of elements currently in the bucket that the vnode hashes to.
*/
#define TABLE_COUNT(vp, li) \
((li)->li_hashtable[ltablehash((vp), (li)->li_htsize)].lh_count)
/*
* Grab/Drop the lock for the bucket this vnode hashes to.
*/
#define TABLE_LOCK_ENTER(vp, li) table_lock_enter(vp, li)
#define TABLE_LOCK_EXIT(vp, li) \
mutex_exit(&(li)->li_hashtable[ltablehash((vp), \
(li)->li_htsize)].lh_lock)
static lnode_t *lfind(struct vnode *, struct loinfo *);
static void lsave(lnode_t *, struct loinfo *);
static struct vfs *makelfsnode(struct vfs *, struct loinfo *);
static struct lfsnode *lfsfind(struct vfs *, struct loinfo *);
uint_t lo_resize_threshold = 1;
uint_t lo_resize_factor = 2;
static kmem_cache_t *lnode_cache;
/*
* Since the hashtable itself isn't protected by a lock, obtaining a
* per-bucket lock proceeds as follows:
*
* (a) li->li_htlock protects li->li_hashtable, li->li_htsize, and
* li->li_retired.
*
* (b) Per-bucket locks (lh_lock) protect the contents of the bucket.
*
* (c) Locking order for resizing the hashtable is li_htlock then
* lh_lock.
*
* To grab the bucket lock we:
*
* (1) Stash away the htsize and the pointer to the hashtable to make
* sure neither change while we're using them.
*
* (2) lgrow() updates the pointer to the hashtable before it updates
* the size: the worst case scenario is that we have the wrong size (but
* the correct table), so we hash to the wrong bucket, grab the wrong
* lock, and then realize that things have changed, rewind and start
* again. If both the size and the table changed since we loaded them,
* we'll realize that too and restart.
*
* (3) The protocol for growing the hashtable involves holding *all* the
* locks in the table, hence the unlocking code (TABLE_LOCK_EXIT())
* doesn't need to do any dances, since neither the table nor the size
* can change while any bucket lock is held.
*
* (4) If the hashtable is growing (by thread t1) while another thread
* (t2) is trying to grab a bucket lock, t2 might have a stale reference
* to li->li_htsize:
*
* - t1 grabs all locks in lgrow()
* - t2 loads li->li_htsize and li->li_hashtable
* - t1 changes li->hashtable
* - t2 loads from an offset in the "stale" hashtable and tries to grab
* the relevant mutex.
*
* If t1 had free'd the stale hashtable, t2 would be in trouble. Hence,
* stale hashtables are not freed but stored in a list of "retired"
* hashtables, which is emptied when the filesystem is unmounted.
*/
static void
table_lock_enter(vnode_t *vp, struct loinfo *li)
{
struct lobucket *chain;
uint_t htsize;
uint_t hash;
for (;;) {
htsize = li->li_htsize;
membar_consumer();
chain = (struct lobucket *)li->li_hashtable;
hash = ltablehash(vp, htsize);
mutex_enter(&chain[hash].lh_lock);
if (li->li_hashtable == chain && li->li_htsize == htsize)
break;
mutex_exit(&chain[hash].lh_lock);
}
}
void
lofs_subrinit(void)
{
/*
* Initialize the cache.
*/
lnode_cache = kmem_cache_create("lnode_cache", sizeof (lnode_t),
0, NULL, NULL, NULL, NULL, NULL, 0);
}
void
lofs_subrfini(void)
{
kmem_cache_destroy(lnode_cache);
}
/*
* Initialize a (struct loinfo), and initialize the hashtable to have
* htsize buckets.
*/
void
lsetup(struct loinfo *li, uint_t htsize)
{
li->li_refct = 0;
li->li_lfs = NULL;
if (htsize == 0)
htsize = LOFS_DEFAULT_HTSIZE;
li->li_htsize = htsize;
li->li_hashtable = kmem_zalloc(htsize * sizeof (*li->li_hashtable),
KM_SLEEP);
mutex_init(&li->li_lfslock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&li->li_htlock, NULL, MUTEX_DEFAULT, NULL);
li->li_retired = NULL;
}
/*
* Destroy a (struct loinfo)
*/
void
ldestroy(struct loinfo *li)
{
uint_t i, htsize;
struct lobucket *table;
struct lo_retired_ht *lrhp, *trhp;
mutex_destroy(&li->li_htlock);
mutex_destroy(&li->li_lfslock);
htsize = li->li_htsize;
table = li->li_hashtable;
for (i = 0; i < htsize; i++)
mutex_destroy(&table[i].lh_lock);
kmem_free(table, htsize * sizeof (*li->li_hashtable));
/*
* Free the retired hashtables.
*/
lrhp = li->li_retired;
while (lrhp != NULL) {
trhp = lrhp;
lrhp = lrhp->lrh_next;
kmem_free(trhp->lrh_table,
trhp->lrh_size * sizeof (*li->li_hashtable));
kmem_free(trhp, sizeof (*trhp));
}
li->li_retired = NULL;
}
/*
* Return a looped back vnode for the given vnode.
* If no lnode exists for this vnode create one and put it
* in a table hashed by vnode. If the lnode for
* this vnode is already in the table return it (ref count is
* incremented by lfind). The lnode will be flushed from the
* table when lo_inactive calls freelonode. The creation of
* a new lnode can be forced via the LOF_FORCE flag even if
* the vnode exists in the table. This is used in the creation
* of a terminating lnode when looping is detected. A unique
* lnode is required for the correct evaluation of the current
* working directory.
* NOTE: vp is assumed to be a held vnode.
*/
struct vnode *
makelonode(struct vnode *vp, struct loinfo *li, int flag)
{
lnode_t *lp, *tlp;
struct vfs *vfsp;
vnode_t *nvp;
lp = NULL;
TABLE_LOCK_ENTER(vp, li);
if (flag != LOF_FORCE)
lp = lfind(vp, li);
if ((flag == LOF_FORCE) || (lp == NULL)) {
/*
* Optimistically assume that we won't need to sleep.
*/
lp = kmem_cache_alloc(lnode_cache, KM_NOSLEEP);
nvp = vn_alloc(KM_NOSLEEP);
if (lp == NULL || nvp == NULL) {
TABLE_LOCK_EXIT(vp, li);
/* The lnode allocation may have succeeded, save it */
tlp = lp;
if (tlp == NULL) {
tlp = kmem_cache_alloc(lnode_cache, KM_SLEEP);
}
if (nvp == NULL) {
nvp = vn_alloc(KM_SLEEP);
}
lp = NULL;
TABLE_LOCK_ENTER(vp, li);
if (flag != LOF_FORCE)
lp = lfind(vp, li);
if (lp != NULL) {
kmem_cache_free(lnode_cache, tlp);
vn_free(nvp);
VN_RELE(vp);
goto found_lnode;
}
lp = tlp;
}
atomic_inc_32(&li->li_refct);
vfsp = makelfsnode(vp->v_vfsp, li);
lp->lo_vnode = nvp;
VN_SET_VFS_TYPE_DEV(nvp, vfsp, vp->v_type, vp->v_rdev);
nvp->v_flag |= (vp->v_flag & (VNOMOUNT|VNOMAP|VDIROPEN));
vn_setops(nvp, lo_vnodeops);
nvp->v_data = (caddr_t)lp;
lp->lo_vp = vp;
lp->lo_looping = 0;
lsave(lp, li);
vn_exists(vp);
} else {
VN_RELE(vp);
}
found_lnode:
TABLE_LOCK_EXIT(vp, li);
return (ltov(lp));
}
/*
* Get/Make vfs structure for given real vfs
*/
static struct vfs *
makelfsnode(struct vfs *vfsp, struct loinfo *li)
{
struct lfsnode *lfs;
struct lfsnode *tlfs;
/*
* Don't grab any locks for the fast (common) case.
*/
if (vfsp == li->li_realvfs)
return (li->li_mountvfs);
ASSERT(li->li_refct > 0);
mutex_enter(&li->li_lfslock);
if ((lfs = lfsfind(vfsp, li)) == NULL) {
mutex_exit(&li->li_lfslock);
lfs = kmem_zalloc(sizeof (*lfs), KM_SLEEP);
mutex_enter(&li->li_lfslock);
if ((tlfs = lfsfind(vfsp, li)) != NULL) {
kmem_free(lfs, sizeof (*lfs));
lfs = tlfs;
goto found_lfs;
}
lfs->lfs_realvfs = vfsp;
/*
* Even though the lfsnode is strictly speaking a private
* implementation detail of lofs, it should behave as a regular
* vfs_t for the benefit of the rest of the kernel.
*/
VFS_INIT(&lfs->lfs_vfs, lo_vfsops, (caddr_t)li);
lfs->lfs_vfs.vfs_fstype = li->li_mountvfs->vfs_fstype;
lfs->lfs_vfs.vfs_flag =
((vfsp->vfs_flag | li->li_mflag) & ~li->li_dflag) &
INHERIT_VFS_FLAG;
lfs->lfs_vfs.vfs_bsize = vfsp->vfs_bsize;
lfs->lfs_vfs.vfs_dev = vfsp->vfs_dev;
lfs->lfs_vfs.vfs_fsid = vfsp->vfs_fsid;
if (vfsp->vfs_mntpt != NULL) {
lfs->lfs_vfs.vfs_mntpt = vfs_getmntpoint(vfsp);
/* Leave a reference to the mountpoint */
}
(void) VFS_ROOT(vfsp, &lfs->lfs_realrootvp);
/*
* We use 1 instead of 0 as the value to associate with
* an idle lfs_vfs. This is to prevent VFS_RELE()
* trying to kmem_free() our lfs_t (which is the wrong
* size).
*/
VFS_HOLD(&lfs->lfs_vfs);
lfs->lfs_next = li->li_lfs;
li->li_lfs = lfs;
vfs_propagate_features(vfsp, &lfs->lfs_vfs);
}
found_lfs:
VFS_HOLD(&lfs->lfs_vfs);
mutex_exit(&li->li_lfslock);
return (&lfs->lfs_vfs);
}
/*
* Free lfs node since no longer in use
*/
static void
freelfsnode(struct lfsnode *lfs, struct loinfo *li)
{
struct lfsnode *prev = NULL;
struct lfsnode *this;
ASSERT(MUTEX_HELD(&li->li_lfslock));
ASSERT(li->li_refct > 0);
for (this = li->li_lfs; this != NULL; this = this->lfs_next) {
if (this == lfs) {
ASSERT(lfs->lfs_vfs.vfs_count == 1);
if (prev == NULL)
li->li_lfs = lfs->lfs_next;
else
prev->lfs_next = lfs->lfs_next;
if (lfs->lfs_realrootvp != NULL) {
VN_RELE(lfs->lfs_realrootvp);
}
if (lfs->lfs_vfs.vfs_mntpt != NULL)
refstr_rele(lfs->lfs_vfs.vfs_mntpt);
if (lfs->lfs_vfs.vfs_implp != NULL) {
ASSERT(lfs->lfs_vfs.vfs_femhead == NULL);
ASSERT(lfs->lfs_vfs.vfs_vskap == NULL);
ASSERT(lfs->lfs_vfs.vfs_fstypevsp == NULL);
kmem_free(lfs->lfs_vfs.vfs_implp,
sizeof (vfs_impl_t));
}
sema_destroy(&lfs->lfs_vfs.vfs_reflock);
kmem_free(lfs, sizeof (struct lfsnode));
return;
}
prev = this;
}
panic("freelfsnode");
/*NOTREACHED*/
}
/*
* Find lfs given real vfs and mount instance(li)
*/
static struct lfsnode *
lfsfind(struct vfs *vfsp, struct loinfo *li)
{
struct lfsnode *lfs;
ASSERT(MUTEX_HELD(&li->li_lfslock));
/*
* We need to handle the case where a UFS filesystem was forced
* unmounted and then a subsequent mount got the same vfs
* structure. If the new mount lies in the lofs hierarchy, then
* this will confuse lofs, because the original vfsp (of the
* forced unmounted filesystem) is still around. We check for
* this condition here.
*
* If we find a cache vfsp hit, then we check to see if the
* cached filesystem was forced unmounted. Skip all such
* entries. This should be safe to do since no
* makelonode()->makelfsnode()->lfsfind() calls should be
* generated for such force-unmounted filesystems (because (ufs)
* lookup would've returned an error).
*/
for (lfs = li->li_lfs; lfs != NULL; lfs = lfs->lfs_next) {
if (lfs->lfs_realvfs == vfsp) {
struct vnode *realvp;
realvp = lfs->lfs_realrootvp;
if (realvp == NULL)
continue;
if (realvp->v_vfsp == NULL || realvp->v_type == VBAD)
continue;
return (lfs);
}
}
return (NULL);
}
/*
* Find real vfs given loopback vfs
*/
struct vfs *
lo_realvfs(struct vfs *vfsp, struct vnode **realrootvpp)
{
struct loinfo *li = vtoli(vfsp);
struct lfsnode *lfs;
ASSERT(li->li_refct > 0);
if (vfsp == li->li_mountvfs) {
if (realrootvpp != NULL)
*realrootvpp = vtol(li->li_rootvp)->lo_vp;
return (li->li_realvfs);
}
mutex_enter(&li->li_lfslock);
for (lfs = li->li_lfs; lfs != NULL; lfs = lfs->lfs_next) {
if (vfsp == &lfs->lfs_vfs) {
if (realrootvpp != NULL)
*realrootvpp = lfs->lfs_realrootvp;
mutex_exit(&li->li_lfslock);
return (lfs->lfs_realvfs);
}
}
panic("lo_realvfs");
/*NOTREACHED*/
}
/*
* Lnode lookup stuff.
* These routines maintain a table of lnodes hashed by vp so
* that the lnode for a vp can be found if it already exists.
*
* NB: A lofs shadow vnode causes exactly one VN_HOLD() on the
* underlying vnode.
*/
/*
* Retire old hashtables.
*/
static void
lretire(struct loinfo *li, struct lobucket *table, uint_t size)
{
struct lo_retired_ht *lrhp;
lrhp = kmem_alloc(sizeof (*lrhp), KM_SLEEP);
lrhp->lrh_table = table;
lrhp->lrh_size = size;
mutex_enter(&li->li_htlock);
lrhp->lrh_next = li->li_retired;
li->li_retired = lrhp;
mutex_exit(&li->li_htlock);
}
/*
* Grow the hashtable.
*/
static void
lgrow(struct loinfo *li, uint_t newsize)
{
uint_t oldsize;
uint_t i;
struct lobucket *oldtable, *newtable;
/*
* It's OK to not have enough memory to resize the hashtable.
* We'll go down this path the next time we add something to the
* table, and retry the allocation then.
*/
if ((newtable = kmem_zalloc(newsize * sizeof (*li->li_hashtable),
KM_NOSLEEP)) == NULL)
return;
mutex_enter(&li->li_htlock);
if (newsize <= li->li_htsize) {
mutex_exit(&li->li_htlock);
kmem_free(newtable, newsize * sizeof (*li->li_hashtable));
return;
}
oldsize = li->li_htsize;
oldtable = li->li_hashtable;
/*
* Grab all locks so TABLE_LOCK_ENTER() calls block until the
* resize is complete.
*/
for (i = 0; i < oldsize; i++)
mutex_enter(&oldtable[i].lh_lock);
/*
* li->li_hashtable gets set before li->li_htsize, so in the
* time between the two assignments, callers of
* TABLE_LOCK_ENTER() cannot hash to a bucket beyond oldsize,
* hence we only need to grab the locks up to oldsize.
*/
for (i = 0; i < oldsize; i++)
mutex_enter(&newtable[i].lh_lock);
/*
* Rehash.
*/
for (i = 0; i < oldsize; i++) {
lnode_t *tlp, *nlp;
for (tlp = oldtable[i].lh_chain; tlp != NULL; tlp = nlp) {
uint_t hash = ltablehash(tlp->lo_vp, newsize);
nlp = tlp->lo_next;
tlp->lo_next = newtable[hash].lh_chain;
newtable[hash].lh_chain = tlp;
newtable[hash].lh_count++;
}
}
/*
* As soon as we store the new hashtable, future locking operations
* will use it. Therefore, we must ensure that all the state we've
* just established reaches global visibility before the new hashtable
* does.
*/
membar_producer();
li->li_hashtable = newtable;
/*
* table_lock_enter() relies on the fact that li->li_hashtable
* is set to its new value before li->li_htsize.
*/
membar_producer();
li->li_htsize = newsize;
/*
* The new state is consistent now, so we can drop all the locks.
*/
for (i = 0; i < oldsize; i++) {
mutex_exit(&newtable[i].lh_lock);
mutex_exit(&oldtable[i].lh_lock);
}
mutex_exit(&li->li_htlock);
lretire(li, oldtable, oldsize);
}
/*
* Put a lnode in the table
*/
static void
lsave(lnode_t *lp, struct loinfo *li)
{
ASSERT(lp->lo_vp);
ASSERT(MUTEX_HELD(TABLE_LOCK(lp->lo_vp, li)));
#ifdef LODEBUG
lo_dprint(4, "lsave lp %p hash %d\n",
lp, ltablehash(lp->lo_vp, li));
#endif
TABLE_COUNT(lp->lo_vp, li)++;
lp->lo_next = TABLE_BUCKET(lp->lo_vp, li);
TABLE_BUCKET(lp->lo_vp, li) = lp;
if (li->li_refct > (li->li_htsize << lo_resize_threshold)) {
TABLE_LOCK_EXIT(lp->lo_vp, li);
lgrow(li, li->li_htsize << lo_resize_factor);
TABLE_LOCK_ENTER(lp->lo_vp, li);
}
}
/*
* Our version of vfs_rele() that stops at 1 instead of 0, and calls
* freelfsnode() instead of kmem_free().
*/
static void
lfs_rele(struct lfsnode *lfs, struct loinfo *li)
{
vfs_t *vfsp = &lfs->lfs_vfs;
ASSERT(MUTEX_HELD(&li->li_lfslock));
ASSERT(vfsp->vfs_count > 1);
if (atomic_dec_32_nv(&vfsp->vfs_count) == 1)
freelfsnode(lfs, li);
}
/*
* Remove a lnode from the table
*/
void
freelonode(lnode_t *lp)
{
lnode_t *lt;
lnode_t *ltprev = NULL;
struct lfsnode *lfs, *nextlfs;
struct vfs *vfsp;
struct vnode *vp = ltov(lp);
struct vnode *realvp = realvp(vp);
struct loinfo *li = vtoli(vp->v_vfsp);
#ifdef LODEBUG
lo_dprint(4, "freelonode lp %p hash %d\n",
lp, ltablehash(lp->lo_vp, li));
#endif
TABLE_LOCK_ENTER(lp->lo_vp, li);
mutex_enter(&vp->v_lock);
if (vp->v_count > 1) {
VN_RELE_LOCKED(vp);
mutex_exit(&vp->v_lock);
TABLE_LOCK_EXIT(lp->lo_vp, li);
return;
}
mutex_exit(&vp->v_lock);
for (lt = TABLE_BUCKET(lp->lo_vp, li); lt != NULL;
ltprev = lt, lt = lt->lo_next) {
if (lt == lp) {
#ifdef LODEBUG
lo_dprint(4, "freeing %p, vfsp %p\n",
vp, vp->v_vfsp);
#endif
atomic_dec_32(&li->li_refct);
vfsp = vp->v_vfsp;
vn_invalid(vp);
if (vfsp != li->li_mountvfs) {
mutex_enter(&li->li_lfslock);
/*
* Check for unused lfs
*/
lfs = li->li_lfs;
while (lfs != NULL) {
nextlfs = lfs->lfs_next;
if (vfsp == &lfs->lfs_vfs) {
lfs_rele(lfs, li);
break;
}
if (lfs->lfs_vfs.vfs_count == 1) {
/*
* Lfs is idle
*/
freelfsnode(lfs, li);
}
lfs = nextlfs;
}
mutex_exit(&li->li_lfslock);
}
if (ltprev == NULL) {
TABLE_BUCKET(lt->lo_vp, li) = lt->lo_next;
} else {
ltprev->lo_next = lt->lo_next;
}
TABLE_COUNT(lt->lo_vp, li)--;
TABLE_LOCK_EXIT(lt->lo_vp, li);
kmem_cache_free(lnode_cache, lt);
vn_free(vp);
VN_RELE(realvp);
return;
}
}
panic("freelonode");
/*NOTREACHED*/
}
/*
* Lookup a lnode by vp
*/
static lnode_t *
lfind(struct vnode *vp, struct loinfo *li)
{
lnode_t *lt;
ASSERT(MUTEX_HELD(TABLE_LOCK(vp, li)));
lt = TABLE_BUCKET(vp, li);
while (lt != NULL) {
if (lt->lo_vp == vp) {
VN_HOLD(ltov(lt));
return (lt);
}
lt = lt->lo_next;
}
return (NULL);
}
#ifdef LODEBUG
static int lofsdebug;
#endif /* LODEBUG */
/*
* Utilities used by both client and server
* Standard levels:
* 0) no debugging
* 1) hard failures
* 2) soft failures
* 3) current test software
* 4) main procedure entry points
* 5) main procedure exit points
* 6) utility procedure entry points
* 7) utility procedure exit points
* 8) obscure procedure entry points
* 9) obscure procedure exit points
* 10) random stuff
* 11) all <= 1
* 12) all <= 2
* 13) all <= 3
* ...
*/
#ifdef LODEBUG
/*VARARGS2*/
lo_dprint(int level, char *str, int a1, int a2, int a3, int a4, int a5, int a6,
int a7, int a8, int a9)
{
if (lofsdebug == level || (lofsdebug > 10 && (lofsdebug - 10) >= level))
printf(str, a1, a2, a3, a4, a5, a6, a7, a8, a9);
}
#endif
/*
* 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) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2024 Oxide Computer Company
*/
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/uio.h>
#include <sys/pathname.h>
#include <sys/kmem.h>
#include <sys/cred.h>
#include <sys/statvfs.h>
#include <sys/fs/lofs_info.h>
#include <sys/fs/lofs_node.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/mkdev.h>
#include <sys/priv.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cmn_err.h>
#include <sys/policy.h>
#include <sys/tsol/label.h>
#include "fs/fs_subr.h"
/*
* This is the loadable module wrapper.
*/
#include <sys/modctl.h>
static mntopts_t lofs_mntopts;
static int lofsinit(int, char *);
static vfsdef_t vfw = {
VFSDEF_VERSION,
"lofs",
lofsinit,
VSW_HASPROTO|VSW_STATS|VSW_ZMOUNT,
&lofs_mntopts
};
/*
* LOFS mount options table
*/
static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
static char *sub_cancel[] = { MNTOPT_LOFS_NOSUB, NULL };
static char *nosub_cancel[] = { MNTOPT_LOFS_SUB, NULL };
static mntopt_t mntopts[] = {
/*
* option name cancel option default arg flags
* private data
*/
{ MNTOPT_XATTR, xattr_cancel, NULL, 0,
(void *)0 },
{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0,
(void *)0 },
{ MNTOPT_LOFS_SUB, sub_cancel, NULL, 0,
(void *)0 },
{ MNTOPT_LOFS_NOSUB, nosub_cancel, NULL, 0,
(void *)0 },
};
static mntopts_t lofs_mntopts = {
sizeof (mntopts) / sizeof (mntopt_t),
mntopts
};
/*
* Module linkage information for the kernel.
*/
static struct modlfs modlfs = {
&mod_fsops, "filesystem for lofs", &vfw
};
static struct modlinkage modlinkage = {
MODREV_1, (void *)&modlfs, NULL
};
/*
* This is the module initialization routine.
*/
int
_init(void)
{
int status;
lofs_subrinit();
status = mod_install(&modlinkage);
if (status != 0) {
/*
* Cleanup previously initialized work.
*/
lofs_subrfini();
}
return (status);
}
/*
* Don't allow the lofs module to be unloaded for now.
* There is a memory leak if it gets unloaded.
*/
int
_fini(void)
{
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int lofsfstype;
vfsops_t *lo_vfsops;
/*
* lo mount vfsop
* Set up mount info record and attach it to vfs struct.
*/
/*ARGSUSED*/
static int
lo_mount(struct vfs *vfsp, struct vnode *vp, struct mounta *uap,
struct cred *cr)
{
int error;
struct vnode *srootvp = NULL; /* the server's root */
struct vnode *realrootvp;
struct loinfo *li;
int nodev;
nodev = vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL);
if ((error = secpolicy_fs_mount(cr, vp, vfsp)) != 0)
return (EPERM);
/*
* Loopback devices which get "nodevices" added can be done without
* "nodevices" set because we cannot import devices into a zone
* with loopback. Note that we have all zone privileges when
* this happens; if not, we'd have gotten "nosuid".
*/
if (!nodev && vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
vfs_setmntopt(vfsp, MNTOPT_DEVICES, NULL, VFS_NODISPLAY);
mutex_enter(&vp->v_lock);
if (!(uap->flags & MS_OVERLAY) &&
(vp->v_count != 1 || (vp->v_flag & VROOT))) {
mutex_exit(&vp->v_lock);
return (EBUSY);
}
mutex_exit(&vp->v_lock);
/*
* Find real root, and make vfs point to real vfs
*/
if (error = lookupname(uap->spec, (uap->flags & MS_SYSSPACE) ?
UIO_SYSSPACE : UIO_USERSPACE, FOLLOW, NULLVPP, &realrootvp))
return (error);
/*
* Enforce MAC policy if needed.
*
* Loopback mounts must not allow writing up. The dominance test
* is intended to prevent a global zone caller from accidentally
* creating write-up conditions between two labeled zones.
* Local zones can't violate MAC on their own without help from
* the global zone because they can't name a pathname that
* they don't already have.
*
* The special case check for the NET_MAC_AWARE process flag is
* to support the case of the automounter in the global zone. We
* permit automounting of local zone directories such as home
* directories, into the global zone as required by setlabel,
* zonecopy, and saving of desktop sessions. Such mounts are
* trusted not to expose the contents of one zone's directories
* to another by leaking them through the global zone.
*/
if (is_system_labeled() && crgetzoneid(cr) == GLOBAL_ZONEID) {
char specname[MAXPATHLEN];
zone_t *from_zptr;
zone_t *to_zptr;
if (vnodetopath(NULL, realrootvp, specname,
sizeof (specname), CRED()) != 0) {
VN_RELE(realrootvp);
return (EACCES);
}
from_zptr = zone_find_by_path(specname);
to_zptr = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
/*
* Special case for scratch zones used for Live Upgrade:
* this is used to mount the zone's root from /root to /a in
* the scratch zone. As with the other special case, this
* appears to be outside of the zone because it's not under
* the zone rootpath, which is $ZONEPATH/lu in the scratch
* zone case.
*/
if (from_zptr != to_zptr &&
!(to_zptr->zone_flags & ZF_IS_SCRATCH)) {
/*
* We know at this point that the labels aren't equal
* because the zone pointers aren't equal, and zones
* can't share a label.
*
* If the source is the global zone then making
* it available to a local zone must be done in
* read-only mode as the label will become admin_low.
*
* If it is a mount between local zones then if
* the current process is in the global zone and has
* the NET_MAC_AWARE flag, then regular read-write
* access is allowed. If it's in some other zone, but
* the label on the mount point dominates the original
* source, then allow the mount as read-only
* ("read-down").
*/
if (from_zptr->zone_id == GLOBAL_ZONEID) {
/* make the mount read-only */
vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
} else { /* cross-zone mount */
if (to_zptr->zone_id == GLOBAL_ZONEID &&
/* LINTED: no consequent */
getpflags(NET_MAC_AWARE, cr) != 0) {
/* Allow the mount as read-write */
} else if (bldominates(
label2bslabel(to_zptr->zone_slabel),
label2bslabel(from_zptr->zone_slabel))) {
/* make the mount read-only */
vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
} else {
VN_RELE(realrootvp);
zone_rele(to_zptr);
zone_rele(from_zptr);
return (EACCES);
}
}
}
zone_rele(to_zptr);
zone_rele(from_zptr);
}
/*
* realrootvp may be an AUTOFS node, in which case we perform a
* VOP_ACCESS() to trigger the mount of the intended filesystem.
* This causes a loopback mount of the intended filesystem instead
* of the AUTOFS filesystem.
*
* If a lofs mount creates a mount loop (such that a lofs vfs is
* mounted on an autofs node and that lofs vfs points back to the
* autofs node which it is mounted on) then a VOP_ACCESS call will
* create a deadlock. Once this deadlock is released, VOP_ACCESS will
* return EINTR. In such a case we don't want the lofs vfs to be
* created as the loop could panic the system.
*/
if ((error = VOP_ACCESS(realrootvp, 0, 0, cr, NULL)) != 0) {
VN_RELE(realrootvp);
return (error);
}
/*
* We're interested in the top most filesystem.
* This is specially important when uap->spec is a trigger
* AUTOFS node, since we're really interested in mounting the
* filesystem AUTOFS mounted as result of the VOP_ACCESS()
* call not the AUTOFS node itself.
*/
if (vn_mountedvfs(realrootvp) != NULL) {
if (error = traverse(&realrootvp)) {
VN_RELE(realrootvp);
return (error);
}
}
/*
* Allocate a vfs info struct and attach it
*/
li = kmem_zalloc(sizeof (struct loinfo), KM_SLEEP);
li->li_realvfs = realrootvp->v_vfsp;
li->li_mountvfs = vfsp;
/*
* Set mount flags to be inherited by loopback vfs's
*/
if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
li->li_mflag |= VFS_RDONLY;
}
if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
li->li_mflag |= (VFS_NOSETUID|VFS_NODEVICES);
}
if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
li->li_mflag |= VFS_NODEVICES;
}
if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
li->li_mflag |= VFS_NOSETUID;
}
/*
* Permissive flags are added to the "deny" bitmap.
*/
if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
li->li_dflag |= VFS_XATTR;
}
if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
li->li_dflag |= VFS_NBMAND;
}
/*
* Propagate inheritable mount flags from the real vfs.
*/
if ((li->li_realvfs->vfs_flag & VFS_RDONLY) &&
!vfs_optionisset(vfsp, MNTOPT_RO, NULL))
vfs_setmntopt(vfsp, MNTOPT_RO, NULL,
VFS_NODISPLAY);
if ((li->li_realvfs->vfs_flag & VFS_NOSETUID) &&
!vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
vfs_setmntopt(vfsp, MNTOPT_NOSETUID, NULL,
VFS_NODISPLAY);
if ((li->li_realvfs->vfs_flag & VFS_NODEVICES) &&
!vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
vfs_setmntopt(vfsp, MNTOPT_NODEVICES, NULL,
VFS_NODISPLAY);
/*
* Permissive flags such as VFS_XATTR, as opposed to restrictive flags
* such as VFS_RDONLY, are handled differently. An explicit
* MNTOPT_NOXATTR should override the underlying filesystem's VFS_XATTR.
*/
if ((li->li_realvfs->vfs_flag & VFS_XATTR) &&
!vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL) &&
!vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
vfs_setmntopt(vfsp, MNTOPT_XATTR, NULL,
VFS_NODISPLAY);
if ((li->li_realvfs->vfs_flag & VFS_NBMAND) &&
!vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL) &&
!vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL))
vfs_setmntopt(vfsp, MNTOPT_NBMAND, NULL,
VFS_NODISPLAY);
li->li_refct = 0;
vfsp->vfs_data = (caddr_t)li;
vfsp->vfs_bcount = 0;
vfsp->vfs_fstype = lofsfstype;
vfsp->vfs_bsize = li->li_realvfs->vfs_bsize;
vfsp->vfs_dev = li->li_realvfs->vfs_dev;
vfsp->vfs_fsid.val[0] = li->li_realvfs->vfs_fsid.val[0];
vfsp->vfs_fsid.val[1] = li->li_realvfs->vfs_fsid.val[1];
if (vfs_optionisset(vfsp, MNTOPT_LOFS_NOSUB, NULL)) {
li->li_flag |= LO_NOSUB;
}
/*
* Propagate any VFS features
*/
vfs_propagate_features(li->li_realvfs, vfsp);
/*
* Setup the hashtable. If the root of this mount isn't a directory,
* there's no point in allocating a large hashtable. A table with one
* bucket is sufficient.
*/
if (realrootvp->v_type != VDIR)
lsetup(li, 1);
else
lsetup(li, 0);
/*
* Make the root vnode
*/
srootvp = makelonode(realrootvp, li, 0);
srootvp->v_flag |= VROOT;
li->li_rootvp = srootvp;
#ifdef LODEBUG
lo_dprint(4, "lo_mount: vfs %p realvfs %p root %p realroot %p li %p\n",
vfsp, li->li_realvfs, srootvp, realrootvp, li);
#endif
return (0);
}
/*
* Undo loopback mount
*/
static int
lo_unmount(struct vfs *vfsp, int flag, struct cred *cr)
{
struct loinfo *li;
if (secpolicy_fs_unmount(cr, vfsp) != 0)
return (EPERM);
/*
* Forced unmount is not supported by this file system
* and thus, ENOTSUP, is being returned.
*/
if (flag & MS_FORCE)
return (ENOTSUP);
li = vtoli(vfsp);
#ifdef LODEBUG
lo_dprint(4, "lo_unmount(%p) li %p\n", vfsp, li);
#endif
if (li->li_refct != 1 || li->li_rootvp->v_count != 1) {
#ifdef LODEBUG
lo_dprint(4, "refct %d v_ct %d\n", li->li_refct,
li->li_rootvp->v_count);
#endif
return (EBUSY);
}
VN_RELE(li->li_rootvp);
return (0);
}
/*
* Find root of lofs mount.
*/
static int
lo_root(struct vfs *vfsp, struct vnode **vpp)
{
*vpp = vtoli(vfsp)->li_rootvp;
#ifdef LODEBUG
lo_dprint(4, "lo_root(0x%p) = %p\n", vfsp, *vpp);
#endif
/*
* If the root of the filesystem is a special file, return the specvp
* version of the vnode. We don't save the specvp vnode in our
* hashtable since that's exclusively for lnodes.
*/
if (IS_DEVVP(*vpp)) {
struct vnode *svp;
svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, kcred);
if (svp == NULL)
return (ENOSYS);
*vpp = svp;
} else {
VN_HOLD(*vpp);
}
return (0);
}
/*
* Get file system statistics.
*/
static int
lo_statvfs(register struct vfs *vfsp, struct statvfs64 *sbp)
{
vnode_t *realrootvp;
#ifdef LODEBUG
lo_dprint(4, "lostatvfs %p\n", vfsp);
#endif
/*
* Using realrootvp->v_vfsp (instead of the realvfsp that was
* cached) is necessary to make lofs work woth forced UFS unmounts.
* In the case of a forced unmount, UFS stores a set of dummy vfsops
* in all the (i)vnodes in the filesystem. The dummy ops simply
* returns back EIO.
*/
(void) lo_realvfs(vfsp, &realrootvp);
if (realrootvp != NULL)
return (VFS_STATVFS(realrootvp->v_vfsp, sbp));
else
return (EIO);
}
/*
* LOFS doesn't have any data or metadata to flush, pending I/O on the
* underlying filesystem will be flushed when such filesystem is synched.
*/
/* ARGSUSED */
static int
lo_sync(struct vfs *vfsp, short flag, struct cred *cr)
{
#ifdef LODEBUG
lo_dprint(4, "lo_sync: %p\n", vfsp);
#endif
return (0);
}
/*
* While the general sync(2) entry point above assumes that the underlying fs
* will be synced, we treat this as a directed blocking sync on the file system
* which means we should attempt the underlying file system.
*/
static int
lo_syncfs(vfs_t *vfsp, uint64_t flags, cred_t *cr)
{
vfs_t *realvfs;
#ifdef LODEBUG
lo_dprint(4, "lo_syncfs: %p\n", vfsp);
#endif
realvfs = lo_realvfs(vfsp, NULL);
if (realvfs != NULL) {
return (VFS_SYNCFS(realvfs, flags, cr));
} else {
return (EIO);
}
}
/*
* Obtain the vnode from the underlying filesystem.
*/
static int
lo_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
{
vnode_t *realrootvp;
#ifdef LODEBUG
lo_dprint(4, "lo_vget: %p\n", vfsp);
#endif
(void) lo_realvfs(vfsp, &realrootvp);
if (realrootvp != NULL)
return (VFS_VGET(realrootvp->v_vfsp, vpp, fidp));
else
return (EIO);
}
/*
* Free mount-specific data.
*/
static void
lo_freevfs(struct vfs *vfsp)
{
struct loinfo *li = vtoli(vfsp);
ldestroy(li);
kmem_free(li, sizeof (struct loinfo));
}
static int
lofsinit(int fstyp, char *name)
{
static const fs_operation_def_t lo_vfsops_template[] = {
VFSNAME_MOUNT, { .vfs_mount = lo_mount },
VFSNAME_UNMOUNT, { .vfs_unmount = lo_unmount },
VFSNAME_ROOT, { .vfs_root = lo_root },
VFSNAME_STATVFS, { .vfs_statvfs = lo_statvfs },
VFSNAME_SYNC, { .vfs_sync = lo_sync },
VFSNAME_VGET, { .vfs_vget = lo_vget },
VFSNAME_FREEVFS, { .vfs_freevfs = lo_freevfs },
VFSNAME_SYNCFS, { .vfs_syncfs = lo_syncfs },
NULL, NULL
};
int error;
error = vfs_setfsops(fstyp, lo_vfsops_template, &lo_vfsops);
if (error != 0) {
cmn_err(CE_WARN, "lofsinit: bad vfs ops template");
return (error);
}
error = vn_make_ops(name, lo_vnodeops_template, &lo_vnodeops);
if (error != 0) {
(void) vfs_freevfsops_by_type(fstyp);
cmn_err(CE_WARN, "lofsinit: bad vnode ops template");
return (error);
}
lofsfstype = fstyp;
return (0);
}
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2015 Joyent, Inc.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/uio.h>
#include <sys/cred.h>
#include <sys/pathname.h>
#include <sys/debug.h>
#include <sys/fs/lofs_node.h>
#include <sys/fs/lofs_info.h>
#include <fs/fs_subr.h>
#include <vm/as.h>
#include <vm/seg.h>
/*
* These are the vnode ops routines which implement the vnode interface to
* the looped-back file system. These routines just take their parameters,
* and then calling the appropriate real vnode routine(s) to do the work.
*/
static int
lo_open(vnode_t **vpp, int flag, struct cred *cr, caller_context_t *ct)
{
vnode_t *vp = *vpp;
vnode_t *rvp;
vnode_t *oldvp;
int error;
#ifdef LODEBUG
lo_dprint(4, "lo_open vp %p cnt=%d realvp %p cnt=%d\n",
vp, vp->v_count, realvp(vp), realvp(vp)->v_count);
#endif
oldvp = vp;
vp = rvp = realvp(vp);
/*
* Need to hold new reference to vp since VOP_OPEN() may
* decide to release it.
*/
VN_HOLD(vp);
error = VOP_OPEN(&rvp, flag, cr, ct);
if (!error && rvp != vp) {
/*
* the FS which we called should have released the
* new reference on vp
*/
*vpp = makelonode(rvp, vtoli(oldvp->v_vfsp), 0);
if ((*vpp)->v_type == VDIR) {
/*
* Copy over any looping flags to the new lnode.
*/
(vtol(*vpp))->lo_looping |= (vtol(oldvp))->lo_looping;
}
if (IS_DEVVP(*vpp)) {
vnode_t *svp;
svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
VN_RELE(*vpp);
if (svp == NULL)
error = ENOSYS;
else
*vpp = svp;
}
VN_RELE(oldvp);
} else {
ASSERT(rvp->v_count > 1);
VN_RELE(rvp);
}
return (error);
}
static int
lo_close(
vnode_t *vp,
int flag,
int count,
offset_t offset,
struct cred *cr,
caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_close vp %p realvp %p\n", vp, realvp(vp));
#endif
vp = realvp(vp);
return (VOP_CLOSE(vp, flag, count, offset, cr, ct));
}
static int
lo_read(vnode_t *vp, struct uio *uiop, int ioflag, struct cred *cr,
caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_read vp %p realvp %p\n", vp, realvp(vp));
#endif
vp = realvp(vp);
return (VOP_READ(vp, uiop, ioflag, cr, ct));
}
static int
lo_write(vnode_t *vp, struct uio *uiop, int ioflag, struct cred *cr,
caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_write vp %p realvp %p\n", vp, realvp(vp));
#endif
vp = realvp(vp);
return (VOP_WRITE(vp, uiop, ioflag, cr, ct));
}
static int
lo_ioctl(
vnode_t *vp,
int cmd,
intptr_t arg,
int flag,
struct cred *cr,
int *rvalp,
caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_ioctl vp %p realvp %p\n", vp, realvp(vp));
#endif
vp = realvp(vp);
return (VOP_IOCTL(vp, cmd, arg, flag, cr, rvalp, ct));
}
static int
lo_setfl(vnode_t *vp, int oflags, int nflags, cred_t *cr, caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_SETFL(vp, oflags, nflags, cr, ct));
}
static int
lo_getattr(
vnode_t *vp,
struct vattr *vap,
int flags,
struct cred *cr,
caller_context_t *ct)
{
int error;
#ifdef LODEBUG
lo_dprint(4, "lo_getattr vp %p realvp %p\n", vp, realvp(vp));
#endif
if (error = VOP_GETATTR(realvp(vp), vap, flags, cr, ct))
return (error);
return (0);
}
static int
lo_setattr(
vnode_t *vp,
struct vattr *vap,
int flags,
struct cred *cr,
caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_setattr vp %p realvp %p\n", vp, realvp(vp));
#endif
vp = realvp(vp);
return (VOP_SETATTR(vp, vap, flags, cr, ct));
}
static int
lo_access(
vnode_t *vp,
int mode,
int flags,
struct cred *cr,
caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_access vp %p realvp %p\n", vp, realvp(vp));
#endif
if (mode & VWRITE) {
if (vp->v_type == VREG && vn_is_readonly(vp))
return (EROFS);
}
vp = realvp(vp);
return (VOP_ACCESS(vp, mode, flags, cr, ct));
}
static int
lo_fsync(vnode_t *vp, int syncflag, struct cred *cr, caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_fsync vp %p realvp %p\n", vp, realvp(vp));
#endif
vp = realvp(vp);
return (VOP_FSYNC(vp, syncflag, cr, ct));
}
/*ARGSUSED*/
static void
lo_inactive(vnode_t *vp, struct cred *cr, caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_inactive %p, realvp %p\n", vp, realvp(vp));
#endif
freelonode(vtol(vp));
}
/* ARGSUSED */
static int
lo_fid(vnode_t *vp, struct fid *fidp, caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_fid %p, realvp %p\n", vp, realvp(vp));
#endif
vp = realvp(vp);
return (VOP_FID(vp, fidp, ct));
}
/*
* Given a vnode of lofs type, lookup nm name and
* return a shadow vnode (of lofs type) of the
* real vnode found.
*
* Due to the nature of lofs, there is a potential
* looping in path traversal.
*
* starting from the mount point of an lofs;
* a loop is defined to be a traversal path
* where the mount point or the real vnode of
* the root of this lofs is encountered twice.
* Once at the start of traversal and second
* when the looping is found.
*
* When a loop is encountered, a shadow of the
* covered vnode is returned to stop the looping.
*
* This normally works, but with the advent of
* the new automounter, returning the shadow of the
* covered vnode (autonode, in this case) does not
* stop the loop. Because further lookup on this
* lonode will cause the autonode to call lo_lookup()
* on the lonode covering it.
*
* example "/net/jurassic/net/jurassic" is a loop.
* returning the shadow of the autonode corresponding to
* "/net/jurassic/net/jurassic" will not terminate the
* loop. To solve this problem we allow the loop to go
* through one more level component lookup. Whichever
* directory is then looked up in "/net/jurassic/net/jurassic"
* the vnode returned is the vnode covered by the autonode
* "net" and this will terminate the loop.
*
* Lookup for dot dot has to be dealt with separately.
* It will be nice to have a "one size fits all" kind
* of solution, so that we don't have so many ifs statement
* in the lo_lookup() to handle dotdot. But, since
* there are so many special cases to handle different
* kinds looping above, we need special codes to handle
* dotdot lookup as well.
*/
static int
lo_lookup(
vnode_t *dvp,
char *nm,
vnode_t **vpp,
struct pathname *pnp,
int flags,
vnode_t *rdir,
struct cred *cr,
caller_context_t *ct,
int *direntflags,
pathname_t *realpnp)
{
vnode_t *vp = NULL, *tvp = NULL, *nonlovp;
int error, is_indirectloop;
vnode_t *realdvp = realvp(dvp);
struct loinfo *li = vtoli(dvp->v_vfsp);
int looping = 0;
int autoloop = 0;
int doingdotdot = 0;
int nosub = 0;
int mkflag = 0;
/*
* If name is empty and no XATTR flags are set, then return
* dvp (empty name == lookup "."). If an XATTR flag is set
* then we need to call VOP_LOOKUP to get the xattr dir.
*/
if (nm[0] == '\0' && ! (flags & (CREATE_XATTR_DIR|LOOKUP_XATTR))) {
VN_HOLD(dvp);
*vpp = dvp;
return (0);
}
if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
doingdotdot++;
/*
* Handle ".." out of mounted filesystem
*/
while ((realdvp->v_flag & VROOT) && realdvp != rootdir) {
realdvp = realdvp->v_vfsp->vfs_vnodecovered;
ASSERT(realdvp != NULL);
}
}
*vpp = NULL; /* default(error) case */
/*
* Do the normal lookup
*/
if (error = VOP_LOOKUP(realdvp, nm, &vp, pnp, flags, rdir, cr,
ct, direntflags, realpnp)) {
vp = NULL;
goto out;
}
/*
* We do this check here to avoid returning a stale file handle to the
* caller.
*/
if (nm[0] == '.' && nm[1] == '\0') {
ASSERT(vp == realdvp);
VN_HOLD(dvp);
VN_RELE(vp);
*vpp = dvp;
return (0);
}
if (doingdotdot) {
if ((vtol(dvp))->lo_looping & LO_LOOPING) {
vfs_t *vfsp;
error = vn_vfsrlock_wait(realdvp);
if (error)
goto out;
vfsp = vn_mountedvfs(realdvp);
/*
* In the standard case if the looping flag is set and
* performing dotdot we would be returning from a
* covered vnode, implying vfsp could not be null. The
* exceptions being if we have looping and overlay
* mounts or looping and covered file systems.
*/
if (vfsp == NULL) {
/*
* Overlay mount or covered file system,
* so just make the shadow node.
*/
vn_vfsunlock(realdvp);
*vpp = makelonode(vp, li, 0);
(vtol(*vpp))->lo_looping |= LO_LOOPING;
return (0);
}
/*
* When looping get the actual found vnode
* instead of the vnode covered.
* Here we have to hold the lock for realdvp
* since an unmount during the traversal to the
* root vnode would turn *vfsp into garbage
* which would be fatal.
*/
error = VFS_ROOT(vfsp, &tvp);
vn_vfsunlock(realdvp);
if (error)
goto out;
if ((tvp == li->li_rootvp) && (vp == realvp(tvp))) {
/*
* we're back at the real vnode
* of the rootvp
*
* return the rootvp
* Ex: /mnt/mnt/..
* where / has been lofs-mounted
* onto /mnt. Return the lofs
* node mounted at /mnt.
*/
*vpp = tvp;
VN_RELE(vp);
return (0);
} else {
/*
* We are returning from a covered
* node whose vfs_mountedhere is
* not pointing to vfs of the current
* root vnode.
* This is a condn where in we
* returned a covered node say Zc
* but Zc is not the cover of current
* root.
* i.e.., if X is the root vnode
* lookup(Zc,"..") is taking us to
* X.
* Ex: /net/X/net/X/Y
*
* If LO_AUTOLOOP (autofs/lofs looping detected)
* has been set then we are encountering the
* cover of Y (Y being any directory vnode
* under /net/X/net/X/).
* When performing a dotdot set the
* returned vp to the vnode covered
* by the mounted lofs, ie /net/X/net/X
*/
VN_RELE(tvp);
if ((vtol(dvp))->lo_looping & LO_AUTOLOOP) {
VN_RELE(vp);
vp = li->li_rootvp;
vp = vp->v_vfsp->vfs_vnodecovered;
VN_HOLD(vp);
*vpp = makelonode(vp, li, 0);
(vtol(*vpp))->lo_looping |= LO_LOOPING;
return (0);
}
}
} else {
/*
* No frills just make the shadow node.
*/
*vpp = makelonode(vp, li, 0);
return (0);
}
}
nosub = (vtoli(dvp->v_vfsp)->li_flag & LO_NOSUB);
/*
* If this vnode is mounted on, then we
* traverse to the vnode which is the root of
* the mounted file system.
*/
if (!nosub && (error = traverse(&vp)))
goto out;
/*
* Make a lnode for the real vnode.
*/
if (vp->v_type != VDIR || nosub) {
*vpp = makelonode(vp, li, 0);
if (IS_DEVVP(*vpp)) {
vnode_t *svp;
svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
VN_RELE(*vpp);
if (svp == NULL)
error = ENOSYS;
else
*vpp = svp;
}
return (error);
}
/*
* if the found vnode (vp) is not of type lofs
* then we're just going to make a shadow of that
* vp and get out.
*
* If the found vnode (vp) is of lofs type, and
* we're not doing dotdot, check if we are
* looping.
*/
if (!doingdotdot && vfs_matchops(vp->v_vfsp, lo_vfsops)) {
/*
* Check if we're looping, i.e.
* vp equals the root vp of the lofs, directly
* or indirectly, return the covered node.
*/
if (!((vtol(dvp))->lo_looping & LO_LOOPING)) {
if (vp == li->li_rootvp) {
/*
* Direct looping condn.
* Ex:- X is / mounted directory so lookup of
* /X/X is a direct looping condn.
*/
tvp = vp;
vp = vp->v_vfsp->vfs_vnodecovered;
VN_HOLD(vp);
VN_RELE(tvp);
looping++;
} else {
/*
* Indirect looping can be defined as
* real lookup returning rootvp of the current
* tree in any level of recursion.
*
* This check is useful if there are multiple
* levels of lofs indirections. Suppose vnode X
* in the current lookup has as its real vnode
* another lofs node. Y = realvp(X) Y should be
* a lofs node for the check to continue or Y
* is not the rootvp of X.
* Ex:- say X and Y are two vnodes
* say real(Y) is X and real(X) is Z
* parent vnode for X and Y is Z
* lookup(Y,"path") say we are looking for Y
* again under Y and we have to return Yc.
* but the lookup of Y under Y doesnot return
* Y the root vnode again here is why.
* 1. lookup(Y,"path of Y") will go to
* 2. lookup(real(Y),"path of Y") and then to
* 3. lookup(real(X),"path of Y").
* and now what lookup level 1 sees is the
* outcome of 2 but the vnode Y is due to
* lookup(Z,"path of Y") so we have to skip
* intermediate levels to find if in any level
* there is a looping.
*/
is_indirectloop = 0;
nonlovp = vp;
while (
vfs_matchops(nonlovp->v_vfsp, lo_vfsops) &&
!(is_indirectloop)) {
if (li->li_rootvp == nonlovp) {
is_indirectloop++;
break;
}
nonlovp = realvp(nonlovp);
}
if (is_indirectloop) {
VN_RELE(vp);
vp = nonlovp;
vp = vp->v_vfsp->vfs_vnodecovered;
VN_HOLD(vp);
looping++;
}
}
} else {
/*
* come here only because of the interaction between
* the autofs and lofs.
*
* Lookup of "/net/X/net/X" will return a shadow of
* an autonode X_a which we call X_l.
*
* Lookup of anything under X_l, will trigger a call to
* auto_lookup(X_a,nm) which will eventually call
* lo_lookup(X_lr,nm) where X_lr is the root vnode of
* the current lofs.
*
* We come here only when we are called with X_l as dvp
* and look for something underneath.
*
* Now that an autofs/lofs looping condition has been
* identified any directory vnode contained within
* dvp will be set to the vnode covered by the
* mounted autofs. Thus all directories within dvp
* will appear empty hence teminating the looping.
* The LO_AUTOLOOP flag is set on the returned lonode
* to indicate the termination of the autofs/lofs
* looping. This is required for the correct behaviour
* when performing a dotdot.
*/
realdvp = realvp(dvp);
while (vfs_matchops(realdvp->v_vfsp, lo_vfsops)) {
realdvp = realvp(realdvp);
}
error = VFS_ROOT(realdvp->v_vfsp, &tvp);
if (error)
goto out;
/*
* tvp now contains the rootvp of the vfs of the
* real vnode of dvp. The directory vnode vp is set
* to the covered vnode to terminate looping. No
* distinction is made between any vp as all directory
* vnodes contained in dvp are returned as the covered
* vnode.
*/
VN_RELE(vp);
vp = tvp; /* possibly is an autonode */
/*
* Need to find the covered vnode
*/
if (vp->v_vfsp->vfs_vnodecovered == NULL) {
/*
* We don't have a covered vnode so this isn't
* an autonode. To find the autonode simply
* find the vnode covered by the lofs rootvp.
*/
vp = li->li_rootvp;
vp = vp->v_vfsp->vfs_vnodecovered;
VN_RELE(tvp);
error = VFS_ROOT(vp->v_vfsp, &tvp);
if (error)
goto out;
vp = tvp; /* now this is an autonode */
if (vp->v_vfsp->vfs_vnodecovered == NULL) {
/*
* Still can't find a covered vnode.
* Fail the lookup, or we'd loop.
*/
error = ENOENT;
goto out;
}
}
vp = vp->v_vfsp->vfs_vnodecovered;
VN_HOLD(vp);
VN_RELE(tvp);
/*
* Force the creation of a new lnode even if the hash
* table contains a lnode that references this vnode.
*/
mkflag = LOF_FORCE;
autoloop++;
}
}
*vpp = makelonode(vp, li, mkflag);
if ((looping) ||
(((vtol(dvp))->lo_looping & LO_LOOPING) && !doingdotdot)) {
(vtol(*vpp))->lo_looping |= LO_LOOPING;
}
if (autoloop) {
(vtol(*vpp))->lo_looping |= LO_AUTOLOOP;
}
out:
if (error != 0 && vp != NULL)
VN_RELE(vp);
#ifdef LODEBUG
lo_dprint(4,
"lo_lookup dvp %x realdvp %x nm '%s' newvp %x real vp %x error %d\n",
dvp, realvp(dvp), nm, *vpp, vp, error);
#endif
return (error);
}
/*ARGSUSED*/
static int
lo_create(
vnode_t *dvp,
char *nm,
struct vattr *va,
enum vcexcl exclusive,
int mode,
vnode_t **vpp,
struct cred *cr,
int flag,
caller_context_t *ct,
vsecattr_t *vsecp)
{
int error;
vnode_t *vp = NULL;
#ifdef LODEBUG
lo_dprint(4, "lo_create vp %p realvp %p\n", dvp, realvp(dvp));
#endif
if (*nm == '\0') {
ASSERT(vpp && dvp == *vpp);
vp = realvp(*vpp);
}
error = VOP_CREATE(realvp(dvp), nm, va, exclusive, mode, &vp, cr, flag,
ct, vsecp);
if (!error) {
*vpp = makelonode(vp, vtoli(dvp->v_vfsp), 0);
if (IS_DEVVP(*vpp)) {
vnode_t *svp;
svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
VN_RELE(*vpp);
if (svp == NULL)
error = ENOSYS;
else
*vpp = svp;
}
} else if (error == ENOSYS && exclusive == NONEXCL &&
dvp == vtoli(dvp->v_vfsp)->li_rootvp &&
realvp(dvp)->v_type == VREG) {
/*
* We have a single regular file lofs mounted, thus the file is
* the root vnode (the directory vp is the file vp). Some
* underlying file systems (e.g. tmpfs or ufs) properly handle
* this style of create but at least zfs won't support create
* this way (see zfs_fvnodeops_template which has fs_nosys for
* the vop_create entry because zfs_create doesn't work
* properly for this case).
*/
if ((error = VOP_ACCESS(dvp, mode, 0, cr, NULL)) == 0) {
/*
* Since we already know the vnode for the existing
* file we can handle create as a no-op, as expected,
* truncating the file if necessary.
*/
struct vattr vattr;
vattr.va_size = 0;
vattr.va_mask = AT_SIZE;
if ((va->va_mask & AT_SIZE) != 0 && va->va_size == 0 &&
VOP_SETATTR(dvp, &vattr, 0, CRED(), NULL) != 0)
return (error);
/*
* vn_createat will do a vn_rele on the file if it is
* pre-existing, which it is in the case of a single
* file mounted as the root. Thus, when we eventually
* close the file the count will already be 1 so the
* vnode would be freed. To prevent that, we add an
* extra hold here.
*/
VN_HOLD(dvp);
*vpp = dvp;
error = 0;
}
}
return (error);
}
static int
lo_remove(
vnode_t *dvp,
char *nm,
struct cred *cr,
caller_context_t *ct,
int flags)
{
#ifdef LODEBUG
lo_dprint(4, "lo_remove vp %p realvp %p\n", dvp, realvp(dvp));
#endif
dvp = realvp(dvp);
return (VOP_REMOVE(dvp, nm, cr, ct, flags));
}
static int
lo_link(
vnode_t *tdvp,
vnode_t *vp,
char *tnm,
struct cred *cr,
caller_context_t *ct,
int flags)
{
vnode_t *realvp;
#ifdef LODEBUG
lo_dprint(4, "lo_link vp %p realvp %p\n", vp, realvp(vp));
#endif
/*
* The source and destination vnodes may be in different lofs
* filesystems sharing the same underlying filesystem, so we need to
* make sure that the filesystem containing the source vnode is not
* mounted read-only (vn_link() has already checked the target vnode).
*
* In a situation such as:
*
* /data - regular filesystem
* /foo - lofs mount of /data/foo
* /bar - read-only lofs mount of /data/bar
*
* This disallows a link from /bar/somefile to /foo/somefile,
* which would otherwise allow changes to somefile on the read-only
* mounted /bar.
*/
if (vn_is_readonly(vp)) {
return (EROFS);
}
while (vn_matchops(vp, lo_vnodeops)) {
vp = realvp(vp);
}
/*
* In the case where the source vnode is on another stacking
* filesystem (such as specfs), the loop above will
* terminate before finding the true underlying vnode.
*
* We use VOP_REALVP here to continue the search.
*/
if (VOP_REALVP(vp, &realvp, ct) == 0)
vp = realvp;
while (vn_matchops(tdvp, lo_vnodeops)) {
tdvp = realvp(tdvp);
}
if (vp->v_vfsp != tdvp->v_vfsp)
return (EXDEV);
return (VOP_LINK(tdvp, vp, tnm, cr, ct, flags));
}
static int
lo_rename(
vnode_t *odvp,
char *onm,
vnode_t *ndvp,
char *nnm,
struct cred *cr,
caller_context_t *ct,
int flags)
{
vnode_t *tnvp;
#ifdef LODEBUG
lo_dprint(4, "lo_rename vp %p realvp %p\n", odvp, realvp(odvp));
#endif
/*
* If we are coming from a loop back mounted fs, that has been
* mounted in the same filesystem as where we want to move to,
* and that filesystem is read/write, but the lofs filesystem is
* read only, we don't want to allow a rename of the file. The
* vn_rename code checks to be sure the target is read/write already
* so that is not necessary here. However, consider the following
* example:
* / - regular root fs
* /foo - directory in root
* /foo/bar - file in foo directory(in root fs)
* /baz - directory in root
* mount -F lofs -o ro /foo /baz - all still in root
* directory
* The fact that we mounted /foo on /baz read only should stop us
* from renaming the file /foo/bar /bar, but it doesn't since
* / is read/write. We are still renaming here since we are still
* in the same filesystem, it is just that we do not check to see
* if the filesystem we are coming from in this case is read only.
*/
if (odvp->v_vfsp->vfs_flag & VFS_RDONLY)
return (EROFS);
/*
* We need to make sure we're not trying to remove a mount point for a
* filesystem mounted on top of lofs, which only we know about.
*/
if (vn_matchops(ndvp, lo_vnodeops)) /* Not our problem. */
goto rename;
/*
* XXXci - Once case-insensitive behavior is implemented, it should
* be added here.
*/
if (VOP_LOOKUP(ndvp, nnm, &tnvp, NULL, 0, NULL, cr,
ct, NULL, NULL) != 0)
goto rename;
if (tnvp->v_type != VDIR) {
VN_RELE(tnvp);
goto rename;
}
if (vn_mountedvfs(tnvp)) {
VN_RELE(tnvp);
return (EBUSY);
}
VN_RELE(tnvp);
rename:
/*
* Since the case we're dealing with above can happen at any layer in
* the stack of lofs filesystems, we need to recurse down the stack,
* checking to see if there are any instances of a filesystem mounted on
* top of lofs. In order to keep on using the lofs version of
* VOP_RENAME(), we make sure that while the target directory is of type
* lofs, the source directory (the one used for getting the fs-specific
* version of VOP_RENAME()) is also of type lofs.
*/
if (vn_matchops(ndvp, lo_vnodeops)) {
ndvp = realvp(ndvp); /* Check the next layer */
} else {
/*
* We can go fast here
*/
while (vn_matchops(odvp, lo_vnodeops)) {
odvp = realvp(odvp);
}
if (odvp->v_vfsp != ndvp->v_vfsp)
return (EXDEV);
}
return (VOP_RENAME(odvp, onm, ndvp, nnm, cr, ct, flags));
}
static int
lo_mkdir(
vnode_t *dvp,
char *nm,
struct vattr *va,
vnode_t **vpp,
struct cred *cr,
caller_context_t *ct,
int flags,
vsecattr_t *vsecp)
{
int error;
#ifdef LODEBUG
lo_dprint(4, "lo_mkdir vp %p realvp %p\n", dvp, realvp(dvp));
#endif
error = VOP_MKDIR(realvp(dvp), nm, va, vpp, cr, ct, flags, vsecp);
if (!error)
*vpp = makelonode(*vpp, vtoli(dvp->v_vfsp), 0);
return (error);
}
static int
lo_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
{
#ifdef LODEBUG
lo_dprint(4, "lo_realvp %p\n", vp);
#endif
while (vn_matchops(vp, lo_vnodeops))
vp = realvp(vp);
if (VOP_REALVP(vp, vpp, ct) != 0)
*vpp = vp;
return (0);
}
static int
lo_rmdir(
vnode_t *dvp,
char *nm,
vnode_t *cdir,
struct cred *cr,
caller_context_t *ct,
int flags)
{
vnode_t *rvp = cdir;
#ifdef LODEBUG
lo_dprint(4, "lo_rmdir vp %p realvp %p\n", dvp, realvp(dvp));
#endif
/* if cdir is lofs vnode ptr get its real vnode ptr */
if (vn_matchops(dvp, vn_getops(rvp)))
(void) lo_realvp(cdir, &rvp, ct);
dvp = realvp(dvp);
return (VOP_RMDIR(dvp, nm, rvp, cr, ct, flags));
}
static int
lo_symlink(
vnode_t *dvp,
char *lnm,
struct vattr *tva,
char *tnm,
struct cred *cr,
caller_context_t *ct,
int flags)
{
#ifdef LODEBUG
lo_dprint(4, "lo_symlink vp %p realvp %p\n", dvp, realvp(dvp));
#endif
dvp = realvp(dvp);
return (VOP_SYMLINK(dvp, lnm, tva, tnm, cr, ct, flags));
}
static int
lo_readlink(
vnode_t *vp,
struct uio *uiop,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_READLINK(vp, uiop, cr, ct));
}
static int
lo_readdir(
vnode_t *vp,
struct uio *uiop,
struct cred *cr,
int *eofp,
caller_context_t *ct,
int flags)
{
#ifdef LODEBUG
lo_dprint(4, "lo_readdir vp %p realvp %p\n", vp, realvp(vp));
#endif
vp = realvp(vp);
return (VOP_READDIR(vp, uiop, cr, eofp, ct, flags));
}
static int
lo_rwlock(vnode_t *vp, int write_lock, caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_RWLOCK(vp, write_lock, ct));
}
static void
lo_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ct)
{
vp = realvp(vp);
VOP_RWUNLOCK(vp, write_lock, ct);
}
static int
lo_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_SEEK(vp, ooff, noffp, ct));
}
static int
lo_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
{
while (vn_matchops(vp1, lo_vnodeops))
vp1 = realvp(vp1);
while (vn_matchops(vp2, lo_vnodeops))
vp2 = realvp(vp2);
return (VOP_CMP(vp1, vp2, ct));
}
static int
lo_frlock(
vnode_t *vp,
int cmd,
struct flock64 *bfp,
int flag,
offset_t offset,
struct flk_callback *flk_cbp,
cred_t *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_FRLOCK(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
}
static int
lo_space(
vnode_t *vp,
int cmd,
struct flock64 *bfp,
int flag,
offset_t offset,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_SPACE(vp, cmd, bfp, flag, offset, cr, ct));
}
static int
lo_getpage(
vnode_t *vp,
offset_t off,
size_t len,
uint_t *prot,
struct page *parr[],
size_t psz,
struct seg *seg,
caddr_t addr,
enum seg_rw rw,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_GETPAGE(vp, off, len, prot, parr, psz, seg, addr, rw, cr,
ct));
}
static int
lo_putpage(
vnode_t *vp,
offset_t off,
size_t len,
int flags,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_PUTPAGE(vp, off, len, flags, cr, ct));
}
static int
lo_map(
vnode_t *vp,
offset_t off,
struct as *as,
caddr_t *addrp,
size_t len,
uchar_t prot,
uchar_t maxprot,
uint_t flags,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_MAP(vp, off, as, addrp, len, prot, maxprot, flags, cr, ct));
}
static int
lo_addmap(
vnode_t *vp,
offset_t off,
struct as *as,
caddr_t addr,
size_t len,
uchar_t prot,
uchar_t maxprot,
uint_t flags,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_ADDMAP(vp, off, as, addr, len, prot, maxprot, flags, cr,
ct));
}
static int
lo_delmap(
vnode_t *vp,
offset_t off,
struct as *as,
caddr_t addr,
size_t len,
uint_t prot,
uint_t maxprot,
uint_t flags,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_DELMAP(vp, off, as, addr, len, prot, maxprot, flags, cr,
ct));
}
static int
lo_poll(
vnode_t *vp,
short events,
int anyyet,
short *reventsp,
struct pollhead **phpp,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_POLL(vp, events, anyyet, reventsp, phpp, ct));
}
static int
lo_dump(vnode_t *vp, caddr_t addr, offset_t bn, offset_t count,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_DUMP(vp, addr, bn, count, ct));
}
static int
lo_pathconf(
vnode_t *vp,
int cmd,
ulong_t *valp,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_PATHCONF(vp, cmd, valp, cr, ct));
}
static int
lo_pageio(
vnode_t *vp,
struct page *pp,
u_offset_t io_off,
size_t io_len,
int flags,
cred_t *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_PAGEIO(vp, pp, io_off, io_len, flags, cr, ct));
}
static void
lo_dispose(
vnode_t *vp,
page_t *pp,
int fl,
int dn,
cred_t *cr,
caller_context_t *ct)
{
vp = realvp(vp);
if (vp != NULL && !VN_ISKAS(vp))
VOP_DISPOSE(vp, pp, fl, dn, cr, ct);
}
static int
lo_setsecattr(
vnode_t *vp,
vsecattr_t *secattr,
int flags,
struct cred *cr,
caller_context_t *ct)
{
if (vn_is_readonly(vp))
return (EROFS);
vp = realvp(vp);
return (VOP_SETSECATTR(vp, secattr, flags, cr, ct));
}
static int
lo_getsecattr(
vnode_t *vp,
vsecattr_t *secattr,
int flags,
struct cred *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_GETSECATTR(vp, secattr, flags, cr, ct));
}
static int
lo_shrlock(
vnode_t *vp,
int cmd,
struct shrlock *shr,
int flag,
cred_t *cr,
caller_context_t *ct)
{
vp = realvp(vp);
return (VOP_SHRLOCK(vp, cmd, shr, flag, cr, ct));
}
/*
* Loopback vnode operations vector.
*/
struct vnodeops *lo_vnodeops;
const fs_operation_def_t lo_vnodeops_template[] = {
VOPNAME_OPEN, { .vop_open = lo_open },
VOPNAME_CLOSE, { .vop_close = lo_close },
VOPNAME_READ, { .vop_read = lo_read },
VOPNAME_WRITE, { .vop_write = lo_write },
VOPNAME_IOCTL, { .vop_ioctl = lo_ioctl },
VOPNAME_SETFL, { .vop_setfl = lo_setfl },
VOPNAME_GETATTR, { .vop_getattr = lo_getattr },
VOPNAME_SETATTR, { .vop_setattr = lo_setattr },
VOPNAME_ACCESS, { .vop_access = lo_access },
VOPNAME_LOOKUP, { .vop_lookup = lo_lookup },
VOPNAME_CREATE, { .vop_create = lo_create },
VOPNAME_REMOVE, { .vop_remove = lo_remove },
VOPNAME_LINK, { .vop_link = lo_link },
VOPNAME_RENAME, { .vop_rename = lo_rename },
VOPNAME_MKDIR, { .vop_mkdir = lo_mkdir },
VOPNAME_RMDIR, { .vop_rmdir = lo_rmdir },
VOPNAME_READDIR, { .vop_readdir = lo_readdir },
VOPNAME_SYMLINK, { .vop_symlink = lo_symlink },
VOPNAME_READLINK, { .vop_readlink = lo_readlink },
VOPNAME_FSYNC, { .vop_fsync = lo_fsync },
VOPNAME_INACTIVE, { .vop_inactive = lo_inactive },
VOPNAME_FID, { .vop_fid = lo_fid },
VOPNAME_RWLOCK, { .vop_rwlock = lo_rwlock },
VOPNAME_RWUNLOCK, { .vop_rwunlock = lo_rwunlock },
VOPNAME_SEEK, { .vop_seek = lo_seek },
VOPNAME_CMP, { .vop_cmp = lo_cmp },
VOPNAME_FRLOCK, { .vop_frlock = lo_frlock },
VOPNAME_SPACE, { .vop_space = lo_space },
VOPNAME_REALVP, { .vop_realvp = lo_realvp },
VOPNAME_GETPAGE, { .vop_getpage = lo_getpage },
VOPNAME_PUTPAGE, { .vop_putpage = lo_putpage },
VOPNAME_MAP, { .vop_map = lo_map },
VOPNAME_ADDMAP, { .vop_addmap = lo_addmap },
VOPNAME_DELMAP, { .vop_delmap = lo_delmap },
VOPNAME_POLL, { .vop_poll = lo_poll },
VOPNAME_DUMP, { .vop_dump = lo_dump },
VOPNAME_DUMPCTL, { .error = fs_error }, /* XXX - why? */
VOPNAME_PATHCONF, { .vop_pathconf = lo_pathconf },
VOPNAME_PAGEIO, { .vop_pageio = lo_pageio },
VOPNAME_DISPOSE, { .vop_dispose = lo_dispose },
VOPNAME_SETSECATTR, { .vop_setsecattr = lo_setsecattr },
VOPNAME_GETSECATTR, { .vop_getsecattr = lo_getsecattr },
VOPNAME_SHRLOCK, { .vop_shrlock = lo_shrlock },
NULL, NULL
};
|