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
////////////////////////////////////////////////////////////////////
// Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
// All rights reserved
// This file was released under the GPLv2 on June 2015.
////////////////////////////////////////////////////////////////////
/*

 File: Misc.cpp

 Module: UDF File System Driver (Kernel mode execution only)

 Description:
   This file contains some miscellaneous support routines.

*/

#include            "udffs.h"
// define the file specific bug-check id
#define         UDF_BUG_CHECK_ID                UDF_FILE_MISC

#include            <stdio.h>

//CCHAR   DefLetter[] = {""};

/*

 Function: UDFInitializeZones()

 Description:
   Allocates some memory for global zones used to allocate FSD structures.
   Either all memory will be allocated or we will back out gracefully.

 Expected Interrupt Level (for execution) :

  IRQL_PASSIVE_LEVEL

 Return Value: STATUS_SUCCESS/Error

*/
NTSTATUS
UDFInitializeZones(VOID)
{
    NTSTATUS            RC = STATUS_SUCCESS;
    uint32              SizeOfZone = UDFGlobalData.DefaultZoneSizeInNumStructs;
    uint32              SizeOfObjectNameZone = 0;
    uint32              SizeOfCCBZone = 0;
//    uint32              SizeOfFCBZone = 0;
    uint32              SizeOfIrpContextZone = 0;
//    uint32              SizeOfFileInfoZone = 0;

    _SEH2_TRY {

        // initialize the spinlock protecting the zones
        KeInitializeSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock));

        // determine memory requirements
        switch (MmQuerySystemSize()) {
        case MmMediumSystem:
            SizeOfObjectNameZone = (4 * SizeOfZone * UDFQuadAlign(sizeof(UDFObjectName))) + sizeof(ZONE_SEGMENT_HEADER);
            SizeOfCCBZone = (4 * SizeOfZone * UDFQuadAlign(sizeof(UDFCCB))) + sizeof(ZONE_SEGMENT_HEADER);
            SizeOfIrpContextZone = (4 * SizeOfZone * UDFQuadAlign(sizeof(UDFIrpContext))) + sizeof(ZONE_SEGMENT_HEADER);
            UDFGlobalData.MaxDelayedCloseCount = 24;
            UDFGlobalData.MinDelayedCloseCount = 6;
            UDFGlobalData.MaxDirDelayedCloseCount = 8;
            UDFGlobalData.MinDirDelayedCloseCount = 2;
            UDFGlobalData.WCacheMaxFrames = 8*4;
            UDFGlobalData.WCacheMaxBlocks = 16*64;
            UDFGlobalData.WCacheBlocksPerFrameSh = 8;
            UDFGlobalData.WCacheFramesToKeepFree = 4;
            break;
        case MmLargeSystem:
            SizeOfObjectNameZone = (8 * SizeOfZone * UDFQuadAlign(sizeof(UDFObjectName))) + sizeof(ZONE_SEGMENT_HEADER);
            SizeOfCCBZone = (8 * SizeOfZone * UDFQuadAlign(sizeof(UDFCCB))) + sizeof(ZONE_SEGMENT_HEADER);
            SizeOfIrpContextZone = (8 * SizeOfZone * UDFQuadAlign(sizeof(UDFIrpContext))) + sizeof(ZONE_SEGMENT_HEADER);
            UDFGlobalData.MaxDelayedCloseCount = 72;
            UDFGlobalData.MinDelayedCloseCount = 18;
            UDFGlobalData.MaxDirDelayedCloseCount = 24;
            UDFGlobalData.MinDirDelayedCloseCount = 6;
            UDFGlobalData.WCacheMaxFrames = 2*16*4;
            UDFGlobalData.WCacheMaxBlocks = 2*16*64;
            UDFGlobalData.WCacheBlocksPerFrameSh = 8;
            UDFGlobalData.WCacheFramesToKeepFree = 8;
            break;
        case MmSmallSystem:
        default:
            SizeOfObjectNameZone = (2 * SizeOfZone * UDFQuadAlign(sizeof(UDFObjectName))) + sizeof(ZONE_SEGMENT_HEADER);
            SizeOfCCBZone = (2 * SizeOfZone * UDFQuadAlign(sizeof(UDFCCB))) + sizeof(ZONE_SEGMENT_HEADER);
            SizeOfIrpContextZone = (2 * SizeOfZone * UDFQuadAlign(sizeof(UDFIrpContext))) + sizeof(ZONE_SEGMENT_HEADER);
            UDFGlobalData.MaxDelayedCloseCount = 8;
            UDFGlobalData.MinDelayedCloseCount = 2;
            UDFGlobalData.MaxDirDelayedCloseCount = 6;
            UDFGlobalData.MinDirDelayedCloseCount = 1;
            UDFGlobalData.WCacheMaxFrames = 8*4/2;
            UDFGlobalData.WCacheMaxBlocks = 16*64/2;
            UDFGlobalData.WCacheBlocksPerFrameSh = 8;
            UDFGlobalData.WCacheFramesToKeepFree = 2;
        }

        // typical NT methodology (at least until *someone* exposed the "difference" between a server and workstation ;-)
        if (MmIsThisAnNtAsSystem()) {
            SizeOfObjectNameZone *= UDF_NTAS_MULTIPLE;
            SizeOfCCBZone *= UDF_NTAS_MULTIPLE;
            SizeOfIrpContextZone *= UDF_NTAS_MULTIPLE;
        }

        // allocate memory for each of the zones and initialize the zones ...
        if (!(UDFGlobalData.ObjectNameZone = DbgAllocatePool(NonPagedPool, SizeOfObjectNameZone))) {
            RC = STATUS_INSUFFICIENT_RESOURCES;
            try_return(RC);<--- Unused variable value 'RC'
        }

        if (!(UDFGlobalData.CCBZone = DbgAllocatePool(NonPagedPool, SizeOfCCBZone))) {
            RC = STATUS_INSUFFICIENT_RESOURCES;
            try_return(RC);<--- Unused variable value 'RC'
        }

        if (!(UDFGlobalData.IrpContextZone = DbgAllocatePool(NonPagedPool, SizeOfIrpContextZone))) {
            RC = STATUS_INSUFFICIENT_RESOURCES;
            try_return(RC);<--- Unused variable value 'RC'
        }

        // initialize each of the zone headers ...
        if (!NT_SUCCESS(RC = ExInitializeZone(&(UDFGlobalData.ObjectNameZoneHeader),
                    UDFQuadAlign(sizeof(UDFObjectName)),
                    UDFGlobalData.ObjectNameZone, SizeOfObjectNameZone))) {
            // failed the initialization, leave ...
            try_return(RC);<--- Unused variable value 'RC'
        }

        if (!NT_SUCCESS(RC = ExInitializeZone(&(UDFGlobalData.CCBZoneHeader),
                    UDFQuadAlign(sizeof(UDFCCB)),
                    UDFGlobalData.CCBZone,
                    SizeOfCCBZone))) {
            // failed the initialization, leave ...
            try_return(RC);<--- Unused variable value 'RC'
        }

        if (!NT_SUCCESS(RC = ExInitializeZone(&(UDFGlobalData.IrpContextZoneHeader),
                    UDFQuadAlign(sizeof(UDFIrpContext)),
                    UDFGlobalData.IrpContextZone,
                    SizeOfIrpContextZone))) {
            // failed the initialization, leave ...
            try_return(RC);<--- Unused variable value 'RC'
        }

try_exit:   NOTHING;

    } _SEH2_FINALLY {
        if (!NT_SUCCESS(RC)) {
            // invoke the destroy routine now ...
            UDFDestroyZones();
        } else {
            // mark the fact that we have allocated zones ...
            UDFSetFlag(UDFGlobalData.UDFFlags, UDF_DATA_FLAGS_ZONES_INITIALIZED);
        }
    } _SEH2_END;

    return(RC);
}


/*************************************************************************
*
* Function: UDFDestroyZones()
*
* Description:
*   Free up the previously allocated memory. NEVER do this once the
*   driver has been successfully loaded.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
VOID UDFDestroyZones(VOID)
{
//    BrutePoint();

    _SEH2_TRY {
        // free up each of the pools
        if(UDFGlobalData.ObjectNameZone) {
            DbgFreePool(UDFGlobalData.ObjectNameZone);
            UDFGlobalData.ObjectNameZone = NULL;
        }
        if(UDFGlobalData.CCBZone) {
            DbgFreePool(UDFGlobalData.CCBZone);
            UDFGlobalData.CCBZone = NULL;
        }
        if(UDFGlobalData.IrpContextZone) {
            DbgFreePool(UDFGlobalData.IrpContextZone);
            UDFGlobalData.IrpContextZone = NULL;
        }

//try_exit: NOTHING;

    } _SEH2_FINALLY {
        UDFGlobalData.UDFFlags &= ~UDF_DATA_FLAGS_ZONES_INITIALIZED;
    } _SEH2_END;

    return;
}


/*************************************************************************
*
* Function: UDFIsIrpTopLevel()
*
* Description:
*   Helps the FSD determine who the "top level" caller is for this
*   request. A request can originate directly from a user process
*   (in which case, the "top level" will be NULL when this routine
*   is invoked), OR the user may have originated either from the NT
*   Cache Manager/VMM ("top level" may be set), or this could be a
*   recursion into our code in which we would have set the "top level"
*   field the last time around.
*
* Expected Interrupt Level (for execution) :
*
*  whatever level a particular dispatch routine is invoked at.
*
* Return Value: TRUE/FALSE (TRUE if top level was NULL when routine invoked)
*
*************************************************************************/
BOOLEAN
__fastcall
UDFIsIrpTopLevel(
    PIRP            Irp)            // the IRP sent to our dispatch routine
{
    if(!IoGetTopLevelIrp()) {
        // OK, so we can set ourselves to become the "top level" component
        IoSetTopLevelIrp(Irp);
        return TRUE;
    }
    return FALSE;
}


/*************************************************************************
*
* Function: UDFExceptionFilter()
*
* Description:
*   This routines allows the driver to determine whether the exception
*   is an "allowed" exception i.e. one we should not-so-quietly consume
*   ourselves, or one which should be propagated onwards in which case
*   we will most likely bring down the machine.
*
*   This routine employs the services of FsRtlIsNtstatusExpected(). This
*   routine returns a BOOLEAN result. A RC of FALSE will cause us to return
*   EXCEPTION_CONTINUE_SEARCH which will probably cause a panic.
*   The FsRtl.. routine returns FALSE iff exception values are (currently) :
*       STATUS_DATATYPE_MISALIGNMENT    ||  STATUS_ACCESS_VIOLATION ||
*       STATUS_ILLEGAL_INSTRUCTION  ||  STATUS_INSTRUCTION_MISALIGNMENT
*
* Expected Interrupt Level (for execution) :
*
*  ?
*
* Return Value: EXCEPTION_EXECUTE_HANDLER/EXECEPTION_CONTINUE_SEARCH
*
*************************************************************************/
long
UDFExceptionFilter(
    PtrUDFIrpContext    PtrIrpContext,
    PEXCEPTION_POINTERS PtrExceptionPointers
    )
{
    long                            ReturnCode = EXCEPTION_EXECUTE_HANDLER;
    NTSTATUS                        ExceptionCode = STATUS_SUCCESS;
#if defined UDF_DBG || defined PRINT_ALWAYS
    ULONG i;

    UDFPrint(("UDFExceptionFilter\n"));
    UDFPrint(("    Ex. Code: %x\n",PtrExceptionPointers->ExceptionRecord->ExceptionCode));
    UDFPrint(("    Ex. Addr: %x\n",PtrExceptionPointers->ExceptionRecord->ExceptionAddress));
    UDFPrint(("    Ex. Flag: %x\n",PtrExceptionPointers->ExceptionRecord->ExceptionFlags));
    UDFPrint(("    Ex. Pnum: %x\n",PtrExceptionPointers->ExceptionRecord->NumberParameters));
    for(i=0;i<PtrExceptionPointers->ExceptionRecord->NumberParameters;i++) {
        UDFPrint(("       %x\n",PtrExceptionPointers->ExceptionRecord->ExceptionInformation[i]));
    }
#ifdef _X86_
    UDFPrint(("Exception context:\n"));
    if(PtrExceptionPointers->ContextRecord->ContextFlags & CONTEXT_INTEGER) {
        UDFPrint(("EAX=%8.8x   ",PtrExceptionPointers->ContextRecord->Eax));
        UDFPrint(("EBX=%8.8x   ",PtrExceptionPointers->ContextRecord->Ebx));
        UDFPrint(("ECX=%8.8x   ",PtrExceptionPointers->ContextRecord->Ecx));
        UDFPrint(("EDX=%8.8x\n",PtrExceptionPointers->ContextRecord->Edx));

        UDFPrint(("ESI=%8.8x   ",PtrExceptionPointers->ContextRecord->Esi));
        UDFPrint(("EDI=%8.8x   ",PtrExceptionPointers->ContextRecord->Edi));
    }
    if(PtrExceptionPointers->ContextRecord->ContextFlags & CONTEXT_CONTROL) {
        UDFPrint(("EBP=%8.8x   ",PtrExceptionPointers->ContextRecord->Esp));
        UDFPrint(("ESP=%8.8x\n",PtrExceptionPointers->ContextRecord->Ebp));

        UDFPrint(("EIP=%8.8x\n",PtrExceptionPointers->ContextRecord->Eip));
    }
//    UDFPrint(("Flags: %s %s    ",PtrExceptionPointers->ContextRecord->Eip));
#endif //_X86_

#endif // UDF_DBG

    // figure out the exception code
    ExceptionCode = PtrExceptionPointers->ExceptionRecord->ExceptionCode;

    if ((ExceptionCode == STATUS_IN_PAGE_ERROR) && (PtrExceptionPointers->ExceptionRecord->NumberParameters >= 3)) {
        ExceptionCode = PtrExceptionPointers->ExceptionRecord->ExceptionInformation[2];
    }

    if (PtrIrpContext) {
        PtrIrpContext->SavedExceptionCode = ExceptionCode;
        UDFSetFlag(PtrIrpContext->IrpContextFlags, UDF_IRP_CONTEXT_EXCEPTION);
    }

    // check if we should propagate this exception or not
    if (!(FsRtlIsNtstatusExpected(ExceptionCode))) {

        // better free up the IrpContext now ...
        if (PtrIrpContext) {
            UDFPrint(("    UDF Driver internal error\n"));
            BrutePoint();
        } else {
            // we are not ok, propagate this exception.
            //  NOTE: we will bring down the machine ...
            ReturnCode = EXCEPTION_CONTINUE_SEARCH;
        }
    }


    // return the appropriate code
    return(ReturnCode);
} // end UDFExceptionFilter()


