Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
/*
 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
/*
 * Copyright (c) 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * 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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
 *
 *	@(#)vfs_cluster.c	8.10 (Berkeley) 3/28/95
 */

#include <sys/param.h>
#include <sys/proc_internal.h>
#include <sys/buf_internal.h>
#include <sys/mount_internal.h>
#include <sys/vnode_internal.h>
#include <sys/trace.h>
#include <kern/kalloc.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/resourcevar.h>
#include <miscfs/specfs/specdev.h>
#include <sys/uio_internal.h>
#include <libkern/libkern.h>
#include <machine/machine_routines.h>
#include <machine/smp.h>

#include <sys/ubc_internal.h>
#include <vm/vnode_pager.h>
#include <vm/vm_upl.h>

#include <mach/mach_types.h>
#include <mach/memory_object_types.h>
#include <mach/vm_map.h>
#include <mach/upl.h>
#include <mach/thread_info.h>
#include <kern/task.h>
#include <kern/policy_internal.h>
#include <kern/thread.h>

#include <vm/vm_kern_xnu.h>
#include <vm/vm_map_xnu.h>
#include <vm/vm_pageout_xnu.h>
#include <vm/vm_fault.h>
#include <vm/vm_ubc.h>

#include <sys/kdebug.h>
#include <sys/kdebug_triage.h>
#include <libkern/OSAtomic.h>

#include <sys/sdt.h>

#include <stdbool.h>

#include <vfs/vfs_disk_conditioner.h>

#if 0
#undef KERNEL_DEBUG
#define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
#endif


#define CL_READ         0x01
#define CL_WRITE        0x02
#define CL_ASYNC        0x04
#define CL_COMMIT       0x08
#define CL_PAGEOUT      0x10
#define CL_AGE          0x20
#define CL_NOZERO       0x40
#define CL_PAGEIN       0x80
#define CL_DEV_MEMORY   0x100
#define CL_PRESERVE     0x200
#define CL_THROTTLE     0x400
#define CL_KEEPCACHED   0x800
#define CL_DIRECT_IO    0x1000
#define CL_PASSIVE      0x2000
#define CL_IOSTREAMING  0x4000
#define CL_CLOSE        0x8000
#define CL_ENCRYPTED    0x10000
#define CL_RAW_ENCRYPTED        0x20000
#define CL_NOCACHE      0x40000
#define CL_DIRECT_IO_FSBLKSZ    0x80000

#define MAX_VECTOR_UPL_SIZE     (2 * MAX_UPL_SIZE_BYTES)

#define CLUSTER_IO_WAITING              ((buf_t)1)

extern void vector_upl_set_iostate(upl_t, upl_t, vm_offset_t, upl_size_t);

struct clios {
	lck_mtx_t io_mtxp;
	u_int  io_completed;       /* amount of io that has currently completed */
	u_int  io_issued;          /* amount of io that was successfully issued */
	int    io_error;           /* error code of first error encountered */
	int    io_wanted;          /* someone is sleeping waiting for a change in state */
};

struct cl_direct_read_lock {
	LIST_ENTRY(cl_direct_read_lock)         chain;
	int32_t                                                         ref_count;
	vnode_t                                                         vp;
	lck_rw_t                                                        rw_lock;
};

#define CL_DIRECT_READ_LOCK_BUCKETS 61

static LIST_HEAD(cl_direct_read_locks, cl_direct_read_lock)
cl_direct_read_locks[CL_DIRECT_READ_LOCK_BUCKETS];

static LCK_GRP_DECLARE(cl_mtx_grp, "cluster I/O");
static LCK_MTX_DECLARE(cl_transaction_mtxp, &cl_mtx_grp);
static LCK_SPIN_DECLARE(cl_direct_read_spin_lock, &cl_mtx_grp);

static ZONE_DEFINE(cl_rd_zone, "cluster_read",
    sizeof(struct cl_readahead), ZC_ZFREE_CLEARMEM);

static ZONE_DEFINE(cl_wr_zone, "cluster_write",
    sizeof(struct cl_writebehind), ZC_ZFREE_CLEARMEM);

#define IO_UNKNOWN      0
#define IO_DIRECT       1
#define IO_CONTIG       2
#define IO_COPY         3

#define PUSH_DELAY      0x01
#define PUSH_ALL        0x02
#define PUSH_SYNC       0x04


static void cluster_EOT(buf_t cbp_head, buf_t cbp_tail, int zero_offset, size_t verify_block_size);
static void cluster_wait_IO(buf_t cbp_head, int async);
static void cluster_complete_transaction(buf_t *cbp_head, void *callback_arg, int *retval, int flags, int needwait);

static int cluster_io_type(struct uio *uio, int *io_type, u_int32_t *io_length, u_int32_t min_length);

static int cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int non_rounded_size,
    int flags, buf_t real_bp, struct clios *iostate, int (*)(buf_t, void *), void *callback_arg);
static void cluster_iodone_verify_continue(void);
static int cluster_iodone(buf_t bp, void *callback_arg);
static int cluster_iodone_finish(buf_t cbp_head, void *callback_arg);
static int cluster_ioerror(upl_t upl, int upl_offset, int abort_size, int error, int io_flags, vnode_t vp);
static int cluster_is_throttled(vnode_t vp);

static void cluster_iostate_wait(struct clios *iostate, u_int target, const char *wait_name);

static void cluster_syncup(vnode_t vp, off_t newEOF, int (*)(buf_t, void *), void *callback_arg, int flags);

static int cluster_handle_split_pagein(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
    u_int io_size, int rounded_size, int local_flags, int (*callback)(buf_t, void *), void *callback_arg);

static void cluster_read_upl_release(upl_t upl, int start_pg, int last_pg, int take_reference);
static int cluster_copy_ubc_data_internal(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty, int take_reference);

static int cluster_read_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t filesize, int flags,
    int (*)(buf_t, void *), void *callback_arg) __attribute__((noinline));
static int cluster_read_direct(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
    int flags, int (*)(buf_t, void *), void *callback_arg) __attribute__((noinline));
static int cluster_read_contig(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
    int (*)(buf_t, void *), void *callback_arg, int flags) __attribute__((noinline));

static int cluster_write_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t oldEOF, off_t newEOF,
    off_t headOff, off_t tailOff, int flags, int (*)(buf_t, void *), void *callback_arg) __attribute__((noinline));
static int cluster_write_direct(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, int *write_type, u_int32_t *write_length,
    int flags, int (*callback)(buf_t, void *), void *callback_arg, uint32_t min_io_size) __attribute__((noinline));
static int cluster_write_contig(vnode_t vp, struct uio *uio, off_t newEOF,
    int *write_type, u_int32_t *write_length, int (*)(buf_t, void *), void *callback_arg, int bflag) __attribute__((noinline));

static void cluster_update_state_internal(vnode_t vp, struct cl_extent *cl, int flags, boolean_t defer_writes, boolean_t *first_pass,
    off_t write_off, int write_cnt, off_t newEOF, int (*callback)(buf_t, void *), void *callback_arg, boolean_t vm_initiated);

static int cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, u_int32_t xsize, int flags, int (*)(buf_t, void *), void *callback_arg);

static int      cluster_read_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize, int (*callback)(buf_t, void *), void *callback_arg, int bflag);
static void     cluster_read_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *ra,
    int (*callback)(buf_t, void *), void *callback_arg, int bflag);

static int      cluster_push_now(vnode_t vp, struct cl_extent *, off_t EOF, int flags, int (*)(buf_t, void *), void *callback_arg, boolean_t vm_ioitiated);

static int      cluster_try_push(struct cl_writebehind *, vnode_t vp, off_t EOF, int push_flag, int flags, int (*)(buf_t, void *),
    void *callback_arg, int *err, boolean_t vm_initiated);

static int      sparse_cluster_switch(struct cl_writebehind *, vnode_t vp, off_t EOF, int (*)(buf_t, void *), void *callback_arg, boolean_t vm_initiated);
static int      sparse_cluster_push(struct cl_writebehind *, void **cmapp, vnode_t vp, off_t EOF, int push_flag,
    int io_flags, int (*)(buf_t, void *), void *callback_arg, boolean_t vm_initiated);
static int      sparse_cluster_add(struct cl_writebehind *, void **cmapp, vnode_t vp, struct cl_extent *, off_t EOF,
    int (*)(buf_t, void *), void *callback_arg, boolean_t vm_initiated);

static kern_return_t vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, u_int *setcountp);
static kern_return_t vfs_drt_get_cluster(void **cmapp, off_t *offsetp, u_int *lengthp);
static kern_return_t vfs_drt_control(void **cmapp, int op_type);
static kern_return_t vfs_get_scmap_push_behavior_internal(void **cmapp, int *push_flag);


/*
 * For throttled IO to check whether
 * a block is cached by the boot cache
 * and thus it can avoid delaying the IO.
 *
 * bootcache_contains_block is initially
 * NULL. The BootCache will set it while
 * the cache is active and clear it when
 * the cache is jettisoned.
 *
 * Returns 0 if the block is not
 * contained in the cache, 1 if it is
 * contained.
 *
 * The function pointer remains valid
 * after the cache has been evicted even
 * if bootcache_contains_block has been
 * cleared.
 *
 * See rdar://9974130 The new throttling mechanism breaks the boot cache for throttled IOs
 */
int (*bootcache_contains_block)(dev_t device, u_int64_t blkno) = NULL;


/*
 * limit the internal I/O size so that we
 * can represent it in a 32 bit int
 */
#define MAX_IO_REQUEST_SIZE     (1024 * 1024 * 512)
#define MAX_IO_CONTIG_SIZE      MAX_UPL_SIZE_BYTES
#define MAX_VECTS               16
/*
 * The MIN_DIRECT_WRITE_SIZE governs how much I/O should be issued before we consider
 * allowing the caller to bypass the buffer cache.  For small I/Os (less than 16k),
 * we have not historically allowed the write to bypass the UBC.
 */
#define MIN_DIRECT_WRITE_SIZE   (16384)

#define WRITE_THROTTLE          6
#define WRITE_THROTTLE_SSD      2
#define WRITE_BEHIND            1
#define WRITE_BEHIND_SSD        1

#if !defined(XNU_TARGET_OS_OSX)
#define PREFETCH                1
#define PREFETCH_SSD            1
uint32_t speculative_prefetch_max = (2048 * 1024);              /* maximum bytes in a specluative read-ahead */
uint32_t speculative_prefetch_max_iosize = (512 * 1024);        /* maximum I/O size to use in a specluative read-ahead */
#else /* XNU_TARGET_OS_OSX */
#define PREFETCH                3
#define PREFETCH_SSD            2
uint32_t speculative_prefetch_max = (MAX_UPL_SIZE_BYTES * 3);   /* maximum bytes in a specluative read-ahead */
uint32_t speculative_prefetch_max_iosize = (512 * 1024);        /* maximum I/O size to use in a specluative read-ahead on SSDs*/
#endif /* ! XNU_TARGET_OS_OSX */

/* maximum bytes for read-ahead */
uint32_t prefetch_max = (1024 * 1024 * 1024);
/* maximum bytes for outstanding reads */
uint32_t overlapping_read_max = (1024 * 1024 * 1024);
/* maximum bytes for outstanding writes */
uint32_t overlapping_write_max = (1024 * 1024 * 1024);

#define IO_SCALE(vp, base)              (vp->v_mount->mnt_ioscale * (base))
#define MAX_CLUSTER_SIZE(vp)            (cluster_max_io_size(vp->v_mount, CL_WRITE))

int     speculative_reads_disabled = 0;

/*
 * throttle the number of async writes that
 * can be outstanding on a single vnode
 * before we issue a synchronous write
 */
#define THROTTLE_MAXCNT 0

uint32_t throttle_max_iosize = (128 * 1024);

#define THROTTLE_MAX_IOSIZE (throttle_max_iosize)

SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_max_iosize, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_max_iosize, 0, "");

uint32_t split_pgin = 1;
uint32_t split_all_pgin = 1;
uint32_t split_all_pgin_equal = 0;
uint32_t split_pgin_headio = 0;

SYSCTL_INT(_kern, OID_AUTO, split_pagein_io, CTLFLAG_RW | CTLFLAG_LOCKED, &split_pgin, 0, "");
#if DEVELOPMENT || DEBUG
SYSCTL_INT(_kern, OID_AUTO, split_pagein_io_all, CTLFLAG_RW | CTLFLAG_LOCKED, &split_all_pgin, 0, "");
SYSCTL_INT(_kern, OID_AUTO, split_pagein_io_equal, CTLFLAG_RW | CTLFLAG_LOCKED, &split_all_pgin_equal, 0, "");
SYSCTL_INT(_kern, OID_AUTO, split_pagein_do_headio, CTLFLAG_RW | CTLFLAG_LOCKED, &split_pgin_headio, 0, "");
#endif

struct verify_buf {
	TAILQ_ENTRY(verify_buf) vb_entry;
	buf_t vb_cbp;
	void* vb_callback_arg;
	int32_t vb_whichq;
};

TAILQ_HEAD(, verify_buf) verify_free_head;
TAILQ_HEAD(, verify_buf) verify_work_head;

#define MAX_VERIFY_THREADS 4
#define MAX_REQUESTS_PER_THREAD  2

static struct verify_buf verify_bufs[MAX_VERIFY_THREADS * MAX_REQUESTS_PER_THREAD];
/*
 * Each thread needs to check if the item at the head of the queue has a UPL
 * pointer that is any of the threads are currently operating on.
 * slot 0 is for the io completion thread to do the request inline if there are no free
 * queue slots.
 */
static int verify_in_flight = 0;

#if defined(XNU_TARGET_OS_IOS) || defined(XNU_TARGET_OS_XR)
#define NUM_DEFAULT_THREADS 2
#elif defined(XNU_TARGET_OS_OSX)
#define NUM_DEFAULT_THREADS 4
#else
#define NUM_DEFAULT_THREADS 0
#endif

static TUNABLE(uint32_t, num_verify_threads, "num_verify_threads", NUM_DEFAULT_THREADS);
static uint32_t cluster_verify_threads = 0; /* will be launched as needed upto num_verify_threads */

#if __AMP__
static TUNABLE(uint32_t, ecore_verify_threads, "ecore_verify_threads", false);
#endif /* __AMP__ */

static void
cluster_verify_init(void)
{
	TAILQ_INIT(&verify_free_head);
	TAILQ_INIT(&verify_work_head);

	if (num_verify_threads > MAX_VERIFY_THREADS) {
		num_verify_threads = MAX_VERIFY_THREADS;
	}

	for (int i = 0; i < num_verify_threads * MAX_REQUESTS_PER_THREAD; i++) {
		TAILQ_INSERT_TAIL(&verify_free_head, &verify_bufs[i], vb_entry);
	}
}

void
cluster_init(void)
{
	for (int i = 0; i < CL_DIRECT_READ_LOCK_BUCKETS; ++i) {
		LIST_INIT(&cl_direct_read_locks[i]);
	}

	cluster_verify_init();
}

uint32_t
cluster_max_io_size(mount_t mp, int type)
{
	uint32_t        max_io_size;
	uint32_t        segcnt;
	uint32_t        maxcnt;

	switch (type) {
	case CL_READ:
		segcnt = mp->mnt_segreadcnt;
		maxcnt = mp->mnt_maxreadcnt;
		break;
	case CL_WRITE:
		segcnt = mp->mnt_segwritecnt;
		maxcnt = mp->mnt_maxwritecnt;
		break;
	default:
		segcnt = min(mp->mnt_segreadcnt, mp->mnt_segwritecnt);
		maxcnt = min(mp->mnt_maxreadcnt, mp->mnt_maxwritecnt);
		break;
	}
	if (segcnt > (MAX_UPL_SIZE_BYTES >> PAGE_SHIFT)) {
		/*
		 * don't allow a size beyond the max UPL size we can create
		 */
		segcnt = MAX_UPL_SIZE_BYTES >> PAGE_SHIFT;
	}
	max_io_size = min((segcnt * PAGE_SIZE), maxcnt);

	if (max_io_size < MAX_UPL_TRANSFER_BYTES) {
		/*
		 * don't allow a size smaller than the old fixed limit
		 */
		max_io_size = MAX_UPL_TRANSFER_BYTES;
	} else {
		/*
		 * make sure the size specified is a multiple of PAGE_SIZE
		 */
		max_io_size &= ~PAGE_MASK;
	}
	return max_io_size;
}

/*
 * Returns max prefetch value. If the value overflows or exceeds the specified
 * 'prefetch_limit', it will be capped at 'prefetch_limit' value.
 */
static inline uint32_t
cluster_max_prefetch(vnode_t vp, uint32_t max_io_size, uint32_t prefetch_limit)
{
	bool is_ssd = disk_conditioner_mount_is_ssd(vp->v_mount);
	uint32_t io_scale = IO_SCALE(vp, is_ssd ? PREFETCH_SSD : PREFETCH);
	uint32_t prefetch = 0;

	if (__improbable(os_mul_overflow(max_io_size, io_scale, &prefetch) ||
	    (prefetch > prefetch_limit))) {
		prefetch = prefetch_limit;
	}

	return prefetch;
}

static inline uint32_t
calculate_max_throttle_size(vnode_t vp)
{
	bool is_ssd = disk_conditioner_mount_is_ssd(vp->v_mount);
	uint32_t io_scale = IO_SCALE(vp, is_ssd ? 2 : 1);

	return MIN(io_scale * THROTTLE_MAX_IOSIZE, MAX_UPL_TRANSFER_BYTES);
}

static inline uint32_t
calculate_max_throttle_cnt(vnode_t vp)
{
	bool is_ssd = disk_conditioner_mount_is_ssd(vp->v_mount);
	uint32_t io_scale = IO_SCALE(vp, 1);

	return is_ssd ? MIN(io_scale, 4) : THROTTLE_MAXCNT;
}

#define CLW_ALLOCATE            0x01
#define CLW_RETURNLOCKED        0x02
#define CLW_IONOCACHE           0x04
#define CLW_IOPASSIVE   0x08

/*
 * if the read ahead context doesn't yet exist,
 * allocate and initialize it...
 * the vnode lock serializes multiple callers
 * during the actual assignment... first one
 * to grab the lock wins... the other callers
 * will release the now unnecessary storage
 *
 * once the context is present, try to grab (but don't block on)
 * the lock associated with it... if someone
 * else currently owns it, than the read
 * will run without read-ahead.  this allows
 * multiple readers to run in parallel and
 * since there's only 1 read ahead context,
 * there's no real loss in only allowing 1
 * reader to have read-ahead enabled.
 */
static struct cl_readahead *
cluster_get_rap(vnode_t vp)
{
	struct ubc_info         *ubc;
	struct cl_readahead     *rap;

	ubc = vp->v_ubcinfo;

	if ((rap = ubc->cl_rahead) == NULL) {
		rap = zalloc_flags(cl_rd_zone, Z_WAITOK | Z_ZERO);
		rap->cl_lastr = -1;
		lck_mtx_init(&rap->cl_lockr, &cl_mtx_grp, LCK_ATTR_NULL);

		vnode_lock(vp);

		if (ubc->cl_rahead == NULL) {
			ubc->cl_rahead = rap;
		} else {
			lck_mtx_destroy(&rap->cl_lockr, &cl_mtx_grp);
			zfree(cl_rd_zone, rap);
			rap = ubc->cl_rahead;
		}
		vnode_unlock(vp);
	}
	if (lck_mtx_try_lock(&rap->cl_lockr) == TRUE) {
		return rap;
	}

	return (struct cl_readahead *)NULL;
}


/*
 * if the write behind context doesn't yet exist,
 * and CLW_ALLOCATE is specified, allocate and initialize it...
 * the vnode lock serializes multiple callers
 * during the actual assignment... first one
 * to grab the lock wins... the other callers
 * will release the now unnecessary storage
 *
 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
 * the lock associated with the write behind context before
 * returning
 */

static struct cl_writebehind *
cluster_get_wbp(vnode_t vp, int flags)
{
	struct ubc_info *ubc;
	struct cl_writebehind *wbp;

	ubc = vp->v_ubcinfo;

	if ((wbp = ubc->cl_wbehind) == NULL) {
		if (!(flags & CLW_ALLOCATE)) {
			return (struct cl_writebehind *)NULL;
		}

		wbp = zalloc_flags(cl_wr_zone, Z_WAITOK | Z_ZERO);

		lck_mtx_init(&wbp->cl_lockw, &cl_mtx_grp, LCK_ATTR_NULL);

		vnode_lock(vp);

		if (ubc->cl_wbehind == NULL) {
			ubc->cl_wbehind = wbp;
		} else {
			lck_mtx_destroy(&wbp->cl_lockw, &cl_mtx_grp);
			zfree(cl_wr_zone, wbp);
			wbp = ubc->cl_wbehind;
		}
		vnode_unlock(vp);
	}
	if (flags & CLW_RETURNLOCKED) {
		lck_mtx_lock(&wbp->cl_lockw);
	}

	return wbp;
}


static void
cluster_syncup(vnode_t vp, off_t newEOF, int (*callback)(buf_t, void *), void *callback_arg, int flags)
{
	struct cl_writebehind *wbp;

	if ((wbp = cluster_get_wbp(vp, 0)) != NULL) {
		if (wbp->cl_number) {
			lck_mtx_lock(&wbp->cl_lockw);

			cluster_try_push(wbp, vp, newEOF, PUSH_ALL | flags, 0, callback, callback_arg, NULL, FALSE);

			lck_mtx_unlock(&wbp->cl_lockw);
		}
	}
}


static int
cluster_io_present_in_BC(vnode_t vp, off_t f_offset)
{
	daddr64_t blkno;
	size_t    io_size;
	int (*bootcache_check_fn)(dev_t device, u_int64_t blkno) = bootcache_contains_block;

	if (bootcache_check_fn && vp->v_mount && vp->v_mount->mnt_devvp) {
		if (VNOP_BLOCKMAP(vp, f_offset, PAGE_SIZE, &blkno, &io_size, NULL, VNODE_READ | VNODE_BLOCKMAP_NO_TRACK, NULL)) {
			return 0;
		}

		if (io_size == 0) {
			return 0;
		}

		if (bootcache_check_fn(vp->v_mount->mnt_devvp->v_rdev, blkno)) {
			return 1;
		}
	}
	return 0;
}


static int
cluster_is_throttled(vnode_t vp)
{
	return throttle_io_will_be_throttled(-1, vp->v_mount);
}


static void
cluster_iostate_wait(struct clios *iostate, u_int target, const char *wait_name)
{
	lck_mtx_lock(&iostate->io_mtxp);

	while ((iostate->io_issued - iostate->io_completed) > target) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
		    iostate->io_issued, iostate->io_completed, target, 0, 0);

		iostate->io_wanted = 1;
		msleep((caddr_t)&iostate->io_wanted, &iostate->io_mtxp, PRIBIO + 1, wait_name, NULL);

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
		    iostate->io_issued, iostate->io_completed, target, 0, 0);
	}
	lck_mtx_unlock(&iostate->io_mtxp);
}


static void
cluster_handle_associated_upl(struct clios *iostate, upl_t upl,
    upl_offset_t upl_offset, upl_size_t size, off_t f_offset)
{
	if (!size) {
		return;
	}

	upl_t associated_upl = upl_associated_upl(upl);

	if (!associated_upl) {
		return;
	}

	/*
	 * The associated upl functions as a "range lock" for the file.
	 *
	 * The associated upl is created and is attached to to the upl in
	 * cluster_io when the direct io write is being started. Since the
	 * upl may be released in parts so the corresponding associated upl
	 * has to be released in parts as well.
	 *
	 * We have the f_offset, upl_offset and size and from that we have figure
	 * out the associated upl offset and length, we are interested in.
	 */
	upl_offset_t assoc_upl_offset, assoc_upl_end;

	/*                        ALIGNED UPL's                            */
	if ((upl_offset & PAGE_MASK) == (f_offset & PAGE_MASK)) {
		assoc_upl_offset = trunc_page_32(upl_offset);
		assoc_upl_end = round_page_32(upl_offset + size);
		goto do_commit;
	}

	/*
	 *                    HANDLE UNALIGNED UPLS
	 *
	 *  ( See also cluster_io where the associated upl is created )
	 *  While we create the upl in one go, we will be dumping the pages in
	 *  the upl in "transaction sized chunks" relative to the upl. Except
	 *  for the first transction, the upl_offset will always be page aligned.
	 *  and when the upl's are not aligned the associated upl offset will not
	 *  be page aligned and so we have to truncate and round up the starting
	 *  and the end of the pages in question and see if they are shared with
	 *  other transctions or not. If two transctions "share" a page in the
	 *  associated upl, the first one to complete "marks" it and skips that
	 *  page and the second  one will include it in the "commit range"
	 *
	 *  As an example, consider the case where 4 transctions are needed (this
	 *  is the worst case).
	 *
	 *  Transaction for 0-1 (size -> PAGE_SIZE - upl_offset)
	 *
	 *  This covers the associated upl from a -> c. a->b is not shared but
	 *  b-c is shared with the next transction so the first one to complete
	 *  will only "mark" it.
	 *
	 *  Transaction for 1-2 (size -> PAGE_SIZE)
	 *
	 *  For transaction 1, assoc_upl_offset would be 0 (corresponding to the
	 *  file offset a or b depending on what file offset the upl_offset
	 *  corrssponds to ) and assoc_upl_end would correspond to the file
	 *  offset c.
	 *
	 *                 (associated_upl - based on f_offset alignment)
	 *       0         a    b    c    d    e     f
	 *       <----|----|----|----|----|----|-----|---->
	 *
	 *
	 *                  (upl - based on user buffer address alignment)
	 *                   <__--|----|----|--__>
	 *
	 *                   0    1    2    3
	 *
	 */
	upl_size_t assoc_upl_size = upl_get_size(associated_upl);
#if 0
	/* knock off the simple case first -> this transaction covers the entire UPL */
	upl_offset_t upl_end = round_page_32(upl_offset + size);
	upl_size_t upl_size = vector_upl_get_size(upl);

	if ((trunc_page_32(upl_offset) == 0) && (upl_end == upl_size)) {
		assoc_upl_offset = 0;
		assoc_upl_end = assoc_upl_size;
		goto do_commit;
	}
#endif
	off_t assoc_upl_start_f_offset = upl_adjusted_offset(associated_upl, PAGE_MASK);

	assoc_upl_offset = (upl_offset_t)trunc_page_64(f_offset - assoc_upl_start_f_offset);
	assoc_upl_end = round_page_64(f_offset + size) - assoc_upl_start_f_offset;

	/*
	 * We can only sanity check the offset returned by upl_adjusted_offset
	 * for the first transaction for this UPL i.e. when (upl_offset < PAGE_SIZE)
	 */
	assertf((upl_offset >= PAGE_SIZE) || ((assoc_upl_start_f_offset == trunc_page_64(f_offset)) && (assoc_upl_offset == 0)),
	    "upl_offset = %d, f_offset = %lld, size = %d, start_f_offset = %lld,  assoc_upl_offset = %d",
	    upl_offset, f_offset, size, assoc_upl_start_f_offset, assoc_upl_offset);

	assertf((upl_offset == assoc_upl_offset) || (upl_offset > assoc_upl_offset && ((upl_offset - assoc_upl_offset) <= PAGE_SIZE)) ||
	    (assoc_upl_offset > upl_offset && ((assoc_upl_offset - upl_offset) <= PAGE_SIZE)),
	    "abs(upl_offset - assoc_upl_offset) >  PAGE_SIZE : "
	    "upl_offset = %d, f_offset = %lld, size = %d, start_f_offset = %lld, assoc_upl_offset = %d",
	    upl_offset, f_offset, size, assoc_upl_start_f_offset, assoc_upl_offset);

	assertf(assoc_upl_end <= assoc_upl_size,
	    "upl_offset = %d, f_offset = %lld, size = %d, start_f_offset = %lld, assoc_upl_size = %d, assoc_upl_offset = %d, assoc_upl_end = %d",
	    upl_offset, f_offset, size, assoc_upl_start_f_offset, assoc_upl_size, assoc_upl_offset, assoc_upl_end);

	assertf((assoc_upl_size > PAGE_SIZE) || (assoc_upl_offset == 0 && assoc_upl_end == PAGE_SIZE),
	    "upl_offset = %d, f_offset = %lld, size = %d, start_f_offset = %lld, assoc_upl_size = %d, assoc_upl_offset = %d, assoc_upl_end = %d",
	    upl_offset, f_offset, size, assoc_upl_start_f_offset, assoc_upl_size, assoc_upl_offset, assoc_upl_end);

	if (assoc_upl_size == PAGE_SIZE) {
		assoc_upl_offset = 0;
		assoc_upl_end = PAGE_SIZE;
		goto do_commit;
	}

	/*
	 * We have to check if the first and last pages of the associated UPL
	 * range could potentially be shared with other transactions and if the
	 * "sharing transactions" are both done. The first one sets the mark bit
	 * and the second one checks it and if set it includes that page in the
	 * pages to be "freed".
	 */
	bool check_first_pg = (assoc_upl_offset != 0) || ((f_offset + size) < (assoc_upl_start_f_offset + PAGE_SIZE));
	bool check_last_pg = (assoc_upl_end != assoc_upl_size) || (f_offset > ((assoc_upl_start_f_offset + assoc_upl_size) - PAGE_SIZE));

	if (check_first_pg || check_last_pg) {
		int first_pg = assoc_upl_offset >> PAGE_SHIFT;
		int last_pg = trunc_page_32(assoc_upl_end - 1) >> PAGE_SHIFT;
		upl_page_info_t *assoc_pl = UPL_GET_INTERNAL_PAGE_LIST(associated_upl);

		lck_mtx_lock_spin(&iostate->io_mtxp);
		if (check_first_pg && !upl_page_get_mark(assoc_pl, first_pg)) {
			/*
			 * The first page isn't marked so let another transaction
			 * completion handle it.
			 */
			upl_page_set_mark(assoc_pl, first_pg, true);
			assoc_upl_offset += PAGE_SIZE;
		}
		if (check_last_pg && !upl_page_get_mark(assoc_pl, last_pg)) {
			/*
			 * The last page isn't marked so mark the page and let another
			 * transaction completion handle it.
			 */
			upl_page_set_mark(assoc_pl, last_pg, true);
			assoc_upl_end -= PAGE_SIZE;
		}
		lck_mtx_unlock(&iostate->io_mtxp);
	}

	if (assoc_upl_end <= assoc_upl_offset) {
		return;
	}

do_commit:
	size = assoc_upl_end - assoc_upl_offset;

	boolean_t empty;

	/*
	 * We can unlock these pages now and as this is for a
	 * direct/uncached write, we want to dump the pages too.
	 */
	kern_return_t kr = upl_abort_range(associated_upl, assoc_upl_offset, size,
	    UPL_ABORT_DUMP_PAGES, &empty);

	assert(!kr);

	if (!kr && empty) {
		upl_set_associated_upl(upl, NULL);
		upl_deallocate(associated_upl);
	}
}

static void
cluster_iodone_verify_continue(void)
{
	lck_mtx_lock_spin(&cl_transaction_mtxp);
	for (;;) {
		struct verify_buf *vb = TAILQ_FIRST(&verify_work_head);

		if (!vb) {
			assert_wait(&verify_work_head, (THREAD_UNINT));
			break;
		}
		buf_t cbp = vb->vb_cbp;
		void* callback_arg = vb->vb_callback_arg;

		TAILQ_REMOVE(&verify_work_head, vb, vb_entry);
		vb->vb_cbp = NULL;
		vb->vb_callback_arg = NULL;
		vb->vb_whichq = 0;
		TAILQ_INSERT_TAIL(&verify_free_head, vb, vb_entry);
		lck_mtx_unlock(&cl_transaction_mtxp);

		(void)cluster_iodone_finish(cbp, callback_arg);
		cbp = NULL;
		lck_mtx_lock_spin(&cl_transaction_mtxp);
	}
	lck_mtx_unlock(&cl_transaction_mtxp);
	thread_block((thread_continue_t)cluster_iodone_verify_continue);
	/* NOT REACHED */
}

static void
cluster_verify_thread(void)
{
	thread_t self = current_thread();

	thread_set_thread_name(self, "cluster_verify_thread");
#if __AMP__
	if (ecore_verify_threads) {
		kern_return_t kr = thread_soft_bind_pset_type(self, 'E');
		if (kr != KERN_SUCCESS) {
			printf("%s: WARN: failed to bind thread to cluster type; does the hardware topology match expectations?\n", __FUNCTION__);
		}
	}
#endif /* __AMP__ */
#if !defined(__x86_64__)
	thread_group_join_io_storage();
#endif /* __x86_64__ */
	cluster_iodone_verify_continue();
	/* NOT REACHED */
}

static bool
enqueue_buf_for_verify(buf_t cbp, void *callback_arg)
{
	struct verify_buf *vb;

	vb = TAILQ_FIRST(&verify_free_head);
	if (vb) {
		TAILQ_REMOVE(&verify_free_head, vb, vb_entry);
		vb->vb_cbp = cbp;
		vb->vb_callback_arg = callback_arg;
		vb->vb_whichq = 1;
		TAILQ_INSERT_TAIL(&verify_work_head, vb, vb_entry);
		return true;
	} else {
		return false;
	}
}

