DragonFlyBSD Kernel Audit
sys/vfs/hammer2/hammer2.h
← 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
/*
 * Copyright (c) 2011-2018 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@dragonflybsd.org>
 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
 *
 * 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.
 */

/*
 * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
 *
 * This header file contains structures used internally by the HAMMER2
 * implementation.  See hammer2_disk.h for on-disk structures.
 *
 * There is an in-memory representation of all on-media data structure.
 * Almost everything is represented by a hammer2_chain structure in-memory.
 * Other higher-level structures typically map to chains.
 *
 * A great deal of data is accessed simply via its buffer cache buffer,
 * which is mapped for the duration of the chain's lock.  Hammer2 must
 * implement its own buffer cache layer on top of the system layer to
 * allow for different threads to lock different sub-block-sized buffers.
 *
 * When modifications are made to a chain a new filesystem block must be
 * allocated.  Multiple modifications do not typically allocate new blocks
 * until the current block has been flushed.  Flushes do not block the
 * front-end unless the front-end operation crosses the current inode being
 * flushed.
 *
 * The in-memory representation may remain cached (for example in order to
 * placemark clustering locks) even after the related data has been
 * detached.
 */

#ifndef _VFS_HAMMER2_HAMMER2_H_
#define _VFS_HAMMER2_HAMMER2_H_

#ifdef _KERNEL
#include <sys/param.h>
#endif
#include <sys/types.h>
#ifdef _KERNEL
#include <sys/kernel.h>
#endif
#include <sys/conf.h>
#ifdef _KERNEL
#include <sys/systm.h>
#endif
#include <sys/diskslice.h>
#include <sys/tree.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/proc.h>
#include <sys/caps.h>
#include <sys/stat.h>
#include <sys/thread.h>
#include <sys/lockf.h>
#include <sys/buf.h>
#include <sys/queue.h>
#include <sys/limits.h>
#include <sys/dmsg.h>
#include <sys/mutex.h>
#include <sys/lock.h>
#include <sys/file.h>
#include <sys/filio.h>
#include <sys/objcache.h>

#ifdef _KERNEL
#include <sys/signal2.h>
#include <sys/buf2.h>
#include <sys/mutex2.h>
#include <sys/spinlock2.h>
#endif

#include "hammer2_xxhash.h"
#include "hammer2_disk.h"
#include "hammer2_mount.h"
#include "hammer2_ioctl.h"

struct hammer2_io;
struct hammer2_chain;
struct hammer2_inode;
struct hammer2_depend;
struct hammer2_dev;
struct hammer2_pfs;
union hammer2_xop;

/*
 * Mutex and lock shims.  Hammer2 requires support for asynchronous and
 * abortable locks, and both exclusive and shared spinlocks.  Normal
 * synchronous non-abortable locks can be substituted for spinlocks.
 */
typedef mtx_t				hammer2_mtx_t;
typedef mtx_state_t			hammer2_mtx_state_t;

typedef struct spinlock			hammer2_spin_t;

#define hammer2_mtx_ex			mtx_lock_ex_quick
#define hammer2_mtx_ex_try		mtx_lock_ex_try
#define hammer2_mtx_sh			mtx_lock_sh_quick
#define hammer2_mtx_sh_again		mtx_lock_sh_again
#define hammer2_mtx_sh_try		mtx_lock_sh_try
#define hammer2_mtx_unlock		mtx_unlock
#define hammer2_mtx_upgrade_try		mtx_upgrade_try
#define hammer2_mtx_downgrade		mtx_downgrade
#define hammer2_mtx_owned		mtx_owned
#define hammer2_mtx_init		mtx_init
#define hammer2_mtx_temp_release	mtx_lock_temp_release
#define hammer2_mtx_temp_restore	mtx_lock_temp_restore
#define hammer2_mtx_refs		mtx_lockrefs

#define hammer2_spin_init		spin_init
#define hammer2_spin_sh			spin_lock_shared
#define hammer2_spin_ex			spin_lock
#define hammer2_spin_unsh		spin_unlock_shared
#define hammer2_spin_unex		spin_unlock
#define hammer2_spin_lock_update	spin_lock_update
#define hammer2_spin_unlock_update	spin_unlock_update

TAILQ_HEAD(hammer2_xop_list, hammer2_xop_head);
TAILQ_HEAD(hammer2_chain_list, hammer2_chain);

typedef struct hammer2_xop_list	hammer2_xop_list_t;

/*
 * Cap the dynamic calculation for the maximum number of dirty
 * chains and dirty inodes allowed.
 */
#define HAMMER2_LIMIT_DIRTY_CHAINS	(1024*1024)
#define HAMMER2_LIMIT_DIRTY_INODES	(65536)

#define HAMMER2_IOHASH_SIZE		32768
#define HAMMER2_IOHASH_MASK		(HAMMER2_IOHASH_SIZE - 1)

#define HAMMER2_INUMHASH_SIZE		32768
#define HAMMER2_INUMHASH_MASK		(HAMMER2_IOHASH_SIZE - 1)

/*
 * The chain structure tracks a portion of the media topology from the
 * root (volume) down.  Chains represent volumes, inodes, indirect blocks,
 * data blocks, and freemap nodes and leafs.
 *
 * The chain structure utilizes a simple singly-homed topology and the
 * chain's in-memory topology will move around as the chains do, due mainly
 * to renames and indirect block creation.
 *
 * Block Table Updates
 *
 *	Block table updates for insertions and updates are delayed until the
 *	flush.  This allows us to avoid having to modify the parent chain
 *	all the way to the root.
 *
 *	Block table deletions are performed immediately (modifying the parent
 *	in the process) because the flush code uses the chain structure to
 *	track delayed updates and the chain will be (likely) gone or moved to
 *	another location in the topology after a deletion.
 *
 *	A prior iteration of the code tried to keep the relationship intact
 *	on deletes by doing a delete-duplicate operation on the chain, but
 *	it added way too much complexity to the codebase.
 *
 * Flush Synchronization
 *
 *	The flush code must flush modified chains bottom-up.  Because chain
 *	structures can shift around and are NOT topologically stable,
 *	modified chains are independently indexed for the flush.  As the flush
 *	runs it modifies (or further modifies) and updates the parents,
 *	propagating the flush all the way to the volume root.
 *
 *	Modifying front-end operations can occur during a flush but will block
 *	in two cases: (1) when the front-end tries to operate on the inode
 *	currently in the midst of being flushed and (2) if the front-end
 *	crosses an inode currently being flushed (such as during a rename).
 *	So, for example, if you rename directory "x" to "a/b/c/d/e/f/g/x" and
 *	the flusher is currently working on "a/b/c", the rename will block
 *	temporarily in order to ensure that "x" exists in one place or the
 *	other.
 *
 *	Meta-data statistics are updated by the flusher.  The front-end will
 *	make estimates but meta-data must be fully synchronized only during a
 *	flush in order to ensure that it remains correct across a crash.
 *
 *	Multiple flush synchronizations can theoretically be in-flight at the
 *	same time but the implementation is not coded to handle the case and
 *	currently serializes them.
 *
 * Snapshots:
 *
 *	Snapshots currently require the subdirectory tree being snapshotted
 *	to be flushed.  The snapshot then creates a new super-root inode which
 *	copies the flushed blockdata of the directory or file that was
 *	snapshotted.
 *
 * Radix tree NOTES:
 *
 *	- Note that the radix tree runs in powers of 2 only so sub-trees
 *	  cannot straddle edges.
 */
RB_HEAD(hammer2_chain_tree, hammer2_chain);

struct hammer2_reptrack {
	hammer2_spin_t	spin;
	struct hammer2_reptrack *next;
	struct hammer2_chain	*chain;
};

/*
 * Core topology for chain (embedded in chain).  Protected by a spinlock.
 */
struct hammer2_chain_core {
	hammer2_spin_t	spin;
	struct hammer2_reptrack *reptrack;
	struct hammer2_chain_tree rbtree; /* sub-chains */
	int		live_zero;	/* blockref array opt */
	u_int		live_count;	/* live (not deleted) chains in tree */
	u_int		chain_count;	/* live + deleted chains under core */
	int		generation;	/* generation number (inserts only) */
};

typedef struct hammer2_chain_core hammer2_chain_core_t;

/*
 * DIO - Management structure wrapping system buffer cache.
 *
 * HAMMER2 uses an I/O abstraction that allows it to cache and manipulate
 * fixed-sized filesystem buffers frontend by variable-sized hammer2_chain
 * structures.
 */
/* #define HAMMER2_IO_DEBUG */

#ifdef HAMMER2_IO_DEBUG
#define HAMMER2_IO_DEBUG_ARGS	, const char *file, int line
#define HAMMER2_IO_DEBUG_CALL	, file, line
#define HAMMER2_IO_DEBUG_COUNT	2048
#define HAMMER2_IO_DEBUG_MASK	(HAMMER2_IO_DEBUG_COUNT - 1)
#else
#define HAMMER2_IO_DEBUG_ARGS
#define HAMMER2_IO_DEBUG_CALL
#endif

struct hammer2_io {
	struct hammer2_io  *next;
	struct hammer2_dev *hmp;
	struct vnode	*devvp;
	struct buf	*bp;
	off_t		dbase;		/* offset of devvp within volumes */
	off_t		pbase;
	uint64_t	refs;
	int		psize;
	int		act;		/* activity */
	int		btype;		/* approximate BREF_TYPE_* */
	int		ticks;
	int		error;
#ifdef HAMMER2_IO_DEBUG
	int		debug_index;
#else
	int		unused01;
#endif
	uint64_t	dedup_valid;	/* valid for dedup operation */
	uint64_t	dedup_alloc;	/* allocated / de-dupable */
#ifdef HAMMER2_IO_DEBUG
	const char	*debug_file[HAMMER2_IO_DEBUG_COUNT];
	void		*debug_td[HAMMER2_IO_DEBUG_COUNT];
	int		debug_line[HAMMER2_IO_DEBUG_COUNT];
	uint64_t	debug_refs[HAMMER2_IO_DEBUG_COUNT];
#endif
};

typedef struct hammer2_io hammer2_io_t;

struct hammer2_io_hash {
	hammer2_spin_t		spin;
	struct hammer2_io	*base;
};

typedef struct hammer2_io_hash	hammer2_io_hash_t;

#define HAMMER2_DIO_INPROG	0x8000000000000000LLU	/* bio in progress */
#define HAMMER2_DIO_GOOD	0x4000000000000000LLU	/* dio->bp is stable */
#define HAMMER2_DIO_WAITING	0x2000000000000000LLU	/* wait on INPROG */
#define HAMMER2_DIO_DIRTY	0x1000000000000000LLU	/* flush last drop */
#define HAMMER2_DIO_FLUSH	0x0800000000000000LLU	/* immediate flush */