/*************************************************************************
*
* Function: UDFExceptionHandler()
*
* Description:
*   One of the routines in the FSD or in the modules we invoked encountered
*   an exception. We have decided that we will "handle" the exception.
*   Therefore we will prevent the machine from a panic ...
*   You can do pretty much anything you choose to in your commercial
*   driver at this point to ensure a graceful exit. In the UDF
*   driver, We shall simply free up the IrpContext (if any), set the
*   error code in the IRP and complete the IRP at this time ...
*
* Expected Interrupt Level (for execution) :
*
*  ?
*
* Return Value: Error code
*
*************************************************************************/
NTSTATUS
UDFExceptionHandler(
    PtrUDFIrpContext PtrIrpContext,
    PIRP             Irp
    )
{
//    NTSTATUS                        RC;
    NTSTATUS            ExceptionCode = STATUS_INSUFFICIENT_RESOURCES;
    PDEVICE_OBJECT      Device;
    PVPB Vpb;
    PETHREAD Thread;

    UDFPrint(("UDFExceptionHandler \n"));

//    ASSERT(Irp);

    if (!Irp) {
        UDFPrint(("  !Irp, return\n"));
        ASSERT(!PtrIrpContext);
        return ExceptionCode;
    }
    // If it was a queued close (or something like this) then we need not
    // completing it because of MUST_SUCCEED requirement.

    if (PtrIrpContext) {
        ExceptionCode = PtrIrpContext->SavedExceptionCode;
        // Free irp context here
//        UDFReleaseIrpContext(PtrIrpContext);
    } else {
        UDFPrint(("  complete Irp and return\n"));
        // must be insufficient resources ...?
        ExceptionCode = STATUS_INSUFFICIENT_RESOURCES;
        Irp->IoStatus.Status = ExceptionCode;
        Irp->IoStatus.Information = 0;
        // complete the IRP
        IoCompleteRequest(Irp, IO_NO_INCREMENT);

        return ExceptionCode;
    }

    //  Check if we are posting this request.  One of the following must be true
    //  if we are to post a request.
    //
    //      - Status code is STATUS_CANT_WAIT and the request is asynchronous
    //          or we are forcing this to be posted.
    //
    //      - Status code is STATUS_VERIFY_REQUIRED and we are at APC level
    //          or higher.  Can't wait for IO in the verify path in this case.
    //
    //  Set the MORE_PROCESSING flag in the IrpContext to keep if from being
    //  deleted if this is a retryable condition.

    if (ExceptionCode == STATUS_VERIFY_REQUIRED) {
        if (KeGetCurrentIrql() >= APC_LEVEL) {
            UDFPrint(("  use UDFPostRequest()\n"));
            ExceptionCode = UDFPostRequest( PtrIrpContext, Irp );
        }
    }

    //  If we posted the request or our caller will retry then just return here.
    if ((ExceptionCode == STATUS_PENDING) ||
        (ExceptionCode == STATUS_CANT_WAIT)) {

        UDFPrint(("  STATUS_PENDING/STATUS_CANT_WAIT, return\n"));
        return ExceptionCode;
    }

    //  Store this error into the Irp for posting back to the Io system.
    Irp->IoStatus.Status = ExceptionCode;
    if (IoIsErrorUserInduced( ExceptionCode )) {

        //  Check for the various error conditions that can be caused by,
        //  and possibly resolved my the user.
        if (ExceptionCode == STATUS_VERIFY_REQUIRED) {

            //  Now we are at the top level file system entry point.
            //
            //  If we have already posted this request then the device to
            //  verify is in the original thread.  Find this via the Irp.
            Device = IoGetDeviceToVerify( Irp->Tail.Overlay.Thread );
            IoSetDeviceToVerify( Irp->Tail.Overlay.Thread, NULL );

            //  If there is no device in that location then check in the
            //  current thread.
            if (Device == NULL) {

                Device = IoGetDeviceToVerify( PsGetCurrentThread() );
                IoSetDeviceToVerify( PsGetCurrentThread(), NULL );

                ASSERT( Device != NULL );

                //  Let's not BugCheck just because the driver screwed up.
                if (Device == NULL) {

                    UDFPrint(("  Device == NULL, return\n"));
                    ExceptionCode = STATUS_DRIVER_INTERNAL_ERROR;
                    Irp->IoStatus.Status = ExceptionCode;
                    Irp->IoStatus.Information = 0;
                    // complete the IRP
                    IoCompleteRequest(Irp, IO_NO_INCREMENT);

                    UDFReleaseIrpContext(PtrIrpContext);

                    return ExceptionCode;
                }
            }

            UDFPrint(("  use UDFPerformVerify()\n"));
            //  UDFPerformVerify() will do the right thing with the Irp.
            //  If we return STATUS_CANT_WAIT then the current thread
            //  can retry the request.
            return UDFPerformVerify( PtrIrpContext, Irp, Device );
        }

        //
        //  The other user induced conditions generate an error unless
        //  they have been disabled for this request.
        //

        if (FlagOn( PtrIrpContext->IrpContextFlags, UDF_IRP_CONTEXT_FLAG_DISABLE_POPUPS )) {
  
            UDFPrint(("  DISABLE_POPUPS, complete Irp and return\n"));
            Irp->IoStatus.Status = ExceptionCode;
            Irp->IoStatus.Information = 0;
            // complete the IRP
            IoCompleteRequest(Irp, IO_NO_INCREMENT);

            UDFReleaseIrpContext(PtrIrpContext);
            return ExceptionCode;
        } else {

            //  Generate a pop-up
            if (IoGetCurrentIrpStackLocation( Irp )->FileObject != NULL) {

                Vpb = IoGetCurrentIrpStackLocation( Irp )->FileObject->Vpb;
            } else {

                Vpb = NULL;
            }
            //  The device to verify is either in my thread local storage
            //  or that of the thread that owns the Irp.
            Thread = Irp->Tail.Overlay.Thread;
            Device = IoGetDeviceToVerify( Thread );

            if (Device == NULL) {

                Thread = PsGetCurrentThread();
                Device = IoGetDeviceToVerify( Thread );
                ASSERT( Device != NULL );

                //  Let's not BugCheck just because the driver screwed up.
                if (Device == NULL) {
                    UDFPrint(("  Device == NULL, return(2)\n"));
                    Irp->IoStatus.Status = ExceptionCode;
                    Irp->IoStatus.Information = 0;
                    // complete the IRP
                    IoCompleteRequest(Irp, IO_NO_INCREMENT);

                    UDFReleaseIrpContext(PtrIrpContext);

                    return ExceptionCode;
                }
            }

            //  This routine actually causes the pop-up.  It usually
            //  does this by queuing an APC to the callers thread,
            //  but in some cases it will complete the request immediately,
            //  so it is very important to IoMarkIrpPending() first.
            IoMarkIrpPending( Irp );
            IoRaiseHardError( Irp, Vpb, Device );

            //  We will be handing control back to the caller here, so
            //  reset the saved device object.

            UDFPrint(("  use IoSetDeviceToVerify()\n"));
            IoSetDeviceToVerify( Thread, NULL );
            //  The Irp will be completed by Io or resubmitted.  In either
            //  case we must clean up the IrpContext here.

            UDFReleaseIrpContext(PtrIrpContext);
            return STATUS_PENDING;
        }
    }

    // If it was a normal request from IOManager then complete it
    if (Irp) {
        UDFPrint(("  complete Irp\n"));
        // set the error code in the IRP
        Irp->IoStatus.Status = ExceptionCode;
        Irp->IoStatus.Information = 0;
    
        // complete the IRP
        IoCompleteRequest(Irp, IO_NO_INCREMENT);

        UDFReleaseIrpContext(PtrIrpContext);
    }

    UDFPrint(("  return from exception handler with code %x\n", ExceptionCode));
    return(ExceptionCode);
} // end UDFExceptionHandler()

/*************************************************************************
*
* Function: UDFLogEvent()
*
* Description:
*   Log a message in the NT Event Log. This is a rather simplistic log
*   methodology since we can potentially utilize the event log to
*   provide a lot of information to the user (and you should too!)
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
VOID
UDFLogEvent(
    NTSTATUS UDFEventLogId,      // the UDF private message id
    NTSTATUS RC)                 // any NT error code we wish to log ...
{
    _SEH2_TRY {

        // Implement a call to IoAllocateErrorLogEntry() followed by a call
        // to IoWriteErrorLogEntry(). You should note that the call to IoWriteErrorLogEntry()
        // will free memory for the entry once the write completes (which in actuality
        // is an asynchronous operation).

    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        // nothing really we can do here, just do not wish to crash ...
        NOTHING;
    } _SEH2_END;

    return;
} // end UDFLogEvent()


/*************************************************************************
*
* Function: UDFAllocateObjectName()
*
* Description:
*   Allocate a new ObjectName structure to represent an open on-disk object.
*   Also initialize the ObjectName structure to NULL.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: A pointer to the ObjectName structure OR NULL.
*
*************************************************************************/
PtrUDFObjectName
UDFAllocateObjectName(VOID)
{
    PtrUDFObjectName            PtrObjectName = NULL;
    BOOLEAN                     AllocatedFromZone = TRUE;
    KIRQL                       CurrentIrql;

    // first, __try to allocate out of the zone
    KeAcquireSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), &CurrentIrql);
    if (!ExIsFullZone(&(UDFGlobalData.ObjectNameZoneHeader))) {
        // we have enough memory
        PtrObjectName = (PtrUDFObjectName)ExAllocateFromZone(&(UDFGlobalData.ObjectNameZoneHeader));

        // release the spinlock
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);
    } else {
        // release the spinlock
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);

        // if we failed to obtain from the zone, get it directly from the VMM
        PtrObjectName = (PtrUDFObjectName)MyAllocatePool__(NonPagedPool, UDFQuadAlign(sizeof(UDFObjectName)));
        AllocatedFromZone = FALSE;
    }

    if (!PtrObjectName) {
        return NULL;
    }

    // zero out the allocated memory block
    RtlZeroMemory(PtrObjectName, UDFQuadAlign(sizeof(UDFObjectName)));

    // set up some fields ...
    PtrObjectName->NodeIdentifier.NodeType  = UDF_NODE_TYPE_OBJECT_NAME;
    PtrObjectName->NodeIdentifier.NodeSize  = UDFQuadAlign(sizeof(UDFObjectName));


    if (!AllocatedFromZone) {
        UDFSetFlag(PtrObjectName->ObjectNameFlags, UDF_OBJ_NAME_NOT_FROM_ZONE);
    }

    return(PtrObjectName);
} // end UDFAllocateObjectName()


