aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/vfs_mount.c
blob: 73cc34da79ea410db48826be8c95af923c7c6eaf (plain) (blame)
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
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 1999-2004 Poul-Henning Kamp
 * Copyright (c) 1999 Michael Smith
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 * (c) UNIX System Laboratories, Inc.
 * All or some portions of this file are derived from material licensed
 * to the University of California by American Telephone and Telegraph
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 * the permission of UNIX System Laboratories, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/smp.h>
#include <sys/devctl.h>
#include <sys/eventhandler.h>
#include <sys/fcntl.h>
#include <sys/jail.h>
#include <sys/kernel.h>
#include <sys/ktr.h>
#include <sys/libkern.h>
#include <sys/limits.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/filedesc.h>
#include <sys/reboot.h>
#include <sys/sbuf.h>
#include <sys/syscallsubr.h>
#include <sys/sysproto.h>
#include <sys/sx.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/taskqueue.h>
#include <sys/vnode.h>
#include <vm/uma.h>

#include <geom/geom.h>

#include <machine/stdarg.h>

#include <security/audit/audit.h>
#include <security/mac/mac_framework.h>

#define	VFS_MOUNTARG_SIZE_MAX	(1024 * 64)

static int	vfs_domount(struct thread *td, const char *fstype, char *fspath,
		    uint64_t fsflags, struct vfsoptlist **optlist);
static void	free_mntarg(struct mntarg *ma);

static int	usermount = 0;
SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
    "Unprivileged users may mount and unmount file systems");

static bool	default_autoro = false;
SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0,
    "Retry failed r/w mount as r/o if no explicit ro/rw option is specified");

static bool	recursive_forced_unmount = false;
SYSCTL_BOOL(_vfs, OID_AUTO, recursive_forced_unmount, CTLFLAG_RW,
    &recursive_forced_unmount, 0, "Recursively unmount stacked upper mounts"
    " when a file system is forcibly unmounted");

static SYSCTL_NODE(_vfs, OID_AUTO, deferred_unmount,
    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "deferred unmount controls");

static unsigned int	deferred_unmount_retry_limit = 10;
SYSCTL_UINT(_vfs_deferred_unmount, OID_AUTO, retry_limit, CTLFLAG_RW,
    &deferred_unmount_retry_limit, 0,
    "Maximum number of retries for deferred unmount failure");

static int	deferred_unmount_retry_delay_hz;
SYSCTL_INT(_vfs_deferred_unmount, OID_AUTO, retry_delay_hz, CTLFLAG_RW,
    &deferred_unmount_retry_delay_hz, 0,
    "Delay in units of [1/kern.hz]s when retrying a failed deferred unmount");

static int	deferred_unmount_total_retries = 0;
SYSCTL_INT(_vfs_deferred_unmount, OID_AUTO, total_retries, CTLFLAG_RD,
    &deferred_unmount_total_retries, 0,
    "Total number of retried deferred unmounts");

MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
static uma_zone_t mount_zone;

/* List of mounted filesystems. */
struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);

/* For any iteration/modification of mountlist */
struct mtx_padalign __exclusive_cache_line mountlist_mtx;

EVENTHANDLER_LIST_DEFINE(vfs_mounted);
EVENTHANDLER_LIST_DEFINE(vfs_unmounted);

static void vfs_deferred_unmount(void *arg, int pending);
static struct timeout_task deferred_unmount_task;
static struct mtx deferred_unmount_lock;
MTX_SYSINIT(deferred_unmount, &deferred_unmount_lock, "deferred_unmount",
    MTX_DEF);
static STAILQ_HEAD(, mount) deferred_unmount_list =
    STAILQ_HEAD_INITIALIZER(deferred_unmount_list);
TASKQUEUE_DEFINE_THREAD(deferred_unmount);

static void mount_devctl_event(const char *type, struct mount *mp, bool donew);

/*
 * Global opts, taken by all filesystems
 */
static const char *global_opts[] = {
	"errmsg",
	"fstype",
	"fspath",
	"ro",
	"rw",
	"nosuid",
	"noexec",
	NULL
};

static int
mount_init(void *mem, int size, int flags)
{
	struct mount *mp;

	mp = (struct mount *)mem;
	mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
	mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF);
	lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
	mp->mnt_pcpu = uma_zalloc_pcpu(pcpu_zone_16, M_WAITOK | M_ZERO);
	mp->mnt_ref = 0;
	mp->mnt_vfs_ops = 1;
	mp->mnt_rootvnode = NULL;
	return (0);
}

static void
mount_fini(void *mem, int size)
{
	struct mount *mp;

	mp = (struct mount *)mem;
	uma_zfree_pcpu(pcpu_zone_16, mp->mnt_pcpu);
	lockdestroy(&mp->mnt_explock);
	mtx_destroy(&mp->mnt_listmtx);
	mtx_destroy(&mp->mnt_mtx);
}

static void
vfs_mount_init(void *dummy __unused)
{
	TIMEOUT_TASK_INIT(taskqueue_deferred_unmount, &deferred_unmount_task,
	    0, vfs_deferred_unmount, NULL);
	deferred_unmount_retry_delay_hz = hz;
	mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
	    NULL, mount_init, mount_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
	mtx_init(&mountlist_mtx, "mountlist", NULL, MTX_DEF);
}
SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);

/*
 * ---------------------------------------------------------------------
 * Functions for building and sanitizing the mount options
 */

/* Remove one mount option. */
static void
vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
{

	TAILQ_REMOVE(opts, opt, link);
	free(opt->name, M_MOUNT);
	if (opt->value != NULL)
		free(opt->value, M_MOUNT);
	free(opt, M_MOUNT);
}

/* Release all resources related to the mount options. */
void
vfs_freeopts(struct vfsoptlist *opts)
{
	struct vfsopt *opt;

	while (!TAILQ_EMPTY(opts)) {
		opt = TAILQ_FIRST(opts);
		vfs_freeopt(opts, opt);
	}
	free(opts, M_MOUNT);
}

void
vfs_deleteopt(struct vfsoptlist *opts, const char *name)
{
	struct vfsopt *opt, *temp;

	if (opts == NULL)
		return;
	TAILQ_FOREACH_SAFE(opt, opts, link, temp)  {
		if (strcmp(opt->name, name) == 0)
			vfs_freeopt(opts, opt);
	}
}

static int
vfs_isopt_ro(const char *opt)
{

	if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
	    strcmp(opt, "norw") == 0)
		return (1);
	return (0);
}

static int
vfs_isopt_rw(const char *opt)
{

	if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
		return (1);
	return (0);
}

/*
 * Check if options are equal (with or without the "no" prefix).
 */
static int
vfs_equalopts(const char *opt1, const char *opt2)
{
	char *p;

	/* "opt" vs. "opt" or "noopt" vs. "noopt" */
	if (strcmp(opt1, opt2) == 0)
		return (1);
	/* "noopt" vs. "opt" */
	if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
		return (1);
	/* "opt" vs. "noopt" */
	if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
		return (1);
	while ((p = strchr(opt1, '.')) != NULL &&
	    !strncmp(opt1, opt2, ++p - opt1)) {
		opt2 += p - opt1;
		opt1 = p;
		/* "foo.noopt" vs. "foo.opt" */
		if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
			return (1);
		/* "foo.opt" vs. "foo.noopt" */
		if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
			return (1);
	}
	/* "ro" / "rdonly" / "norw" / "rw" / "noro" */
	if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
	    (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
		return (1);
	return (0);
}

/*
 * If a mount option is specified several times,
 * (with or without the "no" prefix) only keep
 * the last occurrence of it.
 */
static void
vfs_sanitizeopts(struct vfsoptlist *opts)
{
	struct vfsopt *opt, *opt2, *tmp;

	TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
		opt2 = TAILQ_PREV(opt, vfsoptlist, link);
		while (opt2 != NULL) {
			if (vfs_equalopts(opt->name, opt2->name)) {
				tmp = TAILQ_PREV(opt2, vfsoptlist, link);
				vfs_freeopt(opts, opt2);
				opt2 = tmp;
			} else {
				opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
			}
		}
	}
}

/*
 * Build a linked list of mount options from a struct uio.
 */
int
vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
{
	struct vfsoptlist *opts;
	struct vfsopt *opt;
	size_t memused, namelen, optlen;
	unsigned int i, iovcnt;
	int error;

	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
	TAILQ_INIT(opts);
	memused = 0;
	iovcnt = auio->uio_iovcnt;
	for (i = 0; i < iovcnt; i += 2) {
		namelen = auio->uio_iov[i].iov_len;
		optlen = auio->uio_iov[i + 1].iov_len;
		memused += sizeof(struct vfsopt) + optlen + namelen;
		/*
		 * Avoid consuming too much memory, and attempts to overflow
		 * memused.
		 */
		if (memused > VFS_MOUNTARG_SIZE_MAX ||
		    optlen > VFS_MOUNTARG_SIZE_MAX ||
		    namelen > VFS_MOUNTARG_SIZE_MAX) {
			error = EINVAL;
			goto bad;
		}

		opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
		opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
		opt->value = NULL;
		opt->len = 0;
		opt->pos = i / 2;
		opt->seen = 0;

		/*
		 * Do this early, so jumps to "bad" will free the current
		 * option.
		 */
		TAILQ_INSERT_TAIL(opts, opt, link);

		if (auio->uio_segflg == UIO_SYSSPACE) {
			bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
		} else {
			error = copyin(auio->uio_iov[i].iov_base, opt->name,
			    namelen);
			if (error)
				goto bad;
		}
		/* Ensure names are null-terminated strings. */
		if (namelen == 0 || opt->name[namelen - 1] != '\0') {
			error = EINVAL;
			goto bad;
		}
		if (optlen != 0) {
			opt->len = optlen;
			opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
			if (auio->uio_segflg == UIO_SYSSPACE) {
				bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
				    optlen);
			} else {
				error = copyin(auio->uio_iov[i + 1].iov_base,
				    opt->value, optlen);
				if (error)
					goto bad;
			}
		}
	}
	vfs_sanitizeopts(opts);
	*options = opts;
	return (0);
bad:
	vfs_freeopts(opts);
	return (error);
}

/*
 * Merge the old mount options with the new ones passed
 * in the MNT_UPDATE case.
 *
 * XXX: This function will keep a "nofoo" option in the new
 * options.  E.g, if the option's canonical name is "foo",
 * "nofoo" ends up in the mount point's active options.
 */
static void
vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
{
	struct vfsopt *opt, *new;

	TAILQ_FOREACH(opt, oldopts, link) {
		new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
		new->name = strdup(opt->name, M_MOUNT);
		if (opt->len != 0) {
			new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
			bcopy(opt->value, new->value, opt->len);
		} else
			new->value = NULL;
		new->len = opt->len;
		new->seen = opt->seen;
		TAILQ_INSERT_HEAD(toopts, new, link);
	}
	vfs_sanitizeopts(toopts);
}

/*
 * Mount a filesystem.
 */