static int
cluster_handle_verification(buf_t cbp_head, vnode_t vp, upl_t upl, int upl_offset, int transaction_size, int error)
{
	off_t start_off = cbp_head->b_clfoffset;
	void *verify_ctx = cbp_head->b_attr.ba_un.verify_ctx;
	caddr_t verify_buf = NULL;
	uint32_t verify_length = transaction_size;
	vnode_verify_flags_t verify_flags = VNODE_VERIFY_CONTEXT_FREE;
	int verify_error = EAGAIN;

	assert(cbp_head->b_attr.ba_flags & BA_WILL_VERIFY);

	cbp_head->b_attr.ba_un.verify_ctx = NULL;
	if (error) {
		goto free_context;
	}

	/*
	 * If we don't have a precomputed hash, we make a single call to both
	 * verify and free the context. If we have a precomputed hash, then we
	 * make two separate calls - one to verify the hash and the second one to
	 * free. If the filesystem returns EAGAIN we fall back to the non
	 * precomputed hash case.
	 */
	if (cbp_head->b_attr.ba_verify_type && cbp_head->b_attr.ba_flags & BA_VERIFY_VALID) {
		verify_buf = (caddr_t)buf_verifyptr_with_size(cbp_head, transaction_size, &verify_length);
		verify_flags = VNODE_VERIFY_WITH_CONTEXT | VNODE_VERIFY_PRECOMPUTED;

		if (verify_buf && verify_length) {
			verify_error = VNOP_VERIFY(vp, start_off, (uint8_t *)verify_buf, verify_length,
			    NULL, &verify_ctx, verify_flags, NULL, NULL);
		} else {
			verify_error = EAGAIN;
		}

		verify_buf = NULL;
		verify_length = transaction_size;
		verify_flags = VNODE_VERIFY_CONTEXT_FREE;
	}

	if (verify_error != EAGAIN) {
		error = verify_error;
	} else {
		vm_offset_t vaddr;

		/*
		 * Map it in.
		 *
		 * ubc_upl_map_range unfortunately cannot handle concurrent map
		 * requests for the same UPL and returns failures when it can't
		 * map. The map exclusive mechanism enforces mutual exclusion
		 * for concurrent requests.
		 */
		verify_error = 0;
		os_atomic_inc(&verify_in_flight, relaxed);
		upl_set_map_exclusive(upl);
		error = ubc_upl_map_range(upl, upl_offset, round_page(transaction_size), VM_PROT_DEFAULT, &vaddr);
		if (error) {
			upl_clear_map_exclusive(upl);
			printf("ubc_upl_map_range returned error %d upl = %p, upl_offset = %d, size = %d",
			    error, upl, (int)upl_offset, (int)round_page(transaction_size));
			error  = EIO;
			if (os_atomic_dec_orig(&verify_in_flight, relaxed) == 0) {
				panic("verify_in_flight underflow");
			}
		} else {
			verify_buf = (caddr_t)vaddr;
			verify_flags |= VNODE_VERIFY_WITH_CONTEXT;
		}
	}

free_context:
	verify_error = VNOP_VERIFY(vp, start_off, (uint8_t *)verify_buf, verify_length,
	    NULL, &verify_ctx, verify_flags, NULL, NULL);
	if (!error) {
		error = verify_error;
	}

	if (verify_buf) {
		(void)ubc_upl_unmap_range(upl, upl_offset, round_page(transaction_size));
		upl_clear_map_exclusive(upl);
		verify_buf = NULL;
		if (os_atomic_dec_orig(&verify_in_flight, relaxed) == 0) {
			panic("verify_in_flight underflow");
		}
	}

	return error;
}

static int
cluster_ioerror(upl_t upl, int upl_offset, int abort_size, int error, int io_flags, vnode_t vp)
{
	int upl_abort_code = 0;
	int page_in  = 0;
	int page_out = 0;

	if ((io_flags & (B_PHYS | B_CACHE)) == (B_PHYS | B_CACHE)) {
		/*
		 * direct write of any flavor, or a direct read that wasn't aligned
		 */
		ubc_upl_commit_range(upl, upl_offset, abort_size, UPL_COMMIT_FREE_ON_EMPTY);
	} else {
		if (io_flags & B_PAGEIO) {
			if (io_flags & B_READ) {
				page_in  = 1;
			} else {
				page_out = 1;
			}
		}
		if (io_flags & B_CACHE) {
			/*
			 * leave pages in the cache unchanged on error
			 */
			upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
		} else if (((io_flags & B_READ) == 0) && ((error != ENXIO) || vnode_isswap(vp))) {
			/*
			 * transient error on pageout/write path... leave pages unchanged
			 */
			upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
		} else if (page_in) {
			upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR;
		} else {
			upl_abort_code = UPL_ABORT_FREE_ON_EMPTY /* | UPL_ABORT_DUMP_PAGES */;
		}

		ubc_upl_abort_range(upl, upl_offset, abort_size, upl_abort_code);
	}
	return upl_abort_code;
}


static int
cluster_iodone(buf_t bp, void *callback_arg)
{
	buf_t   cbp;
	buf_t   cbp_head;
	int     error = 0;
	boolean_t       transaction_complete = FALSE;
	bool async;

	__IGNORE_WCASTALIGN(cbp_head = (buf_t)(bp->b_trans_head));

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_START,
	    cbp_head, bp->b_lblkno, bp->b_bcount, bp->b_flags, 0);

	async = cluster_verify_threads &&
	    (os_atomic_load(&cbp_head->b_attr.ba_flags, acquire) & BA_ASYNC_VERIFY);

	assert(!async || cbp_head->b_attr.ba_un.verify_ctx);

	if (cbp_head->b_trans_next || !(cbp_head->b_flags & B_EOT)) {
		lck_mtx_lock_spin(&cl_transaction_mtxp);

		bp->b_flags |= B_TDONE;

		for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next) {
			/*
			 * all I/O requests that are part of this transaction
			 * have to complete before we can process it
			 */
			if (!(cbp->b_flags & B_TDONE)) {
				KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
				    cbp_head, cbp, cbp->b_bcount, cbp->b_flags, 0);

				lck_mtx_unlock(&cl_transaction_mtxp);

				return 0;
			}

			if (cbp->b_trans_next == CLUSTER_IO_WAITING) {
				KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
				    cbp_head, cbp, cbp->b_bcount, cbp->b_flags, 0);

				lck_mtx_unlock(&cl_transaction_mtxp);
				wakeup(cbp);

				return 0;
			}

			if (cbp->b_flags & B_EOT) {
				transaction_complete = TRUE;

				if (async) {
					async = enqueue_buf_for_verify(cbp_head, callback_arg);
				}
			}
		}
		lck_mtx_unlock(&cl_transaction_mtxp);

		if (transaction_complete == FALSE) {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
			    cbp_head, 0, 0, 0, 0);
			return 0;
		}
	} else if (async) {
		lck_mtx_lock_spin(&cl_transaction_mtxp);
		async = enqueue_buf_for_verify(cbp_head, callback_arg);
		lck_mtx_unlock(&cl_transaction_mtxp);
	}

	if (async) {
		wakeup(&verify_work_head);
	} else {
		error = cluster_iodone_finish(cbp_head, callback_arg);
	}

	return error;
}

static int
cluster_iodone_finish(buf_t cbp_head, void *callback_arg)
{
	int     b_flags;
	int     error;
	int     total_size;
	int     total_resid;
	int     upl_offset;
	int     zero_offset;
	int     pg_offset = 0;
	int     commit_size = 0;
	int     upl_flags = 0;
	int     transaction_size = 0;
	upl_t   upl;
	buf_t   cbp;
	buf_t   cbp_next;
	buf_t   real_bp;
	vnode_t vp;
	struct  clios *iostate;

	error       = 0;
	total_size  = 0;
	total_resid = 0;

	cbp        = cbp_head;
	vp         = cbp->b_vp;
	upl_offset = cbp->b_uploffset;
	upl        = cbp->b_upl;
	b_flags    = cbp->b_flags;
	real_bp    = cbp->b_real_bp;
	zero_offset = cbp->b_validend;
	iostate    = (struct clios *)cbp->b_iostate;

	if (real_bp) {
		real_bp->b_dev = cbp->b_dev;
	}

	while (cbp) {
		if ((cbp->b_flags & B_ERROR) && error == 0) {
			error = cbp->b_error;
		}

		total_resid += cbp->b_resid;
		total_size  += cbp->b_bcount;

		cbp_next = cbp->b_trans_next;

		if (cbp_next == NULL) {
			/*
			 * compute the overall size of the transaction
			 * in case we created one that has 'holes' in it
			 * 'total_size' represents the amount of I/O we
			 * did, not the span of the transaction w/r to the UPL
			 */
			transaction_size = cbp->b_uploffset + cbp->b_bcount - upl_offset;
		}

		cbp = cbp_next;
	}

	if (ISSET(b_flags, B_COMMIT_UPL)) {
		cluster_handle_associated_upl(iostate,
		    cbp_head->b_upl,
		    upl_offset,
		    transaction_size,
		    cbp_head->b_clfoffset);
	}

	if (error == 0 && total_resid) {
		error = EIO;
	}

	if (error == 0) {
		int     (*cliodone_func)(buf_t, void *) = (int (*)(buf_t, void *))(cbp_head->b_cliodone);

		if (cliodone_func != NULL) {
			cbp_head->b_bcount = transaction_size;

			error = (*cliodone_func)(cbp_head, callback_arg);
		}
	}
	if (zero_offset) {
		cluster_zero(upl, zero_offset, PAGE_SIZE - (zero_offset & PAGE_MASK), real_bp);
	}

	if (cbp_head->b_attr.ba_un.verify_ctx) {
		error = cluster_handle_verification(cbp_head, vp, upl, upl_offset, transaction_size, error);
	} else if (cbp_head->b_attr.ba_flags & BA_WILL_VERIFY) {
		error = EBADMSG;
	}

	if (iostate) {
		int need_wakeup = 0;

		/*
		 * someone has issued multiple I/Os asynchrounsly
		 * and is waiting for them to complete (streaming)
		 */
		lck_mtx_lock_spin(&iostate->io_mtxp);

		if (error && iostate->io_error == 0) {
			iostate->io_error = error;
		}

		iostate->io_completed += total_size;

		if (iostate->io_wanted) {
			/*
			 * someone is waiting for the state of
			 * this io stream to change
			 */
			iostate->io_wanted = 0;
			need_wakeup = 1;
		}
		lck_mtx_unlock(&iostate->io_mtxp);

		if (need_wakeup) {
			wakeup((caddr_t)&iostate->io_wanted);
		}
	}

	if (b_flags & B_COMMIT_UPL) {
		pg_offset   = upl_offset & PAGE_MASK;
		commit_size = (pg_offset + transaction_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;

		if (error) {
			upl_set_iodone_error(upl, error);

			upl_flags = cluster_ioerror(upl, upl_offset - pg_offset, commit_size, error, b_flags, vp);
		} else {
			upl_flags = UPL_COMMIT_FREE_ON_EMPTY;

			if ((b_flags & B_PHYS) && (b_flags & B_READ)) {
				upl_flags |= UPL_COMMIT_SET_DIRTY;
			}

			if (b_flags & B_AGE) {
				upl_flags |= UPL_COMMIT_INACTIVATE;
			}

			ubc_upl_commit_range(upl, upl_offset - pg_offset, commit_size, upl_flags);
		}
	}

	cbp = cbp_head->b_trans_next;
	while (cbp) {
		cbp_next = cbp->b_trans_next;

		if (cbp != cbp_head) {
			free_io_buf(cbp);
		}

		cbp = cbp_next;
	}
	free_io_buf(cbp_head);

	if (real_bp) {
		if (error) {
			real_bp->b_flags |= B_ERROR;
			real_bp->b_error = error;
		}
		real_bp->b_resid = total_resid;

		buf_biodone(real_bp);
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
	    upl, upl_offset - pg_offset, commit_size, (error << 24) | upl_flags, 0);

	return error;
}


uint32_t
cluster_throttle_io_limit(vnode_t vp, uint32_t *limit)
{
	if (cluster_is_throttled(vp)) {
		*limit = calculate_max_throttle_size(vp);
		return 1;
	}
	return 0;
}


void
cluster_zero(upl_t upl, upl_offset_t upl_offset, int size, buf_t bp)
{
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 23)) | DBG_FUNC_START,
	    upl_offset, size, bp, 0, 0);

	if (bp == NULL || bp->b_datap == 0) {
		upl_page_info_t *pl;
		addr64_t        zero_addr;

		pl = ubc_upl_pageinfo(upl);

		if (upl_device_page(pl) == TRUE) {
			zero_addr = ((addr64_t)upl_phys_page(pl, 0) << PAGE_SHIFT) + upl_offset;

			bzero_phys_nc(zero_addr, size);
		} else {
			while (size) {
				int     page_offset;
				int     page_index;
				int     zero_cnt;

				page_index  = upl_offset / PAGE_SIZE;
				page_offset = upl_offset & PAGE_MASK;

				zero_addr = ((addr64_t)upl_phys_page(pl, page_index) << PAGE_SHIFT) + page_offset;
				zero_cnt  = min(PAGE_SIZE - page_offset, size);

				bzero_phys(zero_addr, zero_cnt);

				size       -= zero_cnt;
				upl_offset += zero_cnt;
			}
		}
	} else {
		bzero((caddr_t)((vm_offset_t)bp->b_datap + upl_offset), size);
	}

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 23)) | DBG_FUNC_END,
	    upl_offset, size, 0, 0, 0);
}


static void
cluster_EOT(buf_t cbp_head, buf_t cbp_tail, int zero_offset, size_t verify_block_size)
{
	/*
	 * We will assign a verification context to cbp_head.
	 * This will be passed back to the filesystem  when
	 * verifying (in cluster_iodone).
	 */
	if (verify_block_size) {
		off_t start_off = cbp_head->b_clfoffset;
		size_t length;
		void *verify_ctx = NULL;
		int error = 0;
		vnode_t vp = buf_vnode(cbp_head);

		if (cbp_head == cbp_tail) {
			length = cbp_head->b_bcount;
		} else {
			length = (cbp_tail->b_clfoffset + cbp_tail->b_bcount) - start_off;
		}

		/*
		 * zero_offset is non zero for the transaction containing the EOF
		 * (if the filesize is not page aligned). In that case we might
		 * have the transaction size not be page/verify block size aligned
		 */
		if ((zero_offset == 0) &&
		    ((length < verify_block_size) || (length % verify_block_size)) != 0) {
			panic("%s length = %zu, verify_block_size = %zu",
			    __FUNCTION__, length, verify_block_size);
		}

		error = VNOP_VERIFY(vp, start_off, NULL, length,
		    &verify_block_size, &verify_ctx, VNODE_VERIFY_CONTEXT_ALLOC, NULL, NULL);

		assert(!(error && verify_ctx));

		if (verify_ctx) {
			if (num_verify_threads && (os_atomic_load(&cluster_verify_threads, relaxed) == 0)) {
				if (os_atomic_inc_orig(&cluster_verify_threads, relaxed) == 0) {
					thread_t thread;
					int i;

					for (i = 0; i < num_verify_threads && i < MAX_VERIFY_THREADS; i++) {
						kernel_thread_start((thread_continue_t)cluster_verify_thread, NULL, &thread);
						thread_deallocate(thread);
					}
					os_atomic_store(&cluster_verify_threads, i, relaxed);
				} else {
					os_atomic_dec(&cluster_verify_threads, relaxed);
				}
			}
			cbp_head->b_attr.ba_un.verify_ctx = verify_ctx;
			/*
			 * At least one thread is busy (at the time we
			 * checked), so we can let it get queued for
			 * async processing. It's fine if we occasionally get
			 * this wrong.
			 */
			if (os_atomic_load(&verify_in_flight, relaxed)) {
				/* This flag and the setting of ba_un.verify_ctx needs to be ordered */
				os_atomic_or(&cbp_head->b_attr.ba_flags, BA_ASYNC_VERIFY, release);
			}
		}
	} else {
		cbp_head->b_attr.ba_un.verify_ctx = NULL;
	}

	cbp_head->b_validend = zero_offset;
	cbp_tail->b_flags |= B_EOT;
}

static void
cluster_wait_IO(buf_t cbp_head, int async)
{
	buf_t   cbp;

	if (async) {
		/*
		 * Async callback completion will not normally generate a
		 * wakeup upon I/O completion.  To get woken up, we set
		 * b_trans_next (which is safe for us to modify) on the last
		 * buffer to CLUSTER_IO_WAITING so that cluster_iodone knows
		 * to wake us up when all buffers as part of this transaction
		 * are completed.  This is done under the umbrella of
		 * cl_transaction_mtxp which is also taken in cluster_iodone.
		 */
		bool done = true;
		buf_t last = NULL;

		lck_mtx_lock_spin(&cl_transaction_mtxp);

		for (cbp = cbp_head; cbp; last = cbp, cbp = cbp->b_trans_next) {
			if (!ISSET(cbp->b_flags, B_TDONE)) {
				done = false;
			}
		}

		if (!done) {
			last->b_trans_next = CLUSTER_IO_WAITING;

			DTRACE_IO1(wait__start, buf_t, last);
			do {
				msleep(last, &cl_transaction_mtxp, PSPIN | (PRIBIO + 1), "cluster_wait_IO", NULL);

				/*
				 * We should only have been woken up if all the
				 * buffers are completed, but just in case...
				 */
				done = true;
				for (cbp = cbp_head; cbp != CLUSTER_IO_WAITING; cbp = cbp->b_trans_next) {
					if (!ISSET(cbp->b_flags, B_TDONE)) {
						done = false;
						break;
					}
				}
			} while (!done);
			DTRACE_IO1(wait__done, buf_t, last);

			last->b_trans_next = NULL;
		}

		lck_mtx_unlock(&cl_transaction_mtxp);
	} else { // !async
		for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next) {
			buf_biowait(cbp);
		}
	}
}

static void
cluster_complete_transaction(buf_t *cbp_head, void *callback_arg, int *retval, int flags, int needwait)
{
	buf_t   cbp;
	int     error;
	boolean_t isswapout = FALSE;

	/*
	 * cluster_complete_transaction will
	 * only be called if we've issued a complete chain in synchronous mode
	 * or, we've already done a cluster_wait_IO on an incomplete chain
	 */
	if (needwait) {
		for (cbp = *cbp_head; cbp; cbp = cbp->b_trans_next) {
			buf_biowait(cbp);
		}
	}
	/*
	 * we've already waited on all of the I/Os in this transaction,
	 * so mark all of the buf_t's in this transaction as B_TDONE
	 * so that cluster_iodone sees the transaction as completed
	 */
	for (cbp = *cbp_head; cbp; cbp = cbp->b_trans_next) {
		cbp->b_flags |= B_TDONE;
		cbp->b_attr.ba_flags &= ~BA_ASYNC_VERIFY;
	}
	cbp = *cbp_head;

	if ((flags & (CL_ASYNC | CL_PAGEOUT)) == CL_PAGEOUT && vnode_isswap(cbp->b_vp)) {
		isswapout = TRUE;
	}

	error = cluster_iodone(cbp, callback_arg);

	if (!(flags & CL_ASYNC) && error && *retval == 0) {
		if (((flags & (CL_PAGEOUT | CL_KEEPCACHED)) != CL_PAGEOUT) || (error != ENXIO)) {
			*retval = error;
		} else if (isswapout == TRUE) {
			*retval = error;
		}
	}
	*cbp_head = (buf_t)NULL;
}

uint64_t cluster_direct_write_wired = 0;

static int
cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int non_rounded_size,
    int flags, buf_t real_bp, struct clios *iostate, int (*callback)(buf_t, void *), void *callback_arg)
{
	buf_t   cbp;
	u_int   size;
	u_int   io_size;
	int     io_flags;
	int     bmap_flags;
	int     error = 0;
	int     retval = 0;
	buf_t   cbp_head = NULL;
	buf_t   cbp_tail = NULL;
	int     trans_count = 0;
	int     max_trans_count;
	u_int   pg_count;
	int     pg_offset;
	u_int   max_iosize;
	u_int   max_vectors;
	int     priv;
	int     zero_offset = 0;
	int     async_throttle = 0;
	mount_t mp;
	size_t verify_block_size = 0;
	vm_offset_t upl_end_offset;
	vnode_verify_kind_t verify_kind = VK_HASH_NONE;
	boolean_t   need_EOT = FALSE;

	/*
	 * we currently don't support buffers larger than a page
	 */
	if (real_bp && non_rounded_size > PAGE_SIZE) {
		panic("%s(): Called with real buffer of size %d bytes which "
		    "is greater than the maximum allowed size of "
		    "%d bytes (the system PAGE_SIZE).\n",
		    __FUNCTION__, non_rounded_size, PAGE_SIZE);
	}

	mp = vp->v_mount;

	/*
	 * we don't want to do any funny rounding of the size for IO requests
	 * coming through the DIRECT or CONTIGUOUS paths...  those pages don't
	 * belong to us... we can't extend (nor do we need to) the I/O to fill
	 * out a page
	 */
	if (mp->mnt_devblocksize > 1 && !(flags & (CL_DEV_MEMORY | CL_DIRECT_IO))) {
		/*
		 * round the requested size up so that this I/O ends on a
		 * page boundary in case this is a 'write'... if the filesystem
		 * has blocks allocated to back the page beyond the EOF, we want to
		 * make sure to write out the zero's that are sitting beyond the EOF
		 * so that in case the filesystem doesn't explicitly zero this area
		 * if a hole is created via a lseek/write beyond the current EOF,
		 * it will return zeros when it's read back from the disk.  If the
		 * physical allocation doesn't extend for the whole page, we'll
		 * only write/read from the disk up to the end of this allocation
		 * via the extent info returned from the VNOP_BLOCKMAP call.
		 */
		pg_offset = upl_offset & PAGE_MASK;

		size = (((non_rounded_size + pg_offset) + (PAGE_SIZE - 1)) & ~PAGE_MASK) - pg_offset;
	} else {
		/*
		 * anyone advertising a blocksize of 1 byte probably
		 * can't deal with us rounding up the request size
		 * AFP is one such filesystem/device
		 */
		size = non_rounded_size;
	}
	upl_end_offset = upl_offset + size;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_START, (int)f_offset, size, upl_offset, flags, 0);

	/*
	 * Set the maximum transaction size to the maximum desired number of
	 * buffers.
	 */
	max_trans_count = 8;
	if (flags & CL_DEV_MEMORY) {
		max_trans_count = 16;
	}

	if (flags & CL_READ) {
		io_flags = B_READ;
		bmap_flags = VNODE_READ;

		max_iosize  = mp->mnt_maxreadcnt;
		max_vectors = mp->mnt_segreadcnt;

		/* See if we can do cluster verification (pageins and aligned reads) */
		if ((flags & CL_PAGEIN || cluster_verify_threads) &&
		    !(mp->mnt_kern_flag & MNTK_VIRTUALDEV) &&
		    (VNOP_VERIFY(vp, f_offset, NULL, 0, &verify_block_size, NULL, VNODE_VERIFY_DEFAULT, NULL, &verify_kind) == 0) &&
		    verify_block_size) {
			if (verify_block_size != PAGE_SIZE) {
				verify_block_size = 0;
			}
			if (real_bp && verify_block_size) {
				panic("%s(): Called with real buffer and needs verification ",
				    __FUNCTION__);
			}
			/*
			 * For reads, only allow cluster verification if f_offset
			 * and upl_offset are both page aligned. Additionally, for direct reads,
			 * require that the length of the write also be page aligned.
			 * If they are not  page aligned, leave it to the filesystem to do verification.
			 * Strictly speaking, the alignments need to be for verify_block_size
			 * but since the only verify_block_size that is currently supported
			 * is page size, we check against page alignment.
			 */
			if (verify_block_size && !(flags & CL_PAGEIN) &&
			    ((f_offset & PAGE_MASK) || (upl_offset & PAGE_MASK) ||
			    ((flags & CL_DIRECT_IO) && (non_rounded_size & PAGE_MASK)))) {
				verify_block_size = 0;
				verify_kind = VK_HASH_NONE;
			}
			if (verify_block_size && verify_kind && !upl_has_fs_verify_info(upl)) {
				upl_set_fs_verify_info(upl,
				    (upl_adjusted_size(upl, PAGE_MASK) / mp->mnt_devblocksize) * get_num_bytes_for_verify_kind(verify_kind));
			}
		}
	} else {
		io_flags = B_WRITE;
		bmap_flags = VNODE_WRITE;

		max_iosize  = mp->mnt_maxwritecnt;
		max_vectors = mp->mnt_segwritecnt;
	}
	if (verify_block_size) {
		bmap_flags |= VNODE_CLUSTER_VERIFY;
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_NONE, max_iosize, max_vectors, mp->mnt_devblocksize, 0, 0);

	/*
	 * make sure the maximum iosize is a
	 * multiple of the page size
	 */
	max_iosize  &= ~PAGE_MASK;

	/*
	 * Ensure the maximum iosize is sensible.
	 */
	if (!max_iosize) {
		max_iosize = PAGE_SIZE;
	}

	if (flags & CL_THROTTLE) {
		if (!(flags & CL_PAGEOUT) && cluster_is_throttled(vp)) {
			uint32_t max_throttle_size = calculate_max_throttle_size(vp);

			if (max_iosize > max_throttle_size) {
				max_iosize = max_throttle_size;
			}
			async_throttle = calculate_max_throttle_cnt(vp);
		} else {
			if ((flags & CL_DEV_MEMORY)) {
				async_throttle = IO_SCALE(vp, VNODE_ASYNC_THROTTLE);
			} else {
				u_int max_cluster;
				u_int max_cluster_size;
				u_int scale;

				if (vp->v_mount->mnt_minsaturationbytecount) {
					max_cluster_size = vp->v_mount->mnt_minsaturationbytecount;

					scale = 1;
				} else {
					max_cluster_size = MAX_CLUSTER_SIZE(vp);

					if (disk_conditioner_mount_is_ssd(vp->v_mount)) {
						scale = WRITE_THROTTLE_SSD;
					} else {
						scale = WRITE_THROTTLE;
					}
				}
				if (max_iosize > max_cluster_size) {
					max_cluster = max_cluster_size;
				} else {
					max_cluster = max_iosize;
				}

				if (size < max_cluster) {
					max_cluster = size;
				}

				if (flags & CL_CLOSE) {
					scale += MAX_CLUSTERS;
				}

				async_throttle = min(IO_SCALE(vp, VNODE_ASYNC_THROTTLE), ((scale * max_cluster_size) / max_cluster) - 1);
			}
		}
	}
	if (flags & CL_AGE) {
		io_flags |= B_AGE;
	}
	if (flags & (CL_PAGEIN | CL_PAGEOUT)) {
		io_flags |= B_PAGEIO;
	}
	if (flags & (CL_IOSTREAMING)) {
		io_flags |= B_IOSTREAMING;
	}
	if (flags & CL_COMMIT) {
		io_flags |= B_COMMIT_UPL;
	}
	if (flags & CL_DIRECT_IO) {
		io_flags |= B_PHYS;
	}
	if (flags & (CL_PRESERVE | CL_KEEPCACHED)) {
		io_flags |= B_CACHE;
	}
	if (flags & CL_PASSIVE) {
		io_flags |= B_PASSIVE;
	}
	if (flags & CL_ENCRYPTED) {
		io_flags |= B_ENCRYPTED_IO;
	}

	if (vp->v_flag & VSYSTEM) {
		io_flags |= B_META;
	}

	if ((flags & CL_READ) && ((upl_offset + non_rounded_size) & PAGE_MASK) && (!(flags & CL_NOZERO))) {
		/*
		 * then we are going to end up
		 * with a page that we can't complete (the file size wasn't a multiple
		 * of PAGE_SIZE and we're trying to read to the end of the file
		 * so we'll go ahead and zero out the portion of the page we can't
		 * read in from the file
		 */
		zero_offset = (int)(upl_offset + non_rounded_size);
	} else if (!ISSET(flags, CL_READ) && ISSET(flags, CL_DIRECT_IO)) {
		assert(ISSET(flags, CL_COMMIT));

		// For a direct/uncached write, we need to lock pages...
		upl_t cached_upl = NULL;
		upl_page_info_t *cached_pl;

		assert(upl_offset < PAGE_SIZE);

		/*
		 *
		 *                       f_offset = b
		 *                      upl_offset = 8K
		 *
		 *                       (cached_upl - based on f_offset alignment)
		 *       0         a    b              c
		 *       <----|----|----|----|----|----|-----|---->
		 *
		 *
		 *                          (upl - based on user buffer address alignment)
		 *                   <__--|----|----|--__>
		 *
		 *                   0    1x   2x  3x
		 *
		 */
		const off_t cached_upl_f_offset = trunc_page_64(f_offset);
		const int cached_upl_size = round_page_32((f_offset - cached_upl_f_offset) + non_rounded_size);
		int num_retries = 0;

		/*
		 * Create a UPL to lock the pages in the cache whilst the
		 * write is in progress.
		 */
create_cached_upl:
		ubc_create_upl_kernel(vp, cached_upl_f_offset, cached_upl_size, &cached_upl,
		    &cached_pl, UPL_SET_LITE | UPL_WILL_MODIFY, VM_KERN_MEMORY_FILE);
		if (cached_upl && upl_has_wired_pages(cached_upl)) {
			/*
			 * Pages in this UPL would contain stale data after our direct write
			 * (which is intended to overwrite these pages on disk).  The UPL is
			 * just holding these pages "busy" to synchronize with any other I/O
			 * or mmap() access and we have to dump these pages when the direct
			 * write is done.
			 * But we can't do that for wired pages, so let's release this UPL
			 * and fall back to the "cached" path.
			 */
//			printf("*******  FBDP %s:%d vp %p offset 0x%llx size 0x%llx - switching from direct to cached write\n", __FUNCTION__, __LINE__, vp, cached_upl_f_offset, (uint64_t)cached_upl_size);
			ubc_upl_abort_range(cached_upl, 0, cached_upl_size, UPL_ABORT_FREE_ON_EMPTY);
			cached_upl = NULL;
			cached_pl = NULL;
			cluster_direct_write_wired++;
			return ENOTSUP;
		}

		/*
		 * If we are not overwriting the first and last pages completely
		 * we need to write them out first if they are dirty. These pages
		 * will be discarded after the write completes so we might lose
		 * the writes for the parts that are not overwrrtten.
		 */
		bool first_page_needs_sync = false;
		bool last_page_needs_sync = false;

		if (cached_upl && (cached_upl_f_offset < f_offset) && upl_dirty_page(cached_pl, 0)) {
			first_page_needs_sync = true;
		}

		if (cached_upl && (cached_upl_f_offset + cached_upl_size) > (f_offset + non_rounded_size)) {
			int last_page = (cached_upl_size / PAGE_SIZE) - 1;

			if ((last_page != 0 || !first_page_needs_sync) && upl_dirty_page(cached_pl, last_page)) {
				last_page_needs_sync = true;
			}
		}

		if (first_page_needs_sync || last_page_needs_sync) {
			ubc_upl_abort_range(cached_upl, 0, cached_upl_size, UPL_ABORT_FREE_ON_EMPTY);
			cached_upl = NULL;
			cached_pl = NULL;
			if (first_page_needs_sync) {
				ubc_msync(vp, cached_upl_f_offset, cached_upl_f_offset + PAGE_SIZE, NULL, UBC_PUSHALL | UBC_INVALIDATE | UBC_SYNC);
			}
			if (last_page_needs_sync) {
				off_t cached_upl_end_offset = cached_upl_f_offset + cached_upl_size;

				ubc_msync(vp, cached_upl_end_offset - PAGE_SIZE, cached_upl_end_offset, NULL, UBC_PUSHALL | UBC_INVALIDATE | UBC_SYNC);
			}
			if (++num_retries < 16) {
				goto create_cached_upl;
			}
			printf("%s : Number of retries for syncing first or last page reached %d\n", __FUNCTION__, num_retries);
			assertf(num_retries < 16, "%s : Number of retries for syncing first or last page reached %d\n", __FUNCTION__, num_retries);
		}

		/*
		 * Attach this UPL to the other UPL so that we can find it
		 * later.
		 */
		upl_set_associated_upl(upl, cached_upl);
		assertf(!cached_upl ||
		    (upl_adjusted_offset(cached_upl, PAGE_MASK) == cached_upl_f_offset),
		    "upl_adjusted_offset(cached_upl, PAGE_MASK) = %lld, cached_upl_f_offset = %lld",
		    upl_adjusted_offset(cached_upl, PAGE_MASK), cached_upl_f_offset);
	}

	while (size) {
		daddr64_t blkno;
		daddr64_t lblkno;
		size_t  io_size_tmp;
		u_int   io_size_wanted;

		if (size > max_iosize) {
			io_size = max_iosize;
		} else {
			io_size = size;
		}

		io_size_wanted = io_size;
		io_size_tmp = (size_t)io_size;

		if ((error = VNOP_BLOCKMAP(vp, f_offset, io_size, &blkno, &io_size_tmp, NULL, bmap_flags, NULL))) {
			break;
		}

		if (io_size_tmp > io_size_wanted) {
			io_size = io_size_wanted;
		} else {
			io_size = (u_int)io_size_tmp;
		}

		if (real_bp && (real_bp->b_blkno == real_bp->b_lblkno)) {
			real_bp->b_blkno = blkno;
		}

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 24)) | DBG_FUNC_NONE,
		    (int)f_offset, (int)(blkno >> 32), (int)blkno, io_size, 0);

		if (io_size == 0) {
			/*
			 * vnop_blockmap didn't return an error... however, it did
			 * return an extent size of 0 which means we can't
			 * make forward progress on this I/O... a hole in the
			 * file would be returned as a blkno of -1 with a non-zero io_size
			 * a real extent is returned with a blkno != -1 and a non-zero io_size
			 */
			error = EINVAL;
			break;
		}
		if (!(flags & CL_READ) && blkno == -1) {
			off_t   e_offset;
			int     pageout_flags;

			if (upl_get_internal_vectorupl(upl)) {
				panic("Vector UPLs should not take this code-path");
			}
			/*
			 * we're writing into a 'hole'
			 */
			if (flags & CL_PAGEOUT) {
				/*
				 * if we got here via cluster_pageout
				 * then just error the request and return
				 * the 'hole' should already have been covered
				 */
				error = EINVAL;
				break;
			}
			/*
			 * we can get here if the cluster code happens to
			 * pick up a page that was dirtied via mmap vs
			 * a 'write' and the page targets a 'hole'...
			 * i.e. the writes to the cluster were sparse
			 * and the file was being written for the first time
			 *
			 * we can also get here if the filesystem supports
			 * 'holes' that are less than PAGE_SIZE.... because
			 * we can't know if the range in the page that covers
			 * the 'hole' has been dirtied via an mmap or not,
			 * we have to assume the worst and try to push the
			 * entire page to storage.
			 *
			 * Try paging out the page individually before
			 * giving up entirely and dumping it (the pageout
			 * path will insure that the zero extent accounting
			 * has been taken care of before we get back into cluster_io)
			 *
			 * go direct to vnode_pageout so that we don't have to
			 * unbusy the page from the UPL... we used to do this
			 * so that we could call ubc_msync, but that results
			 * in a potential deadlock if someone else races us to acquire
			 * that page and wins and in addition needs one of the pages
			 * we're continuing to hold in the UPL
			 */
			pageout_flags = UPL_MSYNC | UPL_VNODE_PAGER | UPL_NESTED_PAGEOUT;

			if (!(flags & CL_ASYNC)) {
				pageout_flags |= UPL_IOSYNC;
			}
			if (!(flags & CL_COMMIT)) {
				pageout_flags |= UPL_NOCOMMIT;
			}

			if (cbp_head) {
				buf_t prev_cbp;
				uint32_t   bytes_in_last_page;

				/*
				 * first we have to wait for the the current outstanding I/Os
				 * to complete... EOT hasn't been set yet on this transaction
				 * so the pages won't be released
				 */
				cluster_wait_IO(cbp_head, (flags & CL_ASYNC));

				bytes_in_last_page = cbp_head->b_uploffset & PAGE_MASK;
				for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next) {
					bytes_in_last_page += cbp->b_bcount;
				}
				bytes_in_last_page &= PAGE_MASK;

				while (bytes_in_last_page) {
					/*
					 * we've got a transcation that
					 * includes the page we're about to push out through vnode_pageout...
					 * find the bp's in the list which intersect this page and either
					 * remove them entirely from the transaction (there could be multiple bp's), or
					 * round it's iosize down to the page boundary (there can only be one)...
					 *
					 * find the last bp in the list and act on it
					 */
					for (prev_cbp = cbp = cbp_head; cbp->b_trans_next; cbp = cbp->b_trans_next) {
						prev_cbp = cbp;
					}

					if (bytes_in_last_page >= cbp->b_bcount) {
						/*
						 * this buf no longer has any I/O associated with it
						 */
						bytes_in_last_page -= cbp->b_bcount;
						cbp->b_bcount = 0;

						free_io_buf(cbp);

						if (cbp == cbp_head) {
							assert(bytes_in_last_page == 0);
							/*
							 * the buf we just freed was the only buf in
							 * this transaction... so there's no I/O to do
							 */
							cbp_head = NULL;
							cbp_tail = NULL;
						} else {
							/*
							 * remove the buf we just freed from
							 * the transaction list
							 */
							prev_cbp->b_trans_next = NULL;
							cbp_tail = prev_cbp;
						}
					} else {
						/*
						 * this is the last bp that has I/O
						 * intersecting the page of interest
						 * only some of the I/O is in the intersection
						 * so clip the size but keep it in the transaction list
						 */
						cbp->b_bcount -= bytes_in_last_page;
						cbp_tail = cbp;
						bytes_in_last_page = 0;
					}
				}
				if (cbp_head) {
					/*
					 * there was more to the current transaction
					 * than just the page we are pushing out via vnode_pageout...
					 * mark it as finished and complete it... we've already
					 * waited for the I/Os to complete above in the call to cluster_wait_IO
					 */
					cluster_EOT(cbp_head, cbp_tail, 0, 0);

					cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 0);

					trans_count = 0;
				}
			}
			if (vnode_pageout(vp, upl, (upl_offset_t)trunc_page(upl_offset), trunc_page_64(f_offset), PAGE_SIZE, pageout_flags, NULL) != PAGER_SUCCESS) {
				error = EINVAL;
			}
			e_offset = round_page_64(f_offset + 1);
			io_size = (u_int)(e_offset - f_offset);

			f_offset   += io_size;
			upl_offset += io_size;

			if (size >= io_size) {
				size -= io_size;
			} else {
				size = 0;
			}
			/*
			 * keep track of how much of the original request
			 * that we've actually completed... non_rounded_size
			 * may go negative due to us rounding the request
			 * to a page size multiple (i.e.  size > non_rounded_size)
			 */
			non_rounded_size -= io_size;

			if (non_rounded_size <= 0) {
				/*
				 * we've transferred all of the data in the original
				 * request, but we were unable to complete the tail
				 * of the last page because the file didn't have
				 * an allocation to back that portion... this is ok.
				 */
				size = 0;
			}
			if (error) {
				if (size == 0) {
					flags &= ~CL_COMMIT;
				}
				break;
			}
			continue;
		}

		lblkno = (daddr64_t)(f_offset / CLUSTER_IO_BLOCK_SIZE);

		/*
		 * we have now figured out how much I/O we can do - this is in 'io_size'
		 * pg_offset is the starting point in the first page for the I/O
		 * pg_count is the number of full and partial pages that 'io_size' encompasses
		 */
		pg_offset = upl_offset & PAGE_MASK;

		if (flags & CL_DEV_MEMORY) {
			/*
			 * treat physical requests as one 'giant' page
			 */
			pg_count = 1;
		} else {
			pg_count  = (io_size + pg_offset + (PAGE_SIZE - 1)) / PAGE_SIZE;
		}

		if ((flags & CL_READ) && blkno == -1) {
			vm_offset_t  commit_offset;
			int bytes_to_zero;
			int complete_transaction_now = 0;

			/*
			 * if we're reading and blkno == -1, then we've got a
			 * 'hole' in the file that we need to deal with by zeroing
			 * out the affected area in the upl
			 */
			if (io_size >= (u_int)non_rounded_size) {
				/*
				 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
				 * than 'zero_offset' will be non-zero
				 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
				 * (indicated by the io_size finishing off the I/O request for this UPL)
				 * than we're not going to issue an I/O for the
				 * last page in this upl... we need to zero both the hole and the tail
				 * of the page beyond the EOF, since the delayed zero-fill won't kick in
				 */
				bytes_to_zero = non_rounded_size;
				if (!(flags & CL_NOZERO)) {
					bytes_to_zero = (int)((((upl_offset + io_size) + (PAGE_SIZE - 1)) & ~PAGE_MASK) - upl_offset);
				}

				zero_offset = 0;
			} else {
				bytes_to_zero = io_size;
			}

			pg_count = 0;

			cluster_zero(upl, (upl_offset_t)upl_offset, bytes_to_zero, real_bp);

			if (cbp_head) {
				int     pg_resid;

				/*
				 * if there is a current I/O chain pending
				 * then the first page of the group we just zero'd
				 * will be handled by the I/O completion if the zero
				 * fill started in the middle of the page
				 */
				commit_offset = (upl_offset + (PAGE_SIZE - 1)) & ~PAGE_MASK;

				pg_resid = (int)(commit_offset - upl_offset);

				if (bytes_to_zero >= pg_resid) {
					/*
					 * the last page of the current I/O
					 * has been completed...
					 * compute the number of fully zero'd
					 * pages that are beyond it
					 * plus the last page if its partial
					 * and we have no more I/O to issue...
					 * otherwise a partial page is left
					 * to begin the next I/O
					 */
					if ((int)io_size >= non_rounded_size) {
						pg_count = (bytes_to_zero - pg_resid + (PAGE_SIZE - 1)) / PAGE_SIZE;
					} else {
						pg_count = (bytes_to_zero - pg_resid) / PAGE_SIZE;
					}

					complete_transaction_now = 1;
				}
			} else {
				/*
				 * no pending I/O to deal with
				 * so, commit all of the fully zero'd pages
				 * plus the last page if its partial
				 * and we have no more I/O to issue...
				 * otherwise a partial page is left
				 * to begin the next I/O
				 */
				if ((int)io_size >= non_rounded_size) {
					pg_count = (pg_offset + bytes_to_zero + (PAGE_SIZE - 1)) / PAGE_SIZE;
				} else {
					pg_count = (pg_offset + bytes_to_zero) / PAGE_SIZE;
				}

				commit_offset = upl_offset & ~PAGE_MASK;
			}

			// Associated UPL is currently only used in the direct write path
			assert(!upl_associated_upl(upl));

			if ((flags & CL_COMMIT) && pg_count) {
				ubc_upl_commit_range(upl, (upl_offset_t)commit_offset,
				    pg_count * PAGE_SIZE,
				    UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY);
			}
			upl_offset += io_size;
			f_offset   += io_size;
			size       -= io_size;

			/*
			 * keep track of how much of the original request
			 * that we've actually completed... non_rounded_size
			 * may go negative due to us rounding the request
			 * to a page size multiple (i.e.  size > non_rounded_size)
			 */
			non_rounded_size -= io_size;

			if (non_rounded_size <= 0) {
				/*
				 * we've transferred all of the data in the original
				 * request, but we were unable to complete the tail
				 * of the last page because the file didn't have
				 * an allocation to back that portion... this is ok.
				 */
				size = 0;
			}
			if (cbp_head && (complete_transaction_now || size == 0)) {
				cluster_wait_IO(cbp_head, (flags & CL_ASYNC));

				cluster_EOT(cbp_head, cbp_tail, size == 0 ? zero_offset : 0, verify_block_size);

				cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 0);

				trans_count = 0;
			}
			continue;
		}
		if (pg_count > max_vectors) {
			if (((pg_count - max_vectors) * PAGE_SIZE) > io_size) {
				io_size = PAGE_SIZE - pg_offset;
				pg_count = 1;
			} else {
				io_size -= (pg_count - max_vectors) * PAGE_SIZE;
				pg_count = max_vectors;
			}
		}
		/*
		 * If the transaction is going to reach the maximum number of
		 * desired elements, truncate the i/o to the nearest page so
		 * that the actual i/o is initiated after this buffer is
		 * created and added to the i/o chain.
		 *
		 * I/O directed to physically contiguous memory
		 * doesn't have a requirement to make sure we 'fill' a page
		 */
		if (!(flags & CL_DEV_MEMORY) && trans_count >= max_trans_count &&
		    ((upl_offset + io_size) & PAGE_MASK)) {
			vm_offset_t aligned_ofs;

			aligned_ofs = (upl_offset + io_size) & ~PAGE_MASK;
			/*
			 * If the io_size does not actually finish off even a
			 * single page we have to keep adding buffers to the
			 * transaction despite having reached the desired limit.
			 *
			 * Eventually we get here with the page being finished
			 * off (and exceeded) and then we truncate the size of
			 * this i/o request so that it is page aligned so that
			 * we can finally issue the i/o on the transaction.
			 */
			if (aligned_ofs > upl_offset) {
				io_size = (u_int)(aligned_ofs - upl_offset);
				pg_count--;
			}
		}

		if (!(mp->mnt_kern_flag & MNTK_VIRTUALDEV)) {
			/*
			 * if we're not targeting a virtual device i.e. a disk image
			 * it's safe to dip into the reserve pool since real devices
			 * can complete this I/O request without requiring additional
			 * bufs from the alloc_io_buf pool
			 */
			priv = 1;
		} else if ((flags & CL_ASYNC) && !(flags & CL_PAGEOUT) && !cbp_head) {
			/*
			 * Throttle the speculative IO
			 *
			 * We can only throttle this if it is the first iobuf
			 * for the transaction. alloc_io_buf implements
			 * additional restrictions for diskimages anyway.
			 */
			priv = 0;
		} else {
			priv = 1;
		}

		cbp = alloc_io_buf(vp, priv);

		if (flags & CL_PAGEOUT) {
			u_int i;

			/*
			 * since blocks are in offsets of CLUSTER_IO_BLOCK_SIZE, scale
			 * iteration to (PAGE_SIZE * pg_count) of blks.
			 */
			for (i = 0; i < (PAGE_SIZE * pg_count) / CLUSTER_IO_BLOCK_SIZE; i++) {
				if (buf_invalblkno(vp, lblkno + i, 0) == EBUSY) {
					panic("BUSY bp found in cluster_io");
				}
			}
		}
		if (flags & CL_ASYNC) {
			if (buf_setcallback(cbp, (void *)cluster_iodone, callback_arg)) {
				panic("buf_setcallback failed");
			}
		}
		cbp->b_cliodone = (void *)callback;
		cbp->b_flags |= io_flags;
		if (flags & CL_NOCACHE) {
			cbp->b_attr.ba_flags |= BA_NOCACHE;
		}
		if (verify_block_size) {
			cbp->b_attr.ba_flags |= BA_WILL_VERIFY;
			if (verify_kind) {
				cbp->b_attr.ba_verify_type = verify_kind;
			}
		}

		cbp->b_lblkno = lblkno;
		cbp->b_clfoffset = f_offset;
		cbp->b_blkno  = blkno;
		cbp->b_bcount = io_size;

		if (buf_setupl(cbp, upl, (uint32_t)upl_offset)) {
			panic("buf_setupl failed");
		}