/*************************************************************************
*
* Function: UDFReleaseObjectName()
*
* Description:
*   Deallocate a previously allocated structure.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
VOID
__fastcall 
UDFReleaseObjectName(
    PtrUDFObjectName PtrObjectName)
{
    KIRQL            CurrentIrql;

    ASSERT(PtrObjectName);

    // give back memory either to the zone or to the VMM
    if (!(PtrObjectName->ObjectNameFlags & UDF_OBJ_NAME_NOT_FROM_ZONE)) {
        // back to the zone
        KeAcquireSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), &CurrentIrql);
        ExFreeToZone(&(UDFGlobalData.ObjectNameZoneHeader), PtrObjectName);
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);
    } else {
        MyFreePool__(PtrObjectName);
    }

    return;
} // end UDFReleaseObjectName()


/*************************************************************************
*
* Function: UDFAllocateCCB()
*
* Description:
*   Allocate a new CCB structure to represent an open on-disk object.
*   Also initialize the CCB structure to NULL.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: A pointer to the CCB structure OR NULL.
*
*************************************************************************/
PtrUDFCCB
UDFAllocateCCB(VOID)
{
    PtrUDFCCB                   Ccb = NULL;
    BOOLEAN                     AllocatedFromZone = TRUE;
    KIRQL                       CurrentIrql;

    // first, __try to allocate out of the zone
    KeAcquireSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), &CurrentIrql);
    if (!ExIsFullZone(&(UDFGlobalData.CCBZoneHeader))) {
        // we have enough memory
        Ccb = (PtrUDFCCB)ExAllocateFromZone(&(UDFGlobalData.CCBZoneHeader));

        // release the spinlock
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);
    } else {
        // release the spinlock
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);

        // if we failed to obtain from the zone, get it directly from the VMM
        Ccb = (PtrUDFCCB)MyAllocatePool__(NonPagedPool, UDFQuadAlign(sizeof(UDFCCB)));
        AllocatedFromZone = FALSE;
//        UDFPrint(("    CCB allocated @%x\n",Ccb));
    }

    if (!Ccb) {
        return NULL;
    }

    // zero out the allocated memory block
    RtlZeroMemory(Ccb, UDFQuadAlign(sizeof(UDFCCB)));

    // set up some fields ...
    Ccb->NodeIdentifier.NodeType = UDF_NODE_TYPE_CCB;
    Ccb->NodeIdentifier.NodeSize = UDFQuadAlign(sizeof(UDFCCB));


    if (!AllocatedFromZone) {
        UDFSetFlag(Ccb->CCBFlags, UDF_CCB_NOT_FROM_ZONE);
    }

    UDFPrint(("UDFAllocateCCB: %x\n", Ccb));
    return(Ccb);
} // end UDFAllocateCCB()


/*************************************************************************
*
* Function: UDFReleaseCCB()
*
* Description:
*   Deallocate a previously allocated structure.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
VOID
__fastcall 
UDFReleaseCCB(
    PtrUDFCCB Ccb
    )
{
    KIRQL   CurrentIrql;

    ASSERT(Ccb);

    UDFPrint(("UDFReleaseCCB: %x\n", Ccb));
    // give back memory either to the zone or to the VMM
    if(!(Ccb->CCBFlags & UDF_CCB_NOT_FROM_ZONE)) {
        // back to the zone
        KeAcquireSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), &CurrentIrql);
        ExFreeToZone(&(UDFGlobalData.CCBZoneHeader), Ccb);
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);
    } else {
        MyFreePool__(Ccb);
    }

    return;
} // end UDFReleaseCCB()

/*
  Function: UDFCleanupCCB()                      
                                                 
  Description:                                   
    Cleanup and deallocate a previously allocated structure. 
                                                 
  Expected Interrupt Level (for execution) :     
                                                 
   IRQL_PASSIVE_LEVEL                            
                                                 
  Return Value: None                             

*/
VOID
__fastcall 
UDFCleanUpCCB(
    PtrUDFCCB Ccb)
{
//    ASSERT(Ccb);
    if(!Ccb) return; // probably, we havn't allocated it...
    ASSERT(Ccb->NodeIdentifier.NodeType == UDF_NODE_TYPE_CCB);

    _SEH2_TRY {
        if(Ccb->Fcb) {
            UDFTouch(&(Ccb->Fcb->CcbListResource));
            UDFAcquireResourceExclusive(&(Ccb->Fcb->CcbListResource),TRUE);
            RemoveEntryList(&(Ccb->NextCCB));
            UDFReleaseResource(&(Ccb->Fcb->CcbListResource));
        } else {
            BrutePoint();
        }

        if (Ccb->DirectorySearchPattern) {
            if (Ccb->DirectorySearchPattern->Buffer) {
                MyFreePool__(Ccb->DirectorySearchPattern->Buffer);
                Ccb->DirectorySearchPattern->Buffer = NULL;
            }

            MyFreePool__(Ccb->DirectorySearchPattern);
            Ccb->DirectorySearchPattern = NULL;
        }

        UDFReleaseCCB(Ccb);
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;
} // end UDFCleanUpCCB()

/*************************************************************************
*
* Function: UDFAllocateFCB()
*
* Description:
*   Allocate a new FCB structure to represent an open on-disk object.
*   Also initialize the FCB structure to NULL.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: A pointer to the FCB structure OR NULL.
*
*************************************************************************/
PtrUDFFCB
UDFAllocateFCB(VOID)
{
    PtrUDFFCB                   Fcb = NULL;

    Fcb = (PtrUDFFCB)MyAllocatePool__(UDF_FCB_MT, UDFQuadAlign(sizeof(UDFFCB)));

    if (!Fcb) {
        return NULL;
    }

    // zero out the allocated memory block
    RtlZeroMemory(Fcb, UDFQuadAlign(sizeof(UDFFCB)));

    // set up some fields ...
    Fcb->NodeIdentifier.NodeType = UDF_NODE_TYPE_FCB;
    Fcb->NodeIdentifier.NodeSize = UDFQuadAlign(sizeof(UDFFCB));

    UDFPrint(("UDFAllocateFCB: %x\n", Fcb));
    return(Fcb);
} // end UDFAllocateFCB()


/*************************************************************************
*
* Function: UDFReleaseFCB()
*
* Description:
*   Deallocate a previously allocated structure.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
/*VOID
UDFReleaseFCB(
    PtrUDFFCB Fcb
    )
{
    ASSERT(Fcb);

    MyFreePool__(Fcb);

    return;
}*/

/*************************************************************************
*
*
*************************************************************************/
VOID
__fastcall 
UDFCleanUpFCB(
    PtrUDFFCB Fcb
    )
{
    UDFPrint(("UDFCleanUpFCB: %x\n", Fcb));
    if(!Fcb) return;

    ASSERT(Fcb->NodeIdentifier.NodeType == UDF_NODE_TYPE_FCB);

    _SEH2_TRY {
        // Deinitialize FCBName field
        if (Fcb->FCBName) {
            if(Fcb->FCBName->ObjectName.Buffer) {
                MyFreePool__(Fcb->FCBName->ObjectName.Buffer);
                Fcb->FCBName->ObjectName.Buffer = NULL;
#ifdef UDF_DBG
                Fcb->FCBName->ObjectName.Length =
                Fcb->FCBName->ObjectName.MaximumLength = 0;
#endif
            }
#ifdef UDF_DBG
            else {
                UDFPrint(("UDF: Fcb has invalid FCBName Buffer\n"));
                BrutePoint();
            }
#endif
            UDFReleaseObjectName(Fcb->FCBName);
            Fcb->FCBName = NULL;
        }
#ifdef UDF_DBG
        else {
            UDFPrint(("UDF: Fcb has invalid FCBName field\n"));
            BrutePoint();
        }
#endif


        // begin transaction {
        UDFTouch(&(Fcb->Vcb->FcbListResource));
        UDFAcquireResourceExclusive(&(Fcb->Vcb->FcbListResource), TRUE);
        // Remove this FCB from list of all FCB in VCB
        RemoveEntryList(&(Fcb->NextFCB));
        UDFReleaseResource(&(Fcb->Vcb->FcbListResource));
        // } end transaction

        if(Fcb->FCBFlags & UDF_FCB_INITIALIZED_CCB_LIST_RESOURCE)
            UDFDeleteResource(&(Fcb->CcbListResource));

        // Free memory
        UDFReleaseFCB(Fcb);
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;
} // end UDFCleanUpFCB()

#ifdef UDF_DBG
ULONG IrpContextCounter = 0;
#endif //UDF_DBG

/*************************************************************************
*
* Function: UDFAllocateIrpContext()
*
* Description:
*   The UDF FSD creates an IRP context for each request received. This
*   routine simply allocates (and initializes to NULL) a UDFIrpContext
*   structure.
*   Most of the fields in the context structure are then initialized here.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: A pointer to the IrpContext structure OR NULL.
*
*************************************************************************/
PtrUDFIrpContext
UDFAllocateIrpContext(
    PIRP           Irp,
    PDEVICE_OBJECT PtrTargetDeviceObject
    )
{
    PtrUDFIrpContext            PtrIrpContext = NULL;
    BOOLEAN                     AllocatedFromZone = TRUE;
    KIRQL                       CurrentIrql;
    PIO_STACK_LOCATION          IrpSp = NULL;

    // first, __try to allocate out of the zone
    KeAcquireSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), &CurrentIrql);
    if (!ExIsFullZone(&(UDFGlobalData.IrpContextZoneHeader))) {
        // we have enough memory
        PtrIrpContext = (PtrUDFIrpContext)ExAllocateFromZone(&(UDFGlobalData.IrpContextZoneHeader));
  
        // release the spinlock
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);
    } else {
        // release the spinlock
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);

        // if we failed to obtain from the zone, get it directly from the VMM
        PtrIrpContext = (PtrUDFIrpContext)MyAllocatePool__(NonPagedPool, UDFQuadAlign(sizeof(UDFIrpContext)));
        AllocatedFromZone = FALSE;
    }

    // if we could not obtain the required memory, bug-check.
    //  Do NOT do this in your commercial driver, instead handle    the error gracefully ...
    if (!PtrIrpContext) {
        return NULL;
    }

#ifdef UDF_DBG
    IrpContextCounter++;
#endif //UDF_DBG

    // zero out the allocated memory block
    RtlZeroMemory(PtrIrpContext, UDFQuadAlign(sizeof(UDFIrpContext)));

    // set up some fields ...
    PtrIrpContext->NodeIdentifier.NodeType  = UDF_NODE_TYPE_IRP_CONTEXT;
    PtrIrpContext->NodeIdentifier.NodeSize  = UDFQuadAlign(sizeof(UDFIrpContext));


    PtrIrpContext->Irp = Irp;
    PtrIrpContext->TargetDeviceObject = PtrTargetDeviceObject;

    // copy over some fields from the IRP and set appropriate flag values
    if (Irp) {
        IrpSp = IoGetCurrentIrpStackLocation(Irp);
        ASSERT(IrpSp);

        PtrIrpContext->MajorFunction = IrpSp->MajorFunction;
        PtrIrpContext->MinorFunction = IrpSp->MinorFunction;

        // Often, a FSD cannot honor a request for asynchronous processing
        // of certain critical requests. For example, a "close" request on
        // a file object can typically never be deferred. Therefore, do not
        // be surprised if sometimes our FSD (just like all other FSD
        // implementations on the Windows NT system) has to override the flag
        // below.
        if (IrpSp->FileObject == NULL) {
            PtrIrpContext->IrpContextFlags |= UDF_IRP_CONTEXT_CAN_BLOCK;
        } else {
            if (IoIsOperationSynchronous(Irp)) {
                PtrIrpContext->IrpContextFlags |= UDF_IRP_CONTEXT_CAN_BLOCK;
            }
        }
    }

    if (!AllocatedFromZone) {
        UDFSetFlag(PtrIrpContext->IrpContextFlags, UDF_IRP_CONTEXT_NOT_FROM_ZONE);
    }

    // Are we top-level ? This information is used by the dispatching code
    // later (and also by the FSD dispatch routine)
    if (IoGetTopLevelIrp() != Irp) {
        // We are not top-level. Note this fact in the context structure
        UDFSetFlag(PtrIrpContext->IrpContextFlags, UDF_IRP_CONTEXT_NOT_TOP_LEVEL);
    }

    return(PtrIrpContext);
} // end UDFAllocateIrpContext()