#ifndef _SYS_SYSPROTO_H_
struct nmount_args {
	struct iovec *iovp;
	unsigned int iovcnt;
	int flags;
};
#endif
int
sys_nmount(struct thread *td, struct nmount_args *uap)
{
	struct uio *auio;
	int error;
	u_int iovcnt;
	uint64_t flags;

	/*
	 * Mount flags are now 64-bits. On 32-bit archtectures only
	 * 32-bits are passed in, but from here on everything handles
	 * 64-bit flags correctly.
	 */
	flags = uap->flags;

	AUDIT_ARG_FFLAGS(flags);
	CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
	    uap->iovp, uap->iovcnt, flags);

	/*
	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
	 * userspace to set this flag, but we must filter it out if we want
	 * MNT_UPDATE on the root file system to work.
	 * MNT_ROOTFS should only be set by the kernel when mounting its
	 * root file system.
	 */
	flags &= ~MNT_ROOTFS;

	iovcnt = uap->iovcnt;
	/*
	 * Check that we have an even number of iovec's
	 * and that we have at least two options.
	 */
	if ((iovcnt & 1) || (iovcnt < 4)) {
		CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
		    uap->iovcnt);
		return (EINVAL);
	}

	error = copyinuio(uap->iovp, iovcnt, &auio);
	if (error) {
		CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
		    __func__, error);
		return (error);
	}
	error = vfs_donmount(td, flags, auio);

	free(auio, M_IOV);
	return (error);
}

/*
 * ---------------------------------------------------------------------
 * Various utility functions
 */

/*
 * Get a reference on a mount point from a vnode.
 *
 * The vnode is allowed to be passed unlocked and race against dooming. Note in
 * such case there are no guarantees the referenced mount point will still be
 * associated with it after the function returns.
 */
struct mount *
vfs_ref_from_vp(struct vnode *vp)
{
	struct mount *mp;
	struct mount_pcpu *mpcpu;

	mp = atomic_load_ptr(&vp->v_mount);
	if (__predict_false(mp == NULL)) {
		return (mp);
	}
	if (vfs_op_thread_enter(mp, mpcpu)) {
		if (__predict_true(mp == vp->v_mount)) {
			vfs_mp_count_add_pcpu(mpcpu, ref, 1);
			vfs_op_thread_exit(mp, mpcpu);
		} else {
			vfs_op_thread_exit(mp, mpcpu);
			mp = NULL;
		}
	} else {
		MNT_ILOCK(mp);
		if (mp == vp->v_mount) {
			MNT_REF(mp);
			MNT_IUNLOCK(mp);
		} else {
			MNT_IUNLOCK(mp);
			mp = NULL;
		}
	}
	return (mp);
}

void
vfs_ref(struct mount *mp)
{
	struct mount_pcpu *mpcpu;

	CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
	if (vfs_op_thread_enter(mp, mpcpu)) {
		vfs_mp_count_add_pcpu(mpcpu, ref, 1);
		vfs_op_thread_exit(mp, mpcpu);
		return;
	}

	MNT_ILOCK(mp);
	MNT_REF(mp);
	MNT_IUNLOCK(mp);
}

/*
 * Register ump as an upper mount of the mount associated with
 * vnode vp.  This registration will be tracked through
 * mount_upper_node upper, which should be allocated by the
 * caller and stored in per-mount data associated with mp.
 *
 * If successful, this function will return the mount associated
 * with vp, and will ensure that it cannot be unmounted until
 * ump has been unregistered as one of its upper mounts.
 * 
 * Upon failure this function will return NULL.
 */
struct mount *
vfs_register_upper_from_vp(struct vnode *vp, struct mount *ump,
    struct mount_upper_node *upper)
{
	struct mount *mp;

	mp = atomic_load_ptr(&vp->v_mount);
	if (mp == NULL)
		return (NULL);
	MNT_ILOCK(mp);
	if (mp != vp->v_mount ||
	    ((mp->mnt_kern_flag & (MNTK_UNMOUNT | MNTK_RECURSE)) != 0)) {
		MNT_IUNLOCK(mp);
		return (NULL);
	}
	KASSERT(ump != mp, ("upper and lower mounts are identical"));
	upper->mp = ump;
	MNT_REF(mp);
	TAILQ_INSERT_TAIL(&mp->mnt_uppers, upper, mnt_upper_link);
	MNT_IUNLOCK(mp);
	return (mp);
}

/*
 * Register upper mount ump to receive vnode unlink/reclaim
 * notifications from lower mount mp. This registration will
 * be tracked through mount_upper_node upper, which should be
 * allocated by the caller and stored in per-mount data
 * associated with mp.
 *
 * ump must already be registered as an upper mount of mp
 * through a call to vfs_register_upper_from_vp().
 */
void
vfs_register_for_notification(struct mount *mp, struct mount *ump,
    struct mount_upper_node *upper)
{
	upper->mp = ump;
	MNT_ILOCK(mp);
	TAILQ_INSERT_TAIL(&mp->mnt_notify, upper, mnt_upper_link);
	MNT_IUNLOCK(mp);
}

static void
vfs_drain_upper_locked(struct mount *mp)
{
	mtx_assert(MNT_MTX(mp), MA_OWNED);
	while (mp->mnt_upper_pending != 0) {
		mp->mnt_kern_flag |= MNTK_UPPER_WAITER;
		msleep(&mp->mnt_uppers, MNT_MTX(mp), 0, "mntupw", 0);
	}
}

/*
 * Undo a previous call to vfs_register_for_notification().
 * The mount represented by upper must be currently registered
 * as an upper mount for mp.
 */
void
vfs_unregister_for_notification(struct mount *mp,
    struct mount_upper_node *upper)
{
	MNT_ILOCK(mp);
	vfs_drain_upper_locked(mp);
	TAILQ_REMOVE(&mp->mnt_notify, upper, mnt_upper_link);
	MNT_IUNLOCK(mp);
}

/*
 * Undo a previous call to vfs_register_upper_from_vp().
 * This must be done before mp can be unmounted.
 */
void
vfs_unregister_upper(struct mount *mp, struct mount_upper_node *upper)
{
	MNT_ILOCK(mp);
	KASSERT((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0,
	    ("registered upper with pending unmount"));
	vfs_drain_upper_locked(mp);
	TAILQ_REMOVE(&mp->mnt_uppers, upper, mnt_upper_link);
	if ((mp->mnt_kern_flag & MNTK_TASKQUEUE_WAITER) != 0 &&
	    TAILQ_EMPTY(&mp->mnt_uppers)) {
		mp->mnt_kern_flag &= ~MNTK_TASKQUEUE_WAITER;
		wakeup(&mp->mnt_taskqueue_link);
	}
	MNT_REL(mp);
	MNT_IUNLOCK(mp);
}

void
vfs_rel(struct mount *mp)
{
	struct mount_pcpu *mpcpu;

	CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
	if (vfs_op_thread_enter(mp, mpcpu)) {
		vfs_mp_count_sub_pcpu(mpcpu, ref, 1);
		vfs_op_thread_exit(mp, mpcpu);
		return;
	}

	MNT_ILOCK(mp);
	MNT_REL(mp);
	MNT_IUNLOCK(mp);
}

/*
 * Allocate and initialize the mount point struct.
 */
struct mount *
vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
    struct ucred *cred)
{
	struct mount *mp;

	mp = uma_zalloc(mount_zone, M_WAITOK);
	bzero(&mp->mnt_startzero,
	    __rangeof(struct mount, mnt_startzero, mnt_endzero));
	mp->mnt_kern_flag = 0;
	mp->mnt_flag = 0;
	mp->mnt_rootvnode = NULL;
	mp->mnt_vnodecovered = NULL;
	mp->mnt_op = NULL;
	mp->mnt_vfc = NULL;
	TAILQ_INIT(&mp->mnt_nvnodelist);
	mp->mnt_nvnodelistsize = 0;
	TAILQ_INIT(&mp->mnt_lazyvnodelist);
	mp->mnt_lazyvnodelistsize = 0;
	if (mp->mnt_ref != 0 || mp->mnt_lockref != 0 ||
	    mp->mnt_writeopcount != 0)
		panic("%s: non-zero counters on new mp %p\n", __func__, mp);
	if (mp->mnt_vfs_ops != 1)
		panic("%s: vfs_ops should be 1 but %d found\n", __func__,
		    mp->mnt_vfs_ops);
	(void) vfs_busy(mp, MBF_NOWAIT);
	atomic_add_acq_int(&vfsp->vfc_refcount, 1);
	mp->mnt_op = vfsp->vfc_vfsops;
	mp->mnt_vfc = vfsp;
	mp->mnt_stat.f_type = vfsp->vfc_typenum;
	mp->mnt_gen++;
	strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
	mp->mnt_vnodecovered = vp;
	mp->mnt_cred = crdup(cred);
	mp->mnt_stat.f_owner = cred->cr_uid;
	strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
	mp->mnt_iosize_max = DFLTPHYS;
#ifdef MAC
	mac_mount_init(mp);
	mac_mount_create(cred, mp);
#endif
	arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
	mp->mnt_upper_pending = 0;
	TAILQ_INIT(&mp->mnt_uppers);
	TAILQ_INIT(&mp->mnt_notify);
	mp->mnt_taskqueue_flags = 0;
	mp->mnt_unmount_retries = 0;
	return (mp);
}

/*
 * Destroy the mount struct previously allocated by vfs_mount_alloc().
 */
void
vfs_mount_destroy(struct mount *mp)
{

	if (mp->mnt_vfs_ops == 0)
		panic("%s: entered with zero vfs_ops\n", __func__);

	vfs_assert_mount_counters(mp);

	MNT_ILOCK(mp);
	mp->mnt_kern_flag |= MNTK_REFEXPIRE;
	if (mp->mnt_kern_flag & MNTK_MWAIT) {
		mp->mnt_kern_flag &= ~MNTK_MWAIT;
		wakeup(mp);
	}
	while (mp->mnt_ref)
		msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
	KASSERT(mp->mnt_ref == 0,
	    ("%s: invalid refcount in the drain path @ %s:%d", __func__,
	    __FILE__, __LINE__));
	if (mp->mnt_writeopcount != 0)
		panic("vfs_mount_destroy: nonzero writeopcount");
	if (mp->mnt_secondary_writes != 0)
		panic("vfs_mount_destroy: nonzero secondary_writes");
	atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
		struct vnode *vp;

		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
			vn_printf(vp, "dangling vnode ");
		panic("unmount: dangling vnode");
	}
	KASSERT(mp->mnt_upper_pending == 0, ("mnt_upper_pending"));
	KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
	KASSERT(TAILQ_EMPTY(&mp->mnt_notify), ("mnt_notify"));
	if (mp->mnt_nvnodelistsize != 0)
		panic("vfs_mount_destroy: nonzero nvnodelistsize");
	if (mp->mnt_lazyvnodelistsize != 0)
		panic("vfs_mount_destroy: nonzero lazyvnodelistsize");
	if (mp->mnt_lockref != 0)
		panic("vfs_mount_destroy: nonzero lock refcount");
	MNT_IUNLOCK(mp);

	if (mp->mnt_vfs_ops != 1)
		panic("%s: vfs_ops should be 1 but %d found\n", __func__,
		    mp->mnt_vfs_ops);

	if (mp->mnt_rootvnode != NULL)
		panic("%s: mount point still has a root vnode %p\n", __func__,
		    mp->mnt_rootvnode);

	if (mp->mnt_vnodecovered != NULL)
		vrele(mp->mnt_vnodecovered);