#if CONFIG_IOSCHED
		upl_set_blkno(upl, upl_offset, io_size, blkno);
#endif
		cbp->b_trans_next = (buf_t)NULL;

		if ((cbp->b_iostate = (void *)iostate)) {
			/*
			 * caller wants to track the state of this
			 * io... bump the amount issued against this stream
			 */
			iostate->io_issued += io_size;
		}

		if (flags & CL_READ) {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 26)) | DBG_FUNC_NONE,
			    (int)cbp->b_lblkno, (int)cbp->b_blkno, upl_offset, io_size, 0);
		} else {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 27)) | DBG_FUNC_NONE,
			    (int)cbp->b_lblkno, (int)cbp->b_blkno, upl_offset, io_size, 0);
		}

		if (cbp_head) {
			cbp_tail->b_trans_next = cbp;
			cbp_tail = cbp;
		} else {
			cbp_head = cbp;
			cbp_tail = cbp;

			if ((cbp_head->b_real_bp = real_bp)) {
				real_bp = (buf_t)NULL;
			}
		}
		*(buf_t *)(&cbp->b_trans_head) = cbp_head;

		trans_count++;

		upl_offset += io_size;
		f_offset   += io_size;
		size       -= io_size;
		/*
		 * keep track of how much of the original request
		 * that we've actually completed... non_rounded_size
		 * may go negative due to us rounding the request
		 * to a page size multiple (i.e.  size > non_rounded_size)
		 */
		non_rounded_size -= io_size;

		if (non_rounded_size <= 0) {
			/*
			 * we've transferred all of the data in the original
			 * request, but we were unable to complete the tail
			 * of the last page because the file didn't have
			 * an allocation to back that portion... this is ok.
			 */
			size = 0;
		}
		if (size == 0) {
			/*
			 * we have no more I/O to issue, so go
			 * finish the final transaction
			 */
			need_EOT = TRUE;
		} else if (((flags & CL_DEV_MEMORY) || (upl_offset & PAGE_MASK) == 0) &&
		    ((flags & CL_ASYNC) || trans_count > max_trans_count)) {
			/*
			 * I/O directed to physically contiguous memory...
			 * which doesn't have a requirement to make sure we 'fill' a page
			 * or...
			 * the current I/O we've prepared fully
			 * completes the last page in this request
			 * and ...
			 * it's either an ASYNC request or
			 * we've already accumulated more than 8 I/O's into
			 * this transaction so mark it as complete so that
			 * it can finish asynchronously or via the cluster_complete_transaction
			 * below if the request is synchronous
			 */
			need_EOT = TRUE;
		}
		if (need_EOT == TRUE) {
			cluster_EOT(cbp_head, cbp_tail, size == 0 ? zero_offset : 0, verify_block_size);
		}

		if (flags & CL_THROTTLE) {
			(void)vnode_waitforwrites(vp, async_throttle, 0, 0, "cluster_io");
		}

		if (!(io_flags & B_READ)) {
			vnode_startwrite(vp);
		}

		if (flags & CL_RAW_ENCRYPTED) {
			/*
			 * User requested raw encrypted bytes.
			 * Twiddle the bit in the ba_flags for the buffer
			 */
			cbp->b_attr.ba_flags |= BA_RAW_ENCRYPTED_IO;
		}

		(void) VNOP_STRATEGY(cbp);

		if (need_EOT == TRUE) {
			if (!(flags & CL_ASYNC)) {
				cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 1);
			}

			need_EOT = FALSE;
			trans_count = 0;
			cbp_head = NULL;
		}
	}
	if (error) {
		int abort_size;

		io_size = 0;

		if (cbp_head) {
			/*
			 * Wait until all of the outstanding I/O
			 * for this partial transaction has completed
			 */
			cluster_wait_IO(cbp_head, (flags & CL_ASYNC));

			/*
			 * Rewind the upl offset to the beginning of the
			 * transaction.
			 */
			upl_offset = cbp_head->b_uploffset;
		}

		if (ISSET(flags, CL_COMMIT)) {
			cluster_handle_associated_upl(iostate, upl,
			    (upl_offset_t)upl_offset,
			    (upl_size_t)(upl_end_offset - upl_offset),
			    cbp_head ? cbp_head->b_clfoffset : f_offset);
		}

		// Free all the IO buffers in this transaction
		for (cbp = cbp_head; cbp;) {
			buf_t   cbp_next;

			size       += cbp->b_bcount;
			io_size    += cbp->b_bcount;

			cbp_next = cbp->b_trans_next;
			free_io_buf(cbp);
			cbp = cbp_next;
		}

		if (iostate) {
			int need_wakeup = 0;

			/*
			 * update the error condition for this stream
			 * since we never really issued the io
			 * just go ahead and adjust it back
			 */
			lck_mtx_lock_spin(&iostate->io_mtxp);

			if (iostate->io_error == 0) {
				iostate->io_error = error;
			}
			iostate->io_issued -= io_size;

			if (iostate->io_wanted) {
				/*
				 * someone is waiting for the state of
				 * this io stream to change
				 */
				iostate->io_wanted = 0;
				need_wakeup = 1;
			}
			lck_mtx_unlock(&iostate->io_mtxp);

			if (need_wakeup) {
				wakeup((caddr_t)&iostate->io_wanted);
			}
		}

		if (flags & CL_COMMIT) {
			int     upl_flags;

			pg_offset  = upl_offset & PAGE_MASK;
			abort_size = (int)((upl_end_offset - upl_offset + PAGE_MASK) & ~PAGE_MASK);

			upl_flags = cluster_ioerror(upl, (int)(upl_offset - pg_offset),
			    abort_size, error, io_flags, vp);

			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 28)) | DBG_FUNC_NONE,
			    upl, upl_offset - pg_offset, abort_size, (error << 24) | upl_flags, 0);
		}
		if (retval == 0) {
			retval = error;
		}
	} else if (cbp_head) {
		panic("%s(): cbp_head is not NULL.", __FUNCTION__);
	}

	if (real_bp) {
		/*
		 * can get here if we either encountered an error
		 * or we completely zero-filled the request and
		 * no I/O was issued
		 */
		if (error) {
			real_bp->b_flags |= B_ERROR;
			real_bp->b_error = error;
		}
		buf_biodone(real_bp);
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_END, (int)f_offset, size, upl_offset, retval, 0);

	return retval;
}

#define reset_vector_run_state()                                                                                \
	issueVectorUPL = vector_upl_offset = vector_upl_index = vector_upl_iosize = vector_upl_size = 0;

static int
vector_cluster_io(vnode_t vp, upl_t vector_upl, vm_offset_t vector_upl_offset, off_t v_upl_uio_offset, int vector_upl_iosize,
    int io_flag, buf_t real_bp, struct clios *iostate, int (*callback)(buf_t, void *), void *callback_arg)
{
	vector_upl_set_pagelist(vector_upl);

	if (io_flag & CL_READ) {
		if (vector_upl_offset == 0 && ((vector_upl_iosize & PAGE_MASK) == 0)) {
			io_flag &= ~CL_PRESERVE; /*don't zero fill*/
		} else {
			io_flag |= CL_PRESERVE; /*zero fill*/
		}
	}
	return cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, real_bp, iostate, callback, callback_arg);
}

static int
cluster_read_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize, int (*callback)(buf_t, void *), void *callback_arg, int bflag)
{
	int           pages_in_prefetch;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_START,
	    (int)f_offset, size, (int)filesize, 0, 0);

	if (f_offset >= filesize) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_END,
		    (int)f_offset, 0, 0, 0, 0);
		return 0;
	}
	if ((off_t)size > (filesize - f_offset)) {
		size = (u_int)(filesize - f_offset);
	}
	pages_in_prefetch = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;

	advisory_read_ext(vp, filesize, f_offset, size, callback, callback_arg, bflag);

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_END,
	    (int)f_offset + size, pages_in_prefetch, 0, 1, 0);

	return pages_in_prefetch;
}



static void
cluster_read_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *rap, int (*callback)(buf_t, void *), void *callback_arg,
    int bflag)
{
	daddr64_t       r_addr;
	off_t           f_offset;
	int             size_of_prefetch;
	u_int           max_prefetch;


	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_START,
	    (int)extent->b_addr, (int)extent->e_addr, (int)rap->cl_lastr, 0, 0);

	if (extent->b_addr == rap->cl_lastr && extent->b_addr == extent->e_addr) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
		    rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 0, 0);
		return;
	}
	if (rap->cl_lastr == -1 || (extent->b_addr != rap->cl_lastr && extent->b_addr != (rap->cl_lastr + 1))) {
		rap->cl_ralen = 0;
		rap->cl_maxra = 0;

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
		    rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 1, 0);

		return;
	}

	max_prefetch = cluster_max_prefetch(vp,
	    cluster_max_io_size(vp->v_mount, CL_READ), speculative_prefetch_max);

	if (max_prefetch <= PAGE_SIZE) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
		    rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 6, 0);
		return;
	}
	if (extent->e_addr < rap->cl_maxra && rap->cl_ralen >= 4) {
		if ((rap->cl_maxra - extent->e_addr) > (rap->cl_ralen / 4)) {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
			    rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 2, 0);
			return;
		}
	}
	r_addr = MAX(extent->e_addr, rap->cl_maxra) + 1;
	f_offset = (off_t)(r_addr * PAGE_SIZE_64);

	size_of_prefetch = 0;

	ubc_range_op(vp, f_offset, f_offset + PAGE_SIZE_64, UPL_ROP_PRESENT, &size_of_prefetch);

	if (size_of_prefetch) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
		    rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 3, 0);
		return;
	}
	if (f_offset < filesize) {
		daddr64_t read_size;

		rap->cl_ralen = rap->cl_ralen ? min(max_prefetch / PAGE_SIZE, rap->cl_ralen << 1) : 1;

		read_size = (extent->e_addr + 1) - extent->b_addr;

		if (read_size > rap->cl_ralen) {
			if (read_size > max_prefetch / PAGE_SIZE) {
				rap->cl_ralen = max_prefetch / PAGE_SIZE;
			} else {
				rap->cl_ralen = (int)read_size;
			}
		}
		size_of_prefetch = cluster_read_prefetch(vp, f_offset, rap->cl_ralen * PAGE_SIZE, filesize, callback, callback_arg, bflag);

		if (size_of_prefetch) {
			rap->cl_maxra = (r_addr + size_of_prefetch) - 1;
		}
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
	    rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 4, 0);
}


int
cluster_pageout(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
    int size, off_t filesize, int flags)
{
	return cluster_pageout_ext(vp, upl, upl_offset, f_offset, size, filesize, flags, NULL, NULL);
}


int
cluster_pageout_ext(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
    int size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
{
	int           io_size;
	int           rounded_size;
	off_t         max_size;
	int           local_flags;

	local_flags = CL_PAGEOUT | CL_THROTTLE;

	if ((flags & UPL_IOSYNC) == 0) {
		local_flags |= CL_ASYNC;
	}
	if ((flags & UPL_NOCOMMIT) == 0) {
		local_flags |= CL_COMMIT;
	}
	if ((flags & UPL_KEEPCACHED)) {
		local_flags |= CL_KEEPCACHED;
	}
	if (flags & UPL_PAGING_ENCRYPTED) {
		local_flags |= CL_ENCRYPTED;
	}


	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 52)) | DBG_FUNC_NONE,
	    (int)f_offset, size, (int)filesize, local_flags, 0);

	/*
	 * If they didn't specify any I/O, then we are done...
	 * we can't issue an abort because we don't know how
	 * big the upl really is
	 */
	if (size <= 0) {
		return EINVAL;
	}

	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
		if (local_flags & CL_COMMIT) {
			ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
		}
		return EROFS;
	}
	/*
	 * can't page-in from a negative offset
	 * or if we're starting beyond the EOF
	 * or if the file offset isn't page aligned
	 * or the size requested isn't a multiple of PAGE_SIZE
	 */
	if (f_offset < 0 || f_offset >= filesize ||
	    (f_offset & PAGE_MASK_64) || (size & PAGE_MASK)) {
		if (local_flags & CL_COMMIT) {
			ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
		}
		return EINVAL;
	}
	max_size = filesize - f_offset;

	if (size < max_size) {
		io_size = size;
	} else {
		io_size = (int)max_size;
	}

	rounded_size = (io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;

	if (size > rounded_size) {
		if (local_flags & CL_COMMIT) {
			ubc_upl_abort_range(upl, upl_offset + rounded_size, size - rounded_size,
			    UPL_ABORT_FREE_ON_EMPTY);
		}
	}
	return cluster_io(vp, upl, upl_offset, f_offset, io_size,
	           local_flags, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
}


int
cluster_pagein(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
    int size, off_t filesize, int flags)
{
	return cluster_pagein_ext(vp, upl, upl_offset, f_offset, size, filesize, flags, NULL, NULL);
}

#define SPLIT_PAGEIN_MAX_IOSIZE 32768

/*
 * Do a big pagein request as multiple I/Os - the first I/O will be for
 * SPLIT_PAGEIN_MAX_IOSIZE (32K)sized which includes the page that the caused
 * the fault and then i/o will be initiated for the remaining.
 */
static int
cluster_handle_split_pagein(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
    u_int io_size, int rounded_size, int local_flags, int (*callback)(buf_t, void *), void *callback_arg)
{
	upl_page_info_t *pl = ubc_upl_pageinfo(upl);
	const off_t start_f_offset = f_offset;
	const upl_offset_t start_upl_offset = upl_offset;
	const int start_pg = upl_offset >> PAGE_SHIFT;
	const int last_pg = ((upl_offset + rounded_size) >> PAGE_SHIFT) - 1;
	u_int split_io_size = SPLIT_PAGEIN_MAX_IOSIZE;
	u_int head_io_size = 0;
	int retval = 0;
	int error = 0;
	int pg;

	assert(SPLIT_PAGEIN_MAX_IOSIZE >= (2 * PAGE_SIZE));

	for (pg = start_pg; (pg <= last_pg) && !(upl_page_is_needed(pl, pg)); pg++) {
		;
	}

	/*
	 * The global variables affecting behaviour
	 * split_all_pgin -> Split pageins even if we don't find the needed page.
	 * split_pgin_headio -> for a pagein in which there is a head calculated,
	 *                   do the head i/o or not.
	 *
	 * split_all_pgin_equal -> split the entire bug request into equal sized small i/os of 32K.
	 *
	 * Whichever way the i/o is split, the i/o for the needed page always happens first and then we decide
	 * whether we have to do i/o for the head and then if we need to issue equal sized i/o.
	 *
	 * By default we are set up to do only the i/o for the needed page, followed by a "unsplit" tail.
	 */
	if ((pg > start_pg) && (pg <= last_pg)) {
		head_io_size = ((pg - start_pg) * PAGE_SIZE);

		if (head_io_size < SPLIT_PAGEIN_MAX_IOSIZE) {
			head_io_size = 0;
		} else if (!split_all_pgin) {
			goto out;
		} else if ((rounded_size - head_io_size) <= SPLIT_PAGEIN_MAX_IOSIZE) {
			head_io_size = (rounded_size - SPLIT_PAGEIN_MAX_IOSIZE);
		} else {
			head_io_size &= ~(SPLIT_PAGEIN_MAX_IOSIZE - 1);
		}

		assertf(io_size > head_io_size, "io_size is %d, head_io_size = %d", io_size, head_io_size);

		if (head_io_size) {
			upl_offset += head_io_size;
			f_offset += head_io_size;
			io_size -= head_io_size;

			if (!split_pgin_headio) {
				if (local_flags & CL_COMMIT) {
					ubc_upl_abort_range(upl, start_upl_offset, head_io_size,
					    UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
				}
				head_io_size = 0;
			}

			split_io_size = MIN(SPLIT_PAGEIN_MAX_IOSIZE, io_size);
		}

		assertf(io_size >= split_io_size, "io_size is %d, split_io_size = %d", io_size, split_io_size);
	} else if ((pg > last_pg) && !split_all_pgin) {
		goto out;
	}

	/* This is the 32K i/o for the "needed" page */
	retval = cluster_io(vp, upl, upl_offset, f_offset, split_io_size,
	    local_flags | CL_READ | CL_PAGEIN, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);

	io_size -= split_io_size;

	if (io_size) {
		upl_offset += split_io_size;
		f_offset += split_io_size;
	} else if (head_io_size) {
		io_size = head_io_size;
		head_io_size = 0;
		upl_offset = start_upl_offset;
		f_offset = start_f_offset;
	}

	while (io_size) {
		if (split_all_pgin_equal && (io_size > SPLIT_PAGEIN_MAX_IOSIZE)) {
			split_io_size = SPLIT_PAGEIN_MAX_IOSIZE;
		} else {
			split_io_size = io_size;
		}

		assertf(io_size >= split_io_size, "io_size is %d, split_io_size = %d", io_size, split_io_size);

		/* We have to issue this i/o anyway even if we get an error from any of the previous ones */
		error = cluster_io(vp, upl, upl_offset, f_offset, split_io_size,
		    local_flags | CL_READ | CL_PAGEIN, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
		if (!retval) {
			retval = error;
		}

		io_size -= split_io_size;

		if ((io_size == 0) && head_io_size) {
			io_size = head_io_size;
			head_io_size = 0;
			upl_offset = start_upl_offset;
			f_offset = start_f_offset;
		} else if (io_size) {
			upl_offset += split_io_size;
			f_offset += split_io_size;
		}
	}

	return retval;
out:
	return cluster_io(vp, upl, upl_offset, f_offset, io_size,
	           local_flags | CL_READ | CL_PAGEIN, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
}

int
cluster_pagein_ext(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
    int size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
{
	u_int         io_size;
	int           rounded_size;
	off_t         max_size;
	int           retval;
	int           local_flags = 0;

	if (upl == NULL || size < 0) {
		panic("cluster_pagein: NULL upl passed in");
	}

	if ((flags & UPL_IOSYNC) == 0) {
		local_flags |= CL_ASYNC;
	}
	if ((flags & UPL_NOCOMMIT) == 0) {
		local_flags |= CL_COMMIT;
	}
	if (flags & UPL_IOSTREAMING) {
		local_flags |= CL_IOSTREAMING;
	}
	if (flags & UPL_PAGING_ENCRYPTED) {
		local_flags |= CL_ENCRYPTED;
	}


	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 56)) | DBG_FUNC_NONE,
	    (int)f_offset, size, (int)filesize, local_flags, 0);

	/*
	 * can't page-in from a negative offset
	 * or if we're starting beyond the EOF
	 * or if the file offset isn't page aligned
	 * or the size requested isn't a multiple of PAGE_SIZE
	 */
	if (f_offset < 0 || f_offset >= filesize ||
	    (f_offset & PAGE_MASK_64) || (size & PAGE_MASK) || (upl_offset & PAGE_MASK)) {
		if (local_flags & CL_COMMIT) {
			ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
		}

		if (f_offset >= filesize) {
			ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_CLUSTER, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_CL_PGIN_PAST_EOF), 0 /* arg */);
		}

		return EINVAL;
	}
	max_size = filesize - f_offset;

	if (size < max_size) {
		io_size = size;
	} else {
		io_size = (int)max_size;
	}

	rounded_size = (io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;

	if (size > rounded_size && (local_flags & CL_COMMIT)) {
		ubc_upl_abort_range(upl, upl_offset + rounded_size,
		    size - rounded_size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
	}

	if ((io_size > SPLIT_PAGEIN_MAX_IOSIZE) && vnode_isonssd(vp) && split_pgin) {
		return cluster_handle_split_pagein(vp, upl, upl_offset, f_offset, io_size,
		           rounded_size, local_flags, callback, callback_arg);
	}

	retval = cluster_io(vp, upl, upl_offset, f_offset, io_size,
	    local_flags | CL_READ | CL_PAGEIN, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);

	return retval;
}


int
cluster_bp(buf_t bp)
{
	return cluster_bp_ext(bp, NULL, NULL);
}


int
cluster_bp_ext(buf_t bp, int (*callback)(buf_t, void *), void *callback_arg)
{
	off_t  f_offset;
	int    flags;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 19)) | DBG_FUNC_START,
	    bp, (int)bp->b_lblkno, bp->b_bcount, bp->b_flags, 0);

	if (bp->b_flags & B_READ) {
		flags = CL_ASYNC | CL_READ;
	} else {
		flags = CL_ASYNC;
	}
	if (bp->b_flags & B_PASSIVE) {
		flags |= CL_PASSIVE;
	}

	f_offset = ubc_blktooff(bp->b_vp, bp->b_lblkno);

	return cluster_io(bp->b_vp, bp->b_upl, 0, f_offset, bp->b_bcount, flags, bp, (struct clios *)NULL, callback, callback_arg);
}



int
cluster_write(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff, int xflags)
{
	return cluster_write_ext(vp, uio, oldEOF, newEOF, headOff, tailOff, xflags, NULL, NULL);
}


