DragonFlyBSD Kernel Audit
sys/vfs/hammer/hammer_inode.c
← back
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
/*
 * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@backplane.com>
 *
 * 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 DragonFly Project 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 COPYRIGHT HOLDERS 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
 * COPYRIGHT HOLDERS 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 <vm/vm_page2.h>

#include "hammer.h"

static int	hammer_unload_inode(hammer_inode_t ip);
static void	hammer_free_inode(hammer_inode_t ip);
static void	hammer_flush_inode_core(hammer_inode_t ip,
					hammer_flush_group_t flg, int flags);
static int	hammer_setup_child_callback(hammer_record_t rec, void *data);
#if 0
static int	hammer_syncgrp_child_callback(hammer_record_t rec, void *data);
#endif
static int	hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
					hammer_flush_group_t flg);
static int	hammer_setup_parent_inodes_helper(hammer_record_t record,
					int depth, hammer_flush_group_t flg);
static void	hammer_inode_wakereclaims(hammer_inode_t ip);
static struct hammer_inostats *hammer_inode_inostats(hammer_mount_t hmp,
					pid_t pid);
static hammer_inode_t __hammer_find_inode(hammer_transaction_t trans,
					int64_t obj_id, hammer_tid_t asof,
					uint32_t localization);

struct krate hammer_gen_krate = { 1 };

/*
 * RB-Tree support for inode structures
 */
int
hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
{
	if (ip1->obj_localization < ip2->obj_localization)
		return(-1);
	if (ip1->obj_localization > ip2->obj_localization)
		return(1);
	if (ip1->obj_id < ip2->obj_id)
		return(-1);
	if (ip1->obj_id > ip2->obj_id)
		return(1);
	if (ip1->obj_asof < ip2->obj_asof)
		return(-1);
	if (ip1->obj_asof > ip2->obj_asof)
		return(1);
	return(0);
}

int
hammer_redo_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
{
	if (ip1->redo_fifo_start < ip2->redo_fifo_start)
		return(-1);
	if (ip1->redo_fifo_start > ip2->redo_fifo_start)
		return(1);
	return(0);
}

/*
 * RB-Tree support for inode structures / special LOOKUP_INFO
 */
static int
hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
{
	if (info->obj_localization < ip->obj_localization)
		return(-1);
	if (info->obj_localization > ip->obj_localization)
		return(1);
	if (info->obj_id < ip->obj_id)
		return(-1);
	if (info->obj_id > ip->obj_id)
		return(1);
	if (info->obj_asof < ip->obj_asof)
		return(-1);
	if (info->obj_asof > ip->obj_asof)
		return(1);
	return(0);
}

/*
 * Used by hammer_scan_inode_snapshots() to locate all of an object's
 * snapshots.  Note that the asof field is not tested, which we can get
 * away with because it is the lowest-priority field.
 */
static int
hammer_inode_info_cmp_all_history(hammer_inode_t ip, void *data)
{
	hammer_inode_info_t info = data;

	if (ip->obj_localization > info->obj_localization)
		return(1);
	if (ip->obj_localization < info->obj_localization)
		return(-1);
	if (ip->obj_id > info->obj_id)
		return(1);
	if (ip->obj_id < info->obj_id)
		return(-1);
	return(0);
}

/*
 * Used by hammer_unload_pseudofs() to locate all inodes associated with
 * a particular PFS.
 */
static int
hammer_inode_pfs_cmp(hammer_inode_t ip, void *data)
{
	uint32_t localization = *(uint32_t *)data;
	if (ip->obj_localization > localization)
		return(1);
	if (ip->obj_localization < localization)
		return(-1);
	return(0);
}

/*
 * RB-Tree support for pseudofs structures
 */
static int
hammer_pfs_rb_compare(hammer_pseudofs_inmem_t p1, hammer_pseudofs_inmem_t p2)
{
	if (p1->localization < p2->localization)
		return(-1);
	if (p1->localization > p2->localization)
		return(1);
	return(0);
}


RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
		hammer_inode_info_cmp, hammer_inode_info_t);
RB_GENERATE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
             hammer_pfs_rb_compare, uint32_t, localization);

/*
 * The kernel is not actively referencing this vnode but is still holding
 * it cached.
 *
 * This is called from the frontend.
 *
 * MPALMOSTSAFE
 */
int
hammer_vop_inactive(struct vop_inactive_args *ap)
{
	hammer_inode_t ip = VTOI(ap->a_vp);
	hammer_mount_t hmp;

	/*
	 * Degenerate case
	 */
	if (ip == NULL) {
		vrecycle(ap->a_vp);
		return(0);
	}

	/*
	 * If the inode no longer has visibility in the filesystem try to
	 * recycle it immediately, even if the inode is dirty.  Recycling
	 * it quickly allows the system to reclaim buffer cache and VM
	 * resources which can matter a lot in a heavily loaded system.
	 *
	 * This can deadlock in vfsync() if we aren't careful.
	 *
	 * Do not queue the inode to the flusher if we still have visibility,
	 * otherwise namespace calls such as chmod will unnecessarily generate
	 * multiple inode updates.
	 */
	if (ip->ino_data.nlinks == 0) {
		hmp = ip->hmp;
		lwkt_gettoken(&hmp->fs_token);
		hammer_inode_unloadable_check(ip, 0);
		if (ip->flags & HAMMER_INODE_MODMASK)
			hammer_flush_inode(ip, 0);
		lwkt_reltoken(&hmp->fs_token);
		vrecycle(ap->a_vp);
	}
	return(0);
}

/*
 * Release the vnode association.  This is typically (but not always)
 * the last reference on the inode.
 *
 * Once the association is lost we are on our own with regards to
 * flushing the inode.
 *
 * We must interlock ip->vp so hammer_get_vnode() can avoid races.
 */
int
hammer_vop_reclaim(struct vop_reclaim_args *ap)
{
	hammer_inode_t ip;
	hammer_mount_t hmp;
	struct vnode *vp;

	vp = ap->a_vp;

	if ((ip = vp->v_data) != NULL) {
		hmp = ip->hmp;
		lwkt_gettoken(&hmp->fs_token);
		hammer_lock_ex(&ip->lock);
		vp->v_data = NULL;
		ip->vp = NULL;

		if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
			++hammer_count_reclaims;
			++hmp->count_reclaims;
			ip->flags |= HAMMER_INODE_RECLAIM;
		}
		hammer_unlock(&ip->lock);
		vclrisdirty(vp);
		hammer_rel_inode(ip, 1);
		lwkt_reltoken(&hmp->fs_token);
	}
	return(0);
}

/*
 * Inform the kernel that the inode is dirty.  This will be checked
 * by vn_unlock().
 *
 * Theoretically in order to reclaim a vnode the hammer_vop_reclaim()
 * must be called which will interlock against our inode lock, so
 * if VRECLAIMED is not set vp->v_mount (as used by vsetisdirty())
 * should be stable without having to acquire any new locks.
 */
void
hammer_inode_dirty(hammer_inode_t ip)
{
	struct vnode *vp;

	if ((ip->flags & HAMMER_INODE_MODMASK) &&
	    (vp = ip->vp) != NULL &&
	    (vp->v_flag & (VRECLAIMED | VISDIRTY)) == 0) {
		vsetisdirty(vp);
	}
}

/*
 * Return a locked vnode for the specified inode.  The inode must be
 * referenced but NOT LOCKED on entry and will remain referenced on
 * return.
 *
 * Called from the frontend.
 */
int
hammer_get_vnode(hammer_inode_t ip, struct vnode **vpp)
{
	hammer_mount_t hmp;
	struct vnode *vp;
	int error = 0;
	uint8_t obj_type;

	hmp = ip->hmp;

	for (;;) {
		if ((vp = ip->vp) == NULL) {
			error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
			if (error)
				break;
			hammer_lock_ex(&ip->lock);
			if (ip->vp != NULL) {
				hammer_unlock(&ip->lock);
				vp = *vpp;
				vp->v_type = VBAD;
				vx_put(vp);
				continue;
			}
			hammer_ref(&ip->lock);
			vp = *vpp;
			ip->vp = vp;

			obj_type = ip->ino_data.obj_type;
			vp->v_type = hammer_get_vnode_type(obj_type);

			hammer_inode_wakereclaims(ip);

			switch(ip->ino_data.obj_type) {
			case HAMMER_OBJTYPE_CDEV:
			case HAMMER_OBJTYPE_BDEV:
				vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
				addaliasu(vp, ip->ino_data.rmajor,
					  ip->ino_data.rminor);
				break;
			case HAMMER_OBJTYPE_FIFO:
				vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
				break;
			case HAMMER_OBJTYPE_REGFILE:
				break;
			default:
				break;
			}

			/*
			 * Only mark as the root vnode if the ip is not
			 * historical, otherwise the VFS cache will get
			 * confused.  The other half of the special handling
			 * is in hammer_vop_nlookupdotdot().
			 *
			 * Pseudo-filesystem roots can be accessed via
			 * non-root filesystem paths and setting VROOT may
			 * confuse the namecache.  Set VPFSROOT instead.
			 */
			if (ip->obj_id == HAMMER_OBJID_ROOT) {
				if (ip->obj_asof == hmp->asof) {
					if (ip->obj_localization ==
						HAMMER_DEF_LOCALIZATION)
						vsetflags(vp, VROOT);
					else
						vsetflags(vp, VPFSROOT);
				} else {
					vsetflags(vp, VPFSROOT);
				}
			}

			vp->v_data = (void *)ip;
			/* vnode locked by getnewvnode() */
			/* make related vnode dirty if inode dirty? */
			hammer_unlock(&ip->lock);
			if (vp->v_type == VREG) {
				vinitvmio(vp, ip->ino_data.size,
					  hammer_blocksize(ip->ino_data.size),
					  hammer_blockoff(ip->ino_data.size));
			}
			vx_downgrade(vp);
			break;
		}

		/*
		 * Interlock vnode clearing.  This does not prevent the
		 * vnode from going into a reclaimed state but it does
		 * prevent it from being destroyed or reused so the vget()
		 * will properly fail.
		 */
		hammer_lock_ex(&ip->lock);
		if ((vp = ip->vp) == NULL) {
			hammer_unlock(&ip->lock);
			continue;
		}
		vhold(vp);
		hammer_unlock(&ip->lock);

		/*
		 * loop if the vget fails (aka races), or if the vp
		 * no longer matches ip->vp.
		 */
		if (vget(vp, LK_EXCLUSIVE) == 0) {
			if (vp == ip->vp) {
				vdrop(vp);
				break;
			}
			vput(vp);
		}
		vdrop(vp);
	}
	*vpp = vp;
	return(error);
}

/*
 * Locate all copies of the inode for obj_id compatible with the specified
 * asof, reference, and issue the related call-back.  This routine is used
 * for direct-io invalidation and does not create any new inodes.
 */
void
hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
		            int (*callback)(hammer_inode_t ip, void *data),
			    void *data)
{
	hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
				   hammer_inode_info_cmp_all_history,
				   callback, iinfo);
}

/*
 * Acquire a HAMMER inode.  The returned inode is not locked.  These functions
 * do not attach or detach the related vnode (use hammer_get_vnode() for
 * that).
 *
 * The flags argument is only applied for newly created inodes, and only
 * certain flags are inherited.
 *
 * Called from the frontend.
 */