#define HAMMER2_DIO_MASK	0x00FFFFFFFFFFFFFFLLU

struct hammer2_inum_hash {
	hammer2_spin_t		spin;
	struct hammer2_inode	*base;
};

typedef struct hammer2_inum_hash hammer2_inum_hash_t;

/*
 * Primary chain structure keeps track of the topology in-memory.
 */
struct hammer2_chain {
	hammer2_mtx_t		lock;
	hammer2_chain_core_t	core;
	RB_ENTRY(hammer2_chain) rbnode;		/* live chain(s) */
	hammer2_blockref_t	bref;
	struct hammer2_chain	*parent;
	struct hammer2_dev	*hmp;
	struct hammer2_pfs	*pmp;		/* A PFS or super-root (spmp) */

	struct lock	diolk;			/* xop focus interlock */
	hammer2_io_t	*dio;			/* physical data buffer */
	hammer2_media_data_t *data;		/* data pointer shortcut */
	u_int		bytes;			/* physical data size */
	u_int		flags;
	u_int		refs;
	u_int		lockcnt;
	int		error;			/* on-lock data error state */
	int		cache_index;		/* heur speeds up lookup */
};

typedef struct hammer2_chain hammer2_chain_t;

int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);

/*
 * Passed to hammer2_chain_create(), causes methods to be inherited from
 * parent.
 */
#define HAMMER2_METH_DEFAULT		-1

/*
 * Special notes on flags:
 *
 * INITIAL	- This flag allows a chain to be created and for storage to
 *		  be allocated without having to immediately instantiate the
 *		  related buffer.  The data is assumed to be all-zeros.  It
 *		  is primarily used for indirect blocks.
 *
 * MODIFIED	- The chain's media data has been modified.  Prevents chain
 *		  free on lastdrop if still in the topology.
 *
 * UPDATE	- Chain might not be modified but parent blocktable needs
 *		  an update.  Prevents chain free on lastdrop if still in
 *		  the topology.
 *
 * BLKMAPPED	- Indicates that the chain is present in the parent blockmap.
 *
 * BLKMAPUPD	- Indicates that the chain is present but needs to be updated
 *		  in the parent blockmap.
 */
#define HAMMER2_CHAIN_MODIFIED		0x00000001	/* dirty chain data */
#define HAMMER2_CHAIN_ALLOCATED		0x00000002	/* kmalloc'd chain */
#define HAMMER2_CHAIN_DESTROY		0x00000004
#define HAMMER2_CHAIN_DEDUPABLE		0x00000008	/* registered w/dedup */
#define HAMMER2_CHAIN_DELETED		0x00000010	/* deleted chain */
#define HAMMER2_CHAIN_INITIAL		0x00000020	/* initial create */
#define HAMMER2_CHAIN_UPDATE		0x00000040	/* need parent update */
#define HAMMER2_CHAIN_NOTTESTED		0x00000080	/* crc not generated */
#define HAMMER2_CHAIN_TESTEDGOOD	0x00000100	/* crc tested good */
#define HAMMER2_CHAIN_ONFLUSH		0x00000200	/* on a flush list */
#define HAMMER2_CHAIN_UNUSED0400	0x00000400
#define HAMMER2_CHAIN_VOLUMESYNC	0x00000800	/* needs volume sync */
#define HAMMER2_CHAIN_UNUSED1000	0x00001000
#define HAMMER2_CHAIN_COUNTEDBREFS	0x00002000	/* block table stats */
#define HAMMER2_CHAIN_ONRBTREE		0x00004000	/* on parent RB tree */
#define HAMMER2_CHAIN_UNUSED8000	0x00008000
#define HAMMER2_CHAIN_UNUSED10000	0x00010000
#define HAMMER2_CHAIN_RELEASE		0x00020000	/* don't keep around */
#define HAMMER2_CHAIN_BLKMAPPED		0x00040000	/* present in blkmap */
#define HAMMER2_CHAIN_BLKMAPUPD		0x00080000	/* +needs updating */
#define HAMMER2_CHAIN_IOINPROG		0x00100000	/* I/O interlock */
#define HAMMER2_CHAIN_IOSIGNAL		0x00200000	/* I/O interlock */
#define HAMMER2_CHAIN_PFSBOUNDARY	0x00400000	/* super->pfs inode */
#define HAMMER2_CHAIN_HINT_LEAF_COUNT	0x00800000	/* redo leaf count */
#define HAMMER2_CHAIN_UNUSED1000000	0x01000000

#define HAMMER2_CHAIN_FLUSH_MASK	(HAMMER2_CHAIN_MODIFIED |	\
					 HAMMER2_CHAIN_UPDATE |		\
					 HAMMER2_CHAIN_ONFLUSH |	\
					 HAMMER2_CHAIN_DESTROY)

/*
 * Hammer2 error codes, used by chain->error and cluster->error.  The error
 * code is typically set on-lock unless no I/O was requested, and set on
 * I/O otherwise.  If set for a cluster it generally means that the cluster
 * code could not find a valid copy to present.
 *
 * All H2 error codes are flags and can be accumulated by ORing them
 * together.
 *
 * IO		- An I/O error occurred
 * CHECK	- I/O succeeded but did not match the check code
 * INCOMPLETE	- A cluster is not complete enough to use, or
 *		  a chain cannot be loaded because its parent has an error.
 *
 * NOTE: API allows callers to check zero/non-zero to determine if an error
 *	 condition exists.
 *
 * NOTE: Chain's data field is usually NULL on an IO error but not necessarily
 *	 NULL on other errors.  Check chain->error, not chain->data.
 */
#define HAMMER2_ERROR_NONE		0	/* no error (must be 0) */
#define HAMMER2_ERROR_EIO		0x00000001	/* device I/O error */
#define HAMMER2_ERROR_CHECK		0x00000002	/* check code error */
#define HAMMER2_ERROR_INCOMPLETE	0x00000004	/* incomplete cluster */
#define HAMMER2_ERROR_DEPTH		0x00000008	/* tmp depth limit */
#define HAMMER2_ERROR_BADBREF		0x00000010	/* illegal bref */
#define HAMMER2_ERROR_ENOSPC		0x00000020	/* allocation failure */
#define HAMMER2_ERROR_ENOENT		0x00000040	/* entry not found */
#define HAMMER2_ERROR_ENOTEMPTY		0x00000080	/* dir not empty */
#define HAMMER2_ERROR_EAGAIN		0x00000100	/* retry */
#define HAMMER2_ERROR_ENOTDIR		0x00000200	/* not directory */
#define HAMMER2_ERROR_EISDIR		0x00000400	/* is directory */
#define HAMMER2_ERROR_EINPROGRESS	0x00000800	/* already running */
#define HAMMER2_ERROR_ABORTED		0x00001000	/* aborted operation */
#define HAMMER2_ERROR_EOF		0x00002000	/* end of scan */
#define HAMMER2_ERROR_EINVAL		0x00004000	/* catch-all */
#define HAMMER2_ERROR_EEXIST		0x00008000	/* entry exists */
#define HAMMER2_ERROR_EDEADLK		0x00010000
#define HAMMER2_ERROR_ESRCH		0x00020000
#define HAMMER2_ERROR_ETIMEDOUT		0x00040000

/*
 * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
 *
 * NOTES:
 *	NODATA	    - Asks that the chain->data not be resolved in order
 *		      to avoid I/O.
 *
 *	NODIRECT    - Prevents a lookup of offset 0 in an inode from returning
 *		      the inode itself if the inode is in DIRECTDATA mode
 *		      (i.e. file is <= 512 bytes).  Used by the synchronization
 *		      code to prevent confusion.
 *
 *	SHARED	    - The input chain is expected to be locked shared,
 *		      and the output chain is locked shared.
 *
 *	MATCHIND    - Allows an indirect block / freemap node to be returned
 *		      when the passed key range matches the radix.  Remember
 *		      that key_end is inclusive (e.g. {0x000,0xFFF},
 *		      not {0x000,0x1000}).
 *
 *		      (Cannot be used for remote or cluster ops).
 *
 *	ALWAYS	    - Always resolve the data.  If ALWAYS and NODATA are both
 *		      missing, bulk file data is not resolved but inodes and
 *		      other meta-data will.
 */
#define HAMMER2_LOOKUP_UNUSED0001	0x00000001
#define HAMMER2_LOOKUP_NODATA		0x00000002	/* data left NULL */
#define HAMMER2_LOOKUP_NODIRECT		0x00000004	/* no offset=0 DD */
#define HAMMER2_LOOKUP_SHARED		0x00000100
#define HAMMER2_LOOKUP_MATCHIND		0x00000200	/* return all chains */
#define HAMMER2_LOOKUP_UNUSED0400	0x00000400
#define HAMMER2_LOOKUP_ALWAYS		0x00000800	/* resolve data */
#define HAMMER2_LOOKUP_UNUSED1000	0x00001000

/*
 * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
 *
 * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
 *	 blocks in the INITIAL-create state.
 */
#define HAMMER2_MODIFY_OPTDATA		0x00000002	/* data can be NULL */

/*
 * Flags passed to hammer2_chain_lock()
 *
 * NOTE: NONBLOCK is only used for hammer2_chain_repparent() and getparent(),
 *	 other functions (e.g. hammer2_chain_lookup(), etc) can't handle its
 *	 operation.
 */
#define HAMMER2_RESOLVE_NEVER		1
#define HAMMER2_RESOLVE_MAYBE		2
#define HAMMER2_RESOLVE_ALWAYS		3
#define HAMMER2_RESOLVE_MASK		0x0F

#define HAMMER2_RESOLVE_SHARED		0x10	/* request shared lock */
#define HAMMER2_RESOLVE_LOCKAGAIN	0x20	/* another shared lock */
#define HAMMER2_RESOLVE_UNUSED40	0x40
#define HAMMER2_RESOLVE_NONBLOCK	0x80	/* non-blocking */

/*
 * Flags passed to hammer2_chain_delete()
 */
#define HAMMER2_DELETE_PERMANENT	0x0001

/*
 * Flags passed to hammer2_chain_insert() or hammer2_chain_rename()
 * or hammer2_chain_create().
 */
#define HAMMER2_INSERT_PFSROOT		0x0004
#define HAMMER2_INSERT_SAMEPARENT	0x0008

/*
 * hammer2_freemap_adjust()
 */
#define HAMMER2_FREEMAP_DORECOVER	1
#if 0
#define HAMMER2_FREEMAP_DOMAYFREE	2
#define HAMMER2_FREEMAP_DOREALFREE	3
#endif