int
cluster_write_ext(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff,
    int xflags, int (*callback)(buf_t, void *), void *callback_arg)
{
	user_ssize_t    cur_resid;
	int             retval = 0;
	int             flags;
	int             zflags;
	int             bflag;
	int             write_type = IO_COPY;
	u_int32_t       write_length = 0, saved_write_length;
	uint32_t        min_direct_size = MIN_DIRECT_WRITE_SIZE;

	flags = xflags;

	if (flags & IO_PASSIVE) {
		bflag = CL_PASSIVE;
	} else {
		bflag = 0;
	}

	if (vp->v_flag & VNOCACHE_DATA) {
		flags |= IO_NOCACHE;
		bflag |= CL_NOCACHE;
	}
	if (uio == NULL) {
		/*
		 * no user data...
		 * this call is being made to zero-fill some range in the file
		 */
		retval = cluster_write_copy(vp, NULL, (u_int32_t)0, oldEOF, newEOF, headOff, tailOff, flags, callback, callback_arg);

		return retval;
	}
	/*
	 * do a write through the cache if one of the following is true....
	 *   NOCACHE is not true or NODIRECT is true
	 *   the uio request doesn't target USERSPACE
	 * otherwise, find out if we want the direct or contig variant for
	 * the first vector in the uio request
	 */
	if (((flags & (IO_NOCACHE | IO_NODIRECT)) == IO_NOCACHE) && UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) {
		if (flags & IO_NOCACHE_SWRITE) {
			uint32_t fs_bsize = vp->v_mount->mnt_vfsstat.f_bsize;

			if (fs_bsize && (fs_bsize < MIN_DIRECT_WRITE_SIZE) &&
			    ((fs_bsize & (fs_bsize - 1)) == 0)) {
				min_direct_size = fs_bsize;
			}
		}
		retval = cluster_io_type(uio, &write_type, &write_length, min_direct_size);
	}

	if ((flags & (IO_TAILZEROFILL | IO_HEADZEROFILL)) && write_type == IO_DIRECT) {
		/*
		 * must go through the cached variant in this case
		 */
		write_type = IO_COPY;
	}

	while ((cur_resid = uio_resid(uio)) && uio->uio_offset < newEOF && retval == 0) {
		switch (write_type) {
		case IO_COPY:
			/*
			 * make sure the uio_resid isn't too big...
			 * internally, we want to handle all of the I/O in
			 * chunk sizes that fit in a 32 bit int
			 */
			if (cur_resid > (user_ssize_t)(MAX_IO_REQUEST_SIZE)) {
				/*
				 * we're going to have to call cluster_write_copy
				 * more than once...
				 *
				 * only want the last call to cluster_write_copy to
				 * have the IO_TAILZEROFILL flag set and only the
				 * first call should have IO_HEADZEROFILL
				 */
				zflags = flags & ~IO_TAILZEROFILL;
				flags &= ~IO_HEADZEROFILL;

				write_length = MAX_IO_REQUEST_SIZE;
			} else {
				/*
				 * last call to cluster_write_copy
				 */
				zflags = flags;

				write_length = (u_int32_t)cur_resid;
			}
			retval = cluster_write_copy(vp, uio, write_length, oldEOF, newEOF, headOff, tailOff, zflags, callback, callback_arg);
			break;

		case IO_CONTIG:
			zflags = flags & ~(IO_TAILZEROFILL | IO_HEADZEROFILL);

			if (flags & IO_HEADZEROFILL) {
				/*
				 * only do this once per request
				 */
				flags &= ~IO_HEADZEROFILL;

				retval = cluster_write_copy(vp, (struct uio *)0, (u_int32_t)0, (off_t)0, uio->uio_offset,
				    headOff, (off_t)0, zflags | IO_HEADZEROFILL | IO_SYNC, callback, callback_arg);
				if (retval) {
					break;
				}
			}
			retval = cluster_write_contig(vp, uio, newEOF, &write_type, &write_length, callback, callback_arg, bflag);

			if (retval == 0 && (flags & IO_TAILZEROFILL) && uio_resid(uio) == 0) {
				/*
				 * we're done with the data from the user specified buffer(s)
				 * and we've been requested to zero fill at the tail
				 * treat this as an IO_HEADZEROFILL which doesn't require a uio
				 * by rearranging the args and passing in IO_HEADZEROFILL
				 */

				/*
				 * Update the oldEOF to reflect the current EOF. If the UPL page
				 * to zero-fill is not valid (when F_NOCACHE is set), the
				 * cluster_write_copy() will perform RMW on the UPL page when
				 * the oldEOF is not aligned on page boundary due to unaligned
				 * write.
				 */
				if (uio->uio_offset > oldEOF) {
					oldEOF = uio->uio_offset;
				}
				retval = cluster_write_copy(vp, (struct uio *)0, (u_int32_t)0, (off_t)oldEOF, tailOff, uio->uio_offset,
				    (off_t)0, zflags | IO_HEADZEROFILL | IO_SYNC, callback, callback_arg);
			}
			break;

		case IO_DIRECT:
			/*
			 * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
			 */
			saved_write_length = write_length;
			retval = cluster_write_direct(vp, uio, oldEOF, newEOF, &write_type, &write_length, flags, callback, callback_arg, min_direct_size);
			if (retval == ENOTSUP) {
				/* direct I/O didn't work; retry with cached I/O */
//				printf("*******  FBDP %s:%d ENOTSUP cnt %d resid 0x%llx offset 0x%llx write_length 0x%x -> 0x%x\n", __FUNCTION__, __LINE__, uio_iovcnt(uio), (uint64_t) uio_resid(uio), uio_offset(uio), write_length, saved_write_length);
				write_length = saved_write_length;
				write_type = IO_COPY;
				retval = 0;
			}
			break;

		case IO_UNKNOWN:
			retval = cluster_io_type(uio, &write_type, &write_length, min_direct_size);
			break;
		}
		/*
		 * in case we end up calling cluster_write_copy (from cluster_write_direct)
		 * multiple times to service a multi-vector request that is not aligned properly
		 * we need to update the oldEOF so that we
		 * don't zero-fill the head of a page if we've successfully written
		 * data to that area... 'cluster_write_copy' will zero-fill the head of a
		 * page that is beyond the oldEOF if the write is unaligned... we only
		 * want that to happen for the very first page of the cluster_write,
		 * NOT the first page of each vector making up a multi-vector write.
		 */
		if (uio->uio_offset > oldEOF) {
			oldEOF = uio->uio_offset;
		}
	}
	return retval;
}


static int
cluster_write_direct(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, int *write_type, u_int32_t *write_length,
    int flags, int (*callback)(buf_t, void *), void *callback_arg, uint32_t min_io_size)
{
	upl_t            upl = NULL;
	upl_page_info_t  *pl;
	vm_offset_t      upl_offset;
	vm_offset_t      vector_upl_offset = 0;
	u_int32_t        io_req_size;
	u_int32_t        offset_in_file;
	u_int32_t        offset_in_iovbase;
	u_int32_t        io_size;
	int              io_flag = 0;
	upl_size_t       upl_size = 0, vector_upl_size = 0;
	vm_size_t        upl_needed_size;
	mach_msg_type_number_t  pages_in_pl = 0;
	upl_control_flags_t upl_flags;
	kern_return_t    kret = KERN_SUCCESS;
	mach_msg_type_number_t  i = 0;
	int              force_data_sync;
	int              retval = 0;
	int              first_IO = 1;
	struct clios     iostate;
	user_addr_t      iov_base;
	u_int32_t        mem_alignment_mask;
	u_int32_t        devblocksize;
	u_int32_t        max_io_size;
	u_int32_t        max_upl_size;
	u_int32_t        max_vector_size;
	u_int32_t        bytes_outstanding_limit;
	boolean_t        io_throttled = FALSE;

	u_int32_t        vector_upl_iosize = 0;
	int              issueVectorUPL = 0, useVectorUPL = (uio->uio_iovcnt > 1);
	off_t            v_upl_uio_offset = 0;
	int              vector_upl_index = 0;
	upl_t            vector_upl = NULL;
	uio_t            snapshot_uio = NULL;

	uint32_t         io_align_mask;

	/*
	 * When we enter this routine, we know
	 *  -- the resid will not exceed iov_len
	 */
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_START,
	    (int)uio->uio_offset, *write_length, (int)newEOF, 0, 0);

	assert(vm_map_page_shift(current_map()) >= PAGE_SHIFT);

	max_upl_size = cluster_max_io_size(vp->v_mount, CL_WRITE);

	io_flag = CL_ASYNC | CL_PRESERVE | CL_COMMIT | CL_THROTTLE | CL_DIRECT_IO;

	if (flags & IO_PASSIVE) {
		io_flag |= CL_PASSIVE;
	}

	if (flags & IO_NOCACHE) {
		io_flag |= CL_NOCACHE;
	}

	if (flags & IO_SKIP_ENCRYPTION) {
		io_flag |= CL_ENCRYPTED;
	}

	iostate.io_completed = 0;
	iostate.io_issued = 0;
	iostate.io_error = 0;
	iostate.io_wanted = 0;

	lck_mtx_init(&iostate.io_mtxp, &cl_mtx_grp, LCK_ATTR_NULL);

	mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
	devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;

	if (devblocksize == 1) {
		/*
		 * the AFP client advertises a devblocksize of 1
		 * however, its BLOCKMAP routine maps to physical
		 * blocks that are PAGE_SIZE in size...
		 * therefore we can't ask for I/Os that aren't page aligned
		 * or aren't multiples of PAGE_SIZE in size
		 * by setting devblocksize to PAGE_SIZE, we re-instate
		 * the old behavior we had before the mem_alignment_mask
		 * changes went in...
		 */
		devblocksize = PAGE_SIZE;
	}

	io_align_mask = PAGE_MASK;
	if (min_io_size < MIN_DIRECT_WRITE_SIZE) {
		/* The process has opted into fs blocksize direct io writes */
		assert((min_io_size & (min_io_size - 1)) == 0);
		io_align_mask = min_io_size - 1;
		io_flag |= CL_DIRECT_IO_FSBLKSZ;
	}

	if (uio_iovcnt(uio) > 1) {
		/* vector uio -> take a snapshot so we can rollback if needed */
		if (snapshot_uio) {
			uio_free(snapshot_uio);
			snapshot_uio = NULL;
		}
		snapshot_uio = uio_duplicate(uio);
	}

next_dwrite:
	io_req_size = *write_length;
	iov_base = uio_curriovbase(uio);

	offset_in_file = (u_int32_t)(uio->uio_offset & io_align_mask);
	offset_in_iovbase = (u_int32_t)(iov_base & mem_alignment_mask);

	if (offset_in_file || offset_in_iovbase) {
		/*
		 * one of the 2 important offsets is misaligned
		 * so fire an I/O through the cache for this entire vector
		 */
		goto wait_for_dwrites;
	}
	if (iov_base & (devblocksize - 1)) {
		/*
		 * the offset in memory must be on a device block boundary
		 * so that we can guarantee that we can generate an
		 * I/O that ends on a page boundary in cluster_io
		 */
		goto wait_for_dwrites;
	}

	task_update_logical_writes(current_task(), (io_req_size & ~PAGE_MASK), TASK_WRITE_IMMEDIATE, vp);
	while ((io_req_size >= PAGE_SIZE || io_req_size >= min_io_size) && uio->uio_offset < newEOF && retval == 0) {
		int     throttle_type;

		if ((throttle_type = cluster_is_throttled(vp))) {
			uint32_t max_throttle_size = calculate_max_throttle_size(vp);

			/*
			 * we're in the throttle window, at the very least
			 * we want to limit the size of the I/O we're about
			 * to issue
			 */
			if ((flags & IO_RETURN_ON_THROTTLE) && throttle_type == THROTTLE_NOW) {
				/*
				 * we're in the throttle window and at least 1 I/O
				 * has already been issued by a throttleable thread
				 * in this window, so return with EAGAIN to indicate
				 * to the FS issuing the cluster_write call that it
				 * should now throttle after dropping any locks
				 */
				throttle_info_update_by_mount(vp->v_mount);

				io_throttled = TRUE;
				goto wait_for_dwrites;
			}
			max_vector_size = max_throttle_size;
			max_io_size = max_throttle_size;
		} else {
			max_vector_size = MAX_VECTOR_UPL_SIZE;
			max_io_size = max_upl_size;
		}

		if (first_IO) {
			cluster_syncup(vp, newEOF, callback, callback_arg, callback ? PUSH_SYNC : 0);
			first_IO = 0;
		}
		io_size  = io_req_size & ~io_align_mask;
		iov_base = uio_curriovbase(uio);

		if (io_size > max_io_size) {
			io_size = max_io_size;
		}

		if (useVectorUPL && (iov_base & PAGE_MASK)) {
			/*
			 * We have an iov_base that's not page-aligned.
			 * Issue all I/O's that have been collected within
			 * this Vectored UPL.
			 */
			if (vector_upl_index) {
				retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
				if (retval == ENOTSUP) {
					goto enotsup;
				}
				reset_vector_run_state();
			}

			/*
			 * After this point, if we are using the Vector UPL path and the base is
			 * not page-aligned then the UPL with that base will be the first in the vector UPL.
			 */
		}

		upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
		upl_needed_size = (upl_offset + io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_START,
		    (int)upl_offset, upl_needed_size, (int)iov_base, io_size, 0);

		vm_map_t map = UIO_SEG_IS_USER_SPACE(uio->uio_segflg) ? current_map() : kernel_map;
		for (force_data_sync = 0; force_data_sync < 3; force_data_sync++) {
			pages_in_pl = 0;
			upl_size = (upl_size_t)upl_needed_size;
			upl_flags = UPL_FILE_IO | UPL_COPYOUT_FROM | UPL_NO_SYNC |
			    UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;

			kret = vm_map_get_upl(map,
#if HAS_MTE || HAS_MTE_EMULATION_SHIMS
			    vm_memtag_canonicalize(map, (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK))),
#else /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */
			    (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
#endif /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */
			    &upl_size,
			    &upl,
			    NULL,
			    &pages_in_pl,
			    &upl_flags,
			    VM_KERN_MEMORY_FILE,
			    force_data_sync);

			if (kret != KERN_SUCCESS) {
				KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
				    0, 0, 0, kret, 0);
				/*
				 * failed to get pagelist
				 *
				 * we may have already spun some portion of this request
				 * off as async requests... we need to wait for the I/O
				 * to complete before returning
				 */
				goto wait_for_dwrites;
			}
			pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
			pages_in_pl = upl_size / PAGE_SIZE;

			for (i = 0; i < pages_in_pl; i++) {
				if (!upl_valid_page(pl, i)) {
					break;
				}
			}
			if (i == pages_in_pl) {
				break;
			}

			/*
			 * didn't get all the pages back that we
			 * needed... release this upl and try again
			 */
			ubc_upl_abort(upl, 0);
		}
		if (force_data_sync >= 3) {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
			    i, pages_in_pl, upl_size, kret, 0);
			/*
			 * for some reason, we couldn't acquire a hold on all
			 * the pages needed in the user's address space
			 *
			 * we may have already spun some portion of this request
			 * off as async requests... we need to wait for the I/O
			 * to complete before returning
			 */
			goto wait_for_dwrites;
		}

		/*
		 * Consider the possibility that upl_size wasn't satisfied.
		 */
		if (upl_size < upl_needed_size) {
			if (upl_size && upl_offset == 0) {
				io_size = upl_size;
			} else {
				io_size = 0;
			}
		}
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
		    (int)upl_offset, upl_size, (int)iov_base, io_size, 0);

		if (io_size == 0) {
			ubc_upl_abort(upl, 0);
			upl = NULL;
			/*
			 * we may have already spun some portion of this request
			 * off as async requests... we need to wait for the I/O
			 * to complete before returning
			 */
			goto wait_for_dwrites;
		}

		if (useVectorUPL) {
			vm_offset_t end_off = ((iov_base + io_size) & PAGE_MASK);
			if (end_off) {
				issueVectorUPL = 1;
			}
			/*
			 * After this point, if we are using a vector UPL, then
			 * either all the UPL elements end on a page boundary OR
			 * this UPL is the last element because it does not end
			 * on a page boundary.
			 */
		}

		/*
		 * we want push out these writes asynchronously so that we can overlap
		 * the preparation of the next I/O
		 * if there are already too many outstanding writes
		 * wait until some complete before issuing the next
		 */
		if (vp->v_mount->mnt_minsaturationbytecount) {
			bytes_outstanding_limit = vp->v_mount->mnt_minsaturationbytecount;
		} else {
			if (__improbable(os_mul_overflow(max_upl_size, IO_SCALE(vp, 2),
			    &bytes_outstanding_limit) ||
			    (bytes_outstanding_limit > overlapping_write_max))) {
				bytes_outstanding_limit = overlapping_write_max;
			}
		}

		cluster_iostate_wait(&iostate, bytes_outstanding_limit, "cluster_write_direct");

		if (iostate.io_error) {
			/*
			 * one of the earlier writes we issued ran into a hard error
			 * don't issue any more writes, cleanup the UPL
			 * that was just created but not used, then
			 * go wait for all writes that are part of this stream
			 * to complete before returning the error to the caller
			 */
			ubc_upl_abort(upl, 0);
			upl = NULL;

			goto wait_for_dwrites;
		}

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 77)) | DBG_FUNC_START,
		    (int)upl_offset, (int)uio->uio_offset, io_size, io_flag, 0);

		if (!useVectorUPL) {
			retval = cluster_io(vp, upl, upl_offset, uio->uio_offset,
			    io_size, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
		} else {
			if (!vector_upl_index) {
				vector_upl = vector_upl_create(upl_offset, uio->uio_iovcnt);
				v_upl_uio_offset = uio->uio_offset;
				vector_upl_offset = upl_offset;
			}

			vector_upl_set_subupl(vector_upl, upl, upl_size);
			vector_upl_set_iostate(vector_upl, upl, vector_upl_size, upl_size);
			vector_upl_index++;
			vector_upl_iosize += io_size;
			vector_upl_size += upl_size;

			if (issueVectorUPL || vector_upl_index == vector_upl_max_upls(vector_upl) || vector_upl_size >= max_vector_size) {
				retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
				if (retval != ENOTSUP) {
					reset_vector_run_state();
				}
			}
		}
		if (retval == ENOTSUP) {
enotsup:
			/*
			 * Can't do direct I/O.  Try again with cached I/O.
			 */
//			printf("*******  FBDP %s:%d ENOTSUP io_size 0%x resid 0x%llx\n", __FUNCTION__, __LINE__, io_size, uio_resid(uio));
			io_size = 0;
			if (snapshot_uio) {
				int restore_error;

				/*
				 * We've been collecting UPLs for this vector UPL and
				 * moving the uio along.  We need to undo that so that
				 * the I/O can continue where it actually stopped...
				 */
				restore_error = uio_restore(uio, snapshot_uio);
				assert(!restore_error);
				uio_free(snapshot_uio);
				snapshot_uio = NULL;
			}
			if (vector_upl_index) {
				ubc_upl_abort(vector_upl, 0);
				vector_upl = NULL;
			} else {
				ubc_upl_abort(upl, 0);
				upl = NULL;
			}
			goto wait_for_dwrites;
		}

		/*
		 * update the uio structure to
		 * reflect the I/O that we just issued
		 */
		uio_update(uio, (user_size_t)io_size);

		/*
		 * in case we end up calling through to cluster_write_copy to finish
		 * the tail of this request, we need to update the oldEOF so that we
		 * don't zero-fill the head of a page if we've successfully written
		 * data to that area... 'cluster_write_copy' will zero-fill the head of a
		 * page that is beyond the oldEOF if the write is unaligned... we only
		 * want that to happen for the very first page of the cluster_write,
		 * NOT the first page of each vector making up a multi-vector write.
		 */
		if (uio->uio_offset > oldEOF) {
			oldEOF = uio->uio_offset;
		}

		io_req_size -= io_size;

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 77)) | DBG_FUNC_END,
		    (int)upl_offset, (int)uio->uio_offset, io_req_size, retval, 0);
	} /* end while */

	if (retval == 0 && iostate.io_error == 0 && io_req_size == 0) {
		retval = cluster_io_type(uio, write_type, write_length, min_io_size);

		if (retval == 0 && *write_type == IO_DIRECT) {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_NONE,
			    (int)uio->uio_offset, *write_length, (int)newEOF, 0, 0);

			goto next_dwrite;
		}
	}

wait_for_dwrites:

	if (retval == 0 && iostate.io_error == 0 && useVectorUPL && vector_upl_index) {
		retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
		reset_vector_run_state();
	}
	/*
	 * make sure all async writes issued as part of this stream
	 * have completed before we return
	 */
	cluster_iostate_wait(&iostate, 0, "cluster_write_direct");

	if (iostate.io_error) {
		retval = iostate.io_error;
	}

	lck_mtx_destroy(&iostate.io_mtxp, &cl_mtx_grp);

	if (io_throttled == TRUE && retval == 0) {
		retval = EAGAIN;
	}

	if (io_req_size && retval == 0) {
		/*
		 * we couldn't handle the tail of this request in DIRECT mode
		 * so fire it through the copy path
		 *
		 * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
		 * so we can just pass 0 in for the headOff and tailOff
		 */
		if (uio->uio_offset > oldEOF) {
			oldEOF = uio->uio_offset;
		}

		retval = cluster_write_copy(vp, uio, io_req_size, oldEOF, newEOF, (off_t)0, (off_t)0, flags, callback, callback_arg);

		*write_type = IO_UNKNOWN;
	}

	if (snapshot_uio) {
		uio_free(snapshot_uio);
		snapshot_uio = NULL;
	}

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_END,
	    (int)uio->uio_offset, io_req_size, retval, 4, 0);

	return retval;
}


static int
cluster_write_contig(vnode_t vp, struct uio *uio, off_t newEOF, int *write_type, u_int32_t *write_length,
    int (*callback)(buf_t, void *), void *callback_arg, int bflag)
{
	upl_page_info_t *pl;
	addr64_t         src_paddr = 0;
	upl_t            upl[MAX_VECTS];
	vm_offset_t      upl_offset;
	u_int32_t        tail_size = 0;
	u_int32_t        io_size;
	u_int32_t        xsize;
	upl_size_t       upl_size;
	vm_size_t        upl_needed_size;
	mach_msg_type_number_t  pages_in_pl;
	upl_control_flags_t upl_flags;
	kern_return_t    kret;
	struct clios     iostate;
	int              error  = 0;
	int              cur_upl = 0;
	int              num_upl = 0;
	int              n;
	user_addr_t      iov_base;
	u_int32_t        devblocksize;
	u_int32_t        mem_alignment_mask;

	/*
	 * When we enter this routine, we know
	 *  -- the io_req_size will not exceed iov_len
	 *  -- the target address is physically contiguous
	 */
	cluster_syncup(vp, newEOF, callback, callback_arg, callback ? PUSH_SYNC : 0);

	devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
	mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;

	iostate.io_completed = 0;
	iostate.io_issued = 0;
	iostate.io_error = 0;
	iostate.io_wanted = 0;

	lck_mtx_init(&iostate.io_mtxp, &cl_mtx_grp, LCK_ATTR_NULL);

next_cwrite:
	io_size = *write_length;

	iov_base = uio_curriovbase(uio);

	upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
	upl_needed_size = upl_offset + io_size;

	pages_in_pl = 0;
	upl_size = (upl_size_t)upl_needed_size;
	upl_flags = UPL_FILE_IO | UPL_COPYOUT_FROM | UPL_NO_SYNC |
	    UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;

	vm_map_t map = UIO_SEG_IS_USER_SPACE(uio->uio_segflg) ? current_map() : kernel_map;
	kret = vm_map_get_upl(map,
#if HAS_MTE || HAS_MTE_EMULATION_SHIMS
	    vm_memtag_canonicalize(map, vm_map_trunc_page(iov_base, vm_map_page_mask(map))),
#else /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */
	    vm_map_trunc_page(iov_base, vm_map_page_mask(map)),
#endif /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */
	    &upl_size, &upl[cur_upl], NULL, &pages_in_pl, &upl_flags, VM_KERN_MEMORY_FILE, 0);

	if (kret != KERN_SUCCESS) {
		/*
		 * failed to get pagelist
		 */
		error = EINVAL;
		goto wait_for_cwrites;
	}
	num_upl++;

	if (!(upl_flags & UPL_PHYS_CONTIG)) {
		/*
		 * The created UPL needs to have the UPL_PHYS_CONTIG flag.
		 */
		error = EINVAL;
		goto wait_for_cwrites;
	}

	/*
	 * Consider the possibility that upl_size wasn't satisfied.
	 */
	if (upl_size < upl_needed_size) {
		/*
		 * This is a failure in the physical memory case.
		 */
		error = EINVAL;
		goto wait_for_cwrites;
	}
	pl = ubc_upl_pageinfo(upl[cur_upl]);

	src_paddr = ((addr64_t)upl_phys_page(pl, 0) << PAGE_SHIFT) + (addr64_t)upl_offset;

	while (((uio->uio_offset & (devblocksize - 1)) || io_size < devblocksize) && io_size) {
		u_int32_t   head_size;

		head_size = devblocksize - (u_int32_t)(uio->uio_offset & (devblocksize - 1));

		if (head_size > io_size) {
			head_size = io_size;
		}

		error = cluster_align_phys_io(vp, uio, src_paddr, head_size, 0, callback, callback_arg);

		if (error) {
			goto wait_for_cwrites;
		}

		upl_offset += head_size;
		src_paddr  += head_size;
		io_size    -= head_size;

		iov_base   += head_size;
	}
	if ((u_int32_t)iov_base & mem_alignment_mask) {
		/*
		 * request doesn't set up on a memory boundary
		 * the underlying DMA engine can handle...
		 * return an error instead of going through
		 * the slow copy path since the intent of this
		 * path is direct I/O from device memory
		 */
		error = EINVAL;
		goto wait_for_cwrites;
	}

	tail_size = io_size & (devblocksize - 1);
	io_size  -= tail_size;

	while (io_size && error == 0) {
		if (io_size > MAX_IO_CONTIG_SIZE) {
			xsize = MAX_IO_CONTIG_SIZE;
		} else {
			xsize = io_size;
		}
		/*
		 * request asynchronously so that we can overlap
		 * the preparation of the next I/O... we'll do
		 * the commit after all the I/O has completed
		 * since its all issued against the same UPL
		 * if there are already too many outstanding writes
		 * wait until some have completed before issuing the next
		 */
		cluster_iostate_wait(&iostate, MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2), "cluster_write_contig");

		if (iostate.io_error) {
			/*
			 * one of the earlier writes we issued ran into a hard error
			 * don't issue any more writes...
			 * go wait for all writes that are part of this stream
			 * to complete before returning the error to the caller
			 */
			goto wait_for_cwrites;
		}
		/*
		 * issue an asynchronous write to cluster_io
		 */
		error = cluster_io(vp, upl[cur_upl], upl_offset, uio->uio_offset,
		    xsize, CL_DEV_MEMORY | CL_ASYNC | bflag, (buf_t)NULL, (struct clios *)&iostate, callback, callback_arg);

		if (error == 0) {
			/*
			 * The cluster_io write completed successfully,
			 * update the uio structure
			 */
			uio_update(uio, (user_size_t)xsize);

			upl_offset += xsize;
			src_paddr  += xsize;
			io_size    -= xsize;
		}
	}
	if (error == 0 && iostate.io_error == 0 && tail_size == 0 && num_upl < MAX_VECTS) {
		error = cluster_io_type(uio, write_type, write_length, 0);

		if (error == 0 && *write_type == IO_CONTIG) {
			cur_upl++;
			goto next_cwrite;
		}
	} else {
		*write_type = IO_UNKNOWN;
	}

wait_for_cwrites:
	/*
	 * make sure all async writes that are part of this stream
	 * have completed before we proceed
	 */
	cluster_iostate_wait(&iostate, 0, "cluster_write_contig");

	if (iostate.io_error) {
		error = iostate.io_error;
	}

	lck_mtx_destroy(&iostate.io_mtxp, &cl_mtx_grp);

	if (error == 0 && tail_size) {
		error = cluster_align_phys_io(vp, uio, src_paddr, tail_size, 0, callback, callback_arg);
	}

	for (n = 0; n < num_upl; n++) {
		/*
		 * just release our hold on each physically contiguous
		 * region without changing any state
		 */
		ubc_upl_abort(upl[n], 0);
	}

	return error;
}


/*
 * need to avoid a race between an msync of a range of pages dirtied via mmap
 * vs a filesystem such as HFS deciding to write a 'hole' to disk via cluster_write's
 * zerofill mechanism before it has seen the VNOP_PAGEOUTs for the pages being msync'd
 *
 * we should never force-zero-fill pages that are already valid in the cache...
 * the entire page contains valid data (either from disk, zero-filled or dirtied
 * via an mmap) so we can only do damage by trying to zero-fill
 *
 */
static int
cluster_zero_range(upl_t upl, upl_page_info_t *pl, int flags, int io_offset, off_t zero_off, off_t upl_f_offset, int bytes_to_zero)
{
	int zero_pg_index;
	boolean_t need_cluster_zero = TRUE;

	if ((flags & (IO_NOZEROVALID | IO_NOZERODIRTY))) {
		bytes_to_zero = min(bytes_to_zero, PAGE_SIZE - (int)(zero_off & PAGE_MASK_64));
		zero_pg_index = (int)((zero_off - upl_f_offset) / PAGE_SIZE_64);

		if (upl_valid_page(pl, zero_pg_index)) {
			/*
			 * never force zero valid pages - dirty or clean
			 * we'll leave these in the UPL for cluster_write_copy to deal with
			 */
			need_cluster_zero = FALSE;
		}
	}
	if (need_cluster_zero == TRUE) {
		cluster_zero(upl, io_offset, bytes_to_zero, NULL);
	}

	return bytes_to_zero;
}


void
cluster_update_state(vnode_t vp, vm_object_offset_t s_offset, vm_object_offset_t e_offset, boolean_t vm_initiated)
{
	struct cl_extent cl;
	boolean_t first_pass = TRUE;

	assert(s_offset < e_offset);
	assert((s_offset & PAGE_MASK_64) == 0);
	assert((e_offset & PAGE_MASK_64) == 0);

	cl.b_addr = (daddr64_t)(s_offset / PAGE_SIZE_64);
	cl.e_addr = (daddr64_t)(e_offset / PAGE_SIZE_64);

	cluster_update_state_internal(vp, &cl, 0, TRUE, &first_pass, s_offset, (int)(e_offset - s_offset),
	    vp->v_un.vu_ubcinfo->ui_size, NULL, NULL, vm_initiated);
}


static void
cluster_update_state_internal(vnode_t vp, struct cl_extent *cl, int flags, boolean_t defer_writes,
    boolean_t *first_pass, off_t write_off, int write_cnt, off_t newEOF,
    int (*callback)(buf_t, void *), void *callback_arg, boolean_t vm_initiated)
{
	struct cl_writebehind *wbp;
	int     cl_index;
	int     ret_cluster_try_push;
	u_int   max_cluster_pgcount;


	max_cluster_pgcount = MAX_CLUSTER_SIZE(vp) / PAGE_SIZE;

	/*
	 * take the lock to protect our accesses
	 * of the writebehind and sparse cluster state
	 */
	wbp = cluster_get_wbp(vp, CLW_ALLOCATE | CLW_RETURNLOCKED);

	if (wbp->cl_scmap) {
		if (!(flags & IO_NOCACHE)) {
			/*
			 * we've fallen into the sparse
			 * cluster method of delaying dirty pages
			 */
			sparse_cluster_add(wbp, &(wbp->cl_scmap), vp, cl, newEOF, callback, callback_arg, vm_initiated);

			lck_mtx_unlock(&wbp->cl_lockw);
			return;
		}
		/*
		 * must have done cached writes that fell into
		 * the sparse cluster mechanism... we've switched
		 * to uncached writes on the file, so go ahead
		 * and push whatever's in the sparse map
		 * and switch back to normal clustering
		 */
		wbp->cl_number = 0;

		sparse_cluster_push(wbp, &(wbp->cl_scmap), vp, newEOF, PUSH_ALL, 0, callback, callback_arg, vm_initiated);
		/*
		 * no clusters of either type present at this point
		 * so just go directly to start_new_cluster since
		 * we know we need to delay this I/O since we've
		 * already released the pages back into the cache
		 * to avoid the deadlock with sparse_cluster_push
		 */
		goto start_new_cluster;
	}
	if (*first_pass == TRUE) {
		if (write_off == wbp->cl_last_write) {
			wbp->cl_seq_written += write_cnt;
		} else {
			wbp->cl_seq_written = write_cnt;
		}

		wbp->cl_last_write = write_off + write_cnt;

		*first_pass = FALSE;
	}
	if (wbp->cl_number == 0) {
		/*
		 * no clusters currently present
		 */
		goto start_new_cluster;
	}

	for (cl_index = 0; cl_index < wbp->cl_number; cl_index++) {
		/*
		 * check each cluster that we currently hold
		 * try to merge some or all of this write into
		 * one or more of the existing clusters... if
		 * any portion of the write remains, start a
		 * new cluster
		 */
		if (cl->b_addr >= wbp->cl_clusters[cl_index].b_addr) {
			/*
			 * the current write starts at or after the current cluster
			 */
			if (cl->e_addr <= (wbp->cl_clusters[cl_index].b_addr + max_cluster_pgcount)) {
				/*
				 * we have a write that fits entirely
				 * within the existing cluster limits
				 */
				if (cl->e_addr > wbp->cl_clusters[cl_index].e_addr) {
					/*
					 * update our idea of where the cluster ends
					 */
					wbp->cl_clusters[cl_index].e_addr = cl->e_addr;
				}
				break;
			}
			if (cl->b_addr < (wbp->cl_clusters[cl_index].b_addr + max_cluster_pgcount)) {
				/*
				 * we have a write that starts in the middle of the current cluster
				 * but extends beyond the cluster's limit... we know this because
				 * of the previous checks
				 * we'll extend the current cluster to the max
				 * and update the b_addr for the current write to reflect that
				 * the head of it was absorbed into this cluster...
				 * note that we'll always have a leftover tail in this case since
				 * full absorbtion would have occurred in the clause above
				 */
				wbp->cl_clusters[cl_index].e_addr = wbp->cl_clusters[cl_index].b_addr + max_cluster_pgcount;

				cl->b_addr = wbp->cl_clusters[cl_index].e_addr;
			}
			/*
			 * we come here for the case where the current write starts
			 * beyond the limit of the existing cluster or we have a leftover
			 * tail after a partial absorbtion
			 *
			 * in either case, we'll check the remaining clusters before
			 * starting a new one
			 */
		} else {
			/*
			 * the current write starts in front of the cluster we're currently considering
			 */
			if ((wbp->cl_clusters[cl_index].e_addr - cl->b_addr) <= max_cluster_pgcount) {
				/*
				 * we can just merge the new request into
				 * this cluster and leave it in the cache
				 * since the resulting cluster is still
				 * less than the maximum allowable size
				 */
				wbp->cl_clusters[cl_index].b_addr = cl->b_addr;

				if (cl->e_addr > wbp->cl_clusters[cl_index].e_addr) {
					/*
					 * the current write completely
					 * envelops the existing cluster and since
					 * each write is limited to at most max_cluster_pgcount pages
					 * we can just use the start and last blocknos of the write
					 * to generate the cluster limits
					 */
					wbp->cl_clusters[cl_index].e_addr = cl->e_addr;
				}
				break;
			}
			/*
			 * if we were to combine this write with the current cluster
			 * we would exceed the cluster size limit.... so,
			 * let's see if there's any overlap of the new I/O with
			 * the cluster we're currently considering... in fact, we'll
			 * stretch the cluster out to it's full limit and see if we
			 * get an intersection with the current write
			 *
			 */
			if (cl->e_addr > wbp->cl_clusters[cl_index].e_addr - max_cluster_pgcount) {
				/*
				 * the current write extends into the proposed cluster
				 * clip the length of the current write after first combining it's
				 * tail with the newly shaped cluster
				 */
				wbp->cl_clusters[cl_index].b_addr = wbp->cl_clusters[cl_index].e_addr - max_cluster_pgcount;

				cl->e_addr = wbp->cl_clusters[cl_index].b_addr;
			}
			/*
			 * if we get here, there was no way to merge
			 * any portion of this write with this cluster
			 * or we could only merge part of it which
			 * will leave a tail...
			 * we'll check the remaining clusters before starting a new one
			 */
		}
	}
	if (cl_index < wbp->cl_number) {
		/*
		 * we found an existing cluster(s) that we
		 * could entirely merge this I/O into
		 */
		goto delay_io;
	}

	if (defer_writes == FALSE &&
	    wbp->cl_number == MAX_CLUSTERS &&
	    wbp->cl_seq_written >= (MAX_CLUSTERS * (max_cluster_pgcount * PAGE_SIZE))) {
		uint32_t        n;

		if (vp->v_mount->mnt_minsaturationbytecount) {
			n = vp->v_mount->mnt_minsaturationbytecount / MAX_CLUSTER_SIZE(vp);

			if (n > MAX_CLUSTERS) {
				n = MAX_CLUSTERS;
			}
		} else {
			n = 0;
		}

		if (n == 0) {
			if (disk_conditioner_mount_is_ssd(vp->v_mount)) {
				n = WRITE_BEHIND_SSD;
			} else {
				n = WRITE_BEHIND;
			}
		}
		while (n--) {
			cluster_try_push(wbp, vp, newEOF, 0, 0, callback, callback_arg, NULL, vm_initiated);
		}
	}
	if (wbp->cl_number < MAX_CLUSTERS) {
		/*
		 * we didn't find an existing cluster to
		 * merge into, but there's room to start
		 * a new one
		 */
		goto start_new_cluster;
	}
	/*
	 * no exisitng cluster to merge with and no
	 * room to start a new one... we'll try
	 * pushing one of the existing ones... if none of
	 * them are able to be pushed, we'll switch
	 * to the sparse cluster mechanism
	 * cluster_try_push updates cl_number to the
	 * number of remaining clusters... and
	 * returns the number of currently unused clusters
	 */
	ret_cluster_try_push = 0;

	/*
	 * if writes are not deferred, call cluster push immediately
	 */
	if (defer_writes == FALSE) {
		ret_cluster_try_push = cluster_try_push(wbp, vp, newEOF, (flags & IO_NOCACHE) ? 0 : PUSH_DELAY, 0, callback, callback_arg, NULL, vm_initiated);
	}
	/*
	 * execute following regardless of writes being deferred or not
	 */
	if (ret_cluster_try_push == 0) {
		/*
		 * no more room in the normal cluster mechanism
		 * so let's switch to the more expansive but expensive
		 * sparse mechanism....
		 */
		sparse_cluster_switch(wbp, vp, newEOF, callback, callback_arg, vm_initiated);
		sparse_cluster_add(wbp, &(wbp->cl_scmap), vp, cl, newEOF, callback, callback_arg, vm_initiated);

		lck_mtx_unlock(&wbp->cl_lockw);
		return;
	}
start_new_cluster:
	wbp->cl_clusters[wbp->cl_number].b_addr = cl->b_addr;
	wbp->cl_clusters[wbp->cl_number].e_addr = cl->e_addr;

	wbp->cl_clusters[wbp->cl_number].io_flags = 0;

	if (flags & IO_NOCACHE) {
		wbp->cl_clusters[wbp->cl_number].io_flags |= CLW_IONOCACHE;
	}

	if (flags & IO_PASSIVE) {
		wbp->cl_clusters[wbp->cl_number].io_flags |= CLW_IOPASSIVE;
	}

	wbp->cl_number++;
delay_io:
	lck_mtx_unlock(&wbp->cl_lockw);
	return;
}