hammer_inode_t
hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
		 int64_t obj_id, hammer_tid_t asof, uint32_t localization,
		 int flags, int *errorp)
{
	hammer_mount_t hmp = trans->hmp;
	struct hammer_node_cache *cachep;
	struct hammer_cursor cursor;
	hammer_inode_t ip;


	/*
	 * Determine if we already have an inode cached.  If we do then
	 * we are golden.
	 *
	 * If we find an inode with no vnode we have to mark the
	 * transaction such that hammer_inode_waitreclaims() is
	 * called later on to avoid building up an infinite number
	 * of inodes.  Otherwise we can continue to * add new inodes
	 * faster then they can be disposed of, even with the tsleep
	 * delay.
	 *
	 * If we find a dummy inode we return a failure so dounlink
	 * (which does another lookup) doesn't try to mess with the
	 * link count.  hammer_vop_nresolve() uses hammer_get_dummy_inode()
	 * to ref dummy inodes.
	 */
loop:
	*errorp = 0;
	ip = __hammer_find_inode(trans, obj_id, asof, localization);
	if (ip) {
		if (ip->flags & HAMMER_INODE_DUMMY) {
			*errorp = ENOENT;
			return(NULL);
		}
		hammer_ref(&ip->lock);
		return(ip);
	}

	/*
	 * Allocate a new inode structure and deal with races later.
	 */
	ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
	++hammer_count_inodes;
	++hmp->count_inodes;
	ip->obj_id = obj_id;
	ip->obj_asof = asof;
	ip->obj_localization = localization;
	ip->hmp = hmp;
	ip->flags = flags & HAMMER_INODE_RO;
	ip->cache[0].ip = ip;
	ip->cache[1].ip = ip;
	ip->cache[2].ip = ip;
	ip->cache[3].ip = ip;
	if (hmp->ronly)
		ip->flags |= HAMMER_INODE_RO;
	ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
		HAMMER_MAX_KEY;
	RB_INIT(&ip->rec_tree);
	TAILQ_INIT(&ip->target_list);
	hammer_ref(&ip->lock);

	/*
	 * Locate the on-disk inode.  If this is a PFS root we always
	 * access the current version of the root inode and (if it is not
	 * a master) always access information under it with a snapshot
	 * TID.
	 *
	 * We cache recent inode lookups in this directory in dip->cache[2].
	 * If we can't find it we assume the inode we are looking for is
	 * close to the directory inode.
	 */
retry:
	cachep = NULL;
	if (dip) {
		if (dip->cache[2].node)
			cachep = &dip->cache[2];
		else
			cachep = &dip->cache[0];
	}
	hammer_init_cursor(trans, &cursor, cachep, NULL);
	cursor.key_beg.localization = localization | HAMMER_LOCALIZE_INODE;
	cursor.key_beg.obj_id = ip->obj_id;
	cursor.key_beg.key = 0;
	cursor.key_beg.create_tid = 0;
	cursor.key_beg.delete_tid = 0;
	cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
	cursor.key_beg.obj_type = 0;

	cursor.asof = asof;
	cursor.flags = HAMMER_CURSOR_GET_DATA | HAMMER_CURSOR_ASOF;

	*errorp = hammer_btree_lookup(&cursor);
	if (*errorp == EDEADLK) {
		hammer_done_cursor(&cursor);
		goto retry;
	}

	/*
	 * On success the B-Tree lookup will hold the appropriate
	 * buffer cache buffers and provide a pointer to the requested
	 * information.  Copy the information to the in-memory inode
	 * and cache the B-Tree node to improve future operations.
	 */
	if (*errorp == 0) {
		ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
		ip->ino_data = cursor.data->inode;

		/*
		 * cache[0] tries to cache the location of the object inode.
		 * The assumption is that it is near the directory inode.
		 *
		 * cache[1] tries to cache the location of the object data.
		 * We might have something in the governing directory from
		 * scan optimizations (see the strategy code in
		 * hammer_vnops.c).
		 *
		 * We update dip->cache[2], if possible, with the location
		 * of the object inode for future directory shortcuts.
		 */
		hammer_cache_node(&ip->cache[0], cursor.node);
		if (dip) {
			if (dip->cache[3].node) {
				hammer_cache_node(&ip->cache[1],
						  dip->cache[3].node);
			}
			hammer_cache_node(&dip->cache[2], cursor.node);
		}

		/*
		 * The file should not contain any data past the file size
		 * stored in the inode.  Setting save_trunc_off to the
		 * file size instead of max reduces B-Tree lookup overheads
		 * on append by allowing the flusher to avoid checking for
		 * record overwrites.
		 */
		ip->save_trunc_off = ip->ino_data.size;

		/*
		 * Locate and assign the pseudofs management structure to
		 * the inode.
		 */
		if (dip && dip->obj_localization == ip->obj_localization) {
			ip->pfsm = dip->pfsm;
			hammer_ref(&ip->pfsm->lock);
		} else {
			ip->pfsm = hammer_load_pseudofs(trans,
							ip->obj_localization,
							errorp);
			*errorp = 0;	/* ignore ENOENT */
		}
	}

	/*
	 * The inode is placed on the red-black tree and will be synced to
	 * the media when flushed or by the filesystem sync.  If this races
	 * another instantiation/lookup the insertion will fail.
	 */
	if (*errorp == 0) {
		if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
			hammer_free_inode(ip);
			hammer_done_cursor(&cursor);
			goto loop;
		}
		ip->flags |= HAMMER_INODE_ONDISK;
	} else {
		if (ip->flags & HAMMER_INODE_RSV_INODES) {
			ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
			--hmp->rsv_inodes;
		}

		hammer_free_inode(ip);
		ip = NULL;
	}
	hammer_done_cursor(&cursor);

	/*
	 * NEWINODE is only set if the inode becomes dirty later,
	 * setting it here just leads to unnecessary stalls.
	 *
	 * trans->flags |= HAMMER_TRANSF_NEWINODE;
	 */
	return (ip);
}

/*
 * Get a dummy inode to placemark a broken directory entry.
 */
hammer_inode_t
hammer_get_dummy_inode(hammer_transaction_t trans, hammer_inode_t dip,
		 int64_t obj_id, hammer_tid_t asof, uint32_t localization,
		 int flags, int *errorp)
{
	hammer_mount_t hmp = trans->hmp;
	hammer_inode_t ip;

	/*
	 * Determine if we already have an inode cached.  If we do then
	 * we are golden.
	 *
	 * If we find an inode with no vnode we have to mark the
	 * transaction such that hammer_inode_waitreclaims() is
	 * called later on to avoid building up an infinite number
	 * of inodes.  Otherwise we can continue to * add new inodes
	 * faster then they can be disposed of, even with the tsleep
	 * delay.
	 *
	 * If we find a non-fake inode we return an error.  Only fake
	 * inodes can be returned by this routine.
	 */
loop:
	*errorp = 0;
	ip = __hammer_find_inode(trans, obj_id, asof, localization);
	if (ip) {
		if ((ip->flags & HAMMER_INODE_DUMMY) == 0) {
			*errorp = ENOENT;
			return(NULL);
		}
		hammer_ref(&ip->lock);
		return(ip);
	}

	/*
	 * Allocate a new inode structure and deal with races later.
	 */
	ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
	++hammer_count_inodes;
	++hmp->count_inodes;
	ip->obj_id = obj_id;
	ip->obj_asof = asof;
	ip->obj_localization = localization;
	ip->hmp = hmp;
	ip->flags = flags | HAMMER_INODE_RO | HAMMER_INODE_DUMMY;
	ip->cache[0].ip = ip;
	ip->cache[1].ip = ip;
	ip->cache[2].ip = ip;
	ip->cache[3].ip = ip;
	ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
		HAMMER_MAX_KEY;
	RB_INIT(&ip->rec_tree);
	TAILQ_INIT(&ip->target_list);
	hammer_ref(&ip->lock);

	/*
	 * Populate the dummy inode.  Leave everything zero'd out.
	 *
	 * (ip->ino_leaf and ip->ino_data)
	 *
	 * Make the dummy inode a FIFO object which most copy programs
	 * will properly ignore.
	 */
	ip->save_trunc_off = ip->ino_data.size;
	ip->ino_data.obj_type = HAMMER_OBJTYPE_FIFO;

	/*
	 * Locate and assign the pseudofs management structure to
	 * the inode.
	 */
	if (dip && dip->obj_localization == ip->obj_localization) {
		ip->pfsm = dip->pfsm;
		hammer_ref(&ip->pfsm->lock);
	} else {
		ip->pfsm = hammer_load_pseudofs(trans, ip->obj_localization,
						errorp);
		*errorp = 0;	/* ignore ENOENT */
	}

	/*
	 * The inode is placed on the red-black tree and will be synced to
	 * the media when flushed or by the filesystem sync.  If this races
	 * another instantiation/lookup the insertion will fail.
	 *
	 * NOTE: Do not set HAMMER_INODE_ONDISK.  The inode is a fake.
	 */
	if (*errorp == 0) {
		if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
			hammer_free_inode(ip);
			goto loop;
		}
	} else {
		if (ip->flags & HAMMER_INODE_RSV_INODES) {
			ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
			--hmp->rsv_inodes;
		}
		hammer_free_inode(ip);
		ip = NULL;
	}
	trans->flags |= HAMMER_TRANSF_NEWINODE;
	return (ip);
}

/*
 * Return a referenced inode only if it is in our inode cache.
 * Dummy inodes do not count.
 */
hammer_inode_t
hammer_find_inode(hammer_transaction_t trans, int64_t obj_id,
		  hammer_tid_t asof, uint32_t localization)
{
	hammer_inode_t ip;

	ip = __hammer_find_inode(trans, obj_id, asof, localization);
	if (ip) {
		if (ip->flags & HAMMER_INODE_DUMMY)
			ip = NULL;
		else
			hammer_ref(&ip->lock);
	}
	return(ip);
}

/*
 * Return a referenced inode only if it is in our inode cache.
 * This function does not reference inode.
 */
static hammer_inode_t
__hammer_find_inode(hammer_transaction_t trans, int64_t obj_id,
		  hammer_tid_t asof, uint32_t localization)
{
	hammer_mount_t hmp = trans->hmp;
	struct hammer_inode_info iinfo;
	hammer_inode_t ip;

	iinfo.obj_id = obj_id;
	iinfo.obj_asof = asof;
	iinfo.obj_localization = localization;

	ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);

	return(ip);
}

/*
 * Create a new filesystem object, returning the inode in *ipp.  The
 * returned inode will be referenced.  The inode is created in-memory.
 *
 * If pfsm is non-NULL the caller wishes to create the root inode for
 * a non-root PFS.
 */
int
hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
		    struct ucred *cred,
		    hammer_inode_t dip, const char *name, int namelen,
		    hammer_pseudofs_inmem_t pfsm, hammer_inode_t *ipp)
{
	hammer_mount_t hmp;
	hammer_inode_t ip;
	uid_t xuid;
	int error;
	int64_t namekey;
	uint32_t dummy;

	hmp = trans->hmp;

	/*
	 * Disallow the creation of new inodes in directories which
	 * have been deleted.  In HAMMER, this will cause a record
	 * syncing assertion later on in the flush code.
	 */
	if (dip && dip->ino_data.nlinks == 0) {
		*ipp = NULL;
                return (EINVAL);
	}

	/*
	 * Allocate inode
	 */
	ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
	++hammer_count_inodes;
	++hmp->count_inodes;
	trans->flags |= HAMMER_TRANSF_NEWINODE;

	if (pfsm) {
		KKASSERT(pfsm->localization != HAMMER_DEF_LOCALIZATION);
		ip->obj_id = HAMMER_OBJID_ROOT;
		ip->obj_localization = pfsm->localization;
	} else {
		KKASSERT(dip != NULL);
		namekey = hammer_direntry_namekey(dip, name, namelen, &dummy);
		ip->obj_id = hammer_alloc_objid(hmp, dip, namekey);
		ip->obj_localization = dip->obj_localization;
	}

	KKASSERT(ip->obj_id != 0);
	ip->obj_asof = hmp->asof;
	ip->hmp = hmp;
	ip->flush_state = HAMMER_FST_IDLE;
	ip->flags = HAMMER_INODE_DDIRTY |
		    HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
	ip->cache[0].ip = ip;
	ip->cache[1].ip = ip;
	ip->cache[2].ip = ip;
	ip->cache[3].ip = ip;

	ip->trunc_off = HAMMER_MAX_KEY;
	/* ip->save_trunc_off = 0; (already zero) */
	RB_INIT(&ip->rec_tree);
	TAILQ_INIT(&ip->target_list);

	ip->ino_data.atime = trans->time;
	ip->ino_data.mtime = trans->time;
	ip->ino_data.size = 0;
	ip->ino_data.nlinks = 0;

	/*
	 * A nohistory designator on the parent directory is inherited by
	 * the child.  We will do this even for pseudo-fs creation... the
	 * sysad can turn it off.
	 */
	if (dip) {
		ip->ino_data.uflags = dip->ino_data.uflags &
				      (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
	}

	ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
	ip->ino_leaf.base.localization = ip->obj_localization |
					 HAMMER_LOCALIZE_INODE;
	ip->ino_leaf.base.obj_id = ip->obj_id;
	ip->ino_leaf.base.key = 0;
	ip->ino_leaf.base.create_tid = 0;
	ip->ino_leaf.base.delete_tid = 0;
	ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
	ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);

	ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
	ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
	ip->ino_data.mode = vap->va_mode;
	ip->ino_data.ctime = trans->time;

	/*
	 * If we are running version 2 or greater directory entries are
	 * inode-localized instead of data-localized.
	 */
	if (trans->hmp->version >= HAMMER_VOL_VERSION_TWO) {
		if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
			ip->ino_data.cap_flags |=
				HAMMER_INODE_CAP_DIR_LOCAL_INO;
		}
	}
	if (trans->hmp->version >= HAMMER_VOL_VERSION_SIX) {
		if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
			ip->ino_data.cap_flags |=
				HAMMER_INODE_CAP_DIRHASH_ALG1;
		}
	}

	/*
	 * Setup the ".." pointer.  This only needs to be done for directories
	 * but we do it for all objects as a recovery aid if dip exists.
	 * The inode is probably a PFS root if dip is NULL.
	 */
	if (dip)
		ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;

	switch(ip->ino_leaf.base.obj_type) {
	case HAMMER_OBJTYPE_CDEV:
	case HAMMER_OBJTYPE_BDEV:
		ip->ino_data.rmajor = vap->va_rmajor;
		ip->ino_data.rminor = vap->va_rminor;
		break;
	default:
		break;
	}

	/*
	 * Calculate default uid/gid and overwrite with information from
	 * the vap.
	 */
	if (dip) {
		xuid = hammer_to_unix_xid(&dip->ino_data.uid);
		xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
					     xuid, cred, &vap->va_mode);
	} else {
		xuid = 0;
	}
	ip->ino_data.mode = vap->va_mode;

	if (vap->va_vaflags & VA_UID_UUID_VALID)
		ip->ino_data.uid = vap->va_uid_uuid;
	else if (vap->va_uid != (uid_t)VNOVAL)
		hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
	else
		hammer_guid_to_uuid(&ip->ino_data.uid, xuid);

	if (vap->va_vaflags & VA_GID_UUID_VALID)
		ip->ino_data.gid = vap->va_gid_uuid;
	else if (vap->va_gid != (gid_t)VNOVAL)
		hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
	else if (dip)
		ip->ino_data.gid = dip->ino_data.gid;

	hammer_ref(&ip->lock);

	if (pfsm) {
		ip->pfsm = pfsm;
		hammer_ref(&pfsm->lock);
		error = 0;
	} else if (dip->obj_localization == ip->obj_localization) {
		ip->pfsm = dip->pfsm;
		hammer_ref(&ip->pfsm->lock);
		error = 0;
	} else {
		ip->pfsm = hammer_load_pseudofs(trans,
						ip->obj_localization,
						&error);
		error = 0;	/* ignore ENOENT */
	}

	if (error) {
		hammer_free_inode(ip);
		ip = NULL;
	} else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
		hpanic("duplicate obj_id %jx", (intmax_t)ip->obj_id);
		/* not reached */
		hammer_free_inode(ip);
	}
	*ipp = ip;
	return(error);
}