/*************************************************************************
*
* Function: UDFReleaseIrpContext()
*
* Description:
*   Deallocate a previously allocated structure.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
VOID
UDFReleaseIrpContext(
    PtrUDFIrpContext    PtrIrpContext)
{
    if(!PtrIrpContext) return;
//    ASSERT(PtrIrpContext);

#ifdef UDF_DBG
    IrpContextCounter--;
#endif //UDF_DBG

    // give back memory either to the zone or to the VMM
    if (!(PtrIrpContext->IrpContextFlags & UDF_IRP_CONTEXT_NOT_FROM_ZONE)) {
        // back to the zone
        KIRQL                           CurrentIrql;

        KeAcquireSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), &CurrentIrql);
        ExFreeToZone(&(UDFGlobalData.IrpContextZoneHeader), PtrIrpContext);
        KeReleaseSpinLock(&(UDFGlobalData.ZoneAllocationSpinLock), CurrentIrql);
    } else {
        MyFreePool__(PtrIrpContext);
    }

    return;
} // end UDFReleaseIrpContext()


/*************************************************************************
*
* Function: UDFPostRequest()
*
* Description:
*   Queue up a request for deferred processing (in the context of a system
*   worker thread). The caller must have locked the user buffer (if required)
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: STATUS_PENDING
*
*************************************************************************/
NTSTATUS
UDFPostRequest(
    IN PtrUDFIrpContext PtrIrpContext,
    IN PIRP             Irp
    )
{
    KIRQL SavedIrql;
//    PIO_STACK_LOCATION IrpSp;
    PVCB Vcb;

//    IrpSp = IoGetCurrentIrpStackLocation(Irp);

/*
    if(Vcb->StopOverflowQueue) {
        if(Irp) {
            Irp->IoStatus.Status = STATUS_WRONG_VOLUME;
            Irp->IoStatus.Information = 0;
            IoCompleteRequest(Irp, IO_DISK_INCREMENT);
        }
        UDFReleaseIrpContext(PtrIrpContext);
        return STATUS_WRONG_VOLUME;
    }
*/
    // mark the IRP pending if this is not double post
    if(Irp)
        IoMarkIrpPending(Irp);

    Vcb = (PVCB)(PtrIrpContext->TargetDeviceObject->DeviceExtension);
    KeAcquireSpinLock(&(Vcb->OverflowQueueSpinLock), &SavedIrql);

    if ( Vcb->PostedRequestCount > FSP_PER_DEVICE_THRESHOLD) {

        //  We cannot currently respond to this IRP so we'll just enqueue it
        //  to the overflow queue on the volume.
        //  Note: we just reuse LIST_ITEM field inside WorkQueueItem, this
        //  doesn't matter to regular processing of WorkItems.
        InsertTailList( &(Vcb->OverflowQueue),
                        &(PtrIrpContext->WorkQueueItem.List) );
        Vcb->OverflowQueueCount++;
        KeReleaseSpinLock( &(Vcb->OverflowQueueSpinLock), SavedIrql );

    } else {

        //  We are going to send this Irp to an ex worker thread so up
        //  the count.
        Vcb->PostedRequestCount++;

        KeReleaseSpinLock( &(Vcb->OverflowQueueSpinLock), SavedIrql );

        // queue up the request
        ExInitializeWorkItem(&(PtrIrpContext->WorkQueueItem), UDFCommonDispatch, PtrIrpContext);

        ExQueueWorkItem(&(PtrIrpContext->WorkQueueItem), CriticalWorkQueue);
    //    ExQueueWorkItem(&(PtrIrpContext->WorkQueueItem), DelayedWorkQueue);

    }

    // return status pending
    return STATUS_PENDING;
} // end UDFPostRequest()


/*************************************************************************
*
* Function: UDFCommonDispatch()
*
* Description:
*   The common dispatch routine invoked in the context of a system worker
*   thread. All we do here is pretty much case off the major function
*   code and invoke the appropriate FSD dispatch routine for further
*   processing.
*
* Expected Interrupt Level (for execution) :
*
*   IRQL PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
VOID
NTAPI
UDFCommonDispatch(
    IN PVOID Context   // actually is a pointer to IRPContext structure
    )
{
    NTSTATUS         RC = STATUS_SUCCESS;
    PtrUDFIrpContext PtrIrpContext = NULL;
    PIRP             Irp = NULL;
    PVCB             Vcb;
    KIRQL            SavedIrql;
    PLIST_ENTRY      Entry;
    BOOLEAN          SpinLock = FALSE;

    // The context must be a pointer to an IrpContext structure
    PtrIrpContext = (PtrUDFIrpContext)Context;

    // Assert that the Context is legitimate
    if ( !PtrIrpContext ||
         (PtrIrpContext->NodeIdentifier.NodeType != UDF_NODE_TYPE_IRP_CONTEXT) ||
         (PtrIrpContext->NodeIdentifier.NodeSize != UDFQuadAlign(sizeof(UDFIrpContext))) /*||
        !(PtrIrpContext->Irp)*/) {
        UDFPrint(("    Invalid Context\n"));
        BrutePoint();
        return;
    }

    Vcb = (PVCB)(PtrIrpContext->TargetDeviceObject->DeviceExtension);
    ASSERT(Vcb);

    UDFPrint(("  *** Thr: %x  ThCnt: %x  QCnt: %x  Started!\n", PsGetCurrentThread(), Vcb->PostedRequestCount, Vcb->OverflowQueueCount));

    while(TRUE) {

        UDFPrint(("    Next IRP\n"));
        FsRtlEnterFileSystem();

        //  Get a pointer to the IRP structure
        // in some cases we can get Zero pointer to Irp
        Irp = PtrIrpContext->Irp;
        // Now, check if the FSD was top level when the IRP was originally invoked
        // and set the thread context (for the worker thread) appropriately
        if (PtrIrpContext->IrpContextFlags & UDF_IRP_CONTEXT_NOT_TOP_LEVEL) {
            // The FSD is not top level for the original request
            // Set a constant value in TLS to reflect this fact
            IoSetTopLevelIrp((PIRP)FSRTL_FSP_TOP_LEVEL_IRP);
        } else {
            IoSetTopLevelIrp(Irp);
        }

        // Since the FSD routine will now be invoked in the context of this worker
        // thread, we should inform the FSD that it is perfectly OK to block in
        // the context of this thread
        PtrIrpContext->IrpContextFlags |= UDF_IRP_CONTEXT_CAN_BLOCK;

        _SEH2_TRY {

            // Pre-processing has been completed; check the Major Function code value
            // either in the IrpContext (copied from the IRP), or directly from the
            //  IRP itself (we will need a pointer to the stack location to do that),
            //  Then, switch based on the value on the Major Function code
            UDFPrint(("  *** MJ: %x, Thr: %x\n", PtrIrpContext->MajorFunction, PsGetCurrentThread()));
            switch (PtrIrpContext->MajorFunction) {
            case IRP_MJ_CREATE:
                // Invoke the common create routine
                RC = UDFCommonCreate(PtrIrpContext, Irp);
                break;
            case IRP_MJ_READ:
                // Invoke the common read routine
                RC = UDFCommonRead(PtrIrpContext, Irp);
                break;
#ifndef UDF_READ_ONLY_BUILD
            case IRP_MJ_WRITE:
                // Invoke the common write routine
                RC = UDFCommonWrite(PtrIrpContext, Irp);
                break;
#endif //UDF_READ_ONLY_BUILD
            case IRP_MJ_CLEANUP:
                // Invoke the common cleanup routine
                RC = UDFCommonCleanup(PtrIrpContext, Irp);
                break;
            case IRP_MJ_CLOSE:
                // Invoke the common close routine
                RC = UDFCommonClose(PtrIrpContext, Irp);
                break;
            case IRP_MJ_DIRECTORY_CONTROL:
                // Invoke the common directory control routine
                RC = UDFCommonDirControl(PtrIrpContext, Irp);
                break;
            case IRP_MJ_QUERY_INFORMATION:
#ifndef UDF_READ_ONLY_BUILD
            case IRP_MJ_SET_INFORMATION:
#endif //UDF_READ_ONLY_BUILD
                // Invoke the common query/set information routine
                RC = UDFCommonFileInfo(PtrIrpContext, Irp);
                break;
            case IRP_MJ_QUERY_VOLUME_INFORMATION:
                // Invoke the common query volume routine
                RC = UDFCommonQueryVolInfo(PtrIrpContext, Irp);
                break;
#ifndef UDF_READ_ONLY_BUILD
            case IRP_MJ_SET_VOLUME_INFORMATION:
                // Invoke the common query volume routine
                RC = UDFCommonSetVolInfo(PtrIrpContext, Irp);
                break;
#endif //UDF_READ_ONLY_BUILD
#ifdef UDF_HANDLE_EAS
/*            case IRP_MJ_QUERY_EA:
                // Invoke the common query EAs routine
                RC = UDFCommonGetExtendedAttr(PtrIrpContext, Irp);
                break;
            case IRP_MJ_SET_EA:
                // Invoke the common set EAs routine
                RC = UDFCommonSetExtendedAttr(PtrIrpContext, Irp);
                break;*/
#endif // UDF_HANDLE_EAS
#ifdef UDF_ENABLE_SECURITY
            case IRP_MJ_QUERY_SECURITY:
                // Invoke the common query Security routine
                RC = UDFCommonGetSecurity(PtrIrpContext, Irp);
                break;
#ifndef UDF_READ_ONLY_BUILD
            case IRP_MJ_SET_SECURITY:
                // Invoke the common set Security routine
                RC = UDFCommonSetSecurity(PtrIrpContext, Irp);
                break;
#endif //UDF_READ_ONLY_BUILD
#endif // UDF_ENABLE_SECURITY
            // Continue with the remaining possible dispatch routines below ...
            default:
                UDFPrint(("  unhandled *** MJ: %x, Thr: %x\n", PtrIrpContext->MajorFunction, PsGetCurrentThread()));
                // This is the case where we have an invalid major function
                Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
                Irp->IoStatus.Information = 0;
        
                IoCompleteRequest(Irp, IO_NO_INCREMENT);
                // Free up the Irp Context
                UDFReleaseIrpContext(PtrIrpContext);
                break;
            }

            // Note: PtrIrpContext is invalid here
            UDFPrint(("  *** Thr: %x  Done!\n", PsGetCurrentThread()));

        } _SEH2_EXCEPT(UDFExceptionFilter(PtrIrpContext, _SEH2_GetExceptionInformation())) {

            RC = UDFExceptionHandler(PtrIrpContext, Irp);

            UDFLogEvent(UDF_ERROR_INTERNAL_ERROR, RC);
        }  _SEH2_END;

        // Enable preemption
        FsRtlExitFileSystem();

        // Ensure that the "top-level" field is cleared
        IoSetTopLevelIrp(NULL);

        //  If there are any entries on this volume's overflow queue, service
        //  them.
        if(!Vcb) {
            BrutePoint();
            break;
        }

        KeAcquireSpinLock(&(Vcb->OverflowQueueSpinLock), &SavedIrql);
        SpinLock = TRUE;<--- SpinLock is assigned
        if(!Vcb->OverflowQueueCount)
            break;

        Vcb->OverflowQueueCount--;
        Entry = RemoveHeadList(&Vcb->OverflowQueue);
        KeReleaseSpinLock(&(Vcb->OverflowQueueSpinLock), SavedIrql);
        SpinLock = FALSE;<--- SpinLock is overwritten

        PtrIrpContext = CONTAINING_RECORD( Entry,
                                        UDFIrpContext,
                                        WorkQueueItem.List );
    }

    if(!SpinLock)
        KeAcquireSpinLock(&(Vcb->OverflowQueueSpinLock), &SavedIrql);
    Vcb->PostedRequestCount--;
    KeReleaseSpinLock(&(Vcb->OverflowQueueSpinLock), SavedIrql);

    UDFPrint(("  *** Thr: %x  ThCnt: %x  QCnt: %x  Terminated!\n", PsGetCurrentThread(), Vcb->PostedRequestCount, Vcb->OverflowQueueCount));

    return;
} // end UDFCommonDispatch()