/*
 * HAMMER2 cluster - A set of chains representing the same entity.
 *
 * hammer2_cluster typically represents a temporary set of representitive
 * chains.  The one exception is that a hammer2_cluster is embedded in
 * hammer2_inode.  This embedded cluster is ONLY used to track the
 * representitive chains and cannot be directly locked.
 *
 * A cluster is usually temporary (and thus per-thread) for locking purposes,
 * allowing us to embed the asynchronous storage required for cluster
 * operations in the cluster itself and adjust the state and status without
 * having to worry too much about SMP issues.
 *
 * The exception is the cluster embedded in the hammer2_inode structure.
 * This is used to cache the cluster state on an inode-by-inode basis.
 * Individual hammer2_chain structures not incorporated into clusters might
 * also stick around to cache miscellanious elements.
 *
 * Because the cluster is a 'working copy' and is usually subject to cluster
 * quorum rules, it is quite possible for us to end up with an insufficient
 * number of live chains to execute an operation.  If an insufficient number
 * of chains remain in a working copy, the operation may have to be
 * downgraded, retried, stall until the requisit number of chains are
 * available, or possibly even error out depending on the mount type.
 *
 * A cluster's focus is set when it is locked.  The focus can only be set
 * to a chain still part of the synchronized set.
 */
#define HAMMER2_XOPFIFO		16
#define HAMMER2_XOPFIFO_MASK	(HAMMER2_XOPFIFO - 1)
#define HAMMER2_XOPTHREADS_MIN	32
#define HAMMER2_XOPGROUPS_MIN	4

#define HAMMER2_MAXCLUSTER	8
#define HAMMER2_XOPMASK_CLUSTER	((uint64_t)((1LLU << HAMMER2_MAXCLUSTER) - 1))
#define HAMMER2_XOPMASK_VOP	((uint64_t)0x0000000080000000LLU)
#define HAMMER2_XOPMASK_FIFOW	((uint64_t)0x0000000040000000LLU)
#define HAMMER2_XOPMASK_WAIT	((uint64_t)0x0000000020000000LLU)
#define HAMMER2_XOPMASK_FEED	((uint64_t)0x0000000100000000LLU)

#define HAMMER2_XOPMASK_ALLDONE	(HAMMER2_XOPMASK_VOP | HAMMER2_XOPMASK_CLUSTER)

struct hammer2_cluster_item {
	hammer2_chain_t		*chain;
	int			error;
	uint32_t		flags;
};

typedef struct hammer2_cluster_item hammer2_cluster_item_t;

/*
 * INVALID	- Invalid for focus, i.e. not part of synchronized set.
 *		  Once set, this bit is sticky across operations.
 *
 * FEMOD	- Indicates that front-end modifying operations can
 *		  mess with this entry and MODSYNC will copy also
 *		  effect it.
 */
#define HAMMER2_CITEM_INVALID	0x00000001
#define HAMMER2_CITEM_FEMOD	0x00000002
#define HAMMER2_CITEM_NULL	0x00000004

struct hammer2_cluster {
	int			refs;		/* track for deallocation */
	int			ddflag;
	struct hammer2_pfs	*pmp;
	uint32_t		flags;
	int			nchains;
	int			error;		/* error code valid on lock */
	int			focus_index;
	hammer2_chain_t		*focus;		/* current focus (or mod) */
	hammer2_cluster_item_t	array[HAMMER2_MAXCLUSTER];
};

typedef struct hammer2_cluster	hammer2_cluster_t;

/*
 * WRHARD	- Hard mounts can write fully synchronized
 * RDHARD	- Hard mounts can read fully synchronized
 * UNHARD	- Unsynchronized masters present
 * NOHARD	- No masters visible
 * WRSOFT	- Soft mounts can write to at least the SOFT_MASTER
 * RDSOFT	- Soft mounts can read from at least a SOFT_SLAVE
 * UNSOFT	- Unsynchronized slaves present
 * NOSOFT	- No slaves visible
 * RDSLAVE	- slaves are accessible (possibly unsynchronized or remote).
 * MSYNCED	- All masters are fully synchronized
 * SSYNCED	- All known local slaves are fully synchronized to masters
 *
 * All available masters are always incorporated.  All PFSs belonging to a
 * cluster (master, slave, copy, whatever) always try to synchronize the
 * total number of known masters in the PFSs root inode.
 *
 * A cluster might have access to many slaves, copies, or caches, but we
 * have a limited number of cluster slots.  Any such elements which are
 * directly mounted from block device(s) will always be incorporated.   Note
 * that SSYNCED only applies to such elements which are directly mounted,
 * not to any remote slaves, copies, or caches that could be available.  These
 * bits are used to monitor and drive our synchronization threads.
 *
 * When asking the question 'is any data accessible at all', then a simple
 * test against (RDHARD|RDSOFT|RDSLAVE) gives you the answer.  If any of
 * these bits are set the object can be read with certain caveats:
 * RDHARD - no caveats.  RDSOFT - authoritative but might not be synchronized.
 * and RDSLAVE - not authoritative, has some data but it could be old or
 * incomplete.
 *
 * When both soft and hard mounts are available, data will be read and written
 * via the soft mount only.  But all might be in the cluster because
 * background synchronization threads still need to do their work.
 */
#define HAMMER2_CLUSTER_INODE	0x00000001	/* embedded in inode struct */
#define HAMMER2_CLUSTER_UNUSED2	0x00000002
#define HAMMER2_CLUSTER_LOCKED	0x00000004	/* cluster lks not recursive */
#define HAMMER2_CLUSTER_WRHARD	0x00000100	/* hard-mount can write */
#define HAMMER2_CLUSTER_RDHARD	0x00000200	/* hard-mount can read */
#define HAMMER2_CLUSTER_UNHARD	0x00000400	/* unsynchronized masters */
#define HAMMER2_CLUSTER_NOHARD	0x00000800	/* no masters visible */
#define HAMMER2_CLUSTER_WRSOFT	0x00001000	/* soft-mount can write */
#define HAMMER2_CLUSTER_RDSOFT	0x00002000	/* soft-mount can read */
#define HAMMER2_CLUSTER_UNSOFT	0x00004000	/* unsynchronized slaves */
#define HAMMER2_CLUSTER_NOSOFT	0x00008000	/* no slaves visible */
#define HAMMER2_CLUSTER_MSYNCED	0x00010000	/* all masters synchronized */
#define HAMMER2_CLUSTER_SSYNCED	0x00020000	/* known slaves synchronized */

#define HAMMER2_CLUSTER_ANYDATA	( HAMMER2_CLUSTER_RDHARD |	\
				  HAMMER2_CLUSTER_RDSOFT |	\
				  HAMMER2_CLUSTER_RDSLAVE)
#if 0
#define HAMMER2_CLUSTER_RDOK	( HAMMER2_CLUSTER_RDHARD |	\
				  HAMMER2_CLUSTER_RDSOFT)

#define HAMMER2_CLUSTER_WROK	( HAMMER2_CLUSTER_WRHARD |	\
				  HAMMER2_CLUSTER_WRSOFT)
#endif
#define HAMMER2_CLUSTER_ZFLAGS	( HAMMER2_CLUSTER_WRHARD |	\
				  HAMMER2_CLUSTER_RDHARD |	\
				  HAMMER2_CLUSTER_WRSOFT |	\
				  HAMMER2_CLUSTER_RDSOFT |	\
				  HAMMER2_CLUSTER_MSYNCED |	\
				  HAMMER2_CLUSTER_SSYNCED)

TAILQ_HEAD(inoq_head, hammer2_inode);		/* ip->entry */
TAILQ_HEAD(depq_head, hammer2_depend);		/* depend->entry */

struct hammer2_depend {
	TAILQ_ENTRY(hammer2_depend) entry;
	struct inoq_head	sideq;
	long			count;
	int			pass2;
	int			unused01;
};

typedef struct hammer2_depend hammer2_depend_t;

/*
 * A hammer2 inode.
 *
 * NOTE: The inode-embedded cluster is never used directly for I/O (since
 *	 it may be shared).  Instead it will be replicated-in and synchronized
 *	 back out if changed.
 */
struct hammer2_inode {
	struct hammer2_inode	*next;		/* inode tree */
	TAILQ_ENTRY(hammer2_inode) entry;	/* SYNCQ/SIDEQ */
	hammer2_depend_t	*depend;	/* non-NULL if SIDEQ */
	hammer2_depend_t	depend_static;	/* (in-place allocation) */
	hammer2_mtx_t		lock;		/* inode lock */
	hammer2_mtx_t		truncate_lock;	/* prevent truncates */
	struct hammer2_pfs	*pmp;		/* PFS mount */
	struct vnode		*vp;
	hammer2_spin_t		cluster_spin;	/* update cluster */
	hammer2_cluster_t	cluster;
	hammer2_cluster_item_t	ccache[HAMMER2_MAXCLUSTER];
	int			ccache_nchains;
	struct lockf		advlock;
	u_int			flags;
	u_int			refs;		/* +vpref, +flushref */
	int			ihash;		/* xop worker distribution */
	uint8_t			comp_heuristic;
	hammer2_inode_meta_t	meta;		/* copy of meta-data */
	hammer2_off_t		osize;
};

typedef struct hammer2_inode hammer2_inode_t;

/*
 * MODIFIED	- Inode is in a modified state, ip->meta may have changes.
 * RESIZED	- Inode truncated (any) or inode extended beyond
 *		  EMBEDDED_BYTES.
 *
 * SYNCQ	- Inode is included in the current filesystem sync.  The
 *		  DELETING and CREATING flags will be acted upon.
 *
 * SIDEQ	- Inode has likely been disconnected from the vnode topology
 *		  and so is not visible to the vnode-based filesystem syncer
 *		  code, but is dirty and must be included in the next
 *		  filesystem sync.  These inodes are moved to the SYNCQ at
 *		  the time the sync occurs.
 *
 *		  Inodes are not placed on this queue simply because they have
 *		  become dirty, if a vnode is attached.
 *
 * DELETING	- Inode is flagged for deletion during the next filesystem
 *		  sync.  That is, the inode's chain is currently connected
 *		  and must be deleting during the current or next fs sync.
 *
 * CREATING	- Inode is flagged for creation during the next filesystem
 *		  sync.  That is, the inode's chain topology exists (so
 *		  kernel buffer flushes can occur), but is currently
 *		  disconnected and must be inserted during the current or
 *		  next fs sync.  If the DELETING flag is also set, the
 *		  topology can be thrown away instead.
 *
 * If an inode that is already part of the current filesystem sync is
 * modified by the frontend, including by buffer flushes, the inode lock
 * code detects the SYNCQ flag and moves the inode to the head of the
 * flush-in-progress, then blocks until the flush has gotten past it.
 */