/*
 * Final cleanup / freeing of an inode structure
 */
static void
hammer_free_inode(hammer_inode_t ip)
{
	hammer_mount_t hmp;

	hmp = ip->hmp;
	KKASSERT(hammer_oneref(&ip->lock));
	hammer_uncache_node(&ip->cache[0]);
	hammer_uncache_node(&ip->cache[1]);
	hammer_uncache_node(&ip->cache[2]);
	hammer_uncache_node(&ip->cache[3]);
	hammer_inode_wakereclaims(ip);
	if (ip->objid_cache)
		hammer_clear_objid(ip);
	--hammer_count_inodes;
	--hmp->count_inodes;
	if (ip->pfsm) {
		hammer_rel_pseudofs(hmp, ip->pfsm);
		ip->pfsm = NULL;
	}
	kfree(ip, hmp->m_inodes);
}

/*
 * Retrieve pseudo-fs data.  NULL will never be returned.
 *
 * If an error occurs *errorp will be set and a default template is returned,
 * otherwise *errorp is set to 0.  Typically when an error occurs it will
 * be ENOENT.
 */
hammer_pseudofs_inmem_t
hammer_load_pseudofs(hammer_transaction_t trans,
		     uint32_t localization, int *errorp)
{
	hammer_mount_t hmp = trans->hmp;
	hammer_inode_t ip;
	hammer_pseudofs_inmem_t pfsm;
	struct hammer_cursor cursor;
	int bytes;

retry:
	pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
	if (pfsm) {
		hammer_ref(&pfsm->lock);
		*errorp = 0;
		return(pfsm);
	}

	/*
	 * PFS records are associated with the root inode (not the PFS root
	 * inode, but the real root).  Avoid an infinite recursion if loading
	 * the PFS for the real root.
	 */
	if (localization) {
		ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
				      HAMMER_MAX_TID,
				      HAMMER_DEF_LOCALIZATION, 0, errorp);
	} else {
		ip = NULL;
	}

	pfsm = kmalloc(sizeof(*pfsm), hmp->m_misc, M_WAITOK | M_ZERO);
	pfsm->localization = localization;
	pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
	pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;

	hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
	cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION |
				      HAMMER_LOCALIZE_MISC;
	cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
	cursor.key_beg.create_tid = 0;
	cursor.key_beg.delete_tid = 0;
	cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
	cursor.key_beg.obj_type = 0;
	cursor.key_beg.key = localization;
	cursor.asof = HAMMER_MAX_TID;
	cursor.flags |= HAMMER_CURSOR_ASOF;

	if (ip)
		*errorp = hammer_ip_lookup(&cursor);
	else
		*errorp = hammer_btree_lookup(&cursor);
	if (*errorp == 0) {
		*errorp = hammer_ip_resolve_data(&cursor);
		if (*errorp == 0) {
			if (hammer_is_pfs_deleted(&cursor.data->pfsd)) {
				*errorp = ENOENT;
			} else {
				bytes = cursor.leaf->data_len;
				if (bytes > sizeof(pfsm->pfsd))
					bytes = sizeof(pfsm->pfsd);
				bcopy(cursor.data, &pfsm->pfsd, bytes);
			}
		}
	}
	hammer_done_cursor(&cursor);

	pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
	hammer_ref(&pfsm->lock);
	if (ip)
		hammer_rel_inode(ip, 0);
	if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
		kfree(pfsm, hmp->m_misc);
		goto retry;
	}
	return(pfsm);
}

/*
 * Store pseudo-fs data.  The backend will automatically delete any prior
 * on-disk pseudo-fs data but we have to delete in-memory versions.
 */
int
hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
{
	struct hammer_cursor cursor;
	hammer_record_t record;
	hammer_inode_t ip;
	int error;

	/*
	 * PFS records are associated with the root inode (not the PFS root
	 * inode, but the real root).
	 */
	ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
			      HAMMER_DEF_LOCALIZATION, 0, &error);
retry:
	pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
	hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
	cursor.key_beg.localization = ip->obj_localization |
				      HAMMER_LOCALIZE_MISC;
	cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
	cursor.key_beg.create_tid = 0;
	cursor.key_beg.delete_tid = 0;
	cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
	cursor.key_beg.obj_type = 0;
	cursor.key_beg.key = pfsm->localization;
	cursor.asof = HAMMER_MAX_TID;
	cursor.flags |= HAMMER_CURSOR_ASOF;

	/*
	 * Replace any in-memory version of the record.
	 */
	error = hammer_ip_lookup(&cursor);
	if (error == 0 && hammer_cursor_inmem(&cursor)) {
		record = cursor.iprec;
		if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
			KKASSERT(cursor.deadlk_rec == NULL);
			hammer_ref(&record->lock);
			cursor.deadlk_rec = record;
			error = EDEADLK;
		} else {
			record->flags |= HAMMER_RECF_DELETED_FE;
			error = 0;
		}
	}

	/*
	 * Allocate replacement general record.  The backend flush will
	 * delete any on-disk version of the record.
	 */
	if (error == 0 || error == ENOENT) {
		record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
		record->type = HAMMER_MEM_RECORD_GENERAL;

		record->leaf.base.localization = ip->obj_localization |
						 HAMMER_LOCALIZE_MISC;
		record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
		record->leaf.base.key = pfsm->localization;
		record->leaf.data_len = sizeof(pfsm->pfsd);
		bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
		error = hammer_ip_add_record(trans, record);
	}
	hammer_done_cursor(&cursor);
	if (error == EDEADLK)
		goto retry;
	hammer_rel_inode(ip, 0);
	return(error);
}

/*
 * Create a root directory for a PFS if one does not alredy exist.
 *
 * The PFS root stands alone so we must also bump the nlinks count
 * to prevent it from being destroyed on release.
 *
 * Make sure a caller isn't creating a PFS from non-root PFS.
 */
int
hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
		       hammer_pseudofs_inmem_t pfsm, hammer_inode_t dip)
{
	hammer_inode_t ip;
	struct vattr vap;
	int error;

	ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
			      pfsm->localization, 0, &error);
	if (ip == NULL) {
		if (lo_to_pfs(dip->obj_localization) != HAMMER_ROOT_PFSID) {
			hmkprintf(trans->hmp,
				"Warning: creating a PFS from non-root PFS "
				"is not allowed\n");
			return(EINVAL);
		}
		vattr_null(&vap);
		vap.va_mode = 0755;
		vap.va_type = VDIR;
		error = hammer_create_inode(trans, &vap, cred,
					    NULL, NULL, 0,
					    pfsm, &ip);
		if (error == 0) {
			++ip->ino_data.nlinks;
			hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
		}
	}
	if (ip)
		hammer_rel_inode(ip, 0);
	return(error);
}

/*
 * Unload any vnodes & inodes associated with a PFS, return ENOTEMPTY
 * if we are unable to disassociate all the inodes.
 */
static
int
hammer_unload_pseudofs_callback(hammer_inode_t ip, void *data)
{
	int res;

	hammer_ref(&ip->lock);
	if (ip->vp && (ip->vp->v_flag & VPFSROOT)) {
		/*
		 * The hammer pfs-upgrade directive itself might have the
		 * root of the pfs open.  Just allow it.
		 */
		res = 0;
	} else {
		/*
		 * Don't allow any subdirectories or files to be open.
		 */
		if (hammer_isactive(&ip->lock) == 2 && ip->vp)
			vclean_unlocked(ip->vp);	/* might not succeed */
		if (hammer_isactive(&ip->lock) == 1 && ip->vp == NULL)
			res = 0;
		else
			res = -1;	/* stop, someone is using the inode */
	}
	hammer_rel_inode(ip, 0);
	return(res);
}

int
hammer_unload_pseudofs(hammer_transaction_t trans, uint32_t localization)
{
	int res;
	int try;

	for (try = res = 0; try < 4; ++try) {
		res = hammer_ino_rb_tree_RB_SCAN(&trans->hmp->rb_inos_root,
					   hammer_inode_pfs_cmp,
					   hammer_unload_pseudofs_callback,
					   &localization);
		if (res == 0 && try > 1)
			break;
		hammer_flusher_sync(trans->hmp);
	}
	if (res != 0)
		res = ENOTEMPTY;
	return(res);
}


/*
 * Release a reference on a PFS
 */
void
hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
{
	hammer_rel(&pfsm->lock);
	if (hammer_norefs(&pfsm->lock)) {
		RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
		kfree(pfsm, hmp->m_misc);
	}
}

/*
 * Called by hammer_sync_inode().
 */
static int
hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
{
	hammer_transaction_t trans = cursor->trans;
	hammer_record_t record;
	int error;
	int redirty;

retry:
	error = 0;

	/*
	 * If the inode has a presence on-disk then locate it and mark
	 * it deleted, setting DELONDISK.
	 *
	 * The record may or may not be physically deleted, depending on
	 * the retention policy.
	 */
	if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
	    HAMMER_INODE_ONDISK) {
		hammer_normalize_cursor(cursor);
		cursor->key_beg.localization = ip->obj_localization |
					       HAMMER_LOCALIZE_INODE;
		cursor->key_beg.obj_id = ip->obj_id;
		cursor->key_beg.key = 0;
		cursor->key_beg.create_tid = 0;
		cursor->key_beg.delete_tid = 0;
		cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
		cursor->key_beg.obj_type = 0;
		cursor->asof = ip->obj_asof;
		cursor->flags &= ~HAMMER_CURSOR_INITMASK;
		cursor->flags |= HAMMER_CURSOR_ASOF;
		cursor->flags |= HAMMER_CURSOR_BACKEND;

		error = hammer_btree_lookup(cursor);
		if (hammer_debug_inode)
			hdkprintf("IPDEL %p %08x %d\n", ip, ip->flags, error);

		if (error == 0) {
			error = hammer_ip_delete_record(cursor, ip, trans->tid);
			if (hammer_debug_inode)
				hdkprintf("error %d\n", error);
			if (error == 0) {
				ip->flags |= HAMMER_INODE_DELONDISK;
			}
			if (cursor->node)
				hammer_cache_node(&ip->cache[0], cursor->node);
		}
		if (error == EDEADLK) {
			hammer_done_cursor(cursor);
			error = hammer_init_cursor(trans, cursor,
						   &ip->cache[0], ip);
			if (hammer_debug_inode)
				hdkprintf("IPDED %p %d\n", ip, error);
			if (error == 0)
				goto retry;
		}
	}

	/*
	 * Ok, write out the initial record or a new record (after deleting
	 * the old one), unless the DELETED flag is set.  This routine will
	 * clear DELONDISK if it writes out a record.
	 *
	 * Update our inode statistics if this is the first application of
	 * the inode on-disk.
	 */
	if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
		/*
		 * Generate a record and write it to the media.  We clean-up
		 * the state before releasing so we do not have to set-up
		 * a flush_group.
		 */
		record = hammer_alloc_mem_record(ip, 0);
		record->type = HAMMER_MEM_RECORD_INODE;
		record->flush_state = HAMMER_FST_FLUSH;
		record->leaf = ip->sync_ino_leaf;
		record->leaf.base.create_tid = trans->tid;
		record->leaf.data_len = sizeof(ip->sync_ino_data);
		record->leaf.create_ts = trans->time32;
		record->data = (void *)&ip->sync_ino_data;
		record->flags |= HAMMER_RECF_INTERLOCK_BE;

		/*
		 * If this flag is set we cannot sync the new file size
		 * because we haven't finished related truncations.  The
		 * inode will be flushed in another flush group to finish
		 * the job.
		 */
		if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
		    ip->sync_ino_data.size != ip->ino_data.size) {
			redirty = 1;
			ip->sync_ino_data.size = ip->ino_data.size;
		} else {
			redirty = 0;
		}

		for (;;) {
			error = hammer_ip_sync_record_cursor(cursor, record);
			if (hammer_debug_inode)
				hdkprintf("GENREC %p rec %08x %d\n",
					ip, record->flags, error);
			if (error != EDEADLK)
				break;
			hammer_done_cursor(cursor);
			error = hammer_init_cursor(trans, cursor,
						   &ip->cache[0], ip);
			if (hammer_debug_inode)
				hdkprintf("GENREC reinit %d\n", error);
			if (error)
				break;
		}

		/*
		 * Note:  The record was never on the inode's record tree
		 * so just wave our hands importantly and destroy it.
		 */
		record->flags |= HAMMER_RECF_COMMITTED;
		record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
		record->flush_state = HAMMER_FST_IDLE;
		++ip->rec_generation;
		hammer_rel_mem_record(record);

		/*
		 * Finish up.
		 */
		if (error == 0) {
			if (hammer_debug_inode)
				hdkprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
			ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
					    HAMMER_INODE_SDIRTY |
					    HAMMER_INODE_ATIME |
					    HAMMER_INODE_MTIME);
			ip->flags &= ~HAMMER_INODE_DELONDISK;
			if (redirty)
				ip->sync_flags |= HAMMER_INODE_DDIRTY;

			/*
			 * Root volume count of inodes
			 */
			hammer_sync_lock_sh(trans);
			if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
				hammer_modify_volume_field(trans,
							   trans->rootvol,
							   vol0_stat_inodes);
				++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
				hammer_modify_volume_done(trans->rootvol);
				ip->flags |= HAMMER_INODE_ONDISK;
				if (hammer_debug_inode)
					hdkprintf("NOWONDISK %p\n", ip);
			}
			hammer_sync_unlock(trans);
		}
	}

	/*
	 * If the inode has been destroyed, clean out any left-over flags
	 * that may have been set by the frontend.
	 */
	if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
		ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
				    HAMMER_INODE_SDIRTY |
				    HAMMER_INODE_ATIME |
				    HAMMER_INODE_MTIME);
	}
	return(error);
}