/*************************************************************************
*
* Function: UDFInitializeVCB()
*
* Description:
*   Perform the initialization for a VCB structure.
*
* Expected Interrupt Level (for execution) :
*
*   IRQL PASSIVE_LEVEL
*
* Return Value: status
*
*************************************************************************/
NTSTATUS
UDFInitializeVCB(
    IN PDEVICE_OBJECT PtrVolumeDeviceObject,
    IN PDEVICE_OBJECT PtrTargetDeviceObject,
    IN PVPB           PtrVPB
    )
{
    NTSTATUS RC = STATUS_SUCCESS;
    PVCB     Vcb = NULL;
    SHORT    i;

    BOOLEAN VCBResourceInit     = FALSE;
    BOOLEAN BitMapResource1Init = FALSE;
    BOOLEAN FcbListResourceInit = FALSE;
    BOOLEAN FileIdResourceInit  = FALSE;
    BOOLEAN DlocResourceInit    = FALSE;
    BOOLEAN DlocResource2Init   = FALSE;
    BOOLEAN FlushResourceInit   = FALSE;
    BOOLEAN PreallocResourceInit= FALSE;
    BOOLEAN IoResourceInit      = FALSE;

    Vcb = (PVCB)(PtrVolumeDeviceObject->DeviceExtension);

    _SEH2_TRY {
    // Zero it out (typically this has already been done by the I/O
    // Manager but it does not hurt to do it again)!
    RtlZeroMemory(Vcb, sizeof(VCB));

    // Initialize the signature fields
    Vcb->NodeIdentifier.NodeType = UDF_NODE_TYPE_VCB;
    Vcb->NodeIdentifier.NodeSize = sizeof(VCB);

    // Initialize the ERESOURCE object.
    RC = UDFInitializeResourceLite(&(Vcb->VCBResource));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    VCBResourceInit = TRUE;

    RC = UDFInitializeResourceLite(&(Vcb->BitMapResource1));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    BitMapResource1Init = TRUE;

    RC = UDFInitializeResourceLite(&(Vcb->FcbListResource));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    FcbListResourceInit = TRUE;

    RC = UDFInitializeResourceLite(&(Vcb->FileIdResource));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    FileIdResourceInit = TRUE;

    RC = UDFInitializeResourceLite(&(Vcb->DlocResource));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    DlocResourceInit = TRUE;

    RC = UDFInitializeResourceLite(&(Vcb->DlocResource2));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    DlocResource2Init = TRUE;

    RC = UDFInitializeResourceLite(&(Vcb->FlushResource));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    FlushResourceInit = TRUE;

    RC = UDFInitializeResourceLite(&(Vcb->PreallocResource));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    PreallocResourceInit = TRUE;

    RC = UDFInitializeResourceLite(&(Vcb->IoResource));
    if(!NT_SUCCESS(RC))
        try_return(RC);<--- Unused variable value 'RC'
    IoResourceInit = TRUE;

//    RC = UDFInitializeResourceLite(&(Vcb->DelayedCloseResource));
//    ASSERT(NT_SUCCESS(RC));

    // Allocate buffer for statistics
    Vcb->Statistics = (PFILE_SYSTEM_STATISTICS)MyAllocatePool__(NonPagedPool, sizeof(FILE_SYSTEM_STATISTICS) * KeNumberProcessors );
    if(!Vcb->Statistics)
        try_return(RC = STATUS_INSUFFICIENT_RESOURCES);
    RtlZeroMemory( Vcb->Statistics, sizeof(FILE_SYSTEM_STATISTICS) * KeNumberProcessors );
    for (i=0; i < (KeNumberProcessors); i++) {
        Vcb->Statistics[i].Common.FileSystemType = FILESYSTEM_STATISTICS_TYPE_NTFS;
        Vcb->Statistics[i].Common.Version = 1;
        Vcb->Statistics[i].Common.SizeOfCompleteStructure =
            sizeof(FILE_SYSTEM_STATISTICS);
    }

    // We know the target device object.
    // Note that this is not neccessarily a pointer to the actual
    // physical/virtual device on which the logical volume should
    // be mounted. This is actually a pointer to either the actual
    // (real) device or to any device object that may have been
    // attached to it. Any IRPs that we send down should be sent to this
    // device object. However, the "real" physical/virtual device object
    // on which we perform our mount operation can be determined from the
    // RealDevice field in the VPB sent to us.
    Vcb->TargetDeviceObject = PtrTargetDeviceObject;

    // We also have a pointer to the newly created device object representing
    // this logical volume (remember that this VCB structure is simply an
    // extension of the created device object).
    Vcb->VCBDeviceObject = PtrVolumeDeviceObject;

    // We also have the VPB pointer. This was obtained from the
    // Parameters.MountVolume.Vpb field in the current I/O stack location
    // for the mount IRP.
    Vcb->Vpb = PtrVPB;
    // Target Vcb field in Vcb onto itself. This required for check in
    // open/lock/unlock volume dispatch poits
    Vcb->Vcb=Vcb;

    //  Set the removable media flag based on the real device's
    //  characteristics
    if (PtrVPB->RealDevice->Characteristics & FILE_REMOVABLE_MEDIA) {
        Vcb->VCBFlags |= UDF_VCB_FLAGS_REMOVABLE_MEDIA;
    }

    // Initialize the list anchor (head) for some lists in this VCB.
    InitializeListHead(&(Vcb->NextFCB));
    InitializeListHead(&(Vcb->NextNotifyIRP));
    InitializeListHead(&(Vcb->VolumeOpenListHead));

    //  Initialize the overflow queue for the volume
    Vcb->OverflowQueueCount = 0;
    InitializeListHead(&(Vcb->OverflowQueue));

    Vcb->PostedRequestCount = 0;
    KeInitializeSpinLock(&(Vcb->OverflowQueueSpinLock));

    // Initialize the notify IRP list mutex
    FsRtlNotifyInitializeSync(&(Vcb->NotifyIRPMutex));

    // Intilize NtRequiredFCB for this VCB
    Vcb->NTRequiredFCB = (PtrUDFNTRequiredFCB)MyAllocatePool__(NonPagedPool, UDFQuadAlign(sizeof(UDFNTRequiredFCB)));
    if(!Vcb->NTRequiredFCB)
        try_return(RC = STATUS_INSUFFICIENT_RESOURCES);
    RtlZeroMemory(Vcb->NTRequiredFCB, UDFQuadAlign(sizeof(UDFNTRequiredFCB)));

    // Set the initial file size values appropriately. Note that our FSD may
    // wish to guess at the initial amount of information we would like to
    // read from the disk until we have really determined that this a valid
    // logical volume (on disk) that we wish to mount.
    // Vcb->FileSize = Vcb->AllocationSize = ??

    // We do not want to bother with valid data length callbacks
    // from the Cache Manager for the file stream opened for volume metadata
    // information
    Vcb->NTRequiredFCB->CommonFCBHeader.ValidDataLength.QuadPart = 0x7FFFFFFFFFFFFFFFULL;

    Vcb->VolumeLockPID = -1;

    Vcb->VCBOpenCount = 1;

    Vcb->WCacheMaxBlocks        = UDFGlobalData.WCacheMaxBlocks;
    Vcb->WCacheMaxFrames        = UDFGlobalData.WCacheMaxFrames;
    Vcb->WCacheBlocksPerFrameSh = UDFGlobalData.WCacheBlocksPerFrameSh;
    Vcb->WCacheFramesToKeepFree = UDFGlobalData.WCacheFramesToKeepFree;

    // Create a stream file object for this volume.
    //Vcb->PtrStreamFileObject = IoCreateStreamFileObject(NULL,
    //                                            Vcb->Vpb->RealDevice);
    //ASSERT(Vcb->PtrStreamFileObject);

    // Initialize some important fields in the newly created file object.
    //Vcb->PtrStreamFileObject->FsContext = (PVOID)Vcb;      
    //Vcb->PtrStreamFileObject->FsContext2 = NULL;
    //Vcb->PtrStreamFileObject->SectionObjectPointer = &(Vcb->SectionObject);

    //Vcb->PtrStreamFileObject->Vpb = PtrVPB;

    // Link this chap onto the global linked list of all VCB structures.
    // We consider that GlobalDataResource was acquired in past
    UDFAcquireResourceExclusive(&(UDFGlobalData.GlobalDataResource), TRUE);
    InsertTailList(&(UDFGlobalData.VCBQueue), &(Vcb->NextVCB));

    Vcb->TargetDevName.Buffer = (PWCHAR)MyAllocatePool__(NonPagedPool, sizeof(MOUNTDEV_NAME));
    if(!Vcb->TargetDevName.Buffer)
        try_return(RC = STATUS_INSUFFICIENT_RESOURCES);

    RC = UDFPhSendIOCTL(IOCTL_CDRW_GET_DEVICE_NAME /*IOCTL_MOUNTDEV_QUERY_DEVICE_NAME*/, Vcb->TargetDeviceObject,
                    NULL,0,
                    (PVOID)(Vcb->TargetDevName.Buffer),sizeof(MOUNTDEV_NAME),
                    FALSE, NULL);
    if(!NT_SUCCESS(RC)) {

        if(RC == STATUS_BUFFER_OVERFLOW) {
            if(!MyReallocPool__((PCHAR)(Vcb->TargetDevName.Buffer), sizeof(MOUNTDEV_NAME),
                             (PCHAR*)&(Vcb->TargetDevName.Buffer), Vcb->TargetDevName.Buffer[0]+sizeof(MOUNTDEV_NAME)) ) {
                goto Kill_DevName_buffer;
            }

            RC = UDFPhSendIOCTL(IOCTL_CDRW_GET_DEVICE_NAME /*IOCTL_MOUNTDEV_QUERY_DEVICE_NAME*/, Vcb->TargetDeviceObject,
                            NULL,0,
                            (PVOID)(Vcb->TargetDevName.Buffer), Vcb->TargetDevName.Buffer[0]+sizeof(MOUNTDEV_NAME),
                            FALSE, NULL);
            if(!NT_SUCCESS(RC))
                goto Kill_DevName_buffer;

        } else {
Kill_DevName_buffer:
            if(!MyReallocPool__((PCHAR)Vcb->TargetDevName.Buffer, sizeof(MOUNTDEV_NAME),
                                (PCHAR*)&(Vcb->TargetDevName.Buffer), sizeof(REG_NAMELESS_DEV)))
                try_return(RC = STATUS_INSUFFICIENT_RESOURCES);
            RtlCopyMemory(Vcb->TargetDevName.Buffer, REG_NAMELESS_DEV, sizeof(REG_NAMELESS_DEV));
            Vcb->TargetDevName.Length = sizeof(REG_NAMELESS_DEV)-sizeof(WCHAR);
            Vcb->TargetDevName.MaximumLength = sizeof(REG_NAMELESS_DEV);
            goto read_reg;
        }
    }

    Vcb->TargetDevName.MaximumLength =
    (Vcb->TargetDevName.Length = Vcb->TargetDevName.Buffer[0]) + sizeof(WCHAR);
    RtlMoveMemory((PVOID)(Vcb->TargetDevName.Buffer), (PVOID)(Vcb->TargetDevName.Buffer+1), Vcb->TargetDevName.Buffer[0]);
    Vcb->TargetDevName.Buffer[i = (SHORT)(Vcb->TargetDevName.Length/sizeof(WCHAR))] = 0;

    for(;i>=0;i--) {
        if(Vcb->TargetDevName.Buffer[i] == L'\\') {

            Vcb->TargetDevName.Length -= i*sizeof(WCHAR);
            RtlMoveMemory((PVOID)(Vcb->TargetDevName.Buffer), (PVOID)(Vcb->TargetDevName.Buffer+i), Vcb->TargetDevName.Length);
            Vcb->TargetDevName.Buffer[Vcb->TargetDevName.Length/sizeof(WCHAR)] = 0;
            break;
        }
    }

    UDFPrint(("  TargetDevName: %S\n", Vcb->TargetDevName.Buffer));

    // Initialize caching for the stream file object.
    //CcInitializeCacheMap(Vcb->PtrStreamFileObject, (PCC_FILE_SIZES)(&(Vcb->AllocationSize)),
    //                            TRUE,       // We will use pinned access.
    //                            &(UDFGlobalData.CacheMgrCallBacks), Vcb);

read_reg:

    UDFReleaseResource(&(UDFGlobalData.GlobalDataResource));

    // Mark the fact that this VCB structure is initialized.
    Vcb->VCBFlags |= UDF_VCB_FLAGS_VCB_INITIALIZED;

    RC = STATUS_SUCCESS;

try_exit:   NOTHING;

    } _SEH2_FINALLY {
        
        if(!NT_SUCCESS(RC)) {
            if(Vcb->TargetDevName.Buffer)
                MyFreePool__(Vcb->TargetDevName.Buffer);
            if(Vcb->NTRequiredFCB)
                MyFreePool__(Vcb->NTRequiredFCB);
            if(Vcb->Statistics)
                MyFreePool__(Vcb->Statistics);

            if(VCBResourceInit)
                UDFDeleteResource(&(Vcb->VCBResource));
            if(BitMapResource1Init)
                UDFDeleteResource(&(Vcb->BitMapResource1));
            if(FcbListResourceInit)
                UDFDeleteResource(&(Vcb->FcbListResource));
            if(FileIdResourceInit)
                UDFDeleteResource(&(Vcb->FileIdResource));
            if(DlocResourceInit)
                UDFDeleteResource(&(Vcb->DlocResource));
            if(DlocResource2Init)
                UDFDeleteResource(&(Vcb->DlocResource2));
            if(FlushResourceInit)
                UDFDeleteResource(&(Vcb->FlushResource));
            if(PreallocResourceInit)
                UDFDeleteResource(&(Vcb->PreallocResource));
            if(IoResourceInit)
                UDFDeleteResource(&(Vcb->IoResource));
        }
    } _SEH2_END;

    return RC;
} // end UDFInitializeVCB()