static int
cluster_write_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t oldEOF, off_t newEOF, off_t headOff,
    off_t tailOff, int flags, int (*callback)(buf_t, void *), void *callback_arg)
{
	upl_page_info_t *pl;
	upl_t            upl;
	vm_offset_t      upl_offset = 0;
	vm_size_t        upl_size;
	off_t            upl_f_offset;
	int              pages_in_upl;
	int              start_offset;
	int              xfer_resid;
	int              io_size;
	int              io_offset;
	int              bytes_to_zero;
	int              bytes_to_move;
	kern_return_t    kret;
	int              retval = 0;
	int              io_resid;
	long long        total_size;
	long long        zero_cnt;
	off_t            zero_off;
	long long        zero_cnt1;
	off_t            zero_off1;
	off_t            write_off = 0;
	int              write_cnt = 0;
	boolean_t        first_pass = FALSE;
	struct cl_extent cl;
	int              bflag;
	u_int            max_io_size;

	if (uio) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_START,
		    (int)uio->uio_offset, io_req_size, (int)oldEOF, (int)newEOF, 0);

		io_resid = io_req_size;
	} else {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_START,
		    0, 0, (int)oldEOF, (int)newEOF, 0);

		io_resid = 0;
	}
	if (flags & IO_PASSIVE) {
		bflag = CL_PASSIVE;
	} else {
		bflag = 0;
	}
	if (flags & IO_NOCACHE) {
		bflag |= CL_NOCACHE;
	}

	if (flags & IO_SKIP_ENCRYPTION) {
		bflag |= CL_ENCRYPTED;
	}

	zero_cnt  = 0;
	zero_cnt1 = 0;
	zero_off  = 0;
	zero_off1 = 0;

	max_io_size = cluster_max_io_size(vp->v_mount, CL_WRITE);

	if (flags & IO_HEADZEROFILL) {
		/*
		 * some filesystems (HFS is one) don't support unallocated holes within a file...
		 * so we zero fill the intervening space between the old EOF and the offset
		 * where the next chunk of real data begins.... ftruncate will also use this
		 * routine to zero fill to the new EOF when growing a file... in this case, the
		 * uio structure will not be provided
		 */
		if (uio) {
			if (headOff < uio->uio_offset) {
				zero_cnt = uio->uio_offset - headOff;
				zero_off = headOff;
			}
		} else if (headOff < newEOF) {
			zero_cnt = newEOF - headOff;
			zero_off = headOff;
		}
	} else {
		if (uio && uio->uio_offset > oldEOF) {
			zero_off = uio->uio_offset & ~PAGE_MASK_64;

			if (zero_off >= oldEOF) {
				zero_cnt = uio->uio_offset - zero_off;

				flags |= IO_HEADZEROFILL;
			}
		}
	}
	if (flags & IO_TAILZEROFILL) {
		if (uio) {
			zero_off1 = uio->uio_offset + io_req_size;

			if (zero_off1 < tailOff) {
				zero_cnt1 = tailOff - zero_off1;
			}
		}
	} else {
		if (uio && newEOF > oldEOF) {
			zero_off1 = uio->uio_offset + io_req_size;

			if (zero_off1 == newEOF && (zero_off1 & PAGE_MASK_64)) {
				zero_cnt1 = PAGE_SIZE_64 - (zero_off1 & PAGE_MASK_64);

				flags |= IO_TAILZEROFILL;
			}
		}
	}
	if (zero_cnt == 0 && uio == (struct uio *) 0) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_END,
		    retval, 0, 0, 0, 0);
		return 0;
	}
	if (uio) {
		write_off = uio->uio_offset;
		write_cnt = (int)uio_resid(uio);
		/*
		 * delay updating the sequential write info
		 * in the control block until we've obtained
		 * the lock for it
		 */
		first_pass = TRUE;
	}
	while ((total_size = (io_resid + zero_cnt + zero_cnt1)) && retval == 0) {
		/*
		 * for this iteration of the loop, figure out where our starting point is
		 */
		if (zero_cnt) {
			start_offset = (int)(zero_off & PAGE_MASK_64);
			upl_f_offset = zero_off - start_offset;
		} else if (io_resid) {
			start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
			upl_f_offset = uio->uio_offset - start_offset;
		} else {
			start_offset = (int)(zero_off1 & PAGE_MASK_64);
			upl_f_offset = zero_off1 - start_offset;
		}
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 46)) | DBG_FUNC_NONE,
		    (int)zero_off, (int)zero_cnt, (int)zero_off1, (int)zero_cnt1, 0);

		if (total_size > max_io_size) {
			total_size = max_io_size;
		}

		cl.b_addr = (daddr64_t)(upl_f_offset / PAGE_SIZE_64);

		if (uio && ((flags & (IO_SYNC | IO_HEADZEROFILL | IO_TAILZEROFILL)) == 0)) {
			/*
			 * assumption... total_size <= io_resid
			 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
			 */
			if ((start_offset + total_size) > max_io_size) {
				total_size = max_io_size - start_offset;
			}
			xfer_resid = (int)total_size;

			retval = cluster_copy_ubc_data_internal(vp, uio, &xfer_resid, 1, 1);

			if (retval) {
				break;
			}

			io_resid    -= (total_size - xfer_resid);
			total_size   = xfer_resid;
			start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
			upl_f_offset = uio->uio_offset - start_offset;

			if (total_size == 0) {
				if (start_offset) {
					/*
					 * the write did not finish on a page boundary
					 * which will leave upl_f_offset pointing to the
					 * beginning of the last page written instead of
					 * the page beyond it... bump it in this case
					 * so that the cluster code records the last page
					 * written as dirty
					 */
					upl_f_offset += PAGE_SIZE_64;
				}
				upl_size = 0;

				goto check_cluster;
			}
		}
		/*
		 * compute the size of the upl needed to encompass
		 * the requested write... limit each call to cluster_io
		 * to the maximum UPL size... cluster_io will clip if
		 * this exceeds the maximum io_size for the device,
		 * make sure to account for
		 * a starting offset that's not page aligned
		 */
		upl_size = (start_offset + total_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;

		if (upl_size > max_io_size) {
			upl_size = max_io_size;
		}

		pages_in_upl = (int)(upl_size / PAGE_SIZE);
		io_size      = (int)(upl_size - start_offset);

		if ((long long)io_size > total_size) {
			io_size = (int)total_size;
		}

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_START, upl_size, io_size, total_size, 0, 0);


		/*
		 * Gather the pages from the buffer cache.
		 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
		 * that we intend to modify these pages.
		 */
		kret = ubc_create_upl_kernel(vp,
		    upl_f_offset,
		    (int)upl_size,
		    &upl,
		    &pl,
		    UPL_SET_LITE | ((uio != NULL && (uio->uio_flags & UIO_FLAGS_IS_COMPRESSED_FILE)) ? 0 : UPL_WILL_MODIFY),
		    VM_KERN_MEMORY_FILE);
		if (kret != KERN_SUCCESS) {
			panic("cluster_write_copy: failed to get pagelist");
		}

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_END,
		    upl, (int)upl_f_offset, start_offset, 0, 0);

		if (start_offset && upl_f_offset < oldEOF && !upl_valid_page(pl, 0)) {
			int   read_size;

			/*
			 * we're starting in the middle of the first page of the upl
			 * and the page isn't currently valid, so we're going to have
			 * to read it in first... this is a synchronous operation
			 */
			read_size = PAGE_SIZE;

			if ((upl_f_offset + read_size) > oldEOF) {
				read_size = (int)(oldEOF - upl_f_offset);
			}

			retval = cluster_io(vp, upl, 0, upl_f_offset, read_size,
			    CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
			if (retval) {
				/*
				 * we had an error during the read which causes us to abort
				 * the current cluster_write request... before we do, we need
				 * to release the rest of the pages in the upl without modifying
				 * there state and mark the failed page in error
				 */
				ubc_upl_abort_range(upl, 0, PAGE_SIZE, UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);

				if (upl_size > PAGE_SIZE) {
					ubc_upl_abort_range(upl, 0, (upl_size_t)upl_size,
					    UPL_ABORT_FREE_ON_EMPTY);
				}

				KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 45)) | DBG_FUNC_NONE,
				    upl, 0, 0, retval, 0);
				break;
			}
		}
		if ((start_offset == 0 || upl_size > PAGE_SIZE) && ((start_offset + io_size) & PAGE_MASK)) {
			/*
			 * the last offset we're writing to in this upl does not end on a page
			 * boundary... if it's not beyond the old EOF, then we'll also need to
			 * pre-read this page in if it isn't already valid
			 */
			upl_offset = upl_size - PAGE_SIZE;

			if ((upl_f_offset + start_offset + io_size) < oldEOF &&
			    !upl_valid_page(pl, (int)(upl_offset / PAGE_SIZE))) {
				int   read_size;

				read_size = PAGE_SIZE;

				if ((off_t)(upl_f_offset + upl_offset + read_size) > oldEOF) {
					read_size = (int)(oldEOF - (upl_f_offset + upl_offset));
				}

				retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, read_size,
				    CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
				if (retval) {
					/*
					 * we had an error during the read which causes us to abort
					 * the current cluster_write request... before we do, we
					 * need to release the rest of the pages in the upl without
					 * modifying there state and mark the failed page in error
					 */
					ubc_upl_abort_range(upl, (upl_offset_t)upl_offset, PAGE_SIZE, UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);

					if (upl_size > PAGE_SIZE) {
						ubc_upl_abort_range(upl, 0, (upl_size_t)upl_size, UPL_ABORT_FREE_ON_EMPTY);
					}

					KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 45)) | DBG_FUNC_NONE,
					    upl, 0, 0, retval, 0);
					break;
				}
			}
		}
		xfer_resid = io_size;
		io_offset = start_offset;

		while (zero_cnt && xfer_resid) {
			if (zero_cnt < (long long)xfer_resid) {
				bytes_to_zero = (int)zero_cnt;
			} else {
				bytes_to_zero = xfer_resid;
			}

			bytes_to_zero = cluster_zero_range(upl, pl, flags, io_offset, zero_off, upl_f_offset, bytes_to_zero);

			xfer_resid -= bytes_to_zero;
			zero_cnt   -= bytes_to_zero;
			zero_off   += bytes_to_zero;
			io_offset  += bytes_to_zero;
		}
		if (xfer_resid && io_resid) {
			u_int32_t  io_requested;

			bytes_to_move = min(io_resid, xfer_resid);
			io_requested = bytes_to_move;

			retval = cluster_copy_upl_data(uio, upl, io_offset, (int *)&io_requested);

			if (retval) {
				ubc_upl_abort_range(upl, 0, (upl_size_t)upl_size, UPL_ABORT_FREE_ON_EMPTY);

				KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 45)) | DBG_FUNC_NONE,
				    upl, 0, 0, retval, 0);
			} else {
				io_resid   -= bytes_to_move;
				xfer_resid -= bytes_to_move;
				io_offset  += bytes_to_move;
			}
		}
		while (xfer_resid && zero_cnt1 && retval == 0) {
			if (zero_cnt1 < (long long)xfer_resid) {
				bytes_to_zero = (int)zero_cnt1;
			} else {
				bytes_to_zero = xfer_resid;
			}

			bytes_to_zero = cluster_zero_range(upl, pl, flags, io_offset, zero_off1, upl_f_offset, bytes_to_zero);

			xfer_resid -= bytes_to_zero;
			zero_cnt1  -= bytes_to_zero;
			zero_off1  += bytes_to_zero;
			io_offset  += bytes_to_zero;
		}
		if (retval == 0) {
			int do_zeroing = 1;

			io_size += start_offset;

			/* Force more restrictive zeroing behavior only on APFS */
			if ((vnode_tag(vp) == VT_APFS) && (newEOF < oldEOF)) {
				do_zeroing = 0;
			}

			if (do_zeroing && (upl_f_offset + io_size) >= newEOF && (u_int)io_size < upl_size) {
				/*
				 * if we're extending the file with this write
				 * we'll zero fill the rest of the page so that
				 * if the file gets extended again in such a way as to leave a
				 * hole starting at this EOF, we'll have zero's in the correct spot
				 */
				cluster_zero(upl, io_size, (int)(upl_size - io_size), NULL);
			}
			/*
			 * release the upl now if we hold one since...
			 * 1) pages in it may be present in the sparse cluster map
			 *    and may span 2 separate buckets there... if they do and
			 *    we happen to have to flush a bucket to make room and it intersects
			 *    this upl, a deadlock may result on page BUSY
			 * 2) we're delaying the I/O... from this point forward we're just updating
			 *    the cluster state... no need to hold the pages, so commit them
			 * 3) IO_SYNC is set...
			 *    because we had to ask for a UPL that provides currenty non-present pages, the
			 *    UPL has been automatically set to clear the dirty flags (both software and hardware)
			 *    upon committing it... this is not the behavior we want since it's possible for
			 *    pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
			 *    we'll pick these pages back up later with the correct behavior specified.
			 * 4) we don't want to hold pages busy in a UPL and then block on the cluster lock... if a flush
			 *    of this vnode is in progress, we will deadlock if the pages being flushed intersect the pages
			 *    we hold since the flushing context is holding the cluster lock.
			 */
			ubc_upl_commit_range(upl, 0, (upl_size_t)upl_size,
			    UPL_COMMIT_SET_DIRTY | UPL_COMMIT_INACTIVATE | UPL_COMMIT_FREE_ON_EMPTY);
check_cluster:
			/*
			 * calculate the last logical block number
			 * that this delayed I/O encompassed
			 */
			cl.e_addr = (daddr64_t)((upl_f_offset + (off_t)upl_size) / PAGE_SIZE_64);

			if (flags & IO_SYNC) {
				/*
				 * if the IO_SYNC flag is set than we need to bypass
				 * any clustering and immediately issue the I/O
				 *
				 * we don't hold the lock at this point
				 *
				 * we've already dropped the current upl, so pick it back up with COPYOUT_FROM set
				 * so that we correctly deal with a change in state of the hardware modify bit...
				 * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
				 * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
				 * responsible for generating the correct sized I/O(s)
				 */
				retval = cluster_push_now(vp, &cl, newEOF, flags, callback, callback_arg, FALSE);
			} else {
				boolean_t defer_writes = FALSE;

				if (vfs_flags(vp->v_mount) & MNT_DEFWRITE) {
					defer_writes = TRUE;
				}

				cluster_update_state_internal(vp, &cl, flags, defer_writes, &first_pass,
				    write_off, write_cnt, newEOF, callback, callback_arg, FALSE);
			}
		}
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_END, retval, 0, io_resid, 0, 0);

	return retval;
}



int
cluster_read(vnode_t vp, struct uio *uio, off_t filesize, int xflags)
{
	return cluster_read_ext(vp, uio, filesize, xflags, NULL, NULL);
}


int
cluster_read_ext(vnode_t vp, struct uio *uio, off_t filesize, int xflags, int (*callback)(buf_t, void *), void *callback_arg)
{
	int             retval = 0;
	int             flags;
	user_ssize_t    cur_resid;
	u_int32_t       io_size;
	u_int32_t       read_length = 0;
	int             read_type = IO_COPY;
	bool            check_io_type;

	flags = xflags;

	if (vp->v_flag & VNOCACHE_DATA) {
		flags |= IO_NOCACHE;
	}
	if ((vp->v_flag & VRAOFF) || speculative_reads_disabled) {
		flags |= IO_RAOFF;
	}

	if (flags & IO_SKIP_ENCRYPTION) {
		flags |= IO_ENCRYPTED;
	}

	/*
	 * do a read through the cache if one of the following is true....
	 *   NOCACHE is not true
	 *   the uio request doesn't target USERSPACE (unless IO_NOCACHE_SYSSPACE is also set)
	 * Alternatively, if IO_ENCRYPTED is set, then we want to bypass the cache as well.
	 * Reading encrypted data from a CP filesystem should never result in the data touching
	 * the UBC.
	 *
	 * otherwise, find out if we want the direct or contig variant for
	 * the first vector in the uio request
	 */
	check_io_type = false;
	if (flags & IO_NOCACHE) {
		if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) {
			/*
			 * no-cache to user-space: ok to consider IO_DIRECT.
			 */
			check_io_type = true;
		} else if (uio->uio_segflg == UIO_SYSSPACE &&
		    (flags & IO_NOCACHE_SYSSPACE)) {
			/*
			 * no-cache to kernel-space but w/ IO_NOCACHE_SYSSPACE:
			 * ok to consider IO_DIRECT.
			 * The caller should make sure to target kernel buffer
			 * that is backed by regular anonymous memory (i.e.
			 * not backed by the kernel object or an external
			 * memory manager like device memory or a file).
			 */
			check_io_type = true;
		}
	} else if (flags & IO_ENCRYPTED) {
		check_io_type = true;
	}
	if (check_io_type) {
		retval = cluster_io_type(uio, &read_type, &read_length, 0);
	}

	while ((cur_resid = uio_resid(uio)) && uio->uio_offset < filesize && retval == 0) {
		switch (read_type) {
		case IO_COPY:
			/*
			 * make sure the uio_resid isn't too big...
			 * internally, we want to handle all of the I/O in
			 * chunk sizes that fit in a 32 bit int
			 */
			if (cur_resid > (user_ssize_t)(MAX_IO_REQUEST_SIZE)) {
				io_size = MAX_IO_REQUEST_SIZE;
			} else {
				io_size = (u_int32_t)cur_resid;
			}

			retval = cluster_read_copy(vp, uio, io_size, filesize, flags, callback, callback_arg);
			break;

		case IO_DIRECT:
			retval = cluster_read_direct(vp, uio, filesize, &read_type, &read_length, flags, callback, callback_arg);
			break;

		case IO_CONTIG:
			retval = cluster_read_contig(vp, uio, filesize, &read_type, &read_length, callback, callback_arg, flags);
			break;

		case IO_UNKNOWN:
			retval = cluster_io_type(uio, &read_type, &read_length, 0);
			break;
		}
	}
	return retval;
}



static void
cluster_read_upl_release(upl_t upl, int start_pg, int last_pg, int take_reference)
{
	int range;
	int abort_flags = UPL_ABORT_FREE_ON_EMPTY;

	if ((range = last_pg - start_pg)) {
		if (take_reference) {
			abort_flags |= UPL_ABORT_REFERENCE;
		}

		ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, range * PAGE_SIZE, abort_flags);
	}
}


static int
cluster_read_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
{
	upl_page_info_t *pl;
	upl_t            upl = NULL;
	vm_offset_t      upl_offset;
	u_int32_t        upl_size;
	off_t            upl_f_offset;
	int              start_offset;
	int              start_pg;
	int              last_pg;
	int              uio_last = 0;
	int              pages_in_upl;
	off_t            max_size;
	off_t            last_ioread_offset;
	off_t            last_request_offset;
	kern_return_t    kret;
	int              error  = 0;
	int              retval = 0;
	u_int32_t        size_of_prefetch;
	u_int32_t        xsize;
	u_int32_t        io_size;
	u_int32_t        max_rd_size;
	u_int32_t        max_io_size;
	u_int32_t        max_prefetch;
	u_int            rd_ahead_enabled = 1;
	u_int            prefetch_enabled = 1;
	struct cl_readahead *   rap;
	struct clios            iostate;
	struct cl_extent        extent;
	int              bflag;
	int              take_reference = 1;
	int              policy = IOPOL_DEFAULT;
	boolean_t        iolock_inited = FALSE;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_START,
	    (int)uio->uio_offset, io_req_size, (int)filesize, flags, 0);

	if (flags & IO_ENCRYPTED) {
		panic("encrypted blocks will hit UBC!");
	}

	policy = throttle_get_io_policy(NULL);

	if (policy == THROTTLE_LEVEL_TIER3 || policy == THROTTLE_LEVEL_TIER2 || (flags & IO_NOCACHE)) {
		take_reference = 0;
	}

	if (flags & IO_PASSIVE) {
		bflag = CL_PASSIVE;
	} else {
		bflag = 0;
	}

	if (flags & IO_NOCACHE) {
		bflag |= CL_NOCACHE;
	}

	if (flags & IO_SKIP_ENCRYPTION) {
		bflag |= CL_ENCRYPTED;
	}

	max_io_size = cluster_max_io_size(vp->v_mount, CL_READ);
	max_prefetch = cluster_max_prefetch(vp, max_io_size, prefetch_max);
	max_rd_size = max_prefetch;

	last_request_offset = uio->uio_offset + io_req_size;

	if (last_request_offset > filesize) {
		last_request_offset = filesize;
	}

	if ((flags & (IO_RAOFF | IO_NOCACHE)) || ((last_request_offset & ~PAGE_MASK_64) == (uio->uio_offset & ~PAGE_MASK_64))) {
		rd_ahead_enabled = 0;
		rap = NULL;
	} else {
		if (cluster_is_throttled(vp)) {
			/*
			 * we're in the throttle window, at the very least
			 * we want to limit the size of the I/O we're about
			 * to issue
			 */
			rd_ahead_enabled = 0;
			prefetch_enabled = 0;

			max_rd_size = calculate_max_throttle_size(vp);
		}
		if ((rap = cluster_get_rap(vp)) == NULL) {
			rd_ahead_enabled = 0;
		} else {
			extent.b_addr = uio->uio_offset / PAGE_SIZE_64;
			extent.e_addr = (last_request_offset - 1) / PAGE_SIZE_64;
		}
	}
	if (rap != NULL && rap->cl_ralen && (rap->cl_lastr == extent.b_addr || (rap->cl_lastr + 1) == extent.b_addr)) {
		/*
		 * determine if we already have a read-ahead in the pipe courtesy of the
		 * last read systemcall that was issued...
		 * if so, pick up it's extent to determine where we should start
		 * with respect to any read-ahead that might be necessary to
		 * garner all the data needed to complete this read systemcall
		 */
		last_ioread_offset = (rap->cl_maxra * PAGE_SIZE_64) + PAGE_SIZE_64;

		if (last_ioread_offset < uio->uio_offset) {
			last_ioread_offset = (off_t)0;
		} else if (last_ioread_offset > last_request_offset) {
			last_ioread_offset = last_request_offset;
		}
	} else {
		last_ioread_offset = (off_t)0;
	}

	while (io_req_size && uio->uio_offset < filesize && retval == 0) {
		max_size = filesize - uio->uio_offset;
		bool leftover_upl_aborted = false;

		if ((off_t)(io_req_size) < max_size) {
			io_size = io_req_size;
		} else {
			io_size = (u_int32_t)max_size;
		}

		if (!(flags & IO_NOCACHE)) {
			while (io_size) {
				u_int32_t io_resid;
				u_int32_t io_requested;

				/*
				 * if we keep finding the pages we need already in the cache, then
				 * don't bother to call cluster_read_prefetch since it costs CPU cycles
				 * to determine that we have all the pages we need... once we miss in
				 * the cache and have issued an I/O, than we'll assume that we're likely
				 * to continue to miss in the cache and it's to our advantage to try and prefetch
				 */
				if (last_request_offset && last_ioread_offset && (size_of_prefetch = (u_int32_t)(last_request_offset - last_ioread_offset))) {
					if ((last_ioread_offset - uio->uio_offset) <= max_rd_size && prefetch_enabled) {
						/*
						 * we've already issued I/O for this request and
						 * there's still work to do and
						 * our prefetch stream is running dry, so issue a
						 * pre-fetch I/O... the I/O latency will overlap
						 * with the copying of the data
						 */
						if (size_of_prefetch > max_rd_size) {
							size_of_prefetch = max_rd_size;
						}

						size_of_prefetch = cluster_read_prefetch(vp, last_ioread_offset, size_of_prefetch, filesize, callback, callback_arg, bflag);

						last_ioread_offset += (off_t)(size_of_prefetch * PAGE_SIZE);

						if (last_ioread_offset > last_request_offset) {
							last_ioread_offset = last_request_offset;
						}
					}
				}
				/*
				 * limit the size of the copy we're about to do so that
				 * we can notice that our I/O pipe is running dry and
				 * get the next I/O issued before it does go dry
				 */
				if (last_ioread_offset && io_size > (max_io_size / 4)) {
					io_resid = (max_io_size / 4);
				} else {
					io_resid = io_size;
				}

				io_requested = io_resid;

				retval = cluster_copy_ubc_data_internal(vp, uio, (int *)&io_resid, 0, take_reference);

				xsize = io_requested - io_resid;

				io_size -= xsize;
				io_req_size -= xsize;

				if (retval || io_resid) {
					/*
					 * if we run into a real error or
					 * a page that is not in the cache
					 * we need to leave streaming mode
					 */
					break;
				}

				if (rd_ahead_enabled && (io_size == 0 || last_ioread_offset == last_request_offset)) {
					/*
					 * we're already finished the I/O for this read request
					 * let's see if we should do a read-ahead
					 */
					cluster_read_ahead(vp, &extent, filesize, rap, callback, callback_arg, bflag);
				}
			}
			if (retval) {
				break;
			}
			if (io_size == 0) {
				if (rap != NULL) {
					if (extent.e_addr < rap->cl_lastr) {
						rap->cl_maxra = 0;
					}
					rap->cl_lastr = extent.e_addr;
				}
				break;
			}
			/*
			 * recompute max_size since cluster_copy_ubc_data_internal
			 * may have advanced uio->uio_offset
			 */
			max_size = filesize - uio->uio_offset;
		}

		iostate.io_completed = 0;
		iostate.io_issued = 0;
		iostate.io_error = 0;
		iostate.io_wanted = 0;

		if ((flags & IO_RETURN_ON_THROTTLE)) {
			if (cluster_is_throttled(vp) == THROTTLE_NOW) {
				if (!cluster_io_present_in_BC(vp, uio->uio_offset)) {
					/*
					 * we're in the throttle window and at least 1 I/O
					 * has already been issued by a throttleable thread
					 * in this window, so return with EAGAIN to indicate
					 * to the FS issuing the cluster_read call that it
					 * should now throttle after dropping any locks
					 */
					throttle_info_update_by_mount(vp->v_mount);

					retval = EAGAIN;
					break;
				}
			}
		}

		/*
		 * compute the size of the upl needed to encompass
		 * the requested read... limit each call to cluster_io
		 * to the maximum UPL size... cluster_io will clip if
		 * this exceeds the maximum io_size for the device,
		 * make sure to account for
		 * a starting offset that's not page aligned
		 */
		start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
		upl_f_offset = uio->uio_offset - (off_t)start_offset;

		if (io_size > max_rd_size) {
			io_size = max_rd_size;
		}

		upl_size = (start_offset + io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;

		if (flags & IO_NOCACHE) {
			if (upl_size > max_io_size) {
				upl_size = max_io_size;
			}
		} else {
			if (upl_size > max_io_size / 4) {
				upl_size = max_io_size / 4;
				upl_size &= ~PAGE_MASK;

				if (upl_size == 0) {
					upl_size = PAGE_SIZE;
				}
			}
		}
		pages_in_upl = upl_size / PAGE_SIZE;

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 33)) | DBG_FUNC_START,
		    upl, (int)upl_f_offset, upl_size, start_offset, 0);

		kret = ubc_create_upl_kernel(vp,
		    upl_f_offset,
		    upl_size,
		    &upl,
		    &pl,
		    UPL_FILE_IO | UPL_SET_LITE,
		    VM_KERN_MEMORY_FILE);
		if (kret != KERN_SUCCESS) {
			panic("cluster_read_copy: failed to get pagelist");
		}

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 33)) | DBG_FUNC_END,
		    upl, (int)upl_f_offset, upl_size, start_offset, 0);

		/*
		 * scan from the beginning of the upl looking for the first
		 * non-valid page.... this will become the first page in
		 * the request we're going to make to 'cluster_io'... if all
		 * of the pages are valid, we won't call through to 'cluster_io'
		 */
		for (start_pg = 0; start_pg < pages_in_upl; start_pg++) {
			if (!upl_valid_page(pl, start_pg)) {
				break;
			}
		}

		/*
		 * scan from the starting invalid page looking for a valid
		 * page before the end of the upl is reached, if we
		 * find one, then it will be the last page of the request to
		 * 'cluster_io'
		 */
		for (last_pg = start_pg; last_pg < pages_in_upl; last_pg++) {
			if (upl_valid_page(pl, last_pg)) {
				break;
			}
		}

		if (start_pg < last_pg) {
			/*
			 * we found a range of 'invalid' pages that must be filled
			 * if the last page in this range is the last page of the file
			 * we may have to clip the size of it to keep from reading past
			 * the end of the last physical block associated with the file
			 */
			if (iolock_inited == FALSE) {
				lck_mtx_init(&iostate.io_mtxp, &cl_mtx_grp, LCK_ATTR_NULL);

				iolock_inited = TRUE;
			}
			upl_offset = start_pg * PAGE_SIZE;
			io_size    = (last_pg - start_pg) * PAGE_SIZE;

			if ((off_t)(upl_f_offset + upl_offset + io_size) > filesize) {
				io_size = (u_int32_t)(filesize - (upl_f_offset + upl_offset));
			}

			/*
			 * Find out if this needs verification, we'll have to manage the UPL
			 * diffrently if so. Note that this call only lets us know if
			 * verification is enabled on this mount point, the actual verification
			 * is performed in the File system.
			 */
			size_t verify_block_size = 0;
			if ((VNOP_VERIFY(vp, start_offset, NULL, 0, &verify_block_size, NULL, VNODE_VERIFY_DEFAULT, NULL, NULL) == 0) /* && verify_block_size */) {
				for (uio_last = last_pg; uio_last < pages_in_upl; uio_last++) {
					if (!upl_valid_page(pl, uio_last)) {
						break;
					}
				}
				if (uio_last < pages_in_upl) {
					/*
					 * there were some invalid pages beyond the valid pages
					 * that we didn't issue an I/O for, just release them
					 * unchanged now, so that any prefetch/readahed can
					 * include them
					 */
					ubc_upl_abort_range(upl, uio_last * PAGE_SIZE,
					    (pages_in_upl - uio_last) * PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
					leftover_upl_aborted = true;
				}
			}

			/*
			 * issue an asynchronous read to cluster_io
			 */

			error = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset,
			    io_size, CL_READ | CL_ASYNC | bflag, (buf_t)NULL, &iostate, callback, callback_arg);

			if (rap) {
				if (extent.e_addr < rap->cl_maxra) {
					/*
					 * we've just issued a read for a block that should have been
					 * in the cache courtesy of the read-ahead engine... something
					 * has gone wrong with the pipeline, so reset the read-ahead
					 * logic which will cause us to restart from scratch
					 */
					rap->cl_maxra = 0;
				}
			}
		}
		if (error == 0) {
			/*
			 * if the read completed successfully, or there was no I/O request
			 * issued, than copy the data into user land via 'cluster_upl_copy_data'
			 * we'll first add on any 'valid'
			 * pages that were present in the upl when we acquired it.
			 */
			u_int  val_size;

			if (!leftover_upl_aborted) {
				for (uio_last = last_pg; uio_last < pages_in_upl; uio_last++) {
					if (!upl_valid_page(pl, uio_last)) {
						break;
					}
				}
				if (uio_last < pages_in_upl) {
					/*
					 * there were some invalid pages beyond the valid pages
					 * that we didn't issue an I/O for, just release them
					 * unchanged now, so that any prefetch/readahed can
					 * include them
					 */
					ubc_upl_abort_range(upl, uio_last * PAGE_SIZE,
					    (pages_in_upl - uio_last) * PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
				}
			}

			/*
			 * compute size to transfer this round,  if io_req_size is
			 * still non-zero after this attempt, we'll loop around and
			 * set up for another I/O.
			 */
			val_size = (uio_last * PAGE_SIZE) - start_offset;

			if (val_size > max_size) {
				val_size = (u_int)max_size;
			}

			if (val_size > io_req_size) {
				val_size = io_req_size;
			}

			if ((uio->uio_offset + val_size) > last_ioread_offset) {
				last_ioread_offset = uio->uio_offset + val_size;
			}

			if ((size_of_prefetch = (u_int32_t)(last_request_offset - last_ioread_offset)) && prefetch_enabled) {
				if ((last_ioread_offset - (uio->uio_offset + val_size)) <= upl_size) {
					/*
					 * if there's still I/O left to do for this request, and...
					 * we're not in hard throttle mode, and...
					 * we're close to using up the previous prefetch, then issue a
					 * new pre-fetch I/O... the I/O latency will overlap
					 * with the copying of the data
					 */
					if (size_of_prefetch > max_rd_size) {
						size_of_prefetch = max_rd_size;
					}

					size_of_prefetch = cluster_read_prefetch(vp, last_ioread_offset, size_of_prefetch, filesize, callback, callback_arg, bflag);

					last_ioread_offset += (off_t)(size_of_prefetch * PAGE_SIZE);

					if (last_ioread_offset > last_request_offset) {
						last_ioread_offset = last_request_offset;
					}
				}
			} else if ((uio->uio_offset + val_size) == last_request_offset) {
				/*
				 * this transfer will finish this request, so...
				 * let's try to read ahead if we're in
				 * a sequential access pattern and we haven't
				 * explicitly disabled it
				 */
				if (rd_ahead_enabled) {
					cluster_read_ahead(vp, &extent, filesize, rap, callback, callback_arg, bflag);
				}

				if (rap != NULL) {
					if (extent.e_addr < rap->cl_lastr) {
						rap->cl_maxra = 0;
					}
					rap->cl_lastr = extent.e_addr;
				}
			}
			if (iolock_inited == TRUE) {
				cluster_iostate_wait(&iostate, 0, "cluster_read_copy");
			}

			if (iostate.io_error) {
				error = iostate.io_error;
			} else {
				u_int32_t io_requested;

				io_requested = val_size;

				retval = cluster_copy_upl_data(uio, upl, start_offset, (int *)&io_requested);

				io_req_size -= (val_size - io_requested);
			}
		} else {
			if (iolock_inited == TRUE) {
				cluster_iostate_wait(&iostate, 0, "cluster_read_copy");
			}
		}
		if (start_pg < last_pg) {
			/*
			 * compute the range of pages that we actually issued an I/O for
			 * and either commit them as valid if the I/O succeeded
			 * or abort them if the I/O failed or we're not supposed to
			 * keep them in the cache
			 */
			io_size = (last_pg - start_pg) * PAGE_SIZE;

			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_START, upl, start_pg * PAGE_SIZE, io_size, error, 0);

			if (error || (flags & IO_NOCACHE)) {
				ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, io_size,
				    UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
			} else {
				int     commit_flags = UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY;

				if (take_reference) {
					commit_flags |= UPL_COMMIT_INACTIVATE;
				} else {
					commit_flags |= UPL_COMMIT_SPECULATE;
				}

				ubc_upl_commit_range(upl, start_pg * PAGE_SIZE, io_size, commit_flags);
			}
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_END, upl, start_pg * PAGE_SIZE, io_size, error, 0);
		}
		if ((last_pg - start_pg) < pages_in_upl) {
			/*
			 * the set of pages that we issued an I/O for did not encompass
			 * the entire upl... so just release these without modifying
			 * their state
			 */
			if (error) {
				if (leftover_upl_aborted) {
					ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, (uio_last - start_pg) * PAGE_SIZE,
					    UPL_ABORT_FREE_ON_EMPTY);
				} else {
					ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
				}
			} else {
				KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_START,
				    upl, -1, pages_in_upl - (last_pg - start_pg), 0, 0);

				/*
				 * handle any valid pages at the beginning of
				 * the upl... release these appropriately
				 */
				cluster_read_upl_release(upl, 0, start_pg, take_reference);

				/*
				 * handle any valid pages immediately after the
				 * pages we issued I/O for... ... release these appropriately
				 */
				cluster_read_upl_release(upl, last_pg, uio_last, take_reference);

				KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_END, upl, -1, -1, 0, 0);
			}
		}
		if (retval == 0) {
			retval = error;
		}

		if (io_req_size) {
			uint32_t max_throttle_size = calculate_max_throttle_size(vp);

			if (cluster_is_throttled(vp)) {
				/*
				 * we're in the throttle window, at the very least
				 * we want to limit the size of the I/O we're about
				 * to issue
				 */
				rd_ahead_enabled = 0;
				prefetch_enabled = 0;
				max_rd_size = max_throttle_size;
			} else {
				if (max_rd_size == max_throttle_size) {
					/*
					 * coming out of throttled state
					 */
					if (policy != THROTTLE_LEVEL_TIER3 && policy != THROTTLE_LEVEL_TIER2) {
						if (rap != NULL) {
							rd_ahead_enabled = 1;
						}
						prefetch_enabled = 1;
					}
					max_rd_size = max_prefetch;
					last_ioread_offset = 0;
				}
			}
		}
	}
	if (iolock_inited == TRUE) {
		/*
		 * cluster_io returned an error after it
		 * had already issued some I/O.  we need
		 * to wait for that I/O to complete before
		 * we can destroy the iostate mutex...
		 * 'retval' already contains the early error
		 * so no need to pick it up from iostate.io_error
		 */
		cluster_iostate_wait(&iostate, 0, "cluster_read_copy");

		lck_mtx_destroy(&iostate.io_mtxp, &cl_mtx_grp);
	}
	if (rap != NULL) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_END,
		    (int)uio->uio_offset, io_req_size, rap->cl_lastr, retval, 0);

		lck_mtx_unlock(&rap->cl_lockr);
	} else {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_END,
		    (int)uio->uio_offset, io_req_size, 0, retval, 0);
	}

	return retval;
}