/*
 * Update only the itimes fields.
 *
 * ATIME can be updated without generating any UNDO.  MTIME is updated
 * with UNDO so it is guaranteed to be synchronized properly in case of
 * a crash.
 *
 * Neither field is included in the B-Tree leaf element's CRC, which is how
 * we can get away with updating ATIME the way we do.
 */
static int
hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
{
	hammer_transaction_t trans = cursor->trans;
	int error;

retry:
	if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
	    HAMMER_INODE_ONDISK) {
		return(0);
	}

	hammer_normalize_cursor(cursor);
	cursor->key_beg.localization = ip->obj_localization |
				       HAMMER_LOCALIZE_INODE;
	cursor->key_beg.obj_id = ip->obj_id;
	cursor->key_beg.key = 0;
	cursor->key_beg.create_tid = 0;
	cursor->key_beg.delete_tid = 0;
	cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
	cursor->key_beg.obj_type = 0;
	cursor->asof = ip->obj_asof;
	cursor->flags &= ~HAMMER_CURSOR_INITMASK;
	cursor->flags |= HAMMER_CURSOR_ASOF;
	cursor->flags |= HAMMER_CURSOR_GET_DATA;
	cursor->flags |= HAMMER_CURSOR_BACKEND;

	error = hammer_btree_lookup(cursor);
	if (error == 0) {
		hammer_cache_node(&ip->cache[0], cursor->node);
		if (ip->sync_flags & HAMMER_INODE_MTIME) {
			/*
			 * Updating MTIME requires an UNDO.  Just cover
			 * both atime and mtime.
			 */
			hammer_sync_lock_sh(trans);
			hammer_modify_buffer(trans, cursor->data_buffer,
				&cursor->data->inode.mtime,
				sizeof(cursor->data->inode.atime) +
				sizeof(cursor->data->inode.mtime));
			cursor->data->inode.atime = ip->sync_ino_data.atime;
			cursor->data->inode.mtime = ip->sync_ino_data.mtime;
			hammer_modify_buffer_done(cursor->data_buffer);
			hammer_sync_unlock(trans);
		} else if (ip->sync_flags & HAMMER_INODE_ATIME) {
			/*
			 * Updating atime only can be done in-place with
			 * no UNDO.
			 */
			hammer_sync_lock_sh(trans);
			hammer_modify_buffer_noundo(trans, cursor->data_buffer);
			cursor->data->inode.atime = ip->sync_ino_data.atime;
			hammer_modify_buffer_done(cursor->data_buffer);
			hammer_sync_unlock(trans);
		}
		ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
	}
	if (error == EDEADLK) {
		hammer_done_cursor(cursor);
		error = hammer_init_cursor(trans, cursor, &ip->cache[0], ip);
		if (error == 0)
			goto retry;
	}
	return(error);
}

/*
 * Release a reference on an inode, flush as requested.
 *
 * On the last reference we queue the inode to the flusher for its final
 * disposition.
 */
void
hammer_rel_inode(hammer_inode_t ip, int flush)
{
	/*
	 * Handle disposition when dropping the last ref.
	 */
	for (;;) {
		if (hammer_oneref(&ip->lock)) {
			/*
			 * Determine whether on-disk action is needed for
			 * the inode's final disposition.
			 */
			KKASSERT(ip->vp == NULL);
			hammer_inode_unloadable_check(ip, 0);
			if (ip->flags & HAMMER_INODE_MODMASK) {
				hammer_flush_inode(ip, 0);
			} else if (hammer_oneref(&ip->lock)) {
				hammer_unload_inode(ip);
				break;
			}
		} else {
			if (flush)
				hammer_flush_inode(ip, 0);

			/*
			 * The inode still has multiple refs, try to drop
			 * one ref.
			 */
			KKASSERT(hammer_isactive(&ip->lock) >= 1);
			if (hammer_isactive(&ip->lock) > 1) {
				hammer_rel(&ip->lock);
				break;
			}
		}
	}
}

/*
 * Unload and destroy the specified inode.  Must be called with one remaining
 * reference.  The reference is disposed of.
 *
 * The inode must be completely clean.
 */
static int
hammer_unload_inode(hammer_inode_t ip)
{
	hammer_mount_t hmp = ip->hmp;

	KASSERT(hammer_oneref(&ip->lock),
		("hammer_unload_inode: %d refs", hammer_isactive(&ip->lock)));
	KKASSERT(ip->vp == NULL);
	KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
	KKASSERT(ip->cursor_ip_refs == 0);
	KKASSERT(hammer_notlocked(&ip->lock));
	KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);

	KKASSERT(RB_EMPTY(&ip->rec_tree));
	KKASSERT(TAILQ_EMPTY(&ip->target_list));

	if (ip->flags & HAMMER_INODE_RDIRTY) {
		RB_REMOVE(hammer_redo_rb_tree, &hmp->rb_redo_root, ip);
		ip->flags &= ~HAMMER_INODE_RDIRTY;
	}
	RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);

	hammer_free_inode(ip);
	return(0);
}

/*
 * Called during unmounting if a critical error occured.  The in-memory
 * inode and all related structures are destroyed.
 *
 * If a critical error did not occur the unmount code calls the standard
 * release and asserts that the inode is gone.
 */
int
hammer_destroy_inode_callback(hammer_inode_t ip, void *data __unused)
{
	hammer_record_t rec;

	/*
	 * Get rid of the inodes in-memory records, regardless of their
	 * state, and clear the mod-mask.
	 */
	while ((rec = TAILQ_FIRST(&ip->target_list)) != NULL) {
		TAILQ_REMOVE(&ip->target_list, rec, target_entry);
		rec->target_ip = NULL;
		if (rec->flush_state == HAMMER_FST_SETUP)
			rec->flush_state = HAMMER_FST_IDLE;
	}
	while ((rec = RB_ROOT(&ip->rec_tree)) != NULL) {
		if (rec->flush_state == HAMMER_FST_FLUSH)
			--rec->flush_group->refs;
		else
			hammer_ref(&rec->lock);
		KKASSERT(hammer_oneref(&rec->lock));
		rec->flush_state = HAMMER_FST_IDLE;
		rec->flush_group = NULL;
		rec->flags |= HAMMER_RECF_DELETED_FE; /* wave hands */
		rec->flags |= HAMMER_RECF_DELETED_BE; /* wave hands */
		++ip->rec_generation;
		hammer_rel_mem_record(rec);
	}
	ip->flags &= ~HAMMER_INODE_MODMASK;
	ip->sync_flags &= ~HAMMER_INODE_MODMASK;
	KKASSERT(ip->vp == NULL);

	/*
	 * Remove the inode from any flush group, force it idle.  FLUSH
	 * and SETUP states have an inode ref.
	 */
	switch(ip->flush_state) {
	case HAMMER_FST_FLUSH:
		RB_REMOVE(hammer_fls_rb_tree, &ip->flush_group->flush_tree, ip);
		--ip->flush_group->refs;
		ip->flush_group = NULL;
		/* fall through */
	case HAMMER_FST_SETUP:
		hammer_rel(&ip->lock);
		ip->flush_state = HAMMER_FST_IDLE;
		/* fall through */
	case HAMMER_FST_IDLE:
		break;
	}

	/*
	 * There shouldn't be any associated vnode.  The unload needs at
	 * least one ref, if we do have a vp steal its ip ref.
	 */
	if (ip->vp) {
		hdkprintf("Unexpected vnode association ip %p vp %p\n",
			ip, ip->vp);
		ip->vp->v_data = NULL;
		ip->vp = NULL;
	} else {
		hammer_ref(&ip->lock);
	}
	hammer_unload_inode(ip);
	return(0);
}

/*
 * Called on mount -u when switching from RW to RO or vise-versa.  Adjust
 * the read-only flag for cached inodes.
 *
 * This routine is called from a RB_SCAN().
 */
int
hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
{
	hammer_mount_t hmp = ip->hmp;

	if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
		ip->flags |= HAMMER_INODE_RO;
	else
		ip->flags &= ~HAMMER_INODE_RO;
	return(0);
}

/*
 * A transaction has modified an inode, requiring updates as specified by
 * the passed flags.
 *
 * HAMMER_INODE_DDIRTY: Inode data has been updated, not incl mtime/atime,
 *			and not including size changes due to write-append
 *			(but other size changes are included).
 * HAMMER_INODE_SDIRTY: Inode data has been updated, size changes due to
 *			write-append.
 * HAMMER_INODE_XDIRTY: Dirty in-memory records
 * HAMMER_INODE_BUFS:   Dirty buffer cache buffers
 * HAMMER_INODE_DELETED: Inode record/data must be deleted
 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
 */
void
hammer_modify_inode(hammer_transaction_t trans, hammer_inode_t ip, int flags)
{
	/*
	 * ronly of 0 or 2 does not trigger assertion.
	 * 2 is a special error state
	 */
	KKASSERT(ip->hmp->ronly != 1 ||
		  (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
			    HAMMER_INODE_SDIRTY |
			    HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
			    HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
	if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
		ip->flags |= HAMMER_INODE_RSV_INODES;
		++ip->hmp->rsv_inodes;
	}

	/*
	 * Set the NEWINODE flag in the transaction if the inode
	 * transitions to a dirty state.  This is used to track
	 * the load on the inode cache.
	 */
	if (trans &&
	    (ip->flags & HAMMER_INODE_MODMASK) == 0 &&
	    (flags & HAMMER_INODE_MODMASK)) {
		trans->flags |= HAMMER_TRANSF_NEWINODE;
	}
	if (flags & HAMMER_INODE_MODMASK)
		hammer_inode_dirty(ip);
	ip->flags |= flags;
}

/*
 * Attempt to quickly update the atime for a hammer inode.  Return 0 on
 * success, -1 on failure.
 *
 * We attempt to update the atime with only the ip lock and not the
 * whole filesystem lock in order to improve concurrency.  We can only
 * do this safely if the ATIME flag is already pending on the inode.
 *
 * This function is called via a vnops path (ip pointer is stable) without
 * fs_token held.
 */
int
hammer_update_atime_quick(hammer_inode_t ip)
{
	struct timespec ts;
	int res = -1;

	if ((ip->flags & HAMMER_INODE_RO) ||
	    (ip->hmp->mp->mnt_flag & MNT_NOATIME)) {
		/*
		 * Silently indicate success on read-only mount/snap
		 */
		res = 0;
	} else if (ip->flags & HAMMER_INODE_ATIME) {
		/*
		 * Double check with inode lock held against backend.  This
		 * is only safe if all we need to do is update
		 * ino_data.atime.
		 */
		vfs_timestamp(&ts);
		hammer_lock_ex(&ip->lock);
		if (ip->flags & HAMMER_INODE_ATIME) {
			ip->ino_data.atime =
			    (unsigned long)ts.tv_sec * 1000000ULL +
			    ts.tv_nsec / 1000;
			res = 0;
		}
		hammer_unlock(&ip->lock);
	}
	return res;
}

/*
 * Request that an inode be flushed.  This whole mess cannot block and may
 * recurse (if not synchronous).  Once requested HAMMER will attempt to
 * actively flush the inode until the flush can be done.
 *
 * The inode may already be flushing, or may be in a setup state.  We can
 * place the inode in a flushing state if it is currently idle and flag it
 * to reflush if it is currently flushing.
 *
 * Upon return if the inode could not be flushed due to a setup
 * dependancy, then it will be automatically flushed when the dependancy
 * is satisfied.
 */
void
hammer_flush_inode(hammer_inode_t ip, int flags)
{
	hammer_mount_t hmp;
	hammer_flush_group_t flg;
	int good;

	/*
	 * fill_flush_group is the first flush group we may be able to
	 * continue filling, it may be open or closed but it will always
	 * be past the currently flushing (running) flg.
	 *
	 * next_flush_group is the next open flush group.
	 */
	hmp = ip->hmp;
	while ((flg = hmp->fill_flush_group) != NULL) {
		KKASSERT(flg->running == 0);
		if (flg->total_count + flg->refs <= ip->hmp->undo_rec_limit &&
		    flg->total_count <= hammer_autoflush) {
			break;
		}
		hmp->fill_flush_group = TAILQ_NEXT(flg, flush_entry);
		hammer_flusher_async(ip->hmp, flg);
	}
	if (flg == NULL) {
		flg = kmalloc(sizeof(*flg), hmp->m_misc, M_WAITOK|M_ZERO);
		flg->seq = hmp->flusher.next++;
		if (hmp->next_flush_group == NULL)
			hmp->next_flush_group = flg;
		if (hmp->fill_flush_group == NULL)
			hmp->fill_flush_group = flg;
		RB_INIT(&flg->flush_tree);
		TAILQ_INSERT_TAIL(&hmp->flush_group_list, flg, flush_entry);
	}

	/*
	 * Trivial 'nothing to flush' case.  If the inode is in a SETUP
	 * state we have to put it back into an IDLE state so we can
	 * drop the extra ref.
	 *
	 * If we have a parent dependancy we must still fall through
	 * so we can run it.
	 */
	if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
		if (ip->flush_state == HAMMER_FST_SETUP &&
		    TAILQ_EMPTY(&ip->target_list)) {
			ip->flush_state = HAMMER_FST_IDLE;
			hammer_rel_inode(ip, 0);
		}
		if (ip->flush_state == HAMMER_FST_IDLE)
			return;
	}

	/*
	 * Our flush action will depend on the current state.
	 */
	switch(ip->flush_state) {
	case HAMMER_FST_IDLE:
		/*
		 * We have no dependancies and can flush immediately.  Some
		 * our children may not be flushable so we have to re-test
		 * with that additional knowledge.
		 */
		hammer_flush_inode_core(ip, flg, flags);
		break;
	case HAMMER_FST_SETUP:
		/*
		 * Recurse upwards through dependancies via target_list
		 * and start their flusher actions going if possible.
		 *
		 * 'good' is our connectivity.  -1 means we have none and
		 * can't flush, 0 means there weren't any dependancies, and
		 * 1 means we have good connectivity.
		 */
		good = hammer_setup_parent_inodes(ip, 0, flg);

		if (good >= 0) {
			/*
			 * We can continue if good >= 0.  Determine how
			 * many records under our inode can be flushed (and
			 * mark them).
			 */
			hammer_flush_inode_core(ip, flg, flags);
		} else {
			/*
			 * Parent has no connectivity, tell it to flush
			 * us as soon as it does.
			 *
			 * The REFLUSH flag is also needed to trigger
			 * dependancy wakeups.
			 */
			ip->flags |= HAMMER_INODE_CONN_DOWN |
				     HAMMER_INODE_REFLUSH;
			if (flags & HAMMER_FLUSH_SIGNAL) {
				ip->flags |= HAMMER_INODE_RESIGNAL;
				hammer_flusher_async(ip->hmp, flg);
			}
		}
		break;
	case HAMMER_FST_FLUSH:
		/*
		 * We are already flushing, flag the inode to reflush
		 * if needed after it completes its current flush.
		 *
		 * The REFLUSH flag is also needed to trigger
		 * dependancy wakeups.
		 */
		if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
			ip->flags |= HAMMER_INODE_REFLUSH;
		if (flags & HAMMER_FLUSH_SIGNAL) {
			ip->flags |= HAMMER_INODE_RESIGNAL;
			hammer_flusher_async(ip->hmp, flg);
		}
		break;
	}
}