#define HAMMER2_INODE_MODIFIED		0x0001
#define HAMMER2_INODE_UNUSED0002	0x0002
#define HAMMER2_INODE_UNUSED0004	0x0004
#define HAMMER2_INODE_ONHASH		0x0008
#define HAMMER2_INODE_RESIZED		0x0010	/* requires inode_chain_sync */
#define HAMMER2_INODE_UNUSED0020	0x0020
#define HAMMER2_INODE_ISUNLINKED	0x0040
#define HAMMER2_INODE_UNUSED0080	0x0080
#define HAMMER2_INODE_SIDEQ		0x0100	/* on side processing queue */
#define HAMMER2_INODE_NOSIDEQ		0x0200	/* disable sideq operation */
#define HAMMER2_INODE_DIRTYDATA		0x0400	/* interlocks inode flush */
#define HAMMER2_INODE_SYNCQ		0x0800	/* sync interlock, sequenced */
#define HAMMER2_INODE_DELETING		0x1000	/* sync interlock, chain topo */
#define HAMMER2_INODE_CREATING		0x2000	/* sync interlock, chain topo */
#define HAMMER2_INODE_SYNCQ_WAKEUP	0x4000	/* sync interlock wakeup */
#define HAMMER2_INODE_SYNCQ_PASS2	0x8000	/* force retry delay */

#define HAMMER2_INODE_DIRTY		(HAMMER2_INODE_MODIFIED |	\
					 HAMMER2_INODE_DIRTYDATA |	\
					 HAMMER2_INODE_DELETING |	\
					 HAMMER2_INODE_CREATING)

/*
 * Transaction management sub-structure under hammer2_pfs
 */
struct hammer2_trans {
	uint32_t		flags;
	uint32_t		sync_wait;
};

typedef struct hammer2_trans hammer2_trans_t;

#define HAMMER2_TRANS_ISFLUSH		0x80000000	/* flush code */
#define HAMMER2_TRANS_BUFCACHE		0x40000000	/* bio strategy */
#define HAMMER2_TRANS_SIDEQ		0x20000000	/* run sideq */
#define HAMMER2_TRANS_UNUSED10		0x10000000
#define HAMMER2_TRANS_WAITING		0x08000000	/* someone waiting */
#define HAMMER2_TRANS_RESCAN		0x04000000	/* rescan sideq */
#define HAMMER2_TRANS_MASK		0x00FFFFFF	/* count mask */

#define HAMMER2_FREEMAP_HEUR_NRADIX	4	/* pwr 2 PBUFRADIX-LBUFRADIX */
#define HAMMER2_FREEMAP_HEUR_TYPES	8
#define HAMMER2_FREEMAP_HEUR_SIZE	(HAMMER2_FREEMAP_HEUR_NRADIX * \
					 HAMMER2_FREEMAP_HEUR_TYPES)

#define HAMMER2_DEDUP_HEUR_SIZE		(65536 * 4)
#define HAMMER2_DEDUP_HEUR_MASK		(HAMMER2_DEDUP_HEUR_SIZE - 1)

#define HAMMER2_FLUSH_TOP		0x0001
#define HAMMER2_FLUSH_ALL		0x0002
#define HAMMER2_FLUSH_INODE_STOP	0x0004	/* stop at sub-inode */
#define HAMMER2_FLUSH_FSSYNC		0x0008	/* part of filesystem sync */


/*
 * Hammer2 support thread element.
 *
 * Potentially many support threads can hang off of hammer2, primarily
 * off the hammer2_pfs structure.  Typically:
 *
 * td x Nodes		 	A synchronization thread for each node.
 * td x Nodes x workers		Worker threads for frontend operations.
 * td x 1			Bioq thread for logical buffer writes.
 *
 * In addition, the synchronization thread(s) associated with the
 * super-root PFS (spmp) for a node is responsible for automatic bulkfree
 * and dedup scans.
 */
struct hammer2_thread {
	struct hammer2_pfs *pmp;
	struct hammer2_dev *hmp;
	hammer2_xop_list_t xopq;
	thread_t	td;
	uint32_t	flags;
	int		clindex;	/* cluster element index */
	int		repidx;
	char		*scratch;	/* MAXPHYS */
};

typedef struct hammer2_thread hammer2_thread_t;

#define HAMMER2_THREAD_UNMOUNTING	0x0001	/* unmount request */
#define HAMMER2_THREAD_DEV		0x0002	/* related to dev, not pfs */
#define HAMMER2_THREAD_WAITING		0x0004	/* thread in idle tsleep */
#define HAMMER2_THREAD_REMASTER		0x0008	/* remaster request */
#define HAMMER2_THREAD_STOP		0x0010	/* exit request */
#define HAMMER2_THREAD_FREEZE		0x0020	/* force idle */
#define HAMMER2_THREAD_FROZEN		0x0040	/* thread is frozen */
#define HAMMER2_THREAD_XOPQ		0x0080	/* work pending */
#define HAMMER2_THREAD_STOPPED		0x0100	/* thread has stopped */
#define HAMMER2_THREAD_UNFREEZE		0x0200

#define HAMMER2_THREAD_WAKEUP_MASK	(HAMMER2_THREAD_UNMOUNTING |	\
					 HAMMER2_THREAD_REMASTER |	\
					 HAMMER2_THREAD_STOP |		\
					 HAMMER2_THREAD_FREEZE |	\
					 HAMMER2_THREAD_XOPQ)

/*
 * Support structure for dedup heuristic.
 */
struct hammer2_dedup {
	hammer2_off_t	data_off;
	uint64_t	data_crc;
	uint32_t	ticks;
	uint32_t	saved_error;
};

typedef struct hammer2_dedup hammer2_dedup_t;

/*
 * hammer2_xop - container for VOP/XOP operation (allocated, not on stack).
 *
 * This structure is used to distribute a VOP operation across multiple
 * nodes.  It provides a rendezvous for concurrent node execution and
 * can be detached from the frontend operation to allow the frontend to
 * return early.
 *
 * This structure also sequences operations on up to three inodes.
 */
typedef void (*hammer2_xop_func_t)(union hammer2_xop *xop, void *scratch,
				   int clindex);

struct hammer2_xop_desc {
	hammer2_xop_func_t	storage_func;	/* local storage function */
	hammer2_xop_func_t	dmsg_dispatch;	/* dmsg dispatch function */
	hammer2_xop_func_t	dmsg_process;	/* dmsg processing function */
	const char		*id;
};

typedef struct hammer2_xop_desc hammer2_xop_desc_t;

struct hammer2_xop_fifo {
	TAILQ_ENTRY(hammer2_xop_head) entry;
	hammer2_chain_t		*array[HAMMER2_XOPFIFO];
	int			errors[HAMMER2_XOPFIFO];
	int			ri;
	int			wi;
	int			flags;
	hammer2_thread_t	*thr;
};

typedef struct hammer2_xop_fifo hammer2_xop_fifo_t;

#define HAMMER2_XOP_FIFO_RUN	0x0001
#define HAMMER2_XOP_FIFO_STALL	0x0002

struct hammer2_xop_head {
	hammer2_xop_desc_t	*desc;
	hammer2_tid_t		mtid;
	struct hammer2_inode	*ip1;
	struct hammer2_inode	*ip2;
	struct hammer2_inode	*ip3;
	struct hammer2_inode	*ip4;
	uint64_t		run_mask;
	uint64_t		chk_mask;
	int			flags;
	int			state;
	int			error;
	hammer2_key_t		collect_key;
	char			*name1;
	size_t			name1_len;
	char			*name2;
	size_t			name2_len;
	hammer2_xop_fifo_t	collect[HAMMER2_MAXCLUSTER];
	hammer2_cluster_t	cluster;	/* help collections */
	hammer2_io_t		*focus_dio;
};

typedef struct hammer2_xop_head hammer2_xop_head_t;

struct hammer2_xop_ipcluster {
	hammer2_xop_head_t	head;
};

struct hammer2_xop_strategy {
	hammer2_xop_head_t	head;
	hammer2_key_t		lbase;
	int			finished;
	hammer2_mtx_t		lock;
	struct bio		*bio;
};

struct hammer2_xop_readdir {
	hammer2_xop_head_t	head;
	hammer2_key_t		lkey;
};

struct hammer2_xop_nresolve {
	hammer2_xop_head_t	head;
	hammer2_key_t		lhc;	/* if name is NULL used lhc */
};

struct hammer2_xop_unlink {
	hammer2_xop_head_t	head;
	int			isdir;
	int			dopermanent;
};

#define H2DOPERM_PERMANENT	0x01
#define H2DOPERM_FORCE		0x02
#define H2DOPERM_IGNINO		0x04

struct hammer2_xop_nrename {
	hammer2_xop_head_t	head;
	hammer2_tid_t		lhc;
	int			ip_key;
};

struct hammer2_xop_scanlhc {
	hammer2_xop_head_t	head;
	hammer2_key_t		lhc;
};

struct hammer2_xop_scanall {
	hammer2_xop_head_t	head;
	hammer2_key_t		key_beg;	/* inclusive */
	hammer2_key_t		key_end;	/* inclusive */
	int			resolve_flags;
	int			lookup_flags;
};

struct hammer2_xop_lookup {
	hammer2_xop_head_t	head;
	hammer2_key_t		lhc;
};

struct hammer2_xop_mkdirent {
	hammer2_xop_head_t	head;
	hammer2_dirent_head_t	dirent;
	hammer2_key_t		lhc;
};

struct hammer2_xop_create {
	hammer2_xop_head_t	head;
	hammer2_inode_meta_t	meta;		/* initial metadata */
	hammer2_key_t		lhc;
	int			flags;
};

struct hammer2_xop_destroy {
	hammer2_xop_head_t	head;
};

struct hammer2_xop_fsync {
	hammer2_xop_head_t	head;
	hammer2_inode_meta_t	meta;
	hammer2_off_t		osize;
	u_int			ipflags;
	int			clear_directdata;
};

struct hammer2_xop_unlinkall {
	hammer2_xop_head_t	head;
	hammer2_key_t		key_beg;
	hammer2_key_t		key_end;
};

struct hammer2_xop_connect {
	hammer2_xop_head_t	head;
	hammer2_key_t		lhc;
};

struct hammer2_xop_flush {
	hammer2_xop_head_t	head;
};

struct hammer2_xop_bmap {
	hammer2_xop_head_t	head;
	hammer2_off_t		loffset;
	hammer2_off_t		offset;
};

typedef struct hammer2_xop_readdir hammer2_xop_readdir_t;
typedef struct hammer2_xop_nresolve hammer2_xop_nresolve_t;
typedef struct hammer2_xop_unlink hammer2_xop_unlink_t;
typedef struct hammer2_xop_nrename hammer2_xop_nrename_t;
typedef struct hammer2_xop_ipcluster hammer2_xop_ipcluster_t;
typedef struct hammer2_xop_strategy hammer2_xop_strategy_t;
typedef struct hammer2_xop_mkdirent hammer2_xop_mkdirent_t;
typedef struct hammer2_xop_create hammer2_xop_create_t;
typedef struct hammer2_xop_destroy hammer2_xop_destroy_t;
typedef struct hammer2_xop_fsync hammer2_xop_fsync_t;
typedef struct hammer2_xop_unlinkall hammer2_xop_unlinkall_t;
typedef struct hammer2_xop_scanlhc hammer2_xop_scanlhc_t;
typedef struct hammer2_xop_scanall hammer2_xop_scanall_t;
typedef struct hammer2_xop_lookup hammer2_xop_lookup_t;
typedef struct hammer2_xop_connect hammer2_xop_connect_t;
typedef struct hammer2_xop_flush hammer2_xop_flush_t;
typedef struct hammer2_xop_bmap hammer2_xop_bmap_t;