#ifdef MAC
	mac_mount_destroy(mp);
#endif
	if (mp->mnt_opt != NULL)
		vfs_freeopts(mp->mnt_opt);
	crfree(mp->mnt_cred);
	uma_zfree(mount_zone, mp);
}

static bool
vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
{
	/* This is an upgrade of an exisiting mount. */
	if ((fsflags & MNT_UPDATE) != 0)
		return (false);
	/* This is already an R/O mount. */
	if ((fsflags & MNT_RDONLY) != 0)
		return (false);

	switch (error) {
	case ENODEV:	/* generic, geom, ... */
	case EACCES:	/* cam/scsi, ... */
	case EROFS:	/* md, mmcsd, ... */
		/*
		 * These errors can be returned by the storage layer to signal
		 * that the media is read-only.  No harm in the R/O mount
		 * attempt if the error was returned for some other reason.
		 */
		return (true);
	default:
		return (false);
	}
}

int
vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
{
	struct vfsoptlist *optlist;
	struct vfsopt *opt, *tmp_opt;
	char *fstype, *fspath, *errmsg;
	int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
	bool autoro;

	errmsg = fspath = NULL;
	errmsg_len = fspathlen = 0;
	errmsg_pos = -1;
	autoro = default_autoro;

	error = vfs_buildopts(fsoptions, &optlist);
	if (error)
		return (error);

	if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
		errmsg_pos = vfs_getopt_pos(optlist, "errmsg");

	/*
	 * We need these two options before the others,
	 * and they are mandatory for any filesystem.
	 * Ensure they are NUL terminated as well.
	 */
	fstypelen = 0;
	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
	if (error || fstypelen <= 0 || fstype[fstypelen - 1] != '\0') {
		error = EINVAL;
		if (errmsg != NULL)
			strncpy(errmsg, "Invalid fstype", errmsg_len);
		goto bail;
	}
	fspathlen = 0;
	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
	if (error || fspathlen <= 0 || fspath[fspathlen - 1] != '\0') {
		error = EINVAL;
		if (errmsg != NULL)
			strncpy(errmsg, "Invalid fspath", errmsg_len);
		goto bail;
	}

	/*
	 * We need to see if we have the "update" option
	 * before we call vfs_domount(), since vfs_domount() has special
	 * logic based on MNT_UPDATE.  This is very important
	 * when we want to update the root filesystem.
	 */
	TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
		int do_freeopt = 0;

		if (strcmp(opt->name, "update") == 0) {
			fsflags |= MNT_UPDATE;
			do_freeopt = 1;
		}
		else if (strcmp(opt->name, "async") == 0)
			fsflags |= MNT_ASYNC;
		else if (strcmp(opt->name, "force") == 0) {
			fsflags |= MNT_FORCE;
			do_freeopt = 1;
		}
		else if (strcmp(opt->name, "reload") == 0) {
			fsflags |= MNT_RELOAD;
			do_freeopt = 1;
		}
		else if (strcmp(opt->name, "multilabel") == 0)
			fsflags |= MNT_MULTILABEL;
		else if (strcmp(opt->name, "noasync") == 0)
			fsflags &= ~MNT_ASYNC;
		else if (strcmp(opt->name, "noatime") == 0)
			fsflags |= MNT_NOATIME;
		else if (strcmp(opt->name, "atime") == 0) {
			free(opt->name, M_MOUNT);
			opt->name = strdup("nonoatime", M_MOUNT);
		}
		else if (strcmp(opt->name, "noclusterr") == 0)
			fsflags |= MNT_NOCLUSTERR;
		else if (strcmp(opt->name, "clusterr") == 0) {
			free(opt->name, M_MOUNT);
			opt->name = strdup("nonoclusterr", M_MOUNT);
		}
		else if (strcmp(opt->name, "noclusterw") == 0)
			fsflags |= MNT_NOCLUSTERW;
		else if (strcmp(opt->name, "clusterw") == 0) {
			free(opt->name, M_MOUNT);
			opt->name = strdup("nonoclusterw", M_MOUNT);
		}
		else if (strcmp(opt->name, "noexec") == 0)
			fsflags |= MNT_NOEXEC;
		else if (strcmp(opt->name, "exec") == 0) {
			free(opt->name, M_MOUNT);
			opt->name = strdup("nonoexec", M_MOUNT);
		}
		else if (strcmp(opt->name, "nosuid") == 0)
			fsflags |= MNT_NOSUID;
		else if (strcmp(opt->name, "suid") == 0) {
			free(opt->name, M_MOUNT);
			opt->name = strdup("nonosuid", M_MOUNT);
		}
		else if (strcmp(opt->name, "nosymfollow") == 0)
			fsflags |= MNT_NOSYMFOLLOW;
		else if (strcmp(opt->name, "symfollow") == 0) {
			free(opt->name, M_MOUNT);
			opt->name = strdup("nonosymfollow", M_MOUNT);
		}
		else if (strcmp(opt->name, "noro") == 0) {
			fsflags &= ~MNT_RDONLY;
			autoro = false;
		}
		else if (strcmp(opt->name, "rw") == 0) {
			fsflags &= ~MNT_RDONLY;
			autoro = false;
		}
		else if (strcmp(opt->name, "ro") == 0) {
			fsflags |= MNT_RDONLY;
			autoro = false;
		}
		else if (strcmp(opt->name, "rdonly") == 0) {
			free(opt->name, M_MOUNT);
			opt->name = strdup("ro", M_MOUNT);
			fsflags |= MNT_RDONLY;
			autoro = false;
		}
		else if (strcmp(opt->name, "autoro") == 0) {
			do_freeopt = 1;
			autoro = true;
		}
		else if (strcmp(opt->name, "suiddir") == 0)
			fsflags |= MNT_SUIDDIR;
		else if (strcmp(opt->name, "sync") == 0)
			fsflags |= MNT_SYNCHRONOUS;
		else if (strcmp(opt->name, "union") == 0)
			fsflags |= MNT_UNION;
		else if (strcmp(opt->name, "automounted") == 0) {
			fsflags |= MNT_AUTOMOUNTED;
			do_freeopt = 1;
		} else if (strcmp(opt->name, "nocover") == 0) {
			fsflags |= MNT_NOCOVER;
			do_freeopt = 1;
		} else if (strcmp(opt->name, "cover") == 0) {
			fsflags &= ~MNT_NOCOVER;
			do_freeopt = 1;
		} else if (strcmp(opt->name, "emptydir") == 0) {
			fsflags |= MNT_EMPTYDIR;
			do_freeopt = 1;
		} else if (strcmp(opt->name, "noemptydir") == 0) {
			fsflags &= ~MNT_EMPTYDIR;
			do_freeopt = 1;
		}
		if (do_freeopt)
			vfs_freeopt(optlist, opt);
	}

	/*
	 * Be ultra-paranoid about making sure the type and fspath
	 * variables will fit in our mp buffers, including the
	 * terminating NUL.
	 */
	if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
		error = ENAMETOOLONG;
		goto bail;
	}

	error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
	if (error == ENOENT) {
		error = EINVAL;
		if (errmsg != NULL)
			strncpy(errmsg, "Invalid fstype", errmsg_len);
		goto bail;
	}

	/*
	 * See if we can mount in the read-only mode if the error code suggests
	 * that it could be possible and the mount options allow for that.
	 * Never try it if "[no]{ro|rw}" has been explicitly requested and not
	 * overridden by "autoro".
	 */
	if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
		printf("%s: R/W mount failed, possibly R/O media,"
		    " trying R/O mount\n", __func__);
		fsflags |= MNT_RDONLY;
		error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
	}
bail:
	/* copyout the errmsg */
	if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
	    && errmsg_len > 0 && errmsg != NULL) {
		if (fsoptions->uio_segflg == UIO_SYSSPACE) {
			bcopy(errmsg,
			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
		} else {
			copyout(errmsg,
			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
		}
	}

	if (optlist != NULL)
		vfs_freeopts(optlist);
	return (error);
}

/*
 * Old mount API.
 */
#ifndef _SYS_SYSPROTO_H_
struct mount_args {
	char	*type;
	char	*path;
	int	flags;
	caddr_t	data;
};
#endif
/* ARGSUSED */
int
sys_mount(struct thread *td, struct mount_args *uap)
{
	char *fstype;
	struct vfsconf *vfsp = NULL;
	struct mntarg *ma = NULL;
	uint64_t flags;
	int error;

	/*
	 * Mount flags are now 64-bits. On 32-bit architectures only
	 * 32-bits are passed in, but from here on everything handles
	 * 64-bit flags correctly.
	 */
	flags = uap->flags;

	AUDIT_ARG_FFLAGS(flags);

	/*
	 * Filter out MNT_ROOTFS.  We do not want clients of mount() in
	 * userspace to set this flag, but we must filter it out if we want
	 * MNT_UPDATE on the root file system to work.
	 * MNT_ROOTFS should only be set by the kernel when mounting its
	 * root file system.
	 */
	flags &= ~MNT_ROOTFS;

	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
	if (error) {
		free(fstype, M_TEMP);
		return (error);
	}

	AUDIT_ARG_TEXT(fstype);
	vfsp = vfs_byname_kld(fstype, td, &error);
	free(fstype, M_TEMP);
	if (vfsp == NULL)
		return (ENOENT);
	if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 &&
	    vfsp->vfc_vfsops_sd->vfs_cmount == NULL) ||
	    ((vfsp->vfc_flags & VFCF_SBDRY) == 0 &&
	    vfsp->vfc_vfsops->vfs_cmount == NULL))
		return (EOPNOTSUPP);

	ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
	ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
	ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
	ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
	ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");

	if ((vfsp->vfc_flags & VFCF_SBDRY) != 0)
		return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags));
	return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags));
}

/*
 * vfs_domount_first(): first file system mount (not update)
 */