/*
 * We don't want another read/write lock for every vnode in the system
 * so we keep a hash of them here.  There should never be very many of
 * these around at any point in time.
 */
cl_direct_read_lock_t *
cluster_lock_direct_read(vnode_t vp, lck_rw_type_t type)
{
	struct cl_direct_read_locks *head
	        = &cl_direct_read_locks[(uintptr_t)vp / sizeof(*vp)
	    % CL_DIRECT_READ_LOCK_BUCKETS];

	struct cl_direct_read_lock *lck, *new_lck = NULL;

	for (;;) {
		lck_spin_lock(&cl_direct_read_spin_lock);

		LIST_FOREACH(lck, head, chain) {
			if (lck->vp == vp) {
				++lck->ref_count;
				lck_spin_unlock(&cl_direct_read_spin_lock);
				if (new_lck) {
					// Someone beat us to it, ditch the allocation
					lck_rw_destroy(&new_lck->rw_lock, &cl_mtx_grp);
					kfree_type(cl_direct_read_lock_t, new_lck);
				}
				lck_rw_lock(&lck->rw_lock, type);
				return lck;
			}
		}

		if (new_lck) {
			// Use the lock we allocated
			LIST_INSERT_HEAD(head, new_lck, chain);
			lck_spin_unlock(&cl_direct_read_spin_lock);
			lck_rw_lock(&new_lck->rw_lock, type);
			return new_lck;
		}

		lck_spin_unlock(&cl_direct_read_spin_lock);

		// Allocate a new lock
		new_lck = kalloc_type(cl_direct_read_lock_t, Z_WAITOK);
		lck_rw_init(&new_lck->rw_lock, &cl_mtx_grp, LCK_ATTR_NULL);
		new_lck->vp = vp;
		new_lck->ref_count = 1;

		// Got to go round again
	}
}

void
cluster_unlock_direct_read(cl_direct_read_lock_t *lck)
{
	lck_rw_done(&lck->rw_lock);

	lck_spin_lock(&cl_direct_read_spin_lock);
	if (lck->ref_count == 1) {
		LIST_REMOVE(lck, chain);
		lck_spin_unlock(&cl_direct_read_spin_lock);
		lck_rw_destroy(&lck->rw_lock, &cl_mtx_grp);
		kfree_type(cl_direct_read_lock_t, lck);
	} else {
		--lck->ref_count;
		lck_spin_unlock(&cl_direct_read_spin_lock);
	}
}

static int
cluster_read_direct(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
    int flags, int (*callback)(buf_t, void *), void *callback_arg)
{
	upl_t            upl = NULL;
	upl_page_info_t  *pl;
	off_t            max_io_size;
	size_t           verify_block_size = 0;
	vm_offset_t      upl_offset, vector_upl_offset = 0;
	upl_size_t       upl_size = 0, vector_upl_size = 0;
	vm_size_t        upl_needed_size;
	unsigned int     pages_in_pl;
	upl_control_flags_t upl_flags;
	kern_return_t    kret = KERN_SUCCESS;
	unsigned int     i;
	int              force_data_sync;
	int              retval = 0;
	int              no_zero_fill = 0;
	int              io_flag = 0;
	int              misaligned = 0;
	struct clios     iostate;
	user_addr_t      iov_base;
	u_int32_t        io_req_size;
	u_int32_t        offset_in_file;
	u_int32_t        offset_in_iovbase;
	u_int32_t        io_size;
	u_int32_t        io_min;
	u_int32_t        xsize;
	u_int32_t        devblocksize;
	u_int32_t        mem_alignment_mask;
	u_int32_t        max_upl_size;
	u_int32_t        max_rd_size;
	u_int32_t        max_rd_ahead;
	u_int32_t        max_vector_size;
	boolean_t        io_throttled = FALSE;

	u_int32_t        vector_upl_iosize = 0;
	int              issueVectorUPL = 0, useVectorUPL = (uio->uio_iovcnt > 1);
	off_t            v_upl_uio_offset = 0;
	int              vector_upl_index = 0;
	upl_t            vector_upl = NULL;
	cl_direct_read_lock_t *lock = NULL;
	uint32_t verify_mask = 0;

	assert(vm_map_page_shift(current_map()) >= PAGE_SHIFT);

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_START,
	    (int)uio->uio_offset, (int)filesize, *read_type, *read_length, 0);

	max_upl_size = cluster_max_io_size(vp->v_mount, CL_READ);

	max_rd_size = max_upl_size;

	if (__improbable(os_mul_overflow(max_rd_size, IO_SCALE(vp, 2),
	    &max_rd_ahead) || (max_rd_ahead > overlapping_read_max))) {
		max_rd_ahead = overlapping_read_max;
	}

	io_flag = CL_COMMIT | CL_READ | CL_ASYNC | CL_NOZERO | CL_DIRECT_IO;

	if (flags & IO_PASSIVE) {
		io_flag |= CL_PASSIVE;
	}

	if (flags & IO_ENCRYPTED) {
		io_flag |= CL_RAW_ENCRYPTED;
	}

	if (flags & IO_NOCACHE) {
		io_flag |= CL_NOCACHE;
	}

	if (flags & IO_SKIP_ENCRYPTION) {
		io_flag |= CL_ENCRYPTED;
	}

	iostate.io_completed = 0;
	iostate.io_issued = 0;
	iostate.io_error = 0;
	iostate.io_wanted = 0;

	lck_mtx_init(&iostate.io_mtxp, &cl_mtx_grp, LCK_ATTR_NULL);

	devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
	mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_NONE,
	    (int)devblocksize, (int)mem_alignment_mask, 0, 0, 0);

	if (devblocksize == 1) {
		/*
		 * the AFP client advertises a devblocksize of 1
		 * however, its BLOCKMAP routine maps to physical
		 * blocks that are PAGE_SIZE in size...
		 * therefore we can't ask for I/Os that aren't page aligned
		 * or aren't multiples of PAGE_SIZE in size
		 * by setting devblocksize to PAGE_SIZE, we re-instate
		 * the old behavior we had before the mem_alignment_mask
		 * changes went in...
		 */
		devblocksize = PAGE_SIZE;
	}

	/*
	 * We are going to need this uio for the prefaulting later
	 * especially for the cases where multiple non-contiguous
	 * iovs are passed into this routine.
	 *
	 * Note that we only want to prefault for direct IOs to userspace buffers,
	 * not kernel buffers.
	 */
	uio_t uio_acct = NULL;
	if (uio->uio_segflg != UIO_SYSSPACE) {
		uio_acct = uio_duplicate(uio);
	}

	retval = VNOP_VERIFY(vp, 0, NULL, 0, &verify_block_size, NULL, VNODE_VERIFY_DEFAULT, NULL, NULL);
	if (retval) {
		verify_block_size = 0;
	} else if (verify_block_size) {
		assert((verify_block_size & (verify_block_size - 1)) == 0);
		verify_mask = verify_block_size - 1;
	}

next_dread:
	io_req_size = *read_length;
	iov_base = uio_curriovbase(uio);

	offset_in_file = (u_int32_t)uio->uio_offset & (devblocksize - 1);
	offset_in_iovbase = (u_int32_t)iov_base & mem_alignment_mask;

	if (vm_map_page_mask(current_map()) < PAGE_MASK) {
		/*
		 * XXX TODO4K
		 * Direct I/O might not work as expected from a 16k kernel space
		 * to a 4k user space because each 4k chunk might point to
		 * a different 16k physical page...
		 * Let's go the "misaligned" way.
		 */
		if (!misaligned) {
			DEBUG4K_VFS("forcing misaligned\n");
		}
		misaligned = 1;
	}

	if (offset_in_file || offset_in_iovbase) {
		/*
		 * one of the 2 important offsets is misaligned
		 * so fire an I/O through the cache for this entire vector
		 */
		misaligned = 1;
	}
	if (iov_base & (devblocksize - 1)) {
		/*
		 * the offset in memory must be on a device block boundary
		 * so that we can guarantee that we can generate an
		 * I/O that ends on a page boundary in cluster_io
		 */
		misaligned = 1;
	}

	if (verify_block_size && !misaligned && ((uio_offset(uio) & verify_mask) || (uio_resid(uio) & verify_mask))) {
		/*
		 * If the offset is not aligned to the verification block size
		 * or the size is not aligned to the verification block size,
		 * we simply send this through the cached i/o path as that is
		 * what the Filesystem will end up doing anyway i.e. it will
		 * read all the remaining data in order to verify it and then
		 * discard the data it has read.
		 */
		misaligned = 1;
	}

	max_io_size = filesize - uio->uio_offset;

	/*
	 * The user must request IO in aligned chunks.  If the
	 * offset into the file is bad, or the userland pointer
	 * is non-aligned, then we cannot service the encrypted IO request.
	 */
	if (flags & IO_ENCRYPTED) {
		if (misaligned || (io_req_size & (devblocksize - 1))) {
			retval = EINVAL;
		}

		max_io_size = roundup(max_io_size, devblocksize);
	}

	if ((off_t)io_req_size > max_io_size) {
		io_req_size = (u_int32_t)max_io_size;
	}

	/*
	 * When we get to this point, we know...
	 *  -- the offset into the file is on a devblocksize boundary
	 */

	while (io_req_size && retval == 0) {
		u_int32_t io_start;

		if (cluster_is_throttled(vp)) {
			uint32_t max_throttle_size = calculate_max_throttle_size(vp);

			/*
			 * we're in the throttle window, at the very least
			 * we want to limit the size of the I/O we're about
			 * to issue
			 */
			max_rd_size  = max_throttle_size;
			max_rd_ahead = max_throttle_size - 1;
			max_vector_size = max_throttle_size;
		} else {
			max_rd_size  = max_upl_size;
			max_rd_ahead = max_rd_size * IO_SCALE(vp, 2);
			max_vector_size = MAX_VECTOR_UPL_SIZE;
		}
		io_start = io_size = io_req_size;

		/*
		 * First look for pages already in the cache
		 * and move them to user space.  But only do this
		 * check if we are not retrieving encrypted data directly
		 * from the filesystem;  those blocks should never
		 * be in the UBC.
		 *
		 * cluster_copy_ubc_data returns the resid
		 * in io_size
		 */
		if ((flags & IO_ENCRYPTED) == 0) {
			retval = cluster_copy_ubc_data_internal(vp, uio, (int *)&io_size, 0, 0);
		}
		/*
		 * calculate the number of bytes actually copied
		 * starting size - residual
		 */
		xsize = io_start - io_size;

		io_req_size -= xsize;

		if (useVectorUPL && (xsize || (iov_base & PAGE_MASK))) {
			/*
			 * We found something in the cache or we have an iov_base that's not
			 * page-aligned.
			 *
			 * Issue all I/O's that have been collected within this Vectored UPL.
			 */
			if (vector_upl_index) {
				retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
				reset_vector_run_state();
			}

			if (xsize) {
				useVectorUPL = 0;
			}

			/*
			 * After this point, if we are using the Vector UPL path and the base is
			 * not page-aligned then the UPL with that base will be the first in the vector UPL.
			 */
		}

		/*
		 * check to see if we are finished with this request.
		 *
		 * If we satisfied this IO already, then io_req_size will be 0.
		 * Otherwise, see if the IO was mis-aligned and needs to go through
		 * the UBC to deal with the 'tail'.
		 *
		 */
		if (io_req_size == 0 || (misaligned)) {
			/*
			 * see if there's another uio vector to
			 * process that's of type IO_DIRECT
			 *
			 * break out of while loop to get there
			 */
			break;
		}
		/*
		 * assume the request ends on a device block boundary
		 */
		io_min = devblocksize;

		/*
		 * we can handle I/O's in multiples of the device block size
		 * however, if io_size isn't a multiple of devblocksize we
		 * want to clip it back to the nearest page boundary since
		 * we are going to have to go through cluster_read_copy to
		 * deal with the 'overhang'... by clipping it to a PAGE_SIZE
		 * multiple, we avoid asking the drive for the same physical
		 * blocks twice.. once for the partial page at the end of the
		 * request and a 2nd time for the page we read into the cache
		 * (which overlaps the end of the direct read) in order to
		 * get at the overhang bytes
		 */
		if (io_size & (devblocksize - 1)) {
			assert(!(flags & IO_ENCRYPTED));
			/*
			 * Clip the request to the previous page size boundary
			 * since request does NOT end on a device block boundary
			 */
			io_size &= ~PAGE_MASK;
			io_min = PAGE_SIZE;
		}
		if (retval || io_size < io_min) {
			/*
			 * either an error or we only have the tail left to
			 * complete via the copy path...
			 * we may have already spun some portion of this request
			 * off as async requests... we need to wait for the I/O
			 * to complete before returning
			 */
			goto wait_for_dreads;
		}

		/*
		 * Don't re-check the UBC data if we are looking for uncached IO
		 * or asking for encrypted blocks.
		 */
		if ((flags & IO_ENCRYPTED) == 0) {
			if ((xsize = io_size) > max_rd_size) {
				xsize = max_rd_size;
			}

			io_size = 0;

			if (!lock) {
				/*
				 * We hold a lock here between the time we check the
				 * cache and the time we issue I/O.  This saves us
				 * from having to lock the pages in the cache.  Not
				 * all clients will care about this lock but some
				 * clients may want to guarantee stability between
				 * here and when the I/O is issued in which case they
				 * will take the lock exclusively.
				 */
				lock = cluster_lock_direct_read(vp, LCK_RW_TYPE_SHARED);
			}

			ubc_range_op(vp, uio->uio_offset, uio->uio_offset + xsize, UPL_ROP_ABSENT, (int *)&io_size);

			if (io_size == 0) {
				/*
				 * a page must have just come into the cache
				 * since the first page in this range is no
				 * longer absent, go back and re-evaluate
				 */
				continue;
			}
		}
		if ((flags & IO_RETURN_ON_THROTTLE)) {
			if (cluster_is_throttled(vp) == THROTTLE_NOW) {
				if (!cluster_io_present_in_BC(vp, uio->uio_offset)) {
					/*
					 * we're in the throttle window and at least 1 I/O
					 * has already been issued by a throttleable thread
					 * in this window, so return with EAGAIN to indicate
					 * to the FS issuing the cluster_read call that it
					 * should now throttle after dropping any locks
					 */
					throttle_info_update_by_mount(vp->v_mount);

					io_throttled = TRUE;
					goto wait_for_dreads;
				}
			}
		}
		if (io_size > max_rd_size) {
			io_size = max_rd_size;
		}

		iov_base = uio_curriovbase(uio);

		upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
		upl_needed_size = (upl_offset + io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_START,
		    (int)upl_offset, upl_needed_size, (int)iov_base, io_size, 0);

		if (upl_offset == 0 && ((io_size & PAGE_MASK) == 0)) {
			no_zero_fill = 1;
		} else {
			no_zero_fill = 0;
		}

		vm_map_t map = UIO_SEG_IS_USER_SPACE(uio->uio_segflg) ? current_map() : kernel_map;
		for (force_data_sync = 0; force_data_sync < 3; force_data_sync++) {
			pages_in_pl = 0;
			upl_size = (upl_size_t)upl_needed_size;
			upl_flags = UPL_FILE_IO | UPL_NO_SYNC | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
			if (no_zero_fill) {
				upl_flags |= UPL_NOZEROFILL;
			}
			if (force_data_sync) {
				upl_flags |= UPL_FORCE_DATA_SYNC;
			}

			kret = vm_map_create_upl(map,
			    (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
			    &upl_size, &upl, NULL, &pages_in_pl, &upl_flags, VM_KERN_MEMORY_FILE);

			if (kret != KERN_SUCCESS) {
				KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
				    (int)upl_offset, upl_size, io_size, kret, 0);
				/*
				 * failed to get pagelist
				 *
				 * we may have already spun some portion of this request
				 * off as async requests... we need to wait for the I/O
				 * to complete before returning
				 */
				goto wait_for_dreads;
			}
			pages_in_pl = upl_size / PAGE_SIZE;
			pl = UPL_GET_INTERNAL_PAGE_LIST(upl);

			for (i = 0; i < pages_in_pl; i++) {
				if (!upl_page_present(pl, i)) {
					break;
				}
			}
			if (i == pages_in_pl) {
				break;
			}

			ubc_upl_abort(upl, 0);
		}
		if (force_data_sync >= 3) {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
			    (int)upl_offset, upl_size, io_size, kret, 0);

			goto wait_for_dreads;
		}
		/*
		 * Consider the possibility that upl_size wasn't satisfied.
		 */
		if (upl_size < upl_needed_size) {
			if (upl_size && upl_offset == 0) {
				io_size = upl_size;
			} else {
				io_size = 0;
			}
		}
		if (io_size == 0) {
			ubc_upl_abort(upl, 0);
			goto wait_for_dreads;
		}
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
		    (int)upl_offset, upl_size, io_size, kret, 0);

		if (useVectorUPL) {
			vm_offset_t end_off = ((iov_base + io_size) & PAGE_MASK);
			if (end_off) {
				issueVectorUPL = 1;
			}
			/*
			 * After this point, if we are using a vector UPL, then
			 * either all the UPL elements end on a page boundary OR
			 * this UPL is the last element because it does not end
			 * on a page boundary.
			 */
		}

		/*
		 * request asynchronously so that we can overlap
		 * the preparation of the next I/O
		 * if there are already too many outstanding reads
		 * wait until some have completed before issuing the next read
		 */
		cluster_iostate_wait(&iostate, max_rd_ahead, "cluster_read_direct");

		if (iostate.io_error) {
			/*
			 * one of the earlier reads we issued ran into a hard error
			 * don't issue any more reads, cleanup the UPL
			 * that was just created but not used, then
			 * go wait for any other reads to complete before
			 * returning the error to the caller
			 */
			ubc_upl_abort(upl, 0);

			goto wait_for_dreads;
		}
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 73)) | DBG_FUNC_START,
		    upl, (int)upl_offset, (int)uio->uio_offset, io_size, 0);

		if (!useVectorUPL) {
			if (no_zero_fill) {
				io_flag &= ~CL_PRESERVE;
			} else {
				io_flag |= CL_PRESERVE;
			}

			retval = cluster_io(vp, upl, upl_offset, uio->uio_offset, io_size, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
		} else {
			if (!vector_upl_index) {
				vector_upl = vector_upl_create(upl_offset, uio->uio_iovcnt);
				v_upl_uio_offset = uio->uio_offset;
				vector_upl_offset = upl_offset;
			}

			vector_upl_set_subupl(vector_upl, upl, upl_size);
			vector_upl_set_iostate(vector_upl, upl, vector_upl_size, upl_size);
			vector_upl_index++;
			vector_upl_size += upl_size;
			vector_upl_iosize += io_size;

			if (issueVectorUPL || vector_upl_index == vector_upl_max_upls(vector_upl) || vector_upl_size >= max_vector_size) {
				retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
				reset_vector_run_state();
			}
		}

		if (lock) {
			// We don't need to wait for the I/O to complete
			cluster_unlock_direct_read(lock);
			lock = NULL;
		}

		/*
		 * update the uio structure
		 */
		if ((flags & IO_ENCRYPTED) && (max_io_size < io_size)) {
			uio_update(uio, (user_size_t)max_io_size);
		} else {
			uio_update(uio, (user_size_t)io_size);
		}

		io_req_size -= io_size;

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 73)) | DBG_FUNC_END,
		    upl, (int)uio->uio_offset, io_req_size, retval, 0);
	} /* end while */

	if (retval == 0 && iostate.io_error == 0 && io_req_size == 0 && uio->uio_offset < filesize) {
		retval = cluster_io_type(uio, read_type, read_length, 0);

		if (retval == 0 && *read_type == IO_DIRECT) {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_NONE,
			    (int)uio->uio_offset, (int)filesize, *read_type, *read_length, 0);

			goto next_dread;
		}
	}

wait_for_dreads:

	if (retval == 0 && iostate.io_error == 0 && useVectorUPL && vector_upl_index) {
		retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
		reset_vector_run_state();
	}

	// We don't need to wait for the I/O to complete
	if (lock) {
		cluster_unlock_direct_read(lock);
	}

	/*
	 * make sure all async reads that are part of this stream
	 * have completed before we return
	 */
	cluster_iostate_wait(&iostate, 0, "cluster_read_direct");

	if (iostate.io_error) {
		retval = iostate.io_error;
	}

	lck_mtx_destroy(&iostate.io_mtxp, &cl_mtx_grp);

	if (io_throttled == TRUE && retval == 0) {
		retval = EAGAIN;
	}

	vm_map_offset_t current_page_size, current_page_mask;
	current_page_size = vm_map_page_size(current_map());
	current_page_mask = vm_map_page_mask(current_map());
	if (uio_acct) {
		assert(uio_acct->uio_segflg != UIO_SYSSPACE);
		off_t bytes_to_prefault = 0, bytes_prefaulted = 0;
		user_addr_t curr_iov_base = 0;
		user_addr_t curr_iov_end = 0;
		user_size_t curr_iov_len = 0;

		bytes_to_prefault = uio_offset(uio) - uio_offset(uio_acct);

		for (; bytes_prefaulted < bytes_to_prefault;) {
			curr_iov_base = uio_curriovbase(uio_acct);
			curr_iov_len = MIN(uio_curriovlen(uio_acct), bytes_to_prefault - bytes_prefaulted);
			curr_iov_end = curr_iov_base + curr_iov_len;

			for (; curr_iov_base < curr_iov_end;) {
				/*
				 * This is specifically done for pmap accounting purposes.
				 * vm_pre_fault() will call vm_fault() to enter the page into
				 * the pmap if there isn't _a_ physical page for that VA already.
				 */
				vm_pre_fault(vm_map_trunc_page(curr_iov_base, current_page_mask), VM_PROT_READ);
				curr_iov_base += current_page_size;
				bytes_prefaulted += current_page_size;
			}
			/*
			 * Use update instead of advance so we can see how many iovs we processed.
			 */
			uio_update(uio_acct, curr_iov_len);
		}
		uio_free(uio_acct);
		uio_acct = NULL;
	}

	if (io_req_size && retval == 0) {
		/*
		 * we couldn't handle the tail of this request in DIRECT mode
		 * so fire it through the copy path
		 */
		if (flags & IO_ENCRYPTED) {
			/*
			 * We cannot fall back to the copy path for encrypted I/O. If this
			 * happens, there is something wrong with the user buffer passed
			 * down.
			 */
			retval = EFAULT;
		} else {
			retval = cluster_read_copy(vp, uio, io_req_size, filesize, flags, callback, callback_arg);
		}

		*read_type = IO_UNKNOWN;
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_END,
	    (int)uio->uio_offset, (int)uio_resid(uio), io_req_size, retval, 0);

	return retval;
}


static int
cluster_read_contig(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
    int (*callback)(buf_t, void *), void *callback_arg, int flags)
{
	upl_page_info_t *pl;
	upl_t            upl[MAX_VECTS];
	vm_offset_t      upl_offset;
	addr64_t         dst_paddr = 0;
	user_addr_t      iov_base;
	off_t            max_size;
	upl_size_t       upl_size;
	vm_size_t        upl_needed_size;
	mach_msg_type_number_t  pages_in_pl;
	upl_control_flags_t upl_flags;
	kern_return_t    kret;
	struct clios     iostate;
	int              error = 0;
	int              cur_upl = 0;
	int              num_upl = 0;
	int              n;
	u_int32_t        xsize;
	u_int32_t        io_size;
	u_int32_t        devblocksize;
	u_int32_t        mem_alignment_mask;
	u_int32_t        tail_size = 0;
	int              bflag;

	if (flags & IO_PASSIVE) {
		bflag = CL_PASSIVE;
	} else {
		bflag = 0;
	}

	if (flags & IO_NOCACHE) {
		bflag |= CL_NOCACHE;
	}

	/*
	 * When we enter this routine, we know
	 *  -- the read_length will not exceed the current iov_len
	 *  -- the target address is physically contiguous for read_length
	 */
	cluster_syncup(vp, filesize, callback, callback_arg, PUSH_SYNC);

	devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
	mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;

	iostate.io_completed = 0;
	iostate.io_issued = 0;
	iostate.io_error = 0;
	iostate.io_wanted = 0;

	lck_mtx_init(&iostate.io_mtxp, &cl_mtx_grp, LCK_ATTR_NULL);

next_cread:
	io_size = *read_length;

	max_size = filesize - uio->uio_offset;

	if (io_size > max_size) {
		io_size = (u_int32_t)max_size;
	}

	iov_base = uio_curriovbase(uio);

	upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
	upl_needed_size = upl_offset + io_size;

	pages_in_pl = 0;
	upl_size = (upl_size_t)upl_needed_size;
	upl_flags = UPL_FILE_IO | UPL_NO_SYNC | UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;


	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 92)) | DBG_FUNC_START,
	    (int)upl_offset, (int)upl_size, (int)iov_base, io_size, 0);

	vm_map_t map = UIO_SEG_IS_USER_SPACE(uio->uio_segflg) ? current_map() : kernel_map;
	kret = vm_map_get_upl(map,
#if HAS_MTE || HAS_MTE_EMULATION_SHIMS
	    vm_memtag_canonicalize(map, vm_map_trunc_page(iov_base, vm_map_page_mask(map))),
#else /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */
	    vm_map_trunc_page(iov_base, vm_map_page_mask(map)),
#endif /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */
	    &upl_size, &upl[cur_upl], NULL, &pages_in_pl, &upl_flags, VM_KERN_MEMORY_FILE, 0);

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 92)) | DBG_FUNC_END,
	    (int)upl_offset, upl_size, io_size, kret, 0);

	if (kret != KERN_SUCCESS) {
		/*
		 * failed to get pagelist
		 */
		error = EINVAL;
		goto wait_for_creads;
	}
	num_upl++;

	if (!(upl_flags & UPL_PHYS_CONTIG)) {
		/*
		 * The created UPL needs to have the UPL_PHYS_CONTIG flag.
		 */
		error = EINVAL;
		goto wait_for_creads;
	}

	if (upl_size < upl_needed_size) {
		/*
		 * The upl_size wasn't satisfied.
		 */
		error = EINVAL;
		goto wait_for_creads;
	}
	pl = ubc_upl_pageinfo(upl[cur_upl]);

	dst_paddr = ((addr64_t)upl_phys_page(pl, 0) << PAGE_SHIFT) + (addr64_t)upl_offset;

	while (((uio->uio_offset & (devblocksize - 1)) || io_size < devblocksize) && io_size) {
		u_int32_t   head_size;

		head_size = devblocksize - (u_int32_t)(uio->uio_offset & (devblocksize - 1));

		if (head_size > io_size) {
			head_size = io_size;
		}

		error = cluster_align_phys_io(vp, uio, dst_paddr, head_size, CL_READ, callback, callback_arg);

		if (error) {
			goto wait_for_creads;
		}

		upl_offset += head_size;
		dst_paddr  += head_size;
		io_size    -= head_size;

		iov_base   += head_size;
	}
	if ((u_int32_t)iov_base & mem_alignment_mask) {
		/*
		 * request doesn't set up on a memory boundary
		 * the underlying DMA engine can handle...
		 * return an error instead of going through
		 * the slow copy path since the intent of this
		 * path is direct I/O to device memory
		 */
		error = EINVAL;
		goto wait_for_creads;
	}

	tail_size = io_size & (devblocksize - 1);

	io_size  -= tail_size;

	while (io_size && error == 0) {
		if (io_size > MAX_IO_CONTIG_SIZE) {
			xsize = MAX_IO_CONTIG_SIZE;
		} else {
			xsize = io_size;
		}
		/*
		 * request asynchronously so that we can overlap
		 * the preparation of the next I/O... we'll do
		 * the commit after all the I/O has completed
		 * since its all issued against the same UPL
		 * if there are already too many outstanding reads
		 * wait until some have completed before issuing the next
		 */
		cluster_iostate_wait(&iostate, MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2), "cluster_read_contig");

		if (iostate.io_error) {
			/*
			 * one of the earlier reads we issued ran into a hard error
			 * don't issue any more reads...
			 * go wait for any other reads to complete before
			 * returning the error to the caller
			 */
			goto wait_for_creads;
		}
		error = cluster_io(vp, upl[cur_upl], upl_offset, uio->uio_offset, xsize,
		    CL_READ | CL_NOZERO | CL_DEV_MEMORY | CL_ASYNC | bflag,
		    (buf_t)NULL, &iostate, callback, callback_arg);
		/*
		 * The cluster_io read was issued successfully,
		 * update the uio structure
		 */
		if (error == 0) {
			uio_update(uio, (user_size_t)xsize);

			dst_paddr  += xsize;
			upl_offset += xsize;
			io_size    -= xsize;
		}
	}
	if (error == 0 && iostate.io_error == 0 && tail_size == 0 && num_upl < MAX_VECTS && uio->uio_offset < filesize) {
		error = cluster_io_type(uio, read_type, read_length, 0);

		if (error == 0 && *read_type == IO_CONTIG) {
			cur_upl++;
			goto next_cread;
		}
	} else {
		*read_type = IO_UNKNOWN;
	}

wait_for_creads:
	/*
	 * make sure all async reads that are part of this stream
	 * have completed before we proceed
	 */
	cluster_iostate_wait(&iostate, 0, "cluster_read_contig");

	if (iostate.io_error) {
		error = iostate.io_error;
	}

	lck_mtx_destroy(&iostate.io_mtxp, &cl_mtx_grp);

	if (error == 0 && tail_size) {
		error = cluster_align_phys_io(vp, uio, dst_paddr, tail_size, CL_READ, callback, callback_arg);
	}

	for (n = 0; n < num_upl; n++) {
		/*
		 * just release our hold on each physically contiguous
		 * region without changing any state
		 */
		ubc_upl_abort(upl[n], 0);
	}

	return error;
}