union hammer2_xop {
	hammer2_xop_head_t	head;
	hammer2_xop_ipcluster_t	xop_ipcluster;
	hammer2_xop_readdir_t	xop_readdir;
	hammer2_xop_nresolve_t	xop_nresolve;
	hammer2_xop_unlink_t	xop_unlink;
	hammer2_xop_nrename_t	xop_nrename;
	hammer2_xop_strategy_t	xop_strategy;
	hammer2_xop_mkdirent_t	xop_mkdirent;
	hammer2_xop_create_t	xop_create;
	hammer2_xop_destroy_t	xop_destroy;
	hammer2_xop_fsync_t	xop_fsync;
	hammer2_xop_unlinkall_t	xop_unlinkall;
	hammer2_xop_scanlhc_t	xop_scanlhc;
	hammer2_xop_scanall_t	xop_scanall;
	hammer2_xop_lookup_t	xop_lookup;
	hammer2_xop_flush_t	xop_flush;
	hammer2_xop_connect_t	xop_connect;
	hammer2_xop_bmap_t	xop_bmap;
};

typedef union hammer2_xop hammer2_xop_t;

/*
 * hammer2_xop_group - Manage XOP support threads.
 */
struct hammer2_xop_group {
	hammer2_thread_t	thrs[HAMMER2_MAXCLUSTER];
};

typedef struct hammer2_xop_group hammer2_xop_group_t;

/*
 * flags to hammer2_xop_collect()
 */
#define HAMMER2_XOP_COLLECT_NOWAIT	0x00000001
#define HAMMER2_XOP_COLLECT_WAITALL	0x00000002

/*
 * flags to hammer2_xop_alloc()
 *
 * MODIFYING	- This is a modifying transaction, allocate a mtid.
 */
#define HAMMER2_XOP_MODIFYING		0x00000001
#define HAMMER2_XOP_STRATEGY		0x00000002
#define HAMMER2_XOP_INODE_STOP		0x00000004
#define HAMMER2_XOP_VOLHDR		0x00000008
#define HAMMER2_XOP_FSSYNC		0x00000010

/*
 * Device vnode management structure
 */
struct hammer2_devvp {
	TAILQ_ENTRY(hammer2_devvp) entry;
	struct vnode	*devvp;		/* device vnode */
	char		*path;		/* device vnode path */
	int		open;		/* 1 if devvp open */
};

typedef struct hammer2_devvp hammer2_devvp_t;

TAILQ_HEAD(hammer2_devvp_list, hammer2_devvp);

typedef struct hammer2_devvp_list hammer2_devvp_list_t;

/*
 * Volume management structure
 */
struct hammer2_volume {
	hammer2_devvp_t *dev;		/* device vnode management */
	int		id;		/* volume id */
	hammer2_off_t	offset;		/* offset within volumes */
	hammer2_off_t	size;		/* volume size */
};

typedef struct hammer2_volume hammer2_volume_t;

/*
 * Global (per partition) management structure, represents a hard block
 * device.  Typically referenced by hammer2_chain structures when applicable.
 * Typically not used for network-managed elements.
 *
 * Note that a single hammer2_dev can be indirectly tied to multiple system
 * mount points.  There is no direct relationship.  System mounts are
 * per-cluster-id, not per-block-device, and a single hard mount might contain
 * many PFSs and those PFSs might combine together in various ways to form
 * the set of available clusters.
 */
struct hammer2_dev {
	struct vnode	*devvp;		/* device vnode for root volume */
	int		ronly;		/* read-only mount */
	int		mount_count;	/* number of actively mounted PFSs */
	TAILQ_ENTRY(hammer2_dev) mntentry; /* hammer2_mntlist */

	struct malloc_type *mchain_obj;
	struct malloc_type *mio_obj;
	struct malloc_type *mmsg;
	kdmsg_iocom_t	iocom;		/* volume-level dmsg interface */
	hammer2_io_hash_t iohash[HAMMER2_IOHASH_SIZE];
	int		iofree_count;
	int		io_iterator;
	int		freemap_relaxed;
	int		unused01;
	hammer2_chain_t vchain;		/* anchor chain (topology) */
	hammer2_chain_t fchain;		/* anchor chain (freemap) */
	hammer2_spin_t	list_spin;
	struct hammer2_pfs *spmp;	/* super-root pmp for transactions */
	struct lock	vollk;		/* lockmgr lock */
	struct lock	bulklk;		/* bulkfree operation lock */
	struct lock	bflock;		/* bulk-free manual function lock */
	hammer2_off_t	heur_freemap[HAMMER2_FREEMAP_HEUR_SIZE];
	hammer2_dedup_t heur_dedup[HAMMER2_DEDUP_HEUR_SIZE];
	int		volhdrno;	/* last volhdrno written */
	uint32_t	hflags;		/* HMNT2 flags applicable to device */
	hammer2_off_t	free_reserved;	/* nominal free reserved */
	hammer2_off_t	total_size;	/* total size of volumes */
	int		nvolumes;	/* total number of volumes */
	hammer2_thread_t bfthr;		/* bulk-free thread */
	char		devrepname[64];	/* for kprintf */
	hammer2_volume_data_t voldata;
	hammer2_volume_data_t volsync;	/* synchronized voldata */

	hammer2_devvp_list_t devvpl;	/* list of device vnodes including *devvp */
	hammer2_volume_t volumes[HAMMER2_MAX_VOLUMES]; /* list of volumes */
};

typedef struct hammer2_dev hammer2_dev_t;

/*
 * Per-cluster management structure.  This structure will be tied to a
 * system mount point if the system is mounting the PFS, but is also used
 * to manage clusters encountered during the super-root scan or received
 * via LNK_SPANs that might not be mounted.
 *
 * This structure is also used to represent the super-root that hangs off
 * of a hard mount point.  The super-root is not really a cluster element.
 * In this case the spmp_hmp field will be non-NULL.  It's just easier to do
 * this than to special case super-root manipulation in the hammer2_chain*
 * code as being only hammer2_dev-related.
 *
 * pfs_mode and pfs_nmasters are rollup fields which critically describes
 * how elements of the cluster act on the cluster.  pfs_mode is only applicable
 * when a PFS is mounted by the system.  pfs_nmasters is our best guess as to
 * how many masters have been configured for a cluster and is always
 * applicable.  pfs_types[] is an array with 1:1 correspondance to the
 * iroot cluster and describes the PFS types of the nodes making up the
 * cluster.
 *
 * WARNING! Portions of this structure have deferred initialization.  In
 *	    particular, if not mounted there will be no wthread.
 *	    umounted network PFSs will also be missing iroot and numerous
 *	    other fields will not be initialized prior to mount.
 *
 *	    Synchronization threads are chain-specific and only applicable
 *	    to local hard PFS entries.  A hammer2_pfs structure may contain
 *	    more than one when multiple hard PFSs are present on the local
 *	    machine which require synchronization monitoring.  Most PFSs
 *	    (such as snapshots) are 1xMASTER PFSs which do not need a
 *	    synchronization thread.
 *
 * WARNING! The chains making up pfs->iroot's cluster are accounted for in
 *	    hammer2_dev->mount_count when the pfs is associated with a mount
 *	    point.
 */
struct hammer2_pfs {
	struct mount		*mp;
	TAILQ_ENTRY(hammer2_pfs) mntentry;	/* hammer2_pfslist */
	uuid_t			pfs_clid;
	hammer2_dev_t		*spmp_hmp;	/* only if super-root pmp */
	hammer2_dev_t		*force_local;	/* only if 'local' mount */
	hammer2_inode_t		*iroot;		/* PFS root inode */
	uint8_t			pfs_types[HAMMER2_MAXCLUSTER];
	char			*pfs_names[HAMMER2_MAXCLUSTER];
	hammer2_dev_t		*pfs_hmps[HAMMER2_MAXCLUSTER];
	hammer2_blockset_t	pfs_iroot_blocksets[HAMMER2_MAXCLUSTER];
	hammer2_spin_t          blockset_spin;
	hammer2_trans_t		trans;
	struct lock		lock;		/* PFS lock for certain ops */
	struct netexport	export;		/* nfs export */
	int			unused00;
	int			ronly;		/* read-only mount */
	int			hflags;		/* pfs-specific mount flags */
	struct malloc_type	*minode_obj;
	/* note: inumhash not applicable to spmp */
	hammer2_inum_hash_t inumhash[HAMMER2_INUMHASH_SIZE];
	long			inum_count;	/* #of inodes in inumhash */
	int			flags;
	hammer2_tid_t		modify_tid;	/* modify transaction id */
	hammer2_tid_t		inode_tid;	/* inode allocator */
	uint8_t			pfs_nmasters;	/* total masters */
	uint8_t			pfs_mode;	/* operating mode PFSMODE */
	uint8_t			unused01;
	uint8_t			unused02;
	int			free_ticks;	/* free_* calculations */
	long			inmem_inodes;
	hammer2_off_t		free_reserved;
	hammer2_off_t		free_nominal;
	uint32_t		inmem_dirty_chains;
	int			count_lwinprog;	/* logical write in prog */
	hammer2_spin_t		list_spin;
	struct inoq_head	syncq;		/* SYNCQ flagged inodes */
	struct depq_head	depq;		/* SIDEQ flagged inodes */
	long			sideq_count;	/* total inodes on depq */
	hammer2_thread_t	sync_thrs[HAMMER2_MAXCLUSTER];
	uint32_t		cluster_flags;	/* cached cluster flags */
	int			has_xop_threads;
	hammer2_spin_t		xop_spin;	/* xop sequencer */
	hammer2_xop_group_t	*xop_groups;
};

typedef struct hammer2_pfs hammer2_pfs_t;

TAILQ_HEAD(hammer2_pfslist, hammer2_pfs);

/*
 * pmp->flags
 */
#define HAMMER2_PMPF_SPMP	0x00000001
#define HAMMER2_PMPF_EMERG	0x00000002	/* Emergency delete mode */

#define HAMMER2_DIRTYCHAIN_WAITING	0x80000000
#define HAMMER2_DIRTYCHAIN_MASK		0x7FFFFFFF

#define HAMMER2_LWINPROG_WAITING	0x80000000
#define HAMMER2_LWINPROG_WAITING0	0x40000000
#define HAMMER2_LWINPROG_MASK		0x3FFFFFFF