UDFFSD_MEDIA_TYPE
UDFGetMediaClass(
    PVCB Vcb
    )
{
    switch(Vcb->FsDeviceType) {
    case FILE_DEVICE_CD_ROM_FILE_SYSTEM:
        if(Vcb->VCBFlags & (UDF_VCB_FLAGS_VOLUME_READ_ONLY |
                            UDF_VCB_FLAGS_MEDIA_READ_ONLY))
            return MediaCdrom;
        if(Vcb->CDR_Mode)
            return MediaCdr;
        if((Vcb->MediaType >= MediaType_UnknownSize_CDR) &&
           (Vcb->MediaType < MediaType_UnknownSize_CDRW)) {
            return MediaCdr;
        }
        if((Vcb->MediaType >= MediaType_UnknownSize_CDRW) &&
           (Vcb->MediaType < MediaType_UnknownSize_Unknown)) {
            return MediaCdrw;
        }
        if(Vcb->MediaClassEx == CdMediaClass_CDR) {
            return MediaCdr;
        }
        if(Vcb->MediaClassEx == CdMediaClass_DVDR ||
           Vcb->MediaClassEx == CdMediaClass_DVDpR ||
           Vcb->MediaClassEx == CdMediaClass_HD_DVDR ||
           Vcb->MediaClassEx == CdMediaClass_BDR) {
            return MediaDvdr;
        }
        if(Vcb->MediaClassEx == CdMediaClass_CDRW) {
            return MediaCdrw;
        }
        if(Vcb->MediaClassEx == CdMediaClass_DVDRW ||
           Vcb->MediaClassEx == CdMediaClass_DVDpRW ||
           Vcb->MediaClassEx == CdMediaClass_DVDRAM ||
           Vcb->MediaClassEx == CdMediaClass_HD_DVDRW ||
           Vcb->MediaClassEx == CdMediaClass_HD_DVDRAM ||
           Vcb->MediaClassEx == CdMediaClass_BDRE) {
            return MediaDvdrw;
        }
        // 
        if(Vcb->MediaClassEx == CdMediaClass_CDROM ||
           Vcb->MediaClassEx == CdMediaClass_DVDROM ||
           Vcb->MediaClassEx == CdMediaClass_HD_DVDROM ||
           Vcb->MediaClassEx == CdMediaClass_BDROM) {
            return MediaCdrom;
        }
        return MediaCdrom;
#ifdef UDF_HDD_SUPPORT
    case FILE_DEVICE_DISK_FILE_SYSTEM:
        if(Vcb->TargetDeviceObject->Characteristics & FILE_FLOPPY_DISKETTE)
            return MediaFloppy;
        if(Vcb->TargetDeviceObject->Characteristics & FILE_REMOVABLE_MEDIA)
            return MediaZip;
        return MediaHdd;
#endif //UDF_HDD_SUPPORT
    }
    return MediaUnknown;
} // end UDFGetMediaClass()

typedef ULONG
(*ptrUDFGetParameter)(
    IN PVCB Vcb, 
    IN PCWSTR Name,
    IN ULONG DefValue
    );

VOID
UDFUpdateCompatOption(
    PVCB Vcb,
    BOOLEAN Update,
    BOOLEAN UseCfg,
    PCWSTR Name,
    ULONG Flag,
    BOOLEAN Default
    )
{
    ptrUDFGetParameter UDFGetParameter = UseCfg ? UDFGetCfgParameter : UDFGetRegParameter;

    if(UDFGetParameter(Vcb, Name, Update ? ((Vcb->CompatFlags & Flag) ? TRUE : FALSE) : Default)) {
        Vcb->CompatFlags |= Flag;
    } else {
        Vcb->CompatFlags &= ~Flag;
    }
} // end UDFUpdateCompatOption()

VOID
UDFReadRegKeys(
    PVCB Vcb,
    BOOLEAN Update,
    BOOLEAN UseCfg
    )
{
    ULONG mult = 1;
    ptrUDFGetParameter UDFGetParameter = UseCfg ? UDFGetCfgParameter : UDFGetRegParameter;
    
    Vcb->DefaultRegName = UDFMediaClassName[(ULONG)UDFGetMediaClass(Vcb)].ClassName;

    // Should we use Extended FE by default ?
    Vcb->UseExtendedFE = (UCHAR)UDFGetParameter(Vcb, REG_USEEXTENDEDFE_NAME,
        Update ? Vcb->UseExtendedFE : FALSE);
    if(Vcb->UseExtendedFE != TRUE) Vcb->UseExtendedFE = FALSE;
    // What type of AllocDescs should we use
    Vcb->DefaultAllocMode = (USHORT)UDFGetParameter(Vcb, REG_DEFALLOCMODE_NAME,
        Update ? Vcb->DefaultAllocMode : ICB_FLAG_AD_SHORT);
    if(Vcb->DefaultAllocMode > ICB_FLAG_AD_LONG) Vcb->DefaultAllocMode = ICB_FLAG_AD_SHORT;
    // Default UID & GID to be set on newly created files
    Vcb->DefaultUID = UDFGetParameter(Vcb, UDF_DEFAULT_UID_NAME, Update ? Vcb->DefaultUID : -1);
    Vcb->DefaultGID = UDFGetParameter(Vcb, UDF_DEFAULT_GID_NAME, Update ? Vcb->DefaultGID : -1);
    // FE allocation charge for plain Dirs
    Vcb->FECharge = UDFGetParameter(Vcb, UDF_FE_CHARGE_NAME, Update ? Vcb->FECharge : 0);
    if(!Vcb->FECharge)
        Vcb->FECharge = UDF_DEFAULT_FE_CHARGE;
    // FE allocation charge for Stream Dirs (SDir)
    Vcb->FEChargeSDir = UDFGetParameter(Vcb, UDF_FE_CHARGE_SDIR_NAME,
        Update ? Vcb->FEChargeSDir : 0);
    if(!Vcb->FEChargeSDir)
        Vcb->FEChargeSDir = UDF_DEFAULT_FE_CHARGE_SDIR;
    // How many Deleted entries should contain Directory to make us
    // start packing it.
    Vcb->PackDirThreshold = UDFGetParameter(Vcb, UDF_DIR_PACK_THRESHOLD_NAME,
        Update ? Vcb->PackDirThreshold : 0);
    if(Vcb->PackDirThreshold == 0xffffffff)
        Vcb->PackDirThreshold = UDF_DEFAULT_DIR_PACK_THRESHOLD;
    // The binary exponent for the number of Pages to be read-ahead'ed
    // This information would be sent to System Cache Manager
    if(!Update) {
        Vcb->SystemCacheGran = (1 << UDFGetParameter(Vcb, UDF_READAHEAD_GRAN_NAME, 0)) * PAGE_SIZE;
        if(!Vcb->SystemCacheGran)
            Vcb->SystemCacheGran = UDF_DEFAULT_READAHEAD_GRAN;
    }
    // Timeouts for FreeSpaceBitMap & TheWholeDirTree flushes
    Vcb->BM_FlushPriod = UDFGetParameter(Vcb, UDF_BM_FLUSH_PERIOD_NAME,
        Update ? Vcb->BM_FlushPriod : 0);
    if(!Vcb->BM_FlushPriod) {
        Vcb->BM_FlushPriod = UDF_DEFAULT_BM_FLUSH_TIMEOUT;
    } else
    if(Vcb->BM_FlushPriod == (ULONG)-1) {
        Vcb->BM_FlushPriod = 0;
    }
    Vcb->Tree_FlushPriod = UDFGetParameter(Vcb, UDF_TREE_FLUSH_PERIOD_NAME,
        Update ? Vcb->Tree_FlushPriod : 0);
    if(!Vcb->Tree_FlushPriod) {
        Vcb->Tree_FlushPriod = UDF_DEFAULT_TREE_FLUSH_TIMEOUT;
    } else
    if(Vcb->Tree_FlushPriod == (ULONG)-1) {
        Vcb->Tree_FlushPriod = 0;
    }
    Vcb->SkipCountLimit = UDFGetParameter(Vcb, UDF_NO_UPDATE_PERIOD_NAME,
        Update ? Vcb->SkipCountLimit : 0);
    if(!Vcb->SkipCountLimit)
        Vcb->SkipCountLimit = -1;

    Vcb->SkipEjectCountLimit = UDFGetParameter(Vcb, UDF_NO_EJECT_PERIOD_NAME,
        Update ? Vcb->SkipEjectCountLimit : 3);

    if(!Update) {<--- Assuming that condition '!Update' is not redundant
        // How many threads are allowed to sodomize Disc simultaneously on each CPU
        Vcb->ThreadsPerCpu = UDFGetParameter(Vcb, UDF_FSP_THREAD_PER_CPU_NAME,
            Update ? Vcb->ThreadsPerCpu : 2);<--- Condition 'Update' is always false
        if(Vcb->ThreadsPerCpu < 2)
            Vcb->ThreadsPerCpu = UDF_DEFAULT_FSP_THREAD_PER_CPU;
    }
    // The mimimum FileSize increment when we'll decide not to allocate
    // on-disk space.
    Vcb->SparseThreshold = UDFGetParameter(Vcb, UDF_SPARSE_THRESHOLD_NAME,
        Update ? Vcb->SparseThreshold : 0);
    if(!Vcb->SparseThreshold)
        Vcb->SparseThreshold = UDF_DEFAULT_SPARSE_THRESHOLD;
    // This option is used to VERIFY all the data written. It decreases performance
    Vcb->VerifyOnWrite = UDFGetParameter(Vcb, UDF_VERIFY_ON_WRITE_NAME,
        Update ? Vcb->VerifyOnWrite : FALSE) ? TRUE : FALSE;

#ifndef UDF_READ_ONLY_BUILD
    // Should we update AttrFileTime on Attr changes
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_UPDATE_TIMES_ATTR, UDF_VCB_IC_UPDATE_ATTR_TIME, FALSE);
    // Should we update ModifyFileTime on Writes changes
    // It also affects ARCHIVE bit setting on write operations
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_UPDATE_TIMES_MOD, UDF_VCB_IC_UPDATE_MODIFY_TIME, FALSE);
    // Should we update AccessFileTime on Exec & so on.
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_UPDATE_TIMES_ACCS, UDF_VCB_IC_UPDATE_ACCESS_TIME, FALSE);
    // Should we update Archive bit
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_UPDATE_ATTR_ARCH, UDF_VCB_IC_UPDATE_ARCH_BIT, FALSE);
    // Should we update Dir's Times & Attrs on Modify
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_UPDATE_DIR_TIMES_ATTR_W, UDF_VCB_IC_UPDATE_DIR_WRITE, FALSE);
    // Should we update Dir's Times & Attrs on Access
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_UPDATE_DIR_TIMES_ATTR_R, UDF_VCB_IC_UPDATE_DIR_READ, FALSE);
    // Should we allow user to write into Read-Only Directory
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_ALLOW_WRITE_IN_RO_DIR, UDF_VCB_IC_WRITE_IN_RO_DIR, TRUE);
    // Should we allow user to change Access Time for unchanged Directory
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_ALLOW_UPDATE_TIMES_ACCS_UCHG_DIR, UDF_VCB_IC_UPDATE_UCHG_DIR_ACCESS_TIME, FALSE);
#endif //UDF_READ_ONLY_BUILD
    // Should we record Allocation Descriptors in W2k-compatible form
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_W2K_COMPAT_ALLOC_DESCS, UDF_VCB_IC_W2K_COMPAT_ALLOC_DESCS, TRUE);
    // Should we read LONG_ADs with invalid PartitionReferenceNumber (generated by Nero Instant Burner)
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_INSTANT_COMPAT_ALLOC_DESCS, UDF_VCB_IC_INSTANT_COMPAT_ALLOC_DESCS, TRUE);
    // Should we make a copy of VolumeLabel in LVD
    // usually only PVD is updated
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_W2K_COMPAT_VLABEL, UDF_VCB_IC_W2K_COMPAT_VLABEL, TRUE);
    // Should we handle or ignore HW_RO flag
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_HANDLE_HW_RO, UDF_VCB_IC_HW_RO, FALSE);
    // Should we handle or ignore SOFT_RO flag
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_HANDLE_SOFT_RO, UDF_VCB_IC_SOFT_RO, TRUE);

    // Check if we should generate UDF-style or OS-style DOS-names
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_OS_NATIVE_DOS_NAME, UDF_VCB_IC_OS_NATIVE_DOS_NAME, FALSE);
#ifndef UDF_READ_ONLY_BUILD
    // should we force FO_WRITE_THROUGH on removable media
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_FORCE_WRITE_THROUGH_NAME, UDF_VCB_IC_FORCE_WRITE_THROUGH, 
                          (Vcb->TargetDeviceObject->Characteristics & FILE_REMOVABLE_MEDIA) ? TRUE : FALSE
                         );