static int
cluster_io_type(struct uio *uio, int *io_type, u_int32_t *io_length, u_int32_t min_length)
{
	user_size_t      iov_len;
	user_addr_t      iov_base = 0;
	upl_t            upl;
	upl_size_t       upl_size;
	upl_control_flags_t upl_flags;
	int              retval = 0;

	/*
	 * skip over any emtpy vectors
	 */
	uio_update(uio, (user_size_t)0);

	iov_len = MIN(uio_curriovlen(uio), uio_resid(uio));

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 94)) | DBG_FUNC_START, uio, (int)iov_len, 0, 0, 0);

	if (iov_len) {
		iov_base = uio_curriovbase(uio);
		/*
		 * make sure the size of the vector isn't too big...
		 * internally, we want to handle all of the I/O in
		 * chunk sizes that fit in a 32 bit int
		 */
		if (iov_len > (user_size_t)MAX_IO_REQUEST_SIZE) {
			upl_size = MAX_IO_REQUEST_SIZE;
		} else {
			upl_size = (u_int32_t)iov_len;
		}

		upl_flags = UPL_QUERY_OBJECT_TYPE;

		vm_map_t map = UIO_SEG_IS_USER_SPACE(uio->uio_segflg) ? current_map() : kernel_map;
		if ((vm_map_get_upl(map,
#if HAS_MTE || HAS_MTE_EMULATION_SHIMS
		    vm_memtag_canonicalize(map, vm_map_trunc_page(iov_base, vm_map_page_mask(map))),
#else /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */
		    vm_map_trunc_page(iov_base, vm_map_page_mask(map)),
#endif /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */
		    &upl_size, &upl, NULL, NULL, &upl_flags, VM_KERN_MEMORY_FILE, 0)) != KERN_SUCCESS) {
			/*
			 * the user app must have passed in an invalid address
			 */
			retval = EFAULT;
		}
		if (upl_size == 0) {
			retval = EFAULT;
		}

		*io_length = upl_size;

		if (upl_flags & UPL_PHYS_CONTIG) {
			*io_type = IO_CONTIG;
		} else if (iov_len >= min_length) {
			*io_type = IO_DIRECT;
		} else {
			*io_type = IO_COPY;
		}
	} else {
		/*
		 * nothing left to do for this uio
		 */
		*io_length = 0;
		*io_type   = IO_UNKNOWN;
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 94)) | DBG_FUNC_END, iov_base, *io_type, *io_length, retval, 0);

	if (*io_type == IO_DIRECT &&
	    vm_map_page_shift(current_map()) < PAGE_SHIFT) {
		/* no direct I/O for sub-page-size address spaces */
		DEBUG4K_VFS("io_type IO_DIRECT -> IO_COPY\n");
		*io_type = IO_COPY;
	}

	return retval;
}


/*
 * generate advisory I/O's in the largest chunks possible
 * the completed pages will be released into the VM cache
 */
int
advisory_read(vnode_t vp, off_t filesize, off_t f_offset, int resid)
{
	return advisory_read_ext(vp, filesize, f_offset, resid, NULL, NULL, CL_PASSIVE);
}

int
advisory_read_ext(vnode_t vp, off_t filesize, off_t f_offset, int resid, int (*callback)(buf_t, void *), void *callback_arg, int bflag)
{
	upl_page_info_t *pl;
	upl_t            upl = NULL;
	vm_offset_t      upl_offset;
	int              upl_size;
	off_t            upl_f_offset;
	int              start_offset;
	int              start_pg;
	int              last_pg;
	int              pages_in_upl;
	off_t            max_size;
	int              io_size;
	kern_return_t    kret;
	int              retval = 0;
	int              issued_io;
	int              skip_range;
	uint32_t         max_io_size;


	if (!UBCINFOEXISTS(vp)) {
		return EINVAL;
	}

	if (f_offset < 0 || resid < 0) {
		return EINVAL;
	}

	max_io_size = cluster_max_io_size(vp->v_mount, CL_READ);

	if (disk_conditioner_mount_is_ssd(vp->v_mount)) {
		if (max_io_size > speculative_prefetch_max_iosize) {
			max_io_size = speculative_prefetch_max_iosize;
		}
	}

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 60)) | DBG_FUNC_START,
	    (int)f_offset, resid, (int)filesize, 0, 0);

	while (resid && f_offset < filesize && retval == 0) {
		/*
		 * compute the size of the upl needed to encompass
		 * the requested read... limit each call to cluster_io
		 * to the maximum UPL size... cluster_io will clip if
		 * this exceeds the maximum io_size for the device,
		 * make sure to account for
		 * a starting offset that's not page aligned
		 */
		start_offset = (int)(f_offset & PAGE_MASK_64);
		upl_f_offset = f_offset - (off_t)start_offset;
		max_size     = filesize - f_offset;

		if (resid < max_size) {
			io_size = resid;
		} else {
			io_size = (int)max_size;
		}

		upl_size = (start_offset + io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
		if ((uint32_t)upl_size > max_io_size) {
			upl_size = max_io_size;
		}

		skip_range = 0;
		/*
		 * return the number of contiguously present pages in the cache
		 * starting at upl_f_offset within the file
		 */
		ubc_range_op(vp, upl_f_offset, upl_f_offset + upl_size, UPL_ROP_PRESENT, &skip_range);

		if (skip_range) {
			/*
			 * skip over pages already present in the cache
			 */
			io_size = skip_range - start_offset;

			f_offset += io_size;
			resid    -= io_size;

			if (skip_range == upl_size) {
				continue;
			}
			/*
			 * have to issue some real I/O
			 * at this point, we know it's starting on a page boundary
			 * because we've skipped over at least the first page in the request
			 */
			start_offset = 0;
			upl_f_offset += skip_range;
			upl_size     -= skip_range;
		}
		pages_in_upl = upl_size / PAGE_SIZE;

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 61)) | DBG_FUNC_START,
		    upl, (int)upl_f_offset, upl_size, start_offset, 0);

		kret = ubc_create_upl_kernel(vp,
		    upl_f_offset,
		    upl_size,
		    &upl,
		    &pl,
		    UPL_RET_ONLY_ABSENT | UPL_SET_LITE,
		    VM_KERN_MEMORY_FILE);
		if (kret != KERN_SUCCESS) {
			return retval;
		}
		issued_io = 0;

		/*
		 * before we start marching forward, we must make sure we end on
		 * a present page, otherwise we will be working with a freed
		 * upl
		 */
		for (last_pg = pages_in_upl - 1; last_pg >= 0; last_pg--) {
			if (upl_page_present(pl, last_pg)) {
				break;
			}
		}
		pages_in_upl = last_pg + 1;


		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 61)) | DBG_FUNC_END,
		    upl, (int)upl_f_offset, upl_size, start_offset, 0);


		for (last_pg = 0; last_pg < pages_in_upl;) {
			/*
			 * scan from the beginning of the upl looking for the first
			 * page that is present.... this will become the first page in
			 * the request we're going to make to 'cluster_io'... if all
			 * of the pages are absent, we won't call through to 'cluster_io'
			 */
			for (start_pg = last_pg; start_pg < pages_in_upl; start_pg++) {
				if (upl_page_present(pl, start_pg)) {
					break;
				}
			}

			/*
			 * scan from the starting present page looking for an absent
			 * page before the end of the upl is reached, if we
			 * find one, then it will terminate the range of pages being
			 * presented to 'cluster_io'
			 */
			for (last_pg = start_pg; last_pg < pages_in_upl; last_pg++) {
				if (!upl_page_present(pl, last_pg)) {
					break;
				}
			}

			if (last_pg > start_pg) {
				/*
				 * we found a range of pages that must be filled
				 * if the last page in this range is the last page of the file
				 * we may have to clip the size of it to keep from reading past
				 * the end of the last physical block associated with the file
				 */
				upl_offset = start_pg * PAGE_SIZE;
				io_size    = (last_pg - start_pg) * PAGE_SIZE;

				if ((off_t)(upl_f_offset + upl_offset + io_size) > filesize) {
					io_size = (int)(filesize - (upl_f_offset + upl_offset));
				}

				/*
				 * issue an asynchronous read to cluster_io
				 */
				retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, io_size,
				    CL_ASYNC | CL_READ | CL_COMMIT | CL_AGE | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);

				issued_io = 1;
			}
		}
		if (issued_io == 0) {
			ubc_upl_abort(upl, 0);
		}

		io_size = upl_size - start_offset;

		if (io_size > resid) {
			io_size = resid;
		}
		f_offset += io_size;
		resid    -= io_size;
	}

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 60)) | DBG_FUNC_END,
	    (int)f_offset, resid, retval, 0, 0);

	return retval;
}


int
cluster_push(vnode_t vp, int flags)
{
	return cluster_push_ext(vp, flags, NULL, NULL);
}


int
cluster_push_ext(vnode_t vp, int flags, int (*callback)(buf_t, void *), void *callback_arg)
{
	return cluster_push_err(vp, flags, callback, callback_arg, NULL);
}

/* write errors via err, but return the number of clusters written */
extern uint32_t system_inshutdown;
uint32_t cl_sparse_push_error = 0;
int
cluster_push_err(vnode_t vp, int flags, int (*callback)(buf_t, void *), void *callback_arg, int *err)
{
	int     retval;
	int     my_sparse_wait = 0;
	struct  cl_writebehind *wbp;
	int     local_err = 0;

	if (err) {
		*err = 0;
	}

	if (!UBCINFOEXISTS(vp)) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_NONE, kdebug_vnode(vp), flags, 0, -1, 0);
		return 0;
	}
	/* return if deferred write is set */
	if (((unsigned int)vfs_flags(vp->v_mount) & MNT_DEFWRITE) && (flags & IO_DEFWRITE)) {
		return 0;
	}
	if ((wbp = cluster_get_wbp(vp, CLW_RETURNLOCKED)) == NULL) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_NONE, kdebug_vnode(vp), flags, 0, -2, 0);
		return 0;
	}
	if (!ISSET(flags, IO_SYNC) && wbp->cl_number == 0 && wbp->cl_scmap == NULL) {
		lck_mtx_unlock(&wbp->cl_lockw);

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_NONE, kdebug_vnode(vp), flags, 0, -3, 0);
		return 0;
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_START,
	    wbp->cl_scmap, wbp->cl_number, flags, 0, 0);

	/*
	 * if we have an fsync in progress, we don't want to allow any additional
	 * sync/fsync/close(s) to occur until it finishes.
	 * note that its possible for writes to continue to occur to this file
	 * while we're waiting and also once the fsync starts to clean if we're
	 * in the sparse map case
	 */
	while (wbp->cl_sparse_wait) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_START, kdebug_vnode(vp), 0, 0, 0, 0);

		msleep((caddr_t)&wbp->cl_sparse_wait, &wbp->cl_lockw, PRIBIO + 1, "cluster_push_ext", NULL);

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_END, kdebug_vnode(vp), 0, 0, 0, 0);
	}
	if (flags & IO_SYNC) {
		my_sparse_wait = 1;
		wbp->cl_sparse_wait = 1;

		/*
		 * this is an fsync (or equivalent)... we must wait for any existing async
		 * cleaning operations to complete before we evaulate the current state
		 * and finish cleaning... this insures that all writes issued before this
		 * fsync actually get cleaned to the disk before this fsync returns
		 */
		while (wbp->cl_sparse_pushes) {
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 98)) | DBG_FUNC_START, kdebug_vnode(vp), 0, 0, 0, 0);

			msleep((caddr_t)&wbp->cl_sparse_pushes, &wbp->cl_lockw, PRIBIO + 1, "cluster_push_ext", NULL);

			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 98)) | DBG_FUNC_END, kdebug_vnode(vp), 0, 0, 0, 0);
		}
	}
	if (wbp->cl_scmap) {
		void    *scmap;

		if (wbp->cl_sparse_pushes < SPARSE_PUSH_LIMIT) {
			scmap = wbp->cl_scmap;
			wbp->cl_scmap = NULL;

			wbp->cl_sparse_pushes++;

			lck_mtx_unlock(&wbp->cl_lockw);

			retval = sparse_cluster_push(wbp, &scmap, vp, ubc_getsize(vp), PUSH_ALL, flags, callback, callback_arg, FALSE);

			lck_mtx_lock(&wbp->cl_lockw);

			wbp->cl_sparse_pushes--;

			if (retval) {
				if (wbp->cl_scmap != NULL) {
					/*
					 * panic("cluster_push_err: Expected NULL cl_scmap\n");
					 *
					 * This can happen if we get an error from the underlying FS
					 * e.g. ENOSPC, EPERM or EIO etc. We hope that these errors
					 * are transient and the I/Os will succeed at a later point.
					 *
					 * The tricky part here is that a new sparse cluster has been
					 * allocated and tracking a different set of dirty pages. So these
					 * pages are not going to be pushed out with the next sparse_cluster_push.
					 * An explicit msync or file close will, however, push the pages out.
					 *
					 * What if those calls still don't work? And so, during shutdown we keep
					 * trying till we succeed...
					 */

					if (system_inshutdown) {
						if ((retval == ENOSPC) && (vp->v_mount->mnt_flag & (MNT_LOCAL | MNT_REMOVABLE)) == MNT_LOCAL) {
							os_atomic_inc(&cl_sparse_push_error, relaxed);
						}
					} else {
						vfs_drt_control(&scmap, 0); /* emit stats and free this memory. Dirty pages stay intact. */
						scmap = NULL;
					}
				} else {
					wbp->cl_scmap = scmap;
				}
			}

			if (wbp->cl_sparse_wait && wbp->cl_sparse_pushes == 0) {
				wakeup((caddr_t)&wbp->cl_sparse_pushes);
			}
		} else {
			retval = sparse_cluster_push(wbp, &(wbp->cl_scmap), vp, ubc_getsize(vp), PUSH_ALL, flags, callback, callback_arg, FALSE);
		}

		local_err = retval;

		if (err) {
			*err = retval;
		}
		retval = 1;
	} else {
		retval = cluster_try_push(wbp, vp, ubc_getsize(vp), PUSH_ALL, flags, callback, callback_arg, &local_err, FALSE);
		if (err) {
			*err = local_err;
		}
	}
	lck_mtx_unlock(&wbp->cl_lockw);

	if (flags & IO_SYNC) {
		(void)vnode_waitforwrites(vp, 0, 0, 0, "cluster_push");
	}

	if (my_sparse_wait) {
		/*
		 * I'm the owner of the serialization token
		 * clear it and wakeup anyone that is waiting
		 * for me to finish
		 */
		lck_mtx_lock(&wbp->cl_lockw);

		wbp->cl_sparse_wait = 0;
		wakeup((caddr_t)&wbp->cl_sparse_wait);

		lck_mtx_unlock(&wbp->cl_lockw);
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_END,
	    wbp->cl_scmap, wbp->cl_number, retval, local_err, 0);

	return retval;
}


__private_extern__ void
cluster_release(struct ubc_info *ubc)
{
	struct cl_writebehind *wbp;
	struct cl_readahead   *rap;

	if ((wbp = ubc->cl_wbehind)) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 81)) | DBG_FUNC_START, ubc, wbp->cl_scmap, 0, 0, 0);

		if (wbp->cl_scmap) {
			vfs_drt_control(&(wbp->cl_scmap), 0);
		}
		lck_mtx_destroy(&wbp->cl_lockw, &cl_mtx_grp);
		zfree(cl_wr_zone, wbp);
		ubc->cl_wbehind = NULL;
	} else {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 81)) | DBG_FUNC_START, ubc, 0, 0, 0, 0);
	}

	if ((rap = ubc->cl_rahead)) {
		lck_mtx_destroy(&rap->cl_lockr, &cl_mtx_grp);
		zfree(cl_rd_zone, rap);
		ubc->cl_rahead  = NULL;
	}

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 81)) | DBG_FUNC_END, ubc, rap, wbp, 0, 0);
}


static int
cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int push_flag, int io_flags, int (*callback)(buf_t, void *), void *callback_arg, int *err, boolean_t vm_initiated)
{
	int cl_index;
	int cl_index1;
	int min_index;
	int cl_len;
	int cl_pushed = 0;
	struct cl_wextent l_clusters[MAX_CLUSTERS];
	u_int  max_cluster_pgcount;
	int error = 0;

	max_cluster_pgcount = MAX_CLUSTER_SIZE(vp) / PAGE_SIZE;
	/*
	 * the write behind context exists and has
	 * already been locked...
	 */
	if (wbp->cl_number == 0) {
		/*
		 * no clusters to push
		 * return number of empty slots
		 */
		return MAX_CLUSTERS;
	}

	/*
	 * make a local 'sorted' copy of the clusters
	 * and clear wbp->cl_number so that new clusters can
	 * be developed
	 */
	for (cl_index = 0; cl_index < wbp->cl_number; cl_index++) {
		for (min_index = -1, cl_index1 = 0; cl_index1 < wbp->cl_number; cl_index1++) {
			if (wbp->cl_clusters[cl_index1].b_addr == wbp->cl_clusters[cl_index1].e_addr) {
				continue;
			}
			if (min_index == -1) {
				min_index = cl_index1;
			} else if (wbp->cl_clusters[cl_index1].b_addr < wbp->cl_clusters[min_index].b_addr) {
				min_index = cl_index1;
			}
		}
		if (min_index == -1) {
			break;
		}

		l_clusters[cl_index].b_addr = wbp->cl_clusters[min_index].b_addr;
		l_clusters[cl_index].e_addr = wbp->cl_clusters[min_index].e_addr;
		l_clusters[cl_index].io_flags = wbp->cl_clusters[min_index].io_flags;

		wbp->cl_clusters[min_index].b_addr = wbp->cl_clusters[min_index].e_addr;
	}
	wbp->cl_number = 0;

	cl_len = cl_index;

	/* skip switching to the sparse cluster mechanism if on diskimage */
	if (((push_flag & PUSH_DELAY) && cl_len == MAX_CLUSTERS) &&
	    !(vp->v_mount->mnt_kern_flag & MNTK_VIRTUALDEV)) {
		int   i;

		/*
		 * determine if we appear to be writing the file sequentially
		 * if not, by returning without having pushed any clusters
		 * we will cause this vnode to be pushed into the sparse cluster mechanism
		 * used for managing more random I/O patterns
		 *
		 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
		 * that's why we're in try_push with PUSH_DELAY...
		 *
		 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
		 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
		 * so we can just make a simple pass through, up to, but not including the last one...
		 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
		 * are sequential
		 *
		 * we let the last one be partial as long as it was adjacent to the previous one...
		 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
		 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
		 */
		for (i = 0; i < MAX_CLUSTERS - 1; i++) {
			if ((l_clusters[i].e_addr - l_clusters[i].b_addr) != max_cluster_pgcount) {
				goto dont_try;
			}
			if (l_clusters[i].e_addr != l_clusters[i + 1].b_addr) {
				goto dont_try;
			}
		}
	}
	if (vm_initiated == TRUE) {
		lck_mtx_unlock(&wbp->cl_lockw);
	}

	for (cl_index = 0; cl_index < cl_len; cl_index++) {
		int     flags;
		struct  cl_extent cl;
		int retval;

		flags = io_flags & (IO_PASSIVE | IO_CLOSE | IO_SYNC);

		/*
		 * try to push each cluster in turn...
		 */
		if (l_clusters[cl_index].io_flags & CLW_IONOCACHE) {
			flags |= IO_NOCACHE;
		}

		if (l_clusters[cl_index].io_flags & CLW_IOPASSIVE) {
			flags |= IO_PASSIVE;
		}

		if (push_flag & PUSH_SYNC) {
			flags |= IO_SYNC;
		}

		cl.b_addr = l_clusters[cl_index].b_addr;
		cl.e_addr = l_clusters[cl_index].e_addr;

		retval = cluster_push_now(vp, &cl, EOF, flags, callback, callback_arg, vm_initiated);

		if (retval == 0) {
			cl_pushed++;

			l_clusters[cl_index].b_addr = 0;
			l_clusters[cl_index].e_addr = 0;
		} else if (error == 0) {
			error = retval;
		}

		if (!(push_flag & PUSH_ALL)) {
			break;
		}
	}
	if (vm_initiated == TRUE) {
		lck_mtx_lock(&wbp->cl_lockw);
	}

	if (err) {
		*err = error;
	}

dont_try:
	if (cl_len > cl_pushed) {
		/*
		 * we didn't push all of the clusters, so
		 * lets try to merge them back in to the vnode
		 */
		if ((MAX_CLUSTERS - wbp->cl_number) < (cl_len - cl_pushed)) {
			/*
			 * we picked up some new clusters while we were trying to
			 * push the old ones... this can happen because I've dropped
			 * the vnode lock... the sum of the
			 * leftovers plus the new cluster count exceeds our ability
			 * to represent them, so switch to the sparse cluster mechanism
			 *
			 * collect the active public clusters...
			 */
			sparse_cluster_switch(wbp, vp, EOF, callback, callback_arg, vm_initiated);

			for (cl_index = 0, cl_index1 = 0; cl_index < cl_len; cl_index++) {
				if (l_clusters[cl_index].b_addr == l_clusters[cl_index].e_addr) {
					continue;
				}
				wbp->cl_clusters[cl_index1].b_addr = l_clusters[cl_index].b_addr;
				wbp->cl_clusters[cl_index1].e_addr = l_clusters[cl_index].e_addr;
				wbp->cl_clusters[cl_index1].io_flags = l_clusters[cl_index].io_flags;

				cl_index1++;
			}
			/*
			 * update the cluster count
			 */
			wbp->cl_number = cl_index1;

			/*
			 * and collect the original clusters that were moved into the
			 * local storage for sorting purposes
			 */
			sparse_cluster_switch(wbp, vp, EOF, callback, callback_arg, vm_initiated);
		} else {
			/*
			 * we've got room to merge the leftovers back in
			 * just append them starting at the next 'hole'
			 * represented by wbp->cl_number
			 */
			for (cl_index = 0, cl_index1 = wbp->cl_number; cl_index < cl_len; cl_index++) {
				if (l_clusters[cl_index].b_addr == l_clusters[cl_index].e_addr) {
					continue;
				}

				wbp->cl_clusters[cl_index1].b_addr = l_clusters[cl_index].b_addr;
				wbp->cl_clusters[cl_index1].e_addr = l_clusters[cl_index].e_addr;
				wbp->cl_clusters[cl_index1].io_flags = l_clusters[cl_index].io_flags;

				cl_index1++;
			}
			/*
			 * update the cluster count
			 */
			wbp->cl_number = cl_index1;
		}
	}
	return MAX_CLUSTERS - wbp->cl_number;
}



static int
cluster_push_now(vnode_t vp, struct cl_extent *cl, off_t EOF, int flags,
    int (*callback)(buf_t, void *), void *callback_arg, boolean_t vm_initiated)
{
	upl_page_info_t *pl;
	upl_t            upl;
	vm_offset_t      upl_offset;
	int              upl_size;
	off_t            upl_f_offset;
	int              pages_in_upl;
	int              start_pg;
	int              last_pg;
	int              io_size;
	int              io_flags;
	int              upl_flags;
	int              bflag;
	int              size;
	int              error = 0;
	int              retval;
	kern_return_t    kret;

	if (flags & IO_PASSIVE) {
		bflag = CL_PASSIVE;
	} else {
		bflag = 0;
	}

	if (flags & IO_SKIP_ENCRYPTION) {
		bflag |= CL_ENCRYPTED;
	}

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_START,
	    (int)cl->b_addr, (int)cl->e_addr, (int)EOF, flags, 0);

	if ((pages_in_upl = (int)(cl->e_addr - cl->b_addr)) == 0) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_END, 1, 0, 0, 0, 0);

		return 0;
	}
	upl_size = pages_in_upl * PAGE_SIZE;
	upl_f_offset = (off_t)(cl->b_addr * PAGE_SIZE_64);

	if (upl_f_offset + upl_size >= EOF) {
		if (upl_f_offset >= EOF) {
			/*
			 * must have truncated the file and missed
			 * clearing a dangling cluster (i.e. it's completely
			 * beyond the new EOF
			 */
			KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_END, 1, 1, 0, 0, 0);

			return 0;
		}
		size = (int)(EOF - upl_f_offset);

		upl_size = (size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
		pages_in_upl = upl_size / PAGE_SIZE;
	} else {
		size = upl_size;
	}


	if (vm_initiated) {
		vnode_pageout(vp, NULL, (upl_offset_t)0, upl_f_offset, (upl_size_t)upl_size,
		    UPL_MSYNC | UPL_VNODE_PAGER | UPL_KEEPCACHED, &error);

		return error;
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_START, upl_size, size, 0, 0, 0);

	/*
	 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
	 *
	 * - only pages that are currently dirty are returned... these are the ones we need to clean
	 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
	 * - if we have to abort the I/O for some reason, the software dirty bit is left set since we didn't clean the page
	 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
	 *   someone dirties this page while the I/O is in progress, we don't lose track of the new state
	 *
	 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
	 */

	if ((vp->v_flag & VNOCACHE_DATA) || (flags & IO_NOCACHE)) {
		upl_flags = UPL_COPYOUT_FROM | UPL_RET_ONLY_DIRTY | UPL_SET_LITE | UPL_WILL_BE_DUMPED;
	} else {
		upl_flags = UPL_COPYOUT_FROM | UPL_RET_ONLY_DIRTY | UPL_SET_LITE;
	}

	kret = ubc_create_upl_kernel(vp,
	    upl_f_offset,
	    upl_size,
	    &upl,
	    &pl,
	    upl_flags,
	    VM_KERN_MEMORY_FILE);
	if (kret != KERN_SUCCESS) {
		panic("cluster_push: failed to get pagelist");
	}

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_END, upl, upl_f_offset, 0, 0, 0);

	/*
	 * since we only asked for the dirty pages back
	 * it's possible that we may only get a few or even none, so...
	 * before we start marching forward, we must make sure we know
	 * where the last present page is in the UPL, otherwise we could
	 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
	 * employed by commit_range and abort_range.
	 */
	for (last_pg = pages_in_upl - 1; last_pg >= 0; last_pg--) {
		if (upl_page_present(pl, last_pg)) {
			break;
		}
	}
	pages_in_upl = last_pg + 1;

	if (pages_in_upl == 0) {
		ubc_upl_abort(upl, 0);

		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_END, 1, 2, 0, 0, 0);
		return 0;
	}

	for (last_pg = 0; last_pg < pages_in_upl;) {
		/*
		 * find the next dirty page in the UPL
		 * this will become the first page in the
		 * next I/O to generate
		 */
		for (start_pg = last_pg; start_pg < pages_in_upl; start_pg++) {
			if (upl_dirty_page(pl, start_pg)) {
				break;
			}
			if (upl_page_present(pl, start_pg)) {
				/*
				 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
				 * just release these unchanged since we're not going
				 * to steal them or change their state
				 */
				ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
			}
		}
		if (start_pg >= pages_in_upl) {
			/*
			 * done... no more dirty pages to push
			 */
			break;
		}
		if (start_pg > last_pg) {
			/*
			 * skipped over some non-dirty pages
			 */
			size -= ((start_pg - last_pg) * PAGE_SIZE);
		}

		/*
		 * find a range of dirty pages to write
		 */
		for (last_pg = start_pg; last_pg < pages_in_upl; last_pg++) {
			if (!upl_dirty_page(pl, last_pg)) {
				break;
			}
		}
		upl_offset = start_pg * PAGE_SIZE;

		io_size = min(size, (last_pg - start_pg) * PAGE_SIZE);

		io_flags = CL_THROTTLE | CL_COMMIT | CL_AGE | bflag;

		if (!(flags & IO_SYNC)) {
			io_flags |= CL_ASYNC;
		}

		if (flags & IO_CLOSE) {
			io_flags |= CL_CLOSE;
		}

		if (flags & IO_NOCACHE) {
			io_flags |= CL_NOCACHE;
		}

		retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, io_size,
		    io_flags, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);

		if (error == 0 && retval) {
			error = retval;
		}

		size -= io_size;
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_END, 1, 3, error, 0, 0);

	return error;
}


/*
 * sparse_cluster_switch is called with the write behind lock held
 */
static int
sparse_cluster_switch(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int (*callback)(buf_t, void *), void *callback_arg, boolean_t vm_initiated)
{
	int     cl_index;
	int     error = 0;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 78)) | DBG_FUNC_START, kdebug_vnode(vp), wbp->cl_scmap, wbp->cl_number, 0, 0);

	for (cl_index = 0; cl_index < wbp->cl_number; cl_index++) {
		int       flags;
		struct cl_extent cl;

		for (cl.b_addr = wbp->cl_clusters[cl_index].b_addr; cl.b_addr < wbp->cl_clusters[cl_index].e_addr; cl.b_addr++) {
			if (ubc_page_op(vp, (off_t)(cl.b_addr * PAGE_SIZE_64), 0, NULL, &flags) == KERN_SUCCESS) {
				if (flags & UPL_POP_DIRTY) {
					cl.e_addr = cl.b_addr + 1;

					error = sparse_cluster_add(wbp, &(wbp->cl_scmap), vp, &cl, EOF, callback, callback_arg, vm_initiated);

					if (error) {
						break;
					}
				}
			}
		}
	}
	wbp->cl_number -= cl_index;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 78)) | DBG_FUNC_END, kdebug_vnode(vp), wbp->cl_scmap, wbp->cl_number, error, 0);

	return error;
}


/*
 * sparse_cluster_push must be called with the write-behind lock held if the scmap is
 * still associated with the write-behind context... however, if the scmap has been disassociated
 * from the write-behind context (the cluster_push case), the wb lock is not held
 */
static int
sparse_cluster_push(struct cl_writebehind *wbp, void **scmap, vnode_t vp, off_t EOF, int push_flag,
    int io_flags, int (*callback)(buf_t, void *), void *callback_arg, boolean_t vm_initiated)
{
	struct cl_extent cl;
	off_t           offset;
	u_int           length;
	void            *l_scmap;
	int error = 0;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 79)) | DBG_FUNC_START, kdebug_vnode(vp), (*scmap), 0, push_flag, 0);

	if (push_flag & PUSH_ALL) {
		vfs_drt_control(scmap, 1);
	}

	l_scmap = *scmap;

	for (;;) {
		int retval;

		if (vfs_drt_get_cluster(scmap, &offset, &length) != KERN_SUCCESS) {
			/*
			 * Not finding anything to push will return KERN_FAILURE.
			 * Confusing since it isn't really a failure. But that's the
			 * reason we don't set 'error' here like we do below.
			 */
			break;
		}

		if (vm_initiated == TRUE) {
			lck_mtx_unlock(&wbp->cl_lockw);
		}

		cl.b_addr = (daddr64_t)(offset / PAGE_SIZE_64);
		cl.e_addr = (daddr64_t)((offset + length) / PAGE_SIZE_64);

		retval = cluster_push_now(vp, &cl, EOF, io_flags, callback, callback_arg, vm_initiated);
		if (error == 0 && retval) {
			error = retval;
		}

		if (vm_initiated == TRUE) {
			lck_mtx_lock(&wbp->cl_lockw);

			if (*scmap != l_scmap) {
				break;
			}
		}

		if (error) {
			if (vfs_drt_mark_pages(scmap, offset, length, NULL) != KERN_SUCCESS) {
				panic("Failed to restore dirty state on failure");
			}

			break;
		}

		if (!(push_flag & PUSH_ALL)) {
			break;
		}
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 79)) | DBG_FUNC_END, kdebug_vnode(vp), (*scmap), error, 0, 0);

	return error;
}


/*
 * sparse_cluster_add is called with the write behind lock held
 */
static int
sparse_cluster_add(struct cl_writebehind *wbp, void **scmap, vnode_t vp, struct cl_extent *cl, off_t EOF,
    int (*callback)(buf_t, void *), void *callback_arg, boolean_t vm_initiated)
{
	u_int   new_dirty;
	u_int   length;
	off_t   offset;
	int     error = 0;
	int     push_flag = 0; /* Is this a valid value? */

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 80)) | DBG_FUNC_START, (*scmap), 0, cl->b_addr, (int)cl->e_addr, 0);

	offset = (off_t)(cl->b_addr * PAGE_SIZE_64);
	length = ((u_int)(cl->e_addr - cl->b_addr)) * PAGE_SIZE;

	while (vfs_drt_mark_pages(scmap, offset, length, &new_dirty) != KERN_SUCCESS) {
		/*
		 * no room left in the map
		 * only a partial update was done
		 * push out some pages and try again
		 */

		if (vfs_get_scmap_push_behavior_internal(scmap, &push_flag)) {
			push_flag = 0;
		}

		error = sparse_cluster_push(wbp, scmap, vp, EOF, push_flag, 0, callback, callback_arg, vm_initiated);

		if (error) {
			break;
		}

		offset += (new_dirty * PAGE_SIZE_64);
		length -= (new_dirty * PAGE_SIZE);
	}
	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 80)) | DBG_FUNC_END, kdebug_vnode(vp), (*scmap), error, 0, 0);

	return error;
}