/*
 * hammer2_cluster_check
 */
#define HAMMER2_CHECK_NULL	0x00000001

/*
 * Misc
 */
#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
#define VTOI(vp)	((hammer2_inode_t *)(vp)->v_data)
#endif

#if defined(_KERNEL)

#ifdef MALLOC_DECLARE
MALLOC_DECLARE(M_HAMMER2);
#endif

static __inline
hammer2_pfs_t *
MPTOPMP(struct mount *mp)
{
	return ((hammer2_pfs_t *)mp->mnt_data);
}

#define HAMMER2_DEDUP_FRAG      (HAMMER2_PBUFSIZE / 64)
#define HAMMER2_DEDUP_FRAGRADIX (HAMMER2_PBUFRADIX - 6)

static __inline
uint64_t
hammer2_dedup_mask(hammer2_io_t *dio, hammer2_off_t data_off, u_int bytes)
{
	int bbeg;
	int bits;
	uint64_t mask;

	bbeg = (int)((data_off & ~HAMMER2_OFF_MASK_RADIX) - dio->pbase) >>
	       HAMMER2_DEDUP_FRAGRADIX;
	bits = (int)((bytes + (HAMMER2_DEDUP_FRAG - 1)) >>
	       HAMMER2_DEDUP_FRAGRADIX);
	if (bbeg + bits == 64)
		mask = (uint64_t)-1;
	else
		mask = ((uint64_t)1 << (bbeg + bits)) - 1;

	mask &= ~(((uint64_t)1 << bbeg) - 1);

	return mask;
}

static __inline
int
hammer2_error_to_errno(int error)
{
	if (error) {
		if (error & HAMMER2_ERROR_EIO)
			error = EIO;
		else if (error & HAMMER2_ERROR_CHECK)
			error = EDOM;
		else if (error & HAMMER2_ERROR_ABORTED)
			error = EINTR;
		else if (error & HAMMER2_ERROR_BADBREF)
			error = EIO;
		else if (error & HAMMER2_ERROR_ENOSPC)
			error = ENOSPC;
		else if (error & HAMMER2_ERROR_ENOENT)
			error = ENOENT;
		else if (error & HAMMER2_ERROR_ENOTEMPTY)
			error = ENOTEMPTY;
		else if (error & HAMMER2_ERROR_EAGAIN)
			error = EAGAIN;
		else if (error & HAMMER2_ERROR_ENOTDIR)
			error = ENOTDIR;
		else if (error & HAMMER2_ERROR_EISDIR)
			error = EISDIR;
		else if (error & HAMMER2_ERROR_EINPROGRESS)
			error = EINPROGRESS;
		else if (error & HAMMER2_ERROR_EEXIST)
			error = EEXIST;
		else if (error & HAMMER2_ERROR_EINVAL)
			error = EINVAL;
		else if (error & HAMMER2_ERROR_EDEADLK)
			error = EDEADLK;
		else if (error & HAMMER2_ERROR_ESRCH)
			error = ESRCH;
		else if (error & HAMMER2_ERROR_ETIMEDOUT)
			error = ETIMEDOUT;
		else
			error = EDOM;
	}
	return error;
}

static __inline
int
hammer2_errno_to_error(int error)
{
	switch(error) {
	case 0:
		return 0;
	case EIO:
		return HAMMER2_ERROR_EIO;
	case EDOM:
		return HAMMER2_ERROR_CHECK;
	case EINTR:
		return HAMMER2_ERROR_ABORTED;
	//case EIO:
	//	return HAMMER2_ERROR_BADBREF;
	case ENOSPC:
		return HAMMER2_ERROR_ENOSPC;
	case ENOENT:
		return HAMMER2_ERROR_ENOENT;
	case ENOTEMPTY:
		return HAMMER2_ERROR_ENOTEMPTY;
	case EAGAIN:
		return HAMMER2_ERROR_EAGAIN;
	case ENOTDIR:
		return HAMMER2_ERROR_ENOTDIR;
	case EISDIR:
		return HAMMER2_ERROR_EISDIR;
	case EINPROGRESS:
		return HAMMER2_ERROR_EINPROGRESS;
	case EEXIST:
		return HAMMER2_ERROR_EEXIST;
	case EINVAL:
		return HAMMER2_ERROR_EINVAL;
	case EDEADLK:
		return HAMMER2_ERROR_EDEADLK;
	case ESRCH:
		return HAMMER2_ERROR_ESRCH;
	case ETIMEDOUT:
		return HAMMER2_ERROR_ETIMEDOUT;
	default:
		return HAMMER2_ERROR_EINVAL;
	}
}


extern struct vop_ops hammer2_vnode_vops;
extern struct vop_ops hammer2_spec_vops;
extern struct vop_ops hammer2_fifo_vops;
extern struct hammer2_pfslist hammer2_pfslist;
extern struct lock hammer2_mntlk;

extern int hammer2_aux_flags;
extern int hammer2_debug;
extern int hammer2_xop_nthreads;
extern int hammer2_xop_sgroups;
extern int hammer2_xop_xgroups;
extern int hammer2_xop_xbase;
extern int hammer2_xop_mod;
extern long hammer2_debug_inode;
extern int hammer2_cluster_meta_read;
extern int hammer2_cluster_data_read;
extern int hammer2_cluster_write;
extern int hammer2_dedup_enable;
extern int hammer2_always_compress;
extern int hammer2_flush_pipe;
extern int hammer2_dio_count;
extern int hammer2_dio_limit;
extern int hammer2_bulkfree_tps;
extern int hammer2_spread_workers;
extern int hammer2_limit_saved_depth;
extern long hammer2_chain_allocs;
extern long hammer2_limit_saved_chains;
extern long hammer2_limit_dirty_chains;
extern long hammer2_limit_dirty_inodes;
extern long hammer2_count_modified_chains;
extern long hammer2_iod_file_read;
extern long hammer2_iod_meta_read;
extern long hammer2_iod_indr_read;
extern long hammer2_iod_fmap_read;
extern long hammer2_iod_volu_read;
extern long hammer2_iod_file_write;
extern long hammer2_iod_file_wembed;
extern long hammer2_iod_file_wzero;
extern long hammer2_iod_file_wdedup;
extern long hammer2_iod_meta_write;
extern long hammer2_iod_indr_write;
extern long hammer2_iod_fmap_write;
extern long hammer2_iod_volu_write;

extern long hammer2_process_icrc32;
extern long hammer2_process_xxhash64;

extern struct objcache *cache_buffer_read;
extern struct objcache *cache_buffer_write;
extern struct objcache *cache_xops;

/*
 * hammer2_subr.c
 */
#define hammer2_icrc32(buf, size)	iscsi_crc32((buf), (size))
#define hammer2_icrc32c(buf, size, crc)	iscsi_crc32_ext((buf), (size), (crc))

int hammer2_signal_check(time_t *timep);
const char *hammer2_error_str(int error);
const char *hammer2_bref_type_str(int btype);

int hammer2_get_dtype(uint8_t type);
int hammer2_get_vtype(uint8_t type);
uint8_t hammer2_get_obj_type(enum vtype vtype);
void hammer2_time_to_timespec(uint64_t xtime, struct timespec *ts);
uint64_t hammer2_timespec_to_time(const struct timespec *ts);
uint32_t hammer2_to_unix_xid(const uuid_t *uuid);
void hammer2_guid_to_uuid(uuid_t *uuid, uint32_t guid);

hammer2_key_t hammer2_dirhash(const char *aname, size_t len);
int hammer2_getradix(size_t bytes);

int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
			hammer2_key_t *lbasep, hammer2_key_t *leofp);
int hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase);
void hammer2_update_time(uint64_t *timep);
void hammer2_adjreadcounter(int btype, size_t bytes);
void hammer2_adjwritecounter(int btype, size_t bytes);

/*
 * hammer2_inode.c
 */
void hammer2_inum_hash_init(hammer2_pfs_t *pmp);
struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
hammer2_inode_t *hammer2_inode_lookup(hammer2_pfs_t *pmp,
			hammer2_tid_t inum);
hammer2_inode_t *hammer2_inode_get(hammer2_pfs_t *pmp,
			hammer2_xop_head_t *xop, hammer2_tid_t inum, int idx);
void hammer2_inode_ref(hammer2_inode_t *ip);
void hammer2_inode_drop(hammer2_inode_t *ip);
void hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_cluster_t *cluster);
void hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
			int idx);
hammer2_key_t hammer2_inode_data_count(const hammer2_inode_t *ip);
hammer2_key_t hammer2_inode_inode_count(const hammer2_inode_t *ip);
void hammer2_inode_modify(hammer2_inode_t *ip);
void hammer2_inode_delayed_sideq(hammer2_inode_t *ip);
void hammer2_inode_lock(hammer2_inode_t *ip, int how);
void hammer2_inode_lock4(hammer2_inode_t *ip1, hammer2_inode_t *ip2,
			hammer2_inode_t *ip3, hammer2_inode_t *ip4);
void hammer2_inode_unlock(hammer2_inode_t *ip);
void hammer2_inode_depend(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
hammer2_chain_t *hammer2_inode_chain(hammer2_inode_t *ip, int clindex, int how);
hammer2_chain_t *hammer2_inode_chain_and_parent(hammer2_inode_t *ip,
			int clindex, hammer2_chain_t **parentp, int how);
hammer2_mtx_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
void hammer2_inode_lock_temp_restore(hammer2_inode_t *ip,
			hammer2_mtx_state_t ostate);
int hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
void hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int);

hammer2_inode_t *hammer2_inode_create_normal(hammer2_inode_t *pip,
			struct vattr *vap, struct ucred *cred,
			hammer2_key_t inum, int *errorp);
hammer2_inode_t *hammer2_inode_create_pfs(hammer2_pfs_t *spmp,
			const char *name, size_t name_len,
			int *errorp);
int hammer2_inode_chain_ins(hammer2_inode_t *ip);
int hammer2_inode_chain_des(hammer2_inode_t *ip);
int hammer2_inode_chain_sync(hammer2_inode_t *ip);
int hammer2_inode_chain_flush(hammer2_inode_t *ip, int flags);
int hammer2_inode_unlink_finisher(hammer2_inode_t *ip, struct vnode **vpp);
void hammer2_inode_vprecycle(struct vnode *vp);
int hammer2_dirent_create(hammer2_inode_t *dip, const char *name,
			size_t name_len, hammer2_key_t inum, uint8_t type);

/*
 * hammer2_chain.c
 */
hammer2_chain_t *hammer2_chain_alloc(hammer2_dev_t *hmp,
				hammer2_pfs_t *pmp,
				hammer2_blockref_t *bref);