static int
vfs_domount_first(
	struct thread *td,		/* Calling thread. */
	struct vfsconf *vfsp,		/* File system type. */
	char *fspath,			/* Mount path. */
	struct vnode *vp,		/* Vnode to be covered. */
	uint64_t fsflags,		/* Flags common to all filesystems. */
	struct vfsoptlist **optlist	/* Options local to the filesystem. */
	)
{
	struct vattr va;
	struct mount *mp;
	struct vnode *newdp, *rootvp;
	int error, error1;
	bool unmounted;

	ASSERT_VOP_ELOCKED(vp, __func__);
	KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));

	/*
	 * If the jail of the calling thread lacks permission for this type of
	 * file system, or is trying to cover its own root, deny immediately.
	 */
	if (jailed(td->td_ucred) && (!prison_allow(td->td_ucred,
	    vfsp->vfc_prison_flag) || vp == td->td_ucred->cr_prison->pr_root)) {
		vput(vp);
		return (EPERM);
	}

	/*
	 * If the user is not root, ensure that they own the directory
	 * onto which we are attempting to mount.
	 */
	error = VOP_GETATTR(vp, &va, td->td_ucred);
	if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
		error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN);
	if (error == 0)
		error = vinvalbuf(vp, V_SAVE, 0, 0);
	if (error == 0 && vp->v_type != VDIR)
		error = ENOTDIR;
	if (error == 0 && (fsflags & MNT_EMPTYDIR) != 0)
		error = vfs_emptydir(vp);
	if (error == 0) {
		VI_LOCK(vp);
		if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
			vp->v_iflag |= VI_MOUNT;
		else
			error = EBUSY;
		VI_UNLOCK(vp);
	}
	if (error != 0) {
		vput(vp);
		return (error);
	}
	vn_seqc_write_begin(vp);
	VOP_UNLOCK(vp);

	/* Allocate and initialize the filesystem. */
	mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
	/* XXXMAC: pass to vfs_mount_alloc? */
	mp->mnt_optnew = *optlist;
	/* Set the mount level flags. */
	mp->mnt_flag = (fsflags &
	    (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY | MNT_FORCE));

	/*
	 * Mount the filesystem.
	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
	 * get.  No freeing of cn_pnbuf.
	 */
	error1 = 0;
	unmounted = true;
	if ((error = VFS_MOUNT(mp)) != 0 ||
	    (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 ||
	    (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) {
		rootvp = NULL;
		if (error1 != 0) {
			MPASS(error == 0);
			rootvp = vfs_cache_root_clear(mp);
			if (rootvp != NULL) {
				vhold(rootvp);
				vrele(rootvp);
			}
			(void)vn_start_write(NULL, &mp, V_WAIT);
			MNT_ILOCK(mp);
			mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_UNMOUNTF;
			MNT_IUNLOCK(mp);
			VFS_PURGE(mp);
			error = VFS_UNMOUNT(mp, 0);
			vn_finished_write(mp);
			if (error != 0) {
				printf(
		    "failed post-mount (%d): rollback unmount returned %d\n",
				    error1, error);
				unmounted = false;
			}
			error = error1;
		}
		vfs_unbusy(mp);
		mp->mnt_vnodecovered = NULL;
		if (unmounted) {
			/* XXXKIB wait for mnt_lockref drain? */
			vfs_mount_destroy(mp);
		}
		VI_LOCK(vp);
		vp->v_iflag &= ~VI_MOUNT;
		VI_UNLOCK(vp);
		if (rootvp != NULL) {
			vn_seqc_write_end(rootvp);
			vdrop(rootvp);
		}
		vn_seqc_write_end(vp);
		vrele(vp);
		return (error);
	}
	vn_seqc_write_begin(newdp);
	VOP_UNLOCK(newdp);

	if (mp->mnt_opt != NULL)
		vfs_freeopts(mp->mnt_opt);
	mp->mnt_opt = mp->mnt_optnew;
	*optlist = NULL;

	/*
	 * Prevent external consumers of mount options from reading mnt_optnew.
	 */
	mp->mnt_optnew = NULL;

	MNT_ILOCK(mp);
	if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
	    (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
		mp->mnt_kern_flag |= MNTK_ASYNC;
	else
		mp->mnt_kern_flag &= ~MNTK_ASYNC;
	MNT_IUNLOCK(mp);

	VI_LOCK(vp);
	vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
	vp->v_mountedhere = mp;
	VI_UNLOCK(vp);
	cache_purge(vp);

	/*
	 * We need to lock both vnodes.
	 *
	 * Use vn_lock_pair to avoid establishing an ordering between vnodes
	 * from different filesystems.
	 */
	vn_lock_pair(vp, false, newdp, false);

	VI_LOCK(vp);
	vp->v_iflag &= ~VI_MOUNT;
	VI_UNLOCK(vp);
	/* Place the new filesystem at the end of the mount list. */
	mtx_lock(&mountlist_mtx);
	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
	mtx_unlock(&mountlist_mtx);
	vfs_event_signal(NULL, VQ_MOUNT, 0);
	VOP_UNLOCK(vp);
	EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td);
	VOP_UNLOCK(newdp);
	mount_devctl_event("MOUNT", mp, false);
	mountcheckdirs(vp, newdp);
	vn_seqc_write_end(vp);
	vn_seqc_write_end(newdp);
	vrele(newdp);
	if ((mp->mnt_flag & MNT_RDONLY) == 0)
		vfs_allocate_syncvnode(mp);
	vfs_op_exit(mp);
	vfs_unbusy(mp);
	return (0);
}

/*
 * vfs_domount_update(): update of mounted file system
 */
static int
vfs_domount_update(
	struct thread *td,		/* Calling thread. */
	struct vnode *vp,		/* Mount point vnode. */
	uint64_t fsflags,		/* Flags common to all filesystems. */
	struct vfsoptlist **optlist	/* Options local to the filesystem. */
	)
{
	struct export_args export;
	struct o2export_args o2export;
	struct vnode *rootvp;
	void *bufp;
	struct mount *mp;
	int error, export_error, i, len;
	uint64_t flag;
	gid_t *grps;

	ASSERT_VOP_ELOCKED(vp, __func__);
	KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
	mp = vp->v_mount;

	if ((vp->v_vflag & VV_ROOT) == 0) {
		if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
		    == 0)
			error = EXDEV;
		else
			error = EINVAL;
		vput(vp);
		return (error);
	}

	/*
	 * We only allow the filesystem to be reloaded if it
	 * is currently mounted read-only.
	 */
	flag = mp->mnt_flag;
	if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
		vput(vp);
		return (EOPNOTSUPP);	/* Needs translation */
	}
	/*
	 * Only privileged root, or (if MNT_USER is set) the user that
	 * did the original mount is permitted to update it.
	 */
	error = vfs_suser(mp, td);
	if (error != 0) {
		vput(vp);
		return (error);
	}
	if (vfs_busy(mp, MBF_NOWAIT)) {
		vput(vp);
		return (EBUSY);
	}
	VI_LOCK(vp);
	if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
		VI_UNLOCK(vp);
		vfs_unbusy(mp);
		vput(vp);
		return (EBUSY);
	}
	vp->v_iflag |= VI_MOUNT;
	VI_UNLOCK(vp);
	VOP_UNLOCK(vp);

	vfs_op_enter(mp);
	vn_seqc_write_begin(vp);

	rootvp = NULL;
	MNT_ILOCK(mp);
	if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
		MNT_IUNLOCK(mp);
		error = EBUSY;
		goto end;
	}
	mp->mnt_flag &= ~MNT_UPDATEMASK;
	mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
	    MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
	if ((mp->mnt_flag & MNT_ASYNC) == 0)
		mp->mnt_kern_flag &= ~MNTK_ASYNC;
	rootvp = vfs_cache_root_clear(mp);
	MNT_IUNLOCK(mp);
	mp->mnt_optnew = *optlist;
	vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);

	/*
	 * Mount the filesystem.
	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
	 * get.  No freeing of cn_pnbuf.
	 */
	error = VFS_MOUNT(mp);

	export_error = 0;
	/* Process the export option. */
	if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
	    &len) == 0) {
		/* Assume that there is only 1 ABI for each length. */
		switch (len) {
		case (sizeof(struct oexport_args)):
			bzero(&o2export, sizeof(o2export));
			/* FALLTHROUGH */
		case (sizeof(o2export)):
			bcopy(bufp, &o2export, len);
			export.ex_flags = (uint64_t)o2export.ex_flags;
			export.ex_root = o2export.ex_root;
			export.ex_uid = o2export.ex_anon.cr_uid;
			export.ex_groups = NULL;
			export.ex_ngroups = o2export.ex_anon.cr_ngroups;
			if (export.ex_ngroups > 0) {
				if (export.ex_ngroups <= XU_NGROUPS) {
					export.ex_groups = malloc(
					    export.ex_ngroups * sizeof(gid_t),
					    M_TEMP, M_WAITOK);
					for (i = 0; i < export.ex_ngroups; i++)
						export.ex_groups[i] =
						  o2export.ex_anon.cr_groups[i];
				} else
					export_error = EINVAL;
			} else if (export.ex_ngroups < 0)
				export_error = EINVAL;
			export.ex_addr = o2export.ex_addr;
			export.ex_addrlen = o2export.ex_addrlen;
			export.ex_mask = o2export.ex_mask;
			export.ex_masklen = o2export.ex_masklen;
			export.ex_indexfile = o2export.ex_indexfile;
			export.ex_numsecflavors = o2export.ex_numsecflavors;
			if (export.ex_numsecflavors < MAXSECFLAVORS) {
				for (i = 0; i < export.ex_numsecflavors; i++)
					export.ex_secflavors[i] =
					    o2export.ex_secflavors[i];
			} else
				export_error = EINVAL;
			if (export_error == 0)
				export_error = vfs_export(mp, &export);
			free(export.ex_groups, M_TEMP);
			break;
		case (sizeof(export)):
			bcopy(bufp, &export, len);
			grps = NULL;
			if (export.ex_ngroups > 0) {
				if (export.ex_ngroups <= NGROUPS_MAX) {
					grps = malloc(export.ex_ngroups *
					    sizeof(gid_t), M_TEMP, M_WAITOK);
					export_error = copyin(export.ex_groups,
					    grps, export.ex_ngroups *
					    sizeof(gid_t));
					if (export_error == 0)
						export.ex_groups = grps;
				} else
					export_error = EINVAL;
			} else if (export.ex_ngroups == 0)
				export.ex_groups = NULL;
			else
				export_error = EINVAL;
			if (export_error == 0)
				export_error = vfs_export(mp, &export);
			free(grps, M_TEMP);
			break;
		default:
			export_error = EINVAL;
			break;
		}
	}

	MNT_ILOCK(mp);
	if (error == 0) {
		mp->mnt_flag &=	~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
		    MNT_SNAPSHOT);
	} else {
		/*
		 * If we fail, restore old mount flags. MNT_QUOTA is special,
		 * because it is not part of MNT_UPDATEMASK, but it could have
		 * changed in the meantime if quotactl(2) was called.
		 * All in all we want current value of MNT_QUOTA, not the old
		 * one.
		 */
		mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
	}
	if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
	    (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
		mp->mnt_kern_flag |= MNTK_ASYNC;
	else
		mp->mnt_kern_flag &= ~MNTK_ASYNC;
	MNT_IUNLOCK(mp);

	if (error != 0)
		goto end;

	mount_devctl_event("REMOUNT", mp, true);
	if (mp->mnt_opt != NULL)
		vfs_freeopts(mp->mnt_opt);
	mp->mnt_opt = mp->mnt_optnew;
	*optlist = NULL;
	(void)VFS_STATFS(mp, &mp->mnt_stat);
	/*
	 * Prevent external consumers of mount options from reading
	 * mnt_optnew.
	 */
	mp->mnt_optnew = NULL;

	if ((mp->mnt_flag & MNT_RDONLY) == 0)
		vfs_allocate_syncvnode(mp);
	else
		vfs_deallocate_syncvnode(mp);
end:
	vfs_op_exit(mp);
	if (rootvp != NULL) {
		vn_seqc_write_end(rootvp);
		vrele(rootvp);
	}
	vn_seqc_write_end(vp);
	vfs_unbusy(mp);
	VI_LOCK(vp);
	vp->v_iflag &= ~VI_MOUNT;
	VI_UNLOCK(vp);
	vrele(vp);
	return (error != 0 ? error : export_error);
}