static int
cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, u_int32_t xsize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
{
	upl_page_info_t  *pl;
	upl_t            upl;
	addr64_t         ubc_paddr;
	kern_return_t    kret;
	int              error = 0;
	int              did_read = 0;
	int              abort_flags;
	int              upl_flags;
	int              bflag;

	if (flags & IO_PASSIVE) {
		bflag = CL_PASSIVE;
	} else {
		bflag = 0;
	}

	if (flags & IO_NOCACHE) {
		bflag |= CL_NOCACHE;
	}

	upl_flags = UPL_SET_LITE;

	if (!(flags & CL_READ)) {
		/*
		 * "write" operation:  let the UPL subsystem know
		 * that we intend to modify the buffer cache pages
		 * we're gathering.
		 */
		upl_flags |= UPL_WILL_MODIFY;
	} else {
		/*
		 * indicate that there is no need to pull the
		 * mapping for this page... we're only going
		 * to read from it, not modify it.
		 */
		upl_flags |= UPL_FILE_IO;
	}
	kret = ubc_create_upl_kernel(vp,
	    uio->uio_offset & ~PAGE_MASK_64,
	    PAGE_SIZE,
	    &upl,
	    &pl,
	    upl_flags,
	    VM_KERN_MEMORY_FILE);

	if (kret != KERN_SUCCESS) {
		return EINVAL;
	}

	if (!upl_valid_page(pl, 0)) {
		/*
		 * issue a synchronous read to cluster_io
		 */
		error = cluster_io(vp, upl, 0, uio->uio_offset & ~PAGE_MASK_64, PAGE_SIZE,
		    CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
		if (error) {
			ubc_upl_abort_range(upl, 0, PAGE_SIZE, UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);

			return error;
		}
		did_read = 1;
	}
	ubc_paddr = ((addr64_t)upl_phys_page(pl, 0) << PAGE_SHIFT) + (addr64_t)(uio->uio_offset & PAGE_MASK_64);

/*
 *	NOTE:  There is no prototype for the following in BSD. It, and the definitions
 *	of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
 *	osfmk/ppc/mappings.h.  They are not included here because there appears to be no
 *	way to do so without exporting them to kexts as well.
 */
	if (flags & CL_READ) {
//		copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk);	/* Copy physical to physical and flush the destination */
		copypv(ubc_paddr, usr_paddr, xsize, 2 |        1 |        4);           /* Copy physical to physical and flush the destination */
	} else {
//		copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc);	/* Copy physical to physical and flush the source */
		copypv(usr_paddr, ubc_paddr, xsize, 2 |        1 |        8);           /* Copy physical to physical and flush the source */
	}
	if (!(flags & CL_READ) || (upl_valid_page(pl, 0) && upl_dirty_page(pl, 0))) {
		/*
		 * issue a synchronous write to cluster_io
		 */
		error = cluster_io(vp, upl, 0, uio->uio_offset & ~PAGE_MASK_64, PAGE_SIZE,
		    bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
	}
	if (error == 0) {
		uio_update(uio, (user_size_t)xsize);
	}

	if (did_read) {
		abort_flags = UPL_ABORT_FREE_ON_EMPTY;
	} else {
		abort_flags = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_DUMP_PAGES;
	}

	ubc_upl_abort_range(upl, 0, PAGE_SIZE, abort_flags);

	return error;
}

int
cluster_copy_upl_data(struct uio *uio, upl_t upl, int upl_offset, int *io_resid)
{
	int       pg_offset;
	int       pg_index;
	int       csize;
	int       segflg;
	int       retval = 0;
	int       xsize;
	upl_page_info_t *pl;
	int       dirty_count;

	xsize = *io_resid;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_START,
	    (int)uio->uio_offset, upl_offset, xsize, 0, 0);

	segflg = uio->uio_segflg;

	switch (segflg) {
	case UIO_USERSPACE32:
	case UIO_USERISPACE32:
		uio->uio_segflg = UIO_PHYS_USERSPACE32;
		break;

	case UIO_USERSPACE:
	case UIO_USERISPACE:
		uio->uio_segflg = UIO_PHYS_USERSPACE;
		break;

	case UIO_USERSPACE64:
	case UIO_USERISPACE64:
		uio->uio_segflg = UIO_PHYS_USERSPACE64;
		break;

	case UIO_SYSSPACE:
		uio->uio_segflg = UIO_PHYS_SYSSPACE;
		break;
	}
	pl = ubc_upl_pageinfo(upl);

	pg_index  = upl_offset / PAGE_SIZE;
	pg_offset = upl_offset & PAGE_MASK;
	csize     = min(PAGE_SIZE - pg_offset, xsize);

	dirty_count = 0;
	while (xsize && retval == 0) {
		addr64_t  paddr;
		ppnum_t pn = upl_phys_page(pl, pg_index);

		paddr = ((addr64_t)pn << PAGE_SHIFT) + pg_offset;
		if ((uio->uio_rw == UIO_WRITE) && (upl_dirty_page(pl, pg_index) == FALSE)) {
			dirty_count++;
		}

		/* such phyiscal pages should never be restricted pages */
		if (pmap_is_page_restricted(pn)) {
			panic("%s: cannot uiomove64 into a restricted page", __func__);
		}

		retval = uiomove64(paddr, csize, uio);

		pg_index += 1;
		pg_offset = 0;
		xsize    -= csize;
		csize     = min(PAGE_SIZE, xsize);
	}
	*io_resid = xsize;

	uio->uio_segflg = segflg;

	if (dirty_count) {
		task_update_logical_writes(current_task(), (dirty_count * PAGE_SIZE), TASK_WRITE_DEFERRED, upl_lookup_vnode(upl));
	}

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
	    (int)uio->uio_offset, xsize, retval, segflg, 0);

	return retval;
}


int
cluster_copy_ubc_data(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty)
{
	return cluster_copy_ubc_data_internal(vp, uio, io_resid, mark_dirty, 1);
}


static int
cluster_copy_ubc_data_internal(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty, int take_reference)
{
	int       segflg;
	int       io_size;
	int       xsize;
	int       start_offset;
	int       retval = 0;
	memory_object_control_t  control;

	io_size = *io_resid;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_START,
	    (int)uio->uio_offset, io_size, mark_dirty, take_reference, 0);

	control = ubc_getobject(vp, UBC_FLAGS_NONE);

	if (control == MEMORY_OBJECT_CONTROL_NULL) {
		KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
		    (int)uio->uio_offset, io_size, retval, 3, 0);

		return 0;
	}
	segflg = uio->uio_segflg;

	switch (segflg) {
	case UIO_USERSPACE32:
	case UIO_USERISPACE32:
		uio->uio_segflg = UIO_PHYS_USERSPACE32;
		break;

	case UIO_USERSPACE64:
	case UIO_USERISPACE64:
		uio->uio_segflg = UIO_PHYS_USERSPACE64;
		break;

	case UIO_USERSPACE:
	case UIO_USERISPACE:
		uio->uio_segflg = UIO_PHYS_USERSPACE;
		break;

	case UIO_SYSSPACE:
		uio->uio_segflg = UIO_PHYS_SYSSPACE;
		break;
	}

	if ((io_size = *io_resid)) {
		start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
		xsize = (int)uio_resid(uio);

		retval = memory_object_control_uiomove(control, uio->uio_offset - start_offset, uio,
		    start_offset, io_size, mark_dirty, take_reference);
		xsize -= uio_resid(uio);

		int num_bytes_copied = xsize;
		if (num_bytes_copied && uio_rw(uio)) {
			task_update_logical_writes(current_task(), num_bytes_copied, TASK_WRITE_DEFERRED, vp);
		}
		io_size -= xsize;
	}
	uio->uio_segflg = segflg;
	*io_resid       = io_size;

	KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
	    (int)uio->uio_offset, io_size, retval, 0x80000000 | segflg, 0);

	return retval;
}


int
is_file_clean(vnode_t vp, off_t filesize)
{
	off_t f_offset;
	int   flags;
	int   total_dirty = 0;

	for (f_offset = 0; f_offset < filesize; f_offset += PAGE_SIZE_64) {
		if (ubc_page_op(vp, f_offset, 0, NULL, &flags) == KERN_SUCCESS) {
			if (flags & UPL_POP_DIRTY) {
				total_dirty++;
			}
		}
	}
	if (total_dirty) {
		return EINVAL;
	}

	return 0;
}



/*
 * Dirty region tracking/clustering mechanism.
 *
 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
 * dirty regions within a larger space (file).  It is primarily intended to
 * support clustering in large files with many dirty areas.
 *
 * The implementation assumes that the dirty regions are pages.
 *
 * To represent dirty pages within the file, we store bit vectors in a
 * variable-size circular hash.
 */

/*
 * Bitvector size.  This determines the number of pages we group in a
 * single hashtable entry.  Each hashtable entry is aligned to this
 * size within the file.
 */
#define DRT_BITVECTOR_PAGES             ((1024 * 256) / PAGE_SIZE)

/*
 * File offset handling.
 *
 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
 * the correct formula is  (~((DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1))
 */
#define DRT_ADDRESS_MASK                (~((DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1))
#define DRT_ALIGN_ADDRESS(addr)         ((addr) & DRT_ADDRESS_MASK)

/*
 * Hashtable address field handling.
 *
 * The low-order bits of the hashtable address are used to conserve
 * space.
 *
 * DRT_HASH_COUNT_MASK must be large enough to store the range
 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
 * to indicate that the bucket is actually unoccupied.
 */
#define DRT_HASH_GET_ADDRESS(scm, i)    ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
#define DRT_HASH_SET_ADDRESS(scm, i, a)                                                                 \
	do {                                                                                            \
	        (scm)->scm_hashtable[(i)].dhe_control =                                                 \
	            ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
	} while (0)
#define DRT_HASH_COUNT_MASK             0x1ff
#define DRT_HASH_GET_COUNT(scm, i)      ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
#define DRT_HASH_SET_COUNT(scm, i, c)                                                                                   \
	do {                                                                                                            \
	        (scm)->scm_hashtable[(i)].dhe_control =                                                                 \
	            ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK);       \
	} while (0)
#define DRT_HASH_CLEAR(scm, i)                                                                                          \
	do {                                                                                                            \
	        (scm)->scm_hashtable[(i)].dhe_control =	0;                                                              \
	} while (0)
#define DRT_HASH_VACATE(scm, i)         DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
#define DRT_HASH_VACANT(scm, i)         (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
#define DRT_HASH_COPY(oscm, oi, scm, i)                                                                 \
	do {                                                                                            \
	        (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control;        \
	        DRT_BITVECTOR_COPY(oscm, oi, scm, i);                                                   \
	} while(0);


#if !defined(XNU_TARGET_OS_OSX)
/*
 * Hash table moduli.
 *
 * Since the hashtable entry's size is dependent on the size of
 * the bitvector, and since the hashtable size is constrained to
 * both being prime and fitting within the desired allocation
 * size, these values need to be manually determined.
 *
 * For DRT_BITVECTOR_SIZE = 64, the entry size is 16 bytes.
 *
 * The small hashtable allocation is 4096 bytes, so the modulus is 251.
 * The large hashtable allocation is 32768 bytes, so the modulus is 2039.
 * The xlarge hashtable allocation is 131072 bytes, so the modulus is 8179.
 */

#define DRT_HASH_SMALL_MODULUS  251
#define DRT_HASH_LARGE_MODULUS  2039
#define DRT_HASH_XLARGE_MODULUS  8179

/*
 * Physical memory required before the large hash modulus is permitted.
 *
 * On small memory systems, the large hash modulus can lead to phsyical
 * memory starvation, so we avoid using it there.
 */
#define DRT_HASH_LARGE_MEMORY_REQUIRED  (1024LL * 1024LL * 1024LL)      /* 1GiB */
#define DRT_HASH_XLARGE_MEMORY_REQUIRED  (8 * 1024LL * 1024LL * 1024LL)  /* 8GiB */

#define DRT_SMALL_ALLOCATION    4096    /* 80 bytes spare */
#define DRT_LARGE_ALLOCATION    32768   /* 144 bytes spare */
#define DRT_XLARGE_ALLOCATION    131072  /* 208 bytes spare */

#else /* XNU_TARGET_OS_OSX */
/*
 * Hash table moduli.
 *
 * Since the hashtable entry's size is dependent on the size of
 * the bitvector, and since the hashtable size is constrained to
 * both being prime and fitting within the desired allocation
 * size, these values need to be manually determined.
 *
 * For DRT_BITVECTOR_SIZE = 64, the entry size is 16 bytes.
 *
 * The small hashtable allocation is 16384 bytes, so the modulus is 1019.
 * The large hashtable allocation is 131072 bytes, so the modulus is 8179.
 * The xlarge hashtable allocation is 524288 bytes, so the modulus is 32749.
 */

#define DRT_HASH_SMALL_MODULUS  1019
#define DRT_HASH_LARGE_MODULUS  8179
#define DRT_HASH_XLARGE_MODULUS  32749

/*
 * Physical memory required before the large hash modulus is permitted.
 *
 * On small memory systems, the large hash modulus can lead to phsyical
 * memory starvation, so we avoid using it there.
 */
#define DRT_HASH_LARGE_MEMORY_REQUIRED  (4 * 1024LL * 1024LL * 1024LL)  /* 4GiB */
#define DRT_HASH_XLARGE_MEMORY_REQUIRED  (32 * 1024LL * 1024LL * 1024LL)  /* 32GiB */

#define DRT_SMALL_ALLOCATION    16384   /* 80 bytes spare */
#define DRT_LARGE_ALLOCATION    131072  /* 208 bytes spare */
#define DRT_XLARGE_ALLOCATION   524288  /* 304 bytes spare */

#endif /* ! XNU_TARGET_OS_OSX */

/* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */

/*
 * Hashtable entry.
 */
struct vfs_drt_hashentry {
	u_int64_t       dhe_control;
/*
 * dhe_bitvector was declared as dhe_bitvector[DRT_BITVECTOR_PAGES / 32];
 * DRT_BITVECTOR_PAGES is defined as ((1024 * 256) / PAGE_SIZE)
 * Since PAGE_SIZE is only known at boot time,
 *	-define MAX_DRT_BITVECTOR_PAGES for smallest supported page size (4k)
 *	-declare dhe_bitvector array for largest possible length
 */
#define MAX_DRT_BITVECTOR_PAGES (1024 * 256)/( 4 * 1024)
	u_int32_t       dhe_bitvector[MAX_DRT_BITVECTOR_PAGES / 32];
};

/*
 * Hashtable bitvector handling.
 *
 * Bitvector fields are 32 bits long.
 */

#define DRT_HASH_SET_BIT(scm, i, bit)                           \
	(scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))

#define DRT_HASH_CLEAR_BIT(scm, i, bit)                         \
	(scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))

#define DRT_HASH_TEST_BIT(scm, i, bit)                          \
	((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))

#define DRT_BITVECTOR_CLEAR(scm, i)                             \
	bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (MAX_DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))

#define DRT_BITVECTOR_COPY(oscm, oi, scm, i)                    \
	bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0],    \
	    &(scm)->scm_hashtable[(i)].dhe_bitvector[0],        \
	    (MAX_DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))

/*
 * Dirty Region Tracking structure.
 *
 * The hashtable is allocated entirely inside the DRT structure.
 *
 * The hash is a simple circular prime modulus arrangement, the structure
 * is resized from small to large if it overflows.
 */

struct vfs_drt_clustermap {
	u_int32_t               scm_magic;      /* sanity/detection */
#define DRT_SCM_MAGIC           0x12020003
	u_int32_t               scm_modulus;    /* current ring size */
	u_int32_t               scm_buckets;    /* number of occupied buckets */
	u_int32_t               scm_lastclean;  /* last entry we cleaned */
	u_int32_t               scm_iskips;     /* number of slot skips */

	struct vfs_drt_hashentry scm_hashtable[0];
};


#define DRT_HASH(scm, addr)             ((addr) % (scm)->scm_modulus)
#define DRT_HASH_NEXT(scm, addr)        (((addr) + 1) % (scm)->scm_modulus)

/*
 * Debugging codes and arguments.
 */
#define DRT_DEBUG_EMPTYFREE     (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
#define DRT_DEBUG_RETCLUSTER    (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
#define DRT_DEBUG_ALLOC         (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
#define DRT_DEBUG_INSERT        (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
#define DRT_DEBUG_MARK          (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
	                                                    * dirty */
                                                           /* 0, setcount */
                                                           /* 1 (clean, no map) */
                                                           /* 2 (map alloc fail) */
                                                           /* 3, resid (partial) */
#define DRT_DEBUG_6             (FSDBG_CODE(DBG_FSRW, 87))
#define DRT_DEBUG_SCMDATA       (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
	                                                    * lastclean, iskips */


static kern_return_t    vfs_drt_alloc_map(struct vfs_drt_clustermap **cmapp);
static kern_return_t    vfs_drt_free_map(struct vfs_drt_clustermap *cmap);
static kern_return_t    vfs_drt_search_index(struct vfs_drt_clustermap *cmap,
    u_int64_t offset, int *indexp);
static kern_return_t    vfs_drt_get_index(struct vfs_drt_clustermap **cmapp,
    u_int64_t offset,
    int *indexp,
    int recursed);
static kern_return_t    vfs_drt_do_mark_pages(
	void            **cmapp,
	u_int64_t       offset,
	u_int           length,
	u_int           *setcountp,
	int             dirty);
static void             vfs_drt_trace(
	struct vfs_drt_clustermap *cmap,
	int code,
	int arg1,
	int arg2,
	int arg3,
	int arg4);


/*
 * Allocate and initialise a sparse cluster map.
 *
 * Will allocate a new map, resize or compact an existing map.
 *
 * XXX we should probably have at least one intermediate map size,
 * as the 1:16 ratio seems a bit drastic.
 */
static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap **cmapp)
{
	struct vfs_drt_clustermap *cmap = NULL, *ocmap = NULL;
	kern_return_t   kret = KERN_SUCCESS;
	u_int64_t       offset = 0;
	u_int32_t       i = 0;
	int             modulus_size = 0, map_size = 0, active_buckets = 0, index = 0, copycount = 0;

	ocmap = NULL;
	if (cmapp != NULL) {
		ocmap = *cmapp;
	}

	/*
	 * Decide on the size of the new map.
	 */
	if (ocmap == NULL) {
		modulus_size = DRT_HASH_SMALL_MODULUS;
		map_size = DRT_SMALL_ALLOCATION;
	} else {
		/* count the number of active buckets in the old map */
		active_buckets = 0;
		for (i = 0; i < ocmap->scm_modulus; i++) {
			if (!DRT_HASH_VACANT(ocmap, i) &&
			    (DRT_HASH_GET_COUNT(ocmap, i) != 0)) {
				active_buckets++;
			}
		}
		/*
		 * If we're currently using the small allocation, check to
		 * see whether we should grow to the large one.
		 */
		if (ocmap->scm_modulus == DRT_HASH_SMALL_MODULUS) {
			/*
			 * If the ring is nearly full and we are allowed to
			 * use the large modulus, upgrade.
			 */
			if ((active_buckets > (DRT_HASH_SMALL_MODULUS - 5)) &&
			    (max_mem >= DRT_HASH_LARGE_MEMORY_REQUIRED)) {
				modulus_size = DRT_HASH_LARGE_MODULUS;
				map_size = DRT_LARGE_ALLOCATION;
			} else {
				modulus_size = DRT_HASH_SMALL_MODULUS;
				map_size = DRT_SMALL_ALLOCATION;
			}
		} else if (ocmap->scm_modulus == DRT_HASH_LARGE_MODULUS) {
			if ((active_buckets > (DRT_HASH_LARGE_MODULUS - 5)) &&
			    (max_mem >= DRT_HASH_XLARGE_MEMORY_REQUIRED)) {
				modulus_size = DRT_HASH_XLARGE_MODULUS;
				map_size = DRT_XLARGE_ALLOCATION;
			} else {
				/*
				 * If the ring is completely full and we can't
				 * expand, there's nothing useful for us to do.
				 * Behave as though we had compacted into the new
				 * array and return.
				 */
				return KERN_SUCCESS;
			}
		} else {
			/* already using the xlarge modulus */
			modulus_size = DRT_HASH_XLARGE_MODULUS;
			map_size = DRT_XLARGE_ALLOCATION;

			/*
			 * If the ring is completely full, there's
			 * nothing useful for us to do.  Behave as
			 * though we had compacted into the new
			 * array and return.
			 */
			if (active_buckets >= DRT_HASH_XLARGE_MODULUS) {
				return KERN_SUCCESS;
			}
		}
	}

	/*
	 * Allocate and initialise the new map.
	 */

	kret = kmem_alloc(kernel_map, (vm_offset_t *)&cmap, map_size,
	    KMA_DATA, VM_KERN_MEMORY_FILE);
	if (kret != KERN_SUCCESS) {
		return kret;
	}
	cmap->scm_magic = DRT_SCM_MAGIC;
	cmap->scm_modulus = modulus_size;
	cmap->scm_buckets = 0;
	cmap->scm_lastclean = 0;
	cmap->scm_iskips = 0;
	for (i = 0; i < cmap->scm_modulus; i++) {
		DRT_HASH_CLEAR(cmap, i);
		DRT_HASH_VACATE(cmap, i);
		DRT_BITVECTOR_CLEAR(cmap, i);
	}

	/*
	 * If there's an old map, re-hash entries from it into the new map.
	 */
	copycount = 0;
	if (ocmap != NULL) {
		for (i = 0; i < ocmap->scm_modulus; i++) {
			/* skip empty buckets */
			if (DRT_HASH_VACANT(ocmap, i) ||
			    (DRT_HASH_GET_COUNT(ocmap, i) == 0)) {
				continue;
			}
			/* get new index */
			offset = DRT_HASH_GET_ADDRESS(ocmap, i);
			kret = vfs_drt_get_index(&cmap, offset, &index, 1);
			if (kret != KERN_SUCCESS) {
				/* XXX need to bail out gracefully here */
				panic("vfs_drt: new cluster map mysteriously too small");
				index = 0;
			}
			/* copy */
			DRT_HASH_COPY(ocmap, i, cmap, index);
			copycount++;
		}
	}

	/* log what we've done */
	vfs_drt_trace(cmap, DRT_DEBUG_ALLOC, copycount, 0, 0, 0);

	/*
	 * It's important to ensure that *cmapp always points to
	 * a valid map, so we must overwrite it before freeing
	 * the old map.
	 */
	*cmapp = cmap;
	if (ocmap != NULL) {
		/* emit stats into trace buffer */
		vfs_drt_trace(ocmap, DRT_DEBUG_SCMDATA,
		    ocmap->scm_modulus,
		    ocmap->scm_buckets,
		    ocmap->scm_lastclean,
		    ocmap->scm_iskips);

		vfs_drt_free_map(ocmap);
	}
	return KERN_SUCCESS;
}


/*
 * Free a sparse cluster map.
 */
static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap *cmap)
{
	vm_size_t map_size = 0;

	if (cmap->scm_modulus == DRT_HASH_SMALL_MODULUS) {
		map_size = DRT_SMALL_ALLOCATION;
	} else if (cmap->scm_modulus == DRT_HASH_LARGE_MODULUS) {
		map_size = DRT_LARGE_ALLOCATION;
	} else if (cmap->scm_modulus == DRT_HASH_XLARGE_MODULUS) {
		map_size = DRT_XLARGE_ALLOCATION;
	} else {
		panic("vfs_drt_free_map: Invalid modulus %d", cmap->scm_modulus);
	}

	kmem_free(kernel_map, (vm_offset_t)cmap, map_size);
	return KERN_SUCCESS;
}


/*
 * Find the hashtable slot currently occupied by an entry for the supplied offset.
 */
static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap *cmap, u_int64_t offset, int *indexp)
{
	int             index;
	u_int32_t       i;

	offset = DRT_ALIGN_ADDRESS(offset);
	index = DRT_HASH(cmap, offset);

	/* traverse the hashtable */
	for (i = 0; i < cmap->scm_modulus; i++) {
		/*
		 * If the slot is vacant, we can stop.
		 */
		if (DRT_HASH_VACANT(cmap, index)) {
			break;
		}

		/*
		 * If the address matches our offset, we have success.
		 */
		if (DRT_HASH_GET_ADDRESS(cmap, index) == offset) {
			*indexp = index;
			return KERN_SUCCESS;
		}

		/*
		 * Move to the next slot, try again.
		 */
		index = DRT_HASH_NEXT(cmap, index);
	}
	/*
	 * It's not there.
	 */
	return KERN_FAILURE;
}

/*
 * Find the hashtable slot for the supplied offset.  If we haven't allocated
 * one yet, allocate one and populate the address field.  Note that it will
 * not have a nonzero page count and thus will still technically be free, so
 * in the case where we are called to clean pages, the slot will remain free.
 */
static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap **cmapp, u_int64_t offset, int *indexp, int recursed)
{
	struct vfs_drt_clustermap *cmap;
	kern_return_t   kret;
	u_int32_t       index;
	u_int32_t       i;

	cmap = *cmapp;

	/* look for an existing entry */
	kret = vfs_drt_search_index(cmap, offset, indexp);
	if (kret == KERN_SUCCESS) {
		return kret;
	}

	/* need to allocate an entry */
	offset = DRT_ALIGN_ADDRESS(offset);
	index = DRT_HASH(cmap, offset);

	/* scan from the index forwards looking for a vacant slot */
	for (i = 0; i < cmap->scm_modulus; i++) {
		/* slot vacant? */
		if (DRT_HASH_VACANT(cmap, index) || DRT_HASH_GET_COUNT(cmap, index) == 0) {
			cmap->scm_buckets++;
			if (index < cmap->scm_lastclean) {
				cmap->scm_lastclean = index;
			}
			DRT_HASH_SET_ADDRESS(cmap, index, offset);
			DRT_HASH_SET_COUNT(cmap, index, 0);
			DRT_BITVECTOR_CLEAR(cmap, index);
			*indexp = index;
			vfs_drt_trace(cmap, DRT_DEBUG_INSERT, (int)offset, i, 0, 0);
			return KERN_SUCCESS;
		}
		cmap->scm_iskips += i;
		index = DRT_HASH_NEXT(cmap, index);
	}

	/*
	 * We haven't found a vacant slot, so the map is full.  If we're not
	 * already recursed, try reallocating/compacting it.
	 */
	if (recursed) {
		return KERN_FAILURE;
	}
	kret = vfs_drt_alloc_map(cmapp);
	if (kret == KERN_SUCCESS) {
		/* now try to insert again */
		kret = vfs_drt_get_index(cmapp, offset, indexp, 1);
	}
	return kret;
}

/*
 * Implementation of set dirty/clean.
 *
 * In the 'clean' case, not finding a map is OK.
 */
static kern_return_t
vfs_drt_do_mark_pages(
	void            **private,
	u_int64_t       offset,
	u_int           length,
	u_int           *setcountp,
	int             dirty)
{
	struct vfs_drt_clustermap *cmap, **cmapp;
	kern_return_t   kret;
	int             i, index, pgoff, pgcount, setcount, ecount;

	cmapp = (struct vfs_drt_clustermap **)private;
	cmap = *cmapp;

	vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_START, (int)offset, (int)length, dirty, 0);

	if (setcountp != NULL) {
		*setcountp = 0;
	}

	/* allocate a cluster map if we don't already have one */
	if (cmap == NULL) {
		/* no cluster map, nothing to clean */
		if (!dirty) {
			vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 1, 0, 0, 0);
			return KERN_SUCCESS;
		}
		kret = vfs_drt_alloc_map(cmapp);
		if (kret != KERN_SUCCESS) {
			vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 2, 0, 0, 0);
			return kret;
		}
	}
	setcount = 0;

	/*
	 * Iterate over the length of the region.
	 */
	while (length > 0) {
		/*
		 * Get the hashtable index for this offset.
		 *
		 * XXX this will add blank entries if we are clearing a range
		 * that hasn't been dirtied.
		 */
		kret = vfs_drt_get_index(cmapp, offset, &index, 0);
		cmap = *cmapp;  /* may have changed! */
		/* this may be a partial-success return */
		if (kret != KERN_SUCCESS) {
			if (setcountp != NULL) {
				*setcountp = setcount;
			}
			vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 3, (int)length, 0, 0);

			return kret;
		}

		/*
		 * Work out how many pages we're modifying in this
		 * hashtable entry.
		 */
		pgoff = (int)((offset - DRT_ALIGN_ADDRESS(offset)) / PAGE_SIZE);
		pgcount = min((length / PAGE_SIZE), (DRT_BITVECTOR_PAGES - pgoff));

		/*
		 * Iterate over pages, dirty/clearing as we go.
		 */
		ecount = DRT_HASH_GET_COUNT(cmap, index);
		for (i = 0; i < pgcount; i++) {
			if (dirty) {
				if (!DRT_HASH_TEST_BIT(cmap, index, pgoff + i)) {
					if (ecount >= DRT_BITVECTOR_PAGES) {
						panic("ecount >= DRT_BITVECTOR_PAGES, cmap = %p, index = %d, bit = %d", cmap, index, pgoff + i);
					}
					DRT_HASH_SET_BIT(cmap, index, pgoff + i);
					ecount++;
					setcount++;
				}
			} else {
				if (DRT_HASH_TEST_BIT(cmap, index, pgoff + i)) {
					if (ecount <= 0) {
						panic("ecount <= 0, cmap = %p, index = %d, bit = %d", cmap, index, pgoff + i);
					}
					assert(ecount > 0);
					DRT_HASH_CLEAR_BIT(cmap, index, pgoff + i);
					ecount--;
					setcount++;
				}
			}
		}
		DRT_HASH_SET_COUNT(cmap, index, ecount);

		offset += pgcount * PAGE_SIZE;
		length -= pgcount * PAGE_SIZE;
	}
	if (setcountp != NULL) {
		*setcountp = setcount;
	}

	vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 0, setcount, 0, 0);

	return KERN_SUCCESS;
}

/*
 * Mark a set of pages as dirty/clean.
 *
 * This is a public interface.
 *
 * cmapp
 *	Pointer to storage suitable for holding a pointer.  Note that
 *	this must either be NULL or a value set by this function.
 *
 * size
 *	Current file size in bytes.
 *
 * offset
 *	Offset of the first page to be marked as dirty, in bytes.  Must be
 *	page-aligned.
 *
 * length
 *	Length of dirty region, in bytes.  Must be a multiple of PAGE_SIZE.
 *
 * setcountp
 *	Number of pages newly marked dirty by this call (optional).
 *
 * Returns KERN_SUCCESS if all the pages were successfully marked.
 */
static kern_return_t
vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, u_int *setcountp)
{
	/* XXX size unused, drop from interface */
	return vfs_drt_do_mark_pages(cmapp, offset, length, setcountp, 1);
}

#if 0
static kern_return_t
vfs_drt_unmark_pages(void **cmapp, off_t offset, u_int length)
{
	return vfs_drt_do_mark_pages(cmapp, offset, length, NULL, 0);
}
#endif

/*
 * Get a cluster of dirty pages.
 *
 * This is a public interface.
 *
 * cmapp
 *	Pointer to storage managed by drt_mark_pages.  Note that this must
 *	be NULL or a value set by drt_mark_pages.
 *
 * offsetp
 *	Returns the byte offset into the file of the first page in the cluster.
 *
 * lengthp
 *	Returns the length in bytes of the cluster of dirty pages.
 *
 * Returns success if a cluster was found.  If KERN_FAILURE is returned, there
 * are no dirty pages meeting the minmum size criteria.  Private storage will
 * be released if there are no more dirty pages left in the map
 *
 */
static kern_return_t
vfs_drt_get_cluster(void **cmapp, off_t *offsetp, u_int *lengthp)
{
	struct vfs_drt_clustermap *cmap;
	u_int64_t       offset;
	u_int           length;
	u_int32_t       j;
	int             index, i, fs, ls;

	/* sanity */
	if ((cmapp == NULL) || (*cmapp == NULL)) {
		return KERN_FAILURE;
	}
	cmap = *cmapp;

	/* walk the hashtable */
	for (offset = 0, j = 0; j < cmap->scm_modulus; offset += (DRT_BITVECTOR_PAGES * PAGE_SIZE), j++) {
		index = DRT_HASH(cmap, offset);

		if (DRT_HASH_VACANT(cmap, index) || (DRT_HASH_GET_COUNT(cmap, index) == 0)) {
			continue;
		}

		/* scan the bitfield for a string of bits */
		fs = -1;

		for (i = 0; i < DRT_BITVECTOR_PAGES; i++) {
			if (DRT_HASH_TEST_BIT(cmap, index, i)) {
				fs = i;
				break;
			}
		}
		if (fs == -1) {
			/*  didn't find any bits set */
			panic("vfs_drt: entry summary count > 0 but no bits set in map, cmap = %p, index = %d, count = %lld",
			    cmap, index, DRT_HASH_GET_COUNT(cmap, index));
		}
		for (ls = 0; i < DRT_BITVECTOR_PAGES; i++, ls++) {
			if (!DRT_HASH_TEST_BIT(cmap, index, i)) {
				break;
			}
		}

		/* compute offset and length, mark pages clean */
		offset = DRT_HASH_GET_ADDRESS(cmap, index) + (PAGE_SIZE * fs);
		length = ls * PAGE_SIZE;
		vfs_drt_do_mark_pages(cmapp, offset, length, NULL, 0);
		cmap->scm_lastclean = index;

		/* return successful */
		*offsetp = (off_t)offset;
		*lengthp = length;

		vfs_drt_trace(cmap, DRT_DEBUG_RETCLUSTER, (int)offset, (int)length, 0, 0);
		return KERN_SUCCESS;
	}
	/*
	 * We didn't find anything... hashtable is empty
	 * emit stats into trace buffer and
	 * then free it
	 */
	vfs_drt_trace(cmap, DRT_DEBUG_SCMDATA,
	    cmap->scm_modulus,
	    cmap->scm_buckets,
	    cmap->scm_lastclean,
	    cmap->scm_iskips);

	vfs_drt_free_map(cmap);
	*cmapp = NULL;

	return KERN_FAILURE;
}


static kern_return_t
vfs_drt_control(void **cmapp, int op_type)
{
	struct vfs_drt_clustermap *cmap;

	/* sanity */
	if ((cmapp == NULL) || (*cmapp == NULL)) {
		return KERN_FAILURE;
	}
	cmap = *cmapp;

	switch (op_type) {
	case 0:
		/* emit stats into trace buffer */
		vfs_drt_trace(cmap, DRT_DEBUG_SCMDATA,
		    cmap->scm_modulus,
		    cmap->scm_buckets,
		    cmap->scm_lastclean,
		    cmap->scm_iskips);

		vfs_drt_free_map(cmap);
		*cmapp = NULL;
		break;

	case 1:
		cmap->scm_lastclean = 0;
		break;
	}
	return KERN_SUCCESS;
}



/*
 * Emit a summary of the state of the clustermap into the trace buffer
 * along with some caller-provided data.
 */
#if KDEBUG
static void
vfs_drt_trace(__unused struct vfs_drt_clustermap *cmap, int code, int arg1, int arg2, int arg3, int arg4)
{
	KERNEL_DEBUG(code, arg1, arg2, arg3, arg4, 0);
}
#else
static void
vfs_drt_trace(__unused struct vfs_drt_clustermap *cmap, __unused int code,
    __unused int arg1, __unused int arg2, __unused int arg3,
    __unused int arg4)
{
}
#endif

#if 0
/*
 * Perform basic sanity check on the hash entry summary count
 * vs. the actual bits set in the entry.
 */
static void
vfs_drt_sanity(struct vfs_drt_clustermap *cmap)
{
	int index, i;
	int bits_on;

	for (index = 0; index < cmap->scm_modulus; index++) {
		if (DRT_HASH_VACANT(cmap, index)) {
			continue;
		}

		for (bits_on = 0, i = 0; i < DRT_BITVECTOR_PAGES; i++) {
			if (DRT_HASH_TEST_BIT(cmap, index, i)) {
				bits_on++;
			}
		}
		if (bits_on != DRT_HASH_GET_COUNT(cmap, index)) {
			panic("bits_on = %d,  index = %d", bits_on, index);
		}
	}
}
#endif

/*
 * Internal interface only.
 */
static kern_return_t
vfs_get_scmap_push_behavior_internal(void **cmapp, int *push_flag)
{
	struct vfs_drt_clustermap *cmap;

	/* sanity */
	if ((cmapp == NULL) || (*cmapp == NULL) || (push_flag == NULL)) {
		return KERN_FAILURE;
	}
	cmap = *cmapp;

	if (cmap->scm_modulus == DRT_HASH_XLARGE_MODULUS) {
		/*
		 * If we have a full xlarge sparse cluster,
		 * we push it out all at once so the cluster
		 * map can be available to absorb more I/Os.
		 * This is done on large memory configs so
		 * the small I/Os don't interfere with the
		 * pro workloads.
		 */
		*push_flag = PUSH_ALL;
	}
	return KERN_SUCCESS;
}