#endif //UDF_READ_ONLY_BUILD
    // Should we ignore FO_SEQUENTIAL_ONLY
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_IGNORE_SEQUENTIAL_IO, UDF_VCB_IC_IGNORE_SEQUENTIAL_IO, FALSE);
// Force Read-only mounts
#ifndef UDF_READ_ONLY_BUILD
    UDFUpdateCompatOption(Vcb, Update, UseCfg, UDF_FORCE_HW_RO, UDF_VCB_IC_FORCE_HW_RO, FALSE);
#else //UDF_READ_ONLY_BUILD
    Vcb->CompatFlags |= UDF_VCB_IC_FORCE_HW_RO;
#endif //UDF_READ_ONLY_BUILD
    // Check if we should send FLUSH request for File/Dir down to
    // underlaying driver
    if(UDFGetParameter(Vcb, UDF_FLUSH_MEDIA,Update ? Vcb->FlushMedia : FALSE)) {
        Vcb->FlushMedia = TRUE;
    } else {
        Vcb->FlushMedia = FALSE;
    }
    // compare data from packet with data to be writen there
    // before physical writing
    if(!UDFGetParameter(Vcb, UDF_COMPARE_BEFORE_WRITE, Update ? Vcb->DoNotCompareBeforeWrite : FALSE)) {
        Vcb->DoNotCompareBeforeWrite = TRUE;
    } else {
        Vcb->DoNotCompareBeforeWrite = FALSE;
    }
    if(!Update)  {
        if(UDFGetParameter(Vcb, UDF_CHAINED_IO, TRUE)) {
            Vcb->CacheChainedIo = TRUE;
        }

        if(UDFGetParameter(Vcb, UDF_FORCE_MOUNT_ALL, FALSE)) {
            Vcb->VCBFlags |= UDF_VCB_FLAGS_RAW_DISK;
        }
        // Should we show Blank.Cd file on damaged/unformatted,
        // but UDF-compatible disks
        Vcb->ShowBlankCd = (UCHAR)UDFGetParameter(Vcb, UDF_SHOW_BLANK_CD, FALSE);
        if(Vcb->ShowBlankCd) {
            Vcb->CompatFlags |= UDF_VCB_IC_SHOW_BLANK_CD;
            if(Vcb->ShowBlankCd > 2) {
                Vcb->ShowBlankCd = 2;
            }
        }
        // Should we wait util CD device return from
        // Becoming Ready state
        if(UDFGetParameter(Vcb, UDF_WAIT_CD_SPINUP, TRUE)) {
            Vcb->CompatFlags |= UDF_VCB_IC_WAIT_CD_SPINUP;
        }
        // Should we remenber bad VDS locations during mount
        // Caching will improve mount performance on bad disks, but
        // will degrade mauntability of unreliable discs
        if(UDFGetParameter(Vcb, UDF_CACHE_BAD_VDS, TRUE)) {
            Vcb->CompatFlags |= UDF_VCB_IC_CACHE_BAD_VDS;
        }

        // Set partitially damaged volume mount mode
        Vcb->PartitialDamagedVolumeAction = (UCHAR)UDFGetParameter(Vcb, UDF_PART_DAMAGED_BEHAVIOR, UDF_PART_DAMAGED_RW);
        if(Vcb->PartitialDamagedVolumeAction > 2) {
            Vcb->PartitialDamagedVolumeAction = UDF_PART_DAMAGED_RW;
        }

        // Set partitially damaged volume mount mode
        Vcb->NoFreeRelocationSpaceVolumeAction = (UCHAR)UDFGetParameter(Vcb, UDF_NO_SPARE_BEHAVIOR, UDF_PART_DAMAGED_RW);
        if(Vcb->NoFreeRelocationSpaceVolumeAction > 1) {
            Vcb->NoFreeRelocationSpaceVolumeAction = UDF_PART_DAMAGED_RW;
        }

        // Set dirty volume mount mode
        if(UDFGetParameter(Vcb, UDF_DIRTY_VOLUME_BEHAVIOR, UDF_PART_DAMAGED_RO)) {
            Vcb->CompatFlags |= UDF_VCB_IC_DIRTY_RO;
        }

        mult = UDFGetParameter(Vcb, UDF_CACHE_SIZE_MULTIPLIER, 1);
        if(!mult) mult = 1;
        Vcb->WCacheMaxBlocks *= mult;
        Vcb->WCacheMaxFrames *= mult;

        if(UDFGetParameter(Vcb, UDF_USE_EJECT_BUTTON, TRUE)) {
            Vcb->UseEvent = TRUE;
        }
    }
    return;
} // end UDFReadRegKeys()

ULONG
UDFGetRegParameter(
    IN PVCB Vcb, 
    IN PCWSTR Name,
    IN ULONG DefValue
    )
{
    return UDFRegCheckParameterValue(&(UDFGlobalData.SavedRegPath),
                                     Name,
                                     Vcb ? &(Vcb->TargetDevName) : NULL,
                                     Vcb ? Vcb->DefaultRegName : NULL,
                                     DefValue);
} // end UDFGetRegParameter()

ULONG
UDFGetCfgParameter(
    IN PVCB Vcb, 
    IN PCWSTR Name,
    IN ULONG DefValue
    )
{
    ULONG len;
    CHAR NameA[128];
    ULONG ret_val=0;
    CHAR a;
    BOOLEAN wait_name=TRUE;
    BOOLEAN wait_val=FALSE;
    BOOLEAN wait_nl=FALSE;
    ULONG radix=10;
    ULONG i;

    PUCHAR Cfg    = Vcb->Cfg;
    ULONG  Length = Vcb->CfgLength;

    if(!Cfg || !Length)
        return DefValue;

    len = wcslen(Name);
    if(len >= sizeof(NameA))
        return DefValue;
    sprintf(NameA, "%S", Name);

    for(i=0; i<Length; i++) {
        a=Cfg[i];
        switch(a) {
        case '\n':
        case '\r':
        case ',':
            if(wait_val)
                return DefValue;
            continue;
        case ';':
        case '#':
        case '[': // ignore sections for now, treat as comment
            if(!wait_name)
                return DefValue;
            wait_nl = TRUE;
            continue;
        case '=':
            if(!wait_val)
                return DefValue;
            continue;
        case ' ':
        case '\t':
            continue;
        default:
            if(wait_nl)
                continue;
        }
        if(wait_name) {
            if(i+len+2 > Length)
                return DefValue;
            if(RtlCompareMemory(Cfg+i, NameA, len) == len) {
                a=Cfg[i+len];
                switch(a) {
                case '\n':
                case '\r':
                case ',':
                case ';':
                case '#':
                    return DefValue;
                case '=':
                case ' ':
                case '\t':
                    break;
                default:
                    wait_nl = TRUE;
                    wait_val = FALSE;
                    i+=len;
                    continue;
                }
                wait_name = FALSE;
                wait_nl = FALSE;
                wait_val = TRUE;
                i+=len;
                
            } else {
                wait_nl = TRUE;
            }
            continue;
        }
        if(wait_val) {
            if(i+3 > Length) {
                if(a=='0' && Cfg[i+1]=='x') {
                    i+=2;
                    radix=16;
                }
            }
            if(i >= Length) {
                return DefValue;
            }
            while(i<Length) {
                a=Cfg[i];
                switch(a) {
                case '\n':
                case '\r':
                case ' ':
                case '\t':
                case ',':
                case ';':
                case '#':
                    if(wait_val)
                        return DefValue;
                    return ret_val;
                }
                if(a >= '0' && a <= '9') {
                    a -= '0';
                } else {
                    if(radix != 16)
                        return DefValue;
                    if(a >= 'a' && a <= 'f') {
                        a -= 'a';
                    } else
                    if(a >= 'A' && a <= 'F') {
                        a -= 'A';
                    } else {
                        return DefValue;
                    }
                    a += 0x0a;
                }
                ret_val = ret_val*radix + a;
                wait_val = FALSE;
                i++;
            }
            return ret_val;
        }
    }
    return DefValue;

} // end UDFGetCfgParameter()