/*
 * vfs_domount(): actually attempt a filesystem mount.
 */
static int
vfs_domount(
	struct thread *td,		/* Calling thread. */
	const char *fstype,		/* Filesystem type. */
	char *fspath,			/* Mount path. */
	uint64_t fsflags,		/* Flags common to all filesystems. */
	struct vfsoptlist **optlist	/* Options local to the filesystem. */
	)
{
	struct vfsconf *vfsp;
	struct nameidata nd;
	struct vnode *vp;
	char *pathbuf;
	int error;

	/*
	 * Be ultra-paranoid about making sure the type and fspath
	 * variables will fit in our mp buffers, including the
	 * terminating NUL.
	 */
	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
		return (ENAMETOOLONG);

	if (jailed(td->td_ucred) || usermount == 0) {
		if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
			return (error);
	}

	/*
	 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
	 */
	if (fsflags & MNT_EXPORTED) {
		error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
		if (error)
			return (error);
	}
	if (fsflags & MNT_SUIDDIR) {
		error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
		if (error)
			return (error);
	}
	/*
	 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
	 */
	if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
		if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
			fsflags |= MNT_NOSUID | MNT_USER;
	}

	/* Load KLDs before we lock the covered vnode to avoid reversals. */
	vfsp = NULL;
	if ((fsflags & MNT_UPDATE) == 0) {
		/* Don't try to load KLDs if we're mounting the root. */
		if (fsflags & MNT_ROOTFS) {
			if ((vfsp = vfs_byname(fstype)) == NULL)
				return (ENODEV);
		} else {
			if ((vfsp = vfs_byname_kld(fstype, td, &error)) == NULL)
				return (error);
		}
	}

	/*
	 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
	 */
	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
	    fspath);
	error = namei(&nd);
	if (error != 0)
		return (error);
	NDFREE_PNBUF(&nd);
	vp = nd.ni_vp;
	if ((fsflags & MNT_UPDATE) == 0) {
		if ((vp->v_vflag & VV_ROOT) != 0 &&
		    (fsflags & MNT_NOCOVER) != 0) {
			vput(vp);
			return (EBUSY);
		}
		pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
		strcpy(pathbuf, fspath);
		error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
		if (error == 0) {
			error = vfs_domount_first(td, vfsp, pathbuf, vp,
			    fsflags, optlist);
		}
		free(pathbuf, M_TEMP);
	} else
		error = vfs_domount_update(td, vp, fsflags, optlist);

	return (error);
}

/*
 * Unmount a filesystem.
 *
 * Note: unmount takes a path to the vnode mounted on as argument, not
 * special file (as before).
 */
#ifndef _SYS_SYSPROTO_H_
struct unmount_args {
	char	*path;
	int	flags;
};
#endif
/* ARGSUSED */
int
sys_unmount(struct thread *td, struct unmount_args *uap)
{

	return (kern_unmount(td, uap->path, uap->flags));
}

int
kern_unmount(struct thread *td, const char *path, int flags)
{
	struct nameidata nd;
	struct mount *mp;
	char *fsidbuf, *pathbuf;
	fsid_t fsid;
	int error;

	AUDIT_ARG_VALUE(flags);
	if (jailed(td->td_ucred) || usermount == 0) {
		error = priv_check(td, PRIV_VFS_UNMOUNT);
		if (error)
			return (error);
	}

	if (flags & MNT_BYFSID) {
		fsidbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
		error = copyinstr(path, fsidbuf, MNAMELEN, NULL);
		if (error) {
			free(fsidbuf, M_TEMP);
			return (error);
		}

		AUDIT_ARG_TEXT(fsidbuf);
		/* Decode the filesystem ID. */
		if (sscanf(fsidbuf, "FSID:%d:%d", &fsid.val[0], &fsid.val[1]) != 2) {
			free(fsidbuf, M_TEMP);
			return (EINVAL);
		}

		mp = vfs_getvfs(&fsid);
		free(fsidbuf, M_TEMP);
		if (mp == NULL) {
			return (ENOENT);
		}
	} else {
		pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
		error = copyinstr(path, pathbuf, MNAMELEN, NULL);
		if (error) {
			free(pathbuf, M_TEMP);
			return (error);
		}

		/*
		 * Try to find global path for path argument.
		 */
		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
		    UIO_SYSSPACE, pathbuf);
		if (namei(&nd) == 0) {
			NDFREE_PNBUF(&nd);
			error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
			    MNAMELEN);
			if (error == 0)
				vput(nd.ni_vp);
		}
		mtx_lock(&mountlist_mtx);
		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
				vfs_ref(mp);
				break;
			}
		}
		mtx_unlock(&mountlist_mtx);
		free(pathbuf, M_TEMP);
		if (mp == NULL) {
			/*
			 * Previously we returned ENOENT for a nonexistent path and
			 * EINVAL for a non-mountpoint.  We cannot tell these apart
			 * now, so in the !MNT_BYFSID case return the more likely
			 * EINVAL for compatibility.
			 */
			return (EINVAL);
		}
	}

	/*
	 * Don't allow unmounting the root filesystem.
	 */
	if (mp->mnt_flag & MNT_ROOTFS) {
		vfs_rel(mp);
		return (EINVAL);
	}
	error = dounmount(mp, flags, td);
	return (error);
}

/*
 * Return error if any of the vnodes, ignoring the root vnode
 * and the syncer vnode, have non-zero usecount.
 *
 * This function is purely advisory - it can return false positives
 * and negatives.
 */
static int
vfs_check_usecounts(struct mount *mp)
{
	struct vnode *vp, *mvp;

	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
		if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
		    vp->v_usecount != 0) {
			VI_UNLOCK(vp);
			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
			return (EBUSY);
		}
		VI_UNLOCK(vp);
	}

	return (0);
}

static void
dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
{

	mtx_assert(MNT_MTX(mp), MA_OWNED);
	mp->mnt_kern_flag &= ~mntkflags;
	if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
		mp->mnt_kern_flag &= ~MNTK_MWAIT;
		wakeup(mp);
	}
	vfs_op_exit_locked(mp);
	MNT_IUNLOCK(mp);
	if (coveredvp != NULL) {
		VOP_UNLOCK(coveredvp);
		vdrop(coveredvp);
	}
	vn_finished_write(mp);
}

/*
 * There are various reference counters associated with the mount point.
 * Normally it is permitted to modify them without taking the mnt ilock,
 * but this behavior can be temporarily disabled if stable value is needed
 * or callers are expected to block (e.g. to not allow new users during
 * forced unmount).
 */
void
vfs_op_enter(struct mount *mp)
{
	struct mount_pcpu *mpcpu;
	int cpu;

	MNT_ILOCK(mp);
	mp->mnt_vfs_ops++;
	if (mp->mnt_vfs_ops > 1) {
		MNT_IUNLOCK(mp);
		return;
	}
	vfs_op_barrier_wait(mp);
	CPU_FOREACH(cpu) {
		mpcpu = vfs_mount_pcpu_remote(mp, cpu);

		mp->mnt_ref += mpcpu->mntp_ref;
		mpcpu->mntp_ref = 0;

		mp->mnt_lockref += mpcpu->mntp_lockref;
		mpcpu->mntp_lockref = 0;

		mp->mnt_writeopcount += mpcpu->mntp_writeopcount;
		mpcpu->mntp_writeopcount = 0;
	}
	if (mp->mnt_ref <= 0 || mp->mnt_lockref < 0 || mp->mnt_writeopcount < 0)
		panic("%s: invalid count(s) on mp %p: ref %d lockref %d writeopcount %d\n",
		    __func__, mp, mp->mnt_ref, mp->mnt_lockref, mp->mnt_writeopcount);
	MNT_IUNLOCK(mp);
	vfs_assert_mount_counters(mp);
}

void
vfs_op_exit_locked(struct mount *mp)
{

	mtx_assert(MNT_MTX(mp), MA_OWNED);

	if (mp->mnt_vfs_ops <= 0)
		panic("%s: invalid vfs_ops count %d for mp %p\n",
		    __func__, mp->mnt_vfs_ops, mp);
	KASSERT(mp->mnt_vfs_ops > 1 ||
	    (mp->mnt_kern_flag & (MNTK_UNMOUNT | MNTK_SUSPEND)) == 0,
	    ("%s: vfs_ops too low (%d) for mp %p in unmount or suspend",
	    __func__, mp->mnt_vfs_ops, mp));
	mp->mnt_vfs_ops--;
}

void
vfs_op_exit(struct mount *mp)
{

	MNT_ILOCK(mp);
	vfs_op_exit_locked(mp);
	MNT_IUNLOCK(mp);
}

struct vfs_op_barrier_ipi {
	struct mount *mp;
	struct smp_rendezvous_cpus_retry_arg srcra;
};

static void
vfs_op_action_func(void *arg)
{
	struct vfs_op_barrier_ipi *vfsopipi;
	struct mount *mp;

	vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
	mp = vfsopipi->mp;

	if (!vfs_op_thread_entered(mp))
		smp_rendezvous_cpus_done(arg);
}

static void
vfs_op_wait_func(void *arg, int cpu)
{
	struct vfs_op_barrier_ipi *vfsopipi;
	struct mount *mp;
	struct mount_pcpu *mpcpu;

	vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
	mp = vfsopipi->mp;

	mpcpu = vfs_mount_pcpu_remote(mp, cpu);
	while (atomic_load_int(&mpcpu->mntp_thread_in_ops))
		cpu_spinwait();
}

void
vfs_op_barrier_wait(struct mount *mp)
{
	struct vfs_op_barrier_ipi vfsopipi;

	vfsopipi.mp = mp;

	smp_rendezvous_cpus_retry(all_cpus,
	    smp_no_rendezvous_barrier,
	    vfs_op_action_func,
	    smp_no_rendezvous_barrier,
	    vfs_op_wait_func,
	    &vfsopipi.srcra);
}

#ifdef DIAGNOSTIC
void
vfs_assert_mount_counters(struct mount *mp)
{
	struct mount_pcpu *mpcpu;
	int cpu;

	if (mp->mnt_vfs_ops == 0)
		return;

	CPU_FOREACH(cpu) {
		mpcpu = vfs_mount_pcpu_remote(mp, cpu);
		if (mpcpu->mntp_ref != 0 ||
		    mpcpu->mntp_lockref != 0 ||
		    mpcpu->mntp_writeopcount != 0)
			vfs_dump_mount_counters(mp);
	}
}