/*
 * Scan ip->target_list, which is a list of records owned by PARENTS to our
 * ip which reference our ip.
 *
 * XXX This is a huge mess of recursive code, but not one bit of it blocks
 *     so for now do not ref/deref the structures.  Note that if we use the
 *     ref/rel code later, the rel CAN block.
 */
static int
hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
			   hammer_flush_group_t flg)
{
	hammer_record_t depend;
	int good;
	int r;

	/*
	 * If we hit our recursion limit and we have parent dependencies
	 * We cannot continue.  Returning < 0 will cause us to be flagged
	 * for reflush.  Returning -2 cuts off additional dependency checks
	 * because they are likely to also hit the depth limit.
	 *
	 * We cannot return < 0 if there are no dependencies or there might
	 * not be anything to wakeup (ip).
	 */
	if (depth == 20 && TAILQ_FIRST(&ip->target_list)) {
		if (hammer_debug_general & 0x10000)
			hkrateprintf(&hammer_gen_krate,
			    "Warning: depth limit reached on "
			    "setup recursion, inode %p %016jx\n",
			    ip, (intmax_t)ip->obj_id);
		return(-2);
	}

	/*
	 * Scan dependencies
	 */
	good = 0;
	TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
		r = hammer_setup_parent_inodes_helper(depend, depth, flg);
		KKASSERT(depend->target_ip == ip);
		if (r < 0 && good == 0)
			good = -1;
		if (r > 0)
			good = 1;

		/*
		 * If we failed due to the recursion depth limit then stop
		 * now.
		 */
		if (r == -2)
			break;
	}
	return(good);
}

/*
 * This helper function takes a record representing the dependancy between
 * the parent inode and child inode.
 *
 * record		= record in question (*rec in below)
 * record->ip		= parent inode (*pip in below)
 * record->target_ip	= child inode (*ip in below)
 *
 * *pip--------------\
 *    ^               \rec_tree
 *     \               \
 *      \ip            /\\\\\ rbtree of recs from parent inode's view
 *       \            //\\\\\\
 *        \          / ........
 *         \        /
 *          \------*rec------target_ip------>*ip
 *               ...target_entry<----...----->target_list<---...
 *                                            list of recs from inode's view
 *
 * We are asked to recurse upwards and convert the record from SETUP
 * to FLUSH if possible.
 *
 * Return 1 if the record gives us connectivity
 *
 * Return 0 if the record is not relevant
 *
 * Return -1 if we can't resolve the dependancy and there is no connectivity.
 */
static int
hammer_setup_parent_inodes_helper(hammer_record_t record, int depth,
				  hammer_flush_group_t flg)
{
	hammer_inode_t pip;
	int good;

	KKASSERT(record->flush_state != HAMMER_FST_IDLE);
	pip = record->ip;

	/*
	 * If the record is already flushing, is it in our flush group?
	 *
	 * If it is in our flush group but it is a general record or a
	 * delete-on-disk, it does not improve our connectivity (return 0),
	 * and if the target inode is not trying to destroy itself we can't
	 * allow the operation yet anyway (the second return -1).
	 */
	if (record->flush_state == HAMMER_FST_FLUSH) {
		/*
		 * If not in our flush group ask the parent to reflush
		 * us as soon as possible.
		 */
		if (record->flush_group != flg) {
			pip->flags |= HAMMER_INODE_REFLUSH;
			record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
			return(-1);
		}

		/*
		 * If in our flush group everything is already set up,
		 * just return whether the record will improve our
		 * visibility or not.
		 */
		if (record->type == HAMMER_MEM_RECORD_ADD)
			return(1);
		return(0);
	}

	/*
	 * It must be a setup record.  Try to resolve the setup dependancies
	 * by recursing upwards so we can place ip on the flush list.
	 *
	 * Limit ourselves to 20 levels of recursion to avoid blowing out
	 * the kernel stack.  If we hit the recursion limit we can't flush
	 * until the parent flushes.  The parent will flush independantly
	 * on its own and ultimately a deep recursion will be resolved.
	 */
	KKASSERT(record->flush_state == HAMMER_FST_SETUP);

	good = hammer_setup_parent_inodes(pip, depth + 1, flg);

	/*
	 * If good < 0 the parent has no connectivity and we cannot safely
	 * flush the directory entry, which also means we can't flush our
	 * ip.  Flag us for downward recursion once the parent's
	 * connectivity is resolved.  Flag the parent for [re]flush or it
	 * may not check for downward recursions.
	 */
	if (good < 0) {
		pip->flags |= HAMMER_INODE_REFLUSH;
		record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
		return(good);
	}

	/*
	 * We are go, place the parent inode in a flushing state so we can
	 * place its record in a flushing state.  Note that the parent
	 * may already be flushing.  The record must be in the same flush
	 * group as the parent.
	 */
	if (pip->flush_state != HAMMER_FST_FLUSH)
		hammer_flush_inode_core(pip, flg, HAMMER_FLUSH_RECURSION);
	KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);

	/*
	 * It is possible for a rename to create a loop in the recursion
	 * and revisit a record.  This will result in the record being
	 * placed in a flush state unexpectedly.  This check deals with
	 * the case.
	 */
	if (record->flush_state == HAMMER_FST_FLUSH) {
		if (record->type == HAMMER_MEM_RECORD_ADD)
			return(1);
		return(0);
	}

	KKASSERT(record->flush_state == HAMMER_FST_SETUP);

#if 0
	if (record->type == HAMMER_MEM_RECORD_DEL &&
	    (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
		/*
		 * Regardless of flushing state we cannot sync this path if the
		 * record represents a delete-on-disk but the target inode
		 * is not ready to sync its own deletion.
		 *
		 * XXX need to count effective nlinks to determine whether
		 * the flush is ok, otherwise removing a hardlink will
		 * just leave the DEL record to rot.
		 */
		record->target_ip->flags |= HAMMER_INODE_REFLUSH;
		return(-1);
	} else
#endif
	if (pip->flush_group == flg) {
		/*
		 * Because we have not calculated nlinks yet we can just
		 * set records to the flush state if the parent is in
		 * the same flush group as we are.
		 */
		record->flush_state = HAMMER_FST_FLUSH;
		record->flush_group = flg;
		++record->flush_group->refs;
		hammer_ref(&record->lock);

		/*
		 * A general directory-add contributes to our visibility.
		 *
		 * Otherwise it is probably a directory-delete or
		 * delete-on-disk record and does not contribute to our
		 * visibility (but we can still flush it).
		 */
		if (record->type == HAMMER_MEM_RECORD_ADD)
			return(1);
		return(0);
	} else {
		/*
		 * If the parent is not in our flush group we cannot
		 * flush this record yet, there is no visibility.
		 * We tell the parent to reflush and mark ourselves
		 * so the parent knows it should flush us too.
		 */
		pip->flags |= HAMMER_INODE_REFLUSH;
		record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
		return(-1);
	}
}

/*
 * This is the core routine placing an inode into the FST_FLUSH state.
 */
static void
hammer_flush_inode_core(hammer_inode_t ip, hammer_flush_group_t flg, int flags)
{
	hammer_mount_t hmp = ip->hmp;
	int go_count;

	/*
	 * Set flush state and prevent the flusher from cycling into
	 * the next flush group.  Do not place the ip on the list yet.
	 * Inodes not in the idle state get an extra reference.
	 */
	KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
	if (ip->flush_state == HAMMER_FST_IDLE)
		hammer_ref(&ip->lock);
	ip->flush_state = HAMMER_FST_FLUSH;
	ip->flush_group = flg;
	++hmp->flusher.group_lock;
	++hmp->count_iqueued;
	++hammer_count_iqueued;
	++flg->total_count;
	hammer_redo_fifo_start_flush(ip);

#if 0
	/*
	 * We need to be able to vfsync/truncate from the backend.
	 *
	 * XXX Any truncation from the backend will acquire the vnode
	 *     independently.
	 */
	KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
	if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
		ip->flags |= HAMMER_INODE_VHELD;
		vref(ip->vp);
	}
#endif

	/*
	 * Figure out how many in-memory records we can actually flush
	 * (not including inode meta-data, buffers, etc).
	 */
	KKASSERT((ip->flags & HAMMER_INODE_WOULDBLOCK) == 0);
	if (flags & HAMMER_FLUSH_RECURSION) {
		/*
		 * If this is a upwards recursion we do not want to
		 * recurse down again!
		 */
		go_count = 1;
#if 0
	} else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
		/*
		 * No new records are added if we must complete a flush
		 * from a previous cycle, but we do have to move the records
		 * from the previous cycle to the current one.
		 */
#if 0
		go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
				   hammer_syncgrp_child_callback, NULL);
#endif
		go_count = 1;