void hammer2_chain_init(hammer2_chain_t *chain);
void hammer2_chain_ref(hammer2_chain_t *chain);
void hammer2_chain_ref_hold(hammer2_chain_t *chain);
void hammer2_chain_drop(hammer2_chain_t *chain);
void hammer2_chain_drop_unhold(hammer2_chain_t *chain);
void hammer2_chain_unhold(hammer2_chain_t *chain);
void hammer2_chain_rehold(hammer2_chain_t *chain);
int hammer2_chain_lock(hammer2_chain_t *chain, int how);
//void hammer2_chain_lock_unhold(hammer2_chain_t *chain, int how);
void hammer2_chain_load_data(hammer2_chain_t *chain);

int hammer2_chain_inode_find(hammer2_pfs_t *pmp, hammer2_key_t inum,
				int clindex, int flags,
				hammer2_chain_t **parentp,
				hammer2_chain_t **chainp);
int hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
				hammer2_off_t dedup_off, int flags);
int hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
				hammer2_tid_t mtid, int flags);
int hammer2_chain_resize(hammer2_chain_t *chain,
				hammer2_tid_t mtid, hammer2_off_t dedup_off,
				int nradix, int flags);
void hammer2_chain_unlock(hammer2_chain_t *chain);
//void hammer2_chain_unlock_hold(hammer2_chain_t *chain);
hammer2_chain_t *hammer2_chain_get(hammer2_chain_t *parent, int generation,
				hammer2_blockref_t *bref, int how);
hammer2_chain_t *hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags);
void hammer2_chain_lookup_done(hammer2_chain_t *parent);
hammer2_chain_t *hammer2_chain_getparent(hammer2_chain_t *chain, int flags);
hammer2_chain_t *hammer2_chain_repparent(hammer2_chain_t **chainp, int flags);
hammer2_chain_t *hammer2_chain_lookup(hammer2_chain_t **parentp,
				hammer2_key_t *key_nextp,
				hammer2_key_t key_beg, hammer2_key_t key_end,
				int *errorp, int flags);
hammer2_chain_t *hammer2_chain_next(hammer2_chain_t **parentp,
				hammer2_chain_t *chain,
				hammer2_key_t *key_nextp,
				hammer2_key_t key_beg, hammer2_key_t key_end,
				int *errorp, int flags);
int hammer2_chain_scan(hammer2_chain_t *parent,
				hammer2_chain_t **chainp,
				hammer2_blockref_t *bref,
				int *firstp, int flags);

int hammer2_chain_create(hammer2_chain_t **parentp, hammer2_chain_t **chainp,
				hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
				int methods, hammer2_key_t key, int keybits,
				int type, size_t bytes, hammer2_tid_t mtid,
				hammer2_off_t dedup_off, int flags);
void hammer2_chain_rename(hammer2_chain_t **parentp,
				hammer2_chain_t *chain,
				hammer2_tid_t mtid, int flags);
int hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
				hammer2_tid_t mtid, int flags);
int hammer2_chain_indirect_maintenance(hammer2_chain_t *parent,
				hammer2_chain_t *chain);
void hammer2_chain_setflush(hammer2_chain_t *chain);
void hammer2_chain_countbrefs(hammer2_chain_t *chain,
				hammer2_blockref_t *base, int count);
hammer2_chain_t *hammer2_chain_bulksnap(hammer2_dev_t *hmp);
void hammer2_chain_bulkdrop(hammer2_chain_t *copy);

void hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata);
int hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata);
int hammer2_chain_dirent_test(hammer2_chain_t *chain, const char *name,
				size_t name_len);

void hammer2_base_delete(hammer2_chain_t *parent,
				hammer2_blockref_t *base, int count,
				hammer2_chain_t *chain,
				hammer2_blockref_t *obref);
void hammer2_base_insert(hammer2_chain_t *parent,
				hammer2_blockref_t *base, int count,
				hammer2_chain_t *chain,
				hammer2_blockref_t *elm);
void hammer2_dump_chain(hammer2_chain_t *chain, int tab, int bi, int *countp,
				char pfx, u_int flags);

/*
 * hammer2_flush.c
 */
void hammer2_trans_manage_init(hammer2_pfs_t *pmp);
int hammer2_flush(hammer2_chain_t *chain, int istop);
void hammer2_trans_init(hammer2_pfs_t *pmp, uint32_t flags);
void hammer2_trans_setflags(hammer2_pfs_t *pmp, uint32_t flags);
void hammer2_trans_clearflags(hammer2_pfs_t *pmp, uint32_t flags);
hammer2_tid_t hammer2_trans_sub(hammer2_pfs_t *pmp);
void hammer2_trans_done(hammer2_pfs_t *pmp, uint32_t flags);
hammer2_tid_t hammer2_trans_newinum(hammer2_pfs_t *pmp);
void hammer2_trans_assert_strategy(hammer2_pfs_t *pmp);

/*
 * hammer2_ioctl.c
 */
int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
				int fflag, struct ucred *cred);

/*
 * hammer2_io.c
 */
void hammer2_io_hash_init(hammer2_dev_t *hmp);
void hammer2_io_inval(hammer2_io_t *dio, hammer2_off_t data_off, u_int bytes);
void hammer2_io_hash_cleanup_all(hammer2_dev_t *hmp);
char *hammer2_io_data(hammer2_io_t *dio, off_t lbase);
void hammer2_io_bkvasync(hammer2_io_t *dio);
void hammer2_io_dedup_set(hammer2_dev_t *hmp, hammer2_blockref_t *bref);
void hammer2_io_dedup_delete(hammer2_dev_t *hmp, uint8_t btype,
				hammer2_off_t data_off, u_int bytes);
void hammer2_io_dedup_assert(hammer2_dev_t *hmp, hammer2_off_t data_off,
				u_int bytes);
int hammer2_io_new(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
				hammer2_io_t **diop);
int hammer2_io_newnz(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
				hammer2_io_t **diop);