void
vfs_dump_mount_counters(struct mount *mp)
{
	struct mount_pcpu *mpcpu;
	int ref, lockref, writeopcount;
	int cpu;

	printf("%s: mp %p vfs_ops %d\n", __func__, mp, mp->mnt_vfs_ops);

	printf("        ref : ");
	ref = mp->mnt_ref;
	CPU_FOREACH(cpu) {
		mpcpu = vfs_mount_pcpu_remote(mp, cpu);
		printf("%d ", mpcpu->mntp_ref);
		ref += mpcpu->mntp_ref;
	}
	printf("\n");
	printf("    lockref : ");
	lockref = mp->mnt_lockref;
	CPU_FOREACH(cpu) {
		mpcpu = vfs_mount_pcpu_remote(mp, cpu);
		printf("%d ", mpcpu->mntp_lockref);
		lockref += mpcpu->mntp_lockref;
	}
	printf("\n");
	printf("writeopcount: ");
	writeopcount = mp->mnt_writeopcount;
	CPU_FOREACH(cpu) {
		mpcpu = vfs_mount_pcpu_remote(mp, cpu);
		printf("%d ", mpcpu->mntp_writeopcount);
		writeopcount += mpcpu->mntp_writeopcount;
	}
	printf("\n");

	printf("counter       struct total\n");
	printf("ref             %-5d  %-5d\n", mp->mnt_ref, ref);
	printf("lockref         %-5d  %-5d\n", mp->mnt_lockref, lockref);
	printf("writeopcount    %-5d  %-5d\n", mp->mnt_writeopcount, writeopcount);

	panic("invalid counts on struct mount");
}
#endif

int
vfs_mount_fetch_counter(struct mount *mp, enum mount_counter which)
{
	struct mount_pcpu *mpcpu;
	int cpu, sum;

	switch (which) {
	case MNT_COUNT_REF:
		sum = mp->mnt_ref;
		break;
	case MNT_COUNT_LOCKREF:
		sum = mp->mnt_lockref;
		break;
	case MNT_COUNT_WRITEOPCOUNT:
		sum = mp->mnt_writeopcount;
		break;
	}

	CPU_FOREACH(cpu) {
		mpcpu = vfs_mount_pcpu_remote(mp, cpu);
		switch (which) {
		case MNT_COUNT_REF:
			sum += mpcpu->mntp_ref;
			break;
		case MNT_COUNT_LOCKREF:
			sum += mpcpu->mntp_lockref;
			break;
		case MNT_COUNT_WRITEOPCOUNT:
			sum += mpcpu->mntp_writeopcount;
			break;
		}
	}
	return (sum);
}

static bool
deferred_unmount_enqueue(struct mount *mp, uint64_t flags, bool requeue,
    int timeout_ticks)
{
	bool enqueued;

	enqueued = false;
	mtx_lock(&deferred_unmount_lock);
	if ((mp->mnt_taskqueue_flags & MNT_DEFERRED) == 0 || requeue) {
		mp->mnt_taskqueue_flags = flags | MNT_DEFERRED;
		STAILQ_INSERT_TAIL(&deferred_unmount_list, mp,
		    mnt_taskqueue_link);
		enqueued = true;
	}
	mtx_unlock(&deferred_unmount_lock);

	if (enqueued) {
		taskqueue_enqueue_timeout(taskqueue_deferred_unmount,
		    &deferred_unmount_task, timeout_ticks);
	}

	return (enqueued);
}

/*
 * Taskqueue handler for processing async/recursive unmounts
 */
static void
vfs_deferred_unmount(void *argi __unused, int pending __unused)
{
	STAILQ_HEAD(, mount) local_unmounts;
	uint64_t flags;
	struct mount *mp, *tmp;
	int error;
	unsigned int retries;
	bool unmounted;

	STAILQ_INIT(&local_unmounts);
	mtx_lock(&deferred_unmount_lock);
	STAILQ_CONCAT(&local_unmounts, &deferred_unmount_list); 
	mtx_unlock(&deferred_unmount_lock);

	STAILQ_FOREACH_SAFE(mp, &local_unmounts, mnt_taskqueue_link, tmp) {
		flags = mp->mnt_taskqueue_flags;
		KASSERT((flags & MNT_DEFERRED) != 0,
		    ("taskqueue unmount without MNT_DEFERRED"));
		error = dounmount(mp, flags, curthread);
		if (error != 0) {
			MNT_ILOCK(mp);
			unmounted = ((mp->mnt_kern_flag & MNTK_REFEXPIRE) != 0);
			MNT_IUNLOCK(mp);

			/*
			 * The deferred unmount thread is the only thread that
			 * modifies the retry counts, so locking/atomics aren't
			 * needed here.
			 */
			retries = (mp->mnt_unmount_retries)++;
			deferred_unmount_total_retries++;
			if (!unmounted && retries < deferred_unmount_retry_limit) {
				deferred_unmount_enqueue(mp, flags, true,
				    -deferred_unmount_retry_delay_hz);
			} else {
				if (retries >= deferred_unmount_retry_limit) {
					printf("giving up on deferred unmount "
					    "of %s after %d retries, error %d\n",
					    mp->mnt_stat.f_mntonname, retries, error);
				}
				vfs_rel(mp);
			}
		}
	}
}

/*
 * Do the actual filesystem unmount.
 */
int
dounmount(struct mount *mp, uint64_t flags, struct thread *td)
{
	struct mount_upper_node *upper;
	struct vnode *coveredvp, *rootvp;
	int error;
	uint64_t async_flag;
	int mnt_gen_r;
	unsigned int retries;

	KASSERT((flags & MNT_DEFERRED) == 0 ||
	    (flags & (MNT_RECURSE | MNT_FORCE)) == (MNT_RECURSE | MNT_FORCE),
	    ("MNT_DEFERRED requires MNT_RECURSE | MNT_FORCE"));

	/*
	 * If the caller has explicitly requested the unmount to be handled by
	 * the taskqueue and we're not already in taskqueue context, queue
	 * up the unmount request and exit.  This is done prior to any
	 * credential checks; MNT_DEFERRED should be used only for kernel-
	 * initiated unmounts and will therefore be processed with the
	 * (kernel) credentials of the taskqueue thread.  Still, callers
	 * should be sure this is the behavior they want.
	 */
	if ((flags & MNT_DEFERRED) != 0 &&
	    taskqueue_member(taskqueue_deferred_unmount, curthread) == 0) {
		if (!deferred_unmount_enqueue(mp, flags, false, 0))
			vfs_rel(mp);
		return (EINPROGRESS);
	}

	/*
	 * Only privileged root, or (if MNT_USER is set) the user that did the
	 * original mount is permitted to unmount this filesystem.
	 * This check should be made prior to queueing up any recursive
	 * unmounts of upper filesystems.  Those unmounts will be executed
	 * with kernel thread credentials and are expected to succeed, so
	 * we must at least ensure the originating context has sufficient
	 * privilege to unmount the base filesystem before proceeding with
	 * the uppers.
	 */
	error = vfs_suser(mp, td);
	if (error != 0) {
		KASSERT((flags & MNT_DEFERRED) == 0,
		    ("taskqueue unmount with insufficient privilege"));
		vfs_rel(mp);
		return (error);
	}

	if (recursive_forced_unmount && ((flags & MNT_FORCE) != 0))
		flags |= MNT_RECURSE;

	if ((flags & MNT_RECURSE) != 0) {
		KASSERT((flags & MNT_FORCE) != 0,
		    ("MNT_RECURSE requires MNT_FORCE"));

		MNT_ILOCK(mp);
		/*
		 * Set MNTK_RECURSE to prevent new upper mounts from being
		 * added, and note that an operation on the uppers list is in
		 * progress.  This will ensure that unregistration from the
		 * uppers list, and therefore any pending unmount of the upper
		 * FS, can't complete until after we finish walking the list.
		 */
		mp->mnt_kern_flag |= MNTK_RECURSE;
		mp->mnt_upper_pending++;
		TAILQ_FOREACH(upper, &mp->mnt_uppers, mnt_upper_link) {
			retries = upper->mp->mnt_unmount_retries;
			if (retries > deferred_unmount_retry_limit) {
				error = EBUSY;
				continue;
			}
			MNT_IUNLOCK(mp);

			vfs_ref(upper->mp);
			if (!deferred_unmount_enqueue(upper->mp, flags,
			    false, 0))
				vfs_rel(upper->mp);
			MNT_ILOCK(mp);
		}
		mp->mnt_upper_pending--;
		if ((mp->mnt_kern_flag & MNTK_UPPER_WAITER) != 0 &&
		    mp->mnt_upper_pending == 0) {
			mp->mnt_kern_flag &= ~MNTK_UPPER_WAITER;
			wakeup(&mp->mnt_uppers);
		}

		/*
		 * If we're not on the taskqueue, wait until the uppers list
		 * is drained before proceeding with unmount.  Otherwise, if
		 * we are on the taskqueue and there are still pending uppers,
		 * just re-enqueue on the end of the taskqueue.
		 */
		if ((flags & MNT_DEFERRED) == 0) {
			while (error == 0 && !TAILQ_EMPTY(&mp->mnt_uppers)) {
				mp->mnt_kern_flag |= MNTK_TASKQUEUE_WAITER;
				error = msleep(&mp->mnt_taskqueue_link,
				    MNT_MTX(mp), PCATCH, "umntqw", 0);
			}
			if (error != 0) {
				MNT_REL(mp);
				MNT_IUNLOCK(mp);
				return (error);
			}
		} else if (!TAILQ_EMPTY(&mp->mnt_uppers)) {
			MNT_IUNLOCK(mp);
			if (error == 0)
				deferred_unmount_enqueue(mp, flags, true, 0);
			return (error);
		}
		MNT_IUNLOCK(mp);
		KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers not empty"));
	}

	/* Allow the taskqueue to safely re-enqueue on failure */
	if ((flags & MNT_DEFERRED) != 0)
		vfs_ref(mp);

	if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
		mnt_gen_r = mp->mnt_gen;
		VI_LOCK(coveredvp);
		vholdl(coveredvp);
		vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
		/*
		 * Check for mp being unmounted while waiting for the
		 * covered vnode lock.
		 */
		if (coveredvp->v_mountedhere != mp ||
		    coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
			VOP_UNLOCK(coveredvp);
			vdrop(coveredvp);
			vfs_rel(mp);
			return (EBUSY);
		}
	}

	vfs_op_enter(mp);

	vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
	MNT_ILOCK(mp);
	if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
	    (mp->mnt_flag & MNT_UPDATE) != 0 ||
	    !TAILQ_EMPTY(&mp->mnt_uppers)) {
		dounmount_cleanup(mp, coveredvp, 0);
		return (EBUSY);
	}
	mp->mnt_kern_flag |= MNTK_UNMOUNT;
	rootvp = vfs_cache_root_clear(mp);
	if (coveredvp != NULL)
		vn_seqc_write_begin(coveredvp);
	if (flags & MNT_NONBUSY) {
		MNT_IUNLOCK(mp);
		error = vfs_check_usecounts(mp);
		MNT_ILOCK(mp);
		if (error != 0) {
			vn_seqc_write_end(coveredvp);
			dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT);
			if (rootvp != NULL) {
				vn_seqc_write_end(rootvp);
				vrele(rootvp);
			}
			return (error);
		}
	}
	/* Allow filesystems to detect that a forced unmount is in progress. */
	if (flags & MNT_FORCE) {
		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
		MNT_IUNLOCK(mp);
		/*
		 * Must be done after setting MNTK_UNMOUNTF and before
		 * waiting for mnt_lockref to become 0.
		 */
		VFS_PURGE(mp);
		MNT_ILOCK(mp);
	}
	error = 0;
	if (mp->mnt_lockref) {
		mp->mnt_kern_flag |= MNTK_DRAINING;
		error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
		    "mount drain", 0);
	}
	MNT_IUNLOCK(mp);
	KASSERT(mp->mnt_lockref == 0,
	    ("%s: invalid lock refcount in the drain path @ %s:%d",
	    __func__, __FILE__, __LINE__));
	KASSERT(error == 0,
	    ("%s: invalid return value for msleep in the drain path @ %s:%d",
	    __func__, __FILE__, __LINE__));

	/*
	 * We want to keep the vnode around so that we can vn_seqc_write_end
	 * after we are done with unmount. Downgrade our reference to a mere
	 * hold count so that we don't interefere with anything.
	 */
	if (rootvp != NULL) {
		vhold(rootvp);
		vrele(rootvp);
	}

	if (mp->mnt_flag & MNT_EXPUBLIC)
		vfs_setpublicfs(NULL, NULL, NULL);

	vfs_periodic(mp, MNT_WAIT);
	MNT_ILOCK(mp);
	async_flag = mp->mnt_flag & MNT_ASYNC;
	mp->mnt_flag &= ~MNT_ASYNC;
	mp->mnt_kern_flag &= ~MNTK_ASYNC;
	MNT_IUNLOCK(mp);
	vfs_deallocate_syncvnode(mp);
	error = VFS_UNMOUNT(mp, flags);
	vn_finished_write(mp);
	/*
	 * If we failed to flush the dirty blocks for this mount point,
	 * undo all the cdir/rdir and rootvnode changes we made above.
	 * Unless we failed to do so because the device is reporting that
	 * it doesn't exist anymore.
	 */
	if (error && error != ENXIO) {
		MNT_ILOCK(mp);
		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
			MNT_IUNLOCK(mp);
			vfs_allocate_syncvnode(mp);
			MNT_ILOCK(mp);
		}
		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
		mp->mnt_flag |= async_flag;
		if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
		    (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
			mp->mnt_kern_flag |= MNTK_ASYNC;
		if (mp->mnt_kern_flag & MNTK_MWAIT) {
			mp->mnt_kern_flag &= ~MNTK_MWAIT;
			wakeup(mp);
		}
		vfs_op_exit_locked(mp);
		MNT_IUNLOCK(mp);
		if (coveredvp) {
			vn_seqc_write_end(coveredvp);
			VOP_UNLOCK(coveredvp);
			vdrop(coveredvp);
		}
		if (rootvp != NULL) {
			vn_seqc_write_end(rootvp);
			vdrop(rootvp);
		}
		return (error);
	}

	mtx_lock(&mountlist_mtx);
	TAILQ_REMOVE(&mountlist, mp, mnt_list);
	mtx_unlock(&mountlist_mtx);
	EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td);
	if (coveredvp != NULL) {
		VI_LOCK(coveredvp);
		vn_irflag_unset_locked(coveredvp, VIRF_MOUNTPOINT);
		coveredvp->v_mountedhere = NULL;
		vn_seqc_write_end_locked(coveredvp);
		VI_UNLOCK(coveredvp);
		VOP_UNLOCK(coveredvp);
		vdrop(coveredvp);
	}
	mount_devctl_event("UNMOUNT", mp, false);
	if (rootvp != NULL) {
		vn_seqc_write_end(rootvp);
		vdrop(rootvp);
	}
	vfs_event_signal(NULL, VQ_UNMOUNT, 0);
	if (rootvnode != NULL && mp == rootvnode->v_mount) {
		vrele(rootvnode);
		rootvnode = NULL;
	}
	if (mp == rootdevmp)
		rootdevmp = NULL;
	if ((flags & MNT_DEFERRED) != 0)
		vfs_rel(mp);
	vfs_mount_destroy(mp);
	return (0);
}