#endif
	} else {
		/*
		 * Normal flush, scan records and bring them into the flush.
		 * Directory adds and deletes are usually skipped (they are
		 * grouped with the related inode rather then with the
		 * directory).
		 *
		 * go_count can be negative, which means the scan aborted
		 * due to the flush group being over-full and we should
		 * flush what we have.
		 */
		go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
				   hammer_setup_child_callback, NULL);
	}

	/*
	 * This is a more involved test that includes go_count.  If we
	 * can't flush, flag the inode and return.  If go_count is 0 we
	 * were are unable to flush any records in our rec_tree and
	 * must ignore the XDIRTY flag.
	 */
	if (go_count == 0) {
		if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
			--hmp->count_iqueued;
			--hammer_count_iqueued;

			--flg->total_count;
			ip->flush_state = HAMMER_FST_SETUP;
			ip->flush_group = NULL;
			if (flags & HAMMER_FLUSH_SIGNAL) {
				ip->flags |= HAMMER_INODE_REFLUSH |
					     HAMMER_INODE_RESIGNAL;
			} else {
				ip->flags |= HAMMER_INODE_REFLUSH;
			}
#if 0
			if (ip->flags & HAMMER_INODE_VHELD) {
				ip->flags &= ~HAMMER_INODE_VHELD;
				vrele(ip->vp);
			}
#endif

			/*
			 * REFLUSH is needed to trigger dependancy wakeups
			 * when an inode is in SETUP.
			 */
			ip->flags |= HAMMER_INODE_REFLUSH;
			if (--hmp->flusher.group_lock == 0)
				wakeup(&hmp->flusher.group_lock);
			return;
		}
	}

	/*
	 * Snapshot the state of the inode for the backend flusher.
	 *
	 * We continue to retain save_trunc_off even when all truncations
	 * have been resolved as an optimization to determine if we can
	 * skip the B-Tree lookup for overwrite deletions.
	 *
	 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
	 * and stays in ip->flags.  Once set, it stays set until the
	 * inode is destroyed.
	 */
	if (ip->flags & HAMMER_INODE_TRUNCATED) {
		KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
		ip->sync_trunc_off = ip->trunc_off;
		ip->trunc_off = HAMMER_MAX_KEY;
		ip->flags &= ~HAMMER_INODE_TRUNCATED;
		ip->sync_flags |= HAMMER_INODE_TRUNCATED;

		/*
		 * The save_trunc_off used to cache whether the B-Tree
		 * holds any records past that point is not used until
		 * after the truncation has succeeded, so we can safely
		 * set it now.
		 */
		if (ip->save_trunc_off > ip->sync_trunc_off)
			ip->save_trunc_off = ip->sync_trunc_off;
	}
	ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
			   ~HAMMER_INODE_TRUNCATED);
	ip->sync_ino_leaf = ip->ino_leaf;
	ip->sync_ino_data = ip->ino_data;
	ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;

	/*
	 * The flusher list inherits our inode and reference.
	 */
	KKASSERT(flg->running == 0);
	RB_INSERT(hammer_fls_rb_tree, &flg->flush_tree, ip);
	if (--hmp->flusher.group_lock == 0)
		wakeup(&hmp->flusher.group_lock);

	/*
	 * Auto-flush the group if it grows too large.  Make sure the
	 * inode reclaim wait pipeline continues to work.
	 */
	if (flg->total_count >= hammer_autoflush ||
	    flg->total_count >= hammer_limit_reclaims / 4) {
		if (hmp->fill_flush_group == flg)
			hmp->fill_flush_group = TAILQ_NEXT(flg, flush_entry);
		hammer_flusher_async(hmp, flg);
	}
}

/*
 * Callback for scan of ip->rec_tree.  Try to include each record in our
 * flush.  ip->flush_group has been set but the inode has not yet been
 * moved into a flushing state.
 *
 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
 * both inodes.
 *
 * We return 1 for any record placed or found in FST_FLUSH, which prevents
 * the caller from shortcutting the flush.
 */
static int
hammer_setup_child_callback(hammer_record_t rec, void *data)
{
	hammer_flush_group_t flg;
	hammer_inode_t target_ip;
	hammer_inode_t ip;
	int r;

	/*
	 * Records deleted or committed by the backend are ignored.
	 * Note that the flush detects deleted frontend records at
	 * multiple points to deal with races.  This is just the first
	 * line of defense.  The only time HAMMER_RECF_DELETED_FE cannot
	 * be set is when HAMMER_RECF_INTERLOCK_BE is set, because it
	 * messes up link-count calculations.
	 *
	 * NOTE: Don't get confused between record deletion and, say,
	 * directory entry deletion.  The deletion of a directory entry
	 * which is on-media has nothing to do with the record deletion
	 * flags.
	 */
	if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
			  HAMMER_RECF_COMMITTED)) {
		if (rec->flush_state == HAMMER_FST_FLUSH) {
			KKASSERT(rec->flush_group == rec->ip->flush_group);
			r = 1;
		} else {
			r = 0;
		}
		return(r);
	}

	/*
	 * If the record is in an idle state it has no dependancies and
	 * can be flushed.
	 */
	ip = rec->ip;
	flg = ip->flush_group;
	r = 0;

	switch(rec->flush_state) {
	case HAMMER_FST_IDLE:
		/*
		 * The record has no setup dependancy, we can flush it.
		 */
		KKASSERT(rec->target_ip == NULL);
		rec->flush_state = HAMMER_FST_FLUSH;
		rec->flush_group = flg;
		++flg->refs;
		hammer_ref(&rec->lock);
		r = 1;
		break;
	case HAMMER_FST_SETUP:
		/*
		 * The record has a setup dependancy.  These are typically
		 * directory entry adds and deletes.  Such entries will be
		 * flushed when their inodes are flushed so we do not
		 * usually have to add them to the flush here.  However,
		 * if the target_ip has set HAMMER_INODE_CONN_DOWN then
		 * it is asking us to flush this record (and it).
		 */
		target_ip = rec->target_ip;
		KKASSERT(target_ip != NULL);
		KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);

		/*
		 * If the target IP is already flushing in our group
		 * we could associate the record, but target_ip has
		 * already synced ino_data to sync_ino_data and we
		 * would also have to adjust nlinks.   Plus there are
		 * ordering issues for adds and deletes.
		 *
		 * Reflush downward if this is an ADD, and upward if
		 * this is a DEL.
		 */
		if (target_ip->flush_state == HAMMER_FST_FLUSH) {
			if (rec->type == HAMMER_MEM_RECORD_ADD)
				ip->flags |= HAMMER_INODE_REFLUSH;
			else
				target_ip->flags |= HAMMER_INODE_REFLUSH;
			break;
		}

		/*
		 * Target IP is not yet flushing.  This can get complex
		 * because we have to be careful about the recursion.
		 *
		 * Directories create an issue for us in that if a flush
		 * of a directory is requested the expectation is to flush
		 * any pending directory entries, but this will cause the
		 * related inodes to recursively flush as well.  We can't
		 * really defer the operation so just get as many as we
		 * can and
		 */
#if 0
		if ((target_ip->flags & HAMMER_INODE_RECLAIM) == 0 &&
		    (target_ip->flags & HAMMER_INODE_CONN_DOWN) == 0) {
			/*
			 * We aren't reclaiming and the target ip was not
			 * previously prevented from flushing due to this
			 * record dependancy.  Do not flush this record.
			 */
			/*r = 0;*/
		} else
#endif
		if (flg->total_count + flg->refs >
			   ip->hmp->undo_rec_limit) {
			/*
			 * Our flush group is over-full and we risk blowing
			 * out the UNDO FIFO.  Stop the scan, flush what we
			 * have, then reflush the directory.
			 *
			 * The directory may be forced through multiple
			 * flush groups before it can be completely
			 * flushed.
			 */
			ip->flags |= HAMMER_INODE_RESIGNAL |
				     HAMMER_INODE_REFLUSH;
			r = -1;
		} else if (rec->type == HAMMER_MEM_RECORD_ADD) {
			/*
			 * If the target IP is not flushing we can force
			 * it to flush, even if it is unable to write out
			 * any of its own records we have at least one in
			 * hand that we CAN deal with.
			 */
			rec->flush_state = HAMMER_FST_FLUSH;
			rec->flush_group = flg;
			++flg->refs;
			hammer_ref(&rec->lock);
			hammer_flush_inode_core(target_ip, flg,
						HAMMER_FLUSH_RECURSION);
			r = 1;
		} else {
			/*
			 * General or delete-on-disk record.
			 *
			 * XXX this needs help.  If a delete-on-disk we could
			 * disconnect the target.  If the target has its own
			 * dependancies they really need to be flushed.
			 *
			 * XXX
			 */
			rec->flush_state = HAMMER_FST_FLUSH;
			rec->flush_group = flg;
			++flg->refs;
			hammer_ref(&rec->lock);
			hammer_flush_inode_core(target_ip, flg,
						HAMMER_FLUSH_RECURSION);
			r = 1;
		}
		break;
	case HAMMER_FST_FLUSH:
		/*
		 * The record could be part of a previous flush group if the
		 * inode is a directory (the record being a directory entry).
		 * Once the flush group was closed a hammer_test_inode()
		 * function can cause a new flush group to be setup, placing
		 * the directory inode itself in a new flush group.
		 *
		 * When associated with a previous flush group we count it
		 * as if it were in our current flush group, since it will
		 * effectively be flushed by the time we flush our current
		 * flush group.
		 */
		KKASSERT(
		    rec->ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY ||
		    rec->flush_group == flg);
		r = 1;
		break;
	}
	return(r);
}

#if 0
/*
 * This version just moves records already in a flush state to the new
 * flush group and that is it.
 */
static int
hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
{
	hammer_inode_t ip = rec->ip;

	switch(rec->flush_state) {
	case HAMMER_FST_FLUSH:
		KKASSERT(rec->flush_group == ip->flush_group);
		break;
	default:
		break;
	}
	return(0);
}
#endif

/*
 * Wait for a previously queued flush to complete.
 *
 * If a critical error occured we don't try to wait.
 */
void
hammer_wait_inode(hammer_inode_t ip)
{
	/*
	 * The inode can be in a SETUP state in which case RESIGNAL
	 * should be set.  If RESIGNAL is not set then the previous
	 * flush completed and a later operation placed the inode
	 * in a passive setup state again, so we're done.
	 *
	 * The inode can be in a FLUSH state in which case we
	 * can just wait for completion.
	 */
	while (ip->flush_state == HAMMER_FST_FLUSH ||
	    (ip->flush_state == HAMMER_FST_SETUP &&
	     (ip->flags & HAMMER_INODE_RESIGNAL))) {
		/*
		 * Don't try to flush on a critical error
		 */
		if (ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
			break;

		/*
		 * If the inode was already being flushed its flg
		 * may not have been queued to the backend.  We
		 * have to make sure it gets queued or we can wind
		 * up blocked or deadlocked (particularly if we are
		 * the vnlru thread).
		 */
		if (ip->flush_state == HAMMER_FST_FLUSH) {
			KKASSERT(ip->flush_group);
			if (ip->flush_group->closed == 0) {
				if (hammer_debug_inode) {
					hkprintf("debug: forcing "
						"async flush ip %016jx\n",
						(intmax_t)ip->obj_id);
				}
				hammer_flusher_async(ip->hmp, ip->flush_group);
				continue; /* retest */
			}
		}

		/*
		 * In a flush state with the flg queued to the backend
		 * or in a setup state with RESIGNAL set, we can safely
		 * wait.
		 */
		ip->flags |= HAMMER_INODE_FLUSHW;
		tsleep(&ip->flags, 0, "hmrwin", 0);
	}

#if 0
	/*
	 * The inode may have been in a passive setup state,
	 * call flush to make sure we get signaled.
	 */
	if (ip->flush_state == HAMMER_FST_SETUP)
		hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
#endif

}

/*
 * Called by the backend code when a flush has been completed.
 * The inode has already been removed from the flush list.
 *
 * A pipelined flush can occur, in which case we must re-enter the
 * inode on the list and re-copy its fields.
 */
void
hammer_sync_inode_done(hammer_inode_t ip, int error)
{
	hammer_mount_t hmp;
	int dorel;

	KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);

	hmp = ip->hmp;

	/*
	 * Auto-reflush if the backend could not completely flush
	 * the inode.  This fixes a case where a deferred buffer flush
	 * could cause fsync to return early.
	 */
	if (ip->sync_flags & HAMMER_INODE_MODMASK)
		ip->flags |= HAMMER_INODE_REFLUSH;

	/*
	 * Merge left-over flags back into the frontend and fix the state.
	 * Incomplete truncations are retained by the backend.
	 */
	ip->error = error;
	ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
	ip->sync_flags &= HAMMER_INODE_TRUNCATED;

	/*
	 * The backend may have adjusted nlinks, so if the adjusted nlinks
	 * does not match the fronttend set the frontend's DDIRTY flag again.
	 */
	if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
		ip->flags |= HAMMER_INODE_DDIRTY;

	/*
	 * Fix up the dirty buffer status.
	 */
	if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
		ip->flags |= HAMMER_INODE_BUFS;
	}
	hammer_redo_fifo_end_flush(ip);

	/*
	 * Re-set the XDIRTY flag if some of the inode's in-memory records
	 * could not be flushed.
	 */
	KKASSERT((RB_EMPTY(&ip->rec_tree) &&
		  (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
		 (!RB_EMPTY(&ip->rec_tree) &&
		  (ip->flags & HAMMER_INODE_XDIRTY) != 0));

	/*
	 * Do not lose track of inodes which no longer have vnode
	 * assocations, otherwise they may never get flushed again.
	 *
	 * The reflush flag can be set superfluously, causing extra pain
	 * for no reason.  If the inode is no longer modified it no longer
	 * needs to be flushed.
	 */
	if (ip->flags & HAMMER_INODE_MODMASK) {
		if (ip->vp == NULL)
			ip->flags |= HAMMER_INODE_REFLUSH;
	} else {
		ip->flags &= ~HAMMER_INODE_REFLUSH;
	}

	/*
	 * The fs token is held but the inode lock is not held.  Because this
	 * is a backend flush it is possible that the vnode has no references
	 * and cause a reclaim race inside vsetisdirty() if/when it blocks.
	 *
	 * Therefore, we must lock the inode around this particular dirtying
	 * operation.  We don't have to around other dirtying operations
	 * where the vnode is implicitly or explicitly held.
	 */
	if (ip->flags & HAMMER_INODE_MODMASK) {
		hammer_lock_ex(&ip->lock);
		hammer_inode_dirty(ip);
		hammer_unlock(&ip->lock);
	}

	/*
	 * Adjust the flush state.
	 */
	if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
		/*
		 * We were unable to flush out all our records, leave the
		 * inode in a flush state and in the current flush group.
		 * The flush group will be re-run.
		 *
		 * This occurs if the UNDO block gets too full or there is
		 * too much dirty meta-data and allows the flusher to
		 * finalize the UNDO block and then re-flush.
		 */
		ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
		dorel = 0;
	} else {
		/*
		 * Remove from the flush_group
		 */
		RB_REMOVE(hammer_fls_rb_tree, &ip->flush_group->flush_tree, ip);
		ip->flush_group = NULL;

#if 0
		/*
		 * Clean up the vnode ref and tracking counts.
		 */
		if (ip->flags & HAMMER_INODE_VHELD) {
			ip->flags &= ~HAMMER_INODE_VHELD;
			vrele(ip->vp);
		}
#endif
		--hmp->count_iqueued;
		--hammer_count_iqueued;

		/*
		 * And adjust the state.
		 */
		if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
			ip->flush_state = HAMMER_FST_IDLE;
			dorel = 1;
		} else {
			ip->flush_state = HAMMER_FST_SETUP;
			dorel = 0;
		}

		/*
		 * If the frontend is waiting for a flush to complete,
		 * wake it up.
		 */
		if (ip->flags & HAMMER_INODE_FLUSHW) {
			ip->flags &= ~HAMMER_INODE_FLUSHW;
			wakeup(&ip->flags);
		}

		/*
		 * If the frontend made more changes and requested another
		 * flush, then try to get it running.
		 *
		 * Reflushes are aborted when the inode is errored out.
		 */
		if (ip->flags & HAMMER_INODE_REFLUSH) {
			ip->flags &= ~HAMMER_INODE_REFLUSH;
			if (ip->flags & HAMMER_INODE_RESIGNAL) {
				ip->flags &= ~HAMMER_INODE_RESIGNAL;
				hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
			} else {
				hammer_flush_inode(ip, 0);
			}
		}
	}

	/*
	 * If we have no parent dependancies we can clear CONN_DOWN
	 */
	if (TAILQ_EMPTY(&ip->target_list))
		ip->flags &= ~HAMMER_INODE_CONN_DOWN;

	/*
	 * If the inode is now clean drop the space reservation.
	 */
	if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
	    (ip->flags & HAMMER_INODE_RSV_INODES)) {
		ip->flags &= ~HAMMER_INODE_RSV_INODES;
		--hmp->rsv_inodes;
	}

	ip->flags &= ~HAMMER_INODE_SLAVEFLUSH;

	if (dorel)
		hammer_rel_inode(ip, 0);
}