int _hammer2_io_bread(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
				hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
void hammer2_io_setdirty(hammer2_io_t *dio);

hammer2_io_t *_hammer2_io_getblk(hammer2_dev_t *hmp, int btype, off_t lbase,
				int lsize, int op HAMMER2_IO_DEBUG_ARGS);
hammer2_io_t *_hammer2_io_getquick(hammer2_dev_t *hmp, off_t lbase,
				int lsize HAMMER2_IO_DEBUG_ARGS);
void _hammer2_io_putblk(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
int _hammer2_io_bwrite(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
void _hammer2_io_bawrite(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
void _hammer2_io_bdwrite(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
void _hammer2_io_brelse(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
void _hammer2_io_bqrelse(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
void _hammer2_io_ref(hammer2_io_t *dio HAMMER2_IO_DEBUG_ARGS);

#ifndef HAMMER2_IO_DEBUG

#define hammer2_io_getblk(hmp, btype, lbase, lsize, op)			\
	_hammer2_io_getblk((hmp), (btype), (lbase), (lsize), (op))
#define hammer2_io_getquick(hmp, lbase, lsize)				\
	_hammer2_io_getquick((hmp), (lbase), (lsize))
#define hammer2_io_putblk(diop)						\
	_hammer2_io_putblk(diop)
#define hammer2_io_bwrite(diop)						\
	_hammer2_io_bwrite((diop))
#define hammer2_io_bawrite(diop)					\
	_hammer2_io_bawrite((diop))
#define hammer2_io_bdwrite(diop)					\
	_hammer2_io_bdwrite((diop))
#define hammer2_io_brelse(diop)						\
	_hammer2_io_brelse((diop))
#define hammer2_io_bqrelse(diop)					\
	_hammer2_io_bqrelse((diop))
#define hammer2_io_ref(dio)						\
	_hammer2_io_ref((dio))

#define hammer2_io_bread(hmp, btype, lbase, lsize, diop)		\
	_hammer2_io_bread((hmp), (btype), (lbase), (lsize), (diop))

#else

#define hammer2_io_getblk(hmp, btype, lbase, lsize, op)			\
	_hammer2_io_getblk((hmp), (btype), (lbase), (lsize), (op),	\
	__FILE__, __LINE__)

#define hammer2_io_getquick(hmp, lbase, lsize)				\
	_hammer2_io_getquick((hmp), (lbase), (lsize), __FILE__, __LINE__)

#define hammer2_io_putblk(diop)						\
	_hammer2_io_putblk(diop, __FILE__, __LINE__)

#define hammer2_io_bwrite(diop)						\
	_hammer2_io_bwrite((diop), __FILE__, __LINE__)
#define hammer2_io_bawrite(diop)					\
	_hammer2_io_bawrite((diop), __FILE__, __LINE__)
#define hammer2_io_bdwrite(diop)					\
	_hammer2_io_bdwrite((diop), __FILE__, __LINE__)
#define hammer2_io_brelse(diop)						\
	_hammer2_io_brelse((diop), __FILE__, __LINE__)
#define hammer2_io_bqrelse(diop)					\
	_hammer2_io_bqrelse((diop), __FILE__, __LINE__)
#define hammer2_io_ref(dio)						\
	_hammer2_io_ref((dio), __FILE__, __LINE__)

#define hammer2_io_bread(hmp, btype, lbase, lsize, diop)		\
	_hammer2_io_bread((hmp), (btype), (lbase), (lsize), (diop),	\
			  __FILE__, __LINE__)

#endif

/*
 * hammer2_admin.c
 */
void hammer2_thr_signal(hammer2_thread_t *thr, uint32_t flags);
void hammer2_thr_signal2(hammer2_thread_t *thr,
			uint32_t pflags, uint32_t nflags);
void hammer2_thr_wait(hammer2_thread_t *thr, uint32_t flags);
void hammer2_thr_wait_neg(hammer2_thread_t *thr, uint32_t flags);
int hammer2_thr_wait_any(hammer2_thread_t *thr, uint32_t flags, int timo);
void hammer2_thr_create(hammer2_thread_t *thr,
			hammer2_pfs_t *pmp, hammer2_dev_t *hmp,
			const char *id, int clindex, int repidx,
			void (*func)(void *arg));
void hammer2_thr_delete(hammer2_thread_t *thr);
void hammer2_thr_remaster(hammer2_thread_t *thr);
void hammer2_thr_freeze_async(hammer2_thread_t *thr);
void hammer2_thr_freeze(hammer2_thread_t *thr);
void hammer2_thr_unfreeze(hammer2_thread_t *thr);
int hammer2_thr_break(hammer2_thread_t *thr);
void hammer2_primary_xops_thread(void *arg);

/*
 * hammer2_thread.c (XOP API)
 */
void *hammer2_xop_alloc(hammer2_inode_t *ip, int flags);
void hammer2_xop_setname(hammer2_xop_head_t *xop,
				const char *name, size_t name_len);
void hammer2_xop_setname2(hammer2_xop_head_t *xop,
				const char *name, size_t name_len);
size_t hammer2_xop_setname_inum(hammer2_xop_head_t *xop, hammer2_key_t inum);
void hammer2_xop_setip2(hammer2_xop_head_t *xop, hammer2_inode_t *ip2);
void hammer2_xop_setip3(hammer2_xop_head_t *xop, hammer2_inode_t *ip3);
void hammer2_xop_setip4(hammer2_xop_head_t *xop, hammer2_inode_t *ip4);
void hammer2_xop_reinit(hammer2_xop_head_t *xop);
void hammer2_xop_helper_create(hammer2_pfs_t *pmp);
void hammer2_xop_helper_cleanup(hammer2_pfs_t *pmp);
void hammer2_xop_start(hammer2_xop_head_t *xop, hammer2_xop_desc_t *desc);
void hammer2_xop_start_except(hammer2_xop_head_t *xop, hammer2_xop_desc_t *desc,
				int notidx);
int hammer2_xop_collect(hammer2_xop_head_t *xop, int flags);
void hammer2_xop_retire(hammer2_xop_head_t *xop, uint64_t mask);
int hammer2_xop_active(hammer2_xop_head_t *xop);
int hammer2_xop_feed(hammer2_xop_head_t *xop, hammer2_chain_t *chain,
				int clindex, int error);

/*
 * hammer2_synchro.c
 */
void hammer2_primary_sync_thread(void *arg);

/*
 * XOP backends in hammer2_xops.c, primarily for VNOPS.  Other XOP backends
 * may be integrated into other source files.
 */
void hammer2_xop_ipcluster(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_readdir(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_nresolve(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_unlink(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_nrename(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_scanlhc(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_scanall(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_lookup(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_delete(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_inode_mkdirent(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_inode_create(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_inode_create_det(hammer2_xop_t *xop,
				void *scratch, int clindex);
void hammer2_xop_inode_create_ins(hammer2_xop_t *xop,
				void *scratch, int clindex);
void hammer2_xop_inode_destroy(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_inode_chain_sync(hammer2_xop_t *xop, void *scratch,
				int clindex);
void hammer2_xop_inode_unlinkall(hammer2_xop_t *xop, void *scratch,
				int clindex);
void hammer2_xop_inode_connect(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_inode_flush(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_strategy_read(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_strategy_write(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_xop_bmap(hammer2_xop_t *xop, void *scratch, int clindex);

void hammer2_dmsg_ipcluster(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_readdir(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_nresolve(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_unlink(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_nrename(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_scanlhc(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_scanall(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_lookup(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_inode_mkdirent(hammer2_xop_t *xop, void *scratch,
				int clindex);
void hammer2_dmsg_inode_create(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_inode_destroy(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_inode_chain_sync(hammer2_xop_t *xop, void *scratch,
				int clindex);
void hammer2_dmsg_inode_unlinkall(hammer2_xop_t *xop, void *scratch,
				int clindex);
void hammer2_dmsg_inode_connect(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_inode_flush(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_strategy_read(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_dmsg_strategy_write(hammer2_xop_t *xop, void *scratch,
				int clindex);

void hammer2_rmsg_ipcluster(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_readdir(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_nresolve(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_unlink(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_nrename(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_scanlhc(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_scanall(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_lookup(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_inode_mkdirent(hammer2_xop_t *xop, void *scratch,
				int clindex);
void hammer2_rmsg_inode_create(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_inode_destroy(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_inode_chain_sync(hammer2_xop_t *xop, void *scratch,
				int clindex);
void hammer2_rmsg_inode_unlinkall(hammer2_xop_t *xop, void *scratch,
				int clindex);
void hammer2_rmsg_inode_connect(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_inode_flush(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_strategy_read(hammer2_xop_t *xop, void *scratch, int clindex);
void hammer2_rmsg_strategy_write(hammer2_xop_t *xop, void *scratch,
				int clindex);

extern hammer2_xop_desc_t hammer2_ipcluster_desc;
extern hammer2_xop_desc_t hammer2_readdir_desc;
extern hammer2_xop_desc_t hammer2_nresolve_desc;
extern hammer2_xop_desc_t hammer2_unlink_desc;
extern hammer2_xop_desc_t hammer2_nrename_desc;
extern hammer2_xop_desc_t hammer2_scanlhc_desc;
extern hammer2_xop_desc_t hammer2_scanall_desc;
extern hammer2_xop_desc_t hammer2_lookup_desc;
extern hammer2_xop_desc_t hammer2_delete_desc;
extern hammer2_xop_desc_t hammer2_inode_mkdirent_desc;
extern hammer2_xop_desc_t hammer2_inode_create_desc;
extern hammer2_xop_desc_t hammer2_inode_create_det_desc;
extern hammer2_xop_desc_t hammer2_inode_create_ins_desc;
extern hammer2_xop_desc_t hammer2_inode_destroy_desc;
extern hammer2_xop_desc_t hammer2_inode_chain_sync_desc;
extern hammer2_xop_desc_t hammer2_inode_unlinkall_desc;
extern hammer2_xop_desc_t hammer2_inode_connect_desc;
extern hammer2_xop_desc_t hammer2_inode_flush_desc;
extern hammer2_xop_desc_t hammer2_strategy_read_desc;
extern hammer2_xop_desc_t hammer2_strategy_write_desc;
extern hammer2_xop_desc_t hammer2_bmap_desc;

/*
 * hammer2_msgops.c
 */
int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);

/*
 * hammer2_vfsops.c
 */
int hammer2_vfs_sync(struct mount *mp, int waitflags);
int hammer2_vfs_sync_pmp(hammer2_pfs_t *pmp, int waitfor);
int hammer2_vfs_enospace(hammer2_inode_t *ip, off_t bytes, struct ucred *cred);

hammer2_pfs_t *hammer2_pfsalloc(hammer2_chain_t *chain,
				const hammer2_inode_data_t *ripdata,
				hammer2_dev_t *force_local);
void hammer2_pfsdealloc(hammer2_pfs_t *pmp, int clindex, int destroying);
int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
				ino_t ino, struct vnode **vpp);

void hammer2_lwinprog_ref(hammer2_pfs_t *pmp);
void hammer2_lwinprog_drop(hammer2_pfs_t *pmp);
void hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int pipe);

void hammer2_pfs_memory_wait(hammer2_pfs_t *pmp);
void hammer2_pfs_memory_inc(hammer2_pfs_t *pmp);
void hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp, int count);

void hammer2_voldata_lock(hammer2_dev_t *hmp);
void hammer2_voldata_unlock(hammer2_dev_t *hmp);
void hammer2_voldata_modify(hammer2_dev_t *hmp);

/*
 * hammer2_freemap.c
 */
int hammer2_freemap_alloc(hammer2_chain_t *chain, size_t bytes);
void hammer2_freemap_adjust(hammer2_dev_t *hmp,
				hammer2_blockref_t *bref, int how);

/*
 * hammer2_cluster.c
 */
uint8_t hammer2_cluster_type(hammer2_cluster_t *cluster);
void hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref);
void hammer2_cluster_ref(hammer2_cluster_t *cluster);
void hammer2_cluster_drop(hammer2_cluster_t *cluster);
void hammer2_cluster_unhold(hammer2_cluster_t *cluster);
void hammer2_cluster_rehold(hammer2_cluster_t *cluster);
void hammer2_cluster_lock(hammer2_cluster_t *cluster, int how);
int hammer2_cluster_check(hammer2_cluster_t *cluster, hammer2_key_t lokey,
			int flags);
void hammer2_cluster_unlock(hammer2_cluster_t *cluster);

void hammer2_bulkfree_init(hammer2_dev_t *hmp);
void hammer2_bulkfree_uninit(hammer2_dev_t *hmp);
int hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
			struct hammer2_ioc_bulkfree *bfi);
void hammer2_dummy_xop_from_chain(hammer2_xop_head_t *xop,
			hammer2_chain_t *chain);

/*
 * hammer2_iocom.c
 */
void hammer2_iocom_init(hammer2_dev_t *hmp);
void hammer2_iocom_uninit(hammer2_dev_t *hmp);
void hammer2_cluster_reconnect(hammer2_dev_t *hmp, struct file *fp);
void hammer2_volconf_update(hammer2_dev_t *hmp, int index);

/*
 * hammer2_strategy.c
 */
int hammer2_vop_strategy(struct vop_strategy_args *ap);
int hammer2_vop_bmap(struct vop_bmap_args *ap);
void hammer2_bioq_sync(hammer2_pfs_t *pmp);
void hammer2_dedup_record(hammer2_chain_t *chain, hammer2_io_t *dio,
				const char *data);
void hammer2_dedup_clear(hammer2_dev_t *hmp);

/*
 * hammer2_ondisk.c
 */
int hammer2_open_devvp(const hammer2_devvp_list_t *devvpl, int ronly);
int hammer2_close_devvp(const hammer2_devvp_list_t *devvpl, int ronly);
int hammer2_init_devvp(const char *blkdevs, int rootmount,
			hammer2_devvp_list_t *devvpl);
void hammer2_cleanup_devvp(hammer2_devvp_list_t *devvpl);
int hammer2_init_volumes(struct mount *mp, const hammer2_devvp_list_t *devvpl,
			hammer2_volume_t *volumes,
			hammer2_volume_data_t *rootvoldata,
			int *rootvolzone,
			struct vnode **rootvoldevvp);
hammer2_volume_t *hammer2_get_volume(hammer2_dev_t *hmp, hammer2_off_t offset);

/*
 * More complex inlines
 */

#define hammer2_xop_gdata(xop)	_hammer2_xop_gdata((xop), __FILE__, __LINE__)

static __inline
const hammer2_media_data_t *
_hammer2_xop_gdata(hammer2_xop_head_t *xop, const char *file, int line)
{
	hammer2_chain_t *focus;
	const void *data;

	focus = xop->cluster.focus;
	if (focus->dio) {
		lockmgr(&focus->diolk, LK_SHARED);
		if ((xop->focus_dio = focus->dio) != NULL) {
			_hammer2_io_ref(xop->focus_dio HAMMER2_IO_DEBUG_CALL);
			hammer2_io_bkvasync(xop->focus_dio);
		}
		data = focus->data;
		lockmgr(&focus->diolk, LK_RELEASE);
	} else {
		data = focus->data;
	}

	return data;
}

#define hammer2_xop_pdata(xop)	_hammer2_xop_pdata((xop), __FILE__, __LINE__)

static __inline
void
_hammer2_xop_pdata(hammer2_xop_head_t *xop, const char *file, int line)
{
	if (xop->focus_dio)
		_hammer2_io_putblk(&xop->focus_dio HAMMER2_IO_DEBUG_CALL);
}

static __inline
void
hammer2_knote(struct vnode *vp, int flags)
{
	if (flags)
		KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
}

#endif /* !_KERNEL */
#endif /* !_VFS_HAMMER2_HAMMER2_H_ */