/*
 * Report errors during filesystem mounting.
 */
void
vfs_mount_error(struct mount *mp, const char *fmt, ...)
{
	struct vfsoptlist *moptlist = mp->mnt_optnew;
	va_list ap;
	int error, len;
	char *errmsg;

	error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
	if (error || errmsg == NULL || len <= 0)
		return;

	va_start(ap, fmt);
	vsnprintf(errmsg, (size_t)len, fmt, ap);
	va_end(ap);
}

void
vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
{
	va_list ap;
	int error, len;
	char *errmsg;

	error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
	if (error || errmsg == NULL || len <= 0)
		return;

	va_start(ap, fmt);
	vsnprintf(errmsg, (size_t)len, fmt, ap);
	va_end(ap);
}

/*
 * ---------------------------------------------------------------------
 * Functions for querying mount options/arguments from filesystems.
 */

/*
 * Check that no unknown options are given
 */
int
vfs_filteropt(struct vfsoptlist *opts, const char **legal)
{
	struct vfsopt *opt;
	char errmsg[255];
	const char **t, *p, *q;
	int ret = 0;

	TAILQ_FOREACH(opt, opts, link) {
		p = opt->name;
		q = NULL;
		if (p[0] == 'n' && p[1] == 'o')
			q = p + 2;
		for(t = global_opts; *t != NULL; t++) {
			if (strcmp(*t, p) == 0)
				break;
			if (q != NULL) {
				if (strcmp(*t, q) == 0)
					break;
			}
		}
		if (*t != NULL)
			continue;
		for(t = legal; *t != NULL; t++) {
			if (strcmp(*t, p) == 0)
				break;
			if (q != NULL) {
				if (strcmp(*t, q) == 0)
					break;
			}
		}
		if (*t != NULL)
			continue;
		snprintf(errmsg, sizeof(errmsg),
		    "mount option <%s> is unknown", p);
		ret = EINVAL;
	}
	if (ret != 0) {
		TAILQ_FOREACH(opt, opts, link) {
			if (strcmp(opt->name, "errmsg") == 0) {
				strncpy((char *)opt->value, errmsg, opt->len);
				break;
			}
		}
		if (opt == NULL)
			printf("%s\n", errmsg);
	}
	return (ret);
}

/*
 * Get a mount option by its name.
 *
 * Return 0 if the option was found, ENOENT otherwise.
 * If len is non-NULL it will be filled with the length
 * of the option. If buf is non-NULL, it will be filled
 * with the address of the option.
 */
int
vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len)
{
	struct vfsopt *opt;

	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));

	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) == 0) {
			opt->seen = 1;
			if (len != NULL)
				*len = opt->len;
			if (buf != NULL)
				*buf = opt->value;
			return (0);
		}
	}
	return (ENOENT);
}

int
vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
{
	struct vfsopt *opt;

	if (opts == NULL)
		return (-1);

	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) == 0) {
			opt->seen = 1;
			return (opt->pos);
		}
	}
	return (-1);
}

int
vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
{
	char *opt_value, *vtp;
	quad_t iv;
	int error, opt_len;

	error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
	if (error != 0)
		return (error);
	if (opt_len == 0 || opt_value == NULL)
		return (EINVAL);
	if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
		return (EINVAL);
	iv = strtoq(opt_value, &vtp, 0);
	if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
		return (EINVAL);
	if (iv < 0)
		return (EINVAL);
	switch (vtp[0]) {
	case 't': case 'T':
		iv *= 1024;
		/* FALLTHROUGH */
	case 'g': case 'G':
		iv *= 1024;
		/* FALLTHROUGH */
	case 'm': case 'M':
		iv *= 1024;
		/* FALLTHROUGH */
	case 'k': case 'K':
		iv *= 1024;
	case '\0':
		break;
	default:
		return (EINVAL);
	}
	*value = iv;

	return (0);
}

char *
vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
{
	struct vfsopt *opt;

	*error = 0;
	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) != 0)
			continue;
		opt->seen = 1;
		if (opt->len == 0 ||
		    ((char *)opt->value)[opt->len - 1] != '\0') {
			*error = EINVAL;
			return (NULL);
		}
		return (opt->value);
	}
	*error = ENOENT;
	return (NULL);
}

int
vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
	uint64_t val)
{
	struct vfsopt *opt;

	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) == 0) {
			opt->seen = 1;
			if (w != NULL)
				*w |= val;
			return (1);
		}
	}
	if (w != NULL)
		*w &= ~val;
	return (0);
}

int
vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
{
	va_list ap;
	struct vfsopt *opt;
	int ret;

	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));

	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) != 0)
			continue;
		opt->seen = 1;
		if (opt->len == 0 || opt->value == NULL)
			return (0);
		if (((char *)opt->value)[opt->len - 1] != '\0')
			return (0);
		va_start(ap, fmt);
		ret = vsscanf(opt->value, fmt, ap);
		va_end(ap);
		return (ret);
	}
	return (0);
}

int
vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
{
	struct vfsopt *opt;

	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) != 0)
			continue;
		opt->seen = 1;
		if (opt->value == NULL)
			opt->len = len;
		else {
			if (opt->len != len)
				return (EINVAL);
			bcopy(value, opt->value, len);
		}
		return (0);
	}
	return (ENOENT);
}

int
vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
{
	struct vfsopt *opt;

	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) != 0)
			continue;
		opt->seen = 1;
		if (opt->value == NULL)
			opt->len = len;
		else {
			if (opt->len < len)
				return (EINVAL);
			opt->len = len;
			bcopy(value, opt->value, len);
		}
		return (0);
	}
	return (ENOENT);
}

int
vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
{
	struct vfsopt *opt;

	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) != 0)
			continue;
		opt->seen = 1;
		if (opt->value == NULL)
			opt->len = strlen(value) + 1;
		else if (strlcpy(opt->value, value, opt->len) >= opt->len)
			return (EINVAL);
		return (0);
	}
	return (ENOENT);
}

/*
 * Find and copy a mount option.
 *
 * The size of the buffer has to be specified
 * in len, if it is not the same length as the
 * mount option, EINVAL is returned.
 * Returns ENOENT if the option is not found.
 */
int
vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len)
{
	struct vfsopt *opt;

	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));

	TAILQ_FOREACH(opt, opts, link) {
		if (strcmp(name, opt->name) == 0) {
			opt->seen = 1;
			if (len != opt->len)
				return (EINVAL);
			bcopy(opt->value, dest, opt->len);
			return (0);
		}
	}
	return (ENOENT);
}

int
__vfs_statfs(struct mount *mp, struct statfs *sbp)
{
	/*
	 * Filesystems only fill in part of the structure for updates, we
	 * have to read the entirety first to get all content.
	 */
	if (sbp != &mp->mnt_stat)
		memcpy(sbp, &mp->mnt_stat, sizeof(*sbp));

	/*
	 * Set these in case the underlying filesystem fails to do so.
	 */
	sbp->f_version = STATFS_VERSION;
	sbp->f_namemax = NAME_MAX;
	sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
	sbp->f_nvnodelistsize = mp->mnt_nvnodelistsize;

	return (mp->mnt_op->vfs_statfs(mp, sbp));
}

void
vfs_mountedfrom(struct mount *mp, const char *from)
{

	bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
	strlcpy(mp->mnt_stat.f_mntfromname, from,
	    sizeof mp->mnt_stat.f_mntfromname);
}

/*
 * ---------------------------------------------------------------------
 * This is the api for building mount args and mounting filesystems from
 * inside the kernel.
 *
 * The API works by accumulation of individual args.  First error is
 * latched.
 *
 * XXX: should be documented in new manpage kernel_mount(9)
 */