/*
 * Called from hammer_sync_inode() to synchronize in-memory records
 * to the media.
 */
static int
hammer_sync_record_callback(hammer_record_t record, void *data)
{
	hammer_cursor_t cursor = data;
	hammer_transaction_t trans = cursor->trans;
	hammer_mount_t hmp = trans->hmp;
	int error;

	/*
	 * Skip records that do not belong to the current flush.
	 */
	++hammer_stats_record_iterations;
	if (record->flush_state != HAMMER_FST_FLUSH)
		return(0);

	if (record->flush_group != record->ip->flush_group) {
		hdkprintf("rec %p ip %p bad flush group %p %p\n",
			record,
			record->ip,
			record->flush_group,
			record->ip->flush_group);
		if (hammer_debug_critical)
			Debugger("blah2");
		return(0);
	}
	KKASSERT(record->flush_group == record->ip->flush_group);

	/*
	 * Interlock the record using the BE flag.  Once BE is set the
	 * frontend cannot change the state of FE.
	 *
	 * NOTE: If FE is set prior to us setting BE we still sync the
	 * record out, but the flush completion code converts it to
	 * a delete-on-disk record instead of destroying it.
	 */
	KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
	record->flags |= HAMMER_RECF_INTERLOCK_BE;

	/*
	 * The backend has already disposed of the record.
	 */
	if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
		error = 0;
		goto done;
	}

	/*
	 * If the whole inode is being deleted and all on-disk records will
	 * be deleted very soon, we can't sync any new records to disk
	 * because they will be deleted in the same transaction they were
	 * created in (delete_tid == create_tid), which will assert.
	 *
	 * XXX There may be a case with RECORD_ADD with DELETED_FE set
	 * that we currently panic on.
	 */
	if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
		switch(record->type) {
		case HAMMER_MEM_RECORD_DATA:
			/*
			 * We don't have to do anything, if the record was
			 * committed the space will have been accounted for
			 * in the blockmap.
			 */
			/* fall through */
		case HAMMER_MEM_RECORD_GENERAL:
			/*
			 * Set deleted-by-backend flag.  Do not set the
			 * backend committed flag, because we are throwing
			 * the record away.
			 */
			record->flags |= HAMMER_RECF_DELETED_BE;
			++record->ip->rec_generation;
			error = 0;
			goto done;
		case HAMMER_MEM_RECORD_ADD:
			hpanic("illegal add during inode deletion record %p",
				record);
			break; /* NOT REACHED */
		case HAMMER_MEM_RECORD_INODE:
			hpanic("attempt to sync inode record %p?", record);
			break; /* NOT REACHED */
		case HAMMER_MEM_RECORD_DEL:
			/*
			 * Follow through and issue the on-disk deletion
			 */
			break;
		}
	}

	/*
	 * If DELETED_FE is set special handling is needed for directory
	 * entries.  Dependant pieces related to the directory entry may
	 * have already been synced to disk.  If this occurs we have to
	 * sync the directory entry and then change the in-memory record
	 * from an ADD to a DELETE to cover the fact that it's been
	 * deleted by the frontend.
	 *
	 * A directory delete covering record (MEM_RECORD_DEL) can never
	 * be deleted by the frontend.
	 *
	 * Any other record type (aka DATA) can be deleted by the frontend.
	 * XXX At the moment the flusher must skip it because there may
	 * be another data record in the flush group for the same block,
	 * meaning that some frontend data changes can leak into the backend's
	 * synchronization point.
	 */
	if (record->flags & HAMMER_RECF_DELETED_FE) {
		if (record->type == HAMMER_MEM_RECORD_ADD) {
			/*
			 * Convert a front-end deleted directory-add to
			 * a directory-delete entry later.
			 */
			record->flags |= HAMMER_RECF_CONVERT_DELETE;
		} else {
			/*
			 * Dispose of the record (race case).  Mark as
			 * deleted by backend (and not committed).
			 */
			KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
			record->flags |= HAMMER_RECF_DELETED_BE;
			++record->ip->rec_generation;
			error = 0;
			goto done;
		}
	}

	/*
	 * Assign the create_tid for new records.  Deletions already
	 * have the record's entire key properly set up.
	 */
	if (record->type != HAMMER_MEM_RECORD_DEL) {
		record->leaf.base.create_tid = trans->tid;
		record->leaf.create_ts = trans->time32;
	}

	/*
	 * This actually moves the record to the on-media B-Tree.  We
	 * must also generate REDO_TERM entries in the UNDO/REDO FIFO
	 * indicating that the related REDO_WRITE(s) have been committed.
	 *
	 * During recovery any REDO_TERM's within the nominal recovery span
	 * are ignored since the related meta-data is being undone, causing
	 * any matching REDO_WRITEs to execute.  The REDO_TERMs outside
	 * the nominal recovery span will match against REDO_WRITEs and
	 * prevent them from being executed (because the meta-data has
	 * already been synchronized).
	 */
	if (record->flags & HAMMER_RECF_REDO) {
		KKASSERT(record->type == HAMMER_MEM_RECORD_DATA);
		hammer_generate_redo(trans, record->ip,
				     record->leaf.base.key -
					 record->leaf.data_len,
				     HAMMER_REDO_TERM_WRITE,
				     NULL,
				     record->leaf.data_len);
	}

	for (;;) {
		error = hammer_ip_sync_record_cursor(cursor, record);
		if (error != EDEADLK)
			break;
		hammer_done_cursor(cursor);
		error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
					   record->ip);
		if (error)
			break;
	}
	record->flags &= ~HAMMER_RECF_CONVERT_DELETE;

	if (error)
		error = -error;
done:
	hammer_flush_record_done(record, error);

	/*
	 * Do partial finalization if we have built up too many dirty
	 * buffers.  Otherwise a buffer cache deadlock can occur when
	 * doing things like creating tens of thousands of tiny files.
	 *
	 * We must release our cursor lock to avoid a 3-way deadlock
	 * due to the exclusive sync lock the finalizer must get.
	 *
	 * WARNING: See warnings in hammer_unlock_cursor() function.
	 */
        if (hammer_flusher_meta_limit(hmp) ||
	    vm_paging_severe()) {
		hammer_unlock_cursor(cursor);
                hammer_flusher_finalize(trans, 0);
		hammer_lock_cursor(cursor);
	}
	return(error);
}

/*
 * Backend function called by the flusher to sync an inode to media.
 */
int
hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip)
{
	struct hammer_cursor cursor;
	hammer_node_t tmp_node;
	hammer_record_t depend;
	hammer_record_t next;
	int error, tmp_error;
	uint64_t nlinks;

	if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
		return(0);

	error = hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
	if (error)
		goto done;

	/*
	 * Any directory records referencing this inode which are not in
	 * our current flush group must adjust our nlink count for the
	 * purposes of synchronizating to disk.
	 *
	 * Records which are in our flush group can be unlinked from our
	 * inode now, potentially allowing the inode to be physically
	 * deleted.
	 *
	 * This cannot block.
	 */
	nlinks = ip->ino_data.nlinks;
	next = TAILQ_FIRST(&ip->target_list);
	while ((depend = next) != NULL) {
		next = TAILQ_NEXT(depend, target_entry);
		if (depend->flush_state == HAMMER_FST_FLUSH &&
		    depend->flush_group == ip->flush_group) {
			/*
			 * If this is an ADD that was deleted by the frontend
			 * the frontend nlinks count will have already been
			 * decremented, but the backend is going to sync its
			 * directory entry and must account for it.  The
			 * record will be converted to a delete-on-disk when
			 * it gets synced.
			 *
			 * If the ADD was not deleted by the frontend we
			 * can remove the dependancy from our target_list.
			 */
			if (depend->flags & HAMMER_RECF_DELETED_FE) {
				++nlinks;
			} else {
				TAILQ_REMOVE(&ip->target_list, depend,
					     target_entry);
				depend->target_ip = NULL;
			}
		} else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
			/*
			 * Not part of our flush group and not deleted by
			 * the front-end, adjust the link count synced to
			 * the media (undo what the frontend did when it
			 * queued the record).
			 */
			KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
			switch(depend->type) {
			case HAMMER_MEM_RECORD_ADD:
				--nlinks;
				break;
			case HAMMER_MEM_RECORD_DEL:
				++nlinks;
				break;
			default:
				break;
			}
		}
	}

	/*
	 * Set dirty if we had to modify the link count.
	 */
	if (ip->sync_ino_data.nlinks != nlinks) {
		KKASSERT((int64_t)nlinks >= 0);
		ip->sync_ino_data.nlinks = nlinks;
		ip->sync_flags |= HAMMER_INODE_DDIRTY;
	}

	/*
	 * If there is a trunction queued destroy any data past the (aligned)
	 * truncation point.  Userland will have dealt with the buffer
	 * containing the truncation point for us.
	 *
	 * We don't flush pending frontend data buffers until after we've
	 * dealt with the truncation.
	 */
	if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
		/*
		 * Interlock trunc_off.  The VOP front-end may continue to
		 * make adjustments to it while we are blocked.
		 */
		off_t trunc_off;
		off_t aligned_trunc_off;
		int blkmask;

		trunc_off = ip->sync_trunc_off;
		blkmask = hammer_blocksize(trunc_off) - 1;
		aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;

		/*
		 * Delete any whole blocks on-media.  The front-end has
		 * already cleaned out any partial block and made it
		 * pending.  The front-end may have updated trunc_off
		 * while we were blocked so we only use sync_trunc_off.
		 *
		 * This operation can blow out the buffer cache, EWOULDBLOCK
		 * means we were unable to complete the deletion.  The
		 * deletion will update sync_trunc_off in that case.
		 */
		error = hammer_ip_delete_range(&cursor, ip,
						aligned_trunc_off,
						HAMMER_MAX_KEY, 2);
		if (error == EWOULDBLOCK) {
			ip->flags |= HAMMER_INODE_WOULDBLOCK;
			error = 0;
			goto defer_buffer_flush;
		}

		if (error)
			goto done;

		/*
		 * Generate a REDO_TERM_TRUNC entry in the UNDO/REDO FIFO.
		 *
		 * XXX we do this even if we did not previously generate
		 * a REDO_TRUNC record.  This operation may enclosed the
		 * range for multiple prior truncation entries in the REDO
		 * log.
		 */
		if (trans->hmp->version >= HAMMER_VOL_VERSION_FOUR &&
		    (ip->flags & HAMMER_INODE_RDIRTY)) {
			hammer_generate_redo(trans, ip, aligned_trunc_off,
					     HAMMER_REDO_TERM_TRUNC,
					     NULL, 0);
		}

		/*
		 * Clear the truncation flag on the backend after we have
		 * completed the deletions.  Backend data is now good again
		 * (including new records we are about to sync, below).
		 *
		 * Leave sync_trunc_off intact.  As we write additional
		 * records the backend will update sync_trunc_off.  This
		 * tells the backend whether it can skip the overwrite
		 * test.  This should work properly even when the backend
		 * writes full blocks where the truncation point straddles
		 * the block because the comparison is against the base
		 * offset of the record.
		 */
		ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
		/* ip->sync_trunc_off = HAMMER_MAX_KEY; */
	} else {
		error = 0;
	}

	/*
	 * Now sync related records.  These will typically be directory
	 * entries, records tracking direct-writes, or delete-on-disk records.
	 */
	if (error == 0) {
		tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
				    hammer_sync_record_callback, &cursor);
		if (tmp_error < 0)
			tmp_error = -error;
		if (tmp_error)
			error = tmp_error;
	}
	hammer_cache_node(&ip->cache[1], cursor.node);

	/*
	 * Re-seek for inode update, assuming our cache hasn't been ripped
	 * out from under us.
	 */
	if (error == 0) {
		tmp_node = hammer_ref_node_safe(trans, &ip->cache[0], &error);
		if (tmp_node) {
			hammer_cursor_downgrade(&cursor);
			hammer_lock_sh(&tmp_node->lock);
			if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
				hammer_cursor_seek(&cursor, tmp_node, 0);
			hammer_unlock(&tmp_node->lock);
			hammer_rel_node(tmp_node);
		}
		error = 0;
	}

	/*
	 * If we are deleting the inode the frontend had better not have
	 * any active references on elements making up the inode.
	 *
	 * The call to hammer_ip_delete_clean() cleans up auxillary records
	 * but not DB or DATA records.  Those must have already been deleted
	 * by the normal truncation mechanic.
	 */
	if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
		RB_EMPTY(&ip->rec_tree)  &&
	    (ip->sync_flags & HAMMER_INODE_DELETING) &&
	    (ip->flags & HAMMER_INODE_DELETED) == 0) {
		int count1 = 0;

		error = hammer_ip_delete_clean(&cursor, ip, &count1);
		if (error == 0) {
			ip->flags |= HAMMER_INODE_DELETED;
			ip->sync_flags &= ~HAMMER_INODE_DELETING;
			ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
			KKASSERT(RB_EMPTY(&ip->rec_tree));

			/*
			 * Set delete_tid in both the frontend and backend
			 * copy of the inode record.  The DELETED flag handles
			 * this, do not set DDIRTY.
			 */
			ip->ino_leaf.base.delete_tid = trans->tid;
			ip->sync_ino_leaf.base.delete_tid = trans->tid;
			ip->ino_leaf.delete_ts = trans->time32;
			ip->sync_ino_leaf.delete_ts = trans->time32;


			/*
			 * Adjust the inode count in the volume header
			 */
			hammer_sync_lock_sh(trans);
			if (ip->flags & HAMMER_INODE_ONDISK) {
				hammer_modify_volume_field(trans,
							   trans->rootvol,
							   vol0_stat_inodes);
				--ip->hmp->rootvol->ondisk->vol0_stat_inodes;
				hammer_modify_volume_done(trans->rootvol);
			}
			hammer_sync_unlock(trans);
		}
	}

	if (error)
		goto done;
	ip->sync_flags &= ~HAMMER_INODE_BUFS;