VOID
UDFReleaseVCB(
    PVCB  Vcb
    )
{
    LARGE_INTEGER delay;
    UDFPrint(("UDFReleaseVCB\n"));

    delay.QuadPart = -500000; // 0.05 sec
    while(Vcb->PostedRequestCount) {
        UDFPrint(("UDFReleaseVCB: PostedRequestCount = %d\n", Vcb->PostedRequestCount));
        // spin until all queues IRPs are processed
        KeDelayExecutionThread(KernelMode, FALSE, &delay);
        delay.QuadPart -= 500000; // grow delay 0.05 sec
    }

    _SEH2_TRY {
        UDFPrint(("UDF: Flushing buffers\n"));
        UDFVRelease(Vcb);
        WCacheFlushAll__(&(Vcb->FastCache),Vcb);
        WCacheRelease__(&(Vcb->FastCache));

    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

#ifdef UDF_DBG
    _SEH2_TRY {
        if (!ExIsResourceAcquiredShared(&UDFGlobalData.GlobalDataResource)) {
            UDFPrint(("UDF: attempt to access to not protected data\n"));
            UDFPrint(("UDF: UDFGlobalData\n"));
            BrutePoint();
        }
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;
#endif

    _SEH2_TRY {
        RemoveEntryList(&(Vcb->NextVCB));
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

/*    _SEH2_TRY {
        if(Vcb->VCBFlags & UDF_VCB_FLAGS_STOP_WAITER_EVENT)
            KeWaitForSingleObject(&(Vcb->WaiterStopped), Executive, KernelMode, FALSE, NULL);
            Vcb->VCBFlags &= ~UDF_VCB_FLAGS_STOP_WAITER_EVENT;
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    }*/

    _SEH2_TRY {
        UDFPrint(("UDF: Delete resources\n"));
        UDFDeleteResource(&(Vcb->VCBResource));
        UDFDeleteResource(&(Vcb->BitMapResource1));
        UDFDeleteResource(&(Vcb->FcbListResource));
        UDFDeleteResource(&(Vcb->FileIdResource));
        UDFDeleteResource(&(Vcb->DlocResource));
        UDFDeleteResource(&(Vcb->DlocResource2));
        UDFDeleteResource(&(Vcb->FlushResource));
        UDFDeleteResource(&(Vcb->PreallocResource));
        UDFDeleteResource(&(Vcb->IoResource));
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

    _SEH2_TRY {
        UDFPrint(("UDF: Cleanup VCB\n"));
        ASSERT(IsListEmpty(&(Vcb->NextNotifyIRP)));
        FsRtlNotifyUninitializeSync(&(Vcb->NotifyIRPMutex));
        UDFCleanupVCB(Vcb);
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

    _SEH2_TRY {
        UDFPrint(("UDF: Delete DO\n"));
        IoDeleteDevice(Vcb->VCBDeviceObject);
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

} // end UDFReleaseVCB()

/*
    Read DWORD from Registry
*/
ULONG
UDFRegCheckParameterValue(
    IN PUNICODE_STRING RegistryPath,
    IN PCWSTR Name,
    IN PUNICODE_STRING PtrVolumePath,
    IN PCWSTR DefaultPath,
    IN ULONG DefValue
    )
{
    NTSTATUS          status;

    ULONG             val = DefValue;

    UNICODE_STRING    paramStr;
    UNICODE_STRING    defaultParamStr;
    UNICODE_STRING    paramPathUnknownStr;

    UNICODE_STRING    paramSuffix;
    UNICODE_STRING    paramPath;
    UNICODE_STRING    paramPathUnknown;
    UNICODE_STRING    paramDevPath;
    UNICODE_STRING    defaultParamPath;

    _SEH2_TRY {

        paramPath.Buffer = NULL;
        paramDevPath.Buffer = NULL;
        paramPathUnknown.Buffer = NULL;
        defaultParamPath.Buffer = NULL;

        // First append \Parameters to the passed in registry path
        // Note, RtlInitUnicodeString doesn't allocate memory
        RtlInitUnicodeString(&paramStr, L"\\Parameters");
        RtlInitUnicodeString(&paramPath, NULL);

        RtlInitUnicodeString(&paramPathUnknownStr, REG_DEFAULT_UNKNOWN);
        RtlInitUnicodeString(&paramPathUnknown, NULL);
        
        paramPathUnknown.MaximumLength = RegistryPath->Length + paramPathUnknownStr.Length + paramStr.Length + sizeof(WCHAR);
        paramPath.MaximumLength = RegistryPath->Length + paramStr.Length + sizeof(WCHAR);

        paramPath.Buffer = (PWCH)MyAllocatePool__(PagedPool, paramPath.MaximumLength);
        if(!paramPath.Buffer) {
            UDFPrint(("UDFCheckRegValue: couldn't allocate paramPath\n"));
            try_return(val = DefValue);
        }
        paramPathUnknown.Buffer = (PWCH)MyAllocatePool__(PagedPool, paramPathUnknown.MaximumLength);
        if(!paramPathUnknown.Buffer) {
            UDFPrint(("UDFCheckRegValue: couldn't allocate paramPathUnknown\n"));
            try_return(val = DefValue);
        }

        RtlZeroMemory(paramPath.Buffer, paramPath.MaximumLength);
        status = RtlAppendUnicodeToString(&paramPath, RegistryPath->Buffer);
        if(!NT_SUCCESS(status)) {
            try_return(val = DefValue);
        }
        status = RtlAppendUnicodeToString(&paramPath, paramStr.Buffer);
        if(!NT_SUCCESS(status)) {
            try_return(val = DefValue);
        }
        UDFPrint(("UDFCheckRegValue: (1) |%S|\n", paramPath.Buffer));

        RtlZeroMemory(paramPathUnknown.Buffer, paramPathUnknown.MaximumLength);
        status = RtlAppendUnicodeToString(&paramPathUnknown, RegistryPath->Buffer);
        if(!NT_SUCCESS(status)) {
            try_return(val = DefValue);
        }
        status = RtlAppendUnicodeToString(&paramPathUnknown, paramStr.Buffer);
        if(!NT_SUCCESS(status)) {
            try_return(val = DefValue);
        }
        status = RtlAppendUnicodeToString(&paramPathUnknown, paramPathUnknownStr.Buffer);
        if(!NT_SUCCESS(status)) {
            try_return(val = DefValue);
        }
        UDFPrint(("UDFCheckRegValue: (2) |%S|\n", paramPathUnknown.Buffer));

        // First append \Parameters\Default_XXX to the passed in registry path
        if(DefaultPath) {
            RtlInitUnicodeString(&defaultParamStr, DefaultPath);
            RtlInitUnicodeString(&defaultParamPath, NULL);
            defaultParamPath.MaximumLength = paramPath.Length + defaultParamStr.Length + sizeof(WCHAR);
            defaultParamPath.Buffer = (PWCH)MyAllocatePool__(PagedPool, defaultParamPath.MaximumLength);
            if(!defaultParamPath.Buffer) {
                UDFPrint(("UDFCheckRegValue: couldn't allocate defaultParamPath\n"));
                try_return(val = DefValue);
            }

            RtlZeroMemory(defaultParamPath.Buffer, defaultParamPath.MaximumLength);
            status = RtlAppendUnicodeToString(&defaultParamPath, paramPath.Buffer);
            if(!NT_SUCCESS(status)) {
                try_return(val = DefValue);
            }
            status = RtlAppendUnicodeToString(&defaultParamPath, defaultParamStr.Buffer);
            if(!NT_SUCCESS(status)) {
                try_return(val = DefValue);
            }
            UDFPrint(("UDFCheckRegValue: (3) |%S|\n", defaultParamPath.Buffer));
        }

        if(PtrVolumePath) {
            paramSuffix = *PtrVolumePath;
        } else {
            RtlInitUnicodeString(&paramSuffix, NULL);
        }

        RtlInitUnicodeString(&paramDevPath, NULL);
        // now build the device specific path
        paramDevPath.MaximumLength = paramPath.Length + paramSuffix.Length + sizeof(WCHAR);
        paramDevPath.Buffer = (PWCH)MyAllocatePool__(PagedPool, paramDevPath.MaximumLength);
        if(!paramDevPath.Buffer) {
            try_return(val = DefValue);
        }

        RtlZeroMemory(paramDevPath.Buffer, paramDevPath.MaximumLength);
        status = RtlAppendUnicodeToString(&paramDevPath, paramPath.Buffer);
        if(!NT_SUCCESS(status)) {
            try_return(val = DefValue);
        }
        if(paramSuffix.Buffer) {
            status = RtlAppendUnicodeToString(&paramDevPath, paramSuffix.Buffer);
            if(!NT_SUCCESS(status)) {
                try_return(val = DefValue);
            }
        }

        UDFPrint(( " Parameter = %ws\n", Name));

        {
            HKEY hk = NULL;
            status = RegTGetKeyHandle(NULL, RegistryPath->Buffer, &hk);
            if(NT_SUCCESS(status)) {
                RegTCloseKeyHandle(hk);
            }
        }


        // *** Read GLOBAL_DEFAULTS from
        // "\DwUdf\Parameters_Unknown\"

        status = RegTGetDwordValue(NULL, paramPath.Buffer, Name, &val);<--- status is assigned

        // *** Read DEV_CLASS_SPEC_DEFAULTS (if any) from
        // "\DwUdf\Parameters_%DevClass%\"

        if(DefaultPath) {
            status = RegTGetDwordValue(NULL, defaultParamPath.Buffer, Name, &val);<--- status is assigned
        }

        // *** Read DEV_SPEC_PARAMS from (if device supports GetDevName)
        // "\DwUdf\Parameters\%DevName%\"

        status = RegTGetDwordValue(NULL, paramDevPath.Buffer, Name, &val);<--- status is overwritten<--- status is overwritten

try_exit:   NOTHING;

    } _SEH2_FINALLY {

        if(DefaultPath && defaultParamPath.Buffer) {
            MyFreePool__(defaultParamPath.Buffer);
        }
        if(paramPath.Buffer) {
            MyFreePool__(paramPath.Buffer);
        }
        if(paramDevPath.Buffer) {
            MyFreePool__(paramDevPath.Buffer);
        }
        if(paramPathUnknown.Buffer) {
            MyFreePool__(paramPathUnknown.Buffer);
        }
    } _SEH2_END;

    UDFPrint(( "UDFCheckRegValue: %ws for drive %s is %x\n\n", Name, PtrVolumePath, val));
    return val;
} // end UDFRegCheckParameterValue()

/*
Routine Description:
    This routine is called to initialize an IrpContext for the current
    UDFFS request.  The IrpContext is on the stack and we need to initialize
    it for the current request.  The request is a close operation.

Arguments:

    IrpContext - IrpContext to initialize.

    IrpContextLite - source for initialization

Return Value:

    None

*/
VOID
UDFInitializeIrpContextFromLite(
    OUT PtrUDFIrpContext    *IrpContext,
    IN PtrUDFIrpContextLite IrpContextLite
    )
{
    (*IrpContext) = UDFAllocateIrpContext(NULL, IrpContextLite->RealDevice);
    //  Zero and then initialize the structure.

    //  Major/Minor Function codes
    (*IrpContext)->MajorFunction = IRP_MJ_CLOSE;
    (*IrpContext)->Fcb = IrpContextLite->Fcb;
    (*IrpContext)->TreeLength = IrpContextLite->TreeLength;
    (*IrpContext)->IrpContextFlags |= (IrpContextLite->IrpContextFlags & ~UDF_IRP_CONTEXT_NOT_FROM_ZONE);

    //  Set the wait parameter
    UDFSetFlag( (*IrpContext)->IrpContextFlags, UDF_IRP_CONTEXT_CAN_BLOCK );

    return;
} // end UDFInitializeIrpContextFromLite()

/*
Routine Description:
    This routine is called to initialize an IrpContext for the current
    UDFFS request.  The IrpContext is on the stack and we need to initialize
    it for the current request.  The request is a close operation.

Arguments:

    IrpContext - IrpContext to initialize.

    IrpContextLite - source for initialization

Return Value:

    None

*/
NTSTATUS
UDFInitializeIrpContextLite(
    OUT PtrUDFIrpContextLite *IrpContextLite,
    IN PtrUDFIrpContext    IrpContext,
    IN PtrUDFFCB           Fcb
    )
{
    PtrUDFIrpContextLite LocalIrpContextLite = (PtrUDFIrpContextLite)MyAllocatePool__(NonPagedPool,sizeof(UDFIrpContextLite));
    if(!LocalIrpContextLite)
        return STATUS_INSUFFICIENT_RESOURCES;
    //  Zero and then initialize the structure.
    RtlZeroMemory( LocalIrpContextLite, sizeof( UDFIrpContextLite ));

    LocalIrpContextLite->NodeIdentifier.NodeType  = UDF_NODE_TYPE_IRP_CONTEXT_LITE;
    LocalIrpContextLite->NodeIdentifier.NodeSize  = sizeof(UDFIrpContextLite);

    LocalIrpContextLite->Fcb = Fcb;
    LocalIrpContextLite->TreeLength = IrpContext->TreeLength;
    //  Copy RealDevice for workque algorithms.
    LocalIrpContextLite->RealDevice = IrpContext->TargetDeviceObject;
    LocalIrpContextLite->IrpContextFlags = IrpContext->IrpContextFlags;
    *IrpContextLite = LocalIrpContextLite;

    return STATUS_SUCCESS;
} // end UDFInitializeIrpContextLite()

NTSTATUS
NTAPI
UDFQuerySetEA(
    PDEVICE_OBJECT DeviceObject,       // the logical volume device object
    PIRP           Irp                 // I/O Request Packet
    )
{
    NTSTATUS         RC = STATUS_SUCCESS;
//    PtrUDFIrpContext PtrIrpContext = NULL;
    BOOLEAN          AreWeTopLevel = FALSE;

    UDFPrint(("UDFQuerySetEA: \n"));

    FsRtlEnterFileSystem();
    ASSERT(DeviceObject);
    ASSERT(Irp);

    // set the top level context
    AreWeTopLevel = UDFIsIrpTopLevel(Irp);

    RC = STATUS_EAS_NOT_SUPPORTED;
    Irp->IoStatus.Status = RC;
    Irp->IoStatus.Information = 0;
    // complete the IRP
    IoCompleteRequest(Irp, IO_DISK_INCREMENT);

    if(AreWeTopLevel) {
        IoSetTopLevelIrp(NULL);
    }

    FsRtlExitFileSystem();

    return(RC);
} // end UDFQuerySetEA()

ULONG
UDFIsResourceAcquired(
    IN PERESOURCE Resource
    )
{
    ULONG ReAcqRes =
        ExIsResourceAcquiredExclusiveLite(Resource) ? 1 :
        (ExIsResourceAcquiredSharedLite(Resource) ? 2 : 0);
    return ReAcqRes;
} // end UDFIsResourceAcquired()

BOOLEAN
UDFAcquireResourceExclusiveWithCheck(
    IN PERESOURCE Resource
    )
{
    ULONG ReAcqRes =
        ExIsResourceAcquiredExclusiveLite(Resource) ? 1 :
        (ExIsResourceAcquiredSharedLite(Resource) ? 2 : 0);
    if(ReAcqRes) {
        UDFPrint(("UDFAcquireResourceExclusiveWithCheck: ReAcqRes, %x\n", ReAcqRes));
    } else {
//        BrutePoint();
    }

    if(ReAcqRes == 1) {
        // OK
    } else
    if(ReAcqRes == 2) {
        UDFPrint(("UDFAcquireResourceExclusiveWithCheck: !!! Shared !!!\n"));
        //BrutePoint();
    } else {
        UDFAcquireResourceExclusive(Resource, TRUE);
        return TRUE;
    }
    return FALSE;
} // end UDFAcquireResourceExclusiveWithCheck()

BOOLEAN
UDFAcquireResourceSharedWithCheck(
    IN PERESOURCE Resource
    )
{
    ULONG ReAcqRes =
        ExIsResourceAcquiredExclusiveLite(Resource) ? 1 :
        (ExIsResourceAcquiredSharedLite(Resource) ? 2 : 0);
    if(ReAcqRes) {
        UDFPrint(("UDFAcquireResourceSharedWithCheck: ReAcqRes, %x\n", ReAcqRes));
/*    } else {
        BrutePoint();*/
    }

    if(ReAcqRes == 2) {
        // OK
    } else
    if(ReAcqRes == 1) {
        UDFPrint(("UDFAcquireResourceSharedWithCheck: Exclusive\n"));
        //BrutePoint();
    } else {
        UDFAcquireResourceShared(Resource, TRUE);
        return TRUE;
    }
    return FALSE;
} // end UDFAcquireResourceSharedWithCheck()

NTSTATUS
UDFWCacheErrorHandler(
    IN PVOID Context,
    IN PWCACHE_ERROR_CONTEXT ErrorInfo
    )
{
    InterlockedIncrement((PLONG)&(((PVCB)Context)->IoErrorCounter));
    return ErrorInfo->Status;
}

#include "Include/misc_common.cpp"
#include "Include/regtools.cpp"