/* A memory allocation which must be freed when we are done */
struct mntaarg {
	SLIST_ENTRY(mntaarg)	next;
};

/* The header for the mount arguments */
struct mntarg {
	struct iovec *v;
	int len;
	int error;
	SLIST_HEAD(, mntaarg)	list;
};

/*
 * Add a boolean argument.
 *
 * flag is the boolean value.
 * name must start with "no".
 */
struct mntarg *
mount_argb(struct mntarg *ma, int flag, const char *name)
{

	KASSERT(name[0] == 'n' && name[1] == 'o',
	    ("mount_argb(...,%s): name must start with 'no'", name));

	return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
}

/*
 * Add an argument printf style
 */
struct mntarg *
mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
{
	va_list ap;
	struct mntaarg *maa;
	struct sbuf *sb;
	int len;

	if (ma == NULL) {
		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
		SLIST_INIT(&ma->list);
	}
	if (ma->error)
		return (ma);

	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
	    M_MOUNT, M_WAITOK);
	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
	ma->v[ma->len].iov_len = strlen(name) + 1;
	ma->len++;

	sb = sbuf_new_auto();
	va_start(ap, fmt);
	sbuf_vprintf(sb, fmt, ap);
	va_end(ap);
	sbuf_finish(sb);
	len = sbuf_len(sb) + 1;
	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
	SLIST_INSERT_HEAD(&ma->list, maa, next);
	bcopy(sbuf_data(sb), maa + 1, len);
	sbuf_delete(sb);

	ma->v[ma->len].iov_base = maa + 1;
	ma->v[ma->len].iov_len = len;
	ma->len++;

	return (ma);
}

/*
 * Add an argument which is a userland string.
 */
struct mntarg *
mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
{
	struct mntaarg *maa;
	char *tbuf;

	if (val == NULL)
		return (ma);
	if (ma == NULL) {
		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
		SLIST_INIT(&ma->list);
	}
	if (ma->error)
		return (ma);
	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
	SLIST_INSERT_HEAD(&ma->list, maa, next);
	tbuf = (void *)(maa + 1);
	ma->error = copyinstr(val, tbuf, len, NULL);
	return (mount_arg(ma, name, tbuf, -1));
}

/*
 * Plain argument.
 *
 * If length is -1, treat value as a C string.
 */
struct mntarg *
mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
{

	if (ma == NULL) {
		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
		SLIST_INIT(&ma->list);
	}
	if (ma->error)
		return (ma);

	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
	    M_MOUNT, M_WAITOK);
	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
	ma->v[ma->len].iov_len = strlen(name) + 1;
	ma->len++;

	ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
	if (len < 0)
		ma->v[ma->len].iov_len = strlen(val) + 1;
	else
		ma->v[ma->len].iov_len = len;
	ma->len++;
	return (ma);
}

/*
 * Free a mntarg structure
 */
static void
free_mntarg(struct mntarg *ma)
{
	struct mntaarg *maa;

	while (!SLIST_EMPTY(&ma->list)) {
		maa = SLIST_FIRST(&ma->list);
		SLIST_REMOVE_HEAD(&ma->list, next);
		free(maa, M_MOUNT);
	}
	free(ma->v, M_MOUNT);
	free(ma, M_MOUNT);
}

/*
 * Mount a filesystem
 */
int
kernel_mount(struct mntarg *ma, uint64_t flags)
{
	struct uio auio;
	int error;

	KASSERT(ma != NULL, ("kernel_mount NULL ma"));
	KASSERT(ma->error != 0 || ma->v != NULL, ("kernel_mount NULL ma->v"));
	KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));

	error = ma->error;
	if (error == 0) {
		auio.uio_iov = ma->v;
		auio.uio_iovcnt = ma->len;
		auio.uio_segflg = UIO_SYSSPACE;
		error = vfs_donmount(curthread, flags, &auio);
	}
	free_mntarg(ma);
	return (error);
}

/* Map from mount options to printable formats. */
static struct mntoptnames optnames[] = {
	MNTOPT_NAMES
};

#define DEVCTL_LEN 1024
static void
mount_devctl_event(const char *type, struct mount *mp, bool donew)
{
	const uint8_t *cp;
	struct mntoptnames *fp;
	struct sbuf sb;
	struct statfs *sfp = &mp->mnt_stat;
	char *buf;

	buf = malloc(DEVCTL_LEN, M_MOUNT, M_NOWAIT);
	if (buf == NULL)
		return;
	sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN);
	sbuf_cpy(&sb, "mount-point=\"");
	devctl_safe_quote_sb(&sb, sfp->f_mntonname);
	sbuf_cat(&sb, "\" mount-dev=\"");
	devctl_safe_quote_sb(&sb, sfp->f_mntfromname);
	sbuf_cat(&sb, "\" mount-type=\"");
	devctl_safe_quote_sb(&sb, sfp->f_fstypename);
	sbuf_cat(&sb, "\" fsid=0x");
	cp = (const uint8_t *)&sfp->f_fsid.val[0];
	for (int i = 0; i < sizeof(sfp->f_fsid); i++)
		sbuf_printf(&sb, "%02x", cp[i]);
	sbuf_printf(&sb, " owner=%u flags=\"", sfp->f_owner);
	for (fp = optnames; fp->o_opt != 0; fp++) {
		if ((mp->mnt_flag & fp->o_opt) != 0) {
			sbuf_cat(&sb, fp->o_name);
			sbuf_putc(&sb, ';');
		}
	}
	sbuf_putc(&sb, '"');
	sbuf_finish(&sb);

	/*
	 * Options are not published because the form of the options depends on
	 * the file system and may include binary data. In addition, they don't
	 * necessarily provide enough useful information to be actionable when
	 * devd processes them.
	 */

	if (sbuf_error(&sb) == 0)
		devctl_notify("VFS", "FS", type, sbuf_data(&sb));
	sbuf_delete(&sb);
	free(buf, M_MOUNT);
}

/*
 * Force remount specified mount point to read-only.  The argument
 * must be busied to avoid parallel unmount attempts.
 *
 * Intended use is to prevent further writes if some metadata
 * inconsistency is detected.  Note that the function still flushes
 * all cached metadata and data for the mount point, which might be
 * not always suitable.
 */
int
vfs_remount_ro(struct mount *mp)
{
	struct vfsoptlist *opts;
	struct vfsopt *opt;
	struct vnode *vp_covered, *rootvp;
	int error;

	KASSERT(mp->mnt_lockref > 0,
	    ("vfs_remount_ro: mp %p is not busied", mp));
	KASSERT((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0,
	    ("vfs_remount_ro: mp %p is being unmounted (and busy?)", mp));

	rootvp = NULL;
	vp_covered = mp->mnt_vnodecovered;
	error = vget(vp_covered, LK_EXCLUSIVE | LK_NOWAIT);
	if (error != 0)
		return (error);
	VI_LOCK(vp_covered);
	if ((vp_covered->v_iflag & VI_MOUNT) != 0) {
		VI_UNLOCK(vp_covered);
		vput(vp_covered);
		return (EBUSY);
	}
	vp_covered->v_iflag |= VI_MOUNT;
	VI_UNLOCK(vp_covered);
	vfs_op_enter(mp);
	vn_seqc_write_begin(vp_covered);

	MNT_ILOCK(mp);
	if ((mp->mnt_flag & MNT_RDONLY) != 0) {
		MNT_IUNLOCK(mp);
		error = EBUSY;
		goto out;
	}
	mp->mnt_flag |= MNT_UPDATE | MNT_FORCE | MNT_RDONLY;
	rootvp = vfs_cache_root_clear(mp);
	MNT_IUNLOCK(mp);

	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK | M_ZERO);
	TAILQ_INIT(opts);
	opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK | M_ZERO);
	opt->name = strdup("ro", M_MOUNT);
	opt->value = NULL;
	TAILQ_INSERT_TAIL(opts, opt, link);
	vfs_mergeopts(opts, mp->mnt_opt);
	mp->mnt_optnew = opts;

	error = VFS_MOUNT(mp);

	if (error == 0) {
		MNT_ILOCK(mp);
		mp->mnt_flag &= ~(MNT_UPDATE | MNT_FORCE);
		MNT_IUNLOCK(mp);
		vfs_deallocate_syncvnode(mp);
		if (mp->mnt_opt != NULL)
			vfs_freeopts(mp->mnt_opt);
		mp->mnt_opt = mp->mnt_optnew;
	} else {
		MNT_ILOCK(mp);
		mp->mnt_flag &= ~(MNT_UPDATE | MNT_FORCE | MNT_RDONLY);
		MNT_IUNLOCK(mp);
		vfs_freeopts(mp->mnt_optnew);
	}
	mp->mnt_optnew = NULL;

out:
	vfs_op_exit(mp);
	VI_LOCK(vp_covered);
	vp_covered->v_iflag &= ~VI_MOUNT;
	VI_UNLOCK(vp_covered);
	vput(vp_covered);
	vn_seqc_write_end(vp_covered);
	if (rootvp != NULL) {
		vn_seqc_write_end(rootvp);
		vrele(rootvp);
	}
	return (error);
}

/*
 * Suspend write operations on all local writeable filesystems.  Does
 * full sync of them in the process.
 *
 * Iterate over the mount points in reverse order, suspending most
 * recently mounted filesystems first.  It handles a case where a
 * filesystem mounted from a md(4) vnode-backed device should be
 * suspended before the filesystem that owns the vnode.
 */
void
suspend_all_fs(void)
{
	struct mount *mp;
	int error;

	mtx_lock(&mountlist_mtx);
	TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
		error = vfs_busy(mp, MBF_MNTLSTLOCK | MBF_NOWAIT);
		if (error != 0)
			continue;
		if ((mp->mnt_flag & (MNT_RDONLY | MNT_LOCAL)) != MNT_LOCAL ||
		    (mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
			mtx_lock(&mountlist_mtx);
			vfs_unbusy(mp);
			continue;
		}
		error = vfs_write_suspend(mp, 0);
		if (error == 0) {
			MNT_ILOCK(mp);
			MPASS((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0);
			mp->mnt_kern_flag |= MNTK_SUSPEND_ALL;
			MNT_IUNLOCK(mp);
			mtx_lock(&mountlist_mtx);
		} else {
			printf("suspend of %s failed, error %d\n",
			    mp->mnt_stat.f_mntonname, error);
			mtx_lock(&mountlist_mtx);
			vfs_unbusy(mp);
		}
	}
	mtx_unlock(&mountlist_mtx);
}

void
resume_all_fs(void)
{
	struct mount *mp;

	mtx_lock(&mountlist_mtx);
	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
		if ((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0)
			continue;
		mtx_unlock(&mountlist_mtx);
		MNT_ILOCK(mp);
		MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) != 0);
		mp->mnt_kern_flag &= ~MNTK_SUSPEND_ALL;
		MNT_IUNLOCK(mp);
		vfs_write_resume(mp, 0);
		mtx_lock(&mountlist_mtx);
		vfs_unbusy(mp);
	}
	mtx_unlock(&mountlist_mtx);
}