defer_buffer_flush:
	/*
	 * Now update the inode's on-disk inode-data and/or on-disk record.
	 * DELETED and ONDISK are managed only in ip->flags.
	 *
	 * In the case of a defered buffer flush we still update the on-disk
	 * inode to satisfy visibility requirements if there happen to be
	 * directory dependancies.
	 */
	switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
	case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
		/*
		 * If deleted and on-disk, don't set any additional flags.
		 * the delete flag takes care of things.
		 *
		 * Clear flags which may have been set by the frontend.
		 */
		ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
				    HAMMER_INODE_SDIRTY |
				    HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
				    HAMMER_INODE_DELETING);
		break;
	case HAMMER_INODE_DELETED:
		/*
		 * Take care of the case where a deleted inode was never
		 * flushed to the disk in the first place.
		 *
		 * Clear flags which may have been set by the frontend.
		 */
		ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
				    HAMMER_INODE_SDIRTY |
				    HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
				    HAMMER_INODE_DELETING);
		while (RB_ROOT(&ip->rec_tree)) {
			hammer_record_t record = RB_ROOT(&ip->rec_tree);
			hammer_ref(&record->lock);
			KKASSERT(hammer_oneref(&record->lock));
			record->flags |= HAMMER_RECF_DELETED_BE;
			++record->ip->rec_generation;
			hammer_rel_mem_record(record);
		}
		break;
	case HAMMER_INODE_ONDISK:
		/*
		 * If already on-disk, do not set any additional flags.
		 */
		break;
	default:
		/*
		 * If not on-disk and not deleted, set DDIRTY to force
		 * an initial record to be written.
		 *
		 * Also set the create_tid in both the frontend and backend
		 * copy of the inode record.
		 */
		ip->ino_leaf.base.create_tid = trans->tid;
		ip->ino_leaf.create_ts = trans->time32;
		ip->sync_ino_leaf.base.create_tid = trans->tid;
		ip->sync_ino_leaf.create_ts = trans->time32;
		ip->sync_flags |= HAMMER_INODE_DDIRTY;
		break;
	}

	/*
	 * If DDIRTY or SDIRTY is set, write out a new record.
	 * If the inode is already on-disk the old record is marked as
	 * deleted.
	 *
	 * If DELETED is set hammer_update_inode() will delete the existing
	 * record without writing out a new one.
	 */
	if (ip->flags & HAMMER_INODE_DELETED) {
		error = hammer_update_inode(&cursor, ip);
	} else
	if (!(ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_SDIRTY)) &&
	    (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
		error = hammer_update_itimes(&cursor, ip);
	} else
	if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_SDIRTY |
			      HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
		error = hammer_update_inode(&cursor, ip);
	}
done:
	if (ip->flags & HAMMER_INODE_MODMASK)
		hammer_inode_dirty(ip);
	if (error) {
		hammer_critical_error(ip->hmp, ip, error,
				      "while syncing inode");
	}
	hammer_done_cursor(&cursor);
	return(error);
}

/*
 * This routine is called when the OS is no longer actively referencing
 * the inode (but might still be keeping it cached), or when releasing
 * the last reference to an inode.
 *
 * At this point if the inode's nlinks count is zero we want to destroy
 * it, which may mean destroying it on-media too.
 */
void
hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
{
	struct vnode *vp;

	/*
	 * Set the DELETING flag when the link count drops to 0 and the
	 * OS no longer has any opens on the inode.
	 *
	 * The backend will clear DELETING (a mod flag) and set DELETED
	 * (a state flag) when it is actually able to perform the
	 * operation.
	 *
	 * Don't reflag the deletion if the flusher is currently syncing
	 * one that was already flagged.  A previously set DELETING flag
	 * may bounce around flags and sync_flags until the operation is
	 * completely done.
	 *
	 * Do not attempt to modify a snapshot inode (one set to read-only).
	 */
	if (ip->ino_data.nlinks == 0 &&
	    ((ip->flags | ip->sync_flags) & (HAMMER_INODE_RO|HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
		ip->flags |= HAMMER_INODE_DELETING;
		ip->flags |= HAMMER_INODE_TRUNCATED;
		ip->trunc_off = 0;
		vp = NULL;
		if (getvp) {
			if (hammer_get_vnode(ip, &vp) != 0)
				return;
		}

		/*
		 * Final cleanup
		 */
		if (ip->vp)
			nvtruncbuf(ip->vp, 0, HAMMER_BUFSIZE, 0, 0);
		if (ip->flags & HAMMER_INODE_MODMASK)
			hammer_inode_dirty(ip);
		if (getvp)
			vput(vp);
	}
}

/*
 * After potentially resolving a dependancy the inode is tested
 * to determine whether it needs to be reflushed.
 */
void
hammer_test_inode(hammer_inode_t ip)
{
	if (ip->flags & HAMMER_INODE_REFLUSH) {
		ip->flags &= ~HAMMER_INODE_REFLUSH;
		hammer_ref(&ip->lock);
		if (ip->flags & HAMMER_INODE_RESIGNAL) {
			ip->flags &= ~HAMMER_INODE_RESIGNAL;
			hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
		} else {
			hammer_flush_inode(ip, 0);
		}
		hammer_rel_inode(ip, 0);
	}
}

/*
 * Clear the RECLAIM flag on an inode.  This occurs when the inode is
 * reassociated with a vp or just before it gets freed.
 *
 * Pipeline wakeups to threads blocked due to an excessive number of
 * detached inodes.  This typically occurs when atime updates accumulate
 * while scanning a directory tree.
 */
static void
hammer_inode_wakereclaims(hammer_inode_t ip)
{
	struct hammer_reclaim *reclaim;
	hammer_mount_t hmp = ip->hmp;

	if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
		return;

	--hammer_count_reclaims;
	--hmp->count_reclaims;
	ip->flags &= ~HAMMER_INODE_RECLAIM;

	if ((reclaim = TAILQ_FIRST(&hmp->reclaim_list)) != NULL) {
		KKASSERT(reclaim->count > 0);
		if (--reclaim->count == 0) {
			TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
			wakeup(reclaim);
		}
	}
}

/*
 * Setup our reclaim pipeline.  We only let so many detached (and dirty)
 * inodes build up before we start blocking.  This routine is called
 * if a new inode is created or an inode is loaded from media.
 *
 * When we block we don't care *which* inode has finished reclaiming,
 * as long as one does.
 *
 * The reclaim pipeline is primarily governed by the auto-flush which is
 * 1/4 hammer_limit_reclaims.  We don't want to block if the count is
 * less than 1/2 hammer_limit_reclaims.  From 1/2 to full count is
 * dynamically governed.
 */
void
hammer_inode_waitreclaims(hammer_transaction_t trans)
{
	hammer_mount_t hmp = trans->hmp;
	struct hammer_reclaim reclaim;
	int lower_limit;

	/*
	 * Track inode load, delay if the number of reclaiming inodes is
	 * between 2/4 and 4/4 hammer_limit_reclaims, depending.
	 */
	if (curthread->td_proc) {
		struct hammer_inostats *stats;

		stats = hammer_inode_inostats(hmp, curthread->td_proc->p_pid);
		++stats->count;

		if (stats->count > hammer_limit_reclaims / 2)
			stats->count = hammer_limit_reclaims / 2;
		lower_limit = hammer_limit_reclaims - stats->count;
		if (hammer_debug_general & 0x10000) {
			hdkprintf("pid %5d limit %d\n",
				(int)curthread->td_proc->p_pid, lower_limit);
		}
	} else {
		lower_limit = hammer_limit_reclaims * 3 / 4;
	}
	if (hmp->count_reclaims >= lower_limit) {
		reclaim.count = 1;
		TAILQ_INSERT_TAIL(&hmp->reclaim_list, &reclaim, entry);
		tsleep(&reclaim, 0, "hmrrcm", hz);
		if (reclaim.count > 0)
			TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
	}
}

/*
 * Keep track of reclaim statistics on a per-pid basis using a loose
 * 4-way set associative hash table.  Collisions inherit the count of
 * the previous entry.
 *
 * NOTE: We want to be careful here to limit the chain size.  If the chain
 *	 size is too large a pid will spread its stats out over too many
 *	 entries under certain types of heavy filesystem activity and
 *	 wind up not delaying long enough.
 */
static
struct hammer_inostats *
hammer_inode_inostats(hammer_mount_t hmp, pid_t pid)
{
	struct hammer_inostats *stats;
	int delta;
	int chain;
	static volatile int iterator;	/* we don't care about MP races */

	/*
	 * Chain up to 4 times to find our entry.
	 */
	for (chain = 0; chain < 4; ++chain) {
		stats = &hmp->inostats[(pid + chain) & HAMMER_INOSTATS_HMASK];
		if (stats->pid == pid)
			break;
	}

	/*
	 * Replace one of the four chaining entries with our new entry.
	 */
	if (chain == 4) {
		stats = &hmp->inostats[(pid + (iterator++ & 3)) &
				       HAMMER_INOSTATS_HMASK];
		stats->pid = pid;
	}

	/*
	 * Decay the entry
	 */
	if (stats->count && stats->ltick != ticks) {
		delta = ticks - stats->ltick;
		stats->ltick = ticks;
		if (delta <= 0 || delta > hz * 60)
			stats->count = 0;
		else
			stats->count = stats->count * hz / (hz + delta);
	}
	if (hammer_debug_general & 0x10000)
		hdkprintf("pid %5d stats %d\n", (int)pid, stats->count);
	return (stats);
}

#if 0

/*
 * XXX not used, doesn't work very well due to the large batching nature
 * of flushes.
 *
 * A larger then normal backlog of inodes is sitting in the flusher,
 * enforce a general slowdown to let it catch up.  This routine is only
 * called on completion of a non-flusher-related transaction which
 * performed B-Tree node I/O.
 *
 * It is possible for the flusher to stall in a continuous load.
 * blogbench -i1000 -o seems to do a good job generating this sort of load.
 * If the flusher is unable to catch up the inode count can bloat until
 * we run out of kvm.
 *
 * This is a bit of a hack.
 */
void
hammer_inode_waithard(hammer_mount_t hmp)
{
	/*
	 * Hysteresis.
	 */
	if (hmp->flags & HAMMER_MOUNT_FLUSH_RECOVERY) {
		if (hmp->count_reclaims < hammer_limit_reclaims / 2 &&
		    hmp->count_iqueued < hmp->count_inodes / 20) {
			hmp->flags &= ~HAMMER_MOUNT_FLUSH_RECOVERY;
			return;
		}
	} else {
		if (hmp->count_reclaims < hammer_limit_reclaims ||
		    hmp->count_iqueued < hmp->count_inodes / 10) {
			return;
		}
		hmp->flags |= HAMMER_MOUNT_FLUSH_RECOVERY;
	}

	/*
	 * Block for one flush cycle.
	 */
	hammer_flusher_wait_next(hmp);
}

#endif